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