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