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