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
COLUMN_FROM_XPIXEL(GtkCMCList * clist,gint x)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
LIST_WIDTH(GtkCMCList * clist)114 LIST_WIDTH (GtkCMCList * clist)
115 {
116   gint last_column;
117 
118   for (last_column = clist->columns - 1;
119        last_column >= 0 && !clist->column[last_column].visible; last_column--);
120 
121   if (last_column >= 0)
122     return (clist->column[last_column].area.x +
123 	    clist->column[last_column].area.width +
124 	    COLUMN_INSET + CELL_SPACING);
125   return 0;
126 }
127 
128 /* returns the GList item for the nth row */
129 #define	ROW_ELEMENT(clist, row)	(((row) == (clist)->rows - 1) ? \
130 				 (clist)->row_list_end : \
131 				 g_list_nth ((clist)->row_list, (row)))
132 
133 
134 /* redraw the list if it's not frozen */
135 #define CLIST_UNFROZEN(clist)     (((GtkCMCList*) (clist))->freeze_count == 0)
136 #define	CLIST_REFRESH(clist)	G_STMT_START { \
137   if (CLIST_UNFROZEN (clist)) \
138     GTK_CMCLIST_GET_CLASS (clist)->refresh ((GtkCMCList*) (clist)); \
139 } G_STMT_END
140 
141 
142 /* Signals */
143 enum {
144   SELECT_ROW,
145   UNSELECT_ROW,
146   ROW_MOVE,
147   CLICK_COLUMN,
148   RESIZE_COLUMN,
149   TOGGLE_FOCUS_ROW,
150   SELECT_ALL,
151   UNSELECT_ALL,
152   UNDO_SELECTION,
153   START_SELECTION,
154   END_SELECTION,
155   TOGGLE_ADD_MODE,
156   EXTEND_SELECTION,
157   SCROLL_VERTICAL,
158   SCROLL_HORIZONTAL,
159   ABORT_COLUMN_RESIZE,
160   LAST_SIGNAL
161 };
162 
163 enum {
164   SYNC_REMOVE,
165   SYNC_INSERT
166 };
167 
168 enum {
169   ARG_0,
170   ARG_N_COLUMNS,
171   ARG_SHADOW_TYPE,
172   ARG_SELECTION_MODE,
173   ARG_ROW_HEIGHT,
174   ARG_TITLES_ACTIVE,
175   ARG_REORDERABLE,
176   ARG_USE_DRAG_ICONS,
177   ARG_SORT_TYPE
178 };
179 
180 /* GtkCMCList Methods */
181 static void     gtk_cmclist_class_init  (GtkCMCListClass         *klass);
182 static void     gtk_cmclist_init        (GtkCMCList              *clist);
183 static GObject* gtk_cmclist_constructor (GType                  type,
184 				       guint                  n_construct_properties,
185 				       GObjectConstructParam *construct_params);
186 
187 /* GtkObject Methods */
188 static void gtk_cmclist_destroy  (GtkObject *object);
189 static void gtk_cmclist_finalize (GObject   *object);
190 static void gtk_cmclist_set_arg  (GObject *object,
191 				guint      arg_id,
192 				const GValue *value,
193 				GParamSpec *spec);
194 static void gtk_cmclist_get_arg  (GObject *object,
195 				guint      arg_id,
196 				GValue *value,
197 				GParamSpec *spec);
198 
199 /* GtkWidget Methods */
200 static void gtk_cmclist_set_scroll_adjustments (GtkCMCList      *clist,
201 					      GtkAdjustment *hadjustment,
202 					      GtkAdjustment *vadjustment);
203 static void gtk_cmclist_realize         (GtkWidget        *widget);
204 static void gtk_cmclist_unrealize       (GtkWidget        *widget);
205 static void gtk_cmclist_map             (GtkWidget        *widget);
206 static void gtk_cmclist_unmap           (GtkWidget        *widget);
207 static gint gtk_cmclist_expose          (GtkWidget        *widget,
208                                          GdkEventExpose   *event);
209 static gint gtk_cmclist_button_press    (GtkWidget        *widget,
210 				       GdkEventButton   *event);
211 static gint gtk_cmclist_button_release  (GtkWidget        *widget,
212 				       GdkEventButton   *event);
213 static gint gtk_cmclist_motion          (GtkWidget        *widget,
214 			               GdkEventMotion   *event);
215 static void gtk_cmclist_size_request    (GtkWidget        *widget,
216 				       GtkRequisition   *requisition);
217 static void gtk_cmclist_size_allocate   (GtkWidget        *widget,
218 				       GtkAllocation    *allocation);
219 static void gtk_cmclist_undraw_focus      (GtkWidget        *widget);
220 static void gtk_cmclist_draw_focus      (GtkWidget        *widget);
221 static gint gtk_cmclist_focus_in        (GtkWidget        *widget,
222 				       GdkEventFocus    *event);
223 static gint gtk_cmclist_focus_out       (GtkWidget        *widget,
224 				       GdkEventFocus    *event);
225 static gint gtk_cmclist_focus           (GtkWidget        *widget,
226 				       GtkDirectionType  direction);
227 static void gtk_cmclist_set_focus_child (GtkContainer     *container,
228 				       GtkWidget        *child);
229 static void gtk_cmclist_style_set       (GtkWidget        *widget,
230 				       GtkStyle         *previous_style);
231 static void gtk_cmclist_drag_begin      (GtkWidget        *widget,
232 				       GdkDragContext   *context);
233 static gint gtk_cmclist_drag_motion     (GtkWidget        *widget,
234 				       GdkDragContext   *context,
235 				       gint              x,
236 				       gint              y,
237 				       guint             time);
238 static void gtk_cmclist_drag_leave      (GtkWidget        *widget,
239 				       GdkDragContext   *context,
240 				       guint             time);
241 static void gtk_cmclist_drag_end        (GtkWidget        *widget,
242 				       GdkDragContext   *context);
243 static gboolean gtk_cmclist_drag_drop   (GtkWidget      *widget,
244 				       GdkDragContext *context,
245 				       gint            x,
246 				       gint            y,
247 				       guint           time);
248 static void gtk_cmclist_drag_data_get   (GtkWidget        *widget,
249 				       GdkDragContext   *context,
250 				       GtkSelectionData *selection_data,
251 				       guint             info,
252 				       guint             time);
253 static void gtk_cmclist_drag_data_received (GtkWidget        *widget,
254 					  GdkDragContext   *context,
255 					  gint              x,
256 					  gint              y,
257 					  GtkSelectionData *selection_data,
258 					  guint             info,
259 					  guint             time);
260 
261 /* GtkContainer Methods */
262 static void gtk_cmclist_forall          (GtkContainer  *container,
263 			               gboolean       include_internals,
264 			               GtkCallback    callback,
265 			               gpointer       callback_data);
266 
267 /* Selection */
268 static void toggle_row                (GtkCMCList      *clist,
269 			               gint           row,
270 			               gint           column,
271 			               GdkEvent      *event);
272 static void real_select_row           (GtkCMCList      *clist,
273 			               gint           row,
274 			               gint           column,
275 			               GdkEvent      *event);
276 static void real_unselect_row         (GtkCMCList      *clist,
277 			               gint           row,
278 			               gint           column,
279 			               GdkEvent      *event);
280 static void update_extended_selection (GtkCMCList      *clist,
281 				       gint           row);
282 static GList *selection_find          (GtkCMCList      *clist,
283 			               gint           row_number,
284 			               GList         *row_list_element);
285 static void real_select_all           (GtkCMCList      *clist);
286 static void real_unselect_all         (GtkCMCList      *clist);
287 static void move_vertical             (GtkCMCList      *clist,
288 			               gint           row,
289 			               gfloat         align);
290 static void move_horizontal           (GtkCMCList      *clist,
291 			               gint           diff);
292 static void real_undo_selection       (GtkCMCList      *clist);
293 static void fake_unselect_all         (GtkCMCList      *clist,
294 			               gint           row);
295 static void fake_toggle_row           (GtkCMCList      *clist,
296 			               gint           row);
297 static void resync_selection          (GtkCMCList      *clist,
298 			               GdkEvent      *event);
299 static void sync_selection            (GtkCMCList      *clist,
300 	                               gint           row,
301                                        gint           mode);
302 static void set_anchor                (GtkCMCList      *clist,
303 			               gboolean       add_mode,
304 			               gint           anchor,
305 			               gint           undo_anchor);
306 static void start_selection           (GtkCMCList      *clist);
307 static void end_selection             (GtkCMCList      *clist);
308 static void toggle_add_mode           (GtkCMCList      *clist);
309 static void toggle_focus_row          (GtkCMCList      *clist);
310 static void extend_selection          (GtkCMCList      *clist,
311 			               GtkScrollType  scroll_type,
312 			               gfloat         position,
313 			               gboolean       auto_start_selection);
314 static gint get_selection_info        (GtkCMCList       *clist,
315 				       gint            x,
316 				       gint            y,
317 				       gint           *row,
318 				       gint           *column);
319 
320 /* Scrolling */
321 static void move_focus_row     (GtkCMCList      *clist,
322 			        GtkScrollType  scroll_type,
323 			        gfloat         position);
324 static void scroll_horizontal  (GtkCMCList      *clist,
325 			        GtkScrollType  scroll_type,
326 			        gfloat         position);
327 static void scroll_vertical    (GtkCMCList      *clist,
328 			        GtkScrollType  scroll_type,
329 			        gfloat         position);
330 static void move_horizontal    (GtkCMCList      *clist,
331 				gint           diff);
332 static void move_vertical      (GtkCMCList      *clist,
333 				gint           row,
334 				gfloat         align);
335 static gint horizontal_timeout (GtkCMCList      *clist);
336 static gint vertical_timeout   (GtkCMCList      *clist);
337 static void remove_grab        (GtkCMCList      *clist);
338 
339 
340 /* Resize Columns */
341 static void draw_xor_line             (GtkCMCList       *clist);
342 static gint new_column_width          (GtkCMCList       *clist,
343 			               gint            column,
344 			               gint           *x);
345 static void column_auto_resize        (GtkCMCList       *clist,
346 				       GtkCMCListRow    *clist_row,
347 				       gint            column,
348 				       gint            old_width);
349 static void real_resize_column        (GtkCMCList       *clist,
350 				       gint            column,
351 				       gint            width);
352 static void abort_column_resize       (GtkCMCList       *clist);
353 static void cell_size_request         (GtkCMCList       *clist,
354 			               GtkCMCListRow    *clist_row,
355 			               gint            column,
356 				       GtkRequisition *requisition);
357 
358 /* Buttons */
359 static void column_button_create      (GtkCMCList       *clist,
360 				       gint            column);
361 static void column_button_clicked     (GtkWidget      *widget,
362 				       gpointer        data);
363 
364 /* Adjustments */
365 static void adjust_adjustments        (GtkCMCList       *clist,
366 				       gboolean        block_resize);
367 static void vadjustment_value_changed (GtkAdjustment  *adjustment,
368 				       gpointer        data);
369 static void hadjustment_value_changed (GtkAdjustment  *adjustment,
370 				       gpointer        data);
371 
372 /* Drawing */
373 static void get_cell_style   (GtkCMCList      *clist,
374 			      GtkCMCListRow   *clist_row,
375 			      gint           state,
376 			      gint           column,
377 			      GtkStyle     **style);
378 static gint draw_cell_pixbuf (GdkWindow     *window,
379 			      GdkRectangle  *clip_rectangle,
380 			      cairo_t	    *cr,
381 			      GdkPixbuf     *pixbuf,
382 			      gint           x,
383 			      gint           y,
384 			      gint           width,
385 			      gint           height);
386 static void draw_row         (GtkCMCList      *clist,
387 			      GdkRectangle  *area,
388 			      gint           row,
389 			      GtkCMCListRow   *clist_row);
390 static void draw_rows        (GtkCMCList      *clist,
391 			      GdkRectangle  *area);
392 static void clist_refresh    (GtkCMCList      *clist);
393 
394 /* Size Allocation / Requisition */
395 static void size_allocate_title_buttons (GtkCMCList *clist);
396 static void size_allocate_columns       (GtkCMCList *clist,
397 					 gboolean  block_resize);
398 static gint list_requisition_width      (GtkCMCList *clist);
399 
400 /* Memory Allocation/Distruction Routines */
401 static GtkCMCListColumn *columns_new (GtkCMCList      *clist);
402 static void column_title_new       (GtkCMCList      *clist,
403 			            gint           column,
404 			            const gchar   *title);
405 static void columns_delete         (GtkCMCList      *clist);
406 static GtkCMCListRow *row_new        (GtkCMCList      *clist);
407 static void row_delete             (GtkCMCList      *clist,
408 			            GtkCMCListRow   *clist_row);
409 static void set_cell_contents      (GtkCMCList      *clist,
410 			            GtkCMCListRow   *clist_row,
411 				    gint           column,
412 				    GtkCMCellType    type,
413 				    const gchar   *text,
414 				    guint8         spacing,
415 				    GdkPixbuf     *pixbuf);
416 static gint real_insert_row        (GtkCMCList      *clist,
417 				    gint           row,
418 				    gchar         *text[]);
419 static void real_remove_row        (GtkCMCList      *clist,
420 				    gint           row);
421 static void real_clear             (GtkCMCList      *clist);
422 
423 /* Sorting */
424 static gint default_compare        (GtkCMCList      *clist,
425 			            gconstpointer  row1,
426 			            gconstpointer  row2);
427 static void real_sort_list         (GtkCMCList      *clist);
428 static GList *gtk_cmclist_merge      (GtkCMCList      *clist,
429 				    GList         *a,
430 				    GList         *b);
431 static GList *gtk_cmclist_mergesort  (GtkCMCList      *clist,
432 				    GList         *list,
433 				    gint           num);
434 /* Misc */
435 static gboolean title_focus_in   (GtkCMCList *clist,
436 				  gint      dir);
437 static gboolean title_focus_move (GtkCMCList *clist,
438 				  gint      dir);
439 
440 static void real_row_move             (GtkCMCList  *clist,
441 			               gint       source_row,
442 			               gint       dest_row);
443 static gint column_title_passive_func (GtkWidget *widget,
444 				       GdkEvent  *event,
445 				       gpointer   data);
446 static void drag_dest_cell            (GtkCMCList         *clist,
447 				       gint              x,
448 				       gint              y,
449 				       GtkCMCListDestInfo *dest_info);
450 
451 
452 
453 static guint clist_signals[LAST_SIGNAL] = {0};
454 
455 static const GtkTargetEntry clist_target_table = { "gtk-clist-drag-reorder", 0, 0};
456 
457 static gpointer gtk_cmclist_parent_class = NULL;
458 
459 GType
gtk_cmclist_get_type(void)460 gtk_cmclist_get_type (void)
461 {
462   static GType clist_type = 0;
463 
464   if (!clist_type)
465     {
466       static const GTypeInfo clist_info =
467       {
468 			sizeof (GtkCMCListClass),
469 
470 			(GBaseInitFunc) NULL,
471 			(GBaseFinalizeFunc) NULL,
472 
473 			(GClassInitFunc) gtk_cmclist_class_init,
474 			(GClassFinalizeFunc) NULL,
475 			NULL,	/* class_data */
476 
477 			sizeof (GtkCMCList),
478 			0,	/* n_preallocs */
479 			(GInstanceInitFunc) gtk_cmclist_init,
480 
481 			(const GTypeValueTable *) NULL	/* value table */
482 	};
483 	clist_type = g_type_register_static (GTK_TYPE_CONTAINER, "GtkCMCList", &clist_info, (GTypeFlags)0);
484     }
485 
486   return clist_type;
487 }
488 
489 static void
gtk_cmclist_class_init(GtkCMCListClass * klass)490 gtk_cmclist_class_init (GtkCMCListClass *klass)
491 {
492   GObjectClass *object_class = G_OBJECT_CLASS (klass);
493   GtkObjectClass *gtk_object_class;
494   GtkWidgetClass *widget_class;
495   GtkContainerClass *container_class;
496   GtkBindingSet *binding_set;
497 
498   object_class->constructor = gtk_cmclist_constructor;
499 
500   gtk_object_class = (GtkObjectClass *) klass;
501   widget_class = (GtkWidgetClass *) klass;
502   container_class = (GtkContainerClass *) klass;
503 
504   gtk_cmclist_parent_class = g_type_class_peek (GTK_TYPE_CONTAINER);
505 
506   object_class->finalize = gtk_cmclist_finalize;
507   gtk_object_class->destroy = gtk_cmclist_destroy;
508   object_class->set_property = gtk_cmclist_set_arg;
509   object_class->get_property = gtk_cmclist_get_arg;
510 
511 
512   widget_class->realize = gtk_cmclist_realize;
513   widget_class->unrealize = gtk_cmclist_unrealize;
514   widget_class->map = gtk_cmclist_map;
515   widget_class->unmap = gtk_cmclist_unmap;
516   widget_class->button_press_event = gtk_cmclist_button_press;
517   widget_class->button_release_event = gtk_cmclist_button_release;
518   widget_class->motion_notify_event = gtk_cmclist_motion;
519   widget_class->expose_event = gtk_cmclist_expose;
520   widget_class->size_request = gtk_cmclist_size_request;
521   widget_class->size_allocate = gtk_cmclist_size_allocate;
522   widget_class->focus_in_event = gtk_cmclist_focus_in;
523   widget_class->focus_out_event = gtk_cmclist_focus_out;
524   widget_class->style_set = gtk_cmclist_style_set;
525   widget_class->drag_begin = gtk_cmclist_drag_begin;
526   widget_class->drag_end = gtk_cmclist_drag_end;
527   widget_class->drag_motion = gtk_cmclist_drag_motion;
528   widget_class->drag_leave = gtk_cmclist_drag_leave;
529   widget_class->drag_drop = gtk_cmclist_drag_drop;
530   widget_class->drag_data_get = gtk_cmclist_drag_data_get;
531   widget_class->drag_data_received = gtk_cmclist_drag_data_received;
532   widget_class->focus = gtk_cmclist_focus;
533 
534   /* container_class->add = NULL; use the default GtkContainerClass warning */
535   /* container_class->remove=NULL; use the default GtkContainerClass warning */
536 
537   container_class->forall = gtk_cmclist_forall;
538   container_class->set_focus_child = gtk_cmclist_set_focus_child;
539 
540   klass->set_scroll_adjustments = gtk_cmclist_set_scroll_adjustments;
541   klass->refresh = clist_refresh;
542   klass->select_row = real_select_row;
543   klass->unselect_row = real_unselect_row;
544   klass->row_move = real_row_move;
545   klass->undo_selection = real_undo_selection;
546   klass->resync_selection = resync_selection;
547   klass->selection_find = selection_find;
548   klass->click_column = NULL;
549   klass->resize_column = real_resize_column;
550   klass->draw_row = draw_row;
551   klass->insert_row = real_insert_row;
552   klass->remove_row = real_remove_row;
553   klass->clear = real_clear;
554   klass->sort_list = real_sort_list;
555   klass->select_all = real_select_all;
556   klass->unselect_all = real_unselect_all;
557   klass->fake_unselect_all = fake_unselect_all;
558   klass->scroll_horizontal = scroll_horizontal;
559   klass->scroll_vertical = scroll_vertical;
560   klass->extend_selection = extend_selection;
561   klass->toggle_focus_row = toggle_focus_row;
562   klass->toggle_add_mode = toggle_add_mode;
563   klass->start_selection = start_selection;
564   klass->end_selection = end_selection;
565   klass->abort_column_resize = abort_column_resize;
566   klass->set_cell_contents = set_cell_contents;
567   klass->cell_size_request = cell_size_request;
568 
569   g_object_class_install_property (object_class,
570 				ARG_N_COLUMNS,
571 				g_param_spec_uint ("n-columns",
572 				"N-Columns",
573 				"N-Columns",
574 				1,
575 				G_MAXINT,
576 				1,
577 				G_PARAM_READWRITE|G_PARAM_CONSTRUCT_ONLY));
578   g_object_class_install_property (object_class,
579 				ARG_SHADOW_TYPE,
580 				g_param_spec_enum ("shadow-type",
581 				"shadow-type",
582 				"shadow-type",
583 				GTK_TYPE_SHADOW_TYPE, 0,
584 				G_PARAM_READWRITE));
585   g_object_class_install_property (object_class,
586 				ARG_SELECTION_MODE,
587 				g_param_spec_enum ("selection-mode",
588 				"selection-mode",
589 				"selection-mode",
590 				GTK_TYPE_SELECTION_MODE, 0,
591 				G_PARAM_READWRITE));
592   g_object_class_install_property (object_class,
593 				ARG_ROW_HEIGHT,
594 				g_param_spec_uint ("row-height",
595 				"row-height",
596 				"row-height",
597 				0,
598 				G_MAXINT,
599 				0,
600 				G_PARAM_READWRITE));
601   g_object_class_install_property (object_class,
602 				ARG_REORDERABLE,
603 				g_param_spec_boolean ("reorderable",
604 				"reorderable",
605 				"reorderable",
606 				TRUE,
607 				G_PARAM_READWRITE));
608   g_object_class_install_property (object_class,
609 				ARG_TITLES_ACTIVE,
610 				g_param_spec_boolean ("titles-active",
611 				"titles-active",
612 				"titles-active",
613 				TRUE,
614 				G_PARAM_READWRITE));
615   g_object_class_install_property (object_class,
616 				ARG_USE_DRAG_ICONS,
617 				g_param_spec_boolean ("use-drag-icons",
618 				"use-drag-icons",
619 				"use-drag-icons",
620 				TRUE,
621 				G_PARAM_READWRITE));
622   g_object_class_install_property (object_class,
623 				ARG_SORT_TYPE,
624 				g_param_spec_enum ("sort-type",
625 				"sort-type",
626 				"sort-type",
627 				GTK_TYPE_SORT_TYPE, 0,
628 				G_PARAM_READWRITE));
629   widget_class->set_scroll_adjustments_signal =
630  		g_signal_new ("set_scroll_adjustments",
631 			      G_TYPE_FROM_CLASS (object_class),
632 			      G_SIGNAL_RUN_LAST,
633 			      G_STRUCT_OFFSET (GtkCMCListClass, set_scroll_adjustments),
634 			      NULL, NULL,
635 			      claws_marshal_VOID__OBJECT_OBJECT,
636 			      G_TYPE_NONE, 2,
637 			      GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
638 
639   clist_signals[SELECT_ROW] =
640  		g_signal_new ("select_row",
641 			      G_TYPE_FROM_CLASS (object_class),
642 			      G_SIGNAL_RUN_FIRST,
643 			      G_STRUCT_OFFSET (GtkCMCListClass, select_row),
644 			      NULL, NULL,
645 			      claws_marshal_VOID__INT_INT_BOXED,
646 			      G_TYPE_NONE, 3,
647 			      G_TYPE_INT,
648 			      G_TYPE_INT,
649 			      GDK_TYPE_EVENT | G_SIGNAL_TYPE_STATIC_SCOPE);
650   clist_signals[UNSELECT_ROW] =
651  		g_signal_new ("unselect_row",
652 			      G_TYPE_FROM_CLASS (object_class),
653 			      G_SIGNAL_RUN_FIRST,
654 			      G_STRUCT_OFFSET (GtkCMCListClass, unselect_row),
655 			      NULL, NULL,
656 			      claws_marshal_VOID__INT_INT_BOXED,
657 			      G_TYPE_NONE, 3,
658 			      G_TYPE_INT,
659 			      G_TYPE_INT,
660 			      GDK_TYPE_EVENT);
661   clist_signals[ROW_MOVE] =
662  		g_signal_new ("row_move",
663 			      G_TYPE_FROM_CLASS (object_class),
664 			      G_SIGNAL_RUN_LAST,
665 			      G_STRUCT_OFFSET (GtkCMCListClass, row_move),
666 			      NULL, NULL,
667 			      claws_marshal_VOID__INT_INT,
668 			      G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
669   clist_signals[CLICK_COLUMN] =
670  		g_signal_new ("click_column",
671 			      G_TYPE_FROM_CLASS (object_class),
672 			      G_SIGNAL_RUN_FIRST,
673 			      G_STRUCT_OFFSET (GtkCMCListClass, click_column),
674 			      NULL, NULL,
675 			      claws_marshal_VOID__INT,
676 		    	      G_TYPE_NONE, 1, G_TYPE_INT);
677   clist_signals[RESIZE_COLUMN] =
678  		g_signal_new ("resize_column",
679 			      G_TYPE_FROM_CLASS (object_class),
680 			      G_SIGNAL_RUN_LAST,
681 			      G_STRUCT_OFFSET (GtkCMCListClass, resize_column),
682 			      NULL, NULL,
683 			      claws_marshal_VOID__INT_INT,
684 		    	      G_TYPE_NONE, 2, G_TYPE_INT, G_TYPE_INT);
685 
686   clist_signals[TOGGLE_FOCUS_ROW] =
687  		g_signal_new ("toggle_focus_row",
688 			      G_TYPE_FROM_CLASS (object_class),
689 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
690 			      G_STRUCT_OFFSET (GtkCMCListClass, toggle_focus_row),
691 			      NULL, NULL,
692 			      claws_marshal_VOID__VOID,
693 		    	      G_TYPE_NONE, 0);
694   clist_signals[SELECT_ALL] =
695  		g_signal_new ("select_all",
696 			      G_TYPE_FROM_CLASS (object_class),
697 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
698 			      G_STRUCT_OFFSET (GtkCMCListClass, select_all),
699 			      NULL, NULL,
700 			      claws_marshal_VOID__VOID,
701 		    	      G_TYPE_NONE, 0);
702   clist_signals[UNSELECT_ALL] =
703  		g_signal_new ("unselect_all",
704 			      G_TYPE_FROM_CLASS (object_class),
705 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
706 			      G_STRUCT_OFFSET (GtkCMCListClass, unselect_all),
707 			      NULL, NULL,
708 			      claws_marshal_VOID__VOID,
709 		    	      G_TYPE_NONE, 0);
710   clist_signals[UNDO_SELECTION] =
711  		g_signal_new ("undo_selection",
712 			      G_TYPE_FROM_CLASS (object_class),
713 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
714 			      G_STRUCT_OFFSET (GtkCMCListClass, undo_selection),
715 			      NULL, NULL,
716 			      claws_marshal_VOID__VOID,
717 		    	      G_TYPE_NONE, 0);
718   clist_signals[START_SELECTION] =
719  		g_signal_new ("start_selection",
720 			      G_TYPE_FROM_CLASS (object_class),
721 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
722 			      G_STRUCT_OFFSET (GtkCMCListClass, start_selection),
723 			      NULL, NULL,
724 			      claws_marshal_VOID__VOID,
725 		    	      G_TYPE_NONE, 0);
726   clist_signals[END_SELECTION] =
727  		g_signal_new ("end_selection",
728 			      G_TYPE_FROM_CLASS (object_class),
729 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
730 			      G_STRUCT_OFFSET (GtkCMCListClass, end_selection),
731 			      NULL, NULL,
732 			      claws_marshal_VOID__VOID,
733 		    	      G_TYPE_NONE, 0);
734   clist_signals[TOGGLE_ADD_MODE] =
735  		g_signal_new ("toggle_add_mode",
736 			      G_TYPE_FROM_CLASS (object_class),
737 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
738 			      G_STRUCT_OFFSET (GtkCMCListClass, toggle_add_mode),
739 			      NULL, NULL,
740 			      claws_marshal_VOID__VOID,
741 		    	      G_TYPE_NONE, 0);
742   clist_signals[EXTEND_SELECTION] =
743  		g_signal_new ("extend_selection",
744 			      G_TYPE_FROM_CLASS (object_class),
745 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
746 			      G_STRUCT_OFFSET (GtkCMCListClass, extend_selection),
747 			      NULL, NULL,
748 			      claws_marshal_VOID__ENUM_FLOAT_BOOLEAN,
749 		    	      G_TYPE_NONE, 3, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT, G_TYPE_BOOLEAN);
750   clist_signals[SCROLL_VERTICAL] =
751  		g_signal_new ("scroll_vertical",
752 			      G_TYPE_FROM_CLASS (object_class),
753 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
754 			      G_STRUCT_OFFSET (GtkCMCListClass, scroll_vertical),
755 			      NULL, NULL,
756 			      claws_marshal_VOID__ENUM_FLOAT,
757 		    	      G_TYPE_NONE, 2, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT);
758   clist_signals[SCROLL_HORIZONTAL] =
759  		g_signal_new ("scroll_horizontal",
760 			      G_TYPE_FROM_CLASS (object_class),
761 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
762 			      G_STRUCT_OFFSET (GtkCMCListClass, scroll_horizontal),
763 			      NULL, NULL,
764 			      claws_marshal_VOID__ENUM_FLOAT,
765 		    	      G_TYPE_NONE, 2, GTK_TYPE_SCROLL_TYPE, G_TYPE_FLOAT);
766   clist_signals[ABORT_COLUMN_RESIZE] =
767  		g_signal_new ("abort_column_resize",
768 			      G_TYPE_FROM_CLASS (object_class),
769 			      G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
770 			      G_STRUCT_OFFSET (GtkCMCListClass, abort_column_resize),
771 			      NULL, NULL,
772 			      claws_marshal_VOID__VOID,
773 		    	      G_TYPE_NONE, 0);
774 
775   binding_set = gtk_binding_set_by_class (klass);
776   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, 0,
777 			        "scroll_vertical", 2,
778 				G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
779 				G_TYPE_FLOAT, 0.0);
780   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, 0,
781 				"scroll_vertical", 2,
782 				G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
783 				G_TYPE_FLOAT, 0.0);
784   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, 0,
785 				"scroll_vertical", 2,
786 				G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
787 				G_TYPE_FLOAT, 0.0);
788   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, 0,
789 				"scroll_vertical", 2,
790 				G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
791 				G_TYPE_FLOAT, 0.0);
792   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, 0,
793 				"scroll_vertical", 2,
794 				G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
795 				G_TYPE_FLOAT, 0.0);
796   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, 0,
797 				"scroll_vertical", 2,
798 				G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
799 				G_TYPE_FLOAT, 0.0);
800   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, 0,
801 				"scroll_vertical", 2,
802 				G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
803 				G_TYPE_FLOAT, 0.0);
804   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, 0,
805 				"scroll_vertical", 2,
806 				G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
807 				G_TYPE_FLOAT, 0.0);
808   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
809 				"scroll_vertical", 2,
810 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
811 				G_TYPE_FLOAT, 0.0);
812   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
813 				"scroll_vertical", 2,
814 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
815 				G_TYPE_FLOAT, 0.0);
816   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
817 				"scroll_vertical", 2,
818 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
819 				G_TYPE_FLOAT, 1.0);
820   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
821 				"scroll_vertical", 2,
822 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
823 				G_TYPE_FLOAT, 1.0);
824 
825   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Up, GDK_SHIFT_MASK,
826 				"extend_selection", 3,
827 				G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
828 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
829   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Up, GDK_SHIFT_MASK,
830 				"extend_selection", 3,
831 				G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
832 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
833   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Down, GDK_SHIFT_MASK,
834 				"extend_selection", 3,
835 				G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
836 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
837   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Down, GDK_SHIFT_MASK,
838 				"extend_selection", 3,
839 				G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
840 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
841   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Up, GDK_SHIFT_MASK,
842 				"extend_selection", 3,
843 				G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
844 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
845   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Up, GDK_SHIFT_MASK,
846 				"extend_selection", 3,
847 				G_TYPE_ENUM, GTK_SCROLL_PAGE_BACKWARD,
848 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
849   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Page_Down, GDK_SHIFT_MASK,
850 				"extend_selection", 3,
851 				G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
852 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
853   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Page_Down, GDK_SHIFT_MASK,
854 				"extend_selection", 3,
855 				G_TYPE_ENUM, GTK_SCROLL_PAGE_FORWARD,
856 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
857   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home,
858 				GDK_SHIFT_MASK | GDK_CONTROL_MASK,
859 				"extend_selection", 3,
860 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
861 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
862   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home,
863                                 GDK_SHIFT_MASK | GDK_CONTROL_MASK,
864 				"extend_selection", 3,
865 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
866 				G_TYPE_FLOAT, 0.0, G_TYPE_BOOLEAN, TRUE);
867   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End,
868 				GDK_SHIFT_MASK | GDK_CONTROL_MASK,
869 				"extend_selection", 3,
870 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
871 				G_TYPE_FLOAT, 1.0, G_TYPE_BOOLEAN, TRUE);
872   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End,
873 				GDK_SHIFT_MASK | GDK_CONTROL_MASK,
874 				"extend_selection", 3,
875 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
876 				G_TYPE_FLOAT, 1.0, G_TYPE_BOOLEAN, TRUE);
877 
878 
879   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Left, 0,
880 				"scroll_horizontal", 2,
881 				G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
882 				G_TYPE_FLOAT, 0.0);
883   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Left, 0,
884 				"scroll_horizontal", 2,
885 				G_TYPE_ENUM, GTK_SCROLL_STEP_BACKWARD,
886 				G_TYPE_FLOAT, 0.0);
887 
888   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Right, 0,
889 				"scroll_horizontal", 2,
890 				G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
891 				G_TYPE_FLOAT, 0.0);
892   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Right, 0,
893 				"scroll_horizontal", 2,
894 				G_TYPE_ENUM, GTK_SCROLL_STEP_FORWARD,
895 				G_TYPE_FLOAT, 0.0);
896 
897   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Home, 0,
898 				"scroll_horizontal", 2,
899 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
900 				G_TYPE_FLOAT, 0.0);
901   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Home, 0,
902 				"scroll_horizontal", 2,
903 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
904 				G_TYPE_FLOAT, 0.0);
905 
906   gtk_binding_entry_add_signal (binding_set, GDK_KEY_End, 0,
907 				"scroll_horizontal", 2,
908 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
909 				G_TYPE_FLOAT, 1.0);
910 
911   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_End, 0,
912 				"scroll_horizontal", 2,
913 				G_TYPE_ENUM, GTK_SCROLL_JUMP,
914 				G_TYPE_FLOAT, 1.0);
915 
916   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
917 				"undo_selection", 0);
918   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Escape, 0,
919 				"abort_column_resize", 0);
920   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
921 				"toggle_focus_row", 0);
922   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
923 				"toggle_focus_row", 0);
924   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
925 				"toggle_add_mode", 0);
926   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
927 				"toggle_add_mode", 0);
928   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
929 				"select_all", 0);
930   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Divide, GDK_CONTROL_MASK,
931 				"select_all", 0);
932   gtk_binding_entry_add_signal (binding_set, '\\', GDK_CONTROL_MASK,
933 				"unselect_all", 0);
934   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_L,
935 				GDK_RELEASE_MASK | GDK_SHIFT_MASK,
936 				"end_selection", 0);
937   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_R,
938 				GDK_RELEASE_MASK | GDK_SHIFT_MASK,
939 				"end_selection", 0);
940   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_L,
941 				GDK_RELEASE_MASK | GDK_SHIFT_MASK |
942 				GDK_CONTROL_MASK,
943 				"end_selection", 0);
944   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Shift_R,
945 				GDK_RELEASE_MASK | GDK_SHIFT_MASK |
946 				GDK_CONTROL_MASK,
947 				"end_selection", 0);
948 }
949 
950 static void
gtk_cmclist_set_arg(GObject * object,guint arg_id,const GValue * value,GParamSpec * spec)951 gtk_cmclist_set_arg (GObject *object,
952 				guint      arg_id,
953 				const GValue *value,
954 				GParamSpec *spec)
955 {
956   GtkCMCList *clist;
957 
958   clist = GTK_CMCLIST (object);
959 
960   switch (arg_id)
961     {
962     case ARG_N_COLUMNS: /* only set at construction time */
963       clist->columns = MAX (1, g_value_get_uint (value));
964       break;
965     case ARG_SHADOW_TYPE:
966       gtk_cmclist_set_shadow_type (clist, g_value_get_enum (value));
967       break;
968     case ARG_SELECTION_MODE:
969       gtk_cmclist_set_selection_mode (clist, g_value_get_enum (value));
970       break;
971     case ARG_ROW_HEIGHT:
972       gtk_cmclist_set_row_height (clist, g_value_get_uint (value));
973       break;
974     case ARG_REORDERABLE:
975       gtk_cmclist_set_reorderable (clist, g_value_get_boolean (value));
976       break;
977     case ARG_TITLES_ACTIVE:
978       if (g_value_get_boolean (value))
979 	gtk_cmclist_column_titles_active (clist);
980       else
981 	gtk_cmclist_column_titles_passive (clist);
982       break;
983     case ARG_USE_DRAG_ICONS:
984       gtk_cmclist_set_use_drag_icons (clist, g_value_get_boolean (value));
985       break;
986     case ARG_SORT_TYPE:
987       gtk_cmclist_set_sort_type (clist, g_value_get_enum (value));
988       break;
989     }
990 }
991 
992 static void
gtk_cmclist_get_arg(GObject * object,guint arg_id,GValue * value,GParamSpec * spec)993 gtk_cmclist_get_arg (GObject *object,
994 				guint      arg_id,
995 				GValue *value,
996 				GParamSpec *spec)
997 {
998   GtkCMCList *clist;
999 
1000   clist = GTK_CMCLIST (object);
1001 
1002   switch (arg_id)
1003     {
1004       guint i;
1005 
1006     case ARG_N_COLUMNS:
1007       g_value_set_uint(value, clist->columns);
1008       break;
1009     case ARG_SHADOW_TYPE:
1010       g_value_set_enum(value, clist->shadow_type);
1011       break;
1012     case ARG_SELECTION_MODE:
1013       g_value_set_enum(value, clist->selection_mode);
1014       break;
1015     case ARG_ROW_HEIGHT:
1016       g_value_set_uint(value, GTK_CMCLIST_ROW_HEIGHT_SET(clist) ? clist->row_height : 0);
1017       break;
1018     case ARG_REORDERABLE:
1019       g_value_set_boolean(value, GTK_CMCLIST_REORDERABLE (clist));
1020       break;
1021     case ARG_TITLES_ACTIVE:
1022       g_value_set_boolean(value, TRUE);
1023       for (i = 0; i < clist->columns; i++)
1024 	if (clist->column[i].button &&
1025 	    !gtk_widget_get_sensitive (clist->column[i].button))
1026 	  {
1027 	    g_value_set_boolean(value, FALSE);
1028 	    break;
1029 	  }
1030       break;
1031     case ARG_USE_DRAG_ICONS:
1032       g_value_set_boolean(value, GTK_CMCLIST_USE_DRAG_ICONS (clist));
1033       break;
1034     case ARG_SORT_TYPE:
1035       g_value_set_enum(value, clist->sort_type);
1036       break;
1037     default:
1038       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, arg_id, spec);
1039       break;
1040     }
1041 }
1042 
1043 static void
gtk_cmclist_init(GtkCMCList * clist)1044 gtk_cmclist_init (GtkCMCList *clist)
1045 {
1046   clist->flags = 0;
1047 
1048   gtk_widget_set_has_window (GTK_WIDGET(clist), TRUE);
1049   gtk_widget_set_can_focus (GTK_WIDGET(clist), TRUE);
1050   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_DRAW_DRAG_LINE);
1051   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_USE_DRAG_ICONS);
1052 
1053   clist->freeze_count = 0;
1054 
1055   clist->rows = 0;
1056   clist->row_height = 0;
1057   clist->row_list = NULL;
1058   clist->row_list_end = NULL;
1059 
1060   clist->columns = 0;
1061 
1062   clist->title_window = NULL;
1063   clist->column_title_area.x = 0;
1064   clist->column_title_area.y = 0;
1065   clist->column_title_area.width = 1;
1066   clist->column_title_area.height = 1;
1067 
1068   clist->clist_window = NULL;
1069   clist->clist_window_width = 1;
1070   clist->clist_window_height = 1;
1071 
1072   clist->hoffset = 0;
1073   clist->voffset = 0;
1074 
1075   clist->shadow_type = GTK_SHADOW_IN;
1076   clist->vadjustment = NULL;
1077   clist->hadjustment = NULL;
1078 
1079   clist->button_actions[0] = GTK_CMBUTTON_SELECTS | GTK_CMBUTTON_DRAGS;
1080   clist->button_actions[1] = GTK_CMBUTTON_IGNORED;
1081   clist->button_actions[2] = GTK_CMBUTTON_IGNORED;
1082   clist->button_actions[3] = GTK_CMBUTTON_IGNORED;
1083   clist->button_actions[4] = GTK_CMBUTTON_IGNORED;
1084 
1085   clist->cursor_drag = NULL;
1086   clist->x_drag = 0;
1087 
1088   clist->selection_mode = GTK_SELECTION_SINGLE;
1089   clist->selection = NULL;
1090   clist->selection_end = NULL;
1091   clist->undo_selection = NULL;
1092   clist->undo_unselection = NULL;
1093 
1094   clist->focus_row = -1;
1095   clist->focus_header_column = -1;
1096   clist->undo_anchor = -1;
1097 
1098   clist->anchor = -1;
1099   clist->anchor_state = GTK_STATE_SELECTED;
1100   clist->drag_pos = -1;
1101   clist->htimer = 0;
1102   clist->vtimer = 0;
1103 
1104   clist->click_cell.row = -1;
1105   clist->click_cell.column = -1;
1106 
1107   clist->compare = default_compare;
1108   clist->sort_type = GTK_SORT_ASCENDING;
1109   clist->sort_column = 0;
1110 
1111   clist->drag_highlight_row = -1;
1112 }
1113 
1114 /* Constructor */
1115 static GObject*
gtk_cmclist_constructor(GType type,guint n_construct_properties,GObjectConstructParam * construct_properties)1116 gtk_cmclist_constructor (GType                  type,
1117 		       guint                  n_construct_properties,
1118 		       GObjectConstructParam *construct_properties)
1119 {
1120   GObject *object = G_OBJECT_CLASS (gtk_cmclist_parent_class)->constructor (type,
1121 								n_construct_properties,
1122 								construct_properties);
1123   GtkCMCList *clist = GTK_CMCLIST (object);
1124 
1125   /* allocate memory for columns */
1126   clist->column = columns_new (clist);
1127 
1128   /* there needs to be at least one column button
1129    * because there is alot of code that will break if it
1130    * isn't there
1131    */
1132   column_button_create (clist, 0);
1133 
1134   return object;
1135 }
1136 
1137 /* GTKCLIST PUBLIC INTERFACE
1138  *   gtk_cmclist_new
1139  *   gtk_cmclist_new_with_titles
1140  *   gtk_cmclist_set_hadjustment
1141  *   gtk_cmclist_set_vadjustment
1142  *   gtk_cmclist_get_hadjustment
1143  *   gtk_cmclist_get_vadjustment
1144  *   gtk_cmclist_set_shadow_type
1145  *   gtk_cmclist_set_selection_mode
1146  *   gtk_cmclist_freeze
1147  *   gtk_cmclist_thaw
1148  */
1149 GtkWidget*
gtk_cmclist_new(gint columns)1150 gtk_cmclist_new (gint columns)
1151 {
1152   return gtk_cmclist_new_with_titles (columns, NULL);
1153 }
1154 
1155 GtkWidget*
gtk_cmclist_new_with_titles(gint columns,gchar * titles[])1156 gtk_cmclist_new_with_titles (gint   columns,
1157 			   gchar *titles[])
1158 {
1159   GtkCMCList *clist;
1160 
1161   clist = g_object_new (GTK_TYPE_CMCLIST,
1162 			"n_columns", columns,
1163 			NULL);
1164   if (titles)
1165     {
1166       guint i;
1167 
1168       for (i = 0; i < clist->columns; i++)
1169 	gtk_cmclist_set_column_title (clist, i, titles[i]);
1170       gtk_cmclist_column_titles_show (clist);
1171     }
1172   else
1173     gtk_cmclist_column_titles_hide (clist);
1174 
1175   return GTK_WIDGET (clist);
1176 }
1177 
1178 void
gtk_cmclist_set_hadjustment(GtkCMCList * clist,GtkAdjustment * adjustment)1179 gtk_cmclist_set_hadjustment (GtkCMCList      *clist,
1180 			   GtkAdjustment *adjustment)
1181 {
1182   GtkAdjustment *old_adjustment;
1183 
1184   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1185   if (adjustment)
1186     cm_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1187 
1188   if (clist->hadjustment == adjustment)
1189     return;
1190 
1191   old_adjustment = clist->hadjustment;
1192 
1193   if (clist->hadjustment)
1194     {
1195       g_signal_handlers_disconnect_matched(G_OBJECT (clist->hadjustment), G_SIGNAL_MATCH_DATA,
1196 		      	0, 0, 0, 0, clist);
1197 
1198       g_object_unref (G_OBJECT (clist->hadjustment));
1199     }
1200 
1201   clist->hadjustment = adjustment;
1202 
1203   if (clist->hadjustment)
1204     {
1205       g_object_ref_sink (clist->hadjustment);
1206       g_signal_connect (G_OBJECT (clist->hadjustment), "value_changed",
1207 			  G_CALLBACK( hadjustment_value_changed),
1208 			  (gpointer) clist);
1209     }
1210 
1211   if (!clist->hadjustment || !old_adjustment)
1212     gtk_widget_queue_resize (GTK_WIDGET (clist));
1213 }
1214 
1215 GtkAdjustment *
gtk_cmclist_get_hadjustment(GtkCMCList * clist)1216 gtk_cmclist_get_hadjustment (GtkCMCList *clist)
1217 {
1218   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1219 
1220   return clist->hadjustment;
1221 }
1222 
1223 void
gtk_cmclist_set_vadjustment(GtkCMCList * clist,GtkAdjustment * adjustment)1224 gtk_cmclist_set_vadjustment (GtkCMCList      *clist,
1225 			   GtkAdjustment *adjustment)
1226 {
1227   GtkAdjustment *old_adjustment;
1228 
1229   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1230   if (adjustment)
1231     cm_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
1232 
1233   if (clist->vadjustment == adjustment)
1234     return;
1235 
1236   old_adjustment = clist->vadjustment;
1237 
1238   if (clist->vadjustment)
1239     {
1240       g_signal_handlers_disconnect_matched(G_OBJECT (clist->vadjustment), G_SIGNAL_MATCH_DATA,
1241 		      	0, 0, 0, 0, clist);
1242       g_object_unref (G_OBJECT (clist->vadjustment));
1243     }
1244 
1245   clist->vadjustment = adjustment;
1246 
1247   if (clist->vadjustment)
1248     {
1249       g_object_ref_sink (clist->vadjustment);
1250 
1251       g_signal_connect (G_OBJECT (clist->vadjustment), "value_changed",
1252 			  G_CALLBACK(vadjustment_value_changed),
1253 			  (gpointer) clist);
1254     }
1255 
1256   if (!clist->vadjustment || !old_adjustment)
1257     gtk_widget_queue_resize (GTK_WIDGET (clist));
1258 }
1259 
1260 GtkAdjustment *
gtk_cmclist_get_vadjustment(GtkCMCList * clist)1261 gtk_cmclist_get_vadjustment (GtkCMCList *clist)
1262 {
1263   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1264 
1265   return clist->vadjustment;
1266 }
1267 
1268 static void
gtk_cmclist_set_scroll_adjustments(GtkCMCList * clist,GtkAdjustment * hadjustment,GtkAdjustment * vadjustment)1269 gtk_cmclist_set_scroll_adjustments (GtkCMCList      *clist,
1270 				  GtkAdjustment *hadjustment,
1271 				  GtkAdjustment *vadjustment)
1272 {
1273   if (clist->hadjustment != hadjustment)
1274     gtk_cmclist_set_hadjustment (clist, hadjustment);
1275   if (clist->vadjustment != vadjustment)
1276     gtk_cmclist_set_vadjustment (clist, vadjustment);
1277 }
1278 
1279 void
gtk_cmclist_set_shadow_type(GtkCMCList * clist,GtkShadowType type)1280 gtk_cmclist_set_shadow_type (GtkCMCList      *clist,
1281 			   GtkShadowType  type)
1282 {
1283   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1284 
1285   clist->shadow_type = type;
1286 
1287   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1288     gtk_widget_queue_resize (GTK_WIDGET (clist));
1289 }
1290 
1291 void
gtk_cmclist_set_selection_mode(GtkCMCList * clist,GtkSelectionMode mode)1292 gtk_cmclist_set_selection_mode (GtkCMCList         *clist,
1293 			      GtkSelectionMode  mode)
1294 {
1295   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1296   cm_return_if_fail (mode != GTK_SELECTION_NONE);
1297 
1298   if (mode == clist->selection_mode)
1299     return;
1300 
1301   clist->selection_mode = mode;
1302   clist->anchor = -1;
1303   clist->anchor_state = GTK_STATE_SELECTED;
1304   clist->drag_pos = -1;
1305   clist->undo_anchor = clist->focus_row;
1306 
1307   g_list_free (clist->undo_selection);
1308   g_list_free (clist->undo_unselection);
1309   clist->undo_selection = NULL;
1310   clist->undo_unselection = NULL;
1311 
1312   switch (mode)
1313     {
1314     case GTK_SELECTION_MULTIPLE:
1315       return;
1316     case GTK_SELECTION_BROWSE:
1317     case GTK_SELECTION_SINGLE:
1318       gtk_cmclist_unselect_all (clist);
1319       break;
1320     default:
1321       /* Someone set it by hand */
1322       g_assert_not_reached ();
1323     }
1324 }
1325 
1326 void
gtk_cmclist_freeze(GtkCMCList * clist)1327 gtk_cmclist_freeze (GtkCMCList *clist)
1328 {
1329   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1330 
1331   clist->freeze_count++;
1332 }
1333 
1334 void
gtk_cmclist_thaw(GtkCMCList * clist)1335 gtk_cmclist_thaw (GtkCMCList *clist)
1336 {
1337   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1338 
1339   if (clist->freeze_count)
1340     {
1341       clist->freeze_count--;
1342       CLIST_REFRESH (clist);
1343     }
1344 }
1345 
1346 /* PUBLIC COLUMN FUNCTIONS
1347  *   gtk_cmclist_column_titles_show
1348  *   gtk_cmclist_column_titles_hide
1349  *   gtk_cmclist_column_title_active
1350  *   gtk_cmclist_column_title_passive
1351  *   gtk_cmclist_column_titles_active
1352  *   gtk_cmclist_column_titles_passive
1353  *   gtk_cmclist_set_column_title
1354  *   gtk_cmclist_get_column_title
1355  *   gtk_cmclist_set_column_widget
1356  *   gtk_cmclist_set_column_justification
1357  *   gtk_cmclist_set_column_visibility
1358  *   gtk_cmclist_set_column_resizeable
1359  *   gtk_cmclist_set_column_auto_resize
1360  *   gtk_cmclist_optimal_column_width
1361  *   gtk_cmclist_set_column_width
1362  *   gtk_cmclist_set_column_min_width
1363  *   gtk_cmclist_set_column_max_width
1364  */
1365 void
gtk_cmclist_column_titles_show(GtkCMCList * clist)1366 gtk_cmclist_column_titles_show (GtkCMCList *clist)
1367 {
1368   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1369 
1370   if (!GTK_CMCLIST_SHOW_TITLES(clist))
1371     {
1372       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_SHOW_TITLES);
1373       if (clist->title_window)
1374 	gdk_window_show (clist->title_window);
1375       gtk_widget_queue_resize (GTK_WIDGET (clist));
1376     }
1377 }
1378 
1379 void
gtk_cmclist_column_titles_hide(GtkCMCList * clist)1380 gtk_cmclist_column_titles_hide (GtkCMCList *clist)
1381 {
1382   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1383 
1384   if (GTK_CMCLIST_SHOW_TITLES(clist))
1385     {
1386       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_SHOW_TITLES);
1387       if (clist->title_window)
1388 	gdk_window_hide (clist->title_window);
1389       gtk_widget_queue_resize (GTK_WIDGET (clist));
1390     }
1391 }
1392 
1393 void
gtk_cmclist_column_title_active(GtkCMCList * clist,gint column)1394 gtk_cmclist_column_title_active (GtkCMCList *clist,
1395 			       gint      column)
1396 {
1397   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1398 
1399   if (column < 0 || column >= clist->columns)
1400     return;
1401   if (!clist->column[column].button || !clist->column[column].button_passive)
1402     return;
1403 
1404   clist->column[column].button_passive = FALSE;
1405 
1406   g_signal_handlers_disconnect_matched(G_OBJECT (clist->column[column].button), G_SIGNAL_MATCH_FUNC,
1407 		    0, 0, 0, column_title_passive_func, 0);
1408 
1409   gtk_widget_set_can_focus (clist->column[column].button, TRUE);
1410   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1411     gtk_widget_queue_draw (clist->column[column].button);
1412 }
1413 
1414 void
gtk_cmclist_column_title_passive(GtkCMCList * clist,gint column)1415 gtk_cmclist_column_title_passive (GtkCMCList *clist,
1416 				gint      column)
1417 {
1418   GtkToggleButton *button;
1419 
1420   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1421 
1422   if (column < 0 || column >= clist->columns)
1423     return;
1424   if (!clist->column[column].button || clist->column[column].button_passive)
1425     return;
1426 
1427   button = GTK_TOGGLE_BUTTON (clist->column[column].button);
1428 
1429   clist->column[column].button_passive = TRUE;
1430 
1431   if (gtk_toggle_button_get_active(button))
1432 	g_signal_connect(G_OBJECT (clist->column[column].button),
1433 			 "button-release-event",
1434 			 G_CALLBACK(column_title_passive_func),
1435 			 NULL);
1436   if (gtk_widget_is_focus(gtk_bin_get_child(GTK_BIN(button))))
1437 	g_signal_connect(G_OBJECT (clist->column[column].button),
1438 			 "leave-notify-event",
1439 			 G_CALLBACK(column_title_passive_func),
1440 			 NULL);
1441 
1442   g_signal_connect (G_OBJECT (clist->column[column].button), "event",
1443 		      G_CALLBACK(column_title_passive_func), NULL);
1444 
1445   gtk_widget_set_can_focus (clist->column[column].button, FALSE);
1446   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1447     gtk_widget_queue_draw (clist->column[column].button);
1448 }
1449 
1450 void
gtk_cmclist_column_titles_active(GtkCMCList * clist)1451 gtk_cmclist_column_titles_active (GtkCMCList *clist)
1452 {
1453   gint i;
1454 
1455   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1456 
1457   for (i = 0; i < clist->columns; i++)
1458     gtk_cmclist_column_title_active (clist, i);
1459 }
1460 
1461 void
gtk_cmclist_column_titles_passive(GtkCMCList * clist)1462 gtk_cmclist_column_titles_passive (GtkCMCList *clist)
1463 {
1464   gint i;
1465 
1466   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1467 
1468   for (i = 0; i < clist->columns; i++)
1469     gtk_cmclist_column_title_passive (clist, i);
1470 }
1471 
1472 void
gtk_cmclist_set_column_title(GtkCMCList * clist,gint column,const gchar * title)1473 gtk_cmclist_set_column_title (GtkCMCList    *clist,
1474 			    gint         column,
1475 			    const gchar *title)
1476 {
1477   gint new_button = 0;
1478   GtkWidget *old_widget;
1479   GtkWidget *alignment = NULL;
1480   GtkWidget *label;
1481 
1482   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1483 
1484   if (column < 0 || column >= clist->columns)
1485     return;
1486 
1487   /* if the column button doesn't currently exist,
1488    * it has to be created first */
1489   if (!clist->column[column].button)
1490     {
1491       column_button_create (clist, column);
1492       new_button = 1;
1493     }
1494 
1495   column_title_new (clist, column, title);
1496 
1497   /* remove and destroy the old widget */
1498   old_widget = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1499   if (old_widget)
1500     gtk_container_remove (GTK_CONTAINER (clist->column[column].button), old_widget);
1501 
1502   /* create new alignment based no column justification */
1503   switch (clist->column[column].justification)
1504     {
1505     case GTK_JUSTIFY_LEFT:
1506       alignment = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
1507       break;
1508 
1509     case GTK_JUSTIFY_RIGHT:
1510       alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
1511       break;
1512 
1513     case GTK_JUSTIFY_CENTER:
1514       alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1515       break;
1516 
1517     case GTK_JUSTIFY_FILL:
1518       alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
1519       break;
1520     }
1521 
1522   gtk_widget_push_composite_child ();
1523   label = gtk_label_new (clist->column[column].title);
1524   gtk_widget_pop_composite_child ();
1525   gtk_container_add (GTK_CONTAINER (alignment), label);
1526   gtk_container_add (GTK_CONTAINER (clist->column[column].button), alignment);
1527   gtk_widget_show (label);
1528   gtk_widget_show (alignment);
1529 
1530   /* if this button didn't previously exist, then the
1531    * column button positions have to be re-computed */
1532   if (gtk_widget_get_visible (GTK_WIDGET(clist)) && new_button)
1533     size_allocate_title_buttons (clist);
1534 }
1535 
1536 gchar *
gtk_cmclist_get_column_title(GtkCMCList * clist,gint column)1537 gtk_cmclist_get_column_title (GtkCMCList *clist,
1538 			    gint      column)
1539 {
1540   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1541 
1542   if (column < 0 || column >= clist->columns)
1543     return NULL;
1544 
1545   return clist->column[column].title;
1546 }
1547 
1548 void
gtk_cmclist_set_column_widget(GtkCMCList * clist,gint column,GtkWidget * widget)1549 gtk_cmclist_set_column_widget (GtkCMCList  *clist,
1550 			     gint       column,
1551 			     GtkWidget *widget)
1552 {
1553   gint new_button = 0;
1554   GtkWidget *old_widget;
1555 
1556   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1557 
1558   if (column < 0 || column >= clist->columns)
1559     return;
1560 
1561   /* if the column button doesn't currently exist,
1562    * it has to be created first */
1563   if (!clist->column[column].button)
1564     {
1565       column_button_create (clist, column);
1566       new_button = 1;
1567     }
1568 
1569   column_title_new (clist, column, NULL);
1570 
1571   /* remove and destroy the old widget */
1572   old_widget = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1573   if (old_widget)
1574     gtk_container_remove (GTK_CONTAINER (clist->column[column].button),
1575 			  old_widget);
1576 
1577   /* add and show the widget */
1578   if (widget)
1579     {
1580       gtk_container_add (GTK_CONTAINER (clist->column[column].button), widget);
1581       gtk_widget_show (widget);
1582     }
1583 
1584   /* if this button didn't previously exist, then the
1585    * column button positions have to be re-computed */
1586   if (gtk_widget_get_visible (GTK_WIDGET(clist)) && new_button)
1587     size_allocate_title_buttons (clist);
1588 }
1589 
1590 GtkWidget *
gtk_cmclist_get_column_widget(GtkCMCList * clist,gint column)1591 gtk_cmclist_get_column_widget (GtkCMCList *clist,
1592 			     gint      column)
1593 {
1594   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
1595 
1596   if (column < 0 || column >= clist->columns)
1597     return NULL;
1598 
1599   if (clist->column[column].button)
1600 	return gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1601 
1602   return NULL;
1603 }
1604 
1605 void
gtk_cmclist_set_column_justification(GtkCMCList * clist,gint column,GtkJustification justification)1606 gtk_cmclist_set_column_justification (GtkCMCList         *clist,
1607 				    gint              column,
1608 				    GtkJustification  justification)
1609 {
1610   GtkWidget *alignment;
1611 
1612   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1613 
1614   if (column < 0 || column >= clist->columns)
1615     return;
1616 
1617   clist->column[column].justification = justification;
1618 
1619   /* change the alinment of the button title if it's not a
1620    * custom widget */
1621   if (clist->column[column].title)
1622     {
1623       alignment = gtk_bin_get_child (GTK_BIN (clist->column[column].button));
1624 
1625       switch (clist->column[column].justification)
1626 	{
1627 	case GTK_JUSTIFY_LEFT:
1628 	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.0, 0.5, 0.0, 0.0);
1629 	  break;
1630 
1631 	case GTK_JUSTIFY_RIGHT:
1632 	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 1.0, 0.5, 0.0, 0.0);
1633 	  break;
1634 
1635 	case GTK_JUSTIFY_CENTER:
1636 	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1637 	  break;
1638 
1639 	case GTK_JUSTIFY_FILL:
1640 	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
1641 	  break;
1642 
1643 	default:
1644 	  break;
1645 	}
1646     }
1647 
1648   if (CLIST_UNFROZEN (clist))
1649     draw_rows (clist, NULL);
1650 }
1651 
1652 void
gtk_cmclist_set_column_visibility(GtkCMCList * clist,gint column,gboolean visible)1653 gtk_cmclist_set_column_visibility (GtkCMCList *clist,
1654 				 gint      column,
1655 				 gboolean  visible)
1656 {
1657   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1658 
1659   if (column < 0 || column >= clist->columns)
1660     return;
1661   if (clist->column[column].visible == visible)
1662     return;
1663 
1664   /* don't hide last visible column */
1665   if (!visible)
1666     {
1667       gint i;
1668       gint vis_columns = 0;
1669 
1670       for (i = 0, vis_columns = 0; i < clist->columns && vis_columns < 2; i++)
1671 	if (clist->column[i].visible)
1672 	  vis_columns++;
1673 
1674       if (vis_columns < 2)
1675 	return;
1676     }
1677 
1678   clist->column[column].visible = visible;
1679 
1680   if (clist->column[column].button)
1681     {
1682       if (visible)
1683 	gtk_widget_show (clist->column[column].button);
1684       else
1685 	gtk_widget_hide (clist->column[column].button);
1686     }
1687 
1688   gtk_widget_queue_resize (GTK_WIDGET(clist));
1689 }
1690 
1691 void
gtk_cmclist_set_column_resizeable(GtkCMCList * clist,gint column,gboolean resizeable)1692 gtk_cmclist_set_column_resizeable (GtkCMCList *clist,
1693 				 gint      column,
1694 				 gboolean  resizeable)
1695 {
1696   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1697 
1698   if (column < 0 || column >= clist->columns)
1699     return;
1700   if (clist->column[column].resizeable == resizeable)
1701     return;
1702 
1703   clist->column[column].resizeable = resizeable;
1704   if (resizeable)
1705     clist->column[column].auto_resize = FALSE;
1706 
1707   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1708     size_allocate_title_buttons (clist);
1709 }
1710 
1711 void
gtk_cmclist_set_column_auto_resize(GtkCMCList * clist,gint column,gboolean auto_resize)1712 gtk_cmclist_set_column_auto_resize (GtkCMCList *clist,
1713 				  gint      column,
1714 				  gboolean  auto_resize)
1715 {
1716   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1717 
1718   if (column < 0 || column >= clist->columns)
1719     return;
1720   if (clist->column[column].auto_resize == auto_resize)
1721     return;
1722 
1723   clist->column[column].auto_resize = auto_resize;
1724   if (auto_resize)
1725     {
1726       clist->column[column].resizeable = FALSE;
1727       if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1728 	{
1729 	  gint width;
1730 
1731 	  width = gtk_cmclist_optimal_column_width (clist, column);
1732 	  gtk_cmclist_set_column_width (clist, column, width);
1733 	}
1734     }
1735 
1736   if (gtk_widget_get_visible (GTK_WIDGET(clist)))
1737     size_allocate_title_buttons (clist);
1738 }
1739 
1740 gint
gtk_cmclist_columns_autosize(GtkCMCList * clist)1741 gtk_cmclist_columns_autosize (GtkCMCList *clist)
1742 {
1743   gint i;
1744   gint width;
1745 
1746   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
1747 
1748   gtk_cmclist_freeze (clist);
1749   width = 0;
1750   for (i = 0; i < clist->columns; i++)
1751     {
1752       gtk_cmclist_set_column_width (clist, i,
1753 				  gtk_cmclist_optimal_column_width (clist, i));
1754 
1755       width += clist->column[i].width;
1756     }
1757 
1758   gtk_cmclist_thaw (clist);
1759   return width;
1760 }
1761 
1762 gint
gtk_cmclist_optimal_column_width(GtkCMCList * clist,gint column)1763 gtk_cmclist_optimal_column_width (GtkCMCList *clist,
1764 				gint      column)
1765 {
1766   GtkRequisition requisition;
1767   GList *list;
1768   gint width;
1769 
1770   cm_return_val_if_fail (GTK_CMCLIST (clist), 0);
1771 
1772   if (column < 0 || column >= clist->columns)
1773     return 0;
1774 
1775   if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
1776     {
1777       gtk_widget_get_requisition (clist->column[column].button, &requisition);
1778       width = requisition.width
1779 #if 0
1780 	     (CELL_SPACING + (2 * COLUMN_INSET)))
1781 #endif
1782 		;
1783     }
1784   else
1785     width = 0;
1786 
1787   for (list = clist->row_list; list; list = list->next)
1788     {
1789   GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1790 	(clist, GTK_CMCLIST_ROW (list), column, &requisition);
1791       width = MAX (width, requisition.width);
1792     }
1793 
1794   return width;
1795 }
1796 
1797 void
gtk_cmclist_set_column_width(GtkCMCList * clist,gint column,gint width)1798 gtk_cmclist_set_column_width (GtkCMCList *clist,
1799 			    gint      column,
1800 			    gint      width)
1801 {
1802   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1803 
1804   if (column < 0 || column >= clist->columns)
1805     return;
1806 
1807   g_signal_emit (G_OBJECT (clist), clist_signals[RESIZE_COLUMN], 0,
1808 		   column, width);
1809 }
1810 
1811 void
gtk_cmclist_set_column_min_width(GtkCMCList * clist,gint column,gint min_width)1812 gtk_cmclist_set_column_min_width (GtkCMCList *clist,
1813 				gint      column,
1814 				gint      min_width)
1815 {
1816   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1817 
1818   if (column < 0 || column >= clist->columns)
1819     return;
1820   if (clist->column[column].min_width == min_width)
1821     return;
1822 
1823   if (clist->column[column].max_width >= 0  &&
1824       clist->column[column].max_width < min_width)
1825     clist->column[column].min_width = clist->column[column].max_width;
1826   else
1827     clist->column[column].min_width = min_width;
1828 
1829   if (clist->column[column].area.width < clist->column[column].min_width)
1830     gtk_cmclist_set_column_width (clist, column,clist->column[column].min_width);
1831 }
1832 
1833 void
gtk_cmclist_set_column_max_width(GtkCMCList * clist,gint column,gint max_width)1834 gtk_cmclist_set_column_max_width (GtkCMCList *clist,
1835 				gint      column,
1836 				gint      max_width)
1837 {
1838   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1839 
1840   if (column < 0 || column >= clist->columns)
1841     return;
1842   if (clist->column[column].max_width == max_width)
1843     return;
1844 
1845   if (clist->column[column].min_width >= 0 && max_width >= 0 &&
1846       clist->column[column].min_width > max_width)
1847     clist->column[column].max_width = clist->column[column].min_width;
1848   else
1849     clist->column[column].max_width = max_width;
1850 
1851   if (clist->column[column].area.width > clist->column[column].max_width)
1852     gtk_cmclist_set_column_width (clist, column,clist->column[column].max_width);
1853 }
1854 
1855 /* PRIVATE COLUMN FUNCTIONS
1856  *   column_auto_resize
1857  *   real_resize_column
1858  *   abort_column_resize
1859  *   size_allocate_title_buttons
1860  *   size_allocate_columns
1861  *   list_requisition_width
1862  *   new_column_width
1863  *   column_button_create
1864  *   column_button_clicked
1865  *   column_title_passive_func
1866  */
1867 static void
column_auto_resize(GtkCMCList * clist,GtkCMCListRow * clist_row,gint column,gint old_width)1868 column_auto_resize (GtkCMCList    *clist,
1869 		    GtkCMCListRow *clist_row,
1870 		    gint         column,
1871 		    gint         old_width)
1872 {
1873   /* resize column if needed for auto_resize */
1874   GtkRequisition requisition;
1875 
1876   if (!clist->column[column].auto_resize ||
1877       GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
1878     return;
1879 
1880   if (clist_row)
1881     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
1882 						   column, &requisition);
1883   else
1884     requisition.width = 0;
1885 
1886   if (requisition.width > clist->column[column].width)
1887     gtk_cmclist_set_column_width (clist, column, requisition.width);
1888   else if (requisition.width < old_width &&
1889 	   old_width == clist->column[column].width)
1890     {
1891       GList *list;
1892       gint new_width = 0;
1893 
1894       /* run a "gtk_cmclist_optimal_column_width" but break, if
1895        * the column doesn't shrink */
1896       if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[column].button)
1897     {
1898 	gtk_widget_get_requisition (clist->column[column].button, &requisition);
1899 	new_width = (requisition.width -
1900 		     (CELL_SPACING + (2 * COLUMN_INSET)));
1901     }
1902       else
1903 	new_width = 0;
1904 
1905       for (list = clist->row_list; list; list = list->next)
1906 	{
1907 	  GTK_CMCLIST_GET_CLASS (clist)->cell_size_request
1908 	    (clist, GTK_CMCLIST_ROW (list), column, &requisition);
1909 	  new_width = MAX (new_width, requisition.width);
1910 	  if (new_width == clist->column[column].width)
1911 	    break;
1912 	}
1913       if (new_width < clist->column[column].width)
1914 	gtk_cmclist_set_column_width
1915 	  (clist, column, MAX (new_width, clist->column[column].min_width));
1916     }
1917 }
1918 
1919 static void
real_resize_column(GtkCMCList * clist,gint column,gint width)1920 real_resize_column (GtkCMCList *clist,
1921 		    gint      column,
1922 		    gint      width)
1923 {
1924   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1925 
1926   if (column < 0 || column >= clist->columns)
1927     return;
1928 
1929   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
1930     width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
1931   if (clist->column[column].max_width >= 0 &&
1932       width > clist->column[column].max_width)
1933     width = clist->column[column].max_width;
1934 
1935   clist->column[column].width = width;
1936   clist->column[column].width_set = TRUE;
1937 
1938   /* FIXME: this is quite expensive to do if the widget hasn't
1939    *        been size_allocated yet, and pointless. Should
1940    *        a flag be kept
1941    */
1942   size_allocate_columns (clist, TRUE);
1943   size_allocate_title_buttons (clist);
1944 
1945   CLIST_REFRESH (clist);
1946 }
1947 
1948 static void
abort_column_resize(GtkCMCList * clist)1949 abort_column_resize (GtkCMCList *clist)
1950 {
1951   cm_return_if_fail (GTK_IS_CMCLIST (clist));
1952 
1953   if (!GTK_CMCLIST_IN_DRAG(clist))
1954     return;
1955 
1956   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_IN_DRAG);
1957   gtk_grab_remove (GTK_WIDGET (clist));
1958   gdk_display_pointer_ungrab (gtk_widget_get_display (GTK_WIDGET (clist)),
1959 			      GDK_CURRENT_TIME);
1960   clist->drag_pos = -1;
1961 
1962   if (clist->x_drag >= 0 && clist->x_drag <= clist->clist_window_width - 1)
1963     clist_refresh(clist);
1964 }
1965 
1966 static void
size_allocate_title_buttons(GtkCMCList * clist)1967 size_allocate_title_buttons (GtkCMCList *clist)
1968 {
1969   GtkAllocation button_allocation;
1970   gint last_column;
1971   gint last_button = 0;
1972   gint i;
1973 
1974   if (!gtk_widget_get_realized (GTK_WIDGET(clist)))
1975     return;
1976 
1977   button_allocation.x = clist->hoffset;
1978   button_allocation.y = 0;
1979   button_allocation.width = 0;
1980   button_allocation.height = clist->column_title_area.height;
1981 
1982   /* find last visible column */
1983   for (last_column = clist->columns - 1; last_column >= 0; last_column--)
1984     if (clist->column[last_column].visible)
1985       break;
1986 
1987   for (i = 0; i < last_column; i++)
1988     {
1989       if (!clist->column[i].visible)
1990 	{
1991 	  last_button = i + 1;
1992 	  gdk_window_hide (clist->column[i].window);
1993 	  continue;
1994 	}
1995 
1996       button_allocation.width += (clist->column[i].area.width +
1997 				  CELL_SPACING + 2 * COLUMN_INSET);
1998 
1999       if (!clist->column[i + 1].button)
2000 	{
2001 	  gdk_window_hide (clist->column[i].window);
2002 	  continue;
2003 	}
2004 
2005       gtk_widget_size_allocate (clist->column[last_button].button,
2006 				&button_allocation);
2007       button_allocation.x += button_allocation.width;
2008       button_allocation.width = 0;
2009 
2010       if (clist->column[last_button].resizeable)
2011 	{
2012 	  gdk_window_show (clist->column[last_button].window);
2013 	  gdk_window_move_resize (clist->column[last_button].window,
2014 				  button_allocation.x - (DRAG_WIDTH / 2),
2015 				  0, DRAG_WIDTH,
2016 				  clist->column_title_area.height);
2017 	}
2018       else
2019 	gdk_window_hide (clist->column[last_button].window);
2020 
2021       last_button = i + 1;
2022     }
2023 
2024   button_allocation.width += (clist->column[last_column].area.width +
2025 			      2 * (CELL_SPACING + COLUMN_INSET));
2026   gtk_widget_size_allocate (clist->column[last_button].button,
2027 			    &button_allocation);
2028 
2029   if (clist->column[last_button].resizeable)
2030     {
2031       button_allocation.x += button_allocation.width;
2032 
2033       gdk_window_show (clist->column[last_button].window);
2034       gdk_window_move_resize (clist->column[last_button].window,
2035 			      button_allocation.x - (DRAG_WIDTH / 2),
2036 			      0, DRAG_WIDTH, clist->column_title_area.height);
2037     }
2038   else
2039     gdk_window_hide (clist->column[last_button].window);
2040 }
2041 
2042 static void
size_allocate_columns(GtkCMCList * clist,gboolean block_resize)2043 size_allocate_columns (GtkCMCList *clist,
2044 		       gboolean  block_resize)
2045 {
2046   GtkRequisition requisition;
2047   gint xoffset = CELL_SPACING + COLUMN_INSET;
2048   gint last_column;
2049   gint i;
2050 
2051   /* find last visible column and calculate correct column width */
2052   for (last_column = clist->columns - 1;
2053        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2054 
2055   if (last_column < 0)
2056     return;
2057 
2058   for (i = 0; i <= last_column; i++)
2059     {
2060       if (!clist->column[i].visible)
2061 	continue;
2062       clist->column[i].area.x = xoffset;
2063       if (clist->column[i].width_set)
2064 	{
2065 	  if (!block_resize && GTK_CMCLIST_SHOW_TITLES(clist) &&
2066 	      clist->column[i].auto_resize && clist->column[i].button)
2067 	    {
2068 	      gint width;
2069 
2070 	      gtk_widget_get_requisition (clist->column[i].button, &requisition);
2071 	      width = (requisition.width -
2072 		       (CELL_SPACING + (2 * COLUMN_INSET)));
2073 
2074 	      if (width > clist->column[i].width)
2075 		gtk_cmclist_set_column_width (clist, i, width);
2076 	    }
2077 
2078 	  clist->column[i].area.width = clist->column[i].width;
2079 	  xoffset += clist->column[i].width + CELL_SPACING + (2* COLUMN_INSET);
2080 	}
2081       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2082 	{
2083 	  gtk_widget_get_requisition (clist->column[i].button, &requisition);
2084 	  clist->column[i].area.width =
2085 	    requisition.width -
2086 	    (CELL_SPACING + (2 * COLUMN_INSET));
2087 	  xoffset += requisition.width;
2088 	}
2089     }
2090 
2091   clist->column[last_column].area.width = clist->column[last_column].area.width
2092     + MAX (0, clist->clist_window_width + COLUMN_INSET - xoffset);
2093 }
2094 
2095 static gint
list_requisition_width(GtkCMCList * clist)2096 list_requisition_width (GtkCMCList *clist)
2097 {
2098   GtkRequisition requisition;
2099   gint width = CELL_SPACING;
2100   gint i;
2101 
2102   for (i = clist->columns - 1; i >= 0; i--)
2103     {
2104       if (!clist->column[i].visible)
2105 	continue;
2106 
2107       if (clist->column[i].width_set)
2108 	width += clist->column[i].width + CELL_SPACING + (2 * COLUMN_INSET);
2109       else if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2110     {
2111 	gtk_widget_get_requisition (clist->column[i].button, &requisition);
2112 	width += requisition.width;
2113     }
2114     }
2115 
2116   return width;
2117 }
2118 
2119 /* this function returns the new width of the column being resized given
2120  * the column and x position of the cursor; the x cursor position is passed
2121  * in as a pointer and automagicly corrected if it's beyond min/max limits */
2122 static gint
new_column_width(GtkCMCList * clist,gint column,gint * x)2123 new_column_width (GtkCMCList *clist,
2124 		  gint      column,
2125 		  gint     *x)
2126 {
2127   gint xthickness = gtk_widget_get_style (GTK_WIDGET (clist))->xthickness;
2128   gint width;
2129   gint cx;
2130   gint dx;
2131   gint last_column;
2132 
2133   /* first translate the x position from widget->window
2134    * to clist->clist_window */
2135   cx = *x - xthickness;
2136 
2137   for (last_column = clist->columns - 1;
2138        last_column >= 0 && !clist->column[last_column].visible; last_column--);
2139 
2140   /* calculate new column width making sure it doesn't end up
2141    * less than the minimum width */
2142   dx = (COLUMN_LEFT_XPIXEL (clist, column) + COLUMN_INSET +
2143 	(column < last_column) * CELL_SPACING);
2144   width = cx - dx;
2145 
2146   if (width < MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width))
2147     {
2148       width = MAX (COLUMN_MIN_WIDTH, clist->column[column].min_width);
2149       cx = dx + width;
2150       *x = cx + xthickness;
2151     }
2152   else if (clist->column[column].max_width >= COLUMN_MIN_WIDTH &&
2153 	   width > clist->column[column].max_width)
2154     {
2155       width = clist->column[column].max_width;
2156       cx = dx + clist->column[column].max_width;
2157       *x = cx + xthickness;
2158     }
2159 
2160   if (cx < 0 || cx > clist->clist_window_width)
2161     *x = -1;
2162 
2163   return width;
2164 }
2165 
2166 static void
column_button_create(GtkCMCList * clist,gint column)2167 column_button_create (GtkCMCList *clist,
2168 		      gint      column)
2169 {
2170   GtkWidget *button;
2171 
2172   gtk_widget_push_composite_child ();
2173   button = clist->column[column].button = gtk_button_new ();
2174   GtkRcStyle *style = gtk_rc_style_new();
2175   style->ythickness = 0;
2176   gtk_widget_modify_style(clist->column[column].button, style);
2177   g_object_unref(style);
2178   gtk_container_set_border_width(GTK_CONTAINER(button), 0);
2179   gtk_widget_pop_composite_child ();
2180 
2181   if (gtk_widget_get_realized (GTK_WIDGET(clist)) && clist->title_window)
2182     gtk_widget_set_parent_window (clist->column[column].button,
2183 				  clist->title_window);
2184   gtk_widget_set_parent (button, GTK_WIDGET (clist));
2185 
2186   g_signal_connect (G_OBJECT (button), "clicked",
2187 		      G_CALLBACK(column_button_clicked),
2188 		      (gpointer) clist);
2189   gtk_widget_show (button);
2190 }
2191 
2192 static void
column_button_clicked(GtkWidget * widget,gpointer data)2193 column_button_clicked (GtkWidget *widget,
2194 		       gpointer   data)
2195 {
2196   gint i;
2197   GtkCMCList *clist;
2198 
2199   cm_return_if_fail (widget != NULL);
2200   cm_return_if_fail (GTK_IS_CMCLIST (data));
2201 
2202   clist = GTK_CMCLIST (data);
2203 
2204   /* find the column who's button was pressed */
2205   for (i = 0; i < clist->columns; i++)
2206     if (clist->column[i].button == widget)
2207       break;
2208 
2209   g_signal_emit (G_OBJECT (clist), clist_signals[CLICK_COLUMN], 0, i);
2210 }
2211 
2212 static gint
column_title_passive_func(GtkWidget * widget,GdkEvent * event,gpointer data)2213 column_title_passive_func (GtkWidget *widget,
2214 			   GdkEvent  *event,
2215 			   gpointer   data)
2216 {
2217   cm_return_val_if_fail (event != NULL, FALSE);
2218 
2219   switch (event->type)
2220     {
2221     case GDK_MOTION_NOTIFY:
2222     case GDK_BUTTON_PRESS:
2223     case GDK_2BUTTON_PRESS:
2224     case GDK_3BUTTON_PRESS:
2225     case GDK_BUTTON_RELEASE:
2226     case GDK_ENTER_NOTIFY:
2227     case GDK_LEAVE_NOTIFY:
2228       return TRUE;
2229     default:
2230       break;
2231     }
2232   return FALSE;
2233 }
2234 
2235 
2236 /* PUBLIC CELL FUNCTIONS
2237  *   gtk_cmclist_get_cell_type
2238  *   gtk_cmclist_set_text
2239  *   gtk_cmclist_get_text
2240  *   gtk_cmclist_set_pixbuf
2241  *   gtk_cmclist_get_pixbuf
2242  *   gtk_cmclist_set_pixtext
2243  *   gtk_cmclist_get_pixtext
2244  *   gtk_cmclist_set_shift
2245  */
2246 GtkCMCellType
gtk_cmclist_get_cell_type(GtkCMCList * clist,gint row,gint column)2247 gtk_cmclist_get_cell_type (GtkCMCList *clist,
2248 			 gint      row,
2249 			 gint      column)
2250 {
2251   GtkCMCListRow *clist_row;
2252 
2253   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2254 
2255   if (row < 0 || row >= clist->rows)
2256     return -1;
2257   if (column < 0 || column >= clist->columns)
2258     return -1;
2259 
2260   clist_row = ROW_ELEMENT (clist, row)->data;
2261 
2262   return clist_row->cell[column].type;
2263 }
2264 
2265 void
gtk_cmclist_set_text(GtkCMCList * clist,gint row,gint column,const gchar * text)2266 gtk_cmclist_set_text (GtkCMCList    *clist,
2267 		    gint         row,
2268 		    gint         column,
2269 		    const gchar *text)
2270 {
2271   GtkCMCListRow *clist_row;
2272 
2273   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2274 
2275   if (row < 0 || row >= clist->rows)
2276     return;
2277   if (column < 0 || column >= clist->columns)
2278     return;
2279 
2280   clist_row = ROW_ELEMENT (clist, row)->data;
2281 
2282   /* if text is null, then the cell is empty */
2283   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2284     (clist, clist_row, column, GTK_CMCELL_TEXT, text, 0, NULL);
2285 
2286   /* redraw the list if it's not frozen */
2287   if (CLIST_UNFROZEN (clist))
2288     {
2289       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2290 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2291     }
2292 }
2293 
2294 gint
gtk_cmclist_get_text(GtkCMCList * clist,gint row,gint column,gchar ** text)2295 gtk_cmclist_get_text (GtkCMCList  *clist,
2296 		    gint       row,
2297 		    gint       column,
2298 		    gchar    **text)
2299 {
2300   GtkCMCListRow *clist_row;
2301 
2302   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2303 
2304   if (row < 0 || row >= clist->rows)
2305     return 0;
2306   if (column < 0 || column >= clist->columns)
2307     return 0;
2308 
2309   clist_row = ROW_ELEMENT (clist, row)->data;
2310 
2311   if (clist_row->cell[column].type != GTK_CMCELL_TEXT)
2312     return 0;
2313 
2314   if (text)
2315     *text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2316 
2317   return 1;
2318 }
2319 
2320 void
gtk_cmclist_set_pixbuf(GtkCMCList * clist,gint row,gint column,GdkPixbuf * pixbuf)2321 gtk_cmclist_set_pixbuf (GtkCMCList  *clist,
2322 		      gint       row,
2323 		      gint       column,
2324 		      GdkPixbuf *pixbuf)
2325 {
2326   GtkCMCListRow *clist_row;
2327 
2328   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2329 
2330   if (row < 0 || row >= clist->rows)
2331     return;
2332   if (column < 0 || column >= clist->columns)
2333     return;
2334 
2335   clist_row = ROW_ELEMENT (clist, row)->data;
2336 
2337   g_object_ref (pixbuf);
2338 
2339   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2340     (clist, clist_row, column, GTK_CMCELL_PIXBUF, NULL, 0, pixbuf);
2341 
2342   /* redraw the list if it's not frozen */
2343   if (CLIST_UNFROZEN (clist))
2344     {
2345       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2346 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2347     }
2348 }
2349 
2350 gint
gtk_cmclist_get_pixbuf(GtkCMCList * clist,gint row,gint column,GdkPixbuf ** pixbuf)2351 gtk_cmclist_get_pixbuf (GtkCMCList   *clist,
2352 		      gint        row,
2353 		      gint        column,
2354 		      GdkPixbuf **pixbuf)
2355 {
2356   GtkCMCListRow *clist_row;
2357 
2358   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2359 
2360   if (row < 0 || row >= clist->rows)
2361     return 0;
2362   if (column < 0 || column >= clist->columns)
2363     return 0;
2364 
2365   clist_row = ROW_ELEMENT (clist, row)->data;
2366 
2367   if (clist_row->cell[column].type != GTK_CMCELL_PIXBUF)
2368     return 0;
2369 
2370   if (pixbuf)
2371   {
2372     *pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2373   }
2374 
2375   return 1;
2376 }
2377 
2378 void
gtk_cmclist_set_pixtext(GtkCMCList * clist,gint row,gint column,const gchar * text,guint8 spacing,GdkPixbuf * pixbuf)2379 gtk_cmclist_set_pixtext (GtkCMCList    *clist,
2380 		       gint         row,
2381 		       gint         column,
2382 		       const gchar *text,
2383 		       guint8       spacing,
2384 		       GdkPixbuf   *pixbuf)
2385 {
2386   GtkCMCListRow *clist_row;
2387 
2388   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2389 
2390   if (row < 0 || row >= clist->rows)
2391     return;
2392   if (column < 0 || column >= clist->columns)
2393     return;
2394 
2395   clist_row = ROW_ELEMENT (clist, row)->data;
2396 
2397   g_object_ref (pixbuf);
2398   GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2399     (clist, clist_row, column, GTK_CMCELL_PIXTEXT, text, spacing, pixbuf);
2400 
2401   /* redraw the list if it's not frozen */
2402   if (CLIST_UNFROZEN (clist))
2403     {
2404       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2405 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2406     }
2407 }
2408 
2409 gint
gtk_cmclist_get_pixtext(GtkCMCList * clist,gint row,gint column,gchar ** text,guint8 * spacing,GdkPixbuf ** pixbuf)2410 gtk_cmclist_get_pixtext (GtkCMCList   *clist,
2411 		       gint        row,
2412 		       gint        column,
2413 		       gchar     **text,
2414 		       guint8     *spacing,
2415 		       GdkPixbuf **pixbuf)
2416 {
2417   GtkCMCListRow *clist_row;
2418 
2419   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
2420 
2421   if (row < 0 || row >= clist->rows)
2422     return 0;
2423   if (column < 0 || column >= clist->columns)
2424     return 0;
2425 
2426   clist_row = ROW_ELEMENT (clist, row)->data;
2427 
2428   if (clist_row->cell[column].type != GTK_CMCELL_PIXTEXT)
2429     return 0;
2430 
2431   if (text)
2432     *text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2433   if (spacing)
2434     *spacing = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2435   if (pixbuf)
2436     *pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2437 
2438   return 1;
2439 }
2440 
2441 void
gtk_cmclist_set_shift(GtkCMCList * clist,gint row,gint column,gint vertical,gint horizontal)2442 gtk_cmclist_set_shift (GtkCMCList *clist,
2443 		     gint      row,
2444 		     gint      column,
2445 		     gint      vertical,
2446 		     gint      horizontal)
2447 {
2448   GtkRequisition requisition = { 0 };
2449   GtkCMCListRow *clist_row;
2450 
2451   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2452 
2453   if (row < 0 || row >= clist->rows)
2454     return;
2455   if (column < 0 || column >= clist->columns)
2456     return;
2457 
2458   clist_row = ROW_ELEMENT (clist, row)->data;
2459 
2460   if (clist->column[column].auto_resize &&
2461       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2462     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2463 						   column, &requisition);
2464 
2465   clist_row->cell[column].vertical = vertical;
2466   clist_row->cell[column].horizontal = horizontal;
2467 
2468   column_auto_resize (clist, clist_row, column, requisition.width);
2469 
2470   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2471     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
2472 }
2473 
2474 /* PRIVATE CELL FUNCTIONS
2475  *   set_cell_contents
2476  *   cell_size_request
2477  */
2478 static void
set_cell_contents(GtkCMCList * clist,GtkCMCListRow * clist_row,gint column,GtkCMCellType type,const gchar * text,guint8 spacing,GdkPixbuf * pixbuf)2479 set_cell_contents (GtkCMCList    *clist,
2480 		   GtkCMCListRow *clist_row,
2481 		   gint         column,
2482 		   GtkCMCellType  type,
2483 		   const gchar *text,
2484 		   guint8       spacing,
2485 		   GdkPixbuf   *pixbuf)
2486 {
2487   GtkRequisition requisition;
2488   gchar *old_text = NULL;
2489   GdkPixbuf *old_pixbuf = NULL;
2490 
2491   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2492   cm_return_if_fail (clist_row != NULL);
2493 
2494   if (clist->column[column].auto_resize &&
2495       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2496     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
2497 						   column, &requisition);
2498 
2499   switch (clist_row->cell[column].type)
2500     {
2501     case GTK_CMCELL_EMPTY:
2502       break;
2503     case GTK_CMCELL_TEXT:
2504       old_text = GTK_CMCELL_TEXT (clist_row->cell[column])->text;
2505       break;
2506     case GTK_CMCELL_PIXBUF:
2507       old_pixbuf = GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf;
2508       break;
2509     case GTK_CMCELL_PIXTEXT:
2510       old_text = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text;
2511       old_pixbuf = GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf;
2512       break;
2513     case GTK_CMCELL_WIDGET:
2514       /* unimplemented */
2515       break;
2516     default:
2517       break;
2518     }
2519 
2520   clist_row->cell[column].type = GTK_CMCELL_EMPTY;
2521 
2522   /* Note that pixbuf and mask were already ref'ed by the caller
2523    */
2524   switch (type)
2525     {
2526     case GTK_CMCELL_TEXT:
2527       if (text)
2528 	{
2529 	  clist_row->cell[column].type = GTK_CMCELL_TEXT;
2530 	  GTK_CMCELL_TEXT (clist_row->cell[column])->text = g_strdup (text);
2531 	}
2532       break;
2533     case GTK_CMCELL_PIXBUF:
2534       if (pixbuf)
2535 	{
2536 	  clist_row->cell[column].type = GTK_CMCELL_PIXBUF;
2537 	  GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf = pixbuf;
2538 	}
2539       break;
2540     case GTK_CMCELL_PIXTEXT:
2541       if (text && pixbuf)
2542 	{
2543 	  clist_row->cell[column].type = GTK_CMCELL_PIXTEXT;
2544 	  GTK_CMCELL_PIXTEXT (clist_row->cell[column])->text = g_strdup (text);
2545 	  GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing = spacing;
2546 	  GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf = pixbuf;
2547 	}
2548       break;
2549     default:
2550       break;
2551     }
2552 
2553   if (clist->column[column].auto_resize &&
2554       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
2555     column_auto_resize (clist, clist_row, column, requisition.width);
2556 
2557   g_free (old_text);
2558   if (old_pixbuf)
2559     g_object_unref (old_pixbuf);
2560 }
2561 
2562 PangoLayout *
_gtk_cmclist_create_cell_layout(GtkCMCList * clist,GtkCMCListRow * clist_row,gint column)2563 _gtk_cmclist_create_cell_layout (GtkCMCList       *clist,
2564 			       GtkCMCListRow    *clist_row,
2565 			       gint            column)
2566 {
2567   PangoLayout *layout;
2568   GtkStyle *style;
2569   GtkCMCell *cell;
2570   gchar *text;
2571 
2572   get_cell_style (clist, clist_row, GTK_STATE_NORMAL, column, &style);
2573 
2574 
2575   cell = &clist_row->cell[column];
2576   switch (cell->type)
2577     {
2578     case GTK_CMCELL_TEXT:
2579     case GTK_CMCELL_PIXTEXT:
2580       text = ((cell->type == GTK_CMCELL_PIXTEXT) ?
2581 	      GTK_CMCELL_PIXTEXT (*cell)->text :
2582 	      GTK_CMCELL_TEXT (*cell)->text);
2583 
2584       if (!text)
2585 	return NULL;
2586 
2587       layout = gtk_widget_create_pango_layout (GTK_WIDGET (clist),
2588 					       ((cell->type == GTK_CMCELL_PIXTEXT) ?
2589 						GTK_CMCELL_PIXTEXT (*cell)->text :
2590 						GTK_CMCELL_TEXT (*cell)->text));
2591       pango_layout_set_font_description (layout, style->font_desc);
2592 
2593       return layout;
2594 
2595     default:
2596       return NULL;
2597     }
2598 }
2599 
2600 static void
cell_size_request(GtkCMCList * clist,GtkCMCListRow * clist_row,gint column,GtkRequisition * requisition)2601 cell_size_request (GtkCMCList       *clist,
2602 		   GtkCMCListRow    *clist_row,
2603 		   gint            column,
2604 		   GtkRequisition *requisition)
2605 {
2606   gint width;
2607   gint height;
2608   PangoLayout *layout;
2609   PangoRectangle logical_rect;
2610 
2611   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2612   cm_return_if_fail (requisition != NULL);
2613 
2614   layout = _gtk_cmclist_create_cell_layout (clist, clist_row, column);
2615   if (layout)
2616     {
2617       pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
2618 
2619       requisition->width = logical_rect.width;
2620       requisition->height = logical_rect.height;
2621 
2622       g_object_unref (G_OBJECT (layout));
2623     }
2624   else
2625     {
2626       requisition->width  = 0;
2627       requisition->height = 0;
2628     }
2629 
2630   if (layout && clist_row->cell[column].type == GTK_CMCELL_PIXTEXT)
2631     requisition->width += GTK_CMCELL_PIXTEXT (clist_row->cell[column])->spacing;
2632 
2633   switch (clist_row->cell[column].type)
2634     {
2635     case GTK_CMCELL_PIXTEXT:
2636       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2637       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[column])->pixbuf);
2638       requisition->width += width;
2639       requisition->height = MAX (requisition->height, height);
2640       break;
2641     case GTK_CMCELL_PIXBUF:
2642       width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2643       height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[column])->pixbuf);
2644       requisition->width += width;
2645       requisition->height = MAX (requisition->height, height);
2646       break;
2647 
2648     default:
2649       break;
2650     }
2651 
2652   requisition->width  += clist_row->cell[column].horizontal;
2653   requisition->height += clist_row->cell[column].vertical;
2654 }
2655 
2656 /* PUBLIC INSERT/REMOVE ROW FUNCTIONS
2657  *   gtk_cmclist_prepend
2658  *   gtk_cmclist_append
2659  *   gtk_cmclist_insert
2660  *   gtk_cmclist_remove
2661  *   gtk_cmclist_clear
2662  */
2663 gint
gtk_cmclist_prepend(GtkCMCList * clist,gchar * text[])2664 gtk_cmclist_prepend (GtkCMCList    *clist,
2665 		   gchar       *text[])
2666 {
2667   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2668   cm_return_val_if_fail (text != NULL, -1);
2669 
2670   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, 0, text);
2671 }
2672 
2673 gint
gtk_cmclist_append(GtkCMCList * clist,gchar * text[])2674 gtk_cmclist_append (GtkCMCList    *clist,
2675 		  gchar       *text[])
2676 {
2677   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2678   cm_return_val_if_fail (text != NULL, -1);
2679 
2680   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, clist->rows, text);
2681 }
2682 
2683 gint
gtk_cmclist_insert(GtkCMCList * clist,gint row,gchar * text[])2684 gtk_cmclist_insert (GtkCMCList    *clist,
2685 		  gint         row,
2686 		  gchar       *text[])
2687 {
2688   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2689   cm_return_val_if_fail (text != NULL, -1);
2690 
2691   if (row < 0 || row > clist->rows)
2692     row = clist->rows;
2693 
2694   return GTK_CMCLIST_GET_CLASS (clist)->insert_row (clist, row, text);
2695 }
2696 
2697 void
gtk_cmclist_remove(GtkCMCList * clist,gint row)2698 gtk_cmclist_remove (GtkCMCList *clist,
2699 		  gint      row)
2700 {
2701   GTK_CMCLIST_GET_CLASS (clist)->remove_row (clist, row);
2702 }
2703 
2704 void
gtk_cmclist_clear(GtkCMCList * clist)2705 gtk_cmclist_clear (GtkCMCList *clist)
2706 {
2707   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2708 
2709   GTK_CMCLIST_GET_CLASS (clist)->clear (clist);
2710 }
2711 
2712 /* PRIVATE INSERT/REMOVE ROW FUNCTIONS
2713  *   real_insert_row
2714  *   real_remove_row
2715  *   real_clear
2716  *   real_row_move
2717  */
2718 static gint
real_insert_row(GtkCMCList * clist,gint row,gchar * text[])2719 real_insert_row (GtkCMCList *clist,
2720 		 gint      row,
2721 		 gchar    *text[])
2722 {
2723   gint i;
2724   GtkCMCListRow *clist_row;
2725 
2726   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
2727   cm_return_val_if_fail (text != NULL, -1);
2728 
2729   /* return if out of bounds */
2730   if (row < 0 || row > clist->rows)
2731     return -1;
2732 
2733   /* create the row */
2734   clist_row = row_new (clist);
2735 
2736   /* set the text in the row's columns */
2737   for (i = 0; i < clist->columns; i++)
2738     if (text[i])
2739       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
2740 	(clist, clist_row, i, GTK_CMCELL_TEXT, text[i], 0, NULL);
2741 
2742   if (!clist->rows)
2743     {
2744       clist->row_list = g_list_append (clist->row_list, clist_row);
2745       clist->row_list_end = clist->row_list;
2746     }
2747   else
2748     {
2749       if (GTK_CMCLIST_AUTO_SORT(clist))   /* override insertion pos */
2750 	{
2751 	  GList *work;
2752 
2753 	  row = 0;
2754 	  work = clist->row_list;
2755 
2756 	  if (clist->sort_type == GTK_SORT_ASCENDING)
2757 	    {
2758 	      while (row < clist->rows &&
2759 		     clist->compare (clist, clist_row,
2760 				     GTK_CMCLIST_ROW (work)) > 0)
2761 		{
2762 		  row++;
2763 		  work = work->next;
2764 		}
2765 	    }
2766 	  else
2767 	    {
2768 	      while (row < clist->rows &&
2769 		     clist->compare (clist, clist_row,
2770 				     GTK_CMCLIST_ROW (work)) < 0)
2771 		{
2772 		  row++;
2773 		  work = work->next;
2774 		}
2775 	    }
2776 	}
2777 
2778       /* reset the row end pointer if we're inserting at the end of the list */
2779       if (row == clist->rows)
2780 	clist->row_list_end = (g_list_append (clist->row_list_end,
2781 					      clist_row))->next;
2782       else
2783 	clist->row_list = g_list_insert (clist->row_list, clist_row, row);
2784 
2785     }
2786   clist->rows++;
2787 
2788   if (row < ROW_FROM_YPIXEL (clist, 0))
2789     clist->voffset -= (clist->row_height + CELL_SPACING);
2790 
2791   /* syncronize the selection list */
2792   sync_selection (clist, row, SYNC_INSERT);
2793 
2794   if (clist->rows == 1)
2795     {
2796       clist->focus_row = 0;
2797       if (clist->selection_mode == GTK_SELECTION_BROWSE)
2798 	gtk_cmclist_select_row (clist, 0, -1);
2799     }
2800 
2801   /* redraw the list if it isn't frozen */
2802   if (CLIST_UNFROZEN (clist))
2803     {
2804       adjust_adjustments (clist, FALSE);
2805 
2806       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
2807 	draw_rows (clist, NULL);
2808     }
2809 
2810   return row;
2811 }
2812 
2813 static void
real_remove_row(GtkCMCList * clist,gint row)2814 real_remove_row (GtkCMCList *clist,
2815 		 gint      row)
2816 {
2817   gint was_visible;
2818   GList *list;
2819   GtkCMCListRow *clist_row;
2820 
2821   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2822 
2823   /* return if out of bounds */
2824   if (row < 0 || row > (clist->rows - 1))
2825     return;
2826 
2827   was_visible = (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE);
2828 
2829   /* get the row we're going to delete */
2830   list = ROW_ELEMENT (clist, row);
2831   g_assert (list != NULL);
2832   clist_row = list->data;
2833 
2834   /* if we're removing a selected row, we have to make sure
2835    * it's properly unselected, and then sync up the clist->selected
2836    * list to reflect the deincrimented indexies of rows after the
2837    * removal */
2838   if (clist_row->state == GTK_STATE_SELECTED)
2839     g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
2840 		     row, -1, NULL);
2841 
2842   sync_selection (clist, row, SYNC_REMOVE);
2843 
2844   /* reset the row end pointer if we're removing at the end of the list */
2845   clist->rows--;
2846   if (clist->row_list == list)
2847     clist->row_list = g_list_next (list);
2848   if (clist->row_list_end == list)
2849     clist->row_list_end = g_list_previous (list);
2850   list = g_list_remove (list, clist_row);
2851 
2852   if (row < ROW_FROM_YPIXEL (clist, 0))
2853     clist->voffset += clist->row_height + CELL_SPACING;
2854 
2855   if (clist->selection_mode == GTK_SELECTION_BROWSE && !clist->selection &&
2856       clist->focus_row >= 0)
2857     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
2858 		     clist->focus_row, -1, NULL);
2859 
2860   /* toast the row */
2861   row_delete (clist, clist_row);
2862 
2863   /* redraw the row if it isn't frozen */
2864   if (CLIST_UNFROZEN (clist))
2865     {
2866       adjust_adjustments (clist, FALSE);
2867 
2868       if (was_visible)
2869 	draw_rows (clist, NULL);
2870     }
2871 }
2872 
2873 static void
real_clear(GtkCMCList * clist)2874 real_clear (GtkCMCList *clist)
2875 {
2876   GList *list;
2877   GList *free_list;
2878   GtkRequisition requisition;
2879   gint i;
2880 
2881   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2882 
2883   /* free up the selection list */
2884   g_list_free (clist->selection);
2885   g_list_free (clist->undo_selection);
2886   g_list_free (clist->undo_unselection);
2887 
2888   clist->selection = NULL;
2889   clist->selection_end = NULL;
2890   clist->undo_selection = NULL;
2891   clist->undo_unselection = NULL;
2892   clist->voffset = 0;
2893   clist->focus_row = -1;
2894   clist->anchor = -1;
2895   clist->undo_anchor = -1;
2896   clist->anchor_state = GTK_STATE_SELECTED;
2897   clist->drag_pos = -1;
2898 
2899   /* remove all the rows */
2900   GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
2901   free_list = clist->row_list;
2902   clist->row_list = NULL;
2903   clist->row_list_end = NULL;
2904   clist->rows = 0;
2905   for (list = free_list; list; list = list->next)
2906     row_delete (clist, GTK_CMCLIST_ROW (list));
2907   g_list_free (free_list);
2908   GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_RESIZE_BLOCKED);
2909   for (i = 0; i < clist->columns; i++)
2910     if (clist->column[i].auto_resize)
2911       {
2912 	if (GTK_CMCLIST_SHOW_TITLES(clist) && clist->column[i].button)
2913     {
2914 	gtk_widget_get_requisition (clist->column[i].button, &requisition);
2915 	  gtk_cmclist_set_column_width
2916 	    (clist, i, (requisition.width -
2917 			(CELL_SPACING + (2 * COLUMN_INSET))));
2918     }
2919 	else
2920 	  gtk_cmclist_set_column_width (clist, i, 0);
2921       }
2922   /* zero-out the scrollbars */
2923   if (clist->vadjustment)
2924     {
2925       gtk_adjustment_set_value (clist->vadjustment, 0.0);
2926       CLIST_REFRESH (clist);
2927     }
2928   else
2929     gtk_widget_queue_resize (GTK_WIDGET (clist));
2930 }
2931 
2932 static void
real_row_move(GtkCMCList * clist,gint source_row,gint dest_row)2933 real_row_move (GtkCMCList *clist,
2934 	       gint      source_row,
2935 	       gint      dest_row)
2936 {
2937   GtkCMCListRow *clist_row;
2938   GList *list;
2939   gint first, last;
2940   gint d;
2941 
2942   cm_return_if_fail (GTK_IS_CMCLIST (clist));
2943 
2944   if (GTK_CMCLIST_AUTO_SORT(clist))
2945     return;
2946 
2947   if (source_row < 0 || source_row >= clist->rows ||
2948       dest_row   < 0 || dest_row   >= clist->rows ||
2949       source_row == dest_row)
2950     return;
2951 
2952   gtk_cmclist_freeze (clist);
2953 
2954   /* unlink source row */
2955   clist_row = ROW_ELEMENT (clist, source_row)->data;
2956   if (source_row == clist->rows - 1)
2957     clist->row_list_end = clist->row_list_end->prev;
2958   clist->row_list = g_list_remove (clist->row_list, clist_row);
2959   clist->rows--;
2960 
2961   /* relink source row */
2962   clist->row_list = g_list_insert (clist->row_list, clist_row, dest_row);
2963   if (dest_row == clist->rows)
2964     clist->row_list_end = clist->row_list_end->next;
2965   clist->rows++;
2966 
2967   /* sync selection */
2968   if (source_row > dest_row)
2969     {
2970       first = dest_row;
2971       last  = source_row;
2972       d = 1;
2973     }
2974   else
2975     {
2976       first = source_row;
2977       last  = dest_row;
2978       d = -1;
2979     }
2980 
2981   for (list = clist->selection; list; list = list->next)
2982     {
2983       if (list->data == GINT_TO_POINTER (source_row))
2984 	list->data = GINT_TO_POINTER (dest_row);
2985       else if (first <= GPOINTER_TO_INT (list->data) &&
2986 	       last >= GPOINTER_TO_INT (list->data))
2987 	list->data = GINT_TO_POINTER (GPOINTER_TO_INT (list->data) + d);
2988     }
2989 
2990   if (clist->focus_row == source_row)
2991     clist->focus_row = dest_row;
2992   else if (clist->focus_row > first)
2993     clist->focus_row += d;
2994 
2995   gtk_cmclist_thaw (clist);
2996 }
2997 
2998 /* PUBLIC ROW FUNCTIONS
2999  *   gtk_cmclist_moveto
3000  *   gtk_cmclist_set_row_height
3001  *   gtk_cmclist_set_row_data
3002  *   gtk_cmclist_set_row_data_full
3003  *   gtk_cmclist_get_row_data
3004  *   gtk_cmclist_find_row_from_data
3005  *   gtk_cmclist_swap_rows
3006  *   gtk_cmclist_row_move
3007  *   gtk_cmclist_row_is_visible
3008  *   gtk_cmclist_set_foreground
3009  *   gtk_cmclist_set_background
3010  */
3011 void
gtk_cmclist_moveto(GtkCMCList * clist,gint row,gint column,gfloat row_align,gfloat col_align)3012 gtk_cmclist_moveto (GtkCMCList *clist,
3013 		  gint      row,
3014 		  gint      column,
3015 		  gfloat    row_align,
3016 		  gfloat    col_align)
3017 {
3018   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3019 
3020   if (row < -1 || row >= clist->rows)
3021     return;
3022   if (column < -1 || column >= clist->columns)
3023     return;
3024 
3025   row_align = CLAMP (row_align, 0, 1);
3026   col_align = CLAMP (col_align, 0, 1);
3027 
3028   /* adjust horizontal scrollbar */
3029   if (clist->hadjustment && column >= 0)
3030     {
3031       gint x;
3032 
3033       x = (COLUMN_LEFT (clist, column) - CELL_SPACING - COLUMN_INSET -
3034 	   (col_align * (clist->clist_window_width - 2 * COLUMN_INSET -
3035 			 CELL_SPACING - clist->column[column].area.width)));
3036       if (x < 0)
3037 	gtk_adjustment_set_value (clist->hadjustment, 0.0);
3038       else if (x > LIST_WIDTH (clist) - clist->clist_window_width)
3039 	gtk_adjustment_set_value
3040 	  (clist->hadjustment, LIST_WIDTH (clist) - clist->clist_window_width);
3041       else
3042 	gtk_adjustment_set_value (clist->hadjustment, x);
3043     }
3044 
3045   /* adjust vertical scrollbar */
3046   if (clist->vadjustment && row >= 0)
3047     move_vertical (clist, row, row_align);
3048 }
3049 
3050 void
gtk_cmclist_set_row_height(GtkCMCList * clist,guint height)3051 gtk_cmclist_set_row_height (GtkCMCList *clist,
3052 			  guint     height)
3053 {
3054   GtkStyle *style;
3055   GtkWidget *widget;
3056 
3057   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3058 
3059   widget = GTK_WIDGET (clist);
3060 
3061   style = gtk_widget_get_style (widget);
3062 
3063   if (height > 0)
3064     {
3065       clist->row_height = height;
3066       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3067     }
3068   else
3069     {
3070       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ROW_HEIGHT_SET);
3071       clist->row_height = 0;
3072     }
3073 
3074   if (style->font_desc)
3075     {
3076       PangoContext *context = gtk_widget_get_pango_context (widget);
3077       PangoFontMetrics *metrics;
3078 
3079       metrics = pango_context_get_metrics (context,
3080 					   style->font_desc,
3081 					   pango_context_get_language (context));
3082 
3083       if (!GTK_CMCLIST_ROW_HEIGHT_SET(clist))
3084 	{
3085 	  clist->row_height = (pango_font_metrics_get_ascent (metrics) +
3086 			       pango_font_metrics_get_descent (metrics));
3087 	  clist->row_height = PANGO_PIXELS (clist->row_height) + 1;
3088 	}
3089 
3090       pango_font_metrics_unref (metrics);
3091     }
3092 
3093   CLIST_REFRESH (clist);
3094 }
3095 
3096 void
gtk_cmclist_set_row_data(GtkCMCList * clist,gint row,gpointer data)3097 gtk_cmclist_set_row_data (GtkCMCList *clist,
3098 			gint      row,
3099 			gpointer  data)
3100 {
3101   gtk_cmclist_set_row_data_full (clist, row, data, NULL);
3102 }
3103 
3104 void
gtk_cmclist_set_row_data_full(GtkCMCList * clist,gint row,gpointer data,GDestroyNotify destroy)3105 gtk_cmclist_set_row_data_full (GtkCMCList         *clist,
3106 			     gint              row,
3107 			     gpointer          data,
3108 			     GDestroyNotify  destroy)
3109 {
3110   GtkCMCListRow *clist_row;
3111 
3112   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3113 
3114   if (row < 0 || row > (clist->rows - 1))
3115     return;
3116 
3117   clist_row = ROW_ELEMENT (clist, row)->data;
3118 
3119   if (clist_row->destroy)
3120     clist_row->destroy (clist_row->data);
3121 
3122   clist_row->data = data;
3123   clist_row->destroy = destroy;
3124 }
3125 
3126 gpointer
gtk_cmclist_get_row_data(GtkCMCList * clist,gint row)3127 gtk_cmclist_get_row_data (GtkCMCList *clist,
3128 			gint      row)
3129 {
3130   GtkCMCListRow *clist_row;
3131 
3132   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3133 
3134   if (row < 0 || row > (clist->rows - 1))
3135     return NULL;
3136 
3137   clist_row = ROW_ELEMENT (clist, row)->data;
3138   return clist_row->data;
3139 }
3140 
3141 gint
gtk_cmclist_find_row_from_data(GtkCMCList * clist,gpointer data)3142 gtk_cmclist_find_row_from_data (GtkCMCList *clist,
3143 			      gpointer  data)
3144 {
3145   GList *list;
3146   gint n;
3147 
3148   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), -1);
3149 
3150   for (n = 0, list = clist->row_list; list; n++, list = list->next)
3151     if (GTK_CMCLIST_ROW (list)->data == data)
3152       return n;
3153 
3154   return -1;
3155 }
3156 
3157 void
gtk_cmclist_swap_rows(GtkCMCList * clist,gint row1,gint row2)3158 gtk_cmclist_swap_rows (GtkCMCList *clist,
3159 		     gint      row1,
3160 		     gint      row2)
3161 {
3162   gint first, last;
3163 
3164   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3165   cm_return_if_fail (row1 != row2);
3166 
3167   if (GTK_CMCLIST_AUTO_SORT(clist))
3168     return;
3169 
3170   gtk_cmclist_freeze (clist);
3171 
3172   first = MIN (row1, row2);
3173   last  = MAX (row1, row2);
3174 
3175   gtk_cmclist_row_move (clist, last, first);
3176   gtk_cmclist_row_move (clist, first + 1, last);
3177 
3178   gtk_cmclist_thaw (clist);
3179 }
3180 
3181 void
gtk_cmclist_row_move(GtkCMCList * clist,gint source_row,gint dest_row)3182 gtk_cmclist_row_move (GtkCMCList *clist,
3183 		    gint      source_row,
3184 		    gint      dest_row)
3185 {
3186   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3187 
3188   if (GTK_CMCLIST_AUTO_SORT(clist))
3189     return;
3190 
3191   if (source_row < 0 || source_row >= clist->rows ||
3192       dest_row   < 0 || dest_row   >= clist->rows ||
3193       source_row == dest_row)
3194     return;
3195 
3196   g_signal_emit (G_OBJECT (clist), clist_signals[ROW_MOVE], 0,
3197 		   source_row, dest_row);
3198 }
3199 
3200 GtkVisibility
gtk_cmclist_row_is_visible(GtkCMCList * clist,gint row)3201 gtk_cmclist_row_is_visible (GtkCMCList *clist,
3202 			  gint      row)
3203 {
3204   gint top;
3205 
3206   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
3207 
3208   if (row < 0 || row >= clist->rows)
3209     return GTK_VISIBILITY_NONE;
3210 
3211   if (clist->row_height == 0)
3212     return GTK_VISIBILITY_NONE;
3213 
3214   if (row < ROW_FROM_YPIXEL (clist, 0))
3215     return GTK_VISIBILITY_NONE;
3216 
3217   if (row > ROW_FROM_YPIXEL (clist, clist->clist_window_height))
3218     return GTK_VISIBILITY_NONE;
3219 
3220   top = ROW_TOP_YPIXEL (clist, row);
3221 
3222   if ((top < 0)
3223       || ((top + clist->row_height) >= clist->clist_window_height))
3224     return GTK_VISIBILITY_PARTIAL;
3225 
3226   return GTK_VISIBILITY_FULL;
3227 }
3228 
3229 gboolean
gtk_cmclist_row_is_above_viewport(GtkCMCList * clist,gint row)3230 gtk_cmclist_row_is_above_viewport (GtkCMCList *clist,
3231 				gint row)
3232 {
3233 	cm_return_val_if_fail(GTK_IS_CMCLIST (clist), 0);
3234 
3235 	if (row < 0 || row >= clist->rows)
3236 		return FALSE;
3237 
3238 	if (clist->row_height == 0)
3239 		return FALSE;
3240 
3241 	if (row < ROW_FROM_YPIXEL (clist, 0))
3242 		return TRUE;
3243 
3244 	return FALSE;
3245 }
3246 
3247 gboolean
gtk_cmclist_row_is_below_viewport(GtkCMCList * clist,gint row)3248 gtk_cmclist_row_is_below_viewport (GtkCMCList *clist,
3249 				gint row)
3250 {
3251 	cm_return_val_if_fail(GTK_IS_CMCLIST (clist), 0);
3252 
3253 	if (row < 0 || row >= clist->rows)
3254 		return FALSE;
3255 
3256 	if (clist->row_height == 0)
3257 		return FALSE;
3258 
3259 	if (row > ROW_FROM_YPIXEL (clist, clist->clist_window_height))
3260 		return TRUE;
3261 
3262 	return FALSE;
3263 }
3264 
3265 void
gtk_cmclist_set_foreground(GtkCMCList * clist,gint row,const GdkColor * color)3266 gtk_cmclist_set_foreground (GtkCMCList       *clist,
3267 			  gint            row,
3268 			  const GdkColor *color)
3269 {
3270   GtkCMCListRow *clist_row;
3271 
3272   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3273 
3274   if (row < 0 || row >= clist->rows)
3275     return;
3276 
3277   clist_row = ROW_ELEMENT (clist, row)->data;
3278 
3279   if (color)
3280     {
3281       clist_row->foreground = *color;
3282       clist_row->fg_set = TRUE;
3283       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3284 	gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3285 			 &clist_row->foreground, TRUE, TRUE);
3286     }
3287   else
3288     clist_row->fg_set = FALSE;
3289 
3290   if (CLIST_UNFROZEN (clist) && gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3291     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3292 }
3293 
3294 void
gtk_cmclist_set_background(GtkCMCList * clist,gint row,const GdkColor * color)3295 gtk_cmclist_set_background (GtkCMCList       *clist,
3296 			  gint            row,
3297 			  const GdkColor *color)
3298 {
3299   GtkCMCListRow *clist_row;
3300 
3301   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3302 
3303   if (row < 0 || row >= clist->rows)
3304     return;
3305 
3306   clist_row = ROW_ELEMENT (clist, row)->data;
3307 
3308   if (color)
3309     {
3310       clist_row->background = *color;
3311       clist_row->bg_set = TRUE;
3312       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3313 	gdk_colormap_alloc_color (gtk_widget_get_colormap (GTK_WIDGET (clist)),
3314 			 &clist_row->background, TRUE, TRUE);
3315     }
3316   else
3317     clist_row->bg_set = FALSE;
3318 
3319   if (CLIST_UNFROZEN (clist)
3320       && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3321     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3322 }
3323 
3324 /* PUBLIC ROW/CELL STYLE FUNCTIONS
3325  *   gtk_cmclist_set_cell_style
3326  *   gtk_cmclist_get_cell_style
3327  *   gtk_cmclist_set_row_style
3328  *   gtk_cmclist_get_row_style
3329  */
3330 void
gtk_cmclist_set_cell_style(GtkCMCList * clist,gint row,gint column,GtkStyle * style)3331 gtk_cmclist_set_cell_style (GtkCMCList *clist,
3332 			  gint      row,
3333 			  gint      column,
3334 			  GtkStyle *style)
3335 {
3336   GtkRequisition requisition = { 0 };
3337   GtkCMCListRow *clist_row;
3338 
3339   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3340 
3341   if (row < 0 || row >= clist->rows)
3342     return;
3343   if (column < 0 || column >= clist->columns)
3344     return;
3345 
3346   clist_row = ROW_ELEMENT (clist, row)->data;
3347 
3348   if (clist_row->cell[column].style == style)
3349     return;
3350 
3351   if (clist->column[column].auto_resize &&
3352       !GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3353     GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3354 						   column, &requisition);
3355 
3356   if (clist_row->cell[column].style)
3357     {
3358       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3359         gtk_style_detach (clist_row->cell[column].style);
3360       g_object_unref (clist_row->cell[column].style);
3361     }
3362 
3363   clist_row->cell[column].style = style;
3364 
3365   if (clist_row->cell[column].style)
3366     {
3367       g_object_ref (clist_row->cell[column].style);
3368 
3369       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3370         clist_row->cell[column].style =
3371 	  gtk_style_attach (clist_row->cell[column].style,
3372 			    clist->clist_window);
3373     }
3374 
3375   column_auto_resize (clist, clist_row, column, requisition.width);
3376 
3377   /* redraw the list if it's not frozen */
3378   if (CLIST_UNFROZEN (clist))
3379     {
3380       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3381 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3382     }
3383 }
3384 
3385 GtkStyle *
gtk_cmclist_get_cell_style(GtkCMCList * clist,gint row,gint column)3386 gtk_cmclist_get_cell_style (GtkCMCList *clist,
3387 			  gint      row,
3388 			  gint      column)
3389 {
3390   GtkCMCListRow *clist_row;
3391 
3392   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3393 
3394   if (row < 0 || row >= clist->rows || column < 0 || column >= clist->columns)
3395     return NULL;
3396 
3397   clist_row = ROW_ELEMENT (clist, row)->data;
3398 
3399   return clist_row->cell[column].style;
3400 }
3401 
3402 void
gtk_cmclist_set_row_style(GtkCMCList * clist,gint row,GtkStyle * style)3403 gtk_cmclist_set_row_style (GtkCMCList *clist,
3404 			 gint      row,
3405 			 GtkStyle *style)
3406 {
3407   GtkRequisition requisition;
3408   GtkCMCListRow *clist_row;
3409   gint *old_width;
3410   gint i;
3411 
3412   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3413 
3414   if (row < 0 || row >= clist->rows)
3415     return;
3416 
3417   clist_row = ROW_ELEMENT (clist, row)->data;
3418 
3419   if (clist_row->style == style)
3420     return;
3421 
3422   old_width = g_new (gint, clist->columns);
3423 
3424   if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3425     {
3426       for (i = 0; i < clist->columns; i++)
3427 	if (clist->column[i].auto_resize)
3428 	  {
3429 	    GTK_CMCLIST_GET_CLASS (clist)->cell_size_request (clist, clist_row,
3430 							   i, &requisition);
3431 	    old_width[i] = requisition.width;
3432 	  }
3433     }
3434 
3435   if (clist_row->style)
3436     {
3437       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3438         gtk_style_detach (clist_row->style);
3439       g_object_unref (clist_row->style);
3440     }
3441 
3442   clist_row->style = style;
3443 
3444   if (clist_row->style)
3445     {
3446       g_object_ref (clist_row->style);
3447 
3448       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
3449         clist_row->style = gtk_style_attach (clist_row->style,
3450 					     clist->clist_window);
3451     }
3452 
3453   if (GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
3454     for (i = 0; i < clist->columns; i++)
3455       column_auto_resize (clist, clist_row, i, old_width[i]);
3456 
3457   g_free (old_width);
3458 
3459   /* redraw the list if it's not frozen */
3460   if (CLIST_UNFROZEN (clist))
3461     {
3462       if (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3463 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3464     }
3465 }
3466 
3467 GtkStyle *
gtk_cmclist_get_row_style(GtkCMCList * clist,gint row)3468 gtk_cmclist_get_row_style (GtkCMCList *clist,
3469 			 gint      row)
3470 {
3471   GtkCMCListRow *clist_row;
3472 
3473   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), NULL);
3474 
3475   if (row < 0 || row >= clist->rows)
3476     return NULL;
3477 
3478   clist_row = ROW_ELEMENT (clist, row)->data;
3479 
3480   return clist_row->style;
3481 }
3482 
3483 /* PUBLIC SELECTION FUNCTIONS
3484  *   gtk_cmclist_set_selectable
3485  *   gtk_cmclist_get_selectable
3486  *   gtk_cmclist_select_row
3487  *   gtk_cmclist_unselect_row
3488  *   gtk_cmclist_select_all
3489  *   gtk_cmclist_unselect_all
3490  *   gtk_cmclist_undo_selection
3491  */
3492 void
gtk_cmclist_set_selectable(GtkCMCList * clist,gint row,gboolean selectable)3493 gtk_cmclist_set_selectable (GtkCMCList *clist,
3494 			  gint      row,
3495 			  gboolean  selectable)
3496 {
3497   GtkCMCListRow *clist_row;
3498 
3499   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3500 
3501   if (row < 0 || row >= clist->rows)
3502     return;
3503 
3504   clist_row = ROW_ELEMENT (clist, row)->data;
3505 
3506   if (selectable == clist_row->selectable)
3507     return;
3508 
3509   clist_row->selectable = selectable;
3510 
3511   if (!selectable && clist_row->state == GTK_STATE_SELECTED)
3512     {
3513       if (clist->anchor >= 0 &&
3514 	  clist->selection_mode == GTK_SELECTION_MULTIPLE)
3515 	{
3516 	  clist->drag_button = 0;
3517 	  remove_grab (clist);
3518 	  GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3519 	}
3520       g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3521 		       row, -1, NULL);
3522     }
3523 }
3524 
3525 gboolean
gtk_cmclist_get_selectable(GtkCMCList * clist,gint row)3526 gtk_cmclist_get_selectable (GtkCMCList *clist,
3527 			  gint      row)
3528 {
3529   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), FALSE);
3530 
3531   if (row < 0 || row >= clist->rows)
3532     return FALSE;
3533 
3534   return GTK_CMCLIST_ROW (ROW_ELEMENT (clist, row))->selectable;
3535 }
3536 
3537 void
gtk_cmclist_select_row(GtkCMCList * clist,gint row,gint column)3538 gtk_cmclist_select_row (GtkCMCList *clist,
3539 		      gint      row,
3540 		      gint      column)
3541 {
3542   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3543 
3544   if (row < 0 || row >= clist->rows)
3545     return;
3546   if (column < -1 || column >= clist->columns)
3547     return;
3548 
3549   g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3550 		   row, column, NULL);
3551 }
3552 
3553 void
gtk_cmclist_unselect_row(GtkCMCList * clist,gint row,gint column)3554 gtk_cmclist_unselect_row (GtkCMCList *clist,
3555 			gint      row,
3556 			gint      column)
3557 {
3558   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3559 
3560   if (row < 0 || row >= clist->rows)
3561     return;
3562   if (column < -1 || column >= clist->columns)
3563     return;
3564 
3565   g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3566 		   row, column, NULL);
3567 }
3568 
3569 void
gtk_cmclist_select_all(GtkCMCList * clist)3570 gtk_cmclist_select_all (GtkCMCList *clist)
3571 {
3572   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3573 
3574   GTK_CMCLIST_GET_CLASS (clist)->select_all (clist);
3575 }
3576 
3577 void
gtk_cmclist_unselect_all(GtkCMCList * clist)3578 gtk_cmclist_unselect_all (GtkCMCList *clist)
3579 {
3580   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3581 
3582   GTK_CMCLIST_GET_CLASS (clist)->unselect_all (clist);
3583 }
3584 
3585 void
gtk_cmclist_undo_selection(GtkCMCList * clist)3586 gtk_cmclist_undo_selection (GtkCMCList *clist)
3587 {
3588   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3589 
3590   if (clist->selection_mode == GTK_SELECTION_MULTIPLE &&
3591       (clist->undo_selection || clist->undo_unselection))
3592     g_signal_emit (G_OBJECT (clist), clist_signals[UNDO_SELECTION], 0);
3593 }
3594 
3595 /* PRIVATE SELECTION FUNCTIONS
3596  *   selection_find
3597  *   toggle_row
3598  *   fake_toggle_row
3599  *   toggle_focus_row
3600  *   toggle_add_mode
3601  *   real_select_row
3602  *   real_unselect_row
3603  *   real_select_all
3604  *   real_unselect_all
3605  *   fake_unselect_all
3606  *   real_undo_selection
3607  *   set_anchor
3608  *   resync_selection
3609  *   update_extended_selection
3610  *   start_selection
3611  *   end_selection
3612  *   extend_selection
3613  *   sync_selection
3614  */
3615 static GList *
selection_find(GtkCMCList * clist,gint row_number,GList * row_list_element)3616 selection_find (GtkCMCList *clist,
3617 		gint      row_number,
3618 		GList    *row_list_element)
3619 {
3620   return g_list_find (clist->selection, GINT_TO_POINTER (row_number));
3621 }
3622 
3623 static void
toggle_row(GtkCMCList * clist,gint row,gint column,GdkEvent * event)3624 toggle_row (GtkCMCList *clist,
3625 	    gint      row,
3626 	    gint      column,
3627 	    GdkEvent *event)
3628 {
3629   GtkCMCListRow *clist_row;
3630 
3631   switch (clist->selection_mode)
3632     {
3633     case GTK_SELECTION_MULTIPLE:
3634     case GTK_SELECTION_SINGLE:
3635       clist_row = ROW_ELEMENT (clist, row)->data;
3636 
3637       if (!clist_row)
3638 	return;
3639 
3640       if (clist_row->state == GTK_STATE_SELECTED)
3641 	{
3642 	  g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3643 			   row, column, event);
3644 	  return;
3645 	}
3646       break;
3647     case GTK_SELECTION_BROWSE:
3648       g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3649 		       row, column, event);
3650       break;
3651     default:
3652       g_assert_not_reached ();
3653     }
3654 }
3655 
3656 static void
fake_toggle_row(GtkCMCList * clist,gint row)3657 fake_toggle_row (GtkCMCList *clist,
3658 		 gint      row)
3659 {
3660   GList *work;
3661 
3662   work = ROW_ELEMENT (clist, row);
3663 
3664   if (!work || !GTK_CMCLIST_ROW (work)->selectable)
3665     return;
3666 
3667   if (GTK_CMCLIST_ROW (work)->state == GTK_STATE_NORMAL)
3668     clist->anchor_state = GTK_CMCLIST_ROW (work)->state = GTK_STATE_SELECTED;
3669   else
3670     clist->anchor_state = GTK_CMCLIST_ROW (work)->state = GTK_STATE_NORMAL;
3671 
3672   if (CLIST_UNFROZEN (clist) &&
3673       gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3674     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row,
3675 					  GTK_CMCLIST_ROW (work));
3676 }
3677 
3678 static gboolean
clist_has_grab(GtkCMCList * clist)3679 clist_has_grab (GtkCMCList *clist)
3680 {
3681   return (gtk_widget_has_grab (GTK_WIDGET(clist)) &&
3682 	  gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (clist))));
3683 }
3684 
3685 static void
toggle_focus_row(GtkCMCList * clist)3686 toggle_focus_row (GtkCMCList *clist)
3687 {
3688   cm_return_if_fail (clist != 0);
3689   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3690 
3691   if (clist_has_grab (clist) ||
3692       clist->focus_row < 0 || clist->focus_row >= clist->rows)
3693     return;
3694 
3695   switch (clist->selection_mode)
3696     {
3697     case  GTK_SELECTION_SINGLE:
3698       toggle_row (clist, clist->focus_row, 0, NULL);
3699       break;
3700     case GTK_SELECTION_MULTIPLE:
3701       g_list_free (clist->undo_selection);
3702       g_list_free (clist->undo_unselection);
3703       clist->undo_selection = NULL;
3704       clist->undo_unselection = NULL;
3705 
3706       clist->anchor = clist->focus_row;
3707       clist->drag_pos = clist->focus_row;
3708       clist->undo_anchor = clist->focus_row;
3709 
3710       if (GTK_CMCLIST_ADD_MODE(clist))
3711 	fake_toggle_row (clist, clist->focus_row);
3712       else
3713 	GTK_CMCLIST_GET_CLASS (clist)->fake_unselect_all (clist,clist->focus_row);
3714 
3715       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3716       break;
3717     default:
3718       break;
3719     }
3720 }
3721 
3722 static void
toggle_add_mode(GtkCMCList * clist)3723 toggle_add_mode (GtkCMCList *clist)
3724 {
3725   cm_return_if_fail (clist != 0);
3726   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3727 
3728   if (clist_has_grab (clist) ||
3729       clist->selection_mode != GTK_SELECTION_MULTIPLE)
3730     return;
3731 
3732   gtk_cmclist_undraw_focus (GTK_WIDGET (clist));
3733   if (!GTK_CMCLIST_ADD_MODE(clist))
3734     {
3735       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_ADD_MODE);
3736     }
3737   else
3738     {
3739       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ADD_MODE);
3740       clist->anchor_state = GTK_STATE_SELECTED;
3741     }
3742   gtk_cmclist_draw_focus (GTK_WIDGET (clist));
3743 }
3744 
3745 static void
real_select_row(GtkCMCList * clist,gint row,gint column,GdkEvent * event)3746 real_select_row (GtkCMCList *clist,
3747 		 gint      row,
3748 		 gint      column,
3749 		 GdkEvent *event)
3750 {
3751   GtkCMCListRow *clist_row;
3752   GList *list;
3753   gint sel_row;
3754   gboolean row_selected;
3755 
3756   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3757 
3758   if (row < 0 || row > (clist->rows - 1))
3759     return;
3760 
3761   switch (clist->selection_mode)
3762     {
3763     case GTK_SELECTION_SINGLE:
3764     case GTK_SELECTION_BROWSE:
3765 
3766       row_selected = FALSE;
3767       list = clist->selection;
3768 
3769       while (list)
3770 	{
3771 	  sel_row = GPOINTER_TO_INT (list->data);
3772 	  list = list->next;
3773 
3774 	  if (row == sel_row)
3775 	    row_selected = TRUE;
3776 	  else
3777 	    g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3778 			     sel_row, column, event);
3779 	}
3780 
3781       if (row_selected)
3782 	return;
3783 
3784     default:
3785       break;
3786     }
3787 
3788   clist_row = ROW_ELEMENT (clist, row)->data;
3789 
3790   if (clist_row->state != GTK_STATE_NORMAL || !clist_row->selectable)
3791     return;
3792 
3793   clist_row->state = GTK_STATE_SELECTED;
3794   if (!clist->selection)
3795     {
3796       clist->selection = g_list_append (clist->selection,
3797 					GINT_TO_POINTER (row));
3798       clist->selection_end = clist->selection;
3799     }
3800   else
3801     clist->selection_end =
3802       g_list_append (clist->selection_end, GINT_TO_POINTER (row))->next;
3803 
3804   if (CLIST_UNFROZEN (clist)
3805       && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3806     GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3807 }
3808 
3809 static void
real_unselect_row(GtkCMCList * clist,gint row,gint column,GdkEvent * event)3810 real_unselect_row (GtkCMCList *clist,
3811 		   gint      row,
3812 		   gint      column,
3813 		   GdkEvent *event)
3814 {
3815   GtkCMCListRow *clist_row;
3816 
3817   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3818 
3819   if (row < 0 || row > (clist->rows - 1))
3820     return;
3821 
3822   clist_row = ROW_ELEMENT (clist, row)->data;
3823 
3824   if (clist_row->state == GTK_STATE_SELECTED)
3825     {
3826       clist_row->state = GTK_STATE_NORMAL;
3827 
3828       if (clist->selection_end &&
3829 	  clist->selection_end->data == GINT_TO_POINTER (row))
3830 	clist->selection_end = clist->selection_end->prev;
3831 
3832       clist->selection = g_list_remove (clist->selection,
3833 					GINT_TO_POINTER (row));
3834 
3835       if (CLIST_UNFROZEN (clist)
3836 	  && (gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
3837 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row, clist_row);
3838     }
3839 }
3840 
3841 static void
real_select_all(GtkCMCList * clist)3842 real_select_all (GtkCMCList *clist)
3843 {
3844   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3845 
3846   if (clist_has_grab (clist))
3847     return;
3848 
3849   switch (clist->selection_mode)
3850     {
3851     case GTK_SELECTION_SINGLE:
3852     case GTK_SELECTION_BROWSE:
3853       return;
3854 
3855     case GTK_SELECTION_MULTIPLE:
3856       g_list_free (clist->undo_selection);
3857       g_list_free (clist->undo_unselection);
3858       clist->undo_selection = NULL;
3859       clist->undo_unselection = NULL;
3860 
3861       if (clist->rows &&
3862 	  ((GtkCMCListRow *) (clist->row_list->data))->state !=
3863 	  GTK_STATE_SELECTED)
3864 	fake_toggle_row (clist, 0);
3865 
3866       clist->anchor_state =  GTK_STATE_SELECTED;
3867       clist->anchor = 0;
3868       clist->drag_pos = 0;
3869       clist->undo_anchor = clist->focus_row;
3870       update_extended_selection (clist, clist->rows);
3871       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3872       return;
3873     default:
3874       g_assert_not_reached ();
3875     }
3876 }
3877 
3878 static void
real_unselect_all(GtkCMCList * clist)3879 real_unselect_all (GtkCMCList *clist)
3880 {
3881   GList *list;
3882   gint i;
3883 
3884   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3885 
3886   if (clist_has_grab (clist))
3887     return;
3888 
3889   switch (clist->selection_mode)
3890     {
3891     case GTK_SELECTION_BROWSE:
3892       if (clist->focus_row >= 0)
3893 	{
3894 	  g_signal_emit (G_OBJECT (clist),
3895 			   clist_signals[SELECT_ROW], 0,
3896 			   clist->focus_row, -1, NULL);
3897 	  return;
3898 	}
3899       break;
3900     case GTK_SELECTION_MULTIPLE:
3901       g_list_free (clist->undo_selection);
3902       g_list_free (clist->undo_unselection);
3903       clist->undo_selection = NULL;
3904       clist->undo_unselection = NULL;
3905 
3906       clist->anchor = -1;
3907       clist->drag_pos = -1;
3908       clist->undo_anchor = clist->focus_row;
3909       break;
3910     default:
3911       break;
3912     }
3913 
3914   list = clist->selection;
3915   while (list)
3916     {
3917       i = GPOINTER_TO_INT (list->data);
3918       list = list->next;
3919       g_signal_emit (G_OBJECT (clist),
3920 		       clist_signals[UNSELECT_ROW], 0, i, -1, NULL);
3921     }
3922 }
3923 
3924 static void
fake_unselect_all(GtkCMCList * clist,gint row)3925 fake_unselect_all (GtkCMCList *clist,
3926 		   gint      row)
3927 {
3928   GList *list;
3929   GList *work;
3930   gint i;
3931 
3932   if (row >= 0 && (work = ROW_ELEMENT (clist, row)))
3933     {
3934       if (GTK_CMCLIST_ROW (work)->state == GTK_STATE_NORMAL &&
3935 	  GTK_CMCLIST_ROW (work)->selectable)
3936 	{
3937 	  GTK_CMCLIST_ROW (work)->state = GTK_STATE_SELECTED;
3938 
3939 	  if (CLIST_UNFROZEN (clist) &&
3940 	      gtk_cmclist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
3941 	    GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, row,
3942 						  GTK_CMCLIST_ROW (work));
3943 	}
3944     }
3945 
3946   clist->undo_selection = clist->selection;
3947   clist->selection = NULL;
3948   clist->selection_end = NULL;
3949 
3950   for (list = clist->undo_selection; list; list = list->next)
3951     {
3952       if ((i = GPOINTER_TO_INT (list->data)) == row ||
3953 	  !(work = g_list_nth (clist->row_list, i)))
3954 	continue;
3955 
3956       GTK_CMCLIST_ROW (work)->state = GTK_STATE_NORMAL;
3957       if (CLIST_UNFROZEN (clist) &&
3958 	  gtk_cmclist_row_is_visible (clist, i) != GTK_VISIBILITY_NONE)
3959 	GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, NULL, i,
3960 					      GTK_CMCLIST_ROW (work));
3961     }
3962 }
3963 
3964 static void
real_undo_selection(GtkCMCList * clist)3965 real_undo_selection (GtkCMCList *clist)
3966 {
3967   GList *work;
3968 
3969   cm_return_if_fail (GTK_IS_CMCLIST (clist));
3970 
3971   if (clist_has_grab (clist) ||
3972       clist->selection_mode != GTK_SELECTION_MULTIPLE)
3973     return;
3974 
3975   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
3976 
3977   if (!(clist->undo_selection || clist->undo_unselection))
3978     {
3979       gtk_cmclist_unselect_all (clist);
3980       return;
3981     }
3982 
3983   for (work = clist->undo_selection; work; work = work->next)
3984     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
3985 		     GPOINTER_TO_INT (work->data), -1, NULL);
3986 
3987   for (work = clist->undo_unselection; work; work = work->next)
3988     {
3989       /* g_print ("unselect %d\n",GPOINTER_TO_INT (work->data)); */
3990       g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
3991 		       GPOINTER_TO_INT (work->data), -1, NULL);
3992     }
3993 
3994   if (gtk_widget_has_focus(GTK_WIDGET(clist)) && clist->focus_row != clist->undo_anchor)
3995     {
3996       gtk_cmclist_undraw_focus (GTK_WIDGET (clist));
3997       clist->focus_row = clist->undo_anchor;
3998       gtk_cmclist_draw_focus (GTK_WIDGET (clist));
3999     }
4000   else
4001     clist->focus_row = clist->undo_anchor;
4002 
4003   clist->undo_anchor = -1;
4004 
4005   g_list_free (clist->undo_selection);
4006   g_list_free (clist->undo_unselection);
4007   clist->undo_selection = NULL;
4008   clist->undo_unselection = NULL;
4009 
4010   if (ROW_TOP_YPIXEL (clist, clist->focus_row) + clist->row_height >
4011       clist->clist_window_height)
4012     gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4013   else if (ROW_TOP_YPIXEL (clist, clist->focus_row) < 0)
4014     gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4015 }
4016 
4017 static void
set_anchor(GtkCMCList * clist,gboolean add_mode,gint anchor,gint undo_anchor)4018 set_anchor (GtkCMCList *clist,
4019 	    gboolean  add_mode,
4020 	    gint      anchor,
4021 	    gint      undo_anchor)
4022 {
4023   cm_return_if_fail (GTK_IS_CMCLIST (clist));
4024 
4025   if (clist->selection_mode != GTK_SELECTION_MULTIPLE || clist->anchor >= 0)
4026     return;
4027 
4028   g_list_free (clist->undo_selection);
4029   g_list_free (clist->undo_unselection);
4030   clist->undo_selection = NULL;
4031   clist->undo_unselection = NULL;
4032 
4033   if (add_mode)
4034     fake_toggle_row (clist, anchor);
4035   else
4036     {
4037       GTK_CMCLIST_GET_CLASS (clist)->fake_unselect_all (clist, anchor);
4038       clist->anchor_state = GTK_STATE_SELECTED;
4039     }
4040 
4041   clist->anchor = anchor;
4042   clist->drag_pos = anchor;
4043   clist->undo_anchor = undo_anchor;
4044 }
4045 
4046 static void
resync_selection(GtkCMCList * clist,GdkEvent * event)4047 resync_selection (GtkCMCList *clist,
4048 		  GdkEvent *event)
4049 {
4050   gint i;
4051   gint e;
4052   gint row;
4053   GList *list;
4054   GtkCMCListRow *clist_row;
4055 
4056   if (clist->selection_mode != GTK_SELECTION_MULTIPLE)
4057     return;
4058 
4059   if (clist->anchor < 0 || clist->drag_pos < 0)
4060     return;
4061 
4062   gtk_cmclist_freeze (clist);
4063 
4064   i = MIN (clist->anchor, clist->drag_pos);
4065   e = MAX (clist->anchor, clist->drag_pos);
4066 
4067   if (clist->undo_selection)
4068     {
4069       list = clist->selection;
4070       clist->selection = clist->undo_selection;
4071       clist->selection_end = g_list_last (clist->selection);
4072       clist->undo_selection = list;
4073       list = clist->selection;
4074       while (list)
4075 	{
4076 	  row = GPOINTER_TO_INT (list->data);
4077 	  list = list->next;
4078 	  if (row < i || row > e)
4079 	    {
4080 	      clist_row = g_list_nth (clist->row_list, row)->data;
4081 	      if (clist_row->selectable)
4082 		{
4083 		  clist_row->state = GTK_STATE_SELECTED;
4084 		  g_signal_emit (G_OBJECT (clist),
4085 				   clist_signals[UNSELECT_ROW], 0,
4086 				   row, -1, event);
4087 		  clist->undo_selection = g_list_prepend
4088 		    (clist->undo_selection, GINT_TO_POINTER (row));
4089 		}
4090 	    }
4091 	}
4092     }
4093 
4094   if (clist->anchor < clist->drag_pos)
4095     {
4096       for (list = g_list_nth (clist->row_list, i); i <= e;
4097 	   i++, list = list->next)
4098 	if (GTK_CMCLIST_ROW (list)->selectable)
4099 	  {
4100 	    if (g_list_find (clist->selection, GINT_TO_POINTER(i)))
4101 	      {
4102 		if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_NORMAL)
4103 		  {
4104 		    GTK_CMCLIST_ROW (list)->state = GTK_STATE_SELECTED;
4105 		    g_signal_emit (G_OBJECT (clist),
4106 				     clist_signals[UNSELECT_ROW], 0,
4107 				     i, -1, event);
4108 		    clist->undo_selection =
4109 		      g_list_prepend (clist->undo_selection,
4110 				      GINT_TO_POINTER (i));
4111 		  }
4112 	      }
4113 	    else if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_SELECTED)
4114 	      {
4115 		GTK_CMCLIST_ROW (list)->state = GTK_STATE_NORMAL;
4116 		clist->undo_unselection =
4117 		  g_list_prepend (clist->undo_unselection,
4118 				  GINT_TO_POINTER (i));
4119 	      }
4120 	  }
4121     }
4122   else
4123     {
4124       for (list = g_list_nth (clist->row_list, e); i <= e;
4125 	   e--, list = list->prev)
4126 	if (GTK_CMCLIST_ROW (list)->selectable)
4127 	  {
4128 	    if (g_list_find (clist->selection, GINT_TO_POINTER(e)))
4129 	      {
4130 		if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_NORMAL)
4131 		  {
4132 		    GTK_CMCLIST_ROW (list)->state = GTK_STATE_SELECTED;
4133 		    g_signal_emit (G_OBJECT (clist),
4134 				     clist_signals[UNSELECT_ROW], 0,
4135 				     e, -1, event);
4136 		    clist->undo_selection =
4137 		      g_list_prepend (clist->undo_selection,
4138 				      GINT_TO_POINTER (e));
4139 		  }
4140 	      }
4141 	    else if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_SELECTED)
4142 	      {
4143 		GTK_CMCLIST_ROW (list)->state = GTK_STATE_NORMAL;
4144 		clist->undo_unselection =
4145 		  g_list_prepend (clist->undo_unselection,
4146 				  GINT_TO_POINTER (e));
4147 	      }
4148 	  }
4149     }
4150 
4151   clist->undo_unselection = g_list_reverse (clist->undo_unselection);
4152   for (list = clist->undo_unselection; list; list = list->next)
4153     g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
4154 		     GPOINTER_TO_INT (list->data), -1, event);
4155 
4156   clist->anchor = -1;
4157   clist->drag_pos = -1;
4158 
4159   gtk_cmclist_thaw (clist);
4160 }
4161 
4162 static void
update_extended_selection(GtkCMCList * clist,gint row)4163 update_extended_selection (GtkCMCList *clist,
4164 			   gint      row)
4165 {
4166   gint i;
4167   GList *list;
4168   GdkRectangle area;
4169   gint s1 = -1;
4170   gint s2 = -1;
4171   gint e1 = -1;
4172   gint e2 = -1;
4173   gint y1 = clist->clist_window_height;
4174   gint y2 = clist->clist_window_height;
4175   gint h1 = 0;
4176   gint h2 = 0;
4177   gint top;
4178 
4179   if (clist->selection_mode != GTK_SELECTION_MULTIPLE || clist->anchor == -1)
4180     return;
4181 
4182   if (row < 0)
4183     row = 0;
4184   if (row >= clist->rows)
4185     row = clist->rows - 1;
4186 
4187   /* extending downwards */
4188   if (row > clist->drag_pos && clist->anchor <= clist->drag_pos)
4189     {
4190       s2 = clist->drag_pos + 1;
4191       e2 = row;
4192     }
4193   /* extending upwards */
4194   else if (row < clist->drag_pos && clist->anchor >= clist->drag_pos)
4195     {
4196       s2 = row;
4197       e2 = clist->drag_pos - 1;
4198     }
4199   else if (row < clist->drag_pos && clist->anchor < clist->drag_pos)
4200     {
4201       e1 = clist->drag_pos;
4202       /* row and drag_pos on different sides of anchor :
4203 	 take back the selection between anchor and drag_pos,
4204          select between anchor and row */
4205       if (row < clist->anchor)
4206 	{
4207 	  s1 = clist->anchor + 1;
4208 	  s2 = row;
4209 	  e2 = clist->anchor - 1;
4210 	}
4211       /* take back the selection between anchor and drag_pos */
4212       else
4213 	s1 = row + 1;
4214     }
4215   else if (row > clist->drag_pos && clist->anchor > clist->drag_pos)
4216     {
4217       s1 = clist->drag_pos;
4218       /* row and drag_pos on different sides of anchor :
4219 	 take back the selection between anchor and drag_pos,
4220          select between anchor and row */
4221       if (row > clist->anchor)
4222 	{
4223 	  e1 = clist->anchor - 1;
4224 	  s2 = clist->anchor + 1;
4225 	  e2 = row;
4226 	}
4227       /* take back the selection between anchor and drag_pos */
4228       else
4229 	e1 = row - 1;
4230     }
4231 
4232   clist->drag_pos = row;
4233 
4234   area.x = 0;
4235   area.width = clist->clist_window_width;
4236 
4237   /* restore the elements between s1 and e1 */
4238   if (s1 >= 0)
4239     {
4240       for (i = s1, list = g_list_nth (clist->row_list, i); i <= e1;
4241 	   i++, list = list->next)
4242 	if (GTK_CMCLIST_ROW (list)->selectable)
4243 	  {
4244 	    if (GTK_CMCLIST_GET_CLASS (clist)->selection_find (clist, i, list))
4245 	      GTK_CMCLIST_ROW (list)->state = GTK_STATE_SELECTED;
4246 	    else
4247 	      GTK_CMCLIST_ROW (list)->state = GTK_STATE_NORMAL;
4248 	  }
4249 
4250       top = ROW_TOP_YPIXEL (clist, clist->focus_row);
4251 
4252       if (top + clist->row_height <= 0)
4253 	{
4254 	  area.y = 0;
4255 	  area.height = ROW_TOP_YPIXEL (clist, e1) + clist->row_height;
4256 	  draw_rows (clist, &area);
4257 	  gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4258 	}
4259       else if (top >= clist->clist_window_height)
4260 	{
4261 	  area.y = ROW_TOP_YPIXEL (clist, s1) - 1;
4262 	  area.height = clist->clist_window_height - area.y;
4263 	  draw_rows (clist, &area);
4264 	  gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4265 	}
4266       else if (top < 0)
4267 	gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4268       else if (top + clist->row_height > clist->clist_window_height)
4269 	gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4270 
4271       y1 = ROW_TOP_YPIXEL (clist, s1) - 1;
4272       h1 = (e1 - s1 + 1) * (clist->row_height + CELL_SPACING);
4273     }
4274 
4275   /* extend the selection between s2 and e2 */
4276   if (s2 >= 0)
4277     {
4278       for (i = s2, list = g_list_nth (clist->row_list, i); i <= e2;
4279 	   i++, list = list->next)
4280 	if (GTK_CMCLIST_ROW (list)->selectable &&
4281 	    GTK_CMCLIST_ROW (list)->state != clist->anchor_state)
4282 	  GTK_CMCLIST_ROW (list)->state = clist->anchor_state;
4283 
4284       top = ROW_TOP_YPIXEL (clist, clist->focus_row);
4285 
4286       if (top + clist->row_height <= 0)
4287 	{
4288 	  area.y = 0;
4289 	  area.height = ROW_TOP_YPIXEL (clist, e2) + clist->row_height;
4290 	  draw_rows (clist, &area);
4291 	  gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4292 	}
4293       else if (top >= clist->clist_window_height)
4294 	{
4295 	  area.y = ROW_TOP_YPIXEL (clist, s2) - 1;
4296 	  area.height = clist->clist_window_height - area.y;
4297 	  draw_rows (clist, &area);
4298 	  gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4299 	}
4300       else if (top < 0)
4301 	gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4302       else if (top + clist->row_height > clist->clist_window_height)
4303 	gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4304 
4305       y2 = ROW_TOP_YPIXEL (clist, s2) - 1;
4306       h2 = (e2 - s2 + 1) * (clist->row_height + CELL_SPACING);
4307     }
4308 
4309   area.y = MAX (0, MIN (y1, y2));
4310   if (area.y > clist->clist_window_height)
4311     area.y = 0;
4312   area.height = MIN (clist->clist_window_height, h1 + h2);
4313   if (s1 >= 0 && s2 >= 0)
4314     area.height += (clist->row_height + CELL_SPACING);
4315   draw_rows (clist, &area);
4316 }
4317 
4318 static void
start_selection(GtkCMCList * clist)4319 start_selection (GtkCMCList *clist)
4320 {
4321   cm_return_if_fail (GTK_IS_CMCLIST (clist));
4322 
4323   if (clist_has_grab (clist))
4324     return;
4325 
4326   set_anchor (clist, GTK_CMCLIST_ADD_MODE(clist), clist->focus_row,
4327 	      clist->focus_row);
4328 }
4329 
4330 static void
end_selection(GtkCMCList * clist)4331 end_selection (GtkCMCList *clist)
4332 {
4333   cm_return_if_fail (GTK_IS_CMCLIST (clist));
4334 
4335   if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (clist))) &&
4336       gtk_widget_has_focus (GTK_WIDGET(clist)))
4337     return;
4338 
4339   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
4340 }
4341 
4342 static void
extend_selection(GtkCMCList * clist,GtkScrollType scroll_type,gfloat position,gboolean auto_start_selection)4343 extend_selection (GtkCMCList      *clist,
4344 		  GtkScrollType  scroll_type,
4345 		  gfloat         position,
4346 		  gboolean       auto_start_selection)
4347 {
4348   cm_return_if_fail (GTK_IS_CMCLIST (clist));
4349 
4350   if (clist_has_grab (clist) ||
4351       clist->selection_mode != GTK_SELECTION_MULTIPLE)
4352     return;
4353 
4354   if (auto_start_selection)
4355     set_anchor (clist, GTK_CMCLIST_ADD_MODE(clist), clist->focus_row,
4356 		clist->focus_row);
4357   else if (clist->anchor == -1)
4358     return;
4359 
4360   move_focus_row (clist, scroll_type, position);
4361 
4362   if (ROW_TOP_YPIXEL (clist, clist->focus_row) + clist->row_height >
4363       clist->clist_window_height)
4364     gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
4365   else if (ROW_TOP_YPIXEL (clist, clist->focus_row) < 0)
4366     gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
4367 
4368   update_extended_selection (clist, clist->focus_row);
4369 }
4370 
4371 static void
sync_selection(GtkCMCList * clist,gint row,gint mode)4372 sync_selection (GtkCMCList *clist,
4373 		gint      row,
4374 		gint      mode)
4375 {
4376   GList *list;
4377   gint d;
4378 
4379   if (mode == SYNC_INSERT)
4380     d = 1;
4381   else
4382     d = -1;
4383 
4384   if (clist->focus_row >= row)
4385     {
4386       if (d > 0 || clist->focus_row > row)
4387 	clist->focus_row += d;
4388       if (clist->focus_row == -1 && clist->rows >= 1)
4389 	clist->focus_row = 0;
4390       else if (d < 0 && clist->focus_row >= clist->rows - 1)
4391 	clist->focus_row = clist->rows - 2;
4392       else if (clist->focus_row >= clist->rows)	/* Paranoia */
4393 	clist->focus_row = clist->rows - 1;
4394     }
4395 
4396   GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
4397 
4398   g_list_free (clist->undo_selection);
4399   g_list_free (clist->undo_unselection);
4400   clist->undo_selection = NULL;
4401   clist->undo_unselection = NULL;
4402 
4403   clist->anchor = -1;
4404   clist->drag_pos = -1;
4405   clist->undo_anchor = clist->focus_row;
4406 
4407   list = clist->selection;
4408 
4409   while (list)
4410     {
4411       if (GPOINTER_TO_INT (list->data) >= row)
4412 	list->data = ((gchar*) list->data) + d;
4413       list = list->next;
4414     }
4415 }
4416 
4417 /* GTKOBJECT
4418  *   gtk_cmclist_destroy
4419  *   gtk_cmclist_finalize
4420  */
4421 static void
gtk_cmclist_destroy(GtkObject * object)4422 gtk_cmclist_destroy (GtkObject *object)
4423 {
4424   gint i;
4425   GtkCMCList *clist;
4426 
4427   cm_return_if_fail (GTK_IS_CMCLIST (object));
4428 
4429   clist = GTK_CMCLIST (object);
4430 
4431   /* freeze the list */
4432   clist->freeze_count++;
4433 
4434   /* get rid of all the rows */
4435   gtk_cmclist_clear (clist);
4436 
4437   /* Since we don't have a _remove method, unparent the children
4438    * instead of destroying them so the focus will be unset properly.
4439    * (For other containers, the _remove method takes care of the
4440    * unparent) The destroy will happen when the refcount drops
4441    * to zero.
4442    */
4443 
4444   /* unref adjustments */
4445   if (clist->hadjustment)
4446     {
4447       g_signal_handlers_disconnect_matched(G_OBJECT (clist->hadjustment), G_SIGNAL_MATCH_DATA,
4448 		      	0, 0, 0, 0, clist);
4449       g_object_unref (G_OBJECT (clist->hadjustment));
4450       clist->hadjustment = NULL;
4451     }
4452   if (clist->vadjustment)
4453     {
4454       g_signal_handlers_disconnect_matched(G_OBJECT (clist->vadjustment), G_SIGNAL_MATCH_DATA,
4455 		      	0, 0, 0, 0, clist);
4456       g_object_unref (G_OBJECT (clist->vadjustment));
4457       clist->vadjustment = NULL;
4458     }
4459 
4460   remove_grab (clist);
4461 
4462   /* destroy the column buttons */
4463   for (i = 0; i < clist->columns; i++)
4464     if (clist->column[i].button)
4465       {
4466 	gtk_widget_unparent (clist->column[i].button);
4467 	clist->column[i].button = NULL;
4468       }
4469 
4470   if (GTK_OBJECT_CLASS (gtk_cmclist_parent_class)->destroy)
4471     (*GTK_OBJECT_CLASS (gtk_cmclist_parent_class)->destroy) (object);
4472 }
4473 
4474 static void
gtk_cmclist_finalize(GObject * object)4475 gtk_cmclist_finalize (GObject *object)
4476 {
4477   GtkCMCList *clist;
4478 
4479   cm_return_if_fail (GTK_IS_CMCLIST (object));
4480 
4481   clist = GTK_CMCLIST (object);
4482 
4483   columns_delete (clist);
4484 
4485   G_OBJECT_CLASS (gtk_cmclist_parent_class)->finalize (object);
4486 }
4487 
4488 /* GTKWIDGET
4489  *   gtk_cmclist_realize
4490  *   gtk_cmclist_unrealize
4491  *   gtk_cmclist_map
4492  *   gtk_cmclist_unmap
4493  *   gtk_cmclist_expose
4494  *   gtk_cmclist_style_set
4495  *   gtk_cmclist_button_press
4496  *   gtk_cmclist_button_release
4497  *   gtk_cmclist_motion
4498  *   gtk_cmclist_size_request
4499  *   gtk_cmclist_size_allocate
4500  */
4501 static void
gtk_cmclist_realize(GtkWidget * widget)4502 gtk_cmclist_realize (GtkWidget *widget)
4503 {
4504   GtkAllocation allocation;
4505   GtkCMCList *clist;
4506   GtkStyle *style, *attached_style;
4507   GdkWindow *window;
4508   GdkWindowAttr attributes;
4509   GtkCMCListRow *clist_row;
4510   GList *list;
4511   gint attributes_mask;
4512   gint border_width;
4513   gint i;
4514   gint j;
4515 
4516   cm_return_if_fail (GTK_IS_CMCLIST (widget));
4517 
4518   clist = GTK_CMCLIST (widget);
4519 
4520   gtk_widget_set_realized (widget, TRUE);
4521 
4522   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
4523 
4524   gtk_widget_get_allocation (widget, &allocation);
4525 
4526   attributes.window_type = GDK_WINDOW_CHILD;
4527   attributes.x = allocation.x + border_width;
4528   attributes.y = allocation.y + border_width;
4529   attributes.width = allocation.width - border_width * 2;
4530   attributes.height = allocation.height - border_width * 2;
4531   attributes.wclass = GDK_INPUT_OUTPUT;
4532   attributes.visual = gtk_widget_get_visual (widget);
4533   attributes.colormap = gtk_widget_get_colormap (widget);
4534   attributes.event_mask = gtk_widget_get_events (widget);
4535   attributes.event_mask |= (GDK_EXPOSURE_MASK |
4536 			    GDK_BUTTON_PRESS_MASK |
4537 			    GDK_BUTTON_RELEASE_MASK |
4538 			    GDK_KEY_RELEASE_MASK);
4539   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
4540 
4541   /* main window */
4542   window = gdk_window_new (gtk_widget_get_parent_window (widget),
4543 				   &attributes, attributes_mask);
4544   gdk_window_set_user_data (window, clist);
4545   gtk_widget_set_window (widget, window);
4546 
4547   style = gtk_widget_get_style (widget);
4548   attached_style = gtk_style_attach (style, window);
4549   if (attached_style != style) {
4550 	gtk_widget_set_style(widget, attached_style);
4551 	style = attached_style;
4552   }
4553 
4554   gtk_style_set_background (style, window, GTK_STATE_NORMAL);
4555 
4556   /* column-title window */
4557 
4558   attributes.x = clist->column_title_area.x;
4559   attributes.y = clist->column_title_area.y;
4560   attributes.width = clist->column_title_area.width;
4561   attributes.height = clist->column_title_area.height;
4562 
4563   clist->title_window = gdk_window_new (window, &attributes,
4564 					attributes_mask);
4565   gdk_window_set_user_data (clist->title_window, clist);
4566 
4567   gtk_style_set_background (style, clist->title_window,
4568 			    GTK_STATE_NORMAL);
4569   gdk_window_show (clist->title_window);
4570 
4571   /* set things up so column buttons are drawn in title window */
4572   for (i = 0; i < clist->columns; i++)
4573     if (clist->column[i].button)
4574       gtk_widget_set_parent_window (clist->column[i].button,
4575 				    clist->title_window);
4576 
4577   /* clist-window */
4578   attributes.x = (clist->internal_allocation.x +
4579 		  style->xthickness);
4580   attributes.y = (clist->internal_allocation.y +
4581 		  style->ythickness +
4582 		  clist->column_title_area.height);
4583   attributes.width = clist->clist_window_width;
4584   attributes.height = clist->clist_window_height;
4585 
4586   clist->clist_window = gdk_window_new (window, &attributes,
4587 					attributes_mask);
4588   gdk_window_set_user_data (clist->clist_window, clist);
4589 
4590   gdk_window_set_background (clist->clist_window,
4591 			     &style->base[GTK_STATE_NORMAL]);
4592   gdk_window_show (clist->clist_window);
4593   clist->clist_window_width = gdk_window_get_width(clist->clist_window);
4594   clist->clist_window_height = gdk_window_get_height(clist->clist_window);
4595 
4596   /* create resize windows */
4597   attributes.wclass = GDK_INPUT_ONLY;
4598   attributes.event_mask = (GDK_BUTTON_PRESS_MASK |
4599 			   GDK_BUTTON_RELEASE_MASK |
4600 			   GDK_POINTER_MOTION_MASK |
4601 			   GDK_POINTER_MOTION_HINT_MASK);
4602   attributes_mask = GDK_WA_CURSOR;
4603   attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
4604 						  GDK_SB_H_DOUBLE_ARROW);
4605   clist->cursor_drag = attributes.cursor;
4606 
4607   attributes.x =  LIST_WIDTH (clist) + 1;
4608   attributes.y = 0;
4609   attributes.width = 0;
4610   attributes.height = 0;
4611 
4612   for (i = 0; i < clist->columns; i++)
4613     {
4614       clist->column[i].window = gdk_window_new (clist->title_window,
4615 						&attributes, attributes_mask);
4616       gdk_window_set_user_data (clist->column[i].window, clist);
4617     }
4618 
4619   /* This is slightly less efficient than creating them with the
4620    * right size to begin with, but easier
4621    */
4622   size_allocate_title_buttons (clist);
4623 
4624   /* attach optional row/cell styles, allocate foreground/background colors */
4625   list = clist->row_list;
4626   for (i = 0; i < clist->rows; i++)
4627     {
4628       clist_row = list->data;
4629       list = list->next;
4630 
4631       if (clist_row->style)
4632 	clist_row->style = gtk_style_attach (clist_row->style,
4633 					     clist->clist_window);
4634 
4635       if (clist_row->fg_set || clist_row->bg_set)
4636 	{
4637 	  GdkColormap *colormap;
4638 
4639 	  colormap = gtk_widget_get_colormap (widget);
4640 	  if (clist_row->fg_set)
4641 	    gdk_colormap_alloc_color (colormap, &clist_row->foreground, TRUE, TRUE);
4642 	  if (clist_row->bg_set)
4643 	    gdk_colormap_alloc_color (colormap, &clist_row->background, TRUE, TRUE);
4644 	}
4645 
4646       for (j = 0; j < clist->columns; j++)
4647 	if  (clist_row->cell[j].style)
4648 	  clist_row->cell[j].style =
4649 	    gtk_style_attach (clist_row->cell[j].style, clist->clist_window);
4650     }
4651 }
4652 
4653 static void
gtk_cmclist_unrealize(GtkWidget * widget)4654 gtk_cmclist_unrealize (GtkWidget *widget)
4655 {
4656   gint i;
4657   GtkCMCList *clist;
4658 
4659   cm_return_if_fail (GTK_IS_CMCLIST (widget));
4660 
4661   clist = GTK_CMCLIST (widget);
4662 
4663   /* freeze the list */
4664   clist->freeze_count++;
4665 
4666   if (gtk_widget_get_mapped (widget))
4667     gtk_cmclist_unmap (widget);
4668 
4669   gtk_widget_set_mapped (widget, FALSE);
4670 
4671   /* detach optional row/cell styles */
4672   if (gtk_widget_get_realized (widget))
4673     {
4674       GtkCMCListRow *clist_row;
4675       GList *list;
4676       gint j;
4677 
4678       list = clist->row_list;
4679       for (i = 0; i < clist->rows; i++)
4680 	{
4681 	  clist_row = list->data;
4682 	  list = list->next;
4683 
4684 	  if (clist_row->style)
4685 	    gtk_style_detach (clist_row->style);
4686 	  for (j = 0; j < clist->columns; j++)
4687 	    if  (clist_row->cell[j].style)
4688 	      gtk_style_detach (clist_row->cell[j].style);
4689 	}
4690     }
4691 
4692   gdk_cursor_unref (clist->cursor_drag);
4693 
4694   for (i = 0; i < clist->columns; i++)
4695     {
4696       if (clist->column[i].button)
4697 	gtk_widget_unrealize (clist->column[i].button);
4698       if (clist->column[i].window)
4699 	{
4700 	  gdk_window_set_user_data (clist->column[i].window, NULL);
4701 	  gdk_window_destroy (clist->column[i].window);
4702 	  clist->column[i].window = NULL;
4703 	}
4704     }
4705 
4706   gdk_window_set_user_data (clist->clist_window, NULL);
4707   gdk_window_destroy (clist->clist_window);
4708   clist->clist_window = NULL;
4709 
4710   gdk_window_set_user_data (clist->title_window, NULL);
4711   gdk_window_destroy (clist->title_window);
4712   clist->title_window = NULL;
4713 
4714   clist->cursor_drag = NULL;
4715 
4716   if (GTK_WIDGET_CLASS (gtk_cmclist_parent_class)->unrealize)
4717     (* GTK_WIDGET_CLASS (gtk_cmclist_parent_class)->unrealize) (widget);
4718 }
4719 
4720 static void
gtk_cmclist_map(GtkWidget * widget)4721 gtk_cmclist_map (GtkWidget *widget)
4722 {
4723   gint i;
4724   GtkCMCList *clist;
4725 
4726   cm_return_if_fail (GTK_IS_CMCLIST (widget));
4727 
4728   clist = GTK_CMCLIST (widget);
4729 
4730   if (!gtk_widget_get_mapped (widget))
4731     {
4732       gtk_widget_set_mapped (widget, TRUE);
4733 
4734       /* map column buttons */
4735       for (i = 0; i < clist->columns; i++)
4736 	{
4737 	  if (clist->column[i].button &&
4738 	      gtk_widget_get_visible (clist->column[i].button) &&
4739 	      !gtk_widget_get_mapped (clist->column[i].button))
4740 	    gtk_widget_map (clist->column[i].button);
4741 	}
4742 
4743       for (i = 0; i < clist->columns; i++)
4744 	if (clist->column[i].window && clist->column[i].button)
4745 	  {
4746 	    gdk_window_raise (clist->column[i].window);
4747 	    gdk_window_show (clist->column[i].window);
4748 	  }
4749 
4750       gdk_window_show (clist->title_window);
4751       gdk_window_show (clist->clist_window);
4752       gdk_window_show (gtk_widget_get_window (widget));
4753 
4754       /* unfreeze the list */
4755       clist->freeze_count = 0;
4756     }
4757 }
4758 
4759 static void
gtk_cmclist_unmap(GtkWidget * widget)4760 gtk_cmclist_unmap (GtkWidget *widget)
4761 {
4762   gint i;
4763   GtkCMCList *clist;
4764 
4765   cm_return_if_fail (GTK_IS_CMCLIST (widget));
4766 
4767   clist = GTK_CMCLIST (widget);
4768 
4769   if (gtk_widget_get_mapped (widget))
4770     {
4771       gtk_widget_set_mapped (widget, FALSE);
4772 
4773       if (clist_has_grab (clist))
4774 	{
4775 	  remove_grab (clist);
4776 
4777 	  GTK_CMCLIST_GET_CLASS (widget)->resync_selection (clist, NULL);
4778 
4779 	  clist->click_cell.row = -1;
4780 	  clist->click_cell.column = -1;
4781 	  clist->drag_button = 0;
4782 
4783 	  if (GTK_CMCLIST_IN_DRAG(clist))
4784 	    {
4785 	      gpointer drag_data;
4786 
4787 	      GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_IN_DRAG);
4788 	      drag_data = g_object_get_data (G_OBJECT (clist),
4789 					       "gtk-site-data");
4790 	      if (drag_data)
4791 	      	g_signal_handlers_unblock_matched(G_OBJECT(clist), G_SIGNAL_MATCH_DATA,
4792 					0, 0, 0, 0, drag_data);
4793 	    }
4794 	}
4795 
4796       for (i = 0; i < clist->columns; i++)
4797 	if (clist->column[i].window)
4798 	  gdk_window_hide (clist->column[i].window);
4799 
4800       gdk_window_hide (clist->clist_window);
4801       gdk_window_hide (clist->title_window);
4802       gdk_window_hide (gtk_widget_get_window (widget));
4803 
4804       /* unmap column buttons */
4805       for (i = 0; i < clist->columns; i++)
4806 	if (clist->column[i].button &&
4807 	    gtk_widget_get_mapped (clist->column[i].button))
4808 	  gtk_widget_unmap (clist->column[i].button);
4809 
4810       /* freeze the list */
4811       clist->freeze_count++;
4812     }
4813 }
4814 
4815 static gint
gtk_cmclist_expose(GtkWidget * widget,GdkEventExpose * event)4816 gtk_cmclist_expose (GtkWidget      *widget,
4817 		  GdkEventExpose *event)
4818 {
4819   GtkCMCList *clist;
4820 
4821   cm_return_val_if_fail (GTK_IS_CMCLIST (widget), FALSE);
4822   cm_return_val_if_fail (event != NULL, FALSE);
4823 
4824   if (gtk_widget_is_drawable (widget))
4825     {
4826       clist = GTK_CMCLIST (widget);
4827 
4828       /* exposure events on the list */
4829       if (event->window == clist->clist_window)
4830 	draw_rows (clist, &event->area);
4831 
4832       if (event->window == clist->title_window)
4833 	{
4834 	  gint i;
4835 
4836 	  for (i = 0; i < clist->columns; i++)
4837 	    {
4838 	      if (clist->column[i].button) {
4839 		gtk_container_propagate_expose (GTK_CONTAINER (clist),
4840 						clist->column[i].button,
4841 						event);
4842 	      }
4843 	    }
4844 	}
4845     }
4846 
4847   return FALSE;
4848 }
4849 
4850 static void
gtk_cmclist_style_set(GtkWidget * widget,GtkStyle * previous_style)4851 gtk_cmclist_style_set (GtkWidget *widget,
4852 		     GtkStyle  *previous_style)
4853 {
4854   GtkCMCList *clist;
4855   GtkStyle *style;
4856 
4857   cm_return_if_fail (GTK_IS_CMCLIST (widget));
4858 
4859   if (GTK_WIDGET_CLASS (gtk_cmclist_parent_class)->style_set)
4860     (*GTK_WIDGET_CLASS (gtk_cmclist_parent_class)->style_set) (widget, previous_style);
4861 
4862   clist = GTK_CMCLIST (widget);
4863 
4864   if (gtk_widget_get_realized (widget))
4865     {
4866       style = gtk_widget_get_style (widget);
4867       gtk_style_set_background (style, gtk_widget_get_window (widget),
4868 				gtk_widget_get_state(widget));
4869       gtk_style_set_background (style, clist->title_window, GTK_STATE_NORMAL);
4870       gdk_window_set_background (clist->clist_window, &style->base[GTK_STATE_NORMAL]);
4871     }
4872 
4873   /* Fill in data after widget has correct style */
4874 
4875   /* text properties */
4876   if (!GTK_CMCLIST_ROW_HEIGHT_SET(clist))
4877     /* Reset clist->row_height */
4878     gtk_cmclist_set_row_height (clist, 0);
4879 
4880   /* Column widths */
4881   if (!GTK_CMCLIST_AUTO_RESIZE_BLOCKED (clist))
4882     {
4883       gint width;
4884       gint i;
4885 
4886       for (i = 0; i < clist->columns; i++)
4887 	if (clist->column[i].auto_resize)
4888 	  {
4889 	    width = gtk_cmclist_optimal_column_width (clist, i);
4890 	    if (width != clist->column[i].width)
4891 	      gtk_cmclist_set_column_width (clist, i, width);
4892 	  }
4893     }
4894 }
4895 
4896 static gint
gtk_cmclist_button_press(GtkWidget * widget,GdkEventButton * event)4897 gtk_cmclist_button_press (GtkWidget      *widget,
4898 			GdkEventButton *event)
4899 {
4900   gint i;
4901   GtkCMCList *clist;
4902   gint x;
4903   gint y;
4904   gint row;
4905   gint column;
4906   gint button_actions;
4907 
4908   cm_return_val_if_fail (GTK_IS_CMCLIST (widget), FALSE);
4909   cm_return_val_if_fail (event != NULL, FALSE);
4910 
4911   clist = GTK_CMCLIST (widget);
4912 
4913   button_actions = clist->button_actions[event->button - 1];
4914 
4915   if (button_actions == GTK_CMBUTTON_IGNORED)
4916     return FALSE;
4917 
4918   /* selections on the list */
4919   if (event->window == clist->clist_window)
4920     {
4921       x = event->x;
4922       y = event->y;
4923 
4924       if (get_selection_info (clist, x, y, &row, &column))
4925 	{
4926 	  gint old_row = clist->focus_row;
4927 
4928 	  if (clist->focus_row == -1)
4929 	    old_row = row;
4930 
4931 	  if (event->type == GDK_BUTTON_PRESS)
4932 	    {
4933 	      GdkEventMask mask = ((1 << (4 + event->button)) |
4934 				   GDK_POINTER_MOTION_HINT_MASK |
4935 				   GDK_BUTTON_RELEASE_MASK);
4936 
4937 	      if (gdk_pointer_grab (clist->clist_window, FALSE, mask,
4938 				    NULL, NULL, event->time))
4939 		return FALSE;
4940 	      gtk_grab_add (widget);
4941 
4942 	      clist->click_cell.row = row;
4943 	      clist->click_cell.column = column;
4944 	      clist->drag_button = event->button;
4945 	    }
4946 	  else
4947 	    {
4948 	      clist->click_cell.row = -1;
4949 	      clist->click_cell.column = -1;
4950 
4951 	      clist->drag_button = 0;
4952 	      remove_grab (clist);
4953 	    }
4954 
4955 	  if (button_actions & GTK_CMBUTTON_SELECTS)
4956 	    {
4957 	      if (GTK_CMCLIST_ADD_MODE(clist))
4958 		{
4959 		  GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_ADD_MODE);
4960 		  if (gtk_widget_has_focus(widget))
4961 		    {
4962 		      gtk_cmclist_undraw_focus (widget);
4963 		      clist->focus_row = row;
4964 		      gtk_cmclist_draw_focus (widget);
4965 		    }
4966 		  else
4967 		    {
4968 		      clist->focus_row = row;
4969 		    }
4970 		}
4971 	      else if (row != clist->focus_row)
4972 		{
4973 		  if (gtk_widget_has_focus(widget))
4974 		    {
4975 		      gtk_cmclist_undraw_focus (widget);
4976 		      clist->focus_row = row;
4977 		      gtk_cmclist_draw_focus (widget);
4978 		    }
4979 		  else
4980 		    clist->focus_row = row;
4981 		}
4982 	    }
4983 
4984 	  if (!gtk_widget_has_focus(widget))
4985 	    gtk_widget_grab_focus (widget);
4986 
4987 	  if (button_actions & GTK_CMBUTTON_SELECTS)
4988 	    {
4989 	      switch (clist->selection_mode)
4990 		{
4991 		case GTK_SELECTION_SINGLE:
4992 		  if (event->type != GDK_BUTTON_PRESS)
4993 		    {
4994 		      g_signal_emit (G_OBJECT (clist),
4995 				       clist_signals[SELECT_ROW], 0,
4996 				       row, column, event);
4997 		      clist->anchor = -1;
4998 		    }
4999 		  else
5000 		    clist->anchor = row;
5001 		  break;
5002 		case GTK_SELECTION_BROWSE:
5003 		  g_signal_emit (G_OBJECT (clist),
5004 				   clist_signals[SELECT_ROW], 0,
5005 				   row, column, event);
5006 		  break;
5007 		case GTK_SELECTION_MULTIPLE:
5008 		  if (event->type != GDK_BUTTON_PRESS)
5009 		    {
5010 		      if (clist->anchor != -1)
5011 			{
5012 			  update_extended_selection (clist, clist->focus_row);
5013 			  GTK_CMCLIST_GET_CLASS (clist)->resync_selection
5014 			    (clist, (GdkEvent *) event);
5015 			}
5016 		      g_signal_emit (G_OBJECT (clist),
5017 				       clist_signals[SELECT_ROW], 0,
5018 				       row, column, event);
5019 		      break;
5020 		    }
5021 
5022 		  if (event->state & GDK_CONTROL_MASK)
5023 		    {
5024 		      if (event->state & GDK_SHIFT_MASK)
5025 			{
5026 			  if (clist->anchor < 0)
5027 			    {
5028 			      g_list_free (clist->undo_selection);
5029 			      g_list_free (clist->undo_unselection);
5030 			      clist->undo_selection = NULL;
5031 			      clist->undo_unselection = NULL;
5032 			      clist->anchor = old_row;
5033 			      clist->drag_pos = old_row;
5034 			      clist->undo_anchor = old_row;
5035 			    }
5036 			  update_extended_selection (clist, clist->focus_row);
5037 			}
5038 		      else
5039 			{
5040 			  if (clist->anchor == -1)
5041 			    set_anchor (clist, TRUE, row, old_row);
5042 			  else
5043 			    update_extended_selection (clist,
5044 						       clist->focus_row);
5045 			}
5046 		      break;
5047 		    }
5048 
5049 		  if (event->state & GDK_SHIFT_MASK)
5050 		    {
5051 		      set_anchor (clist, FALSE, old_row, old_row);
5052 		      update_extended_selection (clist, clist->focus_row);
5053 		      break;
5054 		    }
5055 
5056 		  if (clist->anchor == -1)
5057 		    set_anchor (clist, FALSE, row, old_row);
5058 		  else
5059 		    update_extended_selection (clist, clist->focus_row);
5060 		  break;
5061 		default:
5062 		  break;
5063 		}
5064 	    }
5065 	}
5066       return TRUE;
5067     }
5068 
5069   /* press on resize windows */
5070   for (i = 0; i < clist->columns; i++)
5071     if (clist->column[i].resizeable && clist->column[i].window &&
5072 	event->window == clist->column[i].window)
5073       {
5074 	gpointer drag_data;
5075 
5076 	if (gdk_pointer_grab (clist->column[i].window, FALSE,
5077 			      GDK_POINTER_MOTION_HINT_MASK |
5078 			      GDK_BUTTON1_MOTION_MASK |
5079 			      GDK_BUTTON_RELEASE_MASK,
5080 			      NULL, NULL, event->time))
5081 	  return FALSE;
5082 
5083 	gtk_grab_add (widget);
5084 	GTK_CMCLIST_SET_FLAG (clist, CMCLIST_IN_DRAG);
5085 
5086 	/* block attached dnd signal handler */
5087 	drag_data = g_object_get_data (G_OBJECT (clist), "gtk-site-data");
5088 	if (drag_data)
5089 	      	g_signal_handlers_block_matched(G_OBJECT(clist), G_SIGNAL_MATCH_DATA,
5090 					0, 0, 0, 0, drag_data);
5091 
5092 	if (!gtk_widget_has_focus(widget))
5093 	  gtk_widget_grab_focus (widget);
5094 
5095 	clist->drag_pos = i;
5096 	clist->x_drag = (COLUMN_LEFT_XPIXEL(clist, i) + COLUMN_INSET +
5097 			 clist->column[i].area.width + CELL_SPACING);
5098 
5099         return TRUE;
5100       }
5101 
5102   return FALSE;
5103 }
5104 
5105 static gint
gtk_cmclist_button_release(GtkWidget * widget,GdkEventButton * event)5106 gtk_cmclist_button_release (GtkWidget      *widget,
5107 			  GdkEventButton *event)
5108 {
5109   GtkCMCList *clist;
5110   gint button_actions;
5111 
5112   cm_return_val_if_fail (GTK_IS_CMCLIST (widget), FALSE);
5113   cm_return_val_if_fail (event != NULL, FALSE);
5114 
5115   clist = GTK_CMCLIST (widget);
5116 
5117   button_actions = clist->button_actions[event->button - 1];
5118   if (button_actions == GTK_CMBUTTON_IGNORED)
5119     return FALSE;
5120 
5121   /* release on resize windows */
5122   if (GTK_CMCLIST_IN_DRAG(clist))
5123     {
5124       gpointer drag_data;
5125       gint width;
5126       gint x;
5127       gint i;
5128 
5129       i = clist->drag_pos;
5130       clist->drag_pos = -1;
5131 
5132       /* unblock attached dnd signal handler */
5133       drag_data = g_object_get_data (G_OBJECT (clist), "gtk-site-data");
5134       if (drag_data)
5135 	      	g_signal_handlers_unblock_matched(G_OBJECT(clist), G_SIGNAL_MATCH_DATA,
5136 					0, 0, 0, 0, drag_data);
5137 
5138       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_IN_DRAG);
5139       gtk_widget_get_pointer (widget, &x, NULL);
5140       gtk_grab_remove (widget);
5141       gdk_display_pointer_ungrab (gtk_widget_get_display (widget), event->time);
5142 
5143       if (clist->x_drag >= 0)
5144 	clist_refresh(clist);
5145 
5146       width = new_column_width (clist, i, &x);
5147       gtk_cmclist_set_column_width (clist, i, width);
5148 
5149       return TRUE;
5150     }
5151 
5152   if (clist->drag_button == event->button)
5153     {
5154       gint row;
5155       gint column;
5156 
5157       clist->drag_button = 0;
5158       clist->click_cell.row = -1;
5159       clist->click_cell.column = -1;
5160 
5161       remove_grab (clist);
5162 
5163       if (button_actions & GTK_CMBUTTON_SELECTS)
5164 	{
5165 	  switch (clist->selection_mode)
5166 	    {
5167 	    case GTK_SELECTION_MULTIPLE:
5168 	      if (!(event->state & GDK_SHIFT_MASK) ||
5169 		  !gtk_widget_get_can_focus (widget) ||
5170 		  event->x < 0 || event->x >= clist->clist_window_width ||
5171 		  event->y < 0 || event->y >= clist->clist_window_height)
5172 		GTK_CMCLIST_GET_CLASS (clist)->resync_selection
5173 		  (clist, (GdkEvent *) event);
5174 	      break;
5175 	    case GTK_SELECTION_SINGLE:
5176 	      if (get_selection_info (clist, event->x, event->y,
5177 				      &row, &column))
5178 		{
5179 		  if (row >= 0 && row < clist->rows && clist->anchor == row)
5180 		    toggle_row (clist, row, column, (GdkEvent *) event);
5181 		}
5182 	      clist->anchor = -1;
5183 	      break;
5184 	    default:
5185 	      break;
5186 	    }
5187 	}
5188 
5189       return TRUE;
5190     }
5191 
5192   return FALSE;
5193 }
5194 
5195 static gint
gtk_cmclist_motion(GtkWidget * widget,GdkEventMotion * event)5196 gtk_cmclist_motion (GtkWidget      *widget,
5197 		  GdkEventMotion *event)
5198 {
5199   GtkCMCList *clist;
5200   gint x;
5201   gint y;
5202   gint row;
5203   gint new_width;
5204   gint button_actions = 0;
5205   guint value;
5206 
5207   cm_return_val_if_fail (GTK_IS_CMCLIST (widget), FALSE);
5208 
5209   clist = GTK_CMCLIST (widget);
5210   if (!clist_has_grab (clist))
5211     return FALSE;
5212 
5213   if (clist->drag_button > 0)
5214     button_actions = clist->button_actions[clist->drag_button - 1];
5215 
5216   if (GTK_CMCLIST_IN_DRAG(clist))
5217     {
5218       if (event->is_hint || event->window != gtk_widget_get_window (widget))
5219 	gtk_widget_get_pointer (widget, &x, NULL);
5220       else
5221 	x = event->x;
5222 
5223       new_width = new_column_width (clist, clist->drag_pos, &x);
5224       if (x != clist->x_drag)
5225 	{
5226 	  /* x_drag < 0 indicates that the xor line is already invisible */
5227 	  if (clist->x_drag >= 0)
5228 	    clist_refresh(clist);
5229 
5230 	  clist->x_drag = x;
5231 
5232 	  if (clist->x_drag >= 0)
5233 	    draw_xor_line (clist);
5234 	}
5235 
5236       if (new_width <= MAX (COLUMN_MIN_WIDTH + 1,
5237 			    clist->column[clist->drag_pos].min_width + 1))
5238 	{
5239 	  if (COLUMN_LEFT_XPIXEL (clist, clist->drag_pos) < 0 && x < 0)
5240 	    gtk_cmclist_moveto (clist, -1, clist->drag_pos, 0, 0);
5241 	  return FALSE;
5242 	}
5243       if (clist->column[clist->drag_pos].max_width >= COLUMN_MIN_WIDTH &&
5244 	  new_width >= clist->column[clist->drag_pos].max_width)
5245 	{
5246 	  if (COLUMN_LEFT_XPIXEL (clist, clist->drag_pos) + new_width >
5247 	      clist->clist_window_width && x < 0)
5248 	    move_horizontal (clist,
5249 			     COLUMN_LEFT_XPIXEL (clist, clist->drag_pos) +
5250 			     new_width - clist->clist_window_width +
5251 			     COLUMN_INSET + CELL_SPACING);
5252 	  return FALSE;
5253 	}
5254     }
5255 
5256   if (event->is_hint || event->window != clist->clist_window)
5257     gdk_window_get_pointer (clist->clist_window, &x, &y, NULL);
5258   else
5259     {
5260       x = event->x;
5261       y = event->y;
5262     }
5263 
5264   if (GTK_CMCLIST_REORDERABLE(clist) && button_actions & GTK_CMBUTTON_DRAGS)
5265     {
5266       /* delayed drag start */
5267       if (event->window == clist->clist_window &&
5268 	  clist->click_cell.row >= 0 && clist->click_cell.column >= 0 &&
5269 	  (y < 0 || y >= clist->clist_window_height ||
5270 	   x < 0 || x >= clist->clist_window_width  ||
5271 	   y < ROW_TOP_YPIXEL (clist, clist->click_cell.row) ||
5272 	   y >= (ROW_TOP_YPIXEL (clist, clist->click_cell.row) +
5273 		 clist->row_height) ||
5274 	   x < COLUMN_LEFT_XPIXEL (clist, clist->click_cell.column) ||
5275 	   x >= (COLUMN_LEFT_XPIXEL(clist, clist->click_cell.column) +
5276 		 clist->column[clist->click_cell.column].area.width)))
5277 	{
5278 	  GtkTargetList  *target_list;
5279 
5280 	  target_list = gtk_target_list_new (&clist_target_table, 1);
5281 	  gtk_drag_begin (widget, target_list, GDK_ACTION_MOVE,
5282 			  clist->drag_button, (GdkEvent *)event);
5283 
5284 	}
5285       return TRUE;
5286     }
5287 
5288   /* horizontal autoscrolling */
5289   if (clist->hadjustment && LIST_WIDTH (clist) > clist->clist_window_width &&
5290       (x < 0 || x >= clist->clist_window_width))
5291     {
5292       if (clist->htimer)
5293 	return FALSE;
5294 
5295       clist->htimer = gdk_threads_add_timeout
5296 	(SCROLL_TIME, (GSourceFunc) horizontal_timeout, clist);
5297       value = gtk_adjustment_get_value (clist->hadjustment);
5298       if (!((x < 0 && value == 0) ||
5299 	    (x >= clist->clist_window_width &&
5300 	     value ==
5301 	     LIST_WIDTH (clist) - clist->clist_window_width)))
5302 	{
5303 	  if (x < 0)
5304 	    move_horizontal (clist, -1 + (x/2));
5305 	  else
5306 	    move_horizontal (clist, 1 + (x - clist->clist_window_width) / 2);
5307 	}
5308     }
5309 
5310   if (GTK_CMCLIST_IN_DRAG(clist))
5311     return FALSE;
5312 
5313   /* vertical autoscrolling */
5314   row = ROW_FROM_YPIXEL (clist, y);
5315 
5316   /* don't scroll on last pixel row if it's a cell spacing */
5317   if (y == clist->clist_window_height - 1 &&
5318       y == ROW_TOP_YPIXEL (clist, row-1) + clist->row_height)
5319     return FALSE;
5320 
5321   if (LIST_HEIGHT (clist) > clist->clist_window_height &&
5322       (y < 0 || y >= clist->clist_window_height))
5323     {
5324       if (clist->vtimer)
5325 	return FALSE;
5326       clist->vtimer = gdk_threads_add_timeout (SCROLL_TIME,
5327 				     (GSourceFunc) vertical_timeout, clist);
5328       if (clist->drag_button &&
5329 	  ((y < 0 && clist->focus_row == 0) ||
5330 	   (y >= clist->clist_window_height &&
5331 	    clist->focus_row == clist->rows - 1)))
5332 	return FALSE;
5333     }
5334 
5335   row = CLAMP (row, 0, clist->rows - 1);
5336 
5337   if (button_actions & GTK_CMBUTTON_SELECTS &&
5338       !g_object_get_data (G_OBJECT (widget), "gtk-site-data"))
5339     {
5340       if (row == clist->focus_row)
5341 	return FALSE;
5342 
5343       gtk_cmclist_undraw_focus (widget);
5344       clist->focus_row = row;
5345       gtk_cmclist_draw_focus (widget);
5346 
5347       switch (clist->selection_mode)
5348 	{
5349 	case GTK_SELECTION_BROWSE:
5350 	  g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
5351 			   clist->focus_row, -1, event);
5352 	  break;
5353 	case GTK_SELECTION_MULTIPLE:
5354 	  update_extended_selection (clist, clist->focus_row);
5355 	  break;
5356 	default:
5357 	  break;
5358 	}
5359     }
5360 
5361   if (ROW_TOP_YPIXEL(clist, row) < 0)
5362     move_vertical (clist, row, 0);
5363   else if (ROW_TOP_YPIXEL(clist, row) + clist->row_height >
5364 	   clist->clist_window_height)
5365     move_vertical (clist, row, 1);
5366 
5367   return FALSE;
5368 }
5369 
5370 static void
gtk_cmclist_size_request(GtkWidget * widget,GtkRequisition * requisition)5371 gtk_cmclist_size_request (GtkWidget      *widget,
5372 			GtkRequisition *requisition)
5373 {
5374   GtkCMCList *clist;
5375   GtkStyle *style;
5376   gint i;
5377   guint border_width;
5378   cm_return_if_fail (GTK_IS_CMCLIST (widget));
5379   cm_return_if_fail (requisition != NULL);
5380 
5381   clist = GTK_CMCLIST (widget);
5382   style = gtk_widget_get_style (widget);
5383 
5384   requisition->width = 0;
5385   requisition->height = 0;
5386 
5387   /* compute the size of the column title (title) area */
5388   clist->column_title_area.height = 0;
5389   if (GTK_CMCLIST_SHOW_TITLES(clist)) {
5390     for (i = 0; i < clist->columns; i++)
5391       if (clist->column[i].button)
5392 	{
5393 	  GtkRequisition child_requisition;
5394 
5395 	  gtk_widget_size_request (clist->column[i].button,
5396 				   &child_requisition);
5397 	  clist->column_title_area.height =
5398 	    MAX (clist->column_title_area.height,
5399 		 child_requisition.height);
5400 	}
5401     //clist->column_title_area.height = font_height;
5402   }
5403   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
5404   requisition->width += (style->xthickness +
5405 			 border_width) * 2;
5406   requisition->height += (clist->column_title_area.height +
5407 			  (style->ythickness +
5408 			   border_width) * 2);
5409 
5410   /* if (!clist->hadjustment) */
5411   requisition->width += list_requisition_width (clist);
5412   /* if (!clist->vadjustment) */
5413   requisition->height += LIST_HEIGHT (clist);
5414 }
5415 
5416 static void
gtk_cmclist_size_allocate(GtkWidget * widget,GtkAllocation * allocation)5417 gtk_cmclist_size_allocate (GtkWidget     *widget,
5418 			 GtkAllocation *allocation)
5419 {
5420   GtkStyle *style;
5421   GtkCMCList *clist;
5422   GtkAllocation clist_allocation;
5423   gint border_width;
5424 
5425   cm_return_if_fail (GTK_IS_CMCLIST (widget));
5426   cm_return_if_fail (allocation != NULL);
5427 
5428   style = gtk_widget_get_style (widget);
5429   clist = GTK_CMCLIST (widget);
5430   gtk_widget_set_allocation (widget, allocation);
5431   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
5432 
5433   if (gtk_widget_get_realized (widget))
5434     {
5435       gdk_window_move_resize (gtk_widget_get_window (widget),
5436 			      allocation->x + border_width,
5437 			      allocation->y + border_width,
5438 			      allocation->width - border_width * 2,
5439 			      allocation->height - border_width * 2);
5440     }
5441 
5442   /* use internal allocation structure for all the math
5443    * because it's easier than always subtracting the container
5444    * border width */
5445   clist->internal_allocation.x = 0;
5446   clist->internal_allocation.y = 0;
5447   clist->internal_allocation.width = MAX (1, (gint)allocation->width -
5448 					  border_width * 2);
5449   clist->internal_allocation.height = MAX (1, (gint)allocation->height -
5450 					   border_width * 2);
5451 
5452   /* allocate clist window assuming no scrollbars */
5453   clist_allocation.x = (clist->internal_allocation.x +
5454 			style->xthickness);
5455   clist_allocation.y = (clist->internal_allocation.y +
5456 			style->ythickness +
5457 			clist->column_title_area.height);
5458   clist_allocation.width = MAX (1, (gint)clist->internal_allocation.width -
5459 				(2 * (gint)style->xthickness));
5460   clist_allocation.height = MAX (1, (gint)clist->internal_allocation.height -
5461 				 (2 * (gint)style->ythickness) -
5462 				 (gint)clist->column_title_area.height);
5463 
5464   clist->clist_window_width = clist_allocation.width;
5465   clist->clist_window_height = clist_allocation.height;
5466 
5467   if (gtk_widget_get_realized (widget))
5468     {
5469       gdk_window_move_resize (clist->clist_window,
5470 			      clist_allocation.x,
5471 			      clist_allocation.y,
5472 			      clist_allocation.width,
5473 			      clist_allocation.height);
5474     }
5475 
5476   /* position the window which holds the column title buttons */
5477   clist->column_title_area.x = style->xthickness;
5478   clist->column_title_area.y = style->ythickness;
5479   clist->column_title_area.width = clist_allocation.width;
5480 
5481   if (gtk_widget_get_realized (widget))
5482     {
5483       gdk_window_move_resize (clist->title_window,
5484 			      clist->column_title_area.x,
5485 			      clist->column_title_area.y,
5486 			      clist->column_title_area.width,
5487 			      clist->column_title_area.height);
5488     }
5489 
5490   /* column button allocation */
5491   size_allocate_columns (clist, FALSE);
5492   size_allocate_title_buttons (clist);
5493 
5494   adjust_adjustments (clist, TRUE);
5495 }
5496 
5497 /* GTKCONTAINER
5498  *   gtk_cmclist_forall
5499  */
5500 static void
gtk_cmclist_forall(GtkContainer * container,gboolean include_internals,GtkCallback callback,gpointer callback_data)5501 gtk_cmclist_forall (GtkContainer *container,
5502 		  gboolean      include_internals,
5503 		  GtkCallback   callback,
5504 		  gpointer      callback_data)
5505 {
5506   GtkCMCList *clist;
5507   guint i;
5508 
5509   cm_return_if_fail (GTK_IS_CMCLIST (container));
5510   cm_return_if_fail (callback != NULL);
5511 
5512   if (!include_internals)
5513     return;
5514 
5515   clist = GTK_CMCLIST (container);
5516 
5517   /* callback for the column buttons */
5518   for (i = 0; i < clist->columns; i++)
5519     if (clist->column[i].button)
5520       (*callback) (clist->column[i].button, callback_data);
5521 }
5522 
5523 /* PRIVATE DRAWING FUNCTIONS
5524  *   get_cell_style
5525  *   draw_cell_pixbuf
5526  *   draw_row
5527  *   draw_rows
5528  *   draw_xor_line
5529  *   clist_refresh
5530  */
5531 static void
get_cell_style(GtkCMCList * clist,GtkCMCListRow * clist_row,gint state,gint column,GtkStyle ** style)5532 get_cell_style (GtkCMCList     *clist,
5533 		GtkCMCListRow  *clist_row,
5534 		gint          state,
5535 		gint          column,
5536 		GtkStyle    **style)
5537 {
5538   GtkStyle *gtkstyle;
5539 
5540   if (clist_row->cell[column].style)
5541     {
5542       if (style)
5543 	*style = clist_row->cell[column].style;
5544     }
5545   else if (clist_row->style)
5546     {
5547       if (style)
5548 	*style = clist_row->style;
5549     }
5550   else
5551     {
5552       gtkstyle = gtk_widget_get_style (GTK_WIDGET (clist));
5553       if (style)
5554 	*style = gtkstyle;
5555     }
5556 }
5557 
5558 static gint
draw_cell_pixbuf(GdkWindow * window,GdkRectangle * clip_rectangle,cairo_t * cr,GdkPixbuf * pixbuf,gint x,gint y,gint width,gint height)5559 draw_cell_pixbuf (GdkWindow    *window,
5560 		  GdkRectangle *clip_rectangle,
5561 		  cairo_t      *cr,
5562 		  GdkPixbuf    *pixbuf,
5563 		  gint          x,
5564 		  gint          y,
5565 		  gint          width,
5566 		  gint          height)
5567 {
5568   gint xsrc = 0;
5569   gint ysrc = 0;
5570 
5571   if (!pixbuf || (width == 0 && height == 0))
5572 	return x;
5573 
5574   if (x < clip_rectangle->x)
5575     {
5576       xsrc = clip_rectangle->x - x;
5577       width -= xsrc;
5578       x = clip_rectangle->x;
5579     }
5580   if (x + width > clip_rectangle->x + clip_rectangle->width)
5581     width = clip_rectangle->x + clip_rectangle->width - x;
5582 
5583   if (y < clip_rectangle->y)
5584     {
5585       ysrc = clip_rectangle->y - y;
5586       height -= ysrc;
5587       y = clip_rectangle->y;
5588     }
5589 
5590   if (y + height > clip_rectangle->y + clip_rectangle->height)
5591     height = clip_rectangle->y + clip_rectangle->height - y;
5592 
5593   gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
5594   cairo_paint(cr);
5595 
5596   return x + MAX (width, 0);
5597 }
5598 
cairo_dash_from_add_mode(GtkCMCList * clist,cairo_t * cr)5599 static void cairo_dash_from_add_mode(GtkCMCList *clist, cairo_t *cr)
5600 {
5601 	const double dashes[] = { 4.0, 4.0 };
5602 	if (GTK_CMCLIST_ADD_MODE(clist))
5603 		cairo_set_dash(cr, dashes, 2, 0);
5604 	else
5605 		cairo_set_dash(cr, NULL, 0, 0);
5606 }
5607 
5608 static void
draw_row(GtkCMCList * clist,GdkRectangle * area,gint row,GtkCMCListRow * clist_row)5609 draw_row (GtkCMCList     *clist,
5610 	  GdkRectangle *area,
5611 	  gint          row,
5612 	  GtkCMCListRow  *clist_row)
5613 {
5614   GtkStyle *style;
5615   GtkWidget *widget;
5616   GdkRectangle *rect;
5617   GdkRectangle row_rectangle;
5618   GdkRectangle cell_rectangle;
5619   GdkRectangle clip_rectangle;
5620   GdkRectangle intersect_rectangle;
5621   gint last_column;
5622   gint state;
5623   gint i;
5624   cairo_t *cr;
5625   cm_return_if_fail (clist != NULL);
5626 
5627   /* bail now if we arn't drawable yet */
5628   if (!gtk_widget_is_drawable (GTK_WIDGET(clist)) || row < 0 || row >= clist->rows)
5629     return;
5630 
5631   widget = GTK_WIDGET (clist);
5632 
5633   /* if the function is passed the pointer to the row instead of null,
5634    * it avoids this expensive lookup */
5635   if (!clist_row)
5636     clist_row = ROW_ELEMENT (clist, row)->data;
5637 
5638   style = clist_row->style ? clist_row->style : gtk_widget_get_style (widget);
5639 
5640   /* rectangle of the entire row */
5641   row_rectangle.x = 0;
5642   row_rectangle.y = ROW_TOP_YPIXEL (clist, row);
5643   row_rectangle.width = clist->clist_window_width;
5644   row_rectangle.height = clist->row_height;
5645 
5646   /* rectangle of the cell spacing above the row */
5647   cell_rectangle.x = 0;
5648   cell_rectangle.y = row_rectangle.y - CELL_SPACING;
5649   cell_rectangle.width = row_rectangle.width;
5650   cell_rectangle.height = CELL_SPACING;
5651 
5652   /* rectangle used to clip drawing operations, its y and height
5653    * positions only need to be set once, so we set them once here.
5654    * the x and width are set withing the drawing loop below once per
5655    * column */
5656   clip_rectangle.y = row_rectangle.y;
5657   clip_rectangle.height = row_rectangle.height;
5658 
5659   state = clist_row->state;
5660   cr = gdk_cairo_create(clist->clist_window);
5661 
5662   /* draw the cell borders and background */
5663   if (area)
5664     {
5665       rect = &intersect_rectangle;
5666       if (gdk_rectangle_intersect (area, &cell_rectangle,
5667 				   &intersect_rectangle)) {
5668 		gdk_cairo_rectangle(cr, &intersect_rectangle);
5669 		gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
5670 		cairo_fill(cr);
5671       }
5672 
5673       /* the last row has to clear its bottom cell spacing too */
5674       if (clist_row == clist->row_list_end->data)
5675 	{
5676 	  cell_rectangle.y += clist->row_height + CELL_SPACING;
5677 
5678 	  if (gdk_rectangle_intersect (area, &cell_rectangle,
5679 				       &intersect_rectangle)) {
5680 	     gdk_cairo_rectangle(cr, &intersect_rectangle);
5681 	     gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
5682 	     cairo_fill(cr);
5683 	    }
5684 	}
5685 
5686       if (!gdk_rectangle_intersect (area, &row_rectangle,&intersect_rectangle))
5687 	return;
5688 
5689     }
5690   else
5691     {
5692       rect = &clip_rectangle;
5693       gdk_cairo_rectangle(cr, &cell_rectangle);
5694       gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
5695       cairo_fill(cr);
5696 
5697       /* the last row has to clear its bottom cell spacing too */
5698       if (clist_row == clist->row_list_end->data)
5699 	{
5700 	  cell_rectangle.y += clist->row_height + CELL_SPACING;
5701 	  gdk_cairo_rectangle(cr, &cell_rectangle);
5702 	  gdk_cairo_set_source_color(cr, &style->base[GTK_STATE_NORMAL]);
5703 	  cairo_fill(cr);
5704 	}
5705     }
5706 
5707   for (last_column = clist->columns - 1;
5708        last_column >= 0 && !clist->column[last_column].visible; last_column--)
5709     ;
5710 
5711   /* iterate and draw all the columns (row cells) and draw their contents */
5712   for (i = 0; i < clist->columns; i++)
5713     {
5714       GtkStyle *style;
5715       PangoLayout *layout;
5716       PangoRectangle logical_rect;
5717 
5718       gint width;
5719       gint height;
5720       gint pixbuf_width;
5721       gint offset = 0;
5722 
5723       if (!clist->column[i].visible)
5724 	continue;
5725 
5726       get_cell_style (clist, clist_row, state, i, &style);
5727 
5728       clip_rectangle.x = clist->column[i].area.x + clist->hoffset;
5729       clip_rectangle.width = clist->column[i].area.width;
5730 
5731       /* calculate clipping region clipping region */
5732       clip_rectangle.x -= COLUMN_INSET + CELL_SPACING;
5733       clip_rectangle.width += (2 * COLUMN_INSET + CELL_SPACING +
5734 			       (i == last_column) * CELL_SPACING);
5735 
5736       if (area && !gdk_rectangle_intersect (area, &clip_rectangle,
5737 					    &intersect_rectangle))
5738 	continue;
5739 
5740       gdk_cairo_rectangle(cr, rect);
5741       gdk_cairo_set_source_color(cr, &style->base[state]);
5742       cairo_fill(cr);
5743 
5744       clip_rectangle.x += COLUMN_INSET + CELL_SPACING;
5745       clip_rectangle.width -= (2 * COLUMN_INSET + CELL_SPACING +
5746 			       (i == last_column) * CELL_SPACING);
5747 
5748 
5749       /* calculate real width for column justification */
5750 
5751       layout = _gtk_cmclist_create_cell_layout (clist, clist_row, i);
5752       if (layout)
5753 	{
5754 	  pango_layout_get_pixel_extents (layout, NULL, &logical_rect);
5755 	  width = logical_rect.width;
5756 	}
5757       else
5758 	width = 0;
5759 
5760       pixbuf_width = 0;
5761       height = 0;
5762       offset = 0;
5763       switch (clist_row->cell[i].type)
5764 	{
5765 	case GTK_CMCELL_PIXBUF:
5766 	  pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
5767 	  height = gdk_pixbuf_get_height(GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf);
5768 	  width += pixbuf_width;
5769 	  break;
5770 	case GTK_CMCELL_PIXTEXT:
5771 	  pixbuf_width = gdk_pixbuf_get_width(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
5772 	  height = gdk_pixbuf_get_height(GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf);
5773 	  width += pixbuf_width + GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
5774 	  break;
5775 	default:
5776 	  break;
5777 	}
5778 
5779       switch (clist->column[i].justification)
5780 	{
5781 	case GTK_JUSTIFY_LEFT:
5782 	  offset = clip_rectangle.x + clist_row->cell[i].horizontal;
5783 	  break;
5784 	case GTK_JUSTIFY_RIGHT:
5785 	  offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
5786 		    clip_rectangle.width - width);
5787 	  break;
5788 	case GTK_JUSTIFY_CENTER:
5789 	case GTK_JUSTIFY_FILL:
5790 	  offset = (clip_rectangle.x + clist_row->cell[i].horizontal +
5791 		    (clip_rectangle.width / 2) - (width / 2));
5792 	  break;
5793 	};
5794 
5795       /* Draw Text and/or Pixbuf */
5796       switch (clist_row->cell[i].type)
5797 	{
5798 	case GTK_CMCELL_PIXBUF:
5799 	  draw_cell_pixbuf (clist->clist_window, &clip_rectangle, cr,
5800 			    GTK_CMCELL_PIXBUF (clist_row->cell[i])->pixbuf,
5801 			    offset,
5802 			    clip_rectangle.y + clist_row->cell[i].vertical +
5803 			    (clip_rectangle.height - height) / 2,
5804 			    pixbuf_width, height);
5805 	  break;
5806 	case GTK_CMCELL_PIXTEXT:
5807 	  offset =
5808 	    draw_cell_pixbuf (clist->clist_window, &clip_rectangle, cr,
5809 			      GTK_CMCELL_PIXTEXT (clist_row->cell[i])->pixbuf,
5810 			      offset,
5811 			      clip_rectangle.y + clist_row->cell[i].vertical+
5812 			      (clip_rectangle.height - height) / 2,
5813 			      pixbuf_width, height);
5814 	  offset += GTK_CMCELL_PIXTEXT (clist_row->cell[i])->spacing;
5815 
5816 	  /* Fall through */
5817 	case GTK_CMCELL_TEXT:
5818 	  if (layout)
5819 	    {
5820 	      gint row_center_offset = (clist->row_height - logical_rect.height - 1) / 2;
5821 	      gdk_cairo_set_source_color(cr, clist_row->fg_set ? &clist_row->foreground : &style->text[state]);
5822 	      cairo_move_to(cr, offset, row_rectangle.y + row_center_offset + clist_row->cell[i].vertical);
5823 	      pango_cairo_show_layout(cr, layout);
5824               g_object_unref (G_OBJECT (layout));
5825 	    }
5826 	  break;
5827 	default:
5828 	  break;
5829 	}
5830     }
5831 
5832   /* draw focus rectangle */
5833   cairo_dash_from_add_mode(clist, cr);
5834   cairo_set_line_width(cr, 1.0);
5835   cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
5836   if (clist->focus_row == row &&
5837       gtk_widget_get_can_focus (widget) && gtk_widget_has_focus(widget))
5838     {
5839       if (!area) {
5840 	cairo_rectangle(cr, row_rectangle.x, row_rectangle.y,
5841 			    row_rectangle.width + 1, row_rectangle.height);
5842 	gdk_cairo_set_source_color(cr, &style->text[GTK_STATE_NORMAL]);
5843 	cairo_stroke(cr);
5844       }
5845       else if (gdk_rectangle_intersect (area, &row_rectangle,
5846 					&intersect_rectangle))
5847 	{
5848 	  cairo_rectangle(cr, row_rectangle.x, row_rectangle.y,
5849 			    row_rectangle.width + 1, row_rectangle.height);
5850 	  gdk_cairo_set_source_color(cr, &style->text[GTK_STATE_NORMAL]);
5851 	  cairo_stroke(cr);
5852 	}
5853     }
5854     cairo_destroy(cr);
5855 
5856 }
5857 
5858 static void
draw_rows(GtkCMCList * clist,GdkRectangle * area)5859 draw_rows (GtkCMCList     *clist,
5860 	   GdkRectangle *area)
5861 {
5862   GList *list;
5863   GtkCMCListRow *clist_row;
5864   gint i;
5865   gint first_row;
5866   gint last_row;
5867 
5868   cm_return_if_fail (GTK_IS_CMCLIST (clist));
5869 
5870   if (clist->row_height == 0 ||
5871       !gtk_widget_is_drawable (GTK_WIDGET(clist)))
5872     return;
5873 
5874   if (area)
5875     {
5876       first_row = ROW_FROM_YPIXEL (clist, area->y);
5877       last_row = ROW_FROM_YPIXEL (clist, area->y + area->height);
5878     }
5879   else
5880     {
5881       first_row = ROW_FROM_YPIXEL (clist, 0);
5882       last_row = ROW_FROM_YPIXEL (clist, clist->clist_window_height);
5883     }
5884 
5885   /* this is a small special case which exposes the bottom cell line
5886    * on the last row -- it might go away if I change the wall the cell
5887    * spacings are drawn
5888    */
5889   if (clist->rows == first_row)
5890     first_row--;
5891 
5892   list = ROW_ELEMENT (clist, first_row);
5893   i = first_row;
5894   while (list)
5895     {
5896       clist_row = list->data;
5897       list = list->next;
5898 
5899       if (i > last_row)
5900 	return;
5901 
5902       GTK_CMCLIST_GET_CLASS (clist)->draw_row (clist, area, i, clist_row);
5903       i++;
5904     }
5905 
5906   if (!area) {
5907     int w, h, y;
5908     cairo_t *cr;
5909     w = gdk_window_get_width(clist->clist_window);
5910     h = gdk_window_get_height(clist->clist_window);
5911     cr = gdk_cairo_create(clist->clist_window);
5912     y = ROW_TOP_YPIXEL (clist, i);
5913     gdk_cairo_set_source_color(cr, &gtk_widget_get_style(GTK_WIDGET(clist))->base[GTK_STATE_NORMAL]);
5914     cairo_rectangle(cr, 0, y, w, h - y);
5915     cairo_fill(cr);
5916     cairo_destroy(cr);
5917   }
5918 }
5919 
5920 static void
draw_xor_line(GtkCMCList * clist)5921 draw_xor_line (GtkCMCList *clist)
5922 {
5923   cairo_t *cr;
5924   cr = gdk_cairo_create(clist->clist_window);
5925   cairo_set_line_width(cr, 1.0);
5926   cairo_move_to(cr, clist->x_drag,
5927 		 gtk_widget_get_style (GTK_WIDGET(clist))->ythickness);
5928   cairo_line_to(cr, clist->x_drag,
5929                  clist->column_title_area.height +
5930 		 clist->clist_window_height + 1);
5931   cairo_stroke(cr);
5932   cairo_destroy(cr);
5933 }
5934 
5935 static void
clist_refresh(GtkCMCList * clist)5936 clist_refresh (GtkCMCList *clist)
5937 {
5938   cm_return_if_fail (GTK_IS_CMCLIST (clist));
5939 
5940   if (CLIST_UNFROZEN (clist))
5941     {
5942       adjust_adjustments (clist, FALSE);
5943       draw_rows (clist, NULL);
5944     }
5945 }
5946 
5947 /* get cell from coordinates
5948  *   get_selection_info
5949  *   gtk_cmclist_get_selection_info
5950  */
5951 static gint
get_selection_info(GtkCMCList * clist,gint x,gint y,gint * row,gint * column)5952 get_selection_info (GtkCMCList *clist,
5953 		    gint      x,
5954 		    gint      y,
5955 		    gint     *row,
5956 		    gint     *column)
5957 {
5958   gint trow, tcol;
5959 
5960   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
5961 
5962   /* bounds checking, return false if the user clicked
5963    * on a blank area */
5964   trow = ROW_FROM_YPIXEL (clist, y);
5965   if (trow >= clist->rows)
5966     return 0;
5967 
5968   if (row)
5969     *row = trow;
5970 
5971   tcol = COLUMN_FROM_XPIXEL (clist, x);
5972   if (tcol >= clist->columns)
5973     return 0;
5974 
5975   if (column)
5976     *column = tcol;
5977 
5978   return 1;
5979 }
5980 
5981 gint
gtk_cmclist_get_selection_info(GtkCMCList * clist,gint x,gint y,gint * row,gint * column)5982 gtk_cmclist_get_selection_info (GtkCMCList *clist,
5983 			      gint      x,
5984 			      gint      y,
5985 			      gint     *row,
5986 			      gint     *column)
5987 {
5988   cm_return_val_if_fail (GTK_IS_CMCLIST (clist), 0);
5989   return get_selection_info (clist, x, y, row, column);
5990 }
5991 
5992 /* PRIVATE ADJUSTMENT FUNCTIONS
5993  *   adjust_adjustments
5994  *   vadjustment_changed
5995  *   hadjustment_changed
5996  *   vadjustment_value_changed
5997  *   hadjustment_value_changed
5998  *   check_exposures
5999  */
6000 static void
adjust_adjustments(GtkCMCList * clist,gboolean block_resize)6001 adjust_adjustments (GtkCMCList *clist,
6002 		    gboolean  block_resize)
6003 {
6004   if (clist->vadjustment)
6005     {
6006       gtk_adjustment_set_page_size (clist->vadjustment, clist->clist_window_height);
6007       gtk_adjustment_set_step_increment (clist->vadjustment, clist->row_height);
6008       gtk_adjustment_set_page_increment (clist->vadjustment,
6009 	MAX (clist->clist_window_height - clist->row_height,
6010 	     clist->clist_window_height / 2));
6011       gtk_adjustment_set_lower (clist->vadjustment, 0);
6012       gtk_adjustment_set_upper (clist->vadjustment, LIST_HEIGHT (clist));
6013 
6014       if ((clist->clist_window_height - clist->voffset) > LIST_HEIGHT (clist) ||
6015 	  (clist->voffset + (gint)gtk_adjustment_get_value (clist->vadjustment)) != 0)
6016 	{
6017 	  gtk_adjustment_set_value (clist->vadjustment,
6018 	   MAX (0, (LIST_HEIGHT (clist) - clist->clist_window_height)));
6019 	  g_signal_emit_by_name (G_OBJECT (clist->vadjustment),
6020 				   "value_changed");
6021 	}
6022       g_signal_emit_by_name (G_OBJECT (clist->vadjustment), "changed");
6023     }
6024 
6025   if (clist->hadjustment)
6026     {
6027       gtk_adjustment_set_page_size (clist->hadjustment, clist->clist_window_width);
6028       gtk_adjustment_set_step_increment (clist->hadjustment, 10);
6029       gtk_adjustment_set_page_increment (clist->hadjustment,
6030 	MAX (clist->clist_window_width -
6031          gtk_adjustment_get_step_increment (clist->hadjustment),
6032 	     clist->clist_window_width / 2));
6033       gtk_adjustment_set_lower (clist->hadjustment, 0);
6034       gtk_adjustment_set_upper (clist->hadjustment, LIST_WIDTH (clist));
6035 
6036       if ((clist->clist_window_width - clist->hoffset) > LIST_WIDTH (clist) ||
6037 	  (clist->hoffset + (gint)gtk_adjustment_get_value (clist->hadjustment)) != 0)
6038 	{
6039 	  gtk_adjustment_set_value (clist->hadjustment, MAX (0, (LIST_WIDTH (clist) -
6040 					       clist->clist_window_width)));
6041 	  g_signal_emit_by_name (G_OBJECT (clist->hadjustment),
6042 				   "value_changed");
6043 	}
6044       g_signal_emit_by_name (G_OBJECT (clist->hadjustment), "changed");
6045     }
6046 
6047   if (!block_resize && (!clist->vadjustment || !clist->hadjustment))
6048     {
6049       GtkWidget *widget;
6050       GtkRequisition requisition;
6051       GtkAllocation allocation;
6052 
6053       widget = GTK_WIDGET (clist);
6054       gtk_widget_size_request (widget, &requisition);
6055       gtk_widget_get_allocation (widget, &allocation);
6056 
6057       if ((!clist->hadjustment &&
6058 	   requisition.width != allocation.width) ||
6059 	  (!clist->vadjustment &&
6060 	   requisition.height != allocation.height))
6061 	gtk_widget_queue_resize (widget);
6062     }
6063 }
6064 
6065 static void
vadjustment_value_changed(GtkAdjustment * adjustment,gpointer data)6066 vadjustment_value_changed (GtkAdjustment *adjustment,
6067 			   gpointer       data)
6068 {
6069   GtkCMCList *clist;
6070   gint dy, value;
6071 
6072   cm_return_if_fail (adjustment != NULL);
6073   cm_return_if_fail (GTK_IS_CMCLIST (data));
6074 
6075   clist = GTK_CMCLIST (data);
6076 
6077   if (adjustment != clist->vadjustment)
6078     return;
6079 
6080   value = -gtk_adjustment_get_value (adjustment);
6081   dy = value - clist->voffset;
6082   clist->voffset = value;
6083 
6084   if (gtk_widget_is_drawable (GTK_WIDGET(clist)))
6085     {
6086       gdk_window_scroll (clist->clist_window, 0, dy);
6087       gdk_window_process_updates (clist->clist_window, FALSE);
6088     }
6089 
6090   return;
6091 }
6092 
6093 typedef struct
6094 {
6095   GdkWindow *window;
6096   gint dx;
6097 } ScrollData;
6098 
6099 /* The window to which widget->window is relative */
6100 #define ALLOCATION_WINDOW(widget)		\
6101    (!gtk_widget_get_has_window (widget) ?		\
6102 	gtk_widget_get_window (widget) :         \
6103 	gdk_window_get_parent (gtk_widget_get_window(widget)))
6104 
6105 static void
adjust_allocation_recurse(GtkWidget * widget,gpointer data)6106 adjust_allocation_recurse (GtkWidget *widget,
6107 			   gpointer   data)
6108 {
6109   GtkAllocation allocation;
6110   ScrollData *scroll_data = data;
6111 
6112   gtk_widget_get_allocation (widget, &allocation);
6113 
6114   if (!gtk_widget_get_realized (widget))
6115     {
6116       if (gtk_widget_get_visible (widget))
6117 	{
6118 	  GdkRectangle tmp_rectangle = allocation;
6119 	  tmp_rectangle.x += scroll_data->dx;
6120 
6121 	  gtk_widget_size_allocate (widget, &tmp_rectangle);
6122 	}
6123     }
6124   else
6125     {
6126       if (ALLOCATION_WINDOW (widget) == scroll_data->window)
6127 	{
6128 	  allocation.x += scroll_data->dx;
6129 	  gtk_widget_set_allocation (widget, &allocation);
6130 
6131 	  if (GTK_IS_CONTAINER (widget))
6132 	    gtk_container_forall (GTK_CONTAINER (widget),
6133 				  adjust_allocation_recurse,
6134 				  data);
6135 	}
6136     }
6137 }
6138 
6139 static void
adjust_allocation(GtkWidget * widget,gint dx)6140 adjust_allocation (GtkWidget *widget,
6141 		   gint       dx)
6142 {
6143   ScrollData scroll_data;
6144 
6145   if (gtk_widget_get_realized (widget))
6146     scroll_data.window = ALLOCATION_WINDOW (widget);
6147   else
6148     scroll_data.window = NULL;
6149 
6150   scroll_data.dx = dx;
6151 
6152   adjust_allocation_recurse (widget, &scroll_data);
6153 }
6154 
6155 static void
hadjustment_value_changed(GtkAdjustment * adjustment,gpointer data)6156 hadjustment_value_changed (GtkAdjustment *adjustment,
6157 			   gpointer       data)
6158 {
6159   GtkCMCList *clist;
6160   GtkContainer *container;
6161   gint i;
6162   gint y = 0;
6163   gint value;
6164   gint dx;
6165   cairo_t *cr;
6166 
6167   cm_return_if_fail (adjustment != NULL);
6168   cm_return_if_fail (GTK_IS_CMCLIST (data));
6169 
6170   clist = GTK_CMCLIST (data);
6171   container = GTK_CONTAINER (data);
6172 
6173   if (adjustment != clist->hadjustment)
6174     return;
6175 
6176   value = gtk_adjustment_get_value (adjustment);
6177 
6178   dx = -value - clist->hoffset;
6179 
6180   if (gtk_widget_get_realized (GTK_WIDGET(clist)))
6181     gdk_window_scroll (clist->title_window, dx, 0);
6182 
6183   /* adjust the column button's allocations */
6184   for (i = 0; i < clist->columns; i++)
6185     if (clist->column[i].button)
6186       adjust_allocation (clist->column[i].button, dx);
6187 
6188   clist->hoffset = -value;
6189 
6190   cr = gdk_cairo_create(clist->clist_window);
6191   cairo_dash_from_add_mode(clist, cr);
6192   cairo_set_line_width(cr, 1.0);
6193   cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
6194   if (gtk_widget_is_drawable (GTK_WIDGET(clist)))
6195     {
6196       GtkWidget *focus_child = gtk_container_get_focus_child (container);
6197 
6198       gdk_window_scroll (clist->clist_window, dx, 0);
6199       gdk_window_process_updates (clist->clist_window, FALSE);
6200 
6201       if (gtk_widget_get_can_focus(GTK_WIDGET(clist)) &&
6202           gtk_widget_has_focus(GTK_WIDGET(clist)) &&
6203           !focus_child && GTK_CMCLIST_ADD_MODE(clist))
6204         {
6205           y = ROW_TOP_YPIXEL (clist, clist->focus_row);
6206 	  cairo_rectangle(cr, 0, y, clist->clist_window_width + 1,
6207                               clist->row_height);
6208 	  cairo_stroke(cr);
6209         }
6210 
6211       if (gtk_widget_get_can_focus(GTK_WIDGET(clist)) &&
6212           gtk_widget_has_focus(GTK_WIDGET(clist)) &&
6213           !focus_child)
6214         {
6215           if (GTK_CMCLIST_ADD_MODE(clist))
6216             {
6217               gint focus_row;
6218 
6219               focus_row = clist->focus_row;
6220               clist->focus_row = -1;
6221               draw_rows (clist, NULL);
6222               clist->focus_row = focus_row;
6223 
6224 	      cairo_rectangle(cr, 0, y, clist->clist_window_width + 1,
6225                               clist->row_height);
6226 	      cairo_stroke(cr);
6227               return;
6228             }
6229         }
6230     }
6231     cairo_destroy(cr);
6232 }
6233 
6234 /* PRIVATE
6235  * Memory Allocation/Distruction Routines for GtkCMCList stuctures
6236  *
6237  * functions:
6238  *   columns_new
6239  *   column_title_new
6240  *   columns_delete
6241  *   row_new
6242  *   row_delete
6243  */
6244 static GtkCMCListColumn *
columns_new(GtkCMCList * clist)6245 columns_new (GtkCMCList *clist)
6246 {
6247   GtkCMCListColumn *column;
6248   gint i;
6249 
6250   column = g_new (GtkCMCListColumn, clist->columns);
6251 
6252   for (i = 0; i < clist->columns; i++)
6253     {
6254       column[i].area.x = 0;
6255       column[i].area.y = 0;
6256       column[i].area.width = 0;
6257       column[i].area.height = 0;
6258       column[i].title = NULL;
6259       column[i].button = NULL;
6260       column[i].window = NULL;
6261       column[i].width = 0;
6262       column[i].min_width = -1;
6263       column[i].max_width = -1;
6264       column[i].visible = TRUE;
6265       column[i].width_set = FALSE;
6266       column[i].resizeable = TRUE;
6267       column[i].auto_resize = FALSE;
6268       column[i].button_passive = FALSE;
6269       column[i].justification = GTK_JUSTIFY_LEFT;
6270     }
6271 
6272   return column;
6273 }
6274 
6275 static void
column_title_new(GtkCMCList * clist,gint column,const gchar * title)6276 column_title_new (GtkCMCList    *clist,
6277 		  gint         column,
6278 		  const gchar *title)
6279 {
6280   g_free (clist->column[column].title);
6281 
6282   clist->column[column].title = g_strdup (title);
6283 }
6284 
6285 static void
columns_delete(GtkCMCList * clist)6286 columns_delete (GtkCMCList *clist)
6287 {
6288   gint i;
6289 
6290   for (i = 0; i < clist->columns; i++)
6291     g_free (clist->column[i].title);
6292 
6293   g_free (clist->column);
6294 }
6295 
6296 static GtkCMCListRow *
row_new(GtkCMCList * clist)6297 row_new (GtkCMCList *clist)
6298 {
6299   int i;
6300   GtkCMCListRow *clist_row;
6301 
6302   clist_row = g_slice_new (GtkCMCListRow);
6303   clist_row->cell = g_slice_alloc (sizeof (GtkCMCell) * clist->columns);
6304 
6305   for (i = 0; i < clist->columns; i++)
6306     {
6307       clist_row->cell[i].type = GTK_CMCELL_EMPTY;
6308       clist_row->cell[i].vertical = 0;
6309       clist_row->cell[i].horizontal = 0;
6310       clist_row->cell[i].style = NULL;
6311     }
6312 
6313   clist_row->fg_set = FALSE;
6314   clist_row->bg_set = FALSE;
6315   clist_row->style = NULL;
6316   clist_row->selectable = TRUE;
6317   clist_row->state = GTK_STATE_NORMAL;
6318   clist_row->data = NULL;
6319   clist_row->destroy = NULL;
6320 
6321   return clist_row;
6322 }
6323 
6324 static void
row_delete(GtkCMCList * clist,GtkCMCListRow * clist_row)6325 row_delete (GtkCMCList    *clist,
6326 	    GtkCMCListRow *clist_row)
6327 {
6328   gint i;
6329 
6330   for (i = 0; i < clist->columns; i++)
6331     {
6332       GTK_CMCLIST_GET_CLASS (clist)->set_cell_contents
6333 	(clist, clist_row, i, GTK_CMCELL_EMPTY, NULL, 0, NULL);
6334       if (clist_row->cell[i].style)
6335 	{
6336 	  if (gtk_widget_get_realized (GTK_WIDGET(clist)))
6337 	    gtk_style_detach (clist_row->cell[i].style);
6338 	  g_object_unref (clist_row->cell[i].style);
6339 	}
6340     }
6341 
6342   if (clist_row->style)
6343     {
6344       if (gtk_widget_get_realized (GTK_WIDGET(clist)))
6345         gtk_style_detach (clist_row->style);
6346       g_object_unref (clist_row->style);
6347     }
6348 
6349   if (clist_row->destroy)
6350     clist_row->destroy (clist_row->data);
6351 
6352   g_slice_free1 (sizeof (GtkCMCell) * clist->columns, clist_row->cell);
6353   g_slice_free (GtkCMCListRow, clist_row);
6354 }
6355 
6356 /* FOCUS FUNCTIONS
6357  *   gtk_cmclist_focus_content_area
6358  *   gtk_cmclist_focus
6359  *   gtk_cmclist_draw_focus
6360  *   gtk_cmclist_focus_in
6361  *   gtk_cmclist_focus_out
6362  *   title_focus
6363  */
6364 static void
gtk_cmclist_focus_content_area(GtkCMCList * clist)6365 gtk_cmclist_focus_content_area (GtkCMCList *clist)
6366 {
6367   if (clist->focus_row < 0)
6368     {
6369       clist->focus_row = 0;
6370 
6371       if ((clist->selection_mode == GTK_SELECTION_BROWSE ||
6372 	   clist->selection_mode == GTK_SELECTION_MULTIPLE) &&
6373 	  !clist->selection)
6374 	g_signal_emit (G_OBJECT (clist),
6375 			 clist_signals[SELECT_ROW], 0,
6376 			 clist->focus_row, -1, NULL);
6377     }
6378   gtk_widget_grab_focus (GTK_WIDGET (clist));
6379 }
6380 
6381 static gboolean
gtk_cmclist_focus(GtkWidget * widget,GtkDirectionType direction)6382 gtk_cmclist_focus (GtkWidget        *widget,
6383 		 GtkDirectionType  direction)
6384 {
6385   GtkCMCList *clist = GTK_CMCLIST (widget);
6386   GtkWidget *focus_child;
6387   gboolean is_current_focus;
6388 
6389   if (!gtk_widget_is_sensitive (widget))
6390     return FALSE;
6391 
6392   focus_child = gtk_container_get_focus_child (GTK_CONTAINER (widget));
6393 
6394   is_current_focus = gtk_widget_is_focus (GTK_WIDGET (clist));
6395 
6396   if (focus_child &&
6397       gtk_widget_child_focus (focus_child, direction))
6398     return TRUE;
6399 
6400   switch (direction)
6401     {
6402     case GTK_DIR_LEFT:
6403     case GTK_DIR_RIGHT:
6404       if (focus_child)
6405 	{
6406 	  if (title_focus_move (clist, direction))
6407 	    return TRUE;
6408 	}
6409       else if (!is_current_focus)
6410 	{
6411 	  gtk_cmclist_focus_content_area (clist);
6412 	  return TRUE;
6413 	}
6414       break;
6415     case GTK_DIR_DOWN:
6416     case GTK_DIR_TAB_FORWARD:
6417       if (!focus_child && !is_current_focus)
6418 	{
6419 	  if (title_focus_in (clist, direction))
6420 	    return TRUE;
6421 	}
6422 
6423       if (!is_current_focus && clist->rows)
6424 	{
6425 	  gtk_cmclist_focus_content_area (clist);
6426 	  return TRUE;
6427 	}
6428       break;
6429     case GTK_DIR_UP:
6430     case GTK_DIR_TAB_BACKWARD:
6431       if (!focus_child && is_current_focus)
6432 	{
6433 	  if (title_focus_in (clist, direction))
6434 	    return TRUE;
6435 	}
6436 
6437       if (!is_current_focus && !focus_child && clist->rows)
6438 	{
6439 	  gtk_cmclist_focus_content_area (clist);
6440 	  return TRUE;
6441 	}
6442       break;
6443     default:
6444       break;
6445     }
6446 
6447   return FALSE;
6448 }
6449 
6450 static void
gtk_cmclist_set_focus_child(GtkContainer * container,GtkWidget * child)6451 gtk_cmclist_set_focus_child (GtkContainer *container,
6452 			   GtkWidget    *child)
6453 {
6454   GtkCMCList *clist = GTK_CMCLIST (container);
6455   gint i;
6456 
6457   for (i = 0; i < clist->columns; i++)
6458     if (clist->column[i].button == child)
6459       clist->focus_header_column = i;
6460 
6461   if (GTK_CONTAINER_CLASS (gtk_cmclist_parent_class)->set_focus_child)
6462     (*GTK_CONTAINER_CLASS (gtk_cmclist_parent_class)->set_focus_child) (container, child);
6463 }
6464 
6465 static void
gtk_cmclist_draw_focus(GtkWidget * widget)6466 gtk_cmclist_draw_focus (GtkWidget *widget)
6467 {
6468   GtkCMCList *clist;
6469   cairo_t *cr;
6470 
6471   cm_return_if_fail (GTK_IS_CMCLIST (widget));
6472 
6473   if (!gtk_widget_is_drawable (widget) || !gtk_widget_get_can_focus (widget))
6474     return;
6475 
6476   clist = GTK_CMCLIST (widget);
6477   if (clist->focus_row >= 0) {
6478     cr = gdk_cairo_create(clist->clist_window);
6479     cairo_dash_from_add_mode(clist, cr);
6480     cairo_set_line_width(cr, 1.0);
6481     cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
6482     cairo_rectangle(cr, 0, ROW_TOP_YPIXEL(clist, clist->focus_row) + 0.5,
6483 			clist->clist_window_width + 1,
6484 			clist->row_height - 0.5);
6485     cairo_stroke(cr);
6486     cairo_destroy(cr);
6487   }
6488 }
6489 
6490 static void
gtk_cmclist_undraw_focus(GtkWidget * widget)6491 gtk_cmclist_undraw_focus (GtkWidget *widget)
6492 {
6493   GtkCMCList *clist;
6494   int row;
6495   cm_return_if_fail (GTK_IS_CMCLIST (widget));
6496 
6497   clist = GTK_CMCLIST(widget);
6498 
6499   if (clist->focus_row < 0)
6500     return;
6501 
6502   if (!gtk_widget_is_drawable (widget) || !gtk_widget_get_can_focus (widget))
6503     return;
6504 
6505   clist = GTK_CMCLIST (widget);
6506   if (clist->focus_row >= 0) {
6507     cairo_t *cr = gdk_cairo_create(clist->clist_window);
6508     cairo_set_line_width(cr, 1.0);
6509     gdk_cairo_set_source_color(cr, &gtk_widget_get_style(widget)->base[GTK_STATE_NORMAL]);
6510     cairo_set_antialias(cr, CAIRO_ANTIALIAS_NONE);
6511     cairo_rectangle(cr, 0, ROW_TOP_YPIXEL(clist, clist->focus_row) + 0.5,
6512 			clist->clist_window_width + 1,
6513 			clist->row_height - 0.5);
6514     cairo_stroke(cr);
6515     cairo_destroy(cr);
6516   }
6517 
6518   row = clist->focus_row;
6519   GTK_CMCLIST_GET_CLASS(GTK_CMCLIST(widget))->draw_row(clist, NULL, row, ROW_ELEMENT (clist, row)->data);
6520 }
6521 
6522 static gint
gtk_cmclist_focus_in(GtkWidget * widget,GdkEventFocus * event)6523 gtk_cmclist_focus_in (GtkWidget     *widget,
6524 		    GdkEventFocus *event)
6525 {
6526   GtkCMCList *clist = GTK_CMCLIST (widget);
6527 
6528   if (clist->selection_mode == GTK_SELECTION_BROWSE &&
6529       clist->selection == NULL && clist->focus_row > -1)
6530     {
6531       GList *list;
6532 
6533       list = g_list_nth (clist->row_list, clist->focus_row);
6534       if (list && GTK_CMCLIST_ROW (list)->selectable)
6535 	g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
6536 			 clist->focus_row, -1, event);
6537       else
6538 	gtk_cmclist_draw_focus (widget);
6539     }
6540   else
6541     gtk_cmclist_undraw_focus (widget);
6542 
6543   return FALSE;
6544 }
6545 
6546 static gint
gtk_cmclist_focus_out(GtkWidget * widget,GdkEventFocus * event)6547 gtk_cmclist_focus_out (GtkWidget     *widget,
6548 		     GdkEventFocus *event)
6549 {
6550   GtkCMCList *clist = GTK_CMCLIST (widget);
6551 
6552   gtk_cmclist_undraw_focus (widget);
6553 
6554   GTK_CMCLIST_GET_CLASS (widget)->resync_selection (clist, (GdkEvent *) event);
6555 
6556   return FALSE;
6557 }
6558 
6559 static gboolean
focus_column(GtkCMCList * clist,gint column,gint dir)6560 focus_column (GtkCMCList *clist, gint column, gint dir)
6561 {
6562   GtkWidget *child = clist->column[column].button;
6563 
6564   if (gtk_widget_child_focus (child, dir))
6565     {
6566       return TRUE;
6567     }
6568   else if (gtk_widget_get_can_focus (child))
6569     {
6570       gtk_widget_grab_focus (child);
6571       return TRUE;
6572     }
6573 
6574   return FALSE;
6575 }
6576 
6577 /* Focus moved onto the headers. Focus first focusable and visible child.
6578  * (FIXME: focus the last focused child if visible)
6579  */
6580 static gboolean
title_focus_in(GtkCMCList * clist,gint dir)6581 title_focus_in (GtkCMCList *clist, gint dir)
6582 {
6583   gint i;
6584   gint left, right;
6585 
6586   if (!GTK_CMCLIST_SHOW_TITLES (clist))
6587     return FALSE;
6588 
6589   /* Check last focused column */
6590   if (clist->focus_header_column != -1)
6591     {
6592       i = clist->focus_header_column;
6593 
6594       left = COLUMN_LEFT_XPIXEL (clist, i);
6595       right = left + clist->column[i].area.width;
6596 
6597       if (left >= 0 && right <= clist->clist_window_width)
6598 	{
6599 	  if (focus_column (clist, i, dir))
6600 	    return TRUE;
6601 	}
6602     }
6603 
6604   /* Check fully visible columns */
6605   for (i = 0 ; i < clist->columns ; i++)
6606     {
6607       left = COLUMN_LEFT_XPIXEL (clist, i);
6608       right = left + clist->column[i].area.width;
6609 
6610       if (left >= 0 && right <= clist->clist_window_width)
6611 	{
6612 	  if (focus_column (clist, i, dir))
6613 	    return TRUE;
6614 	}
6615     }
6616 
6617   /* Check partially visible columns */
6618   for (i = 0 ; i < clist->columns ; i++)
6619     {
6620       left = COLUMN_LEFT_XPIXEL (clist, i);
6621       right = left + clist->column[i].area.width;
6622 
6623       if ((left < 0 && right > 0) ||
6624 	  (left < clist->clist_window_width && right > clist->clist_window_width))
6625 	{
6626 	  if (focus_column (clist, i, dir))
6627 	    return TRUE;
6628 	}
6629     }
6630 
6631   return FALSE;
6632 }
6633 
6634 /* Move the focus right or left within the title buttons, scrolling
6635  * as necessary to keep the focused child visible.
6636  */
6637 static gboolean
title_focus_move(GtkCMCList * clist,gint dir)6638 title_focus_move (GtkCMCList *clist,
6639 		  gint      dir)
6640 {
6641   GtkWidget *focus_child;
6642   gboolean return_val = FALSE;
6643   gint d = 0;
6644   gint i = -1;
6645   gint j;
6646 
6647   if (!GTK_CMCLIST_SHOW_TITLES(clist))
6648     return FALSE;
6649 
6650   focus_child = gtk_container_get_focus_child (GTK_CONTAINER (clist));
6651   g_assert (focus_child);
6652 
6653   /* Movement direction within headers
6654    */
6655   switch (dir)
6656     {
6657     case GTK_DIR_RIGHT:
6658       d = 1;
6659       break;
6660     case GTK_DIR_LEFT:
6661       d = -1;
6662       break;
6663     }
6664 
6665   for (i = 0; i < clist->columns; i++)
6666     if (clist->column[i].button == focus_child)
6667       break;
6668 
6669   g_assert (i != -1);		/* Have a starting column */
6670 
6671   j = i + d;
6672   while (!return_val && j >= 0 && j < clist->columns)
6673     {
6674       if (clist->column[j].button &&
6675 	  gtk_widget_get_visible (clist->column[j].button))
6676 	{
6677 	  if (focus_column (clist, j, dir))
6678 	    {
6679 	      return_val = TRUE;
6680 	      break;
6681 	    }
6682 	}
6683       j += d;
6684     }
6685 
6686   /* If we didn't find it, wrap around and keep looking
6687    */
6688   if (!return_val)
6689     {
6690       j = d > 0 ? 0 : clist->columns - 1;
6691 
6692       while (!return_val && j != i)
6693 	{
6694 	  if (clist->column[j].button &&
6695 	      gtk_widget_get_visible (clist->column[j].button))
6696 	    {
6697 	      if (focus_column (clist, j, dir))
6698 		{
6699 		  return_val = TRUE;
6700 		  break;
6701 		}
6702 	    }
6703 	  j += d;
6704 	}
6705     }
6706 
6707   /* Scroll horizontally so focused column is visible
6708    */
6709   if (return_val)
6710     {
6711       if (COLUMN_LEFT_XPIXEL (clist, j) < CELL_SPACING + COLUMN_INSET)
6712 	gtk_cmclist_moveto (clist, -1, j, 0, 0);
6713       else if (COLUMN_LEFT_XPIXEL(clist, j) + clist->column[j].area.width >
6714 	       clist->clist_window_width)
6715 	{
6716 	  gint last_column;
6717 
6718 	  for (last_column = clist->columns - 1;
6719 	       last_column >= 0 && !clist->column[last_column].visible; last_column--);
6720 
6721 	  if (j == last_column)
6722 	    gtk_cmclist_moveto (clist, -1, j, 0, 0);
6723 	  else
6724 	    gtk_cmclist_moveto (clist, -1, j, 0, 1);
6725 	}
6726     }
6727   return TRUE;			/* Even if we didn't find a new one, we can keep the
6728 				 * focus in the same place.
6729 				 */
6730 }
6731 
6732 /* PRIVATE SCROLLING FUNCTIONS
6733  *   move_focus_row
6734  *   scroll_horizontal
6735  *   scroll_vertical
6736  *   move_horizontal
6737  *   move_vertical
6738  *   horizontal_timeout
6739  *   vertical_timeout
6740  *   remove_grab
6741  */
6742 static void
move_focus_row(GtkCMCList * clist,GtkScrollType scroll_type,gfloat position)6743 move_focus_row (GtkCMCList      *clist,
6744 		GtkScrollType  scroll_type,
6745 		gfloat         position)
6746 {
6747   GtkWidget *widget;
6748 
6749   cm_return_if_fail (clist != 0);
6750   cm_return_if_fail (GTK_IS_CMCLIST (clist));
6751 
6752   widget = GTK_WIDGET (clist);
6753 
6754   switch (scroll_type)
6755     {
6756     case GTK_SCROLL_STEP_UP:
6757     case GTK_SCROLL_STEP_BACKWARD:
6758       if (clist->focus_row <= 0)
6759 	return;
6760       gtk_cmclist_undraw_focus (widget);
6761       clist->focus_row--;
6762       gtk_cmclist_draw_focus (widget);
6763       break;
6764 
6765     case GTK_SCROLL_STEP_DOWN:
6766     case GTK_SCROLL_STEP_FORWARD:
6767       if (clist->focus_row >= clist->rows - 1)
6768 	return;
6769       gtk_cmclist_undraw_focus (widget);
6770       clist->focus_row++;
6771       gtk_cmclist_draw_focus (widget);
6772       break;
6773     case GTK_SCROLL_PAGE_UP:
6774     case GTK_SCROLL_PAGE_BACKWARD:
6775       if (clist->focus_row <= 0)
6776 	return;
6777       gtk_cmclist_undraw_focus (widget);
6778       clist->focus_row = MAX (0, clist->focus_row -
6779 			      (2 * clist->clist_window_height -
6780 			       clist->row_height - CELL_SPACING) /
6781 			      (2 * (clist->row_height + CELL_SPACING)));
6782       gtk_cmclist_draw_focus (widget);
6783       break;
6784     case GTK_SCROLL_PAGE_DOWN:
6785     case GTK_SCROLL_PAGE_FORWARD:
6786       if (clist->focus_row >= clist->rows - 1)
6787 	return;
6788       gtk_cmclist_undraw_focus (widget);
6789       clist->focus_row = MIN (clist->rows - 1, clist->focus_row +
6790 			      (2 * clist->clist_window_height -
6791 			       clist->row_height - CELL_SPACING) /
6792 			      (2 * (clist->row_height + CELL_SPACING)));
6793       gtk_cmclist_draw_focus (widget);
6794       break;
6795     case GTK_SCROLL_JUMP:
6796       if (position >= 0 && position <= 1)
6797 	{
6798 	  gint row = position * (clist->rows - 1);
6799 
6800 	  if (row == clist->focus_row)
6801 	    return;
6802 
6803 	  gtk_cmclist_undraw_focus (widget);
6804 	  clist->focus_row = row;
6805 	  gtk_cmclist_draw_focus (widget);
6806 	}
6807       break;
6808     default:
6809       break;
6810     }
6811 }
6812 
6813 static void
scroll_horizontal(GtkCMCList * clist,GtkScrollType scroll_type,gfloat position)6814 scroll_horizontal (GtkCMCList      *clist,
6815 		   GtkScrollType  scroll_type,
6816 		   gfloat         position)
6817 {
6818   gint column = 0;
6819   gint last_column;
6820 
6821   cm_return_if_fail (clist != 0);
6822   cm_return_if_fail (GTK_IS_CMCLIST (clist));
6823 
6824   if (clist_has_grab (clist))
6825     return;
6826 
6827   for (last_column = clist->columns - 1;
6828        last_column >= 0 && !clist->column[last_column].visible; last_column--)
6829     ;
6830 
6831   switch (scroll_type)
6832     {
6833     case GTK_SCROLL_STEP_BACKWARD:
6834       column = COLUMN_FROM_XPIXEL (clist, 0);
6835       if (COLUMN_LEFT_XPIXEL (clist, column) - CELL_SPACING - COLUMN_INSET >= 0
6836 	  && column > 0)
6837 	column--;
6838       break;
6839     case GTK_SCROLL_STEP_FORWARD:
6840       column = COLUMN_FROM_XPIXEL (clist, clist->clist_window_width);
6841       if (column < 0)
6842 	return;
6843       if (COLUMN_LEFT_XPIXEL (clist, column) +
6844 	  clist->column[column].area.width +
6845 	  CELL_SPACING + COLUMN_INSET - 1 <= clist->clist_window_width &&
6846 	  column < last_column)
6847 	column++;
6848       break;
6849     case GTK_SCROLL_PAGE_BACKWARD:
6850     case GTK_SCROLL_PAGE_FORWARD:
6851       return;
6852     case GTK_SCROLL_JUMP:
6853       if (position >= 0 && position <= 1)
6854 	{
6855 	  gint vis_columns = 0;
6856 	  gint i;
6857 
6858 	  for (i = 0; i <= last_column; i++)
6859  	    if (clist->column[i].visible)
6860 	      vis_columns++;
6861 
6862 	  column = position * vis_columns;
6863 
6864 	  for (i = 0; i <= last_column && column > 0; i++)
6865 	    if (clist->column[i].visible)
6866 	      column--;
6867 
6868 	  column = i;
6869 	}
6870       else
6871 	return;
6872       break;
6873     default:
6874       break;
6875     }
6876 
6877   if (COLUMN_LEFT_XPIXEL (clist, column) < CELL_SPACING + COLUMN_INSET)
6878     gtk_cmclist_moveto (clist, -1, column, 0, 0);
6879   else if (COLUMN_LEFT_XPIXEL (clist, column) + CELL_SPACING + COLUMN_INSET - 1
6880 	   + clist->column[column].area.width > clist->clist_window_width)
6881     {
6882       if (column == last_column)
6883 	gtk_cmclist_moveto (clist, -1, column, 0, 0);
6884       else
6885 	gtk_cmclist_moveto (clist, -1, column, 0, 1);
6886     }
6887 }
6888 
6889 static void
scroll_vertical(GtkCMCList * clist,GtkScrollType scroll_type,gfloat position)6890 scroll_vertical (GtkCMCList      *clist,
6891 		 GtkScrollType  scroll_type,
6892 		 gfloat         position)
6893 {
6894   gint old_focus_row;
6895 
6896   cm_return_if_fail (GTK_IS_CMCLIST (clist));
6897 
6898   if (clist_has_grab (clist))
6899     return;
6900 
6901   switch (clist->selection_mode)
6902     {
6903     case GTK_SELECTION_MULTIPLE:
6904       if (clist->anchor >= 0)
6905 	return;
6906     case GTK_SELECTION_BROWSE:
6907 
6908       old_focus_row = clist->focus_row;
6909       move_focus_row (clist, scroll_type, position);
6910 
6911       if (old_focus_row != clist->focus_row)
6912 	{
6913 	  if (clist->selection_mode == GTK_SELECTION_BROWSE)
6914 	    g_signal_emit (G_OBJECT (clist), clist_signals[UNSELECT_ROW], 0,
6915 			     old_focus_row, -1, NULL);
6916 	  else if (!GTK_CMCLIST_ADD_MODE(clist))
6917 	    {
6918 	      gtk_cmclist_unselect_all (clist);
6919 	      clist->undo_anchor = old_focus_row;
6920 	    }
6921 	}
6922 
6923       switch (gtk_cmclist_row_is_visible (clist, clist->focus_row))
6924 	{
6925 	case GTK_VISIBILITY_NONE:
6926 	  if (old_focus_row != clist->focus_row &&
6927 	      !(clist->selection_mode == GTK_SELECTION_MULTIPLE &&
6928 		GTK_CMCLIST_ADD_MODE(clist)))
6929 	    g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
6930 			     clist->focus_row, -1, NULL);
6931 	  switch (scroll_type)
6932 	    {
6933             case GTK_SCROLL_PAGE_UP:
6934             case GTK_SCROLL_STEP_UP:
6935 	    case GTK_SCROLL_STEP_BACKWARD:
6936 	    case GTK_SCROLL_PAGE_BACKWARD:
6937 	      gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
6938 	      break;
6939             case GTK_SCROLL_PAGE_DOWN:
6940             case GTK_SCROLL_STEP_DOWN:
6941 	    case GTK_SCROLL_STEP_FORWARD:
6942 	    case GTK_SCROLL_PAGE_FORWARD:
6943 	      gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
6944 	      break;
6945 	    case GTK_SCROLL_JUMP:
6946 	      gtk_cmclist_moveto (clist, clist->focus_row, -1, 0.5, 0);
6947 	      break;
6948 	    default:
6949 	      break;
6950 	    }
6951 	  break;
6952 	case GTK_VISIBILITY_PARTIAL:
6953 	  switch (scroll_type)
6954 	    {
6955 	    case GTK_SCROLL_STEP_BACKWARD:
6956 	    case GTK_SCROLL_PAGE_BACKWARD:
6957 	      gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
6958 	      break;
6959 	    case GTK_SCROLL_STEP_FORWARD:
6960 	    case GTK_SCROLL_PAGE_FORWARD:
6961 	      gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
6962 	      break;
6963 	    case GTK_SCROLL_JUMP:
6964 	      gtk_cmclist_moveto (clist, clist->focus_row, -1, 0.5, 0);
6965 	      break;
6966 	    default:
6967 	      break;
6968 	    }
6969 	  /* fallback is intentional */
6970 	default:
6971 	  if (old_focus_row != clist->focus_row &&
6972 	      !(clist->selection_mode == GTK_SELECTION_MULTIPLE &&
6973 		GTK_CMCLIST_ADD_MODE(clist)))
6974 	    g_signal_emit (G_OBJECT (clist), clist_signals[SELECT_ROW], 0,
6975 			     clist->focus_row, -1, NULL);
6976 	  break;
6977 	}
6978       break;
6979     default:
6980       move_focus_row (clist, scroll_type, position);
6981 
6982       if (ROW_TOP_YPIXEL (clist, clist->focus_row) + clist->row_height >
6983 	  clist->clist_window_height)
6984 	gtk_cmclist_moveto (clist, clist->focus_row, -1, 1, 0);
6985       else if (ROW_TOP_YPIXEL (clist, clist->focus_row) < 0)
6986 	gtk_cmclist_moveto (clist, clist->focus_row, -1, 0, 0);
6987       break;
6988     }
6989 }
6990 
6991 static void
move_horizontal(GtkCMCList * clist,gint diff)6992 move_horizontal (GtkCMCList *clist,
6993 		 gint      diff)
6994 {
6995   gdouble value;
6996 
6997   if (!clist->hadjustment)
6998     return;
6999 
7000   value = CLAMP (gtk_adjustment_get_value (clist->hadjustment) + diff, 0.0,
7001 		 gtk_adjustment_get_upper (clist->hadjustment) -
7002 		 gtk_adjustment_get_page_size (clist->hadjustment));
7003   gtk_adjustment_set_value (clist->hadjustment, value);
7004 }
7005 
7006 static void
move_vertical(GtkCMCList * clist,gint row,gfloat align)7007 move_vertical (GtkCMCList *clist,
7008 	       gint      row,
7009 	       gfloat    align)
7010 {
7011   gdouble value;
7012   gdouble upper;
7013   gdouble page_size;
7014 
7015   if (!clist->vadjustment)
7016     return;
7017 
7018   value = (ROW_TOP_YPIXEL (clist, row) - clist->voffset -
7019 	   align * (clist->clist_window_height - clist->row_height) +
7020 	   (2 * align - 1) * CELL_SPACING);
7021 
7022   upper = gtk_adjustment_get_upper (clist->vadjustment);
7023   page_size = gtk_adjustment_get_page_size (clist->vadjustment);
7024   if ((value + page_size) > upper)
7025 	value = upper - page_size;
7026 
7027   gtk_adjustment_set_value (clist->vadjustment, value);
7028 }
7029 
7030 static void
do_fake_motion(GtkWidget * widget)7031 do_fake_motion (GtkWidget *widget)
7032 {
7033   GdkEvent *event = gdk_event_new (GDK_MOTION_NOTIFY);
7034 
7035   event->motion.send_event = TRUE;
7036 
7037   gtk_cmclist_motion (widget, (GdkEventMotion *)event);
7038   gdk_event_free (event);
7039 }
7040 
7041 static gint
horizontal_timeout(GtkCMCList * clist)7042 horizontal_timeout (GtkCMCList *clist)
7043 {
7044   clist->htimer = 0;
7045   do_fake_motion (GTK_WIDGET (clist));
7046 
7047   return FALSE;
7048 }
7049 
7050 static gint
vertical_timeout(GtkCMCList * clist)7051 vertical_timeout (GtkCMCList *clist)
7052 {
7053   clist->vtimer = 0;
7054   do_fake_motion (GTK_WIDGET (clist));
7055 
7056   return FALSE;
7057 }
7058 
7059 static void
remove_grab(GtkCMCList * clist)7060 remove_grab (GtkCMCList *clist)
7061 {
7062   GtkWidget *widget = GTK_WIDGET (clist);
7063 
7064   if (gtk_widget_has_grab (widget))
7065     {
7066       GdkDisplay *display = gtk_widget_get_display (widget);
7067 
7068       gtk_grab_remove (widget);
7069       if (gdk_display_pointer_is_grabbed (display))
7070 	gdk_display_pointer_ungrab (display, GDK_CURRENT_TIME);
7071     }
7072 
7073   if (clist->htimer)
7074     {
7075       g_source_remove (clist->htimer);
7076       clist->htimer = 0;
7077     }
7078 
7079   if (clist->vtimer)
7080     {
7081       g_source_remove (clist->vtimer);
7082       clist->vtimer = 0;
7083     }
7084 }
7085 
7086 /* PUBLIC SORTING FUNCTIONS
7087  * gtk_cmclist_sort
7088  * gtk_cmclist_set_compare_func
7089  * gtk_cmclist_set_auto_sort
7090  * gtk_cmclist_set_sort_type
7091  * gtk_cmclist_set_sort_column
7092  */
7093 void
gtk_cmclist_sort(GtkCMCList * clist)7094 gtk_cmclist_sort (GtkCMCList *clist)
7095 {
7096   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7097 
7098   GTK_CMCLIST_GET_CLASS (clist)->sort_list (clist);
7099 }
7100 
7101 void
gtk_cmclist_set_compare_func(GtkCMCList * clist,GtkCMCListCompareFunc cmp_func)7102 gtk_cmclist_set_compare_func (GtkCMCList            *clist,
7103 			    GtkCMCListCompareFunc  cmp_func)
7104 {
7105   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7106 
7107   clist->compare = (cmp_func) ? cmp_func : default_compare;
7108 }
7109 
7110 void
gtk_cmclist_set_auto_sort(GtkCMCList * clist,gboolean auto_sort)7111 gtk_cmclist_set_auto_sort (GtkCMCList *clist,
7112 			 gboolean  auto_sort)
7113 {
7114   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7115 
7116   if (GTK_CMCLIST_AUTO_SORT(clist) && !auto_sort)
7117     GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_AUTO_SORT);
7118   else if (!GTK_CMCLIST_AUTO_SORT(clist) && auto_sort)
7119     {
7120       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_AUTO_SORT);
7121       gtk_cmclist_sort (clist);
7122     }
7123 }
7124 
7125 void
gtk_cmclist_set_sort_type(GtkCMCList * clist,GtkSortType sort_type)7126 gtk_cmclist_set_sort_type (GtkCMCList    *clist,
7127 			 GtkSortType  sort_type)
7128 {
7129   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7130 
7131   clist->sort_type = sort_type;
7132 }
7133 
7134 void
gtk_cmclist_set_sort_column(GtkCMCList * clist,gint column)7135 gtk_cmclist_set_sort_column (GtkCMCList *clist,
7136 			   gint      column)
7137 {
7138   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7139 
7140   if (column < 0 || column >= clist->columns)
7141     return;
7142 
7143   clist->sort_column = column;
7144 }
7145 
7146 /* PRIVATE SORTING FUNCTIONS
7147  *   default_compare
7148  *   real_sort_list
7149  *   gtk_cmclist_merge
7150  *   gtk_cmclist_mergesort
7151  */
7152 static gint
default_compare(GtkCMCList * clist,gconstpointer ptr1,gconstpointer ptr2)7153 default_compare (GtkCMCList      *clist,
7154 		 gconstpointer  ptr1,
7155 		 gconstpointer  ptr2)
7156 {
7157   char *text1 = NULL;
7158   char *text2 = NULL;
7159 
7160   GtkCMCListRow *row1 = (GtkCMCListRow *) ptr1;
7161   GtkCMCListRow *row2 = (GtkCMCListRow *) ptr2;
7162 
7163   switch (row1->cell[clist->sort_column].type)
7164     {
7165     case GTK_CMCELL_TEXT:
7166       text1 = GTK_CMCELL_TEXT (row1->cell[clist->sort_column])->text;
7167       break;
7168     case GTK_CMCELL_PIXTEXT:
7169       text1 = GTK_CMCELL_PIXTEXT (row1->cell[clist->sort_column])->text;
7170       break;
7171     default:
7172       break;
7173     }
7174 
7175   switch (row2->cell[clist->sort_column].type)
7176     {
7177     case GTK_CMCELL_TEXT:
7178       text2 = GTK_CMCELL_TEXT (row2->cell[clist->sort_column])->text;
7179       break;
7180     case GTK_CMCELL_PIXTEXT:
7181       text2 = GTK_CMCELL_PIXTEXT (row2->cell[clist->sort_column])->text;
7182       break;
7183     default:
7184       break;
7185     }
7186 
7187   if (!text2)
7188     return (text1 != NULL);
7189 
7190   if (!text1)
7191     return -1;
7192 
7193   return strcmp (text1, text2);
7194 }
7195 
7196 static void
real_sort_list(GtkCMCList * clist)7197 real_sort_list (GtkCMCList *clist)
7198 {
7199   GList *list;
7200   GList *work;
7201   gint i;
7202 
7203   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7204 
7205   if (clist->rows <= 1)
7206     return;
7207 
7208   if (clist_has_grab (clist))
7209     return;
7210 
7211   gtk_cmclist_freeze (clist);
7212 
7213   if (clist->anchor != -1 && clist->selection_mode == GTK_SELECTION_MULTIPLE)
7214     {
7215       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
7216       g_list_free (clist->undo_selection);
7217       g_list_free (clist->undo_unselection);
7218       clist->undo_selection = NULL;
7219       clist->undo_unselection = NULL;
7220     }
7221 
7222   clist->row_list = gtk_cmclist_mergesort (clist, clist->row_list, clist->rows);
7223 
7224   work = clist->selection;
7225 
7226   for (i = 0, list = clist->row_list; i < clist->rows; i++, list = list->next)
7227     {
7228       if (GTK_CMCLIST_ROW (list)->state == GTK_STATE_SELECTED)
7229 	{
7230 	  work->data = GINT_TO_POINTER (i);
7231 	  work = work->next;
7232 	}
7233 
7234       if (i == clist->rows - 1)
7235 	clist->row_list_end = list;
7236     }
7237 
7238   gtk_cmclist_thaw (clist);
7239 }
7240 
7241 static GList *
gtk_cmclist_merge(GtkCMCList * clist,GList * a,GList * b)7242 gtk_cmclist_merge (GtkCMCList *clist,
7243 		 GList    *a,         /* first list to merge */
7244 		 GList    *b)         /* second list to merge */
7245 {
7246   GList z = { 0 };                    /* auxiliary node */
7247   GList *c;
7248   gint cmp;
7249 
7250   c = &z;
7251 
7252   if (!a && !b)
7253 	return NULL;
7254 
7255   while (a || b)
7256     {
7257       if (a && !b)
7258 	{
7259 	  c->next = a;
7260 	  a->prev = c;
7261 	  c = a;
7262 	  a = a->next;
7263 	  break;
7264 	}
7265       else if (!a && b)
7266 	{
7267 	  c->next = b;
7268 	  b->prev = c;
7269 	  c = b;
7270 	  b = b->next;
7271 	  break;
7272 	}
7273       else /* a && b */
7274 	{
7275 	  cmp = clist->compare (clist, GTK_CMCLIST_ROW (a), GTK_CMCLIST_ROW (b));
7276 	  if ((cmp >= 0 && clist->sort_type == GTK_SORT_DESCENDING) ||
7277 	      (cmp <= 0 && clist->sort_type == GTK_SORT_ASCENDING))
7278 	    {
7279 	      c->next = a;
7280 	      a->prev = c;
7281 	      c = a;
7282 	      a = a->next;
7283 	    }
7284 	  else
7285 	    {
7286 	      c->next = b;
7287 	      b->prev = c;
7288 	      c = b;
7289 	      b = b->next;
7290 	    }
7291 	}
7292     }
7293 
7294   if (z.next)
7295 	z.next->prev = NULL;
7296 
7297   return z.next;
7298 }
7299 
7300 static GList *
gtk_cmclist_mergesort(GtkCMCList * clist,GList * list,gint num)7301 gtk_cmclist_mergesort (GtkCMCList *clist,
7302 		     GList    *list,         /* the list to sort */
7303 		     gint      num)          /* the list's length */
7304 {
7305   GList *half;
7306   gint i;
7307 
7308   if (num <= 1)
7309     {
7310       return list;
7311     }
7312   else
7313     {
7314       /* move "half" to the middle */
7315       half = list;
7316       for (i = 0; i < num / 2; i++)
7317 	half = half->next;
7318 
7319       /* cut the list in two */
7320       half->prev->next = NULL;
7321       half->prev = NULL;
7322 
7323       /* recursively sort both lists */
7324       return gtk_cmclist_merge (clist,
7325 		       gtk_cmclist_mergesort (clist, list, num / 2),
7326 		       gtk_cmclist_mergesort (clist, half, num - num / 2));
7327     }
7328 }
7329 
7330 /************************/
7331 
7332 static void
drag_source_info_destroy(gpointer data)7333 drag_source_info_destroy (gpointer data)
7334 {
7335   GtkCMCListCellInfo *info = data;
7336 
7337   g_free (info);
7338 }
7339 
7340 static void
drag_dest_info_destroy(gpointer data)7341 drag_dest_info_destroy (gpointer data)
7342 {
7343   GtkCMCListDestInfo *info = data;
7344 
7345   g_free (info);
7346 }
7347 
7348 static void
drag_dest_cell(GtkCMCList * clist,gint x,gint y,GtkCMCListDestInfo * dest_info)7349 drag_dest_cell (GtkCMCList         *clist,
7350 		gint              x,
7351 		gint              y,
7352 		GtkCMCListDestInfo *dest_info)
7353 {
7354   GtkWidget *widget;
7355   GtkStyle *style;
7356   guint border_width;
7357 
7358   widget = GTK_WIDGET (clist);
7359   style = gtk_widget_get_style (widget);
7360 
7361   dest_info->insert_pos = GTK_CMCLIST_DRAG_NONE;
7362 
7363   border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
7364   y -= (border_width +
7365 	style->ythickness +
7366 	clist->column_title_area.height);
7367 
7368   dest_info->cell.row = ROW_FROM_YPIXEL (clist, y);
7369   if (dest_info->cell.row >= clist->rows)
7370     {
7371       dest_info->cell.row = clist->rows - 1;
7372       y = ROW_TOP_YPIXEL (clist, dest_info->cell.row) + clist->row_height;
7373     }
7374   if (dest_info->cell.row < -1)
7375     dest_info->cell.row = -1;
7376 
7377   x -= border_width + style->xthickness;
7378 
7379   dest_info->cell.column = COLUMN_FROM_XPIXEL (clist, x);
7380 
7381   if (dest_info->cell.row >= 0)
7382     {
7383       gint y_delta;
7384       gint h = 0;
7385 
7386       y_delta = y - ROW_TOP_YPIXEL (clist, dest_info->cell.row);
7387 
7388       if (GTK_CMCLIST_DRAW_DRAG_RECT(clist))
7389 	{
7390 	  dest_info->insert_pos = GTK_CMCLIST_DRAG_INTO;
7391 	  h = clist->row_height / 4;
7392 	}
7393       else if (GTK_CMCLIST_DRAW_DRAG_LINE(clist))
7394 	{
7395 	  dest_info->insert_pos = GTK_CMCLIST_DRAG_BEFORE;
7396 	  h = clist->row_height / 2;
7397 	}
7398 
7399       if (GTK_CMCLIST_DRAW_DRAG_LINE(clist))
7400 	{
7401 	  if (y_delta < h)
7402 	    dest_info->insert_pos = GTK_CMCLIST_DRAG_BEFORE;
7403 	  else if (clist->row_height - y_delta < h)
7404 	    dest_info->insert_pos = GTK_CMCLIST_DRAG_AFTER;
7405 	}
7406     }
7407 }
7408 
7409 static void
gtk_cmclist_drag_begin(GtkWidget * widget,GdkDragContext * context)7410 gtk_cmclist_drag_begin (GtkWidget	     *widget,
7411 		      GdkDragContext *context)
7412 {
7413   GtkCMCList *clist;
7414   GtkCMCListCellInfo *info;
7415 
7416   cm_return_if_fail (GTK_IS_CMCLIST (widget));
7417   cm_return_if_fail (context != NULL);
7418 
7419   clist = GTK_CMCLIST (widget);
7420 
7421   clist->drag_button = 0;
7422   remove_grab (clist);
7423 
7424   switch (clist->selection_mode)
7425     {
7426     case GTK_SELECTION_MULTIPLE:
7427       update_extended_selection (clist, clist->focus_row);
7428       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
7429       break;
7430     case GTK_SELECTION_SINGLE:
7431       clist->anchor = -1;
7432     case GTK_SELECTION_BROWSE:
7433       break;
7434     default:
7435       g_assert_not_reached ();
7436     }
7437 
7438   info = g_dataset_get_data (context, "gtk-clist-drag-source");
7439 
7440   if (!info)
7441     {
7442       info = g_new (GtkCMCListCellInfo, 1);
7443 
7444       if (clist->click_cell.row < 0)
7445 	clist->click_cell.row = 0;
7446       else if (clist->click_cell.row >= clist->rows)
7447 	clist->click_cell.row = clist->rows - 1;
7448       info->row = clist->click_cell.row;
7449       info->column = clist->click_cell.column;
7450 
7451       g_dataset_set_data_full (context, "gtk-clist-drag-source", info,
7452 			       drag_source_info_destroy);
7453     }
7454 
7455   if (GTK_CMCLIST_USE_DRAG_ICONS (clist))
7456     gtk_drag_set_icon_default (context);
7457 }
7458 
7459 static void
gtk_cmclist_drag_end(GtkWidget * widget,GdkDragContext * context)7460 gtk_cmclist_drag_end (GtkWidget	   *widget,
7461 		    GdkDragContext *context)
7462 {
7463   GtkCMCList *clist;
7464 
7465   cm_return_if_fail (GTK_IS_CMCLIST (widget));
7466   cm_return_if_fail (context != NULL);
7467 
7468   clist = GTK_CMCLIST (widget);
7469 
7470   clist->click_cell.row = -1;
7471   clist->click_cell.column = -1;
7472 }
7473 
7474 static void
gtk_cmclist_drag_leave(GtkWidget * widget,GdkDragContext * context,guint time)7475 gtk_cmclist_drag_leave (GtkWidget      *widget,
7476 		      GdkDragContext *context,
7477 		      guint           time)
7478 {
7479   GtkCMCList *clist;
7480   GtkCMCListDestInfo *dest_info;
7481 
7482   cm_return_if_fail (GTK_IS_CMCLIST (widget));
7483   cm_return_if_fail (context != NULL);
7484 
7485   clist = GTK_CMCLIST (widget);
7486 
7487   dest_info = g_dataset_get_data (context, "gtk-clist-drag-dest");
7488 
7489   if (dest_info)
7490     {
7491       if (dest_info->cell.row >= 0 &&
7492 	  GTK_CMCLIST_REORDERABLE(clist) &&
7493 	  gtk_drag_get_source_widget (context) == widget)
7494 	{
7495 	  GdkAtom atom = gdk_atom_intern_static_string ("gtk-clist-drag-reorder");
7496 	  GdkAtom found = gtk_drag_dest_find_target(widget, context, NULL);
7497 
7498 	      if (atom == found)
7499 		{
7500 		  clist->drag_highlight_row = -1;
7501 		}
7502 	}
7503       g_dataset_remove_data (context, "gtk-clist-drag-dest");
7504     }
7505 }
7506 
7507 static gint
gtk_cmclist_drag_motion(GtkWidget * widget,GdkDragContext * context,gint x,gint y,guint time)7508 gtk_cmclist_drag_motion (GtkWidget      *widget,
7509 		       GdkDragContext *context,
7510 		       gint            x,
7511 		       gint            y,
7512 		       guint           time)
7513 {
7514   GtkCMCList *clist;
7515   GtkCMCListDestInfo new_info;
7516   GtkCMCListDestInfo *dest_info;
7517 
7518   cm_return_val_if_fail (GTK_IS_CMCLIST (widget), FALSE);
7519 
7520   clist = GTK_CMCLIST (widget);
7521 
7522   dest_info = g_dataset_get_data (context, "gtk-clist-drag-dest");
7523 
7524   if (!dest_info)
7525     {
7526       dest_info = g_new (GtkCMCListDestInfo, 1);
7527 
7528       dest_info->insert_pos  = GTK_CMCLIST_DRAG_NONE;
7529       dest_info->cell.row    = -1;
7530       dest_info->cell.column = -1;
7531 
7532       g_dataset_set_data_full (context, "gtk-clist-drag-dest", dest_info,
7533 			       drag_dest_info_destroy);
7534     }
7535 
7536   drag_dest_cell (clist, x, y, &new_info);
7537 
7538   if (GTK_CMCLIST_REORDERABLE (clist))
7539     {
7540       GdkAtom atom = gdk_atom_intern_static_string ("gtk-clist-drag-reorder");
7541       GdkAtom found = gtk_drag_dest_find_target(widget, context, NULL);
7542 
7543       if (atom == found)
7544 	{
7545 	  if (gtk_drag_get_source_widget (context) != widget ||
7546 	      new_info.insert_pos == GTK_CMCLIST_DRAG_NONE ||
7547 	      new_info.cell.row == clist->click_cell.row ||
7548 	      (new_info.cell.row == clist->click_cell.row - 1 &&
7549 	       new_info.insert_pos == GTK_CMCLIST_DRAG_AFTER) ||
7550 	      (new_info.cell.row == clist->click_cell.row + 1 &&
7551 	       new_info.insert_pos == GTK_CMCLIST_DRAG_BEFORE))
7552 	    {
7553 	      if (dest_info->cell.row < 0)
7554 		{
7555 		  gdk_drag_status (context, GDK_ACTION_DEFAULT, time);
7556 		  return FALSE;
7557 		}
7558 	      return TRUE;
7559 	    }
7560 
7561 	  if (new_info.cell.row != dest_info->cell.row ||
7562 	      (new_info.cell.row == dest_info->cell.row &&
7563 	       dest_info->insert_pos != new_info.insert_pos))
7564 	    {
7565 
7566 	      dest_info->insert_pos  = new_info.insert_pos;
7567 	      dest_info->cell.row    = new_info.cell.row;
7568 	      dest_info->cell.column = new_info.cell.column;
7569 
7570 	      clist->drag_highlight_row = dest_info->cell.row;
7571 	      clist->drag_highlight_pos = dest_info->insert_pos;
7572 
7573 	      gdk_drag_status (context,
7574 		gdk_drag_context_get_suggested_action(context), time);
7575 	    }
7576 	  return TRUE;
7577 	}
7578     }
7579 
7580   dest_info->insert_pos  = new_info.insert_pos;
7581   dest_info->cell.row    = new_info.cell.row;
7582   dest_info->cell.column = new_info.cell.column;
7583   return TRUE;
7584 }
7585 
7586 static gboolean
gtk_cmclist_drag_drop(GtkWidget * widget,GdkDragContext * context,gint x,gint y,guint time)7587 gtk_cmclist_drag_drop (GtkWidget      *widget,
7588 		     GdkDragContext *context,
7589 		     gint            x,
7590 		     gint            y,
7591 		     guint           time)
7592 {
7593   cm_return_val_if_fail (GTK_IS_CMCLIST (widget), FALSE);
7594   cm_return_val_if_fail (context != NULL, FALSE);
7595 
7596   if (GTK_CMCLIST_REORDERABLE (widget) &&
7597       gtk_drag_get_source_widget (context) == widget)
7598     {
7599       GdkAtom atom = gdk_atom_intern_static_string ("gtk-clist-drag-reorder");
7600       GdkAtom found = gtk_drag_dest_find_target(widget, context, NULL);
7601 
7602 	  if (atom == found)
7603 	    return TRUE;
7604     }
7605   return FALSE;
7606 }
7607 
7608 static void
gtk_cmclist_drag_data_received(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * selection_data,guint info,guint time)7609 gtk_cmclist_drag_data_received (GtkWidget        *widget,
7610 			      GdkDragContext   *context,
7611 			      gint              x,
7612 			      gint              y,
7613 			      GtkSelectionData *selection_data,
7614 			      guint             info,
7615 			      guint             time)
7616 {
7617   GtkCMCList *clist;
7618 
7619   cm_return_if_fail (GTK_IS_CMCLIST (widget));
7620   cm_return_if_fail (context != NULL);
7621   cm_return_if_fail (selection_data != NULL);
7622 
7623   clist = GTK_CMCLIST (widget);
7624 
7625   if (GTK_CMCLIST_REORDERABLE (clist) &&
7626       gtk_drag_get_source_widget (context) == widget &&
7627       gtk_selection_data_get_target (selection_data) ==
7628       gdk_atom_intern_static_string ("gtk-clist-drag-reorder") &&
7629       gtk_selection_data_get_format (selection_data) == 8 &&
7630       gtk_selection_data_get_length (selection_data) == sizeof (GtkCMCListCellInfo))
7631     {
7632       GtkCMCListCellInfo *source_info;
7633 
7634       source_info = (GtkCMCListCellInfo *)(gtk_selection_data_get_data (selection_data));
7635       if (source_info)
7636 	{
7637 	  GtkCMCListDestInfo dest_info;
7638 
7639 	  drag_dest_cell (clist, x, y, &dest_info);
7640 
7641 	  if (dest_info.insert_pos == GTK_CMCLIST_DRAG_AFTER)
7642 	    dest_info.cell.row++;
7643 	  if (source_info->row < dest_info.cell.row)
7644 	    dest_info.cell.row--;
7645 	  if (dest_info.cell.row != source_info->row)
7646 	    gtk_cmclist_row_move (clist, source_info->row, dest_info.cell.row);
7647 
7648 	  g_dataset_remove_data (context, "gtk-clist-drag-dest");
7649 	}
7650     }
7651 }
7652 
7653 static void
gtk_cmclist_drag_data_get(GtkWidget * widget,GdkDragContext * context,GtkSelectionData * selection_data,guint info,guint time)7654 gtk_cmclist_drag_data_get (GtkWidget        *widget,
7655 			 GdkDragContext   *context,
7656 			 GtkSelectionData *selection_data,
7657 			 guint             info,
7658 			 guint             time)
7659 {
7660   GdkAtom target;
7661   cm_return_if_fail (GTK_IS_CMCLIST (widget));
7662   cm_return_if_fail (context != NULL);
7663   cm_return_if_fail (selection_data != NULL);
7664 
7665   target = gtk_selection_data_get_target (selection_data);
7666   if (target == gdk_atom_intern_static_string ("gtk-clist-drag-reorder"))
7667     {
7668       GtkCMCListCellInfo *info;
7669 
7670       info = g_dataset_get_data (context, "gtk-clist-drag-source");
7671 
7672       if (info)
7673 	{
7674 	  GtkCMCListCellInfo ret_info;
7675 
7676 	  ret_info.row = info->row;
7677 	  ret_info.column = info->column;
7678 
7679 	  gtk_selection_data_set (selection_data, target,
7680 				  8, (guchar *) &ret_info,
7681 				  sizeof (GtkCMCListCellInfo));
7682 	}
7683     }
7684 }
7685 
7686 void
gtk_cmclist_set_reorderable(GtkCMCList * clist,gboolean reorderable)7687 gtk_cmclist_set_reorderable (GtkCMCList *clist,
7688 			   gboolean  reorderable)
7689 {
7690   GtkWidget *widget;
7691 
7692   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7693 
7694   if ((GTK_CMCLIST_REORDERABLE(clist) != 0) == reorderable)
7695     return;
7696 
7697   widget = GTK_WIDGET (clist);
7698 
7699   if (reorderable)
7700     {
7701       GTK_CMCLIST_SET_FLAG (clist, CMCLIST_REORDERABLE);
7702       gtk_drag_dest_set (widget,
7703 			 GTK_DEST_DEFAULT_MOTION | GTK_DEST_DEFAULT_DROP,
7704 			 &clist_target_table, 1, GDK_ACTION_MOVE);
7705     }
7706   else
7707     {
7708       GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_REORDERABLE);
7709       gtk_drag_dest_unset (GTK_WIDGET (clist));
7710     }
7711 }
7712 
7713 void
gtk_cmclist_set_use_drag_icons(GtkCMCList * clist,gboolean use_icons)7714 gtk_cmclist_set_use_drag_icons (GtkCMCList *clist,
7715 			      gboolean  use_icons)
7716 {
7717   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7718 
7719   if (use_icons != 0)
7720     GTK_CMCLIST_SET_FLAG (clist, CMCLIST_USE_DRAG_ICONS);
7721   else
7722     GTK_CMCLIST_UNSET_FLAG (clist, CMCLIST_USE_DRAG_ICONS);
7723 }
7724 
7725 void
gtk_cmclist_set_button_actions(GtkCMCList * clist,guint button,guint8 button_actions)7726 gtk_cmclist_set_button_actions (GtkCMCList *clist,
7727 			      guint     button,
7728 			      guint8    button_actions)
7729 {
7730   cm_return_if_fail (GTK_IS_CMCLIST (clist));
7731 
7732   if (button < MAX_BUTTON)
7733     {
7734       if (gdk_display_pointer_is_grabbed (gtk_widget_get_display (GTK_WIDGET (clist))) ||
7735 	  gtk_widget_has_grab (GTK_WIDGET(clist)))
7736 	{
7737 	  remove_grab (clist);
7738 	  clist->drag_button = 0;
7739 	}
7740 
7741       GTK_CMCLIST_GET_CLASS (clist)->resync_selection (clist, NULL);
7742 
7743       clist->button_actions[button] = button_actions;
7744     }
7745 }
7746