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