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