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