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