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