# Treemodel

**URL:** <https://www.ruby-forum.com/t/treemodel/108491>\
**Category:** Ruby-Gnome 2\
**Created:** [July 4, 2007, 2:29am UTC](https://www.ruby-forum.com/t/treemodel/108491 "2007-07-04T02:29:55Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 4, 2007, 2:29am UTC](https://www.ruby-forum.com/t/treemodel/108491/1 "2007-07-04T02:29:55Z")

</div>

Is it possible to create a custom treemodel in ruby?  
I’ve seen that other people want to do this but have not heard if  
anyone has been successful.

If it is possible, does anyone have an example to share?

I have data in an sql database that I rather not copy into a treestore  
in order to display in a treeview.

Thanks,  
Alex Norman

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 6, 2007, 2:00pm UTC](https://www.ruby-forum.com/t/treemodel/108491/2 "2007-07-06T14:00:25Z")

</div>

Hi,

From: Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Tue, 3 Jul 2007 17:28:50 -0700

> Is it possible to create a custom treemodel in ruby?

Unfortunately it isn’t.  
This functionality is not implemented yet, because it has some  
technical difficulties.

– Masahiro S.

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 7, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/treemodel/108491/3 "2007-07-07T21:25:12Z")

</div>

Hmm, what are the technical difficulties? This would be really useful  
for me and a lot of other people I assume.  
either way i think the documentation should indicate that it isn’t  
possible to create custom treemodels yet.

-Alex

On 7/6/07, Masahiro S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> [ruby-gnome2-devel-en List Signup and Options](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 8, 2007, 3:32am UTC](https://www.ruby-forum.com/t/treemodel/108491/4 "2007-07-08T03:32:50Z")

</div>

Hi,

On Sat, 7 Jul 2007 12:24:41 -0700  
Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hmm, what are the technical difficulties? This would be really useful  
> for me and a lot of other people I assume.

Could you implement them?  
I hope someone contribute it ;).

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 12, 2007, 11:29am UTC](https://www.ruby-forum.com/t/treemodel/108491/5 "2007-07-12T11:29:58Z")

</div>

> > Hmm, what are the technical difficulties? This would be really useful  
> > for me and a lot of other people I assume.
> 
> Could you implement them?  
> I hope someone contribute it ;).

I’d like to help (also very keen to create a couple of custom cell  
renderers and tree models) but not sure what the first steps should be.  
How should we go about implementing this?

All the best,  
Andrew

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 12, 2007, 2:40pm UTC](https://www.ruby-forum.com/t/treemodel/108491/6 "2007-07-12T14:40:09Z")

</div>

On Sun, 2007-07-08 at 10:31 +0900, Masao M. wrote:

> On Sat, 7 Jul 2007 12:24:41 -0700  
> Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Hmm, what are the technical difficulties? This would be really useful  
> > for me and a lot of other people I assume.
> 
> Could you implement them?  
> I hope someone contribute it ;).

I had a look at doing this about a year ago, but after looking through  
all the docuentation I could find, and looking through quite a bit of  
source code, I ended up thoroughly lost on where to start. I couldn’t  
get my head around how the macros were used to create a GTK object and  
expose it as a subclassable Ruby object.

I would also really like to write custom TreeModels in Ruby, and I’d be  
happy to help with the implementation.

My initial thought was to create something like an SubclassableTreeModel  
whose C implementation was simply calls through to a ruby subclass.  
Would that be a viable approach?

