]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
Merge branch 'master' into client-side-windows
[~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 (item->width == -1)
2920         {
2921           if (item_width > 0)
2922             wrap_width = item_width - pixbuf_width - icon_view->priv->spacing;
2923           else
2924             wrap_width = MAX (2 * pixbuf_width, 50);
2925         }
2926       else if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
2927         wrap_width = item_width;
2928       else
2929         wrap_width = item_width - pixbuf_width - icon_view->priv->spacing;
2930
2931       wrap_width -= ITEM_PADDING * 2;
2932
2933       g_object_set (text_info->cell, "wrap-width", wrap_width, NULL);
2934       g_object_set (text_info->cell, "width", wrap_width, NULL);
2935     }
2936 }
2937
2938 static void
2939 gtk_icon_view_calculate_item_size (GtkIconView     *icon_view,
2940                                    GtkIconViewItem *item)
2941 {
2942   gint spacing;
2943   GList *l;
2944
2945   if (item->width != -1 && item->height != -1) 
2946     return;
2947
2948   if (item->n_cells != icon_view->priv->n_cells)
2949     {
2950       g_free (item->before);
2951       g_free (item->after);
2952       g_free (item->box);
2953       
2954       item->before = g_new0 (gint, icon_view->priv->n_cells);
2955       item->after = g_new0 (gint, icon_view->priv->n_cells);
2956       item->box = g_new0 (GdkRectangle, icon_view->priv->n_cells);
2957
2958       item->n_cells = icon_view->priv->n_cells;
2959     }
2960
2961   gtk_icon_view_set_cell_data (icon_view, item);
2962
2963   spacing = icon_view->priv->spacing;
2964
2965   item->width = 0;
2966   item->height = 0;
2967   for (l = icon_view->priv->cell_list; l; l = l->next)
2968     {
2969       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2970       
2971       if (!info->cell->visible)
2972         continue;
2973       
2974       gtk_cell_renderer_get_size (info->cell, GTK_WIDGET (icon_view), 
2975                                   NULL, NULL, NULL,
2976                                   &item->box[info->position].width, 
2977                                   &item->box[info->position].height);
2978
2979       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2980         {
2981           item->width += item->box[info->position].width 
2982             + (info->position > 0 ? spacing : 0);
2983           item->height = MAX (item->height, item->box[info->position].height);
2984         }
2985       else
2986         {
2987           item->width = MAX (item->width, item->box[info->position].width);
2988           item->height += item->box[info->position].height + (info->position > 0 ? spacing : 0);
2989         }
2990     }
2991
2992   item->width += ITEM_PADDING * 2;
2993   item->height += ITEM_PADDING * 2;
2994 }
2995
2996 static void
2997 gtk_icon_view_calculate_item_size2 (GtkIconView     *icon_view,
2998                                     GtkIconViewItem *item,
2999                                     gint            *max_height)
3000 {
3001   GdkRectangle cell_area;
3002   gint spacing;
3003   GList *l;
3004   gint i, k;
3005   gboolean rtl;
3006
3007   rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
3008
3009   gtk_icon_view_set_cell_data (icon_view, item);
3010
3011   spacing = icon_view->priv->spacing;
3012
3013   item->height = 0;
3014   for (i = 0; i < icon_view->priv->n_cells; i++)
3015     {
3016       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3017         item->height = MAX (item->height, max_height[i]);
3018       else
3019         item->height += max_height[i] + (i > 0 ? spacing : 0);
3020     }
3021
3022   cell_area.x = item->x + ITEM_PADDING;
3023   cell_area.y = item->y + ITEM_PADDING;
3024       
3025   for (k = 0; k < 2; k++)
3026     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
3027       {
3028         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3029         
3030         if (info->pack == (k ? GTK_PACK_START : GTK_PACK_END))
3031           continue;
3032
3033         if (!info->cell->visible)
3034           continue;
3035
3036         if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3037           {
3038             /* We should not subtract ITEM_PADDING from item->height,
3039              * because item->height is recalculated above using
3040              * max_height which does not contain item padding.
3041              */
3042             cell_area.width = item->box[info->position].width;
3043             cell_area.height = item->height;
3044           }
3045         else
3046           {
3047             /* item->width is not recalculated and thus needs to be
3048              * corrected for the padding.
3049              */
3050             cell_area.width = item->width - 2 * ITEM_PADDING;
3051             cell_area.height = max_height[i];
3052           }
3053         
3054         gtk_cell_renderer_get_size (info->cell, GTK_WIDGET (icon_view), 
3055                                     &cell_area,
3056                                     &item->box[info->position].x, &item->box[info->position].y,
3057                                     &item->box[info->position].width, &item->box[info->position].height);
3058         
3059         item->box[info->position].x += cell_area.x;
3060         item->box[info->position].y += cell_area.y;
3061         if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3062           {
3063             item->before[info->position] = item->box[info->position].x - cell_area.x;
3064             item->after[info->position] = cell_area.width - item->box[info->position].width - item->before[info->position];
3065             cell_area.x += cell_area.width + spacing;
3066           }
3067         else
3068           {
3069             if (item->box[info->position].width > item->width - ITEM_PADDING * 2)
3070               {
3071                 item->width = item->box[info->position].width + ITEM_PADDING * 2;
3072                 cell_area.width = item->width;
3073               }
3074             item->before[info->position] = item->box[info->position].y - cell_area.y;
3075             item->after[info->position] = cell_area.height - item->box[info->position].height - item->before[info->position];
3076             cell_area.y += cell_area.height + spacing;
3077           }
3078       }
3079   
3080   if (rtl && icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
3081     {
3082       for (i = 0; i < icon_view->priv->n_cells; i++)
3083         {
3084           item->box[i].x = item->x + item->width - 
3085             (item->box[i].x + item->box[i].width - item->x);
3086         }      
3087     }
3088
3089   item->height += ITEM_PADDING * 2;
3090 }
3091
3092 static void
3093 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
3094 {
3095   g_list_foreach (icon_view->priv->items,
3096                   (GFunc)gtk_icon_view_item_invalidate_size, NULL);
3097 }
3098
3099 static void
3100 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
3101 {
3102   item->width = -1;
3103   item->height = -1;
3104 }
3105
3106 static void
3107 gtk_icon_view_paint_item (GtkIconView     *icon_view,
3108                           cairo_t         *cr,
3109                           GtkIconViewItem *item,
3110                           GdkRectangle    *area,
3111                           GdkDrawable     *drawable,
3112                           gint             x,
3113                           gint             y,
3114                           gboolean         draw_focus)
3115 {
3116   gint focus_width;
3117   gint padding;
3118   GdkRectangle cell_area, box;
3119   GList *l;
3120   gint i;
3121   GtkStateType state;
3122   GtkCellRendererState flags;
3123       
3124   if (icon_view->priv->model == NULL)
3125     return;
3126   
3127   gtk_icon_view_set_cell_data (icon_view, item);
3128
3129   gtk_widget_style_get (GTK_WIDGET (icon_view),
3130                         "focus-line-width", &focus_width,
3131                         NULL);
3132   
3133   padding = focus_width; 
3134   
3135   if (item->selected)
3136     {
3137       flags = GTK_CELL_RENDERER_SELECTED;
3138       if (GTK_WIDGET_HAS_FOCUS (icon_view))
3139         state = GTK_STATE_SELECTED;
3140       else
3141         state = GTK_STATE_ACTIVE;
3142     }
3143   else
3144     {
3145       flags = 0;
3146       state = GTK_STATE_NORMAL;
3147     }
3148   
3149 #ifdef DEBUG_ICON_VIEW
3150   gdk_draw_rectangle (drawable,
3151                       GTK_WIDGET (icon_view)->style->black_gc,
3152                       FALSE,
3153                       x, y, 
3154                       item->width, item->height);
3155 #endif
3156
3157   if (item->selected)
3158     {
3159       gtk_paint_flat_box (GTK_WIDGET (icon_view)->style,
3160                           (GdkWindow *) drawable,
3161                           GTK_STATE_SELECTED,
3162                           GTK_SHADOW_NONE,
3163                           area,
3164                           GTK_WIDGET (icon_view),
3165                           "icon_view_item",
3166                           x, y,
3167                           item->width, item->height);
3168     }
3169   
3170   for (l = icon_view->priv->cell_list; l; l = l->next)
3171     {
3172       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3173       
3174       if (!info->cell->visible)
3175         continue;
3176       
3177       gtk_icon_view_get_cell_area (icon_view, item, info, &cell_area);
3178       
3179 #ifdef DEBUG_ICON_VIEW
3180       gdk_draw_rectangle (drawable,
3181                           GTK_WIDGET (icon_view)->style->black_gc,
3182                           FALSE,
3183                           x - item->x + cell_area.x, 
3184                           y - item->y + cell_area.y, 
3185                           cell_area.width, cell_area.height);
3186
3187       gtk_icon_view_get_cell_box (icon_view, item, info, &box);
3188           
3189       gdk_draw_rectangle (drawable,
3190                           GTK_WIDGET (icon_view)->style->black_gc,
3191                           FALSE,
3192                           x - item->x + box.x, 
3193                           y - item->y + box.y, 
3194                           box.width, box.height);
3195 #endif
3196
3197       cell_area.x = x - item->x + cell_area.x;
3198       cell_area.y = y - item->y + cell_area.y;
3199
3200       gtk_cell_renderer_render (info->cell,
3201                                 drawable,
3202                                 GTK_WIDGET (icon_view),
3203                                 &cell_area, &cell_area, area, flags);
3204     }
3205
3206   if (draw_focus &&
3207       GTK_WIDGET_HAS_FOCUS (icon_view) &&
3208       item == icon_view->priv->cursor_item)
3209     {
3210       for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
3211         {
3212           GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3213
3214           if (!info->cell->visible)
3215             continue;
3216
3217           /* If found a editable/activatable cell, draw focus on it. */
3218           if (icon_view->priv->cursor_cell < 0 &&
3219               info->cell->mode != GTK_CELL_RENDERER_MODE_INERT)
3220             icon_view->priv->cursor_cell = i;
3221
3222           gtk_icon_view_get_cell_box (icon_view, item, info, &box);
3223
3224           if (i == icon_view->priv->cursor_cell)
3225             {
3226               gtk_paint_focus (GTK_WIDGET (icon_view)->style,
3227                                drawable,
3228                                GTK_STATE_NORMAL,
3229                                area,
3230                                GTK_WIDGET (icon_view),
3231                                "icon_view",
3232                                x - item->x + box.x - padding,
3233                                y - item->y + box.y - padding,
3234                                box.width + 2 * padding,
3235                                box.height + 2 * padding);
3236               break;
3237             }
3238         }
3239
3240       /* If there are no editable/activatable cells, draw focus 
3241        * around the whole item.
3242        */
3243       if (icon_view->priv->cursor_cell < 0)
3244         gtk_paint_focus (GTK_WIDGET (icon_view)->style,
3245                          drawable,
3246                          GTK_STATE_NORMAL,
3247                          area,
3248                          GTK_WIDGET (icon_view),
3249                          "icon_view",
3250                          x - padding,
3251                          y - padding,
3252                          item->width + 2 * padding,
3253                          item->height + 2 * padding);
3254     }
3255 }
3256
3257 static void
3258 gtk_icon_view_paint_rubberband (GtkIconView     *icon_view,
3259                                 cairo_t         *cr,
3260                                 GdkRectangle    *area)
3261 {
3262   GdkRectangle rect;
3263   GdkRectangle rubber_rect;
3264   GdkColor *fill_color_gdk;
3265   guchar fill_color_alpha;
3266
3267   rubber_rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
3268   rubber_rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
3269   rubber_rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
3270   rubber_rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
3271
3272   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
3273     return;
3274
3275   gtk_widget_style_get (GTK_WIDGET (icon_view),
3276                         "selection-box-color", &fill_color_gdk,
3277                         "selection-box-alpha", &fill_color_alpha,
3278                         NULL);
3279
3280   if (!fill_color_gdk)
3281     fill_color_gdk = gdk_color_copy (&GTK_WIDGET (icon_view)->style->base[GTK_STATE_SELECTED]);
3282
3283   cairo_set_source_rgba (cr,
3284                          fill_color_gdk->red / 65535.,
3285                          fill_color_gdk->green / 65535.,
3286                          fill_color_gdk->blue / 65535.,
3287                          fill_color_alpha / 255.);
3288
3289   cairo_save (cr);
3290   gdk_cairo_rectangle (cr, &rect);
3291   cairo_clip (cr);
3292   cairo_paint (cr);
3293
3294   /* Draw the border without alpha */
3295   cairo_set_source_rgb (cr,
3296                         fill_color_gdk->red / 65535.,
3297                         fill_color_gdk->green / 65535.,
3298                         fill_color_gdk->blue / 65535.);
3299   cairo_rectangle (cr, 
3300                    rubber_rect.x + 0.5, rubber_rect.y + 0.5,
3301                    rubber_rect.width - 1, rubber_rect.height - 1);
3302   cairo_stroke (cr);
3303   cairo_restore (cr);
3304
3305   gdk_color_free (fill_color_gdk);
3306 }
3307
3308 static void
3309 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
3310                                GtkTreePath *path)
3311 {
3312   GList *l;
3313   gint index;
3314
3315   index = gtk_tree_path_get_indices (path)[0];
3316
3317   for (l = icon_view->priv->items; l; l = l->next) 
3318     {
3319       GtkIconViewItem *item = l->data;
3320
3321       if (item->index == index)
3322         {
3323           gtk_icon_view_queue_draw_item (icon_view, item);
3324           break;
3325         }
3326     }
3327 }
3328
3329 static void
3330 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
3331                                GtkIconViewItem *item)
3332 {
3333   gint focus_width;
3334   GdkRectangle rect;
3335
3336   gtk_widget_style_get (GTK_WIDGET (icon_view),
3337                         "focus-line-width", &focus_width,
3338                         NULL);
3339
3340   rect.x = item->x - focus_width;
3341   rect.y = item->y - focus_width;
3342   rect.width = item->width + 2 * focus_width;
3343   rect.height = item->height + 2 * focus_width;
3344
3345   if (icon_view->priv->bin_window)
3346     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
3347 }
3348
3349 static gboolean
3350 layout_callback (gpointer user_data)
3351 {
3352   GtkIconView *icon_view;
3353
3354   icon_view = GTK_ICON_VIEW (user_data);
3355   
3356   icon_view->priv->layout_idle_id = 0;
3357
3358   gtk_icon_view_layout (icon_view);
3359   
3360   return FALSE;
3361 }
3362
3363 static void
3364 gtk_icon_view_queue_layout (GtkIconView *icon_view)
3365 {
3366   if (icon_view->priv->layout_idle_id != 0)
3367     return;
3368
3369   icon_view->priv->layout_idle_id = gdk_threads_add_idle (layout_callback, icon_view);
3370 }
3371
3372 static void
3373 gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
3374                                GtkIconViewItem *item,
3375                                gint             cursor_cell)
3376 {
3377   AtkObject *obj;
3378   AtkObject *item_obj;
3379   AtkObject *cursor_item_obj;
3380
3381   if (icon_view->priv->cursor_item == item &&
3382       (cursor_cell < 0 || cursor_cell == icon_view->priv->cursor_cell))
3383     return;
3384
3385   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3386   if (icon_view->priv->cursor_item != NULL)
3387     {
3388       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3389       if (obj != NULL)
3390         {
3391           cursor_item_obj = atk_object_ref_accessible_child (obj, icon_view->priv->cursor_item->index);
3392           if (cursor_item_obj != NULL)
3393             atk_object_notify_state_change (cursor_item_obj, ATK_STATE_FOCUSED, FALSE);
3394         }
3395     }
3396   icon_view->priv->cursor_item = item;
3397   if (cursor_cell >= 0)
3398     icon_view->priv->cursor_cell = cursor_cell;
3399
3400   gtk_icon_view_queue_draw_item (icon_view, item);
3401   
3402   /* Notify that accessible focus object has changed */
3403   item_obj = atk_object_ref_accessible_child (obj, item->index);
3404
3405   if (item_obj != NULL)
3406     {
3407       atk_focus_tracker_notify (item_obj);
3408       atk_object_notify_state_change (item_obj, ATK_STATE_FOCUSED, TRUE);
3409       g_object_unref (item_obj); 
3410     }
3411 }
3412
3413
3414 static GtkIconViewItem *
3415 gtk_icon_view_item_new (void)
3416 {
3417   GtkIconViewItem *item;
3418
3419   item = g_new0 (GtkIconViewItem, 1);
3420
3421   item->width = -1;
3422   item->height = -1;
3423   
3424   return item;
3425 }
3426
3427 static void
3428 gtk_icon_view_item_free (GtkIconViewItem *item)
3429 {
3430   g_return_if_fail (item != NULL);
3431
3432   g_free (item->before);
3433   g_free (item->after);
3434   g_free (item->box);
3435
3436   g_free (item);
3437 }
3438
3439
3440 static GtkIconViewItem *
3441 gtk_icon_view_get_item_at_coords (GtkIconView          *icon_view,
3442                                   gint                  x,
3443                                   gint                  y,
3444                                   gboolean              only_in_cell,
3445                                   GtkIconViewCellInfo **cell_at_pos)
3446 {
3447   GList *items, *l;
3448   GdkRectangle box;
3449
3450   for (items = icon_view->priv->items; items; items = items->next)
3451     {
3452       GtkIconViewItem *item = items->data;
3453
3454       if (x >= item->x - icon_view->priv->column_spacing/2 && x <= item->x + item->width + icon_view->priv->column_spacing/2 &&
3455           y >= item->y - icon_view->priv->row_spacing/2 && y <= item->y + item->height + icon_view->priv->row_spacing/2)
3456         {
3457           if (only_in_cell || cell_at_pos)
3458             {
3459               gtk_icon_view_set_cell_data (icon_view, item);
3460
3461               for (l = icon_view->priv->cell_list; l; l = l->next)
3462                 {
3463                   GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3464                   
3465                   if (!info->cell->visible)
3466                     continue;
3467                   
3468                   gtk_icon_view_get_cell_box (icon_view, item, info, &box);
3469                   
3470                   if ((x >= box.x && x <= box.x + box.width &&
3471                        y >= box.y && y <= box.y + box.height) ||
3472                       (x >= box.x  &&
3473                        x <= box.x + box.width &&
3474                        y >= box.y &&
3475                        y <= box.y + box.height))
3476                     {
3477                       if (cell_at_pos)
3478                         *cell_at_pos = info;
3479                       
3480                       return item;
3481                     }
3482                 }
3483
3484               if (only_in_cell)
3485                 return NULL;
3486               
3487               if (cell_at_pos)
3488                 *cell_at_pos = NULL;
3489             }
3490
3491           return item;
3492         }
3493     }
3494
3495   return NULL;
3496 }
3497
3498 static void
3499 gtk_icon_view_select_item (GtkIconView      *icon_view,
3500                            GtkIconViewItem  *item)
3501 {
3502   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3503   g_return_if_fail (item != NULL);
3504
3505   if (item->selected)
3506     return;
3507   
3508   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3509     return;
3510   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3511     gtk_icon_view_unselect_all_internal (icon_view);
3512
3513   item->selected = TRUE;
3514
3515   gtk_icon_view_item_selected_changed (icon_view, item);
3516   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3517
3518   gtk_icon_view_queue_draw_item (icon_view, item);
3519 }
3520
3521
3522 static void
3523 gtk_icon_view_unselect_item (GtkIconView      *icon_view,
3524                              GtkIconViewItem  *item)
3525 {
3526   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3527   g_return_if_fail (item != NULL);
3528
3529   if (!item->selected)
3530     return;
3531   
3532   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3533       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3534     return;
3535   
3536   item->selected = FALSE;
3537
3538   gtk_icon_view_item_selected_changed (icon_view, item);
3539   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3540
3541   gtk_icon_view_queue_draw_item (icon_view, item);
3542 }
3543
3544 static void
3545 verify_items (GtkIconView *icon_view)
3546 {
3547   GList *items;
3548   int i = 0;
3549
3550   for (items = icon_view->priv->items; items; items = items->next)
3551     {
3552       GtkIconViewItem *item = items->data;
3553
3554       if (item->index != i)
3555         g_error ("List item does not match its index: "
3556                  "item index %d and list index %d\n", item->index, i);
3557
3558       i++;
3559     }
3560 }
3561
3562 static void
3563 gtk_icon_view_row_changed (GtkTreeModel *model,
3564                            GtkTreePath  *path,
3565                            GtkTreeIter  *iter,
3566                            gpointer      data)
3567 {
3568   GtkIconViewItem *item;
3569   gint index;
3570   GtkIconView *icon_view;
3571
3572   icon_view = GTK_ICON_VIEW (data);
3573
3574   gtk_icon_view_stop_editing (icon_view, TRUE);
3575   
3576   index = gtk_tree_path_get_indices(path)[0];
3577   item = g_list_nth_data (icon_view->priv->items, index);
3578
3579   gtk_icon_view_item_invalidate_size (item);
3580   gtk_icon_view_queue_layout (icon_view);
3581
3582   verify_items (icon_view);
3583 }
3584
3585 static void
3586 gtk_icon_view_row_inserted (GtkTreeModel *model,
3587                             GtkTreePath  *path,
3588                             GtkTreeIter  *iter,
3589                             gpointer      data)
3590 {
3591   gint index;
3592   GtkIconViewItem *item;
3593   gboolean iters_persist;
3594   GtkIconView *icon_view;
3595   GList *list;
3596   
3597   icon_view = GTK_ICON_VIEW (data);
3598
3599   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3600   
3601   index = gtk_tree_path_get_indices(path)[0];
3602
3603   item = gtk_icon_view_item_new ();
3604
3605   if (iters_persist)
3606     item->iter = *iter;
3607
3608   item->index = index;
3609
3610   /* FIXME: We can be more efficient here,
3611      we can store a tail pointer and use that when
3612      appending (which is a rather common operation)
3613   */
3614   icon_view->priv->items = g_list_insert (icon_view->priv->items,
3615                                          item, index);
3616   
3617   list = g_list_nth (icon_view->priv->items, index + 1);
3618   for (; list; list = list->next)
3619     {
3620       item = list->data;
3621
3622       item->index++;
3623     }
3624     
3625   verify_items (icon_view);
3626
3627   gtk_icon_view_queue_layout (icon_view);
3628 }
3629
3630 static void
3631 gtk_icon_view_row_deleted (GtkTreeModel *model,
3632                            GtkTreePath  *path,
3633                            gpointer      data)
3634 {
3635   gint index;
3636   GtkIconView *icon_view;
3637   GtkIconViewItem *item;
3638   GList *list, *next;
3639   gboolean emit = FALSE;
3640   
3641   icon_view = GTK_ICON_VIEW (data);
3642
3643   index = gtk_tree_path_get_indices(path)[0];
3644
3645   list = g_list_nth (icon_view->priv->items, index);
3646   item = list->data;
3647
3648   gtk_icon_view_stop_editing (icon_view, TRUE);
3649
3650   if (item == icon_view->priv->anchor_item)
3651     icon_view->priv->anchor_item = NULL;
3652
3653   if (item == icon_view->priv->cursor_item)
3654     icon_view->priv->cursor_item = NULL;
3655
3656   if (item->selected)
3657     emit = TRUE;
3658   
3659   gtk_icon_view_item_free (item);
3660
3661   for (next = list->next; next; next = next->next)
3662     {
3663       item = next->data;
3664
3665       item->index--;
3666     }
3667   
3668   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3669
3670   verify_items (icon_view);  
3671   
3672   gtk_icon_view_queue_layout (icon_view);
3673
3674   if (emit)
3675     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3676 }
3677
3678 static void
3679 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3680                               GtkTreePath  *parent,
3681                               GtkTreeIter  *iter,
3682                               gint         *new_order,
3683                               gpointer      data)
3684 {
3685   int i;
3686   int length;
3687   GtkIconView *icon_view;
3688   GList *items = NULL, *list;
3689   GtkIconViewItem **item_array;
3690   gint *order;
3691   
3692   icon_view = GTK_ICON_VIEW (data);
3693
3694   gtk_icon_view_stop_editing (icon_view, TRUE);
3695
3696   length = gtk_tree_model_iter_n_children (model, NULL);
3697
3698   order = g_new (gint, length);
3699   for (i = 0; i < length; i++)
3700     order [new_order[i]] = i;
3701
3702   item_array = g_new (GtkIconViewItem *, length);
3703   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3704     item_array[order[i]] = list->data;
3705   g_free (order);
3706
3707   for (i = length - 1; i >= 0; i--)
3708     {
3709       item_array[i]->index = i;
3710       items = g_list_prepend (items, item_array[i]);
3711     }
3712   
3713   g_free (item_array);
3714   g_list_free (icon_view->priv->items);
3715   icon_view->priv->items = items;
3716
3717   gtk_icon_view_queue_layout (icon_view);
3718
3719   verify_items (icon_view);  
3720 }
3721
3722 static void
3723 gtk_icon_view_build_items (GtkIconView *icon_view)
3724 {
3725   GtkTreeIter iter;
3726   int i;
3727   gboolean iters_persist;
3728   GList *items = NULL;
3729
3730   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3731   
3732   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3733                                       &iter))
3734     return;
3735
3736   i = 0;
3737   
3738   do
3739     {
3740       GtkIconViewItem *item = gtk_icon_view_item_new ();
3741
3742       if (iters_persist)
3743         item->iter = iter;
3744
3745       item->index = i;
3746       
3747       i++;
3748
3749       items = g_list_prepend (items, item);
3750       
3751     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3752
3753   icon_view->priv->items = g_list_reverse (items);
3754 }
3755
3756 static void
3757 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
3758                                 guint           keyval,
3759                                 guint           modmask,
3760                                 GtkMovementStep step,
3761                                 gint            count)
3762 {
3763   
3764   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3765                                 I_("move-cursor"), 2,
3766                                 G_TYPE_ENUM, step,
3767                                 G_TYPE_INT, count);
3768
3769   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3770                                 "move-cursor", 2,
3771                                 G_TYPE_ENUM, step,
3772                                 G_TYPE_INT, count);
3773
3774   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3775    return;
3776
3777   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3778                                 "move-cursor", 2,
3779                                 G_TYPE_ENUM, step,
3780                                 G_TYPE_INT, count);
3781
3782   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3783                                 "move-cursor", 2,
3784                                 G_TYPE_ENUM, step,
3785                                 G_TYPE_INT, count);
3786 }
3787
3788 static gboolean
3789 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
3790                                 GtkMovementStep  step,
3791                                 gint             count)
3792 {
3793   GdkModifierType state;
3794
3795   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3796   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3797                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3798                         step == GTK_MOVEMENT_DISPLAY_LINES ||
3799                         step == GTK_MOVEMENT_PAGES ||
3800                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3801
3802   if (!GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (icon_view)))
3803     return FALSE;
3804
3805   gtk_icon_view_stop_editing (icon_view, FALSE);
3806   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3807
3808   if (gtk_get_current_event_state (&state))
3809     {
3810       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3811         icon_view->priv->ctrl_pressed = TRUE;
3812       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
3813         icon_view->priv->shift_pressed = TRUE;
3814     }
3815   /* else we assume not pressed */
3816
3817   switch (step)
3818     {
3819     case GTK_MOVEMENT_LOGICAL_POSITIONS:
3820     case GTK_MOVEMENT_VISUAL_POSITIONS:
3821       gtk_icon_view_move_cursor_left_right (icon_view, count);
3822       break;
3823     case GTK_MOVEMENT_DISPLAY_LINES:
3824       gtk_icon_view_move_cursor_up_down (icon_view, count);
3825       break;
3826     case GTK_MOVEMENT_PAGES:
3827       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3828       break;
3829     case GTK_MOVEMENT_BUFFER_ENDS:
3830       gtk_icon_view_move_cursor_start_end (icon_view, count);
3831       break;
3832     default:
3833       g_assert_not_reached ();
3834     }
3835
3836   icon_view->priv->ctrl_pressed = FALSE;
3837   icon_view->priv->shift_pressed = FALSE;
3838
3839   icon_view->priv->draw_focus = TRUE;
3840
3841   return TRUE;
3842 }
3843
3844 static GtkIconViewItem *
3845 find_item (GtkIconView     *icon_view,
3846            GtkIconViewItem *current,
3847            gint             row_ofs,
3848            gint             col_ofs)
3849 {
3850   gint row, col;
3851   GList *items;
3852   GtkIconViewItem *item;
3853
3854   /* FIXME: this could be more efficient 
3855    */
3856   row = current->row + row_ofs;
3857   col = current->col + col_ofs;
3858
3859   for (items = icon_view->priv->items; items; items = items->next)
3860     {
3861       item = items->data;
3862       if (item->row == row && item->col == col)
3863         return item;
3864     }
3865   
3866   return NULL;
3867 }
3868
3869 static gint
3870 find_cell (GtkIconView     *icon_view,
3871            GtkIconViewItem *item,
3872            gint             cell,
3873            GtkOrientation   orientation,
3874            gint             step,
3875            gint            *count)
3876 {
3877   gint n_focusable;
3878   gint *focusable;
3879   gint current;
3880   gint i, k;
3881   GList *l;
3882
3883   if (icon_view->priv->orientation != orientation)
3884     return cell;
3885
3886   gtk_icon_view_set_cell_data (icon_view, item);
3887
3888   focusable = g_new0 (gint, icon_view->priv->n_cells);
3889   n_focusable = 0;
3890
3891   current = 0;
3892   for (k = 0; k < 2; k++)
3893     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
3894       {
3895         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3896         
3897         if (info->pack == (k ? GTK_PACK_START : GTK_PACK_END))
3898           continue;
3899         
3900         if (!info->cell->visible)
3901           continue;
3902
3903         if (info->cell->mode != GTK_CELL_RENDERER_MODE_INERT)
3904           {
3905             if (cell == i)
3906               current = n_focusable;
3907
3908             focusable[n_focusable] = i;
3909
3910             n_focusable++;
3911           }
3912       }
3913   
3914   if (n_focusable == 0)
3915     {
3916       g_free (focusable);
3917       return -1;
3918     }
3919
3920   if (cell < 0)
3921     {
3922       current = step > 0 ? 0 : n_focusable - 1;
3923       cell = focusable[current];
3924     }
3925
3926   if (current + *count < 0)
3927     {
3928       cell = -1;
3929       *count = current + *count;
3930     }
3931   else if (current + *count > n_focusable - 1)
3932     {
3933       cell = -1;
3934       *count = current + *count - (n_focusable - 1);
3935     }
3936   else
3937     {
3938       cell = focusable[current + *count];
3939       *count = 0;
3940     }
3941   
3942   g_free (focusable);
3943   
3944   return cell;
3945 }
3946
3947 static GtkIconViewItem *
3948 find_item_page_up_down (GtkIconView     *icon_view,
3949                         GtkIconViewItem *current,
3950                         gint             count)
3951 {
3952   GList *item, *next;
3953   gint y, col;
3954   
3955   col = current->col;
3956   y = current->y + count * icon_view->priv->vadjustment->page_size;
3957
3958   item = g_list_find (icon_view->priv->items, current);
3959   if (count > 0)
3960     {
3961       while (item)
3962         {
3963           for (next = item->next; next; next = next->next)
3964             {
3965               if (((GtkIconViewItem *)next->data)->col == col)
3966                 break;
3967             }
3968           if (!next || ((GtkIconViewItem *)next->data)->y > y)
3969             break;
3970
3971           item = next;
3972         }
3973     }
3974   else 
3975     {
3976       while (item)
3977         {
3978           for (next = item->prev; next; next = next->prev)
3979             {
3980               if (((GtkIconViewItem *)next->data)->col == col)
3981                 break;
3982             }
3983           if (!next || ((GtkIconViewItem *)next->data)->y < y)
3984             break;
3985
3986           item = next;
3987         }
3988     }
3989
3990   if (item)
3991     return item->data;
3992
3993   return NULL;
3994 }
3995
3996 static gboolean
3997 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
3998                                   GtkIconViewItem *anchor,
3999                                   GtkIconViewItem *cursor)
4000 {
4001   GList *items;
4002   GtkIconViewItem *item;
4003   gint row1, row2, col1, col2;
4004   gboolean dirty = FALSE;
4005   
4006   if (anchor->row < cursor->row)
4007     {
4008       row1 = anchor->row;
4009       row2 = cursor->row;
4010     }
4011   else
4012     {
4013       row1 = cursor->row;
4014       row2 = anchor->row;
4015     }
4016
4017   if (anchor->col < cursor->col)
4018     {
4019       col1 = anchor->col;
4020       col2 = cursor->col;
4021     }
4022   else
4023     {
4024       col1 = cursor->col;
4025       col2 = anchor->col;
4026     }
4027
4028   for (items = icon_view->priv->items; items; items = items->next)
4029     {
4030       item = items->data;
4031
4032       if (row1 <= item->row && item->row <= row2 &&
4033           col1 <= item->col && item->col <= col2)
4034         {
4035           if (!item->selected)
4036             {
4037               dirty = TRUE;
4038               item->selected = TRUE;
4039               gtk_icon_view_item_selected_changed (icon_view, item);
4040             }
4041           gtk_icon_view_queue_draw_item (icon_view, item);
4042         }
4043     }
4044
4045   return dirty;
4046 }
4047
4048 static void 
4049 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
4050                                    gint         count)
4051 {
4052   GtkIconViewItem *item;
4053   gint cell;
4054   gboolean dirty = FALSE;
4055   gint step;
4056   
4057   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
4058     return;
4059   
4060   if (!icon_view->priv->cursor_item)
4061     {
4062       GList *list;
4063
4064       if (count > 0)
4065         list = icon_view->priv->items;
4066       else
4067         list = g_list_last (icon_view->priv->items);
4068
4069       item = list ? list->data : NULL;
4070       cell = -1;
4071     }
4072   else
4073     {
4074       item = icon_view->priv->cursor_item;
4075       cell = icon_view->priv->cursor_cell;
4076       step = count > 0 ? 1 : -1;      
4077       while (item)
4078         {
4079           cell = find_cell (icon_view, item, cell,
4080                             GTK_ORIENTATION_VERTICAL, 
4081                             step, &count);
4082           if (count == 0)
4083             break;
4084
4085           item = find_item (icon_view, item, step, 0);
4086           count = count - step;
4087         }
4088     }
4089
4090   if (!item)
4091     {
4092       gtk_widget_error_bell (GTK_WIDGET (icon_view));
4093       return;
4094     }
4095
4096   if (icon_view->priv->ctrl_pressed ||
4097       !icon_view->priv->shift_pressed ||
4098       !icon_view->priv->anchor_item ||
4099       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4100     icon_view->priv->anchor_item = item;
4101
4102   gtk_icon_view_set_cursor_item (icon_view, item, cell);
4103
4104   if (!icon_view->priv->ctrl_pressed &&
4105       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4106     {
4107       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4108       dirty = gtk_icon_view_select_all_between (icon_view, 
4109                                                 icon_view->priv->anchor_item,
4110                                                 item) || dirty;
4111     }
4112
4113   gtk_icon_view_scroll_to_item (icon_view, item);
4114
4115   if (dirty)
4116     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4117 }
4118
4119 static void 
4120 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
4121                                         gint         count)
4122 {
4123   GtkIconViewItem *item;
4124   gboolean dirty = FALSE;
4125   
4126   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
4127     return;
4128   
4129   if (!icon_view->priv->cursor_item)
4130     {
4131       GList *list;
4132
4133       if (count > 0)
4134         list = icon_view->priv->items;
4135       else
4136         list = g_list_last (icon_view->priv->items);
4137
4138       item = list ? list->data : NULL;
4139     }
4140   else
4141     item = find_item_page_up_down (icon_view, 
4142                                    icon_view->priv->cursor_item,
4143                                    count);
4144
4145   if (item == icon_view->priv->cursor_item)
4146     gtk_widget_error_bell (GTK_WIDGET (icon_view));
4147
4148   if (!item)
4149     return;
4150
4151   if (icon_view->priv->ctrl_pressed ||
4152       !icon_view->priv->shift_pressed ||
4153       !icon_view->priv->anchor_item ||
4154       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4155     icon_view->priv->anchor_item = item;
4156
4157   gtk_icon_view_set_cursor_item (icon_view, item, -1);
4158
4159   if (!icon_view->priv->ctrl_pressed &&
4160       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4161     {
4162       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4163       dirty = gtk_icon_view_select_all_between (icon_view, 
4164                                                 icon_view->priv->anchor_item,
4165                                                 item) || dirty;
4166     }
4167
4168   gtk_icon_view_scroll_to_item (icon_view, item);
4169
4170   if (dirty)
4171     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);  
4172 }
4173
4174 static void 
4175 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
4176                                       gint         count)
4177 {
4178   GtkIconViewItem *item;
4179   gint cell = -1;
4180   gboolean dirty = FALSE;
4181   gint step;
4182   
4183   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
4184     return;
4185   
4186   if (!icon_view->priv->cursor_item)
4187     {
4188       GList *list;
4189
4190       if (count > 0)
4191         list = icon_view->priv->items;
4192       else
4193         list = g_list_last (icon_view->priv->items);
4194
4195       item = list ? list->data : NULL;
4196     }
4197   else
4198     {
4199       item = icon_view->priv->cursor_item;
4200       cell = icon_view->priv->cursor_cell;
4201       step = count > 0 ? 1 : -1;
4202       while (item)
4203         {
4204           cell = find_cell (icon_view, item, cell,
4205                             GTK_ORIENTATION_HORIZONTAL, 
4206                             step, &count);
4207           if (count == 0)
4208             break;
4209           
4210           item = find_item (icon_view, item, 0, step);
4211           count = count - step;
4212         }
4213     }
4214
4215   if (!item)
4216     {
4217       gtk_widget_error_bell (GTK_WIDGET (icon_view));
4218       return;
4219     }
4220
4221   if (icon_view->priv->ctrl_pressed ||
4222       !icon_view->priv->shift_pressed ||
4223       !icon_view->priv->anchor_item ||
4224       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4225     icon_view->priv->anchor_item = item;
4226
4227   gtk_icon_view_set_cursor_item (icon_view, item, cell);
4228
4229   if (!icon_view->priv->ctrl_pressed &&
4230       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4231     {
4232       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4233       dirty = gtk_icon_view_select_all_between (icon_view, 
4234                                                 icon_view->priv->anchor_item,
4235                                                 item) || dirty;
4236     }
4237
4238   gtk_icon_view_scroll_to_item (icon_view, item);
4239
4240   if (dirty)
4241     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4242 }
4243
4244 static void 
4245 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
4246                                      gint         count)
4247 {
4248   GtkIconViewItem *item;
4249   GList *list;
4250   gboolean dirty = FALSE;
4251   
4252   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
4253     return;
4254   
4255   if (count < 0)
4256     list = icon_view->priv->items;
4257   else
4258     list = g_list_last (icon_view->priv->items);
4259   
4260   item = list ? list->data : NULL;
4261
4262   if (item == icon_view->priv->cursor_item)
4263     gtk_widget_error_bell (GTK_WIDGET (icon_view));
4264
4265   if (!item)
4266     return;
4267
4268   if (icon_view->priv->ctrl_pressed ||
4269       !icon_view->priv->shift_pressed ||
4270       !icon_view->priv->anchor_item ||
4271       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
4272     icon_view->priv->anchor_item = item;
4273
4274   gtk_icon_view_set_cursor_item (icon_view, item, -1);
4275
4276   if (!icon_view->priv->ctrl_pressed &&
4277       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
4278     {
4279       dirty = gtk_icon_view_unselect_all_internal (icon_view);
4280       dirty = gtk_icon_view_select_all_between (icon_view, 
4281                                                 icon_view->priv->anchor_item,
4282                                                 item) || dirty;
4283     }
4284
4285   gtk_icon_view_scroll_to_item (icon_view, item);
4286
4287   if (dirty)
4288     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
4289 }
4290
4291 /**
4292  * gtk_icon_view_scroll_to_path:
4293  * @icon_view: A #GtkIconView.
4294  * @path: The path of the item to move to.
4295  * @use_align: whether to use alignment arguments, or %FALSE.
4296  * @row_align: The vertical alignment of the item specified by @path.
4297  * @col_align: The horizontal alignment of the item specified by @path.
4298  *
4299  * Moves the alignments of @icon_view to the position specified by @path.  
4300  * @row_align determines where the row is placed, and @col_align determines 
4301  * where @column is placed.  Both are expected to be between 0.0 and 1.0. 
4302  * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means 
4303  * center.
4304  *
4305  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
4306  * tree does the minimum amount of work to scroll the item onto the screen.
4307  * This means that the item will be scrolled to the edge closest to its current
4308  * position.  If the item is currently visible on the screen, nothing is done.
4309  *
4310  * This function only works if the model is set, and @path is a valid row on 
4311  * the model. If the model changes before the @icon_view is realized, the 
4312  * centered path will be modified to reflect this change.
4313  *
4314  * Since: 2.8
4315  **/
4316 void
4317 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
4318                               GtkTreePath *path,
4319                               gboolean     use_align,
4320                               gfloat       row_align,
4321                               gfloat       col_align)
4322 {
4323   GtkIconViewItem *item = NULL;
4324
4325   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4326   g_return_if_fail (path != NULL);
4327   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
4328   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
4329
4330   if (gtk_tree_path_get_depth (path) > 0)
4331     item = g_list_nth_data (icon_view->priv->items,
4332                             gtk_tree_path_get_indices(path)[0]);
4333   
4334   if (!GTK_WIDGET_REALIZED (icon_view) || !item || item->width < 0)
4335     {
4336       if (icon_view->priv->scroll_to_path)
4337         gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4338
4339       icon_view->priv->scroll_to_path = NULL;
4340
4341       if (path)
4342         icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
4343
4344       icon_view->priv->scroll_to_use_align = use_align;
4345       icon_view->priv->scroll_to_row_align = row_align;
4346       icon_view->priv->scroll_to_col_align = col_align;
4347
4348       return;
4349     }
4350
4351   if (use_align)
4352     {
4353       gint x, y;
4354       gint focus_width;
4355       gfloat offset, value;
4356
4357       gtk_widget_style_get (GTK_WIDGET (icon_view),
4358                             "focus-line-width", &focus_width,
4359                             NULL);
4360       
4361       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4362       
4363       offset =  y + item->y - focus_width - 
4364         row_align * (GTK_WIDGET (icon_view)->allocation.height - item->height);
4365       value = CLAMP (icon_view->priv->vadjustment->value + offset, 
4366                      icon_view->priv->vadjustment->lower,
4367                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
4368       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
4369
4370       offset = x + item->x - focus_width - 
4371         col_align * (GTK_WIDGET (icon_view)->allocation.width - item->width);
4372       value = CLAMP (icon_view->priv->hadjustment->value + offset, 
4373                      icon_view->priv->hadjustment->lower,
4374                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
4375       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
4376
4377       gtk_adjustment_changed (icon_view->priv->hadjustment);
4378       gtk_adjustment_changed (icon_view->priv->vadjustment);
4379     }
4380   else
4381     gtk_icon_view_scroll_to_item (icon_view, item);
4382 }
4383
4384
4385 static void     
4386 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view, 
4387                               GtkIconViewItem *item)
4388 {
4389   gint x, y, width, height;
4390   gint focus_width;
4391
4392   gtk_widget_style_get (GTK_WIDGET (icon_view),
4393                         "focus-line-width", &focus_width,
4394                         NULL);
4395
4396   gdk_drawable_get_size (GDK_DRAWABLE (icon_view->priv->bin_window), 
4397                          &width, &height);
4398   gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4399   
4400   if (y + item->y - focus_width < 0)
4401     gtk_adjustment_set_value (icon_view->priv->vadjustment, 
4402                               icon_view->priv->vadjustment->value + y + item->y - focus_width);
4403   else if (y + item->y + item->height + focus_width > GTK_WIDGET (icon_view)->allocation.height)
4404     gtk_adjustment_set_value (icon_view->priv->vadjustment, 
4405                               icon_view->priv->vadjustment->value + y + item->y + item->height 
4406                               + focus_width - GTK_WIDGET (icon_view)->allocation.height);
4407
4408   if (x + item->x - focus_width < 0)
4409     gtk_adjustment_set_value (icon_view->priv->hadjustment, 
4410                               icon_view->priv->hadjustment->value + x + item->x - focus_width);
4411   else if (x + item->x + item->width + focus_width > GTK_WIDGET (icon_view)->allocation.width)
4412     gtk_adjustment_set_value (icon_view->priv->hadjustment, 
4413                               icon_view->priv->hadjustment->value + x + item->x + item->width 
4414                               + focus_width - GTK_WIDGET (icon_view)->allocation.width);
4415
4416   gtk_adjustment_changed (icon_view->priv->hadjustment);
4417   gtk_adjustment_changed (icon_view->priv->vadjustment);
4418 }
4419
4420 /* GtkCellLayout implementation */
4421 static GtkIconViewCellInfo *
4422 gtk_icon_view_get_cell_info (GtkIconView     *icon_view,
4423                              GtkCellRenderer *renderer)
4424 {
4425   GList *i;
4426
4427   for (i = icon_view->priv->cell_list; i; i = i->next)
4428     {
4429       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)i->data;
4430
4431       if (info->cell == renderer)
4432         return info;
4433     }
4434
4435   return NULL;
4436 }
4437
4438 static void
4439 gtk_icon_view_set_cell_data (GtkIconView     *icon_view, 
4440                              GtkIconViewItem *item)
4441 {
4442   GList *i;
4443   gboolean iters_persist;
4444   GtkTreeIter iter;
4445   
4446   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
4447   
4448   if (!iters_persist)
4449     {
4450       GtkTreePath *path;
4451
4452       path = gtk_tree_path_new_from_indices (item->index, -1);
4453       if (!gtk_tree_model_get_iter (icon_view->priv->model, &iter, path))
4454         return;
4455       gtk_tree_path_free (path);
4456     }
4457   else
4458     iter = item->iter;
4459   
4460   for (i = icon_view->priv->cell_list; i; i = i->next)
4461     {
4462       GSList *j;
4463       GtkIconViewCellInfo *info = i->data;
4464
4465       g_object_freeze_notify (G_OBJECT (info->cell));
4466
4467       for (j = info->attributes; j && j->next; j = j->next->next)
4468         {
4469           gchar *property = j->data;
4470           gint column = GPOINTER_TO_INT (j->next->data);
4471           GValue value = {0, };
4472
4473           gtk_tree_model_get_value (icon_view->priv->model, &iter,
4474                                     column, &value);
4475           g_object_set_property (G_OBJECT (info->cell),
4476                                  property, &value);
4477           g_value_unset (&value);
4478         }
4479
4480       if (info->func)
4481         (* info->func) (GTK_CELL_LAYOUT (icon_view),
4482                         info->cell,
4483                         icon_view->priv->model,
4484                         &iter,
4485                         info->func_data);
4486       
4487       g_object_thaw_notify (G_OBJECT (info->cell));
4488     }  
4489 }
4490
4491 static void 
4492 free_cell_attributes (GtkIconViewCellInfo *info)
4493
4494   GSList *list;
4495
4496   list = info->attributes;
4497   while (list && list->next)
4498     {
4499       g_free (list->data);
4500       list = list->next->next;
4501     }
4502   
4503   g_slist_free (info->attributes);
4504   info->attributes = NULL;
4505 }
4506
4507 static void
4508 free_cell_info (GtkIconViewCellInfo *info)
4509 {
4510   free_cell_attributes (info);
4511
4512   g_object_unref (info->cell);
4513   
4514   if (info->destroy)
4515     (* info->destroy) (info->func_data);
4516
4517   g_free (info);
4518 }
4519
4520 static void
4521 gtk_icon_view_cell_layout_pack_start (GtkCellLayout   *layout,
4522                                       GtkCellRenderer *renderer,
4523                                       gboolean         expand)
4524 {
4525   GtkIconViewCellInfo *info;
4526   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4527
4528   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
4529   g_return_if_fail (!gtk_icon_view_get_cell_info (icon_view, renderer));
4530
4531   g_object_ref_sink (renderer);
4532
4533   info = g_new0 (GtkIconViewCellInfo, 1);
4534   info->cell = renderer;
4535   info->expand = expand ? TRUE : FALSE;
4536   info->pack = GTK_PACK_START;
4537   info->position = icon_view->priv->n_cells;
4538   
4539   icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
4540   icon_view->priv->n_cells++;
4541 }
4542
4543 static void
4544 gtk_icon_view_cell_layout_pack_end (GtkCellLayout   *layout,
4545                                     GtkCellRenderer *renderer,
4546                                     gboolean         expand)
4547 {
4548   GtkIconViewCellInfo *info;
4549   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4550
4551   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
4552   g_return_if_fail (!gtk_icon_view_get_cell_info (icon_view, renderer));
4553
4554   g_object_ref_sink (renderer);
4555
4556   info = g_new0 (GtkIconViewCellInfo, 1);
4557   info->cell = renderer;
4558   info->expand = expand ? TRUE : FALSE;
4559   info->pack = GTK_PACK_END;
4560   info->position = icon_view->priv->n_cells;
4561
4562   icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
4563   icon_view->priv->n_cells++;
4564 }
4565
4566 static void
4567 gtk_icon_view_cell_layout_add_attribute (GtkCellLayout   *layout,
4568                                          GtkCellRenderer *renderer,
4569                                          const gchar     *attribute,
4570                                          gint             column)
4571 {
4572   GtkIconViewCellInfo *info;
4573   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4574
4575   info = gtk_icon_view_get_cell_info (icon_view, renderer);
4576   g_return_if_fail (info != NULL);
4577
4578   info->attributes = g_slist_prepend (info->attributes,
4579                                       GINT_TO_POINTER (column));
4580   info->attributes = g_slist_prepend (info->attributes,
4581                                       g_strdup (attribute));
4582 }
4583
4584 static void
4585 gtk_icon_view_cell_layout_clear (GtkCellLayout *layout)
4586 {
4587   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4588
4589   while (icon_view->priv->cell_list)
4590     {
4591       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)icon_view->priv->cell_list->data;
4592       free_cell_info (info);
4593       icon_view->priv->cell_list = g_list_delete_link (icon_view->priv->cell_list, 
4594                                                        icon_view->priv->cell_list);
4595     }
4596
4597   icon_view->priv->n_cells = 0;
4598 }
4599
4600 static void
4601 gtk_icon_view_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4602                                               GtkCellRenderer       *cell,
4603                                               GtkCellLayoutDataFunc  func,
4604                                               gpointer               func_data,
4605                                               GDestroyNotify         destroy)
4606 {
4607   GtkIconViewCellInfo *info;
4608   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4609
4610   info = gtk_icon_view_get_cell_info (icon_view, cell);
4611   g_return_if_fail (info != NULL);
4612
4613   if (info->destroy)
4614     {
4615       GDestroyNotify d = info->destroy;
4616
4617       info->destroy = NULL;
4618       d (info->func_data);
4619     }
4620
4621   info->func = func;
4622   info->func_data = func_data;
4623   info->destroy = destroy;
4624 }
4625
4626 static void
4627 gtk_icon_view_cell_layout_clear_attributes (GtkCellLayout   *layout,
4628                                             GtkCellRenderer *renderer)
4629 {
4630   GtkIconViewCellInfo *info;
4631
4632   info = gtk_icon_view_get_cell_info (GTK_ICON_VIEW (layout), renderer);
4633   if (info != NULL)
4634     free_cell_attributes (info);
4635 }
4636
4637 static void
4638 gtk_icon_view_cell_layout_reorder (GtkCellLayout   *layout,
4639                                    GtkCellRenderer *cell,
4640                                    gint             position)
4641 {
4642   GtkIconView *icon_view;
4643   GList *link, *l;
4644   GtkIconViewCellInfo *info;
4645   gint i;
4646
4647   icon_view = GTK_ICON_VIEW (layout);
4648
4649   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4650
4651   info = gtk_icon_view_get_cell_info (icon_view, cell);
4652
4653   g_return_if_fail (info != NULL);
4654   g_return_if_fail (position >= 0);
4655
4656   link = g_list_find (icon_view->priv->cell_list, info);
4657
4658   g_return_if_fail (link != NULL);
4659
4660   icon_view->priv->cell_list = g_list_delete_link (icon_view->priv->cell_list,
4661                                                    link);
4662   icon_view->priv->cell_list = g_list_insert (icon_view->priv->cell_list,
4663                                              info, position);
4664
4665   for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4666     {
4667       info = l->data;
4668
4669       info->position = i;
4670     }
4671
4672   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
4673 }
4674
4675 static GList *
4676 gtk_icon_view_cell_layout_get_cells (GtkCellLayout *layout)
4677 {
4678   GtkIconView *icon_view = (GtkIconView *)layout;
4679   GList *retval = NULL, *l;
4680
4681   for (l = icon_view->priv->cell_list; l; l = l->next)
4682     {
4683       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
4684
4685       retval = g_list_prepend (retval, info->cell);
4686     }
4687
4688   return g_list_reverse (retval);
4689 }
4690
4691 /* Public API */
4692
4693
4694 /**
4695  * gtk_icon_view_new:
4696  * 
4697  * Creates a new #GtkIconView widget
4698  * 
4699  * Return value: A newly created #GtkIconView widget
4700  *
4701  * Since: 2.6
4702  **/
4703 GtkWidget *
4704 gtk_icon_view_new (void)
4705 {
4706   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4707 }
4708
4709 /**
4710  * gtk_icon_view_new_with_model:
4711  * @model: The model.
4712  * 
4713  * Creates a new #GtkIconView widget with the model @model.
4714  * 
4715  * Return value: A newly created #GtkIconView widget.
4716  *
4717  * Since: 2.6 
4718  **/
4719 GtkWidget *
4720 gtk_icon_view_new_with_model (GtkTreeModel *model)
4721 {
4722   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4723 }
4724
4725 /**
4726  * gtk_icon_view_convert_widget_to_bin_window_coords:
4727  * @icon_view: a #GtkIconView 
4728  * @wx: X coordinate relative to the widget
4729  * @wy: Y coordinate relative to the widget
4730  * @bx: return location for bin_window X coordinate
4731  * @by: return location for bin_window Y coordinate
4732  * 
4733  * Converts widget coordinates to coordinates for the bin_window,
4734  * as expected by e.g. gtk_icon_view_get_path_at_pos(). 
4735  *
4736  * Since: 2.12
4737  */
4738 void
4739 gtk_icon_view_convert_widget_to_bin_window_coords (GtkIconView *icon_view,
4740                                                    gint         wx,
4741                                                    gint         wy, 
4742                                                    gint        *bx,
4743                                                    gint        *by)
4744 {
4745   gint x, y;
4746
4747   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4748
4749   if (icon_view->priv->bin_window) 
4750     gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4751   else
4752     x = y = 0;
4753  
4754   if (bx)
4755     *bx = wx - x;
4756   if (by)
4757     *by = wy - y;
4758 }
4759
4760 /**
4761  * gtk_icon_view_get_path_at_pos:
4762  * @icon_view: A #GtkIconView.
4763  * @x: The x position to be identified
4764  * @y: The y position to be identified
4765  * 
4766  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4767  * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4768  * the cell at the specified position. 
4769  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4770  * widget coordinates to bin_window coordinates.
4771  * 
4772  * Return value: The #GtkTreePath corresponding to the icon or %NULL
4773  * if no icon exists at that position.
4774  *
4775  * Since: 2.6 
4776  **/
4777 GtkTreePath *
4778 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4779                                gint         x,
4780                                gint         y)
4781 {
4782   GtkIconViewItem *item;
4783   GtkTreePath *path;
4784   
4785   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4786
4787   item = gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4788
4789   if (!item)
4790     return NULL;
4791
4792   path = gtk_tree_path_new_from_indices (item->index, -1);
4793
4794   return path;
4795 }
4796
4797 /**
4798  * gtk_icon_view_get_item_at_pos:
4799  * @icon_view: A #GtkIconView.
4800  * @x: The x position to be identified
4801  * @y: The y position to be identified
4802  * @path: Return location for the path, or %NULL
4803  * @cell: Return location for the renderer responsible for the cell
4804  *   at (@x, @y), or %NULL
4805  * 
4806  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4807  * In contrast to gtk_icon_view_get_path_at_pos(), this function also 
4808  * obtains the cell at the specified position. The returned path should
4809  * be freed with gtk_tree_path_free().
4810  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4811  * widget coordinates to bin_window coordinates.
4812  * 
4813  * Return value: %TRUE if an item exists at the specified position
4814  *
4815  * Since: 2.8
4816  **/
4817 gboolean 
4818 gtk_icon_view_get_item_at_pos (GtkIconView      *icon_view,
4819                                gint              x,
4820                                gint              y,
4821                                GtkTreePath     **path,
4822                                GtkCellRenderer **cell)
4823 {
4824   GtkIconViewItem *item;
4825   GtkIconViewCellInfo *info;
4826   
4827   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4828
4829   item = gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &info);
4830
4831   if (path != NULL)
4832     {
4833       if (item != NULL)
4834         *path = gtk_tree_path_new_from_indices (item->index, -1);
4835       else
4836         *path = NULL;
4837     }
4838
4839   if (cell != NULL)
4840     {
4841       if (info != NULL)
4842         *cell = info->cell;
4843       else 
4844         *cell = NULL;
4845     }
4846
4847   return (item != NULL);
4848 }
4849
4850 /**
4851  * gtk_icon_view_set_tooltip_item:
4852  * @icon_view: a #GtkIconView
4853  * @tooltip: a #GtkTooltip
4854  * @path: a #GtkTreePath
4855  * 
4856  * Sets the tip area of @tooltip to be the area covered by the item at @path.
4857  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4858  * See also gtk_tooltip_set_tip_area().
4859  * 
4860  * Since: 2.12
4861  */
4862 void 
4863 gtk_icon_view_set_tooltip_item (GtkIconView     *icon_view,
4864                                 GtkTooltip      *tooltip,
4865                                 GtkTreePath     *path)
4866 {
4867   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4868   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4869
4870   gtk_icon_view_set_tooltip_cell (icon_view, tooltip, path, NULL);
4871 }
4872
4873 /**
4874  * gtk_icon_view_set_tooltip_cell:
4875  * @icon_view: a #GtkIconView
4876  * @tooltip: a #GtkTooltip
4877  * @path: a #GtkTreePath
4878  * @cell: a #GtkCellRenderer or %NULL
4879  *
4880  * Sets the tip area of @tooltip to the area which @cell occupies in
4881  * the item pointed to by @path. See also gtk_tooltip_set_tip_area().
4882  *
4883  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4884  *
4885  * Since: 2.12
4886  */
4887 void
4888 gtk_icon_view_set_tooltip_cell (GtkIconView     *icon_view,
4889                                 GtkTooltip      *tooltip,
4890                                 GtkTreePath     *path,
4891                                 GtkCellRenderer *cell)
4892 {
4893   GdkRectangle rect;
4894   GtkIconViewItem *item = NULL;
4895   GtkIconViewCellInfo *info = NULL;
4896   gint x, y;
4897  
4898   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4899   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4900   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
4901
4902   if (gtk_tree_path_get_depth (path) > 0)
4903     item = g_list_nth_data (icon_view->priv->items,
4904                             gtk_tree_path_get_indices(path)[0]);
4905  
4906   if (!item)
4907     return;
4908
4909   if (cell)
4910     {
4911       info = gtk_icon_view_get_cell_info (icon_view, cell);
4912       gtk_icon_view_get_cell_area (icon_view, item, info, &rect);
4913     }
4914   else
4915     {
4916       rect.x = item->x;
4917       rect.y = item->y;
4918       rect.width = item->width;
4919       rect.height = item->height;
4920     }
4921   
4922   if (icon_view->priv->bin_window)
4923     {
4924       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4925       rect.x += x;
4926       rect.y += y; 
4927     }
4928
4929   gtk_tooltip_set_tip_area (tooltip, &rect); 
4930 }
4931
4932
4933 /**
4934  * gtk_icon_view_get_tooltip_context:
4935  * @icon_view: an #GtkIconView
4936  * @x: the x coordinate (relative to widget coordinates)
4937  * @y: the y coordinate (relative to widget coordinates)
4938  * @keyboard_tip: whether this is a keyboard tooltip or not
4939  * @model: a pointer to receive a #GtkTreeModel or %NULL
4940  * @path: a pointer to receive a #GtkTreePath or %NULL
4941  * @iter: a pointer to receive a #GtkTreeIter or %NULL
4942  *
4943  * This function is supposed to be used in a #GtkWidget::query-tooltip
4944  * signal handler for #GtkIconView.  The @x, @y and @keyboard_tip values
4945  * which are received in the signal handler, should be passed to this
4946  * function without modification.
4947  *
4948  * The return value indicates whether there is an icon view item at the given
4949  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips. For keyboard
4950  * tooltips the item returned will be the cursor item. When %TRUE, then any of
4951  * @model, @path and @iter which have been provided will be set to point to
4952  * that row and the corresponding model. @x and @y will always be converted
4953  * to be relative to @icon_view's bin_window if @keyboard_tooltip is %FALSE.
4954  *
4955  * Return value: whether or not the given tooltip context points to a item
4956  *
4957  * Since: 2.12
4958  */
4959 gboolean
4960 gtk_icon_view_get_tooltip_context (GtkIconView   *icon_view,
4961                                    gint          *x,
4962                                    gint          *y,
4963                                    gboolean       keyboard_tip,
4964                                    GtkTreeModel **model,
4965                                    GtkTreePath  **path,
4966                                    GtkTreeIter   *iter)
4967 {
4968   GtkTreePath *tmppath = NULL;
4969
4970   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4971   g_return_val_if_fail (x != NULL, FALSE);
4972   g_return_val_if_fail (y != NULL, FALSE);
4973
4974   if (keyboard_tip)
4975     {
4976       gtk_icon_view_get_cursor (icon_view, &tmppath, NULL);
4977
4978       if (!tmppath)
4979         return FALSE;
4980     }
4981   else
4982     {
4983       gtk_icon_view_convert_widget_to_bin_window_coords (icon_view, *x, *y,
4984                                                          x, y);
4985
4986       if (!gtk_icon_view_get_item_at_pos (icon_view, *x, *y, &tmppath, NULL))
4987         return FALSE;
4988     }
4989
4990   if (model)
4991     *model = gtk_icon_view_get_model (icon_view);
4992
4993   if (iter)
4994     gtk_tree_model_get_iter (gtk_icon_view_get_model (icon_view),
4995                              iter, tmppath);
4996
4997   if (path)
4998     *path = tmppath;
4999   else
5000     gtk_tree_path_free (tmppath);
5001
5002   return TRUE;
5003 }
5004
5005 static gboolean
5006 gtk_icon_view_set_tooltip_query_cb (GtkWidget  *widget,
5007                                     gint        x,
5008                                     gint        y,
5009                                     gboolean    keyboard_tip,
5010                                     GtkTooltip *tooltip,
5011                                     gpointer    data)
5012 {
5013   gchar *str;
5014   GtkTreeIter iter;
5015   GtkTreePath *path;
5016   GtkTreeModel *model;
5017   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
5018
5019   if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget),
5020                                           &x, &y,
5021                                           keyboard_tip,
5022                                           &model, &path, &iter))
5023     return FALSE;
5024
5025   gtk_tree_model_get (model, &iter, icon_view->priv->tooltip_column, &str, -1);
5026
5027   if (!str)
5028     {
5029       gtk_tree_path_free (path);
5030       return FALSE;
5031     }
5032
5033   gtk_tooltip_set_markup (tooltip, str);
5034   gtk_icon_view_set_tooltip_item (icon_view, tooltip, path);
5035
5036   gtk_tree_path_free (path);
5037   g_free (str);
5038
5039   return TRUE;
5040 }
5041
5042
5043 /**
5044  * gtk_icon_view_set_tooltip_column:
5045  * @icon_view: a #GtkIconView
5046  * @column: an integer, which is a valid column number for @icon_view's model
5047  *
5048  * If you only plan to have simple (text-only) tooltips on full items, you
5049  * can use this function to have #GtkIconView handle these automatically
5050  * for you. @column should be set to the column in @icon_view's model
5051  * containing the tooltip texts, or -1 to disable this feature.
5052  *
5053  * When enabled, #GtkWidget::has-tooltip will be set to %TRUE and
5054  * @icon_view will connect a #GtkWidget::query-tooltip signal handler.
5055  *
5056  * Since: 2.12
5057  */
5058 void
5059 gtk_icon_view_set_tooltip_column (GtkIconView *icon_view,
5060                                   gint         column)
5061 {
5062   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5063
5064   if (column == icon_view->priv->tooltip_column)
5065     return;
5066
5067   if (column == -1)
5068     {
5069       g_signal_handlers_disconnect_by_func (icon_view,
5070                                             gtk_icon_view_set_tooltip_query_cb,
5071                                             NULL);
5072       gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), FALSE);
5073     }
5074   else
5075     {
5076       if (icon_view->priv->tooltip_column == -1)
5077         {
5078           g_signal_connect (icon_view, "query-tooltip",
5079                             G_CALLBACK (gtk_icon_view_set_tooltip_query_cb), NULL);
5080           gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), TRUE);
5081         }
5082     }
5083
5084   icon_view->priv->tooltip_column = column;
5085   g_object_notify (G_OBJECT (icon_view), "tooltip-column");
5086 }
5087
5088 /** 
5089  * gtk_icon_view_get_tooltip_column:
5090  * @icon_view: a #GtkIconView
5091  *
5092  * Returns the column of @icon_view's model which is being used for
5093  * displaying tooltips on @icon_view's rows.
5094  *
5095  * Return value: the index of the tooltip column that is currently being
5096  * used, or -1 if this is disabled.
5097  *
5098  * Since: 2.12
5099  */
5100 gint
5101 gtk_icon_view_get_tooltip_column (GtkIconView *icon_view)
5102 {
5103   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 0);
5104
5105   return icon_view->priv->tooltip_column;
5106 }
5107
5108 /**
5109  * gtk_icon_view_get_visible_range:
5110  * @icon_view: A #GtkIconView
5111  * @start_path: Return location for start of region, or %NULL
5112  * @end_path: Return location for end of region, or %NULL
5113  * 
5114  * Sets @start_path and @end_path to be the first and last visible path. 
5115  * Note that there may be invisible paths in between.
5116  * 
5117  * Both paths should be freed with gtk_tree_path_free() after use.
5118  * 
5119  * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
5120  *
5121  * Since: 2.8
5122  **/
5123 gboolean
5124 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
5125                                  GtkTreePath **start_path,
5126                                  GtkTreePath **end_path)
5127 {
5128   gint start_index = -1;
5129   gint end_index = -1;
5130   GList *icons;
5131
5132   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5133
5134   if (icon_view->priv->hadjustment == NULL ||
5135       icon_view->priv->vadjustment == NULL)
5136     return FALSE;
5137
5138   if (start_path == NULL && end_path == NULL)
5139     return FALSE;
5140   
5141   for (icons = icon_view->priv->items; icons; icons = icons->next) 
5142     {
5143       GtkIconViewItem *item = icons->data;
5144
5145       if ((item->x + item->width >= (int)icon_view->priv->hadjustment->value) &&
5146           (item->y + item->height >= (int)icon_view->priv->vadjustment->value) &&
5147           (item->x <= (int) (icon_view->priv->hadjustment->value + icon_view->priv->hadjustment->page_size)) &&
5148           (item->y <= (int) (icon_view->priv->vadjustment->value + icon_view->priv->vadjustment->page_size)))
5149         {
5150           if (start_index == -1)
5151             start_index = item->index;
5152           end_index = item->index;
5153         }
5154     }
5155
5156   if (start_path && start_index != -1)
5157     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
5158   if (end_path && end_index != -1)
5159     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
5160   
5161   return start_index != -1;
5162 }
5163
5164 /**
5165  * gtk_icon_view_selected_foreach:
5166  * @icon_view: A #GtkIconView.
5167  * @func: The funcion to call for each selected icon.
5168  * @data: User data to pass to the function.
5169  * 
5170  * Calls a function for each selected icon. Note that the model or
5171  * selection cannot be modified from within this function.
5172  *
5173  * Since: 2.6 
5174  **/
5175 void
5176 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
5177                                 GtkIconViewForeachFunc func,
5178                                 gpointer               data)
5179 {
5180   GList *list;
5181   
5182   for (list = icon_view->priv->items; list; list = list->next)
5183     {
5184       GtkIconViewItem *item = list->data;
5185       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5186
5187       if (item->selected)
5188         (* func) (icon_view, path, data);
5189
5190       gtk_tree_path_free (path);
5191     }
5192 }
5193
5194 /**
5195  * gtk_icon_view_set_selection_mode:
5196  * @icon_view: A #GtkIconView.
5197  * @mode: The selection mode
5198  * 
5199  * Sets the selection mode of the @icon_view.
5200  *
5201  * Since: 2.6 
5202  **/
5203 void
5204 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
5205                                   GtkSelectionMode  mode)
5206 {
5207   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5208
5209   if (mode == icon_view->priv->selection_mode)
5210     return;
5211   
5212   if (mode == GTK_SELECTION_NONE ||
5213       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
5214     gtk_icon_view_unselect_all (icon_view);
5215   
5216   icon_view->priv->selection_mode = mode;
5217
5218   g_object_notify (G_OBJECT (icon_view), "selection-mode");
5219 }
5220
5221 /**
5222  * gtk_icon_view_get_selection_mode:
5223  * @icon_view: A #GtkIconView.
5224  * 
5225  * Gets the selection mode of the @icon_view.
5226  *
5227  * Return value: the current selection mode
5228  *
5229  * Since: 2.6 
5230  **/
5231 GtkSelectionMode
5232 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
5233 {
5234   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
5235
5236   return icon_view->priv->selection_mode;
5237 }
5238
5239 /**
5240  * gtk_icon_view_set_model:
5241  * @icon_view: A #GtkIconView.
5242  * @model: The model.
5243  *
5244  * Sets the model for a #GtkIconView.  
5245  * If the @icon_view already has a model set, it will remove 
5246  * it before setting the new model.  If @model is %NULL, then
5247  * it will unset the old model.
5248  *
5249  * Since: 2.6 
5250  **/
5251 void
5252 gtk_icon_view_set_model (GtkIconView *icon_view,
5253                          GtkTreeModel *model)
5254 {
5255   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5256   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
5257   
5258   if (icon_view->priv->model == model)
5259     return;
5260
5261   if (icon_view->priv->scroll_to_path)
5262     {
5263       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
5264       icon_view->priv->scroll_to_path = NULL;
5265     }
5266
5267   gtk_icon_view_stop_editing (icon_view, TRUE);
5268
5269   if (model)
5270     {
5271       GType column_type;
5272       
5273       g_return_if_fail (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY);
5274
5275       if (icon_view->priv->pixbuf_column != -1)
5276         {
5277           column_type = gtk_tree_model_get_column_type (model,
5278                                                         icon_view->priv->pixbuf_column);          
5279
5280           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5281         }
5282
5283       if (icon_view->priv->text_column != -1)
5284         {
5285           column_type = gtk_tree_model_get_column_type (model,
5286                                                         icon_view->priv->text_column);    
5287
5288           g_return_if_fail (column_type == G_TYPE_STRING);
5289         }
5290
5291       if (icon_view->priv->markup_column != -1)
5292         {
5293           column_type = gtk_tree_model_get_column_type (model,
5294                                                         icon_view->priv->markup_column);          
5295
5296           g_return_if_fail (column_type == G_TYPE_STRING);
5297         }
5298       
5299     }
5300   
5301   if (icon_view->priv->model)
5302     {
5303       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5304                                             gtk_icon_view_row_changed,
5305                                             icon_view);
5306       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5307                                             gtk_icon_view_row_inserted,
5308                                             icon_view);
5309       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5310                                             gtk_icon_view_row_deleted,
5311                                             icon_view);
5312       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
5313                                             gtk_icon_view_rows_reordered,
5314                                             icon_view);
5315
5316       g_object_unref (icon_view->priv->model);
5317       
5318       g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
5319       g_list_free (icon_view->priv->items);
5320       icon_view->priv->items = NULL;
5321       icon_view->priv->anchor_item = NULL;
5322       icon_view->priv->cursor_item = NULL;
5323       icon_view->priv->last_single_clicked = NULL;
5324       icon_view->priv->width = 0;
5325       icon_view->priv->height = 0;
5326     }
5327
5328   icon_view->priv->model = model;
5329
5330   if (icon_view->priv->model)
5331     {
5332       g_object_ref (icon_view->priv->model);
5333       g_signal_connect (icon_view->priv->model,
5334                         "row-changed",
5335                         G_CALLBACK (gtk_icon_view_row_changed),
5336                         icon_view);
5337       g_signal_connect (icon_view->priv->model,
5338                         "row-inserted",
5339                         G_CALLBACK (gtk_icon_view_row_inserted),
5340                         icon_view);
5341       g_signal_connect (icon_view->priv->model,
5342                         "row-deleted",
5343                         G_CALLBACK (gtk_icon_view_row_deleted),
5344                         icon_view);
5345       g_signal_connect (icon_view->priv->model,
5346                         "rows-reordered",
5347                         G_CALLBACK (gtk_icon_view_rows_reordered),
5348                         icon_view);
5349
5350       gtk_icon_view_build_items (icon_view);
5351
5352       gtk_icon_view_queue_layout (icon_view);
5353     }
5354
5355   g_object_notify (G_OBJECT (icon_view), "model");  
5356
5357   if (GTK_WIDGET_REALIZED (icon_view))
5358     gtk_widget_queue_resize (GTK_WIDGET (icon_view));
5359 }
5360
5361 /**
5362  * gtk_icon_view_get_model:
5363  * @icon_view: a #GtkIconView
5364  *
5365  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
5366  * model is unset.
5367  *
5368  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
5369  *
5370  * Since: 2.6 
5371  **/
5372 GtkTreeModel *
5373 gtk_icon_view_get_model (GtkIconView *icon_view)
5374 {
5375   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5376
5377   return icon_view->priv->model;
5378 }
5379
5380 static void
5381 update_text_cell (GtkIconView *icon_view)
5382 {
5383   GtkIconViewCellInfo *info;
5384   GList *l;
5385   gint i;
5386           
5387   if (icon_view->priv->text_column == -1 &&
5388       icon_view->priv->markup_column == -1)
5389     {
5390       if (icon_view->priv->text_cell != -1)
5391         {
5392           if (icon_view->priv->pixbuf_cell > icon_view->priv->text_cell)
5393             icon_view->priv->pixbuf_cell--;
5394
5395           info = g_list_nth_data (icon_view->priv->cell_list, 
5396                                   icon_view->priv->text_cell);
5397           
5398           icon_view->priv->cell_list = g_list_remove (icon_view->priv->cell_list, info);
5399           
5400           free_cell_info (info);
5401           
5402           icon_view->priv->n_cells--;
5403           icon_view->priv->text_cell = -1;
5404         }
5405     }
5406   else 
5407     {
5408       if (icon_view->priv->text_cell == -1)
5409         {
5410           GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
5411           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
5412           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
5413             {
5414               info = l->data;
5415               if (info->cell == cell)
5416                 {
5417                   icon_view->priv->text_cell = i;
5418                   break;
5419                 }
5420             }
5421         }
5422       
5423       info = g_list_nth_data (icon_view->priv->cell_list,
5424                               icon_view->priv->text_cell);
5425
5426       if (icon_view->priv->markup_column != -1)
5427         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5428                                         info->cell, 
5429                                         "markup", icon_view->priv->markup_column, 
5430                                         NULL);
5431       else
5432         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5433                                         info->cell, 
5434                                         "text", icon_view->priv->text_column, 
5435                                         NULL);
5436
5437       if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
5438         g_object_set (info->cell,
5439                       "alignment", PANGO_ALIGN_CENTER,
5440                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
5441                       "xalign", 0.0,
5442                       "yalign", 0.0,
5443                       NULL);
5444       else
5445         g_object_set (info->cell,
5446                       "alignment", PANGO_ALIGN_LEFT,
5447                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
5448                       "xalign", 0.0,
5449                       "yalign", 0.0,
5450                       NULL);
5451     }
5452 }
5453
5454 static void
5455 update_pixbuf_cell (GtkIconView *icon_view)
5456 {
5457   GtkIconViewCellInfo *info;
5458   GList *l;
5459   gint i;
5460
5461   if (icon_view->priv->pixbuf_column == -1)
5462     {
5463       if (icon_view->priv->pixbuf_cell != -1)
5464         {
5465           if (icon_view->priv->text_cell > icon_view->priv->pixbuf_cell)
5466             icon_view->priv->text_cell--;
5467
5468           info = g_list_nth_data (icon_view->priv->cell_list, 
5469                                   icon_view->priv->pixbuf_cell);
5470           
5471           icon_view->priv->cell_list = g_list_remove (icon_view->priv->cell_list, info);
5472           
5473           free_cell_info (info);
5474           
5475           icon_view->priv->n_cells--;
5476           icon_view->priv->pixbuf_cell = -1;
5477         }
5478     }
5479   else 
5480     {
5481       if (icon_view->priv->pixbuf_cell == -1)
5482         {
5483           GtkCellRenderer *cell = gtk_cell_renderer_pixbuf_new ();
5484           
5485           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
5486           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
5487             {
5488               info = l->data;
5489               if (info->cell == cell)
5490                 {
5491                   icon_view->priv->pixbuf_cell = i;
5492                   break;
5493                 }
5494             }
5495         }
5496       
5497         info = g_list_nth_data (icon_view->priv->cell_list, 
5498                                 icon_view->priv->pixbuf_cell);
5499         
5500         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
5501                                         info->cell, 
5502                                         "pixbuf", icon_view->priv->pixbuf_column, 
5503                                         NULL);
5504
5505         if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
5506           g_object_set (info->cell,
5507                         "xalign", 0.5,
5508                         "yalign", 1.0,
5509                         NULL);
5510         else
5511           g_object_set (info->cell,
5512                         "xalign", 0.0,
5513                         "yalign", 0.0,
5514                         NULL);
5515     }
5516 }
5517
5518 /**
5519  * gtk_icon_view_set_text_column:
5520  * @icon_view: A #GtkIconView.
5521  * @column: A column in the currently used model, or -1 to display no text
5522  * 
5523  * Sets the column with text for @icon_view to be @column. The text
5524  * column must be of type #G_TYPE_STRING.
5525  *
5526  * Since: 2.6 
5527  **/
5528 void
5529 gtk_icon_view_set_text_column (GtkIconView *icon_view,
5530                                gint          column)
5531 {
5532   if (column == icon_view->priv->text_column)
5533     return;
5534   
5535   if (column == -1)
5536     icon_view->priv->text_column = -1;
5537   else
5538     {
5539       if (icon_view->priv->model != NULL)
5540         {
5541           GType column_type;
5542           
5543           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5544
5545           g_return_if_fail (column_type == G_TYPE_STRING);
5546         }
5547       
5548       icon_view->priv->text_column = column;
5549     }
5550
5551   gtk_icon_view_stop_editing (icon_view, TRUE);
5552
5553   update_text_cell (icon_view);
5554
5555   gtk_icon_view_invalidate_sizes (icon_view);
5556   gtk_icon_view_queue_layout (icon_view);
5557   
5558   g_object_notify (G_OBJECT (icon_view), "text-column");
5559 }
5560
5561 /**
5562  * gtk_icon_view_get_text_column:
5563  * @icon_view: A #GtkIconView.
5564  *
5565  * Returns the column with text for @icon_view.
5566  *
5567  * Returns: the text column, or -1 if it's unset.
5568  *
5569  * Since: 2.6
5570  */
5571 gint
5572 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
5573 {
5574   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5575
5576   return icon_view->priv->text_column;
5577 }
5578
5579 /**
5580  * gtk_icon_view_set_markup_column:
5581  * @icon_view: A #GtkIconView.
5582  * @column: A column in the currently used model, or -1 to display no text
5583  * 
5584  * Sets the column with markup information for @icon_view to be
5585  * @column. The markup column must be of type #G_TYPE_STRING.
5586  * If the markup column is set to something, it overrides
5587  * the text column set by gtk_icon_view_set_text_column().
5588  *
5589  * Since: 2.6
5590  **/
5591 void
5592 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5593                                  gint         column)
5594 {
5595   if (column == icon_view->priv->markup_column)
5596     return;
5597   
5598   if (column == -1)
5599     icon_view->priv->markup_column = -1;
5600   else
5601     {
5602       if (icon_view->priv->model != NULL)
5603         {
5604           GType column_type;
5605           
5606           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5607
5608           g_return_if_fail (column_type == G_TYPE_STRING);
5609         }
5610       
5611       icon_view->priv->markup_column = column;
5612     }
5613
5614   gtk_icon_view_stop_editing (icon_view, TRUE);
5615
5616   update_text_cell (icon_view);
5617
5618   gtk_icon_view_invalidate_sizes (icon_view);
5619   gtk_icon_view_queue_layout (icon_view);
5620   
5621   g_object_notify (G_OBJECT (icon_view), "markup-column");
5622 }
5623
5624 /**
5625  * gtk_icon_view_get_markup_column:
5626  * @icon_view: A #GtkIconView.
5627  *
5628  * Returns the column with markup text for @icon_view.
5629  *
5630  * Returns: the markup column, or -1 if it's unset.
5631  *
5632  * Since: 2.6
5633  */
5634 gint
5635 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
5636 {
5637   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5638
5639   return icon_view->priv->markup_column;
5640 }
5641
5642 /**
5643  * gtk_icon_view_set_pixbuf_column:
5644  * @icon_view: A #GtkIconView.
5645  * @column: A column in the currently used model, or -1 to disable
5646  * 
5647  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5648  * column must be of type #GDK_TYPE_PIXBUF
5649  *
5650  * Since: 2.6 
5651  **/
5652 void
5653 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5654                                  gint         column)
5655 {
5656   if (column == icon_view->priv->pixbuf_column)
5657     return;
5658   
5659   if (column == -1)
5660     icon_view->priv->pixbuf_column = -1;
5661   else
5662     {
5663       if (icon_view->priv->model != NULL)
5664         {
5665           GType column_type;
5666           
5667           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5668
5669           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5670         }
5671       
5672       icon_view->priv->pixbuf_column = column;
5673     }
5674
5675   gtk_icon_view_stop_editing (icon_view, TRUE);
5676
5677   update_pixbuf_cell (icon_view);
5678
5679   gtk_icon_view_invalidate_sizes (icon_view);
5680   gtk_icon_view_queue_layout (icon_view);
5681   
5682   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5683   
5684 }
5685
5686 /**
5687  * gtk_icon_view_get_pixbuf_column:
5688  * @icon_view: A #GtkIconView.
5689  *
5690  * Returns the column with pixbufs for @icon_view.
5691  *
5692  * Returns: the pixbuf column, or -1 if it's unset.
5693  *
5694  * Since: 2.6
5695  */
5696 gint
5697 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
5698 {
5699   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5700
5701   return icon_view->priv->pixbuf_column;
5702 }
5703
5704 /**
5705  * gtk_icon_view_select_path:
5706  * @icon_view: A #GtkIconView.
5707  * @path: The #GtkTreePath to be selected.
5708  * 
5709  * Selects the row at @path.
5710  *
5711  * Since: 2.6
5712  **/
5713 void
5714 gtk_icon_view_select_path (GtkIconView *icon_view,
5715                            GtkTreePath *path)
5716 {
5717   GtkIconViewItem *item = NULL;
5718
5719   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5720   g_return_if_fail (icon_view->priv->model != NULL);
5721   g_return_if_fail (path != NULL);
5722
5723   if (gtk_tree_path_get_depth (path) > 0)
5724     item = g_list_nth_data (icon_view->priv->items,
5725                             gtk_tree_path_get_indices(path)[0]);
5726
5727   if (item)
5728     gtk_icon_view_select_item (icon_view, item);
5729 }
5730
5731 /**
5732  * gtk_icon_view_unselect_path:
5733  * @icon_view: A #GtkIconView.
5734  * @path: The #GtkTreePath to be unselected.
5735  * 
5736  * Unselects the row at @path.
5737  *
5738  * Since: 2.6
5739  **/
5740 void
5741 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5742                              GtkTreePath *path)
5743 {
5744   GtkIconViewItem *item;
5745   
5746   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5747   g_return_if_fail (icon_view->priv->model != NULL);
5748   g_return_if_fail (path != NULL);
5749
5750   item = g_list_nth_data (icon_view->priv->items,
5751                           gtk_tree_path_get_indices(path)[0]);
5752
5753   if (!item)
5754     return;
5755   
5756   gtk_icon_view_unselect_item (icon_view, item);
5757 }
5758
5759 /**
5760  * gtk_icon_view_get_selected_items:
5761  * @icon_view: A #GtkIconView.
5762  *
5763  * Creates a list of paths of all selected items. Additionally, if you are
5764  * planning on modifying the model after calling this function, you may
5765  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5766  * To do this, you can use gtk_tree_row_reference_new().
5767  *
5768  * To free the return value, use:
5769  * |[
5770  * g_list_foreach (list, gtk_tree_path_free, NULL);
5771  * g_list_free (list);
5772  * ]|
5773  *
5774  * Return value: A #GList containing a #GtkTreePath for each selected row.
5775  *
5776  * Since: 2.6
5777  **/
5778 GList *
5779 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5780 {
5781   GList *list;
5782   GList *selected = NULL;
5783   
5784   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5785   
5786   for (list = icon_view->priv->items; list != NULL; list = list->next)
5787     {
5788       GtkIconViewItem *item = list->data;
5789
5790       if (item->selected)
5791         {
5792           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5793
5794           selected = g_list_prepend (selected, path);
5795         }
5796     }
5797
5798   return selected;
5799 }
5800
5801 /**
5802  * gtk_icon_view_select_all:
5803  * @icon_view: A #GtkIconView.
5804  * 
5805  * Selects all the icons. @icon_view must has its selection mode set
5806  * to #GTK_SELECTION_MULTIPLE.
5807  *
5808  * Since: 2.6
5809  **/
5810 void
5811 gtk_icon_view_select_all (GtkIconView *icon_view)
5812 {
5813   GList *items;
5814   gboolean dirty = FALSE;
5815   
5816   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5817
5818   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5819     return;
5820
5821   for (items = icon_view->priv->items; items; items = items->next)
5822     {
5823       GtkIconViewItem *item = items->data;
5824       
5825       if (!item->selected)
5826         {
5827           dirty = TRUE;
5828           item->selected = TRUE;
5829           gtk_icon_view_queue_draw_item (icon_view, item);
5830         }
5831     }
5832
5833   if (dirty)
5834     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5835 }
5836
5837 /**
5838  * gtk_icon_view_unselect_all:
5839  * @icon_view: A #GtkIconView.
5840  * 
5841  * Unselects all the icons.
5842  *
5843  * Since: 2.6
5844  **/
5845 void
5846 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5847 {
5848   gboolean dirty = FALSE;
5849   
5850   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5851
5852   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5853     return;
5854
5855   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5856
5857   if (dirty)
5858     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5859 }
5860
5861 /**
5862  * gtk_icon_view_path_is_selected:
5863  * @icon_view: A #GtkIconView.
5864  * @path: A #GtkTreePath to check selection on.
5865  * 
5866  * Returns %TRUE if the icon pointed to by @path is currently
5867  * selected. If @path does not point to a valid location, %FALSE is returned.
5868  * 
5869  * Return value: %TRUE if @path is selected.
5870  *
5871  * Since: 2.6
5872  **/
5873 gboolean
5874 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5875                                 GtkTreePath *path)
5876 {
5877   GtkIconViewItem *item;
5878   
5879   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5880   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5881   g_return_val_if_fail (path != NULL, FALSE);
5882   
5883   item = g_list_nth_data (icon_view->priv->items,
5884                           gtk_tree_path_get_indices(path)[0]);
5885
5886   if (!item)
5887     return FALSE;
5888   
5889   return item->selected;
5890 }
5891
5892 /**
5893  * gtk_icon_view_item_activated:
5894  * @icon_view: A #GtkIconView
5895  * @path: The #GtkTreePath to be activated
5896  * 
5897  * Activates the item determined by @path.
5898  *
5899  * Since: 2.6
5900  **/
5901 void
5902 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5903                               GtkTreePath      *path)
5904 {
5905   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5906   g_return_if_fail (path != NULL);
5907   
5908   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5909 }
5910
5911 /**
5912  * gtk_icon_view_set_orientation:
5913  * @icon_view: a #GtkIconView
5914  * @orientation: the relative position of texts and icons 
5915  * 
5916  * Sets the ::orientation property which determines whether the labels 
5917  * are drawn beside the icons instead of below.
5918  *
5919  * Since: 2.6
5920  **/
5921 void 
5922 gtk_icon_view_set_orientation (GtkIconView    *icon_view,
5923                                GtkOrientation  orientation)
5924 {
5925   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5926
5927   if (icon_view->priv->orientation != orientation)
5928     {
5929       icon_view->priv->orientation = orientation;
5930
5931       gtk_icon_view_stop_editing (icon_view, TRUE);
5932       gtk_icon_view_invalidate_sizes (icon_view);
5933       gtk_icon_view_queue_layout (icon_view);
5934
5935       update_text_cell (icon_view);
5936       update_pixbuf_cell (icon_view);
5937       
5938       g_object_notify (G_OBJECT (icon_view), "orientation");
5939     }
5940 }
5941
5942 /**
5943  * gtk_icon_view_get_orientation:
5944  * @icon_view: a #GtkIconView
5945  * 
5946  * Returns the value of the ::orientation property which determines 
5947  * whether the labels are drawn beside the icons instead of below. 
5948  * 
5949  * Return value: the relative position of texts and icons 
5950  *
5951  * Since: 2.6
5952  **/
5953 GtkOrientation
5954 gtk_icon_view_get_orientation (GtkIconView *icon_view)
5955 {
5956   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5957                         GTK_ORIENTATION_VERTICAL);
5958
5959   return icon_view->priv->orientation;
5960 }
5961
5962 /**
5963  * gtk_icon_view_set_columns:
5964  * @icon_view: a #GtkIconView
5965  * @columns: the number of columns
5966  * 
5967  * Sets the ::columns property which determines in how
5968  * many columns the icons are arranged. If @columns is
5969  * -1, the number of columns will be chosen automatically 
5970  * to fill the available area. 
5971  *
5972  * Since: 2.6
5973  */
5974 void 
5975 gtk_icon_view_set_columns (GtkIconView *icon_view,
5976                            gint         columns)
5977 {
5978   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5979   
5980   if (icon_view->priv->columns != columns)
5981     {
5982       icon_view->priv->columns = columns;
5983
5984       gtk_icon_view_stop_editing (icon_view, TRUE);
5985       gtk_icon_view_queue_layout (icon_view);
5986       
5987       g_object_notify (G_OBJECT (icon_view), "columns");
5988     }  
5989 }
5990
5991 /**
5992  * gtk_icon_view_get_columns:
5993  * @icon_view: a #GtkIconView
5994  * 
5995  * Returns the value of the ::columns property.
5996  * 
5997  * Return value: the number of columns, or -1
5998  *
5999  * Since: 2.6
6000  */
6001 gint
6002 gtk_icon_view_get_columns (GtkIconView *icon_view)
6003 {
6004   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6005
6006   return icon_view->priv->columns;
6007 }
6008
6009 /**
6010  * gtk_icon_view_set_item_width:
6011  * @icon_view: a #GtkIconView
6012  * @item_width: the width for each item
6013  * 
6014  * Sets the ::item-width property which specifies the width 
6015  * to use for each item. If it is set to -1, the icon view will 
6016  * automatically determine a suitable item size.
6017  *
6018  * Since: 2.6
6019  */
6020 void 
6021 gtk_icon_view_set_item_width (GtkIconView *icon_view,
6022                               gint         item_width)
6023 {
6024   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6025   
6026   if (icon_view->priv->item_width != item_width)
6027     {
6028       icon_view->priv->item_width = item_width;
6029       
6030       gtk_icon_view_stop_editing (icon_view, TRUE);
6031       gtk_icon_view_invalidate_sizes (icon_view);
6032       gtk_icon_view_queue_layout (icon_view);
6033       
6034       update_text_cell (icon_view);
6035
6036       g_object_notify (G_OBJECT (icon_view), "item-width");
6037     }  
6038 }
6039
6040 /**
6041  * gtk_icon_view_get_item_width:
6042  * @icon_view: a #GtkIconView
6043  * 
6044  * Returns the value of the ::item-width property.
6045  * 
6046  * Return value: the width of a single item, or -1
6047  *
6048  * Since: 2.6
6049  */
6050 gint
6051 gtk_icon_view_get_item_width (GtkIconView *icon_view)
6052 {
6053   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6054
6055   return icon_view->priv->item_width;
6056 }
6057
6058
6059 /**
6060  * gtk_icon_view_set_spacing:
6061  * @icon_view: a #GtkIconView
6062  * @spacing: the spacing
6063  * 
6064  * Sets the ::spacing property which specifies the space 
6065  * which is inserted between the cells (i.e. the icon and 
6066  * the text) of an item.
6067  *
6068  * Since: 2.6
6069  */
6070 void 
6071 gtk_icon_view_set_spacing (GtkIconView *icon_view,
6072                            gint         spacing)
6073 {
6074   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6075   
6076   if (icon_view->priv->spacing != spacing)
6077     {
6078       icon_view->priv->spacing = spacing;
6079
6080       gtk_icon_view_stop_editing (icon_view, TRUE);
6081       gtk_icon_view_invalidate_sizes (icon_view);
6082       gtk_icon_view_queue_layout (icon_view);
6083       
6084       g_object_notify (G_OBJECT (icon_view), "spacing");
6085     }  
6086 }
6087
6088 /**
6089  * gtk_icon_view_get_spacing:
6090  * @icon_view: a #GtkIconView
6091  * 
6092  * Returns the value of the ::spacing property.
6093  * 
6094  * Return value: the space between cells 
6095  *
6096  * Since: 2.6
6097  */
6098 gint
6099 gtk_icon_view_get_spacing (GtkIconView *icon_view)
6100 {
6101   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6102
6103   return icon_view->priv->spacing;
6104 }
6105
6106 /**
6107  * gtk_icon_view_set_row_spacing:
6108  * @icon_view: a #GtkIconView
6109  * @row_spacing: the row spacing
6110  * 
6111  * Sets the ::row-spacing property which specifies the space 
6112  * which is inserted between the rows of the icon view.
6113  *
6114  * Since: 2.6
6115  */
6116 void 
6117 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
6118                                gint         row_spacing)
6119 {
6120   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6121   
6122   if (icon_view->priv->row_spacing != row_spacing)
6123     {
6124       icon_view->priv->row_spacing = row_spacing;
6125
6126       gtk_icon_view_stop_editing (icon_view, TRUE);
6127       gtk_icon_view_invalidate_sizes (icon_view);
6128       gtk_icon_view_queue_layout (icon_view);
6129       
6130       g_object_notify (G_OBJECT (icon_view), "row-spacing");
6131     }  
6132 }
6133
6134 /**
6135  * gtk_icon_view_get_row_spacing:
6136  * @icon_view: a #GtkIconView
6137  * 
6138  * Returns the value of the ::row-spacing property.
6139  * 
6140  * Return value: the space between rows
6141  *
6142  * Since: 2.6
6143  */
6144 gint
6145 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
6146 {
6147   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6148
6149   return icon_view->priv->row_spacing;
6150 }
6151
6152 /**
6153  * gtk_icon_view_set_column_spacing:
6154  * @icon_view: a #GtkIconView
6155  * @column_spacing: the column spacing
6156  * 
6157  * Sets the ::column-spacing property which specifies the space 
6158  * which is inserted between the columns of the icon view.
6159  *
6160  * Since: 2.6
6161  */
6162 void 
6163 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
6164                                   gint         column_spacing)
6165 {
6166   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6167   
6168   if (icon_view->priv->column_spacing != column_spacing)
6169     {
6170       icon_view->priv->column_spacing = column_spacing;
6171
6172       gtk_icon_view_stop_editing (icon_view, TRUE);
6173       gtk_icon_view_invalidate_sizes (icon_view);
6174       gtk_icon_view_queue_layout (icon_view);
6175       
6176       g_object_notify (G_OBJECT (icon_view), "column-spacing");
6177     }  
6178 }
6179
6180 /**
6181  * gtk_icon_view_get_column_spacing:
6182  * @icon_view: a #GtkIconView
6183  * 
6184  * Returns the value of the ::column-spacing property.
6185  * 
6186  * Return value: the space between columns
6187  *
6188  * Since: 2.6
6189  */
6190 gint
6191 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
6192 {
6193   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6194
6195   return icon_view->priv->column_spacing;
6196 }
6197
6198 /**
6199  * gtk_icon_view_set_margin:
6200  * @icon_view: a #GtkIconView
6201  * @margin: the margin
6202  * 
6203  * Sets the ::margin property which specifies the space 
6204  * which is inserted at the top, bottom, left and right 
6205  * of the icon view.
6206  *
6207  * Since: 2.6
6208  */
6209 void 
6210 gtk_icon_view_set_margin (GtkIconView *icon_view,
6211                           gint         margin)
6212 {
6213   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6214   
6215   if (icon_view->priv->margin != margin)
6216     {
6217       icon_view->priv->margin = margin;
6218
6219       gtk_icon_view_stop_editing (icon_view, TRUE);
6220       gtk_icon_view_invalidate_sizes (icon_view);
6221       gtk_icon_view_queue_layout (icon_view);
6222       
6223       g_object_notify (G_OBJECT (icon_view), "margin");
6224     }  
6225 }
6226
6227 /**
6228  * gtk_icon_view_get_margin:
6229  * @icon_view: a #GtkIconView
6230  * 
6231  * Returns the value of the ::margin property.
6232  * 
6233  * Return value: the space at the borders 
6234  *
6235  * Since: 2.6
6236  */
6237 gint
6238 gtk_icon_view_get_margin (GtkIconView *icon_view)
6239 {
6240   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
6241
6242   return icon_view->priv->margin;
6243 }
6244
6245
6246 /* Get/set whether drag_motion requested the drag data and
6247  * drag_data_received should thus not actually insert the data,
6248  * since the data doesn't result from a drop.
6249  */
6250 static void
6251 set_status_pending (GdkDragContext *context,
6252                     GdkDragAction   suggested_action)
6253 {
6254   g_object_set_data (G_OBJECT (context),
6255                      I_("gtk-icon-view-status-pending"),
6256                      GINT_TO_POINTER (suggested_action));
6257 }
6258
6259 static GdkDragAction
6260 get_status_pending (GdkDragContext *context)
6261 {
6262   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
6263                                              "gtk-icon-view-status-pending"));
6264 }
6265
6266 static void
6267 unset_reorderable (GtkIconView *icon_view)
6268 {
6269   if (icon_view->priv->reorderable)
6270     {
6271       icon_view->priv->reorderable = FALSE;
6272       g_object_notify (G_OBJECT (icon_view), "reorderable");
6273     }
6274 }
6275
6276 static void
6277 set_source_row (GdkDragContext *context,
6278                 GtkTreeModel   *model,
6279                 GtkTreePath    *source_row)
6280 {
6281   if (source_row)
6282     g_object_set_data_full (G_OBJECT (context),
6283                             I_("gtk-icon-view-source-row"),
6284                             gtk_tree_row_reference_new (model, source_row),
6285                             (GDestroyNotify) gtk_tree_row_reference_free);
6286   else
6287     g_object_set_data_full (G_OBJECT (context),
6288                             I_("gtk-icon-view-source-row"),
6289                             NULL, NULL);
6290 }
6291
6292 static GtkTreePath*
6293 get_source_row (GdkDragContext *context)
6294 {
6295   GtkTreeRowReference *ref;
6296
6297   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
6298
6299   if (ref)
6300     return gtk_tree_row_reference_get_path (ref);
6301   else
6302     return NULL;
6303 }
6304
6305 typedef struct
6306 {
6307   GtkTreeRowReference *dest_row;
6308   gboolean             empty_view_drop;
6309   gboolean             drop_append_mode;
6310 } DestRow;
6311
6312 static void
6313 dest_row_free (gpointer data)
6314 {
6315   DestRow *dr = (DestRow *)data;
6316
6317   gtk_tree_row_reference_free (dr->dest_row);
6318   g_free (dr);
6319 }
6320
6321 static void
6322 set_dest_row (GdkDragContext *context,
6323               GtkTreeModel   *model,
6324               GtkTreePath    *dest_row,
6325               gboolean        empty_view_drop,
6326               gboolean        drop_append_mode)
6327 {
6328   DestRow *dr;
6329
6330   if (!dest_row)
6331     {
6332       g_object_set_data_full (G_OBJECT (context),
6333                               I_("gtk-icon-view-dest-row"),
6334                               NULL, NULL);
6335       return;
6336     }
6337   
6338   dr = g_new0 (DestRow, 1);
6339      
6340   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
6341   dr->empty_view_drop = empty_view_drop;
6342   dr->drop_append_mode = drop_append_mode;
6343   g_object_set_data_full (G_OBJECT (context),
6344                           I_("gtk-icon-view-dest-row"),
6345                           dr, (GDestroyNotify) dest_row_free);
6346 }
6347
6348 static GtkTreePath*
6349 get_dest_row (GdkDragContext *context)
6350 {
6351   DestRow *dr;
6352
6353   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
6354
6355   if (dr)
6356     {
6357       GtkTreePath *path = NULL;
6358       
6359       if (dr->dest_row)
6360         path = gtk_tree_row_reference_get_path (dr->dest_row);
6361       else if (dr->empty_view_drop)
6362         path = gtk_tree_path_new_from_indices (0, -1);
6363       else
6364         path = NULL;
6365
6366       if (path && dr->drop_append_mode)
6367         gtk_tree_path_next (path);
6368
6369       return path;
6370     }
6371   else
6372     return NULL;
6373 }
6374
6375 static gboolean
6376 check_model_dnd (GtkTreeModel *model,
6377                  GType         required_iface,
6378                  const gchar  *signal)
6379 {
6380   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
6381     {
6382       g_warning ("You must override the default '%s' handler "
6383                  "on GtkIconView when using models that don't support "
6384                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
6385                  "is to connect to '%s' and call "
6386                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
6387                  "the default handler from running. Look at the source code "
6388                  "for the default handler in gtkiconview.c to get an idea what "
6389                  "your handler should do. (gtkiconview.c is in the GTK+ source "
6390                  "code.) If you're using GTK+ from a language other than C, "
6391                  "there may be a more natural way to override default handlers, e.g. via derivation.",
6392                  signal, g_type_name (required_iface), signal);
6393       return FALSE;
6394     }
6395   else
6396     return TRUE;
6397 }
6398
6399 static void
6400 remove_scroll_timeout (GtkIconView *icon_view)
6401 {
6402   if (icon_view->priv->scroll_timeout_id != 0)
6403     {
6404       g_source_remove (icon_view->priv->scroll_timeout_id);
6405
6406       icon_view->priv->scroll_timeout_id = 0;
6407     }
6408 }
6409
6410 static void
6411 gtk_icon_view_autoscroll (GtkIconView *icon_view)
6412 {
6413   gint px, py, x, y, width, height;
6414   gint hoffset, voffset;
6415   gfloat value;
6416
6417   gdk_window_get_pointer (GTK_WIDGET (icon_view)->window, &px, &py, NULL);
6418   gdk_window_get_geometry (GTK_WIDGET (icon_view)->window, &x, &y, &width, &height, NULL);
6419
6420   /* see if we are near the edge. */
6421   voffset = py - (y + 2 * SCROLL_EDGE_SIZE);
6422   if (voffset > 0)
6423     voffset = MAX (py - (y + height - 2 * SCROLL_EDGE_SIZE), 0);
6424
6425   hoffset = px - (x + 2 * SCROLL_EDGE_SIZE);
6426   if (hoffset > 0)
6427     hoffset = MAX (px - (x + width - 2 * SCROLL_EDGE_SIZE), 0);
6428
6429   if (voffset != 0)
6430     {
6431       value = CLAMP (icon_view->priv->vadjustment->value + voffset, 
6432                      icon_view->priv->vadjustment->lower,
6433                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
6434       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
6435     }
6436   if (hoffset != 0)
6437     {
6438       value = CLAMP (icon_view->priv->hadjustment->value + hoffset, 
6439                      icon_view->priv->hadjustment->lower,
6440                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
6441       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
6442     }
6443 }
6444
6445
6446 static gboolean
6447 drag_scroll_timeout (gpointer data)
6448 {
6449   GtkIconView *icon_view = GTK_ICON_VIEW (data);
6450
6451   gtk_icon_view_autoscroll (icon_view);
6452
6453   return TRUE;
6454 }
6455
6456
6457 static gboolean
6458 set_destination (GtkIconView    *icon_view,
6459                  GdkDragContext *context,
6460                  gint            x,
6461                  gint            y,
6462                  GdkDragAction  *suggested_action,
6463                  GdkAtom        *target)
6464 {
6465   GtkWidget *widget;
6466   GtkTreePath *path = NULL;
6467   GtkIconViewDropPosition pos;
6468   GtkIconViewDropPosition old_pos;
6469   GtkTreePath *old_dest_path = NULL;
6470   gboolean can_drop = FALSE;
6471
6472   widget = GTK_WIDGET (icon_view);
6473
6474   *suggested_action = 0;
6475   *target = GDK_NONE;
6476
6477   if (!icon_view->priv->dest_set)
6478     {
6479       /* someone unset us as a drag dest, note that if
6480        * we return FALSE drag_leave isn't called
6481        */
6482
6483       gtk_icon_view_set_drag_dest_item (icon_view,
6484                                         NULL,
6485                                         GTK_ICON_VIEW_DROP_LEFT);
6486
6487       remove_scroll_timeout (GTK_ICON_VIEW (widget));
6488
6489       return FALSE; /* no longer a drop site */
6490     }
6491
6492   *target = gtk_drag_dest_find_target (widget, context,
6493                                        gtk_drag_dest_get_target_list (widget));
6494   if (*target == GDK_NONE)
6495     return FALSE;
6496
6497   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
6498     {
6499       gint n_children;
6500       GtkTreeModel *model;
6501       
6502       /* the row got dropped on empty space, let's setup a special case
6503        */
6504
6505       if (path)
6506         gtk_tree_path_free (path);
6507
6508       model = gtk_icon_view_get_model (icon_view);
6509
6510       n_children = gtk_tree_model_iter_n_children (model, NULL);
6511       if (n_children)
6512         {
6513           pos = GTK_ICON_VIEW_DROP_BELOW;
6514           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6515         }
6516       else
6517         {
6518           pos = GTK_ICON_VIEW_DROP_ABOVE;
6519           path = gtk_tree_path_new_from_indices (0, -1);
6520         }
6521
6522       can_drop = TRUE;
6523
6524       goto out;
6525     }
6526
6527   g_assert (path);
6528
6529   gtk_icon_view_get_drag_dest_item (icon_view,
6530                                     &old_dest_path,
6531                                     &old_pos);
6532   
6533   if (old_dest_path)
6534     gtk_tree_path_free (old_dest_path);
6535   
6536   if (TRUE /* FIXME if the location droppable predicate */)
6537     {
6538       can_drop = TRUE;
6539     }
6540
6541 out:
6542   if (can_drop)
6543     {
6544       GtkWidget *source_widget;
6545
6546       *suggested_action = context->suggested_action;
6547       source_widget = gtk_drag_get_source_widget (context);
6548
6549       if (source_widget == widget)
6550         {
6551           /* Default to MOVE, unless the user has
6552            * pressed ctrl or shift to affect available actions
6553            */
6554           if ((context->actions & GDK_ACTION_MOVE) != 0)
6555             *suggested_action = GDK_ACTION_MOVE;
6556         }
6557
6558       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6559                                         path, pos);
6560     }
6561   else
6562     {
6563       /* can't drop here */
6564       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6565                                         NULL,
6566                                         GTK_ICON_VIEW_DROP_LEFT);
6567     }
6568   
6569   if (path)
6570     gtk_tree_path_free (path);
6571   
6572   return TRUE;
6573 }
6574
6575 static GtkTreePath*
6576 get_logical_destination (GtkIconView *icon_view,
6577                          gboolean    *drop_append_mode)
6578 {
6579   /* adjust path to point to the row the drop goes in front of */
6580   GtkTreePath *path = NULL;
6581   GtkIconViewDropPosition pos;
6582   
6583   *drop_append_mode = FALSE;
6584
6585   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6586
6587   if (path == NULL)
6588     return NULL;
6589
6590   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
6591       pos == GTK_ICON_VIEW_DROP_BELOW)
6592     {
6593       GtkTreeIter iter;
6594       GtkTreeModel *model = icon_view->priv->model;
6595
6596       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6597           !gtk_tree_model_iter_next (model, &iter))
6598         *drop_append_mode = TRUE;
6599       else
6600         {
6601           *drop_append_mode = FALSE;
6602           gtk_tree_path_next (path);
6603         }      
6604     }
6605
6606   return path;
6607 }
6608
6609 static gboolean
6610 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
6611                                 GdkEventMotion *event)
6612 {
6613   GtkWidget *widget = GTK_WIDGET (icon_view);
6614   GdkDragContext *context;
6615   GtkTreePath *path = NULL;
6616   gint button;
6617   GtkTreeModel *model;
6618   gboolean retval = FALSE;
6619
6620   if (!icon_view->priv->source_set)
6621     goto out;
6622
6623   if (icon_view->priv->pressed_button < 0)
6624     goto out;
6625
6626   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6627                                  icon_view->priv->press_start_x,
6628                                  icon_view->priv->press_start_y,
6629                                  event->x, event->y))
6630     goto out;
6631
6632   model = gtk_icon_view_get_model (icon_view);
6633
6634   if (model == NULL)
6635     goto out;
6636
6637   button = icon_view->priv->pressed_button;
6638   icon_view->priv->pressed_button = -1;
6639
6640   path = gtk_icon_view_get_path_at_pos (icon_view,
6641                                         icon_view->priv->press_start_x,
6642                                         icon_view->priv->press_start_y);
6643
6644   if (path == NULL)
6645     goto out;
6646
6647   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6648       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6649                                            path))
6650     goto out;
6651
6652   /* FIXME Check whether we're a start button, if not return FALSE and
6653    * free path
6654    */
6655
6656   /* Now we can begin the drag */
6657   
6658   retval = TRUE;
6659
6660   context = gtk_drag_begin (widget,
6661                             gtk_drag_source_get_target_list (widget),
6662                             icon_view->priv->source_actions,
6663                             button,
6664                             (GdkEvent*)event);
6665
6666   set_source_row (context, model, path);
6667   
6668  out:
6669   if (path)
6670     gtk_tree_path_free (path);
6671
6672   return retval;
6673 }
6674
6675 /* Source side drag signals */
6676 static void 
6677 gtk_icon_view_drag_begin (GtkWidget      *widget,
6678                           GdkDragContext *context)
6679 {
6680   GtkIconView *icon_view;
6681   GtkIconViewItem *item;
6682   GdkPixmap *icon;
6683   gint x, y;
6684   GtkTreePath *path;
6685
6686   icon_view = GTK_ICON_VIEW (widget);
6687
6688   /* if the user uses a custom DnD impl, we don't set the icon here */
6689   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6690     return;
6691
6692   item = gtk_icon_view_get_item_at_coords (icon_view,
6693                                            icon_view->priv->press_start_x,
6694                                            icon_view->priv->press_start_y,
6695                                            TRUE,
6696                                            NULL);
6697
6698   g_return_if_fail (item != NULL);
6699
6700   x = icon_view->priv->press_start_x - item->x + 1;
6701   y = icon_view->priv->press_start_y - item->y + 1;
6702   
6703   path = gtk_tree_path_new_from_indices (item->index, -1);
6704   icon = gtk_icon_view_create_drag_icon (icon_view, path);
6705   gtk_tree_path_free (path);
6706
6707   gtk_drag_set_icon_pixmap (context, 
6708                             gdk_drawable_get_colormap (icon),
6709                             icon, 
6710                             NULL, 
6711                             x, y);
6712
6713   g_object_unref (icon);
6714 }
6715
6716 static void 
6717 gtk_icon_view_drag_end (GtkWidget      *widget,
6718                         GdkDragContext *context)
6719 {
6720   /* do nothing */
6721 }
6722
6723 static void 
6724 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6725                              GdkDragContext   *context,
6726                              GtkSelectionData *selection_data,
6727                              guint             info,
6728                              guint             time)
6729 {
6730   GtkIconView *icon_view;
6731   GtkTreeModel *model;
6732   GtkTreePath *source_row;
6733
6734   icon_view = GTK_ICON_VIEW (widget);
6735   model = gtk_icon_view_get_model (icon_view);
6736
6737   if (model == NULL)
6738     return;
6739
6740   if (!icon_view->priv->source_set)
6741     return;
6742
6743   source_row = get_source_row (context);
6744
6745   if (source_row == NULL)
6746     return;
6747
6748   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6749    * any model; for DragSource models there are some other targets
6750    * we also support.
6751    */
6752
6753   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6754       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6755                                           source_row,
6756                                           selection_data))
6757     goto done;
6758
6759   /* If drag_data_get does nothing, try providing row data. */
6760   if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6761     gtk_tree_set_row_drag_data (selection_data,
6762                                 model,
6763                                 source_row);
6764
6765  done:
6766   gtk_tree_path_free (source_row);
6767 }
6768
6769 static void 
6770 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6771                                 GdkDragContext *context)
6772 {
6773   GtkTreeModel *model;
6774   GtkIconView *icon_view;
6775   GtkTreePath *source_row;
6776
6777   icon_view = GTK_ICON_VIEW (widget);
6778   model = gtk_icon_view_get_model (icon_view);
6779
6780   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6781     return;
6782
6783   if (!icon_view->priv->source_set)
6784     return;
6785
6786   source_row = get_source_row (context);
6787
6788   if (source_row == NULL)
6789     return;
6790
6791   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6792                                          source_row);
6793
6794   gtk_tree_path_free (source_row);
6795
6796   set_source_row (context, NULL, NULL);
6797 }
6798
6799 /* Target side drag signals */
6800 static void
6801 gtk_icon_view_drag_leave (GtkWidget      *widget,
6802                           GdkDragContext *context,
6803                           guint           time)
6804 {
6805   GtkIconView *icon_view;
6806
6807   icon_view = GTK_ICON_VIEW (widget);
6808
6809   /* unset any highlight row */
6810   gtk_icon_view_set_drag_dest_item (icon_view,
6811                                     NULL,
6812                                     GTK_ICON_VIEW_DROP_LEFT);
6813
6814   remove_scroll_timeout (icon_view);
6815 }
6816
6817 static gboolean 
6818 gtk_icon_view_drag_motion (GtkWidget      *widget,
6819                            GdkDragContext *context,
6820                            gint            x,
6821                            gint            y,
6822                            guint           time)
6823 {
6824   GtkTreePath *path = NULL;
6825   GtkIconViewDropPosition pos;
6826   GtkIconView *icon_view;
6827   GdkDragAction suggested_action = 0;
6828   GdkAtom target;
6829   gboolean empty;
6830
6831   icon_view = GTK_ICON_VIEW (widget);
6832
6833   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6834     return FALSE;
6835
6836   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6837
6838   /* we only know this *after* set_desination_row */
6839   empty = icon_view->priv->empty_view_drop;
6840
6841   if (path == NULL && !empty)
6842     {
6843       /* Can't drop here. */
6844       gdk_drag_status (context, 0, time);
6845     }
6846   else
6847     {
6848       if (icon_view->priv->scroll_timeout_id == 0)
6849         {
6850           icon_view->priv->scroll_timeout_id =
6851             gdk_threads_add_timeout (50, drag_scroll_timeout, icon_view);
6852         }
6853
6854       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6855         {
6856           /* Request data so we can use the source row when
6857            * determining whether to accept the drop
6858            */
6859           set_status_pending (context, suggested_action);
6860           gtk_drag_get_data (widget, context, target, time);
6861         }
6862       else
6863         {
6864           set_status_pending (context, 0);
6865           gdk_drag_status (context, suggested_action, time);
6866         }
6867     }
6868
6869   if (path)
6870     gtk_tree_path_free (path);
6871
6872   return TRUE;
6873 }
6874
6875 static gboolean 
6876 gtk_icon_view_drag_drop (GtkWidget      *widget,
6877                          GdkDragContext *context,
6878                          gint            x,
6879                          gint            y,
6880                          guint           time)
6881 {
6882   GtkIconView *icon_view;
6883   GtkTreePath *path;
6884   GdkDragAction suggested_action = 0;
6885   GdkAtom target = GDK_NONE;
6886   GtkTreeModel *model;
6887   gboolean drop_append_mode;
6888
6889   icon_view = GTK_ICON_VIEW (widget);
6890   model = gtk_icon_view_get_model (icon_view);
6891
6892   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6893
6894   if (!icon_view->priv->dest_set)
6895     return FALSE;
6896
6897   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6898     return FALSE;
6899
6900   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6901     return FALSE;
6902   
6903   path = get_logical_destination (icon_view, &drop_append_mode);
6904
6905   if (target != GDK_NONE && path != NULL)
6906     {
6907       /* in case a motion had requested drag data, change things so we
6908        * treat drag data receives as a drop.
6909        */
6910       set_status_pending (context, 0);
6911       set_dest_row (context, model, path, 
6912                     icon_view->priv->empty_view_drop, drop_append_mode);
6913     }
6914
6915   if (path)
6916     gtk_tree_path_free (path);
6917
6918   /* Unset this thing */
6919   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6920
6921   if (target != GDK_NONE)
6922     {
6923       gtk_drag_get_data (widget, context, target, time);
6924       return TRUE;
6925     }
6926   else
6927     return FALSE;
6928 }
6929
6930 static void
6931 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6932                                   GdkDragContext   *context,
6933                                   gint              x,
6934                                   gint              y,
6935                                   GtkSelectionData *selection_data,
6936                                   guint             info,
6937                                   guint             time)
6938 {
6939   GtkTreePath *path;
6940   gboolean accepted = FALSE;
6941   GtkTreeModel *model;
6942   GtkIconView *icon_view;
6943   GtkTreePath *dest_row;
6944   GdkDragAction suggested_action;
6945   gboolean drop_append_mode;
6946   
6947   icon_view = GTK_ICON_VIEW (widget);  
6948   model = gtk_icon_view_get_model (icon_view);
6949
6950   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6951     return;
6952
6953   if (!icon_view->priv->dest_set)
6954     return;
6955
6956   suggested_action = get_status_pending (context);
6957
6958   if (suggested_action)
6959     {
6960       /* We are getting this data due to a request in drag_motion,
6961        * rather than due to a request in drag_drop, so we are just
6962        * supposed to call drag_status, not actually paste in the
6963        * data.
6964        */
6965       path = get_logical_destination (icon_view, &drop_append_mode);
6966
6967       if (path == NULL)
6968         suggested_action = 0;
6969
6970       if (suggested_action)
6971         {
6972           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6973                                                      path,
6974                                                      selection_data))
6975             suggested_action = 0;
6976         }
6977
6978       gdk_drag_status (context, suggested_action, time);
6979
6980       if (path)
6981         gtk_tree_path_free (path);
6982
6983       /* If you can't drop, remove user drop indicator until the next motion */
6984       if (suggested_action == 0)
6985         gtk_icon_view_set_drag_dest_item (icon_view,
6986                                           NULL,
6987                                           GTK_ICON_VIEW_DROP_LEFT);
6988       return;
6989     }
6990   
6991
6992   dest_row = get_dest_row (context);
6993
6994   if (dest_row == NULL)
6995     return;
6996
6997   if (selection_data->length >= 0)
6998     {
6999       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
7000                                                  dest_row,
7001                                                  selection_data))
7002         accepted = TRUE;
7003     }
7004
7005   gtk_drag_finish (context,
7006                    accepted,
7007                    (context->action == GDK_ACTION_MOVE),
7008                    time);
7009
7010   gtk_tree_path_free (dest_row);
7011
7012   /* drop dest_row */
7013   set_dest_row (context, NULL, NULL, FALSE, FALSE);
7014 }
7015
7016 /* Drag-and-Drop support */
7017 /**
7018  * gtk_icon_view_enable_model_drag_source:
7019  * @icon_view: a #GtkIconTreeView
7020  * @start_button_mask: Mask of allowed buttons to start drag
7021  * @targets: the table of targets that the drag will support
7022  * @n_targets: the number of items in @targets
7023  * @actions: the bitmask of possible actions for a drag from this
7024  *    widget
7025  *
7026  * Turns @icon_view into a drag source for automatic DND. Calling this
7027  * method sets #GtkIconView:reorderable to %FALSE.
7028  *
7029  * Since: 2.8
7030  **/
7031 void
7032 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
7033                                         GdkModifierType           start_button_mask,
7034                                         const GtkTargetEntry     *targets,
7035                                         gint                      n_targets,
7036                                         GdkDragAction             actions)
7037 {
7038   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7039
7040   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
7041
7042   icon_view->priv->start_button_mask = start_button_mask;
7043   icon_view->priv->source_actions = actions;
7044
7045   icon_view->priv->source_set = TRUE;
7046
7047   unset_reorderable (icon_view);
7048 }
7049
7050 /**
7051  * gtk_icon_view_enable_model_drag_dest:
7052  * @icon_view: a #GtkIconView
7053  * @targets: the table of targets that the drag will support
7054  * @n_targets: the number of items in @targets
7055  * @actions: the bitmask of possible actions for a drag to this
7056  *    widget
7057  *
7058  * Turns @icon_view into a drop destination for automatic DND. Calling this
7059  * method sets #GtkIconView:reorderable to %FALSE.
7060  *
7061  * Since: 2.8
7062  **/
7063 void 
7064 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
7065                                       const GtkTargetEntry *targets,
7066                                       gint                  n_targets,
7067                                       GdkDragAction         actions)
7068 {
7069   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7070
7071   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
7072
7073   icon_view->priv->dest_actions = actions;
7074
7075   icon_view->priv->dest_set = TRUE;
7076
7077   unset_reorderable (icon_view);  
7078 }
7079
7080 /**
7081  * gtk_icon_view_unset_model_drag_source:
7082  * @icon_view: a #GtkIconView
7083  * 
7084  * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
7085  * method sets #GtkIconView:reorderable to %FALSE.
7086  *
7087  * Since: 2.8
7088  **/
7089 void
7090 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
7091 {
7092   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7093
7094   if (icon_view->priv->source_set)
7095     {
7096       gtk_drag_source_unset (GTK_WIDGET (icon_view));
7097       icon_view->priv->source_set = FALSE;
7098     }
7099
7100   unset_reorderable (icon_view);
7101 }
7102
7103 /**
7104  * gtk_icon_view_unset_model_drag_dest:
7105  * @icon_view: a #GtkIconView
7106  * 
7107  * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
7108  * method sets #GtkIconView:reorderable to %FALSE.
7109  *
7110  * Since: 2.8
7111  **/
7112 void
7113 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
7114 {
7115   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7116
7117   if (icon_view->priv->dest_set)
7118     {
7119       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
7120       icon_view->priv->dest_set = FALSE;
7121     }
7122
7123   unset_reorderable (icon_view);
7124 }
7125
7126 /* These are useful to implement your own custom stuff. */
7127 /**
7128  * gtk_icon_view_set_drag_dest_item:
7129  * @icon_view: a #GtkIconView
7130  * @path: The path of the item to highlight, or %NULL.
7131  * @pos: Specifies where to drop, relative to the item
7132  * 
7133  * Sets the item that is highlighted for feedback.
7134  *
7135  * Since: 2.8
7136  */
7137 void
7138 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
7139                                   GtkTreePath              *path,
7140                                   GtkIconViewDropPosition   pos)
7141 {
7142   /* Note; this function is exported to allow a custom DND
7143    * implementation, so it can't touch TreeViewDragInfo
7144    */
7145
7146   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7147
7148   if (icon_view->priv->dest_item)
7149     {
7150       GtkTreePath *current_path;
7151       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
7152       gtk_tree_row_reference_free (icon_view->priv->dest_item);
7153       icon_view->priv->dest_item = NULL;      
7154
7155       gtk_icon_view_queue_draw_path (icon_view, current_path);
7156       gtk_tree_path_free (current_path);
7157     }
7158   
7159   /* special case a drop on an empty model */
7160   icon_view->priv->empty_view_drop = FALSE;
7161   if (pos == GTK_TREE_VIEW_DROP_BEFORE && path
7162       && gtk_tree_path_get_depth (path) == 1
7163       && gtk_tree_path_get_indices (path)[0] == 0)
7164     {
7165       gint n_children;
7166
7167       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
7168                                                    NULL);
7169
7170       if (n_children == 0)
7171         icon_view->priv->empty_view_drop = TRUE;
7172     }
7173
7174   icon_view->priv->dest_pos = pos;
7175
7176   if (path)
7177     {
7178       icon_view->priv->dest_item =
7179         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
7180                                           icon_view->priv->model, path);
7181       
7182       gtk_icon_view_queue_draw_path (icon_view, path);
7183     }
7184 }
7185
7186 /**
7187  * gtk_icon_view_get_drag_dest_item:
7188  * @icon_view: a #GtkIconView
7189  * @path: Return location for the path of the highlighted item, or %NULL.
7190  * @pos: Return location for the drop position, or %NULL
7191  * 
7192  * Gets information about the item that is highlighted for feedback.
7193  *
7194  * Since: 2.8
7195  **/
7196 void
7197 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
7198                                   GtkTreePath             **path,
7199                                   GtkIconViewDropPosition  *pos)
7200 {
7201   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7202
7203   if (path)
7204     {
7205       if (icon_view->priv->dest_item)
7206         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
7207       else
7208         *path = NULL;
7209     }
7210
7211   if (pos)
7212     *pos = icon_view->priv->dest_pos;
7213 }
7214
7215 /**
7216  * gtk_icon_view_get_dest_item_at_pos:
7217  * @icon_view: a #GtkIconView
7218  * @drag_x: the position to determine the destination item for
7219  * @drag_y: the position to determine the destination item for
7220  * @path: Return location for the path of the item, or %NULL.
7221  * @pos: Return location for the drop position, or %NULL
7222  * 
7223  * Determines the destination item for a given position.
7224  * 
7225  * Return value: whether there is an item at the given position.
7226  *
7227  * Since: 2.8
7228  **/
7229 gboolean
7230 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
7231                                     gint                      drag_x,
7232                                     gint                      drag_y,
7233                                     GtkTreePath             **path,
7234                                     GtkIconViewDropPosition  *pos)
7235 {
7236   GtkIconViewItem *item;
7237
7238   /* Note; this function is exported to allow a custom DND
7239    * implementation, so it can't touch TreeViewDragInfo
7240    */
7241
7242   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7243   g_return_val_if_fail (drag_x >= 0, FALSE);
7244   g_return_val_if_fail (drag_y >= 0, FALSE);
7245   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
7246
7247
7248   if (path)
7249     *path = NULL;
7250
7251   item = gtk_icon_view_get_item_at_coords (icon_view, 
7252                                            drag_x + icon_view->priv->hadjustment->value, 
7253                                            drag_y + icon_view->priv->vadjustment->value,
7254                                            FALSE, NULL);
7255
7256   if (item == NULL)
7257     return FALSE;
7258
7259   if (path)
7260     *path = gtk_tree_path_new_from_indices (item->index, -1);
7261
7262   if (pos)
7263     {
7264       if (drag_x < item->x + item->width / 4)
7265         *pos = GTK_ICON_VIEW_DROP_LEFT;
7266       else if (drag_x > item->x + item->width * 3 / 4)
7267         *pos = GTK_ICON_VIEW_DROP_RIGHT;
7268       else if (drag_y < item->y + item->height / 4)
7269         *pos = GTK_ICON_VIEW_DROP_ABOVE;
7270       else if (drag_y > item->y + item->height * 3 / 4)
7271         *pos = GTK_ICON_VIEW_DROP_BELOW;
7272       else
7273         *pos = GTK_ICON_VIEW_DROP_INTO;
7274     }
7275
7276   return TRUE;
7277 }
7278
7279 /**
7280  * gtk_icon_view_create_drag_icon:
7281  * @icon_view: a #GtkIconView
7282  * @path: a #GtkTreePath in @icon_view
7283  *
7284  * Creates a #GdkPixmap representation of the item at @path.  
7285  * This image is used for a drag icon.
7286  *
7287  * Return value: a newly-allocated pixmap of the drag icon.
7288  * 
7289  * Since: 2.8
7290  **/
7291 GdkPixmap *
7292 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
7293                                 GtkTreePath *path)
7294 {
7295   GtkWidget *widget;
7296   cairo_t *cr;
7297   GdkPixmap *drawable;
7298   GList *l;
7299   gint index;
7300   GdkRectangle area;
7301
7302   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
7303   g_return_val_if_fail (path != NULL, NULL);
7304
7305   widget = GTK_WIDGET (icon_view);
7306
7307   if (!GTK_WIDGET_REALIZED (widget))
7308     return NULL;
7309
7310   index = gtk_tree_path_get_indices (path)[0];
7311
7312   for (l = icon_view->priv->items; l; l = l->next) 
7313     {
7314       GtkIconViewItem *item = l->data;
7315       
7316       if (index == item->index)
7317         {
7318           drawable = gdk_pixmap_new (icon_view->priv->bin_window,
7319                                      item->width + 2,
7320                                      item->height + 2,
7321                                      -1);
7322
7323           cr = gdk_cairo_create (drawable);
7324           cairo_set_line_width (cr, 1.);
7325
7326           gdk_cairo_set_source_color
7327             (cr, &widget->style->base[GTK_WIDGET_STATE (widget)]);
7328           cairo_rectangle (cr, 0, 0, item->width + 2, item->height + 2);
7329           cairo_fill (cr);
7330
7331           area.x = 0;
7332           area.y = 0;
7333           area.width = item->width;
7334           area.height = item->height;
7335
7336           gtk_icon_view_paint_item (icon_view, cr, item, &area, 
7337                                     drawable, 1, 1, FALSE); 
7338
7339           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
7340           cairo_rectangle (cr, 0.5, 0.5, item->width + 1, item->height + 1);
7341           cairo_stroke (cr);
7342
7343           cairo_destroy (cr);
7344
7345           return drawable;
7346         }
7347     }
7348   
7349   return NULL;
7350 }
7351
7352 /**
7353  * gtk_icon_view_get_reorderable:
7354  * @icon_view: a #GtkIconView
7355  *
7356  * Retrieves whether the user can reorder the list via drag-and-drop. 
7357  * See gtk_icon_view_set_reorderable().
7358  *
7359  * Return value: %TRUE if the list can be reordered.
7360  *
7361  * Since: 2.8
7362  **/
7363 gboolean
7364 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
7365 {
7366   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
7367
7368   return icon_view->priv->reorderable;
7369 }
7370
7371 static const GtkTargetEntry item_targets[] = {
7372   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
7373 };
7374
7375
7376 /**
7377  * gtk_icon_view_set_reorderable:
7378  * @icon_view: A #GtkIconView.
7379  * @reorderable: %TRUE, if the list of items can be reordered.
7380  *
7381  * This function is a convenience function to allow you to reorder models that
7382  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
7383  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
7384  * the user can reorder the model by dragging and dropping rows.  The
7385  * developer can listen to these changes by connecting to the model's
7386  * row_inserted and row_deleted signals. The reordering is implemented by setting up
7387  * the icon view as a drag source and destination. Therefore, drag and
7388  * drop can not be used in a reorderable view for any other purpose.
7389  *
7390  * This function does not give you any degree of control over the order -- any
7391  * reordering is allowed.  If more control is needed, you should probably
7392  * handle drag and drop manually.
7393  *
7394  * Since: 2.8
7395  **/
7396 void
7397 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
7398                                gboolean     reorderable)
7399 {
7400   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
7401
7402   reorderable = reorderable != FALSE;
7403
7404   if (icon_view->priv->reorderable == reorderable)
7405     return;
7406
7407   if (reorderable)
7408     {
7409       gtk_icon_view_enable_model_drag_source (icon_view,
7410                                               GDK_BUTTON1_MASK,
7411                                               item_targets,
7412                                               G_N_ELEMENTS (item_targets),
7413                                               GDK_ACTION_MOVE);
7414       gtk_icon_view_enable_model_drag_dest (icon_view,
7415                                             item_targets,
7416                                             G_N_ELEMENTS (item_targets),
7417                                             GDK_ACTION_MOVE);
7418     }
7419   else
7420     {
7421       gtk_icon_view_unset_model_drag_source (icon_view);
7422       gtk_icon_view_unset_model_drag_dest (icon_view);
7423     }
7424
7425   icon_view->priv->reorderable = reorderable;
7426
7427   g_object_notify (G_OBJECT (icon_view), "reorderable");
7428 }
7429
7430
7431 /* Accessibility Support */
7432
7433 static gpointer accessible_parent_class;
7434 static gpointer accessible_item_parent_class;
7435 static GQuark accessible_private_data_quark = 0;
7436
7437 #define GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE      (gtk_icon_view_item_accessible_get_type ())
7438 #define GTK_ICON_VIEW_ITEM_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE, GtkIconViewItemAccessible))
7439 #define GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE))
7440
7441 static GType gtk_icon_view_item_accessible_get_type (void);
7442
7443 enum {
7444     ACTION_ACTIVATE,
7445     LAST_ACTION
7446 };
7447
7448 typedef struct
7449 {
7450   AtkObject parent;
7451
7452   GtkIconViewItem *item;
7453
7454   GtkWidget *widget;
7455
7456   AtkStateSet *state_set;
7457
7458   gchar *text;
7459
7460   GtkTextBuffer *text_buffer;
7461
7462   gchar *action_descriptions[LAST_ACTION];
7463   gchar *image_description;
7464   guint action_idle_handler;
7465 } GtkIconViewItemAccessible;
7466
7467 static const gchar *const gtk_icon_view_item_accessible_action_names[] = 
7468 {
7469   "activate",
7470   NULL
7471 };
7472
7473 static const gchar *const gtk_icon_view_item_accessible_action_descriptions[] =
7474 {
7475   "Activate item",
7476   NULL
7477 };
7478 typedef struct _GtkIconViewItemAccessibleClass
7479 {
7480   AtkObjectClass parent_class;
7481
7482 } GtkIconViewItemAccessibleClass;
7483
7484 static gboolean gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item);
7485
7486 static gboolean
7487 gtk_icon_view_item_accessible_idle_do_action (gpointer data)
7488 {
7489   GtkIconViewItemAccessible *item;
7490   GtkIconView *icon_view;
7491   GtkTreePath *path;
7492
7493   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (data);
7494   item->action_idle_handler = 0;
7495
7496   if (item->widget != NULL)
7497     {
7498       icon_view = GTK_ICON_VIEW (item->widget);
7499       path = gtk_tree_path_new_from_indices (item->item->index, -1);
7500       gtk_icon_view_item_activated (icon_view, path);
7501       gtk_tree_path_free (path);
7502     }
7503
7504   return FALSE;
7505 }
7506
7507 static gboolean
7508 gtk_icon_view_item_accessible_action_do_action (AtkAction *action,
7509                                                 gint       i)
7510 {
7511   GtkIconViewItemAccessible *item;
7512
7513   if (i < 0 || i >= LAST_ACTION) 
7514     return FALSE;
7515
7516   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
7517
7518   if (!GTK_IS_ICON_VIEW (item->widget))
7519     return FALSE;
7520
7521   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7522     return FALSE;
7523
7524   switch (i)
7525     {
7526     case ACTION_ACTIVATE:
7527       if (!item->action_idle_handler)
7528         item->action_idle_handler = gdk_threads_add_idle (gtk_icon_view_item_accessible_idle_do_action, item);
7529       break;
7530     default:
7531       g_assert_not_reached ();
7532       return FALSE;
7533
7534     }        
7535   return TRUE;
7536 }
7537
7538 static gint
7539 gtk_icon_view_item_accessible_action_get_n_actions (AtkAction *action)
7540 {
7541         return LAST_ACTION;
7542 }
7543
7544 static const gchar *
7545 gtk_icon_view_item_accessible_action_get_description (AtkAction *action,
7546                                                       gint       i)
7547 {
7548   GtkIconViewItemAccessible *item;
7549
7550   if (i < 0 || i >= LAST_ACTION) 
7551     return NULL;
7552
7553   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
7554
7555   if (item->action_descriptions[i])
7556     return item->action_descriptions[i];
7557   else
7558     return gtk_icon_view_item_accessible_action_descriptions[i];
7559 }
7560
7561 static const gchar *
7562 gtk_icon_view_item_accessible_action_get_name (AtkAction *action,
7563                                                gint       i)
7564 {
7565   if (i < 0 || i >= LAST_ACTION) 
7566     return NULL;
7567
7568   return gtk_icon_view_item_accessible_action_names[i];
7569 }
7570
7571 static gboolean
7572 gtk_icon_view_item_accessible_action_set_description (AtkAction   *action,
7573                                                       gint         i,
7574                                                       const gchar *description)
7575 {
7576   GtkIconViewItemAccessible *item;
7577
7578   if (i < 0 || i >= LAST_ACTION) 
7579     return FALSE;
7580
7581   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
7582
7583   g_free (item->action_descriptions[i]);
7584
7585   item->action_descriptions[i] = g_strdup (description);
7586
7587   return TRUE;
7588 }
7589
7590 static void
7591 atk_action_item_interface_init (AtkActionIface *iface)
7592 {
7593   iface->do_action = gtk_icon_view_item_accessible_action_do_action;
7594   iface->get_n_actions = gtk_icon_view_item_accessible_action_get_n_actions;
7595   iface->get_description = gtk_icon_view_item_accessible_action_get_description;
7596   iface->get_name = gtk_icon_view_item_accessible_action_get_name;
7597   iface->set_description = gtk_icon_view_item_accessible_action_set_description;
7598 }
7599
7600 static const gchar *
7601 gtk_icon_view_item_accessible_image_get_image_description (AtkImage *image)
7602 {
7603   GtkIconViewItemAccessible *item;
7604
7605   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
7606
7607   return item->image_description;
7608 }
7609
7610 static gboolean
7611 gtk_icon_view_item_accessible_image_set_image_description (AtkImage    *image,
7612                                                            const gchar *description)
7613 {
7614   GtkIconViewItemAccessible *item;
7615
7616   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
7617
7618   g_free (item->image_description);
7619   item->image_description = g_strdup (description);
7620
7621   return TRUE;
7622 }
7623
7624 static gboolean
7625 get_pixbuf_box (GtkIconView     *icon_view,
7626                 GtkIconViewItem *item,
7627                 GdkRectangle    *box)
7628 {
7629   GList *l;
7630
7631   for (l = icon_view->priv->cell_list; l; l = l->next)
7632     {
7633       GtkIconViewCellInfo *info = l->data;
7634       
7635       if (GTK_IS_CELL_RENDERER_PIXBUF (info->cell))
7636         {
7637           gtk_icon_view_get_cell_box (icon_view, item, info, box);
7638
7639           return TRUE;
7640         }
7641     }
7642
7643   return FALSE;
7644 }
7645
7646 static gchar *
7647 get_text (GtkIconView     *icon_view,
7648           GtkIconViewItem *item)
7649 {
7650   GList *l;
7651   gchar *text;
7652
7653   for (l = icon_view->priv->cell_list; l; l = l->next)
7654     {
7655       GtkIconViewCellInfo *info = l->data;
7656       
7657       if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
7658         {
7659           g_object_get (info->cell, "text", &text, NULL);
7660           
7661           return text;
7662         }
7663     }
7664
7665   return NULL;
7666 }
7667
7668 static void
7669 gtk_icon_view_item_accessible_image_get_image_size (AtkImage *image,
7670                                                     gint     *width,
7671                                                     gint     *height)
7672 {
7673   GtkIconViewItemAccessible *item;
7674   GdkRectangle box;
7675
7676   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
7677
7678   if (!GTK_IS_ICON_VIEW (item->widget))
7679     return;
7680
7681   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7682     return;
7683
7684   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
7685     {
7686       *width = box.width;
7687       *height = box.height;  
7688     }
7689 }
7690
7691 static void
7692 gtk_icon_view_item_accessible_image_get_image_position (AtkImage    *image,
7693                                                         gint        *x,
7694                                                         gint        *y,
7695                                                         AtkCoordType coord_type)
7696 {
7697   GtkIconViewItemAccessible *item;
7698   GdkRectangle box;
7699
7700   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
7701
7702   if (!GTK_IS_ICON_VIEW (item->widget))
7703     return;
7704
7705   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7706     return;
7707
7708   atk_component_get_position (ATK_COMPONENT (image), x, y, coord_type);
7709
7710   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
7711     {
7712       *x+= box.x - item->item->x;
7713       *y+= box.y - item->item->y;
7714     }
7715
7716 }
7717
7718 static void
7719 atk_image_item_interface_init (AtkImageIface *iface)
7720 {
7721   iface->get_image_description = gtk_icon_view_item_accessible_image_get_image_description;
7722   iface->set_image_description = gtk_icon_view_item_accessible_image_set_image_description;
7723   iface->get_image_size = gtk_icon_view_item_accessible_image_get_image_size;
7724   iface->get_image_position = gtk_icon_view_item_accessible_image_get_image_position;
7725 }
7726
7727 static gchar *
7728 gtk_icon_view_item_accessible_text_get_text (AtkText *text,
7729                                              gint     start_pos,
7730                                              gint     end_pos)
7731 {
7732   GtkIconViewItemAccessible *item;
7733   GtkTextIter start, end;
7734   GtkTextBuffer *buffer;
7735
7736   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7737
7738   if (!GTK_IS_ICON_VIEW (item->widget))
7739     return NULL;
7740
7741   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7742     return NULL;
7743
7744   buffer = item->text_buffer;
7745   gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
7746   if (end_pos < 0)
7747     gtk_text_buffer_get_end_iter (buffer, &end);
7748   else
7749     gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
7750
7751   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7752 }
7753
7754 static gunichar
7755 gtk_icon_view_item_accessible_text_get_character_at_offset (AtkText *text,
7756                                                             gint     offset)
7757 {
7758   GtkIconViewItemAccessible *item;
7759   GtkTextIter start, end;
7760   GtkTextBuffer *buffer;
7761   gchar *string;
7762   gunichar unichar;
7763
7764   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7765
7766   if (!GTK_IS_ICON_VIEW (item->widget))
7767     return '\0';
7768
7769   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7770     return '\0';
7771
7772   buffer = item->text_buffer;
7773   if (offset >= gtk_text_buffer_get_char_count (buffer))
7774     return '\0';
7775
7776   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7777   end = start;
7778   gtk_text_iter_forward_char (&end);
7779   string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
7780   unichar = g_utf8_get_char (string);
7781   g_free(string);
7782
7783   return unichar;
7784 }
7785
7786 #if 0
7787 static void
7788 get_pango_text_offsets (PangoLayout     *layout,
7789                         GtkTextBuffer   *buffer,
7790                         gint             function,
7791                         AtkTextBoundary  boundary_type,
7792                         gint             offset,
7793                         gint            *start_offset,
7794                         gint            *end_offset,
7795                         GtkTextIter     *start_iter,
7796                         GtkTextIter     *end_iter)
7797 {
7798   PangoLayoutIter *iter;
7799   PangoLayoutLine *line, *prev_line = NULL, *prev_prev_line = NULL;
7800   gint index, start_index, end_index;
7801   const gchar *text;
7802   gboolean found = FALSE;
7803
7804   text = pango_layout_get_text (layout);
7805   index = g_utf8_offset_to_pointer (text, offset) - text;
7806   iter = pango_layout_get_iter (layout);
7807   do
7808     {
7809       line = pango_layout_iter_get_line_readonly (iter);
7810       start_index = line->start_index;
7811       end_index = start_index + line->length;
7812
7813       if (index >= start_index && index <= end_index)
7814         {
7815           /*
7816            * Found line for offset
7817            */
7818           switch (function)
7819             {
7820             case 0:
7821                   /*
7822                    * We want the previous line
7823                    */
7824               if (prev_line)
7825                 {
7826                   switch (boundary_type)
7827                     {
7828                     case ATK_TEXT_BOUNDARY_LINE_START:
7829                       end_index = start_index;
7830                       start_index = prev_line->start_index;
7831                       break;
7832                     case ATK_TEXT_BOUNDARY_LINE_END:
7833                       if (prev_prev_line)
7834                         start_index = prev_prev_line->start_index + 
7835                                   prev_prev_line->length;
7836                       end_index = prev_line->start_index + prev_line->length;
7837                       break;
7838                     default:
7839                       g_assert_not_reached();
7840                     }
7841                 }
7842               else
7843                 start_index = end_index = 0;
7844               break;
7845             case 1:
7846               switch (boundary_type)
7847                 {
7848                 case ATK_TEXT_BOUNDARY_LINE_START:
7849                   if (pango_layout_iter_next_line (iter))
7850                     end_index = pango_layout_iter_get_line_readonly (iter)->start_index;
7851                   break;
7852                 case ATK_TEXT_BOUNDARY_LINE_END:
7853                   if (prev_line)
7854                     start_index = prev_line->start_index + 
7855                                   prev_line->length;
7856                   break;
7857                 default:
7858                   g_assert_not_reached();
7859                 }
7860               break;
7861             case 2:
7862                /*
7863                 * We want the next line
7864                 */
7865               if (pango_layout_iter_next_line (iter))
7866                 {
7867                   line = pango_layout_iter_get_line_readonly (iter);
7868                   switch (boundary_type)
7869                     {
7870                     case ATK_TEXT_BOUNDARY_LINE_START:
7871                       start_index = line->start_index;
7872                       if (pango_layout_iter_next_line (iter))
7873                         end_index = pango_layout_iter_get_line_readonly (iter)->start_index;
7874                       else
7875                         end_index = start_index + line->length;
7876                       break;
7877                     case ATK_TEXT_BOUNDARY_LINE_END:
7878                       start_index = end_index;
7879                       end_index = line->start_index + line->length;
7880                       break;
7881                     default:
7882                       g_assert_not_reached();
7883                     }
7884                 }
7885               else
7886                 start_index = end_index;
7887               break;
7888             }
7889           found = TRUE;
7890           break;
7891         }
7892       prev_prev_line = prev_line; 
7893       prev_line = line; 
7894     }
7895   while (pango_layout_iter_next_line (iter));
7896
7897   if (!found)
7898     {
7899       start_index = prev_line->start_index + prev_line->length;
7900       end_index = start_index;
7901     }
7902   pango_layout_iter_free (iter);
7903   *start_offset = g_utf8_pointer_to_offset (text, text + start_index);
7904   *end_offset = g_utf8_pointer_to_offset (text, text + end_index);
7905  
7906   gtk_text_buffer_get_iter_at_offset (buffer, start_iter, *start_offset);
7907   gtk_text_buffer_get_iter_at_offset (buffer, end_iter, *end_offset);
7908 }
7909 #endif
7910
7911 static gchar*
7912 gtk_icon_view_item_accessible_text_get_text_before_offset (AtkText         *text,
7913                                                            gint            offset,
7914                                                            AtkTextBoundary boundary_type,
7915                                                            gint            *start_offset,
7916                                                            gint            *end_offset)
7917 {
7918   GtkIconViewItemAccessible *item;
7919   GtkTextIter start, end;
7920   GtkTextBuffer *buffer;
7921 #if 0
7922   GtkIconView *icon_view;
7923 #endif
7924
7925   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7926
7927   if (!GTK_IS_ICON_VIEW (item->widget))
7928     return NULL;
7929
7930   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7931     return NULL;
7932
7933   buffer = item->text_buffer;
7934
7935   if (!gtk_text_buffer_get_char_count (buffer))
7936     {
7937       *start_offset = 0;
7938       *end_offset = 0;
7939       return g_strdup ("");
7940     }
7941   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7942    
7943   end = start;
7944
7945   switch (boundary_type)
7946     {
7947     case ATK_TEXT_BOUNDARY_CHAR:
7948       gtk_text_iter_backward_char(&start);
7949       break;
7950     case ATK_TEXT_BOUNDARY_WORD_START:
7951       if (!gtk_text_iter_starts_word (&start))
7952         gtk_text_iter_backward_word_start (&start);
7953       end = start;
7954       gtk_text_iter_backward_word_start(&start);
7955       break;
7956     case ATK_TEXT_BOUNDARY_WORD_END:
7957       if (gtk_text_iter_inside_word (&start) &&
7958           !gtk_text_iter_starts_word (&start))
7959         gtk_text_iter_backward_word_start (&start);
7960       while (!gtk_text_iter_ends_word (&start))
7961         {
7962           if (!gtk_text_iter_backward_char (&start))
7963             break;
7964         }
7965       end = start;
7966       gtk_text_iter_backward_word_start(&start);
7967       while (!gtk_text_iter_ends_word (&start))
7968         {
7969           if (!gtk_text_iter_backward_char (&start))
7970             break;
7971         }
7972       break;
7973     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7974       if (!gtk_text_iter_starts_sentence (&start))
7975         gtk_text_iter_backward_sentence_start (&start);
7976       end = start;
7977       gtk_text_iter_backward_sentence_start (&start);
7978       break;
7979     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7980       if (gtk_text_iter_inside_sentence (&start) &&
7981           !gtk_text_iter_starts_sentence (&start))
7982         gtk_text_iter_backward_sentence_start (&start);
7983       while (!gtk_text_iter_ends_sentence (&start))
7984         {
7985           if (!gtk_text_iter_backward_char (&start))
7986             break;
7987         }
7988       end = start;
7989       gtk_text_iter_backward_sentence_start (&start);
7990       while (!gtk_text_iter_ends_sentence (&start))
7991         {
7992           if (!gtk_text_iter_backward_char (&start))
7993             break;
7994         }
7995       break;
7996    case ATK_TEXT_BOUNDARY_LINE_START:
7997    case ATK_TEXT_BOUNDARY_LINE_END:
7998 #if 0
7999       icon_view = GTK_ICON_VIEW (item->widget);
8000       /* FIXME we probably have to use GailTextCell to salvage this */
8001       gtk_icon_view_update_item_text (icon_view, item->item);
8002       get_pango_text_offsets (icon_view->priv->layout,
8003                               buffer,
8004                               0,
8005                               boundary_type,
8006                               offset,
8007                               start_offset,
8008                               end_offset,
8009                               &start,
8010                               &end);
8011 #endif
8012       break;
8013     }
8014
8015   *start_offset = gtk_text_iter_get_offset (&start);
8016   *end_offset = gtk_text_iter_get_offset (&end);
8017
8018   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
8019 }
8020
8021 static gchar*
8022 gtk_icon_view_item_accessible_text_get_text_at_offset (AtkText         *text,
8023                                                        gint            offset,
8024                                                        AtkTextBoundary boundary_type,
8025                                                        gint            *start_offset,
8026                                                        gint            *end_offset)
8027 {
8028   GtkIconViewItemAccessible *item;
8029   GtkTextIter start, end;
8030   GtkTextBuffer *buffer;
8031 #if 0
8032   GtkIconView *icon_view;
8033 #endif
8034
8035   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
8036
8037   if (!GTK_IS_ICON_VIEW (item->widget))
8038     return NULL;
8039
8040   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
8041     return NULL;
8042
8043   buffer = item->text_buffer;
8044
8045   if (!gtk_text_buffer_get_char_count (buffer))
8046     {
8047       *start_offset = 0;
8048       *end_offset = 0;
8049       return g_strdup ("");
8050     }
8051   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
8052    
8053   end = start;
8054
8055   switch (boundary_type)
8056     {
8057     case ATK_TEXT_BOUNDARY_CHAR:
8058       gtk_text_iter_forward_char (&end);
8059       break;
8060     case ATK_TEXT_BOUNDARY_WORD_START:
8061       if (!gtk_text_iter_starts_word (&start))
8062         gtk_text_iter_backward_word_start (&start);
8063       if (gtk_text_iter_inside_word (&end))
8064         gtk_text_iter_forward_word_end (&end);
8065       while (!gtk_text_iter_starts_word (&end))
8066         {
8067           if (!gtk_text_iter_forward_char (&end))
8068             break;
8069         }
8070       break;
8071     case ATK_TEXT_BOUNDARY_WORD_END:
8072       if (gtk_text_iter_inside_word (&start) &&
8073           !gtk_text_iter_starts_word (&start))
8074         gtk_text_iter_backward_word_start (&start);
8075       while (!gtk_text_iter_ends_word (&start))
8076         {
8077           if (!gtk_text_iter_backward_char (&start))
8078             break;
8079         }
8080       gtk_text_iter_forward_word_end (&end);
8081       break;
8082     case ATK_TEXT_BOUNDARY_SENTENCE_START:
8083       if (!gtk_text_iter_starts_sentence (&start))
8084         gtk_text_iter_backward_sentence_start (&start);
8085       if (gtk_text_iter_inside_sentence (&end))
8086         gtk_text_iter_forward_sentence_end (&end);
8087       while (!gtk_text_iter_starts_sentence (&end))
8088         {
8089           if (!gtk_text_iter_forward_char (&end))
8090             break;
8091         }
8092       break;
8093     case ATK_TEXT_BOUNDARY_SENTENCE_END:
8094       if (gtk_text_iter_inside_sentence (&start) &&
8095           !gtk_text_iter_starts_sentence (&start))
8096         gtk_text_iter_backward_sentence_start (&start);
8097       while (!gtk_text_iter_ends_sentence (&start))
8098         {
8099           if (!gtk_text_iter_backward_char (&start))
8100             break;
8101         }
8102       gtk_text_iter_forward_sentence_end (&end);
8103       break;
8104    case ATK_TEXT_BOUNDARY_LINE_START:
8105    case ATK_TEXT_BOUNDARY_LINE_END:
8106 #if 0
8107       icon_view = GTK_ICON_VIEW (item->widget);
8108       /* FIXME we probably have to use GailTextCell to salvage this */
8109       gtk_icon_view_update_item_text (icon_view, item->item);
8110       get_pango_text_offsets (icon_view->priv->layout,
8111                               buffer,
8112                               1,
8113                               boundary_type,
8114                               offset,
8115                               start_offset,
8116                               end_offset,
8117                               &start,
8118                               &end);
8119 #endif
8120       break;
8121     }
8122
8123
8124   *start_offset = gtk_text_iter_get_offset (&start);
8125   *end_offset = gtk_text_iter_get_offset (&end);
8126
8127   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
8128 }
8129
8130 static gchar*
8131 gtk_icon_view_item_accessible_text_get_text_after_offset (AtkText         *text,
8132                                                           gint            offset,
8133                                                           AtkTextBoundary boundary_type,
8134                                                           gint            *start_offset,
8135                                                           gint            *end_offset)
8136 {
8137   GtkIconViewItemAccessible *item;
8138   GtkTextIter start, end;
8139   GtkTextBuffer *buffer;
8140 #if 0
8141   GtkIconView *icon_view;
8142 #endif
8143
8144   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
8145
8146   if (!GTK_IS_ICON_VIEW (item->widget))
8147     return NULL;
8148
8149   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
8150     return NULL;
8151
8152   buffer = item->text_buffer;
8153
8154   if (!gtk_text_buffer_get_char_count (buffer))
8155     {
8156       *start_offset = 0;
8157       *end_offset = 0;
8158       return g_strdup ("");
8159     }
8160   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
8161    
8162   end = start;
8163
8164   switch (boundary_type)
8165     {
8166     case ATK_TEXT_BOUNDARY_CHAR:
8167       gtk_text_iter_forward_char(&start);
8168       gtk_text_iter_forward_chars(&end, 2);
8169       break;
8170     case ATK_TEXT_BOUNDARY_WORD_START:
8171       if (gtk_text_iter_inside_word (&end))
8172         gtk_text_iter_forward_word_end (&end);
8173       while (!gtk_text_iter_starts_word (&end))
8174         {
8175           if (!gtk_text_iter_forward_char (&end))
8176             break;
8177         }
8178       start = end;
8179       if (!gtk_text_iter_is_end (&end))
8180         {
8181           gtk_text_iter_forward_word_end (&end);
8182           while (!gtk_text_iter_starts_word (&end))
8183             {
8184               if (!gtk_text_iter_forward_char (&end))
8185                 break;
8186             }
8187         }
8188       break;
8189     case ATK_TEXT_BOUNDARY_WORD_END:
8190       gtk_text_iter_forward_word_end (&end);
8191       start = end;
8192       if (!gtk_text_iter_is_end (&end))
8193         gtk_text_iter_forward_word_end (&end);
8194       break;
8195     case ATK_TEXT_BOUNDARY_SENTENCE_START:
8196       if (gtk_text_iter_inside_sentence (&end))
8197         gtk_text_iter_forward_sentence_end (&end);
8198       while (!gtk_text_iter_starts_sentence (&end))
8199         {
8200           if (!gtk_text_iter_forward_char (&end))
8201             break;
8202         }
8203       start = end;
8204       if (!gtk_text_iter_is_end (&end))
8205         {
8206           gtk_text_iter_forward_sentence_end (&end);
8207           while (!gtk_text_iter_starts_sentence (&end))
8208             {
8209               if (!gtk_text_iter_forward_char (&end))
8210                 break;
8211             }
8212         }
8213       break;
8214     case ATK_TEXT_BOUNDARY_SENTENCE_END:
8215       gtk_text_iter_forward_sentence_end (&end);
8216       start = end;
8217       if (!gtk_text_iter_is_end (&end))
8218         gtk_text_iter_forward_sentence_end (&end);
8219       break;
8220    case ATK_TEXT_BOUNDARY_LINE_START:
8221    case ATK_TEXT_BOUNDARY_LINE_END:
8222 #if 0
8223       icon_view = GTK_ICON_VIEW (item->widget);
8224       /* FIXME we probably have to use GailTextCell to salvage this */
8225       gtk_icon_view_update_item_text (icon_view, item->item);
8226       get_pango_text_offsets (icon_view->priv->layout,
8227                               buffer,
8228                               2,
8229                               boundary_type,
8230                               offset,
8231                               start_offset,
8232                               end_offset,
8233                               &start,
8234                               &end);
8235 #endif
8236       break;
8237     }
8238   *start_offset = gtk_text_iter_get_offset (&start);
8239   *end_offset = gtk_text_iter_get_offset (&end);
8240
8241   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
8242 }
8243
8244 static gint
8245 gtk_icon_view_item_accessible_text_get_character_count (AtkText *text)
8246 {
8247   GtkIconViewItemAccessible *item;
8248
8249   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
8250
8251   if (!GTK_IS_ICON_VIEW (item->widget))
8252     return 0;
8253
8254   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
8255     return 0;
8256
8257   return gtk_text_buffer_get_char_count (item->text_buffer);
8258 }
8259
8260 static void
8261 gtk_icon_view_item_accessible_text_get_character_extents (AtkText      *text,
8262                                                           gint         offset,
8263                                                           gint         *x,
8264                                                           gint         *y,
8265                                                           gint         *width,
8266                                                           gint         *height,
8267                                                           AtkCoordType coord_type)
8268 {
8269   GtkIconViewItemAccessible *item;
8270 #if 0
8271   GtkIconView *icon_view;
8272   PangoRectangle char_rect;
8273   const gchar *item_text;
8274   gint index;
8275 #endif
8276
8277   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
8278
8279   if (!GTK_IS_ICON_VIEW (item->widget))
8280     return;
8281
8282   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
8283     return;
8284
8285 #if 0
8286   icon_view = GTK_ICON_VIEW (item->widget);
8287       /* FIXME we probably have to use GailTextCell to salvage this */
8288   gtk_icon_view_update_item_text (icon_view, item->item);
8289   item_text = pango_layout_get_text (icon_view->priv->layout);
8290   index = g_utf8_offset_to_pointer (item_text, offset) - item_text;
8291   pango_layout_index_to_pos (icon_view->priv->layout, index, &char_rect);
8292
8293   atk_component_get_position (ATK_COMPONENT (text), x, y, coord_type);
8294   *x += item->item->layout_x - item->item->x + char_rect.x / PANGO_SCALE;
8295   /* Look at gtk_icon_view_paint_item() to see where the text is. */
8296   *x -=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
8297   *y += item->item->layout_y - item->item->y + char_rect.y / PANGO_SCALE;
8298   *width = char_rect.width / PANGO_SCALE;
8299   *height = char_rect.height / PANGO_SCALE;
8300 #endif
8301 }
8302
8303 static gint
8304 gtk_icon_view_item_accessible_text_get_offset_at_point (AtkText      *text,
8305                                                         gint          x,
8306                                                         gint          y,
8307                                                         AtkCoordType coord_type)
8308 {
8309   GtkIconViewItemAccessible *item;
8310   gint offset = 0;
8311 #if 0
8312   GtkIconView *icon_view;
8313   const gchar *item_text;
8314   gint index;
8315   gint l_x, l_y;
8316 #endif
8317
8318   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
8319
8320   if (!GTK_IS_ICON_VIEW (item->widget))
8321     return -1;
8322
8323   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
8324     return -1;
8325
8326 #if 0
8327   icon_view = GTK_ICON_VIEW (item->widget);
8328       /* FIXME we probably have to use GailTextCell to salvage this */
8329   gtk_icon_view_update_item_text (icon_view, item->item);
8330   atk_component_get_position (ATK_COMPONENT (text), &l_x, &l_y, coord_type);
8331   x -= l_x + item->item->layout_x - item->item->x;
8332   x +=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
8333   y -= l_y + item->item->layout_y - item->item->y;
8334   item_text = pango_layout_get_text (icon_view->priv->layout);
8335   if (!pango_layout_xy_to_index (icon_view->priv->layout, 
8336                                 x * PANGO_SCALE,
8337                                 y * PANGO_SCALE,
8338                                 &index, NULL))
8339     {
8340       if (x < 0 || y < 0)
8341         index = 0;
8342       else
8343         index = -1;
8344     } 
8345   if (index == -1)
8346     offset = g_utf8_strlen (item_text, -1);
8347   else
8348     offset = g_utf8_pointer_to_offset (item_text, item_text + index);
8349 #endif
8350   return offset;
8351 }
8352
8353 static void
8354 atk_text_item_interface_init (AtkTextIface *iface)
8355 {
8356   iface->get_text = gtk_icon_view_item_accessible_text_get_text;
8357   iface->get_character_at_offset = gtk_icon_view_item_accessible_text_get_character_at_offset;
8358   iface->get_text_before_offset = gtk_icon_view_item_accessible_text_get_text_before_offset;
8359   iface->get_text_at_offset = gtk_icon_view_item_accessible_text_get_text_at_offset;
8360   iface->get_text_after_offset = gtk_icon_view_item_accessible_text_get_text_after_offset;
8361   iface->get_character_count = gtk_icon_view_item_accessible_text_get_character_count;
8362   iface->get_character_extents = gtk_icon_view_item_accessible_text_get_character_extents;
8363   iface->get_offset_at_point = gtk_icon_view_item_accessible_text_get_offset_at_point;
8364 }
8365
8366 static void
8367 gtk_icon_view_item_accessible_get_extents (AtkComponent *component,
8368                                            gint         *x,
8369                                            gint         *y,
8370                                            gint         *width,
8371                                            gint         *height,
8372                                            AtkCoordType  coord_type)
8373 {
8374   GtkIconViewItemAccessible *item;
8375   AtkObject *parent_obj;
8376   gint l_x, l_y;
8377
8378   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component));
8379
8380   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
8381   if (!GTK_IS_WIDGET (item->widget))
8382     return;
8383
8384   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
8385     return;
8386
8387   *width = item->item->width;
8388   *height = item->item->height;
8389   if (gtk_icon_view_item_accessible_is_showing (item))
8390     {
8391       parent_obj = gtk_widget_get_accessible (item->widget);
8392       atk_component_get_position (ATK_COMPONENT (parent_obj), &l_x, &l_y, coord_type);
8393       *x = l_x + item->item->x;
8394       *y = l_y + item->item->y;
8395     }
8396   else
8397     {
8398       *x = G_MININT;
8399       *y = G_MININT;
8400     }
8401 }
8402
8403 static gboolean
8404 gtk_icon_view_item_accessible_grab_focus (AtkComponent *component)
8405 {
8406   GtkIconViewItemAccessible *item;
8407   GtkWidget *toplevel;
8408
8409   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component), FALSE);
8410
8411   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
8412   if (!GTK_IS_WIDGET (item->widget))
8413     return FALSE;
8414
8415   gtk_widget_grab_focus (item->widget);
8416   gtk_icon_view_set_cursor_item (GTK_ICON_VIEW (item->widget), item->item, -1);
8417   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item->widget));
8418   if (GTK_WIDGET_TOPLEVEL (toplevel))
8419     gtk_window_present (GTK_WINDOW (toplevel));
8420
8421   return TRUE;
8422 }
8423
8424 static void
8425 atk_component_item_interface_init (AtkComponentIface *iface)
8426 {
8427   iface->get_extents = gtk_icon_view_item_accessible_get_extents;
8428   iface->grab_focus = gtk_icon_view_item_accessible_grab_focus;
8429 }
8430
8431 static gboolean
8432 gtk_icon_view_item_accessible_add_state (GtkIconViewItemAccessible *item,
8433                                          AtkStateType               state_type,
8434                                          gboolean                   emit_signal)
8435 {
8436   gboolean rc;
8437
8438   rc = atk_state_set_add_state (item->state_set, state_type);
8439   /*
8440    * The signal should only be generated if the value changed,
8441    * not when the item is set up.  So states that are set
8442    * initially should pass FALSE as the emit_signal argument.
8443    */
8444
8445   if (emit_signal)
8446     {
8447       atk_object_notify_state_change (ATK_OBJECT (item), state_type, TRUE);
8448       /* If state_type is ATK_STATE_VISIBLE, additional notification */
8449       if (state_type == ATK_STATE_VISIBLE)
8450         g_signal_emit_by_name (item, "visible-data-changed");
8451     }
8452
8453   return rc;
8454 }
8455
8456 static gboolean
8457 gtk_icon_view_item_accessible_remove_state (GtkIconViewItemAccessible *item,
8458                                             AtkStateType               state_type,
8459                                             gboolean                   emit_signal)
8460 {
8461   if (atk_state_set_contains_state (item->state_set, state_type))
8462     {
8463       gboolean rc;
8464
8465       rc = atk_state_set_remove_state (item->state_set, state_type);
8466       /*
8467        * The signal should only be generated if the value changed,
8468        * not when the item is set up.  So states that are set
8469        * initially should pass FALSE as the emit_signal argument.
8470        */
8471
8472       if (emit_signal)
8473         {
8474           atk_object_notify_state_change (ATK_OBJECT (item), state_type, FALSE);
8475           /* If state_type is ATK_STATE_VISIBLE, additional notification */
8476           if (state_type == ATK_STATE_VISIBLE)
8477             g_signal_emit_by_name (item, "visible-data-changed");
8478         }
8479
8480       return rc;
8481     }
8482   else
8483     return FALSE;
8484 }
8485
8486 static gboolean
8487 gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item)
8488 {
8489   GtkIconView *icon_view;
8490   GdkRectangle visible_rect;
8491   gboolean is_showing;
8492
8493   /*
8494    * An item is considered "SHOWING" if any part of the item is in the
8495    * visible rectangle.
8496    */
8497
8498   if (!GTK_IS_ICON_VIEW (item->widget))
8499     return FALSE;
8500
8501   if (item->item == NULL)
8502     return FALSE;
8503
8504   icon_view = GTK_ICON_VIEW (item->widget);
8505   visible_rect.x = 0;
8506   if (icon_view->priv->hadjustment)
8507     visible_rect.x += icon_view->priv->hadjustment->value;
8508   visible_rect.y = 0;
8509   if (icon_view->priv->hadjustment)
8510     visible_rect.y += icon_view->priv->vadjustment->value;
8511   visible_rect.width = item->widget->allocation.width;
8512   visible_rect.height = item->widget->allocation.height;
8513
8514   if (((item->item->x + item->item->width) < visible_rect.x) ||
8515      ((item->item->y + item->item->height) < (visible_rect.y)) ||
8516      (item->item->x > (visible_rect.x + visible_rect.width)) ||
8517      (item->item->y > (visible_rect.y + visible_rect.height)))
8518     is_showing =  FALSE;
8519   else
8520     is_showing = TRUE;
8521
8522   return is_showing;
8523 }
8524
8525 static gboolean
8526 gtk_icon_view_item_accessible_set_visibility (GtkIconViewItemAccessible *item,
8527                                               gboolean                   emit_signal)
8528 {
8529   if (gtk_icon_view_item_accessible_is_showing (item))
8530     return gtk_icon_view_item_accessible_add_state (item, ATK_STATE_SHOWING,
8531                                                     emit_signal);
8532   else
8533     return gtk_icon_view_item_accessible_remove_state (item, ATK_STATE_SHOWING,
8534                                                        emit_signal);
8535 }
8536
8537 static void
8538 gtk_icon_view_item_accessible_object_init (GtkIconViewItemAccessible *item)
8539 {
8540   gint i;
8541
8542   item->state_set = atk_state_set_new ();
8543
8544   atk_state_set_add_state (item->state_set, ATK_STATE_ENABLED);
8545   atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSABLE);
8546   atk_state_set_add_state (item->state_set, ATK_STATE_SENSITIVE);
8547   atk_state_set_add_state (item->state_set, ATK_STATE_SELECTABLE);
8548   atk_state_set_add_state (item->state_set, ATK_STATE_VISIBLE);
8549
8550   for (i = 0; i < LAST_ACTION; i++)
8551     item->action_descriptions[i] = NULL;
8552
8553   item->image_description = NULL;
8554
8555   item->action_idle_handler = 0;
8556 }
8557
8558 static void
8559 gtk_icon_view_item_accessible_finalize (GObject *object)
8560 {
8561   GtkIconViewItemAccessible *item;
8562   gint i;
8563
8564   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (object));
8565
8566   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (object);
8567
8568   if (item->widget)
8569     g_object_remove_weak_pointer (G_OBJECT (item->widget), (gpointer) &item->widget);
8570
8571   if (item->state_set)
8572     g_object_unref (item->state_set);
8573
8574   if (item->text_buffer)
8575      g_object_unref (item->text_buffer);
8576
8577   for (i = 0; i < LAST_ACTION; i++)
8578     g_free (item->action_descriptions[i]);
8579
8580   g_free (item->image_description);
8581
8582   if (item->action_idle_handler)
8583     {
8584       g_source_remove (item->action_idle_handler);
8585       item->action_idle_handler = 0;
8586     }
8587
8588   G_OBJECT_CLASS (accessible_item_parent_class)->finalize (object);
8589 }
8590
8591 static G_CONST_RETURN gchar*
8592 gtk_icon_view_item_accessible_get_name (AtkObject *obj)
8593 {
8594   if (obj->name)
8595     return obj->name;
8596   else
8597     {
8598       GtkIconViewItemAccessible *item;
8599       GtkTextIter start_iter;
8600       GtkTextIter end_iter;
8601
8602       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8603  
8604       gtk_text_buffer_get_start_iter (item->text_buffer, &start_iter); 
8605       gtk_text_buffer_get_end_iter (item->text_buffer, &end_iter); 
8606
8607       return gtk_text_buffer_get_text (item->text_buffer, &start_iter, &end_iter, FALSE);
8608     }
8609 }
8610
8611 static AtkObject*
8612 gtk_icon_view_item_accessible_get_parent (AtkObject *obj)
8613 {
8614   GtkIconViewItemAccessible *item;
8615
8616   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), NULL);
8617   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8618
8619   if (item->widget)
8620     return gtk_widget_get_accessible (item->widget);
8621   else
8622     return NULL;
8623 }
8624
8625 static gint
8626 gtk_icon_view_item_accessible_get_index_in_parent (AtkObject *obj)
8627 {
8628   GtkIconViewItemAccessible *item;
8629
8630   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), 0);
8631   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8632
8633   return item->item->index; 
8634 }
8635
8636 static AtkStateSet *
8637 gtk_icon_view_item_accessible_ref_state_set (AtkObject *obj)
8638 {
8639   GtkIconViewItemAccessible *item;
8640   GtkIconView *icon_view;
8641
8642   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8643   g_return_val_if_fail (item->state_set, NULL);
8644
8645   if (!item->widget)
8646     return NULL;
8647
8648   icon_view = GTK_ICON_VIEW (item->widget);
8649   if (icon_view->priv->cursor_item == item->item)
8650     atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSED);
8651   else
8652     atk_state_set_remove_state (item->state_set, ATK_STATE_FOCUSED);
8653   if (item->item->selected)
8654     atk_state_set_add_state (item->state_set, ATK_STATE_SELECTED);
8655   else
8656     atk_state_set_remove_state (item->state_set, ATK_STATE_SELECTED);
8657
8658   return g_object_ref (item->state_set);
8659 }
8660
8661 static void
8662 gtk_icon_view_item_accessible_class_init (AtkObjectClass *klass)
8663 {
8664   GObjectClass *gobject_class;
8665
8666   accessible_item_parent_class = g_type_class_peek_parent (klass);
8667
8668   gobject_class = (GObjectClass *)klass;
8669
8670   gobject_class->finalize = gtk_icon_view_item_accessible_finalize;
8671
8672   klass->get_index_in_parent = gtk_icon_view_item_accessible_get_index_in_parent; 
8673   klass->get_name = gtk_icon_view_item_accessible_get_name; 
8674   klass->get_parent = gtk_icon_view_item_accessible_get_parent; 
8675   klass->ref_state_set = gtk_icon_view_item_accessible_ref_state_set; 
8676 }
8677
8678 static GType
8679 gtk_icon_view_item_accessible_get_type (void)
8680 {
8681   static GType type = 0;
8682
8683   if (!type)
8684     {
8685       const GTypeInfo tinfo =
8686       {
8687         sizeof (GtkIconViewItemAccessibleClass),
8688         (GBaseInitFunc) NULL, /* base init */
8689         (GBaseFinalizeFunc) NULL, /* base finalize */
8690         (GClassInitFunc) gtk_icon_view_item_accessible_class_init, /* class init */
8691         (GClassFinalizeFunc) NULL, /* class finalize */
8692         NULL, /* class data */
8693         sizeof (GtkIconViewItemAccessible), /* instance size */
8694         0, /* nb preallocs */
8695         (GInstanceInitFunc) gtk_icon_view_item_accessible_object_init, /* instance init */
8696         NULL /* value table */
8697       };
8698
8699       static const GInterfaceInfo atk_component_info =
8700       {
8701         (GInterfaceInitFunc) atk_component_item_interface_init,
8702         (GInterfaceFinalizeFunc) NULL,
8703         NULL
8704       };
8705       static const GInterfaceInfo atk_action_info =
8706       {
8707         (GInterfaceInitFunc) atk_action_item_interface_init,
8708         (GInterfaceFinalizeFunc) NULL,
8709         NULL
8710       };
8711       static const GInterfaceInfo atk_image_info =
8712       {
8713         (GInterfaceInitFunc) atk_image_item_interface_init,
8714         (GInterfaceFinalizeFunc) NULL,
8715         NULL
8716       };
8717       static const GInterfaceInfo atk_text_info =
8718       {
8719         (GInterfaceInitFunc) atk_text_item_interface_init,
8720         (GInterfaceFinalizeFunc) NULL,
8721         NULL
8722       };
8723
8724       type = g_type_register_static (ATK_TYPE_OBJECT,
8725                                      I_("GtkIconViewItemAccessible"), &tinfo, 0);
8726       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
8727                                    &atk_component_info);
8728       g_type_add_interface_static (type, ATK_TYPE_ACTION,
8729                                    &atk_action_info);
8730       g_type_add_interface_static (type, ATK_TYPE_IMAGE,
8731                                    &atk_image_info);
8732       g_type_add_interface_static (type, ATK_TYPE_TEXT,
8733                                    &atk_text_info);
8734     }
8735
8736   return type;
8737 }
8738
8739 #define GTK_TYPE_ICON_VIEW_ACCESSIBLE      (gtk_icon_view_accessible_get_type ())
8740 #define GTK_ICON_VIEW_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE, GtkIconViewAccessible))
8741 #define GTK_IS_ICON_VIEW_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE))
8742
8743 static GType gtk_icon_view_accessible_get_type (void);
8744
8745 typedef struct
8746 {
8747    AtkObject parent;
8748 } GtkIconViewAccessible;
8749
8750 typedef struct
8751 {
8752   AtkObject *item;
8753   gint       index;
8754 } GtkIconViewItemAccessibleInfo;
8755
8756 typedef struct
8757 {
8758   GList *items;
8759
8760   GtkAdjustment *old_hadj;
8761   GtkAdjustment *old_vadj;
8762
8763   GtkTreeModel *model;
8764
8765 } GtkIconViewAccessiblePrivate;
8766
8767 static GtkIconViewAccessiblePrivate *
8768 gtk_icon_view_accessible_get_priv (AtkObject *accessible)
8769 {
8770   return g_object_get_qdata (G_OBJECT (accessible),
8771                              accessible_private_data_quark);
8772 }
8773
8774 static void
8775 gtk_icon_view_item_accessible_info_new (AtkObject *accessible,
8776                                         AtkObject *item,
8777                                         gint       index)
8778 {
8779   GtkIconViewItemAccessibleInfo *info;
8780   GtkIconViewItemAccessibleInfo *tmp_info;
8781   GtkIconViewAccessiblePrivate *priv;
8782   GList *items;
8783
8784   info = g_new (GtkIconViewItemAccessibleInfo, 1);
8785   info->item = item;
8786   info->index = index;
8787
8788   priv = gtk_icon_view_accessible_get_priv (accessible);
8789   items = priv->items;
8790   while (items)
8791     {
8792       tmp_info = items->data;
8793       if (tmp_info->index > index)
8794         break;
8795       items = items->next;
8796     }
8797   priv->items = g_list_insert_before (priv->items, items, info);
8798   priv->old_hadj = NULL;
8799   priv->old_vadj = NULL;
8800 }
8801
8802 static gint
8803 gtk_icon_view_accessible_get_n_children (AtkObject *accessible)
8804 {
8805   GtkIconView *icon_view;
8806   GtkWidget *widget;
8807
8808   widget = GTK_ACCESSIBLE (accessible)->widget;
8809   if (!widget)
8810       return 0;
8811
8812   icon_view = GTK_ICON_VIEW (widget);
8813
8814   return g_list_length (icon_view->priv->items);
8815 }
8816
8817 static AtkObject *
8818 gtk_icon_view_accessible_find_child (AtkObject *accessible,
8819                                      gint       index)
8820 {
8821   GtkIconViewAccessiblePrivate *priv;
8822   GtkIconViewItemAccessibleInfo *info;
8823   GList *items;
8824
8825   priv = gtk_icon_view_accessible_get_priv (accessible);
8826   items = priv->items;
8827
8828   while (items)
8829     {
8830       info = items->data;
8831       if (info->index == index)
8832         return info->item;
8833       items = items->next; 
8834     }
8835   return NULL;
8836 }
8837
8838 static AtkObject *
8839 gtk_icon_view_accessible_ref_child (AtkObject *accessible,
8840                                     gint       index)
8841 {
8842   GtkIconView *icon_view;
8843   GtkWidget *widget;
8844   GList *icons;
8845   AtkObject *obj;
8846   GtkIconViewItemAccessible *a11y_item;
8847
8848   widget = GTK_ACCESSIBLE (accessible)->widget;
8849   if (!widget)
8850     return NULL;
8851
8852   icon_view = GTK_ICON_VIEW (widget);
8853   icons = g_list_nth (icon_view->priv->items, index);
8854   obj = NULL;
8855   if (icons)
8856     {
8857       GtkIconViewItem *item = icons->data;
8858    
8859       g_return_val_if_fail (item->index == index, NULL);
8860       obj = gtk_icon_view_accessible_find_child (accessible, index);
8861       if (!obj)
8862         {
8863           gchar *text;
8864
8865           obj = g_object_new (gtk_icon_view_item_accessible_get_type (), NULL);
8866           gtk_icon_view_item_accessible_info_new (accessible,
8867                                                   obj,
8868                                                   index);
8869           obj->role = ATK_ROLE_ICON;
8870           a11y_item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8871           a11y_item->item = item;
8872           a11y_item->widget = widget;
8873           a11y_item->text_buffer = gtk_text_buffer_new (NULL);
8874
8875           gtk_icon_view_set_cell_data (icon_view, item);
8876           text = get_text (icon_view, item);
8877           if (text)
8878             {
8879               gtk_text_buffer_set_text (a11y_item->text_buffer, text, -1);
8880               g_free (text);
8881             } 
8882
8883           gtk_icon_view_item_accessible_set_visibility (a11y_item, FALSE);
8884           g_object_add_weak_pointer (G_OBJECT (widget), (gpointer) &(a11y_item->widget));
8885        }
8886       g_object_ref (obj);
8887     }
8888   return obj;
8889 }
8890
8891 static void
8892 gtk_icon_view_accessible_traverse_items (GtkIconViewAccessible *view,
8893                                          GList                 *list)
8894 {
8895   GtkIconViewAccessiblePrivate *priv;
8896   GtkIconViewItemAccessibleInfo *info;
8897   GtkIconViewItemAccessible *item;
8898   GList *items;
8899   
8900   priv =  gtk_icon_view_accessible_get_priv (ATK_OBJECT (view));
8901   if (priv->items)
8902     {
8903       GtkWidget *widget;
8904       gboolean act_on_item;
8905
8906       widget = GTK_ACCESSIBLE (view)->widget;
8907       if (widget == NULL)
8908         return;
8909
8910       items = priv->items;
8911
8912       act_on_item = (list == NULL);
8913
8914       while (items)
8915         {
8916
8917           info = (GtkIconViewItemAccessibleInfo *)items->data;
8918           item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8919
8920           if (act_on_item == FALSE && list == items)
8921             act_on_item = TRUE;
8922
8923           if (act_on_item)
8924             gtk_icon_view_item_accessible_set_visibility (item, TRUE);
8925
8926           items = items->next;
8927        }
8928    }
8929 }
8930
8931 static void
8932 gtk_icon_view_accessible_adjustment_changed (GtkAdjustment *adjustment,
8933                                              GtkIconView   *icon_view)
8934 {
8935   AtkObject *obj;
8936   GtkIconViewAccessible *view;
8937
8938   /*
8939    * The scrollbars have changed
8940    */
8941   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
8942   view = GTK_ICON_VIEW_ACCESSIBLE (obj);
8943
8944   gtk_icon_view_accessible_traverse_items (view, NULL);
8945 }
8946
8947 static void
8948 gtk_icon_view_accessible_set_scroll_adjustments (GtkWidget      *widget,
8949                                                  GtkAdjustment *hadj,
8950                                                  GtkAdjustment *vadj)
8951 {
8952   AtkObject *atk_obj;
8953   GtkIconViewAccessiblePrivate *priv;
8954
8955   atk_obj = gtk_widget_get_accessible (widget);
8956   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8957
8958   if (priv->old_hadj != hadj)
8959     {
8960       if (priv->old_hadj)
8961         {
8962           g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8963                                         (gpointer *)&priv->old_hadj);
8964           
8965           g_signal_handlers_disconnect_by_func (priv->old_hadj,
8966                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8967                                                 widget);
8968         }
8969       priv->old_hadj = hadj;
8970       if (priv->old_hadj)
8971         {
8972           g_object_add_weak_pointer (G_OBJECT (priv->old_hadj),
8973                                      (gpointer *)&priv->old_hadj);
8974           g_signal_connect (hadj,
8975                             "value-changed",
8976                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8977                             widget);
8978         }
8979     }
8980   if (priv->old_vadj != vadj)
8981     {
8982       if (priv->old_vadj)
8983         {
8984           g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8985                                         (gpointer *)&priv->old_vadj);
8986           
8987           g_signal_handlers_disconnect_by_func (priv->old_vadj,
8988                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8989                                                 widget);
8990         }
8991       priv->old_vadj = vadj;
8992       if (priv->old_vadj)
8993         {
8994           g_object_add_weak_pointer (G_OBJECT (priv->old_vadj),
8995                                      (gpointer *)&priv->old_vadj);
8996           g_signal_connect (vadj,
8997                             "value-changed",
8998                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8999                             widget);
9000         }
9001     }
9002 }
9003
9004 static void
9005 gtk_icon_view_accessible_model_row_changed (GtkTreeModel *tree_model,
9006                                             GtkTreePath  *path,
9007                                             GtkTreeIter  *iter,
9008                                             gpointer      user_data)
9009 {
9010   AtkObject *atk_obj;
9011   gint index;
9012   GtkWidget *widget;
9013   GtkIconView *icon_view;
9014   GtkIconViewItem *item;
9015   GtkIconViewAccessible *a11y_view;
9016   GtkIconViewItemAccessible *a11y_item;
9017   const gchar *name;
9018   gchar *text;
9019
9020   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
9021   a11y_view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
9022   index = gtk_tree_path_get_indices(path)[0];
9023   a11y_item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (
9024       gtk_icon_view_accessible_find_child (atk_obj, index));
9025
9026   if (a11y_item)
9027     {
9028       widget = GTK_ACCESSIBLE (atk_obj)->widget;
9029       icon_view = GTK_ICON_VIEW (widget);
9030       item = a11y_item->item;
9031
9032       name = gtk_icon_view_item_accessible_get_name (ATK_OBJECT (a11y_item));
9033
9034       if (!name || strcmp (name, "") == 0)
9035         {
9036           gtk_icon_view_set_cell_data (icon_view, item);
9037           text = get_text (icon_view, item);
9038           if (text)
9039             {
9040               gtk_text_buffer_set_text (a11y_item->text_buffer, text, -1);
9041               g_free (text);
9042             }
9043         }
9044     }
9045
9046   g_signal_emit_by_name (atk_obj, "visible-data-changed");
9047
9048   return;
9049 }
9050
9051 static void
9052 gtk_icon_view_accessible_model_row_inserted (GtkTreeModel *tree_model,
9053                                              GtkTreePath  *path,
9054                                              GtkTreeIter  *iter,
9055                                              gpointer     user_data)
9056 {
9057   GtkIconViewAccessiblePrivate *priv;
9058   GtkIconViewItemAccessibleInfo *info;
9059   GtkIconViewAccessible *view;
9060   GtkIconViewItemAccessible *item;
9061   GList *items;
9062   GList *tmp_list;
9063   AtkObject *atk_obj;
9064   gint index;
9065
9066   index = gtk_tree_path_get_indices(path)[0];
9067   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
9068   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
9069   priv = gtk_icon_view_accessible_get_priv (atk_obj);
9070
9071   items = priv->items;
9072   tmp_list = NULL;
9073   while (items)
9074     {
9075       info = items->data;
9076       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
9077       if (info->index != item->item->index)
9078         {
9079           if (info->index < index)
9080             g_warning ("Unexpected index value on insertion %d %d", index, info->index);
9081  
9082           if (tmp_list == NULL)
9083             tmp_list = items;
9084    
9085           info->index = item->item->index;
9086         }
9087
9088       items = items->next;
9089     }
9090   gtk_icon_view_accessible_traverse_items (view, tmp_list);
9091   g_signal_emit_by_name (atk_obj, "children-changed::add",
9092                          index, NULL, NULL);
9093   return;
9094 }
9095
9096 static void
9097 gtk_icon_view_accessible_model_row_deleted (GtkTreeModel *tree_model,
9098                                             GtkTreePath  *path,
9099                                             gpointer     user_data)
9100 {
9101   GtkIconViewAccessiblePrivate *priv;
9102   GtkIconViewItemAccessibleInfo *info;
9103   GtkIconViewAccessible *view;
9104   GtkIconViewItemAccessible *item;
9105   GList *items;
9106   GList *tmp_list;
9107   GList *deleted_item;
9108   AtkObject *atk_obj;
9109   gint index;
9110
9111   index = gtk_tree_path_get_indices(path)[0];
9112   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
9113   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
9114   priv = gtk_icon_view_accessible_get_priv (atk_obj);
9115
9116   items = priv->items;
9117   tmp_list = NULL;
9118   deleted_item = NULL;
9119   info = NULL;
9120   while (items)
9121     {
9122       info = items->data;
9123       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
9124       if (info->index == index)
9125         {
9126           deleted_item = items;
9127         }
9128       if (info->index != item->item->index)
9129         {
9130           if (tmp_list == NULL)
9131             tmp_list = items;
9132             
9133           info->index = item->item->index;
9134         }
9135
9136       items = items->next;
9137     }
9138   gtk_icon_view_accessible_traverse_items (view, tmp_list);
9139   if (deleted_item)
9140     {
9141       info = deleted_item->data;
9142       gtk_icon_view_item_accessible_add_state (GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item), ATK_STATE_DEFUNCT, TRUE);
9143       g_signal_emit_by_name (atk_obj, "children-changed::remove",
9144                              index, NULL, NULL);
9145       priv->items = g_list_remove_link (priv->items, deleted_item);
9146       g_free (info);
9147     }
9148
9149   return;
9150 }
9151
9152 static gint
9153 gtk_icon_view_accessible_item_compare (GtkIconViewItemAccessibleInfo *i1,
9154                                        GtkIconViewItemAccessibleInfo *i2)
9155 {
9156   return i1->index - i2->index;
9157 }
9158
9159 static void
9160 gtk_icon_view_accessible_model_rows_reordered (GtkTreeModel *tree_model,
9161                                                GtkTreePath  *path,
9162                                                GtkTreeIter  *iter,
9163                                                gint         *new_order,
9164                                                gpointer     user_data)
9165 {
9166   GtkIconViewAccessiblePrivate *priv;
9167   GtkIconViewItemAccessibleInfo *info;
9168   GtkIconView *icon_view;
9169   GtkIconViewItemAccessible *item;
9170   GList *items;
9171   AtkObject *atk_obj;
9172   gint *order;
9173   gint length, i;
9174
9175   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
9176   icon_view = GTK_ICON_VIEW (user_data);
9177   priv = gtk_icon_view_accessible_get_priv (atk_obj);
9178
9179   length = gtk_tree_model_iter_n_children (tree_model, NULL);
9180
9181   order = g_new (gint, length);
9182   for (i = 0; i < length; i++)
9183     order [new_order[i]] = i;
9184
9185   items = priv->items;
9186   while (items)
9187     {
9188       info = items->data;
9189       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
9190       info->index = order[info->index];
9191       item->item = g_list_nth_data (icon_view->priv->items, info->index);
9192       items = items->next;
9193     }
9194   g_free (order);
9195   priv->items = g_list_sort (priv->items, 
9196                              (GCompareFunc)gtk_icon_view_accessible_item_compare);
9197
9198   return;
9199 }
9200
9201 static void
9202 gtk_icon_view_accessible_disconnect_model_signals (GtkTreeModel *model,
9203                                                    GtkWidget *widget)
9204 {
9205   GObject *obj;
9206
9207   obj = G_OBJECT (model);
9208   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_changed, widget);
9209   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_inserted, widget);
9210   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_deleted, widget);
9211   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_rows_reordered, widget);
9212 }
9213
9214 static void
9215 gtk_icon_view_accessible_connect_model_signals (GtkIconView *icon_view)
9216 {
9217   GObject *obj;
9218
9219   obj = G_OBJECT (icon_view->priv->model);
9220   g_signal_connect_data (obj, "row-changed",
9221                          (GCallback) gtk_icon_view_accessible_model_row_changed,
9222                          icon_view, NULL, 0);
9223   g_signal_connect_data (obj, "row-inserted",
9224                          (GCallback) gtk_icon_view_accessible_model_row_inserted, 
9225                          icon_view, NULL, G_CONNECT_AFTER);
9226   g_signal_connect_data (obj, "row-deleted",
9227                          (GCallback) gtk_icon_view_accessible_model_row_deleted, 
9228                          icon_view, NULL, G_CONNECT_AFTER);
9229   g_signal_connect_data (obj, "rows-reordered",
9230                          (GCallback) gtk_icon_view_accessible_model_rows_reordered, 
9231                          icon_view, NULL, G_CONNECT_AFTER);
9232 }
9233
9234 static void
9235 gtk_icon_view_accessible_clear_cache (GtkIconViewAccessiblePrivate *priv)
9236 {
9237   GtkIconViewItemAccessibleInfo *info;
9238   GList *items;
9239
9240   items = priv->items;
9241   while (items)
9242     {
9243       info = (GtkIconViewItemAccessibleInfo *) items->data;
9244       g_object_unref (info->item);
9245       g_free (items->data);
9246       items = items->next;
9247     }
9248   g_list_free (priv->items);
9249   priv->items = NULL;
9250 }
9251
9252 static void
9253 gtk_icon_view_accessible_notify_gtk (GObject *obj,
9254                                      GParamSpec *pspec)
9255 {
9256   GtkIconView *icon_view;
9257   GtkWidget *widget;
9258   AtkObject *atk_obj;
9259   GtkIconViewAccessiblePrivate *priv;
9260
9261   if (strcmp (pspec->name, "model") == 0)
9262     {
9263       widget = GTK_WIDGET (obj); 
9264       atk_obj = gtk_widget_get_accessible (widget);
9265       priv = gtk_icon_view_accessible_get_priv (atk_obj);
9266       if (priv->model)
9267         {
9268           g_object_remove_weak_pointer (G_OBJECT (priv->model),
9269                                         (gpointer *)&priv->model);
9270           gtk_icon_view_accessible_disconnect_model_signals (priv->model, widget);
9271         }
9272       gtk_icon_view_accessible_clear_cache (priv);
9273
9274       icon_view = GTK_ICON_VIEW (obj);
9275       priv->model = icon_view->priv->model;
9276       /* If there is no model the GtkIconView is probably being destroyed */
9277       if (priv->model)
9278         {
9279           g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
9280           gtk_icon_view_accessible_connect_model_signals (icon_view);
9281         }
9282     }
9283
9284   return;
9285 }
9286
9287 static void
9288 gtk_icon_view_accessible_initialize (AtkObject *accessible,
9289                                      gpointer   data)
9290 {
9291   GtkIconViewAccessiblePrivate *priv;
9292   GtkIconView *icon_view;
9293
9294   if (ATK_OBJECT_CLASS (accessible_parent_class)->initialize)
9295     ATK_OBJECT_CLASS (accessible_parent_class)->initialize (accessible, data);
9296
9297   priv = g_new0 (GtkIconViewAccessiblePrivate, 1);
9298   g_object_set_qdata (G_OBJECT (accessible),
9299                       accessible_private_data_quark,
9300                       priv);
9301
9302   icon_view = GTK_ICON_VIEW (data);
9303   if (icon_view->priv->hadjustment)
9304     {
9305       priv->old_hadj = icon_view->priv->hadjustment;
9306       g_object_add_weak_pointer (G_OBJECT (priv->old_hadj), (gpointer *)&priv->old_hadj);
9307       g_signal_connect (icon_view->priv->hadjustment,
9308                         "value-changed",
9309                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
9310                         icon_view);
9311     } 
9312   if (icon_view->priv->vadjustment)
9313     {
9314       priv->old_vadj = icon_view->priv->vadjustment;
9315       g_object_add_weak_pointer (G_OBJECT (priv->old_vadj), (gpointer *)&priv->old_vadj);
9316       g_signal_connect (icon_view->priv->vadjustment,
9317                         "value-changed",
9318                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
9319                         icon_view);
9320     }
9321   g_signal_connect_after (data,
9322                           "set-scroll-adjustments",
9323                           G_CALLBACK (gtk_icon_view_accessible_set_scroll_adjustments),
9324                           NULL);
9325   g_signal_connect (data,
9326                     "notify",
9327                     G_CALLBACK (gtk_icon_view_accessible_notify_gtk),
9328                     NULL);
9329
9330   priv->model = icon_view->priv->model;
9331   if (priv->model)
9332     {
9333       g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
9334       gtk_icon_view_accessible_connect_model_signals (icon_view);
9335     }
9336                           
9337   accessible->role = ATK_ROLE_LAYERED_PANE;
9338 }
9339
9340 static void
9341 gtk_icon_view_accessible_finalize (GObject *object)
9342 {
9343   GtkIconViewAccessiblePrivate *priv;
9344
9345   priv = gtk_icon_view_accessible_get_priv (ATK_OBJECT (object));
9346   gtk_icon_view_accessible_clear_cache (priv);
9347
9348   g_free (priv);
9349
9350   G_OBJECT_CLASS (accessible_parent_class)->finalize (object);
9351 }
9352
9353 static void
9354 gtk_icon_view_accessible_destroyed (GtkWidget *widget,
9355                                     GtkAccessible *accessible)
9356 {
9357   AtkObject *atk_obj;
9358   GtkIconViewAccessiblePrivate *priv;
9359
9360   atk_obj = ATK_OBJECT (accessible);
9361   priv = gtk_icon_view_accessible_get_priv (atk_obj);
9362   if (priv->old_hadj)
9363     {
9364       g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
9365                                     (gpointer *)&priv->old_hadj);
9366           
9367       g_signal_handlers_disconnect_by_func (priv->old_hadj,
9368                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
9369                                             widget);
9370       priv->old_hadj = NULL;
9371     }
9372   if (priv->old_vadj)
9373     {
9374       g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
9375                                     (gpointer *)&priv->old_vadj);
9376           
9377       g_signal_handlers_disconnect_by_func (priv->old_vadj,
9378                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
9379                                             widget);
9380       priv->old_vadj = NULL;
9381     }
9382 }
9383
9384 static void
9385 gtk_icon_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
9386 {
9387   if (accessible->widget)
9388     {
9389       g_signal_connect_after (accessible->widget,
9390                               "destroy",
9391                               G_CALLBACK (gtk_icon_view_accessible_destroyed),
9392                               accessible);
9393     }
9394   GTK_ACCESSIBLE_CLASS (accessible_parent_class)->connect_widget_destroyed (accessible);
9395 }
9396
9397 static void
9398 gtk_icon_view_accessible_class_init (AtkObjectClass *klass)
9399 {
9400   GObjectClass *gobject_class;
9401   GtkAccessibleClass *accessible_class;
9402
9403   accessible_parent_class = g_type_class_peek_parent (klass);
9404
9405   gobject_class = (GObjectClass *)klass;
9406   accessible_class = (GtkAccessibleClass *)klass;
9407
9408   gobject_class->finalize = gtk_icon_view_accessible_finalize;
9409
9410   klass->get_n_children = gtk_icon_view_accessible_get_n_children;
9411   klass->ref_child = gtk_icon_view_accessible_ref_child;
9412   klass->initialize = gtk_icon_view_accessible_initialize;
9413
9414   accessible_class->connect_widget_destroyed = gtk_icon_view_accessible_connect_widget_destroyed;
9415
9416   accessible_private_data_quark = g_quark_from_static_string ("icon_view-accessible-private-data");
9417 }
9418
9419 static AtkObject*
9420 gtk_icon_view_accessible_ref_accessible_at_point (AtkComponent *component,
9421                                                   gint          x,
9422                                                   gint          y,
9423                                                   AtkCoordType  coord_type)
9424 {
9425   GtkWidget *widget;
9426   GtkIconView *icon_view;
9427   GtkIconViewItem *item;
9428   gint x_pos, y_pos;
9429
9430   widget = GTK_ACCESSIBLE (component)->widget;
9431   if (widget == NULL)
9432   /* State is defunct */
9433     return NULL;
9434
9435   icon_view = GTK_ICON_VIEW (widget);
9436   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
9437   item = gtk_icon_view_get_item_at_coords (icon_view, x - x_pos, y - y_pos, TRUE, NULL);
9438   if (item)
9439     return gtk_icon_view_accessible_ref_child (ATK_OBJECT (component), item->index);
9440
9441   return NULL;
9442 }
9443
9444 static void
9445 atk_component_interface_init (AtkComponentIface *iface)
9446 {
9447   iface->ref_accessible_at_point = gtk_icon_view_accessible_ref_accessible_at_point;
9448 }
9449
9450 static gboolean
9451 gtk_icon_view_accessible_add_selection (AtkSelection *selection,
9452                                         gint i)
9453 {
9454   GtkWidget *widget;
9455   GtkIconView *icon_view;
9456   GtkIconViewItem *item;
9457
9458   widget = GTK_ACCESSIBLE (selection)->widget;
9459   if (widget == NULL)
9460     return FALSE;
9461
9462   icon_view = GTK_ICON_VIEW (widget);
9463
9464   item = g_list_nth_data (icon_view->priv->items, i);
9465
9466   if (!item)
9467     return FALSE;
9468
9469   gtk_icon_view_select_item (icon_view, item);
9470
9471   return TRUE;
9472 }
9473
9474 static gboolean
9475 gtk_icon_view_accessible_clear_selection (AtkSelection *selection)
9476 {
9477   GtkWidget *widget;
9478   GtkIconView *icon_view;
9479
9480   widget = GTK_ACCESSIBLE (selection)->widget;
9481   if (widget == NULL)
9482     return FALSE;
9483
9484   icon_view = GTK_ICON_VIEW (widget);
9485   gtk_icon_view_unselect_all (icon_view);
9486
9487   return TRUE;
9488 }
9489
9490 static AtkObject*
9491 gtk_icon_view_accessible_ref_selection (AtkSelection *selection,
9492                                         gint          i)
9493 {
9494   GList *l;
9495   GtkWidget *widget;
9496   GtkIconView *icon_view;
9497   GtkIconViewItem *item;
9498
9499   widget = GTK_ACCESSIBLE (selection)->widget;
9500   if (widget == NULL)
9501     return NULL;
9502
9503   icon_view = GTK_ICON_VIEW (widget);
9504
9505   l = icon_view->priv->items;
9506   while (l)
9507     {
9508       item = l->data;
9509       if (item->selected)
9510         {
9511           if (i == 0)
9512             return atk_object_ref_accessible_child (gtk_widget_get_accessible (widget), item->index);
9513           else
9514             i--;
9515         }
9516       l = l->next;
9517     }
9518
9519   return NULL;
9520 }
9521
9522 static gint
9523 gtk_icon_view_accessible_get_selection_count (AtkSelection *selection)
9524 {
9525   GtkWidget *widget;
9526   GtkIconView *icon_view;
9527   GtkIconViewItem *item;
9528   GList *l;
9529   gint count;
9530
9531   widget = GTK_ACCESSIBLE (selection)->widget;
9532   if (widget == NULL)
9533     return 0;
9534
9535   icon_view = GTK_ICON_VIEW (widget);
9536
9537   l = icon_view->priv->items;
9538   count = 0;
9539   while (l)
9540     {
9541       item = l->data;
9542
9543       if (item->selected)
9544         count++;
9545
9546       l = l->next;
9547     }
9548
9549   return count;
9550 }
9551
9552 static gboolean
9553 gtk_icon_view_accessible_is_child_selected (AtkSelection *selection,
9554                                             gint          i)
9555 {
9556   GtkWidget *widget;
9557   GtkIconView *icon_view;
9558   GtkIconViewItem *item;
9559
9560   widget = GTK_ACCESSIBLE (selection)->widget;
9561   if (widget == NULL)
9562     return FALSE;
9563
9564   icon_view = GTK_ICON_VIEW (widget);
9565
9566   item = g_list_nth_data (icon_view->priv->items, i);
9567   if (!item)
9568     return FALSE;
9569
9570   return item->selected;
9571 }
9572
9573 static gboolean
9574 gtk_icon_view_accessible_remove_selection (AtkSelection *selection,
9575                                            gint          i)
9576 {
9577   GtkWidget *widget;
9578   GtkIconView *icon_view;
9579   GtkIconViewItem *item;
9580   GList *l;
9581   gint count;
9582
9583   widget = GTK_ACCESSIBLE (selection)->widget;
9584   if (widget == NULL)
9585     return FALSE;
9586
9587   icon_view = GTK_ICON_VIEW (widget);
9588   l = icon_view->priv->items;
9589   count = 0;
9590   while (l)
9591     {
9592       item = l->data;
9593       if (item->selected)
9594         {
9595           if (count == i)
9596             {
9597               gtk_icon_view_unselect_item (icon_view, item);
9598               return TRUE;
9599             }
9600           count++;
9601         }
9602       l = l->next;
9603     }
9604
9605   return FALSE;
9606 }
9607  
9608 static gboolean
9609 gtk_icon_view_accessible_select_all_selection (AtkSelection *selection)
9610 {
9611   GtkWidget *widget;
9612   GtkIconView *icon_view;
9613
9614   widget = GTK_ACCESSIBLE (selection)->widget;
9615   if (widget == NULL)
9616     return FALSE;
9617
9618   icon_view = GTK_ICON_VIEW (widget);
9619   gtk_icon_view_select_all (icon_view);
9620   return TRUE;
9621 }
9622
9623 static void
9624 gtk_icon_view_accessible_selection_interface_init (AtkSelectionIface *iface)
9625 {
9626   iface->add_selection = gtk_icon_view_accessible_add_selection;
9627   iface->clear_selection = gtk_icon_view_accessible_clear_selection;
9628   iface->ref_selection = gtk_icon_view_accessible_ref_selection;
9629   iface->get_selection_count = gtk_icon_view_accessible_get_selection_count;
9630   iface->is_child_selected = gtk_icon_view_accessible_is_child_selected;
9631   iface->remove_selection = gtk_icon_view_accessible_remove_selection;
9632   iface->select_all_selection = gtk_icon_view_accessible_select_all_selection;
9633 }
9634
9635 static GType
9636 gtk_icon_view_accessible_get_type (void)
9637 {
9638   static GType type = 0;
9639
9640   if (!type)
9641     {
9642       static GTypeInfo tinfo =
9643       {
9644         0, /* class size */
9645         (GBaseInitFunc) NULL, /* base init */
9646         (GBaseFinalizeFunc) NULL, /* base finalize */
9647         (GClassInitFunc) gtk_icon_view_accessible_class_init,
9648         (GClassFinalizeFunc) NULL, /* class finalize */
9649         NULL, /* class data */
9650         0, /* instance size */
9651         0, /* nb preallocs */
9652         (GInstanceInitFunc) NULL, /* instance init */
9653         NULL /* value table */
9654       };
9655       static const GInterfaceInfo atk_component_info =
9656       {
9657         (GInterfaceInitFunc) atk_component_interface_init,
9658         (GInterfaceFinalizeFunc) NULL,
9659         NULL
9660       };
9661       static const GInterfaceInfo atk_selection_info = 
9662       {
9663         (GInterfaceInitFunc) gtk_icon_view_accessible_selection_interface_init,
9664         (GInterfaceFinalizeFunc) NULL,
9665         NULL
9666       };
9667
9668       /*
9669        * Figure out the size of the class and instance
9670        * we are deriving from
9671        */
9672       AtkObjectFactory *factory;
9673       GType derived_type;
9674       GTypeQuery query;
9675       GType derived_atk_type;
9676
9677       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
9678       factory = atk_registry_get_factory (atk_get_default_registry (), 
9679                                           derived_type);
9680       derived_atk_type = atk_object_factory_get_accessible_type (factory);
9681       g_type_query (derived_atk_type, &query);
9682       tinfo.class_size = query.class_size;
9683       tinfo.instance_size = query.instance_size;
9684  
9685       type = g_type_register_static (derived_atk_type, 
9686                                      I_("GtkIconViewAccessible"), 
9687                                      &tinfo, 0);
9688       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
9689                                    &atk_component_info);
9690       g_type_add_interface_static (type, ATK_TYPE_SELECTION,
9691                                    &atk_selection_info);
9692     }
9693   return type;
9694 }
9695
9696 static AtkObject *
9697 gtk_icon_view_accessible_new (GObject *obj)
9698 {
9699   AtkObject *accessible;
9700
9701   g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
9702
9703   accessible = g_object_new (gtk_icon_view_accessible_get_type (), NULL);
9704   atk_object_initialize (accessible, obj);
9705
9706   return accessible;
9707 }
9708
9709 static GType
9710 gtk_icon_view_accessible_factory_get_accessible_type (void)
9711 {
9712   return gtk_icon_view_accessible_get_type ();
9713 }
9714
9715 static AtkObject*
9716 gtk_icon_view_accessible_factory_create_accessible (GObject *obj)
9717 {
9718   return gtk_icon_view_accessible_new (obj);
9719 }
9720
9721 static void
9722 gtk_icon_view_accessible_factory_class_init (AtkObjectFactoryClass *klass)
9723 {
9724   klass->create_accessible = gtk_icon_view_accessible_factory_create_accessible;
9725   klass->get_accessible_type = gtk_icon_view_accessible_factory_get_accessible_type;
9726 }
9727
9728 static GType
9729 gtk_icon_view_accessible_factory_get_type (void)
9730 {
9731   static GType type = 0;
9732
9733   if (!type)
9734     {
9735       const GTypeInfo tinfo =
9736       {
9737         sizeof (AtkObjectFactoryClass),
9738         NULL,           /* base_init */
9739         NULL,           /* base_finalize */
9740         (GClassInitFunc) gtk_icon_view_accessible_factory_class_init,
9741         NULL,           /* class_finalize */
9742         NULL,           /* class_data */
9743         sizeof (AtkObjectFactory),
9744         0,             /* n_preallocs */
9745         NULL, NULL
9746       };
9747
9748       type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY, 
9749                                     I_("GtkIconViewAccessibleFactory"),
9750                                     &tinfo, 0);
9751     }
9752   return type;
9753 }
9754
9755
9756 static AtkObject *
9757 gtk_icon_view_get_accessible (GtkWidget *widget)
9758 {
9759   static gboolean first_time = TRUE;
9760
9761   if (first_time)
9762     {
9763       AtkObjectFactory *factory;
9764       AtkRegistry *registry;
9765       GType derived_type; 
9766       GType derived_atk_type; 
9767
9768       /*
9769        * Figure out whether accessibility is enabled by looking at the
9770        * type of the accessible object which would be created for
9771        * the parent type of GtkIconView.
9772        */
9773       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
9774
9775       registry = atk_get_default_registry ();
9776       factory = atk_registry_get_factory (registry,
9777                                           derived_type);
9778       derived_atk_type = atk_object_factory_get_accessible_type (factory);
9779       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE)) 
9780         atk_registry_set_factory_type (registry, 
9781                                        GTK_TYPE_ICON_VIEW,
9782                                        gtk_icon_view_accessible_factory_get_type ());
9783       first_time = FALSE;
9784     } 
9785   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->get_accessible (widget);
9786 }
9787
9788 static gboolean
9789 gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
9790                                           GtkBuilder    *builder,
9791                                           GObject       *child,
9792                                           const gchar   *tagname,
9793                                           GMarkupParser *parser,
9794                                           gpointer      *data)
9795 {
9796   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
9797                                                 tagname, parser, data))
9798     return TRUE;
9799
9800   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
9801                                                       tagname, parser, data);
9802 }
9803
9804 static void
9805 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
9806                                         GtkBuilder   *builder,
9807                                         GObject      *child,
9808                                         const gchar  *tagname,
9809                                         gpointer     *data)
9810 {
9811   if (strcmp (tagname, "attributes") == 0)
9812     _gtk_cell_layout_buildable_custom_tag_end (buildable, builder, child, tagname,
9813                                                data);
9814   else
9815     parent_buildable_iface->custom_tag_end (buildable, builder, child, tagname,
9816                                             data);
9817 }
9818
9819
9820
9821 #define __GTK_ICON_VIEW_C__
9822 #include "gtkaliasdef.c"