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