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