# GenericDirCtrl#re\_create\_tree behaviour

**URL:** <https://www.ruby-forum.com/t/genericdirctrl-re-create-tree-behaviour/153264>\
**Category:** wxRuby\
**Created:** [November 14, 2008, 2:52am UTC](https://www.ruby-forum.com/t/genericdirctrl-re-create-tree-behaviour/153264 "2008-11-14T02:52:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mathias\_Bruce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@Mathias\_Bruce](https://www.ruby-forum.com/u/Mathias_Bruce)\
**Post date:** [November 14, 2008, 2:52am UTC](https://www.ruby-forum.com/t/genericdirctrl-re-create-tree-behaviour/153264/1 "2008-11-14T02:52:10Z")

</div>

Hi

When GenericDirCtrl#re\_create\_tree is called in order to refresh the  
tree with any possible changes made to the file system since it was  
loaded, it doesn’t re-expand all the tree items that were expanded  
prior to calling re\_create\_tree. I’m working on a solution to note all  
open tree items and after re\_create\_tree manually expand the items  
that are still present in the tree.

My question: Is there a nice way that I’ve overlooked to make it  
automagically re-expand previously expanded nodes after recreating the  
tree? I’m on my way towards doing it manually, but since the  
GenericDirCtrl’s TreeCtrl actually re-numbers the items in the tree  
during the recreation process, I’ll have to do it by matching TreeItem  
text strings, and it’s starting to feel like a hack (in the ugly  
sense).

Thanks in advance, and best regards!  
/Mathias

---

<div class="post-metadata">

**Author:** ![Mathias\_Bruce](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/82dd89/32.png) [@Mathias\_Bruce](https://www.ruby-forum.com/u/Mathias_Bruce)\
**Post date:** [November 14, 2008, 12:27pm UTC](https://www.ruby-forum.com/t/genericdirctrl-re-create-tree-behaviour/153264/2 "2008-11-14T12:27:51Z")

</div>

Mathias Bruce wrote:

> When GenericDirCtrl#re\_create\_tree is called in order to refresh the  
> tree with any possible changes made to the file system since it was  
> loaded, it doesn’t re-expand all the tree items that were expanded  
> prior to calling re\_create\_tree. I’m working on a solution to note all  
> open tree items and after re\_create\_tree manually expand the items  
> that are still present in the tree.
> 
> My question: Is there a nice way that I’ve overlooked to make it  
> automagically re-expand previously expanded nodes after recreating the  
> tree?

Sorry, but wxWidgets doesn’t provide a magic way to do this. As the name  
suggests it’s a generic control (ie written from the ground up) so it  
doesn’t benefit from the way that modern OSes update file dialogs etc as  
new files and directories are created.

However it’s not hard to get a list of the currently open folder names:

```
  tree = dir.tree_ctrl
  open = tree.select { | item | tree.expanded?(item) }
  open.map! { | item | tree.item_text(item) }

```

Note that wxRuby creates the tree items as their parent is expanded, so  
this will only traverse whatever folders have been shown, not every  
possible folder.

I expect you’d also want to use the full paths as the unique keys:

```
  def tree.item_path(item)
    return "" if item == root_item
    path = item_text(item)
    while item = item_parent(item) and item != root_item
      path = "#{item_text(item)}/#{path}"
    end
    path
  end

```

I need to fix TreeCtrl#get\_item\_data for the trees from GenericDirCtrl  
as it currently crashes. It would seem sensible for the item\_data to be  
or include the paths. So I’d be grateful if you could follow-up with any  
problems you encounter in this.

Doing the re-opening is left to you - you’ll need to work hierarchically  
down the tree.

thanks  
alex
