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