2011-11-23 [wwp] 3.7.10cvs100
[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     cairo_close_path(cr);
473   }
474   else
475   {
476     gint tmp3 = PM_SIZE / 2;
477     gint tmp6 = PM_SIZE / 6;
478     cairo_move_to(cr, x + tmp6 - justification_factor + (PM_SIZE / 2), y + tmp6 - 1);
479     cairo_rel_line_to(cr, justification_factor * tmp3, tmp3);
480     cairo_rel_line_to(cr, -justification_factor * tmp3, tmp3);
481   }
482   cairo_fill(cr);
483
484   x += justification_factor * (PM_SIZE + 3);
485
486   return x;
487 }
488
489 static gint
490 get_offset(GtkCMCTree     *ctree,
491                       GtkCMCTreeRow  *ctree_row,
492                       gint            column,
493                       GdkRectangle   *clip_rectangle)
494 {
495   gint justify_right;
496   justify_right = (GTK_CMCLIST (ctree)->column[column].justification == GTK_JUSTIFY_RIGHT);
497
498   if (justify_right)
499       return (clip_rectangle->x + clip_rectangle->width - 1 -
500                 ctree->tree_indent * (ctree_row->level - 1));
501
502   return clip_rectangle->x + ctree->tree_indent * (ctree_row->level - 1);
503 }
504
505  static void
506 get_cell_style (GtkCMCList     *clist,
507                 GtkCMCListRow  *clist_row,
508                 gint          state,
509                 gint          column,
510                 GtkStyle    **style)
511 {
512   GtkStyle *gtkstyle;
513
514   gtkstyle = gtk_widget_get_style (GTK_WIDGET (clist));
515
516   if (clist_row->cell[column].style)
517     {
518       if (style)
519         *style = clist_row->cell[column].style;
520     }
521   else if (clist_row->style)
522     {
523       if (style)
524         *style = clist_row->style;
525     }
526   else
527     {
528       if (style)
529         *style = gtkstyle;
530     }
531 }
532
533 static gboolean filter_fg (PangoAttribute *attribute, gpointer data)
534 {
535         const PangoAttrClass *klass = attribute->klass;
536         if (klass->type == PANGO_ATTR_FOREGROUND)
537                 return TRUE;
538
539         return FALSE;   
540 }
541
542 static PangoLayout *
543 create_cell_layout (GtkCMCList       *clist,
544                                GtkCMCListRow    *clist_row,
545                                gint            column)
546 {
547   PangoLayout *layout;
548   GtkStyle *style;
549   GtkCMCell *cell;
550   gchar *text;
551
552   get_cell_style (clist, clist_row, GTK_STATE_NORMAL, column, &style);
553
554
555   cell = &clist_row->cell[column];
556   switch (cell->type)
557     {
558     case GTK_CMCELL_TEXT:
559     case GTK_CMCELL_PIXTEXT:
560       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
561               GTK_CMCELL_PIXTEXT (*cell)->text :
562               GTK_CMCELL_TEXT (*cell)->text);
563
564       if (!text)
565         return NULL;
566       
567       if (!GTK_SCTREE(clist)->use_markup[column]) {
568               layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
569                                                        ((cell->type == GTK_CMCELL_PIXTEXT) ?
570                                                         GTK_CMCELL_PIXTEXT (*cell)->text :
571                                                         GTK_CMCELL_TEXT (*cell)->text));
572               pango_layout_set_font_description (layout, style->font_desc);
573       } else {
574               PangoContext *context = gtk_widget_get_pango_context (GTK_WIDGET(clist));
575               layout = pango_layout_new (context);
576               pango_layout_set_markup (layout, text, -1);
577               pango_layout_set_font_description (layout, style->font_desc);
578               if (clist_row->state == GTK_STATE_SELECTED) {
579                       /* for selected row, we should remove any forced foreground color
580                        * or it looks like shit */
581                       PangoAttrList *list = pango_layout_get_attributes(layout);
582                       PangoAttrList *rem = pango_attr_list_filter(list, filter_fg, NULL);
583                       if (rem)
584                               pango_attr_list_unref(rem);
585               }
586       }
587       
588       return layout;
589       
590     default:
591       return NULL;
592     }
593 }
594
595
596 static void
597 draw_row (GtkCMCList     *clist,
598           GdkRectangle *area,
599           gint          row,
600           GtkCMCListRow  *clist_row)
601 {
602   GtkWidget *widget;
603   GtkStyle *style;
604   GtkCMCTree  *ctree;
605   GdkRectangle *crect;
606   GdkRectangle row_rectangle;
607   GdkRectangle cell_rectangle; 
608   GdkRectangle clip_rectangle;
609   GdkRectangle intersect_rectangle;
610   gint last_column;
611   gint offset = 0;
612   gint state;
613   gint i;
614   static GdkColor greybg={0, 0, 0, 0};
615   static gboolean color_change = TRUE;
616   cairo_t *cr;
617   GdkColor *fgcolor, *bgcolor;
618
619   cm_return_if_fail (clist != NULL);
620   widget = GTK_WIDGET (clist);
621   style = clist_row->style ? clist_row->style : gtk_widget_get_style (widget);
622
623   if (greybg.pixel == 0 &&
624       greybg.red == 0 &&
625       greybg.green == 0 &&
626       greybg.blue == 0) {
627         GdkColor normalbg = {0, 0xffff, 0xffff, 0xffff};
628         if (style) {
629                 normalbg = style->base[GTK_STATE_NORMAL];
630         }
631         if (normalbg.red > 0x8888 && normalbg.green > 0x8888 && normalbg.blue > 0x8888) {
632                 greybg.pixel = normalbg.pixel;
633                 greybg.red = normalbg.red - prefs_common.stripes_color_offset;
634                 greybg.green = normalbg.green - prefs_common.stripes_color_offset;
635                 greybg.blue = normalbg.blue - prefs_common.stripes_color_offset;
636         } else if (normalbg.red < 0x8888 && normalbg.green < 0x8888 && normalbg.blue < 0x8888) {
637                 greybg.pixel = normalbg.pixel;
638                 greybg.red = normalbg.red + prefs_common.stripes_color_offset;
639                 greybg.green = normalbg.green + prefs_common.stripes_color_offset;
640                 greybg.blue = normalbg.blue + prefs_common.stripes_color_offset;
641         } else {
642                 color_change = FALSE;
643         }
644   }
645
646   /* bail now if we arn't drawable yet */
647   if (!gtk_widget_is_drawable (GTK_WIDGET(clist)) || row < 0 || row >= clist->rows)
648     return;
649
650   ctree  = GTK_CMCTREE  (clist);
651
652   /* if the function is passed the pointer to the row instead of null,
653    * it avoids this expensive lookup */
654   if (!clist_row)
655     clist_row = (g_list_nth (clist->row_list, row))->data;
656
657   /* rectangle of the entire row */
658   row_rectangle.x = 0;
659   row_rectangle.y = ROW_TOP_YPIXEL (clist, row);
660   row_rectangle.width = clist->clist_window_width;
661   row_rectangle.height = clist->row_height;
662
663   /* rectangle of the cell spacing above the row */
664   cell_rectangle.x = 0;
665   cell_rectangle.y = row_rectangle.y - CELL_SPACING;
666   cell_rectangle.width = row_rectangle.width;
667   cell_rectangle.height = CELL_SPACING;
668
669   /* rectangle used to clip drawing operations, its y and height
670    * positions only need to be set once, so we set them once here. 
671    * the x and width are set withing the drawing loop below once per
672    * column */
673   clip_rectangle.y = row_rectangle.y;
674   clip_rectangle.height = row_rectangle.height;
675
676   if (prefs_common.use_stripes_everywhere && GTK_SCTREE(ctree)->show_stripes
677       && color_change && row % 2) {
678     bgcolor = &greybg;
679   } else {
680     bgcolor = &style->base[GTK_STATE_NORMAL];
681   }
682   state = clist_row->state;
683
684   cr = gdk_cairo_create(clist->clist_window);
685   
686   if (clist_row->fg_set && state != GTK_STATE_SELECTED)
687         fgcolor = &clist_row->foreground;
688   else
689         fgcolor = &style->fg[clist_row->state];
690   /* draw the cell borders */
691   if (area)
692     {
693       crect = &intersect_rectangle;
694
695       if (gdk_rectangle_intersect (area, &cell_rectangle, crect)) {
696         gdk_cairo_rectangle(cr, &cell_rectangle);
697         gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
698         cairo_fill(cr);
699       }
700     }
701   else
702     {
703       crect = &cell_rectangle;
704
705       gdk_cairo_rectangle(cr, &cell_rectangle);
706       gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
707       cairo_fill(cr);
708     }
709
710   /* the last row has to clear its bottom cell spacing too */
711   if (clist_row == clist->row_list_end->data)
712     {
713       cell_rectangle.y += clist->row_height + CELL_SPACING;
714
715       if (!area || gdk_rectangle_intersect (area, &cell_rectangle, crect))
716         {
717           gdk_cairo_rectangle(cr, crect);
718           gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
719           cairo_fill(cr);
720         }
721     }     
722
723   for (last_column = clist->columns - 1;
724        last_column >= 0 && !clist->column[last_column].visible; last_column--)
725     ;
726
727   /* iterate and draw all the columns (row cells) and draw their contents */
728   for (i = 0; i < clist->columns; i++)
729     {
730       GtkStyle *style;
731       PangoLayout *layout = NULL;
732       PangoRectangle logical_rect;
733
734       gint width;
735       gint height;
736       gint pixbuf_width;
737       gint string_width;
738       gint old_offset;
739
740       if (!clist->column[i].visible)
741         continue;
742
743       get_cell_style (clist, clist_row, state, i, &style);
744
745       /* calculate clipping region */
746       clip_rectangle.x = clist->column[i].area.x + clist->hoffset;
747       clip_rectangle.width = clist->column[i].area.width;
748
749       cell_rectangle.x = clip_rectangle.x - COLUMN_INSET - CELL_SPACING;
750       cell_rectangle.width = (clip_rectangle.width + 2 * COLUMN_INSET +
751                               (1 + (i == last_column)) * CELL_SPACING);
752       cell_rectangle.y = clip_rectangle.y;
753       cell_rectangle.height = clip_rectangle.height;
754
755       string_width = 0;
756       pixbuf_width = 0;
757       height = 0;
758
759       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
760                                             &intersect_rectangle))
761         {
762           if (i != ctree->tree_column)
763             continue;
764         }
765       else
766         {
767           gdk_cairo_rectangle(cr, &cell_rectangle);
768           if (state == GTK_STATE_NORMAL)
769                 gdk_cairo_set_source_color(cr, bgcolor);
770           else
771                 gdk_cairo_set_source_color(cr, &style->base[state]);
772           cairo_fill(cr);
773
774           layout = create_cell_layout (clist, clist_row, i);
775           if (layout)
776             {
777               pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
778               width = logical_rect.width;
779             }
780           else
781             width = 0;
782
783           switch (clist_row->cell[i].type)
784             {
785             case GTK_CMCELL_PIXBUF:
786               pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
787               height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
788               width += pixbuf_width;
789               break;
790             case GTK_CMCELL_PIXTEXT:
791               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
792                 {
793                   pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
794                   height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
795                   width += pixbuf_width;
796                 }
797
798               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->text &&
799                   GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
800                 width +=  GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
801
802               if (i == ctree->tree_column)
803                 width += (ctree->tree_indent *
804                           ((GtkCMCTreeRow *)clist_row)->level);
805               break;
806             default:
807               break;
808             }
809
810           switch (clist->column[i].justification)
811             {
812             case GTK_JUSTIFY_LEFT:
813               offset = clip_rectangle.x + clist_row->cell[i].horizontal;
814               break;
815             case GTK_JUSTIFY_RIGHT:
816               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
817                         clip_rectangle.width - width);
818               break;
819             case GTK_JUSTIFY_CENTER:
820             case GTK_JUSTIFY_FILL:
821               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
822                         (clip_rectangle.width / 2) - (width / 2));
823               break;
824             };
825
826           if (i != ctree->tree_column)
827             {
828               int start_y = (clip_rectangle.height - height) / 2;
829               if (GTK_CMCLIST_ROW_HEIGHT_SET(GTK_CMCLIST(clist)))
830                       start_y = (clip_rectangle.height/2 - height) / 2;
831
832               offset += clist_row->cell[i].horizontal;
833               switch (clist_row->cell[i].type)
834                 {
835                 case GTK_CMCELL_PIXBUF:
836                   draw_cell_pixbuf
837                     (clist->clist_window, &clip_rectangle, cr,
838                      GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf,
839                      offset,
840                      clip_rectangle.y + clist_row->cell[i].vertical +
841                      start_y,
842                      pixbuf_width, height);
843                   break;
844                 case GTK_CMCELL_PIXTEXT:
845                   offset = draw_cell_pixbuf
846                     (clist->clist_window, &clip_rectangle, cr,
847                      GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
848                      offset,
849                      clip_rectangle.y + clist_row->cell[i].vertical +
850                      start_y,
851                      pixbuf_width, height);
852                   offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
853
854                   /* Fall through */
855                 case GTK_CMCELL_TEXT:
856                   if (layout)
857                     {
858                       gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
859                       gdk_cairo_set_source_color(cr, fgcolor);
860                       cairo_move_to(cr, offset, row_rectangle.y + row_center_offset + clist_row->cell[i].vertical);
861                       pango_cairo_show_layout(cr, layout);
862                       g_object_unref (G_OBJECT (layout));
863                     }
864                   break;
865                 default:
866                   break;
867                 }
868               continue;
869             }
870         }
871
872       /* draw ctree->tree_column */
873       cell_rectangle.y -= CELL_SPACING;
874       cell_rectangle.height += CELL_SPACING;
875
876       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
877                                             &intersect_rectangle))
878         {
879           if (layout)
880             g_object_unref (G_OBJECT (layout));
881           continue;
882         }
883
884
885       /* draw lines */
886       offset = get_offset (ctree, (GtkCMCTreeRow *)clist_row, i,
887                                       &clip_rectangle);
888
889       /* draw expander */
890       offset = draw_expander (ctree, (GtkCMCTreeRow *)clist_row,
891                                         style, &clip_rectangle, cr, offset);
892
893       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
894         offset -= ctree->tree_spacing;
895       else
896         offset += ctree->tree_spacing;
897
898       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
899         offset -= (pixbuf_width + clist_row->cell[i].horizontal);
900       else
901         offset += clist_row->cell[i].horizontal;
902
903       old_offset = offset;
904       offset = draw_cell_pixbuf (clist->clist_window, &clip_rectangle, cr,
905                                  GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
906                                  offset, 
907                                  clip_rectangle.y + clist_row->cell[i].vertical
908                                  + (clip_rectangle.height - height) / 2,
909                                  pixbuf_width, height);
910
911       if (layout)
912         {
913           gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
914           
915           if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
916             {
917               offset = (old_offset - string_width);
918               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
919                 offset -= GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
920             }
921           else
922             {
923               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
924                 offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
925             }
926           
927           cairo_move_to(cr, offset, row_rectangle.y + row_center_offset + clist_row->cell[i].vertical);
928           gdk_cairo_set_source_color(cr, fgcolor);
929           pango_cairo_show_layout(cr, layout);
930
931           g_object_unref (G_OBJECT (layout));
932         }
933     }
934    /* draw focus rectangle */
935   if (clist->focus_row == row &&
936       gtk_widget_get_can_focus (widget) && gtk_widget_has_focus (widget))
937     {
938       if (!area || gdk_rectangle_intersect (area, &row_rectangle,
939                                         &intersect_rectangle))
940         {
941             cairo_set_line_width(cr, 1.0);
942             cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
943             gdk_cairo_set_source_color(cr, &style->fg[GTK_STATE_NORMAL]);
944             cairo_rectangle(cr, row_rectangle.x, row_rectangle.y,
945                               row_rectangle.width + 1,
946                               row_rectangle.height);
947             cairo_stroke(cr);
948         }
949      }
950     cairo_destroy(cr);
951 }
952
953 static void
954 gtk_cmctree_class_init (GtkCMCTreeClass *klass)
955 {
956   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
957   GtkObjectClass *object_class;
958   GtkWidgetClass *widget_class;
959   GtkCMCListClass *clist_class;
960   GtkBindingSet *binding_set;
961
962   gobject_class->constructor = gtk_cmctree_constructor;
963
964   object_class = (GtkObjectClass *) klass;
965   widget_class = (GtkWidgetClass *) klass;
966   container_class = (GtkContainerClass *) klass;
967   clist_class = (GtkCMCListClass *) klass;
968
969   parent_class = g_type_class_peek (GTK_TYPE_CMCLIST);
970   container_class = g_type_class_peek (GTK_TYPE_CONTAINER);
971
972   gobject_class->set_property = gtk_cmctree_set_arg;
973   gobject_class->get_property = gtk_cmctree_get_arg;
974
975   widget_class->realize = gtk_cmctree_realize;
976   widget_class->unrealize = gtk_cmctree_unrealize;
977   widget_class->button_press_event = gtk_cmctree_button_press;
978
979   widget_class->drag_begin = gtk_cmctree_drag_begin;
980   widget_class->drag_motion = gtk_cmctree_drag_motion;
981   widget_class->drag_data_received = gtk_cmctree_drag_data_received;
982
983   clist_class->select_row = real_select_row;
984   clist_class->unselect_row = real_unselect_row;
985   clist_class->row_move = real_row_move;
986   clist_class->undo_selection = real_undo_selection;
987   clist_class->resync_selection = resync_selection;
988   clist_class->selection_find = selection_find;
989   clist_class->click_column = NULL;
990   clist_class->draw_row = draw_row;
991   clist_class->clear = real_clear;
992   clist_class->select_all = real_select_all;
993   clist_class->unselect_all = real_unselect_all;
994   clist_class->fake_unselect_all = fake_unselect_all;
995   clist_class->insert_row = real_insert_row;
996   clist_class->remove_row = real_remove_row;
997   clist_class->sort_list = real_sort_list;
998   clist_class->set_cell_contents = set_cell_contents;
999   clist_class->cell_size_request = cell_size_request;
1000
1001   klass->tree_select_row = real_tree_select;
1002   klass->tree_unselect_row = real_tree_unselect;
1003   klass->tree_expand = real_tree_expand;
1004   klass->tree_collapse = real_tree_collapse;
1005   klass->tree_move = real_tree_move;
1006   klass->change_focus_row_expansion = change_focus_row_expansion;
1007
1008   g_object_class_install_property (gobject_class,
1009                                 ARG_N_COLUMNS,
1010                                 g_param_spec_uint ("n-columns",
1011                                 "N-Columns",
1012                                 "N-Columns",
1013                                 1,
1014                                 G_MAXINT,
1015                                 1,
1016                                 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
1017   g_object_class_install_property (gobject_class,
1018                                 ARG_TREE_COLUMN,
1019                                 g_param_spec_uint ("tree-column",
1020                                 "tree-column",
1021                                 "tree-column",
1022                                 0,
1023                                 G_MAXINT,
1024                                 0,
1025                                 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
1026   g_object_class_install_property (gobject_class,
1027                                 ARG_INDENT,
1028                                 g_param_spec_uint ("indent",
1029                                 "indent",
1030                                 "indent",
1031                                 1,
1032                                 G_MAXINT,
1033                                 1,
1034                                 G_PARAM_READWRITE));
1035   g_object_class_install_property (gobject_class,
1036                                 ARG_SPACING,
1037                                 g_param_spec_uint ("spacing",
1038                                 "spacing",
1039                                 "spacing",
1040                                 1,
1041                                 G_MAXINT,
1042                                 1,
1043                                 G_PARAM_READWRITE));
1044   g_object_class_install_property (gobject_class,
1045                                 ARG_SHOW_STUB,
1046                                 g_param_spec_boolean ("show-stub",
1047                                 "show-stub",
1048                                 "show-stub",
1049                                 TRUE,
1050                                 G_PARAM_READWRITE));
1051   g_object_class_install_property (gobject_class,
1052                                 ARG_LINE_STYLE,
1053                                 g_param_spec_enum ("line-style",
1054                                 "line-style",
1055                                 "line-style",
1056                                 GTK_TYPE_CMCTREE_LINE_STYLE, 0,
1057                                 G_PARAM_READWRITE));
1058   g_object_class_install_property (gobject_class,
1059                                 ARG_EXPANDER_STYLE,
1060                                 g_param_spec_enum ("expander-style",
1061                                 "expander-style",
1062                                 "expander-style",
1063                                 GTK_TYPE_CMCTREE_EXPANDER_STYLE, 0,
1064                                 G_PARAM_READWRITE));
1065
1066   ctree_signals[TREE_SELECT_ROW] =
1067                 g_signal_new ("tree_select_row",
1068                               G_TYPE_FROM_CLASS (object_class),
1069                               G_SIGNAL_RUN_FIRST,
1070                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_select_row),
1071                               NULL, NULL,
1072                               claws_marshal_VOID__POINTER_INT,
1073                               G_TYPE_NONE, 2,
1074                               GTK_TYPE_CMCTREE_NODE,
1075                               G_TYPE_INT);
1076   ctree_signals[TREE_UNSELECT_ROW] =
1077                 g_signal_new ("tree_unselect_row",
1078                               G_TYPE_FROM_CLASS (object_class),
1079                               G_SIGNAL_RUN_FIRST,
1080                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_unselect_row),
1081                               NULL, NULL,
1082                               claws_marshal_VOID__POINTER_INT,
1083                               G_TYPE_NONE, 2,
1084                               GTK_TYPE_CMCTREE_NODE,
1085                               G_TYPE_INT);
1086   ctree_signals[TREE_EXPAND] =
1087                 g_signal_new ("tree_expand",
1088                               G_TYPE_FROM_CLASS (object_class),
1089                               G_SIGNAL_RUN_LAST,
1090                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_expand),
1091                               NULL, NULL,
1092                               claws_marshal_VOID__POINTER,
1093                               G_TYPE_NONE, 1,
1094                               GTK_TYPE_CMCTREE_NODE);
1095   ctree_signals[TREE_COLLAPSE] =
1096                 g_signal_new ("tree_collapse",
1097                               G_TYPE_FROM_CLASS (object_class),
1098                               G_SIGNAL_RUN_LAST,
1099                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_collapse),
1100                               NULL, NULL,
1101                               claws_marshal_VOID__POINTER,
1102                               G_TYPE_NONE, 1,
1103                               GTK_TYPE_CMCTREE_NODE);
1104   ctree_signals[TREE_MOVE] =
1105                 g_signal_new ("tree_move",
1106                               G_TYPE_FROM_CLASS (object_class),
1107                               G_SIGNAL_RUN_LAST,
1108                               G_STRUCT_OFFSET (GtkCMCTreeClass, tree_move),
1109                               NULL, NULL,
1110                               claws_marshal_VOID__POINTER_POINTER_POINTER,
1111                               G_TYPE_NONE, 3,
1112                               GTK_TYPE_CMCTREE_NODE,GTK_TYPE_CMCTREE_NODE,GTK_TYPE_CMCTREE_NODE);
1113   ctree_signals[CHANGE_FOCUS_ROW_EXPANSION] =
1114                 g_signal_new ("change_focus_row_expansion",
1115                               G_TYPE_FROM_CLASS (object_class),
1116                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
1117                               G_STRUCT_OFFSET (GtkCMCTreeClass, change_focus_row_expansion),
1118                               NULL, NULL,
1119                               claws_marshal_VOID__ENUM,
1120                               G_TYPE_NONE, 1, GTK_TYPE_CMCTREE_EXPANSION_TYPE);
1121
1122   binding_set = gtk_binding_set_by_class (klass);
1123   gtk_binding_entry_add_signal (binding_set,
1124                                 GDK_KEY_plus, 0,
1125                                 "change_focus_row_expansion", 1,
1126                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND);
1127   gtk_binding_entry_add_signal (binding_set,
1128                                 GDK_KEY_plus, GDK_CONTROL_MASK,
1129                                 "change_focus_row_expansion", 1,
1130                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE);
1131
1132   gtk_binding_entry_add_signal (binding_set,
1133                                 GDK_KEY_KP_Add, 0,
1134                                 "change_focus_row_expansion", 1,
1135                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND);
1136   gtk_binding_entry_add_signal (binding_set,
1137                                 GDK_KEY_KP_Add, GDK_CONTROL_MASK,
1138                                 "change_focus_row_expansion", 1,
1139                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE);
1140   
1141   gtk_binding_entry_add_signal (binding_set,
1142                                 GDK_KEY_minus, 0,
1143                                 "change_focus_row_expansion", 1,
1144                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_COLLAPSE);
1145   gtk_binding_entry_add_signal (binding_set,
1146                                 GDK_KEY_minus, GDK_CONTROL_MASK,
1147                                 "change_focus_row_expansion", 1,
1148                                 G_TYPE_ENUM,
1149                                 GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE);
1150   gtk_binding_entry_add_signal (binding_set,
1151                                 GDK_KEY_KP_Subtract, 0,
1152                                 "change_focus_row_expansion", 1,
1153                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_COLLAPSE);
1154   gtk_binding_entry_add_signal (binding_set,
1155                                 GDK_KEY_KP_Subtract, GDK_CONTROL_MASK,
1156                                 "change_focus_row_expansion", 1,
1157                                 G_TYPE_ENUM,
1158                                 GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE);
1159   gtk_binding_entry_add_signal (binding_set,
1160                                 GDK_KEY_equal, 0,
1161                                 "change_focus_row_expansion", 1,
1162                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1163   gtk_binding_entry_add_signal (binding_set,
1164                                 GDK_KEY_KP_Equal, 0,
1165                                 "change_focus_row_expansion", 1,
1166                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1167   gtk_binding_entry_add_signal (binding_set,
1168                                 GDK_KEY_KP_Multiply, 0,
1169                                 "change_focus_row_expansion", 1,
1170                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1171   gtk_binding_entry_add_signal (binding_set,
1172                                 GDK_KEY_asterisk, 0,
1173                                 "change_focus_row_expansion", 1,
1174                                 G_TYPE_ENUM, GTK_CMCTREE_EXPANSION_TOGGLE);
1175   gtk_binding_entry_add_signal (binding_set,
1176                                 GDK_KEY_KP_Multiply, GDK_CONTROL_MASK,
1177                                 "change_focus_row_expansion", 1,
1178                                 G_TYPE_ENUM,
1179                                 GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE);
1180   gtk_binding_entry_add_signal (binding_set,
1181                                 GDK_KEY_asterisk, GDK_CONTROL_MASK,
1182                                 "change_focus_row_expansion", 1,
1183                                 G_TYPE_ENUM,
1184                                 GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE);  
1185 }
1186
1187 static void
1188 gtk_cmctree_set_arg (GObject *object,
1189                                 guint      arg_id,
1190                                 const GValue *value,
1191                                 GParamSpec *spec)
1192 {
1193   GtkCMCTree *ctree;
1194   GtkCMCList *clist;
1195
1196   ctree = GTK_CMCTREE (object);
1197   clist = GTK_CMCLIST (ctree);
1198
1199   switch (arg_id)
1200     {
1201     case ARG_N_COLUMNS: /* construct-only arg, only set at construction time */
1202 #if !GLIB_CHECK_VERSION(2,10,0)
1203       cm_return_if_fail (clist->row_mem_chunk == NULL);
1204 #endif
1205       clist->columns = MAX (1, g_value_get_uint (value));
1206 #if !GLIB_CHECK_VERSION(2,10,0)
1207       clist->row_mem_chunk = g_mem_chunk_new ("ctree row mem chunk",
1208                                               sizeof (GtkCMCTreeRow),
1209                                               sizeof (GtkCMCTreeRow)
1210                                               * CLIST_OPTIMUM_SIZE,
1211                                               G_ALLOC_AND_FREE);
1212       clist->cell_mem_chunk = g_mem_chunk_new ("ctree cell mem chunk",
1213                                                sizeof (GtkCMCell) * clist->columns,
1214                                                sizeof (GtkCMCell) * clist->columns
1215                                                * CLIST_OPTIMUM_SIZE,
1216                                                G_ALLOC_AND_FREE);
1217 #endif
1218       ctree->tree_column = CLAMP (ctree->tree_column, 0, clist->columns);
1219       break;
1220     case ARG_TREE_COLUMN: /* construct-only arg, only set at construction time */
1221       ctree->tree_column = g_value_get_uint (value);
1222 #if !GLIB_CHECK_VERSION(2,10,0)
1223       if (clist->row_mem_chunk)
1224 #endif
1225         ctree->tree_column = CLAMP (ctree->tree_column, 0, clist->columns);
1226       break;
1227     case ARG_INDENT:
1228       gtk_cmctree_set_indent (ctree, g_value_get_uint (value));
1229       break;
1230     case ARG_SPACING:
1231       gtk_cmctree_set_spacing (ctree, g_value_get_uint (value));
1232       break;
1233     case ARG_SHOW_STUB:
1234       gtk_cmctree_set_show_stub (ctree, g_value_get_boolean (value));
1235       break;
1236     case ARG_LINE_STYLE:
1237       gtk_cmctree_set_line_style (ctree, g_value_get_enum (value));
1238       break;
1239     case ARG_EXPANDER_STYLE:
1240       gtk_cmctree_set_expander_style (ctree, g_value_get_enum (value));
1241       break;
1242     default:
1243       break;
1244     }
1245 }
1246
1247 static void
1248 gtk_cmctree_get_arg (GObject *object,
1249                                 guint      arg_id,
1250                                 GValue *value,
1251                                 GParamSpec *spec)
1252 {
1253   GtkCMCTree *ctree;
1254
1255   ctree = GTK_CMCTREE (object);
1256
1257   switch (arg_id)
1258     {
1259     case ARG_N_COLUMNS:
1260       g_value_set_uint(value, GTK_CMCLIST (ctree)->columns);
1261       break;
1262     case ARG_TREE_COLUMN:
1263       g_value_set_uint(value, ctree->tree_column);
1264       break;
1265     case ARG_INDENT:
1266       g_value_set_uint(value, ctree->tree_indent);
1267       break;
1268     case ARG_SPACING:
1269       g_value_set_uint(value, ctree->tree_spacing);
1270       break;
1271     case ARG_SHOW_STUB:
1272       g_value_set_boolean(value, ctree->show_stub);
1273       break;
1274     case ARG_LINE_STYLE:
1275       g_value_set_enum(value, ctree->line_style);
1276       break;
1277     case ARG_EXPANDER_STYLE:
1278       g_value_set_enum(value, ctree->expander_style);
1279       break;
1280     default:
1281       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, spec);
1282       break;
1283     }
1284 }
1285
1286 static void
1287 gtk_cmctree_init (GtkCMCTree *ctree)
1288 {
1289   GtkCMCList *clist;
1290
1291   GTK_CMCLIST_SET_FLAG (ctree, CMCLIST_DRAW_DRAG_RECT);
1292   GTK_CMCLIST_SET_FLAG (ctree, CMCLIST_DRAW_DRAG_LINE);
1293
1294   clist = GTK_CMCLIST (ctree);
1295
1296   ctree->tree_indent    = 20;
1297   ctree->tree_spacing   = 5;
1298   ctree->tree_column    = 0;
1299   ctree->line_style     = GTK_CMCTREE_LINES_NONE;
1300   ctree->expander_style = GTK_CMCTREE_EXPANDER_TRIANGLE;
1301   ctree->drag_compare   = NULL;
1302   ctree->show_stub      = TRUE;
1303
1304   clist->button_actions[0] |= GTK_CMBUTTON_EXPANDS;
1305 }
1306
1307 static void
1308 ctree_attach_styles (GtkCMCTree     *ctree,
1309                      GtkCMCTreeNode *node,
1310                      gpointer      data)
1311 {
1312   GtkCMCList *clist;
1313   gint i;
1314
1315   clist = GTK_CMCLIST (ctree);
1316
1317   if (GTK_CMCTREE_ROW (node)->row.style)
1318     GTK_CMCTREE_ROW (node)->row.style =
1319       gtk_style_attach (GTK_CMCTREE_ROW (node)->row.style, clist->clist_window);
1320
1321   if (GTK_CMCTREE_ROW (node)->row.fg_set || GTK_CMCTREE_ROW (node)->row.bg_set)
1322     {
1323       GdkColormap *colormap;
1324
1325       colormap = gtk_widget_get_colormap (GTK_WIDGET (ctree));
1326       if (GTK_CMCTREE_ROW (node)->row.fg_set)
1327         gdk_colormap_alloc_color (colormap, &(GTK_CMCTREE_ROW (node)->row.foreground), TRUE, TRUE);
1328       if (GTK_CMCTREE_ROW (node)->row.bg_set)
1329         gdk_colormap_alloc_color (colormap, &(GTK_CMCTREE_ROW (node)->row.background), TRUE, TRUE);
1330     }
1331
1332   for (i = 0; i < clist->columns; i++)
1333     if  (GTK_CMCTREE_ROW (node)->row.cell[i].style)
1334       GTK_CMCTREE_ROW (node)->row.cell[i].style =
1335         gtk_style_attach (GTK_CMCTREE_ROW (node)->row.cell[i].style,
1336                           clist->clist_window);
1337 }
1338
1339 static void
1340 ctree_detach_styles (GtkCMCTree     *ctree,
1341                      GtkCMCTreeNode *node,
1342                      gpointer      data)
1343 {
1344   GtkCMCList *clist;
1345   gint i;
1346
1347   clist = GTK_CMCLIST (ctree);
1348
1349   if (GTK_CMCTREE_ROW (node)->row.style)
1350     gtk_style_detach (GTK_CMCTREE_ROW (node)->row.style);
1351   for (i = 0; i < clist->columns; i++)
1352     if  (GTK_CMCTREE_ROW (node)->row.cell[i].style)
1353       gtk_style_detach (GTK_CMCTREE_ROW (node)->row.cell[i].style);
1354 }
1355
1356 static void
1357 gtk_cmctree_realize (GtkWidget *widget)
1358 {
1359   GtkCMCTree *ctree;
1360   GtkCMCList *clist;
1361   GtkCMCTreeNode *node;
1362   GtkCMCTreeNode *child;
1363   gint i;
1364
1365   cm_return_if_fail (GTK_IS_CMCTREE (widget));
1366
1367   GTK_WIDGET_CLASS (parent_class)->realize (widget);
1368
1369   ctree = GTK_CMCTREE (widget);
1370   clist = GTK_CMCLIST (widget);
1371
1372   node = GTK_CMCTREE_NODE (clist->row_list);
1373   for (i = 0; i < clist->rows; i++)
1374     {
1375       if (GTK_CMCTREE_ROW (node)->children && !GTK_CMCTREE_ROW (node)->expanded)
1376         for (child = GTK_CMCTREE_ROW (node)->children; child;
1377              child = GTK_CMCTREE_ROW (child)->sibling)
1378           gtk_cmctree_pre_recursive (ctree, child, ctree_attach_styles, NULL);
1379       node = GTK_CMCTREE_NODE_NEXT (node);
1380     }
1381 }
1382
1383 static void
1384 gtk_cmctree_unrealize (GtkWidget *widget)
1385 {
1386   GtkCMCTree *ctree;
1387   GtkCMCList *clist;
1388
1389   cm_return_if_fail (GTK_IS_CMCTREE (widget));
1390
1391   GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
1392
1393   ctree = GTK_CMCTREE (widget);
1394   clist = GTK_CMCLIST (widget);
1395
1396   if (gtk_widget_get_realized (widget))
1397     {
1398       GtkCMCTreeNode *node;
1399       GtkCMCTreeNode *child;
1400       gint i;
1401
1402       node = GTK_CMCTREE_NODE (clist->row_list);
1403       for (i = 0; i < clist->rows; i++)
1404         {
1405           if (GTK_CMCTREE_ROW (node)->children &&
1406               !GTK_CMCTREE_ROW (node)->expanded)
1407             for (child = GTK_CMCTREE_ROW (node)->children; child;
1408                  child = GTK_CMCTREE_ROW (child)->sibling)
1409               gtk_cmctree_pre_recursive(ctree, child, ctree_detach_styles, NULL);
1410           node = GTK_CMCTREE_NODE_NEXT (node);
1411         }
1412     }
1413 }
1414
1415 static gint
1416 gtk_cmctree_button_press (GtkWidget      *widget,
1417                         GdkEventButton *event)
1418 {
1419   GtkCMCTree *ctree;
1420   GtkCMCList *clist;
1421   gint button_actions;
1422
1423   cm_return_val_if_fail (GTK_IS_CMCTREE (widget), FALSE);
1424   cm_return_val_if_fail (event != NULL, FALSE);
1425
1426   ctree = GTK_CMCTREE (widget);
1427   clist = GTK_CMCLIST (widget);
1428
1429   button_actions = clist->button_actions[event->button - 1];
1430
1431   if (button_actions == GTK_CMBUTTON_IGNORED)
1432     return FALSE;
1433
1434   if (event->window == clist->clist_window)
1435     {
1436       GtkCMCTreeNode *work;
1437       gint x;
1438       gint y;
1439       gint row;
1440       gint column;
1441
1442       x = event->x;
1443       y = event->y;
1444
1445       if (!gtk_cmclist_get_selection_info (clist, x, y, &row, &column))
1446         return FALSE;
1447
1448       work = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
1449           
1450       if (button_actions & GTK_CMBUTTON_EXPANDS &&
1451           (GTK_CMCTREE_ROW (work)->children && !GTK_CMCTREE_ROW (work)->is_leaf  &&
1452            (event->type == GDK_2BUTTON_PRESS ||
1453             ctree_is_hot_spot (ctree, work, row, x, y))))
1454         {
1455           if (GTK_CMCTREE_ROW (work)->expanded)
1456             gtk_cmctree_collapse (ctree, work);
1457           else
1458             gtk_cmctree_expand (ctree, work);
1459
1460           return TRUE;
1461         }
1462     }
1463   
1464   return GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
1465 }
1466
1467 static gint
1468 gtk_cmctree_get_offset(GtkCMCTree     *ctree,
1469                       GtkCMCTreeRow  *ctree_row,
1470                       gint          column,
1471                       GdkRectangle *clip_rectangle)
1472 {
1473   gint justify_right = (GTK_CMCLIST (ctree)->column[column].justification == GTK_JUSTIFY_RIGHT);
1474
1475   if (justify_right)
1476       return (clip_rectangle->x + clip_rectangle->width - 1 -
1477                 ctree->tree_indent * (ctree_row->level - 1));
1478
1479   return clip_rectangle->x + ctree->tree_indent * (ctree_row->level - 1);
1480 }
1481
1482 static GtkCMCTreeNode *
1483 gtk_cmctree_last_visible (GtkCMCTree     *ctree,
1484                         GtkCMCTreeNode *node)
1485 {
1486   GtkCMCTreeNode *work;
1487   
1488   if (!node)
1489     return NULL;
1490
1491   work = GTK_CMCTREE_ROW (node)->children;
1492
1493   if (!work || !GTK_CMCTREE_ROW (node)->expanded)
1494     return node;
1495
1496   while (GTK_CMCTREE_ROW (work)->sibling)
1497     work = GTK_CMCTREE_ROW (work)->sibling;
1498
1499   return gtk_cmctree_last_visible (ctree, work);
1500 }
1501
1502 static void
1503 gtk_cmctree_link (GtkCMCTree     *ctree,
1504                 GtkCMCTreeNode *node,
1505                 GtkCMCTreeNode *parent,
1506                 GtkCMCTreeNode *sibling,
1507                 gboolean      update_focus_row)
1508 {
1509   GtkCMCList *clist;
1510   GList *list_end;
1511   GList *list;
1512   GList *work;
1513   gboolean visible = FALSE;
1514   gint rows = 0;
1515   
1516   if (sibling)
1517     cm_return_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent);
1518   cm_return_if_fail (node != NULL);
1519   cm_return_if_fail (node != sibling);
1520   cm_return_if_fail (node != parent);
1521
1522   clist = GTK_CMCLIST (ctree);
1523
1524   if (update_focus_row && clist->selection_mode == GTK_SELECTION_MULTIPLE)
1525     {
1526       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1527       
1528       g_list_free (clist->undo_selection);
1529       g_list_free (clist->undo_unselection);
1530       clist->undo_selection = NULL;
1531       clist->undo_unselection = NULL;
1532     }
1533
1534   for (rows = 1, list_end = (GList *)node; list_end->next;
1535        list_end = list_end->next)
1536     rows++;
1537
1538   GTK_CMCTREE_ROW (node)->parent = parent;
1539   GTK_CMCTREE_ROW (node)->sibling = sibling;
1540
1541   if (!parent || (parent && (gtk_cmctree_is_viewable (ctree, parent) &&
1542                              GTK_CMCTREE_ROW (parent)->expanded)))
1543     {
1544       visible = TRUE;
1545       clist->rows += rows;
1546     }
1547
1548   if (parent)
1549     work = (GList *)(GTK_CMCTREE_ROW (parent)->children);
1550   else
1551     work = clist->row_list;
1552
1553   if (sibling)
1554     {
1555       if (work != (GList *)sibling)
1556         {
1557           while (GTK_CMCTREE_ROW (work)->sibling != sibling)
1558             work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
1559           GTK_CMCTREE_ROW (work)->sibling = node;
1560         }
1561
1562       if (sibling == GTK_CMCTREE_NODE (clist->row_list))
1563         clist->row_list = (GList *) node;
1564       if (GTK_CMCTREE_NODE_PREV (sibling) &&
1565           GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (sibling)) == sibling)
1566         {
1567           list = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
1568           list->next = (GList *)node;
1569         }
1570       
1571       list = (GList *)node;
1572       list->prev = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
1573       list_end->next = (GList *)sibling;
1574       list = (GList *)sibling;
1575       list->prev = list_end;
1576       if (parent && GTK_CMCTREE_ROW (parent)->children == sibling)
1577         GTK_CMCTREE_ROW (parent)->children = node;
1578     }
1579   else
1580     {
1581       if (work)
1582         {
1583           /* find sibling */
1584           while (GTK_CMCTREE_ROW (work)->sibling)
1585             work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
1586           GTK_CMCTREE_ROW (work)->sibling = node;
1587           
1588           /* find last visible child of sibling */
1589           work = (GList *) gtk_cmctree_last_visible (ctree,
1590                                                    GTK_CMCTREE_NODE (work));
1591           
1592           list_end->next = work->next;
1593           if (work->next)
1594             list = work->next->prev = list_end;
1595           work->next = (GList *)node;
1596           list = (GList *)node;
1597           list->prev = work;
1598         }
1599       else
1600         {
1601           if (parent)
1602             {
1603               GTK_CMCTREE_ROW (parent)->children = node;
1604               list = (GList *)node;
1605               list->prev = (GList *)parent;
1606               if (GTK_CMCTREE_ROW (parent)->expanded)
1607                 {
1608                   list_end->next = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
1609                   if (GTK_CMCTREE_NODE_NEXT(parent))
1610                     {
1611                       list = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
1612                       list->prev = list_end;
1613                     }
1614                   list = (GList *)parent;
1615                   list->next = (GList *)node;
1616                 }
1617               else
1618                 list_end->next = NULL;
1619             }
1620           else
1621             {
1622               clist->row_list = (GList *)node;
1623               list = (GList *)node;
1624               list->prev = NULL;
1625               list_end->next = NULL;
1626             }
1627         }
1628     }
1629
1630   gtk_cmctree_pre_recursive (ctree, node, tree_update_level, NULL); 
1631
1632   if (clist->row_list_end == NULL ||
1633       clist->row_list_end->next == (GList *)node)
1634     clist->row_list_end = list_end;
1635
1636   if (visible && update_focus_row)
1637     {
1638       gint pos;
1639           
1640       pos = g_list_position (clist->row_list, (GList *)node);
1641   
1642       if (pos <= clist->focus_row)
1643         {
1644           clist->focus_row += rows;
1645           clist->undo_anchor = clist->focus_row;
1646         }
1647     }
1648 }
1649
1650 static void
1651 gtk_cmctree_unlink (GtkCMCTree     *ctree, 
1652                   GtkCMCTreeNode *node,
1653                   gboolean      update_focus_row)
1654 {
1655   GtkCMCList *clist;
1656   gint rows;
1657   gint level;
1658   gint visible;
1659   GtkCMCTreeNode *work;
1660   GtkCMCTreeNode *parent;
1661   GList *list;
1662
1663   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1664   cm_return_if_fail (node != NULL);
1665
1666   clist = GTK_CMCLIST (ctree);
1667   
1668   if (update_focus_row && clist->selection_mode == GTK_SELECTION_MULTIPLE)
1669     {
1670       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1671       
1672       g_list_free (clist->undo_selection);
1673       g_list_free (clist->undo_unselection);
1674       clist->undo_selection = NULL;
1675       clist->undo_unselection = NULL;
1676     }
1677
1678   visible = gtk_cmctree_is_viewable (ctree, node);
1679
1680   /* clist->row_list_end unlinked ? */
1681   if (visible &&
1682       (GTK_CMCTREE_NODE_NEXT (node) == NULL ||
1683        (GTK_CMCTREE_ROW (node)->children &&
1684         gtk_cmctree_is_ancestor (ctree, node,
1685                                GTK_CMCTREE_NODE (clist->row_list_end)))))
1686     clist->row_list_end = (GList *) (GTK_CMCTREE_NODE_PREV (node));
1687
1688   /* update list */
1689   rows = 0;
1690   level = GTK_CMCTREE_ROW (node)->level;
1691   work = GTK_CMCTREE_NODE_NEXT (node);
1692   while (work && GTK_CMCTREE_ROW (work)->level > level)
1693     {
1694       work = GTK_CMCTREE_NODE_NEXT (work);
1695       rows++;
1696     }
1697
1698   if (visible)
1699     {
1700       clist->rows -= (rows + 1);
1701
1702       if (update_focus_row)
1703         {
1704           gint pos;
1705           
1706           pos = g_list_position (clist->row_list, (GList *)node);
1707           if (pos + rows < clist->focus_row)
1708             clist->focus_row -= (rows + 1);
1709           else if (pos <= clist->focus_row)
1710             {
1711               if (!GTK_CMCTREE_ROW (node)->sibling)
1712                 clist->focus_row = MAX (pos - 1, 0);
1713               else
1714                 clist->focus_row = pos;
1715               
1716               clist->focus_row = MIN (clist->focus_row, clist->rows - 1);
1717             }
1718           clist->undo_anchor = clist->focus_row;
1719         }
1720     }
1721
1722   if (work)
1723     {
1724       list = (GList *)GTK_CMCTREE_NODE_PREV (work);
1725       list->next = NULL;
1726       list = (GList *)work;
1727       list->prev = (GList *)GTK_CMCTREE_NODE_PREV (node);
1728     }
1729
1730   if (GTK_CMCTREE_NODE_PREV (node) &&
1731       GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (node)) == node)
1732     {
1733       list = (GList *)GTK_CMCTREE_NODE_PREV (node);
1734       list->next = (GList *)work;
1735     }
1736
1737   /* update tree */
1738   parent = GTK_CMCTREE_ROW (node)->parent;
1739   if (parent)
1740     {
1741       if (GTK_CMCTREE_ROW (parent)->children == node)
1742         {
1743           GTK_CMCTREE_ROW (parent)->children = GTK_CMCTREE_ROW (node)->sibling;
1744           if (!GTK_CMCTREE_ROW (parent)->children)
1745             gtk_cmctree_collapse (ctree, parent);
1746         }
1747       else
1748         {
1749           GtkCMCTreeNode *sibling;
1750
1751           sibling = GTK_CMCTREE_ROW (parent)->children;
1752           while (GTK_CMCTREE_ROW (sibling)->sibling != node)
1753             sibling = GTK_CMCTREE_ROW (sibling)->sibling;
1754           GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
1755         }
1756     }
1757   else
1758     {
1759       if (clist->row_list == (GList *)node)
1760         clist->row_list = (GList *) (GTK_CMCTREE_ROW (node)->sibling);
1761       else
1762         {
1763           GtkCMCTreeNode *sibling;
1764
1765           sibling = GTK_CMCTREE_NODE (clist->row_list);
1766           while (GTK_CMCTREE_ROW (sibling)->sibling != node)
1767             sibling = GTK_CMCTREE_ROW (sibling)->sibling;
1768           GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
1769         }
1770     }
1771 }
1772
1773 static void
1774 real_row_move (GtkCMCList *clist,
1775                gint      source_row,
1776                gint      dest_row)
1777 {
1778   GtkCMCTree *ctree;
1779   GtkCMCTreeNode *node;
1780
1781   cm_return_if_fail (GTK_IS_CMCTREE (clist));
1782
1783   if (GTK_CMCLIST_AUTO_SORT (clist))
1784     return;
1785
1786   if (source_row < 0 || source_row >= clist->rows ||
1787       dest_row   < 0 || dest_row   >= clist->rows ||
1788       source_row == dest_row)
1789     return;
1790
1791   ctree = GTK_CMCTREE (clist);
1792   node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, source_row));
1793
1794   if (source_row < dest_row)
1795     {
1796       GtkCMCTreeNode *work; 
1797
1798       dest_row++;
1799       work = GTK_CMCTREE_ROW (node)->children;
1800
1801       while (work && GTK_CMCTREE_ROW (work)->level > GTK_CMCTREE_ROW (node)->level)
1802         {
1803           work = GTK_CMCTREE_NODE_NEXT (work);
1804           dest_row++;
1805         }
1806
1807       if (dest_row > clist->rows)
1808         dest_row = clist->rows;
1809     }
1810
1811   if (dest_row < clist->rows)
1812     {
1813       GtkCMCTreeNode *sibling;
1814
1815       sibling = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, dest_row));
1816       gtk_cmctree_move (ctree, node, GTK_CMCTREE_ROW (sibling)->parent, sibling);
1817     }
1818   else
1819     gtk_cmctree_move (ctree, node, NULL, NULL);
1820 }
1821
1822 static void
1823 real_tree_move (GtkCMCTree     *ctree,
1824                 GtkCMCTreeNode *node,
1825                 GtkCMCTreeNode *new_parent, 
1826                 GtkCMCTreeNode *new_sibling)
1827 {
1828   GtkCMCList *clist;
1829   GtkCMCTreeNode *work;
1830   gboolean visible = FALSE;
1831
1832   cm_return_if_fail (ctree != NULL);
1833   cm_return_if_fail (node != NULL);
1834   cm_return_if_fail (!new_sibling || 
1835                     GTK_CMCTREE_ROW (new_sibling)->parent == new_parent);
1836
1837   if (new_parent && GTK_CMCTREE_ROW (new_parent)->is_leaf)
1838     return;
1839
1840   /* new_parent != child of child */
1841   for (work = new_parent; work; work = GTK_CMCTREE_ROW (work)->parent)
1842     if (work == node)
1843       return;
1844
1845   clist = GTK_CMCLIST (ctree);
1846
1847   visible = gtk_cmctree_is_viewable (ctree, node);
1848
1849   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
1850     {
1851       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1852       
1853       g_list_free (clist->undo_selection);
1854       g_list_free (clist->undo_unselection);
1855       clist->undo_selection = NULL;
1856       clist->undo_unselection = NULL;
1857     }
1858
1859   if (GTK_CMCLIST_AUTO_SORT (clist))
1860     {
1861       if (new_parent == GTK_CMCTREE_ROW (node)->parent)
1862         return;
1863       
1864       if (new_parent)
1865         new_sibling = GTK_CMCTREE_ROW (new_parent)->children;
1866       else
1867         new_sibling = GTK_CMCTREE_NODE (clist->row_list);
1868
1869       while (new_sibling && clist->compare
1870              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (new_sibling)) > 0)
1871         new_sibling = GTK_CMCTREE_ROW (new_sibling)->sibling;
1872     }
1873
1874   if (new_parent == GTK_CMCTREE_ROW (node)->parent && 
1875       new_sibling == GTK_CMCTREE_ROW (node)->sibling)
1876     return;
1877
1878   gtk_cmclist_freeze (clist);
1879
1880   work = NULL;
1881   if (gtk_cmctree_is_viewable (ctree, node))
1882     work = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
1883       
1884   gtk_cmctree_unlink (ctree, node, FALSE);
1885   gtk_cmctree_link (ctree, node, new_parent, new_sibling, FALSE);
1886   
1887   if (work)
1888     {
1889       while (work &&  !gtk_cmctree_is_viewable (ctree, work))
1890         work = GTK_CMCTREE_ROW (work)->parent;
1891       clist->focus_row = g_list_position (clist->row_list, (GList *)work);
1892       clist->undo_anchor = clist->focus_row;
1893     }
1894
1895   if (clist->column[ctree->tree_column].auto_resize &&
1896       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
1897       (visible || gtk_cmctree_is_viewable (ctree, node)))
1898     gtk_cmclist_set_column_width
1899       (clist, ctree->tree_column,
1900        gtk_cmclist_optimal_column_width (clist, ctree->tree_column));
1901
1902   gtk_cmclist_thaw (clist);
1903 }
1904
1905 static void
1906 change_focus_row_expansion (GtkCMCTree          *ctree,
1907                             GtkCMCTreeExpansionType action)
1908 {
1909   GtkCMCList *clist;
1910   GtkCMCTreeNode *node;
1911
1912   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1913
1914   clist = GTK_CMCLIST (ctree);
1915
1916   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (ctree))) && 
1917       gtk_widget_has_grab (GTK_WIDGET(ctree)))
1918     return;
1919   
1920   if (!(node =
1921         GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row))) ||
1922       GTK_CMCTREE_ROW (node)->is_leaf || !(GTK_CMCTREE_ROW (node)->children))
1923     return;
1924
1925   switch (action)
1926     {
1927     case GTK_CMCTREE_EXPANSION_EXPAND:
1928       gtk_cmctree_expand (ctree, node);
1929       break;
1930     case GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE:
1931       gtk_cmctree_expand_recursive (ctree, node);
1932       break;
1933     case GTK_CMCTREE_EXPANSION_COLLAPSE:
1934       gtk_cmctree_collapse (ctree, node);
1935       break;
1936     case GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE:
1937       gtk_cmctree_collapse_recursive (ctree, node);
1938       break;
1939     case GTK_CMCTREE_EXPANSION_TOGGLE:
1940       gtk_cmctree_toggle_expansion (ctree, node);
1941       break;
1942     case GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE:
1943       gtk_cmctree_toggle_expansion_recursive (ctree, node);
1944       break;
1945     }
1946 }
1947
1948 static void 
1949 real_tree_expand (GtkCMCTree     *ctree,
1950                   GtkCMCTreeNode *node)
1951 {
1952   GtkCMCList *clist;
1953   GtkCMCTreeNode *work;
1954   GtkRequisition requisition;
1955   gboolean visible;
1956
1957   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1958
1959   if (!node || GTK_CMCTREE_ROW (node)->expanded || GTK_CMCTREE_ROW (node)->is_leaf)
1960     return;
1961
1962   clist = GTK_CMCLIST (ctree);
1963   
1964   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
1965
1966   GTK_CMCTREE_ROW (node)->expanded = TRUE;
1967
1968   visible = gtk_cmctree_is_viewable (ctree, node);
1969   /* get cell width if tree_column is auto resized */
1970   if (visible && clist->column[ctree->tree_column].auto_resize &&
1971       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1972     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1973       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
1974
1975   /* unref/unset closed pixbuf */
1976   if (GTK_CMCELL_PIXTEXT 
1977       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
1978     {
1979       g_object_unref
1980         (GTK_CMCELL_PIXTEXT
1981          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
1982       
1983       GTK_CMCELL_PIXTEXT
1984         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
1985     }
1986
1987   /* set/ref opened pixbuf */
1988   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
1989     {
1990       GTK_CMCELL_PIXTEXT 
1991         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
1992         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
1993     }
1994
1995
1996   work = GTK_CMCTREE_ROW (node)->children;
1997   if (work)
1998     {
1999       GList *list = (GList *)work;
2000       gint *cell_width = NULL;
2001       gint tmp = 0;
2002       gint row;
2003       gint i;
2004       
2005       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2006         {
2007           cell_width = g_new0 (gint, clist->columns);
2008           if (clist->column[ctree->tree_column].auto_resize)
2009               cell_width[ctree->tree_column] = requisition.width;
2010
2011           while (work)
2012             {
2013               /* search maximum cell widths of auto_resize columns */
2014               for (i = 0; i < clist->columns; i++)
2015                 if (clist->column[i].auto_resize)
2016                   {
2017                     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2018                       (clist, &GTK_CMCTREE_ROW (work)->row, i, &requisition);
2019                     cell_width[i] = MAX (requisition.width, cell_width[i]);
2020                   }
2021
2022               list = (GList *)work;
2023               work = GTK_CMCTREE_NODE_NEXT (work);
2024               tmp++;
2025             }
2026         }
2027       else
2028         while (work)
2029           {
2030             list = (GList *)work;
2031             work = GTK_CMCTREE_NODE_NEXT (work);
2032             tmp++;
2033           }
2034
2035       list->next = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2036
2037       if (GTK_CMCTREE_NODE_NEXT (node))
2038         {
2039           GList *tmp_list;
2040
2041           tmp_list = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2042           tmp_list->prev = list;
2043         }
2044       else
2045         clist->row_list_end = list;
2046
2047       list = (GList *)node;
2048       list->next = (GList *)(GTK_CMCTREE_ROW (node)->children);
2049
2050       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2051         {
2052           /* resize auto_resize columns if needed */
2053           for (i = 0; i < clist->columns; i++)
2054             if (clist->column[i].auto_resize &&
2055                 cell_width[i] > clist->column[i].width)
2056               gtk_cmclist_set_column_width (clist, i, cell_width[i]);
2057           g_free (cell_width);
2058
2059           /* update focus_row position */
2060           row = g_list_position (clist->row_list, (GList *)node);
2061           if (row < clist->focus_row)
2062             clist->focus_row += tmp;
2063
2064           clist->rows += tmp;
2065           CLIST_REFRESH (clist);
2066         }
2067     }
2068   else if (visible && clist->column[ctree->tree_column].auto_resize)
2069     /* resize tree_column if needed */
2070     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2071                         requisition.width);
2072 }
2073
2074 static void 
2075 real_tree_collapse (GtkCMCTree     *ctree,
2076                     GtkCMCTreeNode *node)
2077 {
2078   GtkCMCList *clist;
2079   GtkCMCTreeNode *work;
2080   GtkRequisition requisition;
2081   gboolean visible;
2082   gint level;
2083
2084   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2085
2086   if (!node || !GTK_CMCTREE_ROW (node)->expanded ||
2087       GTK_CMCTREE_ROW (node)->is_leaf)
2088     return;
2089
2090   clist = GTK_CMCLIST (ctree);
2091
2092   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2093   
2094   GTK_CMCTREE_ROW (node)->expanded = FALSE;
2095   level = GTK_CMCTREE_ROW (node)->level;
2096
2097   visible = gtk_cmctree_is_viewable (ctree, node);
2098   /* get cell width if tree_column is auto resized */
2099   if (visible && clist->column[ctree->tree_column].auto_resize &&
2100       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2101     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2102       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
2103
2104   /* unref/unset opened pixbuf */
2105   if (GTK_CMCELL_PIXTEXT 
2106       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
2107     {
2108       g_object_unref
2109         (GTK_CMCELL_PIXTEXT
2110          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
2111       
2112       GTK_CMCELL_PIXTEXT
2113         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
2114     }
2115
2116   /* set/ref closed pixbuf */
2117   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2118     {
2119       GTK_CMCELL_PIXTEXT 
2120         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
2121         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2122     }
2123
2124   work = GTK_CMCTREE_ROW (node)->children;
2125   if (work)
2126     {
2127       gint tmp = 0;
2128       gint row;
2129       GList *list;
2130
2131       while (work && GTK_CMCTREE_ROW (work)->level > level)
2132         {
2133           work = GTK_CMCTREE_NODE_NEXT (work);
2134           tmp++;
2135         }
2136
2137       if (work)
2138         {
2139           list = (GList *)node;
2140           list->next = (GList *)work;
2141           list = (GList *)GTK_CMCTREE_NODE_PREV (work);
2142           list->next = NULL;
2143           list = (GList *)work;
2144           list->prev = (GList *)node;
2145         }
2146       else
2147         {
2148           list = (GList *)node;
2149           list->next = NULL;
2150           clist->row_list_end = (GList *)node;
2151         }
2152
2153       if (visible)
2154         {
2155           /* resize auto_resize columns if needed */
2156           auto_resize_columns (clist);
2157
2158           row = g_list_position (clist->row_list, (GList *)node);
2159           if (row < clist->focus_row)
2160             clist->focus_row -= tmp;
2161           clist->rows -= tmp;
2162           CLIST_REFRESH (clist);
2163         }
2164     }
2165   else if (visible && clist->column[ctree->tree_column].auto_resize &&
2166            !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2167     /* resize tree_column if needed */
2168     column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2169                         requisition.width);
2170     
2171 }
2172
2173 static void
2174 column_auto_resize (GtkCMCList    *clist,
2175                     GtkCMCListRow *clist_row,
2176                     gint         column,
2177                     gint         old_width)
2178 {
2179   /* resize column if needed for auto_resize */
2180   GtkRequisition requisition;
2181
2182   if (!clist->column[column].auto_resize ||
2183       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2184     return;
2185
2186   if (clist_row)
2187     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2188                                                    column, &requisition);
2189   else
2190     requisition.width = 0;
2191
2192   if (requisition.width > clist->column[column].width)
2193     gtk_cmclist_set_column_width (clist, column, requisition.width);
2194   else if (requisition.width < old_width &&
2195            old_width == clist->column[column].width)
2196     {
2197       GList *list;
2198       gint new_width;
2199
2200       /* run a "gtk_cmclist_optimal_column_width" but break, if
2201        * the column doesn't shrink */
2202       if (GTK_CMCLIST_SHOW_TITLES (clist) && clist->column[column].button)
2203         {
2204         GtkRequisition req;
2205         gtk_widget_get_requisition (clist->column[column].button, &req);
2206         new_width = (req.width -
2207                      (CELL_SPACING + (2 * COLUMN_INSET)));
2208         }
2209       else
2210         new_width = 0;
2211
2212       for (list = clist->row_list; list; list = list->next)
2213         {
2214           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2215             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
2216           new_width = MAX (new_width, requisition.width);
2217           if (new_width == clist->column[column].width)
2218             break;
2219         }
2220       if (new_width < clist->column[column].width)
2221         gtk_cmclist_set_column_width (clist, column, new_width);
2222     }
2223 }
2224
2225 static void
2226 auto_resize_columns (GtkCMCList *clist)
2227 {
2228   gint i;
2229
2230   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2231     return;
2232
2233   for (i = 0; i < clist->columns; i++)
2234     column_auto_resize (clist, NULL, i, clist->column[i].width);
2235 }
2236
2237 static void
2238 cell_size_request (GtkCMCList       *clist,
2239                    GtkCMCListRow    *clist_row,
2240                    gint            column,
2241                    GtkRequisition *requisition)
2242 {
2243   GtkCMCTree *ctree;
2244   gint width;
2245   gint height;
2246   PangoLayout *layout;
2247   PangoRectangle logical_rect;
2248
2249   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2250   cm_return_if_fail (requisition != NULL);
2251
2252   ctree = GTK_CMCTREE (clist);
2253
2254   layout = create_cell_layout (clist, clist_row, column);
2255   if (layout)
2256     {
2257       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2258
2259       requisition->width = logical_rect.width;
2260       requisition->height = logical_rect.height;
2261       
2262       g_object_unref (G_OBJECT (layout));
2263     }
2264   else
2265     {
2266       requisition->width  = 0;
2267       requisition->height = 0;
2268     }
2269
2270   switch (clist_row->cell[column].type)
2271     {
2272     case GTK_CMCELL_PIXTEXT:
2273       if (GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf)
2274         {
2275           width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2276           height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2277           width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2278         }
2279       else
2280         width = height = 0;
2281           
2282       requisition->width += width;
2283       requisition->height = MAX (requisition->height, height);
2284       
2285       if (column == ctree->tree_column)
2286         {
2287           requisition->width += (ctree->tree_spacing + ctree->tree_indent *
2288                                  (((GtkCMCTreeRow *) clist_row)->level - 1));
2289           switch (ctree->expander_style)
2290             {
2291             case GTK_CMCTREE_EXPANDER_NONE:
2292               break;
2293             case GTK_CMCTREE_EXPANDER_TRIANGLE:
2294               requisition->width += PM_SIZE + 3;
2295               break;
2296             }
2297         }
2298       break;
2299     case GTK_CMCELL_PIXBUF:
2300       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2301       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2302       requisition->width += width;
2303       requisition->height = MAX (requisition->height, height);
2304       break;
2305     default:
2306       break;
2307     }
2308
2309   requisition->width  += clist_row->cell[column].horizontal;
2310   requisition->height += clist_row->cell[column].vertical;
2311 }
2312
2313 static void
2314 set_cell_contents (GtkCMCList    *clist,
2315                    GtkCMCListRow *clist_row,
2316                    gint         column,
2317                    GtkCMCellType  type,
2318                    const gchar *text,
2319                    guint8       spacing,
2320                    GdkPixbuf   *pixbuf)
2321 {
2322   gboolean visible = FALSE;
2323   GtkCMCTree *ctree;
2324   GtkRequisition requisition;
2325   gchar *old_text = NULL;
2326   GdkPixbuf *old_pixbuf = NULL;
2327
2328   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2329   cm_return_if_fail (clist_row != NULL);
2330
2331   ctree = GTK_CMCTREE (clist);
2332
2333   if (clist->column[column].auto_resize &&
2334       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2335     {
2336       GtkCMCTreeNode *parent;
2337
2338       parent = ((GtkCMCTreeRow *)clist_row)->parent;
2339       if ((parent && GTK_CMCTREE_ROW (parent)->expanded &&
2340                       gtk_cmctree_is_viewable (ctree, parent)))
2341         {
2342           visible = TRUE;
2343           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2344                                                          column, &requisition);
2345         }
2346     }
2347
2348   switch (clist_row->cell[column].type)
2349     {
2350     case GTK_CMCELL_EMPTY:
2351       break;
2352     case GTK_CMCELL_TEXT:
2353       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2354       break;
2355     case GTK_CMCELL_PIXBUF:
2356       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2357       break;
2358     case GTK_CMCELL_PIXTEXT:
2359       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2360       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2361       break;
2362     case GTK_CMCELL_WIDGET:
2363       /* unimplemented */
2364       break;
2365       
2366     default:
2367       break;
2368     }
2369
2370   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2371   if (column == ctree->tree_column && type != GTK_CMCELL_EMPTY)
2372     type = GTK_CMCELL_PIXTEXT;
2373
2374   /* Note that pixbuf and mask were already ref'ed by the caller
2375    */
2376   switch (type)
2377     {
2378     case GTK_CMCELL_TEXT:
2379       if (text)
2380         {
2381           clist_row->cell[column].type = GTK_CMCELL_TEXT;
2382           GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2383         }
2384       break;
2385     case GTK_CMCELL_PIXBUF:
2386       if (pixbuf)
2387         {
2388           clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2389           GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2390         }
2391       break;
2392     case GTK_CMCELL_PIXTEXT:
2393       if (column == ctree->tree_column)
2394         {
2395           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2396           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2397           if (text)
2398             GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2399           else
2400             GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = NULL;
2401           if (pixbuf)
2402             {
2403               GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2404             }
2405           else
2406             {
2407               GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = NULL;
2408             }
2409         }
2410       else if (text && pixbuf)
2411         {
2412           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2413           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2414           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2415           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2416         }
2417       break;
2418     default:
2419       break;
2420     }
2421   
2422   if (visible && clist->column[column].auto_resize &&
2423       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2424     column_auto_resize (clist, clist_row, column, requisition.width);
2425
2426   g_free (old_text);
2427   if (old_pixbuf)
2428     g_object_unref (old_pixbuf);
2429 }
2430
2431 static void 
2432 set_node_info (GtkCMCTree     *ctree,
2433                GtkCMCTreeNode *node,
2434                const gchar  *text,
2435                guint8        spacing,
2436                GdkPixbuf    *pixbuf_closed,
2437                GdkPixbuf    *pixbuf_opened,
2438                gboolean      is_leaf,
2439                gboolean      expanded)
2440 {
2441   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
2442     {
2443       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
2444     }
2445   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2446     {
2447       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2448     }
2449
2450   GTK_CMCTREE_ROW (node)->pixbuf_opened = NULL;
2451   GTK_CMCTREE_ROW (node)->pixbuf_closed = NULL;
2452
2453   if (pixbuf_closed)
2454     {
2455       GTK_CMCTREE_ROW (node)->pixbuf_closed = g_object_ref (pixbuf_closed);
2456     }
2457   if (pixbuf_opened)
2458     {
2459       GTK_CMCTREE_ROW (node)->pixbuf_opened = g_object_ref (pixbuf_opened);
2460     }
2461
2462   GTK_CMCTREE_ROW (node)->is_leaf  = is_leaf;
2463   GTK_CMCTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2464
2465   if (GTK_CMCTREE_ROW (node)->expanded)
2466     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2467                                 text, spacing, pixbuf_opened);
2468   else 
2469     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2470                                 text, spacing, pixbuf_closed);
2471 }
2472
2473 static void
2474 tree_delete (GtkCMCTree     *ctree, 
2475              GtkCMCTreeNode *node, 
2476              gpointer      data)
2477 {
2478   tree_unselect (ctree,  node, NULL);
2479   row_delete (ctree, GTK_CMCTREE_ROW (node));
2480   g_list_free_1 ((GList *)node);
2481 }
2482
2483 static void
2484 tree_delete_row (GtkCMCTree     *ctree, 
2485                  GtkCMCTreeNode *node, 
2486                  gpointer      data)
2487 {
2488   row_delete (ctree, GTK_CMCTREE_ROW (node));
2489   g_list_free_1 ((GList *)node);
2490 }
2491
2492 static void
2493 tree_update_level (GtkCMCTree     *ctree, 
2494                    GtkCMCTreeNode *node, 
2495                    gpointer      data)
2496 {
2497   if (!node)
2498     return;
2499
2500   if (GTK_CMCTREE_ROW (node)->parent)
2501       GTK_CMCTREE_ROW (node)->level = 
2502         GTK_CMCTREE_ROW (GTK_CMCTREE_ROW (node)->parent)->level + 1;
2503   else
2504       GTK_CMCTREE_ROW (node)->level = 1;
2505 }
2506
2507 static void
2508 tree_select (GtkCMCTree     *ctree, 
2509              GtkCMCTreeNode *node, 
2510              gpointer      data)
2511 {
2512   if (node && GTK_CMCTREE_ROW (node)->row.state != GTK_STATE_SELECTED &&
2513       GTK_CMCTREE_ROW (node)->row.selectable)
2514     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_SELECT_ROW], 0,
2515                      node, -1);
2516 }
2517
2518 static void
2519 tree_unselect (GtkCMCTree     *ctree, 
2520                GtkCMCTreeNode *node, 
2521                gpointer      data)
2522 {
2523   if (node && GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED)
2524     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_UNSELECT_ROW], 0,
2525                      node, -1);
2526 }
2527
2528 static void
2529 tree_expand (GtkCMCTree     *ctree, 
2530              GtkCMCTreeNode *node, 
2531              gpointer      data)
2532 {
2533   if (node && !GTK_CMCTREE_ROW (node)->expanded)
2534     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0,node);
2535 }
2536
2537 static void
2538 tree_collapse (GtkCMCTree     *ctree, 
2539                GtkCMCTreeNode *node, 
2540                gpointer      data)
2541 {
2542   if (node && GTK_CMCTREE_ROW (node)->expanded)
2543     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0,node);
2544 }
2545
2546 static void
2547 tree_collapse_to_depth (GtkCMCTree     *ctree, 
2548                         GtkCMCTreeNode *node, 
2549                         gint          depth)
2550 {
2551   if (node && GTK_CMCTREE_ROW (node)->level == depth)
2552     gtk_cmctree_collapse_recursive (ctree, node);
2553 }
2554
2555 static void
2556 tree_toggle_expansion (GtkCMCTree     *ctree,
2557                        GtkCMCTreeNode *node,
2558                        gpointer      data)
2559 {
2560   if (!node)
2561     return;
2562
2563   if (GTK_CMCTREE_ROW (node)->expanded)
2564     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_COLLAPSE], 0,node);
2565   else
2566     g_signal_emit (G_OBJECT (ctree), ctree_signals[TREE_EXPAND], 0,node);
2567 }
2568
2569 static GtkCMCTreeRow *
2570 row_new (GtkCMCTree *ctree)
2571 {
2572   GtkCMCList *clist;
2573   GtkCMCTreeRow *ctree_row;
2574   int i;
2575
2576   clist = GTK_CMCLIST (ctree);
2577 #if GLIB_CHECK_VERSION(2,10,0)
2578   ctree_row = g_slice_new (GtkCMCTreeRow);
2579   ctree_row->row.cell = g_slice_alloc (sizeof (GtkCMCell) * clist->columns);
2580 #else
2581   ctree_row = g_chunk_new (GtkCMCTreeRow, (GMemChunk *)clist->row_mem_chunk);
2582   ctree_row->row.cell = g_chunk_new (GtkCMCell, (GMemChunk *)clist->cell_mem_chunk);
2583 #endif
2584
2585   for (i = 0; i < clist->columns; i++)
2586     {
2587       ctree_row->row.cell[i].type = GTK_CMCELL_EMPTY;
2588       ctree_row->row.cell[i].vertical = 0;
2589       ctree_row->row.cell[i].horizontal = 0;
2590       ctree_row->row.cell[i].style = NULL;
2591     }
2592   GTK_CMCELL_PIXTEXT (ctree_row->row.cell[ctree->tree_column])->text = NULL;
2593
2594   ctree_row->row.fg_set     = FALSE;
2595   ctree_row->row.bg_set     = FALSE;
2596   ctree_row->row.style      = NULL;
2597   ctree_row->row.selectable = TRUE;
2598   ctree_row->row.state      = GTK_STATE_NORMAL;
2599   ctree_row->row.data       = NULL;
2600   ctree_row->row.destroy    = NULL;
2601
2602   ctree_row->level         = 0;
2603   ctree_row->expanded      = FALSE;
2604   ctree_row->parent        = NULL;
2605   ctree_row->sibling       = NULL;
2606   ctree_row->children      = NULL;
2607   ctree_row->pixbuf_closed = NULL;
2608   ctree_row->pixbuf_opened = NULL;
2609   
2610   return ctree_row;
2611 }
2612
2613 static void
2614 row_delete (GtkCMCTree    *ctree,
2615             GtkCMCTreeRow *ctree_row)
2616 {
2617   GtkCMCList *clist;
2618   gint i;
2619
2620   clist = GTK_CMCLIST (ctree);
2621
2622   for (i = 0; i < clist->columns; i++)
2623     {
2624       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2625         (clist, &(ctree_row->row), i, GTK_CMCELL_EMPTY, NULL, 0, NULL);
2626       if (ctree_row->row.cell[i].style)
2627         {
2628           if (gtk_widget_get_realized (GTK_WIDGET(ctree)))
2629             gtk_style_detach (ctree_row->row.cell[i].style);
2630           g_object_unref (ctree_row->row.cell[i].style);
2631         }
2632     }
2633
2634   if (ctree_row->row.style)
2635     {
2636       if (gtk_widget_get_realized (GTK_WIDGET(ctree)))
2637         gtk_style_detach (ctree_row->row.style);
2638       g_object_unref (ctree_row->row.style);
2639     }
2640
2641   if (ctree_row->pixbuf_closed)
2642     {
2643       g_object_unref (ctree_row->pixbuf_closed);
2644     }
2645
2646   if (ctree_row->pixbuf_opened)
2647     {
2648       g_object_unref (ctree_row->pixbuf_opened);
2649     }
2650
2651   if (ctree_row->row.destroy)
2652     {
2653       GDestroyNotify dnotify = ctree_row->row.destroy;
2654       gpointer ddata = ctree_row->row.data;
2655
2656       ctree_row->row.destroy = NULL;
2657       ctree_row->row.data = NULL;
2658
2659       dnotify (ddata);
2660     }
2661
2662 #if GLIB_CHECK_VERSION(2,10,0)  
2663   g_slice_free1 (sizeof (GtkCMCell) * clist->columns, ctree_row->row.cell);
2664   g_slice_free (GtkCMCTreeRow, ctree_row);
2665 #else
2666   g_mem_chunk_free ((GMemChunk *)clist->cell_mem_chunk, ctree_row->row.cell);
2667   g_mem_chunk_free ((GMemChunk *)clist->row_mem_chunk, ctree_row);
2668 #endif
2669 }
2670
2671 static void
2672 real_select_row (GtkCMCList *clist,
2673                  gint      row,
2674                  gint      column,
2675                  GdkEvent *event)
2676 {
2677   GList *node;
2678
2679   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2680   
2681   if ((node = g_list_nth (clist->row_list, row)) &&
2682       GTK_CMCTREE_ROW (node)->row.selectable)
2683     g_signal_emit (G_OBJECT (clist), ctree_signals[TREE_SELECT_ROW],0,
2684                      node, column);
2685 }
2686
2687 static void
2688 real_unselect_row (GtkCMCList *clist,
2689                    gint      row,
2690                    gint      column,
2691                    GdkEvent *event)
2692 {
2693   GList *node;
2694
2695   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2696
2697   if ((node = g_list_nth (clist->row_list, row)))
2698     g_signal_emit (G_OBJECT (clist), ctree_signals[TREE_UNSELECT_ROW],0,
2699                      node, column);
2700 }
2701
2702 static void
2703 tree_draw_node (GtkCMCTree     *ctree, 
2704                GtkCMCTreeNode *node)
2705 {
2706   GtkCMCList *clist;
2707   
2708   clist = GTK_CMCLIST (ctree);
2709
2710   if (CLIST_UNFROZEN (clist) && gtk_cmctree_is_viewable (ctree, node))
2711     {
2712       GtkCMCTreeNode *work;
2713       gint num = 0;
2714
2715       work = GTK_CMCTREE_NODE (clist->row_list);
2716       while (work && work != node)
2717         {
2718           work = GTK_CMCTREE_NODE_NEXT (work);
2719           num++;
2720         }
2721       if (work && gtk_cmclist_row_is_visible (clist, num) != GTK_VISIBILITY_NONE)
2722         GTK_CMCLIST_GET_CLASS(ctree)->draw_row
2723           (clist, NULL, num, GTK_CMCLIST_ROW ((GList *) node));
2724     }
2725 }
2726
2727 static void
2728 real_tree_select (GtkCMCTree     *ctree,
2729                   GtkCMCTreeNode *node,
2730                   gint          column)
2731 {
2732   GtkCMCList *clist;
2733   GList *list;
2734   GtkCMCTreeNode *sel_row;
2735   gboolean node_selected;
2736
2737   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2738
2739   if (!node || GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED ||
2740       !GTK_CMCTREE_ROW (node)->row.selectable)
2741     return;
2742
2743   clist = GTK_CMCLIST (ctree);
2744
2745   switch (clist->selection_mode)
2746     {
2747     case GTK_SELECTION_SINGLE:
2748     case GTK_SELECTION_BROWSE:
2749
2750       node_selected = FALSE;
2751       list = clist->selection;
2752
2753       while (list)
2754         {
2755           sel_row = list->data;
2756           list = list->next;
2757           
2758           if (node == sel_row)
2759             node_selected = TRUE;
2760           else
2761             g_signal_emit (G_OBJECT (ctree),
2762                              ctree_signals[TREE_UNSELECT_ROW], 0, sel_row, column);
2763         }
2764
2765       if (node_selected)
2766         return;
2767
2768     default:
2769       break;
2770     }
2771
2772   GTK_CMCTREE_ROW (node)->row.state = GTK_STATE_SELECTED;
2773
2774   if (!clist->selection)
2775     {
2776       clist->selection = g_list_append (clist->selection, node);
2777       clist->selection_end = clist->selection;
2778     }
2779   else
2780     clist->selection_end = g_list_append (clist->selection_end, node)->next;
2781
2782   tree_draw_node (ctree, node);
2783 }
2784
2785 static void
2786 real_tree_unselect (GtkCMCTree     *ctree,
2787                     GtkCMCTreeNode *node,
2788                     gint          column)
2789 {
2790   GtkCMCList *clist;
2791
2792   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2793
2794   if (!node || GTK_CMCTREE_ROW (node)->row.state != GTK_STATE_SELECTED)
2795     return;
2796
2797   clist = GTK_CMCLIST (ctree);
2798
2799   if (clist->selection_end && clist->selection_end->data == node)
2800     clist->selection_end = clist->selection_end->prev;
2801
2802   clist->selection = g_list_remove (clist->selection, node);
2803   
2804   GTK_CMCTREE_ROW (node)->row.state = GTK_STATE_NORMAL;
2805
2806   tree_draw_node (ctree, node);
2807 }
2808
2809 static void
2810 select_row_recursive (GtkCMCTree     *ctree, 
2811                       GtkCMCTreeNode *node, 
2812                       gpointer      data)
2813 {
2814   if (!node || GTK_CMCTREE_ROW (node)->row.state == GTK_STATE_SELECTED ||
2815       !GTK_CMCTREE_ROW (node)->row.selectable)
2816     return;
2817
2818   GTK_CMCLIST (ctree)->undo_unselection = 
2819     g_list_prepend (GTK_CMCLIST (ctree)->undo_unselection, node);
2820   gtk_cmctree_select (ctree, node);
2821 }
2822
2823 static void
2824 real_select_all (GtkCMCList *clist)
2825 {
2826   GtkCMCTree *ctree;
2827   GtkCMCTreeNode *node;
2828   
2829   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2830
2831   ctree = GTK_CMCTREE (clist);
2832
2833   switch (clist->selection_mode)
2834     {
2835     case GTK_SELECTION_SINGLE:
2836     case GTK_SELECTION_BROWSE:
2837       return;
2838
2839     case GTK_SELECTION_MULTIPLE:
2840
2841       gtk_cmclist_freeze (clist);
2842
2843       g_list_free (clist->undo_selection);
2844       g_list_free (clist->undo_unselection);
2845       clist->undo_selection = NULL;
2846       clist->undo_unselection = NULL;
2847           
2848       clist->anchor_state = GTK_STATE_SELECTED;
2849       clist->anchor = -1;
2850       clist->drag_pos = -1;
2851       clist->undo_anchor = clist->focus_row;
2852
2853       for (node = GTK_CMCTREE_NODE (clist->row_list); node;
2854            node = GTK_CMCTREE_NODE_NEXT (node))
2855         gtk_cmctree_pre_recursive (ctree, node, select_row_recursive, NULL);
2856
2857       gtk_cmclist_thaw (clist);
2858       break;
2859
2860     default:
2861       /* do nothing */
2862       break;
2863     }
2864 }
2865
2866 static void
2867 real_unselect_all (GtkCMCList *clist)
2868 {
2869   GtkCMCTree *ctree;
2870   GtkCMCTreeNode *node;
2871   GList *list;
2872  
2873   cm_return_if_fail (GTK_IS_CMCTREE (clist));
2874   
2875   ctree = GTK_CMCTREE (clist);
2876
2877   switch (clist->selection_mode)
2878     {
2879     case GTK_SELECTION_BROWSE:
2880       if (clist->focus_row >= 0)
2881         {
2882           gtk_cmctree_select
2883             (ctree,
2884              GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row)));
2885           return;
2886         }
2887       break;
2888
2889     case GTK_SELECTION_MULTIPLE:
2890       g_list_free (clist->undo_selection);
2891       g_list_free (clist->undo_unselection);
2892       clist->undo_selection = NULL;
2893       clist->undo_unselection = NULL;
2894
2895       clist->anchor = -1;
2896       clist->drag_pos = -1;
2897       clist->undo_anchor = clist->focus_row;
2898       break;
2899
2900     default:
2901       break;
2902     }
2903
2904   list = clist->selection;
2905
2906   while (list)
2907     {
2908       node = list->data;
2909       list = list->next;
2910       gtk_cmctree_unselect (ctree, node);
2911     }
2912 }
2913
2914 static gboolean
2915 ctree_is_hot_spot (GtkCMCTree     *ctree, 
2916                    GtkCMCTreeNode *node,
2917                    gint          row, 
2918                    gint          x, 
2919                    gint          y)
2920 {
2921   GtkCMCTreeRow *tree_row;
2922   GtkCMCList *clist;
2923   gint xl;
2924   gint yu;
2925   
2926   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
2927   cm_return_val_if_fail (node != NULL, FALSE);
2928
2929   clist = GTK_CMCLIST (ctree);
2930
2931   if (!clist->column[ctree->tree_column].visible ||
2932       ctree->expander_style == GTK_CMCTREE_EXPANDER_NONE)
2933     return FALSE;
2934
2935   tree_row = GTK_CMCTREE_ROW (node);
2936
2937   yu = (ROW_TOP_YPIXEL (clist, row) + (clist->row_height - PM_SIZE) / 2 -
2938         (clist->row_height - 1) % 2);
2939
2940   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT)
2941     xl = (clist->column[ctree->tree_column].area.x + 
2942           clist->column[ctree->tree_column].area.width - 1 + clist->hoffset -
2943           (tree_row->level - 1) * ctree->tree_indent - PM_SIZE);
2944   else
2945     xl = (clist->column[ctree->tree_column].area.x + clist->hoffset +
2946           (tree_row->level - 1) * ctree->tree_indent);
2947
2948   return (x >= xl && x <= xl + PM_SIZE && y >= yu && y <= yu + PM_SIZE);
2949 }
2950
2951 /***********************************************************
2952  ***********************************************************
2953  ***                  Public interface                   ***
2954  ***********************************************************
2955  ***********************************************************/
2956
2957
2958 /***********************************************************
2959  *           Creation, insertion, deletion                 *
2960  ***********************************************************/
2961
2962 static GObject*
2963 gtk_cmctree_constructor (GType                  type,
2964                        guint                  n_construct_properties,
2965                        GObjectConstructParam *construct_properties)
2966 {
2967   GObject *object = G_OBJECT_CLASS (parent_class)->constructor (type,
2968                                                                 n_construct_properties,
2969                                                                 construct_properties);
2970
2971   return object;
2972 }
2973
2974 GtkWidget*
2975 gtk_cmctree_new_with_titles (gint         columns, 
2976                            gint         tree_column,
2977                            gchar       *titles[])
2978 {
2979   GtkWidget *widget;
2980
2981   cm_return_val_if_fail (columns > 0, NULL);
2982   cm_return_val_if_fail (tree_column >= 0 && tree_column < columns, NULL);
2983
2984   widget = gtk_widget_new (GTK_TYPE_CMCTREE,
2985                            "n_columns", columns,
2986                            "tree_column", tree_column,
2987                            NULL);
2988   if (titles)
2989     {
2990       GtkCMCList *clist = GTK_CMCLIST (widget);
2991       guint i;
2992
2993       for (i = 0; i < columns; i++)
2994         gtk_cmclist_set_column_title (clist, i, titles[i]);
2995       gtk_cmclist_column_titles_show (clist);
2996     }
2997
2998   return widget;
2999 }
3000
3001 GtkWidget *
3002 gtk_cmctree_new (gint columns, 
3003                gint tree_column)
3004 {
3005   return gtk_cmctree_new_with_titles (columns, tree_column, NULL);
3006 }
3007
3008 static gint
3009 real_insert_row (GtkCMCList *clist,
3010                  gint      row,
3011                  gchar    *text[])
3012 {
3013   GtkCMCTreeNode *parent = NULL;
3014   GtkCMCTreeNode *sibling;
3015   GtkCMCTreeNode *node;
3016
3017   cm_return_val_if_fail (GTK_IS_CMCTREE (clist), -1);
3018
3019   sibling = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
3020   if (sibling)
3021     parent = GTK_CMCTREE_ROW (sibling)->parent;
3022
3023   node = gtk_cmctree_insert_node (GTK_CMCTREE (clist), parent, sibling, text, 5,
3024                                 NULL, NULL, TRUE, FALSE);
3025
3026   if (GTK_CMCLIST_AUTO_SORT (clist) || !sibling)
3027     return g_list_position (clist->row_list, (GList *) node);
3028   
3029   return row;
3030 }
3031
3032 GtkCMCTreeNode * 
3033 gtk_cmctree_insert_node (GtkCMCTree     *ctree,
3034                        GtkCMCTreeNode *parent, 
3035                        GtkCMCTreeNode *sibling,
3036                        gchar        *text[],
3037                        guint8        spacing,
3038                        GdkPixbuf    *pixbuf_closed,
3039                        GdkPixbuf    *pixbuf_opened,
3040                        gboolean      is_leaf,
3041                        gboolean      expanded)
3042 {
3043   GtkCMCList *clist;
3044   GtkCMCTreeRow *new_row;
3045   GtkCMCTreeNode *node;
3046   GList *list;
3047   gint i;
3048
3049   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3050   if (sibling)
3051     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3052
3053   if (parent && GTK_CMCTREE_ROW (parent)->is_leaf)
3054     return NULL;
3055
3056   clist = GTK_CMCLIST (ctree);
3057
3058   /* create the row */
3059   new_row = row_new (ctree);
3060   list = g_list_alloc ();
3061   list->data = new_row;
3062   node = GTK_CMCTREE_NODE (list);
3063
3064   if (text)
3065     for (i = 0; i < clist->columns; i++)
3066       if (text[i] && i != ctree->tree_column)
3067         GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
3068           (clist, &(new_row->row), i, GTK_CMCELL_TEXT, text[i], 0, NULL);
3069
3070   set_node_info (ctree, node, text ?
3071                  text[ctree->tree_column] : NULL, spacing, pixbuf_closed,
3072                  pixbuf_opened, is_leaf, expanded);
3073
3074   /* sorted insertion */
3075   if (GTK_CMCLIST_AUTO_SORT (clist))
3076     {
3077       if (parent)
3078         sibling = GTK_CMCTREE_ROW (parent)->children;
3079       else
3080         sibling = GTK_CMCTREE_NODE (clist->row_list);
3081
3082       while (sibling && clist->compare
3083              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (sibling)) > 0)
3084         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3085     }
3086
3087   gtk_cmctree_link (ctree, node, parent, sibling, TRUE);
3088
3089   if (text && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
3090       gtk_cmctree_is_viewable (ctree, node))
3091     {
3092       for (i = 0; i < clist->columns; i++)
3093         if (clist->column[i].auto_resize)
3094           column_auto_resize (clist, &(new_row->row), i, 0);
3095     }
3096
3097   if (clist->rows == 1)
3098     {
3099       clist->focus_row = 0;
3100       if (clist->selection_mode == GTK_SELECTION_BROWSE)
3101         gtk_cmctree_select (ctree, node);
3102     }
3103
3104
3105   CLIST_REFRESH (clist);
3106
3107   return node;
3108 }
3109
3110 GtkCMCTreeNode *
3111 gtk_cmctree_insert_gnode (GtkCMCTree          *ctree,
3112                         GtkCMCTreeNode      *parent,
3113                         GtkCMCTreeNode      *sibling,
3114                         GNode             *gnode,
3115                         GtkCMCTreeGNodeFunc  func,
3116                         gpointer           data)
3117 {
3118   GtkCMCList *clist;
3119   GtkCMCTreeNode *cnode = NULL;
3120   GtkCMCTreeNode *child = NULL;
3121   GtkCMCTreeNode *new_child;
3122   GList *list;
3123   GNode *work;
3124   guint depth = 1;
3125
3126   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3127   cm_return_val_if_fail (gnode != NULL, NULL);
3128   cm_return_val_if_fail (func != NULL, NULL);
3129   if (sibling)
3130     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3131   
3132   clist = GTK_CMCLIST (ctree);
3133
3134   if (parent)
3135     depth = GTK_CMCTREE_ROW (parent)->level + 1;
3136
3137   list = g_list_alloc ();
3138   list->data = row_new (ctree);
3139   cnode = GTK_CMCTREE_NODE (list);
3140
3141   gtk_cmclist_freeze (clist);
3142
3143   set_node_info (ctree, cnode, "", 0, NULL, NULL, TRUE, FALSE);
3144
3145   if (!func (ctree, depth, gnode, cnode, data))
3146     {
3147       tree_delete_row (ctree, cnode, NULL);
3148       gtk_cmclist_thaw (clist);
3149       return NULL;
3150     }
3151
3152   if (GTK_CMCLIST_AUTO_SORT (clist))
3153     {
3154       if (parent)
3155         sibling = GTK_CMCTREE_ROW (parent)->children;
3156       else
3157         sibling = GTK_CMCTREE_NODE (clist->row_list);
3158
3159       while (sibling && clist->compare
3160              (clist, GTK_CMCTREE_ROW (cnode), GTK_CMCTREE_ROW (sibling)) > 0)
3161         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3162     }
3163
3164   gtk_cmctree_link (ctree, cnode, parent, sibling, TRUE);
3165
3166   for (work = g_node_last_child (gnode); work; work = work->prev)
3167     {
3168       new_child = gtk_cmctree_insert_gnode (ctree, cnode, child,
3169                                           work, func, data);
3170       if (new_child)
3171         child = new_child;
3172     }   
3173   
3174   gtk_cmclist_thaw (clist);
3175
3176   return cnode;
3177 }
3178
3179 GNode *
3180 gtk_cmctree_export_to_gnode (GtkCMCTree          *ctree,
3181                            GNode             *parent,
3182                            GNode             *sibling,
3183                            GtkCMCTreeNode      *node,
3184                            GtkCMCTreeGNodeFunc  func,
3185                            gpointer           data)
3186 {
3187   GtkCMCTreeNode *work;
3188   GNode *gnode;
3189   gint depth;
3190
3191   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3192   cm_return_val_if_fail (node != NULL, NULL);
3193   cm_return_val_if_fail (func != NULL, NULL);
3194   if (sibling)
3195     {
3196       cm_return_val_if_fail (parent != NULL, NULL);
3197       cm_return_val_if_fail (sibling->parent == parent, NULL);
3198     }
3199
3200   gnode = g_node_new (NULL);
3201   depth = g_node_depth (parent) + 1;
3202   
3203   if (!func (ctree, depth, gnode, node, data))
3204     {
3205       g_node_destroy (gnode);
3206       return NULL;
3207     }
3208
3209   if (parent)
3210     g_node_insert_before (parent, sibling, gnode);
3211
3212   if (!GTK_CMCTREE_ROW (node)->is_leaf)
3213     {
3214       GNode *new_sibling = NULL;
3215
3216       for (work = GTK_CMCTREE_ROW (node)->children; work;
3217            work = GTK_CMCTREE_ROW (work)->sibling)
3218         new_sibling = gtk_cmctree_export_to_gnode (ctree, gnode, new_sibling,
3219                                                  work, func, data);
3220
3221       g_node_reverse_children (gnode);
3222     }
3223
3224   return gnode;
3225 }
3226   
3227 static void
3228 real_remove_row (GtkCMCList *clist,
3229                  gint      row)
3230 {
3231   GtkCMCTreeNode *node;
3232
3233   cm_return_if_fail (GTK_IS_CMCTREE (clist));
3234
3235   node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, row));
3236
3237   if (node)
3238     gtk_cmctree_remove_node (GTK_CMCTREE (clist), node);
3239 }
3240
3241 void
3242 gtk_cmctree_remove_node (GtkCMCTree     *ctree, 
3243                        GtkCMCTreeNode *node)
3244 {
3245   GtkCMCList *clist;
3246
3247   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3248
3249   clist = GTK_CMCLIST (ctree);
3250
3251   gtk_cmclist_freeze (clist);
3252
3253   if (node)
3254     {
3255       gtk_cmctree_unlink (ctree, node, TRUE);
3256       gtk_cmctree_post_recursive (ctree, node, GTK_CMCTREE_FUNC (tree_delete),
3257                                 NULL);
3258       if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
3259           clist->focus_row >= 0)
3260         gtk_cmclist_select_row (clist, clist->focus_row, -1);
3261
3262       auto_resize_columns (clist);
3263     }
3264   else
3265     gtk_cmclist_clear (clist);
3266
3267   gtk_cmclist_thaw (clist);
3268 }
3269
3270 static void
3271 real_clear (GtkCMCList *clist)
3272 {
3273   GtkCMCTree *ctree;
3274   GtkCMCTreeNode *work;
3275   GtkCMCTreeNode *ptr;
3276
3277   cm_return_if_fail (GTK_IS_CMCTREE (clist));
3278
3279   ctree = GTK_CMCTREE (clist);
3280
3281   /* remove all rows */
3282   work = GTK_CMCTREE_NODE (clist->row_list);
3283   clist->row_list = NULL;
3284   clist->row_list_end = NULL;
3285
3286   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3287   while (work)
3288     {
3289       ptr = work;
3290       work = GTK_CMCTREE_ROW (work)->sibling;
3291       gtk_cmctree_post_recursive (ctree, ptr, GTK_CMCTREE_FUNC (tree_delete_row), 
3292                                 NULL);
3293     }
3294   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3295
3296   parent_class->clear (clist);
3297 }
3298
3299
3300 /***********************************************************
3301  *  Generic recursive functions, querying / finding tree   *
3302  *  information                                            *
3303  ***********************************************************/
3304
3305
3306 void
3307 gtk_cmctree_post_recursive (GtkCMCTree     *ctree, 
3308                           GtkCMCTreeNode *node,
3309                           GtkCMCTreeFunc  func,
3310                           gpointer      data)
3311 {
3312   GtkCMCTreeNode *work;
3313   GtkCMCTreeNode *tmp;
3314
3315   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3316   cm_return_if_fail (func != NULL);
3317
3318   if (node)
3319     work = GTK_CMCTREE_ROW (node)->children;
3320   else
3321     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3322
3323   while (work)
3324     {
3325       tmp = GTK_CMCTREE_ROW (work)->sibling;
3326       gtk_cmctree_post_recursive (ctree, work, func, data);
3327       work = tmp;
3328     }
3329
3330   if (node)
3331     func (ctree, node, data);
3332 }
3333
3334 void
3335 gtk_cmctree_post_recursive_to_depth (GtkCMCTree     *ctree, 
3336                                    GtkCMCTreeNode *node,
3337                                    gint          depth,
3338                                    GtkCMCTreeFunc  func,
3339                                    gpointer      data)
3340 {
3341   GtkCMCTreeNode *work;
3342   GtkCMCTreeNode *tmp;
3343
3344   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3345   cm_return_if_fail (func != NULL);
3346
3347   if (depth < 0)
3348     {
3349       gtk_cmctree_post_recursive (ctree, node, func, data);
3350       return;
3351     }
3352
3353   if (node)
3354     work = GTK_CMCTREE_ROW (node)->children;
3355   else
3356     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3357
3358   if (work && GTK_CMCTREE_ROW (work)->level <= depth)
3359     {
3360       while (work)
3361         {
3362           tmp = GTK_CMCTREE_ROW (work)->sibling;
3363           gtk_cmctree_post_recursive_to_depth (ctree, work, depth, func, data);
3364           work = tmp;
3365         }
3366     }
3367
3368   if (node && GTK_CMCTREE_ROW (node)->level <= depth)
3369     func (ctree, node, data);
3370 }
3371
3372 void
3373 gtk_cmctree_pre_recursive (GtkCMCTree     *ctree, 
3374                          GtkCMCTreeNode *node,
3375                          GtkCMCTreeFunc  func,
3376                          gpointer      data)
3377 {
3378   GtkCMCTreeNode *work;
3379   GtkCMCTreeNode *tmp;
3380
3381   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3382   cm_return_if_fail (func != NULL);
3383
3384   if (node)
3385     {
3386       work = GTK_CMCTREE_ROW (node)->children;
3387       func (ctree, node, data);
3388     }
3389   else
3390     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3391
3392   while (work)
3393     {
3394       tmp = GTK_CMCTREE_ROW (work)->sibling;
3395       gtk_cmctree_pre_recursive (ctree, work, func, data);
3396       work = tmp;
3397     }
3398 }
3399
3400 void
3401 gtk_cmctree_pre_recursive_to_depth (GtkCMCTree     *ctree, 
3402                                   GtkCMCTreeNode *node,
3403                                   gint          depth, 
3404                                   GtkCMCTreeFunc  func,
3405                                   gpointer      data)
3406 {
3407   GtkCMCTreeNode *work;
3408   GtkCMCTreeNode *tmp;
3409
3410   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
3411   cm_return_if_fail (func != NULL);
3412
3413   if (depth < 0)
3414     {
3415       gtk_cmctree_pre_recursive (ctree, node, func, data);
3416       return;
3417     }
3418
3419   if (node)
3420     {
3421       work = GTK_CMCTREE_ROW (node)->children;
3422       if (GTK_CMCTREE_ROW (node)->level <= depth)
3423         func (ctree, node, data);
3424     }
3425   else
3426     work = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3427
3428   if (work && GTK_CMCTREE_ROW (work)->level <= depth)
3429     {
3430       while (work)
3431         {
3432           tmp = GTK_CMCTREE_ROW (work)->sibling;
3433           gtk_cmctree_pre_recursive_to_depth (ctree, work, depth, func, data);
3434           work = tmp;
3435         }
3436     }
3437 }
3438
3439 gboolean
3440 gtk_cmctree_is_viewable (GtkCMCTree     *ctree, 
3441                        GtkCMCTreeNode *node)
3442
3443   GtkCMCTreeRow *work;
3444
3445   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3446   cm_return_val_if_fail (node != NULL, FALSE);
3447
3448   work = GTK_CMCTREE_ROW (node);
3449
3450   while (work && work->parent && GTK_CMCTREE_ROW (work->parent)->expanded)
3451     work = GTK_CMCTREE_ROW (work->parent);
3452
3453   if (!work->parent)
3454     return TRUE;
3455
3456   return FALSE;
3457 }
3458
3459 GtkCMCTreeNode * 
3460 gtk_cmctree_last (GtkCMCTree     *ctree,
3461                 GtkCMCTreeNode *node)
3462 {
3463   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3464
3465   if (!node) 
3466     return NULL;
3467
3468   while (GTK_CMCTREE_ROW (node)->sibling)
3469     node = GTK_CMCTREE_ROW (node)->sibling;
3470   
3471   if (GTK_CMCTREE_ROW (node)->children)
3472     return gtk_cmctree_last (ctree, GTK_CMCTREE_ROW (node)->children);
3473   
3474   return node;
3475 }
3476
3477 GtkCMCTreeNode *
3478 gtk_cmctree_find_node_ptr (GtkCMCTree    *ctree,
3479                          GtkCMCTreeRow *ctree_row)
3480 {
3481   GtkCMCTreeNode *node;
3482   
3483   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3484   cm_return_val_if_fail (ctree_row != NULL, NULL);
3485   
3486   if (ctree_row->parent)
3487     node = GTK_CMCTREE_ROW (ctree_row->parent)->children;
3488   else
3489     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3490
3491   while (GTK_CMCTREE_ROW (node) != ctree_row)
3492     node = GTK_CMCTREE_ROW (node)->sibling;
3493   
3494   return node;
3495 }
3496
3497 GtkCMCTreeNode *
3498 gtk_cmctree_node_nth (GtkCMCTree *ctree,
3499                     guint     row)
3500 {
3501   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3502
3503   if ((row >= GTK_CMCLIST(ctree)->rows))
3504     return NULL;
3505  
3506   return GTK_CMCTREE_NODE (g_list_nth (GTK_CMCLIST (ctree)->row_list, row));
3507 }
3508
3509 gboolean
3510 gtk_cmctree_find (GtkCMCTree     *ctree,
3511                 GtkCMCTreeNode *node,
3512                 GtkCMCTreeNode *child)
3513 {
3514   if (!child)
3515     return FALSE;
3516
3517   if (!node)
3518     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3519
3520   while (node)
3521     {
3522       if (node == child) 
3523         return TRUE;
3524       if (GTK_CMCTREE_ROW (node)->children)
3525         {
3526           if (gtk_cmctree_find (ctree, GTK_CMCTREE_ROW (node)->children, child))
3527             return TRUE;
3528         }
3529       node = GTK_CMCTREE_ROW (node)->sibling;
3530     }
3531   return FALSE;
3532 }
3533
3534 gboolean
3535 gtk_cmctree_is_ancestor (GtkCMCTree     *ctree,
3536                        GtkCMCTreeNode *node,
3537                        GtkCMCTreeNode *child)
3538 {
3539   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), FALSE);
3540   cm_return_val_if_fail (node != NULL, FALSE);
3541
3542   if (GTK_CMCTREE_ROW (node)->children)
3543     return gtk_cmctree_find (ctree, GTK_CMCTREE_ROW (node)->children, child);
3544
3545   return FALSE;
3546 }
3547
3548 GtkCMCTreeNode *
3549 gtk_cmctree_find_by_row_data (GtkCMCTree     *ctree,
3550                             GtkCMCTreeNode *node,
3551                             gpointer      data)
3552 {
3553   GtkCMCTreeNode *work;
3554   
3555   if (!node)
3556     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3557   
3558   while (node)
3559     {
3560       if (GTK_CMCTREE_ROW (node)->row.data == data) 
3561         return node;
3562       if (GTK_CMCTREE_ROW (node)->children &&
3563           (work = gtk_cmctree_find_by_row_data 
3564            (ctree, GTK_CMCTREE_ROW (node)->children, data)))
3565         return work;
3566       node = GTK_CMCTREE_ROW (node)->sibling;
3567     }
3568   return NULL;
3569 }
3570
3571 GList *
3572 gtk_cmctree_find_all_by_row_data (GtkCMCTree     *ctree,
3573                                 GtkCMCTreeNode *node,
3574                                 gpointer      data)
3575 {
3576   GList *list = NULL;
3577
3578   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3579
3580   /* if node == NULL then look in the whole tree */
3581   if (!node)
3582     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3583
3584   while (node)
3585     {
3586       if (GTK_CMCTREE_ROW (node)->row.data == data)
3587         list = g_list_append (list, node);
3588
3589       if (GTK_CMCTREE_ROW (node)->children)
3590         {
3591           GList *sub_list;
3592
3593           sub_list = gtk_cmctree_find_all_by_row_data (ctree,
3594                                                      GTK_CMCTREE_ROW
3595                                                      (node)->children,
3596                                                      data);
3597           list = g_list_concat (list, sub_list);
3598         }
3599       node = GTK_CMCTREE_ROW (node)->sibling;
3600     }
3601   return list;
3602 }
3603
3604 GtkCMCTreeNode *
3605 gtk_cmctree_find_by_row_data_custom (GtkCMCTree     *ctree,
3606                                    GtkCMCTreeNode *node,
3607                                    gpointer      data,
3608                                    GCompareFunc  func)
3609 {
3610   GtkCMCTreeNode *work;
3611
3612   cm_return_val_if_fail (func != NULL, NULL);
3613
3614   if (!node)
3615     node = GTK_CMCTREE_NODE (GTK_CMCLIST (ctree)->row_list);
3616
3617   while (node)
3618     {
3619       if (!func (GTK_CMCTREE_ROW (node)->row.data, data))
3620         return node;
3621       if (GTK_CMCTREE_ROW (node)->children &&
3622           (work = gtk_cmctree_find_by_row_data_custom
3623            (ctree, GTK_CMCTREE_ROW (node)->children, data, func)))
3624         return work;
3625       node = GTK_CMCTREE_ROW (node)->sibling;
3626     }
3627   return NULL;
3628 }
3629
3630 GList *
3631 gtk_cmctree_find_all_by_row_data_custom (GtkCMCTree     *ctree,
3632                                        GtkCMCTreeNode *node,
3633                                        gpointer      data,
3634                                        GCompareFunc  func)
3635 {
3636   GList *list = NUL