]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
Documentation improvements. (#309946, Torsten Schoenfeld)
[~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[info->position].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[info->position].x, &item->box[info->position].y,
2677                                   &item->box[info->position].width, &item->box[info->position].height);
2678
2679       item->box[info->position].x += cell_area.x;
2680       item->box[info->position].y += cell_area.y;
2681       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2682         {
2683           item->before[info->position] = item->box[info->position].x - cell_area.x;
2684           item->after[info->position] = cell_area.width - item->box[info->position].width - item->before[info->position];
2685           cell_area.x += cell_area.width + spacing;
2686         }
2687       else
2688         {
2689           if (item->box[info->position].width > item->width)
2690             {
2691               item->width = item->box[info->position].width;
2692               cell_area.width = item->width;
2693             }
2694           item->before[info->position] = item->box[info->position].y - cell_area.y;
2695           item->after[info->position] = cell_area.height - item->box[info->position].height - item->before[info->position];
2696           cell_area.y += cell_area.height + spacing;
2697         }
2698     }
2699
2700   if (rtl && icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2701     {
2702       for (i = 0; i < icon_view->priv->n_cells; i++)
2703         {
2704           item->box[i].x = item->x + item->width - 
2705             (item->box[i].x + item->box[i].width - item->x);
2706         }      
2707     }   
2708 }
2709
2710 static void
2711 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
2712 {
2713   g_list_foreach (icon_view->priv->items,
2714                   (GFunc)gtk_icon_view_item_invalidate_size, NULL);
2715 }
2716
2717 static void
2718 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
2719 {
2720   item->width = -1;
2721   item->height = -1;
2722 }
2723
2724 static void
2725 gtk_icon_view_paint_item (GtkIconView     *icon_view,
2726                           cairo_t         *cr,
2727                           GtkIconViewItem *item,
2728                           GdkRectangle    *area,
2729                           GdkDrawable     *drawable,
2730                           gint             x,
2731                           gint             y,
2732                           gboolean         draw_focus)
2733 {
2734   gint focus_width, focus_pad;
2735   gint spacing, padding;
2736   GdkRectangle cell_area, box;
2737   GList *l;
2738   gint i;
2739   GtkStateType state;
2740   gboolean rtl;
2741   GtkCellRendererState flags;
2742       
2743   if (icon_view->priv->model == NULL)
2744     return;
2745   
2746   gtk_icon_view_set_cell_data (icon_view, item);
2747
2748   rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2749   
2750   gtk_widget_style_get (GTK_WIDGET (icon_view),
2751                         "focus-line-width", &focus_width,
2752                         "focus-padding", &focus_pad,
2753                         NULL);
2754   
2755   spacing = icon_view->priv->spacing;
2756   padding = focus_width; 
2757   
2758   if (item->selected)
2759     {
2760       flags = GTK_CELL_RENDERER_SELECTED;
2761       if (GTK_WIDGET_HAS_FOCUS (icon_view))
2762         state = GTK_STATE_SELECTED;
2763       else
2764         state = GTK_STATE_ACTIVE;
2765     }
2766   else
2767     {
2768       flags = 0;
2769       state = GTK_STATE_NORMAL;
2770     }
2771   
2772 #ifdef DEBUG_ICON_VIEW
2773   gdk_draw_rectangle (drawable,
2774                       GTK_WIDGET (icon_view)->style->black_gc,
2775                       FALSE,
2776                       x, y, 
2777                       item->width, item->height);
2778 #endif
2779
2780   if (item->selected)
2781     for (l = icon_view->priv->cell_list; l; l = l->next)
2782       {
2783         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2784         
2785         if (!info->cell->visible)
2786           continue;
2787         
2788         gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2789
2790         /* FIXME we hardwire background drawing behind text
2791          * cell renderers here 
2792          */
2793         if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
2794           {
2795             gdk_cairo_set_source_color (cr, &GTK_WIDGET (icon_view)->style->base[state]);
2796             cairo_rectangle (cr,
2797                              x - item->x + box.x, 
2798                              y - item->y + box.y,
2799                              box.width, box.height);
2800             cairo_fill (cr);
2801           }
2802       }
2803   
2804   for (l = icon_view->priv->cell_list; l; l = l->next)
2805     {
2806       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2807       
2808       if (!info->cell->visible)
2809         continue;
2810       
2811       gtk_icon_view_get_cell_area (icon_view, item, info, &cell_area);
2812       
2813 #ifdef DEBUG_ICON_VIEW
2814       gdk_draw_rectangle (drawable,
2815                           GTK_WIDGET (icon_view)->style->black_gc,
2816                           FALSE,
2817                           x - item->x + cell_area.x, 
2818                           y - item->y + cell_area.y, 
2819                           cell_area.width, cell_area.height);
2820
2821       gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2822           
2823       gdk_draw_rectangle (drawable,
2824                           GTK_WIDGET (icon_view)->style->black_gc,
2825                           FALSE,
2826                           x - item->x + box.x, 
2827                           y - item->y + box.y, 
2828                           box.width, box.height);
2829 #endif
2830
2831       cell_area.x = x - item->x + cell_area.x;
2832       cell_area.y = y - item->y + cell_area.y;
2833
2834       gtk_cell_renderer_render (info->cell,
2835                                 drawable,
2836                                 GTK_WIDGET (icon_view),
2837                                 &cell_area, &cell_area, area, flags);
2838       
2839     }
2840
2841   /* FIXME where to draw the focus with generic cell renderers ? */
2842   if (draw_focus && 
2843       GTK_WIDGET_HAS_FOCUS (icon_view) &&
2844       item == icon_view->priv->cursor_item)
2845     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
2846       {
2847         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2848         
2849         if (!info->cell->visible)
2850           continue;
2851         
2852         if (icon_view->priv->cursor_cell < 0 &&
2853             (info->cell->mode != GTK_CELL_RENDERER_MODE_INERT ||
2854              GTK_IS_CELL_RENDERER_TEXT (info->cell)))
2855           icon_view->priv->cursor_cell = i;
2856         
2857         if (i == icon_view->priv->cursor_cell)
2858           {
2859             gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2860
2861             gtk_paint_focus (GTK_WIDGET (icon_view)->style,
2862                              drawable,
2863                              GTK_STATE_NORMAL,
2864                              area,
2865                              GTK_WIDGET (icon_view),
2866                              "icon_view",
2867                              x - item->x + box.x - padding,
2868                              y - item->y + box.y - padding,
2869                              box.width + 2 * padding,
2870                              box.height + 2 * padding);
2871             break;
2872           }
2873       }
2874 }
2875
2876 static void
2877 gtk_icon_view_paint_rubberband (GtkIconView     *icon_view,
2878                                 cairo_t         *cr,
2879                                 GdkRectangle    *area)
2880 {
2881   GdkRectangle rect;
2882   GdkRectangle rubber_rect;
2883   GdkColor *fill_color_gdk;
2884   guchar fill_color_alpha;
2885
2886   rubber_rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
2887   rubber_rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
2888   rubber_rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
2889   rubber_rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
2890
2891   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
2892     return;
2893
2894   gtk_widget_style_get (GTK_WIDGET (icon_view),
2895                         "selection-box-color", &fill_color_gdk,
2896                         "selection-box-alpha", &fill_color_alpha,
2897                         NULL);
2898
2899   if (!fill_color_gdk)
2900     fill_color_gdk = gdk_color_copy (&GTK_WIDGET (icon_view)->style->base[GTK_STATE_SELECTED]);
2901
2902   cairo_set_source_rgba (cr,
2903                          fill_color_gdk->red / 65535.,
2904                          fill_color_gdk->green / 65535.,
2905                          fill_color_gdk->blue / 65535.,
2906                          fill_color_alpha / 255.);
2907
2908   cairo_save (cr);
2909   gdk_cairo_rectangle (cr, &rect);
2910   cairo_clip (cr);
2911   cairo_paint (cr);
2912
2913   /* Draw the border without alpha */
2914   cairo_set_source_rgb (cr,
2915                         fill_color_gdk->red / 65535.,
2916                         fill_color_gdk->green / 65535.,
2917                         fill_color_gdk->blue / 65535.);
2918   cairo_rectangle (cr, 
2919                    rubber_rect.x + 0.5, rubber_rect.y + 0.5,
2920                    rubber_rect.width - 1, rubber_rect.height - 1);
2921   cairo_stroke (cr);
2922   cairo_restore (cr);
2923
2924   gdk_color_free (fill_color_gdk);
2925 }
2926
2927 static void
2928 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
2929                                GtkTreePath *path)
2930 {
2931   GList *l;
2932   gint index;
2933
2934   index = gtk_tree_path_get_indices (path)[0];
2935
2936   for (l = icon_view->priv->items; l; l = l->next) 
2937     {
2938       GtkIconViewItem *item = l->data;
2939
2940       if (item->index == index)
2941         {
2942           gtk_icon_view_queue_draw_item (icon_view, item);
2943           break;
2944         }
2945     }
2946 }
2947
2948 static void
2949 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
2950                                GtkIconViewItem *item)
2951 {
2952   gint focus_width;
2953   GdkRectangle rect;
2954
2955   gtk_widget_style_get (GTK_WIDGET (icon_view),
2956                         "focus-line-width", &focus_width,
2957                         NULL);
2958
2959   rect.x = item->x - focus_width;
2960   rect.y = item->y - focus_width;
2961   rect.width = item->width + 2 * focus_width;
2962   rect.height = item->height + 2 * focus_width;
2963
2964   if (icon_view->priv->bin_window)
2965     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
2966 }
2967
2968 static gboolean
2969 layout_callback (gpointer user_data)
2970 {
2971   GtkIconView *icon_view;
2972
2973   GDK_THREADS_ENTER ();
2974
2975   icon_view = GTK_ICON_VIEW (user_data);
2976   
2977   icon_view->priv->layout_idle_id = 0;
2978
2979   gtk_icon_view_layout (icon_view);
2980   
2981   GDK_THREADS_LEAVE();
2982
2983   return FALSE;
2984 }
2985
2986 static void
2987 gtk_icon_view_queue_layout (GtkIconView *icon_view)
2988 {
2989   if (icon_view->priv->layout_idle_id != 0)
2990     return;
2991
2992   icon_view->priv->layout_idle_id = g_idle_add (layout_callback, icon_view);
2993 }
2994
2995 static void
2996 gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
2997                                GtkIconViewItem *item,
2998                                gint             cursor_cell)
2999 {
3000   AtkObject *obj;
3001   AtkObject *item_obj;
3002
3003   if (icon_view->priv->cursor_item == item &&
3004       (cursor_cell < 0 || cursor_cell == icon_view->priv->cursor_cell))
3005     return;
3006
3007   if (icon_view->priv->cursor_item != NULL)
3008     gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3009   
3010   icon_view->priv->cursor_item = item;
3011   if (cursor_cell >= 0)
3012     icon_view->priv->cursor_cell = cursor_cell;
3013
3014   gtk_icon_view_queue_draw_item (icon_view, item);
3015   
3016   /* Notify that accessible focus object has changed */
3017   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3018   item_obj = atk_object_ref_accessible_child (obj, item->index);
3019
3020   if (item_obj != NULL)
3021     {
3022       atk_focus_tracker_notify (item_obj);
3023       g_object_unref (item_obj); 
3024     }
3025 }
3026
3027
3028 static GtkIconViewItem *
3029 gtk_icon_view_item_new (void)
3030 {
3031   GtkIconViewItem *item;
3032
3033   item = g_new0 (GtkIconViewItem, 1);
3034
3035   item->width = -1;
3036   item->height = -1;
3037   
3038   return item;
3039 }
3040
3041 static void
3042 gtk_icon_view_item_free (GtkIconViewItem *item)
3043 {
3044   g_return_if_fail (item != NULL);
3045
3046   g_free (item->before);
3047   g_free (item->after);
3048   g_free (item->box);
3049
3050   g_free (item);
3051 }
3052
3053
3054 static GtkIconViewItem *
3055 gtk_icon_view_get_item_at_coords (GtkIconView          *icon_view,
3056                                   gint                  x,
3057                                   gint                  y,
3058                                   gboolean              only_in_cell,
3059                                   GtkIconViewCellInfo **cell_at_pos)
3060 {
3061   GList *items, *l;
3062   GdkRectangle box;
3063
3064   for (items = icon_view->priv->items; items; items = items->next)
3065     {
3066       GtkIconViewItem *item = items->data;
3067
3068       if (x >= item->x - icon_view->priv->row_spacing/2 && x <= item->x + item->width + icon_view->priv->row_spacing/2 &&
3069           y >= item->y - icon_view->priv->column_spacing/2 && y <= item->y + item->height + icon_view->priv->column_spacing/2)
3070         {
3071           if (only_in_cell || cell_at_pos)
3072             {
3073               gtk_icon_view_set_cell_data (icon_view, item);
3074
3075               for (l = icon_view->priv->cell_list; l; l = l->next)
3076                 {
3077                   GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3078                   
3079                   if (!info->cell->visible)
3080                     continue;
3081                   
3082                   gtk_icon_view_get_cell_box (icon_view, item, info, &box);
3083                   
3084                   if ((x >= box.x && x <= box.x + box.width &&
3085                        y >= box.y && y <= box.y + box.height) ||
3086                       (x >= box.x  &&
3087                        x <= box.x + box.width &&
3088                        y >= box.y &&
3089                        y <= box.y + box.height))
3090                     {
3091                       if (cell_at_pos)
3092                         *cell_at_pos = info;
3093                       
3094                       return item;
3095                     }
3096                 }
3097
3098               if (only_in_cell)
3099                 return NULL;
3100               
3101               if (cell_at_pos)
3102                 *cell_at_pos = NULL;
3103             }
3104
3105           return item;
3106         }
3107     }
3108
3109   return NULL;
3110 }
3111
3112 static void
3113 gtk_icon_view_select_item (GtkIconView      *icon_view,
3114                            GtkIconViewItem  *item)
3115 {
3116   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3117   g_return_if_fail (item != NULL);
3118
3119   if (item->selected)
3120     return;
3121   
3122   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3123     return;
3124   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3125     gtk_icon_view_unselect_all_internal (icon_view);
3126
3127   item->selected = TRUE;
3128
3129   gtk_icon_view_queue_draw_item (icon_view, item);
3130
3131   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3132 }
3133
3134
3135 static void
3136 gtk_icon_view_unselect_item (GtkIconView      *icon_view,
3137                              GtkIconViewItem  *item)
3138 {
3139   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3140   g_return_if_fail (item != NULL);
3141
3142   if (!item->selected)
3143     return;
3144   
3145   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3146       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3147     return;
3148   
3149   item->selected = FALSE;
3150
3151   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3152
3153   gtk_icon_view_queue_draw_item (icon_view, item);
3154 }
3155
3156 static void
3157 verify_items (GtkIconView *icon_view)
3158 {
3159   GList *items;
3160   int i = 0;
3161
3162   for (items = icon_view->priv->items; items; items = items->next)
3163     {
3164       GtkIconViewItem *item = items->data;
3165
3166       if (item->index != i)
3167         g_error ("List item does not match its index: "
3168                  "item index %d and list index %d\n", item->index, i);
3169
3170       i++;
3171     }
3172 }
3173
3174 static void
3175 gtk_icon_view_row_changed (GtkTreeModel *model,
3176                            GtkTreePath  *path,
3177                            GtkTreeIter  *iter,
3178                            gpointer      data)
3179 {
3180   GtkIconViewItem *item;
3181   gint index;
3182   GtkIconView *icon_view;
3183
3184   icon_view = GTK_ICON_VIEW (data);
3185
3186   gtk_icon_view_stop_editing (icon_view, TRUE);
3187   
3188   index = gtk_tree_path_get_indices(path)[0];
3189   item = g_list_nth_data (icon_view->priv->items, index);
3190
3191   gtk_icon_view_item_invalidate_size (item);
3192   gtk_icon_view_queue_layout (icon_view);
3193
3194   verify_items (icon_view);
3195 }
3196
3197 static void
3198 gtk_icon_view_row_inserted (GtkTreeModel *model,
3199                             GtkTreePath  *path,
3200                             GtkTreeIter  *iter,
3201                             gpointer      data)
3202 {
3203   gint length, index;
3204   GtkIconViewItem *item;
3205   gboolean iters_persist;
3206   GtkIconView *icon_view;
3207   GList *list;
3208   
3209   icon_view = GTK_ICON_VIEW (data);
3210
3211   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3212   
3213   length = gtk_tree_model_iter_n_children (model, NULL);
3214   index = gtk_tree_path_get_indices(path)[0];
3215
3216   item = gtk_icon_view_item_new ();
3217
3218   if (iters_persist)
3219     item->iter = *iter;
3220
3221   item->index = index;
3222
3223   /* FIXME: We can be more efficient here,
3224      we can store a tail pointer and use that when
3225      appending (which is a rather common operation)
3226   */
3227   icon_view->priv->items = g_list_insert (icon_view->priv->items,
3228                                          item, index);
3229   
3230   list = g_list_nth (icon_view->priv->items, index + 1);
3231   for (; list; list = list->next)
3232     {
3233       item = list->data;
3234
3235       item->index++;
3236     }
3237     
3238   verify_items (icon_view);
3239 }
3240
3241 static void
3242 gtk_icon_view_row_deleted (GtkTreeModel *model,
3243                            GtkTreePath  *path,
3244                            gpointer      data)
3245 {
3246   gint index;
3247   GtkIconView *icon_view;
3248   GtkIconViewItem *item;
3249   GList *list, *next;
3250   gboolean emit = FALSE;
3251   
3252   icon_view = GTK_ICON_VIEW (data);
3253
3254   index = gtk_tree_path_get_indices(path)[0];
3255
3256   list = g_list_nth (icon_view->priv->items, index);
3257   item = list->data;
3258
3259   gtk_icon_view_stop_editing (icon_view, TRUE);
3260
3261   if (item == icon_view->priv->anchor_item)
3262     icon_view->priv->anchor_item = NULL;
3263
3264   if (item == icon_view->priv->cursor_item)
3265     icon_view->priv->cursor_item = NULL;
3266
3267   if (item->selected)
3268     emit = TRUE;
3269   
3270   gtk_icon_view_item_free (item);
3271
3272   for (next = list->next; next; next = next->next)
3273     {
3274       item = next->data;
3275
3276       item->index--;
3277     }
3278   
3279   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3280
3281   gtk_icon_view_queue_layout (icon_view);
3282
3283   verify_items (icon_view);  
3284   
3285   if (emit)
3286     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3287 }
3288
3289 static void
3290 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3291                               GtkTreePath  *parent,
3292                               GtkTreeIter  *iter,
3293                               gint         *new_order,
3294                               gpointer      data)
3295 {
3296   int i;
3297   int length;
3298   GtkIconView *icon_view;
3299   GList *items = NULL, *list;
3300   GtkIconViewItem **item_array;
3301   gint *order;
3302   
3303   icon_view = GTK_ICON_VIEW (data);
3304
3305   gtk_icon_view_stop_editing (icon_view, TRUE);
3306
3307   length = gtk_tree_model_iter_n_children (model, NULL);
3308
3309   order = g_new (gint, length);
3310   for (i = 0; i < length; i++)
3311     order [new_order[i]] = i;
3312
3313   item_array = g_new (GtkIconViewItem *, length);
3314   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3315     item_array[order[i]] = list->data;
3316   g_free (order);
3317
3318   for (i = length - 1; i >= 0; i--)
3319     {
3320       item_array[i]->index = i;
3321       items = g_list_prepend (items, item_array[i]);
3322     }
3323   
3324   g_free (item_array);
3325   g_list_free (icon_view->priv->items);
3326   icon_view->priv->items = items;
3327
3328   gtk_icon_view_queue_layout (icon_view);
3329
3330   verify_items (icon_view);  
3331 }
3332
3333 static void
3334 gtk_icon_view_build_items (GtkIconView *icon_view)
3335 {
3336   GtkTreeIter iter;
3337   int i;
3338   gboolean iters_persist;
3339   GList *items = NULL;
3340
3341   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3342   
3343   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3344                                       &iter))
3345     return;
3346
3347   i = 0;
3348   
3349   do
3350     {
3351       GtkIconViewItem *item = gtk_icon_view_item_new ();
3352
3353       if (iters_persist)
3354         item->iter = iter;
3355
3356       item->index = i;
3357       
3358       i++;
3359
3360       items = g_list_prepend (items, item);
3361       
3362     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3363
3364   icon_view->priv->items = g_list_reverse (items);
3365 }
3366
3367 static void
3368 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
3369                                 guint           keyval,
3370                                 guint           modmask,
3371                                 GtkMovementStep step,
3372                                 gint            count)
3373 {
3374   
3375   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3376                                 "move_cursor", 2,
3377                                 G_TYPE_ENUM, step,
3378                                 G_TYPE_INT, count);
3379
3380   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3381                                 "move_cursor", 2,
3382                                 G_TYPE_ENUM, step,
3383                                 G_TYPE_INT, count);
3384
3385   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3386    return;
3387
3388   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3389                                 "move_cursor", 2,
3390                                 G_TYPE_ENUM, step,
3391                                 G_TYPE_INT, count);
3392
3393   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3394                                 "move_cursor", 2,
3395                                 G_TYPE_ENUM, step,
3396                                 G_TYPE_INT, count);
3397 }
3398
3399 static gboolean
3400 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
3401                                 GtkMovementStep  step,
3402                                 gint             count)
3403 {
3404   GdkModifierType state;
3405
3406   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3407   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3408                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3409                         step == GTK_MOVEMENT_DISPLAY_LINES ||
3410                         step == GTK_MOVEMENT_PAGES ||
3411                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3412
3413   if (!GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (icon_view)))
3414     return FALSE;
3415
3416   gtk_icon_view_stop_editing (icon_view, FALSE);
3417   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3418
3419   if (gtk_get_current_event_state (&state))
3420     {
3421       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3422         icon_view->priv->ctrl_pressed = TRUE;
3423       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
3424         icon_view->priv->shift_pressed = TRUE;
3425     }
3426   /* else we assume not pressed */
3427
3428   switch (step)
3429     {
3430     case GTK_MOVEMENT_LOGICAL_POSITIONS:
3431     case GTK_MOVEMENT_VISUAL_POSITIONS:
3432       gtk_icon_view_move_cursor_left_right (icon_view, count);
3433       break;
3434     case GTK_MOVEMENT_DISPLAY_LINES:
3435       gtk_icon_view_move_cursor_up_down (icon_view, count);
3436       break;
3437     case GTK_MOVEMENT_PAGES:
3438       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3439       break;
3440     case GTK_MOVEMENT_BUFFER_ENDS:
3441       gtk_icon_view_move_cursor_start_end (icon_view, count);
3442       break;
3443     default:
3444       g_assert_not_reached ();
3445     }
3446
3447   icon_view->priv->ctrl_pressed = FALSE;
3448   icon_view->priv->shift_pressed = FALSE;
3449
3450   return TRUE;
3451 }
3452
3453 static GtkIconViewItem *
3454 find_item (GtkIconView     *icon_view,
3455            GtkIconViewItem *current,
3456            gint             row_ofs,
3457            gint             col_ofs)
3458 {
3459   gint row, col;
3460   GList *items;
3461   GtkIconViewItem *item;
3462
3463   /* FIXME: this could be more efficient 
3464    */
3465   row = current->row + row_ofs;
3466   col = current->col + col_ofs;
3467
3468   for (items = icon_view->priv->items; items; items = items->next)
3469     {
3470       item = items->data;
3471       if (item->row == row && item->col == col)
3472         return item;
3473     }
3474   
3475   return NULL;
3476 }
3477
3478 static gint
3479 find_cell (GtkIconView     *icon_view,
3480            GtkIconViewItem *item,
3481            gint             cell,
3482            GtkOrientation   orientation,
3483            gint             step,
3484            gint            *count)
3485 {
3486   gint n_focusable;
3487   gint *focusable;
3488   gint first_text;
3489   gint current;
3490   gint i, k;
3491   GList *l;
3492
3493   if (icon_view->priv->orientation != orientation)
3494     return cell;
3495
3496   gtk_icon_view_set_cell_data (icon_view, item);
3497
3498   focusable = g_new0 (gint, icon_view->priv->n_cells);
3499   n_focusable = 0;
3500
3501   first_text = 0;
3502   current = 0;
3503   for (k = 0; k < 2; k++)
3504     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
3505       {
3506         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3507         
3508         if (info->pack == (k ? GTK_PACK_START : GTK_PACK_END))
3509           continue;
3510         
3511         if (!info->cell->visible)
3512           continue;
3513         
3514         if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
3515           first_text = i;
3516
3517         if (info->cell->mode != GTK_CELL_RENDERER_MODE_INERT)
3518           {
3519             if (cell == i)
3520               current = n_focusable;
3521
3522             focusable[n_focusable] = i;
3523
3524             n_focusable++;
3525           }
3526       }
3527   
3528   if (n_focusable == 0)
3529     focusable[n_focusable++] = first_text;
3530
3531   if (cell < 0)
3532     {
3533       current = step > 0 ? 0 : n_focusable - 1;
3534       cell = focusable[current];
3535     }
3536
3537   if (current + *count < 0)
3538     {
3539       cell = -1;
3540       *count = current + *count;
3541     }
3542   else if (current + *count > n_focusable - 1)
3543     {
3544       cell = -1;
3545       *count = current + *count - (n_focusable - 1);
3546     }
3547   else
3548     {
3549       cell = focusable[current + *count];
3550       *count = 0;
3551     }
3552   
3553   g_free (focusable);
3554   
3555   return cell;
3556 }
3557
3558 static GtkIconViewItem *
3559 find_item_page_up_down (GtkIconView     *icon_view,
3560                         GtkIconViewItem *current,
3561                         gint             count)
3562 {
3563   GList *item, *next;
3564   gint y, col;
3565   
3566   col = current->col;
3567   y = current->y + count * icon_view->priv->vadjustment->page_size;
3568
3569   item = g_list_find (icon_view->priv->items, current);
3570   if (count > 0)
3571     {
3572       while (item)
3573         {
3574           for (next = item->next; next; next = next->next)
3575             {
3576               if (((GtkIconViewItem *)next->data)->col == col)
3577                 break;
3578             }
3579           if (!next || ((GtkIconViewItem *)next->data)->y > y)
3580             break;
3581
3582           item = next;
3583         }
3584     }
3585   else 
3586     {
3587       while (item)
3588         {
3589           for (next = item->prev; next; next = next->prev)
3590             {
3591               if (((GtkIconViewItem *)next->data)->col == col)
3592                 break;
3593             }
3594           if (!next || ((GtkIconViewItem *)next->data)->y < y)
3595             break;
3596
3597           item = next;
3598         }
3599     }
3600
3601   if (item)
3602     return item->data;
3603
3604   return NULL;
3605 }
3606
3607 static gboolean
3608 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
3609                                   GtkIconViewItem *anchor,
3610                                   GtkIconViewItem *cursor)
3611 {
3612   GList *items;
3613   GtkIconViewItem *item;
3614   gint row1, row2, col1, col2;
3615   gboolean dirty = FALSE;
3616   
3617   if (anchor->row < cursor->row)
3618     {
3619       row1 = anchor->row;
3620       row2 = cursor->row;
3621     }
3622   else
3623     {
3624       row1 = cursor->row;
3625       row2 = anchor->row;
3626     }
3627
3628   if (anchor->col < cursor->col)
3629     {
3630       col1 = anchor->col;
3631       col2 = cursor->col;
3632     }
3633   else
3634     {
3635       col1 = cursor->col;
3636       col2 = anchor->col;
3637     }
3638
3639   for (items = icon_view->priv->items; items; items = items->next)
3640     {
3641       item = items->data;
3642
3643       if (row1 <= item->row && item->row <= row2 &&
3644           col1 <= item->col && item->col <= col2)
3645         {
3646           if (!item->selected)
3647             dirty = TRUE;
3648
3649           item->selected = TRUE;
3650           
3651           gtk_icon_view_queue_draw_item (icon_view, item);
3652         }
3653     }
3654
3655   return dirty;
3656 }
3657
3658 static void 
3659 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3660                                    gint         count)
3661 {
3662   GtkIconViewItem *item;
3663   gint cell;
3664   gboolean dirty = FALSE;
3665   gint step;
3666   
3667   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3668     return;
3669   
3670   if (!icon_view->priv->cursor_item)
3671     {
3672       GList *list;
3673
3674       if (count > 0)
3675         list = icon_view->priv->items;
3676       else
3677         list = g_list_last (icon_view->priv->items);
3678
3679       item = list ? list->data : NULL;
3680       cell = -1;
3681     }
3682   else
3683     {
3684       item = icon_view->priv->cursor_item;
3685       cell = icon_view->priv->cursor_cell;
3686       step = count > 0 ? 1 : -1;      
3687       while (item)
3688         {
3689           cell = find_cell (icon_view, item, cell,
3690                             GTK_ORIENTATION_VERTICAL, 
3691                             step, &count);
3692           if (count == 0)
3693             break;
3694
3695           item = find_item (icon_view, item, step, 0);
3696           count = count - step;
3697         }
3698     }
3699
3700   if (!item)
3701     return;
3702
3703   if (icon_view->priv->ctrl_pressed ||
3704       !icon_view->priv->shift_pressed ||
3705       !icon_view->priv->anchor_item ||
3706       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3707     icon_view->priv->anchor_item = item;
3708
3709   gtk_icon_view_set_cursor_item (icon_view, item, cell);
3710
3711   if (!icon_view->priv->ctrl_pressed &&
3712       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3713     {
3714       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3715       dirty = gtk_icon_view_select_all_between (icon_view, 
3716                                                 icon_view->priv->anchor_item,
3717                                                 item) || dirty;
3718     }
3719
3720   gtk_icon_view_scroll_to_item (icon_view, item);
3721
3722   if (dirty)
3723     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3724 }
3725
3726 static void 
3727 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3728                                         gint         count)
3729 {
3730   GtkIconViewItem *item;
3731   gboolean dirty = FALSE;
3732   
3733   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3734     return;
3735   
3736   if (!icon_view->priv->cursor_item)
3737     {
3738       GList *list;
3739
3740       if (count > 0)
3741         list = icon_view->priv->items;
3742       else
3743         list = g_list_last (icon_view->priv->items);
3744
3745       item = list ? list->data : NULL;
3746     }
3747   else
3748     item = find_item_page_up_down (icon_view, 
3749                                    icon_view->priv->cursor_item,
3750                                    count);
3751
3752   if (!item)
3753     return;
3754
3755   if (icon_view->priv->ctrl_pressed ||
3756       !icon_view->priv->shift_pressed ||
3757       !icon_view->priv->anchor_item ||
3758       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3759     icon_view->priv->anchor_item = item;
3760
3761   gtk_icon_view_set_cursor_item (icon_view, item, -1);
3762
3763   if (!icon_view->priv->ctrl_pressed &&
3764       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3765     {
3766       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3767       dirty = gtk_icon_view_select_all_between (icon_view, 
3768                                                 icon_view->priv->anchor_item,
3769                                                 item) || dirty;
3770     }
3771
3772   gtk_icon_view_scroll_to_item (icon_view, item);
3773
3774   if (dirty)
3775     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);  
3776 }
3777
3778 static void 
3779 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
3780                                       gint         count)
3781 {
3782   GtkIconViewItem *item;
3783   gint cell = -1;
3784   gboolean dirty = FALSE;
3785   gint step;
3786   
3787   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3788     return;
3789   
3790   if (!icon_view->priv->cursor_item)
3791     {
3792       GList *list;
3793
3794       if (count > 0)
3795         list = icon_view->priv->items;
3796       else
3797         list = g_list_last (icon_view->priv->items);
3798
3799       item = list ? list->data : NULL;
3800     }
3801   else
3802     {
3803       item = icon_view->priv->cursor_item;
3804       cell = icon_view->priv->cursor_cell;
3805       step = count > 0 ? 1 : -1;
3806       while (item)
3807         {
3808           cell = find_cell (icon_view, item, cell,
3809                             GTK_ORIENTATION_HORIZONTAL, 
3810                             step, &count);
3811           if (count == 0)
3812             break;
3813           
3814           item = find_item (icon_view, item, 0, step);
3815           count = count - step;
3816         }
3817     }
3818
3819   if (!item)
3820     return;
3821
3822   if (icon_view->priv->ctrl_pressed ||
3823       !icon_view->priv->shift_pressed ||
3824       !icon_view->priv->anchor_item ||
3825       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3826     icon_view->priv->anchor_item = item;
3827
3828   gtk_icon_view_set_cursor_item (icon_view, item, cell);
3829
3830   if (!icon_view->priv->ctrl_pressed &&
3831       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3832     {
3833       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3834       dirty = gtk_icon_view_select_all_between (icon_view, 
3835                                                 icon_view->priv->anchor_item,
3836                                                 item) || dirty;
3837     }
3838
3839   gtk_icon_view_scroll_to_item (icon_view, item);
3840
3841   if (dirty)
3842     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3843 }
3844
3845 static void 
3846 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
3847                                      gint         count)
3848 {
3849   GtkIconViewItem *item;
3850   GList *list;
3851   gboolean dirty = FALSE;
3852   
3853   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3854     return;
3855   
3856   if (count < 0)
3857     list = icon_view->priv->items;
3858   else
3859     list = g_list_last (icon_view->priv->items);
3860   
3861   item = list ? list->data : NULL;
3862
3863   if (!item)
3864     return;
3865
3866   if (icon_view->priv->ctrl_pressed ||
3867       !icon_view->priv->shift_pressed ||
3868       !icon_view->priv->anchor_item ||
3869       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3870     icon_view->priv->anchor_item = item;
3871
3872   gtk_icon_view_set_cursor_item (icon_view, item, -1);
3873
3874   if (!icon_view->priv->ctrl_pressed &&
3875       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3876     {
3877       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3878       dirty = gtk_icon_view_select_all_between (icon_view, 
3879                                                 icon_view->priv->anchor_item,
3880                                                 item) || dirty;
3881     }
3882
3883   gtk_icon_view_scroll_to_item (icon_view, item);
3884
3885   if (dirty)
3886     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3887 }
3888
3889 /**
3890  * gtk_icon_view_scroll_to_path:
3891  * @icon_view: A #GtkIconView.
3892  * @path: The path of the item to move to.
3893  * @use_align: whether to use alignment arguments, or %FALSE.
3894  * @row_align: The vertical alignment of the item specified by @path.
3895  * @col_align: The horizontal alignment of the item specified by @column.
3896  *
3897  * Moves the alignments of @icon_view to the position specified by @path.  
3898  * @row_align determines where the row is placed, and @col_align determines where 
3899  * @column is placed.  Both are expected to be between 0.0 and 1.0. 
3900  * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means center.
3901  *
3902  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
3903  * tree does the minimum amount of work to scroll the item onto the screen.
3904  * This means that the item will be scrolled to the edge closest to its current
3905  * position.  If the item is currently visible on the screen, nothing is done.
3906  *
3907  * This function only works if the model is set, and @path is a valid row on the
3908  * model.  If the model changes before the @tree_view is realized, the centered
3909  * path will be modified to reflect this change.
3910  *
3911  * Since: 2.8
3912  **/
3913 void
3914 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
3915                               GtkTreePath *path,
3916                               gboolean     use_align,
3917                               gfloat       row_align,
3918                               gfloat       col_align)
3919 {
3920   GtkIconViewItem *item;
3921
3922   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3923   g_return_if_fail (path != NULL);
3924   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
3925   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
3926   
3927   item = g_list_nth (icon_view->priv->items,
3928                      gtk_tree_path_get_indices(path)[0])->data;
3929   
3930   if (!item)
3931     return;
3932   
3933   if (use_align)
3934     {
3935       gint x, y, width, height;
3936       gint focus_width;
3937       gfloat offset, value;
3938
3939       gtk_widget_style_get (GTK_WIDGET (icon_view),
3940                             "focus-line-width", &focus_width,
3941                             NULL);
3942       
3943       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
3944       
3945       offset =  y + item->y - focus_width - 
3946         row_align * (GTK_WIDGET (icon_view)->allocation.height - item->height);
3947       value = CLAMP (icon_view->priv->vadjustment->value + offset, 
3948                      icon_view->priv->vadjustment->lower,
3949                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
3950       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
3951
3952       offset = x + item->x - focus_width - 
3953         col_align * (GTK_WIDGET (icon_view)->allocation.width - item->width);
3954       value = CLAMP (icon_view->priv->hadjustment->value + offset, 
3955                      icon_view->priv->hadjustment->lower,
3956                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
3957       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
3958
3959       gtk_adjustment_changed (icon_view->priv->hadjustment);
3960       gtk_adjustment_changed (icon_view->priv->vadjustment);
3961     }
3962   else
3963     gtk_icon_view_scroll_to_item (icon_view, item);    
3964 }
3965
3966
3967 static void     
3968 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view, 
3969                               GtkIconViewItem *item)
3970 {
3971   gint x, y, width, height;
3972   gint focus_width;
3973
3974   gtk_widget_style_get (GTK_WIDGET (icon_view),
3975                         "focus-line-width", &focus_width,
3976                         NULL);
3977
3978   gdk_drawable_get_size (GDK_DRAWABLE (icon_view->priv->bin_window), 
3979                          &width, &height);
3980   gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
3981
3982   if (y + item->y - focus_width < 0)
3983     gtk_adjustment_set_value (icon_view->priv->vadjustment, 
3984                               icon_view->priv->vadjustment->value + y + item->y - focus_width);
3985   else if (y + item->y + item->height + focus_width > GTK_WIDGET (icon_view)->allocation.height)
3986     gtk_adjustment_set_value (icon_view->priv->vadjustment, 
3987                               icon_view->priv->vadjustment->value + y + item->y + item->height 
3988                               + focus_width - GTK_WIDGET (icon_view)->allocation.height);
3989
3990   if (x + item->x - focus_width < 0)
3991     gtk_adjustment_set_value (icon_view->priv->hadjustment, 
3992                               icon_view->priv->hadjustment->value + x + item->x - focus_width);
3993   else if (x + item->x + item->width + focus_width > GTK_WIDGET (icon_view)->allocation.width)
3994     gtk_adjustment_set_value (icon_view->priv->hadjustment, 
3995                               icon_view->priv->hadjustment->value + x + item->x + item->width 
3996                               + focus_width - GTK_WIDGET (icon_view)->allocation.width);
3997
3998   gtk_adjustment_changed (icon_view->priv->hadjustment);
3999   gtk_adjustment_changed (icon_view->priv->vadjustment);
4000 }
4001
4002 /* GtkCellLayout implementation */
4003 static GtkIconViewCellInfo *
4004 gtk_icon_view_get_cell_info (GtkIconView     *icon_view,
4005                              GtkCellRenderer *renderer)
4006 {
4007   GList *i;
4008
4009   for (i = icon_view->priv->cell_list; i; i = i->next)
4010     {
4011       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)i->data;
4012
4013       if (info->cell == renderer)
4014         return info;
4015     }
4016
4017   return NULL;
4018 }
4019
4020 static void
4021 gtk_icon_view_set_cell_data (GtkIconView     *icon_view, 
4022                              GtkIconViewItem *item)
4023 {
4024   GList *i;
4025   gboolean iters_persist;
4026   GtkTreeIter iter;
4027   
4028   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
4029   
4030   if (!iters_persist)
4031     {
4032       GtkTreePath *path;
4033
4034       path = gtk_tree_path_new_from_indices (item->index, -1);
4035       gtk_tree_model_get_iter (icon_view->priv->model, &iter, path);
4036       gtk_tree_path_free (path);
4037     }
4038   else
4039     iter = item->iter;
4040   
4041   for (i = icon_view->priv->cell_list; i; i = i->next)
4042     {
4043       GSList *j;
4044       GtkIconViewCellInfo *info = i->data;
4045
4046       g_object_freeze_notify (G_OBJECT (info->cell));
4047
4048       for (j = info->attributes; j && j->next; j = j->next->next)
4049         {
4050           gchar *property = j->data;
4051           gint column = GPOINTER_TO_INT (j->next->data);
4052           GValue value = {0, };
4053
4054           gtk_tree_model_get_value (icon_view->priv->model, &iter,
4055                                     column, &value);
4056           g_object_set_property (G_OBJECT (info->cell),
4057                                  property, &value);
4058           g_value_unset (&value);
4059         }
4060
4061       if (info->func)
4062         (* info->func) (GTK_CELL_LAYOUT (icon_view),
4063                         info->cell,
4064                         icon_view->priv->model,
4065                         &iter,
4066                         info->func_data);
4067       
4068       g_object_thaw_notify (G_OBJECT (info->cell));
4069     }  
4070   
4071   adjust_wrap_width (icon_view, item);
4072 }
4073
4074 static void 
4075 free_cell_attributes (GtkIconViewCellInfo *info)
4076
4077   GSList *list;
4078
4079   list = info->attributes;
4080   while (list && list->next)
4081     {
4082       g_free (list->data);
4083       list = list->next->next;
4084     }
4085   
4086   g_slist_free (info->attributes);
4087   info->attributes = NULL;
4088 }
4089
4090 static void
4091 free_cell_info (GtkIconViewCellInfo *info)
4092 {
4093   free_cell_attributes (info);
4094
4095   g_object_unref (info->cell);
4096   
4097   if (info->destroy)
4098     (* info->destroy) (info->func_data);
4099
4100   g_free (info);
4101 }
4102
4103 static void
4104 gtk_icon_view_cell_layout_pack_start (GtkCellLayout   *layout,
4105                                       GtkCellRenderer *renderer,
4106                                       gboolean         expand)
4107 {
4108   GtkIconViewCellInfo *info;
4109   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4110
4111   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
4112   g_return_if_fail (!gtk_icon_view_get_cell_info (icon_view, renderer));
4113
4114   g_object_ref (renderer);
4115   gtk_object_sink (GTK_OBJECT (renderer));
4116
4117   info = g_new0 (GtkIconViewCellInfo, 1);
4118   info->cell = renderer;
4119   info->expand = expand ? TRUE : FALSE;
4120   info->pack = GTK_PACK_START;
4121   info->position = icon_view->priv->n_cells;
4122   
4123   icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
4124   icon_view->priv->n_cells++;
4125 }
4126
4127 static void
4128 gtk_icon_view_cell_layout_pack_end (GtkCellLayout   *layout,
4129                                     GtkCellRenderer *renderer,
4130                                     gboolean         expand)
4131 {
4132   GtkIconViewCellInfo *info;
4133   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4134
4135   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
4136   g_return_if_fail (!gtk_icon_view_get_cell_info (icon_view, renderer));
4137
4138   g_object_ref (renderer);
4139   gtk_object_sink (GTK_OBJECT (renderer));
4140
4141   info = g_new0 (GtkIconViewCellInfo, 1);
4142   info->cell = renderer;
4143   info->expand = expand ? TRUE : FALSE;
4144   info->pack = GTK_PACK_END;
4145   info->position = icon_view->priv->n_cells;
4146
4147   icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
4148   icon_view->priv->n_cells++;
4149 }
4150
4151 static void
4152 gtk_icon_view_cell_layout_add_attribute (GtkCellLayout   *layout,
4153                                          GtkCellRenderer *renderer,
4154                                          const gchar     *attribute,
4155                                          gint             column)
4156 {
4157   GtkIconViewCellInfo *info;
4158   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4159
4160   info = gtk_icon_view_get_cell_info (icon_view, renderer);
4161   g_return_if_fail (info != NULL);
4162
4163   info->attributes = g_slist_prepend (info->attributes,
4164                                       GINT_TO_POINTER (column));
4165   info->attributes = g_slist_prepend (info->attributes,
4166                                       g_strdup (attribute));
4167 }
4168
4169 static void
4170 gtk_icon_view_cell_layout_clear (GtkCellLayout *layout)
4171 {
4172   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4173
4174   while (icon_view->priv->cell_list)
4175     {
4176       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)icon_view->priv->cell_list->data;
4177       free_cell_info (info);
4178       icon_view->priv->cell_list = g_list_delete_link (icon_view->priv->cell_list, 
4179                                                        icon_view->priv->cell_list);
4180     }
4181
4182   icon_view->priv->n_cells = 0;
4183 }
4184
4185 static void
4186 gtk_icon_view_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4187                                               GtkCellRenderer       *cell,
4188                                               GtkCellLayoutDataFunc  func,
4189                                               gpointer               func_data,
4190                                               GDestroyNotify         destroy)
4191 {
4192   GtkIconViewCellInfo *info;
4193   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4194
4195   info = gtk_icon_view_get_cell_info (icon_view, cell);
4196   g_return_if_fail (info != NULL);
4197
4198   if (info->destroy)
4199     {
4200       GDestroyNotify d = info->destroy;
4201
4202       info->destroy = NULL;
4203       d (info->func_data);
4204     }
4205
4206   info->func = func;
4207   info->func_data = func_data;
4208   info->destroy = destroy;
4209 }
4210
4211 static void
4212 gtk_icon_view_cell_layout_clear_attributes (GtkCellLayout   *layout,
4213                                             GtkCellRenderer *renderer)
4214 {
4215   GtkIconViewCellInfo *info;
4216
4217   info = gtk_icon_view_get_cell_info (GTK_ICON_VIEW (layout), renderer);
4218   if (info != NULL)
4219     free_cell_attributes (info);
4220 }
4221
4222 static void
4223 gtk_icon_view_cell_layout_reorder (GtkCellLayout   *layout,
4224                                    GtkCellRenderer *cell,
4225                                    gint             position)
4226 {
4227   GtkIconView *icon_view;
4228   GList *link, *l;
4229   GtkIconViewCellInfo *info;
4230   gint i;
4231
4232   icon_view = GTK_ICON_VIEW (layout);
4233
4234   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4235
4236   info = gtk_icon_view_get_cell_info (icon_view, cell);
4237
4238   g_return_if_fail (info != NULL);
4239   g_return_if_fail (position >= 0);
4240
4241   link = g_list_find (icon_view->priv->cell_list, info);
4242
4243   g_return_if_fail (link != NULL);
4244
4245   icon_view->priv->cell_list = g_list_remove_link (icon_view->priv->cell_list,
4246                                                   link);
4247   icon_view->priv->cell_list = g_list_insert (icon_view->priv->cell_list,
4248                                              info, position);
4249
4250   for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4251     {
4252       info = l->data;
4253
4254       info->position = i;
4255     }
4256
4257   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
4258 }
4259
4260 /* Public API */
4261
4262
4263 /**
4264  * gtk_icon_view_new:
4265  * 
4266  * Creates a new #GtkIconView widget
4267  * 
4268  * Return value: A newly created #GtkIconView widget
4269  *
4270  * Since: 2.6
4271  **/
4272 GtkWidget *
4273 gtk_icon_view_new (void)
4274 {
4275   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4276 }
4277
4278 /**
4279  * gtk_icon_view_new_with_model:
4280  * @model: The model.
4281  * 
4282  * Creates a new #GtkIconView widget with the model @model.
4283  * 
4284  * Return value: A newly created #GtkIconView widget.
4285  *
4286  * Since: 2.6 
4287  **/
4288 GtkWidget *
4289 gtk_icon_view_new_with_model (GtkTreeModel *model)
4290 {
4291   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4292 }
4293
4294
4295 /**
4296  * gtk_icon_view_get_path_at_pos:
4297  * @icon_view: A #GtkIconView.
4298  * @x: The x position to be identified
4299  * @y: The y position to be identified
4300  * 
4301  * Finds the path at the point (@x, @y), relative to widget coordinates.
4302  * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4303  * the cell at the specified position.
4304  * 
4305  * Return value: The #GtkTreePath corresponding to the icon or %NULL
4306  * if no icon exists at that position.
4307  *
4308  * Since: 2.6 
4309  **/
4310 GtkTreePath *
4311 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4312                                gint         x,
4313                                gint         y)
4314 {
4315   GtkIconViewItem *item;
4316   GtkTreePath *path;
4317   
4318   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4319
4320   item = gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4321
4322   if (!item)
4323     return NULL;
4324
4325   path = gtk_tree_path_new_from_indices (item->index, -1);
4326
4327   return path;
4328 }
4329
4330 /**
4331  * gtk_icon_view_get_item_at_pos:
4332  * @icon_view: A #GtkIconView.
4333  * @x: The x position to be identified
4334  * @y: The y position to be identified
4335  * @path: Return location for the path, or %NULL
4336  * @cell: Return location for the renderer responsible for the cell
4337  *   at (@x, @y), or %NULL
4338  * 
4339  * Finds the path at the point (@x, @y), relative to widget coordinates.
4340  * In contrast to gtk_icon_view_get_path_at_pos(), this function also 
4341  * obtains the cell at the specified position. The returned path should
4342  * be freed with gtk_tree_path_free().
4343  * 
4344  * Return value: %TRUE if an item exists at the specified position
4345  *
4346  * Since: 2.8
4347  **/
4348 gboolean 
4349 gtk_icon_view_get_item_at_pos (GtkIconView      *icon_view,
4350                                gint              x,
4351                                gint              y,
4352                                GtkTreePath     **path,
4353                                GtkCellRenderer **cell)
4354 {
4355   GtkIconViewItem *item;
4356   GtkIconViewCellInfo *info;
4357   
4358   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4359
4360   item = gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &info);
4361
4362   if (path != NULL)
4363     {
4364       if (item != NULL)
4365         *path = gtk_tree_path_new_from_indices (item->index, -1);
4366       else
4367         *path = NULL;
4368     }
4369
4370   if (cell != NULL)
4371     {
4372       if (info != NULL)
4373         *cell = info->cell;
4374       else 
4375         *cell = NULL;
4376     }
4377
4378   return (item != NULL);
4379 }
4380
4381
4382 /**
4383  * gtk_icon_view_get_visible_range:
4384  * @icon_view: A #GtkIconView
4385  * @start_path: Return location for start of region, or %NULL
4386  * @end_path: Return location for end of region, or %NULL
4387  * 
4388  * Sets @start_path and @end_path to be the first and last visible path. 
4389  * Note that there may be invisible paths in between.
4390  * 
4391  * Both paths should be freed with gtk_tree_path_free() after use.
4392  * 
4393  * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4394  *
4395  * Since: 2.8
4396  **/
4397 gboolean
4398 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
4399                                  GtkTreePath **start_path,
4400                                  GtkTreePath **end_path)
4401 {
4402   gint start_index = -1;
4403   gint end_index = -1;
4404   GtkIconViewItem *item;
4405   GList *icons;
4406
4407   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4408
4409   if (icon_view->priv->hadjustment == NULL ||
4410       icon_view->priv->vadjustment == NULL)
4411     return FALSE;
4412
4413   if (start_path == NULL && end_path == NULL)
4414     return FALSE;
4415   
4416   for (icons = icon_view->priv->items; icons; icons = icons->next) 
4417     {
4418       GtkIconViewItem *item = icons->data;
4419
4420       if ((item->x + item->width >= (int)icon_view->priv->hadjustment->value) &&
4421           (item->y + item->height >= (int)icon_view->priv->vadjustment->value) &&
4422           (item->x <= (int) (icon_view->priv->hadjustment->value + icon_view->priv->hadjustment->page_size)) &&
4423           (item->y <= (int) (icon_view->priv->vadjustment->value + icon_view->priv->vadjustment->page_size)))
4424         {
4425           if (start_index == -1)
4426             start_index = item->index;
4427           end_index = item->index;
4428         }
4429     }
4430
4431   if (start_path && start_index != -1)
4432     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4433   if (end_path && end_index != -1)
4434     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4435   
4436   return start_index != -1;
4437 }
4438
4439 /**
4440  * gtk_icon_view_selected_foreach:
4441  * @icon_view: A #GtkIconView.
4442  * @func: The funcion to call for each selected icon.
4443  * @data: User data to pass to the function.
4444  * 
4445  * Calls a function for each selected icon. Note that the model or
4446  * selection cannot be modified from within this function.
4447  *
4448  * Since: 2.6 
4449  **/
4450 void
4451 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
4452                                 GtkIconViewForeachFunc func,
4453                                 gpointer               data)
4454 {
4455   GList *list;
4456   
4457   for (list = icon_view->priv->items; list; list = list->next)
4458     {
4459       GtkIconViewItem *item = list->data;
4460       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4461
4462       if (item->selected)
4463         (* func) (icon_view, path, data);
4464
4465       gtk_tree_path_free (path);
4466     }
4467 }
4468
4469 /**
4470  * gtk_icon_view_set_selection_mode:
4471  * @icon_view: A #GtkIconView.
4472  * @mode: The selection mode
4473  * 
4474  * Sets the selection mode of the @icon_view.
4475  *
4476  * Since: 2.6 
4477  **/
4478 void
4479 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
4480                                   GtkSelectionMode  mode)
4481 {
4482   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4483
4484   if (mode == icon_view->priv->selection_mode)
4485     return;
4486   
4487   if (mode == GTK_SELECTION_NONE ||
4488       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4489     gtk_icon_view_unselect_all (icon_view);
4490   
4491   icon_view->priv->selection_mode = mode;
4492
4493   g_object_notify (G_OBJECT (icon_view), "selection-mode");
4494 }
4495
4496 /**
4497  * gtk_icon_view_get_selection_mode:
4498  * @icon_view: A #GtkIconView.
4499  * 
4500  * Gets the selection mode of the @icon_view.
4501  *
4502  * Return value: the current selection mode
4503  *
4504  * Since: 2.6 
4505  **/
4506 GtkSelectionMode
4507 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4508 {
4509   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4510
4511   return icon_view->priv->selection_mode;
4512 }
4513
4514 /**
4515  * gtk_icon_view_set_model:
4516  * @icon_view: A #GtkIconView.
4517  * @model: The model.
4518  *
4519  * Sets the model for a #GtkIconView.  
4520  * If the @icon_view already has a model set, it will remove 
4521  * it before setting the new model.  If @model is %NULL, then
4522  * it will unset the old model.
4523  *
4524  * Since: 2.6 
4525  **/
4526 void
4527 gtk_icon_view_set_model (GtkIconView *icon_view,
4528                          GtkTreeModel *model)
4529 {
4530   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4531   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4532   
4533   if (icon_view->priv->model == model)
4534     return;
4535
4536   gtk_icon_view_stop_editing (icon_view, TRUE);
4537
4538   if (model)
4539     {
4540       GType column_type;
4541       
4542       g_return_if_fail (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY);
4543
4544       if (icon_view->priv->pixbuf_column != -1)
4545         {
4546           column_type = gtk_tree_model_get_column_type (model,
4547                                                         icon_view->priv->pixbuf_column);          
4548
4549           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4550         }
4551
4552       if (icon_view->priv->text_column != -1)
4553         {
4554           column_type = gtk_tree_model_get_column_type (model,
4555                                                         icon_view->priv->text_column);    
4556
4557           g_return_if_fail (column_type == G_TYPE_STRING);
4558         }
4559
4560       if (icon_view->priv->markup_column != -1)
4561         {
4562           column_type = gtk_tree_model_get_column_type (model,
4563                                                         icon_view->priv->markup_column);          
4564
4565           g_return_if_fail (column_type == G_TYPE_STRING);
4566         }
4567       
4568     }
4569   
4570   if (icon_view->priv->model)
4571     {
4572       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4573                                             gtk_icon_view_row_changed,
4574                                             icon_view);
4575       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4576                                             gtk_icon_view_row_inserted,
4577                                             icon_view);
4578       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4579                                             gtk_icon_view_row_deleted,
4580                                             icon_view);
4581       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4582                                             gtk_icon_view_rows_reordered,
4583                                             icon_view);
4584
4585       g_object_unref (icon_view->priv->model);
4586       
4587       g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
4588       g_list_free (icon_view->priv->items);
4589       icon_view->priv->items = NULL;
4590       icon_view->priv->anchor_item = NULL;
4591       icon_view->priv->cursor_item = NULL;
4592       icon_view->priv->last_single_clicked = NULL;
4593       icon_view->priv->width = 0;
4594       icon_view->priv->height = 0;
4595     }
4596
4597   icon_view->priv->model = model;
4598
4599   if (icon_view->priv->model)
4600     {
4601       g_object_ref (icon_view->priv->model);
4602       g_signal_connect (icon_view->priv->model,
4603                         "row_changed",
4604                         G_CALLBACK (gtk_icon_view_row_changed),
4605                         icon_view);
4606       g_signal_connect (icon_view->priv->model,
4607                         "row_inserted",
4608                         G_CALLBACK (gtk_icon_view_row_inserted),
4609                         icon_view);
4610       g_signal_connect (icon_view->priv->model,
4611                         "row_deleted",
4612                         G_CALLBACK (gtk_icon_view_row_deleted),
4613                         icon_view);
4614       g_signal_connect (icon_view->priv->model,
4615                         "rows_reordered",
4616                         G_CALLBACK (gtk_icon_view_rows_reordered),
4617                         icon_view);
4618
4619       gtk_icon_view_build_items (icon_view);
4620
4621       gtk_icon_view_queue_layout (icon_view);
4622     }
4623
4624   g_object_notify (G_OBJECT (icon_view), "model");  
4625
4626   if (GTK_WIDGET_REALIZED (icon_view))
4627     gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4628 }
4629
4630 /**
4631  * gtk_icon_view_get_model:
4632  * @icon_view: a #GtkIconView
4633  *
4634  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
4635  * model is unset.
4636  *
4637  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
4638  *
4639  * Since: 2.6 
4640  **/
4641 GtkTreeModel *
4642 gtk_icon_view_get_model (GtkIconView *icon_view)
4643 {
4644   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4645
4646   return icon_view->priv->model;
4647 }
4648
4649 static void
4650 update_text_cell (GtkIconView *icon_view)
4651 {
4652   GtkIconViewCellInfo *info;
4653   GList *l;
4654   gint i;
4655           
4656   if (icon_view->priv->text_column == -1 &&
4657       icon_view->priv->markup_column == -1)
4658     {
4659       if (icon_view->priv->text_cell != -1)
4660         {
4661           info = g_list_nth_data (icon_view->priv->cell_list, 
4662                                   icon_view->priv->text_cell);
4663           
4664           g_list_remove (icon_view->priv->cell_list, info);
4665           
4666           free_cell_info (info);
4667           
4668           icon_view->priv->n_cells--;
4669           icon_view->priv->text_cell = -1;
4670         }
4671     }
4672   else 
4673     {
4674       if (icon_view->priv->text_cell == -1)
4675         {
4676           GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
4677           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
4678           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4679             {
4680               info = l->data;
4681               if (info->cell == cell)
4682                 {
4683                   icon_view->priv->text_cell = i;
4684                   break;
4685                 }
4686             }
4687         }
4688       
4689       info = g_list_nth_data (icon_view->priv->cell_list,
4690                               icon_view->priv->text_cell);
4691
4692       if (icon_view->priv->markup_column != -1)
4693         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4694                                         info->cell, 
4695                                         "markup", icon_view->priv->markup_column, 
4696                                         NULL);
4697       else
4698         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4699                                         info->cell, 
4700                                         "text", icon_view->priv->text_column, 
4701                                         NULL);
4702
4703       if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
4704         g_object_set (info->cell,
4705                       "wrap_mode", PANGO_WRAP_CHAR,
4706                       "wrap_width", icon_view->priv->item_width,
4707                       "xalign", 0.5,
4708                       "yalign", 0.0,
4709                       NULL);
4710       else
4711         g_object_set (info->cell,
4712                       "wrap_mode", PANGO_WRAP_CHAR,
4713                       "wrap_width", icon_view->priv->item_width,
4714                       "xalign", 0.0,
4715                       "yalign", 0.0,
4716                       NULL);
4717     }
4718 }
4719
4720 static void
4721 update_pixbuf_cell (GtkIconView *icon_view)
4722 {
4723   GtkIconViewCellInfo *info;
4724   GList *l;
4725   gint i;
4726
4727   if (icon_view->priv->pixbuf_column == -1)
4728     {
4729       if (icon_view->priv->pixbuf_cell != -1)
4730         {
4731           info = g_list_nth_data (icon_view->priv->cell_list, 
4732                                   icon_view->priv->pixbuf_cell);
4733           
4734           g_list_remove (icon_view->priv->cell_list, info);
4735           
4736           free_cell_info (info);
4737           
4738           icon_view->priv->n_cells--;
4739           icon_view->priv->pixbuf_cell = -1;
4740         }
4741     }
4742   else 
4743     {
4744       if (icon_view->priv->pixbuf_cell == -1)
4745         {
4746           GtkCellRenderer *cell = gtk_cell_renderer_pixbuf_new ();
4747           
4748           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
4749           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4750             {
4751               info = l->data;
4752               if (info->cell == cell)
4753                 {
4754                   icon_view->priv->pixbuf_cell = i;
4755                   break;
4756                 }
4757             }
4758         }
4759       
4760         info = g_list_nth_data (icon_view->priv->cell_list, 
4761                                 icon_view->priv->pixbuf_cell);
4762         
4763         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4764                                         info->cell, 
4765                                         "pixbuf", icon_view->priv->pixbuf_column, 
4766                                         NULL);
4767
4768         if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
4769           g_object_set (info->cell,
4770                         "follow_state", TRUE, 
4771                         "xalign", 0.5,
4772                         "yalign", 1.0,
4773                         NULL);
4774         else
4775           g_object_set (info->cell,
4776                         "follow_state", TRUE, 
4777                         "xalign", 0.0,
4778                         "yalign", 0.0,
4779                         NULL);
4780     }
4781 }
4782
4783 /**
4784  * gtk_icon_view_set_text_column:
4785  * @icon_view: A #GtkIconView.
4786  * @column: A column in the currently used model.
4787  * 
4788  * Sets the column with text for @icon_view to be @column. The text
4789  * column must be of type #G_TYPE_STRING.
4790  *
4791  * Since: 2.6 
4792  **/
4793 void
4794 gtk_icon_view_set_text_column (GtkIconView *icon_view,
4795                                gint          column)
4796 {
4797   if (column == icon_view->priv->text_column)
4798     return;
4799   
4800   if (column == -1)
4801     icon_view->priv->text_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->text_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), "text-column");
4824 }
4825
4826 /**
4827  * gtk_icon_view_get_text_column:
4828  * @icon_view: A #GtkIconView.
4829  *
4830  * Returns the column with text for @icon_view.
4831  *
4832  * Returns: the text column, or -1 if it's unset.
4833  *
4834  * Since: 2.6
4835  */
4836 gint
4837 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
4838 {
4839   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4840
4841   return icon_view->priv->text_column;
4842 }
4843
4844 /**
4845  * gtk_icon_view_set_markup_column:
4846  * @icon_view: A #GtkIconView.
4847  * @column: A column in the currently used model.
4848  * 
4849  * Sets the column with markup information for @icon_view to be
4850  * @column. The markup column must be of type #G_TYPE_STRING.
4851  * If the markup column is set to something, it overrides
4852  * the text column set by gtk_icon_view_set_text_column().
4853  *
4854  * Since: 2.6
4855  **/
4856 void
4857 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
4858                                  gint         column)
4859 {
4860   if (column == icon_view->priv->markup_column)
4861     return;
4862   
4863   if (column == -1)
4864     icon_view->priv->markup_column = -1;
4865   else
4866     {
4867       if (icon_view->priv->model != NULL)
4868         {
4869           GType column_type;
4870           
4871           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4872
4873           g_return_if_fail (column_type == G_TYPE_STRING);
4874         }
4875       
4876       icon_view->priv->markup_column = column;
4877     }
4878
4879   gtk_icon_view_stop_editing (icon_view, TRUE);
4880
4881   update_text_cell (icon_view);
4882
4883   gtk_icon_view_invalidate_sizes (icon_view);
4884   gtk_icon_view_queue_layout (icon_view);
4885   
4886   g_object_notify (G_OBJECT (icon_view), "markup-column");
4887 }
4888
4889 /**
4890  * gtk_icon_view_get_markup_column:
4891  * @icon_view: A #GtkIconView.
4892  *
4893  * Returns the column with markup text for @icon_view.
4894  *
4895  * Returns: the markup column, or -1 if it's unset.
4896  *
4897  * Since: 2.6
4898  */
4899 gint
4900 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
4901 {
4902   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4903
4904   return icon_view->priv->markup_column;
4905 }
4906
4907 /**
4908  * gtk_icon_view_set_pixbuf_column:
4909  * @icon_view: A #GtkIconView.
4910  * @column: A column in the currently used model.
4911  * 
4912  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
4913  * column must be of type #GDK_TYPE_PIXBUF
4914  *
4915  * Since: 2.6 
4916  **/
4917 void
4918 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
4919                                  gint         column)
4920 {
4921   if (column == icon_view->priv->pixbuf_column)
4922     return;
4923   
4924   if (column == -1)
4925     icon_view->priv->pixbuf_column = -1;
4926   else
4927     {
4928       if (icon_view->priv->model != NULL)
4929         {
4930           GType column_type;
4931           
4932           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4933
4934           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4935         }
4936       
4937       icon_view->priv->pixbuf_column = column;
4938     }
4939
4940   gtk_icon_view_stop_editing (icon_view, TRUE);
4941
4942   update_pixbuf_cell (icon_view);
4943
4944   gtk_icon_view_invalidate_sizes (icon_view);
4945   gtk_icon_view_queue_layout (icon_view);
4946   
4947   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
4948   
4949 }
4950
4951 /**
4952  * gtk_icon_view_get_pixbuf_column:
4953  * @icon_view: A #GtkIconView.
4954  *
4955  * Returns the column with pixbufs for @icon_view.
4956  *
4957  * Returns: the pixbuf column, or -1 if it's unset.
4958  *
4959  * Since: 2.6
4960  */
4961 gint
4962 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
4963 {
4964   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4965
4966   return icon_view->priv->pixbuf_column;
4967 }
4968
4969 /**
4970  * gtk_icon_view_select_path:
4971  * @icon_view: A #GtkIconView.
4972  * @path: The #GtkTreePath to be selected.
4973  * 
4974  * Selects the row at @path.
4975  *
4976  * Since: 2.6
4977  **/
4978 void
4979 gtk_icon_view_select_path (GtkIconView *icon_view,
4980                            GtkTreePath *path)
4981 {
4982   GList *l;
4983
4984   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4985   g_return_if_fail (icon_view->priv->model != NULL);
4986   g_return_if_fail (path != NULL);
4987
4988   l = g_list_nth (icon_view->priv->items,
4989                   gtk_tree_path_get_indices(path)[0]);
4990
4991   if (l != NULL)
4992     gtk_icon_view_select_item (icon_view, l->data);
4993 }
4994
4995 /**
4996  * gtk_icon_view_unselect_path:
4997  * @icon_view: A #GtkIconView.
4998  * @path: The #GtkTreePath to be unselected.
4999  * 
5000  * Unselects the row at @path.
5001  *
5002  * Since: 2.6
5003  **/
5004 void
5005 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5006                              GtkTreePath *path)
5007 {
5008   GtkIconViewItem *item;
5009   
5010   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5011   g_return_if_fail (icon_view->priv->model != NULL);
5012   g_return_if_fail (path != NULL);
5013
5014   item = g_list_nth (icon_view->priv->items,
5015                      gtk_tree_path_get_indices(path)[0])->data;
5016
5017   if (!item)
5018     return;
5019   
5020   gtk_icon_view_unselect_item (icon_view, item);
5021 }
5022
5023 /**
5024  * gtk_icon_view_get_selected_items:
5025  * @icon_view: A #GtkIconView.
5026  *
5027  * Creates a list of paths of all selected items. Additionally, if you are
5028  * planning on modifying the model after calling this function, you may
5029  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5030  * To do this, you can use gtk_tree_row_reference_new().
5031  *
5032  * To free the return value, use:
5033  * <informalexample><programlisting>
5034  * g_list_foreach (list, gtk_tree_path_free, NULL);
5035  * g_list_free (list);
5036  * </programlisting></informalexample>
5037  *
5038  * Return value: A #GList containing a #GtkTreePath for each selected row.
5039  *
5040  * Since: 2.6
5041  **/
5042 GList *
5043 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5044 {
5045   GList *list;
5046   GList *selected = NULL;
5047   
5048   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5049   
5050   for (list = icon_view->priv->items; list != NULL; list = list->next)
5051     {
5052       GtkIconViewItem *item = list->data;
5053
5054       if (item->selected)
5055         {
5056           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5057
5058           selected = g_list_prepend (selected, path);
5059         }
5060     }
5061
5062   return selected;
5063 }
5064
5065 /**
5066  * gtk_icon_view_select_all:
5067  * @icon_view: A #GtkIconView.
5068  * 
5069  * Selects all the icons. @icon_view must has its selection mode set
5070  * to #GTK_SELECTION_MULTIPLE.
5071  *
5072  * Since: 2.6
5073  **/
5074 void
5075 gtk_icon_view_select_all (GtkIconView *icon_view)
5076 {
5077   GList *items;
5078   gboolean dirty = FALSE;
5079   
5080   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5081
5082   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5083     return;
5084
5085   for (items = icon_view->priv->items; items; items = items->next)
5086     {
5087       GtkIconViewItem *item = items->data;
5088       
5089       if (!item->selected)
5090         {
5091           dirty = TRUE;
5092           item->selected = TRUE;
5093           gtk_icon_view_queue_draw_item (icon_view, item);
5094         }
5095     }
5096
5097   if (dirty)
5098     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5099 }
5100
5101 /**
5102  * gtk_icon_view_unselect_all:
5103  * @icon_view: A #GtkIconView.
5104  * 
5105  * Unselects all the icons.
5106  *
5107  * Since: 2.6
5108  **/
5109 void
5110 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5111 {
5112   gboolean dirty = FALSE;
5113   
5114   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5115
5116   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5117     return;
5118
5119   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5120
5121   if (dirty)
5122     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5123 }
5124
5125 /**
5126  * gtk_icon_view_path_is_selected:
5127  * @icon_view: A #GtkIconView.
5128  * @path: A #GtkTreePath to check selection on.
5129  * 
5130  * Returns %TRUE if the icon pointed to by @path is currently
5131  * selected. If @icon does not point to a valid location, %FALSE is returned.
5132  * 
5133  * Return value: %TRUE if @path is selected.
5134  *
5135  * Since: 2.6
5136  **/
5137 gboolean
5138 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5139                                 GtkTreePath *path)
5140 {
5141   GtkIconViewItem *item;
5142   
5143   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5144   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5145   g_return_val_if_fail (path != NULL, FALSE);
5146   
5147   item = g_list_nth (icon_view->priv->items,
5148                      gtk_tree_path_get_indices(path)[0])->data;
5149
5150   if (!item)
5151     return FALSE;
5152   
5153   return item->selected;
5154 }
5155
5156 /**
5157  * gtk_icon_view_item_activated:
5158  * @icon_view: A #GtkIconView
5159  * @path: The #GtkTreePath to be activated
5160  * 
5161  * Activates the item determined by @path.
5162  *
5163  * Since: 2.6
5164  **/
5165 void
5166 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5167                               GtkTreePath      *path)
5168 {
5169   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5170   g_return_if_fail (path != NULL);
5171   
5172   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5173 }
5174
5175 /**
5176  * gtk_icon_view_set_orientation:
5177  * @icon_view: a #GtkIconView
5178  * @orientation: the relative position of texts and icons 
5179  * 
5180  * Sets the ::orientation property which determines whether the labels 
5181  * are drawn beside the icons instead of below.
5182  *
5183  * Since: 2.6
5184  **/
5185 void 
5186 gtk_icon_view_set_orientation (GtkIconView    *icon_view,
5187                                GtkOrientation  orientation)
5188 {
5189   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5190
5191   if (icon_view->priv->orientation != orientation)
5192     {
5193       icon_view->priv->orientation = orientation;
5194
5195       gtk_icon_view_stop_editing (icon_view, TRUE);
5196       gtk_icon_view_invalidate_sizes (icon_view);
5197       gtk_icon_view_queue_layout (icon_view);
5198
5199       update_text_cell (icon_view);
5200       update_pixbuf_cell (icon_view);
5201       
5202       g_object_notify (G_OBJECT (icon_view), "orientation");
5203     }
5204 }
5205
5206 /**
5207  * gtk_icon_view_get_orientation:
5208  * @icon_view: a #GtkIconView
5209  * 
5210  * Returns the value of the ::orientation property which determines 
5211  * whether the labels are drawn beside the icons instead of below. 
5212  * 
5213  * Return value: the relative position of texts and icons 
5214  *
5215  * Since: 2.6
5216  **/
5217 GtkOrientation
5218 gtk_icon_view_get_orientation (GtkIconView *icon_view)
5219 {
5220   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5221                         GTK_ORIENTATION_VERTICAL);
5222
5223   return icon_view->priv->orientation;
5224 }
5225
5226 /**
5227  * gtk_icon_view_set_columns:
5228  * @icon_view: a #GtkIconView
5229  * @columns: the number of columns
5230  * 
5231  * Sets the ::columns property which determines in how
5232  * many columns the icons are arranged. If @columns is
5233  * -1, the number of columns will be chosen automatically 
5234  * to fill the available area. 
5235  *
5236  * Since: 2.6
5237  */
5238 void 
5239 gtk_icon_view_set_columns (GtkIconView *icon_view,
5240                            gint         columns)
5241 {
5242   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5243   
5244   if (icon_view->priv->columns != columns)
5245     {
5246       icon_view->priv->columns = columns;
5247
5248       gtk_icon_view_stop_editing (icon_view, TRUE);
5249       gtk_icon_view_queue_layout (icon_view);
5250       
5251       g_object_notify (G_OBJECT (icon_view), "columns");
5252     }  
5253 }
5254
5255 /**
5256  * gtk_icon_view_get_columns:
5257  * @icon_view: a #GtkIconView
5258  * 
5259  * Returns the value of the ::columns property.
5260  * 
5261  * Return value: the number of columns, or -1
5262  *
5263  * Since: 2.6
5264  */
5265 gint
5266 gtk_icon_view_get_columns (GtkIconView *icon_view)
5267 {
5268   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5269
5270   return icon_view->priv->columns;
5271 }
5272
5273 /**
5274  * gtk_icon_view_set_item_width:
5275  * @icon_view: a #GtkIconView
5276  * @item_width: the width for each item
5277  * 
5278  * Sets the ::item-width property which specifies the width 
5279  * to use for each item. If it is set to -1, the icon view will 
5280  * automatically determine a suitable item size.
5281  *
5282  * Since: 2.6
5283  */
5284 void 
5285 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5286                               gint         item_width)
5287 {
5288   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5289   
5290   if (icon_view->priv->item_width != item_width)
5291     {
5292       icon_view->priv->item_width = item_width;
5293       
5294       gtk_icon_view_stop_editing (icon_view, TRUE);
5295       gtk_icon_view_invalidate_sizes (icon_view);
5296       gtk_icon_view_queue_layout (icon_view);
5297       
5298       update_text_cell (icon_view);
5299
5300       g_object_notify (G_OBJECT (icon_view), "item-width");
5301     }  
5302 }
5303
5304 /**
5305  * gtk_icon_view_get_item_width:
5306  * @icon_view: a #GtkIconView
5307  * 
5308  * Returns the value of the ::item-width property.
5309  * 
5310  * Return value: the width of a single item, or -1
5311  *
5312  * Since: 2.6
5313  */
5314 gint
5315 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5316 {
5317   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5318
5319   return icon_view->priv->item_width;
5320 }
5321
5322
5323 /**
5324  * gtk_icon_view_set_spacing:
5325  * @icon_view: a #GtkIconView
5326  * @spacing: the spacing
5327  * 
5328  * Sets the ::spacing property which specifies the space 
5329  * which is inserted between the cells (i.e. the icon and 
5330  * the text) of an item.
5331  *
5332  * Since: 2.6
5333  */
5334 void 
5335 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5336                            gint         spacing)
5337 {
5338   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5339   
5340   if (icon_view->priv->spacing != spacing)
5341     {
5342       icon_view->priv->spacing = spacing;
5343
5344       gtk_icon_view_stop_editing (icon_view, TRUE);
5345       gtk_icon_view_invalidate_sizes (icon_view);
5346       gtk_icon_view_queue_layout (icon_view);
5347       
5348       g_object_notify (G_OBJECT (icon_view), "spacing");
5349     }  
5350 }
5351
5352 /**
5353  * gtk_icon_view_get_spacing:
5354  * @icon_view: a #GtkIconView
5355  * 
5356  * Returns the value of the ::spacing property.
5357  * 
5358  * Return value: the space between cells 
5359  *
5360  * Since: 2.6
5361  */
5362 gint
5363 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5364 {
5365   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5366
5367   return icon_view->priv->spacing;
5368 }
5369
5370 /**
5371  * gtk_icon_view_set_row_spacing:
5372  * @icon_view: a #GtkIconView
5373  * @row_spacing: the row spacing
5374  * 
5375  * Sets the ::row-spacing property which specifies the space 
5376  * which is inserted between the rows of the icon view.
5377  *
5378  * Since: 2.6
5379  */
5380 void 
5381 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5382                                gint         row_spacing)
5383 {
5384   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5385   
5386   if (icon_view->priv->row_spacing != row_spacing)
5387     {
5388       icon_view->priv->row_spacing = row_spacing;
5389
5390       gtk_icon_view_stop_editing (icon_view, TRUE);
5391       gtk_icon_view_invalidate_sizes (icon_view);
5392       gtk_icon_view_queue_layout (icon_view);
5393       
5394       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5395     }  
5396 }
5397
5398 /**
5399  * gtk_icon_view_get_row_spacing:
5400  * @icon_view: a #GtkIconView
5401  * 
5402  * Returns the value of the ::row-spacing property.
5403  * 
5404  * Return value: the space between rows
5405  *
5406  * Since: 2.6
5407  */
5408 gint
5409 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5410 {
5411   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5412
5413   return icon_view->priv->row_spacing;
5414 }
5415
5416 /**
5417  * gtk_icon_view_set_column_spacing:
5418  * @icon_view: a #GtkIconView
5419  * @column_spacing: the column spacing
5420  * 
5421  * Sets the ::column-spacing property which specifies the space 
5422  * which is inserted between the columns of the icon view.
5423  *
5424  * Since: 2.6
5425  */
5426 void 
5427 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5428                                   gint         column_spacing)
5429 {
5430   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5431   
5432   if (icon_view->priv->column_spacing != column_spacing)
5433     {
5434       icon_view->priv->column_spacing = column_spacing;
5435
5436       gtk_icon_view_stop_editing (icon_view, TRUE);
5437       gtk_icon_view_invalidate_sizes (icon_view);
5438       gtk_icon_view_queue_layout (icon_view);
5439       
5440       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5441     }  
5442 }
5443
5444 /**
5445  * gtk_icon_view_get_column_spacing:
5446  * @icon_view: a #GtkIconView
5447  * 
5448  * Returns the value of the ::column-spacing property.
5449  * 
5450  * Return value: the space between columns
5451  *
5452  * Since: 2.6
5453  */
5454 gint
5455 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5456 {
5457   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5458
5459   return icon_view->priv->column_spacing;
5460 }
5461
5462 /**
5463  * gtk_icon_view_set_margin:
5464  * @icon_view: a #GtkIconView
5465  * @margin: the margin
5466  * 
5467  * Sets the ::margin property which specifies the space 
5468  * which is inserted at the top, bottom, left and right 
5469  * of the icon view.
5470  *
5471  * Since: 2.6
5472  */
5473 void 
5474 gtk_icon_view_set_margin (GtkIconView *icon_view,
5475                           gint         margin)
5476 {
5477   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5478   
5479   if (icon_view->priv->margin != margin)
5480     {
5481       icon_view->priv->margin = margin;
5482
5483       gtk_icon_view_stop_editing (icon_view, TRUE);
5484       gtk_icon_view_invalidate_sizes (icon_view);
5485       gtk_icon_view_queue_layout (icon_view);
5486       
5487       g_object_notify (G_OBJECT (icon_view), "margin");
5488     }  
5489 }
5490
5491 /**
5492  * gtk_icon_view_get_margin:
5493  * @icon_view: a #GtkIconView
5494  * 
5495  * Returns the value of the ::margin property.
5496  * 
5497  * Return value: the space at the borders 
5498  *
5499  * Since: 2.6
5500  */
5501 gint
5502 gtk_icon_view_get_margin (GtkIconView *icon_view)
5503 {
5504   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5505
5506   return icon_view->priv->margin;
5507 }
5508
5509
5510 /* Get/set whether drag_motion requested the drag data and
5511  * drag_data_received should thus not actually insert the data,
5512  * since the data doesn't result from a drop.
5513  */
5514 static void
5515 set_status_pending (GdkDragContext *context,
5516                     GdkDragAction   suggested_action)
5517 {
5518   g_object_set_data (G_OBJECT (context),
5519                      "gtk-icon-view-status-pending",
5520                      GINT_TO_POINTER (suggested_action));
5521 }
5522
5523 static GdkDragAction
5524 get_status_pending (GdkDragContext *context)
5525 {
5526   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5527                                              "gtk-icon-view-status-pending"));
5528 }
5529
5530 static void
5531 unset_reorderable (GtkIconView *icon_view)
5532 {
5533   if (icon_view->priv->reorderable)
5534     {
5535       icon_view->priv->reorderable = FALSE;
5536       g_object_notify (G_OBJECT (icon_view), "reorderable");
5537     }
5538 }
5539
5540 static void
5541 clear_source_info (GtkIconView *icon_view)
5542 {
5543   if (icon_view->priv->source_targets)
5544     gtk_target_list_unref (icon_view->priv->source_targets);
5545   icon_view->priv->source_targets = NULL;
5546
5547   icon_view->priv->source_set = FALSE;
5548 }
5549
5550 static void
5551 clear_dest_info (GtkIconView *icon_view)
5552 {
5553   if (icon_view->priv->dest_targets)
5554     gtk_target_list_unref (icon_view->priv->dest_targets);
5555   icon_view->priv->dest_targets = NULL;
5556
5557   icon_view->priv->dest_set = FALSE;
5558 }
5559
5560 static void
5561 set_source_row (GdkDragContext *context,
5562                 GtkTreeModel   *model,
5563                 GtkTreePath    *source_row)
5564 {
5565   if (source_row)
5566     g_object_set_data_full (G_OBJECT (context),
5567                             "gtk-icon-view-source-row",
5568                             gtk_tree_row_reference_new (model, source_row),
5569                             (GDestroyNotify) gtk_tree_row_reference_free);
5570   else
5571     g_object_set_data_full (G_OBJECT (context),
5572                             "gtk-icon-view-source-row",
5573                             NULL, NULL);
5574 }
5575
5576 static GtkTreePath*
5577 get_source_row (GdkDragContext *context)
5578 {
5579   GtkTreeRowReference *ref;
5580
5581   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5582
5583   if (ref)
5584     return gtk_tree_row_reference_get_path (ref);
5585   else
5586     return NULL;
5587 }
5588
5589 typedef struct
5590 {
5591   GtkTreeRowReference *dest_row;
5592   gboolean             empty_view_drop;
5593   gboolean             drop_append_mode;
5594 } DestRow;
5595
5596 static void
5597 dest_row_free (gpointer data)
5598 {
5599   DestRow *dr = (DestRow *)data;
5600
5601   gtk_tree_row_reference_free (dr->dest_row);
5602   g_free (dr);
5603 }
5604
5605 static void
5606 set_dest_row (GdkDragContext *context,
5607               GtkTreeModel   *model,
5608               GtkTreePath    *dest_row,
5609               gboolean        empty_view_drop,
5610               gboolean        drop_append_mode)
5611 {
5612   DestRow *dr;
5613
5614   if (!dest_row)
5615     {
5616       g_object_set_data_full (G_OBJECT (context),
5617                               "gtk-icon-view-dest-row",
5618                               NULL, NULL);
5619       return;
5620     }
5621   
5622   dr = g_new0 (DestRow, 1);
5623      
5624   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5625   dr->empty_view_drop = empty_view_drop;
5626   dr->drop_append_mode = drop_append_mode;
5627   g_object_set_data_full (G_OBJECT (context),
5628                           "gtk-icon-view-dest-row",
5629                           dr, (GDestroyNotify) dest_row_free);
5630 }
5631
5632 static GtkTreePath*
5633 get_dest_row (GdkDragContext *context)
5634 {
5635   DestRow *dr;
5636
5637   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
5638
5639   if (dr)
5640     {
5641       GtkTreePath *path = NULL;
5642       
5643       if (dr->dest_row)
5644         path = gtk_tree_row_reference_get_path (dr->dest_row);
5645       else if (dr->empty_view_drop)
5646         path = gtk_tree_path_new_from_indices (0, -1);
5647       else
5648         path = NULL;
5649
5650       if (path && dr->drop_append_mode)
5651         gtk_tree_path_next (path);
5652
5653       return path;
5654     }
5655   else
5656     return NULL;
5657 }
5658
5659 static gboolean
5660 check_model_dnd (GtkTreeModel *model,
5661                  GType         required_iface,
5662                  const gchar  *signal)
5663 {
5664   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5665     {
5666       g_warning ("You must override the default '%s' handler "
5667                  "on GtkIconView when using models that don't support "
5668                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5669                  "is to connect to '%s' and call "
5670                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5671                  "the default handler from running. Look at the source code "
5672                  "for the default handler in gtkiconview.c to get an idea what "
5673                  "your handler should do. (gtkiconview.c is in the GTK+ source "
5674                  "code.) If you're using GTK+ from a language other than C, "
5675                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5676                  signal, g_type_name (required_iface), signal);
5677       return FALSE;
5678     }
5679   else
5680     return TRUE;
5681 }
5682
5683 static void
5684 remove_scroll_timeout (GtkIconView *icon_view)
5685 {
5686   if (icon_view->priv->scroll_timeout_id != 0)
5687     {
5688       g_source_remove (icon_view->priv->scroll_timeout_id);
5689
5690       icon_view->priv->scroll_timeout_id = 0;
5691     }
5692 }
5693
5694 static void
5695 gtk_icon_view_autoscroll (GtkIconView *icon_view)
5696 {
5697   gint px, py, x, y, width, height;
5698   gint hoffset, voffset;
5699   gfloat value;
5700
5701   gdk_window_get_pointer (GTK_WIDGET (icon_view)->window, &px, &py, NULL);
5702   gdk_window_get_geometry (GTK_WIDGET (icon_view)->window, &x, &y, &width, &height, NULL);
5703
5704   /* see if we are near the edge. */
5705   voffset = py - (y + 2 * SCROLL_EDGE_SIZE);
5706   if (voffset > 0)
5707     voffset = MAX (py - (y + height - 2 * SCROLL_EDGE_SIZE), 0);
5708
5709   hoffset = px - (x + 2 * SCROLL_EDGE_SIZE);
5710   if (hoffset > 0)
5711     hoffset = MAX (px - (x + width - 2 * SCROLL_EDGE_SIZE), 0);
5712
5713   if (voffset != 0)
5714     {
5715       value = CLAMP (icon_view->priv->vadjustment->value + voffset, 
5716                      icon_view->priv->vadjustment->lower,
5717                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
5718       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
5719     }
5720   if (hoffset != 0)
5721     {
5722       value = CLAMP (icon_view->priv->hadjustment->value + hoffset, 
5723                      icon_view->priv->hadjustment->lower,
5724                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
5725       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
5726     }
5727 }
5728
5729
5730 static gboolean
5731 drag_scroll_timeout (gpointer data)
5732 {
5733   GtkIconView *icon_view = GTK_ICON_VIEW (data);
5734
5735   GDK_THREADS_ENTER ();
5736
5737   gtk_icon_view_autoscroll (icon_view);
5738
5739   GDK_THREADS_LEAVE ();
5740
5741   return TRUE;
5742 }
5743
5744
5745 static gboolean
5746 set_destination (GtkIconView    *icon_view,
5747                  GdkDragContext *context,
5748                  gint            x,
5749                  gint            y,
5750                  GdkDragAction  *suggested_action,
5751                  GdkAtom        *target)
5752 {
5753   GtkWidget *widget;
5754   GtkTreePath *path = NULL;
5755   GtkIconViewDropPosition pos;
5756   GtkIconViewDropPosition old_pos;
5757   GtkTreePath *old_dest_path = NULL;
5758   gboolean can_drop = FALSE;
5759
5760   widget = GTK_WIDGET (icon_view);
5761
5762   *suggested_action = 0;
5763   *target = GDK_NONE;
5764
5765   if (!icon_view->priv->dest_set)
5766     {
5767       /* someone unset us as a drag dest, note that if
5768        * we return FALSE drag_leave isn't called
5769        */
5770
5771       gtk_icon_view_set_drag_dest_item (icon_view,
5772                                         NULL,
5773                                         GTK_ICON_VIEW_DROP_LEFT);
5774
5775       remove_scroll_timeout (GTK_ICON_VIEW (widget));
5776
5777       return FALSE; /* no longer a drop site */
5778     }
5779
5780   *target = gtk_drag_dest_find_target (widget, context, icon_view->priv->dest_targets);
5781   if (*target == GDK_NONE)
5782     return FALSE;
5783
5784   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
5785     {
5786       gint n_children;
5787       GtkTreeModel *model;
5788       
5789       /* the row got dropped on empty space, let's setup a special case
5790        */
5791
5792       if (path)
5793         gtk_tree_path_free (path);
5794
5795       model = gtk_icon_view_get_model (icon_view);
5796
5797       n_children = gtk_tree_model_iter_n_children (model, NULL);
5798       if (n_children)
5799         {
5800           pos = GTK_ICON_VIEW_DROP_BELOW;
5801           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
5802         }
5803       else
5804         {
5805           pos = GTK_ICON_VIEW_DROP_ABOVE;
5806           path = gtk_tree_path_new_from_indices (0, -1);
5807         }
5808
5809       can_drop = TRUE;
5810
5811       goto out;
5812     }
5813
5814   g_assert (path);
5815
5816   gtk_icon_view_get_drag_dest_item (icon_view,
5817                                     &old_dest_path,
5818                                     &old_pos);
5819   
5820   if (old_dest_path)
5821     gtk_tree_path_free (old_dest_path);
5822   
5823   if (TRUE /* FIXME if the location droppable predicate */)
5824     {
5825       can_drop = TRUE;
5826     }
5827
5828 out:
5829   if (can_drop)
5830     {
5831       GtkWidget *source_widget;
5832
5833       *suggested_action = context->suggested_action;
5834       source_widget = gtk_drag_get_source_widget (context);
5835
5836       if (source_widget == widget)
5837         {
5838           /* Default to MOVE, unless the user has
5839            * pressed ctrl or shift to affect available actions
5840            */
5841           if ((context->actions & GDK_ACTION_MOVE) != 0)
5842             *suggested_action = GDK_ACTION_MOVE;
5843         }
5844
5845       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
5846                                         path, pos);
5847     }
5848   else
5849     {
5850       /* can't drop here */
5851       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
5852                                         NULL,
5853                                         GTK_ICON_VIEW_DROP_LEFT);
5854     }
5855   
5856   if (path)
5857     gtk_tree_path_free (path);
5858   
5859   return TRUE;
5860 }
5861
5862 static GtkTreePath*
5863 get_logical_destination (GtkIconView *icon_view,
5864                          gboolean    *drop_append_mode)
5865 {
5866   /* adjust path to point to the row the drop goes in front of */
5867   GtkTreePath *path = NULL;
5868   GtkIconViewDropPosition pos;
5869   
5870   *drop_append_mode = FALSE;
5871
5872   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
5873
5874   if (path == NULL)
5875     return NULL;
5876
5877   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
5878       pos == GTK_ICON_VIEW_DROP_BELOW)
5879     {
5880       GtkTreeIter iter;
5881       GtkTreeModel *model = icon_view->priv->model;
5882
5883       if (!gtk_tree_model_get_iter (model, &iter, path) ||
5884           !gtk_tree_model_iter_next (model, &iter))
5885         *drop_append_mode = TRUE;
5886       else
5887         {
5888           *drop_append_mode = FALSE;
5889           gtk_tree_path_next (path);
5890         }      
5891     }
5892
5893   return path;
5894 }
5895
5896 static gboolean
5897 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
5898                                 GdkEventMotion *event)
5899 {
5900   GdkDragContext *context;
5901   GtkTreePath *path = NULL;
5902   gint button;
5903   GtkTreeModel *model;
5904   gboolean retval = FALSE;
5905
5906   if (!icon_view->priv->source_set)
5907     goto out;
5908
5909   if (icon_view->priv->pressed_button < 0)
5910     goto out;
5911
5912   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
5913                                  icon_view->priv->press_start_x,
5914                                  icon_view->priv->press_start_y,
5915                                  event->x, event->y))
5916     goto out;
5917
5918   model = gtk_icon_view_get_model (icon_view);
5919
5920   if (model == NULL)
5921     goto out;
5922
5923   button = icon_view->priv->pressed_button;
5924   icon_view->priv->pressed_button = -1;
5925
5926   path = gtk_icon_view_get_path_at_pos (icon_view,
5927                                         icon_view->priv->press_start_x,
5928                                         icon_view->priv->press_start_y);
5929
5930   if (path == NULL)
5931     goto out;
5932
5933   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
5934       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
5935                                            path))
5936     goto out;
5937
5938   /* FIXME Check whether we're a start button, if not return FALSE and
5939    * free path
5940    */
5941
5942   /* Now we can begin the drag */
5943   
5944   retval = TRUE;
5945
5946   context = gtk_drag_begin (GTK_WIDGET (icon_view),
5947                             icon_view->priv->source_targets,
5948                             icon_view->priv->source_actions,
5949                             button,
5950                             (GdkEvent*)event);
5951
5952   set_source_row (context, model, path);
5953   
5954  out:
5955   if (path)
5956     gtk_tree_path_free (path);
5957
5958   return retval;
5959 }
5960
5961 /* Source side drag signals */
5962 static void 
5963 gtk_icon_view_drag_begin (GtkWidget      *widget,
5964                           GdkDragContext *context)
5965 {
5966   GtkIconView *icon_view;
5967   GtkIconViewItem *item;
5968   GdkPixmap *icon;
5969   gint x, y;
5970   GtkTreePath *path;
5971
5972   icon_view = GTK_ICON_VIEW (widget);
5973
5974   /* if the user uses a custom DnD impl, we don't set the icon here */
5975   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
5976     return;
5977
5978   item = gtk_icon_view_get_item_at_coords (icon_view,
5979                                            icon_view->priv->press_start_x,
5980                                            icon_view->priv->press_start_y,
5981                                            TRUE,
5982                                            NULL);
5983
5984   g_return_if_fail (item != NULL);
5985
5986   x = icon_view->priv->press_start_x - item->x + 1;
5987   y = icon_view->priv->press_start_y - item->y + 1;
5988   
5989   path = gtk_tree_path_new_from_indices (item->index, -1);
5990   icon = gtk_icon_view_create_drag_icon (icon_view, path);
5991   gtk_tree_path_free (path);
5992
5993   gtk_drag_set_icon_pixmap (context, 
5994                             gdk_drawable_get_colormap (icon),
5995                             icon, 
5996                             NULL, 
5997                             x, y);
5998
5999   g_object_unref (icon);
6000 }
6001
6002 static void 
6003 gtk_icon_view_drag_end (GtkWidget      *widget,
6004                         GdkDragContext *context)
6005 {
6006   /* do nothing */
6007 }
6008
6009 static void 
6010 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6011                              GdkDragContext   *context,
6012                              GtkSelectionData *selection_data,
6013                              guint             info,
6014                              guint             time)
6015 {
6016   GtkIconView *icon_view;
6017   GtkTreeModel *model;
6018   GtkTreePath *source_row;
6019
6020   icon_view = GTK_ICON_VIEW (widget);
6021   model = gtk_icon_view_get_model (icon_view);
6022
6023   if (model == NULL)
6024     return;
6025
6026   if (!icon_view->priv->dest_set)
6027     return;
6028
6029   source_row = get_source_row (context);
6030
6031   if (source_row == NULL)
6032     return;
6033
6034   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6035    * any model; for DragSource models there are some other targets
6036    * we also support.
6037    */
6038
6039   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6040       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6041                                           source_row,
6042                                           selection_data))
6043     goto done;
6044
6045   /* If drag_data_get does nothing, try providing row data. */
6046   if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
6047     gtk_tree_set_row_drag_data (selection_data,
6048                                 model,
6049                                 source_row);
6050
6051  done:
6052   gtk_tree_path_free (source_row);
6053 }
6054
6055 static void 
6056 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6057                                 GdkDragContext *context)
6058 {
6059   GtkTreeModel *model;
6060   GtkIconView *icon_view;
6061   GtkTreePath *source_row;
6062
6063   icon_view = GTK_ICON_VIEW (widget);
6064   model = gtk_icon_view_get_model (icon_view);
6065
6066   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag_data_delete"))
6067     return;
6068
6069   if (!icon_view->priv->dest_set)
6070     return;
6071
6072   source_row = get_source_row (context);
6073
6074   if (source_row == NULL)
6075     return;
6076
6077   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6078                                          source_row);
6079
6080   gtk_tree_path_free (source_row);
6081
6082   set_source_row (context, NULL, NULL);
6083 }
6084
6085 /* Target side drag signals */
6086 static void
6087 gtk_icon_view_drag_leave (GtkWidget      *widget,
6088                           GdkDragContext *context,
6089                           guint           time)
6090 {
6091   GtkIconView *icon_view;
6092
6093   icon_view = GTK_ICON_VIEW (widget);
6094
6095   /* unset any highlight row */
6096   gtk_icon_view_set_drag_dest_item (icon_view,
6097                                     NULL,
6098                                     GTK_ICON_VIEW_DROP_LEFT);
6099
6100   remove_scroll_timeout (icon_view);
6101 }
6102
6103 static gboolean 
6104 gtk_icon_view_drag_motion (GtkWidget      *widget,
6105                            GdkDragContext *context,
6106                            gint            x,
6107                            gint            y,
6108                            guint           time)
6109 {
6110   GtkTreePath *path = NULL;
6111   GtkTreeModel *model;
6112   GtkIconViewDropPosition pos;
6113   GtkIconView *icon_view;
6114   GdkDragAction suggested_action = 0;
6115   GdkAtom target;
6116   gboolean empty;
6117
6118   icon_view = GTK_ICON_VIEW (widget);
6119
6120   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6121     return FALSE;
6122
6123   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6124
6125   /* we only know this *after* set_desination_row */
6126   model = gtk_icon_view_get_model (icon_view);
6127   empty = icon_view->priv->empty_view_drop;
6128
6129   if (path == NULL && !empty)
6130     {
6131       /* Can't drop here. */
6132       gdk_drag_status (context, 0, time);
6133     }
6134   else
6135     {
6136       if (icon_view->priv->scroll_timeout_id == 0)
6137         {
6138           icon_view->priv->scroll_timeout_id =
6139             g_timeout_add (50, drag_scroll_timeout, icon_view);
6140         }
6141
6142       if (target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
6143         {
6144           /* Request data so we can use the source row when
6145            * determining whether to accept the drop
6146            */
6147           set_status_pending (context, suggested_action);
6148           gtk_drag_get_data (widget, context, target, time);
6149         }
6150       else
6151         {
6152           set_status_pending (context, 0);
6153           gdk_drag_status (context, suggested_action, time);
6154         }
6155     }
6156
6157   if (path)
6158     gtk_tree_path_free (path);
6159
6160   return TRUE;
6161 }
6162
6163 static gboolean 
6164 gtk_icon_view_drag_drop (GtkWidget      *widget,
6165                          GdkDragContext *context,
6166                          gint            x,
6167                          gint            y,
6168                          guint           time)
6169 {
6170   GtkIconView *icon_view;
6171   GtkTreePath *path;
6172   GdkDragAction suggested_action = 0;
6173   GdkAtom target = GDK_NONE;
6174   GtkTreeModel *model;
6175   gboolean drop_append_mode;
6176
6177   icon_view = GTK_ICON_VIEW (widget);
6178   model = gtk_icon_view_get_model (icon_view);
6179
6180   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6181
6182   if (!icon_view->priv->dest_set)
6183     return FALSE;
6184
6185   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_drop"))
6186     return FALSE;
6187
6188   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6189     return FALSE;
6190   
6191   path = get_logical_destination (icon_view, &drop_append_mode);
6192
6193   if (target != GDK_NONE && path != NULL)
6194     {
6195       /* in case a motion had requested drag data, change things so we
6196        * treat drag data receives as a drop.
6197        */
6198       set_status_pending (context, 0);
6199       set_dest_row (context, model, path, 
6200                     icon_view->priv->empty_view_drop, drop_append_mode);
6201     }
6202
6203   if (path)
6204     gtk_tree_path_free (path);
6205
6206   /* Unset this thing */
6207   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6208
6209   if (target != GDK_NONE)
6210     {
6211       gtk_drag_get_data (widget, context, target, time);
6212       return TRUE;
6213     }
6214   else
6215     return FALSE;
6216 }
6217
6218 static void
6219 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6220                                   GdkDragContext   *context,
6221                                   gint              x,
6222                                   gint              y,
6223                                   GtkSelectionData *selection_data,
6224                                   guint             info,
6225                                   guint             time)
6226 {
6227   GtkTreePath *path;
6228   gboolean accepted = FALSE;
6229   GtkTreeModel *model;
6230   GtkIconView *icon_view;
6231   GtkTreePath *dest_row;
6232   GdkDragAction suggested_action;
6233   gboolean drop_append_mode;
6234   
6235   icon_view = GTK_ICON_VIEW (widget);  
6236   model = gtk_icon_view_get_model (icon_view);
6237
6238   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_data_received"))
6239     return;
6240
6241   if (!icon_view->priv->dest_set)
6242     return;
6243
6244   suggested_action = get_status_pending (context);
6245
6246   if (suggested_action)
6247     {
6248       /* We are getting this data due to a request in drag_motion,
6249        * rather than due to a request in drag_drop, so we are just
6250        * supposed to call drag_status, not actually paste in the
6251        * data.
6252        */
6253       path = get_logical_destination (icon_view, &drop_append_mode);
6254
6255       if (path == NULL)
6256         suggested_action = 0;
6257
6258       if (suggested_action)
6259         {
6260           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6261                                                      path,
6262                                                      selection_data))
6263             suggested_action = 0;
6264         }
6265
6266       gdk_drag_status (context, suggested_action, time);
6267
6268       if (path)
6269         gtk_tree_path_free (path);
6270
6271       /* If you can't drop, remove user drop indicator until the next motion */
6272       if (suggested_action == 0)
6273         gtk_icon_view_set_drag_dest_item (icon_view,
6274                                           NULL,
6275                                           GTK_ICON_VIEW_DROP_LEFT);
6276       return;
6277     }
6278   
6279
6280   dest_row = get_dest_row (context);
6281
6282   if (dest_row == NULL)
6283     return;
6284
6285   if (selection_data->length >= 0)
6286     {
6287       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6288                                                  dest_row,
6289                                                  selection_data))
6290         accepted = TRUE;
6291     }
6292
6293   gtk_drag_finish (context,
6294                    accepted,
6295                    (context->action == GDK_ACTION_MOVE),
6296                    time);
6297
6298   gtk_tree_path_free (dest_row);
6299
6300   /* drop dest_row */
6301   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6302 }
6303
6304 /* Drag-and-Drop support */
6305 /**
6306  * gtk_icon_view_enable_model_drag_source:
6307  * @icon_view: a #GtkIconTreeView
6308  * @start_button_mask: Mask of allowed buttons to start drag
6309  * @targets: the table of targets that the drag will support
6310  * @n_targets: the number of items in @targets
6311  * @actions: the bitmask of possible actions for a drag from this
6312  *    widget
6313  * 
6314  * Turns @icon_view into a drag source for automatic DND.
6315  *
6316  * Since: 2.8
6317  **/
6318 void
6319 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6320                                         GdkModifierType           start_button_mask,
6321                                         const GtkTargetEntry     *targets,
6322                                         gint                      n_targets,
6323                                         GdkDragAction             actions)
6324 {
6325   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6326
6327   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, NULL, 0, actions);
6328
6329   clear_source_info (icon_view);
6330   icon_view->priv->start_button_mask = start_button_mask;
6331   icon_view->priv->source_targets = gtk_target_list_new (targets, n_targets);
6332   icon_view->priv->source_actions = actions;
6333
6334   icon_view->priv->source_set = TRUE;
6335
6336   unset_reorderable (icon_view);
6337 }
6338
6339 /**
6340  * gtk_icon_view_enable_model_drag_dest:
6341  * @icon_view: a #GtkIconView
6342  * @targets: the table of targets that the drag will support
6343  * @n_targets: the number of items in @targets
6344  * @actions: the bitmask of possible actions for a drag from this
6345  *    widget
6346  * 
6347  * Turns @icon_view into a drop destination for automatic DND.
6348  *
6349  * Since: 2.8
6350  **/
6351 void 
6352 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6353                                       const GtkTargetEntry *targets,
6354                                       gint                  n_targets,
6355                                       GdkDragAction         actions)
6356 {
6357   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6358
6359   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, NULL, 0, actions);
6360
6361   clear_dest_info (icon_view);
6362
6363   icon_view->priv->dest_targets = gtk_target_list_new (targets, n_targets);
6364   icon_view->priv->dest_actions = actions;
6365
6366   icon_view->priv->dest_set = TRUE;
6367
6368   unset_reorderable (icon_view);  
6369 }
6370
6371 /**
6372  * gtk_icon_view_unset_model_drag_source:
6373  * @icon_view: a #GtkIconView
6374  * 
6375  * Undoes the effect of gtk_icon_view_enable_model_drag_source().
6376  *
6377  * Since: 2.8
6378  **/
6379 void
6380 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6381 {
6382   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6383
6384   if (icon_view->priv->source_set)
6385     {
6386       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6387       clear_source_info (icon_view);
6388     }
6389
6390   unset_reorderable (icon_view);
6391 }
6392
6393 /**
6394  * gtk_icon_view_unset_model_drag_dest:
6395  * @icon_view: a #GtkIconView
6396  * 
6397  * Undoes the effect of gtk_icon_view_enable_model_drag_dest().
6398  *
6399  * Since: 2.8
6400  **/
6401 void
6402 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6403 {
6404   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6405
6406   if (icon_view->priv->dest_set)
6407     {
6408       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6409       clear_dest_info (icon_view);
6410     }
6411
6412   unset_reorderable (icon_view);
6413 }
6414
6415 /* These are useful to implement your own custom stuff. */
6416 /**
6417  * gtk_icon_view_set_drag_dest_item:
6418  * @icon_view: a #GtkIconView
6419  * @path: The path of the item to highlight, or %NULL.
6420  * @pos: Specifies whether to drop, relative to the item
6421  * 
6422  * Sets the item that is highlighted for feedback.
6423  *
6424  * Since: 2.8
6425  */
6426 void
6427 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6428                                   GtkTreePath              *path,
6429                                   GtkIconViewDropPosition   pos)
6430 {
6431   /* Note; this function is exported to allow a custom DND
6432    * implementation, so it can't touch TreeViewDragInfo
6433    */
6434
6435   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6436
6437   if (icon_view->priv->dest_item)
6438     {
6439       GtkTreePath *current_path;
6440       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6441       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6442       icon_view->priv->dest_item = NULL;      
6443
6444       gtk_icon_view_queue_draw_path (icon_view, current_path);
6445       gtk_tree_path_free (current_path);
6446     }
6447   
6448   /* special case a drop on an empty model */
6449   icon_view->priv->empty_view_drop = FALSE;
6450   if (pos == GTK_TREE_VIEW_DROP_BEFORE && path
6451       && gtk_tree_path_get_depth (path) == 1
6452       && gtk_tree_path_get_indices (path)[0] == 0)
6453     {
6454       gint n_children;
6455
6456       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6457                                                    NULL);
6458
6459       if (n_children == 0)
6460         icon_view->priv->empty_view_drop = TRUE;
6461     }
6462
6463   icon_view->priv->dest_pos = pos;
6464
6465   if (path)
6466     {
6467       icon_view->priv->dest_item =
6468         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
6469                                           icon_view->priv->model, path);
6470       
6471       gtk_icon_view_queue_draw_path (icon_view, path);
6472     }
6473 }
6474
6475 /**
6476  * gtk_icon_view_get_drag_dest_item:
6477  * @icon_view: a #GtkIconView
6478  * @path: Return location for the path of the highlighted item, or %NULL.
6479  * @pos: Return location for the drop position, or %NULL
6480  * 
6481  * Gets information about the item that is highlighted for feedback.
6482  *
6483  * Since: 2.8
6484  **/
6485 void
6486 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
6487                                   GtkTreePath             **path,
6488                                   GtkIconViewDropPosition  *pos)
6489 {
6490   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6491
6492   if (path)
6493     {
6494       if (icon_view->priv->dest_item)
6495         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6496       else
6497         *path = NULL;
6498     }
6499
6500   if (pos)
6501     *pos = icon_view->priv->dest_pos;
6502 }
6503
6504 /**
6505  * gtk_icon_view_get_dest_item_at_pos:
6506  * @icon_view: a #GtkIconView
6507  * @drag_x: the position to determine the destination item for
6508  * @drag_y: the position to determine the destination item for
6509  * @path: Return location for the path of the highlighted item, or %NULL.
6510  * @pos: Return location for the drop position, or %NULL
6511  * 
6512  * Determines the destination item for a given position.
6513  * 
6514  * Return value: whether there is an item at the given position.
6515  *
6516  * Since: 2.8
6517  **/
6518 gboolean
6519 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
6520                                     gint                      drag_x,
6521                                     gint                      drag_y,
6522                                     GtkTreePath             **path,
6523                                     GtkIconViewDropPosition  *pos)
6524 {
6525   GtkIconViewItem *item;
6526
6527   /* Note; this function is exported to allow a custom DND
6528    * implementation, so it can't touch TreeViewDragInfo
6529    */
6530
6531   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6532   g_return_val_if_fail (drag_x >= 0, FALSE);
6533   g_return_val_if_fail (drag_y >= 0, FALSE);
6534   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6535
6536
6537   if (path)
6538     *path = NULL;
6539
6540   item = gtk_icon_view_get_item_at_coords (icon_view, 
6541                                            drag_x + icon_view->priv->hadjustment->value, 
6542                                            drag_y + icon_view->priv->vadjustment->value,
6543                                            FALSE, NULL);
6544
6545   if (item == NULL)
6546     return FALSE;
6547
6548   if (path)
6549     *path = gtk_tree_path_new_from_indices (item->index, -1);
6550
6551   if (pos)
6552     {
6553       if (drag_x < item->x + item->width / 4)
6554         *pos = GTK_ICON_VIEW_DROP_LEFT;
6555       else if (drag_x > item->x + item->width * 3 / 4)
6556         *pos = GTK_ICON_VIEW_DROP_RIGHT;
6557       else if (drag_y < item->y + item->height / 4)
6558         *pos = GTK_ICON_VIEW_DROP_ABOVE;
6559       else if (drag_y > item->y + item->height * 3 / 4)
6560         *pos = GTK_ICON_VIEW_DROP_BELOW;
6561       else
6562         *pos = GTK_ICON_VIEW_DROP_INTO;
6563     }
6564
6565   return TRUE;
6566 }
6567
6568 /**
6569  * gtk_icon_view_create_drag_icon:
6570  * @icon_view: a #GtkIconView
6571  * @path: a #GtkTreePath in @icon_view
6572  *
6573  * Creates a #GdkPixmap representation of the item at @path.  
6574  * This image is used for a drag icon.
6575  *
6576  * Return value: a newly-allocated pixmap of the drag icon.
6577  * 
6578  * Since: 2.8
6579  **/
6580 GdkPixmap *
6581 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6582                                 GtkTreePath *path)
6583 {
6584   GtkWidget *widget;
6585   cairo_t *cr;
6586   GdkPixmap *drawable;
6587   GList *l;
6588   gint index;
6589   GdkRectangle area;
6590
6591   widget = GTK_WIDGET (icon_view);
6592
6593   index = gtk_tree_path_get_indices (path)[0];
6594
6595   for (l = icon_view->priv->items; l; l = l->next) 
6596     {
6597       GtkIconViewItem *item = l->data;
6598       
6599       if (index == item->index)
6600         {
6601           drawable = gdk_pixmap_new (icon_view->priv->bin_window,
6602                                      item->width + 2,
6603                                      item->height + 2,
6604                                      -1);
6605
6606           cr = gdk_cairo_create (drawable);
6607           cairo_set_line_width (cr, 1.);
6608
6609           gdk_cairo_set_source_color
6610             (cr, &widget->style->base[GTK_WIDGET_STATE (widget)]);
6611           cairo_rectangle (cr, 0, 0, item->width + 2, item->height + 2);
6612           cairo_fill (cr);
6613
6614           area.x = 0;
6615           area.y = 0;
6616           area.width = item->width;
6617           area.height = item->height;
6618
6619           gtk_icon_view_paint_item (icon_view, cr, item, &area, 
6620                                     drawable, 1, 1, FALSE); 
6621
6622           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
6623           cairo_rectangle (cr, 0.5, 0.5, item->width + 1, item->height + 1);
6624           cairo_stroke (cr);
6625
6626           cairo_destroy (cr);
6627
6628           return drawable;
6629         }
6630     }
6631   
6632   return NULL;
6633 }
6634
6635 /**
6636  * gtk_icon_view_get_reorderable:
6637  * @icon_view: a #GtkIconView
6638  *
6639  * Retrieves whether the user can reorder the list via drag-and-drop. 
6640  * See gtk_icon_view_set_reorderable().
6641  *
6642  * Return value: %TRUE if the list can be reordered.
6643  *
6644  * Since: 2.8
6645  **/
6646 gboolean
6647 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
6648 {
6649   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6650
6651   return icon_view->priv->reorderable;
6652 }
6653
6654 static const GtkTargetEntry item_targets[] = {
6655   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
6656 };
6657
6658
6659 /**
6660  * gtk_icon_view_set_reorderable:
6661  * @icon_view: A #GtkIconView.
6662  * @reorderable: %TRUE, if the list of items can be reordered.
6663  *
6664  * This function is a convenience function to allow you to reorder models that
6665  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
6666  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
6667  * the user can reorder the model by dragging and dropping rows.  The
6668  * developer can listen to these changes by connecting to the model's
6669  * row_inserted and row_deleted signals.
6670  *
6671  * This function does not give you any degree of control over the order -- any
6672  * reordering is allowed.  If more control is needed, you should probably
6673  * handle drag and drop manually.
6674  *
6675  * Since: 2.8
6676  **/
6677 void
6678 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
6679                                gboolean     reorderable)
6680 {
6681   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6682
6683   reorderable = reorderable != FALSE;
6684
6685   if (icon_view->priv->reorderable == reorderable)
6686     return;
6687
6688   if (reorderable)
6689     {
6690       gtk_icon_view_enable_model_drag_source (icon_view,
6691                                               GDK_BUTTON1_MASK,
6692                                               item_targets,
6693                                               G_N_ELEMENTS (item_targets),
6694                                               GDK_ACTION_MOVE);
6695       gtk_icon_view_enable_model_drag_dest (icon_view,
6696                                             item_targets,
6697                                             G_N_ELEMENTS (item_targets),
6698                                             GDK_ACTION_MOVE);
6699     }
6700   else
6701     {
6702       gtk_icon_view_unset_model_drag_source (icon_view);
6703       gtk_icon_view_unset_model_drag_dest (icon_view);
6704     }
6705
6706   icon_view->priv->reorderable = reorderable;
6707
6708   g_object_notify (G_OBJECT (icon_view), "reorderable");
6709 }
6710
6711
6712 /* Accessibility Support */
6713
6714 static gpointer accessible_parent_class;
6715 static gpointer accessible_item_parent_class;
6716 static GQuark accessible_private_data_quark = 0;
6717
6718 #define GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE      (gtk_icon_view_item_accessible_get_type ())
6719 #define GTK_ICON_VIEW_ITEM_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE, GtkIconViewItemAccessible))
6720 #define GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE))
6721
6722 static GType gtk_icon_view_item_accessible_get_type (void);
6723
6724 enum {
6725     ACTION_ACTIVATE,
6726     LAST_ACTION
6727 };
6728
6729 typedef struct
6730 {
6731   AtkObject parent;
6732
6733   GtkIconViewItem *item;
6734
6735   GtkWidget *widget;
6736
6737   AtkStateSet *state_set;
6738
6739   gchar *text;
6740
6741   GtkTextBuffer *text_buffer;
6742
6743   gchar *action_descriptions[LAST_ACTION];
6744   gchar *image_description;
6745   guint action_idle_handler;
6746 } GtkIconViewItemAccessible;
6747
6748 static const gchar *const gtk_icon_view_item_accessible_action_names[] = 
6749 {
6750   "activate",
6751   NULL
6752 };
6753
6754 static const gchar *const gtk_icon_view_item_accessible_action_descriptions[] =
6755 {
6756   "Activate item",
6757   NULL
6758 };
6759 typedef struct _GtkIconViewItemAccessibleClass
6760 {
6761   AtkObjectClass parent_class;
6762
6763 } GtkIconViewItemAccessibleClass;
6764
6765 static gboolean gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item);
6766
6767 static gboolean
6768 gtk_icon_view_item_accessible_idle_do_action (gpointer data)
6769 {
6770   GtkIconViewItemAccessible *item;
6771   GtkIconView *icon_view;
6772   GtkTreePath *path;
6773
6774   GDK_THREADS_ENTER ();
6775
6776   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (data);
6777   item->action_idle_handler = 0;
6778
6779   if (item->widget != NULL)
6780     {
6781       icon_view = GTK_ICON_VIEW (item->widget);
6782       path = gtk_tree_path_new_from_indices (item->item->index, -1);
6783       gtk_icon_view_item_activated (icon_view, path);
6784       gtk_tree_path_free (path);
6785     }
6786
6787   GDK_THREADS_LEAVE ();
6788
6789   return FALSE;
6790 }
6791
6792 static gboolean
6793 gtk_icon_view_item_accessible_action_do_action (AtkAction *action,
6794                                                 gint       i)
6795 {
6796   GtkIconViewItemAccessible *item;
6797   GtkIconView *icon_view;
6798
6799   if (i < 0 || i >= LAST_ACTION) 
6800     return FALSE;
6801
6802   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6803
6804   if (!GTK_IS_ICON_VIEW (item->widget))
6805     return FALSE;
6806
6807   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6808     return FALSE;
6809
6810   icon_view = GTK_ICON_VIEW (item->widget);
6811
6812   switch (i)
6813     {
6814     case ACTION_ACTIVATE:
6815       if (!item->action_idle_handler)
6816         item->action_idle_handler = g_idle_add (gtk_icon_view_item_accessible_idle_do_action, item);
6817       break;
6818     default:
6819       g_assert_not_reached ();
6820       return FALSE;
6821
6822     }        
6823   return TRUE;
6824 }
6825
6826 static gint
6827 gtk_icon_view_item_accessible_action_get_n_actions (AtkAction *action)
6828 {
6829         return LAST_ACTION;
6830 }
6831
6832 static const gchar *
6833 gtk_icon_view_item_accessible_action_get_description (AtkAction *action,
6834                                                       gint       i)
6835 {
6836   GtkIconViewItemAccessible *item;
6837
6838   if (i < 0 || i >= LAST_ACTION) 
6839     return NULL;
6840
6841   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6842
6843   if (item->action_descriptions[i])
6844     return item->action_descriptions[i];
6845   else
6846     return gtk_icon_view_item_accessible_action_descriptions[i];
6847 }
6848
6849 static const gchar *
6850 gtk_icon_view_item_accessible_action_get_name (AtkAction *action,
6851                                                gint       i)
6852 {
6853   if (i < 0 || i >= LAST_ACTION) 
6854     return NULL;
6855
6856   return gtk_icon_view_item_accessible_action_names[i];
6857 }
6858
6859 static gboolean
6860 gtk_icon_view_item_accessible_action_set_description (AtkAction   *action,
6861                                                       gint         i,
6862                                                       const gchar *description)
6863 {
6864   GtkIconViewItemAccessible *item;
6865
6866   if (i < 0 || i >= LAST_ACTION) 
6867     return FALSE;
6868
6869   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6870
6871   if (item->action_descriptions[i])
6872     g_free (item->action_descriptions[i]);
6873
6874   item->action_descriptions[i] = g_strdup (description);
6875
6876   return TRUE;
6877 }
6878
6879 static void
6880 atk_action_item_interface_init (AtkActionIface *iface)
6881 {
6882   iface->do_action = gtk_icon_view_item_accessible_action_do_action;
6883   iface->get_n_actions = gtk_icon_view_item_accessible_action_get_n_actions;
6884   iface->get_description = gtk_icon_view_item_accessible_action_get_description;
6885   iface->get_name = gtk_icon_view_item_accessible_action_get_name;
6886   iface->set_description = gtk_icon_view_item_accessible_action_set_description;
6887 }
6888
6889 static const gchar *
6890 gtk_icon_view_item_accessible_image_get_image_description (AtkImage *image)
6891 {
6892   GtkIconViewItemAccessible *item;
6893
6894   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6895
6896   return item->image_description;
6897 }
6898
6899 static gboolean
6900 gtk_icon_view_item_accessible_image_set_image_description (AtkImage    *image,
6901                                                            const gchar *description)
6902 {
6903   GtkIconViewItemAccessible *item;
6904
6905   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6906
6907   g_free (item->image_description);
6908   item->image_description = g_strdup (item->image_description);
6909
6910   return TRUE;
6911 }
6912
6913 static gboolean
6914 get_pixbuf_box (GtkIconView     *icon_view,
6915                 GtkIconViewItem *item,
6916                 GdkRectangle    *box)
6917 {
6918   GList *l;
6919
6920   for (l = icon_view->priv->cell_list; l; l = l->next)
6921     {
6922       GtkIconViewCellInfo *info = l->data;
6923       
6924       if (GTK_IS_CELL_RENDERER_PIXBUF (info->cell))
6925         {
6926           gtk_icon_view_get_cell_box (icon_view, item, info, box);
6927
6928           return TRUE;
6929         }
6930     }
6931
6932   return FALSE;
6933 }
6934
6935 static gchar *
6936 get_text (GtkIconView     *icon_view,
6937           GtkIconViewItem *item)
6938 {
6939   GList *l;
6940   gchar *text;
6941
6942   for (l = icon_view->priv->cell_list; l; l = l->next)
6943     {
6944       GtkIconViewCellInfo *info = l->data;
6945       
6946       if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
6947         {
6948           g_object_get (info->cell, "text", &text, NULL);
6949           
6950           return text;
6951         }
6952     }
6953
6954   return NULL;
6955 }
6956
6957 static void
6958 gtk_icon_view_item_accessible_image_get_image_size (AtkImage *image,
6959                                                     gint     *width,
6960                                                     gint     *height)
6961 {
6962   GtkIconViewItemAccessible *item;
6963   GdkRectangle box;
6964
6965   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6966
6967   if (!GTK_IS_ICON_VIEW (item->widget))
6968     return;
6969
6970   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6971     return;
6972
6973   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
6974     {
6975       *width = box.width;
6976       *height = box.height;  
6977     }
6978 }
6979
6980 static void
6981 gtk_icon_view_item_accessible_image_get_image_position (AtkImage    *image,
6982                                                         gint        *x,
6983                                                         gint        *y,
6984                                                         AtkCoordType coord_type)
6985 {
6986   GtkIconViewItemAccessible *item;
6987   GdkRectangle box;
6988
6989   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6990
6991   if (!GTK_IS_ICON_VIEW (item->widget))
6992     return;
6993
6994   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6995     return;
6996
6997   atk_component_get_position (ATK_COMPONENT (image), x, y, coord_type);
6998
6999   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
7000     {
7001       *x+= box.x - item->item->x;
7002       *y+= box.y - item->item->y;
7003     }
7004
7005 }
7006
7007 static void
7008 atk_image_item_interface_init (AtkImageIface *iface)
7009 {
7010   iface->get_image_description = gtk_icon_view_item_accessible_image_get_image_description;
7011   iface->set_image_description = gtk_icon_view_item_accessible_image_set_image_description;
7012   iface->get_image_size = gtk_icon_view_item_accessible_image_get_image_size;
7013   iface->get_image_position = gtk_icon_view_item_accessible_image_get_image_position;
7014 }
7015
7016 static gchar *
7017 gtk_icon_view_item_accessible_text_get_text (AtkText *text,
7018                                              gint     start_pos,
7019                                              gint     end_pos)
7020 {
7021   GtkIconViewItemAccessible *item;
7022   GtkTextIter start, end;
7023   GtkTextBuffer *buffer;
7024
7025   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7026
7027   if (!GTK_IS_ICON_VIEW (item->widget))
7028     return NULL;
7029
7030   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7031     return NULL;
7032
7033   buffer = item->text_buffer;
7034   gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
7035   if (end_pos < 0)
7036     gtk_text_buffer_get_end_iter (buffer, &end);
7037   else
7038     gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
7039
7040   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7041 }
7042
7043 static gunichar
7044 gtk_icon_view_item_accessible_text_get_character_at_offset (AtkText *text,
7045                                                             gint     offset)
7046 {
7047   GtkIconViewItemAccessible *item;
7048   GtkTextIter start, end;
7049   GtkTextBuffer *buffer;
7050   gchar *string;
7051   gunichar unichar;
7052
7053   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7054
7055   if (!GTK_IS_ICON_VIEW (item->widget))
7056     return '\0';
7057
7058   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7059     return '\0';
7060
7061   buffer = item->text_buffer;
7062   if (offset >= gtk_text_buffer_get_char_count (buffer))
7063     return '\0';
7064
7065   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7066   end = start;
7067   gtk_text_iter_forward_char (&end);
7068   string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
7069   unichar = g_utf8_get_char (string);
7070   g_free(string);
7071
7072   return unichar;
7073 }
7074
7075 static void
7076 get_pango_text_offsets (PangoLayout     *layout,
7077                         GtkTextBuffer   *buffer,
7078                         gint             function,
7079                         AtkTextBoundary  boundary_type,
7080                         gint             offset,
7081                         gint            *start_offset,
7082                         gint            *end_offset,
7083                         GtkTextIter     *start_iter,
7084                         GtkTextIter     *end_iter)
7085 {
7086   PangoLayoutIter *iter;
7087   PangoLayoutLine *line, *prev_line = NULL, *prev_prev_line = NULL;
7088   gint index, start_index, end_index;
7089   const gchar *text;
7090   gboolean found = FALSE;
7091
7092   text = pango_layout_get_text (layout);
7093   index = g_utf8_offset_to_pointer (text, offset) - text;
7094   iter = pango_layout_get_iter (layout);
7095   do
7096     {
7097       line = pango_layout_iter_get_line (iter);
7098       start_index = line->start_index;
7099       end_index = start_index + line->length;
7100
7101       if (index >= start_index && index <= end_index)
7102         {
7103           /*
7104            * Found line for offset
7105            */
7106           switch (function)
7107             {
7108             case 0:
7109                   /*
7110                    * We want the previous line
7111                    */
7112               if (prev_line)
7113                 {
7114                   switch (boundary_type)
7115                     {
7116                     case ATK_TEXT_BOUNDARY_LINE_START:
7117                       end_index = start_index;
7118                       start_index = prev_line->start_index;
7119                       break;
7120                     case ATK_TEXT_BOUNDARY_LINE_END:
7121                       if (prev_prev_line)
7122                         start_index = prev_prev_line->start_index + 
7123                                   prev_prev_line->length;
7124                       end_index = prev_line->start_index + prev_line->length;
7125                       break;
7126                     default:
7127                       g_assert_not_reached();
7128                     }
7129                 }
7130               else
7131                 start_index = end_index = 0;
7132               break;
7133             case 1:
7134               switch (boundary_type)
7135                 {
7136                 case ATK_TEXT_BOUNDARY_LINE_START:
7137                   if (pango_layout_iter_next_line (iter))
7138                     end_index = pango_layout_iter_get_line (iter)->start_index;
7139                   break;
7140                 case ATK_TEXT_BOUNDARY_LINE_END:
7141                   if (prev_line)
7142                     start_index = prev_line->start_index + 
7143                                   prev_line->length;
7144                   break;
7145                 default:
7146                   g_assert_not_reached();
7147                 }
7148               break;
7149             case 2:
7150                /*
7151                 * We want the next line
7152                 */
7153               if (pango_layout_iter_next_line (iter))
7154                 {
7155                   line = pango_layout_iter_get_line (iter);
7156                   switch (boundary_type)
7157                     {
7158                     case ATK_TEXT_BOUNDARY_LINE_START:
7159                       start_index = line->start_index;
7160                       if (pango_layout_iter_next_line (iter))
7161                         end_index = pango_layout_iter_get_line (iter)->start_index;
7162                       else
7163                         end_index = start_index + line->length;
7164                       break;
7165                     case ATK_TEXT_BOUNDARY_LINE_END:
7166                       start_index = end_index;
7167                       end_index = line->start_index + line->length;
7168                       break;
7169                     default:
7170                       g_assert_not_reached();
7171                     }
7172                 }
7173               else
7174                 start_index = end_index;
7175               break;
7176             }
7177           found = TRUE;
7178           break;
7179         }
7180       prev_prev_line = prev_line; 
7181       prev_line = line; 
7182     }
7183   while (pango_layout_iter_next_line (iter));
7184
7185   if (!found)
7186     {
7187       start_index = prev_line->start_index + prev_line->length;
7188       end_index = start_index;
7189     }
7190   pango_layout_iter_free (iter);
7191   *start_offset = g_utf8_pointer_to_offset (text, text + start_index);
7192   *end_offset = g_utf8_pointer_to_offset (text, text + end_index);
7193  
7194   gtk_text_buffer_get_iter_at_offset (buffer, start_iter, *start_offset);
7195   gtk_text_buffer_get_iter_at_offset (buffer, end_iter, *end_offset);
7196 }
7197
7198 static gchar*
7199 gtk_icon_view_item_accessible_text_get_text_before_offset (AtkText         *text,
7200                                                            gint            offset,
7201                                                            AtkTextBoundary boundary_type,
7202                                                            gint            *start_offset,
7203                                                            gint            *end_offset)
7204 {
7205   GtkIconViewItemAccessible *item;
7206   GtkTextIter start, end;
7207   GtkTextBuffer *buffer;
7208   GtkIconView *icon_view;
7209
7210   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7211
7212   if (!GTK_IS_ICON_VIEW (item->widget))
7213     return NULL;
7214
7215   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7216     return NULL;
7217
7218   buffer = item->text_buffer;
7219
7220   if (!gtk_text_buffer_get_char_count (buffer))
7221     {
7222       *start_offset = 0;
7223       *end_offset = 0;
7224       return g_strdup ("");
7225     }
7226   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7227    
7228   end = start;
7229
7230   switch (boundary_type)
7231     {
7232     case ATK_TEXT_BOUNDARY_CHAR:
7233       gtk_text_iter_backward_char(&start);
7234       break;
7235     case ATK_TEXT_BOUNDARY_WORD_START:
7236       if (!gtk_text_iter_starts_word (&start))
7237         gtk_text_iter_backward_word_start (&start);
7238       end = start;
7239       gtk_text_iter_backward_word_start(&start);
7240       break;
7241     case ATK_TEXT_BOUNDARY_WORD_END:
7242       if (gtk_text_iter_inside_word (&start) &&
7243           !gtk_text_iter_starts_word (&start))
7244         gtk_text_iter_backward_word_start (&start);
7245       while (!gtk_text_iter_ends_word (&start))
7246         {
7247           if (!gtk_text_iter_backward_char (&start))
7248             break;
7249         }
7250       end = start;
7251       gtk_text_iter_backward_word_start(&start);
7252       while (!gtk_text_iter_ends_word (&start))
7253         {
7254           if (!gtk_text_iter_backward_char (&start))
7255             break;
7256         }
7257       break;
7258     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7259       if (!gtk_text_iter_starts_sentence (&start))
7260         gtk_text_iter_backward_sentence_start (&start);
7261       end = start;
7262       gtk_text_iter_backward_sentence_start (&start);
7263       break;
7264     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7265       if (gtk_text_iter_inside_sentence (&start) &&
7266           !gtk_text_iter_starts_sentence (&start))
7267         gtk_text_iter_backward_sentence_start (&start);
7268       while (!gtk_text_iter_ends_sentence (&start))
7269         {
7270           if (!gtk_text_iter_backward_char (&start))
7271             break;
7272         }
7273       end = start;
7274       gtk_text_iter_backward_sentence_start (&start);
7275       while (!gtk_text_iter_ends_sentence (&start))
7276         {
7277           if (!gtk_text_iter_backward_char (&start))
7278             break;
7279         }
7280       break;
7281    case ATK_TEXT_BOUNDARY_LINE_START:
7282    case ATK_TEXT_BOUNDARY_LINE_END:
7283       icon_view = GTK_ICON_VIEW (item->widget);
7284 #if 0
7285       /* FIXME we probably have to use GailTextCell to salvage this */
7286       gtk_icon_view_update_item_text (icon_view, item->item);
7287       get_pango_text_offsets (icon_view->priv->layout,
7288                               buffer,
7289                               0,
7290                               boundary_type,
7291                               offset,
7292                               start_offset,
7293                               end_offset,
7294                               &start,
7295                               &end);
7296 #endif
7297       break;
7298     }
7299
7300   *start_offset = gtk_text_iter_get_offset (&start);
7301   *end_offset = gtk_text_iter_get_offset (&end);
7302
7303   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7304 }
7305
7306 static gchar*
7307 gtk_icon_view_item_accessible_text_get_text_at_offset (AtkText         *text,
7308                                                        gint            offset,
7309                                                        AtkTextBoundary boundary_type,
7310                                                        gint            *start_offset,
7311                                                        gint            *end_offset)
7312 {
7313   GtkIconViewItemAccessible *item;
7314   GtkTextIter start, end;
7315   GtkTextBuffer *buffer;
7316   GtkIconView *icon_view;
7317
7318   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7319
7320   if (!GTK_IS_ICON_VIEW (item->widget))
7321     return NULL;
7322
7323   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7324     return NULL;
7325
7326   buffer = item->text_buffer;
7327
7328   if (!gtk_text_buffer_get_char_count (buffer))
7329     {
7330       *start_offset = 0;
7331       *end_offset = 0;
7332       return g_strdup ("");
7333     }
7334   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7335    
7336   end = start;
7337
7338   switch (boundary_type)
7339     {
7340     case ATK_TEXT_BOUNDARY_CHAR:
7341       gtk_text_iter_forward_char (&end);
7342       break;
7343     case ATK_TEXT_BOUNDARY_WORD_START:
7344       if (!gtk_text_iter_starts_word (&start))
7345         gtk_text_iter_backward_word_start (&start);
7346       if (gtk_text_iter_inside_word (&end))
7347         gtk_text_iter_forward_word_end (&end);
7348       while (!gtk_text_iter_starts_word (&end))
7349         {
7350           if (!gtk_text_iter_forward_char (&end))
7351             break;
7352         }
7353       break;
7354     case ATK_TEXT_BOUNDARY_WORD_END:
7355       if (gtk_text_iter_inside_word (&start) &&
7356           !gtk_text_iter_starts_word (&start))
7357         gtk_text_iter_backward_word_start (&start);
7358       while (!gtk_text_iter_ends_word (&start))
7359         {
7360           if (!gtk_text_iter_backward_char (&start))
7361             break;
7362         }
7363       gtk_text_iter_forward_word_end (&end);
7364       break;
7365     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7366       if (!gtk_text_iter_starts_sentence (&start))
7367         gtk_text_iter_backward_sentence_start (&start);
7368       if (gtk_text_iter_inside_sentence (&end))
7369         gtk_text_iter_forward_sentence_end (&end);
7370       while (!gtk_text_iter_starts_sentence (&end))
7371         {
7372           if (!gtk_text_iter_forward_char (&end))
7373             break;
7374         }
7375       break;
7376     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7377       if (gtk_text_iter_inside_sentence (&start) &&
7378           !gtk_text_iter_starts_sentence (&start))
7379         gtk_text_iter_backward_sentence_start (&start);
7380       while (!gtk_text_iter_ends_sentence (&start))
7381         {
7382           if (!gtk_text_iter_backward_char (&start))
7383             break;
7384         }
7385       gtk_text_iter_forward_sentence_end (&end);
7386       break;
7387    case ATK_TEXT_BOUNDARY_LINE_START:
7388    case ATK_TEXT_BOUNDARY_LINE_END:
7389       icon_view = GTK_ICON_VIEW (item->widget);
7390 #if 0
7391       /* FIXME we probably have to use GailTextCell to salvage this */
7392       gtk_icon_view_update_item_text (icon_view, item->item);
7393       get_pango_text_offsets (icon_view->priv->layout,
7394                               buffer,
7395                               1,
7396                               boundary_type,
7397                               offset,
7398                               start_offset,
7399                               end_offset,
7400                               &start,
7401                               &end);
7402 #endif
7403       break;
7404     }
7405
7406
7407   *start_offset = gtk_text_iter_get_offset (&start);
7408   *end_offset = gtk_text_iter_get_offset (&end);
7409
7410   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7411 }
7412
7413 static gchar*
7414 gtk_icon_view_item_accessible_text_get_text_after_offset (AtkText         *text,
7415                                                           gint            offset,
7416                                                           AtkTextBoundary boundary_type,
7417                                                           gint            *start_offset,
7418                                                           gint            *end_offset)
7419 {
7420   GtkIconViewItemAccessible *item;
7421   GtkTextIter start, end;
7422   GtkTextBuffer *buffer;
7423   GtkIconView *icon_view;
7424
7425   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7426
7427   if (!GTK_IS_ICON_VIEW (item->widget))
7428     return NULL;
7429
7430   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7431     return NULL;
7432
7433   buffer = item->text_buffer;
7434
7435   if (!gtk_text_buffer_get_char_count (buffer))
7436     {
7437       *start_offset = 0;
7438       *end_offset = 0;
7439       return g_strdup ("");
7440     }
7441   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7442    
7443   end = start;
7444
7445   switch (boundary_type)
7446     {
7447     case ATK_TEXT_BOUNDARY_CHAR:
7448       gtk_text_iter_forward_char(&start);
7449       gtk_text_iter_forward_chars(&end, 2);
7450       break;
7451     case ATK_TEXT_BOUNDARY_WORD_START:
7452       if (gtk_text_iter_inside_word (&end))
7453         gtk_text_iter_forward_word_end (&end);
7454       while (!gtk_text_iter_starts_word (&end))
7455         {
7456           if (!gtk_text_iter_forward_char (&end))
7457             break;
7458         }
7459       start = end;
7460       if (!gtk_text_iter_is_end (&end))
7461         {
7462           gtk_text_iter_forward_word_end (&end);
7463           while (!gtk_text_iter_starts_word (&end))
7464             {
7465               if (!gtk_text_iter_forward_char (&end))
7466                 break;
7467             }
7468         }
7469       break;
7470     case ATK_TEXT_BOUNDARY_WORD_END:
7471       gtk_text_iter_forward_word_end (&end);
7472       start = end;
7473       if (!gtk_text_iter_is_end (&end))
7474         gtk_text_iter_forward_word_end (&end);
7475       break;
7476     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7477       if (gtk_text_iter_inside_sentence (&end))
7478         gtk_text_iter_forward_sentence_end (&end);
7479       while (!gtk_text_iter_starts_sentence (&end))
7480         {
7481           if (!gtk_text_iter_forward_char (&end))
7482             break;
7483         }
7484       start = end;
7485       if (!gtk_text_iter_is_end (&end))
7486         {
7487           gtk_text_iter_forward_sentence_end (&end);
7488           while (!gtk_text_iter_starts_sentence (&end))
7489             {
7490               if (!gtk_text_iter_forward_char (&end))
7491                 break;
7492             }
7493         }
7494       break;
7495     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7496       gtk_text_iter_forward_sentence_end (&end);
7497       start = end;
7498       if (!gtk_text_iter_is_end (&end))
7499         gtk_text_iter_forward_sentence_end (&end);
7500       break;
7501    case ATK_TEXT_BOUNDARY_LINE_START:
7502    case ATK_TEXT_BOUNDARY_LINE_END:
7503       icon_view = GTK_ICON_VIEW (item->widget);
7504 #if 0
7505       /* FIXME we probably have to use GailTextCell to salvage this */
7506       gtk_icon_view_update_item_text (icon_view, item->item);
7507       get_pango_text_offsets (icon_view->priv->layout,
7508                               buffer,
7509                               2,
7510                               boundary_type,
7511                               offset,
7512                               start_offset,
7513                               end_offset,
7514                               &start,
7515                               &end);
7516 #endif
7517       break;
7518     }
7519   *start_offset = gtk_text_iter_get_offset (&start);
7520   *end_offset = gtk_text_iter_get_offset (&end);
7521
7522   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7523 }
7524
7525 static gint
7526 gtk_icon_view_item_accessible_text_get_character_count (AtkText *text)
7527 {
7528   GtkIconViewItemAccessible *item;
7529
7530   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7531
7532   if (!GTK_IS_ICON_VIEW (item->widget))
7533     return 0;
7534
7535   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7536     return 0;
7537
7538   return gtk_text_buffer_get_char_count (item->text_buffer);
7539 }
7540
7541 static void
7542 gtk_icon_view_item_accessible_text_get_character_extents (AtkText      *text,
7543                                                           gint         offset,
7544                                                           gint         *x,
7545                                                           gint         *y,
7546                                                           gint         *width,
7547                                                           gint         *height,
7548                                                           AtkCoordType coord_type)
7549 {
7550   GtkIconViewItemAccessible *item;
7551   GtkIconView *icon_view;
7552   PangoRectangle char_rect;
7553   const gchar *item_text;
7554   gint index;
7555
7556   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7557
7558   if (!GTK_IS_ICON_VIEW (item->widget))
7559     return;
7560
7561   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7562     return;
7563
7564   icon_view = GTK_ICON_VIEW (item->widget);
7565 #if 0
7566       /* FIXME we probably have to use GailTextCell to salvage this */
7567   gtk_icon_view_update_item_text (icon_view, item->item);
7568   item_text = pango_layout_get_text (icon_view->priv->layout);
7569   index = g_utf8_offset_to_pointer (item_text, offset) - item_text;
7570   pango_layout_index_to_pos (icon_view->priv->layout, index, &char_rect);
7571
7572   atk_component_get_position (ATK_COMPONENT (text), x, y, coord_type);
7573   *x += item->item->layout_x - item->item->x + char_rect.x / PANGO_SCALE;
7574   /* Look at gtk_icon_view_paint_item() to see where the text is. */
7575   *x -=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
7576   *y += item->item->layout_y - item->item->y + char_rect.y / PANGO_SCALE;
7577   *width = char_rect.width / PANGO_SCALE;
7578   *height = char_rect.height / PANGO_SCALE;
7579 #endif
7580 }
7581
7582 static gint
7583 gtk_icon_view_item_accessible_text_get_offset_at_point (AtkText      *text,
7584                                                         gint          x,
7585                                                         gint          y,
7586                                                         AtkCoordType coord_type)
7587 {
7588   GtkIconViewItemAccessible *item;
7589   GtkIconView *icon_view;
7590   const gchar *item_text;
7591   gint index;
7592   gint offset;
7593   gint l_x, l_y;
7594
7595   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7596
7597   if (!GTK_IS_ICON_VIEW (item->widget))
7598     return -1;
7599
7600   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7601     return -1;
7602
7603   icon_view = GTK_ICON_VIEW (item->widget);
7604 #if 0
7605       /* FIXME we probably have to use GailTextCell to salvage this */
7606   gtk_icon_view_update_item_text (icon_view, item->item);
7607   atk_component_get_position (ATK_COMPONENT (text), &l_x, &l_y, coord_type);
7608   x -= l_x + item->item->layout_x - item->item->x;
7609   x +=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
7610   y -= l_y + item->item->layout_y - item->item->y;
7611   item_text = pango_layout_get_text (icon_view->priv->layout);
7612   if (!pango_layout_xy_to_index (icon_view->priv->layout, 
7613                                 x * PANGO_SCALE,
7614                                 y * PANGO_SCALE,
7615                                 &index, NULL))
7616     {
7617       if (x < 0 || y < 0)
7618         index = 0;
7619       else
7620         index = -1;
7621     } 
7622   if (index == -1)
7623     offset = g_utf8_strlen (item_text, -1);
7624   else
7625     offset = g_utf8_pointer_to_offset (item_text, item_text + index);
7626 #endif
7627   return offset;
7628 }
7629
7630 static void
7631 atk_text_item_interface_init (AtkTextIface *iface)
7632 {
7633   iface->get_text = gtk_icon_view_item_accessible_text_get_text;
7634   iface->get_character_at_offset = gtk_icon_view_item_accessible_text_get_character_at_offset;
7635   iface->get_text_before_offset = gtk_icon_view_item_accessible_text_get_text_before_offset;
7636   iface->get_text_at_offset = gtk_icon_view_item_accessible_text_get_text_at_offset;
7637   iface->get_text_after_offset = gtk_icon_view_item_accessible_text_get_text_after_offset;
7638   iface->get_character_count = gtk_icon_view_item_accessible_text_get_character_count;
7639   iface->get_character_extents = gtk_icon_view_item_accessible_text_get_character_extents;
7640   iface->get_offset_at_point = gtk_icon_view_item_accessible_text_get_offset_at_point;
7641 }
7642
7643 static void
7644 gtk_icon_view_item_accessible_get_extents (AtkComponent *component,
7645                                            gint         *x,
7646                                            gint         *y,
7647                                            gint         *width,
7648                                            gint         *height,
7649                                            AtkCoordType  coord_type)
7650 {
7651   GtkIconViewItemAccessible *item;
7652   AtkObject *parent_obj;
7653   gint l_x, l_y;
7654
7655   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component));
7656
7657   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
7658   if (!GTK_IS_WIDGET (item->widget))
7659     return;
7660
7661   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7662     return;
7663
7664   *width = item->item->width;
7665   *height = item->item->height;
7666   if (gtk_icon_view_item_accessible_is_showing (item))
7667     {
7668       parent_obj = gtk_widget_get_accessible (item->widget);
7669       atk_component_get_position (ATK_COMPONENT (parent_obj), &l_x, &l_y, coord_type);
7670       *x = l_x + item->item->x;
7671       *y = l_y + item->item->y;
7672     }
7673   else
7674     {
7675       *x = G_MININT;
7676       *y = G_MININT;
7677     }
7678 }
7679
7680 static gboolean
7681 gtk_icon_view_item_accessible_grab_focus (AtkComponent *component)
7682 {
7683   GtkIconViewItemAccessible *item;
7684   GtkWidget *toplevel;
7685
7686   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component), FALSE);
7687
7688   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
7689   if (!GTK_IS_WIDGET (item->widget))
7690     return FALSE;
7691
7692   gtk_widget_grab_focus (item->widget);
7693   gtk_icon_view_set_cursor_item (GTK_ICON_VIEW (item->widget), item->item, -1);
7694   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item->widget));
7695   if (GTK_WIDGET_TOPLEVEL (toplevel))
7696     gtk_window_present (GTK_WINDOW (toplevel));
7697
7698   return TRUE;
7699 }
7700
7701 static void
7702 atk_component_item_interface_init (AtkComponentIface *iface)
7703 {
7704   iface->get_extents = gtk_icon_view_item_accessible_get_extents;
7705   iface->grab_focus = gtk_icon_view_item_accessible_grab_focus;
7706 }
7707
7708 static gboolean
7709 gtk_icon_view_item_accessible_add_state (GtkIconViewItemAccessible *item,
7710                                          AtkStateType               state_type,
7711                                          gboolean                   emit_signal)
7712 {
7713   gboolean rc;
7714
7715   rc = atk_state_set_add_state (item->state_set, state_type);
7716   /*
7717    * The signal should only be generated if the value changed,
7718    * not when the item is set up.  So states that are set
7719    * initially should pass FALSE as the emit_signal argument.
7720    */
7721
7722   if (emit_signal)
7723     {
7724       atk_object_notify_state_change (ATK_OBJECT (item), state_type, TRUE);
7725       /* If state_type is ATK_STATE_VISIBLE, additional notification */
7726       if (state_type == ATK_STATE_VISIBLE)
7727         g_signal_emit_by_name (item, "visible_data_changed");
7728     }
7729
7730   return rc;
7731 }
7732
7733 static gboolean
7734 gtk_icon_view_item_accessible_remove_state (GtkIconViewItemAccessible *item,
7735                                             AtkStateType               state_type,
7736                                             gboolean                   emit_signal)
7737 {
7738   if (atk_state_set_contains_state (item->state_set, state_type))
7739     {
7740       gboolean rc;
7741
7742       rc = atk_state_set_remove_state (item->state_set, state_type);
7743       /*
7744        * The signal should only be generated if the value changed,
7745        * not when the item is set up.  So states that are set
7746        * initially should pass FALSE as the emit_signal argument.
7747        */
7748
7749       if (emit_signal)
7750         {
7751           atk_object_notify_state_change (ATK_OBJECT (item), state_type, FALSE);
7752           /* If state_type is ATK_STATE_VISIBLE, additional notification */
7753           if (state_type == ATK_STATE_VISIBLE)
7754             g_signal_emit_by_name (item, "visible_data_changed");
7755         }
7756
7757       return rc;
7758     }
7759   else
7760     return FALSE;
7761 }
7762
7763 static gboolean
7764 gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item)
7765 {
7766   GtkIconView *icon_view;
7767   GdkRectangle visible_rect;
7768   gboolean is_showing;
7769
7770   /*
7771    * An item is considered "SHOWING" if any part of the item is in the
7772    * visible rectangle.
7773    */
7774
7775   if (!GTK_IS_ICON_VIEW (item->widget))
7776     return FALSE;
7777
7778   if (item->item == NULL)
7779     return FALSE;
7780
7781   icon_view = GTK_ICON_VIEW (item->widget);
7782   visible_rect.x = 0;
7783   if (icon_view->priv->hadjustment)
7784     visible_rect.x += icon_view->priv->hadjustment->value;
7785   visible_rect.y = 0;
7786   if (icon_view->priv->hadjustment)
7787     visible_rect.y += icon_view->priv->vadjustment->value;
7788   visible_rect.width = item->widget->allocation.width;
7789   visible_rect.height = item->widget->allocation.height;
7790
7791   if (((item->item->x + item->item->width) < visible_rect.x) ||
7792      ((item->item->y + item->item->height) < (visible_rect.y)) ||
7793      (item->item->x > (visible_rect.x + visible_rect.width)) ||
7794      (item->item->y > (visible_rect.y + visible_rect.height)))
7795     is_showing =  FALSE;
7796   else
7797     is_showing = TRUE;
7798
7799   return is_showing;
7800 }
7801
7802 static gboolean
7803 gtk_icon_view_item_accessible_set_visibility (GtkIconViewItemAccessible *item,
7804                                               gboolean                   emit_signal)
7805 {
7806   if (gtk_icon_view_item_accessible_is_showing (item))
7807     return gtk_icon_view_item_accessible_add_state (item, ATK_STATE_SHOWING,
7808                                                     emit_signal);
7809   else
7810     return gtk_icon_view_item_accessible_remove_state (item, ATK_STATE_SHOWING,
7811                                                        emit_signal);
7812 }
7813
7814 static void
7815 gtk_icon_view_item_accessible_object_init (GtkIconViewItemAccessible *item)
7816 {
7817   gint i;
7818
7819   item->state_set = atk_state_set_new ();
7820
7821   atk_state_set_add_state (item->state_set, ATK_STATE_ENABLED);
7822   atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSABLE);
7823   atk_state_set_add_state (item->state_set, ATK_STATE_SENSITIVE);
7824   atk_state_set_add_state (item->state_set, ATK_STATE_SELECTABLE);
7825   atk_state_set_add_state (item->state_set, ATK_STATE_VISIBLE);
7826
7827   for (i = 0; i < LAST_ACTION; i++)
7828     item->action_descriptions[i] = NULL;
7829
7830   item->image_description = NULL;
7831
7832   item->action_idle_handler = 0;
7833 }
7834
7835 static void
7836 gtk_icon_view_item_accessible_finalize (GObject *object)
7837 {
7838   GtkIconViewItemAccessible *item;
7839   gint i;
7840
7841   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (object));
7842
7843   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (object);
7844
7845   if (item->widget)
7846     g_object_remove_weak_pointer (G_OBJECT (item->widget), (gpointer) &item->widget);
7847
7848   if (item->state_set)
7849     g_object_unref (item->state_set);
7850
7851   if (item->text_buffer)
7852      g_object_unref (item->text_buffer);
7853
7854   for (i = 0; i < LAST_ACTION; i++)
7855     g_free (item->action_descriptions[i]);
7856
7857   g_free (item->image_description);
7858
7859   if (item->action_idle_handler)
7860     {
7861       g_source_remove (item->action_idle_handler);
7862       item->action_idle_handler = 0;
7863     }
7864
7865   G_OBJECT_CLASS (accessible_item_parent_class)->finalize (object);
7866 }
7867
7868 static G_CONST_RETURN gchar*
7869 gtk_icon_view_item_accessible_get_name (AtkObject *obj)
7870 {
7871   if (obj->name)
7872     return obj->name;
7873   else
7874     {
7875       GtkIconViewItemAccessible *item;
7876       GtkTextIter start_iter;
7877       GtkTextIter end_iter;
7878
7879       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7880  
7881       gtk_text_buffer_get_start_iter (item->text_buffer, &start_iter); 
7882       gtk_text_buffer_get_end_iter (item->text_buffer, &end_iter); 
7883
7884       return gtk_text_buffer_get_text (item->text_buffer, &start_iter, &end_iter, FALSE);
7885     }
7886 }
7887
7888 static AtkObject*
7889 gtk_icon_view_item_accessible_get_parent (AtkObject *obj)
7890 {
7891   GtkIconViewItemAccessible *item;
7892
7893   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), NULL);
7894   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7895
7896   if (item->widget)
7897     return gtk_widget_get_accessible (item->widget);
7898   else
7899     return NULL;
7900 }
7901
7902 static gint
7903 gtk_icon_view_item_accessible_get_index_in_parent (AtkObject *obj)
7904 {
7905   GtkIconViewItemAccessible *item;
7906
7907   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), 0);
7908   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7909
7910   return item->item->index; 
7911 }
7912
7913 static AtkStateSet *
7914 gtk_icon_view_item_accessible_ref_state_set (AtkObject *obj)
7915 {
7916   GtkIconViewItemAccessible *item;
7917   GtkIconView *icon_view;
7918
7919   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7920   g_return_val_if_fail (item->state_set, NULL);
7921
7922   if (!item->widget)
7923     return NULL;
7924
7925   icon_view = GTK_ICON_VIEW (item->widget);
7926   if (icon_view->priv->cursor_item == item->item)
7927     atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSED);
7928   else
7929     atk_state_set_remove_state (item->state_set, ATK_STATE_FOCUSED);
7930
7931   return g_object_ref (item->state_set);
7932 }
7933
7934 static void
7935 gtk_icon_view_item_accessible_class_init (AtkObjectClass *klass)
7936 {
7937   GObjectClass *gobject_class;
7938
7939   accessible_item_parent_class = g_type_class_peek_parent (klass);
7940
7941   gobject_class = (GObjectClass *)klass;
7942
7943   gobject_class->finalize = gtk_icon_view_item_accessible_finalize;
7944
7945   klass->get_index_in_parent = gtk_icon_view_item_accessible_get_index_in_parent; 
7946   klass->get_name = gtk_icon_view_item_accessible_get_name; 
7947   klass->get_parent = gtk_icon_view_item_accessible_get_parent; 
7948   klass->ref_state_set = gtk_icon_view_item_accessible_ref_state_set; 
7949 }
7950
7951 static GType
7952 gtk_icon_view_item_accessible_get_type (void)
7953 {
7954   static GType type = 0;
7955
7956   if (!type)
7957     {
7958       static const GTypeInfo tinfo =
7959       {
7960         sizeof (GtkIconViewItemAccessibleClass),
7961         (GBaseInitFunc) NULL, /* base init */
7962         (GBaseFinalizeFunc) NULL, /* base finalize */
7963         (GClassInitFunc) gtk_icon_view_item_accessible_class_init, /* class init */
7964         (GClassFinalizeFunc) NULL, /* class finalize */
7965         NULL, /* class data */
7966         sizeof (GtkIconViewItemAccessible), /* instance size */
7967         0, /* nb preallocs */
7968         (GInstanceInitFunc) gtk_icon_view_item_accessible_object_init, /* instance init */
7969         NULL /* value table */
7970       };
7971
7972       static const GInterfaceInfo atk_component_info =
7973       {
7974         (GInterfaceInitFunc) atk_component_item_interface_init,
7975         (GInterfaceFinalizeFunc) NULL,
7976         NULL
7977       };
7978       static const GInterfaceInfo atk_action_info =
7979       {
7980         (GInterfaceInitFunc) atk_action_item_interface_init,
7981         (GInterfaceFinalizeFunc) NULL,
7982         NULL
7983       };
7984       static const GInterfaceInfo atk_image_info =
7985       {
7986         (GInterfaceInitFunc) atk_image_item_interface_init,
7987         (GInterfaceFinalizeFunc) NULL,
7988         NULL
7989       };
7990       static const GInterfaceInfo atk_text_info =
7991       {
7992         (GInterfaceInitFunc) atk_text_item_interface_init,
7993         (GInterfaceFinalizeFunc) NULL,
7994         NULL
7995       };
7996
7997       type = g_type_register_static (ATK_TYPE_OBJECT,
7998                                      "GtkIconViewItemAccessible", &tinfo, 0);
7999       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
8000                                    &atk_component_info);
8001       g_type_add_interface_static (type, ATK_TYPE_ACTION,
8002                                    &atk_action_info);
8003       g_type_add_interface_static (type, ATK_TYPE_IMAGE,
8004                                    &atk_image_info);
8005       g_type_add_interface_static (type, ATK_TYPE_TEXT,
8006                                    &atk_text_info);
8007     }
8008
8009   return type;
8010 }
8011
8012 #define GTK_TYPE_ICON_VIEW_ACCESSIBLE      (gtk_icon_view_accessible_get_type ())
8013 #define GTK_ICON_VIEW_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE, GtkIconViewAccessible))
8014 #define GTK_IS_ICON_VIEW_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE))
8015
8016 static GType gtk_icon_view_accessible_get_type (void);
8017
8018 typedef struct
8019 {
8020    AtkObject parent;
8021 } GtkIconViewAccessible;
8022
8023 typedef struct
8024 {
8025   AtkObject *item;
8026   gint       index;
8027 } GtkIconViewItemAccessibleInfo;
8028
8029 typedef struct
8030 {
8031   GList *items;
8032
8033   GtkAdjustment *old_hadj;
8034   GtkAdjustment *old_vadj;
8035
8036   GtkTreeModel *model;
8037
8038 } GtkIconViewAccessiblePrivate;
8039
8040 static GtkIconViewAccessiblePrivate *
8041 gtk_icon_view_accessible_get_priv (AtkObject *accessible)
8042 {
8043   return g_object_get_qdata (G_OBJECT (accessible),
8044                              accessible_private_data_quark);
8045 }
8046
8047 static void
8048 gtk_icon_view_item_accessible_info_new (AtkObject *accessible,
8049                                         AtkObject *item,
8050                                         gint       index)
8051 {
8052   GtkIconViewItemAccessibleInfo *info;
8053   GtkIconViewItemAccessibleInfo *tmp_info;
8054   GtkIconViewAccessiblePrivate *priv;
8055   GList *items;
8056
8057   info = g_new (GtkIconViewItemAccessibleInfo, 1);
8058   info->item = item;
8059   info->index = index;
8060
8061   priv = gtk_icon_view_accessible_get_priv (accessible);
8062   items = priv->items;
8063   while (items)
8064     {
8065       tmp_info = items->data;
8066       if (tmp_info->index > index)
8067         break;
8068       items = items->next;
8069     }
8070   priv->items = g_list_insert_before (priv->items, items, info);
8071   priv->old_hadj = NULL;
8072   priv->old_vadj = NULL;
8073 }
8074
8075 static gint
8076 gtk_icon_view_accessible_get_n_children (AtkObject *accessible)
8077 {
8078   GtkIconView *icon_view;
8079   GtkWidget *widget;
8080
8081   widget = GTK_ACCESSIBLE (accessible)->widget;
8082   if (!widget)
8083       return 0;
8084
8085   icon_view = GTK_ICON_VIEW (widget);
8086
8087   return g_list_length (icon_view->priv->items);
8088 }
8089
8090 static AtkObject *
8091 gtk_icon_view_accessible_find_child (AtkObject *accessible,
8092                                      gint       index)
8093 {
8094   GtkIconViewAccessiblePrivate *priv;
8095   GtkIconViewItemAccessibleInfo *info;
8096   GList *items;
8097
8098   priv = gtk_icon_view_accessible_get_priv (accessible);
8099   items = priv->items;
8100
8101   while (items)
8102     {
8103       info = items->data;
8104       if (info->index == index)
8105         return info->item;
8106       items = items->next; 
8107     }
8108   return NULL;
8109 }
8110
8111 static AtkObject *
8112 gtk_icon_view_accessible_ref_child (AtkObject *accessible,
8113                                     gint       index)
8114 {
8115   GtkIconView *icon_view;
8116   GtkWidget *widget;
8117   GList *icons;
8118   AtkObject *obj;
8119   GtkIconViewItemAccessible *a11y_item;
8120
8121   widget = GTK_ACCESSIBLE (accessible)->widget;
8122   if (!widget)
8123     return NULL;
8124
8125   icon_view = GTK_ICON_VIEW (widget);
8126   icons = g_list_nth (icon_view->priv->items, index);
8127   obj = NULL;
8128   if (icons)
8129     {
8130       GtkIconViewItem *item = icons->data;
8131    
8132       g_return_val_if_fail (item->index == index, NULL);
8133       obj = gtk_icon_view_accessible_find_child (accessible, index);
8134       if (!obj)
8135         {
8136           obj = g_object_new (gtk_icon_view_item_accessible_get_type (), NULL);
8137           gtk_icon_view_item_accessible_info_new (accessible,
8138                                                   obj,
8139                                                   index);
8140           obj->role = ATK_ROLE_ICON;
8141           a11y_item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8142           a11y_item->item = item;
8143           a11y_item->widget = widget;
8144           a11y_item->text_buffer = gtk_text_buffer_new (NULL);
8145
8146           gtk_icon_view_set_cell_data (icon_view, item);
8147           gtk_text_buffer_set_text (a11y_item->text_buffer, 
8148                                     get_text (icon_view, item), -1);
8149
8150           gtk_icon_view_item_accessible_set_visibility (a11y_item, FALSE);
8151           g_object_add_weak_pointer (G_OBJECT (widget), (gpointer) &(a11y_item->widget));
8152        }
8153       g_object_ref (obj);
8154     }
8155   return obj;
8156 }
8157
8158 static void
8159 gtk_icon_view_accessible_traverse_items (GtkIconViewAccessible *view,
8160                                          GList                 *list)
8161 {
8162   GtkIconViewAccessiblePrivate *priv;
8163   GtkIconViewItemAccessibleInfo *info;
8164   GtkIconViewItemAccessible *item;
8165   GList *items;
8166   
8167   priv =  gtk_icon_view_accessible_get_priv (ATK_OBJECT (view));
8168   if (priv->items)
8169     {
8170       GtkWidget *widget;
8171       gboolean act_on_item;
8172
8173       widget = GTK_ACCESSIBLE (view)->widget;
8174       if (widget == NULL)
8175         return;
8176
8177       items = priv->items;
8178
8179       act_on_item = (list == NULL);
8180
8181       while (items)
8182         {
8183
8184           info = (GtkIconViewItemAccessibleInfo *)items->data;
8185           item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8186
8187           if (act_on_item == FALSE && list == items)
8188             act_on_item = TRUE;
8189
8190           if (act_on_item)
8191             gtk_icon_view_item_accessible_set_visibility (item, TRUE);
8192
8193           items = items->next;
8194        }
8195    }
8196 }
8197
8198 static void
8199 gtk_icon_view_accessible_adjustment_changed (GtkAdjustment *adjustment,
8200                                              GtkIconView   *icon_view)
8201 {
8202   AtkObject *obj;
8203   GtkIconViewAccessible *view;
8204
8205   /*
8206    * The scrollbars have changed
8207    */
8208   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
8209   view = GTK_ICON_VIEW_ACCESSIBLE (obj);
8210
8211   gtk_icon_view_accessible_traverse_items (view, NULL);
8212 }
8213
8214 static void
8215 gtk_icon_view_accessible_set_scroll_adjustments (GtkWidget      *widget,
8216                                                  GtkAdjustment *hadj,
8217                                                  GtkAdjustment *vadj)
8218 {
8219   AtkObject *atk_obj;
8220   GtkIconViewAccessiblePrivate *priv;
8221
8222   atk_obj = gtk_widget_get_accessible (widget);
8223   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8224
8225   if (priv->old_hadj != hadj)
8226     {
8227       if (priv->old_hadj)
8228         {
8229           g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8230                                         (gpointer *)&priv->old_hadj);
8231           
8232           g_signal_handlers_disconnect_by_func (priv->old_hadj,
8233                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8234                                                 widget);
8235         }
8236       priv->old_hadj = hadj;
8237       if (priv->old_hadj)
8238         {
8239           g_object_add_weak_pointer (G_OBJECT (priv->old_hadj),
8240                                      (gpointer *)&priv->old_hadj);
8241           g_signal_connect (hadj,
8242                             "value-changed",
8243                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8244                             widget);
8245         }
8246     }
8247   if (priv->old_vadj != vadj)
8248     {
8249       if (priv->old_vadj)
8250         {
8251           g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8252                                         (gpointer *)&priv->old_vadj);
8253           
8254           g_signal_handlers_disconnect_by_func (priv->old_vadj,
8255                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8256                                                 widget);
8257         }
8258       priv->old_vadj = vadj;
8259       if (priv->old_vadj)
8260         {
8261           g_object_add_weak_pointer (G_OBJECT (priv->old_vadj),
8262                                      (gpointer *)&priv->old_vadj);
8263           g_signal_connect (vadj,
8264                             "value-changed",
8265                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8266                             widget);
8267         }
8268     }
8269 }
8270
8271 static void
8272 gtk_icon_view_accessible_model_row_changed (GtkTreeModel *tree_model,
8273                                             GtkTreePath  *path,
8274                                             GtkTreeIter  *iter,
8275                                             gpointer     user_data)
8276 {
8277   AtkObject *atk_obj;
8278
8279   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8280   g_signal_emit_by_name (atk_obj, "visible-data-changed");
8281
8282   return;
8283 }
8284
8285 static void
8286 gtk_icon_view_accessible_model_row_inserted (GtkTreeModel *tree_model,
8287                                              GtkTreePath  *path,
8288                                              GtkTreeIter  *iter,
8289                                              gpointer     user_data)
8290 {
8291   GtkIconViewAccessiblePrivate *priv;
8292   GtkIconViewItemAccessibleInfo *info;
8293   GtkIconViewAccessible *view;
8294   GtkIconViewItemAccessible *item;
8295   GList *items;
8296   GList *tmp_list;
8297   AtkObject *atk_obj;
8298   gint index;
8299
8300   index = gtk_tree_path_get_indices(path)[0];
8301   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8302   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8303   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8304
8305   items = priv->items;
8306   tmp_list = NULL;
8307   while (items)
8308     {
8309       info = items->data;
8310       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8311       if (info->index != item->item->index)
8312         {
8313           if (info->index < index)
8314             g_warning ("Unexpected index value on insertion %d %d", index, info->index);
8315  
8316           if (tmp_list == NULL)
8317             tmp_list = items;
8318    
8319           info->index = item->item->index;
8320         }
8321
8322       items = items->next;
8323     }
8324   gtk_icon_view_accessible_traverse_items (view, tmp_list);
8325   g_signal_emit_by_name (atk_obj, "children_changed::add",
8326                          index, NULL, NULL);
8327   return;
8328 }
8329
8330 static void
8331 gtk_icon_view_accessible_model_row_deleted (GtkTreeModel *tree_model,
8332                                             GtkTreePath  *path,
8333                                             gpointer     user_data)
8334 {
8335   GtkIconViewAccessiblePrivate *priv;
8336   GtkIconViewItemAccessibleInfo *info;
8337   GtkIconViewAccessible *view;
8338   GtkIconViewItemAccessible *item;
8339   GList *items;
8340   GList *tmp_list;
8341   GList *deleted_item;
8342   AtkObject *atk_obj;
8343   gint index;
8344
8345   index = gtk_tree_path_get_indices(path)[0];
8346   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8347   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8348   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8349
8350   items = priv->items;
8351   tmp_list = NULL;
8352   deleted_item = NULL;
8353   info = NULL;
8354   while (items)
8355     {
8356       info = items->data;
8357       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8358       if (info->index == index)
8359         {
8360           deleted_item = items;
8361         }
8362       if (info->index != item->item->index)
8363         {
8364           if (tmp_list == NULL)
8365             tmp_list = items;
8366           else    
8367             info->index = item->item->index;
8368         }
8369
8370       items = items->next;
8371     }
8372   gtk_icon_view_accessible_traverse_items (view, tmp_list);
8373   if (deleted_item)
8374     {
8375       info = deleted_item->data;
8376       gtk_icon_view_item_accessible_add_state (GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item), ATK_STATE_DEFUNCT, TRUE);
8377     }
8378   g_signal_emit_by_name (atk_obj, "children_changed::remove",
8379                          index, NULL, NULL);
8380   if (deleted_item)
8381     {
8382       priv->items = g_list_remove_link (priv->items, deleted_item);
8383       g_free (info);
8384     }
8385
8386   return;
8387 }
8388
8389 static gint
8390 gtk_icon_view_accessible_item_compare (GtkIconViewItemAccessibleInfo *i1,
8391                                        GtkIconViewItemAccessibleInfo *i2)
8392 {
8393   return i1->index - i2->index;
8394 }
8395
8396 static void
8397 gtk_icon_view_accessible_model_rows_reordered (GtkTreeModel *tree_model,
8398                                                GtkTreePath  *path,
8399                                                GtkTreeIter  *iter,
8400                                                gint         *new_order,
8401                                                gpointer     user_data)
8402 {
8403   GtkIconViewAccessiblePrivate *priv;
8404   GtkIconViewItemAccessibleInfo *info;
8405   GtkIconViewAccessible *view;
8406   GtkIconView *icon_view;
8407   GtkIconViewItemAccessible *item;
8408   GList *items;
8409   GList *tmp_list;
8410   AtkObject *atk_obj;
8411
8412   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8413   icon_view = GTK_ICON_VIEW (user_data);
8414   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8415   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8416
8417   items = priv->items;
8418   tmp_list = NULL;
8419   while (items)
8420     {
8421       info = items->data;
8422       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8423       info->index = new_order[info->index];
8424       tmp_list = g_list_nth (icon_view->priv->items, info->index);
8425       item->item = tmp_list->data;
8426       items = items->next;
8427     }
8428   priv->items = g_list_sort (priv->items, 
8429                              (GCompareFunc)gtk_icon_view_accessible_item_compare);
8430
8431   return;
8432 }
8433
8434 static void
8435 gtk_icon_view_accessible_disconnect_model_signals (GtkTreeModel *model,
8436                                                    GtkWidget *widget)
8437 {
8438   GObject *obj;
8439
8440   obj = G_OBJECT (model);
8441   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_changed, widget);
8442   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_inserted, widget);
8443   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_deleted, widget);
8444   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_rows_reordered, widget);
8445 }
8446
8447 static void
8448 gtk_icon_view_accessible_connect_model_signals (GtkIconView *icon_view)
8449 {
8450   GObject *obj;
8451
8452   obj = G_OBJECT (icon_view->priv->model);
8453   g_signal_connect_data (obj, "row-changed",
8454                          (GCallback) gtk_icon_view_accessible_model_row_changed,
8455                          icon_view, NULL, 0);
8456   g_signal_connect_data (obj, "row-inserted",
8457                          (GCallback) gtk_icon_view_accessible_model_row_inserted, 
8458                          icon_view, NULL, G_CONNECT_AFTER);
8459   g_signal_connect_data (obj, "row-deleted",
8460                          (GCallback) gtk_icon_view_accessible_model_row_deleted, 
8461                          icon_view, NULL, G_CONNECT_AFTER);
8462   g_signal_connect_data (obj, "rows-reordered",
8463                          (GCallback) gtk_icon_view_accessible_model_rows_reordered, 
8464                          icon_view, NULL, G_CONNECT_AFTER);
8465 }
8466
8467 static void
8468 gtk_icon_view_accessible_clear_cache (GtkIconViewAccessiblePrivate *priv)
8469 {
8470   GtkIconViewItemAccessibleInfo *info;
8471   GList *items;
8472
8473   items = priv->items;
8474   while (items)
8475     {
8476       info = (GtkIconViewItemAccessibleInfo *) items->data;
8477       g_object_unref (info->item);
8478       g_free (items->data);
8479       items = items->next;
8480     }
8481   g_list_free (priv->items);
8482   priv->items = NULL;
8483 }
8484
8485 static void
8486 gtk_icon_view_accessible_notify_gtk (GObject *obj,
8487                                      GParamSpec *pspec)
8488 {
8489   GtkIconView *icon_view;
8490   GtkWidget *widget;
8491   AtkObject *atk_obj;
8492   GtkIconViewAccessible *view;
8493   GtkIconViewAccessiblePrivate *priv;
8494
8495   if (strcmp (pspec->name, "model") == 0)
8496     {
8497       widget = GTK_WIDGET (obj); 
8498       atk_obj = gtk_widget_get_accessible (widget);
8499       view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8500       priv = gtk_icon_view_accessible_get_priv (atk_obj);
8501       if (priv->model)
8502         {
8503           g_object_remove_weak_pointer (G_OBJECT (priv->model),
8504                                         (gpointer *)&priv->model);
8505           gtk_icon_view_accessible_disconnect_model_signals (priv->model, widget);
8506         }
8507       gtk_icon_view_accessible_clear_cache (priv);
8508
8509       icon_view = GTK_ICON_VIEW (obj);
8510       priv->model = icon_view->priv->model;
8511       /* If there is no model the GtkIconView is probably being destroyed */
8512       if (priv->model)
8513         {
8514           g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
8515           gtk_icon_view_accessible_connect_model_signals (icon_view);
8516         }
8517     }
8518
8519   return;
8520 }
8521
8522 static void
8523 gtk_icon_view_accessible_initialize (AtkObject *accessible,
8524                                      gpointer   data)
8525 {
8526   GtkIconViewAccessiblePrivate *priv;
8527   GtkIconView *icon_view;
8528
8529   if (ATK_OBJECT_CLASS (accessible_parent_class)->initialize)
8530     ATK_OBJECT_CLASS (accessible_parent_class)->initialize (accessible, data);
8531
8532   priv = g_new0 (GtkIconViewAccessiblePrivate, 1);
8533   g_object_set_qdata (G_OBJECT (accessible),
8534                       accessible_private_data_quark,
8535                       priv);
8536
8537   icon_view = GTK_ICON_VIEW (data);
8538   if (icon_view->priv->hadjustment)
8539     {
8540       priv->old_hadj = icon_view->priv->hadjustment;
8541       g_object_add_weak_pointer (G_OBJECT (priv->old_hadj), (gpointer *)&priv->old_hadj);
8542       g_signal_connect (icon_view->priv->hadjustment,
8543                         "value-changed",
8544                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8545                         icon_view);
8546     } 
8547   if (icon_view->priv->vadjustment)
8548     {
8549       priv->old_vadj = icon_view->priv->vadjustment;
8550       g_object_add_weak_pointer (G_OBJECT (priv->old_vadj), (gpointer *)&priv->old_vadj);
8551       g_signal_connect (icon_view->priv->vadjustment,
8552                         "value-changed",
8553                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8554                         icon_view);
8555     }
8556   g_signal_connect_after (data,
8557                           "set_scroll_adjustments",
8558                           G_CALLBACK (gtk_icon_view_accessible_set_scroll_adjustments),
8559                           NULL);
8560   g_signal_connect (data,
8561                     "notify",
8562                     G_CALLBACK (gtk_icon_view_accessible_notify_gtk),
8563                     NULL);
8564
8565   priv->model = icon_view->priv->model;
8566   if (priv->model)
8567     {
8568       g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
8569       gtk_icon_view_accessible_connect_model_signals (icon_view);
8570     }
8571                           
8572   accessible->role = ATK_ROLE_LAYERED_PANE;
8573 }
8574
8575 static void
8576 gtk_icon_view_accessible_finalize (GObject *object)
8577 {
8578   GtkIconViewAccessiblePrivate *priv;
8579
8580   priv = gtk_icon_view_accessible_get_priv (ATK_OBJECT (object));
8581   gtk_icon_view_accessible_clear_cache (priv);
8582
8583   g_free (priv);
8584
8585   G_OBJECT_CLASS (accessible_parent_class)->finalize (object);
8586 }
8587
8588 static void
8589 gtk_icon_view_accessible_destroyed (GtkWidget *widget,
8590                                     GtkAccessible *accessible)
8591 {
8592   AtkObject *atk_obj;
8593   GtkIconViewAccessiblePrivate *priv;
8594
8595   atk_obj = ATK_OBJECT (accessible);
8596   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8597   if (priv->old_hadj)
8598     {
8599       g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8600                                     (gpointer *)&priv->old_hadj);
8601           
8602       g_signal_handlers_disconnect_by_func (priv->old_hadj,
8603                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
8604                                             widget);
8605       priv->old_hadj = NULL;
8606     }
8607   if (priv->old_vadj)
8608     {
8609       g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8610                                     (gpointer *)&priv->old_vadj);
8611           
8612       g_signal_handlers_disconnect_by_func (priv->old_vadj,
8613                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
8614                                             widget);
8615       priv->old_vadj = NULL;
8616     }
8617 }
8618
8619 static void
8620 gtk_icon_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
8621 {
8622   if (accessible->widget)
8623     {
8624       g_signal_connect_after (accessible->widget,
8625                               "destroy",
8626                               G_CALLBACK (gtk_icon_view_accessible_destroyed),
8627                               accessible);
8628     }
8629   GTK_ACCESSIBLE_CLASS (accessible_parent_class)->connect_widget_destroyed (accessible);
8630 }
8631
8632 static void
8633 gtk_icon_view_accessible_class_init (AtkObjectClass *klass)
8634 {
8635   GObjectClass *gobject_class;
8636   GtkAccessibleClass *accessible_class;
8637
8638   accessible_parent_class = g_type_class_peek_parent (klass);
8639
8640   gobject_class = (GObjectClass *)klass;
8641   accessible_class = (GtkAccessibleClass *)klass;
8642
8643   gobject_class->finalize = gtk_icon_view_accessible_finalize;
8644
8645   klass->get_n_children = gtk_icon_view_accessible_get_n_children;
8646   klass->ref_child = gtk_icon_view_accessible_ref_child;
8647   klass->initialize = gtk_icon_view_accessible_initialize;
8648
8649   accessible_class->connect_widget_destroyed = gtk_icon_view_accessible_connect_widget_destroyed;
8650
8651   accessible_private_data_quark = g_quark_from_static_string ("icon_view-accessible-private-data");
8652 }
8653
8654 static AtkObject*
8655 gtk_icon_view_accessible_ref_accessible_at_point (AtkComponent *component,
8656                                                   gint          x,
8657                                                   gint          y,
8658                                                   AtkCoordType  coord_type)
8659 {
8660   GtkWidget *widget;
8661   GtkIconView *icon_view;
8662   GtkIconViewItem *item;
8663   gint x_pos, y_pos;
8664
8665   widget = GTK_ACCESSIBLE (component)->widget;
8666   if (widget == NULL)
8667   /* State is defunct */
8668     return NULL;
8669
8670   icon_view = GTK_ICON_VIEW (widget);
8671   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
8672   item = gtk_icon_view_get_item_at_coords (icon_view, x - x_pos, y - y_pos, TRUE, NULL);
8673   if (item)
8674     return gtk_icon_view_accessible_ref_child (ATK_OBJECT (component), item->index);
8675
8676   return NULL;
8677 }
8678
8679 static void
8680 atk_component_interface_init (AtkComponentIface *iface)
8681 {
8682   iface->ref_accessible_at_point = gtk_icon_view_accessible_ref_accessible_at_point;
8683 }
8684
8685 static gboolean
8686 gtk_icon_view_accessible_add_selection (AtkSelection *selection,
8687                                         gint i)
8688 {
8689   GtkWidget *widget;
8690   GtkIconView *icon_view;
8691   GtkIconViewItem *item;
8692   GList *l;
8693
8694   widget = GTK_ACCESSIBLE (selection)->widget;
8695   if (widget == NULL)
8696     return FALSE;
8697
8698   icon_view = GTK_ICON_VIEW (widget);
8699
8700   l = g_list_nth (icon_view->priv->items, i);
8701   if (!l)
8702     return FALSE;
8703
8704   item = l->data;
8705   gtk_icon_view_select_item (icon_view, item);
8706
8707   return TRUE;
8708 }
8709
8710 static gboolean
8711 gtk_icon_view_accessible_clear_selection (AtkSelection *selection)
8712 {
8713   GtkWidget *widget;
8714   GtkIconView *icon_view;
8715
8716   widget = GTK_ACCESSIBLE (selection)->widget;
8717   if (widget == NULL)
8718     return FALSE;
8719
8720   icon_view = GTK_ICON_VIEW (widget);
8721   gtk_icon_view_unselect_all (icon_view);
8722
8723   return TRUE;
8724 }
8725
8726 static AtkObject*
8727 gtk_icon_view_accessible_ref_selection (AtkSelection *selection,
8728                                         gint          i)
8729 {
8730   GtkWidget *widget;
8731   GtkIconView *icon_view;
8732   GtkIconViewItem *item;
8733   GList *l;
8734
8735   widget = GTK_ACCESSIBLE (selection)->widget;
8736   if (widget == NULL)
8737     return NULL;
8738
8739   icon_view = GTK_ICON_VIEW (widget);
8740
8741   l = icon_view->priv->items;
8742   while (l)
8743     {
8744       item = l->data;
8745       if (item->selected)
8746         {
8747           if (i == 0)
8748             return atk_object_ref_accessible_child (gtk_widget_get_accessible (widget), item->index);
8749           else
8750             i--;
8751         }
8752       l = l->next;
8753     }
8754
8755   return NULL;
8756 }
8757
8758 static gint
8759 gtk_icon_view_accessible_get_selection_count (AtkSelection *selection)
8760 {
8761   GtkWidget *widget;
8762   GtkIconView *icon_view;
8763   GtkIconViewItem *item;
8764   GList *l;
8765   gint count;
8766
8767   widget = GTK_ACCESSIBLE (selection)->widget;
8768   if (widget == NULL)
8769     return 0;
8770
8771   icon_view = GTK_ICON_VIEW (widget);
8772
8773   l = icon_view->priv->items;
8774   count = 0;
8775   while (l)
8776     {
8777       item = l->data;
8778
8779       if (item->selected)
8780         count++;
8781
8782       l = l->next;
8783     }
8784
8785   return count;
8786 }
8787
8788 static gboolean
8789 gtk_icon_view_accessible_is_child_selected (AtkSelection *selection,
8790                                             gint          i)
8791 {
8792   GtkWidget *widget;
8793   GtkIconView *icon_view;
8794   GtkIconViewItem *item;
8795   GList *l;
8796
8797   widget = GTK_ACCESSIBLE (selection)->widget;
8798   if (widget == NULL)
8799     return FALSE;
8800
8801   icon_view = GTK_ICON_VIEW (widget);
8802   l = g_list_nth (icon_view->priv->items, i);
8803   if (!l)
8804     return FALSE;
8805
8806   item = l->data;
8807
8808   return item->selected;
8809 }
8810
8811 static gboolean
8812 gtk_icon_view_accessible_remove_selection (AtkSelection *selection,
8813                                            gint          i)
8814 {
8815   GtkWidget *widget;
8816   GtkIconView *icon_view;
8817   GtkIconViewItem *item;
8818   GList *l;
8819   gint count;
8820
8821   widget = GTK_ACCESSIBLE (selection)->widget;
8822   if (widget == NULL)
8823     return FALSE;
8824
8825   icon_view = GTK_ICON_VIEW (widget);
8826   l = icon_view->priv->items;
8827   count = 0;
8828   while (l)
8829     {
8830       item = l->data;
8831       if (item->selected)
8832         {
8833           if (count == i)
8834             {
8835               gtk_icon_view_unselect_item (icon_view, item);
8836               return TRUE;
8837             }
8838           count++;
8839         }
8840       l = l->next;
8841     }
8842
8843   return FALSE;
8844 }
8845  
8846 static gboolean
8847 gtk_icon_view_accessible_select_all_selection (AtkSelection *selection)
8848 {
8849   GtkWidget *widget;
8850   GtkIconView *icon_view;
8851
8852   widget = GTK_ACCESSIBLE (selection)->widget;
8853   if (widget == NULL)
8854     return FALSE;
8855
8856   icon_view = GTK_ICON_VIEW (widget);
8857   gtk_icon_view_select_all (icon_view);
8858   return TRUE;
8859 }
8860
8861 static void
8862 gtk_icon_view_accessible_selection_interface_init (AtkSelectionIface *iface)
8863 {
8864   iface->add_selection = gtk_icon_view_accessible_add_selection;
8865   iface->clear_selection = gtk_icon_view_accessible_clear_selection;
8866   iface->ref_selection = gtk_icon_view_accessible_ref_selection;
8867   iface->get_selection_count = gtk_icon_view_accessible_get_selection_count;
8868   iface->is_child_selected = gtk_icon_view_accessible_is_child_selected;
8869   iface->remove_selection = gtk_icon_view_accessible_remove_selection;
8870   iface->select_all_selection = gtk_icon_view_accessible_select_all_selection;
8871 }
8872
8873 static GType
8874 gtk_icon_view_accessible_get_type (void)
8875 {
8876   static GType type = 0;
8877
8878   if (!type)
8879     {
8880       static GTypeInfo tinfo =
8881       {
8882         0, /* class size */
8883         (GBaseInitFunc) NULL, /* base init */
8884         (GBaseFinalizeFunc) NULL, /* base finalize */
8885         (GClassInitFunc) gtk_icon_view_accessible_class_init,
8886         (GClassFinalizeFunc) NULL, /* class finalize */
8887         NULL, /* class data */
8888         0, /* instance size */
8889         0, /* nb preallocs */
8890         (GInstanceInitFunc) NULL, /* instance init */
8891         NULL /* value table */
8892       };
8893       static const GInterfaceInfo atk_component_info =
8894       {
8895         (GInterfaceInitFunc) atk_component_interface_init,
8896         (GInterfaceFinalizeFunc) NULL,
8897         NULL
8898       };
8899       static const GInterfaceInfo atk_selection_info = 
8900       {
8901         (GInterfaceInitFunc) gtk_icon_view_accessible_selection_interface_init,
8902         (GInterfaceFinalizeFunc) NULL,
8903         NULL
8904       };
8905
8906       /*
8907        * Figure out the size of the class and instance
8908        * we are deriving from
8909        */
8910       AtkObjectFactory *factory;
8911       GType derived_type;
8912       GTypeQuery query;
8913       GType derived_atk_type;
8914
8915       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
8916       factory = atk_registry_get_factory (atk_get_default_registry (), 
8917                                           derived_type);
8918       derived_atk_type = atk_object_factory_get_accessible_type (factory);
8919       g_type_query (derived_atk_type, &query);
8920       tinfo.class_size = query.class_size;
8921       tinfo.instance_size = query.instance_size;
8922  
8923       type = g_type_register_static (derived_atk_type, 
8924                                      "GtkIconViewAccessible", 
8925                                      &tinfo, 0);
8926       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
8927                                    &atk_component_info);
8928       g_type_add_interface_static (type, ATK_TYPE_SELECTION,
8929                                    &atk_selection_info);
8930     }
8931   return type;
8932 }
8933
8934 static AtkObject *
8935 gtk_icon_view_accessible_new (GObject *obj)
8936 {
8937   AtkObject *accessible;
8938
8939   g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
8940
8941   accessible = g_object_new (gtk_icon_view_accessible_get_type (), NULL);
8942   atk_object_initialize (accessible, obj);
8943
8944   return accessible;
8945 }
8946
8947 static GType
8948 gtk_icon_view_accessible_factory_get_accessible_type (void)
8949 {
8950   return gtk_icon_view_accessible_get_type ();
8951 }
8952
8953 static AtkObject*
8954 gtk_icon_view_accessible_factory_create_accessible (GObject *obj)
8955 {
8956   return gtk_icon_view_accessible_new (obj);
8957 }
8958
8959 static void
8960 gtk_icon_view_accessible_factory_class_init (AtkObjectFactoryClass *klass)
8961 {
8962   klass->create_accessible = gtk_icon_view_accessible_factory_create_accessible;
8963   klass->get_accessible_type = gtk_icon_view_accessible_factory_get_accessible_type;
8964 }
8965
8966 static GType
8967 gtk_icon_view_accessible_factory_get_type (void)
8968 {
8969   static GType type = 0;
8970
8971   if (!type)
8972     {
8973       static const GTypeInfo tinfo =
8974       {
8975         sizeof (AtkObjectFactoryClass),
8976         NULL,           /* base_init */
8977         NULL,           /* base_finalize */
8978         (GClassInitFunc) gtk_icon_view_accessible_factory_class_init,
8979         NULL,           /* class_finalize */
8980         NULL,           /* class_data */
8981         sizeof (AtkObjectFactory),
8982         0,             /* n_preallocs */
8983         NULL, NULL
8984       };
8985
8986       type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY, 
8987                                     "GtkIconViewAccessibleFactory",
8988                                     &tinfo, 0);
8989     }
8990   return type;
8991 }
8992
8993
8994 static AtkObject *
8995 gtk_icon_view_get_accessible (GtkWidget *widget)
8996 {
8997   static gboolean first_time = TRUE;
8998
8999   if (first_time)
9000     {
9001       AtkObjectFactory *factory;
9002       AtkRegistry *registry;
9003       GType derived_type; 
9004       GType derived_atk_type; 
9005
9006       /*
9007        * Figure out whether accessibility is enabled by looking at the
9008        * type of the accessible object which would be created for
9009        * the parent type of GtkIconView.
9010        */
9011       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
9012
9013       registry = atk_get_default_registry ();
9014       factory = atk_registry_get_factory (registry,
9015                                           derived_type);
9016       derived_atk_type = atk_object_factory_get_accessible_type (factory);
9017       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE)) 
9018         atk_registry_set_factory_type (registry, 
9019                                        GTK_TYPE_ICON_VIEW,
9020                                        gtk_icon_view_accessible_factory_get_type ());
9021       first_time = FALSE;
9022     } 
9023   return (* GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->get_accessible) (widget);
9024 }
9025
9026 #define __GTK_ICON_VIEW_C__
9027 #include "gtkaliasdef.c"