2278f567da2926e7f9b2cc76bc776165f243ace3
[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         GTK_CLIST(sctree)->focus_row = row;
1472
1473         if (!additive) {
1474                 gtk_clist_unselect_all (GTK_CLIST (sctree));
1475         }
1476
1477         if (!range) {
1478                 GtkCTreeNode *node;
1479
1480                 node = _node ? _node : gtk_ctree_node_nth (GTK_CTREE(sctree), row);
1481
1482                 /*No need to manage overlapped list*/
1483                 if (additive) {
1484                         if (row_is_selected(sctree, row))
1485                                 gtk_clist_unselect_row (GTK_CLIST (sctree), row, col);
1486                         else
1487                                 g_signal_emit_by_name
1488                                         (G_OBJECT (sctree),
1489                                          "tree_select_row", node, col);
1490                 } else {
1491                         g_signal_emit_by_name
1492                                 (G_OBJECT (sctree),
1493                                  "tree_select_row", node, col);
1494                 }
1495                 sctree->anchor_row = node;
1496         } else
1497                 select_range (sctree, row);
1498 }
1499
1500 /* Our handler for button_press events.  We override all of GtkCList's broken
1501  * behavior.
1502  */
1503 static gint
1504 gtk_sctree_button_press (GtkWidget *widget, GdkEventButton *event)
1505 {
1506         GtkSCTree *sctree;
1507         GtkCList *clist;
1508         gboolean on_row;
1509         gint row;
1510         gint col;
1511         gint retval;
1512
1513         g_return_val_if_fail (widget != NULL, FALSE);
1514         g_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1515         g_return_val_if_fail (event != NULL, FALSE);
1516
1517         sctree = GTK_SCTREE (widget);
1518         clist = GTK_CLIST (widget);
1519         retval = FALSE;
1520
1521         if (event->window != clist->clist_window)
1522                 return (* GTK_WIDGET_CLASS (parent_class)->button_press_event) (widget, event);
1523
1524         on_row = gtk_clist_get_selection_info (clist, event->x, event->y, &row, &col);
1525
1526         if (on_row && !GTK_WIDGET_HAS_FOCUS(widget))
1527                 gtk_widget_grab_focus (widget);
1528
1529         if (gtk_ctree_is_hot_spot (GTK_CTREE(sctree), event->x, event->y)) {
1530                 GtkCTreeNode *node = gtk_ctree_node_nth(GTK_CTREE(sctree), row);
1531                 if (GTK_CTREE_ROW (node)->expanded)
1532                         gtk_ctree_collapse(GTK_CTREE(sctree), node);
1533                 else if (GTK_SCTREE(sctree)->always_expand_recursively)
1534                         gtk_ctree_expand_recursive (GTK_CTREE(sctree), node);
1535                 else
1536                         gtk_ctree_expand(GTK_CTREE(sctree), node);
1537                 return TRUE;
1538         }
1539
1540         switch (event->type) {
1541         case GDK_BUTTON_PRESS:
1542                 if (event->button == 1 || event->button == 2) {
1543                         if (event->button == 2)
1544                                 event->state &= ~(GDK_SHIFT_MASK | GDK_CONTROL_MASK);
1545                         if (on_row) {
1546                                 /* Save the mouse info for DnD */
1547                                 sctree->dnd_press_button = event->button;
1548                                 sctree->dnd_press_x = event->x;
1549                                 sctree->dnd_press_y = event->y;
1550
1551                                 /* Handle selection */
1552                                 if ((row_is_selected (sctree, row)
1553                                      && !(event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)))
1554                                     || ((event->state & GDK_CONTROL_MASK)
1555                                         && !(event->state & GDK_SHIFT_MASK))) {
1556                                         sctree->dnd_select_pending = TRUE;
1557                                         sctree->dnd_select_pending_state = event->state;
1558                                         sctree->dnd_select_pending_row = row;
1559                                 } else {
1560                                         select_row (sctree, row, col, event->state, NULL);
1561                                 }
1562                         } else {
1563                                 gtk_clist_unselect_all (clist);
1564                         }
1565
1566                         retval = TRUE;
1567                 } else if (event->button == 3) {
1568                         /* Emit *_popup_menu signal*/
1569                         if (on_row) {
1570                                 if (!row_is_selected(sctree,row))
1571                                         select_row (sctree, row, col, 0, NULL);
1572                                 g_signal_emit (G_OBJECT (sctree),
1573                                                  sctree_signals[ROW_POPUP_MENU],
1574                                                  0, event);
1575                         } else {
1576                                 gtk_clist_unselect_all(clist);
1577                                 g_signal_emit (G_OBJECT (sctree),
1578                                                  sctree_signals[EMPTY_POPUP_MENU],
1579                                                  0, event);
1580                         }
1581                         retval = TRUE;
1582                 }
1583
1584                 break;
1585
1586         case GDK_2BUTTON_PRESS:
1587                 if (event->button != 1)
1588                         break;
1589
1590                 sctree->dnd_select_pending = FALSE;
1591                 sctree->dnd_select_pending_state = 0;
1592
1593                 if (on_row)
1594                         g_signal_emit (G_OBJECT (sctree),
1595                                        sctree_signals[OPEN_ROW], 0);
1596
1597                 retval = TRUE;
1598                 break;
1599
1600         default:
1601                 break;
1602         }
1603
1604         return retval;
1605 }
1606
1607 /* Our handler for button_release events.  We override all of GtkCList's broken
1608  * behavior.
1609  */
1610 static gint
1611 gtk_sctree_button_release (GtkWidget *widget, GdkEventButton *event)
1612 {
1613         GtkSCTree *sctree;
1614         GtkCList *clist;
1615         gint on_row;
1616         gint row, col;
1617         gint retval;
1618
1619         g_return_val_if_fail (widget != NULL, FALSE);
1620         g_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1621         g_return_val_if_fail (event != NULL, FALSE);
1622
1623         sctree = GTK_SCTREE (widget);
1624         clist = GTK_CLIST (widget);
1625         retval = FALSE;
1626
1627         if (event->window != clist->clist_window)
1628                 return (* GTK_WIDGET_CLASS (parent_class)->button_release_event) (widget, event);
1629
1630         on_row = gtk_clist_get_selection_info (clist, event->x, event->y, &row, &col);
1631
1632         if (!(event->button == 1 || event->button == 2))
1633                 return FALSE;
1634
1635         sctree->dnd_press_button = 0;
1636         sctree->dnd_press_x = 0;
1637         sctree->dnd_press_y = 0;
1638
1639         if (on_row) {
1640                 if (sctree->dnd_select_pending) {
1641                         select_row (sctree, row, col, sctree->dnd_select_pending_state, NULL);
1642                         sctree->dnd_select_pending = FALSE;
1643                         sctree->dnd_select_pending_state = 0;
1644                 }
1645
1646                 retval = TRUE;
1647         }
1648
1649         return retval;
1650 }
1651
1652 /* Our handler for motion_notify events.  We override all of GtkCList's broken
1653  * behavior.
1654  */
1655 static gint
1656 gtk_sctree_motion (GtkWidget *widget, GdkEventMotion *event)
1657 {
1658         GtkSCTree *sctree;
1659         GtkCList *clist;
1660
1661         g_return_val_if_fail (widget != NULL, FALSE);
1662         g_return_val_if_fail (GTK_IS_SCTREE (widget), FALSE);
1663         g_return_val_if_fail (event != NULL, FALSE);
1664
1665         sctree = GTK_SCTREE (widget);
1666         clist = GTK_CLIST (widget);
1667
1668         if (event->window != clist->clist_window)
1669                 return (* GTK_WIDGET_CLASS (parent_class)->motion_notify_event) (widget, event);
1670
1671         if (!((sctree->dnd_press_button == 1 && (event->state & GDK_BUTTON1_MASK))
1672               || (sctree->dnd_press_button == 2 && (event->state & GDK_BUTTON2_MASK))))
1673                 return FALSE;
1674
1675         /* This is the same threshold value that is used in gtkdnd.c */
1676
1677         if (MAX (ABS (sctree->dnd_press_x - event->x),
1678                  ABS (sctree->dnd_press_y - event->y)) <= 3)
1679                 return FALSE;
1680
1681         /* Handle any pending selections */
1682
1683         if (sctree->dnd_select_pending) {
1684                 if (!row_is_selected(sctree,sctree->dnd_select_pending_row))
1685                         select_row (sctree,
1686                                     sctree->dnd_select_pending_row,
1687                                     -1,
1688                                     sctree->dnd_select_pending_state,
1689                                     NULL);
1690
1691                 sctree->dnd_select_pending = FALSE;
1692                 sctree->dnd_select_pending_state = 0;
1693         }
1694
1695         g_signal_emit (G_OBJECT (sctree),
1696                        sctree_signals[START_DRAG],
1697                        0,
1698                        sctree->dnd_press_button,
1699                        event);
1700         return TRUE;
1701 }
1702
1703 /* We override the drag_begin signal to do nothing */
1704 static void
1705 gtk_sctree_drag_begin (GtkWidget *widget, GdkDragContext *context)
1706 {
1707         /* nothing */
1708 }
1709
1710 /* We override the drag_end signal to do nothing */
1711 static void
1712 gtk_sctree_drag_end (GtkWidget *widget, GdkDragContext *context)
1713 {
1714         /* nothing */
1715 }
1716
1717 /* We override the drag_data_get signal to do nothing */
1718 static void
1719 gtk_sctree_drag_data_get (GtkWidget *widget, GdkDragContext *context,
1720                                      GtkSelectionData *data, guint info, guint time)
1721 {
1722         /* nothing */
1723 }
1724
1725 /* We override the drag_leave signal to do nothing */
1726 static void
1727 gtk_sctree_drag_leave (GtkWidget *widget, GdkDragContext *context, guint time)
1728 {
1729         /* nothing */
1730 }
1731
1732 /* We override the drag_motion signal to do nothing */
1733 static gboolean
1734 gtk_sctree_drag_motion (GtkWidget *widget, GdkDragContext *context,
1735                                    gint x, gint y, guint time)
1736 {
1737         return FALSE;
1738 }
1739
1740 /* We override the drag_drop signal to do nothing */
1741 static gboolean
1742 gtk_sctree_drag_drop (GtkWidget *widget, GdkDragContext *context,
1743                                  gint x, gint y, guint time)
1744 {
1745         return FALSE;
1746 }
1747
1748 /* We override the drag_data_received signal to do nothing */
1749 static void
1750 gtk_sctree_drag_data_received (GtkWidget *widget, GdkDragContext *context,
1751                                           gint x, gint y, GtkSelectionData *data,
1752                                           guint info, guint time)
1753 {
1754         /* nothing */
1755 }
1756
1757 /* Our handler for the clear signal of the clist.  We have to reset the anchor
1758  * to null.
1759  */
1760 static void
1761 gtk_sctree_clear (GtkCList *clist)
1762 {
1763         GtkSCTree *sctree;
1764
1765         g_return_if_fail (clist != NULL);
1766         g_return_if_fail (GTK_IS_SCTREE (clist));
1767
1768         sctree = GTK_SCTREE (clist);
1769         sctree->anchor_row = NULL;
1770
1771         if (((GtkCListClass *)parent_class)->clear)
1772                 (* ((GtkCListClass *)parent_class)->clear) (clist);
1773 }
1774
1775 static void
1776 gtk_sctree_real_unselect_all (GtkCList *clist)
1777 {
1778         GtkSCTree *sctree;
1779         gboolean should_freeze = FALSE;
1780
1781         g_return_if_fail (clist != NULL);
1782         g_return_if_fail (GTK_IS_SCTREE (clist));
1783
1784         sctree = GTK_SCTREE (clist);
1785
1786         if (sc_g_list_bigger(GTK_CLIST(sctree)->selection, 10)) {
1787                 should_freeze = TRUE;
1788                 sctree->selecting_range++;
1789                 gtk_clist_freeze (GTK_CLIST (sctree));
1790         }
1791
1792         if (((GtkCListClass *)parent_class)->unselect_all)
1793                 (* ((GtkCListClass *)parent_class)->unselect_all) (clist);
1794
1795         if (should_freeze) {
1796                 gtk_clist_thaw (GTK_CLIST (sctree));
1797                 sctree->selecting_range--;
1798         }
1799 }
1800
1801 /* Our handler for the change_focus_row_expansion signal of the ctree.  
1802  We have to set the anchor to parent visible node.
1803  */
1804 static void 
1805 gtk_sctree_collapse (GtkCTree *ctree, GtkCTreeNode *node)
1806 {
1807         g_return_if_fail (ctree != NULL);
1808         g_return_if_fail (GTK_IS_SCTREE (ctree));
1809
1810         (* parent_class->tree_collapse) (ctree, node);
1811         GTK_SCTREE(ctree)->anchor_row =
1812                 gtk_ctree_node_nth(ctree, GTK_CLIST(ctree)->focus_row);
1813 }
1814
1815 GtkWidget *gtk_sctree_new_with_titles (gint columns, gint tree_column, 
1816                                        gchar *titles[])
1817 {
1818         GtkWidget *widget;
1819                                                                                                             
1820         g_return_val_if_fail (columns > 0, NULL);
1821         g_return_val_if_fail (tree_column >= 0, NULL);
1822                                                                                                             
1823         widget = gtk_widget_new (TYPE_GTK_SCTREE,
1824                                  "n_columns", columns,
1825                                  "tree_column", tree_column,
1826                                  NULL);
1827         if (titles) {
1828                 GtkCList *clist = GTK_CLIST (widget);
1829                 guint i;
1830
1831                 for (i = 0; i < columns; i++)
1832                         gtk_clist_set_column_title (clist, i, titles[i]);
1833                 gtk_clist_column_titles_show (clist);
1834         }
1835
1836         GTK_SCTREE(widget)->show_stripes = TRUE;
1837         GTK_SCTREE(widget)->always_expand_recursively = TRUE;
1838
1839         return widget;
1840 }
1841
1842 void gtk_sctree_select (GtkSCTree *sctree, GtkCTreeNode *node)
1843 {
1844         select_row(sctree, 
1845                    g_list_position(GTK_CLIST(sctree)->row_list, (GList *)node),
1846                    -1, 0, node);
1847 }
1848
1849 void gtk_sctree_select_with_state (GtkSCTree *sctree, GtkCTreeNode *node, int state)
1850 {
1851         select_row(sctree, 
1852                    g_list_position(GTK_CLIST(sctree)->row_list, (GList *)node),
1853                    -1, state, node);
1854 }
1855
1856 void gtk_sctree_unselect_all (GtkSCTree *sctree)
1857 {
1858         gtk_clist_unselect_all(GTK_CLIST(sctree));
1859         sctree->anchor_row = NULL;
1860 }
1861
1862 void gtk_sctree_set_anchor_row (GtkSCTree *sctree, GtkCTreeNode *node)
1863 {
1864         sctree->anchor_row = node;
1865 }
1866
1867 void gtk_sctree_remove_node (GtkSCTree *sctree, GtkCTreeNode *node)
1868 {
1869         if (sctree->anchor_row == node)
1870                 sctree->anchor_row = NULL;
1871         gtk_ctree_remove_node(GTK_CTREE(sctree), node);
1872 }
1873
1874 void gtk_sctree_set_stripes(GtkSCTree  *sctree, gboolean show_stripes)
1875 {
1876         sctree->show_stripes = show_stripes;
1877 }
1878
1879 void gtk_sctree_set_recursive_expand(GtkSCTree  *sctree, gboolean rec_exp)
1880 {
1881         sctree->always_expand_recursively = rec_exp;
1882 }
1883
1884 /***********************************************************
1885  *             Tree sorting functions                      *
1886  ***********************************************************/
1887
1888 static void sink(GtkCList *clist, GPtrArray *numbers, gint root, gint bottom)
1889 {
1890         gint j, k ;
1891         GtkCTreeNode *temp;
1892
1893         j = 2 * root;
1894         k = j + 1;
1895
1896         /* find the maximum element of numbers[root],
1897            numbers[2*root] and numbers[2*root+1] */
1898         if (j <= bottom) {
1899                 if (clist->compare( clist, GTK_CTREE_ROW (g_ptr_array_index(numbers, root)),
1900                                     GTK_CTREE_ROW(g_ptr_array_index( numbers, j))) >= 0)
1901                         j = root;
1902                 if (k <= bottom)
1903                         if (clist->compare( clist, GTK_CTREE_ROW (g_ptr_array_index(numbers, k)),
1904                                             GTK_CTREE_ROW (g_ptr_array_index( numbers, j))) > 0)
1905                                 j = k;
1906                 /* if numbers[root] wasn't the maximum element then
1907                    sink again */
1908                 if (root != j) {
1909                         temp = g_ptr_array_index( numbers,root);
1910                         g_ptr_array_index( numbers, root) = g_ptr_array_index( numbers, j);
1911                         g_ptr_array_index( numbers, j) = temp;
1912                         sink( clist, numbers, j, bottom);
1913                 }
1914         }
1915 }
1916
1917 static void heap_sort(GtkCList *clist, GPtrArray *numbers, gint array_size)
1918 {
1919         gint i;
1920         GtkCTreeNode *temp;
1921         
1922         /* build the Heap */
1923         for (i = (array_size / 2); i >= 1; i--)
1924                 sink( clist, numbers, i, array_size);
1925         /* output the Heap */
1926         for (i = array_size; i >= 2; i--) {
1927                 temp = g_ptr_array_index( numbers, 1);
1928                 g_ptr_array_index( numbers, 1) = g_ptr_array_index( numbers, i);
1929                 g_ptr_array_index( numbers, i) = temp;
1930                 sink( clist, numbers, 1, i-1);
1931         }
1932 }
1933
1934 static void
1935 stree_sort (GtkCTree    *ctree,
1936            GtkCTreeNode *node,
1937            gpointer      data)
1938 {
1939         GtkCTreeNode *list_start, *work, *next;
1940         GPtrArray *row_array, *viewable_array;
1941         GtkCList *clist;
1942         gint i;
1943
1944         clist = GTK_CLIST (ctree);
1945
1946         if (node)
1947                 work = GTK_CTREE_ROW (node)->children;
1948         else
1949                 work = GTK_CTREE_NODE (clist->row_list);
1950
1951         row_array = g_ptr_array_new();
1952         viewable_array = g_ptr_array_new();
1953
1954         if (work) {
1955                 g_ptr_array_add( row_array, NULL);
1956                 while (work) {
1957                         /* add all rows to row_array */
1958                         g_ptr_array_add( row_array, work);
1959                         if (GTK_CTREE_ROW (work)->parent && gtk_ctree_is_viewable( ctree, work))
1960                                 g_ptr_array_add( viewable_array, GTK_CTREE_ROW (work)->parent);
1961                         next = GTK_CTREE_ROW (work)->sibling;
1962                         gtk_sctree_unlink( ctree, work, FALSE);
1963                         work = next;
1964                 }
1965
1966                 heap_sort( clist, row_array, (row_array->len)-1);
1967
1968                 if (node)
1969                         list_start = GTK_CTREE_ROW (node)->children;
1970                 else
1971                         list_start = GTK_CTREE_NODE (clist->row_list);
1972
1973                 if (clist->sort_type == GTK_SORT_ASCENDING) {
1974                         for (i=(row_array->len)-1; i>=1; i--) {
1975                                 work = g_ptr_array_index( row_array, i);
1976                                 gtk_sctree_link( ctree, work, node, list_start, FALSE);
1977                                 list_start = work;
1978                                 /* insert work at the beginning of the list */
1979                         }
1980                 } else {
1981                         for (i=1; i<row_array->len; i++) {
1982                                 work = g_ptr_array_index( row_array, i);
1983                                 gtk_sctree_link( ctree, work, node, list_start, FALSE);
1984                                 list_start = work;
1985                                 /* insert work at the beginning of the list */
1986                         }
1987                 }
1988
1989                 for (i=0; i<viewable_array->len; i++) {
1990                         gtk_ctree_expand( ctree, g_ptr_array_index( viewable_array, i));
1991                 }
1992                 
1993         }
1994         g_ptr_array_free( row_array, TRUE);
1995         g_ptr_array_free( viewable_array, TRUE);
1996 }
1997
1998 void
1999 gtk_sctree_sort_recursive (GtkCTree     *ctree, 
2000                           GtkCTreeNode *node)
2001 {
2002         GtkCList *clist;
2003         GtkCTreeNode *focus_node = NULL;
2004
2005         g_return_if_fail (ctree != NULL);
2006         g_return_if_fail (GTK_IS_CTREE (ctree));
2007
2008         clist = GTK_CLIST (ctree);
2009
2010         gtk_clist_freeze (clist);
2011
2012         if (clist->selection_mode == GTK_SELECTION_EXTENDED) {
2013                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2014       
2015                 g_list_free (clist->undo_selection);
2016                 g_list_free (clist->undo_unselection);
2017                 clist->undo_selection = NULL;
2018                 clist->undo_unselection = NULL;
2019         }
2020
2021         if (!node || (node && gtk_ctree_is_viewable (ctree, node)))
2022                 focus_node = GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
2023       
2024         GTK_SCTREE(ctree)->sorting = TRUE;
2025
2026         gtk_ctree_post_recursive (ctree, node, GTK_CTREE_FUNC (stree_sort), NULL);
2027
2028         if (!node)
2029                 stree_sort (ctree, NULL, NULL);
2030
2031         GTK_SCTREE(ctree)->sorting = FALSE;
2032
2033         if (focus_node) {
2034                 clist->focus_row = g_list_position (clist->row_list,(GList *)focus_node);
2035                 clist->undo_anchor = clist->focus_row;
2036         }
2037
2038         gtk_clist_thaw (clist);
2039 }
2040
2041 void
2042 gtk_sctree_sort_node (GtkCTree     *ctree, 
2043                      GtkCTreeNode *node)
2044 {
2045         GtkCList *clist;
2046         GtkCTreeNode *focus_node = NULL;
2047
2048         g_return_if_fail (ctree != NULL);
2049         g_return_if_fail (GTK_IS_CTREE (ctree));
2050
2051         clist = GTK_CLIST (ctree);
2052
2053         gtk_clist_freeze (clist);
2054
2055         if (clist->selection_mode == GTK_SELECTION_EXTENDED) {
2056                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2057
2058                 g_list_free (clist->undo_selection);
2059                 g_list_free (clist->undo_unselection);
2060                 clist->undo_selection = NULL;
2061                 clist->undo_unselection = NULL;
2062         }
2063
2064         if (!node || (node && gtk_ctree_is_viewable (ctree, node)))
2065                 focus_node = GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
2066
2067         GTK_SCTREE(ctree)->sorting = TRUE;
2068
2069         stree_sort (ctree, node, NULL);
2070
2071         GTK_SCTREE(ctree)->sorting = FALSE;
2072
2073         if (focus_node) {
2074                 clist->focus_row = g_list_position (clist->row_list,(GList *)focus_node);
2075                 clist->undo_anchor = clist->focus_row;
2076         }
2077
2078         gtk_clist_thaw (clist);
2079 }
2080
2081 /************************************************************************/
2082
2083 static void
2084 gtk_sctree_unlink (GtkCTree     *ctree, 
2085                   GtkCTreeNode *node,
2086                   gboolean      update_focus_row)
2087 {
2088         GtkCList *clist;
2089         gint rows;
2090         gint level;
2091         gint visible;
2092         GtkCTreeNode *work;
2093         GtkCTreeNode *parent;
2094         GList *list;
2095
2096         g_return_if_fail (ctree != NULL);
2097         g_return_if_fail (GTK_IS_CTREE (ctree));
2098         g_return_if_fail (node != NULL);
2099
2100         clist = GTK_CLIST (ctree);
2101   
2102         if (update_focus_row && clist->selection_mode == GTK_SELECTION_EXTENDED) {
2103                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2104
2105                 g_list_free (clist->undo_selection);
2106                 g_list_free (clist->undo_unselection);
2107                 clist->undo_selection = NULL;
2108                 clist->undo_unselection = NULL;
2109         }
2110
2111         visible = gtk_ctree_is_viewable (ctree, node);
2112
2113         /* clist->row_list_end unlinked ? */
2114         if (visible && (GTK_CTREE_NODE_NEXT (node) == NULL ||
2115            (GTK_CTREE_ROW (node)->children && gtk_ctree_is_ancestor (ctree, node,
2116             GTK_CTREE_NODE (clist->row_list_end)))))
2117                 clist->row_list_end = (GList *) (GTK_CTREE_NODE_PREV (node));
2118
2119         /* update list */
2120         rows = 0;
2121         level = GTK_CTREE_ROW (node)->level;
2122         work = GTK_CTREE_NODE_NEXT (node);
2123         while (work && GTK_CTREE_ROW (work)->level > level) {
2124                 work = GTK_CTREE_NODE_NEXT (work);
2125                 rows++;
2126         }
2127
2128         if (visible) {
2129                 clist->rows -= (rows + 1);
2130
2131                 if (update_focus_row) {
2132                         gint pos;
2133                         pos = g_list_position (clist->row_list, (GList *)node);
2134                         if (pos + rows < clist->focus_row)
2135                                 clist->focus_row -= (rows + 1);
2136                         else if (pos <= clist->focus_row) {
2137                                 if (!GTK_CTREE_ROW (node)->sibling)
2138                                         clist->focus_row = MAX (pos - 1, 0);
2139                                 else
2140                                         clist->focus_row = pos;
2141               
2142                                 clist->focus_row = MIN (clist->focus_row, clist->rows - 1);
2143                         }
2144                         clist->undo_anchor = clist->focus_row;
2145                 }
2146         }
2147
2148         if (work) {
2149                 list = (GList *)GTK_CTREE_NODE_PREV (work);
2150                 list->next = NULL;
2151                 list = (GList *)work;
2152                 list->prev = (GList *)GTK_CTREE_NODE_PREV (node);
2153         }
2154
2155         if (GTK_CTREE_NODE_PREV (node) &&
2156             GTK_CTREE_NODE_NEXT (GTK_CTREE_NODE_PREV (node)) == node) {
2157                 list = (GList *)GTK_CTREE_NODE_PREV (node);
2158                 list->next = (GList *)work;
2159         }
2160
2161         /* update tree */
2162         parent = GTK_CTREE_ROW (node)->parent;
2163         if (parent) {
2164                 if (GTK_CTREE_ROW (parent)->children == node) {
2165                         GTK_CTREE_ROW (parent)->children = GTK_CTREE_ROW (node)->sibling;
2166                 }
2167                 else {
2168                         GtkCTreeNode *sibling;
2169
2170                         sibling = GTK_CTREE_ROW (parent)->children;
2171                         while (GTK_CTREE_ROW (sibling)->sibling != node)
2172                                 sibling = GTK_CTREE_ROW (sibling)->sibling;
2173                         GTK_CTREE_ROW (sibling)->sibling = GTK_CTREE_ROW (node)->sibling;
2174                 }
2175         }
2176         else {
2177                 if (clist->row_list == (GList *)node)
2178                         clist->row_list = (GList *) (GTK_CTREE_ROW (node)->sibling);
2179                 else {
2180                         GtkCTreeNode *sibling;
2181
2182                         sibling = GTK_CTREE_NODE (clist->row_list);
2183                         while (GTK_CTREE_ROW (sibling)->sibling != node)
2184                                 sibling = GTK_CTREE_ROW (sibling)->sibling;
2185                         GTK_CTREE_ROW (sibling)->sibling = GTK_CTREE_ROW (node)->sibling;
2186                 }
2187         }
2188 }
2189
2190 static void
2191 gtk_sctree_link (GtkCTree     *ctree,
2192                 GtkCTreeNode *node,
2193                 GtkCTreeNode *parent,
2194                 GtkCTreeNode *sibling,
2195                 gboolean      update_focus_row)
2196 {
2197         GtkCList *clist;
2198         GList *list_end;
2199         GList *list;
2200         GList *work;
2201         gboolean visible = FALSE;
2202         gint rows = 0;
2203   
2204         if (sibling)
2205                 g_return_if_fail (GTK_CTREE_ROW (sibling)->parent == parent);
2206         g_return_if_fail (node != NULL);
2207         g_return_if_fail (node != sibling);
2208         g_return_if_fail (node != parent);
2209
2210         clist = GTK_CLIST (ctree);
2211
2212         if (update_focus_row && clist->selection_mode == GTK_SELECTION_EXTENDED) {
2213                 GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2214
2215                 g_list_free (clist->undo_selection);
2216                 g_list_free (clist->undo_unselection);
2217                 clist->undo_selection = NULL;
2218                 clist->undo_unselection = NULL;
2219         }
2220
2221         for (rows = 1, list_end = (GList *)node; list_end->next;
2222              list_end = list_end->next)
2223                 rows++;
2224
2225         GTK_CTREE_ROW (node)->parent = parent;
2226         GTK_CTREE_ROW (node)->sibling = sibling;
2227
2228         if (!parent || (parent && (gtk_ctree_is_viewable (ctree, parent) &&
2229             GTK_CTREE_ROW (parent)->expanded))) {
2230                 visible = TRUE;
2231                 clist->rows += rows;
2232         }
2233
2234         if (parent)
2235                 work = (GList *)(GTK_CTREE_ROW (parent)->children);
2236         else
2237                 work = clist->row_list;
2238
2239         if (sibling) {
2240                 if (work != (GList *)sibling) {
2241                         while (GTK_CTREE_ROW (work)->sibling != sibling)
2242                                 work = (GList *)(GTK_CTREE_ROW (work)->sibling);
2243                         GTK_CTREE_ROW (work)->sibling = node;
2244                 }
2245
2246                 if (sibling == GTK_CTREE_NODE (clist->row_list))
2247                 clist->row_list = (GList *) node;
2248                 if (GTK_CTREE_NODE_PREV (sibling) &&
2249                     GTK_CTREE_NODE_NEXT (GTK_CTREE_NODE_PREV (sibling)) == sibling) {
2250                         list = (GList *)GTK_CTREE_NODE_PREV (sibling);
2251                         list->next = (GList *)node;
2252                 }
2253
2254                 list = (GList *)node;
2255                 list->prev = (GList *)GTK_CTREE_NODE_PREV (sibling);
2256                 list_end->next = (GList *)sibling;
2257                 list = (GList *)sibling;
2258                 list->prev = list_end;
2259                 if (parent && GTK_CTREE_ROW (parent)->children == sibling)
2260                         GTK_CTREE_ROW (parent)->children = node;
2261         }
2262         else {
2263                 if (work) {
2264                         /* find sibling */
2265                         while (GTK_CTREE_ROW (work)->sibling)
2266                         work = (GList *)(GTK_CTREE_ROW (work)->sibling);
2267                         GTK_CTREE_ROW (work)->sibling = node;
2268
2269                         /* find last visible child of sibling */
2270                         work = (GList *) gtk_sctree_last_visible (ctree,
2271                                GTK_CTREE_NODE (work));
2272
2273                         list_end->next = work->next;
2274                         if (work->next)
2275                                 list = work->next->prev = list_end;
2276                         work->next = (GList *)node;
2277                         list = (GList *)node;
2278                         list->prev = work;
2279                 }
2280                 else {
2281                         if (parent) {
2282                                 GTK_CTREE_ROW (parent)->children = node;
2283                                 list = (GList *)node;
2284                                 list->prev = (GList *)parent;
2285                                 if (GTK_CTREE_ROW (parent)->expanded) {
2286                                         list_end->next = (GList *)GTK_CTREE_NODE_NEXT (parent);
2287                                         if (GTK_CTREE_NODE_NEXT(parent)) {
2288                                                 list = (GList *)GTK_CTREE_NODE_NEXT (parent);
2289                                                 list->prev = list_end;
2290                                         }
2291                                         list = (GList *)parent;
2292                                         list->next = (GList *)node;
2293                                 }
2294                                 else
2295                                         list_end->next = NULL;
2296                         }
2297                         else {
2298                                 clist->row_list = (GList *)node;
2299                                 list = (GList *)node;
2300                                 list->prev = NULL;
2301                                 list_end->next = NULL;
2302                         }
2303                 }
2304         }
2305
2306         gtk_ctree_pre_recursive (ctree, node, stree_update_level, NULL); 
2307
2308         if (clist->row_list_end == NULL ||
2309             clist->row_list_end->next == (GList *)node)
2310                 clist->row_list_end = list_end;
2311
2312         if (visible && update_focus_row) {
2313                 gint pos;
2314                 pos = g_list_position (clist->row_list, (GList *)node);
2315   
2316                 if (pos <= clist->focus_row) {
2317                         clist->focus_row += rows;
2318                         clist->undo_anchor = clist->focus_row;
2319                 }
2320         }
2321 }
2322
2323 static void
2324 stree_update_level (GtkCTree     *ctree, 
2325                    GtkCTreeNode *node, 
2326                    gpointer      data)
2327 {
2328         if (!node)
2329                 return;
2330
2331         if (GTK_CTREE_ROW (node)->parent)
2332                 GTK_CTREE_ROW (node)->level = 
2333                 GTK_CTREE_ROW (GTK_CTREE_ROW (node)->parent)->level + 1;
2334         else
2335                 GTK_CTREE_ROW (node)->level = 1;
2336 }
2337
2338 static GtkCTreeNode *
2339 gtk_sctree_last_visible (GtkCTree     *ctree,
2340                         GtkCTreeNode *node)
2341 {
2342         GtkCTreeNode *work;
2343   
2344         if (!node)
2345                 return NULL;
2346
2347         work = GTK_CTREE_ROW (node)->children;
2348
2349         if (!work || !GTK_CTREE_ROW (node)->expanded)
2350                 return node;
2351
2352         while (GTK_CTREE_ROW (work)->sibling)
2353                 work = GTK_CTREE_ROW (work)->sibling;
2354
2355         return gtk_sctree_last_visible (ctree, work);
2356 }
2357
2358 /* this wrapper simply replaces NULL pixmaps 
2359  * with a transparent, 1x1 pixmap. This works
2360  * around a memory problem deep inside gtk, 
2361  * revealed by valgrind. 
2362  */
2363 /*GtkCTreeNode* gtk_sctree_insert_node        (GtkCTree *ctree,
2364                                              GtkCTreeNode *parent,
2365                                              GtkCTreeNode *sibling,
2366                                              gchar *text[],
2367                                              guint8 spacing,
2368                                              GdkPixmap *pixmap_closed,
2369                                              GdkBitmap *mask_closed,
2370                                              GdkPixmap *pixmap_opened,
2371                                              GdkBitmap *mask_opened,
2372                                              gboolean is_leaf,
2373                                              gboolean expanded)
2374 {
2375         if (!emptyxpm) {
2376                 stock_pixmap_gdk(GTK_WIDGET(ctree), STOCK_PIXMAP_EMPTY,
2377                          &emptyxpm, &emptyxpmmask);
2378         }
2379         if (!pixmap_closed) {
2380                 pixmap_closed = emptyxpm;
2381                 mask_closed = emptyxpmmask;
2382         }
2383         if (!pixmap_opened) {
2384                 pixmap_opened = emptyxpm;
2385                 mask_opened = emptyxpmmask;
2386         }
2387         return gtk_ctree_insert_node(ctree, parent, sibling, text,spacing,
2388                 pixmap_closed, mask_closed, pixmap_opened, mask_opened,
2389                 is_leaf, expanded);
2390 }*/
2391
2392 static void 
2393 sset_node_info (GtkCTree     *ctree,
2394                GtkCTreeNode *node,
2395                const gchar  *text,
2396                guint8        spacing,
2397                GdkPixmap    *pixmap_closed,
2398                GdkBitmap    *mask_closed,
2399                GdkPixmap    *pixmap_opened,
2400                GdkBitmap    *mask_opened,
2401                gboolean      is_leaf,
2402                gboolean      expanded)
2403 {
2404   if (GTK_CTREE_ROW (node)->pixmap_opened)
2405     {
2406       gdk_pixmap_unref (GTK_CTREE_ROW (node)->pixmap_opened);
2407       if (GTK_CTREE_ROW (node)->mask_opened) 
2408         gdk_bitmap_unref (GTK_CTREE_ROW (node)->mask_opened);
2409     }
2410   if (GTK_CTREE_ROW (node)->pixmap_closed)
2411     {
2412       gdk_pixmap_unref (GTK_CTREE_ROW (node)->pixmap_closed);
2413       if (GTK_CTREE_ROW (node)->mask_closed) 
2414         gdk_bitmap_unref (GTK_CTREE_ROW (node)->mask_closed);
2415     }
2416
2417   GTK_CTREE_ROW (node)->pixmap_opened = NULL;
2418   GTK_CTREE_ROW (node)->mask_opened   = NULL;
2419   GTK_CTREE_ROW (node)->pixmap_closed = NULL;
2420   GTK_CTREE_ROW (node)->mask_closed   = NULL;
2421
2422   if (pixmap_closed)
2423     {
2424       GTK_CTREE_ROW (node)->pixmap_closed = gdk_pixmap_ref (pixmap_closed);
2425       if (mask_closed) 
2426         GTK_CTREE_ROW (node)->mask_closed = gdk_bitmap_ref (mask_closed);
2427     }
2428   if (pixmap_opened)
2429     {
2430       GTK_CTREE_ROW (node)->pixmap_opened = gdk_pixmap_ref (pixmap_opened);
2431       if (mask_opened) 
2432         GTK_CTREE_ROW (node)->mask_opened = gdk_bitmap_ref (mask_opened);
2433     }
2434
2435   GTK_CTREE_ROW (node)->is_leaf  = is_leaf;
2436   GTK_CTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2437
2438   if (GTK_CTREE_ROW (node)->expanded)
2439     gtk_ctree_node_set_pixtext (ctree, node, ctree->tree_column,
2440                                 text, spacing, pixmap_opened, mask_opened);
2441   else 
2442     gtk_ctree_node_set_pixtext (ctree, node, ctree->tree_column,
2443                                 text, spacing, pixmap_closed, mask_closed);
2444 }
2445
2446 static void
2447 stree_draw_node (GtkCTree     *ctree, 
2448                 GtkCTreeNode *node)
2449 {
2450   GtkCList *clist;
2451   
2452   clist = GTK_CLIST (ctree);
2453
2454   if (CLIST_UNFROZEN (clist) && gtk_ctree_is_viewable (ctree, node))
2455     {
2456       GtkCTreeNode *work;
2457       gint num = 0;
2458       
2459       work = GTK_CTREE_NODE (clist->row_list);
2460       while (work && work != node)
2461         {
2462           work = GTK_CTREE_NODE_NEXT (work);
2463           num++;
2464         }
2465       if (work && gtk_clist_row_is_visible (clist, num) != GTK_VISIBILITY_NONE)
2466         GTK_CLIST_GET_CLASS (clist)->draw_row
2467           (clist, NULL, num, GTK_CLIST_ROW ((GList *) node));
2468     }
2469 }
2470
2471 /* this wrapper simply replaces NULL pixmaps 
2472  * with a transparent, 1x1 pixmap. This works
2473  * around a memory problem deep inside gtk, 
2474  * revealed by valgrind. 
2475  */
2476 void        gtk_sctree_set_node_info        (GtkCTree *ctree,
2477                                              GtkCTreeNode *node,
2478                                              const gchar *text,
2479                                              guint8 spacing,
2480                                              GdkPixmap *pixmap_closed,
2481                                              GdkBitmap *mask_closed,
2482                                              GdkPixmap *pixmap_opened,
2483                                              GdkBitmap *mask_opened,
2484                                              gboolean is_leaf,
2485                                              gboolean expanded)
2486 {
2487   gboolean old_leaf;
2488   gboolean old_expanded;
2489   GtkCTreeNode *work;
2490  
2491   if (!emptyxpm) {
2492           stock_pixmap_gdk(GTK_WIDGET(ctree), STOCK_PIXMAP_EMPTY,
2493                    &emptyxpm, &emptyxpmmask);
2494   }
2495   if (!pixmap_closed) {
2496           pixmap_closed = emptyxpm;
2497           mask_closed = emptyxpmmask;
2498   }
2499   if (!pixmap_opened) {
2500           pixmap_opened = emptyxpm;
2501           mask_opened = emptyxpmmask;
2502   }
2503
2504   if (!GTK_IS_CTREE (ctree) || !node) return;
2505
2506   old_leaf = GTK_CTREE_ROW (node)->is_leaf;
2507   old_expanded = GTK_CTREE_ROW (node)->expanded;
2508
2509   if (is_leaf && (work = GTK_CTREE_ROW (node)->children) != NULL)
2510     {
2511       GtkCTreeNode *ptr;
2512       
2513       while (work)
2514         {
2515           ptr = work;
2516           work = GTK_CTREE_ROW (work)->sibling;
2517           gtk_ctree_remove_node (ctree, ptr);
2518         }
2519     }
2520
2521   sset_node_info (ctree, node, text, spacing, pixmap_closed, mask_closed,
2522                  pixmap_opened, mask_opened, is_leaf, expanded);
2523
2524   if (!is_leaf && !old_leaf)
2525     {
2526       GTK_CTREE_ROW (node)->expanded = old_expanded;
2527       if (expanded && !old_expanded)
2528         gtk_ctree_expand (ctree, node);
2529       else if (!expanded && old_expanded)
2530         gtk_ctree_collapse (ctree, node);
2531     }
2532
2533   GTK_CTREE_ROW (node)->expanded = (is_leaf) ? FALSE : expanded;
2534   
2535   stree_draw_node (ctree, node);
2536 }
2537
2538 static GtkCTreeRow *
2539 srow_new (GtkCTree *ctree)
2540 {
2541   GtkCList *clist;
2542   GtkCTreeRow *ctree_row;
2543   int i;
2544
2545   clist = GTK_CLIST (ctree);
2546 #if GTK_CHECK_VERSION(2,9,0)
2547   ctree_row = g_slice_new (GtkCTreeRow);
2548   ctree_row->row.cell = g_slice_alloc (sizeof (GtkCell) * clist->columns);
2549 #else
2550   ctree_row = g_chunk_new (GtkCTreeRow, (GMemChunk *)clist->row_mem_chunk);
2551   ctree_row->row.cell = g_chunk_new (GtkCell, (GMemChunk *)clist->cell_mem_chunk);
2552 #endif
2553   for (i = 0; i < clist->columns; i++)
2554     {
2555       ctree_row->row.cell[i].type = GTK_CELL_EMPTY;
2556       ctree_row->row.cell[i].vertical = 0;
2557       ctree_row->row.cell[i].horizontal = 0;
2558       ctree_row->row.cell[i].style = NULL;
2559     }
2560
2561   GTK_CELL_PIXTEXT (ctree_row->row.cell[ctree->tree_column])->text = NULL;
2562
2563   ctree_row->row.fg_set     = FALSE;
2564   ctree_row->row.bg_set     = FALSE;
2565   ctree_row->row.style      = NULL;
2566   ctree_row->row.selectable = TRUE;
2567   ctree_row->row.state      = GTK_STATE_NORMAL;
2568   ctree_row->row.data       = NULL;
2569   ctree_row->row.destroy    = NULL;
2570
2571   ctree_row->level         = 0;
2572   ctree_row->expanded      = FALSE;
2573   ctree_row->parent        = NULL;
2574   ctree_row->sibling       = NULL;
2575   ctree_row->children      = NULL;
2576   ctree_row->pixmap_closed = NULL;
2577   ctree_row->mask_closed   = NULL;
2578   ctree_row->pixmap_opened = NULL;
2579   ctree_row->mask_opened   = NULL;
2580   
2581   return ctree_row;
2582 }
2583
2584 static void
2585 srow_delete (GtkCTree    *ctree,
2586             GtkCTreeRow *ctree_row)
2587 {
2588   GtkCList *clist;
2589   gint i;
2590
2591   clist = GTK_CLIST (ctree);
2592
2593   for (i = 0; i < clist->columns; i++)
2594     {
2595       GTK_CLIST_GET_CLASS (clist)->set_cell_contents
2596         (clist, &(ctree_row->row), i, GTK_CELL_EMPTY, NULL, 0, NULL, NULL);
2597       if (ctree_row->row.cell[i].style)
2598         {
2599           if (GTK_WIDGET_REALIZED (ctree))
2600             gtk_style_detach (ctree_row->row.cell[i].style);
2601           g_object_unref (ctree_row->row.cell[i].style);
2602         }
2603     }
2604
2605   if (ctree_row->row.style)
2606     {
2607       if (GTK_WIDGET_REALIZED (ctree))
2608         gtk_style_detach (ctree_row->row.style);
2609       g_object_unref (ctree_row->row.style);
2610     }
2611
2612   if (ctree_row->pixmap_closed)
2613     {
2614       gdk_pixmap_unref (ctree_row->pixmap_closed);
2615       if (ctree_row->mask_closed)
2616         gdk_bitmap_unref (ctree_row->mask_closed);
2617     }
2618
2619   if (ctree_row->pixmap_opened)
2620     {
2621       gdk_pixmap_unref (ctree_row->pixmap_opened);
2622       if (ctree_row->mask_opened)
2623         gdk_bitmap_unref (ctree_row->mask_opened);
2624     }
2625
2626   if (ctree_row->row.destroy)
2627     {
2628       GtkDestroyNotify dnotify = ctree_row->row.destroy;
2629       gpointer ddata = ctree_row->row.data;
2630
2631       ctree_row->row.destroy = NULL;
2632       ctree_row->row.data = NULL;
2633
2634       dnotify (ddata);
2635     }
2636
2637 #if GTK_CHECK_VERSION(2,9,0)  
2638   g_slice_free1 (sizeof (GtkCell) * clist->columns, ctree_row->row.cell);
2639   g_slice_free (GtkCTreeRow, ctree_row);
2640 #else
2641   g_mem_chunk_free ((GMemChunk *)clist->cell_mem_chunk, ctree_row->row.cell);
2642   g_mem_chunk_free ((GMemChunk *)clist->row_mem_chunk, ctree_row);
2643 #endif
2644 }
2645
2646 static void
2647 stree_delete_row (GtkCTree     *ctree, 
2648                  GtkCTreeNode *node, 
2649                  gpointer      data)
2650 {
2651   srow_delete (ctree, GTK_CTREE_ROW (node));
2652   g_list_free_1 ((GList *)node);
2653 }
2654
2655 static void
2656 gtk_sctree_column_auto_resize (GtkCList    *clist,
2657                     GtkCListRow *clist_row,
2658                     gint         column,
2659                     gint         old_width)
2660 {
2661   /* resize column if needed for auto_resize */
2662   GtkRequisition requisition;
2663
2664   if (!clist->column[column].auto_resize ||
2665       GTK_CLIST_AUTO_RESIZE_BLOCKED (clist))
2666     return;
2667
2668   if (clist_row)
2669     GTK_CLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2670                                                    column, &requisition);
2671   else
2672     requisition.width = 0;
2673
2674   if (requisition.width > clist->column[column].width)
2675     gtk_clist_set_column_width (clist, column, requisition.width);
2676   else if (requisition.width < old_width &&
2677            old_width == clist->column[column].width)
2678     {
2679       GList *list;
2680       gint new_width;
2681
2682       /* run a "gtk_clist_optimal_column_width" but break, if
2683        * the column doesn't shrink */
2684       if (GTK_CLIST_SHOW_TITLES (clist) && clist->column[column].button)
2685         new_width = (clist->column[column].button->requisition.width -
2686                      (CELL_SPACING + (2 * COLUMN_INSET)));
2687       else
2688         new_width = 0;
2689
2690       for (list = clist->row_list; list; list = list->next)
2691         {
2692           GTK_CLIST_GET_CLASS (clist)->cell_size_request
2693             (clist, GTK_CLIST_ROW (list), column, &requisition);
2694           new_width = MAX (new_width, requisition.width);
2695           if (new_width == clist->column[column].width)
2696             break;
2697         }
2698       if (new_width < clist->column[column].width)
2699         gtk_clist_set_column_width (clist, column, new_width);
2700     }
2701 }
2702
2703
2704 static void 
2705 gtk_sctree_real_tree_expand (GtkCTree     *ctree,
2706                   GtkCTreeNode *node)
2707 {
2708   GtkCList *clist;
2709   GtkCTreeNode *work;
2710   GtkRequisition requisition;
2711   gboolean visible;
2712   gint level;
2713
2714   g_return_if_fail (GTK_IS_CTREE (ctree));
2715
2716   if (!node || GTK_CTREE_ROW (node)->expanded || GTK_CTREE_ROW (node)->is_leaf)
2717     return;
2718
2719   clist = GTK_CLIST (ctree);
2720   
2721   GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
2722
2723   GTK_CTREE_ROW (node)->expanded = TRUE;
2724   level = GTK_CTREE_ROW (node)->level;
2725
2726   visible = gtk_ctree_is_viewable (ctree, node);
2727   /* get cell width if tree_column is auto resized */
2728   if (visible && clist->column[ctree->tree_column].auto_resize &&
2729       !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist))
2730     GTK_CLIST_GET_CLASS (clist)->cell_size_request
2731       (clist, &GTK_CTREE_ROW (node)->row, ctree->tree_column, &requisition);
2732
2733   /* unref/unset closed pixmap */
2734   if (GTK_CELL_PIXTEXT 
2735       (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap)
2736     {
2737       gdk_pixmap_unref
2738         (GTK_CELL_PIXTEXT
2739          (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap);
2740       
2741       GTK_CELL_PIXTEXT
2742         (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap = NULL;
2743       
2744       if (GTK_CELL_PIXTEXT 
2745           (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask)
2746         {
2747           gdk_pixmap_unref
2748             (GTK_CELL_PIXTEXT 
2749              (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask);
2750           GTK_CELL_PIXTEXT 
2751             (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask = NULL;
2752         }
2753     }
2754
2755   /* set/ref opened pixmap */
2756   if (GTK_CTREE_ROW (node)->pixmap_opened)
2757     {
2758       GTK_CELL_PIXTEXT 
2759         (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->pixmap = 
2760         gdk_pixmap_ref (GTK_CTREE_ROW (node)->pixmap_opened);
2761
2762       if (GTK_CTREE_ROW (node)->mask_opened) 
2763         GTK_CELL_PIXTEXT 
2764           (GTK_CTREE_ROW (node)->row.cell[ctree->tree_column])->mask = 
2765           gdk_pixmap_ref (GTK_CTREE_ROW (node)->mask_opened);
2766     }
2767
2768
2769   work = GTK_CTREE_ROW (node)->children;
2770   if (work)
2771     {
2772       GList *list = (GList *)work;
2773       gint *cell_width = NULL;
2774       gint tmp = 0;
2775       gint row;
2776       gint i;
2777       
2778       if (visible && !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist))
2779         {
2780           cell_width = g_new0 (gint, clist->columns);
2781           if (clist->column[ctree->tree_column].auto_resize)
2782               cell_width[ctree->tree_column] = requisition.width;
2783
2784           while (work)
2785             {
2786               /* search maximum cell widths of auto_resize columns */
2787               for (i = 0; i < clist->columns; i++)
2788                 if (clist->column[i].auto_resize)
2789                   {
2790                     GTK_CLIST_GET_CLASS (clist)->cell_size_request
2791                       (clist, &GTK_CTREE_ROW (work)->row, i, &requisition);
2792                     cell_width[i] = MAX (requisition.width, cell_width[i]);
2793                   }
2794
2795               list = (GList *)work;
2796               work = GTK_CTREE_NODE_NEXT (work);
2797               tmp++;
2798             }
2799         }
2800       else
2801         while (work)
2802           {
2803             list = (GList *)work;
2804             work = GTK_CTREE_NODE_NEXT (work);
2805             tmp++;
2806           }
2807
2808       list->next = (GList *)GTK_CTREE_NODE_NEXT (node);
2809
2810       if (GTK_CTREE_NODE_NEXT (node))
2811         {
2812           GList *tmp_list;
2813
2814           if (clist->row_list_end == list)
2815               clist->row_list_end = g_list_last(list);
2816
2817           tmp_list = (GList *)GTK_CTREE_NODE_NEXT (node);
2818           tmp_list->prev = list;
2819         }
2820       else
2821         clist->row_list_end = list;
2822
2823       list = (GList *)node;
2824       list->next = (GList *)(GTK_CTREE_ROW (node)->children);
2825
2826       if (visible)
2827         {
2828           /* resize auto_resize columns if needed */
2829           for (i = 0; i < clist->columns; i++)
2830             if (clist->column[i].auto_resize &&
2831                 cell_width[i] > clist->column[i].width)
2832               gtk_clist_set_column_width (clist, i, cell_width[i]);
2833           g_free (cell_width);
2834         
2835           if (!GTK_SCTREE(ctree)->sorting) {
2836                   /* update focus_row position */
2837                   row = g_list_position (clist->row_list, (GList *)node);
2838                   if (row < clist->focus_row)
2839                     clist->focus_row += tmp;
2840           }
2841           clist->rows += tmp;
2842           CLIST_REFRESH (clist);
2843         }
2844     }
2845   else if (visible && clist->column[ctree->tree_column].auto_resize)
2846     /* resize tree_column if needed */
2847     gtk_sctree_column_auto_resize (clist, &GTK_CTREE_ROW (node)->row, ctree->tree_column,
2848                         requisition.width);
2849
2850 }
2851
2852 GtkCTreeNode * 
2853 gtk_sctree_insert_node (GtkCTree     *ctree,
2854                        GtkCTreeNode *parent, 
2855                        GtkCTreeNode *sibling,
2856                        gchar        *text[],
2857                        guint8        spacing,
2858                        GdkPixmap    *pixmap_closed,
2859                        GdkBitmap    *mask_closed,
2860                        GdkPixmap    *pixmap_opened,
2861                        GdkBitmap    *mask_opened,
2862                        gboolean      is_leaf,
2863                        gboolean      expanded)
2864 {
2865   GtkCList *clist;
2866   GtkCTreeRow *new_row;
2867   GtkCTreeNode *node;
2868   GList *list;
2869   gint i;
2870
2871   if (!emptyxpm) {
2872           stock_pixmap_gdk(GTK_WIDGET(ctree), STOCK_PIXMAP_EMPTY,
2873                    &emptyxpm, &emptyxpmmask);
2874   }
2875   if (!pixmap_closed) {
2876           pixmap_closed = emptyxpm;
2877           mask_closed = emptyxpmmask;
2878   }
2879   if (!pixmap_opened) {
2880           pixmap_opened = emptyxpm;
2881           mask_opened = emptyxpmmask;
2882   }
2883   g_return_val_if_fail (GTK_IS_CTREE (ctree), NULL);
2884   if (sibling)
2885     g_return_val_if_fail (GTK_CTREE_ROW (sibling)->parent == parent, NULL);
2886
2887   if (parent && GTK_CTREE_ROW (parent)->is_leaf)
2888     return NULL;
2889
2890   clist = GTK_CLIST (ctree);
2891
2892   /* create the row */
2893   new_row = srow_new (ctree);
2894   list = g_list_alloc ();
2895   list->data = new_row;
2896   node = GTK_CTREE_NODE (list);
2897
2898   if (text)
2899     for (i = 0; i < clist->columns; i++)
2900       if (text[i] && i != ctree->tree_column)
2901         GTK_CLIST_GET_CLASS (clist)->set_cell_contents
2902           (clist, &(new_row->row), i, GTK_CELL_TEXT, text[i], 0, NULL, NULL);
2903
2904   sset_node_info (ctree, node, text ?
2905                  text[ctree->tree_column] : NULL, spacing, pixmap_closed,
2906                  mask_closed, pixmap_opened, mask_opened, is_leaf, expanded);
2907
2908   /* sorted insertion */
2909   if (GTK_CLIST_AUTO_SORT (clist))
2910     {
2911       if (parent)
2912         sibling = GTK_CTREE_ROW (parent)->children;
2913       else
2914         sibling = GTK_CTREE_NODE (clist->row_list);
2915
2916       while (sibling && clist->compare
2917              (clist, GTK_CTREE_ROW (node), GTK_CTREE_ROW (sibling)) > 0)
2918         sibling = GTK_CTREE_ROW (sibling)->sibling;
2919     }
2920
2921   gtk_sctree_link (ctree, node, parent, sibling, FALSE);
2922
2923   if (text && !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist) &&
2924       gtk_ctree_is_viewable (ctree, node))
2925     {
2926       for (i = 0; i < clist->columns; i++)
2927         if (clist->column[i].auto_resize)
2928           gtk_sctree_column_auto_resize (clist, &(new_row->row), i, 0);
2929     }
2930
2931   if (clist->rows == 1)
2932     {
2933       clist->focus_row = 0;
2934       if (clist->selection_mode == GTK_SELECTION_BROWSE)
2935         gtk_sctree_select (GTK_SCTREE(ctree), node);
2936     }
2937
2938
2939   CLIST_REFRESH (clist);
2940
2941   return node;
2942 }
2943
2944 GtkCTreeNode *
2945 gtk_sctree_insert_gnode (GtkCTree          *ctree,
2946                         GtkCTreeNode      *parent,
2947                         GtkCTreeNode      *sibling,
2948                         GNode             *gnode,
2949                         GtkCTreeGNodeFunc  func,
2950                         gpointer           data)
2951 {
2952   GtkCList *clist;
2953   GtkCTreeNode *cnode = NULL;
2954   GtkCTreeNode *child = NULL;
2955   GtkCTreeNode *new_child;
2956   GList *list;
2957   GNode *work;
2958   guint depth = 1;
2959
2960   g_return_val_if_fail (GTK_IS_CTREE (ctree), NULL);
2961   g_return_val_if_fail (gnode != NULL, NULL);
2962   g_return_val_if_fail (func != NULL, NULL);
2963   if (sibling)
2964     g_return_val_if_fail (GTK_CTREE_ROW (sibling)->parent == parent, NULL);
2965   
2966   clist = GTK_CLIST (ctree);
2967
2968   if (parent)
2969     depth = GTK_CTREE_ROW (parent)->level + 1;
2970
2971   list = g_list_alloc ();
2972   list->data = srow_new (ctree);
2973   cnode = GTK_CTREE_NODE (list);
2974
2975   gtk_clist_freeze (clist);
2976
2977   sset_node_info (ctree, cnode, "", 0, NULL, NULL, NULL, NULL, TRUE, FALSE);
2978
2979   if (!func (ctree, depth, gnode, cnode, data))
2980     {
2981       stree_delete_row (ctree, cnode, NULL);
2982       gtk_clist_thaw (clist);
2983       return NULL;
2984     }
2985
2986   if (GTK_CLIST_AUTO_SORT (clist))
2987     {
2988       if (parent)
2989         sibling = GTK_CTREE_ROW (parent)->children;
2990       else
2991         sibling = GTK_CTREE_NODE (clist->row_list);
2992
2993       while (sibling && clist->compare
2994              (clist, GTK_CTREE_ROW (cnode), GTK_CTREE_ROW (sibling)) > 0)
2995         sibling = GTK_CTREE_ROW (sibling)->sibling;
2996     }
2997
2998   gtk_sctree_link (ctree, cnode, parent, sibling, FALSE);
2999
3000   for (work = g_node_last_child (gnode); work; work = work->prev)
3001     {
3002       new_child = gtk_sctree_insert_gnode (ctree, cnode, child,
3003                                           work, func, data);
3004       if (new_child)
3005         child = new_child;
3006     }   
3007   
3008   gtk_clist_thaw (clist);
3009
3010   return cnode;
3011 }
3012
3013 static void
3014 sreal_tree_move (GtkCTree     *ctree,
3015                 GtkCTreeNode *node,
3016                 GtkCTreeNode *new_parent, 
3017                 GtkCTreeNode *new_sibling)
3018 {
3019   GtkCList *clist;
3020   GtkCTreeNode *work;
3021   gboolean visible = FALSE;
3022
3023   g_return_if_fail (ctree != NULL);
3024   g_return_if_fail (node != NULL);
3025   g_return_if_fail (!new_sibling || 
3026                     GTK_CTREE_ROW (new_sibling)->parent == new_parent);
3027
3028   if (new_parent && GTK_CTREE_ROW (new_parent)->is_leaf)
3029     return;
3030
3031   /* new_parent != child of child */
3032   for (work = new_parent; work; work = GTK_CTREE_ROW (work)->parent)
3033     if (work == node)
3034       return;
3035
3036   clist = GTK_CLIST (ctree);
3037
3038   visible = gtk_ctree_is_viewable (ctree, node);
3039
3040   if (clist->selection_mode == GTK_SELECTION_MULTIPLE)
3041     {
3042       GTK_CLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3043       
3044       g_list_free (clist->undo_selection);
3045       g_list_free (clist->undo_unselection);
3046       clist->undo_selection = NULL;
3047       clist->undo_unselection = NULL;
3048     }
3049
3050   if (GTK_CLIST_AUTO_SORT (clist))
3051     {
3052       if (new_parent == GTK_CTREE_ROW (node)->parent)
3053         return;
3054       
3055       if (new_parent)
3056         new_sibling = GTK_CTREE_ROW (new_parent)->children;
3057       else
3058         new_sibling = GTK_CTREE_NODE (clist->row_list);
3059
3060       while (new_sibling && clist->compare
3061              (clist, GTK_CTREE_ROW (node), GTK_CTREE_ROW (new_sibling)) > 0)
3062         new_sibling = GTK_CTREE_ROW (new_sibling)->sibling;
3063     }
3064
3065   if (new_parent == GTK_CTREE_ROW (node)->parent && 
3066       new_sibling == GTK_CTREE_ROW (node)->sibling)
3067     return;
3068
3069   gtk_clist_freeze (clist);
3070
3071   work = NULL;
3072
3073   if (!GTK_SCTREE(ctree)->sorting && gtk_ctree_is_viewable (ctree, node))
3074     work = GTK_CTREE_NODE (g_list_nth (clist->row_list, clist->focus_row));
3075       
3076   gtk_sctree_unlink (ctree, node, FALSE);
3077   gtk_sctree_link (ctree, node, new_parent, new_sibling, FALSE);
3078   
3079   if (!GTK_SCTREE(ctree)->sorting && work)
3080     {
3081       while (work &&  !gtk_ctree_is_viewable (ctree, work))
3082         work = GTK_CTREE_ROW (work)->parent;
3083       clist->focus_row = g_list_position (clist->row_list, (GList *)work);
3084       clist->undo_anchor = clist->focus_row;
3085     }
3086
3087   if (clist->column[ctree->tree_column].auto_resize &&
3088       !GTK_CLIST_AUTO_RESIZE_BLOCKED (clist) &&
3089       (visible || gtk_ctree_is_viewable (ctree, node)))
3090     gtk_clist_set_column_width
3091       (clist, ctree->tree_column,
3092        gtk_clist_optimal_column_width (clist, ctree->tree_column));
3093
3094   gtk_clist_thaw (clist);
3095 }
3096
3097 void gtk_sctree_set_column_tooltip          (GtkSCTree          *sctree,
3098                                              int                 column,
3099                                              const gchar        *tip)
3100 {
3101         if (!sctree->tips)
3102                 sctree->tips = gtk_tooltips_new();
3103                 
3104         gtk_tooltips_set_tip(GTK_TOOLTIPS(sctree->tips), 
3105                 GTK_CLIST(sctree)->column[column].button,
3106                         tip, NULL);
3107
3108 }
3109