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