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