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