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