22107f7f343b24069cdb27e80a69235c4e4b73f1
[claws.git] / src / gtk / gtksctree.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Parts of this file:
4  * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
5  *
6  * Parts of this file from gtk/gtkctree.c and gtk/gtkclist.c:
7  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball, Josh MacDonald, 
8  * Copyright (C) 1997-1998 Jay Painter <jpaint@serv.net><jpaint@gimp.org>  
9  *
10  * Parts of this file from gtkflist.c:
11  * Copyright (C) 1999 The Free Software Foundation
12  * Author: Federico Mena <federico@nuclecu.unam.mx>
13  *
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program. If not, see <http://www.gnu.org/licenses/>.
26  * 
27  */
28
29 #include <stdlib.h>
30
31 #include "gtksctree.h"
32 #include "claws-marshal.h"
33 #include "prefs_common.h"
34 #include "utils.h"
35
36 #define CLIST_UNFROZEN(clist)     (((GtkCMCList*) (clist))->freeze_count == 0)
37 #define CLIST_REFRESH(clist)    G_STMT_START { \
38   if (CLIST_UNFROZEN (clist)) \
39     GTK_CMCLIST_GET_CLASS (clist)->refresh ((GtkCMCList*) (clist)); \
40 } G_STMT_END
41 #define CELL_SPACING               1
42 #define CLIST_OPTIMUM_SIZE         64
43 #define COLUMN_INSET               3
44 #define PM_SIZE                    8
45 #define TAB_SIZE                   (PM_SIZE + 6)
46 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
47                                     (((row) + 1) * CELL_SPACING) + \
48                                     (clist)->voffset)
49 #define ROW_FROM_YPIXEL(clist, y)  (((y) - (clist)->voffset) / \
50                                     ((clist)->row_height + CELL_SPACING))
51 #define COLUMN_LEFT_XPIXEL(clist, col)  ((clist)->column[(col)].area.x \
52                                     + (clist)->hoffset)
53 #define COLUMN_LEFT(clist, column) ((clist)->column[(column)].area.x)
54
55 enum {
56         ROW_POPUP_MENU,
57         EMPTY_POPUP_MENU,
58         OPEN_ROW,
59         START_DRAG,
60         LAST_SIGNAL
61 };
62
63 static void gtk_sctree_class_init (GtkSCTreeClass *class);
64 static void gtk_sctree_init (GtkSCTree *sctree);
65
66 static gint gtk_sctree_button_press (GtkWidget *widget, GdkEventButton *event);
67 static gint gtk_sctree_button_release (GtkWidget *widget, GdkEventButton *event);
68 static gint gtk_sctree_motion (GtkWidget *widget, GdkEventMotion *event);
69 static void gtk_sctree_drag_begin (GtkWidget *widget, GdkDragContext *context);
70 static void gtk_sctree_drag_end (GtkWidget *widget, GdkDragContext *context);
71 static void gtk_sctree_drag_data_get (GtkWidget *widget, GdkDragContext *context,
72                                      GtkSelectionData *data, guint info, guint time);
73 static void gtk_sctree_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time);
74 static gboolean gtk_sctree_drag_motion (GtkWidget *widget, GdkDragContext *context,
75                                        gint x, gint y, guint time);
76 static gboolean gtk_sctree_drag_drop (GtkWidget *widget, GdkDragContext *context,
77                                      gint x, gint y, guint time);
78 static void gtk_sctree_drag_data_received (GtkWidget *widget, GdkDragContext *context,
79                                           gint x, gint y, GtkSelectionData *data,
80                                           guint info, guint time);
81
82 static void gtk_sctree_clear (GtkCMCList *clist);
83 static void gtk_sctree_real_unselect_all (GtkCMCList *clist);
84        
85 static void stree_sort (GtkCMCTree *ctree, GtkCMCTreeNode  *node, gpointer data);
86 void gtk_sctree_sort_node (GtkCMCTree *ctree, GtkCMCTreeNode *node);
87 void gtk_sctree_sort_recursive (GtkCMCTree *ctree, GtkCMCTreeNode *node);
88
89 static void gtk_sctree_link (GtkCMCTree *ctree,
90                         GtkCMCTreeNode  *node,
91                         GtkCMCTreeNode  *parent,
92                         GtkCMCTreeNode  *sibling,
93                         gboolean       update_focus_row);
94
95 static void gtk_sctree_unlink (GtkCMCTree      *ctree, 
96                         GtkCMCTreeNode  *node,
97                         gboolean       update_focus_row);
98
99 static void stree_update_level (GtkCMCTree      *ctree, 
100                         GtkCMCTreeNode  *node, 
101                         gpointer       data);
102
103 static GtkCMCTreeNode * gtk_sctree_last_visible (GtkCMCTree     *ctree,
104                                               GtkCMCTreeNode *node);
105 static void gtk_sctree_real_tree_expand            (GtkCMCTree      *ctree,
106                                                  GtkCMCTreeNode  *node);
107 static void gtk_sctree_real_tree_collapse          (GtkCMCTree      *ctree,
108                                                  GtkCMCTreeNode  *node);
109 static void
110 sreal_tree_move (GtkCMCTree     *ctree,
111                 GtkCMCTreeNode *node,
112                 GtkCMCTreeNode *new_parent, 
113                 GtkCMCTreeNode *new_sibling);
114
115 static GtkCMCTreeClass *parent_class;
116
117 static guint sctree_signals[LAST_SIGNAL];
118
119 /**
120  * gtk_sctree_get_type:
121  * @void: 
122  * 
123  * Creates the GtkSCTree class and its type information
124  * 
125  * Return value: The type ID for GtkSCTreeClass
126  **/
127 GType
128 gtk_sctree_get_type (void)
129 {
130         static GType sctree_type = 0;
131
132         if (!sctree_type) {
133                 GTypeInfo sctree_info = {
134                         sizeof (GtkSCTreeClass),
135
136                         (GBaseInitFunc) NULL,
137                         (GBaseFinalizeFunc) NULL,
138
139                         (GClassInitFunc) gtk_sctree_class_init,
140                         (GClassFinalizeFunc) NULL,
141                         NULL,   /* class_data */
142
143                         sizeof (GtkSCTree),
144                         0,      /* n_preallocs */
145                         (GInstanceInitFunc) gtk_sctree_init,
146                 };
147
148                 sctree_type = g_type_register_static (GTK_TYPE_CMCTREE, "GtkSCTree", &sctree_info, (GTypeFlags)0);
149         }
150
151         return sctree_type;
152 }
153
154 static gint
155 gtk_sctree_draw_cell_pixbuf (GdkWindow    *window,
156                   GdkRectangle *clip_rectangle,
157                   GdkGC        *fg_gc,
158                   GdkPixbuf    *pixbuf,
159                   gint          x,
160                   gint          y,
161                   gint          width,
162                   gint          height)
163 {
164   gint xsrc = 0;
165   gint ysrc = 0;
166
167   gdk_gc_set_clip_origin (fg_gc, x, y);
168   if (x < clip_rectangle->x)
169     {
170       xsrc = clip_rectangle->x - x;
171       width -= xsrc;
172       x = clip_rectangle->x;
173     }
174   if (x + width > clip_rectangle->x + clip_rectangle->width)
175     width = clip_rectangle->x + clip_rectangle->width - x;
176
177   if (y < clip_rectangle->y)
178     {
179       ysrc = clip_rectangle->y - y;
180       height -= ysrc;
181       y = clip_rectangle->y;
182     }
183   if (y + height > clip_rectangle->y + clip_rectangle->height)
184     height = clip_rectangle->y + clip_rectangle->height - y;
185
186   if (width > 0 && height > 0)
187     gdk_draw_pixbuf (window, fg_gc, pixbuf, xsrc, ysrc, x, y, width, height, GDK_RGB_DITHER_NONE, 0, 0);
188
189   gdk_gc_set_clip_origin (fg_gc, 0, 0);
190
191   return x + MAX (width, 0);
192 }
193
194 static void
195 gtk_sctree_get_cell_style (GtkCMCList     *clist,
196                 GtkCMCListRow  *clist_row,
197                 gint          state,
198                 gint          row,
199                 gint          column,
200                 GtkStyle    **style,
201                 GdkGC       **fg_gc,
202                 GdkGC       **bg_gc)
203 {
204   gint fg_state;
205
206   if ((state == GTK_STATE_NORMAL) &&
207       (GTK_WIDGET (clist)->state == GTK_STATE_INSENSITIVE))
208     fg_state = GTK_STATE_INSENSITIVE;
209   else
210     fg_state = state;
211
212   if (clist_row->cell[column].style)
213     {
214       if (style)
215         *style = clist_row->cell[column].style;
216       if (fg_gc)
217         *fg_gc = clist_row->cell[column].style->fg_gc[fg_state];
218       if (bg_gc) {
219         if (state == GTK_STATE_SELECTED)
220           *bg_gc = clist_row->cell[column].style->bg_gc[state];
221       }
222     }
223   else if (clist_row->style)
224     {
225       if (style)
226         *style = clist_row->style;
227       if (fg_gc)
228         *fg_gc = clist_row->style->fg_gc[fg_state];
229       if (bg_gc) {
230         if (state == GTK_STATE_SELECTED)
231           *bg_gc = clist_row->style->bg_gc[state];
232         else
233           *bg_gc = clist_row->bg_set ? 
234                 clist->bg_gc : clist_row->style->base_gc[state];
235       }
236     }
237   else
238     {
239       if (style)
240         *style = GTK_WIDGET (clist)->style;
241       if (fg_gc)
242         *fg_gc = GTK_WIDGET (clist)->style->fg_gc[fg_state];
243       if (bg_gc) {
244         if (state == GTK_STATE_SELECTED)
245           *bg_gc = GTK_WIDGET (clist)->style->bg_gc[state];
246         else
247           *bg_gc = GTK_WIDGET (clist)->style->base_gc[state];
248       }
249
250       if (state != GTK_STATE_SELECTED)
251         {
252           if (fg_gc && clist_row->fg_set)
253             *fg_gc = clist->fg_gc;
254           if (bg_gc && clist_row->bg_set)
255             *bg_gc = clist->bg_gc;
256         }
257     }
258 }
259
260 static gint
261 gtk_sctree_draw_expander (GtkCMCTree     *ctree,
262                          GtkCMCTreeRow  *ctree_row,
263                          GtkStyle     *style,
264                          GdkRectangle *clip_rectangle,
265                          gint          x)
266 {
267   GtkCMCList *clist;
268   GdkPoint points[3];
269   gint justification_factor;
270   gint y;
271
272  if (ctree->expander_style == GTK_CMCTREE_EXPANDER_NONE)
273    return x;
274
275   clist = GTK_CMCLIST (ctree);
276   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT)
277     justification_factor = -1;
278   else
279     justification_factor = 1;
280   if (!GTK_CMCLIST_ROW_HEIGHT_SET(GTK_CMCLIST(clist)))
281       y = (clip_rectangle->y + (clip_rectangle->height - PM_SIZE) / 2 -
282           (clip_rectangle->height + 1) % 2);
283   else
284       y = (clip_rectangle->y + (clip_rectangle->height/2 - PM_SIZE) / 2 -
285           (clip_rectangle->height/2 + 1) % 2);
286
287   if (!ctree_row->children)
288     {
289       switch (ctree->expander_style)
290         {
291         case GTK_CMCTREE_EXPANDER_NONE:
292           return x;
293         case GTK_CMCTREE_EXPANDER_TRIANGLE:
294           return x + justification_factor * (PM_SIZE + 3);
295         case GTK_CMCTREE_EXPANDER_SQUARE:
296         case GTK_CMCTREE_EXPANDER_CIRCULAR:
297           return x + justification_factor * (PM_SIZE + 1);
298         }
299     }
300
301   gdk_gc_set_clip_rectangle (style->fg_gc[GTK_STATE_NORMAL], clip_rectangle);
302   gdk_gc_set_clip_rectangle (style->base_gc[GTK_STATE_NORMAL], clip_rectangle);
303
304   switch (ctree->expander_style)
305     {
306     case GTK_CMCTREE_EXPANDER_NONE:
307       break;
308     case GTK_CMCTREE_EXPANDER_TRIANGLE:
309       if (ctree_row->expanded)
310         {
311           points[0].x = x;
312           points[0].y = y + (PM_SIZE + 2) / 6;
313           points[1].x = points[0].x + justification_factor * (PM_SIZE + 2);
314           points[1].y = points[0].y;
315           points[2].x = (points[0].x +
316                          justification_factor * (PM_SIZE + 2) / 2);
317           points[2].y = y + 2 * (PM_SIZE + 2) / 3;
318         }
319       else
320         {
321           points[0].x = x + justification_factor * ((PM_SIZE + 2) / 6 + 2);
322           points[0].y = y - 1;
323           points[1].x = points[0].x;
324           points[1].y = points[0].y + (PM_SIZE + 2);
325           points[2].x = (points[0].x +
326                          justification_factor * (2 * (PM_SIZE + 2) / 3 - 1));
327           points[2].y = points[0].y + (PM_SIZE + 2) / 2;
328         }
329
330       gdk_draw_polygon (clist->clist_window, style->base_gc[GTK_STATE_NORMAL],
331                         TRUE, points, 3);
332       gdk_draw_polygon (clist->clist_window, style->fg_gc[GTK_STATE_NORMAL],
333                         FALSE, points, 3);
334
335       x += justification_factor * (PM_SIZE + 3);
336       break;
337     case GTK_CMCTREE_EXPANDER_SQUARE:
338     case GTK_CMCTREE_EXPANDER_CIRCULAR:
339       if (justification_factor == -1)
340         x += justification_factor * (PM_SIZE + 1);
341
342       if (ctree->expander_style == GTK_CMCTREE_EXPANDER_CIRCULAR)
343         {
344           gdk_draw_arc (clist->clist_window, style->base_gc[GTK_STATE_NORMAL],
345                         TRUE, x, y, PM_SIZE, PM_SIZE, 0, 360 * 64);
346           gdk_draw_arc (clist->clist_window, style->fg_gc[GTK_STATE_NORMAL],
347                         FALSE, x, y, PM_SIZE, PM_SIZE, 0, 360 * 64);
348         }
349       else
350         {
351           gdk_draw_rectangle (clist->clist_window,
352                               style->base_gc[GTK_STATE_NORMAL], TRUE,
353                               x, y, PM_SIZE, PM_SIZE);
354           gdk_draw_rectangle (clist->clist_window,
355                               style->fg_gc[GTK_STATE_NORMAL], FALSE,
356                               x, y, PM_SIZE, PM_SIZE);
357         }
358
359       gdk_draw_line (clist->clist_window, style->fg_gc[GTK_STATE_NORMAL], 
360                      x + 2, y + PM_SIZE / 2, x + PM_SIZE - 2, y + PM_SIZE / 2);
361
362       if (!ctree_row->expanded)
363         gdk_draw_line (clist->clist_window, style->fg_gc[GTK_STATE_NORMAL],
364                        x + PM_SIZE / 2, y + 2,
365                        x + PM_SIZE / 2, y + PM_SIZE - 2);
366
367       if (justification_factor == 1)
368         x += justification_factor * (PM_SIZE + 1);
369       break;
370     }
371
372   gdk_gc_set_clip_rectangle (style->fg_gc[GTK_STATE_NORMAL], NULL);
373   gdk_gc_set_clip_rectangle (style->base_gc[GTK_STATE_NORMAL], NULL);
374
375   return x;
376 }
377
378 static gint
379 gtk_sctree_draw_lines (GtkCMCTree     *ctree,
380                       GtkCMCTreeRow  *ctree_row,
381                       gint          row,
382                       gint          column,
383                       gint          state,
384                       GdkRectangle *clip_rectangle,
385                       GdkRectangle *cell_rectangle,
386                       GdkRectangle *crect,
387                       GdkRectangle *area,
388                       GtkStyle     *style)
389 {
390   GtkCMCList *clist;
391   GtkCMCTreeNode *node;
392   GtkCMCTreeNode *parent;
393   GdkRectangle tree_rectangle;
394   GdkRectangle tc_rectangle;
395   GdkGC *bg_gc;
396   gint offset;
397   gint offset_x;
398   gint offset_y;
399   gint xcenter;
400   gint ycenter;
401   gint next_level;
402   gint column_right;
403   gint column_left;
404   gint justify_right;
405   gint justification_factor;
406   
407   clist = GTK_CMCLIST (ctree);
408   ycenter = clip_rectangle->y + (clip_rectangle->height / 2);
409   justify_right = (clist->column[column].justification == GTK_JUSTIFY_RIGHT);
410
411   if (justify_right)
412     {
413       offset = (clip_rectangle->x + clip_rectangle->width - 1 -
414                 ctree->tree_indent * (ctree_row->level - 1));
415       justification_factor = -1;
416     }
417   else
418     {
419       offset = clip_rectangle->x + ctree->tree_indent * (ctree_row->level - 1);
420       justification_factor = 1;
421     }
422
423   switch (ctree->line_style)
424     {
425     case GTK_CMCTREE_LINES_NONE:
426       break;
427     case GTK_CMCTREE_LINES_TABBED:
428       xcenter = offset + justification_factor * TAB_SIZE;
429
430       column_right = (COLUMN_LEFT_XPIXEL (clist, ctree->tree_column) +
431                       clist->column[ctree->tree_column].area.width +
432                       COLUMN_INSET);
433       column_left = (COLUMN_LEFT_XPIXEL (clist, ctree->tree_column) -
434                      COLUMN_INSET - CELL_SPACING);
435
436       if (area)
437         {
438           tree_rectangle.y = crect->y;
439           tree_rectangle.height = crect->height;
440
441           if (justify_right)
442             {
443               tree_rectangle.x = xcenter;
444               tree_rectangle.width = column_right - xcenter;
445             }
446           else
447             {
448               tree_rectangle.x = column_left;
449               tree_rectangle.width = xcenter - column_left;
450             }
451
452           if (!gdk_rectangle_intersect (area, &tree_rectangle, &tc_rectangle))
453             {
454               offset += justification_factor * 3;
455               break;
456             }
457         }
458
459       gdk_gc_set_clip_rectangle (ctree->lines_gc, crect);
460
461       next_level = ctree_row->level;
462
463       if (!ctree_row->sibling || (ctree_row->children && ctree_row->expanded))
464         {
465           node = gtk_cmctree_find_node_ptr (ctree, ctree_row);
466           if (GTK_CMCTREE_NODE_NEXT (node))
467             next_level = GTK_CMCTREE_ROW (GTK_CMCTREE_NODE_NEXT (node))->level;
468           else
469             next_level = 0;
470         }
471
472       if (ctree->tree_indent > 0)
473         {
474           node = ctree_row->parent;
475           while (node)
476             {
477               xcenter -= (justification_factor * ctree->tree_indent);
478
479               if ((justify_right && xcenter < column_left) ||
480                   (!justify_right && xcenter > column_right))
481                 {
482                   node = GTK_CMCTREE_ROW (node)->parent;
483                   continue;
484                 }
485
486               tree_rectangle.y = cell_rectangle->y;
487               tree_rectangle.height = cell_rectangle->height;
488               if (justify_right)
489                 {
490                   tree_rectangle.x = MAX (xcenter - ctree->tree_indent + 1,
491                                           column_left);
492                   tree_rectangle.width = MIN (xcenter - column_left,
493                                               ctree->tree_indent);
494                 }
495               else
496                 {
497                   tree_rectangle.x = xcenter;
498                   tree_rectangle.width = MIN (column_right - xcenter,
499                                               ctree->tree_indent);
500                 }
501
502               if (!area || gdk_rectangle_intersect (area, &tree_rectangle,
503                                                     &tc_rectangle))
504                 {
505                   gtk_sctree_get_cell_style (clist, &GTK_CMCTREE_ROW (node)->row,
506                                   state, row, column, NULL, NULL, &bg_gc);
507
508                   if (bg_gc == clist->bg_gc)
509                     gdk_gc_set_foreground
510                       (clist->bg_gc, &GTK_CMCTREE_ROW (node)->row.background);
511
512                   if (!area)
513                     gdk_draw_rectangle (clist->clist_window, bg_gc, TRUE,
514                                         tree_rectangle.x,
515                                         tree_rectangle.y,
516                                         tree_rectangle.width,
517                                         tree_rectangle.height);
518                   else 
519                     gdk_draw_rectangle (clist->clist_window, bg_gc, TRUE,
520                                         tc_rectangle.x,
521                                         tc_rectangle.y,
522                                         tc_rectangle.width,
523                                         tc_rectangle.height);
524                 }
525               if (next_level > GTK_CMCTREE_ROW (node)->level)
526                 gdk_draw_line (clist->clist_window, ctree->lines_gc,
527                                xcenter, crect->y,
528                                xcenter, crect->y + crect->height);
529               else
530                 {
531                   gint width;
532
533                   offset_x = MIN (ctree->tree_indent, 2 * TAB_SIZE);
534                   width = offset_x / 2 + offset_x % 2;
535
536                   parent = GTK_CMCTREE_ROW (node)->parent;
537
538                   tree_rectangle.y = ycenter;
539                   tree_rectangle.height = (cell_rectangle->y - ycenter +
540                                            cell_rectangle->height);
541
542                   if (justify_right)
543                     {
544                       tree_rectangle.x = MAX(xcenter + 1 - width, column_left);
545                       tree_rectangle.width = MIN (xcenter + 1 - column_left,
546                                                   width);
547                     }
548                   else
549                     {
550                       tree_rectangle.x = xcenter;
551                       tree_rectangle.width = MIN (column_right - xcenter,
552                                                   width);
553                     }
554
555                   if (!area ||
556                       gdk_rectangle_intersect (area, &tree_rectangle,
557                                                &tc_rectangle))
558                     {
559                       if (parent)
560                         {
561                           gtk_sctree_get_cell_style (clist, &GTK_CMCTREE_ROW (parent)->row,
562                                           state, row, column, NULL, NULL, &bg_gc);
563                           if (bg_gc == clist->bg_gc)
564                             gdk_gc_set_foreground
565                               (clist->bg_gc,
566                                &GTK_CMCTREE_ROW (parent)->row.background);
567                         }
568                       else if (state == GTK_STATE_SELECTED)
569                         bg_gc = style->base_gc[state];
570                       else
571                         bg_gc = GTK_WIDGET (clist)->style->base_gc[state];
572
573                       if (!area)
574                         gdk_draw_rectangle (clist->clist_window, bg_gc, TRUE,
575                                             tree_rectangle.x,
576                                             tree_rectangle.y,
577                                             tree_rectangle.width,
578                                             tree_rectangle.height);
579                       else
580                         gdk_draw_rectangle (clist->clist_window,
581                                             bg_gc, TRUE,
582                                             tc_rectangle.x,
583                                             tc_rectangle.y,
584                                             tc_rectangle.width,
585                                             tc_rectangle.height);
586                     }
587
588                   gtk_sctree_get_cell_style (clist, &GTK_CMCTREE_ROW (node)->row,
589                                   state, row, column, NULL, NULL, &bg_gc);
590                   if (bg_gc == clist->bg_gc)
591                     gdk_gc_set_foreground
592                       (clist->bg_gc, &GTK_CMCTREE_ROW (node)->row.background);
593
594                   gdk_gc_set_clip_rectangle (bg_gc, crect);
595                   gdk_draw_arc (clist->clist_window, bg_gc, TRUE,
596                                 xcenter - (justify_right * offset_x),
597                                 cell_rectangle->y,
598                                 offset_x, clist->row_height,
599                                 (180 + (justify_right * 90)) * 64, 90 * 64);
600                   gdk_gc_set_clip_rectangle (bg_gc, NULL);
601
602                   gdk_draw_line (clist->clist_window, ctree->lines_gc, 
603                                  xcenter, cell_rectangle->y, xcenter, ycenter);
604
605                   if (justify_right)
606                     gdk_draw_arc (clist->clist_window, ctree->lines_gc, FALSE,
607                                   xcenter - offset_x, cell_rectangle->y,
608                                   offset_x, clist->row_height,
609                                   270 * 64, 90 * 64);
610                   else
611                     gdk_draw_arc (clist->clist_window, ctree->lines_gc, FALSE,
612                                   xcenter, cell_rectangle->y,
613                                   offset_x, clist->row_height,
614                                   180 * 64, 90 * 64);
615                 }
616               node = GTK_CMCTREE_ROW (node)->parent;
617             }
618         }
619
620       if (state != GTK_STATE_SELECTED)
621         {
622           tree_rectangle.y = clip_rectangle->y;
623           tree_rectangle.height = clip_rectangle->height;
624           tree_rectangle.width = COLUMN_INSET + CELL_SPACING +
625             MIN (clist->column[ctree->tree_column].area.width + COLUMN_INSET,
626                  TAB_SIZE);
627
628           if (justify_right)
629             tree_rectangle.x = MAX (xcenter + 1, column_left);
630           else
631             tree_rectangle.x = column_left;
632
633           if (!area)
634             gdk_draw_rectangle (clist->clist_window,
635                                 GTK_WIDGET
636                                 (ctree)->style->base_gc[GTK_STATE_NORMAL],
637                                 TRUE,
638                                 tree_rectangle.x,
639                                 tree_rectangle.y,
640                                 tree_rectangle.width,
641                                 tree_rectangle.height);
642           else if (gdk_rectangle_intersect (area, &tree_rectangle,
643                                             &tc_rectangle))
644             gdk_draw_rectangle (clist->clist_window,
645                                 GTK_WIDGET
646                                 (ctree)->style->base_gc[GTK_STATE_NORMAL],
647                                 TRUE,
648                                 tc_rectangle.x,
649                                 tc_rectangle.y,
650                                 tc_rectangle.width,
651                                 tc_rectangle.height);
652         }
653
654       xcenter = offset + (justification_factor * ctree->tree_indent / 2);
655
656       gtk_sctree_get_cell_style (clist, &ctree_row->row, state, row, column, NULL, NULL,
657                       &bg_gc);
658       if (bg_gc == clist->bg_gc)
659         gdk_gc_set_foreground (clist->bg_gc, &ctree_row->row.background);
660
661       gdk_gc_set_clip_rectangle (bg_gc, crect);
662       if (ctree_row->is_leaf)
663         {
664           GdkPoint points[6];
665
666           points[0].x = offset + justification_factor * TAB_SIZE;
667           points[0].y = cell_rectangle->y;
668
669           points[1].x = points[0].x - justification_factor * 4;
670           points[1].y = points[0].y;
671
672           points[2].x = points[1].x - justification_factor * 2;
673           points[2].y = points[1].y + 3;
674
675           points[3].x = points[2].x;
676           points[3].y = points[2].y + clist->row_height - 5;
677
678           points[4].x = points[3].x + justification_factor * 2;
679           points[4].y = points[3].y + 3;
680
681           points[5].x = points[4].x + justification_factor * 4;
682           points[5].y = points[4].y;
683
684           gdk_draw_polygon (clist->clist_window, bg_gc, TRUE, points, 6);
685           gdk_draw_lines (clist->clist_window, ctree->lines_gc, points, 6);
686         }
687       else 
688         {
689           gdk_draw_arc (clist->clist_window, bg_gc, TRUE,
690                         offset - (justify_right * 2 * TAB_SIZE),
691                         cell_rectangle->y,
692                         2 * TAB_SIZE, clist->row_height,
693                         (90 + (180 * justify_right)) * 64, 180 * 64);
694           gdk_draw_arc (clist->clist_window, ctree->lines_gc, FALSE,
695                         offset - (justify_right * 2 * TAB_SIZE),
696                         cell_rectangle->y,
697                         2 * TAB_SIZE, clist->row_height,
698                         (90 + (180 * justify_right)) * 64, 180 * 64);
699         }
700       gdk_gc_set_clip_rectangle (bg_gc, NULL);
701       gdk_gc_set_clip_rectangle (ctree->lines_gc, NULL);
702
703       offset += justification_factor * 3;
704       break;
705     default:
706       xcenter = offset + justification_factor * PM_SIZE / 2;
707
708       if (area)
709         {
710           tree_rectangle.y = crect->y;
711           tree_rectangle.height = crect->height;
712
713           if (justify_right)
714             {
715               tree_rectangle.x = xcenter - PM_SIZE / 2 - 2;
716               tree_rectangle.width = (clip_rectangle->x +
717                                       clip_rectangle->width -tree_rectangle.x);
718             }
719           else
720             {
721               tree_rectangle.x = clip_rectangle->x + PM_SIZE / 2;
722               tree_rectangle.width = (xcenter + PM_SIZE / 2 + 2 -
723                                       clip_rectangle->x);
724             }
725
726           if (!gdk_rectangle_intersect (area, &tree_rectangle, &tc_rectangle))
727             break;
728         }
729
730       offset_x = 1;
731       offset_y = 0;
732       if (ctree->line_style == GTK_CMCTREE_LINES_DOTTED)
733         {
734           offset_x += abs((clip_rectangle->x + clist->hoffset) % 2);
735           offset_y  = abs((cell_rectangle->y + clist->voffset) % 2);
736         }
737
738       clip_rectangle->y--;
739       clip_rectangle->height++;
740       gdk_gc_set_clip_rectangle (ctree->lines_gc, clip_rectangle);
741       gdk_draw_line (clist->clist_window, ctree->lines_gc,
742                      xcenter,
743                      (ctree->show_stub || clist->row_list->data != ctree_row) ?
744                      cell_rectangle->y + offset_y : ycenter,
745                      xcenter,
746                      (ctree_row->sibling) ? crect->y +crect->height : ycenter);
747
748       gdk_draw_line (clist->clist_window, ctree->lines_gc,
749                      xcenter + (justification_factor * offset_x), ycenter,
750                      xcenter + (justification_factor * (PM_SIZE / 2 + 2)),
751                      ycenter);
752
753       node = ctree_row->parent;
754       while (node)
755         {
756           xcenter -= (justification_factor * ctree->tree_indent);
757
758           if (GTK_CMCTREE_ROW (node)->sibling)
759             gdk_draw_line (clist->clist_window, ctree->lines_gc, 
760                            xcenter, cell_rectangle->y + offset_y,
761                            xcenter, crect->y + crect->height);
762           node = GTK_CMCTREE_ROW (node)->parent;
763         }
764       gdk_gc_set_clip_rectangle (ctree->lines_gc, NULL);
765       clip_rectangle->y++;
766       clip_rectangle->height--;
767       break;
768     }
769   return offset;
770 }
771
772 static gboolean filter_fg (PangoAttribute *attribute, gpointer data)
773 {
774         const PangoAttrClass *klass = attribute->klass;
775         if (klass->type == PANGO_ATTR_FOREGROUND)
776                 return TRUE;
777
778         return FALSE;   
779 }
780
781 static PangoLayout *
782 sc_gtk_cmclist_create_cell_layout (GtkCMCList       *clist,
783                                GtkCMCListRow    *clist_row,
784                                gint            column)
785 {
786   PangoLayout *layout;
787   GtkStyle *style;
788   GtkCMCell *cell;
789   gchar *text;
790   
791   gtk_sctree_get_cell_style (clist, clist_row, GTK_STATE_NORMAL, 0, column, &style,
792                   NULL, NULL);
793
794
795   cell = &clist_row->cell[column];
796   switch (cell->type)
797     {
798     case GTK_CMCELL_TEXT:
799     case GTK_CMCELL_PIXTEXT:
800       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
801               GTK_CMCELL_PIXTEXT (*cell)->text :
802               GTK_CMCELL_TEXT (*cell)->text);
803
804       if (!text)
805         return NULL;
806       
807       if (!GTK_SCTREE(clist)->use_markup[column]) {
808               layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
809                                                        ((cell->type == GTK_CMCELL_PIXTEXT) ?
810                                                         GTK_CMCELL_PIXTEXT (*cell)->text :
811                                                         GTK_CMCELL_TEXT (*cell)->text));
812               pango_layout_set_font_description (layout, style->font_desc);
813       } else {
814               PangoContext *context = gtk_widget_get_pango_context (GTK_WIDGET(clist));
815               layout = pango_layout_new (context);
816               pango_layout_set_markup (layout, text, -1);
817               pango_layout_set_font_description (layout, style->font_desc);
818               if (clist_row->state == GTK_STATE_SELECTED) {
819                       /* for selected row, we should remove any forced foreground color
820                        * or it looks like shit */
821                       PangoAttrList *list = pango_layout_get_attributes(layout);
822                       PangoAttrList *rem = pango_attr_list_filter(list, filter_fg, NULL);
823                       if (rem)
824                               pango_attr_list_unref(rem);
825               }
826       }
827       
828       return layout;
829       
830     default:
831       return NULL;
832     }
833 }
834
835 static void
836 gtk_sctree_draw_row (GtkCMCList     *clist,
837           GdkRectangle *area,
838           gint          row,
839           GtkCMCListRow  *clist_row)
840 {
841   GtkWidget *widget;
842   GtkCMCTree  *ctree;
843   GdkRectangle *rect;
844   GdkRectangle *crect;
845   GdkRectangle row_rectangle;
846   GdkRectangle cell_rectangle; 
847   GdkRectangle clip_rectangle;
848   GdkRectangle intersect_rectangle;
849   gint last_column;
850   gint column_left = 0;
851   gint column_right = 0;
852   gint offset = 0;
853   gint state;
854   gint i;
855   static GdkColor greybg={0, 0, 0, 0};
856   static gboolean color_change = TRUE;
857
858   if (greybg.pixel == 0 &&
859       greybg.red == 0 &&
860       greybg.green == 0 &&
861       greybg.blue == 0) {
862         GdkColor normalbg = {0, 0xffff, 0xffff, 0xffff};
863         if (GTK_WIDGET (clist)->style) {
864                 normalbg = GTK_WIDGET (clist)->style->base[GTK_STATE_NORMAL];
865         }
866         if (normalbg.red > 0x8888 && normalbg.green > 0x8888 && normalbg.blue > 0x8888) {
867                 greybg.pixel = normalbg.pixel;
868                 greybg.red = normalbg.red - prefs_common.stripes_color_offset;
869                 greybg.green = normalbg.green - prefs_common.stripes_color_offset;
870                 greybg.blue = normalbg.blue - prefs_common.stripes_color_offset;
871         } else if (normalbg.red < 0x8888 && normalbg.green < 0x8888 && normalbg.blue < 0x8888) {
872                 greybg.pixel = normalbg.pixel;
873                 greybg.red = normalbg.red + prefs_common.stripes_color_offset;
874                 greybg.green = normalbg.green + prefs_common.stripes_color_offset;
875                 greybg.blue = normalbg.blue + prefs_common.stripes_color_offset;
876         } else {
877                 color_change = FALSE;
878         }
879   }
880
881   cm_return_if_fail (clist != NULL);
882
883   /* bail now if we arn't drawable yet */
884   if (!GTK_WIDGET_DRAWABLE (clist) || row < 0 || row >= clist->rows)
885     return;
886
887   widget = GTK_WIDGET (clist);
888   ctree  = GTK_CMCTREE  (clist);
889
890   /* if the function is passed the pointer to the row instead of null,
891    * it avoids this expensive lookup */
892   if (!clist_row)
893     clist_row = (g_list_nth (clist->row_list, row))->data;
894
895   /* rectangle of the entire row */
896   row_rectangle.x = 0;
897   row_rectangle.y = ROW_TOP_YPIXEL (clist, row);
898   row_rectangle.width = clist->clist_window_width;
899   row_rectangle.height = clist->row_height;
900
901   /* rectangle of the cell spacing above the row */
902   cell_rectangle.x = 0;
903   cell_rectangle.y = row_rectangle.y - CELL_SPACING;
904   cell_rectangle.width = row_rectangle.width;
905   cell_rectangle.height = CELL_SPACING;
906
907   /* rectangle used to clip drawing operations, its y and height
908    * positions only need to be set once, so we set them once here. 
909    * the x and width are set withing the drawing loop below once per
910    * column */
911   clip_rectangle.y = row_rectangle.y;
912   clip_rectangle.height = row_rectangle.height;
913
914   if (prefs_common.use_stripes_everywhere && GTK_SCTREE(ctree)->show_stripes
915       && color_change && row % 2) {
916     clist_row->background = greybg;
917     clist_row->bg_set = TRUE;
918   } else {
919     clist_row->bg_set = FALSE;
920   }
921   if (clist_row->state == GTK_STATE_NORMAL)
922     {
923       if (clist_row->fg_set)
924         gdk_gc_set_foreground (clist->fg_gc, &clist_row->foreground);
925       if (clist_row->bg_set)
926         gdk_gc_set_rgb_fg_color (clist->bg_gc, &clist_row->background);
927     }
928   
929   state = clist_row->state;
930
931   gdk_gc_set_foreground (ctree->lines_gc,
932                          &widget->style->fg[clist_row->state]);
933
934   /* draw the cell borders */
935   if (area)
936     {
937       rect = &intersect_rectangle;
938       crect = &intersect_rectangle;
939
940       if (gdk_rectangle_intersect (area, &cell_rectangle, crect))
941         gdk_draw_rectangle (clist->clist_window,
942                             widget->style->base_gc[GTK_STATE_NORMAL], TRUE,
943                             crect->x, crect->y, crect->width, crect->height);
944     }
945   else
946     {
947       rect = &clip_rectangle;
948       crect = &cell_rectangle;
949
950       gdk_draw_rectangle (clist->clist_window,
951                           widget->style->base_gc[GTK_STATE_NORMAL], TRUE,
952                           crect->x, crect->y, crect->width, crect->height);
953     }
954
955   /* horizontal black lines */
956   if (ctree->line_style == GTK_CMCTREE_LINES_TABBED)
957     { 
958
959       column_right = (COLUMN_LEFT_XPIXEL (clist, ctree->tree_column) +
960                       clist->column[ctree->tree_column].area.width +
961                       COLUMN_INSET);
962       column_left = (COLUMN_LEFT_XPIXEL (clist, ctree->tree_column) -
963                      COLUMN_INSET - (ctree->tree_column != 0) * CELL_SPACING);
964
965       switch (clist->column[ctree->tree_column].justification)
966         {
967         case GTK_JUSTIFY_CENTER:
968         case GTK_JUSTIFY_FILL:
969         case GTK_JUSTIFY_LEFT:
970           offset = (column_left + ctree->tree_indent *
971                     (((GtkCMCTreeRow *)clist_row)->level - 1));
972
973           gdk_draw_line (clist->clist_window, ctree->lines_gc, 
974                          MIN (offset + TAB_SIZE, column_right),
975                          cell_rectangle.y,
976                          clist->clist_window_width, cell_rectangle.y);
977           break;
978         case GTK_JUSTIFY_RIGHT:
979           offset = (column_right - 1 - ctree->tree_indent *
980                     (((GtkCMCTreeRow *)clist_row)->level - 1));
981
982           gdk_draw_line (clist->clist_window, ctree->lines_gc,
983                          -1, cell_rectangle.y,
984                          MAX (offset - TAB_SIZE, column_left),
985                          cell_rectangle.y);
986           break;
987         }
988     }
989
990   /* the last row has to clear its bottom cell spacing too */
991   if (clist_row == clist->row_list_end->data)
992     {
993       cell_rectangle.y += clist->row_height + CELL_SPACING;
994
995       if (!area || gdk_rectangle_intersect (area, &cell_rectangle, crect))
996         {
997           gdk_draw_rectangle (clist->clist_window,
998                               widget->style->base_gc[GTK_STATE_NORMAL], TRUE,
999                               crect->x, crect->y, crect->width, crect->height);
1000
1001           /* horizontal black lines */
1002           if (ctree->line_style == GTK_CMCTREE_LINES_TABBED)
1003             { 
1004               switch (clist->column[ctree->tree_column].justification)
1005                 {
1006                 case GTK_JUSTIFY_CENTER:
1007                 case GTK_JUSTIFY_FILL:
1008                 case GTK_JUSTIFY_LEFT:
1009                   gdk_draw_line (clist->clist_window, ctree->lines_gc, 
1010                                  MIN (column_left + TAB_SIZE + COLUMN_INSET +
1011                                       (((GtkCMCTreeRow *)clist_row)->level > 1) *
1012                                       MIN (ctree->tree_indent / 2, TAB_SIZE),
1013                                       column_right),
1014                                  cell_rectangle.y,
1015                                  clist->clist_window_width, cell_rectangle.y);
1016                   break;
1017                 case GTK_JUSTIFY_RIGHT:
1018                   gdk_draw_line (clist->clist_window, ctree->lines_gc, 
1019                                  -1, cell_rectangle.y,
1020                                  MAX (column_right - TAB_SIZE - 1 -
1021                                       COLUMN_INSET -
1022                                       (((GtkCMCTreeRow *)clist_row)->level > 1) *
1023                                       MIN (ctree->tree_indent / 2, TAB_SIZE),
1024                                       column_left - 1), cell_rectangle.y);
1025                   break;
1026                 }
1027             }
1028         }
1029     }     
1030
1031   for (last_column = clist->columns - 1;
1032        last_column >= 0 && !clist->column[last_column].visible; last_column--)
1033     ;
1034
1035   /* iterate and draw all the columns (row cells) and draw their contents */
1036   for (i = 0; i < clist->columns; i++)
1037     {
1038       GtkStyle *style;
1039       GdkGC *fg_gc; 
1040       GdkGC *bg_gc;
1041       PangoLayout *layout = NULL;
1042       PangoRectangle logical_rect;
1043
1044       gint width;
1045       gint height;
1046       gint pixbuf_width;
1047       gint string_width;
1048       gint old_offset;
1049
1050       if (!clist->column[i].visible)
1051         continue;
1052
1053       gtk_sctree_get_cell_style (clist, clist_row, state, row, i, &style, &fg_gc, &bg_gc);
1054
1055       /* calculate clipping region */
1056       clip_rectangle.x = clist->column[i].area.x + clist->hoffset;
1057       clip_rectangle.width = clist->column[i].area.width;
1058
1059       cell_rectangle.x = clip_rectangle.x - COLUMN_INSET - CELL_SPACING;
1060       cell_rectangle.width = (clip_rectangle.width + 2 * COLUMN_INSET +
1061                               (1 + (i == last_column)) * CELL_SPACING);
1062       cell_rectangle.y = clip_rectangle.y;
1063       cell_rectangle.height = clip_rectangle.height;
1064
1065       string_width = 0;
1066       pixbuf_width = 0;
1067       height = 0;
1068
1069       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
1070                                             &intersect_rectangle))
1071         {
1072           if (i != ctree->tree_column)
1073             continue;
1074         }
1075       else
1076         {
1077           gdk_draw_rectangle (clist->clist_window, bg_gc, TRUE,
1078                               crect->x, crect->y, crect->width, crect->height);
1079
1080
1081           layout = sc_gtk_cmclist_create_cell_layout (clist, clist_row, i);
1082           if (layout)
1083             {
1084               pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1085               width = logical_rect.width;
1086             }
1087           else
1088             width = 0;
1089
1090           switch (clist_row->cell[i].type)
1091             {
1092             case GTK_CMCELL_PIXBUF:
1093               pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
1094               height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
1095               width += pixbuf_width;
1096               break;
1097             case GTK_CMCELL_PIXTEXT:
1098               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
1099                 {
1100                   pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
1101                   height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
1102                   width += pixbuf_width;
1103                 }
1104
1105               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->text &&
1106                   GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
1107                 width +=  GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
1108
1109               if (i == ctree->tree_column)
1110                 width += (ctree->tree_indent *
1111                           ((GtkCMCTreeRow *)clist_row)->level);
1112               break;
1113             default:
1114               break;
1115             }
1116
1117           switch (clist->column[i].justification)
1118             {
1119             case GTK_JUSTIFY_LEFT:
1120               offset = clip_rectangle.x + clist_row->cell[i].horizontal;
1121               break;
1122             case GTK_JUSTIFY_RIGHT:
1123               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
1124                         clip_rectangle.width - width);
1125               break;
1126             case GTK_JUSTIFY_CENTER:
1127             case GTK_JUSTIFY_FILL:
1128               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
1129                         (clip_rectangle.width / 2) - (width / 2));
1130               break;
1131             };
1132
1133           if (i != ctree->tree_column)
1134             {
1135               int start_y = (clip_rectangle.height - height) / 2;
1136               if (GTK_CMCLIST_ROW_HEIGHT_SET(GTK_CMCLIST(clist)))
1137                       start_y = (clip_rectangle.height/2 - height) / 2;
1138
1139               offset += clist_row->cell[i].horizontal;
1140               switch (clist_row->cell[i].type)
1141                 {
1142                 case GTK_CMCELL_PIXBUF:
1143                   gtk_sctree_draw_cell_pixbuf
1144                     (clist->clist_window, &clip_rectangle, fg_gc,
1145                      GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf,
1146                      offset,
1147                      clip_rectangle.y + clist_row->cell[i].vertical +
1148                      start_y,
1149                      pixbuf_width, height);
1150                   break;
1151                 case GTK_CMCELL_PIXTEXT:
1152                   offset = gtk_sctree_draw_cell_pixbuf
1153                     (clist->clist_window, &clip_rectangle, fg_gc,
1154                      GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
1155                      offset,
1156                      clip_rectangle.y + clist_row->cell[i].vertical +
1157                      start_y,
1158                      pixbuf_width, height);
1159                   offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
1160
1161                   /* Fall through */
1162                 case GTK_CMCELL_TEXT:
1163                   if (layout)
1164                     {
1165                       gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
1166
1167                       gdk_gc_set_clip_rectangle (fg_gc, &clip_rectangle);
1168                       gdk_draw_layout (clist->clist_window, fg_gc,
1169                                        offset,
1170                                        row_rectangle.y + row_center_offset + clist_row->cell[i].vertical,
1171                                        layout);
1172                       gdk_gc_set_clip_rectangle (fg_gc, NULL);
1173                       g_object_unref (G_OBJECT (layout));
1174                     }
1175                   break;
1176                 default:
1177                   break;
1178                 }
1179               continue;
1180             }
1181         }
1182
1183       if (bg_gc == clist->bg_gc)
1184         gdk_gc_set_background (ctree->lines_gc, &clist_row->background);
1185
1186       /* draw ctree->tree_column */
1187       cell_rectangle.y -= CELL_SPACING;
1188       cell_rectangle.height += CELL_SPACING;
1189
1190       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
1191                                             &intersect_rectangle))
1192         {
1193           if (layout)
1194             g_object_unref (G_OBJECT (layout));
1195           continue;
1196         }
1197
1198       /* draw lines */
1199       offset = gtk_sctree_draw_lines (ctree, (GtkCMCTreeRow *)clist_row, row, i,
1200                                      state, &clip_rectangle, &cell_rectangle,
1201                                      crect, area, style);
1202
1203       /* draw expander */
1204       offset = gtk_sctree_draw_expander (ctree, (GtkCMCTreeRow *)clist_row,
1205                                         style, &clip_rectangle, offset);
1206
1207       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
1208         offset -= ctree->tree_spacing;
1209       else
1210         offset += ctree->tree_spacing;
1211
1212       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
1213         offset -= (pixbuf_width + clist_row->cell[i].horizontal);
1214       else
1215         offset += clist_row->cell[i].horizontal;
1216
1217       old_offset = offset;
1218       offset = gtk_sctree_draw_cell_pixbuf (clist->clist_window, &clip_rectangle, fg_gc,
1219                                  GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
1220                                  offset, 
1221                                  clip_rectangle.y + clist_row->cell[i].vertical
1222                                  + (clip_rectangle.height - height) / 2,
1223                                  pixbuf_width, height);
1224
1225       if (layout)
1226         {
1227           gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
1228           
1229           if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
1230             {
1231               offset = (old_offset - string_width);
1232               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
1233                 offset -= GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
1234             }
1235           else
1236             {
1237               if (GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf)
1238                 offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
1239             }
1240           
1241           gdk_gc_set_clip_rectangle (fg_gc, &clip_rectangle);
1242           gdk_draw_layout (clist->clist_window, fg_gc,
1243                            offset,
1244                            row_rectangle.y + row_center_offset + clist_row->cell[i].vertical,
1245                            layout);
1246
1247           g_object_unref (G_OBJECT (layout));
1248         }
1249       gdk_gc_set_clip_rectangle (fg_gc, NULL);
1250     }
1251
1252   /* draw focus rectangle */
1253   if (clist->focus_row == row &&
1254       GTK_WIDGET_CAN_FOCUS (widget) && GTK_WIDGET_HAS_FOCUS (widget))
1255     {
1256       if (!area)
1257         gdk_draw_rectangle (clist->clist_window, clist->xor_gc, FALSE,
1258                             row_rectangle.x, row_rectangle.y,
1259                             row_rectangle.width - 1, row_rectangle.height - 1);
1260       else if (gdk_rectangle_intersect (area, &row_rectangle,
1261                                         &intersect_rectangle))
1262         {
1263           gdk_gc_set_clip_rectangle (clist->xor_gc, &intersect_rectangle);
1264           gdk_draw_rectangle (clist->clist_window, clist->xor_gc, FALSE,
1265                               row_rectangle.x, row_rectangle.y,
1266                               row_rectangle.width - 1,
1267                               row_rectangle.height - 1);
1268           gdk_gc_set_clip_rectangle (clist->xor_gc, NULL);
1269         }
1270     }
1271 }
1272
1273 static void
1274 gtk_sctree_change_focus_row_expansion (GtkCMCTree          *ctree,
1275                             GtkCMCTreeExpansionType action)
1276 {
1277   GtkCMCList *clist;
1278   GtkCMCTreeNode *node;
1279
1280   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1281
1282   clist = GTK_CMCLIST (ctree);
1283
1284   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (ctree))) && 
1285       GTK_WIDGET_HAS_GRAB (ctree))
1286     return;
1287   
1288   if (!(node =
1289         GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row))) ||
1290       GTK_CMCTREE_ROW (node)->is_leaf || !(GTK_CMCTREE_ROW (node)->children))
1291     return;
1292
1293   switch (action)
1294     {
1295     case GTK_CMCTREE_EXPANSION_EXPAND:
1296       if (GTK_SCTREE(ctree)->always_expand_recursively)
1297               gtk_cmctree_expand_recursive (ctree, node);
1298       else
1299               gtk_cmctree_expand (ctree, node);
1300
1301       break;
1302     case GTK_CMCTREE_EXPANSION_EXPAND_RECURSIVE:
1303       gtk_cmctree_expand_recursive (ctree, node);
1304       break;
1305     case GTK_CMCTREE_EXPANSION_COLLAPSE:
1306       gtk_cmctree_collapse (ctree, node);
1307       break;
1308     case GTK_CMCTREE_EXPANSION_COLLAPSE_RECURSIVE:
1309       gtk_cmctree_collapse_recursive (ctree, node);
1310       break;
1311     case GTK_CMCTREE_EXPANSION_TOGGLE:
1312       if (GTK_SCTREE(ctree)->always_expand_recursively)
1313               gtk_cmctree_toggle_expansion_recursive (ctree, node);
1314       else
1315               gtk_cmctree_toggle_expansion (ctree, node);
1316       break;
1317     case GTK_CMCTREE_EXPANSION_TOGGLE_RECURSIVE:
1318       gtk_cmctree_toggle_expansion_recursive (ctree, node);
1319       break;
1320     }
1321 }
1322
1323 static void gtk_sctree_finalize(GObject *object)
1324 {
1325         GtkSCTree *sctree = GTK_SCTREE(object);
1326         g_free(sctree->use_markup);
1327         sctree->use_markup = NULL;
1328         G_OBJECT_CLASS (parent_class)->finalize (object);
1329 }
1330
1331 /* Standard class initialization function */
1332 static void
1333 gtk_sctree_class_init (GtkSCTreeClass *klass)
1334 {
1335         GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1336         GtkObjectClass *object_class;
1337         GtkWidgetClass *widget_class;
1338         GtkCMCListClass *clist_class;
1339         GtkCMCTreeClass *ctree_class;
1340
1341         object_class = (GtkObjectClass *) klass;
1342         widget_class = (GtkWidgetClass *) klass;
1343         clist_class = (GtkCMCListClass *) klass;
1344         ctree_class = (GtkCMCTreeClass *) klass;
1345
1346         parent_class = g_type_class_peek (gtk_cmctree_get_type ());
1347
1348         sctree_signals[ROW_POPUP_MENU] =
1349                 g_signal_new ("row_popup_menu",
1350                               G_TYPE_FROM_CLASS (klass),
1351                               G_SIGNAL_RUN_FIRST,
1352                               G_STRUCT_OFFSET (GtkSCTreeClass, row_popup_menu),
1353                               NULL, NULL,
1354                               claws_marshal_VOID__POINTER,
1355                               G_TYPE_NONE, 1,
1356                               GDK_TYPE_EVENT);
1357         sctree_signals[EMPTY_POPUP_MENU] =
1358                 g_signal_new ("empty_popup_menu",
1359                               G_TYPE_FROM_CLASS (klass),
1360                               G_SIGNAL_RUN_FIRST,
1361                               G_STRUCT_OFFSET (GtkSCTreeClass, empty_popup_menu),
1362                               NULL, NULL,
1363                               claws_marshal_VOID__POINTER,
1364                               G_TYPE_NONE, 1,
1365                               GDK_TYPE_EVENT);
1366         sctree_signals[OPEN_ROW] =
1367                 g_signal_new ("open_row",
1368                               G_TYPE_FROM_CLASS (klass),
1369                               G_SIGNAL_RUN_FIRST,
1370                               G_STRUCT_OFFSET (GtkSCTreeClass, open_row),
1371                               NULL, NULL,
1372                               g_cclosure_marshal_VOID__VOID,
1373                               G_TYPE_NONE, 0);
1374         sctree_signals[START_DRAG] =
1375                 g_signal_new ("start_drag",
1376                               G_TYPE_FROM_CLASS (klass),
1377                               G_SIGNAL_RUN_FIRST,
1378                               G_STRUCT_OFFSET (GtkSCTreeClass, start_drag),
1379                               NULL, NULL,
1380                               claws_marshal_VOID__INT_POINTER,
1381                               G_TYPE_NONE, 2,
1382                               G_TYPE_INT,
1383                               GDK_TYPE_EVENT);
1384
1385         /* gtk_object_class_add_signals (object_class, sctree_signals, LAST_SIGNAL); */
1386
1387         clist_class->clear = gtk_sctree_clear;
1388         clist_class->draw_row = gtk_sctree_draw_row;
1389         clist_class->unselect_all = gtk_sctree_real_unselect_all;
1390         ctree_class->tree_collapse = gtk_sctree_real_tree_collapse;
1391         ctree_class->tree_expand = gtk_sctree_real_tree_expand;
1392         ctree_class->tree_move = sreal_tree_move;
1393         ctree_class->change_focus_row_expansion = gtk_sctree_change_focus_row_expansion;
1394         
1395         widget_class->button_press_event = gtk_sctree_button_press;
1396         widget_class->button_release_event = gtk_sctree_button_release;
1397         widget_class->motion_notify_event = gtk_sctree_motion;
1398         widget_class->drag_begin = gtk_sctree_drag_begin;
1399         widget_class->drag_end = gtk_sctree_drag_end;
1400         widget_class->drag_data_get = gtk_sctree_drag_data_get;
1401         widget_class->drag_leave = gtk_sctree_drag_leave;
1402         widget_class->drag_motion = gtk_sctree_drag_motion;
1403         widget_class->drag_drop = gtk_sctree_drag_drop;
1404         widget_class->drag_data_received = gtk_sctree_drag_data_received;
1405         
1406         gobject_class->finalize = gtk_sctree_finalize;
1407 }
1408
1409 /* Standard object initialization function */
1410 static void
1411 gtk_sctree_init (GtkSCTree *sctree)
1412 {
1413         sctree->anchor_row = NULL;
1414
1415         /* GtkCMCTree does not specify pointer motion by default */
1416         gtk_widget_add_events (GTK_WIDGET (sctree), GDK_POINTER_MOTION_MASK);
1417         gtk_widget_add_events (GTK_WIDGET (sctree), GDK_POINTER_MOTION_MASK);
1418 }
1419
1420 /* Get information the specified row is selected. */
1421
1422 static gboolean
1423 row_is_selected(GtkSCTree *sctree, gint row)
1424 {
1425         GtkCMCListRow *clist_row;
1426         clist_row =  g_list_nth (GTK_CMCLIST(sctree)->row_list, row)->data;
1427         return clist_row ? clist_row->state == GTK_STATE_SELECTED : FALSE;
1428 }
1429
1430 /* Selects the rows between the anchor to the specified row, inclusive.  */
1431 static void
1432 select_range (GtkSCTree *sctree, gint row)
1433 {
1434         gint prev_row;
1435         gint min, max;
1436         gint i;
1437         GList *node;
1438         if (sctree->anchor_row == NULL) {
1439                 prev_row = row;
1440                 sctree->anchor_row = gtk_cmctree_node_nth(GTK_CMCTREE(sctree), row);
1441         } else
1442                 prev_row = g_list_position(GTK_CMCLIST(sctree)->row_list,
1443                                            (GList *)sctree->anchor_row);
1444
1445         if (row < prev_row) {
1446                 min = row;
1447                 max = prev_row;
1448                 GTK_CMCLIST(sctree)->focus_row = max;
1449         } else {
1450                 min = prev_row;
1451                 max = row;
1452         }
1453         sctree->selecting_range++;
1454         
1455         if (max < min) {
1456                 int t = min;
1457                 min = max;
1458                 max = t;
1459         }
1460         
1461         if (max - min > 10)
1462                 gtk_cmclist_freeze(GTK_CMCLIST(sctree));
1463
1464         node = g_list_nth((GTK_CMCLIST(sctree))->row_list, min);
1465         for (i = min; i < max; i++) {
1466                 if (node && GTK_CMCTREE_ROW (node)->row.selectable) {
1467                         g_signal_emit_by_name(G_OBJECT(sctree), "tree_select_row",
1468                                 node, -1);
1469                 }
1470                 node = node->next;
1471         }
1472         if (max - min > 10)
1473                 gtk_cmclist_thaw(GTK_CMCLIST(sctree));
1474
1475
1476         sctree->selecting_range--;
1477         gtk_cmclist_select_row (GTK_CMCLIST (sctree), max, -1);
1478 }
1479
1480 /* Handles row selection according to the specified modifier state */
1481 /* in certain cases, we arrive here from a function knowing the GtkCMCTreeNode, and having
1482  * already slowly found row using g_list_position. In which case, _node will be non-NULL
1483  * to avoid this function having to slowly find it with g_list_nth. */
1484 static void
1485 select_row (GtkSCTree *sctree, gint row, gint col, guint state, GtkCMCTreeNode *_node)
1486 {
1487         gboolean range, additive;
1488         cm_return_if_fail (sctree != NULL);
1489         cm_return_if_fail (GTK_IS_SCTREE (sctree));
1490     
1491         range = ((state & GDK_SHIFT_MASK) != 0) &&
1492                 (GTK_CMCLIST(sctree)->selection_mode != GTK_SELECTION_SINGLE) &&
1493                 (GTK_CMCLIST(sctree)->selection_mode != GTK_SELECTION_BROWSE);
1494         additive = ((state & GDK_CONTROL_MASK) != 0) &&
1495                    (GTK_CMCLIST(sctree)->selection_mode != GTK_SELECTION_SINGLE) &&
1496                    (GTK_CMCLIST(sctree)->selection_mode != GTK_SELECTION_BROWSE);
1497
1498         if (!range && !additive && sctree->force_additive_sel)
1499                 additive = TRUE;
1500
1501         GTK_CMCLIST(sctree)->focus_row = row;
1502
1503         if (!additive) {
1504                 gtk_cmclist_unselect_all (GTK_CMCLIST (sctree));
1505         }
1506
1507         if (!range) {
1508                 GtkCMCTreeNode *node;
1509
1510                 node = _node ? _node : gtk_cmctree_node_nth (GTK_CMCTREE(sctree), row);
1511
1512                 /*No need to manage overlapped list*/
1513                 if (additive) {
1514                         if (row_is_selected(sctree, row))
1515                                 gtk_cmclist_unselect_row (GTK_CMCLIST (sctree), row, col);
1516                         else
1517                                 g_signal_emit_by_name
1518                                         (G_OBJECT (sctree),
1519                                          "tree_select_row", node, col);
1520                 } else {
1521                         g_signal_emit_by_name
1522                                 (G_OBJECT (sctree),
1523                                  "tree_select_row", node, col);
1524                 }
1525                 sctree->anchor_row = node;
1526         } else
1527                 select_range (sctree, row);
1528 }
1529
1530 static gboolean
1531 sctree_is_hot_spot (GtkSCTree     *sctree, 
1532                    GtkCMCTreeNode *node,
1533                    gint          row, 
1534                    gint          x, 
1535                    gint          y)
1536 {
1537   GtkCMCTreeRow *tree_row;
1538   GtkCMCList *clist;
1539   GtkCMCTree *ctree;
1540   GtkCMCellPixText *cell;
1541   gint xl, xmax;
1542   gint yu;
1543   
1544   cm_return_val_if_fail (GTK_IS_SCTREE (sctree), FALSE);
1545   cm_return_val_if_fail (node != NULL, FALSE);
1546
1547   clist = GTK_CMCLIST (sctree);
1548   ctree = GTK_CMCTREE (sctree);
1549
1550   if (!clist->column[ctree->tree_column].visible ||
1551       ctree->expander_style == GTK_CMCTREE_EXPANDER_NONE)
1552     return FALSE;
1553
1554   tree_row = GTK_CMCTREE_ROW (node);
1555
1556   cell = GTK_CMCELL_PIXTEXT (tree_row->row.cell[ctree->tree_column]);
1557
1558   if (!GTK_CMCLIST_ROW_HEIGHT_SET(GTK_CMCLIST(clist)))
1559      yu = (ROW_TOP_YPIXEL (clist, row) + (clist->row_height - PM_SIZE) / 2 -
1560         (clist->row_height - 1) % 2);
1561   else
1562      yu = (ROW_TOP_YPIXEL (clist, row) + (clist->row_height/2 - PM_SIZE) / 2 -
1563         (clist->row_height/2 - 1) % 2);
1564
1565 #ifndef GENERIC_UMPC
1566   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT)
1567     xl = (clist->column[ctree->tree_column].area.x + 
1568           clist->column[ctree->tree_column].area.width - 1 + clist->hoffset -
1569           (tree_row->level - 1) * ctree->tree_indent - PM_SIZE -
1570           (ctree->line_style == GTK_CMCTREE_LINES_TABBED) * 3);
1571   else
1572     xl = (clist->column[ctree->tree_column].area.x + clist->hoffset +
1573           (tree_row->level - 1) * ctree->tree_indent +
1574           (ctree->line_style == GTK_CMCTREE_LINES_TABBED) * 3);
1575
1576   xmax = xl + PM_SIZE;
1577 #else
1578   if (clist->column[ctree->tree_column].justification == GTK_JUSTIFY_RIGHT) {
1579     xl = (clist->column[ctree->tree_column].area.x + 
1580           clist->column[ctree->tree_column].area.width - 1 + clist->hoffset -
1581           (tree_row->level - 1) * ctree->tree_indent - PM_SIZE -
1582           (ctree->line_style == GTK_CMCTREE_LINES_TABBED) * 3);
1583     xmax = xl + PM_SIZE;
1584   } else if (ctree->tree_column == 0) {
1585     xl = (clist->column[ctree->tree_column].area.x + clist->hoffset +
1586           (ctree->line_style == GTK_CMCTREE_LINES_TABBED) * 3);
1587     xmax = (clist->column[ctree->tree_column].area.x + clist->hoffset +
1588            (tree_row->level - 1) * ctree->tree_indent +
1589            (ctree->line_style == GTK_CMCTREE_LINES_TABBED) * 3) +
1590            PM_SIZE;
1591   } else {
1592     xl = (clist->column[ctree->tree_column].area.x + clist->hoffset +
1593           (tree_row->level - 1) * ctree->tree_indent +
1594           (ctree->line_style == GTK_CMCTREE_LINES_TABBED) * 3);
1595     xmax = xl + PM_SIZE;
1596   }
1597 #endif
1598   return (x >= xl && x <= xmax && y >= yu && y <= yu + PM_SIZE);
1599 }
1600
1601 gboolean
1602 gtk_sctree_is_hot_spot (GtkSCTree *ctree, 
1603                        gint      x, 
1604                        gint      y)
1605 {
1606   GtkCMCTreeNode *node;
1607   gint column;
1608   gint row;
1609   
1610   cm_return_val_if_fail (GTK_IS_SCTREE (ctree), FALSE);
1611
1612   if (gtk_cmclist_get_selection_info (GTK_CMCLIST (ctree), x, y, &row, &column))
1613     if ((node = GTK_CMCTREE_NODE(g_list_nth (GTK_CMCLIST (ctree)->row_list, row))))
1614       return sctree_is_hot_spot (ctree, node, row, x, y);
1615
1616   return FALSE;
1617 }
1618
1619 /* Our handler for button_press events.  We override all of GtkCMCList's broken
1620  * behavior.
1621  */
1622 static gint
1623 gtk_sctree_button_press (GtkWidget *widget, GdkEventButton *event)
1624 {
1625         GtkSCTree *sctree;
1626         GtkCMCList *clist;
1627         gboolean on_row;
1628         gint row;
1629         gint col;
1630         gint retval;
1631
1632         cm_return_val_if_fail (widget != NULL, FALSE);
1633         cm_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1634         cm_return_val_if_fail (event != NULL, FALSE);
1635
1636         sctree = GTK_SCTREE (widget);
1637         clist = GTK_CMCLIST (widget);
1638         retval = FALSE;
1639
1640         if (event->window != clist->clist_window)
1641                 return (* GTK_WIDGET_CLASS (parent_class)->button_press_event) (widget, event);
1642
1643         on_row = gtk_cmclist_get_selection_info (clist, event->x, event->y, &row, &col);
1644
1645         if (on_row && !GTK_WIDGET_HAS_FOCUS(widget))
1646                 gtk_widget_grab_focus (widget);
1647
1648         if (gtk_sctree_is_hot_spot (GTK_SCTREE(sctree), event->x, event->y)) {
1649                 GtkCMCTreeNode *node = gtk_cmctree_node_nth(GTK_CMCTREE(sctree), row);
1650                 if (GTK_CMCTREE_ROW (node)->expanded)
1651                         gtk_cmctree_collapse(GTK_CMCTREE(sctree), node);
1652                 else if (GTK_SCTREE(sctree)->always_expand_recursively)
1653                         gtk_cmctree_expand_recursive (GTK_CMCTREE(sctree), node);
1654                 else
1655                         gtk_cmctree_expand(GTK_CMCTREE(sctree), node);
1656                 return TRUE;
1657         }
1658
1659         switch (event->type) {
1660         case GDK_BUTTON_PRESS:
1661                 if (event->button == 1 || event->button == 2) {
1662                         if (event->button == 2)
1663                                 event->state &= ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK);
1664                         if (on_row) {
1665                                 /* Save the mouse info for DnD */
1666                                 sctree->dnd_press_button = event->button;
1667                                 sctree->dnd_press_x = event->x;
1668                                 sctree->dnd_press_y = event->y;
1669
1670                                 /* Handle selection */
1671                                 if ((row_is_selected (sctree, row)
1672                                      && !(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)))
1673                                     || ((event->state & GDK_CONTROL_MASK)
1674                                         && !(event->state & GDK_SHIFT_MASK))) {
1675                                         sctree->dnd_select_pending = TRUE;
1676                                         sctree->dnd_select_pending_state = event->state;
1677                                         sctree->dnd_select_pending_row = row;
1678                                 } else {
1679                                         select_row (sctree, row, col, event->state, NULL);
1680                                 }
1681                         } else {
1682                                 gtk_cmclist_unselect_all (clist);
1683                         }
1684
1685                         retval = TRUE;
1686                 } else if (event->button == 3) {
1687                         /* Emit *_popup_menu signal*/
1688                         if (on_row) {
1689                                 if (!row_is_selected(sctree,row))
1690                                         select_row (sctree, row, col, 0, NULL);
1691                                 g_signal_emit (G_OBJECT (sctree),
1692                                                  sctree_signals[ROW_POPUP_MENU],
1693                                                  0, event);
1694                         } else {
1695                                 gtk_cmclist_unselect_all(clist);
1696                                 g_signal_emit (G_OBJECT (sctree),
1697                                                  sctree_signals[EMPTY_POPUP_MENU],
1698                                                  0, event);
1699                         }
1700                         retval = TRUE;
1701                 }
1702
1703                 break;
1704
1705         case GDK_2BUTTON_PRESS:
1706                 if (event->button != 1)
1707                         break;
1708
1709                 sctree->dnd_select_pending = FALSE;
1710                 sctree->dnd_select_pending_state = 0;
1711
1712                 if (on_row)
1713                         g_signal_emit (G_OBJECT (sctree),
1714                                        sctree_signals[OPEN_ROW], 0);
1715
1716                 retval = TRUE;
1717                 break;
1718
1719         default:
1720                 break;
1721         }
1722
1723         return retval;
1724 }
1725
1726 /* Our handler for button_release events.  We override all of GtkCMCList's broken
1727  * behavior.
1728  */
1729 static gint
1730 gtk_sctree_button_release (GtkWidget *widget, GdkEventButton *event)
1731 {
1732         GtkSCTree *sctree;
1733         GtkCMCList *clist;
1734         gint on_row;
1735         gint row, col;
1736         gint retval;
1737
1738         cm_return_val_if_fail (widget != NULL, FALSE);
1739         cm_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1740         cm_return_val_if_fail (event != NULL, FALSE);
1741
1742         sctree = GTK_SCTREE (widget);
1743         clist = GTK_CMCLIST (widget);
1744         retval = FALSE;
1745
1746         if (event->window != clist->clist_window)
1747                 return (* GTK_WIDGET_CLASS (parent_class)->button_release_event) (widget, event);
1748
1749         on_row = gtk_cmclist_get_selection_info (clist, event->x, event->y, &row, &col);
1750
1751         if (!(event->button == 1 || event->button == 2))
1752                 return FALSE;
1753
1754         sctree->dnd_press_button = 0;
1755         sctree->dnd_press_x = 0;
1756         sctree->dnd_press_y = 0;
1757
1758         if (on_row) {
1759                 if (sctree->dnd_select_pending) {
1760                         select_row (sctree, row, col, sctree->dnd_select_pending_state, NULL);
1761                         sctree->dnd_select_pending = FALSE;
1762                         sctree->dnd_select_pending_state = 0;
1763                 }
1764
1765                 retval = TRUE;
1766         }
1767
1768         return retval;
1769 }
1770
1771 /* Our handler for motion_notify events.  We override all of GtkCMCList's broken
1772  * behavior.
1773  */
1774 static gint
1775 gtk_sctree_motion (GtkWidget *widget, GdkEventMotion *event)
1776 {
1777         GtkSCTree *sctree;
1778         GtkCMCList *clist;
1779
1780         cm_return_val_if_fail (widget != NULL, FALSE);
1781         cm_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1782         cm_return_val_if_fail (event != NULL, FALSE);
1783
1784         sctree = GTK_SCTREE (widget);
1785         clist = GTK_CMCLIST (widget);
1786
1787         if (event->window != clist->clist_window)
1788                 return (* GTK_WIDGET_CLASS (parent_class)->motion_notify_event) (widget, event);
1789
1790         if (!((sctree->dnd_press_button == 1 && (event->state & GDK_BUTTON1_MASK))
1791               || (sctree->dnd_press_button == 2 && (event->state & GDK_BUTTON2_MASK))))
1792                 return FALSE;
1793
1794         /* This is the same threshold value that is used in gtkdnd.c */
1795
1796 #ifndef GENERIC_UMPC
1797 #define THRESHOLD 3
1798 #else
1799 #define THRESHOLD 8
1800 #endif
1801         if (MAX (ABS (sctree->dnd_press_x - event->x),
1802                  ABS (sctree->dnd_press_y - event->y)) <= THRESHOLD)
1803                 return FALSE;
1804
1805         /* Handle any pending selections */
1806
1807         if (sctree->dnd_select_pending) {
1808                 if (!row_is_selected(sctree,sctree->dnd_select_pending_row))
1809                         select_row (sctree,
1810                                     sctree->dnd_select_pending_row,
1811                                     -1,
1812                                     sctree->dnd_select_pending_state,
1813                                     NULL);
1814
1815                 sctree->dnd_select_pending = FALSE;
1816                 sctree->dnd_select_pending_state = 0;
1817         }
1818
1819         g_signal_emit (G_OBJECT (sctree),
1820                        sctree_signals[START_DRAG],
1821                        0,
1822                        sctree->dnd_press_button,
1823                        event);
1824         return TRUE;
1825 }
1826
1827 /* We override the drag_begin signal to do nothing */
1828 static void
1829 gtk_sctree_drag_begin (GtkWidget *widget, GdkDragContext *context)
1830 {
1831         /* nothing */
1832 }
1833
1834 /* We override the drag_end signal to do nothing */
1835 static void
1836 gtk_sctree_drag_end (GtkWidget *widget, GdkDragContext *context)
1837 {
1838         /* nothing */
1839 }
1840
1841 /* We override the drag_data_get signal to do nothing */
1842 static void
1843 gtk_sctree_drag_data_get (GtkWidget *widget, GdkDragContext *context,
1844                                      GtkSelectionData *data, guint info, guint time)
1845 {
1846         /* nothing */
1847 }
1848
1849 /* We override the drag_leave signal to do nothing */
1850 static void
1851 gtk_sctree_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time)
1852 {
1853         /* nothing */
1854 }
1855
1856 /* We override the drag_motion signal to do nothing */
1857 static gboolean
1858 gtk_sctree_drag_motion (GtkWidget *widget, GdkDragContext *context,
1859                                    gint x, gint y, guint time)
1860 {
1861         return FALSE;
1862 }
1863
1864 /* We override the drag_drop signal to do nothing */
1865 static gboolean
1866 gtk_sctree_drag_drop (GtkWidget *widget, GdkDragContext *context,
1867                                  gint x, gint y, guint time)
1868 {
1869         return FALSE;
1870 }
1871
1872 /* We override the drag_data_received signal to do nothing */
1873 static void
1874 gtk_sctree_drag_data_received (GtkWidget *widget, GdkDragContext *context,
1875                                           gint x, gint y, GtkSelectionData *data,
1876                                           guint info, guint time)
1877 {
1878         /* nothing */
1879 }
1880
1881 /* Our handler for the clear signal of the clist.  We have to reset the anchor
1882  * to null.
1883  */
1884 static void
1885 gtk_sctree_clear (GtkCMCList *clist)
1886 {
1887         GtkSCTree *sctree;
1888
1889         cm_return_if_fail (clist != NULL);
1890         cm_return_if_fail (GTK_IS_SCTREE (clist));
1891
1892         sctree = GTK_SCTREE (clist);
1893         sctree->anchor_row = NULL;
1894
1895         if (((GtkCMCListClass *)parent_class)->clear)
1896                 (* ((GtkCMCListClass *)parent_class)->clear) (clist);
1897 }
1898
1899 static void
1900 gtk_sctree_real_unselect_all (GtkCMCList *clist)
1901 {
1902         GtkSCTree *sctree;
1903         gboolean should_freeze = FALSE;
1904
1905         cm_return_if_fail (clist != NULL);
1906         cm_return_if_fail (GTK_IS_SCTREE (clist));
1907
1908         sctree = GTK_SCTREE (clist);
1909
1910         if (sc_g_list_bigger(GTK_CMCLIST(sctree)->selection, 10)) {
1911                 should_freeze = TRUE;
1912                 sctree->selecting_range++;
1913                 gtk_cmclist_freeze (GTK_CMCLIST (sctree));
1914         }
1915
1916         if (((GtkCMCListClass *)parent_class)->unselect_all)
1917                 (* ((GtkCMCListClass *)parent_class)->unselect_all) (clist);
1918
1919         if (should_freeze) {
1920                 gtk_cmclist_thaw (GTK_CMCLIST (sctree));
1921                 sctree->selecting_range--;
1922         }
1923 }
1924
1925 static void
1926 gtk_sctree_column_auto_resize (GtkCMCList    *clist,
1927                     GtkCMCListRow *clist_row,
1928                     gint         column,
1929                     gint         old_width)
1930 {
1931   /* resize column if needed for auto_resize */
1932   GtkRequisition requisition;
1933
1934   if (!clist->column[column].auto_resize ||
1935       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1936     return;
1937
1938   if (clist_row)
1939     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
1940                                                    column, &requisition);
1941   else
1942     requisition.width = 0;
1943
1944   if (requisition.width > clist->column[column].width)
1945     gtk_cmclist_set_column_width (clist, column, requisition.width);
1946   else if (requisition.width < old_width &&
1947            old_width == clist->column[column].width)
1948     {
1949       GList *list;
1950       gint new_width;
1951
1952       /* run a "gtk_cmclist_optimal_column_width" but break, if
1953        * the column doesn't shrink */
1954       if (GTK_CMCLIST_SHOW_TITLES (clist) && clist->column[column].button)
1955         new_width = (clist->column[column].button->requisition.width -
1956                      (CELL_SPACING + (2 * COLUMN_INSET)));
1957       else
1958         new_width = 0;
1959
1960       for (list = clist->row_list; list; list = list->next)
1961         {
1962           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1963             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1964           new_width = MAX (new_width, requisition.width);
1965           if (new_width == clist->column[column].width)
1966             break;
1967         }
1968       if (new_width < clist->column[column].width)
1969         gtk_cmclist_set_column_width (clist, column, new_width);
1970     }
1971 }
1972
1973 static void
1974 gtk_sctree_auto_resize_columns (GtkCMCList *clist)
1975 {
1976   gint i;
1977
1978   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1979     return;
1980
1981   for (i = 0; i < clist->columns; i++)
1982     gtk_sctree_column_auto_resize (clist, NULL, i, clist->column[i].width);
1983 }
1984
1985 static void 
1986 gtk_sctree_real_tree_collapse (GtkCMCTree     *ctree,
1987                     GtkCMCTreeNode *node)
1988 {
1989   GtkCMCList *clist;
1990   GtkCMCTreeNode *work;
1991   GtkRequisition requisition;
1992   gboolean visible;
1993   gint level;
1994
1995   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
1996
1997   if (!node || !GTK_CMCTREE_ROW (node)->expanded ||
1998       GTK_CMCTREE_ROW (node)->is_leaf)
1999     return;
2000
2001   clist = GTK_CMCLIST (ctree);
2002
2003   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2004   
2005   GTK_CMCTREE_ROW (node)->expanded = FALSE;
2006   level = GTK_CMCTREE_ROW (node)->level;
2007
2008   visible = gtk_cmctree_is_viewable (ctree, node);
2009   /* get cell width if tree_column is auto resized */
2010   if (visible && clist->column[ctree->tree_column].auto_resize &&
2011       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2012     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2013       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
2014
2015   /* unref/unset opened pixbuf */
2016   if (GTK_CMCELL_PIXTEXT 
2017       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
2018     {
2019       g_object_unref
2020         (GTK_CMCELL_PIXTEXT
2021          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
2022       
2023       GTK_CMCELL_PIXTEXT
2024         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
2025     }
2026
2027   /* set/ref closed pixbuf */
2028   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2029     {
2030       GTK_CMCELL_PIXTEXT 
2031         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
2032         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2033     }
2034
2035   work = GTK_CMCTREE_ROW (node)->children;
2036   if (work)
2037     {
2038       gint tmp = 0;
2039       gint row;
2040       GList *list;
2041
2042       while (work && GTK_CMCTREE_ROW (work)->level > level)
2043         {
2044           work = GTK_CMCTREE_NODE_NEXT (work);
2045           tmp++;
2046         }
2047
2048       if (work)
2049         {
2050           list = (GList *)node;
2051           list->next = (GList *)work;
2052           list = (GList *)GTK_CMCTREE_NODE_PREV (work);
2053           list->next = NULL;
2054           list = (GList *)work;
2055           list->prev = (GList *)node;
2056         }
2057       else
2058         {
2059           list = (GList *)node;
2060           list->next = NULL;
2061           clist->row_list_end = (GList *)node;
2062         }
2063
2064       if (visible)
2065         {
2066           /* resize auto_resize columns if needed */
2067           gtk_sctree_auto_resize_columns (clist);
2068
2069           if (!GTK_SCTREE(clist)->sorting) {
2070                   row = g_list_position (clist->row_list, (GList *)node);
2071                   if (row < clist->focus_row)
2072                     clist->focus_row -= tmp;
2073           }
2074           clist->rows -= tmp;
2075           CLIST_REFRESH (clist);
2076         }
2077     }
2078   else if (visible && clist->column[ctree->tree_column].auto_resize &&
2079            !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2080     /* resize tree_column if needed */
2081     gtk_sctree_column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
2082                         requisition.width);
2083     
2084 }
2085
2086
2087 GtkWidget *gtk_sctree_new_with_titles (gint columns, gint tree_column, 
2088                                        gchar *titles[])
2089 {
2090         GtkWidget *widget;
2091                                                                                                             
2092         cm_return_val_if_fail (columns > 0, NULL);
2093         cm_return_val_if_fail (tree_column >= 0, NULL);
2094         
2095         if (tree_column >= columns) {
2096                 g_warning("Wrong tree column");
2097                 tree_column = 0;
2098                 print_backtrace();
2099         }
2100         
2101         widget = gtk_widget_new (TYPE_GTK_SCTREE,
2102                                  "n_columns", columns,
2103                                  "tree_column", tree_column,
2104                                  NULL);
2105         if (titles) {
2106                 GtkCMCList *clist = GTK_CMCLIST (widget);
2107                 guint i;
2108
2109                 for (i = 0; i < columns; i++)
2110                         gtk_cmclist_set_column_title (clist, i, titles[i]);
2111                 gtk_cmclist_column_titles_show (clist);
2112         }
2113
2114         GTK_SCTREE(widget)->show_stripes = TRUE;
2115         GTK_SCTREE(widget)->always_expand_recursively = TRUE;
2116         GTK_SCTREE(widget)->force_additive_sel = FALSE;
2117         
2118         GTK_SCTREE(widget)->use_markup = g_new0(gboolean, columns);
2119
2120         return widget;
2121 }
2122
2123 void gtk_sctree_set_use_markup              (GtkSCTree          *sctree,
2124                                              int                 column,
2125                                              gboolean            markup)
2126 {
2127         gint columns = 0;
2128         GValue value = { 0 };
2129         
2130         cm_return_if_fail(GTK_IS_SCTREE(sctree));
2131
2132         g_value_init (&value, G_TYPE_INT);      
2133         g_object_get_property (G_OBJECT (sctree), "n-columns", &value);
2134         columns = g_value_get_int (&value);
2135         g_value_unset (&value);
2136
2137         cm_return_if_fail(column < columns);
2138
2139         sctree->use_markup[column] = markup;
2140 }
2141
2142 void gtk_sctree_select (GtkSCTree *sctree, GtkCMCTreeNode *node)
2143 {
2144         select_row(sctree, 
2145                    g_list_position(GTK_CMCLIST(sctree)->row_list, (GList *)node),
2146                    -1, 0, node);
2147 }
2148
2149 void gtk_sctree_select_with_state (GtkSCTree *sctree, GtkCMCTreeNode *node, int state)
2150 {
2151         select_row(sctree, 
2152                    g_list_position(GTK_CMCLIST(sctree)->row_list, (GList *)node),
2153                    -1, state, node);
2154 }
2155
2156 void gtk_sctree_unselect_all (GtkSCTree *sctree)
2157 {
2158         gtk_cmclist_unselect_all(GTK_CMCLIST(sctree));
2159         sctree->anchor_row = NULL;
2160 }
2161
2162 void gtk_sctree_set_anchor_row (GtkSCTree *sctree, GtkCMCTreeNode *node)
2163 {
2164         sctree->anchor_row = node;
2165 }
2166
2167 void gtk_sctree_remove_node (GtkSCTree *sctree, GtkCMCTreeNode *node)
2168 {
2169         if (sctree->anchor_row == node)
2170                 sctree->anchor_row = NULL;
2171         gtk_cmctree_remove_node(GTK_CMCTREE(sctree), node);
2172 }
2173
2174 void gtk_sctree_set_stripes(GtkSCTree  *sctree, gboolean show_stripes)
2175 {
2176         sctree->show_stripes = show_stripes;
2177 }
2178
2179 void gtk_sctree_set_recursive_expand(GtkSCTree  *sctree, gboolean rec_exp)
2180 {
2181         sctree->always_expand_recursively = rec_exp;
2182 }
2183
2184 /***********************************************************
2185  *             Tree sorting functions                      *
2186  ***********************************************************/
2187
2188 static void sink(GtkCMCList *clist, GPtrArray *numbers, gint root, gint bottom)
2189 {
2190         gint j, k ;
2191         GtkCMCTreeNode *temp;
2192
2193         j = 2 * root;
2194         k = j + 1;
2195
2196         /* find the maximum element of numbers[root],
2197            numbers[2*root] and numbers[2*root+1] */
2198         if (j <= bottom) {
2199                 if (clist->compare( clist, GTK_CMCTREE_ROW (g_ptr_array_index(numbers, root)),
2200                                     GTK_CMCTREE_ROW(g_ptr_array_index( numbers, j))) >= 0)
2201                         j = root;
2202                 if (k <= bottom)
2203                         if (clist->compare( clist, GTK_CMCTREE_ROW (g_ptr_array_index(numbers, k)),
2204                                             GTK_CMCTREE_ROW (g_ptr_array_index( numbers, j))) > 0)
2205                                 j = k;
2206                 /* if numbers[root] wasn't the maximum element then
2207                    sink again */
2208                 if (root != j) {
2209                         temp = g_ptr_array_index( numbers,root);
2210                         g_ptr_array_index( numbers, root) = g_ptr_array_index( numbers, j);
2211                         g_ptr_array_index( numbers, j) = temp;
2212                         sink( clist, numbers, j, bottom);
2213                 }
2214         }
2215 }
2216
2217 static void heap_sort(GtkCMCList *clist, GPtrArray *numbers, gint array_size)
2218 {
2219         gint i;
2220         GtkCMCTreeNode *temp;
2221         
2222         /* build the Heap */
2223         for (i = (array_size / 2); i >= 1; i--)
2224                 sink( clist, numbers, i, array_size);
2225         /* output the Heap */
2226         for (i = array_size; i >= 2; i--) {
2227                 temp = g_ptr_array_index( numbers, 1);
2228                 g_ptr_array_index( numbers, 1) = g_ptr_array_index( numbers, i);
2229                 g_ptr_array_index( numbers, i) = temp;
2230                 sink( clist, numbers, 1, i-1);
2231         }
2232 }
2233
2234 static void
2235 stree_sort (GtkCMCTree    *ctree,
2236            GtkCMCTreeNode *node,
2237            gpointer      data)
2238 {
2239         GtkCMCTreeNode *list_start, *work, *next;
2240         GPtrArray *row_array, *viewable_array;
2241         GtkCMCList *clist;
2242         gint i;
2243
2244         clist = GTK_CMCLIST (ctree);
2245
2246         if (node)
2247                 work = GTK_CMCTREE_ROW (node)->children;
2248         else
2249                 work = GTK_CMCTREE_NODE (clist->row_list);
2250
2251         row_array = g_ptr_array_new();
2252         viewable_array = g_ptr_array_new();
2253
2254         if (work) {
2255                 g_ptr_array_add( row_array, NULL);
2256                 while (work) {
2257                         /* add all rows to row_array */
2258                         g_ptr_array_add( row_array, work);
2259                         if (GTK_CMCTREE_ROW (work)->parent && gtk_cmctree_is_viewable( ctree, work))
2260                                 g_ptr_array_add( viewable_array, GTK_CMCTREE_ROW (work)->parent);
2261                         next = GTK_CMCTREE_ROW (work)->sibling;
2262                         gtk_sctree_unlink( ctree, work, FALSE);
2263                         work = next;
2264                 }
2265
2266                 heap_sort( clist, row_array, (row_array->len)-1);
2267
2268                 if (node)
2269                         list_start = GTK_CMCTREE_ROW (node)->children;
2270                 else
2271                         list_start = GTK_CMCTREE_NODE (clist->row_list);
2272
2273                 if (clist->sort_type == GTK_SORT_ASCENDING) {
2274                         for (i=(row_array->len)-1; i>=1; i--) {
2275                                 work = g_ptr_array_index( row_array, i);
2276                                 gtk_sctree_link( ctree, work, node, list_start, FALSE);
2277                                 list_start = work;
2278                                 /* insert work at the beginning of the list */
2279                         }
2280                 } else {
2281                         for (i=1; i<row_array->len; i++) {
2282                                 work = g_ptr_array_index( row_array, i);
2283                                 gtk_sctree_link( ctree, work, node, list_start, FALSE);
2284                                 list_start = work;
2285                                 /* insert work at the beginning of the list */
2286                         }
2287                 }
2288
2289                 for (i=0; i<viewable_array->len; i++) {
2290                         gtk_cmctree_expand( ctree, g_ptr_array_index( viewable_array, i));
2291                 }
2292                 
2293         }
2294         g_ptr_array_free( row_array, TRUE);
2295         g_ptr_array_free( viewable_array, TRUE);
2296 }
2297
2298 void
2299 gtk_sctree_sort_recursive (GtkCMCTree     *ctree, 
2300                           GtkCMCTreeNode *node)
2301 {
2302         GtkCMCList *clist;
2303         GtkCMCTreeNode *focus_node = NULL;
2304
2305         cm_return_if_fail (ctree != NULL);
2306         cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2307
2308         clist = GTK_CMCLIST (ctree);
2309
2310         gtk_cmclist_freeze (clist);
2311
2312         if (clist->selection_mode == GTK_SELECTION_EXTENDED) {
2313                 GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2314       
2315                 g_list_free (clist->undo_selection);
2316                 g_list_free (clist->undo_unselection);
2317                 clist->undo_selection = NULL;
2318                 clist->undo_unselection = NULL;
2319         }
2320
2321         if (!node || (node && gtk_cmctree_is_viewable (ctree, node)))
2322                 focus_node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
2323       
2324         GTK_SCTREE(ctree)->sorting = TRUE;
2325
2326         gtk_cmctree_post_recursive (ctree, node, GTK_CMCTREE_FUNC (stree_sort), NULL);
2327
2328         if (!node)
2329                 stree_sort (ctree, NULL, NULL);
2330
2331         GTK_SCTREE(ctree)->sorting = FALSE;
2332
2333         if (focus_node) {
2334                 clist->focus_row = g_list_position (clist->row_list,(GList *)focus_node);
2335                 clist->undo_anchor = clist->focus_row;
2336         }
2337
2338         gtk_cmclist_thaw (clist);
2339 }
2340
2341 void
2342 gtk_sctree_sort_node (GtkCMCTree     *ctree, 
2343                      GtkCMCTreeNode *node)
2344 {
2345         GtkCMCList *clist;
2346         GtkCMCTreeNode *focus_node = NULL;
2347
2348         cm_return_if_fail (ctree != NULL);
2349         cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2350
2351         clist = GTK_CMCLIST (ctree);
2352
2353         gtk_cmclist_freeze (clist);
2354
2355         if (clist->selection_mode == GTK_SELECTION_EXTENDED) {
2356                 GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2357
2358                 g_list_free (clist->undo_selection);
2359                 g_list_free (clist->undo_unselection);
2360                 clist->undo_selection = NULL;
2361                 clist->undo_unselection = NULL;
2362         }
2363
2364         if (!node || (node && gtk_cmctree_is_viewable (ctree, node)))
2365                 focus_node = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
2366
2367         GTK_SCTREE(ctree)->sorting = TRUE;
2368
2369         stree_sort (ctree, node, NULL);
2370
2371         GTK_SCTREE(ctree)->sorting = FALSE;
2372
2373         if (focus_node) {
2374                 clist->focus_row = g_list_position (clist->row_list,(GList *)focus_node);
2375                 clist->undo_anchor = clist->focus_row;
2376         }
2377
2378         gtk_cmclist_thaw (clist);
2379 }
2380
2381 /************************************************************************/
2382
2383 static void
2384 gtk_sctree_unlink (GtkCMCTree     *ctree, 
2385                   GtkCMCTreeNode *node,
2386                   gboolean      update_focus_row)
2387 {
2388         GtkCMCList *clist;
2389         gint rows;
2390         gint level;
2391         gint visible;
2392         GtkCMCTreeNode *work;
2393         GtkCMCTreeNode *parent;
2394         GList *list;
2395
2396         cm_return_if_fail (ctree != NULL);
2397         cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2398         cm_return_if_fail (node != NULL);
2399
2400         clist = GTK_CMCLIST (ctree);
2401   
2402         if (update_focus_row && clist->selection_mode == GTK_SELECTION_EXTENDED) {
2403                 GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2404
2405                 g_list_free (clist->undo_selection);
2406                 g_list_free (clist->undo_unselection);
2407                 clist->undo_selection = NULL;
2408                 clist->undo_unselection = NULL;
2409         }
2410
2411         visible = gtk_cmctree_is_viewable (ctree, node);
2412
2413         /* clist->row_list_end unlinked ? */
2414         if (visible && (GTK_CMCTREE_NODE_NEXT (node) == NULL ||
2415            (GTK_CMCTREE_ROW (node)->children && gtk_cmctree_is_ancestor (ctree, node,
2416             GTK_CMCTREE_NODE (clist->row_list_end)))))
2417                 clist->row_list_end = (GList *) (GTK_CMCTREE_NODE_PREV (node));
2418
2419         /* update list */
2420         rows = 0;
2421         level = GTK_CMCTREE_ROW (node)->level;
2422         work = GTK_CMCTREE_NODE_NEXT (node);
2423         while (work && GTK_CMCTREE_ROW (work)->level > level) {
2424                 work = GTK_CMCTREE_NODE_NEXT (work);
2425                 rows++;
2426         }
2427
2428         if (visible) {
2429                 clist->rows -= (rows + 1);
2430
2431                 if (update_focus_row) {
2432                         gint pos;
2433                         pos = g_list_position (clist->row_list, (GList *)node);
2434                         if (pos + rows < clist->focus_row)
2435                                 clist->focus_row -= (rows + 1);
2436                         else if (pos <= clist->focus_row) {
2437                                 if (!GTK_CMCTREE_ROW (node)->sibling)
2438                                         clist->focus_row = MAX (pos - 1, 0);
2439                                 else
2440                                         clist->focus_row = pos;
2441               
2442                                 clist->focus_row = MIN (clist->focus_row, clist->rows - 1);
2443                         }
2444                         clist->undo_anchor = clist->focus_row;
2445                 }
2446         }
2447
2448         if (work) {
2449                 list = (GList *)GTK_CMCTREE_NODE_PREV (work);
2450                 list->next = NULL;
2451                 list = (GList *)work;
2452                 list->prev = (GList *)GTK_CMCTREE_NODE_PREV (node);
2453         }
2454
2455         if (GTK_CMCTREE_NODE_PREV (node) &&
2456             GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (node)) == node) {
2457                 list = (GList *)GTK_CMCTREE_NODE_PREV (node);
2458                 list->next = (GList *)work;
2459         }
2460
2461         /* update tree */
2462         parent = GTK_CMCTREE_ROW (node)->parent;
2463         if (parent) {
2464                 if (GTK_CMCTREE_ROW (parent)->children == node) {
2465                         GTK_CMCTREE_ROW (parent)->children = GTK_CMCTREE_ROW (node)->sibling;
2466                 }
2467                 else {
2468                         GtkCMCTreeNode *sibling;
2469
2470                         sibling = GTK_CMCTREE_ROW (parent)->children;
2471                         while (GTK_CMCTREE_ROW (sibling)->sibling != node)
2472                                 sibling = GTK_CMCTREE_ROW (sibling)->sibling;
2473                         GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
2474                 }
2475         }
2476         else {
2477                 if (clist->row_list == (GList *)node)
2478                         clist->row_list = (GList *) (GTK_CMCTREE_ROW (node)->sibling);
2479                 else {
2480                         GtkCMCTreeNode *sibling;
2481
2482                         sibling = GTK_CMCTREE_NODE (clist->row_list);
2483                         while (GTK_CMCTREE_ROW (sibling)->sibling != node)
2484                                 sibling = GTK_CMCTREE_ROW (sibling)->sibling;
2485                         GTK_CMCTREE_ROW (sibling)->sibling = GTK_CMCTREE_ROW (node)->sibling;
2486                 }
2487         }
2488 }
2489
2490 static void
2491 gtk_sctree_link (GtkCMCTree     *ctree,
2492                 GtkCMCTreeNode *node,
2493                 GtkCMCTreeNode *parent,
2494                 GtkCMCTreeNode *sibling,
2495                 gboolean      update_focus_row)
2496 {
2497         GtkCMCList *clist;
2498         GList *list_end;
2499         GList *list;
2500         GList *work;
2501         gboolean visible = FALSE;
2502         gint rows = 0;
2503   
2504         if (sibling)
2505                 cm_return_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent);
2506         cm_return_if_fail (node != NULL);
2507         cm_return_if_fail (node != sibling);
2508         cm_return_if_fail (node != parent);
2509
2510         clist = GTK_CMCLIST (ctree);
2511
2512         if (update_focus_row && clist->selection_mode == GTK_SELECTION_EXTENDED) {
2513                 GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2514
2515                 g_list_free (clist->undo_selection);
2516                 g_list_free (clist->undo_unselection);
2517                 clist->undo_selection = NULL;
2518                 clist->undo_unselection = NULL;
2519         }
2520
2521         for (rows = 1, list_end = (GList *)node; list_end->next;
2522              list_end = list_end->next)
2523                 rows++;
2524
2525         GTK_CMCTREE_ROW (node)->parent = parent;
2526         GTK_CMCTREE_ROW (node)->sibling = sibling;
2527
2528         if (!parent || (parent && (gtk_cmctree_is_viewable (ctree, parent) &&
2529             GTK_CMCTREE_ROW (parent)->expanded))) {
2530                 visible = TRUE;
2531                 clist->rows += rows;
2532         }
2533
2534         if (parent)
2535                 work = (GList *)(GTK_CMCTREE_ROW (parent)->children);
2536         else
2537                 work = clist->row_list;
2538
2539         if (sibling) {
2540                 if (work != (GList *)sibling) {
2541                         while (GTK_CMCTREE_ROW (work)->sibling != sibling)
2542                                 work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
2543                         GTK_CMCTREE_ROW (work)->sibling = node;
2544                 }
2545
2546                 if (sibling == GTK_CMCTREE_NODE (clist->row_list))
2547                 clist->row_list = (GList *) node;
2548                 if (GTK_CMCTREE_NODE_PREV (sibling) &&
2549                     GTK_CMCTREE_NODE_NEXT (GTK_CMCTREE_NODE_PREV (sibling)) == sibling) {
2550                         list = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
2551                         list->next = (GList *)node;
2552                 }
2553
2554                 list = (GList *)node;
2555                 list->prev = (GList *)GTK_CMCTREE_NODE_PREV (sibling);
2556                 list_end->next = (GList *)sibling;
2557                 list = (GList *)sibling;
2558                 list->prev = list_end;
2559                 if (parent && GTK_CMCTREE_ROW (parent)->children == sibling)
2560                         GTK_CMCTREE_ROW (parent)->children = node;
2561         }
2562         else {
2563                 if (work) {
2564                         /* find sibling */
2565                         while (GTK_CMCTREE_ROW (work)->sibling)
2566                         work = (GList *)(GTK_CMCTREE_ROW (work)->sibling);
2567                         GTK_CMCTREE_ROW (work)->sibling = node;
2568
2569                         /* find last visible child of sibling */
2570                         work = (GList *) gtk_sctree_last_visible (ctree,
2571                                GTK_CMCTREE_NODE (work));
2572
2573                         list_end->next = work->next;
2574                         if (work->next)
2575                                 list = work->next->prev = list_end;
2576                         work->next = (GList *)node;
2577                         list = (GList *)node;
2578                         list->prev = work;
2579                 }
2580                 else {
2581                         if (parent) {
2582                                 GTK_CMCTREE_ROW (parent)->children = node;
2583                                 list = (GList *)node;
2584                                 list->prev = (GList *)parent;
2585                                 if (GTK_CMCTREE_ROW (parent)->expanded) {
2586                                         list_end->next = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
2587                                         if (GTK_CMCTREE_NODE_NEXT(parent)) {
2588                                                 list = (GList *)GTK_CMCTREE_NODE_NEXT (parent);
2589                                                 list->prev = list_end;
2590                                         }
2591                                         list = (GList *)parent;
2592                                         list->next = (GList *)node;
2593                                 }
2594                                 else
2595                                         list_end->next = NULL;
2596                         }
2597                         else {
2598                                 clist->row_list = (GList *)node;
2599                                 list = (GList *)node;
2600                                 list->prev = NULL;
2601                                 list_end->next = NULL;
2602                         }
2603                 }
2604         }
2605
2606         gtk_cmctree_pre_recursive (ctree, node, stree_update_level, NULL); 
2607
2608         if (clist->row_list_end == NULL ||
2609             clist->row_list_end->next == (GList *)node)
2610                 clist->row_list_end = list_end;
2611
2612         if (visible && update_focus_row) {
2613                 gint pos;
2614                 pos = g_list_position (clist->row_list, (GList *)node);
2615   
2616                 if (pos <= clist->focus_row) {
2617                         clist->focus_row += rows;
2618                         clist->undo_anchor = clist->focus_row;
2619                 }
2620         }
2621 }
2622
2623 static void
2624 stree_update_level (GtkCMCTree     *ctree, 
2625                    GtkCMCTreeNode *node, 
2626                    gpointer      data)
2627 {
2628         if (!node)
2629                 return;
2630
2631         if (GTK_CMCTREE_ROW (node)->parent)
2632                 GTK_CMCTREE_ROW (node)->level = 
2633                 GTK_CMCTREE_ROW (GTK_CMCTREE_ROW (node)->parent)->level + 1;
2634         else
2635                 GTK_CMCTREE_ROW (node)->level = 1;
2636 }
2637
2638 static GtkCMCTreeNode *
2639 gtk_sctree_last_visible (GtkCMCTree     *ctree,
2640                         GtkCMCTreeNode *node)
2641 {
2642         GtkCMCTreeNode *work;
2643   
2644         if (!node)
2645                 return NULL;
2646
2647         work = GTK_CMCTREE_ROW (node)->children;
2648
2649         if (!work || !GTK_CMCTREE_ROW (node)->expanded)
2650                 return node;
2651
2652         while (GTK_CMCTREE_ROW (work)->sibling)
2653                 work = GTK_CMCTREE_ROW (work)->sibling;
2654
2655         return gtk_sctree_last_visible (ctree, work);
2656 }
2657
2658 static void 
2659 sset_node_info (GtkCMCTree     *ctree,
2660                GtkCMCTreeNode *node,
2661                const gchar  *text,
2662                guint8        spacing,
2663                GdkPixbuf    *pixbuf_closed,
2664                GdkPixbuf    *pixbuf_opened,
2665                gboolean      is_leaf,
2666                gboolean      expanded)
2667 {
2668   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
2669     {
2670       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
2671     }
2672   if (GTK_CMCTREE_ROW (node)->pixbuf_closed)
2673     {
2674       g_object_unref (GTK_CMCTREE_ROW (node)->pixbuf_closed);
2675     }
2676
2677   GTK_CMCTREE_ROW (node)->pixbuf_opened = NULL;
2678   GTK_CMCTREE_ROW (node)->pixbuf_closed = NULL;
2679
2680   if (pixbuf_closed)
2681     {
2682       GTK_CMCTREE_ROW (node)->pixbuf_closed = g_object_ref (pixbuf_closed);
2683     }
2684   if (pixbuf_opened)
2685     {
2686       GTK_CMCTREE_ROW (node)->pixbuf_opened = g_object_ref (pixbuf_opened);
2687     }
2688
2689   GTK_CMCTREE_ROW (node)->is_leaf  = is_leaf;
2690   GTK_CMCTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2691
2692   if (GTK_CMCTREE_ROW (node)->expanded)
2693     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2694                                 text, spacing, pixbuf_opened);
2695   else 
2696     gtk_cmctree_node_set_pixtext (ctree, node, ctree->tree_column,
2697                                 text, spacing, pixbuf_closed);
2698 }
2699
2700 static void
2701 stree_draw_node (GtkCMCTree     *ctree, 
2702                 GtkCMCTreeNode *node)
2703 {
2704   GtkCMCList *clist;
2705   
2706   clist = GTK_CMCLIST (ctree);
2707
2708   if (CLIST_UNFROZEN (clist) && gtk_cmctree_is_viewable (ctree, node))
2709     {
2710       GtkCMCTreeNode *work;
2711       gint num = 0;
2712       
2713       work = GTK_CMCTREE_NODE (clist->row_list);
2714       while (work && work != node)
2715         {
2716           work = GTK_CMCTREE_NODE_NEXT (work);
2717           num++;
2718         }
2719       if (work && gtk_cmclist_row_is_visible (clist, num) != GTK_VISIBILITY_NONE)
2720         GTK_CMCLIST_GET_CLASS (clist)->draw_row
2721           (clist, NULL, num, GTK_CMCLIST_ROW ((GList *) node));
2722     }
2723 }
2724
2725 /* this wrapper simply replaces NULL pixbufs 
2726  * with a transparent, 1x1 pixbuf. This works
2727  * around a memory problem deep inside gtk, 
2728  * revealed by valgrind. 
2729  */
2730 void        gtk_sctree_set_node_info        (GtkCMCTree *ctree,
2731                                              GtkCMCTreeNode *node,
2732                                              const gchar *text,
2733                                              guint8 spacing,
2734                                              GdkPixbuf *pixbuf_closed,
2735                                              GdkPixbuf *pixbuf_opened,
2736                                              gboolean is_leaf,
2737                                              gboolean expanded)
2738 {
2739   gboolean old_leaf;
2740   gboolean old_expanded;
2741   GtkCMCTreeNode *work;
2742  
2743   if (!GTK_IS_CMCTREE (ctree) || !node) return;
2744
2745   old_leaf = GTK_CMCTREE_ROW (node)->is_leaf;
2746   old_expanded = GTK_CMCTREE_ROW (node)->expanded;
2747
2748   if (is_leaf && (work = GTK_CMCTREE_ROW (node)->children) != NULL)
2749     {
2750       GtkCMCTreeNode *ptr;
2751       
2752       while (work)
2753         {
2754           ptr = work;
2755           work = GTK_CMCTREE_ROW (work)->sibling;
2756           gtk_cmctree_remove_node (ctree, ptr);
2757         }
2758     }
2759
2760   sset_node_info (ctree, node, text, spacing, pixbuf_closed,
2761                  pixbuf_opened, is_leaf, expanded);
2762
2763   if (!is_leaf && !old_leaf)
2764     {
2765       GTK_CMCTREE_ROW (node)->expanded = old_expanded;
2766       if (expanded && !old_expanded)
2767         gtk_cmctree_expand (ctree, node);
2768       else if (!expanded && old_expanded)
2769         gtk_cmctree_collapse (ctree, node);
2770     }
2771
2772   GTK_CMCTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2773   
2774   stree_draw_node (ctree, node);
2775 }
2776
2777 static GtkCMCTreeRow *
2778 srow_new (GtkCMCTree *ctree)
2779 {
2780   GtkCMCList *clist;
2781   GtkCMCTreeRow *ctree_row;
2782   int i;
2783
2784   clist = GTK_CMCLIST (ctree);
2785 #if GLIB_CHECK_VERSION(2,10,0)
2786   ctree_row = g_slice_new (GtkCMCTreeRow);
2787   ctree_row->row.cell = g_slice_alloc (sizeof (GtkCMCell) * clist->columns);
2788 #else
2789   ctree_row = g_chunk_new (GtkCMCTreeRow, (GMemChunk *)clist->row_mem_chunk);
2790   ctree_row->row.cell = g_chunk_new (GtkCMCell, (GMemChunk *)clist->cell_mem_chunk);
2791 #endif
2792   for (i = 0; i < clist->columns; i++)
2793     {
2794       ctree_row->row.cell[i].type = GTK_CMCELL_EMPTY;
2795       ctree_row->row.cell[i].vertical = 0;
2796       ctree_row->row.cell[i].horizontal = 0;
2797       ctree_row->row.cell[i].style = NULL;
2798     }
2799
2800   GTK_CMCELL_PIXTEXT (ctree_row->row.cell[ctree->tree_column])->text = NULL;
2801
2802   ctree_row->row.fg_set     = FALSE;
2803   ctree_row->row.bg_set     = FALSE;
2804   ctree_row->row.style      = NULL;
2805   ctree_row->row.selectable = TRUE;
2806   ctree_row->row.state      = GTK_STATE_NORMAL;
2807   ctree_row->row.data       = NULL;
2808   ctree_row->row.destroy    = NULL;
2809
2810   ctree_row->level         = 0;
2811   ctree_row->expanded      = FALSE;
2812   ctree_row->parent        = NULL;
2813   ctree_row->sibling       = NULL;
2814   ctree_row->children      = NULL;
2815   ctree_row->pixbuf_closed = NULL;
2816   ctree_row->pixbuf_opened = NULL;
2817   
2818   return ctree_row;
2819 }
2820
2821 static void
2822 srow_delete (GtkCMCTree    *ctree,
2823             GtkCMCTreeRow *ctree_row)
2824 {
2825   GtkCMCList *clist;
2826   gint i;
2827
2828   clist = GTK_CMCLIST (ctree);
2829
2830   for (i = 0; i < clist->columns; i++)
2831     {
2832       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2833         (clist, &(ctree_row->row), i, GTK_CMCELL_EMPTY, NULL, 0, NULL);
2834       if (ctree_row->row.cell[i].style)
2835         {
2836           if (GTK_WIDGET_REALIZED (ctree))
2837             gtk_style_detach (ctree_row->row.cell[i].style);
2838           g_object_unref (ctree_row->row.cell[i].style);
2839         }
2840     }
2841
2842   if (ctree_row->row.style)
2843     {
2844       if (GTK_WIDGET_REALIZED (ctree))
2845         gtk_style_detach (ctree_row->row.style);
2846       g_object_unref (ctree_row->row.style);
2847     }
2848
2849   if (ctree_row->pixbuf_closed)
2850     {
2851       g_object_unref (ctree_row->pixbuf_closed);
2852     }
2853
2854   if (ctree_row->pixbuf_opened)
2855     {
2856       g_object_unref (ctree_row->pixbuf_opened);
2857     }
2858
2859   if (ctree_row->row.destroy)
2860     {
2861       GDestroyNotify dnotify = ctree_row->row.destroy;
2862       gpointer ddata = ctree_row->row.data;
2863
2864       ctree_row->row.destroy = NULL;
2865       ctree_row->row.data = NULL;
2866
2867       dnotify (ddata);
2868     }
2869
2870 #if GLIB_CHECK_VERSION(2,10,0)  
2871   g_slice_free1 (sizeof (GtkCMCell) * clist->columns, ctree_row->row.cell);
2872   g_slice_free (GtkCMCTreeRow, ctree_row);
2873 #else
2874   g_mem_chunk_free ((GMemChunk *)clist->cell_mem_chunk, ctree_row->row.cell);
2875   g_mem_chunk_free ((GMemChunk *)clist->row_mem_chunk, ctree_row);
2876 #endif
2877 }
2878
2879 static void
2880 stree_delete_row (GtkCMCTree     *ctree, 
2881                  GtkCMCTreeNode *node, 
2882                  gpointer      data)
2883 {
2884   srow_delete (ctree, GTK_CMCTREE_ROW (node));
2885   g_list_free_1 ((GList *)node);
2886 }
2887
2888 static void 
2889 gtk_sctree_real_tree_expand (GtkCMCTree     *ctree,
2890                   GtkCMCTreeNode *node)
2891 {
2892   GtkCMCList *clist;
2893   GtkCMCTreeNode *work;
2894   GtkRequisition requisition;
2895   gboolean visible;
2896   gint level;
2897
2898   cm_return_if_fail (GTK_IS_CMCTREE (ctree));
2899
2900   if (!node || GTK_CMCTREE_ROW (node)->expanded || GTK_CMCTREE_ROW (node)->is_leaf)
2901     return;
2902
2903   clist = GTK_CMCLIST (ctree);
2904   
2905   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2906
2907   GTK_CMCTREE_ROW (node)->expanded = TRUE;
2908   level = GTK_CMCTREE_ROW (node)->level;
2909
2910   visible = gtk_cmctree_is_viewable (ctree, node);
2911   /* get cell width if tree_column is auto resized */
2912   if (visible && clist->column[ctree->tree_column].auto_resize &&
2913       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2914     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2915       (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column, &requisition);
2916
2917   /* unref/unset closed pixbuf */
2918   if (GTK_CMCELL_PIXTEXT 
2919       (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf)
2920     {
2921       g_object_unref
2922         (GTK_CMCELL_PIXTEXT
2923          (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf);
2924       
2925       GTK_CMCELL_PIXTEXT
2926         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = NULL;
2927     }
2928
2929   /* set/ref opened pixbuf */
2930   if (GTK_CMCTREE_ROW (node)->pixbuf_opened)
2931     {
2932       GTK_CMCELL_PIXTEXT 
2933         (GTK_CMCTREE_ROW (node)->row.cell[ctree->tree_column])->pixbuf = 
2934         g_object_ref (GTK_CMCTREE_ROW (node)->pixbuf_opened);
2935     }
2936
2937
2938   work = GTK_CMCTREE_ROW (node)->children;
2939   if (work)
2940     {
2941       GList *list = (GList *)work;
2942       gint *cell_width = NULL;
2943       gint tmp = 0;
2944       gint row;
2945       gint i;
2946       
2947       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2948         {
2949           cell_width = g_new0 (gint, clist->columns);
2950           if (clist->column[ctree->tree_column].auto_resize)
2951               cell_width[ctree->tree_column] = requisition.width;
2952
2953           while (work)
2954             {
2955               /* search maximum cell widths of auto_resize columns */
2956               for (i = 0; i < clist->columns; i++)
2957                 if (clist->column[i].auto_resize)
2958                   {
2959                     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2960                       (clist, &GTK_CMCTREE_ROW (work)->row, i, &requisition);
2961                     cell_width[i] = MAX (requisition.width, cell_width[i]);
2962                   }
2963
2964               list = (GList *)work;
2965               work = GTK_CMCTREE_NODE_NEXT (work);
2966               tmp++;
2967             }
2968         }
2969       else
2970         while (work)
2971           {
2972             list = (GList *)work;
2973             work = GTK_CMCTREE_NODE_NEXT (work);
2974             tmp++;
2975           }
2976
2977       list->next = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2978
2979       if (GTK_CMCTREE_NODE_NEXT (node))
2980         {
2981           GList *tmp_list;
2982
2983           if (clist->row_list_end == list)
2984               clist->row_list_end = g_list_last(list);
2985
2986           tmp_list = (GList *)GTK_CMCTREE_NODE_NEXT (node);
2987           tmp_list->prev = list;
2988         }
2989       else
2990         clist->row_list_end = list;
2991
2992       list = (GList *)node;
2993       list->next = (GList *)(GTK_CMCTREE_ROW (node)->children);
2994
2995       if (visible && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2996         {
2997           /* resize auto_resize columns if needed */
2998           for (i = 0; i < clist->columns; i++)
2999             if (clist->column[i].auto_resize &&
3000                 cell_width[i] > clist->column[i].width)
3001               gtk_cmclist_set_column_width (clist, i, cell_width[i]);
3002           g_free (cell_width);
3003         
3004           if (!GTK_SCTREE(ctree)->sorting) {
3005                   /* update focus_row position */
3006                   row = g_list_position (clist->row_list, (GList *)node);
3007                   if (row < clist->focus_row)
3008                     clist->focus_row += tmp;
3009           }
3010           clist->rows += tmp;
3011           CLIST_REFRESH (clist);
3012         }
3013     }
3014   else if (visible && clist->column[ctree->tree_column].auto_resize)
3015     /* resize tree_column if needed */
3016     gtk_sctree_column_auto_resize (clist, &GTK_CMCTREE_ROW (node)->row, ctree->tree_column,
3017                         requisition.width);
3018
3019 }
3020
3021 GtkCMCTreeNode * 
3022 gtk_sctree_insert_node (GtkCMCTree     *ctree,
3023                        GtkCMCTreeNode *parent, 
3024                        GtkCMCTreeNode *sibling,
3025                        gchar        *text[],
3026                        guint8        spacing,
3027                        GdkPixbuf    *pixbuf_closed,
3028                        GdkPixbuf    *pixbuf_opened,
3029                        gboolean      is_leaf,
3030                        gboolean      expanded)
3031 {
3032   GtkCMCList *clist;
3033   GtkCMCTreeRow *new_row;
3034   GtkCMCTreeNode *node;
3035   GList *list;
3036   gint i;
3037
3038   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3039   if (sibling)
3040     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3041
3042   if (parent && GTK_CMCTREE_ROW (parent)->is_leaf)
3043     return NULL;
3044
3045   clist = GTK_CMCLIST (ctree);
3046
3047   /* create the row */
3048   new_row = srow_new (ctree);
3049   list = g_list_alloc ();
3050   list->data = new_row;
3051   node = GTK_CMCTREE_NODE (list);
3052
3053   if (text)
3054     for (i = 0; i < clist->columns; i++)
3055       if (text[i] && i != ctree->tree_column)
3056         GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
3057           (clist, &(new_row->row), i, GTK_CMCELL_TEXT, text[i], 0, NULL);
3058
3059   sset_node_info (ctree, node, text ?
3060                  text[ctree->tree_column] : NULL, spacing, pixbuf_closed,
3061                  pixbuf_opened, is_leaf, expanded);
3062
3063   /* sorted insertion */
3064   if (GTK_CMCLIST_AUTO_SORT (clist))
3065     {
3066       if (parent)
3067         sibling = GTK_CMCTREE_ROW (parent)->children;
3068       else
3069         sibling = GTK_CMCTREE_NODE (clist->row_list);
3070
3071       while (sibling && clist->compare
3072              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (sibling)) > 0)
3073         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3074     }
3075
3076   gtk_sctree_link (ctree, node, parent, sibling, FALSE);
3077
3078   if (text && !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
3079       gtk_cmctree_is_viewable (ctree, node))
3080     {
3081       for (i = 0; i < clist->columns; i++)
3082         if (clist->column[i].auto_resize)
3083           gtk_sctree_column_auto_resize (clist, &(new_row->row), i, 0);
3084     }
3085
3086   if (clist->rows == 1)
3087     {
3088       clist->focus_row = 0;
3089       if (clist->selection_mode == GTK_SELECTION_BROWSE)
3090         gtk_sctree_select (GTK_SCTREE(ctree), node);
3091     }
3092
3093
3094   CLIST_REFRESH (clist);
3095
3096   return node;
3097 }
3098
3099 GtkCMCTreeNode *
3100 gtk_sctree_insert_gnode (GtkCMCTree          *ctree,
3101                         GtkCMCTreeNode      *parent,
3102                         GtkCMCTreeNode      *sibling,
3103                         GNode             *gnode,
3104                         GtkCMCTreeGNodeFunc  func,
3105                         gpointer           data)
3106 {
3107   GtkCMCList *clist;
3108   GtkCMCTreeNode *cnode = NULL;
3109   GtkCMCTreeNode *child = NULL;
3110   GtkCMCTreeNode *new_child;
3111   GList *list;
3112   GNode *work;
3113   guint depth = 1;
3114
3115   cm_return_val_if_fail (GTK_IS_CMCTREE (ctree), NULL);
3116   cm_return_val_if_fail (gnode != NULL, NULL);
3117   cm_return_val_if_fail (func != NULL, NULL);
3118   if (sibling)
3119     cm_return_val_if_fail (GTK_CMCTREE_ROW (sibling)->parent == parent, NULL);
3120   
3121   clist = GTK_CMCLIST (ctree);
3122
3123   if (parent)
3124     depth = GTK_CMCTREE_ROW (parent)->level + 1;
3125
3126   list = g_list_alloc ();
3127   list->data = srow_new (ctree);
3128   cnode = GTK_CMCTREE_NODE (list);
3129
3130   gtk_cmclist_freeze (clist);
3131
3132   sset_node_info (ctree, cnode, "", 0, NULL, NULL, TRUE, FALSE);
3133
3134   if (!func (ctree, depth, gnode, cnode, data))
3135     {
3136       stree_delete_row (ctree, cnode, NULL);
3137       gtk_cmclist_thaw (clist);
3138       return NULL;
3139     }
3140
3141   if (GTK_CMCLIST_AUTO_SORT (clist))
3142     {
3143       if (parent)
3144         sibling = GTK_CMCTREE_ROW (parent)->children;
3145       else
3146         sibling = GTK_CMCTREE_NODE (clist->row_list);
3147
3148       while (sibling && clist->compare
3149              (clist, GTK_CMCTREE_ROW (cnode), GTK_CMCTREE_ROW (sibling)) > 0)
3150         sibling = GTK_CMCTREE_ROW (sibling)->sibling;
3151     }
3152
3153   gtk_sctree_link (ctree, cnode, parent, sibling, FALSE);
3154
3155   for (work = g_node_last_child (gnode); work; work = work->prev)
3156     {
3157       new_child = gtk_sctree_insert_gnode (ctree, cnode, child,
3158                                           work, func, data);
3159       if (new_child)
3160         child = new_child;
3161     }   
3162   
3163   gtk_cmclist_thaw (clist);
3164
3165   return cnode;
3166 }
3167
3168 static void
3169 sreal_tree_move (GtkCMCTree     *ctree,
3170                 GtkCMCTreeNode *node,
3171                 GtkCMCTreeNode *new_parent, 
3172                 GtkCMCTreeNode *new_sibling)
3173 {
3174   GtkCMCList *clist;
3175   GtkCMCTreeNode *work;
3176   gboolean visible = FALSE;
3177
3178   cm_return_if_fail (ctree != NULL);
3179   cm_return_if_fail (node != NULL);
3180   cm_return_if_fail (!new_sibling || 
3181                     GTK_CMCTREE_ROW (new_sibling)->parent == new_parent);
3182
3183   if (new_parent && GTK_CMCTREE_ROW (new_parent)->is_leaf)
3184     return;
3185
3186   /* new_parent != child of child */
3187   for (work = new_parent; work; work = GTK_CMCTREE_ROW (work)->parent)
3188     if (work == node)
3189       return;
3190
3191   clist = GTK_CMCLIST (ctree);
3192
3193   visible = gtk_cmctree_is_viewable (ctree, node);
3194
3195   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
3196     {
3197       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3198       
3199       g_list_free (clist->undo_selection);
3200       g_list_free (clist->undo_unselection);
3201       clist->undo_selection = NULL;
3202       clist->undo_unselection = NULL;
3203     }
3204
3205   if (GTK_CMCLIST_AUTO_SORT (clist))
3206     {
3207       if (new_parent == GTK_CMCTREE_ROW (node)->parent)
3208         return;
3209       
3210       if (new_parent)
3211         new_sibling = GTK_CMCTREE_ROW (new_parent)->children;
3212       else
3213         new_sibling = GTK_CMCTREE_NODE (clist->row_list);
3214
3215       while (new_sibling && clist->compare
3216              (clist, GTK_CMCTREE_ROW (node), GTK_CMCTREE_ROW (new_sibling)) > 0)
3217         new_sibling = GTK_CMCTREE_ROW (new_sibling)->sibling;
3218     }
3219
3220   if (new_parent == GTK_CMCTREE_ROW (node)->parent && 
3221       new_sibling == GTK_CMCTREE_ROW (node)->sibling)
3222     return;
3223
3224   gtk_cmclist_freeze (clist);
3225
3226   work = NULL;
3227
3228   if (!GTK_SCTREE(ctree)->sorting && gtk_cmctree_is_viewable (ctree, node))
3229     work = GTK_CMCTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
3230       
3231   gtk_sctree_unlink (ctree, node, FALSE);
3232   gtk_sctree_link (ctree, node, new_parent, new_sibling, FALSE);
3233   
3234   if (!GTK_SCTREE(ctree)->sorting && work)
3235     {
3236       while (work &&  !gtk_cmctree_is_viewable (ctree, work))
3237         work = GTK_CMCTREE_ROW (work)->parent;
3238       clist->focus_row = g_list_position (clist->row_list, (GList *)work);
3239       clist->undo_anchor = clist->focus_row;
3240     }
3241
3242   if (clist->column[ctree->tree_column].auto_resize &&
3243       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist) &&
3244       (visible || gtk_cmctree_is_viewable (ctree, node)))
3245     gtk_cmclist_set_column_width
3246       (clist, ctree->tree_column,
3247        gtk_cmclist_optimal_column_width (clist, ctree->tree_column));
3248
3249   gtk_cmclist_thaw (clist);
3250 }
3251
3252 void gtk_sctree_set_column_tooltip          (GtkSCTree          *sctree,
3253                                              int                 column,
3254                                              const gchar        *tip)
3255 {
3256 #if !(GTK_CHECK_VERSION(2,12,0))
3257         GtkTooltips *tips;
3258         if (!sctree->tooltips)
3259                 sctree->tooltips = gtk_tooltips_new();
3260         tips = sctree->tooltips;
3261 #endif
3262
3263         CLAWS_SET_TIP(GTK_CMCLIST(sctree)->column[column].button,
3264                         tip);
3265 }
3266