remove all gtk3 conditionals
[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 };
179
180 /* GtkCMCList Methods */
181 static void     gtk_cmclist_class_init  (GtkCMCListClass         *klass);
182 static void     gtk_cmclist_init        (GtkCMCList              *clist);
183 static GObject* gtk_cmclist_constructor (GType                  type,
184                                        guint                  n_construct_properties,
185                                        GObjectConstructParam *construct_params);
186
187 /* GtkObject Methods */
188 static void gtk_cmclist_destroy  (GtkObject *object);
189 static void gtk_cmclist_finalize (GObject   *object);
190 static void gtk_cmclist_set_arg  (GObject *object,
191                                 guint      arg_id,
192                                 const GValue *value,
193                                 GParamSpec *spec);
194 static void gtk_cmclist_get_arg  (GObject *object,
195                                 guint      arg_id,
196                                 GValue *value,
197                                 GParamSpec *spec);
198
199 /* GtkWidget Methods */
200 static void gtk_cmclist_set_scroll_adjustments (GtkCMCList      *clist,
201                                               GtkAdjustment *hadjustment,
202                                               GtkAdjustment *vadjustment);
203 static void gtk_cmclist_realize         (GtkWidget        *widget);
204 static void gtk_cmclist_unrealize       (GtkWidget        *widget);
205 static void gtk_cmclist_map             (GtkWidget        *widget);
206 static void gtk_cmclist_unmap           (GtkWidget        *widget);
207 static gint gtk_cmclist_expose          (GtkWidget        *widget,
208                                          GdkEventExpose   *event);
209 static gint gtk_cmclist_button_press    (GtkWidget        *widget,
210                                        GdkEventButton   *event);
211 static gint gtk_cmclist_button_release  (GtkWidget        *widget,
212                                        GdkEventButton   *event);
213 static gint gtk_cmclist_motion          (GtkWidget        *widget, 
214                                        GdkEventMotion   *event);
215 static void gtk_cmclist_size_request    (GtkWidget        *widget,
216                                        GtkRequisition   *requisition);
217 static void gtk_cmclist_size_allocate   (GtkWidget        *widget,
218                                        GtkAllocation    *allocation);
219 static void gtk_cmclist_undraw_focus      (GtkWidget        *widget);
220 static void gtk_cmclist_draw_focus      (GtkWidget        *widget);
221 static gint gtk_cmclist_focus_in        (GtkWidget        *widget,
222                                        GdkEventFocus    *event);
223 static gint gtk_cmclist_focus_out       (GtkWidget        *widget,
224                                        GdkEventFocus    *event);
225 static gint gtk_cmclist_focus           (GtkWidget        *widget,
226                                        GtkDirectionType  direction);
227 static void gtk_cmclist_set_focus_child (GtkContainer     *container,
228                                        GtkWidget        *child);
229 static void gtk_cmclist_style_set       (GtkWidget        *widget,
230                                        GtkStyle         *previous_style);
231 static void gtk_cmclist_drag_begin      (GtkWidget        *widget,
232                                        GdkDragContext   *context);
233 static gint gtk_cmclist_drag_motion     (GtkWidget        *widget,
234                                        GdkDragContext   *context,
235                                        gint              x,
236                                        gint              y,
237                                        guint             time);
238 static void gtk_cmclist_drag_leave      (GtkWidget        *widget,
239                                        GdkDragContext   *context,
240                                        guint             time);
241 static void gtk_cmclist_drag_end        (GtkWidget        *widget,
242                                        GdkDragContext   *context);
243 static gboolean gtk_cmclist_drag_drop   (GtkWidget      *widget,
244                                        GdkDragContext *context,
245                                        gint            x,
246                                        gint            y,
247                                        guint           time);
248 static void gtk_cmclist_drag_data_get   (GtkWidget        *widget,
249                                        GdkDragContext   *context,
250                                        GtkSelectionData *selection_data,
251                                        guint             info,
252                                        guint             time);
253 static void gtk_cmclist_drag_data_received (GtkWidget        *widget,
254                                           GdkDragContext   *context,
255                                           gint              x,
256                                           gint              y,
257                                           GtkSelectionData *selection_data,
258                                           guint             info,
259                                           guint             time);
260
261 /* GtkContainer Methods */
262 static void gtk_cmclist_forall          (GtkContainer  *container,
263                                        gboolean       include_internals,
264                                        GtkCallback    callback,
265                                        gpointer       callback_data);
266
267 /* Selection */
268 static void toggle_row                (GtkCMCList      *clist,
269                                        gint           row,
270                                        gint           column,
271                                        GdkEvent      *event);
272 static void real_select_row           (GtkCMCList      *clist,
273                                        gint           row,
274                                        gint           column,
275                                        GdkEvent      *event);
276 static void real_unselect_row         (GtkCMCList      *clist,
277                                        gint           row,
278                                        gint           column,
279                                        GdkEvent      *event);
280 static void update_extended_selection (GtkCMCList      *clist,
281                                        gint           row);
282 static GList *selection_find          (GtkCMCList      *clist,
283                                        gint           row_number,
284                                        GList         *row_list_element);
285 static void real_select_all           (GtkCMCList      *clist);
286 static void real_unselect_all         (GtkCMCList      *clist);
287 static void move_vertical             (GtkCMCList      *clist,
288                                        gint           row,
289                                        gfloat         align);
290 static void move_horizontal           (GtkCMCList      *clist,
291                                        gint           diff);
292 static void real_undo_selection       (GtkCMCList      *clist);
293 static void fake_unselect_all         (GtkCMCList      *clist,
294                                        gint           row);
295 static void fake_toggle_row           (GtkCMCList      *clist,
296                                        gint           row);
297 static void resync_selection          (GtkCMCList      *clist,
298                                        GdkEvent      *event);
299 static void sync_selection            (GtkCMCList      *clist,
300                                        gint           row,
301                                        gint           mode);
302 static void set_anchor                (GtkCMCList      *clist,
303                                        gboolean       add_mode,
304                                        gint           anchor,
305                                        gint           undo_anchor);
306 static void start_selection           (GtkCMCList      *clist);
307 static void end_selection             (GtkCMCList      *clist);
308 static void toggle_add_mode           (GtkCMCList      *clist);
309 static void toggle_focus_row          (GtkCMCList      *clist);
310 static void extend_selection          (GtkCMCList      *clist,
311                                        GtkScrollType  scroll_type,
312                                        gfloat         position,
313                                        gboolean       auto_start_selection);
314 static gint get_selection_info        (GtkCMCList       *clist,
315                                        gint            x,
316                                        gint            y,
317                                        gint           *row,
318                                        gint           *column);
319
320 /* Scrolling */
321 static void move_focus_row     (GtkCMCList      *clist,
322                                 GtkScrollType  scroll_type,
323                                 gfloat         position);
324 static void scroll_horizontal  (GtkCMCList      *clist,
325                                 GtkScrollType  scroll_type,
326                                 gfloat         position);
327 static void scroll_vertical    (GtkCMCList      *clist,
328                                 GtkScrollType  scroll_type,
329                                 gfloat         position);
330 static void move_horizontal    (GtkCMCList      *clist,
331                                 gint           diff);
332 static void move_vertical      (GtkCMCList      *clist,
333                                 gint           row,
334                                 gfloat         align);
335 static gint horizontal_timeout (GtkCMCList      *clist);
336 static gint vertical_timeout   (GtkCMCList      *clist);
337 static void remove_grab        (GtkCMCList      *clist);
338
339
340 /* Resize Columns */
341 static void draw_xor_line             (GtkCMCList       *clist);
342 static gint new_column_width          (GtkCMCList       *clist,
343                                        gint            column,
344                                        gint           *x);
345 static void column_auto_resize        (GtkCMCList       *clist,
346                                        GtkCMCListRow    *clist_row,
347                                        gint            column,
348                                        gint            old_width);
349 static void real_resize_column        (GtkCMCList       *clist,
350                                        gint            column,
351                                        gint            width);
352 static void abort_column_resize       (GtkCMCList       *clist);
353 static void cell_size_request         (GtkCMCList       *clist,
354                                        GtkCMCListRow    *clist_row,
355                                        gint            column,
356                                        GtkRequisition *requisition);
357
358 /* Buttons */
359 static void column_button_create      (GtkCMCList       *clist,
360                                        gint            column);
361 static void column_button_clicked     (GtkWidget      *widget,
362                                        gpointer        data);
363
364 /* Adjustments */
365 static void adjust_adjustments        (GtkCMCList       *clist,
366                                        gboolean        block_resize);
367 static void vadjustment_value_changed (GtkAdjustment  *adjustment,
368                                        gpointer        data);
369 static void hadjustment_value_changed (GtkAdjustment  *adjustment,
370                                        gpointer        data);
371
372 /* Drawing */
373 static void get_cell_style   (GtkCMCList      *clist,
374                               GtkCMCListRow   *clist_row,
375                               gint           state,
376                               gint           column,
377                               GtkStyle     **style);
378 static gint draw_cell_pixbuf (GdkWindow     *window,
379                               GdkRectangle  *clip_rectangle,
380                               cairo_t       *cr,
381                               GdkPixbuf     *pixbuf,
382                               gint           x,
383                               gint           y,
384                               gint           width,
385                               gint           height);
386 static void draw_row         (GtkCMCList      *clist,
387                               GdkRectangle  *area,
388                               gint           row,
389                               GtkCMCListRow   *clist_row);
390 static void draw_rows        (GtkCMCList      *clist,
391                               GdkRectangle  *area);
392 static void clist_refresh    (GtkCMCList      *clist);
393      
394 /* Size Allocation / Requisition */
395 static void size_allocate_title_buttons (GtkCMCList *clist);
396 static void size_allocate_columns       (GtkCMCList *clist,
397                                          gboolean  block_resize);
398 static gint list_requisition_width      (GtkCMCList *clist);
399
400 /* Memory Allocation/Distruction Routines */
401 static GtkCMCListColumn *columns_new (GtkCMCList      *clist);
402 static void column_title_new       (GtkCMCList      *clist,
403                                     gint           column,
404                                     const gchar   *title);
405 static void columns_delete         (GtkCMCList      *clist);
406 static GtkCMCListRow *row_new        (GtkCMCList      *clist);
407 static void row_delete             (GtkCMCList      *clist,
408                                     GtkCMCListRow   *clist_row);
409 static void set_cell_contents      (GtkCMCList      *clist,
410                                     GtkCMCListRow   *clist_row,
411                                     gint           column,
412                                     GtkCMCellType    type,
413                                     const gchar   *text,
414                                     guint8         spacing,
415                                     GdkPixbuf     *pixbuf);
416 static gint real_insert_row        (GtkCMCList      *clist,
417                                     gint           row,
418                                     gchar         *text[]);
419 static void real_remove_row        (GtkCMCList      *clist,
420                                     gint           row);
421 static void real_clear             (GtkCMCList      *clist);
422
423 /* Sorting */
424 static gint default_compare        (GtkCMCList      *clist,
425                                     gconstpointer  row1,
426                                     gconstpointer  row2);
427 static void real_sort_list         (GtkCMCList      *clist);
428 static GList *gtk_cmclist_merge      (GtkCMCList      *clist,
429                                     GList         *a,
430                                     GList         *b);
431 static GList *gtk_cmclist_mergesort  (GtkCMCList      *clist,
432                                     GList         *list,
433                                     gint           num);
434 /* Misc */
435 static gboolean title_focus_in   (GtkCMCList *clist,
436                                   gint      dir);
437 static gboolean title_focus_move (GtkCMCList *clist,
438                                   gint      dir);
439
440 static void real_row_move             (GtkCMCList  *clist,
441                                        gint       source_row,
442                                        gint       dest_row);
443 static gint column_title_passive_func (GtkWidget *widget, 
444                                        GdkEvent  *event,
445                                        gpointer   data);
446 static void drag_dest_cell            (GtkCMCList         *clist,
447                                        gint              x,
448                                        gint              y,
449                                        GtkCMCListDestInfo *dest_info);
450
451
452
453 static guint clist_signals[LAST_SIGNAL] = {0};
454
455 static const GtkTargetEntry clist_target_table = { "gtk-clist-drag-reorder", 0, 0};
456
457 static gpointer gtk_cmclist_parent_class = NULL;
458
459 GType
460 gtk_cmclist_get_type (void)
461 {
462   static GType clist_type = 0;
463
464   if (!clist_type)
465     {
466       static const GTypeInfo clist_info =
467       {
468                         sizeof (GtkCMCListClass),
469
470                         (GBaseInitFunc) NULL,
471                         (GBaseFinalizeFunc) NULL,
472
473                         (GClassInitFunc) gtk_cmclist_class_init,
474                         (GClassFinalizeFunc) NULL,
475                         NULL,   /* class_data */
476
477                         sizeof (GtkCMCList),
478                         0,      /* n_preallocs */
479                         (GInstanceInitFunc) gtk_cmclist_init,
480
481                         (const GTypeValueTable *) NULL  /* value table */
482         };
483         clist_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkCMCList", &clist_info, (GTypeFlags)0);
484     }
485
486   return clist_type;
487 }
488
489 static void
490 gtk_cmclist_class_init (GtkCMCListClass *klass)
491 {
492   GObjectClass *object_class = G_OBJECT_CLASS (klass);
493   GtkObjectClass *gtk_object_class;
494   GtkWidgetClass *widget_class;
495   GtkContainerClass *container_class;
496   GtkBindingSet *binding_set;
497
498   object_class->constructor = gtk_cmclist_constructor;
499
500   gtk_object_class = (GtkObjectClass *) klass;
501   widget_class = (GtkWidgetClass *) klass;
502   container_class = (GtkContainerClass *) klass;
503
504   gtk_cmclist_parent_class = g_type_class_peek (GTK_TYPE_CONTAINER);
505
506   object_class->finalize = gtk_cmclist_finalize;
507   gtk_object_class->destroy = gtk_cmclist_destroy;
508   object_class->set_property = gtk_cmclist_set_arg;
509   object_class->get_property = gtk_cmclist_get_arg;
510   
511
512   widget_class->realize = gtk_cmclist_realize;
513   widget_class->unrealize = gtk_cmclist_unrealize;
514   widget_class->map = gtk_cmclist_map;
515   widget_class->unmap = gtk_cmclist_unmap;
516   widget_class->button_press_event = gtk_cmclist_button_press;
517   widget_class->button_release_event = gtk_cmclist_button_release;
518   widget_class->motion_notify_event = gtk_cmclist_motion;
519   widget_class->expose_event = gtk_cmclist_expose;
520   widget_class->size_request = gtk_cmclist_size_request;
521   widget_class->size_allocate = gtk_cmclist_size_allocate;
522   widget_class->focus_in_event = gtk_cmclist_focus_in;
523   widget_class->focus_out_event = gtk_cmclist_focus_out;
524   widget_class->style_set = gtk_cmclist_style_set;
525   widget_class->drag_begin = gtk_cmclist_drag_begin;
526   widget_class->drag_end = gtk_cmclist_drag_end;
527   widget_class->drag_motion = gtk_cmclist_drag_motion;
528   widget_class->drag_leave = gtk_cmclist_drag_leave;
529   widget_class->drag_drop = gtk_cmclist_drag_drop;
530   widget_class->drag_data_get = gtk_cmclist_drag_data_get;
531   widget_class->drag_data_received = gtk_cmclist_drag_data_received;
532   widget_class->focus = gtk_cmclist_focus;
533   
534   /* container_class->add = NULL; use the default GtkContainerClass warning */
535   /* container_class->remove=NULL; use the default GtkContainerClass warning */
536
537   container_class->forall = gtk_cmclist_forall;
538   container_class->set_focus_child = gtk_cmclist_set_focus_child;
539
540   klass->set_scroll_adjustments = gtk_cmclist_set_scroll_adjustments;
541   klass->refresh = clist_refresh;
542   klass->select_row = real_select_row;
543   klass->unselect_row = real_unselect_row;
544   klass->row_move = real_row_move;
545   klass->undo_selection = real_undo_selection;
546   klass->resync_selection = resync_selection;
547   klass->selection_find = selection_find;
548   klass->click_column = NULL;
549   klass->resize_column = real_resize_column;
550   klass->draw_row = draw_row;
551   klass->insert_row = real_insert_row;
552   klass->remove_row = real_remove_row;
553   klass->clear = real_clear;
554   klass->sort_list = real_sort_list;
555   klass->select_all = real_select_all;
556   klass->unselect_all = real_unselect_all;
557   klass->fake_unselect_all = fake_unselect_all;
558   klass->scroll_horizontal = scroll_horizontal;
559   klass->scroll_vertical = scroll_vertical;
560   klass->extend_selection = extend_selection;
561   klass->toggle_focus_row = toggle_focus_row;
562   klass->toggle_add_mode = toggle_add_mode;
563   klass->start_selection = start_selection;
564   klass->end_selection = end_selection;
565   klass->abort_column_resize = abort_column_resize;
566   klass->set_cell_contents = set_cell_contents;
567   klass->cell_size_request = cell_size_request;
568
569   g_object_class_install_property (object_class,
570                                 ARG_N_COLUMNS,
571                                 g_param_spec_uint ("n-columns",
572                                 "N-Columns",
573                                 "N-Columns",
574                                 1,
575                                 G_MAXINT,
576                                 1,
577                                 G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
578   g_object_class_install_property (object_class,
579                                 ARG_SHADOW_TYPE,
580                                 g_param_spec_enum ("shadow-type",
581                                 "shadow-type",
582                                 "shadow-type",
583                                 GTK_TYPE_SHADOW_TYPE, 0,
584                                 G_PARAM_READWRITE));
585   g_object_class_install_property (object_class,
586                                 ARG_SELECTION_MODE,
587                                 g_param_spec_enum ("selection-mode",
588                                 "selection-mode",
589                                 "selection-mode",
590                                 GTK_TYPE_SELECTION_MODE, 0,
591                                 G_PARAM_READWRITE));
592   g_object_class_install_property (object_class,
593                                 ARG_ROW_HEIGHT,
594                                 g_param_spec_uint ("row-height",
595                                 "row-height",
596                                 "row-height",
597                                 0,
598                                 G_MAXINT,
599                                 0,
600                                 G_PARAM_READWRITE));
601   g_object_class_install_property (object_class,
602                                 ARG_REORDERABLE,
603                                 g_param_spec_boolean ("reorderable",
604                                 "reorderable",
605                                 "reorderable",
606                                 TRUE,
607                                 G_PARAM_READWRITE));
608   g_object_class_install_property (object_class,
609                                 ARG_TITLES_ACTIVE,
610                                 g_param_spec_boolean ("titles-active",
611                                 "titles-active",
612                                 "titles-active",
613                                 TRUE,
614                                 G_PARAM_READWRITE));
615   g_object_class_install_property (object_class,
616                                 ARG_USE_DRAG_ICONS,
617                                 g_param_spec_boolean ("use-drag-icons",
618                                 "use-drag-icons",
619                                 "use-drag-icons",
620                                 TRUE,
621                                 G_PARAM_READWRITE));
622   g_object_class_install_property (object_class,
623                                 ARG_SORT_TYPE,
624                                 g_param_spec_enum ("sort-type",
625                                 "sort-type",
626                                 "sort-type",
627                                 GTK_TYPE_SORT_TYPE, 0,
628                                 G_PARAM_READWRITE));
629   widget_class->set_scroll_adjustments_signal =
630                 g_signal_new ("set_scroll_adjustments",
631                               G_TYPE_FROM_CLASS (object_class),
632                               G_SIGNAL_RUN_LAST,
633                               G_STRUCT_OFFSET (GtkCMCListClass, set_scroll_adjustments),
634                               NULL, NULL,
635                               claws_marshal_VOID__OBJECT_OBJECT,
636                               G_TYPE_NONE, 2,
637                               GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
638
639   clist_signals[SELECT_ROW] =
640                 g_signal_new ("select_row",
641                               G_TYPE_FROM_CLASS (object_class),
642                               G_SIGNAL_RUN_FIRST,
643                               G_STRUCT_OFFSET (GtkCMCListClass, select_row),
644                               NULL, NULL,
645                               claws_marshal_VOID__INT_INT_BOXED,
646                               G_TYPE_NONE, 3,
647                               G_TYPE_INT,
648                               G_TYPE_INT,
649                               GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
650   clist_signals[UNSELECT_ROW] =
651                 g_signal_new ("unselect_row",
652                               G_TYPE_FROM_CLASS (object_class),
653                               G_SIGNAL_RUN_FIRST,
654                               G_STRUCT_OFFSET (GtkCMCListClass, unselect_row),
655                               NULL, NULL,
656                               claws_marshal_VOID__INT_INT_BOXED,
657                               G_TYPE_NONE, 3,
658                               G_TYPE_INT,
659                               G_TYPE_INT,
660                               GDK_TYPE_EVENT);
661   clist_signals[ROW_MOVE] =
662                 g_signal_new ("row_move",
663                               G_TYPE_FROM_CLASS (object_class),
664                               G_SIGNAL_RUN_LAST,
665                               G_STRUCT_OFFSET (GtkCMCListClass, row_move),
666                               NULL, NULL,
667                               claws_marshal_VOID__INT_INT,
668                               G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
669   clist_signals[CLICK_COLUMN] =
670                 g_signal_new ("click_column",
671                               G_TYPE_FROM_CLASS (object_class),
672                               G_SIGNAL_RUN_FIRST,
673                               G_STRUCT_OFFSET (GtkCMCListClass, click_column),
674                               NULL, NULL,
675                               claws_marshal_VOID__INT,
676                               G_TYPE_NONE, 1, G_TYPE_INT);
677   clist_signals[RESIZE_COLUMN] =
678                 g_signal_new ("resize_column",
679                               G_TYPE_FROM_CLASS (object_class),
680                               G_SIGNAL_RUN_LAST,
681                               G_STRUCT_OFFSET (GtkCMCListClass, resize_column),
682                               NULL, NULL,
683                               claws_marshal_VOID__INT_INT,
684                               G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
685
686   clist_signals[TOGGLE_FOCUS_ROW] =
687                 g_signal_new ("toggle_focus_row",
688                               G_TYPE_FROM_CLASS (object_class),
689                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
690                               G_STRUCT_OFFSET (GtkCMCListClass, toggle_focus_row),
691                               NULL, NULL,
692                               claws_marshal_VOID__VOID,
693                               G_TYPE_NONE, 0);
694   clist_signals[SELECT_ALL] =
695                 g_signal_new ("select_all",
696                               G_TYPE_FROM_CLASS (object_class),
697                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
698                               G_STRUCT_OFFSET (GtkCMCListClass, select_all),
699                               NULL, NULL,
700                               claws_marshal_VOID__VOID,
701                               G_TYPE_NONE, 0);
702   clist_signals[UNSELECT_ALL] =
703                 g_signal_new ("unselect_all",
704                               G_TYPE_FROM_CLASS (object_class),
705                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
706                               G_STRUCT_OFFSET (GtkCMCListClass, unselect_all),
707                               NULL, NULL,
708                               claws_marshal_VOID__VOID,
709                               G_TYPE_NONE, 0);
710   clist_signals[UNDO_SELECTION] =
711                 g_signal_new ("undo_selection",
712                               G_TYPE_FROM_CLASS (object_class),
713                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
714                               G_STRUCT_OFFSET (GtkCMCListClass, undo_selection),
715                               NULL, NULL,
716                               claws_marshal_VOID__VOID,
717                               G_TYPE_NONE, 0);
718   clist_signals[START_SELECTION] =
719                 g_signal_new ("start_selection",
720                               G_TYPE_FROM_CLASS (object_class),
721                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
722                               G_STRUCT_OFFSET (GtkCMCListClass, start_selection),
723                               NULL, NULL,
724                               claws_marshal_VOID__VOID,
725                               G_TYPE_NONE, 0);
726   clist_signals[END_SELECTION] =
727                 g_signal_new ("end_selection",
728                               G_TYPE_FROM_CLASS (object_class),
729                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
730                               G_STRUCT_OFFSET (GtkCMCListClass, end_selection),
731                               NULL, NULL,
732                               claws_marshal_VOID__VOID,
733                               G_TYPE_NONE, 0);
734   clist_signals[TOGGLE_ADD_MODE] =
735                 g_signal_new ("toggle_add_mode",
736                               G_TYPE_FROM_CLASS (object_class),
737                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
738                               G_STRUCT_OFFSET (GtkCMCListClass, toggle_add_mode),
739                               NULL, NULL,
740                               claws_marshal_VOID__VOID,
741                               G_TYPE_NONE, 0);
742   clist_signals[EXTEND_SELECTION] =
743                 g_signal_new ("extend_selection",
744                               G_TYPE_FROM_CLASS (object_class),
745                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
746                               G_STRUCT_OFFSET (GtkCMCListClass, extend_selection),
747                               NULL, NULL,
748                               claws_marshal_VOID__ENUM_FLOAT_BOOLEAN,
749                               G_TYPE_NONE, 3, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT, G_TYPE_BOOLEAN);
750   clist_signals[SCROLL_VERTICAL] =
751                 g_signal_new ("scroll_vertical",
752                               G_TYPE_FROM_CLASS (object_class),
753                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
754                               G_STRUCT_OFFSET (GtkCMCListClass, scroll_vertical),
755                               NULL, NULL,
756                               claws_marshal_VOID__ENUM_FLOAT,
757                               G_TYPE_NONE, 2, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT);
758   clist_signals[SCROLL_HORIZONTAL] =
759                 g_signal_new ("scroll_horizontal",
760                               G_TYPE_FROM_CLASS (object_class),
761                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
762                               G_STRUCT_OFFSET (GtkCMCListClass, scroll_horizontal),
763                               NULL, NULL,
764                               claws_marshal_VOID__ENUM_FLOAT,
765                               G_TYPE_NONE, 2, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT);
766   clist_signals[ABORT_COLUMN_RESIZE] =
767                 g_signal_new ("abort_column_resize",
768                               G_TYPE_FROM_CLASS (object_class),
769                               G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
770                               G_STRUCT_OFFSET (GtkCMCListClass, abort_column_resize),
771                               NULL, NULL,
772                               claws_marshal_VOID__VOID,
773                               G_TYPE_NONE, 0);
774
775   binding_set = gtk_binding_set_by_class (klass);
776   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
777                                 "scroll_vertical", 2,
778                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
779                                 G_TYPE_FLOAT, 0.0);
780   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
781                                 "scroll_vertical", 2,
782                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
783                                 G_TYPE_FLOAT, 0.0);
784   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
785                                 "scroll_vertical", 2,
786                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
787                                 G_TYPE_FLOAT, 0.0);
788   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
789                                 "scroll_vertical", 2,
790                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
791                                 G_TYPE_FLOAT, 0.0);
792   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
793                                 "scroll_vertical", 2,
794                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
795                                 G_TYPE_FLOAT, 0.0);
796   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
797                                 "scroll_vertical", 2,
798                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
799                                 G_TYPE_FLOAT, 0.0);
800   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
801                                 "scroll_vertical", 2,
802                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
803                                 G_TYPE_FLOAT, 0.0);
804   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
805                                 "scroll_vertical", 2,
806                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
807                                 G_TYPE_FLOAT, 0.0);
808   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
809                                 "scroll_vertical", 2,
810                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
811                                 G_TYPE_FLOAT, 0.0);
812   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
813                                 "scroll_vertical", 2,
814                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
815                                 G_TYPE_FLOAT, 0.0);
816   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
817                                 "scroll_vertical", 2,
818                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
819                                 G_TYPE_FLOAT, 1.0);
820   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
821                                 "scroll_vertical", 2,
822                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
823                                 G_TYPE_FLOAT, 1.0);
824   
825   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_SHIFT_MASK,
826                                 "extend_selection", 3,
827                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
828                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
829   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_SHIFT_MASK,
830                                 "extend_selection", 3,
831                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
832                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
833   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_SHIFT_MASK,
834                                 "extend_selection", 3,
835                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
836                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
837   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_SHIFT_MASK,
838                                 "extend_selection", 3,
839                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
840                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
841   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, GDK_SHIFT_MASK,
842                                 "extend_selection", 3,
843                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
844                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
845   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, GDK_SHIFT_MASK,
846                                 "extend_selection", 3,
847                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
848                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
849   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, GDK_SHIFT_MASK,
850                                 "extend_selection", 3,
851                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
852                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
853   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, GDK_SHIFT_MASK,
854                                 "extend_selection", 3,
855                                 G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
856                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
857   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home,
858                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
859                                 "extend_selection", 3,
860                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
861                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
862   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home,
863                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
864                                 "extend_selection", 3,
865                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
866                                 G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
867   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End,
868                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
869                                 "extend_selection", 3,
870                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
871                                 G_TYPE_FLOAT, 1.0, G_TYPE_BOOLEAN, TRUE);
872   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End,
873                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
874                                 "extend_selection", 3,
875                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
876                                 G_TYPE_FLOAT, 1.0, G_TYPE_BOOLEAN, TRUE);
877
878   
879   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0,
880                                 "scroll_horizontal", 2,
881                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
882                                 G_TYPE_FLOAT, 0.0);
883   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0,
884                                 "scroll_horizontal", 2,
885                                 G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
886                                 G_TYPE_FLOAT, 0.0);
887   
888   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0,
889                                 "scroll_horizontal", 2,
890                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
891                                 G_TYPE_FLOAT, 0.0);
892   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0,
893                                 "scroll_horizontal", 2,
894                                 G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
895                                 G_TYPE_FLOAT, 0.0);
896
897   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
898                                 "scroll_horizontal", 2,
899                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
900                                 G_TYPE_FLOAT, 0.0);
901   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
902                                 "scroll_horizontal", 2,
903                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
904                                 G_TYPE_FLOAT, 0.0);
905   
906   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
907                                 "scroll_horizontal", 2,
908                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
909                                 G_TYPE_FLOAT, 1.0);
910
911   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
912                                 "scroll_horizontal", 2,
913                                 G_TYPE_ENUM, GTK_SCROLL_JUMP,
914                                 G_TYPE_FLOAT, 1.0);
915   
916   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
917                                 "undo_selection", 0);
918   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
919                                 "abort_column_resize", 0);
920   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
921                                 "toggle_focus_row", 0);
922   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
923                                 "toggle_focus_row", 0);  
924   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
925                                 "toggle_add_mode", 0);
926   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
927                                 "toggle_add_mode", 0);
928   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
929                                 "select_all", 0);
930   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Divide, GDK_CONTROL_MASK,
931                                 "select_all", 0);
932   gtk_binding_entry_add_signal (binding_set, '\\', GDK_CONTROL_MASK,
933                                 "unselect_all", 0);
934   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_L,
935                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK,
936                                 "end_selection", 0);
937   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_R,
938                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK,
939                                 "end_selection", 0);
940   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_L,
941                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK |
942                                 GDK_CONTROL_MASK,
943                                 "end_selection", 0);
944   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_R,
945                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK |
946                                 GDK_CONTROL_MASK,
947                                 "end_selection", 0);
948 }
949
950 static void
951 gtk_cmclist_set_arg (GObject *object,
952                                 guint      arg_id,
953                                 const GValue *value,
954                                 GParamSpec *spec)
955 {
956   GtkCMCList *clist;
957
958   clist = GTK_CMCLIST (object);
959
960   switch (arg_id)
961     {
962     case ARG_N_COLUMNS: /* only set at construction time */
963       clist->columns = MAX (1, g_value_get_uint (value));
964       break;
965     case ARG_SHADOW_TYPE:
966       gtk_cmclist_set_shadow_type (clist, g_value_get_enum (value));
967       break;
968     case ARG_SELECTION_MODE:
969       gtk_cmclist_set_selection_mode (clist, g_value_get_enum (value));
970       break;
971     case ARG_ROW_HEIGHT:
972       gtk_cmclist_set_row_height (clist, g_value_get_uint (value));
973       break;
974     case ARG_REORDERABLE:
975       gtk_cmclist_set_reorderable (clist, g_value_get_boolean (value));
976       break;
977     case ARG_TITLES_ACTIVE:
978       if (g_value_get_boolean (value))
979         gtk_cmclist_column_titles_active (clist);
980       else
981         gtk_cmclist_column_titles_passive (clist);
982       break;
983     case ARG_USE_DRAG_ICONS:
984       gtk_cmclist_set_use_drag_icons (clist, g_value_get_boolean (value));
985       break;
986     case ARG_SORT_TYPE:
987       gtk_cmclist_set_sort_type (clist, g_value_get_enum (value));
988       break;
989     }
990 }
991
992 static void
993 gtk_cmclist_get_arg (GObject *object,
994                                 guint      arg_id,
995                                 GValue *value,
996                                 GParamSpec *spec)
997 {
998   GtkCMCList *clist;
999
1000   clist = GTK_CMCLIST (object);
1001
1002   switch (arg_id)
1003     {
1004       guint i;
1005
1006     case ARG_N_COLUMNS:
1007       g_value_set_uint(value, clist->columns);
1008       break;
1009     case ARG_SHADOW_TYPE:
1010       g_value_set_enum(value, clist->shadow_type);
1011       break;
1012     case ARG_SELECTION_MODE:
1013       g_value_set_enum(value, clist->selection_mode);
1014       break;
1015     case ARG_ROW_HEIGHT:
1016       g_value_set_uint(value, GTK_CMCLIST_ROW_HEIGHT_SET(clist) ? clist->row_height : 0);
1017       break;
1018     case ARG_REORDERABLE:
1019       g_value_set_boolean(value, GTK_CMCLIST_REORDERABLE (clist));
1020       break;
1021     case ARG_TITLES_ACTIVE:
1022       g_value_set_boolean(value, TRUE);
1023       for (i = 0; i < clist->columns; i++)
1024         if (clist->column[i].button &&
1025             !gtk_widget_get_sensitive (clist->column[i].button))
1026           {
1027             g_value_set_boolean(value, FALSE);
1028             break;
1029           }
1030       break;
1031     case ARG_USE_DRAG_ICONS:
1032       g_value_set_boolean(value, GTK_CMCLIST_USE_DRAG_ICONS (clist));
1033       break;
1034     case ARG_SORT_TYPE:
1035       g_value_set_enum(value, clist->sort_type);
1036       break;
1037     default:
1038       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, spec);
1039       break;
1040     }
1041 }
1042
1043 static void
1044 gtk_cmclist_init (GtkCMCList *clist)
1045 {
1046   clist->flags = 0;
1047
1048   gtk_widget_set_has_window (GTK_WIDGET(clist), TRUE);
1049   gtk_widget_set_can_focus (GTK_WIDGET(clist), TRUE);
1050   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_DRAW_DRAG_LINE);
1051   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_USE_DRAG_ICONS);
1052
1053   clist->freeze_count = 0;
1054
1055   clist->rows = 0;
1056   clist->row_height = 0;
1057   clist->row_list = NULL;
1058   clist->row_list_end = NULL;
1059
1060   clist->columns = 0;
1061
1062   clist->title_window = NULL;
1063   clist->column_title_area.x = 0;
1064   clist->column_title_area.y = 0;
1065   clist->column_title_area.width = 1;
1066   clist->column_title_area.height = 1;
1067
1068   clist->clist_window = NULL;
1069   clist->clist_window_width = 1;
1070   clist->clist_window_height = 1;
1071
1072   clist->hoffset = 0;
1073   clist->voffset = 0;
1074
1075   clist->shadow_type = GTK_SHADOW_IN;
1076   clist->vadjustment = NULL;
1077   clist->hadjustment = NULL;
1078
1079   clist->button_actions[0] = GTK_CMBUTTON_SELECTS | GTK_CMBUTTON_DRAGS;
1080   clist->button_actions[1] = GTK_CMBUTTON_IGNORED;
1081   clist->button_actions[2] = GTK_CMBUTTON_IGNORED;
1082   clist->button_actions[3] = GTK_CMBUTTON_IGNORED;
1083   clist->button_actions[4] = GTK_CMBUTTON_IGNORED;
1084
1085   clist->cursor_drag = NULL;
1086   clist->x_drag = 0;
1087
1088   clist->selection_mode = GTK_SELECTION_SINGLE;
1089   clist->selection = NULL;
1090   clist->selection_end = NULL;
1091   clist->undo_selection = NULL;
1092   clist->undo_unselection = NULL;
1093
1094   clist->focus_row = -1;
1095   clist->focus_header_column = -1;
1096   clist->undo_anchor = -1;
1097
1098   clist->anchor = -1;
1099   clist->anchor_state = GTK_STATE_SELECTED;
1100   clist->drag_pos = -1;
1101   clist->htimer = 0;
1102   clist->vtimer = 0;
1103
1104   clist->click_cell.row = -1;
1105   clist->click_cell.column = -1;
1106
1107   clist->compare = default_compare;
1108   clist->sort_type = GTK_SORT_ASCENDING;
1109   clist->sort_column = 0;
1110
1111   clist->drag_highlight_row = -1;
1112 }
1113
1114 /* Constructor */
1115 static GObject*
1116 gtk_cmclist_constructor (GType                  type,
1117                        guint                  n_construct_properties,
1118                        GObjectConstructParam *construct_properties)
1119 {
1120   GObject *object = G_OBJECT_CLASS (gtk_cmclist_parent_class)->constructor (type,
1121                                                                 n_construct_properties,
1122                                                                 construct_properties);
1123   GtkCMCList *clist = GTK_CMCLIST (object);
1124   
1125   /* allocate memory for columns */
1126   clist->column = columns_new (clist);
1127   
1128   /* there needs to be at least one column button 
1129    * because there is alot of code that will break if it
1130    * isn't there
1131    */
1132   column_button_create (clist, 0);
1133   
1134   return object;
1135 }
1136
1137 /* GTKCLIST PUBLIC INTERFACE
1138  *   gtk_cmclist_new
1139  *   gtk_cmclist_new_with_titles
1140  *   gtk_cmclist_set_hadjustment
1141  *   gtk_cmclist_set_vadjustment
1142  *   gtk_cmclist_get_hadjustment
1143  *   gtk_cmclist_get_vadjustment
1144  *   gtk_cmclist_set_shadow_type
1145  *   gtk_cmclist_set_selection_mode
1146  *   gtk_cmclist_freeze
1147  *   gtk_cmclist_thaw
1148  */
1149 GtkWidget*
1150 gtk_cmclist_new (gint columns)
1151 {
1152   return gtk_cmclist_new_with_titles (columns, NULL);
1153 }
1154  
1155 GtkWidget*
1156 gtk_cmclist_new_with_titles (gint   columns,
1157                            gchar *titles[])
1158 {
1159   GtkCMCList *clist;
1160
1161   clist = g_object_new (GTK_TYPE_CMCLIST,
1162                         "n_columns", columns,
1163                         NULL);
1164   if (titles)
1165     {
1166       guint i;
1167
1168       for (i = 0; i < clist->columns; i++)
1169         gtk_cmclist_set_column_title (clist, i, titles[i]);
1170       gtk_cmclist_column_titles_show (clist);
1171     }
1172   else
1173     gtk_cmclist_column_titles_hide (clist);
1174
1175   return GTK_WIDGET (clist);
1176 }
1177
1178 void
1179 gtk_cmclist_set_hadjustment (GtkCMCList      *clist,
1180                            GtkAdjustment *adjustment)
1181 {
1182   GtkAdjustment *old_adjustment;
1183
1184   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1185   if (adjustment)
1186     cm_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1187   
1188   if (clist->hadjustment == adjustment)
1189     return;
1190   
1191   old_adjustment = clist->hadjustment;
1192
1193   if (clist->hadjustment)
1194     {
1195       g_signal_handlers_disconnect_matched(G_OBJECT (clist->hadjustment), G_SIGNAL_MATCH_DATA,
1196                         0, 0, 0, 0, clist);
1197
1198       g_object_unref (G_OBJECT (clist->hadjustment));
1199     }
1200
1201   clist->hadjustment = adjustment;
1202
1203   if (clist->hadjustment)
1204     {
1205       g_object_ref_sink (clist->hadjustment);
1206       g_signal_connect (G_OBJECT (clist->hadjustment), "value_changed",
1207                           G_CALLBACK( hadjustment_value_changed),
1208                           (gpointer) clist);
1209     }
1210
1211   if (!clist->hadjustment || !old_adjustment)
1212     gtk_widget_queue_resize (GTK_WIDGET (clist));
1213 }
1214
1215 GtkAdjustment *
1216 gtk_cmclist_get_hadjustment (GtkCMCList *clist)
1217 {
1218   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1219
1220   return clist->hadjustment;
1221 }
1222
1223 void
1224 gtk_cmclist_set_vadjustment (GtkCMCList      *clist,
1225                            GtkAdjustment *adjustment)
1226 {
1227   GtkAdjustment *old_adjustment;
1228
1229   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1230   if (adjustment)
1231     cm_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1232
1233   if (clist->vadjustment == adjustment)
1234     return;
1235   
1236   old_adjustment = clist->vadjustment;
1237
1238   if (clist->vadjustment)
1239     {
1240       g_signal_handlers_disconnect_matched(G_OBJECT (clist->vadjustment), G_SIGNAL_MATCH_DATA,
1241                         0, 0, 0, 0, clist);
1242       g_object_unref (G_OBJECT (clist->vadjustment));
1243     }
1244
1245   clist->vadjustment = adjustment;
1246
1247   if (clist->vadjustment)
1248     {
1249       g_object_ref_sink (clist->vadjustment);
1250
1251       g_signal_connect (G_OBJECT (clist->vadjustment), "value_changed",
1252                           G_CALLBACK(vadjustment_value_changed),
1253                           (gpointer) clist);
1254     }
1255
1256   if (!clist->vadjustment || !old_adjustment)
1257     gtk_widget_queue_resize (GTK_WIDGET (clist));
1258 }
1259
1260 GtkAdjustment *
1261 gtk_cmclist_get_vadjustment (GtkCMCList *clist)
1262 {
1263   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1264
1265   return clist->vadjustment;
1266 }
1267
1268 static void
1269 gtk_cmclist_set_scroll_adjustments (GtkCMCList      *clist,
1270                                   GtkAdjustment *hadjustment,
1271                                   GtkAdjustment *vadjustment)
1272 {
1273   if (clist->hadjustment != hadjustment)
1274     gtk_cmclist_set_hadjustment (clist, hadjustment);
1275   if (clist->vadjustment != vadjustment)
1276     gtk_cmclist_set_vadjustment (clist, vadjustment);
1277 }
1278
1279 void
1280 gtk_cmclist_set_shadow_type (GtkCMCList      *clist,
1281                            GtkShadowType  type)
1282 {
1283   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1284
1285   clist->shadow_type = type;
1286
1287   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1288     gtk_widget_queue_resize (GTK_WIDGET (clist));
1289 }
1290
1291 void
1292 gtk_cmclist_set_selection_mode (GtkCMCList         *clist,
1293                               GtkSelectionMode  mode)
1294 {
1295   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1296   cm_return_if_fail (mode != GTK_SELECTION_NONE);
1297
1298   if (mode == clist->selection_mode)
1299     return;
1300
1301   clist->selection_mode = mode;
1302   clist->anchor = -1;
1303   clist->anchor_state = GTK_STATE_SELECTED;
1304   clist->drag_pos = -1;
1305   clist->undo_anchor = clist->focus_row;
1306
1307   g_list_free (clist->undo_selection);
1308   g_list_free (clist->undo_unselection);
1309   clist->undo_selection = NULL;
1310   clist->undo_unselection = NULL;
1311
1312   switch (mode)
1313     {
1314     case GTK_SELECTION_MULTIPLE:
1315       return;
1316     case GTK_SELECTION_BROWSE:
1317     case GTK_SELECTION_SINGLE:
1318       gtk_cmclist_unselect_all (clist);
1319       break;
1320     default:
1321       /* Someone set it by hand */
1322       g_assert_not_reached ();
1323     }
1324 }
1325
1326 void
1327 gtk_cmclist_freeze (GtkCMCList *clist)
1328 {
1329   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1330
1331   clist->freeze_count++;
1332 }
1333
1334 void
1335 gtk_cmclist_thaw (GtkCMCList *clist)
1336 {
1337   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1338
1339   if (clist->freeze_count)
1340     {
1341       clist->freeze_count--;
1342       CLIST_REFRESH (clist);
1343     }
1344 }
1345
1346 /* PUBLIC COLUMN FUNCTIONS
1347  *   gtk_cmclist_column_titles_show
1348  *   gtk_cmclist_column_titles_hide
1349  *   gtk_cmclist_column_title_active
1350  *   gtk_cmclist_column_title_passive
1351  *   gtk_cmclist_column_titles_active
1352  *   gtk_cmclist_column_titles_passive
1353  *   gtk_cmclist_set_column_title
1354  *   gtk_cmclist_get_column_title
1355  *   gtk_cmclist_set_column_widget
1356  *   gtk_cmclist_set_column_justification
1357  *   gtk_cmclist_set_column_visibility
1358  *   gtk_cmclist_set_column_resizeable
1359  *   gtk_cmclist_set_column_auto_resize
1360  *   gtk_cmclist_optimal_column_width
1361  *   gtk_cmclist_set_column_width
1362  *   gtk_cmclist_set_column_min_width
1363  *   gtk_cmclist_set_column_max_width
1364  */
1365 void
1366 gtk_cmclist_column_titles_show (GtkCMCList *clist)
1367 {
1368   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1369
1370   if (!GTK_CMCLIST_SHOW_TITLES(clist))
1371     {
1372       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_SHOW_TITLES);
1373       if (clist->title_window)
1374         gdk_window_show (clist->title_window);
1375       gtk_widget_queue_resize (GTK_WIDGET (clist));
1376     }
1377 }
1378
1379 void 
1380 gtk_cmclist_column_titles_hide (GtkCMCList *clist)
1381 {
1382   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1383
1384   if (GTK_CMCLIST_SHOW_TITLES(clist))
1385     {
1386       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_SHOW_TITLES);
1387       if (clist->title_window)
1388         gdk_window_hide (clist->title_window);
1389       gtk_widget_queue_resize (GTK_WIDGET (clist));
1390     }
1391 }
1392
1393 void
1394 gtk_cmclist_column_title_active (GtkCMCList *clist,
1395                                gint      column)
1396 {
1397   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1398
1399   if (column < 0 || column >= clist->columns)
1400     return;
1401   if (!clist->column[column].button || !clist->column[column].button_passive)
1402     return;
1403
1404   clist->column[column].button_passive = FALSE;
1405
1406   g_signal_handlers_disconnect_matched(G_OBJECT (clist->column[column].button), G_SIGNAL_MATCH_FUNC,
1407                     0, 0, 0, column_title_passive_func, 0);
1408
1409   gtk_widget_set_can_focus (clist->column[column].button, TRUE);
1410   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1411     gtk_widget_queue_draw (clist->column[column].button);
1412 }
1413
1414 void
1415 gtk_cmclist_column_title_passive (GtkCMCList *clist,
1416                                 gint      column)
1417 {
1418   GtkToggleButton *button;
1419
1420   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1421
1422   if (column < 0 || column >= clist->columns)
1423     return;
1424   if (!clist->column[column].button || clist->column[column].button_passive)
1425     return;
1426
1427   button = GTK_TOGGLE_BUTTON (clist->column[column].button);
1428
1429   clist->column[column].button_passive = TRUE;
1430
1431   if (gtk_toggle_button_get_active(button))
1432         g_signal_connect(G_OBJECT (clist->column[column].button),
1433                          "button-release-event",
1434                          G_CALLBACK(column_title_passive_func),
1435                          NULL);
1436   if (gtk_widget_is_focus(gtk_bin_get_child(GTK_BIN(button))))
1437         g_signal_connect(G_OBJECT (clist->column[column].button),
1438                          "leave-notify-event",
1439                          G_CALLBACK(column_title_passive_func),
1440                          NULL);
1441
1442   g_signal_connect (G_OBJECT (clist->column[column].button), "event",
1443                       G_CALLBACK(column_title_passive_func), NULL);
1444
1445   gtk_widget_set_can_focus (clist->column[column].button, FALSE);
1446   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1447     gtk_widget_queue_draw (clist->column[column].button);
1448 }
1449
1450 void
1451 gtk_cmclist_column_titles_active (GtkCMCList *clist)
1452 {
1453   gint i;
1454
1455   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1456
1457   for (i = 0; i < clist->columns; i++)
1458     gtk_cmclist_column_title_active (clist, i);
1459 }
1460
1461 void
1462 gtk_cmclist_column_titles_passive (GtkCMCList *clist)
1463 {
1464   gint i;
1465
1466   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1467
1468   for (i = 0; i < clist->columns; i++)
1469     gtk_cmclist_column_title_passive (clist, i);
1470 }
1471
1472 void
1473 gtk_cmclist_set_column_title (GtkCMCList    *clist,
1474                             gint         column,
1475                             const gchar *title)
1476 {
1477   gint new_button = 0;
1478   GtkWidget *old_widget;
1479   GtkWidget *alignment = NULL;
1480   GtkWidget *label;
1481
1482   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1483
1484   if (column < 0 || column >= clist->columns)
1485     return;
1486
1487   /* if the column button doesn't currently exist,
1488    * it has to be created first */
1489   if (!clist->column[column].button)
1490     {
1491       column_button_create (clist, column);
1492       new_button = 1;
1493     }
1494
1495   column_title_new (clist, column, title);
1496
1497   /* remove and destroy the old widget */
1498   old_widget = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1499   if (old_widget)
1500     gtk_container_remove (GTK_CONTAINER (clist->column[column].button), old_widget);
1501
1502   /* create new alignment based no column justification */
1503   switch (clist->column[column].justification)
1504     {
1505     case GTK_JUSTIFY_LEFT:
1506       alignment = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
1507       break;
1508
1509     case GTK_JUSTIFY_RIGHT:
1510       alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
1511       break;
1512
1513     case GTK_JUSTIFY_CENTER:
1514       alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1515       break;
1516
1517     case GTK_JUSTIFY_FILL:
1518       alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1519       break;
1520     }
1521
1522   gtk_widget_push_composite_child ();
1523   label = gtk_label_new (clist->column[column].title);
1524   gtk_widget_pop_composite_child ();
1525   gtk_container_add (GTK_CONTAINER (alignment), label);
1526   gtk_container_add (GTK_CONTAINER (clist->column[column].button), alignment);
1527   gtk_widget_show (label);
1528   gtk_widget_show (alignment);
1529
1530   /* if this button didn't previously exist, then the
1531    * column button positions have to be re-computed */
1532   if (gtk_widget_get_visible (GTK_WIDGET(clist)) && new_button)
1533     size_allocate_title_buttons (clist);
1534 }
1535
1536 gchar *
1537 gtk_cmclist_get_column_title (GtkCMCList *clist,
1538                             gint      column)
1539 {
1540   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1541
1542   if (column < 0 || column >= clist->columns)
1543     return NULL;
1544
1545   return clist->column[column].title;
1546 }
1547
1548 void
1549 gtk_cmclist_set_column_widget (GtkCMCList  *clist,
1550                              gint       column,
1551                              GtkWidget *widget)
1552 {
1553   gint new_button = 0;
1554   GtkWidget *old_widget;
1555
1556   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1557
1558   if (column < 0 || column >= clist->columns)
1559     return;
1560
1561   /* if the column button doesn't currently exist,
1562    * it has to be created first */
1563   if (!clist->column[column].button)
1564     {
1565       column_button_create (clist, column);
1566       new_button = 1;
1567     }
1568
1569   column_title_new (clist, column, NULL);
1570
1571   /* remove and destroy the old widget */
1572   old_widget = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1573   if (old_widget)
1574     gtk_container_remove (GTK_CONTAINER (clist->column[column].button),
1575                           old_widget);
1576
1577   /* add and show the widget */
1578   if (widget)
1579     {
1580       gtk_container_add (GTK_CONTAINER (clist->column[column].button), widget);
1581       gtk_widget_show (widget);
1582     }
1583
1584   /* if this button didn't previously exist, then the
1585    * column button positions have to be re-computed */
1586   if (gtk_widget_get_visible (GTK_WIDGET(clist)) && new_button)
1587     size_allocate_title_buttons (clist);
1588 }
1589
1590 GtkWidget *
1591 gtk_cmclist_get_column_widget (GtkCMCList *clist,
1592                              gint      column)
1593 {
1594   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1595
1596   if (column < 0 || column >= clist->columns)
1597     return NULL;
1598
1599   if (clist->column[column].button)
1600         return gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1601
1602   return NULL;
1603 }
1604
1605 void
1606 gtk_cmclist_set_column_justification (GtkCMCList         *clist,
1607                                     gint              column,
1608                                     GtkJustification  justification)
1609 {
1610   GtkWidget *alignment;
1611
1612   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1613
1614   if (column < 0 || column >= clist->columns)
1615     return;
1616
1617   clist->column[column].justification = justification;
1618
1619   /* change the alinment of the button title if it's not a
1620    * custom widget */
1621   if (clist->column[column].title)
1622     {
1623       alignment = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1624
1625       switch (clist->column[column].justification)
1626         {
1627         case GTK_JUSTIFY_LEFT:
1628           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.0, 0.5, 0.0, 0.0);
1629           break;
1630
1631         case GTK_JUSTIFY_RIGHT:
1632           gtk_alignment_set (GTK_ALIGNMENT (alignment), 1.0, 0.5, 0.0, 0.0);
1633           break;
1634
1635         case GTK_JUSTIFY_CENTER:
1636           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1637           break;
1638
1639         case GTK_JUSTIFY_FILL:
1640           gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1641           break;
1642
1643         default:
1644           break;
1645         }
1646     }
1647
1648   if (CLIST_UNFROZEN (clist))
1649     draw_rows (clist, NULL);
1650 }
1651
1652 void
1653 gtk_cmclist_set_column_visibility (GtkCMCList *clist,
1654                                  gint      column,
1655                                  gboolean  visible)
1656 {
1657   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1658
1659   if (column < 0 || column >= clist->columns)
1660     return;
1661   if (clist->column[column].visible == visible)
1662     return;
1663
1664   /* don't hide last visible column */
1665   if (!visible)
1666     {
1667       gint i;
1668       gint vis_columns = 0;
1669
1670       for (i = 0, vis_columns = 0; i < clist->columns && vis_columns < 2; i++)
1671         if (clist->column[i].visible)
1672           vis_columns++;
1673
1674       if (vis_columns < 2)
1675         return;
1676     }
1677
1678   clist->column[column].visible = visible;
1679
1680   if (clist->column[column].button)
1681     {
1682       if (visible)
1683         gtk_widget_show (clist->column[column].button);
1684       else
1685         gtk_widget_hide (clist->column[column].button);
1686     }
1687   
1688   gtk_widget_queue_resize (GTK_WIDGET(clist));
1689 }
1690
1691 void
1692 gtk_cmclist_set_column_resizeable (GtkCMCList *clist,
1693                                  gint      column,
1694                                  gboolean  resizeable)
1695 {
1696   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1697
1698   if (column < 0 || column >= clist->columns)
1699     return;
1700   if (clist->column[column].resizeable == resizeable)
1701     return;
1702
1703   clist->column[column].resizeable = resizeable;
1704   if (resizeable)
1705     clist->column[column].auto_resize = FALSE;
1706
1707   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1708     size_allocate_title_buttons (clist);
1709 }
1710
1711 void
1712 gtk_cmclist_set_column_auto_resize (GtkCMCList *clist,
1713                                   gint      column,
1714                                   gboolean  auto_resize)
1715 {
1716   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1717
1718   if (column < 0 || column >= clist->columns)
1719     return;
1720   if (clist->column[column].auto_resize == auto_resize)
1721     return;
1722
1723   clist->column[column].auto_resize = auto_resize;
1724   if (auto_resize)
1725     {
1726       clist->column[column].resizeable = FALSE;
1727       if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1728         {
1729           gint width;
1730
1731           width = gtk_cmclist_optimal_column_width (clist, column);
1732           gtk_cmclist_set_column_width (clist, column, width);
1733         }
1734     }
1735
1736   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1737     size_allocate_title_buttons (clist);
1738 }
1739
1740 gint
1741 gtk_cmclist_columns_autosize (GtkCMCList *clist)
1742 {
1743   gint i;
1744   gint width;
1745
1746   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
1747
1748   gtk_cmclist_freeze (clist);
1749   width = 0;
1750   for (i = 0; i < clist->columns; i++)
1751     {
1752       gtk_cmclist_set_column_width (clist, i,
1753                                   gtk_cmclist_optimal_column_width (clist, i));
1754
1755       width += clist->column[i].width;
1756     }
1757
1758   gtk_cmclist_thaw (clist);
1759   return width;
1760 }
1761
1762 gint
1763 gtk_cmclist_optimal_column_width (GtkCMCList *clist,
1764                                 gint      column)
1765 {
1766   GtkRequisition requisition;
1767   GList *list;
1768   gint width;
1769
1770   cm_return_val_if_fail (GTK_CMCLIST (clist), 0);
1771
1772   if (column < 0 || column >= clist->columns)
1773     return 0;
1774
1775   if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
1776     {
1777       gtk_widget_get_requisition (clist->column[column].button, &requisition);
1778       width = requisition.width
1779 #if 0
1780              (CELL_SPACING + (2 * COLUMN_INSET)))
1781 #endif
1782                 ;
1783     }
1784   else
1785     width = 0;
1786
1787   for (list = clist->row_list; list; list = list->next)
1788     {
1789   GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1790         (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1791       width = MAX (width, requisition.width);
1792     }
1793
1794   return width;
1795 }
1796
1797 void
1798 gtk_cmclist_set_column_width (GtkCMCList *clist,
1799                             gint      column,
1800                             gint      width)
1801 {
1802   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1803
1804   if (column < 0 || column >= clist->columns)
1805     return;
1806
1807   g_signal_emit (G_OBJECT (clist), clist_signals[RESIZE_COLUMN], 0,
1808                    column, width);
1809 }
1810
1811 void
1812 gtk_cmclist_set_column_min_width (GtkCMCList *clist,
1813                                 gint      column,
1814                                 gint      min_width)
1815 {
1816   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1817
1818   if (column < 0 || column >= clist->columns)
1819     return;
1820   if (clist->column[column].min_width == min_width)
1821     return;
1822
1823   if (clist->column[column].max_width >= 0  &&
1824       clist->column[column].max_width < min_width)
1825     clist->column[column].min_width = clist->column[column].max_width;
1826   else
1827     clist->column[column].min_width = min_width;
1828
1829   if (clist->column[column].area.width < clist->column[column].min_width)
1830     gtk_cmclist_set_column_width (clist, column,clist->column[column].min_width);
1831 }
1832
1833 void
1834 gtk_cmclist_set_column_max_width (GtkCMCList *clist,
1835                                 gint      column,
1836                                 gint      max_width)
1837 {
1838   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1839
1840   if (column < 0 || column >= clist->columns)
1841     return;
1842   if (clist->column[column].max_width == max_width)
1843     return;
1844
1845   if (clist->column[column].min_width >= 0 && max_width >= 0 &&
1846       clist->column[column].min_width > max_width)
1847     clist->column[column].max_width = clist->column[column].min_width;
1848   else
1849     clist->column[column].max_width = max_width;
1850   
1851   if (clist->column[column].area.width > clist->column[column].max_width)
1852     gtk_cmclist_set_column_width (clist, column,clist->column[column].max_width);
1853 }
1854
1855 /* PRIVATE COLUMN FUNCTIONS
1856  *   column_auto_resize
1857  *   real_resize_column
1858  *   abort_column_resize
1859  *   size_allocate_title_buttons
1860  *   size_allocate_columns
1861  *   list_requisition_width
1862  *   new_column_width
1863  *   column_button_create
1864  *   column_button_clicked
1865  *   column_title_passive_func
1866  */
1867 static void
1868 column_auto_resize (GtkCMCList    *clist,
1869                     GtkCMCListRow *clist_row,
1870                     gint         column,
1871                     gint         old_width)
1872 {
1873   /* resize column if needed for auto_resize */
1874   GtkRequisition requisition;
1875
1876   if (!clist->column[column].auto_resize ||
1877       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1878     return;
1879
1880   if (clist_row)
1881     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
1882                                                    column, &requisition);
1883   else
1884     requisition.width = 0;
1885
1886   if (requisition.width > clist->column[column].width)
1887     gtk_cmclist_set_column_width (clist, column, requisition.width);
1888   else if (requisition.width < old_width &&
1889            old_width == clist->column[column].width)
1890     {
1891       GList *list;
1892       gint new_width = 0;
1893
1894       /* run a "gtk_cmclist_optimal_column_width" but break, if
1895        * the column doesn't shrink */
1896       if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
1897     {
1898         gtk_widget_get_requisition (clist->column[column].button, &requisition);
1899         new_width = (requisition.width -
1900                      (CELL_SPACING + (2 * COLUMN_INSET)));
1901     }
1902       else
1903         new_width = 0;
1904
1905       for (list = clist->row_list; list; list = list->next)
1906         {
1907           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1908             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1909           new_width = MAX (new_width, requisition.width);
1910           if (new_width == clist->column[column].width)
1911             break;
1912         }
1913       if (new_width < clist->column[column].width)
1914         gtk_cmclist_set_column_width
1915           (clist, column, MAX (new_width, clist->column[column].min_width));
1916     }
1917 }
1918
1919 static void
1920 real_resize_column (GtkCMCList *clist,
1921                     gint      column,
1922                     gint      width)
1923 {
1924   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1925
1926   if (column < 0 || column >= clist->columns)
1927     return;
1928   
1929   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
1930     width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
1931   if (clist->column[column].max_width >= 0 &&
1932       width > clist->column[column].max_width)
1933     width = clist->column[column].max_width;
1934
1935   clist->column[column].width = width;
1936   clist->column[column].width_set = TRUE;
1937
1938   /* FIXME: this is quite expensive to do if the widget hasn't
1939    *        been size_allocated yet, and pointless. Should
1940    *        a flag be kept
1941    */
1942   size_allocate_columns (clist, TRUE);
1943   size_allocate_title_buttons (clist);
1944
1945   CLIST_REFRESH (clist);
1946 }
1947
1948 static void
1949 abort_column_resize (GtkCMCList *clist)
1950 {
1951   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1952
1953   if (!GTK_CMCLIST_IN_DRAG(clist))
1954     return;
1955
1956   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_IN_DRAG);
1957   gtk_grab_remove (GTK_WIDGET (clist));
1958   gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (clist)),
1959                               GDK_CURRENT_TIME);
1960   clist->drag_pos = -1;
1961
1962   if (clist->x_drag >= 0 && clist->x_drag <= clist->clist_window_width - 1)
1963     clist_refresh(clist);
1964 }
1965
1966 static void
1967 size_allocate_title_buttons (GtkCMCList *clist)
1968 {
1969   GtkAllocation button_allocation;
1970   gint last_column;
1971   gint last_button = 0;
1972   gint i;
1973
1974   if (!gtk_widget_get_realized (GTK_WIDGET(clist)))
1975     return;
1976
1977   button_allocation.x = clist->hoffset;
1978   button_allocation.y = 0;
1979   button_allocation.width = 0;
1980   button_allocation.height = clist->column_title_area.height;
1981
1982   /* find last visible column */
1983   for (last_column = clist->columns - 1; last_column >= 0; last_column--)
1984     if (clist->column[last_column].visible)
1985       break;
1986
1987   for (i = 0; i < last_column; i++)
1988     {
1989       if (!clist->column[i].visible)
1990         {
1991           last_button = i + 1;
1992           gdk_window_hide (clist->column[i].window);
1993           continue;
1994         }
1995
1996       button_allocation.width += (clist->column[i].area.width +
1997                                   CELL_SPACING + 2 * COLUMN_INSET);
1998
1999       if (!clist->column[i + 1].button)
2000         {
2001           gdk_window_hide (clist->column[i].window);
2002           continue;
2003         }
2004
2005       gtk_widget_size_allocate (clist->column[last_button].button,
2006                                 &button_allocation);
2007       button_allocation.x += button_allocation.width;
2008       button_allocation.width = 0;
2009
2010       if (clist->column[last_button].resizeable)
2011         {
2012           gdk_window_show (clist->column[last_button].window);
2013           gdk_window_move_resize (clist->column[last_button].window,
2014                                   button_allocation.x - (DRAG_WIDTH / 2), 
2015                                   0, DRAG_WIDTH,
2016                                   clist->column_title_area.height);
2017         }
2018       else
2019         gdk_window_hide (clist->column[last_button].window);
2020
2021       last_button = i + 1;
2022     }
2023
2024   button_allocation.width += (clist->column[last_column].area.width +
2025                               2 * (CELL_SPACING + COLUMN_INSET));
2026   gtk_widget_size_allocate (clist->column[last_button].button,
2027                             &button_allocation);
2028
2029   if (clist->column[last_button].resizeable)
2030     {
2031       button_allocation.x += button_allocation.width;
2032
2033       gdk_window_show (clist->column[last_button].window);
2034       gdk_window_move_resize (clist->column[last_button].window,
2035                               button_allocation.x - (DRAG_WIDTH / 2), 
2036                               0, DRAG_WIDTH, clist->column_title_area.height);
2037     }
2038   else
2039     gdk_window_hide (clist->column[last_button].window);
2040 }
2041
2042 static void
2043 size_allocate_columns (GtkCMCList *clist,
2044                        gboolean  block_resize)
2045 {
2046   GtkRequisition requisition;
2047   gint xoffset = CELL_SPACING + COLUMN_INSET;
2048   gint last_column;
2049   gint i;
2050
2051   /* find last visible column and calculate correct column width */
2052   for (last_column = clist->columns - 1;
2053        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2054
2055   if (last_column < 0)
2056     return;
2057
2058   for (i = 0; i <= last_column; i++)
2059     {
2060       if (!clist->column[i].visible)
2061         continue;
2062       clist->column[i].area.x = xoffset;
2063       if (clist->column[i].width_set)
2064         {
2065           if (!block_resize && GTK_CMCLIST_SHOW_TITLES(clist) &&
2066               clist->column[i].auto_resize && clist->column[i].button)
2067             {
2068               gint width;
2069
2070               gtk_widget_get_requisition (clist->column[i].button, &requisition);
2071               width = (requisition.width -
2072                        (CELL_SPACING + (2 * COLUMN_INSET)));
2073
2074               if (width > clist->column[i].width)
2075                 gtk_cmclist_set_column_width (clist, i, width);
2076             }
2077
2078           clist->column[i].area.width = clist->column[i].width;
2079           xoffset += clist->column[i].width + CELL_SPACING + (2* COLUMN_INSET);
2080         }
2081       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2082         {
2083           gtk_widget_get_requisition (clist->column[i].button, &requisition);
2084           clist->column[i].area.width =
2085             requisition.width -
2086             (CELL_SPACING + (2 * COLUMN_INSET));
2087           xoffset += requisition.width;
2088         }
2089     }
2090
2091   clist->column[last_column].area.width = clist->column[last_column].area.width
2092     + MAX (0, clist->clist_window_width + COLUMN_INSET - xoffset);
2093 }
2094
2095 static gint
2096 list_requisition_width (GtkCMCList *clist) 
2097 {
2098   GtkRequisition requisition;
2099   gint width = CELL_SPACING;
2100   gint i;
2101
2102   for (i = clist->columns - 1; i >= 0; i--)
2103     {
2104       if (!clist->column[i].visible)
2105         continue;
2106
2107       if (clist->column[i].width_set)
2108         width += clist->column[i].width + CELL_SPACING + (2 * COLUMN_INSET);
2109       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2110     {
2111         gtk_widget_get_requisition (clist->column[i].button, &requisition);
2112         width += requisition.width;
2113     }
2114     }
2115
2116   return width;
2117 }
2118
2119 /* this function returns the new width of the column being resized given
2120  * the column and x position of the cursor; the x cursor position is passed
2121  * in as a pointer and automagicly corrected if it's beyond min/max limits */
2122 static gint
2123 new_column_width (GtkCMCList *clist,
2124                   gint      column,
2125                   gint     *x)
2126 {
2127   gint xthickness = gtk_widget_get_style (GTK_WIDGET (clist))->xthickness;
2128   gint width;
2129   gint cx;
2130   gint dx;
2131   gint last_column;
2132
2133   /* first translate the x position from widget->window
2134    * to clist->clist_window */
2135   cx = *x - xthickness;
2136
2137   for (last_column = clist->columns - 1;
2138        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2139
2140   /* calculate new column width making sure it doesn't end up
2141    * less than the minimum width */
2142   dx = (COLUMN_LEFT_XPIXEL (clist, column) + COLUMN_INSET +
2143         (column < last_column) * CELL_SPACING);
2144   width = cx - dx;
2145
2146   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
2147     {
2148       width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
2149       cx = dx + width;
2150       *x = cx + xthickness;
2151     }
2152   else if (clist->column[column].max_width >= COLUMN_MIN_WIDTH &&
2153            width > clist->column[column].max_width)
2154     {
2155       width = clist->column[column].max_width;
2156       cx = dx + clist->column[column].max_width;
2157       *x = cx + xthickness;
2158     }      
2159
2160   if (cx < 0 || cx > clist->clist_window_width)
2161     *x = -1;
2162
2163   return width;
2164 }
2165
2166 static void
2167 column_button_create (GtkCMCList *clist,
2168                       gint      column)
2169 {
2170   GtkWidget *button;
2171
2172   gtk_widget_push_composite_child ();
2173   button = clist->column[column].button = gtk_button_new ();
2174   GtkRcStyle *style = gtk_rc_style_new();
2175   style->ythickness = 0;
2176   gtk_widget_modify_style(clist->column[column].button, style);
2177   g_object_unref(style);
2178   gtk_container_set_border_width(GTK_CONTAINER(button), 0);
2179   gtk_widget_pop_composite_child ();
2180
2181   if (gtk_widget_get_realized (GTK_WIDGET(clist)) && clist->title_window)
2182     gtk_widget_set_parent_window (clist->column[column].button,
2183                                   clist->title_window);
2184   gtk_widget_set_parent (button, GTK_WIDGET (clist));
2185
2186   g_signal_connect (G_OBJECT (button), "clicked",
2187                       G_CALLBACK(column_button_clicked),
2188                       (gpointer) clist);
2189   gtk_widget_show (button);
2190 }
2191
2192 static void
2193 column_button_clicked (GtkWidget *widget,
2194                        gpointer   data)
2195 {
2196   gint i;
2197   GtkCMCList *clist;
2198
2199   cm_return_if_fail (widget != NULL);
2200   cm_return_if_fail (GTK_IS_CMCLIST (data));
2201
2202   clist = GTK_CMCLIST (data);
2203
2204   /* find the column who's button was pressed */
2205   for (i = 0; i < clist->columns; i++)
2206     if (clist->column[i].button == widget)
2207       break;
2208
2209   g_signal_emit (G_OBJECT (clist), clist_signals[CLICK_COLUMN], 0, i);
2210 }
2211
2212 static gint
2213 column_title_passive_func (GtkWidget *widget, 
2214                            GdkEvent  *event,
2215                            gpointer   data)
2216 {
2217   cm_return_val_if_fail (event != NULL, FALSE);
2218   
2219   switch (event->type)
2220     {
2221     case GDK_MOTION_NOTIFY:
2222     case GDK_BUTTON_PRESS:
2223     case GDK_2BUTTON_PRESS:
2224     case GDK_3BUTTON_PRESS:
2225     case GDK_BUTTON_RELEASE:
2226     case GDK_ENTER_NOTIFY:
2227     case GDK_LEAVE_NOTIFY:
2228       return TRUE;
2229     default:
2230       break;
2231     }
2232   return FALSE;
2233 }
2234
2235
2236 /* PUBLIC CELL FUNCTIONS
2237  *   gtk_cmclist_get_cell_type
2238  *   gtk_cmclist_set_text
2239  *   gtk_cmclist_get_text
2240  *   gtk_cmclist_set_pixbuf
2241  *   gtk_cmclist_get_pixbuf
2242  *   gtk_cmclist_set_pixtext
2243  *   gtk_cmclist_get_pixtext
2244  *   gtk_cmclist_set_shift
2245  */
2246 GtkCMCellType 
2247 gtk_cmclist_get_cell_type (GtkCMCList *clist,
2248                          gint      row,
2249                          gint      column)
2250 {
2251   GtkCMCListRow *clist_row;
2252
2253   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2254
2255   if (row < 0 || row >= clist->rows)
2256     return -1;
2257   if (column < 0 || column >= clist->columns)
2258     return -1;
2259
2260   clist_row = ROW_ELEMENT (clist, row)->data;
2261
2262   return clist_row->cell[column].type;
2263 }
2264
2265 void
2266 gtk_cmclist_set_text (GtkCMCList    *clist,
2267                     gint         row,
2268                     gint         column,
2269                     const gchar *text)
2270 {
2271   GtkCMCListRow *clist_row;
2272
2273   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2274
2275   if (row < 0 || row >= clist->rows)
2276     return;
2277   if (column < 0 || column >= clist->columns)
2278     return;
2279
2280   clist_row = ROW_ELEMENT (clist, row)->data;
2281
2282   /* if text is null, then the cell is empty */
2283   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2284     (clist, clist_row, column, GTK_CMCELL_TEXT, text, 0, NULL);
2285
2286   /* redraw the list if it's not frozen */
2287   if (CLIST_UNFROZEN (clist))
2288     {
2289       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2290         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2291     }
2292 }
2293
2294 gint
2295 gtk_cmclist_get_text (GtkCMCList  *clist,
2296                     gint       row,
2297                     gint       column,
2298                     gchar    **text)
2299 {
2300   GtkCMCListRow *clist_row;
2301
2302   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2303
2304   if (row < 0 || row >= clist->rows)
2305     return 0;
2306   if (column < 0 || column >= clist->columns)
2307     return 0;
2308
2309   clist_row = ROW_ELEMENT (clist, row)->data;
2310
2311   if (clist_row->cell[column].type != GTK_CMCELL_TEXT)
2312     return 0;
2313
2314   if (text)
2315     *text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2316
2317   return 1;
2318 }
2319
2320 void
2321 gtk_cmclist_set_pixbuf (GtkCMCList  *clist,
2322                       gint       row,
2323                       gint       column,
2324                       GdkPixbuf *pixbuf)
2325 {
2326   GtkCMCListRow *clist_row;
2327
2328   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2329
2330   if (row < 0 || row >= clist->rows)
2331     return;
2332   if (column < 0 || column >= clist->columns)
2333     return;
2334
2335   clist_row = ROW_ELEMENT (clist, row)->data;
2336   
2337   g_object_ref (pixbuf);
2338   
2339   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2340     (clist, clist_row, column, GTK_CMCELL_PIXBUF, NULL, 0, pixbuf);
2341
2342   /* redraw the list if it's not frozen */
2343   if (CLIST_UNFROZEN (clist))
2344     {
2345       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2346         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2347     }
2348 }
2349
2350 gint
2351 gtk_cmclist_get_pixbuf (GtkCMCList   *clist,
2352                       gint        row,
2353                       gint        column,
2354                       GdkPixbuf **pixbuf)
2355 {
2356   GtkCMCListRow *clist_row;
2357
2358   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2359
2360   if (row < 0 || row >= clist->rows)
2361     return 0;
2362   if (column < 0 || column >= clist->columns)
2363     return 0;
2364
2365   clist_row = ROW_ELEMENT (clist, row)->data;
2366
2367   if (clist_row->cell[column].type != GTK_CMCELL_PIXBUF)
2368     return 0;
2369
2370   if (pixbuf)
2371   {
2372     *pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2373   }
2374
2375   return 1;
2376 }
2377
2378 void
2379 gtk_cmclist_set_pixtext (GtkCMCList    *clist,
2380                        gint         row,
2381                        gint         column,
2382                        const gchar *text,
2383                        guint8       spacing,
2384                        GdkPixbuf   *pixbuf)
2385 {
2386   GtkCMCListRow *clist_row;
2387
2388   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2389
2390   if (row < 0 || row >= clist->rows)
2391     return;
2392   if (column < 0 || column >= clist->columns)
2393     return;
2394
2395   clist_row = ROW_ELEMENT (clist, row)->data;
2396   
2397   g_object_ref (pixbuf);
2398   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2399     (clist, clist_row, column, GTK_CMCELL_PIXTEXT, text, spacing, pixbuf);
2400
2401   /* redraw the list if it's not frozen */
2402   if (CLIST_UNFROZEN (clist))
2403     {
2404       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2405         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2406     }
2407 }
2408
2409 gint
2410 gtk_cmclist_get_pixtext (GtkCMCList   *clist,
2411                        gint        row,
2412                        gint        column,
2413                        gchar     **text,
2414                        guint8     *spacing,
2415                        GdkPixbuf **pixbuf)
2416 {
2417   GtkCMCListRow *clist_row;
2418
2419   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2420
2421   if (row < 0 || row >= clist->rows)
2422     return 0;
2423   if (column < 0 || column >= clist->columns)
2424     return 0;
2425
2426   clist_row = ROW_ELEMENT (clist, row)->data;
2427
2428   if (clist_row->cell[column].type != GTK_CMCELL_PIXTEXT)
2429     return 0;
2430
2431   if (text)
2432     *text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2433   if (spacing)
2434     *spacing = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2435   if (pixbuf)
2436     *pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2437
2438   return 1;
2439 }
2440
2441 void
2442 gtk_cmclist_set_shift (GtkCMCList *clist,
2443                      gint      row,
2444                      gint      column,
2445                      gint      vertical,
2446                      gint      horizontal)
2447 {
2448   GtkRequisition requisition = { 0 };
2449   GtkCMCListRow *clist_row;
2450
2451   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2452
2453   if (row < 0 || row >= clist->rows)
2454     return;
2455   if (column < 0 || column >= clist->columns)
2456     return;
2457
2458   clist_row = ROW_ELEMENT (clist, row)->data;
2459
2460   if (clist->column[column].auto_resize &&
2461       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2462     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2463                                                    column, &requisition);
2464
2465   clist_row->cell[column].vertical = vertical;
2466   clist_row->cell[column].horizontal = horizontal;
2467
2468   column_auto_resize (clist, clist_row, column, requisition.width);
2469
2470   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2471     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2472 }
2473
2474 /* PRIVATE CELL FUNCTIONS
2475  *   set_cell_contents
2476  *   cell_size_request
2477  */
2478 static void
2479 set_cell_contents (GtkCMCList    *clist,
2480                    GtkCMCListRow *clist_row,
2481                    gint         column,
2482                    GtkCMCellType  type,
2483                    const gchar *text,
2484                    guint8       spacing,
2485                    GdkPixbuf   *pixbuf)
2486 {
2487   GtkRequisition requisition;
2488   gchar *old_text = NULL;
2489   GdkPixbuf *old_pixbuf = NULL;
2490   
2491   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2492   cm_return_if_fail (clist_row != NULL);
2493
2494   if (clist->column[column].auto_resize &&
2495       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2496     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2497                                                    column, &requisition);
2498
2499   switch (clist_row->cell[column].type)
2500     {
2501     case GTK_CMCELL_EMPTY:
2502       break;
2503     case GTK_CMCELL_TEXT:
2504       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2505       break;
2506     case GTK_CMCELL_PIXBUF:
2507       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2508       break;
2509     case GTK_CMCELL_PIXTEXT:
2510       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2511       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2512       break;
2513     case GTK_CMCELL_WIDGET:
2514       /* unimplemented */
2515       break;
2516     default:
2517       break;
2518     }
2519
2520   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2521
2522   /* Note that pixbuf and mask were already ref'ed by the caller
2523    */
2524   switch (type)
2525     {
2526     case GTK_CMCELL_TEXT:
2527       if (text)
2528         {
2529           clist_row->cell[column].type = GTK_CMCELL_TEXT;
2530           GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2531         }
2532       break;
2533     case GTK_CMCELL_PIXBUF:
2534       if (pixbuf)
2535         {
2536           clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2537           GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2538         }
2539       break;
2540     case GTK_CMCELL_PIXTEXT:
2541       if (text && pixbuf)
2542         {
2543           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2544           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2545           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2546           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2547         }
2548       break;
2549     default:
2550       break;
2551     }
2552
2553   if (clist->column[column].auto_resize &&
2554       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2555     column_auto_resize (clist, clist_row, column, requisition.width);
2556
2557   g_free (old_text);
2558   if (old_pixbuf)
2559     g_object_unref (old_pixbuf);
2560 }
2561
2562 PangoLayout *
2563 _gtk_cmclist_create_cell_layout (GtkCMCList       *clist,
2564                                GtkCMCListRow    *clist_row,
2565                                gint            column)
2566 {
2567   PangoLayout *layout;
2568   GtkStyle *style;
2569   GtkCMCell *cell;
2570   gchar *text;
2571   
2572   get_cell_style (clist, clist_row, GTK_STATE_NORMAL, column, &style);
2573
2574
2575   cell = &clist_row->cell[column];
2576   switch (cell->type)
2577     {
2578     case GTK_CMCELL_TEXT:
2579     case GTK_CMCELL_PIXTEXT:
2580       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
2581               GTK_CMCELL_PIXTEXT (*cell)->text :
2582               GTK_CMCELL_TEXT (*cell)->text);
2583
2584       if (!text)
2585         return NULL;
2586       
2587       layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
2588                                                ((cell->type == GTK_CMCELL_PIXTEXT) ?
2589                                                 GTK_CMCELL_PIXTEXT (*cell)->text :
2590                                                 GTK_CMCELL_TEXT (*cell)->text));
2591       pango_layout_set_font_description (layout, style->font_desc);
2592       
2593       return layout;
2594       
2595     default:
2596       return NULL;
2597     }
2598 }
2599
2600 static void
2601 cell_size_request (GtkCMCList       *clist,
2602                    GtkCMCListRow    *clist_row,
2603                    gint            column,
2604                    GtkRequisition *requisition)
2605 {
2606   gint width;
2607   gint height;
2608   PangoLayout *layout;
2609   PangoRectangle logical_rect;
2610
2611   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2612   cm_return_if_fail (requisition != NULL);
2613
2614   layout = _gtk_cmclist_create_cell_layout (clist, clist_row, column);
2615   if (layout)
2616     {
2617       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2618       
2619       requisition->width = logical_rect.width;
2620       requisition->height = logical_rect.height;
2621       
2622       g_object_unref (G_OBJECT (layout));
2623     }
2624   else
2625     {
2626       requisition->width  = 0;
2627       requisition->height = 0;
2628     }
2629
2630   if (layout && clist_row->cell[column].type == GTK_CMCELL_PIXTEXT)
2631     requisition->width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2632
2633   switch (clist_row->cell[column].type)
2634     {
2635     case GTK_CMCELL_PIXTEXT:
2636       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2637       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2638       requisition->width += width;
2639       requisition->height = MAX (requisition->height, height);      
2640       break;
2641     case GTK_CMCELL_PIXBUF:
2642       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2643       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2644       requisition->width += width;
2645       requisition->height = MAX (requisition->height, height);
2646       break;
2647       
2648     default:
2649       break;
2650     }
2651
2652   requisition->width  += clist_row->cell[column].horizontal;
2653   requisition->height += clist_row->cell[column].vertical;
2654 }
2655
2656 /* PUBLIC INSERT/REMOVE ROW FUNCTIONS
2657  *   gtk_cmclist_prepend
2658  *   gtk_cmclist_append
2659  *   gtk_cmclist_insert
2660  *   gtk_cmclist_remove
2661  *   gtk_cmclist_clear
2662  */
2663 gint
2664 gtk_cmclist_prepend (GtkCMCList    *clist,
2665                    gchar       *text[])
2666 {
2667   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2668   cm_return_val_if_fail (text != NULL, -1);
2669
2670   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, 0, text);
2671 }
2672
2673 gint
2674 gtk_cmclist_append (GtkCMCList    *clist,
2675                   gchar       *text[])
2676 {
2677   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2678   cm_return_val_if_fail (text != NULL, -1);
2679
2680   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, clist->rows, text);
2681 }
2682
2683 gint
2684 gtk_cmclist_insert (GtkCMCList    *clist,
2685                   gint         row,
2686                   gchar       *text[])
2687 {
2688   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2689   cm_return_val_if_fail (text != NULL, -1);
2690
2691   if (row < 0 || row > clist->rows)
2692     row = clist->rows;
2693
2694   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, row, text);
2695 }
2696
2697 void
2698 gtk_cmclist_remove (GtkCMCList *clist,
2699                   gint      row)
2700 {
2701   GTK_CMCLIST_GET_CLASS (clist)->remove_row (clist, row);
2702 }
2703
2704 void
2705 gtk_cmclist_clear (GtkCMCList *clist)
2706 {
2707   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2708   
2709   GTK_CMCLIST_GET_CLASS (clist)->clear (clist);
2710 }
2711
2712 /* PRIVATE INSERT/REMOVE ROW FUNCTIONS
2713  *   real_insert_row
2714  *   real_remove_row
2715  *   real_clear
2716  *   real_row_move
2717  */
2718 static gint
2719 real_insert_row (GtkCMCList *clist,
2720                  gint      row,
2721                  gchar    *text[])
2722 {
2723   gint i;
2724   GtkCMCListRow *clist_row;
2725
2726   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2727   cm_return_val_if_fail (text != NULL, -1);
2728
2729   /* return if out of bounds */
2730   if (row < 0 || row > clist->rows)
2731     return -1;
2732
2733   /* create the row */
2734   clist_row = row_new (clist);
2735
2736   /* set the text in the row's columns */
2737   for (i = 0; i < clist->columns; i++)
2738     if (text[i])
2739       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2740         (clist, clist_row, i, GTK_CMCELL_TEXT, text[i], 0, NULL);
2741
2742   if (!clist->rows)
2743     {
2744       clist->row_list = g_list_append (clist->row_list, clist_row);
2745       clist->row_list_end = clist->row_list;
2746     }
2747   else
2748     {
2749       if (GTK_CMCLIST_AUTO_SORT(clist))   /* override insertion pos */
2750         {
2751           GList *work;
2752           
2753           row = 0;
2754           work = clist->row_list;
2755           
2756           if (clist->sort_type == GTK_SORT_ASCENDING)
2757             {
2758               while (row < clist->rows &&
2759                      clist->compare (clist, clist_row,
2760                                      GTK_CMCLIST_ROW (work)) > 0)
2761                 {
2762                   row++;
2763                   work = work->next;
2764                 }
2765             }
2766           else
2767             {
2768               while (row < clist->rows &&
2769                      clist->compare (clist, clist_row,
2770                                      GTK_CMCLIST_ROW (work)) < 0)
2771                 {
2772                   row++;
2773                   work = work->next;
2774                 }
2775             }
2776         }
2777       
2778       /* reset the row end pointer if we're inserting at the end of the list */
2779       if (row == clist->rows)
2780         clist->row_list_end = (g_list_append (clist->row_list_end,
2781                                               clist_row))->next;
2782       else
2783         clist->row_list = g_list_insert (clist->row_list, clist_row, row);
2784
2785     }
2786   clist->rows++;
2787
2788   if (row < ROW_FROM_YPIXEL (clist, 0))
2789     clist->voffset -= (clist->row_height + CELL_SPACING);
2790
2791   /* syncronize the selection list */
2792   sync_selection (clist, row, SYNC_INSERT);
2793
2794   if (clist->rows == 1)
2795     {
2796       clist->focus_row = 0;
2797       if (clist->selection_mode == GTK_SELECTION_BROWSE)
2798         gtk_cmclist_select_row (clist, 0, -1);
2799     }
2800
2801   /* redraw the list if it isn't frozen */
2802   if (CLIST_UNFROZEN (clist))
2803     {
2804       adjust_adjustments (clist, FALSE);
2805
2806       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2807         draw_rows (clist, NULL);
2808     }
2809
2810   return row;
2811 }
2812
2813 static void
2814 real_remove_row (GtkCMCList *clist,
2815                  gint      row)
2816 {
2817   gint was_visible;
2818   GList *list;
2819   GtkCMCListRow *clist_row;
2820
2821   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2822
2823   /* return if out of bounds */
2824   if (row < 0 || row > (clist->rows - 1))
2825     return;
2826
2827   was_visible = (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE);
2828
2829   /* get the row we're going to delete */
2830   list = ROW_ELEMENT (clist, row);
2831   g_assert (list != NULL);
2832   clist_row = list->data;
2833
2834   /* if we're removing a selected row, we have to make sure
2835    * it's properly unselected, and then sync up the clist->selected
2836    * list to reflect the deincrimented indexies of rows after the
2837    * removal */
2838   if (clist_row->state == GTK_STATE_SELECTED)
2839     g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
2840                      row, -1, NULL);
2841
2842   sync_selection (clist, row, SYNC_REMOVE);
2843
2844   /* reset the row end pointer if we're removing at the end of the list */
2845   clist->rows--;
2846   if (clist->row_list == list)
2847     clist->row_list = g_list_next (list);
2848   if (clist->row_list_end == list)
2849     clist->row_list_end = g_list_previous (list);
2850   list = g_list_remove (list, clist_row);
2851
2852   if (row < ROW_FROM_YPIXEL (clist, 0))
2853     clist->voffset += clist->row_height + CELL_SPACING;
2854
2855   if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
2856       clist->focus_row >= 0)
2857     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
2858                      clist->focus_row, -1, NULL);
2859
2860   /* toast the row */
2861   row_delete (clist, clist_row);
2862
2863   /* redraw the row if it isn't frozen */
2864   if (CLIST_UNFROZEN (clist))
2865     {
2866       adjust_adjustments (clist, FALSE);
2867
2868       if (was_visible)
2869         draw_rows (clist, NULL);
2870     }
2871 }
2872
2873 static void
2874 real_clear (GtkCMCList *clist)
2875 {
2876   GList *list;
2877   GList *free_list;
2878   GtkRequisition requisition;
2879   gint i;
2880
2881   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2882
2883   /* free up the selection list */
2884   g_list_free (clist->selection);
2885   g_list_free (clist->undo_selection);
2886   g_list_free (clist->undo_unselection);
2887
2888   clist->selection = NULL;
2889   clist->selection_end = NULL;
2890   clist->undo_selection = NULL;
2891   clist->undo_unselection = NULL;
2892   clist->voffset = 0;
2893   clist->focus_row = -1;
2894   clist->anchor = -1;
2895   clist->undo_anchor = -1;
2896   clist->anchor_state = GTK_STATE_SELECTED;
2897   clist->drag_pos = -1;
2898
2899   /* remove all the rows */
2900   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
2901   free_list = clist->row_list;
2902   clist->row_list = NULL;
2903   clist->row_list_end = NULL;
2904   clist->rows = 0;
2905   for (list = free_list; list; list = list->next)
2906     row_delete (clist, GTK_CMCLIST_ROW (list));
2907   g_list_free (free_list);
2908   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
2909   for (i = 0; i < clist->columns; i++)
2910     if (clist->column[i].auto_resize)
2911       {
2912         if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2913     {
2914         gtk_widget_get_requisition (clist->column[i].button, &requisition);
2915           gtk_cmclist_set_column_width
2916             (clist, i, (requisition.width -
2917                         (CELL_SPACING + (2 * COLUMN_INSET))));
2918     }
2919         else
2920           gtk_cmclist_set_column_width (clist, i, 0);
2921       }
2922   /* zero-out the scrollbars */
2923   if (clist->vadjustment)
2924     {
2925       gtk_adjustment_set_value (clist->vadjustment, 0.0);
2926       CLIST_REFRESH (clist);
2927     }
2928   else
2929     gtk_widget_queue_resize (GTK_WIDGET (clist));
2930 }
2931
2932 static void
2933 real_row_move (GtkCMCList *clist,
2934                gint      source_row,
2935                gint      dest_row)
2936 {
2937   GtkCMCListRow *clist_row;
2938   GList *list;
2939   gint first, last;
2940   gint d;
2941
2942   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2943
2944   if (GTK_CMCLIST_AUTO_SORT(clist))
2945     return;
2946
2947   if (source_row < 0 || source_row >= clist->rows ||
2948       dest_row   < 0 || dest_row   >= clist->rows ||
2949       source_row == dest_row)
2950     return;
2951
2952   gtk_cmclist_freeze (clist);
2953
2954   /* unlink source row */
2955   clist_row = ROW_ELEMENT (clist, source_row)->data;
2956   if (source_row == clist->rows - 1)
2957     clist->row_list_end = clist->row_list_end->prev;
2958   clist->row_list = g_list_remove (clist->row_list, clist_row);
2959   clist->rows--;
2960
2961   /* relink source row */
2962   clist->row_list = g_list_insert (clist->row_list, clist_row, dest_row);
2963   if (dest_row == clist->rows)
2964     clist->row_list_end = clist->row_list_end->next;
2965   clist->rows++;
2966
2967   /* sync selection */
2968   if (source_row > dest_row)
2969     {
2970       first = dest_row;
2971       last  = source_row;
2972       d = 1;
2973     }
2974   else
2975     {
2976       first = source_row;
2977       last  = dest_row;
2978       d = -1;
2979     }
2980
2981   for (list = clist->selection; list; list = list->next)
2982     {
2983       if (list->data == GINT_TO_POINTER (source_row))
2984         list->data = GINT_TO_POINTER (dest_row);
2985       else if (first <= GPOINTER_TO_INT (list->data) &&
2986                last >= GPOINTER_TO_INT (list->data))
2987         list->data = GINT_TO_POINTER (GPOINTER_TO_INT (list->data) + d);
2988     }
2989   
2990   if (clist->focus_row == source_row)
2991     clist->focus_row = dest_row;
2992   else if (clist->focus_row > first)
2993     clist->focus_row += d;
2994
2995   gtk_cmclist_thaw (clist);
2996 }
2997
2998 /* PUBLIC ROW FUNCTIONS
2999  *   gtk_cmclist_moveto
3000  *   gtk_cmclist_set_row_height
3001  *   gtk_cmclist_set_row_data
3002  *   gtk_cmclist_set_row_data_full
3003  *   gtk_cmclist_get_row_data
3004  *   gtk_cmclist_find_row_from_data
3005  *   gtk_cmclist_swap_rows
3006  *   gtk_cmclist_row_move
3007  *   gtk_cmclist_row_is_visible
3008  *   gtk_cmclist_set_foreground
3009  *   gtk_cmclist_set_background
3010  */
3011 void
3012 gtk_cmclist_moveto (GtkCMCList *clist,
3013                   gint      row,
3014                   gint      column,
3015                   gfloat    row_align,
3016                   gfloat    col_align)
3017 {
3018   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3019
3020   if (row < -1 || row >= clist->rows)
3021     return;
3022   if (column < -1 || column >= clist->columns)
3023     return;
3024
3025   row_align = CLAMP (row_align, 0, 1);
3026   col_align = CLAMP (col_align, 0, 1);
3027
3028   /* adjust horizontal scrollbar */
3029   if (clist->hadjustment && column >= 0)
3030     {
3031       gint x;
3032
3033       x = (COLUMN_LEFT (clist, column) - CELL_SPACING - COLUMN_INSET -
3034            (col_align * (clist->clist_window_width - 2 * COLUMN_INSET -
3035                          CELL_SPACING - clist->column[column].area.width)));
3036       if (x < 0)
3037         gtk_adjustment_set_value (clist->hadjustment, 0.0);
3038       else if (x > LIST_WIDTH (clist) - clist->clist_window_width)
3039         gtk_adjustment_set_value 
3040           (clist->hadjustment, LIST_WIDTH (clist) - clist->clist_window_width);
3041       else
3042         gtk_adjustment_set_value (clist->hadjustment, x);
3043     }
3044
3045   /* adjust vertical scrollbar */
3046   if (clist->vadjustment && row >= 0)
3047     move_vertical (clist, row, row_align);
3048 }
3049
3050 void
3051 gtk_cmclist_set_row_height (GtkCMCList *clist,
3052                           guint     height)
3053 {
3054   GtkStyle *style;
3055   GtkWidget *widget;
3056
3057   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3058
3059   widget = GTK_WIDGET (clist);
3060
3061   style = gtk_widget_get_style (widget);
3062
3063   if (height > 0)
3064     {
3065       clist->row_height = height;
3066       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3067     }
3068   else
3069     {
3070       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3071       clist->row_height = 0;
3072     }
3073
3074   if (style->font_desc)
3075     {
3076       PangoContext *context = gtk_widget_get_pango_context (widget);
3077       PangoFontMetrics *metrics;
3078
3079       metrics = pango_context_get_metrics (context,
3080                                            style->font_desc,
3081                                            pango_context_get_language (context));
3082
3083       if (!GTK_CMCLIST_ROW_HEIGHT_SET(clist))
3084         {
3085           clist->row_height = (pango_font_metrics_get_ascent (metrics) +
3086                                pango_font_metrics_get_descent (metrics));
3087           clist->row_height = PANGO_PIXELS (clist->row_height) + 1;
3088         }
3089
3090       pango_font_metrics_unref (metrics);
3091     }
3092
3093   CLIST_REFRESH (clist);
3094 }
3095
3096 void
3097 gtk_cmclist_set_row_data (GtkCMCList *clist,
3098                         gint      row,
3099                         gpointer  data)
3100 {
3101   gtk_cmclist_set_row_data_full (clist, row, data, NULL);
3102 }
3103
3104 void
3105 gtk_cmclist_set_row_data_full (GtkCMCList         *clist,
3106                              gint              row,
3107                              gpointer          data,
3108                              GDestroyNotify  destroy)
3109 {
3110   GtkCMCListRow *clist_row;
3111
3112   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3113
3114   if (row < 0 || row > (clist->rows - 1))
3115     return;
3116
3117   clist_row = ROW_ELEMENT (clist, row)->data;
3118
3119   if (clist_row->destroy)
3120     clist_row->destroy (clist_row->data);
3121   
3122   clist_row->data = data;
3123   clist_row->destroy = destroy;
3124 }
3125
3126 gpointer
3127 gtk_cmclist_get_row_data (GtkCMCList *clist,
3128                         gint      row)
3129 {
3130   GtkCMCListRow *clist_row;
3131
3132   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3133
3134   if (row < 0 || row > (clist->rows - 1))
3135     return NULL;
3136
3137   clist_row = ROW_ELEMENT (clist, row)->data;
3138   return clist_row->data;
3139 }
3140
3141 gint
3142 gtk_cmclist_find_row_from_data (GtkCMCList *clist,
3143                               gpointer  data)
3144 {
3145   GList *list;
3146   gint n;
3147
3148   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
3149
3150   for (n = 0, list = clist->row_list; list; n++, list = list->next)
3151     if (GTK_CMCLIST_ROW (list)->data == data)
3152       return n;
3153
3154   return -1;
3155 }
3156
3157 void 
3158 gtk_cmclist_swap_rows (GtkCMCList *clist,
3159                      gint      row1, 
3160                      gint      row2)
3161 {
3162   gint first, last;
3163
3164   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3165   cm_return_if_fail (row1 != row2);
3166
3167   if (GTK_CMCLIST_AUTO_SORT(clist))
3168     return;
3169
3170   gtk_cmclist_freeze (clist);
3171
3172   first = MIN (row1, row2);
3173   last  = MAX (row1, row2);
3174
3175   gtk_cmclist_row_move (clist, last, first);
3176   gtk_cmclist_row_move (clist, first + 1, last);
3177   
3178   gtk_cmclist_thaw (clist);
3179 }
3180
3181 void
3182 gtk_cmclist_row_move (GtkCMCList *clist,
3183                     gint      source_row,
3184                     gint      dest_row)
3185 {
3186   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3187
3188   if (GTK_CMCLIST_AUTO_SORT(clist))
3189     return;
3190
3191   if (source_row < 0 || source_row >= clist->rows ||
3192       dest_row   < 0 || dest_row   >= clist->rows ||
3193       source_row == dest_row)
3194     return;
3195
3196   g_signal_emit (G_OBJECT (clist), clist_signals[ROW_MOVE], 0,
3197                    source_row, dest_row);
3198 }
3199
3200 GtkVisibility
3201 gtk_cmclist_row_is_visible (GtkCMCList *clist,
3202                           gint      row)
3203 {
3204   gint top;
3205
3206   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
3207
3208   if (row < 0 || row >= clist->rows)
3209     return GTK_VISIBILITY_NONE;
3210
3211   if (clist->row_height == 0)
3212     return GTK_VISIBILITY_NONE;
3213
3214   if (row < ROW_FROM_YPIXEL (clist, 0))
3215     return GTK_VISIBILITY_NONE;
3216
3217   if (row > ROW_FROM_YPIXEL (clist, clist->clist_window_height))
3218     return GTK_VISIBILITY_NONE;
3219
3220   top = ROW_TOP_YPIXEL (clist, row);
3221
3222   if ((top < 0)
3223       || ((top + clist->row_height) >= clist->clist_window_height))
3224     return GTK_VISIBILITY_PARTIAL;
3225
3226   return GTK_VISIBILITY_FULL;
3227 }
3228
3229 gboolean
3230 gtk_cmclist_row_is_above_viewport (GtkCMCList *clist,
3231                                 gint row)
3232 {
3233         cm_return_val_if_fail(GTK_IS_CMCLIST (clist), 0);
3234
3235         if (row < 0 || row >= clist->rows)
3236                 return FALSE;
3237
3238         if (clist->row_height == 0)
3239                 return FALSE;
3240
3241         if (row < ROW_FROM_YPIXEL (clist, 0))
3242                 return TRUE;
3243
3244         return FALSE;
3245 }
3246
3247 gboolean
3248 gtk_cmclist_row_is_below_viewport (GtkCMCList *clist,
3249                                 gint row)
3250 {
3251         cm_return_val_if_fail(GTK_IS_CMCLIST (clist), 0);
3252
3253         if (row < 0 || row >= clist->rows)
3254                 return FALSE;
3255
3256         if (clist->row_height == 0)
3257                 return FALSE;
3258
3259         if (row > ROW_FROM_YPIXEL (clist, clist->clist_window_height))
3260                 return TRUE;
3261
3262         return FALSE;
3263 }
3264
3265 void
3266 gtk_cmclist_set_foreground (GtkCMCList       *clist,
3267                           gint            row,
3268                           const GdkColor *color)
3269 {
3270   GtkCMCListRow *clist_row;
3271
3272   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3273
3274   if (row < 0 || row >= clist->rows)
3275     return;
3276
3277   clist_row = ROW_ELEMENT (clist, row)->data;
3278
3279   if (color)
3280     {
3281       clist_row->foreground = *color;
3282       clist_row->fg_set = TRUE;
3283       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3284         gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3285                          &clist_row->foreground, TRUE, TRUE);
3286     }
3287   else
3288     clist_row->fg_set = FALSE;
3289
3290   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3291     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3292 }
3293
3294 void
3295 gtk_cmclist_set_background (GtkCMCList       *clist,
3296                           gint            row,
3297                           const GdkColor *color)
3298 {
3299   GtkCMCListRow *clist_row;
3300
3301   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3302
3303   if (row < 0 || row >= clist->rows)
3304     return;
3305
3306   clist_row = ROW_ELEMENT (clist, row)->data;
3307
3308   if (color)
3309     {
3310       clist_row->background = *color;
3311       clist_row->bg_set = TRUE;
3312       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3313         gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3314                          &clist_row->background, TRUE, TRUE);
3315     }
3316   else
3317     clist_row->bg_set = FALSE;
3318
3319   if (CLIST_UNFROZEN (clist)
3320       && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3321     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3322 }
3323
3324 /* PUBLIC ROW/CELL STYLE FUNCTIONS
3325  *   gtk_cmclist_set_cell_style
3326  *   gtk_cmclist_get_cell_style
3327  *   gtk_cmclist_set_row_style
3328  *   gtk_cmclist_get_row_style
3329  */
3330 void
3331 gtk_cmclist_set_cell_style (GtkCMCList *clist,
3332                           gint      row,
3333                           gint      column,
3334                           GtkStyle *style)
3335 {
3336   GtkRequisition requisition = { 0 };
3337   GtkCMCListRow *clist_row;
3338
3339   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3340
3341   if (row < 0 || row >= clist->rows)
3342     return;
3343   if (column < 0 || column >= clist->columns)
3344     return;
3345
3346   clist_row = ROW_ELEMENT (clist, row)->data;
3347
3348   if (clist_row->cell[column].style == style)
3349     return;
3350
3351   if (clist->column[column].auto_resize &&
3352       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3353     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3354                                                    column, &requisition);
3355
3356   if (clist_row->cell[column].style)
3357     {
3358       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3359         gtk_style_detach (clist_row->cell[column].style);
3360       g_object_unref (clist_row->cell[column].style);
3361     }
3362
3363   clist_row->cell[column].style = style;
3364
3365   if (clist_row->cell[column].style)
3366     {
3367       g_object_ref (clist_row->cell[column].style);
3368       
3369       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3370         clist_row->cell[column].style =
3371           gtk_style_attach (clist_row->cell[column].style,
3372                             clist->clist_window);
3373     }
3374
3375   column_auto_resize (clist, clist_row, column, requisition.width);
3376
3377   /* redraw the list if it's not frozen */
3378   if (CLIST_UNFROZEN (clist))
3379     {
3380       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3381         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3382     }
3383 }
3384
3385 GtkStyle *
3386 gtk_cmclist_get_cell_style (GtkCMCList *clist,
3387                           gint      row,
3388                           gint      column)
3389 {
3390   GtkCMCListRow *clist_row;
3391
3392   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3393
3394   if (row < 0 || row >= clist->rows || column < 0 || column >= clist->columns)
3395     return NULL;
3396
3397   clist_row = ROW_ELEMENT (clist, row)->data;
3398
3399   return clist_row->cell[column].style;
3400 }
3401
3402 void
3403 gtk_cmclist_set_row_style (GtkCMCList *clist,
3404                          gint      row,
3405                          GtkStyle *style)
3406 {
3407   GtkRequisition requisition;
3408   GtkCMCListRow *clist_row;
3409   gint *old_width;
3410   gint i;
3411
3412   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3413
3414   if (row < 0 || row >= clist->rows)
3415     return;
3416
3417   clist_row = ROW_ELEMENT (clist, row)->data;
3418
3419   if (clist_row->style == style)
3420     return;
3421
3422   old_width = g_new (gint, clist->columns);
3423
3424   if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3425     {
3426       for (i = 0; i < clist->columns; i++)
3427         if (clist->column[i].auto_resize)
3428           {
3429             GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3430                                                            i, &requisition);
3431             old_width[i] = requisition.width;
3432           }
3433     }
3434
3435   if (clist_row->style)
3436     {
3437       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3438         gtk_style_detach (clist_row->style);
3439       g_object_unref (clist_row->style);
3440     }
3441
3442   clist_row->style = style;
3443
3444   if (clist_row->style)
3445     {
3446       g_object_ref (clist_row->style);
3447       
3448       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3449         clist_row->style = gtk_style_attach (clist_row->style,
3450                                              clist->clist_window);
3451     }
3452
3453   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3454     for (i = 0; i < clist->columns; i++)
3455       column_auto_resize (clist, clist_row, i, old_width[i]);
3456
3457   g_free (old_width);
3458
3459   /* redraw the list if it's not frozen */
3460   if (CLIST_UNFROZEN (clist))
3461     {
3462       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3463         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3464     }
3465 }
3466
3467 GtkStyle *
3468 gtk_cmclist_get_row_style (GtkCMCList *clist,
3469                          gint      row)
3470 {
3471   GtkCMCListRow *clist_row;
3472
3473   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3474
3475   if (row < 0 || row >= clist->rows)
3476     return NULL;
3477
3478   clist_row = ROW_ELEMENT (clist, row)->data;
3479
3480   return clist_row->style;
3481 }
3482
3483 /* PUBLIC SELECTION FUNCTIONS
3484  *   gtk_cmclist_set_selectable
3485  *   gtk_cmclist_get_selectable
3486  *   gtk_cmclist_select_row
3487  *   gtk_cmclist_unselect_row
3488  *   gtk_cmclist_select_all
3489  *   gtk_cmclist_unselect_all
3490  *   gtk_cmclist_undo_selection
3491  */
3492 void
3493 gtk_cmclist_set_selectable (GtkCMCList *clist,
3494                           gint      row,
3495                           gboolean  selectable)
3496 {
3497   GtkCMCListRow *clist_row;
3498
3499   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3500
3501   if (row < 0 || row >= clist->rows)
3502     return;
3503
3504   clist_row = ROW_ELEMENT (clist, row)->data;
3505
3506   if (selectable == clist_row->selectable)
3507     return;
3508
3509   clist_row->selectable = selectable;
3510
3511   if (!selectable && clist_row->state == GTK_STATE_SELECTED)
3512     {
3513       if (clist->anchor >= 0 &&
3514           clist->selection_mode == GTK_SELECTION_MULTIPLE)
3515         {
3516           clist->drag_button = 0;
3517           remove_grab (clist);
3518           GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3519         }
3520       g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3521                        row, -1, NULL);
3522     }      
3523 }
3524
3525 gboolean
3526 gtk_cmclist_get_selectable (GtkCMCList *clist,
3527                           gint      row)
3528 {
3529   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), FALSE);
3530
3531   if (row < 0 || row >= clist->rows)
3532     return FALSE;
3533
3534   return GTK_CMCLIST_ROW (ROW_ELEMENT (clist, row))->selectable;
3535 }
3536
3537 void
3538 gtk_cmclist_select_row (GtkCMCList *clist,
3539                       gint      row,
3540                       gint      column)
3541 {
3542   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3543
3544   if (row < 0 || row >= clist->rows)
3545     return;
3546   if (column < -1 || column >= clist->columns)
3547     return;
3548
3549   g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3550                    row, column, NULL);
3551 }
3552
3553 void
3554 gtk_cmclist_unselect_row (GtkCMCList *clist,
3555                         gint      row,
3556                         gint      column)
3557 {
3558   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3559
3560   if (row < 0 || row >= clist->rows)
3561     return;
3562   if (column < -1 || column >= clist->columns)
3563     return;
3564
3565   g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3566                    row, column, NULL);
3567 }
3568
3569 void
3570 gtk_cmclist_select_all (GtkCMCList *clist)
3571 {
3572   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3573
3574   GTK_CMCLIST_GET_CLASS (clist)->select_all (clist);
3575 }
3576
3577 void
3578 gtk_cmclist_unselect_all (GtkCMCList *clist)
3579 {
3580   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3581
3582   GTK_CMCLIST_GET_CLASS (clist)->unselect_all (clist);
3583 }
3584
3585 void
3586 gtk_cmclist_undo_selection (GtkCMCList *clist)
3587 {
3588   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3589
3590   if (clist->selection_mode == GTK_SELECTION_MULTIPLE &&
3591       (clist->undo_selection || clist->undo_unselection))
3592     g_signal_emit (G_OBJECT (clist), clist_signals[UNDO_SELECTION], 0);
3593 }
3594
3595 /* PRIVATE SELECTION FUNCTIONS
3596  *   selection_find
3597  *   toggle_row
3598  *   fake_toggle_row
3599  *   toggle_focus_row
3600  *   toggle_add_mode
3601  *   real_select_row
3602  *   real_unselect_row
3603  *   real_select_all
3604  *   real_unselect_all
3605  *   fake_unselect_all
3606  *   real_undo_selection
3607  *   set_anchor
3608  *   resync_selection
3609  *   update_extended_selection
3610  *   start_selection
3611  *   end_selection
3612  *   extend_selection
3613  *   sync_selection
3614  */
3615 static GList *
3616 selection_find (GtkCMCList *clist,
3617                 gint      row_number,
3618                 GList    *row_list_element)
3619 {
3620   return g_list_find (clist->selection, GINT_TO_POINTER (row_number));
3621 }
3622
3623 static void
3624 toggle_row (GtkCMCList *clist,
3625        &