]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
Make sure that cell boxes are always completely inside the item area.
[~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  * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4392  *
4393  * Since: 2.8
4394  **/
4395 gboolean
4396 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
4397                                  GtkTreePath **start_path,
4398                                  GtkTreePath **end_path)
4399 {
4400   gint start_index = -1;
4401   gint end_index = -1;
4402   GtkIconViewItem *item;
4403   GList *icons;
4404
4405   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4406
4407   if (icon_view->priv->hadjustment == NULL ||
4408       icon_view->priv->vadjustment == NULL)
4409     return FALSE;
4410
4411   if (start_path == NULL && end_path == NULL)
4412     return FALSE;
4413   
4414   for (icons = icon_view->priv->items; icons; icons = icons->next) 
4415     {
4416       GtkIconViewItem *item = icons->data;
4417
4418       if ((item->x + item->width >= (int)icon_view->priv->hadjustment->value) &&
4419           (item->y + item->height >= (int)icon_view->priv->vadjustment->value) &&
4420           (item->x <= (int) (icon_view->priv->hadjustment->value + icon_view->priv->hadjustment->page_size)) &&
4421           (item->y <= (int) (icon_view->priv->vadjustment->value + icon_view->priv->vadjustment->page_size)))
4422         {
4423           if (start_index == -1)
4424             start_index = item->index;
4425           end_index = item->index;
4426         }
4427     }
4428
4429   if (start_path && start_index != -1)
4430     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4431   if (end_path && end_index != -1)
4432     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4433   
4434   return start_index != -1;
4435 }
4436
4437 /**
4438  * gtk_icon_view_selected_foreach:
4439  * @icon_view: A #GtkIconView.
4440  * @func: The funcion to call for each selected icon.
4441  * @data: User data to pass to the function.
4442  * 
4443  * Calls a function for each selected icon. Note that the model or
4444  * selection cannot be modified from within this function.
4445  *
4446  * Since: 2.6 
4447  **/
4448 void
4449 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
4450                                 GtkIconViewForeachFunc func,
4451                                 gpointer               data)
4452 {
4453   GList *list;
4454   
4455   for (list = icon_view->priv->items; list; list = list->next)
4456     {
4457       GtkIconViewItem *item = list->data;
4458       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4459
4460       if (item->selected)
4461         (* func) (icon_view, path, data);
4462
4463       gtk_tree_path_free (path);
4464     }
4465 }
4466
4467 /**
4468  * gtk_icon_view_set_selection_mode:
4469  * @icon_view: A #GtkIconView.
4470  * @mode: The selection mode
4471  * 
4472  * Sets the selection mode of the @icon_view.
4473  *
4474  * Since: 2.6 
4475  **/
4476 void
4477 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
4478                                   GtkSelectionMode  mode)
4479 {
4480   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4481
4482   if (mode == icon_view->priv->selection_mode)
4483     return;
4484   
4485   if (mode == GTK_SELECTION_NONE ||
4486       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4487     gtk_icon_view_unselect_all (icon_view);
4488   
4489   icon_view->priv->selection_mode = mode;
4490
4491   g_object_notify (G_OBJECT (icon_view), "selection-mode");
4492 }
4493
4494 /**
4495  * gtk_icon_view_get_selection_mode:
4496  * @icon_view: A #GtkIconView.
4497  * 
4498  * Gets the selection mode of the @icon_view.
4499  *
4500  * Return value: the current selection mode
4501  *
4502  * Since: 2.6 
4503  **/
4504 GtkSelectionMode
4505 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4506 {
4507   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4508
4509   return icon_view->priv->selection_mode;
4510 }
4511
4512 /**
4513  * gtk_icon_view_set_model:
4514  * @icon_view: A #GtkIconView.
4515  * @model: The model.
4516  *
4517  * Sets the model for a #GtkIconView.  
4518  * If the @icon_view already has a model set, it will remove 
4519  * it before setting the new model.  If @model is %NULL, then
4520  * it will unset the old model.
4521  *
4522  * Since: 2.6 
4523  **/
4524 void
4525 gtk_icon_view_set_model (GtkIconView *icon_view,
4526                          GtkTreeModel *model)
4527 {
4528   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4529   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4530   
4531   if (icon_view->priv->model == model)
4532     return;
4533
4534   gtk_icon_view_stop_editing (icon_view, TRUE);
4535
4536   if (model)
4537     {
4538       GType column_type;
4539       
4540       g_return_if_fail (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY);
4541
4542       if (icon_view->priv->pixbuf_column != -1)
4543         {
4544           column_type = gtk_tree_model_get_column_type (model,
4545                                                         icon_view->priv->pixbuf_column);          
4546
4547           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4548         }
4549
4550       if (icon_view->priv->text_column != -1)
4551         {
4552           column_type = gtk_tree_model_get_column_type (model,
4553                                                         icon_view->priv->text_column);    
4554
4555           g_return_if_fail (column_type == G_TYPE_STRING);
4556         }
4557
4558       if (icon_view->priv->markup_column != -1)
4559         {
4560           column_type = gtk_tree_model_get_column_type (model,
4561                                                         icon_view->priv->markup_column);          
4562
4563           g_return_if_fail (column_type == G_TYPE_STRING);
4564         }
4565       
4566     }
4567   
4568   if (icon_view->priv->model)
4569     {
4570       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4571                                             gtk_icon_view_row_changed,
4572                                             icon_view);
4573       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4574                                             gtk_icon_view_row_inserted,
4575                                             icon_view);
4576       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4577                                             gtk_icon_view_row_deleted,
4578                                             icon_view);
4579       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4580                                             gtk_icon_view_rows_reordered,
4581                                             icon_view);
4582
4583       g_object_unref (icon_view->priv->model);
4584       
4585       g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
4586       g_list_free (icon_view->priv->items);
4587       icon_view->priv->items = NULL;
4588       icon_view->priv->anchor_item = NULL;
4589       icon_view->priv->cursor_item = NULL;
4590       icon_view->priv->last_single_clicked = NULL;
4591       icon_view->priv->width = 0;
4592       icon_view->priv->height = 0;
4593     }
4594
4595   icon_view->priv->model = model;
4596
4597   if (icon_view->priv->model)
4598     {
4599       g_object_ref (icon_view->priv->model);
4600       g_signal_connect (icon_view->priv->model,
4601                         "row_changed",
4602                         G_CALLBACK (gtk_icon_view_row_changed),
4603                         icon_view);
4604       g_signal_connect (icon_view->priv->model,
4605                         "row_inserted",
4606                         G_CALLBACK (gtk_icon_view_row_inserted),
4607                         icon_view);
4608       g_signal_connect (icon_view->priv->model,
4609                         "row_deleted",
4610                         G_CALLBACK (gtk_icon_view_row_deleted),
4611                         icon_view);
4612       g_signal_connect (icon_view->priv->model,
4613                         "rows_reordered",
4614                         G_CALLBACK (gtk_icon_view_rows_reordered),
4615                         icon_view);
4616
4617       gtk_icon_view_build_items (icon_view);
4618
4619       gtk_icon_view_queue_layout (icon_view);
4620     }
4621
4622   g_object_notify (G_OBJECT (icon_view), "model");  
4623
4624   if (GTK_WIDGET_REALIZED (icon_view))
4625     gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4626 }
4627
4628 /**
4629  * gtk_icon_view_get_model:
4630  * @icon_view: a #GtkIconView
4631  *
4632  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
4633  * model is unset.
4634  *
4635  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
4636  *
4637  * Since: 2.6 
4638  **/
4639 GtkTreeModel *
4640 gtk_icon_view_get_model (GtkIconView *icon_view)
4641 {
4642   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4643
4644   return icon_view->priv->model;
4645 }
4646
4647 static void
4648 update_text_cell (GtkIconView *icon_view)
4649 {
4650   GtkIconViewCellInfo *info;
4651   GList *l;
4652   gint i;
4653           
4654   if (icon_view->priv->text_column == -1 &&
4655       icon_view->priv->markup_column == -1)
4656     {
4657       if (icon_view->priv->text_cell != -1)
4658         {
4659           info = g_list_nth_data (icon_view->priv->cell_list, 
4660                                   icon_view->priv->text_cell);
4661           
4662           g_list_remove (icon_view->priv->cell_list, info);
4663           
4664           free_cell_info (info);
4665           
4666           icon_view->priv->n_cells--;
4667           icon_view->priv->text_cell = -1;
4668         }
4669     }
4670   else 
4671     {
4672       if (icon_view->priv->text_cell == -1)
4673         {
4674           GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
4675           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
4676           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4677             {
4678               info = l->data;
4679               if (info->cell == cell)
4680                 {
4681                   icon_view->priv->text_cell = i;
4682                   break;
4683                 }
4684             }
4685         }
4686       
4687       info = g_list_nth_data (icon_view->priv->cell_list,
4688                               icon_view->priv->text_cell);
4689
4690       if (icon_view->priv->markup_column != -1)
4691         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4692                                         info->cell, 
4693                                         "markup", icon_view->priv->markup_column, 
4694                                         NULL);
4695       else
4696         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4697                                         info->cell, 
4698                                         "text", icon_view->priv->text_column, 
4699                                         NULL);
4700
4701       if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
4702         g_object_set (info->cell,
4703                       "wrap_mode", PANGO_WRAP_CHAR,
4704                       "wrap_width", icon_view->priv->item_width,
4705                       "xalign", 0.5,
4706                       "yalign", 0.0,
4707                       NULL);
4708       else
4709         g_object_set (info->cell,
4710                       "wrap_mode", PANGO_WRAP_CHAR,
4711                       "wrap_width", icon_view->priv->item_width,
4712                       "xalign", 0.0,
4713                       "yalign", 0.0,
4714                       NULL);
4715     }
4716 }
4717
4718 static void
4719 update_pixbuf_cell (GtkIconView *icon_view)
4720 {
4721   GtkIconViewCellInfo *info;
4722   GList *l;
4723   gint i;
4724
4725   if (icon_view->priv->pixbuf_column == -1)
4726     {
4727       if (icon_view->priv->pixbuf_cell != -1)
4728         {
4729           info = g_list_nth_data (icon_view->priv->cell_list, 
4730                                   icon_view->priv->pixbuf_cell);
4731           
4732           g_list_remove (icon_view->priv->cell_list, info);
4733           
4734           free_cell_info (info);
4735           
4736           icon_view->priv->n_cells--;
4737           icon_view->priv->pixbuf_cell = -1;
4738         }
4739     }
4740   else 
4741     {
4742       if (icon_view->priv->pixbuf_cell == -1)
4743         {
4744           GtkCellRenderer *cell = gtk_cell_renderer_pixbuf_new ();
4745           
4746           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
4747           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4748             {
4749               info = l->data;
4750               if (info->cell == cell)
4751                 {
4752                   icon_view->priv->pixbuf_cell = i;
4753                   break;
4754                 }
4755             }
4756         }
4757       
4758         info = g_list_nth_data (icon_view->priv->cell_list, 
4759                                 icon_view->priv->pixbuf_cell);
4760         
4761         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4762                                         info->cell, 
4763                                         "pixbuf", icon_view->priv->pixbuf_column, 
4764                                         NULL);
4765
4766         if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
4767           g_object_set (info->cell,
4768                         "follow_state", TRUE, 
4769                         "xalign", 0.5,
4770                         "yalign", 1.0,
4771                         NULL);
4772         else
4773           g_object_set (info->cell,
4774                         "follow_state", TRUE, 
4775                         "xalign", 0.0,
4776                         "yalign", 0.0,
4777                         NULL);
4778     }
4779 }
4780
4781 /**
4782  * gtk_icon_view_set_text_column:
4783  * @icon_view: A #GtkIconView.
4784  * @column: A column in the currently used model.
4785  * 
4786  * Sets the column with text for @icon_view to be @column. The text
4787  * column must be of type #G_TYPE_STRING.
4788  *
4789  * Since: 2.6 
4790  **/
4791 void
4792 gtk_icon_view_set_text_column (GtkIconView *icon_view,
4793                                gint          column)
4794 {
4795   if (column == icon_view->priv->text_column)
4796     return;
4797   
4798   if (column == -1)
4799     icon_view->priv->text_column = -1;
4800   else
4801     {
4802       if (icon_view->priv->model != NULL)
4803         {
4804           GType column_type;
4805           
4806           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4807
4808           g_return_if_fail (column_type == G_TYPE_STRING);
4809         }
4810       
4811       icon_view->priv->text_column = column;
4812     }
4813
4814   gtk_icon_view_stop_editing (icon_view, TRUE);
4815
4816   update_text_cell (icon_view);
4817
4818   gtk_icon_view_invalidate_sizes (icon_view);
4819   gtk_icon_view_queue_layout (icon_view);
4820   
4821   g_object_notify (G_OBJECT (icon_view), "text-column");
4822 }
4823
4824 /**
4825  * gtk_icon_view_get_text_column:
4826  * @icon_view: A #GtkIconView.
4827  *
4828  * Returns the column with text for @icon_view.
4829  *
4830  * Returns: the text column, or -1 if it's unset.
4831  *
4832  * Since: 2.6
4833  */
4834 gint
4835 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
4836 {
4837   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4838
4839   return icon_view->priv->text_column;
4840 }
4841
4842 /**
4843  * gtk_icon_view_set_markup_column:
4844  * @icon_view: A #GtkIconView.
4845  * @column: A column in the currently used model.
4846  * 
4847  * Sets the column with markup information for @icon_view to be
4848  * @column. The markup column must be of type #G_TYPE_STRING.
4849  * If the markup column is set to something, it overrides
4850  * the text column set by gtk_icon_view_set_text_column().
4851  *
4852  * Since: 2.6
4853  **/
4854 void
4855 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
4856                                  gint         column)
4857 {
4858   if (column == icon_view->priv->markup_column)
4859     return;
4860   
4861   if (column == -1)
4862     icon_view->priv->markup_column = -1;
4863   else
4864     {
4865       if (icon_view->priv->model != NULL)
4866         {
4867           GType column_type;
4868           
4869           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4870
4871           g_return_if_fail (column_type == G_TYPE_STRING);
4872         }
4873       
4874       icon_view->priv->markup_column = column;
4875     }
4876
4877   gtk_icon_view_stop_editing (icon_view, TRUE);
4878
4879   update_text_cell (icon_view);
4880
4881   gtk_icon_view_invalidate_sizes (icon_view);
4882   gtk_icon_view_queue_layout (icon_view);
4883   
4884   g_object_notify (G_OBJECT (icon_view), "markup-column");
4885 }
4886
4887 /**
4888  * gtk_icon_view_get_markup_column:
4889  * @icon_view: A #GtkIconView.
4890  *
4891  * Returns the column with markup text for @icon_view.
4892  *
4893  * Returns: the markup column, or -1 if it's unset.
4894  *
4895  * Since: 2.6
4896  */
4897 gint
4898 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
4899 {
4900   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4901
4902   return icon_view->priv->markup_column;
4903 }
4904
4905 /**
4906  * gtk_icon_view_set_pixbuf_column:
4907  * @icon_view: A #GtkIconView.
4908  * @column: A column in the currently used model.
4909  * 
4910  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
4911  * column must be of type #GDK_TYPE_PIXBUF
4912  *
4913  * Since: 2.6 
4914  **/
4915 void
4916 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
4917                                  gint         column)
4918 {
4919   if (column == icon_view->priv->pixbuf_column)
4920     return;
4921   
4922   if (column == -1)
4923     icon_view->priv->pixbuf_column = -1;
4924   else
4925     {
4926       if (icon_view->priv->model != NULL)
4927         {
4928           GType column_type;
4929           
4930           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4931
4932           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4933         }
4934       
4935       icon_view->priv->pixbuf_column = column;
4936     }
4937
4938   gtk_icon_view_stop_editing (icon_view, TRUE);
4939
4940   update_pixbuf_cell (icon_view);
4941
4942   gtk_icon_view_invalidate_sizes (icon_view);
4943   gtk_icon_view_queue_layout (icon_view);
4944   
4945   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
4946   
4947 }
4948
4949 /**
4950  * gtk_icon_view_get_pixbuf_column:
4951  * @icon_view: A #GtkIconView.
4952  *
4953  * Returns the column with pixbufs for @icon_view.
4954  *
4955  * Returns: the pixbuf column, or -1 if it's unset.
4956  *
4957  * Since: 2.6
4958  */
4959 gint
4960 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
4961 {
4962   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4963
4964   return icon_view->priv->pixbuf_column;
4965 }
4966
4967 /**
4968  * gtk_icon_view_select_path:
4969  * @icon_view: A #GtkIconView.
4970  * @path: The #GtkTreePath to be selected.
4971  * 
4972  * Selects the row at @path.
4973  *
4974  * Since: 2.6
4975  **/
4976 void
4977 gtk_icon_view_select_path (GtkIconView *icon_view,
4978                            GtkTreePath *path)
4979 {
4980   GList *l;
4981
4982   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4983   g_return_if_fail (icon_view->priv->model != NULL);
4984   g_return_if_fail (path != NULL);
4985
4986   l = g_list_nth (icon_view->priv->items,
4987                   gtk_tree_path_get_indices(path)[0]);
4988
4989   if (l != NULL)
4990     gtk_icon_view_select_item (icon_view, l->data);
4991 }
4992
4993 /**
4994  * gtk_icon_view_unselect_path:
4995  * @icon_view: A #GtkIconView.
4996  * @path: The #GtkTreePath to be unselected.
4997  * 
4998  * Unselects the row at @path.
4999  *
5000  * Since: 2.6
5001  **/
5002 void
5003 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5004                              GtkTreePath *path)
5005 {
5006   GtkIconViewItem *item;
5007   
5008   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5009   g_return_if_fail (icon_view->priv->model != NULL);
5010   g_return_if_fail (path != NULL);
5011
5012   item = g_list_nth (icon_view->priv->items,
5013                      gtk_tree_path_get_indices(path)[0])->data;
5014
5015   if (!item)
5016     return;
5017   
5018   gtk_icon_view_unselect_item (icon_view, item);
5019 }
5020
5021 /**
5022  * gtk_icon_view_get_selected_items:
5023  * @icon_view: A #GtkIconView.
5024  *
5025  * Creates a list of paths of all selected items. Additionally, if you are
5026  * planning on modifying the model after calling this function, you may
5027  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5028  * To do this, you can use gtk_tree_row_reference_new().
5029  *
5030  * To free the return value, use:
5031  * <informalexample><programlisting>
5032  * g_list_foreach (list, gtk_tree_path_free, NULL);
5033  * g_list_free (list);
5034  * </programlisting></informalexample>
5035  *
5036  * Return value: A #GList containing a #GtkTreePath for each selected row.
5037  *
5038  * Since: 2.6
5039  **/
5040 GList *
5041 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5042 {
5043   GList *list;
5044   GList *selected = NULL;
5045   
5046   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5047   
5048   for (list = icon_view->priv->items; list != NULL; list = list->next)
5049     {
5050       GtkIconViewItem *item = list->data;
5051
5052       if (item->selected)
5053         {
5054           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5055
5056           selected = g_list_prepend (selected, path);
5057         }
5058     }
5059
5060   return selected;
5061 }
5062
5063 /**
5064  * gtk_icon_view_select_all:
5065  * @icon_view: A #GtkIconView.
5066  * 
5067  * Selects all the icons. @icon_view must has its selection mode set
5068  * to #GTK_SELECTION_MULTIPLE.
5069  *
5070  * Since: 2.6
5071  **/
5072 void
5073 gtk_icon_view_select_all (GtkIconView *icon_view)
5074 {
5075   GList *items;
5076   gboolean dirty = FALSE;
5077   
5078   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5079
5080   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5081     return;
5082
5083   for (items = icon_view->priv->items; items; items = items->next)
5084     {
5085       GtkIconViewItem *item = items->data;
5086       
5087       if (!item->selected)
5088         {
5089           dirty = TRUE;
5090           item->selected = TRUE;
5091           gtk_icon_view_queue_draw_item (icon_view, item);
5092         }
5093     }
5094
5095   if (dirty)
5096     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5097 }
5098
5099 /**
5100  * gtk_icon_view_unselect_all:
5101  * @icon_view: A #GtkIconView.
5102  * 
5103  * Unselects all the icons.
5104  *
5105  * Since: 2.6
5106  **/
5107 void
5108 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5109 {
5110   gboolean dirty = FALSE;
5111   
5112   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5113
5114   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5115     return;
5116
5117   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5118
5119   if (dirty)
5120     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5121 }
5122
5123 /**
5124  * gtk_icon_view_path_is_selected:
5125  * @icon_view: A #GtkIconView.
5126  * @path: A #GtkTreePath to check selection on.
5127  * 
5128  * Returns %TRUE if the icon pointed to by @path is currently
5129  * selected. If @icon does not point to a valid location, %FALSE is returned.
5130  * 
5131  * Return value: %TRUE if @path is selected.
5132  *
5133  * Since: 2.6
5134  **/
5135 gboolean
5136 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5137                                 GtkTreePath *path)
5138 {
5139   GtkIconViewItem *item;
5140   
5141   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5142   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5143   g_return_val_if_fail (path != NULL, FALSE);
5144   
5145   item = g_list_nth (icon_view->priv->items,
5146                      gtk_tree_path_get_indices(path)[0])->data;
5147
5148   if (!item)
5149     return FALSE;
5150   
5151   return item->selected;
5152 }
5153
5154 /**
5155  * gtk_icon_view_item_activated:
5156  * @icon_view: A #GtkIconView
5157  * @path: The #GtkTreePath to be activated
5158  * 
5159  * Activates the item determined by @path.
5160  *
5161  * Since: 2.6
5162  **/
5163 void
5164 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5165                               GtkTreePath      *path)
5166 {
5167   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5168   g_return_if_fail (path != NULL);
5169   
5170   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5171 }
5172
5173 /**
5174  * gtk_icon_view_set_orientation:
5175  * @icon_view: a #GtkIconView
5176  * @orientation: the relative position of texts and icons 
5177  * 
5178  * Sets the ::orientation property which determines whether the labels 
5179  * are drawn beside the icons instead of below.
5180  *
5181  * Since: 2.6
5182  **/
5183 void 
5184 gtk_icon_view_set_orientation (GtkIconView    *icon_view,
5185                                GtkOrientation  orientation)
5186 {
5187   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5188
5189   if (icon_view->priv->orientation != orientation)
5190     {
5191       icon_view->priv->orientation = orientation;
5192
5193       gtk_icon_view_stop_editing (icon_view, TRUE);
5194       gtk_icon_view_invalidate_sizes (icon_view);
5195       gtk_icon_view_queue_layout (icon_view);
5196
5197       update_text_cell (icon_view);
5198       update_pixbuf_cell (icon_view);
5199       
5200       g_object_notify (G_OBJECT (icon_view), "orientation");
5201     }
5202 }
5203
5204 /**
5205  * gtk_icon_view_get_orientation:
5206  * @icon_view: a #GtkIconView
5207  * 
5208  * Returns the value of the ::orientation property which determines 
5209  * whether the labels are drawn beside the icons instead of below. 
5210  * 
5211  * Return value: the relative position of texts and icons 
5212  *
5213  * Since: 2.6
5214  **/
5215 GtkOrientation
5216 gtk_icon_view_get_orientation (GtkIconView *icon_view)
5217 {
5218   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5219                         GTK_ORIENTATION_VERTICAL);
5220
5221   return icon_view->priv->orientation;
5222 }
5223
5224 /**
5225  * gtk_icon_view_set_columns:
5226  * @icon_view: a #GtkIconView
5227  * @columns: the number of columns
5228  * 
5229  * Sets the ::columns property which determines in how
5230  * many columns the icons are arranged. If @columns is
5231  * -1, the number of columns will be chosen automatically 
5232  * to fill the available area. 
5233  *
5234  * Since: 2.6
5235  */
5236 void 
5237 gtk_icon_view_set_columns (GtkIconView *icon_view,
5238                            gint         columns)
5239 {
5240   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5241   
5242   if (icon_view->priv->columns != columns)
5243     {
5244       icon_view->priv->columns = columns;
5245
5246       gtk_icon_view_stop_editing (icon_view, TRUE);
5247       gtk_icon_view_queue_layout (icon_view);
5248       
5249       g_object_notify (G_OBJECT (icon_view), "columns");
5250     }  
5251 }
5252
5253 /**
5254  * gtk_icon_view_get_columns:
5255  * @icon_view: a #GtkIconView
5256  * 
5257  * Returns the value of the ::columns property.
5258  * 
5259  * Return value: the number of columns, or -1
5260  *
5261  * Since: 2.6
5262  */
5263 gint
5264 gtk_icon_view_get_columns (GtkIconView *icon_view)
5265 {
5266   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5267
5268   return icon_view->priv->columns;
5269 }
5270
5271 /**
5272  * gtk_icon_view_set_item_width:
5273  * @icon_view: a #GtkIconView
5274  * @item_width: the width for each item
5275  * 
5276  * Sets the ::item-width property which specifies the width 
5277  * to use for each item. If it is set to -1, the icon view will 
5278  * automatically determine a suitable item size.
5279  *
5280  * Since: 2.6
5281  */
5282 void 
5283 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5284                               gint         item_width)
5285 {
5286   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5287   
5288   if (icon_view->priv->item_width != item_width)
5289     {
5290       icon_view->priv->item_width = item_width;
5291       
5292       gtk_icon_view_stop_editing (icon_view, TRUE);
5293       gtk_icon_view_invalidate_sizes (icon_view);
5294       gtk_icon_view_queue_layout (icon_view);
5295       
5296       update_text_cell (icon_view);
5297
5298       g_object_notify (G_OBJECT (icon_view), "item-width");
5299     }  
5300 }
5301
5302 /**
5303  * gtk_icon_view_get_item_width:
5304  * @icon_view: a #GtkIconView
5305  * 
5306  * Returns the value of the ::item-width property.
5307  * 
5308  * Return value: the width of a single item, or -1
5309  *
5310  * Since: 2.6
5311  */
5312 gint
5313 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5314 {
5315   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5316
5317   return icon_view->priv->item_width;
5318 }
5319
5320
5321 /**
5322  * gtk_icon_view_set_spacing:
5323  * @icon_view: a #GtkIconView
5324  * @spacing: the spacing
5325  * 
5326  * Sets the ::spacing property which specifies the space 
5327  * which is inserted between the cells (i.e. the icon and 
5328  * the text) of an item.
5329  *
5330  * Since: 2.6
5331  */
5332 void 
5333 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5334                            gint         spacing)
5335 {
5336   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5337   
5338   if (icon_view->priv->spacing != spacing)
5339     {
5340       icon_view->priv->spacing = spacing;
5341
5342       gtk_icon_view_stop_editing (icon_view, TRUE);
5343       gtk_icon_view_invalidate_sizes (icon_view);
5344       gtk_icon_view_queue_layout (icon_view);
5345       
5346       g_object_notify (G_OBJECT (icon_view), "spacing");
5347     }  
5348 }
5349
5350 /**
5351  * gtk_icon_view_get_spacing:
5352  * @icon_view: a #GtkIconView
5353  * 
5354  * Returns the value of the ::spacing property.
5355  * 
5356  * Return value: the space between cells 
5357  *
5358  * Since: 2.6
5359  */
5360 gint
5361 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5362 {
5363   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5364
5365   return icon_view->priv->spacing;
5366 }
5367
5368 /**
5369  * gtk_icon_view_set_row_spacing:
5370  * @icon_view: a #GtkIconView
5371  * @row_spacing: the row spacing
5372  * 
5373  * Sets the ::row-spacing property which specifies the space 
5374  * which is inserted between the rows of the icon view.
5375  *
5376  * Since: 2.6
5377  */
5378 void 
5379 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5380                                gint         row_spacing)
5381 {
5382   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5383   
5384   if (icon_view->priv->row_spacing != row_spacing)
5385     {
5386       icon_view->priv->row_spacing = row_spacing;
5387
5388       gtk_icon_view_stop_editing (icon_view, TRUE);
5389       gtk_icon_view_invalidate_sizes (icon_view);
5390       gtk_icon_view_queue_layout (icon_view);
5391       
5392       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5393     }  
5394 }
5395
5396 /**
5397  * gtk_icon_view_get_row_spacing:
5398  * @icon_view: a #GtkIconView
5399  * 
5400  * Returns the value of the ::row-spacing property.
5401  * 
5402  * Return value: the space between rows
5403  *
5404  * Since: 2.6
5405  */
5406 gint
5407 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5408 {
5409   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5410
5411   return icon_view->priv->row_spacing;
5412 }
5413
5414 /**
5415  * gtk_icon_view_set_column_spacing:
5416  * @icon_view: a #GtkIconView
5417  * @column_spacing: the column spacing
5418  * 
5419  * Sets the ::column-spacing property which specifies the space 
5420  * which is inserted between the columns of the icon view.
5421  *
5422  * Since: 2.6
5423  */
5424 void 
5425 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5426                                   gint         column_spacing)
5427 {
5428   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5429   
5430   if (icon_view->priv->column_spacing != column_spacing)
5431     {
5432       icon_view->priv->column_spacing = column_spacing;
5433
5434       gtk_icon_view_stop_editing (icon_view, TRUE);
5435       gtk_icon_view_invalidate_sizes (icon_view);
5436       gtk_icon_view_queue_layout (icon_view);
5437       
5438       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5439     }  
5440 }
5441
5442 /**
5443  * gtk_icon_view_get_column_spacing:
5444  * @icon_view: a #GtkIconView
5445  * 
5446  * Returns the value of the ::column-spacing property.
5447  * 
5448  * Return value: the space between columns
5449  *
5450  * Since: 2.6
5451  */
5452 gint
5453 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5454 {
5455   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5456
5457   return icon_view->priv->column_spacing;
5458 }
5459
5460 /**
5461  * gtk_icon_view_set_margin:
5462  * @icon_view: a #GtkIconView
5463  * @margin: the margin
5464  * 
5465  * Sets the ::margin property which specifies the space 
5466  * which is inserted at the top, bottom, left and right 
5467  * of the icon view.
5468  *
5469  * Since: 2.6
5470  */
5471 void 
5472 gtk_icon_view_set_margin (GtkIconView *icon_view,
5473                           gint         margin)
5474 {
5475   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5476   
5477   if (icon_view->priv->margin != margin)
5478     {
5479       icon_view->priv->margin = margin;
5480
5481       gtk_icon_view_stop_editing (icon_view, TRUE);
5482       gtk_icon_view_invalidate_sizes (icon_view);
5483       gtk_icon_view_queue_layout (icon_view);
5484       
5485       g_object_notify (G_OBJECT (icon_view), "margin");
5486     }  
5487 }
5488
5489 /**
5490  * gtk_icon_view_get_margin:
5491  * @icon_view: a #GtkIconView
5492  * 
5493  * Returns the value of the ::margin property.
5494  * 
5495  * Return value: the space at the borders 
5496  *
5497  * Since: 2.6
5498  */
5499 gint
5500 gtk_icon_view_get_margin (GtkIconView *icon_view)
5501 {
5502   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5503
5504   return icon_view->priv->margin;
5505 }
5506
5507
5508 /* Get/set whether drag_motion requested the drag data and
5509  * drag_data_received should thus not actually insert the data,
5510  * since the data doesn't result from a drop.
5511  */
5512 static void
5513 set_status_pending (GdkDragContext *context,
5514                     GdkDragAction   suggested_action)
5515 {
5516   g_object_set_data (G_OBJECT (context),
5517                      "gtk-icon-view-status-pending",
5518                      GINT_TO_POINTER (suggested_action));
5519 }
5520
5521 static GdkDragAction
5522 get_status_pending (GdkDragContext *context)
5523 {
5524   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5525                                              "gtk-icon-view-status-pending"));
5526 }
5527
5528 static void
5529 unset_reorderable (GtkIconView *icon_view)
5530 {
5531   if (icon_view->priv->reorderable)
5532     {
5533       icon_view->priv->reorderable = FALSE;
5534       g_object_notify (G_OBJECT (icon_view), "reorderable");
5535     }
5536 }
5537
5538 static void
5539 clear_source_info (GtkIconView *icon_view)
5540 {
5541   if (icon_view->priv->source_targets)
5542     gtk_target_list_unref (icon_view->priv->source_targets);
5543   icon_view->priv->source_targets = NULL;
5544
5545   icon_view->priv->source_set = FALSE;
5546 }
5547
5548 static void
5549 clear_dest_info (GtkIconView *icon_view)
5550 {
5551   if (icon_view->priv->dest_targets)
5552     gtk_target_list_unref (icon_view->priv->dest_targets);
5553   icon_view->priv->dest_targets = NULL;
5554
5555   icon_view->priv->dest_set = FALSE;
5556 }
5557
5558 static void
5559 set_source_row (GdkDragContext *context,
5560                 GtkTreeModel   *model,
5561                 GtkTreePath    *source_row)
5562 {
5563   if (source_row)
5564     g_object_set_data_full (G_OBJECT (context),
5565                             "gtk-icon-view-source-row",
5566                             gtk_tree_row_reference_new (model, source_row),
5567                             (GDestroyNotify) gtk_tree_row_reference_free);
5568   else
5569     g_object_set_data_full (G_OBJECT (context),
5570                             "gtk-icon-view-source-row",
5571                             NULL, NULL);
5572 }
5573
5574 static GtkTreePath*
5575 get_source_row (GdkDragContext *context)
5576 {
5577   GtkTreeRowReference *ref;
5578
5579   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5580
5581   if (ref)
5582     return gtk_tree_row_reference_get_path (ref);
5583   else
5584     return NULL;
5585 }
5586
5587 typedef struct
5588 {
5589   GtkTreeRowReference *dest_row;
5590   gboolean             empty_view_drop;
5591   gboolean             drop_append_mode;
5592 } DestRow;
5593
5594 static void
5595 dest_row_free (gpointer data)
5596 {
5597   DestRow *dr = (DestRow *)data;
5598
5599   gtk_tree_row_reference_free (dr->dest_row);
5600   g_free (dr);
5601 }
5602
5603 static void
5604 set_dest_row (GdkDragContext *context,
5605               GtkTreeModel   *model,
5606               GtkTreePath    *dest_row,
5607               gboolean        empty_view_drop,
5608               gboolean        drop_append_mode)
5609 {
5610   DestRow *dr;
5611
5612   if (!dest_row)
5613     {
5614       g_object_set_data_full (G_OBJECT (context),
5615                               "gtk-icon-view-dest-row",
5616                               NULL, NULL);
5617       return;
5618     }
5619   
5620   dr = g_new0 (DestRow, 1);
5621      
5622   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5623   dr->empty_view_drop = empty_view_drop;
5624   dr->drop_append_mode = drop_append_mode;
5625   g_object_set_data_full (G_OBJECT (context),
5626                           "gtk-icon-view-dest-row",
5627                           dr, (GDestroyNotify) dest_row_free);
5628 }
5629
5630 static GtkTreePath*
5631 get_dest_row (GdkDragContext *context)
5632 {
5633   DestRow *dr;
5634
5635   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
5636
5637   if (dr)
5638     {
5639       GtkTreePath *path = NULL;
5640       
5641       if (dr->dest_row)
5642         path = gtk_tree_row_reference_get_path (dr->dest_row);
5643       else if (dr->empty_view_drop)
5644         path = gtk_tree_path_new_from_indices (0, -1);
5645       else
5646         path = NULL;
5647
5648       if (path && dr->drop_append_mode)
5649         gtk_tree_path_next (path);
5650
5651       return path;
5652     }
5653   else
5654     return NULL;
5655 }
5656
5657 static gboolean
5658 check_model_dnd (GtkTreeModel *model,
5659                  GType         required_iface,
5660                  const gchar  *signal)
5661 {
5662   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5663     {
5664       g_warning ("You must override the default '%s' handler "
5665                  "on GtkIconView when using models that don't support "
5666                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5667                  "is to connect to '%s' and call "
5668                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5669                  "the default handler from running. Look at the source code "
5670                  "for the default handler in gtkiconview.c to get an idea what "
5671                  "your handler should do. (gtkiconview.c is in the GTK+ source "
5672                  "code.) If you're using GTK+ from a language other than C, "
5673                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5674                  signal, g_type_name (required_iface), signal);
5675       return FALSE;
5676     }
5677   else
5678     return TRUE;
5679 }
5680
5681 static void
5682 remove_scroll_timeout (GtkIconView *icon_view)
5683 {
5684   if (icon_view->priv->scroll_timeout_id != 0)
5685     {
5686       g_source_remove (icon_view->priv->scroll_timeout_id);
5687
5688       icon_view->priv->scroll_timeout_id = 0;
5689     }
5690 }
5691
5692 static void
5693 gtk_icon_view_autoscroll (GtkIconView *icon_view)
5694 {
5695   gint px, py, x, y, width, height;
5696   gint hoffset, voffset;
5697   gfloat value;
5698
5699   gdk_window_get_pointer (GTK_WIDGET (icon_view)->window, &px, &py, NULL);
5700   gdk_window_get_geometry (GTK_WIDGET (icon_view)->window, &x, &y, &width, &height, NULL);
5701
5702   /* see if we are near the edge. */
5703   voffset = py - (y + 2 * SCROLL_EDGE_SIZE);
5704   if (voffset > 0)
5705     voffset = MAX (py - (y + height - 2 * SCROLL_EDGE_SIZE), 0);
5706
5707   hoffset = px - (x + 2 * SCROLL_EDGE_SIZE);
5708   if (hoffset > 0)
5709     hoffset = MAX (px - (x + width - 2 * SCROLL_EDGE_SIZE), 0);
5710
5711   if (voffset != 0)
5712     {
5713       value = CLAMP (icon_view->priv->vadjustment->value + voffset, 
5714                      icon_view->priv->vadjustment->lower,
5715                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
5716       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
5717     }
5718   if (hoffset != 0)
5719     {
5720       value = CLAMP (icon_view->priv->hadjustment->value + hoffset, 
5721                      icon_view->priv->hadjustment->lower,
5722                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
5723       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
5724     }
5725 }
5726
5727
5728 static gboolean
5729 drag_scroll_timeout (gpointer data)
5730 {
5731   GtkIconView *icon_view = GTK_ICON_VIEW (data);
5732
5733   GDK_THREADS_ENTER ();
5734
5735   gtk_icon_view_autoscroll (icon_view);
5736
5737   GDK_THREADS_LEAVE ();
5738
5739   return TRUE;
5740 }
5741
5742
5743 static gboolean
5744 set_destination (GtkIconView    *icon_view,
5745                  GdkDragContext *context,
5746                  gint            x,
5747                  gint            y,
5748                  GdkDragAction  *suggested_action,
5749                  GdkAtom        *target)
5750 {
5751   GtkWidget *widget;
5752   GtkTreePath *path = NULL;
5753   GtkIconViewDropPosition pos;
5754   GtkIconViewDropPosition old_pos;
5755   GtkTreePath *old_dest_path = NULL;
5756   gboolean can_drop = FALSE;
5757
5758   widget = GTK_WIDGET (icon_view);
5759
5760   *suggested_action = 0;
5761   *target = GDK_NONE;
5762
5763   if (!icon_view->priv->dest_set)
5764     {
5765       /* someone unset us as a drag dest, note that if
5766        * we return FALSE drag_leave isn't called
5767        */
5768
5769       gtk_icon_view_set_drag_dest_item (icon_view,
5770                                         NULL,
5771                                         GTK_ICON_VIEW_DROP_LEFT);
5772
5773       remove_scroll_timeout (GTK_ICON_VIEW (widget));
5774
5775       return FALSE; /* no longer a drop site */
5776     }
5777
5778   *target = gtk_drag_dest_find_target (widget, context, icon_view->priv->dest_targets);
5779   if (*target == GDK_NONE)
5780     return FALSE;
5781
5782   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
5783     {
5784       gint n_children;
5785       GtkTreeModel *model;
5786       
5787       /* the row got dropped on empty space, let's setup a special case
5788        */
5789
5790       if (path)
5791         gtk_tree_path_free (path);
5792
5793       model = gtk_icon_view_get_model (icon_view);
5794
5795       n_children = gtk_tree_model_iter_n_children (model, NULL);
5796       if (n_children)
5797         {
5798           pos = GTK_ICON_VIEW_DROP_BELOW;
5799           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
5800         }
5801       else
5802         {
5803           pos = GTK_ICON_VIEW_DROP_ABOVE;
5804           path = gtk_tree_path_new_from_indices (0, -1);
5805         }
5806
5807       can_drop = TRUE;
5808
5809       goto out;
5810     }
5811
5812   g_assert (path);
5813
5814   gtk_icon_view_get_drag_dest_item (icon_view,
5815                                     &old_dest_path,
5816                                     &old_pos);
5817   
5818   if (old_dest_path)
5819     gtk_tree_path_free (old_dest_path);
5820   
5821   if (TRUE /* FIXME if the location droppable predicate */)
5822     {
5823       can_drop = TRUE;
5824     }
5825
5826 out:
5827   if (can_drop)
5828     {
5829       GtkWidget *source_widget;
5830
5831       *suggested_action = context->suggested_action;
5832       source_widget = gtk_drag_get_source_widget (context);
5833
5834       if (source_widget == widget)
5835         {
5836           /* Default to MOVE, unless the user has
5837            * pressed ctrl or shift to affect available actions
5838            */
5839           if ((context->actions & GDK_ACTION_MOVE) != 0)
5840             *suggested_action = GDK_ACTION_MOVE;
5841         }
5842
5843       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
5844                                         path, pos);
5845     }
5846   else
5847     {
5848       /* can't drop here */
5849       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
5850                                         NULL,
5851                                         GTK_ICON_VIEW_DROP_LEFT);
5852     }
5853   
5854   if (path)
5855     gtk_tree_path_free (path);
5856   
5857   return TRUE;
5858 }
5859
5860 static GtkTreePath*
5861 get_logical_destination (GtkIconView *icon_view,
5862                          gboolean    *drop_append_mode)
5863 {
5864   /* adjust path to point to the row the drop goes in front of */
5865   GtkTreePath *path = NULL;
5866   GtkIconViewDropPosition pos;
5867   
5868   *drop_append_mode = FALSE;
5869
5870   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
5871
5872   if (path == NULL)
5873     return NULL;
5874
5875   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
5876       pos == GTK_ICON_VIEW_DROP_BELOW)
5877     {
5878       GtkTreeIter iter;
5879       GtkTreeModel *model = icon_view->priv->model;
5880
5881       if (!gtk_tree_model_get_iter (model, &iter, path) ||
5882           !gtk_tree_model_iter_next (model, &iter))
5883         *drop_append_mode = TRUE;
5884       else
5885         {
5886           *drop_append_mode = FALSE;
5887           gtk_tree_path_next (path);
5888         }      
5889     }
5890
5891   return path;
5892 }
5893
5894 static gboolean
5895 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
5896                                 GdkEventMotion *event)
5897 {
5898   GdkDragContext *context;
5899   GtkTreePath *path = NULL;
5900   gint button;
5901   GtkTreeModel *model;
5902   gboolean retval = FALSE;
5903
5904   if (!icon_view->priv->source_set)
5905     goto out;
5906
5907   if (icon_view->priv->pressed_button < 0)
5908     goto out;
5909
5910   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
5911                                  icon_view->priv->press_start_x,
5912                                  icon_view->priv->press_start_y,
5913                                  event->x, event->y))
5914     goto out;
5915
5916   model = gtk_icon_view_get_model (icon_view);
5917
5918   if (model == NULL)
5919     goto out;
5920
5921   button = icon_view->priv->pressed_button;
5922   icon_view->priv->pressed_button = -1;
5923
5924   path = gtk_icon_view_get_path_at_pos (icon_view,
5925                                         icon_view->priv->press_start_x,
5926                                         icon_view->priv->press_start_y);
5927
5928   if (path == NULL)
5929     goto out;
5930
5931   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
5932       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
5933                                            path))
5934     goto out;
5935
5936   /* FIXME Check whether we're a start button, if not return FALSE and
5937    * free path
5938    */
5939
5940   /* Now we can begin the drag */
5941   
5942   retval = TRUE;
5943
5944   context = gtk_drag_begin (GTK_WIDGET (icon_view),
5945                             icon_view->priv->source_targets,
5946                             icon_view->priv->source_actions,
5947                             button,
5948                             (GdkEvent*)event);
5949
5950   set_source_row (context, model, path);
5951   
5952  out:
5953   if (path)
5954     gtk_tree_path_free (path);
5955
5956   return retval;
5957 }
5958
5959 /* Source side drag signals */
5960 static void 
5961 gtk_icon_view_drag_begin (GtkWidget      *widget,
5962                           GdkDragContext *context)
5963 {
5964   GtkIconView *icon_view;
5965   GtkIconViewItem *item;
5966   GdkPixmap *icon;
5967   gint x, y;
5968   GtkTreePath *path;
5969
5970   icon_view = GTK_ICON_VIEW (widget);
5971
5972   /* if the user uses a custom DnD impl, we don't set the icon here */
5973   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
5974     return;
5975
5976   item = gtk_icon_view_get_item_at_coords (icon_view,
5977                                            icon_view->priv->press_start_x,
5978                                            icon_view->priv->press_start_y,
5979                                            TRUE,
5980                                            NULL);
5981
5982   g_return_if_fail (item != NULL);
5983
5984   x = icon_view->priv->press_start_x - item->x + 1;
5985   y = icon_view->priv->press_start_y - item->y + 1;
5986   
5987   path = gtk_tree_path_new_from_indices (item->index, -1);
5988   icon = gtk_icon_view_create_drag_icon (icon_view, path);
5989   gtk_tree_path_free (path);
5990
5991   gtk_drag_set_icon_pixmap (context, 
5992                             gdk_drawable_get_colormap (icon),
5993                             icon, 
5994                             NULL, 
5995                             x, y);
5996
5997   g_object_unref (icon);
5998 }
5999
6000 static void 
6001 gtk_icon_view_drag_end (GtkWidget      *widget,
6002                         GdkDragContext *context)
6003 {
6004   /* do nothing */
6005 }
6006
6007 static void 
6008 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6009                              GdkDragContext   *context,
6010                              GtkSelectionData *selection_data,
6011                              guint             info,
6012                              guint             time)
6013 {
6014   GtkIconView *icon_view;
6015   GtkTreeModel *model;
6016   GtkTreePath *source_row;
6017
6018   icon_view = GTK_ICON_VIEW (widget);
6019   model = gtk_icon_view_get_model (icon_view);
6020
6021   if (model == NULL)
6022     return;
6023
6024   if (!icon_view->priv->dest_set)
6025     return;
6026
6027   source_row = get_source_row (context);
6028
6029   if (source_row == NULL)
6030     return;
6031
6032   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6033    * any model; for DragSource models there are some other targets
6034    * we also support.
6035    */
6036
6037   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6038       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6039                                           source_row,
6040                                           selection_data))
6041     goto done;
6042
6043   /* If drag_data_get does nothing, try providing row data. */
6044   if (selection_data->target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
6045     gtk_tree_set_row_drag_data (selection_data,
6046                                 model,
6047                                 source_row);
6048
6049  done:
6050   gtk_tree_path_free (source_row);
6051 }
6052
6053 static void 
6054 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6055                                 GdkDragContext *context)
6056 {
6057   GtkTreeModel *model;
6058   GtkIconView *icon_view;
6059   GtkTreePath *source_row;
6060
6061   icon_view = GTK_ICON_VIEW (widget);
6062   model = gtk_icon_view_get_model (icon_view);
6063
6064   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag_data_delete"))
6065     return;
6066
6067   if (!icon_view->priv->dest_set)
6068     return;
6069
6070   source_row = get_source_row (context);
6071
6072   if (source_row == NULL)
6073     return;
6074
6075   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6076                                          source_row);
6077
6078   gtk_tree_path_free (source_row);
6079
6080   set_source_row (context, NULL, NULL);
6081 }
6082
6083 /* Target side drag signals */
6084 static void
6085 gtk_icon_view_drag_leave (GtkWidget      *widget,
6086                           GdkDragContext *context,
6087                           guint           time)
6088 {
6089   GtkIconView *icon_view;
6090
6091   icon_view = GTK_ICON_VIEW (widget);
6092
6093   /* unset any highlight row */
6094   gtk_icon_view_set_drag_dest_item (icon_view,
6095                                     NULL,
6096                                     GTK_ICON_VIEW_DROP_LEFT);
6097
6098   remove_scroll_timeout (icon_view);
6099 }
6100
6101 static gboolean 
6102 gtk_icon_view_drag_motion (GtkWidget      *widget,
6103                            GdkDragContext *context,
6104                            gint            x,
6105                            gint            y,
6106                            guint           time)
6107 {
6108   GtkTreePath *path = NULL;
6109   GtkTreeModel *model;
6110   GtkIconViewDropPosition pos;
6111   GtkIconView *icon_view;
6112   GdkDragAction suggested_action = 0;
6113   GdkAtom target;
6114   gboolean empty;
6115
6116   icon_view = GTK_ICON_VIEW (widget);
6117
6118   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6119     return FALSE;
6120
6121   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6122
6123   /* we only know this *after* set_desination_row */
6124   model = gtk_icon_view_get_model (icon_view);
6125   empty = icon_view->priv->empty_view_drop;
6126
6127   if (path == NULL && !empty)
6128     {
6129       /* Can't drop here. */
6130       gdk_drag_status (context, 0, time);
6131     }
6132   else
6133     {
6134       if (icon_view->priv->scroll_timeout_id == 0)
6135         {
6136           icon_view->priv->scroll_timeout_id =
6137             g_timeout_add (50, drag_scroll_timeout, icon_view);
6138         }
6139
6140       if (target == gdk_atom_intern ("GTK_TREE_MODEL_ROW", FALSE))
6141         {
6142           /* Request data so we can use the source row when
6143            * determining whether to accept the drop
6144            */
6145           set_status_pending (context, suggested_action);
6146           gtk_drag_get_data (widget, context, target, time);
6147         }
6148       else
6149         {
6150           set_status_pending (context, 0);
6151           gdk_drag_status (context, suggested_action, time);
6152         }
6153     }
6154
6155   if (path)
6156     gtk_tree_path_free (path);
6157
6158   return TRUE;
6159 }
6160
6161 static gboolean 
6162 gtk_icon_view_drag_drop (GtkWidget      *widget,
6163                          GdkDragContext *context,
6164                          gint            x,
6165                          gint            y,
6166                          guint           time)
6167 {
6168   GtkIconView *icon_view;
6169   GtkTreePath *path;
6170   GdkDragAction suggested_action = 0;
6171   GdkAtom target = GDK_NONE;
6172   GtkTreeModel *model;
6173   gboolean drop_append_mode;
6174
6175   icon_view = GTK_ICON_VIEW (widget);
6176   model = gtk_icon_view_get_model (icon_view);
6177
6178   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6179
6180   if (!icon_view->priv->dest_set)
6181     return FALSE;
6182
6183   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_drop"))
6184     return FALSE;
6185
6186   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6187     return FALSE;
6188   
6189   path = get_logical_destination (icon_view, &drop_append_mode);
6190
6191   if (target != GDK_NONE && path != NULL)
6192     {
6193       /* in case a motion had requested drag data, change things so we
6194        * treat drag data receives as a drop.
6195        */
6196       set_status_pending (context, 0);
6197       set_dest_row (context, model, path, 
6198                     icon_view->priv->empty_view_drop, drop_append_mode);
6199     }
6200
6201   if (path)
6202     gtk_tree_path_free (path);
6203
6204   /* Unset this thing */
6205   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6206
6207   if (target != GDK_NONE)
6208     {
6209       gtk_drag_get_data (widget, context, target, time);
6210       return TRUE;
6211     }
6212   else
6213     return FALSE;
6214 }
6215
6216 static void
6217 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6218                                   GdkDragContext   *context,
6219                                   gint              x,
6220                                   gint              y,
6221                                   GtkSelectionData *selection_data,
6222                                   guint             info,
6223                                   guint             time)
6224 {
6225   GtkTreePath *path;
6226   gboolean accepted = FALSE;
6227   GtkTreeModel *model;
6228   GtkIconView *icon_view;
6229   GtkTreePath *dest_row;
6230   GdkDragAction suggested_action;
6231   gboolean drop_append_mode;
6232   
6233   icon_view = GTK_ICON_VIEW (widget);  
6234   model = gtk_icon_view_get_model (icon_view);
6235
6236   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_data_received"))
6237     return;
6238
6239   if (!icon_view->priv->dest_set)
6240     return;
6241
6242   suggested_action = get_status_pending (context);
6243
6244   if (suggested_action)
6245     {
6246       /* We are getting this data due to a request in drag_motion,
6247        * rather than due to a request in drag_drop, so we are just
6248        * supposed to call drag_status, not actually paste in the
6249        * data.
6250        */
6251       path = get_logical_destination (icon_view, &drop_append_mode);
6252
6253       if (path == NULL)
6254         suggested_action = 0;
6255
6256       if (suggested_action)
6257         {
6258           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6259                                                      path,
6260                                                      selection_data))
6261             suggested_action = 0;
6262         }
6263
6264       gdk_drag_status (context, suggested_action, time);
6265
6266       if (path)
6267         gtk_tree_path_free (path);
6268
6269       /* If you can't drop, remove user drop indicator until the next motion */
6270       if (suggested_action == 0)
6271         gtk_icon_view_set_drag_dest_item (icon_view,
6272                                           NULL,
6273                                           GTK_ICON_VIEW_DROP_LEFT);
6274       return;
6275     }
6276   
6277
6278   dest_row = get_dest_row (context);
6279
6280   if (dest_row == NULL)
6281     return;
6282
6283   if (selection_data->length >= 0)
6284     {
6285       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6286                                                  dest_row,
6287                                                  selection_data))
6288         accepted = TRUE;
6289     }
6290
6291   gtk_drag_finish (context,
6292                    accepted,
6293                    (context->action == GDK_ACTION_MOVE),
6294                    time);
6295
6296   gtk_tree_path_free (dest_row);
6297
6298   /* drop dest_row */
6299   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6300 }
6301
6302 /* Drag-and-Drop support */
6303 /**
6304  * gtk_icon_view_enable_model_drag_source:
6305  * @icon_view: a #GtkIconTreeView
6306  * @start_button_mask: Mask of allowed buttons to start drag
6307  * @targets: the table of targets that the drag will support
6308  * @n_targets: the number of items in @targets
6309  * @actions: the bitmask of possible actions for a drag from this
6310  *    widget
6311  * 
6312  * Turns @icon_view into a drag source for automatic DND.
6313  *
6314  * Since: 2.8
6315  **/
6316 void
6317 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6318                                         GdkModifierType           start_button_mask,
6319                                         const GtkTargetEntry     *targets,
6320                                         gint                      n_targets,
6321                                         GdkDragAction             actions)
6322 {
6323   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6324
6325   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, NULL, 0, actions);
6326
6327   clear_source_info (icon_view);
6328   icon_view->priv->start_button_mask = start_button_mask;
6329   icon_view->priv->source_targets = gtk_target_list_new (targets, n_targets);
6330   icon_view->priv->source_actions = actions;
6331
6332   icon_view->priv->source_set = TRUE;
6333
6334   unset_reorderable (icon_view);
6335 }
6336
6337 /**
6338  * gtk_icon_view_enable_model_drag_dest:
6339  * @icon_view: a #GtkIconView
6340  * @targets: the table of targets that the drag will support
6341  * @n_targets: the number of items in @targets
6342  * @actions: the bitmask of possible actions for a drag from this
6343  *    widget
6344  * 
6345  * Turns @icon_view into a drop destination for automatic DND.
6346  *
6347  * Since: 2.8
6348  **/
6349 void 
6350 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6351                                       const GtkTargetEntry *targets,
6352                                       gint                  n_targets,
6353                                       GdkDragAction         actions)
6354 {
6355   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6356
6357   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, NULL, 0, actions);
6358
6359   clear_dest_info (icon_view);
6360
6361   icon_view->priv->dest_targets = gtk_target_list_new (targets, n_targets);
6362   icon_view->priv->dest_actions = actions;
6363
6364   icon_view->priv->dest_set = TRUE;
6365
6366   unset_reorderable (icon_view);  
6367 }
6368
6369 /**
6370  * gtk_icon_view_unset_model_drag_source:
6371  * @icon_view: a #GtkIconView
6372  * 
6373  * Undoes the effect of gtk_icon_view_enable_model_drag_source().
6374  *
6375  * Since: 2.8
6376  **/
6377 void
6378 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6379 {
6380   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6381
6382   if (icon_view->priv->source_set)
6383     {
6384       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6385       clear_source_info (icon_view);
6386     }
6387
6388   unset_reorderable (icon_view);
6389 }
6390
6391 /**
6392  * gtk_icon_view_unset_model_drag_dest:
6393  * @icon_view: a #GtkIconView
6394  * 
6395  * Undoes the effect of gtk_icon_view_enable_model_drag_dest().
6396  *
6397  * Since: 2.8
6398  **/
6399 void
6400 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6401 {
6402   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6403
6404   if (icon_view->priv->dest_set)
6405     {
6406       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6407       clear_dest_info (icon_view);
6408     }
6409
6410   unset_reorderable (icon_view);
6411 }
6412
6413 /* These are useful to implement your own custom stuff. */
6414 /**
6415  * gtk_icon_view_set_drag_dest_item:
6416  * @icon_view: a #GtkIconView
6417  * @path: The path of the item to highlight, or %NULL.
6418  * @pos: Specifies whether to drop, relative to the item
6419  * 
6420  * Sets the item that is highlighted for feedback.
6421  *
6422  * Since: 2.8
6423  */
6424 void
6425 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6426                                   GtkTreePath              *path,
6427                                   GtkIconViewDropPosition   pos)
6428 {
6429   /* Note; this function is exported to allow a custom DND
6430    * implementation, so it can't touch TreeViewDragInfo
6431    */
6432
6433   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6434
6435   if (icon_view->priv->dest_item)
6436     {
6437       GtkTreePath *current_path;
6438       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6439       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6440       icon_view->priv->dest_item = NULL;      
6441
6442       gtk_icon_view_queue_draw_path (icon_view, current_path);
6443       gtk_tree_path_free (current_path);
6444     }
6445   
6446   /* special case a drop on an empty model */
6447   icon_view->priv->empty_view_drop = FALSE;
6448   if (pos == GTK_TREE_VIEW_DROP_BEFORE && path
6449       && gtk_tree_path_get_depth (path) == 1
6450       && gtk_tree_path_get_indices (path)[0] == 0)
6451     {
6452       gint n_children;
6453
6454       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6455                                                    NULL);
6456
6457       if (n_children == 0)
6458         icon_view->priv->empty_view_drop = TRUE;
6459     }
6460
6461   icon_view->priv->dest_pos = pos;
6462
6463   if (path)
6464     {
6465       icon_view->priv->dest_item =
6466         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
6467                                           icon_view->priv->model, path);
6468       
6469       gtk_icon_view_queue_draw_path (icon_view, path);
6470     }
6471 }
6472
6473 /**
6474  * gtk_icon_view_get_drag_dest_item:
6475  * @icon_view: a #GtkIconView
6476  * @path: Return location for the path of the highlighted item, or %NULL.
6477  * @pos: Return location for the drop position, or %NULL
6478  * 
6479  * Gets information about the item that is highlighted for feedback.
6480  *
6481  * Since: 2.8
6482  **/
6483 void
6484 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
6485                                   GtkTreePath             **path,
6486                                   GtkIconViewDropPosition  *pos)
6487 {
6488   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6489
6490   if (path)
6491     {
6492       if (icon_view->priv->dest_item)
6493         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6494       else
6495         *path = NULL;
6496     }
6497
6498   if (pos)
6499     *pos = icon_view->priv->dest_pos;
6500 }
6501
6502 /**
6503  * gtk_icon_view_get_dest_item_at_pos:
6504  * @icon_view: a #GtkIconView
6505  * @drag_x: the position to determine the destination item for
6506  * @drag_y: the position to determine the destination item for
6507  * @path: Return location for the path of the highlighted item, or %NULL.
6508  * @pos: Return location for the drop position, or %NULL
6509  * 
6510  * Determines the destination item for a given position.
6511  * 
6512  * Return value: whether there is an item at the given position.
6513  *
6514  * Since: 2.8
6515  **/
6516 gboolean
6517 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
6518                                     gint                      drag_x,
6519                                     gint                      drag_y,
6520                                     GtkTreePath             **path,
6521                                     GtkIconViewDropPosition  *pos)
6522 {
6523   GtkIconViewItem *item;
6524
6525   /* Note; this function is exported to allow a custom DND
6526    * implementation, so it can't touch TreeViewDragInfo
6527    */
6528
6529   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6530   g_return_val_if_fail (drag_x >= 0, FALSE);
6531   g_return_val_if_fail (drag_y >= 0, FALSE);
6532   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6533
6534
6535   if (path)
6536     *path = NULL;
6537
6538   item = gtk_icon_view_get_item_at_coords (icon_view, 
6539                                            drag_x + icon_view->priv->hadjustment->value, 
6540                                            drag_y + icon_view->priv->vadjustment->value,
6541                                            FALSE, NULL);
6542
6543   if (item == NULL)
6544     return FALSE;
6545
6546   if (path)
6547     *path = gtk_tree_path_new_from_indices (item->index, -1);
6548
6549   if (pos)
6550     {
6551       if (drag_x < item->x + item->width / 4)
6552         *pos = GTK_ICON_VIEW_DROP_LEFT;
6553       else if (drag_x > item->x + item->width * 3 / 4)
6554         *pos = GTK_ICON_VIEW_DROP_RIGHT;
6555       else if (drag_y < item->y + item->height / 4)
6556         *pos = GTK_ICON_VIEW_DROP_ABOVE;
6557       else if (drag_y > item->y + item->height * 3 / 4)
6558         *pos = GTK_ICON_VIEW_DROP_BELOW;
6559       else
6560         *pos = GTK_ICON_VIEW_DROP_INTO;
6561     }
6562
6563   return TRUE;
6564 }
6565
6566 /**
6567  * gtk_icon_view_create_drag_icon:
6568  * @icon_view: a #GtkIconView
6569  * @path: a #GtkTreePath in @icon_view
6570  *
6571  * Creates a #GdkPixmap representation of the item at @path.  
6572  * This image is used for a drag icon.
6573  *
6574  * Return value: a newly-allocated pixmap of the drag icon.
6575  * 
6576  * Since: 2.8
6577  **/
6578 GdkPixmap *
6579 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6580                                 GtkTreePath *path)
6581 {
6582   GtkWidget *widget;
6583   cairo_t *cr;
6584   GdkPixmap *drawable;
6585   GList *l;
6586   gint index;
6587   GdkRectangle area;
6588
6589   widget = GTK_WIDGET (icon_view);
6590
6591   index = gtk_tree_path_get_indices (path)[0];
6592
6593   for (l = icon_view->priv->items; l; l = l->next) 
6594     {
6595       GtkIconViewItem *item = l->data;
6596       
6597       if (index == item->index)
6598         {
6599           drawable = gdk_pixmap_new (icon_view->priv->bin_window,
6600                                      item->width + 2,
6601                                      item->height + 2,
6602                                      -1);
6603
6604           cr = gdk_cairo_create (drawable);
6605           cairo_set_line_width (cr, 1.);
6606
6607           gdk_cairo_set_source_color
6608             (cr, &widget->style->base[GTK_WIDGET_STATE (widget)]);
6609           cairo_rectangle (cr, 0, 0, item->width + 2, item->height + 2);
6610           cairo_fill (cr);
6611
6612           area.x = 0;
6613           area.y = 0;
6614           area.width = item->width;
6615           area.height = item->height;
6616
6617           gtk_icon_view_paint_item (icon_view, cr, item, &area, 
6618                                     drawable, 1, 1, FALSE); 
6619
6620           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
6621           cairo_rectangle (cr, 0.5, 0.5, item->width + 1, item->height + 1);
6622           cairo_stroke (cr);
6623
6624           cairo_destroy (cr);
6625
6626           return drawable;
6627         }
6628     }
6629   
6630   return NULL;
6631 }
6632
6633 /**
6634  * gtk_icon_view_get_reorderable:
6635  * @icon_view: a #GtkIconView
6636  *
6637  * Retrieves whether the user can reorder the list via drag-and-drop. 
6638  * See gtk_icon_view_set_reorderable().
6639  *
6640  * Return value: %TRUE if the list can be reordered.
6641  *
6642  * Since: 3.8
6643  **/
6644 gboolean
6645 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
6646 {
6647   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6648
6649   return icon_view->priv->reorderable;
6650 }
6651
6652 static const GtkTargetEntry item_targets[] = {
6653   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
6654 };
6655
6656
6657 /**
6658  * gtk_icon_view_set_reorderable:
6659  * @icon_view: A #GtkIconView.
6660  * @reorderable: %TRUE, if the list of items can be reordered.
6661  *
6662  * This function is a convenience function to allow you to reorder models that
6663  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
6664  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
6665  * the user can reorder the model by dragging and dropping rows.  The
6666  * developer can listen to these changes by connecting to the model's
6667  * row_inserted and row_deleted signals.
6668  *
6669  * This function does not give you any degree of control over the order -- any
6670  * reordering is allowed.  If more control is needed, you should probably
6671  * handle drag and drop manually.
6672  *
6673  * Since: 2.8
6674  **/
6675 void
6676 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
6677                                gboolean     reorderable)
6678 {
6679   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6680
6681   reorderable = reorderable != FALSE;
6682
6683   if (icon_view->priv->reorderable == reorderable)
6684     return;
6685
6686   if (reorderable)
6687     {
6688       gtk_icon_view_enable_model_drag_source (icon_view,
6689                                               GDK_BUTTON1_MASK,
6690                                               item_targets,
6691                                               G_N_ELEMENTS (item_targets),
6692                                               GDK_ACTION_MOVE);
6693       gtk_icon_view_enable_model_drag_dest (icon_view,
6694                                             item_targets,
6695                                             G_N_ELEMENTS (item_targets),
6696                                             GDK_ACTION_MOVE);
6697     }
6698   else
6699     {
6700       gtk_icon_view_unset_model_drag_source (icon_view);
6701       gtk_icon_view_unset_model_drag_dest (icon_view);
6702     }
6703
6704   icon_view->priv->reorderable = reorderable;
6705
6706   g_object_notify (G_OBJECT (icon_view), "reorderable");
6707 }
6708
6709
6710 /* Accessibility Support */
6711
6712 static gpointer accessible_parent_class;
6713 static gpointer accessible_item_parent_class;
6714 static GQuark accessible_private_data_quark = 0;
6715
6716 #define GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE      (gtk_icon_view_item_accessible_get_type ())
6717 #define GTK_ICON_VIEW_ITEM_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE, GtkIconViewItemAccessible))
6718 #define GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE))
6719
6720 static GType gtk_icon_view_item_accessible_get_type (void);
6721
6722 enum {
6723     ACTION_ACTIVATE,
6724     LAST_ACTION
6725 };
6726
6727 typedef struct
6728 {
6729   AtkObject parent;
6730
6731   GtkIconViewItem *item;
6732
6733   GtkWidget *widget;
6734
6735   AtkStateSet *state_set;
6736
6737   gchar *text;
6738
6739   GtkTextBuffer *text_buffer;
6740
6741   gchar *action_descriptions[LAST_ACTION];
6742   gchar *image_description;
6743   guint action_idle_handler;
6744 } GtkIconViewItemAccessible;
6745
6746 static const gchar *const gtk_icon_view_item_accessible_action_names[] = 
6747 {
6748   "activate",
6749   NULL
6750 };
6751
6752 static const gchar *const gtk_icon_view_item_accessible_action_descriptions[] =
6753 {
6754   "Activate item",
6755   NULL
6756 };
6757 typedef struct _GtkIconViewItemAccessibleClass
6758 {
6759   AtkObjectClass parent_class;
6760
6761 } GtkIconViewItemAccessibleClass;
6762
6763 static gboolean gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item);
6764
6765 static gboolean
6766 gtk_icon_view_item_accessible_idle_do_action (gpointer data)
6767 {
6768   GtkIconViewItemAccessible *item;
6769   GtkIconView *icon_view;
6770   GtkTreePath *path;
6771
6772   GDK_THREADS_ENTER ();
6773
6774   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (data);
6775   item->action_idle_handler = 0;
6776
6777   if (item->widget != NULL)
6778     {
6779       icon_view = GTK_ICON_VIEW (item->widget);
6780       path = gtk_tree_path_new_from_indices (item->item->index, -1);
6781       gtk_icon_view_item_activated (icon_view, path);
6782       gtk_tree_path_free (path);
6783     }
6784
6785   GDK_THREADS_LEAVE ();
6786
6787   return FALSE;
6788 }
6789
6790 static gboolean
6791 gtk_icon_view_item_accessible_action_do_action (AtkAction *action,
6792                                                 gint       i)
6793 {
6794   GtkIconViewItemAccessible *item;
6795   GtkIconView *icon_view;
6796
6797   if (i < 0 || i >= LAST_ACTION) 
6798     return FALSE;
6799
6800   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6801
6802   if (!GTK_IS_ICON_VIEW (item->widget))
6803     return FALSE;
6804
6805   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6806     return FALSE;
6807
6808   icon_view = GTK_ICON_VIEW (item->widget);
6809
6810   switch (i)
6811     {
6812     case ACTION_ACTIVATE:
6813       if (!item->action_idle_handler)
6814         item->action_idle_handler = g_idle_add (gtk_icon_view_item_accessible_idle_do_action, item);
6815       break;
6816     default:
6817       g_assert_not_reached ();
6818       return FALSE;
6819
6820     }        
6821   return TRUE;
6822 }
6823
6824 static gint
6825 gtk_icon_view_item_accessible_action_get_n_actions (AtkAction *action)
6826 {
6827         return LAST_ACTION;
6828 }
6829
6830 static const gchar *
6831 gtk_icon_view_item_accessible_action_get_description (AtkAction *action,
6832                                                       gint       i)
6833 {
6834   GtkIconViewItemAccessible *item;
6835
6836   if (i < 0 || i >= LAST_ACTION) 
6837     return NULL;
6838
6839   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6840
6841   if (item->action_descriptions[i])
6842     return item->action_descriptions[i];
6843   else
6844     return gtk_icon_view_item_accessible_action_descriptions[i];
6845 }
6846
6847 static const gchar *
6848 gtk_icon_view_item_accessible_action_get_name (AtkAction *action,
6849                                                gint       i)
6850 {
6851   if (i < 0 || i >= LAST_ACTION) 
6852     return NULL;
6853
6854   return gtk_icon_view_item_accessible_action_names[i];
6855 }
6856
6857 static gboolean
6858 gtk_icon_view_item_accessible_action_set_description (AtkAction   *action,
6859                                                       gint         i,
6860                                                       const gchar *description)
6861 {
6862   GtkIconViewItemAccessible *item;
6863
6864   if (i < 0 || i >= LAST_ACTION) 
6865     return FALSE;
6866
6867   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6868
6869   if (item->action_descriptions[i])
6870     g_free (item->action_descriptions[i]);
6871
6872   item->action_descriptions[i] = g_strdup (description);
6873
6874   return TRUE;
6875 }
6876
6877 static void
6878 atk_action_item_interface_init (AtkActionIface *iface)
6879 {
6880   iface->do_action = gtk_icon_view_item_accessible_action_do_action;
6881   iface->get_n_actions = gtk_icon_view_item_accessible_action_get_n_actions;
6882   iface->get_description = gtk_icon_view_item_accessible_action_get_description;
6883   iface->get_name = gtk_icon_view_item_accessible_action_get_name;
6884   iface->set_description = gtk_icon_view_item_accessible_action_set_description;
6885 }
6886
6887 static const gchar *
6888 gtk_icon_view_item_accessible_image_get_image_description (AtkImage *image)
6889 {
6890   GtkIconViewItemAccessible *item;
6891
6892   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6893
6894   return item->image_description;
6895 }
6896
6897 static gboolean
6898 gtk_icon_view_item_accessible_image_set_image_description (AtkImage    *image,
6899                                                            const gchar *description)
6900 {
6901   GtkIconViewItemAccessible *item;
6902
6903   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6904
6905   g_free (item->image_description);
6906   item->image_description = g_strdup (item->image_description);
6907
6908   return TRUE;
6909 }
6910
6911 static gboolean
6912 get_pixbuf_box (GtkIconView     *icon_view,
6913                 GtkIconViewItem *item,
6914                 GdkRectangle    *box)
6915 {
6916   GList *l;
6917
6918   for (l = icon_view->priv->cell_list; l; l = l->next)
6919     {
6920       GtkIconViewCellInfo *info = l->data;
6921       
6922       if (GTK_IS_CELL_RENDERER_PIXBUF (info->cell))
6923         {
6924           gtk_icon_view_get_cell_box (icon_view, item, info, box);
6925
6926           return TRUE;
6927         }
6928     }
6929
6930   return FALSE;
6931 }
6932
6933 static gchar *
6934 get_text (GtkIconView     *icon_view,
6935           GtkIconViewItem *item)
6936 {
6937   GList *l;
6938   gchar *text;
6939
6940   for (l = icon_view->priv->cell_list; l; l = l->next)
6941     {
6942       GtkIconViewCellInfo *info = l->data;
6943       
6944       if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
6945         {
6946           g_object_get (info->cell, "text", &text, NULL);
6947           
6948           return text;
6949         }
6950     }
6951
6952   return NULL;
6953 }
6954
6955 static void
6956 gtk_icon_view_item_accessible_image_get_image_size (AtkImage *image,
6957                                                     gint     *width,
6958                                                     gint     *height)
6959 {
6960   GtkIconViewItemAccessible *item;
6961   GdkRectangle box;
6962
6963   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6964
6965   if (!GTK_IS_ICON_VIEW (item->widget))
6966     return;
6967
6968   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6969     return;
6970
6971   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
6972     {
6973       *width = box.width;
6974       *height = box.height;  
6975     }
6976 }
6977
6978 static void
6979 gtk_icon_view_item_accessible_image_get_image_position (AtkImage    *image,
6980                                                         gint        *x,
6981                                                         gint        *y,
6982                                                         AtkCoordType coord_type)
6983 {
6984   GtkIconViewItemAccessible *item;
6985   GdkRectangle box;
6986
6987   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6988
6989   if (!GTK_IS_ICON_VIEW (item->widget))
6990     return;
6991
6992   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6993     return;
6994
6995   atk_component_get_position (ATK_COMPONENT (image), x, y, coord_type);
6996
6997   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
6998     {
6999       *x+= box.x - item->item->x;
7000       *y+= box.y - item->item->y;
7001     }
7002
7003 }
7004
7005 static void
7006 atk_image_item_interface_init (AtkImageIface *iface)
7007 {
7008   iface->get_image_description = gtk_icon_view_item_accessible_image_get_image_description;
7009   iface->set_image_description = gtk_icon_view_item_accessible_image_set_image_description;
7010   iface->get_image_size = gtk_icon_view_item_accessible_image_get_image_size;
7011   iface->get_image_position = gtk_icon_view_item_accessible_image_get_image_position;
7012 }
7013
7014 static gchar *
7015 gtk_icon_view_item_accessible_text_get_text (AtkText *text,
7016                                              gint     start_pos,
7017                                              gint     end_pos)
7018 {
7019   GtkIconViewItemAccessible *item;
7020   GtkTextIter start, end;
7021   GtkTextBuffer *buffer;
7022
7023   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7024
7025   if (!GTK_IS_ICON_VIEW (item->widget))
7026     return NULL;
7027
7028   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7029     return NULL;
7030
7031   buffer = item->text_buffer;
7032   gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
7033   if (end_pos < 0)
7034     gtk_text_buffer_get_end_iter (buffer, &end);
7035   else
7036     gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
7037
7038   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7039 }
7040
7041 static gunichar
7042 gtk_icon_view_item_accessible_text_get_character_at_offset (AtkText *text,
7043                                                             gint     offset)
7044 {
7045   GtkIconViewItemAccessible *item;
7046   GtkTextIter start, end;
7047   GtkTextBuffer *buffer;
7048   gchar *string;
7049   gunichar unichar;
7050
7051   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7052
7053   if (!GTK_IS_ICON_VIEW (item->widget))
7054     return '\0';
7055
7056   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7057     return '\0';
7058
7059   buffer = item->text_buffer;
7060   if (offset >= gtk_text_buffer_get_char_count (buffer))
7061     return '\0';
7062
7063   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7064   end = start;
7065   gtk_text_iter_forward_char (&end);
7066   string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
7067   unichar = g_utf8_get_char (string);
7068   g_free(string);
7069
7070   return unichar;
7071 }
7072
7073 static void
7074 get_pango_text_offsets (PangoLayout     *layout,
7075                         GtkTextBuffer   *buffer,
7076                         gint             function,
7077                         AtkTextBoundary  boundary_type,
7078                         gint             offset,
7079                         gint            *start_offset,
7080                         gint            *end_offset,
7081                         GtkTextIter     *start_iter,
7082                         GtkTextIter     *end_iter)
7083 {
7084   PangoLayoutIter *iter;
7085   PangoLayoutLine *line, *prev_line = NULL, *prev_prev_line = NULL;
7086   gint index, start_index, end_index;
7087   const gchar *text;
7088   gboolean found = FALSE;
7089
7090   text = pango_layout_get_text (layout);
7091   index = g_utf8_offset_to_pointer (text, offset) - text;
7092   iter = pango_layout_get_iter (layout);
7093   do
7094     {
7095       line = pango_layout_iter_get_line (iter);
7096       start_index = line->start_index;
7097       end_index = start_index + line->length;
7098
7099       if (index >= start_index && index <= end_index)
7100         {
7101           /*
7102            * Found line for offset
7103            */
7104           switch (function)
7105             {
7106             case 0:
7107                   /*
7108                    * We want the previous line
7109                    */
7110               if (prev_line)
7111                 {
7112                   switch (boundary_type)
7113                     {
7114                     case ATK_TEXT_BOUNDARY_LINE_START:
7115                       end_index = start_index;
7116                       start_index = prev_line->start_index;
7117                       break;
7118                     case ATK_TEXT_BOUNDARY_LINE_END:
7119                       if (prev_prev_line)
7120                         start_index = prev_prev_line->start_index + 
7121                                   prev_prev_line->length;
7122                       end_index = prev_line->start_index + prev_line->length;
7123                       break;
7124                     default:
7125                       g_assert_not_reached();
7126                     }
7127                 }
7128               else
7129                 start_index = end_index = 0;
7130               break;
7131             case 1:
7132               switch (boundary_type)
7133                 {
7134                 case ATK_TEXT_BOUNDARY_LINE_START:
7135                   if (pango_layout_iter_next_line (iter))
7136                     end_index = pango_layout_iter_get_line (iter)->start_index;
7137                   break;
7138                 case ATK_TEXT_BOUNDARY_LINE_END:
7139                   if (prev_line)
7140                     start_index = prev_line->start_index + 
7141                                   prev_line->length;
7142                   break;
7143                 default:
7144                   g_assert_not_reached();
7145                 }
7146               break;
7147             case 2:
7148                /*
7149                 * We want the next line
7150                 */
7151               if (pango_layout_iter_next_line (iter))
7152                 {
7153                   line = pango_layout_iter_get_line (iter);
7154                   switch (boundary_type)
7155                     {
7156                     case ATK_TEXT_BOUNDARY_LINE_START:
7157                       start_index = line->start_index;
7158                       if (pango_layout_iter_next_line (iter))
7159                         end_index = pango_layout_iter_get_line (iter)->start_index;
7160                       else
7161                         end_index = start_index + line->length;
7162                       break;
7163                     case ATK_TEXT_BOUNDARY_LINE_END:
7164                       start_index = end_index;
7165                       end_index = line->start_index + line->length;
7166                       break;
7167                     default:
7168                       g_assert_not_reached();
7169                     }
7170                 }
7171               else
7172                 start_index = end_index;
7173               break;
7174             }
7175           found = TRUE;
7176           break;
7177         }
7178       prev_prev_line = prev_line; 
7179       prev_line = line; 
7180     }
7181   while (pango_layout_iter_next_line (iter));
7182
7183   if (!found)
7184     {
7185       start_index = prev_line->start_index + prev_line->length;
7186       end_index = start_index;
7187     }
7188   pango_layout_iter_free (iter);
7189   *start_offset = g_utf8_pointer_to_offset (text, text + start_index);
7190   *end_offset = g_utf8_pointer_to_offset (text, text + end_index);
7191  
7192   gtk_text_buffer_get_iter_at_offset (buffer, start_iter, *start_offset);
7193   gtk_text_buffer_get_iter_at_offset (buffer, end_iter, *end_offset);
7194 }
7195
7196 static gchar*
7197 gtk_icon_view_item_accessible_text_get_text_before_offset (AtkText         *text,
7198                                                            gint            offset,
7199                                                            AtkTextBoundary boundary_type,
7200                                                            gint            *start_offset,
7201                                                            gint            *end_offset)
7202 {
7203   GtkIconViewItemAccessible *item;
7204   GtkTextIter start, end;
7205   GtkTextBuffer *buffer;
7206   GtkIconView *icon_view;
7207
7208   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7209
7210   if (!GTK_IS_ICON_VIEW (item->widget))
7211     return NULL;
7212
7213   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7214     return NULL;
7215
7216   buffer = item->text_buffer;
7217
7218   if (!gtk_text_buffer_get_char_count (buffer))
7219     {
7220       *start_offset = 0;
7221       *end_offset = 0;
7222       return g_strdup ("");
7223     }
7224   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7225    
7226   end = start;
7227
7228   switch (boundary_type)
7229     {
7230     case ATK_TEXT_BOUNDARY_CHAR:
7231       gtk_text_iter_backward_char(&start);
7232       break;
7233     case ATK_TEXT_BOUNDARY_WORD_START:
7234       if (!gtk_text_iter_starts_word (&start))
7235         gtk_text_iter_backward_word_start (&start);
7236       end = start;
7237       gtk_text_iter_backward_word_start(&start);
7238       break;
7239     case ATK_TEXT_BOUNDARY_WORD_END:
7240       if (gtk_text_iter_inside_word (&start) &&
7241           !gtk_text_iter_starts_word (&start))
7242         gtk_text_iter_backward_word_start (&start);
7243       while (!gtk_text_iter_ends_word (&start))
7244         {
7245           if (!gtk_text_iter_backward_char (&start))
7246             break;
7247         }
7248       end = start;
7249       gtk_text_iter_backward_word_start(&start);
7250       while (!gtk_text_iter_ends_word (&start))
7251         {
7252           if (!gtk_text_iter_backward_char (&start))
7253             break;
7254         }
7255       break;
7256     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7257       if (!gtk_text_iter_starts_sentence (&start))
7258         gtk_text_iter_backward_sentence_start (&start);
7259       end = start;
7260       gtk_text_iter_backward_sentence_start (&start);
7261       break;
7262     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7263       if (gtk_text_iter_inside_sentence (&start) &&
7264           !gtk_text_iter_starts_sentence (&start))
7265         gtk_text_iter_backward_sentence_start (&start);
7266       while (!gtk_text_iter_ends_sentence (&start))
7267         {
7268           if (!gtk_text_iter_backward_char (&start))
7269             break;
7270         }
7271       end = start;
7272       gtk_text_iter_backward_sentence_start (&start);
7273       while (!gtk_text_iter_ends_sentence (&start))
7274         {
7275           if (!gtk_text_iter_backward_char (&start))
7276             break;
7277         }
7278       break;
7279    case ATK_TEXT_BOUNDARY_LINE_START:
7280    case ATK_TEXT_BOUNDARY_LINE_END:
7281       icon_view = GTK_ICON_VIEW (item->widget);
7282 #if 0
7283       /* FIXME we probably have to use GailTextCell to salvage this */
7284       gtk_icon_view_update_item_text (icon_view, item->item);
7285       get_pango_text_offsets (icon_view->priv->layout,
7286                               buffer,
7287                               0,
7288                               boundary_type,
7289                               offset,
7290                               start_offset,
7291                               end_offset,
7292                               &start,
7293                               &end);
7294 #endif
7295       break;
7296     }
7297
7298   *start_offset = gtk_text_iter_get_offset (&start);
7299   *end_offset = gtk_text_iter_get_offset (&end);
7300
7301   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7302 }
7303
7304 static gchar*
7305 gtk_icon_view_item_accessible_text_get_text_at_offset (AtkText         *text,
7306                                                        gint            offset,
7307                                                        AtkTextBoundary boundary_type,
7308                                                        gint            *start_offset,
7309                                                        gint            *end_offset)
7310 {
7311   GtkIconViewItemAccessible *item;
7312   GtkTextIter start, end;
7313   GtkTextBuffer *buffer;
7314   GtkIconView *icon_view;
7315
7316   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7317
7318   if (!GTK_IS_ICON_VIEW (item->widget))
7319     return NULL;
7320
7321   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7322     return NULL;
7323
7324   buffer = item->text_buffer;
7325
7326   if (!gtk_text_buffer_get_char_count (buffer))
7327     {
7328       *start_offset = 0;
7329       *end_offset = 0;
7330       return g_strdup ("");
7331     }
7332   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7333    
7334   end = start;
7335
7336   switch (boundary_type)
7337     {
7338     case ATK_TEXT_BOUNDARY_CHAR:
7339       gtk_text_iter_forward_char (&end);
7340       break;
7341     case ATK_TEXT_BOUNDARY_WORD_START:
7342       if (!gtk_text_iter_starts_word (&start))
7343         gtk_text_iter_backward_word_start (&start);
7344       if (gtk_text_iter_inside_word (&end))
7345         gtk_text_iter_forward_word_end (&end);
7346       while (!gtk_text_iter_starts_word (&end))
7347         {
7348           if (!gtk_text_iter_forward_char (&end))
7349             break;
7350         }
7351       break;
7352     case ATK_TEXT_BOUNDARY_WORD_END:
7353       if (gtk_text_iter_inside_word (&start) &&
7354           !gtk_text_iter_starts_word (&start))
7355         gtk_text_iter_backward_word_start (&start);
7356       while (!gtk_text_iter_ends_word (&start))
7357         {
7358           if (!gtk_text_iter_backward_char (&start))
7359             break;
7360         }
7361       gtk_text_iter_forward_word_end (&end);
7362       break;
7363     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7364       if (!gtk_text_iter_starts_sentence (&start))
7365         gtk_text_iter_backward_sentence_start (&start);
7366       if (gtk_text_iter_inside_sentence (&end))
7367         gtk_text_iter_forward_sentence_end (&end);
7368       while (!gtk_text_iter_starts_sentence (&end))
7369         {
7370           if (!gtk_text_iter_forward_char (&end))
7371             break;
7372         }
7373       break;
7374     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7375       if (gtk_text_iter_inside_sentence (&start) &&
7376           !gtk_text_iter_starts_sentence (&start))
7377         gtk_text_iter_backward_sentence_start (&start);
7378       while (!gtk_text_iter_ends_sentence (&start))
7379         {
7380           if (!gtk_text_iter_backward_char (&start))
7381             break;
7382         }
7383       gtk_text_iter_forward_sentence_end (&end);
7384       break;
7385    case ATK_TEXT_BOUNDARY_LINE_START:
7386    case ATK_TEXT_BOUNDARY_LINE_END:
7387       icon_view = GTK_ICON_VIEW (item->widget);
7388 #if 0
7389       /* FIXME we probably have to use GailTextCell to salvage this */
7390       gtk_icon_view_update_item_text (icon_view, item->item);
7391       get_pango_text_offsets (icon_view->priv->layout,
7392                               buffer,
7393                               1,
7394                               boundary_type,
7395                               offset,
7396                               start_offset,
7397                               end_offset,
7398                               &start,
7399                               &end);
7400 #endif
7401       break;
7402     }
7403
7404
7405   *start_offset = gtk_text_iter_get_offset (&start);
7406   *end_offset = gtk_text_iter_get_offset (&end);
7407
7408   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7409 }
7410
7411 static gchar*
7412 gtk_icon_view_item_accessible_text_get_text_after_offset (AtkText         *text,
7413                                                           gint            offset,
7414                                                           AtkTextBoundary boundary_type,
7415                                                           gint            *start_offset,
7416                                                           gint            *end_offset)
7417 {
7418   GtkIconViewItemAccessible *item;
7419   GtkTextIter start, end;
7420   GtkTextBuffer *buffer;
7421   GtkIconView *icon_view;
7422
7423   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7424
7425   if (!GTK_IS_ICON_VIEW (item->widget))
7426     return NULL;
7427
7428   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7429     return NULL;
7430
7431   buffer = item->text_buffer;
7432
7433   if (!gtk_text_buffer_get_char_count (buffer))
7434     {
7435       *start_offset = 0;
7436       *end_offset = 0;
7437       return g_strdup ("");
7438     }
7439   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7440    
7441   end = start;
7442
7443   switch (boundary_type)
7444     {
7445     case ATK_TEXT_BOUNDARY_CHAR:
7446       gtk_text_iter_forward_char(&start);
7447       gtk_text_iter_forward_chars(&end, 2);
7448       break;
7449     case ATK_TEXT_BOUNDARY_WORD_START:
7450       if (gtk_text_iter_inside_word (&end))
7451         gtk_text_iter_forward_word_end (&end);
7452       while (!gtk_text_iter_starts_word (&end))
7453         {
7454           if (!gtk_text_iter_forward_char (&end))
7455             break;
7456         }
7457       start = end;
7458       if (!gtk_text_iter_is_end (&end))
7459         {
7460           gtk_text_iter_forward_word_end (&end);
7461           while (!gtk_text_iter_starts_word (&end))
7462             {
7463               if (!gtk_text_iter_forward_char (&end))
7464                 break;
7465             }
7466         }
7467       break;
7468     case ATK_TEXT_BOUNDARY_WORD_END:
7469       gtk_text_iter_forward_word_end (&end);
7470       start = end;
7471       if (!gtk_text_iter_is_end (&end))
7472         gtk_text_iter_forward_word_end (&end);
7473       break;
7474     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7475       if (gtk_text_iter_inside_sentence (&end))
7476         gtk_text_iter_forward_sentence_end (&end);
7477       while (!gtk_text_iter_starts_sentence (&end))
7478         {
7479           if (!gtk_text_iter_forward_char (&end))
7480             break;
7481         }
7482       start = end;
7483       if (!gtk_text_iter_is_end (&end))
7484         {
7485           gtk_text_iter_forward_sentence_end (&end);
7486           while (!gtk_text_iter_starts_sentence (&end))
7487             {
7488               if (!gtk_text_iter_forward_char (&end))
7489                 break;
7490             }
7491         }
7492       break;
7493     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7494       gtk_text_iter_forward_sentence_end (&end);
7495       start = end;
7496       if (!gtk_text_iter_is_end (&end))
7497         gtk_text_iter_forward_sentence_end (&end);
7498       break;
7499    case ATK_TEXT_BOUNDARY_LINE_START:
7500    case ATK_TEXT_BOUNDARY_LINE_END:
7501       icon_view = GTK_ICON_VIEW (item->widget);
7502 #if 0
7503       /* FIXME we probably have to use GailTextCell to salvage this */
7504       gtk_icon_view_update_item_text (icon_view, item->item);
7505       get_pango_text_offsets (icon_view->priv->layout,
7506                               buffer,
7507                               2,
7508                               boundary_type,
7509                               offset,
7510                               start_offset,
7511                               end_offset,
7512                               &start,
7513                               &end);
7514 #endif
7515       break;
7516     }
7517   *start_offset = gtk_text_iter_get_offset (&start);
7518   *end_offset = gtk_text_iter_get_offset (&end);
7519
7520   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7521 }
7522
7523 static gint
7524 gtk_icon_view_item_accessible_text_get_character_count (AtkText *text)
7525 {
7526   GtkIconViewItemAccessible *item;
7527
7528   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7529
7530   if (!GTK_IS_ICON_VIEW (item->widget))
7531     return 0;
7532
7533   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7534     return 0;
7535
7536   return gtk_text_buffer_get_char_count (item->text_buffer);
7537 }
7538
7539 static void
7540 gtk_icon_view_item_accessible_text_get_character_extents (AtkText      *text,
7541                                                           gint         offset,
7542                                                           gint         *x,
7543                                                           gint         *y,
7544                                                           gint         *width,
7545                                                           gint         *height,
7546                                                           AtkCoordType coord_type)
7547 {
7548   GtkIconViewItemAccessible *item;
7549   GtkIconView *icon_view;
7550   PangoRectangle char_rect;
7551   const gchar *item_text;
7552   gint index;
7553
7554   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7555
7556   if (!GTK_IS_ICON_VIEW (item->widget))
7557     return;
7558
7559   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7560     return;
7561
7562   icon_view = GTK_ICON_VIEW (item->widget);
7563 #if 0
7564       /* FIXME we probably have to use GailTextCell to salvage this */
7565   gtk_icon_view_update_item_text (icon_view, item->item);
7566   item_text = pango_layout_get_text (icon_view->priv->layout);
7567   index = g_utf8_offset_to_pointer (item_text, offset) - item_text;
7568   pango_layout_index_to_pos (icon_view->priv->layout, index, &char_rect);
7569
7570   atk_component_get_position (ATK_COMPONENT (text), x, y, coord_type);
7571   *x += item->item->layout_x - item->item->x + char_rect.x / PANGO_SCALE;
7572   /* Look at gtk_icon_view_paint_item() to see where the text is. */
7573   *x -=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
7574   *y += item->item->layout_y - item->item->y + char_rect.y / PANGO_SCALE;
7575   *width = char_rect.width / PANGO_SCALE;
7576   *height = char_rect.height / PANGO_SCALE;
7577 #endif
7578 }
7579
7580 static gint
7581 gtk_icon_view_item_accessible_text_get_offset_at_point (AtkText      *text,
7582                                                         gint          x,
7583                                                         gint          y,
7584                                                         AtkCoordType coord_type)
7585 {
7586   GtkIconViewItemAccessible *item;
7587   GtkIconView *icon_view;
7588   const gchar *item_text;
7589   gint index;
7590   gint offset;
7591   gint l_x, l_y;
7592
7593   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7594
7595   if (!GTK_IS_ICON_VIEW (item->widget))
7596     return -1;
7597
7598   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7599     return -1;
7600
7601   icon_view = GTK_ICON_VIEW (item->widget);
7602 #if 0
7603       /* FIXME we probably have to use GailTextCell to salvage this */
7604   gtk_icon_view_update_item_text (icon_view, item->item);
7605   atk_component_get_position (ATK_COMPONENT (text), &l_x, &l_y, coord_type);
7606   x -= l_x + item->item->layout_x - item->item->x;
7607   x +=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
7608   y -= l_y + item->item->layout_y - item->item->y;
7609   item_text = pango_layout_get_text (icon_view->priv->layout);
7610   if (!pango_layout_xy_to_index (icon_view->priv->layout, 
7611                                 x * PANGO_SCALE,
7612                                 y * PANGO_SCALE,
7613                                 &index, NULL))
7614     {
7615       if (x < 0 || y < 0)
7616         index = 0;
7617       else
7618         index = -1;
7619     } 
7620   if (index == -1)
7621     offset = g_utf8_strlen (item_text, -1);
7622   else
7623     offset = g_utf8_pointer_to_offset (item_text, item_text + index);
7624 #endif
7625   return offset;
7626 }
7627
7628 static void
7629 atk_text_item_interface_init (AtkTextIface *iface)
7630 {
7631   iface->get_text = gtk_icon_view_item_accessible_text_get_text;
7632   iface->get_character_at_offset = gtk_icon_view_item_accessible_text_get_character_at_offset;
7633   iface->get_text_before_offset = gtk_icon_view_item_accessible_text_get_text_before_offset;
7634   iface->get_text_at_offset = gtk_icon_view_item_accessible_text_get_text_at_offset;
7635   iface->get_text_after_offset = gtk_icon_view_item_accessible_text_get_text_after_offset;
7636   iface->get_character_count = gtk_icon_view_item_accessible_text_get_character_count;
7637   iface->get_character_extents = gtk_icon_view_item_accessible_text_get_character_extents;
7638   iface->get_offset_at_point = gtk_icon_view_item_accessible_text_get_offset_at_point;
7639 }
7640
7641 static void
7642 gtk_icon_view_item_accessible_get_extents (AtkComponent *component,
7643                                            gint         *x,
7644                                            gint         *y,
7645                                            gint         *width,
7646                                            gint         *height,
7647                                            AtkCoordType  coord_type)
7648 {
7649   GtkIconViewItemAccessible *item;
7650   AtkObject *parent_obj;
7651   gint l_x, l_y;
7652
7653   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component));
7654
7655   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
7656   if (!GTK_IS_WIDGET (item->widget))
7657     return;
7658
7659   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7660     return;
7661
7662   *width = item->item->width;
7663   *height = item->item->height;
7664   if (gtk_icon_view_item_accessible_is_showing (item))
7665     {
7666       parent_obj = gtk_widget_get_accessible (item->widget);
7667       atk_component_get_position (ATK_COMPONENT (parent_obj), &l_x, &l_y, coord_type);
7668       *x = l_x + item->item->x;
7669       *y = l_y + item->item->y;
7670     }
7671   else
7672     {
7673       *x = G_MININT;
7674       *y = G_MININT;
7675     }
7676 }
7677
7678 static gboolean
7679 gtk_icon_view_item_accessible_grab_focus (AtkComponent *component)
7680 {
7681   GtkIconViewItemAccessible *item;
7682   GtkWidget *toplevel;
7683
7684   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component), FALSE);
7685
7686   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
7687   if (!GTK_IS_WIDGET (item->widget))
7688     return FALSE;
7689
7690   gtk_widget_grab_focus (item->widget);
7691   gtk_icon_view_set_cursor_item (GTK_ICON_VIEW (item->widget), item->item, -1);
7692   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item->widget));
7693   if (GTK_WIDGET_TOPLEVEL (toplevel))
7694     gtk_window_present (GTK_WINDOW (toplevel));
7695
7696   return TRUE;
7697 }
7698
7699 static void
7700 atk_component_item_interface_init (AtkComponentIface *iface)
7701 {
7702   iface->get_extents = gtk_icon_view_item_accessible_get_extents;
7703   iface->grab_focus = gtk_icon_view_item_accessible_grab_focus;
7704 }
7705
7706 static gboolean
7707 gtk_icon_view_item_accessible_add_state (GtkIconViewItemAccessible *item,
7708                                          AtkStateType               state_type,
7709                                          gboolean                   emit_signal)
7710 {
7711   gboolean rc;
7712
7713   rc = atk_state_set_add_state (item->state_set, state_type);
7714   /*
7715    * The signal should only be generated if the value changed,
7716    * not when the item is set up.  So states that are set
7717    * initially should pass FALSE as the emit_signal argument.
7718    */
7719
7720   if (emit_signal)
7721     {
7722       atk_object_notify_state_change (ATK_OBJECT (item), state_type, TRUE);
7723       /* If state_type is ATK_STATE_VISIBLE, additional notification */
7724       if (state_type == ATK_STATE_VISIBLE)
7725         g_signal_emit_by_name (item, "visible_data_changed");
7726     }
7727
7728   return rc;
7729 }
7730
7731 static gboolean
7732 gtk_icon_view_item_accessible_remove_state (GtkIconViewItemAccessible *item,
7733                                             AtkStateType               state_type,
7734                                             gboolean                   emit_signal)
7735 {
7736   if (atk_state_set_contains_state (item->state_set, state_type))
7737     {
7738       gboolean rc;
7739
7740       rc = atk_state_set_remove_state (item->state_set, state_type);
7741       /*
7742        * The signal should only be generated if the value changed,
7743        * not when the item is set up.  So states that are set
7744        * initially should pass FALSE as the emit_signal argument.
7745        */
7746
7747       if (emit_signal)
7748         {
7749           atk_object_notify_state_change (ATK_OBJECT (item), state_type, FALSE);
7750           /* If state_type is ATK_STATE_VISIBLE, additional notification */
7751           if (state_type == ATK_STATE_VISIBLE)
7752             g_signal_emit_by_name (item, "visible_data_changed");
7753         }
7754
7755       return rc;
7756     }
7757   else
7758     return FALSE;
7759 }
7760
7761 static gboolean
7762 gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item)
7763 {
7764   GtkIconView *icon_view;
7765   GdkRectangle visible_rect;
7766   gboolean is_showing;
7767
7768   /*
7769    * An item is considered "SHOWING" if any part of the item is in the
7770    * visible rectangle.
7771    */
7772
7773   if (!GTK_IS_ICON_VIEW (item->widget))
7774     return FALSE;
7775
7776   if (item->item == NULL)
7777     return FALSE;
7778
7779   icon_view = GTK_ICON_VIEW (item->widget);
7780   visible_rect.x = 0;
7781   if (icon_view->priv->hadjustment)
7782     visible_rect.x += icon_view->priv->hadjustment->value;
7783   visible_rect.y = 0;
7784   if (icon_view->priv->hadjustment)
7785     visible_rect.y += icon_view->priv->vadjustment->value;
7786   visible_rect.width = item->widget->allocation.width;
7787   visible_rect.height = item->widget->allocation.height;
7788
7789   if (((item->item->x + item->item->width) < visible_rect.x) ||
7790      ((item->item->y + item->item->height) < (visible_rect.y)) ||
7791      (item->item->x > (visible_rect.x + visible_rect.width)) ||
7792      (item->item->y > (visible_rect.y + visible_rect.height)))
7793     is_showing =  FALSE;
7794   else
7795     is_showing = TRUE;
7796
7797   return is_showing;
7798 }
7799
7800 static gboolean
7801 gtk_icon_view_item_accessible_set_visibility (GtkIconViewItemAccessible *item,
7802                                               gboolean                   emit_signal)
7803 {
7804   if (gtk_icon_view_item_accessible_is_showing (item))
7805     return gtk_icon_view_item_accessible_add_state (item, ATK_STATE_SHOWING,
7806                                                     emit_signal);
7807   else
7808     return gtk_icon_view_item_accessible_remove_state (item, ATK_STATE_SHOWING,
7809                                                        emit_signal);
7810 }
7811
7812 static void
7813 gtk_icon_view_item_accessible_object_init (GtkIconViewItemAccessible *item)
7814 {
7815   gint i;
7816
7817   item->state_set = atk_state_set_new ();
7818
7819   atk_state_set_add_state (item->state_set, ATK_STATE_ENABLED);
7820   atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSABLE);
7821   atk_state_set_add_state (item->state_set, ATK_STATE_SENSITIVE);
7822   atk_state_set_add_state (item->state_set, ATK_STATE_SELECTABLE);
7823   atk_state_set_add_state (item->state_set, ATK_STATE_VISIBLE);
7824
7825   for (i = 0; i < LAST_ACTION; i++)
7826     item->action_descriptions[i] = NULL;
7827
7828   item->image_description = NULL;
7829
7830   item->action_idle_handler = 0;
7831 }
7832
7833 static void
7834 gtk_icon_view_item_accessible_finalize (GObject *object)
7835 {
7836   GtkIconViewItemAccessible *item;
7837   gint i;
7838
7839   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (object));
7840
7841   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (object);
7842
7843   if (item->widget)
7844     g_object_remove_weak_pointer (G_OBJECT (item->widget), (gpointer) &item->widget);
7845
7846   if (item->state_set)
7847     g_object_unref (item->state_set);
7848
7849   if (item->text_buffer)
7850      g_object_unref (item->text_buffer);
7851
7852   for (i = 0; i < LAST_ACTION; i++)
7853     g_free (item->action_descriptions[i]);
7854
7855   g_free (item->image_description);
7856
7857   if (item->action_idle_handler)
7858     {
7859       g_source_remove (item->action_idle_handler);
7860       item->action_idle_handler = 0;
7861     }
7862
7863   G_OBJECT_CLASS (accessible_item_parent_class)->finalize (object);
7864 }
7865
7866 static G_CONST_RETURN gchar*
7867 gtk_icon_view_item_accessible_get_name (AtkObject *obj)
7868 {
7869   if (obj->name)
7870     return obj->name;
7871   else
7872     {
7873       GtkIconViewItemAccessible *item;
7874       GtkTextIter start_iter;
7875       GtkTextIter end_iter;
7876
7877       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7878  
7879       gtk_text_buffer_get_start_iter (item->text_buffer, &start_iter); 
7880       gtk_text_buffer_get_end_iter (item->text_buffer, &end_iter); 
7881
7882       return gtk_text_buffer_get_text (item->text_buffer, &start_iter, &end_iter, FALSE);
7883     }
7884 }
7885
7886 static AtkObject*
7887 gtk_icon_view_item_accessible_get_parent (AtkObject *obj)
7888 {
7889   GtkIconViewItemAccessible *item;
7890
7891   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), NULL);
7892   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7893
7894   if (item->widget)
7895     return gtk_widget_get_accessible (item->widget);
7896   else
7897     return NULL;
7898 }
7899
7900 static gint
7901 gtk_icon_view_item_accessible_get_index_in_parent (AtkObject *obj)
7902 {
7903   GtkIconViewItemAccessible *item;
7904
7905   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), 0);
7906   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7907
7908   return item->item->index; 
7909 }
7910
7911 static AtkStateSet *
7912 gtk_icon_view_item_accessible_ref_state_set (AtkObject *obj)
7913 {
7914   GtkIconViewItemAccessible *item;
7915   GtkIconView *icon_view;
7916
7917   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7918   g_return_val_if_fail (item->state_set, NULL);
7919
7920   if (!item->widget)
7921     return NULL;
7922
7923   icon_view = GTK_ICON_VIEW (item->widget);
7924   if (icon_view->priv->cursor_item == item->item)
7925     atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSED);
7926   else
7927     atk_state_set_remove_state (item->state_set, ATK_STATE_FOCUSED);
7928
7929   return g_object_ref (item->state_set);
7930 }
7931
7932 static void
7933 gtk_icon_view_item_accessible_class_init (AtkObjectClass *klass)
7934 {
7935   GObjectClass *gobject_class;
7936
7937   accessible_item_parent_class = g_type_class_peek_parent (klass);
7938
7939   gobject_class = (GObjectClass *)klass;
7940
7941   gobject_class->finalize = gtk_icon_view_item_accessible_finalize;
7942
7943   klass->get_index_in_parent = gtk_icon_view_item_accessible_get_index_in_parent; 
7944   klass->get_name = gtk_icon_view_item_accessible_get_name; 
7945   klass->get_parent = gtk_icon_view_item_accessible_get_parent; 
7946   klass->ref_state_set = gtk_icon_view_item_accessible_ref_state_set; 
7947 }
7948
7949 static GType
7950 gtk_icon_view_item_accessible_get_type (void)
7951 {
7952   static GType type = 0;
7953
7954   if (!type)
7955     {
7956       static const GTypeInfo tinfo =
7957       {
7958         sizeof (GtkIconViewItemAccessibleClass),
7959         (GBaseInitFunc) NULL, /* base init */
7960         (GBaseFinalizeFunc) NULL, /* base finalize */
7961         (GClassInitFunc) gtk_icon_view_item_accessible_class_init, /* class init */
7962         (GClassFinalizeFunc) NULL, /* class finalize */
7963         NULL, /* class data */
7964         sizeof (GtkIconViewItemAccessible), /* instance size */
7965         0, /* nb preallocs */
7966         (GInstanceInitFunc) gtk_icon_view_item_accessible_object_init, /* instance init */
7967         NULL /* value table */
7968       };
7969
7970       static const GInterfaceInfo atk_component_info =
7971       {
7972         (GInterfaceInitFunc) atk_component_item_interface_init,
7973         (GInterfaceFinalizeFunc) NULL,
7974         NULL
7975       };
7976       static const GInterfaceInfo atk_action_info =
7977       {
7978         (GInterfaceInitFunc) atk_action_item_interface_init,
7979         (GInterfaceFinalizeFunc) NULL,
7980         NULL
7981       };
7982       static const GInterfaceInfo atk_image_info =
7983       {
7984         (GInterfaceInitFunc) atk_image_item_interface_init,
7985         (GInterfaceFinalizeFunc) NULL,
7986         NULL
7987       };
7988       static const GInterfaceInfo atk_text_info =
7989       {
7990         (GInterfaceInitFunc) atk_text_item_interface_init,
7991         (GInterfaceFinalizeFunc) NULL,
7992         NULL
7993       };
7994
7995       type = g_type_register_static (ATK_TYPE_OBJECT,
7996                                      "GtkIconViewItemAccessible", &tinfo, 0);
7997       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
7998                                    &atk_component_info);
7999       g_type_add_interface_static (type, ATK_TYPE_ACTION,
8000                                    &atk_action_info);
8001       g_type_add_interface_static (type, ATK_TYPE_IMAGE,
8002                                    &atk_image_info);
8003       g_type_add_interface_static (type, ATK_TYPE_TEXT,
8004                                    &atk_text_info);
8005     }
8006
8007   return type;
8008 }
8009
8010 #define GTK_TYPE_ICON_VIEW_ACCESSIBLE      (gtk_icon_view_accessible_get_type ())
8011 #define GTK_ICON_VIEW_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE, GtkIconViewAccessible))
8012 #define GTK_IS_ICON_VIEW_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE))
8013
8014 static GType gtk_icon_view_accessible_get_type (void);
8015
8016 typedef struct
8017 {
8018    AtkObject parent;
8019 } GtkIconViewAccessible;
8020
8021 typedef struct
8022 {
8023   AtkObject *item;
8024   gint       index;
8025 } GtkIconViewItemAccessibleInfo;
8026
8027 typedef struct
8028 {
8029   GList *items;
8030
8031   GtkAdjustment *old_hadj;
8032   GtkAdjustment *old_vadj;
8033
8034   GtkTreeModel *model;
8035
8036 } GtkIconViewAccessiblePrivate;
8037
8038 static GtkIconViewAccessiblePrivate *
8039 gtk_icon_view_accessible_get_priv (AtkObject *accessible)
8040 {
8041   return g_object_get_qdata (G_OBJECT (accessible),
8042                              accessible_private_data_quark);
8043 }
8044
8045 static void
8046 gtk_icon_view_item_accessible_info_new (AtkObject *accessible,
8047                                         AtkObject *item,
8048                                         gint       index)
8049 {
8050   GtkIconViewItemAccessibleInfo *info;
8051   GtkIconViewItemAccessibleInfo *tmp_info;
8052   GtkIconViewAccessiblePrivate *priv;
8053   GList *items;
8054
8055   info = g_new (GtkIconViewItemAccessibleInfo, 1);
8056   info->item = item;
8057   info->index = index;
8058
8059   priv = gtk_icon_view_accessible_get_priv (accessible);
8060   items = priv->items;
8061   while (items)
8062     {
8063       tmp_info = items->data;
8064       if (tmp_info->index > index)
8065         break;
8066       items = items->next;
8067     }
8068   priv->items = g_list_insert_before (priv->items, items, info);
8069   priv->old_hadj = NULL;
8070   priv->old_vadj = NULL;
8071 }
8072
8073 static gint
8074 gtk_icon_view_accessible_get_n_children (AtkObject *accessible)
8075 {
8076   GtkIconView *icon_view;
8077   GtkWidget *widget;
8078
8079   widget = GTK_ACCESSIBLE (accessible)->widget;
8080   if (!widget)
8081       return 0;
8082
8083   icon_view = GTK_ICON_VIEW (widget);
8084
8085   return g_list_length (icon_view->priv->items);
8086 }
8087
8088 static AtkObject *
8089 gtk_icon_view_accessible_find_child (AtkObject *accessible,
8090                                      gint       index)
8091 {
8092   GtkIconViewAccessiblePrivate *priv;
8093   GtkIconViewItemAccessibleInfo *info;
8094   GList *items;
8095
8096   priv = gtk_icon_view_accessible_get_priv (accessible);
8097   items = priv->items;
8098
8099   while (items)
8100     {
8101       info = items->data;
8102       if (info->index == index)
8103         return info->item;
8104       items = items->next; 
8105     }
8106   return NULL;
8107 }
8108
8109 static AtkObject *
8110 gtk_icon_view_accessible_ref_child (AtkObject *accessible,
8111                                     gint       index)
8112 {
8113   GtkIconView *icon_view;
8114   GtkWidget *widget;
8115   GList *icons;
8116   AtkObject *obj;
8117   GtkIconViewItemAccessible *a11y_item;
8118
8119   widget = GTK_ACCESSIBLE (accessible)->widget;
8120   if (!widget)
8121     return NULL;
8122
8123   icon_view = GTK_ICON_VIEW (widget);
8124   icons = g_list_nth (icon_view->priv->items, index);
8125   obj = NULL;
8126   if (icons)
8127     {
8128       GtkIconViewItem *item = icons->data;
8129    
8130       g_return_val_if_fail (item->index == index, NULL);
8131       obj = gtk_icon_view_accessible_find_child (accessible, index);
8132       if (!obj)
8133         {
8134           obj = g_object_new (gtk_icon_view_item_accessible_get_type (), NULL);
8135           gtk_icon_view_item_accessible_info_new (accessible,
8136                                                   obj,
8137                                                   index);
8138           obj->role = ATK_ROLE_ICON;
8139           a11y_item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8140           a11y_item->item = item;
8141           a11y_item->widget = widget;
8142           a11y_item->text_buffer = gtk_text_buffer_new (NULL);
8143
8144           gtk_icon_view_set_cell_data (icon_view, item);
8145           gtk_text_buffer_set_text (a11y_item->text_buffer, 
8146                                     get_text (icon_view, item), -1);
8147
8148           gtk_icon_view_item_accessible_set_visibility (a11y_item, FALSE);
8149           g_object_add_weak_pointer (G_OBJECT (widget), (gpointer) &(a11y_item->widget));
8150        }
8151       g_object_ref (obj);
8152     }
8153   return obj;
8154 }
8155
8156 static void
8157 gtk_icon_view_accessible_traverse_items (GtkIconViewAccessible *view,
8158                                          GList                 *list)
8159 {
8160   GtkIconViewAccessiblePrivate *priv;
8161   GtkIconViewItemAccessibleInfo *info;
8162   GtkIconViewItemAccessible *item;
8163   GList *items;
8164   
8165   priv =  gtk_icon_view_accessible_get_priv (ATK_OBJECT (view));
8166   if (priv->items)
8167     {
8168       GtkWidget *widget;
8169       gboolean act_on_item;
8170
8171       widget = GTK_ACCESSIBLE (view)->widget;
8172       if (widget == NULL)
8173         return;
8174
8175       items = priv->items;
8176
8177       act_on_item = (list == NULL);
8178
8179       while (items)
8180         {
8181
8182           info = (GtkIconViewItemAccessibleInfo *)items->data;
8183           item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8184
8185           if (act_on_item == FALSE && list == items)
8186             act_on_item = TRUE;
8187
8188           if (act_on_item)
8189             gtk_icon_view_item_accessible_set_visibility (item, TRUE);
8190
8191           items = items->next;
8192        }
8193    }
8194 }
8195
8196 static void
8197 gtk_icon_view_accessible_adjustment_changed (GtkAdjustment *adjustment,
8198                                              GtkIconView   *icon_view)
8199 {
8200   AtkObject *obj;
8201   GtkIconViewAccessible *view;
8202
8203   /*
8204    * The scrollbars have changed
8205    */
8206   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
8207   view = GTK_ICON_VIEW_ACCESSIBLE (obj);
8208
8209   gtk_icon_view_accessible_traverse_items (view, NULL);
8210 }
8211
8212 static void
8213 gtk_icon_view_accessible_set_scroll_adjustments (GtkWidget      *widget,
8214                                                  GtkAdjustment *hadj,
8215                                                  GtkAdjustment *vadj)
8216 {
8217   AtkObject *atk_obj;
8218   GtkIconViewAccessiblePrivate *priv;
8219
8220   atk_obj = gtk_widget_get_accessible (widget);
8221   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8222
8223   if (priv->old_hadj != hadj)
8224     {
8225       if (priv->old_hadj)
8226         {
8227           g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8228                                         (gpointer *)&priv->old_hadj);
8229           
8230           g_signal_handlers_disconnect_by_func (priv->old_hadj,
8231                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8232                                                 widget);
8233         }
8234       priv->old_hadj = hadj;
8235       if (priv->old_hadj)
8236         {
8237           g_object_add_weak_pointer (G_OBJECT (priv->old_hadj),
8238                                      (gpointer *)&priv->old_hadj);
8239           g_signal_connect (hadj,
8240                             "value-changed",
8241                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8242                             widget);
8243         }
8244     }
8245   if (priv->old_vadj != vadj)
8246     {
8247       if (priv->old_vadj)
8248         {
8249           g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8250                                         (gpointer *)&priv->old_vadj);
8251           
8252           g_signal_handlers_disconnect_by_func (priv->old_vadj,
8253                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8254                                                 widget);
8255         }
8256       priv->old_vadj = vadj;
8257       if (priv->old_vadj)
8258         {
8259           g_object_add_weak_pointer (G_OBJECT (priv->old_vadj),
8260                                      (gpointer *)&priv->old_vadj);
8261           g_signal_connect (vadj,
8262                             "value-changed",
8263                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8264                             widget);
8265         }
8266     }
8267 }
8268
8269 static void
8270 gtk_icon_view_accessible_model_row_changed (GtkTreeModel *tree_model,
8271                                             GtkTreePath  *path,
8272                                             GtkTreeIter  *iter,
8273                                             gpointer     user_data)
8274 {
8275   AtkObject *atk_obj;
8276
8277   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8278   g_signal_emit_by_name (atk_obj, "visible-data-changed");
8279
8280   return;
8281 }
8282
8283 static void
8284 gtk_icon_view_accessible_model_row_inserted (GtkTreeModel *tree_model,
8285                                              GtkTreePath  *path,
8286                                              GtkTreeIter  *iter,
8287                                              gpointer     user_data)
8288 {
8289   GtkIconViewAccessiblePrivate *priv;
8290   GtkIconViewItemAccessibleInfo *info;
8291   GtkIconViewAccessible *view;
8292   GtkIconViewItemAccessible *item;
8293   GList *items;
8294   GList *tmp_list;
8295   AtkObject *atk_obj;
8296   gint index;
8297
8298   index = gtk_tree_path_get_indices(path)[0];
8299   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8300   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8301   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8302
8303   items = priv->items;
8304   tmp_list = NULL;
8305   while (items)
8306     {
8307       info = items->data;
8308       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8309       if (info->index != item->item->index)
8310         {
8311           if (info->index < index)
8312             g_warning ("Unexpected index value on insertion %d %d", index, info->index);
8313  
8314           if (tmp_list == NULL)
8315             tmp_list = items;
8316    
8317           info->index = item->item->index;
8318         }
8319
8320       items = items->next;
8321     }
8322   gtk_icon_view_accessible_traverse_items (view, tmp_list);
8323   g_signal_emit_by_name (atk_obj, "children_changed::add",
8324                          index, NULL, NULL);
8325   return;
8326 }
8327
8328 static void
8329 gtk_icon_view_accessible_model_row_deleted (GtkTreeModel *tree_model,
8330                                             GtkTreePath  *path,
8331                                             gpointer     user_data)
8332 {
8333   GtkIconViewAccessiblePrivate *priv;
8334   GtkIconViewItemAccessibleInfo *info;
8335   GtkIconViewAccessible *view;
8336   GtkIconViewItemAccessible *item;
8337   GList *items;
8338   GList *tmp_list;
8339   GList *deleted_item;
8340   AtkObject *atk_obj;
8341   gint index;
8342
8343   index = gtk_tree_path_get_indices(path)[0];
8344   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8345   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8346   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8347
8348   items = priv->items;
8349   tmp_list = NULL;
8350   deleted_item = NULL;
8351   info = NULL;
8352   while (items)
8353     {
8354       info = items->data;
8355       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8356       if (info->index == index)
8357         {
8358           deleted_item = items;
8359         }
8360       if (info->index != item->item->index)
8361         {
8362           if (tmp_list == NULL)
8363             tmp_list = items;
8364           else    
8365             info->index = item->item->index;
8366         }
8367
8368       items = items->next;
8369     }
8370   gtk_icon_view_accessible_traverse_items (view, tmp_list);
8371   if (deleted_item)
8372     {
8373       info = deleted_item->data;
8374       gtk_icon_view_item_accessible_add_state (GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item), ATK_STATE_DEFUNCT, TRUE);
8375     }
8376   g_signal_emit_by_name (atk_obj, "children_changed::remove",
8377                          index, NULL, NULL);
8378   if (deleted_item)
8379     {
8380       priv->items = g_list_remove_link (priv->items, deleted_item);
8381       g_free (info);
8382     }
8383
8384   return;
8385 }
8386
8387 static gint
8388 gtk_icon_view_accessible_item_compare (GtkIconViewItemAccessibleInfo *i1,
8389                                        GtkIconViewItemAccessibleInfo *i2)
8390 {
8391   return i1->index - i2->index;
8392 }
8393
8394 static void
8395 gtk_icon_view_accessible_model_rows_reordered (GtkTreeModel *tree_model,
8396                                                GtkTreePath  *path,
8397                                                GtkTreeIter  *iter,
8398                                                gint         *new_order,
8399                                                gpointer     user_data)
8400 {
8401   GtkIconViewAccessiblePrivate *priv;
8402   GtkIconViewItemAccessibleInfo *info;
8403   GtkIconViewAccessible *view;
8404   GtkIconView *icon_view;
8405   GtkIconViewItemAccessible *item;
8406   GList *items;
8407   GList *tmp_list;
8408   AtkObject *atk_obj;
8409
8410   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8411   icon_view = GTK_ICON_VIEW (user_data);
8412   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8413   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8414
8415   items = priv->items;
8416   tmp_list = NULL;
8417   while (items)
8418     {
8419       info = items->data;
8420       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8421       info->index = new_order[info->index];
8422       tmp_list = g_list_nth (icon_view->priv->items, info->index);
8423       item->item = tmp_list->data;
8424       items = items->next;
8425     }
8426   priv->items = g_list_sort (priv->items, 
8427                              (GCompareFunc)gtk_icon_view_accessible_item_compare);
8428
8429   return;
8430 }
8431
8432 static void
8433 gtk_icon_view_accessible_disconnect_model_signals (GtkTreeModel *model,
8434                                                    GtkWidget *widget)
8435 {
8436   GObject *obj;
8437
8438   obj = G_OBJECT (model);
8439   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_changed, widget);
8440   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_inserted, widget);
8441   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_deleted, widget);
8442   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_rows_reordered, widget);
8443 }
8444
8445 static void
8446 gtk_icon_view_accessible_connect_model_signals (GtkIconView *icon_view)
8447 {
8448   GObject *obj;
8449
8450   obj = G_OBJECT (icon_view->priv->model);
8451   g_signal_connect_data (obj, "row-changed",
8452                          (GCallback) gtk_icon_view_accessible_model_row_changed,
8453                          icon_view, NULL, 0);
8454   g_signal_connect_data (obj, "row-inserted",
8455                          (GCallback) gtk_icon_view_accessible_model_row_inserted, 
8456                          icon_view, NULL, G_CONNECT_AFTER);
8457   g_signal_connect_data (obj, "row-deleted",
8458                          (GCallback) gtk_icon_view_accessible_model_row_deleted, 
8459                          icon_view, NULL, G_CONNECT_AFTER);
8460   g_signal_connect_data (obj, "rows-reordered",
8461                          (GCallback) gtk_icon_view_accessible_model_rows_reordered, 
8462                          icon_view, NULL, G_CONNECT_AFTER);
8463 }
8464
8465 static void
8466 gtk_icon_view_accessible_clear_cache (GtkIconViewAccessiblePrivate *priv)
8467 {
8468   GtkIconViewItemAccessibleInfo *info;
8469   GList *items;
8470
8471   items = priv->items;
8472   while (items)
8473     {
8474       info = (GtkIconViewItemAccessibleInfo *) items->data;
8475       g_object_unref (info->item);
8476       g_free (items->data);
8477       items = items->next;
8478     }
8479   g_list_free (priv->items);
8480   priv->items = NULL;
8481 }
8482
8483 static void
8484 gtk_icon_view_accessible_notify_gtk (GObject *obj,
8485                                      GParamSpec *pspec)
8486 {
8487   GtkIconView *icon_view;
8488   GtkWidget *widget;
8489   AtkObject *atk_obj;
8490   GtkIconViewAccessible *view;
8491   GtkIconViewAccessiblePrivate *priv;
8492
8493   if (strcmp (pspec->name, "model") == 0)
8494     {
8495       widget = GTK_WIDGET (obj); 
8496       atk_obj = gtk_widget_get_accessible (widget);
8497       view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8498       priv = gtk_icon_view_accessible_get_priv (atk_obj);
8499       if (priv->model)
8500         {
8501           g_object_remove_weak_pointer (G_OBJECT (priv->model),
8502                                         (gpointer *)&priv->model);
8503           gtk_icon_view_accessible_disconnect_model_signals (priv->model, widget);
8504         }
8505       gtk_icon_view_accessible_clear_cache (priv);
8506
8507       icon_view = GTK_ICON_VIEW (obj);
8508       priv->model = icon_view->priv->model;
8509       /* If there is no model the GtkIconView is probably being destroyed */
8510       if (priv->model)
8511         {
8512           g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
8513           gtk_icon_view_accessible_connect_model_signals (icon_view);
8514         }
8515     }
8516
8517   return;
8518 }
8519
8520 static void
8521 gtk_icon_view_accessible_initialize (AtkObject *accessible,
8522                                      gpointer   data)
8523 {
8524   GtkIconViewAccessiblePrivate *priv;
8525   GtkIconView *icon_view;
8526
8527   if (ATK_OBJECT_CLASS (accessible_parent_class)->initialize)
8528     ATK_OBJECT_CLASS (accessible_parent_class)->initialize (accessible, data);
8529
8530   priv = g_new0 (GtkIconViewAccessiblePrivate, 1);
8531   g_object_set_qdata (G_OBJECT (accessible),
8532                       accessible_private_data_quark,
8533                       priv);
8534
8535   icon_view = GTK_ICON_VIEW (data);
8536   if (icon_view->priv->hadjustment)
8537     {
8538       priv->old_hadj = icon_view->priv->hadjustment;
8539       g_object_add_weak_pointer (G_OBJECT (priv->old_hadj), (gpointer *)&priv->old_hadj);
8540       g_signal_connect (icon_view->priv->hadjustment,
8541                         "value-changed",
8542                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8543                         icon_view);
8544     } 
8545   if (icon_view->priv->vadjustment)
8546     {
8547       priv->old_vadj = icon_view->priv->vadjustment;
8548       g_object_add_weak_pointer (G_OBJECT (priv->old_vadj), (gpointer *)&priv->old_vadj);
8549       g_signal_connect (icon_view->priv->vadjustment,
8550                         "value-changed",
8551                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8552                         icon_view);
8553     }
8554   g_signal_connect_after (data,
8555                           "set_scroll_adjustments",
8556                           G_CALLBACK (gtk_icon_view_accessible_set_scroll_adjustments),
8557                           NULL);
8558   g_signal_connect (data,
8559                     "notify",
8560                     G_CALLBACK (gtk_icon_view_accessible_notify_gtk),
8561                     NULL);
8562
8563   priv->model = icon_view->priv->model;
8564   if (priv->model)
8565     {
8566       g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
8567       gtk_icon_view_accessible_connect_model_signals (icon_view);
8568     }
8569                           
8570   accessible->role = ATK_ROLE_LAYERED_PANE;
8571 }
8572
8573 static void
8574 gtk_icon_view_accessible_finalize (GObject *object)
8575 {
8576   GtkIconViewAccessiblePrivate *priv;
8577
8578   priv = gtk_icon_view_accessible_get_priv (ATK_OBJECT (object));
8579   gtk_icon_view_accessible_clear_cache (priv);
8580
8581   g_free (priv);
8582
8583   G_OBJECT_CLASS (accessible_parent_class)->finalize (object);
8584 }
8585
8586 static void
8587 gtk_icon_view_accessible_destroyed (GtkWidget *widget,
8588                                     GtkAccessible *accessible)
8589 {
8590   AtkObject *atk_obj;
8591   GtkIconViewAccessiblePrivate *priv;
8592
8593   atk_obj = ATK_OBJECT (accessible);
8594   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8595   if (priv->old_hadj)
8596     {
8597       g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8598                                     (gpointer *)&priv->old_hadj);
8599           
8600       g_signal_handlers_disconnect_by_func (priv->old_hadj,
8601                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
8602                                             widget);
8603       priv->old_hadj = NULL;
8604     }
8605   if (priv->old_vadj)
8606     {
8607       g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8608                                     (gpointer *)&priv->old_vadj);
8609           
8610       g_signal_handlers_disconnect_by_func (priv->old_vadj,
8611                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
8612                                             widget);
8613       priv->old_vadj = NULL;
8614     }
8615 }
8616
8617 static void
8618 gtk_icon_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
8619 {
8620   if (accessible->widget)
8621     {
8622       g_signal_connect_after (accessible->widget,
8623                               "destroy",
8624                               G_CALLBACK (gtk_icon_view_accessible_destroyed),
8625                               accessible);
8626     }
8627   GTK_ACCESSIBLE_CLASS (accessible_parent_class)->connect_widget_destroyed (accessible);
8628 }
8629
8630 static void
8631 gtk_icon_view_accessible_class_init (AtkObjectClass *klass)
8632 {
8633   GObjectClass *gobject_class;
8634   GtkAccessibleClass *accessible_class;
8635
8636   accessible_parent_class = g_type_class_peek_parent (klass);
8637
8638   gobject_class = (GObjectClass *)klass;
8639   accessible_class = (GtkAccessibleClass *)klass;
8640
8641   gobject_class->finalize = gtk_icon_view_accessible_finalize;
8642
8643   klass->get_n_children = gtk_icon_view_accessible_get_n_children;
8644   klass->ref_child = gtk_icon_view_accessible_ref_child;
8645   klass->initialize = gtk_icon_view_accessible_initialize;
8646
8647   accessible_class->connect_widget_destroyed = gtk_icon_view_accessible_connect_widget_destroyed;
8648
8649   accessible_private_data_quark = g_quark_from_static_string ("icon_view-accessible-private-data");
8650 }
8651
8652 static AtkObject*
8653 gtk_icon_view_accessible_ref_accessible_at_point (AtkComponent *component,
8654                                                   gint          x,
8655                                                   gint          y,
8656                                                   AtkCoordType  coord_type)
8657 {
8658   GtkWidget *widget;
8659   GtkIconView *icon_view;
8660   GtkIconViewItem *item;
8661   gint x_pos, y_pos;
8662
8663   widget = GTK_ACCESSIBLE (component)->widget;
8664   if (widget == NULL)
8665   /* State is defunct */
8666     return NULL;
8667
8668   icon_view = GTK_ICON_VIEW (widget);
8669   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
8670   item = gtk_icon_view_get_item_at_coords (icon_view, x - x_pos, y - y_pos, TRUE, NULL);
8671   if (item)
8672     return gtk_icon_view_accessible_ref_child (ATK_OBJECT (component), item->index);
8673
8674   return NULL;
8675 }
8676
8677 static void
8678 atk_component_interface_init (AtkComponentIface *iface)
8679 {
8680   iface->ref_accessible_at_point = gtk_icon_view_accessible_ref_accessible_at_point;
8681 }
8682
8683 static gboolean
8684 gtk_icon_view_accessible_add_selection (AtkSelection *selection,
8685                                         gint i)
8686 {
8687   GtkWidget *widget;
8688   GtkIconView *icon_view;
8689   GtkIconViewItem *item;
8690   GList *l;
8691
8692   widget = GTK_ACCESSIBLE (selection)->widget;
8693   if (widget == NULL)
8694     return FALSE;
8695
8696   icon_view = GTK_ICON_VIEW (widget);
8697
8698   l = g_list_nth (icon_view->priv->items, i);
8699   if (!l)
8700     return FALSE;
8701
8702   item = l->data;
8703   gtk_icon_view_select_item (icon_view, item);
8704
8705   return TRUE;
8706 }
8707
8708 static gboolean
8709 gtk_icon_view_accessible_clear_selection (AtkSelection *selection)
8710 {
8711   GtkWidget *widget;
8712   GtkIconView *icon_view;
8713
8714   widget = GTK_ACCESSIBLE (selection)->widget;
8715   if (widget == NULL)
8716     return FALSE;
8717
8718   icon_view = GTK_ICON_VIEW (widget);
8719   gtk_icon_view_unselect_all (icon_view);
8720
8721   return TRUE;
8722 }
8723
8724 static AtkObject*
8725 gtk_icon_view_accessible_ref_selection (AtkSelection *selection,
8726                                         gint          i)
8727 {
8728   GtkWidget *widget;
8729   GtkIconView *icon_view;
8730   GtkIconViewItem *item;
8731   GList *l;
8732
8733   widget = GTK_ACCESSIBLE (selection)->widget;
8734   if (widget == NULL)
8735     return NULL;
8736
8737   icon_view = GTK_ICON_VIEW (widget);
8738
8739   l = icon_view->priv->items;
8740   while (l)
8741     {
8742       item = l->data;
8743       if (item->selected)
8744         {
8745           if (i == 0)
8746             return atk_object_ref_accessible_child (gtk_widget_get_accessible (widget), item->index);
8747           else
8748             i--;
8749         }
8750       l = l->next;
8751     }
8752
8753   return NULL;
8754 }
8755
8756 static gint
8757 gtk_icon_view_accessible_get_selection_count (AtkSelection *selection)
8758 {
8759   GtkWidget *widget;
8760   GtkIconView *icon_view;
8761   GtkIconViewItem *item;
8762   GList *l;
8763   gint count;
8764
8765   widget = GTK_ACCESSIBLE (selection)->widget;
8766   if (widget == NULL)
8767     return 0;
8768
8769   icon_view = GTK_ICON_VIEW (widget);
8770
8771   l = icon_view->priv->items;
8772   count = 0;
8773   while (l)
8774     {
8775       item = l->data;
8776
8777       if (item->selected)
8778         count++;
8779
8780       l = l->next;
8781     }
8782
8783   return count;
8784 }
8785
8786 static gboolean
8787 gtk_icon_view_accessible_is_child_selected (AtkSelection *selection,
8788                                             gint          i)
8789 {
8790   GtkWidget *widget;
8791   GtkIconView *icon_view;
8792   GtkIconViewItem *item;
8793   GList *l;
8794
8795   widget = GTK_ACCESSIBLE (selection)->widget;
8796   if (widget == NULL)
8797     return FALSE;
8798
8799   icon_view = GTK_ICON_VIEW (widget);
8800   l = g_list_nth (icon_view->priv->items, i);
8801   if (!l)
8802     return FALSE;
8803
8804   item = l->data;
8805
8806   return item->selected;
8807 }
8808
8809 static gboolean
8810 gtk_icon_view_accessible_remove_selection (AtkSelection *selection,
8811                                            gint          i)
8812 {
8813   GtkWidget *widget;
8814   GtkIconView *icon_view;
8815   GtkIconViewItem *item;
8816   GList *l;
8817   gint count;
8818
8819   widget = GTK_ACCESSIBLE (selection)->widget;
8820   if (widget == NULL)
8821     return FALSE;
8822
8823   icon_view = GTK_ICON_VIEW (widget);
8824   l = icon_view->priv->items;
8825   count = 0;
8826   while (l)
8827     {
8828       item = l->data;
8829       if (item->selected)
8830         {
8831           if (count == i)
8832             {
8833               gtk_icon_view_unselect_item (icon_view, item);
8834               return TRUE;
8835             }
8836           count++;
8837         }
8838       l = l->next;
8839     }
8840
8841   return FALSE;
8842 }
8843  
8844 static gboolean
8845 gtk_icon_view_accessible_select_all_selection (AtkSelection *selection)
8846 {
8847   GtkWidget *widget;
8848   GtkIconView *icon_view;
8849
8850   widget = GTK_ACCESSIBLE (selection)->widget;
8851   if (widget == NULL)
8852     return FALSE;
8853
8854   icon_view = GTK_ICON_VIEW (widget);
8855   gtk_icon_view_select_all (icon_view);
8856   return TRUE;
8857 }
8858
8859 static void
8860 gtk_icon_view_accessible_selection_interface_init (AtkSelectionIface *iface)
8861 {
8862   iface->add_selection = gtk_icon_view_accessible_add_selection;
8863   iface->clear_selection = gtk_icon_view_accessible_clear_selection;
8864   iface->ref_selection = gtk_icon_view_accessible_ref_selection;
8865   iface->get_selection_count = gtk_icon_view_accessible_get_selection_count;
8866   iface->is_child_selected = gtk_icon_view_accessible_is_child_selected;
8867   iface->remove_selection = gtk_icon_view_accessible_remove_selection;
8868   iface->select_all_selection = gtk_icon_view_accessible_select_all_selection;
8869 }
8870
8871 static GType
8872 gtk_icon_view_accessible_get_type (void)
8873 {
8874   static GType type = 0;
8875
8876   if (!type)
8877     {
8878       static GTypeInfo tinfo =
8879       {
8880         0, /* class size */
8881         (GBaseInitFunc) NULL, /* base init */
8882         (GBaseFinalizeFunc) NULL, /* base finalize */
8883         (GClassInitFunc) gtk_icon_view_accessible_class_init,
8884         (GClassFinalizeFunc) NULL, /* class finalize */
8885         NULL, /* class data */
8886         0, /* instance size */
8887         0, /* nb preallocs */
8888         (GInstanceInitFunc) NULL, /* instance init */
8889         NULL /* value table */
8890       };
8891       static const GInterfaceInfo atk_component_info =
8892       {
8893         (GInterfaceInitFunc) atk_component_interface_init,
8894         (GInterfaceFinalizeFunc) NULL,
8895         NULL
8896       };
8897       static const GInterfaceInfo atk_selection_info = 
8898       {
8899         (GInterfaceInitFunc) gtk_icon_view_accessible_selection_interface_init,
8900         (GInterfaceFinalizeFunc) NULL,
8901         NULL
8902       };
8903
8904       /*
8905        * Figure out the size of the class and instance
8906        * we are deriving from
8907        */
8908       AtkObjectFactory *factory;
8909       GType derived_type;
8910       GTypeQuery query;
8911       GType derived_atk_type;
8912
8913       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
8914       factory = atk_registry_get_factory (atk_get_default_registry (), 
8915                                           derived_type);
8916       derived_atk_type = atk_object_factory_get_accessible_type (factory);
8917       g_type_query (derived_atk_type, &query);
8918       tinfo.class_size = query.class_size;
8919       tinfo.instance_size = query.instance_size;
8920  
8921       type = g_type_register_static (derived_atk_type, 
8922                                      "GtkIconViewAccessible", 
8923                                      &tinfo, 0);
8924       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
8925                                    &atk_component_info);
8926       g_type_add_interface_static (type, ATK_TYPE_SELECTION,
8927                                    &atk_selection_info);
8928     }
8929   return type;
8930 }
8931
8932 static AtkObject *
8933 gtk_icon_view_accessible_new (GObject *obj)
8934 {
8935   AtkObject *accessible;
8936
8937   g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
8938
8939   accessible = g_object_new (gtk_icon_view_accessible_get_type (), NULL);
8940   atk_object_initialize (accessible, obj);
8941
8942   return accessible;
8943 }
8944
8945 static GType
8946 gtk_icon_view_accessible_factory_get_accessible_type (void)
8947 {
8948   return gtk_icon_view_accessible_get_type ();
8949 }
8950
8951 static AtkObject*
8952 gtk_icon_view_accessible_factory_create_accessible (GObject *obj)
8953 {
8954   return gtk_icon_view_accessible_new (obj);
8955 }
8956
8957 static void
8958 gtk_icon_view_accessible_factory_class_init (AtkObjectFactoryClass *klass)
8959 {
8960   klass->create_accessible = gtk_icon_view_accessible_factory_create_accessible;
8961   klass->get_accessible_type = gtk_icon_view_accessible_factory_get_accessible_type;
8962 }
8963
8964 static GType
8965 gtk_icon_view_accessible_factory_get_type (void)
8966 {
8967   static GType type = 0;
8968
8969   if (!type)
8970     {
8971       static const GTypeInfo tinfo =
8972       {
8973         sizeof (AtkObjectFactoryClass),
8974         NULL,           /* base_init */
8975         NULL,           /* base_finalize */
8976         (GClassInitFunc) gtk_icon_view_accessible_factory_class_init,
8977         NULL,           /* class_finalize */
8978         NULL,           /* class_data */
8979         sizeof (AtkObjectFactory),
8980         0,             /* n_preallocs */
8981         NULL, NULL
8982       };
8983
8984       type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY, 
8985                                     "GtkIconViewAccessibleFactory",
8986                                     &tinfo, 0);
8987     }
8988   return type;
8989 }
8990
8991
8992 static AtkObject *
8993 gtk_icon_view_get_accessible (GtkWidget *widget)
8994 {
8995   static gboolean first_time = TRUE;
8996
8997   if (first_time)
8998     {
8999       AtkObjectFactory *factory;
9000       AtkRegistry *registry;
9001       GType derived_type; 
9002       GType derived_atk_type; 
9003
9004       /*
9005        * Figure out whether accessibility is enabled by looking at the
9006        * type of the accessible object which would be created for
9007        * the parent type of GtkIconView.
9008        */
9009       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
9010
9011       registry = atk_get_default_registry ();
9012       factory = atk_registry_get_factory (registry,
9013                                           derived_type);
9014       derived_atk_type = atk_object_factory_get_accessible_type (factory);
9015       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE)) 
9016         atk_registry_set_factory_type (registry, 
9017                                        GTK_TYPE_ICON_VIEW,
9018                                        gtk_icon_view_accessible_factory_get_type ());
9019       first_time = FALSE;
9020     } 
9021   return (* GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->get_accessible) (widget);
9022 }
9023
9024 #define __GTK_ICON_VIEW_C__
9025 #include "gtkaliasdef.c"