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