2010-03-25 [paul] 3.7.5cvs39
[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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_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_Escape, 0,
924                                 "undo_selection", 0);
925   gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
926                                 "abort_column_resize", 0);
927   gtk_binding_entry_add_signal (binding_set, GDK_space, 0,
928                                 "toggle_focus_row", 0);
929   gtk_binding_entry_add_signal (binding_set, GDK_KP_Space, 0,
930                                 "toggle_focus_row", 0);  
931   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK,
932                                 "toggle_add_mode", 0);
933   gtk_binding_entry_add_signal (binding_set, GDK_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_Shift_L,
942                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK,
943                                 "end_selection", 0);
944   gtk_binding_entry_add_signal (binding_set, GDK_Shift_R,
945                                 GDK_RELEASE_MASK | GDK_SHIFT_MASK,
946                                 "end_selection", 0);
947   gtk_binding_entry_add_signal (binding_set, GDK_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_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             !gtkut_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   GTK_WIDGET_UNSET_FLAGS (clist, GTK_NO_WINDOW);
1056   GTK_WIDGET_SET_FLAGS (clist, GTK_CAN_FOCUS);
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 (gtkut_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   GTK_WIDGET_SET_FLAGS (clist->column[column].button, GTK_CAN_FOCUS);
1458   if (gtkut_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   GtkButton *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_BUTTON (clist->column[column].button);
1476
1477   clist->column[column].button_passive = TRUE;
1478
1479   if (button->button_down)
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 (button->in_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   GTK_WIDGET_UNSET_FLAGS (clist->column[column].button, GTK_CAN_FOCUS);
1494   if (gtkut_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 (clist->column[column].button)->child;
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 (gtkut_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 (clist->column[column].button)->child;
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 (gtkut_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 (clist->column[column].button)->child;
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 (clist->column[column].button)->child;
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 (gtkut_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 (gtkut_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     width = (clist->column[column].button->requisition.width)
1825 #if 0
1826              (CELL_SPACING + (2 * COLUMN_INSET)))
1827 #endif
1828                 ;
1829   else
1830     width = 0;
1831
1832   for (list = clist->row_list; list; list = list->next)
1833     {
1834   GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1835         (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1836       width = MAX (width, requisition.width);
1837     }
1838
1839   return width;
1840 }
1841
1842 void
1843 gtk_cmclist_set_column_width (GtkCMCList *clist,
1844                             gint      column,
1845                             gint      width)
1846 {
1847   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1848
1849   if (column < 0 || column >= clist->columns)
1850     return;
1851
1852   g_signal_emit (G_OBJECT (clist), clist_signals[RESIZE_COLUMN], 0,
1853                    column, width);
1854 }
1855
1856 void
1857 gtk_cmclist_set_column_min_width (GtkCMCList *clist,
1858                                 gint      column,
1859                                 gint      min_width)
1860 {
1861   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1862
1863   if (column < 0 || column >= clist->columns)
1864     return;
1865   if (clist->column[column].min_width == min_width)
1866     return;
1867
1868   if (clist->column[column].max_width >= 0  &&
1869       clist->column[column].max_width < min_width)
1870     clist->column[column].min_width = clist->column[column].max_width;
1871   else
1872     clist->column[column].min_width = min_width;
1873
1874   if (clist->column[column].area.width < clist->column[column].min_width)
1875     gtk_cmclist_set_column_width (clist, column,clist->column[column].min_width);
1876 }
1877
1878 void
1879 gtk_cmclist_set_column_max_width (GtkCMCList *clist,
1880                                 gint      column,
1881                                 gint      max_width)
1882 {
1883   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1884
1885   if (column < 0 || column >= clist->columns)
1886     return;
1887   if (clist->column[column].max_width == max_width)
1888     return;
1889
1890   if (clist->column[column].min_width >= 0 && max_width >= 0 &&
1891       clist->column[column].min_width > max_width)
1892     clist->column[column].max_width = clist->column[column].min_width;
1893   else
1894     clist->column[column].max_width = max_width;
1895   
1896   if (clist->column[column].area.width > clist->column[column].max_width)
1897     gtk_cmclist_set_column_width (clist, column,clist->column[column].max_width);
1898 }
1899
1900 /* PRIVATE COLUMN FUNCTIONS
1901  *   column_auto_resize
1902  *   real_resize_column
1903  *   abort_column_resize
1904  *   size_allocate_title_buttons
1905  *   size_allocate_columns
1906  *   list_requisition_width
1907  *   new_column_width
1908  *   column_button_create
1909  *   column_button_clicked
1910  *   column_title_passive_func
1911  */
1912 static void
1913 column_auto_resize (GtkCMCList    *clist,
1914                     GtkCMCListRow *clist_row,
1915                     gint         column,
1916                     gint         old_width)
1917 {
1918   /* resize column if needed for auto_resize */
1919   GtkRequisition requisition;
1920
1921   if (!clist->column[column].auto_resize ||
1922       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1923     return;
1924
1925   if (clist_row)
1926     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
1927                                                    column, &requisition);
1928   else
1929     requisition.width = 0;
1930
1931   if (requisition.width > clist->column[column].width)
1932     gtk_cmclist_set_column_width (clist, column, requisition.width);
1933   else if (requisition.width < old_width &&
1934            old_width == clist->column[column].width)
1935     {
1936       GList *list;
1937       gint new_width = 0;
1938
1939       /* run a "gtk_cmclist_optimal_column_width" but break, if
1940        * the column doesn't shrink */
1941       if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
1942         new_width = (clist->column[column].button->requisition.width -
1943                      (CELL_SPACING + (2 * COLUMN_INSET)));
1944       else
1945         new_width = 0;
1946
1947       for (list = clist->row_list; list; list = list->next)
1948         {
1949           GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1950             (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1951           new_width = MAX (new_width, requisition.width);
1952           if (new_width == clist->column[column].width)
1953             break;
1954         }
1955       if (new_width < clist->column[column].width)
1956         gtk_cmclist_set_column_width
1957           (clist, column, MAX (new_width, clist->column[column].min_width));
1958     }
1959 }
1960
1961 static void
1962 real_resize_column (GtkCMCList *clist,
1963                     gint      column,
1964                     gint      width)
1965 {
1966   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1967
1968   if (column < 0 || column >= clist->columns)
1969     return;
1970   
1971   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
1972     width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
1973   if (clist->column[column].max_width >= 0 &&
1974       width > clist->column[column].max_width)
1975     width = clist->column[column].max_width;
1976
1977   clist->column[column].width = width;
1978   clist->column[column].width_set = TRUE;
1979
1980   /* FIXME: this is quite expensive to do if the widget hasn't
1981    *        been size_allocated yet, and pointless. Should
1982    *        a flag be kept
1983    */
1984   size_allocate_columns (clist, TRUE);
1985   size_allocate_title_buttons (clist);
1986
1987   CLIST_REFRESH (clist);
1988 }
1989
1990 static void
1991 abort_column_resize (GtkCMCList *clist)
1992 {
1993   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1994
1995   if (!GTK_CMCLIST_IN_DRAG(clist))
1996     return;
1997
1998   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_IN_DRAG);
1999   gtk_grab_remove (GTK_WIDGET (clist));
2000   gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (clist)),
2001                               GDK_CURRENT_TIME);
2002   clist->drag_pos = -1;
2003
2004   if (clist->x_drag >= 0 && clist->x_drag <= clist->clist_window_width - 1)
2005     draw_xor_line (clist);
2006
2007   if (GTK_CMCLIST_ADD_MODE(clist))
2008     {
2009       gdk_gc_set_line_attributes (clist->xor_gc, 1, GDK_LINE_ON_OFF_DASH, 0,0);
2010       gdk_gc_set_dashes (clist->xor_gc, 0, "\4\4", 2);
2011     }
2012 }
2013
2014 static void
2015 size_allocate_title_buttons (GtkCMCList *clist)
2016 {
2017   GtkAllocation button_allocation;
2018   gint last_column;
2019   gint last_button = 0;
2020   gint i;
2021
2022   if (!gtkut_widget_get_realized (GTK_WIDGET(clist)))
2023     return;
2024
2025   button_allocation.x = clist->hoffset;
2026   button_allocation.y = 0;
2027   button_allocation.width = 0;
2028   button_allocation.height = clist->column_title_area.height;
2029
2030   /* find last visible column */
2031   for (last_column = clist->columns - 1; last_column >= 0; last_column--)
2032     if (clist->column[last_column].visible)
2033       break;
2034
2035   for (i = 0; i < last_column; i++)
2036     {
2037       if (!clist->column[i].visible)
2038         {
2039           last_button = i + 1;
2040           gdk_window_hide (clist->column[i].window);
2041           continue;
2042         }
2043
2044       button_allocation.width += (clist->column[i].area.width +
2045                                   CELL_SPACING + 2 * COLUMN_INSET);
2046
2047       if (!clist->column[i + 1].button)
2048         {
2049           gdk_window_hide (clist->column[i].window);
2050           continue;
2051         }
2052
2053       gtk_widget_size_allocate (clist->column[last_button].button,
2054                                 &button_allocation);
2055       button_allocation.x += button_allocation.width;
2056       button_allocation.width = 0;
2057
2058       if (clist->column[last_button].resizeable)
2059         {
2060           gdk_window_show (clist->column[last_button].window);
2061           gdk_window_move_resize (clist->column[last_button].window,
2062                                   button_allocation.x - (DRAG_WIDTH / 2), 
2063                                   0, DRAG_WIDTH,
2064                                   clist->column_title_area.height);
2065         }
2066       else
2067         gdk_window_hide (clist->column[last_button].window);
2068
2069       last_button = i + 1;
2070     }
2071
2072   button_allocation.width += (clist->column[last_column].area.width +
2073                               2 * (CELL_SPACING + COLUMN_INSET));
2074   gtk_widget_size_allocate (clist->column[last_button].button,
2075                             &button_allocation);
2076
2077   if (clist->column[last_button].resizeable)
2078     {
2079       button_allocation.x += button_allocation.width;
2080
2081       gdk_window_show (clist->column[last_button].window);
2082       gdk_window_move_resize (clist->column[last_button].window,
2083                               button_allocation.x - (DRAG_WIDTH / 2), 
2084                               0, DRAG_WIDTH, clist->column_title_area.height);
2085     }
2086   else
2087     gdk_window_hide (clist->column[last_button].window);
2088 }
2089
2090 static void
2091 size_allocate_columns (GtkCMCList *clist,
2092                        gboolean  block_resize)
2093 {
2094   gint xoffset = CELL_SPACING + COLUMN_INSET;
2095   gint last_column;
2096   gint i;
2097
2098   /* find last visible column and calculate correct column width */
2099   for (last_column = clist->columns - 1;
2100        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2101
2102   if (last_column < 0)
2103     return;
2104
2105   for (i = 0; i <= last_column; i++)
2106     {
2107       if (!clist->column[i].visible)
2108         continue;
2109       clist->column[i].area.x = xoffset;
2110       if (clist->column[i].width_set)
2111         {
2112           if (!block_resize && GTK_CMCLIST_SHOW_TITLES(clist) &&
2113               clist->column[i].auto_resize && clist->column[i].button)
2114             {
2115               gint width;
2116
2117               width = (clist->column[i].button->requisition.width -
2118                        (CELL_SPACING + (2 * COLUMN_INSET)));
2119
2120               if (width > clist->column[i].width)
2121                 gtk_cmclist_set_column_width (clist, i, width);
2122             }
2123
2124           clist->column[i].area.width = clist->column[i].width;
2125           xoffset += clist->column[i].width + CELL_SPACING + (2* COLUMN_INSET);
2126         }
2127       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2128         {
2129           clist->column[i].area.width =
2130             clist->column[i].button->requisition.width -
2131             (CELL_SPACING + (2 * COLUMN_INSET));
2132           xoffset += clist->column[i].button->requisition.width;
2133         }
2134     }
2135
2136   clist->column[last_column].area.width = clist->column[last_column].area.width
2137     + MAX (0, clist->clist_window_width + COLUMN_INSET - xoffset);
2138 }
2139
2140 static gint
2141 list_requisition_width (GtkCMCList *clist) 
2142 {
2143   gint width = CELL_SPACING;
2144   gint i;
2145
2146   for (i = clist->columns - 1; i >= 0; i--)
2147     {
2148       if (!clist->column[i].visible)
2149         continue;
2150
2151       if (clist->column[i].width_set)
2152         width += clist->column[i].width + CELL_SPACING + (2 * COLUMN_INSET);
2153       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2154         width += clist->column[i].button->requisition.width;
2155     }
2156
2157   return width;
2158 }
2159
2160 /* this function returns the new width of the column being resized given
2161  * the column and x position of the cursor; the x cursor position is passed
2162  * in as a pointer and automagicly corrected if it's beyond min/max limits */
2163 static gint
2164 new_column_width (GtkCMCList *clist,
2165                   gint      column,
2166                   gint     *x)
2167 {
2168   gint xthickness = GTK_WIDGET (clist)->style->xthickness;
2169   gint width;
2170   gint cx;
2171   gint dx;
2172   gint last_column;
2173
2174   /* first translate the x position from widget->window
2175    * to clist->clist_window */
2176   cx = *x - xthickness;
2177
2178   for (last_column = clist->columns - 1;
2179        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2180
2181   /* calculate new column width making sure it doesn't end up
2182    * less than the minimum width */
2183   dx = (COLUMN_LEFT_XPIXEL (clist, column) + COLUMN_INSET +
2184         (column < last_column) * CELL_SPACING);
2185   width = cx - dx;
2186
2187   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
2188     {
2189       width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
2190       cx = dx + width;
2191       *x = cx + xthickness;
2192     }
2193   else if (clist->column[column].max_width >= COLUMN_MIN_WIDTH &&
2194            width > clist->column[column].max_width)
2195     {
2196       width = clist->column[column].max_width;
2197       cx = dx + clist->column[column].max_width;
2198       *x = cx + xthickness;
2199     }      
2200
2201   if (cx < 0 || cx > clist->clist_window_width)
2202     *x = -1;
2203
2204   return width;
2205 }
2206
2207 static void
2208 column_button_create (GtkCMCList *clist,
2209                       gint      column)
2210 {
2211   GtkWidget *button;
2212
2213   gtk_widget_push_composite_child ();
2214   button = clist->column[column].button = gtk_button_new ();
2215   GtkRcStyle *style = gtk_rc_style_new();
2216   style->ythickness = 0;
2217   gtk_widget_modify_style(clist->column[column].button, style);
2218   g_object_unref(style);
2219   gtk_container_set_border_width(GTK_CONTAINER(button), 0);
2220   gtk_widget_pop_composite_child ();
2221
2222   if (gtkut_widget_get_realized (GTK_WIDGET(clist)) && clist->title_window)
2223     gtk_widget_set_parent_window (clist->column[column].button,
2224                                   clist->title_window);
2225   gtk_widget_set_parent (button, GTK_WIDGET (clist));
2226
2227   g_signal_connect (G_OBJECT (button), "clicked",
2228                       G_CALLBACK(column_button_clicked),
2229                       (gpointer) clist);
2230   gtk_widget_show (button);
2231 }
2232
2233 static void
2234 column_button_clicked (GtkWidget *widget,
2235                        gpointer   data)
2236 {
2237   gint i;
2238   GtkCMCList *clist;
2239
2240   cm_return_if_fail (widget != NULL);
2241   cm_return_if_fail (GTK_IS_CMCLIST (data));
2242
2243   clist = GTK_CMCLIST (data);
2244
2245   /* find the column who's button was pressed */
2246   for (i = 0; i < clist->columns; i++)
2247     if (clist->column[i].button == widget)
2248       break;
2249
2250   g_signal_emit (G_OBJECT (clist), clist_signals[CLICK_COLUMN], 0, i);
2251 }
2252
2253 static gint
2254 column_title_passive_func (GtkWidget *widget, 
2255                            GdkEvent  *event,
2256                            gpointer   data)
2257 {
2258   cm_return_val_if_fail (event != NULL, FALSE);
2259   
2260   switch (event->type)
2261     {
2262     case GDK_MOTION_NOTIFY:
2263     case GDK_BUTTON_PRESS:
2264     case GDK_2BUTTON_PRESS:
2265     case GDK_3BUTTON_PRESS:
2266     case GDK_BUTTON_RELEASE:
2267     case GDK_ENTER_NOTIFY:
2268     case GDK_LEAVE_NOTIFY:
2269       return TRUE;
2270     default:
2271       break;
2272     }
2273   return FALSE;
2274 }
2275
2276
2277 /* PUBLIC CELL FUNCTIONS
2278  *   gtk_cmclist_get_cell_type
2279  *   gtk_cmclist_set_text
2280  *   gtk_cmclist_get_text
2281  *   gtk_cmclist_set_pixbuf
2282  *   gtk_cmclist_get_pixbuf
2283  *   gtk_cmclist_set_pixtext
2284  *   gtk_cmclist_get_pixtext
2285  *   gtk_cmclist_set_shift
2286  */
2287 GtkCMCellType 
2288 gtk_cmclist_get_cell_type (GtkCMCList *clist,
2289                          gint      row,
2290                          gint      column)
2291 {
2292   GtkCMCListRow *clist_row;
2293
2294   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2295
2296   if (row < 0 || row >= clist->rows)
2297     return -1;
2298   if (column < 0 || column >= clist->columns)
2299     return -1;
2300
2301   clist_row = ROW_ELEMENT (clist, row)->data;
2302
2303   return clist_row->cell[column].type;
2304 }
2305
2306 void
2307 gtk_cmclist_set_text (GtkCMCList    *clist,
2308                     gint         row,
2309                     gint         column,
2310                     const gchar *text)
2311 {
2312   GtkCMCListRow *clist_row;
2313
2314   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2315
2316   if (row < 0 || row >= clist->rows)
2317     return;
2318   if (column < 0 || column >= clist->columns)
2319     return;
2320
2321   clist_row = ROW_ELEMENT (clist, row)->data;
2322
2323   /* if text is null, then the cell is empty */
2324   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2325     (clist, clist_row, column, GTK_CMCELL_TEXT, text, 0, NULL);
2326
2327   /* redraw the list if it's not frozen */
2328   if (CLIST_UNFROZEN (clist))
2329     {
2330       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2331         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2332     }
2333 }
2334
2335 gint
2336 gtk_cmclist_get_text (GtkCMCList  *clist,
2337                     gint       row,
2338                     gint       column,
2339                     gchar    **text)
2340 {
2341   GtkCMCListRow *clist_row;
2342
2343   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2344
2345   if (row < 0 || row >= clist->rows)
2346     return 0;
2347   if (column < 0 || column >= clist->columns)
2348     return 0;
2349
2350   clist_row = ROW_ELEMENT (clist, row)->data;
2351
2352   if (clist_row->cell[column].type != GTK_CMCELL_TEXT)
2353     return 0;
2354
2355   if (text)
2356     *text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2357
2358   return 1;
2359 }
2360
2361 void
2362 gtk_cmclist_set_pixbuf (GtkCMCList  *clist,
2363                       gint       row,
2364                       gint       column,
2365                       GdkPixbuf *pixbuf)
2366 {
2367   GtkCMCListRow *clist_row;
2368
2369   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2370
2371   if (row < 0 || row >= clist->rows)
2372     return;
2373   if (column < 0 || column >= clist->columns)
2374     return;
2375
2376   clist_row = ROW_ELEMENT (clist, row)->data;
2377   
2378   g_object_ref (pixbuf);
2379   
2380   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2381     (clist, clist_row, column, GTK_CMCELL_PIXBUF, NULL, 0, pixbuf);
2382
2383   /* redraw the list if it's not frozen */
2384   if (CLIST_UNFROZEN (clist))
2385     {
2386       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2387         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2388     }
2389 }
2390
2391 gint
2392 gtk_cmclist_get_pixbuf (GtkCMCList   *clist,
2393                       gint        row,
2394                       gint        column,
2395                       GdkPixbuf **pixbuf)
2396 {
2397   GtkCMCListRow *clist_row;
2398
2399   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2400
2401   if (row < 0 || row >= clist->rows)
2402     return 0;
2403   if (column < 0 || column >= clist->columns)
2404     return 0;
2405
2406   clist_row = ROW_ELEMENT (clist, row)->data;
2407
2408   if (clist_row->cell[column].type != GTK_CMCELL_PIXBUF)
2409     return 0;
2410
2411   if (pixbuf)
2412   {
2413     *pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2414   }
2415
2416   return 1;
2417 }
2418
2419 void
2420 gtk_cmclist_set_pixtext (GtkCMCList    *clist,
2421                        gint         row,
2422                        gint         column,
2423                        const gchar *text,
2424                        guint8       spacing,
2425                        GdkPixbuf   *pixbuf)
2426 {
2427   GtkCMCListRow *clist_row;
2428
2429   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2430
2431   if (row < 0 || row >= clist->rows)
2432     return;
2433   if (column < 0 || column >= clist->columns)
2434     return;
2435
2436   clist_row = ROW_ELEMENT (clist, row)->data;
2437   
2438   g_object_ref (pixbuf);
2439   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2440     (clist, clist_row, column, GTK_CMCELL_PIXTEXT, text, spacing, pixbuf);
2441
2442   /* redraw the list if it's not frozen */
2443   if (CLIST_UNFROZEN (clist))
2444     {
2445       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2446         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2447     }
2448 }
2449
2450 gint
2451 gtk_cmclist_get_pixtext (GtkCMCList   *clist,
2452                        gint        row,
2453                        gint        column,
2454                        gchar     **text,
2455                        guint8     *spacing,
2456                        GdkPixbuf **pixbuf)
2457 {
2458   GtkCMCListRow *clist_row;
2459
2460   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2461
2462   if (row < 0 || row >= clist->rows)
2463     return 0;
2464   if (column < 0 || column >= clist->columns)
2465     return 0;
2466
2467   clist_row = ROW_ELEMENT (clist, row)->data;
2468
2469   if (clist_row->cell[column].type != GTK_CMCELL_PIXTEXT)
2470     return 0;
2471
2472   if (text)
2473     *text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2474   if (spacing)
2475     *spacing = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2476   if (pixbuf)
2477     *pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2478
2479   return 1;
2480 }
2481
2482 void
2483 gtk_cmclist_set_shift (GtkCMCList *clist,
2484                      gint      row,
2485                      gint      column,
2486                      gint      vertical,
2487                      gint      horizontal)
2488 {
2489   GtkRequisition requisition = { 0 };
2490   GtkCMCListRow *clist_row;
2491
2492   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2493
2494   if (row < 0 || row >= clist->rows)
2495     return;
2496   if (column < 0 || column >= clist->columns)
2497     return;
2498
2499   clist_row = ROW_ELEMENT (clist, row)->data;
2500
2501   if (clist->column[column].auto_resize &&
2502       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2503     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2504                                                    column, &requisition);
2505
2506   clist_row->cell[column].vertical = vertical;
2507   clist_row->cell[column].horizontal = horizontal;
2508
2509   column_auto_resize (clist, clist_row, column, requisition.width);
2510
2511   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2512     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2513 }
2514
2515 /* PRIVATE CELL FUNCTIONS
2516  *   set_cell_contents
2517  *   cell_size_request
2518  */
2519 static void
2520 set_cell_contents (GtkCMCList    *clist,
2521                    GtkCMCListRow *clist_row,
2522                    gint         column,
2523                    GtkCMCellType  type,
2524                    const gchar *text,
2525                    guint8       spacing,
2526                    GdkPixbuf   *pixbuf)
2527 {
2528   GtkRequisition requisition;
2529   gchar *old_text = NULL;
2530   GdkPixbuf *old_pixbuf = NULL;
2531   
2532   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2533   cm_return_if_fail (clist_row != NULL);
2534
2535   if (clist->column[column].auto_resize &&
2536       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2537     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2538                                                    column, &requisition);
2539
2540   switch (clist_row->cell[column].type)
2541     {
2542     case GTK_CMCELL_EMPTY:
2543       break;
2544     case GTK_CMCELL_TEXT:
2545       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2546       break;
2547     case GTK_CMCELL_PIXBUF:
2548       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2549       break;
2550     case GTK_CMCELL_PIXTEXT:
2551       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2552       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2553       break;
2554     case GTK_CMCELL_WIDGET:
2555       /* unimplemented */
2556       break;
2557     default:
2558       break;
2559     }
2560
2561   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2562
2563   /* Note that pixbuf and mask were already ref'ed by the caller
2564    */
2565   switch (type)
2566     {
2567     case GTK_CMCELL_TEXT:
2568       if (text)
2569         {
2570           clist_row->cell[column].type = GTK_CMCELL_TEXT;
2571           GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2572         }
2573       break;
2574     case GTK_CMCELL_PIXBUF:
2575       if (pixbuf)
2576         {
2577           clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2578           GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2579         }
2580       break;
2581     case GTK_CMCELL_PIXTEXT:
2582       if (text && pixbuf)
2583         {
2584           clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2585           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2586           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2587           GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2588         }
2589       break;
2590     default:
2591       break;
2592     }
2593
2594   if (clist->column[column].auto_resize &&
2595       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2596     column_auto_resize (clist, clist_row, column, requisition.width);
2597
2598   g_free (old_text);
2599   if (old_pixbuf)
2600     g_object_unref (old_pixbuf);
2601 }
2602
2603 PangoLayout *
2604 _gtk_cmclist_create_cell_layout (GtkCMCList       *clist,
2605                                GtkCMCListRow    *clist_row,
2606                                gint            column)
2607 {
2608   PangoLayout *layout;
2609   GtkStyle *style;
2610   GtkCMCell *cell;
2611   gchar *text;
2612   
2613   get_cell_style (clist, clist_row, GTK_STATE_NORMAL, column, &style,
2614                   NULL, NULL);
2615
2616
2617   cell = &clist_row->cell[column];
2618   switch (cell->type)
2619     {
2620     case GTK_CMCELL_TEXT:
2621     case GTK_CMCELL_PIXTEXT:
2622       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
2623               GTK_CMCELL_PIXTEXT (*cell)->text :
2624               GTK_CMCELL_TEXT (*cell)->text);
2625
2626       if (!text)
2627         return NULL;
2628       
2629       layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
2630                                                ((cell->type == GTK_CMCELL_PIXTEXT) ?
2631                                                 GTK_CMCELL_PIXTEXT (*cell)->text :
2632                                                 GTK_CMCELL_TEXT (*cell)->text));
2633       pango_layout_set_font_description (layout, style->font_desc);
2634       
2635       return layout;
2636       
2637     default:
2638       return NULL;
2639     }
2640 }
2641
2642 static void
2643 cell_size_request (GtkCMCList       *clist,
2644                    GtkCMCListRow    *clist_row,
2645                    gint            column,
2646                    GtkRequisition *requisition)
2647 {
2648   gint width;
2649   gint height;
2650   PangoLayout *layout;
2651   PangoRectangle logical_rect;
2652
2653   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2654   cm_return_if_fail (requisition != NULL);
2655
2656   layout = _gtk_cmclist_create_cell_layout (clist, clist_row, column);
2657   if (layout)
2658     {
2659       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2660       
2661       requisition->width = logical_rect.width;
2662       requisition->height = logical_rect.height;
2663       
2664       g_object_unref (G_OBJECT (layout));
2665     }
2666   else
2667     {
2668       requisition->width  = 0;
2669       requisition->height = 0;
2670     }
2671
2672   if (layout && clist_row->cell[column].type == GTK_CMCELL_PIXTEXT)
2673     requisition->width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2674
2675   switch (clist_row->cell[column].type)
2676     {
2677     case GTK_CMCELL_PIXTEXT:
2678       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2679       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2680       requisition->width += width;
2681       requisition->height = MAX (requisition->height, height);      
2682       break;
2683     case GTK_CMCELL_PIXBUF:
2684       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2685       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2686       requisition->width += width;
2687       requisition->height = MAX (requisition->height, height);
2688       break;
2689       
2690     default:
2691       break;
2692     }
2693
2694   requisition->width  += clist_row->cell[column].horizontal;
2695   requisition->height += clist_row->cell[column].vertical;
2696 }
2697
2698 /* PUBLIC INSERT/REMOVE ROW FUNCTIONS
2699  *   gtk_cmclist_prepend
2700  *   gtk_cmclist_append
2701  *   gtk_cmclist_insert
2702  *   gtk_cmclist_remove
2703  *   gtk_cmclist_clear
2704  */
2705 gint
2706 gtk_cmclist_prepend (GtkCMCList    *clist,
2707                    gchar       *text[])
2708 {
2709   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2710   cm_return_val_if_fail (text != NULL, -1);
2711
2712   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, 0, text);
2713 }
2714
2715 gint
2716 gtk_cmclist_append (GtkCMCList    *clist,
2717                   gchar       *text[])
2718 {
2719   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2720   cm_return_val_if_fail (text != NULL, -1);
2721
2722   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, clist->rows, text);
2723 }
2724
2725 gint
2726 gtk_cmclist_insert (GtkCMCList    *clist,
2727                   gint         row,
2728                   gchar       *text[])
2729 {
2730   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2731   cm_return_val_if_fail (text != NULL, -1);
2732
2733   if (row < 0 || row > clist->rows)
2734     row = clist->rows;
2735
2736   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, row, text);
2737 }
2738
2739 void
2740 gtk_cmclist_remove (GtkCMCList *clist,
2741                   gint      row)
2742 {
2743   GTK_CMCLIST_GET_CLASS (clist)->remove_row (clist, row);
2744 }
2745
2746 void
2747 gtk_cmclist_clear (GtkCMCList *clist)
2748 {
2749   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2750   
2751   GTK_CMCLIST_GET_CLASS (clist)->clear (clist);
2752 }
2753
2754 /* PRIVATE INSERT/REMOVE ROW FUNCTIONS
2755  *   real_insert_row
2756  *   real_remove_row
2757  *   real_clear
2758  *   real_row_move
2759  */
2760 static gint
2761 real_insert_row (GtkCMCList *clist,
2762                  gint      row,
2763                  gchar    *text[])
2764 {
2765   gint i;
2766   GtkCMCListRow *clist_row;
2767
2768   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2769   cm_return_val_if_fail (text != NULL, -1);
2770
2771   /* return if out of bounds */
2772   if (row < 0 || row > clist->rows)
2773     return -1;
2774
2775   /* create the row */
2776   clist_row = row_new (clist);
2777
2778   /* set the text in the row's columns */
2779   for (i = 0; i < clist->columns; i++)
2780     if (text[i])
2781       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2782         (clist, clist_row, i, GTK_CMCELL_TEXT, text[i], 0, NULL);
2783
2784   if (!clist->rows)
2785     {
2786       clist->row_list = g_list_append (clist->row_list, clist_row);
2787       clist->row_list_end = clist->row_list;
2788     }
2789   else
2790     {
2791       if (GTK_CMCLIST_AUTO_SORT(clist))   /* override insertion pos */
2792         {
2793           GList *work;
2794           
2795           row = 0;
2796           work = clist->row_list;
2797           
2798           if (clist->sort_type == GTK_SORT_ASCENDING)
2799             {
2800               while (row < clist->rows &&
2801                      clist->compare (clist, clist_row,
2802                                      GTK_CMCLIST_ROW (work)) > 0)
2803                 {
2804                   row++;
2805                   work = work->next;
2806                 }
2807             }
2808           else
2809             {
2810               while (row < clist->rows &&
2811                      clist->compare (clist, clist_row,
2812                                      GTK_CMCLIST_ROW (work)) < 0)
2813                 {
2814                   row++;
2815                   work = work->next;
2816                 }
2817             }
2818         }
2819       
2820       /* reset the row end pointer if we're inserting at the end of the list */
2821       if (row == clist->rows)
2822         clist->row_list_end = (g_list_append (clist->row_list_end,
2823                                               clist_row))->next;
2824       else
2825         clist->row_list = g_list_insert (clist->row_list, clist_row, row);
2826
2827     }
2828   clist->rows++;
2829
2830   if (row < ROW_FROM_YPIXEL (clist, 0))
2831     clist->voffset -= (clist->row_height + CELL_SPACING);
2832
2833   /* syncronize the selection list */
2834   sync_selection (clist, row, SYNC_INSERT);
2835
2836   if (clist->rows == 1)
2837     {
2838       clist->focus_row = 0;
2839       if (clist->selection_mode == GTK_SELECTION_BROWSE)
2840         gtk_cmclist_select_row (clist, 0, -1);
2841     }
2842
2843   /* redraw the list if it isn't frozen */
2844   if (CLIST_UNFROZEN (clist))
2845     {
2846       adjust_adjustments (clist, FALSE);
2847
2848       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2849         draw_rows (clist, NULL);
2850     }
2851
2852   return row;
2853 }
2854
2855 static void
2856 real_remove_row (GtkCMCList *clist,
2857                  gint      row)
2858 {
2859   gint was_visible;
2860   GList *list;
2861   GtkCMCListRow *clist_row;
2862
2863   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2864
2865   /* return if out of bounds */
2866   if (row < 0 || row > (clist->rows - 1))
2867     return;
2868
2869   was_visible = (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE);
2870
2871   /* get the row we're going to delete */
2872   list = ROW_ELEMENT (clist, row);
2873   g_assert (list != NULL);
2874   clist_row = list->data;
2875
2876   /* if we're removing a selected row, we have to make sure
2877    * it's properly unselected, and then sync up the clist->selected
2878    * list to reflect the deincrimented indexies of rows after the
2879    * removal */
2880   if (clist_row->state == GTK_STATE_SELECTED)
2881     g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
2882                      row, -1, NULL);
2883
2884   sync_selection (clist, row, SYNC_REMOVE);
2885
2886   /* reset the row end pointer if we're removing at the end of the list */
2887   clist->rows--;
2888   if (clist->row_list == list)
2889     clist->row_list = g_list_next (list);
2890   if (clist->row_list_end == list)
2891     clist->row_list_end = g_list_previous (list);
2892   list = g_list_remove (list, clist_row);
2893
2894   if (row < ROW_FROM_YPIXEL (clist, 0))
2895     clist->voffset += clist->row_height + CELL_SPACING;
2896
2897   if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
2898       clist->focus_row >= 0)
2899     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
2900                      clist->focus_row, -1, NULL);
2901
2902   /* toast the row */
2903   row_delete (clist, clist_row);
2904
2905   /* redraw the row if it isn't frozen */
2906   if (CLIST_UNFROZEN (clist))
2907     {
2908       adjust_adjustments (clist, FALSE);
2909
2910       if (was_visible)
2911         draw_rows (clist, NULL);
2912     }
2913 }
2914
2915 static void
2916 real_clear (GtkCMCList *clist)
2917 {
2918   GList *list;
2919   GList *free_list;
2920   gint i;
2921
2922   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2923
2924   /* free up the selection list */
2925   g_list_free (clist->selection);
2926   g_list_free (clist->undo_selection);
2927   g_list_free (clist->undo_unselection);
2928
2929   clist->selection = NULL;
2930   clist->selection_end = NULL;
2931   clist->undo_selection = NULL;
2932   clist->undo_unselection = NULL;
2933   clist->voffset = 0;
2934   clist->focus_row = -1;
2935   clist->anchor = -1;
2936   clist->undo_anchor = -1;
2937   clist->anchor_state = GTK_STATE_SELECTED;
2938   clist->drag_pos = -1;
2939
2940   /* remove all the rows */
2941   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
2942   free_list = clist->row_list;
2943   clist->row_list = NULL;
2944   clist->row_list_end = NULL;
2945   clist->rows = 0;
2946   for (list = free_list; list; list = list->next)
2947     row_delete (clist, GTK_CMCLIST_ROW (list));
2948   g_list_free (free_list);
2949   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
2950   for (i = 0; i < clist->columns; i++)
2951     if (clist->column[i].auto_resize)
2952       {
2953         if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2954           gtk_cmclist_set_column_width
2955             (clist, i, (clist->column[i].button->requisition.width -
2956                         (CELL_SPACING + (2 * COLUMN_INSET))));
2957         else
2958           gtk_cmclist_set_column_width (clist, i, 0);
2959       }
2960   /* zero-out the scrollbars */
2961   if (clist->vadjustment)
2962     {
2963       gtk_adjustment_set_value (clist->vadjustment, 0.0);
2964       CLIST_REFRESH (clist);
2965     }
2966   else
2967     gtk_widget_queue_resize (GTK_WIDGET (clist));
2968 }
2969
2970 static void
2971 real_row_move (GtkCMCList *clist,
2972                gint      source_row,
2973                gint      dest_row)
2974 {
2975   GtkCMCListRow *clist_row;
2976   GList *list;
2977   gint first, last;
2978   gint d;
2979
2980   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2981
2982   if (GTK_CMCLIST_AUTO_SORT(clist))
2983     return;
2984
2985   if (source_row < 0 || source_row >= clist->rows ||
2986       dest_row   < 0 || dest_row   >= clist->rows ||
2987       source_row == dest_row)
2988     return;
2989
2990   gtk_cmclist_freeze (clist);
2991
2992   /* unlink source row */
2993   clist_row = ROW_ELEMENT (clist, source_row)->data;
2994   if (source_row == clist->rows - 1)
2995     clist->row_list_end = clist->row_list_end->prev;
2996   clist->row_list = g_list_remove (clist->row_list, clist_row);
2997   clist->rows--;
2998
2999   /* relink source row */
3000   clist->row_list = g_list_insert (clist->row_list, clist_row, dest_row);
3001   if (dest_row == clist->rows)
3002     clist->row_list_end = clist->row_list_end->next;
3003   clist->rows++;
3004
3005   /* sync selection */
3006   if (source_row > dest_row)
3007     {
3008       first = dest_row;
3009       last  = source_row;
3010       d = 1;
3011     }
3012   else
3013     {
3014       first = source_row;
3015       last  = dest_row;
3016       d = -1;
3017     }
3018
3019   for (list = clist->selection; list; list = list->next)
3020     {
3021       if (list->data == GINT_TO_POINTER (source_row))
3022         list->data = GINT_TO_POINTER (dest_row);
3023       else if (first <= GPOINTER_TO_INT (list->data) &&
3024                last >= GPOINTER_TO_INT (list->data))
3025         list->data = GINT_TO_POINTER (GPOINTER_TO_INT (list->data) + d);
3026     }
3027   
3028   if (clist->focus_row == source_row)
3029     clist->focus_row = dest_row;
3030   else if (clist->focus_row > first)
3031     clist->focus_row += d;
3032
3033   gtk_cmclist_thaw (clist);
3034 }
3035
3036 /* PUBLIC ROW FUNCTIONS
3037  *   gtk_cmclist_moveto
3038  *   gtk_cmclist_set_row_height
3039  *   gtk_cmclist_set_row_data
3040  *   gtk_cmclist_set_row_data_full
3041  *   gtk_cmclist_get_row_data
3042  *   gtk_cmclist_find_row_from_data
3043  *   gtk_cmclist_swap_rows
3044  *   gtk_cmclist_row_move
3045  *   gtk_cmclist_row_is_visible
3046  *   gtk_cmclist_set_foreground
3047  *   gtk_cmclist_set_background
3048  */
3049 void
3050 gtk_cmclist_moveto (GtkCMCList *clist,
3051                   gint      row,
3052                   gint      column,
3053                   gfloat    row_align,
3054                   gfloat    col_align)
3055 {
3056   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3057
3058   if (row < -1 || row >= clist->rows)
3059     return;
3060   if (column < -1 || column >= clist->columns)
3061     return;
3062
3063   row_align = CLAMP (row_align, 0, 1);
3064   col_align = CLAMP (col_align, 0, 1);
3065
3066   /* adjust horizontal scrollbar */
3067   if (clist->hadjustment && column >= 0)
3068     {
3069       gint x;
3070
3071       x = (COLUMN_LEFT (clist, column) - CELL_SPACING - COLUMN_INSET -
3072            (col_align * (clist->clist_window_width - 2 * COLUMN_INSET -
3073                          CELL_SPACING - clist->column[column].area.width)));
3074       if (x < 0)
3075         gtk_adjustment_set_value (clist->hadjustment, 0.0);
3076       else if (x > LIST_WIDTH (clist) - clist->clist_window_width)
3077         gtk_adjustment_set_value 
3078           (clist->hadjustment, LIST_WIDTH (clist) - clist->clist_window_width);
3079       else
3080         gtk_adjustment_set_value (clist->hadjustment, x);
3081     }
3082
3083   /* adjust vertical scrollbar */
3084   if (clist->vadjustment && row >= 0)
3085     move_vertical (clist, row, row_align);
3086 }
3087
3088 void
3089 gtk_cmclist_set_row_height (GtkCMCList *clist,
3090                           guint     height)
3091 {
3092   GtkWidget *widget;
3093
3094   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3095
3096   widget = GTK_WIDGET (clist);
3097
3098   if (height > 0)
3099     {
3100       clist->row_height = height;
3101       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3102     }
3103   else
3104     {
3105       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3106       clist->row_height = 0;
3107     }
3108
3109   if (widget->style->font_desc)
3110     {
3111       PangoContext *context = gtk_widget_get_pango_context (widget);
3112       PangoFontMetrics *metrics;
3113
3114       metrics = pango_context_get_metrics (context,
3115                                            widget->style->font_desc,
3116                                            pango_context_get_language (context));
3117       
3118       if (!GTK_CMCLIST_ROW_HEIGHT_SET(clist))
3119         {
3120           clist->row_height = (pango_font_metrics_get_ascent (metrics) +
3121                                pango_font_metrics_get_descent (metrics));
3122           clist->row_height = PANGO_PIXELS (clist->row_height);
3123         }
3124
3125       pango_font_metrics_unref (metrics);
3126     }
3127       
3128   CLIST_REFRESH (clist);
3129 }
3130
3131 void
3132 gtk_cmclist_set_row_data (GtkCMCList *clist,
3133                         gint      row,
3134                         gpointer  data)
3135 {
3136   gtk_cmclist_set_row_data_full (clist, row, data, NULL);
3137 }
3138
3139 void
3140 gtk_cmclist_set_row_data_full (GtkCMCList         *clist,
3141                              gint              row,
3142                              gpointer          data,
3143                              GDestroyNotify  destroy)
3144 {
3145   GtkCMCListRow *clist_row;
3146
3147   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3148
3149   if (row < 0 || row > (clist->rows - 1))
3150     return;
3151
3152   clist_row = ROW_ELEMENT (clist, row)->data;
3153
3154   if (clist_row->destroy)
3155     clist_row->destroy (clist_row->data);
3156   
3157   clist_row->data = data;
3158   clist_row->destroy = destroy;
3159 }
3160
3161 gpointer
3162 gtk_cmclist_get_row_data (GtkCMCList *clist,
3163                         gint      row)
3164 {
3165   GtkCMCListRow *clist_row;
3166
3167   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3168
3169   if (row < 0 || row > (clist->rows - 1))
3170     return NULL;
3171
3172   clist_row = ROW_ELEMENT (clist, row)->data;
3173   return clist_row->data;
3174 }
3175
3176 gint
3177 gtk_cmclist_find_row_from_data (GtkCMCList *clist,
3178                               gpointer  data)
3179 {
3180   GList *list;
3181   gint n;
3182
3183   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
3184
3185   for (n = 0, list = clist->row_list; list; n++, list = list->next)
3186     if (GTK_CMCLIST_ROW (list)->data == data)
3187       return n;
3188
3189   return -1;
3190 }
3191
3192 void 
3193 gtk_cmclist_swap_rows (GtkCMCList *clist,
3194                      gint      row1, 
3195                      gint      row2)
3196 {
3197   gint first, last;
3198
3199   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3200   cm_return_if_fail (row1 != row2);
3201
3202   if (GTK_CMCLIST_AUTO_SORT(clist))
3203     return;
3204
3205   gtk_cmclist_freeze (clist);
3206
3207   first = MIN (row1, row2);
3208   last  = MAX (row1, row2);
3209
3210   gtk_cmclist_row_move (clist, last, first);
3211   gtk_cmclist_row_move (clist, first + 1, last);
3212   
3213   gtk_cmclist_thaw (clist);
3214 }
3215
3216 void
3217 gtk_cmclist_row_move (GtkCMCList *clist,
3218                     gint      source_row,
3219                     gint      dest_row)
3220 {
3221   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3222
3223   if (GTK_CMCLIST_AUTO_SORT(clist))
3224     return;
3225
3226   if (source_row < 0 || source_row >= clist->rows ||
3227       dest_row   < 0 || dest_row   >= clist->rows ||
3228       source_row == dest_row)
3229     return;
3230
3231   g_signal_emit (G_OBJECT (clist), clist_signals[ROW_MOVE], 0,
3232                    source_row, dest_row);
3233 }
3234
3235 GtkVisibility
3236 gtk_cmclist_row_is_visible (GtkCMCList *clist,
3237                           gint      row)
3238 {
3239   gint top;
3240
3241   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
3242
3243   if (row < 0 || row >= clist->rows)
3244     return GTK_VISIBILITY_NONE;
3245
3246   if (clist->row_height == 0)
3247     return GTK_VISIBILITY_NONE;
3248
3249   if (row < ROW_FROM_YPIXEL (clist, 0))
3250     return GTK_VISIBILITY_NONE;
3251
3252   if (row > ROW_FROM_YPIXEL (clist, clist->clist_window_height))
3253     return GTK_VISIBILITY_NONE;
3254
3255   top = ROW_TOP_YPIXEL (clist, row);
3256
3257   if ((top < 0)
3258       || ((top + clist->row_height) >= clist->clist_window_height))
3259     return GTK_VISIBILITY_PARTIAL;
3260
3261   return GTK_VISIBILITY_FULL;
3262 }
3263
3264 void
3265 gtk_cmclist_set_foreground (GtkCMCList       *clist,
3266                           gint            row,
3267                           const GdkColor *color)
3268 {
3269   GtkCMCListRow *clist_row;
3270
3271   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3272
3273   if (row < 0 || row >= clist->rows)
3274     return;
3275
3276   clist_row = ROW_ELEMENT (clist, row)->data;
3277
3278   if (color)
3279     {
3280       clist_row->foreground = *color;
3281       clist_row->fg_set = TRUE;
3282       if (gtkut_widget_get_realized (GTK_WIDGET(clist)))
3283         gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3284                          &clist_row->foreground, TRUE, TRUE);
3285     }
3286   else
3287     clist_row->fg_set = FALSE;
3288
3289   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3290     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3291 }
3292
3293 void
3294 gtk_cmclist_set_background (GtkCMCList       *clist,
3295                           gint            row,
3296                           const GdkColor *color)
3297 {
3298   GtkCMCListRow *clist_row;
3299
3300   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3301
3302   if (row < 0 || row >= clist->rows)
3303     return;
3304
3305   clist_row = ROW_ELEMENT (clist, row)->data;
3306
3307   if (color)
3308     {
3309       clist_row->background = *color;
3310       clist_row->bg_set = TRUE;
3311       if (gtkut_widget_get_realized (GTK_WIDGET(clist)))
3312         gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3313                          &clist_row->background, TRUE, TRUE);
3314     }
3315   else
3316     clist_row->bg_set = FALSE;
3317
3318   if (CLIST_UNFROZEN (clist)
3319       && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3320     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3321 }
3322
3323 /* PUBLIC ROW/CELL STYLE FUNCTIONS
3324  *   gtk_cmclist_set_cell_style
3325  *   gtk_cmclist_get_cell_style
3326  *   gtk_cmclist_set_row_style
3327  *   gtk_cmclist_get_row_style
3328  */
3329 void
3330 gtk_cmclist_set_cell_style (GtkCMCList *clist,
3331                           gint      row,
3332                           gint      column,
3333                           GtkStyle *style)
3334 {
3335   GtkRequisition requisition = { 0 };
3336   GtkCMCListRow *clist_row;
3337
3338   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3339
3340   if (row < 0 || row >= clist->rows)
3341     return;
3342   if (column < 0 || column >= clist->columns)
3343     return;
3344
3345   clist_row = ROW_ELEMENT (clist, row)->data;
3346
3347   if (clist_row->cell[column].style == style)
3348     return;
3349
3350   if (clist->column[column].auto_resize &&
3351       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3352     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3353                                                    column, &requisition);
3354
3355   if (clist_row->cell[column].style)
3356     {
3357       if (gtkut_widget_get_realized (GTK_WIDGET(clist)))
3358         gtk_style_detach (clist_row->cell[column].style);
3359       g_object_unref (clist_row->cell[column].style);
3360     }
3361
3362   clist_row->cell[column].style = style;
3363
3364   if (clist_row->cell[column].style)
3365     {
3366       g_object_ref (clist_row->cell[column].style);
3367       
3368       if (gtkut_widget_get_realized (GTK_WIDGET(clist)))
3369         clist_row->cell[column].style =
3370           gtk_style_attach (clist_row->cell[column].style,
3371                             clist->clist_window);
3372     }
3373
3374   column_auto_resize (clist, clist_row, column, requisition.width);
3375
3376   /* redraw the list if it's not frozen */
3377   if (CLIST_UNFROZEN (clist))
3378     {
3379       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3380         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3381     }
3382 }
3383
3384 GtkStyle *
3385 gtk_cmclist_get_cell_style (GtkCMCList *clist,
3386                           gint      row,
3387                           gint      column)
3388 {
3389   GtkCMCListRow *clist_row;
3390
3391   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3392
3393   if (row < 0 || row >= clist->rows || column < 0 || column >= clist->columns)
3394     return NULL;
3395
3396   clist_row = ROW_ELEMENT (clist, row)->data;
3397
3398   return clist_row->cell[column].style;
3399 }
3400
3401 void
3402 gtk_cmclist_set_row_style (GtkCMCList *clist,
3403                          gint      row,
3404                          GtkStyle *style)
3405 {
3406   GtkRequisition requisition;
3407   GtkCMCListRow *clist_row;
3408   gint *old_width;
3409   gint i;
3410
3411   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3412
3413   if (row < 0 || row >= clist->rows)
3414     return;
3415
3416   clist_row = ROW_ELEMENT (clist, row)->data;
3417
3418   if (clist_row->style == style)
3419     return;
3420
3421   old_width = g_new (gint, clist->columns);
3422
3423   if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3424     {
3425       for (i = 0; i < clist->columns; i++)
3426         if (clist->column[i].auto_resize)
3427           {
3428             GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3429                                                            i, &requisition);
3430             old_width[i] = requisition.width;
3431           }
3432     }
3433
3434   if (clist_row->style)
3435     {
3436       if (gtkut_widget_get_realized (GTK_WIDGET(clist)))
3437         gtk_style_detach (clist_row->style);
3438       g_object_unref (clist_row->style);
3439     }
3440
3441   clist_row->style = style;
3442
3443   if (clist_row->style)
3444     {
3445       g_object_ref (clist_row->style);
3446       
3447       if (gtkut_widget_get_realized (GTK_WIDGET(clist)))
3448         clist_row->style = gtk_style_attach (clist_row->style,
3449                                              clist->clist_window);
3450     }
3451
3452   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3453     for (i = 0; i < clist->columns; i++)
3454       column_auto_resize (clist, clist_row, i, old_width[i]);
3455
3456   g_free (old_width);
3457
3458   /* redraw the list if it's not frozen */
3459   if (CLIST_UNFROZEN (clist))
3460     {
3461       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3462         GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3463     }
3464 }
3465
3466 GtkStyle *
3467 gtk_cmclist_get_row_style (GtkCMCList *clist,
3468                          gint      row)
3469 {
3470   GtkCMCListRow *clist_row;
3471
3472   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3473
3474   if (row < 0 || row >= clist->rows)
3475     return NULL;
3476
3477   clist_row = ROW_ELEMENT (clist, row)->data;
3478
3479   return clist_row->style;
3480 }
3481
3482 /* PUBLIC SELECTION FUNCTIONS
3483  *   gtk_cmclist_set_selectable
3484  *   gtk_cmclist_get_selectable
3485  *   gtk_cmclist_select_row
3486  *   gtk_cmclist_unselect_row
3487  *   gtk_cmclist_select_all
3488  *   gtk_cmclist_unselect_all
3489  *   gtk_cmclist_undo_selection
3490  */
3491 void
3492 gtk_cmclist_set_selectable (GtkCMCList *clist,
3493                           gint      row,
3494                           gboolean  selectable)
3495 {
3496   GtkCMCListRow *clist_row;
3497
3498   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3499
3500   if (row < 0 || row >= clist->rows)
3501     return;
3502
3503   clist_row = ROW_ELEMENT (clist, row)->data;
3504
3505   if (selectable == clist_row->selectable)
3506     return;
3507
3508   clist_row->selectable = selectable;
3509
3510   if (!selectable && clist_row->state == GTK_STATE_SELECTED)
3511     {
3512       if (clist->anchor >= 0 &&
3513           clist->selection_mode == GTK_SELECTION_MULTIPLE)
3514         {
3515           clist->drag_button = 0;
3516           remove_grab (clist);
3517           GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3518         }
3519       g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3520                        row, -1, NULL);
3521     }      
3522 }
3523
3524 gboolean
3525 gtk_cmclist_get_selectable (GtkCMCList *clist,
3526                           gint      row)
3527 {
3528   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), FALSE);
3529
3530   if (row < 0 || row >= clist->rows)
3531     return FALSE;
3532
3533   return GTK_CMCLIST_ROW (ROW_ELEMENT (clist, row))->selectable;
3534 }
3535
3536 void
3537 gtk_cmclist_select_row (GtkCMCList *clist,
3538                       gint      row,
3539                       gint      column)
3540 {
3541   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3542
3543   if (row < 0 || row >= clist->rows)
3544     return;
3545   if (column < -1 || column >= clist->columns)
3546     return;
3547
3548   g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3549                    row, column, NULL);
3550 }
3551
3552 void
3553 gtk_cmclist_unselect_row (GtkCMCList *clist,
3554                         gint      row,
3555                         gint      column)
3556 {
3557   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3558
3559   if (row < 0 || row >= clist->rows)
3560     return;
3561   if (column < -1 || column >= clist->columns)
3562     return;
3563
3564   g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3565                    row, column, NULL);
3566 }
3567
3568 void
3569 gtk_cmclist_select_all (GtkCMCList *clist)
3570 {
3571   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3572
3573   GTK_CMCLIST_GET_CLASS (clist)->select_all (clist);
3574 }
3575
3576 void
3577 gtk_cmclist_unselect_all (GtkCMCList *clist)
3578 {
3579   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3580
3581   GTK_CMCLIST_GET_CLASS (clist)->unselect_all (clist);
3582 }
3583
3584 void
3585 gtk_cmclist_undo_selection (GtkCMCList *clist)
3586 {
3587   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3588
3589   if (clist->selection_mode == GTK_SELECTION_MULTIPLE &&
3590       (clist->undo_selection || clist->undo_unselection))
3591     g_signal_emit (G_OBJECT (clist), clist_signals[UNDO_SELECTION], 0);
3592 }
3593
3594 /* PRIVATE SELECTION FUNCTIONS
3595  *   selection_find
3596  *   toggle_row
3597  *   fake_toggle_row
3598  *   toggle_focus_row
3599  *   toggle_add_mode
3600  *   real_select_row
3601  *   real_unselect_row
3602  *   real_select_all
3603  *   real_unselect_all
3604  *   fake_unselect_all
3605  *   real_undo_selection
3606  *   set_anchor
3607  *   resync_selection
3608  *   update_extended_selection
3609  *   start_selection
3610  *   end_selection
3611  *   extend_selection