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