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