# New wxTree method

**URL:** <https://www.ruby-forum.com/t/new-wxtree-method/120111>\
**Category:** wxRuby\
**Created:** [November 13, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/new-wxtree-method/120111 "2007-11-13T21:05:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 13, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/new-wxtree-method/120111/1 "2007-11-13T21:05:22Z")

</div>

Here’s my first try at a contribution to the wxRuby project:  
The tree control seemed to lack a more Ruby-ish way to add a bulk of  
items to a tree control (and to easily connect a data structure to a  
tree). Since tree view can be seen as an array of arrays, I simply added  
a method called append\_array (see attached file). With it, you can add a  
full tree as simple as:

* * *

## root = treectrl.add\_root(“Root”) treectrl.append\_array(root, [“one”, “two”, [“sub1”, “sub2”], “three”])

It also gives your objects the possibility to interact a bit more with  
the tree item, if needed. See the docs for the method.

My plans are also to add at least an update\_array\_item() method, to be  
able to refresh an object’s visual tree state dynamically. However, the  
wxRuby docs for TreeCtrl#set\_item\_image isn’t in Ruby syntax, so I don’t  
know how to figure out the last parameter (which I need).

Would this be an useful addition to the existing wxRuby tree control?  
Comments are welcome!

Best regards,

Jari W.

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 14, 2007, 11:31pm UTC](https://www.ruby-forum.com/t/new-wxtree-method/120111/2 "2007-11-14T23:31:15Z")

</div>

Hi Jari

Jari W. wrote:

> * * *

Thanks, I like this. If it’s OK with you, I’m going to add it to  
wxSugar, where there are some other convenient methods. Things to  
consider in developing this approach might be:

- How can we create a more general “nice” API for bulk adding items to  
all multi-item classes, like ListBox, ListCtrl etc
- It would be useful to have a way of adding item data, which I usually  
need to use with these type of classes

> My plans are also to add at least an update\_array\_item() method, to be  
> able to refresh an object’s visual tree state dynamically. However,  
> the wxRuby docs for TreeCtrl#set\_item\_image isn’t in Ruby syntax, so I  
> don’t know how to figure out the last parameter (which I need).  
> The values are Wx::TREE\_ITEM\_ICON\_EXPANDED, Wx::TREE\_ITEM\_ICON\_NORMAL  
> etc - see samples/treectrl/ for an example of their usage. The docs  
> needed a little tidy-up, thanks for the report.

On a tangent, it would also be nice to have a way to remember and  
reestore a TreeCtrl’s state in terms of the expanded and collapsed  
items. I’ve written something that does this for an app, but I wonder if  
it would be more generally useful.

alex

PS - I’ve just committed the whole set of Graphics\* classes (except  
Renderer, which didn’t appear useful in Ruby) to SVN trunk. The  
transforms (shears etc), gradient fills and so on work really  
impressively, and all anti-aliased

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 15, 2007, 12:50am UTC](https://www.ruby-forum.com/t/new-wxtree-method/120111/3 "2007-11-15T00:50:45Z")

</div>

> On a tangent, it would also be nice to have a way to remember and  
> reestore a TreeCtrl’s state in terms of the expanded and collapsed  
> items. I’ve written something that does this for an app, but I wonder if  
> it would be more generally useful.

I would really like to see that functionality added, I have thought  
about adding something similar but it is very low on my priority list.

Sean

---

<div class="post-metadata">

**Author:** ![Jari\_W](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a698b9/32.png) [@Jari\_W](https://www.ruby-forum.com/u/Jari_W)\
**Post date:** [November 15, 2007, 12:08pm UTC](https://www.ruby-forum.com/t/new-wxtree-method/120111/4 "2007-11-15T12:08:48Z")

</div>

Alex F. wrote:

> Thanks, I like this. If it’s OK with you, I’m going to add it to  
> wxSugar, where there are some other convenient methods.

That sounds good, but I need to first add a small thing to the  
tree\_item\_ref callback method that I missed. When the object  
(optionally) receives the info about the treeitem id, it has to get the  
TreeCtrl object as well. Otherwise the object can’t reference the tree  
id in a good way, if the object resides in another module etc.

I’ll e-mail you an update later today.

> - How can we create a more general “nice” API for bulk adding items to  
> all multi-item classes, like ListBox, ListCtrl etc

I’ll look into this! Perhaps the API terminology should be streamlined  
for these kind of Ruby bulk-add (and related) methods? Any suggestions?

> - It would be useful to have a way of adding item data, which I usually  
> need to use with these type of classes

As you’ve seen, present in my TreeCtrl implementation, but probably  
harder to do in a ListBox, since there’s no item\_data for the strings.  
However, I’m thinking of adding a ListBox#insert\_obj\_items(), with the  
syntax:  
ListBox#insert\_obj\_items(array, method, pos)

This would work like ListBox#insert\_items(), appart from that you can  
specify the method in the array objects where the data should be  
retrieved (and then a to\_s conversion to make sure the data is in string  
format). Often, the data for list boxes are already available in object  
arrays, but not necessarily in string arrays.

> PS - I’ve just committed the whole set of Graphics\* classes (except  
> Renderer, which didn’t appear useful in Ruby) to SVN trunk. The  
> transforms (shears etc), gradient fills and so on work really  
> impressively, and all anti-aliased

Sounds great! I’ll see if I can get the SVN to work.

Best regards,

Jari W.
