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