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