2 * Copyright (C) 2002, 2004 Anders Carlsson <andersca@gnu.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 #include "gtkiconview.h"
23 #include "gtkiconviewprivate.h"
25 #include "gtkadjustment.h"
26 #include "gtkcelllayout.h"
27 #include "gtkcellrenderer.h"
28 #include "gtkcellareabox.h"
29 #include "gtkcellareacontext.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcellrendererpixbuf.h"
32 #include "gtkorientable.h"
33 #include "gtkmarshalers.h"
34 #include "gtkbindings.h"
38 #include "gtkaccessible.h"
39 #include "gtkwindow.h"
41 #include "gtkcombobox.h"
42 #include "gtkscrollable.h"
43 #include "gtksizerequest.h"
44 #include "gtktreednd.h"
45 #include "gtktypebuiltins.h"
46 #include "gtkprivate.h"
47 #include "a11y/gtkiconviewaccessible.h"
52 * @short_description: A widget which displays a list of icons in a grid
54 * #GtkIconView provides an alternative view on a #GtkTreeModel.
55 * It displays the model as a grid of icons with labels. Like
56 * #GtkTreeView, it allows to select one or multiple items
57 * (depending on the selection mode, see gtk_icon_view_set_selection_mode()).
58 * In addition to selection with the arrow keys, #GtkIconView supports
59 * rubberband selection, which is controlled by dragging the pointer.
61 * Note that if the tree model is backed by an actual tree store (as
62 * opposed to a flat list where the mapping to icons is obvious),
63 * #GtkIconView will only display the first level of the tree and
64 * ignore the tree's branches.
67 #define SCROLL_EDGE_SIZE 15
69 typedef struct _GtkIconViewChild GtkIconViewChild;
70 struct _GtkIconViewChild
98 PROP_ITEM_ORIENTATION,
111 /* For scrollable interface */
119 static void gtk_icon_view_cell_layout_init (GtkCellLayoutIface *iface);
120 static void gtk_icon_view_dispose (GObject *object);
121 static GObject *gtk_icon_view_constructor (GType type,
122 guint n_construct_properties,
123 GObjectConstructParam *construct_properties);
124 static void gtk_icon_view_set_property (GObject *object,
128 static void gtk_icon_view_get_property (GObject *object,
132 /* GtkWidget vfuncs */
133 static void gtk_icon_view_destroy (GtkWidget *widget);
134 static void gtk_icon_view_realize (GtkWidget *widget);
135 static void gtk_icon_view_unrealize (GtkWidget *widget);
136 static void gtk_icon_view_style_updated (GtkWidget *widget);
137 static void gtk_icon_view_state_flags_changed (GtkWidget *widget,
138 GtkStateFlags previous_state);
139 static GtkSizeRequestMode gtk_icon_view_get_request_mode (GtkWidget *widget);
140 static void gtk_icon_view_get_preferred_width (GtkWidget *widget,
143 static void gtk_icon_view_get_preferred_width_for_height
148 static void gtk_icon_view_get_preferred_height (GtkWidget *widget,
151 static void gtk_icon_view_get_preferred_height_for_width
156 static void gtk_icon_view_size_allocate (GtkWidget *widget,
157 GtkAllocation *allocation);
158 static gboolean gtk_icon_view_draw (GtkWidget *widget,
160 static gboolean gtk_icon_view_motion (GtkWidget *widget,
161 GdkEventMotion *event);
162 static gboolean gtk_icon_view_button_press (GtkWidget *widget,
163 GdkEventButton *event);
164 static gboolean gtk_icon_view_button_release (GtkWidget *widget,
165 GdkEventButton *event);
166 static gboolean gtk_icon_view_key_press (GtkWidget *widget,
168 static gboolean gtk_icon_view_key_release (GtkWidget *widget,
172 /* GtkContainer vfuncs */
173 static void gtk_icon_view_remove (GtkContainer *container,
175 static void gtk_icon_view_forall (GtkContainer *container,
176 gboolean include_internals,
177 GtkCallback callback,
178 gpointer callback_data);
180 /* GtkIconView vfuncs */
181 static void gtk_icon_view_real_select_all (GtkIconView *icon_view);
182 static void gtk_icon_view_real_unselect_all (GtkIconView *icon_view);
183 static void gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view);
184 static void gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view);
185 static gboolean gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view);
187 /* Internal functions */
188 static void gtk_icon_view_set_hadjustment_values (GtkIconView *icon_view);
189 static void gtk_icon_view_set_vadjustment_values (GtkIconView *icon_view);
190 static void gtk_icon_view_set_hadjustment (GtkIconView *icon_view,
191 GtkAdjustment *adjustment);
192 static void gtk_icon_view_set_vadjustment (GtkIconView *icon_view,
193 GtkAdjustment *adjustment);
194 static void gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
195 GtkIconView *icon_view);
196 static void gtk_icon_view_layout (GtkIconView *icon_view);
197 static void gtk_icon_view_paint_item (GtkIconView *icon_view,
199 GtkIconViewItem *item,
202 gboolean draw_focus);
203 static void gtk_icon_view_paint_rubberband (GtkIconView *icon_view,
205 static void gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
207 static void gtk_icon_view_queue_draw_item (GtkIconView *icon_view,
208 GtkIconViewItem *item);
209 static void gtk_icon_view_start_rubberbanding (GtkIconView *icon_view,
213 static void gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view);
214 static void gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view);
215 static gboolean gtk_icon_view_item_hit_test (GtkIconView *icon_view,
216 GtkIconViewItem *item,
221 static gboolean gtk_icon_view_unselect_all_internal (GtkIconView *icon_view);
222 static void gtk_icon_view_update_rubberband (gpointer data);
223 static void gtk_icon_view_item_invalidate_size (GtkIconViewItem *item);
224 static void gtk_icon_view_invalidate_sizes (GtkIconView *icon_view);
225 static void gtk_icon_view_add_move_binding (GtkBindingSet *binding_set,
228 GtkMovementStep step,
230 static gboolean gtk_icon_view_real_move_cursor (GtkIconView *icon_view,
231 GtkMovementStep step,
233 static void gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
235 static void gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
237 static void gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
239 static void gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
241 static void gtk_icon_view_scroll_to_item (GtkIconView *icon_view,
242 GtkIconViewItem *item);
243 static gboolean gtk_icon_view_select_all_between (GtkIconView *icon_view,
244 GtkIconViewItem *anchor,
245 GtkIconViewItem *cursor);
247 static void gtk_icon_view_ensure_cell_area (GtkIconView *icon_view,
248 GtkCellArea *cell_area);
250 static GtkCellArea *gtk_icon_view_cell_layout_get_area (GtkCellLayout *layout);
252 static void gtk_icon_view_item_selected_changed (GtkIconView *icon_view,
253 GtkIconViewItem *item);
255 static void gtk_icon_view_add_editable (GtkCellArea *area,
256 GtkCellRenderer *renderer,
257 GtkCellEditable *editable,
258 GdkRectangle *cell_area,
260 GtkIconView *icon_view);
261 static void gtk_icon_view_remove_editable (GtkCellArea *area,
262 GtkCellRenderer *renderer,
263 GtkCellEditable *editable,
264 GtkIconView *icon_view);
265 static void update_text_cell (GtkIconView *icon_view);
266 static void update_pixbuf_cell (GtkIconView *icon_view);
268 /* Source side drag signals */
269 static void gtk_icon_view_drag_begin (GtkWidget *widget,
270 GdkDragContext *context);
271 static void gtk_icon_view_drag_end (GtkWidget *widget,
272 GdkDragContext *context);
273 static void gtk_icon_view_drag_data_get (GtkWidget *widget,
274 GdkDragContext *context,
275 GtkSelectionData *selection_data,
278 static void gtk_icon_view_drag_data_delete (GtkWidget *widget,
279 GdkDragContext *context);
281 /* Target side drag signals */
282 static void gtk_icon_view_drag_leave (GtkWidget *widget,
283 GdkDragContext *context,
285 static gboolean gtk_icon_view_drag_motion (GtkWidget *widget,
286 GdkDragContext *context,
290 static gboolean gtk_icon_view_drag_drop (GtkWidget *widget,
291 GdkDragContext *context,
295 static void gtk_icon_view_drag_data_received (GtkWidget *widget,
296 GdkDragContext *context,
299 GtkSelectionData *selection_data,
302 static gboolean gtk_icon_view_maybe_begin_drag (GtkIconView *icon_view,
303 GdkEventMotion *event);
305 static void remove_scroll_timeout (GtkIconView *icon_view);
308 static GtkBuildableIface *parent_buildable_iface;
309 static void gtk_icon_view_buildable_init (GtkBuildableIface *iface);
310 static gboolean gtk_icon_view_buildable_custom_tag_start (GtkBuildable *buildable,
313 const gchar *tagname,
314 GMarkupParser *parser,
316 static void gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
319 const gchar *tagname,
322 static guint icon_view_signals[LAST_SIGNAL] = { 0 };
324 G_DEFINE_TYPE_WITH_CODE (GtkIconView, gtk_icon_view, GTK_TYPE_CONTAINER,
325 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
326 gtk_icon_view_cell_layout_init)
327 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
328 gtk_icon_view_buildable_init)
329 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
332 gtk_icon_view_class_init (GtkIconViewClass *klass)
334 GObjectClass *gobject_class;
335 GtkWidgetClass *widget_class;
336 GtkContainerClass *container_class;
337 GtkBindingSet *binding_set;
339 binding_set = gtk_binding_set_by_class (klass);
341 g_type_class_add_private (klass, sizeof (GtkIconViewPrivate));
343 gobject_class = (GObjectClass *) klass;
344 widget_class = (GtkWidgetClass *) klass;
345 container_class = (GtkContainerClass *) klass;
347 gobject_class->constructor = gtk_icon_view_constructor;
348 gobject_class->dispose = gtk_icon_view_dispose;
349 gobject_class->set_property = gtk_icon_view_set_property;
350 gobject_class->get_property = gtk_icon_view_get_property;
352 widget_class->destroy = gtk_icon_view_destroy;
353 widget_class->realize = gtk_icon_view_realize;
354 widget_class->unrealize = gtk_icon_view_unrealize;
355 widget_class->style_updated = gtk_icon_view_style_updated;
356 widget_class->get_request_mode = gtk_icon_view_get_request_mode;
357 widget_class->get_preferred_width = gtk_icon_view_get_preferred_width;
358 widget_class->get_preferred_height = gtk_icon_view_get_preferred_height;
359 widget_class->get_preferred_width_for_height = gtk_icon_view_get_preferred_width_for_height;
360 widget_class->get_preferred_height_for_width = gtk_icon_view_get_preferred_height_for_width;
361 widget_class->size_allocate = gtk_icon_view_size_allocate;
362 widget_class->draw = gtk_icon_view_draw;
363 widget_class->motion_notify_event = gtk_icon_view_motion;
364 widget_class->button_press_event = gtk_icon_view_button_press;
365 widget_class->button_release_event = gtk_icon_view_button_release;
366 widget_class->key_press_event = gtk_icon_view_key_press;
367 widget_class->key_release_event = gtk_icon_view_key_release;
368 widget_class->drag_begin = gtk_icon_view_drag_begin;
369 widget_class->drag_end = gtk_icon_view_drag_end;
370 widget_class->drag_data_get = gtk_icon_view_drag_data_get;
371 widget_class->drag_data_delete = gtk_icon_view_drag_data_delete;
372 widget_class->drag_leave = gtk_icon_view_drag_leave;
373 widget_class->drag_motion = gtk_icon_view_drag_motion;
374 widget_class->drag_drop = gtk_icon_view_drag_drop;
375 widget_class->drag_data_received = gtk_icon_view_drag_data_received;
376 widget_class->state_flags_changed = gtk_icon_view_state_flags_changed;
378 container_class->remove = gtk_icon_view_remove;
379 container_class->forall = gtk_icon_view_forall;
381 klass->select_all = gtk_icon_view_real_select_all;
382 klass->unselect_all = gtk_icon_view_real_unselect_all;
383 klass->select_cursor_item = gtk_icon_view_real_select_cursor_item;
384 klass->toggle_cursor_item = gtk_icon_view_real_toggle_cursor_item;
385 klass->activate_cursor_item = gtk_icon_view_real_activate_cursor_item;
386 klass->move_cursor = gtk_icon_view_real_move_cursor;
390 * GtkIconView:selection-mode:
392 * The ::selection-mode property specifies the selection mode of
393 * icon view. If the mode is #GTK_SELECTION_MULTIPLE, rubberband selection
394 * is enabled, for the other modes, only keyboard selection is possible.
398 g_object_class_install_property (gobject_class,
400 g_param_spec_enum ("selection-mode",
401 P_("Selection mode"),
402 P_("The selection mode"),
403 GTK_TYPE_SELECTION_MODE,
404 GTK_SELECTION_SINGLE,
405 GTK_PARAM_READWRITE));
408 * GtkIconView:pixbuf-column:
410 * The ::pixbuf-column property contains the number of the model column
411 * containing the pixbufs which are displayed. The pixbuf column must be
412 * of type #GDK_TYPE_PIXBUF. Setting this property to -1 turns off the
413 * display of pixbufs.
417 g_object_class_install_property (gobject_class,
419 g_param_spec_int ("pixbuf-column",
421 P_("Model column used to retrieve the icon pixbuf from"),
423 GTK_PARAM_READWRITE));
426 * GtkIconView:text-column:
428 * The ::text-column property contains the number of the model column
429 * containing the texts which are displayed. The text column must be
430 * of type #G_TYPE_STRING. If this property and the :markup-column
431 * property are both set to -1, no texts are displayed.
435 g_object_class_install_property (gobject_class,
437 g_param_spec_int ("text-column",
439 P_("Model column used to retrieve the text from"),
441 GTK_PARAM_READWRITE));
445 * GtkIconView:markup-column:
447 * The ::markup-column property contains the number of the model column
448 * containing markup information to be displayed. The markup column must be
449 * of type #G_TYPE_STRING. If this property and the :text-column property
450 * are both set to column numbers, it overrides the text column.
451 * If both are set to -1, no texts are displayed.
455 g_object_class_install_property (gobject_class,
457 g_param_spec_int ("markup-column",
459 P_("Model column used to retrieve the text if using Pango markup"),
461 GTK_PARAM_READWRITE));
463 g_object_class_install_property (gobject_class,
465 g_param_spec_object ("model",
466 P_("Icon View Model"),
467 P_("The model for the icon view"),
469 GTK_PARAM_READWRITE));
472 * GtkIconView:columns:
474 * The columns property contains the number of the columns in which the
475 * items should be displayed. If it is -1, the number of columns will
476 * be chosen automatically to fill the available area.
480 g_object_class_install_property (gobject_class,
482 g_param_spec_int ("columns",
483 P_("Number of columns"),
484 P_("Number of columns to display"),
486 GTK_PARAM_READWRITE));
490 * GtkIconView:item-width:
492 * The item-width property specifies the width to use for each item.
493 * If it is set to -1, the icon view will automatically determine a
494 * suitable item size.
498 g_object_class_install_property (gobject_class,
500 g_param_spec_int ("item-width",
501 P_("Width for each item"),
502 P_("The width used for each item"),
504 GTK_PARAM_READWRITE));
507 * GtkIconView:spacing:
509 * The spacing property specifies the space which is inserted between
510 * the cells (i.e. the icon and the text) of an item.
514 g_object_class_install_property (gobject_class,
516 g_param_spec_int ("spacing",
518 P_("Space which is inserted between cells of an item"),
520 GTK_PARAM_READWRITE));
523 * GtkIconView:row-spacing:
525 * The row-spacing property specifies the space which is inserted between
526 * the rows of the icon view.
530 g_object_class_install_property (gobject_class,
532 g_param_spec_int ("row-spacing",
534 P_("Space which is inserted between grid rows"),
536 GTK_PARAM_READWRITE));
539 * GtkIconView:column-spacing:
541 * The column-spacing property specifies the space which is inserted between
542 * the columns of the icon view.
546 g_object_class_install_property (gobject_class,
548 g_param_spec_int ("column-spacing",
549 P_("Column Spacing"),
550 P_("Space which is inserted between grid columns"),
552 GTK_PARAM_READWRITE));
555 * GtkIconView:margin:
557 * The margin property specifies the space which is inserted
558 * at the edges of the icon view.
562 g_object_class_install_property (gobject_class,
564 g_param_spec_int ("margin",
566 P_("Space which is inserted at the edges of the icon view"),
568 GTK_PARAM_READWRITE));
571 * GtkIconView:item-orientation:
573 * The item-orientation property specifies how the cells (i.e. the icon and
574 * the text) of the item are positioned relative to each other.
578 g_object_class_install_property (gobject_class,
579 PROP_ITEM_ORIENTATION,
580 g_param_spec_enum ("item-orientation",
581 P_("Item Orientation"),
582 P_("How the text and icon of each item are positioned relative to each other"),
583 GTK_TYPE_ORIENTATION,
584 GTK_ORIENTATION_VERTICAL,
585 GTK_PARAM_READWRITE));
588 * GtkIconView:reorderable:
590 * The reorderable property specifies if the items can be reordered
595 g_object_class_install_property (gobject_class,
597 g_param_spec_boolean ("reorderable",
599 P_("View is reorderable"),
603 g_object_class_install_property (gobject_class,
605 g_param_spec_int ("tooltip-column",
606 P_("Tooltip Column"),
607 P_("The column in the model containing the tooltip texts for the items"),
611 GTK_PARAM_READWRITE));
614 * GtkIconView:item-padding:
616 * The item-padding property specifies the padding around each
617 * of the icon view's item.
621 g_object_class_install_property (gobject_class,
623 g_param_spec_int ("item-padding",
625 P_("Padding around icon view items"),
627 GTK_PARAM_READWRITE));
630 * GtkIconView:cell-area:
632 * The #GtkCellArea used to layout cell renderers for this view.
634 * If no area is specified when creating the icon view with gtk_icon_view_new_with_area()
635 * a #GtkCellAreaBox will be used.
639 g_object_class_install_property (gobject_class,
641 g_param_spec_object ("cell-area",
643 P_("The GtkCellArea used to layout cells"),
645 GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
647 /* Scrollable interface properties */
648 g_object_class_override_property (gobject_class, PROP_HADJUSTMENT, "hadjustment");
649 g_object_class_override_property (gobject_class, PROP_VADJUSTMENT, "vadjustment");
650 g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
651 g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
653 /* Style properties */
654 gtk_widget_class_install_style_property (widget_class,
655 g_param_spec_boxed ("selection-box-color",
656 P_("Selection Box Color"),
657 P_("Color of the selection box"),
659 GTK_PARAM_READABLE));
661 gtk_widget_class_install_style_property (widget_class,
662 g_param_spec_uchar ("selection-box-alpha",
663 P_("Selection Box Alpha"),
664 P_("Opacity of the selection box"),
667 GTK_PARAM_READABLE));
671 * GtkIconView::item-activated:
672 * @iconview: the object on which the signal is emitted
673 * @path: the #GtkTreePath for the activated item
675 * The ::item-activated signal is emitted when the method
676 * gtk_icon_view_item_activated() is called or the user double
677 * clicks an item. It is also emitted when a non-editable item
678 * is selected and one of the keys: Space, Return or Enter is
681 icon_view_signals[ITEM_ACTIVATED] =
682 g_signal_new (I_("item-activated"),
683 G_TYPE_FROM_CLASS (gobject_class),
685 G_STRUCT_OFFSET (GtkIconViewClass, item_activated),
687 g_cclosure_marshal_VOID__BOXED,
692 * GtkIconView::selection-changed:
693 * @iconview: the object on which the signal is emitted
695 * The ::selection-changed signal is emitted when the selection
696 * (i.e. the set of selected items) changes.
698 icon_view_signals[SELECTION_CHANGED] =
699 g_signal_new (I_("selection-changed"),
700 G_TYPE_FROM_CLASS (gobject_class),
702 G_STRUCT_OFFSET (GtkIconViewClass, selection_changed),
704 g_cclosure_marshal_VOID__VOID,
708 * GtkIconView::select-all:
709 * @iconview: the object on which the signal is emitted
711 * A <link linkend="keybinding-signals">keybinding signal</link>
712 * which gets emitted when the user selects all items.
714 * Applications should not connect to it, but may emit it with
715 * g_signal_emit_by_name() if they need to control selection
718 * The default binding for this signal is Ctrl-a.
720 icon_view_signals[SELECT_ALL] =
721 g_signal_new (I_("select-all"),
722 G_TYPE_FROM_CLASS (gobject_class),
723 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
724 G_STRUCT_OFFSET (GtkIconViewClass, select_all),
726 g_cclosure_marshal_VOID__VOID,
730 * GtkIconView::unselect-all:
731 * @iconview: the object on which the signal is emitted
733 * A <link linkend="keybinding-signals">keybinding signal</link>
734 * which gets emitted when the user unselects all items.
736 * Applications should not connect to it, but may emit it with
737 * g_signal_emit_by_name() if they need to control selection
740 * The default binding for this signal is Ctrl-Shift-a.
742 icon_view_signals[UNSELECT_ALL] =
743 g_signal_new (I_("unselect-all"),
744 G_TYPE_FROM_CLASS (gobject_class),
745 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
746 G_STRUCT_OFFSET (GtkIconViewClass, unselect_all),
748 g_cclosure_marshal_VOID__VOID,
752 * GtkIconView::select-cursor-item:
753 * @iconview: the object on which the signal is emitted
755 * A <link linkend="keybinding-signals">keybinding signal</link>
756 * which gets emitted when the user selects the item that is currently
759 * Applications should not connect to it, but may emit it with
760 * g_signal_emit_by_name() if they need to control selection
763 * There is no default binding for this signal.
765 icon_view_signals[SELECT_CURSOR_ITEM] =
766 g_signal_new (I_("select-cursor-item"),
767 G_TYPE_FROM_CLASS (gobject_class),
768 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
769 G_STRUCT_OFFSET (GtkIconViewClass, select_cursor_item),
771 g_cclosure_marshal_VOID__VOID,
775 * GtkIconView::toggle-cursor-item:
776 * @iconview: the object on which the signal is emitted
778 * A <link linkend="keybinding-signals">keybinding signal</link>
779 * which gets emitted when the user toggles whether the currently
780 * focused item is selected or not. The exact effect of this
781 * depend on the selection mode.
783 * Applications should not connect to it, but may emit it with
784 * g_signal_emit_by_name() if they need to control selection
787 * There is no default binding for this signal is Ctrl-Space.
789 icon_view_signals[TOGGLE_CURSOR_ITEM] =
790 g_signal_new (I_("toggle-cursor-item"),
791 G_TYPE_FROM_CLASS (gobject_class),
792 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
793 G_STRUCT_OFFSET (GtkIconViewClass, toggle_cursor_item),
795 g_cclosure_marshal_VOID__VOID,
799 * GtkIconView::activate-cursor-item:
800 * @iconview: the object on which the signal is emitted
802 * A <link linkend="keybinding-signals">keybinding signal</link>
803 * which gets emitted when the user activates the currently
806 * Applications should not connect to it, but may emit it with
807 * g_signal_emit_by_name() if they need to control activation
810 * The default bindings for this signal are Space, Return and Enter.
812 icon_view_signals[ACTIVATE_CURSOR_ITEM] =
813 g_signal_new (I_("activate-cursor-item"),
814 G_TYPE_FROM_CLASS (gobject_class),
815 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
816 G_STRUCT_OFFSET (GtkIconViewClass, activate_cursor_item),
818 _gtk_marshal_BOOLEAN__VOID,
822 * GtkIconView::move-cursor:
823 * @iconview: the object which received the signal
824 * @step: the granularity of the move, as a #GtkMovementStep
825 * @count: the number of @step units to move
827 * The ::move-cursor signal is a
828 * <link linkend="keybinding-signals">keybinding signal</link>
829 * which gets emitted when the user initiates a cursor movement.
831 * Applications should not connect to it, but may emit it with
832 * g_signal_emit_by_name() if they need to control the cursor
835 * The default bindings for this signal include
837 * <listitem>Arrow keys which move by individual steps</listitem>
838 * <listitem>Home/End keys which move to the first/last item</listitem>
839 * <listitem>PageUp/PageDown which move by "pages"</listitem>
842 * All of these will extend the selection when combined with
843 * the Shift modifier.
845 icon_view_signals[MOVE_CURSOR] =
846 g_signal_new (I_("move-cursor"),
847 G_TYPE_FROM_CLASS (gobject_class),
848 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
849 G_STRUCT_OFFSET (GtkIconViewClass, move_cursor),
851 _gtk_marshal_BOOLEAN__ENUM_INT,
853 GTK_TYPE_MOVEMENT_STEP,
857 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
859 gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
861 gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
862 "toggle-cursor-item", 0);
863 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
864 "toggle-cursor-item", 0);
866 gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
867 "activate-cursor-item", 0);
868 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
869 "activate-cursor-item", 0);
870 gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
871 "activate-cursor-item", 0);
872 gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
873 "activate-cursor-item", 0);
874 gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
875 "activate-cursor-item", 0);
877 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Up, 0,
878 GTK_MOVEMENT_DISPLAY_LINES, -1);
879 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
880 GTK_MOVEMENT_DISPLAY_LINES, -1);
882 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Down, 0,
883 GTK_MOVEMENT_DISPLAY_LINES, 1);
884 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
885 GTK_MOVEMENT_DISPLAY_LINES, 1);
887 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_p, GDK_CONTROL_MASK,
888 GTK_MOVEMENT_DISPLAY_LINES, -1);
890 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_n, GDK_CONTROL_MASK,
891 GTK_MOVEMENT_DISPLAY_LINES, 1);
893 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Home, 0,
894 GTK_MOVEMENT_BUFFER_ENDS, -1);
895 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
896 GTK_MOVEMENT_BUFFER_ENDS, -1);
898 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_End, 0,
899 GTK_MOVEMENT_BUFFER_ENDS, 1);
900 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_End, 0,
901 GTK_MOVEMENT_BUFFER_ENDS, 1);
903 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
904 GTK_MOVEMENT_PAGES, -1);
905 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
906 GTK_MOVEMENT_PAGES, -1);
908 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
909 GTK_MOVEMENT_PAGES, 1);
910 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
911 GTK_MOVEMENT_PAGES, 1);
913 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Right, 0,
914 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
915 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Left, 0,
916 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
918 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
919 GTK_MOVEMENT_VISUAL_POSITIONS, 1);
920 gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
921 GTK_MOVEMENT_VISUAL_POSITIONS, -1);
923 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ICON_VIEW_ACCESSIBLE);
927 gtk_icon_view_buildable_init (GtkBuildableIface *iface)
929 parent_buildable_iface = g_type_interface_peek_parent (iface);
930 iface->add_child = _gtk_cell_layout_buildable_add_child;
931 iface->custom_tag_start = gtk_icon_view_buildable_custom_tag_start;
932 iface->custom_tag_end = gtk_icon_view_buildable_custom_tag_end;
936 gtk_icon_view_cell_layout_init (GtkCellLayoutIface *iface)
938 iface->get_area = gtk_icon_view_cell_layout_get_area;
942 gtk_icon_view_init (GtkIconView *icon_view)
944 icon_view->priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_view,
948 icon_view->priv->width = 0;
949 icon_view->priv->height = 0;
950 icon_view->priv->selection_mode = GTK_SELECTION_SINGLE;
951 icon_view->priv->pressed_button = -1;
952 icon_view->priv->press_start_x = -1;
953 icon_view->priv->press_start_y = -1;
954 icon_view->priv->text_column = -1;
955 icon_view->priv->markup_column = -1;
956 icon_view->priv->pixbuf_column = -1;
957 icon_view->priv->text_cell = NULL;
958 icon_view->priv->pixbuf_cell = NULL;
959 icon_view->priv->tooltip_column = -1;
961 gtk_widget_set_can_focus (GTK_WIDGET (icon_view), TRUE);
963 icon_view->priv->item_orientation = GTK_ORIENTATION_VERTICAL;
965 icon_view->priv->columns = -1;
966 icon_view->priv->item_width = -1;
967 icon_view->priv->spacing = 0;
968 icon_view->priv->row_spacing = 6;
969 icon_view->priv->column_spacing = 6;
970 icon_view->priv->margin = 6;
971 icon_view->priv->item_padding = 6;
973 icon_view->priv->draw_focus = TRUE;
975 icon_view->priv->row_contexts =
976 g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
979 /* GObject methods */
981 gtk_icon_view_constructor (GType type,
982 guint n_construct_properties,
983 GObjectConstructParam *construct_properties)
985 GtkIconView *icon_view;
988 object = G_OBJECT_CLASS (gtk_icon_view_parent_class)->constructor
989 (type, n_construct_properties, construct_properties);
991 icon_view = (GtkIconView *) object;
993 gtk_icon_view_ensure_cell_area (icon_view, NULL);
999 gtk_icon_view_dispose (GObject *object)
1001 GtkIconView *icon_view;
1002 GtkIconViewPrivate *priv;
1004 icon_view = GTK_ICON_VIEW (object);
1005 priv = icon_view->priv;
1007 if (priv->cell_area_context)
1009 g_object_unref (priv->cell_area_context);
1010 priv->cell_area_context = NULL;
1013 if (priv->row_contexts)
1015 g_ptr_array_free (priv->row_contexts, TRUE);
1016 priv->row_contexts = NULL;
1019 if (priv->cell_area)
1021 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
1023 g_signal_handler_disconnect (priv->cell_area, priv->add_editable_id);
1024 g_signal_handler_disconnect (priv->cell_area, priv->remove_editable_id);
1025 priv->add_editable_id = 0;
1026 priv->remove_editable_id = 0;
1028 g_object_unref (priv->cell_area);
1029 priv->cell_area = NULL;
1032 G_OBJECT_CLASS (gtk_icon_view_parent_class)->dispose (object);
1036 gtk_icon_view_set_property (GObject *object,
1038 const GValue *value,
1041 GtkIconView *icon_view;
1044 icon_view = GTK_ICON_VIEW (object);
1048 case PROP_SELECTION_MODE:
1049 gtk_icon_view_set_selection_mode (icon_view, g_value_get_enum (value));
1051 case PROP_PIXBUF_COLUMN:
1052 gtk_icon_view_set_pixbuf_column (icon_view, g_value_get_int (value));
1054 case PROP_TEXT_COLUMN:
1055 gtk_icon_view_set_text_column (icon_view, g_value_get_int (value));
1057 case PROP_MARKUP_COLUMN:
1058 gtk_icon_view_set_markup_column (icon_view, g_value_get_int (value));
1061 gtk_icon_view_set_model (icon_view, g_value_get_object (value));
1063 case PROP_ITEM_ORIENTATION:
1064 gtk_icon_view_set_item_orientation (icon_view, g_value_get_enum (value));
1067 gtk_icon_view_set_columns (icon_view, g_value_get_int (value));
1069 case PROP_ITEM_WIDTH:
1070 gtk_icon_view_set_item_width (icon_view, g_value_get_int (value));
1073 gtk_icon_view_set_spacing (icon_view, g_value_get_int (value));
1075 case PROP_ROW_SPACING:
1076 gtk_icon_view_set_row_spacing (icon_view, g_value_get_int (value));
1078 case PROP_COLUMN_SPACING:
1079 gtk_icon_view_set_column_spacing (icon_view, g_value_get_int (value));
1082 gtk_icon_view_set_margin (icon_view, g_value_get_int (value));
1084 case PROP_REORDERABLE:
1085 gtk_icon_view_set_reorderable (icon_view, g_value_get_boolean (value));
1088 case PROP_TOOLTIP_COLUMN:
1089 gtk_icon_view_set_tooltip_column (icon_view, g_value_get_int (value));
1092 case PROP_ITEM_PADDING:
1093 gtk_icon_view_set_item_padding (icon_view, g_value_get_int (value));
1096 case PROP_CELL_AREA:
1097 /* Construct-only, can only be assigned once */
1098 area = g_value_get_object (value);
1101 if (icon_view->priv->cell_area != NULL)
1103 g_warning ("cell-area has already been set, ignoring construct property");
1104 g_object_ref_sink (area);
1105 g_object_unref (area);
1108 gtk_icon_view_ensure_cell_area (icon_view, area);
1112 case PROP_HADJUSTMENT:
1113 gtk_icon_view_set_hadjustment (icon_view, g_value_get_object (value));
1115 case PROP_VADJUSTMENT:
1116 gtk_icon_view_set_vadjustment (icon_view, g_value_get_object (value));
1118 case PROP_HSCROLL_POLICY:
1119 icon_view->priv->hscroll_policy = g_value_get_enum (value);
1120 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
1122 case PROP_VSCROLL_POLICY:
1123 icon_view->priv->vscroll_policy = g_value_get_enum (value);
1124 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
1128 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1134 gtk_icon_view_get_property (GObject *object,
1139 GtkIconView *icon_view;
1141 icon_view = GTK_ICON_VIEW (object);
1145 case PROP_SELECTION_MODE:
1146 g_value_set_enum (value, icon_view->priv->selection_mode);
1148 case PROP_PIXBUF_COLUMN:
1149 g_value_set_int (value, icon_view->priv->pixbuf_column);
1151 case PROP_TEXT_COLUMN:
1152 g_value_set_int (value, icon_view->priv->text_column);
1154 case PROP_MARKUP_COLUMN:
1155 g_value_set_int (value, icon_view->priv->markup_column);
1158 g_value_set_object (value, icon_view->priv->model);
1160 case PROP_ITEM_ORIENTATION:
1161 g_value_set_enum (value, icon_view->priv->item_orientation);
1164 g_value_set_int (value, icon_view->priv->columns);
1166 case PROP_ITEM_WIDTH:
1167 g_value_set_int (value, icon_view->priv->item_width);
1170 g_value_set_int (value, icon_view->priv->spacing);
1172 case PROP_ROW_SPACING:
1173 g_value_set_int (value, icon_view->priv->row_spacing);
1175 case PROP_COLUMN_SPACING:
1176 g_value_set_int (value, icon_view->priv->column_spacing);
1179 g_value_set_int (value, icon_view->priv->margin);
1181 case PROP_REORDERABLE:
1182 g_value_set_boolean (value, icon_view->priv->reorderable);
1184 case PROP_TOOLTIP_COLUMN:
1185 g_value_set_int (value, icon_view->priv->tooltip_column);
1188 case PROP_ITEM_PADDING:
1189 g_value_set_int (value, icon_view->priv->item_padding);
1192 case PROP_CELL_AREA:
1193 g_value_set_object (value, icon_view->priv->cell_area);
1196 case PROP_HADJUSTMENT:
1197 g_value_set_object (value, icon_view->priv->hadjustment);
1199 case PROP_VADJUSTMENT:
1200 g_value_set_object (value, icon_view->priv->vadjustment);
1202 case PROP_HSCROLL_POLICY:
1203 g_value_set_enum (value, icon_view->priv->hscroll_policy);
1205 case PROP_VSCROLL_POLICY:
1206 g_value_set_enum (value, icon_view->priv->vscroll_policy);
1210 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1215 /* GtkWidget methods */
1217 gtk_icon_view_destroy (GtkWidget *widget)
1219 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1221 gtk_icon_view_set_model (icon_view, NULL);
1223 if (icon_view->priv->scroll_to_path != NULL)
1225 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1226 icon_view->priv->scroll_to_path = NULL;
1229 remove_scroll_timeout (icon_view);
1231 if (icon_view->priv->hadjustment != NULL)
1233 g_object_unref (icon_view->priv->hadjustment);
1234 icon_view->priv->hadjustment = NULL;
1237 if (icon_view->priv->vadjustment != NULL)
1239 g_object_unref (icon_view->priv->vadjustment);
1240 icon_view->priv->vadjustment = NULL;
1243 GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->destroy (widget);
1247 gtk_icon_view_realize (GtkWidget *widget)
1249 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1250 GtkAllocation allocation;
1252 GdkWindowAttr attributes;
1253 gint attributes_mask;
1254 GtkStyleContext *context;
1256 gtk_widget_set_realized (widget, TRUE);
1258 gtk_widget_get_allocation (widget, &allocation);
1260 /* Make the main, clipping window */
1261 attributes.window_type = GDK_WINDOW_CHILD;
1262 attributes.x = allocation.x;
1263 attributes.y = allocation.y;
1264 attributes.width = allocation.width;
1265 attributes.height = allocation.height;
1266 attributes.wclass = GDK_INPUT_OUTPUT;
1267 attributes.visual = gtk_widget_get_visual (widget);
1268 attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1270 attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1272 window = gdk_window_new (gtk_widget_get_parent_window (widget),
1273 &attributes, attributes_mask);
1274 gtk_widget_set_window (widget, window);
1275 gdk_window_set_user_data (window, widget);
1277 gtk_widget_get_allocation (widget, &allocation);
1279 /* Make the window for the icon view */
1282 attributes.width = MAX (icon_view->priv->width, allocation.width);
1283 attributes.height = MAX (icon_view->priv->height, allocation.height);
1284 attributes.event_mask = (GDK_EXPOSURE_MASK |
1286 GDK_SMOOTH_SCROLL_MASK |
1287 GDK_POINTER_MOTION_MASK |
1288 GDK_BUTTON_PRESS_MASK |
1289 GDK_BUTTON_RELEASE_MASK |
1290 GDK_KEY_PRESS_MASK |
1291 GDK_KEY_RELEASE_MASK) |
1292 gtk_widget_get_events (widget);
1294 icon_view->priv->bin_window = gdk_window_new (window,
1295 &attributes, attributes_mask);
1296 gdk_window_set_user_data (icon_view->priv->bin_window, widget);
1298 context = gtk_widget_get_style_context (widget);
1300 gtk_style_context_save (context);
1301 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
1302 gtk_style_context_set_background (context, icon_view->priv->bin_window);
1303 gtk_style_context_set_background (context, window);
1304 gtk_style_context_restore (context);
1306 gdk_window_show (icon_view->priv->bin_window);
1310 gtk_icon_view_unrealize (GtkWidget *widget)
1312 GtkIconView *icon_view;
1314 icon_view = GTK_ICON_VIEW (widget);
1316 gdk_window_set_user_data (icon_view->priv->bin_window, NULL);
1317 gdk_window_destroy (icon_view->priv->bin_window);
1318 icon_view->priv->bin_window = NULL;
1320 GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->unrealize (widget);
1324 _gtk_icon_view_update_background (GtkIconView *icon_view)
1326 GtkWidget *widget = GTK_WIDGET (icon_view);
1328 if (gtk_widget_get_realized (widget))
1330 GtkStyleContext *context;
1332 context = gtk_widget_get_style_context (widget);
1334 gtk_style_context_save (context);
1335 gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
1337 gtk_style_context_set_background (context, gtk_widget_get_window (widget));
1338 gtk_style_context_set_background (context, icon_view->priv->bin_window);
1340 gtk_style_context_restore (context);
1345 gtk_icon_view_state_flags_changed (GtkWidget *widget,
1346 GtkStateFlags previous_state)
1348 _gtk_icon_view_update_background (GTK_ICON_VIEW (widget));
1349 gtk_widget_queue_draw (widget);
1353 gtk_icon_view_style_updated (GtkWidget *widget)
1355 GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->style_updated (widget);
1357 _gtk_icon_view_update_background (GTK_ICON_VIEW (widget));
1358 gtk_widget_queue_resize (widget);
1362 gtk_icon_view_get_n_items (GtkIconView *icon_view)
1364 GtkIconViewPrivate *priv = icon_view->priv;
1366 if (priv->model == NULL)
1369 return gtk_tree_model_iter_n_children (priv->model, NULL);
1373 adjust_wrap_width (GtkIconView *icon_view)
1375 if (icon_view->priv->text_cell)
1377 gint wrap_width = 50;
1379 /* Here we go with the same old guess, try the icon size and set double
1380 * the size of the first icon found in the list, naive but works much
1382 if (icon_view->priv->items && icon_view->priv->pixbuf_cell)
1384 gtk_cell_renderer_get_preferred_width (icon_view->priv->pixbuf_cell,
1385 GTK_WIDGET (icon_view),
1388 wrap_width = MAX (wrap_width * 2, 50);
1391 g_object_set (icon_view->priv->text_cell, "wrap-width", wrap_width, NULL);
1392 g_object_set (icon_view->priv->text_cell, "width", wrap_width, NULL);
1396 /* General notes about layout
1398 * The icon view is layouted like this:
1400 * +----------+ s +----------+
1401 * | padding | p | padding |
1402 * | +------+ | a | +------+ |
1403 * | | cell | | c | | cell | |
1404 * | +------+ | i | +------+ |
1406 * +----------+ g +----------+
1408 * In size request and allocation code, there are 3 sizes that are used:
1410 * This is the size returned by gtk_cell_area_get_preferred_foo(). In places
1411 * where code is interacting with the cell area and renderers this is useful.
1413 * This is the cell size plus the item padding on each side.
1415 * This is the padded size plus the spacing. This is what's used for most
1416 * calculations because it can (ab)use the following formula:
1417 * iconview_size = 2 * margin + n_items * spaced_size - spacing
1418 * So when reading this code and fixing my bugs where I confuse these two, be
1419 * aware of this distinction.
1422 cell_area_get_preferred_size (GtkIconView *icon_view,
1423 GtkCellAreaContext *context,
1424 GtkOrientation orientation,
1429 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1432 gtk_cell_area_get_preferred_width_for_height (icon_view->priv->cell_area,
1434 GTK_WIDGET (icon_view),
1438 gtk_cell_area_get_preferred_width (icon_view->priv->cell_area,
1440 GTK_WIDGET (icon_view),
1446 gtk_cell_area_get_preferred_height_for_width (icon_view->priv->cell_area,
1448 GTK_WIDGET (icon_view),
1452 gtk_cell_area_get_preferred_height (icon_view->priv->cell_area,
1454 GTK_WIDGET (icon_view),
1460 gtk_icon_view_get_preferred_item_size (GtkIconView *icon_view,
1461 GtkOrientation orientation,
1466 GtkIconViewPrivate *priv = icon_view->priv;
1467 GtkCellAreaContext *context;
1470 context = gtk_cell_area_create_context (priv->cell_area);
1472 for_size -= 2 * priv->item_padding;
1476 /* This is necessary for the context to work properly */
1477 for (items = priv->items; items; items = items->next)
1479 GtkIconViewItem *item = items->data;
1481 _gtk_icon_view_set_cell_data (icon_view, item);
1482 cell_area_get_preferred_size (icon_view, context, 1 - orientation, -1, NULL, NULL);
1486 for (items = priv->items; items; items = items->next)
1488 GtkIconViewItem *item = items->data;
1490 _gtk_icon_view_set_cell_data (icon_view, item);
1491 if (items == priv->items)
1492 adjust_wrap_width (icon_view);
1493 cell_area_get_preferred_size (icon_view, context, orientation, for_size, NULL, NULL);
1496 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1499 gtk_cell_area_context_get_preferred_width_for_height (context,
1503 gtk_cell_area_context_get_preferred_width (context,
1509 gtk_cell_area_context_get_preferred_height_for_width (context,
1513 gtk_cell_area_context_get_preferred_height (context,
1517 if (orientation == GTK_ORIENTATION_HORIZONTAL && priv->item_width >= 0)
1520 *minimum = MAX (*minimum, priv->item_width);
1522 *natural = *minimum;
1526 *minimum += 2 * priv->item_padding;
1528 *natural += 2 * priv->item_padding;
1530 g_object_unref (context);
1534 gtk_icon_view_compute_n_items_for_size (GtkIconView *icon_view,
1535 GtkOrientation orientation,
1538 gint *min_item_size,
1540 gint *max_item_size)
1542 GtkIconViewPrivate *priv = icon_view->priv;
1543 int minimum, natural, spacing;
1545 g_return_if_fail (min_item_size == NULL || min_items != NULL);
1546 g_return_if_fail (max_item_size == NULL || max_items != NULL);
1548 gtk_icon_view_get_preferred_item_size (icon_view, orientation, -1, &minimum, &natural);
1550 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1551 spacing = priv->column_spacing;
1553 spacing = priv->row_spacing;
1555 size -= 2 * priv->margin;
1560 if (priv->columns > 0)
1562 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1565 *min_items = priv->columns;
1567 *max_items = priv->columns;
1571 int n_items = gtk_icon_view_get_n_items (icon_view);
1574 *min_items = (n_items + priv->columns - 1) / priv->columns;
1576 *max_items = (n_items + priv->columns - 1) / priv->columns;
1583 if (size <= minimum)
1586 *max_items = size / minimum;
1591 if (size <= natural)
1594 *min_items = size / natural;
1600 *min_item_size = size / *min_items;
1601 *min_item_size = MIN (*min_item_size, natural);
1602 *min_item_size -= spacing;
1603 *min_item_size -= 2 * priv->item_padding;
1608 *max_item_size = size / *max_items;
1609 *max_item_size = MIN (*max_item_size, natural);
1610 *max_item_size -= spacing;
1611 *max_item_size -= 2 * priv->item_padding;
1615 static GtkSizeRequestMode
1616 gtk_icon_view_get_request_mode (GtkWidget *widget)
1618 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1622 gtk_icon_view_get_preferred_width (GtkWidget *widget,
1626 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1627 GtkIconViewPrivate *priv = icon_view->priv;
1628 int item_min, item_nat;
1630 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, -1, &item_min, &item_nat);
1632 if (priv->columns > 0)
1634 *minimum = item_min * priv->columns + priv->column_spacing * (priv->columns - 1);
1635 *natural = item_nat * priv->columns + priv->column_spacing * (priv->columns - 1);
1639 int n_items = gtk_icon_view_get_n_items (icon_view);
1648 *minimum = item_min;
1649 *natural = item_nat * n_items + priv->column_spacing * (n_items - 1);
1653 *minimum += 2 * priv->margin;
1654 *natural += 2 * priv->margin;
1658 gtk_icon_view_get_preferred_width_for_height (GtkWidget *widget,
1663 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1664 GtkIconViewPrivate *priv = icon_view->priv;
1665 int item_min, item_nat, rows, row_height, n_items;
1667 gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_VERTICAL, height, &rows, &row_height, NULL, NULL);
1668 n_items = gtk_icon_view_get_n_items (icon_view);
1670 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, row_height, &item_min, &item_nat);
1671 *minimum = (item_min + priv->column_spacing) * ((n_items + rows - 1) / rows) - priv->column_spacing;
1672 *natural = (item_nat + priv->column_spacing) * ((n_items + rows - 1) / rows) - priv->column_spacing;
1674 *minimum += 2 * priv->margin;
1675 *natural += 2 * priv->margin;
1679 gtk_icon_view_get_preferred_height (GtkWidget *widget,
1683 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1684 GtkIconViewPrivate *priv = icon_view->priv;
1685 int item_min, item_nat, n_items;
1687 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, -1, &item_min, &item_nat);
1688 n_items = gtk_icon_view_get_n_items (icon_view);
1690 if (priv->columns > 0)
1692 int n_rows = (n_items + priv->columns - 1) / priv->columns;
1694 *minimum = item_min * n_rows + priv->row_spacing * (n_rows - 1);
1695 *natural = item_nat * n_rows + priv->row_spacing * (n_rows - 1);
1706 *minimum = item_min;
1707 *natural = item_nat * n_items + priv->row_spacing * (n_items - 1);
1711 *minimum += 2 * priv->margin;
1712 *natural += 2 * priv->margin;
1716 gtk_icon_view_get_preferred_height_for_width (GtkWidget *widget,
1721 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1722 GtkIconViewPrivate *priv = icon_view->priv;
1723 int item_min, item_nat, columns, column_width, n_items;
1725 gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_HORIZONTAL, width, NULL, NULL, &columns, &column_width);
1726 n_items = gtk_icon_view_get_n_items (icon_view);
1728 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, column_width, &item_min, &item_nat);
1729 *minimum = (item_min + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1730 *natural = (item_nat + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1732 *minimum += 2 * priv->margin;
1733 *natural += 2 * priv->margin;
1737 gtk_icon_view_allocate_children (GtkIconView *icon_view)
1741 for (list = icon_view->priv->children; list; list = list->next)
1743 GtkIconViewChild *child = list->data;
1745 /* totally ignore our child's requisition */
1746 gtk_widget_size_allocate (child->widget, &child->area);
1751 gtk_icon_view_size_allocate (GtkWidget *widget,
1752 GtkAllocation *allocation)
1754 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1756 gtk_widget_set_allocation (widget, allocation);
1758 gtk_icon_view_layout (icon_view);
1760 if (gtk_widget_get_realized (widget))
1762 gdk_window_move_resize (gtk_widget_get_window (widget),
1763 allocation->x, allocation->y,
1764 allocation->width, allocation->height);
1765 gdk_window_resize (icon_view->priv->bin_window,
1766 MAX (icon_view->priv->width, allocation->width),
1767 MAX (icon_view->priv->height, allocation->height));
1770 gtk_icon_view_allocate_children (icon_view);
1772 /* Delay signal emission */
1773 g_object_freeze_notify (G_OBJECT (icon_view->priv->hadjustment));
1774 g_object_freeze_notify (G_OBJECT (icon_view->priv->vadjustment));
1776 gtk_icon_view_set_hadjustment_values (icon_view);
1777 gtk_icon_view_set_vadjustment_values (icon_view);
1779 if (gtk_widget_get_realized (widget) &&
1780 icon_view->priv->scroll_to_path)
1783 path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
1784 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1785 icon_view->priv->scroll_to_path = NULL;
1787 gtk_icon_view_scroll_to_path (icon_view, path,
1788 icon_view->priv->scroll_to_use_align,
1789 icon_view->priv->scroll_to_row_align,
1790 icon_view->priv->scroll_to_col_align);
1791 gtk_tree_path_free (path);
1794 /* Emit any pending signals now */
1795 g_object_thaw_notify (G_OBJECT (icon_view->priv->hadjustment));
1796 g_object_thaw_notify (G_OBJECT (icon_view->priv->vadjustment));
1800 gtk_icon_view_draw (GtkWidget *widget,
1803 GtkIconView *icon_view;
1807 GtkIconViewDropPosition dest_pos;
1808 GtkIconViewItem *dest_item = NULL;
1810 icon_view = GTK_ICON_VIEW (widget);
1812 if (!gtk_cairo_should_draw_window (cr, icon_view->priv->bin_window))
1817 gtk_cairo_transform_to_window (cr, widget, icon_view->priv->bin_window);
1819 cairo_set_line_width (cr, 1.);
1821 gtk_icon_view_get_drag_dest_item (icon_view, &path, &dest_pos);
1825 dest_index = gtk_tree_path_get_indices (path)[0];
1826 gtk_tree_path_free (path);
1831 for (icons = icon_view->priv->items; icons; icons = icons->next)
1833 GtkIconViewItem *item = icons->data;
1834 GdkRectangle paint_area;
1836 paint_area.x = item->cell_area.x - icon_view->priv->item_padding;
1837 paint_area.y = item->cell_area.y - icon_view->priv->item_padding;
1838 paint_area.width = item->cell_area.width + icon_view->priv->item_padding * 2;
1839 paint_area.height = item->cell_area.height + icon_view->priv->item_padding * 2;
1843 cairo_rectangle (cr, paint_area.x, paint_area.y, paint_area.width, paint_area.height);
1846 if (gdk_cairo_get_clip_rectangle (cr, NULL))
1848 gtk_icon_view_paint_item (icon_view, cr, item,
1849 item->cell_area.x, item->cell_area.y,
1850 icon_view->priv->draw_focus);
1852 if (dest_index == item->index)
1860 dest_pos != GTK_ICON_VIEW_NO_DROP)
1862 GtkStyleContext *context;
1863 GdkRectangle rect = { 0 };
1865 context = gtk_widget_get_style_context (widget);
1869 case GTK_ICON_VIEW_DROP_INTO:
1870 rect = dest_item->cell_area;
1872 case GTK_ICON_VIEW_DROP_ABOVE:
1873 rect.x = dest_item->cell_area.x;
1874 rect.y = dest_item->cell_area.y - 1;
1875 rect.width = dest_item->cell_area.width;
1878 case GTK_ICON_VIEW_DROP_LEFT:
1879 rect.x = dest_item->cell_area.x - 1;
1880 rect.y = dest_item->cell_area.y;
1882 rect.height = dest_item->cell_area.height;
1884 case GTK_ICON_VIEW_DROP_BELOW:
1885 rect.x = dest_item->cell_area.x;
1886 rect.y = dest_item->cell_area.y + dest_item->cell_area.height - 1;
1887 rect.width = dest_item->cell_area.width;
1890 case GTK_ICON_VIEW_DROP_RIGHT:
1891 rect.x = dest_item->cell_area.x + dest_item->cell_area.width - 1;
1892 rect.y = dest_item->cell_area.y;
1894 rect.height = dest_item->cell_area.height;
1895 case GTK_ICON_VIEW_NO_DROP: ;
1899 gtk_render_focus (context, cr,
1901 rect.width, rect.height);
1904 if (icon_view->priv->doing_rubberband)
1905 gtk_icon_view_paint_rubberband (icon_view, cr);
1909 return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->draw (widget, cr);
1913 rubberband_scroll_timeout (gpointer data)
1915 GtkIconView *icon_view = data;
1917 gtk_adjustment_set_value (icon_view->priv->vadjustment,
1918 gtk_adjustment_get_value (icon_view->priv->vadjustment) +
1919 icon_view->priv->scroll_value_diff);
1921 gtk_icon_view_update_rubberband (icon_view);
1927 gtk_icon_view_motion (GtkWidget *widget,
1928 GdkEventMotion *event)
1930 GtkAllocation allocation;
1931 GtkIconView *icon_view;
1934 icon_view = GTK_ICON_VIEW (widget);
1936 gtk_icon_view_maybe_begin_drag (icon_view, event);
1938 if (icon_view->priv->doing_rubberband)
1940 gtk_icon_view_update_rubberband (widget);
1942 abs_y = event->y - icon_view->priv->height *
1943 (gtk_adjustment_get_value (icon_view->priv->vadjustment) /
1944 (gtk_adjustment_get_upper (icon_view->priv->vadjustment) -
1945 gtk_adjustment_get_lower (icon_view->priv->vadjustment)));
1947 gtk_widget_get_allocation (widget, &allocation);
1949 if (abs_y < 0 || abs_y > allocation.height)
1952 icon_view->priv->scroll_value_diff = abs_y;
1954 icon_view->priv->scroll_value_diff = abs_y - allocation.height;
1956 icon_view->priv->event_last_x = event->x;
1957 icon_view->priv->event_last_y = event->y;
1959 if (icon_view->priv->scroll_timeout_id == 0)
1960 icon_view->priv->scroll_timeout_id = gdk_threads_add_timeout (30, rubberband_scroll_timeout,
1964 remove_scroll_timeout (icon_view);
1968 GtkIconViewItem *item, *last_prelight_item;
1969 GtkCellRenderer *cell = NULL;
1971 last_prelight_item = icon_view->priv->last_prelight;
1972 item = _gtk_icon_view_get_item_at_coords (icon_view,
1979 item->prelight = TRUE;
1980 gtk_icon_view_queue_draw_item (icon_view, item);
1983 if (last_prelight_item != NULL &&
1984 last_prelight_item != item)
1986 last_prelight_item->prelight = FALSE;
1987 gtk_icon_view_queue_draw_item (icon_view,
1988 icon_view->priv->last_prelight);
1991 icon_view->priv->last_prelight = item;
1998 gtk_icon_view_remove (GtkContainer *container,
2001 GtkIconView *icon_view;
2002 GtkIconViewChild *child = NULL;
2005 icon_view = GTK_ICON_VIEW (container);
2007 tmp_list = icon_view->priv->children;
2010 child = tmp_list->data;
2011 if (child->widget == widget)
2013 gtk_widget_unparent (widget);
2015 icon_view->priv->children = g_list_remove_link (icon_view->priv->children, tmp_list);
2016 g_list_free_1 (tmp_list);
2021 tmp_list = tmp_list->next;
2026 gtk_icon_view_forall (GtkContainer *container,
2027 gboolean include_internals,
2028 GtkCallback callback,
2029 gpointer callback_data)
2031 GtkIconView *icon_view;
2032 GtkIconViewChild *child = NULL;
2035 icon_view = GTK_ICON_VIEW (container);
2037 tmp_list = icon_view->priv->children;
2040 child = tmp_list->data;
2041 tmp_list = tmp_list->next;
2043 (* callback) (child->widget, callback_data);
2048 gtk_icon_view_item_selected_changed (GtkIconView *icon_view,
2049 GtkIconViewItem *item)
2052 AtkObject *item_obj;
2054 obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
2057 item_obj = atk_object_ref_accessible_child (obj, item->index);
2058 if (item_obj != NULL)
2060 atk_object_notify_state_change (item_obj, ATK_STATE_SELECTED, item->selected);
2061 g_object_unref (item_obj);
2067 gtk_icon_view_add_editable (GtkCellArea *area,
2068 GtkCellRenderer *renderer,
2069 GtkCellEditable *editable,
2070 GdkRectangle *cell_area,
2072 GtkIconView *icon_view)
2074 GtkIconViewChild *child;
2075 GtkWidget *widget = GTK_WIDGET (editable);
2077 child = g_new (GtkIconViewChild, 1);
2079 child->widget = widget;
2080 child->area.x = cell_area->x;
2081 child->area.y = cell_area->y;
2082 child->area.width = cell_area->width;
2083 child->area.height = cell_area->height;
2085 icon_view->priv->children = g_list_append (icon_view->priv->children, child);
2087 if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2088 gtk_widget_set_parent_window (child->widget, icon_view->priv->bin_window);
2090 gtk_widget_set_parent (widget, GTK_WIDGET (icon_view));
2094 gtk_icon_view_remove_editable (GtkCellArea *area,
2095 GtkCellRenderer *renderer,
2096 GtkCellEditable *editable,
2097 GtkIconView *icon_view)
2101 if (gtk_widget_has_focus (GTK_WIDGET (editable)))
2102 gtk_widget_grab_focus (GTK_WIDGET (icon_view));
2104 gtk_container_remove (GTK_CONTAINER (icon_view),
2105 GTK_WIDGET (editable));
2107 path = gtk_tree_path_new_from_string (gtk_cell_area_get_current_path_string (area));
2108 gtk_icon_view_queue_draw_path (icon_view, path);
2109 gtk_tree_path_free (path);
2113 * gtk_icon_view_set_cursor:
2114 * @icon_view: A #GtkIconView
2115 * @path: A #GtkTreePath
2116 * @cell: (allow-none): One of the cell renderers of @icon_view, or %NULL
2117 * @start_editing: %TRUE if the specified cell should start being edited.
2119 * Sets the current keyboard focus to be at @path, and selects it. This is
2120 * useful when you want to focus the user's attention on a particular item.
2121 * If @cell is not %NULL, then focus is given to the cell specified by
2122 * it. Additionally, if @start_editing is %TRUE, then editing should be
2123 * started in the specified cell.
2125 * This function is often followed by <literal>gtk_widget_grab_focus
2126 * (icon_view)</literal> in order to give keyboard focus to the widget.
2127 * Please note that editing can only happen when the widget is realized.
2132 gtk_icon_view_set_cursor (GtkIconView *icon_view,
2134 GtkCellRenderer *cell,
2135 gboolean start_editing)
2137 GtkIconViewItem *item = NULL;
2139 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
2140 g_return_if_fail (path != NULL);
2141 g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
2143 if (icon_view->priv->cell_area)
2144 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2146 if (gtk_tree_path_get_depth (path) == 1)
2147 item = g_list_nth_data (icon_view->priv->items,
2148 gtk_tree_path_get_indices(path)[0]);
2153 _gtk_icon_view_set_cursor_item (icon_view, item, cell);
2154 gtk_icon_view_scroll_to_path (icon_view, path, FALSE, 0.0, 0.0);
2156 if (start_editing &&
2157 icon_view->priv->cell_area)
2159 GtkCellAreaContext *context;
2161 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2162 _gtk_icon_view_set_cell_data (icon_view, item);
2163 gtk_cell_area_activate (icon_view->priv->cell_area, context,
2164 GTK_WIDGET (icon_view), &item->cell_area,
2165 0 /* XXX flags */, TRUE);
2170 * gtk_icon_view_get_cursor:
2171 * @icon_view: A #GtkIconView
2172 * @path: (out) (allow-none): Return location for the current cursor path,
2174 * @cell: (out) (allow-none): Return location the current focus cell, or %NULL
2176 * Fills in @path and @cell with the current cursor path and cell.
2177 * If the cursor isn't currently set, then *@path will be %NULL.
2178 * If no cell currently has focus, then *@cell will be %NULL.
2180 * The returned #GtkTreePath must be freed with gtk_tree_path_free().
2182 * Return value: %TRUE if the cursor is set.
2187 gtk_icon_view_get_cursor (GtkIconView *icon_view,
2189 GtkCellRenderer **cell)
2191 GtkIconViewItem *item;
2193 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
2195 item = icon_view->priv->cursor_item;
2200 *path = gtk_tree_path_new_from_indices (item->index, -1);
2205 if (cell != NULL && item != NULL && icon_view->priv->cell_area != NULL)
2206 *cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
2208 return (item != NULL);
2212 gtk_icon_view_button_press (GtkWidget *widget,
2213 GdkEventButton *event)
2215 GtkIconView *icon_view;
2216 GtkIconViewItem *item;
2217 gboolean dirty = FALSE;
2218 GtkCellRenderer *cell = NULL, *cursor_cell = NULL;
2220 icon_view = GTK_ICON_VIEW (widget);
2222 if (event->window != icon_view->priv->bin_window)
2225 if (!gtk_widget_has_focus (widget))
2226 gtk_widget_grab_focus (widget);
2228 if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_BUTTON_PRESS)
2230 GdkModifierType extend_mod_mask;
2231 GdkModifierType modify_mod_mask;
2234 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_EXTEND_SELECTION);
2237 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_MODIFY_SELECTION);
2239 item = _gtk_icon_view_get_item_at_coords (icon_view,
2245 * We consider only the the cells' area as the item area if the
2246 * item is not selected, but if it *is* selected, the complete
2247 * selection rectangle is considered to be part of the item.
2249 if (item != NULL && (cell != NULL || item->selected))
2253 if (gtk_cell_renderer_is_activatable (cell))
2257 gtk_icon_view_scroll_to_item (icon_view, item);
2259 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2261 _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2263 else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
2264 (event->state & extend_mod_mask))
2266 gtk_icon_view_unselect_all_internal (icon_view);
2268 _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2269 if (!icon_view->priv->anchor_item)
2270 icon_view->priv->anchor_item = item;
2272 gtk_icon_view_select_all_between (icon_view,
2273 icon_view->priv->anchor_item,
2279 if ((icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE ||
2280 ((icon_view->priv->selection_mode == GTK_SELECTION_SINGLE) && item->selected)) &&
2281 (event->state & modify_mod_mask))
2283 item->selected = !item->selected;
2284 gtk_icon_view_queue_draw_item (icon_view, item);
2289 gtk_icon_view_unselect_all_internal (icon_view);
2291 item->selected = TRUE;
2292 gtk_icon_view_queue_draw_item (icon_view, item);
2295 _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2296 icon_view->priv->anchor_item = item;
2299 /* Save press to possibly begin a drag */
2300 if (icon_view->priv->pressed_button < 0)
2302 icon_view->priv->pressed_button = event->button;
2303 icon_view->priv->press_start_x = event->x;
2304 icon_view->priv->press_start_y = event->y;
2307 if (!icon_view->priv->last_single_clicked)
2308 icon_view->priv->last_single_clicked = item;
2310 /* cancel the current editing, if it exists */
2311 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2313 if (cell != NULL && gtk_cell_renderer_is_activatable (cell))
2315 GtkCellAreaContext *context;
2317 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2319 _gtk_icon_view_set_cell_data (icon_view, item);
2320 gtk_cell_area_activate (icon_view->priv->cell_area, context,
2321 GTK_WIDGET (icon_view),
2322 &item->cell_area, 0/* XXX flags */, FALSE);
2327 if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
2328 !(event->state & modify_mod_mask))
2330 dirty = gtk_icon_view_unselect_all_internal (icon_view);
2333 if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
2334 gtk_icon_view_start_rubberbanding (icon_view, event->device, event->x, event->y);
2337 /* don't draw keyboard focus around an clicked-on item */
2338 icon_view->priv->draw_focus = FALSE;
2341 if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_2BUTTON_PRESS)
2343 item = _gtk_icon_view_get_item_at_coords (icon_view,
2348 if (item && item == icon_view->priv->last_single_clicked)
2352 path = gtk_tree_path_new_from_indices (item->index, -1);
2353 gtk_icon_view_item_activated (icon_view, path);
2354 gtk_tree_path_free (path);
2357 icon_view->priv->last_single_clicked = NULL;
2358 icon_view->priv->pressed_button = -1;
2362 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2364 return event->button == GDK_BUTTON_PRIMARY;
2368 gtk_icon_view_button_release (GtkWidget *widget,
2369 GdkEventButton *event)
2371 GtkIconView *icon_view;
2373 icon_view = GTK_ICON_VIEW (widget);
2375 if (icon_view->priv->pressed_button == event->button)
2376 icon_view->priv->pressed_button = -1;
2378 gtk_icon_view_stop_rubberbanding (icon_view);
2380 remove_scroll_timeout (icon_view);
2386 gtk_icon_view_key_press (GtkWidget *widget,
2389 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2391 if (icon_view->priv->doing_rubberband)
2393 if (event->keyval == GDK_KEY_Escape)
2394 gtk_icon_view_stop_rubberbanding (icon_view);
2399 return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2403 gtk_icon_view_key_release (GtkWidget *widget,
2406 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2408 if (icon_view->priv->doing_rubberband)
2411 return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2415 gtk_icon_view_update_rubberband (gpointer data)
2417 GtkIconView *icon_view;
2419 GdkRectangle old_area;
2420 GdkRectangle new_area;
2421 GdkRectangle common;
2422 cairo_region_t *invalid_region;
2424 icon_view = GTK_ICON_VIEW (data);
2426 gdk_window_get_device_position (icon_view->priv->bin_window,
2427 icon_view->priv->rubberband_device,
2433 old_area.x = MIN (icon_view->priv->rubberband_x1,
2434 icon_view->priv->rubberband_x2);
2435 old_area.y = MIN (icon_view->priv->rubberband_y1,
2436 icon_view->priv->rubberband_y2);
2437 old_area.width = ABS (icon_view->priv->rubberband_x2 -
2438 icon_view->priv->rubberband_x1) + 1;
2439 old_area.height = ABS (icon_view->priv->rubberband_y2 -
2440 icon_view->priv->rubberband_y1) + 1;
2442 new_area.x = MIN (icon_view->priv->rubberband_x1, x);
2443 new_area.y = MIN (icon_view->priv->rubberband_y1, y);
2444 new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
2445 new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
2447 invalid_region = cairo_region_create_rectangle (&old_area);
2448 cairo_region_union_rectangle (invalid_region, &new_area);
2450 gdk_rectangle_intersect (&old_area, &new_area, &common);
2451 if (common.width > 2 && common.height > 2)
2453 cairo_region_t *common_region;
2455 /* make sure the border is invalidated */
2461 common_region = cairo_region_create_rectangle (&common);
2463 cairo_region_subtract (invalid_region, common_region);
2464 cairo_region_destroy (common_region);
2467 gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
2469 cairo_region_destroy (invalid_region);
2471 icon_view->priv->rubberband_x2 = x;
2472 icon_view->priv->rubberband_y2 = y;
2474 gtk_icon_view_update_rubberband_selection (icon_view);
2478 gtk_icon_view_start_rubberbanding (GtkIconView *icon_view,
2485 if (icon_view->priv->rubberband_device)
2488 for (items = icon_view->priv->items; items; items = items->next)
2490 GtkIconViewItem *item = items->data;
2492 item->selected_before_rubberbanding = item->selected;
2495 icon_view->priv->rubberband_x1 = x;
2496 icon_view->priv->rubberband_y1 = y;
2497 icon_view->priv->rubberband_x2 = x;
2498 icon_view->priv->rubberband_y2 = y;
2500 icon_view->priv->doing_rubberband = TRUE;
2501 icon_view->priv->rubberband_device = device;
2503 gtk_device_grab_add (GTK_WIDGET (icon_view), device, TRUE);
2507 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
2509 if (!icon_view->priv->doing_rubberband)
2512 gtk_device_grab_remove (GTK_WIDGET (icon_view),
2513 icon_view->priv->rubberband_device);
2515 icon_view->priv->doing_rubberband = FALSE;
2516 icon_view->priv->rubberband_device = NULL;
2518 gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2522 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
2525 gint x, y, width, height;
2526 gboolean dirty = FALSE;
2528 x = MIN (icon_view->priv->rubberband_x1,
2529 icon_view->priv->rubberband_x2);
2530 y = MIN (icon_view->priv->rubberband_y1,
2531 icon_view->priv->rubberband_y2);
2532 width = ABS (icon_view->priv->rubberband_x1 -
2533 icon_view->priv->rubberband_x2);
2534 height = ABS (icon_view->priv->rubberband_y1 -
2535 icon_view->priv->rubberband_y2);
2537 for (items = icon_view->priv->items; items; items = items->next)
2539 GtkIconViewItem *item = items->data;
2543 is_in = gtk_icon_view_item_hit_test (icon_view, item,
2544 x, y, width, height);
2546 selected = is_in ^ item->selected_before_rubberbanding;
2548 if (item->selected != selected)
2550 item->selected = selected;
2552 gtk_icon_view_queue_draw_item (icon_view, item);
2557 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2562 GdkRectangle hit_rect;
2567 hit_test (GtkCellRenderer *renderer,
2568 const GdkRectangle *cell_area,
2569 const GdkRectangle *cell_background,
2572 if (MIN (data->hit_rect.x + data->hit_rect.width, cell_area->x + cell_area->width) -
2573 MAX (data->hit_rect.x, cell_area->x) > 0 &&
2574 MIN (data->hit_rect.y + data->hit_rect.height, cell_area->y + cell_area->height) -
2575 MAX (data->hit_rect.y, cell_area->y) > 0)
2578 return (data->hit != FALSE);
2582 gtk_icon_view_item_hit_test (GtkIconView *icon_view,
2583 GtkIconViewItem *item,
2589 HitTestData data = { { x, y, width, height }, FALSE };
2590 GtkCellAreaContext *context;
2591 GdkRectangle *item_area = &item->cell_area;
2593 if (MIN (x + width, item_area->x + item_area->width) - MAX (x, item_area->x) <= 0 ||
2594 MIN (y + height, item_area->y + item_area->height) - MAX (y, item_area->y) <= 0)
2597 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2599 _gtk_icon_view_set_cell_data (icon_view, item);
2600 gtk_cell_area_foreach_alloc (icon_view->priv->cell_area, context,
2601 GTK_WIDGET (icon_view),
2602 item_area, item_area,
2603 (GtkCellAllocCallback)hit_test, &data);
2609 gtk_icon_view_unselect_all_internal (GtkIconView *icon_view)
2611 gboolean dirty = FALSE;
2614 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2617 for (items = icon_view->priv->items; items; items = items->next)
2619 GtkIconViewItem *item = items->data;
2623 item->selected = FALSE;
2625 gtk_icon_view_queue_draw_item (icon_view, item);
2626 gtk_icon_view_item_selected_changed (icon_view, item);
2634 /* GtkIconView signals */
2636 gtk_icon_view_real_select_all (GtkIconView *icon_view)
2638 gtk_icon_view_select_all (icon_view);
2642 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
2644 gtk_icon_view_unselect_all (icon_view);
2648 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
2650 gtk_icon_view_unselect_all (icon_view);
2652 if (icon_view->priv->cursor_item != NULL)
2653 _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2657 gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view)
2660 GtkCellAreaContext *context;
2662 if (!icon_view->priv->cursor_item)
2665 context = g_ptr_array_index (icon_view->priv->row_contexts, icon_view->priv->cursor_item->row);
2667 _gtk_icon_view_set_cell_data (icon_view, icon_view->priv->cursor_item);
2668 gtk_cell_area_activate (icon_view->priv->cell_area, context,
2669 GTK_WIDGET (icon_view),
2670 &icon_view->priv->cursor_item->cell_area,
2674 path = gtk_tree_path_new_from_indices (icon_view->priv->cursor_item->index, -1);
2675 gtk_icon_view_item_activated (icon_view, path);
2676 gtk_tree_path_free (path);
2682 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
2684 if (!icon_view->priv->cursor_item)
2687 switch (icon_view->priv->selection_mode)
2689 case GTK_SELECTION_NONE:
2691 case GTK_SELECTION_BROWSE:
2692 _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2694 case GTK_SELECTION_SINGLE:
2695 if (icon_view->priv->cursor_item->selected)
2696 _gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
2698 _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2700 case GTK_SELECTION_MULTIPLE:
2701 icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
2702 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2704 gtk_icon_view_item_selected_changed (icon_view, icon_view->priv->cursor_item);
2705 gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2711 gtk_icon_view_set_hadjustment_values (GtkIconView *icon_view)
2713 GtkAllocation allocation;
2714 GtkAdjustment *adj = icon_view->priv->hadjustment;
2715 gdouble old_page_size;
2721 gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2723 old_value = gtk_adjustment_get_value (adj);
2724 old_upper = gtk_adjustment_get_upper (adj);
2725 old_page_size = gtk_adjustment_get_page_size (adj);
2726 new_upper = MAX (allocation.width, icon_view->priv->width);
2728 if (gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL)
2730 /* Make sure no scrolling occurs for RTL locales also (if possible) */
2731 /* Quick explanation:
2732 * In LTR locales, leftmost portion of visible rectangle should stay
2733 * fixed, which means left edge of scrollbar thumb should remain fixed
2734 * and thus adjustment's value should stay the same.
2736 * In RTL locales, we want to keep rightmost portion of visible
2737 * rectangle fixed. This means right edge of thumb should remain fixed.
2738 * In this case, upper - value - page_size should remain constant.
2740 new_value = (new_upper - allocation.width) -
2741 (old_upper - old_value - old_page_size);
2742 new_value = CLAMP (new_value, 0, new_upper - allocation.width);
2745 new_value = CLAMP (old_value, 0, new_upper - allocation.width);
2747 gtk_adjustment_configure (adj,
2751 allocation.width * 0.1,
2752 allocation.width * 0.9,
2757 gtk_icon_view_set_vadjustment_values (GtkIconView *icon_view)
2759 GtkAllocation allocation;
2760 GtkAdjustment *adj = icon_view->priv->vadjustment;
2762 gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2764 gtk_adjustment_configure (adj,
2765 gtk_adjustment_get_value (adj),
2767 MAX (allocation.height, icon_view->priv->height),
2768 allocation.height * 0.1,
2769 allocation.height * 0.9,
2774 gtk_icon_view_set_hadjustment (GtkIconView *icon_view,
2775 GtkAdjustment *adjustment)
2777 GtkIconViewPrivate *priv = icon_view->priv;
2779 if (adjustment && priv->hadjustment == adjustment)
2782 if (priv->hadjustment != NULL)
2784 g_signal_handlers_disconnect_matched (priv->hadjustment,
2785 G_SIGNAL_MATCH_DATA,
2786 0, 0, NULL, NULL, icon_view);
2787 g_object_unref (priv->hadjustment);
2791 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2794 g_signal_connect (adjustment, "value-changed",
2795 G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2796 priv->hadjustment = g_object_ref_sink (adjustment);
2797 gtk_icon_view_set_hadjustment_values (icon_view);
2799 g_object_notify (G_OBJECT (icon_view), "hadjustment");
2803 gtk_icon_view_set_vadjustment (GtkIconView *icon_view,
2804 GtkAdjustment *adjustment)
2806 GtkIconViewPrivate *priv = icon_view->priv;
2808 if (adjustment && priv->vadjustment == adjustment)
2811 if (priv->vadjustment != NULL)
2813 g_signal_handlers_disconnect_matched (priv->vadjustment,
2814 G_SIGNAL_MATCH_DATA,
2815 0, 0, NULL, NULL, icon_view);
2816 g_object_unref (priv->vadjustment);
2820 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2823 g_signal_connect (adjustment, "value-changed",
2824 G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2825 priv->vadjustment = g_object_ref_sink (adjustment);
2826 gtk_icon_view_set_vadjustment_values (icon_view);
2828 g_object_notify (G_OBJECT (icon_view), "vadjustment");
2832 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
2833 GtkIconView *icon_view)
2835 GtkIconViewPrivate *priv = icon_view->priv;
2837 if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2839 gdk_window_move (priv->bin_window,
2840 - gtk_adjustment_get_value (priv->hadjustment),
2841 - gtk_adjustment_get_value (priv->vadjustment));
2843 if (icon_view->priv->doing_rubberband)
2844 gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
2846 _gtk_icon_view_accessible_adjustment_changed (icon_view);
2851 compare_sizes (gconstpointer p1,
2855 return GPOINTER_TO_INT (((const GtkRequestedSize *) p1)->data)
2856 - GPOINTER_TO_INT (((const GtkRequestedSize *) p2)->data);
2860 gtk_icon_view_layout (GtkIconView *icon_view)
2862 GtkIconViewPrivate *priv = icon_view->priv;
2863 GtkWidget *widget = GTK_WIDGET (icon_view);
2865 gint item_width; /* this doesn't include item_padding */
2866 gint n_columns, n_rows, n_items;
2868 GtkRequestedSize *sizes;
2871 rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2874 n_items = gtk_icon_view_get_n_items (icon_view);
2876 gtk_icon_view_compute_n_items_for_size (icon_view,
2877 GTK_ORIENTATION_HORIZONTAL,
2878 gtk_widget_get_allocated_width (widget),
2880 &n_columns, &item_width);
2881 n_rows = (n_items + n_columns - 1) / n_columns;
2883 priv->width = n_columns * (item_width + 2 * priv->item_padding + priv->column_spacing) - priv->column_spacing;
2884 priv->width += 2 * priv->margin;
2885 priv->width = MAX (priv->width, gtk_widget_get_allocated_width (widget));
2887 /* Clear the per row contexts */
2888 g_ptr_array_set_size (icon_view->priv->row_contexts, 0);
2890 gtk_cell_area_context_reset (priv->cell_area_context);
2891 /* because layouting is complicated. We designed an API
2892 * that is O(N²) and nonsensical.
2893 * And we're proud of it. */
2894 for (items = priv->items; items; items = items->next)
2896 _gtk_icon_view_set_cell_data (icon_view, items->data);
2897 gtk_cell_area_get_preferred_width (priv->cell_area,
2898 priv->cell_area_context,
2903 sizes = g_newa (GtkRequestedSize, n_rows);
2904 items = priv->items;
2905 priv->height = priv->margin;
2907 /* Collect the heights for all rows */
2908 for (row = 0; row < n_rows; row++)
2910 GtkCellAreaContext *context = gtk_cell_area_copy_context (priv->cell_area, priv->cell_area_context);
2911 g_ptr_array_add (priv->row_contexts, context);
2913 for (col = 0; col < n_columns && items; col++, items = items->next)
2915 GtkIconViewItem *item = items->data;
2917 _gtk_icon_view_set_cell_data (icon_view, item);
2918 gtk_cell_area_get_preferred_height_for_width (priv->cell_area,
2925 sizes[row].data = GINT_TO_POINTER (row);
2926 gtk_cell_area_context_get_preferred_height_for_width (context,
2928 &sizes[row].minimum_size,
2929 &sizes[row].natural_size);
2930 priv->height += sizes[row].minimum_size + 2 * priv->item_padding + priv->row_spacing;
2933 priv->height -= priv->row_spacing;
2934 priv->height += priv->margin;
2935 priv->height = MIN (priv->height, gtk_widget_get_allocated_height (widget));
2937 gtk_distribute_natural_allocation (gtk_widget_get_allocated_height (widget) - priv->height,
2941 /* Actually allocate the rows */
2942 g_qsort_with_data (sizes, n_rows, sizeof (GtkRequestedSize), compare_sizes, NULL);
2944 items = priv->items;
2945 priv->height = priv->margin;
2947 for (row = 0; row < n_rows; row++)
2949 GtkCellAreaContext *context = g_ptr_array_index (priv->row_contexts, row);
2950 gtk_cell_area_context_allocate (context, item_width, sizes[row].minimum_size);
2952 priv->height += priv->item_padding;
2954 for (col = 0; col < n_columns && items; col++, items = items->next)
2956 GtkIconViewItem *item = items->data;
2958 item->cell_area.x = priv->margin + (col * 2 + 1) * priv->item_padding + col * (priv->column_spacing + item_width);
2959 item->cell_area.width = item_width;
2960 item->cell_area.y = priv->height;
2961 item->cell_area.height = sizes[row].minimum_size;
2966 item->cell_area.x = priv->width - item_width - item->cell_area.x;
2967 item->col = n_columns - 1 - col;
2971 priv->height += sizes[row].minimum_size + priv->item_padding + priv->row_spacing;
2974 priv->height -= priv->row_spacing;
2975 priv->height += priv->margin;
2976 priv->height = MAX (priv->height, gtk_widget_get_allocated_height (widget));
2980 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
2982 /* Clear all item sizes */
2983 g_list_foreach (icon_view->priv->items,
2984 (GFunc)gtk_icon_view_item_invalidate_size, NULL);
2986 /* Re-layout the items */
2987 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
2991 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
2993 item->cell_area.width = -1;
2994 item->cell_area.height = -1;
2998 gtk_icon_view_paint_item (GtkIconView *icon_view,
3000 GtkIconViewItem *item,
3003 gboolean draw_focus)
3005 GdkRectangle cell_area;
3006 GtkStateFlags state = 0;
3007 GtkCellRendererState flags = 0;
3008 GtkStyleContext *style_context;
3009 GtkWidget *widget = GTK_WIDGET (icon_view);
3010 GtkIconViewPrivate *priv = icon_view->priv;
3011 GtkCellAreaContext *context;
3013 if (priv->model == NULL || item->cell_area.width <= 0 || item->cell_area.height <= 0)
3016 _gtk_icon_view_set_cell_data (icon_view, item);
3018 style_context = gtk_widget_get_style_context (widget);
3019 state = gtk_widget_get_state_flags (widget);
3021 gtk_style_context_save (style_context);
3022 gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_VIEW);
3023 gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_CELL);
3025 state &= ~(GTK_STATE_FLAG_SELECTED | GTK_STATE_FLAG_PRELIGHT);
3029 if ((state & GTK_STATE_FLAG_FOCUSED) &&
3030 item == icon_view->priv->cursor_item)
3032 flags |= GTK_CELL_RENDERER_FOCUSED;
3035 state |= GTK_STATE_FLAG_SELECTED;
3036 flags |= GTK_CELL_RENDERER_SELECTED;
3041 state |= GTK_STATE_FLAG_PRELIGHT;
3042 flags |= GTK_CELL_RENDERER_PRELIT;
3045 gtk_style_context_set_state (style_context, state);
3049 gtk_render_background (style_context, cr,
3050 x - icon_view->priv->item_padding,
3051 y - icon_view->priv->item_padding,
3052 item->cell_area.width + icon_view->priv->item_padding * 2,
3053 item->cell_area.height + icon_view->priv->item_padding * 2);
3054 gtk_render_frame (style_context, cr,
3055 x - icon_view->priv->item_padding,
3056 y - icon_view->priv->item_padding,
3057 item->cell_area.width + icon_view->priv->item_padding * 2,
3058 item->cell_area.height + icon_view->priv->item_padding * 2);
3063 cell_area.width = item->cell_area.width;
3064 cell_area.height = item->cell_area.height;
3066 context = g_ptr_array_index (priv->row_contexts, item->row);
3067 gtk_cell_area_render (priv->cell_area, context,
3068 widget, cr, &cell_area, &cell_area, flags,
3071 gtk_style_context_restore (style_context);
3075 gtk_icon_view_paint_rubberband (GtkIconView *icon_view,
3078 GtkStyleContext *context;
3083 rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
3084 rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
3085 rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
3086 rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
3088 context = gtk_widget_get_style_context (GTK_WIDGET (icon_view));
3090 gtk_style_context_save (context);
3091 gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
3093 gdk_cairo_rectangle (cr, &rect);
3096 gtk_render_background (context, cr,
3098 rect.width, rect.height);
3099 gtk_render_frame (context, cr,
3101 rect.width, rect.height);
3103 gtk_style_context_restore (context);
3108 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
3114 index = gtk_tree_path_get_indices (path)[0];
3116 for (l = icon_view->priv->items; l; l = l->next)
3118 GtkIconViewItem *item = l->data;
3120 if (item->index == index)
3122 gtk_icon_view_queue_draw_item (icon_view, item);
3129 gtk_icon_view_queue_draw_item (GtkIconView *icon_view,
3130 GtkIconViewItem *item)
3133 GdkRectangle *item_area = &item->cell_area;
3135 rect.x = item_area->x - icon_view->priv->item_padding;
3136 rect.y = item_area->y - icon_view->priv->item_padding;
3137 rect.width = item_area->width + icon_view->priv->item_padding * 2;
3138 rect.height = item_area->height + icon_view->priv->item_padding * 2;
3140 if (icon_view->priv->bin_window)
3141 gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
3145 _gtk_icon_view_set_cursor_item (GtkIconView *icon_view,
3146 GtkIconViewItem *item,
3147 GtkCellRenderer *cursor_cell)
3150 AtkObject *item_obj;
3151 AtkObject *cursor_item_obj;
3153 /* When hitting this path from keynav, the focus cell is
3154 * already set, we dont need to notify the atk object
3155 * but we still need to queue the draw here (in the case
3156 * that the focus cell changes but not the cursor item).
3158 gtk_icon_view_queue_draw_item (icon_view, item);
3160 if (icon_view->priv->cursor_item == item &&
3161 (cursor_cell == NULL || cursor_cell == gtk_cell_area_get_focus_cell (icon_view->priv->cell_area)))
3164 obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3165 if (icon_view->priv->cursor_item != NULL)
3167 gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3170 cursor_item_obj = atk_object_ref_accessible_child (obj, icon_view->priv->cursor_item->index);
3171 if (cursor_item_obj != NULL)
3172 atk_object_notify_state_change (cursor_item_obj, ATK_STATE_FOCUSED, FALSE);
3175 icon_view->priv->cursor_item = item;
3178 gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cursor_cell);
3181 /* Make sure there is a cell in focus initially */
3182 if (!gtk_cell_area_get_focus_cell (icon_view->priv->cell_area))
3183 gtk_cell_area_focus (icon_view->priv->cell_area, GTK_DIR_TAB_FORWARD);
3186 /* Notify that accessible focus object has changed */
3187 item_obj = atk_object_ref_accessible_child (obj, item->index);
3189 if (item_obj != NULL)
3191 atk_focus_tracker_notify (item_obj);
3192 atk_object_notify_state_change (item_obj, ATK_STATE_FOCUSED, TRUE);
3193 g_object_unref (item_obj);
3198 static GtkIconViewItem *
3199 gtk_icon_view_item_new (void)
3201 GtkIconViewItem *item;
3203 item = g_slice_new0 (GtkIconViewItem);
3205 item->cell_area.width = -1;
3206 item->cell_area.height = -1;
3212 gtk_icon_view_item_free (GtkIconViewItem *item)
3214 g_return_if_fail (item != NULL);
3216 g_slice_free (GtkIconViewItem, item);
3220 _gtk_icon_view_get_item_at_coords (GtkIconView *icon_view,
3223 gboolean only_in_cell,
3224 GtkCellRenderer **cell_at_pos)
3229 *cell_at_pos = NULL;
3231 for (items = icon_view->priv->items; items; items = items->next)
3233 GtkIconViewItem *item = items->data;
3234 GdkRectangle *item_area = &item->cell_area;
3236 if (x >= item_area->x - icon_view->priv->column_spacing/2 &&
3237 x <= item_area->x + item_area->width + icon_view->priv->column_spacing/2 &&
3238 y >= item_area->y - icon_view->priv->row_spacing/2 &&
3239 y <= item_area->y + item_area->height + icon_view->priv->row_spacing/2)
3241 if (only_in_cell || cell_at_pos)
3243 GtkCellRenderer *cell = NULL;
3244 GtkCellAreaContext *context;
3246 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
3247 _gtk_icon_view_set_cell_data (icon_view, item);
3249 if (x >= item_area->x && x <= item_area->x + item_area->width &&
3250 y >= item_area->y && y <= item_area->y + item_area->height)
3251 cell = gtk_cell_area_get_cell_at_position (icon_view->priv->cell_area, context,
3252 GTK_WIDGET (icon_view),
3257 *cell_at_pos = cell;
3260 return cell != NULL ? item : NULL;
3271 _gtk_icon_view_select_item (GtkIconView *icon_view,
3272 GtkIconViewItem *item)
3274 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3275 g_return_if_fail (item != NULL);
3280 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3282 else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3283 gtk_icon_view_unselect_all_internal (icon_view);
3285 item->selected = TRUE;
3287 gtk_icon_view_item_selected_changed (icon_view, item);
3288 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3290 gtk_icon_view_queue_draw_item (icon_view, item);
3295 _gtk_icon_view_unselect_item (GtkIconView *icon_view,
3296 GtkIconViewItem *item)
3298 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3299 g_return_if_fail (item != NULL);
3301 if (!item->selected)
3304 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3305 icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3308 item->selected = FALSE;
3310 gtk_icon_view_item_selected_changed (icon_view, item);
3311 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3313 gtk_icon_view_queue_draw_item (icon_view, item);
3317 verify_items (GtkIconView *icon_view)
3322 for (items = icon_view->priv->items; items; items = items->next)
3324 GtkIconViewItem *item = items->data;
3326 if (item->index != i)
3327 g_error ("List item does not match its index: "
3328 "item index %d and list index %d\n", item->index, i);
3335 gtk_icon_view_row_changed (GtkTreeModel *model,
3340 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3342 /* ignore changes in branches */
3343 if (gtk_tree_path_get_depth (path) > 1)
3346 /* An icon view subclass might add it's own model and populate
3347 * things at init() time instead of waiting for the constructor()
3350 if (icon_view->priv->cell_area)
3351 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3353 /* Here we can use a "grow-only" strategy for optimization
3354 * and only invalidate a single item and queue a relayout
3355 * instead of invalidating the whole thing.
3357 * For now GtkIconView still cant deal with huge models
3358 * so just invalidate the whole thing when the model
3361 gtk_icon_view_invalidate_sizes (icon_view);
3363 verify_items (icon_view);
3367 gtk_icon_view_row_inserted (GtkTreeModel *model,
3372 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3374 GtkIconViewItem *item;
3377 /* ignore changes in branches */
3378 if (gtk_tree_path_get_depth (path) > 1)
3381 index = gtk_tree_path_get_indices(path)[0];
3383 item = gtk_icon_view_item_new ();
3385 item->index = index;
3387 /* FIXME: We can be more efficient here,
3388 we can store a tail pointer and use that when
3389 appending (which is a rather common operation)
3391 icon_view->priv->items = g_list_insert (icon_view->priv->items,
3394 list = g_list_nth (icon_view->priv->items, index + 1);
3395 for (; list; list = list->next)
3402 verify_items (icon_view);
3404 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3408 gtk_icon_view_row_deleted (GtkTreeModel *model,
3412 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3414 GtkIconViewItem *item;
3416 gboolean emit = FALSE;
3418 /* ignore changes in branches */
3419 if (gtk_tree_path_get_depth (path) > 1)
3422 index = gtk_tree_path_get_indices(path)[0];
3424 list = g_list_nth (icon_view->priv->items, index);
3427 if (icon_view->priv->cell_area)
3428 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3430 if (item == icon_view->priv->anchor_item)
3431 icon_view->priv->anchor_item = NULL;
3433 if (item == icon_view->priv->cursor_item)
3434 icon_view->priv->cursor_item = NULL;
3436 if (item == icon_view->priv->last_prelight)
3437 icon_view->priv->last_prelight = NULL;
3442 gtk_icon_view_item_free (item);
3444 for (next = list->next; next; next = next->next)
3451 icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3453 verify_items (icon_view);
3455 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3458 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3462 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3463 GtkTreePath *parent,
3468 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3471 GList *items = NULL, *list;
3472 GtkIconViewItem **item_array;
3475 /* ignore changes in branches */
3479 if (icon_view->priv->cell_area)
3480 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3482 length = gtk_tree_model_iter_n_children (model, NULL);
3484 order = g_new (gint, length);
3485 for (i = 0; i < length; i++)
3486 order [new_order[i]] = i;
3488 item_array = g_new (GtkIconViewItem *, length);
3489 for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3490 item_array[order[i]] = list->data;
3493 for (i = length - 1; i >= 0; i--)
3495 item_array[i]->index = i;
3496 items = g_list_prepend (items, item_array[i]);
3499 g_free (item_array);
3500 g_list_free (icon_view->priv->items);
3501 icon_view->priv->items = items;
3503 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3505 verify_items (icon_view);
3509 gtk_icon_view_build_items (GtkIconView *icon_view)
3513 GList *items = NULL;
3515 if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3523 GtkIconViewItem *item = gtk_icon_view_item_new ();
3529 items = g_list_prepend (items, item);
3531 } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3533 icon_view->priv->items = g_list_reverse (items);
3537 gtk_icon_view_add_move_binding (GtkBindingSet *binding_set,
3540 GtkMovementStep step,
3544 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3545 I_("move-cursor"), 2,
3549 gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3554 if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3557 gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3562 gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3569 gtk_icon_view_real_move_cursor (GtkIconView *icon_view,
3570 GtkMovementStep step,
3573 GdkModifierType state;
3575 g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3576 g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3577 step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3578 step == GTK_MOVEMENT_DISPLAY_LINES ||
3579 step == GTK_MOVEMENT_PAGES ||
3580 step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3582 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3585 gtk_cell_area_stop_editing (icon_view->priv->cell_area, FALSE);
3586 gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3588 if (gtk_get_current_event_state (&state))
3590 GdkModifierType extend_mod_mask;
3591 GdkModifierType modify_mod_mask;
3594 gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3595 GDK_MODIFIER_INTENT_EXTEND_SELECTION);
3597 gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3598 GDK_MODIFIER_INTENT_MODIFY_SELECTION);
3600 if ((state & modify_mod_mask) == modify_mod_mask)
3601 icon_view->priv->modify_selection_pressed = TRUE;
3602 if ((state & extend_mod_mask) == extend_mod_mask)
3603 icon_view->priv->extend_selection_pressed = TRUE;
3605 /* else we assume not pressed */
3609 case GTK_MOVEMENT_LOGICAL_POSITIONS:
3610 case GTK_MOVEMENT_VISUAL_POSITIONS:
3611 gtk_icon_view_move_cursor_left_right (icon_view, count);
3613 case GTK_MOVEMENT_DISPLAY_LINES:
3614 gtk_icon_view_move_cursor_up_down (icon_view, count);
3616 case GTK_MOVEMENT_PAGES:
3617 gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3619 case GTK_MOVEMENT_BUFFER_ENDS:
3620 gtk_icon_view_move_cursor_start_end (icon_view, count);
3623 g_assert_not_reached ();
3626 icon_view->priv->modify_selection_pressed = FALSE;
3627 icon_view->priv->extend_selection_pressed = FALSE;
3629 icon_view->priv->draw_focus = TRUE;
3634 static GtkIconViewItem *
3635 find_item (GtkIconView *icon_view,
3636 GtkIconViewItem *current,
3642 GtkIconViewItem *item;
3644 /* FIXME: this could be more efficient
3646 row = current->row + row_ofs;
3647 col = current->col + col_ofs;
3649 for (items = icon_view->priv->items; items; items = items->next)
3652 if (item->row == row && item->col == col)
3659 static GtkIconViewItem *
3660 find_item_page_up_down (GtkIconView *icon_view,
3661 GtkIconViewItem *current,
3668 y = current->cell_area.y + count * gtk_adjustment_get_page_size (icon_view->priv->vadjustment);
3670 item = g_list_find (icon_view->priv->items, current);
3675 for (next = item->next; next; next = next->next)
3677 if (((GtkIconViewItem *)next->data)->col == col)
3680 if (!next || ((GtkIconViewItem *)next->data)->cell_area.y > y)
3690 for (next = item->prev; next; next = next->prev)
3692 if (((GtkIconViewItem *)next->data)->col == col)
3695 if (!next || ((GtkIconViewItem *)next->data)->cell_area.y < y)
3709 gtk_icon_view_select_all_between (GtkIconView *icon_view,
3710 GtkIconViewItem *anchor,
3711 GtkIconViewItem *cursor)
3714 GtkIconViewItem *item;
3715 gint row1, row2, col1, col2;
3716 gboolean dirty = FALSE;
3718 if (anchor->row < cursor->row)
3729 if (anchor->col < cursor->col)
3740 for (items = icon_view->priv->items; items; items = items->next)
3744 if (row1 <= item->row && item->row <= row2 &&
3745 col1 <= item->col && item->col <= col2)
3747 if (!item->selected)
3750 item->selected = TRUE;
3751 gtk_icon_view_item_selected_changed (icon_view, item);
3753 gtk_icon_view_queue_draw_item (icon_view, item);
3761 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3764 GtkIconViewItem *item;
3765 GtkCellRenderer *cell = NULL;
3766 gboolean dirty = FALSE;
3768 GtkDirectionType direction;
3770 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3773 direction = count < 0 ? GTK_DIR_UP : GTK_DIR_DOWN;
3775 if (!icon_view->priv->cursor_item)
3780 list = icon_view->priv->items;
3782 list = g_list_last (icon_view->priv->items);
3788 /* Give focus to the first cell initially */
3789 _gtk_icon_view_set_cell_data (icon_view, item);
3790 gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3799 item = icon_view->priv->cursor_item;
3800 step = count > 0 ? 1 : -1;
3802 /* Save the current focus cell in case we hit the edge */
3803 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3807 _gtk_icon_view_set_cell_data (icon_view, item);
3809 if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3812 item = find_item (icon_view, item, step, 0);
3818 if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3820 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3822 gtk_widget_child_focus (toplevel,
3823 direction == GTK_DIR_UP ?
3824 GTK_DIR_TAB_BACKWARD :
3825 GTK_DIR_TAB_FORWARD);
3829 gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3833 if (icon_view->priv->modify_selection_pressed ||
3834 !icon_view->priv->extend_selection_pressed ||
3835 !icon_view->priv->anchor_item ||
3836 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3837 icon_view->priv->anchor_item = item;
3839 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3840 _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3842 if (!icon_view->priv->modify_selection_pressed &&
3843 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3845 dirty = gtk_icon_view_unselect_all_internal (icon_view);
3846 dirty = gtk_icon_view_select_all_between (icon_view,
3847 icon_view->priv->anchor_item,
3851 gtk_icon_view_scroll_to_item (icon_view, item);
3854 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3858 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3861 GtkIconViewItem *item;
3862 gboolean dirty = FALSE;
3864 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3867 if (!icon_view->priv->cursor_item)
3872 list = icon_view->priv->items;
3874 list = g_list_last (icon_view->priv->items);
3876 item = list ? list->data : NULL;
3879 item = find_item_page_up_down (icon_view,
3880 icon_view->priv->cursor_item,
3883 if (item == icon_view->priv->cursor_item)
3884 gtk_widget_error_bell (GTK_WIDGET (icon_view));
3889 if (icon_view->priv->modify_selection_pressed ||
3890 !icon_view->priv->extend_selection_pressed ||
3891 !icon_view->priv->anchor_item ||
3892 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3893 icon_view->priv->anchor_item = item;
3895 _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
3897 if (!icon_view->priv->modify_selection_pressed &&
3898 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3900 dirty = gtk_icon_view_unselect_all_internal (icon_view);
3901 dirty = gtk_icon_view_select_all_between (icon_view,
3902 icon_view->priv->anchor_item,
3906 gtk_icon_view_scroll_to_item (icon_view, item);
3909 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3913 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
3916 GtkIconViewItem *item;
3917 GtkCellRenderer *cell = NULL;
3918 gboolean dirty = FALSE;
3920 GtkDirectionType direction;
3922 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3925 direction = count < 0 ? GTK_DIR_LEFT : GTK_DIR_RIGHT;
3927 if (!icon_view->priv->cursor_item)
3932 list = icon_view->priv->items;
3934 list = g_list_last (icon_view->priv->items);
3940 /* Give focus to the first cell initially */
3941 _gtk_icon_view_set_cell_data (icon_view, item);
3942 gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3951 item = icon_view->priv->cursor_item;
3952 step = count > 0 ? 1 : -1;
3954 /* Save the current focus cell in case we hit the edge */
3955 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3959 _gtk_icon_view_set_cell_data (icon_view, item);
3961 if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3964 item = find_item (icon_view, item, 0, step);
3970 if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3972 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3974 gtk_widget_child_focus (toplevel,
3975 direction == GTK_DIR_LEFT ?
3976 GTK_DIR_TAB_BACKWARD :
3977 GTK_DIR_TAB_FORWARD);
3981 gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3985 if (icon_view->priv->modify_selection_pressed ||
3986 !icon_view->priv->extend_selection_pressed ||
3987 !icon_view->priv->anchor_item ||
3988 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3989 icon_view->priv->anchor_item = item;
3991 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3992 _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3994 if (!icon_view->priv->modify_selection_pressed &&
3995 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3997 dirty = gtk_icon_view_unselect_all_internal (icon_view);
3998 dirty = gtk_icon_view_select_all_between (icon_view,
3999 icon_view->priv->anchor_item,
4003 gtk_icon_view_scroll_to_item (icon_view, item);
4006 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4010 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
4013 GtkIconViewItem *item;
4015 gboolean dirty = FALSE;
4017 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
4021 list = icon_view->priv->items;
4023 list = g_list_last (icon_view->priv->items);
4025 item = list ? list->data : NULL;
4027 if (item == icon_view->priv->cursor_item)
4028 gtk_widget_error_bell (GTK_WIDGET (icon_view));
4033 if (icon_view->priv->modify_selection_pressed ||
4034 !icon_view->priv->extend_selection_pressed ||
4035 !icon_view->priv->anchor_item ||
4036 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4037 icon_view->priv->anchor_item = item;
4039 _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
4041 if (!icon_view->priv->modify_selection_pressed &&
4042 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4044 dirty = gtk_icon_view_unselect_all_internal (icon_view);
4045 dirty = gtk_icon_view_select_all_between (icon_view,
4046 icon_view->priv->anchor_item,
4050 gtk_icon_view_scroll_to_item (icon_view, item);
4053 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4057 * gtk_icon_view_scroll_to_path:
4058 * @icon_view: A #GtkIconView.
4059 * @path: The path of the item to move to.
4060 * @use_align: whether to use alignment arguments, or %FALSE.
4061 * @row_align: The vertical alignment of the item specified by @path.
4062 * @col_align: The horizontal alignment of the item specified by @path.
4064 * Moves the alignments of @icon_view to the position specified by @path.
4065 * @row_align determines where the row is placed, and @col_align determines
4066 * where @column is placed. Both are expected to be between 0.0 and 1.0.
4067 * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means
4070 * If @use_align is %FALSE, then the alignment arguments are ignored, and the
4071 * tree does the minimum amount of work to scroll the item onto the screen.
4072 * This means that the item will be scrolled to the edge closest to its current
4073 * position. If the item is currently visible on the screen, nothing is done.
4075 * This function only works if the model is set, and @path is a valid row on
4076 * the model. If the model changes before the @icon_view is realized, the
4077 * centered path will be modified to reflect this change.
4082 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
4088 GtkIconViewItem *item = NULL;
4091 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4092 g_return_if_fail (path != NULL);
4093 g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
4094 g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
4096 widget = GTK_WIDGET (icon_view);
4098 if (gtk_tree_path_get_depth (path) > 0)
4099 item = g_list_nth_data (icon_view->priv->items,
4100 gtk_tree_path_get_indices(path)[0]);
4102 if (!item || item->cell_area.width < 0 ||
4103 !gtk_widget_get_realized (widget))
4105 if (icon_view->priv->scroll_to_path)
4106 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4108 icon_view->priv->scroll_to_path = NULL;
4111 icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
4113 icon_view->priv->scroll_to_use_align = use_align;
4114 icon_view->priv->scroll_to_row_align = row_align;
4115 icon_view->priv->scroll_to_col_align = col_align;
4122 GtkAllocation allocation;
4125 GdkRectangle item_area =
4127 item->cell_area.x - icon_view->priv->item_padding,
4128 item->cell_area.y - icon_view->priv->item_padding,
4129 item->cell_area.width + icon_view->priv->item_padding * 2,
4130 item->cell_area.height + icon_view->priv->item_padding * 2
4133 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4135 gtk_widget_get_allocation (widget, &allocation);
4137 offset = y + item_area.y - row_align * (allocation.height - item_area.height);
4139 gtk_adjustment_set_value (icon_view->priv->vadjustment,
4140 gtk_adjustment_get_value (icon_view->priv->vadjustment) + offset);
4142 offset = x + item_area.x - col_align * (allocation.width - item_area.width);
4144 gtk_adjustment_set_value (icon_view->priv->hadjustment,
4145 gtk_adjustment_get_value (icon_view->priv->hadjustment) + offset);
4147 gtk_adjustment_changed (icon_view->priv->hadjustment);
4148 gtk_adjustment_changed (icon_view->priv->vadjustment);
4151 gtk_icon_view_scroll_to_item (icon_view, item);
4156 gtk_icon_view_scroll_to_item (GtkIconView *icon_view,
4157 GtkIconViewItem *item)
4159 GtkIconViewPrivate *priv = icon_view->priv;
4160 GtkWidget *widget = GTK_WIDGET (icon_view);
4161 GtkAdjustment *hadj, *vadj;
4162 GtkAllocation allocation;
4164 GdkRectangle item_area;
4166 item_area.x = item->cell_area.x - priv->item_padding;
4167 item_area.y = item->cell_area.y - priv->item_padding;
4168 item_area.width = item->cell_area.width + priv->item_padding * 2;
4169 item_area.height = item->cell_area.height + priv->item_padding * 2;
4171 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4172 gtk_widget_get_allocation (widget, &allocation);
4174 hadj = icon_view->priv->hadjustment;
4175 vadj = icon_view->priv->vadjustment;
4177 if (y + item_area.y < 0)
4178 gtk_adjustment_set_value (vadj,
4179 gtk_adjustment_get_value (vadj)
4181 else if (y + item_area.y + item_area.height > allocation.height)
4182 gtk_adjustment_set_value (vadj,
4183 gtk_adjustment_get_value (vadj)
4184 + y + item_area.y + item_area.height - allocation.height);
4186 if (x + item_area.x < 0)
4187 gtk_adjustment_set_value (hadj,
4188 gtk_adjustment_get_value (hadj)
4190 else if (x + item_area.x + item_area.width > allocation.width)
4191 gtk_adjustment_set_value (hadj,
4192 gtk_adjustment_get_value (hadj)
4193 + x + item_area.x + item_area.width - allocation.width);
4195 gtk_adjustment_changed (hadj);
4196 gtk_adjustment_changed (vadj);
4199 /* GtkCellLayout implementation */
4202 gtk_icon_view_ensure_cell_area (GtkIconView *icon_view,
4203 GtkCellArea *cell_area)
4205 GtkIconViewPrivate *priv = icon_view->priv;
4207 if (priv->cell_area)
4211 priv->cell_area = cell_area;
4213 priv->cell_area = gtk_cell_area_box_new ();
4215 g_object_ref_sink (priv->cell_area);
4217 if (GTK_IS_ORIENTABLE (priv->cell_area))
4218 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->cell_area), priv->item_orientation);
4220 priv->cell_area_context = gtk_cell_area_create_context (priv->cell_area);
4222 priv->add_editable_id =
4223 g_signal_connect (priv->cell_area, "add-editable",
4224 G_CALLBACK (gtk_icon_view_add_editable), icon_view);
4225 priv->remove_editable_id =
4226 g_signal_connect (priv->cell_area, "remove-editable",
4227 G_CALLBACK (gtk_icon_view_remove_editable), icon_view);
4229 update_text_cell (icon_view);
4230 update_pixbuf_cell (icon_view);
4233 static GtkCellArea *
4234 gtk_icon_view_cell_layout_get_area (GtkCellLayout *cell_layout)
4236 GtkIconView *icon_view = GTK_ICON_VIEW (cell_layout);
4237 GtkIconViewPrivate *priv = icon_view->priv;
4239 if (G_UNLIKELY (!priv->cell_area))
4240 gtk_icon_view_ensure_cell_area (icon_view, NULL);
4242 return icon_view->priv->cell_area;
4246 _gtk_icon_view_set_cell_data (GtkIconView *icon_view,
4247 GtkIconViewItem *item)
4252 path = gtk_tree_path_new_from_indices (item->index, -1);
4253 if (!gtk_tree_model_get_iter (icon_view->priv->model, &iter, path))
4255 gtk_tree_path_free (path);
4257 gtk_cell_area_apply_attributes (icon_view->priv->cell_area,
4258 icon_view->priv->model,
4259 &iter, FALSE, FALSE);
4268 * gtk_icon_view_new:
4270 * Creates a new #GtkIconView widget
4272 * Return value: A newly created #GtkIconView widget
4277 gtk_icon_view_new (void)
4279 return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4283 * gtk_icon_view_new_with_area:
4284 * @area: the #GtkCellArea to use to layout cells
4286 * Creates a new #GtkIconView widget using the
4287 * specified @area to layout cells inside the icons.
4289 * Return value: A newly created #GtkIconView widget
4294 gtk_icon_view_new_with_area (GtkCellArea *area)
4296 return g_object_new (GTK_TYPE_ICON_VIEW, "cell-area", area, NULL);
4300 * gtk_icon_view_new_with_model:
4301 * @model: The model.
4303 * Creates a new #GtkIconView widget with the model @model.
4305 * Return value: A newly created #GtkIconView widget.
4310 gtk_icon_view_new_with_model (GtkTreeModel *model)
4312 return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4316 * gtk_icon_view_convert_widget_to_bin_window_coords:
4317 * @icon_view: a #GtkIconView
4318 * @wx: X coordinate relative to the widget
4319 * @wy: Y coordinate relative to the widget
4320 * @bx: (out): return location for bin_window X coordinate
4321 * @by: (out): return location for bin_window Y coordinate
4323 * Converts widget coordinates to coordinates for the bin_window,
4324 * as expected by e.g. gtk_icon_view_get_path_at_pos().
4329 gtk_icon_view_convert_widget_to_bin_window_coords (GtkIconView *icon_view,
4337 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4339 if (icon_view->priv->bin_window)
4340 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4351 * gtk_icon_view_get_path_at_pos:
4352 * @icon_view: A #GtkIconView.
4353 * @x: The x position to be identified
4354 * @y: The y position to be identified
4356 * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4357 * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4358 * the cell at the specified position.
4359 * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4360 * widget coordinates to bin_window coordinates.
4362 * Return value: The #GtkTreePath corresponding to the icon or %NULL
4363 * if no icon exists at that position.
4368 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4372 GtkIconViewItem *item;
4375 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4377 item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4382 path = gtk_tree_path_new_from_indices (item->index, -1);
4388 * gtk_icon_view_get_item_at_pos:
4389 * @icon_view: A #GtkIconView.
4390 * @x: The x position to be identified
4391 * @y: The y position to be identified
4392 * @path: (out) (allow-none): Return location for the path, or %NULL
4393 * @cell: (out) (allow-none): Return location for the renderer
4394 * responsible for the cell at (@x, @y), or %NULL
4396 * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4397 * In contrast to gtk_icon_view_get_path_at_pos(), this function also
4398 * obtains the cell at the specified position. The returned path should
4399 * be freed with gtk_tree_path_free().
4400 * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4401 * widget coordinates to bin_window coordinates.
4403 * Return value: %TRUE if an item exists at the specified position
4408 gtk_icon_view_get_item_at_pos (GtkIconView *icon_view,
4412 GtkCellRenderer **cell)
4414 GtkIconViewItem *item;
4415 GtkCellRenderer *renderer = NULL;
4417 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4419 item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &renderer);
4424 *path = gtk_tree_path_new_from_indices (item->index, -1);
4432 return (item != NULL);
4436 * gtk_icon_view_set_tooltip_item:
4437 * @icon_view: a #GtkIconView
4438 * @tooltip: a #GtkTooltip
4439 * @path: a #GtkTreePath
4441 * Sets the tip area of @tooltip to be the area covered by the item at @path.
4442 * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4443 * See also gtk_tooltip_set_tip_area().
4448 gtk_icon_view_set_tooltip_item (GtkIconView *icon_view,
4449 GtkTooltip *tooltip,
4452 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4453 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4455 gtk_icon_view_set_tooltip_cell (icon_view, tooltip, path, NULL);
4459 * gtk_icon_view_set_tooltip_cell:
4460 * @icon_view: a #GtkIconView
4461 * @tooltip: a #GtkTooltip
4462 * @path: a #GtkTreePath
4463 * @cell: (allow-none): a #GtkCellRenderer or %NULL
4465 * Sets the tip area of @tooltip to the area which @cell occupies in
4466 * the item pointed to by @path. See also gtk_tooltip_set_tip_area().
4468 * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4473 gtk_icon_view_set_tooltip_cell (GtkIconView *icon_view,
4474 GtkTooltip *tooltip,
4476 GtkCellRenderer *cell)
4479 GtkIconViewItem *item = NULL;
4482 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4483 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4484 g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
4486 if (gtk_tree_path_get_depth (path) > 0)
4487 item = g_list_nth_data (icon_view->priv->items,
4488 gtk_tree_path_get_indices(path)[0]);
4495 GtkCellAreaContext *context;
4497 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
4498 _gtk_icon_view_set_cell_data (icon_view, item);
4499 gtk_cell_area_get_cell_allocation (icon_view->priv->cell_area, context,
4500 GTK_WIDGET (icon_view),
4501 cell, &item->cell_area, &rect);
4505 rect.x = item->cell_area.x - icon_view->priv->item_padding;
4506 rect.y = item->cell_area.y - icon_view->priv->item_padding;
4507 rect.width = item->cell_area.width + icon_view->priv->item_padding * 2;
4508 rect.height = item->cell_area.height + icon_view->priv->item_padding * 2;
4511 if (icon_view->priv->bin_window)
4513 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4518 gtk_tooltip_set_tip_area (tooltip, &rect);
4523 * gtk_icon_view_get_tooltip_context:
4524 * @icon_view: an #GtkIconView
4525 * @x: (inout): the x coordinate (relative to widget coordinates)
4526 * @y: (inout): the y coordinate (relative to widget coordinates)
4527 * @keyboard_tip: whether this is a keyboard tooltip or not
4528 * @model: (out) (allow-none): a pointer to receive a #GtkTreeModel or %NULL
4529 * @path: (out) (allow-none): a pointer to receive a #GtkTreePath or %NULL
4530 * @iter: (out) (allow-none): a pointer to receive a #GtkTreeIter or %NULL
4532 * This function is supposed to be used in a #GtkWidget::query-tooltip
4533 * signal handler for #GtkIconView. The @x, @y and @keyboard_tip values
4534 * which are received in the signal handler, should be passed to this
4535 * function without modification.
4537 * The return value indicates whether there is an icon view item at the given
4538 * coordinates (%TRUE) or not (%FALSE) for mouse tooltips. For keyboard
4539 * tooltips the item returned will be the cursor item. When %TRUE, then any of
4540 * @model, @path and @iter which have been provided will be set to point to
4541 * that row and the corresponding model. @x and @y will always be converted
4542 * to be relative to @icon_view's bin_window if @keyboard_tooltip is %FALSE.
4544 * Return value: whether or not the given tooltip context points to a item
4549 gtk_icon_view_get_tooltip_context (GtkIconView *icon_view,
4552 gboolean keyboard_tip,
4553 GtkTreeModel **model,
4557 GtkTreePath *tmppath = NULL;
4559 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4560 g_return_val_if_fail (x != NULL, FALSE);
4561 g_return_val_if_fail (y != NULL, FALSE);
4565 gtk_icon_view_get_cursor (icon_view, &tmppath, NULL);
4572 gtk_icon_view_convert_widget_to_bin_window_coords (icon_view, *x, *y,
4575 if (!gtk_icon_view_get_item_at_pos (icon_view, *x, *y, &tmppath, NULL))
4580 *model = gtk_icon_view_get_model (icon_view);
4583 gtk_tree_model_get_iter (gtk_icon_view_get_model (icon_view),
4589 gtk_tree_path_free (tmppath);
4595 gtk_icon_view_set_tooltip_query_cb (GtkWidget *widget,
4598 gboolean keyboard_tip,
4599 GtkTooltip *tooltip,
4605 GtkTreeModel *model;
4606 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
4608 if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget),
4611 &model, &path, &iter))
4614 gtk_tree_model_get (model, &iter, icon_view->priv->tooltip_column, &str, -1);
4618 gtk_tree_path_free (path);
4622 gtk_tooltip_set_markup (tooltip, str);
4623 gtk_icon_view_set_tooltip_item (icon_view, tooltip, path);
4625 gtk_tree_path_free (path);
4633 * gtk_icon_view_set_tooltip_column:
4634 * @icon_view: a #GtkIconView
4635 * @column: an integer, which is a valid column number for @icon_view's model
4637 * If you only plan to have simple (text-only) tooltips on full items, you
4638 * can use this function to have #GtkIconView handle these automatically
4639 * for you. @column should be set to the column in @icon_view's model
4640 * containing the tooltip texts, or -1 to disable this feature.
4642 * When enabled, #GtkWidget:has-tooltip will be set to %TRUE and
4643 * @icon_view will connect a #GtkWidget::query-tooltip signal handler.
4645 * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
4646 * so &, <, etc have to be escaped in the text.
4651 gtk_icon_view_set_tooltip_column (GtkIconView *icon_view,
4654 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4656 if (column == icon_view->priv->tooltip_column)
4661 g_signal_handlers_disconnect_by_func (icon_view,
4662 gtk_icon_view_set_tooltip_query_cb,
4664 gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), FALSE);
4668 if (icon_view->priv->tooltip_column == -1)
4670 g_signal_connect (icon_view, "query-tooltip",
4671 G_CALLBACK (gtk_icon_view_set_tooltip_query_cb), NULL);
4672 gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), TRUE);
4676 icon_view->priv->tooltip_column = column;
4677 g_object_notify (G_OBJECT (icon_view), "tooltip-column");
4681 * gtk_icon_view_get_tooltip_column:
4682 * @icon_view: a #GtkIconView
4684 * Returns the column of @icon_view's model which is being used for
4685 * displaying tooltips on @icon_view's rows.
4687 * Return value: the index of the tooltip column that is currently being
4688 * used, or -1 if this is disabled.
4693 gtk_icon_view_get_tooltip_column (GtkIconView *icon_view)
4695 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 0);
4697 return icon_view->priv->tooltip_column;
4701 * gtk_icon_view_get_visible_range:
4702 * @icon_view: A #GtkIconView
4703 * @start_path: (out) (allow-none): Return location for start of region,
4705 * @end_path: (out) (allow-none): Return location for end of region, or %NULL
4707 * Sets @start_path and @end_path to be the first and last visible path.
4708 * Note that there may be invisible paths in between.
4710 * Both paths should be freed with gtk_tree_path_free() after use.
4712 * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4717 gtk_icon_view_get_visible_range (GtkIconView *icon_view,
4718 GtkTreePath **start_path,
4719 GtkTreePath **end_path)
4721 gint start_index = -1;
4722 gint end_index = -1;
4725 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4727 if (icon_view->priv->hadjustment == NULL ||
4728 icon_view->priv->vadjustment == NULL)
4731 if (start_path == NULL && end_path == NULL)
4734 for (icons = icon_view->priv->items; icons; icons = icons->next)
4736 GtkIconViewItem *item = icons->data;
4737 GdkRectangle *item_area = &item->cell_area;
4739 if ((item_area->x + item_area->width >= (int)gtk_adjustment_get_value (icon_view->priv->hadjustment)) &&
4740 (item_area->y + item_area->height >= (int)gtk_adjustment_get_value (icon_view->priv->vadjustment)) &&
4742 (int) (gtk_adjustment_get_value (icon_view->priv->hadjustment) +
4743 gtk_adjustment_get_page_size (icon_view->priv->hadjustment))) &&
4745 (int) (gtk_adjustment_get_value (icon_view->priv->vadjustment) +
4746 gtk_adjustment_get_page_size (icon_view->priv->vadjustment))))
4748 if (start_index == -1)
4749 start_index = item->index;
4750 end_index = item->index;
4754 if (start_path && start_index != -1)
4755 *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4756 if (end_path && end_index != -1)
4757 *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4759 return start_index != -1;
4763 * gtk_icon_view_selected_foreach:
4764 * @icon_view: A #GtkIconView.
4765 * @func: (scope call): The function to call for each selected icon.
4766 * @data: User data to pass to the function.
4768 * Calls a function for each selected icon. Note that the model or
4769 * selection cannot be modified from within this function.
4774 gtk_icon_view_selected_foreach (GtkIconView *icon_view,
4775 GtkIconViewForeachFunc func,
4780 for (list = icon_view->priv->items; list; list = list->next)
4782 GtkIconViewItem *item = list->data;
4783 GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4786 (* func) (icon_view, path, data);
4788 gtk_tree_path_free (path);
4793 * gtk_icon_view_set_selection_mode:
4794 * @icon_view: A #GtkIconView.
4795 * @mode: The selection mode
4797 * Sets the selection mode of the @icon_view.
4802 gtk_icon_view_set_selection_mode (GtkIconView *icon_view,
4803 GtkSelectionMode mode)
4805 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4807 if (mode == icon_view->priv->selection_mode)
4810 if (mode == GTK_SELECTION_NONE ||
4811 icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4812 gtk_icon_view_unselect_all (icon_view);
4814 icon_view->priv->selection_mode = mode;
4816 g_object_notify (G_OBJECT (icon_view), "selection-mode");
4820 * gtk_icon_view_get_selection_mode:
4821 * @icon_view: A #GtkIconView.
4823 * Gets the selection mode of the @icon_view.
4825 * Return value: the current selection mode
4830 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4832 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4834 return icon_view->priv->selection_mode;
4838 * gtk_icon_view_set_model:
4839 * @icon_view: A #GtkIconView.
4840 * @model: (allow-none): The model.
4842 * Sets the model for a #GtkIconView.
4843 * If the @icon_view already has a model set, it will remove
4844 * it before setting the new model. If @model is %NULL, then
4845 * it will unset the old model.
4850 gtk_icon_view_set_model (GtkIconView *icon_view,
4851 GtkTreeModel *model)
4853 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4854 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4856 if (icon_view->priv->model == model)
4859 if (icon_view->priv->scroll_to_path)
4861 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4862 icon_view->priv->scroll_to_path = NULL;
4865 /* The area can be NULL while disposing */
4866 if (icon_view->priv->cell_area)
4867 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4873 if (icon_view->priv->pixbuf_column != -1)
4875 column_type = gtk_tree_model_get_column_type (model,
4876 icon_view->priv->pixbuf_column);
4878 g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4881 if (icon_view->priv->text_column != -1)
4883 column_type = gtk_tree_model_get_column_type (model,
4884 icon_view->priv->text_column);
4886 g_return_if_fail (column_type == G_TYPE_STRING);
4889 if (icon_view->priv->markup_column != -1)
4891 column_type = gtk_tree_model_get_column_type (model,
4892 icon_view->priv->markup_column);
4894 g_return_if_fail (column_type == G_TYPE_STRING);
4899 if (icon_view->priv->model)
4901 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4902 gtk_icon_view_row_changed,
4904 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4905 gtk_icon_view_row_inserted,
4907 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4908 gtk_icon_view_row_deleted,
4910 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4911 gtk_icon_view_rows_reordered,
4914 g_object_unref (icon_view->priv->model);
4916 g_list_free_full (icon_view->priv->items, (GDestroyNotify) gtk_icon_view_item_free);
4917 icon_view->priv->items = NULL;
4918 icon_view->priv->anchor_item = NULL;
4919 icon_view->priv->cursor_item = NULL;
4920 icon_view->priv->last_single_clicked = NULL;
4921 icon_view->priv->last_prelight = NULL;
4922 icon_view->priv->width = 0;
4923 icon_view->priv->height = 0;
4926 icon_view->priv->model = model;
4928 if (icon_view->priv->model)
4930 g_object_ref (icon_view->priv->model);
4931 g_signal_connect (icon_view->priv->model,
4933 G_CALLBACK (gtk_icon_view_row_changed),
4935 g_signal_connect (icon_view->priv->model,
4937 G_CALLBACK (gtk_icon_view_row_inserted),
4939 g_signal_connect (icon_view->priv->model,
4941 G_CALLBACK (gtk_icon_view_row_deleted),
4943 g_signal_connect (icon_view->priv->model,
4945 G_CALLBACK (gtk_icon_view_rows_reordered),
4948 gtk_icon_view_build_items (icon_view);
4951 g_object_notify (G_OBJECT (icon_view), "model");
4953 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4957 * gtk_icon_view_get_model:
4958 * @icon_view: a #GtkIconView
4960 * Returns the model the #GtkIconView is based on. Returns %NULL if the
4963 * Return value: (transfer none): A #GtkTreeModel, or %NULL if none is
4964 * currently being used.
4969 gtk_icon_view_get_model (GtkIconView *icon_view)
4971 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4973 return icon_view->priv->model;
4977 update_text_cell (GtkIconView *icon_view)
4979 if (!icon_view->priv->cell_area)
4982 if (icon_view->priv->text_column == -1 &&
4983 icon_view->priv->markup_column == -1)
4985 if (icon_view->priv->text_cell != NULL)
4987 gtk_cell_area_remove (icon_view->priv->cell_area,
4988 icon_view->priv->text_cell);
4989 icon_view->priv->text_cell = NULL;
4994 if (icon_view->priv->text_cell == NULL)
4996 icon_view->priv->text_cell = gtk_cell_renderer_text_new ();
4998 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), icon_view->priv->text_cell, FALSE);
5001 if (icon_view->priv->markup_column != -1)
5002 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5003 icon_view->priv->text_cell,
5004 "markup", icon_view->priv->markup_column,
5007 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5008 icon_view->priv->text_cell,
5009 "text", icon_view->priv->text_column,
5012 if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5013 g_object_set (icon_view->priv->text_cell,
5014 "alignment", PANGO_ALIGN_CENTER,
5015 "wrap-mode", PANGO_WRAP_WORD_CHAR,
5020 g_object_set (icon_view->priv->text_cell,
5021 "alignment", PANGO_ALIGN_LEFT,
5022 "wrap-mode", PANGO_WRAP_WORD_CHAR,
5030 update_pixbuf_cell (GtkIconView *icon_view)
5032 if (!icon_view->priv->cell_area)
5035 if (icon_view->priv->pixbuf_column == -1)
5037 if (icon_view->priv->pixbuf_cell != NULL)
5039 gtk_cell_area_remove (icon_view->priv->cell_area,
5040 icon_view->priv->pixbuf_cell);
5042 icon_view->priv->pixbuf_cell = NULL;
5047 if (icon_view->priv->pixbuf_cell == NULL)
5049 icon_view->priv->pixbuf_cell = gtk_cell_renderer_pixbuf_new ();
5051 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), icon_view->priv->pixbuf_cell, FALSE);
5054 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5055 icon_view->priv->pixbuf_cell,
5056 "pixbuf", icon_view->priv->pixbuf_column,
5059 if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5060 g_object_set (icon_view->priv->pixbuf_cell,
5065 g_object_set (icon_view->priv->pixbuf_cell,
5073 * gtk_icon_view_set_text_column:
5074 * @icon_view: A #GtkIconView.
5075 * @column: A column in the currently used model, or -1 to display no text
5077 * Sets the column with text for @icon_view to be @column. The text
5078 * column must be of type #G_TYPE_STRING.
5083 gtk_icon_view_set_text_column (GtkIconView *icon_view,
5086 if (column == icon_view->priv->text_column)
5090 icon_view->priv->text_column = -1;
5093 if (icon_view->priv->model != NULL)
5097 column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5099 g_return_if_fail (column_type == G_TYPE_STRING);
5102 icon_view->priv->text_column = column;
5105 if (icon_view->priv->cell_area)
5106 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5108 update_text_cell (icon_view);
5110 gtk_icon_view_invalidate_sizes (icon_view);
5112 g_object_notify (G_OBJECT (icon_view), "text-column");
5116 * gtk_icon_view_get_text_column:
5117 * @icon_view: A #GtkIconView.
5119 * Returns the column with text for @icon_view.
5121 * Returns: the text column, or -1 if it's unset.
5126 gtk_icon_view_get_text_column (GtkIconView *icon_view)
5128 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5130 return icon_view->priv->text_column;
5134 * gtk_icon_view_set_markup_column:
5135 * @icon_view: A #GtkIconView.
5136 * @column: A column in the currently used model, or -1 to display no text
5138 * Sets the column with markup information for @icon_view to be
5139 * @column. The markup column must be of type #G_TYPE_STRING.
5140 * If the markup column is set to something, it overrides
5141 * the text column set by gtk_icon_view_set_text_column().
5146 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5149 if (column == icon_view->priv->markup_column)
5153 icon_view->priv->markup_column = -1;
5156 if (icon_view->priv->model != NULL)
5160 column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5162 g_return_if_fail (column_type == G_TYPE_STRING);
5165 icon_view->priv->markup_column = column;
5168 if (icon_view->priv->cell_area)
5169 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5171 update_text_cell (icon_view);
5173 gtk_icon_view_invalidate_sizes (icon_view);
5175 g_object_notify (G_OBJECT (icon_view), "markup-column");
5179 * gtk_icon_view_get_markup_column:
5180 * @icon_view: A #GtkIconView.
5182 * Returns the column with markup text for @icon_view.
5184 * Returns: the markup column, or -1 if it's unset.
5189 gtk_icon_view_get_markup_column (GtkIconView *icon_view)
5191 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5193 return icon_view->priv->markup_column;
5197 * gtk_icon_view_set_pixbuf_column:
5198 * @icon_view: A #GtkIconView.
5199 * @column: A column in the currently used model, or -1 to disable
5201 * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5202 * column must be of type #GDK_TYPE_PIXBUF
5207 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5210 if (column == icon_view->priv->pixbuf_column)
5214 icon_view->priv->pixbuf_column = -1;
5217 if (icon_view->priv->model != NULL)
5221 column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5223 g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5226 icon_view->priv->pixbuf_column = column;
5229 if (icon_view->priv->cell_area)
5230 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5232 update_pixbuf_cell (icon_view);
5234 gtk_icon_view_invalidate_sizes (icon_view);
5236 g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5241 * gtk_icon_view_get_pixbuf_column:
5242 * @icon_view: A #GtkIconView.
5244 * Returns the column with pixbufs for @icon_view.
5246 * Returns: the pixbuf column, or -1 if it's unset.
5251 gtk_icon_view_get_pixbuf_column (GtkIconView *icon_view)
5253 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5255 return icon_view->priv->pixbuf_column;
5259 * gtk_icon_view_select_path:
5260 * @icon_view: A #GtkIconView.
5261 * @path: The #GtkTreePath to be selected.
5263 * Selects the row at @path.
5268 gtk_icon_view_select_path (GtkIconView *icon_view,
5271 GtkIconViewItem *item = NULL;
5273 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5274 g_return_if_fail (icon_view->priv->model != NULL);
5275 g_return_if_fail (path != NULL);
5277 if (gtk_tree_path_get_depth (path) > 0)
5278 item = g_list_nth_data (icon_view->priv->items,
5279 gtk_tree_path_get_indices(path)[0]);
5282 _gtk_icon_view_select_item (icon_view, item);
5286 * gtk_icon_view_unselect_path:
5287 * @icon_view: A #GtkIconView.
5288 * @path: The #GtkTreePath to be unselected.
5290 * Unselects the row at @path.
5295 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5298 GtkIconViewItem *item;
5300 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5301 g_return_if_fail (icon_view->priv->model != NULL);
5302 g_return_if_fail (path != NULL);
5304 item = g_list_nth_data (icon_view->priv->items,
5305 gtk_tree_path_get_indices(path)[0]);
5310 _gtk_icon_view_unselect_item (icon_view, item);
5314 * gtk_icon_view_get_selected_items:
5315 * @icon_view: A #GtkIconView.
5317 * Creates a list of paths of all selected items. Additionally, if you are
5318 * planning on modifying the model after calling this function, you may
5319 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5320 * To do this, you can use gtk_tree_row_reference_new().
5322 * To free the return value, use:
5324 * g_list_free_full (list, (GDestroyNotify) gtk_tree_patch_free);
5327 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
5332 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5335 GList *selected = NULL;
5337 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5339 for (list = icon_view->priv->items; list != NULL; list = list->next)
5341 GtkIconViewItem *item = list->data;
5345 GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5347 selected = g_list_prepend (selected, path);
5355 * gtk_icon_view_select_all:
5356 * @icon_view: A #GtkIconView.
5358 * Selects all the icons. @icon_view must has its selection mode set
5359 * to #GTK_SELECTION_MULTIPLE.
5364 gtk_icon_view_select_all (GtkIconView *icon_view)
5367 gboolean dirty = FALSE;
5369 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5371 if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5374 for (items = icon_view->priv->items; items; items = items->next)
5376 GtkIconViewItem *item = items->data;
5378 if (!item->selected)
5381 item->selected = TRUE;
5382 gtk_icon_view_queue_draw_item (icon_view, item);
5387 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5391 * gtk_icon_view_unselect_all:
5392 * @icon_view: A #GtkIconView.
5394 * Unselects all the icons.
5399 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5401 gboolean dirty = FALSE;
5403 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5405 if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5408 dirty = gtk_icon_view_unselect_all_internal (icon_view);
5411 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5415 * gtk_icon_view_path_is_selected:
5416 * @icon_view: A #GtkIconView.
5417 * @path: A #GtkTreePath to check selection on.
5419 * Returns %TRUE if the icon pointed to by @path is currently
5420 * selected. If @path does not point to a valid location, %FALSE is returned.
5422 * Return value: %TRUE if @path is selected.
5427 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5430 GtkIconViewItem *item;
5432 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5433 g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5434 g_return_val_if_fail (path != NULL, FALSE);
5436 item = g_list_nth_data (icon_view->priv->items,
5437 gtk_tree_path_get_indices(path)[0]);
5442 return item->selected;
5446 * gtk_icon_view_get_item_row:
5447 * @icon_view: a #GtkIconView
5448 * @path: the #GtkTreePath of the item
5450 * Gets the row in which the item @path is currently
5451 * displayed. Row numbers start at 0.
5453 * Returns: The row in which the item is displayed
5458 gtk_icon_view_get_item_row (GtkIconView *icon_view,
5461 GtkIconViewItem *item;
5463 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5464 g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5465 g_return_val_if_fail (path != NULL, -1);
5467 item = g_list_nth_data (icon_view->priv->items,
5468 gtk_tree_path_get_indices(path)[0]);
5477 * gtk_icon_view_get_item_column:
5478 * @icon_view: a #GtkIconView
5479 * @path: the #GtkTreePath of the item
5481 * Gets the column in which the item @path is currently
5482 * displayed. Column numbers start at 0.
5484 * Returns: The column in which the item is displayed
5489 gtk_icon_view_get_item_column (GtkIconView *icon_view,
5492 GtkIconViewItem *item;
5494 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5495 g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5496 g_return_val_if_fail (path != NULL, -1);
5498 item = g_list_nth_data (icon_view->priv->items,
5499 gtk_tree_path_get_indices(path)[0]);
5508 * gtk_icon_view_item_activated:
5509 * @icon_view: A #GtkIconView
5510 * @path: The #GtkTreePath to be activated
5512 * Activates the item determined by @path.
5517 gtk_icon_view_item_activated (GtkIconView *icon_view,
5520 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5521 g_return_if_fail (path != NULL);
5523 g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5527 * gtk_icon_view_set_item_orientation:
5528 * @icon_view: a #GtkIconView
5529 * @orientation: the relative position of texts and icons
5531 * Sets the ::item-orientation property which determines whether the labels
5532 * are drawn beside the icons instead of below.
5537 gtk_icon_view_set_item_orientation (GtkIconView *icon_view,
5538 GtkOrientation orientation)
5540 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5542 if (icon_view->priv->item_orientation != orientation)
5544 icon_view->priv->item_orientation = orientation;
5546 if (icon_view->priv->cell_area)
5548 if (GTK_IS_ORIENTABLE (icon_view->priv->cell_area))
5549 gtk_orientable_set_orientation (GTK_ORIENTABLE (icon_view->priv->cell_area),
5550 icon_view->priv->item_orientation);
5552 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5555 gtk_icon_view_invalidate_sizes (icon_view);
5557 update_text_cell (icon_view);
5558 update_pixbuf_cell (icon_view);
5560 g_object_notify (G_OBJECT (icon_view), "item-orientation");
5565 * gtk_icon_view_get_item_orientation:
5566 * @icon_view: a #GtkIconView
5568 * Returns the value of the ::item-orientation property which determines
5569 * whether the labels are drawn beside the icons instead of below.
5571 * Return value: the relative position of texts and icons
5576 gtk_icon_view_get_item_orientation (GtkIconView *icon_view)
5578 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view),
5579 GTK_ORIENTATION_VERTICAL);
5581 return icon_view->priv->item_orientation;
5585 * gtk_icon_view_set_columns:
5586 * @icon_view: a #GtkIconView
5587 * @columns: the number of columns
5589 * Sets the ::columns property which determines in how
5590 * many columns the icons are arranged. If @columns is
5591 * -1, the number of columns will be chosen automatically
5592 * to fill the available area.
5597 gtk_icon_view_set_columns (GtkIconView *icon_view,
5600 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5602 if (icon_view->priv->columns != columns)
5604 icon_view->priv->columns = columns;
5606 if (icon_view->priv->cell_area)
5607 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5609 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
5611 g_object_notify (G_OBJECT (icon_view), "columns");
5616 * gtk_icon_view_get_columns:
5617 * @icon_view: a #GtkIconView
5619 * Returns the value of the ::columns property.
5621 * Return value: the number of columns, or -1
5626 gtk_icon_view_get_columns (GtkIconView *icon_view)
5628 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5630 return icon_view->priv->columns;
5634 * gtk_icon_view_set_item_width:
5635 * @icon_view: a #GtkIconView
5636 * @item_width: the width for each item
5638 * Sets the ::item-width property which specifies the width
5639 * to use for each item. If it is set to -1, the icon view will
5640 * automatically determine a suitable item size.
5645 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5648 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5650 if (icon_view->priv->item_width != item_width)
5652 icon_view->priv->item_width = item_width;
5654 if (icon_view->priv->cell_area)
5655 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5657 gtk_icon_view_invalidate_sizes (icon_view);
5659 update_text_cell (icon_view);
5661 g_object_notify (G_OBJECT (icon_view), "item-width");
5666 * gtk_icon_view_get_item_width:
5667 * @icon_view: a #GtkIconView
5669 * Returns the value of the ::item-width property.
5671 * Return value: the width of a single item, or -1
5676 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5678 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5680 return icon_view->priv->item_width;
5685 * gtk_icon_view_set_spacing:
5686 * @icon_view: a #GtkIconView
5687 * @spacing: the spacing
5689 * Sets the ::spacing property which specifies the space
5690 * which is inserted between the cells (i.e. the icon and
5691 * the text) of an item.
5696 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5699 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5701 if (icon_view->priv->spacing != spacing)
5703 icon_view->priv->spacing = spacing;
5705 if (icon_view->priv->cell_area)
5706 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5708 gtk_icon_view_invalidate_sizes (icon_view);
5710 g_object_notify (G_OBJECT (icon_view), "spacing");
5715 * gtk_icon_view_get_spacing:
5716 * @icon_view: a #GtkIconView
5718 * Returns the value of the ::spacing property.
5720 * Return value: the space between cells
5725 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5727 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5729 return icon_view->priv->spacing;
5733 * gtk_icon_view_set_row_spacing:
5734 * @icon_view: a #GtkIconView
5735 * @row_spacing: the row spacing
5737 * Sets the ::row-spacing property which specifies the space
5738 * which is inserted between the rows of the icon view.
5743 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5746 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5748 if (icon_view->priv->row_spacing != row_spacing)
5750 icon_view->priv->row_spacing = row_spacing;
5752 if (icon_view->priv->cell_area)
5753 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5755 gtk_icon_view_invalidate_sizes (icon_view);
5757 g_object_notify (G_OBJECT (icon_view), "row-spacing");
5762 * gtk_icon_view_get_row_spacing:
5763 * @icon_view: a #GtkIconView
5765 * Returns the value of the ::row-spacing property.
5767 * Return value: the space between rows
5772 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5774 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5776 return icon_view->priv->row_spacing;
5780 * gtk_icon_view_set_column_spacing:
5781 * @icon_view: a #GtkIconView
5782 * @column_spacing: the column spacing
5784 * Sets the ::column-spacing property which specifies the space
5785 * which is inserted between the columns of the icon view.
5790 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5791 gint column_spacing)
5793 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5795 if (icon_view->priv->column_spacing != column_spacing)
5797 icon_view->priv->column_spacing = column_spacing;
5799 if (icon_view->priv->cell_area)
5800 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5802 gtk_icon_view_invalidate_sizes (icon_view);
5804 g_object_notify (G_OBJECT (icon_view), "column-spacing");
5809 * gtk_icon_view_get_column_spacing:
5810 * @icon_view: a #GtkIconView
5812 * Returns the value of the ::column-spacing property.
5814 * Return value: the space between columns
5819 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5821 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5823 return icon_view->priv->column_spacing;
5827 * gtk_icon_view_set_margin:
5828 * @icon_view: a #GtkIconView
5829 * @margin: the margin
5831 * Sets the ::margin property which specifies the space
5832 * which is inserted at the top, bottom, left and right
5838 gtk_icon_view_set_margin (GtkIconView *icon_view,
5841 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5843 if (icon_view->priv->margin != margin)
5845 icon_view->priv->margin = margin;
5847 if (icon_view->priv->cell_area)
5848 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5850 gtk_icon_view_invalidate_sizes (icon_view);
5852 g_object_notify (G_OBJECT (icon_view), "margin");
5857 * gtk_icon_view_get_margin:
5858 * @icon_view: a #GtkIconView
5860 * Returns the value of the ::margin property.
5862 * Return value: the space at the borders
5867 gtk_icon_view_get_margin (GtkIconView *icon_view)
5869 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5871 return icon_view->priv->margin;
5875 * gtk_icon_view_set_item_padding:
5876 * @icon_view: a #GtkIconView
5877 * @item_padding: the item padding
5879 * Sets the #GtkIconView:item-padding property which specifies the padding
5880 * around each of the icon view's items.
5885 gtk_icon_view_set_item_padding (GtkIconView *icon_view,
5888 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5890 if (icon_view->priv->item_padding != item_padding)
5892 icon_view->priv->item_padding = item_padding;
5894 if (icon_view->priv->cell_area)
5895 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5897 gtk_icon_view_invalidate_sizes (icon_view);
5899 g_object_notify (G_OBJECT (icon_view), "item-padding");
5904 * gtk_icon_view_get_item_padding:
5905 * @icon_view: a #GtkIconView
5907 * Returns the value of the ::item-padding property.
5909 * Return value: the padding around items
5914 gtk_icon_view_get_item_padding (GtkIconView *icon_view)
5916 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5918 return icon_view->priv->item_padding;
5921 /* Get/set whether drag_motion requested the drag data and
5922 * drag_data_received should thus not actually insert the data,
5923 * since the data doesn't result from a drop.
5926 set_status_pending (GdkDragContext *context,
5927 GdkDragAction suggested_action)
5929 g_object_set_data (G_OBJECT (context),
5930 I_("gtk-icon-view-status-pending"),
5931 GINT_TO_POINTER (suggested_action));
5934 static GdkDragAction
5935 get_status_pending (GdkDragContext *context)
5937 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5938 "gtk-icon-view-status-pending"));
5942 unset_reorderable (GtkIconView *icon_view)
5944 if (icon_view->priv->reorderable)
5946 icon_view->priv->reorderable = FALSE;
5947 g_object_notify (G_OBJECT (icon_view), "reorderable");
5952 set_source_row (GdkDragContext *context,
5953 GtkTreeModel *model,
5954 GtkTreePath *source_row)
5957 g_object_set_data_full (G_OBJECT (context),
5958 I_("gtk-icon-view-source-row"),
5959 gtk_tree_row_reference_new (model, source_row),
5960 (GDestroyNotify) gtk_tree_row_reference_free);
5962 g_object_set_data_full (G_OBJECT (context),
5963 I_("gtk-icon-view-source-row"),
5968 get_source_row (GdkDragContext *context)
5970 GtkTreeRowReference *ref;
5972 ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5975 return gtk_tree_row_reference_get_path (ref);
5982 GtkTreeRowReference *dest_row;
5983 gboolean empty_view_drop;
5984 gboolean drop_append_mode;
5988 dest_row_free (gpointer data)
5990 DestRow *dr = (DestRow *)data;
5992 gtk_tree_row_reference_free (dr->dest_row);
5997 set_dest_row (GdkDragContext *context,
5998 GtkTreeModel *model,
5999 GtkTreePath *dest_row,
6000 gboolean empty_view_drop,
6001 gboolean drop_append_mode)
6007 g_object_set_data_full (G_OBJECT (context),
6008 I_("gtk-icon-view-dest-row"),
6013 dr = g_new0 (DestRow, 1);
6015 dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
6016 dr->empty_view_drop = empty_view_drop;
6017 dr->drop_append_mode = drop_append_mode;
6018 g_object_set_data_full (G_OBJECT (context),
6019 I_("gtk-icon-view-dest-row"),
6020 dr, (GDestroyNotify) dest_row_free);
6024 get_dest_row (GdkDragContext *context)
6028 dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
6032 GtkTreePath *path = NULL;
6035 path = gtk_tree_row_reference_get_path (dr->dest_row);
6036 else if (dr->empty_view_drop)
6037 path = gtk_tree_path_new_from_indices (0, -1);
6041 if (path && dr->drop_append_mode)
6042 gtk_tree_path_next (path);
6051 check_model_dnd (GtkTreeModel *model,
6052 GType required_iface,
6053 const gchar *signal)
6055 if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
6057 g_warning ("You must override the default '%s' handler "
6058 "on GtkIconView when using models that don't support "
6059 "the %s interface and enabling drag-and-drop. The simplest way to do this "
6060 "is to connect to '%s' and call "
6061 "g_signal_stop_emission_by_name() in your signal handler to prevent "
6062 "the default handler from running. Look at the source code "
6063 "for the default handler in gtkiconview.c to get an idea what "
6064 "your handler should do. (gtkiconview.c is in the GTK+ source "
6065 "code.) If you're using GTK+ from a language other than C, "
6066 "there may be a more natural way to override default handlers, e.g. via derivation.",
6067 signal, g_type_name (required_iface), signal);
6075 remove_scroll_timeout (GtkIconView *icon_view)
6077 if (icon_view->priv->scroll_timeout_id != 0)
6079 g_source_remove (icon_view->priv->scroll_timeout_id);
6081 icon_view->priv->scroll_timeout_id = 0;
6086 gtk_icon_view_autoscroll (GtkIconView *icon_view,
6090 gint px, py, width, height;
6091 gint hoffset, voffset;
6093 window = gtk_widget_get_window (GTK_WIDGET (icon_view));
6095 gdk_window_get_device_position (window, device, &px, &py, NULL);
6096 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
6098 /* see if we are near the edge. */
6099 voffset = py - 2 * SCROLL_EDGE_SIZE;
6101 voffset = MAX (py - (height - 2 * SCROLL_EDGE_SIZE), 0);
6103 hoffset = px - 2 * SCROLL_EDGE_SIZE;
6105 hoffset = MAX (px - (width - 2 * SCROLL_EDGE_SIZE), 0);
6108 gtk_adjustment_set_value (icon_view->priv->vadjustment,
6109 gtk_adjustment_get_value (icon_view->priv->vadjustment) + voffset);
6112 gtk_adjustment_set_value (icon_view->priv->hadjustment,
6113 gtk_adjustment_get_value (icon_view->priv->hadjustment) + hoffset);
6117 GtkIconView *icon_view;
6122 drag_scroll_timeout (gpointer datap)
6124 DragScrollData *data = datap;
6126 gtk_icon_view_autoscroll (data->icon_view, data->device);
6132 drag_scroll_data_free (DragScrollData *data)
6134 g_slice_free (DragScrollData, data);
6138 set_destination (GtkIconView *icon_view,
6139 GdkDragContext *context,
6142 GdkDragAction *suggested_action,
6146 GtkTreePath *path = NULL;
6147 GtkIconViewDropPosition pos;
6148 GtkIconViewDropPosition old_pos;
6149 GtkTreePath *old_dest_path = NULL;
6150 gboolean can_drop = FALSE;
6152 widget = GTK_WIDGET (icon_view);
6154 *suggested_action = 0;
6157 if (!icon_view->priv->dest_set)
6159 /* someone unset us as a drag dest, note that if
6160 * we return FALSE drag_leave isn't called
6163 gtk_icon_view_set_drag_dest_item (icon_view,
6165 GTK_ICON_VIEW_DROP_LEFT);
6167 remove_scroll_timeout (GTK_ICON_VIEW (widget));
6169 return FALSE; /* no longer a drop site */
6172 *target = gtk_drag_dest_find_target (widget, context,
6173 gtk_drag_dest_get_target_list (widget));
6174 if (*target == GDK_NONE)
6177 if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos))
6180 GtkTreeModel *model;
6182 /* the row got dropped on empty space, let's setup a special case
6186 gtk_tree_path_free (path);
6188 model = gtk_icon_view_get_model (icon_view);
6190 n_children = gtk_tree_model_iter_n_children (model, NULL);
6193 pos = GTK_ICON_VIEW_DROP_BELOW;
6194 path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6198 pos = GTK_ICON_VIEW_DROP_ABOVE;
6199 path = gtk_tree_path_new_from_indices (0, -1);
6209 gtk_icon_view_get_drag_dest_item (icon_view,
6214 gtk_tree_path_free (old_dest_path);
6216 if (TRUE /* FIXME if the location droppable predicate */)
6224 GtkWidget *source_widget;
6226 *suggested_action = gdk_drag_context_get_suggested_action (context);
6227 source_widget = gtk_drag_get_source_widget (context);
6229 if (source_widget == widget)
6231 /* Default to MOVE, unless the user has
6232 * pressed ctrl or shift to affect available actions
6234 if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6235 *suggested_action = GDK_ACTION_MOVE;
6238 gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6243 /* can't drop here */
6244 gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6246 GTK_ICON_VIEW_DROP_LEFT);
6250 gtk_tree_path_free (path);
6256 get_logical_destination (GtkIconView *icon_view,
6257 gboolean *drop_append_mode)
6259 /* adjust path to point to the row the drop goes in front of */
6260 GtkTreePath *path = NULL;
6261 GtkIconViewDropPosition pos;
6263 *drop_append_mode = FALSE;
6265 gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6270 if (pos == GTK_ICON_VIEW_DROP_RIGHT ||
6271 pos == GTK_ICON_VIEW_DROP_BELOW)
6274 GtkTreeModel *model = icon_view->priv->model;
6276 if (!gtk_tree_model_get_iter (model, &iter, path) ||
6277 !gtk_tree_model_iter_next (model, &iter))
6278 *drop_append_mode = TRUE;
6281 *drop_append_mode = FALSE;
6282 gtk_tree_path_next (path);
6290 gtk_icon_view_maybe_begin_drag (GtkIconView *icon_view,
6291 GdkEventMotion *event)
6293 GtkWidget *widget = GTK_WIDGET (icon_view);
6294 GdkDragContext *context;
6295 GtkTreePath *path = NULL;
6297 GtkTreeModel *model;
6298 gboolean retval = FALSE;
6300 if (!icon_view->priv->source_set)
6303 if (icon_view->priv->pressed_button < 0)
6306 if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6307 icon_view->priv->press_start_x,
6308 icon_view->priv->press_start_y,
6309 event->x, event->y))
6312 model = gtk_icon_view_get_model (icon_view);
6317 button = icon_view->priv->pressed_button;
6318 icon_view->priv->pressed_button = -1;
6320 path = gtk_icon_view_get_path_at_pos (icon_view,
6321 icon_view->priv->press_start_x,
6322 icon_view->priv->press_start_y);
6327 if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6328 !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6332 /* FIXME Check whether we're a start button, if not return FALSE and
6336 /* Now we can begin the drag */
6340 context = gtk_drag_begin (widget,
6341 gtk_drag_source_get_target_list (widget),
6342 icon_view->priv->source_actions,
6346 set_source_row (context, model, path);
6350 gtk_tree_path_free (path);
6355 /* Source side drag signals */
6357 gtk_icon_view_drag_begin (GtkWidget *widget,
6358 GdkDragContext *context)
6360 GtkIconView *icon_view;
6361 GtkIconViewItem *item;
6362 cairo_surface_t *icon;
6366 icon_view = GTK_ICON_VIEW (widget);
6368 /* if the user uses a custom DnD impl, we don't set the icon here */
6369 if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6372 item = _gtk_icon_view_get_item_at_coords (icon_view,
6373 icon_view->priv->press_start_x,
6374 icon_view->priv->press_start_y,
6378 g_return_if_fail (item != NULL);
6380 x = icon_view->priv->press_start_x - item->cell_area.x + 1;
6381 y = icon_view->priv->press_start_y - item->cell_area.y + 1;
6383 path = gtk_tree_path_new_from_indices (item->index, -1);
6384 icon = gtk_icon_view_create_drag_icon (icon_view, path);
6385 gtk_tree_path_free (path);
6387 cairo_surface_set_device_offset (icon, -x, -y);
6389 gtk_drag_set_icon_surface (context, icon);
6391 cairo_surface_destroy (icon);
6395 gtk_icon_view_drag_end (GtkWidget *widget,
6396 GdkDragContext *context)
6402 gtk_icon_view_drag_data_get (GtkWidget *widget,
6403 GdkDragContext *context,
6404 GtkSelectionData *selection_data,
6408 GtkIconView *icon_view;
6409 GtkTreeModel *model;
6410 GtkTreePath *source_row;
6412 icon_view = GTK_ICON_VIEW (widget);
6413 model = gtk_icon_view_get_model (icon_view);
6418 if (!icon_view->priv->source_set)
6421 source_row = get_source_row (context);
6423 if (source_row == NULL)
6426 /* We can implement the GTK_TREE_MODEL_ROW target generically for
6427 * any model; for DragSource models there are some other targets
6431 if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6432 gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6437 /* If drag_data_get does nothing, try providing row data. */
6438 if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6439 gtk_tree_set_row_drag_data (selection_data,
6444 gtk_tree_path_free (source_row);
6448 gtk_icon_view_drag_data_delete (GtkWidget *widget,
6449 GdkDragContext *context)
6451 GtkTreeModel *model;
6452 GtkIconView *icon_view;
6453 GtkTreePath *source_row;
6455 icon_view = GTK_ICON_VIEW (widget);
6456 model = gtk_icon_view_get_model (icon_view);
6458 if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6461 if (!icon_view->priv->source_set)
6464 source_row = get_source_row (context);
6466 if (source_row == NULL)
6469 gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6472 gtk_tree_path_free (source_row);
6474 set_source_row (context, NULL, NULL);
6477 /* Target side drag signals */
6479 gtk_icon_view_drag_leave (GtkWidget *widget,
6480 GdkDragContext *context,
6483 GtkIconView *icon_view;
6485 icon_view = GTK_ICON_VIEW (widget);
6487 /* unset any highlight row */
6488 gtk_icon_view_set_drag_dest_item (icon_view,
6490 GTK_ICON_VIEW_DROP_LEFT);
6492 remove_scroll_timeout (icon_view);
6496 gtk_icon_view_drag_motion (GtkWidget *widget,
6497 GdkDragContext *context,
6502 GtkTreePath *path = NULL;
6503 GtkIconViewDropPosition pos;
6504 GtkIconView *icon_view;
6505 GdkDragAction suggested_action = 0;
6509 icon_view = GTK_ICON_VIEW (widget);
6511 if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6514 gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6516 /* we only know this *after* set_desination_row */
6517 empty = icon_view->priv->empty_view_drop;
6519 if (path == NULL && !empty)
6521 /* Can't drop here. */
6522 gdk_drag_status (context, 0, time);
6526 if (icon_view->priv->scroll_timeout_id == 0)
6528 DragScrollData *data = g_slice_new (DragScrollData);
6529 data->icon_view = icon_view;
6530 data->device = gdk_drag_context_get_device (context);
6532 icon_view->priv->scroll_timeout_id =
6533 gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 50, drag_scroll_timeout, data, (GDestroyNotify) drag_scroll_data_free);
6536 if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6538 /* Request data so we can use the source row when
6539 * determining whether to accept the drop
6541 set_status_pending (context, suggested_action);
6542 gtk_drag_get_data (widget, context, target, time);
6546 set_status_pending (context, 0);
6547 gdk_drag_status (context, suggested_action, time);
6552 gtk_tree_path_free (path);
6558 gtk_icon_view_drag_drop (GtkWidget *widget,
6559 GdkDragContext *context,
6564 GtkIconView *icon_view;
6566 GdkDragAction suggested_action = 0;
6567 GdkAtom target = GDK_NONE;
6568 GtkTreeModel *model;
6569 gboolean drop_append_mode;
6571 icon_view = GTK_ICON_VIEW (widget);
6572 model = gtk_icon_view_get_model (icon_view);
6574 remove_scroll_timeout (GTK_ICON_VIEW (widget));
6576 if (!icon_view->priv->dest_set)
6579 if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6582 if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6585 path = get_logical_destination (icon_view, &drop_append_mode);
6587 if (target != GDK_NONE && path != NULL)
6589 /* in case a motion had requested drag data, change things so we
6590 * treat drag data receives as a drop.
6592 set_status_pending (context, 0);
6593 set_dest_row (context, model, path,
6594 icon_view->priv->empty_view_drop, drop_append_mode);
6598 gtk_tree_path_free (path);
6600 /* Unset this thing */
6601 gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6603 if (target != GDK_NONE)
6605 gtk_drag_get_data (widget, context, target, time);
6613 gtk_icon_view_drag_data_received (GtkWidget *widget,
6614 GdkDragContext *context,
6617 GtkSelectionData *selection_data,
6622 gboolean accepted = FALSE;
6623 GtkTreeModel *model;
6624 GtkIconView *icon_view;
6625 GtkTreePath *dest_row;
6626 GdkDragAction suggested_action;
6627 gboolean drop_append_mode;
6629 icon_view = GTK_ICON_VIEW (widget);
6630 model = gtk_icon_view_get_model (icon_view);
6632 if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6635 if (!icon_view->priv->dest_set)
6638 suggested_action = get_status_pending (context);
6640 if (suggested_action)
6642 /* We are getting this data due to a request in drag_motion,
6643 * rather than due to a request in drag_drop, so we are just
6644 * supposed to call drag_status, not actually paste in the
6647 path = get_logical_destination (icon_view, &drop_append_mode);
6650 suggested_action = 0;
6652 if (suggested_action)
6654 if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6657 suggested_action = 0;
6660 gdk_drag_status (context, suggested_action, time);
6663 gtk_tree_path_free (path);
6665 /* If you can't drop, remove user drop indicator until the next motion */
6666 if (suggested_action == 0)
6667 gtk_icon_view_set_drag_dest_item (icon_view,
6669 GTK_ICON_VIEW_DROP_LEFT);
6674 dest_row = get_dest_row (context);
6676 if (dest_row == NULL)
6679 if (gtk_selection_data_get_length (selection_data) >= 0)
6681 if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6687 gtk_drag_finish (context,
6689 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
6692 gtk_tree_path_free (dest_row);
6695 set_dest_row (context, NULL, NULL, FALSE, FALSE);
6698 /* Drag-and-Drop support */
6700 * gtk_icon_view_enable_model_drag_source:
6701 * @icon_view: a #GtkIconTreeView
6702 * @start_button_mask: Mask of allowed buttons to start drag
6703 * @targets: (array length=n_targets): the table of targets that the drag will
6705 * @n_targets: the number of items in @targets
6706 * @actions: the bitmask of possible actions for a drag from this
6709 * Turns @icon_view into a drag source for automatic DND. Calling this
6710 * method sets #GtkIconView:reorderable to %FALSE.
6715 gtk_icon_view_enable_model_drag_source (GtkIconView *icon_view,
6716 GdkModifierType start_button_mask,
6717 const GtkTargetEntry *targets,
6719 GdkDragAction actions)
6721 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6723 gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6725 icon_view->priv->start_button_mask = start_button_mask;
6726 icon_view->priv->source_actions = actions;
6728 icon_view->priv->source_set = TRUE;
6730 unset_reorderable (icon_view);
6734 * gtk_icon_view_enable_model_drag_dest:
6735 * @icon_view: a #GtkIconView
6736 * @targets: (array length=n_targets): the table of targets that the drag will
6738 * @n_targets: the number of items in @targets
6739 * @actions: the bitmask of possible actions for a drag to this
6742 * Turns @icon_view into a drop destination for automatic DND. Calling this
6743 * method sets #GtkIconView:reorderable to %FALSE.
6748 gtk_icon_view_enable_model_drag_dest (GtkIconView *icon_view,
6749 const GtkTargetEntry *targets,
6751 GdkDragAction actions)
6753 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6755 gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6757 icon_view->priv->dest_actions = actions;
6759 icon_view->priv->dest_set = TRUE;
6761 unset_reorderable (icon_view);
6765 * gtk_icon_view_unset_model_drag_source:
6766 * @icon_view: a #GtkIconView
6768 * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
6769 * method sets #GtkIconView:reorderable to %FALSE.
6774 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6776 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6778 if (icon_view->priv->source_set)
6780 gtk_drag_source_unset (GTK_WIDGET (icon_view));
6781 icon_view->priv->source_set = FALSE;
6784 unset_reorderable (icon_view);
6788 * gtk_icon_view_unset_model_drag_dest:
6789 * @icon_view: a #GtkIconView
6791 * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
6792 * method sets #GtkIconView:reorderable to %FALSE.
6797 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6799 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6801 if (icon_view->priv->dest_set)
6803 gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6804 icon_view->priv->dest_set = FALSE;
6807 unset_reorderable (icon_view);
6810 /* These are useful to implement your own custom stuff. */
6812 * gtk_icon_view_set_drag_dest_item:
6813 * @icon_view: a #GtkIconView
6814 * @path: (allow-none): The path of the item to highlight, or %NULL.
6815 * @pos: Specifies where to drop, relative to the item
6817 * Sets the item that is highlighted for feedback.
6822 gtk_icon_view_set_drag_dest_item (GtkIconView *icon_view,
6824 GtkIconViewDropPosition pos)
6826 /* Note; this function is exported to allow a custom DND
6827 * implementation, so it can't touch TreeViewDragInfo
6830 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6832 if (icon_view->priv->dest_item)
6834 GtkTreePath *current_path;
6835 current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6836 gtk_tree_row_reference_free (icon_view->priv->dest_item);
6837 icon_view->priv->dest_item = NULL;
6839 gtk_icon_view_queue_draw_path (icon_view, current_path);
6840 gtk_tree_path_free (current_path);
6843 /* special case a drop on an empty model */
6844 icon_view->priv->empty_view_drop = FALSE;
6845 if (pos == GTK_ICON_VIEW_DROP_ABOVE && path
6846 && gtk_tree_path_get_depth (path) == 1
6847 && gtk_tree_path_get_indices (path)[0] == 0)
6851 n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6854 if (n_children == 0)
6855 icon_view->priv->empty_view_drop = TRUE;
6858 icon_view->priv->dest_pos = pos;
6862 icon_view->priv->dest_item =
6863 gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view),
6864 icon_view->priv->model, path);
6866 gtk_icon_view_queue_draw_path (icon_view, path);
6871 * gtk_icon_view_get_drag_dest_item:
6872 * @icon_view: a #GtkIconView
6873 * @path: (out) (allow-none): Return location for the path of
6874 * the highlighted item, or %NULL.
6875 * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6877 * Gets information about the item that is highlighted for feedback.
6882 gtk_icon_view_get_drag_dest_item (GtkIconView *icon_view,
6884 GtkIconViewDropPosition *pos)
6886 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6890 if (icon_view->priv->dest_item)
6891 *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6897 *pos = icon_view->priv->dest_pos;
6901 * gtk_icon_view_get_dest_item_at_pos:
6902 * @icon_view: a #GtkIconView
6903 * @drag_x: the position to determine the destination item for
6904 * @drag_y: the position to determine the destination item for
6905 * @path: (out) (allow-none): Return location for the path of the item,
6907 * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6909 * Determines the destination item for a given position.
6911 * Return value: whether there is an item at the given position.
6916 gtk_icon_view_get_dest_item_at_pos (GtkIconView *icon_view,
6920 GtkIconViewDropPosition *pos)
6922 GtkIconViewItem *item;
6924 /* Note; this function is exported to allow a custom DND
6925 * implementation, so it can't touch TreeViewDragInfo
6928 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6929 g_return_val_if_fail (drag_x >= 0, FALSE);
6930 g_return_val_if_fail (drag_y >= 0, FALSE);
6931 g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6937 item = _gtk_icon_view_get_item_at_coords (icon_view,
6938 drag_x + gtk_adjustment_get_value (icon_view->priv->hadjustment),
6939 drag_y + gtk_adjustment_get_value (icon_view->priv->vadjustment),
6946 *path = gtk_tree_path_new_from_indices (item->index, -1);
6950 if (drag_x < item->cell_area.x + item->cell_area.width / 4)
6951 *pos = GTK_ICON_VIEW_DROP_LEFT;
6952 else if (drag_x > item->cell_area.x + item->cell_area.width * 3 / 4)
6953 *pos = GTK_ICON_VIEW_DROP_RIGHT;
6954 else if (drag_y < item->cell_area.y + item->cell_area.height / 4)
6955 *pos = GTK_ICON_VIEW_DROP_ABOVE;
6956 else if (drag_y > item->cell_area.y + item->cell_area.height * 3 / 4)
6957 *pos = GTK_ICON_VIEW_DROP_BELOW;
6959 *pos = GTK_ICON_VIEW_DROP_INTO;
6966 * gtk_icon_view_create_drag_icon:
6967 * @icon_view: a #GtkIconView
6968 * @path: a #GtkTreePath in @icon_view
6970 * Creates a #cairo_surface_t representation of the item at @path.
6971 * This image is used for a drag icon.
6973 * Return value: (transfer full): a newly-allocated surface of the drag icon.
6978 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6982 GtkStyleContext *context;
6984 cairo_surface_t *surface;
6988 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
6989 g_return_val_if_fail (path != NULL, NULL);
6991 widget = GTK_WIDGET (icon_view);
6992 context = gtk_widget_get_style_context (widget);
6994 if (!gtk_widget_get_realized (widget))
6997 index = gtk_tree_path_get_indices (path)[0];
6999 for (l = icon_view->priv->items; l; l = l->next)
7001 GtkIconViewItem *item = l->data;
7003 if (index == item->index)
7005 GdkRectangle rect = {
7006 item->cell_area.x - icon_view->priv->item_padding,
7007 item->cell_area.y - icon_view->priv->item_padding,
7008 item->cell_area.width + icon_view->priv->item_padding * 2,
7009 item->cell_area.height + icon_view->priv->item_padding * 2
7012 surface = gdk_window_create_similar_surface (icon_view->priv->bin_window,
7013 CAIRO_CONTENT_COLOR,
7017 cr = cairo_create (surface);
7018 cairo_set_line_width (cr, 1.);
7020 gtk_render_background (context, cr, 0, 0,
7021 rect.width + 2, rect.height + 2);
7025 cairo_rectangle (cr, 1, 1, rect.width, rect.height);
7028 gtk_icon_view_paint_item (icon_view, cr, item,
7029 icon_view->priv->item_padding + 1,
7030 icon_view->priv->item_padding + 1, FALSE);
7034 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
7035 cairo_rectangle (cr, 0.5, 0.5, rect.width + 1, rect.height + 1);
7048 * gtk_icon_view_get_reorderable:
7049 * @icon_view: a #GtkIconView
7051 * Retrieves whether the user can reorder the list via drag-and-drop.
7052 * See gtk_icon_view_set_reorderable().
7054 * Return value: %TRUE if the list can be reordered.
7059 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
7061 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7063 return icon_view->priv->reorderable;
7066 static const GtkTargetEntry item_targets[] = {
7067 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
7072 * gtk_icon_view_set_reorderable:
7073 * @icon_view: A #GtkIconView.
7074 * @reorderable: %TRUE, if the list of items can be reordered.
7076 * This function is a convenience function to allow you to reorder models that
7077 * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface. Both
7078 * #GtkTreeStore and #GtkListStore support these. If @reorderable is %TRUE, then
7079 * the user can reorder the model by dragging and dropping rows. The
7080 * developer can listen to these changes by connecting to the model's
7081 * row_inserted and row_deleted signals. The reordering is implemented by setting up
7082 * the icon view as a drag source and destination. Therefore, drag and
7083 * drop can not be used in a reorderable view for any other purpose.
7085 * This function does not give you any degree of control over the order -- any
7086 * reordering is allowed. If more control is needed, you should probably
7087 * handle drag and drop manually.
7092 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
7093 gboolean reorderable)
7095 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7097 reorderable = reorderable != FALSE;
7099 if (icon_view->priv->reorderable == reorderable)
7104 gtk_icon_view_enable_model_drag_source (icon_view,
7107 G_N_ELEMENTS (item_targets),
7109 gtk_icon_view_enable_model_drag_dest (icon_view,
7111 G_N_ELEMENTS (item_targets),
7116 gtk_icon_view_unset_model_drag_source (icon_view);
7117 gtk_icon_view_unset_model_drag_dest (icon_view);
7120 icon_view->priv->reorderable = reorderable;
7122 g_object_notify (G_OBJECT (icon_view), "reorderable");
7126 gtk_icon_view_buildable_custom_tag_start (GtkBuildable *buildable,
7127 GtkBuilder *builder,
7129 const gchar *tagname,
7130 GMarkupParser *parser,
7133 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
7134 tagname, parser, data))
7137 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
7138 tagname, parser, data);
7142 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
7143 GtkBuilder *builder,
7145 const gchar *tagname,
7148 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder,
7149 child, tagname, data))
7150 parent_buildable_iface->custom_tag_end (buildable, builder,
7151 child, tagname, data);