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