1 /* gtkiconview.c
2  * Copyright (C) 2002, 2004  Anders Carlsson <andersca@gnu.org>
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 
18 #include "config.h"
19 
20 #include <string.h>
21 
22 #include "gtkiconview.h"
23 #include "gtkiconviewprivate.h"
24 
25 #include "gtkadjustmentprivate.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"
35 #include "gtkdnd.h"
36 #include "gtkmain.h"
37 #include "gtkintl.h"
38 #include "gtkaccessible.h"
39 #include "gtkwindow.h"
40 #include "gtkentry.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 "gtkcssnodeprivate.h"
48 #include "gtkwidgetprivate.h"
49 #include "gtkstylecontextprivate.h"
50 #include "a11y/gtkiconviewaccessibleprivate.h"
51 
52 /**
53  * SECTION:gtkiconview
54  * @title: GtkIconView
55  * @short_description: A widget which displays a list of icons in a grid
56  *
57  * #GtkIconView provides an alternative view on a #GtkTreeModel.
58  * It displays the model as a grid of icons with labels. Like
59  * #GtkTreeView, it allows to select one or multiple items
60  * (depending on the selection mode, see gtk_icon_view_set_selection_mode()).
61  * In addition to selection with the arrow keys, #GtkIconView supports
62  * rubberband selection, which is controlled by dragging the pointer.
63  *
64  * Note that if the tree model is backed by an actual tree store (as
65  * opposed to a flat list where the mapping to icons is obvious),
66  * #GtkIconView will only display the first level of the tree and
67  * ignore the tree’s branches.
68  *
69  * # CSS nodes
70  *
71  * |[<!-- language="plain" -->
72  * iconview.view
73  * ╰── [rubberband]
74  * ]|
75  *
76  * GtkIconView has a single CSS node with name iconview and style class .view.
77  * For rubberband selection, a subnode with name rubberband is used.
78  */
79 
80 #define SCROLL_EDGE_SIZE 15
81 
82 typedef struct _GtkIconViewChild GtkIconViewChild;
83 struct _GtkIconViewChild
84 {
85   GtkWidget    *widget;
86   GdkRectangle  area;
87 };
88 
89 /* Signals */
90 enum
91 {
92   ITEM_ACTIVATED,
93   SELECTION_CHANGED,
94   SELECT_ALL,
95   UNSELECT_ALL,
96   SELECT_CURSOR_ITEM,
97   TOGGLE_CURSOR_ITEM,
98   MOVE_CURSOR,
99   ACTIVATE_CURSOR_ITEM,
100   LAST_SIGNAL
101 };
102 
103 /* Properties */
104 enum
105 {
106   PROP_0,
107   PROP_PIXBUF_COLUMN,
108   PROP_TEXT_COLUMN,
109   PROP_MARKUP_COLUMN,
110   PROP_SELECTION_MODE,
111   PROP_ITEM_ORIENTATION,
112   PROP_MODEL,
113   PROP_COLUMNS,
114   PROP_ITEM_WIDTH,
115   PROP_SPACING,
116   PROP_ROW_SPACING,
117   PROP_COLUMN_SPACING,
118   PROP_MARGIN,
119   PROP_REORDERABLE,
120   PROP_TOOLTIP_COLUMN,
121   PROP_ITEM_PADDING,
122   PROP_CELL_AREA,
123   PROP_HADJUSTMENT,
124   PROP_VADJUSTMENT,
125   PROP_HSCROLL_POLICY,
126   PROP_VSCROLL_POLICY,
127   PROP_ACTIVATE_ON_SINGLE_CLICK
128 };
129 
130 /* GObject vfuncs */
131 static void             gtk_icon_view_cell_layout_init          (GtkCellLayoutIface *iface);
132 static void             gtk_icon_view_dispose                   (GObject            *object);
133 static void             gtk_icon_view_constructed               (GObject            *object);
134 static void             gtk_icon_view_set_property              (GObject            *object,
135 								 guint               prop_id,
136 								 const GValue       *value,
137 								 GParamSpec         *pspec);
138 static void             gtk_icon_view_get_property              (GObject            *object,
139 								 guint               prop_id,
140 								 GValue             *value,
141 								 GParamSpec         *pspec);
142 /* GtkWidget vfuncs */
143 static void             gtk_icon_view_destroy                   (GtkWidget          *widget);
144 static void             gtk_icon_view_realize                   (GtkWidget          *widget);
145 static void             gtk_icon_view_unrealize                 (GtkWidget          *widget);
146 static GtkSizeRequestMode gtk_icon_view_get_request_mode        (GtkWidget          *widget);
147 static void             gtk_icon_view_get_preferred_width       (GtkWidget          *widget,
148 								 gint               *minimum,
149 								 gint               *natural);
150 static void             gtk_icon_view_get_preferred_width_for_height
151                                                                 (GtkWidget          *widget,
152                                                                  gint                height,
153 								 gint               *minimum,
154 								 gint               *natural);
155 static void             gtk_icon_view_get_preferred_height      (GtkWidget          *widget,
156 								 gint               *minimum,
157 								 gint               *natural);
158 static void             gtk_icon_view_get_preferred_height_for_width
159                                                                 (GtkWidget          *widget,
160                                                                  gint                width,
161 								 gint               *minimum,
162 								 gint               *natural);
163 static void             gtk_icon_view_size_allocate             (GtkWidget          *widget,
164 								 GtkAllocation      *allocation);
165 static gboolean         gtk_icon_view_draw                      (GtkWidget          *widget,
166                                                                  cairo_t            *cr);
167 static gboolean         gtk_icon_view_motion                    (GtkWidget          *widget,
168 								 GdkEventMotion     *event);
169 static gboolean         gtk_icon_view_leave                     (GtkWidget          *widget,
170 								 GdkEventCrossing   *event);
171 static gboolean         gtk_icon_view_button_press              (GtkWidget          *widget,
172 								 GdkEventButton     *event);
173 static gboolean         gtk_icon_view_button_release            (GtkWidget          *widget,
174 								 GdkEventButton     *event);
175 static gboolean         gtk_icon_view_key_press                 (GtkWidget          *widget,
176 								 GdkEventKey        *event);
177 static gboolean         gtk_icon_view_key_release               (GtkWidget          *widget,
178 								 GdkEventKey        *event);
179 
180 
181 /* GtkContainer vfuncs */
182 static void             gtk_icon_view_remove                    (GtkContainer       *container,
183 								 GtkWidget          *widget);
184 static void             gtk_icon_view_forall                    (GtkContainer       *container,
185 								 gboolean            include_internals,
186 								 GtkCallback         callback,
187 								 gpointer            callback_data);
188 
189 /* GtkIconView vfuncs */
190 static void             gtk_icon_view_real_select_all           (GtkIconView        *icon_view);
191 static void             gtk_icon_view_real_unselect_all         (GtkIconView        *icon_view);
192 static void             gtk_icon_view_real_select_cursor_item   (GtkIconView        *icon_view);
193 static void             gtk_icon_view_real_toggle_cursor_item   (GtkIconView        *icon_view);
194 static gboolean         gtk_icon_view_real_activate_cursor_item (GtkIconView        *icon_view);
195 
196  /* Internal functions */
197 static void                 gtk_icon_view_set_hadjustment_values         (GtkIconView            *icon_view);
198 static void                 gtk_icon_view_set_vadjustment_values         (GtkIconView            *icon_view);
199 static void                 gtk_icon_view_set_hadjustment                (GtkIconView            *icon_view,
200                                                                           GtkAdjustment          *adjustment);
201 static void                 gtk_icon_view_set_vadjustment                (GtkIconView            *icon_view,
202                                                                           GtkAdjustment          *adjustment);
203 static void                 gtk_icon_view_adjustment_changed             (GtkAdjustment          *adjustment,
204 									  GtkIconView            *icon_view);
205 static void                 gtk_icon_view_layout                         (GtkIconView            *icon_view);
206 static void                 gtk_icon_view_paint_item                     (GtkIconView            *icon_view,
207 									  cairo_t                *cr,
208 									  GtkIconViewItem        *item,
209 									  gint                    x,
210 									  gint                    y,
211 									  gboolean                draw_focus);
212 static void                 gtk_icon_view_paint_rubberband               (GtkIconView            *icon_view,
213 								          cairo_t                *cr);
214 static void                 gtk_icon_view_queue_draw_path                (GtkIconView *icon_view,
215 									  GtkTreePath *path);
216 static void                 gtk_icon_view_queue_draw_item                (GtkIconView            *icon_view,
217 									  GtkIconViewItem        *item);
218 static void                 gtk_icon_view_start_rubberbanding            (GtkIconView            *icon_view,
219                                                                           GdkDevice              *device,
220 									  gint                    x,
221 									  gint                    y);
222 static void                 gtk_icon_view_stop_rubberbanding             (GtkIconView            *icon_view);
223 static void                 gtk_icon_view_update_rubberband_selection    (GtkIconView            *icon_view);
224 static gboolean             gtk_icon_view_item_hit_test                  (GtkIconView            *icon_view,
225 									  GtkIconViewItem        *item,
226 									  gint                    x,
227 									  gint                    y,
228 									  gint                    width,
229 									  gint                    height);
230 static gboolean             gtk_icon_view_unselect_all_internal          (GtkIconView            *icon_view);
231 static void                 gtk_icon_view_update_rubberband              (gpointer                data);
232 static void                 gtk_icon_view_item_invalidate_size           (GtkIconViewItem        *item);
233 static void                 gtk_icon_view_invalidate_sizes               (GtkIconView            *icon_view);
234 static void                 gtk_icon_view_add_move_binding               (GtkBindingSet          *binding_set,
235 									  guint                   keyval,
236 									  guint                   modmask,
237 									  GtkMovementStep         step,
238 									  gint                    count);
239 static gboolean             gtk_icon_view_real_move_cursor               (GtkIconView            *icon_view,
240 									  GtkMovementStep         step,
241 									  gint                    count);
242 static void                 gtk_icon_view_move_cursor_up_down            (GtkIconView            *icon_view,
243 									  gint                    count);
244 static void                 gtk_icon_view_move_cursor_page_up_down       (GtkIconView            *icon_view,
245 									  gint                    count);
246 static void                 gtk_icon_view_move_cursor_left_right         (GtkIconView            *icon_view,
247 									  gint                    count);
248 static void                 gtk_icon_view_move_cursor_start_end          (GtkIconView            *icon_view,
249 									  gint                    count);
250 static void                 gtk_icon_view_scroll_to_item                 (GtkIconView            *icon_view,
251 									  GtkIconViewItem        *item);
252 static gboolean             gtk_icon_view_select_all_between             (GtkIconView            *icon_view,
253 									  GtkIconViewItem        *anchor,
254 									  GtkIconViewItem        *cursor);
255 
256 static void                 gtk_icon_view_ensure_cell_area               (GtkIconView            *icon_view,
257                                                                           GtkCellArea            *cell_area);
258 
259 static GtkCellArea         *gtk_icon_view_cell_layout_get_area           (GtkCellLayout          *layout);
260 
261 static void                 gtk_icon_view_item_selected_changed          (GtkIconView            *icon_view,
262 		                                                          GtkIconViewItem        *item);
263 
264 static void                 gtk_icon_view_add_editable                   (GtkCellArea            *area,
265 									  GtkCellRenderer        *renderer,
266 									  GtkCellEditable        *editable,
267 									  GdkRectangle           *cell_area,
268 									  const gchar            *path,
269 									  GtkIconView            *icon_view);
270 static void                 gtk_icon_view_remove_editable                (GtkCellArea            *area,
271 									  GtkCellRenderer        *renderer,
272 									  GtkCellEditable        *editable,
273 									  GtkIconView            *icon_view);
274 static void                 update_text_cell                             (GtkIconView            *icon_view);
275 static void                 update_pixbuf_cell                           (GtkIconView            *icon_view);
276 
277 /* Source side drag signals */
278 static void gtk_icon_view_drag_begin       (GtkWidget        *widget,
279                                             GdkDragContext   *context);
280 static void gtk_icon_view_drag_end         (GtkWidget        *widget,
281                                             GdkDragContext   *context);
282 static void gtk_icon_view_drag_data_get    (GtkWidget        *widget,
283                                             GdkDragContext   *context,
284                                             GtkSelectionData *selection_data,
285                                             guint             info,
286                                             guint             time);
287 static void gtk_icon_view_drag_data_delete (GtkWidget        *widget,
288                                             GdkDragContext   *context);
289 
290 /* Target side drag signals */
291 static void     gtk_icon_view_drag_leave         (GtkWidget        *widget,
292                                                   GdkDragContext   *context,
293                                                   guint             time);
294 static gboolean gtk_icon_view_drag_motion        (GtkWidget        *widget,
295                                                   GdkDragContext   *context,
296                                                   gint              x,
297                                                   gint              y,
298                                                   guint             time);
299 static gboolean gtk_icon_view_drag_drop          (GtkWidget        *widget,
300                                                   GdkDragContext   *context,
301                                                   gint              x,
302                                                   gint              y,
303                                                   guint             time);
304 static void     gtk_icon_view_drag_data_received (GtkWidget        *widget,
305                                                   GdkDragContext   *context,
306                                                   gint              x,
307                                                   gint              y,
308                                                   GtkSelectionData *selection_data,
309                                                   guint             info,
310                                                   guint             time);
311 static gboolean gtk_icon_view_maybe_begin_drag   (GtkIconView             *icon_view,
312 					   	  GdkEventMotion          *event);
313 
314 static void     remove_scroll_timeout            (GtkIconView *icon_view);
315 
316 /* GtkBuildable */
317 static GtkBuildableIface *parent_buildable_iface;
318 static void     gtk_icon_view_buildable_init             (GtkBuildableIface *iface);
319 static gboolean gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
320 							  GtkBuilder    *builder,
321 							  GObject       *child,
322 							  const gchar   *tagname,
323 							  GMarkupParser *parser,
324 							  gpointer      *data);
325 static void     gtk_icon_view_buildable_custom_tag_end   (GtkBuildable  *buildable,
326 							  GtkBuilder    *builder,
327 							  GObject       *child,
328 							  const gchar   *tagname,
329 							  gpointer      *data);
330 
331 static guint icon_view_signals[LAST_SIGNAL] = { 0 };
332 
G_DEFINE_TYPE_WITH_CODE(GtkIconView,gtk_icon_view,GTK_TYPE_CONTAINER,G_ADD_PRIVATE (GtkIconView)G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,gtk_icon_view_cell_layout_init)G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,gtk_icon_view_buildable_init)G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE,NULL))333 G_DEFINE_TYPE_WITH_CODE (GtkIconView, gtk_icon_view, GTK_TYPE_CONTAINER,
334                          G_ADD_PRIVATE (GtkIconView)
335 			 G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
336 						gtk_icon_view_cell_layout_init)
337 			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
338 						gtk_icon_view_buildable_init)
339 			 G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
340 
341 static void
342 gtk_icon_view_class_init (GtkIconViewClass *klass)
343 {
344   GObjectClass *gobject_class;
345   GtkWidgetClass *widget_class;
346   GtkContainerClass *container_class;
347   GtkBindingSet *binding_set;
348 
349   binding_set = gtk_binding_set_by_class (klass);
350 
351   gobject_class = (GObjectClass *) klass;
352   widget_class = (GtkWidgetClass *) klass;
353   container_class = (GtkContainerClass *) klass;
354 
355   gobject_class->constructed = gtk_icon_view_constructed;
356   gobject_class->dispose = gtk_icon_view_dispose;
357   gobject_class->set_property = gtk_icon_view_set_property;
358   gobject_class->get_property = gtk_icon_view_get_property;
359 
360   widget_class->destroy = gtk_icon_view_destroy;
361   widget_class->realize = gtk_icon_view_realize;
362   widget_class->unrealize = gtk_icon_view_unrealize;
363   widget_class->get_request_mode = gtk_icon_view_get_request_mode;
364   widget_class->get_preferred_width = gtk_icon_view_get_preferred_width;
365   widget_class->get_preferred_height = gtk_icon_view_get_preferred_height;
366   widget_class->get_preferred_width_for_height = gtk_icon_view_get_preferred_width_for_height;
367   widget_class->get_preferred_height_for_width = gtk_icon_view_get_preferred_height_for_width;
368   widget_class->size_allocate = gtk_icon_view_size_allocate;
369   widget_class->draw = gtk_icon_view_draw;
370   widget_class->motion_notify_event = gtk_icon_view_motion;
371   widget_class->leave_notify_event = gtk_icon_view_leave;
372   widget_class->button_press_event = gtk_icon_view_button_press;
373   widget_class->button_release_event = gtk_icon_view_button_release;
374   widget_class->key_press_event = gtk_icon_view_key_press;
375   widget_class->key_release_event = gtk_icon_view_key_release;
376   widget_class->drag_begin = gtk_icon_view_drag_begin;
377   widget_class->drag_end = gtk_icon_view_drag_end;
378   widget_class->drag_data_get = gtk_icon_view_drag_data_get;
379   widget_class->drag_data_delete = gtk_icon_view_drag_data_delete;
380   widget_class->drag_leave = gtk_icon_view_drag_leave;
381   widget_class->drag_motion = gtk_icon_view_drag_motion;
382   widget_class->drag_drop = gtk_icon_view_drag_drop;
383   widget_class->drag_data_received = gtk_icon_view_drag_data_received;
384 
385   container_class->remove = gtk_icon_view_remove;
386   container_class->forall = gtk_icon_view_forall;
387 
388   klass->select_all = gtk_icon_view_real_select_all;
389   klass->unselect_all = gtk_icon_view_real_unselect_all;
390   klass->select_cursor_item = gtk_icon_view_real_select_cursor_item;
391   klass->toggle_cursor_item = gtk_icon_view_real_toggle_cursor_item;
392   klass->activate_cursor_item = gtk_icon_view_real_activate_cursor_item;
393   klass->move_cursor = gtk_icon_view_real_move_cursor;
394 
395   /* Properties */
396   /**
397    * GtkIconView:selection-mode:
398    *
399    * The ::selection-mode property specifies the selection mode of
400    * icon view. If the mode is #GTK_SELECTION_MULTIPLE, rubberband selection
401    * is enabled, for the other modes, only keyboard selection is possible.
402    *
403    * Since: 2.6
404    */
405   g_object_class_install_property (gobject_class,
406 				   PROP_SELECTION_MODE,
407 				   g_param_spec_enum ("selection-mode",
408 						      P_("Selection mode"),
409 						      P_("The selection mode"),
410 						      GTK_TYPE_SELECTION_MODE,
411 						      GTK_SELECTION_SINGLE,
412 						      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
413 
414   /**
415    * GtkIconView:pixbuf-column:
416    *
417    * The ::pixbuf-column property contains the number of the model column
418    * containing the pixbufs which are displayed. The pixbuf column must be
419    * of type #GDK_TYPE_PIXBUF. Setting this property to -1 turns off the
420    * display of pixbufs.
421    *
422    * Since: 2.6
423    */
424   g_object_class_install_property (gobject_class,
425 				   PROP_PIXBUF_COLUMN,
426 				   g_param_spec_int ("pixbuf-column",
427 						     P_("Pixbuf column"),
428 						     P_("Model column used to retrieve the icon pixbuf from"),
429 						     -1, G_MAXINT, -1,
430 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
431 
432   /**
433    * GtkIconView:text-column:
434    *
435    * The ::text-column property contains the number of the model column
436    * containing the texts which are displayed. The text column must be
437    * of type #G_TYPE_STRING. If this property and the :markup-column
438    * property are both set to -1, no texts are displayed.
439    *
440    * Since: 2.6
441    */
442   g_object_class_install_property (gobject_class,
443 				   PROP_TEXT_COLUMN,
444 				   g_param_spec_int ("text-column",
445 						     P_("Text column"),
446 						     P_("Model column used to retrieve the text from"),
447 						     -1, G_MAXINT, -1,
448 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
449 
450 
451   /**
452    * GtkIconView:markup-column:
453    *
454    * The ::markup-column property contains the number of the model column
455    * containing markup information to be displayed. The markup column must be
456    * of type #G_TYPE_STRING. If this property and the :text-column property
457    * are both set to column numbers, it overrides the text column.
458    * If both are set to -1, no texts are displayed.
459    *
460    * Since: 2.6
461    */
462   g_object_class_install_property (gobject_class,
463 				   PROP_MARKUP_COLUMN,
464 				   g_param_spec_int ("markup-column",
465 						     P_("Markup column"),
466 						     P_("Model column used to retrieve the text if using Pango markup"),
467 						     -1, G_MAXINT, -1,
468 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
469 
470   g_object_class_install_property (gobject_class,
471                                    PROP_MODEL,
472                                    g_param_spec_object ("model",
473 							P_("Icon View Model"),
474 							P_("The model for the icon view"),
475 							GTK_TYPE_TREE_MODEL,
476 							GTK_PARAM_READWRITE));
477 
478   /**
479    * GtkIconView:columns:
480    *
481    * The columns property contains the number of the columns in which the
482    * items should be displayed. If it is -1, the number of columns will
483    * be chosen automatically to fill the available area.
484    *
485    * Since: 2.6
486    */
487   g_object_class_install_property (gobject_class,
488 				   PROP_COLUMNS,
489 				   g_param_spec_int ("columns",
490 						     P_("Number of columns"),
491 						     P_("Number of columns to display"),
492 						     -1, G_MAXINT, -1,
493 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
494 
495 
496   /**
497    * GtkIconView:item-width:
498    *
499    * The item-width property specifies the width to use for each item.
500    * If it is set to -1, the icon view will automatically determine a
501    * suitable item size.
502    *
503    * Since: 2.6
504    */
505   g_object_class_install_property (gobject_class,
506 				   PROP_ITEM_WIDTH,
507 				   g_param_spec_int ("item-width",
508 						     P_("Width for each item"),
509 						     P_("The width used for each item"),
510 						     -1, G_MAXINT, -1,
511 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
512 
513   /**
514    * GtkIconView:spacing:
515    *
516    * The spacing property specifies the space which is inserted between
517    * the cells (i.e. the icon and the text) of an item.
518    *
519    * Since: 2.6
520    */
521   g_object_class_install_property (gobject_class,
522                                    PROP_SPACING,
523                                    g_param_spec_int ("spacing",
524 						     P_("Spacing"),
525 						     P_("Space which is inserted between cells of an item"),
526 						     0, G_MAXINT, 0,
527 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
528 
529   /**
530    * GtkIconView:row-spacing:
531    *
532    * The row-spacing property specifies the space which is inserted between
533    * the rows of the icon view.
534    *
535    * Since: 2.6
536    */
537   g_object_class_install_property (gobject_class,
538                                    PROP_ROW_SPACING,
539                                    g_param_spec_int ("row-spacing",
540 						     P_("Row Spacing"),
541 						     P_("Space which is inserted between grid rows"),
542 						     0, G_MAXINT, 6,
543 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
544 
545   /**
546    * GtkIconView:column-spacing:
547    *
548    * The column-spacing property specifies the space which is inserted between
549    * the columns of the icon view.
550    *
551    * Since: 2.6
552    */
553   g_object_class_install_property (gobject_class,
554                                    PROP_COLUMN_SPACING,
555                                    g_param_spec_int ("column-spacing",
556 						     P_("Column Spacing"),
557 						     P_("Space which is inserted between grid columns"),
558 						     0, G_MAXINT, 6,
559 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
560 
561   /**
562    * GtkIconView:margin:
563    *
564    * The margin property specifies the space which is inserted
565    * at the edges of the icon view.
566    *
567    * Since: 2.6
568    */
569   g_object_class_install_property (gobject_class,
570                                    PROP_MARGIN,
571                                    g_param_spec_int ("margin",
572 						     P_("Margin"),
573 						     P_("Space which is inserted at the edges of the icon view"),
574 						     0, G_MAXINT, 6,
575 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
576 
577   /**
578    * GtkIconView:item-orientation:
579    *
580    * The item-orientation property specifies how the cells (i.e. the icon and
581    * the text) of the item are positioned relative to each other.
582    *
583    * Since: 2.6
584    */
585   g_object_class_install_property (gobject_class,
586 				   PROP_ITEM_ORIENTATION,
587 				   g_param_spec_enum ("item-orientation",
588 						      P_("Item Orientation"),
589 						      P_("How the text and icon of each item are positioned relative to each other"),
590 						      GTK_TYPE_ORIENTATION,
591 						      GTK_ORIENTATION_VERTICAL,
592 						      GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
593 
594   /**
595    * GtkIconView:reorderable:
596    *
597    * The reorderable property specifies if the items can be reordered
598    * by DND.
599    *
600    * Since: 2.8
601    */
602   g_object_class_install_property (gobject_class,
603                                    PROP_REORDERABLE,
604                                    g_param_spec_boolean ("reorderable",
605 							 P_("Reorderable"),
606 							 P_("View is reorderable"),
607 							 FALSE,
608 							 G_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
609 
610     g_object_class_install_property (gobject_class,
611                                      PROP_TOOLTIP_COLUMN,
612                                      g_param_spec_int ("tooltip-column",
613                                                        P_("Tooltip Column"),
614                                                        P_("The column in the model containing the tooltip texts for the items"),
615                                                        -1,
616                                                        G_MAXINT,
617                                                        -1,
618                                                        GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
619 
620   /**
621    * GtkIconView:item-padding:
622    *
623    * The item-padding property specifies the padding around each
624    * of the icon view's item.
625    *
626    * Since: 2.18
627    */
628   g_object_class_install_property (gobject_class,
629                                    PROP_ITEM_PADDING,
630                                    g_param_spec_int ("item-padding",
631 						     P_("Item Padding"),
632 						     P_("Padding around icon view items"),
633 						     0, G_MAXINT, 6,
634 						     GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
635 
636   /**
637    * GtkIconView:cell-area:
638    *
639    * The #GtkCellArea used to layout cell renderers for this view.
640    *
641    * If no area is specified when creating the icon view with gtk_icon_view_new_with_area()
642    * a #GtkCellAreaBox will be used.
643    *
644    * Since: 3.0
645    */
646   g_object_class_install_property (gobject_class,
647 				   PROP_CELL_AREA,
648 				   g_param_spec_object ("cell-area",
649 							P_("Cell Area"),
650 							P_("The GtkCellArea used to layout cells"),
651 							GTK_TYPE_CELL_AREA,
652 							GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
653 
654   /**
655    * GtkIconView:activate-on-single-click:
656    *
657    * The activate-on-single-click property specifies whether the "item-activated" signal
658    * will be emitted after a single click.
659    *
660    * Since: 3.8
661    */
662   g_object_class_install_property (gobject_class,
663                                    PROP_ACTIVATE_ON_SINGLE_CLICK,
664                                    g_param_spec_boolean ("activate-on-single-click",
665 							 P_("Activate on Single Click"),
666 							 P_("Activate row on a single click"),
667 							 FALSE,
668 							 GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
669 
670   /* Scrollable interface properties */
671   g_object_class_override_property (gobject_class, PROP_HADJUSTMENT,    "hadjustment");
672   g_object_class_override_property (gobject_class, PROP_VADJUSTMENT,    "vadjustment");
673   g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
674   g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
675 
676   /* Style properties */
677   /**
678    * GtkIconView:selection-box-color:
679    *
680    * The color of the selection box.
681    *
682    * Deprecated: 3.20: The color of the selection box is determined by CSS;
683    *     the value of this style property is ignored.
684    */
685   gtk_widget_class_install_style_property (widget_class,
686                                            g_param_spec_boxed ("selection-box-color",
687                                                                P_("Selection Box Color"),
688                                                                P_("Color of the selection box"),
689                                                                g_type_from_name ("GdkColor"),
690                                                                GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
691 
692 
693   /**
694    * GtkIconView:selection-box-alpha:
695    *
696    * The opacity of the selection box.
697    *
698    * Deprecated: 3.20: The opacity of the selection box is determined by CSS;
699    *     the value of this style property is ignored.
700    */
701   gtk_widget_class_install_style_property (widget_class,
702                                            g_param_spec_uchar ("selection-box-alpha",
703                                                                P_("Selection Box Alpha"),
704                                                                P_("Opacity of the selection box"),
705                                                                0, 0xff,
706                                                                0x40,
707                                                                GTK_PARAM_READABLE|G_PARAM_DEPRECATED));
708 
709   /* Signals */
710   /**
711    * GtkIconView::item-activated:
712    * @iconview: the object on which the signal is emitted
713    * @path: the #GtkTreePath for the activated item
714    *
715    * The ::item-activated signal is emitted when the method
716    * gtk_icon_view_item_activated() is called, when the user double
717    * clicks an item with the "activate-on-single-click" property set
718    * to %FALSE, or when the user single clicks an item when the
719    * "activate-on-single-click" property set to %TRUE. It is also
720    * emitted when a non-editable item is selected and one of the keys:
721    * Space, Return or Enter is pressed.
722    */
723   icon_view_signals[ITEM_ACTIVATED] =
724     g_signal_new (I_("item-activated"),
725 		  G_TYPE_FROM_CLASS (gobject_class),
726 		  G_SIGNAL_RUN_LAST,
727 		  G_STRUCT_OFFSET (GtkIconViewClass, item_activated),
728 		  NULL, NULL,
729 		  NULL,
730 		  G_TYPE_NONE, 1,
731 		  GTK_TYPE_TREE_PATH);
732 
733   /**
734    * GtkIconView::selection-changed:
735    * @iconview: the object on which the signal is emitted
736    *
737    * The ::selection-changed signal is emitted when the selection
738    * (i.e. the set of selected items) changes.
739    */
740   icon_view_signals[SELECTION_CHANGED] =
741     g_signal_new (I_("selection-changed"),
742 		  G_TYPE_FROM_CLASS (gobject_class),
743 		  G_SIGNAL_RUN_FIRST,
744 		  G_STRUCT_OFFSET (GtkIconViewClass, selection_changed),
745 		  NULL, NULL,
746 		  NULL,
747 		  G_TYPE_NONE, 0);
748 
749   /**
750    * GtkIconView::select-all:
751    * @iconview: the object on which the signal is emitted
752    *
753    * A [keybinding signal][GtkBindingSignal]
754    * which gets emitted when the user selects all items.
755    *
756    * Applications should not connect to it, but may emit it with
757    * g_signal_emit_by_name() if they need to control selection
758    * programmatically.
759    *
760    * The default binding for this signal is Ctrl-a.
761    */
762   icon_view_signals[SELECT_ALL] =
763     g_signal_new (I_("select-all"),
764 		  G_TYPE_FROM_CLASS (gobject_class),
765 		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
766 		  G_STRUCT_OFFSET (GtkIconViewClass, select_all),
767 		  NULL, NULL,
768 		  NULL,
769 		  G_TYPE_NONE, 0);
770 
771   /**
772    * GtkIconView::unselect-all:
773    * @iconview: the object on which the signal is emitted
774    *
775    * A [keybinding signal][GtkBindingSignal]
776    * which gets emitted when the user unselects all items.
777    *
778    * Applications should not connect to it, but may emit it with
779    * g_signal_emit_by_name() if they need to control selection
780    * programmatically.
781    *
782    * The default binding for this signal is Ctrl-Shift-a.
783    */
784   icon_view_signals[UNSELECT_ALL] =
785     g_signal_new (I_("unselect-all"),
786 		  G_TYPE_FROM_CLASS (gobject_class),
787 		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
788 		  G_STRUCT_OFFSET (GtkIconViewClass, unselect_all),
789 		  NULL, NULL,
790 		  NULL,
791 		  G_TYPE_NONE, 0);
792 
793   /**
794    * GtkIconView::select-cursor-item:
795    * @iconview: the object on which the signal is emitted
796    *
797    * A [keybinding signal][GtkBindingSignal]
798    * which gets emitted when the user selects the item that is currently
799    * focused.
800    *
801    * Applications should not connect to it, but may emit it with
802    * g_signal_emit_by_name() if they need to control selection
803    * programmatically.
804    *
805    * There is no default binding for this signal.
806    */
807   icon_view_signals[SELECT_CURSOR_ITEM] =
808     g_signal_new (I_("select-cursor-item"),
809 		  G_TYPE_FROM_CLASS (gobject_class),
810 		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
811 		  G_STRUCT_OFFSET (GtkIconViewClass, select_cursor_item),
812 		  NULL, NULL,
813 		  NULL,
814 		  G_TYPE_NONE, 0);
815 
816   /**
817    * GtkIconView::toggle-cursor-item:
818    * @iconview: the object on which the signal is emitted
819    *
820    * A [keybinding signal][GtkBindingSignal]
821    * which gets emitted when the user toggles whether the currently
822    * focused item is selected or not. The exact effect of this
823    * depend on the selection mode.
824    *
825    * Applications should not connect to it, but may emit it with
826    * g_signal_emit_by_name() if they need to control selection
827    * programmatically.
828    *
829    * There is no default binding for this signal is Ctrl-Space.
830    */
831   icon_view_signals[TOGGLE_CURSOR_ITEM] =
832     g_signal_new (I_("toggle-cursor-item"),
833 		  G_TYPE_FROM_CLASS (gobject_class),
834 		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
835 		  G_STRUCT_OFFSET (GtkIconViewClass, toggle_cursor_item),
836 		  NULL, NULL,
837 		  NULL,
838 		  G_TYPE_NONE, 0);
839 
840   /**
841    * GtkIconView::activate-cursor-item:
842    * @iconview: the object on which the signal is emitted
843    *
844    * A [keybinding signal][GtkBindingSignal]
845    * which gets emitted when the user activates the currently
846    * focused item.
847    *
848    * Applications should not connect to it, but may emit it with
849    * g_signal_emit_by_name() if they need to control activation
850    * programmatically.
851    *
852    * The default bindings for this signal are Space, Return and Enter.
853    */
854   icon_view_signals[ACTIVATE_CURSOR_ITEM] =
855     g_signal_new (I_("activate-cursor-item"),
856 		  G_TYPE_FROM_CLASS (gobject_class),
857 		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
858 		  G_STRUCT_OFFSET (GtkIconViewClass, activate_cursor_item),
859 		  NULL, NULL,
860 		  _gtk_marshal_BOOLEAN__VOID,
861 		  G_TYPE_BOOLEAN, 0);
862   g_signal_set_va_marshaller (icon_view_signals[ACTIVATE_CURSOR_ITEM],
863                               G_TYPE_FROM_CLASS (klass),
864                               _gtk_marshal_BOOLEAN__VOIDv);
865 
866   /**
867    * GtkIconView::move-cursor:
868    * @iconview: the object which received the signal
869    * @step: the granularity of the move, as a #GtkMovementStep
870    * @count: the number of @step units to move
871    *
872    * The ::move-cursor signal is a
873    * [keybinding signal][GtkBindingSignal]
874    * which gets emitted when the user initiates a cursor movement.
875    *
876    * Applications should not connect to it, but may emit it with
877    * g_signal_emit_by_name() if they need to control the cursor
878    * programmatically.
879    *
880    * The default bindings for this signal include
881    * - Arrow keys which move by individual steps
882    * - Home/End keys which move to the first/last item
883    * - PageUp/PageDown which move by "pages"
884    * All of these will extend the selection when combined with
885    * the Shift modifier.
886    */
887   icon_view_signals[MOVE_CURSOR] =
888     g_signal_new (I_("move-cursor"),
889 		  G_TYPE_FROM_CLASS (gobject_class),
890 		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
891 		  G_STRUCT_OFFSET (GtkIconViewClass, move_cursor),
892 		  NULL, NULL,
893 		  _gtk_marshal_BOOLEAN__ENUM_INT,
894 		  G_TYPE_BOOLEAN, 2,
895 		  GTK_TYPE_MOVEMENT_STEP,
896 		  G_TYPE_INT);
897   g_signal_set_va_marshaller (icon_view_signals[MOVE_CURSOR],
898                               G_TYPE_FROM_CLASS (klass),
899                               _gtk_marshal_BOOLEAN__ENUM_INTv);
900 
901   /* Key bindings */
902   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
903 				"select-all", 0);
904   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
905 				"unselect-all", 0);
906   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
907 				"toggle-cursor-item", 0);
908   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
909 				"toggle-cursor-item", 0);
910 
911   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0,
912 				"activate-cursor-item", 0);
913   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
914 				"activate-cursor-item", 0);
915   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
916 				"activate-cursor-item", 0);
917   gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
918 				"activate-cursor-item", 0);
919   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
920 				"activate-cursor-item", 0);
921 
922   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Up, 0,
923 				  GTK_MOVEMENT_DISPLAY_LINES, -1);
924   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
925 				  GTK_MOVEMENT_DISPLAY_LINES, -1);
926 
927   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Down, 0,
928 				  GTK_MOVEMENT_DISPLAY_LINES, 1);
929   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
930 				  GTK_MOVEMENT_DISPLAY_LINES, 1);
931 
932   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_p, GDK_CONTROL_MASK,
933 				  GTK_MOVEMENT_DISPLAY_LINES, -1);
934 
935   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_n, GDK_CONTROL_MASK,
936 				  GTK_MOVEMENT_DISPLAY_LINES, 1);
937 
938   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Home, 0,
939 				  GTK_MOVEMENT_BUFFER_ENDS, -1);
940   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
941 				  GTK_MOVEMENT_BUFFER_ENDS, -1);
942 
943   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_End, 0,
944 				  GTK_MOVEMENT_BUFFER_ENDS, 1);
945   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_End, 0,
946 				  GTK_MOVEMENT_BUFFER_ENDS, 1);
947 
948   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
949 				  GTK_MOVEMENT_PAGES, -1);
950   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
951 				  GTK_MOVEMENT_PAGES, -1);
952 
953   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
954 				  GTK_MOVEMENT_PAGES, 1);
955   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
956 				  GTK_MOVEMENT_PAGES, 1);
957 
958   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Right, 0,
959 				  GTK_MOVEMENT_VISUAL_POSITIONS, 1);
960   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Left, 0,
961 				  GTK_MOVEMENT_VISUAL_POSITIONS, -1);
962 
963   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
964 				  GTK_MOVEMENT_VISUAL_POSITIONS, 1);
965   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
966 				  GTK_MOVEMENT_VISUAL_POSITIONS, -1);
967 
968   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ICON_VIEW_ACCESSIBLE);
969   gtk_widget_class_set_css_name (widget_class, "iconview");
970 }
971 
972 static void
gtk_icon_view_buildable_init(GtkBuildableIface * iface)973 gtk_icon_view_buildable_init (GtkBuildableIface *iface)
974 {
975   parent_buildable_iface = g_type_interface_peek_parent (iface);
976   iface->add_child = _gtk_cell_layout_buildable_add_child;
977   iface->custom_tag_start = gtk_icon_view_buildable_custom_tag_start;
978   iface->custom_tag_end = gtk_icon_view_buildable_custom_tag_end;
979 }
980 
981 static void
gtk_icon_view_cell_layout_init(GtkCellLayoutIface * iface)982 gtk_icon_view_cell_layout_init (GtkCellLayoutIface *iface)
983 {
984   iface->get_area = gtk_icon_view_cell_layout_get_area;
985 }
986 
987 static void
gtk_icon_view_init(GtkIconView * icon_view)988 gtk_icon_view_init (GtkIconView *icon_view)
989 {
990   icon_view->priv = gtk_icon_view_get_instance_private (icon_view);
991 
992   icon_view->priv->width = 0;
993   icon_view->priv->height = 0;
994   icon_view->priv->selection_mode = GTK_SELECTION_SINGLE;
995   icon_view->priv->pressed_button = -1;
996   icon_view->priv->press_start_x = -1;
997   icon_view->priv->press_start_y = -1;
998   icon_view->priv->text_column = -1;
999   icon_view->priv->markup_column = -1;
1000   icon_view->priv->pixbuf_column = -1;
1001   icon_view->priv->text_cell = NULL;
1002   icon_view->priv->pixbuf_cell = NULL;
1003   icon_view->priv->tooltip_column = -1;
1004 
1005   gtk_widget_set_can_focus (GTK_WIDGET (icon_view), TRUE);
1006 
1007   icon_view->priv->item_orientation = GTK_ORIENTATION_VERTICAL;
1008 
1009   icon_view->priv->columns = -1;
1010   icon_view->priv->item_width = -1;
1011   icon_view->priv->spacing = 0;
1012   icon_view->priv->row_spacing = 6;
1013   icon_view->priv->column_spacing = 6;
1014   icon_view->priv->margin = 6;
1015   icon_view->priv->item_padding = 6;
1016   icon_view->priv->activate_on_single_click = FALSE;
1017 
1018   icon_view->priv->draw_focus = TRUE;
1019 
1020   icon_view->priv->row_contexts =
1021     g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
1022 
1023   gtk_style_context_add_class (gtk_widget_get_style_context (GTK_WIDGET (icon_view)),
1024                                GTK_STYLE_CLASS_VIEW);
1025 }
1026 
1027 /* GObject methods */
1028 
1029 static void
gtk_icon_view_constructed(GObject * object)1030 gtk_icon_view_constructed (GObject *object)
1031 {
1032   GtkIconView *icon_view = GTK_ICON_VIEW (object);
1033 
1034   G_OBJECT_CLASS (gtk_icon_view_parent_class)->constructed (object);
1035 
1036   gtk_icon_view_ensure_cell_area (icon_view, NULL);
1037 }
1038 
1039 static void
gtk_icon_view_dispose(GObject * object)1040 gtk_icon_view_dispose (GObject *object)
1041 {
1042   GtkIconView *icon_view;
1043   GtkIconViewPrivate *priv;
1044 
1045   icon_view = GTK_ICON_VIEW (object);
1046   priv      = icon_view->priv;
1047 
1048   if (priv->cell_area_context)
1049     {
1050       g_object_unref (priv->cell_area_context);
1051       priv->cell_area_context = NULL;
1052     }
1053 
1054   if (priv->row_contexts)
1055     {
1056       g_ptr_array_free (priv->row_contexts, TRUE);
1057       priv->row_contexts = NULL;
1058     }
1059 
1060   if (priv->cell_area)
1061     {
1062       gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
1063 
1064       g_signal_handler_disconnect (priv->cell_area, priv->add_editable_id);
1065       g_signal_handler_disconnect (priv->cell_area, priv->remove_editable_id);
1066       priv->add_editable_id = 0;
1067       priv->remove_editable_id = 0;
1068 
1069       g_object_unref (priv->cell_area);
1070       priv->cell_area = NULL;
1071     }
1072 
1073   G_OBJECT_CLASS (gtk_icon_view_parent_class)->dispose (object);
1074 }
1075 
1076 static void
gtk_icon_view_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)1077 gtk_icon_view_set_property (GObject      *object,
1078 			    guint         prop_id,
1079 			    const GValue *value,
1080 			    GParamSpec   *pspec)
1081 {
1082   GtkIconView *icon_view;
1083   GtkCellArea *area;
1084 
1085   icon_view = GTK_ICON_VIEW (object);
1086 
1087   switch (prop_id)
1088     {
1089     case PROP_SELECTION_MODE:
1090       gtk_icon_view_set_selection_mode (icon_view, g_value_get_enum (value));
1091       break;
1092     case PROP_PIXBUF_COLUMN:
1093       gtk_icon_view_set_pixbuf_column (icon_view, g_value_get_int (value));
1094       break;
1095     case PROP_TEXT_COLUMN:
1096       gtk_icon_view_set_text_column (icon_view, g_value_get_int (value));
1097       break;
1098     case PROP_MARKUP_COLUMN:
1099       gtk_icon_view_set_markup_column (icon_view, g_value_get_int (value));
1100       break;
1101     case PROP_MODEL:
1102       gtk_icon_view_set_model (icon_view, g_value_get_object (value));
1103       break;
1104     case PROP_ITEM_ORIENTATION:
1105       gtk_icon_view_set_item_orientation (icon_view, g_value_get_enum (value));
1106       break;
1107     case PROP_COLUMNS:
1108       gtk_icon_view_set_columns (icon_view, g_value_get_int (value));
1109       break;
1110     case PROP_ITEM_WIDTH:
1111       gtk_icon_view_set_item_width (icon_view, g_value_get_int (value));
1112       break;
1113     case PROP_SPACING:
1114       gtk_icon_view_set_spacing (icon_view, g_value_get_int (value));
1115       break;
1116     case PROP_ROW_SPACING:
1117       gtk_icon_view_set_row_spacing (icon_view, g_value_get_int (value));
1118       break;
1119     case PROP_COLUMN_SPACING:
1120       gtk_icon_view_set_column_spacing (icon_view, g_value_get_int (value));
1121       break;
1122     case PROP_MARGIN:
1123       gtk_icon_view_set_margin (icon_view, g_value_get_int (value));
1124       break;
1125     case PROP_REORDERABLE:
1126       gtk_icon_view_set_reorderable (icon_view, g_value_get_boolean (value));
1127       break;
1128 
1129     case PROP_TOOLTIP_COLUMN:
1130       gtk_icon_view_set_tooltip_column (icon_view, g_value_get_int (value));
1131       break;
1132 
1133     case PROP_ITEM_PADDING:
1134       gtk_icon_view_set_item_padding (icon_view, g_value_get_int (value));
1135       break;
1136 
1137     case PROP_ACTIVATE_ON_SINGLE_CLICK:
1138       gtk_icon_view_set_activate_on_single_click (icon_view, g_value_get_boolean (value));
1139       break;
1140 
1141     case PROP_CELL_AREA:
1142       /* Construct-only, can only be assigned once */
1143       area = g_value_get_object (value);
1144       if (area)
1145         {
1146           if (icon_view->priv->cell_area != NULL)
1147             {
1148               g_warning ("cell-area has already been set, ignoring construct property");
1149               g_object_ref_sink (area);
1150               g_object_unref (area);
1151             }
1152           else
1153             gtk_icon_view_ensure_cell_area (icon_view, area);
1154         }
1155       break;
1156 
1157     case PROP_HADJUSTMENT:
1158       gtk_icon_view_set_hadjustment (icon_view, g_value_get_object (value));
1159       break;
1160     case PROP_VADJUSTMENT:
1161       gtk_icon_view_set_vadjustment (icon_view, g_value_get_object (value));
1162       break;
1163     case PROP_HSCROLL_POLICY:
1164       if (icon_view->priv->hscroll_policy != g_value_get_enum (value))
1165         {
1166           icon_view->priv->hscroll_policy = g_value_get_enum (value);
1167           gtk_widget_queue_resize (GTK_WIDGET (icon_view));
1168           g_object_notify_by_pspec (object, pspec);
1169         }
1170       break;
1171     case PROP_VSCROLL_POLICY:
1172       if (icon_view->priv->vscroll_policy != g_value_get_enum (value))
1173         {
1174           icon_view->priv->vscroll_policy = g_value_get_enum (value);
1175           gtk_widget_queue_resize (GTK_WIDGET (icon_view));
1176           g_object_notify_by_pspec (object, pspec);
1177         }
1178       break;
1179 
1180     default:
1181       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1182       break;
1183     }
1184 }
1185 
1186 static void
gtk_icon_view_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)1187 gtk_icon_view_get_property (GObject      *object,
1188 			    guint         prop_id,
1189 			    GValue       *value,
1190 			    GParamSpec   *pspec)
1191 {
1192   GtkIconView *icon_view;
1193 
1194   icon_view = GTK_ICON_VIEW (object);
1195 
1196   switch (prop_id)
1197     {
1198     case PROP_SELECTION_MODE:
1199       g_value_set_enum (value, icon_view->priv->selection_mode);
1200       break;
1201     case PROP_PIXBUF_COLUMN:
1202       g_value_set_int (value, icon_view->priv->pixbuf_column);
1203       break;
1204     case PROP_TEXT_COLUMN:
1205       g_value_set_int (value, icon_view->priv->text_column);
1206       break;
1207     case PROP_MARKUP_COLUMN:
1208       g_value_set_int (value, icon_view->priv->markup_column);
1209       break;
1210     case PROP_MODEL:
1211       g_value_set_object (value, icon_view->priv->model);
1212       break;
1213     case PROP_ITEM_ORIENTATION:
1214       g_value_set_enum (value, icon_view->priv->item_orientation);
1215       break;
1216     case PROP_COLUMNS:
1217       g_value_set_int (value, icon_view->priv->columns);
1218       break;
1219     case PROP_ITEM_WIDTH:
1220       g_value_set_int (value, icon_view->priv->item_width);
1221       break;
1222     case PROP_SPACING:
1223       g_value_set_int (value, icon_view->priv->spacing);
1224       break;
1225     case PROP_ROW_SPACING:
1226       g_value_set_int (value, icon_view->priv->row_spacing);
1227       break;
1228     case PROP_COLUMN_SPACING:
1229       g_value_set_int (value, icon_view->priv->column_spacing);
1230       break;
1231     case PROP_MARGIN:
1232       g_value_set_int (value, icon_view->priv->margin);
1233       break;
1234     case PROP_REORDERABLE:
1235       g_value_set_boolean (value, icon_view->priv->reorderable);
1236       break;
1237     case PROP_TOOLTIP_COLUMN:
1238       g_value_set_int (value, icon_view->priv->tooltip_column);
1239       break;
1240 
1241     case PROP_ITEM_PADDING:
1242       g_value_set_int (value, icon_view->priv->item_padding);
1243       break;
1244 
1245     case PROP_ACTIVATE_ON_SINGLE_CLICK:
1246       g_value_set_boolean (value, icon_view->priv->activate_on_single_click);
1247       break;
1248 
1249     case PROP_CELL_AREA:
1250       g_value_set_object (value, icon_view->priv->cell_area);
1251       break;
1252 
1253     case PROP_HADJUSTMENT:
1254       g_value_set_object (value, icon_view->priv->hadjustment);
1255       break;
1256     case PROP_VADJUSTMENT:
1257       g_value_set_object (value, icon_view->priv->vadjustment);
1258       break;
1259     case PROP_HSCROLL_POLICY:
1260       g_value_set_enum (value, icon_view->priv->hscroll_policy);
1261       break;
1262     case PROP_VSCROLL_POLICY:
1263       g_value_set_enum (value, icon_view->priv->vscroll_policy);
1264       break;
1265 
1266     default:
1267       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1268       break;
1269     }
1270 }
1271 
1272 /* GtkWidget methods */
1273 static void
gtk_icon_view_destroy(GtkWidget * widget)1274 gtk_icon_view_destroy (GtkWidget *widget)
1275 {
1276   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1277 
1278   gtk_icon_view_set_model (icon_view, NULL);
1279 
1280   if (icon_view->priv->scroll_to_path != NULL)
1281     {
1282       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1283       icon_view->priv->scroll_to_path = NULL;
1284     }
1285 
1286   remove_scroll_timeout (icon_view);
1287 
1288   if (icon_view->priv->hadjustment != NULL)
1289     {
1290       g_object_unref (icon_view->priv->hadjustment);
1291       icon_view->priv->hadjustment = NULL;
1292     }
1293 
1294   if (icon_view->priv->vadjustment != NULL)
1295     {
1296       g_object_unref (icon_view->priv->vadjustment);
1297       icon_view->priv->vadjustment = NULL;
1298     }
1299 
1300   GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->destroy (widget);
1301 }
1302 
1303 static void
gtk_icon_view_realize(GtkWidget * widget)1304 gtk_icon_view_realize (GtkWidget *widget)
1305 {
1306   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1307   GtkAllocation allocation;
1308   GdkWindow *window;
1309   GdkWindowAttr attributes;
1310   gint attributes_mask;
1311 
1312   gtk_widget_set_realized (widget, TRUE);
1313 
1314   gtk_widget_get_allocation (widget, &allocation);
1315 
1316   /* Make the main, clipping window */
1317   attributes.window_type = GDK_WINDOW_CHILD;
1318   attributes.x = allocation.x;
1319   attributes.y = allocation.y;
1320   attributes.width = allocation.width;
1321   attributes.height = allocation.height;
1322   attributes.wclass = GDK_INPUT_OUTPUT;
1323   attributes.visual = gtk_widget_get_visual (widget);
1324   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1325 
1326   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1327 
1328   window = gdk_window_new (gtk_widget_get_parent_window (widget),
1329                            &attributes, attributes_mask);
1330   gtk_widget_set_window (widget, window);
1331   gtk_widget_register_window (widget, window);
1332 
1333   gtk_widget_get_allocation (widget, &allocation);
1334 
1335   /* Make the window for the icon view */
1336   attributes.x = 0;
1337   attributes.y = 0;
1338   attributes.width = MAX (icon_view->priv->width, allocation.width);
1339   attributes.height = MAX (icon_view->priv->height, allocation.height);
1340   attributes.event_mask = (GDK_SCROLL_MASK |
1341                            GDK_SMOOTH_SCROLL_MASK |
1342                            GDK_POINTER_MOTION_MASK |
1343                            GDK_LEAVE_NOTIFY_MASK |
1344                            GDK_BUTTON_PRESS_MASK |
1345                            GDK_BUTTON_RELEASE_MASK |
1346                            GDK_KEY_PRESS_MASK |
1347                            GDK_KEY_RELEASE_MASK) |
1348     gtk_widget_get_events (widget);
1349 
1350   icon_view->priv->bin_window = gdk_window_new (window,
1351 						&attributes, attributes_mask);
1352   gtk_widget_register_window (widget, icon_view->priv->bin_window);
1353   gdk_window_show (icon_view->priv->bin_window);
1354 }
1355 
1356 static void
gtk_icon_view_unrealize(GtkWidget * widget)1357 gtk_icon_view_unrealize (GtkWidget *widget)
1358 {
1359   GtkIconView *icon_view;
1360 
1361   icon_view = GTK_ICON_VIEW (widget);
1362 
1363   gtk_widget_unregister_window (widget, icon_view->priv->bin_window);
1364   gdk_window_destroy (icon_view->priv->bin_window);
1365   icon_view->priv->bin_window = NULL;
1366 
1367   GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->unrealize (widget);
1368 }
1369 
1370 static gint
gtk_icon_view_get_n_items(GtkIconView * icon_view)1371 gtk_icon_view_get_n_items (GtkIconView *icon_view)
1372 {
1373   GtkIconViewPrivate *priv = icon_view->priv;
1374 
1375   if (priv->model == NULL)
1376     return 0;
1377 
1378   return gtk_tree_model_iter_n_children (priv->model, NULL);
1379 }
1380 
1381 static void
adjust_wrap_width(GtkIconView * icon_view)1382 adjust_wrap_width (GtkIconView *icon_view)
1383 {
1384   if (icon_view->priv->text_cell)
1385     {
1386       gint pixbuf_width, wrap_width;
1387 
1388       if (icon_view->priv->items && icon_view->priv->pixbuf_cell)
1389         {
1390           gtk_cell_renderer_get_preferred_width (icon_view->priv->pixbuf_cell,
1391                                                  GTK_WIDGET (icon_view),
1392                                                  &pixbuf_width, NULL);
1393         }
1394       else
1395         {
1396           pixbuf_width = 0;
1397         }
1398 
1399       if (icon_view->priv->item_width >= 0)
1400         {
1401           if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
1402             {
1403               wrap_width = icon_view->priv->item_width;
1404             }
1405           else
1406             {
1407               wrap_width = icon_view->priv->item_width - pixbuf_width;
1408             }
1409 
1410           wrap_width -= 2 * icon_view->priv->item_padding * 2;
1411         }
1412       else
1413         {
1414           wrap_width = MAX (pixbuf_width * 2, 50);
1415         }
1416 
1417       if (icon_view->priv->items && icon_view->priv->pixbuf_cell)
1418 	{
1419           /* Here we go with the same old guess, try the icon size and set double
1420            * the size of the first icon found in the list, naive but works much
1421            * of the time */
1422 
1423 	  wrap_width = MAX (wrap_width * 2, 50);
1424 	}
1425 
1426       g_object_set (icon_view->priv->text_cell, "wrap-width", wrap_width, NULL);
1427       g_object_set (icon_view->priv->text_cell, "width", wrap_width, NULL);
1428     }
1429 }
1430 
1431 /* General notes about layout
1432  *
1433  * The icon view is layouted like this:
1434  *
1435  * +----------+  s  +----------+
1436  * | padding  |  p  | padding  |
1437  * | +------+ |  a  | +------+ |
1438  * | | cell | |  c  | | cell | |
1439  * | +------+ |  i  | +------+ |
1440  * |          |  n  |          |
1441  * +----------+  g  +----------+
1442  *
1443  * In size request and allocation code, there are 3 sizes that are used:
1444  * * cell size
1445  *   This is the size returned by gtk_cell_area_get_preferred_foo(). In places
1446  *   where code is interacting with the cell area and renderers this is useful.
1447  * * padded size
1448  *   This is the cell size plus the item padding on each side.
1449  * * spaced size
1450  *   This is the padded size plus the spacing. This is what’s used for most
1451  *   calculations because it can (ab)use the following formula:
1452  *   iconview_size = 2 * margin + n_items * spaced_size - spacing
1453  * So when reading this code and fixing my bugs where I confuse these two, be
1454  * aware of this distinction.
1455  */
1456 static void
cell_area_get_preferred_size(GtkIconView * icon_view,GtkCellAreaContext * context,GtkOrientation orientation,gint for_size,gint * minimum,gint * natural)1457 cell_area_get_preferred_size (GtkIconView        *icon_view,
1458                               GtkCellAreaContext *context,
1459                               GtkOrientation      orientation,
1460                               gint                for_size,
1461                               gint               *minimum,
1462                               gint               *natural)
1463 {
1464   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1465     {
1466       if (for_size > 0)
1467         gtk_cell_area_get_preferred_width_for_height (icon_view->priv->cell_area,
1468                                                       context,
1469                                                       GTK_WIDGET (icon_view),
1470                                                       for_size,
1471                                                       minimum, natural);
1472       else
1473         gtk_cell_area_get_preferred_width (icon_view->priv->cell_area,
1474                                            context,
1475                                            GTK_WIDGET (icon_view),
1476                                            minimum, natural);
1477     }
1478   else
1479     {
1480       if (for_size > 0)
1481         gtk_cell_area_get_preferred_height_for_width (icon_view->priv->cell_area,
1482                                                       context,
1483                                                       GTK_WIDGET (icon_view),
1484                                                       for_size,
1485                                                       minimum, natural);
1486       else
1487         gtk_cell_area_get_preferred_height (icon_view->priv->cell_area,
1488                                             context,
1489                                             GTK_WIDGET (icon_view),
1490                                             minimum, natural);
1491     }
1492 }
1493 
1494 static gboolean
gtk_icon_view_is_empty(GtkIconView * icon_view)1495 gtk_icon_view_is_empty (GtkIconView *icon_view)
1496 {
1497   return icon_view->priv->items == NULL;
1498 }
1499 
1500 static void
gtk_icon_view_get_preferred_item_size(GtkIconView * icon_view,GtkOrientation orientation,gint for_size,gint * minimum,gint * natural)1501 gtk_icon_view_get_preferred_item_size (GtkIconView    *icon_view,
1502                                        GtkOrientation  orientation,
1503                                        gint            for_size,
1504                                        gint           *minimum,
1505                                        gint           *natural)
1506 {
1507   GtkIconViewPrivate *priv = icon_view->priv;
1508   GtkCellAreaContext *context;
1509   GList *items;
1510 
1511   g_assert (!gtk_icon_view_is_empty (icon_view));
1512 
1513   context = gtk_cell_area_create_context (priv->cell_area);
1514 
1515   for_size -= 2 * priv->item_padding;
1516 
1517   if (for_size > 0)
1518     {
1519       /* This is necessary for the context to work properly */
1520       for (items = priv->items; items; items = items->next)
1521         {
1522           GtkIconViewItem *item = items->data;
1523 
1524           _gtk_icon_view_set_cell_data (icon_view, item);
1525           cell_area_get_preferred_size (icon_view, context, 1 - orientation, -1, NULL, NULL);
1526         }
1527     }
1528 
1529   for (items = priv->items; items; items = items->next)
1530     {
1531       GtkIconViewItem *item = items->data;
1532 
1533       _gtk_icon_view_set_cell_data (icon_view, item);
1534       if (items == priv->items)
1535         adjust_wrap_width (icon_view);
1536       cell_area_get_preferred_size (icon_view, context, orientation, for_size, NULL, NULL);
1537     }
1538 
1539   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1540     {
1541       if (for_size > 0)
1542         gtk_cell_area_context_get_preferred_width_for_height (context,
1543                                                               for_size,
1544                                                               minimum, natural);
1545       else
1546         gtk_cell_area_context_get_preferred_width (context,
1547                                                    minimum, natural);
1548     }
1549   else
1550     {
1551       if (for_size > 0)
1552         gtk_cell_area_context_get_preferred_height_for_width (context,
1553                                                               for_size,
1554                                                               minimum, natural);
1555       else
1556         gtk_cell_area_context_get_preferred_height (context,
1557                                                     minimum, natural);
1558     }
1559 
1560   if (orientation == GTK_ORIENTATION_HORIZONTAL && priv->item_width >= 0)
1561     {
1562       if (minimum)
1563         *minimum = MAX (*minimum, priv->item_width);
1564       if (natural)
1565         *natural = *minimum;
1566     }
1567 
1568   if (minimum)
1569     *minimum = MAX (1, *minimum + 2 * priv->item_padding);
1570   if (natural)
1571     *natural = MAX (1, *natural + 2 * priv->item_padding);
1572 
1573   g_object_unref (context);
1574 }
1575 
1576 static void
gtk_icon_view_compute_n_items_for_size(GtkIconView * icon_view,GtkOrientation orientation,gint size,gint * min_items,gint * min_item_size,gint * max_items,gint * max_item_size)1577 gtk_icon_view_compute_n_items_for_size (GtkIconView    *icon_view,
1578                                         GtkOrientation  orientation,
1579                                         gint            size,
1580                                         gint           *min_items,
1581                                         gint           *min_item_size,
1582                                         gint           *max_items,
1583                                         gint           *max_item_size)
1584 {
1585   GtkIconViewPrivate *priv = icon_view->priv;
1586   int minimum, natural, spacing;
1587 
1588   g_return_if_fail (min_item_size == NULL || min_items != NULL);
1589   g_return_if_fail (max_item_size == NULL || max_items != NULL);
1590   g_return_if_fail (!gtk_icon_view_is_empty (icon_view));
1591 
1592   gtk_icon_view_get_preferred_item_size (icon_view, orientation, -1, &minimum, &natural);
1593 
1594   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1595     spacing = priv->column_spacing;
1596   else
1597     spacing = priv->row_spacing;
1598 
1599   size -= 2 * priv->margin;
1600   size += spacing;
1601   minimum += spacing;
1602   natural += spacing;
1603 
1604   if (priv->columns > 0)
1605     {
1606       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1607         {
1608           if (min_items)
1609             *min_items = priv->columns;
1610           if (max_items)
1611             *max_items = priv->columns;
1612         }
1613       else
1614         {
1615           int n_items = gtk_icon_view_get_n_items (icon_view);
1616 
1617           if (min_items)
1618             *min_items = (n_items + priv->columns - 1) / priv->columns;
1619           if (max_items)
1620             *max_items = (n_items + priv->columns - 1) / priv->columns;
1621         }
1622     }
1623   else
1624     {
1625       if (max_items)
1626         {
1627           if (size <= minimum)
1628             *max_items = 1;
1629           else
1630             *max_items = size / minimum;
1631         }
1632 
1633       if (min_items)
1634         {
1635           if (size <= natural)
1636             *min_items = 1;
1637           else
1638             *min_items = size / natural;
1639         }
1640     }
1641 
1642   if (min_item_size)
1643     {
1644       *min_item_size = size / *min_items;
1645       *min_item_size = CLAMP (*min_item_size, minimum, natural);
1646       *min_item_size -= spacing;
1647       *min_item_size -= 2 * priv->item_padding;
1648     }
1649 
1650   if (max_item_size)
1651     {
1652       *max_item_size = size / *max_items;
1653       *max_item_size = CLAMP (*max_item_size, minimum, natural);
1654       *max_item_size -= spacing;
1655       *max_item_size -= 2 * priv->item_padding;
1656     }
1657 }
1658 
1659 static GtkSizeRequestMode
gtk_icon_view_get_request_mode(GtkWidget * widget)1660 gtk_icon_view_get_request_mode (GtkWidget *widget)
1661 {
1662   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1663 }
1664 
1665 static void
gtk_icon_view_get_preferred_width(GtkWidget * widget,gint * minimum,gint * natural)1666 gtk_icon_view_get_preferred_width (GtkWidget *widget,
1667 				   gint      *minimum,
1668 				   gint      *natural)
1669 {
1670   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1671   GtkIconViewPrivate *priv = icon_view->priv;
1672   int item_min, item_nat;
1673 
1674   if (gtk_icon_view_is_empty (icon_view))
1675     {
1676       *minimum = *natural = 2 * priv->margin;
1677       return;
1678     }
1679 
1680   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, -1, &item_min, &item_nat);
1681 
1682   if (priv->columns > 0)
1683     {
1684       *minimum = item_min * priv->columns + priv->column_spacing * (priv->columns - 1);
1685       *natural = item_nat * priv->columns + priv->column_spacing * (priv->columns - 1);
1686     }
1687   else
1688     {
1689       int n_items = gtk_icon_view_get_n_items (icon_view);
1690 
1691       *minimum = item_min;
1692       *natural = item_nat * n_items + priv->column_spacing * (n_items - 1);
1693     }
1694 
1695   *minimum += 2 * priv->margin;
1696   *natural += 2 * priv->margin;
1697 }
1698 
1699 static void
gtk_icon_view_get_preferred_width_for_height(GtkWidget * widget,gint height,gint * minimum,gint * natural)1700 gtk_icon_view_get_preferred_width_for_height (GtkWidget *widget,
1701                                               gint       height,
1702                                               gint      *minimum,
1703                                               gint      *natural)
1704 {
1705   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1706   GtkIconViewPrivate *priv = icon_view->priv;
1707   int item_min, item_nat, rows, row_height, n_items;
1708 
1709   if (gtk_icon_view_is_empty (icon_view))
1710     {
1711       *minimum = *natural = 2 * priv->margin;
1712       return;
1713     }
1714 
1715   gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_VERTICAL, height, &rows, &row_height, NULL, NULL);
1716   n_items = gtk_icon_view_get_n_items (icon_view);
1717 
1718   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, row_height, &item_min, &item_nat);
1719   *minimum = (item_min + priv->column_spacing) * ((n_items + rows - 1) / rows) - priv->column_spacing;
1720   *natural = (item_nat + priv->column_spacing) * ((n_items + rows - 1) / rows) - priv->column_spacing;
1721 
1722   *minimum += 2 * priv->margin;
1723   *natural += 2 * priv->margin;
1724 }
1725 
1726 static void
gtk_icon_view_get_preferred_height(GtkWidget * widget,gint * minimum,gint * natural)1727 gtk_icon_view_get_preferred_height (GtkWidget *widget,
1728 				    gint      *minimum,
1729 				    gint      *natural)
1730 {
1731   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1732   GtkIconViewPrivate *priv = icon_view->priv;
1733   int item_min, item_nat, n_items;
1734 
1735   if (gtk_icon_view_is_empty (icon_view))
1736     {
1737       *minimum = *natural = 2 * priv->margin;
1738       return;
1739     }
1740 
1741   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, -1, &item_min, &item_nat);
1742   n_items = gtk_icon_view_get_n_items (icon_view);
1743 
1744   if (priv->columns > 0)
1745     {
1746       int n_rows = (n_items + priv->columns - 1) / priv->columns;
1747 
1748       *minimum = item_min * n_rows + priv->row_spacing * (n_rows - 1);
1749       *natural = item_nat * n_rows + priv->row_spacing * (n_rows - 1);
1750     }
1751   else
1752     {
1753       *minimum = item_min;
1754       *natural = item_nat * n_items + priv->row_spacing * (n_items - 1);
1755     }
1756 
1757   *minimum += 2 * priv->margin;
1758   *natural += 2 * priv->margin;
1759 }
1760 
1761 static void
gtk_icon_view_get_preferred_height_for_width(GtkWidget * widget,gint width,gint * minimum,gint * natural)1762 gtk_icon_view_get_preferred_height_for_width (GtkWidget *widget,
1763                                               gint       width,
1764                                               gint      *minimum,
1765                                               gint      *natural)
1766 {
1767   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1768   GtkIconViewPrivate *priv = icon_view->priv;
1769   int item_min, item_nat, columns, column_width, n_items;
1770 
1771   if (gtk_icon_view_is_empty (icon_view))
1772     {
1773       *minimum = *natural = 2 * priv->margin;
1774       return;
1775     }
1776 
1777   gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_HORIZONTAL, width, NULL, NULL, &columns, &column_width);
1778   n_items = gtk_icon_view_get_n_items (icon_view);
1779 
1780   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, column_width, &item_min, &item_nat);
1781   *minimum = (item_min + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1782   *natural = (item_nat + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1783 
1784   *minimum += 2 * priv->margin;
1785   *natural += 2 * priv->margin;
1786 }
1787 
1788 static void
gtk_icon_view_allocate_children(GtkIconView * icon_view)1789 gtk_icon_view_allocate_children (GtkIconView *icon_view)
1790 {
1791   GList *list;
1792 
1793   for (list = icon_view->priv->children; list; list = list->next)
1794     {
1795       GtkIconViewChild *child = list->data;
1796 
1797       /* totally ignore our child's requisition */
1798       gtk_widget_size_allocate (child->widget, &child->area);
1799     }
1800 }
1801 
1802 static void
gtk_icon_view_size_allocate(GtkWidget * widget,GtkAllocation * allocation)1803 gtk_icon_view_size_allocate (GtkWidget      *widget,
1804 			     GtkAllocation  *allocation)
1805 {
1806   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1807 
1808   gtk_widget_set_allocation (widget, allocation);
1809 
1810   gtk_icon_view_layout (icon_view);
1811 
1812   if (gtk_widget_get_realized (widget))
1813     {
1814       gdk_window_move_resize (gtk_widget_get_window (widget),
1815 			      allocation->x, allocation->y,
1816 			      allocation->width, allocation->height);
1817       gdk_window_resize (icon_view->priv->bin_window,
1818 			 MAX (icon_view->priv->width, allocation->width),
1819 			 MAX (icon_view->priv->height, allocation->height));
1820     }
1821 
1822   gtk_icon_view_allocate_children (icon_view);
1823 
1824   /* Delay signal emission */
1825   g_object_freeze_notify (G_OBJECT (icon_view->priv->hadjustment));
1826   g_object_freeze_notify (G_OBJECT (icon_view->priv->vadjustment));
1827 
1828   gtk_icon_view_set_hadjustment_values (icon_view);
1829   gtk_icon_view_set_vadjustment_values (icon_view);
1830 
1831   if (gtk_widget_get_realized (widget) &&
1832       icon_view->priv->scroll_to_path)
1833     {
1834       GtkTreePath *path;
1835       path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
1836       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1837       icon_view->priv->scroll_to_path = NULL;
1838 
1839       gtk_icon_view_scroll_to_path (icon_view, path,
1840 				    icon_view->priv->scroll_to_use_align,
1841 				    icon_view->priv->scroll_to_row_align,
1842 				    icon_view->priv->scroll_to_col_align);
1843       gtk_tree_path_free (path);
1844     }
1845 
1846   /* Emit any pending signals now */
1847   g_object_thaw_notify (G_OBJECT (icon_view->priv->hadjustment));
1848   g_object_thaw_notify (G_OBJECT (icon_view->priv->vadjustment));
1849 }
1850 
1851 static gboolean
gtk_icon_view_draw(GtkWidget * widget,cairo_t * cr)1852 gtk_icon_view_draw (GtkWidget *widget,
1853                     cairo_t   *cr)
1854 {
1855   GtkIconView *icon_view;
1856   GList *icons;
1857   GtkTreePath *path;
1858   gint dest_index;
1859   GtkIconViewDropPosition dest_pos;
1860   GtkIconViewItem *dest_item = NULL;
1861   GtkStyleContext *context;
1862 
1863   icon_view = GTK_ICON_VIEW (widget);
1864 
1865   context = gtk_widget_get_style_context (widget);
1866   gtk_render_background (context, cr,
1867                          0, 0,
1868                          gtk_widget_get_allocated_width (widget),
1869                          gtk_widget_get_allocated_height (widget));
1870 
1871   if (!gtk_cairo_should_draw_window (cr, icon_view->priv->bin_window))
1872     return FALSE;
1873 
1874   cairo_save (cr);
1875 
1876   gtk_cairo_transform_to_window (cr, widget, icon_view->priv->bin_window);
1877 
1878   cairo_set_line_width (cr, 1.);
1879 
1880   gtk_icon_view_get_drag_dest_item (icon_view, &path, &dest_pos);
1881 
1882   if (path)
1883     {
1884       dest_index = gtk_tree_path_get_indices (path)[0];
1885       gtk_tree_path_free (path);
1886     }
1887   else
1888     dest_index = -1;
1889 
1890   for (icons = icon_view->priv->items; icons; icons = icons->next)
1891     {
1892       GtkIconViewItem *item = icons->data;
1893       GdkRectangle paint_area;
1894 
1895       paint_area.x      = item->cell_area.x      - icon_view->priv->item_padding;
1896       paint_area.y      = item->cell_area.y      - icon_view->priv->item_padding;
1897       paint_area.width  = item->cell_area.width  + icon_view->priv->item_padding * 2;
1898       paint_area.height = item->cell_area.height + icon_view->priv->item_padding * 2;
1899 
1900       cairo_save (cr);
1901 
1902       cairo_rectangle (cr, paint_area.x, paint_area.y, paint_area.width, paint_area.height);
1903       cairo_clip (cr);
1904 
1905       if (gdk_cairo_get_clip_rectangle (cr, NULL))
1906         {
1907           gtk_icon_view_paint_item (icon_view, cr, item,
1908                                     item->cell_area.x, item->cell_area.y,
1909                                     icon_view->priv->draw_focus);
1910 
1911           if (dest_index == item->index)
1912             dest_item = item;
1913         }
1914 
1915       cairo_restore (cr);
1916     }
1917 
1918   if (dest_item &&
1919       dest_pos != GTK_ICON_VIEW_NO_DROP)
1920     {
1921       GdkRectangle rect = { 0 };
1922 
1923       switch (dest_pos)
1924 	{
1925 	case GTK_ICON_VIEW_DROP_INTO:
1926           rect = dest_item->cell_area;
1927 	  break;
1928 	case GTK_ICON_VIEW_DROP_ABOVE:
1929           rect.x = dest_item->cell_area.x;
1930           rect.y = dest_item->cell_area.y - 1;
1931           rect.width = dest_item->cell_area.width;
1932           rect.height = 2;
1933 	  break;
1934 	case GTK_ICON_VIEW_DROP_LEFT:
1935           rect.x = dest_item->cell_area.x - 1;
1936           rect.y = dest_item->cell_area.y;
1937           rect.width = 2;
1938           rect.height = dest_item->cell_area.height;
1939 	  break;
1940 	case GTK_ICON_VIEW_DROP_BELOW:
1941           rect.x = dest_item->cell_area.x;
1942           rect.y = dest_item->cell_area.y + dest_item->cell_area.height - 1;
1943           rect.width = dest_item->cell_area.width;
1944           rect.height = 2;
1945 	  break;
1946 	case GTK_ICON_VIEW_DROP_RIGHT:
1947           rect.x = dest_item->cell_area.x + dest_item->cell_area.width - 1;
1948           rect.y = dest_item->cell_area.y;
1949           rect.width = 2;
1950           rect.height = dest_item->cell_area.height;
1951 	case GTK_ICON_VIEW_NO_DROP: ;
1952 	  break;
1953         }
1954 
1955       gtk_render_focus (context, cr,
1956                         rect.x, rect.y,
1957                         rect.width, rect.height);
1958     }
1959 
1960   if (icon_view->priv->doing_rubberband)
1961     gtk_icon_view_paint_rubberband (icon_view, cr);
1962 
1963   cairo_restore (cr);
1964 
1965   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->draw (widget, cr);
1966 }
1967 
1968 static gboolean
rubberband_scroll_timeout(gpointer data)1969 rubberband_scroll_timeout (gpointer data)
1970 {
1971   GtkIconView *icon_view = data;
1972 
1973   gtk_adjustment_set_value (icon_view->priv->vadjustment,
1974                             gtk_adjustment_get_value (icon_view->priv->vadjustment) +
1975                             icon_view->priv->scroll_value_diff);
1976 
1977   gtk_icon_view_update_rubberband (icon_view);
1978 
1979   return TRUE;
1980 }
1981 
1982 static gboolean
gtk_icon_view_motion(GtkWidget * widget,GdkEventMotion * event)1983 gtk_icon_view_motion (GtkWidget      *widget,
1984 		      GdkEventMotion *event)
1985 {
1986   GtkAllocation allocation;
1987   GtkIconView *icon_view;
1988   gint abs_y;
1989 
1990   icon_view = GTK_ICON_VIEW (widget);
1991 
1992   gtk_icon_view_maybe_begin_drag (icon_view, event);
1993 
1994   if (icon_view->priv->doing_rubberband)
1995     {
1996       gtk_icon_view_update_rubberband (widget);
1997 
1998       abs_y = event->y - icon_view->priv->height *
1999 	(gtk_adjustment_get_value (icon_view->priv->vadjustment) /
2000 	 (gtk_adjustment_get_upper (icon_view->priv->vadjustment) -
2001 	  gtk_adjustment_get_lower (icon_view->priv->vadjustment)));
2002 
2003       gtk_widget_get_allocation (widget, &allocation);
2004 
2005       if (abs_y < 0 || abs_y > allocation.height)
2006 	{
2007 	  if (abs_y < 0)
2008 	    icon_view->priv->scroll_value_diff = abs_y;
2009 	  else
2010 	    icon_view->priv->scroll_value_diff = abs_y - allocation.height;
2011 
2012 	  icon_view->priv->event_last_x = event->x;
2013 	  icon_view->priv->event_last_y = event->y;
2014 
2015 	  if (icon_view->priv->scroll_timeout_id == 0) {
2016 	    icon_view->priv->scroll_timeout_id = gdk_threads_add_timeout (30, rubberband_scroll_timeout,
2017 								icon_view);
2018 	    g_source_set_name_by_id (icon_view->priv->scroll_timeout_id, "[gtk+] rubberband_scroll_timeout");
2019 	  }
2020  	}
2021       else
2022 	remove_scroll_timeout (icon_view);
2023     }
2024   else
2025     {
2026       GtkIconViewItem *item, *last_prelight_item;
2027       GtkCellRenderer *cell = NULL;
2028 
2029       last_prelight_item = icon_view->priv->last_prelight;
2030       item = _gtk_icon_view_get_item_at_coords (icon_view,
2031                                                event->x, event->y,
2032                                                FALSE,
2033                                                &cell);
2034 
2035       if (item != last_prelight_item)
2036         {
2037           if (item != NULL)
2038             {
2039               gtk_icon_view_queue_draw_item (icon_view, item);
2040             }
2041 
2042           if (last_prelight_item != NULL)
2043             {
2044               gtk_icon_view_queue_draw_item (icon_view,
2045                                              icon_view->priv->last_prelight);
2046             }
2047 
2048           icon_view->priv->last_prelight = item;
2049         }
2050     }
2051 
2052   return TRUE;
2053 }
2054 
2055 static gboolean
gtk_icon_view_leave(GtkWidget * widget,GdkEventCrossing * event)2056 gtk_icon_view_leave (GtkWidget        *widget,
2057                      GdkEventCrossing *event)
2058 {
2059   GtkIconView *icon_view;
2060   GtkIconViewPrivate *priv;
2061 
2062   icon_view = GTK_ICON_VIEW (widget);
2063   priv = icon_view->priv;
2064 
2065   if (priv->last_prelight)
2066     {
2067       gtk_icon_view_queue_draw_item (icon_view, priv->last_prelight);
2068       priv->last_prelight = NULL;
2069     }
2070 
2071   return FALSE;
2072 }
2073 
2074 static void
gtk_icon_view_remove(GtkContainer * container,GtkWidget * widget)2075 gtk_icon_view_remove (GtkContainer *container,
2076 		      GtkWidget    *widget)
2077 {
2078   GtkIconView *icon_view;
2079   GtkIconViewChild *child = NULL;
2080   GList *tmp_list;
2081 
2082   icon_view = GTK_ICON_VIEW (container);
2083 
2084   tmp_list = icon_view->priv->children;
2085   while (tmp_list)
2086     {
2087       child = tmp_list->data;
2088       if (child->widget == widget)
2089 	{
2090 	  gtk_widget_unparent (widget);
2091 
2092 	  icon_view->priv->children = g_list_remove_link (icon_view->priv->children, tmp_list);
2093 	  g_list_free_1 (tmp_list);
2094 	  g_free (child);
2095 	  return;
2096 	}
2097 
2098       tmp_list = tmp_list->next;
2099     }
2100 }
2101 
2102 static void
gtk_icon_view_forall(GtkContainer * container,gboolean include_internals,GtkCallback callback,gpointer callback_data)2103 gtk_icon_view_forall (GtkContainer *container,
2104 		      gboolean      include_internals,
2105 		      GtkCallback   callback,
2106 		      gpointer      callback_data)
2107 {
2108   GtkIconView *icon_view;
2109   GtkIconViewChild *child = NULL;
2110   GList *tmp_list;
2111 
2112   icon_view = GTK_ICON_VIEW (container);
2113 
2114   tmp_list = icon_view->priv->children;
2115   while (tmp_list)
2116     {
2117       child = tmp_list->data;
2118       tmp_list = tmp_list->next;
2119 
2120       (* callback) (child->widget, callback_data);
2121     }
2122 }
2123 
2124 static void
gtk_icon_view_item_selected_changed(GtkIconView * icon_view,GtkIconViewItem * item)2125 gtk_icon_view_item_selected_changed (GtkIconView      *icon_view,
2126                                      GtkIconViewItem  *item)
2127 {
2128   AtkObject *obj;
2129   AtkObject *item_obj;
2130 
2131   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
2132   if (obj != NULL)
2133     {
2134       item_obj = atk_object_ref_accessible_child (obj, item->index);
2135       if (item_obj != NULL)
2136         {
2137           atk_object_notify_state_change (item_obj, ATK_STATE_SELECTED, item->selected);
2138           g_object_unref (item_obj);
2139         }
2140     }
2141 }
2142 
2143 static void
gtk_icon_view_add_editable(GtkCellArea * area,GtkCellRenderer * renderer,GtkCellEditable * editable,GdkRectangle * cell_area,const gchar * path,GtkIconView * icon_view)2144 gtk_icon_view_add_editable (GtkCellArea            *area,
2145 			    GtkCellRenderer        *renderer,
2146 			    GtkCellEditable        *editable,
2147 			    GdkRectangle           *cell_area,
2148 			    const gchar            *path,
2149 			    GtkIconView            *icon_view)
2150 {
2151   GtkIconViewChild *child;
2152   GtkWidget *widget = GTK_WIDGET (editable);
2153 
2154   child = g_new (GtkIconViewChild, 1);
2155 
2156   child->widget      = widget;
2157   child->area.x      = cell_area->x;
2158   child->area.y      = cell_area->y;
2159   child->area.width  = cell_area->width;
2160   child->area.height = cell_area->height;
2161 
2162   icon_view->priv->children = g_list_append (icon_view->priv->children, child);
2163 
2164   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2165     gtk_widget_set_parent_window (child->widget, icon_view->priv->bin_window);
2166 
2167   gtk_widget_set_parent (widget, GTK_WIDGET (icon_view));
2168 }
2169 
2170 static void
gtk_icon_view_remove_editable(GtkCellArea * area,GtkCellRenderer * renderer,GtkCellEditable * editable,GtkIconView * icon_view)2171 gtk_icon_view_remove_editable (GtkCellArea            *area,
2172 			       GtkCellRenderer        *renderer,
2173 			       GtkCellEditable        *editable,
2174 			       GtkIconView            *icon_view)
2175 {
2176   GtkTreePath *path;
2177 
2178   if (gtk_widget_has_focus (GTK_WIDGET (editable)))
2179     gtk_widget_grab_focus (GTK_WIDGET (icon_view));
2180 
2181   gtk_container_remove (GTK_CONTAINER (icon_view),
2182 			GTK_WIDGET (editable));
2183 
2184   path = gtk_tree_path_new_from_string (gtk_cell_area_get_current_path_string (area));
2185   gtk_icon_view_queue_draw_path (icon_view, path);
2186   gtk_tree_path_free (path);
2187 }
2188 
2189 /**
2190  * gtk_icon_view_set_cursor:
2191  * @icon_view: A #GtkIconView
2192  * @path: A #GtkTreePath
2193  * @cell: (allow-none): One of the cell renderers of @icon_view, or %NULL
2194  * @start_editing: %TRUE if the specified cell should start being edited.
2195  *
2196  * Sets the current keyboard focus to be at @path, and selects it.  This is
2197  * useful when you want to focus the user’s attention on a particular item.
2198  * If @cell is not %NULL, then focus is given to the cell specified by
2199  * it. Additionally, if @start_editing is %TRUE, then editing should be
2200  * started in the specified cell.
2201  *
2202  * This function is often followed by `gtk_widget_grab_focus
2203  * (icon_view)` in order to give keyboard focus to the widget.
2204  * Please note that editing can only happen when the widget is realized.
2205  *
2206  * Since: 2.8
2207  **/
2208 void
gtk_icon_view_set_cursor(GtkIconView * icon_view,GtkTreePath * path,GtkCellRenderer * cell,gboolean start_editing)2209 gtk_icon_view_set_cursor (GtkIconView     *icon_view,
2210 			  GtkTreePath     *path,
2211 			  GtkCellRenderer *cell,
2212 			  gboolean         start_editing)
2213 {
2214   GtkIconViewItem *item = NULL;
2215 
2216   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
2217   g_return_if_fail (path != NULL);
2218   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
2219 
2220   if (icon_view->priv->cell_area)
2221     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2222 
2223   if (gtk_tree_path_get_depth (path) == 1)
2224     item = g_list_nth_data (icon_view->priv->items,
2225 			    gtk_tree_path_get_indices(path)[0]);
2226 
2227   if (!item)
2228     return;
2229 
2230   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
2231   gtk_icon_view_scroll_to_path (icon_view, path, FALSE, 0.0, 0.0);
2232 
2233   if (start_editing &&
2234       icon_view->priv->cell_area)
2235     {
2236       GtkCellAreaContext *context;
2237 
2238       context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2239       _gtk_icon_view_set_cell_data (icon_view, item);
2240       gtk_cell_area_activate (icon_view->priv->cell_area, context,
2241 			      GTK_WIDGET (icon_view), &item->cell_area,
2242 			      0 /* XXX flags */, TRUE);
2243     }
2244 }
2245 
2246 /**
2247  * gtk_icon_view_get_cursor:
2248  * @icon_view: A #GtkIconView
2249  * @path: (out) (allow-none) (transfer full): Return location for the current
2250  *        cursor path, or %NULL
2251  * @cell: (out) (allow-none) (transfer none): Return location the current
2252  *        focus cell, or %NULL
2253  *
2254  * Fills in @path and @cell with the current cursor path and cell.
2255  * If the cursor isn’t currently set, then *@path will be %NULL.
2256  * If no cell currently has focus, then *@cell will be %NULL.
2257  *
2258  * The returned #GtkTreePath must be freed with gtk_tree_path_free().
2259  *
2260  * Returns: %TRUE if the cursor is set.
2261  *
2262  * Since: 2.8
2263  **/
2264 gboolean
gtk_icon_view_get_cursor(GtkIconView * icon_view,GtkTreePath ** path,GtkCellRenderer ** cell)2265 gtk_icon_view_get_cursor (GtkIconView      *icon_view,
2266 			  GtkTreePath     **path,
2267 			  GtkCellRenderer **cell)
2268 {
2269   GtkIconViewItem *item;
2270 
2271   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
2272 
2273   item = icon_view->priv->cursor_item;
2274 
2275   if (path != NULL)
2276     {
2277       if (item != NULL)
2278 	*path = gtk_tree_path_new_from_indices (item->index, -1);
2279       else
2280 	*path = NULL;
2281     }
2282 
2283   if (cell != NULL && item != NULL && icon_view->priv->cell_area != NULL)
2284     *cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
2285 
2286   return (item != NULL);
2287 }
2288 
2289 static gboolean
gtk_icon_view_button_press(GtkWidget * widget,GdkEventButton * event)2290 gtk_icon_view_button_press (GtkWidget      *widget,
2291 			    GdkEventButton *event)
2292 {
2293   GtkIconView *icon_view;
2294   GtkIconViewItem *item;
2295   gboolean dirty = FALSE;
2296   GtkCellRenderer *cell = NULL, *cursor_cell = NULL;
2297 
2298   icon_view = GTK_ICON_VIEW (widget);
2299 
2300   if (event->window != icon_view->priv->bin_window)
2301     return FALSE;
2302 
2303   if (!gtk_widget_has_focus (widget))
2304     gtk_widget_grab_focus (widget);
2305 
2306   if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_BUTTON_PRESS)
2307     {
2308       GdkModifierType extend_mod_mask;
2309       GdkModifierType modify_mod_mask;
2310 
2311       extend_mod_mask =
2312         gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_EXTEND_SELECTION);
2313 
2314       modify_mod_mask =
2315         gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_MODIFY_SELECTION);
2316 
2317       item = _gtk_icon_view_get_item_at_coords (icon_view,
2318 					       event->x, event->y,
2319 					       FALSE,
2320 					       &cell);
2321 
2322       /*
2323        * We consider only the cells' area as the item area if the
2324        * item is not selected, but if it *is* selected, the complete
2325        * selection rectangle is considered to be part of the item.
2326        */
2327       if (item != NULL && (cell != NULL || item->selected))
2328 	{
2329 	  if (cell != NULL)
2330 	    {
2331 	      if (gtk_cell_renderer_is_activatable (cell))
2332 		cursor_cell = cell;
2333 	    }
2334 
2335 	  gtk_icon_view_scroll_to_item (icon_view, item);
2336 
2337 	  if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2338 	    {
2339 	      _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2340 	    }
2341 	  else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
2342 		   (event->state & extend_mod_mask))
2343 	    {
2344 	      gtk_icon_view_unselect_all_internal (icon_view);
2345 
2346 	      _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2347 	      if (!icon_view->priv->anchor_item)
2348 		icon_view->priv->anchor_item = item;
2349 	      else
2350 		gtk_icon_view_select_all_between (icon_view,
2351 						  icon_view->priv->anchor_item,
2352 						  item);
2353 	      dirty = TRUE;
2354 	    }
2355 	  else
2356 	    {
2357 	      if ((icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE ||
2358 		  ((icon_view->priv->selection_mode == GTK_SELECTION_SINGLE) && item->selected)) &&
2359 		  (event->state & modify_mod_mask))
2360 		{
2361 		  item->selected = !item->selected;
2362 		  gtk_icon_view_queue_draw_item (icon_view, item);
2363 		  dirty = TRUE;
2364 		}
2365 	      else
2366 		{
2367 		  gtk_icon_view_unselect_all_internal (icon_view);
2368 
2369 		  item->selected = TRUE;
2370 		  gtk_icon_view_queue_draw_item (icon_view, item);
2371 		  dirty = TRUE;
2372 		}
2373 	      _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2374 	      icon_view->priv->anchor_item = item;
2375 	    }
2376 
2377 	  /* Save press to possibly begin a drag */
2378 	  if (icon_view->priv->pressed_button < 0)
2379 	    {
2380 	      icon_view->priv->pressed_button = event->button;
2381 	      icon_view->priv->press_start_x = event->x;
2382 	      icon_view->priv->press_start_y = event->y;
2383 	    }
2384 
2385           icon_view->priv->last_single_clicked = item;
2386 
2387 	  /* cancel the current editing, if it exists */
2388 	  gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2389 
2390 	  if (cell != NULL && gtk_cell_renderer_is_activatable (cell))
2391 	    {
2392 	      GtkCellAreaContext *context;
2393 
2394 	      context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2395 
2396 	      _gtk_icon_view_set_cell_data (icon_view, item);
2397 	      gtk_cell_area_activate (icon_view->priv->cell_area, context,
2398 				      GTK_WIDGET (icon_view),
2399 				      &item->cell_area, 0/* XXX flags */, FALSE);
2400 	    }
2401 	}
2402       else
2403 	{
2404 	  if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
2405 	      !(event->state & modify_mod_mask))
2406 	    {
2407 	      dirty = gtk_icon_view_unselect_all_internal (icon_view);
2408 	    }
2409 
2410 	  if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
2411             gtk_icon_view_start_rubberbanding (icon_view, event->device, event->x, event->y);
2412 	}
2413 
2414       /* don't draw keyboard focus around an clicked-on item */
2415       icon_view->priv->draw_focus = FALSE;
2416     }
2417 
2418   if (!icon_view->priv->activate_on_single_click
2419       && event->button == GDK_BUTTON_PRIMARY
2420       && event->type == GDK_2BUTTON_PRESS)
2421     {
2422       item = _gtk_icon_view_get_item_at_coords (icon_view,
2423 					       event->x, event->y,
2424 					       FALSE,
2425 					       NULL);
2426 
2427       if (item && item == icon_view->priv->last_single_clicked)
2428 	{
2429 	  GtkTreePath *path;
2430 
2431 	  path = gtk_tree_path_new_from_indices (item->index, -1);
2432 	  gtk_icon_view_item_activated (icon_view, path);
2433 	  gtk_tree_path_free (path);
2434 	}
2435 
2436       icon_view->priv->last_single_clicked = NULL;
2437       icon_view->priv->pressed_button = -1;
2438     }
2439 
2440   if (dirty)
2441     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2442 
2443   return event->button == GDK_BUTTON_PRIMARY;
2444 }
2445 
2446 static gboolean
button_event_modifies_selection(GdkEventButton * event)2447 button_event_modifies_selection (GdkEventButton *event)
2448 {
2449         return (event->state & (GDK_CONTROL_MASK | GDK_SHIFT_MASK)) != 0;
2450 }
2451 
2452 static gboolean
gtk_icon_view_button_release(GtkWidget * widget,GdkEventButton * event)2453 gtk_icon_view_button_release (GtkWidget      *widget,
2454 			      GdkEventButton *event)
2455 {
2456   GtkIconView *icon_view;
2457 
2458   icon_view = GTK_ICON_VIEW (widget);
2459 
2460   if (icon_view->priv->pressed_button == event->button)
2461     icon_view->priv->pressed_button = -1;
2462 
2463   gtk_icon_view_stop_rubberbanding (icon_view);
2464 
2465   remove_scroll_timeout (icon_view);
2466 
2467   if (event->button == GDK_BUTTON_PRIMARY
2468       && icon_view->priv->activate_on_single_click
2469       && !button_event_modifies_selection (event)
2470       && icon_view->priv->last_single_clicked != NULL)
2471     {
2472       GtkIconViewItem *item;
2473 
2474       item = _gtk_icon_view_get_item_at_coords (icon_view,
2475                                                 event->x, event->y,
2476                                                 FALSE,
2477                                                 NULL);
2478       if (item == icon_view->priv->last_single_clicked)
2479         {
2480           GtkTreePath *path;
2481           path = gtk_tree_path_new_from_indices (item->index, -1);
2482           gtk_icon_view_item_activated (icon_view, path);
2483           gtk_tree_path_free (path);
2484         }
2485 
2486       icon_view->priv->last_single_clicked = NULL;
2487     }
2488 
2489   return TRUE;
2490 }
2491 
2492 static gboolean
gtk_icon_view_key_press(GtkWidget * widget,GdkEventKey * event)2493 gtk_icon_view_key_press (GtkWidget      *widget,
2494 			 GdkEventKey    *event)
2495 {
2496   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2497 
2498   if (icon_view->priv->doing_rubberband)
2499     {
2500       if (event->keyval == GDK_KEY_Escape)
2501 	gtk_icon_view_stop_rubberbanding (icon_view);
2502 
2503       return TRUE;
2504     }
2505 
2506   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2507 }
2508 
2509 static gboolean
gtk_icon_view_key_release(GtkWidget * widget,GdkEventKey * event)2510 gtk_icon_view_key_release (GtkWidget      *widget,
2511 			   GdkEventKey    *event)
2512 {
2513   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2514 
2515   if (icon_view->priv->doing_rubberband)
2516     return TRUE;
2517 
2518   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_release_event (widget, event);
2519 }
2520 
2521 static void
gtk_icon_view_update_rubberband(gpointer data)2522 gtk_icon_view_update_rubberband (gpointer data)
2523 {
2524   GtkIconView *icon_view;
2525   gint x, y;
2526   GdkRectangle old_area;
2527   GdkRectangle new_area;
2528   cairo_region_t *invalid_region;
2529 
2530   icon_view = GTK_ICON_VIEW (data);
2531 
2532   gdk_window_get_device_position (icon_view->priv->bin_window,
2533                                   icon_view->priv->rubberband_device,
2534                                   &x, &y, NULL);
2535 
2536   x = MAX (x, 0);
2537   y = MAX (y, 0);
2538 
2539   old_area.x = MIN (icon_view->priv->rubberband_x1,
2540 		    icon_view->priv->rubberband_x2);
2541   old_area.y = MIN (icon_view->priv->rubberband_y1,
2542 		    icon_view->priv->rubberband_y2);
2543   old_area.width = ABS (icon_view->priv->rubberband_x2 -
2544 			icon_view->priv->rubberband_x1) + 1;
2545   old_area.height = ABS (icon_view->priv->rubberband_y2 -
2546 			 icon_view->priv->rubberband_y1) + 1;
2547 
2548   new_area.x = MIN (icon_view->priv->rubberband_x1, x);
2549   new_area.y = MIN (icon_view->priv->rubberband_y1, y);
2550   new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
2551   new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
2552 
2553   invalid_region = cairo_region_create_rectangle (&old_area);
2554   cairo_region_union_rectangle (invalid_region, &new_area);
2555 
2556   gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
2557 
2558   cairo_region_destroy (invalid_region);
2559 
2560   icon_view->priv->rubberband_x2 = x;
2561   icon_view->priv->rubberband_y2 = y;
2562 
2563   gtk_icon_view_update_rubberband_selection (icon_view);
2564 }
2565 
2566 static void
gtk_icon_view_start_rubberbanding(GtkIconView * icon_view,GdkDevice * device,gint x,gint y)2567 gtk_icon_view_start_rubberbanding (GtkIconView  *icon_view,
2568                                    GdkDevice    *device,
2569 				   gint          x,
2570 				   gint          y)
2571 {
2572   GtkIconViewPrivate *priv = icon_view->priv;
2573   GList *items;
2574   GtkCssNode *widget_node;
2575 
2576   if (priv->rubberband_device)
2577     return;
2578 
2579   for (items = priv->items; items; items = items->next)
2580     {
2581       GtkIconViewItem *item = items->data;
2582 
2583       item->selected_before_rubberbanding = item->selected;
2584     }
2585 
2586   priv->rubberband_x1 = x;
2587   priv->rubberband_y1 = y;
2588   priv->rubberband_x2 = x;
2589   priv->rubberband_y2 = y;
2590 
2591   priv->doing_rubberband = TRUE;
2592   priv->rubberband_device = device;
2593 
2594   widget_node = gtk_widget_get_css_node (GTK_WIDGET (icon_view));
2595   priv->rubberband_node = gtk_css_node_new ();
2596   gtk_css_node_set_name (priv->rubberband_node, I_("rubberband"));
2597   gtk_css_node_set_parent (priv->rubberband_node, widget_node);
2598   gtk_css_node_set_state (priv->rubberband_node, gtk_css_node_get_state (widget_node));
2599   g_object_unref (priv->rubberband_node);
2600 }
2601 
2602 static void
gtk_icon_view_stop_rubberbanding(GtkIconView * icon_view)2603 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
2604 {
2605   GtkIconViewPrivate *priv = icon_view->priv;
2606 
2607   if (!priv->doing_rubberband)
2608     return;
2609 
2610   priv->doing_rubberband = FALSE;
2611   priv->rubberband_device = NULL;
2612   gtk_css_node_set_parent (priv->rubberband_node, NULL);
2613   priv->rubberband_node = NULL;
2614 
2615   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2616 }
2617 
2618 static void
gtk_icon_view_update_rubberband_selection(GtkIconView * icon_view)2619 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
2620 {
2621   GList *items;
2622   gint x, y, width, height;
2623   gboolean dirty = FALSE;
2624 
2625   x = MIN (icon_view->priv->rubberband_x1,
2626 	   icon_view->priv->rubberband_x2);
2627   y = MIN (icon_view->priv->rubberband_y1,
2628 	   icon_view->priv->rubberband_y2);
2629   width = ABS (icon_view->priv->rubberband_x1 -
2630 	       icon_view->priv->rubberband_x2);
2631   height = ABS (icon_view->priv->rubberband_y1 -
2632 		icon_view->priv->rubberband_y2);
2633 
2634   for (items = icon_view->priv->items; items; items = items->next)
2635     {
2636       GtkIconViewItem *item = items->data;
2637       gboolean is_in;
2638       gboolean selected;
2639 
2640       is_in = gtk_icon_view_item_hit_test (icon_view, item,
2641 					   x, y, width, height);
2642 
2643       selected = is_in ^ item->selected_before_rubberbanding;
2644 
2645       if (item->selected != selected)
2646 	{
2647 	  item->selected = selected;
2648 	  dirty = TRUE;
2649 	  gtk_icon_view_queue_draw_item (icon_view, item);
2650 	}
2651     }
2652 
2653   if (dirty)
2654     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2655 }
2656 
2657 
2658 typedef struct {
2659   GdkRectangle hit_rect;
2660   gboolean     hit;
2661 } HitTestData;
2662 
2663 static gboolean
hit_test(GtkCellRenderer * renderer,const GdkRectangle * cell_area,const GdkRectangle * cell_background,HitTestData * data)2664 hit_test (GtkCellRenderer    *renderer,
2665 	  const GdkRectangle *cell_area,
2666 	  const GdkRectangle *cell_background,
2667 	  HitTestData        *data)
2668 {
2669   if (MIN (data->hit_rect.x + data->hit_rect.width, cell_area->x + cell_area->width) -
2670       MAX (data->hit_rect.x, cell_area->x) > 0 &&
2671       MIN (data->hit_rect.y + data->hit_rect.height, cell_area->y + cell_area->height) -
2672       MAX (data->hit_rect.y, cell_area->y) > 0)
2673     data->hit = TRUE;
2674 
2675   return (data->hit != FALSE);
2676 }
2677 
2678 static gboolean
gtk_icon_view_item_hit_test(GtkIconView * icon_view,GtkIconViewItem * item,gint x,gint y,gint width,gint height)2679 gtk_icon_view_item_hit_test (GtkIconView      *icon_view,
2680 			     GtkIconViewItem  *item,
2681 			     gint              x,
2682 			     gint              y,
2683 			     gint              width,
2684 			     gint              height)
2685 {
2686   HitTestData data = { { x, y, width, height }, FALSE };
2687   GtkCellAreaContext *context;
2688   GdkRectangle *item_area = &item->cell_area;
2689 
2690   if (MIN (x + width, item_area->x + item_area->width) - MAX (x, item_area->x) <= 0 ||
2691       MIN (y + height, item_area->y + item_area->height) - MAX (y, item_area->y) <= 0)
2692     return FALSE;
2693 
2694   context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2695 
2696   _gtk_icon_view_set_cell_data (icon_view, item);
2697   gtk_cell_area_foreach_alloc (icon_view->priv->cell_area, context,
2698 			       GTK_WIDGET (icon_view),
2699 			       item_area, item_area,
2700 			       (GtkCellAllocCallback)hit_test, &data);
2701 
2702   return data.hit;
2703 }
2704 
2705 static gboolean
gtk_icon_view_unselect_all_internal(GtkIconView * icon_view)2706 gtk_icon_view_unselect_all_internal (GtkIconView  *icon_view)
2707 {
2708   gboolean dirty = FALSE;
2709   GList *items;
2710 
2711   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2712     return FALSE;
2713 
2714   for (items = icon_view->priv->items; items; items = items->next)
2715     {
2716       GtkIconViewItem *item = items->data;
2717 
2718       if (item->selected)
2719 	{
2720 	  item->selected = FALSE;
2721 	  dirty = TRUE;
2722 	  gtk_icon_view_queue_draw_item (icon_view, item);
2723 	  gtk_icon_view_item_selected_changed (icon_view, item);
2724 	}
2725     }
2726 
2727   return dirty;
2728 }
2729 
2730 
2731 /* GtkIconView signals */
2732 static void
gtk_icon_view_real_select_all(GtkIconView * icon_view)2733 gtk_icon_view_real_select_all (GtkIconView *icon_view)
2734 {
2735   gtk_icon_view_select_all (icon_view);
2736 }
2737 
2738 static void
gtk_icon_view_real_unselect_all(GtkIconView * icon_view)2739 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
2740 {
2741   gtk_icon_view_unselect_all (icon_view);
2742 }
2743 
2744 static void
gtk_icon_view_real_select_cursor_item(GtkIconView * icon_view)2745 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
2746 {
2747   gtk_icon_view_unselect_all (icon_view);
2748 
2749   if (icon_view->priv->cursor_item != NULL)
2750     _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2751 }
2752 
2753 static gboolean
gtk_icon_view_real_activate_cursor_item(GtkIconView * icon_view)2754 gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view)
2755 {
2756   GtkTreePath *path;
2757   GtkCellAreaContext *context;
2758 
2759   if (!icon_view->priv->cursor_item)
2760     return FALSE;
2761 
2762   context = g_ptr_array_index (icon_view->priv->row_contexts, icon_view->priv->cursor_item->row);
2763 
2764   _gtk_icon_view_set_cell_data (icon_view, icon_view->priv->cursor_item);
2765   gtk_cell_area_activate (icon_view->priv->cell_area, context,
2766                           GTK_WIDGET (icon_view),
2767                           &icon_view->priv->cursor_item->cell_area,
2768                           0 /* XXX flags */,
2769                           FALSE);
2770 
2771   path = gtk_tree_path_new_from_indices (icon_view->priv->cursor_item->index, -1);
2772   gtk_icon_view_item_activated (icon_view, path);
2773   gtk_tree_path_free (path);
2774 
2775   return TRUE;
2776 }
2777 
2778 static void
gtk_icon_view_real_toggle_cursor_item(GtkIconView * icon_view)2779 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
2780 {
2781   if (!icon_view->priv->cursor_item)
2782     return;
2783 
2784   switch (icon_view->priv->selection_mode)
2785     {
2786     case GTK_SELECTION_NONE:
2787       break;
2788     case GTK_SELECTION_BROWSE:
2789       _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2790       break;
2791     case GTK_SELECTION_SINGLE:
2792       if (icon_view->priv->cursor_item->selected)
2793 	_gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
2794       else
2795 	_gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2796       break;
2797     case GTK_SELECTION_MULTIPLE:
2798       icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
2799       g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2800 
2801       gtk_icon_view_item_selected_changed (icon_view, icon_view->priv->cursor_item);
2802       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2803       break;
2804     }
2805 }
2806 
2807 static void
gtk_icon_view_set_hadjustment_values(GtkIconView * icon_view)2808 gtk_icon_view_set_hadjustment_values (GtkIconView *icon_view)
2809 {
2810   GtkAllocation  allocation;
2811   GtkAdjustment *adj = icon_view->priv->hadjustment;
2812   gdouble old_page_size;
2813   gdouble old_upper;
2814   gdouble old_value;
2815   gdouble new_value;
2816   gdouble new_upper;
2817 
2818   gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2819 
2820   old_value = gtk_adjustment_get_value (adj);
2821   old_upper = gtk_adjustment_get_upper (adj);
2822   old_page_size = gtk_adjustment_get_page_size (adj);
2823   new_upper = MAX (allocation.width, icon_view->priv->width);
2824 
2825   if (gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL)
2826     {
2827       /* Make sure no scrolling occurs for RTL locales also (if possible) */
2828       /* Quick explanation:
2829        *   In LTR locales, leftmost portion of visible rectangle should stay
2830        *   fixed, which means left edge of scrollbar thumb should remain fixed
2831        *   and thus adjustment's value should stay the same.
2832        *
2833        *   In RTL locales, we want to keep rightmost portion of visible
2834        *   rectangle fixed. This means right edge of thumb should remain fixed.
2835        *   In this case, upper - value - page_size should remain constant.
2836        */
2837       new_value = (new_upper - allocation.width) -
2838                   (old_upper - old_value - old_page_size);
2839       new_value = CLAMP (new_value, 0, new_upper - allocation.width);
2840     }
2841   else
2842     new_value = CLAMP (old_value, 0, new_upper - allocation.width);
2843 
2844   gtk_adjustment_configure (adj,
2845                             new_value,
2846                             0.0,
2847                             new_upper,
2848                             allocation.width * 0.1,
2849                             allocation.width * 0.9,
2850                             allocation.width);
2851 }
2852 
2853 static void
gtk_icon_view_set_vadjustment_values(GtkIconView * icon_view)2854 gtk_icon_view_set_vadjustment_values (GtkIconView *icon_view)
2855 {
2856   GtkAllocation  allocation;
2857   GtkAdjustment *adj = icon_view->priv->vadjustment;
2858 
2859   gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2860 
2861   gtk_adjustment_configure (adj,
2862                             gtk_adjustment_get_value (adj),
2863                             0.0,
2864                             MAX (allocation.height, icon_view->priv->height),
2865                             allocation.height * 0.1,
2866                             allocation.height * 0.9,
2867                             allocation.height);
2868 }
2869 
2870 static void
gtk_icon_view_set_hadjustment(GtkIconView * icon_view,GtkAdjustment * adjustment)2871 gtk_icon_view_set_hadjustment (GtkIconView   *icon_view,
2872                                GtkAdjustment *adjustment)
2873 {
2874   GtkIconViewPrivate *priv = icon_view->priv;
2875 
2876   if (adjustment && priv->hadjustment == adjustment)
2877     return;
2878 
2879   if (priv->hadjustment != NULL)
2880     {
2881       g_signal_handlers_disconnect_matched (priv->hadjustment,
2882                                             G_SIGNAL_MATCH_DATA,
2883                                             0, 0, NULL, NULL, icon_view);
2884       g_object_unref (priv->hadjustment);
2885     }
2886 
2887   if (!adjustment)
2888     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2889                                      0.0, 0.0, 0.0);
2890 
2891   g_signal_connect (adjustment, "value-changed",
2892                     G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2893   priv->hadjustment = g_object_ref_sink (adjustment);
2894   gtk_icon_view_set_hadjustment_values (icon_view);
2895 
2896   g_object_notify (G_OBJECT (icon_view), "hadjustment");
2897 }
2898 
2899 static void
gtk_icon_view_set_vadjustment(GtkIconView * icon_view,GtkAdjustment * adjustment)2900 gtk_icon_view_set_vadjustment (GtkIconView   *icon_view,
2901                                GtkAdjustment *adjustment)
2902 {
2903   GtkIconViewPrivate *priv = icon_view->priv;
2904 
2905   if (adjustment && priv->vadjustment == adjustment)
2906     return;
2907 
2908   if (priv->vadjustment != NULL)
2909     {
2910       g_signal_handlers_disconnect_matched (priv->vadjustment,
2911                                             G_SIGNAL_MATCH_DATA,
2912                                             0, 0, NULL, NULL, icon_view);
2913       g_object_unref (priv->vadjustment);
2914     }
2915 
2916   if (!adjustment)
2917     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2918                                      0.0, 0.0, 0.0);
2919 
2920   g_signal_connect (adjustment, "value-changed",
2921                     G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2922   priv->vadjustment = g_object_ref_sink (adjustment);
2923   gtk_icon_view_set_vadjustment_values (icon_view);
2924 
2925   g_object_notify (G_OBJECT (icon_view), "vadjustment");
2926 }
2927 
2928 static void
gtk_icon_view_adjustment_changed(GtkAdjustment * adjustment,GtkIconView * icon_view)2929 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
2930                                   GtkIconView   *icon_view)
2931 {
2932   GtkIconViewPrivate *priv = icon_view->priv;
2933 
2934   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2935     {
2936       gdk_window_move (priv->bin_window,
2937                        - gtk_adjustment_get_value (priv->hadjustment),
2938                        - gtk_adjustment_get_value (priv->vadjustment));
2939 
2940       if (icon_view->priv->doing_rubberband)
2941         gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
2942 
2943       _gtk_icon_view_accessible_adjustment_changed (icon_view);
2944     }
2945 }
2946 
2947 static gint
compare_sizes(gconstpointer p1,gconstpointer p2,gpointer unused)2948 compare_sizes (gconstpointer p1,
2949 	       gconstpointer p2,
2950                gpointer      unused)
2951 {
2952   return GPOINTER_TO_INT (((const GtkRequestedSize *) p1)->data)
2953        - GPOINTER_TO_INT (((const GtkRequestedSize *) p2)->data);
2954 }
2955 
2956 static void
gtk_icon_view_layout(GtkIconView * icon_view)2957 gtk_icon_view_layout (GtkIconView *icon_view)
2958 {
2959   GtkIconViewPrivate *priv = icon_view->priv;
2960   GtkWidget *widget = GTK_WIDGET (icon_view);
2961   GList *items;
2962   gint item_width; /* this doesn't include item_padding */
2963   gint n_columns, n_rows, n_items;
2964   gint col, row;
2965   GtkRequestedSize *sizes;
2966   gboolean rtl;
2967 
2968   if (gtk_icon_view_is_empty (icon_view))
2969     return;
2970 
2971   rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2972   n_items = gtk_icon_view_get_n_items (icon_view);
2973 
2974   gtk_icon_view_compute_n_items_for_size (icon_view,
2975                                           GTK_ORIENTATION_HORIZONTAL,
2976                                           gtk_widget_get_allocated_width (widget),
2977                                           NULL, NULL,
2978                                           &n_columns, &item_width);
2979   n_rows = (n_items + n_columns - 1) / n_columns;
2980 
2981   priv->width = n_columns * (item_width + 2 * priv->item_padding + priv->column_spacing) - priv->column_spacing;
2982   priv->width += 2 * priv->margin;
2983   priv->width = MAX (priv->width, gtk_widget_get_allocated_width (widget));
2984 
2985   /* Clear the per row contexts */
2986   g_ptr_array_set_size (icon_view->priv->row_contexts, 0);
2987 
2988   gtk_cell_area_context_reset (priv->cell_area_context);
2989   /* because layouting is complicated. We designed an API
2990    * that is O(N²) and nonsensical.
2991    * And we're proud of it. */
2992   for (items = priv->items; items; items = items->next)
2993     {
2994       _gtk_icon_view_set_cell_data (icon_view, items->data);
2995       gtk_cell_area_get_preferred_width (priv->cell_area,
2996                                          priv->cell_area_context,
2997                                          widget,
2998                                          NULL, NULL);
2999     }
3000 
3001   sizes = g_newa (GtkRequestedSize, n_rows);
3002   items = priv->items;
3003   priv->height = priv->margin;
3004 
3005   /* Collect the heights for all rows */
3006   for (row = 0; row < n_rows; row++)
3007     {
3008       GtkCellAreaContext *context = gtk_cell_area_copy_context (priv->cell_area, priv->cell_area_context);
3009       g_ptr_array_add (priv->row_contexts, context);
3010 
3011       for (col = 0; col < n_columns && items; col++, items = items->next)
3012         {
3013           GtkIconViewItem *item = items->data;
3014 
3015           _gtk_icon_view_set_cell_data (icon_view, item);
3016           gtk_cell_area_get_preferred_height_for_width (priv->cell_area,
3017                                                         context,
3018                                                         widget,
3019                                                         item_width,
3020                                                         NULL, NULL);
3021         }
3022 
3023       sizes[row].data = GINT_TO_POINTER (row);
3024       gtk_cell_area_context_get_preferred_height_for_width (context,
3025                                                             item_width,
3026                                                             &sizes[row].minimum_size,
3027                                                             &sizes[row].natural_size);
3028       priv->height += sizes[row].minimum_size + 2 * priv->item_padding + priv->row_spacing;
3029     }
3030 
3031   priv->height -= priv->row_spacing;
3032   priv->height += priv->margin;
3033   priv->height = MIN (priv->height, gtk_widget_get_allocated_height (widget));
3034 
3035   gtk_distribute_natural_allocation (gtk_widget_get_allocated_height (widget) - priv->height,
3036                                      n_rows,
3037                                      sizes);
3038 
3039   /* Actually allocate the rows */
3040   g_qsort_with_data (sizes, n_rows, sizeof (GtkRequestedSize), compare_sizes, NULL);
3041 
3042   items = priv->items;
3043   priv->height = priv->margin;
3044 
3045   for (row = 0; row < n_rows; row++)
3046     {
3047       GtkCellAreaContext *context = g_ptr_array_index (priv->row_contexts, row);
3048       gtk_cell_area_context_allocate (context, item_width, sizes[row].minimum_size);
3049 
3050       priv->height += priv->item_padding;
3051 
3052       for (col = 0; col < n_columns && items; col++, items = items->next)
3053         {
3054           GtkIconViewItem *item = items->data;
3055 
3056           item->cell_area.x = priv->margin + (col * 2 + 1) * priv->item_padding + col * (priv->column_spacing + item_width);
3057           item->cell_area.width = item_width;
3058           item->cell_area.y = priv->height;
3059           item->cell_area.height = sizes[row].minimum_size;
3060           item->row = row;
3061           item->col = col;
3062           if (rtl)
3063             {
3064               item->cell_area.x = priv->width - item_width - item->cell_area.x;
3065               item->col = n_columns - 1 - col;
3066             }
3067         }
3068 
3069       priv->height += sizes[row].minimum_size + priv->item_padding + priv->row_spacing;
3070     }
3071 
3072   priv->height -= priv->row_spacing;
3073   priv->height += priv->margin;
3074   priv->height = MAX (priv->height, gtk_widget_get_allocated_height (widget));
3075 }
3076 
3077 static void
gtk_icon_view_invalidate_sizes(GtkIconView * icon_view)3078 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
3079 {
3080   /* Clear all item sizes */
3081   g_list_foreach (icon_view->priv->items,
3082 		  (GFunc)gtk_icon_view_item_invalidate_size, NULL);
3083 
3084   /* Re-layout the items */
3085   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3086 }
3087 
3088 static void
gtk_icon_view_item_invalidate_size(GtkIconViewItem * item)3089 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
3090 {
3091   item->cell_area.width = -1;
3092   item->cell_area.height = -1;
3093 }
3094 
3095 static void
gtk_icon_view_paint_item(GtkIconView * icon_view,cairo_t * cr,GtkIconViewItem * item,gint x,gint y,gboolean draw_focus)3096 gtk_icon_view_paint_item (GtkIconView     *icon_view,
3097                           cairo_t         *cr,
3098                           GtkIconViewItem *item,
3099                           gint             x,
3100                           gint             y,
3101                           gboolean         draw_focus)
3102 {
3103   GdkRectangle cell_area;
3104   GtkStateFlags state = 0;
3105   GtkCellRendererState flags = 0;
3106   GtkStyleContext *style_context;
3107   GtkWidget *widget = GTK_WIDGET (icon_view);
3108   GtkIconViewPrivate *priv = icon_view->priv;
3109   GtkCellAreaContext *context;
3110 
3111   if (priv->model == NULL || item->cell_area.width <= 0 || item->cell_area.height <= 0)
3112     return;
3113 
3114   _gtk_icon_view_set_cell_data (icon_view, item);
3115 
3116   style_context = gtk_widget_get_style_context (widget);
3117   state = gtk_widget_get_state_flags (widget);
3118 
3119   gtk_style_context_save (style_context);
3120   gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_CELL);
3121 
3122   state &= ~(GTK_STATE_FLAG_SELECTED | GTK_STATE_FLAG_PRELIGHT);
3123 
3124   if ((state & GTK_STATE_FLAG_FOCUSED) &&
3125       item == icon_view->priv->cursor_item)
3126     flags |= GTK_CELL_RENDERER_FOCUSED;
3127 
3128   if (item->selected)
3129     {
3130       state |= GTK_STATE_FLAG_SELECTED;
3131       flags |= GTK_CELL_RENDERER_SELECTED;
3132     }
3133 
3134   if (item == priv->last_prelight)
3135     {
3136       state |= GTK_STATE_FLAG_PRELIGHT;
3137       flags |= GTK_CELL_RENDERER_PRELIT;
3138     }
3139 
3140   gtk_style_context_set_state (style_context, state);
3141 
3142   gtk_render_background (style_context, cr,
3143                          x - priv->item_padding,
3144                          y - priv->item_padding,
3145                          item->cell_area.width  + priv->item_padding * 2,
3146                          item->cell_area.height + priv->item_padding * 2);
3147   gtk_render_frame (style_context, cr,
3148                     x - priv->item_padding,
3149                     y - priv->item_padding,
3150                     item->cell_area.width  + priv->item_padding * 2,
3151                     item->cell_area.height + priv->item_padding * 2);
3152 
3153   cell_area.x      = x;
3154   cell_area.y      = y;
3155   cell_area.width  = item->cell_area.width;
3156   cell_area.height = item->cell_area.height;
3157 
3158   context = g_ptr_array_index (priv->row_contexts, item->row);
3159   gtk_cell_area_render (priv->cell_area, context,
3160                         widget, cr, &cell_area, &cell_area, flags,
3161                         draw_focus);
3162 
3163   gtk_style_context_restore (style_context);
3164 }
3165 
3166 static void
gtk_icon_view_paint_rubberband(GtkIconView * icon_view,cairo_t * cr)3167 gtk_icon_view_paint_rubberband (GtkIconView *icon_view,
3168 				cairo_t     *cr)
3169 {
3170   GtkIconViewPrivate *priv = icon_view->priv;
3171   GtkStyleContext *context;
3172   GdkRectangle rect;
3173 
3174   cairo_save (cr);
3175 
3176   rect.x = MIN (priv->rubberband_x1, priv->rubberband_x2);
3177   rect.y = MIN (priv->rubberband_y1, priv->rubberband_y2);
3178   rect.width = ABS (priv->rubberband_x1 - priv->rubberband_x2) + 1;
3179   rect.height = ABS (priv->rubberband_y1 - priv->rubberband_y2) + 1;
3180 
3181   context = gtk_widget_get_style_context (GTK_WIDGET (icon_view));
3182 
3183   gtk_style_context_save_to_node (context, priv->rubberband_node);
3184 
3185   gdk_cairo_rectangle (cr, &rect);
3186   cairo_clip (cr);
3187 
3188   gtk_render_background (context, cr,
3189                          rect.x, rect.y,
3190                          rect.width, rect.height);
3191   gtk_render_frame (context, cr,
3192                     rect.x, rect.y,
3193                     rect.width, rect.height);
3194 
3195   gtk_style_context_restore (context);
3196   cairo_restore (cr);
3197 }
3198 
3199 static void
gtk_icon_view_queue_draw_path(GtkIconView * icon_view,GtkTreePath * path)3200 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
3201 			       GtkTreePath *path)
3202 {
3203   GList *l;
3204   gint index;
3205 
3206   index = gtk_tree_path_get_indices (path)[0];
3207 
3208   for (l = icon_view->priv->items; l; l = l->next)
3209     {
3210       GtkIconViewItem *item = l->data;
3211 
3212       if (item->index == index)
3213 	{
3214 	  gtk_icon_view_queue_draw_item (icon_view, item);
3215 	  break;
3216 	}
3217     }
3218 }
3219 
3220 static void
gtk_icon_view_queue_draw_item(GtkIconView * icon_view,GtkIconViewItem * item)3221 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
3222 			       GtkIconViewItem *item)
3223 {
3224   GdkRectangle  rect;
3225   GdkRectangle *item_area = &item->cell_area;
3226 
3227   rect.x      = item_area->x - icon_view->priv->item_padding;
3228   rect.y      = item_area->y - icon_view->priv->item_padding;
3229   rect.width  = item_area->width  + icon_view->priv->item_padding * 2;
3230   rect.height = item_area->height + icon_view->priv->item_padding * 2;
3231 
3232   if (icon_view->priv->bin_window)
3233     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
3234 }
3235 
3236 void
_gtk_icon_view_set_cursor_item(GtkIconView * icon_view,GtkIconViewItem * item,GtkCellRenderer * cursor_cell)3237 _gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
3238                                 GtkIconViewItem *item,
3239                                 GtkCellRenderer *cursor_cell)
3240 {
3241   AtkObject *obj;
3242   AtkObject *item_obj;
3243   AtkObject *cursor_item_obj;
3244 
3245   /* When hitting this path from keynav, the focus cell is
3246    * already set, we dont need to notify the atk object
3247    * but we still need to queue the draw here (in the case
3248    * that the focus cell changes but not the cursor item).
3249    */
3250   gtk_icon_view_queue_draw_item (icon_view, item);
3251 
3252   if (icon_view->priv->cursor_item == item &&
3253       (cursor_cell == NULL || cursor_cell == gtk_cell_area_get_focus_cell (icon_view->priv->cell_area)))
3254     return;
3255 
3256   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3257   if (icon_view->priv->cursor_item != NULL)
3258     {
3259       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3260       if (obj != NULL)
3261         {
3262           cursor_item_obj = atk_object_ref_accessible_child (obj, icon_view->priv->cursor_item->index);
3263           if (cursor_item_obj != NULL)
3264             atk_object_notify_state_change (cursor_item_obj, ATK_STATE_FOCUSED, FALSE);
3265         }
3266     }
3267   icon_view->priv->cursor_item = item;
3268 
3269   if (cursor_cell)
3270     gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cursor_cell);
3271   else
3272     {
3273       /* Make sure there is a cell in focus initially */
3274       if (!gtk_cell_area_get_focus_cell (icon_view->priv->cell_area))
3275 	gtk_cell_area_focus (icon_view->priv->cell_area, GTK_DIR_TAB_FORWARD);
3276     }
3277 
3278   /* Notify that accessible focus object has changed */
3279   item_obj = atk_object_ref_accessible_child (obj, item->index);
3280 
3281   if (item_obj != NULL)
3282     {
3283       G_GNUC_BEGIN_IGNORE_DEPRECATIONS;
3284       atk_focus_tracker_notify (item_obj);
3285       G_GNUC_END_IGNORE_DEPRECATIONS;
3286       atk_object_notify_state_change (item_obj, ATK_STATE_FOCUSED, TRUE);
3287       g_object_unref (item_obj);
3288     }
3289 }
3290 
3291 
3292 static GtkIconViewItem *
gtk_icon_view_item_new(void)3293 gtk_icon_view_item_new (void)
3294 {
3295   GtkIconViewItem *item;
3296 
3297   item = g_slice_new0 (GtkIconViewItem);
3298 
3299   item->cell_area.width  = -1;
3300   item->cell_area.height = -1;
3301 
3302   return item;
3303 }
3304 
3305 static void
gtk_icon_view_item_free(GtkIconViewItem * item)3306 gtk_icon_view_item_free (GtkIconViewItem *item)
3307 {
3308   g_return_if_fail (item != NULL);
3309 
3310   g_slice_free (GtkIconViewItem, item);
3311 }
3312 
3313 GtkIconViewItem *
_gtk_icon_view_get_item_at_coords(GtkIconView * icon_view,gint x,gint y,gboolean only_in_cell,GtkCellRenderer ** cell_at_pos)3314 _gtk_icon_view_get_item_at_coords (GtkIconView          *icon_view,
3315                                    gint                  x,
3316                                    gint                  y,
3317                                    gboolean              only_in_cell,
3318                                    GtkCellRenderer     **cell_at_pos)
3319 {
3320   GList *items;
3321 
3322   if (cell_at_pos)
3323     *cell_at_pos = NULL;
3324 
3325   for (items = icon_view->priv->items; items; items = items->next)
3326     {
3327       GtkIconViewItem *item = items->data;
3328       GdkRectangle    *item_area = &item->cell_area;
3329 
3330       if (x >= item_area->x - icon_view->priv->column_spacing/2 &&
3331 	  x <= item_area->x + item_area->width + icon_view->priv->column_spacing/2 &&
3332 	  y >= item_area->y - icon_view->priv->row_spacing/2 &&
3333 	  y <= item_area->y + item_area->height + icon_view->priv->row_spacing/2)
3334 	{
3335 	  if (only_in_cell || cell_at_pos)
3336 	    {
3337 	      GtkCellRenderer *cell = NULL;
3338 	      GtkCellAreaContext *context;
3339 
3340 	      context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
3341 	      _gtk_icon_view_set_cell_data (icon_view, item);
3342 
3343 	      if (x >= item_area->x && x <= item_area->x + item_area->width &&
3344 		  y >= item_area->y && y <= item_area->y + item_area->height)
3345 		cell = gtk_cell_area_get_cell_at_position (icon_view->priv->cell_area, context,
3346 							   GTK_WIDGET (icon_view),
3347 							   item_area,
3348 							   x, y, NULL);
3349 
3350 	      if (cell_at_pos)
3351 		*cell_at_pos = cell;
3352 
3353 	      if (only_in_cell)
3354 		return cell != NULL ? item : NULL;
3355 	      else
3356 		return item;
3357 	    }
3358 	  return item;
3359 	}
3360     }
3361   return NULL;
3362 }
3363 
3364 void
_gtk_icon_view_select_item(GtkIconView * icon_view,GtkIconViewItem * item)3365 _gtk_icon_view_select_item (GtkIconView      *icon_view,
3366                             GtkIconViewItem  *item)
3367 {
3368   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3369   g_return_if_fail (item != NULL);
3370 
3371   if (item->selected)
3372     return;
3373 
3374   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3375     return;
3376   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3377     gtk_icon_view_unselect_all_internal (icon_view);
3378 
3379   item->selected = TRUE;
3380 
3381   gtk_icon_view_item_selected_changed (icon_view, item);
3382   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3383 
3384   gtk_icon_view_queue_draw_item (icon_view, item);
3385 }
3386 
3387 
3388 void
_gtk_icon_view_unselect_item(GtkIconView * icon_view,GtkIconViewItem * item)3389 _gtk_icon_view_unselect_item (GtkIconView      *icon_view,
3390                               GtkIconViewItem  *item)
3391 {
3392   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3393   g_return_if_fail (item != NULL);
3394 
3395   if (!item->selected)
3396     return;
3397 
3398   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3399       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3400     return;
3401 
3402   item->selected = FALSE;
3403 
3404   gtk_icon_view_item_selected_changed (icon_view, item);
3405   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3406 
3407   gtk_icon_view_queue_draw_item (icon_view, item);
3408 }
3409 
3410 static void
verify_items(GtkIconView * icon_view)3411 verify_items (GtkIconView *icon_view)
3412 {
3413   GList *items;
3414   int i = 0;
3415 
3416   for (items = icon_view->priv->items; items; items = items->next)
3417     {
3418       GtkIconViewItem *item = items->data;
3419 
3420       if (item->index != i)
3421 	g_error ("List item does not match its index: "
3422 		 "item index %d and list index %d\n", item->index, i);
3423 
3424       i++;
3425     }
3426 }
3427 
3428 static void
gtk_icon_view_row_changed(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,gpointer data)3429 gtk_icon_view_row_changed (GtkTreeModel *model,
3430                            GtkTreePath  *path,
3431                            GtkTreeIter  *iter,
3432                            gpointer      data)
3433 {
3434   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3435 
3436   /* ignore changes in branches */
3437   if (gtk_tree_path_get_depth (path) > 1)
3438     return;
3439 
3440   /* An icon view subclass might add it's own model and populate
3441    * things at init() time instead of waiting for the constructor()
3442    * to be called
3443    */
3444   if (icon_view->priv->cell_area)
3445     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3446 
3447   /* Here we can use a "grow-only" strategy for optimization
3448    * and only invalidate a single item and queue a relayout
3449    * instead of invalidating the whole thing.
3450    *
3451    * For now GtkIconView still cant deal with huge models
3452    * so just invalidate the whole thing when the model
3453    * changes.
3454    */
3455   gtk_icon_view_invalidate_sizes (icon_view);
3456 
3457   verify_items (icon_view);
3458 }
3459 
3460 static void
gtk_icon_view_row_inserted(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,gpointer data)3461 gtk_icon_view_row_inserted (GtkTreeModel *model,
3462 			    GtkTreePath  *path,
3463 			    GtkTreeIter  *iter,
3464 			    gpointer      data)
3465 {
3466   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3467   gint index;
3468   GtkIconViewItem *item;
3469   GList *list;
3470 
3471   /* ignore changes in branches */
3472   if (gtk_tree_path_get_depth (path) > 1)
3473     return;
3474 
3475   gtk_tree_model_ref_node (model, iter);
3476 
3477   index = gtk_tree_path_get_indices(path)[0];
3478 
3479   item = gtk_icon_view_item_new ();
3480 
3481   item->index = index;
3482 
3483   /* FIXME: We can be more efficient here,
3484      we can store a tail pointer and use that when
3485      appending (which is a rather common operation)
3486   */
3487   icon_view->priv->items = g_list_insert (icon_view->priv->items,
3488 					 item, index);
3489 
3490   list = g_list_nth (icon_view->priv->items, index + 1);
3491   for (; list; list = list->next)
3492     {
3493       item = list->data;
3494 
3495       item->index++;
3496     }
3497 
3498   verify_items (icon_view);
3499 
3500   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3501 }
3502 
3503 static void
gtk_icon_view_row_deleted(GtkTreeModel * model,GtkTreePath * path,gpointer data)3504 gtk_icon_view_row_deleted (GtkTreeModel *model,
3505 			   GtkTreePath  *path,
3506 			   gpointer      data)
3507 {
3508   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3509   gint index;
3510   GtkIconViewItem *item;
3511   GList *list, *next;
3512   gboolean emit = FALSE;
3513   GtkTreeIter iter;
3514 
3515   /* ignore changes in branches */
3516   if (gtk_tree_path_get_depth (path) > 1)
3517     return;
3518 
3519   if (gtk_tree_model_get_iter (model, &iter, path))
3520     gtk_tree_model_unref_node (model, &iter);
3521 
3522   index = gtk_tree_path_get_indices(path)[0];
3523 
3524   list = g_list_nth (icon_view->priv->items, index);
3525   item = list->data;
3526 
3527   if (icon_view->priv->cell_area)
3528     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3529 
3530   if (item == icon_view->priv->anchor_item)
3531     icon_view->priv->anchor_item = NULL;
3532 
3533   if (item == icon_view->priv->cursor_item)
3534     icon_view->priv->cursor_item = NULL;
3535 
3536   if (item == icon_view->priv->last_prelight)
3537     icon_view->priv->last_prelight = NULL;
3538 
3539   if (item->selected)
3540     emit = TRUE;
3541 
3542   gtk_icon_view_item_free (item);
3543 
3544   for (next = list->next; next; next = next->next)
3545     {
3546       item = next->data;
3547 
3548       item->index--;
3549     }
3550 
3551   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3552 
3553   verify_items (icon_view);
3554 
3555   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3556 
3557   if (emit)
3558     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3559 }
3560 
3561 static void
gtk_icon_view_rows_reordered(GtkTreeModel * model,GtkTreePath * parent,GtkTreeIter * iter,gint * new_order,gpointer data)3562 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3563 			      GtkTreePath  *parent,
3564 			      GtkTreeIter  *iter,
3565 			      gint         *new_order,
3566 			      gpointer      data)
3567 {
3568   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3569   int i;
3570   int length;
3571   GList *items = NULL, *list;
3572   GtkIconViewItem **item_array;
3573   gint *order;
3574 
3575   /* ignore changes in branches */
3576   if (iter != NULL)
3577     return;
3578 
3579   if (icon_view->priv->cell_area)
3580     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3581 
3582   length = gtk_tree_model_iter_n_children (model, NULL);
3583 
3584   order = g_new (gint, length);
3585   for (i = 0; i < length; i++)
3586     order [new_order[i]] = i;
3587 
3588   item_array = g_new (GtkIconViewItem *, length);
3589   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3590     item_array[order[i]] = list->data;
3591   g_free (order);
3592 
3593   for (i = length - 1; i >= 0; i--)
3594     {
3595       item_array[i]->index = i;
3596       items = g_list_prepend (items, item_array[i]);
3597     }
3598 
3599   g_free (item_array);
3600   g_list_free (icon_view->priv->items);
3601   icon_view->priv->items = items;
3602 
3603   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3604 
3605   verify_items (icon_view);
3606 }
3607 
3608 static void
gtk_icon_view_build_items(GtkIconView * icon_view)3609 gtk_icon_view_build_items (GtkIconView *icon_view)
3610 {
3611   GtkTreeIter iter;
3612   int i;
3613   GList *items = NULL;
3614 
3615   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3616 				      &iter))
3617     return;
3618 
3619   i = 0;
3620 
3621   do
3622     {
3623       GtkIconViewItem *item = gtk_icon_view_item_new ();
3624 
3625       item->index = i;
3626 
3627       i++;
3628 
3629       items = g_list_prepend (items, item);
3630 
3631     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3632 
3633   icon_view->priv->items = g_list_reverse (items);
3634 }
3635 
3636 static void
gtk_icon_view_add_move_binding(GtkBindingSet * binding_set,guint keyval,guint modmask,GtkMovementStep step,gint count)3637 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
3638 				guint           keyval,
3639 				guint           modmask,
3640 				GtkMovementStep step,
3641 				gint            count)
3642 {
3643 
3644   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3645                                 I_("move-cursor"), 2,
3646                                 G_TYPE_ENUM, step,
3647                                 G_TYPE_INT, count);
3648 
3649   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3650                                 "move-cursor", 2,
3651                                 G_TYPE_ENUM, step,
3652                                 G_TYPE_INT, count);
3653 
3654   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3655    return;
3656 
3657   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3658                                 "move-cursor", 2,
3659                                 G_TYPE_ENUM, step,
3660                                 G_TYPE_INT, count);
3661 
3662   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3663                                 "move-cursor", 2,
3664                                 G_TYPE_ENUM, step,
3665                                 G_TYPE_INT, count);
3666 }
3667 
3668 static gboolean
gtk_icon_view_real_move_cursor(GtkIconView * icon_view,GtkMovementStep step,gint count)3669 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
3670 				GtkMovementStep  step,
3671 				gint             count)
3672 {
3673   GdkModifierType state;
3674 
3675   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3676   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3677 			step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3678 			step == GTK_MOVEMENT_DISPLAY_LINES ||
3679 			step == GTK_MOVEMENT_PAGES ||
3680 			step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3681 
3682   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3683     return FALSE;
3684 
3685   gtk_cell_area_stop_editing (icon_view->priv->cell_area, FALSE);
3686   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3687 
3688   if (gtk_get_current_event_state (&state))
3689     {
3690       GdkModifierType extend_mod_mask;
3691       GdkModifierType modify_mod_mask;
3692 
3693       extend_mod_mask =
3694         gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3695                                       GDK_MODIFIER_INTENT_EXTEND_SELECTION);
3696       modify_mod_mask =
3697         gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3698                                       GDK_MODIFIER_INTENT_MODIFY_SELECTION);
3699 
3700       if ((state & modify_mod_mask) == modify_mod_mask)
3701         icon_view->priv->modify_selection_pressed = TRUE;
3702       if ((state & extend_mod_mask) == extend_mod_mask)
3703         icon_view->priv->extend_selection_pressed = TRUE;
3704     }
3705   /* else we assume not pressed */
3706 
3707   switch (step)
3708     {
3709     case GTK_MOVEMENT_LOGICAL_POSITIONS:
3710     case GTK_MOVEMENT_VISUAL_POSITIONS:
3711       gtk_icon_view_move_cursor_left_right (icon_view, count);
3712       break;
3713     case GTK_MOVEMENT_DISPLAY_LINES:
3714       gtk_icon_view_move_cursor_up_down (icon_view, count);
3715       break;
3716     case GTK_MOVEMENT_PAGES:
3717       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3718       break;
3719     case GTK_MOVEMENT_BUFFER_ENDS:
3720       gtk_icon_view_move_cursor_start_end (icon_view, count);
3721       break;
3722     default:
3723       g_assert_not_reached ();
3724     }
3725 
3726   icon_view->priv->modify_selection_pressed = FALSE;
3727   icon_view->priv->extend_selection_pressed = FALSE;
3728 
3729   icon_view->priv->draw_focus = TRUE;
3730 
3731   return TRUE;
3732 }
3733 
3734 static GtkIconViewItem *
find_item(GtkIconView * icon_view,GtkIconViewItem * current,gint row_ofs,gint col_ofs)3735 find_item (GtkIconView     *icon_view,
3736 	   GtkIconViewItem *current,
3737 	   gint             row_ofs,
3738 	   gint             col_ofs)
3739 {
3740   gint row, col;
3741   GList *items;
3742   GtkIconViewItem *item;
3743 
3744   /* FIXME: this could be more efficient
3745    */
3746   row = current->row + row_ofs;
3747   col = current->col + col_ofs;
3748 
3749   for (items = icon_view->priv->items; items; items = items->next)
3750     {
3751       item = items->data;
3752       if (item->row == row && item->col == col)
3753 	return item;
3754     }
3755 
3756   return NULL;
3757 }
3758 
3759 static GtkIconViewItem *
find_item_page_up_down(GtkIconView * icon_view,GtkIconViewItem * current,gint count)3760 find_item_page_up_down (GtkIconView     *icon_view,
3761 			GtkIconViewItem *current,
3762 			gint             count)
3763 {
3764   GList *item, *next;
3765   gint y, col;
3766 
3767   col = current->col;
3768   y = current->cell_area.y + count * gtk_adjustment_get_page_size (icon_view->priv->vadjustment);
3769 
3770   item = g_list_find (icon_view->priv->items, current);
3771   if (count > 0)
3772     {
3773       while (item)
3774 	{
3775 	  for (next = item->next; next; next = next->next)
3776 	    {
3777 	      if (((GtkIconViewItem *)next->data)->col == col)
3778 		break;
3779 	    }
3780 	  if (!next || ((GtkIconViewItem *)next->data)->cell_area.y > y)
3781 	    break;
3782 
3783 	  item = next;
3784 	}
3785     }
3786   else
3787     {
3788       while (item)
3789 	{
3790 	  for (next = item->prev; next; next = next->prev)
3791 	    {
3792 	      if (((GtkIconViewItem *)next->data)->col == col)
3793 		break;
3794 	    }
3795 	  if (!next || ((GtkIconViewItem *)next->data)->cell_area.y < y)
3796 	    break;
3797 
3798 	  item = next;
3799 	}
3800     }
3801 
3802   if (item)
3803     return item->data;
3804 
3805   return NULL;
3806 }
3807 
3808 static gboolean
gtk_icon_view_select_all_between(GtkIconView * icon_view,GtkIconViewItem * anchor,GtkIconViewItem * cursor)3809 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
3810 				  GtkIconViewItem *anchor,
3811 				  GtkIconViewItem *cursor)
3812 {
3813   GList *items;
3814   GtkIconViewItem *item;
3815   gint row1, row2, col1, col2;
3816   gboolean dirty = FALSE;
3817 
3818   if (anchor->row < cursor->row)
3819     {
3820       row1 = anchor->row;
3821       row2 = cursor->row;
3822     }
3823   else
3824     {
3825       row1 = cursor->row;
3826       row2 = anchor->row;
3827     }
3828 
3829   if (anchor->col < cursor->col)
3830     {
3831       col1 = anchor->col;
3832       col2 = cursor->col;
3833     }
3834   else
3835     {
3836       col1 = cursor->col;
3837       col2 = anchor->col;
3838     }
3839 
3840   for (items = icon_view->priv->items; items; items = items->next)
3841     {
3842       item = items->data;
3843 
3844       if (row1 <= item->row && item->row <= row2 &&
3845 	  col1 <= item->col && item->col <= col2)
3846 	{
3847 	  if (!item->selected)
3848 	    {
3849 	      dirty = TRUE;
3850 	      item->selected = TRUE;
3851 	      gtk_icon_view_item_selected_changed (icon_view, item);
3852 	    }
3853 	  gtk_icon_view_queue_draw_item (icon_view, item);
3854 	}
3855     }
3856 
3857   return dirty;
3858 }
3859 
3860 static void
gtk_icon_view_move_cursor_up_down(GtkIconView * icon_view,gint count)3861 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3862 				   gint         count)
3863 {
3864   GtkIconViewItem *item;
3865   GtkCellRenderer *cell = NULL;
3866   gboolean dirty = FALSE;
3867   gint step;
3868   GtkDirectionType direction;
3869 
3870   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3871     return;
3872 
3873   direction = count < 0 ? GTK_DIR_UP : GTK_DIR_DOWN;
3874 
3875   if (!icon_view->priv->cursor_item)
3876     {
3877       GList *list;
3878 
3879       if (count > 0)
3880 	list = icon_view->priv->items;
3881       else
3882 	list = g_list_last (icon_view->priv->items);
3883 
3884       if (list)
3885         {
3886           item = list->data;
3887 
3888           /* Give focus to the first cell initially */
3889           _gtk_icon_view_set_cell_data (icon_view, item);
3890           gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3891         }
3892       else
3893         {
3894           item = NULL;
3895         }
3896     }
3897   else
3898     {
3899       item = icon_view->priv->cursor_item;
3900       step = count > 0 ? 1 : -1;
3901 
3902       /* Save the current focus cell in case we hit the edge */
3903       cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3904 
3905       while (item)
3906 	{
3907 	  _gtk_icon_view_set_cell_data (icon_view, item);
3908 
3909 	  if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3910 	    break;
3911 
3912 	  item = find_item (icon_view, item, step, 0);
3913 	}
3914     }
3915 
3916   if (!item)
3917     {
3918       if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3919         {
3920           GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3921           if (toplevel)
3922             gtk_widget_child_focus (toplevel,
3923                                     direction == GTK_DIR_UP ?
3924                                     GTK_DIR_TAB_BACKWARD :
3925                                     GTK_DIR_TAB_FORWARD);
3926 
3927         }
3928 
3929       gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3930       return;
3931     }
3932 
3933   if (icon_view->priv->modify_selection_pressed ||
3934       !icon_view->priv->extend_selection_pressed ||
3935       !icon_view->priv->anchor_item ||
3936       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3937     icon_view->priv->anchor_item = item;
3938 
3939   cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3940   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3941 
3942   if (!icon_view->priv->modify_selection_pressed &&
3943       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3944     {
3945       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3946       dirty = gtk_icon_view_select_all_between (icon_view,
3947 						icon_view->priv->anchor_item,
3948 						item) || dirty;
3949     }
3950 
3951   gtk_icon_view_scroll_to_item (icon_view, item);
3952 
3953   if (dirty)
3954     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3955 }
3956 
3957 static void
gtk_icon_view_move_cursor_page_up_down(GtkIconView * icon_view,gint count)3958 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3959 					gint         count)
3960 {
3961   GtkIconViewItem *item;
3962   gboolean dirty = FALSE;
3963 
3964   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3965     return;
3966 
3967   if (!icon_view->priv->cursor_item)
3968     {
3969       GList *list;
3970 
3971       if (count > 0)
3972 	list = icon_view->priv->items;
3973       else
3974 	list = g_list_last (icon_view->priv->items);
3975 
3976       item = list ? list->data : NULL;
3977     }
3978   else
3979     item = find_item_page_up_down (icon_view,
3980 				   icon_view->priv->cursor_item,
3981 				   count);
3982 
3983   if (item == icon_view->priv->cursor_item)
3984     gtk_widget_error_bell (GTK_WIDGET (icon_view));
3985 
3986   if (!item)
3987     return;
3988 
3989   if (icon_view->priv->modify_selection_pressed ||
3990       !icon_view->priv->extend_selection_pressed ||
3991       !icon_view->priv->anchor_item ||
3992       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3993     icon_view->priv->anchor_item = item;
3994 
3995   _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
3996 
3997   if (!icon_view->priv->modify_selection_pressed &&
3998       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3999     {
4000       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4001       dirty = gtk_icon_view_select_all_between (icon_view,
4002 						icon_view->priv->anchor_item,
4003 						item) || dirty;
4004     }
4005 
4006   gtk_icon_view_scroll_to_item (icon_view, item);
4007 
4008   if (dirty)
4009     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4010 }
4011 
4012 static void
gtk_icon_view_move_cursor_left_right(GtkIconView * icon_view,gint count)4013 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
4014 				      gint         count)
4015 {
4016   GtkIconViewItem *item;
4017   GtkCellRenderer *cell = NULL;
4018   gboolean dirty = FALSE;
4019   gint step;
4020   GtkDirectionType direction;
4021 
4022   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
4023     return;
4024 
4025   direction = count < 0 ? GTK_DIR_LEFT : GTK_DIR_RIGHT;
4026 
4027   if (!icon_view->priv->cursor_item)
4028     {
4029       GList *list;
4030 
4031       if (count > 0)
4032 	list = icon_view->priv->items;
4033       else
4034 	list = g_list_last (icon_view->priv->items);
4035 
4036       if (list)
4037         {
4038           item = list->data;
4039 
4040           /* Give focus to the first cell initially */
4041           _gtk_icon_view_set_cell_data (icon_view, item);
4042           gtk_cell_area_focus (icon_view->priv->cell_area, direction);
4043         }
4044       else
4045         {
4046           item = NULL;
4047         }
4048     }
4049   else
4050     {
4051       item = icon_view->priv->cursor_item;
4052       step = count > 0 ? 1 : -1;
4053 
4054       /* Save the current focus cell in case we hit the edge */
4055       cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
4056 
4057       while (item)
4058 	{
4059 	  _gtk_icon_view_set_cell_data (icon_view, item);
4060 
4061 	  if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
4062 	    break;
4063 
4064 	  item = find_item (icon_view, item, 0, step);
4065 	}
4066     }
4067 
4068   if (!item)
4069     {
4070       if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
4071         {
4072           GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
4073           if (toplevel)
4074             gtk_widget_child_focus (toplevel,
4075                                     direction == GTK_DIR_LEFT ?
4076                                     GTK_DIR_TAB_BACKWARD :
4077                                     GTK_DIR_TAB_FORWARD);
4078 
4079         }
4080 
4081       gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
4082       return;
4083     }
4084 
4085   if (icon_view->priv->modify_selection_pressed ||
4086       !icon_view->priv->extend_selection_pressed ||
4087       !icon_view->priv->anchor_item ||
4088       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4089     icon_view->priv->anchor_item = item;
4090 
4091   cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
4092   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
4093 
4094   if (!icon_view->priv->modify_selection_pressed &&
4095       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4096     {
4097       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4098       dirty = gtk_icon_view_select_all_between (icon_view,
4099 						icon_view->priv->anchor_item,
4100 						item) || dirty;
4101     }
4102 
4103   gtk_icon_view_scroll_to_item (icon_view, item);
4104 
4105   if (dirty)
4106     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4107 }
4108 
4109 static void
gtk_icon_view_move_cursor_start_end(GtkIconView * icon_view,gint count)4110 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
4111 				     gint         count)
4112 {
4113   GtkIconViewItem *item;
4114   GList *list;
4115   gboolean dirty = FALSE;
4116 
4117   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
4118     return;
4119 
4120   if (count < 0)
4121     list = icon_view->priv->items;
4122   else
4123     list = g_list_last (icon_view->priv->items);
4124 
4125   item = list ? list->data : NULL;
4126 
4127   if (item == icon_view->priv->cursor_item)
4128     gtk_widget_error_bell (GTK_WIDGET (icon_view));
4129 
4130   if (!item)
4131     return;
4132 
4133   if (icon_view->priv->modify_selection_pressed ||
4134       !icon_view->priv->extend_selection_pressed ||
4135       !icon_view->priv->anchor_item ||
4136       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4137     icon_view->priv->anchor_item = item;
4138 
4139   _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
4140 
4141   if (!icon_view->priv->modify_selection_pressed &&
4142       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4143     {
4144       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4145       dirty = gtk_icon_view_select_all_between (icon_view,
4146 						icon_view->priv->anchor_item,
4147 						item) || dirty;
4148     }
4149 
4150   gtk_icon_view_scroll_to_item (icon_view, item);
4151 
4152   if (dirty)
4153     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4154 }
4155 
4156 /**
4157  * gtk_icon_view_scroll_to_path:
4158  * @icon_view: A #GtkIconView.
4159  * @path: The path of the item to move to.
4160  * @use_align: whether to use alignment arguments, or %FALSE.
4161  * @row_align: The vertical alignment of the item specified by @path.
4162  * @col_align: The horizontal alignment of the item specified by @path.
4163  *
4164  * Moves the alignments of @icon_view to the position specified by @path.
4165  * @row_align determines where the row is placed, and @col_align determines
4166  * where @column is placed.  Both are expected to be between 0.0 and 1.0.
4167  * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means
4168  * center.
4169  *
4170  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
4171  * tree does the minimum amount of work to scroll the item onto the screen.
4172  * This means that the item will be scrolled to the edge closest to its current
4173  * position.  If the item is currently visible on the screen, nothing is done.
4174  *
4175  * This function only works if the model is set, and @path is a valid row on
4176  * the model. If the model changes before the @icon_view is realized, the
4177  * centered path will be modified to reflect this change.
4178  *
4179  * Since: 2.8
4180  **/
4181 void
gtk_icon_view_scroll_to_path(GtkIconView * icon_view,GtkTreePath * path,gboolean use_align,gfloat row_align,gfloat col_align)4182 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
4183 			      GtkTreePath *path,
4184 			      gboolean     use_align,
4185 			      gfloat       row_align,
4186 			      gfloat       col_align)
4187 {
4188   GtkIconViewItem *item = NULL;
4189   GtkWidget *widget;
4190 
4191   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4192   g_return_if_fail (path != NULL);
4193   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
4194   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
4195 
4196   widget = GTK_WIDGET (icon_view);
4197 
4198   if (gtk_tree_path_get_depth (path) > 0)
4199     item = g_list_nth_data (icon_view->priv->items,
4200 			    gtk_tree_path_get_indices(path)[0]);
4201 
4202   if (!item || item->cell_area.width < 0 ||
4203       !gtk_widget_get_realized (widget))
4204     {
4205       if (icon_view->priv->scroll_to_path)
4206 	gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4207 
4208       icon_view->priv->scroll_to_path = NULL;
4209 
4210       if (path)
4211 	icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
4212 
4213       icon_view->priv->scroll_to_use_align = use_align;
4214       icon_view->priv->scroll_to_row_align = row_align;
4215       icon_view->priv->scroll_to_col_align = col_align;
4216 
4217       return;
4218     }
4219 
4220   if (use_align)
4221     {
4222       GtkAllocation allocation;
4223       gint x, y;
4224       gfloat offset;
4225       GdkRectangle item_area =
4226 	{
4227 	  item->cell_area.x - icon_view->priv->item_padding,
4228 	  item->cell_area.y - icon_view->priv->item_padding,
4229 	  item->cell_area.width  + icon_view->priv->item_padding * 2,
4230 	  item->cell_area.height + icon_view->priv->item_padding * 2
4231 	};
4232 
4233       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4234 
4235       gtk_widget_get_allocation (widget, &allocation);
4236 
4237       offset = y + item_area.y - row_align * (allocation.height - item_area.height);
4238 
4239       gtk_adjustment_set_value (icon_view->priv->vadjustment,
4240                                 gtk_adjustment_get_value (icon_view->priv->vadjustment) + offset);
4241 
4242       offset = x + item_area.x - col_align * (allocation.width - item_area.width);
4243 
4244       gtk_adjustment_set_value (icon_view->priv->hadjustment,
4245                                 gtk_adjustment_get_value (icon_view->priv->hadjustment) + offset);
4246     }
4247   else
4248     gtk_icon_view_scroll_to_item (icon_view, item);
4249 }
4250 
4251 
4252 static void
gtk_icon_view_scroll_to_item(GtkIconView * icon_view,GtkIconViewItem * item)4253 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view,
4254                               GtkIconViewItem *item)
4255 {
4256   GtkIconViewPrivate *priv = icon_view->priv;
4257   GtkWidget *widget = GTK_WIDGET (icon_view);
4258   GtkAdjustment *hadj, *vadj;
4259   GtkAllocation allocation;
4260   gint x, y;
4261   GdkRectangle item_area;
4262 
4263   item_area.x = item->cell_area.x - priv->item_padding;
4264   item_area.y = item->cell_area.y - priv->item_padding;
4265   item_area.width = item->cell_area.width  + priv->item_padding * 2;
4266   item_area.height = item->cell_area.height + priv->item_padding * 2;
4267 
4268   gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4269   gtk_widget_get_allocation (widget, &allocation);
4270 
4271   hadj = icon_view->priv->hadjustment;
4272   vadj = icon_view->priv->vadjustment;
4273 
4274   if (y + item_area.y < 0)
4275     gtk_adjustment_animate_to_value (vadj,
4276                                      gtk_adjustment_get_value (vadj)
4277                                      + y + item_area.y);
4278   else if (y + item_area.y + item_area.height > allocation.height)
4279     gtk_adjustment_animate_to_value (vadj,
4280                                      gtk_adjustment_get_value (vadj)
4281                                      + y + item_area.y + item_area.height - allocation.height);
4282 
4283   if (x + item_area.x < 0)
4284     gtk_adjustment_animate_to_value (hadj,
4285                                      gtk_adjustment_get_value (hadj)
4286                                      + x + item_area.x);
4287   else if (x + item_area.x + item_area.width > allocation.width)
4288     gtk_adjustment_animate_to_value (hadj,
4289                                      gtk_adjustment_get_value (hadj)
4290                                      + x + item_area.x + item_area.width - allocation.width);
4291 }
4292 
4293 /* GtkCellLayout implementation */
4294 
4295 static void
gtk_icon_view_ensure_cell_area(GtkIconView * icon_view,GtkCellArea * cell_area)4296 gtk_icon_view_ensure_cell_area (GtkIconView *icon_view,
4297                                 GtkCellArea *cell_area)
4298 {
4299   GtkIconViewPrivate *priv = icon_view->priv;
4300 
4301   if (priv->cell_area)
4302     return;
4303 
4304   if (cell_area)
4305     priv->cell_area = cell_area;
4306   else
4307     priv->cell_area = gtk_cell_area_box_new ();
4308 
4309   g_object_ref_sink (priv->cell_area);
4310 
4311   if (GTK_IS_ORIENTABLE (priv->cell_area))
4312     gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->cell_area), priv->item_orientation);
4313 
4314   priv->cell_area_context = gtk_cell_area_create_context (priv->cell_area);
4315 
4316   priv->add_editable_id =
4317     g_signal_connect (priv->cell_area, "add-editable",
4318                       G_CALLBACK (gtk_icon_view_add_editable), icon_view);
4319   priv->remove_editable_id =
4320     g_signal_connect (priv->cell_area, "remove-editable",
4321                       G_CALLBACK (gtk_icon_view_remove_editable), icon_view);
4322 
4323   update_text_cell (icon_view);
4324   update_pixbuf_cell (icon_view);
4325 }
4326 
4327 static GtkCellArea *
gtk_icon_view_cell_layout_get_area(GtkCellLayout * cell_layout)4328 gtk_icon_view_cell_layout_get_area (GtkCellLayout *cell_layout)
4329 {
4330   GtkIconView *icon_view = GTK_ICON_VIEW (cell_layout);
4331   GtkIconViewPrivate *priv = icon_view->priv;
4332 
4333   if (G_UNLIKELY (!priv->cell_area))
4334     gtk_icon_view_ensure_cell_area (icon_view, NULL);
4335 
4336   return icon_view->priv->cell_area;
4337 }
4338 
4339 void
_gtk_icon_view_set_cell_data(GtkIconView * icon_view,GtkIconViewItem * item)4340 _gtk_icon_view_set_cell_data (GtkIconView     *icon_view,
4341 			      GtkIconViewItem *item)
4342 {
4343   GtkTreeIter iter;
4344   GtkTreePath *path;
4345 
4346   path = gtk_tree_path_new_from_indices (item->index, -1);
4347   if (!gtk_tree_model_get_iter (icon_view->priv->model, &iter, path))
4348     return;
4349   gtk_tree_path_free (path);
4350 
4351   gtk_cell_area_apply_attributes (icon_view->priv->cell_area,
4352 				  icon_view->priv->model,
4353 				  &iter, FALSE, FALSE);
4354 }
4355 
4356 
4357 
4358 /* Public API */
4359 
4360 
4361 /**
4362  * gtk_icon_view_new:
4363  *
4364  * Creates a new #GtkIconView widget
4365  *
4366  * Returns: A newly created #GtkIconView widget
4367  *
4368  * Since: 2.6
4369  **/
4370 GtkWidget *
gtk_icon_view_new(void)4371 gtk_icon_view_new (void)
4372 {
4373   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4374 }
4375 
4376 /**
4377  * gtk_icon_view_new_with_area:
4378  * @area: the #GtkCellArea to use to layout cells
4379  *
4380  * Creates a new #GtkIconView widget using the
4381  * specified @area to layout cells inside the icons.
4382  *
4383  * Returns: A newly created #GtkIconView widget
4384  *
4385  * Since: 3.0
4386  **/
4387 GtkWidget *
gtk_icon_view_new_with_area(GtkCellArea * area)4388 gtk_icon_view_new_with_area (GtkCellArea *area)
4389 {
4390   return g_object_new (GTK_TYPE_ICON_VIEW, "cell-area", area, NULL);
4391 }
4392 
4393 /**
4394  * gtk_icon_view_new_with_model:
4395  * @model: The model.
4396  *
4397  * Creates a new #GtkIconView widget with the model @model.
4398  *
4399  * Returns: A newly created #GtkIconView widget.
4400  *
4401  * Since: 2.6
4402  **/
4403 GtkWidget *
gtk_icon_view_new_with_model(GtkTreeModel * model)4404 gtk_icon_view_new_with_model (GtkTreeModel *model)
4405 {
4406   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4407 }
4408 
4409 /**
4410  * gtk_icon_view_convert_widget_to_bin_window_coords:
4411  * @icon_view: a #GtkIconView
4412  * @wx: X coordinate relative to the widget
4413  * @wy: Y coordinate relative to the widget
4414  * @bx: (out): return location for bin_window X coordinate
4415  * @by: (out): return location for bin_window Y coordinate
4416  *
4417  * Converts widget coordinates to coordinates for the bin_window,
4418  * as expected by e.g. gtk_icon_view_get_path_at_pos().
4419  *
4420  * Since: 2.12
4421  */
4422 void
gtk_icon_view_convert_widget_to_bin_window_coords(GtkIconView * icon_view,gint wx,gint wy,gint * bx,gint * by)4423 gtk_icon_view_convert_widget_to_bin_window_coords (GtkIconView *icon_view,
4424                                                    gint         wx,
4425                                                    gint         wy,
4426                                                    gint        *bx,
4427                                                    gint        *by)
4428 {
4429   gint x, y;
4430 
4431   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4432 
4433   if (icon_view->priv->bin_window)
4434     gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4435   else
4436     x = y = 0;
4437 
4438   if (bx)
4439     *bx = wx - x;
4440   if (by)
4441     *by = wy - y;
4442 }
4443 
4444 /**
4445  * gtk_icon_view_get_path_at_pos:
4446  * @icon_view: A #GtkIconView.
4447  * @x: The x position to be identified
4448  * @y: The y position to be identified
4449  *
4450  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4451  * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4452  * the cell at the specified position.
4453  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4454  * widget coordinates to bin_window coordinates.
4455  *
4456  * Returns: (nullable) (transfer full): The #GtkTreePath corresponding
4457  * to the icon or %NULL if no icon exists at that position.
4458  *
4459  * Since: 2.6
4460  **/
4461 GtkTreePath *
gtk_icon_view_get_path_at_pos(GtkIconView * icon_view,gint x,gint y)4462 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4463 			       gint         x,
4464 			       gint         y)
4465 {
4466   GtkIconViewItem *item;
4467   GtkTreePath *path;
4468 
4469   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4470 
4471   item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4472 
4473   if (!item)
4474     return NULL;
4475 
4476   path = gtk_tree_path_new_from_indices (item->index, -1);
4477 
4478   return path;
4479 }
4480 
4481 /**
4482  * gtk_icon_view_get_item_at_pos:
4483  * @icon_view: A #GtkIconView.
4484  * @x: The x position to be identified
4485  * @y: The y position to be identified
4486  * @path: (out) (allow-none): Return location for the path, or %NULL
4487  * @cell: (out) (allow-none) (transfer none): Return location for the renderer
4488  *   responsible for the cell at (@x, @y), or %NULL
4489  *
4490  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4491  * In contrast to gtk_icon_view_get_path_at_pos(), this function also
4492  * obtains the cell at the specified position. The returned path should
4493  * be freed with gtk_tree_path_free().
4494  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4495  * widget coordinates to bin_window coordinates.
4496  *
4497  * Returns: %TRUE if an item exists at the specified position
4498  *
4499  * Since: 2.8
4500  **/
4501 gboolean
gtk_icon_view_get_item_at_pos(GtkIconView * icon_view,gint x,gint y,GtkTreePath ** path,GtkCellRenderer ** cell)4502 gtk_icon_view_get_item_at_pos (GtkIconView      *icon_view,
4503 			       gint              x,
4504 			       gint              y,
4505 			       GtkTreePath     **path,
4506 			       GtkCellRenderer **cell)
4507 {
4508   GtkIconViewItem *item;
4509   GtkCellRenderer *renderer = NULL;
4510 
4511   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4512 
4513   item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &renderer);
4514 
4515   if (path != NULL)
4516     {
4517       if (item != NULL)
4518 	*path = gtk_tree_path_new_from_indices (item->index, -1);
4519       else
4520 	*path = NULL;
4521     }
4522 
4523   if (cell != NULL)
4524     *cell = renderer;
4525 
4526   return (item != NULL);
4527 }
4528 
4529 /**
4530  * gtk_icon_view_get_cell_rect:
4531  * @icon_view: a #GtkIconView
4532  * @path: a #GtkTreePath
4533  * @cell: (allow-none): a #GtkCellRenderer or %NULL
4534  * @rect: (out): rectangle to fill with cell rect
4535  *
4536  * Fills the bounding rectangle in widget coordinates for the cell specified by
4537  * @path and @cell. If @cell is %NULL the main cell area is used.
4538  *
4539  * This function is only valid if @icon_view is realized.
4540  *
4541  * Returns: %FALSE if there is no such item, %TRUE otherwise
4542  *
4543  * Since: 3.6
4544  */
4545 gboolean
gtk_icon_view_get_cell_rect(GtkIconView * icon_view,GtkTreePath * path,GtkCellRenderer * cell,GdkRectangle * rect)4546 gtk_icon_view_get_cell_rect (GtkIconView     *icon_view,
4547                              GtkTreePath     *path,
4548                              GtkCellRenderer *cell,
4549                              GdkRectangle    *rect)
4550 {
4551   GtkIconViewItem *item = NULL;
4552   gint x, y;
4553 
4554   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4555   g_return_val_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell), FALSE);
4556 
4557   if (gtk_tree_path_get_depth (path) > 0)
4558     item = g_list_nth_data (icon_view->priv->items,
4559 			    gtk_tree_path_get_indices(path)[0]);
4560 
4561   if (!item)
4562     return FALSE;
4563 
4564   if (cell)
4565     {
4566       GtkCellAreaContext *context;
4567 
4568       context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
4569       _gtk_icon_view_set_cell_data (icon_view, item);
4570       gtk_cell_area_get_cell_allocation (icon_view->priv->cell_area, context,
4571 					 GTK_WIDGET (icon_view),
4572 					 cell, &item->cell_area, rect);
4573     }
4574   else
4575     {
4576       rect->x = item->cell_area.x - icon_view->priv->item_padding;
4577       rect->y = item->cell_area.y - icon_view->priv->item_padding;
4578       rect->width  = item->cell_area.width  + icon_view->priv->item_padding * 2;
4579       rect->height = item->cell_area.height + icon_view->priv->item_padding * 2;
4580     }
4581 
4582   if (icon_view->priv->bin_window)
4583     {
4584       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4585       rect->x += x;
4586       rect->y += y;
4587     }
4588 
4589   return TRUE;
4590 }
4591 
4592 /**
4593  * gtk_icon_view_set_tooltip_item:
4594  * @icon_view: a #GtkIconView
4595  * @tooltip: a #GtkTooltip
4596  * @path: a #GtkTreePath
4597  *
4598  * Sets the tip area of @tooltip to be the area covered by the item at @path.
4599  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4600  * See also gtk_tooltip_set_tip_area().
4601  *
4602  * Since: 2.12
4603  */
4604 void
gtk_icon_view_set_tooltip_item(GtkIconView * icon_view,GtkTooltip * tooltip,GtkTreePath * path)4605 gtk_icon_view_set_tooltip_item (GtkIconView     *icon_view,
4606                                 GtkTooltip      *tooltip,
4607                                 GtkTreePath     *path)
4608 {
4609   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4610   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4611 
4612   gtk_icon_view_set_tooltip_cell (icon_view, tooltip, path, NULL);
4613 }
4614 
4615 /**
4616  * gtk_icon_view_set_tooltip_cell:
4617  * @icon_view: a #GtkIconView
4618  * @tooltip: a #GtkTooltip
4619  * @path: a #GtkTreePath
4620  * @cell: (allow-none): a #GtkCellRenderer or %NULL
4621  *
4622  * Sets the tip area of @tooltip to the area which @cell occupies in
4623  * the item pointed to by @path. See also gtk_tooltip_set_tip_area().
4624  *
4625  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4626  *
4627  * Since: 2.12
4628  */
4629 void
gtk_icon_view_set_tooltip_cell(GtkIconView * icon_view,GtkTooltip * tooltip,GtkTreePath * path,GtkCellRenderer * cell)4630 gtk_icon_view_set_tooltip_cell (GtkIconView     *icon_view,
4631                                 GtkTooltip      *tooltip,
4632                                 GtkTreePath     *path,
4633                                 GtkCellRenderer *cell)
4634 {
4635   GdkRectangle rect;
4636 
4637   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4638   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4639   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
4640 
4641   if (!gtk_icon_view_get_cell_rect (icon_view, path, cell, &rect))
4642     return;
4643 
4644   gtk_tooltip_set_tip_area (tooltip, &rect);
4645 }
4646 
4647 
4648 /**
4649  * gtk_icon_view_get_tooltip_context:
4650  * @icon_view: an #GtkIconView
4651  * @x: (inout): the x coordinate (relative to widget coordinates)
4652  * @y: (inout): the y coordinate (relative to widget coordinates)
4653  * @keyboard_tip: whether this is a keyboard tooltip or not
4654  * @model: (out) (allow-none) (transfer none): a pointer to receive a
4655  *         #GtkTreeModel or %NULL
4656  * @path: (out) (allow-none): a pointer to receive a #GtkTreePath or %NULL
4657  * @iter: (out) (allow-none): a pointer to receive a #GtkTreeIter or %NULL
4658  *
4659  * This function is supposed to be used in a #GtkWidget::query-tooltip
4660  * signal handler for #GtkIconView.  The @x, @y and @keyboard_tip values
4661  * which are received in the signal handler, should be passed to this
4662  * function without modification.
4663  *
4664  * The return value indicates whether there is an icon view item at the given
4665  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips. For keyboard
4666  * tooltips the item returned will be the cursor item. When %TRUE, then any of
4667  * @model, @path and @iter which have been provided will be set to point to
4668  * that row and the corresponding model. @x and @y will always be converted
4669  * to be relative to @icon_view’s bin_window if @keyboard_tooltip is %FALSE.
4670  *
4671  * Returns: whether or not the given tooltip context points to a item
4672  *
4673  * Since: 2.12
4674  */
4675 gboolean
gtk_icon_view_get_tooltip_context(GtkIconView * icon_view,gint * x,gint * y,gboolean keyboard_tip,GtkTreeModel ** model,GtkTreePath ** path,GtkTreeIter * iter)4676 gtk_icon_view_get_tooltip_context (GtkIconView   *icon_view,
4677                                    gint          *x,
4678                                    gint          *y,
4679                                    gboolean       keyboard_tip,
4680                                    GtkTreeModel **model,
4681                                    GtkTreePath  **path,
4682                                    GtkTreeIter   *iter)
4683 {
4684   GtkTreePath *tmppath = NULL;
4685 
4686   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4687   g_return_val_if_fail (x != NULL, FALSE);
4688   g_return_val_if_fail (y != NULL, FALSE);
4689 
4690   if (keyboard_tip)
4691     {
4692       gtk_icon_view_get_cursor (icon_view, &tmppath, NULL);
4693 
4694       if (!tmppath)
4695         return FALSE;
4696     }
4697   else
4698     {
4699       gtk_icon_view_convert_widget_to_bin_window_coords (icon_view, *x, *y,
4700                                                          x, y);
4701 
4702       if (!gtk_icon_view_get_item_at_pos (icon_view, *x, *y, &tmppath, NULL))
4703         return FALSE;
4704     }
4705 
4706   if (model)
4707     *model = gtk_icon_view_get_model (icon_view);
4708 
4709   if (iter)
4710     gtk_tree_model_get_iter (gtk_icon_view_get_model (icon_view),
4711                              iter, tmppath);
4712 
4713   if (path)
4714     *path = tmppath;
4715   else
4716     gtk_tree_path_free (tmppath);
4717 
4718   return TRUE;
4719 }
4720 
4721 static gboolean
gtk_icon_view_set_tooltip_query_cb(GtkWidget * widget,gint x,gint y,gboolean keyboard_tip,GtkTooltip * tooltip,gpointer data)4722 gtk_icon_view_set_tooltip_query_cb (GtkWidget  *widget,
4723                                     gint        x,
4724                                     gint        y,
4725                                     gboolean    keyboard_tip,
4726                                     GtkTooltip *tooltip,
4727                                     gpointer    data)
4728 {
4729   gchar *str;
4730   GtkTreeIter iter;
4731   GtkTreePath *path;
4732   GtkTreeModel *model;
4733   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
4734 
4735   if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget),
4736                                           &x, &y,
4737                                           keyboard_tip,
4738                                           &model, &path, &iter))
4739     return FALSE;
4740 
4741   gtk_tree_model_get (model, &iter, icon_view->priv->tooltip_column, &str, -1);
4742 
4743   if (!str)
4744     {
4745       gtk_tree_path_free (path);
4746       return FALSE;
4747     }
4748 
4749   gtk_tooltip_set_markup (tooltip, str);
4750   gtk_icon_view_set_tooltip_item (icon_view, tooltip, path);
4751 
4752   gtk_tree_path_free (path);
4753   g_free (str);
4754 
4755   return TRUE;
4756 }
4757 
4758 
4759 /**
4760  * gtk_icon_view_set_tooltip_column:
4761  * @icon_view: a #GtkIconView
4762  * @column: an integer, which is a valid column number for @icon_view’s model
4763  *
4764  * If you only plan to have simple (text-only) tooltips on full items, you
4765  * can use this function to have #GtkIconView handle these automatically
4766  * for you. @column should be set to the column in @icon_view’s model
4767  * containing the tooltip texts, or -1 to disable this feature.
4768  *
4769  * When enabled, #GtkWidget:has-tooltip will be set to %TRUE and
4770  * @icon_view will connect a #GtkWidget::query-tooltip signal handler.
4771  *
4772  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
4773  * so &, <, etc have to be escaped in the text.
4774  *
4775  * Since: 2.12
4776  */
4777 void
gtk_icon_view_set_tooltip_column(GtkIconView * icon_view,gint column)4778 gtk_icon_view_set_tooltip_column (GtkIconView *icon_view,
4779                                   gint         column)
4780 {
4781   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4782 
4783   if (column == icon_view->priv->tooltip_column)
4784     return;
4785 
4786   if (column == -1)
4787     {
4788       g_signal_handlers_disconnect_by_func (icon_view,
4789                                             gtk_icon_view_set_tooltip_query_cb,
4790                                             NULL);
4791       gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), FALSE);
4792     }
4793   else
4794     {
4795       if (icon_view->priv->tooltip_column == -1)
4796         {
4797           g_signal_connect (icon_view, "query-tooltip",
4798                             G_CALLBACK (gtk_icon_view_set_tooltip_query_cb), NULL);
4799           gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), TRUE);
4800         }
4801     }
4802 
4803   icon_view->priv->tooltip_column = column;
4804   g_object_notify (G_OBJECT (icon_view), "tooltip-column");
4805 }
4806 
4807 /**
4808  * gtk_icon_view_get_tooltip_column:
4809  * @icon_view: a #GtkIconView
4810  *
4811  * Returns the column of @icon_view’s model which is being used for
4812  * displaying tooltips on @icon_view’s rows.
4813  *
4814  * Returns: the index of the tooltip column that is currently being
4815  * used, or -1 if this is disabled.
4816  *
4817  * Since: 2.12
4818  */
4819 gint
gtk_icon_view_get_tooltip_column(GtkIconView * icon_view)4820 gtk_icon_view_get_tooltip_column (GtkIconView *icon_view)
4821 {
4822   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 0);
4823 
4824   return icon_view->priv->tooltip_column;
4825 }
4826 
4827 /**
4828  * gtk_icon_view_get_visible_range:
4829  * @icon_view: A #GtkIconView
4830  * @start_path: (out) (allow-none): Return location for start of region,
4831  *              or %NULL
4832  * @end_path: (out) (allow-none): Return location for end of region, or %NULL
4833  *
4834  * Sets @start_path and @end_path to be the first and last visible path.
4835  * Note that there may be invisible paths in between.
4836  *
4837  * Both paths should be freed with gtk_tree_path_free() after use.
4838  *
4839  * Returns: %TRUE, if valid paths were placed in @start_path and @end_path
4840  *
4841  * Since: 2.8
4842  **/
4843 gboolean
gtk_icon_view_get_visible_range(GtkIconView * icon_view,GtkTreePath ** start_path,GtkTreePath ** end_path)4844 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
4845 				 GtkTreePath **start_path,
4846 				 GtkTreePath **end_path)
4847 {
4848   gint start_index = -1;
4849   gint end_index = -1;
4850   GList *icons;
4851 
4852   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4853 
4854   if (icon_view->priv->hadjustment == NULL ||
4855       icon_view->priv->vadjustment == NULL)
4856     return FALSE;
4857 
4858   if (start_path == NULL && end_path == NULL)
4859     return FALSE;
4860 
4861   for (icons = icon_view->priv->items; icons; icons = icons->next)
4862     {
4863       GtkIconViewItem *item = icons->data;
4864       GdkRectangle    *item_area = &item->cell_area;
4865 
4866       if ((item_area->x + item_area->width >= (int)gtk_adjustment_get_value (icon_view->priv->hadjustment)) &&
4867 	  (item_area->y + item_area->height >= (int)gtk_adjustment_get_value (icon_view->priv->vadjustment)) &&
4868 	  (item_area->x <=
4869 	   (int) (gtk_adjustment_get_value (icon_view->priv->hadjustment) +
4870 		  gtk_adjustment_get_page_size (icon_view->priv->hadjustment))) &&
4871 	  (item_area->y <=
4872 	   (int) (gtk_adjustment_get_value (icon_view->priv->vadjustment) +
4873 		  gtk_adjustment_get_page_size (icon_view->priv->vadjustment))))
4874 	{
4875 	  if (start_index == -1)
4876 	    start_index = item->index;
4877 	  end_index = item->index;
4878 	}
4879     }
4880 
4881   if (start_path && start_index != -1)
4882     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4883   if (end_path && end_index != -1)
4884     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4885 
4886   return start_index != -1;
4887 }
4888 
4889 /**
4890  * gtk_icon_view_selected_foreach:
4891  * @icon_view: A #GtkIconView.
4892  * @func: (scope call): The function to call for each selected icon.
4893  * @data: User data to pass to the function.
4894  *
4895  * Calls a function for each selected icon. Note that the model or
4896  * selection cannot be modified from within this function.
4897  *
4898  * Since: 2.6
4899  **/
4900 void
gtk_icon_view_selected_foreach(GtkIconView * icon_view,GtkIconViewForeachFunc func,gpointer data)4901 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
4902 				GtkIconViewForeachFunc func,
4903 				gpointer               data)
4904 {
4905   GList *list;
4906 
4907   for (list = icon_view->priv->items; list; list = list->next)
4908     {
4909       GtkIconViewItem *item = list->data;
4910       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4911 
4912       if (item->selected)
4913 	(* func) (icon_view, path, data);
4914 
4915       gtk_tree_path_free (path);
4916     }
4917 }
4918 
4919 /**
4920  * gtk_icon_view_set_selection_mode:
4921  * @icon_view: A #GtkIconView.
4922  * @mode: The selection mode
4923  *
4924  * Sets the selection mode of the @icon_view.
4925  *
4926  * Since: 2.6
4927  **/
4928 void
gtk_icon_view_set_selection_mode(GtkIconView * icon_view,GtkSelectionMode mode)4929 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
4930 				  GtkSelectionMode  mode)
4931 {
4932   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4933 
4934   if (mode == icon_view->priv->selection_mode)
4935     return;
4936 
4937   if (mode == GTK_SELECTION_NONE ||
4938       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4939     gtk_icon_view_unselect_all (icon_view);
4940 
4941   icon_view->priv->selection_mode = mode;
4942 
4943   g_object_notify (G_OBJECT (icon_view), "selection-mode");
4944 }
4945 
4946 /**
4947  * gtk_icon_view_get_selection_mode:
4948  * @icon_view: A #GtkIconView.
4949  *
4950  * Gets the selection mode of the @icon_view.
4951  *
4952  * Returns: the current selection mode
4953  *
4954  * Since: 2.6
4955  **/
4956 GtkSelectionMode
gtk_icon_view_get_selection_mode(GtkIconView * icon_view)4957 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4958 {
4959   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4960 
4961   return icon_view->priv->selection_mode;
4962 }
4963 
4964 /**
4965  * gtk_icon_view_set_model:
4966  * @icon_view: A #GtkIconView.
4967  * @model: (allow-none): The model.
4968  *
4969  * Sets the model for a #GtkIconView.
4970  * If the @icon_view already has a model set, it will remove
4971  * it before setting the new model.  If @model is %NULL, then
4972  * it will unset the old model.
4973  *
4974  * Since: 2.6
4975  **/
4976 void
gtk_icon_view_set_model(GtkIconView * icon_view,GtkTreeModel * model)4977 gtk_icon_view_set_model (GtkIconView *icon_view,
4978 			 GtkTreeModel *model)
4979 {
4980   gboolean dirty;
4981 
4982   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4983   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4984 
4985   if (icon_view->priv->model == model)
4986     return;
4987 
4988   if (icon_view->priv->scroll_to_path)
4989     {
4990       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4991       icon_view->priv->scroll_to_path = NULL;
4992     }
4993 
4994   /* The area can be NULL while disposing */
4995   if (icon_view->priv->cell_area)
4996     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4997 
4998   dirty = gtk_icon_view_unselect_all_internal (icon_view);
4999 
5000   if (model)
5001     {
5002       GType column_type;
5003 
5004       if (icon_view->priv->pixbuf_column != -1)
5005 	{
5006 	  column_type = gtk_tree_model_get_column_type (model,
5007 							icon_view->priv->pixbuf_column);
5008 
5009 	  g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5010 	}
5011 
5012       if (icon_view->priv->text_column != -1)
5013 	{
5014 	  column_type = gtk_tree_model_get_column_type (model,
5015 							icon_view->priv->text_column);
5016 
5017 	  g_return_if_fail (column_type == G_TYPE_STRING);
5018 	}
5019 
5020       if (icon_view->priv->markup_column != -1)
5021 	{
5022 	  column_type = gtk_tree_model_get_column_type (model,
5023 							icon_view->priv->markup_column);
5024 
5025 	  g_return_if_fail (column_type == G_TYPE_STRING);
5026 	}
5027 
5028     }
5029 
5030   if (icon_view->priv->model)
5031     {
5032       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5033 					    gtk_icon_view_row_changed,
5034 					    icon_view);
5035       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5036 					    gtk_icon_view_row_inserted,
5037 					    icon_view);
5038       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5039 					    gtk_icon_view_row_deleted,
5040 					    icon_view);
5041       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5042 					    gtk_icon_view_rows_reordered,
5043 					    icon_view);
5044 
5045       g_object_unref (icon_view->priv->model);
5046 
5047       g_list_free_full (icon_view->priv->items, (GDestroyNotify) gtk_icon_view_item_free);
5048       icon_view->priv->items = NULL;
5049       icon_view->priv->anchor_item = NULL;
5050       icon_view->priv->cursor_item = NULL;
5051       icon_view->priv->last_single_clicked = NULL;
5052       icon_view->priv->last_prelight = NULL;
5053       icon_view->priv->width = 0;
5054       icon_view->priv->height = 0;
5055     }
5056 
5057   icon_view->priv->model = model;
5058 
5059   if (icon_view->priv->model)
5060     {
5061       g_object_ref (icon_view->priv->model);
5062       g_signal_connect (icon_view->priv->model,
5063 			"row-changed",
5064 			G_CALLBACK (gtk_icon_view_row_changed),
5065 			icon_view);
5066       g_signal_connect (icon_view->priv->model,
5067 			"row-inserted",
5068 			G_CALLBACK (gtk_icon_view_row_inserted),
5069 			icon_view);
5070       g_signal_connect (icon_view->priv->model,
5071 			"row-deleted",
5072 			G_CALLBACK (gtk_icon_view_row_deleted),
5073 			icon_view);
5074       g_signal_connect (icon_view->priv->model,
5075 			"rows-reordered",
5076 			G_CALLBACK (gtk_icon_view_rows_reordered),
5077 			icon_view);
5078 
5079       gtk_icon_view_build_items (icon_view);
5080     }
5081 
5082   g_object_notify (G_OBJECT (icon_view), "model");
5083 
5084   if (dirty)
5085     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5086 
5087   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
5088 }
5089 
5090 /**
5091  * gtk_icon_view_get_model:
5092  * @icon_view: a #GtkIconView
5093  *
5094  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
5095  * model is unset.
5096  *
5097  * Returns: (nullable) (transfer none): A #GtkTreeModel, or %NULL if none is
5098  *     currently being used.
5099  *
5100  * Since: 2.6
5101  **/
5102 GtkTreeModel *
gtk_icon_view_get_model(GtkIconView * icon_view)5103 gtk_icon_view_get_model (GtkIconView *icon_view)
5104 {
5105   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5106 
5107   return icon_view->priv->model;
5108 }
5109 
5110 static void
update_text_cell(GtkIconView * icon_view)5111 update_text_cell (GtkIconView *icon_view)
5112 {
5113   if (!icon_view->priv->cell_area)
5114     return;
5115 
5116   if (icon_view->priv->text_column == -1 &&
5117       icon_view->priv->markup_column == -1)
5118     {
5119       if (icon_view->priv->text_cell != NULL)
5120 	{
5121 	  gtk_cell_area_remove (icon_view->priv->cell_area,
5122 				icon_view->priv->text_cell);
5123 	  icon_view->priv->text_cell = NULL;
5124 	}
5125     }
5126   else
5127     {
5128       if (icon_view->priv->text_cell == NULL)
5129 	{
5130 	  icon_view->priv->text_cell = gtk_cell_renderer_text_new ();
5131 
5132 	  gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), icon_view->priv->text_cell, FALSE);
5133 	}
5134 
5135       if (icon_view->priv->markup_column != -1)
5136 	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5137 					icon_view->priv->text_cell,
5138 					"markup", icon_view->priv->markup_column,
5139 					NULL);
5140       else
5141 	gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5142 					icon_view->priv->text_cell,
5143 					"text", icon_view->priv->text_column,
5144 					NULL);
5145 
5146       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5147 	g_object_set (icon_view->priv->text_cell,
5148                       "alignment", PANGO_ALIGN_CENTER,
5149 		      "wrap-mode", PANGO_WRAP_WORD_CHAR,
5150 		      "xalign", 0.5,
5151 		      "yalign", 0.0,
5152 		      NULL);
5153       else
5154 	g_object_set (icon_view->priv->text_cell,
5155                       "alignment", PANGO_ALIGN_LEFT,
5156 		      "wrap-mode", PANGO_WRAP_WORD_CHAR,
5157 		      "xalign", 0.0,
5158 		      "yalign", 0.5,
5159 		      NULL);
5160     }
5161 }
5162 
5163 static void
update_pixbuf_cell(GtkIconView * icon_view)5164 update_pixbuf_cell (GtkIconView *icon_view)
5165 {
5166   if (!icon_view->priv->cell_area)
5167     return;
5168 
5169   if (icon_view->priv->pixbuf_column == -1)
5170     {
5171       if (icon_view->priv->pixbuf_cell != NULL)
5172 	{
5173 	  gtk_cell_area_remove (icon_view->priv->cell_area,
5174 				icon_view->priv->pixbuf_cell);
5175 
5176 	  icon_view->priv->pixbuf_cell = NULL;
5177 	}
5178     }
5179   else
5180     {
5181       if (icon_view->priv->pixbuf_cell == NULL)
5182 	{
5183 	  icon_view->priv->pixbuf_cell = gtk_cell_renderer_pixbuf_new ();
5184 
5185 	  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), icon_view->priv->pixbuf_cell, FALSE);
5186 	}
5187 
5188       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5189 				      icon_view->priv->pixbuf_cell,
5190 				      "pixbuf", icon_view->priv->pixbuf_column,
5191 				      NULL);
5192 
5193       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5194 	g_object_set (icon_view->priv->pixbuf_cell,
5195 		      "xalign", 0.5,
5196 		      "yalign", 1.0,
5197 		      NULL);
5198       else
5199 	g_object_set (icon_view->priv->pixbuf_cell,
5200 		      "xalign", 0.0,
5201 		      "yalign", 0.0,
5202 		      NULL);
5203     }
5204 }
5205 
5206 /**
5207  * gtk_icon_view_set_text_column:
5208  * @icon_view: A #GtkIconView.
5209  * @column: A column in the currently used model, or -1 to display no text
5210  *
5211  * Sets the column with text for @icon_view to be @column. The text
5212  * column must be of type #G_TYPE_STRING.
5213  *
5214  * Since: 2.6
5215  **/
5216 void
gtk_icon_view_set_text_column(GtkIconView * icon_view,gint column)5217 gtk_icon_view_set_text_column (GtkIconView *icon_view,
5218 			       gint          column)
5219 {
5220   if (column == icon_view->priv->text_column)
5221     return;
5222 
5223   if (column == -1)
5224     icon_view->priv->text_column = -1;
5225   else
5226     {
5227       if (icon_view->priv->model != NULL)
5228 	{
5229 	  GType column_type;
5230 
5231 	  column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5232 
5233 	  g_return_if_fail (column_type == G_TYPE_STRING);
5234 	}
5235 
5236       icon_view->priv->text_column = column;
5237     }
5238 
5239   if (icon_view->priv->cell_area)
5240     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5241 
5242   update_text_cell (icon_view);
5243 
5244   gtk_icon_view_invalidate_sizes (icon_view);
5245 
5246   g_object_notify (G_OBJECT (icon_view), "text-column");
5247 }
5248 
5249 /**
5250  * gtk_icon_view_get_text_column:
5251  * @icon_view: A #GtkIconView.
5252  *
5253  * Returns the column with text for @icon_view.
5254  *
5255  * Returns: the text column, or -1 if it’s unset.
5256  *
5257  * Since: 2.6
5258  */
5259 gint
gtk_icon_view_get_text_column(GtkIconView * icon_view)5260 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
5261 {
5262   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5263 
5264   return icon_view->priv->text_column;
5265 }
5266 
5267 /**
5268  * gtk_icon_view_set_markup_column:
5269  * @icon_view: A #GtkIconView.
5270  * @column: A column in the currently used model, or -1 to display no text
5271  *
5272  * Sets the column with markup information for @icon_view to be
5273  * @column. The markup column must be of type #G_TYPE_STRING.
5274  * If the markup column is set to something, it overrides
5275  * the text column set by gtk_icon_view_set_text_column().
5276  *
5277  * Since: 2.6
5278  **/
5279 void
gtk_icon_view_set_markup_column(GtkIconView * icon_view,gint column)5280 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5281 				 gint         column)
5282 {
5283   if (column == icon_view->priv->markup_column)
5284     return;
5285 
5286   if (column == -1)
5287     icon_view->priv->markup_column = -1;
5288   else
5289     {
5290       if (icon_view->priv->model != NULL)
5291 	{
5292 	  GType column_type;
5293 
5294 	  column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5295 
5296 	  g_return_if_fail (column_type == G_TYPE_STRING);
5297 	}
5298 
5299       icon_view->priv->markup_column = column;
5300     }
5301 
5302   if (icon_view->priv->cell_area)
5303     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5304 
5305   update_text_cell (icon_view);
5306 
5307   gtk_icon_view_invalidate_sizes (icon_view);
5308 
5309   g_object_notify (G_OBJECT (icon_view), "markup-column");
5310 }
5311 
5312 /**
5313  * gtk_icon_view_get_markup_column:
5314  * @icon_view: A #GtkIconView.
5315  *
5316  * Returns the column with markup text for @icon_view.
5317  *
5318  * Returns: the markup column, or -1 if it’s unset.
5319  *
5320  * Since: 2.6
5321  */
5322 gint
gtk_icon_view_get_markup_column(GtkIconView * icon_view)5323 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
5324 {
5325   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5326 
5327   return icon_view->priv->markup_column;
5328 }
5329 
5330 /**
5331  * gtk_icon_view_set_pixbuf_column:
5332  * @icon_view: A #GtkIconView.
5333  * @column: A column in the currently used model, or -1 to disable
5334  *
5335  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5336  * column must be of type #GDK_TYPE_PIXBUF
5337  *
5338  * Since: 2.6
5339  **/
5340 void
gtk_icon_view_set_pixbuf_column(GtkIconView * icon_view,gint column)5341 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5342 				 gint         column)
5343 {
5344   if (column == icon_view->priv->pixbuf_column)
5345     return;
5346 
5347   if (column == -1)
5348     icon_view->priv->pixbuf_column = -1;
5349   else
5350     {
5351       if (icon_view->priv->model != NULL)
5352 	{
5353 	  GType column_type;
5354 
5355 	  column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5356 
5357 	  g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5358 	}
5359 
5360       icon_view->priv->pixbuf_column = column;
5361     }
5362 
5363   if (icon_view->priv->cell_area)
5364     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5365 
5366   update_pixbuf_cell (icon_view);
5367 
5368   gtk_icon_view_invalidate_sizes (icon_view);
5369 
5370   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5371 
5372 }
5373 
5374 /**
5375  * gtk_icon_view_get_pixbuf_column:
5376  * @icon_view: A #GtkIconView.
5377  *
5378  * Returns the column with pixbufs for @icon_view.
5379  *
5380  * Returns: the pixbuf column, or -1 if it’s unset.
5381  *
5382  * Since: 2.6
5383  */
5384 gint
gtk_icon_view_get_pixbuf_column(GtkIconView * icon_view)5385 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
5386 {
5387   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5388 
5389   return icon_view->priv->pixbuf_column;
5390 }
5391 
5392 /**
5393  * gtk_icon_view_select_path:
5394  * @icon_view: A #GtkIconView.
5395  * @path: The #GtkTreePath to be selected.
5396  *
5397  * Selects the row at @path.
5398  *
5399  * Since: 2.6
5400  **/
5401 void
gtk_icon_view_select_path(GtkIconView * icon_view,GtkTreePath * path)5402 gtk_icon_view_select_path (GtkIconView *icon_view,
5403 			   GtkTreePath *path)
5404 {
5405   GtkIconViewItem *item = NULL;
5406 
5407   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5408   g_return_if_fail (icon_view->priv->model != NULL);
5409   g_return_if_fail (path != NULL);
5410 
5411   if (gtk_tree_path_get_depth (path) > 0)
5412     item = g_list_nth_data (icon_view->priv->items,
5413 			    gtk_tree_path_get_indices(path)[0]);
5414 
5415   if (item)
5416     _gtk_icon_view_select_item (icon_view, item);
5417 }
5418 
5419 /**
5420  * gtk_icon_view_unselect_path:
5421  * @icon_view: A #GtkIconView.
5422  * @path: The #GtkTreePath to be unselected.
5423  *
5424  * Unselects the row at @path.
5425  *
5426  * Since: 2.6
5427  **/
5428 void
gtk_icon_view_unselect_path(GtkIconView * icon_view,GtkTreePath * path)5429 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5430 			     GtkTreePath *path)
5431 {
5432   GtkIconViewItem *item;
5433 
5434   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5435   g_return_if_fail (icon_view->priv->model != NULL);
5436   g_return_if_fail (path != NULL);
5437 
5438   item = g_list_nth_data (icon_view->priv->items,
5439 			  gtk_tree_path_get_indices(path)[0]);
5440 
5441   if (!item)
5442     return;
5443 
5444   _gtk_icon_view_unselect_item (icon_view, item);
5445 }
5446 
5447 /**
5448  * gtk_icon_view_get_selected_items:
5449  * @icon_view: A #GtkIconView.
5450  *
5451  * Creates a list of paths of all selected items. Additionally, if you are
5452  * planning on modifying the model after calling this function, you may
5453  * want to convert the returned list into a list of #GtkTreeRowReferences.
5454  * To do this, you can use gtk_tree_row_reference_new().
5455  *
5456  * To free the return value, use:
5457  * |[<!-- language="C" -->
5458  * g_list_free_full (list, (GDestroyNotify) gtk_tree_path_free);
5459  * ]|
5460  *
5461  * Returns: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
5462  *
5463  * Since: 2.6
5464  **/
5465 GList *
gtk_icon_view_get_selected_items(GtkIconView * icon_view)5466 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5467 {
5468   GList *list;
5469   GList *selected = NULL;
5470 
5471   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5472 
5473   for (list = icon_view->priv->items; list != NULL; list = list->next)
5474     {
5475       GtkIconViewItem *item = list->data;
5476 
5477       if (item->selected)
5478 	{
5479 	  GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5480 
5481 	  selected = g_list_prepend (selected, path);
5482 	}
5483     }
5484 
5485   return selected;
5486 }
5487 
5488 /**
5489  * gtk_icon_view_select_all:
5490  * @icon_view: A #GtkIconView.
5491  *
5492  * Selects all the icons. @icon_view must has its selection mode set
5493  * to #GTK_SELECTION_MULTIPLE.
5494  *
5495  * Since: 2.6
5496  **/
5497 void
gtk_icon_view_select_all(GtkIconView * icon_view)5498 gtk_icon_view_select_all (GtkIconView *icon_view)
5499 {
5500   GList *items;
5501   gboolean dirty = FALSE;
5502 
5503   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5504 
5505   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5506     return;
5507 
5508   for (items = icon_view->priv->items; items; items = items->next)
5509     {
5510       GtkIconViewItem *item = items->data;
5511 
5512       if (!item->selected)
5513 	{
5514 	  dirty = TRUE;
5515 	  item->selected = TRUE;
5516 	  gtk_icon_view_queue_draw_item (icon_view, item);
5517 	}
5518     }
5519 
5520   if (dirty)
5521     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5522 }
5523 
5524 /**
5525  * gtk_icon_view_unselect_all:
5526  * @icon_view: A #GtkIconView.
5527  *
5528  * Unselects all the icons.
5529  *
5530  * Since: 2.6
5531  **/
5532 void
gtk_icon_view_unselect_all(GtkIconView * icon_view)5533 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5534 {
5535   gboolean dirty = FALSE;
5536 
5537   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5538 
5539   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5540     return;
5541 
5542   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5543 
5544   if (dirty)
5545     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5546 }
5547 
5548 /**
5549  * gtk_icon_view_path_is_selected:
5550  * @icon_view: A #GtkIconView.
5551  * @path: A #GtkTreePath to check selection on.
5552  *
5553  * Returns %TRUE if the icon pointed to by @path is currently
5554  * selected. If @path does not point to a valid location, %FALSE is returned.
5555  *
5556  * Returns: %TRUE if @path is selected.
5557  *
5558  * Since: 2.6
5559  **/
5560 gboolean
gtk_icon_view_path_is_selected(GtkIconView * icon_view,GtkTreePath * path)5561 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5562 				GtkTreePath *path)
5563 {
5564   GtkIconViewItem *item;
5565 
5566   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5567   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5568   g_return_val_if_fail (path != NULL, FALSE);
5569 
5570   item = g_list_nth_data (icon_view->priv->items,
5571 			  gtk_tree_path_get_indices(path)[0]);
5572 
5573   if (!item)
5574     return FALSE;
5575 
5576   return item->selected;
5577 }
5578 
5579 /**
5580  * gtk_icon_view_get_item_row:
5581  * @icon_view: a #GtkIconView
5582  * @path: the #GtkTreePath of the item
5583  *
5584  * Gets the row in which the item @path is currently
5585  * displayed. Row numbers start at 0.
5586  *
5587  * Returns: The row in which the item is displayed
5588  *
5589  * Since: 2.22
5590  */
5591 gint
gtk_icon_view_get_item_row(GtkIconView * icon_view,GtkTreePath * path)5592 gtk_icon_view_get_item_row (GtkIconView *icon_view,
5593                             GtkTreePath *path)
5594 {
5595   GtkIconViewItem *item;
5596 
5597   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5598   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5599   g_return_val_if_fail (path != NULL, -1);
5600 
5601   item = g_list_nth_data (icon_view->priv->items,
5602                           gtk_tree_path_get_indices(path)[0]);
5603 
5604   if (!item)
5605     return -1;
5606 
5607   return item->row;
5608 }
5609 
5610 /**
5611  * gtk_icon_view_get_item_column:
5612  * @icon_view: a #GtkIconView
5613  * @path: the #GtkTreePath of the item
5614  *
5615  * Gets the column in which the item @path is currently
5616  * displayed. Column numbers start at 0.
5617  *
5618  * Returns: The column in which the item is displayed
5619  *
5620  * Since: 2.22
5621  */
5622 gint
gtk_icon_view_get_item_column(GtkIconView * icon_view,GtkTreePath * path)5623 gtk_icon_view_get_item_column (GtkIconView *icon_view,
5624                                GtkTreePath *path)
5625 {
5626   GtkIconViewItem *item;
5627 
5628   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5629   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5630   g_return_val_if_fail (path != NULL, -1);
5631 
5632   item = g_list_nth_data (icon_view->priv->items,
5633                           gtk_tree_path_get_indices(path)[0]);
5634 
5635   if (!item)
5636     return -1;
5637 
5638   return item->col;
5639 }
5640 
5641 /**
5642  * gtk_icon_view_item_activated:
5643  * @icon_view: A #GtkIconView
5644  * @path: The #GtkTreePath to be activated
5645  *
5646  * Activates the item determined by @path.
5647  *
5648  * Since: 2.6
5649  **/
5650 void
gtk_icon_view_item_activated(GtkIconView * icon_view,GtkTreePath * path)5651 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5652 			      GtkTreePath      *path)
5653 {
5654   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5655   g_return_if_fail (path != NULL);
5656 
5657   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5658 }
5659 
5660 /**
5661  * gtk_icon_view_set_item_orientation:
5662  * @icon_view: a #GtkIconView
5663  * @orientation: the relative position of texts and icons
5664  *
5665  * Sets the ::item-orientation property which determines whether the labels
5666  * are drawn beside the icons instead of below.
5667  *
5668  * Since: 2.6
5669  **/
5670 void
gtk_icon_view_set_item_orientation(GtkIconView * icon_view,GtkOrientation orientation)5671 gtk_icon_view_set_item_orientation (GtkIconView    *icon_view,
5672                                     GtkOrientation  orientation)
5673 {
5674   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5675 
5676   if (icon_view->priv->item_orientation != orientation)
5677     {
5678       icon_view->priv->item_orientation = orientation;
5679 
5680       if (icon_view->priv->cell_area)
5681 	{
5682 	  if (GTK_IS_ORIENTABLE (icon_view->priv->cell_area))
5683 	    gtk_orientable_set_orientation (GTK_ORIENTABLE (icon_view->priv->cell_area),
5684 					    icon_view->priv->item_orientation);
5685 
5686 	  gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5687 	}
5688 
5689       gtk_icon_view_invalidate_sizes (icon_view);
5690 
5691       update_text_cell (icon_view);
5692       update_pixbuf_cell (icon_view);
5693 
5694       g_object_notify (G_OBJECT (icon_view), "item-orientation");
5695     }
5696 }
5697 
5698 /**
5699  * gtk_icon_view_get_item_orientation:
5700  * @icon_view: a #GtkIconView
5701  *
5702  * Returns the value of the ::item-orientation property which determines
5703  * whether the labels are drawn beside the icons instead of below.
5704  *
5705  * Returns: the relative position of texts and icons
5706  *
5707  * Since: 2.6
5708  **/
5709 GtkOrientation
gtk_icon_view_get_item_orientation(GtkIconView * icon_view)5710 gtk_icon_view_get_item_orientation (GtkIconView *icon_view)
5711 {
5712   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view),
5713 			GTK_ORIENTATION_VERTICAL);
5714 
5715   return icon_view->priv->item_orientation;
5716 }
5717 
5718 /**
5719  * gtk_icon_view_set_columns:
5720  * @icon_view: a #GtkIconView
5721  * @columns: the number of columns
5722  *
5723  * Sets the ::columns property which determines in how
5724  * many columns the icons are arranged. If @columns is
5725  * -1, the number of columns will be chosen automatically
5726  * to fill the available area.
5727  *
5728  * Since: 2.6
5729  */
5730 void
gtk_icon_view_set_columns(GtkIconView * icon_view,gint columns)5731 gtk_icon_view_set_columns (GtkIconView *icon_view,
5732 			   gint         columns)
5733 {
5734   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5735 
5736   if (icon_view->priv->columns != columns)
5737     {
5738       icon_view->priv->columns = columns;
5739 
5740       if (icon_view->priv->cell_area)
5741 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5742 
5743       gtk_widget_queue_resize (GTK_WIDGET (icon_view));
5744 
5745       g_object_notify (G_OBJECT (icon_view), "columns");
5746     }
5747 }
5748 
5749 /**
5750  * gtk_icon_view_get_columns:
5751  * @icon_view: a #GtkIconView
5752  *
5753  * Returns the value of the ::columns property.
5754  *
5755  * Returns: the number of columns, or -1
5756  *
5757  * Since: 2.6
5758  */
5759 gint
gtk_icon_view_get_columns(GtkIconView * icon_view)5760 gtk_icon_view_get_columns (GtkIconView *icon_view)
5761 {
5762   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5763 
5764   return icon_view->priv->columns;
5765 }
5766 
5767 /**
5768  * gtk_icon_view_set_item_width:
5769  * @icon_view: a #GtkIconView
5770  * @item_width: the width for each item
5771  *
5772  * Sets the ::item-width property which specifies the width
5773  * to use for each item. If it is set to -1, the icon view will
5774  * automatically determine a suitable item size.
5775  *
5776  * Since: 2.6
5777  */
5778 void
gtk_icon_view_set_item_width(GtkIconView * icon_view,gint item_width)5779 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5780 			      gint         item_width)
5781 {
5782   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5783 
5784   if (icon_view->priv->item_width != item_width)
5785     {
5786       icon_view->priv->item_width = item_width;
5787 
5788       if (icon_view->priv->cell_area)
5789 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5790 
5791       gtk_icon_view_invalidate_sizes (icon_view);
5792 
5793       update_text_cell (icon_view);
5794 
5795       g_object_notify (G_OBJECT (icon_view), "item-width");
5796     }
5797 }
5798 
5799 /**
5800  * gtk_icon_view_get_item_width:
5801  * @icon_view: a #GtkIconView
5802  *
5803  * Returns the value of the ::item-width property.
5804  *
5805  * Returns: the width of a single item, or -1
5806  *
5807  * Since: 2.6
5808  */
5809 gint
gtk_icon_view_get_item_width(GtkIconView * icon_view)5810 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5811 {
5812   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5813 
5814   return icon_view->priv->item_width;
5815 }
5816 
5817 
5818 /**
5819  * gtk_icon_view_set_spacing:
5820  * @icon_view: a #GtkIconView
5821  * @spacing: the spacing
5822  *
5823  * Sets the ::spacing property which specifies the space
5824  * which is inserted between the cells (i.e. the icon and
5825  * the text) of an item.
5826  *
5827  * Since: 2.6
5828  */
5829 void
gtk_icon_view_set_spacing(GtkIconView * icon_view,gint spacing)5830 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5831 			   gint         spacing)
5832 {
5833   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5834 
5835   if (icon_view->priv->spacing != spacing)
5836     {
5837       icon_view->priv->spacing = spacing;
5838 
5839       if (icon_view->priv->cell_area)
5840 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5841 
5842       gtk_icon_view_invalidate_sizes (icon_view);
5843 
5844       g_object_notify (G_OBJECT (icon_view), "spacing");
5845     }
5846 }
5847 
5848 /**
5849  * gtk_icon_view_get_spacing:
5850  * @icon_view: a #GtkIconView
5851  *
5852  * Returns the value of the ::spacing property.
5853  *
5854  * Returns: the space between cells
5855  *
5856  * Since: 2.6
5857  */
5858 gint
gtk_icon_view_get_spacing(GtkIconView * icon_view)5859 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5860 {
5861   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5862 
5863   return icon_view->priv->spacing;
5864 }
5865 
5866 /**
5867  * gtk_icon_view_set_row_spacing:
5868  * @icon_view: a #GtkIconView
5869  * @row_spacing: the row spacing
5870  *
5871  * Sets the ::row-spacing property which specifies the space
5872  * which is inserted between the rows of the icon view.
5873  *
5874  * Since: 2.6
5875  */
5876 void
gtk_icon_view_set_row_spacing(GtkIconView * icon_view,gint row_spacing)5877 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5878 			       gint         row_spacing)
5879 {
5880   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5881 
5882   if (icon_view->priv->row_spacing != row_spacing)
5883     {
5884       icon_view->priv->row_spacing = row_spacing;
5885 
5886       if (icon_view->priv->cell_area)
5887 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5888 
5889       gtk_icon_view_invalidate_sizes (icon_view);
5890 
5891       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5892     }
5893 }
5894 
5895 /**
5896  * gtk_icon_view_get_row_spacing:
5897  * @icon_view: a #GtkIconView
5898  *
5899  * Returns the value of the ::row-spacing property.
5900  *
5901  * Returns: the space between rows
5902  *
5903  * Since: 2.6
5904  */
5905 gint
gtk_icon_view_get_row_spacing(GtkIconView * icon_view)5906 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5907 {
5908   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5909 
5910   return icon_view->priv->row_spacing;
5911 }
5912 
5913 /**
5914  * gtk_icon_view_set_column_spacing:
5915  * @icon_view: a #GtkIconView
5916  * @column_spacing: the column spacing
5917  *
5918  * Sets the ::column-spacing property which specifies the space
5919  * which is inserted between the columns of the icon view.
5920  *
5921  * Since: 2.6
5922  */
5923 void
gtk_icon_view_set_column_spacing(GtkIconView * icon_view,gint column_spacing)5924 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5925 				  gint         column_spacing)
5926 {
5927   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5928 
5929   if (icon_view->priv->column_spacing != column_spacing)
5930     {
5931       icon_view->priv->column_spacing = column_spacing;
5932 
5933       if (icon_view->priv->cell_area)
5934 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5935 
5936       gtk_icon_view_invalidate_sizes (icon_view);
5937 
5938       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5939     }
5940 }
5941 
5942 /**
5943  * gtk_icon_view_get_column_spacing:
5944  * @icon_view: a #GtkIconView
5945  *
5946  * Returns the value of the ::column-spacing property.
5947  *
5948  * Returns: the space between columns
5949  *
5950  * Since: 2.6
5951  */
5952 gint
gtk_icon_view_get_column_spacing(GtkIconView * icon_view)5953 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5954 {
5955   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5956 
5957   return icon_view->priv->column_spacing;
5958 }
5959 
5960 /**
5961  * gtk_icon_view_set_margin:
5962  * @icon_view: a #GtkIconView
5963  * @margin: the margin
5964  *
5965  * Sets the ::margin property which specifies the space
5966  * which is inserted at the top, bottom, left and right
5967  * of the icon view.
5968  *
5969  * Since: 2.6
5970  */
5971 void
gtk_icon_view_set_margin(GtkIconView * icon_view,gint margin)5972 gtk_icon_view_set_margin (GtkIconView *icon_view,
5973 			  gint         margin)
5974 {
5975   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5976 
5977   if (icon_view->priv->margin != margin)
5978     {
5979       icon_view->priv->margin = margin;
5980 
5981       if (icon_view->priv->cell_area)
5982 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5983 
5984       gtk_icon_view_invalidate_sizes (icon_view);
5985 
5986       g_object_notify (G_OBJECT (icon_view), "margin");
5987     }
5988 }
5989 
5990 /**
5991  * gtk_icon_view_get_margin:
5992  * @icon_view: a #GtkIconView
5993  *
5994  * Returns the value of the ::margin property.
5995  *
5996  * Returns: the space at the borders
5997  *
5998  * Since: 2.6
5999  */
6000 gint
gtk_icon_view_get_margin(GtkIconView * icon_view)6001 gtk_icon_view_get_margin (GtkIconView *icon_view)
6002 {
6003   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6004 
6005   return icon_view->priv->margin;
6006 }
6007 
6008 /**
6009  * gtk_icon_view_set_item_padding:
6010  * @icon_view: a #GtkIconView
6011  * @item_padding: the item padding
6012  *
6013  * Sets the #GtkIconView:item-padding property which specifies the padding
6014  * around each of the icon view’s items.
6015  *
6016  * Since: 2.18
6017  */
6018 void
gtk_icon_view_set_item_padding(GtkIconView * icon_view,gint item_padding)6019 gtk_icon_view_set_item_padding (GtkIconView *icon_view,
6020 				gint         item_padding)
6021 {
6022   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6023 
6024   if (icon_view->priv->item_padding != item_padding)
6025     {
6026       icon_view->priv->item_padding = item_padding;
6027 
6028       if (icon_view->priv->cell_area)
6029 	gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
6030 
6031       gtk_icon_view_invalidate_sizes (icon_view);
6032 
6033       g_object_notify (G_OBJECT (icon_view), "item-padding");
6034     }
6035 }
6036 
6037 /**
6038  * gtk_icon_view_get_item_padding:
6039  * @icon_view: a #GtkIconView
6040  *
6041  * Returns the value of the ::item-padding property.
6042  *
6043  * Returns: the padding around items
6044  *
6045  * Since: 2.18
6046  */
6047 gint
gtk_icon_view_get_item_padding(GtkIconView * icon_view)6048 gtk_icon_view_get_item_padding (GtkIconView *icon_view)
6049 {
6050   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6051 
6052   return icon_view->priv->item_padding;
6053 }
6054 
6055 /* Get/set whether drag_motion requested the drag data and
6056  * drag_data_received should thus not actually insert the data,
6057  * since the data doesn’t result from a drop.
6058  */
6059 static void
set_status_pending(GdkDragContext * context,GdkDragAction suggested_action)6060 set_status_pending (GdkDragContext *context,
6061                     GdkDragAction   suggested_action)
6062 {
6063   g_object_set_data (G_OBJECT (context),
6064                      I_("gtk-icon-view-status-pending"),
6065                      GINT_TO_POINTER (suggested_action));
6066 }
6067 
6068 static GdkDragAction
get_status_pending(GdkDragContext * context)6069 get_status_pending (GdkDragContext *context)
6070 {
6071   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
6072                                              "gtk-icon-view-status-pending"));
6073 }
6074 
6075 static void
unset_reorderable(GtkIconView * icon_view)6076 unset_reorderable (GtkIconView *icon_view)
6077 {
6078   if (icon_view->priv->reorderable)
6079     {
6080       icon_view->priv->reorderable = FALSE;
6081       g_object_notify (G_OBJECT (icon_view), "reorderable");
6082     }
6083 }
6084 
6085 static void
set_source_row(GdkDragContext * context,GtkTreeModel * model,GtkTreePath * source_row)6086 set_source_row (GdkDragContext *context,
6087                 GtkTreeModel   *model,
6088                 GtkTreePath    *source_row)
6089 {
6090   if (source_row)
6091     g_object_set_data_full (G_OBJECT (context),
6092 			    I_("gtk-icon-view-source-row"),
6093 			    gtk_tree_row_reference_new (model, source_row),
6094 			    (GDestroyNotify) gtk_tree_row_reference_free);
6095   else
6096     g_object_set_data_full (G_OBJECT (context),
6097 			    I_("gtk-icon-view-source-row"),
6098 			    NULL, NULL);
6099 }
6100 
6101 static GtkTreePath*
get_source_row(GdkDragContext * context)6102 get_source_row (GdkDragContext *context)
6103 {
6104   GtkTreeRowReference *ref;
6105 
6106   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
6107 
6108   if (ref)
6109     return gtk_tree_row_reference_get_path (ref);
6110   else
6111     return NULL;
6112 }
6113 
6114 typedef struct
6115 {
6116   GtkTreeRowReference *dest_row;
6117   gboolean             empty_view_drop;
6118   gboolean             drop_append_mode;
6119 } DestRow;
6120 
6121 static void
dest_row_free(gpointer data)6122 dest_row_free (gpointer data)
6123 {
6124   DestRow *dr = (DestRow *)data;
6125 
6126   gtk_tree_row_reference_free (dr->dest_row);
6127   g_free (dr);
6128 }
6129 
6130 static void
set_dest_row(GdkDragContext * context,GtkTreeModel * model,GtkTreePath * dest_row,gboolean empty_view_drop,gboolean drop_append_mode)6131 set_dest_row (GdkDragContext *context,
6132 	      GtkTreeModel   *model,
6133 	      GtkTreePath    *dest_row,
6134 	      gboolean        empty_view_drop,
6135 	      gboolean        drop_append_mode)
6136 {
6137   DestRow *dr;
6138 
6139   if (!dest_row)
6140     {
6141       g_object_set_data_full (G_OBJECT (context),
6142 			      I_("gtk-icon-view-dest-row"),
6143 			      NULL, NULL);
6144       return;
6145     }
6146 
6147   dr = g_new0 (DestRow, 1);
6148 
6149   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
6150   dr->empty_view_drop = empty_view_drop;
6151   dr->drop_append_mode = drop_append_mode;
6152   g_object_set_data_full (G_OBJECT (context),
6153                           I_("gtk-icon-view-dest-row"),
6154                           dr, (GDestroyNotify) dest_row_free);
6155 }
6156 
6157 static GtkTreePath*
get_dest_row(GdkDragContext * context)6158 get_dest_row (GdkDragContext *context)
6159 {
6160   DestRow *dr;
6161 
6162   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
6163 
6164   if (dr)
6165     {
6166       GtkTreePath *path = NULL;
6167 
6168       if (dr->dest_row)
6169 	path = gtk_tree_row_reference_get_path (dr->dest_row);
6170       else if (dr->empty_view_drop)
6171 	path = gtk_tree_path_new_from_indices (0, -1);
6172       else
6173 	path = NULL;
6174 
6175       if (path && dr->drop_append_mode)
6176 	gtk_tree_path_next (path);
6177 
6178       return path;
6179     }
6180   else
6181     return NULL;
6182 }
6183 
6184 static gboolean
check_model_dnd(GtkTreeModel * model,GType required_iface,const gchar * signal)6185 check_model_dnd (GtkTreeModel *model,
6186                  GType         required_iface,
6187                  const gchar  *signal)
6188 {
6189   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
6190     {
6191       g_warning ("You must override the default '%s' handler "
6192                  "on GtkIconView when using models that don't support "
6193                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
6194                  "is to connect to '%s' and call "
6195                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
6196                  "the default handler from running. Look at the source code "
6197                  "for the default handler in gtkiconview.c to get an idea what "
6198                  "your handler should do. (gtkiconview.c is in the GTK+ source "
6199                  "code.) If you're using GTK+ from a language other than C, "
6200                  "there may be a more natural way to override default handlers, e.g. via derivation.",
6201                  signal, g_type_name (required_iface), signal);
6202       return FALSE;
6203     }
6204   else
6205     return TRUE;
6206 }
6207 
6208 static void
remove_scroll_timeout(GtkIconView * icon_view)6209 remove_scroll_timeout (GtkIconView *icon_view)
6210 {
6211   if (icon_view->priv->scroll_timeout_id != 0)
6212     {
6213       g_source_remove (icon_view->priv->scroll_timeout_id);
6214 
6215       icon_view->priv->scroll_timeout_id = 0;
6216     }
6217 }
6218 
6219 static void
gtk_icon_view_autoscroll(GtkIconView * icon_view)6220 gtk_icon_view_autoscroll (GtkIconView *icon_view)
6221 {
6222   GdkWindow *window;
6223   gint px, py, width, height;
6224   gint hoffset, voffset;
6225 
6226   window = gtk_widget_get_window (GTK_WIDGET (icon_view));
6227 
6228   px = icon_view->priv->event_last_x;
6229   py = icon_view->priv->event_last_y;
6230   gdk_window_get_geometry (window, NULL, NULL, &width, &height);
6231 
6232   /* see if we are near the edge. */
6233   voffset = py - 2 * SCROLL_EDGE_SIZE;
6234   if (voffset > 0)
6235     voffset = MAX (py - (height - 2 * SCROLL_EDGE_SIZE), 0);
6236 
6237   hoffset = px - 2 * SCROLL_EDGE_SIZE;
6238   if (hoffset > 0)
6239     hoffset = MAX (px - (width - 2 * SCROLL_EDGE_SIZE), 0);
6240 
6241   if (voffset != 0)
6242     gtk_adjustment_set_value (icon_view->priv->vadjustment,
6243                               gtk_adjustment_get_value (icon_view->priv->vadjustment) + voffset);
6244 
6245   if (hoffset != 0)
6246     gtk_adjustment_set_value (icon_view->priv->hadjustment,
6247                               gtk_adjustment_get_value (icon_view->priv->hadjustment) + hoffset);
6248 }
6249 
6250 static gboolean
drag_scroll_timeout(gpointer data)6251 drag_scroll_timeout (gpointer data)
6252 {
6253   gtk_icon_view_autoscroll (data);
6254 
6255   return TRUE;
6256 }
6257 
6258 static gboolean
set_destination(GtkIconView * icon_view,GdkDragContext * context,gint x,gint y,GdkDragAction * suggested_action,GdkAtom * target)6259 set_destination (GtkIconView    *icon_view,
6260 		 GdkDragContext *context,
6261 		 gint            x,
6262 		 gint            y,
6263 		 GdkDragAction  *suggested_action,
6264 		 GdkAtom        *target)
6265 {
6266   GtkWidget *widget;
6267   GtkTreePath *path = NULL;
6268   GtkIconViewDropPosition pos;
6269   GtkIconViewDropPosition old_pos;
6270   GtkTreePath *old_dest_path = NULL;
6271   gboolean can_drop = FALSE;
6272 
6273   widget = GTK_WIDGET (icon_view);
6274 
6275   *suggested_action = 0;
6276   *target = GDK_NONE;
6277 
6278   if (!icon_view->priv->dest_set)
6279     {
6280       /* someone unset us as a drag dest, note that if
6281        * we return FALSE drag_leave isn't called
6282        */
6283 
6284       gtk_icon_view_set_drag_dest_item (icon_view,
6285 					NULL,
6286 					GTK_ICON_VIEW_DROP_LEFT);
6287 
6288       remove_scroll_timeout (GTK_ICON_VIEW (widget));
6289 
6290       return FALSE; /* no longer a drop site */
6291     }
6292 
6293   *target = gtk_drag_dest_find_target (widget, context,
6294                                        gtk_drag_dest_get_target_list (widget));
6295   if (*target == GDK_NONE)
6296     return FALSE;
6297 
6298   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos))
6299     {
6300       gint n_children;
6301       GtkTreeModel *model;
6302 
6303       /* the row got dropped on empty space, let's setup a special case
6304        */
6305 
6306       if (path)
6307 	gtk_tree_path_free (path);
6308 
6309       model = gtk_icon_view_get_model (icon_view);
6310 
6311       n_children = gtk_tree_model_iter_n_children (model, NULL);
6312       if (n_children)
6313         {
6314           pos = GTK_ICON_VIEW_DROP_BELOW;
6315           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6316         }
6317       else
6318         {
6319           pos = GTK_ICON_VIEW_DROP_ABOVE;
6320           path = gtk_tree_path_new_from_indices (0, -1);
6321         }
6322 
6323       can_drop = TRUE;
6324 
6325       goto out;
6326     }
6327 
6328   g_assert (path);
6329 
6330   gtk_icon_view_get_drag_dest_item (icon_view,
6331 				    &old_dest_path,
6332 				    &old_pos);
6333 
6334   if (old_dest_path)
6335     gtk_tree_path_free (old_dest_path);
6336 
6337   if (TRUE /* FIXME if the location droppable predicate */)
6338     {
6339       can_drop = TRUE;
6340     }
6341 
6342 out:
6343   if (can_drop)
6344     {
6345       GtkWidget *source_widget;
6346 
6347       *suggested_action = gdk_drag_context_get_suggested_action (context);
6348       source_widget = gtk_drag_get_source_widget (context);
6349 
6350       if (source_widget == widget)
6351         {
6352           /* Default to MOVE, unless the user has
6353            * pressed ctrl or shift to affect available actions
6354            */
6355           if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6356             *suggested_action = GDK_ACTION_MOVE;
6357         }
6358 
6359       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6360 					path, pos);
6361     }
6362   else
6363     {
6364       /* can't drop here */
6365       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6366 					NULL,
6367 					GTK_ICON_VIEW_DROP_LEFT);
6368     }
6369 
6370   if (path)
6371     gtk_tree_path_free (path);
6372 
6373   return TRUE;
6374 }
6375 
6376 static GtkTreePath*
get_logical_destination(GtkIconView * icon_view,gboolean * drop_append_mode)6377 get_logical_destination (GtkIconView *icon_view,
6378 			 gboolean    *drop_append_mode)
6379 {
6380   /* adjust path to point to the row the drop goes in front of */
6381   GtkTreePath *path = NULL;
6382   GtkIconViewDropPosition pos;
6383 
6384   *drop_append_mode = FALSE;
6385 
6386   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6387 
6388   if (path == NULL)
6389     return NULL;
6390 
6391   if (pos == GTK_ICON_VIEW_DROP_RIGHT ||
6392       pos == GTK_ICON_VIEW_DROP_BELOW)
6393     {
6394       GtkTreeIter iter;
6395       GtkTreeModel *model = icon_view->priv->model;
6396 
6397       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6398           !gtk_tree_model_iter_next (model, &iter))
6399         *drop_append_mode = TRUE;
6400       else
6401         {
6402           *drop_append_mode = FALSE;
6403           gtk_tree_path_next (path);
6404         }
6405     }
6406 
6407   return path;
6408 }
6409 
6410 static gboolean
gtk_icon_view_maybe_begin_drag(GtkIconView * icon_view,GdkEventMotion * event)6411 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
6412 				GdkEventMotion *event)
6413 {
6414   GtkWidget *widget = GTK_WIDGET (icon_view);
6415   GdkDragContext *context;
6416   GtkTreePath *path = NULL;
6417   gint button;
6418   GtkTreeModel *model;
6419   gboolean retval = FALSE;
6420 
6421   if (!icon_view->priv->source_set)
6422     goto out;
6423 
6424   if (icon_view->priv->pressed_button < 0)
6425     goto out;
6426 
6427   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6428                                  icon_view->priv->press_start_x,
6429                                  icon_view->priv->press_start_y,
6430                                  event->x, event->y))
6431     goto out;
6432 
6433   model = gtk_icon_view_get_model (icon_view);
6434 
6435   if (model == NULL)
6436     goto out;
6437 
6438   button = icon_view->priv->pressed_button;
6439   icon_view->priv->pressed_button = -1;
6440 
6441   path = gtk_icon_view_get_path_at_pos (icon_view,
6442 					icon_view->priv->press_start_x,
6443 					icon_view->priv->press_start_y);
6444 
6445   if (path == NULL)
6446     goto out;
6447 
6448   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6449       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6450 					   path))
6451     goto out;
6452 
6453   /* FIXME Check whether we're a start button, if not return FALSE and
6454    * free path
6455    */
6456 
6457   /* Now we can begin the drag */
6458 
6459   retval = TRUE;
6460 
6461   context = gtk_drag_begin_with_coordinates (widget,
6462                                              gtk_drag_source_get_target_list (widget),
6463                                              icon_view->priv->source_actions,
6464                                              button,
6465                                              (GdkEvent*)event,
6466                                              icon_view->priv->press_start_x,
6467                                              icon_view->priv->press_start_y);
6468 
6469   set_source_row (context, model, path);
6470 
6471  out:
6472   if (path)
6473     gtk_tree_path_free (path);
6474 
6475   return retval;
6476 }
6477 
6478 /* Source side drag signals */
6479 static void
gtk_icon_view_drag_begin(GtkWidget * widget,GdkDragContext * context)6480 gtk_icon_view_drag_begin (GtkWidget      *widget,
6481 			  GdkDragContext *context)
6482 {
6483   GtkIconView *icon_view;
6484   GtkIconViewItem *item;
6485   cairo_surface_t *icon;
6486   gint x, y;
6487   GtkTreePath *path;
6488   double sx, sy;
6489 
6490   icon_view = GTK_ICON_VIEW (widget);
6491 
6492   /* if the user uses a custom DnD impl, we don't set the icon here */
6493   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6494     return;
6495 
6496   item = _gtk_icon_view_get_item_at_coords (icon_view,
6497 					   icon_view->priv->press_start_x,
6498 					   icon_view->priv->press_start_y,
6499 					   TRUE,
6500 					   NULL);
6501 
6502   g_return_if_fail (item != NULL);
6503 
6504   x = icon_view->priv->press_start_x - item->cell_area.x + icon_view->priv->item_padding;
6505   y = icon_view->priv->press_start_y - item->cell_area.y + icon_view->priv->item_padding;
6506 
6507   path = gtk_tree_path_new_from_indices (item->index, -1);
6508   icon = gtk_icon_view_create_drag_icon (icon_view, path);
6509   gtk_tree_path_free (path);
6510 
6511   cairo_surface_get_device_scale (icon, &sx, &sy);
6512   cairo_surface_set_device_offset (icon, -x * sx, -y * sy);
6513 
6514   gtk_drag_set_icon_surface (context, icon);
6515 
6516   cairo_surface_destroy (icon);
6517 }
6518 
6519 static void
gtk_icon_view_drag_end(GtkWidget * widget,GdkDragContext * context)6520 gtk_icon_view_drag_end (GtkWidget      *widget,
6521 			GdkDragContext *context)
6522 {
6523   /* do nothing */
6524 }
6525 
6526 static void
gtk_icon_view_drag_data_get(GtkWidget * widget,GdkDragContext * context,GtkSelectionData * selection_data,guint info,guint time)6527 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6528 			     GdkDragContext   *context,
6529 			     GtkSelectionData *selection_data,
6530 			     guint             info,
6531 			     guint             time)
6532 {
6533   GtkIconView *icon_view;
6534   GtkTreeModel *model;
6535   GtkTreePath *source_row;
6536 
6537   icon_view = GTK_ICON_VIEW (widget);
6538   model = gtk_icon_view_get_model (icon_view);
6539 
6540   if (model == NULL)
6541     return;
6542 
6543   if (!icon_view->priv->source_set)
6544     return;
6545 
6546   source_row = get_source_row (context);
6547 
6548   if (source_row == NULL)
6549     return;
6550 
6551   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6552    * any model; for DragSource models there are some other targets
6553    * we also support.
6554    */
6555 
6556   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6557       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6558                                           source_row,
6559                                           selection_data))
6560     goto done;
6561 
6562   /* If drag_data_get does nothing, try providing row data. */
6563   if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6564     gtk_tree_set_row_drag_data (selection_data,
6565 				model,
6566 				source_row);
6567 
6568  done:
6569   gtk_tree_path_free (source_row);
6570 }
6571 
6572 static void
gtk_icon_view_drag_data_delete(GtkWidget * widget,GdkDragContext * context)6573 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6574 				GdkDragContext *context)
6575 {
6576   GtkTreeModel *model;
6577   GtkIconView *icon_view;
6578   GtkTreePath *source_row;
6579 
6580   icon_view = GTK_ICON_VIEW (widget);
6581   model = gtk_icon_view_get_model (icon_view);
6582 
6583   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6584     return;
6585 
6586   if (!icon_view->priv->source_set)
6587     return;
6588 
6589   source_row = get_source_row (context);
6590 
6591   if (source_row == NULL)
6592     return;
6593 
6594   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6595                                          source_row);
6596 
6597   gtk_tree_path_free (source_row);
6598 
6599   set_source_row (context, NULL, NULL);
6600 }
6601 
6602 /* Target side drag signals */
6603 static void
gtk_icon_view_drag_leave(GtkWidget * widget,GdkDragContext * context,guint time)6604 gtk_icon_view_drag_leave (GtkWidget      *widget,
6605 			  GdkDragContext *context,
6606 			  guint           time)
6607 {
6608   GtkIconView *icon_view;
6609 
6610   icon_view = GTK_ICON_VIEW (widget);
6611 
6612   /* unset any highlight row */
6613   gtk_icon_view_set_drag_dest_item (icon_view,
6614 				    NULL,
6615 				    GTK_ICON_VIEW_DROP_LEFT);
6616 
6617   remove_scroll_timeout (icon_view);
6618 }
6619 
6620 static gboolean
gtk_icon_view_drag_motion(GtkWidget * widget,GdkDragContext * context,gint x,gint y,guint time)6621 gtk_icon_view_drag_motion (GtkWidget      *widget,
6622 			   GdkDragContext *context,
6623 			   gint            x,
6624 			   gint            y,
6625 			   guint           time)
6626 {
6627   GtkTreePath *path = NULL;
6628   GtkIconViewDropPosition pos;
6629   GtkIconView *icon_view;
6630   GdkDragAction suggested_action = 0;
6631   GdkAtom target;
6632   gboolean empty;
6633 
6634   icon_view = GTK_ICON_VIEW (widget);
6635 
6636   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6637     return FALSE;
6638 
6639   icon_view->priv->event_last_x = x;
6640   icon_view->priv->event_last_y = y;
6641 
6642   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6643 
6644   /* we only know this *after* set_desination_row */
6645   empty = icon_view->priv->empty_view_drop;
6646 
6647   if (path == NULL && !empty)
6648     {
6649       /* Can't drop here. */
6650       gdk_drag_status (context, 0, time);
6651     }
6652   else
6653     {
6654       if (icon_view->priv->scroll_timeout_id == 0)
6655 	{
6656 	  icon_view->priv->scroll_timeout_id =
6657 	    gdk_threads_add_timeout (50, drag_scroll_timeout, icon_view);
6658 	  g_source_set_name_by_id (icon_view->priv->scroll_timeout_id, "[gtk+] drag_scroll_timeout");
6659 	}
6660 
6661       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6662         {
6663           /* Request data so we can use the source row when
6664            * determining whether to accept the drop
6665            */
6666           set_status_pending (context, suggested_action);
6667           gtk_drag_get_data (widget, context, target, time);
6668         }
6669       else
6670         {
6671           set_status_pending (context, 0);
6672           gdk_drag_status (context, suggested_action, time);
6673         }
6674     }
6675 
6676   if (path)
6677     gtk_tree_path_free (path);
6678 
6679   return TRUE;
6680 }
6681 
6682 static gboolean
gtk_icon_view_drag_drop(GtkWidget * widget,GdkDragContext * context,gint x,gint y,guint time)6683 gtk_icon_view_drag_drop (GtkWidget      *widget,
6684 			 GdkDragContext *context,
6685 			 gint            x,
6686 			 gint            y,
6687 			 guint           time)
6688 {
6689   GtkIconView *icon_view;
6690   GtkTreePath *path;
6691   GdkDragAction suggested_action = 0;
6692   GdkAtom target = GDK_NONE;
6693   GtkTreeModel *model;
6694   gboolean drop_append_mode;
6695 
6696   icon_view = GTK_ICON_VIEW (widget);
6697   model = gtk_icon_view_get_model (icon_view);
6698 
6699   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6700 
6701   if (!icon_view->priv->dest_set)
6702     return FALSE;
6703 
6704   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6705     return FALSE;
6706 
6707   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6708     return FALSE;
6709 
6710   path = get_logical_destination (icon_view, &drop_append_mode);
6711 
6712   if (target != GDK_NONE && path != NULL)
6713     {
6714       /* in case a motion had requested drag data, change things so we
6715        * treat drag data receives as a drop.
6716        */
6717       set_status_pending (context, 0);
6718       set_dest_row (context, model, path,
6719 		    icon_view->priv->empty_view_drop, drop_append_mode);
6720     }
6721 
6722   if (path)
6723     gtk_tree_path_free (path);
6724 
6725   /* Unset this thing */
6726   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6727 
6728   if (target != GDK_NONE)
6729     {
6730       gtk_drag_get_data (widget, context, target, time);
6731       return TRUE;
6732     }
6733   else
6734     return FALSE;
6735 }
6736 
6737 static void
gtk_icon_view_drag_data_received(GtkWidget * widget,GdkDragContext * context,gint x,gint y,GtkSelectionData * selection_data,guint info,guint time)6738 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6739 				  GdkDragContext   *context,
6740 				  gint              x,
6741 				  gint              y,
6742 				  GtkSelectionData *selection_data,
6743 				  guint             info,
6744 				  guint             time)
6745 {
6746   GtkTreePath *path;
6747   gboolean accepted = FALSE;
6748   GtkTreeModel *model;
6749   GtkIconView *icon_view;
6750   GtkTreePath *dest_row;
6751   GdkDragAction suggested_action;
6752   gboolean drop_append_mode;
6753 
6754   icon_view = GTK_ICON_VIEW (widget);
6755   model = gtk_icon_view_get_model (icon_view);
6756 
6757   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6758     return;
6759 
6760   if (!icon_view->priv->dest_set)
6761     return;
6762 
6763   suggested_action = get_status_pending (context);
6764 
6765   if (suggested_action)
6766     {
6767       /* We are getting this data due to a request in drag_motion,
6768        * rather than due to a request in drag_drop, so we are just
6769        * supposed to call drag_status, not actually paste in the
6770        * data.
6771        */
6772       path = get_logical_destination (icon_view, &drop_append_mode);
6773 
6774       if (path == NULL)
6775         suggested_action = 0;
6776 
6777       if (suggested_action)
6778         {
6779 	  if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6780 						     path,
6781 						     selection_data))
6782 	    suggested_action = 0;
6783         }
6784 
6785       gdk_drag_status (context, suggested_action, time);
6786 
6787       if (path)
6788         gtk_tree_path_free (path);
6789 
6790       /* If you can't drop, remove user drop indicator until the next motion */
6791       if (suggested_action == 0)
6792         gtk_icon_view_set_drag_dest_item (icon_view,
6793 					  NULL,
6794 					  GTK_ICON_VIEW_DROP_LEFT);
6795       return;
6796     }
6797 
6798 
6799   dest_row = get_dest_row (context);
6800 
6801   if (dest_row == NULL)
6802     return;
6803 
6804   if (gtk_selection_data_get_length (selection_data) >= 0)
6805     {
6806       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6807                                                  dest_row,
6808                                                  selection_data))
6809         accepted = TRUE;
6810     }
6811 
6812   gtk_drag_finish (context,
6813                    accepted,
6814                    (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
6815                    time);
6816 
6817   gtk_tree_path_free (dest_row);
6818 
6819   /* drop dest_row */
6820   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6821 }
6822 
6823 /* Drag-and-Drop support */
6824 /**
6825  * gtk_icon_view_enable_model_drag_source:
6826  * @icon_view: a #GtkIconView
6827  * @start_button_mask: Mask of allowed buttons to start drag
6828  * @targets: (array length=n_targets): the table of targets that the drag will
6829  *           support
6830  * @n_targets: the number of items in @targets
6831  * @actions: the bitmask of possible actions for a drag from this
6832  *    widget
6833  *
6834  * Turns @icon_view into a drag source for automatic DND. Calling this
6835  * method sets #GtkIconView:reorderable to %FALSE.
6836  *
6837  * Since: 2.8
6838  **/
6839 void
gtk_icon_view_enable_model_drag_source(GtkIconView * icon_view,GdkModifierType start_button_mask,const GtkTargetEntry * targets,gint n_targets,GdkDragAction actions)6840 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6841 					GdkModifierType           start_button_mask,
6842 					const GtkTargetEntry     *targets,
6843 					gint                      n_targets,
6844 					GdkDragAction             actions)
6845 {
6846   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6847 
6848   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6849 
6850   icon_view->priv->start_button_mask = start_button_mask;
6851   icon_view->priv->source_actions = actions;
6852 
6853   icon_view->priv->source_set = TRUE;
6854 
6855   unset_reorderable (icon_view);
6856 }
6857 
6858 /**
6859  * gtk_icon_view_enable_model_drag_dest:
6860  * @icon_view: a #GtkIconView
6861  * @targets: (array length=n_targets): the table of targets that the drag will
6862  *           support
6863  * @n_targets: the number of items in @targets
6864  * @actions: the bitmask of possible actions for a drag to this
6865  *    widget
6866  *
6867  * Turns @icon_view into a drop destination for automatic DND. Calling this
6868  * method sets #GtkIconView:reorderable to %FALSE.
6869  *
6870  * Since: 2.8
6871  **/
6872 void
gtk_icon_view_enable_model_drag_dest(GtkIconView * icon_view,const GtkTargetEntry * targets,gint n_targets,GdkDragAction actions)6873 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6874 				      const GtkTargetEntry *targets,
6875 				      gint                  n_targets,
6876 				      GdkDragAction         actions)
6877 {
6878   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6879 
6880   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6881 
6882   icon_view->priv->dest_actions = actions;
6883 
6884   icon_view->priv->dest_set = TRUE;
6885 
6886   unset_reorderable (icon_view);
6887 }
6888 
6889 /**
6890  * gtk_icon_view_unset_model_drag_source:
6891  * @icon_view: a #GtkIconView
6892  *
6893  * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
6894  * method sets #GtkIconView:reorderable to %FALSE.
6895  *
6896  * Since: 2.8
6897  **/
6898 void
gtk_icon_view_unset_model_drag_source(GtkIconView * icon_view)6899 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6900 {
6901   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6902 
6903   if (icon_view->priv->source_set)
6904     {
6905       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6906       icon_view->priv->source_set = FALSE;
6907     }
6908 
6909   unset_reorderable (icon_view);
6910 }
6911 
6912 /**
6913  * gtk_icon_view_unset_model_drag_dest:
6914  * @icon_view: a #GtkIconView
6915  *
6916  * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
6917  * method sets #GtkIconView:reorderable to %FALSE.
6918  *
6919  * Since: 2.8
6920  **/
6921 void
gtk_icon_view_unset_model_drag_dest(GtkIconView * icon_view)6922 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6923 {
6924   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6925 
6926   if (icon_view->priv->dest_set)
6927     {
6928       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6929       icon_view->priv->dest_set = FALSE;
6930     }
6931 
6932   unset_reorderable (icon_view);
6933 }
6934 
6935 /* These are useful to implement your own custom stuff. */
6936 /**
6937  * gtk_icon_view_set_drag_dest_item:
6938  * @icon_view: a #GtkIconView
6939  * @path: (allow-none): The path of the item to highlight, or %NULL.
6940  * @pos: Specifies where to drop, relative to the item
6941  *
6942  * Sets the item that is highlighted for feedback.
6943  *
6944  * Since: 2.8
6945  */
6946 void
gtk_icon_view_set_drag_dest_item(GtkIconView * icon_view,GtkTreePath * path,GtkIconViewDropPosition pos)6947 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6948 				  GtkTreePath              *path,
6949 				  GtkIconViewDropPosition   pos)
6950 {
6951   /* Note; this function is exported to allow a custom DND
6952    * implementation, so it can't touch TreeViewDragInfo
6953    */
6954 
6955   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6956 
6957   if (icon_view->priv->dest_item)
6958     {
6959       GtkTreePath *current_path;
6960       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6961       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6962       icon_view->priv->dest_item = NULL;
6963 
6964       gtk_icon_view_queue_draw_path (icon_view, current_path);
6965       gtk_tree_path_free (current_path);
6966     }
6967 
6968   /* special case a drop on an empty model */
6969   icon_view->priv->empty_view_drop = FALSE;
6970   if (pos == GTK_ICON_VIEW_DROP_ABOVE && path
6971       && gtk_tree_path_get_depth (path) == 1
6972       && gtk_tree_path_get_indices (path)[0] == 0)
6973     {
6974       gint n_children;
6975 
6976       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6977                                                    NULL);
6978 
6979       if (n_children == 0)
6980         icon_view->priv->empty_view_drop = TRUE;
6981     }
6982 
6983   icon_view->priv->dest_pos = pos;
6984 
6985   if (path)
6986     {
6987       icon_view->priv->dest_item =
6988         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view),
6989 					  icon_view->priv->model, path);
6990 
6991       gtk_icon_view_queue_draw_path (icon_view, path);
6992     }
6993 }
6994 
6995 /**
6996  * gtk_icon_view_get_drag_dest_item:
6997  * @icon_view: a #GtkIconView
6998  * @path: (out) (allow-none): Return location for the path of
6999  *        the highlighted item, or %NULL.
7000  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
7001  *
7002  * Gets information about the item that is highlighted for feedback.
7003  *
7004  * Since: 2.8
7005  **/
7006 void
gtk_icon_view_get_drag_dest_item(GtkIconView * icon_view,GtkTreePath ** path,GtkIconViewDropPosition * pos)7007 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
7008 				  GtkTreePath             **path,
7009 				  GtkIconViewDropPosition  *pos)
7010 {
7011   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7012 
7013   if (path)
7014     {
7015       if (icon_view->priv->dest_item)
7016         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
7017       else
7018 	*path = NULL;
7019     }
7020 
7021   if (pos)
7022     *pos = icon_view->priv->dest_pos;
7023 }
7024 
7025 /**
7026  * gtk_icon_view_get_dest_item_at_pos:
7027  * @icon_view: a #GtkIconView
7028  * @drag_x: the position to determine the destination item for
7029  * @drag_y: the position to determine the destination item for
7030  * @path: (out) (allow-none): Return location for the path of the item,
7031  *    or %NULL.
7032  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
7033  *
7034  * Determines the destination item for a given position.
7035  *
7036  * Returns: whether there is an item at the given position.
7037  *
7038  * Since: 2.8
7039  **/
7040 gboolean
gtk_icon_view_get_dest_item_at_pos(GtkIconView * icon_view,gint drag_x,gint drag_y,GtkTreePath ** path,GtkIconViewDropPosition * pos)7041 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
7042 				    gint                      drag_x,
7043 				    gint                      drag_y,
7044 				    GtkTreePath             **path,
7045 				    GtkIconViewDropPosition  *pos)
7046 {
7047   GtkIconViewItem *item;
7048 
7049   /* Note; this function is exported to allow a custom DND
7050    * implementation, so it can't touch TreeViewDragInfo
7051    */
7052 
7053   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7054   g_return_val_if_fail (drag_x >= 0, FALSE);
7055   g_return_val_if_fail (drag_y >= 0, FALSE);
7056   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
7057 
7058 
7059   if (path)
7060     *path = NULL;
7061 
7062   item = _gtk_icon_view_get_item_at_coords (icon_view,
7063 					   drag_x + gtk_adjustment_get_value (icon_view->priv->hadjustment),
7064 					   drag_y + gtk_adjustment_get_value (icon_view->priv->vadjustment),
7065 					   FALSE, NULL);
7066 
7067   if (item == NULL)
7068     return FALSE;
7069 
7070   if (path)
7071     *path = gtk_tree_path_new_from_indices (item->index, -1);
7072 
7073   if (pos)
7074     {
7075       if (drag_x < item->cell_area.x + item->cell_area.width / 4)
7076 	*pos = GTK_ICON_VIEW_DROP_LEFT;
7077       else if (drag_x > item->cell_area.x + item->cell_area.width * 3 / 4)
7078 	*pos = GTK_ICON_VIEW_DROP_RIGHT;
7079       else if (drag_y < item->cell_area.y + item->cell_area.height / 4)
7080 	*pos = GTK_ICON_VIEW_DROP_ABOVE;
7081       else if (drag_y > item->cell_area.y + item->cell_area.height * 3 / 4)
7082 	*pos = GTK_ICON_VIEW_DROP_BELOW;
7083       else
7084 	*pos = GTK_ICON_VIEW_DROP_INTO;
7085     }
7086 
7087   return TRUE;
7088 }
7089 
7090 /**
7091  * gtk_icon_view_create_drag_icon:
7092  * @icon_view: a #GtkIconView
7093  * @path: a #GtkTreePath in @icon_view
7094  *
7095  * Creates a #cairo_surface_t representation of the item at @path.
7096  * This image is used for a drag icon.
7097  *
7098  * Returns: (transfer full): a newly-allocated surface of the drag icon.
7099  *
7100  * Since: 2.8
7101  **/
7102 cairo_surface_t *
gtk_icon_view_create_drag_icon(GtkIconView * icon_view,GtkTreePath * path)7103 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
7104 				GtkTreePath *path)
7105 {
7106   GtkWidget *widget;
7107   cairo_t *cr;
7108   cairo_surface_t *surface;
7109   GList *l;
7110   gint index;
7111 
7112   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
7113   g_return_val_if_fail (path != NULL, NULL);
7114 
7115   widget = GTK_WIDGET (icon_view);
7116 
7117   if (!gtk_widget_get_realized (widget))
7118     return NULL;
7119 
7120   index = gtk_tree_path_get_indices (path)[0];
7121 
7122   for (l = icon_view->priv->items; l; l = l->next)
7123     {
7124       GtkIconViewItem *item = l->data;
7125 
7126       if (index == item->index)
7127 	{
7128 	  GdkRectangle rect = {
7129 	    item->cell_area.x - icon_view->priv->item_padding,
7130 	    item->cell_area.y - icon_view->priv->item_padding,
7131 	    item->cell_area.width  + icon_view->priv->item_padding * 2,
7132 	    item->cell_area.height + icon_view->priv->item_padding * 2
7133 	  };
7134 
7135 	  surface = gdk_window_create_similar_surface (icon_view->priv->bin_window,
7136                                                        CAIRO_CONTENT_COLOR_ALPHA,
7137                                                        rect.width,
7138                                                        rect.height);
7139 
7140 	  cr = cairo_create (surface);
7141 
7142 	  gtk_icon_view_paint_item (icon_view, cr, item,
7143 				    icon_view->priv->item_padding,
7144 				    icon_view->priv->item_padding,
7145                                     FALSE);
7146 
7147 	  cairo_destroy (cr);
7148 
7149 	  return surface;
7150 	}
7151     }
7152 
7153   return NULL;
7154 }
7155 
7156 /**
7157  * gtk_icon_view_get_reorderable:
7158  * @icon_view: a #GtkIconView
7159  *
7160  * Retrieves whether the user can reorder the list via drag-and-drop.
7161  * See gtk_icon_view_set_reorderable().
7162  *
7163  * Returns: %TRUE if the list can be reordered.
7164  *
7165  * Since: 2.8
7166  **/
7167 gboolean
gtk_icon_view_get_reorderable(GtkIconView * icon_view)7168 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
7169 {
7170   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7171 
7172   return icon_view->priv->reorderable;
7173 }
7174 
7175 static const GtkTargetEntry item_targets[] = {
7176   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
7177 };
7178 
7179 
7180 /**
7181  * gtk_icon_view_set_reorderable:
7182  * @icon_view: A #GtkIconView.
7183  * @reorderable: %TRUE, if the list of items can be reordered.
7184  *
7185  * This function is a convenience function to allow you to reorder models that
7186  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
7187  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
7188  * the user can reorder the model by dragging and dropping rows.  The
7189  * developer can listen to these changes by connecting to the model's
7190  * row_inserted and row_deleted signals. The reordering is implemented by setting up
7191  * the icon view as a drag source and destination. Therefore, drag and
7192  * drop can not be used in a reorderable view for any other purpose.
7193  *
7194  * This function does not give you any degree of control over the order -- any
7195  * reordering is allowed.  If more control is needed, you should probably
7196  * handle drag and drop manually.
7197  *
7198  * Since: 2.8
7199  **/
7200 void
gtk_icon_view_set_reorderable(GtkIconView * icon_view,gboolean reorderable)7201 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
7202 			       gboolean     reorderable)
7203 {
7204   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7205 
7206   reorderable = reorderable != FALSE;
7207 
7208   if (icon_view->priv->reorderable == reorderable)
7209     return;
7210 
7211   if (reorderable)
7212     {
7213       gtk_icon_view_enable_model_drag_source (icon_view,
7214 					      GDK_BUTTON1_MASK,
7215 					      item_targets,
7216 					      G_N_ELEMENTS (item_targets),
7217 					      GDK_ACTION_MOVE);
7218       gtk_icon_view_enable_model_drag_dest (icon_view,
7219 					    item_targets,
7220 					    G_N_ELEMENTS (item_targets),
7221 					    GDK_ACTION_MOVE);
7222     }
7223   else
7224     {
7225       gtk_icon_view_unset_model_drag_source (icon_view);
7226       gtk_icon_view_unset_model_drag_dest (icon_view);
7227     }
7228 
7229   icon_view->priv->reorderable = reorderable;
7230 
7231   g_object_notify (G_OBJECT (icon_view), "reorderable");
7232 }
7233 
7234 /**
7235  * gtk_icon_view_set_activate_on_single_click:
7236  * @icon_view: a #GtkIconView
7237  * @single: %TRUE to emit item-activated on a single click
7238  *
7239  * Causes the #GtkIconView::item-activated signal to be emitted on
7240  * a single click instead of a double click.
7241  *
7242  * Since: 3.8
7243  **/
7244 void
gtk_icon_view_set_activate_on_single_click(GtkIconView * icon_view,gboolean single)7245 gtk_icon_view_set_activate_on_single_click (GtkIconView *icon_view,
7246                                             gboolean     single)
7247 {
7248   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7249 
7250   single = single != FALSE;
7251 
7252   if (icon_view->priv->activate_on_single_click == single)
7253     return;
7254 
7255   icon_view->priv->activate_on_single_click = single;
7256   g_object_notify (G_OBJECT (icon_view), "activate-on-single-click");
7257 }
7258 
7259 /**
7260  * gtk_icon_view_get_activate_on_single_click:
7261  * @icon_view: a #GtkIconView
7262  *
7263  * Gets the setting set by gtk_icon_view_set_activate_on_single_click().
7264  *
7265  * Returns: %TRUE if item-activated will be emitted on a single click
7266  *
7267  * Since: 3.8
7268  **/
7269 gboolean
gtk_icon_view_get_activate_on_single_click(GtkIconView * icon_view)7270 gtk_icon_view_get_activate_on_single_click (GtkIconView *icon_view)
7271 {
7272   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7273 
7274   return icon_view->priv->activate_on_single_click;
7275 }
7276 
7277 static gboolean
gtk_icon_view_buildable_custom_tag_start(GtkBuildable * buildable,GtkBuilder * builder,GObject * child,const gchar * tagname,GMarkupParser * parser,gpointer * data)7278 gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
7279                                           GtkBuilder    *builder,
7280                                           GObject       *child,
7281                                           const gchar   *tagname,
7282                                           GMarkupParser *parser,
7283                                           gpointer      *data)
7284 {
7285   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
7286                                                 tagname, parser, data))
7287     return TRUE;
7288 
7289   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
7290                                                       tagname, parser, data);
7291 }
7292 
7293 static void
gtk_icon_view_buildable_custom_tag_end(GtkBuildable * buildable,GtkBuilder * builder,GObject * child,const gchar * tagname,gpointer * data)7294 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
7295                                         GtkBuilder   *builder,
7296                                         GObject      *child,
7297                                         const gchar  *tagname,
7298                                         gpointer     *data)
7299 {
7300   if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder,
7301                                                   child, tagname, data))
7302     parent_buildable_iface->custom_tag_end (buildable, builder,
7303                                             child, tagname, data);
7304 }
7305