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