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