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