Forum: wxRuby GenericDirCtrl#re_create_tree behaviour

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Mathias Bruce (Guest)
on 2008-11-14 03:52
(Received via mailing list)
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
Alex F. (Guest)
on 2008-11-14 13:27
(Received via mailing list)
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
This topic is locked and can not be replied to.