f8d95418308cfab2b75b24d6fc41e6d50598fb27
[claws.git] / src / gtk / gtkcmctree.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball, Josh MacDonald, 
3  * Copyright (C) 1997-1998 Jay Painter <jpaint@serv.net><jpaint@gimp.org>  
4  *
5  * GtkCMCTree widget for GTK+
6  * Copyright (C) 1998 Lars Hamann and Stefan Jeske
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23
24 /*
25  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
26  * file for a list of people on the GTK+ Team.  See the ChangeLog
27  * files for a list of changes.  These files are distributed with
28  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
29  */
30
31 #include <config.h>
32 #include <stdlib.h>
33
34 #include <gtk/gtk.h>
35 #include <gdk/gdkkeysyms.h>
36 #include "gtkcmctree.h"
37 #include "claws-marshal.h"
38 #include "utils.h"
39 #include "gtkutils.c"
40
41 #define PM_SIZE                    8
42 #define TAB_SIZE                   (PM_SIZE + 6)
43 #define CELL_SPACING               1
44 #define CLIST_OPTIMUM_SIZE         64
45 #define COLUMN_INSET               3
46 #define DRAG_WIDTH                 6
47
48 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
49                                     (((row) + 1) * CELL_SPACING) + \
50                                     (clist)->voffset)
51 #define ROW_FROM_YPIXEL(clist, y)  (((y) - (clist)->voffset) / \
52                                     ((clist)->row_height + CELL_SPACING))
53 #define COLUMN_LEFT_XPIXEL(clist, col)  ((clist)->column[(col)].area.x \
54                                     + (clist)->hoffset)
55 #define COLUMN_LEFT(clist, column) ((clist)->column[(column)].area.x)
56
57 GType
58 gtk_cmctree_pos_get_type (void)
59 {
60   static GType etype = 0;
61   if (etype == 0) {
62     static const GEnumValue values[] = {
63       { GTK_CMCTREE_POS_BEFORE, "GTK_CMCTREE_POS_BEFORE", "before" },
64       { GTK_CMCTREE_POS_AS_CHILD, "GTK_CMCTREE_POS_AS_CHILD", "as-child" },
65       { GTK_CMCTREE_POS_AFTER, "GTK_CMCTREE_POS_AFTER", "after" },
66       { 0, NULL, NULL }
67     };
68 #if GLIB_CHECK_VERSION(2,10,0)
69     etype = g_enum_register_static (g_intern_static_string ("GtkCMCTreePos"), values);
70 #else
71     etype = g_enum_register_static ("GtkCMCTreePos", values);
72 #endif
73   }
74   return etype;
75 }
76 GType
77 gtk_cmctree_line_style_get_type (void)
78 {
79   static GType etype = 0;
80   if (etype == 0) {
81     static const GEnumValue values[] = {
82       { GTK_CMCTREE_LINES_NONE, "GTK_CMCTREE_LINES_NONE", "none" },
83       { 0, NULL, NULL }
84     };
85 #if GLIB_CHECK_VERSION(2,10,0)
86     etype = g_enum_register_static (g_intern_static_string ("GtkCMCTreeLineStyle"), values);
87 #else
88     etype = g_enum_register_static ("GtkCMCTreeLineStyle", values);
89 #endif
90   }
91   return etype;
92 }
93 GType
94 gtk_cmctree_expander_style_get_type (void)
95 {
96   static GType etype = 0;
97   if (etype == 0) {
98     static const GEnumValue values[] = {
99       { GTK_CMCTREE_EXPANDER_NONE, "GTK_CMCTREE_EXPANDER_NONE", "none" },
100       { GTK_CMCTREE_EXPANDER_TRIANGLE, "GTK_CMCTREE_EXPANDER_TRIANGLE", "triangle" },
101       { 0, NULL, NULL }
102     };
103 #if GLIB_CHECK_VERSION(2,10,0)
104     etype = g_enum_register_static (g_intern_static_string ("GtkCMCTreeExpanderStyle"), values);
105 #else
106     etype = g_enum_register_static ("GtkCMCTreeExpanderStyle", values);
107 #endif
108   }
109   return etype;
110 }
111 GType
112 gtk_cmctree_expansion_type_get_type (void)
113 {
114   static GType etype = 0;
115   if (etype == 0) {
116     static const GEnumValue values[] = {
117       { GTK_CMCTREE_EXPANSION_EXPAND, "GTK_CMCTREE_EXPANSION_EXPAND", "expand" },
118       { GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE, "GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE", "expand-recursive" },
119       { GTK_CMCTREE_EXPANSION_COLLAPSE, "GTK_CMCTREE_EXPANSION_COLLAPSE", "collapse" },
120       { GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE, "GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE", "collapse-recursive" },
121       { GTK_CMCTREE_EXPANSION_TOGGLE, "GTK_CMCTREE_EXPANSION_TOGGLE", "toggle" },
122       { GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE, "GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE", "toggle-recursive" },
123       { 0, NULL, NULL }
124     };
125 #if GLIB_CHECK_VERSION(2,10,0)
126     etype = g_enum_register_static (g_intern_static_string ("GtkCMCTreeExpansionType"), values);
127 #else
128     etype = g_enum_register_static ("GtkCMCTreeExpansionType", values);
129 #endif
130   }
131   return etype;
132 }
133
134
135 static inline gint
136 COLUMN_FROM_XPIXEL (GtkCMCList * clist,
137                     gint x)
138 {
139   gint i, cx;
140
141   for (i = 0; i < clist->columns; i++)
142     if (clist->column[i].visible)
143       {
144         cx = clist->column[i].area.x + clist->hoffset;
145
146         if (x >= (cx - (COLUMN_INSET + CELL_SPACING)) &&
147             x <= (cx + clist->column[i].area.width + COLUMN_INSET))
148           return i;
149       }
150
151   /* no match */
152   return -1;
153 }
154
155 #define CLIST_UNFROZEN(clist)     (((GtkCMCList*) (clist))->freeze_count == 0)
156 #define CLIST_REFRESH(clist)    G_STMT_START { \
157   if (CLIST_UNFROZEN (clist)) \
158     GTK_CMCLIST_GET_CLASS (clist)->refresh ((GtkCMCList*) (clist)); \
159 } G_STMT_END
160
161
162 enum {
163   ARG_0,
164   ARG_N_COLUMNS,
165   ARG_TREE_COLUMN,
166   ARG_INDENT,
167   ARG_SPACING,
168   ARG_SHOW_STUB,
169   ARG_LINE_STYLE,
170   ARG_EXPANDER_STYLE
171 };
172
173
174 static void     gtk_cmctree_class_init    (GtkCMCTreeClass         *klass);
175 static void     gtk_cmctree_init          (GtkCMCTree              *ctree);
176 static GObject* gtk_cmctree_constructor   (GType                  type,
177                                          guint                  n_construct_properties,
178                                          GObjectConstructParam *construct_params);
179 static void gtk_cmctree_set_arg         (GObject *object,
180                                 guint      arg_id,
181                                 const GValue *value,
182                                 GParamSpec *spec);
183 static void gtk_cmctree_get_arg         (GObject *object,
184                                 guint      arg_id,
185                                 GValue *value,
186                                 GParamSpec *spec);
187 static void gtk_cmctree_realize           (GtkWidget      *widget);
188 static void gtk_cmctree_unrealize         (GtkWidget      *widget);
189 static gint gtk_cmctree_button_press      (GtkWidget      *widget,
190                                          GdkEventButton *event);
191 static void ctree_attach_styles         (GtkCMCTree       *ctree,
192                                          GtkCMCTreeNode   *node,
193                                          gpointer        data);
194 static void ctree_detach_styles         (GtkCMCTree       *ctree,
195                                          GtkCMCTreeNode   *node, 
196                                          gpointer        data);
197 static void set_cell_contents           (GtkCMCList      *clist,
198                                          GtkCMCListRow   *clist_row,
199                                          gint           column,
200                                          GtkCMCellType    type,
201                                          const gchar   *text,
202                                          guint8         spacing,
203                                          GdkPixbuf     *pixbuf);
204 static void set_node_info               (GtkCMCTree      *ctree,
205                                          GtkCMCTreeNode  *node,
206                                          const gchar   *text,
207                                          guint8         spacing,
208                                          GdkPixbuf     *pixbuf_closed,
209                                          GdkPixbuf     *pixbuf_opened,
210                                          gboolean       is_leaf,
211                                          gboolean       expanded);
212 static GtkCMCTreeRow *row_new             (GtkCMCTree      *ctree);
213 static void row_delete                  (GtkCMCTree      *ctree,
214                                          GtkCMCTreeRow   *ctree_row);
215 static void tree_delete                 (GtkCMCTree      *ctree, 
216                                          GtkCMCTreeNode  *node, 
217                                          gpointer       data);
218 static void tree_delete_row             (GtkCMCTree      *ctree, 
219                                          GtkCMCTreeNode  *node, 
220                                          gpointer       data);
221 static void real_clear                  (GtkCMCList      *clist);
222 static void tree_update_level           (GtkCMCTree      *ctree, 
223                                          GtkCMCTreeNode  *node, 
224                                          gpointer       data);
225 static void tree_select                 (GtkCMCTree      *ctree, 
226                                          GtkCMCTreeNode  *node, 
227                                          gpointer       data);
228 static void tree_unselect               (GtkCMCTree      *ctree, 
229                                          GtkCMCTreeNode  *node, 
230                                          gpointer       data);
231 static void real_select_all             (GtkCMCList      *clist);
232 static void real_unselect_all           (GtkCMCList      *clist);
233 static void tree_expand                 (GtkCMCTree      *ctree, 
234                                          GtkCMCTreeNode  *node,
235                                          gpointer       data);
236 static void tree_collapse               (GtkCMCTree      *ctree, 
237                                          GtkCMCTreeNode  *node,
238                                          gpointer       data);
239 static void tree_collapse_to_depth      (GtkCMCTree      *ctree, 
240                                          GtkCMCTreeNode  *node, 
241                                          gint           depth);
242 static void tree_toggle_expansion       (GtkCMCTree      *ctree,
243                                          GtkCMCTreeNode  *node,
244                                          gpointer       data);
245 static void change_focus_row_expansion  (GtkCMCTree      *ctree,
246                                          GtkCMCTreeExpansionType expansion);
247 static void real_select_row             (GtkCMCList      *clist,
248                                          gint           row,
249                                          gint           column,
250                                          GdkEvent      *event);
251 static void real_unselect_row           (GtkCMCList      *clist,
252                                          gint           row,
253                                          gint           column,
254                                          GdkEvent      *event);
255 static void real_tree_select            (GtkCMCTree      *ctree,
256                                          GtkCMCTreeNode  *node,
257                                          gint           column);
258 static void real_tree_unselect          (GtkCMCTree      *ctree,
259                                          GtkCMCTreeNode  *node,
260                                          gint           column);
261 static void real_tree_expand            (GtkCMCTree      *ctree,
262                                          GtkCMCTreeNode  *node);
263 static void real_tree_collapse          (GtkCMCTree      *ctree,
264                                          GtkCMCTreeNode  *node);
265 static void real_tree_move              (GtkCMCTree      *ctree,
266                                          GtkCMCTreeNode  *node,
267                                          GtkCMCTreeNode  *new_parent, 
268                                          GtkCMCTreeNode  *new_sibling);
269 static void real_row_move               (GtkCMCList      *clist,
270                                          gint           source_row,
271                                          gint           dest_row);
272 static void gtk_cmctree_link              (GtkCMCTree      *ctree,
273                                          GtkCMCTreeNode  *node,
274                                          GtkCMCTreeNode  *parent,
275                                          GtkCMCTreeNode  *sibling,
276                                          gboolean       update_focus_row);
277 static void gtk_cmctree_unlink            (GtkCMCTree      *ctree, 
278                                          GtkCMCTreeNode  *node,
279                                          gboolean       update_focus_row);
280 static GtkCMCTreeNode * gtk_cmctree_last_visible (GtkCMCTree     *ctree,
281                                               GtkCMCTreeNode *node);
282 static gboolean ctree_is_hot_spot       (GtkCMCTree      *ctree, 
283                                          GtkCMCTreeNode  *node,
284                                          gint           row, 
285                                          gint           x, 
286                                          gint           y);
287 static void tree_sort                   (GtkCMCTree      *ctree,
288                                          GtkCMCTreeNode  *node,
289                                          gpointer       data);
290 static void fake_unselect_all           (GtkCMCList      *clist,
291                                          gint           row);
292 static GList * selection_find           (GtkCMCList      *clist,
293                                          gint           row_number,
294                                          GList         *row_list_element);
295 static void resync_selection            (GtkCMCList      *clist,
296                                          GdkEvent      *event);
297 static void real_undo_selection         (GtkCMCList      *clist);
298 static void select_row_recursive        (GtkCMCTree      *ctree, 
299                                          GtkCMCTreeNode  *node, 
300                                          gpointer       data);
301 static gint real_insert_row             (GtkCMCList      *clist,
302                                          gint           row,
303                                          gchar         *text[]);
304 static void real_remove_row             (GtkCMCList      *clist,
305                                          gint           row);
306 static void real_sort_list              (GtkCMCList      *clist);
307 static void cell_size_request           (GtkCMCList       *clist,
308                                          GtkCMCListRow    *clist_row,
309                                          gint            column,
310                                          GtkRequisition *requisition);
311 static void column_auto_resize          (GtkCMCList       *clist,
312                                          GtkCMCListRow    *clist_row,
313                                          gint            column,
314                                          gint            old_width);
315 static void auto_resize_columns         (GtkCMCList       *clist);
316
317
318 static gboolean check_drag               (GtkCMCTree         *ctree,
319                                           GtkCMCTreeNode     *drag_source,
320                                           GtkCMCTreeNode     *drag_target,
321                                           GtkCMCListDragPos   insert_pos);
322 static void gtk_cmctree_drag_begin         (GtkWidget        *widget,
323                                           GdkDragContext   *context);
324 static gint gtk_cmctree_drag_motion        (GtkWidget        *widget,
325                                           GdkDragContext   *context,
326                                           gint              x,
327                                           gint              y,
328                                           guint             time);
329 static void gtk_cmctree_drag_data_received (GtkWidget        *widget,
330                                           GdkDragContext   *context,
331                                           gint              x,
332                                           gint              y,
333                                           GtkSelectionData *selection_data,
334                                           guint             info,
335                                           guint32           time);
336 static void remove_grab                  (GtkCMCList         *clist);
337 static void drag_dest_cell               (GtkCMCList         *clist,
338                                           gint              x,
339                                           gint              y,
340                                           GtkCMCListDestInfo *dest_info);
341
342
343 enum
344 {
345   TREE_SELECT_ROW,
346   TREE_UNSELECT_ROW,
347   TREE_EXPAND,
348   TREE_COLLAPSE,
349   TREE_MOVE,
350   CHANGE_FOCUS_ROW_EXPANSION,
351   LAST_SIGNAL
352 };
353
354 static GtkCMCListClass *parent_class = NULL;
355 static GtkContainerClass *container_class = NULL;
356 static guint ctree_signals[LAST_SIGNAL] = {0};
357
358
359 GType
360 gtk_cmctree_get_type (void)
361 {
362   static GType ctree_type = 0;
363
364   if (!ctree_type)
365     {
366       static const GTypeInfo ctree_info =
367       {
368                         sizeof (GtkCMCTreeClass),
369
370                         (GBaseInitFunc) NULL,
371                         (GBaseFinalizeFunc) NULL,
372
373                         (GClassInitFunc) gtk_cmctree_class_init,
374                         (GClassFinalizeFunc) NULL,
375                         NULL,   /* class_data */
376
377                         sizeof (GtkCMCTree),
378                         0,      /* n_preallocs */
379                         (GInstanceInitFunc) gtk_cmctree_init,
380       };
381
382         ctree_type = g_type_register_static (GTK_TYPE_CMCLIST, "GtkCMCTree", &ctree_info, (GTypeFlags)0);
383     }
384
385   return ctree_type;
386 }
387
388 static gint
389 draw_cell_pixbuf (GdkWindow    *window,
390                   GdkRectangle *clip_rectangle,
391                   cairo_t      *cr,
392                   GdkPixbuf    *pixbuf,
393                   gint          x,
394                   gint          y,
395                   gint          width,
396                   gint          height)
397 {
398   gint xsrc = 0;
399   gint ysrc = 0;
400
401   if (!pixbuf || (width == 0 && height == 0))
402         return x;
403
404   if (x < clip_rectangle->x)
405     {
406       xsrc = clip_rectangle->x - x;
407       width -= xsrc;
408       x = clip_rectangle->x;
409     }
410   if (x + width > clip_rectangle->x + clip_rectangle->width)
411     width = clip_rectangle->x + clip_rectangle->width - x;
412
413   if (y < clip_rectangle->y)
414     {
415       ysrc = clip_rectangle->y - y;
416       height -= ysrc;
417       y = clip_rectangle->y;
418     }
419
420   if (y + height > clip_rectangle->y + clip_rectangle->height)
421     height = clip_rectangle->y + clip_rectangle->height - y;
422
423   gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
424   cairo_paint(cr);
425
426   return x + MAX (width, 0);
427 }
428
429 static gint
430 draw_expander (GtkCMCTree     *ctree,
431                GtkCMCTreeRow  *ctree_row,
432                GtkStyle     *style,
433                GdkRectangle *clip_rectangle,
434                cairo_t      *cr,
435                gint          x)
436 {
437   GtkCMCList *clist;
438   gint justification_factor;
439   gint y;
440
441  if (ctree->expander_style == GTK_CMCTREE_EXPANDER_NONE)
442    return x;
443
444   clist = GTK_CMCLIST (ctree);
445   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT)
446     justification_factor = -1;
447   else
448     justification_factor = 1;
449   if (!GTK_CMCLIST_ROW_HEIGHT_SET(GTK_CMCLIST(clist)))
450       y = (clip_rectangle->y + (clip_rectangle->height - PM_SIZE) / 2 -
451           (clip_rectangle->height + 1) % 2) + 1;
452   else
453       y = (clip_rectangle->y + (clip_rectangle->height/2 - PM_SIZE) / 2 -
454           (clip_rectangle->height/2 + 1) % 2) + 1;
455
456   if (!ctree_row->children)
457     {
458           return x + justification_factor * (PM_SIZE + 3);
459     }
460
461   /* pixel offsets +/- 1 or +/- justification_factor here and there ..
462    * to fill correctly, somewhat ... what do I do wrong?
463    */
464   gdk_cairo_set_source_color(cr, &gtk_widget_get_style(GTK_WIDGET(ctree))->fg[GTK_STATE_NORMAL]);
465   if (ctree_row->expanded)
466   {
467     gint tmp3 = PM_SIZE / 2;
468     gint tmp6 = PM_SIZE / 6;
469     cairo_move_to(cr, x + justification_factor * (tmp3 + tmp6) + (PM_SIZE / 2), y + 1);
470     cairo_rel_line_to(cr, 0, tmp3 + tmp6 + 1);
471     cairo_rel_line_to(cr, -justification_factor * (tmp3 + tmp6) - justification_factor, -1);
472   }
473   else
474   {
475     gint tmp3 = PM_SIZE / 2;
476     gint tmp6 = PM_SIZE / 6;
477     cairo_move_to(cr, x + tmp6 - justification_factor + (PM_SIZE / 2), y + tmp6 - 1);
478     cairo_rel_line_to(cr, justification_factor * tmp3, tmp3);
479     cairo_rel_line_to(cr, -justification_factor * tmp3, tmp3);
480   }
481   cairo_fill(cr);
482
483   x += justification_factor * (PM_SIZE + 3);
484
485   return x;
486 }
487
488 static gint
489 get_offset(GtkCMCTree     *ctree,
490                       GtkCMCTreeRow  *ctree_row,
491                       gint            column,
492                       GdkRectangle   *clip_rectangle)
493 {
494   gint justify_right;
495   justify_right = (GTK_CMCLIST (ctree)->column[column].justification == GTK_JUSTIFY_RIGHT);
496
497   if (justify_right)
498       return (clip_rectangle->x + clip_rectangle->width - 1 -
499                 ctree->tree_indent * (ctree_row->level - 1));
500
501   return clip_rectangle->x + ctree->tree_indent * (ctree_row->level - 1);
502 }
503
504  static void
505 get_cell_style (GtkCMCList     *clist,
506                 GtkCMCListRow  *clist_row,
507                 gint          state,
508                 gint          column,
509                 GtkStyle    **style)
510 {
511   GtkStyle *gtkstyle;
512
513   gtkstyle = gtk_widget_get_style (GTK_WIDGET (clist));
514
515   if (clist_row->cell[column].style)
516     {
517       if (style)
518         *style = clist_row->cell[column].style;
519     }
520   else if (clist_row->style)
521     {
522       if (style)
523         *style = clist_row->style;
524     }
525   else
526     {
527       if (style)
528         *style = gtkstyle;
529     }
530 }
531
532 static gboolean filter_fg (PangoAttribute *attribute, gpointer data)
533 {
534         const PangoAttrClass *klass = attribute->klass;
535         if (klass->type == PANGO_ATTR_FOREGROUND)
536                 return TRUE;
537
538         return FALSE;   
539 }
540
541 static PangoLayout *
542 create_cell_layout (GtkCMCList       *clist,
543                                GtkCMCListRow    *clist_row,
544                                gint            column)
545 {
546   PangoLayout *layout;
547   GtkStyle *style;
548   GtkCMCell *cell;
549   gchar *text;
550
551   get_cell_style (clist, clist_row, GTK_STATE_NORMAL, column, &style);
552
553
554   cell = &clist_row->cell[column];
555   switch (cell->type)
556     {
557     case GTK_CMCELL_TEXT:
558     case GTK_CMCELL_PIXTEXT:
559       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
560               GTK_CMCELL_PIXTEXT (*cell)->text :
561               GTK_CMCELL_TEXT (*cell)->text);
562
563       if (!text)
564         return NULL;
565       
566       if (!GTK_SCTREE(clist)->use_markup[column]) {
567               layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
568                                                        ((cell->type == GTK_CMCELL_PIXTEXT) ?
569                                                         GTK_CMCELL_PIXTEXT (*cell)->text :
570                                                         GTK_CMCELL_TEXT (*cell)->text));
571               pango_layout_set_font_description (layout, style->font_desc);
572       } else {
573               PangoContext *context = gtk_widget_get_pango_context (GTK_WIDGET(clist));
574               layout = pango_layout_new (context);
575               pango_layout_set_markup (layout, text, -1);
576               pango_layout_set_font_description (layout, style->font_desc);
577               if (clist_row->state == GTK_STATE_SELECTED) {
578                       /* for selected row, we should remove any forced foreground color
579                        * or it looks like shit */
580                       PangoAttrList *list = pango_layout_get_attributes(layout);
581                       PangoAttrList *rem = pango_attr_list_filter(list, filter_fg, NULL);
582                       if (rem)
583                               pango_attr_list_unref(rem);
584               }
585       }
586       
587       return layout;
588       
589     default:
590       return NULL;
591     }
592 }
593
594
595 static void
596 draw_row (GtkCMCList     *clist,
597           GdkRectangle *area,
598           gint          row,
599           GtkCMCListRow  *clist_row)
600 {
601   GtkWidget *widget;
602   GtkStyle *style;
603   GtkCMCTree  *ctree;
604   GdkRectangle *crect;
605   GdkRectangle row_rectangle;
606   GdkRectangle cell_rectangle; 
607   GdkRectangle clip_rectangle;
608   GdkRectangle intersect_rectangle;
609   gint last_column;
610   gint offset = 0;
611   gint state;
612   gint i;
613   static GdkColor greybg={0, 0, 0, 0};
614   static gboolean color_change = TRUE;
615   cairo_t *cr;
616   GdkColor *fgcolor, *bgcolor;
617
618   cm_return_if_fail (clist != NULL);
619   widget = GTK_WIDGET (clist);
620
621   /* if the function is passed the pointer to the row instead of null,
622    * it avoids this expensive lookup */
623   if (!clist_row)
624     clist_row = (g_list_nth (clist->row_list, row))->data;
625
626   style = clist_row->style ? clist_row->style : gtk_widget_get_style (widget);
627
628   if (greybg.pixel == 0 &&
629       greybg.red == 0 &&
630       greybg.green == 0 &&
631       greybg.blue == 0) {
632         GdkColor normalbg = {0, 0xffff, 0xffff, 0xffff};
633         if (style) {
634                 normalbg = style->base[GTK_STATE_NORMAL];
635         }
636         if (normalbg.red > 0x8888 && normalbg.green > 0x8888 && normalbg.blue > 0x8888) {
637                 greybg.pixel = normalbg.pixel;
638                 greybg.red = normalbg.red - prefs_common.stripes_color_offset;
639                 greybg.green = normalbg.green - prefs_common.stripes_color_offset;
640                 greybg.blue = normalbg.blue - prefs_common.stripes_color_offset;
641         } else if (normalbg.red < 0x8888 && normalbg.green < 0x8888 && normalbg.blue < 0x8888) {
642                 greybg.pixel = normalbg.pixel;
643                 greybg.red = normalbg.red + prefs_common.stripes_color_offset;
644                 greybg.green = normalbg.green + prefs_common.stripes_color_offset;
645                 greybg.blue = normalbg.blue + prefs_common.stripes_color_offset;
646         } else {
647                 color_change = FALSE;
648         }
649   }
650
651   /* bail now if we arn't drawable yet */
652   if (!gtk_widget_is_drawable (GTK_WIDGET(clist)) || row < 0 || row >= clist->rows)
653     return;
654
655   ctree  = GTK_CMCTREE  (clist);
656
657   /* rectangle of the entire row */
658   row_rectangle.x = 0;
659   row_rectangle.y = ROW_TOP_YPIXEL (clist, row);
660   row_rectangle.width = clist->clist_window_width;
661   row_rectangle.height = clist->row_height;
662
663   /* rectangle of the cell spacing above the row */
664   cell_rectangle.x = 0;
665   cell_rectangle.y = row_rectangle.y - CELL_SPACING;
666   cell_rectangle.width = row_rectangle.width;
667   cell_rectangle.height = CELL_SPACING;
668
669   /* rectangle used to clip drawing operations, its y and height
670    * positions only need to be set once, so we set them once here. 
671    * the x and width are set withing the drawing loop below once per
672    * column */
673   clip_rectangle.y = row_rectangle.y;
674   clip_rectangle.height = row_rectangle.height;
675
676   if (prefs_common.use_stripes_everywhere && GTK_SCTREE(ctree)->show_stripes
677       && color_change && row % 2) {
678     bgcolor = &greybg;
679   } else {
680     bgcolor = &style->base[GTK_STATE_NORMAL];
681   }
682   state = clist_row->state;
683
684   cr = gdk_cairo_create(clist->clist_window);
685   
686   if (clist_row->fg_set && state != GTK_STATE_SELECTED)
687         fgcolor = &clist_row->foreground;
688   else
689         fgcolor = &style->fg[clist_row->state];
690   /* draw the cell borders */
691   if (area)
692     {
693       crect = &intersect_rectangle;
694
695       if (gdk_rectangle_intersect (area, &cell_rectangle, crect)) {
696         gdk_cairo_rectangle(cr, &cell_rectangle);
697         gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
698         cairo_fill(cr);
699         cairo_rectangle(cr, cell_rectangle.x, cell_rectangle.y + row_rectangle.height + 1,cell_rectangle.width,cell_rectangle.height);
700         cairo_fill(cr);
701       }
702     }
703   else
704     {
705       crect = &cell_rectangle;
706
707       gdk_cairo_rectangle(cr, &cell_rectangle);
708       gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
709       cairo_fill(cr);
710       cairo_rectangle(cr, cell_rectangle.x, cell_rectangle.y + row_rectangle.height + 1,cell_rectangle.width,cell_rectangle.height);
711       cairo_fill(cr);
712     }
713
714   /* the last row has to clear its bottom cell spacing too */
715   if (clist_row == clist->row_list_end->data)
716     {
717       cell_rectangle.y += clist->row_height + CELL_SPACING;
718
719       if (!area || gdk_rectangle_intersect (area, &cell_rectangle, crect))
720         {
721           gdk_cairo_rectangle(cr, crect);
722           gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
723           cairo_fill(cr);
724         }
725     }     
726
727   for (last_column = clist->columns - 1;
728        last_column >= 0 && !clist->column[last_column].visible; last_column--)
729     ;
730
731   /* iterate and draw all the columns (row cells) and draw their contents */
732   for (i = 0; i < clist->columns; i++)
733     {
734       GtkStyle *style;
735       PangoLayout *layout = NULL;
736       PangoRectangle logical_rect;
737
738       gint width;
739       gint height;
740       gint pixbuf_width;
741       gint string_width;
742       gint old_offset;
743
744       if (!clist->column[i].visible)
745         continue;
746
747       get_cell_style (clist, clist_row, state, i, &style);
748
749       /* calculate clipping region */
750       clip_rectangle.x = clist->column[i].area.x + clist->hoffset;
751       clip_rectangle.width = clist->column[i].area.width;
752
753       cell_rectangle.x = clip_rectangle.x - COLUMN_INSET - CELL_SPACING;
754       cell_rectangle.width = (clip_rectangle.width + 2 * COLUMN_INSET +
755                               (1 + (i == last_column)) * CELL_SPACING);
756       cell_rectangle.y = clip_rectangle.y;
757       cell_rectangle.height = clip_rectangle.height;
758
759       string_width = 0;
760       pixbuf_width = 0;
761       height = 0;
762
763       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
764                                             &intersect_rectangle))
765         {
766           if (i != ctree->tree_column)
767             continue;
768         }
769       else
770         {
771           gdk_cairo_rectangle(cr, &cell_rectangle);
772           if (state == GTK_STATE_NORMAL)
773                 gdk_cairo_set_source_color(cr, bgcolor);
774           else
775                 gdk_cairo_set_source_color(cr, &style->base[state]);
776           cairo_fill(cr);
777           layout = create_cell_layout (clist, clist_row, i);
778           if (layout)
779             {
780               pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
781               width = logical_rect.width;
782             }
783           else
784             width = 0;
785
786           switch (clist_row->cell[i].type)
787             {
788             case GTK_CMCELL_PIXBUF:
789               pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
790               height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
791               width += pixbuf_width;
792               break;
793             case GTK_CMCELL_PIXTEXT:
794               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
795                 {
796                   pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
797                   height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
798                   width += pixbuf_width;
799                 }
800
801               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->text &&
802                   GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
803                 width +=  GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
804
805               if (i == ctree->tree_column)
806                 width += (ctree->tree_indent *
807                           ((GtkCMCTreeRow *)clist_row)->level);
808               break;
809             default:
810               break;
811             }
812
813           switch (clist->column[i].justification)
814             {
815             case GTK_JUSTIFY_LEFT:
816               offset = clip_rectangle.x + clist_row->cell[i].horizontal;
817               break;
818             case GTK_JUSTIFY_RIGHT:
819               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
820                         clip_rectangle.width - width);
821               break;
822             case GTK_JUSTIFY_CENTER:
823             case GTK_JUSTIFY_FILL:
824               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
825                         (clip_rectangle.width / 2) - (width / 2));
826               break;
827             };
828
829           if (i != ctree->tree_column)
830             {
831               int start_y = (clip_rectangle.height - height) / 2;
832               if (GTK_CMCLIST_ROW_HEIGHT_SET(GTK_CMCLIST(clist)))
833                       start_y = (clip_rectangle.height/2 - height) / 2;
834
835               offset += clist_row->cell[i].horizontal;
836               switch (clist_row->cell[i].type)
837                 {
838                 case GTK_CMCELL_PIXBUF:
839                   draw_cell_pixbuf
840                     (clist->clist_window, &clip_rectangle, cr,
841                      GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf,
842                      offset,
843                      clip_rectangle.y + clist_row->cell[i].vertical +
844                      start_y,
845                      pixbuf_width, height);
846                   break;
847                 case GTK_CMCELL_PIXTEXT:
848                   offset = draw_cell_pixbuf
849                     (clist->clist_window, &clip_rectangle, cr,
850                      GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
851                      offset,
852                      clip_rectangle.y + clist_row->cell[i].vertical +
853                      start_y,
854                      pixbuf_width, height);
855                   offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
856
857                   /* Fall through */
858                 case GTK_CMCELL_TEXT:
859                   if (layout)
860                     {
861                       gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
862                       gdk_cairo_set_source_color(cr, fgcolor);
863                       cairo_move_to(cr, offset, row_rectangle.y + row_center_offset + clist_row->cell[i].vertical);
864                       pango_cairo_show_layout(cr, layout);
865                       g_object_unref (G_OBJECT (layout));
866                     }
867                   break;
868                 default:
869                   break;
870                 }
871               continue;
872             }
873         }
874
875       /* draw ctree->tree_column */
876       cell_rectangle.y -= CELL_SPACING;
877       cell_rectangle.height += CELL_SPACING;
878
879       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
880                                             &intersect_rectangle))
881         {
882           if (layout)
883             g_object_unref (G_OBJECT (layout));
884           continue;
885         }
886
887
888       /* draw lines */
889       offset = get_offset (ctree, (GtkCMCTreeRow *)clist_row, i,
890                                       &clip_rectangle);
891
892       /* draw expander */
893       offset = draw_expander (ctree, (GtkCMCTreeRow *)clist_row,
894                                         style, &clip_rectangle, cr, offset);
895
896       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
897         offset -= ctree->tree_spacing;
898       else
899         offset += ctree->tree_spacing;
900
901       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
902         offset -= (pixbuf_width + clist_row->cell[i].horizontal);
903       else
904         offset += clist_row->cell[i].horizontal;
905
906       old_offset = offset;
907       offset = draw_cell_pixbuf (clist->clist_window, &clip_rectangle, cr,
908                                  GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
909                                  offset, 
910                                  clip_rectangle.y + clist_row->cell[i].vertical
911                                  + (clip_rectangle.height - height) / 2,
912                                  pixbuf_width, height);
913
914       if (layout)
915         {
916           gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
917           
918           if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
919             {
920               offset = (old_offset - string_width);
921               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
922                 offset -= GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
923             }
924           else
925             {
926               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
927                 offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
928             }
929           
930           cairo_move_to(cr, offset, row_rectangle.y + row_center_offset + clist_row->cell[i].vertical);
931           gdk_cairo_set_source_color(cr, fgcolor);
932           pango_cairo_show_layout(cr, layout);
933           g_object_unref (G_OBJECT (layout));
934         }
935     }
936    /* draw focus rectangle */
937   if (clist->focus_row == row &&
938       gtk_widget_get_can_focus (widget) && gtk_widget_has_focus (widget)
939        && state == GTK_STATE_SELECTED)
940     {
941       if (!area || gdk_rectangle_intersect (area, &row_rectangle,
942                                         &intersect_rectangle))
943         {
944             cairo_set_line_width(cr, 1.0);
945             cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
946             gdk_cairo_set_source_color(cr, &style->fg[GTK_STATE_NORMAL]);
947             cairo_move_to (cr, row_rectangle.x, row_rectangle.y + 0.5);
948             cairo_line_to (cr, row_rectangle.x + row_rectangle.width, row_rectangle.y + 0.5);
949             cairo_move_to (cr, row_rectangle.x, row_rectangle.y + row_rectangle.height - 0.5);
950             cairo_line_to (cr, row_rectangle.x + row_rectangle.width, row_rectangle.y + row_rectangle.height - 0.5);
951             cairo_stroke(cr);
952         }
953      }
954     cairo_destroy(cr);
955 }
956
957 static void
958 gtk_cmctree_class_init (GtkCMCTreeClass *klass)
959 {
960   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
961 #if !GTK_CHECK_VERSION(3, 0, 0)
962   GtkObjectClass *object_class;
963 #else /* for simplicity */
964   GtkWidgetClass *object_class;
965 #endif
966   GtkWidgetClass *widget_class;
967   GtkCMCListClass *clist_class;
968   GtkBindingSet *binding_set;
969
970   gobject_class->constructor = gtk_cmctree_constructor;
971
972 #if !GTK_CHECK_VERSION(3, 0, 0)
973   object_class = (GtkObjectClass *) klass;
974 #else /* for simplicity */
975   object_class = (GtkWidgetClass *) klass;
976 #endif
977   widget_class = (GtkWidgetClass *) klass;
978   container_class = (GtkContainerClass *) klass;
979   clist_class = (GtkCMCListClass *) klass;
980
981   parent_class = g_type_class_peek (GTK_TYPE_CMCLIST);
982   container_class = g_type_class_peek (GTK_TYPE_CONTAINER);
983
984   gobject_class->set_property = gtk_cmctree_set_arg;
985   gobject_class->get_property = gtk_cmctree_get_arg;
986
987   widget_class->realize = gtk_cmctree_realize;
988   widget_class->unrealize = gtk_cmctree_unrealize;
989   widget_class->button_press_event = gtk_cmctree_button_press;
990
991   widget_class->drag_begin = gtk_cmctree_drag_begin;
992   widget_class->drag_motion = gtk_cmctree_drag_motion;
993   widget_class->drag_data_received = gtk_cmctree_drag_data_received;
994
995   clist_class->select_row = real_select_row;
996   clist_class->unselect_row = real_unselect_row;
997   clist_class->row_move = real_row_move;
998   clist_class->undo_selection = real_undo_selection;
999   clist_class->resync_selection = resync_selection;
1000   clist_class->selection_find = selection_find;
1001   clist_class->click_column = NULL;
1002   clist_class->draw_row = draw_row;
1003   clist_class->clear = real_clear;
1004   clist_class->select_all = real_select_all;
1005   clist_class->unselect_all = real_unselect_all;
1006   clist_class->fake_unselect_all = fake_unselect_all;
1007   clist_class->insert_row = real_insert_row;
1008   clist_class->remove_row = real_remove_row;
1009   clist_class->sort_list = real_sort_list;
1010   clist_class->set_cell_contents = set_cell_contents;
1011   clist_class->cell_size_request = cell_size_request;
1012
1013   klass->tree_select_row = real_tree_select;
1014   klass->tree_unselect_row = real_tree_unselect;
1015   klass->tree_expand = real_tree_expand;
1016   klass->tree_collapse = real_tree_collapse;
1017   klass->tree_move = real_tree_move;
1018   klass->change_focus_row_expansion = change_focus_row_expansion;
1019
1020   g_object_class_install_property (gobject_class,
1021                                 ARG_N_COLUMNS,
1022                                 g_param_spec_uint ("n-columns",
1023                                 "N-Columns",
1024                                 "N-Columns",
1025                                 1,
1026                                 G_MAXINT,
1027                                 1,
1028                                 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
1029   g_object_class_install_property (gobject_class,
1030                                 ARG_TREE_COLUMN,
1031                                 g_param_spec_uint ("tree-column",
1032                                 "tree-column",
1033                                 "tree-column",
1034                                 0,
1035                                 G_MAXINT,
1036                                 0,
1037                                 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
1038   g_object_class_install_property (gobject_class,
1039                                 ARG_INDENT,
1040                                 g_param_spec_uint ("indent",
1041                                 "indent",
1042                                 "indent",
1043                                 1,
1044                                 G_MAXINT,
1045                                 1,
1046                                 G_PARAM_READWRITE));
1047   g_object_class_install_property (gobject_class,
1048                                 ARG_SPACING,
1049                                 g_param_spec_uint ("spacing",
1050                                 "spacing",
1051                                 "spacing",
1052                                 1,
1053                                 G_MAXINT,
1054                                 1,
1055                                 G_PARAM_READWRITE));
1056   g_object_class_install_property (gobject_class,
1057                                 ARG_SHOW_STUB,
1058                                 g_param_spec_boolean ("show-stub",
1059                                 "show-stub",
1060                                 "show-stub",
1061                                 TRUE,
1062                                 G_PARAM_READWRITE));
1063   g_object_class_install_property (gobject_class,
1064                                 ARG_LINE_STYLE,
1065                                 g_param_spec_enum ("line-style",
1066                                 "line-style",
1067                                 "line-style",
1068                                 GTK_TYPE_CMCTREE_LINE_STYLE, 0,
1069                                 G_PARAM_READWRITE));
1070   g_object_class_install_property (gobject_class,
1071                                 ARG_EXPANDER_STYLE,
1072                                 g_param_spec_enum ("expander-style",
1073                                 "expander-style",
1074                                 "expander-style",
1075                                 GTK_TYPE_CMCTREE_EXPANDER_STYLE, 0,
1076                                 G_PARAM_READWRITE));
1077
1078   ctree_signals[TREE_SELECT_ROW] =
1079                 g_signal_new ("tree_select_row",
1080                               G_TYPE_FROM_CLASS (object_class),
1081                               G_SIGNAL_RUN_FIRST,
1082                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_select_row),
1083                               NULL, NULL,
1084                               claws_marshal_VOID__POINTER_INT,
1085                               G_TYPE_NONE, 2,
1086                               GTK_TYPE_CMCTREE_NODE,
1087                               G_TYPE_INT);
1088   ctree_signals[TREE_UNSELECT_ROW] =
1089                 g_signal_new ("tree_unselect_row",
1090                               G_TYPE_FROM_CLASS (object_class),
1091                               G_SIGNAL_RUN_FIRST,
1092                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_unselect_row),
1093                               NULL, NULL,
1094                               claws_marshal_VOID__POINTER_INT,
1095                               G_TYPE_NONE, 2,
1096                               GTK_TYPE_CMCTREE_NODE,
1097                               G_TYPE_INT);
1098   ctree_signals[TREE_EXPAND] =
1099                 g_signal_new ("tree_expand",
1100                               G_TYPE_FROM_CLASS (object_class),
1101                               G_SIGNAL_RUN_LAST,
1102                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_expand),
1103                               NULL, NULL,
1104                               claws_marshal_VOID__POINTER,
1105                               G_TYPE_NONE, 1,
1106                               GTK_TYPE_CMCTREE_NODE);
1107   ctree_signals[TREE_COLLAPSE] =
1108                 g_signal_new ("tree_collapse",
1109                               G_TYPE_FROM_CLASS (object_class),
1110                               G_SIGNAL_RUN_LAST,
1111                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_collapse),
1112                               NULL, NULL,
1113                               claws_marshal_VOID__POINTER,
1114                               G_TYPE_NONE, 1,
1115                               GTK_TYPE_CMCTREE_NODE);
1116   ctree_signals[TREE_MOVE] =
1117                 g_signal_new ("tree_move",
1118                               G_TYPE_FROM_CLASS (object_class),
1119                               G_SIGNAL_RUN_LAST,
1120                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_move),
1121                               NULL, NULL,
1122                               claws_marshal_VOID__POINTER_POINTER_POINTER,
1123                               G_TYPE_NONE, 3,
1124                               GTK_TYPE_CMCTREE_NODE,GTK_TYPE_CMCTREE_NODE,GTK_TYPE_CMCTREE_NODE);
1125   ctree_signals[CHANGE_FOCUS_ROW_EXPANSION] =
1126                 g_signal_new ("change_focus_row_expansion",
1127                               G_TYPE_FROM_CLASS (object_class),
1128                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1129                               G_STRUCT_OFFSET (GtkCMCTreeClass, change_focus_row_expansion),
1130                               NULL, NULL,
1131                               claws_marshal_VOID__ENUM,
1132                               G_TYPE_NONE, 1, GTK_TYPE_CMCTREE_EXPANSION_TYPE);
1133
1134   binding_set = gtk_binding_set_by_class (klass);
1135   gtk_binding_entry_add_signal (binding_set,
1136                                 GDK_KEY_plus, 0,
1137                                 "change_focus_row_expansion", 1,
1138                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND);
1139   gtk_binding_entry_add_signal (binding_set,
1140                                 GDK_KEY_plus, GDK_CONTROL_MASK,
1141                                 "change_focus_row_expansion", 1,
1142                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE);
1143
1144   gtk_binding_entry_add_signal (binding_set,
1145                                 GDK_KEY_KP_Add, 0,
1146                                 "change_focus_row_expansion", 1,
1147                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND);
1148   gtk_binding_entry_add_signal (binding_set,
1149                                 GDK_KEY_KP_Add, GDK_CONTROL_MASK,
1150                                 "change_focus_row_expansion", 1,
1151                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE);
1152   
1153   gtk_binding_entry_add_signal (binding_set,
1154                                 GDK_KEY_minus, 0,
1155                                 "change_focus_row_expansion", 1,
1156                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_COLLAPSE);
1157   gtk_binding_entry_add_signal (binding_set,
1158                                 GDK_KEY_minus, GDK_CONTROL_MASK,
1159                                 "change_focus_row_expansion", 1,
1160                                 G_TYPE_ENUM,
1161                                 GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE);
1162   gtk_binding_entry_add_signal (binding_set,
1163                                 GDK_KEY_KP_Subtract, 0,
1164                                 "change_focus_row_expansion", 1,
1165                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_COLLAPSE);
1166   gtk_binding_entry_add_signal (binding_set,
1167                                 GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
1168                                 "change_focus_row_expansion", 1,
1169                                 G_TYPE_ENUM,
1170                                 GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE);
1171   gtk_binding_entry_add_signal (binding_set,
1172                                 GDK_KEY_equal, 0,
1173                                 "change_focus_row_expansion", 1,
1174                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1175   gtk_binding_entry_add_signal (binding_set,
1176                                 GDK_KEY_KP_Equal, 0,
1177                                 "change_focus_row_expansion", 1,
1178                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1179   gtk_binding_entry_add_signal (binding_set,
1180                                 GDK_KEY_KP_Multiply, 0,
1181                                 "change_focus_row_expansion", 1,
1182                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1183   gtk_binding_entry_add_signal (binding_set,
1184                                 GDK_KEY_asterisk, 0,
1185                                 "change_focus_row_expansion", 1,
1186                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1187   gtk_binding_entry_add_signal (binding_set,
1188                                 GDK_KEY_KP_Multiply, GDK_CONTROL_MASK,
1189                                 "change_focus_row_expansion", 1,
1190                                 G_TYPE_ENUM,
1191                                 GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE);
1192   gtk_binding_entry_add_signal (binding_set,
1193                                 GDK_KEY_asterisk, GDK_CONTROL_MASK,
1194                                 "change_focus_row_expansion", 1,
1195                                 G_TYPE_ENUM,
1196                                 GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE);  
1197 }
1198
1199 static void
1200 gtk_cmctree_set_arg (GObject *object,
1201                                 guint      arg_id,
1202                                 const GValue *value,
1203                                 GParamSpec *spec)
1204 {
1205   GtkCMCTree *ctree;
1206   GtkCMCList *clist;
1207
1208   ctree = GTK_CMCTREE (object);
1209   clist = GTK_CMCLIST (ctree);
1210
1211   switch (arg_id)
1212     {
1213     case ARG_N_COLUMNS: /* construct-only arg, only set at construction time */
1214 #if !GLIB_CHECK_VERSION(2,10,0)
1215       cm_return_if_fail (clist->row_mem_chunk == NULL);
1216 #endif
1217       clist->columns = MAX (1, g_value_get_uint (value));
1218 #if !GLIB_CHECK_VERSION(2,10,0)
1219       clist->row_mem_chunk = g_mem_chunk_new ("ctree row mem chunk",
1220                                               sizeof (GtkCMCTreeRow),
1221                                               sizeof (GtkCMCTreeRow)
1222                                               * CLIST_OPTIMUM_SIZE,
1223                                               G_ALLOC_AND_FREE);
1224       clist->cell_mem_chunk = g_mem_chunk_new ("ctree cell mem chunk",
1225                                                sizeof (GtkCMCell) * clist->columns,
1226                                                sizeof (GtkCMCell) * clist->columns
1227                                                * CLIST_OPTIMUM_SIZE,
1228                                                G_ALLOC_AND_FREE);
1229 #endif
1230       ctree->tree_column = CLAMP (ctree->tree_column, 0, clist->columns);
1231       break;
1232     case ARG_TREE_COLUMN: /* construct-only arg, only set at construction time */
1233       ctree->tree_column = g_value_get_uint (value);
1234 #if !GLIB_CHECK_VERSION(2,10,0)
1235       if (clist->row_mem_chunk)
1236 #endif
1237         ctree->tree_column = CLAMP (ctree->tree_column, 0, clist->columns);
1238       break;
1239     case ARG_INDENT:
1240       gtk_cmctree_set_indent (ctree, g_value_get_uint (value));
1241       break;
1242     case ARG_SPACING:
1243       gtk_cmctree_set_spacing (ctree, g_value_get_uint (value));
1244       break;
1245     case ARG_SHOW_STUB:
1246       gtk_cmctree_set_show_stub (ctree, g_value_get_boolean (value));
1247       break;
1248     case ARG_LINE_STYLE:
1249       gtk_cmctree_set_line_style (ctree, g_value_get_enum (value));
1250       break;
1251     case ARG_EXPANDER_STYLE:
1252       gtk_cmctree_set_expander_style (ctree, g_value_get_enum (value));
1253       break;
1254     default:
1255       break;
1256     }
1257 }
1258
1259 static void
1260 gtk_cmctree_get_arg (GObject *object,
1261                                 guint      arg_id,
1262                                 GValue *value,
1263                                 GParamSpec *spec)
1264 {
1265   GtkCMCTree *ctree;
1266
1267   ctree = GTK_CMCTREE (object);
1268
1269   switch (arg_id)
1270     {
1271     case ARG_N_COLUMNS:
1272       g_value_set_uint(value, GTK_CMCLIST (ctree)->columns);
1273       break;
1274     case ARG_TREE_COLUMN:
1275       g_value_set_uint(value, ctree->tree_column);
1276       break;
1277     case ARG_INDENT:
1278       g_value_set_uint(value, ctree->tree_indent);
1279       break;
1280     case ARG_SPACING:
1281       g_value_set_uint(value, ctree->tree_spacing);
1282       break;
1283     case ARG_SHOW_STUB:
1284       g_value_set_boolean(value, ctree->show_stub);
1285       break;
1286     case ARG_LINE_STYLE:
1287       g_value_set_enum(value, ctree->line_style);
1288       break;
1289     case ARG_EXPANDER_STYLE:
1290       g_value_set_enum(value, ctree->expander_style);
1291       break;
1292     default:
1293       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, spec);
1294       break;
1295     }
1296 }
1297
1298 static void
1299 gtk_cmctree_init (GtkCMCTree *ctree)
1300 {
1301   GtkCMCList *clist;
1302
1303   GTK_CMCLIST_SET_FLAG (ctree, CMCLIST_DRAW_DRAG_RECT);
1304   GTK_CMCLIST_SET_FLAG (ctree, CMCLIST_DRAW_DRAG_LINE);
1305
1306   clist = GTK_CMCLIST (ctree);
1307
1308   ctree->tree_indent    = 20;
1309   ctree->tree_spacing   = 5;
1310   ctree->tree_column    = 0;
1311   ctree->line_style     = GTK_CMCTREE_LINES_NONE;
1312   ctree->expander_style = GTK_CMCTREE_EXPANDER_TRIANGLE;
1313   ctree->drag_compare   = NULL;
1314   ctree->show_stub      = TRUE;
1315
1316   clist->button_actions[0] |= GTK_CMBUTTON_EXPANDS;
1317 }
1318
1319 static void
1320 ctree_attach_styles (GtkCMCTree     *ctree,
1321                      GtkCMCTreeNode *node,
1322                      gpointer      data)
1323 {
1324   GtkCMCList *clist;
1325   gint i;
1326
1327   clist = GTK_CMCLIST (ctree);
1328
1329   if (GTK_CMCTREE_ROW (node)->row.style)
1330     GTK_CMCTREE_ROW (node)->row.style =
1331       gtk_style_attach (GTK_CMCTREE_ROW (node)->row.style, clist->clist_window);
1332
1333 #if !GTK_CHECK_VERSION(3, 0, 0)
1334   if (GTK_CMCTREE_ROW (node)->row.fg_set || GTK_CMCTREE_ROW (node)->row.bg_set)
1335     {
1336       GdkColormap *colormap;
1337
1338       colormap = gtk_widget_get_colormap (GTK_WIDGET (ctree));
1339       if (GTK_CMCTREE_ROW (node)->row.fg_set)
1340         gdk_colormap_alloc_color (colormap, &(GTK_CMCTREE_ROW (node)->row.foreground), TRUE, TRUE);
1341       if (GTK_CMCTREE_ROW (node)->row.bg_set)
1342         gdk_colormap_alloc_color (colormap, &(GTK_CMCTREE_ROW (node)->row.background), TRUE, TRUE);
1343     }
1344 #endif
1345
1346   for (i = 0; i < clist->columns; i++)
1347     if  (GTK_CMCTREE_ROW (node)->row.cell[i].style)
1348       GTK_CMCTREE_ROW (node)->row.cell[i].style =
1349         gtk_style_attach (GTK_CMCTREE_ROW (node)->row.cell[i].style,
1350                           clist->clist_window);
1351 }
1352
1353 static void
1354 ctree_detach_styles (GtkCMCTree     *ctree,
1355                      GtkCMCTreeNode *node,
1356                      gpointer      data)
1357 {
1358   GtkCMCList *clist;
1359   gint i;
1360
1361   clist = GTK_CMCLIST (ctree);
1362
1363   if (GTK_CMCTREE_ROW (node)->row.style)
1364     gtk_style_detach (GTK_CMCTREE_ROW (node)->row.style);
1365   for (i = 0; i < clist->columns; i++)
1366     if  (GTK_CMCTREE_ROW (node)->row.cell[i].style)
1367       gtk_style_detach (GTK_CMCTREE_ROW (node)->row.cell[i].style);
1368 }
1369
1370 static void
1371 gtk_cmctree_realize (GtkWidget *widget)
1372 {
1373   GtkCMCTree *ctree;
1374   GtkCMCList *clist;
1375   GtkCMCTreeNode *node;
1376   GtkCMCTreeNode *child;
1377   gint i;
1378
1379   cm_return_if_fail (GTK_IS_CMCTREE (widget));
1380
1381   GTK_WIDGET_CLASS (parent_class)->realize (widget);
1382
1383   ctree = GTK_CMCTREE (widget);
1384   clist = GTK_CMCLIST (widget);
1385
1386   node = GTK_CMCTREE_NODE (clist->row_list);
1387   for (i = 0; i < clist->rows; i++)
1388     {
1389       if (GTK_CMCTREE_ROW (node)->children && !GTK_CMCTREE_ROW (node)->expanded)
1390         for (child = GTK_CMCTREE_ROW (node)->children; child;
1391              child = GTK_CMCTREE_ROW (child)->sibling)
1392           gtk_cmctree_pre_recursive (ctree, child, ctree_attach_styles, NULL);
1393       node = GTK_CMCTREE_NODE_NEXT (node);
1394     }
1395 }
1396
1397 static void
1398 gtk_cmctree_unrealize (GtkWidget *widget)
1399 {
1400   GtkCMCTree *ctree;
1401   GtkCMCList *clist;
1402
1403   cm_return_if_fail (GTK_IS_CMCTREE (widget));
1404
1405   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
1406
1407   ctree = GTK_CMCTREE (widget);
1408   clist = GTK_CMCLIST (widget);
1409
1410   if (gtk_widget_get_realized (widget))
1411     {
1412       GtkCMCTreeNode *node;
1413       GtkCMCTreeNode *child;
1414       gint i;
1415
1416       node = GTK_CMCTREE_NODE (clist->row_list);
1417       for (i = 0; i < clist->rows; i++)
1418         {
1419           if (GTK_CMCTREE_ROW (node)->children &&
1420               !GTK_CMCTREE_ROW (node)->expanded)
1421             for (child = GTK_CMCTREE_ROW (node)->children; child;
1422                  child = GTK_CMCTREE_ROW (child)->sibling)
1423               gtk_cmctree_pre_recursive(ctree, child, ctree_detach_styles, NULL);
1424           node = GTK_CMCTREE_NODE_NEXT (node);
1425         }
1426     }
1427 }
1428
1429 static gint
1430 gtk_cmctree_button_press (GtkWidget      *widget,
1431                         GdkEventButton *event)
1432 {
1433   GtkCMCTree *ctree;
1434   GtkCMCList *clist;
1435   gint button_actions;
1436
1437   cm_return_val_if_fail (GTK_IS_CMCTREE (widget), FALSE);
1438   cm_return_val_if_fail (event != NULL, FALSE);
1439
1440   ctree = GTK_CMCTREE (widget);
1441   clist = GTK_CMCLIST (widget);
1442
1443   button_actions = clist->button_actions[event->button - 1];
1444
1445   if (button_actions == GTK_CMBUTTON_IGNORED)
1446     return FALSE;
1447
1448   if (event->window == clist->clist_window)
1449     {
1450       GtkCMCTreeNode *work;
1451       gint x;
1452       gint y;
1453       gint row;
1454       gint column;
1455
1456       x = event->x;
1457       y = event->y;
1458
1459       if (!gtk_cmclist_get_selection_info (clist, x, y, &row, &column))
1460         return FALSE;
1461
1462       work = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
1463           
1464       if (button_actions & GTK_CMBUTTON_EXPANDS &&
1465           (GTK_CMCTREE_ROW (work)->children && !GTK_CMCTREE_ROW (work)->is_leaf  &&
1466            (event->type == GDK_2BUTTON_PRESS ||
1467             ctree_is_hot_spot (ctree, work, row, x, y))))
1468         {
1469           if (GTK_CMCTREE_ROW (work)->expanded)
1470             gtk_cmctree_collapse (ctree, work);
1471           else
1472             gtk_cmctree_expand (ctree, work);
1473
1474           return TRUE;
1475         }
1476     }
1477   
1478   return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1479 }
1480
1481 static GtkCMCTreeNode *
1482 gtk_cmctree_last_visible (GtkCMCTree     *ctree,
1483                         GtkCMCTreeNode *node)
1484 {
1485   GtkCMCTreeNode *work;
1486   
1487   if (!node)
1488     return NULL;
1489
1490   work = GTK_CMCTREE_ROW (node)->children;
1491
1492   if (!work || !GTK_CMCTREE_ROW (node)->expanded)
1493     return node;
1494
1495   while (GTK_CMCTREE_ROW (work)->sibling)
1496     work = GTK_CMCTREE_ROW (work)->sibling;
1497
1498   return gtk_cmctree_last_visible (ctree, work);
1499 }
1500
1501 static void
1502 gtk_cmctree_link (GtkCMCTree     *ctree,
1503                 GtkCMCTreeNode *node,
1504                 GtkCMCTreeNode *parent,
1505                 GtkCMCTreeNode *sibling,
1506                 gboolean      update_focus_row)
1507 {
1508   GtkCMCList *clist;
1509   GList *list_end;
1510   GList *list;
1511   GList *work;
1512   gboolean visible = FALSE;
1513   gint rows = 0;
1514   
1515   if (sibling)
1516     cm_return_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent);
1517   cm_return_if_fail (node != NULL);
1518   cm_return_if_fail (node != sibling);
1519   cm_return_if_fail (node != parent);
1520
1521   clist = GTK_CMCLIST (ctree);
1522
1523   if (update_focus_row && clist->selection_mode == GTK_SELECTION_MULTIPLE)
1524     {
1525       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1526       
1527       g_list_free (clist->undo_selection);
1528       g_list_free (clist->undo_unselection);
1529       clist->undo_selection = NULL;
1530       clist->undo_unselection = NULL;
1531     }
1532
1533   for (rows = 1, list_end = (GList *)node; list_end->next;
1534        list_end = list_end->next)
1535     rows++;
1536
1537   GTK_CMCTREE_ROW (node)->parent = parent;
1538   GTK_CMCTREE_ROW (node)->sibling = sibling;
1539
1540   if (!parent || (parent && (gtk_cmctree_is_viewable (ctree, parent) &&
1541                              GTK_CMCTREE_ROW (parent)->expanded)))
1542     {
1543       visible = TRUE;
1544       clist->rows += rows;
1545     }
1546
1547   if (parent)
1548     work = (GList *)(GTK_CMCTREE_ROW (parent)->children);
1549   else
1550     work = clist->row_list;
1551
1552   if (sibling)
1553     {
1554       if (work != (GList *)sibling)
1555         {
1556           while (GTK_CMCTREE_ROW (work)->sibling != sibling)
1557             work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
1558           GTK_CMCTREE_ROW (work)->sibling = node;
1559         }
1560
1561       if (sibling == GTK_CMCTREE_NODE (clist->row_list))
1562         clist->row_list = (GList *) node;
1563       if (GTK_CMCTREE_NODE_PREV (sibling) &&
1564           GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (sibling)) == sibling)
1565         {
1566           list = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
1567           list->next = (GList *)node;
1568         }
1569       
1570       list = (GList *)node;
1571       list->prev = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
1572       list_end->next = (GList *)sibling;
1573       list = (GList *)sibling;
1574       list->prev = list_end;
1575       if (parent && GTK_CMCTREE_ROW (parent)->children == sibling)
1576         GTK_CMCTREE_ROW (parent)->children = node;
1577     }
1578   else
1579     {
1580       if (work)
1581         {
1582           /* find sibling */
1583           while (GTK_CMCTREE_ROW (work)->sibling)
1584             work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
1585           GTK_CMCTREE_ROW (work)->sibling = node;
1586           
1587           /* find last visible child of sibling */
1588           work = (GList *) gtk_cmctree_last_visible (ctree,
1589                                                    GTK_CMCTREE_NODE (work));
1590           
1591           list_end->next = work->next;
1592           if (work->next)
1593             list = work->next->prev = list_end;
1594           work->next = (GList *)node;
1595           list = (GList *)node;
1596           list->prev = work;
1597         }
1598       else
1599         {
1600           if (parent)
1601             {
1602               GTK_CMCTREE_ROW (parent)->children = node;
1603               list = (GList *)node;
1604               list->prev = (GList *)parent;
1605               if (GTK_CMCTREE_ROW (parent)->expanded)
1606                 {
1607                   list_end->next = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
1608                   if (GTK_CMCTREE_NODE_NEXT(parent))
1609                     {
1610                       list = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
1611                       list->prev = list_end;
1612                     }
1613                   list = (GList *)parent;
1614                   list->next = (GList *)node;
1615                 }
1616               else
1617                 list_end->next = NULL;
1618             }
1619           else
1620             {
1621               clist->row_list = (GList *)node;
1622               list = (GList *)node;
1623               list->prev = NULL;
1624               list_end->next = NULL;
1625             }
1626         }
1627     }
1628
1629   gtk_cmctree_pre_recursive (ctree, node, tree_update_level, NULL); 
1630
1631   if (clist->row_list_end == NULL ||
1632       clist->row_list_end->next == (GList *)node)
1633     clist->row_list_end = list_end;
1634
1635   if (visible && update_focus_row)
1636     {
1637       gint pos;
1638           
1639       pos = g_list_position (clist->row_list, (GList *)node);
1640   
1641       if (pos <= clist->focus_row)
1642         {
1643           clist->focus_row += rows;
1644           clist->undo_anchor = clist->focus_row;
1645         }
1646     }
1647 }
1648
1649 static void
1650 gtk_cmctree_unlink (GtkCMCTree     *ctree, 
1651                   GtkCMCTreeNode *node,
1652                   gboolean      update_focus_row)
1653 {
1654   GtkCMCList *clist;
1655   gint rows;
1656   gint level;
1657   gint visible;
1658   GtkCMCTreeNode *work;
1659   GtkCMCTreeNode *parent;
1660   GList *list;
1661
1662   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1663   cm_return_if_fail (node != NULL);
1664
1665   clist = GTK_CMCLIST (ctree);
1666   
1667   if (update_focus_row && clist->selection_mode == GTK_SELECTION_MULTIPLE)
1668     {
1669       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1670       
1671       g_list_free (clist->undo_selection);
1672       g_list_free (clist->undo_unselection);
1673       clist->undo_selection = NULL;
1674       clist->undo_unselection = NULL;
1675     }
1676
1677   visible = gtk_cmctree_is_viewable (ctree, node);
1678
1679   /* clist->row_list_end unlinked ? */
1680   if (visible &&
1681       (GTK_CMCTREE_NODE_NEXT (node) == NULL ||
1682        (GTK_CMCTREE_ROW (node)->children &&
1683         gtk_cmctree_is_ancestor (ctree, node,
1684                                GTK_CMCTREE_NODE (clist->row_list_end)))))
1685     clist->row_list_end = (GList *) (GTK_CMCTREE_NODE_PREV (node));
1686
1687   /* update list */
1688   rows = 0;
1689   level = GTK_CMCTREE_ROW (node)->level;
1690   work = GTK_CMCTREE_NODE_NEXT (node);
1691   while (work && GTK_CMCTREE_ROW (work)->level > level)
1692     {
1693       work = GTK_CMCTREE_NODE_NEXT (work);
1694       rows++;
1695     }
1696
1697   if (visible)
1698     {
1699       clist->rows -= (rows + 1);
1700
1701       if (update_focus_row)
1702         {
1703           gint pos;
1704           
1705           pos = g_list_position (clist->row_list, (GList *)node);
1706           if (pos + rows < clist->focus_row)
1707             clist->focus_row -= (rows + 1);
1708           else if (pos <= clist->focus_row)
1709             {
1710               if (!GTK_CMCTREE_ROW (node)->sibling)
1711                 clist->focus_row = MAX (pos - 1, 0);
1712               else
1713                 clist->focus_row = pos;
1714               
1715               clist->focus_row = MIN (clist->focus_row, clist->rows - 1);
1716             }
1717           clist->undo_anchor = clist->focus_row;
1718         }
1719     }
1720
1721   if (work)
1722     {
1723       list = (GList *)GTK_CMCTREE_NODE_PREV (work);
1724       list->next = NULL;
1725       list = (GList *)work;
1726       list->prev = (GList *)GTK_CMCTREE_NODE_PREV (node);
1727     }
1728
1729   if (GTK_CMCTREE_NODE_PREV (node) &&
1730       GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (node)) == node)
1731     {
1732       list = (GList *)GTK_CMCTREE_NODE_PREV (node);
1733       list->next = (GList *)work;
1734     }
1735
1736   /* update tree */
1737   parent = GTK_CMCTREE_ROW (node)->parent;
1738   if (parent)
1739     {
1740       if (GTK_CMCTREE_ROW (parent)->children == node)
1741         {
1742           GTK_CMCTREE_ROW (parent)->children = GTK_CMCTREE_ROW (node)->sibling;
1743           if (!GTK_CMCTREE_ROW (parent)->children)
1744             gtk_cmctree_collapse (ctree, parent);
1745         }
1746       else
1747         {
1748           GtkCMCTreeNode *sibling;
1749
1750           sibling = GTK_CMCTREE_ROW (parent)->children;
1751           while (GTK_CMCTREE_ROW (sibling)->sibling != node)
1752             sibling = GTK_CMCTREE_ROW (sibling)->sibling;
1753           GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
1754         }
1755     }
1756   else
1757     {
1758       if (clist->row_list == (GList *)node)
1759         clist->row_list = (GList *) (GTK_CMCTREE_ROW (node)->sibling);
1760       else
1761         {
1762           GtkCMCTreeNode *sibling;
1763
1764           sibling = GTK_CMCTREE_NODE (clist->row_list);
1765           while (GTK_CMCTREE_ROW (sibling)->sibling != node)
1766             sibling = GTK_CMCTREE_ROW (sibling)->sibling;
1767           GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
1768         }
1769     }
1770 }
1771
1772 static void
1773 real_row_move (GtkCMCList *clist,
1774                gint      source_row,
1775                gint      dest_row)
1776 {
1777   GtkCMCTree *ctree;
1778   GtkCMCTreeNode *node;
1779
1780   cm_return_if_fail (GTK_IS_CMCTREE (clist));
1781
1782   if (GTK_CMCLIST_AUTO_SORT (clist))
1783     return;
1784
1785   if (source_row < 0 || source_row >= clist->rows ||
1786       dest_row   < 0 || dest_row   >= clist->rows ||
1787       source_row == dest_row)
1788     return;
1789
1790   ctree = GTK_CMCTREE (clist);
1791   node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, source_row));
1792
1793   if (source_row < dest_row)
1794     {
1795       GtkCMCTreeNode *work; 
1796
1797       dest_row++;
1798       work = GTK_CMCTREE_ROW (node)->children;
1799
1800       while (work && GTK_CMCTREE_ROW (work)->level > GTK_CMCTREE_ROW (node)->level)
1801         {
1802           work = GTK_CMCTREE_NODE_NEXT (work);
1803           dest_row++;
1804         }
1805
1806       if (dest_row > clist->rows)
1807         dest_row = clist->rows;
1808     }
1809
1810   if (dest_row < clist->rows)
1811     {
1812       GtkCMCTreeNode *sibling;
1813
1814       sibling = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, dest_row));
1815       gtk_cmctree_move (ctree, node, GTK_CMCTREE_ROW (sibling)->parent, sibling);
1816     }
1817   else
1818     gtk_cmctree_move (ctree, node, NULL, NULL);
1819 }
1820
1821 static void
1822 real_tree_move (GtkCMCTree     *ctree,
1823                 GtkCMCTreeNode *node,
1824                 GtkCMCTreeNode *new_parent, 
1825                 GtkCMCTreeNode *new_sibling)
1826 {
1827   GtkCMCList *clist;
1828   GtkCMCTreeNode *work;
1829   gboolean visible = FALSE;
1830
1831   cm_return_if_fail (ctree != NULL);
1832   cm_return_if_fail (node != NULL);
1833   cm_return_if_fail (!new_sibling || 
1834                     GTK_CMCTREE_ROW (new_sibling)->parent == new_parent);
1835
1836   if (new_parent && GTK_CMCTREE_ROW (new_parent)->is_leaf)
1837     return;
1838
1839   /* new_parent != child of child */
1840   for (work = new_parent; work; work = GTK_CMCTREE_ROW (work)->parent)
1841     if (work == node)
1842       return;
1843
1844   clist = GTK_CMCLIST (ctree);
1845
1846   visible = gtk_cmctree_is_viewable (ctree, node);
1847
1848   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
1849     {
1850       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1851       
1852       g_list_free (clist->undo_selection);
1853       g_list_free (clist->undo_unselection);
1854       clist->undo_selection = NULL;
1855       clist->undo_unselection = NULL;
1856     }
1857
1858   if (GTK_CMCLIST_AUTO_SORT (clist))
1859     {
1860       if (new_parent == GTK_CMCTREE_ROW (node)->parent)
1861         return;
1862       
1863       if (new_parent)
1864         new_sibling = GTK_CMCTREE_ROW (new_parent)->children;
1865       else
1866         new_sibling = GTK_CMCTREE_NODE (clist->row_list);
1867
1868       while (new_sibling && clist->compare
1869              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (new_sibling)) > 0)
1870         new_sibling = GTK_CMCTREE_ROW (new_sibling)->sibling;
1871     }
1872
1873   if (new_parent == GTK_CMCTREE_ROW (node)->parent && 
1874       new_sibling == GTK_CMCTREE_ROW (node)->sibling)
1875     return;
1876
1877   gtk_cmclist_freeze (clist);
1878
1879   work = NULL;
1880   if (gtk_cmctree_is_viewable (ctree, node))
1881     work = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
1882       
1883   gtk_cmctree_unlink (ctree, node, FALSE);
1884   gtk_cmctree_link (ctree, node, new_parent, new_sibling, FALSE);
1885   
1886   if (work)
1887     {
1888       while (work &&  !gtk_cmctree_is_viewable (ctree, work))
1889         work = GTK_CMCTREE_ROW (work)->parent;
1890       clist->focus_row = g_list_position (clist->row_list, (GList *)work);
1891       clist->undo_anchor = clist->focus_row;
1892     }
1893
1894   if (clist->column[ctree->tree_column].auto_resize &&
1895       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
1896       (visible || gtk_cmctree_is_viewable (ctree, node)))
1897     gtk_cmclist_set_column_width
1898       (clist, ctree->tree_column,
1899        gtk_cmclist_optimal_column_width (clist, ctree->tree_column));
1900
1901   gtk_cmclist_thaw (clist);
1902 }
1903
1904 static void
1905 change_focus_row_expansion (GtkCMCTree          *ctree,
1906                             GtkCMCTreeExpansionType action)
1907 {
1908   GtkCMCList *clist;
1909   GtkCMCTreeNode *node;
1910
1911   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1912
1913   clist = GTK_CMCLIST (ctree);
1914
1915   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (ctree))) && 
1916       gtk_widget_has_grab (GTK_WIDGET(ctree)))
1917     return;
1918   
1919   if (!(node =
1920         GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row))) ||
1921       GTK_CMCTREE_ROW (node)->is_leaf || !(GTK_CMCTREE_ROW (node)->children))
1922     return;
1923
1924   switch (action)
1925     {
1926     case GTK_CMCTREE_EXPANSION_EXPAND:
1927       gtk_cmctree_expand (ctree, node);
1928       break;
1929     case GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE:
1930       gtk_cmctree_expand_recursive (ctree, node);
1931       break;
1932     case GTK_CMCTREE_EXPANSION_COLLAPSE:
1933       gtk_cmctree_collapse (ctree, node);
1934       break;
1935     case GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE:
1936       gtk_cmctree_collapse_recursive (ctree, node);
1937       break;
1938     case GTK_CMCTREE_EXPANSION_TOGGLE:
1939       gtk_cmctree_toggle_expansion (ctree, node);
1940       break;
1941     case GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE:
1942       gtk_cmctree_toggle_expansion_recursive (ctree, node);
1943       break;
1944     }
1945 }
1946
1947 static void 
1948 real_tree_expand (GtkCMCTree     *ctree,
1949                   GtkCMCTreeNode *node)
1950 {
1951   GtkCMCList *clist;
1952   GtkCMCTreeNode *work;
1953   GtkRequisition requisition;
1954   gboolean visible;
1955
1956   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1957
1958   if (!node || GTK_CMCTREE_ROW (node)->expanded || GTK_CMCTREE_ROW (node)->is_leaf)
1959     return;
1960
1961   clist = GTK_CMCLIST (ctree);
1962   
1963   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1964
1965   GTK_CMCTREE_ROW (node)->expanded = TRUE;
1966
1967   visible = gtk_cmctree_is_viewable (ctree, node);
1968   /* get cell width if tree_column is auto resized */
1969   if (visible && clist->column[ctree->tree_column].auto_resize &&
1970       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1971     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1972       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
1973
1974   /* unref/unset closed pixbuf */
1975   if (GTK_CMCELL_PIXTEXT 
1976       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
1977     {
1978       g_object_unref
1979         (GTK_CMCELL_PIXTEXT
1980          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
1981       
1982       GTK_CMCELL_PIXTEXT
1983         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
1984     }
1985
1986   /* set/ref opened pixbuf */
1987   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
1988     {
1989       GTK_CMCELL_PIXTEXT 
1990         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
1991         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
1992     }
1993
1994
1995   work = GTK_CMCTREE_ROW (node)->children;
1996   if (work)
1997     {
1998       GList *list = (GList *)work;
1999       gint *cell_width = NULL;
2000       gint tmp = 0;
2001       gint row;
2002       gint i;
2003       
2004       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2005         {
2006           cell_width = g_new0 (gint, clist->columns);
2007           if (clist->column[ctree->tree_column].auto_resize)
2008               cell_width[ctree->tree_column] = requisition.width;
2009
2010           while (work)
2011             {
2012               /* search maximum cell widths of auto_resize columns */
2013               for (i = 0; i < clist->columns; i++)
2014                 if (clist->column[i].auto_resize)
2015                   {
2016                     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2017                       (clist, &GTK_CMCTREE_ROW (work)->row, i, &requisition);
2018                     cell_width[i] = MAX (requisition.width, cell_width[i]);
2019                   }
2020
2021               list = (GList *)work;
2022               work = GTK_CMCTREE_NODE_NEXT (work);
2023               tmp++;
2024             }
2025         }
2026       else
2027         while (work)
2028           {
2029             list = (GList *)work;
2030             work = GTK_CMCTREE_NODE_NEXT (work);
2031             tmp++;
2032           }
2033
2034       list->next = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2035
2036       if (GTK_CMCTREE_NODE_NEXT (node))
2037         {
2038           GList *tmp_list;
2039
2040           tmp_list = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2041           tmp_list->prev = list;
2042         }
2043       else
2044         clist->row_list_end = list;
2045
2046       list = (GList *)node;
2047       list->next = (GList *)(GTK_CMCTREE_ROW (node)->children);
2048
2049       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2050         {
2051           /* resize auto_resize columns if needed */
2052           for (i = 0; i < clist->columns; i++)
2053             if (clist->column[i].auto_resize &&
2054                 cell_width[i] > clist->column[i].width)
2055               gtk_cmclist_set_column_width (clist, i, cell_width[i]);
2056           g_free (cell_width);
2057
2058           /* update focus_row position */
2059           row = g_list_position (clist->row_list, (GList *)node);
2060           if (row < clist->focus_row)
2061             clist->focus_row += tmp;
2062
2063           clist->rows += tmp;
2064           CLIST_REFRESH (clist);
2065         }
2066     }
2067   else if (visible && clist->column[ctree->tree_column].auto_resize)
2068     /* resize tree_column if needed */
2069     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2070                         requisition.width);
2071 }
2072
2073 static void 
2074 real_tree_collapse (GtkCMCTree     *ctree,
2075                     GtkCMCTreeNode *node)
2076 {
2077   GtkCMCList *clist;
2078   GtkCMCTreeNode *work;
2079   GtkRequisition requisition;
2080   gboolean visible;
2081   gint level;
2082
2083   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2084
2085   if (!node || !GTK_CMCTREE_ROW (node)->expanded ||
2086       GTK_CMCTREE_ROW (node)->is_leaf)
2087     return;
2088
2089   clist = GTK_CMCLIST (ctree);
2090
2091   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2092   
2093   GTK_CMCTREE_ROW (node)->expanded = FALSE;
2094   level = GTK_CMCTREE_ROW (node)->level;
2095
2096   visible = gtk_cmctree_is_viewable (ctree, node);
2097   /* get cell width if tree_column is auto resized */
2098   if (visible && clist->column[ctree->tree_column].auto_resize &&
2099       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2100     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2101       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
2102
2103   /* unref/unset opened pixbuf */
2104   if (GTK_CMCELL_PIXTEXT 
2105       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
2106     {
2107       g_object_unref
2108         (GTK_CMCELL_PIXTEXT
2109          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
2110       
2111       GTK_CMCELL_PIXTEXT
2112         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
2113     }
2114
2115   /* set/ref closed pixbuf */
2116   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2117     {
2118       GTK_CMCELL_PIXTEXT 
2119         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
2120         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2121     }
2122
2123   work = GTK_CMCTREE_ROW (node)->children;
2124   if (work)
2125     {
2126       gint tmp = 0;
2127       gint row;
2128       GList *list;
2129
2130       while (work && GTK_CMCTREE_ROW (work)->level > level)
2131         {
2132           work = GTK_CMCTREE_NODE_NEXT (work);
2133           tmp++;
2134         }
2135
2136       if (work)
2137         {
2138           list = (GList *)node;
2139           list->next = (GList *)work;
2140           list = (GList *)GTK_CMCTREE_NODE_PREV (work);
2141           list->next = NULL;
2142           list = (GList *)work;
2143           list->prev = (GList *)node;
2144         }
2145       else
2146         {
2147           list = (GList *)node;
2148           list->next = NULL;
2149           clist->row_list_end = (GList *)node;
2150         }
2151
2152       if (visible)
2153         {
2154           /* resize auto_resize columns if needed */
2155           auto_resize_columns (clist);
2156
2157           row = g_list_position (clist->row_list, (GList *)node);
2158           if (row < clist->focus_row)
2159             clist->focus_row -= tmp;
2160           clist->rows -= tmp;
2161           CLIST_REFRESH (clist);
2162         }
2163     }
2164   else if (visible && clist->column[ctree->tree_column].auto_resize &&
2165            !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2166     /* resize tree_column if needed */
2167     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2168                         requisition.width);
2169     
2170 }
2171
2172 static void
2173 column_auto_resize (GtkCMCList    *clist,
2174                     GtkCMCListRow *clist_row,
2175                     gint         column,
2176                     gint         old_width)
2177 {
2178   /* resize column if needed for auto_resize */
2179   GtkRequisition requisition;
2180
2181   if (!clist->column[column].auto_resize ||
2182       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2183     return;
2184
2185   if (clist_row)
2186     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2187                                                    column, &requisition);
2188   else
2189     requisition.width = 0;
2190
2191   if (requisition.width > clist->column[column].width)
2192     gtk_cmclist_set_column_width (clist, column, requisition.width);
2193   else if (requisition.width < old_width &&
2194            old_width == clist->column[column].width)
2195     {
2196       GList *list;
2197       gint new_width;
2198
2199       /* run a "gtk_cmclist_optimal_column_width" but break, if
2200        * the column doesn't shrink */
2201       if (GTK_CMCLIST_SHOW_TITLES (clist) && clist->column[column].button)
2202         {
2203         GtkRequisition req;
2204         gtk_widget_get_requisition (clist->column[column].button, &req);
2205         new_width = (req.width -
2206                      (CELL_SPACING + (2 * COLUMN_INSET)));
2207         }
2208       else
2209         new_width = 0;
2210
2211       for (list = clist->row_list; list; list = list->next)
2212         {
2213           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2214             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
2215           new_width = MAX (new_width, requisition.width);
2216           if (new_width == clist->column[column].width)
2217             break;
2218         }
2219       if (new_width < clist->column[column].width)
2220         gtk_cmclist_set_column_width (clist, column, new_width);
2221     }
2222 }
2223
2224 static void
2225 auto_resize_columns (GtkCMCList *clist)
2226 {
2227   gint i;
2228
2229   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2230     return;
2231
2232   for (i = 0; i < clist->columns; i++)
2233     column_auto_resize (clist, NULL, i, clist->column[i].width);
2234 }
2235
2236 static void
2237 cell_size_request (GtkCMCList       *clist,
2238                    GtkCMCListRow    *clist_row,
2239                    gint            column,
2240                    GtkRequisition *requisition)
2241 {
2242   GtkCMCTree *ctree;
2243   gint width;
2244   gint height;
2245   PangoLayout *layout;
2246   PangoRectangle logical_rect;
2247
2248   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2249   cm_return_if_fail (requisition != NULL);
2250
2251   ctree = GTK_CMCTREE (clist);
2252
2253   layout = create_cell_layout (clist, clist_row, column);
2254   if (layout)
2255     {
2256       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2257
2258       requisition->width = logical_rect.width;
2259       requisition->height = logical_rect.height;
2260       
2261       g_object_unref (G_OBJECT (layout));
2262     }
2263   else
2264     {
2265       requisition->width  = 0;
2266       requisition->height = 0;
2267     }
2268
2269   switch (clist_row->cell[column].type)
2270     {
2271     case GTK_CMCELL_PIXTEXT:
2272       if (GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf)
2273         {
2274           width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2275           height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2276           width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2277         }
2278       else
2279         width = height = 0;
2280           
2281       requisition->width += width;
2282       requisition->height = MAX (requisition->height, height);
2283       
2284       if (column == ctree->tree_column)
2285         {
2286           requisition->width += (ctree->tree_spacing + ctree->tree_indent *
2287                                  (((GtkCMCTreeRow *) clist_row)->level - 1));
2288           switch (ctree->expander_style)
2289             {
2290             case GTK_CMCTREE_EXPANDER_NONE:
2291               break;
2292             case GTK_CMCTREE_EXPANDER_TRIANGLE:
2293               requisition->width += PM_SIZE + 3;
2294               break;
2295             }
2296         }
2297       break;
2298     case GTK_CMCELL_PIXBUF:
2299       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2300       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2301       requisition->width += width;
2302       requisition->height = MAX (requisition->height, height);
2303       break;
2304     default:
2305       break;
2306     }
2307
2308   requisition->width  += clist_row->cell[column].horizontal;
2309   requisition->height += clist_row->cell[column].vertical;
2310 }
2311
2312 static void
2313 set_cell_contents (GtkCMCList    *clist,
2314                    GtkCMCListRow *clist_row,
2315                    gint         column,
2316                    GtkCMCellType  type,
2317                    const gchar *text,
2318                    guint8       spacing,
2319                    GdkPixbuf   *pixbuf)
2320 {
2321   gboolean visible = FALSE;
2322   GtkCMCTree *ctree;
2323   GtkRequisition requisition;
2324   gchar *old_text = NULL;
2325   GdkPixbuf *old_pixbuf = NULL;
2326
2327   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2328   cm_return_if_fail (clist_row != NULL);
2329
2330   ctree = GTK_CMCTREE (clist);
2331
2332   if (clist->column[column].auto_resize &&
2333       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2334     {
2335       GtkCMCTreeNode *parent;
2336
2337       parent = ((GtkCMCTreeRow *)clist_row)->parent;
2338       if ((parent && GTK_CMCTREE_ROW (parent)->expanded &&
2339                       gtk_cmctree_is_viewable (ctree, parent)))
2340         {
2341           visible = TRUE;
2342           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2343                                                          column, &requisition);
2344         }
2345     }
2346
2347   switch (clist_row->cell[column].type)
2348     {
2349     case GTK_CMCELL_EMPTY:
2350       break;
2351     case GTK_CMCELL_TEXT:
2352       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2353       break;
2354     case GTK_CMCELL_PIXBUF:
2355       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2356       break;
2357     case GTK_CMCELL_PIXTEXT:
2358       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2359       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2360       break;
2361     case GTK_CMCELL_WIDGET:
2362       /* unimplemented */
2363       break;
2364       
2365     default:
2366       break;
2367     }
2368
2369   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2370   if (column == ctree->tree_column && type != GTK_CMCELL_EMPTY)
2371     type = GTK_CMCELL_PIXTEXT;
2372
2373   /* Note that pixbuf and mask were already ref'ed by the caller
2374    */
2375   switch (type)
2376     {
2377     case GTK_CMCELL_TEXT:
2378       if (text)
2379         {
2380           clist_row->cell[column].type = GTK_CMCELL_TEXT;
2381           GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2382         }
2383       break;
2384     case GTK_CMCELL_PIXBUF:
2385       if (pixbuf)
2386         {
2387           clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2388           GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2389         }
2390       break;
2391     case GTK_CMCELL_PIXTEXT:
2392       if (column == ctree->tree_column)
2393         {
2394           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2395           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2396           if (text)
2397             GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2398           else
2399             GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = NULL;
2400           if (pixbuf)
2401             {
2402               GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2403             }
2404           else
2405             {
2406               GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = NULL;
2407             }
2408         }
2409       else if (text && pixbuf)
2410         {
2411           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2412           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2413           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2414           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2415         }
2416       break;
2417     default:
2418       break;
2419     }
2420   
2421   if (visible && clist->column[column].auto_resize &&
2422       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2423     column_auto_resize (clist, clist_row, column, requisition.width);
2424
2425   g_free (old_text);
2426   if (old_pixbuf)
2427     g_object_unref (old_pixbuf);
2428 }
2429
2430 static void 
2431 set_node_info (GtkCMCTree     *ctree,
2432                GtkCMCTreeNode *node,
2433                const gchar  *text,
2434                guint8        spacing,
2435                GdkPixbuf    *pixbuf_closed,
2436                GdkPixbuf    *pixbuf_opened,
2437                gboolean      is_leaf,
2438                gboolean      expanded)
2439 {
2440   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
2441     {
2442       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
2443     }
2444   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2445     {
2446       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2447     }
2448
2449   GTK_CMCTREE_ROW (node)->pixbuf_opened = NULL;
2450   GTK_CMCTREE_ROW (node)->pixbuf_closed = NULL;
2451
2452   if (pixbuf_closed)
2453     {
2454       GTK_CMCTREE_ROW (node)->pixbuf_closed = g_object_ref (pixbuf_closed);
2455     }
2456   if (pixbuf_opened)
2457     {
2458       GTK_CMCTREE_ROW (node)->pixbuf_opened = g_object_ref (pixbuf_opened);
2459     }
2460
2461   GTK_CMCTREE_ROW (node)->is_leaf  = is_leaf;
2462   GTK_CMCTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2463
2464   if (GTK_CMCTREE_ROW (node)->expanded)
2465     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2466                                 text, spacing, pixbuf_opened);
2467   else 
2468     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2469                                 text, spacing, pixbuf_closed);
2470 }
2471
2472 static void
2473 tree_delete (GtkCMCTree     *ctree, 
2474              GtkCMCTreeNode *node, 
2475              gpointer      data)
2476 {
2477   tree_unselect (ctree,  node, NULL);
2478   row_delete (ctree, GTK_CMCTREE_ROW (node));
2479   g_list_free_1 ((GList *)node);
2480 }
2481
2482 static void
2483 tree_delete_row (GtkCMCTree     *ctree, 
2484                  GtkCMCTreeNode *node, 
2485                  gpointer      data)
2486 {
2487   row_delete (ctree, GTK_CMCTREE_ROW (node));
2488   g_list_free_1 ((GList *)node);
2489 }
2490
2491 static void
2492 tree_update_level (GtkCMCTree     *ctree, 
2493                    GtkCMCTreeNode *node, 
2494                    gpointer      data)
2495 {
2496   if (!node)
2497     return;
2498
2499   if (GTK_CMCTREE_ROW (node)->parent)
2500       GTK_CMCTREE_ROW (node)->level = 
2501         GTK_CMCTREE_ROW (GTK_CMCTREE_ROW (node)->parent)->level + 1;
2502   else
2503       GTK_CMCTREE_ROW (node)->level = 1;
2504 }
2505
2506 static void
2507 tree_select (GtkCMCTree     *ctree, 
2508              GtkCMCTreeNode *node, 
2509              gpointer      data)
2510 {
2511   if (node && GTK_CMCTREE_ROW (node)->row.state != GTK_STATE_SELECTED &&
2512       GTK_CMCTREE_ROW (node)->row.selectable)
2513     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_SELECT_ROW], 0,
2514                      node, -1);
2515 }
2516
2517 static void
2518 tree_unselect (GtkCMCTree     *ctree, 
2519                GtkCMCTreeNode *node, 
2520                gpointer      data)
2521 {
2522   if (node && GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED)
2523     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_UNSELECT_ROW], 0,
2524                      node, -1);
2525 }
2526
2527 static void
2528 tree_expand (GtkCMCTree     *ctree, 
2529              GtkCMCTreeNode *node, 
2530              gpointer      data)
2531 {
2532   if (node && !GTK_CMCTREE_ROW (node)->expanded)
2533     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0,node);
2534 }
2535
2536 static void
2537 tree_collapse (GtkCMCTree     *ctree, 
2538                GtkCMCTreeNode *node, 
2539                gpointer      data)
2540 {
2541   if (node && GTK_CMCTREE_ROW (node)->expanded)
2542     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0,node);
2543 }
2544
2545 static void
2546 tree_collapse_to_depth (GtkCMCTree     *ctree, 
2547                         GtkCMCTreeNode *node, 
2548                         gint          depth)
2549 {
2550   if (node && GTK_CMCTREE_ROW (node)->level == depth)
2551     gtk_cmctree_collapse_recursive (ctree, node);
2552 }
2553
2554 static void
2555 tree_toggle_expansion (GtkCMCTree     *ctree,
2556                        GtkCMCTreeNode *node,
2557                        gpointer      data)
2558 {
2559   if (!node)
2560     return;
2561
2562   if (GTK_CMCTREE_ROW (node)->expanded)
2563     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0,node);
2564   else
2565     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0,node);
2566 }
2567
2568 static GtkCMCTreeRow *
2569 row_new (GtkCMCTree *ctree)
2570 {
2571   GtkCMCList *clist;
2572   GtkCMCTreeRow *ctree_row;
2573   int i;
2574
2575   clist = GTK_CMCLIST (ctree);
2576 #if GLIB_CHECK_VERSION(2,10,0)
2577   ctree_row = g_slice_new (GtkCMCTreeRow);
2578   ctree_row->row.cell = g_slice_alloc (sizeof (GtkCMCell) * clist->columns);
2579 #else
2580   ctree_row = g_chunk_new (GtkCMCTreeRow, (GMemChunk *)clist->row_mem_chunk);
2581   ctree_row->row.cell = g_chunk_new (GtkCMCell, (GMemChunk *)clist->cell_mem_chunk);
2582 #endif
2583
2584   for (i = 0; i < clist->columns; i++)
2585     {
2586       ctree_row->row.cell[i].type = GTK_CMCELL_EMPTY;
2587       ctree_row->row.cell[i].vertical = 0;
2588       ctree_row->row.cell[i].horizontal = 0;
2589       ctree_row->row.cell[i].style = NULL;
2590     }
2591   GTK_CMCELL_PIXTEXT (ctree_row->row.cell[ctree->tree_column])->text = NULL;
2592
2593   ctree_row->row.fg_set     = FALSE;
2594   ctree_row->row.bg_set     = FALSE;
2595   ctree_row->row.style      = NULL;
2596   ctree_row->row.selectable = TRUE;
2597   ctree_row->row.state      = GTK_STATE_NORMAL;
2598   ctree_row->row.data       = NULL;
2599   ctree_row->row.destroy    = NULL;
2600
2601   ctree_row->level         = 0;
2602   ctree_row->expanded      = FALSE;
2603   ctree_row->parent        = NULL;
2604   ctree_row->sibling       = NULL;
2605   ctree_row->children      = NULL;
2606   ctree_row->pixbuf_closed = NULL;
2607   ctree_row->pixbuf_opened = NULL;
2608   
2609   return ctree_row;
2610 }
2611
2612 static void
2613 row_delete (GtkCMCTree    *ctree,
2614             GtkCMCTreeRow *ctree_row)
2615 {
2616   GtkCMCList *clist;
2617   gint i;
2618
2619   clist = GTK_CMCLIST (ctree);
2620
2621   for (i = 0; i < clist->columns; i++)
2622     {
2623       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2624         (clist, &(ctree_row->row), i, GTK_CMCELL_EMPTY, NULL, 0, NULL);
2625       if (ctree_row->row.cell[i].style)
2626         {
2627           if (gtk_widget_get_realized (GTK_WIDGET(ctree)))
2628             gtk_style_detach (ctree_row->row.cell[i].style);
2629           g_object_unref (ctree_row->row.cell[i].style);
2630         }
2631     }
2632
2633   if (ctree_row->row.style)
2634     {
2635       if (gtk_widget_get_realized (GTK_WIDGET(ctree)))
2636         gtk_style_detach (ctree_row->row.style);
2637       g_object_unref (ctree_row->row.style);
2638     }
2639
2640   if (ctree_row->pixbuf_closed)
2641     {
2642       g_object_unref (ctree_row->pixbuf_closed);
2643     }
2644
2645   if (ctree_row->pixbuf_opened)
2646     {
2647       g_object_unref (ctree_row->pixbuf_opened);
2648     }
2649
2650   if (ctree_row->row.destroy)
2651     {
2652       GDestroyNotify dnotify = ctree_row->row.destroy;
2653       gpointer ddata = ctree_row->row.data;
2654
2655       ctree_row->row.destroy = NULL;
2656       ctree_row->row.data = NULL;
2657
2658       dnotify (ddata);
2659     }
2660
2661 #if GLIB_CHECK_VERSION(2,10,0)  
2662   g_slice_free1 (sizeof (GtkCMCell) * clist->columns, ctree_row->row.cell);
2663   g_slice_free (GtkCMCTreeRow, ctree_row);
2664 #else
2665   g_mem_chunk_free ((GMemChunk *)clist->cell_mem_chunk, ctree_row->row.cell);
2666   g_mem_chunk_free ((GMemChunk *)clist->row_mem_chunk, ctree_row);
2667 #endif
2668 }
2669
2670 static void
2671 real_select_row (GtkCMCList *clist,
2672                  gint      row,
2673                  gint      column,
2674                  GdkEvent *event)
2675 {
2676   GList *node;
2677
2678   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2679   
2680   if ((node = g_list_nth (clist->row_list, row)) &&
2681       GTK_CMCTREE_ROW (node)->row.selectable)
2682     g_signal_emit (G_OBJECT (clist), ctree_signals[TREE_SELECT_ROW],0,
2683                      node, column);
2684 }
2685
2686 static void
2687 real_unselect_row (GtkCMCList *clist,
2688                    gint      row,
2689                    gint      column,
2690                    GdkEvent *event)
2691 {
2692   GList *node;
2693
2694   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2695
2696   if ((node = g_list_nth (clist->row_list, row)))
2697     g_signal_emit (G_OBJECT (clist), ctree_signals[TREE_UNSELECT_ROW],0,
2698                      node, column);
2699 }
2700
2701 static void
2702 tree_draw_node (GtkCMCTree     *ctree, 
2703                GtkCMCTreeNode *node)
2704 {
2705   GtkCMCList *clist;
2706   
2707   clist = GTK_CMCLIST (ctree);
2708
2709   if (CLIST_UNFROZEN (clist) && gtk_cmctree_is_viewable (ctree, node))
2710     {
2711       GtkCMCTreeNode *work;
2712       gint num = 0;
2713
2714       work = GTK_CMCTREE_NODE (clist->row_list);
2715       while (work && work != node)
2716         {
2717           work = GTK_CMCTREE_NODE_NEXT (work);
2718           num++;
2719         }
2720       if (work && gtk_cmclist_row_is_visible (clist, num) != GTK_VISIBILITY_NONE)
2721         GTK_CMCLIST_GET_CLASS(ctree)->draw_row
2722           (clist, NULL, num, GTK_CMCLIST_ROW ((GList *) node));
2723     }
2724 }
2725
2726 static void
2727 real_tree_select (GtkCMCTree     *ctree,
2728                   GtkCMCTreeNode *node,
2729                   gint          column)
2730 {
2731   GtkCMCList *clist;
2732   GList *list;
2733   GtkCMCTreeNode *sel_row;
2734   gboolean node_selected;
2735
2736   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2737
2738   if (!node || GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED ||
2739       !GTK_CMCTREE_ROW (node)->row.selectable)
2740     return;
2741
2742   clist = GTK_CMCLIST (ctree);
2743
2744   switch (clist->selection_mode)
2745     {
2746     case GTK_SELECTION_SINGLE:
2747     case GTK_SELECTION_BROWSE:
2748
2749       node_selected = FALSE;
2750       list = clist->selection;
2751
2752       while (list)
2753         {
2754           sel_row = list->data;
2755           list = list->next;
2756           
2757           if (node == sel_row)
2758             node_selected = TRUE;
2759           else
2760             g_signal_emit (G_OBJECT (ctree),
2761                              ctree_signals[TREE_UNSELECT_ROW], 0, sel_row, column);
2762         }
2763
2764       if (node_selected)
2765         return;
2766
2767     default:
2768       break;
2769     }
2770
2771   GTK_CMCTREE_ROW (node)->row.state = GTK_STATE_SELECTED;
2772
2773   if (!clist->selection)
2774     {
2775       clist->selection = g_list_append (clist->selection, node);
2776       clist->selection_end = clist->selection;
2777     }
2778   else
2779     clist->selection_end = g_list_append (clist->selection_end, node)->next;
2780
2781   tree_draw_node (ctree, node);
2782 }
2783
2784 static void
2785 real_tree_unselect (GtkCMCTree     *ctree,
2786                     GtkCMCTreeNode *node,
2787                     gint          column)
2788 {
2789   GtkCMCList *clist;
2790
2791   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2792
2793   if (!node || GTK_CMCTREE_ROW (node)->row.state != GTK_STATE_SELECTED)
2794     return;
2795
2796   clist = GTK_CMCLIST (ctree);
2797
2798   if (clist->selection_end && clist->selection_end->data == node)
2799     clist->selection_end = clist->selection_end->prev;
2800
2801   clist->selection = g_list_remove (clist->selection, node);
2802   
2803   GTK_CMCTREE_ROW (node)->row.state = GTK_STATE_NORMAL;
2804
2805   tree_draw_node (ctree, node);
2806 }
2807
2808 static void
2809 select_row_recursive (GtkCMCTree     *ctree, 
2810                       GtkCMCTreeNode *node, 
2811                       gpointer      data)
2812 {
2813   if (!node || GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED ||
2814       !GTK_CMCTREE_ROW (node)->row.selectable)
2815     return;
2816
2817   GTK_CMCLIST (ctree)->undo_unselection = 
2818     g_list_prepend (GTK_CMCLIST (ctree)->undo_unselection, node);
2819   gtk_cmctree_select (ctree, node);
2820 }
2821
2822 static void
2823 real_select_all (GtkCMCList *clist)
2824 {
2825   GtkCMCTree *ctree;
2826   GtkCMCTreeNode *node;
2827   
2828   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2829
2830   ctree = GTK_CMCTREE (clist);
2831
2832   switch (clist->selection_mode)
2833     {
2834     case GTK_SELECTION_SINGLE:
2835     case GTK_SELECTION_BROWSE:
2836       return;
2837
2838     case GTK_SELECTION_MULTIPLE:
2839
2840       gtk_cmclist_freeze (clist);
2841
2842       g_list_free (clist->undo_selection);
2843       g_list_free (clist->undo_unselection);
2844       clist->undo_selection = NULL;
2845       clist->undo_unselection = NULL;
2846           
2847       clist->anchor_state = GTK_STATE_SELECTED;
2848       clist->anchor = -1;
2849       clist->drag_pos = -1;
2850       clist->undo_anchor = clist->focus_row;
2851
2852       for (node = GTK_CMCTREE_NODE (clist->row_list); node;
2853            node = GTK_CMCTREE_NODE_NEXT (node))
2854         gtk_cmctree_pre_recursive (ctree, node, select_row_recursive, NULL);
2855
2856       gtk_cmclist_thaw (clist);
2857       break;
2858
2859     default:
2860       /* do nothing */
2861       break;
2862     }
2863 }
2864
2865 static void
2866 real_unselect_all (GtkCMCList *clist)
2867 {
2868   GtkCMCTree *ctree;
2869   GtkCMCTreeNode *node;
2870   GList *list;
2871  
2872   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2873   
2874   ctree = GTK_CMCTREE (clist);
2875
2876   switch (clist->selection_mode)
2877     {
2878     case GTK_SELECTION_BROWSE:
2879       if (clist->focus_row >= 0)
2880         {
2881           gtk_cmctree_select
2882             (ctree,
2883              GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row)));
2884           return;
2885         }
2886       break;
2887
2888     case GTK_SELECTION_MULTIPLE:
2889       g_list_free (clist->undo_selection);
2890       g_list_free (clist->undo_unselection);
2891       clist->undo_selection = NULL;
2892       clist->undo_unselection = NULL;
2893
2894       clist->anchor = -1;
2895       clist->drag_pos = -1;
2896       clist->undo_anchor = clist->focus_row;
2897       break;
2898
2899     default:
2900       break;
2901     }
2902
2903   list = clist->selection;
2904
2905   while (list)
2906     {
2907       node = list->data;
2908       list = list->next;
2909       gtk_cmctree_unselect (ctree, node);
2910     }
2911 }
2912
2913 static gboolean
2914 ctree_is_hot_spot (GtkCMCTree     *ctree, 
2915                    GtkCMCTreeNode *node,
2916                    gint          row, 
2917                    gint          x, 
2918                    gint          y)
2919 {
2920   GtkCMCTreeRow *tree_row;
2921   GtkCMCList *clist;
2922   gint xl;
2923   gint yu;
2924   gint hotspot_size;
2925
2926   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
2927   cm_return_val_if_fail (node != NULL, FALSE);
2928
2929   clist = GTK_CMCLIST (ctree);
2930
2931   if (!clist->column[ctree->tree_column].visible ||
2932       ctree->expander_style == GTK_CMCTREE_EXPANDER_NONE)
2933     return FALSE;
2934
2935   tree_row = GTK_CMCTREE_ROW (node);
2936
2937   hotspot_size = clist->row_height-2;
2938   if (hotspot_size > clist->column[ctree->tree_column].area.width - 2)
2939         hotspot_size = clist->column[ctree->tree_column].area.width - 2;
2940
2941   yu = (ROW_TOP_YPIXEL (clist, row) + (clist->row_height - hotspot_size) / 2 -
2942         (clist->row_height - 1) % 2);
2943
2944   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT)
2945     xl = (clist->column[ctree->tree_column].area.x + 
2946           clist->column[ctree->tree_column].area.width - 1 + clist->hoffset -
2947           (tree_row->level - 1) * ctree->tree_indent - hotspot_size);
2948   else
2949     xl = (clist->column[ctree->tree_column].area.x + clist->hoffset +
2950           (tree_row->level - 1) * ctree->tree_indent);
2951
2952   return (x >= xl && x <= xl + hotspot_size && y >= yu && y <= yu + hotspot_size);
2953 }
2954
2955 /***********************************************************
2956  ***********************************************************
2957  ***                  Public interface                   ***
2958  ***********************************************************
2959  ***********************************************************/
2960
2961
2962 /***********************************************************
2963  *           Creation, insertion, deletion                 *
2964  ***********************************************************/
2965
2966 static GObject*
2967 gtk_cmctree_constructor (GType                  type,
2968                        guint                  n_construct_properties,
2969                        GObjectConstructParam *construct_properties)
2970 {
2971   GObject *object = G_OBJECT_CLASS (parent_class)->constructor (type,
2972                                                                 n_construct_properties,
2973                                                                 construct_properties);
2974
2975   return object;
2976 }
2977
2978 GtkWidget*
2979 gtk_cmctree_new_with_titles (gint         columns, 
2980                            gint         tree_column,
2981                            gchar       *titles[])
2982 {
2983   GtkWidget *widget;
2984
2985   cm_return_val_if_fail (columns > 0, NULL);
2986   cm_return_val_if_fail (tree_column >= 0 && tree_column < columns, NULL);
2987
2988   widget = gtk_widget_new (GTK_TYPE_CMCTREE,
2989                            "n_columns", columns,
2990                            "tree_column", tree_column,
2991                            NULL);
2992   if (titles)
2993     {
2994       GtkCMCList *clist = GTK_CMCLIST (widget);
2995       guint i;
2996
2997       for (i = 0; i < columns; i++)
2998         gtk_cmclist_set_column_title (clist, i, titles[i]);
2999       gtk_cmclist_column_titles_show (clist);
3000     }
3001
3002   return widget;
3003 }
3004
3005 GtkWidget *
3006 gtk_cmctree_new (gint columns, 
3007                gint tree_column)
3008 {
3009   return gtk_cmctree_new_with_titles (columns, tree_column, NULL);
3010 }
3011
3012 static gint
3013 real_insert_row (GtkCMCList *clist,
3014                  gint      row,
3015                  gchar    *text[])
3016 {
3017   GtkCMCTreeNode *parent = NULL;
3018   GtkCMCTreeNode *sibling;
3019   GtkCMCTreeNode *node;
3020
3021   cm_return_val_if_fail (GTK_IS_CMCTREE (clist), -1);
3022
3023   sibling = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
3024   if (sibling)
3025     parent = GTK_CMCTREE_ROW (sibling)->parent;
3026
3027   node = gtk_cmctree_insert_node (GTK_CMCTREE (clist), parent, sibling, text, 5,
3028                                 NULL, NULL, TRUE, FALSE);
3029
3030   if (GTK_CMCLIST_AUTO_SORT (clist) || !sibling)
3031     return g_list_position (clist->row_list, (GList *) node);
3032   
3033   return row;
3034 }
3035
3036 GtkCMCTreeNode * 
3037 gtk_cmctree_insert_node (GtkCMCTree     *ctree,
3038                        GtkCMCTreeNode *parent, 
3039                        GtkCMCTreeNode *sibling,
3040                        gchar        *text[],
3041                        guint8        spacing,
3042                        GdkPixbuf    *pixbuf_closed,
3043                        GdkPixbuf    *pixbuf_opened,
3044                        gboolean      is_leaf,
3045                        gboolean      expanded)
3046 {
3047   GtkCMCList *clist;
3048   GtkCMCTreeRow *new_row;
3049   GtkCMCTreeNode *node;
3050   GList *list;
3051   gint i;
3052
3053   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3054   if (sibling)
3055     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3056
3057   if (parent && GTK_CMCTREE_ROW (parent)->is_leaf)
3058     return NULL;
3059
3060   clist = GTK_CMCLIST (ctree);
3061
3062   /* create the row */
3063   new_row = row_new (ctree);
3064   list = g_list_alloc ();
3065   list->data = new_row;
3066   node = GTK_CMCTREE_NODE (list);
3067
3068   if (text)
3069     for (i = 0; i < clist->columns; i++)
3070       if (text[i] && i != ctree->tree_column)
3071         GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
3072           (clist, &(new_row->row), i, GTK_CMCELL_TEXT, text[i], 0, NULL);
3073
3074   set_node_info (ctree, node, text ?
3075                  text[ctree->tree_column] : NULL, spacing, pixbuf_closed,
3076                  pixbuf_opened, is_leaf, expanded);
3077
3078   /* sorted insertion */
3079   if (GTK_CMCLIST_AUTO_SORT (clist))
3080     {
3081       if (parent)
3082         sibling = GTK_CMCTREE_ROW (parent)->children;
3083       else
3084         sibling = GTK_CMCTREE_NODE (clist->row_list);
3085
3086       while (sibling && clist->compare
3087              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (sibling)) > 0)
3088         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3089     }
3090
3091   gtk_cmctree_link (ctree, node, parent, sibling, TRUE);
3092
3093   if (text && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
3094       gtk_cmctree_is_viewable (ctree, node))
3095     {
3096       for (i = 0; i < clist->columns; i++)
3097         if (clist->column[i].auto_resize)
3098           column_auto_resize (clist, &(new_row->row), i, 0);
3099     }
3100
3101   if (clist->rows == 1)
3102     {
3103       clist->focus_row = 0;
3104       if (clist->selection_mode == GTK_SELECTION_BROWSE)
3105         gtk_cmctree_select (ctree, node);
3106     }
3107
3108
3109   CLIST_REFRESH (clist);
3110
3111   return node;
3112 }
3113
3114 GtkCMCTreeNode *
3115 gtk_cmctree_insert_gnode (GtkCMCTree          *ctree,
3116                         GtkCMCTreeNode      *parent,
3117                         GtkCMCTreeNode      *sibling,
3118                         GNode             *gnode,
3119                         GtkCMCTreeGNodeFunc  func,
3120                         gpointer           data)
3121 {
3122   GtkCMCList *clist;
3123   GtkCMCTreeNode *cnode = NULL;
3124   GtkCMCTreeNode *child = NULL;
3125   GtkCMCTreeNode *new_child;
3126   GList *list;
3127   GNode *work;
3128   guint depth = 1;
3129
3130   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3131   cm_return_val_if_fail (gnode != NULL, NULL);
3132   cm_return_val_if_fail (func != NULL, NULL);
3133   if (sibling)
3134     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3135   
3136   clist = GTK_CMCLIST (ctree);
3137
3138   if (parent)
3139     depth = GTK_CMCTREE_ROW (parent)->level + 1;
3140
3141   list = g_list_alloc ();
3142   list->data = row_new (ctree);
3143   cnode = GTK_CMCTREE_NODE (list);
3144
3145   gtk_cmclist_freeze (clist);
3146
3147   set_node_info (ctree, cnode, "", 0, NULL, NULL, TRUE, FALSE);
3148
3149   if (!func (ctree, depth, gnode, cnode, data))
3150     {
3151       tree_delete_row (ctree, cnode, NULL);
3152       gtk_cmclist_thaw (clist);
3153       return NULL;
3154     }
3155
3156   if (GTK_CMCLIST_AUTO_SORT (clist))
3157     {
3158       if (parent)
3159         sibling = GTK_CMCTREE_ROW (parent)->children;
3160       else
3161         sibling = GTK_CMCTREE_NODE (clist->row_list);
3162
3163       while (sibling && clist->compare
3164              (clist, GTK_CMCTREE_ROW (cnode), GTK_CMCTREE_ROW (sibling)) > 0)
3165         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3166     }
3167
3168   gtk_cmctree_link (ctree, cnode, parent, sibling, TRUE);
3169
3170   for (work = g_node_last_child (gnode); work; work = work->prev)
3171     {
3172       new_child = gtk_cmctree_insert_gnode (ctree, cnode, child,
3173                                           work, func, data);
3174       if (new_child)
3175         child = new_child;
3176     }   
3177   
3178   gtk_cmclist_thaw (clist);
3179
3180   return cnode;
3181 }
3182
3183 GNode *
3184 gtk_cmctree_export_to_gnode (GtkCMCTree          *ctree,
3185                            GNode             *parent,
3186                            GNode             *sibling,
3187                            GtkCMCTreeNode      *node,
3188                            GtkCMCTreeGNodeFunc  func,
3189                            gpointer           data)
3190 {
3191   GtkCMCTreeNode *work;
3192   GNode *gnode;
3193   gint depth;
3194
3195   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3196   cm_return_val_if_fail (node != NULL, NULL);
3197   cm_return_val_if_fail (func != NULL, NULL);
3198   if (sibling)
3199     {
3200       cm_return_val_if_fail (parent != NULL, NULL);
3201       cm_return_val_if_fail (sibling->parent == parent, NULL);
3202     }
3203
3204   gnode = g_node_new (NULL);
3205   depth = g_node_depth (parent) + 1;
3206   
3207   if (!func (ctree, depth, gnode, node, data))
3208     {
3209       g_node_destroy (gnode);
3210       return NULL;
3211     }
3212
3213   if (parent)
3214     g_node_insert_before (parent, sibling, gnode);
3215
3216   if (!GTK_CMCTREE_ROW (node)->is_leaf)
3217     {
3218       GNode *new_sibling = NULL;
3219
3220       for (work = GTK_CMCTREE_ROW (node)->children; work;
3221            work = GTK_CMCTREE_ROW (work)->sibling)
3222         new_sibling = gtk_cmctree_export_to_gnode (ctree, gnode, new_sibling,
3223                                                  work, func, data);
3224
3225       g_node_reverse_children (gnode);
3226     }
3227
3228   return gnode;
3229 }
3230   
3231 static void
3232 real_remove_row (GtkCMCList *clist,
3233                  gint      row)
3234 {
3235   GtkCMCTreeNode *node;
3236
3237   cm_return_if_fail (GTK_IS_CMCTREE (clist));
3238
3239   node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
3240
3241   if (node)
3242     gtk_cmctree_remove_node (GTK_CMCTREE (clist), node);
3243 }
3244
3245 void
3246 gtk_cmctree_remove_node (GtkCMCTree     *ctree, 
3247                        GtkCMCTreeNode *node)
3248 {
3249   GtkCMCList *clist;
3250
3251   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3252
3253   clist = GTK_CMCLIST (ctree);
3254
3255   gtk_cmclist_freeze (clist);
3256
3257   if (node)
3258     {
3259       gtk_cmctree_unlink (ctree, node, TRUE);
3260       gtk_cmctree_post_recursive (ctree, node, GTK_CMCTREE_FUNC (tree_delete),
3261                                 NULL);
3262       if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
3263           clist->focus_row >= 0)
3264         gtk_cmclist_select_row (clist, clist->focus_row, -1);
3265
3266       auto_resize_columns (clist);
3267     }
3268   else
3269     gtk_cmclist_clear (clist);
3270
3271   gtk_cmclist_thaw (clist);
3272 }
3273
3274 static void
3275 real_clear (GtkCMCList *clist)
3276 {
3277   GtkCMCTree *ctree;
3278   GtkCMCTreeNode *work;
3279   GtkCMCTreeNode *ptr;
3280
3281   cm_return_if_fail (GTK_IS_CMCTREE (clist));
3282
3283   ctree = GTK_CMCTREE (clist);
3284
3285   /* remove all rows */
3286   work = GTK_CMCTREE_NODE (clist->row_list);
3287   clist->row_list = NULL;
3288   clist->row_list_end = NULL;
3289
3290   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3291   while (work)
3292     {
3293       ptr = work;
3294       work = GTK_CMCTREE_ROW (work)->sibling;
3295       gtk_cmctree_post_recursive (ctree, ptr, GTK_CMCTREE_FUNC (tree_delete_row), 
3296                                 NULL);
3297     }
3298   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3299
3300   parent_class->clear (clist);
3301 }
3302
3303
3304 /***********************************************************
3305  *  Generic recursive functions, querying / finding tree   *
3306  *  information                                            *
3307  ***********************************************************/
3308
3309
3310 void
3311 gtk_cmctree_post_recursive (GtkCMCTree     *ctree, 
3312                           GtkCMCTreeNode *node,
3313                           GtkCMCTreeFunc  func,
3314                           gpointer      data)
3315 {
3316   GtkCMCTreeNode *work;
3317   GtkCMCTreeNode *tmp;
3318
3319   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3320   cm_return_if_fail (func != NULL);
3321
3322   if (node)
3323     work = GTK_CMCTREE_ROW (node)->children;
3324   else
3325     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3326
3327   while (work)
3328     {
3329       tmp = GTK_CMCTREE_ROW (work)->sibling;
3330       gtk_cmctree_post_recursive (ctree, work, func, data);
3331       work = tmp;
3332     }
3333
3334   if (node)
3335     func (ctree, node, data);
3336 }
3337
3338 void
3339 gtk_cmctree_post_recursive_to_depth (GtkCMCTree     *ctree, 
3340                                    GtkCMCTreeNode *node,
3341                                    gint          depth,
3342                                    GtkCMCTreeFunc  func,
3343                                    gpointer      data)
3344 {
3345   GtkCMCTreeNode *work;
3346   GtkCMCTreeNode *tmp;
3347
3348   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3349   cm_return_if_fail (func != NULL);
3350
3351   if (depth < 0)
3352     {
3353       gtk_cmctree_post_recursive (ctree, node, func, data);
3354       return;
3355     }
3356
3357   if (node)
3358     work = GTK_CMCTREE_ROW (node)->children;
3359   else
3360     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3361
3362   if (work && GTK_CMCTREE_ROW (work)->level <= depth)
3363     {
3364       while (work)
3365         {
3366           tmp = GTK_CMCTREE_ROW (work)->sibling;
3367           gtk_cmctree_post_recursive_to_depth (ctree, work, depth, func, data);
3368           work = tmp;
3369         }
3370     }
3371
3372   if (node && GTK_CMCTREE_ROW (node)->level <= depth)
3373     func (ctree, node, data);
3374 }
3375
3376 void
3377 gtk_cmctree_pre_recursive (GtkCMCTree     *ctree, 
3378                          GtkCMCTreeNode *node,
3379                          GtkCMCTreeFunc  func,
3380                          gpointer      data)
3381 {
3382   GtkCMCTreeNode *work;
3383   GtkCMCTreeNode *tmp;
3384
3385   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3386   cm_return_if_fail (func != NULL);
3387
3388   if (node)
3389     {
3390       work = GTK_CMCTREE_ROW (node)->children;
3391       func (ctree, node, data);
3392     }
3393   else
3394     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3395
3396   while (work)
3397     {
3398       tmp = GTK_CMCTREE_ROW (work)->sibling;
3399       gtk_cmctree_pre_recursive (ctree, work, func, data);
3400       work = tmp;
3401     }
3402 }
3403
3404 void
3405 gtk_cmctree_pre_recursive_to_depth (GtkCMCTree     *ctree, 
3406                                   GtkCMCTreeNode *node,
3407                                   gint          depth, 
3408                                   GtkCMCTreeFunc  func,
3409                                   gpointer      data)
3410 {
3411   GtkCMCTreeNode *work;
3412   GtkCMCTreeNode *tmp;
3413
3414   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3415   cm_return_if_fail (func != NULL);
3416
3417   if (depth < 0)
3418     {
3419       gtk_cmctree_pre_recursive (ctree, node, func, data);
3420       return;
3421     }
3422
3423   if (node)
3424     {
3425       work = GTK_CMCTREE_ROW (node)->children;
3426       if (GTK_CMCTREE_ROW (node)->level <= depth)
3427         func (ctree, node, data);
3428     }
3429   else
3430     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3431
3432   if (work && GTK_CMCTREE_ROW (work)->level <= depth)
3433     {
3434       while (work)
3435         {
3436           tmp = GTK_CMCTREE_ROW (work)->sibling;
3437           gtk_cmctree_pre_recursive_to_depth (ctree, work, depth, func, data);
3438           work = tmp;
3439         }
3440     }
3441 }
3442
3443 gboolean
3444 gtk_cmctree_is_viewable (GtkCMCTree     *ctree, 
3445                        GtkCMCTreeNode *node)
3446
3447   GtkCMCTreeRow *work;
3448
3449   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3450   cm_return_val_if_fail (node != NULL, FALSE);
3451
3452   work = GTK_CMCTREE_ROW (node);
3453
3454   while (work && work->parent && GTK_CMCTREE_ROW (work->parent)->expanded)
3455     work = GTK_CMCTREE_ROW (work->parent);
3456
3457   if (!work->parent)
3458     return TRUE;
3459
3460   return FALSE;
3461 }
3462
3463 GtkCMCTreeNode * 
3464 gtk_cmctree_last (GtkCMCTree     *ctree,
3465                 GtkCMCTreeNode *node)
3466 {
3467   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3468
3469   if (!node) 
3470     return NULL;
3471
3472   while (GTK_CMCTREE_ROW (node)->sibling)
3473     node = GTK_CMCTREE_ROW (node)->sibling;
3474   
3475   if (GTK_CMCTREE_ROW (node)->children)
3476     return gtk_cmctree_last (ctree, GTK_CMCTREE_ROW (node)->children);
3477   
3478   return node;
3479 }
3480
3481 GtkCMCTreeNode *
3482 gtk_cmctree_find_node_ptr (GtkCMCTree    *ctree,
3483                          GtkCMCTreeRow *ctree_row)
3484 {
3485   GtkCMCTreeNode *node;
3486   
3487   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3488   cm_return_val_if_fail (ctree_row != NULL, NULL);
3489   
3490   if (ctree_row->parent)
3491     node = GTK_CMCTREE_ROW (ctree_row->parent)->children;
3492   else
3493     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3494
3495   while (GTK_CMCTREE_ROW (node) != ctree_row)
3496     node = GTK_CMCTREE_ROW (node)->sibling;
3497   
3498   return node;
3499 }
3500
3501 GtkCMCTreeNode *
3502 gtk_cmctree_node_nth (GtkCMCTree *ctree,
3503                     guint     row)
3504 {
3505   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3506
3507   if ((row >= GTK_CMCLIST(ctree)->rows))
3508     return NULL;
3509  
3510   return GTK_CMCTREE_NODE (g_list_nth (GTK_CMCLIST (ctree)->row_list, row));
3511 }
3512
3513 gboolean
3514 gtk_cmctree_find (GtkCMCTree     *ctree,
3515                 GtkCMCTreeNode *node,
3516                 GtkCMCTreeNode *child)
3517 {
3518   if (!child)
3519     return FALSE;
3520
3521   if (!node)
3522     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3523
3524   while (node)
3525     {
3526       if (node == child) 
3527         return TRUE;
3528       if (GTK_CMCTREE_ROW (node)->children)
3529         {
3530           if (gtk_cmctree_find (ctree, GTK_CMCTREE_ROW (node)->children, child))
3531             return TRUE;
3532         }
3533       node = GTK_CMCTREE_ROW (node)->sibling;
3534     }
3535   return FALSE;
3536 }
3537
3538 gboolean
3539 gtk_cmctree_is_ancestor (GtkCMCTree     *ctree,
3540                        GtkCMCTreeNode *node,
3541                        GtkCMCTreeNode *child)
3542 {
3543   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3544   cm_return_val_if_fail (node != NULL, FALSE);
3545
3546   if (GTK_CMCTREE_ROW (node)->children)
3547     return gtk_cmctree_find (ctree, GTK_CMCTREE_ROW (node)->children, child);
3548
3549   return FALSE;
3550 }
3551
3552 GtkCMCTreeNode *
3553 gtk_cmctree_find_by_row_data (GtkCMCTree     *ctree,
3554                             GtkCMCTreeNode *node,
3555                             gpointer      data)
3556 {
3557   GtkCMCTreeNode *work;
3558   
3559   if (!node)
3560     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3561   
3562   while (node)
3563     {
3564       if (GTK_CMCTREE_ROW (node)->row.data == data) 
3565         return node;
3566       if (GTK_CMCTREE_ROW (node)->children &&
3567           (work = gtk_cmctree_find_by_row_data 
3568            (ctree, GTK_CMCTREE_ROW (node)->children, data)))
3569         return work;
3570       node = GTK_CMCTREE_ROW (node)->sibling;
3571     }
3572   return NULL;
3573 }
3574
3575 GList *
3576 gtk_cmctree_find_all_by_row_data (GtkCMCTree     *ctree,
3577                                 GtkCMCTreeNode *node,
3578                                 gpointer      data)
3579 {
3580   GList *list = NULL;
3581
3582   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3583
3584   /* if node == NULL then look in the whole tree */
3585   if (!node)
3586     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3587
3588   while (node)
3589     {
3590       if (GTK_CMCTREE_ROW (node)->row.data == data)
3591         list = g_list_append (list, node);
3592
3593       if (GTK_CMCTREE_ROW (node)->children)
3594         {
3595           GList *sub_list;
3596
3597           sub_list = gtk_cmctree_find_all_by_row_data (ctree,
3598                                                      GTK_CMCTREE_ROW
3599                                                      (node)->children,
3600                                                      data);
3601           list = g_list_concat (list, sub_list);
3602         }
3603       node = GTK_CMCTREE_ROW (node)->sibling;
3604     }
3605   return list;
3606 }
3607
3608 GtkCMCTreeNode *
3609 gtk_cmctree_find_by_row_data_custom (GtkCMCTree     *ctree,
3610                                    GtkCMCTreeNode *node,
3611                                    gpointer      data,
3612                                    GCompareFunc  func)
3613 {
3614   GtkCMCTreeNode *work;
3615
3616   cm_return_val_if_fail (func != NULL, NULL);
3617
3618   if (!node)
3619     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3620
3621   while (node)
3622     {
3623       if (!func (GTK_CMCTREE_ROW (node)->row.data, data))
3624         return node;
3625       if (GTK_CMCTREE_ROW (node)->children &&
3626           (work = gtk_cmctree_find_by_row_data_custom
3627            (ctree, GTK_CMCTREE_ROW (node)->children, data, func)))
3628         return work;
3629       node = GTK_CMCTREE_ROW (node)->sibling;
3630     }
3631   return NULL;
3632 }
3633
3634 GList *
3635 gtk_cmctree_find_all_by_row_data_custom (GtkCMCTree     *ctree,
3636                                        GtkCMCTreeNode *node,
3637                                        gpointer      data,
3638                                        GCompareFunc  func)
3639 {
3640   GList *list = NULL;
3641
3642   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3643   cm_return_val_if_fail (func != NULL, NULL);
3644
3645   /* if node == NULL then look in the whole tree */
3646   if (!node)
3647     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3648
3649   while (node)
3650     {
3651       if (!func (GTK_CMCTREE_ROW (node)->row.data, data))
3652         list = g_list_append (list, node);
3653
3654       if (GTK_CMCTREE_ROW (node)->children)
3655         {
3656           GList *sub_list;
3657
3658           sub_list = gtk_cmctree_find_all_by_row_data_custom (ctree,
3659                                                             GTK_CMCTREE_ROW
3660                                                             (node)->children,
3661                                                             data,
3662                                                             func);
3663           list = g_list_concat (list, sub_list);
3664         }
3665       node = GTK_CMCTREE_ROW (node)->sibling;
3666     }
3667   return list;
3668 }
3669
3670 gboolean
3671 gtk_cmctree_is_hot_spot (GtkCMCTree *ctree, 
3672                        gint      x, 
3673                        gint      y)
3674 {
3675   GtkCMCTreeNode *node;
3676   gint column;
3677   gint row;
3678   
3679   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3680
3681   if (gtk_cmclist_get_selection_info (GTK_CMCLIST (ctree), x, y, &row, &column))
3682     if ((node = GTK_CMCTREE_NODE(g_list_nth (GTK_CMCLIST (ctree)->row_list, row))))
3683       return ctree_is_hot_spot (ctree, node, row, x, y);
3684
3685   return FALSE;
3686 }
3687
3688
3689 /***********************************************************
3690  *   Tree signals : move, expand, collapse, (un)select     *
3691  ***********************************************************/
3692
3693
3694 void
3695 gtk_cmctree_move (GtkCMCTree     *ctree,
3696                 GtkCMCTreeNode *node,
3697                 GtkCMCTreeNode *new_parent, 
3698                 GtkCMCTreeNode *new_sibling)
3699 {
3700   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3701   cm_return_if_fail (node != NULL);
3702   
3703   g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_MOVE], 0, node,
3704                    new_parent, new_sibling);
3705 }
3706
3707 void
3708 gtk_cmctree_expand (GtkCMCTree     *ctree,
3709                   GtkCMCTreeNode *node)
3710 {
3711   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3712   cm_return_if_fail (node != NULL);
3713   
3714   if (GTK_CMCTREE_ROW (node)->is_leaf)
3715     return;
3716
3717   g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0, node);
3718 }
3719
3720 void 
3721 gtk_cmctree_expand_recursive (GtkCMCTree     *ctree,
3722                             GtkCMCTreeNode *node)
3723 {
3724   GtkCMCList *clist;
3725   gboolean thaw = FALSE;
3726
3727   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3728
3729   clist = GTK_CMCLIST (ctree);
3730
3731   if (node && GTK_CMCTREE_ROW (node)->is_leaf)
3732     return;
3733
3734   if (CLIST_UNFROZEN (clist) && (!node || gtk_cmctree_is_viewable (ctree, node)))
3735     {
3736       gtk_cmclist_freeze (clist);
3737       thaw = TRUE;
3738     }
3739
3740   gtk_cmctree_post_recursive (ctree, node, GTK_CMCTREE_FUNC (tree_expand), NULL);
3741
3742   if (thaw)
3743     gtk_cmclist_thaw (clist);
3744 }
3745
3746 void 
3747 gtk_cmctree_expand_to_depth (GtkCMCTree     *ctree,
3748                            GtkCMCTreeNode *node,
3749                            gint          depth)
3750 {
3751   GtkCMCList *clist;
3752   gboolean thaw = FALSE;
3753
3754   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3755
3756   clist = GTK_CMCLIST (ctree);
3757
3758   if (node && GTK_CMCTREE_ROW (node)->is_leaf)
3759     return;
3760
3761   if (CLIST_UNFROZEN (clist) && (!node || gtk_cmctree_is_viewable (ctree, node)))
3762     {
3763       gtk_cmclist_freeze (clist);
3764       thaw = TRUE;
3765     }
3766
3767   gtk_cmctree_post_recursive_to_depth (ctree, node, depth,
3768                                      GTK_CMCTREE_FUNC (tree_expand), NULL);
3769
3770   if (thaw)
3771     gtk_cmclist_thaw (clist);
3772 }
3773
3774 void
3775 gtk_cmctree_collapse (GtkCMCTree     *ctree,
3776                     GtkCMCTreeNode *node)
3777 {
3778   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3779   cm_return_if_fail (node != NULL);
3780   
3781   if (GTK_CMCTREE_ROW (node)->is_leaf)
3782     return;
3783
3784   g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0, node);
3785 }
3786
3787 void 
3788 gtk_cmctree_collapse_recursive (GtkCMCTree     *ctree,
3789                               GtkCMCTreeNode *node)
3790 {
3791   GtkCMCList *clist;
3792   gboolean thaw = FALSE;
3793   gint i;
3794
3795   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3796
3797   if (node && GTK_CMCTREE_ROW (node)->is_leaf)
3798     return;
3799
3800   clist = GTK_CMCLIST (ctree);
3801
3802   if (CLIST_UNFROZEN (clist) && (!node || gtk_cmctree_is_viewable (ctree, node)))
3803     {
3804       gtk_cmclist_freeze (clist);
3805       thaw = TRUE;
3806     }
3807
3808   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3809   gtk_cmctree_post_recursive (ctree, node, GTK_CMCTREE_FUNC (tree_collapse), NULL);
3810   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3811   for (i = 0; i < clist->columns; i++)
3812     if (clist->column[i].auto_resize)
3813       gtk_cmclist_set_column_width (clist, i,
3814                                   gtk_cmclist_optimal_column_width (clist, i));
3815
3816   if (thaw)
3817     gtk_cmclist_thaw (clist);
3818 }
3819
3820 void 
3821 gtk_cmctree_collapse_to_depth (GtkCMCTree     *ctree,
3822                              GtkCMCTreeNode *node,
3823                              gint          depth)
3824 {
3825   GtkCMCList *clist;
3826   gboolean thaw = FALSE;
3827   gint i;
3828
3829   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3830
3831   if (node && GTK_CMCTREE_ROW (node)->is_leaf)
3832     return;
3833
3834   clist = GTK_CMCLIST (ctree);
3835
3836   if (CLIST_UNFROZEN (clist) && (!node || gtk_cmctree_is_viewable (ctree, node)))
3837     {
3838       gtk_cmclist_freeze (clist);
3839       thaw = TRUE;
3840     }
3841
3842   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3843   gtk_cmctree_post_recursive_to_depth (ctree, node, depth,
3844                                      GTK_CMCTREE_FUNC (tree_collapse_to_depth),
3845                                      GINT_TO_POINTER (depth));
3846   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3847   for (i = 0; i < clist->columns; i++)
3848     if (clist->column[i].auto_resize)
3849       gtk_cmclist_set_column_width (clist, i,
3850                                   gtk_cmclist_optimal_column_width (clist, i));
3851
3852   if (thaw)
3853     gtk_cmclist_thaw (clist);
3854 }
3855
3856 void
3857 gtk_cmctree_toggle_expansion (GtkCMCTree     *ctree,
3858                             GtkCMCTreeNode *node)
3859 {
3860   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3861   cm_return_if_fail (node != NULL);
3862   
3863   if (GTK_CMCTREE_ROW (node)->is_leaf)
3864     return;
3865
3866   tree_toggle_expansion (ctree, node, NULL);
3867 }
3868
3869 void 
3870 gtk_cmctree_toggle_expansion_recursive (GtkCMCTree     *ctree,
3871                                       GtkCMCTreeNode *node)
3872 {
3873   GtkCMCList *clist;
3874   gboolean thaw = FALSE;
3875
3876   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3877   
3878   if (node && GTK_CMCTREE_ROW (node)->is_leaf)
3879     return;
3880
3881   clist = GTK_CMCLIST (ctree);
3882
3883   if (CLIST_UNFROZEN (clist) && (!node || gtk_cmctree_is_viewable (ctree, node)))
3884     {
3885       gtk_cmclist_freeze (clist);
3886       thaw = TRUE;
3887     }
3888   
3889   gtk_cmctree_post_recursive (ctree, node,
3890                             GTK_CMCTREE_FUNC (tree_toggle_expansion), NULL);
3891
3892   if (thaw)
3893     gtk_cmclist_thaw (clist);
3894 }
3895
3896 void
3897 gtk_cmctree_select (GtkCMCTree     *ctree, 
3898                   GtkCMCTreeNode *node)
3899 {
3900   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3901   cm_return_if_fail (node != NULL);
3902
3903   if (GTK_CMCTREE_ROW (node)->row.selectable)
3904     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_SELECT_ROW], 0,
3905                      node, -1);
3906 }
3907
3908 void
3909 gtk_cmctree_unselect (GtkCMCTree     *ctree, 
3910                     GtkCMCTreeNode *node)
3911 {
3912   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3913   cm_return_if_fail (node != NULL);
3914
3915   g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_UNSELECT_ROW], 0,
3916                    node, -1);
3917 }
3918
3919 void
3920 gtk_cmctree_select_recursive (GtkCMCTree     *ctree, 
3921                             GtkCMCTreeNode *node)
3922 {
3923   gtk_cmctree_real_select_recursive (ctree, node, TRUE);
3924 }
3925
3926 void
3927 gtk_cmctree_unselect_recursive (GtkCMCTree     *ctree, 
3928                               GtkCMCTreeNode *node)
3929 {
3930   gtk_cmctree_real_select_recursive (ctree, node, FALSE);
3931 }
3932
3933 void
3934 gtk_cmctree_real_select_recursive (GtkCMCTree     *ctree, 
3935                                  GtkCMCTreeNode *node, 
3936                                  gint          state)
3937 {
3938   GtkCMCList *clist;
3939   gboolean thaw = FALSE;
3940
3941   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3942
3943   clist = GTK_CMCLIST (ctree);
3944
3945   if ((state && 
3946        (clist->selection_mode ==  GTK_SELECTION_BROWSE ||
3947         clist->selection_mode == GTK_SELECTION_SINGLE)) ||
3948       (!state && clist->selection_mode ==  GTK_SELECTION_BROWSE))
3949     return;
3950
3951   if (CLIST_UNFROZEN (clist) && (!node || gtk_cmctree_is_viewable (ctree, node)))
3952     {
3953       gtk_cmclist_freeze (clist);
3954       thaw = TRUE;
3955     }
3956
3957   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
3958     {
3959       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3960       
3961       g_list_free (clist->undo_selection);
3962       g_list_free (clist->undo_unselection);
3963       clist->undo_selection = NULL;
3964       clist->undo_unselection = NULL;
3965     }
3966
3967   if (state)
3968     gtk_cmctree_post_recursive (ctree, node,
3969                               GTK_CMCTREE_FUNC (tree_select), NULL);
3970   else 
3971     gtk_cmctree_post_recursive (ctree, node,
3972                               GTK_CMCTREE_FUNC (tree_unselect), NULL);
3973   
3974   if (thaw)
3975     gtk_cmclist_thaw (clist);
3976 }
3977
3978
3979 /***********************************************************
3980  *           Analogons of GtkCMCList functions               *
3981  ***********************************************************/
3982
3983
3984 void 
3985 gtk_cmctree_node_set_text (GtkCMCTree     *ctree,
3986                          GtkCMCTreeNode *node,
3987                          gint          column,
3988                          const gchar  *text)
3989 {
3990   GtkCMCList *clist;
3991
3992   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3993   cm_return_if_fail (node != NULL);
3994
3995   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
3996     return;
3997   
3998   clist = GTK_CMCLIST (ctree);
3999
4000   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
4001     (clist, &(GTK_CMCTREE_ROW (node)->row), column, GTK_CMCELL_TEXT,
4002      text, 0, NULL);
4003
4004   tree_draw_node (ctree, node);
4005 }
4006
4007 void 
4008 gtk_cmctree_node_set_pixbuf (GtkCMCTree     *ctree,
4009                            GtkCMCTreeNode *node,
4010                            gint          column,
4011                            GdkPixbuf    *pixbuf)
4012 {
4013   GtkCMCList *clist;
4014
4015   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
4016   cm_return_if_fail (node != NULL);
4017   cm_return_if_fail (pixbuf != NULL);
4018
4019   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4020     return;
4021
4022   g_object_ref (pixbuf);
4023
4024   clist = GTK_CMCLIST (ctree);
4025
4026   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
4027     (clist, &(GTK_CMCTREE_ROW (node)->row), column, GTK_CMCELL_PIXBUF,
4028      NULL, 0, pixbuf);
4029
4030   tree_draw_node (ctree, node);
4031 }
4032
4033 void 
4034 gtk_cmctree_node_set_pixtext (GtkCMCTree     *ctree,
4035                             GtkCMCTreeNode *node,
4036                             gint          column,
4037                             const gchar  *text,
4038                             guint8        spacing,
4039                             GdkPixbuf    *pixbuf)
4040 {
4041   GtkCMCList *clist;
4042
4043   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
4044   cm_return_if_fail (node != NULL);
4045   if (column != ctree->tree_column)
4046     cm_return_if_fail (pixbuf != NULL);
4047   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4048     return;
4049
4050   clist = GTK_CMCLIST (ctree);
4051
4052   if (pixbuf)
4053     {
4054       g_object_ref (pixbuf);
4055     }
4056
4057   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
4058     (clist, &(GTK_CMCTREE_ROW (node)->row), column, GTK_CMCELL_PIXTEXT,
4059      text, spacing, pixbuf);
4060
4061   tree_draw_node (ctree, node);
4062 }
4063
4064 void 
4065 gtk_cmctree_set_node_info (GtkCMCTree     *ctree,
4066                          GtkCMCTreeNode *node,
4067                          const gchar  *text,
4068                          guint8        spacing,
4069                          GdkPixbuf    *pixbuf_closed,
4070                          GdkPixbuf    *pixbuf_opened,
4071                          gboolean      is_leaf,
4072                          gboolean      expanded)
4073 {
4074   gboolean old_leaf;
4075   gboolean old_expanded;
4076  
4077   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
4078   cm_return_if_fail (node != NULL);
4079
4080   old_leaf = GTK_CMCTREE_ROW (node)->is_leaf;
4081   old_expanded = GTK_CMCTREE_ROW (node)->expanded;
4082
4083   if (is_leaf && GTK_CMCTREE_ROW (node)->children)
4084     {
4085       GtkCMCTreeNode *work;
4086       GtkCMCTreeNode *ptr;
4087       
4088       work = GTK_CMCTREE_ROW (node)->children;
4089       while (work)
4090         {
4091           ptr = work;
4092           work = GTK_CMCTREE_ROW (work)->sibling;
4093           gtk_cmctree_remove_node (ctree, ptr);
4094         }
4095     }
4096
4097   set_node_info (ctree, node, text, spacing, pixbuf_closed,
4098                  pixbuf_opened, is_leaf, expanded);
4099
4100   if (!is_leaf && !old_leaf)
4101     {
4102       GTK_CMCTREE_ROW (node)->expanded = old_expanded;
4103       if (expanded && !old_expanded)
4104         gtk_cmctree_expand (ctree, node);
4105       else if (!expanded && old_expanded)
4106         gtk_cmctree_collapse (ctree, node);
4107     }
4108
4109   GTK_CMCTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
4110   
4111   tree_draw_node (ctree, node);
4112 }
4113
4114 void
4115 gtk_cmctree_node_set_shift (GtkCMCTree     *ctree,
4116                           GtkCMCTreeNode *node,
4117                           gint          column,
4118                           gint          vertical,
4119                           gint          horizontal)
4120 {
4121   GtkCMCList *clist;
4122   GtkRequisition requisition;
4123   gboolean visible = FALSE;
4124
4125   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
4126   cm_return_if_fail (node != NULL);
4127
4128   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4129     return;
4130
4131   clist = GTK_CMCLIST (ctree);
4132
4133   if (clist->column[column].auto_resize &&
4134       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
4135     {
4136       visible = gtk_cmctree_is_viewable (ctree, node);
4137       if (visible)
4138         GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
4139           (clist, &GTK_CMCTREE_ROW (node)->row, column, &requisition);
4140     }
4141
4142   GTK_CMCTREE_ROW (node)->row.cell[column].vertical   = vertical;
4143   GTK_CMCTREE_ROW (node)->row.cell[column].horizontal = horizontal;
4144
4145   if (visible)
4146     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row,
4147                         column, requisition.width);
4148
4149   tree_draw_node (ctree, node);
4150 }
4151
4152 static void
4153 remove_grab (GtkCMCList *clist)
4154 {
4155   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (clist))) && 
4156       gtk_widget_has_grab (GTK_WIDGET(clist)))
4157     {
4158       gtk_grab_remove (GTK_WIDGET (clist));
4159       gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (clist)),
4160                                   GDK_CURRENT_TIME);
4161     }
4162
4163   if (clist->htimer)
4164     {
4165       g_source_remove (clist->htimer);
4166       clist->htimer = 0;
4167     }
4168
4169   if (clist->vtimer)
4170     {
4171       g_source_remove (clist->vtimer);
4172       clist->vtimer = 0;
4173     }
4174 }
4175
4176 void
4177 gtk_cmctree_node_set_selectable (GtkCMCTree     *ctree,
4178                                GtkCMCTreeNode *node,
4179                                gboolean      selectable)
4180 {
4181   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
4182   cm_return_if_fail (node != NULL);
4183
4184   if (selectable == GTK_CMCTREE_ROW (node)->row.selectable)
4185     return;
4186
4187   GTK_CMCTREE_ROW (node)->row.selectable = selectable;
4188
4189   if (!selectable && GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED)
4190     {
4191       GtkCMCList *clist;
4192
4193       clist = GTK_CMCLIST (ctree);
4194
4195       if (clist->anchor >= 0 &&
4196           clist->selection_mode == GTK_SELECTION_MULTIPLE)
4197         {
4198           clist->drag_button = 0;
4199           remove_grab (clist);
4200
4201           GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
4202         }
4203       gtk_cmctree_unselect (ctree, node);
4204     }      
4205 }
4206
4207 gboolean
4208 gtk_cmctree_node_get_selectable (GtkCMCTree     *ctree,
4209                                GtkCMCTreeNode *node)
4210 {
4211   cm_return_val_if_fail (node != NULL, FALSE);
4212
4213   return GTK_CMCTREE_ROW (node)->row.selectable;
4214 }
4215
4216 GtkCMCellType 
4217 gtk_cmctree_node_get_cell_type (GtkCMCTree     *ctree,
4218                               GtkCMCTreeNode *node,
4219                               gint          column)
4220 {
4221   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), -1);
4222   cm_return_val_if_fail (node != NULL, -1);
4223
4224   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4225     return -1;
4226
4227   return GTK_CMCTREE_ROW (node)->row.cell[column].type;
4228 }
4229
4230 gboolean
4231 gtk_cmctree_node_get_text (GtkCMCTree      *ctree,
4232                          GtkCMCTreeNode  *node,
4233                          gint           column,
4234                          gchar        **text)
4235 {
4236   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
4237   cm_return_val_if_fail (node != NULL, FALSE);
4238
4239   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4240     return FALSE;
4241
4242   if (GTK_CMCTREE_ROW (node)->row.cell[column].type != GTK_CMCELL_TEXT)
4243     return FALSE;
4244
4245   if (text)
4246     *text = GTK_CMCELL_TEXT (GTK_CMCTREE_ROW (node)->row.cell[column])->text;
4247
4248   return TRUE;
4249 }
4250
4251 gboolean
4252 gtk_cmctree_node_get_pixbuf (GtkCMCTree     *ctree,
4253                            GtkCMCTreeNode *node,
4254                            gint          column,
4255                            GdkPixbuf   **pixbuf)
4256 {
4257   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
4258   cm_return_val_if_fail (node != NULL, FALSE);
4259
4260   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4261     return FALSE;
4262
4263   if (GTK_CMCTREE_ROW (node)->row.cell[column].type != GTK_CMCELL_PIXBUF)
4264     return FALSE;
4265
4266   if (pixbuf)
4267     *pixbuf = GTK_CMCELL_PIXBUF (GTK_CMCTREE_ROW (node)->row.cell[column])->pixbuf;
4268
4269   return TRUE;
4270 }
4271
4272 gboolean
4273 gtk_cmctree_node_get_pixtext (GtkCMCTree      *ctree,
4274                             GtkCMCTreeNode  *node,
4275                             gint           column,
4276                             gchar        **text,
4277                             guint8        *spacing,
4278                             GdkPixbuf    **pixbuf)
4279 {
4280   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
4281   cm_return_val_if_fail (node != NULL, FALSE);
4282   
4283   if (column < 0 || column >= GTK_CMCLIST (ctree)->columns)
4284     return FALSE;
4285   
4286   if (GTK_CMCTREE_ROW (node)->row.cell[column].type != GTK_CMCELL_PIXTEXT)
4287     return FALSE;
4288   
4289   if (text)
4290     *text = GTK_CMCELL_PIXTEXT (GTK_CMCTREE_ROW (node)->row.cell[column])->text;
4291   if (spacing)
4292     *spacing = GTK_CMCELL_PIXTEXT (GTK_CMCTREE_ROW 
4293                                  (node)->row.cell[column])->spacing;
4294   if (pixbuf)
4295     *pixbuf = GTK_CMCELL_PIXTEXT (GTK_CMCTREE_ROW 
4296                                 (node)->row.cell[column])->pixbuf;
4297   
4298   return TRUE;
4299 }
4300
4301 gboolean
4302 gtk_cmctree_get_node_info (GtkCMCTree      *ctree,
4303                          GtkCMCTreeNode  *node,
4304                          gchar        **text,
4305                          guint8        *spacing,
4306                          GdkPixbuf    **pixbuf_closed,
4307                          GdkPixbuf    **pixbuf_opened,
4308                          gboolean      *is_leaf,
4309                          gboolean      *expanded)
4310 {
4311   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
4312   cm_return_val_if_fail (node != NULL, FALSE);
4313   
4314   if (text)
4315     *text = GTK_CMCELL_PIXTEXT 
4316       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->text;
4317   if (spacing)
4318     *spacing = GTK_CMCELL_PIXTEXT 
4319       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->spacing;
4320   if (pixbuf_closed)
4321     *pixbuf_closed = GTK_CMCTREE_ROW (node)->pixbuf_closed;
4322   if (pixbuf_opened)
4323     *pixbuf_opened = GTK_CMCTREE_ROW (node)->pixbuf_opened;
4324   if (is_leaf)
4325     *is_leaf = GTK_CMCTREE_ROW (node)->is_leaf;
4326   if (expanded)
4327     *expanded = GTK_CMCTREE_ROW (node)->expanded;
4328   
4329   return TRUE;
4330 }
4331
4332 void
4333 gtk_cmctree_node_set_cell_style (GtkCMCTree     *ctree,
4334                                GtkCMCTreeNode *node,
4335                                gint          column,
4336                                GtkStyle     *style)
4337 {
4338   GtkCMCList *clist;
4339   GtkRequisition requisition;
4340   gboolean visible = FALSE;
4341
4342   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
4343   cm_return_if_fail (node != NULL);
4344
4345   clist = GTK_CMCLIST (ctree);
4346
4347   if (column < 0 || column >= clist->columns)
4348     return;
4349
<