2006-09-20 [colin] 2.4.0cvs200
[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 - prefs_common.stripes_color_offset;
852                 greybg.green = normalbg.green - prefs_common.stripes_color_offset;
853                 greybg.blue = normalbg.blue - prefs_common.stripes_color_offset;
854         } else if (normalbg.red < 0x8888 && normalbg.green < 0x8888 && normalbg.blue < 0x8888) {
855                 greybg.pixel = normalbg.pixel;
856                 greybg.red = normalbg.red + prefs_common.stripes_color_offset;
857                 greybg.green = normalbg.green + prefs_common.stripes_color_offset;
858                 greybg.blue = normalbg.blue + prefs_common.stripes_color_offset;
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.use_stripes_everywhere && GTK_SCTREE(ctree)->show_stripes
898       && color_change && row % 2) {
899     clist_row->background = greybg;
900     clist_row->bg_set = TRUE;
901   } else {
902     clist_row->bg_set = FALSE;
903   }
904   if (clist_row->state == GTK_STATE_NORMAL)
905     {
906       if (clist_row->fg_set)
907         gdk_gc_set_foreground (clist->fg_gc, &clist_row->foreground);
908       if (clist_row->bg_set)
909         gdk_gc_set_rgb_fg_color (clist->bg_gc, &clist_row->background);
910     }
911   
912   state = clist_row->state;
913
914   gdk_gc_set_foreground (ctree->lines_gc,
915                          &widget->style->fg[clist_row->state]);
916
917   /* draw the cell borders */
918   if (area)
919     {
920       rect = &intersect_rectangle;
921       crect = &intersect_rectangle;
922
923       if (gdk_rectangle_intersect (area, &cell_rectangle, crect))
924         gdk_draw_rectangle (clist->clist_window,
925                             widget->style->base_gc[GTK_STATE_NORMAL], TRUE,
926                             crect->x, crect->y, crect->width, crect->height);
927     }
928   else
929     {
930       rect = &clip_rectangle;
931       crect = &cell_rectangle;
932
933       gdk_draw_rectangle (clist->clist_window,
934                           widget->style->base_gc[GTK_STATE_NORMAL], TRUE,
935                           crect->x, crect->y, crect->width, crect->height);
936     }
937
938   /* horizontal black lines */
939   if (ctree->line_style == GTK_CTREE_LINES_TABBED)
940     { 
941
942       column_right = (COLUMN_LEFT_XPIXEL (clist, ctree->tree_column) +
943                       clist->column[ctree->tree_column].area.width +
944                       COLUMN_INSET);
945       column_left = (COLUMN_LEFT_XPIXEL (clist, ctree->tree_column) -
946                      COLUMN_INSET - (ctree->tree_column != 0) * CELL_SPACING);
947
948       switch (clist->column[ctree->tree_column].justification)
949         {
950         case GTK_JUSTIFY_CENTER:
951         case GTK_JUSTIFY_FILL:
952         case GTK_JUSTIFY_LEFT:
953           offset = (column_left + ctree->tree_indent *
954                     (((GtkCTreeRow *)clist_row)->level - 1));
955
956           gdk_draw_line (clist->clist_window, ctree->lines_gc, 
957                          MIN (offset + TAB_SIZE, column_right),
958                          cell_rectangle.y,
959                          clist->clist_window_width, cell_rectangle.y);
960           break;
961         case GTK_JUSTIFY_RIGHT:
962           offset = (column_right - 1 - ctree->tree_indent *
963                     (((GtkCTreeRow *)clist_row)->level - 1));
964
965           gdk_draw_line (clist->clist_window, ctree->lines_gc,
966                          -1, cell_rectangle.y,
967                          MAX (offset - TAB_SIZE, column_left),
968                          cell_rectangle.y);
969           break;
970         }
971     }
972
973   /* the last row has to clear its bottom cell spacing too */
974   if (clist_row == clist->row_list_end->data)
975     {
976       cell_rectangle.y += clist->row_height + CELL_SPACING;
977
978       if (!area || gdk_rectangle_intersect (area, &cell_rectangle, crect))
979         {
980           gdk_draw_rectangle (clist->clist_window,
981                               widget->style->base_gc[GTK_STATE_NORMAL], TRUE,
982                               crect->x, crect->y, crect->width, crect->height);
983
984           /* horizontal black lines */
985           if (ctree->line_style == GTK_CTREE_LINES_TABBED)
986             { 
987               switch (clist->column[ctree->tree_column].justification)
988                 {
989                 case GTK_JUSTIFY_CENTER:
990                 case GTK_JUSTIFY_FILL:
991                 case GTK_JUSTIFY_LEFT:
992                   gdk_draw_line (clist->clist_window, ctree->lines_gc, 
993                                  MIN (column_left + TAB_SIZE + COLUMN_INSET +
994                                       (((GtkCTreeRow *)clist_row)->level > 1) *
995                                       MIN (ctree->tree_indent / 2, TAB_SIZE),
996                                       column_right),
997                                  cell_rectangle.y,
998                                  clist->clist_window_width, cell_rectangle.y);
999                   break;
1000                 case GTK_JUSTIFY_RIGHT:
1001                   gdk_draw_line (clist->clist_window, ctree->lines_gc, 
1002                                  -1, cell_rectangle.y,
1003                                  MAX (column_right - TAB_SIZE - 1 -
1004                                       COLUMN_INSET -
1005                                       (((GtkCTreeRow *)clist_row)->level > 1) *
1006                                       MIN (ctree->tree_indent / 2, TAB_SIZE),
1007                                       column_left - 1), cell_rectangle.y);
1008                   break;
1009                 }
1010             }
1011         }
1012     }     
1013
1014   for (last_column = clist->columns - 1;
1015        last_column >= 0 && !clist->column[last_column].visible; last_column--)
1016     ;
1017
1018   /* iterate and draw all the columns (row cells) and draw their contents */
1019   for (i = 0; i < clist->columns; i++)
1020     {
1021       GtkStyle *style;
1022       GdkGC *fg_gc; 
1023       GdkGC *bg_gc;
1024       PangoLayout *layout = NULL;
1025       PangoRectangle logical_rect;
1026
1027       gint width;
1028       gint height;
1029       gint pixmap_width;
1030       gint string_width;
1031       gint old_offset;
1032
1033       if (!clist->column[i].visible)
1034         continue;
1035
1036       gtk_sctree_get_cell_style (clist, clist_row, state, row, i, &style, &fg_gc, &bg_gc);
1037
1038       /* calculate clipping region */
1039       clip_rectangle.x = clist->column[i].area.x + clist->hoffset;
1040       clip_rectangle.width = clist->column[i].area.width;
1041
1042       cell_rectangle.x = clip_rectangle.x - COLUMN_INSET - CELL_SPACING;
1043       cell_rectangle.width = (clip_rectangle.width + 2 * COLUMN_INSET +
1044                               (1 + (i == last_column)) * CELL_SPACING);
1045       cell_rectangle.y = clip_rectangle.y;
1046       cell_rectangle.height = clip_rectangle.height;
1047
1048       string_width = 0;
1049       pixmap_width = 0;
1050
1051       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
1052                                             &intersect_rectangle))
1053         {
1054           if (i != ctree->tree_column)
1055             continue;
1056         }
1057       else
1058         {
1059           gdk_draw_rectangle (clist->clist_window, bg_gc, TRUE,
1060                               crect->x, crect->y, crect->width, crect->height);
1061
1062
1063           layout = sc_gtk_clist_create_cell_layout (clist, clist_row, i);
1064           if (layout)
1065             {
1066               pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
1067               width = logical_rect.width;
1068             }
1069           else
1070             width = 0;
1071
1072           switch (clist_row->cell[i].type)
1073             {
1074             case GTK_CELL_PIXMAP:
1075               gdk_window_get_size
1076                 (GTK_CELL_PIXMAP (clist_row->cell[i])->pixmap, &pixmap_width,
1077                  &height);
1078               width += pixmap_width;
1079               break;
1080             case GTK_CELL_PIXTEXT:
1081               if (GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap)
1082                 {
1083                   gdk_window_get_size 
1084                     (GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap,
1085                      &pixmap_width, &height);
1086                   width += pixmap_width;
1087                 }
1088
1089               if (GTK_CELL_PIXTEXT (clist_row->cell[i])->text &&
1090                   GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap)
1091                 width +=  GTK_CELL_PIXTEXT (clist_row->cell[i])->spacing;
1092
1093               if (i == ctree->tree_column)
1094                 width += (ctree->tree_indent *
1095                           ((GtkCTreeRow *)clist_row)->level);
1096               break;
1097             default:
1098               break;
1099             }
1100
1101           switch (clist->column[i].justification)
1102             {
1103             case GTK_JUSTIFY_LEFT:
1104               offset = clip_rectangle.x + clist_row->cell[i].horizontal;
1105               break;
1106             case GTK_JUSTIFY_RIGHT:
1107               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
1108                         clip_rectangle.width - width);
1109               break;
1110             case GTK_JUSTIFY_CENTER:
1111             case GTK_JUSTIFY_FILL:
1112               offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
1113                         (clip_rectangle.width / 2) - (width / 2));
1114               break;
1115             };
1116
1117           if (i != ctree->tree_column)
1118             {
1119               offset += clist_row->cell[i].horizontal;
1120               switch (clist_row->cell[i].type)
1121                 {
1122                 case GTK_CELL_PIXMAP:
1123                   gtk_sctree_draw_cell_pixmap
1124                     (clist->clist_window, &clip_rectangle, fg_gc,
1125                      GTK_CELL_PIXMAP (clist_row->cell[i])->pixmap,
1126                      GTK_CELL_PIXMAP (clist_row->cell[i])->mask,
1127                      offset,
1128                      clip_rectangle.y + clist_row->cell[i].vertical +
1129                      (clip_rectangle.height - height) / 2,
1130                      pixmap_width, height);
1131                   break;
1132                 case GTK_CELL_PIXTEXT:
1133                   offset = gtk_sctree_draw_cell_pixmap
1134                     (clist->clist_window, &clip_rectangle, fg_gc,
1135                      GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap,
1136                      GTK_CELL_PIXTEXT (clist_row->cell[i])->mask,
1137                      offset,
1138                      clip_rectangle.y + clist_row->cell[i].vertical +
1139                      (clip_rectangle.height - height) / 2,
1140                      pixmap_width, height);
1141                   offset += GTK_CELL_PIXTEXT (clist_row->cell[i])->spacing;
1142
1143                   /* Fall through */
1144                 case GTK_CELL_TEXT:
1145                   if (layout)
1146                     {
1147                       gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
1148
1149                       gdk_gc_set_clip_rectangle (fg_gc, &clip_rectangle);
1150                       gdk_draw_layout (clist->clist_window, fg_gc,
1151                                        offset,
1152                                        row_rectangle.y + row_center_offset + clist_row->cell[i].vertical,
1153                                        layout);
1154                       gdk_gc_set_clip_rectangle (fg_gc, NULL);
1155                       g_object_unref (G_OBJECT (layout));
1156                     }
1157                   break;
1158                 default:
1159                   break;
1160                 }
1161               continue;
1162             }
1163         }
1164
1165       if (bg_gc == clist->bg_gc)
1166         gdk_gc_set_background (ctree->lines_gc, &clist_row->background);
1167
1168       /* draw ctree->tree_column */
1169       cell_rectangle.y -= CELL_SPACING;
1170       cell_rectangle.height += CELL_SPACING;
1171
1172       if (area && !gdk_rectangle_intersect (area, &cell_rectangle,
1173                                             &intersect_rectangle))
1174         {
1175           if (layout)
1176             g_object_unref (G_OBJECT (layout));
1177           continue;
1178         }
1179
1180       /* draw lines */
1181       offset = gtk_sctree_draw_lines (ctree, (GtkCTreeRow *)clist_row, row, i,
1182                                      state, &clip_rectangle, &cell_rectangle,
1183                                      crect, area, style);
1184
1185       /* draw expander */
1186       offset = gtk_sctree_draw_expander (ctree, (GtkCTreeRow *)clist_row,
1187                                         style, &clip_rectangle, offset);
1188
1189       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
1190         offset -= ctree->tree_spacing;
1191       else
1192         offset += ctree->tree_spacing;
1193
1194       if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
1195         offset -= (pixmap_width + clist_row->cell[i].horizontal);
1196       else
1197         offset += clist_row->cell[i].horizontal;
1198
1199       old_offset = offset;
1200       offset = gtk_sctree_draw_cell_pixmap (clist->clist_window, &clip_rectangle, fg_gc,
1201                                  GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap,
1202                                  GTK_CELL_PIXTEXT (clist_row->cell[i])->mask,
1203                                  offset, 
1204                                  clip_rectangle.y + clist_row->cell[i].vertical
1205                                  + (clip_rectangle.height - height) / 2,
1206                                  pixmap_width, height);
1207
1208       if (layout)
1209         {
1210           gint row_center_offset = (clist->row_height - logical_rect.height) / 2;
1211           
1212           if (clist->column[i].justification == GTK_JUSTIFY_RIGHT)
1213             {
1214               offset = (old_offset - string_width);
1215               if (GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap)
1216                 offset -= GTK_CELL_PIXTEXT (clist_row->cell[i])->spacing;
1217             }
1218           else
1219             {
1220               if (GTK_CELL_PIXTEXT (clist_row->cell[i])->pixmap)
1221                 offset += GTK_CELL_PIXTEXT (clist_row->cell[i])->spacing;
1222             }
1223           
1224           gdk_gc_set_clip_rectangle (fg_gc, &clip_rectangle);
1225           gdk_draw_layout (clist->clist_window, fg_gc,
1226                            offset,
1227                            row_rectangle.y + row_center_offset + clist_row->cell[i].vertical,
1228                            layout);
1229
1230           g_object_unref (G_OBJECT (layout));
1231         }
1232       gdk_gc_set_clip_rectangle (fg_gc, NULL);
1233     }
1234
1235   /* draw focus rectangle */
1236   if (clist->focus_row == row &&
1237       GTK_WIDGET_CAN_FOCUS (widget) && GTK_WIDGET_HAS_FOCUS (widget))
1238     {
1239       if (!area)
1240         gdk_draw_rectangle (clist->clist_window, clist->xor_gc, FALSE,
1241                             row_rectangle.x, row_rectangle.y,
1242                             row_rectangle.width - 1, row_rectangle.height - 1);
1243       else if (gdk_rectangle_intersect (area, &row_rectangle,
1244                                         &intersect_rectangle))
1245         {
1246           gdk_gc_set_clip_rectangle (clist->xor_gc, &intersect_rectangle);
1247           gdk_draw_rectangle (clist->clist_window, clist->xor_gc, FALSE,
1248                               row_rectangle.x, row_rectangle.y,
1249                               row_rectangle.width - 1,
1250                               row_rectangle.height - 1);
1251           gdk_gc_set_clip_rectangle (clist->xor_gc, NULL);
1252         }
1253     }
1254 }
1255
1256 static void
1257 gtk_sctree_change_focus_row_expansion (GtkCTree          *ctree,
1258                             GtkCTreeExpansionType action)
1259 {
1260   GtkCList *clist;
1261   GtkCTreeNode *node;
1262
1263   g_return_if_fail (GTK_IS_CTREE (ctree));
1264
1265   clist = GTK_CLIST (ctree);
1266
1267   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (ctree))) && 
1268       GTK_WIDGET_HAS_GRAB (ctree))
1269     return;
1270   
1271   if (!(node =
1272         GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row))) ||
1273       GTK_CTREE_ROW (node)->is_leaf || !(GTK_CTREE_ROW (node)->children))
1274     return;
1275
1276   switch (action)
1277     {
1278     case GTK_CTREE_EXPANSION_EXPAND:
1279       gtk_ctree_expand_recursive (ctree, node);
1280       break;
1281     case GTK_CTREE_EXPANSION_EXPAND_RECURSIVE:
1282       gtk_ctree_expand_recursive (ctree, node);
1283       break;
1284     case GTK_CTREE_EXPANSION_COLLAPSE:
1285       gtk_ctree_collapse (ctree, node);
1286       break;
1287     case GTK_CTREE_EXPANSION_COLLAPSE_RECURSIVE:
1288       gtk_ctree_collapse_recursive (ctree, node);
1289       break;
1290     case GTK_CTREE_EXPANSION_TOGGLE:
1291       gtk_ctree_toggle_expansion_recursive (ctree, node);
1292       break;
1293     case GTK_CTREE_EXPANSION_TOGGLE_RECURSIVE:
1294       gtk_ctree_toggle_expansion_recursive (ctree, node);
1295       break;
1296     }
1297 }
1298
1299 /* Standard class initialization function */
1300 static void
1301 gtk_sctree_class_init (GtkSCTreeClass *klass)
1302 {
1303         GtkObjectClass *object_class;
1304         GtkWidgetClass *widget_class;
1305         GtkCListClass *clist_class;
1306         GtkCTreeClass *ctree_class;
1307
1308         object_class = (GtkObjectClass *) klass;
1309         widget_class = (GtkWidgetClass *) klass;
1310         clist_class = (GtkCListClass *) klass;
1311         ctree_class = (GtkCTreeClass *) klass;
1312
1313         parent_class = gtk_type_class (gtk_ctree_get_type ());
1314
1315         sctree_signals[ROW_POPUP_MENU] =
1316                 g_signal_new ("row_popup_menu",
1317                               G_TYPE_FROM_CLASS (klass),
1318                               G_SIGNAL_RUN_FIRST,
1319                               G_STRUCT_OFFSET (GtkSCTreeClass, row_popup_menu),
1320                               NULL, NULL,
1321                               sylpheed_marshal_VOID__POINTER,
1322                               G_TYPE_NONE, 1,
1323                               GDK_TYPE_EVENT);
1324         sctree_signals[EMPTY_POPUP_MENU] =
1325                 g_signal_new ("empty_popup_menu",
1326                               G_TYPE_FROM_CLASS (klass),
1327                               G_SIGNAL_RUN_FIRST,
1328                               G_STRUCT_OFFSET (GtkSCTreeClass, empty_popup_menu),
1329                               NULL, NULL,
1330                               sylpheed_marshal_VOID__POINTER,
1331                               G_TYPE_NONE, 1,
1332                               GDK_TYPE_EVENT);
1333         sctree_signals[OPEN_ROW] =
1334                 g_signal_new ("open_row",
1335                               G_TYPE_FROM_CLASS (klass),
1336                               G_SIGNAL_RUN_FIRST,
1337                               G_STRUCT_OFFSET (GtkSCTreeClass, open_row),
1338                               NULL, NULL,
1339                               g_cclosure_marshal_VOID__VOID,
1340                               G_TYPE_NONE, 0);
1341         sctree_signals[START_DRAG] =
1342                 g_signal_new ("start_drag",
1343                               G_TYPE_FROM_CLASS (klass),
1344                               G_SIGNAL_RUN_FIRST,
1345                               G_STRUCT_OFFSET (GtkSCTreeClass, start_drag),
1346                               NULL, NULL,
1347                               sylpheed_marshal_VOID__INT_POINTER,
1348                               G_TYPE_NONE, 2,
1349                               G_TYPE_INT,
1350                               GDK_TYPE_EVENT);
1351
1352         /* gtk_object_class_add_signals (object_class, sctree_signals, LAST_SIGNAL); */
1353
1354         clist_class->clear = gtk_sctree_clear;
1355         clist_class->draw_row = gtk_sctree_draw_row;
1356         ctree_class->tree_collapse = gtk_sctree_collapse;
1357         ctree_class->tree_expand = gtk_sctree_real_tree_expand;
1358         ctree_class->tree_move = sreal_tree_move;
1359         ctree_class->change_focus_row_expansion = gtk_sctree_change_focus_row_expansion;
1360
1361         widget_class->button_press_event = gtk_sctree_button_press;
1362         widget_class->button_release_event = gtk_sctree_button_release;
1363         widget_class->motion_notify_event = gtk_sctree_motion;
1364         widget_class->drag_begin = gtk_sctree_drag_begin;
1365         widget_class->drag_end = gtk_sctree_drag_end;
1366         widget_class->drag_data_get = gtk_sctree_drag_data_get;
1367         widget_class->drag_leave = gtk_sctree_drag_leave;
1368         widget_class->drag_motion = gtk_sctree_drag_motion;
1369         widget_class->drag_drop = gtk_sctree_drag_drop;
1370         widget_class->drag_data_received = gtk_sctree_drag_data_received;
1371 }
1372
1373 /* Standard object initialization function */
1374 static void
1375 gtk_sctree_init (GtkSCTree *sctree)
1376 {
1377         sctree->anchor_row = NULL;
1378
1379         /* GtkCTree does not specify pointer motion by default */
1380         gtk_widget_add_events (GTK_WIDGET (sctree), GDK_POINTER_MOTION_MASK);
1381         gtk_widget_add_events (GTK_WIDGET (sctree), GDK_POINTER_MOTION_MASK);
1382 }
1383
1384 /* Get information the specified row is selected. */
1385
1386 static gboolean
1387 row_is_selected(GtkSCTree *sctree, gint row)
1388 {
1389         GtkCListRow *clist_row;
1390         clist_row =  g_list_nth (GTK_CLIST(sctree)->row_list, row)->data;
1391         return clist_row ? clist_row->state == GTK_STATE_SELECTED : FALSE;
1392 }
1393
1394 /* Selects the rows between the anchor to the specified row, inclusive.  */
1395 static void
1396 select_range (GtkSCTree *sctree, gint row)
1397 {
1398         gint prev_row;
1399         gint min, max;
1400         gint i;
1401         GList *node;
1402         if (sctree->anchor_row == NULL) {
1403                 prev_row = row;
1404                 sctree->anchor_row = gtk_ctree_node_nth(GTK_CTREE(sctree), row);
1405         } else
1406                 prev_row = g_list_position(GTK_CLIST(sctree)->row_list,
1407                                            (GList *)sctree->anchor_row);
1408
1409         if (row < prev_row) {
1410                 min = row;
1411                 max = prev_row;
1412                 GTK_CLIST(sctree)->focus_row = max;
1413         } else {
1414                 min = prev_row;
1415                 max = row;
1416         }
1417         sctree->selecting_range = TRUE;
1418         
1419         if (max < min) {
1420                 int t = min;
1421                 min = max;
1422                 max = t;
1423         }
1424         
1425         if (max - min > 10)
1426                 gtk_clist_freeze(GTK_CLIST(sctree));
1427
1428         node = g_list_nth((GTK_CLIST(sctree))->row_list, min);
1429         for (i = min; i < max; i++) {
1430                 if (node && GTK_CTREE_ROW (node)->row.selectable) {
1431                         g_signal_emit_by_name(G_OBJECT(sctree), "tree_select_row",
1432                                 node, -1);
1433                 }
1434                 node = node->next;
1435         }
1436         if (max - min > 10)
1437                 gtk_clist_thaw(GTK_CLIST(sctree));
1438
1439
1440         sctree->selecting_range = FALSE;
1441         gtk_clist_select_row (GTK_CLIST (sctree), max, -1);
1442 }
1443
1444 /* Handles row selection according to the specified modifier state */
1445 /* in certain cases, we arrive here from a function knowing the GtkCTreeNode, and having
1446  * already slowly found row using g_list_position. In which case, _node will be non-NULL
1447  * to avoid this function having to slowly find it with g_list_nth. */
1448 static void
1449 select_row (GtkSCTree *sctree, gint row, gint col, guint state, GtkCTreeNode *_node)
1450 {
1451         gboolean range, additive;
1452         g_return_if_fail (sctree != NULL);
1453         g_return_if_fail (GTK_IS_SCTREE (sctree));
1454     
1455         range = ((state & GDK_SHIFT_MASK) != 0) &&
1456                 (GTK_CLIST(sctree)->selection_mode != GTK_SELECTION_SINGLE) &&
1457                 (GTK_CLIST(sctree)->selection_mode != GTK_SELECTION_BROWSE);
1458         additive = ((state & GDK_CONTROL_MASK) != 0) &&
1459                    (GTK_CLIST(sctree)->selection_mode != GTK_SELECTION_SINGLE) &&
1460                    (GTK_CLIST(sctree)->selection_mode != GTK_SELECTION_BROWSE);
1461
1462         GTK_CLIST(sctree)->focus_row = row;
1463
1464         if (!additive) {
1465                 /* if this selection isn't additive, we have to unselect what
1466                  * is selected. Here, heavy GUI updates can occur if we have 
1467                  * a big selection. See if more than one line is selected, in
1468                  * which case, freeze, else don't. */
1469
1470                 gboolean should_freeze = FALSE;
1471                 if (sc_g_list_bigger(GTK_CLIST(sctree)->selection, 10)) {
1472                         should_freeze = TRUE;
1473                         sctree->selecting_range = TRUE;
1474                         gtk_clist_freeze (GTK_CLIST (sctree));
1475                 }
1476
1477                 gtk_clist_unselect_all (GTK_CLIST (sctree));
1478
1479                 if (should_freeze) {
1480                         gtk_clist_thaw (GTK_CLIST (sctree));
1481                         sctree->selecting_range = FALSE;
1482                 }
1483         }
1484
1485         if (!range) {
1486                 GtkCTreeNode *node;
1487
1488                 node = _node ? _node : gtk_ctree_node_nth (GTK_CTREE(sctree), row);
1489
1490                 /*No need to manage overlapped list*/
1491                 if (additive) {
1492                         if (row_is_selected(sctree, row))
1493                                 gtk_clist_unselect_row (GTK_CLIST (sctree), row, col);
1494                         else
1495                                 g_signal_emit_by_name
1496                                         (G_OBJECT (sctree),
1497                                          "tree_select_row", node, col);
1498                 } else {
1499                         g_signal_emit_by_name
1500                                 (G_OBJECT (sctree),
1501                                  "tree_select_row", node, col);
1502                 }
1503                 sctree->anchor_row = node;
1504         } else
1505                 select_range (sctree, row);
1506 }
1507
1508 /* Our handler for button_press events.  We override all of GtkCList's broken
1509  * behavior.
1510  */
1511 static gint
1512 gtk_sctree_button_press (GtkWidget *widget, GdkEventButton *event)
1513 {
1514         GtkSCTree *sctree;
1515         GtkCList *clist;
1516         gboolean on_row;
1517         gint row;
1518         gint col;
1519         gint retval;
1520
1521         g_return_val_if_fail (widget != NULL, FALSE);
1522         g_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1523         g_return_val_if_fail (event != NULL, FALSE);
1524
1525         sctree = GTK_SCTREE (widget);
1526         clist = GTK_CLIST (widget);
1527         retval = FALSE;
1528
1529         if (event->window != clist->clist_window)
1530                 return (* GTK_WIDGET_CLASS (parent_class)->button_press_event) (widget, event);
1531
1532         on_row = gtk_clist_get_selection_info (clist, event->x, event->y, &row, &col);
1533
1534         if (on_row && !GTK_WIDGET_HAS_FOCUS(widget))
1535                 gtk_widget_grab_focus (widget);
1536
1537         if (gtk_ctree_is_hot_spot (GTK_CTREE(sctree), event->x, event->y)) {
1538                 GtkCTreeNode *node = gtk_ctree_node_nth(GTK_CTREE(sctree), row);
1539                 if (GTK_CTREE_ROW (node)->expanded)
1540                         gtk_ctree_collapse(GTK_CTREE(sctree), node);
1541                 else
1542                         gtk_ctree_expand_recursive(GTK_CTREE(sctree), node);
1543                 return TRUE;
1544         }
1545
1546         switch (event->type) {
1547         case GDK_BUTTON_PRESS:
1548                 if (event->button == 1 || event->button == 2) {
1549                         if (event->button == 2)
1550                                 event->state &= ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK);
1551                         if (on_row) {
1552                                 /* Save the mouse info for DnD */
1553                                 sctree->dnd_press_button = event->button;
1554                                 sctree->dnd_press_x = event->x;
1555                                 sctree->dnd_press_y = event->y;
1556
1557                                 /* Handle selection */
1558                                 if ((row_is_selected (sctree, row)
1559                                      && !(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)))
1560                                     || ((event->state & GDK_CONTROL_MASK)
1561                                         && !(event->state & GDK_SHIFT_MASK))) {
1562                                         sctree->dnd_select_pending = TRUE;
1563                                         sctree->dnd_select_pending_state = event->state;
1564                                         sctree->dnd_select_pending_row = row;
1565                                 } else {
1566                                         select_row (sctree, row, col, event->state, NULL);
1567                                 }
1568                         } else {
1569                                 sctree->selecting_range = TRUE;
1570                                 gtk_clist_unselect_all (clist);
1571                                 sctree->selecting_range = FALSE;
1572                         }
1573
1574                         retval = TRUE;
1575                 } else if (event->button == 3) {
1576                         /* Emit *_popup_menu signal*/
1577                         if (on_row) {
1578                                 if (!row_is_selected(sctree,row))
1579                                         select_row (sctree, row, col, 0, NULL);
1580                                 g_signal_emit (G_OBJECT (sctree),
1581                                                  sctree_signals[ROW_POPUP_MENU],
1582                                                  0, event);
1583                         } else {
1584                                 sctree->selecting_range = TRUE;
1585                                 gtk_clist_unselect_all(clist);
1586                                 sctree->selecting_range = FALSE;
1587                                 g_signal_emit (G_OBJECT (sctree),
1588                                                  sctree_signals[EMPTY_POPUP_MENU],
1589                                                  0, event);
1590                         }
1591                         retval = TRUE;
1592                 }
1593
1594                 break;
1595
1596         case GDK_2BUTTON_PRESS:
1597                 if (event->button != 1)
1598                         break;
1599
1600                 sctree->dnd_select_pending = FALSE;
1601                 sctree->dnd_select_pending_state = 0;
1602
1603                 if (on_row)
1604                         g_signal_emit (G_OBJECT (sctree),
1605                                        sctree_signals[OPEN_ROW], 0);
1606
1607                 retval = TRUE;
1608                 break;
1609
1610         default:
1611                 break;
1612         }
1613
1614         return retval;
1615 }
1616
1617 /* Our handler for button_release events.  We override all of GtkCList's broken
1618  * behavior.
1619  */
1620 static gint
1621 gtk_sctree_button_release (GtkWidget *widget, GdkEventButton *event)
1622 {
1623         GtkSCTree *sctree;
1624         GtkCList *clist;
1625         gint on_row;
1626         gint row, col;
1627         gint retval;
1628
1629         g_return_val_if_fail (widget != NULL, FALSE);
1630         g_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1631         g_return_val_if_fail (event != NULL, FALSE);
1632
1633         sctree = GTK_SCTREE (widget);
1634         clist = GTK_CLIST (widget);
1635         retval = FALSE;
1636
1637         if (event->window != clist->clist_window)
1638                 return (* GTK_WIDGET_CLASS (parent_class)->button_release_event) (widget, event);
1639
1640         on_row = gtk_clist_get_selection_info (clist, event->x, event->y, &row, &col);
1641
1642         if (!(event->button == 1 || event->button == 2))
1643                 return FALSE;
1644
1645         sctree->dnd_press_button = 0;
1646         sctree->dnd_press_x = 0;
1647         sctree->dnd_press_y = 0;
1648
1649         if (on_row) {
1650                 if (sctree->dnd_select_pending) {
1651                         select_row (sctree, row, col, sctree->dnd_select_pending_state, NULL);
1652                         sctree->dnd_select_pending = FALSE;
1653                         sctree->dnd_select_pending_state = 0;
1654                 }
1655
1656                 retval = TRUE;
1657         }
1658
1659         return retval;
1660 }
1661
1662 /* Our handler for motion_notify events.  We override all of GtkCList's broken
1663  * behavior.
1664  */
1665 static gint
1666 gtk_sctree_motion (GtkWidget *widget, GdkEventMotion *event)
1667 {
1668         GtkSCTree *sctree;
1669         GtkCList *clist;
1670
1671         g_return_val_if_fail (widget != NULL, FALSE);
1672         g_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1673         g_return_val_if_fail (event != NULL, FALSE);
1674
1675         sctree = GTK_SCTREE (widget);
1676         clist = GTK_CLIST (widget);
1677
1678         if (event->window != clist->clist_window)
1679                 return (* GTK_WIDGET_CLASS (parent_class)->motion_notify_event) (widget, event);
1680
1681         if (!((sctree->dnd_press_button == 1 && (event->state & GDK_BUTTON1_MASK))
1682               || (sctree->dnd_press_button == 2 && (event->state & GDK_BUTTON2_MASK))))
1683                 return FALSE;
1684
1685         /* This is the same threshold value that is used in gtkdnd.c */
1686
1687         if (MAX (ABS (sctree->dnd_press_x - event->x),
1688                  ABS (sctree->dnd_press_y - event->y)) <= 3)
1689                 return FALSE;
1690
1691         /* Handle any pending selections */
1692
1693         if (sctree->dnd_select_pending) {
1694                 if (!row_is_selected(sctree,sctree->dnd_select_pending_row))
1695                         select_row (sctree,
1696                                     sctree->dnd_select_pending_row,
1697                                     -1,
1698                                     sctree->dnd_select_pending_state,
1699                                     NULL);
1700
1701                 sctree->dnd_select_pending = FALSE;
1702                 sctree->dnd_select_pending_state = 0;
1703         }
1704
1705         g_signal_emit (G_OBJECT (sctree),
1706                        sctree_signals[START_DRAG],
1707                        0,
1708                        sctree->dnd_press_button,
1709                        event);
1710         return TRUE;
1711 }
1712
1713 /* We override the drag_begin signal to do nothing */
1714 static void
1715 gtk_sctree_drag_begin (GtkWidget *widget, GdkDragContext *context)
1716 {
1717         /* nothing */
1718 }
1719
1720 /* We override the drag_end signal to do nothing */
1721 static void
1722 gtk_sctree_drag_end (GtkWidget *widget, GdkDragContext *context)
1723 {
1724         /* nothing */
1725 }
1726
1727 /* We override the drag_data_get signal to do nothing */
1728 static void
1729 gtk_sctree_drag_data_get (GtkWidget *widget, GdkDragContext *context,
1730                                      GtkSelectionData *data, guint info, guint time)
1731 {
1732         /* nothing */
1733 }
1734
1735 /* We override the drag_leave signal to do nothing */
1736 static void
1737 gtk_sctree_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time)
1738 {
1739         /* nothing */
1740 }
1741
1742 /* We override the drag_motion signal to do nothing */
1743 static gboolean
1744 gtk_sctree_drag_motion (GtkWidget *widget, GdkDragContext *context,
1745                                    gint x, gint y, guint time)
1746 {
1747         return FALSE;
1748 }
1749
1750 /* We override the drag_drop signal to do nothing */
1751 static gboolean
1752 gtk_sctree_drag_drop (GtkWidget *widget, GdkDragContext *context,
1753                                  gint x, gint y, guint time)
1754 {
1755         return FALSE;
1756 }
1757
1758 /* We override the drag_data_received signal to do nothing */
1759 static void
1760 gtk_sctree_drag_data_received (GtkWidget *widget, GdkDragContext *context,
1761                                           gint x, gint y, GtkSelectionData *data,
1762                                           guint info, guint time)
1763 {
1764         /* nothing */
1765 }
1766
1767 /* Our handler for the clear signal of the clist.  We have to reset the anchor
1768  * to null.
1769  */
1770 static void
1771 gtk_sctree_clear (GtkCList *clist)
1772 {
1773         GtkSCTree *sctree;
1774
1775         g_return_if_fail (clist != NULL);
1776         g_return_if_fail (GTK_IS_SCTREE (clist));
1777
1778         sctree = GTK_SCTREE (clist);
1779         sctree->anchor_row = NULL;
1780
1781         if (((GtkCListClass *)parent_class)->clear)
1782                 (* ((GtkCListClass *)parent_class)->clear) (clist);
1783 }
1784
1785 /* Our handler for the change_focus_row_expansion signal of the ctree.  
1786  We have to set the anchor to parent visible node.
1787  */
1788 static void 
1789 gtk_sctree_collapse (GtkCTree *ctree, GtkCTreeNode *node)
1790 {
1791         g_return_if_fail (ctree != NULL);
1792         g_return_if_fail (GTK_IS_SCTREE (ctree));
1793
1794         (* parent_class->tree_collapse) (ctree, node);
1795         GTK_SCTREE(ctree)->anchor_row =
1796                 gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->focus_row);
1797 }
1798
1799 GtkWidget *gtk_sctree_new_with_titles (gint columns, gint tree_column, 
1800                                        gchar *titles[])
1801 {
1802         GtkWidget *widget;
1803                                                                                                             
1804         g_return_val_if_fail (columns > 0, NULL);
1805         g_return_val_if_fail (tree_column >= 0, NULL);
1806                                                                                                             
1807         widget = gtk_widget_new (TYPE_GTK_SCTREE,
1808                                  "n_columns", columns,
1809                                  "tree_column", tree_column,
1810                                  NULL);
1811         if (titles) {
1812                 GtkCList *clist = GTK_CLIST (widget);
1813                 guint i;
1814
1815                 for (i = 0; i < columns; i++)
1816                         gtk_clist_set_column_title (clist, i, titles[i]);
1817                 gtk_clist_column_titles_show (clist);
1818         }
1819
1820         GTK_SCTREE(widget)->show_stripes = TRUE;
1821
1822         return widget;
1823 }
1824
1825 void gtk_sctree_select (GtkSCTree *sctree, GtkCTreeNode *node)
1826 {
1827         select_row(sctree, 
1828                    g_list_position(GTK_CLIST(sctree)->row_list, (GList *)node),
1829                    -1, 0, node);
1830 }
1831
1832 void gtk_sctree_select_with_state (GtkSCTree *sctree, GtkCTreeNode *node, int state)
1833 {
1834         select_row(sctree, 
1835                    g_list_position(GTK_CLIST(sctree)->row_list, (GList *)node),
1836                    -1, state, node);
1837 }
1838
1839 void gtk_sctree_unselect_all (GtkSCTree *sctree)
1840 {
1841         gboolean froze = FALSE;
1842         sctree->selecting_range = TRUE;
1843         if (sc_g_list_bigger(GTK_CLIST(sctree)->selection, 1)) {
1844                 gtk_clist_freeze(GTK_CLIST(sctree));
1845                 froze = TRUE;
1846         }
1847         gtk_clist_unselect_all(GTK_CLIST(sctree));
1848         if (froze)
1849                 gtk_clist_thaw(GTK_CLIST(sctree));
1850         sctree->selecting_range = FALSE;
1851         sctree->anchor_row = NULL;
1852 }
1853
1854 void gtk_sctree_set_anchor_row (GtkSCTree *sctree, GtkCTreeNode *node)
1855 {
1856         sctree->anchor_row = node;
1857 }
1858
1859 void gtk_sctree_remove_node (GtkSCTree *sctree, GtkCTreeNode *node)
1860 {
1861         if (sctree->anchor_row == node)
1862                 sctree->anchor_row = NULL;
1863         gtk_ctree_remove_node(GTK_CTREE(sctree), node);
1864 }
1865
1866 void gtk_sctree_set_stripes(GtkSCTree  *sctree, gboolean show_stripes)
1867 {
1868         sctree->show_stripes = show_stripes;
1869 }
1870
1871 /***********************************************************
1872  *             Tree sorting functions                      *
1873  ***********************************************************/
1874
1875 static void sink(GtkCList *clist, GPtrArray *numbers, gint root, gint bottom)
1876 {
1877         gint j, k ;
1878         GtkCTreeNode *temp;
1879
1880         j = 2 * root;
1881         k = j + 1;
1882
1883         /* find the maximum element of numbers[root],
1884            numbers[2*root] and numbers[2*root+1] */
1885         if (j <= bottom) {
1886                 if (clist->compare( clist, GTK_CTREE_ROW (g_ptr_array_index(numbers, root)),
1887                                     GTK_CTREE_ROW(g_ptr_array_index( numbers, j))) >= 0)
1888                         j = root;
1889                 if (k <= bottom)
1890                         if (clist->compare( clist, GTK_CTREE_ROW (g_ptr_array_index(numbers, k)),
1891                                             GTK_CTREE_ROW (g_ptr_array_index( numbers, j))) > 0)
1892                                 j = k;
1893                 /* if numbers[root] wasn't the maximum element then
1894                    sink again */
1895                 if (root != j) {
1896                         temp = g_ptr_array_index( numbers,root);
1897                         g_ptr_array_index( numbers, root) = g_ptr_array_index( numbers, j);
1898                         g_ptr_array_index( numbers, j) = temp;
1899                         sink( clist, numbers, j, bottom);
1900                 }
1901         }
1902 }
1903
1904 static void heap_sort(GtkCList *clist, GPtrArray *numbers, gint array_size)
1905 {
1906         gint i;
1907         GtkCTreeNode *temp;
1908         
1909         /* build the Heap */
1910         for (i = (array_size / 2); i >= 1; i--)
1911                 sink( clist, numbers, i, array_size);
1912         /* output the Heap */
1913         for (i = array_size; i >= 2; i--) {
1914                 temp = g_ptr_array_index( numbers, 1);
1915                 g_ptr_array_index( numbers, 1) = g_ptr_array_index( numbers, i);
1916                 g_ptr_array_index( numbers, i) = temp;
1917                 sink( clist, numbers, 1, i-1);
1918         }
1919 }
1920
1921 static void
1922 stree_sort (GtkCTree    *ctree,
1923            GtkCTreeNode *node,
1924            gpointer      data)
1925 {
1926         GtkCTreeNode *list_start, *work, *next;
1927         GPtrArray *row_array, *viewable_array;
1928         GtkCList *clist;
1929         gint i;
1930
1931         clist = GTK_CLIST (ctree);
1932
1933         if (node)
1934                 work = GTK_CTREE_ROW (node)->children;
1935         else
1936                 work = GTK_CTREE_NODE (clist->row_list);
1937
1938         row_array = g_ptr_array_new();
1939         viewable_array = g_ptr_array_new();
1940
1941         if (work) {
1942                 g_ptr_array_add( row_array, NULL);
1943                 while (work) {
1944                         /* add all rows to row_array */
1945                         g_ptr_array_add( row_array, work);
1946                         if (GTK_CTREE_ROW (work)->parent && gtk_ctree_is_viewable( ctree, work))
1947                                 g_ptr_array_add( viewable_array, GTK_CTREE_ROW (work)->parent);
1948                         next = GTK_CTREE_ROW (work)->sibling;
1949                         gtk_sctree_unlink( ctree, work, FALSE);
1950                         work = next;
1951                 }
1952
1953                 heap_sort( clist, row_array, (row_array->len)-1);
1954
1955                 if (node)
1956                         list_start = GTK_CTREE_ROW (node)->children;
1957                 else
1958                         list_start = GTK_CTREE_NODE (clist->row_list);
1959
1960                 if (clist->sort_type == GTK_SORT_ASCENDING) {
1961                         for (i=(row_array->len)-1; i>=1; i--) {
1962                                 work = g_ptr_array_index( row_array, i);
1963                                 gtk_sctree_link( ctree, work, node, list_start, FALSE);
1964                                 list_start = work;
1965                                 /* insert work at the beginning of the list */
1966                         }
1967                 } else {
1968                         for (i=1; i<row_array->len; i++) {
1969                                 work = g_ptr_array_index( row_array, i);
1970                                 gtk_sctree_link( ctree, work, node, list_start, FALSE);
1971                                 list_start = work;
1972                                 /* insert work at the beginning of the list */
1973                         }
1974                 }
1975
1976                 for (i=0; i<viewable_array->len; i++) {
1977                         gtk_ctree_expand( ctree, g_ptr_array_index( viewable_array, i));
1978                 }
1979                 
1980         }
1981         g_ptr_array_free( row_array, TRUE);
1982         g_ptr_array_free( viewable_array, TRUE);
1983 }
1984
1985 void
1986 gtk_sctree_sort_recursive (GtkCTree     *ctree, 
1987                           GtkCTreeNode *node)
1988 {
1989         GtkCList *clist;
1990         GtkCTreeNode *focus_node = NULL;
1991
1992         g_return_if_fail (ctree != NULL);
1993         g_return_if_fail (GTK_IS_CTREE (ctree));
1994
1995         clist = GTK_CLIST (ctree);
1996
1997         gtk_clist_freeze (clist);
1998
1999         if (clist->selection_mode == GTK_SELECTION_EXTENDED) {
2000                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2001       
2002                 g_list_free (clist->undo_selection);
2003                 g_list_free (clist->undo_unselection);
2004                 clist->undo_selection = NULL;
2005                 clist->undo_unselection = NULL;
2006         }
2007
2008         if (!node || (node && gtk_ctree_is_viewable (ctree, node)))
2009                 focus_node = GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
2010       
2011         GTK_SCTREE(ctree)->sorting = TRUE;
2012
2013         gtk_ctree_post_recursive (ctree, node, GTK_CTREE_FUNC (stree_sort), NULL);
2014
2015         if (!node)
2016                 stree_sort (ctree, NULL, NULL);
2017
2018         GTK_SCTREE(ctree)->sorting = FALSE;
2019
2020         if (focus_node) {
2021                 clist->focus_row = g_list_position (clist->row_list,(GList *)focus_node);
2022                 clist->undo_anchor = clist->focus_row;
2023         }
2024
2025         gtk_clist_thaw (clist);
2026 }
2027
2028 void
2029 gtk_sctree_sort_node (GtkCTree     *ctree, 
2030                      GtkCTreeNode *node)
2031 {
2032         GtkCList *clist;
2033         GtkCTreeNode *focus_node = NULL;
2034
2035         g_return_if_fail (ctree != NULL);
2036         g_return_if_fail (GTK_IS_CTREE (ctree));
2037
2038         clist = GTK_CLIST (ctree);
2039
2040         gtk_clist_freeze (clist);
2041
2042         if (clist->selection_mode == GTK_SELECTION_EXTENDED) {
2043                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2044
2045                 g_list_free (clist->undo_selection);
2046                 g_list_free (clist->undo_unselection);
2047                 clist->undo_selection = NULL;
2048                 clist->undo_unselection = NULL;
2049         }
2050
2051         if (!node || (node && gtk_ctree_is_viewable (ctree, node)))
2052                 focus_node = GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
2053
2054         GTK_SCTREE(ctree)->sorting = TRUE;
2055
2056         stree_sort (ctree, node, NULL);
2057
2058         GTK_SCTREE(ctree)->sorting = FALSE;
2059
2060         if (focus_node) {
2061                 clist->focus_row = g_list_position (clist->row_list,(GList *)focus_node);
2062                 clist->undo_anchor = clist->focus_row;
2063         }
2064
2065         gtk_clist_thaw (clist);
2066 }
2067
2068 /************************************************************************/
2069
2070 static void
2071 gtk_sctree_unlink (GtkCTree     *ctree, 
2072                   GtkCTreeNode *node,
2073                   gboolean      update_focus_row)
2074 {
2075         GtkCList *clist;
2076         gint rows;
2077         gint level;
2078         gint visible;
2079         GtkCTreeNode *work;
2080         GtkCTreeNode *parent;
2081         GList *list;
2082
2083         g_return_if_fail (ctree != NULL);
2084         g_return_if_fail (GTK_IS_CTREE (ctree));
2085         g_return_if_fail (node != NULL);
2086
2087         clist = GTK_CLIST (ctree);
2088   
2089         if (update_focus_row && clist->selection_mode == GTK_SELECTION_EXTENDED) {
2090                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2091
2092                 g_list_free (clist->undo_selection);
2093                 g_list_free (clist->undo_unselection);
2094                 clist->undo_selection = NULL;
2095                 clist->undo_unselection = NULL;
2096         }
2097
2098         visible = gtk_ctree_is_viewable (ctree, node);
2099
2100         /* clist->row_list_end unlinked ? */
2101         if (visible && (GTK_CTREE_NODE_NEXT (node) == NULL ||
2102            (GTK_CTREE_ROW (node)->children && gtk_ctree_is_ancestor (ctree, node,
2103             GTK_CTREE_NODE (clist->row_list_end)))))
2104                 clist->row_list_end = (GList *) (GTK_CTREE_NODE_PREV (node));
2105
2106         /* update list */
2107         rows = 0;
2108         level = GTK_CTREE_ROW (node)->level;
2109         work = GTK_CTREE_NODE_NEXT (node);
2110         while (work && GTK_CTREE_ROW (work)->level > level) {
2111                 work = GTK_CTREE_NODE_NEXT (work);
2112                 rows++;
2113         }
2114
2115         if (visible) {
2116                 clist->rows -= (rows + 1);
2117
2118                 if (update_focus_row) {
2119                         gint pos;
2120                         pos = g_list_position (clist->row_list, (GList *)node);
2121                         if (pos + rows < clist->focus_row)
2122                                 clist->focus_row -= (rows + 1);
2123                         else if (pos <= clist->focus_row) {
2124                                 if (!GTK_CTREE_ROW (node)->sibling)
2125                                         clist->focus_row = MAX (pos - 1, 0);
2126                                 else
2127                                         clist->focus_row = pos;
2128               
2129                                 clist->focus_row = MIN (clist->focus_row, clist->rows - 1);
2130                         }
2131                         clist->undo_anchor = clist->focus_row;
2132                 }
2133         }
2134
2135         if (work) {
2136                 list = (GList *)GTK_CTREE_NODE_PREV (work);
2137                 list->next = NULL;
2138                 list = (GList *)work;
2139                 list->prev = (GList *)GTK_CTREE_NODE_PREV (node);
2140         }
2141
2142         if (GTK_CTREE_NODE_PREV (node) &&
2143             GTK_CTREE_NODE_NEXT (GTK_CTREE_NODE_PREV (node)) == node) {
2144                 list = (GList *)GTK_CTREE_NODE_PREV (node);
2145                 list->next = (GList *)work;
2146         }
2147
2148         /* update tree */
2149         parent = GTK_CTREE_ROW (node)->parent;
2150         if (parent) {
2151                 if (GTK_CTREE_ROW (parent)->children == node) {
2152                         GTK_CTREE_ROW (parent)->children = GTK_CTREE_ROW (node)->sibling;
2153                 }
2154                 else {
2155                         GtkCTreeNode *sibling;
2156
2157                         sibling = GTK_CTREE_ROW (parent)->children;
2158                         while (GTK_CTREE_ROW (sibling)->sibling != node)
2159                                 sibling = GTK_CTREE_ROW (sibling)->sibling;
2160                         GTK_CTREE_ROW (sibling)->sibling = GTK_CTREE_ROW (node)->sibling;
2161                 }
2162         }
2163         else {
2164                 if (clist->row_list == (GList *)node)
2165                         clist->row_list = (GList *) (GTK_CTREE_ROW (node)->sibling);
2166                 else {
2167                         GtkCTreeNode *sibling;
2168
2169                         sibling = GTK_CTREE_NODE (clist->row_list);
2170                         while (GTK_CTREE_ROW (sibling)->sibling != node)
2171                                 sibling = GTK_CTREE_ROW (sibling)->sibling;
2172                         GTK_CTREE_ROW (sibling)->sibling = GTK_CTREE_ROW (node)->sibling;
2173                 }
2174         }
2175 }
2176
2177 static void
2178 gtk_sctree_link (GtkCTree     *ctree,
2179                 GtkCTreeNode *node,
2180                 GtkCTreeNode *parent,
2181                 GtkCTreeNode *sibling,
2182                 gboolean      update_focus_row)
2183 {
2184         GtkCList *clist;
2185         GList *list_end;
2186         GList *list;
2187         GList *work;
2188         gboolean visible = FALSE;
2189         gint rows = 0;
2190   
2191         if (sibling)
2192                 g_return_if_fail (GTK_CTREE_ROW (sibling)->parent == parent);
2193         g_return_if_fail (node != NULL);
2194         g_return_if_fail (node != sibling);
2195         g_return_if_fail (node != parent);
2196
2197         clist = GTK_CLIST (ctree);
2198
2199         if (update_focus_row && clist->selection_mode == GTK_SELECTION_EXTENDED) {
2200                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2201
2202                 g_list_free (clist->undo_selection);
2203                 g_list_free (clist->undo_unselection);
2204                 clist->undo_selection = NULL;
2205                 clist->undo_unselection = NULL;
2206         }
2207
2208         for (rows = 1, list_end = (GList *)node; list_end->next;
2209              list_end = list_end->next)
2210                 rows++;
2211
2212         GTK_CTREE_ROW (node)->parent = parent;
2213         GTK_CTREE_ROW (node)->sibling = sibling;
2214
2215         if (!parent || (parent && (gtk_ctree_is_viewable (ctree, parent) &&
2216             GTK_CTREE_ROW (parent)->expanded))) {
2217                 visible = TRUE;
2218                 clist->rows += rows;
2219         }
2220
2221         if (parent)
2222                 work = (GList *)(GTK_CTREE_ROW (parent)->children);
2223         else
2224                 work = clist->row_list;
2225
2226         if (sibling) {
2227                 if (work != (GList *)sibling) {
2228                         while (GTK_CTREE_ROW (work)->sibling != sibling)
2229                                 work = (GList *)(GTK_CTREE_ROW (work)->sibling);
2230                         GTK_CTREE_ROW (work)->sibling = node;
2231                 }
2232
2233                 if (sibling == GTK_CTREE_NODE (clist->row_list))
2234                 clist->row_list = (GList *) node;
2235                 if (GTK_CTREE_NODE_PREV (sibling) &&
2236                     GTK_CTREE_NODE_NEXT (GTK_CTREE_NODE_PREV (sibling)) == sibling) {
2237                         list = (GList *)GTK_CTREE_NODE_PREV (sibling);
2238                         list->next = (GList *)node;
2239                 }
2240
2241                 list = (GList *)node;
2242                 list->prev = (GList *)GTK_CTREE_NODE_PREV (sibling);
2243                 list_end->next = (GList *)sibling;
2244                 list = (GList *)sibling;
2245                 list->prev = list_end;
2246                 if (parent && GTK_CTREE_ROW (parent)->children == sibling)
2247                         GTK_CTREE_ROW (parent)->children = node;
2248         }
2249         else {
2250                 if (work) {
2251                         /* find sibling */
2252                         while (GTK_CTREE_ROW (work)->sibling)
2253                         work = (GList *)(GTK_CTREE_ROW (work)->sibling);
2254                         GTK_CTREE_ROW (work)->sibling = node;
2255
2256                         /* find last visible child of sibling */
2257                         work = (GList *) gtk_sctree_last_visible (ctree,
2258                                GTK_CTREE_NODE (work));
2259
2260                         list_end->next = work->next;
2261                         if (work->next)
2262                                 list = work->next->prev = list_end;
2263                         work->next = (GList *)node;
2264                         list = (GList *)node;
2265                         list->prev = work;
2266                 }
2267                 else {
2268                         if (parent) {
2269                                 GTK_CTREE_ROW (parent)->children = node;
2270                                 list = (GList *)node;
2271                                 list->prev = (GList *)parent;
2272                                 if (GTK_CTREE_ROW (parent)->expanded) {
2273                                         list_end->next = (GList *)GTK_CTREE_NODE_NEXT (parent);
2274                                         if (GTK_CTREE_NODE_NEXT(parent)) {
2275                                                 list = (GList *)GTK_CTREE_NODE_NEXT (parent);
2276                                                 list->prev = list_end;
2277                                         }
2278                                         list = (GList *)parent;
2279                                         list->next = (GList *)node;
2280                                 }
2281                                 else
2282                                         list_end->next = NULL;
2283                         }
2284                         else {
2285                                 clist->row_list = (GList *)node;
2286                                 list = (GList *)node;
2287                                 list->prev = NULL;
2288                                 list_end->next = NULL;
2289                         }
2290                 }
2291         }
2292
2293         gtk_ctree_pre_recursive (ctree, node, stree_update_level, NULL); 
2294
2295         if (clist->row_list_end == NULL ||
2296             clist->row_list_end->next == (GList *)node)
2297                 clist->row_list_end = list_end;
2298
2299         if (visible && update_focus_row) {
2300                 gint pos;
2301                 pos = g_list_position (clist->row_list, (GList *)node);
2302   
2303                 if (pos <= clist->focus_row) {
2304                         clist->focus_row += rows;
2305                         clist->undo_anchor = clist->focus_row;
2306                 }
2307         }
2308 }
2309
2310 static void
2311 stree_update_level (GtkCTree     *ctree, 
2312                    GtkCTreeNode *node, 
2313                    gpointer      data)
2314 {
2315         if (!node)
2316                 return;
2317
2318         if (GTK_CTREE_ROW (node)->parent)
2319                 GTK_CTREE_ROW (node)->level = 
2320                 GTK_CTREE_ROW (GTK_CTREE_ROW (node)->parent)->level + 1;
2321         else
2322                 GTK_CTREE_ROW (node)->level = 1;
2323 }
2324
2325 static GtkCTreeNode *
2326 gtk_sctree_last_visible (GtkCTree     *ctree,
2327                         GtkCTreeNode *node)
2328 {
2329         GtkCTreeNode *work;
2330   
2331         if (!node)
2332                 return NULL;
2333
2334         work = GTK_CTREE_ROW (node)->children;
2335
2336         if (!work || !GTK_CTREE_ROW (node)->expanded)
2337                 return node;
2338
2339         while (GTK_CTREE_ROW (work)->sibling)
2340                 work = GTK_CTREE_ROW (work)->sibling;
2341
2342         return gtk_sctree_last_visible (ctree, work);
2343 }
2344
2345 /* this wrapper simply replaces NULL pixmaps 
2346  * with a transparent, 1x1 pixmap. This works
2347  * around a memory problem deep inside gtk, 
2348  * revealed by valgrind. 
2349  */
2350 /*GtkCTreeNode* gtk_sctree_insert_node        (GtkCTree *ctree,
2351                                              GtkCTreeNode *parent,
2352                                              GtkCTreeNode *sibling,
2353                                              gchar *text[],
2354                                              guint8 spacing,
2355                                              GdkPixmap *pixmap_closed,
2356                                              GdkBitmap *mask_closed,
2357                                              GdkPixmap *pixmap_opened,
2358                                              GdkBitmap *mask_opened,
2359                                              gboolean is_leaf,
2360                                              gboolean expanded)
2361 {
2362         if (!emptyxpm) {
2363                 stock_pixmap_gdk(GTK_WIDGET(ctree), STOCK_PIXMAP_EMPTY,
2364                          &emptyxpm, &emptyxpmmask);
2365         }
2366         if (!pixmap_closed) {
2367                 pixmap_closed = emptyxpm;
2368                 mask_closed = emptyxpmmask;
2369         }
2370         if (!pixmap_opened) {
2371                 pixmap_opened = emptyxpm;
2372                 mask_opened = emptyxpmmask;
2373         }
2374         return gtk_ctree_insert_node(ctree, parent, sibling, text,spacing,
2375                 pixmap_closed, mask_closed, pixmap_opened, mask_opened,
2376                 is_leaf, expanded);
2377 }*/
2378
2379 static void 
2380 sset_node_info (GtkCTree     *ctree,
2381                GtkCTreeNode *node,
2382                const gchar  *text,
2383                guint8        spacing,
2384                GdkPixmap    *pixmap_closed,
2385                GdkBitmap    *mask_closed,
2386                GdkPixmap    *pixmap_opened,
2387                GdkBitmap    *mask_opened,
2388                gboolean      is_leaf,
2389                gboolean      expanded)
2390 {
2391   if (GTK_CTREE_ROW (node)->pixmap_opened)
2392     {
2393       gdk_pixmap_unref (GTK_CTREE_ROW (node)->pixmap_opened);
2394       if (GTK_CTREE_ROW (node)->mask_opened) 
2395         gdk_bitmap_unref (GTK_CTREE_ROW (node)->mask_opened);
2396     }
2397   if (GTK_CTREE_ROW (node)->pixmap_closed)
2398     {
2399       gdk_pixmap_unref (GTK_CTREE_ROW (node)->pixmap_closed);
2400       if (GTK_CTREE_ROW (node)->mask_closed) 
2401         gdk_bitmap_unref (GTK_CTREE_ROW (node)->mask_closed);
2402     }
2403
2404   GTK_CTREE_ROW (node)->pixmap_opened = NULL;
2405   GTK_CTREE_ROW (node)->mask_opened   = NULL;
2406   GTK_CTREE_ROW (node)->pixmap_closed = NULL;
2407   GTK_CTREE_ROW (node)->mask_closed   = NULL;
2408
2409   if (pixmap_closed)
2410     {
2411       GTK_CTREE_ROW (node)->pixmap_closed = gdk_pixmap_ref (pixmap_closed);
2412       if (mask_closed) 
2413         GTK_CTREE_ROW (node)->mask_closed = gdk_bitmap_ref (mask_closed);
2414     }
2415   if (pixmap_opened)
2416     {
2417       GTK_CTREE_ROW (node)->pixmap_opened = gdk_pixmap_ref (pixmap_opened);
2418       if (mask_opened) 
2419         GTK_CTREE_ROW (node)->mask_opened = gdk_bitmap_ref (mask_opened);
2420     }
2421
2422   GTK_CTREE_ROW (node)->is_leaf  = is_leaf;
2423   GTK_CTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2424
2425   if (GTK_CTREE_ROW (node)->expanded)
2426     gtk_ctree_node_set_pixtext (ctree, node, ctree->tree_column,
2427                                 text, spacing, pixmap_opened, mask_opened);
2428   else 
2429     gtk_ctree_node_set_pixtext (ctree, node, ctree->tree_column,
2430                                 text, spacing, pixmap_closed, mask_closed);
2431 }
2432
2433 static void
2434 stree_draw_node (GtkCTree     *ctree, 
2435                 GtkCTreeNode *node)
2436 {
2437   GtkCList *clist;
2438   
2439   clist = GTK_CLIST (ctree);
2440
2441   if (CLIST_UNFROZEN (clist) && gtk_ctree_is_viewable (ctree, node))
2442     {
2443       GtkCTreeNode *work;
2444       gint num = 0;
2445       
2446       work = GTK_CTREE_NODE (clist->row_list);
2447       while (work && work != node)
2448         {
2449           work = GTK_CTREE_NODE_NEXT (work);
2450           num++;
2451         }
2452       if (work && gtk_clist_row_is_visible (clist, num) != GTK_VISIBILITY_NONE)
2453         GTK_CLIST_GET_CLASS (clist)->draw_row
2454           (clist, NULL, num, GTK_CLIST_ROW ((GList *) node));
2455     }
2456 }
2457
2458 /* this wrapper simply replaces NULL pixmaps 
2459  * with a transparent, 1x1 pixmap. This works
2460  * around a memory problem deep inside gtk, 
2461  * revealed by valgrind. 
2462  */
2463 void        gtk_sctree_set_node_info        (GtkCTree *ctree,
2464                                              GtkCTreeNode *node,
2465                                              const gchar *text,
2466                                              guint8 spacing,
2467                                              GdkPixmap *pixmap_closed,
2468                                              GdkBitmap *mask_closed,
2469                                              GdkPixmap *pixmap_opened,
2470                                              GdkBitmap *mask_opened,
2471                                              gboolean is_leaf,
2472                                              gboolean expanded)
2473 {
2474   gboolean old_leaf;
2475   gboolean old_expanded;
2476   GtkCTreeNode *work;
2477  
2478   if (!emptyxpm) {
2479           stock_pixmap_gdk(GTK_WIDGET(ctree), STOCK_PIXMAP_EMPTY,
2480                    &emptyxpm, &emptyxpmmask);
2481   }
2482   if (!pixmap_closed) {
2483           pixmap_closed = emptyxpm;
2484           mask_closed = emptyxpmmask;
2485   }
2486   if (!pixmap_opened) {
2487           pixmap_opened = emptyxpm;
2488           mask_opened = emptyxpmmask;
2489   }
2490
2491   if (!GTK_IS_CTREE (ctree) || !node) return;
2492
2493   old_leaf = GTK_CTREE_ROW (node)->is_leaf;
2494   old_expanded = GTK_CTREE_ROW (node)->expanded;
2495
2496   if (is_leaf && (work = GTK_CTREE_ROW (node)->children) != NULL)
2497     {
2498       GtkCTreeNode *ptr;
2499       
2500       while (work)
2501         {
2502           ptr = work;
2503           work = GTK_CTREE_ROW (work)->sibling;
2504           gtk_ctree_remove_node (ctree, ptr);
2505         }
2506     }
2507
2508   sset_node_info (ctree, node, text, spacing, pixmap_closed, mask_closed,
2509                  pixmap_opened, mask_opened, is_leaf, expanded);
2510
2511   if (!is_leaf && !old_leaf)
2512     {
2513       GTK_CTREE_ROW (node)->expanded = old_expanded;
2514       if (expanded && !old_expanded)
2515         gtk_ctree_expand (ctree, node);
2516       else if (!expanded && old_expanded)
2517         gtk_ctree_collapse (ctree, node);
2518     }
2519
2520   GTK_CTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2521   
2522   stree_draw_node (ctree, node);
2523 }
2524
2525 static GtkCTreeRow *
2526 srow_new (GtkCTree *ctree)
2527 {
2528   GtkCList *clist;
2529   GtkCTreeRow *ctree_row;
2530   int i;
2531
2532   clist = GTK_CLIST (ctree);
2533 #if GTK_CHECK_VERSION(2,9,0)
2534   ctree_row = g_slice_new (GtkCTreeRow);
2535   ctree_row->row.cell = g_slice_alloc (sizeof (GtkCell) * clist->columns);
2536 #else
2537   ctree_row = g_chunk_new (GtkCTreeRow, (GMemChunk *)clist->row_mem_chunk);
2538   ctree_row->row.cell = g_chunk_new (GtkCell, (GMemChunk *)clist->cell_mem_chunk);
2539 #endif
2540   for (i = 0; i < clist->columns; i++)
2541     {
2542       ctree_row->row.cell[i].type = GTK_CELL_EMPTY;
2543       ctree_row->row.cell[i].vertical = 0;
2544       ctree_row->row.cell[i].horizontal = 0;
2545       ctree_row->row.cell[i].style = NULL;
2546     }
2547
2548   GTK_CELL_PIXTEXT (ctree_row->row.cell[ctree->tree_column])->text = NULL;
2549
2550   ctree_row->row.fg_set     = FALSE;
2551   ctree_row->row.bg_set     = FALSE;
2552   ctree_row->row.style      = NULL;
2553   ctree_row->row.selectable = TRUE;
2554   ctree_row->row.state      = GTK_STATE_NORMAL;
2555   ctree_row->row.data       = NULL;
2556   ctree_row->row.destroy    = NULL;
2557
2558   ctree_row->level         = 0;
2559   ctree_row->expanded      = FALSE;
2560   ctree_row->parent        = NULL;
2561   ctree_row->sibling       = NULL;
2562   ctree_row->children      = NULL;
2563   ctree_row->pixmap_closed = NULL;
2564   ctree_row->mask_closed   = NULL;
2565   ctree_row->pixmap_opened = NULL;
2566   ctree_row->mask_opened   = NULL;
2567   
2568   return ctree_row;
2569 }
2570
2571 static void
2572 srow_delete (GtkCTree    *ctree,
2573             GtkCTreeRow *ctree_row)
2574 {
2575   GtkCList *clist;
2576   gint i;
2577
2578   clist = GTK_CLIST (ctree);
2579
2580   for (i = 0; i < clist->columns; i++)
2581     {
2582       GTK_CLIST_GET_CLASS (clist)->set_cell_contents
2583         (clist, &(ctree_row->row), i, GTK_CELL_EMPTY, NULL, 0, NULL, NULL);
2584       if (ctree_row->row.cell[i].style)
2585         {
2586           if (GTK_WIDGET_REALIZED (ctree))
2587             gtk_style_detach (ctree_row->row.cell[i].style);
2588           g_object_unref (ctree_row->row.cell[i].style);
2589         }
2590     }
2591
2592   if (ctree_row->row.style)
2593     {
2594       if (GTK_WIDGET_REALIZED (ctree))
2595         gtk_style_detach (ctree_row->row.style);
2596       g_object_unref (ctree_row->row.style);
2597     }
2598
2599   if (ctree_row->pixmap_closed)
2600     {
2601       gdk_pixmap_unref (ctree_row->pixmap_closed);
2602       if (ctree_row->mask_closed)
2603         gdk_bitmap_unref (ctree_row->mask_closed);
2604     }
2605
2606   if (ctree_row->pixmap_opened)
2607     {
2608       gdk_pixmap_unref (ctree_row->pixmap_opened);
2609       if (ctree_row->mask_opened)
2610         gdk_bitmap_unref (ctree_row->mask_opened);
2611     }
2612
2613   if (ctree_row->row.destroy)
2614     {
2615       GtkDestroyNotify dnotify = ctree_row->row.destroy;
2616       gpointer ddata = ctree_row->row.data;
2617
2618       ctree_row->row.destroy = NULL;
2619       ctree_row->row.data = NULL;
2620
2621       dnotify (ddata);
2622     }
2623
2624 #if GTK_CHECK_VERSION(2,9,0)  
2625   g_slice_free1 (sizeof (GtkCell) * clist->columns, ctree_row->row.cell);
2626   g_slice_free (GtkCTreeRow, ctree_row);
2627 #else
2628   g_mem_chunk_free ((GMemChunk *)clist->cell_mem_chunk, ctree_row->row.cell);
2629   g_mem_chunk_free ((GMemChunk *)clist->row_mem_chunk, ctree_row);
2630 #endif
2631 }
2632
2633 static void
2634 stree_delete_row (GtkCTree     *ctree, 
2635                  GtkCTreeNode *node, 
2636                  gpointer      data)
2637 {
2638   srow_delete (ctree, GTK_CTREE_ROW (node));
2639   g_list_free_1 ((GList *)node);
2640 }
2641
2642 static void
2643 gtk_sctree_column_auto_resize (GtkCList    *clist,
2644                     GtkCListRow *clist_row,
2645                     gint         column,
2646                     gint         old_width)
2647 {
2648   /* resize column if needed for auto_resize */
2649   GtkRequisition requisition;
2650
2651   if (!clist->column[column].auto_resize ||
2652       GTK_CLIST_AUTO_RESIZE_BLOCKED (clist))
2653     return;
2654
2655   if (clist_row)
2656     GTK_CLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2657                                                    column, &requisition);
2658   else
2659     requisition.width = 0;
2660
2661   if (requisition.width > clist->column[column].width)
2662     gtk_clist_set_column_width (clist, column, requisition.width);
2663   else if (requisition.width < old_width &&
2664            old_width == clist->column[column].width)
2665     {
2666       GList *list;
2667       gint new_width;
2668
2669       /* run a "gtk_clist_optimal_column_width" but break, if
2670        * the column doesn't shrink */
2671       if (GTK_CLIST_SHOW_TITLES (clist) && clist->column[column].button)
2672         new_width = (clist->column[column].button->requisition.width -
2673                      (CELL_SPACING + (2 * COLUMN_INSET)));
2674       else
2675         new_width = 0;
2676
2677       for (list = clist->row_list; list; list = list->next)
2678         {
2679           GTK_CLIST_GET_CLASS (clist)->cell_size_request
2680             (clist, GTK_CLIST_ROW (list), column, &requisition);
2681           new_width = MAX (new_width, requisition.width);
2682           if (new_width == clist->column[column].width)
2683             break;
2684         }
2685       if (new_width < clist->column[column].width)
2686         gtk_clist_set_column_width (clist, column, new_width);
2687     }
2688 }
2689
2690
2691 static void 
2692 gtk_sctree_real_tree_expand (GtkCTree     *ctree,
2693                   GtkCTreeNode *node)
2694 {
2695   GtkCList *clist;
2696   GtkCTreeNode *work;
2697   GtkRequisition requisition;
2698   gboolean visible;
2699   gint level;
2700
2701   g_return_if_fail (GTK_IS_CTREE (ctree));
2702
2703   if (!node || GTK_CTREE_ROW (node)->expanded || GTK_CTREE_ROW (node)->is_leaf)
2704     return;
2705
2706   clist = GTK_CLIST (ctree);
2707   
2708   GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2709
2710   GTK_CTREE_ROW (node)->expanded = TRUE;
2711   level = GTK_CTREE_ROW (node)->level;
2712
2713   visible = gtk_ctree_is_viewable (ctree, node);
2714   /* get cell width if tree_column is auto resized */
2715   if (visible && clist->column[ctree->tree_column].auto_resize &&
2716       !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist))
2717     GTK_CLIST_GET_CLASS (clist)->cell_size_request
2718       (clist, &GTK_CTREE_ROW (node)->row, ctree->tree_column, &requisition);
2719
2720   /* unref/unset closed pixmap */
2721   if (GTK_CELL_PIXTEXT 
2722       (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap)
2723     {
2724       gdk_pixmap_unref
2725         (GTK_CELL_PIXTEXT
2726          (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap);
2727       
2728       GTK_CELL_PIXTEXT
2729         (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap = NULL;
2730       
2731       if (GTK_CELL_PIXTEXT 
2732           (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask)
2733         {
2734           gdk_pixmap_unref
2735             (GTK_CELL_PIXTEXT 
2736              (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask);
2737           GTK_CELL_PIXTEXT 
2738             (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask = NULL;
2739         }
2740     }
2741
2742   /* set/ref opened pixmap */
2743   if (GTK_CTREE_ROW (node)->pixmap_opened)
2744     {
2745       GTK_CELL_PIXTEXT 
2746         (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap = 
2747         gdk_pixmap_ref (GTK_CTREE_ROW (node)->pixmap_opened);
2748
2749       if (GTK_CTREE_ROW (node)->mask_opened) 
2750         GTK_CELL_PIXTEXT 
2751           (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask = 
2752           gdk_pixmap_ref (GTK_CTREE_ROW (node)->mask_opened);
2753     }
2754
2755
2756   work = GTK_CTREE_ROW (node)->children;
2757   if (work)
2758     {
2759       GList *list = (GList *)work;
2760       gint *cell_width = NULL;
2761       gint tmp = 0;
2762       gint row;
2763       gint i;
2764       
2765       if (visible && !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist))
2766         {
2767           cell_width = g_new0 (gint, clist->columns);
2768           if (clist->column[ctree->tree_column].auto_resize)
2769               cell_width[ctree->tree_column] = requisition.width;
2770
2771           while (work)
2772             {
2773               /* search maximum cell widths of auto_resize columns */
2774               for (i = 0; i < clist->columns; i++)
2775                 if (clist->column[i].auto_resize)
2776                   {
2777                     GTK_CLIST_GET_CLASS (clist)->cell_size_request
2778                       (clist, &GTK_CTREE_ROW (work)->row, i, &requisition);
2779                     cell_width[i] = MAX (requisition.width, cell_width[i]);
2780                   }
2781
2782               list = (GList *)work;
2783               work = GTK_CTREE_NODE_NEXT (work);
2784               tmp++;
2785             }
2786         }
2787       else
2788         while (work)
2789           {
2790             list = (GList *)work;
2791             work = GTK_CTREE_NODE_NEXT (work);
2792             tmp++;
2793           }
2794
2795       list->next = (GList *)GTK_CTREE_NODE_NEXT (node);
2796
2797       if (GTK_CTREE_NODE_NEXT (node))
2798         {
2799           GList *tmp_list;
2800
2801           tmp_list = (GList *)GTK_CTREE_NODE_NEXT (node);
2802           tmp_list->prev = list;
2803         }
2804       else
2805         clist->row_list_end = list;
2806
2807       list = (GList *)node;
2808       list->next = (GList *)(GTK_CTREE_ROW (node)->children);
2809
2810       if (visible)
2811         {
2812           /* resize auto_resize columns if needed */
2813           for (i = 0; i < clist->columns; i++)
2814             if (clist->column[i].auto_resize &&
2815                 cell_width[i] > clist->column[i].width)
2816               gtk_clist_set_column_width (clist, i, cell_width[i]);
2817           g_free (cell_width);
2818         
2819           if (!GTK_SCTREE(ctree)->sorting) {
2820                   /* update focus_row position */
2821                   row = g_list_position (clist->row_list, (GList *)node);
2822                   if (row < clist->focus_row)
2823                     clist->focus_row += tmp;
2824           }
2825           clist->rows += tmp;
2826           CLIST_REFRESH (clist);
2827         }
2828     }
2829   else if (visible && clist->column[ctree->tree_column].auto_resize)
2830     /* resize tree_column if needed */
2831     gtk_sctree_column_auto_resize (clist, &GTK_CTREE_ROW (node)->row, ctree->tree_column,
2832                         requisition.width);
2833
2834 }
2835
2836 GtkCTreeNode * 
2837 gtk_sctree_insert_node (GtkCTree     *ctree,
2838                        GtkCTreeNode *parent, 
2839                        GtkCTreeNode *sibling,
2840                        gchar        *text[],
2841                        guint8        spacing,
2842                        GdkPixmap    *pixmap_closed,
2843                        GdkBitmap    *mask_closed,
2844                        GdkPixmap    *pixmap_opened,
2845                        GdkBitmap    *mask_opened,
2846                        gboolean      is_leaf,
2847                        gboolean      expanded)
2848 {
2849   GtkCList *clist;
2850   GtkCTreeRow *new_row;
2851   GtkCTreeNode *node;
2852   GList *list;
2853   gint i;
2854
2855   if (!emptyxpm) {
2856           stock_pixmap_gdk(GTK_WIDGET(ctree), STOCK_PIXMAP_EMPTY,
2857                    &emptyxpm, &emptyxpmmask);
2858   }
2859   if (!pixmap_closed) {
2860           pixmap_closed = emptyxpm;
2861           mask_closed = emptyxpmmask;
2862   }
2863   if (!pixmap_opened) {
2864           pixmap_opened = emptyxpm;
2865           mask_opened = emptyxpmmask;
2866   }
2867   g_return_val_if_fail (GTK_IS_CTREE (ctree), NULL);
2868   if (sibling)
2869     g_return_val_if_fail (GTK_CTREE_ROW (sibling)->parent == parent, NULL);
2870
2871   if (parent && GTK_CTREE_ROW (parent)->is_leaf)
2872     return NULL;
2873
2874   clist = GTK_CLIST (ctree);
2875
2876   /* create the row */
2877   new_row = srow_new (ctree);
2878   list = g_list_alloc ();
2879   list->data = new_row;
2880   node = GTK_CTREE_NODE (list);
2881
2882   if (text)
2883     for (i = 0; i < clist->columns; i++)
2884       if (text[i] && i != ctree->tree_column)
2885         GTK_CLIST_GET_CLASS (clist)->set_cell_contents
2886           (clist, &(new_row->row), i, GTK_CELL_TEXT, text[i], 0, NULL, NULL);
2887
2888   sset_node_info (ctree, node, text ?
2889                  text[ctree->tree_column] : NULL, spacing, pixmap_closed,
2890                  mask_closed, pixmap_opened, mask_opened, is_leaf, expanded);
2891
2892   /* sorted insertion */
2893   if (GTK_CLIST_AUTO_SORT (clist))
2894     {
2895       if (parent)
2896         sibling = GTK_CTREE_ROW (parent)->children;
2897       else
2898         sibling = GTK_CTREE_NODE (clist->row_list);
2899
2900       while (sibling && clist->compare
2901              (clist, GTK_CTREE_ROW (node), GTK_CTREE_ROW (sibling)) > 0)
2902         sibling = GTK_CTREE_ROW (sibling)->sibling;
2903     }
2904
2905   gtk_sctree_link (ctree, node, parent, sibling, FALSE);
2906
2907   if (text && !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist) &&
2908       gtk_ctree_is_viewable (ctree, node))
2909     {
2910       for (i = 0; i < clist->columns; i++)
2911         if (clist->column[i].auto_resize)
2912           gtk_sctree_column_auto_resize (clist, &(new_row->row), i, 0);
2913     }
2914
2915   if (clist->rows == 1)
2916     {
2917       clist->focus_row = 0;
2918       if (clist->selection_mode == GTK_SELECTION_BROWSE)
2919         gtk_sctree_select (GTK_SCTREE(ctree), node);
2920     }
2921
2922
2923   CLIST_REFRESH (clist);
2924
2925   return node;
2926 }
2927
2928 GtkCTreeNode *
2929 gtk_sctree_insert_gnode (GtkCTree          *ctree,
2930                         GtkCTreeNode      *parent,
2931                         GtkCTreeNode      *sibling,
2932                         GNode             *gnode,
2933                         GtkCTreeGNodeFunc  func,
2934                         gpointer           data)
2935 {
2936   GtkCList *clist;
2937   GtkCTreeNode *cnode = NULL;
2938   GtkCTreeNode *child = NULL;
2939   GtkCTreeNode *new_child;
2940   GList *list;
2941   GNode *work;
2942   guint depth = 1;
2943
2944   g_return_val_if_fail (GTK_IS_CTREE (ctree), NULL);
2945   g_return_val_if_fail (gnode != NULL, NULL);
2946   g_return_val_if_fail (func != NULL, NULL);
2947   if (sibling)
2948     g_return_val_if_fail (GTK_CTREE_ROW (sibling)->parent == parent, NULL);
2949   
2950   clist = GTK_CLIST (ctree);
2951
2952   if (parent)
2953     depth = GTK_CTREE_ROW (parent)->level + 1;
2954
2955   list = g_list_alloc ();
2956   list->data = srow_new (ctree);
2957   cnode = GTK_CTREE_NODE (list);
2958
2959   gtk_clist_freeze (clist);
2960
2961   sset_node_info (ctree, cnode, "", 0, NULL, NULL, NULL, NULL, TRUE, FALSE);
2962
2963   if (!func (ctree, depth, gnode, cnode, data))
2964     {
2965       stree_delete_row (ctree, cnode, NULL);
2966       gtk_clist_thaw (clist);
2967       return NULL;
2968     }
2969
2970   if (GTK_CLIST_AUTO_SORT (clist))
2971     {
2972       if (parent)
2973         sibling = GTK_CTREE_ROW (parent)->children;
2974       else
2975         sibling = GTK_CTREE_NODE (clist->row_list);
2976
2977       while (sibling && clist->compare
2978              (clist, GTK_CTREE_ROW (cnode), GTK_CTREE_ROW (sibling)) > 0)
2979         sibling = GTK_CTREE_ROW (sibling)->sibling;
2980     }
2981
2982   gtk_sctree_link (ctree, cnode, parent, sibling, FALSE);
2983
2984   for (work = g_node_last_child (gnode); work; work = work->prev)
2985     {
2986       new_child = gtk_sctree_insert_gnode (ctree, cnode, child,
2987                                           work, func, data);
2988       if (new_child)
2989         child = new_child;
2990     }   
2991   
2992   gtk_clist_thaw (clist);
2993
2994   return cnode;
2995 }
2996
2997 static void
2998 sreal_tree_move (GtkCTree     *ctree,
2999                 GtkCTreeNode *node,
3000                 GtkCTreeNode *new_parent, 
3001                 GtkCTreeNode *new_sibling)
3002 {
3003   GtkCList *clist;
3004   GtkCTreeNode *work;
3005   gboolean visible = FALSE;
3006
3007   g_return_if_fail (ctree != NULL);
3008   g_return_if_fail (node != NULL);
3009   g_return_if_fail (!new_sibling || 
3010                     GTK_CTREE_ROW (new_sibling)->parent == new_parent);
3011
3012   if (new_parent && GTK_CTREE_ROW (new_parent)->is_leaf)
3013     return;
3014
3015   /* new_parent != child of child */
3016   for (work = new_parent; work; work = GTK_CTREE_ROW (work)->parent)
3017     if (work == node)
3018       return;
3019
3020   clist = GTK_CLIST (ctree);
3021
3022   visible = gtk_ctree_is_viewable (ctree, node);
3023
3024   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
3025     {
3026       GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3027       
3028       g_list_free (clist->undo_selection);
3029       g_list_free (clist->undo_unselection);
3030       clist->undo_selection = NULL;
3031       clist->undo_unselection = NULL;
3032     }
3033
3034   if (GTK_CLIST_AUTO_SORT (clist))
3035     {
3036       if (new_parent == GTK_CTREE_ROW (node)->parent)
3037         return;
3038       
3039       if (new_parent)
3040         new_sibling = GTK_CTREE_ROW (new_parent)->children;
3041       else
3042         new_sibling = GTK_CTREE_NODE (clist->row_list);
3043
3044       while (new_sibling && clist->compare
3045              (clist, GTK_CTREE_ROW (node), GTK_CTREE_ROW (new_sibling)) > 0)
3046         new_sibling = GTK_CTREE_ROW (new_sibling)->sibling;
3047     }
3048
3049   if (new_parent == GTK_CTREE_ROW (node)->parent && 
3050       new_sibling == GTK_CTREE_ROW (node)->sibling)
3051     return;
3052
3053   gtk_clist_freeze (clist);
3054
3055   work = NULL;
3056
3057   if (!GTK_SCTREE(ctree)->sorting && gtk_ctree_is_viewable (ctree, node))
3058     work = GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
3059       
3060   gtk_sctree_unlink (ctree, node, FALSE);
3061   gtk_sctree_link (ctree, node, new_parent, new_sibling, FALSE);
3062   
3063   if (!GTK_SCTREE(ctree)->sorting && work)
3064     {
3065       while (work &&  !gtk_ctree_is_viewable (ctree, work))
3066         work = GTK_CTREE_ROW (work)->parent;
3067       clist->focus_row = g_list_position (clist->row_list, (GList *)work);
3068       clist->undo_anchor = clist->focus_row;
3069     }
3070
3071   if (clist->column[ctree->tree_column].auto_resize &&
3072       !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist) &&
3073       (visible || gtk_ctree_is_viewable (ctree, node)))
3074     gtk_clist_set_column_width
3075       (clist, ctree->tree_column,
3076        gtk_clist_optimal_column_width (clist, ctree->tree_column));
3077
3078   gtk_clist_thaw (clist);
3079 }
3080
3081 void gtk_sctree_set_column_tooltip          (GtkSCTree          *sctree,
3082                                              int                 column,
3083                                              const gchar        *tip)
3084 {
3085         if (!sctree->tips)
3086                 sctree->tips = gtk_tooltips_new();
3087                 
3088         gtk_tooltips_set_tip(GTK_TOOLTIPS(sctree->tips), 
3089                 GTK_CLIST(sctree)->column[column].button,
3090                         tip, NULL);
3091
3092 }
3093