# Controls within a wxTreeCtrl

**URL:** <https://www.ruby-forum.com/t/controls-within-a-wxtreectrl/124371>\
**Category:** wxRuby\
**Created:** [December 28, 2007, 3:25am UTC](https://www.ruby-forum.com/t/controls-within-a-wxtreectrl/124371 "2007-12-28T03:25:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ratkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@ratkins](https://www.ruby-forum.com/u/ratkins)\
**Post date:** [December 28, 2007, 3:25am UTC](https://www.ruby-forum.com/t/controls-within-a-wxtreectrl/124371/1 "2007-12-28T03:25:01Z")

</div>

What I’d like to be able to do is use a wxTreeCtrl to manage a tree  
layout  
of not only text, but several buttons which would line up adjacent to  
the  
text in the tree items. I’ve googled around a bit on this and it  
doesn’t  
seem straightforward, if it is possible at all.

Can anyone provide any pointers for attempting this?

In a simple ASCI schematic, it would be like this:

- RootItem A B C
  - TreeItem1 A B C
  - TreeItem2 A B C

where RootItem and TreeItem(s) are text labels, and A, B, and C, are  
simple  
buttons with icons on them.

I am starting to think that I will have to synthesize the entire thing  
using  
sizers. Would it even be possible in the wx model to dynamically  
add/remove  
items and spacers to create a tree-like behavior? Thoughts?

Thanks,  
Bob

---

<div class="post-metadata">

**Author:** ![ratkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@ratkins](https://www.ruby-forum.com/u/ratkins)\
**Post date:** [December 28, 2007, 3:56am UTC](https://www.ruby-forum.com/t/controls-within-a-wxtreectrl/124371/2 "2007-12-28T03:56:02Z")

</div>

Robert A. wrote:

> What I’d like to be able to do is use a wxTreeCtrl to manage a tree  
> layout of not only text, but several buttons which would line up  
> adjacent to the text in the tree items. I’ve googled around a bit on  
> this and it doesn’t seem straightforward, if it is possible at all.
> 
> Can anyone provide any pointers for attempting this?  
> This comes up frequently in C++ wxWidgets. There’s no core class in  
> wxWidgets which does this, and hence nothing in wxRuby. But there are  
> numerous user-contributed classes on eg wxCode which have multi-column  
> TreeCtrls.

But at present these are only in C++, and I have no experience of trying  
to wrap third-party C++ extensions in Ruby. For some classes this should  
not be too hard using SWIG - but TreeCtrl is one of the most complex  
wrappings in wxRuby.

> I am starting to think that I will have to synthesize the entire thing  
> using sizers. Would it even be possible in the wx model to  
> dynamically add/remove items and spacers to create a tree-like  
> behavior? Thoughts?  
> One possibility might be to have two sibling controls, a TreeCtrl on the  
> left, and a Panel on the right where the buttons are drawn. Use  
> TreeCtrl#get\_bounding\_rect to find the position of each of the visible  
> tree items, and place Wx::Buttons at an appropriate height on the right.  
> Or you can draw directly control-like things upon the panel with a  
> DeviceContext (DC), possibly using RendererNative to get the right look  
> and feel for your platform. Either way, update the Panel’s contents as  
> tree items are collapsed and expanded (with event handlers).

This would mean that there would be a gap between the text labels and  
the associated buttons. Closer to what you want would be to try drawing  
directly on top of the TreeCtrl, again using get\_bounding\_rect to find  
where the label is positioned. But I am not sure how well drawing  
directly on top of a native control will work - be sure to test on any  
platform you want to use.

hth  
alex

---

<div class="post-metadata">

**Author:** ![ratkins](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bcef8e/32.png) [@ratkins](https://www.ruby-forum.com/u/ratkins)\
**Post date:** [December 28, 2007, 4:13am UTC](https://www.ruby-forum.com/t/controls-within-a-wxtreectrl/124371/3 "2007-12-28T04:13:33Z")

</div>

Alex F. wrote:

> Use TreeCtrl#get\_bounding\_rect to find the position of each of the visible  
> tree items  
> PS - I guess no-one’s ever wanted to use this method before - it works  
> OK for me but in an un-rubyish way. You have to create an empty Wx::Rect  
> object, then pass it in as a parameter to have its values updated with  
> the x,y,w,h values.

We’ll add something more ruby-ish which accepts an id and returns a  
bounding rect for 1.9.3

alex
