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