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