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