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