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