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