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