bye  
John

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 13, 2007, 12:20pm UTC](https://www.ruby-forum.com/t/treemodel/108491/7 "2007-07-13T12:20:37Z")

</div>

> My initial thought was to create something like an SubclassableTreeModel  
> whose C implementation was simply calls through to a ruby subclass.  
> Would that be a viable approach?

That sounds good to me! But how would the C methods in the  
SubclassableTreeModel delegate to the ruby methods?

All the best,  
Andrew

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 13, 2007, 1:41pm UTC](https://www.ruby-forum.com/t/treemodel/108491/8 "2007-07-13T13:41:14Z")

</div>

i also use treemodels to view mysql data,  
i copy the data into the treemodel (sync the data back to the sql server  
on treeview events like destroy and stuff), and let the treemodel do the  
sorting and filtering (on user events)  
and i tested it on different scenarios, and my conclusion was that this  
method is a lot faster than doing the filtering and sorting with the  
mysql server, that’s over 3000 rows.  
i think your implementation might bring a bit of confort to the ruby  
coders in the same situation, but for larger amount of data they’ll have  
to go the old way.

and of course i’m also very interested in your solution.

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 13, 2007, 2:42pm UTC](https://www.ruby-forum.com/t/treemodel/108491/9 "2007-07-13T14:42:59Z")

</div>

Looks like we can base this around the following C code:

rb\_funcall(self, rb\_intern(“ruby\_method\_name”), number\_of\_args, args…)

within each ‘subclassable’ method, to call ruby code. No idea how to get  
the “self” reference, though (ie. the Ruby object that we should  
delegate to), from within the C subclass - any ideas? There must be a  
way…

It would be great, in the long run, to produce a few handy classes like  
subclassableWidget, SubclassableCellRenderer, SubclassableTreeModel etc.

Best,  
Andrew

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 13, 2007, 7:59pm UTC](https://www.ruby-forum.com/t/treemodel/108491/10 "2007-07-13T19:59:19Z")

</div>

Hi,

From: Alex [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date: Sat, 7 Jul 2007 12:24:41 -0700

> Hmm, what are the technical difficulties?

As always, one big problem is memory management.  
If we represent nodes of treemodel as ruby objects, we need to manage  
them with ruby’s garbage collection. But they are refered by  
GtkTreeIter and we cannot keep track of the usages of GtkTreeIter.  
After all, I don’t know the precise condition when we should protect a  
node from garbage collection and when we shouldn’t.

Another reason is that we don’t have satisfactory basis for  
implementing gobject types in ruby yet. I think we need it first  
before supporting custom treemodel.

– Masahiro S.

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 14, 2007, 10:14pm UTC](https://www.ruby-forum.com/t/treemodel/108491/11 "2007-07-14T22:14:29Z")

</div>

On Sat, 2007-07-14 at 02:57 +0900, Masahiro S. wrote:

> > Hmm, what are the technical difficulties?
> 
> As always, one big problem is memory management.  
> If we represent nodes of treemodel as ruby objects, we need to manage  
> them with ruby’s garbage collection. But they are refered by  
> GtkTreeIter and we cannot keep track of the usages of GtkTreeIter.  
> After all, I don’t know the precise condition when we should protect a  
> node from garbage collection and when we shouldn’t.

I’m thinking of the Java Swing TreeModel interface. A TreeModel subclass  
would have it’s own (Ruby) storage mechanism for objects in the tree.  
Say the storage mechanism used Array instances. Any nodes in the tree  
would end up as items in arrays, which I think would take care of one  
part of the garbage collection issue.

The other part could possibly be to have Ruby iterator objects that act  
as wrappers for GtkTreeIter objects. That way, if a ruby node has been  
removed from the TreeModel’s storage mechanism but an iterator still  
refers to it, the iterator will be bound to a Ruby object and so it  
won’t be garbage collected until the iterator object is ready to be  
garbage collected.

> Another reason is that we don’t have satisfactory basis for  
> implementing gobject types in ruby yet. I think we need it first  
> before supporting custom treemodel.

I won’t make a suggestion here, because I don’t know enough about how  
gobject works 😉

bye  
John

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)

---

<div class="post-metadata">

**Author:** ![alex](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/db5fbb/32.png) [@alex](https://www.ruby-forum.com/u/alex)\
**Post date:** [July 14, 2007, 10:24pm UTC](https://www.ruby-forum.com/t/treemodel/108491/12 "2007-07-14T22:24:29Z")

</div>

On Fri, 2007-07-13 at 12:20 +0200, Andrew Whyte wrote:

> > My initial thought was to create something like an SubclassableTreeModel  
> > whose C implementation was simply calls through to a ruby subclass.  
> > Would that be a viable approach?
> 
> That sounds good to me! But how would the C methods in the  
> SubclassableTreeModel delegate to the ruby methods?

There would need to be a GTK object which has (in the loose sense) a  
ruby object. Any calls to the GTK object GtkTreeModel methods would  
forward to specific methods in the ruby object, using the normal Ruby  
method call mechanism.

Maybe a more idiomatic way to do it would be that the GTK object is  
wrapped in a Module, and a class that wants to be a TreeModel includes  
the relevant module and then implements the methods expected by the GTK  
object.

Does that make sense?

bye  
John

* * *

This [SF.net](http://SF.net) email is sponsored by DB2 Express  
Download DB2 Express C - the FREE version of DB2 express and take  
control of your XML. No limits. Just data. Click to get it now.  
[http://sourceforge.net/powerbar/db2/](http://sourceforge.net/powerbar/db2/)
