Swap nodes in wxTreeCtrl

359 views
Skip to first unread message

Elmi

unread,
Apr 27, 2011, 1:21:08 AM4/27/11
to wx-users
Hi,

is there a possibility to swap nodes (including their childs) within a
wxTreeCtrl or - at least - to remove a node and place it at a
different position including all its childs?

What I plan to implement is some kind of editing functionality where
users can move some nodes up or down. Here I hope there is a
possibility to do that without the need to re-create the complete
tree.

Vadim Zeitlin

unread,
Apr 27, 2011, 3:37:07 AM4/27/11
to wx-u...@googlegroups.com
On Tue, 26 Apr 2011 22:21:08 -0700 (PDT) Elmi <satzk...@googlemail.com> wrote:

E> is there a possibility to swap nodes (including their childs) within a
E> wxTreeCtrl or - at least - to remove a node and place it at a
E> different position including all its childs?

Consider that a node is basically just a label, an icon and, maybe, a
client data pointer. So swapping them is as simple as swapping their
labels, icons and client data pointers if needed. And while wxTreeCtrl
doesn't provide any way to do it, it's trivial to do this yourself.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Satz Klauer

unread,
Apr 27, 2011, 8:29:02 AM4/27/11
to wx-u...@googlegroups.com
On 4/27/11, Vadim Zeitlin <va...@wxwidgets.org> wrote:
> On Tue, 26 Apr 2011 22:21:08 -0700 (PDT) Elmi <satzk...@googlemail.com>
> wrote:
>
> E> is there a possibility to swap nodes (including their childs) within a
> E> wxTreeCtrl or - at least - to remove a node and place it at a
> E> different position including all its childs?
>
> Consider that a node is basically just a label, an icon and, maybe, a
> client data pointer. So swapping them is as simple as swapping their
> labels, icons and client data pointers if needed. And while wxTreeCtrl
> doesn't provide any way to do it, it's trivial to do this yourself.

Hm, I afraid I id not understand it. When I append a new node I get a
wxTreeItemId object returned. What should I do when I want to swap two
nodes? Simply exchanging the wxTreeItemId-objects should not do the
job because there only the memory areas of the objects are exchanged,
the wxTreeCtrl should not notice that?

Vadim Zeitlin

unread,
Apr 28, 2011, 9:40:31 AM4/28/11
to wx-u...@googlegroups.com
On Wed, 27 Apr 2011 14:29:02 +0200 Satz Klauer <satzk...@googlemail.com> wrote:

SK> On 4/27/11, Vadim Zeitlin <va...@wxwidgets.org> wrote:
SK> > On Tue, 26 Apr 2011 22:21:08 -0700 (PDT) Elmi <satzk...@googlemail.com>
SK> > wrote:
SK> >
SK> > E> is there a possibility to swap nodes (including their childs) within a
SK> > E> wxTreeCtrl or - at least - to remove a node and place it at a
SK> > E> different position including all its childs?
SK> >
SK> > Consider that a node is basically just a label, an icon and, maybe, a
SK> > client data pointer. So swapping them is as simple as swapping their
SK> > labels, icons and client data pointers if needed. And while wxTreeCtrl
SK> > doesn't provide any way to do it, it's trivial to do this yourself.
SK>
SK> Hm, I afraid I id not understand it. When I append a new node I get a
SK> wxTreeItemId object returned. What should I do when I want to swap two
SK> nodes? Simply exchanging the wxTreeItemId-objects should not do the
SK> job because there only the memory areas of the objects are exchanged,
SK> the wxTreeCtrl should not notice that?

What I mean to say is that you should swap the nodes at your application
level, not at wxTreeCtrl level. I.e. just exchange wxTreeItemIds you store
internally.

Satz Klauer

unread,
Apr 28, 2011, 3:21:42 PM4/28/11
to wx-u...@googlegroups.com

But that doesn't solves the problem of the children of these nodes:
when I exchange the meaning of the nodes that does in no way influence
the children of them - which would have to be exchanged too.

Vadim Zeitlin

unread,
Apr 28, 2011, 4:25:16 PM4/28/11
to wx-u...@googlegroups.com
On Thu, 28 Apr 2011 21:21:42 +0200 Satz Klauer <satzk...@googlemail.com> wrote:

