r4255: Make Path optional in PanelRemove SOAP call, allowing removal of items based
[rox-filer/translations.git] / ROX-Filer / src / collection.c
blob31e5639108b19fa317c881271f9bef5e253d8d8d
1 /*
2 * $Id$
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
10 * double-clicks etc.
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)
15 * any later version.
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
20 * more details.
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
27 #include "config.h"
29 #include <stdlib.h>
31 #include <gtk/gtk.h>
32 #include <gdk/gdkkeysyms.h>
33 #include "global.h"
35 #include "collection.h"
37 #define MIN_WIDTH 80
38 #define MIN_HEIGHT 60
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)
49 enum
51 PROP_0,
52 PROP_VADJUSTMENT
55 /* Signals:
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.
72 enum
74 GAIN_SELECTION,
75 LOSE_SELECTION,
76 SELECTION_CHANGED,
77 LAST_SIGNAL
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,
88 int item,
89 GdkRectangle *area);
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,
103 guint prop_id,
104 GValue *value,
105 GParamSpec *pspec);
106 static void collection_set_property(GObject *object,
107 guint prop_id,
108 const GValue *value,
109 GParamSpec *pspec);
110 static gint collection_expose(GtkWidget *widget, GdkEventExpose *event);
111 static void default_draw_item(GtkWidget *widget,
112 CollectionItem *data,
113 GdkRectangle *area,
114 gpointer user_data);
115 static gboolean default_test_point(Collection *collection,
116 int point_x, int point_y,
117 CollectionItem *data,
118 int width, int height,
119 gpointer user_data);
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,
131 gint item,
132 gboolean selected,
133 gboolean signal);
134 static gint collection_scroll_event(GtkWidget *widget, GdkEventScroll *event);
135 static int collection_get_rows(const Collection *collection);
136 static int collection_get_cols(const Collection *collection);
139 /* The number of rows, at least 1. */
140 static inline int collection_get_rows(const Collection *collection)
142 int rows = (collection->number_of_items + collection->columns - 1) /
143 collection->columns;
144 return MAX(rows, 1);
147 /* The number of columns _actually_ displayed, at least 1. This
148 * function is required in vertical_order layout-based manipulation
149 * such as moving the cursor to detect the last column. */
150 static inline int collection_get_cols(const Collection *collection)
152 if (collection->vertical_order)
154 int rows = collection_get_rows(collection);
155 int cols = (collection->number_of_items + rows - 1) / rows;
156 return MAX(1, cols);
158 else
159 return collection->columns;
162 static void draw_focus_at(Collection *collection, GdkRectangle *area)
164 GtkWidget *widget;
165 GtkStateType state;
167 widget = GTK_WIDGET(collection);
169 if (GTK_WIDGET_FLAGS(widget) & GTK_HAS_FOCUS)
170 state = GTK_STATE_ACTIVE;
171 else
172 state = GTK_STATE_INSENSITIVE;
174 gtk_paint_focus(widget->style,
175 widget->window,
176 state,
177 NULL,
178 widget,
179 "collection",
180 area->x, area->y,
181 collection->item_width,
182 area->height);
185 static void draw_one_item(Collection *collection, int item, GdkRectangle *area)
187 if (item < collection->number_of_items)
189 collection->draw_item((GtkWidget *) collection,
190 &collection->items[item],
191 area, collection->cb_user_data);
194 if (item == collection->cursor_item)
195 draw_focus_at(collection, area);
198 GType collection_get_type(void)
200 static GType my_type = 0;
202 if (!my_type)
204 static const GTypeInfo info =
206 sizeof(CollectionClass),
207 NULL, /* base_init */
208 NULL, /* base_finalise */
209 (GClassInitFunc) collection_class_init,
210 NULL, /* class_finalise */
211 NULL, /* class_data */
212 sizeof(Collection),
213 0, /* n_preallocs */
214 collection_init
217 my_type = g_type_register_static(gtk_widget_get_type(),
218 "Collection", &info, 0);
221 return my_type;
224 typedef void (*FinalizeFn)(GObject *object);
226 static void collection_class_init(GObjectClass *gclass, gpointer data)
228 CollectionClass *collection_class = (CollectionClass *) gclass;
229 GtkObjectClass *object_class = (GtkObjectClass *) gclass;
230 GtkWidgetClass *widget_class = (GtkWidgetClass *) gclass;
232 parent_class = gtk_type_class(gtk_widget_get_type());
234 object_class->destroy = collection_destroy;
235 G_OBJECT_CLASS(object_class)->finalize =
236 (FinalizeFn) collection_finalize;
238 widget_class->realize = collection_realize;
239 widget_class->expose_event = collection_expose;
240 widget_class->size_request = collection_size_request;
241 widget_class->size_allocate = collection_size_allocate;
243 widget_class->key_press_event = collection_key_press;
245 widget_class->motion_notify_event = collection_motion_notify;
246 widget_class->map = collection_map;
247 widget_class->scroll_event = collection_scroll_event;
249 gclass->set_property = collection_set_property;
250 gclass->get_property = collection_get_property;
252 collection_class->gain_selection = NULL;
253 collection_class->lose_selection = NULL;
254 collection_class->selection_changed = NULL;
256 collection_signals[GAIN_SELECTION] = g_signal_new("gain_selection",
257 G_TYPE_FROM_CLASS(gclass),
258 G_SIGNAL_RUN_LAST,
259 G_STRUCT_OFFSET(CollectionClass,
260 gain_selection),
261 NULL, NULL,
262 g_cclosure_marshal_VOID__INT,
263 G_TYPE_NONE, 1,
264 G_TYPE_INT);
266 collection_signals[LOSE_SELECTION] = g_signal_new("lose_selection",
267 G_TYPE_FROM_CLASS(gclass),
268 G_SIGNAL_RUN_LAST,
269 G_STRUCT_OFFSET(CollectionClass,
270 lose_selection),
271 NULL, NULL,
272 g_cclosure_marshal_VOID__INT,
273 G_TYPE_NONE, 1,
274 G_TYPE_INT);
276 collection_signals[SELECTION_CHANGED] = g_signal_new(
277 "selection_changed",
278 G_TYPE_FROM_CLASS(gclass),
279 G_SIGNAL_RUN_LAST,
280 G_STRUCT_OFFSET(CollectionClass,
281 selection_changed),
282 NULL, NULL,
283 g_cclosure_marshal_VOID__INT,
284 G_TYPE_NONE, 1,
285 G_TYPE_INT);
287 g_object_class_install_property(gclass,
288 PROP_VADJUSTMENT,
289 g_param_spec_object("vadjustment",
290 "Vertical Adjustment",
291 "The GtkAdjustment for the vertical position.",
292 GTK_TYPE_ADJUSTMENT,
293 G_PARAM_READWRITE | G_PARAM_CONSTRUCT));
296 static void collection_init(GTypeInstance *instance, gpointer g_class)
298 Collection *object = (Collection *) instance;
300 g_return_if_fail(object != NULL);
301 g_return_if_fail(IS_COLLECTION(object));
303 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(object), GTK_CAN_FOCUS);
305 object->number_of_items = 0;
306 object->number_selected = 0;
307 object->block_selection_changed = 0;
308 object->columns = 1;
309 object->vertical_order = FALSE;
310 object->item_width = 64;
311 object->item_height = 64;
312 object->vadj = NULL;
314 object->items = g_new(CollectionItem, MINIMUM_ITEMS);
315 object->cursor_item = -1;
316 object->cursor_item_old = -1;
317 object->wink_item = -1;
318 object->wink_on_map = -1;
319 object->array_size = MINIMUM_ITEMS;
320 object->draw_item = default_draw_item;
321 object->test_point = default_test_point;
322 object->free_item = NULL;
325 GtkWidget* collection_new(void)
327 return GTK_WIDGET(gtk_widget_new(collection_get_type(), NULL));
330 /* After this we are unusable, but our data (if any) is still hanging around.
331 * It will be freed later with finalize.
333 static void collection_destroy(GtkObject *object)
335 Collection *collection;
337 g_return_if_fail(object != NULL);
338 g_return_if_fail(IS_COLLECTION(object));
340 collection = COLLECTION(object);
342 collection_clear(collection);
344 if (collection->vadj)
346 g_object_unref(G_OBJECT(collection->vadj));
347 collection->vadj = NULL;
350 if (GTK_OBJECT_CLASS(parent_class)->destroy)
351 (*GTK_OBJECT_CLASS(parent_class)->destroy)(object);
354 /* This is the last thing that happens to us. Free all data. */
355 static void collection_finalize(GObject *object)
357 Collection *collection;
359 collection = COLLECTION(object);
361 g_return_if_fail(collection->number_of_items == 0);
363 g_free(collection->items);
365 if (G_OBJECT_CLASS(parent_class)->finalize)
366 G_OBJECT_CLASS(parent_class)->finalize(object);
369 static void collection_map(GtkWidget *widget)
371 Collection *collection = COLLECTION(widget);
373 if (GTK_WIDGET_CLASS(parent_class)->map)
374 (*GTK_WIDGET_CLASS(parent_class)->map)(widget);
376 if (collection->wink_on_map >= 0)
378 collection_wink_item(collection, collection->wink_on_map);
379 collection->wink_on_map = -1;
383 static void collection_realize(GtkWidget *widget)
385 Collection *collection;
386 GdkWindowAttr attributes;
387 gint attributes_mask;
388 GdkGCValues xor_values;
389 GdkColor *bg, *fg;
391 g_return_if_fail(widget != NULL);
392 g_return_if_fail(IS_COLLECTION(widget));
393 g_return_if_fail(widget->parent != NULL);
395 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
396 collection = COLLECTION(widget);
398 attributes.x = widget->allocation.x;
399 attributes.y = widget->allocation.y;
400 attributes.width = widget->allocation.width;
401 attributes.height = widget->allocation.height;
402 attributes.wclass = GDK_INPUT_OUTPUT;
403 attributes.window_type = GDK_WINDOW_CHILD;
404 attributes.event_mask = gtk_widget_get_events(widget) |
405 GDK_EXPOSURE_MASK |
406 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK |
407 GDK_BUTTON1_MOTION_MASK | GDK_BUTTON2_MOTION_MASK |
408 GDK_BUTTON3_MOTION_MASK;
409 attributes.visual = gtk_widget_get_visual(widget);
410 attributes.colormap = gtk_widget_get_colormap(widget);
412 attributes_mask = GDK_WA_X | GDK_WA_Y |
413 GDK_WA_VISUAL | GDK_WA_COLORMAP;
414 widget->window = gdk_window_new(gtk_widget_get_parent_window(widget),
415 &attributes, attributes_mask);
417 widget->style = gtk_style_attach(widget->style, widget->window);
419 gdk_window_set_user_data(widget->window, widget);
420 gdk_window_set_background(widget->window,
421 &widget->style->bg[GTK_STATE_NORMAL]);
423 bg = &widget->style->bg[GTK_STATE_NORMAL];
424 fg = &widget->style->fg[GTK_STATE_NORMAL];
425 xor_values.function = GDK_XOR;
426 xor_values.foreground.pixel = fg->pixel ^ bg->pixel;
427 collection->xor_gc = gdk_gc_new_with_values(widget->window,
428 &xor_values,
429 GDK_GC_FOREGROUND
430 | GDK_GC_FUNCTION);
433 static void collection_size_request(GtkWidget *widget,
434 GtkRequisition *requisition)
436 Collection *collection = COLLECTION(widget);
437 int rows;
439 /* We ask for the total size we need; our containing viewport
440 * will deal with scrolling.
442 requisition->width = MIN_WIDTH;
443 rows = collection_get_rows(collection);
444 requisition->height = rows * collection->item_height;
447 static gboolean scroll_after_alloc(Collection *collection)
449 if (collection->wink_item != -1)
450 scroll_to_show(collection, collection->wink_item);
451 else if (collection->cursor_item != -1)
452 scroll_to_show(collection, collection->cursor_item);
453 g_object_unref(G_OBJECT(collection));
455 return FALSE;
458 static void collection_size_allocate(GtkWidget *widget,
459 GtkAllocation *allocation)
461 Collection *collection;
462 int old_columns;
463 gboolean cursor_visible = FALSE;
465 g_return_if_fail(widget != NULL);
466 g_return_if_fail(IS_COLLECTION(widget));
467 g_return_if_fail(allocation != NULL);
469 collection = COLLECTION(widget);
471 if (collection->cursor_item != -1)
473 int first, last;
474 int crow, ccol;
476 collection_item_to_rowcol(collection, collection->cursor_item,
477 &crow, &ccol);
479 get_visible_limits(collection, &first, &last);
481 cursor_visible = crow >= first && crow <= last;
484 old_columns = collection->columns;
486 widget->allocation = *allocation;
488 collection->columns = allocation->width / collection->item_width;
489 if (collection->columns < 1)
490 collection->columns = 1;
492 if (GTK_WIDGET_REALIZED(widget))
494 gdk_window_move_resize(widget->window,
495 allocation->x, allocation->y,
496 allocation->width, allocation->height);
498 if (cursor_visible)
499 scroll_to_show(collection, collection->cursor_item);
502 if (old_columns != collection->columns)
504 /* Need to go around again... */
505 gtk_widget_queue_resize(widget);
507 else if (collection->wink_item != -1 || collection->cursor_item != -1)
509 /* Viewport resets the adjustments after the alloc */
510 g_object_ref(G_OBJECT(collection));
511 g_idle_add((GSourceFunc) scroll_after_alloc, collection);
515 /* Return the area occupied by the item at (row, col) by filling
516 * in 'area'.
518 static void collection_get_item_area(Collection *collection,
519 int row, int col,
520 GdkRectangle *area)
523 area->x = col * collection->item_width;
524 area->y = row * collection->item_height;
526 area->width = collection->item_width;
527 area->height = collection->item_height;
528 if (col == collection->columns - 1)
529 area->width <<= 1;
532 static gint collection_expose(GtkWidget *widget, GdkEventExpose *event)
534 Collection *collection;
535 GdkRectangle item_area;
536 int row, col;
537 int item;
538 int start_row, last_row;
539 int start_col, last_col;
540 int phys_last_col;
542 g_return_val_if_fail(widget != NULL, FALSE);
543 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
544 g_return_val_if_fail(event != NULL, FALSE);
546 gtk_paint_flat_box(widget->style, widget->window, GTK_STATE_NORMAL,
547 GTK_SHADOW_NONE, &event->area,
548 widget, "base", 0, 0, -1, -1);
550 collection = COLLECTION(widget);
552 /* Calculate the ranges to plot */
553 start_row = event->area.y / collection->item_height;
554 last_row = (event->area.y + event->area.height - 1)
555 / collection->item_height;
557 if (last_row >= collection_get_rows(collection))
558 last_row = collection_get_rows(collection) - 1;
560 start_col = event->area.x / collection->item_width;
561 phys_last_col = (event->area.x + event->area.width - 1)
562 / collection->item_width;
564 /* The right-most column may be wider than the others.
565 * Therefore, to redraw the area after the last 'real' column
566 * we may have to draw the right-most column.
568 if (start_col >= collection->columns)
569 start_col = collection->columns - 1;
571 if (phys_last_col >= collection->columns)
572 last_col = collection->columns - 1;
573 else
574 last_col = phys_last_col;
577 for(row = start_row; row <= last_row; row++)
578 for(col = start_col; col <= last_col; col++)
580 item = collection_rowcol_to_item(collection, row, col);
581 if (item == 0 || item < collection->number_of_items) {
582 collection_get_item_area(collection,
583 row, col, &item_area);
584 draw_one_item(collection, item, &item_area);
588 if (collection->lasso_box)
589 draw_lasso_box(collection);
591 return FALSE;
594 static void default_draw_item(GtkWidget *widget,
595 CollectionItem *item,
596 GdkRectangle *area,
597 gpointer user_data)
599 gdk_draw_arc(widget->window,
600 item->selected ? widget->style->white_gc
601 : widget->style->black_gc,
602 TRUE,
603 area->x, area->y,
604 COLLECTION(widget)->item_width, area->height,
605 0, 360 * 64);
609 static gboolean default_test_point(Collection *collection,
610 int point_x, int point_y,
611 CollectionItem *item,
612 int width, int height,
613 gpointer user_data)
615 float f_x, f_y;
617 /* Convert to point in unit circle */
618 f_x = ((float) point_x / width) - 0.5;
619 f_y = ((float) point_y / height) - 0.5;
621 return (f_x * f_x) + (f_y * f_y) <= .25;
624 static void collection_set_property(GObject *object,
625 guint prop_id,
626 const GValue *value,
627 GParamSpec *pspec)
629 Collection *collection;
631 collection = COLLECTION(object);
633 switch (prop_id)
635 case PROP_VADJUSTMENT:
636 collection_set_adjustment(collection,
637 g_value_get_object(value));
638 break;
639 default:
640 G_OBJECT_WARN_INVALID_PROPERTY_ID(object,
641 prop_id, pspec);
642 break;
646 static void collection_set_adjustment(Collection *collection,
647 GtkAdjustment *vadj)
649 if (vadj)
650 g_return_if_fail(GTK_IS_ADJUSTMENT(vadj));
651 else
652 vadj = GTK_ADJUSTMENT(gtk_adjustment_new(0.0,
653 0.0, 0.0,
654 0.0, 0.0, 0.0));
656 if (collection->vadj == vadj)
657 return;
659 if (collection->vadj)
660 g_object_unref(G_OBJECT(collection->vadj));
662 collection->vadj = vadj;
663 g_object_ref(G_OBJECT(collection->vadj));
664 gtk_object_sink(GTK_OBJECT(collection->vadj));
667 static void collection_get_property(GObject *object,
668 guint prop_id,
669 GValue *value,
670 GParamSpec *pspec)
672 Collection *collection;
674 collection = COLLECTION(object);
676 switch (prop_id)
678 case PROP_VADJUSTMENT:
679 g_value_set_object(value, G_OBJECT(collection->vadj));
680 break;
681 default:
682 G_OBJECT_WARN_INVALID_PROPERTY_ID(object,
683 prop_id, pspec);
684 break;
688 static void resize_arrays(Collection *collection, guint new_size)
690 g_return_if_fail(collection != NULL);
691 g_return_if_fail(IS_COLLECTION(collection));
692 g_return_if_fail(new_size >= collection->number_of_items);
694 collection->items = g_realloc(collection->items,
695 sizeof(CollectionItem) * new_size);
696 collection->array_size = new_size;
699 static gint collection_key_press(GtkWidget *widget, GdkEventKey *event)
701 Collection *collection;
702 int item;
703 int key;
705 g_return_val_if_fail(widget != NULL, FALSE);
706 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
707 g_return_val_if_fail(event != NULL, FALSE);
709 collection = (Collection *) widget;
710 item = collection->cursor_item;
712 key = event->keyval;
713 if (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
715 if (key == GDK_Left || key == GDK_Right || \
716 key == GDK_Up || key == GDK_Down)
717 return TRUE;
718 return FALSE;
721 switch (key)
723 case GDK_Left:
724 collection_move_cursor(collection, 0, -1);
725 break;
726 case GDK_Right:
727 collection_move_cursor(collection, 0, 1);
728 break;
729 case GDK_Up:
730 collection_move_cursor(collection, -1, 0);
731 break;
732 case GDK_Down:
733 collection_move_cursor(collection, 1, 0);
734 break;
735 case GDK_Home:
736 collection_set_cursor_item(collection, 0, TRUE);
737 break;
738 case GDK_End:
739 collection_set_cursor_item(collection,
740 MAX((gint) collection->number_of_items - 1, 0),
741 TRUE);
742 break;
743 case GDK_Page_Up:
745 int first, last;
746 get_visible_limits(collection, &first, &last);
747 collection_move_cursor(collection, first - last - 1, 0);
748 break;
750 case GDK_Page_Down:
752 int first, last;
753 get_visible_limits(collection, &first, &last);
754 collection_move_cursor(collection, last - first + 1, 0);
755 break;
757 default:
758 return FALSE;
761 return TRUE;
764 /* Wheel mouse scrolling */
765 static gint collection_scroll_event(GtkWidget *widget, GdkEventScroll *event)
767 Collection *collection;
768 int diff = 0;
770 g_return_val_if_fail(widget != NULL, FALSE);
771 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
772 g_return_val_if_fail(event != NULL, FALSE);
774 collection = COLLECTION(widget);
776 if (event->direction == GDK_SCROLL_UP)
777 diff = -1;
778 else if (event->direction == GDK_SCROLL_DOWN)
779 diff = 1;
780 else
781 return FALSE;
783 if (diff)
785 int old_value = collection->vadj->value;
786 int new_value = 0;
787 gboolean box = collection->lasso_box;
788 int step = collection->vadj->page_increment / 2;
790 new_value = CLAMP(old_value + diff * step, 0.0,
791 collection->vadj->upper
792 - collection->vadj->page_size);
793 diff = new_value - old_value;
794 if (diff)
796 if (box)
798 remove_lasso_box(collection);
799 collection->drag_box_y[0] -= diff;
801 gtk_adjustment_set_value(collection->vadj, new_value);
802 if (box)
803 add_lasso_box(collection);
807 return TRUE;
810 /* 'from' and 'to' are pixel positions. 'step' is the size of each item.
811 * Returns the index of the first item covered, and the number of items.
813 static void get_range(int from, int to, int step, gint *pos, gint *len)
815 int margin = MIN(step / 4, 40);
817 if (from > to)
819 int tmp = to;
820 to = from;
821 from = tmp;
824 from = (from + margin) / step; /* First item */
825 to = (to + step - margin) / step; /* Last item (inclusive) */
827 *pos = MAX(from, 0);
828 *len = to - *pos;
831 /* Fills in the area with a rectangle corresponding to the current
832 * size of the lasso box (units of items, not pixels).
834 * The box will only span valid columns, but the total number
835 * of items is not taken into account (rows or cols).
837 static void find_lasso_area(Collection *collection, GdkRectangle *area)
839 int cols = collection->columns;
840 int dx = collection->drag_box_x[0] - collection->drag_box_x[1];
841 int dy = collection->drag_box_y[0] - collection->drag_box_y[1];
843 if (ABS(dx) < 8 && ABS(dy) < 8)
845 /* Didn't move far enough - ignore */
846 area->x = area->y = 0;
847 area->width = 0;
848 area->height = 0;
849 return;
852 get_range(collection->drag_box_x[0], collection->drag_box_x[1],
853 collection->item_width, &area->x, &area->width);
855 if (area->x >= cols)
856 area->width = 0;
857 else if (area->x + area->width > cols)
858 area->width = cols - area->x;
860 get_range(collection->drag_box_y[0], collection->drag_box_y[1],
861 collection->item_height, &area->y, &area->height);
864 static void collection_process_area(Collection *collection,
865 GdkRectangle *area,
866 GdkFunction fn,
867 guint32 time)
869 int x, y;
870 int rows = collection_get_rows(collection);
871 int cols = collection->columns;
872 guint32 stacked_time;
873 int item;
874 gboolean changed = FALSE;
875 guint old_selected;
877 g_return_if_fail(fn == GDK_SET || fn == GDK_INVERT);
879 old_selected = collection->number_selected;
881 stacked_time = current_event_time;
882 current_event_time = time;
884 collection->block_selection_changed++;
886 for (y = area->y; y < area->y + area->height && y < rows; y++)
887 for (x = area->x; x < area->x + area->width && x < cols; x++)
889 item = collection_rowcol_to_item(collection, y, x);
890 if (item < collection->number_of_items) {
891 if (fn == GDK_INVERT)
892 collection_item_set_selected(
893 collection, item,
894 !collection-> items[item].selected,
895 FALSE);
896 else
897 collection_item_set_selected(
898 collection, item, TRUE, FALSE);
900 changed = TRUE;
904 if (collection->number_selected && !old_selected)
905 g_signal_emit(collection,
906 collection_signals[GAIN_SELECTION], 0,
907 current_event_time);
908 else if (!collection->number_selected && old_selected)
909 g_signal_emit(collection,
910 collection_signals[LOSE_SELECTION], 0,
911 current_event_time);
913 collection_unblock_selection_changed(collection,
914 current_event_time, changed);
915 current_event_time = stacked_time;
918 static gint collection_motion_notify(GtkWidget *widget,
919 GdkEventMotion *event)
921 Collection *collection;
922 gint x, y;
924 g_return_val_if_fail(widget != NULL, FALSE);
925 g_return_val_if_fail(IS_COLLECTION(widget), FALSE);
926 g_return_val_if_fail(event != NULL, FALSE);
928 collection = COLLECTION(widget);
930 if (!collection->lasso_box)
931 return FALSE;
933 if (event->window != widget->window)
934 gdk_window_get_pointer(widget->window, &x, &y, NULL);
935 else
937 x = event->x;
938 y = event->y;
941 remove_lasso_box(collection);
942 collection->drag_box_x[1] = x;
943 collection->drag_box_y[1] = y;
944 add_lasso_box(collection);
945 return TRUE;
948 static void add_lasso_box(Collection *collection)
950 g_return_if_fail(collection != NULL);
951 g_return_if_fail(IS_COLLECTION(collection));
952 g_return_if_fail(collection->lasso_box == FALSE);
954 collection->lasso_box = TRUE;
955 draw_lasso_box(collection);
958 static void draw_lasso_box(Collection *collection)
960 GtkWidget *widget;
961 int x, y, width, height;
963 widget = GTK_WIDGET(collection);
965 x = MIN(collection->drag_box_x[0], collection->drag_box_x[1]);
966 y = MIN(collection->drag_box_y[0], collection->drag_box_y[1]);
967 width = abs(collection->drag_box_x[1] - collection->drag_box_x[0]);
968 height = abs(collection->drag_box_y[1] - collection->drag_box_y[0]);
970 /* XXX: A redraw bug sometimes leaves a one-pixel dot on the screen.
971 * As a quick hack, don't draw boxes that small for now...
973 if (width || height)
974 gdk_draw_rectangle(widget->window, collection->xor_gc, FALSE,
975 x, y, width, height);
978 static void abort_lasso(Collection *collection)
980 if (collection->lasso_box)
981 remove_lasso_box(collection);
984 static void remove_lasso_box(Collection *collection)
986 g_return_if_fail(collection != NULL);
987 g_return_if_fail(IS_COLLECTION(collection));
988 g_return_if_fail(collection->lasso_box == TRUE);
990 draw_lasso_box(collection);
992 collection->lasso_box = FALSE;
994 return;
997 /* Make sure that 'item' is fully visible (vertically), scrolling if not. */
998 static void scroll_to_show(Collection *collection, int item)
1000 int first, last, row, col;
1002 g_return_if_fail(collection != NULL);
1003 g_return_if_fail(IS_COLLECTION(collection));
1005 collection_item_to_rowcol(collection, item, &row, &col);
1006 get_visible_limits(collection, &first, &last);
1008 if (row <= first)
1010 gtk_adjustment_set_value(collection->vadj,
1011 row * collection->item_height);
1013 else if (row >= last)
1015 GtkWidget *widget = (GtkWidget *) collection;
1016 gint height;
1018 if (GTK_WIDGET_REALIZED(widget))
1020 height = collection->vadj->page_size;
1021 gtk_adjustment_set_value(collection->vadj,
1022 (row + 1) * collection->item_height - height);
1027 /* Return the first and last rows which are [partly] visible. Does not
1028 * ensure that the rows actually exist (contain items).
1030 static void get_visible_limits(Collection *collection, int *first, int *last)
1032 GtkWidget *widget = (GtkWidget *) collection;
1033 gint scroll = 0, height;
1035 g_return_if_fail(collection != NULL);
1036 g_return_if_fail(IS_COLLECTION(collection));
1037 g_return_if_fail(first != NULL && last != NULL);
1039 if (!GTK_WIDGET_REALIZED(widget))
1041 *first = 0;
1042 *last = 0;
1044 else
1046 scroll = collection->vadj->value;
1047 height = collection->vadj->page_size;
1049 *first = MAX(scroll / collection->item_height, 0);
1050 *last = (scroll + height - 1) /collection->item_height;
1052 if (*last < *first)
1053 *last = *first;
1057 /* Cancel the current wink effect. */
1058 static void cancel_wink(Collection *collection)
1060 gint item;
1062 g_return_if_fail(collection != NULL);
1063 g_return_if_fail(IS_COLLECTION(collection));
1064 g_return_if_fail(collection->wink_item != -1);
1066 item = collection->wink_item;
1068 collection->wink_item = -1;
1069 g_source_remove(collection->wink_timeout);
1071 collection_draw_item(collection, item, TRUE);
1074 /* Draw/undraw a box around collection->wink_item */
1075 static void invert_wink(Collection *collection)
1077 GdkRectangle area;
1078 gint row, col;
1080 g_return_if_fail(collection->wink_item >= 0);
1082 if (!GTK_WIDGET_REALIZED(GTK_WIDGET(collection)))
1083 return;
1085 collection_item_to_rowcol(collection, collection->wink_item,
1086 &row, &col);
1087 collection_get_item_area(collection, row, col, &area);
1089 gdk_draw_rectangle(((GtkWidget *) collection)->window,
1090 collection->xor_gc, FALSE,
1091 area.x, area.y,
1092 collection->item_width - 1,
1093 area.height - 1);
1096 static gboolean wink_timeout(Collection *collection)
1098 gint item;
1100 g_return_val_if_fail(collection != NULL, FALSE);
1101 g_return_val_if_fail(IS_COLLECTION(collection), FALSE);
1102 g_return_val_if_fail(collection->wink_item != -1, FALSE);
1104 item = collection->wink_item;
1106 if (collection->winks_left-- > 0)
1108 invert_wink(collection);
1109 return TRUE;
1112 collection->wink_item = -1;
1114 collection_draw_item(collection, item, TRUE);
1116 return FALSE;
1119 /* Change the selected state of an item.
1120 * Send GAIN/LOSE signals if 'signal' is TRUE.
1121 * Send SELECTION_CHANGED unless blocked.
1122 * Updates number_selected and redraws the item.
1124 static void collection_item_set_selected(Collection *collection,
1125 gint item,
1126 gboolean selected,
1127 gboolean signal)
1129 g_return_if_fail(collection != NULL);
1130 g_return_if_fail(IS_COLLECTION(collection));
1131 g_return_if_fail(item >= 0 && item < collection->number_of_items);
1133 if (collection->items[item].selected == selected)
1134 return;
1136 collection->items[item].selected = selected;
1137 collection_draw_item(collection, item, TRUE);
1139 if (selected)
1141 collection->number_selected++;
1142 if (signal && collection->number_selected == 1)
1143 g_signal_emit(collection,
1144 collection_signals[GAIN_SELECTION], 0,
1145 current_event_time);
1147 else
1149 collection->number_selected--;
1150 if (signal && collection->number_selected == 0)
1151 g_signal_emit(collection,
1152 collection_signals[LOSE_SELECTION], 0,
1153 current_event_time);
1156 EMIT_SELECTION_CHANGED(collection, current_event_time);
1159 /* Functions for managing collections */
1161 /* Remove all objects from the collection */
1162 void collection_clear(Collection *collection)
1164 collection_delete_if(collection, NULL, NULL);
1167 /* Inserts a new item at the end. The new item is unselected, and its
1168 * number is returned.
1170 gint collection_insert(Collection *collection, gpointer data, gpointer view)
1172 int item;
1174 /* g_return_val_if_fail(IS_COLLECTION(collection), -1); (slow) */
1176 item = collection->number_of_items;
1178 if (item >= collection->array_size)
1179 resize_arrays(collection, item + (item >> 1));
1181 collection->items[item].data = data;
1182 collection->items[item].view_data = view;
1183 collection->items[item].selected = FALSE;
1185 collection->number_of_items++;
1187 gtk_widget_queue_resize(GTK_WIDGET(collection));
1189 collection_draw_item(collection, item, FALSE);
1191 return item;
1194 void collection_unselect_item(Collection *collection, gint item)
1196 collection_item_set_selected(collection, item, FALSE, TRUE);
1199 void collection_select_item(Collection *collection, gint item)
1201 collection_item_set_selected(collection, item, TRUE, TRUE);
1204 void collection_toggle_item(Collection *collection, gint item)
1206 collection_item_set_selected(collection, item,
1207 !collection->items[item].selected, TRUE);
1210 /* Select all items in the collection */
1211 void collection_select_all(Collection *collection)
1213 GtkWidget *widget;
1214 int item = 0;
1216 g_return_if_fail(collection != NULL);
1217 g_return_if_fail(IS_COLLECTION(collection));
1219 widget = GTK_WIDGET(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 GtkWidget *widget;
1279 int i = 0;
1280 int end; /* Selected items to end up with */
1282 g_return_if_fail(collection != NULL);
1283 g_return_if_fail(IS_COLLECTION(collection));
1284 g_return_if_fail(item >= -1 && item < collection->number_of_items);
1286 widget = GTK_WIDGET(collection);
1288 if (item == -1)
1289 end = 0;
1290 else
1292 collection_select_item(collection, item);
1293 end = 1;
1296 if (collection->number_selected == 0)
1297 return;
1299 while (collection->number_selected > end)
1301 while (i == item || !collection->items[i].selected)
1302 i++;
1304 collection->items[i].selected = FALSE;
1305 collection_draw_item(collection, i, TRUE);
1306 i++;
1308 collection->number_selected--;
1311 if (end == 0)
1312 g_signal_emit(collection, collection_signals[LOSE_SELECTION], 0,
1313 current_event_time);
1314 EMIT_SELECTION_CHANGED(collection, current_event_time);
1317 /* Unselect all items in the collection */
1318 void collection_clear_selection(Collection *collection)
1320 g_return_if_fail(collection != NULL);
1321 g_return_if_fail(IS_COLLECTION(collection));
1323 collection_clear_except(collection, -1);
1326 /* Force a redraw of the specified item, if it is visible */
1327 void collection_draw_item(Collection *collection, gint item, gboolean blank)
1329 GdkRectangle area;
1330 GtkWidget *widget;
1331 int row, col;
1333 g_return_if_fail(collection != NULL);
1334 /* g_return_if_fail(IS_COLLECTION(collection)); (slow) */
1335 g_return_if_fail(item >= 0 &&
1336 (item == 0 || item < collection->number_of_items));
1338 widget = GTK_WIDGET(collection);
1339 if (!GTK_WIDGET_REALIZED(widget))
1340 return;
1342 collection_item_to_rowcol(collection, item, &row, &col);
1344 collection_get_item_area(collection, row, col, &area);
1346 gdk_window_invalidate_rect(widget->window, &area, FALSE);
1349 void collection_set_item_size(Collection *collection, int width, int height)
1351 GtkWidget *widget;
1353 g_return_if_fail(collection != NULL);
1354 g_return_if_fail(IS_COLLECTION(collection));
1355 g_return_if_fail(width > 4 && height > 4);
1357 if (collection->item_width == width &&
1358 collection->item_height == height)
1359 return;
1361 widget = GTK_WIDGET(collection);
1363 collection->item_width = width;
1364 collection->item_height = height;
1366 if (GTK_WIDGET_REALIZED(widget))
1368 gint window_width;
1370 gdk_drawable_get_size(widget->window, &window_width, NULL);
1371 collection->columns = MAX(window_width / collection->item_width,
1373 if (collection->cursor_item != -1)
1374 scroll_to_show(collection, collection->cursor_item);
1375 gtk_widget_queue_draw(widget);
1378 gtk_widget_queue_resize(GTK_WIDGET(collection));
1381 static int (*cmp_callback)(const void *a, const void *b) = NULL;
1382 static int collection_cmp(const void *a, const void *b)
1384 return cmp_callback(((CollectionItem *) a)->data,
1385 ((CollectionItem *) b)->data);
1387 static int collection_rcmp(const void *a, const void *b)
1389 return -cmp_callback(((CollectionItem *) a)->data,
1390 ((CollectionItem *) b)->data);
1393 /* Cursor is positioned on item with the same data as before the sort.
1394 * Same for the wink item.
1396 void collection_qsort(Collection *collection,
1397 int (*compar)(const void *, const void *),
1398 GtkSortType order)
1400 int cursor, wink, items, wink_on_map;
1401 gpointer cursor_data = NULL;
1402 gpointer wink_data = NULL;
1403 gpointer wink_on_map_data = NULL;
1404 CollectionItem *array;
1405 int i;
1406 int mul = order == GTK_SORT_ASCENDING ? 1 : -1;
1408 g_return_if_fail(collection != NULL);
1409 g_return_if_fail(IS_COLLECTION(collection));
1410 g_return_if_fail(compar != NULL);
1411 g_return_if_fail(cmp_callback == NULL);
1413 /* Check to see if it needs sorting (saves redrawing) */
1414 if (collection->number_of_items < 2)
1415 return;
1417 array = collection->items;
1418 for (i = 1; i < collection->number_of_items; i++)
1420 if (mul * compar(array[i - 1].data, array[i].data) > 0)
1421 break;
1423 if (i == collection->number_of_items)
1424 return; /* Already sorted */
1426 items = collection->number_of_items;
1428 wink_on_map = collection->wink_on_map;
1429 if (wink_on_map >= 0 && wink_on_map < items)
1431 wink_on_map_data = collection->items[wink_on_map].data;
1432 collection->wink_on_map = -1;
1434 else
1435 wink = -1;
1437 wink = collection->wink_item;
1438 if (wink >= 0 && wink < items)
1440 wink_data = collection->items[wink].data;
1441 collection->wink_item = -1;
1443 else
1444 wink = -1;
1446 cursor = collection->cursor_item;
1447 if (cursor >= 0 && cursor < items)
1448 cursor_data = collection->items[cursor].data;
1449 else
1450 cursor = -1;
1452 cmp_callback = compar;
1453 qsort(collection->items, items, sizeof(collection->items[0]),
1454 order == GTK_SORT_ASCENDING ? collection_cmp
1455 : collection_rcmp);
1456 cmp_callback = NULL;
1458 if (cursor > -1 || wink > -1 || wink_on_map > -1)
1460 int item;
1462 for (item = 0; item < items; item++)
1464 if (collection->items[item].data == cursor_data)
1465 collection_set_cursor_item(collection, item,
1466 TRUE);
1467 if (collection->items[item].data == wink_on_map_data)
1468 collection->wink_on_map = item;
1469 if (collection->items[item].data == wink_data)
1471 collection->cursor_item_old = item;
1472 collection->wink_item = item;
1473 scroll_to_show(collection, item);
1478 gtk_widget_queue_draw(GTK_WIDGET(collection));
1481 /* Find an item in a sorted collection.
1482 * Returns the item number, or -1 if not found.
1484 int collection_find_item(Collection *collection, gpointer data,
1485 int (*compar)(const void *, const void *),
1486 GtkSortType order)
1488 int lower, upper;
1489 int mul = order == GTK_SORT_ASCENDING ? 1 : -1;
1491 g_return_val_if_fail(collection != NULL, -1);
1492 g_return_val_if_fail(IS_COLLECTION(collection), -1);
1493 g_return_val_if_fail(compar != NULL, -1);
1495 /* If item is here, then: lower <= i < upper */
1496 lower = 0;
1497 upper = collection->number_of_items;
1499 while (lower < upper)
1501 int i, cmp;
1503 i = (lower + upper) >> 1;
1505 cmp = mul * compar(collection->items[i].data, data);
1506 if (cmp == 0)
1507 return i;
1509 if (cmp > 0)
1510 upper = i;
1511 else
1512 lower = i + 1;
1515 return -1;
1518 /* Return the number of the item under the point (x,y), or -1 for none.
1519 * This may call your test_point callback. The point is relative to the
1520 * collection's origin.
1522 int collection_get_item(Collection *collection, int x, int y)
1524 int row, col;
1525 int width;
1526 int item;
1528 g_return_val_if_fail(collection != NULL, -1);
1530 col = x / collection->item_width;
1531 row = y / collection->item_height;
1533 if (col >= collection->columns)
1534 col = collection->columns - 1;
1536 if (col < 0 || row < 0)
1537 return -1;
1539 if (col == collection->columns - 1)
1540 width = collection->item_width << 1;
1541 else
1542 width = collection->item_width;
1544 item = collection_rowcol_to_item(collection, row, col);
1545 if (item >= collection->number_of_items)
1546 return -1;
1548 x -= col * collection->item_width;
1549 y -= row * collection->item_height;
1551 if (collection->test_point(collection, x, y,
1552 &collection->items[item], width, collection->item_height,
1553 collection->cb_user_data))
1554 return item;
1556 return -1;
1559 /* Set the cursor/highlight over the given item. Passing -1
1560 * hides the cursor. As a special case, you may set the cursor item
1561 * to zero when there are no items.
1563 void collection_set_cursor_item(Collection *collection, gint item,
1564 gboolean may_scroll)
1566 int old_item;
1568 g_return_if_fail(collection != NULL);
1569 g_return_if_fail(IS_COLLECTION(collection));
1570 g_return_if_fail(item >= -1 &&
1571 (item < collection->number_of_items || item == 0));
1573 old_item = collection->cursor_item;
1575 if (old_item == item)
1576 return;
1578 collection->cursor_item = item;
1580 if (old_item != -1)
1581 collection_draw_item(collection, old_item, TRUE);
1583 if (item != -1)
1585 collection_draw_item(collection, item, TRUE);
1586 if (may_scroll)
1587 scroll_to_show(collection, item);
1589 else if (old_item != -1)
1590 collection->cursor_item_old = old_item;
1593 /* Briefly highlight an item to draw the user's attention to it.
1594 * -1 cancels the effect, as does deleting items, sorting the collection
1595 * or starting a new wink effect.
1596 * Otherwise, the effect will cancel itself after a short pause.
1597 * */
1598 void collection_wink_item(Collection *collection, gint item)
1600 g_return_if_fail(collection != NULL);
1601 g_return_if_fail(IS_COLLECTION(collection));
1602 g_return_if_fail(item >= -1 && item < collection->number_of_items);
1604 if (collection->wink_item != -1)
1605 cancel_wink(collection);
1606 if (item == -1)
1607 return;
1609 if (!GTK_WIDGET_MAPPED(GTK_WIDGET(collection)))
1611 collection->wink_on_map = item;
1612 return;
1615 collection->cursor_item_old = collection->wink_item = item;
1616 collection->winks_left = MAX_WINKS;
1618 collection->wink_timeout = g_timeout_add(70,
1619 (GSourceFunc) wink_timeout,
1620 collection);
1621 scroll_to_show(collection, item);
1622 invert_wink(collection);
1624 gdk_flush();
1627 /* Call test(item, data) on each item in the collection.
1628 * Remove all items for which it returns TRUE. test() should
1629 * free the data before returning TRUE. The collection is in an
1630 * inconsistant state during this call (ie, when test() is called).
1632 * If test is NULL, remove all items.
1634 void collection_delete_if(Collection *collection,
1635 gboolean (*test)(gpointer item, gpointer data),
1636 gpointer data)
1638 int in, out = 0;
1639 int selected = 0;
1640 int cursor;
1642 g_return_if_fail(collection != NULL);
1643 g_return_if_fail(IS_COLLECTION(collection));
1645 cursor = collection->cursor_item;
1647 for (in = 0; in < collection->number_of_items; in++)
1649 if (test && !test(collection->items[in].data, data))
1651 /* Keep item */
1652 if (collection->items[in].selected)
1654 collection->items[out].selected = TRUE;
1655 selected++;
1657 else
1658 collection->items[out].selected = FALSE;
1660 collection->items[out].data =
1661 collection->items[in].data;
1662 collection->items[out].view_data =
1663 collection->items[in].view_data;
1664 out++;
1666 else
1668 /* Remove item */
1669 if (collection->free_item)
1670 collection->free_item(collection,
1671 &collection->items[in]);
1673 if (collection->cursor_item >= in)
1674 cursor--;
1678 if (in != out)
1680 collection->cursor_item = cursor;
1682 if (collection->wink_item != -1)
1684 collection->wink_item = -1;
1685 g_source_remove(collection->wink_timeout);
1688 collection->number_of_items = out;
1689 if (collection->number_selected && !selected)
1691 /* We've lost all the selected items */
1692 g_signal_emit(collection,
1693 collection_signals[LOSE_SELECTION], 0,
1694 current_event_time);
1697 collection->number_selected = selected;
1698 resize_arrays(collection,
1699 MAX(collection->number_of_items, MINIMUM_ITEMS));
1701 if (GTK_WIDGET_REALIZED(GTK_WIDGET(collection)))
1702 gtk_widget_queue_draw(GTK_WIDGET(collection));
1704 gtk_widget_queue_resize(GTK_WIDGET(collection));
1708 /* Move the cursor by the given row and column offsets.
1709 * Moving by (0,0) can be used to simply make the cursor appear.
1711 void collection_move_cursor(Collection *collection, int drow, int dcol)
1713 int row, col, item;
1714 int first, last, total_rows, total_cols;
1716 g_return_if_fail(collection != NULL);
1717 g_return_if_fail(IS_COLLECTION(collection));
1719 if (!collection->number_of_items)
1721 /* Show the cursor, even though there are no items */
1722 collection_set_cursor_item(collection, 0, TRUE);
1723 return;
1726 get_visible_limits(collection, &first, &last);
1727 total_rows = collection_get_rows(collection);
1728 total_cols = collection_get_cols(collection);
1730 item = collection->cursor_item;
1731 if (item == -1)
1733 item = MIN(collection->cursor_item_old,
1734 collection->number_of_items - 1);
1737 if (item == -1)
1739 col = 0;
1740 row = first;
1742 else
1744 collection_item_to_rowcol(collection, item, &row, &col);
1746 col += dcol;
1747 if (collection->vertical_order)
1749 col = MAX(0,col);
1750 col = MIN(col, total_cols - 1);
1753 if (row < first)
1754 row = first;
1755 else if (row > last)
1756 row = last;
1757 else {
1758 row += drow;
1759 if (!collection->vertical_order)
1761 row = MAX(row, 0);
1762 row = MIN(row, total_rows - 1);
1767 item = collection_rowcol_to_item(collection, row, col);
1769 item = MAX(item, 0);
1770 item = MIN(item, collection->number_of_items-1);
1772 collection_set_cursor_item(collection, item, TRUE);
1775 /* Start a lasso box drag */
1776 void collection_lasso_box(Collection *collection, int x, int y)
1778 collection->drag_box_x[0] = x;
1779 collection->drag_box_y[0] = y;
1780 collection->drag_box_x[1] = x;
1781 collection->drag_box_y[1] = y;
1783 add_lasso_box(collection);
1786 /* Remove the lasso box. Applies fn to each item inside the box.
1787 * fn may be GDK_INVERT, GDK_SET, GDK_NOOP or GDK_CLEAR.
1789 void collection_end_lasso(Collection *collection, GdkFunction fn)
1791 if (fn != GDK_CLEAR)
1793 GdkRectangle region;
1795 find_lasso_area(collection, &region);
1797 collection_process_area(collection, &region, fn,
1798 GDK_CURRENT_TIME);
1801 abort_lasso(collection);
1804 /* Unblock the selection_changed signal, emitting the signal if the
1805 * block counter reaches zero and emit is TRUE.
1807 void collection_unblock_selection_changed(Collection *collection,
1808 guint time,
1809 gboolean emit)
1811 g_return_if_fail(collection != NULL);
1812 g_return_if_fail(IS_COLLECTION(collection));
1813 g_return_if_fail(collection->block_selection_changed > 0);
1815 collection->block_selection_changed--;
1817 if (emit && !collection->block_selection_changed)
1818 g_signal_emit(collection,
1819 collection_signals[SELECTION_CHANGED], 0, time);
1822 /* Translate the item number to the (row, column) form */
1823 void collection_item_to_rowcol (const Collection *collection,
1824 int item, int *row, int *col)
1826 if (!collection->vertical_order)
1828 *row = item / collection->columns;
1829 *col = item % collection->columns;
1831 else
1833 int rows = collection_get_rows(collection);
1834 *row = item % rows;
1835 *col = item / rows;
1841 /* Translate the (row, column) form to the item number.
1842 * May return a number >= collection->number_of_items.
1844 int collection_rowcol_to_item(const Collection *collection, int row, int col)
1846 if (!collection->vertical_order)
1847 return row * collection->columns + col;
1848 else
1850 int rows = collection_get_rows(collection);
1851 if (row >= rows)
1852 return collection->number_of_items;
1853 return row + col * rows;