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