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);
1397 cell_area_get_preferred_size (GtkIconView *icon_view,
1398 GtkCellAreaContext *context,
1399 GtkOrientation orientation,
1404 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1407 gtk_cell_area_get_preferred_width_for_height (icon_view->priv->cell_area,
1409 GTK_WIDGET (icon_view),
1413 gtk_cell_area_get_preferred_width (icon_view->priv->cell_area,
1415 GTK_WIDGET (icon_view),
1421 gtk_cell_area_get_preferred_height_for_width (icon_view->priv->cell_area,
1423 GTK_WIDGET (icon_view),
1427 gtk_cell_area_get_preferred_height (icon_view->priv->cell_area,
1429 GTK_WIDGET (icon_view),
1435 gtk_icon_view_get_preferred_item_size (GtkIconView *icon_view,
1436 GtkOrientation orientation,
1441 GtkIconViewPrivate *priv = icon_view->priv;
1442 GtkCellAreaContext *context;
1445 context = gtk_cell_area_create_context (priv->cell_area);
1447 for_size -= 2 * priv->item_padding;
1451 /* This is necessary for the context to work properly */
1452 for (items = priv->items; items; items = items->next)
1454 GtkIconViewItem *item = items->data;
1456 _gtk_icon_view_set_cell_data (icon_view, item);
1457 cell_area_get_preferred_size (icon_view, context, 1 - orientation, -1, NULL, NULL);
1461 for (items = priv->items; items; items = items->next)
1463 GtkIconViewItem *item = items->data;
1465 _gtk_icon_view_set_cell_data (icon_view, item);
1466 if (items == priv->items)
1467 adjust_wrap_width (icon_view);
1468 cell_area_get_preferred_size (icon_view, context, orientation, for_size, NULL, NULL);
1471 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1474 gtk_cell_area_context_get_preferred_width_for_height (context,
1478 gtk_cell_area_context_get_preferred_width (context,
1484 gtk_cell_area_context_get_preferred_height_for_width (context,
1488 gtk_cell_area_context_get_preferred_height (context,
1492 if (orientation == GTK_ORIENTATION_HORIZONTAL && priv->item_width >= 0)
1495 *minimum = MAX (*minimum, priv->item_width);
1497 *natural = *minimum;
1501 *minimum += 2 * priv->item_padding;
1503 *natural += 2 * priv->item_padding;
1505 g_object_unref (context);
1509 gtk_icon_view_compute_n_items_for_size (GtkIconView *icon_view,
1510 GtkOrientation orientation,
1513 gint *min_item_size,
1515 gint *max_item_size)
1517 GtkIconViewPrivate *priv = icon_view->priv;
1518 int minimum, natural;
1520 g_return_if_fail (min_item_size == NULL || min_items != NULL);
1521 g_return_if_fail (max_item_size == NULL || max_items != NULL);
1523 gtk_icon_view_get_preferred_item_size (icon_view, orientation, -1, &minimum, &natural);
1525 size -= 2 * priv->margin;
1526 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1527 size += priv->column_spacing;
1529 size += priv->row_spacing;
1531 if (priv->columns > 0)
1533 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1536 *min_items = priv->columns;
1538 *max_items = priv->columns;
1542 int n_items = gtk_icon_view_get_n_items (icon_view);
1545 *min_items = (n_items + priv->columns - 1) / priv->columns;
1547 *max_items = (n_items + priv->columns - 1) / priv->columns;
1552 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1554 minimum += priv->column_spacing;
1555 natural += priv->column_spacing;
1559 minimum += priv->row_spacing;
1560 natural += priv->row_spacing;
1565 if (size <= minimum)
1568 *max_items = size / minimum;
1573 if (size <= natural)
1576 *min_items = size / natural;
1582 *min_item_size = size / *min_items;
1583 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1584 *min_item_size -= priv->column_spacing;
1586 *min_item_size -= priv->row_spacing;
1587 *min_item_size = MIN (*min_item_size, natural);
1588 *min_item_size -= 2 * priv->item_padding;
1593 *max_item_size = size / *max_items;
1594 if (orientation == GTK_ORIENTATION_HORIZONTAL)
1595 *max_item_size -= priv->column_spacing;
1597 *max_item_size -= priv->row_spacing;
1598 *max_item_size = MIN (*max_item_size, natural);
1599 *max_item_size -= 2 * priv->item_padding;
1603 static GtkSizeRequestMode
1604 gtk_icon_view_get_request_mode (GtkWidget *widget)
1606 return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1610 gtk_icon_view_get_preferred_width (GtkWidget *widget,
1614 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1615 GtkIconViewPrivate *priv = icon_view->priv;
1616 int item_min, item_nat;
1618 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, -1, &item_min, &item_nat);
1620 if (priv->columns > 0)
1622 *minimum = item_min * priv->columns + priv->column_spacing * (priv->columns - 1);
1623 *natural = item_nat * priv->columns + priv->column_spacing * (priv->columns - 1);
1627 int n_items = gtk_icon_view_get_n_items (icon_view);
1636 *minimum = item_min;
1637 *natural = item_nat * n_items + priv->column_spacing * (n_items - 1);
1641 *minimum += 2 * priv->margin;
1642 *natural += 2 * priv->margin;
1646 gtk_icon_view_get_preferred_width_for_height (GtkWidget *widget,
1651 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1652 GtkIconViewPrivate *priv = icon_view->priv;
1653 int item_min, item_nat, rows, row_height, n_items;
1655 gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_VERTICAL, height, &rows, &row_height, NULL, NULL);
1656 n_items = gtk_icon_view_get_n_items (icon_view);
1658 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, row_height, &item_min, &item_nat);
1659 *minimum = item_min * ((n_items + rows - 1) / rows);
1660 *natural = item_nat * ((n_items + rows - 1) / rows);
1662 *minimum += 2 * priv->margin;
1663 *natural += 2 * priv->margin;
1667 gtk_icon_view_get_preferred_height (GtkWidget *widget,
1671 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1672 GtkIconViewPrivate *priv = icon_view->priv;
1673 int item_min, item_nat, n_items;
1675 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, -1, &item_min, &item_nat);
1676 n_items = gtk_icon_view_get_n_items (icon_view);
1678 if (priv->columns > 0)
1680 int n_rows = (n_items + priv->columns - 1) / priv->columns;
1682 *minimum = item_min * n_rows + priv->row_spacing * (n_rows - 1);
1683 *natural = item_nat * n_rows + priv->row_spacing * (n_rows - 1);
1694 *minimum = item_min;
1695 *natural = item_nat * n_items + priv->row_spacing * (n_items - 1);
1699 *minimum += 2 * priv->margin;
1700 *natural += 2 * priv->margin;
1704 gtk_icon_view_get_preferred_height_for_width (GtkWidget *widget,
1709 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1710 GtkIconViewPrivate *priv = icon_view->priv;
1711 int item_min, item_nat, columns, column_width, n_items;
1713 gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_HORIZONTAL, width, NULL, NULL, &columns, &column_width);
1714 n_items = gtk_icon_view_get_n_items (icon_view);
1716 gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, column_width, &item_min, &item_nat);
1717 *minimum = (item_min + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1718 *natural = (item_nat + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1720 *minimum += 2 * priv->margin;
1721 *natural += 2 * priv->margin;
1725 gtk_icon_view_allocate_children (GtkIconView *icon_view)
1729 for (list = icon_view->priv->children; list; list = list->next)
1731 GtkIconViewChild *child = list->data;
1733 /* totally ignore our child's requisition */
1734 gtk_widget_size_allocate (child->widget, &child->area);
1739 gtk_icon_view_size_allocate (GtkWidget *widget,
1740 GtkAllocation *allocation)
1742 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1744 gtk_widget_set_allocation (widget, allocation);
1746 gtk_icon_view_layout (icon_view);
1748 if (gtk_widget_get_realized (widget))
1750 gdk_window_move_resize (gtk_widget_get_window (widget),
1751 allocation->x, allocation->y,
1752 allocation->width, allocation->height);
1753 gdk_window_resize (icon_view->priv->bin_window,
1754 MAX (icon_view->priv->width, allocation->width),
1755 MAX (icon_view->priv->height, allocation->height));
1758 gtk_icon_view_allocate_children (icon_view);
1760 /* Delay signal emission */
1761 g_object_freeze_notify (G_OBJECT (icon_view->priv->hadjustment));
1762 g_object_freeze_notify (G_OBJECT (icon_view->priv->vadjustment));
1764 gtk_icon_view_set_hadjustment_values (icon_view);
1765 gtk_icon_view_set_vadjustment_values (icon_view);
1767 if (gtk_widget_get_realized (widget) &&
1768 icon_view->priv->scroll_to_path)
1771 path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
1772 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1773 icon_view->priv->scroll_to_path = NULL;
1775 gtk_icon_view_scroll_to_path (icon_view, path,
1776 icon_view->priv->scroll_to_use_align,
1777 icon_view->priv->scroll_to_row_align,
1778 icon_view->priv->scroll_to_col_align);
1779 gtk_tree_path_free (path);
1782 /* Emit any pending signals now */
1783 g_object_thaw_notify (G_OBJECT (icon_view->priv->hadjustment));
1784 g_object_thaw_notify (G_OBJECT (icon_view->priv->vadjustment));
1788 gtk_icon_view_draw (GtkWidget *widget,
1791 GtkIconView *icon_view;
1795 GtkIconViewDropPosition dest_pos;
1796 GtkIconViewItem *dest_item = NULL;
1798 icon_view = GTK_ICON_VIEW (widget);
1800 if (!gtk_cairo_should_draw_window (cr, icon_view->priv->bin_window))
1805 gtk_cairo_transform_to_window (cr, widget, icon_view->priv->bin_window);
1807 cairo_set_line_width (cr, 1.);
1809 gtk_icon_view_get_drag_dest_item (icon_view, &path, &dest_pos);
1813 dest_index = gtk_tree_path_get_indices (path)[0];
1814 gtk_tree_path_free (path);
1819 for (icons = icon_view->priv->items; icons; icons = icons->next)
1821 GtkIconViewItem *item = icons->data;
1822 GdkRectangle paint_area;
1824 paint_area.x = item->cell_area.x - icon_view->priv->item_padding;
1825 paint_area.y = item->cell_area.y - icon_view->priv->item_padding;
1826 paint_area.width = item->cell_area.width + icon_view->priv->item_padding * 2;
1827 paint_area.height = item->cell_area.height + icon_view->priv->item_padding * 2;
1831 cairo_rectangle (cr, paint_area.x, paint_area.y, paint_area.width, paint_area.height);
1834 if (gdk_cairo_get_clip_rectangle (cr, NULL))
1836 gtk_icon_view_paint_item (icon_view, cr, item,
1837 item->cell_area.x, item->cell_area.y,
1838 icon_view->priv->draw_focus);
1840 if (dest_index == item->index)
1848 dest_pos != GTK_ICON_VIEW_NO_DROP)
1850 GtkStyleContext *context;
1851 GdkRectangle rect = { 0 };
1853 context = gtk_widget_get_style_context (widget);
1857 case GTK_ICON_VIEW_DROP_INTO:
1858 rect = dest_item->cell_area;
1860 case GTK_ICON_VIEW_DROP_ABOVE:
1861 rect.x = dest_item->cell_area.x;
1862 rect.y = dest_item->cell_area.y - 1;
1863 rect.width = dest_item->cell_area.width;
1866 case GTK_ICON_VIEW_DROP_LEFT:
1867 rect.x = dest_item->cell_area.x - 1;
1868 rect.y = dest_item->cell_area.y;
1870 rect.height = dest_item->cell_area.height;
1872 case GTK_ICON_VIEW_DROP_BELOW:
1873 rect.x = dest_item->cell_area.x;
1874 rect.y = dest_item->cell_area.y + dest_item->cell_area.height - 1;
1875 rect.width = dest_item->cell_area.width;
1878 case GTK_ICON_VIEW_DROP_RIGHT:
1879 rect.x = dest_item->cell_area.x + dest_item->cell_area.width - 1;
1880 rect.y = dest_item->cell_area.y;
1882 rect.height = dest_item->cell_area.height;
1883 case GTK_ICON_VIEW_NO_DROP: ;
1887 gtk_render_focus (context, cr,
1889 rect.width, rect.height);
1892 if (icon_view->priv->doing_rubberband)
1893 gtk_icon_view_paint_rubberband (icon_view, cr);
1897 return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->draw (widget, cr);
1901 rubberband_scroll_timeout (gpointer data)
1903 GtkIconView *icon_view = data;
1905 gtk_adjustment_set_value (icon_view->priv->vadjustment,
1906 gtk_adjustment_get_value (icon_view->priv->vadjustment) +
1907 icon_view->priv->scroll_value_diff);
1909 gtk_icon_view_update_rubberband (icon_view);
1915 gtk_icon_view_motion (GtkWidget *widget,
1916 GdkEventMotion *event)
1918 GtkAllocation allocation;
1919 GtkIconView *icon_view;
1922 icon_view = GTK_ICON_VIEW (widget);
1924 gtk_icon_view_maybe_begin_drag (icon_view, event);
1926 if (icon_view->priv->doing_rubberband)
1928 gtk_icon_view_update_rubberband (widget);
1930 abs_y = event->y - icon_view->priv->height *
1931 (gtk_adjustment_get_value (icon_view->priv->vadjustment) /
1932 (gtk_adjustment_get_upper (icon_view->priv->vadjustment) -
1933 gtk_adjustment_get_lower (icon_view->priv->vadjustment)));
1935 gtk_widget_get_allocation (widget, &allocation);
1937 if (abs_y < 0 || abs_y > allocation.height)
1940 icon_view->priv->scroll_value_diff = abs_y;
1942 icon_view->priv->scroll_value_diff = abs_y - allocation.height;
1944 icon_view->priv->event_last_x = event->x;
1945 icon_view->priv->event_last_y = event->y;
1947 if (icon_view->priv->scroll_timeout_id == 0)
1948 icon_view->priv->scroll_timeout_id = gdk_threads_add_timeout (30, rubberband_scroll_timeout,
1952 remove_scroll_timeout (icon_view);
1956 GtkIconViewItem *item, *last_prelight_item;
1957 GtkCellRenderer *cell = NULL;
1959 last_prelight_item = icon_view->priv->last_prelight;
1960 item = _gtk_icon_view_get_item_at_coords (icon_view,
1967 item->prelight = TRUE;
1968 gtk_icon_view_queue_draw_item (icon_view, item);
1971 if (last_prelight_item != NULL &&
1972 last_prelight_item != item)
1974 last_prelight_item->prelight = FALSE;
1975 gtk_icon_view_queue_draw_item (icon_view,
1976 icon_view->priv->last_prelight);
1979 icon_view->priv->last_prelight = item;
1986 gtk_icon_view_remove (GtkContainer *container,
1989 GtkIconView *icon_view;
1990 GtkIconViewChild *child = NULL;
1993 icon_view = GTK_ICON_VIEW (container);
1995 tmp_list = icon_view->priv->children;
1998 child = tmp_list->data;
1999 if (child->widget == widget)
2001 gtk_widget_unparent (widget);
2003 icon_view->priv->children = g_list_remove_link (icon_view->priv->children, tmp_list);
2004 g_list_free_1 (tmp_list);
2009 tmp_list = tmp_list->next;
2014 gtk_icon_view_forall (GtkContainer *container,
2015 gboolean include_internals,
2016 GtkCallback callback,
2017 gpointer callback_data)
2019 GtkIconView *icon_view;
2020 GtkIconViewChild *child = NULL;
2023 icon_view = GTK_ICON_VIEW (container);
2025 tmp_list = icon_view->priv->children;
2028 child = tmp_list->data;
2029 tmp_list = tmp_list->next;
2031 (* callback) (child->widget, callback_data);
2036 gtk_icon_view_item_selected_changed (GtkIconView *icon_view,
2037 GtkIconViewItem *item)
2040 AtkObject *item_obj;
2042 obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
2045 item_obj = atk_object_ref_accessible_child (obj, item->index);
2046 if (item_obj != NULL)
2048 atk_object_notify_state_change (item_obj, ATK_STATE_SELECTED, item->selected);
2049 g_object_unref (item_obj);
2055 gtk_icon_view_add_editable (GtkCellArea *area,
2056 GtkCellRenderer *renderer,
2057 GtkCellEditable *editable,
2058 GdkRectangle *cell_area,
2060 GtkIconView *icon_view)
2062 GtkIconViewChild *child;
2063 GtkWidget *widget = GTK_WIDGET (editable);
2065 child = g_new (GtkIconViewChild, 1);
2067 child->widget = widget;
2068 child->area.x = cell_area->x;
2069 child->area.y = cell_area->y;
2070 child->area.width = cell_area->width;
2071 child->area.height = cell_area->height;
2073 icon_view->priv->children = g_list_append (icon_view->priv->children, child);
2075 if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2076 gtk_widget_set_parent_window (child->widget, icon_view->priv->bin_window);
2078 gtk_widget_set_parent (widget, GTK_WIDGET (icon_view));
2082 gtk_icon_view_remove_editable (GtkCellArea *area,
2083 GtkCellRenderer *renderer,
2084 GtkCellEditable *editable,
2085 GtkIconView *icon_view)
2089 if (gtk_widget_has_focus (GTK_WIDGET (editable)))
2090 gtk_widget_grab_focus (GTK_WIDGET (icon_view));
2092 gtk_container_remove (GTK_CONTAINER (icon_view),
2093 GTK_WIDGET (editable));
2095 path = gtk_tree_path_new_from_string (gtk_cell_area_get_current_path_string (area));
2096 gtk_icon_view_queue_draw_path (icon_view, path);
2097 gtk_tree_path_free (path);
2101 * gtk_icon_view_set_cursor:
2102 * @icon_view: A #GtkIconView
2103 * @path: A #GtkTreePath
2104 * @cell: (allow-none): One of the cell renderers of @icon_view, or %NULL
2105 * @start_editing: %TRUE if the specified cell should start being edited.
2107 * Sets the current keyboard focus to be at @path, and selects it. This is
2108 * useful when you want to focus the user's attention on a particular item.
2109 * If @cell is not %NULL, then focus is given to the cell specified by
2110 * it. Additionally, if @start_editing is %TRUE, then editing should be
2111 * started in the specified cell.
2113 * This function is often followed by <literal>gtk_widget_grab_focus
2114 * (icon_view)</literal> in order to give keyboard focus to the widget.
2115 * Please note that editing can only happen when the widget is realized.
2120 gtk_icon_view_set_cursor (GtkIconView *icon_view,
2122 GtkCellRenderer *cell,
2123 gboolean start_editing)
2125 GtkIconViewItem *item = NULL;
2127 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
2128 g_return_if_fail (path != NULL);
2129 g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
2131 if (icon_view->priv->cell_area)
2132 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2134 if (gtk_tree_path_get_depth (path) == 1)
2135 item = g_list_nth_data (icon_view->priv->items,
2136 gtk_tree_path_get_indices(path)[0]);
2141 _gtk_icon_view_set_cursor_item (icon_view, item, cell);
2142 gtk_icon_view_scroll_to_path (icon_view, path, FALSE, 0.0, 0.0);
2144 if (start_editing &&
2145 icon_view->priv->cell_area)
2147 GtkCellAreaContext *context;
2149 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2150 _gtk_icon_view_set_cell_data (icon_view, item);
2151 gtk_cell_area_activate (icon_view->priv->cell_area, context,
2152 GTK_WIDGET (icon_view), &item->cell_area,
2153 0 /* XXX flags */, TRUE);
2158 * gtk_icon_view_get_cursor:
2159 * @icon_view: A #GtkIconView
2160 * @path: (out) (allow-none): Return location for the current cursor path,
2162 * @cell: (out) (allow-none): Return location the current focus cell, or %NULL
2164 * Fills in @path and @cell with the current cursor path and cell.
2165 * If the cursor isn't currently set, then *@path will be %NULL.
2166 * If no cell currently has focus, then *@cell will be %NULL.
2168 * The returned #GtkTreePath must be freed with gtk_tree_path_free().
2170 * Return value: %TRUE if the cursor is set.
2175 gtk_icon_view_get_cursor (GtkIconView *icon_view,
2177 GtkCellRenderer **cell)
2179 GtkIconViewItem *item;
2181 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
2183 item = icon_view->priv->cursor_item;
2188 *path = gtk_tree_path_new_from_indices (item->index, -1);
2193 if (cell != NULL && item != NULL && icon_view->priv->cell_area != NULL)
2194 *cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
2196 return (item != NULL);
2200 gtk_icon_view_button_press (GtkWidget *widget,
2201 GdkEventButton *event)
2203 GtkIconView *icon_view;
2204 GtkIconViewItem *item;
2205 gboolean dirty = FALSE;
2206 GtkCellRenderer *cell = NULL, *cursor_cell = NULL;
2208 icon_view = GTK_ICON_VIEW (widget);
2210 if (event->window != icon_view->priv->bin_window)
2213 if (!gtk_widget_has_focus (widget))
2214 gtk_widget_grab_focus (widget);
2216 if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_BUTTON_PRESS)
2218 GdkModifierType extend_mod_mask;
2219 GdkModifierType modify_mod_mask;
2222 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_EXTEND_SELECTION);
2225 gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_MODIFY_SELECTION);
2227 item = _gtk_icon_view_get_item_at_coords (icon_view,
2233 * We consider only the the cells' area as the item area if the
2234 * item is not selected, but if it *is* selected, the complete
2235 * selection rectangle is considered to be part of the item.
2237 if (item != NULL && (cell != NULL || item->selected))
2241 if (gtk_cell_renderer_is_activatable (cell))
2245 gtk_icon_view_scroll_to_item (icon_view, item);
2247 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2249 _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2251 else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
2252 (event->state & extend_mod_mask))
2254 gtk_icon_view_unselect_all_internal (icon_view);
2256 _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2257 if (!icon_view->priv->anchor_item)
2258 icon_view->priv->anchor_item = item;
2260 gtk_icon_view_select_all_between (icon_view,
2261 icon_view->priv->anchor_item,
2267 if ((icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE ||
2268 ((icon_view->priv->selection_mode == GTK_SELECTION_SINGLE) && item->selected)) &&
2269 (event->state & modify_mod_mask))
2271 item->selected = !item->selected;
2272 gtk_icon_view_queue_draw_item (icon_view, item);
2277 gtk_icon_view_unselect_all_internal (icon_view);
2279 item->selected = TRUE;
2280 gtk_icon_view_queue_draw_item (icon_view, item);
2283 _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2284 icon_view->priv->anchor_item = item;
2287 /* Save press to possibly begin a drag */
2288 if (icon_view->priv->pressed_button < 0)
2290 icon_view->priv->pressed_button = event->button;
2291 icon_view->priv->press_start_x = event->x;
2292 icon_view->priv->press_start_y = event->y;
2295 if (!icon_view->priv->last_single_clicked)
2296 icon_view->priv->last_single_clicked = item;
2298 /* cancel the current editing, if it exists */
2299 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2301 if (cell != NULL && gtk_cell_renderer_is_activatable (cell))
2303 GtkCellAreaContext *context;
2305 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2307 _gtk_icon_view_set_cell_data (icon_view, item);
2308 gtk_cell_area_activate (icon_view->priv->cell_area, context,
2309 GTK_WIDGET (icon_view),
2310 &item->cell_area, 0/* XXX flags */, FALSE);
2315 if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
2316 !(event->state & modify_mod_mask))
2318 dirty = gtk_icon_view_unselect_all_internal (icon_view);
2321 if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
2322 gtk_icon_view_start_rubberbanding (icon_view, event->device, event->x, event->y);
2325 /* don't draw keyboard focus around an clicked-on item */
2326 icon_view->priv->draw_focus = FALSE;
2329 if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_2BUTTON_PRESS)
2331 item = _gtk_icon_view_get_item_at_coords (icon_view,
2336 if (item && item == icon_view->priv->last_single_clicked)
2340 path = gtk_tree_path_new_from_indices (item->index, -1);
2341 gtk_icon_view_item_activated (icon_view, path);
2342 gtk_tree_path_free (path);
2345 icon_view->priv->last_single_clicked = NULL;
2346 icon_view->priv->pressed_button = -1;
2350 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2352 return event->button == GDK_BUTTON_PRIMARY;
2356 gtk_icon_view_button_release (GtkWidget *widget,
2357 GdkEventButton *event)
2359 GtkIconView *icon_view;
2361 icon_view = GTK_ICON_VIEW (widget);
2363 if (icon_view->priv->pressed_button == event->button)
2364 icon_view->priv->pressed_button = -1;
2366 gtk_icon_view_stop_rubberbanding (icon_view);
2368 remove_scroll_timeout (icon_view);
2374 gtk_icon_view_key_press (GtkWidget *widget,
2377 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2379 if (icon_view->priv->doing_rubberband)
2381 if (event->keyval == GDK_KEY_Escape)
2382 gtk_icon_view_stop_rubberbanding (icon_view);
2387 return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2391 gtk_icon_view_key_release (GtkWidget *widget,
2394 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2396 if (icon_view->priv->doing_rubberband)
2399 return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2403 gtk_icon_view_update_rubberband (gpointer data)
2405 GtkIconView *icon_view;
2407 GdkRectangle old_area;
2408 GdkRectangle new_area;
2409 GdkRectangle common;
2410 cairo_region_t *invalid_region;
2412 icon_view = GTK_ICON_VIEW (data);
2414 gdk_window_get_device_position (icon_view->priv->bin_window,
2415 icon_view->priv->rubberband_device,
2421 old_area.x = MIN (icon_view->priv->rubberband_x1,
2422 icon_view->priv->rubberband_x2);
2423 old_area.y = MIN (icon_view->priv->rubberband_y1,
2424 icon_view->priv->rubberband_y2);
2425 old_area.width = ABS (icon_view->priv->rubberband_x2 -
2426 icon_view->priv->rubberband_x1) + 1;
2427 old_area.height = ABS (icon_view->priv->rubberband_y2 -
2428 icon_view->priv->rubberband_y1) + 1;
2430 new_area.x = MIN (icon_view->priv->rubberband_x1, x);
2431 new_area.y = MIN (icon_view->priv->rubberband_y1, y);
2432 new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
2433 new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
2435 invalid_region = cairo_region_create_rectangle (&old_area);
2436 cairo_region_union_rectangle (invalid_region, &new_area);
2438 gdk_rectangle_intersect (&old_area, &new_area, &common);
2439 if (common.width > 2 && common.height > 2)
2441 cairo_region_t *common_region;
2443 /* make sure the border is invalidated */
2449 common_region = cairo_region_create_rectangle (&common);
2451 cairo_region_subtract (invalid_region, common_region);
2452 cairo_region_destroy (common_region);
2455 gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
2457 cairo_region_destroy (invalid_region);
2459 icon_view->priv->rubberband_x2 = x;
2460 icon_view->priv->rubberband_y2 = y;
2462 gtk_icon_view_update_rubberband_selection (icon_view);
2466 gtk_icon_view_start_rubberbanding (GtkIconView *icon_view,
2473 if (icon_view->priv->rubberband_device)
2476 for (items = icon_view->priv->items; items; items = items->next)
2478 GtkIconViewItem *item = items->data;
2480 item->selected_before_rubberbanding = item->selected;
2483 icon_view->priv->rubberband_x1 = x;
2484 icon_view->priv->rubberband_y1 = y;
2485 icon_view->priv->rubberband_x2 = x;
2486 icon_view->priv->rubberband_y2 = y;
2488 icon_view->priv->doing_rubberband = TRUE;
2489 icon_view->priv->rubberband_device = device;
2491 gtk_device_grab_add (GTK_WIDGET (icon_view), device, TRUE);
2495 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
2497 if (!icon_view->priv->doing_rubberband)
2500 gtk_device_grab_remove (GTK_WIDGET (icon_view),
2501 icon_view->priv->rubberband_device);
2503 icon_view->priv->doing_rubberband = FALSE;
2504 icon_view->priv->rubberband_device = NULL;
2506 gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2510 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
2513 gint x, y, width, height;
2514 gboolean dirty = FALSE;
2516 x = MIN (icon_view->priv->rubberband_x1,
2517 icon_view->priv->rubberband_x2);
2518 y = MIN (icon_view->priv->rubberband_y1,
2519 icon_view->priv->rubberband_y2);
2520 width = ABS (icon_view->priv->rubberband_x1 -
2521 icon_view->priv->rubberband_x2);
2522 height = ABS (icon_view->priv->rubberband_y1 -
2523 icon_view->priv->rubberband_y2);
2525 for (items = icon_view->priv->items; items; items = items->next)
2527 GtkIconViewItem *item = items->data;
2531 is_in = gtk_icon_view_item_hit_test (icon_view, item,
2532 x, y, width, height);
2534 selected = is_in ^ item->selected_before_rubberbanding;
2536 if (item->selected != selected)
2538 item->selected = selected;
2540 gtk_icon_view_queue_draw_item (icon_view, item);
2545 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2550 GdkRectangle hit_rect;
2555 hit_test (GtkCellRenderer *renderer,
2556 const GdkRectangle *cell_area,
2557 const GdkRectangle *cell_background,
2560 if (MIN (data->hit_rect.x + data->hit_rect.width, cell_area->x + cell_area->width) -
2561 MAX (data->hit_rect.x, cell_area->x) > 0 &&
2562 MIN (data->hit_rect.y + data->hit_rect.height, cell_area->y + cell_area->height) -
2563 MAX (data->hit_rect.y, cell_area->y) > 0)
2566 return (data->hit != FALSE);
2570 gtk_icon_view_item_hit_test (GtkIconView *icon_view,
2571 GtkIconViewItem *item,
2577 HitTestData data = { { x, y, width, height }, FALSE };
2578 GtkCellAreaContext *context;
2579 GdkRectangle *item_area = &item->cell_area;
2581 if (MIN (x + width, item_area->x + item_area->width) - MAX (x, item_area->x) <= 0 ||
2582 MIN (y + height, item_area->y + item_area->height) - MAX (y, item_area->y) <= 0)
2585 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2587 _gtk_icon_view_set_cell_data (icon_view, item);
2588 gtk_cell_area_foreach_alloc (icon_view->priv->cell_area, context,
2589 GTK_WIDGET (icon_view),
2590 item_area, item_area,
2591 (GtkCellAllocCallback)hit_test, &data);
2597 gtk_icon_view_unselect_all_internal (GtkIconView *icon_view)
2599 gboolean dirty = FALSE;
2602 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2605 for (items = icon_view->priv->items; items; items = items->next)
2607 GtkIconViewItem *item = items->data;
2611 item->selected = FALSE;
2613 gtk_icon_view_queue_draw_item (icon_view, item);
2614 gtk_icon_view_item_selected_changed (icon_view, item);
2622 /* GtkIconView signals */
2624 gtk_icon_view_real_select_all (GtkIconView *icon_view)
2626 gtk_icon_view_select_all (icon_view);
2630 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
2632 gtk_icon_view_unselect_all (icon_view);
2636 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
2638 gtk_icon_view_unselect_all (icon_view);
2640 if (icon_view->priv->cursor_item != NULL)
2641 _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2645 gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view)
2648 GtkCellAreaContext *context;
2650 if (!icon_view->priv->cursor_item)
2653 context = g_ptr_array_index (icon_view->priv->row_contexts, icon_view->priv->cursor_item->row);
2655 _gtk_icon_view_set_cell_data (icon_view, icon_view->priv->cursor_item);
2656 gtk_cell_area_activate (icon_view->priv->cell_area, context,
2657 GTK_WIDGET (icon_view),
2658 &icon_view->priv->cursor_item->cell_area,
2662 path = gtk_tree_path_new_from_indices (icon_view->priv->cursor_item->index, -1);
2663 gtk_icon_view_item_activated (icon_view, path);
2664 gtk_tree_path_free (path);
2670 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
2672 if (!icon_view->priv->cursor_item)
2675 switch (icon_view->priv->selection_mode)
2677 case GTK_SELECTION_NONE:
2679 case GTK_SELECTION_BROWSE:
2680 _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2682 case GTK_SELECTION_SINGLE:
2683 if (icon_view->priv->cursor_item->selected)
2684 _gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
2686 _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2688 case GTK_SELECTION_MULTIPLE:
2689 icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
2690 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2692 gtk_icon_view_item_selected_changed (icon_view, icon_view->priv->cursor_item);
2693 gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2699 gtk_icon_view_set_hadjustment_values (GtkIconView *icon_view)
2701 GtkAllocation allocation;
2702 GtkAdjustment *adj = icon_view->priv->hadjustment;
2703 gdouble old_page_size;
2709 gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2711 old_value = gtk_adjustment_get_value (adj);
2712 old_upper = gtk_adjustment_get_upper (adj);
2713 old_page_size = gtk_adjustment_get_page_size (adj);
2714 new_upper = MAX (allocation.width, icon_view->priv->width);
2716 if (gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL)
2718 /* Make sure no scrolling occurs for RTL locales also (if possible) */
2719 /* Quick explanation:
2720 * In LTR locales, leftmost portion of visible rectangle should stay
2721 * fixed, which means left edge of scrollbar thumb should remain fixed
2722 * and thus adjustment's value should stay the same.
2724 * In RTL locales, we want to keep rightmost portion of visible
2725 * rectangle fixed. This means right edge of thumb should remain fixed.
2726 * In this case, upper - value - page_size should remain constant.
2728 new_value = (new_upper - allocation.width) -
2729 (old_upper - old_value - old_page_size);
2730 new_value = CLAMP (new_value, 0, new_upper - allocation.width);
2733 new_value = CLAMP (old_value, 0, new_upper - allocation.width);
2735 gtk_adjustment_configure (adj,
2739 allocation.width * 0.1,
2740 allocation.width * 0.9,
2745 gtk_icon_view_set_vadjustment_values (GtkIconView *icon_view)
2747 GtkAllocation allocation;
2748 GtkAdjustment *adj = icon_view->priv->vadjustment;
2750 gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2752 gtk_adjustment_configure (adj,
2753 gtk_adjustment_get_value (adj),
2755 MAX (allocation.height, icon_view->priv->height),
2756 allocation.height * 0.1,
2757 allocation.height * 0.9,
2762 gtk_icon_view_set_hadjustment (GtkIconView *icon_view,
2763 GtkAdjustment *adjustment)
2765 GtkIconViewPrivate *priv = icon_view->priv;
2767 if (adjustment && priv->hadjustment == adjustment)
2770 if (priv->hadjustment != NULL)
2772 g_signal_handlers_disconnect_matched (priv->hadjustment,
2773 G_SIGNAL_MATCH_DATA,
2774 0, 0, NULL, NULL, icon_view);
2775 g_object_unref (priv->hadjustment);
2779 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2782 g_signal_connect (adjustment, "value-changed",
2783 G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2784 priv->hadjustment = g_object_ref_sink (adjustment);
2785 gtk_icon_view_set_hadjustment_values (icon_view);
2787 g_object_notify (G_OBJECT (icon_view), "hadjustment");
2791 gtk_icon_view_set_vadjustment (GtkIconView *icon_view,
2792 GtkAdjustment *adjustment)
2794 GtkIconViewPrivate *priv = icon_view->priv;
2796 if (adjustment && priv->vadjustment == adjustment)
2799 if (priv->vadjustment != NULL)
2801 g_signal_handlers_disconnect_matched (priv->vadjustment,
2802 G_SIGNAL_MATCH_DATA,
2803 0, 0, NULL, NULL, icon_view);
2804 g_object_unref (priv->vadjustment);
2808 adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2811 g_signal_connect (adjustment, "value-changed",
2812 G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2813 priv->vadjustment = g_object_ref_sink (adjustment);
2814 gtk_icon_view_set_vadjustment_values (icon_view);
2816 g_object_notify (G_OBJECT (icon_view), "vadjustment");
2820 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
2821 GtkIconView *icon_view)
2823 GtkIconViewPrivate *priv = icon_view->priv;
2825 if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2827 gdk_window_move (priv->bin_window,
2828 - gtk_adjustment_get_value (priv->hadjustment),
2829 - gtk_adjustment_get_value (priv->vadjustment));
2831 if (icon_view->priv->doing_rubberband)
2832 gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
2834 _gtk_icon_view_accessible_adjustment_changed (icon_view);
2839 compare_sizes (gconstpointer p1,
2843 return GPOINTER_TO_INT (((const GtkRequestedSize *) p1)->data)
2844 - GPOINTER_TO_INT (((const GtkRequestedSize *) p2)->data);
2848 gtk_icon_view_layout (GtkIconView *icon_view)
2850 GtkIconViewPrivate *priv = icon_view->priv;
2851 GtkWidget *widget = GTK_WIDGET (icon_view);
2853 gint item_width; /* this doesn't include item_padding */
2854 gint n_columns, n_rows, n_items;
2856 GtkRequestedSize *sizes;
2859 rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2862 n_items = gtk_icon_view_get_n_items (icon_view);
2864 gtk_icon_view_compute_n_items_for_size (icon_view,
2865 GTK_ORIENTATION_HORIZONTAL,
2866 gtk_widget_get_allocated_width (widget),
2868 &n_columns, &item_width);
2869 n_rows = (n_items + n_columns - 1) / n_columns;
2873 /* We might need vertical scrolling here */
2874 int min_width = item_width + 2 * priv->item_padding + 2 * priv->margin;
2875 priv->width = MAX (min_width, gtk_widget_get_allocated_width (widget));
2879 priv->width = gtk_widget_get_allocated_width (widget);
2882 /* Clear the per row contexts */
2883 g_ptr_array_set_size (icon_view->priv->row_contexts, 0);
2885 gtk_cell_area_context_reset (priv->cell_area_context);
2886 /* because layouting is complicated. We designed an API
2887 * that is O(N²) and nonsensical.
2888 * And we're proud of it. */
2889 for (items = priv->items; items; items = items->next)
2891 _gtk_icon_view_set_cell_data (icon_view, items->data);
2892 gtk_cell_area_get_preferred_width (priv->cell_area,
2893 priv->cell_area_context,
2898 sizes = g_newa (GtkRequestedSize, n_rows);
2899 items = priv->items;
2900 priv->height = priv->margin;
2902 /* Collect the heights for all rows */
2903 for (row = 0; row < n_rows; row++)
2905 GtkCellAreaContext *context = gtk_cell_area_copy_context (priv->cell_area, priv->cell_area_context);
2906 g_ptr_array_add (priv->row_contexts, context);
2908 for (col = 0; col < n_columns && items; col++, items = items->next)
2910 GtkIconViewItem *item = items->data;
2912 _gtk_icon_view_set_cell_data (icon_view, item);
2913 gtk_cell_area_get_preferred_height_for_width (priv->cell_area,
2920 sizes[row].data = GINT_TO_POINTER (row);
2921 gtk_cell_area_context_get_preferred_height_for_width (context,
2923 &sizes[row].minimum_size,
2924 &sizes[row].natural_size);
2925 priv->height += sizes[row].minimum_size + 2 * priv->item_padding + priv->row_spacing;
2928 priv->height -= priv->row_spacing;
2929 priv->height += priv->margin;
2930 priv->height = MIN (priv->height, gtk_widget_get_allocated_height (widget));
2932 gtk_distribute_natural_allocation (gtk_widget_get_allocated_height (widget) - priv->height,
2936 /* Actually allocate the rows */
2937 g_qsort_with_data (sizes, n_rows, sizeof (GtkRequestedSize), compare_sizes, NULL);
2939 items = priv->items;
2940 priv->height = priv->margin;
2942 for (row = 0; row < n_rows; row++)
2944 GtkCellAreaContext *context = g_ptr_array_index (priv->row_contexts, row);
2945 gtk_cell_area_context_allocate (context, item_width, sizes[row].minimum_size);
2947 priv->height += priv->item_padding;
2949 for (col = 0; col < n_columns && items; col++, items = items->next)
2951 GtkIconViewItem *item = items->data;
2953 item->cell_area.x = priv->margin + (col * 2 + 1) * priv->item_padding + col * (priv->column_spacing + item_width);
2954 item->cell_area.width = item_width;
2955 item->cell_area.y = priv->height;
2956 item->cell_area.height = sizes[row].minimum_size;
2961 item->cell_area.x = priv->width - item_width - item->cell_area.x;
2962 item->col = n_columns - 1 - col;
2966 priv->height += sizes[row].minimum_size + priv->item_padding + priv->row_spacing;
2969 priv->height -= priv->row_spacing;
2970 priv->height += priv->margin;
2971 priv->height = MAX (priv->height, gtk_widget_get_allocated_height (widget));
2975 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
2977 /* Clear all item sizes */
2978 g_list_foreach (icon_view->priv->items,
2979 (GFunc)gtk_icon_view_item_invalidate_size, NULL);
2981 /* Re-layout the items */
2982 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
2986 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
2988 item->cell_area.width = -1;
2989 item->cell_area.height = -1;
2993 gtk_icon_view_paint_item (GtkIconView *icon_view,
2995 GtkIconViewItem *item,
2998 gboolean draw_focus)
3000 GdkRectangle cell_area;
3001 GtkStateFlags state = 0;
3002 GtkCellRendererState flags = 0;
3003 GtkStyleContext *style_context;
3004 GtkWidget *widget = GTK_WIDGET (icon_view);
3005 GtkIconViewPrivate *priv = icon_view->priv;
3006 GtkCellAreaContext *context;
3008 if (priv->model == NULL || item->cell_area.width <= 0 || item->cell_area.height <= 0)
3011 _gtk_icon_view_set_cell_data (icon_view, item);
3013 style_context = gtk_widget_get_style_context (widget);
3014 state = gtk_widget_get_state_flags (widget);
3016 gtk_style_context_save (style_context);
3017 gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_VIEW);
3018 gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_CELL);
3020 state &= ~(GTK_STATE_FLAG_SELECTED | GTK_STATE_FLAG_PRELIGHT);
3024 if ((state & GTK_STATE_FLAG_FOCUSED) &&
3025 item == icon_view->priv->cursor_item)
3027 flags |= GTK_CELL_RENDERER_FOCUSED;
3030 state |= GTK_STATE_FLAG_SELECTED;
3031 flags |= GTK_CELL_RENDERER_SELECTED;
3036 state |= GTK_STATE_FLAG_PRELIGHT;
3037 flags |= GTK_CELL_RENDERER_PRELIT;
3040 gtk_style_context_set_state (style_context, state);
3044 gtk_render_background (style_context, cr,
3045 x - icon_view->priv->item_padding,
3046 y - icon_view->priv->item_padding,
3047 item->cell_area.width + icon_view->priv->item_padding * 2,
3048 item->cell_area.height + icon_view->priv->item_padding * 2);
3049 gtk_render_frame (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);
3058 cell_area.width = item->cell_area.width;
3059 cell_area.height = item->cell_area.height;
3061 context = g_ptr_array_index (priv->row_contexts, item->row);
3062 gtk_cell_area_render (priv->cell_area, context,
3063 widget, cr, &cell_area, &cell_area, flags,
3066 gtk_style_context_restore (style_context);
3070 gtk_icon_view_paint_rubberband (GtkIconView *icon_view,
3073 GtkStyleContext *context;
3078 rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
3079 rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
3080 rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
3081 rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
3083 context = gtk_widget_get_style_context (GTK_WIDGET (icon_view));
3085 gtk_style_context_save (context);
3086 gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
3088 gdk_cairo_rectangle (cr, &rect);
3091 gtk_render_background (context, cr,
3093 rect.width, rect.height);
3094 gtk_render_frame (context, cr,
3096 rect.width, rect.height);
3098 gtk_style_context_restore (context);
3103 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
3109 index = gtk_tree_path_get_indices (path)[0];
3111 for (l = icon_view->priv->items; l; l = l->next)
3113 GtkIconViewItem *item = l->data;
3115 if (item->index == index)
3117 gtk_icon_view_queue_draw_item (icon_view, item);
3124 gtk_icon_view_queue_draw_item (GtkIconView *icon_view,
3125 GtkIconViewItem *item)
3128 GdkRectangle *item_area = &item->cell_area;
3130 rect.x = item_area->x - icon_view->priv->item_padding;
3131 rect.y = item_area->y - icon_view->priv->item_padding;
3132 rect.width = item_area->width + icon_view->priv->item_padding * 2;
3133 rect.height = item_area->height + icon_view->priv->item_padding * 2;
3135 if (icon_view->priv->bin_window)
3136 gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
3140 _gtk_icon_view_set_cursor_item (GtkIconView *icon_view,
3141 GtkIconViewItem *item,
3142 GtkCellRenderer *cursor_cell)
3145 AtkObject *item_obj;
3146 AtkObject *cursor_item_obj;
3148 /* When hitting this path from keynav, the focus cell is
3149 * already set, we dont need to notify the atk object
3150 * but we still need to queue the draw here (in the case
3151 * that the focus cell changes but not the cursor item).
3153 gtk_icon_view_queue_draw_item (icon_view, item);
3155 if (icon_view->priv->cursor_item == item &&
3156 (cursor_cell == NULL || cursor_cell == gtk_cell_area_get_focus_cell (icon_view->priv->cell_area)))
3159 obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3160 if (icon_view->priv->cursor_item != NULL)
3162 gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3165 cursor_item_obj = atk_object_ref_accessible_child (obj, icon_view->priv->cursor_item->index);
3166 if (cursor_item_obj != NULL)
3167 atk_object_notify_state_change (cursor_item_obj, ATK_STATE_FOCUSED, FALSE);
3170 icon_view->priv->cursor_item = item;
3173 gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cursor_cell);
3176 /* Make sure there is a cell in focus initially */
3177 if (!gtk_cell_area_get_focus_cell (icon_view->priv->cell_area))
3178 gtk_cell_area_focus (icon_view->priv->cell_area, GTK_DIR_TAB_FORWARD);
3181 /* Notify that accessible focus object has changed */
3182 item_obj = atk_object_ref_accessible_child (obj, item->index);
3184 if (item_obj != NULL)
3186 atk_focus_tracker_notify (item_obj);
3187 atk_object_notify_state_change (item_obj, ATK_STATE_FOCUSED, TRUE);
3188 g_object_unref (item_obj);
3193 static GtkIconViewItem *
3194 gtk_icon_view_item_new (void)
3196 GtkIconViewItem *item;
3198 item = g_slice_new0 (GtkIconViewItem);
3200 item->cell_area.width = -1;
3201 item->cell_area.height = -1;
3207 gtk_icon_view_item_free (GtkIconViewItem *item)
3209 g_return_if_fail (item != NULL);
3211 g_slice_free (GtkIconViewItem, item);
3215 _gtk_icon_view_get_item_at_coords (GtkIconView *icon_view,
3218 gboolean only_in_cell,
3219 GtkCellRenderer **cell_at_pos)
3224 *cell_at_pos = NULL;
3226 for (items = icon_view->priv->items; items; items = items->next)
3228 GtkIconViewItem *item = items->data;
3229 GdkRectangle *item_area = &item->cell_area;
3231 if (x >= item_area->x - icon_view->priv->column_spacing/2 &&
3232 x <= item_area->x + item_area->width + icon_view->priv->column_spacing/2 &&
3233 y >= item_area->y - icon_view->priv->row_spacing/2 &&
3234 y <= item_area->y + item_area->height + icon_view->priv->row_spacing/2)
3236 if (only_in_cell || cell_at_pos)
3238 GtkCellRenderer *cell = NULL;
3239 GtkCellAreaContext *context;
3241 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
3242 _gtk_icon_view_set_cell_data (icon_view, item);
3244 if (x >= item_area->x && x <= item_area->x + item_area->width &&
3245 y >= item_area->y && y <= item_area->y + item_area->height)
3246 cell = gtk_cell_area_get_cell_at_position (icon_view->priv->cell_area, context,
3247 GTK_WIDGET (icon_view),
3252 *cell_at_pos = cell;
3255 return cell != NULL ? item : NULL;
3266 _gtk_icon_view_select_item (GtkIconView *icon_view,
3267 GtkIconViewItem *item)
3269 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3270 g_return_if_fail (item != NULL);
3275 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3277 else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3278 gtk_icon_view_unselect_all_internal (icon_view);
3280 item->selected = TRUE;
3282 gtk_icon_view_item_selected_changed (icon_view, item);
3283 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3285 gtk_icon_view_queue_draw_item (icon_view, item);
3290 _gtk_icon_view_unselect_item (GtkIconView *icon_view,
3291 GtkIconViewItem *item)
3293 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3294 g_return_if_fail (item != NULL);
3296 if (!item->selected)
3299 if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3300 icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3303 item->selected = FALSE;
3305 gtk_icon_view_item_selected_changed (icon_view, item);
3306 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3308 gtk_icon_view_queue_draw_item (icon_view, item);
3312 verify_items (GtkIconView *icon_view)
3317 for (items = icon_view->priv->items; items; items = items->next)
3319 GtkIconViewItem *item = items->data;
3321 if (item->index != i)
3322 g_error ("List item does not match its index: "
3323 "item index %d and list index %d\n", item->index, i);
3330 gtk_icon_view_row_changed (GtkTreeModel *model,
3335 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3337 /* ignore changes in branches */
3338 if (gtk_tree_path_get_depth (path) > 1)
3341 /* An icon view subclass might add it's own model and populate
3342 * things at init() time instead of waiting for the constructor()
3345 if (icon_view->priv->cell_area)
3346 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3348 /* Here we can use a "grow-only" strategy for optimization
3349 * and only invalidate a single item and queue a relayout
3350 * instead of invalidating the whole thing.
3352 * For now GtkIconView still cant deal with huge models
3353 * so just invalidate the whole thing when the model
3356 gtk_icon_view_invalidate_sizes (icon_view);
3358 verify_items (icon_view);
3362 gtk_icon_view_row_inserted (GtkTreeModel *model,
3367 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3369 GtkIconViewItem *item;
3372 /* ignore changes in branches */
3373 if (gtk_tree_path_get_depth (path) > 1)
3376 index = gtk_tree_path_get_indices(path)[0];
3378 item = gtk_icon_view_item_new ();
3380 item->index = index;
3382 /* FIXME: We can be more efficient here,
3383 we can store a tail pointer and use that when
3384 appending (which is a rather common operation)
3386 icon_view->priv->items = g_list_insert (icon_view->priv->items,
3389 list = g_list_nth (icon_view->priv->items, index + 1);
3390 for (; list; list = list->next)
3397 verify_items (icon_view);
3399 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3403 gtk_icon_view_row_deleted (GtkTreeModel *model,
3407 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3409 GtkIconViewItem *item;
3411 gboolean emit = FALSE;
3413 /* ignore changes in branches */
3414 if (gtk_tree_path_get_depth (path) > 1)
3417 index = gtk_tree_path_get_indices(path)[0];
3419 list = g_list_nth (icon_view->priv->items, index);
3422 if (icon_view->priv->cell_area)
3423 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3425 if (item == icon_view->priv->anchor_item)
3426 icon_view->priv->anchor_item = NULL;
3428 if (item == icon_view->priv->cursor_item)
3429 icon_view->priv->cursor_item = NULL;
3431 if (item == icon_view->priv->last_prelight)
3432 icon_view->priv->last_prelight = NULL;
3437 gtk_icon_view_item_free (item);
3439 for (next = list->next; next; next = next->next)
3446 icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3448 verify_items (icon_view);
3450 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3453 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3457 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3458 GtkTreePath *parent,
3463 GtkIconView *icon_view = GTK_ICON_VIEW (data);
3466 GList *items = NULL, *list;
3467 GtkIconViewItem **item_array;
3470 /* ignore changes in branches */
3474 if (icon_view->priv->cell_area)
3475 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3477 length = gtk_tree_model_iter_n_children (model, NULL);
3479 order = g_new (gint, length);
3480 for (i = 0; i < length; i++)
3481 order [new_order[i]] = i;
3483 item_array = g_new (GtkIconViewItem *, length);
3484 for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3485 item_array[order[i]] = list->data;
3488 for (i = length - 1; i >= 0; i--)
3490 item_array[i]->index = i;
3491 items = g_list_prepend (items, item_array[i]);
3494 g_free (item_array);
3495 g_list_free (icon_view->priv->items);
3496 icon_view->priv->items = items;
3498 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3500 verify_items (icon_view);
3504 gtk_icon_view_build_items (GtkIconView *icon_view)
3508 GList *items = NULL;
3510 if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3518 GtkIconViewItem *item = gtk_icon_view_item_new ();
3524 items = g_list_prepend (items, item);
3526 } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3528 icon_view->priv->items = g_list_reverse (items);
3532 gtk_icon_view_add_move_binding (GtkBindingSet *binding_set,
3535 GtkMovementStep step,
3539 gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3540 I_("move-cursor"), 2,
3544 gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3549 if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3552 gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3557 gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3564 gtk_icon_view_real_move_cursor (GtkIconView *icon_view,
3565 GtkMovementStep step,
3568 GdkModifierType state;
3570 g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3571 g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3572 step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3573 step == GTK_MOVEMENT_DISPLAY_LINES ||
3574 step == GTK_MOVEMENT_PAGES ||
3575 step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3577 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3580 gtk_cell_area_stop_editing (icon_view->priv->cell_area, FALSE);
3581 gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3583 if (gtk_get_current_event_state (&state))
3585 GdkModifierType extend_mod_mask;
3586 GdkModifierType modify_mod_mask;
3589 gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3590 GDK_MODIFIER_INTENT_EXTEND_SELECTION);
3592 gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3593 GDK_MODIFIER_INTENT_MODIFY_SELECTION);
3595 if ((state & modify_mod_mask) == modify_mod_mask)
3596 icon_view->priv->modify_selection_pressed = TRUE;
3597 if ((state & extend_mod_mask) == extend_mod_mask)
3598 icon_view->priv->extend_selection_pressed = TRUE;
3600 /* else we assume not pressed */
3604 case GTK_MOVEMENT_LOGICAL_POSITIONS:
3605 case GTK_MOVEMENT_VISUAL_POSITIONS:
3606 gtk_icon_view_move_cursor_left_right (icon_view, count);
3608 case GTK_MOVEMENT_DISPLAY_LINES:
3609 gtk_icon_view_move_cursor_up_down (icon_view, count);
3611 case GTK_MOVEMENT_PAGES:
3612 gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3614 case GTK_MOVEMENT_BUFFER_ENDS:
3615 gtk_icon_view_move_cursor_start_end (icon_view, count);
3618 g_assert_not_reached ();
3621 icon_view->priv->modify_selection_pressed = FALSE;
3622 icon_view->priv->extend_selection_pressed = FALSE;
3624 icon_view->priv->draw_focus = TRUE;
3629 static GtkIconViewItem *
3630 find_item (GtkIconView *icon_view,
3631 GtkIconViewItem *current,
3637 GtkIconViewItem *item;
3639 /* FIXME: this could be more efficient
3641 row = current->row + row_ofs;
3642 col = current->col + col_ofs;
3644 for (items = icon_view->priv->items; items; items = items->next)
3647 if (item->row == row && item->col == col)
3654 static GtkIconViewItem *
3655 find_item_page_up_down (GtkIconView *icon_view,
3656 GtkIconViewItem *current,
3663 y = current->cell_area.y + count * gtk_adjustment_get_page_size (icon_view->priv->vadjustment);
3665 item = g_list_find (icon_view->priv->items, current);
3670 for (next = item->next; next; next = next->next)
3672 if (((GtkIconViewItem *)next->data)->col == col)
3675 if (!next || ((GtkIconViewItem *)next->data)->cell_area.y > y)
3685 for (next = item->prev; next; next = next->prev)
3687 if (((GtkIconViewItem *)next->data)->col == col)
3690 if (!next || ((GtkIconViewItem *)next->data)->cell_area.y < y)
3704 gtk_icon_view_select_all_between (GtkIconView *icon_view,
3705 GtkIconViewItem *anchor,
3706 GtkIconViewItem *cursor)
3709 GtkIconViewItem *item;
3710 gint row1, row2, col1, col2;
3711 gboolean dirty = FALSE;
3713 if (anchor->row < cursor->row)
3724 if (anchor->col < cursor->col)
3735 for (items = icon_view->priv->items; items; items = items->next)
3739 if (row1 <= item->row && item->row <= row2 &&
3740 col1 <= item->col && item->col <= col2)
3742 if (!item->selected)
3745 item->selected = TRUE;
3746 gtk_icon_view_item_selected_changed (icon_view, item);
3748 gtk_icon_view_queue_draw_item (icon_view, item);
3756 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3759 GtkIconViewItem *item;
3760 GtkCellRenderer *cell = NULL;
3761 gboolean dirty = FALSE;
3763 GtkDirectionType direction;
3765 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3768 direction = count < 0 ? GTK_DIR_UP : GTK_DIR_DOWN;
3770 if (!icon_view->priv->cursor_item)
3775 list = icon_view->priv->items;
3777 list = g_list_last (icon_view->priv->items);
3783 /* Give focus to the first cell initially */
3784 _gtk_icon_view_set_cell_data (icon_view, item);
3785 gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3794 item = icon_view->priv->cursor_item;
3795 step = count > 0 ? 1 : -1;
3797 /* Save the current focus cell in case we hit the edge */
3798 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3802 _gtk_icon_view_set_cell_data (icon_view, item);
3804 if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3807 item = find_item (icon_view, item, step, 0);
3813 if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3815 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3817 gtk_widget_child_focus (toplevel,
3818 direction == GTK_DIR_UP ?
3819 GTK_DIR_TAB_BACKWARD :
3820 GTK_DIR_TAB_FORWARD);
3824 gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3828 if (icon_view->priv->modify_selection_pressed ||
3829 !icon_view->priv->extend_selection_pressed ||
3830 !icon_view->priv->anchor_item ||
3831 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3832 icon_view->priv->anchor_item = item;
3834 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3835 _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3837 if (!icon_view->priv->modify_selection_pressed &&
3838 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3840 dirty = gtk_icon_view_unselect_all_internal (icon_view);
3841 dirty = gtk_icon_view_select_all_between (icon_view,
3842 icon_view->priv->anchor_item,
3846 gtk_icon_view_scroll_to_item (icon_view, item);
3849 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3853 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3856 GtkIconViewItem *item;
3857 gboolean dirty = FALSE;
3859 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3862 if (!icon_view->priv->cursor_item)
3867 list = icon_view->priv->items;
3869 list = g_list_last (icon_view->priv->items);
3871 item = list ? list->data : NULL;
3874 item = find_item_page_up_down (icon_view,
3875 icon_view->priv->cursor_item,
3878 if (item == icon_view->priv->cursor_item)
3879 gtk_widget_error_bell (GTK_WIDGET (icon_view));
3884 if (icon_view->priv->modify_selection_pressed ||
3885 !icon_view->priv->extend_selection_pressed ||
3886 !icon_view->priv->anchor_item ||
3887 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3888 icon_view->priv->anchor_item = item;
3890 _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
3892 if (!icon_view->priv->modify_selection_pressed &&
3893 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3895 dirty = gtk_icon_view_unselect_all_internal (icon_view);
3896 dirty = gtk_icon_view_select_all_between (icon_view,
3897 icon_view->priv->anchor_item,
3901 gtk_icon_view_scroll_to_item (icon_view, item);
3904 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3908 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
3911 GtkIconViewItem *item;
3912 GtkCellRenderer *cell = NULL;
3913 gboolean dirty = FALSE;
3915 GtkDirectionType direction;
3917 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3920 direction = count < 0 ? GTK_DIR_LEFT : GTK_DIR_RIGHT;
3922 if (!icon_view->priv->cursor_item)
3927 list = icon_view->priv->items;
3929 list = g_list_last (icon_view->priv->items);
3935 /* Give focus to the first cell initially */
3936 _gtk_icon_view_set_cell_data (icon_view, item);
3937 gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3946 item = icon_view->priv->cursor_item;
3947 step = count > 0 ? 1 : -1;
3949 /* Save the current focus cell in case we hit the edge */
3950 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3954 _gtk_icon_view_set_cell_data (icon_view, item);
3956 if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3959 item = find_item (icon_view, item, 0, step);
3965 if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3967 GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3969 gtk_widget_child_focus (toplevel,
3970 direction == GTK_DIR_LEFT ?
3971 GTK_DIR_TAB_BACKWARD :
3972 GTK_DIR_TAB_FORWARD);
3976 gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3980 if (icon_view->priv->modify_selection_pressed ||
3981 !icon_view->priv->extend_selection_pressed ||
3982 !icon_view->priv->anchor_item ||
3983 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3984 icon_view->priv->anchor_item = item;
3986 cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3987 _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3989 if (!icon_view->priv->modify_selection_pressed &&
3990 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3992 dirty = gtk_icon_view_unselect_all_internal (icon_view);
3993 dirty = gtk_icon_view_select_all_between (icon_view,
3994 icon_view->priv->anchor_item,
3998 gtk_icon_view_scroll_to_item (icon_view, item);
4001 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4005 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
4008 GtkIconViewItem *item;
4010 gboolean dirty = FALSE;
4012 if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
4016 list = icon_view->priv->items;
4018 list = g_list_last (icon_view->priv->items);
4020 item = list ? list->data : NULL;
4022 if (item == icon_view->priv->cursor_item)
4023 gtk_widget_error_bell (GTK_WIDGET (icon_view));
4028 if (icon_view->priv->modify_selection_pressed ||
4029 !icon_view->priv->extend_selection_pressed ||
4030 !icon_view->priv->anchor_item ||
4031 icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4032 icon_view->priv->anchor_item = item;
4034 _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
4036 if (!icon_view->priv->modify_selection_pressed &&
4037 icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4039 dirty = gtk_icon_view_unselect_all_internal (icon_view);
4040 dirty = gtk_icon_view_select_all_between (icon_view,
4041 icon_view->priv->anchor_item,
4045 gtk_icon_view_scroll_to_item (icon_view, item);
4048 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4052 * gtk_icon_view_scroll_to_path:
4053 * @icon_view: A #GtkIconView.
4054 * @path: The path of the item to move to.
4055 * @use_align: whether to use alignment arguments, or %FALSE.
4056 * @row_align: The vertical alignment of the item specified by @path.
4057 * @col_align: The horizontal alignment of the item specified by @path.
4059 * Moves the alignments of @icon_view to the position specified by @path.
4060 * @row_align determines where the row is placed, and @col_align determines
4061 * where @column is placed. Both are expected to be between 0.0 and 1.0.
4062 * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means
4065 * If @use_align is %FALSE, then the alignment arguments are ignored, and the
4066 * tree does the minimum amount of work to scroll the item onto the screen.
4067 * This means that the item will be scrolled to the edge closest to its current
4068 * position. If the item is currently visible on the screen, nothing is done.
4070 * This function only works if the model is set, and @path is a valid row on
4071 * the model. If the model changes before the @icon_view is realized, the
4072 * centered path will be modified to reflect this change.
4077 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
4083 GtkIconViewItem *item = NULL;
4086 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4087 g_return_if_fail (path != NULL);
4088 g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
4089 g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
4091 widget = GTK_WIDGET (icon_view);
4093 if (gtk_tree_path_get_depth (path) > 0)
4094 item = g_list_nth_data (icon_view->priv->items,
4095 gtk_tree_path_get_indices(path)[0]);
4097 if (!item || item->cell_area.width < 0 ||
4098 !gtk_widget_get_realized (widget))
4100 if (icon_view->priv->scroll_to_path)
4101 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4103 icon_view->priv->scroll_to_path = NULL;
4106 icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
4108 icon_view->priv->scroll_to_use_align = use_align;
4109 icon_view->priv->scroll_to_row_align = row_align;
4110 icon_view->priv->scroll_to_col_align = col_align;
4117 GtkAllocation allocation;
4120 GdkRectangle item_area =
4122 item->cell_area.x - icon_view->priv->item_padding,
4123 item->cell_area.y - icon_view->priv->item_padding,
4124 item->cell_area.width + icon_view->priv->item_padding * 2,
4125 item->cell_area.height + icon_view->priv->item_padding * 2
4128 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4130 gtk_widget_get_allocation (widget, &allocation);
4132 offset = y + item_area.y - row_align * (allocation.height - item_area.height);
4134 gtk_adjustment_set_value (icon_view->priv->vadjustment,
4135 gtk_adjustment_get_value (icon_view->priv->vadjustment) + offset);
4137 offset = x + item_area.x - col_align * (allocation.width - item_area.width);
4139 gtk_adjustment_set_value (icon_view->priv->hadjustment,
4140 gtk_adjustment_get_value (icon_view->priv->hadjustment) + offset);
4142 gtk_adjustment_changed (icon_view->priv->hadjustment);
4143 gtk_adjustment_changed (icon_view->priv->vadjustment);
4146 gtk_icon_view_scroll_to_item (icon_view, item);
4151 gtk_icon_view_scroll_to_item (GtkIconView *icon_view,
4152 GtkIconViewItem *item)
4154 GtkIconViewPrivate *priv = icon_view->priv;
4155 GtkWidget *widget = GTK_WIDGET (icon_view);
4156 GtkAdjustment *hadj, *vadj;
4157 GtkAllocation allocation;
4159 GdkRectangle item_area;
4161 item_area.x = item->cell_area.x - priv->item_padding;
4162 item_area.y = item->cell_area.y - priv->item_padding;
4163 item_area.width = item->cell_area.width + priv->item_padding * 2;
4164 item_area.height = item->cell_area.height + priv->item_padding * 2;
4166 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4167 gtk_widget_get_allocation (widget, &allocation);
4169 hadj = icon_view->priv->hadjustment;
4170 vadj = icon_view->priv->vadjustment;
4172 if (y + item_area.y < 0)
4173 gtk_adjustment_set_value (vadj,
4174 gtk_adjustment_get_value (vadj)
4176 else if (y + item_area.y + item_area.height > allocation.height)
4177 gtk_adjustment_set_value (vadj,
4178 gtk_adjustment_get_value (vadj)
4179 + y + item_area.y + item_area.height - allocation.height);
4181 if (x + item_area.x < 0)
4182 gtk_adjustment_set_value (hadj,
4183 gtk_adjustment_get_value (hadj)
4185 else if (x + item_area.x + item_area.width > allocation.width)
4186 gtk_adjustment_set_value (hadj,
4187 gtk_adjustment_get_value (hadj)
4188 + x + item_area.x + item_area.width - allocation.width);
4190 gtk_adjustment_changed (hadj);
4191 gtk_adjustment_changed (vadj);
4194 /* GtkCellLayout implementation */
4197 gtk_icon_view_ensure_cell_area (GtkIconView *icon_view,
4198 GtkCellArea *cell_area)
4200 GtkIconViewPrivate *priv = icon_view->priv;
4202 if (priv->cell_area)
4206 priv->cell_area = cell_area;
4208 priv->cell_area = gtk_cell_area_box_new ();
4210 g_object_ref_sink (priv->cell_area);
4212 if (GTK_IS_ORIENTABLE (priv->cell_area))
4213 gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->cell_area), priv->item_orientation);
4215 priv->cell_area_context = gtk_cell_area_create_context (priv->cell_area);
4217 priv->add_editable_id =
4218 g_signal_connect (priv->cell_area, "add-editable",
4219 G_CALLBACK (gtk_icon_view_add_editable), icon_view);
4220 priv->remove_editable_id =
4221 g_signal_connect (priv->cell_area, "remove-editable",
4222 G_CALLBACK (gtk_icon_view_remove_editable), icon_view);
4224 update_text_cell (icon_view);
4225 update_pixbuf_cell (icon_view);
4228 static GtkCellArea *
4229 gtk_icon_view_cell_layout_get_area (GtkCellLayout *cell_layout)
4231 GtkIconView *icon_view = GTK_ICON_VIEW (cell_layout);
4232 GtkIconViewPrivate *priv = icon_view->priv;
4234 if (G_UNLIKELY (!priv->cell_area))
4235 gtk_icon_view_ensure_cell_area (icon_view, NULL);
4237 return icon_view->priv->cell_area;
4241 _gtk_icon_view_set_cell_data (GtkIconView *icon_view,
4242 GtkIconViewItem *item)
4247 path = gtk_tree_path_new_from_indices (item->index, -1);
4248 if (!gtk_tree_model_get_iter (icon_view->priv->model, &iter, path))
4250 gtk_tree_path_free (path);
4252 gtk_cell_area_apply_attributes (icon_view->priv->cell_area,
4253 icon_view->priv->model,
4254 &iter, FALSE, FALSE);
4263 * gtk_icon_view_new:
4265 * Creates a new #GtkIconView widget
4267 * Return value: A newly created #GtkIconView widget
4272 gtk_icon_view_new (void)
4274 return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4278 * gtk_icon_view_new_with_area:
4279 * @area: the #GtkCellArea to use to layout cells
4281 * Creates a new #GtkIconView widget using the
4282 * specified @area to layout cells inside the icons.
4284 * Return value: A newly created #GtkIconView widget
4289 gtk_icon_view_new_with_area (GtkCellArea *area)
4291 return g_object_new (GTK_TYPE_ICON_VIEW, "cell-area", area, NULL);
4295 * gtk_icon_view_new_with_model:
4296 * @model: The model.
4298 * Creates a new #GtkIconView widget with the model @model.
4300 * Return value: A newly created #GtkIconView widget.
4305 gtk_icon_view_new_with_model (GtkTreeModel *model)
4307 return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4311 * gtk_icon_view_convert_widget_to_bin_window_coords:
4312 * @icon_view: a #GtkIconView
4313 * @wx: X coordinate relative to the widget
4314 * @wy: Y coordinate relative to the widget
4315 * @bx: (out): return location for bin_window X coordinate
4316 * @by: (out): return location for bin_window Y coordinate
4318 * Converts widget coordinates to coordinates for the bin_window,
4319 * as expected by e.g. gtk_icon_view_get_path_at_pos().
4324 gtk_icon_view_convert_widget_to_bin_window_coords (GtkIconView *icon_view,
4332 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4334 if (icon_view->priv->bin_window)
4335 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4346 * gtk_icon_view_get_path_at_pos:
4347 * @icon_view: A #GtkIconView.
4348 * @x: The x position to be identified
4349 * @y: The y position to be identified
4351 * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4352 * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4353 * the cell at the specified position.
4354 * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4355 * widget coordinates to bin_window coordinates.
4357 * Return value: The #GtkTreePath corresponding to the icon or %NULL
4358 * if no icon exists at that position.
4363 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4367 GtkIconViewItem *item;
4370 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4372 item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4377 path = gtk_tree_path_new_from_indices (item->index, -1);
4383 * gtk_icon_view_get_item_at_pos:
4384 * @icon_view: A #GtkIconView.
4385 * @x: The x position to be identified
4386 * @y: The y position to be identified
4387 * @path: (out) (allow-none): Return location for the path, or %NULL
4388 * @cell: (out) (allow-none): Return location for the renderer
4389 * responsible for the cell at (@x, @y), or %NULL
4391 * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4392 * In contrast to gtk_icon_view_get_path_at_pos(), this function also
4393 * obtains the cell at the specified position. The returned path should
4394 * be freed with gtk_tree_path_free().
4395 * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4396 * widget coordinates to bin_window coordinates.
4398 * Return value: %TRUE if an item exists at the specified position
4403 gtk_icon_view_get_item_at_pos (GtkIconView *icon_view,
4407 GtkCellRenderer **cell)
4409 GtkIconViewItem *item;
4410 GtkCellRenderer *renderer = NULL;
4412 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4414 item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &renderer);
4419 *path = gtk_tree_path_new_from_indices (item->index, -1);
4427 return (item != NULL);
4431 * gtk_icon_view_set_tooltip_item:
4432 * @icon_view: a #GtkIconView
4433 * @tooltip: a #GtkTooltip
4434 * @path: a #GtkTreePath
4436 * Sets the tip area of @tooltip to be the area covered by the item at @path.
4437 * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4438 * See also gtk_tooltip_set_tip_area().
4443 gtk_icon_view_set_tooltip_item (GtkIconView *icon_view,
4444 GtkTooltip *tooltip,
4447 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4448 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4450 gtk_icon_view_set_tooltip_cell (icon_view, tooltip, path, NULL);
4454 * gtk_icon_view_set_tooltip_cell:
4455 * @icon_view: a #GtkIconView
4456 * @tooltip: a #GtkTooltip
4457 * @path: a #GtkTreePath
4458 * @cell: (allow-none): a #GtkCellRenderer or %NULL
4460 * Sets the tip area of @tooltip to the area which @cell occupies in
4461 * the item pointed to by @path. See also gtk_tooltip_set_tip_area().
4463 * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4468 gtk_icon_view_set_tooltip_cell (GtkIconView *icon_view,
4469 GtkTooltip *tooltip,
4471 GtkCellRenderer *cell)
4474 GtkIconViewItem *item = NULL;
4477 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4478 g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4479 g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
4481 if (gtk_tree_path_get_depth (path) > 0)
4482 item = g_list_nth_data (icon_view->priv->items,
4483 gtk_tree_path_get_indices(path)[0]);
4490 GtkCellAreaContext *context;
4492 context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
4493 _gtk_icon_view_set_cell_data (icon_view, item);
4494 gtk_cell_area_get_cell_allocation (icon_view->priv->cell_area, context,
4495 GTK_WIDGET (icon_view),
4496 cell, &item->cell_area, &rect);
4500 rect.x = item->cell_area.x - icon_view->priv->item_padding;
4501 rect.y = item->cell_area.y - icon_view->priv->item_padding;
4502 rect.width = item->cell_area.width + icon_view->priv->item_padding * 2;
4503 rect.height = item->cell_area.height + icon_view->priv->item_padding * 2;
4506 if (icon_view->priv->bin_window)
4508 gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4513 gtk_tooltip_set_tip_area (tooltip, &rect);
4518 * gtk_icon_view_get_tooltip_context:
4519 * @icon_view: an #GtkIconView
4520 * @x: (inout): the x coordinate (relative to widget coordinates)
4521 * @y: (inout): the y coordinate (relative to widget coordinates)
4522 * @keyboard_tip: whether this is a keyboard tooltip or not
4523 * @model: (out) (allow-none): a pointer to receive a #GtkTreeModel or %NULL
4524 * @path: (out) (allow-none): a pointer to receive a #GtkTreePath or %NULL
4525 * @iter: (out) (allow-none): a pointer to receive a #GtkTreeIter or %NULL
4527 * This function is supposed to be used in a #GtkWidget::query-tooltip
4528 * signal handler for #GtkIconView. The @x, @y and @keyboard_tip values
4529 * which are received in the signal handler, should be passed to this
4530 * function without modification.
4532 * The return value indicates whether there is an icon view item at the given
4533 * coordinates (%TRUE) or not (%FALSE) for mouse tooltips. For keyboard
4534 * tooltips the item returned will be the cursor item. When %TRUE, then any of
4535 * @model, @path and @iter which have been provided will be set to point to
4536 * that row and the corresponding model. @x and @y will always be converted
4537 * to be relative to @icon_view's bin_window if @keyboard_tooltip is %FALSE.
4539 * Return value: whether or not the given tooltip context points to a item
4544 gtk_icon_view_get_tooltip_context (GtkIconView *icon_view,
4547 gboolean keyboard_tip,
4548 GtkTreeModel **model,
4552 GtkTreePath *tmppath = NULL;
4554 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4555 g_return_val_if_fail (x != NULL, FALSE);
4556 g_return_val_if_fail (y != NULL, FALSE);
4560 gtk_icon_view_get_cursor (icon_view, &tmppath, NULL);
4567 gtk_icon_view_convert_widget_to_bin_window_coords (icon_view, *x, *y,
4570 if (!gtk_icon_view_get_item_at_pos (icon_view, *x, *y, &tmppath, NULL))
4575 *model = gtk_icon_view_get_model (icon_view);
4578 gtk_tree_model_get_iter (gtk_icon_view_get_model (icon_view),
4584 gtk_tree_path_free (tmppath);
4590 gtk_icon_view_set_tooltip_query_cb (GtkWidget *widget,
4593 gboolean keyboard_tip,
4594 GtkTooltip *tooltip,
4600 GtkTreeModel *model;
4601 GtkIconView *icon_view = GTK_ICON_VIEW (widget);
4603 if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget),
4606 &model, &path, &iter))
4609 gtk_tree_model_get (model, &iter, icon_view->priv->tooltip_column, &str, -1);
4613 gtk_tree_path_free (path);
4617 gtk_tooltip_set_markup (tooltip, str);
4618 gtk_icon_view_set_tooltip_item (icon_view, tooltip, path);
4620 gtk_tree_path_free (path);
4628 * gtk_icon_view_set_tooltip_column:
4629 * @icon_view: a #GtkIconView
4630 * @column: an integer, which is a valid column number for @icon_view's model
4632 * If you only plan to have simple (text-only) tooltips on full items, you
4633 * can use this function to have #GtkIconView handle these automatically
4634 * for you. @column should be set to the column in @icon_view's model
4635 * containing the tooltip texts, or -1 to disable this feature.
4637 * When enabled, #GtkWidget:has-tooltip will be set to %TRUE and
4638 * @icon_view will connect a #GtkWidget::query-tooltip signal handler.
4640 * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
4641 * so &, <, etc have to be escaped in the text.
4646 gtk_icon_view_set_tooltip_column (GtkIconView *icon_view,
4649 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4651 if (column == icon_view->priv->tooltip_column)
4656 g_signal_handlers_disconnect_by_func (icon_view,
4657 gtk_icon_view_set_tooltip_query_cb,
4659 gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), FALSE);
4663 if (icon_view->priv->tooltip_column == -1)
4665 g_signal_connect (icon_view, "query-tooltip",
4666 G_CALLBACK (gtk_icon_view_set_tooltip_query_cb), NULL);
4667 gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), TRUE);
4671 icon_view->priv->tooltip_column = column;
4672 g_object_notify (G_OBJECT (icon_view), "tooltip-column");
4676 * gtk_icon_view_get_tooltip_column:
4677 * @icon_view: a #GtkIconView
4679 * Returns the column of @icon_view's model which is being used for
4680 * displaying tooltips on @icon_view's rows.
4682 * Return value: the index of the tooltip column that is currently being
4683 * used, or -1 if this is disabled.
4688 gtk_icon_view_get_tooltip_column (GtkIconView *icon_view)
4690 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 0);
4692 return icon_view->priv->tooltip_column;
4696 * gtk_icon_view_get_visible_range:
4697 * @icon_view: A #GtkIconView
4698 * @start_path: (out) (allow-none): Return location for start of region,
4700 * @end_path: (out) (allow-none): Return location for end of region, or %NULL
4702 * Sets @start_path and @end_path to be the first and last visible path.
4703 * Note that there may be invisible paths in between.
4705 * Both paths should be freed with gtk_tree_path_free() after use.
4707 * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4712 gtk_icon_view_get_visible_range (GtkIconView *icon_view,
4713 GtkTreePath **start_path,
4714 GtkTreePath **end_path)
4716 gint start_index = -1;
4717 gint end_index = -1;
4720 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4722 if (icon_view->priv->hadjustment == NULL ||
4723 icon_view->priv->vadjustment == NULL)
4726 if (start_path == NULL && end_path == NULL)
4729 for (icons = icon_view->priv->items; icons; icons = icons->next)
4731 GtkIconViewItem *item = icons->data;
4732 GdkRectangle *item_area = &item->cell_area;
4734 if ((item_area->x + item_area->width >= (int)gtk_adjustment_get_value (icon_view->priv->hadjustment)) &&
4735 (item_area->y + item_area->height >= (int)gtk_adjustment_get_value (icon_view->priv->vadjustment)) &&
4737 (int) (gtk_adjustment_get_value (icon_view->priv->hadjustment) +
4738 gtk_adjustment_get_page_size (icon_view->priv->hadjustment))) &&
4740 (int) (gtk_adjustment_get_value (icon_view->priv->vadjustment) +
4741 gtk_adjustment_get_page_size (icon_view->priv->vadjustment))))
4743 if (start_index == -1)
4744 start_index = item->index;
4745 end_index = item->index;
4749 if (start_path && start_index != -1)
4750 *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4751 if (end_path && end_index != -1)
4752 *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4754 return start_index != -1;
4758 * gtk_icon_view_selected_foreach:
4759 * @icon_view: A #GtkIconView.
4760 * @func: (scope call): The function to call for each selected icon.
4761 * @data: User data to pass to the function.
4763 * Calls a function for each selected icon. Note that the model or
4764 * selection cannot be modified from within this function.
4769 gtk_icon_view_selected_foreach (GtkIconView *icon_view,
4770 GtkIconViewForeachFunc func,
4775 for (list = icon_view->priv->items; list; list = list->next)
4777 GtkIconViewItem *item = list->data;
4778 GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4781 (* func) (icon_view, path, data);
4783 gtk_tree_path_free (path);
4788 * gtk_icon_view_set_selection_mode:
4789 * @icon_view: A #GtkIconView.
4790 * @mode: The selection mode
4792 * Sets the selection mode of the @icon_view.
4797 gtk_icon_view_set_selection_mode (GtkIconView *icon_view,
4798 GtkSelectionMode mode)
4800 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4802 if (mode == icon_view->priv->selection_mode)
4805 if (mode == GTK_SELECTION_NONE ||
4806 icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4807 gtk_icon_view_unselect_all (icon_view);
4809 icon_view->priv->selection_mode = mode;
4811 g_object_notify (G_OBJECT (icon_view), "selection-mode");
4815 * gtk_icon_view_get_selection_mode:
4816 * @icon_view: A #GtkIconView.
4818 * Gets the selection mode of the @icon_view.
4820 * Return value: the current selection mode
4825 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4827 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4829 return icon_view->priv->selection_mode;
4833 * gtk_icon_view_set_model:
4834 * @icon_view: A #GtkIconView.
4835 * @model: (allow-none): The model.
4837 * Sets the model for a #GtkIconView.
4838 * If the @icon_view already has a model set, it will remove
4839 * it before setting the new model. If @model is %NULL, then
4840 * it will unset the old model.
4845 gtk_icon_view_set_model (GtkIconView *icon_view,
4846 GtkTreeModel *model)
4848 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4849 g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4851 if (icon_view->priv->model == model)
4854 if (icon_view->priv->scroll_to_path)
4856 gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4857 icon_view->priv->scroll_to_path = NULL;
4860 /* The area can be NULL while disposing */
4861 if (icon_view->priv->cell_area)
4862 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4868 if (icon_view->priv->pixbuf_column != -1)
4870 column_type = gtk_tree_model_get_column_type (model,
4871 icon_view->priv->pixbuf_column);
4873 g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4876 if (icon_view->priv->text_column != -1)
4878 column_type = gtk_tree_model_get_column_type (model,
4879 icon_view->priv->text_column);
4881 g_return_if_fail (column_type == G_TYPE_STRING);
4884 if (icon_view->priv->markup_column != -1)
4886 column_type = gtk_tree_model_get_column_type (model,
4887 icon_view->priv->markup_column);
4889 g_return_if_fail (column_type == G_TYPE_STRING);
4894 if (icon_view->priv->model)
4896 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4897 gtk_icon_view_row_changed,
4899 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4900 gtk_icon_view_row_inserted,
4902 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4903 gtk_icon_view_row_deleted,
4905 g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4906 gtk_icon_view_rows_reordered,
4909 g_object_unref (icon_view->priv->model);
4911 g_list_free_full (icon_view->priv->items, (GDestroyNotify) gtk_icon_view_item_free);
4912 icon_view->priv->items = NULL;
4913 icon_view->priv->anchor_item = NULL;
4914 icon_view->priv->cursor_item = NULL;
4915 icon_view->priv->last_single_clicked = NULL;
4916 icon_view->priv->last_prelight = NULL;
4917 icon_view->priv->width = 0;
4918 icon_view->priv->height = 0;
4921 icon_view->priv->model = model;
4923 if (icon_view->priv->model)
4925 g_object_ref (icon_view->priv->model);
4926 g_signal_connect (icon_view->priv->model,
4928 G_CALLBACK (gtk_icon_view_row_changed),
4930 g_signal_connect (icon_view->priv->model,
4932 G_CALLBACK (gtk_icon_view_row_inserted),
4934 g_signal_connect (icon_view->priv->model,
4936 G_CALLBACK (gtk_icon_view_row_deleted),
4938 g_signal_connect (icon_view->priv->model,
4940 G_CALLBACK (gtk_icon_view_rows_reordered),
4943 gtk_icon_view_build_items (icon_view);
4946 g_object_notify (G_OBJECT (icon_view), "model");
4948 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4952 * gtk_icon_view_get_model:
4953 * @icon_view: a #GtkIconView
4955 * Returns the model the #GtkIconView is based on. Returns %NULL if the
4958 * Return value: (transfer none): A #GtkTreeModel, or %NULL if none is
4959 * currently being used.
4964 gtk_icon_view_get_model (GtkIconView *icon_view)
4966 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4968 return icon_view->priv->model;
4972 update_text_cell (GtkIconView *icon_view)
4974 if (!icon_view->priv->cell_area)
4977 if (icon_view->priv->text_column == -1 &&
4978 icon_view->priv->markup_column == -1)
4980 if (icon_view->priv->text_cell != NULL)
4982 gtk_cell_area_remove (icon_view->priv->cell_area,
4983 icon_view->priv->text_cell);
4984 icon_view->priv->text_cell = NULL;
4989 if (icon_view->priv->text_cell == NULL)
4991 icon_view->priv->text_cell = gtk_cell_renderer_text_new ();
4993 gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), icon_view->priv->text_cell, FALSE);
4996 if (icon_view->priv->markup_column != -1)
4997 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4998 icon_view->priv->text_cell,
4999 "markup", icon_view->priv->markup_column,
5002 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5003 icon_view->priv->text_cell,
5004 "text", icon_view->priv->text_column,
5007 if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5008 g_object_set (icon_view->priv->text_cell,
5009 "alignment", PANGO_ALIGN_CENTER,
5010 "wrap-mode", PANGO_WRAP_WORD_CHAR,
5015 g_object_set (icon_view->priv->text_cell,
5016 "alignment", PANGO_ALIGN_LEFT,
5017 "wrap-mode", PANGO_WRAP_WORD_CHAR,
5025 update_pixbuf_cell (GtkIconView *icon_view)
5027 if (!icon_view->priv->cell_area)
5030 if (icon_view->priv->pixbuf_column == -1)
5032 if (icon_view->priv->pixbuf_cell != NULL)
5034 gtk_cell_area_remove (icon_view->priv->cell_area,
5035 icon_view->priv->pixbuf_cell);
5037 icon_view->priv->pixbuf_cell = NULL;
5042 if (icon_view->priv->pixbuf_cell == NULL)
5044 icon_view->priv->pixbuf_cell = gtk_cell_renderer_pixbuf_new ();
5046 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), icon_view->priv->pixbuf_cell, FALSE);
5049 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5050 icon_view->priv->pixbuf_cell,
5051 "pixbuf", icon_view->priv->pixbuf_column,
5054 if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5055 g_object_set (icon_view->priv->pixbuf_cell,
5060 g_object_set (icon_view->priv->pixbuf_cell,
5068 * gtk_icon_view_set_text_column:
5069 * @icon_view: A #GtkIconView.
5070 * @column: A column in the currently used model, or -1 to display no text
5072 * Sets the column with text for @icon_view to be @column. The text
5073 * column must be of type #G_TYPE_STRING.
5078 gtk_icon_view_set_text_column (GtkIconView *icon_view,
5081 if (column == icon_view->priv->text_column)
5085 icon_view->priv->text_column = -1;
5088 if (icon_view->priv->model != NULL)
5092 column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5094 g_return_if_fail (column_type == G_TYPE_STRING);
5097 icon_view->priv->text_column = column;
5100 if (icon_view->priv->cell_area)
5101 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5103 update_text_cell (icon_view);
5105 gtk_icon_view_invalidate_sizes (icon_view);
5107 g_object_notify (G_OBJECT (icon_view), "text-column");
5111 * gtk_icon_view_get_text_column:
5112 * @icon_view: A #GtkIconView.
5114 * Returns the column with text for @icon_view.
5116 * Returns: the text column, or -1 if it's unset.
5121 gtk_icon_view_get_text_column (GtkIconView *icon_view)
5123 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5125 return icon_view->priv->text_column;
5129 * gtk_icon_view_set_markup_column:
5130 * @icon_view: A #GtkIconView.
5131 * @column: A column in the currently used model, or -1 to display no text
5133 * Sets the column with markup information for @icon_view to be
5134 * @column. The markup column must be of type #G_TYPE_STRING.
5135 * If the markup column is set to something, it overrides
5136 * the text column set by gtk_icon_view_set_text_column().
5141 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5144 if (column == icon_view->priv->markup_column)
5148 icon_view->priv->markup_column = -1;
5151 if (icon_view->priv->model != NULL)
5155 column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5157 g_return_if_fail (column_type == G_TYPE_STRING);
5160 icon_view->priv->markup_column = column;
5163 if (icon_view->priv->cell_area)
5164 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5166 update_text_cell (icon_view);
5168 gtk_icon_view_invalidate_sizes (icon_view);
5170 g_object_notify (G_OBJECT (icon_view), "markup-column");
5174 * gtk_icon_view_get_markup_column:
5175 * @icon_view: A #GtkIconView.
5177 * Returns the column with markup text for @icon_view.
5179 * Returns: the markup column, or -1 if it's unset.
5184 gtk_icon_view_get_markup_column (GtkIconView *icon_view)
5186 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5188 return icon_view->priv->markup_column;
5192 * gtk_icon_view_set_pixbuf_column:
5193 * @icon_view: A #GtkIconView.
5194 * @column: A column in the currently used model, or -1 to disable
5196 * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5197 * column must be of type #GDK_TYPE_PIXBUF
5202 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5205 if (column == icon_view->priv->pixbuf_column)
5209 icon_view->priv->pixbuf_column = -1;
5212 if (icon_view->priv->model != NULL)
5216 column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5218 g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5221 icon_view->priv->pixbuf_column = column;
5224 if (icon_view->priv->cell_area)
5225 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5227 update_pixbuf_cell (icon_view);
5229 gtk_icon_view_invalidate_sizes (icon_view);
5231 g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5236 * gtk_icon_view_get_pixbuf_column:
5237 * @icon_view: A #GtkIconView.
5239 * Returns the column with pixbufs for @icon_view.
5241 * Returns: the pixbuf column, or -1 if it's unset.
5246 gtk_icon_view_get_pixbuf_column (GtkIconView *icon_view)
5248 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5250 return icon_view->priv->pixbuf_column;
5254 * gtk_icon_view_select_path:
5255 * @icon_view: A #GtkIconView.
5256 * @path: The #GtkTreePath to be selected.
5258 * Selects the row at @path.
5263 gtk_icon_view_select_path (GtkIconView *icon_view,
5266 GtkIconViewItem *item = NULL;
5268 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5269 g_return_if_fail (icon_view->priv->model != NULL);
5270 g_return_if_fail (path != NULL);
5272 if (gtk_tree_path_get_depth (path) > 0)
5273 item = g_list_nth_data (icon_view->priv->items,
5274 gtk_tree_path_get_indices(path)[0]);
5277 _gtk_icon_view_select_item (icon_view, item);
5281 * gtk_icon_view_unselect_path:
5282 * @icon_view: A #GtkIconView.
5283 * @path: The #GtkTreePath to be unselected.
5285 * Unselects the row at @path.
5290 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5293 GtkIconViewItem *item;
5295 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5296 g_return_if_fail (icon_view->priv->model != NULL);
5297 g_return_if_fail (path != NULL);
5299 item = g_list_nth_data (icon_view->priv->items,
5300 gtk_tree_path_get_indices(path)[0]);
5305 _gtk_icon_view_unselect_item (icon_view, item);
5309 * gtk_icon_view_get_selected_items:
5310 * @icon_view: A #GtkIconView.
5312 * Creates a list of paths of all selected items. Additionally, if you are
5313 * planning on modifying the model after calling this function, you may
5314 * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5315 * To do this, you can use gtk_tree_row_reference_new().
5317 * To free the return value, use:
5319 * g_list_free_full (list, (GDestroyNotify) gtk_tree_patch_free);
5322 * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
5327 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5330 GList *selected = NULL;
5332 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5334 for (list = icon_view->priv->items; list != NULL; list = list->next)
5336 GtkIconViewItem *item = list->data;
5340 GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5342 selected = g_list_prepend (selected, path);
5350 * gtk_icon_view_select_all:
5351 * @icon_view: A #GtkIconView.
5353 * Selects all the icons. @icon_view must has its selection mode set
5354 * to #GTK_SELECTION_MULTIPLE.
5359 gtk_icon_view_select_all (GtkIconView *icon_view)
5362 gboolean dirty = FALSE;
5364 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5366 if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5369 for (items = icon_view->priv->items; items; items = items->next)
5371 GtkIconViewItem *item = items->data;
5373 if (!item->selected)
5376 item->selected = TRUE;
5377 gtk_icon_view_queue_draw_item (icon_view, item);
5382 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5386 * gtk_icon_view_unselect_all:
5387 * @icon_view: A #GtkIconView.
5389 * Unselects all the icons.
5394 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5396 gboolean dirty = FALSE;
5398 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5400 if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5403 dirty = gtk_icon_view_unselect_all_internal (icon_view);
5406 g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5410 * gtk_icon_view_path_is_selected:
5411 * @icon_view: A #GtkIconView.
5412 * @path: A #GtkTreePath to check selection on.
5414 * Returns %TRUE if the icon pointed to by @path is currently
5415 * selected. If @path does not point to a valid location, %FALSE is returned.
5417 * Return value: %TRUE if @path is selected.
5422 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5425 GtkIconViewItem *item;
5427 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5428 g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5429 g_return_val_if_fail (path != NULL, FALSE);
5431 item = g_list_nth_data (icon_view->priv->items,
5432 gtk_tree_path_get_indices(path)[0]);
5437 return item->selected;
5441 * gtk_icon_view_get_item_row:
5442 * @icon_view: a #GtkIconView
5443 * @path: the #GtkTreePath of the item
5445 * Gets the row in which the item @path is currently
5446 * displayed. Row numbers start at 0.
5448 * Returns: The row in which the item is displayed
5453 gtk_icon_view_get_item_row (GtkIconView *icon_view,
5456 GtkIconViewItem *item;
5458 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5459 g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5460 g_return_val_if_fail (path != NULL, -1);
5462 item = g_list_nth_data (icon_view->priv->items,
5463 gtk_tree_path_get_indices(path)[0]);
5472 * gtk_icon_view_get_item_column:
5473 * @icon_view: a #GtkIconView
5474 * @path: the #GtkTreePath of the item
5476 * Gets the column in which the item @path is currently
5477 * displayed. Column numbers start at 0.
5479 * Returns: The column in which the item is displayed
5484 gtk_icon_view_get_item_column (GtkIconView *icon_view,
5487 GtkIconViewItem *item;
5489 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5490 g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5491 g_return_val_if_fail (path != NULL, -1);
5493 item = g_list_nth_data (icon_view->priv->items,
5494 gtk_tree_path_get_indices(path)[0]);
5503 * gtk_icon_view_item_activated:
5504 * @icon_view: A #GtkIconView
5505 * @path: The #GtkTreePath to be activated
5507 * Activates the item determined by @path.
5512 gtk_icon_view_item_activated (GtkIconView *icon_view,
5515 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5516 g_return_if_fail (path != NULL);
5518 g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5522 * gtk_icon_view_set_item_orientation:
5523 * @icon_view: a #GtkIconView
5524 * @orientation: the relative position of texts and icons
5526 * Sets the ::item-orientation property which determines whether the labels
5527 * are drawn beside the icons instead of below.
5532 gtk_icon_view_set_item_orientation (GtkIconView *icon_view,
5533 GtkOrientation orientation)
5535 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5537 if (icon_view->priv->item_orientation != orientation)
5539 icon_view->priv->item_orientation = orientation;
5541 if (icon_view->priv->cell_area)
5543 if (GTK_IS_ORIENTABLE (icon_view->priv->cell_area))
5544 gtk_orientable_set_orientation (GTK_ORIENTABLE (icon_view->priv->cell_area),
5545 icon_view->priv->item_orientation);
5547 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5550 gtk_icon_view_invalidate_sizes (icon_view);
5552 update_text_cell (icon_view);
5553 update_pixbuf_cell (icon_view);
5555 g_object_notify (G_OBJECT (icon_view), "item-orientation");
5560 * gtk_icon_view_get_item_orientation:
5561 * @icon_view: a #GtkIconView
5563 * Returns the value of the ::item-orientation property which determines
5564 * whether the labels are drawn beside the icons instead of below.
5566 * Return value: the relative position of texts and icons
5571 gtk_icon_view_get_item_orientation (GtkIconView *icon_view)
5573 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view),
5574 GTK_ORIENTATION_VERTICAL);
5576 return icon_view->priv->item_orientation;
5580 * gtk_icon_view_set_columns:
5581 * @icon_view: a #GtkIconView
5582 * @columns: the number of columns
5584 * Sets the ::columns property which determines in how
5585 * many columns the icons are arranged. If @columns is
5586 * -1, the number of columns will be chosen automatically
5587 * to fill the available area.
5592 gtk_icon_view_set_columns (GtkIconView *icon_view,
5595 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5597 if (icon_view->priv->columns != columns)
5599 icon_view->priv->columns = columns;
5601 if (icon_view->priv->cell_area)
5602 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5604 gtk_widget_queue_resize (GTK_WIDGET (icon_view));
5606 g_object_notify (G_OBJECT (icon_view), "columns");
5611 * gtk_icon_view_get_columns:
5612 * @icon_view: a #GtkIconView
5614 * Returns the value of the ::columns property.
5616 * Return value: the number of columns, or -1
5621 gtk_icon_view_get_columns (GtkIconView *icon_view)
5623 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5625 return icon_view->priv->columns;
5629 * gtk_icon_view_set_item_width:
5630 * @icon_view: a #GtkIconView
5631 * @item_width: the width for each item
5633 * Sets the ::item-width property which specifies the width
5634 * to use for each item. If it is set to -1, the icon view will
5635 * automatically determine a suitable item size.
5640 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5643 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5645 if (icon_view->priv->item_width != item_width)
5647 icon_view->priv->item_width = item_width;
5649 if (icon_view->priv->cell_area)
5650 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5652 gtk_icon_view_invalidate_sizes (icon_view);
5654 update_text_cell (icon_view);
5656 g_object_notify (G_OBJECT (icon_view), "item-width");
5661 * gtk_icon_view_get_item_width:
5662 * @icon_view: a #GtkIconView
5664 * Returns the value of the ::item-width property.
5666 * Return value: the width of a single item, or -1
5671 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5673 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5675 return icon_view->priv->item_width;
5680 * gtk_icon_view_set_spacing:
5681 * @icon_view: a #GtkIconView
5682 * @spacing: the spacing
5684 * Sets the ::spacing property which specifies the space
5685 * which is inserted between the cells (i.e. the icon and
5686 * the text) of an item.
5691 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5694 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5696 if (icon_view->priv->spacing != spacing)
5698 icon_view->priv->spacing = spacing;
5700 if (icon_view->priv->cell_area)
5701 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5703 gtk_icon_view_invalidate_sizes (icon_view);
5705 g_object_notify (G_OBJECT (icon_view), "spacing");
5710 * gtk_icon_view_get_spacing:
5711 * @icon_view: a #GtkIconView
5713 * Returns the value of the ::spacing property.
5715 * Return value: the space between cells
5720 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5722 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5724 return icon_view->priv->spacing;
5728 * gtk_icon_view_set_row_spacing:
5729 * @icon_view: a #GtkIconView
5730 * @row_spacing: the row spacing
5732 * Sets the ::row-spacing property which specifies the space
5733 * which is inserted between the rows of the icon view.
5738 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5741 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5743 if (icon_view->priv->row_spacing != row_spacing)
5745 icon_view->priv->row_spacing = row_spacing;
5747 if (icon_view->priv->cell_area)
5748 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5750 gtk_icon_view_invalidate_sizes (icon_view);
5752 g_object_notify (G_OBJECT (icon_view), "row-spacing");
5757 * gtk_icon_view_get_row_spacing:
5758 * @icon_view: a #GtkIconView
5760 * Returns the value of the ::row-spacing property.
5762 * Return value: the space between rows
5767 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5769 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5771 return icon_view->priv->row_spacing;
5775 * gtk_icon_view_set_column_spacing:
5776 * @icon_view: a #GtkIconView
5777 * @column_spacing: the column spacing
5779 * Sets the ::column-spacing property which specifies the space
5780 * which is inserted between the columns of the icon view.
5785 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5786 gint column_spacing)
5788 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5790 if (icon_view->priv->column_spacing != column_spacing)
5792 icon_view->priv->column_spacing = column_spacing;
5794 if (icon_view->priv->cell_area)
5795 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5797 gtk_icon_view_invalidate_sizes (icon_view);
5799 g_object_notify (G_OBJECT (icon_view), "column-spacing");
5804 * gtk_icon_view_get_column_spacing:
5805 * @icon_view: a #GtkIconView
5807 * Returns the value of the ::column-spacing property.
5809 * Return value: the space between columns
5814 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5816 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5818 return icon_view->priv->column_spacing;
5822 * gtk_icon_view_set_margin:
5823 * @icon_view: a #GtkIconView
5824 * @margin: the margin
5826 * Sets the ::margin property which specifies the space
5827 * which is inserted at the top, bottom, left and right
5833 gtk_icon_view_set_margin (GtkIconView *icon_view,
5836 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5838 if (icon_view->priv->margin != margin)
5840 icon_view->priv->margin = margin;
5842 if (icon_view->priv->cell_area)
5843 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5845 gtk_icon_view_invalidate_sizes (icon_view);
5847 g_object_notify (G_OBJECT (icon_view), "margin");
5852 * gtk_icon_view_get_margin:
5853 * @icon_view: a #GtkIconView
5855 * Returns the value of the ::margin property.
5857 * Return value: the space at the borders
5862 gtk_icon_view_get_margin (GtkIconView *icon_view)
5864 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5866 return icon_view->priv->margin;
5870 * gtk_icon_view_set_item_padding:
5871 * @icon_view: a #GtkIconView
5872 * @item_padding: the item padding
5874 * Sets the #GtkIconView:item-padding property which specifies the padding
5875 * around each of the icon view's items.
5880 gtk_icon_view_set_item_padding (GtkIconView *icon_view,
5883 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5885 if (icon_view->priv->item_padding != item_padding)
5887 icon_view->priv->item_padding = item_padding;
5889 if (icon_view->priv->cell_area)
5890 gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5892 gtk_icon_view_invalidate_sizes (icon_view);
5894 g_object_notify (G_OBJECT (icon_view), "item-padding");
5899 * gtk_icon_view_get_item_padding:
5900 * @icon_view: a #GtkIconView
5902 * Returns the value of the ::item-padding property.
5904 * Return value: the padding around items
5909 gtk_icon_view_get_item_padding (GtkIconView *icon_view)
5911 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5913 return icon_view->priv->item_padding;
5916 /* Get/set whether drag_motion requested the drag data and
5917 * drag_data_received should thus not actually insert the data,
5918 * since the data doesn't result from a drop.
5921 set_status_pending (GdkDragContext *context,
5922 GdkDragAction suggested_action)
5924 g_object_set_data (G_OBJECT (context),
5925 I_("gtk-icon-view-status-pending"),
5926 GINT_TO_POINTER (suggested_action));
5929 static GdkDragAction
5930 get_status_pending (GdkDragContext *context)
5932 return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5933 "gtk-icon-view-status-pending"));
5937 unset_reorderable (GtkIconView *icon_view)
5939 if (icon_view->priv->reorderable)
5941 icon_view->priv->reorderable = FALSE;
5942 g_object_notify (G_OBJECT (icon_view), "reorderable");
5947 set_source_row (GdkDragContext *context,
5948 GtkTreeModel *model,
5949 GtkTreePath *source_row)
5952 g_object_set_data_full (G_OBJECT (context),
5953 I_("gtk-icon-view-source-row"),
5954 gtk_tree_row_reference_new (model, source_row),
5955 (GDestroyNotify) gtk_tree_row_reference_free);
5957 g_object_set_data_full (G_OBJECT (context),
5958 I_("gtk-icon-view-source-row"),
5963 get_source_row (GdkDragContext *context)
5965 GtkTreeRowReference *ref;
5967 ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5970 return gtk_tree_row_reference_get_path (ref);
5977 GtkTreeRowReference *dest_row;
5978 gboolean empty_view_drop;
5979 gboolean drop_append_mode;
5983 dest_row_free (gpointer data)
5985 DestRow *dr = (DestRow *)data;
5987 gtk_tree_row_reference_free (dr->dest_row);
5992 set_dest_row (GdkDragContext *context,
5993 GtkTreeModel *model,
5994 GtkTreePath *dest_row,
5995 gboolean empty_view_drop,
5996 gboolean drop_append_mode)
6002 g_object_set_data_full (G_OBJECT (context),
6003 I_("gtk-icon-view-dest-row"),
6008 dr = g_new0 (DestRow, 1);
6010 dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
6011 dr->empty_view_drop = empty_view_drop;
6012 dr->drop_append_mode = drop_append_mode;
6013 g_object_set_data_full (G_OBJECT (context),
6014 I_("gtk-icon-view-dest-row"),
6015 dr, (GDestroyNotify) dest_row_free);
6019 get_dest_row (GdkDragContext *context)
6023 dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
6027 GtkTreePath *path = NULL;
6030 path = gtk_tree_row_reference_get_path (dr->dest_row);
6031 else if (dr->empty_view_drop)
6032 path = gtk_tree_path_new_from_indices (0, -1);
6036 if (path && dr->drop_append_mode)
6037 gtk_tree_path_next (path);
6046 check_model_dnd (GtkTreeModel *model,
6047 GType required_iface,
6048 const gchar *signal)
6050 if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
6052 g_warning ("You must override the default '%s' handler "
6053 "on GtkIconView when using models that don't support "
6054 "the %s interface and enabling drag-and-drop. The simplest way to do this "
6055 "is to connect to '%s' and call "
6056 "g_signal_stop_emission_by_name() in your signal handler to prevent "
6057 "the default handler from running. Look at the source code "
6058 "for the default handler in gtkiconview.c to get an idea what "
6059 "your handler should do. (gtkiconview.c is in the GTK+ source "
6060 "code.) If you're using GTK+ from a language other than C, "
6061 "there may be a more natural way to override default handlers, e.g. via derivation.",
6062 signal, g_type_name (required_iface), signal);
6070 remove_scroll_timeout (GtkIconView *icon_view)
6072 if (icon_view->priv->scroll_timeout_id != 0)
6074 g_source_remove (icon_view->priv->scroll_timeout_id);
6076 icon_view->priv->scroll_timeout_id = 0;
6081 gtk_icon_view_autoscroll (GtkIconView *icon_view,
6085 gint px, py, width, height;
6086 gint hoffset, voffset;
6088 window = gtk_widget_get_window (GTK_WIDGET (icon_view));
6090 gdk_window_get_device_position (window, device, &px, &py, NULL);
6091 gdk_window_get_geometry (window, NULL, NULL, &width, &height);
6093 /* see if we are near the edge. */
6094 voffset = py - 2 * SCROLL_EDGE_SIZE;
6096 voffset = MAX (py - (height - 2 * SCROLL_EDGE_SIZE), 0);
6098 hoffset = px - 2 * SCROLL_EDGE_SIZE;
6100 hoffset = MAX (px - (width - 2 * SCROLL_EDGE_SIZE), 0);
6103 gtk_adjustment_set_value (icon_view->priv->vadjustment,
6104 gtk_adjustment_get_value (icon_view->priv->vadjustment) + voffset);
6107 gtk_adjustment_set_value (icon_view->priv->hadjustment,
6108 gtk_adjustment_get_value (icon_view->priv->hadjustment) + hoffset);
6112 GtkIconView *icon_view;
6117 drag_scroll_timeout (gpointer datap)
6119 DragScrollData *data = datap;
6121 gtk_icon_view_autoscroll (data->icon_view, data->device);
6127 drag_scroll_data_free (DragScrollData *data)
6129 g_slice_free (DragScrollData, data);
6133 set_destination (GtkIconView *icon_view,
6134 GdkDragContext *context,
6137 GdkDragAction *suggested_action,
6141 GtkTreePath *path = NULL;
6142 GtkIconViewDropPosition pos;
6143 GtkIconViewDropPosition old_pos;
6144 GtkTreePath *old_dest_path = NULL;
6145 gboolean can_drop = FALSE;
6147 widget = GTK_WIDGET (icon_view);
6149 *suggested_action = 0;
6152 if (!icon_view->priv->dest_set)
6154 /* someone unset us as a drag dest, note that if
6155 * we return FALSE drag_leave isn't called
6158 gtk_icon_view_set_drag_dest_item (icon_view,
6160 GTK_ICON_VIEW_DROP_LEFT);
6162 remove_scroll_timeout (GTK_ICON_VIEW (widget));
6164 return FALSE; /* no longer a drop site */
6167 *target = gtk_drag_dest_find_target (widget, context,
6168 gtk_drag_dest_get_target_list (widget));
6169 if (*target == GDK_NONE)
6172 if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos))
6175 GtkTreeModel *model;
6177 /* the row got dropped on empty space, let's setup a special case
6181 gtk_tree_path_free (path);
6183 model = gtk_icon_view_get_model (icon_view);
6185 n_children = gtk_tree_model_iter_n_children (model, NULL);
6188 pos = GTK_ICON_VIEW_DROP_BELOW;
6189 path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6193 pos = GTK_ICON_VIEW_DROP_ABOVE;
6194 path = gtk_tree_path_new_from_indices (0, -1);
6204 gtk_icon_view_get_drag_dest_item (icon_view,
6209 gtk_tree_path_free (old_dest_path);
6211 if (TRUE /* FIXME if the location droppable predicate */)
6219 GtkWidget *source_widget;
6221 *suggested_action = gdk_drag_context_get_suggested_action (context);
6222 source_widget = gtk_drag_get_source_widget (context);
6224 if (source_widget == widget)
6226 /* Default to MOVE, unless the user has
6227 * pressed ctrl or shift to affect available actions
6229 if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6230 *suggested_action = GDK_ACTION_MOVE;
6233 gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6238 /* can't drop here */
6239 gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6241 GTK_ICON_VIEW_DROP_LEFT);
6245 gtk_tree_path_free (path);
6251 get_logical_destination (GtkIconView *icon_view,
6252 gboolean *drop_append_mode)
6254 /* adjust path to point to the row the drop goes in front of */
6255 GtkTreePath *path = NULL;
6256 GtkIconViewDropPosition pos;
6258 *drop_append_mode = FALSE;
6260 gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6265 if (pos == GTK_ICON_VIEW_DROP_RIGHT ||
6266 pos == GTK_ICON_VIEW_DROP_BELOW)
6269 GtkTreeModel *model = icon_view->priv->model;
6271 if (!gtk_tree_model_get_iter (model, &iter, path) ||
6272 !gtk_tree_model_iter_next (model, &iter))
6273 *drop_append_mode = TRUE;
6276 *drop_append_mode = FALSE;
6277 gtk_tree_path_next (path);
6285 gtk_icon_view_maybe_begin_drag (GtkIconView *icon_view,
6286 GdkEventMotion *event)
6288 GtkWidget *widget = GTK_WIDGET (icon_view);
6289 GdkDragContext *context;
6290 GtkTreePath *path = NULL;
6292 GtkTreeModel *model;
6293 gboolean retval = FALSE;
6295 if (!icon_view->priv->source_set)
6298 if (icon_view->priv->pressed_button < 0)
6301 if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6302 icon_view->priv->press_start_x,
6303 icon_view->priv->press_start_y,
6304 event->x, event->y))
6307 model = gtk_icon_view_get_model (icon_view);
6312 button = icon_view->priv->pressed_button;
6313 icon_view->priv->pressed_button = -1;
6315 path = gtk_icon_view_get_path_at_pos (icon_view,
6316 icon_view->priv->press_start_x,
6317 icon_view->priv->press_start_y);
6322 if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6323 !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6327 /* FIXME Check whether we're a start button, if not return FALSE and
6331 /* Now we can begin the drag */
6335 context = gtk_drag_begin (widget,
6336 gtk_drag_source_get_target_list (widget),
6337 icon_view->priv->source_actions,
6341 set_source_row (context, model, path);
6345 gtk_tree_path_free (path);
6350 /* Source side drag signals */
6352 gtk_icon_view_drag_begin (GtkWidget *widget,
6353 GdkDragContext *context)
6355 GtkIconView *icon_view;
6356 GtkIconViewItem *item;
6357 cairo_surface_t *icon;
6361 icon_view = GTK_ICON_VIEW (widget);
6363 /* if the user uses a custom DnD impl, we don't set the icon here */
6364 if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6367 item = _gtk_icon_view_get_item_at_coords (icon_view,
6368 icon_view->priv->press_start_x,
6369 icon_view->priv->press_start_y,
6373 g_return_if_fail (item != NULL);
6375 x = icon_view->priv->press_start_x - item->cell_area.x + 1;
6376 y = icon_view->priv->press_start_y - item->cell_area.y + 1;
6378 path = gtk_tree_path_new_from_indices (item->index, -1);
6379 icon = gtk_icon_view_create_drag_icon (icon_view, path);
6380 gtk_tree_path_free (path);
6382 cairo_surface_set_device_offset (icon, -x, -y);
6384 gtk_drag_set_icon_surface (context, icon);
6386 cairo_surface_destroy (icon);
6390 gtk_icon_view_drag_end (GtkWidget *widget,
6391 GdkDragContext *context)
6397 gtk_icon_view_drag_data_get (GtkWidget *widget,
6398 GdkDragContext *context,
6399 GtkSelectionData *selection_data,
6403 GtkIconView *icon_view;
6404 GtkTreeModel *model;
6405 GtkTreePath *source_row;
6407 icon_view = GTK_ICON_VIEW (widget);
6408 model = gtk_icon_view_get_model (icon_view);
6413 if (!icon_view->priv->source_set)
6416 source_row = get_source_row (context);
6418 if (source_row == NULL)
6421 /* We can implement the GTK_TREE_MODEL_ROW target generically for
6422 * any model; for DragSource models there are some other targets
6426 if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6427 gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6432 /* If drag_data_get does nothing, try providing row data. */
6433 if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6434 gtk_tree_set_row_drag_data (selection_data,
6439 gtk_tree_path_free (source_row);
6443 gtk_icon_view_drag_data_delete (GtkWidget *widget,
6444 GdkDragContext *context)
6446 GtkTreeModel *model;
6447 GtkIconView *icon_view;
6448 GtkTreePath *source_row;
6450 icon_view = GTK_ICON_VIEW (widget);
6451 model = gtk_icon_view_get_model (icon_view);
6453 if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6456 if (!icon_view->priv->source_set)
6459 source_row = get_source_row (context);
6461 if (source_row == NULL)
6464 gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6467 gtk_tree_path_free (source_row);
6469 set_source_row (context, NULL, NULL);
6472 /* Target side drag signals */
6474 gtk_icon_view_drag_leave (GtkWidget *widget,
6475 GdkDragContext *context,
6478 GtkIconView *icon_view;
6480 icon_view = GTK_ICON_VIEW (widget);
6482 /* unset any highlight row */
6483 gtk_icon_view_set_drag_dest_item (icon_view,
6485 GTK_ICON_VIEW_DROP_LEFT);
6487 remove_scroll_timeout (icon_view);
6491 gtk_icon_view_drag_motion (GtkWidget *widget,
6492 GdkDragContext *context,
6497 GtkTreePath *path = NULL;
6498 GtkIconViewDropPosition pos;
6499 GtkIconView *icon_view;
6500 GdkDragAction suggested_action = 0;
6504 icon_view = GTK_ICON_VIEW (widget);
6506 if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6509 gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6511 /* we only know this *after* set_desination_row */
6512 empty = icon_view->priv->empty_view_drop;
6514 if (path == NULL && !empty)
6516 /* Can't drop here. */
6517 gdk_drag_status (context, 0, time);
6521 if (icon_view->priv->scroll_timeout_id == 0)
6523 DragScrollData *data = g_slice_new (DragScrollData);
6524 data->icon_view = icon_view;
6525 data->device = gdk_drag_context_get_device (context);
6527 icon_view->priv->scroll_timeout_id =
6528 gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 50, drag_scroll_timeout, data, (GDestroyNotify) drag_scroll_data_free);
6531 if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6533 /* Request data so we can use the source row when
6534 * determining whether to accept the drop
6536 set_status_pending (context, suggested_action);
6537 gtk_drag_get_data (widget, context, target, time);
6541 set_status_pending (context, 0);
6542 gdk_drag_status (context, suggested_action, time);
6547 gtk_tree_path_free (path);
6553 gtk_icon_view_drag_drop (GtkWidget *widget,
6554 GdkDragContext *context,
6559 GtkIconView *icon_view;
6561 GdkDragAction suggested_action = 0;
6562 GdkAtom target = GDK_NONE;
6563 GtkTreeModel *model;
6564 gboolean drop_append_mode;
6566 icon_view = GTK_ICON_VIEW (widget);
6567 model = gtk_icon_view_get_model (icon_view);
6569 remove_scroll_timeout (GTK_ICON_VIEW (widget));
6571 if (!icon_view->priv->dest_set)
6574 if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6577 if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6580 path = get_logical_destination (icon_view, &drop_append_mode);
6582 if (target != GDK_NONE && path != NULL)
6584 /* in case a motion had requested drag data, change things so we
6585 * treat drag data receives as a drop.
6587 set_status_pending (context, 0);
6588 set_dest_row (context, model, path,
6589 icon_view->priv->empty_view_drop, drop_append_mode);
6593 gtk_tree_path_free (path);
6595 /* Unset this thing */
6596 gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6598 if (target != GDK_NONE)
6600 gtk_drag_get_data (widget, context, target, time);
6608 gtk_icon_view_drag_data_received (GtkWidget *widget,
6609 GdkDragContext *context,
6612 GtkSelectionData *selection_data,
6617 gboolean accepted = FALSE;
6618 GtkTreeModel *model;
6619 GtkIconView *icon_view;
6620 GtkTreePath *dest_row;
6621 GdkDragAction suggested_action;
6622 gboolean drop_append_mode;
6624 icon_view = GTK_ICON_VIEW (widget);
6625 model = gtk_icon_view_get_model (icon_view);
6627 if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6630 if (!icon_view->priv->dest_set)
6633 suggested_action = get_status_pending (context);
6635 if (suggested_action)
6637 /* We are getting this data due to a request in drag_motion,
6638 * rather than due to a request in drag_drop, so we are just
6639 * supposed to call drag_status, not actually paste in the
6642 path = get_logical_destination (icon_view, &drop_append_mode);
6645 suggested_action = 0;
6647 if (suggested_action)
6649 if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6652 suggested_action = 0;
6655 gdk_drag_status (context, suggested_action, time);
6658 gtk_tree_path_free (path);
6660 /* If you can't drop, remove user drop indicator until the next motion */
6661 if (suggested_action == 0)
6662 gtk_icon_view_set_drag_dest_item (icon_view,
6664 GTK_ICON_VIEW_DROP_LEFT);
6669 dest_row = get_dest_row (context);
6671 if (dest_row == NULL)
6674 if (gtk_selection_data_get_length (selection_data) >= 0)
6676 if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6682 gtk_drag_finish (context,
6684 (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
6687 gtk_tree_path_free (dest_row);
6690 set_dest_row (context, NULL, NULL, FALSE, FALSE);
6693 /* Drag-and-Drop support */
6695 * gtk_icon_view_enable_model_drag_source:
6696 * @icon_view: a #GtkIconTreeView
6697 * @start_button_mask: Mask of allowed buttons to start drag
6698 * @targets: (array length=n_targets): the table of targets that the drag will
6700 * @n_targets: the number of items in @targets
6701 * @actions: the bitmask of possible actions for a drag from this
6704 * Turns @icon_view into a drag source for automatic DND. Calling this
6705 * method sets #GtkIconView:reorderable to %FALSE.
6710 gtk_icon_view_enable_model_drag_source (GtkIconView *icon_view,
6711 GdkModifierType start_button_mask,
6712 const GtkTargetEntry *targets,
6714 GdkDragAction actions)
6716 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6718 gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6720 icon_view->priv->start_button_mask = start_button_mask;
6721 icon_view->priv->source_actions = actions;
6723 icon_view->priv->source_set = TRUE;
6725 unset_reorderable (icon_view);
6729 * gtk_icon_view_enable_model_drag_dest:
6730 * @icon_view: a #GtkIconView
6731 * @targets: (array length=n_targets): the table of targets that the drag will
6733 * @n_targets: the number of items in @targets
6734 * @actions: the bitmask of possible actions for a drag to this
6737 * Turns @icon_view into a drop destination for automatic DND. Calling this
6738 * method sets #GtkIconView:reorderable to %FALSE.
6743 gtk_icon_view_enable_model_drag_dest (GtkIconView *icon_view,
6744 const GtkTargetEntry *targets,
6746 GdkDragAction actions)
6748 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6750 gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6752 icon_view->priv->dest_actions = actions;
6754 icon_view->priv->dest_set = TRUE;
6756 unset_reorderable (icon_view);
6760 * gtk_icon_view_unset_model_drag_source:
6761 * @icon_view: a #GtkIconView
6763 * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
6764 * method sets #GtkIconView:reorderable to %FALSE.
6769 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6771 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6773 if (icon_view->priv->source_set)
6775 gtk_drag_source_unset (GTK_WIDGET (icon_view));
6776 icon_view->priv->source_set = FALSE;
6779 unset_reorderable (icon_view);
6783 * gtk_icon_view_unset_model_drag_dest:
6784 * @icon_view: a #GtkIconView
6786 * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
6787 * method sets #GtkIconView:reorderable to %FALSE.
6792 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6794 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6796 if (icon_view->priv->dest_set)
6798 gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6799 icon_view->priv->dest_set = FALSE;
6802 unset_reorderable (icon_view);
6805 /* These are useful to implement your own custom stuff. */
6807 * gtk_icon_view_set_drag_dest_item:
6808 * @icon_view: a #GtkIconView
6809 * @path: (allow-none): The path of the item to highlight, or %NULL.
6810 * @pos: Specifies where to drop, relative to the item
6812 * Sets the item that is highlighted for feedback.
6817 gtk_icon_view_set_drag_dest_item (GtkIconView *icon_view,
6819 GtkIconViewDropPosition pos)
6821 /* Note; this function is exported to allow a custom DND
6822 * implementation, so it can't touch TreeViewDragInfo
6825 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6827 if (icon_view->priv->dest_item)
6829 GtkTreePath *current_path;
6830 current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6831 gtk_tree_row_reference_free (icon_view->priv->dest_item);
6832 icon_view->priv->dest_item = NULL;
6834 gtk_icon_view_queue_draw_path (icon_view, current_path);
6835 gtk_tree_path_free (current_path);
6838 /* special case a drop on an empty model */
6839 icon_view->priv->empty_view_drop = FALSE;
6840 if (pos == GTK_ICON_VIEW_DROP_ABOVE && path
6841 && gtk_tree_path_get_depth (path) == 1
6842 && gtk_tree_path_get_indices (path)[0] == 0)
6846 n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6849 if (n_children == 0)
6850 icon_view->priv->empty_view_drop = TRUE;
6853 icon_view->priv->dest_pos = pos;
6857 icon_view->priv->dest_item =
6858 gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view),
6859 icon_view->priv->model, path);
6861 gtk_icon_view_queue_draw_path (icon_view, path);
6866 * gtk_icon_view_get_drag_dest_item:
6867 * @icon_view: a #GtkIconView
6868 * @path: (out) (allow-none): Return location for the path of
6869 * the highlighted item, or %NULL.
6870 * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6872 * Gets information about the item that is highlighted for feedback.
6877 gtk_icon_view_get_drag_dest_item (GtkIconView *icon_view,
6879 GtkIconViewDropPosition *pos)
6881 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6885 if (icon_view->priv->dest_item)
6886 *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6892 *pos = icon_view->priv->dest_pos;
6896 * gtk_icon_view_get_dest_item_at_pos:
6897 * @icon_view: a #GtkIconView
6898 * @drag_x: the position to determine the destination item for
6899 * @drag_y: the position to determine the destination item for
6900 * @path: (out) (allow-none): Return location for the path of the item,
6902 * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6904 * Determines the destination item for a given position.
6906 * Return value: whether there is an item at the given position.
6911 gtk_icon_view_get_dest_item_at_pos (GtkIconView *icon_view,
6915 GtkIconViewDropPosition *pos)
6917 GtkIconViewItem *item;
6919 /* Note; this function is exported to allow a custom DND
6920 * implementation, so it can't touch TreeViewDragInfo
6923 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6924 g_return_val_if_fail (drag_x >= 0, FALSE);
6925 g_return_val_if_fail (drag_y >= 0, FALSE);
6926 g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6932 item = _gtk_icon_view_get_item_at_coords (icon_view,
6933 drag_x + gtk_adjustment_get_value (icon_view->priv->hadjustment),
6934 drag_y + gtk_adjustment_get_value (icon_view->priv->vadjustment),
6941 *path = gtk_tree_path_new_from_indices (item->index, -1);
6945 if (drag_x < item->cell_area.x + item->cell_area.width / 4)
6946 *pos = GTK_ICON_VIEW_DROP_LEFT;
6947 else if (drag_x > item->cell_area.x + item->cell_area.width * 3 / 4)
6948 *pos = GTK_ICON_VIEW_DROP_RIGHT;
6949 else if (drag_y < item->cell_area.y + item->cell_area.height / 4)
6950 *pos = GTK_ICON_VIEW_DROP_ABOVE;
6951 else if (drag_y > item->cell_area.y + item->cell_area.height * 3 / 4)
6952 *pos = GTK_ICON_VIEW_DROP_BELOW;
6954 *pos = GTK_ICON_VIEW_DROP_INTO;
6961 * gtk_icon_view_create_drag_icon:
6962 * @icon_view: a #GtkIconView
6963 * @path: a #GtkTreePath in @icon_view
6965 * Creates a #cairo_surface_t representation of the item at @path.
6966 * This image is used for a drag icon.
6968 * Return value: (transfer full): a newly-allocated surface of the drag icon.
6973 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6977 GtkStyleContext *context;
6979 cairo_surface_t *surface;
6983 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
6984 g_return_val_if_fail (path != NULL, NULL);
6986 widget = GTK_WIDGET (icon_view);
6987 context = gtk_widget_get_style_context (widget);
6989 if (!gtk_widget_get_realized (widget))
6992 index = gtk_tree_path_get_indices (path)[0];
6994 for (l = icon_view->priv->items; l; l = l->next)
6996 GtkIconViewItem *item = l->data;
6998 if (index == item->index)
7000 GdkRectangle rect = {
7001 item->cell_area.x - icon_view->priv->item_padding,
7002 item->cell_area.y - icon_view->priv->item_padding,
7003 item->cell_area.width + icon_view->priv->item_padding * 2,
7004 item->cell_area.height + icon_view->priv->item_padding * 2
7007 surface = gdk_window_create_similar_surface (icon_view->priv->bin_window,
7008 CAIRO_CONTENT_COLOR,
7012 cr = cairo_create (surface);
7013 cairo_set_line_width (cr, 1.);
7015 gtk_render_background (context, cr, 0, 0,
7016 rect.width + 2, rect.height + 2);
7020 cairo_rectangle (cr, 1, 1, rect.width, rect.height);
7023 gtk_icon_view_paint_item (icon_view, cr, item,
7024 icon_view->priv->item_padding + 1,
7025 icon_view->priv->item_padding + 1, FALSE);
7029 cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
7030 cairo_rectangle (cr, 0.5, 0.5, rect.width + 1, rect.height + 1);
7043 * gtk_icon_view_get_reorderable:
7044 * @icon_view: a #GtkIconView
7046 * Retrieves whether the user can reorder the list via drag-and-drop.
7047 * See gtk_icon_view_set_reorderable().
7049 * Return value: %TRUE if the list can be reordered.
7054 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
7056 g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7058 return icon_view->priv->reorderable;
7061 static const GtkTargetEntry item_targets[] = {
7062 { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
7067 * gtk_icon_view_set_reorderable:
7068 * @icon_view: A #GtkIconView.
7069 * @reorderable: %TRUE, if the list of items can be reordered.
7071 * This function is a convenience function to allow you to reorder models that
7072 * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface. Both
7073 * #GtkTreeStore and #GtkListStore support these. If @reorderable is %TRUE, then
7074 * the user can reorder the model by dragging and dropping rows. The
7075 * developer can listen to these changes by connecting to the model's
7076 * row_inserted and row_deleted signals. The reordering is implemented by setting up
7077 * the icon view as a drag source and destination. Therefore, drag and
7078 * drop can not be used in a reorderable view for any other purpose.
7080 * This function does not give you any degree of control over the order -- any
7081 * reordering is allowed. If more control is needed, you should probably
7082 * handle drag and drop manually.
7087 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
7088 gboolean reorderable)
7090 g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7092 reorderable = reorderable != FALSE;
7094 if (icon_view->priv->reorderable == reorderable)
7099 gtk_icon_view_enable_model_drag_source (icon_view,
7102 G_N_ELEMENTS (item_targets),
7104 gtk_icon_view_enable_model_drag_dest (icon_view,
7106 G_N_ELEMENTS (item_targets),
7111 gtk_icon_view_unset_model_drag_source (icon_view);
7112 gtk_icon_view_unset_model_drag_dest (icon_view);
7115 icon_view->priv->reorderable = reorderable;
7117 g_object_notify (G_OBJECT (icon_view), "reorderable");
7121 gtk_icon_view_buildable_custom_tag_start (GtkBuildable *buildable,
7122 GtkBuilder *builder,
7124 const gchar *tagname,
7125 GMarkupParser *parser,
7128 if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
7129 tagname, parser, data))
7132 return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
7133 tagname, parser, data);
7137 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
7138 GtkBuilder *builder,
7140 const gchar *tagname,
7143 if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder,
7144 child, tagname, data))
7145 parent_buildable_iface->custom_tag_end (buildable, builder,
7146 child, tagname, data);