]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
iconview: Ensure icons are at least 1x1 pixels
[~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 void
1460 gtk_icon_view_get_preferred_item_size (GtkIconView    *icon_view,
1461                                        GtkOrientation  orientation,
1462                                        gint            for_size,
1463                                        gint           *minimum,
1464                                        gint           *natural)
1465 {
1466   GtkIconViewPrivate *priv = icon_view->priv;
1467   GtkCellAreaContext *context;
1468   GList *items;
1469
1470   if (priv->items == NULL)
1471     {
1472       *minimum = 1;
1473       *natural = 1;
1474       return;
1475     }
1476
1477   context = gtk_cell_area_create_context (priv->cell_area);
1478
1479   for_size -= 2 * priv->item_padding;
1480
1481   if (for_size > 0)
1482     {
1483       /* This is necessary for the context to work properly */
1484       for (items = priv->items; items; items = items->next)
1485         {
1486           GtkIconViewItem *item = items->data;
1487
1488           _gtk_icon_view_set_cell_data (icon_view, item);
1489           cell_area_get_preferred_size (icon_view, context, 1 - orientation, -1, NULL, NULL);
1490         }
1491     }
1492
1493   for (items = priv->items; items; items = items->next)
1494     {
1495       GtkIconViewItem *item = items->data;
1496
1497       _gtk_icon_view_set_cell_data (icon_view, item);
1498       if (items == priv->items)
1499         adjust_wrap_width (icon_view);
1500       cell_area_get_preferred_size (icon_view, context, orientation, for_size, NULL, NULL);
1501     }
1502
1503   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1504     {
1505       if (for_size > 0)
1506         gtk_cell_area_context_get_preferred_width_for_height (context,
1507                                                               for_size,
1508                                                               minimum, natural);
1509       else
1510         gtk_cell_area_context_get_preferred_width (context,
1511                                                    minimum, natural);
1512     }
1513   else
1514     {
1515       if (for_size > 0)
1516         gtk_cell_area_context_get_preferred_height_for_width (context,
1517                                                               for_size,
1518                                                               minimum, natural);
1519       else
1520         gtk_cell_area_context_get_preferred_height (context,
1521                                                     minimum, natural);
1522     }
1523
1524   if (orientation == GTK_ORIENTATION_HORIZONTAL && priv->item_width >= 0)
1525     {
1526       if (minimum)
1527         *minimum = MAX (*minimum, priv->item_width);
1528       if (natural)
1529         *natural = *minimum;
1530     }
1531
1532   if (minimum)
1533     *minimum = MAX (1, *minimum + 2 * priv->item_padding);
1534   if (natural)
1535     *natural = MAX (1, *natural + 2 * priv->item_padding);
1536
1537   g_object_unref (context);
1538 }
1539
1540 static void
1541 gtk_icon_view_compute_n_items_for_size (GtkIconView    *icon_view,
1542                                         GtkOrientation  orientation,
1543                                         gint            size,
1544                                         gint           *min_items,
1545                                         gint           *min_item_size,
1546                                         gint           *max_items,
1547                                         gint           *max_item_size)
1548 {
1549   GtkIconViewPrivate *priv = icon_view->priv;
1550   int minimum, natural, spacing;
1551
1552   g_return_if_fail (min_item_size == NULL || min_items != NULL);
1553   g_return_if_fail (max_item_size == NULL || max_items != NULL);
1554
1555   gtk_icon_view_get_preferred_item_size (icon_view, orientation, -1, &minimum, &natural);
1556
1557   if (orientation == GTK_ORIENTATION_HORIZONTAL)
1558     spacing = priv->column_spacing;
1559   else
1560     spacing = priv->row_spacing;
1561   
1562   size -= 2 * priv->margin;
1563   size += spacing;
1564   minimum += spacing;
1565   natural += spacing;
1566
1567   if (priv->columns > 0)
1568     {
1569       if (orientation == GTK_ORIENTATION_HORIZONTAL)
1570         {
1571           if (min_items)
1572             *min_items = priv->columns;
1573           if (max_items)
1574             *max_items = priv->columns;
1575         }
1576       else
1577         {
1578           int n_items = gtk_icon_view_get_n_items (icon_view);
1579
1580           if (min_items)
1581             *min_items = (n_items + priv->columns - 1) / priv->columns;
1582           if (max_items)
1583             *max_items = (n_items + priv->columns - 1) / priv->columns;
1584         }
1585     }
1586   else
1587     {
1588       if (max_items)
1589         {
1590           if (size <= minimum)
1591             *max_items = 1;
1592           else
1593             *max_items = size / minimum;
1594         }
1595
1596       if (min_items)
1597         {
1598           if (size <= natural)
1599             *min_items = 1;
1600           else
1601             *min_items = size / natural;
1602         }
1603     }
1604
1605   if (min_item_size)
1606     {
1607       *min_item_size = size / *min_items;
1608       *min_item_size = CLAMP (*min_item_size, minimum, natural);
1609       *min_item_size -= spacing;
1610       *min_item_size -= 2 * priv->item_padding;
1611     }
1612
1613   if (max_item_size)
1614     {
1615       *max_item_size = size / *max_items;
1616       *max_item_size = CLAMP (*max_item_size, minimum, natural);
1617       *max_item_size -= spacing;
1618       *max_item_size -= 2 * priv->item_padding;
1619     }
1620 }
1621
1622 static GtkSizeRequestMode
1623 gtk_icon_view_get_request_mode (GtkWidget *widget)
1624 {
1625   return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
1626 }
1627
1628 static void
1629 gtk_icon_view_get_preferred_width (GtkWidget *widget,
1630                                    gint      *minimum,
1631                                    gint      *natural)
1632 {
1633   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1634   GtkIconViewPrivate *priv = icon_view->priv;
1635   int item_min, item_nat;
1636
1637   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, -1, &item_min, &item_nat);
1638
1639   if (priv->columns > 0)
1640     {
1641       *minimum = item_min * priv->columns + priv->column_spacing * (priv->columns - 1);
1642       *natural = item_nat * priv->columns + priv->column_spacing * (priv->columns - 1);
1643     }
1644   else
1645     {
1646       int n_items = gtk_icon_view_get_n_items (icon_view);
1647
1648       if (n_items == 0)
1649         {
1650           *minimum = 0;
1651           *natural = 0;
1652         }
1653       else
1654         {
1655           *minimum = item_min;
1656           *natural = item_nat * n_items + priv->column_spacing * (n_items - 1);
1657         }
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   gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_VERTICAL, height, &rows, &row_height, NULL, NULL);
1675   n_items = gtk_icon_view_get_n_items (icon_view);
1676
1677   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_HORIZONTAL, row_height, &item_min, &item_nat);
1678   *minimum = (item_min + priv->column_spacing) * ((n_items + rows - 1) / rows) - priv->column_spacing;
1679   *natural = (item_nat + priv->column_spacing) * ((n_items + rows - 1) / rows) - priv->column_spacing;
1680
1681   *minimum += 2 * priv->margin;
1682   *natural += 2 * priv->margin;
1683 }
1684
1685 static void
1686 gtk_icon_view_get_preferred_height (GtkWidget *widget,
1687                                     gint      *minimum,
1688                                     gint      *natural)
1689 {
1690   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1691   GtkIconViewPrivate *priv = icon_view->priv;
1692   int item_min, item_nat, n_items;
1693
1694   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, -1, &item_min, &item_nat);
1695   n_items = gtk_icon_view_get_n_items (icon_view);
1696
1697   if (priv->columns > 0)
1698     {
1699       int n_rows = (n_items + priv->columns - 1) / priv->columns;
1700
1701       *minimum = item_min * n_rows + priv->row_spacing * (n_rows - 1);
1702       *natural = item_nat * n_rows + priv->row_spacing * (n_rows - 1);
1703     }
1704   else
1705     {
1706       if (n_items == 0)
1707         {
1708           *minimum = 0;
1709           *natural = 0;
1710         }
1711       else
1712         {
1713           *minimum = item_min;
1714           *natural = item_nat * n_items + priv->row_spacing * (n_items - 1);
1715         }
1716     }
1717
1718   *minimum += 2 * priv->margin;
1719   *natural += 2 * priv->margin;
1720 }
1721
1722 static void
1723 gtk_icon_view_get_preferred_height_for_width (GtkWidget *widget,
1724                                               gint       width,
1725                                               gint      *minimum,
1726                                               gint      *natural)
1727 {
1728   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1729   GtkIconViewPrivate *priv = icon_view->priv;
1730   int item_min, item_nat, columns, column_width, n_items;
1731
1732   gtk_icon_view_compute_n_items_for_size (icon_view, GTK_ORIENTATION_HORIZONTAL, width, NULL, NULL, &columns, &column_width);
1733   n_items = gtk_icon_view_get_n_items (icon_view);
1734
1735   gtk_icon_view_get_preferred_item_size (icon_view, GTK_ORIENTATION_VERTICAL, column_width, &item_min, &item_nat);
1736   *minimum = (item_min + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1737   *natural = (item_nat + priv->row_spacing) * ((n_items + columns - 1) / columns) - priv->row_spacing;
1738
1739   *minimum += 2 * priv->margin;
1740   *natural += 2 * priv->margin;
1741 }
1742
1743 static void
1744 gtk_icon_view_allocate_children (GtkIconView *icon_view)
1745 {
1746   GList *list;
1747
1748   for (list = icon_view->priv->children; list; list = list->next)
1749     {
1750       GtkIconViewChild *child = list->data;
1751
1752       /* totally ignore our child's requisition */
1753       gtk_widget_size_allocate (child->widget, &child->area);
1754     }
1755 }
1756
1757 static void
1758 gtk_icon_view_size_allocate (GtkWidget      *widget,
1759                              GtkAllocation  *allocation)
1760 {
1761   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1762
1763   gtk_widget_set_allocation (widget, allocation);
1764
1765   gtk_icon_view_layout (icon_view);
1766
1767   if (gtk_widget_get_realized (widget))
1768     {
1769       gdk_window_move_resize (gtk_widget_get_window (widget),
1770                               allocation->x, allocation->y,
1771                               allocation->width, allocation->height);
1772       gdk_window_resize (icon_view->priv->bin_window,
1773                          MAX (icon_view->priv->width, allocation->width),
1774                          MAX (icon_view->priv->height, allocation->height));
1775     }
1776
1777   gtk_icon_view_allocate_children (icon_view);
1778
1779   /* Delay signal emission */
1780   g_object_freeze_notify (G_OBJECT (icon_view->priv->hadjustment));
1781   g_object_freeze_notify (G_OBJECT (icon_view->priv->vadjustment));
1782
1783   gtk_icon_view_set_hadjustment_values (icon_view);
1784   gtk_icon_view_set_vadjustment_values (icon_view);
1785
1786   if (gtk_widget_get_realized (widget) &&
1787       icon_view->priv->scroll_to_path)
1788     {
1789       GtkTreePath *path;
1790       path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
1791       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1792       icon_view->priv->scroll_to_path = NULL;
1793
1794       gtk_icon_view_scroll_to_path (icon_view, path,
1795                                     icon_view->priv->scroll_to_use_align,
1796                                     icon_view->priv->scroll_to_row_align,
1797                                     icon_view->priv->scroll_to_col_align);
1798       gtk_tree_path_free (path);
1799     }
1800
1801   /* Emit any pending signals now */
1802   g_object_thaw_notify (G_OBJECT (icon_view->priv->hadjustment));
1803   g_object_thaw_notify (G_OBJECT (icon_view->priv->vadjustment));
1804 }
1805
1806 static gboolean
1807 gtk_icon_view_draw (GtkWidget *widget,
1808                     cairo_t   *cr)
1809 {
1810   GtkIconView *icon_view;
1811   GList *icons;
1812   GtkTreePath *path;
1813   gint dest_index;
1814   GtkIconViewDropPosition dest_pos;
1815   GtkIconViewItem *dest_item = NULL;
1816
1817   icon_view = GTK_ICON_VIEW (widget);
1818
1819   if (!gtk_cairo_should_draw_window (cr, icon_view->priv->bin_window))
1820     return FALSE;
1821
1822   cairo_save (cr);
1823
1824   gtk_cairo_transform_to_window (cr, widget, icon_view->priv->bin_window);
1825
1826   cairo_set_line_width (cr, 1.);
1827
1828   gtk_icon_view_get_drag_dest_item (icon_view, &path, &dest_pos);
1829
1830   if (path)
1831     {
1832       dest_index = gtk_tree_path_get_indices (path)[0];
1833       gtk_tree_path_free (path);
1834     }
1835   else
1836     dest_index = -1;
1837
1838   for (icons = icon_view->priv->items; icons; icons = icons->next)
1839     {
1840       GtkIconViewItem *item = icons->data;
1841       GdkRectangle paint_area;
1842
1843       paint_area.x      = item->cell_area.x      - icon_view->priv->item_padding;
1844       paint_area.y      = item->cell_area.y      - icon_view->priv->item_padding;
1845       paint_area.width  = item->cell_area.width  + icon_view->priv->item_padding * 2;
1846       paint_area.height = item->cell_area.height + icon_view->priv->item_padding * 2;
1847       
1848       cairo_save (cr);
1849
1850       cairo_rectangle (cr, paint_area.x, paint_area.y, paint_area.width, paint_area.height);
1851       cairo_clip (cr);
1852
1853       if (gdk_cairo_get_clip_rectangle (cr, NULL))
1854         {
1855           gtk_icon_view_paint_item (icon_view, cr, item,
1856                                     item->cell_area.x, item->cell_area.y,
1857                                     icon_view->priv->draw_focus);
1858
1859           if (dest_index == item->index)
1860             dest_item = item;
1861         }
1862
1863       cairo_restore (cr);
1864     }
1865
1866   if (dest_item &&
1867       dest_pos != GTK_ICON_VIEW_NO_DROP)
1868     {
1869       GtkStyleContext *context;
1870       GdkRectangle rect = { 0 };
1871
1872       context = gtk_widget_get_style_context (widget);
1873
1874       switch (dest_pos)
1875         {
1876         case GTK_ICON_VIEW_DROP_INTO:
1877           rect = dest_item->cell_area;
1878           break;
1879         case GTK_ICON_VIEW_DROP_ABOVE:
1880           rect.x = dest_item->cell_area.x;
1881           rect.y = dest_item->cell_area.y - 1;
1882           rect.width = dest_item->cell_area.width;
1883           rect.height = 2;
1884           break;
1885         case GTK_ICON_VIEW_DROP_LEFT:
1886           rect.x = dest_item->cell_area.x - 1;
1887           rect.y = dest_item->cell_area.y;
1888           rect.width = 2;
1889           rect.height = dest_item->cell_area.height;
1890           break;
1891         case GTK_ICON_VIEW_DROP_BELOW:
1892           rect.x = dest_item->cell_area.x;
1893           rect.y = dest_item->cell_area.y + dest_item->cell_area.height - 1;
1894           rect.width = dest_item->cell_area.width;
1895           rect.height = 2;
1896           break;
1897         case GTK_ICON_VIEW_DROP_RIGHT:
1898           rect.x = dest_item->cell_area.x + dest_item->cell_area.width - 1;
1899           rect.y = dest_item->cell_area.y;
1900           rect.width = 2;
1901           rect.height = dest_item->cell_area.height;
1902         case GTK_ICON_VIEW_NO_DROP: ;
1903           break;
1904         }
1905
1906       gtk_render_focus (context, cr,
1907                         rect.x, rect.y,
1908                         rect.width, rect.height);
1909     }
1910
1911   if (icon_view->priv->doing_rubberband)
1912     gtk_icon_view_paint_rubberband (icon_view, cr);
1913
1914   cairo_restore (cr);
1915
1916   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->draw (widget, cr);
1917 }
1918
1919 static gboolean
1920 rubberband_scroll_timeout (gpointer data)
1921 {
1922   GtkIconView *icon_view = data;
1923
1924   gtk_adjustment_set_value (icon_view->priv->vadjustment,
1925                             gtk_adjustment_get_value (icon_view->priv->vadjustment) +
1926                             icon_view->priv->scroll_value_diff);
1927
1928   gtk_icon_view_update_rubberband (icon_view);
1929   
1930   return TRUE;
1931 }
1932
1933 static gboolean
1934 gtk_icon_view_motion (GtkWidget      *widget,
1935                       GdkEventMotion *event)
1936 {
1937   GtkAllocation allocation;
1938   GtkIconView *icon_view;
1939   gint abs_y;
1940   
1941   icon_view = GTK_ICON_VIEW (widget);
1942
1943   gtk_icon_view_maybe_begin_drag (icon_view, event);
1944
1945   if (icon_view->priv->doing_rubberband)
1946     {
1947       gtk_icon_view_update_rubberband (widget);
1948       
1949       abs_y = event->y - icon_view->priv->height *
1950         (gtk_adjustment_get_value (icon_view->priv->vadjustment) /
1951          (gtk_adjustment_get_upper (icon_view->priv->vadjustment) -
1952           gtk_adjustment_get_lower (icon_view->priv->vadjustment)));
1953
1954       gtk_widget_get_allocation (widget, &allocation);
1955
1956       if (abs_y < 0 || abs_y > allocation.height)
1957         {
1958           if (abs_y < 0)
1959             icon_view->priv->scroll_value_diff = abs_y;
1960           else
1961             icon_view->priv->scroll_value_diff = abs_y - allocation.height;
1962
1963           icon_view->priv->event_last_x = event->x;
1964           icon_view->priv->event_last_y = event->y;
1965
1966           if (icon_view->priv->scroll_timeout_id == 0)
1967             icon_view->priv->scroll_timeout_id = gdk_threads_add_timeout (30, rubberband_scroll_timeout, 
1968                                                                 icon_view);
1969         }
1970       else 
1971         remove_scroll_timeout (icon_view);
1972     }
1973   else
1974     {
1975       GtkIconViewItem *item, *last_prelight_item;
1976       GtkCellRenderer *cell = NULL;
1977
1978       last_prelight_item = icon_view->priv->last_prelight;
1979       item = _gtk_icon_view_get_item_at_coords (icon_view,
1980                                                event->x, event->y,
1981                                                FALSE,
1982                                                &cell);
1983
1984       if (item != NULL)
1985         {
1986           item->prelight = TRUE;
1987           gtk_icon_view_queue_draw_item (icon_view, item);
1988         }
1989
1990       if (last_prelight_item != NULL &&
1991           last_prelight_item != item)
1992         {
1993           last_prelight_item->prelight = FALSE;
1994           gtk_icon_view_queue_draw_item (icon_view,
1995                                          icon_view->priv->last_prelight);
1996         }
1997
1998       icon_view->priv->last_prelight = item;
1999     }
2000   
2001   return TRUE;
2002 }
2003
2004 static void
2005 gtk_icon_view_remove (GtkContainer *container,
2006                       GtkWidget    *widget)
2007 {
2008   GtkIconView *icon_view;
2009   GtkIconViewChild *child = NULL;
2010   GList *tmp_list;
2011
2012   icon_view = GTK_ICON_VIEW (container);
2013   
2014   tmp_list = icon_view->priv->children;
2015   while (tmp_list)
2016     {
2017       child = tmp_list->data;
2018       if (child->widget == widget)
2019         {
2020           gtk_widget_unparent (widget);
2021
2022           icon_view->priv->children = g_list_remove_link (icon_view->priv->children, tmp_list);
2023           g_list_free_1 (tmp_list);
2024           g_free (child);
2025           return;
2026         }
2027
2028       tmp_list = tmp_list->next;
2029     }
2030 }
2031
2032 static void
2033 gtk_icon_view_forall (GtkContainer *container,
2034                       gboolean      include_internals,
2035                       GtkCallback   callback,
2036                       gpointer      callback_data)
2037 {
2038   GtkIconView *icon_view;
2039   GtkIconViewChild *child = NULL;
2040   GList *tmp_list;
2041
2042   icon_view = GTK_ICON_VIEW (container);
2043
2044   tmp_list = icon_view->priv->children;
2045   while (tmp_list)
2046     {
2047       child = tmp_list->data;
2048       tmp_list = tmp_list->next;
2049
2050       (* callback) (child->widget, callback_data);
2051     }
2052 }
2053
2054 static void 
2055 gtk_icon_view_item_selected_changed (GtkIconView      *icon_view,
2056                                      GtkIconViewItem  *item)
2057 {
2058   AtkObject *obj;
2059   AtkObject *item_obj;
2060
2061   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
2062   if (obj != NULL)
2063     {
2064       item_obj = atk_object_ref_accessible_child (obj, item->index);
2065       if (item_obj != NULL)
2066         {
2067           atk_object_notify_state_change (item_obj, ATK_STATE_SELECTED, item->selected);
2068           g_object_unref (item_obj);
2069         }
2070     }
2071 }
2072
2073 static void
2074 gtk_icon_view_add_editable (GtkCellArea            *area,
2075                             GtkCellRenderer        *renderer,
2076                             GtkCellEditable        *editable,
2077                             GdkRectangle           *cell_area,
2078                             const gchar            *path,
2079                             GtkIconView            *icon_view)
2080 {
2081   GtkIconViewChild *child;
2082   GtkWidget *widget = GTK_WIDGET (editable);
2083   
2084   child = g_new (GtkIconViewChild, 1);
2085   
2086   child->widget      = widget;
2087   child->area.x      = cell_area->x;
2088   child->area.y      = cell_area->y;
2089   child->area.width  = cell_area->width;
2090   child->area.height = cell_area->height;
2091
2092   icon_view->priv->children = g_list_append (icon_view->priv->children, child);
2093
2094   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2095     gtk_widget_set_parent_window (child->widget, icon_view->priv->bin_window);
2096   
2097   gtk_widget_set_parent (widget, GTK_WIDGET (icon_view));
2098 }
2099
2100 static void
2101 gtk_icon_view_remove_editable (GtkCellArea            *area,
2102                                GtkCellRenderer        *renderer,
2103                                GtkCellEditable        *editable,
2104                                GtkIconView            *icon_view)
2105 {
2106   GtkTreePath *path;
2107
2108   if (gtk_widget_has_focus (GTK_WIDGET (editable)))
2109     gtk_widget_grab_focus (GTK_WIDGET (icon_view));
2110   
2111   gtk_container_remove (GTK_CONTAINER (icon_view),
2112                         GTK_WIDGET (editable));  
2113
2114   path = gtk_tree_path_new_from_string (gtk_cell_area_get_current_path_string (area));
2115   gtk_icon_view_queue_draw_path (icon_view, path);
2116   gtk_tree_path_free (path);
2117 }
2118
2119 /**
2120  * gtk_icon_view_set_cursor:
2121  * @icon_view: A #GtkIconView
2122  * @path: A #GtkTreePath
2123  * @cell: (allow-none): One of the cell renderers of @icon_view, or %NULL
2124  * @start_editing: %TRUE if the specified cell should start being edited.
2125  *
2126  * Sets the current keyboard focus to be at @path, and selects it.  This is
2127  * useful when you want to focus the user's attention on a particular item.
2128  * If @cell is not %NULL, then focus is given to the cell specified by 
2129  * it. Additionally, if @start_editing is %TRUE, then editing should be 
2130  * started in the specified cell.  
2131  *
2132  * This function is often followed by <literal>gtk_widget_grab_focus 
2133  * (icon_view)</literal> in order to give keyboard focus to the widget.  
2134  * Please note that editing can only happen when the widget is realized.
2135  *
2136  * Since: 2.8
2137  **/
2138 void
2139 gtk_icon_view_set_cursor (GtkIconView     *icon_view,
2140                           GtkTreePath     *path,
2141                           GtkCellRenderer *cell,
2142                           gboolean         start_editing)
2143 {
2144   GtkIconViewItem *item = NULL;
2145
2146   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
2147   g_return_if_fail (path != NULL);
2148   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
2149
2150   if (icon_view->priv->cell_area)
2151     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2152
2153   if (gtk_tree_path_get_depth (path) == 1)
2154     item = g_list_nth_data (icon_view->priv->items,
2155                             gtk_tree_path_get_indices(path)[0]);
2156   
2157   if (!item)
2158     return;
2159   
2160   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
2161   gtk_icon_view_scroll_to_path (icon_view, path, FALSE, 0.0, 0.0);
2162
2163   if (start_editing && 
2164       icon_view->priv->cell_area)
2165     {
2166       GtkCellAreaContext *context;
2167
2168       context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2169       _gtk_icon_view_set_cell_data (icon_view, item);
2170       gtk_cell_area_activate (icon_view->priv->cell_area, context, 
2171                               GTK_WIDGET (icon_view), &item->cell_area, 
2172                               0 /* XXX flags */, TRUE);
2173     }
2174 }
2175
2176 /**
2177  * gtk_icon_view_get_cursor:
2178  * @icon_view: A #GtkIconView
2179  * @path: (out) (allow-none): Return location for the current cursor path,
2180  *        or %NULL
2181  * @cell: (out) (allow-none): Return location the current focus cell, or %NULL
2182  *
2183  * Fills in @path and @cell with the current cursor path and cell. 
2184  * If the cursor isn't currently set, then *@path will be %NULL.  
2185  * If no cell currently has focus, then *@cell will be %NULL.
2186  *
2187  * The returned #GtkTreePath must be freed with gtk_tree_path_free().
2188  *
2189  * Return value: %TRUE if the cursor is set.
2190  *
2191  * Since: 2.8
2192  **/
2193 gboolean
2194 gtk_icon_view_get_cursor (GtkIconView      *icon_view,
2195                           GtkTreePath     **path,
2196                           GtkCellRenderer **cell)
2197 {
2198   GtkIconViewItem *item;
2199
2200   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
2201
2202   item = icon_view->priv->cursor_item;
2203
2204   if (path != NULL)
2205     {
2206       if (item != NULL)
2207         *path = gtk_tree_path_new_from_indices (item->index, -1);
2208       else
2209         *path = NULL;
2210     }
2211
2212   if (cell != NULL && item != NULL && icon_view->priv->cell_area != NULL)
2213     *cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
2214
2215   return (item != NULL);
2216 }
2217
2218 static gboolean
2219 gtk_icon_view_button_press (GtkWidget      *widget,
2220                             GdkEventButton *event)
2221 {
2222   GtkIconView *icon_view;
2223   GtkIconViewItem *item;
2224   gboolean dirty = FALSE;
2225   GtkCellRenderer *cell = NULL, *cursor_cell = NULL;
2226
2227   icon_view = GTK_ICON_VIEW (widget);
2228
2229   if (event->window != icon_view->priv->bin_window)
2230     return FALSE;
2231
2232   if (!gtk_widget_has_focus (widget))
2233     gtk_widget_grab_focus (widget);
2234
2235   if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_BUTTON_PRESS)
2236     {
2237       GdkModifierType extend_mod_mask;
2238       GdkModifierType modify_mod_mask;
2239
2240       extend_mod_mask =
2241         gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_EXTEND_SELECTION);
2242
2243       modify_mod_mask =
2244         gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_MODIFY_SELECTION);
2245
2246       item = _gtk_icon_view_get_item_at_coords (icon_view, 
2247                                                event->x, event->y,
2248                                                FALSE,
2249                                                &cell);
2250
2251       /*
2252        * We consider only the the cells' area as the item area if the
2253        * item is not selected, but if it *is* selected, the complete
2254        * selection rectangle is considered to be part of the item.
2255        */
2256       if (item != NULL && (cell != NULL || item->selected))
2257         {
2258           if (cell != NULL)
2259             {
2260               if (gtk_cell_renderer_is_activatable (cell))
2261                 cursor_cell = cell;
2262             }
2263
2264           gtk_icon_view_scroll_to_item (icon_view, item);
2265           
2266           if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2267             {
2268               _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2269             }
2270           else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
2271                    (event->state & extend_mod_mask))
2272             {
2273               gtk_icon_view_unselect_all_internal (icon_view);
2274
2275               _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2276               if (!icon_view->priv->anchor_item)
2277                 icon_view->priv->anchor_item = item;
2278               else 
2279                 gtk_icon_view_select_all_between (icon_view,
2280                                                   icon_view->priv->anchor_item,
2281                                                   item);
2282               dirty = TRUE;
2283             }
2284           else 
2285             {
2286               if ((icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE ||
2287                   ((icon_view->priv->selection_mode == GTK_SELECTION_SINGLE) && item->selected)) &&
2288                   (event->state & modify_mod_mask))
2289                 {
2290                   item->selected = !item->selected;
2291                   gtk_icon_view_queue_draw_item (icon_view, item);
2292                   dirty = TRUE;
2293                 }
2294               else
2295                 {
2296                   gtk_icon_view_unselect_all_internal (icon_view);
2297
2298                   item->selected = TRUE;
2299                   gtk_icon_view_queue_draw_item (icon_view, item);
2300                   dirty = TRUE;
2301                 }
2302               _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
2303               icon_view->priv->anchor_item = item;
2304             }
2305
2306           /* Save press to possibly begin a drag */
2307           if (icon_view->priv->pressed_button < 0)
2308             {
2309               icon_view->priv->pressed_button = event->button;
2310               icon_view->priv->press_start_x = event->x;
2311               icon_view->priv->press_start_y = event->y;
2312             }
2313
2314           if (!icon_view->priv->last_single_clicked)
2315             icon_view->priv->last_single_clicked = item;
2316
2317           /* cancel the current editing, if it exists */
2318           gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
2319
2320           if (cell != NULL && gtk_cell_renderer_is_activatable (cell))
2321             {
2322               GtkCellAreaContext *context;
2323
2324               context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2325
2326               _gtk_icon_view_set_cell_data (icon_view, item);
2327               gtk_cell_area_activate (icon_view->priv->cell_area, context,
2328                                       GTK_WIDGET (icon_view),
2329                                       &item->cell_area, 0/* XXX flags */, FALSE);
2330             }
2331         }
2332       else
2333         {
2334           if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
2335               !(event->state & modify_mod_mask))
2336             {
2337               dirty = gtk_icon_view_unselect_all_internal (icon_view);
2338             }
2339           
2340           if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
2341             gtk_icon_view_start_rubberbanding (icon_view, event->device, event->x, event->y);
2342         }
2343
2344       /* don't draw keyboard focus around an clicked-on item */
2345       icon_view->priv->draw_focus = FALSE;
2346     }
2347
2348   if (event->button == GDK_BUTTON_PRIMARY && event->type == GDK_2BUTTON_PRESS)
2349     {
2350       item = _gtk_icon_view_get_item_at_coords (icon_view,
2351                                                event->x, event->y,
2352                                                FALSE,
2353                                                NULL);
2354
2355       if (item && item == icon_view->priv->last_single_clicked)
2356         {
2357           GtkTreePath *path;
2358
2359           path = gtk_tree_path_new_from_indices (item->index, -1);
2360           gtk_icon_view_item_activated (icon_view, path);
2361           gtk_tree_path_free (path);
2362         }
2363
2364       icon_view->priv->last_single_clicked = NULL;
2365       icon_view->priv->pressed_button = -1;
2366     }
2367   
2368   if (dirty)
2369     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2370
2371   return event->button == GDK_BUTTON_PRIMARY;
2372 }
2373
2374 static gboolean
2375 gtk_icon_view_button_release (GtkWidget      *widget,
2376                               GdkEventButton *event)
2377 {
2378   GtkIconView *icon_view;
2379
2380   icon_view = GTK_ICON_VIEW (widget);
2381   
2382   if (icon_view->priv->pressed_button == event->button)
2383     icon_view->priv->pressed_button = -1;
2384
2385   gtk_icon_view_stop_rubberbanding (icon_view);
2386
2387   remove_scroll_timeout (icon_view);
2388
2389   return TRUE;
2390 }
2391
2392 static gboolean
2393 gtk_icon_view_key_press (GtkWidget      *widget,
2394                          GdkEventKey    *event)
2395 {
2396   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2397
2398   if (icon_view->priv->doing_rubberband)
2399     {
2400       if (event->keyval == GDK_KEY_Escape)
2401         gtk_icon_view_stop_rubberbanding (icon_view);
2402
2403       return TRUE;
2404     }
2405
2406   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2407 }
2408
2409 static gboolean
2410 gtk_icon_view_key_release (GtkWidget      *widget,
2411                            GdkEventKey    *event)
2412 {
2413   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2414
2415   if (icon_view->priv->doing_rubberband)
2416     return TRUE;
2417
2418   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2419 }
2420
2421 static void
2422 gtk_icon_view_update_rubberband (gpointer data)
2423 {
2424   GtkIconView *icon_view;
2425   gint x, y;
2426   GdkRectangle old_area;
2427   GdkRectangle new_area;
2428   GdkRectangle common;
2429   cairo_region_t *invalid_region;
2430   
2431   icon_view = GTK_ICON_VIEW (data);
2432
2433   gdk_window_get_device_position (icon_view->priv->bin_window,
2434                                   icon_view->priv->rubberband_device,
2435                                   &x, &y, NULL);
2436
2437   x = MAX (x, 0);
2438   y = MAX (y, 0);
2439
2440   old_area.x = MIN (icon_view->priv->rubberband_x1,
2441                     icon_view->priv->rubberband_x2);
2442   old_area.y = MIN (icon_view->priv->rubberband_y1,
2443                     icon_view->priv->rubberband_y2);
2444   old_area.width = ABS (icon_view->priv->rubberband_x2 -
2445                         icon_view->priv->rubberband_x1) + 1;
2446   old_area.height = ABS (icon_view->priv->rubberband_y2 -
2447                          icon_view->priv->rubberband_y1) + 1;
2448   
2449   new_area.x = MIN (icon_view->priv->rubberband_x1, x);
2450   new_area.y = MIN (icon_view->priv->rubberband_y1, y);
2451   new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
2452   new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
2453
2454   invalid_region = cairo_region_create_rectangle (&old_area);
2455   cairo_region_union_rectangle (invalid_region, &new_area);
2456
2457   gdk_rectangle_intersect (&old_area, &new_area, &common);
2458   if (common.width > 2 && common.height > 2)
2459     {
2460       cairo_region_t *common_region;
2461
2462       /* make sure the border is invalidated */
2463       common.x += 1;
2464       common.y += 1;
2465       common.width -= 2;
2466       common.height -= 2;
2467       
2468       common_region = cairo_region_create_rectangle (&common);
2469
2470       cairo_region_subtract (invalid_region, common_region);
2471       cairo_region_destroy (common_region);
2472     }
2473   
2474   gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
2475     
2476   cairo_region_destroy (invalid_region);
2477
2478   icon_view->priv->rubberband_x2 = x;
2479   icon_view->priv->rubberband_y2 = y;  
2480
2481   gtk_icon_view_update_rubberband_selection (icon_view);
2482 }
2483
2484 static void
2485 gtk_icon_view_start_rubberbanding (GtkIconView  *icon_view,
2486                                    GdkDevice    *device,
2487                                    gint          x,
2488                                    gint          y)
2489 {
2490   GList *items;
2491
2492   if (icon_view->priv->rubberband_device)
2493     return;
2494
2495   for (items = icon_view->priv->items; items; items = items->next)
2496     {
2497       GtkIconViewItem *item = items->data;
2498
2499       item->selected_before_rubberbanding = item->selected;
2500     }
2501   
2502   icon_view->priv->rubberband_x1 = x;
2503   icon_view->priv->rubberband_y1 = y;
2504   icon_view->priv->rubberband_x2 = x;
2505   icon_view->priv->rubberband_y2 = y;
2506
2507   icon_view->priv->doing_rubberband = TRUE;
2508   icon_view->priv->rubberband_device = device;
2509
2510   gtk_device_grab_add (GTK_WIDGET (icon_view), device, TRUE);
2511 }
2512
2513 static void
2514 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
2515 {
2516   if (!icon_view->priv->doing_rubberband)
2517     return;
2518
2519   gtk_device_grab_remove (GTK_WIDGET (icon_view),
2520                           icon_view->priv->rubberband_device);
2521
2522   icon_view->priv->doing_rubberband = FALSE;
2523   icon_view->priv->rubberband_device = NULL;
2524
2525   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2526 }
2527
2528 static void
2529 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
2530 {
2531   GList *items;
2532   gint x, y, width, height;
2533   gboolean dirty = FALSE;
2534   
2535   x = MIN (icon_view->priv->rubberband_x1,
2536            icon_view->priv->rubberband_x2);
2537   y = MIN (icon_view->priv->rubberband_y1,
2538            icon_view->priv->rubberband_y2);
2539   width = ABS (icon_view->priv->rubberband_x1 - 
2540                icon_view->priv->rubberband_x2);
2541   height = ABS (icon_view->priv->rubberband_y1 - 
2542                 icon_view->priv->rubberband_y2);
2543   
2544   for (items = icon_view->priv->items; items; items = items->next)
2545     {
2546       GtkIconViewItem *item = items->data;
2547       gboolean is_in;
2548       gboolean selected;
2549       
2550       is_in = gtk_icon_view_item_hit_test (icon_view, item, 
2551                                            x, y, width, height);
2552
2553       selected = is_in ^ item->selected_before_rubberbanding;
2554
2555       if (item->selected != selected)
2556         {
2557           item->selected = selected;
2558           dirty = TRUE;
2559           gtk_icon_view_queue_draw_item (icon_view, item);
2560         }
2561     }
2562
2563   if (dirty)
2564     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2565 }
2566
2567
2568 typedef struct {
2569   GdkRectangle hit_rect;
2570   gboolean     hit;
2571 } HitTestData;
2572
2573 static gboolean 
2574 hit_test (GtkCellRenderer    *renderer,
2575           const GdkRectangle *cell_area,
2576           const GdkRectangle *cell_background,
2577           HitTestData        *data)
2578 {
2579   if (MIN (data->hit_rect.x + data->hit_rect.width, cell_area->x + cell_area->width) - 
2580       MAX (data->hit_rect.x, cell_area->x) > 0 &&
2581       MIN (data->hit_rect.y + data->hit_rect.height, cell_area->y + cell_area->height) - 
2582       MAX (data->hit_rect.y, cell_area->y) > 0)
2583     data->hit = TRUE;
2584   
2585   return (data->hit != FALSE);
2586 }
2587
2588 static gboolean
2589 gtk_icon_view_item_hit_test (GtkIconView      *icon_view,
2590                              GtkIconViewItem  *item,
2591                              gint              x,
2592                              gint              y,
2593                              gint              width,
2594                              gint              height)
2595 {
2596   HitTestData data = { { x, y, width, height }, FALSE };
2597   GtkCellAreaContext *context;
2598   GdkRectangle *item_area = &item->cell_area;
2599    
2600   if (MIN (x + width, item_area->x + item_area->width) - MAX (x, item_area->x) <= 0 ||
2601       MIN (y + height, item_area->y + item_area->height) - MAX (y, item_area->y) <= 0)
2602     return FALSE;
2603
2604   context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2605
2606   _gtk_icon_view_set_cell_data (icon_view, item);
2607   gtk_cell_area_foreach_alloc (icon_view->priv->cell_area, context,
2608                                GTK_WIDGET (icon_view),
2609                                item_area, item_area,
2610                                (GtkCellAllocCallback)hit_test, &data);
2611
2612   return data.hit;
2613 }
2614
2615 static gboolean
2616 gtk_icon_view_unselect_all_internal (GtkIconView  *icon_view)
2617 {
2618   gboolean dirty = FALSE;
2619   GList *items;
2620
2621   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2622     return FALSE;
2623
2624   for (items = icon_view->priv->items; items; items = items->next)
2625     {
2626       GtkIconViewItem *item = items->data;
2627
2628       if (item->selected)
2629         {
2630           item->selected = FALSE;
2631           dirty = TRUE;
2632           gtk_icon_view_queue_draw_item (icon_view, item);
2633           gtk_icon_view_item_selected_changed (icon_view, item);
2634         }
2635     }
2636
2637   return dirty;
2638 }
2639
2640
2641 /* GtkIconView signals */
2642 static void
2643 gtk_icon_view_real_select_all (GtkIconView *icon_view)
2644 {
2645   gtk_icon_view_select_all (icon_view);
2646 }
2647
2648 static void
2649 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
2650 {
2651   gtk_icon_view_unselect_all (icon_view);
2652 }
2653
2654 static void
2655 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
2656 {
2657   gtk_icon_view_unselect_all (icon_view);
2658
2659   if (icon_view->priv->cursor_item != NULL)
2660     _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2661 }
2662
2663 static gboolean
2664 gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view)
2665 {
2666   GtkTreePath *path;
2667   GtkCellAreaContext *context;
2668
2669   if (!icon_view->priv->cursor_item)
2670     return FALSE;
2671
2672   context = g_ptr_array_index (icon_view->priv->row_contexts, icon_view->priv->cursor_item->row);
2673
2674   _gtk_icon_view_set_cell_data (icon_view, icon_view->priv->cursor_item);
2675   gtk_cell_area_activate (icon_view->priv->cell_area, context,
2676                           GTK_WIDGET (icon_view),
2677                           &icon_view->priv->cursor_item->cell_area,
2678                           0 /* XXX flags */,
2679                           FALSE);
2680
2681   path = gtk_tree_path_new_from_indices (icon_view->priv->cursor_item->index, -1);
2682   gtk_icon_view_item_activated (icon_view, path);
2683   gtk_tree_path_free (path);
2684
2685   return TRUE;
2686 }
2687
2688 static void
2689 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
2690 {
2691   if (!icon_view->priv->cursor_item)
2692     return;
2693
2694   switch (icon_view->priv->selection_mode)
2695     {
2696     case GTK_SELECTION_NONE:
2697       break;
2698     case GTK_SELECTION_BROWSE:
2699       _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2700       break;
2701     case GTK_SELECTION_SINGLE:
2702       if (icon_view->priv->cursor_item->selected)
2703         _gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
2704       else
2705         _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2706       break;
2707     case GTK_SELECTION_MULTIPLE:
2708       icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
2709       g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0); 
2710       
2711       gtk_icon_view_item_selected_changed (icon_view, icon_view->priv->cursor_item);      
2712       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2713       break;
2714     }
2715 }
2716
2717 static void
2718 gtk_icon_view_set_hadjustment_values (GtkIconView *icon_view)
2719 {
2720   GtkAllocation  allocation;
2721   GtkAdjustment *adj = icon_view->priv->hadjustment;
2722   gdouble old_page_size;
2723   gdouble old_upper;
2724   gdouble old_value;
2725   gdouble new_value;
2726   gdouble new_upper;
2727
2728   gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2729
2730   old_value = gtk_adjustment_get_value (adj);
2731   old_upper = gtk_adjustment_get_upper (adj);
2732   old_page_size = gtk_adjustment_get_page_size (adj);
2733   new_upper = MAX (allocation.width, icon_view->priv->width);
2734
2735   if (gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL)
2736     {
2737       /* Make sure no scrolling occurs for RTL locales also (if possible) */
2738       /* Quick explanation:
2739        *   In LTR locales, leftmost portion of visible rectangle should stay
2740        *   fixed, which means left edge of scrollbar thumb should remain fixed
2741        *   and thus adjustment's value should stay the same.
2742        *
2743        *   In RTL locales, we want to keep rightmost portion of visible
2744        *   rectangle fixed. This means right edge of thumb should remain fixed.
2745        *   In this case, upper - value - page_size should remain constant.
2746        */
2747       new_value = (new_upper - allocation.width) -
2748                   (old_upper - old_value - old_page_size);
2749       new_value = CLAMP (new_value, 0, new_upper - allocation.width);
2750     }
2751   else
2752     new_value = CLAMP (old_value, 0, new_upper - allocation.width);
2753
2754   gtk_adjustment_configure (adj,
2755                             new_value,
2756                             0.0,
2757                             new_upper,
2758                             allocation.width * 0.1,
2759                             allocation.width * 0.9,
2760                             allocation.width);
2761 }
2762
2763 static void
2764 gtk_icon_view_set_vadjustment_values (GtkIconView *icon_view)
2765 {
2766   GtkAllocation  allocation;
2767   GtkAdjustment *adj = icon_view->priv->vadjustment;
2768
2769   gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2770
2771   gtk_adjustment_configure (adj,
2772                             gtk_adjustment_get_value (adj),
2773                             0.0,
2774                             MAX (allocation.height, icon_view->priv->height),
2775                             allocation.height * 0.1,
2776                             allocation.height * 0.9,
2777                             allocation.height);
2778 }
2779
2780 static void
2781 gtk_icon_view_set_hadjustment (GtkIconView   *icon_view,
2782                                GtkAdjustment *adjustment)
2783 {
2784   GtkIconViewPrivate *priv = icon_view->priv;
2785
2786   if (adjustment && priv->hadjustment == adjustment)
2787     return;
2788
2789   if (priv->hadjustment != NULL)
2790     {
2791       g_signal_handlers_disconnect_matched (priv->hadjustment,
2792                                             G_SIGNAL_MATCH_DATA,
2793                                             0, 0, NULL, NULL, icon_view);
2794       g_object_unref (priv->hadjustment);
2795     }
2796
2797   if (!adjustment)
2798     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2799                                      0.0, 0.0, 0.0);
2800
2801   g_signal_connect (adjustment, "value-changed",
2802                     G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2803   priv->hadjustment = g_object_ref_sink (adjustment);
2804   gtk_icon_view_set_hadjustment_values (icon_view);
2805
2806   g_object_notify (G_OBJECT (icon_view), "hadjustment");
2807 }
2808
2809 static void
2810 gtk_icon_view_set_vadjustment (GtkIconView   *icon_view,
2811                                GtkAdjustment *adjustment)
2812 {
2813   GtkIconViewPrivate *priv = icon_view->priv;
2814
2815   if (adjustment && priv->vadjustment == adjustment)
2816     return;
2817
2818   if (priv->vadjustment != NULL)
2819     {
2820       g_signal_handlers_disconnect_matched (priv->vadjustment,
2821                                             G_SIGNAL_MATCH_DATA,
2822                                             0, 0, NULL, NULL, icon_view);
2823       g_object_unref (priv->vadjustment);
2824     }
2825
2826   if (!adjustment)
2827     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2828                                      0.0, 0.0, 0.0);
2829
2830   g_signal_connect (adjustment, "value-changed",
2831                     G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2832   priv->vadjustment = g_object_ref_sink (adjustment);
2833   gtk_icon_view_set_vadjustment_values (icon_view);
2834
2835   g_object_notify (G_OBJECT (icon_view), "vadjustment");
2836 }
2837
2838 static void
2839 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
2840                                   GtkIconView   *icon_view)
2841 {
2842   GtkIconViewPrivate *priv = icon_view->priv;
2843
2844   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2845     {
2846       gdk_window_move (priv->bin_window,
2847                        - gtk_adjustment_get_value (priv->hadjustment),
2848                        - gtk_adjustment_get_value (priv->vadjustment));
2849
2850       if (icon_view->priv->doing_rubberband)
2851         gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
2852       
2853       _gtk_icon_view_accessible_adjustment_changed (icon_view);
2854     }
2855 }
2856
2857 static gint
2858 compare_sizes (gconstpointer p1,
2859                gconstpointer p2,
2860                gpointer      unused)
2861 {
2862   return GPOINTER_TO_INT (((const GtkRequestedSize *) p1)->data)
2863        - GPOINTER_TO_INT (((const GtkRequestedSize *) p2)->data);
2864 }
2865
2866 static void
2867 gtk_icon_view_layout (GtkIconView *icon_view)
2868 {
2869   GtkIconViewPrivate *priv = icon_view->priv;
2870   GtkWidget *widget = GTK_WIDGET (icon_view);
2871   GList *items;
2872   gint item_width; /* this doesn't include item_padding */
2873   gint n_columns, n_rows, n_items;
2874   gint col, row;
2875   GtkRequestedSize *sizes;
2876   gboolean rtl;
2877
2878   rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2879
2880
2881   n_items = gtk_icon_view_get_n_items (icon_view);
2882
2883   gtk_icon_view_compute_n_items_for_size (icon_view, 
2884                                           GTK_ORIENTATION_HORIZONTAL,
2885                                           gtk_widget_get_allocated_width (widget),
2886                                           NULL, NULL,
2887                                           &n_columns, &item_width);
2888   n_rows = (n_items + n_columns - 1) / n_columns;
2889
2890   priv->width = n_columns * (item_width + 2 * priv->item_padding + priv->column_spacing) - priv->column_spacing;
2891   priv->width += 2 * priv->margin;
2892   priv->width = MAX (priv->width, gtk_widget_get_allocated_width (widget));
2893
2894   /* Clear the per row contexts */
2895   g_ptr_array_set_size (icon_view->priv->row_contexts, 0);
2896
2897   gtk_cell_area_context_reset (priv->cell_area_context);
2898   /* because layouting is complicated. We designed an API
2899    * that is O(N²) and nonsensical.
2900    * And we're proud of it. */
2901   for (items = priv->items; items; items = items->next)
2902     {
2903       _gtk_icon_view_set_cell_data (icon_view, items->data);
2904       gtk_cell_area_get_preferred_width (priv->cell_area,
2905                                          priv->cell_area_context,
2906                                          widget,
2907                                          NULL, NULL);
2908     }
2909
2910   sizes = g_newa (GtkRequestedSize, n_rows);
2911   items = priv->items;
2912   priv->height = priv->margin;
2913
2914   /* Collect the heights for all rows */
2915   for (row = 0; row < n_rows; row++)
2916     {
2917       GtkCellAreaContext *context = gtk_cell_area_copy_context (priv->cell_area, priv->cell_area_context);
2918       g_ptr_array_add (priv->row_contexts, context);
2919
2920       for (col = 0; col < n_columns && items; col++, items = items->next)
2921         {
2922           GtkIconViewItem *item = items->data;
2923
2924           _gtk_icon_view_set_cell_data (icon_view, item);
2925           gtk_cell_area_get_preferred_height_for_width (priv->cell_area,
2926                                                         context,
2927                                                         widget,
2928                                                         item_width, 
2929                                                         NULL, NULL);
2930         }
2931       
2932       sizes[row].data = GINT_TO_POINTER (row);
2933       gtk_cell_area_context_get_preferred_height_for_width (context,
2934                                                             item_width,
2935                                                             &sizes[row].minimum_size,
2936                                                             &sizes[row].natural_size);
2937       priv->height += sizes[row].minimum_size + 2 * priv->item_padding + priv->row_spacing;
2938     }
2939
2940   priv->height -= priv->row_spacing;
2941   priv->height += priv->margin;
2942   priv->height = MIN (priv->height, gtk_widget_get_allocated_height (widget));
2943
2944   gtk_distribute_natural_allocation (gtk_widget_get_allocated_height (widget) - priv->height,
2945                                      n_rows,
2946                                      sizes);
2947
2948   /* Actually allocate the rows */
2949   g_qsort_with_data (sizes, n_rows, sizeof (GtkRequestedSize), compare_sizes, NULL);
2950   
2951   items = priv->items;
2952   priv->height = priv->margin;
2953
2954   for (row = 0; row < n_rows; row++)
2955     {
2956       GtkCellAreaContext *context = g_ptr_array_index (priv->row_contexts, row);
2957       gtk_cell_area_context_allocate (context, item_width, sizes[row].minimum_size);
2958
2959       priv->height += priv->item_padding;
2960
2961       for (col = 0; col < n_columns && items; col++, items = items->next)
2962         {
2963           GtkIconViewItem *item = items->data;
2964
2965           item->cell_area.x = priv->margin + (col * 2 + 1) * priv->item_padding + col * (priv->column_spacing + item_width);
2966           item->cell_area.width = item_width;
2967           item->cell_area.y = priv->height;
2968           item->cell_area.height = sizes[row].minimum_size;
2969           item->row = row;
2970           item->col = col;
2971           if (rtl)
2972             {
2973               item->cell_area.x = priv->width - item_width - item->cell_area.x;
2974               item->col = n_columns - 1 - col;
2975             }
2976         }
2977
2978       priv->height += sizes[row].minimum_size + priv->item_padding + priv->row_spacing;
2979     }
2980
2981   priv->height -= priv->row_spacing;
2982   priv->height += priv->margin;
2983   priv->height = MAX (priv->height, gtk_widget_get_allocated_height (widget));
2984 }
2985
2986 static void
2987 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
2988 {
2989   /* Clear all item sizes */
2990   g_list_foreach (icon_view->priv->items,
2991                   (GFunc)gtk_icon_view_item_invalidate_size, NULL);
2992
2993   /* Re-layout the items */
2994   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
2995 }
2996
2997 static void
2998 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
2999 {
3000   item->cell_area.width = -1;
3001   item->cell_area.height = -1;
3002 }
3003
3004 static void
3005 gtk_icon_view_paint_item (GtkIconView     *icon_view,
3006                           cairo_t         *cr,
3007                           GtkIconViewItem *item,
3008                           gint             x,
3009                           gint             y,
3010                           gboolean         draw_focus)
3011 {
3012   GdkRectangle cell_area;
3013   GtkStateFlags state = 0;
3014   GtkCellRendererState flags = 0;
3015   GtkStyleContext *style_context;
3016   GtkWidget *widget = GTK_WIDGET (icon_view);
3017   GtkIconViewPrivate *priv = icon_view->priv;
3018   GtkCellAreaContext *context;
3019
3020   if (priv->model == NULL || item->cell_area.width <= 0 || item->cell_area.height <= 0)
3021     return;
3022
3023   _gtk_icon_view_set_cell_data (icon_view, item);
3024
3025   style_context = gtk_widget_get_style_context (widget);
3026   state = gtk_widget_get_state_flags (widget);
3027
3028   gtk_style_context_save (style_context);
3029   gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_VIEW);
3030   gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_CELL);
3031
3032   state &= ~(GTK_STATE_FLAG_SELECTED | GTK_STATE_FLAG_PRELIGHT);
3033
3034   if (item->selected)
3035     {
3036       if ((state & GTK_STATE_FLAG_FOCUSED) &&
3037           item == icon_view->priv->cursor_item)
3038         {
3039           flags |= GTK_CELL_RENDERER_FOCUSED;
3040         }
3041
3042       state |= GTK_STATE_FLAG_SELECTED;
3043       flags |= GTK_CELL_RENDERER_SELECTED;
3044     }
3045
3046   if (item->prelight)
3047     {
3048       state |= GTK_STATE_FLAG_PRELIGHT;
3049       flags |= GTK_CELL_RENDERER_PRELIT;
3050     }
3051
3052   gtk_style_context_set_state (style_context, state);
3053
3054   if (item->selected)
3055     {
3056       gtk_render_background (style_context, cr,
3057                              x - icon_view->priv->item_padding,
3058                              y - icon_view->priv->item_padding,
3059                              item->cell_area.width  + icon_view->priv->item_padding * 2,
3060                              item->cell_area.height + icon_view->priv->item_padding * 2);
3061       gtk_render_frame (style_context, cr,
3062                         x - icon_view->priv->item_padding,
3063                         y - icon_view->priv->item_padding,
3064                         item->cell_area.width  + icon_view->priv->item_padding * 2,
3065                         item->cell_area.height + icon_view->priv->item_padding * 2);
3066     }
3067
3068   cell_area.x      = x;
3069   cell_area.y      = y;
3070   cell_area.width  = item->cell_area.width;
3071   cell_area.height = item->cell_area.height;
3072
3073   context = g_ptr_array_index (priv->row_contexts, item->row);
3074   gtk_cell_area_render (priv->cell_area, context,
3075                         widget, cr, &cell_area, &cell_area, flags,
3076                         draw_focus);
3077
3078   gtk_style_context_restore (style_context);
3079 }
3080
3081 static void
3082 gtk_icon_view_paint_rubberband (GtkIconView     *icon_view,
3083                                 cairo_t         *cr)
3084 {
3085   GtkStyleContext *context;
3086   GdkRectangle rect;
3087
3088   cairo_save (cr);
3089
3090   rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
3091   rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
3092   rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
3093   rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
3094
3095   context = gtk_widget_get_style_context (GTK_WIDGET (icon_view));
3096
3097   gtk_style_context_save (context);
3098   gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
3099
3100   gdk_cairo_rectangle (cr, &rect);
3101   cairo_clip (cr);
3102
3103   gtk_render_background (context, cr,
3104                          rect.x, rect.y,
3105                          rect.width, rect.height);
3106   gtk_render_frame (context, cr,
3107                     rect.x, rect.y,
3108                     rect.width, rect.height);
3109
3110   gtk_style_context_restore (context);
3111   cairo_restore (cr);
3112 }
3113
3114 static void
3115 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
3116                                GtkTreePath *path)
3117 {
3118   GList *l;
3119   gint index;
3120
3121   index = gtk_tree_path_get_indices (path)[0];
3122
3123   for (l = icon_view->priv->items; l; l = l->next) 
3124     {
3125       GtkIconViewItem *item = l->data;
3126
3127       if (item->index == index)
3128         {
3129           gtk_icon_view_queue_draw_item (icon_view, item);
3130           break;
3131         }
3132     }
3133 }
3134
3135 static void
3136 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
3137                                GtkIconViewItem *item)
3138 {
3139   GdkRectangle  rect;
3140   GdkRectangle *item_area = &item->cell_area;
3141
3142   rect.x      = item_area->x - icon_view->priv->item_padding;
3143   rect.y      = item_area->y - icon_view->priv->item_padding;
3144   rect.width  = item_area->width  + icon_view->priv->item_padding * 2;
3145   rect.height = item_area->height + icon_view->priv->item_padding * 2;
3146
3147   if (icon_view->priv->bin_window)
3148     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
3149 }
3150
3151 void
3152 _gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
3153                                 GtkIconViewItem *item,
3154                                 GtkCellRenderer *cursor_cell)
3155 {
3156   AtkObject *obj;
3157   AtkObject *item_obj;
3158   AtkObject *cursor_item_obj;
3159
3160   /* When hitting this path from keynav, the focus cell is
3161    * already set, we dont need to notify the atk object
3162    * but we still need to queue the draw here (in the case
3163    * that the focus cell changes but not the cursor item).
3164    */
3165   gtk_icon_view_queue_draw_item (icon_view, item);
3166
3167   if (icon_view->priv->cursor_item == item &&
3168       (cursor_cell == NULL || cursor_cell == gtk_cell_area_get_focus_cell (icon_view->priv->cell_area)))
3169     return;
3170
3171   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3172   if (icon_view->priv->cursor_item != NULL)
3173     {
3174       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3175       if (obj != NULL)
3176         {
3177           cursor_item_obj = atk_object_ref_accessible_child (obj, icon_view->priv->cursor_item->index);
3178           if (cursor_item_obj != NULL)
3179             atk_object_notify_state_change (cursor_item_obj, ATK_STATE_FOCUSED, FALSE);
3180         }
3181     }
3182   icon_view->priv->cursor_item = item;
3183
3184   if (cursor_cell)
3185     gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cursor_cell);
3186   else
3187     {
3188       /* Make sure there is a cell in focus initially */
3189       if (!gtk_cell_area_get_focus_cell (icon_view->priv->cell_area))
3190         gtk_cell_area_focus (icon_view->priv->cell_area, GTK_DIR_TAB_FORWARD);
3191     }
3192   
3193   /* Notify that accessible focus object has changed */
3194   item_obj = atk_object_ref_accessible_child (obj, item->index);
3195
3196   if (item_obj != NULL)
3197     {
3198       atk_focus_tracker_notify (item_obj);
3199       atk_object_notify_state_change (item_obj, ATK_STATE_FOCUSED, TRUE);
3200       g_object_unref (item_obj); 
3201     }
3202 }
3203
3204
3205 static GtkIconViewItem *
3206 gtk_icon_view_item_new (void)
3207 {
3208   GtkIconViewItem *item;
3209
3210   item = g_slice_new0 (GtkIconViewItem);
3211
3212   item->cell_area.width  = -1;
3213   item->cell_area.height = -1;
3214   
3215   return item;
3216 }
3217
3218 static void
3219 gtk_icon_view_item_free (GtkIconViewItem *item)
3220 {
3221   g_return_if_fail (item != NULL);
3222
3223   g_slice_free (GtkIconViewItem, item);
3224 }
3225
3226 GtkIconViewItem *
3227 _gtk_icon_view_get_item_at_coords (GtkIconView          *icon_view,
3228                                    gint                  x,
3229                                    gint                  y,
3230                                    gboolean              only_in_cell,
3231                                    GtkCellRenderer     **cell_at_pos)
3232 {
3233   GList *items;
3234
3235   if (cell_at_pos)
3236     *cell_at_pos = NULL;
3237
3238   for (items = icon_view->priv->items; items; items = items->next)
3239     {
3240       GtkIconViewItem *item = items->data;
3241       GdkRectangle    *item_area = &item->cell_area;
3242
3243       if (x >= item_area->x - icon_view->priv->column_spacing/2 && 
3244           x <= item_area->x + item_area->width + icon_view->priv->column_spacing/2 &&
3245           y >= item_area->y - icon_view->priv->row_spacing/2 && 
3246           y <= item_area->y + item_area->height + icon_view->priv->row_spacing/2)
3247         {
3248           if (only_in_cell || cell_at_pos)
3249             {
3250               GtkCellRenderer *cell = NULL;
3251               GtkCellAreaContext *context;
3252
3253               context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
3254               _gtk_icon_view_set_cell_data (icon_view, item);
3255
3256               if (x >= item_area->x && x <= item_area->x + item_area->width &&
3257                   y >= item_area->y && y <= item_area->y + item_area->height)
3258                 cell = gtk_cell_area_get_cell_at_position (icon_view->priv->cell_area, context,
3259                                                            GTK_WIDGET (icon_view),
3260                                                            item_area,
3261                                                            x, y, NULL);
3262
3263               if (cell_at_pos)
3264                 *cell_at_pos = cell;
3265
3266               if (only_in_cell)
3267                 return cell != NULL ? item : NULL;
3268               else
3269                 return item;
3270             }
3271           return item;
3272         }
3273     }
3274   return NULL;
3275 }
3276
3277 void
3278 _gtk_icon_view_select_item (GtkIconView      *icon_view,
3279                             GtkIconViewItem  *item)
3280 {
3281   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3282   g_return_if_fail (item != NULL);
3283
3284   if (item->selected)
3285     return;
3286   
3287   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3288     return;
3289   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3290     gtk_icon_view_unselect_all_internal (icon_view);
3291
3292   item->selected = TRUE;
3293
3294   gtk_icon_view_item_selected_changed (icon_view, item);
3295   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3296
3297   gtk_icon_view_queue_draw_item (icon_view, item);
3298 }
3299
3300
3301 void
3302 _gtk_icon_view_unselect_item (GtkIconView      *icon_view,
3303                               GtkIconViewItem  *item)
3304 {
3305   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3306   g_return_if_fail (item != NULL);
3307
3308   if (!item->selected)
3309     return;
3310   
3311   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3312       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3313     return;
3314   
3315   item->selected = FALSE;
3316
3317   gtk_icon_view_item_selected_changed (icon_view, item);
3318   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3319
3320   gtk_icon_view_queue_draw_item (icon_view, item);
3321 }
3322
3323 static void
3324 verify_items (GtkIconView *icon_view)
3325 {
3326   GList *items;
3327   int i = 0;
3328
3329   for (items = icon_view->priv->items; items; items = items->next)
3330     {
3331       GtkIconViewItem *item = items->data;
3332
3333       if (item->index != i)
3334         g_error ("List item does not match its index: "
3335                  "item index %d and list index %d\n", item->index, i);
3336
3337       i++;
3338     }
3339 }
3340
3341 static void
3342 gtk_icon_view_row_changed (GtkTreeModel *model,
3343                            GtkTreePath  *path,
3344                            GtkTreeIter  *iter,
3345                            gpointer      data)
3346 {
3347   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3348
3349   /* ignore changes in branches */
3350   if (gtk_tree_path_get_depth (path) > 1)
3351     return;
3352
3353   /* An icon view subclass might add it's own model and populate
3354    * things at init() time instead of waiting for the constructor() 
3355    * to be called 
3356    */
3357   if (icon_view->priv->cell_area)
3358     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3359
3360   /* Here we can use a "grow-only" strategy for optimization
3361    * and only invalidate a single item and queue a relayout
3362    * instead of invalidating the whole thing.
3363    *
3364    * For now GtkIconView still cant deal with huge models
3365    * so just invalidate the whole thing when the model
3366    * changes.
3367    */
3368   gtk_icon_view_invalidate_sizes (icon_view);
3369
3370   verify_items (icon_view);
3371 }
3372
3373 static void
3374 gtk_icon_view_row_inserted (GtkTreeModel *model,
3375                             GtkTreePath  *path,
3376                             GtkTreeIter  *iter,
3377                             gpointer      data)
3378 {
3379   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3380   gint index;
3381   GtkIconViewItem *item;
3382   GList *list;
3383
3384   /* ignore changes in branches */
3385   if (gtk_tree_path_get_depth (path) > 1)
3386     return;
3387
3388   index = gtk_tree_path_get_indices(path)[0];
3389
3390   item = gtk_icon_view_item_new ();
3391
3392   item->index = index;
3393
3394   /* FIXME: We can be more efficient here,
3395      we can store a tail pointer and use that when
3396      appending (which is a rather common operation)
3397   */
3398   icon_view->priv->items = g_list_insert (icon_view->priv->items,
3399                                          item, index);
3400   
3401   list = g_list_nth (icon_view->priv->items, index + 1);
3402   for (; list; list = list->next)
3403     {
3404       item = list->data;
3405
3406       item->index++;
3407     }
3408     
3409   verify_items (icon_view);
3410
3411   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3412 }
3413
3414 static void
3415 gtk_icon_view_row_deleted (GtkTreeModel *model,
3416                            GtkTreePath  *path,
3417                            gpointer      data)
3418 {
3419   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3420   gint index;
3421   GtkIconViewItem *item;
3422   GList *list, *next;
3423   gboolean emit = FALSE;
3424
3425   /* ignore changes in branches */
3426   if (gtk_tree_path_get_depth (path) > 1)
3427     return;
3428
3429   index = gtk_tree_path_get_indices(path)[0];
3430
3431   list = g_list_nth (icon_view->priv->items, index);
3432   item = list->data;
3433
3434   if (icon_view->priv->cell_area)
3435     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3436
3437   if (item == icon_view->priv->anchor_item)
3438     icon_view->priv->anchor_item = NULL;
3439
3440   if (item == icon_view->priv->cursor_item)
3441     icon_view->priv->cursor_item = NULL;
3442
3443   if (item == icon_view->priv->last_prelight)
3444     icon_view->priv->last_prelight = NULL;
3445
3446   if (item->selected)
3447     emit = TRUE;
3448   
3449   gtk_icon_view_item_free (item);
3450
3451   for (next = list->next; next; next = next->next)
3452     {
3453       item = next->data;
3454
3455       item->index--;
3456     }
3457   
3458   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3459
3460   verify_items (icon_view);  
3461   
3462   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3463
3464   if (emit)
3465     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3466 }
3467
3468 static void
3469 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3470                               GtkTreePath  *parent,
3471                               GtkTreeIter  *iter,
3472                               gint         *new_order,
3473                               gpointer      data)
3474 {
3475   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3476   int i;
3477   int length;
3478   GList *items = NULL, *list;
3479   GtkIconViewItem **item_array;
3480   gint *order;
3481
3482   /* ignore changes in branches */
3483   if (iter != NULL)
3484     return;
3485
3486   if (icon_view->priv->cell_area)
3487     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3488
3489   length = gtk_tree_model_iter_n_children (model, NULL);
3490
3491   order = g_new (gint, length);
3492   for (i = 0; i < length; i++)
3493     order [new_order[i]] = i;
3494
3495   item_array = g_new (GtkIconViewItem *, length);
3496   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3497     item_array[order[i]] = list->data;
3498   g_free (order);
3499
3500   for (i = length - 1; i >= 0; i--)
3501     {
3502       item_array[i]->index = i;
3503       items = g_list_prepend (items, item_array[i]);
3504     }
3505   
3506   g_free (item_array);
3507   g_list_free (icon_view->priv->items);
3508   icon_view->priv->items = items;
3509
3510   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
3511
3512   verify_items (icon_view);  
3513 }
3514
3515 static void
3516 gtk_icon_view_build_items (GtkIconView *icon_view)
3517 {
3518   GtkTreeIter iter;
3519   int i;
3520   GList *items = NULL;
3521
3522   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3523                                       &iter))
3524     return;
3525
3526   i = 0;
3527   
3528   do
3529     {
3530       GtkIconViewItem *item = gtk_icon_view_item_new ();
3531
3532       item->index = i;
3533       
3534       i++;
3535
3536       items = g_list_prepend (items, item);
3537       
3538     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3539
3540   icon_view->priv->items = g_list_reverse (items);
3541 }
3542
3543 static void
3544 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
3545                                 guint           keyval,
3546                                 guint           modmask,
3547                                 GtkMovementStep step,
3548                                 gint            count)
3549 {
3550   
3551   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3552                                 I_("move-cursor"), 2,
3553                                 G_TYPE_ENUM, step,
3554                                 G_TYPE_INT, count);
3555
3556   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3557                                 "move-cursor", 2,
3558                                 G_TYPE_ENUM, step,
3559                                 G_TYPE_INT, count);
3560
3561   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3562    return;
3563
3564   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3565                                 "move-cursor", 2,
3566                                 G_TYPE_ENUM, step,
3567                                 G_TYPE_INT, count);
3568
3569   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3570                                 "move-cursor", 2,
3571                                 G_TYPE_ENUM, step,
3572                                 G_TYPE_INT, count);
3573 }
3574
3575 static gboolean
3576 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
3577                                 GtkMovementStep  step,
3578                                 gint             count)
3579 {
3580   GdkModifierType state;
3581
3582   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3583   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3584                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3585                         step == GTK_MOVEMENT_DISPLAY_LINES ||
3586                         step == GTK_MOVEMENT_PAGES ||
3587                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3588
3589   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3590     return FALSE;
3591
3592   gtk_cell_area_stop_editing (icon_view->priv->cell_area, FALSE);
3593   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3594
3595   if (gtk_get_current_event_state (&state))
3596     {
3597       GdkModifierType extend_mod_mask;
3598       GdkModifierType modify_mod_mask;
3599
3600       extend_mod_mask =
3601         gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3602                                       GDK_MODIFIER_INTENT_EXTEND_SELECTION);
3603       modify_mod_mask =
3604         gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3605                                       GDK_MODIFIER_INTENT_MODIFY_SELECTION);
3606
3607       if ((state & modify_mod_mask) == modify_mod_mask)
3608         icon_view->priv->modify_selection_pressed = TRUE;
3609       if ((state & extend_mod_mask) == extend_mod_mask)
3610         icon_view->priv->extend_selection_pressed = TRUE;
3611     }
3612   /* else we assume not pressed */
3613
3614   switch (step)
3615     {
3616     case GTK_MOVEMENT_LOGICAL_POSITIONS:
3617     case GTK_MOVEMENT_VISUAL_POSITIONS:
3618       gtk_icon_view_move_cursor_left_right (icon_view, count);
3619       break;
3620     case GTK_MOVEMENT_DISPLAY_LINES:
3621       gtk_icon_view_move_cursor_up_down (icon_view, count);
3622       break;
3623     case GTK_MOVEMENT_PAGES:
3624       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3625       break;
3626     case GTK_MOVEMENT_BUFFER_ENDS:
3627       gtk_icon_view_move_cursor_start_end (icon_view, count);
3628       break;
3629     default:
3630       g_assert_not_reached ();
3631     }
3632
3633   icon_view->priv->modify_selection_pressed = FALSE;
3634   icon_view->priv->extend_selection_pressed = FALSE;
3635
3636   icon_view->priv->draw_focus = TRUE;
3637
3638   return TRUE;
3639 }
3640
3641 static GtkIconViewItem *
3642 find_item (GtkIconView     *icon_view,
3643            GtkIconViewItem *current,
3644            gint             row_ofs,
3645            gint             col_ofs)
3646 {
3647   gint row, col;
3648   GList *items;
3649   GtkIconViewItem *item;
3650
3651   /* FIXME: this could be more efficient 
3652    */
3653   row = current->row + row_ofs;
3654   col = current->col + col_ofs;
3655
3656   for (items = icon_view->priv->items; items; items = items->next)
3657     {
3658       item = items->data;
3659       if (item->row == row && item->col == col)
3660         return item;
3661     }
3662   
3663   return NULL;
3664 }
3665
3666 static GtkIconViewItem *
3667 find_item_page_up_down (GtkIconView     *icon_view,
3668                         GtkIconViewItem *current,
3669                         gint             count)
3670 {
3671   GList *item, *next;
3672   gint y, col;
3673   
3674   col = current->col;
3675   y = current->cell_area.y + count * gtk_adjustment_get_page_size (icon_view->priv->vadjustment);
3676
3677   item = g_list_find (icon_view->priv->items, current);
3678   if (count > 0)
3679     {
3680       while (item)
3681         {
3682           for (next = item->next; next; next = next->next)
3683             {
3684               if (((GtkIconViewItem *)next->data)->col == col)
3685                 break;
3686             }
3687           if (!next || ((GtkIconViewItem *)next->data)->cell_area.y > y)
3688             break;
3689
3690           item = next;
3691         }
3692     }
3693   else 
3694     {
3695       while (item)
3696         {
3697           for (next = item->prev; next; next = next->prev)
3698             {
3699               if (((GtkIconViewItem *)next->data)->col == col)
3700                 break;
3701             }
3702           if (!next || ((GtkIconViewItem *)next->data)->cell_area.y < y)
3703             break;
3704
3705           item = next;
3706         }
3707     }
3708
3709   if (item)
3710     return item->data;
3711
3712   return NULL;
3713 }
3714
3715 static gboolean
3716 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
3717                                   GtkIconViewItem *anchor,
3718                                   GtkIconViewItem *cursor)
3719 {
3720   GList *items;
3721   GtkIconViewItem *item;
3722   gint row1, row2, col1, col2;
3723   gboolean dirty = FALSE;
3724   
3725   if (anchor->row < cursor->row)
3726     {
3727       row1 = anchor->row;
3728       row2 = cursor->row;
3729     }
3730   else
3731     {
3732       row1 = cursor->row;
3733       row2 = anchor->row;
3734     }
3735
3736   if (anchor->col < cursor->col)
3737     {
3738       col1 = anchor->col;
3739       col2 = cursor->col;
3740     }
3741   else
3742     {
3743       col1 = cursor->col;
3744       col2 = anchor->col;
3745     }
3746
3747   for (items = icon_view->priv->items; items; items = items->next)
3748     {
3749       item = items->data;
3750
3751       if (row1 <= item->row && item->row <= row2 &&
3752           col1 <= item->col && item->col <= col2)
3753         {
3754           if (!item->selected)
3755             {
3756               dirty = TRUE;
3757               item->selected = TRUE;
3758               gtk_icon_view_item_selected_changed (icon_view, item);
3759             }
3760           gtk_icon_view_queue_draw_item (icon_view, item);
3761         }
3762     }
3763
3764   return dirty;
3765 }
3766
3767 static void 
3768 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3769                                    gint         count)
3770 {
3771   GtkIconViewItem *item;
3772   GtkCellRenderer *cell = NULL;
3773   gboolean dirty = FALSE;
3774   gint step;
3775   GtkDirectionType direction;
3776
3777   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3778     return;
3779
3780   direction = count < 0 ? GTK_DIR_UP : GTK_DIR_DOWN;
3781
3782   if (!icon_view->priv->cursor_item)
3783     {
3784       GList *list;
3785
3786       if (count > 0)
3787         list = icon_view->priv->items;
3788       else
3789         list = g_list_last (icon_view->priv->items);
3790
3791       if (list)
3792         {
3793           item = list->data;
3794
3795           /* Give focus to the first cell initially */
3796           _gtk_icon_view_set_cell_data (icon_view, item);
3797           gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3798         }
3799       else
3800         {
3801           item = NULL;
3802         }
3803     }
3804   else
3805     {
3806       item = icon_view->priv->cursor_item;
3807       step = count > 0 ? 1 : -1;      
3808
3809       /* Save the current focus cell in case we hit the edge */
3810       cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3811
3812       while (item)
3813         {
3814           _gtk_icon_view_set_cell_data (icon_view, item);
3815
3816           if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3817             break;
3818
3819           item = find_item (icon_view, item, step, 0);
3820         }
3821     }
3822
3823   if (!item)
3824     {
3825       if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3826         {
3827           GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3828           if (toplevel)
3829             gtk_widget_child_focus (toplevel,
3830                                     direction == GTK_DIR_UP ?
3831                                     GTK_DIR_TAB_BACKWARD :
3832                                     GTK_DIR_TAB_FORWARD);
3833
3834         }
3835
3836       gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3837       return;
3838     }
3839
3840   if (icon_view->priv->modify_selection_pressed ||
3841       !icon_view->priv->extend_selection_pressed ||
3842       !icon_view->priv->anchor_item ||
3843       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3844     icon_view->priv->anchor_item = item;
3845
3846   cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3847   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3848
3849   if (!icon_view->priv->modify_selection_pressed &&
3850       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3851     {
3852       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3853       dirty = gtk_icon_view_select_all_between (icon_view, 
3854                                                 icon_view->priv->anchor_item,
3855                                                 item) || dirty;
3856     }
3857
3858   gtk_icon_view_scroll_to_item (icon_view, item);
3859
3860   if (dirty)
3861     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3862 }
3863
3864 static void 
3865 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3866                                         gint         count)
3867 {
3868   GtkIconViewItem *item;
3869   gboolean dirty = FALSE;
3870   
3871   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3872     return;
3873   
3874   if (!icon_view->priv->cursor_item)
3875     {
3876       GList *list;
3877
3878       if (count > 0)
3879         list = icon_view->priv->items;
3880       else
3881         list = g_list_last (icon_view->priv->items);
3882
3883       item = list ? list->data : NULL;
3884     }
3885   else
3886     item = find_item_page_up_down (icon_view, 
3887                                    icon_view->priv->cursor_item,
3888                                    count);
3889
3890   if (item == icon_view->priv->cursor_item)
3891     gtk_widget_error_bell (GTK_WIDGET (icon_view));
3892
3893   if (!item)
3894     return;
3895
3896   if (icon_view->priv->modify_selection_pressed ||
3897       !icon_view->priv->extend_selection_pressed ||
3898       !icon_view->priv->anchor_item ||
3899       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3900     icon_view->priv->anchor_item = item;
3901
3902   _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
3903
3904   if (!icon_view->priv->modify_selection_pressed &&
3905       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3906     {
3907       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3908       dirty = gtk_icon_view_select_all_between (icon_view, 
3909                                                 icon_view->priv->anchor_item,
3910                                                 item) || dirty;
3911     }
3912
3913   gtk_icon_view_scroll_to_item (icon_view, item);
3914
3915   if (dirty)
3916     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);  
3917 }
3918
3919 static void 
3920 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
3921                                       gint         count)
3922 {
3923   GtkIconViewItem *item;
3924   GtkCellRenderer *cell = NULL;
3925   gboolean dirty = FALSE;
3926   gint step;
3927   GtkDirectionType direction;
3928
3929   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3930     return;
3931
3932   direction = count < 0 ? GTK_DIR_LEFT : GTK_DIR_RIGHT;
3933
3934   if (!icon_view->priv->cursor_item)
3935     {
3936       GList *list;
3937
3938       if (count > 0)
3939         list = icon_view->priv->items;
3940       else
3941         list = g_list_last (icon_view->priv->items);
3942
3943       if (list)
3944         {
3945           item = list->data;
3946
3947           /* Give focus to the first cell initially */
3948           _gtk_icon_view_set_cell_data (icon_view, item);
3949           gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3950         }
3951       else
3952         {
3953           item = NULL;
3954         }
3955     }
3956   else
3957     {
3958       item = icon_view->priv->cursor_item;
3959       step = count > 0 ? 1 : -1;
3960
3961       /* Save the current focus cell in case we hit the edge */
3962       cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3963
3964       while (item)
3965         {
3966           _gtk_icon_view_set_cell_data (icon_view, item);
3967
3968           if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3969             break;
3970           
3971           item = find_item (icon_view, item, 0, step);
3972         }
3973     }
3974
3975   if (!item)
3976     {
3977       if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3978         {
3979           GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3980           if (toplevel)
3981             gtk_widget_child_focus (toplevel,
3982                                     direction == GTK_DIR_LEFT ?
3983                                     GTK_DIR_TAB_BACKWARD :
3984                                     GTK_DIR_TAB_FORWARD);
3985
3986         }
3987
3988       gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3989       return;
3990     }
3991
3992   if (icon_view->priv->modify_selection_pressed ||
3993       !icon_view->priv->extend_selection_pressed ||
3994       !icon_view->priv->anchor_item ||
3995       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3996     icon_view->priv->anchor_item = item;
3997
3998   cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3999   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
4000
4001   if (!icon_view->priv->modify_selection_pressed &&
4002       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4003     {
4004       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4005       dirty = gtk_icon_view_select_all_between (icon_view, 
4006                                                 icon_view->priv->anchor_item,
4007                                                 item) || dirty;
4008     }
4009
4010   gtk_icon_view_scroll_to_item (icon_view, item);
4011
4012   if (dirty)
4013     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4014 }
4015
4016 static void 
4017 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
4018                                      gint         count)
4019 {
4020   GtkIconViewItem *item;
4021   GList *list;
4022   gboolean dirty = FALSE;
4023   
4024   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
4025     return;
4026   
4027   if (count < 0)
4028     list = icon_view->priv->items;
4029   else
4030     list = g_list_last (icon_view->priv->items);
4031   
4032   item = list ? list->data : NULL;
4033
4034   if (item == icon_view->priv->cursor_item)
4035     gtk_widget_error_bell (GTK_WIDGET (icon_view));
4036
4037   if (!item)
4038     return;
4039
4040   if (icon_view->priv->modify_selection_pressed ||
4041       !icon_view->priv->extend_selection_pressed ||
4042       !icon_view->priv->anchor_item ||
4043       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4044     icon_view->priv->anchor_item = item;
4045
4046   _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
4047
4048   if (!icon_view->priv->modify_selection_pressed &&
4049       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4050     {
4051       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4052       dirty = gtk_icon_view_select_all_between (icon_view, 
4053                                                 icon_view->priv->anchor_item,
4054                                                 item) || dirty;
4055     }
4056
4057   gtk_icon_view_scroll_to_item (icon_view, item);
4058
4059   if (dirty)
4060     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4061 }
4062
4063 /**
4064  * gtk_icon_view_scroll_to_path:
4065  * @icon_view: A #GtkIconView.
4066  * @path: The path of the item to move to.
4067  * @use_align: whether to use alignment arguments, or %FALSE.
4068  * @row_align: The vertical alignment of the item specified by @path.
4069  * @col_align: The horizontal alignment of the item specified by @path.
4070  *
4071  * Moves the alignments of @icon_view to the position specified by @path.  
4072  * @row_align determines where the row is placed, and @col_align determines 
4073  * where @column is placed.  Both are expected to be between 0.0 and 1.0. 
4074  * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means 
4075  * center.
4076  *
4077  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
4078  * tree does the minimum amount of work to scroll the item onto the screen.
4079  * This means that the item will be scrolled to the edge closest to its current
4080  * position.  If the item is currently visible on the screen, nothing is done.
4081  *
4082  * This function only works if the model is set, and @path is a valid row on 
4083  * the model. If the model changes before the @icon_view is realized, the 
4084  * centered path will be modified to reflect this change.
4085  *
4086  * Since: 2.8
4087  **/
4088 void
4089 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
4090                               GtkTreePath *path,
4091                               gboolean     use_align,
4092                               gfloat       row_align,
4093                               gfloat       col_align)
4094 {
4095   GtkIconViewItem *item = NULL;
4096   GtkWidget *widget;
4097
4098   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4099   g_return_if_fail (path != NULL);
4100   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
4101   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
4102
4103   widget = GTK_WIDGET (icon_view);
4104
4105   if (gtk_tree_path_get_depth (path) > 0)
4106     item = g_list_nth_data (icon_view->priv->items,
4107                             gtk_tree_path_get_indices(path)[0]);
4108   
4109   if (!item || item->cell_area.width < 0 ||
4110       !gtk_widget_get_realized (widget))
4111     {
4112       if (icon_view->priv->scroll_to_path)
4113         gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4114
4115       icon_view->priv->scroll_to_path = NULL;
4116
4117       if (path)
4118         icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
4119
4120       icon_view->priv->scroll_to_use_align = use_align;
4121       icon_view->priv->scroll_to_row_align = row_align;
4122       icon_view->priv->scroll_to_col_align = col_align;
4123
4124       return;
4125     }
4126
4127   if (use_align)
4128     {
4129       GtkAllocation allocation;
4130       gint x, y;
4131       gfloat offset;
4132       GdkRectangle item_area = 
4133         { 
4134           item->cell_area.x - icon_view->priv->item_padding, 
4135           item->cell_area.y - icon_view->priv->item_padding, 
4136           item->cell_area.width  + icon_view->priv->item_padding * 2, 
4137           item->cell_area.height + icon_view->priv->item_padding * 2 
4138         };
4139
4140       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4141
4142       gtk_widget_get_allocation (widget, &allocation);
4143
4144       offset = y + item_area.y - row_align * (allocation.height - item_area.height);
4145
4146       gtk_adjustment_set_value (icon_view->priv->vadjustment,
4147                                 gtk_adjustment_get_value (icon_view->priv->vadjustment) + offset);
4148
4149       offset = x + item_area.x - col_align * (allocation.width - item_area.width);
4150
4151       gtk_adjustment_set_value (icon_view->priv->hadjustment,
4152                                 gtk_adjustment_get_value (icon_view->priv->hadjustment) + offset);
4153
4154       gtk_adjustment_changed (icon_view->priv->hadjustment);
4155       gtk_adjustment_changed (icon_view->priv->vadjustment);
4156     }
4157   else
4158     gtk_icon_view_scroll_to_item (icon_view, item);
4159 }
4160
4161
4162 static void
4163 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view,
4164                               GtkIconViewItem *item)
4165 {
4166   GtkIconViewPrivate *priv = icon_view->priv;
4167   GtkWidget *widget = GTK_WIDGET (icon_view);
4168   GtkAdjustment *hadj, *vadj;
4169   GtkAllocation allocation;
4170   gint x, y;
4171   GdkRectangle item_area;
4172
4173   item_area.x = item->cell_area.x - priv->item_padding;
4174   item_area.y = item->cell_area.y - priv->item_padding;
4175   item_area.width = item->cell_area.width  + priv->item_padding * 2;
4176   item_area.height = item->cell_area.height + priv->item_padding * 2;
4177
4178   gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4179   gtk_widget_get_allocation (widget, &allocation);
4180
4181   hadj = icon_view->priv->hadjustment;
4182   vadj = icon_view->priv->vadjustment;
4183
4184   if (y + item_area.y < 0)
4185     gtk_adjustment_set_value (vadj,
4186                               gtk_adjustment_get_value (vadj)
4187                                 + y + item_area.y);
4188   else if (y + item_area.y + item_area.height > allocation.height)
4189     gtk_adjustment_set_value (vadj,
4190                               gtk_adjustment_get_value (vadj)
4191                                 + y + item_area.y + item_area.height - allocation.height);
4192
4193   if (x + item_area.x < 0)
4194     gtk_adjustment_set_value (hadj,
4195                               gtk_adjustment_get_value (hadj)
4196                                 + x + item_area.x);
4197   else if (x + item_area.x + item_area.width > allocation.width)
4198     gtk_adjustment_set_value (hadj,
4199                               gtk_adjustment_get_value (hadj)
4200                                 + x + item_area.x + item_area.width - allocation.width);
4201
4202   gtk_adjustment_changed (hadj);
4203   gtk_adjustment_changed (vadj);
4204 }
4205
4206 /* GtkCellLayout implementation */
4207
4208 static void
4209 gtk_icon_view_ensure_cell_area (GtkIconView *icon_view,
4210                                 GtkCellArea *cell_area)
4211 {
4212   GtkIconViewPrivate *priv = icon_view->priv;
4213
4214   if (priv->cell_area)
4215     return;
4216
4217   if (cell_area)
4218     priv->cell_area = cell_area;
4219   else
4220     priv->cell_area = gtk_cell_area_box_new ();
4221
4222   g_object_ref_sink (priv->cell_area);
4223
4224   if (GTK_IS_ORIENTABLE (priv->cell_area))
4225     gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->cell_area), priv->item_orientation);
4226
4227   priv->cell_area_context = gtk_cell_area_create_context (priv->cell_area);
4228
4229   priv->add_editable_id =
4230     g_signal_connect (priv->cell_area, "add-editable",
4231                       G_CALLBACK (gtk_icon_view_add_editable), icon_view);
4232   priv->remove_editable_id =
4233     g_signal_connect (priv->cell_area, "remove-editable",
4234                       G_CALLBACK (gtk_icon_view_remove_editable), icon_view);
4235
4236   update_text_cell (icon_view);
4237   update_pixbuf_cell (icon_view);
4238 }
4239
4240 static GtkCellArea *
4241 gtk_icon_view_cell_layout_get_area (GtkCellLayout *cell_layout)
4242 {
4243   GtkIconView *icon_view = GTK_ICON_VIEW (cell_layout);
4244   GtkIconViewPrivate *priv = icon_view->priv;
4245
4246   if (G_UNLIKELY (!priv->cell_area))
4247     gtk_icon_view_ensure_cell_area (icon_view, NULL);
4248
4249   return icon_view->priv->cell_area;
4250 }
4251
4252 void
4253 _gtk_icon_view_set_cell_data (GtkIconView     *icon_view,
4254                               GtkIconViewItem *item)
4255 {
4256   GtkTreeIter iter;
4257   GtkTreePath *path;
4258
4259   path = gtk_tree_path_new_from_indices (item->index, -1);
4260   if (!gtk_tree_model_get_iter (icon_view->priv->model, &iter, path))
4261     return;
4262   gtk_tree_path_free (path);
4263
4264   gtk_cell_area_apply_attributes (icon_view->priv->cell_area, 
4265                                   icon_view->priv->model,
4266                                   &iter, FALSE, FALSE);
4267 }
4268
4269
4270
4271 /* Public API */
4272
4273
4274 /**
4275  * gtk_icon_view_new:
4276  * 
4277  * Creates a new #GtkIconView widget
4278  * 
4279  * Return value: A newly created #GtkIconView widget
4280  *
4281  * Since: 2.6
4282  **/
4283 GtkWidget *
4284 gtk_icon_view_new (void)
4285 {
4286   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4287 }
4288
4289 /**
4290  * gtk_icon_view_new_with_area:
4291  * @area: the #GtkCellArea to use to layout cells
4292  * 
4293  * Creates a new #GtkIconView widget using the
4294  * specified @area to layout cells inside the icons.
4295  * 
4296  * Return value: A newly created #GtkIconView widget
4297  *
4298  * Since: 3.0
4299  **/
4300 GtkWidget *
4301 gtk_icon_view_new_with_area (GtkCellArea *area)
4302 {
4303   return g_object_new (GTK_TYPE_ICON_VIEW, "cell-area", area, NULL);
4304 }
4305
4306 /**
4307  * gtk_icon_view_new_with_model:
4308  * @model: The model.
4309  * 
4310  * Creates a new #GtkIconView widget with the model @model.
4311  * 
4312  * Return value: A newly created #GtkIconView widget.
4313  *
4314  * Since: 2.6 
4315  **/
4316 GtkWidget *
4317 gtk_icon_view_new_with_model (GtkTreeModel *model)
4318 {
4319   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4320 }
4321
4322 /**
4323  * gtk_icon_view_convert_widget_to_bin_window_coords:
4324  * @icon_view: a #GtkIconView 
4325  * @wx: X coordinate relative to the widget
4326  * @wy: Y coordinate relative to the widget
4327  * @bx: (out): return location for bin_window X coordinate
4328  * @by: (out): return location for bin_window Y coordinate
4329  * 
4330  * Converts widget coordinates to coordinates for the bin_window,
4331  * as expected by e.g. gtk_icon_view_get_path_at_pos(). 
4332  *
4333  * Since: 2.12
4334  */
4335 void
4336 gtk_icon_view_convert_widget_to_bin_window_coords (GtkIconView *icon_view,
4337                                                    gint         wx,
4338                                                    gint         wy, 
4339                                                    gint        *bx,
4340                                                    gint        *by)
4341 {
4342   gint x, y;
4343
4344   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4345
4346   if (icon_view->priv->bin_window) 
4347     gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4348   else
4349     x = y = 0;
4350  
4351   if (bx)
4352     *bx = wx - x;
4353   if (by)
4354     *by = wy - y;
4355 }
4356
4357 /**
4358  * gtk_icon_view_get_path_at_pos:
4359  * @icon_view: A #GtkIconView.
4360  * @x: The x position to be identified
4361  * @y: The y position to be identified
4362  * 
4363  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4364  * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4365  * the cell at the specified position. 
4366  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4367  * widget coordinates to bin_window coordinates.
4368  * 
4369  * Return value: The #GtkTreePath corresponding to the icon or %NULL
4370  * if no icon exists at that position.
4371  *
4372  * Since: 2.6 
4373  **/
4374 GtkTreePath *
4375 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4376                                gint         x,
4377                                gint         y)
4378 {
4379   GtkIconViewItem *item;
4380   GtkTreePath *path;
4381   
4382   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4383
4384   item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4385
4386   if (!item)
4387     return NULL;
4388
4389   path = gtk_tree_path_new_from_indices (item->index, -1);
4390
4391   return path;
4392 }
4393
4394 /**
4395  * gtk_icon_view_get_item_at_pos:
4396  * @icon_view: A #GtkIconView.
4397  * @x: The x position to be identified
4398  * @y: The y position to be identified
4399  * @path: (out) (allow-none): Return location for the path, or %NULL
4400  * @cell: (out) (allow-none): Return location for the renderer
4401  *   responsible for the cell at (@x, @y), or %NULL
4402  * 
4403  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4404  * In contrast to gtk_icon_view_get_path_at_pos(), this function also 
4405  * obtains the cell at the specified position. The returned path should
4406  * be freed with gtk_tree_path_free().
4407  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4408  * widget coordinates to bin_window coordinates.
4409  * 
4410  * Return value: %TRUE if an item exists at the specified position
4411  *
4412  * Since: 2.8
4413  **/
4414 gboolean 
4415 gtk_icon_view_get_item_at_pos (GtkIconView      *icon_view,
4416                                gint              x,
4417                                gint              y,
4418                                GtkTreePath     **path,
4419                                GtkCellRenderer **cell)
4420 {
4421   GtkIconViewItem *item;
4422   GtkCellRenderer *renderer = NULL;
4423   
4424   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4425
4426   item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &renderer);
4427
4428   if (path != NULL)
4429     {
4430       if (item != NULL)
4431         *path = gtk_tree_path_new_from_indices (item->index, -1);
4432       else
4433         *path = NULL;
4434     }
4435
4436   if (cell != NULL)
4437     *cell = renderer;
4438
4439   return (item != NULL);
4440 }
4441
4442 /**
4443  * gtk_icon_view_set_tooltip_item:
4444  * @icon_view: a #GtkIconView
4445  * @tooltip: a #GtkTooltip
4446  * @path: a #GtkTreePath
4447  * 
4448  * Sets the tip area of @tooltip to be the area covered by the item at @path.
4449  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4450  * See also gtk_tooltip_set_tip_area().
4451  * 
4452  * Since: 2.12
4453  */
4454 void 
4455 gtk_icon_view_set_tooltip_item (GtkIconView     *icon_view,
4456                                 GtkTooltip      *tooltip,
4457                                 GtkTreePath     *path)
4458 {
4459   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4460   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4461
4462   gtk_icon_view_set_tooltip_cell (icon_view, tooltip, path, NULL);
4463 }
4464
4465 /**
4466  * gtk_icon_view_set_tooltip_cell:
4467  * @icon_view: a #GtkIconView
4468  * @tooltip: a #GtkTooltip
4469  * @path: a #GtkTreePath
4470  * @cell: (allow-none): a #GtkCellRenderer or %NULL
4471  *
4472  * Sets the tip area of @tooltip to the area which @cell occupies in
4473  * the item pointed to by @path. See also gtk_tooltip_set_tip_area().
4474  *
4475  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4476  *
4477  * Since: 2.12
4478  */
4479 void
4480 gtk_icon_view_set_tooltip_cell (GtkIconView     *icon_view,
4481                                 GtkTooltip      *tooltip,
4482                                 GtkTreePath     *path,
4483                                 GtkCellRenderer *cell)
4484 {
4485   GdkRectangle rect;
4486   GtkIconViewItem *item = NULL;
4487   gint x, y;
4488  
4489   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4490   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4491   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
4492
4493   if (gtk_tree_path_get_depth (path) > 0)
4494     item = g_list_nth_data (icon_view->priv->items,
4495                             gtk_tree_path_get_indices(path)[0]);
4496  
4497   if (!item)
4498     return;
4499
4500   if (cell)
4501     {
4502       GtkCellAreaContext *context;
4503
4504       context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
4505       _gtk_icon_view_set_cell_data (icon_view, item);
4506       gtk_cell_area_get_cell_allocation (icon_view->priv->cell_area, context,
4507                                          GTK_WIDGET (icon_view),
4508                                          cell, &item->cell_area, &rect);
4509     }
4510   else
4511     {
4512       rect.x = item->cell_area.x - icon_view->priv->item_padding;
4513       rect.y = item->cell_area.y - icon_view->priv->item_padding;
4514       rect.width  = item->cell_area.width  + icon_view->priv->item_padding * 2;
4515       rect.height = item->cell_area.height + icon_view->priv->item_padding * 2;
4516     }
4517   
4518   if (icon_view->priv->bin_window)
4519     {
4520       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4521       rect.x += x;
4522       rect.y += y; 
4523     }
4524
4525   gtk_tooltip_set_tip_area (tooltip, &rect); 
4526 }
4527
4528
4529 /**
4530  * gtk_icon_view_get_tooltip_context:
4531  * @icon_view: an #GtkIconView
4532  * @x: (inout): the x coordinate (relative to widget coordinates)
4533  * @y: (inout): the y coordinate (relative to widget coordinates)
4534  * @keyboard_tip: whether this is a keyboard tooltip or not
4535  * @model: (out) (allow-none): a pointer to receive a #GtkTreeModel or %NULL
4536  * @path: (out) (allow-none): a pointer to receive a #GtkTreePath or %NULL
4537  * @iter: (out) (allow-none): a pointer to receive a #GtkTreeIter or %NULL
4538  *
4539  * This function is supposed to be used in a #GtkWidget::query-tooltip
4540  * signal handler for #GtkIconView.  The @x, @y and @keyboard_tip values
4541  * which are received in the signal handler, should be passed to this
4542  * function without modification.
4543  *
4544  * The return value indicates whether there is an icon view item at the given
4545  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips. For keyboard
4546  * tooltips the item returned will be the cursor item. When %TRUE, then any of
4547  * @model, @path and @iter which have been provided will be set to point to
4548  * that row and the corresponding model. @x and @y will always be converted
4549  * to be relative to @icon_view's bin_window if @keyboard_tooltip is %FALSE.
4550  *
4551  * Return value: whether or not the given tooltip context points to a item
4552  *
4553  * Since: 2.12
4554  */
4555 gboolean
4556 gtk_icon_view_get_tooltip_context (GtkIconView   *icon_view,
4557                                    gint          *x,
4558                                    gint          *y,
4559                                    gboolean       keyboard_tip,
4560                                    GtkTreeModel **model,
4561                                    GtkTreePath  **path,
4562                                    GtkTreeIter   *iter)
4563 {
4564   GtkTreePath *tmppath = NULL;
4565
4566   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4567   g_return_val_if_fail (x != NULL, FALSE);
4568   g_return_val_if_fail (y != NULL, FALSE);
4569
4570   if (keyboard_tip)
4571     {
4572       gtk_icon_view_get_cursor (icon_view, &tmppath, NULL);
4573
4574       if (!tmppath)
4575         return FALSE;
4576     }
4577   else
4578     {
4579       gtk_icon_view_convert_widget_to_bin_window_coords (icon_view, *x, *y,
4580                                                          x, y);
4581
4582       if (!gtk_icon_view_get_item_at_pos (icon_view, *x, *y, &tmppath, NULL))
4583         return FALSE;
4584     }
4585
4586   if (model)
4587     *model = gtk_icon_view_get_model (icon_view);
4588
4589   if (iter)
4590     gtk_tree_model_get_iter (gtk_icon_view_get_model (icon_view),
4591                              iter, tmppath);
4592
4593   if (path)
4594     *path = tmppath;
4595   else
4596     gtk_tree_path_free (tmppath);
4597
4598   return TRUE;
4599 }
4600
4601 static gboolean
4602 gtk_icon_view_set_tooltip_query_cb (GtkWidget  *widget,
4603                                     gint        x,
4604                                     gint        y,
4605                                     gboolean    keyboard_tip,
4606                                     GtkTooltip *tooltip,
4607                                     gpointer    data)
4608 {
4609   gchar *str;
4610   GtkTreeIter iter;
4611   GtkTreePath *path;
4612   GtkTreeModel *model;
4613   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
4614
4615   if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget),
4616                                           &x, &y,
4617                                           keyboard_tip,
4618                                           &model, &path, &iter))
4619     return FALSE;
4620
4621   gtk_tree_model_get (model, &iter, icon_view->priv->tooltip_column, &str, -1);
4622
4623   if (!str)
4624     {
4625       gtk_tree_path_free (path);
4626       return FALSE;
4627     }
4628
4629   gtk_tooltip_set_markup (tooltip, str);
4630   gtk_icon_view_set_tooltip_item (icon_view, tooltip, path);
4631
4632   gtk_tree_path_free (path);
4633   g_free (str);
4634
4635   return TRUE;
4636 }
4637
4638
4639 /**
4640  * gtk_icon_view_set_tooltip_column:
4641  * @icon_view: a #GtkIconView
4642  * @column: an integer, which is a valid column number for @icon_view's model
4643  *
4644  * If you only plan to have simple (text-only) tooltips on full items, you
4645  * can use this function to have #GtkIconView handle these automatically
4646  * for you. @column should be set to the column in @icon_view's model
4647  * containing the tooltip texts, or -1 to disable this feature.
4648  *
4649  * When enabled, #GtkWidget:has-tooltip will be set to %TRUE and
4650  * @icon_view will connect a #GtkWidget::query-tooltip signal handler.
4651  *
4652  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
4653  * so &amp;, &lt;, etc have to be escaped in the text.
4654  *
4655  * Since: 2.12
4656  */
4657 void
4658 gtk_icon_view_set_tooltip_column (GtkIconView *icon_view,
4659                                   gint         column)
4660 {
4661   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4662
4663   if (column == icon_view->priv->tooltip_column)
4664     return;
4665
4666   if (column == -1)
4667     {
4668       g_signal_handlers_disconnect_by_func (icon_view,
4669                                             gtk_icon_view_set_tooltip_query_cb,
4670                                             NULL);
4671       gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), FALSE);
4672     }
4673   else
4674     {
4675       if (icon_view->priv->tooltip_column == -1)
4676         {
4677           g_signal_connect (icon_view, "query-tooltip",
4678                             G_CALLBACK (gtk_icon_view_set_tooltip_query_cb), NULL);
4679           gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), TRUE);
4680         }
4681     }
4682
4683   icon_view->priv->tooltip_column = column;
4684   g_object_notify (G_OBJECT (icon_view), "tooltip-column");
4685 }
4686
4687 /**
4688  * gtk_icon_view_get_tooltip_column:
4689  * @icon_view: a #GtkIconView
4690  *
4691  * Returns the column of @icon_view's model which is being used for
4692  * displaying tooltips on @icon_view's rows.
4693  *
4694  * Return value: the index of the tooltip column that is currently being
4695  * used, or -1 if this is disabled.
4696  *
4697  * Since: 2.12
4698  */
4699 gint
4700 gtk_icon_view_get_tooltip_column (GtkIconView *icon_view)
4701 {
4702   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 0);
4703
4704   return icon_view->priv->tooltip_column;
4705 }
4706
4707 /**
4708  * gtk_icon_view_get_visible_range:
4709  * @icon_view: A #GtkIconView
4710  * @start_path: (out) (allow-none): Return location for start of region,
4711  *              or %NULL
4712  * @end_path: (out) (allow-none): Return location for end of region, or %NULL
4713  * 
4714  * Sets @start_path and @end_path to be the first and last visible path. 
4715  * Note that there may be invisible paths in between.
4716  * 
4717  * Both paths should be freed with gtk_tree_path_free() after use.
4718  * 
4719  * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4720  *
4721  * Since: 2.8
4722  **/
4723 gboolean
4724 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
4725                                  GtkTreePath **start_path,
4726                                  GtkTreePath **end_path)
4727 {
4728   gint start_index = -1;
4729   gint end_index = -1;
4730   GList *icons;
4731
4732   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4733
4734   if (icon_view->priv->hadjustment == NULL ||
4735       icon_view->priv->vadjustment == NULL)
4736     return FALSE;
4737
4738   if (start_path == NULL && end_path == NULL)
4739     return FALSE;
4740   
4741   for (icons = icon_view->priv->items; icons; icons = icons->next) 
4742     {
4743       GtkIconViewItem *item = icons->data;
4744       GdkRectangle    *item_area = &item->cell_area;
4745
4746       if ((item_area->x + item_area->width >= (int)gtk_adjustment_get_value (icon_view->priv->hadjustment)) &&
4747           (item_area->y + item_area->height >= (int)gtk_adjustment_get_value (icon_view->priv->vadjustment)) &&
4748           (item_area->x <= 
4749            (int) (gtk_adjustment_get_value (icon_view->priv->hadjustment) + 
4750                   gtk_adjustment_get_page_size (icon_view->priv->hadjustment))) &&
4751           (item_area->y <= 
4752            (int) (gtk_adjustment_get_value (icon_view->priv->vadjustment) + 
4753                   gtk_adjustment_get_page_size (icon_view->priv->vadjustment))))
4754         {
4755           if (start_index == -1)
4756             start_index = item->index;
4757           end_index = item->index;
4758         }
4759     }
4760
4761   if (start_path && start_index != -1)
4762     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4763   if (end_path && end_index != -1)
4764     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4765   
4766   return start_index != -1;
4767 }
4768
4769 /**
4770  * gtk_icon_view_selected_foreach:
4771  * @icon_view: A #GtkIconView.
4772  * @func: (scope call): The function to call for each selected icon.
4773  * @data: User data to pass to the function.
4774  * 
4775  * Calls a function for each selected icon. Note that the model or
4776  * selection cannot be modified from within this function.
4777  *
4778  * Since: 2.6 
4779  **/
4780 void
4781 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
4782                                 GtkIconViewForeachFunc func,
4783                                 gpointer               data)
4784 {
4785   GList *list;
4786   
4787   for (list = icon_view->priv->items; list; list = list->next)
4788     {
4789       GtkIconViewItem *item = list->data;
4790       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4791
4792       if (item->selected)
4793         (* func) (icon_view, path, data);
4794
4795       gtk_tree_path_free (path);
4796     }
4797 }
4798
4799 /**
4800  * gtk_icon_view_set_selection_mode:
4801  * @icon_view: A #GtkIconView.
4802  * @mode: The selection mode
4803  * 
4804  * Sets the selection mode of the @icon_view.
4805  *
4806  * Since: 2.6 
4807  **/
4808 void
4809 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
4810                                   GtkSelectionMode  mode)
4811 {
4812   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4813
4814   if (mode == icon_view->priv->selection_mode)
4815     return;
4816   
4817   if (mode == GTK_SELECTION_NONE ||
4818       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4819     gtk_icon_view_unselect_all (icon_view);
4820   
4821   icon_view->priv->selection_mode = mode;
4822
4823   g_object_notify (G_OBJECT (icon_view), "selection-mode");
4824 }
4825
4826 /**
4827  * gtk_icon_view_get_selection_mode:
4828  * @icon_view: A #GtkIconView.
4829  * 
4830  * Gets the selection mode of the @icon_view.
4831  *
4832  * Return value: the current selection mode
4833  *
4834  * Since: 2.6 
4835  **/
4836 GtkSelectionMode
4837 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4838 {
4839   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4840
4841   return icon_view->priv->selection_mode;
4842 }
4843
4844 /**
4845  * gtk_icon_view_set_model:
4846  * @icon_view: A #GtkIconView.
4847  * @model: (allow-none): The model.
4848  *
4849  * Sets the model for a #GtkIconView.
4850  * If the @icon_view already has a model set, it will remove
4851  * it before setting the new model.  If @model is %NULL, then
4852  * it will unset the old model.
4853  *
4854  * Since: 2.6 
4855  **/
4856 void
4857 gtk_icon_view_set_model (GtkIconView *icon_view,
4858                          GtkTreeModel *model)
4859 {
4860   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4861   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4862   
4863   if (icon_view->priv->model == model)
4864     return;
4865
4866   if (icon_view->priv->scroll_to_path)
4867     {
4868       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4869       icon_view->priv->scroll_to_path = NULL;
4870     }
4871
4872   /* The area can be NULL while disposing */
4873   if (icon_view->priv->cell_area)
4874     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4875
4876   if (model)
4877     {
4878       GType column_type;
4879
4880       if (icon_view->priv->pixbuf_column != -1)
4881         {
4882           column_type = gtk_tree_model_get_column_type (model,
4883                                                         icon_view->priv->pixbuf_column);          
4884
4885           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4886         }
4887
4888       if (icon_view->priv->text_column != -1)
4889         {
4890           column_type = gtk_tree_model_get_column_type (model,
4891                                                         icon_view->priv->text_column);    
4892
4893           g_return_if_fail (column_type == G_TYPE_STRING);
4894         }
4895
4896       if (icon_view->priv->markup_column != -1)
4897         {
4898           column_type = gtk_tree_model_get_column_type (model,
4899                                                         icon_view->priv->markup_column);          
4900
4901           g_return_if_fail (column_type == G_TYPE_STRING);
4902         }
4903       
4904     }
4905   
4906   if (icon_view->priv->model)
4907     {
4908       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4909                                             gtk_icon_view_row_changed,
4910                                             icon_view);
4911       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4912                                             gtk_icon_view_row_inserted,
4913                                             icon_view);
4914       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4915                                             gtk_icon_view_row_deleted,
4916                                             icon_view);
4917       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4918                                             gtk_icon_view_rows_reordered,
4919                                             icon_view);
4920
4921       g_object_unref (icon_view->priv->model);
4922       
4923       g_list_free_full (icon_view->priv->items, (GDestroyNotify) gtk_icon_view_item_free);
4924       icon_view->priv->items = NULL;
4925       icon_view->priv->anchor_item = NULL;
4926       icon_view->priv->cursor_item = NULL;
4927       icon_view->priv->last_single_clicked = NULL;
4928       icon_view->priv->last_prelight = NULL;
4929       icon_view->priv->width = 0;
4930       icon_view->priv->height = 0;
4931     }
4932
4933   icon_view->priv->model = model;
4934
4935   if (icon_view->priv->model)
4936     {
4937       g_object_ref (icon_view->priv->model);
4938       g_signal_connect (icon_view->priv->model,
4939                         "row-changed",
4940                         G_CALLBACK (gtk_icon_view_row_changed),
4941                         icon_view);
4942       g_signal_connect (icon_view->priv->model,
4943                         "row-inserted",
4944                         G_CALLBACK (gtk_icon_view_row_inserted),
4945                         icon_view);
4946       g_signal_connect (icon_view->priv->model,
4947                         "row-deleted",
4948                         G_CALLBACK (gtk_icon_view_row_deleted),
4949                         icon_view);
4950       g_signal_connect (icon_view->priv->model,
4951                         "rows-reordered",
4952                         G_CALLBACK (gtk_icon_view_rows_reordered),
4953                         icon_view);
4954
4955       gtk_icon_view_build_items (icon_view);
4956     }
4957
4958   g_object_notify (G_OBJECT (icon_view), "model");  
4959
4960   gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4961 }
4962
4963 /**
4964  * gtk_icon_view_get_model:
4965  * @icon_view: a #GtkIconView
4966  *
4967  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
4968  * model is unset.
4969  *
4970  * Return value: (transfer none): A #GtkTreeModel, or %NULL if none is
4971  *     currently being used.
4972  *
4973  * Since: 2.6 
4974  **/
4975 GtkTreeModel *
4976 gtk_icon_view_get_model (GtkIconView *icon_view)
4977 {
4978   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4979
4980   return icon_view->priv->model;
4981 }
4982
4983 static void
4984 update_text_cell (GtkIconView *icon_view)
4985 {
4986   if (!icon_view->priv->cell_area)
4987     return;
4988
4989   if (icon_view->priv->text_column == -1 &&
4990       icon_view->priv->markup_column == -1)
4991     {
4992       if (icon_view->priv->text_cell != NULL)
4993         {
4994           gtk_cell_area_remove (icon_view->priv->cell_area, 
4995                                 icon_view->priv->text_cell);
4996           icon_view->priv->text_cell = NULL;
4997         }
4998     }
4999   else 
5000     {
5001       if (icon_view->priv->text_cell == NULL)
5002         {
5003           icon_view->priv->text_cell = gtk_cell_renderer_text_new ();
5004
5005           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), icon_view->priv->text_cell, FALSE);
5006         }
5007
5008       if (icon_view->priv->markup_column != -1)
5009         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5010                                         icon_view->priv->text_cell, 
5011                                         "markup", icon_view->priv->markup_column, 
5012                                         NULL);
5013       else
5014         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5015                                         icon_view->priv->text_cell, 
5016                                         "text", icon_view->priv->text_column, 
5017                                         NULL);
5018
5019       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5020         g_object_set (icon_view->priv->text_cell,
5021                       "alignment", PANGO_ALIGN_CENTER,
5022                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
5023                       "xalign", 0.5,
5024                       "yalign", 0.0,
5025                       NULL);
5026       else
5027         g_object_set (icon_view->priv->text_cell,
5028                       "alignment", PANGO_ALIGN_LEFT,
5029                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
5030                       "xalign", 0.0,
5031                       "yalign", 0.5,
5032                       NULL);
5033     }
5034 }
5035
5036 static void
5037 update_pixbuf_cell (GtkIconView *icon_view)
5038 {
5039   if (!icon_view->priv->cell_area)
5040     return;
5041
5042   if (icon_view->priv->pixbuf_column == -1)
5043     {
5044       if (icon_view->priv->pixbuf_cell != NULL)
5045         {
5046           gtk_cell_area_remove (icon_view->priv->cell_area, 
5047                                 icon_view->priv->pixbuf_cell);
5048
5049           icon_view->priv->pixbuf_cell = NULL;
5050         }
5051     }
5052   else 
5053     {
5054       if (icon_view->priv->pixbuf_cell == NULL)
5055         {
5056           icon_view->priv->pixbuf_cell = gtk_cell_renderer_pixbuf_new ();
5057           
5058           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), icon_view->priv->pixbuf_cell, FALSE);
5059         }
5060       
5061       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5062                                       icon_view->priv->pixbuf_cell, 
5063                                       "pixbuf", icon_view->priv->pixbuf_column, 
5064                                       NULL);
5065
5066       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
5067         g_object_set (icon_view->priv->pixbuf_cell,
5068                       "xalign", 0.5,
5069                       "yalign", 1.0,
5070                       NULL);
5071       else
5072         g_object_set (icon_view->priv->pixbuf_cell,
5073                       "xalign", 0.0,
5074                       "yalign", 0.0,
5075                       NULL);
5076     }
5077 }
5078
5079 /**
5080  * gtk_icon_view_set_text_column:
5081  * @icon_view: A #GtkIconView.
5082  * @column: A column in the currently used model, or -1 to display no text
5083  * 
5084  * Sets the column with text for @icon_view to be @column. The text
5085  * column must be of type #G_TYPE_STRING.
5086  *
5087  * Since: 2.6 
5088  **/
5089 void
5090 gtk_icon_view_set_text_column (GtkIconView *icon_view,
5091                                gint          column)
5092 {
5093   if (column == icon_view->priv->text_column)
5094     return;
5095   
5096   if (column == -1)
5097     icon_view->priv->text_column = -1;
5098   else
5099     {
5100       if (icon_view->priv->model != NULL)
5101         {
5102           GType column_type;
5103           
5104           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5105
5106           g_return_if_fail (column_type == G_TYPE_STRING);
5107         }
5108       
5109       icon_view->priv->text_column = column;
5110     }
5111
5112   if (icon_view->priv->cell_area)
5113     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5114
5115   update_text_cell (icon_view);
5116
5117   gtk_icon_view_invalidate_sizes (icon_view);
5118   
5119   g_object_notify (G_OBJECT (icon_view), "text-column");
5120 }
5121
5122 /**
5123  * gtk_icon_view_get_text_column:
5124  * @icon_view: A #GtkIconView.
5125  *
5126  * Returns the column with text for @icon_view.
5127  *
5128  * Returns: the text column, or -1 if it's unset.
5129  *
5130  * Since: 2.6
5131  */
5132 gint
5133 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
5134 {
5135   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5136
5137   return icon_view->priv->text_column;
5138 }
5139
5140 /**
5141  * gtk_icon_view_set_markup_column:
5142  * @icon_view: A #GtkIconView.
5143  * @column: A column in the currently used model, or -1 to display no text
5144  * 
5145  * Sets the column with markup information for @icon_view to be
5146  * @column. The markup column must be of type #G_TYPE_STRING.
5147  * If the markup column is set to something, it overrides
5148  * the text column set by gtk_icon_view_set_text_column().
5149  *
5150  * Since: 2.6
5151  **/
5152 void
5153 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5154                                  gint         column)
5155 {
5156   if (column == icon_view->priv->markup_column)
5157     return;
5158   
5159   if (column == -1)
5160     icon_view->priv->markup_column = -1;
5161   else
5162     {
5163       if (icon_view->priv->model != NULL)
5164         {
5165           GType column_type;
5166           
5167           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5168
5169           g_return_if_fail (column_type == G_TYPE_STRING);
5170         }
5171       
5172       icon_view->priv->markup_column = column;
5173     }
5174
5175   if (icon_view->priv->cell_area)
5176     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5177
5178   update_text_cell (icon_view);
5179
5180   gtk_icon_view_invalidate_sizes (icon_view);
5181   
5182   g_object_notify (G_OBJECT (icon_view), "markup-column");
5183 }
5184
5185 /**
5186  * gtk_icon_view_get_markup_column:
5187  * @icon_view: A #GtkIconView.
5188  *
5189  * Returns the column with markup text for @icon_view.
5190  *
5191  * Returns: the markup column, or -1 if it's unset.
5192  *
5193  * Since: 2.6
5194  */
5195 gint
5196 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
5197 {
5198   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5199
5200   return icon_view->priv->markup_column;
5201 }
5202
5203 /**
5204  * gtk_icon_view_set_pixbuf_column:
5205  * @icon_view: A #GtkIconView.
5206  * @column: A column in the currently used model, or -1 to disable
5207  * 
5208  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5209  * column must be of type #GDK_TYPE_PIXBUF
5210  *
5211  * Since: 2.6 
5212  **/
5213 void
5214 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5215                                  gint         column)
5216 {
5217   if (column == icon_view->priv->pixbuf_column)
5218     return;
5219   
5220   if (column == -1)
5221     icon_view->priv->pixbuf_column = -1;
5222   else
5223     {
5224       if (icon_view->priv->model != NULL)
5225         {
5226           GType column_type;
5227           
5228           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5229
5230           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5231         }
5232       
5233       icon_view->priv->pixbuf_column = column;
5234     }
5235
5236   if (icon_view->priv->cell_area)
5237     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5238
5239   update_pixbuf_cell (icon_view);
5240
5241   gtk_icon_view_invalidate_sizes (icon_view);
5242   
5243   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5244   
5245 }
5246
5247 /**
5248  * gtk_icon_view_get_pixbuf_column:
5249  * @icon_view: A #GtkIconView.
5250  *
5251  * Returns the column with pixbufs for @icon_view.
5252  *
5253  * Returns: the pixbuf column, or -1 if it's unset.
5254  *
5255  * Since: 2.6
5256  */
5257 gint
5258 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
5259 {
5260   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5261
5262   return icon_view->priv->pixbuf_column;
5263 }
5264
5265 /**
5266  * gtk_icon_view_select_path:
5267  * @icon_view: A #GtkIconView.
5268  * @path: The #GtkTreePath to be selected.
5269  * 
5270  * Selects the row at @path.
5271  *
5272  * Since: 2.6
5273  **/
5274 void
5275 gtk_icon_view_select_path (GtkIconView *icon_view,
5276                            GtkTreePath *path)
5277 {
5278   GtkIconViewItem *item = NULL;
5279
5280   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5281   g_return_if_fail (icon_view->priv->model != NULL);
5282   g_return_if_fail (path != NULL);
5283
5284   if (gtk_tree_path_get_depth (path) > 0)
5285     item = g_list_nth_data (icon_view->priv->items,
5286                             gtk_tree_path_get_indices(path)[0]);
5287
5288   if (item)
5289     _gtk_icon_view_select_item (icon_view, item);
5290 }
5291
5292 /**
5293  * gtk_icon_view_unselect_path:
5294  * @icon_view: A #GtkIconView.
5295  * @path: The #GtkTreePath to be unselected.
5296  * 
5297  * Unselects the row at @path.
5298  *
5299  * Since: 2.6
5300  **/
5301 void
5302 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5303                              GtkTreePath *path)
5304 {
5305   GtkIconViewItem *item;
5306   
5307   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5308   g_return_if_fail (icon_view->priv->model != NULL);
5309   g_return_if_fail (path != NULL);
5310
5311   item = g_list_nth_data (icon_view->priv->items,
5312                           gtk_tree_path_get_indices(path)[0]);
5313
5314   if (!item)
5315     return;
5316   
5317   _gtk_icon_view_unselect_item (icon_view, item);
5318 }
5319
5320 /**
5321  * gtk_icon_view_get_selected_items:
5322  * @icon_view: A #GtkIconView.
5323  *
5324  * Creates a list of paths of all selected items. Additionally, if you are
5325  * planning on modifying the model after calling this function, you may
5326  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5327  * To do this, you can use gtk_tree_row_reference_new().
5328  *
5329  * To free the return value, use:
5330  * |[
5331  * g_list_free_full (list, (GDestroyNotify) gtk_tree_patch_free);
5332  * ]|
5333  *
5334  * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
5335  *
5336  * Since: 2.6
5337  **/
5338 GList *
5339 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5340 {
5341   GList *list;
5342   GList *selected = NULL;
5343   
5344   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5345   
5346   for (list = icon_view->priv->items; list != NULL; list = list->next)
5347     {
5348       GtkIconViewItem *item = list->data;
5349
5350       if (item->selected)
5351         {
5352           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5353
5354           selected = g_list_prepend (selected, path);
5355         }
5356     }
5357
5358   return selected;
5359 }
5360
5361 /**
5362  * gtk_icon_view_select_all:
5363  * @icon_view: A #GtkIconView.
5364  * 
5365  * Selects all the icons. @icon_view must has its selection mode set
5366  * to #GTK_SELECTION_MULTIPLE.
5367  *
5368  * Since: 2.6
5369  **/
5370 void
5371 gtk_icon_view_select_all (GtkIconView *icon_view)
5372 {
5373   GList *items;
5374   gboolean dirty = FALSE;
5375   
5376   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5377
5378   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5379     return;
5380
5381   for (items = icon_view->priv->items; items; items = items->next)
5382     {
5383       GtkIconViewItem *item = items->data;
5384       
5385       if (!item->selected)
5386         {
5387           dirty = TRUE;
5388           item->selected = TRUE;
5389           gtk_icon_view_queue_draw_item (icon_view, item);
5390         }
5391     }
5392
5393   if (dirty)
5394     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5395 }
5396
5397 /**
5398  * gtk_icon_view_unselect_all:
5399  * @icon_view: A #GtkIconView.
5400  * 
5401  * Unselects all the icons.
5402  *
5403  * Since: 2.6
5404  **/
5405 void
5406 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5407 {
5408   gboolean dirty = FALSE;
5409   
5410   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5411
5412   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5413     return;
5414
5415   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5416
5417   if (dirty)
5418     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5419 }
5420
5421 /**
5422  * gtk_icon_view_path_is_selected:
5423  * @icon_view: A #GtkIconView.
5424  * @path: A #GtkTreePath to check selection on.
5425  * 
5426  * Returns %TRUE if the icon pointed to by @path is currently
5427  * selected. If @path does not point to a valid location, %FALSE is returned.
5428  * 
5429  * Return value: %TRUE if @path is selected.
5430  *
5431  * Since: 2.6
5432  **/
5433 gboolean
5434 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5435                                 GtkTreePath *path)
5436 {
5437   GtkIconViewItem *item;
5438   
5439   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5440   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5441   g_return_val_if_fail (path != NULL, FALSE);
5442   
5443   item = g_list_nth_data (icon_view->priv->items,
5444                           gtk_tree_path_get_indices(path)[0]);
5445
5446   if (!item)
5447     return FALSE;
5448   
5449   return item->selected;
5450 }
5451
5452 /**
5453  * gtk_icon_view_get_item_row:
5454  * @icon_view: a #GtkIconView
5455  * @path: the #GtkTreePath of the item
5456  *
5457  * Gets the row in which the item @path is currently
5458  * displayed. Row numbers start at 0.
5459  *
5460  * Returns: The row in which the item is displayed
5461  *
5462  * Since: 2.22
5463  */
5464 gint
5465 gtk_icon_view_get_item_row (GtkIconView *icon_view,
5466                             GtkTreePath *path)
5467 {
5468   GtkIconViewItem *item;
5469
5470   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5471   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5472   g_return_val_if_fail (path != NULL, -1);
5473
5474   item = g_list_nth_data (icon_view->priv->items,
5475                           gtk_tree_path_get_indices(path)[0]);
5476
5477   if (!item)
5478     return -1;
5479
5480   return item->row;
5481 }
5482
5483 /**
5484  * gtk_icon_view_get_item_column:
5485  * @icon_view: a #GtkIconView
5486  * @path: the #GtkTreePath of the item
5487  *
5488  * Gets the column in which the item @path is currently
5489  * displayed. Column numbers start at 0.
5490  *
5491  * Returns: The column in which the item is displayed
5492  *
5493  * Since: 2.22
5494  */
5495 gint
5496 gtk_icon_view_get_item_column (GtkIconView *icon_view,
5497                                GtkTreePath *path)
5498 {
5499   GtkIconViewItem *item;
5500
5501   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5502   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5503   g_return_val_if_fail (path != NULL, -1);
5504
5505   item = g_list_nth_data (icon_view->priv->items,
5506                           gtk_tree_path_get_indices(path)[0]);
5507
5508   if (!item)
5509     return -1;
5510
5511   return item->col;
5512 }
5513
5514 /**
5515  * gtk_icon_view_item_activated:
5516  * @icon_view: A #GtkIconView
5517  * @path: The #GtkTreePath to be activated
5518  * 
5519  * Activates the item determined by @path.
5520  *
5521  * Since: 2.6
5522  **/
5523 void
5524 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5525                               GtkTreePath      *path)
5526 {
5527   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5528   g_return_if_fail (path != NULL);
5529   
5530   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5531 }
5532
5533 /**
5534  * gtk_icon_view_set_item_orientation:
5535  * @icon_view: a #GtkIconView
5536  * @orientation: the relative position of texts and icons 
5537  * 
5538  * Sets the ::item-orientation property which determines whether the labels 
5539  * are drawn beside the icons instead of below.
5540  *
5541  * Since: 2.6
5542  **/
5543 void 
5544 gtk_icon_view_set_item_orientation (GtkIconView    *icon_view,
5545                                     GtkOrientation  orientation)
5546 {
5547   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5548
5549   if (icon_view->priv->item_orientation != orientation)
5550     {
5551       icon_view->priv->item_orientation = orientation;
5552
5553       if (icon_view->priv->cell_area)
5554         {
5555           if (GTK_IS_ORIENTABLE (icon_view->priv->cell_area))
5556             gtk_orientable_set_orientation (GTK_ORIENTABLE (icon_view->priv->cell_area), 
5557                                             icon_view->priv->item_orientation);
5558
5559           gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5560         }
5561
5562       gtk_icon_view_invalidate_sizes (icon_view);
5563
5564       update_text_cell (icon_view);
5565       update_pixbuf_cell (icon_view);
5566       
5567       g_object_notify (G_OBJECT (icon_view), "item-orientation");
5568     }
5569 }
5570
5571 /**
5572  * gtk_icon_view_get_item_orientation:
5573  * @icon_view: a #GtkIconView
5574  * 
5575  * Returns the value of the ::item-orientation property which determines 
5576  * whether the labels are drawn beside the icons instead of below. 
5577  * 
5578  * Return value: the relative position of texts and icons 
5579  *
5580  * Since: 2.6
5581  **/
5582 GtkOrientation
5583 gtk_icon_view_get_item_orientation (GtkIconView *icon_view)
5584 {
5585   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5586                         GTK_ORIENTATION_VERTICAL);
5587
5588   return icon_view->priv->item_orientation;
5589 }
5590
5591 /**
5592  * gtk_icon_view_set_columns:
5593  * @icon_view: a #GtkIconView
5594  * @columns: the number of columns
5595  * 
5596  * Sets the ::columns property which determines in how
5597  * many columns the icons are arranged. If @columns is
5598  * -1, the number of columns will be chosen automatically 
5599  * to fill the available area. 
5600  *
5601  * Since: 2.6
5602  */
5603 void 
5604 gtk_icon_view_set_columns (GtkIconView *icon_view,
5605                            gint         columns)
5606 {
5607   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5608   
5609   if (icon_view->priv->columns != columns)
5610     {
5611       icon_view->priv->columns = columns;
5612
5613       if (icon_view->priv->cell_area)
5614         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5615
5616       gtk_widget_queue_resize (GTK_WIDGET (icon_view));
5617       
5618       g_object_notify (G_OBJECT (icon_view), "columns");
5619     }  
5620 }
5621
5622 /**
5623  * gtk_icon_view_get_columns:
5624  * @icon_view: a #GtkIconView
5625  * 
5626  * Returns the value of the ::columns property.
5627  * 
5628  * Return value: the number of columns, or -1
5629  *
5630  * Since: 2.6
5631  */
5632 gint
5633 gtk_icon_view_get_columns (GtkIconView *icon_view)
5634 {
5635   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5636
5637   return icon_view->priv->columns;
5638 }
5639
5640 /**
5641  * gtk_icon_view_set_item_width:
5642  * @icon_view: a #GtkIconView
5643  * @item_width: the width for each item
5644  * 
5645  * Sets the ::item-width property which specifies the width 
5646  * to use for each item. If it is set to -1, the icon view will 
5647  * automatically determine a suitable item size.
5648  *
5649  * Since: 2.6
5650  */
5651 void 
5652 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5653                               gint         item_width)
5654 {
5655   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5656   
5657   if (icon_view->priv->item_width != item_width)
5658     {
5659       icon_view->priv->item_width = item_width;
5660       
5661       if (icon_view->priv->cell_area)
5662         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5663
5664       gtk_icon_view_invalidate_sizes (icon_view);
5665       
5666       update_text_cell (icon_view);
5667
5668       g_object_notify (G_OBJECT (icon_view), "item-width");
5669     }  
5670 }
5671
5672 /**
5673  * gtk_icon_view_get_item_width:
5674  * @icon_view: a #GtkIconView
5675  * 
5676  * Returns the value of the ::item-width property.
5677  * 
5678  * Return value: the width of a single item, or -1
5679  *
5680  * Since: 2.6
5681  */
5682 gint
5683 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5684 {
5685   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5686
5687   return icon_view->priv->item_width;
5688 }
5689
5690
5691 /**
5692  * gtk_icon_view_set_spacing:
5693  * @icon_view: a #GtkIconView
5694  * @spacing: the spacing
5695  * 
5696  * Sets the ::spacing property which specifies the space 
5697  * which is inserted between the cells (i.e. the icon and 
5698  * the text) of an item.
5699  *
5700  * Since: 2.6
5701  */
5702 void 
5703 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5704                            gint         spacing)
5705 {
5706   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5707   
5708   if (icon_view->priv->spacing != spacing)
5709     {
5710       icon_view->priv->spacing = spacing;
5711
5712       if (icon_view->priv->cell_area)
5713         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5714
5715       gtk_icon_view_invalidate_sizes (icon_view);
5716
5717       g_object_notify (G_OBJECT (icon_view), "spacing");
5718     }  
5719 }
5720
5721 /**
5722  * gtk_icon_view_get_spacing:
5723  * @icon_view: a #GtkIconView
5724  * 
5725  * Returns the value of the ::spacing property.
5726  * 
5727  * Return value: the space between cells 
5728  *
5729  * Since: 2.6
5730  */
5731 gint
5732 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5733 {
5734   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5735
5736   return icon_view->priv->spacing;
5737 }
5738
5739 /**
5740  * gtk_icon_view_set_row_spacing:
5741  * @icon_view: a #GtkIconView
5742  * @row_spacing: the row spacing
5743  * 
5744  * Sets the ::row-spacing property which specifies the space 
5745  * which is inserted between the rows of the icon view.
5746  *
5747  * Since: 2.6
5748  */
5749 void 
5750 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5751                                gint         row_spacing)
5752 {
5753   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5754   
5755   if (icon_view->priv->row_spacing != row_spacing)
5756     {
5757       icon_view->priv->row_spacing = row_spacing;
5758
5759       if (icon_view->priv->cell_area)
5760         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5761
5762       gtk_icon_view_invalidate_sizes (icon_view);
5763
5764       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5765     }  
5766 }
5767
5768 /**
5769  * gtk_icon_view_get_row_spacing:
5770  * @icon_view: a #GtkIconView
5771  * 
5772  * Returns the value of the ::row-spacing property.
5773  * 
5774  * Return value: the space between rows
5775  *
5776  * Since: 2.6
5777  */
5778 gint
5779 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5780 {
5781   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5782
5783   return icon_view->priv->row_spacing;
5784 }
5785
5786 /**
5787  * gtk_icon_view_set_column_spacing:
5788  * @icon_view: a #GtkIconView
5789  * @column_spacing: the column spacing
5790  * 
5791  * Sets the ::column-spacing property which specifies the space 
5792  * which is inserted between the columns of the icon view.
5793  *
5794  * Since: 2.6
5795  */
5796 void 
5797 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5798                                   gint         column_spacing)
5799 {
5800   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5801   
5802   if (icon_view->priv->column_spacing != column_spacing)
5803     {
5804       icon_view->priv->column_spacing = column_spacing;
5805
5806       if (icon_view->priv->cell_area)
5807         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5808
5809       gtk_icon_view_invalidate_sizes (icon_view);
5810
5811       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5812     }  
5813 }
5814
5815 /**
5816  * gtk_icon_view_get_column_spacing:
5817  * @icon_view: a #GtkIconView
5818  * 
5819  * Returns the value of the ::column-spacing property.
5820  * 
5821  * Return value: the space between columns
5822  *
5823  * Since: 2.6
5824  */
5825 gint
5826 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5827 {
5828   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5829
5830   return icon_view->priv->column_spacing;
5831 }
5832
5833 /**
5834  * gtk_icon_view_set_margin:
5835  * @icon_view: a #GtkIconView
5836  * @margin: the margin
5837  * 
5838  * Sets the ::margin property which specifies the space 
5839  * which is inserted at the top, bottom, left and right 
5840  * of the icon view.
5841  *
5842  * Since: 2.6
5843  */
5844 void 
5845 gtk_icon_view_set_margin (GtkIconView *icon_view,
5846                           gint         margin)
5847 {
5848   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5849   
5850   if (icon_view->priv->margin != margin)
5851     {
5852       icon_view->priv->margin = margin;
5853
5854       if (icon_view->priv->cell_area)
5855         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5856
5857       gtk_icon_view_invalidate_sizes (icon_view);
5858
5859       g_object_notify (G_OBJECT (icon_view), "margin");
5860     }  
5861 }
5862
5863 /**
5864  * gtk_icon_view_get_margin:
5865  * @icon_view: a #GtkIconView
5866  * 
5867  * Returns the value of the ::margin property.
5868  * 
5869  * Return value: the space at the borders 
5870  *
5871  * Since: 2.6
5872  */
5873 gint
5874 gtk_icon_view_get_margin (GtkIconView *icon_view)
5875 {
5876   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5877
5878   return icon_view->priv->margin;
5879 }
5880
5881 /**
5882  * gtk_icon_view_set_item_padding:
5883  * @icon_view: a #GtkIconView
5884  * @item_padding: the item padding
5885  *
5886  * Sets the #GtkIconView:item-padding property which specifies the padding
5887  * around each of the icon view's items.
5888  *
5889  * Since: 2.18
5890  */
5891 void
5892 gtk_icon_view_set_item_padding (GtkIconView *icon_view,
5893                                 gint         item_padding)
5894 {
5895   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5896   
5897   if (icon_view->priv->item_padding != item_padding)
5898     {
5899       icon_view->priv->item_padding = item_padding;
5900
5901       if (icon_view->priv->cell_area)
5902         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5903
5904       gtk_icon_view_invalidate_sizes (icon_view);
5905
5906       g_object_notify (G_OBJECT (icon_view), "item-padding");
5907     }  
5908 }
5909
5910 /**
5911  * gtk_icon_view_get_item_padding:
5912  * @icon_view: a #GtkIconView
5913  * 
5914  * Returns the value of the ::item-padding property.
5915  * 
5916  * Return value: the padding around items
5917  *
5918  * Since: 2.18
5919  */
5920 gint
5921 gtk_icon_view_get_item_padding (GtkIconView *icon_view)
5922 {
5923   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5924
5925   return icon_view->priv->item_padding;
5926 }
5927
5928 /* Get/set whether drag_motion requested the drag data and
5929  * drag_data_received should thus not actually insert the data,
5930  * since the data doesn't result from a drop.
5931  */
5932 static void
5933 set_status_pending (GdkDragContext *context,
5934                     GdkDragAction   suggested_action)
5935 {
5936   g_object_set_data (G_OBJECT (context),
5937                      I_("gtk-icon-view-status-pending"),
5938                      GINT_TO_POINTER (suggested_action));
5939 }
5940
5941 static GdkDragAction
5942 get_status_pending (GdkDragContext *context)
5943 {
5944   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5945                                              "gtk-icon-view-status-pending"));
5946 }
5947
5948 static void
5949 unset_reorderable (GtkIconView *icon_view)
5950 {
5951   if (icon_view->priv->reorderable)
5952     {
5953       icon_view->priv->reorderable = FALSE;
5954       g_object_notify (G_OBJECT (icon_view), "reorderable");
5955     }
5956 }
5957
5958 static void
5959 set_source_row (GdkDragContext *context,
5960                 GtkTreeModel   *model,
5961                 GtkTreePath    *source_row)
5962 {
5963   if (source_row)
5964     g_object_set_data_full (G_OBJECT (context),
5965                             I_("gtk-icon-view-source-row"),
5966                             gtk_tree_row_reference_new (model, source_row),
5967                             (GDestroyNotify) gtk_tree_row_reference_free);
5968   else
5969     g_object_set_data_full (G_OBJECT (context),
5970                             I_("gtk-icon-view-source-row"),
5971                             NULL, NULL);
5972 }
5973
5974 static GtkTreePath*
5975 get_source_row (GdkDragContext *context)
5976 {
5977   GtkTreeRowReference *ref;
5978
5979   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5980
5981   if (ref)
5982     return gtk_tree_row_reference_get_path (ref);
5983   else
5984     return NULL;
5985 }
5986
5987 typedef struct
5988 {
5989   GtkTreeRowReference *dest_row;
5990   gboolean             empty_view_drop;
5991   gboolean             drop_append_mode;
5992 } DestRow;
5993
5994 static void
5995 dest_row_free (gpointer data)
5996 {
5997   DestRow *dr = (DestRow *)data;
5998
5999   gtk_tree_row_reference_free (dr->dest_row);
6000   g_free (dr);
6001 }
6002
6003 static void
6004 set_dest_row (GdkDragContext *context,
6005               GtkTreeModel   *model,
6006               GtkTreePath    *dest_row,
6007               gboolean        empty_view_drop,
6008               gboolean        drop_append_mode)
6009 {
6010   DestRow *dr;
6011
6012   if (!dest_row)
6013     {
6014       g_object_set_data_full (G_OBJECT (context),
6015                               I_("gtk-icon-view-dest-row"),
6016                               NULL, NULL);
6017       return;
6018     }
6019   
6020   dr = g_new0 (DestRow, 1);
6021      
6022   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
6023   dr->empty_view_drop = empty_view_drop;
6024   dr->drop_append_mode = drop_append_mode;
6025   g_object_set_data_full (G_OBJECT (context),
6026                           I_("gtk-icon-view-dest-row"),
6027                           dr, (GDestroyNotify) dest_row_free);
6028 }
6029
6030 static GtkTreePath*
6031 get_dest_row (GdkDragContext *context)
6032 {
6033   DestRow *dr;
6034
6035   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
6036
6037   if (dr)
6038     {
6039       GtkTreePath *path = NULL;
6040       
6041       if (dr->dest_row)
6042         path = gtk_tree_row_reference_get_path (dr->dest_row);
6043       else if (dr->empty_view_drop)
6044         path = gtk_tree_path_new_from_indices (0, -1);
6045       else
6046         path = NULL;
6047
6048       if (path && dr->drop_append_mode)
6049         gtk_tree_path_next (path);
6050
6051       return path;
6052     }
6053   else
6054     return NULL;
6055 }
6056
6057 static gboolean
6058 check_model_dnd (GtkTreeModel *model,
6059                  GType         required_iface,
6060                  const gchar  *signal)
6061 {
6062   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
6063     {
6064       g_warning ("You must override the default '%s' handler "
6065                  "on GtkIconView when using models that don't support "
6066                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
6067                  "is to connect to '%s' and call "
6068                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
6069                  "the default handler from running. Look at the source code "
6070                  "for the default handler in gtkiconview.c to get an idea what "
6071                  "your handler should do. (gtkiconview.c is in the GTK+ source "
6072                  "code.) If you're using GTK+ from a language other than C, "
6073                  "there may be a more natural way to override default handlers, e.g. via derivation.",
6074                  signal, g_type_name (required_iface), signal);
6075       return FALSE;
6076     }
6077   else
6078     return TRUE;
6079 }
6080
6081 static void
6082 remove_scroll_timeout (GtkIconView *icon_view)
6083 {
6084   if (icon_view->priv->scroll_timeout_id != 0)
6085     {
6086       g_source_remove (icon_view->priv->scroll_timeout_id);
6087
6088       icon_view->priv->scroll_timeout_id = 0;
6089     }
6090 }
6091
6092 static void
6093 gtk_icon_view_autoscroll (GtkIconView *icon_view,
6094                           GdkDevice   *device)
6095 {
6096   GdkWindow *window;
6097   gint px, py, width, height;
6098   gint hoffset, voffset;
6099
6100   window = gtk_widget_get_window (GTK_WIDGET (icon_view));
6101
6102   gdk_window_get_device_position (window, device, &px, &py, NULL);
6103   gdk_window_get_geometry (window, NULL, NULL, &width, &height);
6104
6105   /* see if we are near the edge. */
6106   voffset = py - 2 * SCROLL_EDGE_SIZE;
6107   if (voffset > 0)
6108     voffset = MAX (py - (height - 2 * SCROLL_EDGE_SIZE), 0);
6109
6110   hoffset = px - 2 * SCROLL_EDGE_SIZE;
6111   if (hoffset > 0)
6112     hoffset = MAX (px - (width - 2 * SCROLL_EDGE_SIZE), 0);
6113
6114   if (voffset != 0)
6115     gtk_adjustment_set_value (icon_view->priv->vadjustment,
6116                               gtk_adjustment_get_value (icon_view->priv->vadjustment) + voffset);
6117
6118   if (hoffset != 0)
6119     gtk_adjustment_set_value (icon_view->priv->hadjustment,
6120                               gtk_adjustment_get_value (icon_view->priv->hadjustment) + hoffset);
6121 }
6122
6123 typedef struct {
6124   GtkIconView *icon_view;
6125   GdkDevice   *device;
6126 } DragScrollData;
6127
6128 static gboolean
6129 drag_scroll_timeout (gpointer datap)
6130 {
6131   DragScrollData *data = datap;
6132
6133   gtk_icon_view_autoscroll (data->icon_view, data->device);
6134
6135   return TRUE;
6136 }
6137
6138 static void
6139 drag_scroll_data_free (DragScrollData *data)
6140 {
6141   g_slice_free (DragScrollData, data);
6142 }
6143
6144 static gboolean
6145 set_destination (GtkIconView    *icon_view,
6146                  GdkDragContext *context,
6147                  gint            x,
6148                  gint            y,
6149                  GdkDragAction  *suggested_action,
6150                  GdkAtom        *target)
6151 {
6152   GtkWidget *widget;
6153   GtkTreePath *path = NULL;
6154   GtkIconViewDropPosition pos;
6155   GtkIconViewDropPosition old_pos;
6156   GtkTreePath *old_dest_path = NULL;
6157   gboolean can_drop = FALSE;
6158
6159   widget = GTK_WIDGET (icon_view);
6160
6161   *suggested_action = 0;
6162   *target = GDK_NONE;
6163
6164   if (!icon_view->priv->dest_set)
6165     {
6166       /* someone unset us as a drag dest, note that if
6167        * we return FALSE drag_leave isn't called
6168        */
6169
6170       gtk_icon_view_set_drag_dest_item (icon_view,
6171                                         NULL,
6172                                         GTK_ICON_VIEW_DROP_LEFT);
6173
6174       remove_scroll_timeout (GTK_ICON_VIEW (widget));
6175
6176       return FALSE; /* no longer a drop site */
6177     }
6178
6179   *target = gtk_drag_dest_find_target (widget, context,
6180                                        gtk_drag_dest_get_target_list (widget));
6181   if (*target == GDK_NONE)
6182     return FALSE;
6183
6184   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
6185     {
6186       gint n_children;
6187       GtkTreeModel *model;
6188       
6189       /* the row got dropped on empty space, let's setup a special case
6190        */
6191
6192       if (path)
6193         gtk_tree_path_free (path);
6194
6195       model = gtk_icon_view_get_model (icon_view);
6196
6197       n_children = gtk_tree_model_iter_n_children (model, NULL);
6198       if (n_children)
6199         {
6200           pos = GTK_ICON_VIEW_DROP_BELOW;
6201           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6202         }
6203       else
6204         {
6205           pos = GTK_ICON_VIEW_DROP_ABOVE;
6206           path = gtk_tree_path_new_from_indices (0, -1);
6207         }
6208
6209       can_drop = TRUE;
6210
6211       goto out;
6212     }
6213
6214   g_assert (path);
6215
6216   gtk_icon_view_get_drag_dest_item (icon_view,
6217                                     &old_dest_path,
6218                                     &old_pos);
6219   
6220   if (old_dest_path)
6221     gtk_tree_path_free (old_dest_path);
6222   
6223   if (TRUE /* FIXME if the location droppable predicate */)
6224     {
6225       can_drop = TRUE;
6226     }
6227
6228 out:
6229   if (can_drop)
6230     {
6231       GtkWidget *source_widget;
6232
6233       *suggested_action = gdk_drag_context_get_suggested_action (context);
6234       source_widget = gtk_drag_get_source_widget (context);
6235
6236       if (source_widget == widget)
6237         {
6238           /* Default to MOVE, unless the user has
6239            * pressed ctrl or shift to affect available actions
6240            */
6241           if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6242             *suggested_action = GDK_ACTION_MOVE;
6243         }
6244
6245       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6246                                         path, pos);
6247     }
6248   else
6249     {
6250       /* can't drop here */
6251       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6252                                         NULL,
6253                                         GTK_ICON_VIEW_DROP_LEFT);
6254     }
6255   
6256   if (path)
6257     gtk_tree_path_free (path);
6258   
6259   return TRUE;
6260 }
6261
6262 static GtkTreePath*
6263 get_logical_destination (GtkIconView *icon_view,
6264                          gboolean    *drop_append_mode)
6265 {
6266   /* adjust path to point to the row the drop goes in front of */
6267   GtkTreePath *path = NULL;
6268   GtkIconViewDropPosition pos;
6269   
6270   *drop_append_mode = FALSE;
6271
6272   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6273
6274   if (path == NULL)
6275     return NULL;
6276
6277   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
6278       pos == GTK_ICON_VIEW_DROP_BELOW)
6279     {
6280       GtkTreeIter iter;
6281       GtkTreeModel *model = icon_view->priv->model;
6282
6283       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6284           !gtk_tree_model_iter_next (model, &iter))
6285         *drop_append_mode = TRUE;
6286       else
6287         {
6288           *drop_append_mode = FALSE;
6289           gtk_tree_path_next (path);
6290         }      
6291     }
6292
6293   return path;
6294 }
6295
6296 static gboolean
6297 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
6298                                 GdkEventMotion *event)
6299 {
6300   GtkWidget *widget = GTK_WIDGET (icon_view);
6301   GdkDragContext *context;
6302   GtkTreePath *path = NULL;
6303   gint button;
6304   GtkTreeModel *model;
6305   gboolean retval = FALSE;
6306
6307   if (!icon_view->priv->source_set)
6308     goto out;
6309
6310   if (icon_view->priv->pressed_button < 0)
6311     goto out;
6312
6313   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6314                                  icon_view->priv->press_start_x,
6315                                  icon_view->priv->press_start_y,
6316                                  event->x, event->y))
6317     goto out;
6318
6319   model = gtk_icon_view_get_model (icon_view);
6320
6321   if (model == NULL)
6322     goto out;
6323
6324   button = icon_view->priv->pressed_button;
6325   icon_view->priv->pressed_button = -1;
6326
6327   path = gtk_icon_view_get_path_at_pos (icon_view,
6328                                         icon_view->priv->press_start_x,
6329                                         icon_view->priv->press_start_y);
6330
6331   if (path == NULL)
6332     goto out;
6333
6334   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6335       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6336                                            path))
6337     goto out;
6338
6339   /* FIXME Check whether we're a start button, if not return FALSE and
6340    * free path
6341    */
6342
6343   /* Now we can begin the drag */
6344   
6345   retval = TRUE;
6346
6347   context = gtk_drag_begin (widget,
6348                             gtk_drag_source_get_target_list (widget),
6349                             icon_view->priv->source_actions,
6350                             button,
6351                             (GdkEvent*)event);
6352
6353   set_source_row (context, model, path);
6354   
6355  out:
6356   if (path)
6357     gtk_tree_path_free (path);
6358
6359   return retval;
6360 }
6361
6362 /* Source side drag signals */
6363 static void 
6364 gtk_icon_view_drag_begin (GtkWidget      *widget,
6365                           GdkDragContext *context)
6366 {
6367   GtkIconView *icon_view;
6368   GtkIconViewItem *item;
6369   cairo_surface_t *icon;
6370   gint x, y;
6371   GtkTreePath *path;
6372
6373   icon_view = GTK_ICON_VIEW (widget);
6374
6375   /* if the user uses a custom DnD impl, we don't set the icon here */
6376   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6377     return;
6378
6379   item = _gtk_icon_view_get_item_at_coords (icon_view,
6380                                            icon_view->priv->press_start_x,
6381                                            icon_view->priv->press_start_y,
6382                                            TRUE,
6383                                            NULL);
6384
6385   g_return_if_fail (item != NULL);
6386
6387   x = icon_view->priv->press_start_x - item->cell_area.x + 1;
6388   y = icon_view->priv->press_start_y - item->cell_area.y + 1;
6389   
6390   path = gtk_tree_path_new_from_indices (item->index, -1);
6391   icon = gtk_icon_view_create_drag_icon (icon_view, path);
6392   gtk_tree_path_free (path);
6393
6394   cairo_surface_set_device_offset (icon, -x, -y);
6395
6396   gtk_drag_set_icon_surface (context, icon);
6397
6398   cairo_surface_destroy (icon);
6399 }
6400
6401 static void 
6402 gtk_icon_view_drag_end (GtkWidget      *widget,
6403                         GdkDragContext *context)
6404 {
6405   /* do nothing */
6406 }
6407
6408 static void 
6409 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6410                              GdkDragContext   *context,
6411                              GtkSelectionData *selection_data,
6412                              guint             info,
6413                              guint             time)
6414 {
6415   GtkIconView *icon_view;
6416   GtkTreeModel *model;
6417   GtkTreePath *source_row;
6418
6419   icon_view = GTK_ICON_VIEW (widget);
6420   model = gtk_icon_view_get_model (icon_view);
6421
6422   if (model == NULL)
6423     return;
6424
6425   if (!icon_view->priv->source_set)
6426     return;
6427
6428   source_row = get_source_row (context);
6429
6430   if (source_row == NULL)
6431     return;
6432
6433   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6434    * any model; for DragSource models there are some other targets
6435    * we also support.
6436    */
6437
6438   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6439       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6440                                           source_row,
6441                                           selection_data))
6442     goto done;
6443
6444   /* If drag_data_get does nothing, try providing row data. */
6445   if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6446     gtk_tree_set_row_drag_data (selection_data,
6447                                 model,
6448                                 source_row);
6449
6450  done:
6451   gtk_tree_path_free (source_row);
6452 }
6453
6454 static void 
6455 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6456                                 GdkDragContext *context)
6457 {
6458   GtkTreeModel *model;
6459   GtkIconView *icon_view;
6460   GtkTreePath *source_row;
6461
6462   icon_view = GTK_ICON_VIEW (widget);
6463   model = gtk_icon_view_get_model (icon_view);
6464
6465   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6466     return;
6467
6468   if (!icon_view->priv->source_set)
6469     return;
6470
6471   source_row = get_source_row (context);
6472
6473   if (source_row == NULL)
6474     return;
6475
6476   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6477                                          source_row);
6478
6479   gtk_tree_path_free (source_row);
6480
6481   set_source_row (context, NULL, NULL);
6482 }
6483
6484 /* Target side drag signals */
6485 static void
6486 gtk_icon_view_drag_leave (GtkWidget      *widget,
6487                           GdkDragContext *context,
6488                           guint           time)
6489 {
6490   GtkIconView *icon_view;
6491
6492   icon_view = GTK_ICON_VIEW (widget);
6493
6494   /* unset any highlight row */
6495   gtk_icon_view_set_drag_dest_item (icon_view,
6496                                     NULL,
6497                                     GTK_ICON_VIEW_DROP_LEFT);
6498
6499   remove_scroll_timeout (icon_view);
6500 }
6501
6502 static gboolean 
6503 gtk_icon_view_drag_motion (GtkWidget      *widget,
6504                            GdkDragContext *context,
6505                            gint            x,
6506                            gint            y,
6507                            guint           time)
6508 {
6509   GtkTreePath *path = NULL;
6510   GtkIconViewDropPosition pos;
6511   GtkIconView *icon_view;
6512   GdkDragAction suggested_action = 0;
6513   GdkAtom target;
6514   gboolean empty;
6515
6516   icon_view = GTK_ICON_VIEW (widget);
6517
6518   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6519     return FALSE;
6520
6521   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6522
6523   /* we only know this *after* set_desination_row */
6524   empty = icon_view->priv->empty_view_drop;
6525
6526   if (path == NULL && !empty)
6527     {
6528       /* Can't drop here. */
6529       gdk_drag_status (context, 0, time);
6530     }
6531   else
6532     {
6533       if (icon_view->priv->scroll_timeout_id == 0)
6534         {
6535           DragScrollData *data = g_slice_new (DragScrollData);
6536           data->icon_view = icon_view;
6537           data->device = gdk_drag_context_get_device (context);
6538
6539           icon_view->priv->scroll_timeout_id =
6540             gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 50, drag_scroll_timeout, data, (GDestroyNotify) drag_scroll_data_free);
6541         }
6542
6543       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6544         {
6545           /* Request data so we can use the source row when
6546            * determining whether to accept the drop
6547            */
6548           set_status_pending (context, suggested_action);
6549           gtk_drag_get_data (widget, context, target, time);
6550         }
6551       else
6552         {
6553           set_status_pending (context, 0);
6554           gdk_drag_status (context, suggested_action, time);
6555         }
6556     }
6557
6558   if (path)
6559     gtk_tree_path_free (path);
6560
6561   return TRUE;
6562 }
6563
6564 static gboolean 
6565 gtk_icon_view_drag_drop (GtkWidget      *widget,
6566                          GdkDragContext *context,
6567                          gint            x,
6568                          gint            y,
6569                          guint           time)
6570 {
6571   GtkIconView *icon_view;
6572   GtkTreePath *path;
6573   GdkDragAction suggested_action = 0;
6574   GdkAtom target = GDK_NONE;
6575   GtkTreeModel *model;
6576   gboolean drop_append_mode;
6577
6578   icon_view = GTK_ICON_VIEW (widget);
6579   model = gtk_icon_view_get_model (icon_view);
6580
6581   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6582
6583   if (!icon_view->priv->dest_set)
6584     return FALSE;
6585
6586   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6587     return FALSE;
6588
6589   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6590     return FALSE;
6591   
6592   path = get_logical_destination (icon_view, &drop_append_mode);
6593
6594   if (target != GDK_NONE && path != NULL)
6595     {
6596       /* in case a motion had requested drag data, change things so we
6597        * treat drag data receives as a drop.
6598        */
6599       set_status_pending (context, 0);
6600       set_dest_row (context, model, path, 
6601                     icon_view->priv->empty_view_drop, drop_append_mode);
6602     }
6603
6604   if (path)
6605     gtk_tree_path_free (path);
6606
6607   /* Unset this thing */
6608   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6609
6610   if (target != GDK_NONE)
6611     {
6612       gtk_drag_get_data (widget, context, target, time);
6613       return TRUE;
6614     }
6615   else
6616     return FALSE;
6617 }
6618
6619 static void
6620 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6621                                   GdkDragContext   *context,
6622                                   gint              x,
6623                                   gint              y,
6624                                   GtkSelectionData *selection_data,
6625                                   guint             info,
6626                                   guint             time)
6627 {
6628   GtkTreePath *path;
6629   gboolean accepted = FALSE;
6630   GtkTreeModel *model;
6631   GtkIconView *icon_view;
6632   GtkTreePath *dest_row;
6633   GdkDragAction suggested_action;
6634   gboolean drop_append_mode;
6635   
6636   icon_view = GTK_ICON_VIEW (widget);  
6637   model = gtk_icon_view_get_model (icon_view);
6638
6639   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6640     return;
6641
6642   if (!icon_view->priv->dest_set)
6643     return;
6644
6645   suggested_action = get_status_pending (context);
6646
6647   if (suggested_action)
6648     {
6649       /* We are getting this data due to a request in drag_motion,
6650        * rather than due to a request in drag_drop, so we are just
6651        * supposed to call drag_status, not actually paste in the
6652        * data.
6653        */
6654       path = get_logical_destination (icon_view, &drop_append_mode);
6655
6656       if (path == NULL)
6657         suggested_action = 0;
6658
6659       if (suggested_action)
6660         {
6661           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6662                                                      path,
6663                                                      selection_data))
6664             suggested_action = 0;
6665         }
6666
6667       gdk_drag_status (context, suggested_action, time);
6668
6669       if (path)
6670         gtk_tree_path_free (path);
6671
6672       /* If you can't drop, remove user drop indicator until the next motion */
6673       if (suggested_action == 0)
6674         gtk_icon_view_set_drag_dest_item (icon_view,
6675                                           NULL,
6676                                           GTK_ICON_VIEW_DROP_LEFT);
6677       return;
6678     }
6679   
6680
6681   dest_row = get_dest_row (context);
6682
6683   if (dest_row == NULL)
6684     return;
6685
6686   if (gtk_selection_data_get_length (selection_data) >= 0)
6687     {
6688       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6689                                                  dest_row,
6690                                                  selection_data))
6691         accepted = TRUE;
6692     }
6693
6694   gtk_drag_finish (context,
6695                    accepted,
6696                    (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
6697                    time);
6698
6699   gtk_tree_path_free (dest_row);
6700
6701   /* drop dest_row */
6702   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6703 }
6704
6705 /* Drag-and-Drop support */
6706 /**
6707  * gtk_icon_view_enable_model_drag_source:
6708  * @icon_view: a #GtkIconTreeView
6709  * @start_button_mask: Mask of allowed buttons to start drag
6710  * @targets: (array length=n_targets): the table of targets that the drag will
6711  *           support
6712  * @n_targets: the number of items in @targets
6713  * @actions: the bitmask of possible actions for a drag from this
6714  *    widget
6715  *
6716  * Turns @icon_view into a drag source for automatic DND. Calling this
6717  * method sets #GtkIconView:reorderable to %FALSE.
6718  *
6719  * Since: 2.8
6720  **/
6721 void
6722 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6723                                         GdkModifierType           start_button_mask,
6724                                         const GtkTargetEntry     *targets,
6725                                         gint                      n_targets,
6726                                         GdkDragAction             actions)
6727 {
6728   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6729
6730   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6731
6732   icon_view->priv->start_button_mask = start_button_mask;
6733   icon_view->priv->source_actions = actions;
6734
6735   icon_view->priv->source_set = TRUE;
6736
6737   unset_reorderable (icon_view);
6738 }
6739
6740 /**
6741  * gtk_icon_view_enable_model_drag_dest:
6742  * @icon_view: a #GtkIconView
6743  * @targets: (array length=n_targets): the table of targets that the drag will
6744  *           support
6745  * @n_targets: the number of items in @targets
6746  * @actions: the bitmask of possible actions for a drag to this
6747  *    widget
6748  *
6749  * Turns @icon_view into a drop destination for automatic DND. Calling this
6750  * method sets #GtkIconView:reorderable to %FALSE.
6751  *
6752  * Since: 2.8
6753  **/
6754 void 
6755 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6756                                       const GtkTargetEntry *targets,
6757                                       gint                  n_targets,
6758                                       GdkDragAction         actions)
6759 {
6760   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6761
6762   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6763
6764   icon_view->priv->dest_actions = actions;
6765
6766   icon_view->priv->dest_set = TRUE;
6767
6768   unset_reorderable (icon_view);  
6769 }
6770
6771 /**
6772  * gtk_icon_view_unset_model_drag_source:
6773  * @icon_view: a #GtkIconView
6774  * 
6775  * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
6776  * method sets #GtkIconView:reorderable to %FALSE.
6777  *
6778  * Since: 2.8
6779  **/
6780 void
6781 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6782 {
6783   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6784
6785   if (icon_view->priv->source_set)
6786     {
6787       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6788       icon_view->priv->source_set = FALSE;
6789     }
6790
6791   unset_reorderable (icon_view);
6792 }
6793
6794 /**
6795  * gtk_icon_view_unset_model_drag_dest:
6796  * @icon_view: a #GtkIconView
6797  * 
6798  * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
6799  * method sets #GtkIconView:reorderable to %FALSE.
6800  *
6801  * Since: 2.8
6802  **/
6803 void
6804 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6805 {
6806   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6807
6808   if (icon_view->priv->dest_set)
6809     {
6810       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6811       icon_view->priv->dest_set = FALSE;
6812     }
6813
6814   unset_reorderable (icon_view);
6815 }
6816
6817 /* These are useful to implement your own custom stuff. */
6818 /**
6819  * gtk_icon_view_set_drag_dest_item:
6820  * @icon_view: a #GtkIconView
6821  * @path: (allow-none): The path of the item to highlight, or %NULL.
6822  * @pos: Specifies where to drop, relative to the item
6823  *
6824  * Sets the item that is highlighted for feedback.
6825  *
6826  * Since: 2.8
6827  */
6828 void
6829 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6830                                   GtkTreePath              *path,
6831                                   GtkIconViewDropPosition   pos)
6832 {
6833   /* Note; this function is exported to allow a custom DND
6834    * implementation, so it can't touch TreeViewDragInfo
6835    */
6836
6837   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6838
6839   if (icon_view->priv->dest_item)
6840     {
6841       GtkTreePath *current_path;
6842       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6843       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6844       icon_view->priv->dest_item = NULL;      
6845
6846       gtk_icon_view_queue_draw_path (icon_view, current_path);
6847       gtk_tree_path_free (current_path);
6848     }
6849   
6850   /* special case a drop on an empty model */
6851   icon_view->priv->empty_view_drop = FALSE;
6852   if (pos == GTK_ICON_VIEW_DROP_ABOVE && path
6853       && gtk_tree_path_get_depth (path) == 1
6854       && gtk_tree_path_get_indices (path)[0] == 0)
6855     {
6856       gint n_children;
6857
6858       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6859                                                    NULL);
6860
6861       if (n_children == 0)
6862         icon_view->priv->empty_view_drop = TRUE;
6863     }
6864
6865   icon_view->priv->dest_pos = pos;
6866
6867   if (path)
6868     {
6869       icon_view->priv->dest_item =
6870         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
6871                                           icon_view->priv->model, path);
6872       
6873       gtk_icon_view_queue_draw_path (icon_view, path);
6874     }
6875 }
6876
6877 /**
6878  * gtk_icon_view_get_drag_dest_item:
6879  * @icon_view: a #GtkIconView
6880  * @path: (out) (allow-none): Return location for the path of
6881  *        the highlighted item, or %NULL.
6882  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6883  * 
6884  * Gets information about the item that is highlighted for feedback.
6885  *
6886  * Since: 2.8
6887  **/
6888 void
6889 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
6890                                   GtkTreePath             **path,
6891                                   GtkIconViewDropPosition  *pos)
6892 {
6893   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6894
6895   if (path)
6896     {
6897       if (icon_view->priv->dest_item)
6898         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6899       else
6900         *path = NULL;
6901     }
6902
6903   if (pos)
6904     *pos = icon_view->priv->dest_pos;
6905 }
6906
6907 /**
6908  * gtk_icon_view_get_dest_item_at_pos:
6909  * @icon_view: a #GtkIconView
6910  * @drag_x: the position to determine the destination item for
6911  * @drag_y: the position to determine the destination item for
6912  * @path: (out) (allow-none): Return location for the path of the item,
6913  *    or %NULL.
6914  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6915  * 
6916  * Determines the destination item for a given position.
6917  * 
6918  * Return value: whether there is an item at the given position.
6919  *
6920  * Since: 2.8
6921  **/
6922 gboolean
6923 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
6924                                     gint                      drag_x,
6925                                     gint                      drag_y,
6926                                     GtkTreePath             **path,
6927                                     GtkIconViewDropPosition  *pos)
6928 {
6929   GtkIconViewItem *item;
6930
6931   /* Note; this function is exported to allow a custom DND
6932    * implementation, so it can't touch TreeViewDragInfo
6933    */
6934
6935   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6936   g_return_val_if_fail (drag_x >= 0, FALSE);
6937   g_return_val_if_fail (drag_y >= 0, FALSE);
6938   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6939
6940
6941   if (path)
6942     *path = NULL;
6943
6944   item = _gtk_icon_view_get_item_at_coords (icon_view, 
6945                                            drag_x + gtk_adjustment_get_value (icon_view->priv->hadjustment), 
6946                                            drag_y + gtk_adjustment_get_value (icon_view->priv->vadjustment),
6947                                            FALSE, NULL);
6948
6949   if (item == NULL)
6950     return FALSE;
6951
6952   if (path)
6953     *path = gtk_tree_path_new_from_indices (item->index, -1);
6954
6955   if (pos)
6956     {
6957       if (drag_x < item->cell_area.x + item->cell_area.width / 4)
6958         *pos = GTK_ICON_VIEW_DROP_LEFT;
6959       else if (drag_x > item->cell_area.x + item->cell_area.width * 3 / 4)
6960         *pos = GTK_ICON_VIEW_DROP_RIGHT;
6961       else if (drag_y < item->cell_area.y + item->cell_area.height / 4)
6962         *pos = GTK_ICON_VIEW_DROP_ABOVE;
6963       else if (drag_y > item->cell_area.y + item->cell_area.height * 3 / 4)
6964         *pos = GTK_ICON_VIEW_DROP_BELOW;
6965       else
6966         *pos = GTK_ICON_VIEW_DROP_INTO;
6967     }
6968
6969   return TRUE;
6970 }
6971
6972 /**
6973  * gtk_icon_view_create_drag_icon:
6974  * @icon_view: a #GtkIconView
6975  * @path: a #GtkTreePath in @icon_view
6976  *
6977  * Creates a #cairo_surface_t representation of the item at @path.  
6978  * This image is used for a drag icon.
6979  *
6980  * Return value: (transfer full): a newly-allocated surface of the drag icon.
6981  * 
6982  * Since: 2.8
6983  **/
6984 cairo_surface_t *
6985 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6986                                 GtkTreePath *path)
6987 {
6988   GtkWidget *widget;
6989   GtkStyleContext *context;
6990   cairo_t *cr;
6991   cairo_surface_t *surface;
6992   GList *l;
6993   gint index;
6994
6995   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
6996   g_return_val_if_fail (path != NULL, NULL);
6997
6998   widget = GTK_WIDGET (icon_view);
6999   context = gtk_widget_get_style_context (widget);
7000
7001   if (!gtk_widget_get_realized (widget))
7002     return NULL;
7003
7004   index = gtk_tree_path_get_indices (path)[0];
7005
7006   for (l = icon_view->priv->items; l; l = l->next) 
7007     {
7008       GtkIconViewItem *item = l->data;
7009       
7010       if (index == item->index)
7011         {
7012           GdkRectangle rect = { 
7013             item->cell_area.x - icon_view->priv->item_padding, 
7014             item->cell_area.y - icon_view->priv->item_padding, 
7015             item->cell_area.width  + icon_view->priv->item_padding * 2, 
7016             item->cell_area.height + icon_view->priv->item_padding * 2 
7017           };
7018
7019           surface = gdk_window_create_similar_surface (icon_view->priv->bin_window,
7020                                                        CAIRO_CONTENT_COLOR,
7021                                                        rect.width + 2,
7022                                                        rect.height + 2);
7023
7024           cr = cairo_create (surface);
7025           cairo_set_line_width (cr, 1.);
7026
7027           gtk_render_background (context, cr, 0, 0,
7028                                  rect.width + 2, rect.height + 2);
7029
7030           cairo_save (cr);
7031
7032           cairo_rectangle (cr, 1, 1, rect.width, rect.height);
7033           cairo_clip (cr);
7034
7035           gtk_icon_view_paint_item (icon_view, cr, item, 
7036                                     icon_view->priv->item_padding + 1, 
7037                                     icon_view->priv->item_padding + 1, FALSE);
7038
7039           cairo_restore (cr);
7040
7041           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
7042           cairo_rectangle (cr, 0.5, 0.5, rect.width + 1, rect.height + 1);
7043           cairo_stroke (cr);
7044
7045           cairo_destroy (cr);
7046
7047           return surface;
7048         }
7049     }
7050   
7051   return NULL;
7052 }
7053
7054 /**
7055  * gtk_icon_view_get_reorderable:
7056  * @icon_view: a #GtkIconView
7057  *
7058  * Retrieves whether the user can reorder the list via drag-and-drop. 
7059  * See gtk_icon_view_set_reorderable().
7060  *
7061  * Return value: %TRUE if the list can be reordered.
7062  *
7063  * Since: 2.8
7064  **/
7065 gboolean
7066 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
7067 {
7068   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7069
7070   return icon_view->priv->reorderable;
7071 }
7072
7073 static const GtkTargetEntry item_targets[] = {
7074   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
7075 };
7076
7077
7078 /**
7079  * gtk_icon_view_set_reorderable:
7080  * @icon_view: A #GtkIconView.
7081  * @reorderable: %TRUE, if the list of items can be reordered.
7082  *
7083  * This function is a convenience function to allow you to reorder models that
7084  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
7085  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
7086  * the user can reorder the model by dragging and dropping rows.  The
7087  * developer can listen to these changes by connecting to the model's
7088  * row_inserted and row_deleted signals. The reordering is implemented by setting up
7089  * the icon view as a drag source and destination. Therefore, drag and
7090  * drop can not be used in a reorderable view for any other purpose.
7091  *
7092  * This function does not give you any degree of control over the order -- any
7093  * reordering is allowed.  If more control is needed, you should probably
7094  * handle drag and drop manually.
7095  *
7096  * Since: 2.8
7097  **/
7098 void
7099 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
7100                                gboolean     reorderable)
7101 {
7102   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7103
7104   reorderable = reorderable != FALSE;
7105
7106   if (icon_view->priv->reorderable == reorderable)
7107     return;
7108
7109   if (reorderable)
7110     {
7111       gtk_icon_view_enable_model_drag_source (icon_view,
7112                                               GDK_BUTTON1_MASK,
7113                                               item_targets,
7114                                               G_N_ELEMENTS (item_targets),
7115                                               GDK_ACTION_MOVE);
7116       gtk_icon_view_enable_model_drag_dest (icon_view,
7117                                             item_targets,
7118                                             G_N_ELEMENTS (item_targets),
7119                                             GDK_ACTION_MOVE);
7120     }
7121   else
7122     {
7123       gtk_icon_view_unset_model_drag_source (icon_view);
7124       gtk_icon_view_unset_model_drag_dest (icon_view);
7125     }
7126
7127   icon_view->priv->reorderable = reorderable;
7128
7129   g_object_notify (G_OBJECT (icon_view), "reorderable");
7130 }
7131
7132 static gboolean
7133 gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
7134                                           GtkBuilder    *builder,
7135                                           GObject       *child,
7136                                           const gchar   *tagname,
7137                                           GMarkupParser *parser,
7138                                           gpointer      *data)
7139 {
7140   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
7141                                                 tagname, parser, data))
7142     return TRUE;
7143
7144   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
7145                                                       tagname, parser, data);
7146 }
7147
7148 static void
7149 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
7150                                         GtkBuilder   *builder,
7151                                         GObject      *child,
7152                                         const gchar  *tagname,
7153                                         gpointer     *data)
7154 {
7155   if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder,
7156                                                   child, tagname, data))
7157     parent_buildable_iface->custom_tag_end (buildable, builder,
7158                                             child, tagname, data);
7159 }