4 * Collection - a GTK+ widget
5 * Copyright (C) 2005, the ROX-Filer team.
7 * The collection widget provides an area for displaying a collection of
8 * objects (such as files). It allows the user to choose a selection of
9 * them and provides signals to allow popping up menus, detecting
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the Free
14 * Software Foundation; either version 2 of the License, or (at your option)
17 * This program is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
22 * You should have received a copy of the GNU General Public License along with
23 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
24 * Place, Suite 330, Boston, MA 02111-1307 USA
32 #include <gdk/gdkkeysyms.h>
35 #include "collection.h"
39 #define MINIMUM_ITEMS 16
41 #define MAX_WINKS 5 /* Should be an odd number */
43 /* Macro to emit the "selection_changed" signal only if allowed */
44 #define EMIT_SELECTION_CHANGED(collection, time) \
45 if (!collection->block_selection_changed) \
46 g_signal_emit(collection, \
47 collection_signals[SELECTION_CHANGED], 0, time)
57 * void gain_selection(collection, time, user_data)
58 * We've gone from no selected items to having a selection.
59 * Time is the time of the event that caused the change, or
60 * GDK_CURRENT_TIME if not known.
62 * void lose_selection(collection, time, user_data)
63 * We've dropped to having no selected items.
64 * Time is the time of the event that caused the change, or
65 * GDK_CURRENT_TIME if not known.
67 * void selection_changed(collection, user_data)
68 * The set of selected items has changed.
69 * Time is the time of the event that caused the change, or
70 * GDK_CURRENT_TIME if not known.
80 static guint collection_signals
[LAST_SIGNAL
] = { 0 };
82 static guint32 current_event_time
= GDK_CURRENT_TIME
;
84 static GtkWidgetClass
*parent_class
= NULL
;
86 /* Static prototypes */
87 static void draw_one_item(Collection
*collection
,
90 static void collection_class_init(GObjectClass
*gclass
, gpointer data
);
91 static void collection_init(GTypeInstance
*object
, gpointer g_class
);
92 static void collection_destroy(GtkObject
*object
);
93 static void collection_finalize(GObject
*object
);
94 static void collection_realize(GtkWidget
*widget
);
95 static void collection_map(GtkWidget
*widget
);
96 static void collection_size_request(GtkWidget
*widget
,
97 GtkRequisition
*requisition
);
98 static void collection_size_allocate(GtkWidget
*widget
,
99 GtkAllocation
*allocation
);
100 static void collection_set_adjustment(Collection
*collection
,
101 GtkAdjustment
*vadj
);
102 static void collection_get_property(GObject
*object
,
106 static void collection_set_property(GObject
*object
,
110 static gint
collection_expose(GtkWidget
*widget
, GdkEventExpose
*event
);
111 static void default_draw_item(GtkWidget
*widget
,
112 CollectionItem
*data
,
115 static gboolean
default_test_point(Collection
*collection
,
116 int point_x
, int point_y
,
117 CollectionItem
*data
,
118 int width
, int height
,
120 static gint
collection_motion_notify(GtkWidget
*widget
,
121 GdkEventMotion
*event
);
122 static void add_lasso_box(Collection
*collection
);
123 static void abort_lasso(Collection
*collection
);
124 static void remove_lasso_box(Collection
*collection
);
125 static void draw_lasso_box(Collection
*collection
);
126 static void cancel_wink(Collection
*collection
);
127 static gint
collection_key_press(GtkWidget
*widget
, GdkEventKey
*event
);
128 static void get_visible_limits(Collection
*collection
, int *first
, int *last
);
129 static void scroll_to_show(Collection
*collection
, int item
);
130 static void collection_item_set_selected(Collection
*collection
,
134 static gint
collection_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
);
136 static void draw_focus_at(Collection
*collection
, GdkRectangle
*area
)
141 widget
= GTK_WIDGET(collection
);
143 if (GTK_WIDGET_FLAGS(widget
) & GTK_HAS_FOCUS
)
144 state
= GTK_STATE_ACTIVE
;
146 state
= GTK_STATE_INSENSITIVE
;
148 gtk_paint_focus(widget
->style
,
155 collection
->item_width
,
159 static void draw_one_item(Collection
*collection
, int item
, GdkRectangle
*area
)
161 if (item
< collection
->number_of_items
)
163 collection
->draw_item((GtkWidget
*) collection
,
164 &collection
->items
[item
],
165 area
, collection
->cb_user_data
);
168 if (item
== collection
->cursor_item
)
169 draw_focus_at(collection
, area
);
172 GType
collection_get_type(void)
174 static GType my_type
= 0;
178 static const GTypeInfo info
=
180 sizeof(CollectionClass
),
181 NULL
, /* base_init */
182 NULL
, /* base_finalise */
183 (GClassInitFunc
) collection_class_init
,
184 NULL
, /* class_finalise */
185 NULL
, /* class_data */
191 my_type
= g_type_register_static(gtk_widget_get_type(),
192 "Collection", &info
, 0);
198 typedef void (*FinalizeFn
)(GObject
*object
);
200 static void collection_class_init(GObjectClass
*gclass
, gpointer data
)
202 CollectionClass
*collection_class
= (CollectionClass
*) gclass
;
203 GtkObjectClass
*object_class
= (GtkObjectClass
*) gclass
;
204 GtkWidgetClass
*widget_class
= (GtkWidgetClass
*) gclass
;
206 parent_class
= gtk_type_class(gtk_widget_get_type());
208 object_class
->destroy
= collection_destroy
;
209 G_OBJECT_CLASS(object_class
)->finalize
=
210 (FinalizeFn
) collection_finalize
;
212 widget_class
->realize
= collection_realize
;
213 widget_class
->expose_event
= collection_expose
;
214 widget_class
->size_request
= collection_size_request
;
215 widget_class
->size_allocate
= collection_size_allocate
;
217 widget_class
->key_press_event
= collection_key_press
;
219 widget_class
->motion_notify_event
= collection_motion_notify
;
220 widget_class
->map
= collection_map
;
221 widget_class
->scroll_event
= collection_scroll_event
;
223 gclass
->set_property
= collection_set_property
;
224 gclass
->get_property
= collection_get_property
;
226 collection_class
->gain_selection
= NULL
;
227 collection_class
->lose_selection
= NULL
;
228 collection_class
->selection_changed
= NULL
;
230 collection_signals
[GAIN_SELECTION
] = g_signal_new("gain_selection",
231 G_TYPE_FROM_CLASS(gclass
),
233 G_STRUCT_OFFSET(CollectionClass
,
236 g_cclosure_marshal_VOID__INT
,
240 collection_signals
[LOSE_SELECTION
] = g_signal_new("lose_selection",
241 G_TYPE_FROM_CLASS(gclass
),
243 G_STRUCT_OFFSET(CollectionClass
,
246 g_cclosure_marshal_VOID__INT
,
250 collection_signals
[SELECTION_CHANGED
] = g_signal_new(
252 G_TYPE_FROM_CLASS(gclass
),
254 G_STRUCT_OFFSET(CollectionClass
,
257 g_cclosure_marshal_VOID__INT
,
261 g_object_class_install_property(gclass
,
263 g_param_spec_object("vadjustment",
264 "Vertical Adjustment",
265 "The GtkAdjustment for the vertical position.",
267 G_PARAM_READWRITE
| G_PARAM_CONSTRUCT
));
270 static void collection_init(GTypeInstance
*instance
, gpointer g_class
)
272 Collection
*object
= (Collection
*) instance
;
274 g_return_if_fail(object
!= NULL
);
275 g_return_if_fail(IS_COLLECTION(object
));
277 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(object
), GTK_CAN_FOCUS
);
279 object
->number_of_items
= 0;
280 object
->number_selected
= 0;
281 object
->block_selection_changed
= 0;
283 object
->item_width
= 64;
284 object
->item_height
= 64;
287 object
->items
= g_new(CollectionItem
, MINIMUM_ITEMS
);
288 object
->cursor_item
= -1;
289 object
->cursor_item_old
= -1;
290 object
->wink_item
= -1;
291 object
->wink_on_map
= -1;
292 object
->array_size
= MINIMUM_ITEMS
;
293 object
->draw_item
= default_draw_item
;
294 object
->test_point
= default_test_point
;
295 object
->free_item
= NULL
;
298 GtkWidget
* collection_new(void)
300 return GTK_WIDGET(gtk_widget_new(collection_get_type(), NULL
));
303 /* After this we are unusable, but our data (if any) is still hanging around.
304 * It will be freed later with finalize.
306 static void collection_destroy(GtkObject
*object
)
308 Collection
*collection
;
310 g_return_if_fail(object
!= NULL
);
311 g_return_if_fail(IS_COLLECTION(object
));
313 collection
= COLLECTION(object
);
315 collection_clear(collection
);
317 if (collection
->vadj
)
319 g_object_unref(G_OBJECT(collection
->vadj
));
320 collection
->vadj
= NULL
;
323 if (GTK_OBJECT_CLASS(parent_class
)->destroy
)
324 (*GTK_OBJECT_CLASS(parent_class
)->destroy
)(object
);
327 /* This is the last thing that happens to us. Free all data. */
328 static void collection_finalize(GObject
*object
)
330 Collection
*collection
;
332 collection
= COLLECTION(object
);
334 g_return_if_fail(collection
->number_of_items
== 0);
336 g_free(collection
->items
);
338 if (G_OBJECT_CLASS(parent_class
)->finalize
)
339 G_OBJECT_CLASS(parent_class
)->finalize(object
);
342 static void collection_map(GtkWidget
*widget
)
344 Collection
*collection
= COLLECTION(widget
);
346 if (GTK_WIDGET_CLASS(parent_class
)->map
)
347 (*GTK_WIDGET_CLASS(parent_class
)->map
)(widget
);
349 if (collection
->wink_on_map
>= 0)
351 collection_wink_item(collection
, collection
->wink_on_map
);
352 collection
->wink_on_map
= -1;
356 static void collection_realize(GtkWidget
*widget
)
358 Collection
*collection
;
359 GdkWindowAttr attributes
;
360 gint attributes_mask
;
361 GdkGCValues xor_values
;
364 g_return_if_fail(widget
!= NULL
);
365 g_return_if_fail(IS_COLLECTION(widget
));
366 g_return_if_fail(widget
->parent
!= NULL
);
368 GTK_WIDGET_SET_FLAGS(widget
, GTK_REALIZED
);
369 collection
= COLLECTION(widget
);
371 attributes
.x
= widget
->allocation
.x
;
372 attributes
.y
= widget
->allocation
.y
;
373 attributes
.width
= widget
->allocation
.width
;
374 attributes
.height
= widget
->allocation
.height
;
375 attributes
.wclass
= GDK_INPUT_OUTPUT
;
376 attributes
.window_type
= GDK_WINDOW_CHILD
;
377 attributes
.event_mask
= gtk_widget_get_events(widget
) |
379 GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
|
380 GDK_BUTTON1_MOTION_MASK
| GDK_BUTTON2_MOTION_MASK
|
381 GDK_BUTTON3_MOTION_MASK
;
382 attributes
.visual
= gtk_widget_get_visual(widget
);
383 attributes
.colormap
= gtk_widget_get_colormap(widget
);
385 attributes_mask
= GDK_WA_X
| GDK_WA_Y
|
386 GDK_WA_VISUAL
| GDK_WA_COLORMAP
;
387 widget
->window
= gdk_window_new(gtk_widget_get_parent_window(widget
),
388 &attributes
, attributes_mask
);
390 widget
->style
= gtk_style_attach(widget
->style
, widget
->window
);
392 gdk_window_set_user_data(widget
->window
, widget
);
393 gdk_window_set_background(widget
->window
,
394 &widget
->style
->bg
[GTK_STATE_NORMAL
]);
396 bg
= &widget
->style
->bg
[GTK_STATE_NORMAL
];
397 fg
= &widget
->style
->fg
[GTK_STATE_NORMAL
];
398 xor_values
.function
= GDK_XOR
;
399 xor_values
.foreground
.pixel
= fg
->pixel
^ bg
->pixel
;
400 collection
->xor_gc
= gdk_gc_new_with_values(widget
->window
,
406 static void collection_size_request(GtkWidget
*widget
,
407 GtkRequisition
*requisition
)
409 Collection
*collection
= COLLECTION(widget
);
410 int rows
, cols
= collection
->columns
;
412 /* We ask for the total size we need; our containing viewport
413 * will deal with scrolling.
415 requisition
->width
= MIN_WIDTH
;
416 rows
= (collection
->number_of_items
+ cols
- 1) / cols
;
417 requisition
->height
= rows
* collection
->item_height
;
420 static gboolean
scroll_after_alloc(Collection
*collection
)
422 if (collection
->wink_item
!= -1)
423 scroll_to_show(collection
, collection
->wink_item
);
424 else if (collection
->cursor_item
!= -1)
425 scroll_to_show(collection
, collection
->cursor_item
);
426 g_object_unref(G_OBJECT(collection
));
431 static void collection_size_allocate(GtkWidget
*widget
,
432 GtkAllocation
*allocation
)
434 Collection
*collection
;
436 gboolean cursor_visible
= FALSE
;
438 g_return_if_fail(widget
!= NULL
);
439 g_return_if_fail(IS_COLLECTION(widget
));
440 g_return_if_fail(allocation
!= NULL
);
442 collection
= COLLECTION(widget
);
444 if (collection
->cursor_item
!= -1)
447 int crow
= collection
->cursor_item
/ collection
->columns
;
449 get_visible_limits(collection
, &first
, &last
);
451 cursor_visible
= crow
>= first
&& crow
<= last
;
454 old_columns
= collection
->columns
;
456 widget
->allocation
= *allocation
;
458 collection
->columns
= allocation
->width
/ collection
->item_width
;
459 if (collection
->columns
< 1)
460 collection
->columns
= 1;
462 if (GTK_WIDGET_REALIZED(widget
))
464 gdk_window_move_resize(widget
->window
,
465 allocation
->x
, allocation
->y
,
466 allocation
->width
, allocation
->height
);
469 scroll_to_show(collection
, collection
->cursor_item
);
472 if (old_columns
!= collection
->columns
)
474 /* Need to go around again... */
475 gtk_widget_queue_resize(widget
);
477 else if (collection
->wink_item
!= -1 || collection
->cursor_item
!= -1)
479 /* Viewport resets the adjustments after the alloc */
480 g_object_ref(G_OBJECT(collection
));
481 g_idle_add((GSourceFunc
) scroll_after_alloc
, collection
);
485 /* Return the area occupied by the item at (row, col) by filling
488 static void collection_get_item_area(Collection
*collection
,
493 area
->x
= col
* collection
->item_width
;
494 area
->y
= row
* collection
->item_height
;
496 area
->width
= collection
->item_width
;
497 area
->height
= collection
->item_height
;
498 if (col
== collection
->columns
- 1)
502 static gint
collection_expose(GtkWidget
*widget
, GdkEventExpose
*event
)
504 Collection
*collection
;
505 GdkRectangle item_area
;
508 int start_row
, last_row
;
509 int start_col
, last_col
;
512 g_return_val_if_fail(widget
!= NULL
, FALSE
);
513 g_return_val_if_fail(IS_COLLECTION(widget
), FALSE
);
514 g_return_val_if_fail(event
!= NULL
, FALSE
);
516 gtk_paint_flat_box(widget
->style
, widget
->window
, GTK_STATE_NORMAL
,
517 GTK_SHADOW_NONE
, &event
->area
,
518 widget
, "base", 0, 0, -1, -1);
520 collection
= COLLECTION(widget
);
522 /* Calculate the ranges to plot */
523 start_row
= event
->area
.y
/ collection
->item_height
;
524 last_row
= (event
->area
.y
+ event
->area
.height
- 1)
525 / collection
->item_height
;
528 start_col
= event
->area
.x
/ collection
->item_width
;
529 phys_last_col
= (event
->area
.x
+ event
->area
.width
- 1)
530 / collection
->item_width
;
532 /* The right-most column may be wider than the others.
533 * Therefore, to redraw the area after the last 'real' column
534 * we may have to draw the right-most column.
536 if (start_col
>= collection
->columns
)
537 start_col
= collection
->columns
- 1;
539 if (phys_last_col
>= collection
->columns
)
540 last_col
= collection
->columns
- 1;
542 last_col
= phys_last_col
;
546 item
= row
* collection
->columns
+ col
;
548 while ((item
== 0 || item
< collection
->number_of_items
)
551 collection_get_item_area(collection
, row
, col
, &item_area
);
553 draw_one_item(collection
, item
, &item_area
);
560 item
= row
* collection
->columns
+ col
;
566 if (collection
->lasso_box
)
567 draw_lasso_box(collection
);
572 static void default_draw_item(GtkWidget
*widget
,
573 CollectionItem
*item
,
577 gdk_draw_arc(widget
->window
,
578 item
->selected
? widget
->style
->white_gc
579 : widget
->style
->black_gc
,
582 COLLECTION(widget
)->item_width
, area
->height
,
587 static gboolean
default_test_point(Collection
*collection
,
588 int point_x
, int point_y
,
589 CollectionItem
*item
,
590 int width
, int height
,
595 /* Convert to point in unit circle */
596 f_x
= ((float) point_x
/ width
) - 0.5;
597 f_y
= ((float) point_y
/ height
) - 0.5;
599 return (f_x
* f_x
) + (f_y
* f_y
) <= .25;
602 static void collection_set_property(GObject
*object
,
607 Collection
*collection
;
609 collection
= COLLECTION(object
);
613 case PROP_VADJUSTMENT
:
614 collection_set_adjustment(collection
,
615 g_value_get_object(value
));
618 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
,
624 static void collection_set_adjustment(Collection
*collection
,
628 g_return_if_fail(GTK_IS_ADJUSTMENT(vadj
));
630 vadj
= GTK_ADJUSTMENT(gtk_adjustment_new(0.0,
634 if (collection
->vadj
== vadj
)
637 if (collection
->vadj
)
638 g_object_unref(G_OBJECT(collection
->vadj
));
640 collection
->vadj
= vadj
;
641 g_object_ref(G_OBJECT(collection
->vadj
));
642 gtk_object_sink(GTK_OBJECT(collection
->vadj
));
645 static void collection_get_property(GObject
*object
,
650 Collection
*collection
;
652 collection
= COLLECTION(object
);
656 case PROP_VADJUSTMENT
:
657 g_value_set_object(value
, G_OBJECT(collection
->vadj
));
660 G_OBJECT_WARN_INVALID_PROPERTY_ID(object
,
666 static void resize_arrays(Collection
*collection
, guint new_size
)
668 g_return_if_fail(collection
!= NULL
);
669 g_return_if_fail(IS_COLLECTION(collection
));
670 g_return_if_fail(new_size
>= collection
->number_of_items
);
672 collection
->items
= g_realloc(collection
->items
,
673 sizeof(CollectionItem
) * new_size
);
674 collection
->array_size
= new_size
;
677 static gint
collection_key_press(GtkWidget
*widget
, GdkEventKey
*event
)
679 Collection
*collection
;
683 g_return_val_if_fail(widget
!= NULL
, FALSE
);
684 g_return_val_if_fail(IS_COLLECTION(widget
), FALSE
);
685 g_return_val_if_fail(event
!= NULL
, FALSE
);
687 collection
= (Collection
*) widget
;
688 item
= collection
->cursor_item
;
691 if (event
->state
& (GDK_CONTROL_MASK
| GDK_SHIFT_MASK
))
693 if (key
== GDK_Left
|| key
== GDK_Right
|| \
694 key
== GDK_Up
|| key
== GDK_Down
)
702 collection_move_cursor(collection
, 0, -1);
705 collection_move_cursor(collection
, 0, 1);
708 collection_move_cursor(collection
, -1, 0);
711 collection_move_cursor(collection
, 1, 0);
714 collection_set_cursor_item(collection
, 0, TRUE
);
717 collection_set_cursor_item(collection
,
718 MAX((gint
) collection
->number_of_items
- 1, 0),
724 get_visible_limits(collection
, &first
, &last
);
725 collection_move_cursor(collection
, first
- last
- 1, 0);
731 get_visible_limits(collection
, &first
, &last
);
732 collection_move_cursor(collection
, last
- first
+ 1, 0);
742 /* Wheel mouse scrolling */
743 static gint
collection_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
)
745 Collection
*collection
;
748 g_return_val_if_fail(widget
!= NULL
, FALSE
);
749 g_return_val_if_fail(IS_COLLECTION(widget
), FALSE
);
750 g_return_val_if_fail(event
!= NULL
, FALSE
);
752 collection
= COLLECTION(widget
);
754 if (event
->direction
== GDK_SCROLL_UP
)
756 else if (event
->direction
== GDK_SCROLL_DOWN
)
763 int old_value
= collection
->vadj
->value
;
765 gboolean box
= collection
->lasso_box
;
766 int step
= collection
->vadj
->page_increment
/ 2;
768 new_value
= CLAMP(old_value
+ diff
* step
, 0.0,
769 collection
->vadj
->upper
770 - collection
->vadj
->page_size
);
771 diff
= new_value
- old_value
;
776 remove_lasso_box(collection
);
777 collection
->drag_box_y
[0] -= diff
;
779 gtk_adjustment_set_value(collection
->vadj
, new_value
);
781 add_lasso_box(collection
);
788 /* 'from' and 'to' are pixel positions. 'step' is the size of each item.
789 * Returns the index of the first item covered, and the number of items.
791 static void get_range(int from
, int to
, int step
, gint
*pos
, gint
*len
)
793 int margin
= MIN(step
/ 4, 40);
802 from
= (from
+ margin
) / step
; /* First item */
803 to
= (to
+ step
- margin
) / step
; /* Last item (inclusive) */
809 /* Fills in the area with a rectangle corresponding to the current
810 * size of the lasso box (units of items, not pixels).
812 * The box will only span valid columns, but the total number
813 * of items is not taken into account (rows or cols).
815 static void find_lasso_area(Collection
*collection
, GdkRectangle
*area
)
817 int cols
= collection
->columns
;
818 int dx
= collection
->drag_box_x
[0] - collection
->drag_box_x
[1];
819 int dy
= collection
->drag_box_y
[0] - collection
->drag_box_y
[1];
821 if (ABS(dx
) < 8 && ABS(dy
) < 8)
823 /* Didn't move far enough - ignore */
824 area
->x
= area
->y
= 0;
830 get_range(collection
->drag_box_x
[0], collection
->drag_box_x
[1],
831 collection
->item_width
, &area
->x
, &area
->width
);
835 else if (area
->x
+ area
->width
> cols
)
836 area
->width
= cols
- area
->x
;
838 get_range(collection
->drag_box_y
[0], collection
->drag_box_y
[1],
839 collection
->item_height
, &area
->y
, &area
->height
);
842 static void collection_process_area(Collection
*collection
,
848 guint32 stacked_time
;
850 gboolean changed
= FALSE
;
853 g_return_if_fail(fn
== GDK_SET
|| fn
== GDK_INVERT
);
855 old_selected
= collection
->number_selected
;
857 stacked_time
= current_event_time
;
858 current_event_time
= time
;
860 collection
->block_selection_changed
++;
862 for (y
= area
->y
; y
< area
->y
+ area
->height
; y
++)
864 item
= y
* collection
->columns
+ area
->x
;
866 for (x
= area
->x
; x
< area
->x
+ area
->width
; x
++)
868 if (item
>= collection
->number_of_items
)
871 if (fn
== GDK_INVERT
)
872 collection_item_set_selected(collection
, item
,
873 !collection
->items
[item
].selected
,
876 collection_item_set_selected(collection
, item
,
885 if (collection
->number_selected
&& !old_selected
)
886 g_signal_emit(collection
,
887 collection_signals
[GAIN_SELECTION
], 0,
889 else if (!collection
->number_selected
&& old_selected
)
890 g_signal_emit(collection
,
891 collection_signals
[LOSE_SELECTION
], 0,
894 collection_unblock_selection_changed(collection
,
895 current_event_time
, changed
);
896 current_event_time
= stacked_time
;
899 static gint
collection_motion_notify(GtkWidget
*widget
,
900 GdkEventMotion
*event
)
902 Collection
*collection
;
905 g_return_val_if_fail(widget
!= NULL
, FALSE
);
906 g_return_val_if_fail(IS_COLLECTION(widget
), FALSE
);
907 g_return_val_if_fail(event
!= NULL
, FALSE
);
909 collection
= COLLECTION(widget
);
911 if (!collection
->lasso_box
)
914 if (event
->window
!= widget
->window
)
915 gdk_window_get_pointer(widget
->window
, &x
, &y
, NULL
);
922 remove_lasso_box(collection
);
923 collection
->drag_box_x
[1] = x
;
924 collection
->drag_box_y
[1] = y
;
925 add_lasso_box(collection
);
929 static void add_lasso_box(Collection
*collection
)
931 g_return_if_fail(collection
!= NULL
);
932 g_return_if_fail(IS_COLLECTION(collection
));
933 g_return_if_fail(collection
->lasso_box
== FALSE
);
935 collection
->lasso_box
= TRUE
;
936 draw_lasso_box(collection
);
939 static void draw_lasso_box(Collection
*collection
)
942 int x
, y
, width
, height
;
944 widget
= GTK_WIDGET(collection
);
946 x
= MIN(collection
->drag_box_x
[0], collection
->drag_box_x
[1]);
947 y
= MIN(collection
->drag_box_y
[0], collection
->drag_box_y
[1]);
948 width
= abs(collection
->drag_box_x
[1] - collection
->drag_box_x
[0]);
949 height
= abs(collection
->drag_box_y
[1] - collection
->drag_box_y
[0]);
951 /* XXX: A redraw bug sometimes leaves a one-pixel dot on the screen.
952 * As a quick hack, don't draw boxes that small for now...
955 gdk_draw_rectangle(widget
->window
, collection
->xor_gc
, FALSE
,
956 x
, y
, width
, height
);
959 static void abort_lasso(Collection
*collection
)
961 if (collection
->lasso_box
)
962 remove_lasso_box(collection
);
965 static void remove_lasso_box(Collection
*collection
)
967 g_return_if_fail(collection
!= NULL
);
968 g_return_if_fail(IS_COLLECTION(collection
));
969 g_return_if_fail(collection
->lasso_box
== TRUE
);
971 draw_lasso_box(collection
);
973 collection
->lasso_box
= FALSE
;
978 /* Make sure that 'item' is fully visible (vertically), scrolling if not. */
979 static void scroll_to_show(Collection
*collection
, int item
)
981 int first
, last
, row
;
983 g_return_if_fail(collection
!= NULL
);
984 g_return_if_fail(IS_COLLECTION(collection
));
986 row
= item
/ collection
->columns
;
987 get_visible_limits(collection
, &first
, &last
);
991 gtk_adjustment_set_value(collection
->vadj
,
992 row
* collection
->item_height
);
994 else if (row
>= last
)
996 GtkWidget
*widget
= (GtkWidget
*) collection
;
999 if (GTK_WIDGET_REALIZED(widget
))
1001 height
= collection
->vadj
->page_size
;
1002 gtk_adjustment_set_value(collection
->vadj
,
1003 (row
+ 1) * collection
->item_height
- height
);
1008 /* Return the first and last rows which are [partly] visible. Does not
1009 * ensure that the rows actually exist (contain items).
1011 static void get_visible_limits(Collection
*collection
, int *first
, int *last
)
1013 GtkWidget
*widget
= (GtkWidget
*) collection
;
1014 gint scroll
= 0, height
;
1016 g_return_if_fail(collection
!= NULL
);
1017 g_return_if_fail(IS_COLLECTION(collection
));
1018 g_return_if_fail(first
!= NULL
&& last
!= NULL
);
1020 if (!GTK_WIDGET_REALIZED(widget
))
1027 scroll
= collection
->vadj
->value
;
1028 height
= collection
->vadj
->page_size
;
1030 *first
= MAX(scroll
/ collection
->item_height
, 0);
1031 *last
= (scroll
+ height
- 1) /collection
->item_height
;
1038 /* Cancel the current wink effect. */
1039 static void cancel_wink(Collection
*collection
)
1043 g_return_if_fail(collection
!= NULL
);
1044 g_return_if_fail(IS_COLLECTION(collection
));
1045 g_return_if_fail(collection
->wink_item
!= -1);
1047 item
= collection
->wink_item
;
1049 collection
->wink_item
= -1;
1050 g_source_remove(collection
->wink_timeout
);
1052 collection_draw_item(collection
, item
, TRUE
);
1055 /* Draw/undraw a box around collection->wink_item */
1056 static void invert_wink(Collection
*collection
)
1061 g_return_if_fail(collection
->wink_item
>= 0);
1063 if (!GTK_WIDGET_REALIZED(GTK_WIDGET(collection
)))
1066 col
= collection
->wink_item
% collection
->columns
;
1067 row
= collection
->wink_item
/ collection
->columns
;
1068 collection_get_item_area(collection
, row
, col
, &area
);
1070 gdk_draw_rectangle(((GtkWidget
*) collection
)->window
,
1071 collection
->xor_gc
, FALSE
,
1073 collection
->item_width
- 1,
1077 static gboolean
wink_timeout(Collection
*collection
)
1081 g_return_val_if_fail(collection
!= NULL
, FALSE
);
1082 g_return_val_if_fail(IS_COLLECTION(collection
), FALSE
);
1083 g_return_val_if_fail(collection
->wink_item
!= -1, FALSE
);
1085 item
= collection
->wink_item
;
1087 if (collection
->winks_left
-- > 0)
1089 invert_wink(collection
);
1093 collection
->wink_item
= -1;
1095 collection_draw_item(collection
, item
, TRUE
);
1100 /* Change the selected state of an item.
1101 * Send GAIN/LOSE signals if 'signal' is TRUE.
1102 * Send SELECTION_CHANGED unless blocked.
1103 * Updates number_selected and redraws the item.
1105 static void collection_item_set_selected(Collection
*collection
,
1110 g_return_if_fail(collection
!= NULL
);
1111 g_return_if_fail(IS_COLLECTION(collection
));
1112 g_return_if_fail(item
>= 0 && item
< collection
->number_of_items
);
1114 if (collection
->items
[item
].selected
== selected
)
1117 collection
->items
[item
].selected
= selected
;
1118 collection_draw_item(collection
, item
, TRUE
);
1122 collection
->number_selected
++;
1123 if (signal
&& collection
->number_selected
== 1)
1124 g_signal_emit(collection
,
1125 collection_signals
[GAIN_SELECTION
], 0,
1126 current_event_time
);
1130 collection
->number_selected
--;
1131 if (signal
&& collection
->number_selected
== 0)
1132 g_signal_emit(collection
,
1133 collection_signals
[LOSE_SELECTION
], 0,
1134 current_event_time
);
1137 EMIT_SELECTION_CHANGED(collection
, current_event_time
);
1140 /* Functions for managing collections */
1142 /* Remove all objects from the collection */
1143 void collection_clear(Collection
*collection
)
1145 collection_delete_if(collection
, NULL
, NULL
);
1148 /* Inserts a new item at the end. The new item is unselected, and its
1149 * number is returned.
1151 gint
collection_insert(Collection
*collection
, gpointer data
, gpointer view
)
1155 /* g_return_val_if_fail(IS_COLLECTION(collection), -1); (slow) */
1157 item
= collection
->number_of_items
;
1159 if (item
>= collection
->array_size
)
1160 resize_arrays(collection
, item
+ (item
>> 1));
1162 collection
->items
[item
].data
= data
;
1163 collection
->items
[item
].view_data
= view
;
1164 collection
->items
[item
].selected
= FALSE
;
1166 collection
->number_of_items
++;
1168 gtk_widget_queue_resize(GTK_WIDGET(collection
));
1170 collection_draw_item(collection
, item
, FALSE
);
1175 void collection_unselect_item(Collection
*collection
, gint item
)
1177 collection_item_set_selected(collection
, item
, FALSE
, TRUE
);
1180 void collection_select_item(Collection
*collection
, gint item
)
1182 collection_item_set_selected(collection
, item
, TRUE
, TRUE
);
1185 void collection_toggle_item(Collection
*collection
, gint item
)
1187 collection_item_set_selected(collection
, item
,
1188 !collection
->items
[item
].selected
, TRUE
);
1191 /* Select all items in the collection */
1192 void collection_select_all(Collection
*collection
)
1197 g_return_if_fail(collection
!= NULL
);
1198 g_return_if_fail(IS_COLLECTION(collection
));
1200 widget
= GTK_WIDGET(collection
);
1202 if (collection
->number_selected
== collection
->number_of_items
)
1203 return; /* Nothing to do */
1205 while (collection
->number_selected
< collection
->number_of_items
)
1207 while (collection
->items
[item
].selected
)
1210 collection
->items
[item
].selected
= TRUE
;
1211 collection_draw_item(collection
, item
, TRUE
);
1214 collection
->number_selected
++;
1217 g_signal_emit(collection
, collection_signals
[GAIN_SELECTION
], 0,
1218 current_event_time
);
1219 EMIT_SELECTION_CHANGED(collection
, current_event_time
);
1222 /* Toggle all items in the collection */
1223 void collection_invert_selection(Collection
*collection
)
1227 g_return_if_fail(collection
!= NULL
);
1228 g_return_if_fail(IS_COLLECTION(collection
));
1230 if (collection
->number_selected
== 0)
1232 collection_select_all(collection
);
1235 else if (collection
->number_of_items
== collection
->number_selected
)
1237 collection_clear_selection(collection
);
1241 for (item
= 0; item
< collection
->number_of_items
; item
++)
1242 collection
->items
[item
].selected
=
1243 !collection
->items
[item
].selected
;
1245 collection
->number_selected
= collection
->number_of_items
-
1246 collection
->number_selected
;
1248 /* Have to redraw everything... */
1249 gtk_widget_queue_draw(GTK_WIDGET(collection
));
1251 EMIT_SELECTION_CHANGED(collection
, current_event_time
);
1254 /* Unselect all items except number item, which is selected (-1 to unselect
1257 void collection_clear_except(Collection
*collection
, gint item
)
1261 int end
; /* Selected items to end up with */
1263 g_return_if_fail(collection
!= NULL
);
1264 g_return_if_fail(IS_COLLECTION(collection
));
1265 g_return_if_fail(item
>= -1 && item
< collection
->number_of_items
);
1267 widget
= GTK_WIDGET(collection
);
1273 collection_select_item(collection
, item
);
1277 if (collection
->number_selected
== 0)
1280 while (collection
->number_selected
> end
)
1282 while (i
== item
|| !collection
->items
[i
].selected
)
1285 collection
->items
[i
].selected
= FALSE
;
1286 collection_draw_item(collection
, i
, TRUE
);
1289 collection
->number_selected
--;
1293 g_signal_emit(collection
, collection_signals
[LOSE_SELECTION
], 0,
1294 current_event_time
);
1295 EMIT_SELECTION_CHANGED(collection
, current_event_time
);
1298 /* Unselect all items in the collection */
1299 void collection_clear_selection(Collection
*collection
)
1301 g_return_if_fail(collection
!= NULL
);
1302 g_return_if_fail(IS_COLLECTION(collection
));
1304 collection_clear_except(collection
, -1);
1307 /* Force a redraw of the specified item, if it is visible */
1308 void collection_draw_item(Collection
*collection
, gint item
, gboolean blank
)
1314 g_return_if_fail(collection
!= NULL
);
1315 /* g_return_if_fail(IS_COLLECTION(collection)); (slow) */
1316 g_return_if_fail(item
>= 0 &&
1317 (item
== 0 || item
< collection
->number_of_items
));
1319 widget
= GTK_WIDGET(collection
);
1320 if (!GTK_WIDGET_REALIZED(widget
))
1323 col
= item
% collection
->columns
;
1324 row
= item
/ collection
->columns
;
1326 collection_get_item_area(collection
, row
, col
, &area
);
1328 gdk_window_invalidate_rect(widget
->window
, &area
, FALSE
);
1331 void collection_set_item_size(Collection
*collection
, int width
, int height
)
1335 g_return_if_fail(collection
!= NULL
);
1336 g_return_if_fail(IS_COLLECTION(collection
));
1337 g_return_if_fail(width
> 4 && height
> 4);
1339 if (collection
->item_width
== width
&&
1340 collection
->item_height
== height
)
1343 widget
= GTK_WIDGET(collection
);
1345 collection
->item_width
= width
;
1346 collection
->item_height
= height
;
1348 if (GTK_WIDGET_REALIZED(widget
))
1352 gdk_drawable_get_size(widget
->window
, &window_width
, NULL
);
1353 collection
->columns
= MAX(window_width
/ collection
->item_width
,
1355 if (collection
->cursor_item
!= -1)
1356 scroll_to_show(collection
, collection
->cursor_item
);
1357 gtk_widget_queue_draw(widget
);
1360 gtk_widget_queue_resize(GTK_WIDGET(collection
));
1363 static int (*cmp_callback
)(const void *a
, const void *b
) = NULL
;
1364 static int collection_cmp(const void *a
, const void *b
)
1366 return cmp_callback(((CollectionItem
*) a
)->data
,
1367 ((CollectionItem
*) b
)->data
);
1369 static int collection_rcmp(const void *a
, const void *b
)
1371 return -cmp_callback(((CollectionItem
*) a
)->data
,
1372 ((CollectionItem
*) b
)->data
);
1375 /* Cursor is positioned on item with the same data as before the sort.
1376 * Same for the wink item.
1378 void collection_qsort(Collection
*collection
,
1379 int (*compar
)(const void *, const void *),
1382 int cursor
, wink
, items
, wink_on_map
;
1383 gpointer cursor_data
= NULL
;
1384 gpointer wink_data
= NULL
;
1385 gpointer wink_on_map_data
= NULL
;
1386 CollectionItem
*array
;
1388 int mul
= order
== GTK_SORT_ASCENDING
? 1 : -1;
1390 g_return_if_fail(collection
!= NULL
);
1391 g_return_if_fail(IS_COLLECTION(collection
));
1392 g_return_if_fail(compar
!= NULL
);
1393 g_return_if_fail(cmp_callback
== NULL
);
1395 /* Check to see if it needs sorting (saves redrawing) */
1396 if (collection
->number_of_items
< 2)
1399 array
= collection
->items
;
1400 for (i
= 1; i
< collection
->number_of_items
; i
++)
1402 if (mul
* compar(array
[i
- 1].data
, array
[i
].data
) > 0)
1405 if (i
== collection
->number_of_items
)
1406 return; /* Already sorted */
1408 items
= collection
->number_of_items
;
1410 wink_on_map
= collection
->wink_on_map
;
1411 if (wink_on_map
>= 0 && wink_on_map
< items
)
1413 wink_on_map_data
= collection
->items
[wink_on_map
].data
;
1414 collection
->wink_on_map
= -1;
1419 wink
= collection
->wink_item
;
1420 if (wink
>= 0 && wink
< items
)
1422 wink_data
= collection
->items
[wink
].data
;
1423 collection
->wink_item
= -1;
1428 cursor
= collection
->cursor_item
;
1429 if (cursor
>= 0 && cursor
< items
)
1430 cursor_data
= collection
->items
[cursor
].data
;
1434 cmp_callback
= compar
;
1435 qsort(collection
->items
, items
, sizeof(collection
->items
[0]),
1436 order
== GTK_SORT_ASCENDING
? collection_cmp
1438 cmp_callback
= NULL
;
1440 if (cursor
> -1 || wink
> -1 || wink_on_map
> -1)
1444 for (item
= 0; item
< items
; item
++)
1446 if (collection
->items
[item
].data
== cursor_data
)
1447 collection_set_cursor_item(collection
, item
,
1449 if (collection
->items
[item
].data
== wink_on_map_data
)
1450 collection
->wink_on_map
= item
;
1451 if (collection
->items
[item
].data
== wink_data
)
1453 collection
->cursor_item_old
= item
;
1454 collection
->wink_item
= item
;
1455 scroll_to_show(collection
, item
);
1460 gtk_widget_queue_draw(GTK_WIDGET(collection
));
1463 /* Find an item in a sorted collection.
1464 * Returns the item number, or -1 if not found.
1466 int collection_find_item(Collection
*collection
, gpointer data
,
1467 int (*compar
)(const void *, const void *),
1471 int mul
= order
== GTK_SORT_ASCENDING
? 1 : -1;
1473 g_return_val_if_fail(collection
!= NULL
, -1);
1474 g_return_val_if_fail(IS_COLLECTION(collection
), -1);
1475 g_return_val_if_fail(compar
!= NULL
, -1);
1477 /* If item is here, then: lower <= i < upper */
1479 upper
= collection
->number_of_items
;
1481 while (lower
< upper
)
1485 i
= (lower
+ upper
) >> 1;
1487 cmp
= mul
* compar(collection
->items
[i
].data
, data
);
1500 /* Return the number of the item under the point (x,y), or -1 for none.
1501 * This may call your test_point callback. The point is relative to the
1502 * collection's origin.
1504 int collection_get_item(Collection
*collection
, int x
, int y
)
1510 g_return_val_if_fail(collection
!= NULL
, -1);
1512 col
= x
/ collection
->item_width
;
1513 row
= y
/ collection
->item_height
;
1515 if (col
>= collection
->columns
)
1516 col
= collection
->columns
- 1;
1518 if (col
< 0 || row
< 0)
1521 if (col
== collection
->columns
- 1)
1522 width
= collection
->item_width
<< 1;
1524 width
= collection
->item_width
;
1526 item
= col
+ row
* collection
->columns
;
1527 if (item
>= collection
->number_of_items
)
1530 x
-= col
* collection
->item_width
;
1531 y
-= row
* collection
->item_height
;
1533 if (collection
->test_point(collection
, x
, y
,
1534 &collection
->items
[item
], width
, collection
->item_height
,
1535 collection
->cb_user_data
))
1541 /* Set the cursor/highlight over the given item. Passing -1
1542 * hides the cursor. As a special case, you may set the cursor item
1543 * to zero when there are no items.
1545 void collection_set_cursor_item(Collection
*collection
, gint item
,
1546 gboolean may_scroll
)
1550 g_return_if_fail(collection
!= NULL
);
1551 g_return_if_fail(IS_COLLECTION(collection
));
1552 g_return_if_fail(item
>= -1 &&
1553 (item
< collection
->number_of_items
|| item
== 0));
1555 old_item
= collection
->cursor_item
;
1557 if (old_item
== item
)
1560 collection
->cursor_item
= item
;
1563 collection_draw_item(collection
, old_item
, TRUE
);
1567 collection_draw_item(collection
, item
, TRUE
);
1569 scroll_to_show(collection
, item
);
1571 else if (old_item
!= -1)
1572 collection
->cursor_item_old
= old_item
;
1575 /* Briefly highlight an item to draw the user's attention to it.
1576 * -1 cancels the effect, as does deleting items, sorting the collection
1577 * or starting a new wink effect.
1578 * Otherwise, the effect will cancel itself after a short pause.
1580 void collection_wink_item(Collection
*collection
, gint item
)
1582 g_return_if_fail(collection
!= NULL
);
1583 g_return_if_fail(IS_COLLECTION(collection
));
1584 g_return_if_fail(item
>= -1 && item
< collection
->number_of_items
);
1586 if (collection
->wink_item
!= -1)
1587 cancel_wink(collection
);
1591 if (!GTK_WIDGET_MAPPED(GTK_WIDGET(collection
)))
1593 collection
->wink_on_map
= item
;
1597 collection
->cursor_item_old
= collection
->wink_item
= item
;
1598 collection
->winks_left
= MAX_WINKS
;
1600 collection
->wink_timeout
= g_timeout_add(70,
1601 (GSourceFunc
) wink_timeout
,
1603 scroll_to_show(collection
, item
);
1604 invert_wink(collection
);
1609 /* Call test(item, data) on each item in the collection.
1610 * Remove all items for which it returns TRUE. test() should
1611 * free the data before returning TRUE. The collection is in an
1612 * inconsistant state during this call (ie, when test() is called).
1614 * If test is NULL, remove all items.
1616 void collection_delete_if(Collection
*collection
,
1617 gboolean (*test
)(gpointer item
, gpointer data
),
1624 g_return_if_fail(collection
!= NULL
);
1625 g_return_if_fail(IS_COLLECTION(collection
));
1627 cursor
= collection
->cursor_item
;
1629 for (in
= 0; in
< collection
->number_of_items
; in
++)
1631 if (test
&& !test(collection
->items
[in
].data
, data
))
1634 if (collection
->items
[in
].selected
)
1636 collection
->items
[out
].selected
= TRUE
;
1640 collection
->items
[out
].selected
= FALSE
;
1642 collection
->items
[out
].data
=
1643 collection
->items
[in
].data
;
1644 collection
->items
[out
].view_data
=
1645 collection
->items
[in
].view_data
;
1651 if (collection
->free_item
)
1652 collection
->free_item(collection
,
1653 &collection
->items
[in
]);
1655 if (collection
->cursor_item
>= in
)
1662 collection
->cursor_item
= cursor
;
1664 if (collection
->wink_item
!= -1)
1666 collection
->wink_item
= -1;
1667 g_source_remove(collection
->wink_timeout
);
1670 collection
->number_of_items
= out
;
1671 if (collection
->number_selected
&& !selected
)
1673 /* We've lost all the selected items */
1674 g_signal_emit(collection
,
1675 collection_signals
[LOSE_SELECTION
], 0,
1676 current_event_time
);
1679 collection
->number_selected
= selected
;
1680 resize_arrays(collection
,
1681 MAX(collection
->number_of_items
, MINIMUM_ITEMS
));
1683 if (GTK_WIDGET_REALIZED(GTK_WIDGET(collection
)))
1684 gtk_widget_queue_draw(GTK_WIDGET(collection
));
1686 gtk_widget_queue_resize(GTK_WIDGET(collection
));
1690 /* Move the cursor by the given row and column offsets.
1691 * Moving by (0,0) can be used to simply make the cursor appear.
1693 void collection_move_cursor(Collection
*collection
, int drow
, int dcol
)
1696 int first
, last
, total_rows
;
1698 g_return_if_fail(collection
!= NULL
);
1699 g_return_if_fail(IS_COLLECTION(collection
));
1701 if (!collection
->number_of_items
)
1703 /* Show the cursor, even though there are no items */
1704 collection_set_cursor_item(collection
, 0, TRUE
);
1708 get_visible_limits(collection
, &first
, &last
);
1710 item
= collection
->cursor_item
;
1713 item
= MIN(collection
->cursor_item_old
,
1714 collection
->number_of_items
- 1);
1724 row
= item
/ collection
->columns
;
1725 col
= item
% collection
->columns
+ dcol
;
1729 else if (row
> last
)
1732 row
= MAX(row
+ drow
, 0);
1735 total_rows
= (collection
->number_of_items
+ collection
->columns
- 1)
1736 / collection
->columns
;
1738 if (row
>= total_rows
- 1 && drow
> 0)
1740 row
= total_rows
- 1;
1741 item
= col
+ row
* collection
->columns
;
1742 if (item
>= collection
->number_of_items
- 1)
1744 collection_set_cursor_item(collection
,
1745 collection
->number_of_items
- 1,
1753 item
= col
+ row
* collection
->columns
;
1755 if (item
>= 0 && item
< collection
->number_of_items
)
1756 collection_set_cursor_item(collection
, item
, TRUE
);
1759 /* Start a lasso box drag */
1760 void collection_lasso_box(Collection
*collection
, int x
, int y
)
1762 collection
->drag_box_x
[0] = x
;
1763 collection
->drag_box_y
[0] = y
;
1764 collection
->drag_box_x
[1] = x
;
1765 collection
->drag_box_y
[1] = y
;
1767 add_lasso_box(collection
);
1770 /* Remove the lasso box. Applies fn to each item inside the box.
1771 * fn may be GDK_INVERT, GDK_SET, GDK_NOOP or GDK_CLEAR.
1773 void collection_end_lasso(Collection
*collection
, GdkFunction fn
)
1775 if (fn
!= GDK_CLEAR
)
1777 GdkRectangle region
;
1779 find_lasso_area(collection
, ®ion
);
1781 collection_process_area(collection
, ®ion
, fn
,
1785 abort_lasso(collection
);
1788 /* Unblock the selection_changed signal, emitting the signal if the
1789 * block counter reaches zero and emit is TRUE.
1791 void collection_unblock_selection_changed(Collection
*collection
,
1795 g_return_if_fail(collection
!= NULL
);
1796 g_return_if_fail(IS_COLLECTION(collection
));
1797 g_return_if_fail(collection
->block_selection_changed
> 0);
1799 collection
->block_selection_changed
--;
1801 if (emit
&& !collection
->block_selection_changed
)
1802 g_signal_emit(collection
,
1803 collection_signals
[SELECTION_CHANGED
], 0, time
);