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