Re: Moving tree items




"Lachac, Gerry" <G.Lachac@dialogic.com> writes:

> Jerome Bolliet wrote: 
> > Alistair Cunningham wrote:
> 
> >> I have a tree item in a tree, and wish at run time to move 
> >> this item to another place in the same tree. Can this be
> >> done without destroying the item?
> 
> > At the moment, it's not possible.
> 
> >> Any ideas?
> 
> > Yes... I need to modify the widget to authorize this.
> > I've some modifications to do. Another one.
> > I hope that will be fixe quickly.
> 
> Can a similar change also be made for a GtkList as well?  I recently had to
> implement moving things up and down a list by  allocating a new GtkListItem,
> copying the old one and remove it from the list, inserting the new one
> above/below where the old one was and then deleting the old one.  Pretty
> tedious.
> 
> Off the top of my head, perhaps a "move" function for a GList and then some
> way to re-draw the widget that contains the GList by having it re-evaluate
> that list.  Being a newbie to gtk, it occurs to me that this would be useful
> and inherited by all widgets containing GList's. (if something like this
> would even work :-)

For a GtkList no recreation, copying is needed:

 GList *items_to_move = g_list_append (NULL, item_to_remove);
 
 gtk_widget_ref (item_to_remove);
 gtk_list_remove_items (GtkList (list), items_to_move);
 gtk_list_insert_items (GtkList (list), items_to_move, new_position);
 gtk_widget_unref (item_to_remove);

To explain the code:

 - creates a GList with a single item.

 - increments the reference count on the item, so that
   when it is removed from its parent, it is not freed. (Even though
   it is an orphan, somebody cares about it...)

 - removes the item from the list
 
 - inserts it in the new position

 - unreferences it, since the List now references it again.

This can be shortened to:

 GList *items_to_move = g_list_append (NULL, item_to_remove);
 
 gtk_list_remove_items_no_unref (GtkList (list), items_to_move);
 gtk_list_insert_items (GtkList (list), items_to_move, new_position);
 gtk_widget_unref (item_to_remove);

Regards,
                                        Owen



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]