SK> > What I mean to say is that you should swap the nodes at your application
SK> > level, not at wxTreeCtrl level. I.e. just exchange wxTreeItemIds you store
SK> > internally.
SK>
SK> But that doesn't solves the problem of the children of these nodes:
SK> when I exchange the meaning of the nodes that does in no way influence
SK> the children of them - which would have to be exchanged too.

I might be missing something but if you change the labels of the nodes and
exchange their stored ids it looks to me like everything work as needed but
instead of really modifying the tree you will have just relabeled its items
in some sense.

Satz Klauer

unread,
Apr 29, 2011, 5:35:16 AM4/29/11
to wx-u...@googlegroups.com
On 4/28/11, Vadim Zeitlin <va...@wxwidgets.org> wrote:
> but
> instead of really modifying the tree you will have just relabeled its items
> in some sense.

Yes, that's exactly my problem!?

To avoid some misunderstandings let me try it with an example. I use
following tree-structure:

root
- node 1
--- subnode a
- node 2
--- subnode c
--- subnode d
----- sub-subnode X
--- subnode e

There I want to swap "node 1" and "node 2" _including_ their children
so that I get the following structure afterwards

root
- node 2
--- subnode c
--- subnode d
----- sub-subnode X
--- subnode e
- node 1
--- subnode a

But when I play around with the IDs and labels only I get this mixture

root
- node 2
--- subnode a
- node 1
--- subnode c
--- subnode d
----- sub-subnode X
--- subnode e

Now "node 1" has the children from "node 2". On the other hand as soon
as I remove a node from the tree just to insert it at a different
position the information about its children get lost.

So how can I solve that? Do I really have to re-create the tree or at
least the swapped nodes completely? This can be a complex task,
especially when the structure is more complicated as in the example
(means more sub-, subsub-, subsubsubchildren than above). In this case
I would have to store the complete node structure - an information
that should be already available within the tree-control!?

Mike Musterd

unread,
Apr 29, 2011, 5:42:38 AM4/29/11
to wx-u...@googlegroups.com
Can't you simply swap the parents?


2011/4/29 Satz Klauer <satzk...@googlemail.com>

Vadim Zeitlin

unread,
Apr 29, 2011, 9:58:34 AM4/29/11
to wx-u...@googlegroups.com
On Fri, 29 Apr 2011 11:35:16 +0200 Satz Klauer <satzk...@googlemail.com> wrote:

SK> On 4/28/11, Vadim Zeitlin <va...@wxwidgets.org> wrote:
SK> > but
SK> > instead of really modifying the tree you will have just relabeled its items
SK> > in some sense.
SK>
SK> Yes, that's exactly my problem!?
SK>
SK> To avoid some misunderstandings let me try it with an example. I use
SK> following tree-structure:
SK>
SK> root
SK> - node 1
SK> --- subnode a
SK> - node 2
SK> --- subnode c
SK> --- subnode d
SK> ----- sub-subnode X
SK> --- subnode e
SK>
SK> There I want to swap "node 1" and "node 2" including their children
SK> so that I get the following structure afterwards
SK>
SK> root
SK> - node 2
SK> --- subnode c
SK> --- subnode d
SK> ----- sub-subnode X
SK> --- subnode e
SK> - node 1
SK> --- subnode a
SK>
SK> But when I play around with the IDs and labels only I get this mixture
SK>
SK> root
SK> - node 2
SK> --- subnode a
SK> - node 1
SK> --- subnode c
SK> --- subnode d
SK> ----- sub-subnode X
SK> --- subnode e

Sorry, I thought that this was what you wanted to do for some reason.
Looking at the beginning of the thread I see that I simply misunderstood
you now.

Anyhow, I can think about another approach which would use wxTreeCtrl
support for sorting children of the given item so I guess you could also
use this to simply resort the root children to put them in the order you
want (you will need to override wxTreeCtrl::OnCompareItems() to implement
this) but I'm not sure if it's really much simpler than the straightforward
solution of just recreating everything.

SK> So how can I solve that? Do I really have to re-create the tree or at
SK> least the swapped nodes completely? This can be a complex task,
SK> especially when the structure is more complicated as in the example
SK> (means more sub-, subsub-, subsubsubchildren than above). In this case
SK> I would have to store the complete node structure - an information
SK> that should be already available within the tree-control!?

Of course it's available and you can enumerate all the children of the
given item using wxTreeCtrl methods. So the straightforward solution would
be to do a breadth-first traversal of node 1 creating its mirror as another
node which would be appended after the node 2 and then deleting the
original node 1.

Reply all
Reply to author
Forward
0 new messages