[wxruby-users] help with project viewer

Alex Fenton alex at pressure.to
Sat Jan 17 03:43:48 EST 2009


James Waudby wrote:
> I've just begun using wxRuby and I've been finding my feet with things
> quite easily, however there's one thing I'm uncertain of at the moment
> which I'd like to do and that's a project viewer.
>
> I know that it would have to be using TreeCtrl.
>
> If someone could explain to me how to build a custom Tree with my own
> folders and files that would be really appreciated.

Firstly, there's a generic directory / file tree widget built-in with 
wxRuby., called GenericDirCtrl. This is built using TreeCtrl, so if you 
know C++ it's a decent source for study.

One challenge can be that traversing a large file system in Ruby is slow 
so the tree is slow to present. To get round this, you only present the 
currently open items (adding them via append_item). For each item that 
is a directory, call set_item_has_children to display it with an 
unopened 'expand' button. Then provide an evt_tree_item_expanding event 
handler that appends the file/directory children as a directory is opened.

You'll probably want to use the item_data field (which attaches any Ruby 
object to an item in the treectrl) to store fuller information about 
each file/directory. This could just be the full path, or it could be a 
hash with extra info (eg SVN status, or stat-like data).

Each icon that you might use is stored in a ImageList, and the index of 
the relevant image (depending on file type, for example) is passed to 
calls to append_item / add_item.

As an aside, it would be nice to have a model-based TreeCtrl - one where 
a non-GUI class defines how objects are fetched and related, and the GUI 
class does the presenting work. It's a nice way to write code, I find, 
for Grid - GridTableBase - and the development version of wxWidgets 
supports this, I believe.

a


More information about the wxruby-users mailing list