Converted README to markdown
[rox-filer.git] / ROX-Filer / src / collection.c
blobbacd9bb0d13aba02e13d2ae4c79288ee3c178112
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * The collection widget provides an area for displaying a collection of
6 * objects (such as files). It allows the user to choose a selection of
7 * them and provides signals to allow popping up menus, detecting
8 * double-clicks etc.
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by the Free
12 * Software Foundation; either version 2 of the License, or (at your option)
13 * any later version.
15 * This program is distributed in the hope that it will be useful, but WITHOUT
16 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
18 * more details.
20 * You should have received a copy of the GNU General Public License along with
21 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
22 * Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "config.h"
27 #include <stdlib.h>
29 #include <gtk/gtk.h>
30 #include <gdk/gdkkeysyms.h>
31 #include "global.h"
33 #include "collection.h"
35 #define MIN_WIDTH 80
36 #define MIN_HEIGHT 60
37 #define MINIMUM_ITEMS 16
39 #define MAX_WINKS 5 /* Should be an odd number */
41 /* Macro to emit the "selection_changed" signal only if allowed */
42 #define EMIT_SELECTION_CHANGED(collection, time) \
43 if (!collection->block_selection_changed) \
44 g_signal_emit(collection, \
45 collection_signals[SELECTION_CHANGED], 0, time)
47 enum
49 PROP_0,
50 PROP_VADJUSTMENT
53 /* Signals:
55 * void gain_selection(collection, time, user_data)
56 * We've gone from no selected items to having a selection.
57 * Time is the time of the event that caused the change, or
58 * GDK_CURRENT_TIME if not known.
60 * void lose_selection(collection, time, user_data)
61 * We've dropped to having no selected items.
62 * Time is the time of the event that caused the change, or
63 * GDK_CURRENT_TIME if not known.
65 * void selection_changed(collection, user_data)
66 * The set of selected items has changed.
67 * Time is the time of the event that caused the change, or
68 * GDK_CURRENT_TIME if not known.
70 enum
72 GAIN_SELECTION,
73 LOSE_SELECTION,
74 SELECTION_CHANGED,
75 LAST_SIGNAL
78 static guint collection_signals[LAST_SIGNAL] = { 0 };
80 static guint32 current_event_time = GDK_CURRENT_TIME;
82 static GtkWidgetClass *parent_class = NULL;
84 /* Static prototypes */
85 static void draw_one_item(Collection *collection,
86 int item,
87 GdkRectangle *area);
88 static void collection_class_init(GObjectClass *gclass, gpointer data);
89 static void collection_init(GTypeInstance *object, gpointer g_class);
90 static void collection_destroy(GtkObject *object);
91 static void collection_finalize(GObject *object);
92 static void collection_realize(GtkWidget *widget);
93 static void collection_map(GtkWidget *widget);
94 static void collection_size_request(GtkWidget *widget,
95 GtkRequisition *requisition);
96 static void collection_size_allocate(GtkWidget *widget,
97 GtkAllocation *allocation);
98 static void collection_set_adjustment(Collection *collection,
99 GtkAdjustment *vadj);
100 static void collection_get_property(GObject *object,
101 guint prop_id,
102 GValue *value,
103 GParamSpec *pspec);
104 static void collection_set_property(GObject *object,
105 guint prop_id,
106 const GValue *value,
107 GParamSpec *pspec);
108 static gint collection_expose(GtkWidget *widget, GdkEventExpose *event);
109 static void default_draw_item(GtkWidget *widget,
110 CollectionItem *data,
111 GdkRectangle *area,
112 gpointer user_data);
113 static gboolean default_test_point(Collection *collection,
114 int point_x, int point_y,
115 CollectionItem *data,
116 int width, int height,
117 gpointer user_data);
118 static gint collection_motion_notify(GtkWidget *widget,
119 GdkEventMotion *event);
120 static void add_lasso_box(Collection *collection);
121 static void abort_lasso(Collection *collection);
122 static void remove_lasso_box(Collection *collection);
123 static void draw_lasso_box(Collection *collection);
124 static void cancel_wink(Collection *collection);
125 static gint collection_key_press(GtkWidget *widget, GdkEventKey *event);
126 static void get_visible_limits(Collection *collection, int *first, int *last);
127 static void scroll_to_show(Collection *collection, int item);
128 static void collection_item_set_selected(Collection *collection,
129 gint item,
130 gboolean selected,
131 gboolean signal);
132 static gint collection_scroll_event(GtkWidget *widget, GdkEventScroll *event);
133 static int collection_get_rows(const Collection *collection);
134 static int collection_get_cols(const Collection *collection);
137 /* The number of rows, at least 1. */
138 static inline int collection_get_rows(const Collection *collection)
140 int rows = (collection->number_of_items + collection->columns - 1) /
141 collection->columns;
142 return MAX(rows, 1);
145 /* The number of columns _actually_ displayed, at least 1. This
146 * function is required in vertical_order layout-based manipulation
147 * such as moving the cursor to detect the last column. */
148 static inline int collection_get_cols(const Collection *collection)
150 if (collection->vertical_order)
152 int rows = collection_get_rows(collection);
153 int cols = (collection->number_of_items + rows - 1) / rows;
154 return MAX(1, cols);
156 else
157 return collection->columns;
160 static void draw_focus_at(Collection *collection, GdkRectangle *area)
162 GtkWidget *widget;
163 GtkStateType state;
165 widget = GTK_WIDGET(collection);
167 if (GTK_WIDGET_FLAGS(widget) & GTK_HAS_FOCUS)
168 state = GTK_STATE_ACTIVE;
169 else
170 state = GTK_STATE_INSENSITIVE;
172 gtk_paint_focus(widget->style,
173 widget->window,
174 state,
175 NULL,
176 widget,
177 "collection",
178 area->x, area->y,
179 collection->item_width,
180 area->height);
183 static void draw_one_item(Collection *collection, int item, GdkRectangle *area)
185 if (item < collection->number_of_items)
187 collection->draw_item((GtkWidget *) collection,
188 &collection->items[item],
189 area, collection->cb_user_data);
192 if (item == collection->cursor_item)
193 draw_focus_at(collection, area);
196 GType collection_get_type(void)
198 static GType my_type = 0;
200 if (!my_type)
202 static const GTypeInfo info =
204 sizeof(CollectionClass),
205 NULL, /* base_init */
206 NULL, /* base_finalise */
207 (GClassInitFunc) collection_class_init,
208 NULL, /* class_finalise */
209 NULL, /* class_data */
210 sizeof(Collection),
211 0, /* n_preallocs */
212 collection_init
215 my_type = g_type_register_static(gtk_widget_get_type(),
216 "Collection", &info, 0);
219 return my_type;
222 typedef void (*FinalizeFn)(GObject *object);
224 static void collection_class_init(GObjectClass *gclass, gpointer data)
226 CollectionClass *collection_class = (CollectionClass *) gclass;
227 GtkObjectClass *object_class = (GtkObjectClass *) gclass;
228 GtkWidgetClass *widget_class = (GtkWidgetClass *) gclass;
230 parent_class = gtk_type_class(gtk_widget_get_type());
232 object_class->destroy = collection_destroy;
233 G_OBJECT_CLASS(object_class)->finalize =
234 (FinalizeFn) collection_finalize;
236 widget_class->realize = collection_realize;
237 widget_class->expose_event = collection_expose;
238 widget_class->size_request = collection_size_request;
239 widget_class->size_allocate = collection_size_allocate;
241 widget_class->key_press_event = collection_key_press;
243 widget_class->motion_notify_event = collection_motion_notify;
244 widget_class->map = collection_map;
245 widget_class->scroll_event = collection_scroll_event;
247 gclass->set_property = collection_set_property;
248 gclass->get_property = collection_get_property;
250 collection_class->gain_selection = NULL;
251 collection_class->lose_selection = NULL;
252 collection_class->selection_changed = NULL;
254 collection_signals[GAIN_SELECTION] = g_signal_new("gain_selection",
255 G_TYPE_FROM_CLASS(gclass),
256 G_SIGNAL_RUN_LAST,
257 G_STRUCT_OFFSET(CollectionClass,
258 gain_selection),
259 NULL, NULL,
260 g_cclosure_marshal_VOID__INT,
261 G_TYPE_NONE, 1,
262 G_TYPE_INT);
264 collection_signals[LOSE_SELECTION] = g_signal_new("lose_selection",
265 G_TYPE_FROM_CLASS(gclass),
266 G_SIGNAL_RUN_LAST,
267 G_STRUCT_OFFSET(CollectionClass,
268 lose_selection),
269 NULL, NULL,
270 g_cclosure_marshal_VOID__INT,
271 G_TYPE_NONE, 1,
272 G_TYPE_INT);
274 collection_signals[SELECTION_CHANGED] = g_signal_new(
275 "selection_changed",
276 G_TYPE_FROM_CLASS(gclass),
277 G_SIGNAL_RUN_LAST,
278 G_STRUCT_OFFSET(CollectionClass,
279 selection_changed),
280 NULL, NULL,
281 g_cclosure_marshal_VOID__INT,
282 G_TYPE_NONE, 1,
283 G_TYPE_INT);
285 g_object_class_install_property(gclass,
286 PROP_VADJUSTMENT,
287 g_param_spec_object("vadjustment",
288 "Vertical Adjustment",
289 "The GtkAdjustment for the vertical position.",
290 GTK_TYPE_ADJUSTMENT,
291 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
294 static void collection_init(GTypeInstance *instance, gpointer g_class)
296 Collection *object = (Collection *) instance;
298 g_return_if_fail(object != NULL);
299 g_return_if_fail(IS_COLLECTION(object));
301 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(object), GTK_CAN_FOCUS);
303 object->number_of_items = 0;
304 object->number_selected = 0;
305 object->block_selection_changed = 0;
306 object->columns = 1;
307 object->vertical_order = FALSE;
308 object->item_width = 64;
309 object->item_height = 64;
310 object->vadj = NULL;
312 object->items = g_new(CollectionItem, MINIMUM_ITEMS);
313 object->cursor_item = -1;
314 object->cursor_item_old = -1;
315 object->wink_item = -1;
316 object->wink_on_map = -1;
317 object->array_size = MINIMUM_ITEMS;
318 object->draw_item = default_draw_item;
319 object->test_point = default_test_point;
320 object->free_item = NULL;
323 GtkWidget* collection_new(void)
325 return GTK_WIDGET(gtk_widget_new(collection_get_type(), NULL));
328 /* After this we are unusable, but our data (if any) is still hanging around.
329 * It will be freed later with finalize.
331 static void collection_destroy(GtkObject *object)
333 Collection *collection;
335 g_return_if_fail(object != NULL);
336 g_return_if_fail(IS_COLLECTION(object));
338 collection = COLLECTION(object);
340 collection_clear(collection);
342 if (collection->vadj)
344 g_object_unref(G_OBJECT(collection->vadj));
345 collection->vadj = NULL;
348 if (GTK_OBJECT_CLASS(parent_class)->destroy)
349 (*GTK_OBJECT_CLASS(parent_class)->destroy)(object);
352 /* This is the last thing that happens to us. Free all data. */
353 static void collection_finalize(GObject *object)
355 Collection *collection;
357 collection = COLLECTION(object);
359 g_return_if_fail(collection->number_of_items == 0);
361 g_free(collection->items);
363 if (G_OBJECT_CLASS(parent_class)->finalize)
364 G_OBJECT_CLASS(parent_class)->finalize(object);
367 static void collection_map(GtkWidget *widget)
369 Collection *collection = COLLECTION(widget);
371 if (GTK_WIDGET_CLASS(parent_class)->map)
372 (*GTK_WIDGET_CLASS(parent_class)->map)(widget);
374 if (collection->wink_on_map >= 0)
376 collection_wink_item(collection, collection->wink_on_map);
377 collection->wink_on_map = -1;
381 static void collection_realize(GtkWidget *widget)
383 Collection *collection;
384 GdkWindowAttr attributes;
385 gint attributes_mask;
386 GdkGCValues xor_values;
387 GdkColor *bg, *fg;
389 g_return_if_fail(widget != NULL);
390 g_return_if_fail(IS_COLLECTION(widget));
391 g_return_if_fail(widget->parent != NULL);
393 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
394 collection = COLLECTION(widget);
396 attributes.x = widget->allocation.x;
397 attributes.y = widget->allocation.y;
398 attributes.width = widget->allocation.width;
399 attributes.height = widget->allocation.height;
400 attributes.wclass = GDK_INPUT_OUTPUT;
401 attributes.window_type = GDK_WINDOW_CHILD;
402 attributes.event_mask = gtk_widget_get_events(widget) |
403 GDK_EXPOSURE_MASK |
404 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
405 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
406 GDK_BUTTON3_MOTION_MASK;
407 attributes.visual = gtk_widget_get_visual(widget);
408 attributes.colormap = gtk_widget_get_colormap(widget);
410 attributes_mask = GDK_WA_X | GDK_WA_Y |
411 GDK_WA_VISUAL | GDK_WA_COLORMAP;
412 widget->window = gdk_window_new(gtk_widget_get_parent_window(widget),
413 &attributes, attributes_mask);
415 widget->style = gtk_style_attach(widget->style, widget->window);
417 gdk_window_set_user_data(widget->window, widget);
418 gdk_window_set_background(widget->window,
419 &widget->style->base[GTK_STATE_NORMAL]);
421 bg = &widget->style->base[GTK_STATE_NORMAL];
422 fg = &widget->style->text[GTK_STATE_NORMAL];
423 xor_values.function = GDK_XOR;
424 xor_values.foreground.pixel = fg->pixel ^ bg->pixel;
425 collection->xor_gc = gdk_gc_new_with_values(widget->window,
426 &xor_values,
427 GDK_GC_FOREGROUND
428 | GDK_GC_FUNCTION);
431 static void collection_size_request(GtkWidget *widget,
432 GtkRequisition *requisition)
434 Collection *collection = COLLECTION(widget);
435 int rows;
437 /* We ask for the total size we need; our containing viewport
438 * will deal with scrolling.
440 requisition->width = MIN_WIDTH;
441 rows = collection_get_rows(collection);
442 requisition->height = rows * collection->item_height;
445 static gboolean scroll_after_alloc(Collection *collection)
447 if (collection->wink_item != -1)
448 scroll_to_show(collection, collection->wink_item);
449 else if (collection->cursor_item != -1)
450 scroll_to_show(collection, collection->cursor_item);
451 g_object_unref(G_OBJECT(collection));
453 return FALSE;
456 static void collection_size_allocate(GtkWidget *widget,
457 GtkAllocation *allocation)
459 Collection *collection;
460 int old_columns;
461 gboolean cursor_visible = FALSE;
463 g_return_if_fail(widget != NULL);
464 g_return_if_fail(IS_COLLECTION(widget));
465 g_return_if_fail(allocation != NULL);
467 collection = COLLECTION(widget);
469 if (collection->cursor_item != -1)
471 int first, last;
472 int crow, ccol;
474 collection_item_to_rowcol(collection, collection->cursor_item,
475 &crow, &ccol);
477 get_visible_limits(collection, &first, &last);
479 cursor_visible = crow >= first && crow <= last;
482 old_columns = collection->columns;
484 widget->allocation = *allocation;
486 collection->columns = allocation->width / collection->item_width;
487 if (collection->columns < 1)
488 collection->columns = 1;
490 if (GTK_WIDGET_REALIZED(widget))
492 gdk_window_move_resize(widget->window,
493 allocation->x, allocation->y,
494 allocation->width, allocation->height);
496 if (cursor_visible)
497 scroll_to_show(collection, collection->cursor_item);
500 if (old_columns != collection->columns)
502 /* Need to go around again... */
503 gtk_widget_queue_resize(widget);
505 else if (collection->wink_item != -1 || collection->cursor_item != -1)
507 /* Viewport resets the adjustments after the alloc */
508 g_object_ref(G_OBJECT(collection));
509 g_idle_add((GSourceFunc) scroll_after_alloc, collection);
513 /* Return the area occupied by the item at (row, col) by filling
514 * in 'area'.
516 static void collection_get_item_area(Collection *collection,
517 int row, int col,
518 GdkRectangle *area)
521 area->x = col * collection->item_width;
522 area->y = row * collection->item_height;
524 area->width = collection->item_width;
525 area->height = collection->item_height;
526 if (col == collection->columns - 1)
527 area->width <<= 1;
530 static gint collection_expose(GtkWidget *widget, GdkEventExpose *event)
532 Collection *collection;
533 GdkRectangle item_area;
534 int row, col;
535 int item;
536 int start_row, last_row;
537 int start_col, last_col;
538 int phys_last_col;
540 g_return_val_if_fail(widget != NULL, FALSE);
541 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
542 g_return_val_if_fail(event != NULL, FALSE);
544 /* Note about 'detail' argument:
545 * - If set to "base", lighthouse theme will crash
546 * - If set to NULL, cleanice theme will crash
548 * Clear the background only if we have a background pixmap.
550 if (widget->style->bg_pixmap[GTK_STATE_NORMAL])
551 gtk_paint_flat_box(widget->style, widget->window, GTK_STATE_NORMAL,
552 GTK_SHADOW_NONE, &event->area,
553 widget, "collection", 0, 0, -1, -1);
555 collection = COLLECTION(widget);
557 /* Calculate the ranges to plot */
558 start_row = event->area.y / collection->item_height;
559 last_row = (event->area.y + event->area.height - 1)
560 / collection->item_height;
562 if (last_row >= collection_get_rows(collection))
563 last_row = collection_get_rows(collection) - 1;
565 start_col = event->area.x / collection->item_width;
566 phys_last_col = (event->area.x + event->area.width - 1)
567 / collection->item_width;
569 /* The right-most column may be wider than the others.
570 * Therefore, to redraw the area after the last 'real' column
571 * we may have to draw the right-most column.
573 if (start_col >= collection->columns)
574 start_col = collection->columns - 1;
576 if (phys_last_col >= collection->columns)
577 last_col = collection->columns - 1;
578 else
579 last_col = phys_last_col;
582 for(row = start_row; row <= last_row; row++)
583 for(col = start_col; col <= last_col; col++)
585 item = collection_rowcol_to_item(collection, row, col);
586 if (item == 0 || item < collection->number_of_items) {
587 collection_get_item_area(collection,
588 row, col, &item_area);
589 draw_one_item(collection, item, &item_area);
593 if (collection->lasso_box)
594 draw_lasso_box(collection);
596 return FALSE;
599 static void default_draw_item(GtkWidget *widget,
600 CollectionItem *item,
601 GdkRectangle *area,
602 gpointer user_data)
604 gdk_draw_arc(widget->window,
605 item->selected ? widget->style->white_gc
606 : widget->style->black_gc,
607 TRUE,
608 area->x, area->y,
609 COLLECTION(widget)->item_width, area->height,
610 0, 360 * 64);
614 static gboolean default_test_point(Collection *collection,
615 int point_x, int point_y,
616 CollectionItem *item,
617 int width, int height,
618 gpointer user_data)
620 float f_x, f_y;
622 /* Convert to point in unit circle */
623 f_x = ((float) point_x / width) - 0.5;
624 f_y = ((float) point_y / height) - 0.5;
626 return (f_x * f_x) + (f_y * f_y) <= .25;
629 static void collection_set_property(GObject *object,
630 guint prop_id,
631 const GValue *value,
632 GParamSpec *pspec)
634 Collection *collection;
636 collection = COLLECTION(object);
638 switch (prop_id)
640 case PROP_VADJUSTMENT:
641 collection_set_adjustment(collection,
642 g_value_get_object(value));
643 break;
644 default:
645 G_OBJECT_WARN_INVALID_PROPERTY_ID(object,
646 prop_id, pspec);
647 break;
651 static void collection_set_adjustment(Collection *collection,
652 GtkAdjustment *vadj)
654 if (vadj)
655 g_return_if_fail(GTK_IS_ADJUSTMENT(vadj));
656 else
657 vadj = GTK_ADJUSTMENT(gtk_adjustment_new(0.0,
658 0.0, 0.0,
659 0.0, 0.0, 0.0));
661 if (collection->vadj == vadj)
662 return;
664 if (collection->vadj)
665 g_object_unref(G_OBJECT(collection->vadj));
667 collection->vadj = vadj;
668 g_object_ref(G_OBJECT(collection->vadj));
669 gtk_object_sink(GTK_OBJECT(collection->vadj));
672 static void collection_get_property(GObject *object,
673 guint prop_id,
674 GValue *value,
675 GParamSpec *pspec)
677 Collection *collection;
679 collection = COLLECTION(object);
681 switch (prop_id)
683 case PROP_VADJUSTMENT:
684 g_value_set_object(value, G_OBJECT(collection->vadj));
685 break;
686 default:
687 G_OBJECT_WARN_INVALID_PROPERTY_ID(object,
688 prop_id, pspec);
689 break;
693 static void resize_arrays(Collection *collection, guint new_size)
695 g_return_if_fail(collection != NULL);
696 g_return_if_fail(IS_COLLECTION(collection));
697 g_return_if_fail(new_size >= collection->number_of_items);
699 collection->items = g_realloc(collection->items,
700 sizeof(CollectionItem) * new_size);
701 collection->array_size = new_size;
704 static gint collection_key_press(GtkWidget *widget, GdkEventKey *event)
706 Collection *collection;
707 int key;
709 g_return_val_if_fail(widget != NULL, FALSE);
710 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
711 g_return_val_if_fail(event != NULL, FALSE);
713 collection = (Collection *) widget;
715 key = event->keyval;
716 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
718 if (key == GDK_Left || key == GDK_Right || \
719 key == GDK_Up || key == GDK_Down)
720 return TRUE;
721 return FALSE;
724 switch (key)
726 case GDK_Left:
727 collection_move_cursor(collection, 0, -1);
728 break;
729 case GDK_Right:
730 collection_move_cursor(collection, 0, 1);
731 break;
732 case GDK_Up:
733 collection_move_cursor(collection, -1, 0);
734 break;
735 case GDK_Down:
736 collection_move_cursor(collection, 1, 0);
737 break;
738 case GDK_Home:
739 collection_set_cursor_item(collection, 0, TRUE);
740 break;
741 case GDK_End:
742 collection_set_cursor_item(collection,
743 MAX((gint) collection->number_of_items - 1, 0),
744 TRUE);
745 break;
746 case GDK_Page_Up:
748 int first, last;
749 get_visible_limits(collection, &first, &last);
750 collection_move_cursor(collection, first - last - 1, 0);
751 break;
753 case GDK_Page_Down:
755 int first, last;
756 get_visible_limits(collection, &first, &last);
757 collection_move_cursor(collection, last - first + 1, 0);
758 break;
760 default:
761 return FALSE;
764 return TRUE;
767 /* Wheel mouse scrolling */
768 static gint collection_scroll_event(GtkWidget *widget, GdkEventScroll *event)
770 Collection *collection;
771 int diff = 0;
773 g_return_val_if_fail(widget != NULL, FALSE);
774 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
775 g_return_val_if_fail(event != NULL, FALSE);
777 collection = COLLECTION(widget);
779 if (event->direction == GDK_SCROLL_UP)
780 diff = -1;
781 else if (event->direction == GDK_SCROLL_DOWN)
782 diff = 1;
783 else
784 return FALSE;
786 if (diff)
788 int old_value = collection->vadj->value;
789 int new_value = 0;
790 gboolean box = collection->lasso_box;
791 int step = collection->vadj->page_increment / 2;
793 new_value = CLAMP(old_value + diff * step, 0.0,
794 collection->vadj->upper
795 - collection->vadj->page_size);
796 diff = new_value - old_value;
797 if (diff)
799 if (box)
801 remove_lasso_box(collection);
802 collection->drag_box_y[0] -= diff;
804 gtk_adjustment_set_value(collection->vadj, new_value);
805 if (box)
806 add_lasso_box(collection);
810 return TRUE;
813 /* 'from' and 'to' are pixel positions. 'step' is the size of each item.
814 * Returns the index of the first item covered, and the number of items.
816 static void get_range(int from, int to, int step, gint *pos, gint *len)
818 int margin = MIN(step / 4, 40);
820 if (from > to)
822 int tmp = to;
823 to = from;
824 from = tmp;
827 from = (from + margin) / step; /* First item */
828 to = (to + step - margin) / step; /* Last item (inclusive) */
830 *pos = MAX(from, 0);
831 *len = to - *pos;
834 /* Fills in the area with a rectangle corresponding to the current
835 * size of the lasso box (units of items, not pixels).
837 * The box will only span valid columns, but the total number
838 * of items is not taken into account (rows or cols).
840 static void find_lasso_area(Collection *collection, GdkRectangle *area)
842 int cols = collection->columns;
843 int dx = collection->drag_box_x[0] - collection->drag_box_x[1];
844 int dy = collection->drag_box_y[0] - collection->drag_box_y[1];
846 if (ABS(dx) < 8 && ABS(dy) < 8)
848 /* Didn't move far enough - ignore */
849 area->x = area->y = 0;
850 area->width = 0;
851 area->height = 0;
852 return;
855 get_range(collection->drag_box_x[0], collection->drag_box_x[1],
856 collection->item_width, &area->x, &area->width);
858 if (area->x >= cols)
859 area->width = 0;
860 else if (area->x + area->width > cols)
861 area->width = cols - area->x;
863 get_range(collection->drag_box_y[0], collection->drag_box_y[1],
864 collection->item_height, &area->y, &area->height);
867 static void collection_process_area(Collection *collection,
868 GdkRectangle *area,
869 GdkFunction fn,
870 guint32 time)
872 int x, y;
873 int rows = collection_get_rows(collection);
874 int cols = collection->columns;
875 guint32 stacked_time;
876 int item;
877 gboolean changed = FALSE;
878 guint old_selected;
880 g_return_if_fail(fn == GDK_SET || fn == GDK_INVERT);
882 old_selected = collection->number_selected;
884 stacked_time = current_event_time;
885 current_event_time = time;
887 collection->block_selection_changed++;
889 for (y = area->y; y < area->y + area->height && y < rows; y++)
890 for (x = area->x; x < area->x + area->width && x < cols; x++)
892 item = collection_rowcol_to_item(collection, y, x);
893 if (item < collection->number_of_items) {
894 if (fn == GDK_INVERT)
895 collection_item_set_selected(
896 collection, item,
897 !collection-> items[item].selected,
898 FALSE);
899 else
900 collection_item_set_selected(
901 collection, item, TRUE, FALSE);
903 changed = TRUE;
907 if (collection->number_selected && !old_selected)
908 g_signal_emit(collection,
909 collection_signals[GAIN_SELECTION], 0,
910 current_event_time);
911 else if (!collection->number_selected && old_selected)
912 g_signal_emit(collection,
913 collection_signals[LOSE_SELECTION], 0,
914 current_event_time);
916 collection_unblock_selection_changed(collection,
917 current_event_time, changed);
918 current_event_time = stacked_time;
921 static gint collection_motion_notify(GtkWidget *widget,
922 GdkEventMotion *event)
924 Collection *collection;
925 gint x, y;
927 g_return_val_if_fail(widget != NULL, FALSE);
928 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
929 g_return_val_if_fail(event != NULL, FALSE);
931 collection = COLLECTION(widget);
933 if (!collection->lasso_box)
934 return FALSE;
936 if (event->window != widget->window)
937 gdk_window_get_pointer(widget->window, &x, &y, NULL);
938 else
940 x = event->x;
941 y = event->y;
944 remove_lasso_box(collection);
945 collection->drag_box_x[1] = x;
946 collection->drag_box_y[1] = y;
947 add_lasso_box(collection);
948 return TRUE;
951 static void add_lasso_box(Collection *collection)
953 g_return_if_fail(collection != NULL);
954 g_return_if_fail(IS_COLLECTION(collection));
955 g_return_if_fail(collection->lasso_box == FALSE);
957 collection->lasso_box = TRUE;
958 draw_lasso_box(collection);
961 static void draw_lasso_box(Collection *collection)
963 GtkWidget *widget;
964 int x, y, width, height;
966 widget = GTK_WIDGET(collection);
968 x = MIN(collection->drag_box_x[0], collection->drag_box_x[1]);
969 y = MIN(collection->drag_box_y[0], collection->drag_box_y[1]);
970 width = abs(collection->drag_box_x[1] - collection->drag_box_x[0]);
971 height = abs(collection->drag_box_y[1] - collection->drag_box_y[0]);
973 /* XXX: A redraw bug sometimes leaves a one-pixel dot on the screen.
974 * As a quick hack, don't draw boxes that small for now...
976 if (width || height)
977 gdk_draw_rectangle(widget->window, collection->xor_gc, FALSE,
978 x, y, width, height);
981 static void abort_lasso(Collection *collection)
983 if (collection->lasso_box)
984 remove_lasso_box(collection);
987 static void remove_lasso_box(Collection *collection)
989 g_return_if_fail(collection != NULL);
990 g_return_if_fail(IS_COLLECTION(collection));
991 g_return_if_fail(collection->lasso_box == TRUE);
993 draw_lasso_box(collection);
995 collection->lasso_box = FALSE;
997 return;
1000 /* Make sure that 'item' is fully visible (vertically), scrolling if not. */
1001 static void scroll_to_show(Collection *collection, int item)
1003 int first, last, row, col;
1005 g_return_if_fail(collection != NULL);
1006 g_return_if_fail(IS_COLLECTION(collection));
1008 collection_item_to_rowcol(collection, item, &row, &col);
1009 get_visible_limits(collection, &first, &last);
1011 if (row <= first)
1013 gtk_adjustment_set_value(collection->vadj,
1014 row * collection->item_height);
1016 else if (row >= last)
1018 GtkWidget *widget = (GtkWidget *) collection;
1019 gint height;
1021 if (GTK_WIDGET_REALIZED(widget))
1023 height = collection->vadj->page_size;
1024 gtk_adjustment_set_value(collection->vadj,
1025 (row + 1) * collection->item_height - height);
1030 /* Return the first and last rows which are [partly] visible. Does not
1031 * ensure that the rows actually exist (contain items).
1033 static void get_visible_limits(Collection *collection, int *first, int *last)
1035 GtkWidget *widget = (GtkWidget *) collection;
1036 gint scroll = 0, height;
1038 g_return_if_fail(collection != NULL);
1039 g_return_if_fail(IS_COLLECTION(collection));
1040 g_return_if_fail(first != NULL && last != NULL);
1042 if (!GTK_WIDGET_REALIZED(widget))
1044 *first = 0;
1045 *last = 0;
1047 else
1049 scroll = collection->vadj->value;
1050 height = collection->vadj->page_size;
1052 *first = MAX(scroll / collection->item_height, 0);
1053 *last = (scroll + height - 1) /collection->item_height;
1055 if (*last < *first)
1056 *last = *first;
1060 /* Cancel the current wink effect. */
1061 static void cancel_wink(Collection *collection)
1063 gint item;
1065 g_return_if_fail(collection != NULL);
1066 g_return_if_fail(IS_COLLECTION(collection));
1067 g_return_if_fail(collection->wink_item != -1);
1069 item = collection->wink_item;
1071 collection->wink_item = -1;
1072 g_source_remove(collection->wink_timeout);
1074 collection_draw_item(collection, item, TRUE);
1077 /* Draw/undraw a box around collection->wink_item */
1078 static void invert_wink(Collection *collection)
1080 GdkRectangle area;
1081 gint row, col;
1083 g_return_if_fail(collection->wink_item >= 0);
1085 if (!GTK_WIDGET_REALIZED(GTK_WIDGET(collection)))
1086 return;
1088 collection_item_to_rowcol(collection, collection->wink_item,
1089 &row, &col);
1090 collection_get_item_area(collection, row, col, &area);
1092 gdk_draw_rectangle(((GtkWidget *) collection)->window,
1093 collection->xor_gc, FALSE,
1094 area.x, area.y,
1095 collection->item_width - 1,
1096 area.height - 1);
1099 static gboolean wink_timeout(Collection *collection)
1101 gint item;
1103 g_return_val_if_fail(collection != NULL, FALSE);
1104 g_return_val_if_fail(IS_COLLECTION(collection), FALSE);
1105 g_return_val_if_fail(collection->wink_item != -1, FALSE);
1107 item = collection->wink_item;
1109 if (collection->winks_left-- > 0)
1111 invert_wink(collection);
1112 return TRUE;
1115 collection->wink_item = -1;
1117 collection_draw_item(collection, item, TRUE);
1119 return FALSE;
1122 /* Change the selected state of an item.
1123 * Send GAIN/LOSE signals if 'signal' is TRUE.
1124 * Send SELECTION_CHANGED unless blocked.
1125 * Updates number_selected and redraws the item.
1127 static void collection_item_set_selected(Collection *collection,
1128 gint item,
1129 gboolean selected,
1130 gboolean signal)
1132 g_return_if_fail(collection != NULL);
1133 g_return_if_fail(IS_COLLECTION(collection));
1134 g_return_if_fail(item >= 0 && item < collection->number_of_items);
1136 if (collection->items[item].selected == selected)
1137 return;
1139 collection->items[item].selected = selected;
1140 collection_draw_item(collection, item, TRUE);
1142 if (selected)
1144 collection->number_selected++;
1145 if (signal && collection->number_selected == 1)
1146 g_signal_emit(collection,
1147 collection_signals[GAIN_SELECTION], 0,
1148 current_event_time);
1150 else
1152 collection->number_selected--;
1153 if (signal && collection->number_selected == 0)
1154 g_signal_emit(collection,
1155 collection_signals[LOSE_SELECTION], 0,
1156 current_event_time);
1159 EMIT_SELECTION_CHANGED(collection, current_event_time);
1162 /* Functions for managing collections */
1164 /* Remove all objects from the collection */
1165 void collection_clear(Collection *collection)
1167 collection_delete_if(collection, NULL, NULL);
1170 /* Inserts a new item at the end. The new item is unselected, and its
1171 * number is returned.
1173 gint collection_insert(Collection *collection, gpointer data, gpointer view)
1175 int item;
1177 /* g_return_val_if_fail(IS_COLLECTION(collection), -1); (slow) */
1179 item = collection->number_of_items;
1181 if (item >= collection->array_size)
1182 resize_arrays(collection, item + (item >> 1));
1184 collection->items[item].data = data;
1185 collection->items[item].view_data = view;
1186 collection->items[item].selected = FALSE;
1188 collection->number_of_items++;
1190 gtk_widget_queue_resize(GTK_WIDGET(collection));
1192 collection_draw_item(collection, item, FALSE);
1194 return item;
1197 void collection_unselect_item(Collection *collection, gint item)
1199 collection_item_set_selected(collection, item, FALSE, TRUE);
1202 void collection_select_item(Collection *collection, gint item)
1204 collection_item_set_selected(collection, item, TRUE, TRUE);
1207 void collection_toggle_item(Collection *collection, gint item)
1209 collection_item_set_selected(collection, item,
1210 !collection->items[item].selected, TRUE);
1213 /* Select all items in the collection */
1214 void collection_select_all(Collection *collection)
1216 int item = 0;
1218 g_return_if_fail(collection != NULL);
1219 g_return_if_fail(IS_COLLECTION(collection));
1221 if (collection->number_selected == collection->number_of_items)
1222 return; /* Nothing to do */
1224 while (collection->number_selected < collection->number_of_items)
1226 while (collection->items[item].selected)
1227 item++;
1229 collection->items[item].selected = TRUE;
1230 collection_draw_item(collection, item, TRUE);
1231 item++;
1233 collection->number_selected++;
1236 g_signal_emit(collection, collection_signals[GAIN_SELECTION], 0,
1237 current_event_time);
1238 EMIT_SELECTION_CHANGED(collection, current_event_time);
1241 /* Toggle all items in the collection */
1242 void collection_invert_selection(Collection *collection)
1244 int item;
1246 g_return_if_fail(collection != NULL);
1247 g_return_if_fail(IS_COLLECTION(collection));
1249 if (collection->number_selected == 0)
1251 collection_select_all(collection);
1252 return;
1254 else if (collection->number_of_items == collection->number_selected)
1256 collection_clear_selection(collection);
1257 return;
1260 for (item = 0; item < collection->number_of_items; item++)
1261 collection->items[item].selected =
1262 !collection->items[item].selected;
1264 collection->number_selected = collection->number_of_items -
1265 collection->number_selected;
1267 /* Have to redraw everything... */
1268 gtk_widget_queue_draw(GTK_WIDGET(collection));
1270 EMIT_SELECTION_CHANGED(collection, current_event_time);
1273 /* Unselect all items except number item, which is selected (-1 to unselect
1274 * everything).
1276 void collection_clear_except(Collection *collection, gint item)
1278 int i = 0;
1279 int end; /* Selected items to end up with */
1281 g_return_if_fail(collection != NULL);
1282 g_return_if_fail(IS_COLLECTION(collection));
1283 g_return_if_fail(item >= -1 && item < collection->number_of_items);
1285 if (item == -1)
1286 end = 0;
1287 else
1289 collection_select_item(collection, item);
1290 end = 1;
1293 if (collection->number_selected == 0)
1294 return;
1296 while (collection->number_selected > end)
1298 while (i == item || !collection->items[i].selected)
1299 i++;
1301 collection->items[i].selected = FALSE;
1302 collection_draw_item(collection, i, TRUE);
1303 i++;
1305 collection->number_selected--;
1308 if (end == 0)
1309 g_signal_emit(collection, collection_signals[LOSE_SELECTION], 0,
1310 current_event_time);
1311 EMIT_SELECTION_CHANGED(collection, current_event_time);
1314 /* Unselect all items in the collection */
1315 void collection_clear_selection(Collection *collection)
1317 g_return_if_fail(collection != NULL);
1318 g_return_if_fail(IS_COLLECTION(collection));
1320 collection_clear_except(collection, -1);
1323 /* Force a redraw of the specified item, if it is visible */
1324 void collection_draw_item(Collection *collection, gint item, gboolean blank)
1326 GdkRectangle area;
1327 GtkWidget *widget;
1328 int row, col;
1330 g_return_if_fail(collection != NULL);
1331 /* g_return_if_fail(IS_COLLECTION(collection)); (slow) */
1332 g_return_if_fail(item >= 0 &&
1333 (item == 0 || item < collection->number_of_items));
1335 widget = GTK_WIDGET(collection);
1336 if (!GTK_WIDGET_REALIZED(widget))
1337 return;
1339 collection_item_to_rowcol(collection, item, &row, &col);
1341 collection_get_item_area(collection, row, col, &area);
1343 gdk_window_invalidate_rect(widget->window, &area, FALSE);
1346 void collection_set_item_size(Collection *collection, int width, int height)
1348 GtkWidget *widget;
1350 g_return_if_fail(collection != NULL);
1351 g_return_if_fail(IS_COLLECTION(collection));
1352 g_return_if_fail(width > 4 && height > 4);
1354 if (collection->item_width == width &&
1355 collection->item_height == height)
1356 return;
1358 widget = GTK_WIDGET(collection);
1360 collection->item_width = width;
1361 collection->item_height = height;
1363 if (GTK_WIDGET_REALIZED(widget))
1365 gint window_width;
1367 gdk_drawable_get_size(widget->window, &window_width, NULL);
1368 collection->columns = MAX(window_width / collection->item_width,
1370 if (collection->cursor_item != -1)
1371 scroll_to_show(collection, collection->cursor_item);
1372 gtk_widget_queue_draw(widget);
1375 gtk_widget_queue_resize(GTK_WIDGET(collection));
1378 static int (*cmp_callback)(const void *a, const void *b) = NULL;
1379 static int collection_cmp(const void *a, const void *b)
1381 return cmp_callback(((CollectionItem *) a)->data,
1382 ((CollectionItem *) b)->data);
1384 static int collection_rcmp(const void *a, const void *b)
1386 return -cmp_callback(((CollectionItem *) a)->data,
1387 ((CollectionItem *) b)->data);
1390 /* Cursor is positioned on item with the same data as before the sort.
1391 * Same for the wink item.
1393 void collection_qsort(Collection *collection,
1394 int (*compar)(const void *, const void *),
1395 GtkSortType order)
1397 int cursor, wink, items, wink_on_map;
1398 gpointer cursor_data = NULL;
1399 gpointer wink_data = NULL;
1400 gpointer wink_on_map_data = NULL;
1401 CollectionItem *array;
1402 int i;
1403 int mul = order == GTK_SORT_ASCENDING ? 1 : -1;
1405 g_return_if_fail(collection != NULL);
1406 g_return_if_fail(IS_COLLECTION(collection));
1407 g_return_if_fail(compar != NULL);
1408 g_return_if_fail(cmp_callback == NULL);
1410 /* Check to see if it needs sorting (saves redrawing) */
1411 if (collection->number_of_items < 2)
1412 return;
1414 array = collection->items;
1415 for (i = 1; i < collection->number_of_items; i++)
1417 if (mul * compar(array[i - 1].data, array[i].data) > 0)
1418 break;
1420 if (i == collection->number_of_items)
1421 return; /* Already sorted */
1423 items = collection->number_of_items;
1425 wink_on_map = collection->wink_on_map;
1426 if (wink_on_map >= 0 && wink_on_map < items)
1428 wink_on_map_data = collection->items[wink_on_map].data;
1429 collection->wink_on_map = -1;
1431 else
1432 wink = -1;
1434 wink = collection->wink_item;
1435 if (wink >= 0 && wink < items)
1437 wink_data = collection->items[wink].data;
1438 collection->wink_item = -1;
1440 else
1441 wink = -1;
1443 cursor = collection->cursor_item;
1444 if (cursor >= 0 && cursor < items)
1445 cursor_data = collection->items[cursor].data;
1446 else
1447 cursor = -1;
1449 cmp_callback = compar;
1450 qsort(collection->items, items, sizeof(collection->items[0]),
1451 order == GTK_SORT_ASCENDING ? collection_cmp
1452 : collection_rcmp);
1453 cmp_callback = NULL;
1455 if (cursor > -1 || wink > -1 || wink_on_map > -1)
1457 int item;
1459 for (item = 0; item < items; item++)
1461 if (collection->items[item].data == cursor_data)
1462 collection_set_cursor_item(collection, item,
1463 TRUE);
1464 if (collection->items[item].data == wink_on_map_data)
1465 collection->wink_on_map = item;
1466 if (collection->items[item].data == wink_data)
1468 collection->cursor_item_old = item;
1469 collection->wink_item = item;
1470 scroll_to_show(collection, item);
1475 gtk_widget_queue_draw(GTK_WIDGET(collection));
1478 /* Find an item in a sorted collection.
1479 * Returns the item number, or -1 if not found.
1481 int collection_find_item(Collection *collection, gpointer data,
1482 int (*compar)(const void *, const void *),
1483 GtkSortType order)
1485 int lower, upper;
1486 int mul = order == GTK_SORT_ASCENDING ? 1 : -1;
1488 g_return_val_if_fail(collection != NULL, -1);
1489 g_return_val_if_fail(IS_COLLECTION(collection), -1);
1490 g_return_val_if_fail(compar != NULL, -1);
1492 /* If item is here, then: lower <= i < upper */
1493 lower = 0;
1494 upper = collection->number_of_items;
1496 while (lower < upper)
1498 int i, cmp;
1500 i = (lower + upper) >> 1;
1502 cmp = mul * compar(collection->items[i].data, data);
1503 if (cmp == 0)
1504 return i;
1506 if (cmp > 0)
1507 upper = i;
1508 else
1509 lower = i + 1;
1512 return -1;
1515 /* Return the number of the item under the point (x,y), or -1 for none.
1516 * This may call your test_point callback. The point is relative to the
1517 * collection's origin.
1519 int collection_get_item(Collection *collection, int x, int y)
1521 int row, col;
1522 int width;
1523 int item;
1525 g_return_val_if_fail(collection != NULL, -1);
1527 col = x / collection->item_width;
1528 row = y / collection->item_height;
1530 if (col >= collection->columns)
1531 col = collection->columns - 1;
1533 if (col < 0 || row < 0)
1534 return -1;
1536 if (col == collection->columns - 1)
1537 width = collection->item_width << 1;
1538 else
1539 width = collection->item_width;
1541 item = collection_rowcol_to_item(collection, row, col);
1542 if (item >= collection->number_of_items)
1543 return -1;
1545 x -= col * collection->item_width;
1546 y -= row * collection->item_height;
1548 if (collection->test_point(collection, x, y,
1549 &collection->items[item], width, collection->item_height,
1550 collection->cb_user_data))
1551 return item;
1553 return -1;
1556 /* Set the cursor/highlight over the given item. Passing -1
1557 * hides the cursor. As a special case, you may set the cursor item
1558 * to zero when there are no items.
1560 void collection_set_cursor_item(Collection *collection, gint item,
1561 gboolean may_scroll)
1563 int old_item;
1565 g_return_if_fail(collection != NULL);
1566 g_return_if_fail(IS_COLLECTION(collection));
1567 g_return_if_fail(item >= -1 &&
1568 (item < collection->number_of_items || item == 0));
1570 old_item = collection->cursor_item;
1572 if (old_item == item)
1573 return;
1575 collection->cursor_item = item;
1577 if (old_item != -1)
1578 collection_draw_item(collection, old_item, TRUE);
1580 if (item != -1)
1582 collection_draw_item(collection, item, TRUE);
1583 if (may_scroll)
1584 scroll_to_show(collection, item);
1586 else if (old_item != -1)
1587 collection->cursor_item_old = old_item;
1590 /* Briefly highlight an item to draw the user's attention to it.
1591 * -1 cancels the effect, as does deleting items, sorting the collection
1592 * or starting a new wink effect.
1593 * Otherwise, the effect will cancel itself after a short pause.
1594 * */
1595 void collection_wink_item(Collection *collection, gint item)
1597 g_return_if_fail(collection != NULL);
1598 g_return_if_fail(IS_COLLECTION(collection));
1599 g_return_if_fail(item >= -1 && item < collection->number_of_items);
1601 if (collection->wink_item != -1)
1602 cancel_wink(collection);
1603 if (item == -1)
1604 return;
1606 if (!GTK_WIDGET_MAPPED(GTK_WIDGET(collection)))
1608 collection->wink_on_map = item;
1609 return;
1612 collection->cursor_item_old = collection->wink_item = item;
1613 collection->winks_left = MAX_WINKS;
1615 collection->wink_timeout = g_timeout_add(70,
1616 (GSourceFunc) wink_timeout,
1617 collection);
1618 scroll_to_show(collection, item);
1619 invert_wink(collection);
1621 gdk_flush();
1624 /* Call test(item, data) on each item in the collection.
1625 * Remove all items for which it returns TRUE. test() should
1626 * free the data before returning TRUE. The collection is in an
1627 * inconsistant state during this call (ie, when test() is called).
1629 * If test is NULL, remove all items.
1631 void collection_delete_if(Collection *collection,
1632 gboolean (*test)(gpointer item, gpointer data),
1633 gpointer data)
1635 int in, out = 0;
1636 int selected = 0;
1637 int cursor;
1639 g_return_if_fail(collection != NULL);
1640 g_return_if_fail(IS_COLLECTION(collection));
1642 cursor = collection->cursor_item;
1644 for (in = 0; in < collection->number_of_items; in++)
1646 if (test && !test(collection->items[in].data, data))
1648 /* Keep item */
1649 if (collection->items[in].selected)
1651 collection->items[out].selected = TRUE;
1652 selected++;
1654 else
1655 collection->items[out].selected = FALSE;
1657 collection->items[out].data =
1658 collection->items[in].data;
1659 collection->items[out].view_data =
1660 collection->items[in].view_data;
1661 out++;
1663 else
1665 /* Remove item */
1666 if (collection->free_item)
1667 collection->free_item(collection,
1668 &collection->items[in]);
1670 if (collection->cursor_item >= in)
1671 cursor--;
1675 if (in != out)
1677 collection->cursor_item = cursor;
1679 if (collection->wink_item != -1)
1681 collection->wink_item = -1;
1682 g_source_remove(collection->wink_timeout);
1685 collection->number_of_items = out;
1686 if (collection->number_selected && !selected)
1688 /* We've lost all the selected items */
1689 g_signal_emit(collection,
1690 collection_signals[LOSE_SELECTION], 0,
1691 current_event_time);
1694 collection->number_selected = selected;
1695 resize_arrays(collection,
1696 MAX(collection->number_of_items, MINIMUM_ITEMS));
1698 if (GTK_WIDGET_REALIZED(GTK_WIDGET(collection)))
1699 gtk_widget_queue_draw(GTK_WIDGET(collection));
1701 gtk_widget_queue_resize(GTK_WIDGET(collection));
1705 /* Move the cursor by the given row and column offsets.
1706 * Moving by (0,0) can be used to simply make the cursor appear.
1708 void collection_move_cursor(Collection *collection, int drow, int dcol)
1710 int row, col, item;
1711 int first, last, total_rows, total_cols;
1713 g_return_if_fail(collection != NULL);
1714 g_return_if_fail(IS_COLLECTION(collection));
1716 if (!collection->number_of_items)
1718 /* Show the cursor, even though there are no items */
1719 collection_set_cursor_item(collection, 0, TRUE);
1720 return;
1723 get_visible_limits(collection, &first, &last);
1724 total_rows = collection_get_rows(collection);
1725 total_cols = collection_get_cols(collection);
1727 item = collection->cursor_item;
1728 if (item == -1)
1730 item = MIN(collection->cursor_item_old,
1731 collection->number_of_items - 1);
1734 if (item == -1)
1736 col = 0;
1737 row = first;
1739 else
1741 collection_item_to_rowcol(collection, item, &row, &col);
1743 col += dcol;
1744 if (collection->vertical_order)
1746 col = MAX(0,col);
1747 col = MIN(col, total_cols - 1);
1750 if (row < first)
1751 row = first;
1752 else if (row > last)
1753 row = last;
1754 else {
1755 row += drow;
1756 if (collection->vertical_order)
1758 if (row >= total_rows)
1760 row = 0;
1761 col += 1;
1764 else
1766 row = MAX(row, 0);
1767 row = MIN(row, total_rows - 1);
1772 item = collection_rowcol_to_item(collection, row, col);
1774 item = MAX(item, 0);
1775 item = MIN(item, collection->number_of_items-1);
1777 collection_set_cursor_item(collection, item, TRUE);
1780 /* Start a lasso box drag */
1781 void collection_lasso_box(Collection *collection, int x, int y)
1783 collection->drag_box_x[0] = x;
1784 collection->drag_box_y[0] = y;
1785 collection->drag_box_x[1] = x;
1786 collection->drag_box_y[1] = y;
1788 add_lasso_box(collection);
1791 /* Remove the lasso box. Applies fn to each item inside the box.
1792 * fn may be GDK_INVERT, GDK_SET, GDK_NOOP or GDK_CLEAR.
1794 void collection_end_lasso(Collection *collection, GdkFunction fn)
1796 if (fn != GDK_CLEAR)
1798 GdkRectangle region;
1800 find_lasso_area(collection, &region);
1802 collection_process_area(collection, &region, fn,
1803 GDK_CURRENT_TIME);
1806 abort_lasso(collection);
1809 /* Unblock the selection_changed signal, emitting the signal if the
1810 * block counter reaches zero and emit is TRUE.
1812 void collection_unblock_selection_changed(Collection *collection,
1813 guint time,
1814 gboolean emit)
1816 g_return_if_fail(collection != NULL);
1817 g_return_if_fail(IS_COLLECTION(collection));
1818 g_return_if_fail(collection->block_selection_changed > 0);
1820 collection->block_selection_changed--;
1822 if (emit && !collection->block_selection_changed)
1823 g_signal_emit(collection,
1824 collection_signals[SELECTION_CHANGED], 0, time);
1827 /* Translate the item number to the (row, column) form */
1828 void collection_item_to_rowcol (const Collection *collection,
1829 int item, int *row, int *col)
1831 if (!collection->vertical_order)
1833 *row = item / collection->columns;
1834 *col = item % collection->columns;
1836 else
1838 int rows = collection_get_rows(collection);
1839 *row = item % rows;
1840 *col = item / rows;
1846 /* Translate the (row, column) form to the item number.
1847 * May return a number >= collection->number_of_items.
1849 int collection_rowcol_to_item(const Collection *collection, int row, int col)
1851 if (!collection->vertical_order)
1852 return row * collection->columns + col;
1853 else
1855 int rows = collection_get_rows(collection);
1856 if (row >= rows)
1857 return collection->number_of_items;
1858 return row + col * rows;