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