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