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