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