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