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