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