]> Pileus Git - ~andy/gtk/blob - gtk/gtktree.c
marshaller fixes.
[~andy/gtk] / gtk / gtktree.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtkmain.h"
28 #include "gtksignal.h"
29 #include "gtklist.h"
30
31 #define GTK_ENABLE_BROKEN
32 #include "gtktree.h"
33 #include "gtktreeitem.h"
34
35 enum {
36   SELECTION_CHANGED,
37   SELECT_CHILD,
38   UNSELECT_CHILD,
39   LAST_SIGNAL
40 };
41
42 static void gtk_tree_class_init      (GtkTreeClass   *klass);
43 static void gtk_tree_init            (GtkTree        *tree);
44 static void gtk_tree_destroy         (GtkObject      *object);
45 static void gtk_tree_map             (GtkWidget      *widget);
46 static void gtk_tree_parent_set      (GtkWidget      *widget,
47                                       GtkWidget      *previous_parent);
48 static void gtk_tree_unmap           (GtkWidget      *widget);
49 static void gtk_tree_realize         (GtkWidget      *widget);
50 static gint gtk_tree_expose          (GtkWidget      *widget,
51                                       GdkEventExpose *event);
52 static gint gtk_tree_motion_notify   (GtkWidget      *widget,
53                                       GdkEventMotion *event);
54 static gint gtk_tree_button_press    (GtkWidget      *widget,
55                                       GdkEventButton *event);
56 static gint gtk_tree_button_release  (GtkWidget      *widget,
57                                       GdkEventButton *event);
58 static void gtk_tree_size_request    (GtkWidget      *widget,
59                                       GtkRequisition *requisition);
60 static void gtk_tree_size_allocate   (GtkWidget      *widget,
61                                       GtkAllocation  *allocation);
62 static void gtk_tree_add             (GtkContainer   *container,
63                                       GtkWidget      *widget);
64 static void gtk_tree_forall          (GtkContainer   *container,
65                                       gboolean        include_internals,
66                                       GtkCallback     callback,
67                                       gpointer        callback_data);
68
69 static void gtk_real_tree_select_child   (GtkTree       *tree,
70                                           GtkWidget     *child);
71 static void gtk_real_tree_unselect_child (GtkTree       *tree,
72                                           GtkWidget     *child);
73
74 static GtkType gtk_tree_child_type  (GtkContainer   *container);
75
76 static GtkContainerClass *parent_class = NULL;
77 static guint tree_signals[LAST_SIGNAL] = { 0 };
78
79 GtkType
80 gtk_tree_get_type (void)
81 {
82   static GtkType tree_type = 0;
83   
84   if (!tree_type)
85     {
86       static const GtkTypeInfo tree_info =
87       {
88         "GtkTree",
89         sizeof (GtkTree),
90         sizeof (GtkTreeClass),
91         (GtkClassInitFunc) gtk_tree_class_init,
92         (GtkObjectInitFunc) gtk_tree_init,
93         /* reserved_1 */ NULL,
94         /* reserved_2 */ NULL,
95         (GtkClassInitFunc) NULL,
96       };
97       
98       tree_type = gtk_type_unique (gtk_container_get_type (), &tree_info);
99     }
100   
101   return tree_type;
102 }
103
104 static void
105 gtk_tree_class_init (GtkTreeClass *class)
106 {
107   GtkObjectClass *object_class;
108   GtkWidgetClass *widget_class;
109   GtkContainerClass *container_class;
110   
111   object_class = (GtkObjectClass*) class;
112   widget_class = (GtkWidgetClass*) class;
113   container_class = (GtkContainerClass*) class;
114   
115   parent_class = gtk_type_class (gtk_container_get_type ());
116   
117   
118   object_class->destroy = gtk_tree_destroy;
119   
120   widget_class->map = gtk_tree_map;
121   widget_class->unmap = gtk_tree_unmap;
122   widget_class->parent_set = gtk_tree_parent_set;
123   widget_class->realize = gtk_tree_realize;
124   widget_class->expose_event = gtk_tree_expose;
125   widget_class->motion_notify_event = gtk_tree_motion_notify;
126   widget_class->button_press_event = gtk_tree_button_press;
127   widget_class->button_release_event = gtk_tree_button_release;
128   widget_class->size_request = gtk_tree_size_request;
129   widget_class->size_allocate = gtk_tree_size_allocate;
130   
131   container_class->add = gtk_tree_add;
132   container_class->remove = 
133     (void (*)(GtkContainer *, GtkWidget *)) gtk_tree_remove_item;
134   container_class->forall = gtk_tree_forall;
135   container_class->child_type = gtk_tree_child_type;
136   
137   class->selection_changed = NULL;
138   class->select_child = gtk_real_tree_select_child;
139   class->unselect_child = gtk_real_tree_unselect_child;
140
141   tree_signals[SELECTION_CHANGED] =
142     gtk_signal_new ("selection_changed",
143                     GTK_RUN_FIRST,
144                     GTK_CLASS_TYPE (object_class),
145                     GTK_SIGNAL_OFFSET (GtkTreeClass, selection_changed),
146                     gtk_marshal_VOID__VOID,
147                     GTK_TYPE_NONE, 0);
148   tree_signals[SELECT_CHILD] =
149     gtk_signal_new ("select_child",
150                     GTK_RUN_FIRST,
151                     GTK_CLASS_TYPE (object_class),
152                     GTK_SIGNAL_OFFSET (GtkTreeClass, select_child),
153                     gtk_marshal_VOID__OBJECT,
154                     GTK_TYPE_NONE, 1,
155                     GTK_TYPE_WIDGET);
156   tree_signals[UNSELECT_CHILD] =
157     gtk_signal_new ("unselect_child",
158                     GTK_RUN_FIRST,
159                     GTK_CLASS_TYPE (object_class),
160                     GTK_SIGNAL_OFFSET (GtkTreeClass, unselect_child),
161                     gtk_marshal_VOID__OBJECT,
162                     GTK_TYPE_NONE, 1,
163                     GTK_TYPE_WIDGET);
164 }
165
166 static GtkType
167 gtk_tree_child_type (GtkContainer     *container)
168 {
169   return GTK_TYPE_TREE_ITEM;
170 }
171
172 static void
173 gtk_tree_init (GtkTree *tree)
174 {
175   tree->children = NULL;
176   tree->root_tree = tree;
177   tree->selection = NULL;
178   tree->tree_owner = NULL;
179   tree->selection_mode = GTK_SELECTION_SINGLE;
180   tree->indent_value = 9;
181   tree->current_indent = 0;
182   tree->level = 0;
183   tree->view_mode = GTK_TREE_VIEW_LINE;
184   tree->view_line = 1;
185 }
186
187 GtkWidget*
188 gtk_tree_new (void)
189 {
190   return GTK_WIDGET (gtk_type_new (gtk_tree_get_type ()));
191 }
192
193 void
194 gtk_tree_append (GtkTree   *tree,
195                  GtkWidget *tree_item)
196 {
197   g_return_if_fail (tree != NULL);
198   g_return_if_fail (GTK_IS_TREE (tree));
199   g_return_if_fail (tree_item != NULL);
200   g_return_if_fail (GTK_IS_TREE_ITEM (tree_item));
201   
202   gtk_tree_insert (tree, tree_item, -1);
203 }
204
205 void
206 gtk_tree_prepend (GtkTree   *tree,
207                   GtkWidget *tree_item)
208 {
209   g_return_if_fail (tree != NULL);
210   g_return_if_fail (GTK_IS_TREE (tree));
211   g_return_if_fail (tree_item != NULL);
212   g_return_if_fail (GTK_IS_TREE_ITEM (tree_item));
213   
214   gtk_tree_insert (tree, tree_item, 0);
215 }
216
217 void
218 gtk_tree_insert (GtkTree   *tree,
219                  GtkWidget *tree_item,
220                  gint       position)
221 {
222   gint nchildren;
223   
224   g_return_if_fail (tree != NULL);
225   g_return_if_fail (GTK_IS_TREE (tree));
226   g_return_if_fail (tree_item != NULL);
227   g_return_if_fail (GTK_IS_TREE_ITEM (tree_item));
228   
229   nchildren = g_list_length (tree->children);
230   
231   if ((position < 0) || (position > nchildren))
232     position = nchildren;
233   
234   if (position == nchildren)
235     tree->children = g_list_append (tree->children, tree_item);
236   else
237     tree->children = g_list_insert (tree->children, tree_item, position);
238   
239   gtk_widget_set_parent (tree_item, GTK_WIDGET (tree));
240   
241   if (GTK_WIDGET_REALIZED (tree_item->parent))
242     gtk_widget_realize (tree_item);
243
244   if (GTK_WIDGET_VISIBLE (tree_item->parent) && GTK_WIDGET_VISIBLE (tree_item))
245     {
246       if (GTK_WIDGET_MAPPED (tree_item->parent))
247         gtk_widget_map (tree_item);
248
249       gtk_widget_queue_resize (tree_item);
250     }
251 }
252
253 static void
254 gtk_tree_add (GtkContainer *container,
255               GtkWidget    *child)
256 {
257   GtkTree *tree;
258   
259   g_return_if_fail (container != NULL);
260   g_return_if_fail (GTK_IS_TREE (container));
261   g_return_if_fail (GTK_IS_TREE_ITEM (child));
262   
263   tree = GTK_TREE (container);
264   
265   tree->children = g_list_append (tree->children, child);
266   
267   gtk_widget_set_parent (child, GTK_WIDGET (container));
268   
269   if (GTK_WIDGET_REALIZED (child->parent))
270     gtk_widget_realize (child);
271
272   if (GTK_WIDGET_VISIBLE (child->parent) && GTK_WIDGET_VISIBLE (child))
273     {
274       if (GTK_WIDGET_MAPPED (child->parent))
275         gtk_widget_map (child);
276
277       gtk_widget_queue_resize (child);
278     }
279   
280   if (!tree->selection && (tree->selection_mode == GTK_SELECTION_BROWSE))
281     gtk_tree_select_child (tree, child);
282 }
283
284 static gint
285 gtk_tree_button_press (GtkWidget      *widget,
286                        GdkEventButton *event)
287 {
288   GtkTree *tree;
289   GtkWidget *item;
290   
291   g_return_val_if_fail (widget != NULL, FALSE);
292   g_return_val_if_fail (GTK_IS_TREE (widget), FALSE);
293   g_return_val_if_fail (event != NULL, FALSE);
294   
295   tree = GTK_TREE (widget);
296   item = gtk_get_event_widget ((GdkEvent*) event);
297   
298   while (item && !GTK_IS_TREE_ITEM (item))
299     item = item->parent;
300   
301   if (!item || (item->parent != widget))
302     return FALSE;
303   
304   switch(event->button) 
305     {
306     case 1:
307       gtk_tree_select_child (tree, item);
308       break;
309     case 2:
310       if(GTK_TREE_ITEM(item)->subtree) gtk_tree_item_expand(GTK_TREE_ITEM(item));
311       break;
312     case 3:
313       if(GTK_TREE_ITEM(item)->subtree) gtk_tree_item_collapse(GTK_TREE_ITEM(item));
314       break;
315     }
316   
317   return TRUE;
318 }
319
320 static gint
321 gtk_tree_button_release (GtkWidget      *widget,
322                          GdkEventButton *event)
323 {
324   GtkTree *tree;
325   GtkWidget *item;
326   
327   g_return_val_if_fail (widget != NULL, FALSE);
328   g_return_val_if_fail (GTK_IS_TREE (widget), FALSE);
329   g_return_val_if_fail (event != NULL, FALSE);
330   
331   tree = GTK_TREE (widget);
332   item = gtk_get_event_widget ((GdkEvent*) event);
333   
334   return TRUE;
335 }
336
337 gint
338 gtk_tree_child_position (GtkTree   *tree,
339                          GtkWidget *child)
340 {
341   GList *children;
342   gint pos;
343   
344   
345   g_return_val_if_fail (tree != NULL, -1);
346   g_return_val_if_fail (GTK_IS_TREE (tree), -1);
347   g_return_val_if_fail (child != NULL, -1);
348   
349   pos = 0;
350   children = tree->children;
351   
352   while (children)
353     {
354       if (child == GTK_WIDGET (children->data)) 
355         return pos;
356       
357       pos += 1;
358       children = children->next;
359     }
360   
361   
362   return -1;
363 }
364
365 void
366 gtk_tree_clear_items (GtkTree *tree,
367                       gint     start,
368                       gint     end)
369 {
370   GtkWidget *widget;
371   GList *clear_list;
372   GList *tmp_list;
373   guint nchildren;
374   guint index;
375   
376   g_return_if_fail (tree != NULL);
377   g_return_if_fail (GTK_IS_TREE (tree));
378   
379   nchildren = g_list_length (tree->children);
380   
381   if (nchildren > 0)
382     {
383       if ((end < 0) || (end > nchildren))
384         end = nchildren;
385       
386       if (start >= end)
387         return;
388       
389       tmp_list = g_list_nth (tree->children, start);
390       clear_list = NULL;
391       index = start;
392       while (tmp_list && index <= end)
393         {
394           widget = tmp_list->data;
395           tmp_list = tmp_list->next;
396           index++;
397           
398           clear_list = g_list_prepend (clear_list, widget);
399         }
400       
401       gtk_tree_remove_items (tree, clear_list);
402     }
403 }
404
405 static void
406 gtk_tree_destroy (GtkObject *object)
407 {
408   GtkTree *tree;
409   GtkWidget *child;
410   GList *children;
411   
412   g_return_if_fail (object != NULL);
413   g_return_if_fail (GTK_IS_TREE (object));
414   
415   tree = GTK_TREE (object);
416   
417   children = tree->children;
418   while (children)
419     {
420       child = children->data;
421       children = children->next;
422       
423       gtk_widget_ref (child);
424       gtk_widget_unparent (child);
425       gtk_widget_destroy (child);
426       gtk_widget_unref (child);
427     }
428   
429   g_list_free (tree->children);
430   tree->children = NULL;
431   
432   if (tree->root_tree == tree)
433     {
434       GList *node;
435       for (node = tree->selection; node; node = node->next)
436         gtk_widget_unref ((GtkWidget *)node->data);
437       g_list_free (tree->selection);
438       tree->selection = NULL;
439     }
440   
441   if (GTK_OBJECT_CLASS (parent_class)->destroy)
442     (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
443 }
444
445 static gint
446 gtk_tree_expose (GtkWidget      *widget,
447                  GdkEventExpose *event)
448 {
449   GtkTree *tree;
450   GtkWidget *child;
451   GdkEventExpose child_event;
452   GList *children;
453   
454   
455   g_return_val_if_fail (widget != NULL, FALSE);
456   g_return_val_if_fail (GTK_IS_TREE (widget), FALSE);
457   g_return_val_if_fail (event != NULL, FALSE);
458   
459   if (GTK_WIDGET_DRAWABLE (widget))
460     {
461       tree = GTK_TREE (widget);
462       
463       child_event = *event;
464       
465       children = tree->children;
466       while (children)
467         {
468           child = children->data;
469           children = children->next;
470           
471           if (GTK_WIDGET_NO_WINDOW (child) &&
472               gtk_widget_intersect (child, &event->area, &child_event.area))
473             gtk_widget_event (child, (GdkEvent*) &child_event);
474         }
475     }
476   
477   
478   return FALSE;
479 }
480
481 static void
482 gtk_tree_forall (GtkContainer *container,
483                  gboolean      include_internals,
484                  GtkCallback   callback,
485                  gpointer      callback_data)
486 {
487   GtkTree *tree;
488   GtkWidget *child;
489   GList *children;
490   
491   
492   g_return_if_fail (container != NULL);
493   g_return_if_fail (GTK_IS_TREE (container));
494   g_return_if_fail (callback != NULL);
495   
496   tree = GTK_TREE (container);
497   children = tree->children;
498   
499   while (children)
500     {
501       child = children->data;
502       children = children->next;
503       
504       (* callback) (child, callback_data);
505     }
506 }
507
508 static void
509 gtk_tree_unselect_all (GtkTree *tree)
510 {
511   GList *tmp_list, *selection;
512   GtkWidget *tmp_item;
513       
514   selection = tree->selection;
515   tree->selection = NULL;
516
517   tmp_list = selection;
518   while (tmp_list)
519     {
520       tmp_item = selection->data;
521
522       if (tmp_item->parent &&
523           GTK_IS_TREE (tmp_item->parent) &&
524           GTK_TREE (tmp_item->parent)->root_tree == tree)
525         gtk_tree_item_deselect (GTK_TREE_ITEM (tmp_item));
526
527       gtk_widget_unref (tmp_item);
528
529       tmp_list = tmp_list->next;
530     }
531
532   g_list_free (selection);
533 }
534
535 static void
536 gtk_tree_parent_set (GtkWidget *widget,
537                      GtkWidget *previous_parent)
538 {
539   GtkTree *tree = GTK_TREE (widget);
540   GtkWidget *child;
541   GList *children;
542   
543   if (widget->parent && GTK_IS_TREE (widget->parent))
544     {
545       gtk_tree_unselect_all (tree);
546       
547       /* set root tree for this tree */
548       tree->root_tree = GTK_TREE(widget->parent)->root_tree;
549       
550       tree->level = GTK_TREE(GTK_WIDGET(tree)->parent)->level+1;
551       tree->indent_value = GTK_TREE(GTK_WIDGET(tree)->parent)->indent_value;
552       tree->current_indent = GTK_TREE(GTK_WIDGET(tree)->parent)->current_indent + 
553         tree->indent_value;
554       tree->view_mode = GTK_TREE(GTK_WIDGET(tree)->parent)->view_mode;
555       tree->view_line = GTK_TREE(GTK_WIDGET(tree)->parent)->view_line;
556     }
557   else
558     {
559       tree->root_tree = tree;
560       
561       tree->level = 0;
562       tree->current_indent = 0;
563     }
564
565   children = tree->children;
566   while (children)
567     {
568       child = children->data;
569       children = children->next;
570       
571       if (GTK_TREE_ITEM (child)->subtree)
572         gtk_tree_parent_set (GTK_TREE_ITEM (child)->subtree, child);
573     }
574 }
575
576 static void
577 gtk_tree_map (GtkWidget *widget)
578 {
579   GtkTree *tree = GTK_TREE (widget);
580   GtkWidget *child;
581   GList *children;
582   
583   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
584   
585   children = tree->children;
586   while (children)
587     {
588       child = children->data;
589       children = children->next;
590       
591       if (GTK_WIDGET_VISIBLE (child) &&
592           !GTK_WIDGET_MAPPED (child))
593         gtk_widget_map (child);
594       
595       if (GTK_TREE_ITEM (child)->subtree)
596         {
597           child = GTK_WIDGET (GTK_TREE_ITEM (child)->subtree);
598           
599           if (GTK_WIDGET_VISIBLE (child) && !GTK_WIDGET_MAPPED (child))
600             gtk_widget_map (child);
601         }
602     }
603
604   gdk_window_show (widget->window);
605 }
606
607 static gint
608 gtk_tree_motion_notify (GtkWidget      *widget,
609                         GdkEventMotion *event)
610 {
611   g_return_val_if_fail (widget != NULL, FALSE);
612   g_return_val_if_fail (GTK_IS_TREE (widget), FALSE);
613   g_return_val_if_fail (event != NULL, FALSE);
614   
615 #ifdef TREE_DEBUG
616   g_message("gtk_tree_motion_notify\n");
617 #endif /* TREE_DEBUG */
618   
619   return FALSE;
620 }
621
622 static void
623 gtk_tree_realize (GtkWidget *widget)
624 {
625   GdkWindowAttr attributes;
626   gint attributes_mask;
627   
628   
629   g_return_if_fail (widget != NULL);
630   g_return_if_fail (GTK_IS_TREE (widget));
631   
632   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
633   
634   attributes.window_type = GDK_WINDOW_CHILD;
635   attributes.x = widget->allocation.x;
636   attributes.y = widget->allocation.y;
637   attributes.width = widget->allocation.width;
638   attributes.height = widget->allocation.height;
639   attributes.wclass = GDK_INPUT_OUTPUT;
640   attributes.visual = gtk_widget_get_visual (widget);
641   attributes.colormap = gtk_widget_get_colormap (widget);
642   attributes.event_mask = gtk_widget_get_events (widget) | GDK_EXPOSURE_MASK;
643   
644   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
645   
646   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
647   gdk_window_set_user_data (widget->window, widget);
648   
649   widget->style = gtk_style_attach (widget->style, widget->window);
650   gdk_window_set_background (widget->window, 
651                              &widget->style->base[GTK_STATE_NORMAL]);
652 }
653
654 void
655 gtk_tree_remove_item (GtkTree      *container,
656                       GtkWidget    *widget)
657 {
658   GList *item_list;
659   
660   g_return_if_fail (container != NULL);
661   g_return_if_fail (GTK_IS_TREE (container));
662   g_return_if_fail (widget != NULL);
663   g_return_if_fail (container == GTK_TREE (widget->parent));
664   
665   item_list = g_list_append (NULL, widget);
666   
667   gtk_tree_remove_items (GTK_TREE (container), item_list);
668   
669   g_list_free (item_list);
670 }
671
672 /* used by gtk_tree_remove_items to make the function independant of
673    order in list of items to remove.
674    Sort item bu depth in tree */
675 static gint 
676 gtk_tree_sort_item_by_depth(GtkWidget* a, GtkWidget* b)
677 {
678   if((GTK_TREE(a->parent)->level) < (GTK_TREE(b->parent)->level))
679     return 1;
680   if((GTK_TREE(a->parent)->level) > (GTK_TREE(b->parent)->level))
681     return -1;
682   
683   return 0;
684 }
685
686 void
687 gtk_tree_remove_items (GtkTree *tree,
688                        GList   *items)
689 {
690   GtkWidget *widget;
691   GList *selected_widgets;
692   GList *tmp_list;
693   GList *sorted_list;
694   GtkTree *real_tree;
695   GtkTree *root_tree;
696   
697   g_return_if_fail (tree != NULL);
698   g_return_if_fail (GTK_IS_TREE (tree));
699   
700 #ifdef TREE_DEBUG
701   g_message("+ gtk_tree_remove_items [ tree %#x items list %#x ]\n", (int)tree, (int)items);
702 #endif /* TREE_DEBUG */
703   
704   /* We may not yet be mapped, so we actively have to find our
705    * root tree
706    */
707   if (tree->root_tree)
708     root_tree = tree->root_tree;
709   else
710     {
711       GtkWidget *tmp = GTK_WIDGET (tree);
712       while (tmp->parent && GTK_IS_TREE (tmp->parent))
713         tmp = tmp->parent;
714       
715       root_tree = GTK_TREE (tmp);
716     }
717   
718   tmp_list = items;
719   selected_widgets = NULL;
720   sorted_list = NULL;
721   widget = NULL;
722   
723 #ifdef TREE_DEBUG
724   g_message("* sort list by depth\n");
725 #endif /* TREE_DEBUG */
726   
727   while (tmp_list)
728     {
729       
730 #ifdef TREE_DEBUG
731       g_message ("* item [%#x] depth [%d]\n", 
732                  (int)tmp_list->data,
733                  (int)GTK_TREE(GTK_WIDGET(tmp_list->data)->parent)->level);
734 #endif /* TREE_DEBUG */
735       
736       sorted_list = g_list_insert_sorted(sorted_list,
737                                          tmp_list->data,
738                                          (GCompareFunc)gtk_tree_sort_item_by_depth);
739       tmp_list = g_list_next(tmp_list);
740     }
741   
742 #ifdef TREE_DEBUG
743   /* print sorted list */
744   g_message("* sorted list result\n");
745   tmp_list = sorted_list;
746   while(tmp_list)
747     {
748       g_message("* item [%#x] depth [%d]\n", 
749                 (int)tmp_list->data,
750                 (int)GTK_TREE(GTK_WIDGET(tmp_list->data)->parent)->level);
751       tmp_list = g_list_next(tmp_list);
752     }
753 #endif /* TREE_DEBUG */
754   
755 #ifdef TREE_DEBUG
756   g_message("* scan sorted list\n");
757 #endif /* TREE_DEBUG */
758   
759   tmp_list = sorted_list;
760   while (tmp_list)
761     {
762       widget = tmp_list->data;
763       tmp_list = tmp_list->next;
764       
765 #ifdef TREE_DEBUG
766       g_message("* item [%#x] subtree [%#x]\n", 
767                 (int)widget, (int)GTK_TREE_ITEM_SUBTREE(widget));
768 #endif /* TREE_DEBUG */
769       
770       /* get real owner of this widget */
771       real_tree = GTK_TREE(widget->parent);
772 #ifdef TREE_DEBUG
773       g_message("* subtree having this widget [%#x]\n", (int)real_tree);
774 #endif /* TREE_DEBUG */
775       
776       
777       if (widget->state == GTK_STATE_SELECTED)
778         {
779           selected_widgets = g_list_prepend (selected_widgets, widget);
780 #ifdef TREE_DEBUG
781           g_message("* selected widget - adding it in selected list [%#x]\n",
782                     (int)selected_widgets);
783 #endif /* TREE_DEBUG */
784         }
785       
786       /* remove this item from its real parent */
787 #ifdef TREE_DEBUG
788       g_message("* remove widget from its owner tree\n");
789 #endif /* TREE_DEBUG */
790       real_tree->children = g_list_remove (real_tree->children, widget);
791       
792       /* remove subtree associate at this item if it exist */      
793       if(GTK_TREE_ITEM(widget)->subtree) 
794         {
795 #ifdef TREE_DEBUG
796           g_message("* remove subtree associate at this item [%#x]\n",
797                     (int) GTK_TREE_ITEM(widget)->subtree);
798 #endif /* TREE_DEBUG */
799           if (GTK_WIDGET_MAPPED (GTK_TREE_ITEM(widget)->subtree))
800             gtk_widget_unmap (GTK_TREE_ITEM(widget)->subtree);
801           
802           gtk_widget_unparent (GTK_TREE_ITEM(widget)->subtree);
803           GTK_TREE_ITEM(widget)->subtree = NULL;
804         }
805       
806       /* really remove widget for this item */
807 #ifdef TREE_DEBUG
808       g_message("* unmap and unparent widget [%#x]\n", (int)widget);
809 #endif /* TREE_DEBUG */
810       if (GTK_WIDGET_MAPPED (widget))
811         gtk_widget_unmap (widget);
812       
813       gtk_widget_unparent (widget);
814       
815       /* delete subtree if there is no children in it */
816       if(real_tree->children == NULL && 
817          real_tree != root_tree)
818         {
819 #ifdef TREE_DEBUG
820           g_message("* owner tree don't have children ... destroy it\n");
821 #endif /* TREE_DEBUG */
822           gtk_tree_item_remove_subtree(GTK_TREE_ITEM(real_tree->tree_owner));
823         }
824       
825 #ifdef TREE_DEBUG
826       g_message("* next item in list\n");
827 #endif /* TREE_DEBUG */
828     }
829   
830   if (selected_widgets)
831     {
832 #ifdef TREE_DEBUG
833       g_message("* scan selected item list\n");
834 #endif /* TREE_DEBUG */
835       tmp_list = selected_widgets;
836       while (tmp_list)
837         {
838           widget = tmp_list->data;
839           tmp_list = tmp_list->next;
840           
841 #ifdef TREE_DEBUG
842           g_message("* widget [%#x] subtree [%#x]\n", 
843                     (int)widget, (int)GTK_TREE_ITEM_SUBTREE(widget));
844 #endif /* TREE_DEBUG */
845           
846           /* remove widget of selection */
847           root_tree->selection = g_list_remove (root_tree->selection, widget);
848           
849           /* unref it to authorize is destruction */
850           gtk_widget_unref (widget);
851         }
852       
853       /* emit only one selection_changed signal */
854       gtk_signal_emit (GTK_OBJECT (root_tree), 
855                        tree_signals[SELECTION_CHANGED]);
856     }
857   
858 #ifdef TREE_DEBUG
859   g_message("* free selected_widgets list\n");
860 #endif /* TREE_DEBUG */
861   g_list_free (selected_widgets);
862   g_list_free (sorted_list);
863   
864   if (root_tree->children && !root_tree->selection &&
865       (root_tree->selection_mode == GTK_SELECTION_BROWSE))
866     {
867 #ifdef TREE_DEBUG
868       g_message("* BROWSE mode, select another item\n");
869 #endif /* TREE_DEBUG */
870       widget = root_tree->children->data;
871       gtk_tree_select_child (root_tree, widget);
872     }
873   
874   if (GTK_WIDGET_VISIBLE (root_tree))
875     {
876 #ifdef TREE_DEBUG
877       g_message("* query queue resizing for root_tree\n");
878 #endif /* TREE_DEBUG */      
879       gtk_widget_queue_resize (GTK_WIDGET (root_tree));
880     }
881 }
882
883 void
884 gtk_tree_select_child (GtkTree   *tree,
885                        GtkWidget *tree_item)
886 {
887   g_return_if_fail (tree != NULL);
888   g_return_if_fail (GTK_IS_TREE (tree));
889   g_return_if_fail (tree_item != NULL);
890   g_return_if_fail (GTK_IS_TREE_ITEM (tree_item));
891   
892   gtk_signal_emit (GTK_OBJECT (tree), tree_signals[SELECT_CHILD], tree_item);
893 }
894
895 void
896 gtk_tree_select_item (GtkTree   *tree,
897                       gint       item)
898 {
899   GList *tmp_list;
900   
901   g_return_if_fail (tree != NULL);
902   g_return_if_fail (GTK_IS_TREE (tree));
903   
904   tmp_list = g_list_nth (tree->children, item);
905   if (tmp_list)
906     gtk_tree_select_child (tree, GTK_WIDGET (tmp_list->data));
907   
908 }
909
910 static void
911 gtk_tree_size_allocate (GtkWidget     *widget,
912                         GtkAllocation *allocation)
913 {
914   GtkTree *tree;
915   GtkWidget *child, *subtree;
916   GtkAllocation child_allocation;
917   GList *children;
918   
919   
920   g_return_if_fail (widget != NULL);
921   g_return_if_fail (GTK_IS_TREE (widget));
922   g_return_if_fail (allocation != NULL);
923   
924   tree = GTK_TREE (widget);
925   
926   widget->allocation = *allocation;
927   if (GTK_WIDGET_REALIZED (widget))
928     gdk_window_move_resize (widget->window,
929                             allocation->x, allocation->y,
930                             allocation->width, allocation->height);
931   
932   if (tree->children)
933     {
934       child_allocation.x = GTK_CONTAINER (tree)->border_width;
935       child_allocation.y = GTK_CONTAINER (tree)->border_width;
936       child_allocation.width = MAX (1, (gint)allocation->width - child_allocation.x * 2);
937       
938       children = tree->children;
939       
940       while (children)
941         {
942           child = children->data;
943           children = children->next;
944           
945           if (GTK_WIDGET_VISIBLE (child))
946             {
947               GtkRequisition child_requisition;
948               gtk_widget_get_child_requisition (child, &child_requisition);
949               
950               child_allocation.height = child_requisition.height;
951               
952               gtk_widget_size_allocate (child, &child_allocation);
953               
954               child_allocation.y += child_allocation.height;
955               
956               if((subtree = GTK_TREE_ITEM(child)->subtree))
957                 if(GTK_WIDGET_VISIBLE (subtree))
958                   {
959                     child_allocation.height = subtree->requisition.height;
960                     gtk_widget_size_allocate (subtree, &child_allocation);
961                     child_allocation.y += child_allocation.height;
962                   }
963             }
964         }
965     }
966   
967 }
968
969 static void
970 gtk_tree_size_request (GtkWidget      *widget,
971                        GtkRequisition *requisition)
972 {
973   GtkTree *tree;
974   GtkWidget *child, *subtree;
975   GList *children;
976   GtkRequisition child_requisition;
977   
978   
979   g_return_if_fail (widget != NULL);
980   g_return_if_fail (GTK_IS_TREE (widget));
981   g_return_if_fail (requisition != NULL);
982   
983   tree = GTK_TREE (widget);
984   requisition->width = 0;
985   requisition->height = 0;
986   
987   children = tree->children;
988   while (children)
989     {
990       child = children->data;
991       children = children->next;
992       
993       if (GTK_WIDGET_VISIBLE (child))
994         {
995           gtk_widget_size_request (child, &child_requisition);
996           
997           requisition->width = MAX (requisition->width, child_requisition.width);
998           requisition->height += child_requisition.height;
999           
1000           if((subtree = GTK_TREE_ITEM(child)->subtree) &&
1001              GTK_WIDGET_VISIBLE (subtree))
1002             {
1003               gtk_widget_size_request (subtree, &child_requisition);
1004               
1005               requisition->width = MAX (requisition->width, 
1006                                         child_requisition.width);
1007               
1008               requisition->height += child_requisition.height;
1009             }
1010         }
1011     }
1012   
1013   requisition->width += GTK_CONTAINER (tree)->border_width * 2;
1014   requisition->height += GTK_CONTAINER (tree)->border_width * 2;
1015   
1016   requisition->width = MAX (requisition->width, 1);
1017   requisition->height = MAX (requisition->height, 1);
1018   
1019 }
1020
1021 static void
1022 gtk_tree_unmap (GtkWidget *widget)
1023 {
1024   
1025   g_return_if_fail (widget != NULL);
1026   g_return_if_fail (GTK_IS_TREE (widget));
1027   
1028   GTK_WIDGET_UNSET_FLAGS (widget, GTK_MAPPED);
1029   gdk_window_hide (widget->window);
1030   
1031 }
1032
1033 void
1034 gtk_tree_unselect_child (GtkTree   *tree,
1035                          GtkWidget *tree_item)
1036 {
1037   g_return_if_fail (tree != NULL);
1038   g_return_if_fail (GTK_IS_TREE (tree));
1039   g_return_if_fail (tree_item != NULL);
1040   g_return_if_fail (GTK_IS_TREE_ITEM (tree_item));
1041   
1042   gtk_signal_emit (GTK_OBJECT (tree), tree_signals[UNSELECT_CHILD], tree_item);
1043 }
1044
1045 void
1046 gtk_tree_unselect_item (GtkTree *tree,
1047                         gint     item)
1048 {
1049   GList *tmp_list;
1050   
1051   g_return_if_fail (tree != NULL);
1052   g_return_if_fail (GTK_IS_TREE (tree));
1053   
1054   tmp_list = g_list_nth (tree->children, item);
1055   if (tmp_list)
1056     gtk_tree_unselect_child (tree, GTK_WIDGET (tmp_list->data));
1057   
1058 }
1059
1060 static void
1061 gtk_real_tree_select_child (GtkTree   *tree,
1062                             GtkWidget *child)
1063 {
1064   GList *selection, *root_selection;
1065   GList *tmp_list;
1066   GtkWidget *tmp_item;
1067   
1068   g_return_if_fail (tree != NULL);
1069   g_return_if_fail (GTK_IS_TREE (tree));
1070   g_return_if_fail (child != NULL);
1071   g_return_if_fail (GTK_IS_TREE_ITEM (child));
1072
1073   root_selection = tree->root_tree->selection;
1074   
1075   switch (tree->root_tree->selection_mode)
1076     {
1077     case GTK_SELECTION_SINGLE:
1078       
1079       selection = root_selection;
1080       
1081       /* remove old selection list */
1082       while (selection)
1083         {
1084           tmp_item = selection->data;
1085           
1086           if (tmp_item != child)
1087             {
1088               gtk_tree_item_deselect (GTK_TREE_ITEM (tmp_item));
1089               
1090               tmp_list = selection;
1091               selection = selection->next;
1092               
1093               root_selection = g_list_remove_link (root_selection, tmp_list);
1094               gtk_widget_unref (tmp_item);
1095               
1096               g_list_free (tmp_list);
1097             }
1098           else
1099             selection = selection->next;
1100         }
1101       
1102       if (child->state == GTK_STATE_NORMAL)
1103         {
1104           gtk_tree_item_select (GTK_TREE_ITEM (child));
1105           root_selection = g_list_prepend (root_selection, child);
1106           gtk_widget_ref (child);
1107         }
1108       else if (child->state == GTK_STATE_SELECTED)
1109         {
1110           gtk_tree_item_deselect (GTK_TREE_ITEM (child));
1111           root_selection = g_list_remove (root_selection, child);
1112           gtk_widget_unref (child);
1113         }
1114       
1115       tree->root_tree->selection = root_selection;
1116       
1117       gtk_signal_emit (GTK_OBJECT (tree->root_tree), 
1118                        tree_signals[SELECTION_CHANGED]);
1119       break;
1120       
1121       
1122     case GTK_SELECTION_BROWSE:
1123       selection = root_selection;
1124       
1125       while (selection)
1126         {
1127           tmp_item = selection->data;
1128           
1129           if (tmp_item != child)
1130             {
1131               gtk_tree_item_deselect (GTK_TREE_ITEM (tmp_item));
1132               
1133               tmp_list = selection;
1134               selection = selection->next;
1135               
1136               root_selection = g_list_remove_link (root_selection, tmp_list);
1137               gtk_widget_unref (tmp_item);
1138               
1139               g_list_free (tmp_list);
1140             }
1141           else
1142             selection = selection->next;
1143         }
1144       
1145       tree->root_tree->selection = root_selection;
1146       
1147       if (child->state == GTK_STATE_NORMAL)
1148         {
1149           gtk_tree_item_select (GTK_TREE_ITEM (child));
1150           root_selection = g_list_prepend (root_selection, child);
1151           gtk_widget_ref (child);
1152           tree->root_tree->selection = root_selection;
1153           gtk_signal_emit (GTK_OBJECT (tree->root_tree), 
1154                            tree_signals[SELECTION_CHANGED]);
1155         }
1156       break;
1157       
1158     case GTK_SELECTION_MULTIPLE:
1159       if (child->state == GTK_STATE_NORMAL)
1160         {
1161           gtk_tree_item_select (GTK_TREE_ITEM (child));
1162           root_selection = g_list_prepend (root_selection, child);
1163           gtk_widget_ref (child);
1164           tree->root_tree->selection = root_selection;
1165           gtk_signal_emit (GTK_OBJECT (tree->root_tree), 
1166                            tree_signals[SELECTION_CHANGED]);
1167         }
1168       else if (child->state == GTK_STATE_SELECTED)
1169         {
1170           gtk_tree_item_deselect (GTK_TREE_ITEM (child));
1171           root_selection = g_list_remove (root_selection, child);
1172           gtk_widget_unref (child);
1173           tree->root_tree->selection = root_selection;
1174           gtk_signal_emit (GTK_OBJECT (tree->root_tree), 
1175                            tree_signals[SELECTION_CHANGED]);
1176         }
1177       break;
1178       
1179     case GTK_SELECTION_EXTENDED:
1180       break;
1181     }
1182 }
1183
1184 static void
1185 gtk_real_tree_unselect_child (GtkTree   *tree,
1186                               GtkWidget *child)
1187 {
1188   g_return_if_fail (tree != NULL);
1189   g_return_if_fail (GTK_IS_TREE (tree));
1190   g_return_if_fail (child != NULL);
1191   g_return_if_fail (GTK_IS_TREE_ITEM (child));
1192   
1193   switch (tree->selection_mode)
1194     {
1195     case GTK_SELECTION_SINGLE:
1196     case GTK_SELECTION_MULTIPLE:
1197     case GTK_SELECTION_BROWSE:
1198       if (child->state == GTK_STATE_SELECTED)
1199         {
1200           GtkTree* root_tree = GTK_TREE_ROOT_TREE(tree);
1201           gtk_tree_item_deselect (GTK_TREE_ITEM (child));
1202           root_tree->selection = g_list_remove (root_tree->selection, child);
1203           gtk_widget_unref (child);
1204           gtk_signal_emit (GTK_OBJECT (tree->root_tree), 
1205                            tree_signals[SELECTION_CHANGED]);
1206         }
1207       break;
1208       
1209     case GTK_SELECTION_EXTENDED:
1210       break;
1211     }
1212 }
1213
1214 void
1215 gtk_tree_set_selection_mode (GtkTree       *tree,
1216                              GtkSelectionMode mode) 
1217 {
1218   g_return_if_fail (tree != NULL);
1219   g_return_if_fail (GTK_IS_TREE (tree));
1220   
1221   tree->selection_mode = mode;
1222 }
1223
1224 void
1225 gtk_tree_set_view_mode (GtkTree       *tree,
1226                         GtkTreeViewMode mode) 
1227 {
1228   g_return_if_fail (tree != NULL);
1229   g_return_if_fail (GTK_IS_TREE (tree));
1230   
1231   tree->view_mode = mode;
1232 }
1233
1234 void
1235 gtk_tree_set_view_lines (GtkTree       *tree,
1236                          guint          flag) 
1237 {
1238   g_return_if_fail (tree != NULL);
1239   g_return_if_fail (GTK_IS_TREE (tree));
1240   
1241   tree->view_line = flag;
1242 }