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