3a28036e9ab4c5748ff1632b5418c0196411957e
[claws.git] / src / gtk / gtkcmclist.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball, Josh MacDonald, 
3  * Copyright (C) 1997-1998 Jay Painter <jpaint@serv.net><jpaint@gimp.org>  
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 /*
22  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23  * file for a list of people on the GTK+ Team.  See the ChangeLog
24  * files for a list of changes.  These files are distributed with
25  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
26  */
27
28 #include <config.h>
29
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <gtk/gtk.h>
34 #include "claws-marshal.h"
35 #include "gtkcmclist.h"
36 #include <gdk/gdkkeysyms.h>
37 #include "utils.h"
38 #include "gtkutils.h"
39
40 /* length of button_actions array */
41 #define MAX_BUTTON 5
42
43 /* the number rows memchunk expands at a time */
44 #define CMCLIST_OPTIMUM_SIZE 64
45
46 /* the width of the column resize windows */
47 #define DRAG_WIDTH  6
48
49 /* minimum allowed width of a column */
50 #define COLUMN_MIN_WIDTH 5
51
52 /* this defigns the base grid spacing */
53 #define CELL_SPACING 1
54
55 /* added the horizontal space at the beginning and end of a row*/
56 #define COLUMN_INSET 3
57
58 /* used for auto-scrolling */
59 #define SCROLL_TIME  100
60
61 /* gives the top pixel of the given row in context of
62  * the clist's voffset */
63 #define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
64                                     (((row) + 1) * CELL_SPACING) + \
65                                     (clist)->voffset)
66
67 /* returns the row index from a y pixel location in the 
68  * context of the clist's voffset */
69 #define ROW_FROM_YPIXEL(clist, y)  (((y) - (clist)->voffset) / \
70                                     ((clist)->row_height + CELL_SPACING))
71
72 /* gives the left pixel of the given column in context of
73  * the clist's hoffset */
74 #define COLUMN_LEFT_XPIXEL(clist, colnum)  ((clist)->column[(colnum)].area.x + \
75                                             (clist)->hoffset)
76
77 /* returns the column index from a x pixel location in the 
78  * context of the clist's hoffset */
79 static inline gint
80 COLUMN_FROM_XPIXEL (GtkCMCList * clist,
81                     gint x)
82 {
83   gint i, cx;
84
85   for (i = 0; i < clist->columns; i++)
86     if (clist->column[i].visible)
87       {
88         cx = clist->column[i].area.x + clist->hoffset;
89
90         if (x >= (cx - (COLUMN_INSET + CELL_SPACING)) &&
91             x <= (cx + clist->column[i].area.width + COLUMN_INSET))
92           return i;
93       }
94
95   /* no match */
96   return -1;
97 }
98
99 /* returns the top pixel of the given row in the context of
100  * the list height */
101 #define ROW_TOP(clist, row)        (((clist)->row_height + CELL_SPACING) * (row))
102
103 /* returns the left pixel of the given column in the context of
104  * the list width */
105 #define COLUMN_LEFT(clist, colnum) ((clist)->column[(colnum)].area.x)
106
107 /* returns the total height of the list */
108 #define LIST_HEIGHT(clist)         (((clist)->row_height * ((clist)->rows)) + \
109                                     (CELL_SPACING * ((clist)->rows + 1)))
110
111
112 /* returns the total width of the list */
113 static inline gint
114 LIST_WIDTH (GtkCMCList * clist) 
115 {
116   gint last_column;
117
118   for (last_column = clist->columns - 1;
119        last_column >= 0 && !clist->column[last_column].visible; last_column--);
120
121   if (last_column >= 0)
122     return (clist->column[last_column].area.x +
123             clist->column[last_column].area.width +
124             COLUMN_INSET + CELL_SPACING);
125   return 0;
126 }
127
128 /* returns the GList item for the nth row */
129 #define ROW_ELEMENT(clist, row) (((row) == (clist)->rows - 1) ? \
130                                  (clist)->row_list_end : \
131                                  g_list_nth ((clist)->row_list, (row)))
132
133
134 /* redraw the list if it's not frozen */
135 #define CLIST_UNFROZEN(clist)     (((GtkCMCList*) (clist))->freeze_count == 0)
136 #define CLIST_REFRESH(clist)    G_STMT_START { \
137   if (CLIST_UNFROZEN (clist)) \
138     GTK_CMCLIST_GET_CLASS (clist)->refresh ((GtkCMCList*) (clist)); \
139 } G_STMT_END
140
141
142 /* Signals */
143 enum {
144   SELECT_ROW,
145   UNSELECT_ROW,
146   ROW_MOVE,
147   CLICK_COLUMN,
148   RESIZE_COLUMN,
149   TOGGLE_FOCUS_ROW,
150   SELECT_ALL,
151   UNSELECT_ALL,
152   UNDO_SELECTION,
153   START_SELECTION,
154   END_SELECTION,
155   TOGGLE_ADD_MODE,
156   EXTEND_SELECTION,
157   SCROLL_VERTICAL,
158   SCROLL_HORIZONTAL,
159   ABORT_COLUMN_RESIZE,
160   LAST_SIGNAL
161 };
162
163 enum {
164   SYNC_REMOVE,
165   SYNC_INSERT
166 };
167
168 enum {
169   ARG_0,
170   ARG_N_COLUMNS,
171   ARG_SHADOW_TYPE,
172   ARG_SELECTION_MODE,
173   ARG_ROW_HEIGHT,
174   ARG_TITLES_ACTIVE,
175   ARG_REORDERABLE,
176   ARG_USE_DRAG_ICONS,
177   ARG_SORT_TYPE
178 #if GTK_CHECK_VERSION(3, 0, 0)
179   ,
180   ARG_HADJUSTMENT,
181   ARG_VADJUSTMENT,
182   ARG_HADJUSTMENT_POLICY,
183   ARG_VADJUSTMENT_POLICY
184 #endif
185 };
186
187 /* GtkCMCList Methods */
188 static void     gtk_cmclist_class_init  (GtkCMCListClass         *klass);
189 static void     gtk_cmclist_init        (GtkCMCList              *clist);
190 static GObject* gtk_cmclist_constructor (GType                  type,
191                                        guint                  n_construct_properties,
192                                        GObjectConstructParam *construct_params);
193
194 /* GtkObject Methods */
195 #if !GTK_CHECK_VERSION(3, 0, 0)
196 static void gtk_cmclist_destroy  (GtkObject *object);
197 #else
198 static void gtk_cmclist_destroy  (GtkWidget *object);
199 #endif
200 static void gtk_cmclist_finalize (GObject   *object);
201 static void gtk_cmclist_set_arg  (GObject *object,
202                                 guint      arg_id,
203                                 const GValue *value,
204                                 GParamSpec *spec);
205 static void gtk_cmclist_get_arg  (GObject *object,
206                                 guint      arg_id,
207                                 GValue *value,
208                                 GParamSpec *spec);
209
210 /* GtkWidget Methods */
211 #if !GTK_CHECK_VERSION(3, 0, 0)
212 static void gtk_cmclist_set_scroll_adjustments (GtkCMCList      *clist,
213                                               GtkAdjustment *hadjustment,
214                                               GtkAdjustment *vadjustment);
215 #endif
216 static void gtk_cmclist_realize         (GtkWidget        *widget);
217 static void gtk_cmclist_unrealize       (GtkWidget        *widget);
218 static void gtk_cmclist_map             (GtkWidget        *widget);
219 static void gtk_cmclist_unmap           (GtkWidget        *widget);
220 #if !GTK_CHECK_VERSION(3, 0, 0)
221 static gint gtk_cmclist_expose          (GtkWidget        *widget,
222                                          GdkEventExpose   *event);
223 #else
224 static gint gtk_cmclist_expose          (GtkWidget *widget,
225                                          cairo_t *event);
226 #endif
227 static gint gtk_cmclist_button_press    (GtkWidget        *widget,
228                                        GdkEventButton   *event);
229 static gint gtk_cmclist_button_release  (GtkWidget        *widget,
230                                        GdkEventButton   *event);
231 static gint gtk_cmclist_motion          (GtkWidget        *widget, 
232                                        GdkEventMotion   *event);
233 #if GTK_CHECK_VERSION(3, 0, 0)
234 static void gtk_cmclist_get_preferred_height (GtkWidget *widget,
235                                  gint      *minimal_height,
236                                  gint      *natural_height);
237 static void gtk_cmclist_get_preferred_width (GtkWidget *widget,
238                                  gint      *minimal_width,
239                                  gint      *natural_width);
240 #endif
241 static void gtk_cmclist_size_request    (GtkWidget        *widget,
242                                        GtkRequisition   *requisition);
243 static void gtk_cmclist_size_allocate   (GtkWidget        *widget,
244                                        GtkAllocation    *allocation);
245 static void gtk_cmclist_undraw_focus      (GtkWidget        *widget);
246 static void gtk_cmclist_draw_focus      (GtkWidget        *widget);
247 static gint gtk_cmclist_focus_in        (GtkWidget        *widget,
248                                        GdkEventFocus    *event);
249 static gint gtk_cmclist_focus_out       (GtkWidget        *widget,
250                                        GdkEventFocus    *event);
251 static gint gtk_cmclist_focus           (GtkWidget        *widget,
252                                        GtkDirectionType  direction);
253 static void gtk_cmclist_set_focus_child (GtkContainer     *container,
254                                        GtkWidget        *child);
255 static void gtk_cmclist_style_set       (GtkWidget        *widget,
256                                        GtkStyle         *previous_style);
257 static void gtk_cmclist_drag_begin      (GtkWidget        *widget,
258                                        GdkDragContext   *context);
259 static gint gtk_cmclist_drag_motion     (GtkWidget        *widget,
260                                        GdkDragContext   *context,
261                                        gint              x,
262                                        gint              y,
263                                        guint             time);
264 static void gtk_cmclist_drag_leave      (GtkWidget        *widget,
265                                        GdkDragContext   *context,
266                                        guint             time);
267 static void gtk_cmclist_drag_end        (GtkWidget        *widget,
268                                        GdkDragContext   *context);
269 static gboolean gtk_cmclist_drag_drop   (GtkWidget      *widget,
270                                        GdkDragContext *context,
271                                        gint            x,
272                                        gint            y,
273                                        guint           time);
274 static void gtk_cmclist_drag_data_get   (GtkWidget        *widget,
275                                        GdkDragContext   *context,
276                                        GtkSelectionData *selection_data,
277                                        guint             info,
278                                        guint             time);
279 static void gtk_cmclist_drag_data_received (GtkWidget        *widget,
280                                           GdkDragContext   *context,
281                                           gint              x,
282                                           gint              y,
283                                           GtkSelectionData *selection_data,
284                                           guint             info,
285                                           guint             time);
286
287 /* GtkContainer Methods */
288 static void gtk_cmclist_forall          (GtkContainer  *container,
289                                        gboolean       include_internals,
290                                        GtkCallback    callback,
291                                        gpointer       callback_data);
292
293 /* Selection */
294 static void toggle_row                (GtkCMCList      *clist,
295                                        gint           row,
296                                        gint           column,
297                                        GdkEvent      *event);
298 static void real_select_row           (GtkCMCList      *clist,
299                                        gint           row,
300                                        gint           column,
301                                        GdkEvent      *event);
302 static void real_unselect_row         (GtkCMCList      *clist,
303                                        gint           row,
304                                        gint           column,
305                                        GdkEvent      *event);
306 static void update_extended_selection (GtkCMCList      *clist,
307                                        gint           row);
308 static GList *selection_find          (GtkCMCList      *clist,
309                                        gint           row_number,
310                                        GList         *row_list_element);
311 static void real_select_all           (GtkCMCList      *clist);
312 static void real_unselect_all         (GtkCMCList      *clist);
313 static void move_vertical             (GtkCMCList      *clist,
314                                        gint           row,
315                                        gfloat         align);
316 static void move_horizontal           (GtkCMCList      *clist,
317                                        gint           diff);
318 static void real_undo_selection       (GtkCMCList      *clist);
319 static void fake_unselect_all         (GtkCMCList      *clist,
320                                        gint           row);
321 static void fake_toggle_row           (GtkCMCList      *clist,
322                                        gint           row);
323 static void resync_selection          (GtkCMCList      *clist,
324                                        GdkEvent      *event);
325 static void sync_selection            (GtkCMCList      *clist,
326                                        gint           row,
327                                        gint           mode);
328 static void set_anchor                (GtkCMCList      *clist,
329                                        gboolean       add_mode,
330                                        gint           anchor,
331                                        gint           undo_anchor);
332 static void start_selection           (GtkCMCList      *clist);
333 static void end_selection             (GtkCMCList      *clist);
334 static void toggle_add_mode           (GtkCMCList      *clist);
335 static void toggle_focus_row          (GtkCMCList      *clist);
336 static void extend_selection          (GtkCMCList      *clist,
337                                        GtkScrollType  scroll_type,
338                                        gfloat         position,
339                                        gboolean       auto_start_selection);
340 static gint get_selection_info        (GtkCMCList       *clist,
341                                        gint            x,
342                                        gint            y,
343                                        gint           *row,
344                                        gint           *column);
345
346 /* Scrolling */
347 static void move_focus_row     (GtkCMCList      *clist,
348                                 GtkScrollType  scroll_type,
349                                 gfloat         position);
350 static void scroll_horizontal  (GtkCMCList      *clist,
351                                 GtkScrollType  scroll_type,
352                                 gfloat         position);
353 static void scroll_vertical    (GtkCMCList      *clist,
354                                 GtkScrollType  scroll_type,
355                                 gfloat         position);
356 static void move_horizontal    (GtkCMCList      *clist,
357                                 gint           diff);
358 static void move_vertical      (GtkCMCList      *clist,
359                                 gint           row,
360                                 gfloat         align);
361 static gint horizontal_timeout (GtkCMCList      *clist);
362 static gint vertical_timeout   (GtkCMCList      *clist);
363 static void remove_grab        (GtkCMCList      *clist);
364
365
366 /* Resize Columns */
367 static void draw_xor_line             (GtkCMCList       *clist);
368 static gint new_column_width          (GtkCMCList       *clist,
369                                        gint            column,
370                                        gint           *x);
371 static void column_auto_resize        (GtkCMCList       *clist,
372                                        GtkCMCListRow    *clist_row,
373                                        gint            column,
374                                        gint            old_width);
375 static void real_resize_column        (GtkCMCList       *clist,
376                                        gint            column,
377                                        gint            width);
378 static void abort_column_resize       (GtkCMCList       *clist);
379 static void cell_size_request         (GtkCMCList       *clist,
380                                        GtkCMCListRow    *clist_row,
381                                        gint            column,
382                                        GtkRequisition *requisition);
383
384 /* Buttons */
385 static void column_button_create      (GtkCMCList       *clist,
386                                        gint            column);
387 static void column_button_clicked     (GtkWidget      *widget,
388                                        gpointer        data);
389
390 /* Adjustments */
391 static void adjust_adjustments        (GtkCMCList       *clist,
392                                        gboolean        block_resize);
393 static void vadjustment_value_changed (GtkAdjustment  *adjustment,
394                                        gpointer        data);
395 static void hadjustment_value_changed (GtkAdjustment  *adjustment,
396                                        gpointer        data);
397
398 /* Drawing */
399 static void get_cell_style   (GtkCMCList      *clist,
400                               GtkCMCListRow   *clist_row,
401                               gint           state,
402                               gint           column,
403                               GtkStyle     **style);
404 static gint draw_cell_pixbuf (GdkWindow     *window,
405                               GdkRectangle  *clip_rectangle,
406                               cairo_t       *cr,
407                               GdkPixbuf     *pixbuf,
408                               gint           x,
409                               gint           y,
410                               gint           width,
411                               gint           height);
412 static void draw_row         (GtkCMCList      *clist,
413                               GdkRectangle  *area,
414                               gint           row,
415                               GtkCMCListRow   *clist_row);
416 static void draw_rows        (GtkCMCList      *clist,
417                               GdkRectangle  *area);
418 static void clist_refresh    (GtkCMCList      *clist);
419      
420 /* Size Allocation / Requisition */
421 static void size_allocate_title_buttons (GtkCMCList *clist);
422 static void size_allocate_columns       (GtkCMCList *clist,
423                                          gboolean  block_resize);
424 static gint list_requisition_width      (GtkCMCList *clist);
425
426 /* Memory Allocation/Distruction Routines */
427 static GtkCMCListColumn *columns_new (GtkCMCList      *clist);
428 static void column_title_new       (GtkCMCList      *clist,
429                                     gint           column,
430                                     const gchar   *title);
431 static void columns_delete         (GtkCMCList      *clist);
432 static GtkCMCListRow *row_new        (GtkCMCList      *clist);
433 static void row_delete             (GtkCMCList      *clist,
434                                     GtkCMCListRow   *clist_row);
435 static void set_cell_contents      (GtkCMCList      *clist,
436                                     GtkCMCListRow   *clist_row,
437                                     gint           column,
438                                     GtkCMCellType    type,
439                                     const gchar   *text,
440                                     guint8         spacing,
441                                     GdkPixbuf     *pixbuf);
442 static gint real_insert_row        (GtkCMCList      *clist,
443                                     gint           row,
444                                     gchar         *text[]);
445 static void real_remove_row        (GtkCMCList      *clist,
446                                     gint           row);
447 static void real_clear             (GtkCMCList      *clist);
448
449 /* Sorting */
450 static gint default_compare        (GtkCMCList      *clist,
451                                     gconstpointer  row1,
452                                     gconstpointer  row2);
453 static void real_sort_list         (GtkCMCList      *clist);
454 static GList *gtk_cmclist_merge      (GtkCMCList      *clist,
455                                     GList         *a,
456                                     GList         *b);
457 static GList *gtk_cmclist_mergesort  (GtkCMCList      *clist,
458                                     GList         *list,
459                                     gint           num);
460 /* Misc */
461 static gboolean title_focus_in   (GtkCMCList *clist,
462                                   gint      dir);
463 static gboolean title_focus_move (GtkCMCList *clist,
464                                   gint      dir);
465
466 static void real_row_move             (GtkCMCList  *clist,
467                                        gint       source_row,
468                                        gint       dest_row);
469 static gint column_title_passive_func (GtkWidget *widget, 
470                                        GdkEvent  *event,
471                                        gpointer   data);
472 static void drag_dest_cell            (GtkCMCList         *clist,
473                                        gint              x,
474                                        gint              y,
475                                        GtkCMCListDestInfo *dest_info);
476
477
478
479 static guint clist_signals[LAST_SIGNAL] = {0};
480
481 static const GtkTargetEntry clist_target_table = { "gtk-clist-drag-reorder", 0, 0};
482
483 #if !GTK_CHECK_VERSION(3, 0, 0)
484 static gpointer gtk_cmclist_parent_class = NULL;
485
486 GType
487 gtk_cmclist_get_type (void)
488 {
489   static GType clist_type = 0;
490
491   if (!clist_type)
492     {
493       static const GTypeInfo clist_info =
494       {
495                         sizeof (GtkCMCListClass),
496
497                         (GBaseInitFunc) NULL,
498                         (GBaseFinalizeFunc) NULL,
499
500                         (GClassInitFunc) gtk_cmclist_class_init,
501                         (GClassFinalizeFunc) NULL,
502                         NULL,   /* class_data */
503
504                         sizeof (GtkCMCList),
505                         0,      /* n_preallocs */
506                         (GInstanceInitFunc) gtk_cmclist_init,
507         };
508         clist_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkCMCList", &clist_info, (GTypeFlags)0);
509     }
510
511   return clist_type;
512 }
513 #else
514 G_DEFINE_TYPE_WITH_CODE (GtkCMCList, gtk_cmclist, GTK_TYPE_CONTAINER,
515                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE,
516                          NULL))
517 #endif
518
519 static void
520 gtk_cmclist_class_init (GtkCMCListClass *klass)
521 {
522   GObjectClass *object_class = G_OBJECT_CLASS (klass);
523 #if !GTK_CHECK_VERSION(3, 0, 0)
524   GtkObjectClass *gtk_object_class;
525 #endif
526   GtkWidgetClass *widget_class;
527   GtkContainerClass *container_class;
528   GtkBindingSet *binding_set;
529
530   object_class->constructor = gtk_cmclist_constructor;
531
532 #if !GTK_CHECK_VERSION(3, 0, 0)
533   gtk_object_class = (GtkObjectClass *) klass;
534 #endif
535   widget_class = (GtkWidgetClass *) klass;
536   container_class = (GtkContainerClass *) klass;
537
538 #if !GTK_CHECK_VERSION(3, 0, 0)
539   gtk_cmclist_parent_class = g_type_class_peek (GTK_TYPE_CONTAINER);
540 #endif
541
542   object_class->finalize = gtk_cmclist_finalize;
543 #if !GTK_CHECK_VERSION(3, 0, 0)
544   gtk_object_class->destroy = gtk_cmclist_destroy;
545 #else
546   widget_class->destroy = gtk_cmclist_destroy;
547 #endif
548   object_class->set_property = gtk_cmclist_set_arg;
549   object_class->get_property = gtk_cmclist_get_arg;
550   
551
552   widget_class->realize = gtk_cmclist_realize;
553   widget_class->unrealize = gtk_cmclist_unrealize;
554   widget_class->map = gtk_cmclist_map;
555   widget_class->unmap = gtk_cmclist_unmap;
556   widget_class->button_press_event = gtk_cmclist_button_press;
557   widget_class->button_release_event = gtk_cmclist_button_release;
558   widget_class->motion_notify_event = gtk_cmclist_motion;
559 #if !GTK_CHECK_VERSION(3, 0, 0)
560   widget_class->expose_event = gtk_cmclist_expose;
561 #else
562   widget_class->draw = gtk_cmclist_expose;
563 #endif
564 #if !GTK_CHECK_VERSION(3, 0, 0)
565   widget_class->size_request = gtk_cmclist_size_request;
566 #else
567   widget_class->get_preferred_width = gtk_cmclist_get_preferred_width;
568   widget_class->get_preferred_height = gtk_cmclist_get_preferred_height;
569 #endif
570   widget_class->size_allocate = gtk_cmclist_size_allocate;
571   widget_class->focus_in_event = gtk_cmclist_focus_in;
572   widget_class->focus_out_event = gtk_cmclist_focus_out;
573   widget_class->style_set = gtk_cmclist_style_set;
574   widget_class->drag_begin = gtk_cmclist_drag_begin;
575   widget_class->drag_end = gtk_cmclist_drag_end;
576   widget_class->drag_motion = gtk_cmclist_drag_motion;
577   widget_class->drag_leave = gtk_cmclist_drag_leave;
578   widget_class->drag_drop = gtk_cmclist_drag_drop;
579   widget_class->drag_data_get = gtk_cmclist_drag_data_get;
580   widget_class->drag_data_received = gtk_cmclist_drag_data_received;
581   widget_class->focus = gtk_cmclist_focus;
582   
583   /* container_class->add = NULL; use the default GtkContainerClass warning */
584   /* container_class->remove=NULL; use the default GtkContainerClass warning */
585
586   container_class->forall = gtk_cmclist_forall;
587   container_class->set_focus_child = gtk_cmclist_set_focus_child;
588
589 #if !GTK_CHECK_VERSION(3, 0, 0)
590   klass->set_scroll_adjustments = gtk_cmclist_set_scroll_adjustments;
591 #endif
592   klass->refresh = clist_refresh;
593   klass->select_row = real_select_row;
594   klass->unselect_row = real_unselect_row;
595   klass->row_move = real_row_move;
596   klass->undo_selection = real_undo_selection;
597   klass->resync_selection = resync_selection;
598   klass->selection_find = selection_find;
599   klass->click_column = NULL;
600   klass->resize_column = real_resize_column;
601   klass->draw_row = draw_row;
602   klass->insert_row = real_insert_row;
603   klass->remove_row = real_remove_row;
604   klass->clear = real_clear;
605   klass->sort_list = real_sort_list;
606   klass->select_all = real_select_all;
607   klass->unselect_all = real_unselect_all;
608   klass->fake_unselect_all = fake_unselect_all;
609   klass->scroll_horizontal = scroll_horizontal;
610   klass->scroll_vertical = scroll_vertical;
611   klass->extend_selection = extend_selection;
612   klass->toggle_focus_row = toggle_focus_row;
613   klass->toggle_add_mode = toggle_add_mode;
614   klass->start_selection = start_selection;
615   klass->end_selection = end_selection;
616   klass->abort_column_resize = abort_column_resize;
617   klass->set_cell_contents = set_cell_contents;
618   klass->cell_size_request = cell_size_request;
619
620   g_object_class_install_property (object_class,
621                                 ARG_N_COLUMNS,
622                                 g_param_spec_uint ("n-columns",
623                                 "N-Columns",
624                                 "N-Columns",
625                                 1,
626                                 G_MAXINT,
627                                 1,
628                                 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
629   g_object_class_install_property (object_class,
630                                 ARG_SHADOW_TYPE,
631                                 g_param_spec_enum ("shadow-type",
632                                 "shadow-type",
633                                 "shadow-type",
634                                 GTK_TYPE_SHADOW_TYPE, 0,
635                                 G_PARAM_READWRITE));
636   g_object_class_install_property (object_class,
637                                 ARG_SELECTION_MODE,
638                                 g_param_spec_enum ("selection-mode",
639                                 "selection-mode",
640                                 "selection-mode",
641                                 GTK_TYPE_SELECTION_MODE, 0,
642                                 G_PARAM_READWRITE));
643   g_object_class_install_property (object_class,
644                                 ARG_ROW_HEIGHT,
645                                 g_param_spec_uint ("row-height",
646                                 "row-height",
647                                 "row-height",
648                                 0,
649                                 G_MAXINT,
650                                 0,
651                                 G_PARAM_READWRITE));
652   g_object_class_install_property (object_class,
653                                 ARG_REORDERABLE,
654                                 g_param_spec_boolean ("reorderable",
655                                 "reorderable",
656                                 "reorderable",
657                                 TRUE,
658                                 G_PARAM_READWRITE));
659   g_object_class_install_property (object_class,
660                                 ARG_TITLES_ACTIVE,
661                                 g_param_spec_boolean ("titles-active",
662                                 "titles-active",
663                                 "titles-active",
664                                 TRUE,
665                                 G_PARAM_READWRITE));
666   g_object_class_install_property (object_class,
667                                 ARG_USE_DRAG_ICONS,
668                                 g_param_spec_boolean ("use-drag-icons",
669                                 "use-drag-icons",
670                                 "use-drag-icons",
671                                 TRUE,
672                                 G_PARAM_READWRITE));
673   g_object_class_install_property (object_class,
674                                 ARG_SORT_TYPE,
675                                 g_param_spec_enum ("sort-type",
676                                 "sort-type",
677                                 "sort-type",
678                                 GTK_TYPE_SORT_TYPE, 0,
679                                 G_PARAM_READWRITE));
680 #if !GTK_CHECK_VERSION(3, 0, 0)
681   widget_class->set_scroll_adjustments_signal =
682                 g_signal_new ("set_scroll_adjustments",
683                               G_TYPE_FROM_CLASS (object_class),
684                               G_SIGNAL_RUN_LAST,
685                               G_STRUCT_OFFSET (GtkCMCListClass, set_scroll_adjustments),
686                               NULL, NULL,
687                               claws_marshal_VOID__OBJECT_OBJECT,
688                               G_TYPE_NONE, 2,
689                               GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
690 #else
691   /* Scrollable interface properties */
692   g_object_class_override_property (object_class, ARG_HADJUSTMENT, "hadjustment");
693   g_object_class_override_property (object_class, ARG_VADJUSTMENT, "vadjustment");
694   g_object_class_override_property (object_class, ARG_HADJUSTMENT_POLICY, "hscroll-policy");
695   g_object_class_override_property (object_class, ARG_VADJUSTMENT_POLICY, "vscroll-policy");
696 #endif
697
698   clist_signals[SELECT_ROW] =
699                 g_signal_new ("select_row",
700                               G_TYPE_FROM_CLASS (object_class),
701                               G_SIGNAL_RUN_FIRST,
702                               G_STRUCT_OFFSET (GtkCMCListClass, select_row),
703                               NULL, NULL,
704                               claws_marshal_VOID__INT_INT_BOXED,
705                               G_TYPE_NONE, 3,
706                               G_TYPE_INT,
707                               G_TYPE_INT,
708                               GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
709   clist_signals[UNSELECT_ROW] =
710                 g_signal_new ("unselect_row",
711                               G_TYPE_FROM_CLASS (object_class),
712                               G_SIGNAL_RUN_FIRST,
713                               G_STRUCT_OFFSET (GtkCMCListClass, unselect_row),
714                               NULL, NULL,
715                               claws_marshal_VOID__INT_INT_BOXED,
716                               G_TYPE_NONE, 3,
717                               G_TYPE_INT,
718                               G_TYPE_INT,
719                               GDK_TYPE_EVENT);
720   clist_signals[ROW_MOVE] =
721                 g_signal_new ("row_move",
722                               G_TYPE_FROM_CLASS (object_class),
723                               G_SIGNAL_RUN_LAST,
724                               G_STRUCT_OFFSET (GtkCMCListClass, row_move),
725                               NULL, NULL,
726                               claws_marshal_VOID__INT_INT,
727                               G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
728   clist_signals[CLICK_COLUMN] =
729                 g_signal_new ("click_column",
730                               G_TYPE_FROM_CLASS (object_class),
731                               G_SIGNAL_RUN_FIRST,
732                               G_STRUCT_OFFSET (GtkCMCListClass, click_column),
733                               NULL, NULL,
734                               claws_marshal_VOID__INT,
735                               G_TYPE_NONE, 1, G_TYPE_INT);
736   clist_signals[RESIZE_COLUMN] =
737                 g_signal_new ("resize_column",
738                               G_TYPE_FROM_CLASS (object_class),
739                               G_SIGNAL_RUN_LAST,
740                               G_STRUCT_OFFSET (GtkCMCListClass, resize_column),
741                               NULL, NULL,
742                               claws_marshal_VOID__INT_INT,
743                               G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
744
745   clist_signals[TOGGLE_FOCUS_ROW] =
746                 g_signal_new ("toggle_focus_row",
747                               G_TYPE_FROM_CLASS (object_class),
748                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
749                               G_STRUCT_OFFSET (GtkCMCListClass, toggle_focus_row),
750                               NULL, NULL,
751                               claws_marshal_VOID__VOID,
752                               G_TYPE_NONE, 0);
753   clist_signals[SELECT_ALL] =
754                 g_signal_new ("select_all",
755                               G_TYPE_FROM_CLASS (object_class),
756                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
757                               G_STRUCT_OFFSET (GtkCMCListClass, select_all),
758                               NULL, NULL,
759                               claws_marshal_VOID__VOID,
760                               G_TYPE_NONE, 0);
761   clist_signals[UNSELECT_ALL] =
762                 g_signal_new ("unselect_all",
763                               G_TYPE_FROM_CLASS (object_class),
764                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
765                               G_STRUCT_OFFSET (GtkCMCListClass, unselect_all),
766                               NULL, NULL,
767                               claws_marshal_VOID__VOID,
768                               G_TYPE_NONE, 0);
769   clist_signals[UNDO_SELECTION] =
770                 g_signal_new ("undo_selection",
771                               G_TYPE_FROM_CLASS (object_class),
772                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
773                               G_STRUCT_OFFSET (GtkCMCListClass, undo_selection),
774                               NULL, NULL,
775                               claws_marshal_VOID__VOID,
776                               G_TYPE_NONE, 0);
777   clist_signals[START_SELECTION] =
778                 g_signal_new ("start_selection",
779                               G_TYPE_FROM_CLASS (object_class),
780                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
781                               G_STRUCT_OFFSET (GtkCMCListClass, start_selection),
782                               NULL, NULL,
783                               claws_marshal_VOID__VOID,
784                               G_TYPE_NONE, 0);
785   clist_signals[END_SELECTION] =
786                 g_signal_new ("end_selection",
787                               G_TYPE_FROM_CLASS (object_class),
788                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
789                               G_STRUCT_OFFSET (GtkCMCListClass, end_selection),
790                               NULL, NULL,
791                               claws_marshal_VOID__VOID,
792                               G_TYPE_NONE, 0);
793   clist_signals[TOGGLE_ADD_MODE] =
794                 g_signal_new ("toggle_add_mode",
795                               G_TYPE_FROM_CLASS (object_class),
796                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
797                               G_STRUCT_OFFSET (GtkCMCListClass, toggle_add_mode),
798                               NULL, NULL,
799                               claws_marshal_VOID__VOID,
800                               G_TYPE_NONE, 0);
801   clist_signals[EXTEND_SELECTION] =
802                 g_signal_new ("extend_selection",
803                               G_TYPE_FROM_CLASS (object_class),
804                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
805                               G_STRUCT_OFFSET (GtkCMCListClass, extend_selection),
806                               NULL, NULL,
807                               claws_marshal_VOID__ENUM_FLOAT_BOOLEAN,
808                               G_TYPE_NONE, 3, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT, G_TYPE_BOOLEAN);
809   clist_signals[SCROLL_VERTICAL] =
810                 g_signal_new ("scroll_vertical",
811                               G_TYPE_FROM_CLASS (object_class),
812                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
813                               G_STRUCT_OFFSET (GtkCMCListClass, scroll_vertical),
814                               NULL, NULL,
815                               claws_marshal_VOID__ENUM_FLOAT,
816                               G_TYPE_NONE, 2, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT);
817   clist_signals[SCROLL_HORIZONTAL] =
818                 g_signal_new ("scroll_horizontal",
819                               G_TYPE_FROM_CLASS (object_class),
820                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
821                               G_STRUCT_OFFSET (GtkCMCListClass, scroll_horizontal),
822                               NULL, NULL,
823                               claws_marshal_VOID__ENUM_FLOAT,
824                               G_TYPE_NONE, 2, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT);
825   clist_signals[ABORT_COLUMN_RESIZE] =
826                 g_signal_new ("abort_column_resize",
827                               G_TYPE_FROM_CLASS (object_class),
828                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
829                               G_STRUCT_OFFSET (GtkCMCListClass, abort_column_resize),
830                               NULL, NULL,
831                               claws_marshal_VOID__VOID,
832                               G_TYPE_NONE, 0);
833
834   binding_set = gtk_binding_set_by_class (klass);
835   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
836                                 "scroll_vertical", 2,
837                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
838                                 G_TYPE_FLOAT, 0.0);
839   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
840                                 "scroll_vertical", 2,
841                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
842                                 G_TYPE_FLOAT, 0.0);
843   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
844                                 "scroll_vertical", 2,
845                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
846                                 G_TYPE_FLOAT, 0.0);
847   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
848                                 "scroll_vertical", 2,
849                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
850                                 G_TYPE_FLOAT, 0.0);
851   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
852                                 "scroll_vertical", 2,
853                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
854                                 G_TYPE_FLOAT, 0.0);
855   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
856                                 "scroll_vertical", 2,
857                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
858                                 G_TYPE_FLOAT, 0.0);
859   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
860                                 "scroll_vertical", 2,
861                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
862                                 G_TYPE_FLOAT, 0.0);
863   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
864                                 "scroll_vertical", 2,
865                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
866                                 G_TYPE_FLOAT, 0.0);
867   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
868                                 "scroll_vertical", 2,
869                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
870                                 G_TYPE_FLOAT, 0.0);
871   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
872                                 "scroll_vertical", 2,
873                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
874                                 G_TYPE_FLOAT, 0.0);
875   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
876                                 "scroll_vertical", 2,
877                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
878                                 G_TYPE_FLOAT, 1.0);
879   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
880                                 "scroll_vertical", 2,
881                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
882                                 G_TYPE_FLOAT, 1.0);
883   
884   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_SHIFT_MASK,
885                                 "extend_selection", 3,
886                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
887                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
888   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_SHIFT_MASK,
889                                 "extend_selection", 3,
890                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
891                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
892   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_SHIFT_MASK,
893                                 "extend_selection", 3,
894                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
895                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
896   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_SHIFT_MASK,
897                                 "extend_selection", 3,
898                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
899                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
900   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, GDK_SHIFT_MASK,
901                                 "extend_selection", 3,
902                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
903                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
904   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, GDK_SHIFT_MASK,
905                                 "extend_selection", 3,
906                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
907                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
908   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, GDK_SHIFT_MASK,
909                                 "extend_selection", 3,
910                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
911                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
912   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, GDK_SHIFT_MASK,
913                                 "extend_selection", 3,
914                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
915                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
916   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home,
917                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
918                                 "extend_selection", 3,
919                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
920                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
921   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home,
922                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
923                                 "extend_selection", 3,
924                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
925                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
926   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End,
927                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
928                                 "extend_selection", 3,
929                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
930                                 G_TYPE_FLOAT, 1.0, G_TYPE_BOOLEAN, TRUE);
931   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End,
932                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
933                                 "extend_selection", 3,
934                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
935                                 G_TYPE_FLOAT, 1.0, G_TYPE_BOOLEAN, TRUE);
936
937   
938   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0,
939                                 "scroll_horizontal", 2,
940                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
941                                 G_TYPE_FLOAT, 0.0);
942   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0,
943                                 "scroll_horizontal", 2,
944                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
945                                 G_TYPE_FLOAT, 0.0);
946   
947   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0,
948                                 "scroll_horizontal", 2,
949                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
950                                 G_TYPE_FLOAT, 0.0);
951   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0,
952                                 "scroll_horizontal", 2,
953                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
954                                 G_TYPE_FLOAT, 0.0);
955
956   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
957                                 "scroll_horizontal", 2,
958                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
959                                 G_TYPE_FLOAT, 0.0);
960   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
961                                 "scroll_horizontal", 2,
962                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
963                                 G_TYPE_FLOAT, 0.0);
964   
965   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
966                                 "scroll_horizontal", 2,
967                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
968                                 G_TYPE_FLOAT, 1.0);
969
970   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
971                                 "scroll_horizontal", 2,
972                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
973                                 G_TYPE_FLOAT, 1.0);
974   
975   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
976                                 "undo_selection", 0);
977   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
978                                 "abort_column_resize", 0);
979   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
980                                 "toggle_focus_row", 0);
981   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
982                                 "toggle_focus_row", 0);  
983   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
984                                 "toggle_add_mode", 0);
985   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
986                                 "toggle_add_mode", 0);
987   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
988                                 "select_all", 0);
989   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Divide, GDK_CONTROL_MASK,
990                                 "select_all", 0);
991   gtk_binding_entry_add_signal (binding_set, '\\', GDK_CONTROL_MASK,
992                                 "unselect_all", 0);
993   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_L,
994                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK,
995                                 "end_selection", 0);
996   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_R,
997                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK,
998                                 "end_selection", 0);
999   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_L,
1000                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK |
1001                                 GDK_CONTROL_MASK,
1002                                 "end_selection", 0);
1003   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_R,
1004                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK |
1005                                 GDK_CONTROL_MASK,
1006                                 "end_selection", 0);
1007 }
1008
1009 static void
1010 gtk_cmclist_set_arg (GObject *object,
1011                                 guint      arg_id,
1012                                 const GValue *value,
1013                                 GParamSpec *spec)
1014 {
1015   GtkCMCList *clist;
1016
1017   clist = GTK_CMCLIST (object);
1018
1019   switch (arg_id)
1020     {
1021     case ARG_N_COLUMNS: /* only set at construction time */
1022       clist->columns = MAX (1, g_value_get_uint (value));
1023       break;
1024     case ARG_SHADOW_TYPE:
1025       gtk_cmclist_set_shadow_type (clist, g_value_get_enum (value));
1026       break;
1027     case ARG_SELECTION_MODE:
1028       gtk_cmclist_set_selection_mode (clist, g_value_get_enum (value));
1029       break;
1030     case ARG_ROW_HEIGHT:
1031       gtk_cmclist_set_row_height (clist, g_value_get_uint (value));
1032       break;
1033     case ARG_REORDERABLE:
1034       gtk_cmclist_set_reorderable (clist, g_value_get_boolean (value));
1035       break;
1036     case ARG_TITLES_ACTIVE:
1037       if (g_value_get_boolean (value))
1038         gtk_cmclist_column_titles_active (clist);
1039       else
1040         gtk_cmclist_column_titles_passive (clist);
1041       break;
1042     case ARG_USE_DRAG_ICONS:
1043       gtk_cmclist_set_use_drag_icons (clist, g_value_get_boolean (value));
1044       break;
1045     case ARG_SORT_TYPE:
1046       gtk_cmclist_set_sort_type (clist, g_value_get_enum (value));
1047       break;
1048 #if GTK_CHECK_VERSION(3, 0, 0)
1049     case ARG_HADJUSTMENT:
1050       gtk_cmclist_set_hadjustment (clist, g_value_get_object (value));
1051       break;
1052     case ARG_VADJUSTMENT:
1053       gtk_cmclist_set_vadjustment (clist, g_value_get_object (value));
1054       break;
1055     case ARG_HADJUSTMENT_POLICY:
1056     case ARG_VADJUSTMENT_POLICY:
1057       break;
1058 #endif
1059     }
1060 }
1061
1062 static void
1063 gtk_cmclist_get_arg (GObject *object,
1064                                 guint      arg_id,
1065                                 GValue *value,
1066                                 GParamSpec *spec)
1067 {
1068   GtkCMCList *clist;
1069
1070   clist = GTK_CMCLIST (object);
1071
1072   switch (arg_id)
1073     {
1074       guint i;
1075
1076     case ARG_N_COLUMNS:
1077       g_value_set_uint(value, clist->columns);
1078       break;
1079     case ARG_SHADOW_TYPE:
1080       g_value_set_enum(value, clist->shadow_type);
1081       break;
1082     case ARG_SELECTION_MODE:
1083       g_value_set_enum(value, clist->selection_mode);
1084       break;
1085     case ARG_ROW_HEIGHT:
1086       g_value_set_uint(value, GTK_CMCLIST_ROW_HEIGHT_SET(clist) ? clist->row_height : 0);
1087       break;
1088     case ARG_REORDERABLE:
1089       g_value_set_boolean(value, GTK_CMCLIST_REORDERABLE (clist));
1090       break;
1091     case ARG_TITLES_ACTIVE:
1092       g_value_set_boolean(value, TRUE);
1093       for (i = 0; i < clist->columns; i++)
1094         if (clist->column[i].button &&
1095             !gtk_widget_get_sensitive (clist->column[i].button))
1096           {
1097             g_value_set_boolean(value, FALSE);
1098             break;
1099           }
1100       break;
1101     case ARG_USE_DRAG_ICONS:
1102       g_value_set_boolean(value, GTK_CMCLIST_USE_DRAG_ICONS (clist));
1103       break;
1104     case ARG_SORT_TYPE:
1105       g_value_set_enum(value, clist->sort_type);
1106       break;
1107 #if GTK_CHECK_VERSION(3, 0, 0)
1108     case ARG_HADJUSTMENT:
1109       g_value_set_object(value, gtk_cmclist_get_hadjustment(clist));
1110       break;
1111     case ARG_VADJUSTMENT:
1112       g_value_set_object(value, gtk_cmclist_get_vadjustment(clist));
1113       break;
1114     case ARG_HADJUSTMENT_POLICY:
1115     case ARG_VADJUSTMENT_POLICY:
1116       g_value_set_enum(value, GTK_SCROLL_NATURAL);
1117       break;
1118 #endif
1119     default:
1120       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, spec);
1121       break;
1122     }
1123 }
1124
1125 static void
1126 gtk_cmclist_init (GtkCMCList *clist)
1127 {
1128   clist->flags = 0;
1129
1130   gtkut_widget_set_has_window (GTK_WIDGET(clist), TRUE);
1131   gtkut_widget_set_can_focus (GTK_WIDGET(clist), TRUE);
1132   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_DRAW_DRAG_LINE);
1133   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_USE_DRAG_ICONS);
1134
1135
1136 #if !GLIB_CHECK_VERSION(2,10,0)
1137   clist->row_mem_chunk = NULL;
1138   clist->cell_mem_chunk = NULL;
1139 #endif
1140
1141   clist->freeze_count = 0;
1142
1143   clist->rows = 0;
1144   clist->row_height = 0;
1145   clist->row_list = NULL;
1146   clist->row_list_end = NULL;
1147
1148   clist->columns = 0;
1149
1150   clist->title_window = NULL;
1151   clist->column_title_area.x = 0;
1152   clist->column_title_area.y = 0;
1153   clist->column_title_area.width = 1;
1154   clist->column_title_area.height = 1;
1155
1156   clist->clist_window = NULL;
1157   clist->clist_window_width = 1;
1158   clist->clist_window_height = 1;
1159
1160   clist->hoffset = 0;
1161   clist->voffset = 0;
1162
1163   clist->shadow_type = GTK_SHADOW_IN;
1164   clist->vadjustment = NULL;
1165   clist->hadjustment = NULL;
1166
1167   clist->button_actions[0] = GTK_CMBUTTON_SELECTS | GTK_CMBUTTON_DRAGS;
1168   clist->button_actions[1] = GTK_CMBUTTON_IGNORED;
1169   clist->button_actions[2] = GTK_CMBUTTON_IGNORED;
1170   clist->button_actions[3] = GTK_CMBUTTON_IGNORED;
1171   clist->button_actions[4] = GTK_CMBUTTON_IGNORED;
1172
1173   clist->cursor_drag = NULL;
1174   clist->x_drag = 0;
1175
1176   clist->selection_mode = GTK_SELECTION_SINGLE;
1177   clist->selection = NULL;
1178   clist->selection_end = NULL;
1179   clist->undo_selection = NULL;
1180   clist->undo_unselection = NULL;
1181
1182   clist->focus_row = -1;
1183   clist->focus_header_column = -1;
1184   clist->undo_anchor = -1;
1185
1186   clist->anchor = -1;
1187   clist->anchor_state = GTK_STATE_SELECTED;
1188   clist->drag_pos = -1;
1189   clist->htimer = 0;
1190   clist->vtimer = 0;
1191
1192   clist->click_cell.row = -1;
1193   clist->click_cell.column = -1;
1194
1195   clist->compare = default_compare;
1196   clist->sort_type = GTK_SORT_ASCENDING;
1197   clist->sort_column = 0;
1198
1199   clist->drag_highlight_row = -1;
1200 }
1201
1202 /* Constructor */
1203 static GObject*
1204 gtk_cmclist_constructor (GType                  type,
1205                        guint                  n_construct_properties,
1206                        GObjectConstructParam *construct_properties)
1207 {
1208   GObject *object = G_OBJECT_CLASS (gtk_cmclist_parent_class)->constructor (type,
1209                                                                 n_construct_properties,
1210                                                                 construct_properties);
1211   GtkCMCList *clist = GTK_CMCLIST (object);
1212   
1213 #if !GLIB_CHECK_VERSION(2,10,0)
1214   if (!clist->row_mem_chunk)
1215     clist->row_mem_chunk = g_mem_chunk_new ("clist row mem chunk",
1216                                             sizeof (GtkCMCListRow),
1217                                             sizeof (GtkCMCListRow) *
1218                                             CMCLIST_OPTIMUM_SIZE, 
1219                                             G_ALLOC_AND_FREE);
1220   
1221   if (!clist->cell_mem_chunk)
1222     clist->cell_mem_chunk = g_mem_chunk_new ("clist cell mem chunk",
1223                                              sizeof (GtkCMCell) * clist->columns,
1224                                              sizeof (GtkCMCell) * clist->columns *
1225                                              CMCLIST_OPTIMUM_SIZE, 
1226                                              G_ALLOC_AND_FREE);
1227 #endif
1228
1229   /* allocate memory for columns */
1230   clist->column = columns_new (clist);
1231   
1232   /* there needs to be at least one column button 
1233    * because there is alot of code that will break if it
1234    * isn't there
1235    */
1236   column_button_create (clist, 0);
1237   
1238   return object;
1239 }
1240
1241 /* GTKCLIST PUBLIC INTERFACE
1242  *   gtk_cmclist_new
1243  *   gtk_cmclist_new_with_titles
1244  *   gtk_cmclist_set_hadjustment
1245  *   gtk_cmclist_set_vadjustment
1246  *   gtk_cmclist_get_hadjustment
1247  *   gtk_cmclist_get_vadjustment
1248  *   gtk_cmclist_set_shadow_type
1249  *   gtk_cmclist_set_selection_mode
1250  *   gtk_cmclist_freeze
1251  *   gtk_cmclist_thaw
1252  */
1253 GtkWidget*
1254 gtk_cmclist_new (gint columns)
1255 {
1256   return gtk_cmclist_new_with_titles (columns, NULL);
1257 }
1258  
1259 GtkWidget*
1260 gtk_cmclist_new_with_titles (gint   columns,
1261                            gchar *titles[])
1262 {
1263   GtkCMCList *clist;
1264
1265   clist = g_object_new (GTK_TYPE_CMCLIST,
1266                         "n_columns", columns,
1267                         NULL);
1268   if (titles)
1269     {
1270       guint i;
1271
1272       for (i = 0; i < clist->columns; i++)
1273         gtk_cmclist_set_column_title (clist, i, titles[i]);
1274       gtk_cmclist_column_titles_show (clist);
1275     }
1276   else
1277     gtk_cmclist_column_titles_hide (clist);
1278
1279   return GTK_WIDGET (clist);
1280 }
1281
1282 void
1283 gtk_cmclist_set_hadjustment (GtkCMCList      *clist,
1284                            GtkAdjustment *adjustment)
1285 {
1286   GtkAdjustment *old_adjustment;
1287
1288   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1289   if (adjustment)
1290     cm_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1291   
1292   if (clist->hadjustment == adjustment)
1293     return;
1294   
1295   old_adjustment = clist->hadjustment;
1296
1297   if (clist->hadjustment)
1298     {
1299       g_signal_handlers_disconnect_matched(G_OBJECT (clist->hadjustment), G_SIGNAL_MATCH_DATA,
1300                         0, 0, 0, 0, clist);
1301
1302       g_object_unref (G_OBJECT (clist->hadjustment));
1303     }
1304
1305   clist->hadjustment = adjustment;
1306
1307   if (clist->hadjustment)
1308     {
1309 #if GLIB_CHECK_VERSION(2,10,0)
1310       g_object_ref_sink (clist->hadjustment);
1311 #else
1312       gtk_object_ref (G_OBJECT (clist->hadjustment));
1313       gtk_object_sink (G_OBJECT (clist->hadjustment));
1314 #endif
1315       g_signal_connect (G_OBJECT (clist->hadjustment), "value_changed",
1316                           G_CALLBACK( hadjustment_value_changed),
1317                           (gpointer) clist);
1318     }
1319
1320   if (!clist->hadjustment || !old_adjustment)
1321     gtk_widget_queue_resize (GTK_WIDGET (clist));
1322 }
1323
1324 GtkAdjustment *
1325 gtk_cmclist_get_hadjustment (GtkCMCList *clist)
1326 {
1327   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1328
1329   return clist->hadjustment;
1330 }
1331
1332 void
1333 gtk_cmclist_set_vadjustment (GtkCMCList      *clist,
1334                            GtkAdjustment *adjustment)
1335 {
1336   GtkAdjustment *old_adjustment;
1337
1338   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1339   if (adjustment)
1340     cm_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1341
1342   if (clist->vadjustment == adjustment)
1343     return;
1344   
1345   old_adjustment = clist->vadjustment;
1346
1347   if (clist->vadjustment)
1348     {
1349       g_signal_handlers_disconnect_matched(G_OBJECT (clist->vadjustment), G_SIGNAL_MATCH_DATA,
1350                         0, 0, 0, 0, clist);
1351       g_object_unref (G_OBJECT (clist->vadjustment));
1352     }
1353
1354   clist->vadjustment = adjustment;
1355
1356   if (clist->vadjustment)
1357     {
1358 #if GLIB_CHECK_VERSION(2,10,0)
1359       g_object_ref_sink (clist->vadjustment);
1360 #else
1361       gtk_object_ref (G_OBJECT (clist->vadjustment));
1362       gtk_object_sink (G_OBJECT (clist->vadjustment));
1363 #endif
1364
1365       g_signal_connect (G_OBJECT (clist->vadjustment), "value_changed",
1366                           G_CALLBACK(vadjustment_value_changed),
1367                           (gpointer) clist);
1368     }
1369
1370   if (!clist->vadjustment || !old_adjustment)
1371     gtk_widget_queue_resize (GTK_WIDGET (clist));
1372 }
1373
1374 GtkAdjustment *
1375 gtk_cmclist_get_vadjustment (GtkCMCList *clist)
1376 {
1377   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1378
1379   return clist->vadjustment;
1380 }
1381
1382 #if !GTK_CHECK_VERSION(3, 0, 0)
1383 static void
1384 gtk_cmclist_set_scroll_adjustments (GtkCMCList      *clist,
1385                                   GtkAdjustment *hadjustment,
1386                                   GtkAdjustment *vadjustment)
1387 {
1388   if (clist->hadjustment != hadjustment)
1389     gtk_cmclist_set_hadjustment (clist, hadjustment);
1390   if (clist->vadjustment != vadjustment)
1391     gtk_cmclist_set_vadjustment (clist, vadjustment);
1392 }
1393 #endif
1394
1395 void
1396 gtk_cmclist_set_shadow_type (GtkCMCList      *clist,
1397                            GtkShadowType  type)
1398 {
1399   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1400
1401   clist->shadow_type = type;
1402
1403   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1404     gtk_widget_queue_resize (GTK_WIDGET (clist));
1405 }
1406
1407 void
1408 gtk_cmclist_set_selection_mode (GtkCMCList         *clist,
1409                               GtkSelectionMode  mode)
1410 {
1411   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1412   cm_return_if_fail (mode != GTK_SELECTION_NONE);
1413
1414   if (mode == clist->selection_mode)
1415     return;
1416
1417   clist->selection_mode = mode;
1418   clist->anchor = -1;
1419   clist->anchor_state = GTK_STATE_SELECTED;
1420   clist->drag_pos = -1;
1421   clist->undo_anchor = clist->focus_row;
1422
1423   g_list_free (clist->undo_selection);
1424   g_list_free (clist->undo_unselection);
1425   clist->undo_selection = NULL;
1426   clist->undo_unselection = NULL;
1427
1428   switch (mode)
1429     {
1430     case GTK_SELECTION_MULTIPLE:
1431       return;
1432     case GTK_SELECTION_BROWSE:
1433     case GTK_SELECTION_SINGLE:
1434       gtk_cmclist_unselect_all (clist);
1435       break;
1436     default:
1437       /* Someone set it by hand */
1438       g_assert_not_reached ();
1439     }
1440 }
1441
1442 void
1443 gtk_cmclist_freeze (GtkCMCList *clist)
1444 {
1445   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1446
1447   clist->freeze_count++;
1448 }
1449
1450 void
1451 gtk_cmclist_thaw (GtkCMCList *clist)
1452 {
1453   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1454
1455   if (clist->freeze_count)
1456     {
1457       clist->freeze_count--;
1458       CLIST_REFRESH (clist);
1459     }
1460 }
1461
1462 /* PUBLIC COLUMN FUNCTIONS
1463  *   gtk_cmclist_column_titles_show
1464  *   gtk_cmclist_column_titles_hide
1465  *   gtk_cmclist_column_title_active
1466  *   gtk_cmclist_column_title_passive
1467  *   gtk_cmclist_column_titles_active
1468  *   gtk_cmclist_column_titles_passive
1469  *   gtk_cmclist_set_column_title
1470  *   gtk_cmclist_get_column_title
1471  *   gtk_cmclist_set_column_widget
1472  *   gtk_cmclist_set_column_justification
1473  *   gtk_cmclist_set_column_visibility
1474  *   gtk_cmclist_set_column_resizeable
1475  *   gtk_cmclist_set_column_auto_resize
1476  *   gtk_cmclist_optimal_column_width
1477  *   gtk_cmclist_set_column_width
1478  *   gtk_cmclist_set_column_min_width
1479  *   gtk_cmclist_set_column_max_width
1480  */
1481 void
1482 gtk_cmclist_column_titles_show (GtkCMCList *clist)
1483 {
1484   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1485
1486   if (!GTK_CMCLIST_SHOW_TITLES(clist))
1487     {
1488       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_SHOW_TITLES);
1489       if (clist->title_window)
1490         gdk_window_show (clist->title_window);
1491       gtk_widget_queue_resize (GTK_WIDGET (clist));
1492     }
1493 }
1494
1495 void 
1496 gtk_cmclist_column_titles_hide (GtkCMCList *clist)
1497 {
1498   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1499
1500   if (GTK_CMCLIST_SHOW_TITLES(clist))
1501     {
1502       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_SHOW_TITLES);
1503       if (clist->title_window)
1504         gdk_window_hide (clist->title_window);
1505       gtk_widget_queue_resize (GTK_WIDGET (clist));
1506     }
1507 }
1508
1509 void
1510 gtk_cmclist_column_title_active (GtkCMCList *clist,
1511                                gint      column)
1512 {
1513   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1514
1515   if (column < 0 || column >= clist->columns)
1516     return;
1517   if (!clist->column[column].button || !clist->column[column].button_passive)
1518     return;
1519
1520   clist->column[column].button_passive = FALSE;
1521
1522   g_signal_handlers_disconnect_matched(G_OBJECT (clist->column[column].button), G_SIGNAL_MATCH_FUNC,
1523                     0, 0, 0, column_title_passive_func, 0);
1524
1525   gtkut_widget_set_can_focus (clist->column[column].button, TRUE);
1526   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1527     gtk_widget_queue_draw (clist->column[column].button);
1528 }
1529
1530 void
1531 gtk_cmclist_column_title_passive (GtkCMCList *clist,
1532                                 gint      column)
1533 {
1534   GtkToggleButton *button;
1535
1536   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1537
1538   if (column < 0 || column >= clist->columns)
1539     return;
1540   if (!clist->column[column].button || clist->column[column].button_passive)
1541     return;
1542
1543   button = GTK_TOGGLE_BUTTON (clist->column[column].button);
1544
1545   clist->column[column].button_passive = TRUE;
1546
1547   if (gtk_toggle_button_get_active(button))
1548         g_signal_connect(G_OBJECT (clist->column[column].button),
1549                          "button-release-event",
1550                          G_CALLBACK(column_title_passive_func),
1551                          NULL);
1552   if (gtk_widget_is_focus(gtk_bin_get_child(GTK_BIN(button))))
1553         g_signal_connect(G_OBJECT (clist->column[column].button),
1554                          "leave-notify-event",
1555                          G_CALLBACK(column_title_passive_func),
1556                          NULL);
1557
1558   g_signal_connect (G_OBJECT (clist->column[column].button), "event",
1559                       G_CALLBACK(column_title_passive_func), NULL);
1560
1561   gtkut_widget_set_can_focus (clist->column[column].button, FALSE);
1562   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1563     gtk_widget_queue_draw (clist->column[column].button);
1564 }
1565
1566 void
1567 gtk_cmclist_column_titles_active (GtkCMCList *clist)
1568 {
1569   gint i;
1570
1571   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1572
1573   for (i = 0; i < clist->columns; i++)
1574     gtk_cmclist_column_title_active (clist, i);
1575 }
1576
1577 void
1578 gtk_cmclist_column_titles_passive (GtkCMCList *clist)
1579 {
1580   gint i;
1581
1582   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1583
1584   for (i = 0; i < clist->columns; i++)
1585     gtk_cmclist_column_title_passive (clist, i);
1586 }
1587
1588 void
1589 gtk_cmclist_set_column_title (GtkCMCList    *clist,
1590                             gint         column,
1591                             const gchar *title)
1592 {
1593   gint new_button = 0;
1594   GtkWidget *old_widget;
1595   GtkWidget *alignment = NULL;
1596   GtkWidget *label;
1597
1598   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1599
1600   if (column < 0 || column >= clist->columns)
1601     return;
1602
1603   /* if the column button doesn't currently exist,
1604    * it has to be created first */
1605   if (!clist->column[column].button)
1606     {
1607       column_button_create (clist, column);
1608       new_button = 1;
1609     }
1610
1611   column_title_new (clist, column, title);
1612
1613   /* remove and destroy the old widget */
1614   old_widget = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1615   if (old_widget)
1616     gtk_container_remove (GTK_CONTAINER (clist->column[column].button), old_widget);
1617
1618   /* create new alignment based no column justification */
1619   switch (clist->column[column].justification)
1620     {
1621     case GTK_JUSTIFY_LEFT:
1622       alignment = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
1623       break;
1624
1625     case GTK_JUSTIFY_RIGHT:
1626       alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
1627       break;
1628
1629     case GTK_JUSTIFY_CENTER:
1630       alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1631       break;
1632
1633     case GTK_JUSTIFY_FILL:
1634       alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1635       break;
1636     }
1637
1638   gtk_widget_push_composite_child ();
1639   label = gtk_label_new (clist->column[column].title);
1640   gtk_widget_pop_composite_child ();
1641   gtk_container_add (GTK_CONTAINER (alignment), label);
1642   gtk_container_add (GTK_CONTAINER (clist->column[column].button), alignment);
1643   gtk_widget_show (label);
1644   gtk_widget_show (alignment);
1645
1646   /* if this button didn't previously exist, then the
1647    * column button positions have to be re-computed */
1648   if (gtk_widget_get_visible (GTK_WIDGET(clist)) && new_button)
1649     size_allocate_title_buttons (clist);
1650 }
1651
1652 gchar *
1653 gtk_cmclist_get_column_title (GtkCMCList *clist,
1654                             gint      column)
1655 {
1656   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1657
1658   if (column < 0 || column >= clist->columns)
1659     return NULL;
1660
1661   return clist->column[column].title;
1662 }
1663
1664 void
1665 gtk_cmclist_set_column_widget (GtkCMCList  *clist,
1666                              gint       column,
1667                              GtkWidget *widget)
1668 {
1669   gint new_button = 0;
1670   GtkWidget *old_widget;
1671
1672   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1673
1674   if (column < 0 || column >= clist->columns)
1675     return;
1676
1677   /* if the column button doesn't currently exist,
1678    * it has to be created first */
1679   if (!clist->column[column].button)
1680     {
1681       column_button_create (clist, column);
1682       new_button = 1;
1683     }
1684
1685   column_title_new (clist, column, NULL);
1686
1687   /* remove and destroy the old widget */
1688   old_widget = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1689   if (old_widget)
1690     gtk_container_remove (GTK_CONTAINER (clist->column[column].button),
1691                           old_widget);
1692
1693   /* add and show the widget */
1694   if (widget)
1695     {
1696       gtk_container_add (GTK_CONTAINER (clist->column[column].button), widget);
1697       gtk_widget_show (widget);
1698     }
1699
1700   /* if this button didn't previously exist, then the
1701    * column button positions have to be re-computed */
1702   if (gtk_widget_get_visible (GTK_WIDGET(clist)) && new_button)
1703     size_allocate_title_buttons (clist);
1704 }
1705
1706 GtkWidget *
1707 gtk_cmclist_get_column_widget (GtkCMCList *clist,
1708                              gint      column)
1709 {
1710   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1711
1712   if (column < 0 || column >= clist->columns)
1713     return NULL;
1714
1715   if (clist->column[column].button)
1716         return gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1717
1718   return NULL;
1719 }
1720
1721 void
1722 gtk_cmclist_set_column_justification (GtkCMCList         *clist,
1723                                     gint              column,
1724                                     GtkJustification  justification)
1725 {
1726   GtkWidget *alignment;
1727
1728   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1729
1730   if (column < 0 || column >= clist->columns)
1731     return;
1732
1733   clist->column[column].justification = justification;
1734
1735   /* change the alinment of the button title if it's not a
1736    * custom widget */
1737   if (clist->column[column].title)
1738     {
1739       alignment = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1740
1741       switch (clist->column[column].justification)
1742         {
1743         case GTK_JUSTIFY_LEFT:
1744           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.0, 0.5, 0.0, 0.0);
1745           break;
1746
1747         case GTK_JUSTIFY_RIGHT:
1748           gtk_alignment_set (GTK_ALIGNMENT (alignment), 1.0, 0.5, 0.0, 0.0);
1749           break;
1750
1751         case GTK_JUSTIFY_CENTER:
1752           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1753           break;
1754
1755         case GTK_JUSTIFY_FILL:
1756           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1757           break;
1758
1759         default:
1760           break;
1761         }
1762     }
1763
1764   if (CLIST_UNFROZEN (clist))
1765     draw_rows (clist, NULL);
1766 }
1767
1768 void
1769 gtk_cmclist_set_column_visibility (GtkCMCList *clist,
1770                                  gint      column,
1771                                  gboolean  visible)
1772 {
1773   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1774
1775   if (column < 0 || column >= clist->columns)
1776     return;
1777   if (clist->column[column].visible == visible)
1778     return;
1779
1780   /* don't hide last visible column */
1781   if (!visible)
1782     {
1783       gint i;
1784       gint vis_columns = 0;
1785
1786       for (i = 0, vis_columns = 0; i < clist->columns && vis_columns < 2; i++)
1787         if (clist->column[i].visible)
1788           vis_columns++;
1789
1790       if (vis_columns < 2)
1791         return;
1792     }
1793
1794   clist->column[column].visible = visible;
1795
1796   if (clist->column[column].button)
1797     {
1798       if (visible)
1799         gtk_widget_show (clist->column[column].button);
1800       else
1801         gtk_widget_hide (clist->column[column].button);
1802     }
1803   
1804   gtk_widget_queue_resize (GTK_WIDGET(clist));
1805 }
1806
1807 void
1808 gtk_cmclist_set_column_resizeable (GtkCMCList *clist,
1809                                  gint      column,
1810                                  gboolean  resizeable)
1811 {
1812   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1813
1814   if (column < 0 || column >= clist->columns)
1815     return;
1816   if (clist->column[column].resizeable == resizeable)
1817     return;
1818
1819   clist->column[column].resizeable = resizeable;
1820   if (resizeable)
1821     clist->column[column].auto_resize = FALSE;
1822
1823   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1824     size_allocate_title_buttons (clist);
1825 }
1826
1827 void
1828 gtk_cmclist_set_column_auto_resize (GtkCMCList *clist,
1829                                   gint      column,
1830                                   gboolean  auto_resize)
1831 {
1832   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1833
1834   if (column < 0 || column >= clist->columns)
1835     return;
1836   if (clist->column[column].auto_resize == auto_resize)
1837     return;
1838
1839   clist->column[column].auto_resize = auto_resize;
1840   if (auto_resize)
1841     {
1842       clist->column[column].resizeable = FALSE;
1843       if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1844         {
1845           gint width;
1846
1847           width = gtk_cmclist_optimal_column_width (clist, column);
1848           gtk_cmclist_set_column_width (clist, column, width);
1849         }
1850     }
1851
1852   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1853     size_allocate_title_buttons (clist);
1854 }
1855
1856 gint
1857 gtk_cmclist_columns_autosize (GtkCMCList *clist)
1858 {
1859   gint i;
1860   gint width;
1861
1862   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
1863
1864   gtk_cmclist_freeze (clist);
1865   width = 0;
1866   for (i = 0; i < clist->columns; i++)
1867     {
1868       gtk_cmclist_set_column_width (clist, i,
1869                                   gtk_cmclist_optimal_column_width (clist, i));
1870
1871       width += clist->column[i].width;
1872     }
1873
1874   gtk_cmclist_thaw (clist);
1875   return width;
1876 }
1877
1878 gint
1879 gtk_cmclist_optimal_column_width (GtkCMCList *clist,
1880                                 gint      column)
1881 {
1882   GtkRequisition requisition;
1883   GList *list;
1884   gint width;
1885
1886   cm_return_val_if_fail (GTK_CMCLIST (clist), 0);
1887
1888   if (column < 0 || column >= clist->columns)
1889     return 0;
1890
1891   if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
1892     {
1893       gtk_widget_get_requisition (clist->column[column].button, &requisition);
1894       width = requisition.width
1895 #if 0
1896              (CELL_SPACING + (2 * COLUMN_INSET)))
1897 #endif
1898                 ;
1899     }
1900   else
1901     width = 0;
1902
1903   for (list = clist->row_list; list; list = list->next)
1904     {
1905   GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1906         (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1907       width = MAX (width, requisition.width);
1908     }
1909
1910   return width;
1911 }
1912
1913 void
1914 gtk_cmclist_set_column_width (GtkCMCList *clist,
1915                             gint      column,
1916                             gint      width)
1917 {
1918   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1919
1920   if (column < 0 || column >= clist->columns)
1921     return;
1922
1923   g_signal_emit (G_OBJECT (clist), clist_signals[RESIZE_COLUMN], 0,
1924                    column, width);
1925 }
1926
1927 void
1928 gtk_cmclist_set_column_min_width (GtkCMCList *clist,
1929                                 gint      column,
1930                                 gint      min_width)
1931 {
1932   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1933
1934   if (column < 0 || column >= clist->columns)
1935     return;
1936   if (clist->column[column].min_width == min_width)
1937     return;
1938
1939   if (clist->column[column].max_width >= 0  &&
1940       clist->column[column].max_width < min_width)
1941     clist->column[column].min_width = clist->column[column].max_width;
1942   else
1943     clist->column[column].min_width = min_width;
1944
1945   if (clist->column[column].area.width < clist->column[column].min_width)
1946     gtk_cmclist_set_column_width (clist, column,clist->column[column].min_width);
1947 }
1948
1949 void
1950 gtk_cmclist_set_column_max_width (GtkCMCList *clist,
1951                                 gint      column,
1952                                 gint      max_width)
1953 {
1954   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1955
1956   if (column < 0 || column >= clist->columns)
1957     return;
1958   if (clist->column[column].max_width == max_width)
1959     return;
1960
1961   if (clist->column[column].min_width >= 0 && max_width >= 0 &&
1962       clist->column[column].min_width > max_width)
1963     clist->column[column].max_width = clist->column[column].min_width;
1964   else
1965     clist->column[column].max_width = max_width;
1966   
1967   if (clist->column[column].area.width > clist->column[column].max_width)
1968     gtk_cmclist_set_column_width (clist, column,clist->column[column].max_width);
1969 }
1970
1971 /* PRIVATE COLUMN FUNCTIONS
1972  *   column_auto_resize
1973  *   real_resize_column
1974  *   abort_column_resize
1975  *   size_allocate_title_buttons
1976  *   size_allocate_columns
1977  *   list_requisition_width
1978  *   new_column_width
1979  *   column_button_create
1980  *   column_button_clicked
1981  *   column_title_passive_func
1982  */
1983 static void
1984 column_auto_resize (GtkCMCList    *clist,
1985                     GtkCMCListRow *clist_row,
1986                     gint         column,
1987                     gint         old_width)
1988 {
1989   /* resize column if needed for auto_resize */
1990   GtkRequisition requisition;
1991
1992   if (!clist->column[column].auto_resize ||
1993       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1994     return;
1995
1996   if (clist_row)
1997     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
1998                                                    column, &requisition);
1999   else
2000     requisition.width = 0;
2001
2002   if (requisition.width > clist->column[column].width)
2003     gtk_cmclist_set_column_width (clist, column, requisition.width);
2004   else if (requisition.width < old_width &&
2005            old_width == clist->column[column].width)
2006     {
2007       GList *list;
2008       gint new_width = 0;
2009
2010       /* run a "gtk_cmclist_optimal_column_width" but break, if
2011        * the column doesn't shrink */
2012       if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
2013     {
2014         gtk_widget_get_requisition (clist->column[column].button, &requisition);
2015         new_width = (requisition.width -
2016                      (CELL_SPACING + (2 * COLUMN_INSET)));
2017     }
2018       else
2019         new_width = 0;
2020
2021       for (list = clist->row_list; list; list = list->next)
2022         {
2023           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
2024             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
2025           new_width = MAX (new_width, requisition.width);
2026           if (new_width == clist->column[column].width)
2027             break;
2028         }
2029       if (new_width < clist->column[column].width)
2030         gtk_cmclist_set_column_width
2031           (clist, column, MAX (new_width, clist->column[column].min_width));
2032     }
2033 }
2034
2035 static void
2036 real_resize_column (GtkCMCList *clist,
2037                     gint      column,
2038                     gint      width)
2039 {
2040   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2041
2042   if (column < 0 || column >= clist->columns)
2043     return;
2044   
2045   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
2046     width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
2047   if (clist->column[column].max_width >= 0 &&
2048       width > clist->column[column].max_width)
2049     width = clist->column[column].max_width;
2050
2051   clist->column[column].width = width;
2052   clist->column[column].width_set = TRUE;
2053
2054   /* FIXME: this is quite expensive to do if the widget hasn't
2055    *        been size_allocated yet, and pointless. Should
2056    *        a flag be kept
2057    */
2058   size_allocate_columns (clist, TRUE);
2059   size_allocate_title_buttons (clist);
2060
2061   CLIST_REFRESH (clist);
2062 }
2063
2064 static void
2065 abort_column_resize (GtkCMCList *clist)
2066 {
2067   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2068
2069   if (!GTK_CMCLIST_IN_DRAG(clist))
2070     return;
2071
2072   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_IN_DRAG);
2073   gtk_grab_remove (GTK_WIDGET (clist));
2074   gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (clist)),
2075                               GDK_CURRENT_TIME);
2076   clist->drag_pos = -1;
2077
2078   if (clist->x_drag >= 0 && clist->x_drag <= clist->clist_window_width - 1)
2079     clist_refresh(clist);
2080 }
2081
2082 static void
2083 size_allocate_title_buttons (GtkCMCList *clist)
2084 {
2085   GtkAllocation button_allocation;
2086   gint last_column;
2087   gint last_button = 0;
2088   gint i;
2089
2090   if (!gtk_widget_get_realized (GTK_WIDGET(clist)))
2091     return;
2092
2093   button_allocation.x = clist->hoffset;
2094   button_allocation.y = 0;
2095   button_allocation.width = 0;
2096   button_allocation.height = clist->column_title_area.height;
2097
2098   /* find last visible column */
2099   for (last_column = clist->columns - 1; last_column >= 0; last_column--)
2100     if (clist->column[last_column].visible)
2101       break;
2102
2103   for (i = 0; i < last_column; i++)
2104     {
2105       if (!clist->column[i].visible)
2106         {
2107           last_button = i + 1;
2108           gdk_window_hide (clist->column[i].window);
2109           continue;
2110         }
2111
2112       button_allocation.width += (clist->column[i].area.width +
2113                                   CELL_SPACING + 2 * COLUMN_INSET);
2114
2115       if (!clist->column[i + 1].button)
2116         {
2117           gdk_window_hide (clist->column[i].window);
2118           continue;
2119         }
2120
2121       gtk_widget_size_allocate (clist->column[last_button].button,
2122                                 &button_allocation);
2123       button_allocation.x += button_allocation.width;
2124       button_allocation.width = 0;
2125
2126       if (clist->column[last_button].resizeable)
2127         {
2128           gdk_window_show (clist->column[last_button].window);
2129           gdk_window_move_resize (clist->column[last_button].window,
2130                                   button_allocation.x - (DRAG_WIDTH / 2), 
2131                                   0, DRAG_WIDTH,
2132                                   clist->column_title_area.height);
2133         }
2134       else
2135         gdk_window_hide (clist->column[last_button].window);
2136
2137       last_button = i + 1;
2138     }
2139
2140   button_allocation.width += (clist->column[last_column].area.width +
2141                               2 * (CELL_SPACING + COLUMN_INSET));
2142   gtk_widget_size_allocate (clist->column[last_button].button,
2143                             &button_allocation);
2144
2145   if (clist->column[last_button].resizeable)
2146     {
2147       button_allocation.x += button_allocation.width;
2148
2149       gdk_window_show (clist->column[last_button].window);
2150       gdk_window_move_resize (clist->column[last_button].window,
2151                               button_allocation.x - (DRAG_WIDTH / 2), 
2152                               0, DRAG_WIDTH, clist->column_title_area.height);
2153     }
2154   else
2155     gdk_window_hide (clist->column[last_button].window);
2156 }
2157
2158 static void
2159 size_allocate_columns (GtkCMCList *clist,
2160                        gboolean  block_resize)
2161 {
2162   GtkRequisition requisition;
2163   gint xoffset = CELL_SPACING + COLUMN_INSET;
2164   gint last_column;
2165   gint i;
2166
2167   /* find last visible column and calculate correct column width */
2168   for (last_column = clist->columns - 1;
2169        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2170
2171   if (last_column < 0)
2172     return;
2173
2174   for (i = 0; i <= last_column; i++)
2175     {
2176       if (!clist->column[i].visible)
2177         continue;
2178       clist->column[i].area.x = xoffset;
2179       if (clist->column[i].width_set)
2180         {
2181           if (!block_resize && GTK_CMCLIST_SHOW_TITLES(clist) &&
2182               clist->column[i].auto_resize && clist->column[i].button)
2183             {
2184               gint width;
2185
2186               gtk_widget_get_requisition (clist->column[i].button, &requisition);
2187               width = (requisition.width -
2188                        (CELL_SPACING + (2 * COLUMN_INSET)));
2189
2190               if (width > clist->column[i].width)
2191                 gtk_cmclist_set_column_width (clist, i, width);
2192             }
2193
2194           clist->column[i].area.width = clist->column[i].width;
2195           xoffset += clist->column[i].width + CELL_SPACING + (2* COLUMN_INSET);
2196         }
2197       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2198         {
2199           gtk_widget_get_requisition (clist->column[i].button, &requisition);
2200           clist->column[i].area.width =
2201             requisition.width -
2202             (CELL_SPACING + (2 * COLUMN_INSET));
2203           xoffset += requisition.width;
2204         }
2205     }
2206
2207   clist->column[last_column].area.width = clist->column[last_column].area.width
2208     + MAX (0, clist->clist_window_width + COLUMN_INSET - xoffset);
2209 }
2210
2211 static gint
2212 list_requisition_width (GtkCMCList *clist) 
2213 {
2214   GtkRequisition requisition;
2215   gint width = CELL_SPACING;
2216   gint i;
2217
2218   for (i = clist->columns - 1; i >= 0; i--)
2219     {
2220       if (!clist->column[i].visible)
2221         continue;
2222
2223       if (clist->column[i].width_set)
2224         width += clist->column[i].width + CELL_SPACING + (2 * COLUMN_INSET);
2225       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2226     {
2227         gtk_widget_get_requisition (clist->column[i].button, &requisition);
2228         width += requisition.width;
2229     }
2230     }
2231
2232   return width;
2233 }
2234
2235 /* this function returns the new width of the column being resized given
2236  * the column and x position of the cursor; the x cursor position is passed
2237  * in as a pointer and automagicly corrected if it's beyond min/max limits */
2238 static gint
2239 new_column_width (GtkCMCList *clist,
2240                   gint      column,
2241                   gint     *x)
2242 {
2243   gint xthickness = gtk_widget_get_style (GTK_WIDGET (clist))->xthickness;
2244   gint width;
2245   gint cx;
2246   gint dx;
2247   gint last_column;
2248
2249   /* first translate the x position from widget->window
2250    * to clist->clist_window */
2251   cx = *x - xthickness;
2252
2253   for (last_column = clist->columns - 1;
2254        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2255
2256   /* calculate new column width making sure it doesn't end up
2257    * less than the minimum width */
2258   dx = (COLUMN_LEFT_XPIXEL (clist, column) + COLUMN_INSET +
2259         (column < last_column) * CELL_SPACING);
2260   width = cx - dx;
2261
2262   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
2263     {
2264       width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
2265       cx = dx + width;
2266       *x = cx + xthickness;
2267     }
2268   else if (clist->column[column].max_width >= COLUMN_MIN_WIDTH &&
2269            width > clist->column[column].max_width)
2270     {
2271       width = clist->column[column].max_width;
2272       cx = dx + clist->column[column].max_width;
2273       *x = cx + xthickness;
2274     }      
2275
2276   if (cx < 0 || cx > clist->clist_window_width)
2277     *x = -1;
2278
2279   return width;
2280 }
2281
2282 static void
2283 column_button_create (GtkCMCList *clist,
2284                       gint      column)
2285 {
2286   GtkWidget *button;
2287
2288   gtk_widget_push_composite_child ();
2289   button = clist->column[column].button = gtk_button_new ();
2290   GtkRcStyle *style = gtk_rc_style_new();
2291   style->ythickness = 0;
2292   gtk_widget_modify_style(clist->column[column].button, style);
2293   g_object_unref(style);
2294   gtk_container_set_border_width(GTK_CONTAINER(button), 0);
2295   gtk_widget_pop_composite_child ();
2296
2297   if (gtk_widget_get_realized (GTK_WIDGET(clist)) && clist->title_window)
2298     gtk_widget_set_parent_window (clist->column[column].button,
2299                                   clist->title_window);
2300   gtk_widget_set_parent (button, GTK_WIDGET (clist));
2301
2302   g_signal_connect (G_OBJECT (button), "clicked",
2303                       G_CALLBACK(column_button_clicked),
2304                       (gpointer) clist);
2305   gtk_widget_show (button);
2306 }
2307
2308 static void
2309 column_button_clicked (GtkWidget *widget,
2310                        gpointer   data)
2311 {
2312   gint i;
2313   GtkCMCList *clist;
2314
2315   cm_return_if_fail (widget != NULL);
2316   cm_return_if_fail (GTK_IS_CMCLIST (data));
2317
2318   clist = GTK_CMCLIST (data);
2319
2320   /* find the column who's button was pressed */
2321   for (i = 0; i < clist->columns; i++)
2322     if (clist->column[i].button == widget)
2323       break;
2324
2325   g_signal_emit (G_OBJECT (clist), clist_signals[CLICK_COLUMN], 0, i);
2326 }
2327
2328 static gint
2329 column_title_passive_func (GtkWidget *widget, 
2330                            GdkEvent  *event,
2331                            gpointer   data)
2332 {
2333   cm_return_val_if_fail (event != NULL, FALSE);
2334   
2335   switch (event->type)
2336     {
2337     case GDK_MOTION_NOTIFY:
2338     case GDK_BUTTON_PRESS:
2339     case GDK_2BUTTON_PRESS:
2340     case GDK_3BUTTON_PRESS:
2341     case GDK_BUTTON_RELEASE:
2342     case GDK_ENTER_NOTIFY:
2343     case GDK_LEAVE_NOTIFY:
2344       return TRUE;
2345     default:
2346       break;
2347     }
2348   return FALSE;
2349 }
2350
2351
2352 /* PUBLIC CELL FUNCTIONS
2353  *   gtk_cmclist_get_cell_type
2354  *   gtk_cmclist_set_text
2355  *   gtk_cmclist_get_text
2356  *   gtk_cmclist_set_pixbuf
2357  *   gtk_cmclist_get_pixbuf
2358  *   gtk_cmclist_set_pixtext
2359  *   gtk_cmclist_get_pixtext
2360  *   gtk_cmclist_set_shift
2361  */
2362 GtkCMCellType 
2363 gtk_cmclist_get_cell_type (GtkCMCList *clist,
2364                          gint      row,
2365                          gint      column)
2366 {
2367   GtkCMCListRow *clist_row;
2368
2369   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2370
2371   if (row < 0 || row >= clist->rows)
2372     return -1;
2373   if (column < 0 || column >= clist->columns)
2374     return -1;
2375
2376   clist_row = ROW_ELEMENT (clist, row)->data;
2377
2378   return clist_row->cell[column].type;
2379 }
2380
2381 void
2382 gtk_cmclist_set_text (GtkCMCList    *clist,
2383                     gint         row,
2384                     gint         column,
2385                     const gchar *text)
2386 {
2387   GtkCMCListRow *clist_row;
2388
2389   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2390
2391   if (row < 0 || row >= clist->rows)
2392     return;
2393   if (column < 0 || column >= clist->columns)
2394     return;
2395
2396   clist_row = ROW_ELEMENT (clist, row)->data;
2397
2398   /* if text is null, then the cell is empty */
2399   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2400     (clist, clist_row, column, GTK_CMCELL_TEXT, text, 0, NULL);
2401
2402   /* redraw the list if it's not frozen */
2403   if (CLIST_UNFROZEN (clist))
2404     {
2405       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2406         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2407     }
2408 }
2409
2410 gint
2411 gtk_cmclist_get_text (GtkCMCList  *clist,
2412                     gint       row,
2413                     gint       column,
2414                     gchar    **text)
2415 {
2416   GtkCMCListRow *clist_row;
2417
2418   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2419
2420   if (row < 0 || row >= clist->rows)
2421     return 0;
2422   if (column < 0 || column >= clist->columns)
2423     return 0;
2424
2425   clist_row = ROW_ELEMENT (clist, row)->data;
2426
2427   if (clist_row->cell[column].type != GTK_CMCELL_TEXT)
2428     return 0;
2429
2430   if (text)
2431     *text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2432
2433   return 1;
2434 }
2435
2436 void
2437 gtk_cmclist_set_pixbuf (GtkCMCList  *clist,
2438                       gint       row,
2439                       gint       column,
2440                       GdkPixbuf *pixbuf)
2441 {
2442   GtkCMCListRow *clist_row;
2443
2444   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2445
2446   if (row < 0 || row >= clist->rows)
2447     return;
2448   if (column < 0 || column >= clist->columns)
2449     return;
2450
2451   clist_row = ROW_ELEMENT (clist, row)->data;
2452   
2453   g_object_ref (pixbuf);
2454   
2455   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2456     (clist, clist_row, column, GTK_CMCELL_PIXBUF, NULL, 0, pixbuf);
2457
2458   /* redraw the list if it's not frozen */
2459   if (CLIST_UNFROZEN (clist))
2460     {
2461       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2462         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2463     }
2464 }
2465
2466 gint
2467 gtk_cmclist_get_pixbuf (GtkCMCList   *clist,
2468                       gint        row,
2469                       gint        column,
2470                       GdkPixbuf **pixbuf)
2471 {
2472   GtkCMCListRow *clist_row;
2473
2474   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2475
2476   if (row < 0 || row >= clist->rows)
2477     return 0;
2478   if (column < 0 || column >= clist->columns)
2479     return 0;
2480
2481   clist_row = ROW_ELEMENT (clist, row)->data;
2482
2483   if (clist_row->cell[column].type != GTK_CMCELL_PIXBUF)
2484     return 0;
2485
2486   if (pixbuf)
2487   {
2488     *pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2489   }
2490
2491   return 1;
2492 }
2493
2494 void
2495 gtk_cmclist_set_pixtext (GtkCMCList    *clist,
2496                        gint         row,
2497                        gint         column,
2498                        const gchar *text,
2499                        guint8       spacing,
2500                        GdkPixbuf   *pixbuf)
2501 {
2502   GtkCMCListRow *clist_row;
2503
2504   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2505
2506   if (row < 0 || row >= clist->rows)
2507     return;
2508   if (column < 0 || column >= clist->columns)
2509     return;
2510
2511   clist_row = ROW_ELEMENT (clist, row)->data;
2512   
2513   g_object_ref (pixbuf);
2514   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2515     (clist, clist_row, column, GTK_CMCELL_PIXTEXT, text, spacing, pixbuf);
2516
2517   /* redraw the list if it's not frozen */
2518   if (CLIST_UNFROZEN (clist))
2519     {
2520       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2521         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2522     }
2523 }
2524
2525 gint
2526 gtk_cmclist_get_pixtext (GtkCMCList   *clist,
2527                        gint        row,
2528                        gint        column,
2529                        gchar     **text,
2530                        guint8     *spacing,
2531                        GdkPixbuf **pixbuf)
2532 {
2533   GtkCMCListRow *clist_row;
2534
2535   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2536
2537   if (row < 0 || row >= clist->rows)
2538     return 0;
2539   if (column < 0 || column >= clist->columns)
2540     return 0;
2541
2542   clist_row = ROW_ELEMENT (clist, row)->data;
2543
2544   if (clist_row->cell[column].type != GTK_CMCELL_PIXTEXT)
2545     return 0;
2546
2547   if (text)
2548     *text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2549   if (spacing)
2550     *spacing = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2551   if (pixbuf)
2552     *pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2553
2554   return 1;
2555 }
2556
2557 void
2558 gtk_cmclist_set_shift (GtkCMCList *clist,
2559                      gint      row,
2560                      gint      column,
2561                      gint      vertical,
2562                      gint      horizontal)
2563 {
2564   GtkRequisition requisition = { 0 };
2565   GtkCMCListRow *clist_row;
2566
2567   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2568
2569   if (row < 0 || row >= clist->rows)
2570     return;
2571   if (column < 0 || column >= clist->columns)
2572     return;
2573
2574   clist_row = ROW_ELEMENT (clist, row)->data;
2575
2576   if (clist->column[column].auto_resize &&
2577       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2578     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2579                                                    column, &requisition);
2580
2581   clist_row->cell[column].vertical = vertical;
2582   clist_row->cell[column].horizontal = horizontal;
2583
2584   column_auto_resize (clist, clist_row, column, requisition.width);
2585
2586   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2587     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2588 }
2589
2590 /* PRIVATE CELL FUNCTIONS
2591  *   set_cell_contents
2592  *   cell_size_request
2593  */
2594 static void
2595 set_cell_contents (GtkCMCList    *clist,
2596                    GtkCMCListRow *clist_row,
2597                    gint         column,
2598                    GtkCMCellType  type,
2599                    const gchar *text,
2600                    guint8       spacing,
2601                    GdkPixbuf   *pixbuf)
2602 {
2603   GtkRequisition requisition;
2604   gchar *old_text = NULL;
2605   GdkPixbuf *old_pixbuf = NULL;
2606   
2607   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2608   cm_return_if_fail (clist_row != NULL);
2609
2610   if (clist->column[column].auto_resize &&
2611       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2612     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2613                                                    column, &requisition);
2614
2615   switch (clist_row->cell[column].type)
2616     {
2617     case GTK_CMCELL_EMPTY:
2618       break;
2619     case GTK_CMCELL_TEXT:
2620       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2621       break;
2622     case GTK_CMCELL_PIXBUF:
2623       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2624       break;
2625     case GTK_CMCELL_PIXTEXT:
2626       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2627       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2628       break;
2629     case GTK_CMCELL_WIDGET:
2630       /* unimplemented */
2631       break;
2632     default:
2633       break;
2634     }
2635
2636   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2637
2638   /* Note that pixbuf and mask were already ref'ed by the caller
2639    */
2640   switch (type)
2641     {
2642     case GTK_CMCELL_TEXT:
2643       if (text)
2644         {
2645           clist_row->cell[column].type = GTK_CMCELL_TEXT;
2646           GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2647         }
2648       break;
2649     case GTK_CMCELL_PIXBUF:
2650       if (pixbuf)
2651         {
2652           clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2653           GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2654         }
2655       break;
2656     case GTK_CMCELL_PIXTEXT:
2657       if (text && pixbuf)
2658         {
2659           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2660           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2661           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2662           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2663         }
2664       break;
2665     default:
2666       break;
2667     }
2668
2669   if (clist->column[column].auto_resize &&
2670       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2671     column_auto_resize (clist, clist_row, column, requisition.width);
2672
2673   g_free (old_text);
2674   if (old_pixbuf)
2675     g_object_unref (old_pixbuf);
2676 }
2677
2678 PangoLayout *
2679 _gtk_cmclist_create_cell_layout (GtkCMCList       *clist,
2680                                GtkCMCListRow    *clist_row,
2681                                gint            column)
2682 {
2683   PangoLayout *layout;
2684   GtkStyle *style;
2685   GtkCMCell *cell;
2686   gchar *text;
2687   
2688   get_cell_style (clist, clist_row, GTK_STATE_NORMAL, column, &style);
2689
2690
2691   cell = &clist_row->cell[column];
2692   switch (cell->type)
2693     {
2694     case GTK_CMCELL_TEXT:
2695     case GTK_CMCELL_PIXTEXT:
2696       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
2697               GTK_CMCELL_PIXTEXT (*cell)->text :
2698               GTK_CMCELL_TEXT (*cell)->text);
2699
2700       if (!text)
2701         return NULL;
2702       
2703       layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
2704                                                ((cell->type == GTK_CMCELL_PIXTEXT) ?
2705                                                 GTK_CMCELL_PIXTEXT (*cell)->text :
2706                                                 GTK_CMCELL_TEXT (*cell)->text));
2707       pango_layout_set_font_description (layout, style->font_desc);
2708       
2709       return layout;
2710       
2711     default:
2712       return NULL;
2713     }
2714 }
2715
2716 static void
2717 cell_size_request (GtkCMCList       *clist,
2718                    GtkCMCListRow    *clist_row,
2719                    gint            column,
2720                    GtkRequisition *requisition)
2721 {
2722   gint width;
2723   gint height;
2724   PangoLayout *layout;
2725   PangoRectangle logical_rect;
2726
2727   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2728   cm_return_if_fail (requisition != NULL);
2729
2730   layout = _gtk_cmclist_create_cell_layout (clist, clist_row, column);
2731   if (layout)
2732     {
2733       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2734       
2735       requisition->width = logical_rect.width;
2736       requisition->height = logical_rect.height;
2737       
2738       g_object_unref (G_OBJECT (layout));
2739     }
2740   else
2741     {
2742       requisition->width  = 0;
2743       requisition->height = 0;
2744     }
2745
2746   if (layout && clist_row->cell[column].type == GTK_CMCELL_PIXTEXT)
2747     requisition->width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2748
2749   switch (clist_row->cell[column].type)
2750     {
2751     case GTK_CMCELL_PIXTEXT:
2752       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2753       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2754       requisition->width += width;
2755       requisition->height = MAX (requisition->height, height);      
2756       break;
2757     case GTK_CMCELL_PIXBUF:
2758       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2759       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2760       requisition->width += width;
2761       requisition->height = MAX (requisition->height, height);
2762       break;
2763       
2764     default:
2765       break;
2766     }
2767
2768   requisition->width  += clist_row->cell[column].horizontal;
2769   requisition->height += clist_row->cell[column].vertical;
2770 }
2771
2772 /* PUBLIC INSERT/REMOVE ROW FUNCTIONS
2773  *   gtk_cmclist_prepend
2774  *   gtk_cmclist_append
2775  *   gtk_cmclist_insert
2776  *   gtk_cmclist_remove
2777  *   gtk_cmclist_clear
2778  */
2779 gint
2780 gtk_cmclist_prepend (GtkCMCList    *clist,
2781                    gchar       *text[])
2782 {
2783   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2784   cm_return_val_if_fail (text != NULL, -1);
2785
2786   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, 0, text);
2787 }
2788
2789 gint
2790 gtk_cmclist_append (GtkCMCList    *clist,
2791                   gchar       *text[])
2792 {
2793   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2794   cm_return_val_if_fail (text != NULL, -1);
2795
2796   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, clist->rows, text);
2797 }
2798
2799 gint
2800 gtk_cmclist_insert (GtkCMCList    *clist,
2801                   gint         row,
2802                   gchar       *text[])
2803 {
2804   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2805   cm_return_val_if_fail (text != NULL, -1);
2806
2807   if (row < 0 || row > clist->rows)
2808     row = clist->rows;
2809
2810   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, row, text);
2811 }
2812
2813 void
2814 gtk_cmclist_remove (GtkCMCList *clist,
2815                   gint      row)
2816 {
2817   GTK_CMCLIST_GET_CLASS (clist)->remove_row (clist, row);
2818 }
2819
2820 void
2821 gtk_cmclist_clear (GtkCMCList *clist)
2822 {
2823   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2824   
2825   GTK_CMCLIST_GET_CLASS (clist)->clear (clist);
2826 }
2827
2828 /* PRIVATE INSERT/REMOVE ROW FUNCTIONS
2829  *   real_insert_row
2830  *   real_remove_row
2831  *   real_clear
2832  *   real_row_move
2833  */
2834 static gint
2835 real_insert_row (GtkCMCList *clist,
2836                  gint      row,
2837                  gchar    *text[])
2838 {
2839   gint i;
2840   GtkCMCListRow *clist_row;
2841
2842   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2843   cm_return_val_if_fail (text != NULL, -1);
2844
2845   /* return if out of bounds */
2846   if (row < 0 || row > clist->rows)
2847     return -1;
2848
2849   /* create the row */
2850   clist_row = row_new (clist);
2851
2852   /* set the text in the row's columns */
2853   for (i = 0; i < clist->columns; i++)
2854     if (text[i])
2855       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2856         (clist, clist_row, i, GTK_CMCELL_TEXT, text[i], 0, NULL);
2857
2858   if (!clist->rows)
2859     {
2860       clist->row_list = g_list_append (clist->row_list, clist_row);
2861       clist->row_list_end = clist->row_list;
2862     }
2863   else
2864     {
2865       if (GTK_CMCLIST_AUTO_SORT(clist))   /* override insertion pos */
2866         {
2867           GList *work;
2868           
2869           row = 0;
2870           work = clist->row_list;
2871           
2872           if (clist->sort_type == GTK_SORT_ASCENDING)
2873             {
2874               while (row < clist->rows &&
2875                      clist->compare (clist, clist_row,
2876                                      GTK_CMCLIST_ROW (work)) > 0)
2877                 {
2878                   row++;
2879                   work = work->next;
2880                 }
2881             }
2882           else
2883             {
2884               while (row < clist->rows &&
2885                      clist->compare (clist, clist_row,
2886                                      GTK_CMCLIST_ROW (work)) < 0)
2887                 {
2888                   row++;
2889                   work = work->next;
2890                 }
2891             }
2892         }
2893       
2894       /* reset the row end pointer if we're inserting at the end of the list */
2895       if (row == clist->rows)
2896         clist->row_list_end = (g_list_append (clist->row_list_end,
2897                                               clist_row))->next;
2898       else
2899         clist->row_list = g_list_insert (clist->row_list, clist_row, row);
2900
2901     }
2902   clist->rows++;
2903
2904   if (row < ROW_FROM_YPIXEL (clist, 0))
2905     clist->voffset -= (clist->row_height + CELL_SPACING);
2906
2907   /* syncronize the selection list */
2908   sync_selection (clist, row, SYNC_INSERT);
2909
2910   if (clist->rows == 1)
2911     {
2912       clist->focus_row = 0;
2913       if (clist->selection_mode == GTK_SELECTION_BROWSE)
2914         gtk_cmclist_select_row (clist, 0, -1);
2915     }
2916
2917   /* redraw the list if it isn't frozen */
2918   if (CLIST_UNFROZEN (clist))
2919     {
2920       adjust_adjustments (clist, FALSE);
2921
2922       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2923         draw_rows (clist, NULL);
2924     }
2925
2926   return row;
2927 }
2928
2929 static void
2930 real_remove_row (GtkCMCList *clist,
2931                  gint      row)
2932 {
2933   gint was_visible;
2934   GList *list;
2935   GtkCMCListRow *clist_row;
2936
2937   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2938
2939   /* return if out of bounds */
2940   if (row < 0 || row > (clist->rows - 1))
2941     return;
2942
2943   was_visible = (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE);
2944
2945   /* get the row we're going to delete */
2946   list = ROW_ELEMENT (clist, row);
2947   g_assert (list != NULL);
2948   clist_row = list->data;
2949
2950   /* if we're removing a selected row, we have to make sure
2951    * it's properly unselected, and then sync up the clist->selected
2952    * list to reflect the deincrimented indexies of rows after the
2953    * removal */
2954   if (clist_row->state == GTK_STATE_SELECTED)
2955     g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
2956                      row, -1, NULL);
2957
2958   sync_selection (clist, row, SYNC_REMOVE);
2959
2960   /* reset the row end pointer if we're removing at the end of the list */
2961   clist->rows--;
2962   if (clist->row_list == list)
2963     clist->row_list = g_list_next (list);
2964   if (clist->row_list_end == list)
2965     clist->row_list_end = g_list_previous (list);
2966   list = g_list_remove (list, clist_row);
2967
2968   if (row < ROW_FROM_YPIXEL (clist, 0))
2969     clist->voffset += clist->row_height + CELL_SPACING;
2970
2971   if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
2972       clist->focus_row >= 0)
2973     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
2974                      clist->focus_row, -1, NULL);
2975
2976   /* toast the row */
2977   row_delete (clist, clist_row);
2978
2979   /* redraw the row if it isn't frozen */
2980   if (CLIST_UNFROZEN (clist))
2981     {
2982       adjust_adjustments (clist, FALSE);
2983
2984       if (was_visible)
2985         draw_rows (clist, NULL);
2986     }
2987 }
2988
2989 static void
2990 real_clear (GtkCMCList *clist)
2991 {
2992   GList *list;
2993   GList *free_list;
2994   GtkRequisition requisition;
2995   gint i;
2996
2997   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2998
2999   /* free up the selection list */
3000   g_list_free (clist->selection);
3001   g_list_free (clist->undo_selection);
3002   g_list_free (clist->undo_unselection);
3003
3004   clist->selection = NULL;
3005   clist->selection_end = NULL;
3006   clist->undo_selection = NULL;
3007   clist->undo_unselection = NULL;
3008   clist->voffset = 0;
3009   clist->focus_row = -1;
3010   clist->anchor = -1;
3011   clist->undo_anchor = -1;
3012   clist->anchor_state = GTK_STATE_SELECTED;
3013   clist->drag_pos = -1;
3014
3015   /* remove all the rows */
3016   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3017   free_list = clist->row_list;
3018   clist->row_list = NULL;
3019   clist->row_list_end = NULL;
3020   clist->rows = 0;
3021   for (list = free_list; list; list = list->next)
3022     row_delete (clist, GTK_CMCLIST_ROW (list));
3023   g_list_free (free_list);
3024   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
3025   for (i = 0; i < clist->columns; i++)
3026     if (clist->column[i].auto_resize)
3027       {
3028         if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
3029     {
3030         gtk_widget_get_requisition (clist->column[i].button, &requisition);
3031           gtk_cmclist_set_column_width
3032             (clist, i, (requisition.width -
3033                         (CELL_SPACING + (2 * COLUMN_INSET))));
3034     }
3035         else
3036           gtk_cmclist_set_column_width (clist, i, 0);
3037       }
3038   /* zero-out the scrollbars */
3039   if (clist->vadjustment)
3040     {
3041       gtk_adjustment_set_value (clist->vadjustment, 0.0);
3042       CLIST_REFRESH (clist);
3043     }
3044   else
3045     gtk_widget_queue_resize (GTK_WIDGET (clist));
3046 }
3047
3048 static void
3049 real_row_move (GtkCMCList *clist,
3050                gint      source_row,
3051                gint      dest_row)
3052 {
3053   GtkCMCListRow *clist_row;
3054   GList *list;
3055   gint first, last;
3056   gint d;
3057
3058   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3059
3060   if (GTK_CMCLIST_AUTO_SORT(clist))
3061     return;
3062
3063   if (source_row < 0 || source_row >= clist->rows ||
3064       dest_row   < 0 || dest_row   >= clist->rows ||
3065       source_row == dest_row)
3066     return;
3067
3068   gtk_cmclist_freeze (clist);
3069
3070   /* unlink source row */
3071   clist_row = ROW_ELEMENT (clist, source_row)->data;
3072   if (source_row == clist->rows - 1)
3073     clist->row_list_end = clist->row_list_end->prev;
3074   clist->row_list = g_list_remove (clist->row_list, clist_row);
3075   clist->rows--;
3076
3077   /* relink source row */
3078   clist->row_list = g_list_insert (clist->row_list, clist_row, dest_row);
3079   if (dest_row == clist->rows)
3080     clist->row_list_end = clist->row_list_end->next;
3081   clist->rows++;
3082
3083   /* sync selection */
3084   if (source_row > dest_row)
3085     {
3086       first = dest_row;
3087       last  = source_row;
3088       d = 1;
3089     }
3090   else
3091     {
3092       first = source_row;
3093       last  = dest_row;
3094       d = -1;
3095     }
3096
3097   for (list = clist->selection; list; list = list->next)
3098     {
3099       if (list->data == GINT_TO_POINTER (source_row))
3100         list->data = GINT_TO_POINTER (dest_row);
3101       else if (first <= GPOINTER_TO_INT (list->data) &&
3102                last >= GPOINTER_TO_INT (list->data))
3103         list->data = GINT_TO_POINTER (GPOINTER_TO_INT (list->data) + d);
3104     }
3105   
3106   if (clist->focus_row == source_row)
3107     clist->focus_row = dest_row;
3108   else if (clist->focus_row > first)
3109     clist->focus_row += d;
3110
3111   gtk_cmclist_thaw (clist);
3112 }
3113
3114 /* PUBLIC ROW FUNCTIONS
3115  *   gtk_cmclist_moveto
3116  *   gtk_cmclist_set_row_height
3117  *   gtk_cmclist_set_row_data
3118  *   gtk_cmclist_set_row_data_full
3119  *   gtk_cmclist_get_row_data
3120  *   gtk_cmclist_find_row_from_data
3121  *   gtk_cmclist_swap_rows
3122  *   gtk_cmclist_row_move
3123  *   gtk_cmclist_row_is_visible
3124  *   gtk_cmclist_set_foreground
3125  *   gtk_cmclist_set_background
3126  */
3127 void
3128 gtk_cmclist_moveto (GtkCMCList *clist,
3129                   gint      row,
3130                   gint      column,
3131                   gfloat    row_align,
3132                   gfloat    col_align)
3133 {
3134   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3135
3136   if (row < -1 || row >= clist->rows)
3137     return;
3138   if (column < -1 || column >= clist->columns)
3139     return;
3140
3141   row_align = CLAMP (row_align, 0, 1);
3142   col_align = CLAMP (col_align, 0, 1);
3143
3144   /* adjust horizontal scrollbar */
3145   if (clist->hadjustment && column >= 0)
3146     {
3147       gint x;
3148
3149       x = (COLUMN_LEFT (clist, column) - CELL_SPACING - COLUMN_INSET -
3150            (col_align * (clist->clist_window_width - 2 * COLUMN_INSET -
3151                          CELL_SPACING - clist->column[column].area.width)));
3152       if (x < 0)
3153         gtk_adjustment_set_value (clist->hadjustment, 0.0);
3154       else if (x > LIST_WIDTH (clist) - clist->clist_window_width)
3155         gtk_adjustment_set_value 
3156           (clist->hadjustment, LIST_WIDTH (clist) - clist->clist_window_width);
3157       else
3158         gtk_adjustment_set_value (clist->hadjustment, x);
3159     }
3160
3161   /* adjust vertical scrollbar */
3162   if (clist->vadjustment && row >= 0)
3163     move_vertical (clist, row, row_align);
3164 }
3165
3166 void
3167 gtk_cmclist_set_row_height (GtkCMCList *clist,
3168                           guint     height)
3169 {
3170   GtkStyle *style;
3171   GtkWidget *widget;
3172
3173   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3174
3175   widget = GTK_WIDGET (clist);
3176
3177   style = gtk_widget_get_style (widget);
3178
3179   if (height > 0)
3180     {
3181       clist->row_height = height;
3182       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3183     }
3184   else
3185     {
3186       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3187       clist->row_height = 0;
3188     }
3189
3190   if (style->font_desc)
3191     {
3192       PangoContext *context = gtk_widget_get_pango_context (widget);
3193       PangoFontMetrics *metrics;
3194
3195       metrics = pango_context_get_metrics (context,
3196                                            style->font_desc,
3197                                            pango_context_get_language (context));
3198
3199       if (!GTK_CMCLIST_ROW_HEIGHT_SET(clist))
3200         {
3201           clist->row_height = (pango_font_metrics_get_ascent (metrics) +
3202                                pango_font_metrics_get_descent (metrics));
3203           clist->row_height = PANGO_PIXELS (clist->row_height) + 1;
3204         }
3205
3206       pango_font_metrics_unref (metrics);
3207     }
3208
3209   CLIST_REFRESH (clist);
3210 }
3211
3212 void
3213 gtk_cmclist_set_row_data (GtkCMCList *clist,
3214                         gint      row,
3215                         gpointer  data)
3216 {
3217   gtk_cmclist_set_row_data_full (clist, row, data, NULL);
3218 }
3219
3220 void
3221 gtk_cmclist_set_row_data_full (GtkCMCList         *clist,
3222                              gint              row,
3223                              gpointer          data,
3224                              GDestroyNotify  destroy)
3225 {
3226   GtkCMCListRow *clist_row;
3227
3228   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3229
3230   if (row < 0 || row > (clist->rows - 1))
3231     return;
3232
3233   clist_row = ROW_ELEMENT (clist, row)->data;
3234
3235   if (clist_row->destroy)
3236     clist_row->destroy (clist_row->data);
3237   
3238   clist_row->data = data;
3239   clist_row->destroy = destroy;
3240 }
3241
3242 gpointer
3243 gtk_cmclist_get_row_data (GtkCMCList *clist,
3244                         gint      row)
3245 {
3246   GtkCMCListRow *clist_row;
3247
3248   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3249
3250   if (row < 0 || row > (clist->rows - 1))
3251     return NULL;
3252
3253   clist_row = ROW_ELEMENT (clist, row)->data;
3254   return clist_row->data;
3255 }
3256
3257 gint
3258 gtk_cmclist_find_row_from_data (GtkCMCList *clist,
3259                               gpointer  data)
3260 {
3261   GList *list;
3262   gint n;
3263
3264   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
3265
3266   for (n = 0, list = clist->row_list; list; n++, list = list->next)
3267     if (GTK_CMCLIST_ROW (list)->data == data)
3268       return n;
3269
3270   return -1;
3271 }
3272
3273 void 
3274 gtk_cmclist_swap_rows (GtkCMCList *clist,
3275                      gint      row1, 
3276                      gint      row2)
3277 {
3278   gint first, last;
3279
3280   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3281   cm_return_if_fail (row1 != row2);
3282
3283   if (GTK_CMCLIST_AUTO_SORT(clist))
3284     return;
3285
3286   gtk_cmclist_freeze (clist);
3287
3288   first = MIN (row1, row2);
3289   last  = MAX (row1, row2);
3290
3291   gtk_cmclist_row_move (clist, last, first);
3292   gtk_cmclist_row_move (clist, first + 1, last);
3293   
3294   gtk_cmclist_thaw (clist);
3295 }
3296
3297 void
3298 gtk_cmclist_row_move (GtkCMCList *clist,
3299                     gint      source_row,
3300                     gint      dest_row)
3301 {
3302   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3303
3304   if (GTK_CMCLIST_AUTO_SORT(clist))
3305     return;
3306
3307   if (source_row < 0 || source_row >= clist->rows ||
3308       dest_row   < 0 || dest_row   >= clist->rows ||
3309       source_row == dest_row)
3310     return;
3311
3312   g_signal_emit (G_OBJECT (clist), clist_signals[ROW_MOVE], 0,
3313                    source_row, dest_row);
3314 }
3315
3316 GtkVisibility
3317 gtk_cmclist_row_is_visible (GtkCMCList *clist,
3318                           gint      row)
3319 {
3320   gint top;
3321
3322   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
3323
3324   if (row < 0 || row >= clist->rows)
3325     return GTK_VISIBILITY_NONE;
3326
3327   if (clist->row_height == 0)
3328     return GTK_VISIBILITY_NONE;
3329
3330   if (row < ROW_FROM_YPIXEL (clist, 0))
3331     return GTK_VISIBILITY_NONE;
3332
3333   if (row > ROW_FROM_YPIXEL (clist, clist->clist_window_height))
3334     return GTK_VISIBILITY_NONE;
3335
3336   top = ROW_TOP_YPIXEL (clist, row);
3337
3338   if ((top < 0)
3339       || ((top + clist->row_height) >= clist->clist_window_height))
3340     return GTK_VISIBILITY_PARTIAL;
3341
3342   return GTK_VISIBILITY_FULL;
3343 }
3344
3345 void
3346 gtk_cmclist_set_foreground (GtkCMCList       *clist,
3347                           gint            row,
3348                           const GdkColor *color)
3349 {
3350   GtkCMCListRow *clist_row;
3351
3352   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3353
3354   if (row < 0 || row >= clist->rows)
3355     return;
3356
3357   clist_row = ROW_ELEMENT (clist, row)->data;
3358
3359   if (color)
3360     {
3361       clist_row->foreground = *color;
3362       clist_row->fg_set = TRUE;
3363 #if !GTK_CHECK_VERSION(3, 0, 0)
3364       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3365         gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3366                          &clist_row->foreground, TRUE, TRUE);
3367 #endif
3368     }
3369   else
3370     clist_row->fg_set = FALSE;
3371
3372   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3373     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3374 }
3375
3376 void
3377 gtk_cmclist_set_background (GtkCMCList       *clist,
3378                           gint            row,
3379                           const GdkColor *color)
3380 {
3381   GtkCMCListRow *clist_row;
3382
3383   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3384
3385   if (row < 0 || row >= clist->rows)
3386     return;
3387
3388   clist_row = ROW_ELEMENT (clist, row)->data;
3389
3390   if (color)
3391     {
3392       clist_row->background = *color;
3393       clist_row->bg_set = TRUE;
3394 #if !GTK_CHECK_VERSION(3, 0, 0)
3395       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3396         gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3397                          &clist_row->background, TRUE, TRUE);
3398 #endif
3399     }
3400   else
3401     clist_row->bg_set = FALSE;
3402
3403   if (CLIST_UNFROZEN (clist)
3404       && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3405     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3406 }
3407
3408 /* PUBLIC ROW/CELL STYLE FUNCTIONS
3409  *   gtk_cmclist_set_cell_style
3410  *   gtk_cmclist_get_cell_style
3411  *   gtk_cmclist_set_row_style
3412  *   gtk_cmclist_get_row_style
3413  */
3414 void
3415 gtk_cmclist_set_cell_style (GtkCMCList *clist,
3416                           gint      row,
3417                           gint      column,
3418                           GtkStyle *style)
3419 {
3420   GtkRequisition requisition = { 0 };
3421   GtkCMCListRow *clist_row;
3422
3423   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3424
3425   if (row < 0 || row >= clist->rows)
3426     return;
3427   if (column < 0 || column >= clist->columns)
3428     return;
3429
3430   clist_row = ROW_ELEMENT (clist, row)->data;
3431
3432   if (clist_row->cell[column].style == style)
3433     return;
3434
3435   if (clist->column[column].auto_resize &&
3436       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3437     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3438                                                    column, &requisition);
3439
3440   if (clist_row->cell[column].style)
3441     {
3442       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3443         gtk_style_detach (clist_row->cell[column].style);
3444       g_object_unref (clist_row->cell[column].style);
3445     }
3446
3447   clist_row->cell[column].style = style;
3448
3449   if (clist_row->cell[column].style)
3450     {
3451       g_object_ref (clist_row->cell[column].style);
3452       
3453       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3454         clist_row->cell[column].style =
3455           gtk_style_attach (clist_row->cell[column].style,
3456                             clist->clist_window);
3457     }
3458
3459   column_auto_resize (clist, clist_row, column, requisition.width);
3460
3461   /* redraw the list if it's not frozen */
3462   if (CLIST_UNFROZEN (clist))
3463     {
3464       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3465         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3466     }
3467 }
3468
3469 GtkStyle *
3470 gtk_cmclist_get_cell_style (GtkCMCList *clist,
3471                           gint      row,
3472                           gint      column)
3473 {
3474   GtkCMCListRow *clist_row;
3475
3476   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3477
3478   if (row < 0 || row >= clist->rows || column < 0 || column >= clist->columns)
3479     return NULL;
3480
3481   clist_row = ROW_ELEMENT (clist, row)->data;
3482
3483   return clist_row->cell[column].style;
3484 }
3485
3486 void
3487 gtk_cmclist_set_row_style (GtkCMCList *clist,
3488                          gint      row,
3489                          GtkStyle *style)
3490 {
3491   GtkRequisition requisition;
3492   GtkCMCListRow *clist_row;
3493   gint *old_width;
3494   gint i;
3495
3496   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3497
3498   if (row < 0 || row >= clist->rows)
3499     return;
3500
3501   clist_row = ROW_ELEMENT (clist, row)->data;
3502
3503   if (clist_row->style == style)
3504     return;
3505
3506   old_width = g_new (gint, clist->columns);
3507
3508   if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3509     {
3510       for (i = 0; i < clist->columns; i++)
3511         if (clist->column[i].auto_resize)
3512           {
3513             GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3514                                                            i, &requisition);
3515             old_width[i] = requisition.width;
3516           }
3517     }
3518
3519   if (clist_row->style)
3520     {
3521       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3522         gtk_style_detach (clist_row->style);
3523       g_object_unref (clist_row->style);
3524     }
3525
3526   clist_row->style = style;
3527
3528   if (clist_row->style)
3529     {
3530       g_object_ref (clist_row->style);
3531       
3532       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3533         clist_row->style = gtk_style_attach (clist_row->style,
3534                                              clist->clist_window);
3535     }
3536
3537   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3538     for (i = 0; i < clist->columns; i++)
3539       column_auto_resize (clist, clist_row, i, old_width[i]);
3540
3541   g_free (old_width);
3542
3543   /* redraw the list if it's not frozen */
3544   if (CLIST_UNFROZEN (clist))
3545     {
3546       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3547         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3548     }
3549 }
3550
3551 GtkStyle *
3552 gtk_cmclist_get_row_style (GtkCMCList *clist,
3553                          gint      row)
3554 {
3555   GtkCMCListRow *clist_row;
3556
3557   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3558
3559   if (row < 0 || row >= clist->rows)
3560     return NULL;
3561
3562   clist_row = ROW_ELEMENT (clist, row)->data;
3563
3564   return clist_row->style;
3565 }
3566
3567 /* PUBLIC SELECTION FUNCTIONS
3568  *   gtk_cmclist_set_selectable
3569  *   gtk_cmclist_get_selectable
3570  *   gtk_cmclist_select_row
3571  *   gtk_cmclist_unselect_row
3572  *   gtk_cmclist_select_all
3573  *   gtk_cmclist_unselect_all
3574  *   gtk_cmclist_undo_selection
3575  */
3576 void
3577 gtk_cmclist_set_selectable (GtkCMCList *clist,
3578                           gint      row,
3579                           gboolean  selectable)
3580 {
3581   GtkCMCListRow *clist_row;
3582
3583   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3584
3585   if (row < 0 || row >= clist->rows)
3586     return;
3587
3588   clist_row = ROW_ELEMENT (clist, row)->data;
3589
3590   if (selectable == clist_row->selectable)
3591     return;
3592
3593   clist_row->selectable = selectable;
3594
3595   if (!selectable && clist_row->state == GTK_STATE_SELECTED)
3596     {
3597       if (clist->anchor >= 0 &&
3598           clist->selection_mode == GTK_SELECTION_MULTIPLE)
3599         {
3600           clist->drag_button = 0;
3601           remove_grab (clist);
3602           GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3603         }
3604       g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3605                        row, -1, NULL);
3606     }      
3607 }
3608
3609 gboolean
3610 gtk_cmclist_get_selectable (GtkCMCList *clist,
3611                           gint      row)
3612 {
3613   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), FALSE);
3614
3615   if (row < 0 || row >= clist->rows)
3616     return FALSE;
3617
3618   return GTK_CMCLIST_ROW (ROW_ELEMENT (clist, row))->selectable;
3619 }
3620
3621 void
3622 gtk_cmclist_select_row (GtkCMCList *clist,
3623                       gint      row,
3624                       gint      column)
3625 {
3626   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3627
3628   if (row < 0 || row >= clist->rows)
3629     return;
3630   if (column < -1 || column >= clist->columns)
3631     return;
3632
3633   g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3634                    row, column, NULL);
3635 }
3636
3637 void
3638 gtk_cmclist_unselect_row (GtkCMCList *clist,
3639                         gint      row,
3640                         gint      column)
3641 {
3642   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3643
3644   if (row < 0 || row >= clist->rows)
3645     return;
3646   if (column < -1 || column >= clist->columns)
3647     return;
3648
3649   g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3650                    row, column, NULL);
3651 }
3652
3653 void
3654 gtk_cmclist_select_all (GtkCMCList *clist)
3655 {
3656   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3657
3658   GTK_CMCLIST_GET_CLASS (clist)->select_all (clist);
3659 }
3660
3661 void
3662 gtk_cmclist_unselect_all (GtkCMCList *clist)
3663 {
3664   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3665
3666   GTK_CMCLIST_GET_CLASS (clist)->unselect_all (clist);
3667 }
3668
3669 void
3670 gtk_cmclist_undo_selection (GtkCMCList *clist)
3671 {
3672   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3673
3674   if (clist->selection_mode == GTK_SELECTION_MULTIPLE &&
3675       (clist->undo_selection || clist->undo_unselection))
3676     g_signal_emit (G_OBJECT (clist), clist_signals[UNDO_SELECTION], 0);
3677 }
3678
3679 /* PRIVATE SELECTION FUNCTIONS
3680  *   selection_find
3681  *   toggle_row
3682  *   fake_toggle_row
3683  *   toggle_focus_row
3684  *   toggle_add_mode
3685  *   real_select_row
3686  *   real_unselect_row
3687  *   real_select_all
3688  *   real_unselect_all
3689  *   fake_unselect_all
3690  *   real_undo_selection
3691  *   set_anchor
3692  *   resync_selection
3693  *   update_extended_selection
3694  *   start_selection
3695  *   end_selection
3696  *   extend_selection
3697  *   sync_selection
3698  */
3699 static GList *
3700 selection_find (GtkCMCList *clist,
3701                 gint      row_number,
3702                 GList    *row_list_element)
3703 {
3704   return g_list_find (clist->selection, GINT_TO_POINTER (row_number));
3705 }
3706
3707 static void
3708 toggle_row (GtkCMCList *clist,
3709             gint      row,
3710             gint      column,
3711             GdkEvent *event)
3712 {
3713   GtkCMCListRow *clist_row;
3714
3715   switch (clist->selection_mode)
3716     {
3717     case GTK_SELECTION_MULTIPLE:
3718     case GTK_SELECTION_SINGLE:
3719       clist_row = ROW_ELEMENT (clist, row)->data;
3720
3721       if (!clist_row)
3722         return;
3723
3724       if (clist_row->state == GTK_STATE_SELECTED)
3725         {
3726           g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3727                            row, column, event);
3728           return;
3729         }
3730       break;
3731     case GTK_SELECTION_BROWSE:
3732       g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3733                        row, column, event);
3734       break;
3735     default:
3736       g_assert_not_reached ();
3737     }
3738 }
3739
3740 static void
3741 fake_toggle_row (GtkCMCList *clist,
3742                  gint      row)
3743 {
3744   GList *work;
3745
3746   work = ROW_ELEMENT (clist, row);
3747
3748   if (!work || !GTK_CMCLIST_ROW (work)->selectable)
3749     return;
3750   
3751   if (GTK_CMCLIST_ROW (work)->state == GTK_STATE_NORMAL)
3752     clist->anchor_state = GTK_CMCLIST_ROW (work)->state = GTK_STATE_SELECTED;
3753   else
3754     clist->anchor_state = GTK_CMCLIST_ROW (work)->state = GTK_STATE_NORMAL;
3755   
3756   if (CLIST_UNFROZEN (clist) &&
3757       gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3758     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row,
3759                                           GTK_CMCLIST_ROW (work));
3760 }
3761
3762 static gboolean
3763 clist_has_grab (GtkCMCList *clist)
3764 {
3765   return (gtk_widget_has_grab (GTK_WIDGET(clist)) &&
3766           gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (clist))));
3767 }
3768
3769 static void
3770 toggle_focus_row (GtkCMCList *clist)
3771 {
3772   cm_return_if_fail (clist != 0);
3773   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3774
3775   if (clist_has_grab (clist) ||
3776       clist->focus_row < 0 || clist->focus_row >= clist->rows)
3777     return;
3778
3779   switch (clist->selection_mode)
3780     {
3781     case  GTK_SELECTION_SINGLE:
3782       toggle_row (clist, clist->focus_row, 0, NULL);
3783       break;
3784     case GTK_SELECTION_MULTIPLE:
3785       g_list_free (clist->undo_selection);
3786       g_list_free (clist->undo_unselection);
3787       clist->undo_selection = NULL;
3788       clist->undo_unselection = NULL;
3789
3790       clist->anchor = clist->focus_row;
3791       clist->drag_pos = clist->focus_row;
3792       clist->undo_anchor = clist->focus_row;
3793       
3794       if (GTK_CMCLIST_ADD_MODE(clist))
3795         fake_toggle_row (clist, clist->focus_row);
3796       else
3797         GTK_CMCLIST_GET_CLASS (clist)->fake_unselect_all (clist,clist->focus_row);
3798
3799       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3800       break;
3801     default:
3802       break;
3803     }
3804 }
3805
3806 static void
3807 toggle_add_mode (GtkCMCList *clist)
3808 {
3809   cm_return_if_fail (clist != 0);
3810   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3811   
3812   if (clist_has_grab (clist) ||
3813       clist->selection_mode != GTK_SELECTION_MULTIPLE)
3814     return;
3815
3816   gtk_cmclist_undraw_focus (GTK_WIDGET (clist));
3817   if (!GTK_CMCLIST_ADD_MODE(clist))
3818     {
3819       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_ADD_MODE);
3820     }
3821   else
3822     {
3823       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ADD_MODE);
3824       clist->anchor_state = GTK_STATE_SELECTED;
3825     }
3826   gtk_cmclist_draw_focus (GTK_WIDGET (clist));
3827 }
3828
3829 static void
3830 real_select_row (GtkCMCList *clist,
3831                  gint      row,
3832                  gint      column,
3833                  GdkEvent *event)
3834 {
3835   GtkCMCListRow *clist_row;
3836   GList *list;
3837   gint sel_row;
3838   gboolean row_selected;
3839
3840   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3841
3842   if (row < 0 || row > (clist->rows - 1))
3843     return;
3844
3845   switch (clist->selection_mode)
3846     {
3847     case GTK_SELECTION_SINGLE:
3848     case GTK_SELECTION_BROWSE:
3849
3850       row_selected = FALSE;
3851       list = clist->selection;
3852
3853       while (list)
3854         {
3855           sel_row = GPOINTER_TO_INT (list->data);
3856           list = list->next;
3857
3858           if (row == sel_row)
3859             row_selected = TRUE;
3860           else
3861             g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3862                              sel_row, column, event);
3863         }
3864
3865       if (row_selected)
3866         return;
3867       
3868     default:
3869       break;
3870     }
3871
3872   clist_row = ROW_ELEMENT (clist, row)->data;
3873
3874   if (clist_row->state != GTK_STATE_NORMAL || !clist_row->selectable)
3875     return;
3876
3877   clist_row->state = GTK_STATE_SELECTED;
3878   if (!clist->selection)
3879     {
3880       clist->selection = g_list_append (clist->selection,
3881                                         GINT_TO_POINTER (row));
3882       clist->selection_end = clist->selection;
3883     }
3884   else
3885     clist->selection_end = 
3886       g_list_append (clist->selection_end, GINT_TO_POINTER (row))->next;
3887   
3888   if (CLIST_UNFROZEN (clist)
3889       && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3890     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3891 }
3892
3893 static void
3894 real_unselect_row (GtkCMCList *clist,
3895                    gint      row,
3896                    gint      column,
3897                    GdkEvent *event)
3898 {
3899   GtkCMCListRow *clist_row;
3900
3901   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3902
3903   if (row < 0 || row > (clist->rows - 1))
3904     return;
3905
3906   clist_row = ROW_ELEMENT (clist, row)->data;
3907
3908   if (clist_row->state == GTK_STATE_SELECTED)
3909     {
3910       clist_row->state = GTK_STATE_NORMAL;
3911
3912       if (clist->selection_end && 
3913           clist->selection_end->data == GINT_TO_POINTER (row))
3914         clist->selection_end = clist->selection_end->prev;
3915
3916       clist->selection = g_list_remove (clist->selection,
3917                                         GINT_TO_POINTER (row));
3918       
3919       if (CLIST_UNFROZEN (clist)
3920           && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3921         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3922     }
3923 }
3924
3925 static void
3926 real_select_all (GtkCMCList *clist)
3927 {
3928   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3929
3930   if (clist_has_grab (clist))
3931     return;
3932
3933   switch (clist->selection_mode)
3934     {
3935     case GTK_SELECTION_SINGLE:
3936     case GTK_SELECTION_BROWSE:
3937       return;
3938
3939     case GTK_SELECTION_MULTIPLE:
3940       g_list_free (clist->undo_selection);
3941       g_list_free (clist->undo_unselection);
3942       clist->undo_selection = NULL;
3943       clist->undo_unselection = NULL;
3944           
3945       if (clist->rows &&
3946           ((GtkCMCListRow *) (clist->row_list->data))->state !=
3947           GTK_STATE_SELECTED)
3948         fake_toggle_row (clist, 0);
3949
3950       clist->anchor_state =  GTK_STATE_SELECTED;
3951       clist->anchor = 0;
3952       clist->drag_pos = 0;
3953       clist->undo_anchor = clist->focus_row;
3954       update_extended_selection (clist, clist->rows);
3955       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3956       return;
3957     default:
3958       g_assert_not_reached ();
3959     }
3960 }
3961
3962 static void
3963 real_unselect_all (GtkCMCList *clist)
3964 {
3965   GList *list;
3966   gint i;
3967  
3968   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3969
3970   if (clist_has_grab (clist))
3971     return;
3972
3973   switch (clist->selection_mode)
3974     {
3975     case GTK_SELECTION_BROWSE:
3976       if (clist->focus_row >= 0)
3977         {
3978           g_signal_emit (G_OBJECT (clist),
3979                            clist_signals[SELECT_ROW], 0,
3980                            clist->focus_row, -1, NULL);
3981           return;
3982         }
3983       break;
3984     case GTK_SELECTION_MULTIPLE:
3985       g_list_free (clist->undo_selection);
3986       g_list_free (clist->undo_unselection);
3987       clist->undo_selection = NULL;
3988       clist->undo_unselection = NULL;
3989
3990       clist->anchor = -1;
3991       clist->drag_pos = -1;
3992       clist->undo_anchor = clist->focus_row;
3993       break;
3994     default:
3995       break;
3996     }
3997
3998   list = clist->selection;
3999   while (list)
4000     {
4001       i = GPOINTER_TO_INT (list->data);
4002       list = list->next;
4003       g_signal_emit (G_OBJECT (clist),
4004                        clist_signals[UNSELECT_ROW], 0, i, -1, NULL);
4005     }
4006 }
4007
4008 static void
4009 fake_unselect_all (GtkCMCList *clist,
4010                    gint      row)
4011 {
4012   GList *list;
4013   GList *work;
4014   gint i;
4015
4016   if (row >= 0 && (work = ROW_ELEMENT (clist, row)))
4017     {
4018       if (GTK_CMCLIST_ROW (work)->state == GTK_STATE_NORMAL &&
4019           GTK_CMCLIST_ROW (work)->selectable)
4020         {
4021           GTK_CMCLIST_ROW (work)->state = GTK_STATE_SELECTED;
4022           
4023           if (CLIST_UNFROZEN (clist) &&
4024               gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
4025             GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row,
4026                                                   GTK_CMCLIST_ROW (work));
4027         }  
4028     }
4029
4030   clist->undo_selection = clist->selection;
4031   clist->selection = NULL;
4032   clist->selection_end = NULL;
4033
4034   for (list = clist->undo_selection; list; list = list->next)
4035     {
4036       if ((i = GPOINTER_TO_INT (list->data)) == row ||
4037           !(work = g_list_nth (clist->row_list, i)))
4038         continue;
4039
4040       GTK_CMCLIST_ROW (work)->state = GTK_STATE_NORMAL;
4041       if (CLIST_UNFROZEN (clist) &&
4042           gtk_cmclist_row_is_visible (clist, i) != GTK_VISIBILITY_NONE)
4043         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, i,
4044                                               GTK_CMCLIST_ROW (work));
4045     }
4046 }
4047
4048 static void
4049 real_undo_selection (GtkCMCList *clist)
4050 {
4051   GList *work;
4052
4053   cm_return_if_fail (GTK_IS_CMCLIST (clist));
4054
4055   if (clist_has_grab (clist) ||
4056       clist->selection_mode != GTK_SELECTION_MULTIPLE)
4057     return;
4058
4059   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
4060
4061   if (!(clist->undo_selection || clist->undo_unselection))
4062     {
4063       gtk_cmclist_unselect_all (clist);
4064       return;
4065     }
4066
4067   for (work = clist->undo_selection; work; work = work->next)
4068     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
4069                      GPOINTER_TO_INT (work->data), -1, NULL);
4070
4071   for (work = clist->undo_unselection; work; work = work->next)
4072     {
4073       /* g_print ("unselect %d\n",GPOINTER_TO_INT (work->data)); */
4074       g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
4075                        GPOINTER_TO_INT (work->data), -1, NULL);
4076     }
4077
4078   if (gtk_widget_has_focus(GTK_WIDGET(clist)) && clist->focus_row != clist->undo_anchor)
4079     {
4080       gtk_cmclist_undraw_focus (GTK_WIDGET (clist));
4081       clist->focus_row = clist->undo_anchor;
4082       gtk_cmclist_draw_focus (GTK_WIDGET (clist));
4083     }
4084   else
4085     clist->focus_row = clist->undo_anchor;
4086   
4087   clist->undo_anchor = -1;
4088  
4089   g_list_free (clist->undo_selection);
4090   g_list_free (clist->undo_unselection);
4091   clist->undo_selection = NULL;
4092   clist->undo_unselection = NULL;
4093
4094   if (ROW_TOP_YPIXEL (clist, clist->focus_row) + clist->row_height >
4095       clist->clist_window_height)
4096     gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4097   else if (ROW_TOP_YPIXEL (clist, clist->focus_row) < 0)
4098     gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4099 }
4100
4101 static void
4102 set_anchor (GtkCMCList *clist,
4103             gboolean  add_mode,
4104             gint      anchor,
4105             gint      undo_anchor)
4106 {
4107   cm_return_if_fail (GTK_IS_CMCLIST (clist));
4108   
4109   if (clist->selection_mode != GTK_SELECTION_MULTIPLE || clist->anchor >= 0)
4110     return;
4111
4112   g_list_free (clist->undo_selection);
4113   g_list_free (clist->undo_unselection);
4114   clist->undo_selection = NULL;
4115   clist->undo_unselection = NULL;
4116
4117   if (add_mode)
4118     fake_toggle_row (clist, anchor);
4119   else
4120     {
4121       GTK_CMCLIST_GET_CLASS (clist)->fake_unselect_all (clist, anchor);
4122       clist->anchor_state = GTK_STATE_SELECTED;
4123     }
4124
4125   clist->anchor = anchor;
4126   clist->drag_pos = anchor;
4127   clist->undo_anchor = undo_anchor;
4128 }
4129
4130 static void
4131 resync_selection (GtkCMCList *clist,
4132                   GdkEvent *event)
4133 {
4134   gint i;
4135   gint e;
4136   gint row;
4137   GList *list;
4138   GtkCMCListRow *clist_row;
4139
4140   if (clist->selection_mode != GTK_SELECTION_MULTIPLE)
4141     return;
4142
4143   if (clist->anchor < 0 || clist->drag_pos < 0)
4144     return;
4145
4146   gtk_cmclist_freeze (clist);
4147
4148   i = MIN (clist->anchor, clist->drag_pos);
4149   e = MAX (clist->anchor, clist->drag_pos);
4150
4151   if (clist->undo_selection)
4152     {
4153       list = clist->selection;
4154       clist->selection = clist->undo_selection;
4155       clist->selection_end = g_list_last (clist->selection);
4156       clist->undo_selection = list;
4157       list = clist->selection;
4158       while (list)
4159         {
4160           row = GPOINTER_TO_INT (list->data);
4161           list = list->next;
4162           if (row < i || row > e)
4163             {
4164               clist_row = g_list_nth (clist->row_list, row)->data;
4165               if (clist_row->selectable)
4166                 {
4167                   clist_row->state = GTK_STATE_SELECTED;
4168                   g_signal_emit (G_OBJECT (clist),
4169                                    clist_signals[UNSELECT_ROW], 0,
4170                                    row, -1, event);
4171                   clist->undo_selection = g_list_prepend
4172                     (clist->undo_selection, GINT_TO_POINTER (row));
4173                 }
4174             }
4175         }
4176     }    
4177
4178   if (clist->anchor < clist->drag_pos)
4179     {
4180       for (list = g_list_nth (clist->row_list, i); i <= e;
4181            i++, list = list->next)
4182         if (GTK_CMCLIST_ROW (list)->selectable)
4183           {
4184             if (g_list_find (clist->selection, GINT_TO_POINTER(i)))
4185               {
4186                 if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_NORMAL)
4187                   {
4188                     GTK_CMCLIST_ROW (list)->state = GTK_STATE_SELECTED;
4189                     g_signal_emit (G_OBJECT (clist),
4190                                      clist_signals[UNSELECT_ROW], 0,
4191                                      i, -1, event);
4192                     clist->undo_selection =
4193                       g_list_prepend (clist->undo_selection,
4194                                       GINT_TO_POINTER (i));
4195                   }
4196               }
4197             else if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_SELECTED)
4198               {
4199                 GTK_CMCLIST_ROW (list)->state = GTK_STATE_NORMAL;
4200                 clist->undo_unselection =
4201                   g_list_prepend (clist->undo_unselection,
4202                                   GINT_TO_POINTER (i));
4203               }
4204           }
4205     }
4206   else
4207     {
4208       for (list = g_list_nth (clist->row_list, e); i <= e;
4209            e--, list = list->prev)
4210         if (GTK_CMCLIST_ROW (list)->selectable)
4211           {
4212             if (g_list_find (clist->selection, GINT_TO_POINTER(e)))
4213               {
4214                 if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_NORMAL)
4215                   {
4216                     GTK_CMCLIST_ROW (list)->state = GTK_STATE_SELECTED;
4217                     g_signal_emit (G_OBJECT (clist),
4218                                      clist_signals[UNSELECT_ROW], 0,
4219                                      e, -1, event);
4220                     clist->undo_selection =
4221                       g_list_prepend (clist->undo_selection,
4222                                       GINT_TO_POINTER (e));
4223                   }
4224               }
4225             else if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_SELECTED)
4226               {
4227                 GTK_CMCLIST_ROW (list)->state = GTK_STATE_NORMAL;
4228                 clist->undo_unselection =
4229                   g_list_prepend (clist->undo_unselection,
4230                                   GINT_TO_POINTER (e));
4231               }
4232           }
4233     }
4234   
4235   clist->undo_unselection = g_list_reverse (clist->undo_unselection);
4236   for (list = clist->undo_unselection; list; list = list->next)
4237     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
4238                      GPOINTER_TO_INT (list->data), -1, event);
4239
4240   clist->anchor = -1;
4241   clist->drag_pos = -1;
4242
4243   gtk_cmclist_thaw (clist);
4244 }
4245
4246 static void
4247 update_extended_selection (GtkCMCList *clist,
4248                            gint      row)
4249 {
4250   gint i;
4251   GList *list;
4252   GdkRectangle area;
4253   gint s1 = -1;
4254   gint s2 = -1;
4255   gint e1 = -1;
4256   gint e2 = -1;
4257   gint y1 = clist->clist_window_height;
4258   gint y2 = clist->clist_window_height;
4259   gint h1 = 0;
4260   gint h2 = 0;
4261   gint top;
4262
4263   if (clist->selection_mode != GTK_SELECTION_MULTIPLE || clist->anchor == -1)
4264     return;
4265
4266   if (row < 0)
4267     row = 0;
4268   if (row >= clist->rows)
4269     row = clist->rows - 1;
4270
4271   /* extending downwards */
4272   if (row > clist->drag_pos && clist->anchor <= clist->drag_pos)
4273     {
4274       s2 = clist->drag_pos + 1;
4275       e2 = row;
4276     }
4277   /* extending upwards */
4278   else if (row < clist->drag_pos && clist->anchor >= clist->drag_pos)
4279     {
4280       s2 = row;
4281       e2 = clist->drag_pos - 1;
4282     }
4283   else if (row < clist->drag_pos && clist->anchor < clist->drag_pos)
4284     {
4285       e1 = clist->drag_pos;
4286       /* row and drag_pos on different sides of anchor :
4287          take back the selection between anchor and drag_pos,
4288          select between anchor and row */
4289       if (row < clist->anchor)
4290         {
4291           s1 = clist->anchor + 1;
4292           s2 = row;
4293           e2 = clist->anchor - 1;
4294         }
4295       /* take back the selection between anchor and drag_pos */
4296       else
4297         s1 = row + 1;
4298     }
4299   else if (row > clist->drag_pos && clist->anchor > clist->drag_pos)
4300     {
4301       s1 = clist->drag_pos;
4302       /* row and drag_pos on different sides of anchor :
4303          take back the selection between anchor and drag_pos,
4304          select between anchor and row */
4305       if (row > clist->anchor)
4306         {
4307           e1 = clist->anchor - 1;
4308           s2 = clist->anchor + 1;
4309           e2 = row;
4310         }
4311       /* take back the selection between anchor and drag_pos */
4312       else
4313         e1 = row - 1;
4314     }
4315
4316   clist->drag_pos = row;
4317
4318   area.x = 0;
4319   area.width = clist->clist_window_width;
4320
4321   /* restore the elements between s1 and e1 */
4322   if (s1 >= 0)
4323     {
4324       for (i = s1,&nbs