2012-09-16 [mones] 3.8.1cvs60
[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 GtkCMCTreeNode *
1471 gtk_cmctree_last_visible (GtkCMCTree     *ctree,
1472                         GtkCMCTreeNode *node)
1473 {
1474   GtkCMCTreeNode *work;
1475   
1476   if (!node)
1477     return NULL;
1478
1479   work = GTK_CMCTREE_ROW (node)->children;
1480
1481   if (!work || !GTK_CMCTREE_ROW (node)->expanded)
1482     return node;
1483
1484   while (GTK_CMCTREE_ROW (work)->sibling)
1485     work = GTK_CMCTREE_ROW (work)->sibling;
1486
1487   return gtk_cmctree_last_visible (ctree, work);
1488 }
1489
1490 static void
1491 gtk_cmctree_link (GtkCMCTree     *ctree,
1492                 GtkCMCTreeNode *node,
1493                 GtkCMCTreeNode *parent,
1494                 GtkCMCTreeNode *sibling,
1495                 gboolean      update_focus_row)
1496 {
1497   GtkCMCList *clist;
1498   GList *list_end;
1499   GList *list;
1500   GList *work;
1501   gboolean visible = FALSE;
1502   gint rows = 0;
1503   
1504   if (sibling)
1505     cm_return_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent);
1506   cm_return_if_fail (node != NULL);
1507   cm_return_if_fail (node != sibling);
1508   cm_return_if_fail (node != parent);
1509
1510   clist = GTK_CMCLIST (ctree);
1511
1512   if (update_focus_row && clist->selection_mode == GTK_SELECTION_MULTIPLE)
1513     {
1514       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1515       
1516       g_list_free (clist->undo_selection);
1517       g_list_free (clist->undo_unselection);
1518       clist->undo_selection = NULL;
1519       clist->undo_unselection = NULL;
1520     }
1521
1522   for (rows = 1, list_end = (GList *)node; list_end->next;
1523        list_end = list_end->next)
1524     rows++;
1525
1526   GTK_CMCTREE_ROW (node)->parent = parent;
1527   GTK_CMCTREE_ROW (node)->sibling = sibling;
1528
1529   if (!parent || (parent && (gtk_cmctree_is_viewable (ctree, parent) &&
1530                              GTK_CMCTREE_ROW (parent)->expanded)))
1531     {
1532       visible = TRUE;
1533       clist->rows += rows;
1534     }
1535
1536   if (parent)
1537     work = (GList *)(GTK_CMCTREE_ROW (parent)->children);
1538   else
1539     work = clist->row_list;
1540
1541   if (sibling)
1542     {
1543       if (work != (GList *)sibling)
1544         {
1545           while (GTK_CMCTREE_ROW (work)->sibling != sibling)
1546             work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
1547           GTK_CMCTREE_ROW (work)->sibling = node;
1548         }
1549
1550       if (sibling == GTK_CMCTREE_NODE (clist->row_list))
1551         clist->row_list = (GList *) node;
1552       if (GTK_CMCTREE_NODE_PREV (sibling) &&
1553           GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (sibling)) == sibling)
1554         {
1555           list = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
1556           list->next = (GList *)node;
1557         }
1558       
1559       list = (GList *)node;
1560       list->prev = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
1561       list_end->next = (GList *)sibling;
1562       list = (GList *)sibling;
1563       list->prev = list_end;
1564       if (parent && GTK_CMCTREE_ROW (parent)->children == sibling)
1565         GTK_CMCTREE_ROW (parent)->children = node;
1566     }
1567   else
1568     {
1569       if (work)
1570         {
1571           /* find sibling */
1572           while (GTK_CMCTREE_ROW (work)->sibling)
1573             work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
1574           GTK_CMCTREE_ROW (work)->sibling = node;
1575           
1576           /* find last visible child of sibling */
1577           work = (GList *) gtk_cmctree_last_visible (ctree,
1578                                                    GTK_CMCTREE_NODE (work));
1579           
1580           list_end->next = work->next;
1581           if (work->next)
1582             list = work->next->prev = list_end;
1583           work->next = (GList *)node;
1584           list = (GList *)node;
1585           list->prev = work;
1586         }
1587       else
1588         {
1589           if (parent)
1590             {
1591               GTK_CMCTREE_ROW (parent)->children = node;
1592               list = (GList *)node;
1593               list->prev = (GList *)parent;
1594               if (GTK_CMCTREE_ROW (parent)->expanded)
1595                 {
1596                   list_end->next = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
1597                   if (GTK_CMCTREE_NODE_NEXT(parent))
1598                     {
1599                       list = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
1600                       list->prev = list_end;
1601                     }
1602                   list = (GList *)parent;
1603                   list->next = (GList *)node;
1604                 }
1605               else
1606                 list_end->next = NULL;
1607             }
1608           else
1609             {
1610               clist->row_list = (GList *)node;
1611               list = (GList *)node;
1612               list->prev = NULL;
1613               list_end->next = NULL;
1614             }
1615         }
1616     }
1617
1618   gtk_cmctree_pre_recursive (ctree, node, tree_update_level, NULL); 
1619
1620   if (clist->row_list_end == NULL ||
1621       clist->row_list_end->next == (GList *)node)
1622     clist->row_list_end = list_end;
1623
1624   if (visible && update_focus_row)
1625     {
1626       gint pos;
1627           
1628       pos = g_list_position (clist->row_list, (GList *)node);
1629   
1630       if (pos <= clist->focus_row)
1631         {
1632           clist->focus_row += rows;
1633           clist->undo_anchor = clist->focus_row;
1634         }
1635     }
1636 }
1637
1638 static void
1639 gtk_cmctree_unlink (GtkCMCTree     *ctree, 
1640                   GtkCMCTreeNode *node,
1641                   gboolean      update_focus_row)
1642 {
1643   GtkCMCList *clist;
1644   gint rows;
1645   gint level;
1646   gint visible;
1647   GtkCMCTreeNode *work;
1648   GtkCMCTreeNode *parent;
1649   GList *list;
1650
1651   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1652   cm_return_if_fail (node != NULL);
1653
1654   clist = GTK_CMCLIST (ctree);
1655   
1656   if (update_focus_row && clist->selection_mode == GTK_SELECTION_MULTIPLE)
1657     {
1658       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1659       
1660       g_list_free (clist->undo_selection);
1661       g_list_free (clist->undo_unselection);
1662       clist->undo_selection = NULL;
1663       clist->undo_unselection = NULL;
1664     }
1665
1666   visible = gtk_cmctree_is_viewable (ctree, node);
1667
1668   /* clist->row_list_end unlinked ? */
1669   if (visible &&
1670       (GTK_CMCTREE_NODE_NEXT (node) == NULL ||
1671        (GTK_CMCTREE_ROW (node)->children &&
1672         gtk_cmctree_is_ancestor (ctree, node,
1673                                GTK_CMCTREE_NODE (clist->row_list_end)))))
1674     clist->row_list_end = (GList *) (GTK_CMCTREE_NODE_PREV (node));
1675
1676   /* update list */
1677   rows = 0;
1678   level = GTK_CMCTREE_ROW (node)->level;
1679   work = GTK_CMCTREE_NODE_NEXT (node);
1680   while (work && GTK_CMCTREE_ROW (work)->level > level)
1681     {
1682       work = GTK_CMCTREE_NODE_NEXT (work);
1683       rows++;
1684     }
1685
1686   if (visible)
1687     {
1688       clist->rows -= (rows + 1);
1689
1690       if (update_focus_row)
1691         {
1692           gint pos;
1693           
1694           pos = g_list_position (clist->row_list, (GList *)node);
1695           if (pos + rows < clist->focus_row)
1696             clist->focus_row -= (rows + 1);
1697           else if (pos <= clist->focus_row)
1698             {
1699               if (!GTK_CMCTREE_ROW (node)->sibling)
1700                 clist->focus_row = MAX (pos - 1, 0);
1701               else
1702                 clist->focus_row = pos;
1703               
1704               clist->focus_row = MIN (clist->focus_row, clist->rows - 1);
1705             }
1706           clist->undo_anchor = clist->focus_row;
1707         }
1708     }
1709
1710   if (work)
1711     {
1712       list = (GList *)GTK_CMCTREE_NODE_PREV (work);
1713       list->next = NULL;
1714       list = (GList *)work;
1715       list->prev = (GList *)GTK_CMCTREE_NODE_PREV (node);
1716     }
1717
1718   if (GTK_CMCTREE_NODE_PREV (node) &&
1719       GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (node)) == node)
1720     {
1721       list = (GList *)GTK_CMCTREE_NODE_PREV (node);
1722       list->next = (GList *)work;
1723     }
1724
1725   /* update tree */
1726   parent = GTK_CMCTREE_ROW (node)->parent;
1727   if (parent)
1728     {
1729       if (GTK_CMCTREE_ROW (parent)->children == node)
1730         {
1731           GTK_CMCTREE_ROW (parent)->children = GTK_CMCTREE_ROW (node)->sibling;
1732           if (!GTK_CMCTREE_ROW (parent)->children)
1733             gtk_cmctree_collapse (ctree, parent);
1734         }
1735       else
1736         {
1737           GtkCMCTreeNode *sibling;
1738
1739           sibling = GTK_CMCTREE_ROW (parent)->children;
1740           while (GTK_CMCTREE_ROW (sibling)->sibling != node)
1741             sibling = GTK_CMCTREE_ROW (sibling)->sibling;
1742           GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
1743         }
1744     }
1745   else
1746     {
1747       if (clist->row_list == (GList *)node)
1748         clist->row_list = (GList *) (GTK_CMCTREE_ROW (node)->sibling);
1749       else
1750         {
1751           GtkCMCTreeNode *sibling;
1752
1753           sibling = GTK_CMCTREE_NODE (clist->row_list);
1754           while (GTK_CMCTREE_ROW (sibling)->sibling != node)
1755             sibling = GTK_CMCTREE_ROW (sibling)->sibling;
1756           GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
1757         }
1758     }
1759 }
1760
1761 static void
1762 real_row_move (GtkCMCList *clist,
1763                gint      source_row,
1764                gint      dest_row)
1765 {
1766   GtkCMCTree *ctree;
1767   GtkCMCTreeNode *node;
1768
1769   cm_return_if_fail (GTK_IS_CMCTREE (clist));
1770
1771   if (GTK_CMCLIST_AUTO_SORT (clist))
1772     return;
1773
1774   if (source_row < 0 || source_row >= clist->rows ||
1775       dest_row   < 0 || dest_row   >= clist->rows ||
1776       source_row == dest_row)
1777     return;
1778
1779   ctree = GTK_CMCTREE (clist);
1780   node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, source_row));
1781
1782   if (source_row < dest_row)
1783     {
1784       GtkCMCTreeNode *work; 
1785
1786       dest_row++;
1787       work = GTK_CMCTREE_ROW (node)->children;
1788
1789       while (work && GTK_CMCTREE_ROW (work)->level > GTK_CMCTREE_ROW (node)->level)
1790         {
1791           work = GTK_CMCTREE_NODE_NEXT (work);
1792           dest_row++;
1793         }
1794
1795       if (dest_row > clist->rows)
1796         dest_row = clist->rows;
1797     }
1798
1799   if (dest_row < clist->rows)
1800     {
1801       GtkCMCTreeNode *sibling;
1802
1803       sibling = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, dest_row));
1804       gtk_cmctree_move (ctree, node, GTK_CMCTREE_ROW (sibling)->parent, sibling);
1805     }
1806   else
1807     gtk_cmctree_move (ctree, node, NULL, NULL);
1808 }
1809
1810 static void
1811 real_tree_move (GtkCMCTree     *ctree,
1812                 GtkCMCTreeNode *node,
1813                 GtkCMCTreeNode *new_parent, 
1814                 GtkCMCTreeNode *new_sibling)
1815 {
1816   GtkCMCList *clist;
1817   GtkCMCTreeNode *work;
1818   gboolean visible = FALSE;
1819
1820   cm_return_if_fail (ctree != NULL);
1821   cm_return_if_fail (node != NULL);
1822   cm_return_if_fail (!new_sibling || 
1823                     GTK_CMCTREE_ROW (new_sibling)->parent == new_parent);
1824
1825   if (new_parent && GTK_CMCTREE_ROW (new_parent)->is_leaf)
1826     return;
1827
1828   /* new_parent != child of child */
1829   for (work = new_parent; work; work = GTK_CMCTREE_ROW (work)->parent)
1830     if (work == node)
1831       return;
1832
1833   clist = GTK_CMCLIST (ctree);
1834
1835   visible = gtk_cmctree_is_viewable (ctree, node);
1836
1837   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
1838     {
1839       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1840       
1841       g_list_free (clist->undo_selection);
1842       g_list_free (clist->undo_unselection);
1843       clist->undo_selection = NULL;
1844       clist->undo_unselection = NULL;
1845     }
1846
1847   if (GTK_CMCLIST_AUTO_SORT (clist))
1848     {
1849       if (new_parent == GTK_CMCTREE_ROW (node)->parent)
1850         return;
1851       
1852       if (new_parent)
1853         new_sibling = GTK_CMCTREE_ROW (new_parent)->children;
1854       else
1855         new_sibling = GTK_CMCTREE_NODE (clist->row_list);
1856
1857       while (new_sibling && clist->compare
1858              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (new_sibling)) > 0)
1859         new_sibling = GTK_CMCTREE_ROW (new_sibling)->sibling;
1860     }
1861
1862   if (new_parent == GTK_CMCTREE_ROW (node)->parent && 
1863       new_sibling == GTK_CMCTREE_ROW (node)->sibling)
1864     return;
1865
1866   gtk_cmclist_freeze (clist);
1867
1868   work = NULL;
1869   if (gtk_cmctree_is_viewable (ctree, node))
1870     work = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
1871       
1872   gtk_cmctree_unlink (ctree, node, FALSE);
1873   gtk_cmctree_link (ctree, node, new_parent, new_sibling, FALSE);
1874   
1875   if (work)
1876     {
1877       while (work &&  !gtk_cmctree_is_viewable (ctree, work))
1878         work = GTK_CMCTREE_ROW (work)->parent;
1879       clist->focus_row = g_list_position (clist->row_list, (GList *)work);
1880       clist->undo_anchor = clist->focus_row;
1881     }
1882
1883   if (clist->column[ctree->tree_column].auto_resize &&
1884       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
1885       (visible || gtk_cmctree_is_viewable (ctree, node)))
1886     gtk_cmclist_set_column_width
1887       (clist, ctree->tree_column,
1888        gtk_cmclist_optimal_column_width (clist, ctree->tree_column));
1889
1890   gtk_cmclist_thaw (clist);
1891 }
1892
1893 static void
1894 change_focus_row_expansion (GtkCMCTree          *ctree,
1895                             GtkCMCTreeExpansionType action)
1896 {
1897   GtkCMCList *clist;
1898   GtkCMCTreeNode *node;
1899
1900   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1901
1902   clist = GTK_CMCLIST (ctree);
1903
1904   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (ctree))) && 
1905       gtk_widget_has_grab (GTK_WIDGET(ctree)))
1906     return;
1907   
1908   if (!(node =
1909         GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row))) ||
1910       GTK_CMCTREE_ROW (node)->is_leaf || !(GTK_CMCTREE_ROW (node)->children))
1911     return;
1912
1913   switch (action)
1914     {
1915     case GTK_CMCTREE_EXPANSION_EXPAND:
1916       gtk_cmctree_expand (ctree, node);
1917       break;
1918     case GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE:
1919       gtk_cmctree_expand_recursive (ctree, node);
1920       break;
1921     case GTK_CMCTREE_EXPANSION_COLLAPSE:
1922       gtk_cmctree_collapse (ctree, node);
1923       break;
1924     case GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE:
1925       gtk_cmctree_collapse_recursive (ctree, node);
1926       break;
1927     case GTK_CMCTREE_EXPANSION_TOGGLE:
1928       gtk_cmctree_toggle_expansion (ctree, node);
1929       break;
1930     case GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE:
1931       gtk_cmctree_toggle_expansion_recursive (ctree, node);
1932       break;
1933     }
1934 }
1935
1936 static void 
1937 real_tree_expand (GtkCMCTree     *ctree,
1938                   GtkCMCTreeNode *node)
1939 {
1940   GtkCMCList *clist;
1941   GtkCMCTreeNode *work;
1942   GtkRequisition requisition;
1943   gboolean visible;
1944
1945   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1946
1947   if (!node || GTK_CMCTREE_ROW (node)->expanded || GTK_CMCTREE_ROW (node)->is_leaf)
1948     return;
1949
1950   clist = GTK_CMCLIST (ctree);
1951   
1952   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1953
1954   GTK_CMCTREE_ROW (node)->expanded = TRUE;
1955
1956   visible = gtk_cmctree_is_viewable (ctree, node);
1957   /* get cell width if tree_column is auto resized */
1958   if (visible && clist->column[ctree->tree_column].auto_resize &&
1959       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1960     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1961       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
1962
1963   /* unref/unset closed pixbuf */
1964   if (GTK_CMCELL_PIXTEXT 
1965       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
1966     {
1967       g_object_unref
1968         (GTK_CMCELL_PIXTEXT
1969          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
1970       
1971       GTK_CMCELL_PIXTEXT
1972         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
1973     }
1974
1975   /* set/ref opened pixbuf */
1976   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
1977     {
1978       GTK_CMCELL_PIXTEXT 
1979         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
1980         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
1981     }
1982
1983
1984   work = GTK_CMCTREE_ROW (node)->children;
1985   if (work)
1986     {
1987       GList *list = (GList *)work;
1988       gint *cell_width = NULL;
1989       gint tmp = 0;
1990       gint row;
1991       gint i;
1992       
1993       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1994         {
1995           cell_width = g_new0 (gint, clist->columns);
1996           if (clist->column[ctree->tree_column].auto_resize)
1997               cell_width[ctree->tree_column] = requisition.width;
1998
1999           while (work)
2000             {
2001               /* search maximum cell widths of auto_resize columns */
2002               for (i = 0; i < clist->columns; i++)
2003                 if (clist->column[i].auto_resize)
2004                   {
2005                     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2006                       (clist, &GTK_CMCTREE_ROW (work)->row, i, &requisition);
2007                     cell_width[i] = MAX (requisition.width, cell_width[i]);
2008                   }
2009
2010               list = (GList *)work;
2011               work = GTK_CMCTREE_NODE_NEXT (work);
2012               tmp++;
2013             }
2014         }
2015       else
2016         while (work)
2017           {
2018             list = (GList *)work;
2019             work = GTK_CMCTREE_NODE_NEXT (work);
2020             tmp++;
2021           }
2022
2023       list->next = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2024
2025       if (GTK_CMCTREE_NODE_NEXT (node))
2026         {
2027           GList *tmp_list;
2028
2029           tmp_list = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2030           tmp_list->prev = list;
2031         }
2032       else
2033         clist->row_list_end = list;
2034
2035       list = (GList *)node;
2036       list->next = (GList *)(GTK_CMCTREE_ROW (node)->children);
2037
2038       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2039         {
2040           /* resize auto_resize columns if needed */
2041           for (i = 0; i < clist->columns; i++)
2042             if (clist->column[i].auto_resize &&
2043                 cell_width[i] > clist->column[i].width)
2044               gtk_cmclist_set_column_width (clist, i, cell_width[i]);
2045           g_free (cell_width);
2046
2047           /* update focus_row position */
2048           row = g_list_position (clist->row_list, (GList *)node);
2049           if (row < clist->focus_row)
2050             clist->focus_row += tmp;
2051
2052           clist->rows += tmp;
2053           CLIST_REFRESH (clist);
2054         }
2055     }
2056   else if (visible && clist->column[ctree->tree_column].auto_resize)
2057     /* resize tree_column if needed */
2058     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2059                         requisition.width);
2060 }
2061
2062 static void 
2063 real_tree_collapse (GtkCMCTree     *ctree,
2064                     GtkCMCTreeNode *node)
2065 {
2066   GtkCMCList *clist;
2067   GtkCMCTreeNode *work;
2068   GtkRequisition requisition;
2069   gboolean visible;
2070   gint level;
2071
2072   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2073
2074   if (!node || !GTK_CMCTREE_ROW (node)->expanded ||
2075       GTK_CMCTREE_ROW (node)->is_leaf)
2076     return;
2077
2078   clist = GTK_CMCLIST (ctree);
2079
2080   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2081   
2082   GTK_CMCTREE_ROW (node)->expanded = FALSE;
2083   level = GTK_CMCTREE_ROW (node)->level;
2084
2085   visible = gtk_cmctree_is_viewable (ctree, node);
2086   /* get cell width if tree_column is auto resized */
2087   if (visible && clist->column[ctree->tree_column].auto_resize &&
2088       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2089     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2090       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
2091
2092   /* unref/unset opened pixbuf */
2093   if (GTK_CMCELL_PIXTEXT 
2094       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
2095     {
2096       g_object_unref
2097         (GTK_CMCELL_PIXTEXT
2098          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
2099       
2100       GTK_CMCELL_PIXTEXT
2101         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
2102     }
2103
2104   /* set/ref closed pixbuf */
2105   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2106     {
2107       GTK_CMCELL_PIXTEXT 
2108         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
2109         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2110     }
2111
2112   work = GTK_CMCTREE_ROW (node)->children;
2113   if (work)
2114     {
2115       gint tmp = 0;
2116       gint row;
2117       GList *list;
2118
2119       while (work && GTK_CMCTREE_ROW (work)->level > level)
2120         {
2121           work = GTK_CMCTREE_NODE_NEXT (work);
2122           tmp++;
2123         }
2124
2125       if (work)
2126         {
2127           list = (GList *)node;
2128           list->next = (GList *)work;
2129           list = (GList *)GTK_CMCTREE_NODE_PREV (work);
2130           list->next = NULL;
2131           list = (GList *)work;
2132           list->prev = (GList *)node;
2133         }
2134       else
2135         {
2136           list = (GList *)node;
2137           list->next = NULL;
2138           clist->row_list_end = (GList *)node;
2139         }
2140
2141       if (visible)
2142         {
2143           /* resize auto_resize columns if needed */
2144           auto_resize_columns (clist);
2145
2146           row = g_list_position (clist->row_list, (GList *)node);
2147           if (row < clist->focus_row)
2148             clist->focus_row -= tmp;
2149           clist->rows -= tmp;
2150           CLIST_REFRESH (clist);
2151         }
2152     }
2153   else if (visible && clist->column[ctree->tree_column].auto_resize &&
2154            !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2155     /* resize tree_column if needed */
2156     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2157                         requisition.width);
2158     
2159 }
2160
2161 static void
2162 column_auto_resize (GtkCMCList    *clist,
2163                     GtkCMCListRow *clist_row,
2164                     gint         column,
2165                     gint         old_width)
2166 {
2167   /* resize column if needed for auto_resize */
2168   GtkRequisition requisition;
2169
2170   if (!clist->column[column].auto_resize ||
2171       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2172     return;
2173
2174   if (clist_row)
2175     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2176                                                    column, &requisition);
2177   else
2178     requisition.width = 0;
2179
2180   if (requisition.width > clist->column[column].width)
2181     gtk_cmclist_set_column_width (clist, column, requisition.width);
2182   else if (requisition.width < old_width &&
2183            old_width == clist->column[column].width)
2184     {
2185       GList *list;
2186       gint new_width;
2187
2188       /* run a "gtk_cmclist_optimal_column_width" but break, if
2189        * the column doesn't shrink */
2190       if (GTK_CMCLIST_SHOW_TITLES (clist) && clist->column[column].button)
2191         {
2192         GtkRequisition req;
2193         gtk_widget_get_requisition (clist->column[column].button, &req);
2194         new_width = (req.width -
2195                      (CELL_SPACING + (2 * COLUMN_INSET)));
2196         }
2197       else
2198         new_width = 0;
2199
2200       for (list = clist->row_list; list; list = list->next)
2201         {
2202           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2203             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
2204           new_width = MAX (new_width, requisition.width);
2205           if (new_width == clist->column[column].width)
2206             break;
2207         }
2208       if (new_width < clist->column[column].width)
2209         gtk_cmclist_set_column_width (clist, column, new_width);
2210     }
2211 }
2212
2213 static void
2214 auto_resize_columns (GtkCMCList *clist)
2215 {
2216   gint i;
2217
2218   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2219     return;
2220
2221   for (i = 0; i < clist->columns; i++)
2222     column_auto_resize (clist, NULL, i, clist->column[i].width);
2223 }
2224
2225 static void
2226 cell_size_request (GtkCMCList       *clist,
2227                    GtkCMCListRow    *clist_row,
2228                    gint            column,
2229                    GtkRequisition *requisition)
2230 {
2231   GtkCMCTree *ctree;
2232   gint width;
2233   gint height;
2234   PangoLayout *layout;
2235   PangoRectangle logical_rect;
2236
2237   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2238   cm_return_if_fail (requisition != NULL);
2239
2240   ctree = GTK_CMCTREE (clist);
2241
2242   layout = create_cell_layout (clist, clist_row, column);
2243   if (layout)
2244     {
2245       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2246
2247       requisition->width = logical_rect.width;
2248       requisition->height = logical_rect.height;
2249       
2250       g_object_unref (G_OBJECT (layout));
2251     }
2252   else
2253     {
2254       requisition->width  = 0;
2255       requisition->height = 0;
2256     }
2257
2258   switch (clist_row->cell[column].type)
2259     {
2260     case GTK_CMCELL_PIXTEXT:
2261       if (GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf)
2262         {
2263           width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2264           height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2265           width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2266         }
2267       else
2268         width = height = 0;
2269           
2270       requisition->width += width;
2271       requisition->height = MAX (requisition->height, height);
2272       
2273       if (column == ctree->tree_column)
2274         {
2275           requisition->width += (ctree->tree_spacing + ctree->tree_indent *
2276                                  (((GtkCMCTreeRow *) clist_row)->level - 1));
2277           switch (ctree->expander_style)
2278             {
2279             case GTK_CMCTREE_EXPANDER_NONE:
2280               break;
2281             case GTK_CMCTREE_EXPANDER_TRIANGLE:
2282               requisition->width += PM_SIZE + 3;
2283               break;
2284             }
2285         }
2286       break;
2287     case GTK_CMCELL_PIXBUF:
2288       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2289       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2290       requisition->width += width;
2291       requisition->height = MAX (requisition->height, height);
2292       break;
2293     default:
2294       break;
2295     }
2296
2297   requisition->width  += clist_row->cell[column].horizontal;
2298   requisition->height += clist_row->cell[column].vertical;
2299 }
2300
2301 static void
2302 set_cell_contents (GtkCMCList    *clist,
2303                    GtkCMCListRow *clist_row,
2304                    gint         column,
2305                    GtkCMCellType  type,
2306                    const gchar *text,
2307                    guint8       spacing,
2308                    GdkPixbuf   *pixbuf)
2309 {
2310   gboolean visible = FALSE;
2311   GtkCMCTree *ctree;
2312   GtkRequisition requisition;
2313   gchar *old_text = NULL;
2314   GdkPixbuf *old_pixbuf = NULL;
2315
2316   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2317   cm_return_if_fail (clist_row != NULL);
2318
2319   ctree = GTK_CMCTREE (clist);
2320
2321   if (clist->column[column].auto_resize &&
2322       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2323     {
2324       GtkCMCTreeNode *parent;
2325
2326       parent = ((GtkCMCTreeRow *)clist_row)->parent;
2327       if ((parent && GTK_CMCTREE_ROW (parent)->expanded &&
2328                       gtk_cmctree_is_viewable (ctree, parent)))
2329         {
2330           visible = TRUE;
2331           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2332                                                          column, &requisition);
2333         }
2334     }
2335
2336   switch (clist_row->cell[column].type)
2337     {
2338     case GTK_CMCELL_EMPTY:
2339       break;
2340     case GTK_CMCELL_TEXT:
2341       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2342       break;
2343     case GTK_CMCELL_PIXBUF:
2344       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2345       break;
2346     case GTK_CMCELL_PIXTEXT:
2347       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2348       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2349       break;
2350     case GTK_CMCELL_WIDGET:
2351       /* unimplemented */
2352       break;
2353       
2354     default:
2355       break;
2356     }
2357
2358   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2359   if (column == ctree->tree_column && type != GTK_CMCELL_EMPTY)
2360     type = GTK_CMCELL_PIXTEXT;
2361
2362   /* Note that pixbuf and mask were already ref'ed by the caller
2363    */
2364   switch (type)
2365     {
2366     case GTK_CMCELL_TEXT:
2367       if (text)
2368         {
2369           clist_row->cell[column].type = GTK_CMCELL_TEXT;
2370           GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2371         }
2372       break;
2373     case GTK_CMCELL_PIXBUF:
2374       if (pixbuf)
2375         {
2376           clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2377           GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2378         }
2379       break;
2380     case GTK_CMCELL_PIXTEXT:
2381       if (column == ctree->tree_column)
2382         {
2383           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2384           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2385           if (text)
2386             GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2387           else
2388             GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = NULL;
2389           if (pixbuf)
2390             {
2391               GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2392             }
2393           else
2394             {
2395               GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = NULL;
2396             }
2397         }
2398       else if (text && pixbuf)
2399         {
2400           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2401           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2402           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2403           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2404         }
2405       break;
2406     default:
2407       break;
2408     }
2409   
2410   if (visible && clist->column[column].auto_resize &&
2411       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2412     column_auto_resize (clist, clist_row, column, requisition.width);
2413
2414   g_free (old_text);
2415   if (old_pixbuf)
2416     g_object_unref (old_pixbuf);
2417 }
2418
2419 static void 
2420 set_node_info (GtkCMCTree     *ctree,
2421                GtkCMCTreeNode *node,
2422                const gchar  *text,
2423                guint8        spacing,
2424                GdkPixbuf    *pixbuf_closed,
2425                GdkPixbuf    *pixbuf_opened,
2426                gboolean      is_leaf,
2427                gboolean      expanded)
2428 {
2429   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
2430     {
2431       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
2432     }
2433   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2434     {
2435       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2436     }
2437
2438   GTK_CMCTREE_ROW (node)->pixbuf_opened = NULL;
2439   GTK_CMCTREE_ROW (node)->pixbuf_closed = NULL;
2440
2441   if (pixbuf_closed)
2442     {
2443       GTK_CMCTREE_ROW (node)->pixbuf_closed = g_object_ref (pixbuf_closed);
2444     }
2445   if (pixbuf_opened)
2446     {
2447       GTK_CMCTREE_ROW (node)->pixbuf_opened = g_object_ref (pixbuf_opened);
2448     }
2449
2450   GTK_CMCTREE_ROW (node)->is_leaf  = is_leaf;
2451   GTK_CMCTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2452
2453   if (GTK_CMCTREE_ROW (node)->expanded)
2454     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2455                                 text, spacing, pixbuf_opened);
2456   else 
2457     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2458                                 text, spacing, pixbuf_closed);
2459 }
2460
2461 static void
2462 tree_delete (GtkCMCTree     *ctree, 
2463              GtkCMCTreeNode *node, 
2464              gpointer      data)
2465 {
2466   tree_unselect (ctree,  node, NULL);
2467   row_delete (ctree, GTK_CMCTREE_ROW (node));
2468   g_list_free_1 ((GList *)node);
2469 }
2470
2471 static void
2472 tree_delete_row (GtkCMCTree     *ctree, 
2473                  GtkCMCTreeNode *node, 
2474                  gpointer      data)
2475 {
2476   row_delete (ctree, GTK_CMCTREE_ROW (node));
2477   g_list_free_1 ((GList *)node);
2478 }
2479
2480 static void
2481 tree_update_level (GtkCMCTree     *ctree, 
2482                    GtkCMCTreeNode *node, 
2483                    gpointer      data)
2484 {
2485   if (!node)
2486     return;
2487
2488   if (GTK_CMCTREE_ROW (node)->parent)
2489       GTK_CMCTREE_ROW (node)->level = 
2490         GTK_CMCTREE_ROW (GTK_CMCTREE_ROW (node)->parent)->level + 1;
2491   else
2492       GTK_CMCTREE_ROW (node)->level = 1;
2493 }
2494
2495 static void
2496 tree_select (GtkCMCTree     *ctree, 
2497              GtkCMCTreeNode *node, 
2498              gpointer      data)
2499 {
2500   if (node && GTK_CMCTREE_ROW (node)->row.state != GTK_STATE_SELECTED &&
2501       GTK_CMCTREE_ROW (node)->row.selectable)
2502     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_SELECT_ROW], 0,
2503                      node, -1);
2504 }
2505
2506 static void
2507 tree_unselect (GtkCMCTree     *ctree, 
2508                GtkCMCTreeNode *node, 
2509                gpointer      data)
2510 {
2511   if (node && GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED)
2512     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_UNSELECT_ROW], 0,
2513                      node, -1);
2514 }
2515
2516 static void
2517 tree_expand (GtkCMCTree     *ctree, 
2518              GtkCMCTreeNode *node, 
2519              gpointer      data)
2520 {
2521   if (node && !GTK_CMCTREE_ROW (node)->expanded)
2522     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0,node);
2523 }
2524
2525 static void
2526 tree_collapse (GtkCMCTree     *ctree, 
2527                GtkCMCTreeNode *node, 
2528                gpointer      data)
2529 {
2530   if (node && GTK_CMCTREE_ROW (node)->expanded)
2531     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0,node);
2532 }
2533
2534 static void
2535 tree_collapse_to_depth (GtkCMCTree     *ctree, 
2536                         GtkCMCTreeNode *node, 
2537                         gint          depth)
2538 {
2539   if (node && GTK_CMCTREE_ROW (node)->level == depth)
2540     gtk_cmctree_collapse_recursive (ctree, node);
2541 }
2542
2543 static void
2544 tree_toggle_expansion (GtkCMCTree     *ctree,
2545                        GtkCMCTreeNode *node,
2546                        gpointer      data)
2547 {
2548   if (!node)
2549     return;
2550
2551   if (GTK_CMCTREE_ROW (node)->expanded)
2552     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0,node);
2553   else
2554     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0,node);
2555 }
2556
2557 static GtkCMCTreeRow *
2558 row_new (GtkCMCTree *ctree)
2559 {
2560   GtkCMCList *clist;
2561   GtkCMCTreeRow *ctree_row;
2562   int i;
2563
2564   clist = GTK_CMCLIST (ctree);
2565 #if GLIB_CHECK_VERSION(2,10,0)
2566   ctree_row = g_slice_new (GtkCMCTreeRow);
2567   ctree_row->row.cell = g_slice_alloc (sizeof (GtkCMCell) * clist->columns);
2568 #else
2569   ctree_row = g_chunk_new (GtkCMCTreeRow, (GMemChunk *)clist->row_mem_chunk);
2570   ctree_row->row.cell = g_chunk_new (GtkCMCell, (GMemChunk *)clist->cell_mem_chunk);
2571 #endif
2572
2573   for (i = 0; i < clist->columns; i++)
2574     {
2575       ctree_row->row.cell[i].type = GTK_CMCELL_EMPTY;
2576       ctree_row->row.cell[i].vertical = 0;
2577       ctree_row->row.cell[i].horizontal = 0;
2578       ctree_row->row.cell[i].style = NULL;
2579     }
2580   GTK_CMCELL_PIXTEXT (ctree_row->row.cell[ctree->tree_column])->text = NULL;
2581
2582   ctree_row->row.fg_set     = FALSE;
2583   ctree_row->row.bg_set     = FALSE;
2584   ctree_row->row.style      = NULL;
2585   ctree_row->row.selectable = TRUE;
2586   ctree_row->row.state      = GTK_STATE_NORMAL;
2587   ctree_row->row.data       = NULL;
2588   ctree_row->row.destroy    = NULL;
2589
2590   ctree_row->level         = 0;
2591   ctree_row->expanded      = FALSE;
2592   ctree_row->parent        = NULL;
2593   ctree_row->sibling       = NULL;
2594   ctree_row->children      = NULL;
2595   ctree_row->pixbuf_closed = NULL;
2596   ctree_row->pixbuf_opened = NULL;
2597   
2598   return ctree_row;
2599 }
2600
2601 static void
2602 row_delete (GtkCMCTree    *ctree,
2603             GtkCMCTreeRow *ctree_row)
2604 {
2605   GtkCMCList *clist;
2606   gint i;
2607
2608   clist = GTK_CMCLIST (ctree);
2609
2610   for (i = 0; i < clist->columns; i++)
2611     {
2612       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2613         (clist, &(ctree_row->row), i, GTK_CMCELL_EMPTY, NULL, 0, NULL);
2614       if (ctree_row->row.cell[i].style)
2615         {
2616           if (gtk_widget_get_realized (GTK_WIDGET(ctree)))
2617             gtk_style_detach (ctree_row->row.cell[i].style);
2618           g_object_unref (ctree_row->row.cell[i].style);
2619         }
2620     }
2621
2622   if (ctree_row->row.style)
2623     {
2624       if (gtk_widget_get_realized (GTK_WIDGET(ctree)))
2625         gtk_style_detach (ctree_row->row.style);
2626       g_object_unref (ctree_row->row.style);
2627     }
2628
2629   if (ctree_row->pixbuf_closed)
2630     {
2631       g_object_unref (ctree_row->pixbuf_closed);
2632     }
2633
2634   if (ctree_row->pixbuf_opened)
2635     {
2636       g_object_unref (ctree_row->pixbuf_opened);
2637     }
2638
2639   if (ctree_row->row.destroy)
2640     {
2641       GDestroyNotify dnotify = ctree_row->row.destroy;
2642       gpointer ddata = ctree_row->row.data;
2643
2644       ctree_row->row.destroy = NULL;
2645       ctree_row->row.data = NULL;
2646
2647       dnotify (ddata);
2648     }
2649
2650 #if GLIB_CHECK_VERSION(2,10,0)  
2651   g_slice_free1 (sizeof (GtkCMCell) * clist->columns, ctree_row->row.cell);
2652   g_slice_free (GtkCMCTreeRow, ctree_row);
2653 #else
2654   g_mem_chunk_free ((GMemChunk *)clist->cell_mem_chunk, ctree_row->row.cell);
2655   g_mem_chunk_free ((GMemChunk *)clist->row_mem_chunk, ctree_row);
2656 #endif
2657 }
2658
2659 static void
2660 real_select_row (GtkCMCList *clist,
2661                  gint      row,
2662                  gint      column,
2663                  GdkEvent *event)
2664 {
2665   GList *node;
2666
2667   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2668   
2669   if ((node = g_list_nth (clist->row_list, row)) &&
2670       GTK_CMCTREE_ROW (node)->row.selectable)
2671     g_signal_emit (G_OBJECT (clist), ctree_signals[TREE_SELECT_ROW],0,
2672                      node, column);
2673 }
2674
2675 static void
2676 real_unselect_row (GtkCMCList *clist,
2677                    gint      row,
2678                    gint      column,
2679                    GdkEvent *event)
2680 {
2681   GList *node;
2682
2683   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2684
2685   if ((node = g_list_nth (clist->row_list, row)))
2686     g_signal_emit (G_OBJECT (clist), ctree_signals[TREE_UNSELECT_ROW],0,
2687                      node, column);
2688 }
2689
2690 static void
2691 tree_draw_node (GtkCMCTree     *ctree, 
2692                GtkCMCTreeNode *node)
2693 {
2694   GtkCMCList *clist;
2695   
2696   clist = GTK_CMCLIST (ctree);
2697
2698   if (CLIST_UNFROZEN (clist) && gtk_cmctree_is_viewable (ctree, node))
2699     {
2700       GtkCMCTreeNode *work;
2701       gint num = 0;
2702
2703       work = GTK_CMCTREE_NODE (clist->row_list);
2704       while (work && work != node)
2705         {
2706           work = GTK_CMCTREE_NODE_NEXT (work);
2707           num++;
2708         }
2709       if (work && gtk_cmclist_row_is_visible (clist, num) != GTK_VISIBILITY_NONE)
2710         GTK_CMCLIST_GET_CLASS(ctree)->draw_row
2711           (clist, NULL, num, GTK_CMCLIST_ROW ((GList *) node));
2712     }
2713 }
2714
2715 static void
2716 real_tree_select (GtkCMCTree     *ctree,
2717                   GtkCMCTreeNode *node,
2718                   gint          column)
2719 {
2720   GtkCMCList *clist;
2721   GList *list;
2722   GtkCMCTreeNode *sel_row;
2723   gboolean node_selected;
2724
2725   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2726
2727   if (!node || GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED ||
2728       !GTK_CMCTREE_ROW (node)->row.selectable)
2729     return;
2730
2731   clist = GTK_CMCLIST (ctree);
2732
2733   switch (clist->selection_mode)
2734     {
2735     case GTK_SELECTION_SINGLE:
2736     case GTK_SELECTION_BROWSE:
2737
2738       node_selected = FALSE;
2739       list = clist->selection;
2740
2741       while (list)
2742         {
2743           sel_row = list->data;
2744           list = list->next;
2745           
2746           if (node == sel_row)
2747             node_selected = TRUE;
2748           else
2749             g_signal_emit (G_OBJECT (ctree),
2750                              ctree_signals[TREE_UNSELECT_ROW], 0, sel_row, column);
2751         }
2752
2753       if (node_selected)
2754         return;
2755
2756     default:
2757       break;
2758     }
2759
2760   GTK_CMCTREE_ROW (node)->row.state = GTK_STATE_SELECTED;
2761
2762   if (!clist->selection)
2763     {
2764       clist->selection = g_list_append (clist->selection, node);
2765       clist->selection_end = clist->selection;
2766     }
2767   else
2768     clist->selection_end = g_list_append (clist->selection_end, node)->next;
2769
2770   tree_draw_node (ctree, node);
2771 }
2772
2773 static void
2774 real_tree_unselect (GtkCMCTree     *ctree,
2775                     GtkCMCTreeNode *node,
2776                     gint          column)
2777 {
2778   GtkCMCList *clist;
2779
2780   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2781
2782   if (!node || GTK_CMCTREE_ROW (node)->row.state != GTK_STATE_SELECTED)
2783     return;
2784
2785   clist = GTK_CMCLIST (ctree);
2786
2787   if (clist->selection_end && clist->selection_end->data == node)
2788     clist->selection_end = clist->selection_end->prev;
2789
2790   clist->selection = g_list_remove (clist->selection, node);
2791   
2792   GTK_CMCTREE_ROW (node)->row.state = GTK_STATE_NORMAL;
2793
2794   tree_draw_node (ctree, node);
2795 }
2796
2797 static void
2798 select_row_recursive (GtkCMCTree     *ctree, 
2799                       GtkCMCTreeNode *node, 
2800                       gpointer      data)
2801 {
2802   if (!node || GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED ||
2803       !GTK_CMCTREE_ROW (node)->row.selectable)
2804     return;
2805
2806   GTK_CMCLIST (ctree)->undo_unselection = 
2807     g_list_prepend (GTK_CMCLIST (ctree)->undo_unselection, node);
2808   gtk_cmctree_select (ctree, node);
2809 }
2810
2811 static void
2812 real_select_all (GtkCMCList *clist)
2813 {
2814   GtkCMCTree *ctree;
2815   GtkCMCTreeNode *node;
2816   
2817   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2818
2819   ctree = GTK_CMCTREE (clist);
2820
2821   switch (clist->selection_mode)
2822     {
2823     case GTK_SELECTION_SINGLE:
2824     case GTK_SELECTION_BROWSE:
2825       return;
2826
2827     case GTK_SELECTION_MULTIPLE:
2828
2829       gtk_cmclist_freeze (clist);
2830
2831       g_list_free (clist->undo_selection);
2832       g_list_free (clist->undo_unselection);
2833       clist->undo_selection = NULL;
2834       clist->undo_unselection = NULL;
2835           
2836       clist->anchor_state = GTK_STATE_SELECTED;
2837       clist->anchor = -1;
2838       clist->drag_pos = -1;
2839       clist->undo_anchor = clist->focus_row;
2840
2841       for (node = GTK_CMCTREE_NODE (clist->row_list); node;
2842            node = GTK_CMCTREE_NODE_NEXT (node))
2843         gtk_cmctree_pre_recursive (ctree, node, select_row_recursive, NULL);
2844
2845       gtk_cmclist_thaw (clist);
2846       break;
2847
2848     default:
2849       /* do nothing */
2850       break;
2851     }
2852 }
2853
2854 static void
2855 real_unselect_all (GtkCMCList *clist)
2856 {
2857   GtkCMCTree *ctree;
2858   GtkCMCTreeNode *node;
2859   GList *list;
2860  
2861   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2862   
2863   ctree = GTK_CMCTREE (clist);
2864
2865   switch (clist->selection_mode)
2866     {
2867     case GTK_SELECTION_BROWSE:
2868       if (clist->focus_row >= 0)
2869         {
2870           gtk_cmctree_select
2871             (ctree,
2872              GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row)));
2873           return;
2874         }
2875       break;
2876
2877     case GTK_SELECTION_MULTIPLE:
2878       g_list_free (clist->undo_selection);
2879       g_list_free (clist->undo_unselection);
2880       clist->undo_selection = NULL;
2881       clist->undo_unselection = NULL;
2882
2883       clist->anchor = -1;
2884       clist->drag_pos = -1;
2885       clist->undo_anchor = clist->focus_row;
2886       break;
2887
2888     default:
2889       break;
2890     }
2891
2892   list = clist->selection;
2893
2894   while (list)
2895     {
2896       node = list->data;
2897       list = list->next;
2898       gtk_cmctree_unselect (ctree, node);
2899     }
2900 }
2901
2902 static gboolean
2903 ctree_is_hot_spot (GtkCMCTree     *ctree, 
2904                    GtkCMCTreeNode *node,
2905                    gint          row, 
2906                    gint          x, 
2907                    gint          y)
2908 {
2909   GtkCMCTreeRow *tree_row;
2910   GtkCMCList *clist;
2911   gint xl;
2912   gint yu;
2913   
2914   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
2915   cm_return_val_if_fail (node != NULL, FALSE);
2916
2917   clist = GTK_CMCLIST (ctree);
2918
2919   if (!clist->column[ctree->tree_column].visible ||
2920       ctree->expander_style == GTK_CMCTREE_EXPANDER_NONE)
2921     return FALSE;
2922
2923   tree_row = GTK_CMCTREE_ROW (node);
2924
2925   yu = (ROW_TOP_YPIXEL (clist, row) + (clist->row_height - PM_SIZE) / 2 -
2926         (clist->row_height - 1) % 2);
2927
2928   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT)
2929     xl = (clist->column[ctree->tree_column].area.x + 
2930           clist->column[ctree->tree_column].area.width - 1 + clist->hoffset -
2931           (tree_row->level - 1) * ctree->tree_indent - PM_SIZE);
2932   else
2933     xl = (clist->column[ctree->tree_column].area.x + clist->hoffset +
2934           (tree_row->level - 1) * ctree->tree_indent);
2935
2936   return (x >= xl && x <= xl + PM_SIZE && y >= yu && y <= yu + PM_SIZE);
2937 }
2938
2939 /***********************************************************
2940  ***********************************************************
2941  ***                  Public interface                   ***
2942  ***********************************************************
2943  ***********************************************************/
2944
2945
2946 /***********************************************************
2947  *           Creation, insertion, deletion                 *
2948  ***********************************************************/
2949
2950 static GObject*
2951 gtk_cmctree_constructor (GType                  type,
2952                        guint                  n_construct_properties,
2953                        GObjectConstructParam *construct_properties)
2954 {
2955   GObject *object = G_OBJECT_CLASS (parent_class)->constructor (type,
2956                                                                 n_construct_properties,
2957                                                                 construct_properties);
2958
2959   return object;
2960 }
2961
2962 GtkWidget*
2963 gtk_cmctree_new_with_titles (gint         columns, 
2964                            gint         tree_column,
2965                            gchar       *titles[])
2966 {
2967   GtkWidget *widget;
2968
2969   cm_return_val_if_fail (columns > 0, NULL);
2970   cm_return_val_if_fail (tree_column >= 0 && tree_column < columns, NULL);
2971
2972   widget = gtk_widget_new (GTK_TYPE_CMCTREE,
2973                            "n_columns", columns,
2974                            "tree_column", tree_column,
2975                            NULL);
2976   if (titles)
2977     {
2978       GtkCMCList *clist = GTK_CMCLIST (widget);
2979       guint i;
2980
2981       for (i = 0; i < columns; i++)
2982         gtk_cmclist_set_column_title (clist, i, titles[i]);
2983       gtk_cmclist_column_titles_show (clist);
2984     }
2985
2986   return widget;
2987 }
2988
2989 GtkWidget *
2990 gtk_cmctree_new (gint columns, 
2991                gint tree_column)
2992 {
2993   return gtk_cmctree_new_with_titles (columns, tree_column, NULL);
2994 }
2995
2996 static gint
2997 real_insert_row (GtkCMCList *clist,
2998                  gint      row,
2999                  gchar    *text[])
3000 {
3001   GtkCMCTreeNode *parent = NULL;
3002   GtkCMCTreeNode *sibling;
3003   GtkCMCTreeNode *node;
3004
3005   cm_return_val_if_fail (GTK_IS_CMCTREE (clist), -1);
3006
3007   sibling = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
3008   if (sibling)
3009     parent = GTK_CMCTREE_ROW (sibling)->parent;
3010
3011   node = gtk_cmctree_insert_node (GTK_CMCTREE (clist), parent, sibling, text, 5,
3012                                 NULL, NULL, TRUE, FALSE);
3013
3014   if (GTK_CMCLIST_AUTO_SORT (clist) || !sibling)
3015     return g_list_position (clist->row_list, (GList *) node);
3016   
3017   return row;
3018 }
3019
3020 GtkCMCTreeNode * 
3021 gtk_cmctree_insert_node (GtkCMCTree     *ctree,
3022                        GtkCMCTreeNode *parent, 
3023                        GtkCMCTreeNode *sibling,
3024                        gchar        *text[],
3025                        guint8        spacing,
3026                        GdkPixbuf    *pixbuf_closed,
3027                        GdkPixbuf    *pixbuf_opened,
3028                        gboolean      is_leaf,
3029                        gboolean      expanded)
3030 {
3031   GtkCMCList *clist;
3032   GtkCMCTreeRow *new_row;
3033   GtkCMCTreeNode *node;
3034   GList *list;
3035   gint i;
3036
3037   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3038   if (sibling)
3039     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3040
3041   if (parent && GTK_CMCTREE_ROW (parent)->is_leaf)
3042     return NULL;
3043
3044   clist = GTK_CMCLIST (ctree);
3045
3046   /* create the row */
3047   new_row = row_new (ctree);
3048   list = g_list_alloc ();
3049   list->data = new_row;
3050   node = GTK_CMCTREE_NODE (list);
3051
3052   if (text)
3053     for (i = 0; i < clist->columns; i++)
3054       if (text[i] && i != ctree->tree_column)
3055         GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
3056           (clist, &(new_row->row), i, GTK_CMCELL_TEXT, text[i], 0, NULL);
3057
3058   set_node_info (ctree, node, text ?
3059                  text[ctree->tree_column] : NULL, spacing, pixbuf_closed,
3060                  pixbuf_opened, is_leaf, expanded);
3061
3062   /* sorted insertion */
3063   if (GTK_CMCLIST_AUTO_SORT (clist))
3064     {
3065       if (parent)
3066         sibling = GTK_CMCTREE_ROW (parent)->children;
3067       else
3068         sibling = GTK_CMCTREE_NODE (clist->row_list);
3069
3070       while (sibling && clist->compare
3071              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (sibling)) > 0)
3072         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3073     }
3074
3075   gtk_cmctree_link (ctree, node, parent, sibling, TRUE);
3076
3077   if (text && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
3078       gtk_cmctree_is_viewable (ctree, node))
3079     {
3080       for (i = 0; i < clist->columns; i++)
3081         if (clist->column[i].auto_resize)
3082           column_auto_resize (clist, &(new_row->row), i, 0);
3083     }
3084
3085   if (clist->rows == 1)
3086     {
3087       clist->focus_row = 0;
3088       if (clist->selection_mode == GTK_SELECTION_BROWSE)
3089         gtk_cmctree_select (ctree, node);
3090     }
3091
3092
3093   CLIST_REFRESH (clist);
3094
3095   return node;
3096 }
3097
3098 GtkCMCTreeNode *
3099 gtk_cmctree_insert_gnode (GtkCMCTree          *ctree,
3100                         GtkCMCTreeNode      *parent,
3101                         GtkCMCTreeNode      *sibling,
3102                         GNode             *gnode,
3103                         GtkCMCTreeGNodeFunc  func,
3104                         gpointer           data)
3105 {
3106   GtkCMCList *clist;
3107   GtkCMCTreeNode *cnode = NULL;
3108   GtkCMCTreeNode *child = NULL;
3109   GtkCMCTreeNode *new_child;
3110   GList *list;
3111   GNode *work;
3112   guint depth = 1;
3113
3114   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3115   cm_return_val_if_fail (gnode != NULL, NULL);
3116   cm_return_val_if_fail (func != NULL, NULL);
3117   if (sibling)
3118     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3119   
3120   clist = GTK_CMCLIST (ctree);
3121
3122   if (parent)
3123     depth = GTK_CMCTREE_ROW (parent)->level + 1;
3124
3125   list = g_list_alloc ();
3126   list->data = row_new (ctree);
3127   cnode = GTK_CMCTREE_NODE (list);
3128
3129   gtk_cmclist_freeze (clist);
3130
3131   set_node_info (ctree, cnode, "", 0, NULL, NULL, TRUE, FALSE);
3132
3133   if (!func (ctree, depth, gnode, cnode, data))
3134     {
3135       tree_delete_row (ctree, cnode, NULL);
3136       gtk_cmclist_thaw (clist);
3137       return NULL;
3138     }
3139
3140   if (GTK_CMCLIST_AUTO_SORT (clist))
3141     {
3142       if (parent)
3143         sibling = GTK_CMCTREE_ROW (parent)->children;
3144       else
3145         sibling = GTK_CMCTREE_NODE (clist->row_list);
3146
3147       while (sibling && clist->compare
3148              (clist, GTK_CMCTREE_ROW (cnode), GTK_CMCTREE_ROW (sibling)) > 0)
3149         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3150     }
3151
3152   gtk_cmctree_link (ctree, cnode, parent, sibling, TRUE);
3153
3154   for (work = g_node_last_child (gnode); work; work = work->prev)
3155     {
3156       new_child = gtk_cmctree_insert_gnode (ctree, cnode, child,
3157                                           work, func, data);
3158       if (new_child)
3159         child = new_child;
3160     }   
3161   
3162   gtk_cmclist_thaw (clist);
3163
3164   return cnode;
3165 }
3166
3167 GNode *
3168 gtk_cmctree_export_to_gnode (GtkCMCTree          *ctree,
3169                            GNode             *parent,
3170                            GNode             *sibling,
3171                            GtkCMCTreeNode      *node,
3172                            GtkCMCTreeGNodeFunc  func,
3173                            gpointer           data)
3174 {
3175   GtkCMCTreeNode *work;
3176   GNode *gnode;
3177   gint depth;
3178
3179   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3180   cm_return_val_if_fail (node != NULL, NULL);
3181   cm_return_val_if_fail (func != NULL, NULL);
3182   if (sibling)
3183     {
3184       cm_return_val_if_fail (parent != NULL, NULL);
3185       cm_return_val_if_fail (sibling->parent == parent, NULL);
3186     }
3187
3188   gnode = g_node_new (NULL);
3189   depth = g_node_depth (parent) + 1;
3190   
3191   if (!func (ctree, depth, gnode, node, data))
3192     {
3193       g_node_destroy (gnode);
3194       return NULL;
3195     }
3196
3197   if (parent)
3198     g_node_insert_before (parent, sibling, gnode);
3199
3200   if (!GTK_CMCTREE_ROW (node)->is_leaf)
3201     {
3202       GNode *new_sibling = NULL;
3203
3204       for (work = GTK_CMCTREE_ROW (node)->children; work;
3205            work = GTK_CMCTREE_ROW (work)->sibling)
3206         new_sibling = gtk_cmctree_export_to_gnode (ctree, gnode, new_sibling,
3207                                                  work, func, data);
3208
3209       g_node_reverse_children (gnode);
3210     }
3211
3212   return gnode;
3213 }
3214   
3215 static void
3216 real_remove_row (GtkCMCList *clist,
3217                  gint      row)
3218 {
3219   GtkCMCTreeNode *node;
3220
3221   cm_return_if_fail (GTK_IS_CMCTREE (clist));
3222
3223   node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
3224
3225   if (node)
3226     gtk_cmctree_remove_node (GTK_CMCTREE (clist), node);
3227 }
3228
3229 void
3230 gtk_cmctree_remove_node (GtkCMCTree     *ctree, 
3231                        GtkCMCTreeNode *node)
3232 {
3233   GtkCMCList *clist;
3234
3235   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3236
3237   clist = GTK_CMCLIST (ctree);
3238
3239   gtk_cmclist_freeze (clist);
3240
3241   if (node)
3242     {
3243       gtk_cmctree_unlink (ctree, node, TRUE);
3244       gtk_cmctree_post_recursive (ctree, node, GTK_CMCTREE_FUNC (tree_delete),
3245                                 NULL);
3246       if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
3247           clist->focus_row >= 0)
3248         gtk_cmclist_select_row (clist, clist->focus_row, -1);
3249
3250       auto_resize_columns (clist);
3251     }
3252   else
3253     gtk_cmclist_clear (clist);
3254
3255   gtk_cmclist_thaw (clist);
3256 }
3257
3258 static void
3259 real_clear (GtkCMCList *clist)
3260 {
3261   GtkCMCTree *ctree;
3262   GtkCMCTreeNode *work;
3263   GtkCMCTreeNode *ptr;
3264
3265   cm_return_if_fail (GTK_IS_CMCTREE (clist));
3266
3267   ctree = GTK_CMCTREE (clist);
3268
3269   /* remove all rows */
3270   work = GTK_CMCTREE_NODE (clist->row_list);
3271   clist->row_list = NULL;
3272   clist->row_list_end = NULL;
3273
3274   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3275   while (work)
3276     {
3277       ptr = work;
3278       work = GTK_CMCTREE_ROW (work)->sibling;
3279       gtk_cmctree_post_recursive (ctree, ptr, GTK_CMCTREE_FUNC (tree_delete_row), 
3280                                 NULL);
3281     }
3282   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3283
3284   parent_class->clear (clist);
3285 }
3286
3287
3288 /***********************************************************
3289  *  Generic recursive functions, querying / finding tree   *
3290  *  information                                            *
3291  ***********************************************************/
3292
3293
3294 void
3295 gtk_cmctree_post_recursive (GtkCMCTree     *ctree, 
3296                           GtkCMCTreeNode *node,
3297                           GtkCMCTreeFunc  func,
3298                           gpointer      data)
3299 {
3300   GtkCMCTreeNode *work;
3301   GtkCMCTreeNode *tmp;
3302
3303   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3304   cm_return_if_fail (func != NULL);
3305
3306   if (node)
3307     work = GTK_CMCTREE_ROW (node)->children;
3308   else
3309     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3310
3311   while (work)
3312     {
3313       tmp = GTK_CMCTREE_ROW (work)->sibling;
3314       gtk_cmctree_post_recursive (ctree, work, func, data);
3315       work = tmp;
3316     }
3317
3318   if (node)
3319     func (ctree, node, data);
3320 }
3321
3322 void
3323 gtk_cmctree_post_recursive_to_depth (GtkCMCTree     *ctree, 
3324                                    GtkCMCTreeNode *node,
3325                                    gint          depth,
3326                                    GtkCMCTreeFunc  func,
3327                                    gpointer      data)
3328 {
3329   GtkCMCTreeNode *work;
3330   GtkCMCTreeNode *tmp;
3331
3332   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3333   cm_return_if_fail (func != NULL);
3334
3335   if (depth < 0)
3336     {
3337       gtk_cmctree_post_recursive (ctree, node, func, data);
3338       return;
3339     }
3340
3341   if (node)
3342     work = GTK_CMCTREE_ROW (node)->children;
3343   else
3344     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3345
3346   if (work && GTK_CMCTREE_ROW (work)->level <= depth)
3347     {
3348       while (work)
3349         {
3350           tmp = GTK_CMCTREE_ROW (work)->sibling;
3351           gtk_cmctree_post_recursive_to_depth (ctree, work, depth, func, data);
3352           work = tmp;
3353         }
3354     }
3355
3356   if (node && GTK_CMCTREE_ROW (node)->level <= depth)
3357     func (ctree, node, data);
3358 }
3359
3360 void
3361 gtk_cmctree_pre_recursive (GtkCMCTree     *ctree, 
3362                          GtkCMCTreeNode *node,
3363                          GtkCMCTreeFunc  func,
3364                          gpointer      data)
3365 {
3366   GtkCMCTreeNode *work;
3367   GtkCMCTreeNode *tmp;
3368
3369   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3370   cm_return_if_fail (func != NULL);
3371
3372   if (node)
3373     {
3374       work = GTK_CMCTREE_ROW (node)->children;
3375       func (ctree, node, data);
3376     }
3377   else
3378     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3379
3380   while (work)
3381     {
3382       tmp = GTK_CMCTREE_ROW (work)->sibling;
3383       gtk_cmctree_pre_recursive (ctree, work, func, data);
3384       work = tmp;
3385     }
3386 }
3387
3388 void
3389 gtk_cmctree_pre_recursive_to_depth (GtkCMCTree     *ctree, 
3390                                   GtkCMCTreeNode *node,
3391                                   gint          depth, 
3392                                   GtkCMCTreeFunc  func,
3393                                   gpointer      data)
3394 {
3395   GtkCMCTreeNode *work;
3396   GtkCMCTreeNode *tmp;
3397
3398   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3399   cm_return_if_fail (func != NULL);
3400
3401   if (depth < 0)
3402     {
3403       gtk_cmctree_pre_recursive (ctree, node, func, data);
3404       return;
3405     }
3406
3407   if (node)
3408     {
3409       work = GTK_CMCTREE_ROW (node)->children;
3410       if (GTK_CMCTREE_ROW (node)->level <= depth)
3411         func (ctree, node, data);
3412     }
3413   else
3414     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3415
3416   if (work && GTK_CMCTREE_ROW (work)->level <= depth)
3417     {
3418       while (work)
3419         {
3420           tmp = GTK_CMCTREE_ROW (work)->sibling;
3421           gtk_cmctree_pre_recursive_to_depth (ctree, work, depth, func, data);
3422           work = tmp;
3423         }
3424     }
3425 }
3426
3427 gboolean
3428 gtk_cmctree_is_viewable (GtkCMCTree     *ctree, 
3429                        GtkCMCTreeNode *node)
3430
3431   GtkCMCTreeRow *work;
3432
3433   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3434   cm_return_val_if_fail (node != NULL, FALSE);
3435
3436   work = GTK_CMCTREE_ROW (node);
3437
3438   while (work && work->parent && GTK_CMCTREE_ROW (work->parent)->expanded)
3439     work = GTK_CMCTREE_ROW (work->parent);
3440
3441   if (!work->parent)
3442     return TRUE;
3443
3444   return FALSE;
3445 }
3446
3447 GtkCMCTreeNode * 
3448 gtk_cmctree_last (GtkCMCTree     *ctree,
3449                 GtkCMCTreeNode *node)
3450 {
3451   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3452
3453   if (!node) 
3454     return NULL;
3455
3456   while (GTK_CMCTREE_ROW (node)->sibling)
3457     node = GTK_CMCTREE_ROW (node)->sibling;
3458   
3459   if (GTK_CMCTREE_ROW (node)->children)
3460     return gtk_cmctree_last (ctree, GTK_CMCTREE_ROW (node)->children);
3461   
3462   return node;
3463 }
3464
3465 GtkCMCTreeNode *
3466 gtk_cmctree_find_node_ptr (GtkCMCTree    *ctree,
3467                          GtkCMCTreeRow *ctree_row)
3468 {
3469   GtkCMCTreeNode *node;
3470   
3471   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3472   cm_return_val_if_fail (ctree_row != NULL, NULL);
3473   
3474   if (ctree_row->parent)
3475     node = GTK_CMCTREE_ROW (ctree_row->parent)->children;
3476   else
3477     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3478
3479   while (GTK_CMCTREE_ROW (node) != ctree_row)
3480     node = GTK_CMCTREE_ROW (node)->sibling;
3481   
3482   return node;
3483 }
3484
3485 GtkCMCTreeNode *
3486 gtk_cmctree_node_nth (GtkCMCTree *ctree,
3487                     guint     row)
3488 {
3489   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3490
3491   if ((row >= GTK_CMCLIST(ctree)->rows))
3492     return NULL;
3493  
3494   return GTK_CMCTREE_NODE (g_list_nth (GTK_CMCLIST (ctree)->row_list, row));
3495 }
3496
3497 gboolean
3498 gtk_cmctree_find (GtkCMCTree     *ctree,
3499                 GtkCMCTreeNode *node,
3500                 GtkCMCTreeNode *child)
3501 {
3502   if (!child)
3503     return FALSE;
3504
3505   if (!node)
3506     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3507
3508   while (node)
3509     {
3510       if (node == child) 
3511         return TRUE;
3512       if (GTK_CMCTREE_ROW (node)->children)
3513         {
3514           if (gtk_cmctree_find (ctree, GTK_CMCTREE_ROW (node)->children, child))
3515             return TRUE;
3516         }
3517       node = GTK_CMCTREE_ROW (node)->sibling;
3518     }
3519   return FALSE;
3520 }
3521
3522 gboolean
3523 gtk_cmctree_is_ancestor (GtkCMCTree     *ctree,
3524                        GtkCMCTreeNode *node,
3525                        GtkCMCTreeNode *child)
3526 {
3527   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3528   cm_return_val_if_fail (node != NULL, FALSE);
3529
3530   if (GTK_CMCTREE_ROW (node)->children)
3531     return gtk_cmctree_find (ctree, GTK_CMCTREE_ROW (node)->children, child);
3532
3533   return FALSE;
3534 }
3535
3536 GtkCMCTreeNode *
3537 gtk_cmctree_find_by_row_data (GtkCMCTree     *ctree,
3538                             GtkCMCTreeNode *node,
3539                             gpointer      data)
3540 {
3541   GtkCMCTreeNode *work;
3542   
3543   if (!node)
3544     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3545   
3546   while (node)
3547     {
3548       if (GTK_CMCTREE_ROW (node)->row.data == data) 
3549         return node;
3550       if (GTK_CMCTREE_ROW (node)->children &&
3551           (work = gtk_cmctree_find_by_row_data 
3552            (ctree, GTK_CMCTREE_ROW (node)->children, data)))
3553         return work;
3554       node = GTK_CMCTREE_ROW (node)->sibling;
3555     }
3556   return NULL;
3557 }
3558
3559 GList *
3560 gtk_cmctree_find_all_by_row_data (GtkCMCTree     *ctree,
3561                                 GtkCMCTreeNode *node,
3562                                 gpointer      data)
3563 {
3564   GList *list = NULL;
3565
3566   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3567
3568   /* if node == NULL then look in the whole tree */
3569   if (!node)
3570     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3571
3572   while (node)
3573     {
3574       if (GTK_CMCTREE_ROW (node)->row.data == data)
3575         list = g_list_append (list, node);
3576
3577       if (GTK_CMCTREE_ROW (node)->children)
3578         {
3579           GList *sub_list;
3580
3581           sub_list = gtk_cmctree_find_all_by_row_data (ctree,
3582                                                      GTK_CMCTREE_ROW
3583                                                      (node)->children,
3584                                                      data);
3585           list = g_list_concat (list, sub_list);
3586         }
3587       node = GTK_CMCTREE_ROW (node)->sibling;
3588     }
3589   return list;
3590 }
3591
3592 GtkCMCTreeNode *
3593 gtk_cmctree_find_by_row_data_custom (GtkCMCTree     *ctree,
3594                                    GtkCMCTreeNode *node,
3595                                    gpointer      data,
3596                                    GCompareFunc  func)
3597 {
3598   GtkCMCTreeNode *work;
3599
3600   cm_return_val_if_fail (func != NULL, NULL);
3601
3602   if (!node)
3603     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3604
3605   while (node)
3606     {
3607       if (!func (GTK_CMCTREE_ROW (node)->row.data, data))
3608         return node;
3609       if (GTK_CMCTREE_ROW (node)->children &&
3610           (work = gtk_cmctree_find_by_row_data_custom
3611            (ctree, GTK_CMCTREE_ROW (node)->children, data, func)))
3612         return work;
3613       node = GTK_CMCTREE_ROW (node)->sibling;
3614     }
3615   return NULL;
3616 }
3617
3618 GList *
3619 gtk_cmctree_find_all_by_row_data_custom (GtkCMCTree     *ctree,
3620                                        GtkCMCTreeNode *node,
3621                                        gpointer      data,
3622                                        GCompareFunc  func)
3623 {
3624   GList *list = NULL;
3625
3626   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3627   cm_return_val_if_fail (func != NULL, NULL);
3628
3629   /* if node == NULL then look in the whole tree */
3630   if (!node)
3631     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3632
3633   while (node)
3634     {
3635       if (!func (GTK_CMCTREE_ROW (node)->row.data, data))