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