]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
Deprecate all the public API that is using GdkColor struct
[~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 "gtkiconview.h"
25 #include "gtkiconviewprivate.h"
26
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 "gtkscrollable.h"
44 #include "gtksizerequest.h"
45 #include "gtktreednd.h"
46 #include "gtktypebuiltins.h"
47 #include "gtkprivate.h"
48 #include "a11y/gtkiconviewaccessible.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 _GtkIconViewChild GtkIconViewChild;
73 struct _GtkIconViewChild
74 {
75   GtkWidget    *widget;
76   GdkRectangle  area;
77 };
78
79 /* Signals */
80 enum
81 {
82   ITEM_ACTIVATED,
83   SELECTION_CHANGED,
84   SELECT_ALL,
85   UNSELECT_ALL,
86   SELECT_CURSOR_ITEM,
87   TOGGLE_CURSOR_ITEM,
88   MOVE_CURSOR,
89   ACTIVATE_CURSOR_ITEM,
90   LAST_SIGNAL
91 };
92
93 /* Properties */
94 enum
95 {
96   PROP_0,
97   PROP_PIXBUF_COLUMN,
98   PROP_TEXT_COLUMN,
99   PROP_MARKUP_COLUMN,
100   PROP_SELECTION_MODE,
101   PROP_ITEM_ORIENTATION,
102   PROP_MODEL,
103   PROP_COLUMNS,
104   PROP_ITEM_WIDTH,
105   PROP_SPACING,
106   PROP_ROW_SPACING,
107   PROP_COLUMN_SPACING,
108   PROP_MARGIN,
109   PROP_REORDERABLE,
110   PROP_TOOLTIP_COLUMN,
111   PROP_ITEM_PADDING,
112   PROP_CELL_AREA,
113
114   /* For scrollable interface */
115   PROP_HADJUSTMENT,
116   PROP_VADJUSTMENT,
117   PROP_HSCROLL_POLICY,
118   PROP_VSCROLL_POLICY
119 };
120
121 /* GObject vfuncs */
122 static void             gtk_icon_view_cell_layout_init          (GtkCellLayoutIface *iface);
123 static void             gtk_icon_view_dispose                   (GObject            *object);
124 static GObject         *gtk_icon_view_constructor               (GType               type,
125                                                                  guint               n_construct_properties,
126                                                                  GObjectConstructParam *construct_properties);
127 static void             gtk_icon_view_set_property              (GObject            *object,
128                                                                  guint               prop_id,
129                                                                  const GValue       *value,
130                                                                  GParamSpec         *pspec);
131 static void             gtk_icon_view_get_property              (GObject            *object,
132                                                                  guint               prop_id,
133                                                                  GValue             *value,
134                                                                  GParamSpec         *pspec);
135 /* GtkWidget vfuncs */
136 static void             gtk_icon_view_destroy                   (GtkWidget          *widget);
137 static void             gtk_icon_view_realize                   (GtkWidget          *widget);
138 static void             gtk_icon_view_unrealize                 (GtkWidget          *widget);
139 static void             gtk_icon_view_style_updated             (GtkWidget          *widget);
140 static void             gtk_icon_view_state_flags_changed       (GtkWidget          *widget,
141                                                                  GtkStateFlags       previous_state);
142 static void             gtk_icon_view_get_preferred_width       (GtkWidget          *widget,
143                                                                  gint               *minimum,
144                                                                  gint               *natural);
145 static void             gtk_icon_view_get_preferred_height      (GtkWidget          *widget,
146                                                                  gint               *minimum,
147                                                                  gint               *natural);
148 static void             gtk_icon_view_size_allocate             (GtkWidget          *widget,
149                                                                  GtkAllocation      *allocation);
150 static gboolean         gtk_icon_view_draw                      (GtkWidget          *widget,
151                                                                  cairo_t            *cr);
152 static gboolean         gtk_icon_view_motion                    (GtkWidget          *widget,
153                                                                  GdkEventMotion     *event);
154 static gboolean         gtk_icon_view_button_press              (GtkWidget          *widget,
155                                                                  GdkEventButton     *event);
156 static gboolean         gtk_icon_view_button_release            (GtkWidget          *widget,
157                                                                  GdkEventButton     *event);
158 static gboolean         gtk_icon_view_key_press                 (GtkWidget          *widget,
159                                                                  GdkEventKey        *event);
160 static gboolean         gtk_icon_view_key_release               (GtkWidget          *widget,
161                                                                  GdkEventKey        *event);
162
163
164 /* GtkContainer vfuncs */
165 static void             gtk_icon_view_remove                    (GtkContainer       *container,
166                                                                  GtkWidget          *widget);
167 static void             gtk_icon_view_forall                    (GtkContainer       *container,
168                                                                  gboolean            include_internals,
169                                                                  GtkCallback         callback,
170                                                                  gpointer            callback_data);
171
172 /* GtkIconView vfuncs */
173 static void             gtk_icon_view_real_select_all           (GtkIconView        *icon_view);
174 static void             gtk_icon_view_real_unselect_all         (GtkIconView        *icon_view);
175 static void             gtk_icon_view_real_select_cursor_item   (GtkIconView        *icon_view);
176 static void             gtk_icon_view_real_toggle_cursor_item   (GtkIconView        *icon_view);
177 static gboolean         gtk_icon_view_real_activate_cursor_item (GtkIconView        *icon_view);
178
179  /* Internal functions */
180 static void                 gtk_icon_view_set_hadjustment_values         (GtkIconView            *icon_view);
181 static void                 gtk_icon_view_set_vadjustment_values         (GtkIconView            *icon_view);
182 static void                 gtk_icon_view_set_hadjustment                (GtkIconView            *icon_view,
183                                                                           GtkAdjustment          *adjustment);
184 static void                 gtk_icon_view_set_vadjustment                (GtkIconView            *icon_view,
185                                                                           GtkAdjustment          *adjustment);
186 static void                 gtk_icon_view_adjustment_changed             (GtkAdjustment          *adjustment,
187                                                                           GtkIconView            *icon_view);
188 static void                 gtk_icon_view_layout                         (GtkIconView            *icon_view);
189 static void                 gtk_icon_view_paint_item                     (GtkIconView            *icon_view,
190                                                                           cairo_t                *cr,
191                                                                           GtkIconViewItem        *item,
192                                                                           gint                    x,
193                                                                           gint                    y,
194                                                                           gboolean                draw_focus);
195 static void                 gtk_icon_view_paint_rubberband               (GtkIconView            *icon_view,
196                                                                           cairo_t                *cr);
197 static void                 gtk_icon_view_queue_draw_path                (GtkIconView *icon_view,
198                                                                           GtkTreePath *path);
199 static void                 gtk_icon_view_queue_draw_item                (GtkIconView            *icon_view,
200                                                                           GtkIconViewItem        *item);
201 static void                 gtk_icon_view_queue_layout                   (GtkIconView            *icon_view);
202 static void                 gtk_icon_view_start_rubberbanding            (GtkIconView            *icon_view,
203                                                                           GdkDevice              *device,
204                                                                           gint                    x,
205                                                                           gint                    y);
206 static void                 gtk_icon_view_stop_rubberbanding             (GtkIconView            *icon_view);
207 static void                 gtk_icon_view_update_rubberband_selection    (GtkIconView            *icon_view);
208 static gboolean             gtk_icon_view_item_hit_test                  (GtkIconView            *icon_view,
209                                                                           GtkIconViewItem        *item,
210                                                                           gint                    x,
211                                                                           gint                    y,
212                                                                           gint                    width,
213                                                                           gint                    height);
214 static gboolean             gtk_icon_view_unselect_all_internal          (GtkIconView            *icon_view);
215 static void                 gtk_icon_view_cache_widths                   (GtkIconView            *icon_view);
216 static void                 gtk_icon_view_update_rubberband              (gpointer                data);
217 static void                 gtk_icon_view_item_invalidate_size           (GtkIconViewItem        *item);
218 static void                 gtk_icon_view_invalidate_sizes               (GtkIconView            *icon_view);
219 static void                 gtk_icon_view_add_move_binding               (GtkBindingSet          *binding_set,
220                                                                           guint                   keyval,
221                                                                           guint                   modmask,
222                                                                           GtkMovementStep         step,
223                                                                           gint                    count);
224 static gboolean             gtk_icon_view_real_move_cursor               (GtkIconView            *icon_view,
225                                                                           GtkMovementStep         step,
226                                                                           gint                    count);
227 static void                 gtk_icon_view_move_cursor_up_down            (GtkIconView            *icon_view,
228                                                                           gint                    count);
229 static void                 gtk_icon_view_move_cursor_page_up_down       (GtkIconView            *icon_view,
230                                                                           gint                    count);
231 static void                 gtk_icon_view_move_cursor_left_right         (GtkIconView            *icon_view,
232                                                                           gint                    count);
233 static void                 gtk_icon_view_move_cursor_start_end          (GtkIconView            *icon_view,
234                                                                           gint                    count);
235 static void                 gtk_icon_view_scroll_to_item                 (GtkIconView            *icon_view,
236                                                                           GtkIconViewItem        *item);
237 static gboolean             gtk_icon_view_select_all_between             (GtkIconView            *icon_view,
238                                                                           GtkIconViewItem        *anchor,
239                                                                           GtkIconViewItem        *cursor);
240
241 static void                 gtk_icon_view_ensure_cell_area               (GtkIconView            *icon_view,
242                                                                           GtkCellArea            *cell_area);
243
244 static GtkCellArea         *gtk_icon_view_cell_layout_get_area           (GtkCellLayout          *layout);
245
246 static void                 gtk_icon_view_item_selected_changed          (GtkIconView            *icon_view,
247                                                                           GtkIconViewItem        *item);
248
249 static void                 gtk_icon_view_add_editable                   (GtkCellArea            *area,
250                                                                           GtkCellRenderer        *renderer,
251                                                                           GtkCellEditable        *editable,
252                                                                           GdkRectangle           *cell_area,
253                                                                           const gchar            *path,
254                                                                           GtkIconView            *icon_view);
255 static void                 gtk_icon_view_remove_editable                (GtkCellArea            *area,
256                                                                           GtkCellRenderer        *renderer,
257                                                                           GtkCellEditable        *editable,
258                                                                           GtkIconView            *icon_view);
259 static void                 gtk_icon_view_context_changed                (GtkCellAreaContext     *context,
260                                                                           GParamSpec             *pspec,
261                                                                           GtkIconView            *icon_view);
262 static void                 update_text_cell                             (GtkIconView            *icon_view);
263 static void                 update_pixbuf_cell                           (GtkIconView            *icon_view);
264
265 /* Source side drag signals */
266 static void gtk_icon_view_drag_begin       (GtkWidget        *widget,
267                                             GdkDragContext   *context);
268 static void gtk_icon_view_drag_end         (GtkWidget        *widget,
269                                             GdkDragContext   *context);
270 static void gtk_icon_view_drag_data_get    (GtkWidget        *widget,
271                                             GdkDragContext   *context,
272                                             GtkSelectionData *selection_data,
273                                             guint             info,
274                                             guint             time);
275 static void gtk_icon_view_drag_data_delete (GtkWidget        *widget,
276                                             GdkDragContext   *context);
277
278 /* Target side drag signals */
279 static void     gtk_icon_view_drag_leave         (GtkWidget        *widget,
280                                                   GdkDragContext   *context,
281                                                   guint             time);
282 static gboolean gtk_icon_view_drag_motion        (GtkWidget        *widget,
283                                                   GdkDragContext   *context,
284                                                   gint              x,
285                                                   gint              y,
286                                                   guint             time);
287 static gboolean gtk_icon_view_drag_drop          (GtkWidget        *widget,
288                                                   GdkDragContext   *context,
289                                                   gint              x,
290                                                   gint              y,
291                                                   guint             time);
292 static void     gtk_icon_view_drag_data_received (GtkWidget        *widget,
293                                                   GdkDragContext   *context,
294                                                   gint              x,
295                                                   gint              y,
296                                                   GtkSelectionData *selection_data,
297                                                   guint             info,
298                                                   guint             time);
299 static gboolean gtk_icon_view_maybe_begin_drag   (GtkIconView             *icon_view,
300                                                   GdkEventMotion          *event);
301
302 static void     remove_scroll_timeout            (GtkIconView *icon_view);
303
304 /* GtkBuildable */
305 static GtkBuildableIface *parent_buildable_iface;
306 static void     gtk_icon_view_buildable_init             (GtkBuildableIface *iface);
307 static gboolean gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
308                                                           GtkBuilder    *builder,
309                                                           GObject       *child,
310                                                           const gchar   *tagname,
311                                                           GMarkupParser *parser,
312                                                           gpointer      *data);
313 static void     gtk_icon_view_buildable_custom_tag_end   (GtkBuildable  *buildable,
314                                                           GtkBuilder    *builder,
315                                                           GObject       *child,
316                                                           const gchar   *tagname,
317                                                           gpointer      *data);
318
319 static guint icon_view_signals[LAST_SIGNAL] = { 0 };
320
321 G_DEFINE_TYPE_WITH_CODE (GtkIconView, gtk_icon_view, GTK_TYPE_CONTAINER,
322                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
323                                                 gtk_icon_view_cell_layout_init)
324                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
325                                                 gtk_icon_view_buildable_init)
326                          G_IMPLEMENT_INTERFACE (GTK_TYPE_SCROLLABLE, NULL))
327
328 static void
329 gtk_icon_view_class_init (GtkIconViewClass *klass)
330 {
331   GObjectClass *gobject_class;
332   GtkWidgetClass *widget_class;
333   GtkContainerClass *container_class;
334   GtkBindingSet *binding_set;
335   
336   binding_set = gtk_binding_set_by_class (klass);
337
338   g_type_class_add_private (klass, sizeof (GtkIconViewPrivate));
339
340   gobject_class = (GObjectClass *) klass;
341   widget_class = (GtkWidgetClass *) klass;
342   container_class = (GtkContainerClass *) klass;
343
344   gobject_class->constructor = gtk_icon_view_constructor;
345   gobject_class->dispose = gtk_icon_view_dispose;
346   gobject_class->set_property = gtk_icon_view_set_property;
347   gobject_class->get_property = gtk_icon_view_get_property;
348
349   widget_class->destroy = gtk_icon_view_destroy;
350   widget_class->realize = gtk_icon_view_realize;
351   widget_class->unrealize = gtk_icon_view_unrealize;
352   widget_class->style_updated = gtk_icon_view_style_updated;
353   widget_class->get_preferred_width = gtk_icon_view_get_preferred_width;
354   widget_class->get_preferred_height = gtk_icon_view_get_preferred_height;
355   widget_class->size_allocate = gtk_icon_view_size_allocate;
356   widget_class->draw = gtk_icon_view_draw;
357   widget_class->motion_notify_event = gtk_icon_view_motion;
358   widget_class->button_press_event = gtk_icon_view_button_press;
359   widget_class->button_release_event = gtk_icon_view_button_release;
360   widget_class->key_press_event = gtk_icon_view_key_press;
361   widget_class->key_release_event = gtk_icon_view_key_release;
362   widget_class->drag_begin = gtk_icon_view_drag_begin;
363   widget_class->drag_end = gtk_icon_view_drag_end;
364   widget_class->drag_data_get = gtk_icon_view_drag_data_get;
365   widget_class->drag_data_delete = gtk_icon_view_drag_data_delete;
366   widget_class->drag_leave = gtk_icon_view_drag_leave;
367   widget_class->drag_motion = gtk_icon_view_drag_motion;
368   widget_class->drag_drop = gtk_icon_view_drag_drop;
369   widget_class->drag_data_received = gtk_icon_view_drag_data_received;
370   widget_class->state_flags_changed = gtk_icon_view_state_flags_changed;
371
372   container_class->remove = gtk_icon_view_remove;
373   container_class->forall = gtk_icon_view_forall;
374
375   klass->select_all = gtk_icon_view_real_select_all;
376   klass->unselect_all = gtk_icon_view_real_unselect_all;
377   klass->select_cursor_item = gtk_icon_view_real_select_cursor_item;
378   klass->toggle_cursor_item = gtk_icon_view_real_toggle_cursor_item;
379   klass->activate_cursor_item = gtk_icon_view_real_activate_cursor_item;  
380   klass->move_cursor = gtk_icon_view_real_move_cursor;
381   
382   /* Properties */
383   /**
384    * GtkIconView:selection-mode:
385    * 
386    * The ::selection-mode property specifies the selection mode of
387    * icon view. If the mode is #GTK_SELECTION_MULTIPLE, rubberband selection
388    * is enabled, for the other modes, only keyboard selection is possible.
389    *
390    * Since: 2.6
391    */
392   g_object_class_install_property (gobject_class,
393                                    PROP_SELECTION_MODE,
394                                    g_param_spec_enum ("selection-mode",
395                                                       P_("Selection mode"),
396                                                       P_("The selection mode"),
397                                                       GTK_TYPE_SELECTION_MODE,
398                                                       GTK_SELECTION_SINGLE,
399                                                       GTK_PARAM_READWRITE));
400
401   /**
402    * GtkIconView:pixbuf-column:
403    *
404    * The ::pixbuf-column property contains the number of the model column
405    * containing the pixbufs which are displayed. The pixbuf column must be 
406    * of type #GDK_TYPE_PIXBUF. Setting this property to -1 turns off the
407    * display of pixbufs.
408    *
409    * Since: 2.6
410    */
411   g_object_class_install_property (gobject_class,
412                                    PROP_PIXBUF_COLUMN,
413                                    g_param_spec_int ("pixbuf-column",
414                                                      P_("Pixbuf column"),
415                                                      P_("Model column used to retrieve the icon pixbuf from"),
416                                                      -1, G_MAXINT, -1,
417                                                      GTK_PARAM_READWRITE));
418
419   /**
420    * GtkIconView:text-column:
421    *
422    * The ::text-column property contains the number of the model column
423    * containing the texts which are displayed. The text column must be 
424    * of type #G_TYPE_STRING. If this property and the :markup-column 
425    * property are both set to -1, no texts are displayed.   
426    *
427    * Since: 2.6
428    */
429   g_object_class_install_property (gobject_class,
430                                    PROP_TEXT_COLUMN,
431                                    g_param_spec_int ("text-column",
432                                                      P_("Text column"),
433                                                      P_("Model column used to retrieve the text from"),
434                                                      -1, G_MAXINT, -1,
435                                                      GTK_PARAM_READWRITE));
436
437   
438   /**
439    * GtkIconView:markup-column:
440    *
441    * The ::markup-column property contains the number of the model column
442    * containing markup information to be displayed. The markup column must be 
443    * of type #G_TYPE_STRING. If this property and the :text-column property 
444    * are both set to column numbers, it overrides the text column.
445    * If both are set to -1, no texts are displayed.   
446    *
447    * Since: 2.6
448    */
449   g_object_class_install_property (gobject_class,
450                                    PROP_MARKUP_COLUMN,
451                                    g_param_spec_int ("markup-column",
452                                                      P_("Markup column"),
453                                                      P_("Model column used to retrieve the text if using Pango markup"),
454                                                      -1, G_MAXINT, -1,
455                                                      GTK_PARAM_READWRITE));
456   
457   g_object_class_install_property (gobject_class,
458                                    PROP_MODEL,
459                                    g_param_spec_object ("model",
460                                                         P_("Icon View Model"),
461                                                         P_("The model for the icon view"),
462                                                         GTK_TYPE_TREE_MODEL,
463                                                         GTK_PARAM_READWRITE));
464   
465   /**
466    * GtkIconView:columns:
467    *
468    * The columns property contains the number of the columns in which the
469    * items should be displayed. If it is -1, the number of columns will
470    * be chosen automatically to fill the available area.
471    *
472    * Since: 2.6
473    */
474   g_object_class_install_property (gobject_class,
475                                    PROP_COLUMNS,
476                                    g_param_spec_int ("columns",
477                                                      P_("Number of columns"),
478                                                      P_("Number of columns to display"),
479                                                      -1, G_MAXINT, -1,
480                                                      GTK_PARAM_READWRITE));
481   
482
483   /**
484    * GtkIconView:item-width:
485    *
486    * The item-width property specifies the width to use for each item. 
487    * If it is set to -1, the icon view will automatically determine a 
488    * suitable item size.
489    *
490    * Since: 2.6
491    */
492   g_object_class_install_property (gobject_class,
493                                    PROP_ITEM_WIDTH,
494                                    g_param_spec_int ("item-width",
495                                                      P_("Width for each item"),
496                                                      P_("The width used for each item"),
497                                                      -1, G_MAXINT, -1,
498                                                      GTK_PARAM_READWRITE));  
499
500   /**
501    * GtkIconView:spacing:
502    *
503    * The spacing property specifies the space which is inserted between
504    * the cells (i.e. the icon and the text) of an item.
505    *
506    * Since: 2.6
507    */
508   g_object_class_install_property (gobject_class,
509                                    PROP_SPACING,
510                                    g_param_spec_int ("spacing",
511                                                      P_("Spacing"),
512                                                      P_("Space which is inserted between cells of an item"),
513                                                      0, G_MAXINT, 0,
514                                                      GTK_PARAM_READWRITE));
515
516   /**
517    * GtkIconView:row-spacing:
518    *
519    * The row-spacing property specifies the space which is inserted between
520    * the rows of the icon view.
521    *
522    * Since: 2.6
523    */
524   g_object_class_install_property (gobject_class,
525                                    PROP_ROW_SPACING,
526                                    g_param_spec_int ("row-spacing",
527                                                      P_("Row Spacing"),
528                                                      P_("Space which is inserted between grid rows"),
529                                                      0, G_MAXINT, 6,
530                                                      GTK_PARAM_READWRITE));
531
532   /**
533    * GtkIconView:column-spacing:
534    *
535    * The column-spacing property specifies the space which is inserted between
536    * the columns of the icon view.
537    *
538    * Since: 2.6
539    */
540   g_object_class_install_property (gobject_class,
541                                    PROP_COLUMN_SPACING,
542                                    g_param_spec_int ("column-spacing",
543                                                      P_("Column Spacing"),
544                                                      P_("Space which is inserted between grid columns"),
545                                                      0, G_MAXINT, 6,
546                                                      GTK_PARAM_READWRITE));
547
548   /**
549    * GtkIconView:margin:
550    *
551    * The margin property specifies the space which is inserted 
552    * at the edges of the icon view.
553    *
554    * Since: 2.6
555    */
556   g_object_class_install_property (gobject_class,
557                                    PROP_MARGIN,
558                                    g_param_spec_int ("margin",
559                                                      P_("Margin"),
560                                                      P_("Space which is inserted at the edges of the icon view"),
561                                                      0, G_MAXINT, 6,
562                                                      GTK_PARAM_READWRITE));
563
564   /**
565    * GtkIconView:item-orientation:
566    *
567    * The item-orientation property specifies how the cells (i.e. the icon and
568    * the text) of the item are positioned relative to each other.
569    *
570    * Since: 2.6
571    */
572   g_object_class_install_property (gobject_class,
573                                    PROP_ITEM_ORIENTATION,
574                                    g_param_spec_enum ("item-orientation",
575                                                       P_("Item Orientation"),
576                                                       P_("How the text and icon of each item are positioned relative to each other"),
577                                                       GTK_TYPE_ORIENTATION,
578                                                       GTK_ORIENTATION_VERTICAL,
579                                                       GTK_PARAM_READWRITE));
580
581   /**
582    * GtkIconView:reorderable:
583    *
584    * The reorderable property specifies if the items can be reordered
585    * by DND.
586    *
587    * Since: 2.8
588    */
589   g_object_class_install_property (gobject_class,
590                                    PROP_REORDERABLE,
591                                    g_param_spec_boolean ("reorderable",
592                                                          P_("Reorderable"),
593                                                          P_("View is reorderable"),
594                                                          FALSE,
595                                                          G_PARAM_READWRITE));
596
597     g_object_class_install_property (gobject_class,
598                                      PROP_TOOLTIP_COLUMN,
599                                      g_param_spec_int ("tooltip-column",
600                                                        P_("Tooltip Column"),
601                                                        P_("The column in the model containing the tooltip texts for the items"),
602                                                        -1,
603                                                        G_MAXINT,
604                                                        -1,
605                                                        GTK_PARAM_READWRITE));
606
607   /**
608    * GtkIconView:item-padding:
609    *
610    * The item-padding property specifies the padding around each
611    * of the icon view's item.
612    *
613    * Since: 2.18
614    */
615   g_object_class_install_property (gobject_class,
616                                    PROP_ITEM_PADDING,
617                                    g_param_spec_int ("item-padding",
618                                                      P_("Item Padding"),
619                                                      P_("Padding around icon view items"),
620                                                      0, G_MAXINT, 6,
621                                                      GTK_PARAM_READWRITE));
622
623   /**
624    * GtkIconView:cell-area:
625    *
626    * The #GtkCellArea used to layout cell renderers for this view.
627    *
628    * If no area is specified when creating the icon view with gtk_icon_view_new_with_area() 
629    * a #GtkCellAreaBox will be used.
630    *
631    * Since: 3.0
632    */
633   g_object_class_install_property (gobject_class,
634                                    PROP_CELL_AREA,
635                                    g_param_spec_object ("cell-area",
636                                                         P_("Cell Area"),
637                                                         P_("The GtkCellArea used to layout cells"),
638                                                         GTK_TYPE_CELL_AREA,
639                                                         GTK_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
640
641   /* Scrollable interface properties */
642   g_object_class_override_property (gobject_class, PROP_HADJUSTMENT,    "hadjustment");
643   g_object_class_override_property (gobject_class, PROP_VADJUSTMENT,    "vadjustment");
644   g_object_class_override_property (gobject_class, PROP_HSCROLL_POLICY, "hscroll-policy");
645   g_object_class_override_property (gobject_class, PROP_VSCROLL_POLICY, "vscroll-policy");
646
647   /* Style properties */
648   gtk_widget_class_install_style_property (widget_class,
649                                            g_param_spec_boxed ("selection-box-color",
650                                                                P_("Selection Box Color"),
651                                                                P_("Color of the selection box"),
652                                                                GDK_TYPE_COLOR,
653                                                                GTK_PARAM_READABLE));
654
655   gtk_widget_class_install_style_property (widget_class,
656                                            g_param_spec_uchar ("selection-box-alpha",
657                                                                P_("Selection Box Alpha"),
658                                                                P_("Opacity of the selection box"),
659                                                                0, 0xff,
660                                                                0x40,
661                                                                GTK_PARAM_READABLE));
662
663   /* Signals */
664   /**
665    * GtkIconView::item-activated:
666    * @iconview: the object on which the signal is emitted
667    * @path: the #GtkTreePath for the activated item
668    *
669    * The ::item-activated signal is emitted when the method
670    * gtk_icon_view_item_activated() is called or the user double 
671    * clicks an item. It is also emitted when a non-editable item
672    * is selected and one of the keys: Space, Return or Enter is
673    * pressed.
674    */
675   icon_view_signals[ITEM_ACTIVATED] =
676     g_signal_new (I_("item-activated"),
677                   G_TYPE_FROM_CLASS (gobject_class),
678                   G_SIGNAL_RUN_LAST,
679                   G_STRUCT_OFFSET (GtkIconViewClass, item_activated),
680                   NULL, NULL,
681                   g_cclosure_marshal_VOID__BOXED,
682                   G_TYPE_NONE, 1,
683                   GTK_TYPE_TREE_PATH);
684
685   /**
686    * GtkIconView::selection-changed:
687    * @iconview: the object on which the signal is emitted
688    *
689    * The ::selection-changed signal is emitted when the selection
690    * (i.e. the set of selected items) changes.
691    */
692   icon_view_signals[SELECTION_CHANGED] =
693     g_signal_new (I_("selection-changed"),
694                   G_TYPE_FROM_CLASS (gobject_class),
695                   G_SIGNAL_RUN_FIRST,
696                   G_STRUCT_OFFSET (GtkIconViewClass, selection_changed),
697                   NULL, NULL,
698                   g_cclosure_marshal_VOID__VOID,
699                   G_TYPE_NONE, 0);
700   
701   /**
702    * GtkIconView::select-all:
703    * @iconview: the object on which the signal is emitted
704    *
705    * A <link linkend="keybinding-signals">keybinding signal</link>
706    * which gets emitted when the user selects all items.
707    *
708    * Applications should not connect to it, but may emit it with
709    * g_signal_emit_by_name() if they need to control selection
710    * programmatically.
711    * 
712    * The default binding for this signal is Ctrl-a.
713    */
714   icon_view_signals[SELECT_ALL] =
715     g_signal_new (I_("select-all"),
716                   G_TYPE_FROM_CLASS (gobject_class),
717                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
718                   G_STRUCT_OFFSET (GtkIconViewClass, select_all),
719                   NULL, NULL,
720                   g_cclosure_marshal_VOID__VOID,
721                   G_TYPE_NONE, 0);
722   
723   /**
724    * GtkIconView::unselect-all:
725    * @iconview: the object on which the signal is emitted
726    *
727    * A <link linkend="keybinding-signals">keybinding signal</link>
728    * which gets emitted when the user unselects all items.
729    *
730    * Applications should not connect to it, but may emit it with
731    * g_signal_emit_by_name() if they need to control selection
732    * programmatically.
733    * 
734    * The default binding for this signal is Ctrl-Shift-a. 
735    */
736   icon_view_signals[UNSELECT_ALL] =
737     g_signal_new (I_("unselect-all"),
738                   G_TYPE_FROM_CLASS (gobject_class),
739                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
740                   G_STRUCT_OFFSET (GtkIconViewClass, unselect_all),
741                   NULL, NULL,
742                   g_cclosure_marshal_VOID__VOID,
743                   G_TYPE_NONE, 0);
744
745   /**
746    * GtkIconView::select-cursor-item:
747    * @iconview: the object on which the signal is emitted
748    *
749    * A <link linkend="keybinding-signals">keybinding signal</link>
750    * which gets emitted when the user selects the item that is currently
751    * focused.
752    *
753    * Applications should not connect to it, but may emit it with
754    * g_signal_emit_by_name() if they need to control selection
755    * programmatically.
756    * 
757    * There is no default binding for this signal.
758    */
759   icon_view_signals[SELECT_CURSOR_ITEM] =
760     g_signal_new (I_("select-cursor-item"),
761                   G_TYPE_FROM_CLASS (gobject_class),
762                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
763                   G_STRUCT_OFFSET (GtkIconViewClass, select_cursor_item),
764                   NULL, NULL,
765                   g_cclosure_marshal_VOID__VOID,
766                   G_TYPE_NONE, 0);
767
768   /**
769    * GtkIconView::toggle-cursor-item:
770    * @iconview: the object on which the signal is emitted
771    *
772    * A <link linkend="keybinding-signals">keybinding signal</link>
773    * which gets emitted when the user toggles whether the currently
774    * focused item is selected or not. The exact effect of this 
775    * depend on the selection mode.
776    *
777    * Applications should not connect to it, but may emit it with
778    * g_signal_emit_by_name() if they need to control selection
779    * programmatically.
780    * 
781    * There is no default binding for this signal is Ctrl-Space.
782    */
783   icon_view_signals[TOGGLE_CURSOR_ITEM] =
784     g_signal_new (I_("toggle-cursor-item"),
785                   G_TYPE_FROM_CLASS (gobject_class),
786                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
787                   G_STRUCT_OFFSET (GtkIconViewClass, toggle_cursor_item),
788                   NULL, NULL,
789                   g_cclosure_marshal_VOID__VOID,
790                   G_TYPE_NONE, 0);
791
792   /**
793    * GtkIconView::activate-cursor-item:
794    * @iconview: the object on which the signal is emitted
795    *
796    * A <link linkend="keybinding-signals">keybinding signal</link>
797    * which gets emitted when the user activates the currently 
798    * focused item. 
799    *
800    * Applications should not connect to it, but may emit it with
801    * g_signal_emit_by_name() if they need to control activation
802    * programmatically.
803    * 
804    * The default bindings for this signal are Space, Return and Enter.
805    */
806   icon_view_signals[ACTIVATE_CURSOR_ITEM] =
807     g_signal_new (I_("activate-cursor-item"),
808                   G_TYPE_FROM_CLASS (gobject_class),
809                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
810                   G_STRUCT_OFFSET (GtkIconViewClass, activate_cursor_item),
811                   NULL, NULL,
812                   _gtk_marshal_BOOLEAN__VOID,
813                   G_TYPE_BOOLEAN, 0);
814   
815   /**
816    * GtkIconView::move-cursor:
817    * @iconview: the object which received the signal
818    * @step: the granularity of the move, as a #GtkMovementStep
819    * @count: the number of @step units to move
820    *
821    * The ::move-cursor signal is a
822    * <link linkend="keybinding-signals">keybinding signal</link>
823    * which gets emitted when the user initiates a cursor movement.
824    *
825    * Applications should not connect to it, but may emit it with
826    * g_signal_emit_by_name() if they need to control the cursor
827    * programmatically.
828    *
829    * The default bindings for this signal include
830    * <itemizedlist>
831    * <listitem>Arrow keys which move by individual steps</listitem>
832    * <listitem>Home/End keys which move to the first/last item</listitem>
833    * <listitem>PageUp/PageDown which move by "pages"</listitem>
834    * </itemizedlist>
835    *
836    * All of these will extend the selection when combined with
837    * the Shift modifier.
838    */
839   icon_view_signals[MOVE_CURSOR] =
840     g_signal_new (I_("move-cursor"),
841                   G_TYPE_FROM_CLASS (gobject_class),
842                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
843                   G_STRUCT_OFFSET (GtkIconViewClass, move_cursor),
844                   NULL, NULL,
845                   _gtk_marshal_BOOLEAN__ENUM_INT,
846                   G_TYPE_BOOLEAN, 2,
847                   GTK_TYPE_MOVEMENT_STEP,
848                   G_TYPE_INT);
849
850   /* Key bindings */
851   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK, 
852                                 "select-all", 0);
853   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK, 
854                                 "unselect-all", 0);
855   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK, 
856                                 "toggle-cursor-item", 0);
857   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
858                                 "toggle-cursor-item", 0);
859
860   gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, 0, 
861                                 "activate-cursor-item", 0);
862   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, 0,
863                                 "activate-cursor-item", 0);
864   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0, 
865                                 "activate-cursor-item", 0);
866   gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0, 
867                                 "activate-cursor-item", 0);
868   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0, 
869                                 "activate-cursor-item", 0);
870
871   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Up, 0,
872                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
873   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
874                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
875
876   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Down, 0,
877                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
878   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
879                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
880
881   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_p, GDK_CONTROL_MASK,
882                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
883
884   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_n, GDK_CONTROL_MASK,
885                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
886
887   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Home, 0,
888                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
889   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
890                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
891
892   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_End, 0,
893                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
894   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_End, 0,
895                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
896
897   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
898                                   GTK_MOVEMENT_PAGES, -1);
899   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
900                                   GTK_MOVEMENT_PAGES, -1);
901
902   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
903                                   GTK_MOVEMENT_PAGES, 1);
904   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
905                                   GTK_MOVEMENT_PAGES, 1);
906
907   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Right, 0, 
908                                   GTK_MOVEMENT_VISUAL_POSITIONS, 1);
909   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_Left, 0, 
910                                   GTK_MOVEMENT_VISUAL_POSITIONS, -1);
911
912   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Right, 0, 
913                                   GTK_MOVEMENT_VISUAL_POSITIONS, 1);
914   gtk_icon_view_add_move_binding (binding_set, GDK_KEY_KP_Left, 0, 
915                                   GTK_MOVEMENT_VISUAL_POSITIONS, -1);
916
917   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_ICON_VIEW_ACCESSIBLE);
918 }
919
920 static void
921 gtk_icon_view_buildable_init (GtkBuildableIface *iface)
922 {
923   parent_buildable_iface = g_type_interface_peek_parent (iface);
924   iface->add_child = _gtk_cell_layout_buildable_add_child;
925   iface->custom_tag_start = gtk_icon_view_buildable_custom_tag_start;
926   iface->custom_tag_end = gtk_icon_view_buildable_custom_tag_end;
927 }
928
929 static void
930 gtk_icon_view_cell_layout_init (GtkCellLayoutIface *iface)
931 {
932   iface->get_area = gtk_icon_view_cell_layout_get_area;
933 }
934
935 static void
936 gtk_icon_view_init (GtkIconView *icon_view)
937 {
938   icon_view->priv = G_TYPE_INSTANCE_GET_PRIVATE (icon_view,
939                                                  GTK_TYPE_ICON_VIEW,
940                                                  GtkIconViewPrivate);
941
942   icon_view->priv->width = 0;
943   icon_view->priv->height = 0;
944   icon_view->priv->selection_mode = GTK_SELECTION_SINGLE;
945   icon_view->priv->pressed_button = -1;
946   icon_view->priv->press_start_x = -1;
947   icon_view->priv->press_start_y = -1;
948   icon_view->priv->text_column = -1;
949   icon_view->priv->markup_column = -1;  
950   icon_view->priv->pixbuf_column = -1;
951   icon_view->priv->text_cell = NULL;
952   icon_view->priv->pixbuf_cell = NULL;  
953   icon_view->priv->tooltip_column = -1;  
954
955   gtk_widget_set_can_focus (GTK_WIDGET (icon_view), TRUE);
956
957   icon_view->priv->item_orientation = GTK_ORIENTATION_VERTICAL;
958
959   icon_view->priv->columns = -1;
960   icon_view->priv->item_width = -1;
961   icon_view->priv->spacing = 0;
962   icon_view->priv->row_spacing = 6;
963   icon_view->priv->column_spacing = 6;
964   icon_view->priv->margin = 6;
965   icon_view->priv->item_padding = 6;
966
967   icon_view->priv->draw_focus = TRUE;
968
969   icon_view->priv->row_contexts = 
970     g_ptr_array_new_with_free_func ((GDestroyNotify)g_object_unref);
971 }
972
973 /* GObject methods */
974 static GObject *
975 gtk_icon_view_constructor (GType               type,
976                            guint               n_construct_properties,
977                            GObjectConstructParam *construct_properties)
978 {
979   GtkIconView        *icon_view;
980   GObject            *object;
981
982   object = G_OBJECT_CLASS (gtk_icon_view_parent_class)->constructor
983     (type, n_construct_properties, construct_properties);
984
985   icon_view = (GtkIconView *) object;
986
987   gtk_icon_view_ensure_cell_area (icon_view, NULL);
988
989   return object;
990 }
991
992 static void
993 gtk_icon_view_dispose (GObject *object)
994 {
995   GtkIconView *icon_view;
996   GtkIconViewPrivate *priv;
997
998   icon_view = GTK_ICON_VIEW (object);
999   priv      = icon_view->priv;
1000
1001   if (priv->cell_area_context)
1002     {
1003       g_signal_handler_disconnect (priv->cell_area_context, priv->context_changed_id);
1004       priv->context_changed_id = 0;
1005
1006       g_object_unref (priv->cell_area_context);
1007       priv->cell_area_context = NULL;
1008     }
1009
1010   if (priv->row_contexts)
1011     {
1012       g_ptr_array_free (priv->row_contexts, TRUE);
1013       priv->row_contexts = NULL;
1014     }
1015
1016   if (priv->cell_area)
1017     {
1018       gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
1019
1020       g_signal_handler_disconnect (priv->cell_area, priv->add_editable_id);
1021       g_signal_handler_disconnect (priv->cell_area, priv->remove_editable_id);
1022       priv->add_editable_id = 0;
1023       priv->remove_editable_id = 0;
1024
1025       g_object_unref (priv->cell_area);
1026       priv->cell_area = NULL;
1027     }
1028
1029   G_OBJECT_CLASS (gtk_icon_view_parent_class)->dispose (object);
1030 }
1031
1032 static void
1033 gtk_icon_view_set_property (GObject      *object,
1034                             guint         prop_id,
1035                             const GValue *value,
1036                             GParamSpec   *pspec)
1037 {
1038   GtkIconView *icon_view;
1039   GtkCellArea *area;
1040
1041   icon_view = GTK_ICON_VIEW (object);
1042
1043   switch (prop_id)
1044     {
1045     case PROP_SELECTION_MODE:
1046       gtk_icon_view_set_selection_mode (icon_view, g_value_get_enum (value));
1047       break;
1048     case PROP_PIXBUF_COLUMN:
1049       gtk_icon_view_set_pixbuf_column (icon_view, g_value_get_int (value));
1050       break;
1051     case PROP_TEXT_COLUMN:
1052       gtk_icon_view_set_text_column (icon_view, g_value_get_int (value));
1053       break;
1054     case PROP_MARKUP_COLUMN:
1055       gtk_icon_view_set_markup_column (icon_view, g_value_get_int (value));
1056       break;
1057     case PROP_MODEL:
1058       gtk_icon_view_set_model (icon_view, g_value_get_object (value));
1059       break;
1060     case PROP_ITEM_ORIENTATION:
1061       gtk_icon_view_set_item_orientation (icon_view, g_value_get_enum (value));
1062       break;
1063     case PROP_COLUMNS:
1064       gtk_icon_view_set_columns (icon_view, g_value_get_int (value));
1065       break;
1066     case PROP_ITEM_WIDTH:
1067       gtk_icon_view_set_item_width (icon_view, g_value_get_int (value));
1068       break;
1069     case PROP_SPACING:
1070       gtk_icon_view_set_spacing (icon_view, g_value_get_int (value));
1071       break;
1072     case PROP_ROW_SPACING:
1073       gtk_icon_view_set_row_spacing (icon_view, g_value_get_int (value));
1074       break;
1075     case PROP_COLUMN_SPACING:
1076       gtk_icon_view_set_column_spacing (icon_view, g_value_get_int (value));
1077       break;
1078     case PROP_MARGIN:
1079       gtk_icon_view_set_margin (icon_view, g_value_get_int (value));
1080       break;
1081     case PROP_REORDERABLE:
1082       gtk_icon_view_set_reorderable (icon_view, g_value_get_boolean (value));
1083       break;
1084       
1085     case PROP_TOOLTIP_COLUMN:
1086       gtk_icon_view_set_tooltip_column (icon_view, g_value_get_int (value));
1087       break;
1088
1089     case PROP_ITEM_PADDING:
1090       gtk_icon_view_set_item_padding (icon_view, g_value_get_int (value));
1091       break;
1092
1093     case PROP_CELL_AREA:
1094       /* Construct-only, can only be assigned once */
1095       area = g_value_get_object (value);
1096       if (area)
1097         {
1098           if (icon_view->priv->cell_area != NULL)
1099             {
1100               g_warning ("cell-area has already been set, ignoring construct property");
1101               g_object_ref_sink (area);
1102               g_object_unref (area);
1103             }
1104           else
1105             gtk_icon_view_ensure_cell_area (icon_view, area);
1106         }
1107       break;
1108
1109     case PROP_HADJUSTMENT:
1110       gtk_icon_view_set_hadjustment (icon_view, g_value_get_object (value));
1111       break;
1112     case PROP_VADJUSTMENT:
1113       gtk_icon_view_set_vadjustment (icon_view, g_value_get_object (value));
1114       break;
1115     case PROP_HSCROLL_POLICY:
1116       icon_view->priv->hscroll_policy = g_value_get_enum (value);
1117       gtk_widget_queue_resize (GTK_WIDGET (icon_view));
1118       break;
1119     case PROP_VSCROLL_POLICY:
1120       icon_view->priv->vscroll_policy = g_value_get_enum (value);
1121       gtk_widget_queue_resize (GTK_WIDGET (icon_view));
1122       break;
1123
1124     default:
1125       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1126       break;
1127     }
1128 }
1129
1130 static void
1131 gtk_icon_view_get_property (GObject      *object,
1132                             guint         prop_id,
1133                             GValue       *value,
1134                             GParamSpec   *pspec)
1135 {
1136   GtkIconView *icon_view;
1137
1138   icon_view = GTK_ICON_VIEW (object);
1139
1140   switch (prop_id)
1141     {
1142     case PROP_SELECTION_MODE:
1143       g_value_set_enum (value, icon_view->priv->selection_mode);
1144       break;
1145     case PROP_PIXBUF_COLUMN:
1146       g_value_set_int (value, icon_view->priv->pixbuf_column);
1147       break;
1148     case PROP_TEXT_COLUMN:
1149       g_value_set_int (value, icon_view->priv->text_column);
1150       break;
1151     case PROP_MARKUP_COLUMN:
1152       g_value_set_int (value, icon_view->priv->markup_column);
1153       break;
1154     case PROP_MODEL:
1155       g_value_set_object (value, icon_view->priv->model);
1156       break;
1157     case PROP_ITEM_ORIENTATION:
1158       g_value_set_enum (value, icon_view->priv->item_orientation);
1159       break;
1160     case PROP_COLUMNS:
1161       g_value_set_int (value, icon_view->priv->columns);
1162       break;
1163     case PROP_ITEM_WIDTH:
1164       g_value_set_int (value, icon_view->priv->item_width);
1165       break;
1166     case PROP_SPACING:
1167       g_value_set_int (value, icon_view->priv->spacing);
1168       break;
1169     case PROP_ROW_SPACING:
1170       g_value_set_int (value, icon_view->priv->row_spacing);
1171       break;
1172     case PROP_COLUMN_SPACING:
1173       g_value_set_int (value, icon_view->priv->column_spacing);
1174       break;
1175     case PROP_MARGIN:
1176       g_value_set_int (value, icon_view->priv->margin);
1177       break;
1178     case PROP_REORDERABLE:
1179       g_value_set_boolean (value, icon_view->priv->reorderable);
1180       break;
1181     case PROP_TOOLTIP_COLUMN:
1182       g_value_set_int (value, icon_view->priv->tooltip_column);
1183       break;
1184
1185     case PROP_ITEM_PADDING:
1186       g_value_set_int (value, icon_view->priv->item_padding);
1187       break;
1188
1189     case PROP_CELL_AREA:
1190       g_value_set_object (value, icon_view->priv->cell_area);
1191       break;
1192
1193     case PROP_HADJUSTMENT:
1194       g_value_set_object (value, icon_view->priv->hadjustment);
1195       break;
1196     case PROP_VADJUSTMENT:
1197       g_value_set_object (value, icon_view->priv->vadjustment);
1198       break;
1199     case PROP_HSCROLL_POLICY:
1200       g_value_set_enum (value, icon_view->priv->hscroll_policy);
1201       break;
1202     case PROP_VSCROLL_POLICY:
1203       g_value_set_enum (value, icon_view->priv->vscroll_policy);
1204       break;
1205
1206     default:
1207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1208       break;
1209     }
1210 }
1211
1212 /* GtkWidget methods */
1213 static void
1214 gtk_icon_view_destroy (GtkWidget *widget)
1215 {
1216   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1217
1218   gtk_icon_view_set_model (icon_view, NULL);
1219
1220   if (icon_view->priv->layout_idle_id != 0)
1221     {
1222       g_source_remove (icon_view->priv->layout_idle_id);
1223       icon_view->priv->layout_idle_id = 0;
1224     }
1225
1226   if (icon_view->priv->scroll_to_path != NULL)
1227     {
1228       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1229       icon_view->priv->scroll_to_path = NULL;
1230     }
1231
1232   remove_scroll_timeout (icon_view);
1233
1234   if (icon_view->priv->hadjustment != NULL)
1235     {
1236       g_object_unref (icon_view->priv->hadjustment);
1237       icon_view->priv->hadjustment = NULL;
1238     }
1239
1240   if (icon_view->priv->vadjustment != NULL)
1241     {
1242       g_object_unref (icon_view->priv->vadjustment);
1243       icon_view->priv->vadjustment = NULL;
1244     }
1245
1246   GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->destroy (widget);
1247 }
1248
1249 static void
1250 gtk_icon_view_realize (GtkWidget *widget)
1251 {
1252   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1253   GtkAllocation allocation;
1254   GdkWindow *window;
1255   GdkWindowAttr attributes;
1256   gint attributes_mask;
1257   GtkStyleContext *context;
1258
1259   gtk_widget_set_realized (widget, TRUE);
1260
1261   gtk_widget_get_allocation (widget, &allocation);
1262
1263   /* Make the main, clipping window */
1264   attributes.window_type = GDK_WINDOW_CHILD;
1265   attributes.x = allocation.x;
1266   attributes.y = allocation.y;
1267   attributes.width = allocation.width;
1268   attributes.height = allocation.height;
1269   attributes.wclass = GDK_INPUT_OUTPUT;
1270   attributes.visual = gtk_widget_get_visual (widget);
1271   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1272
1273   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL;
1274
1275   window = gdk_window_new (gtk_widget_get_parent_window (widget),
1276                            &attributes, attributes_mask);
1277   gtk_widget_set_window (widget, window);
1278   gdk_window_set_user_data (window, widget);
1279
1280   gtk_widget_get_allocation (widget, &allocation);
1281
1282   /* Make the window for the icon view */
1283   attributes.x = 0;
1284   attributes.y = 0;
1285   attributes.width = MAX (icon_view->priv->width, allocation.width);
1286   attributes.height = MAX (icon_view->priv->height, allocation.height);
1287   attributes.event_mask = (GDK_EXPOSURE_MASK |
1288                            GDK_SCROLL_MASK |
1289                            GDK_POINTER_MOTION_MASK |
1290                            GDK_BUTTON_PRESS_MASK |
1291                            GDK_BUTTON_RELEASE_MASK |
1292                            GDK_KEY_PRESS_MASK |
1293                            GDK_KEY_RELEASE_MASK) |
1294     gtk_widget_get_events (widget);
1295   
1296   icon_view->priv->bin_window = gdk_window_new (window,
1297                                                 &attributes, attributes_mask);
1298   gdk_window_set_user_data (icon_view->priv->bin_window, widget);
1299
1300   context = gtk_widget_get_style_context (widget);
1301
1302   gtk_style_context_save (context);
1303   gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
1304   gtk_style_context_set_background (context, icon_view->priv->bin_window);
1305   gtk_style_context_restore (context);
1306
1307   gdk_window_show (icon_view->priv->bin_window);
1308 }
1309
1310 static void
1311 gtk_icon_view_unrealize (GtkWidget *widget)
1312 {
1313   GtkIconView *icon_view;
1314
1315   icon_view = GTK_ICON_VIEW (widget);
1316
1317   gdk_window_set_user_data (icon_view->priv->bin_window, NULL);
1318   gdk_window_destroy (icon_view->priv->bin_window);
1319   icon_view->priv->bin_window = NULL;
1320
1321   GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->unrealize (widget);
1322 }
1323
1324 static void
1325 _gtk_icon_view_update_background (GtkIconView *icon_view)
1326 {
1327   GtkWidget *widget = GTK_WIDGET (icon_view);
1328
1329   if (gtk_widget_get_realized (widget))
1330     {
1331       GtkStyleContext *context;
1332
1333       context = gtk_widget_get_style_context (widget);
1334
1335       gtk_style_context_save (context);
1336       gtk_style_context_add_class (context, GTK_STYLE_CLASS_VIEW);
1337
1338       gtk_style_context_set_background (context, gtk_widget_get_window (widget));
1339       gtk_style_context_set_background (context, icon_view->priv->bin_window);
1340
1341       gtk_style_context_restore (context);
1342     }
1343 }
1344
1345 static void
1346 gtk_icon_view_state_flags_changed (GtkWidget     *widget,
1347                                    GtkStateFlags  previous_state)
1348 {
1349   _gtk_icon_view_update_background (GTK_ICON_VIEW (widget));
1350   gtk_widget_queue_draw (widget);
1351 }
1352
1353 static void
1354 gtk_icon_view_style_updated (GtkWidget *widget)
1355 {
1356   GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->style_updated (widget);
1357
1358   _gtk_icon_view_update_background (GTK_ICON_VIEW (widget));
1359   gtk_widget_queue_resize (widget);
1360 }
1361
1362 static void
1363 gtk_icon_view_get_preferred_width (GtkWidget      *widget,
1364                                    gint           *minimum,
1365                                    gint           *natural)
1366 {
1367   *minimum = *natural = GTK_ICON_VIEW (widget)->priv->width;
1368 }
1369
1370 static void
1371 gtk_icon_view_get_preferred_height (GtkWidget      *widget,
1372                                    gint           *minimum,
1373                                    gint           *natural)
1374 {
1375   *minimum = *natural = GTK_ICON_VIEW (widget)->priv->height;
1376 }
1377
1378 static void
1379 gtk_icon_view_allocate_children (GtkIconView *icon_view)
1380 {
1381   GList *list;
1382
1383   for (list = icon_view->priv->children; list; list = list->next)
1384     {
1385       GtkIconViewChild *child = list->data;
1386
1387       /* totally ignore our child's requisition */
1388       gtk_widget_size_allocate (child->widget, &child->area);
1389     }
1390 }
1391
1392 static void
1393 gtk_icon_view_size_allocate (GtkWidget      *widget,
1394                              GtkAllocation  *allocation)
1395 {
1396   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1397
1398   gtk_widget_set_allocation (widget, allocation);
1399
1400   if (gtk_widget_get_realized (widget))
1401     {
1402       gdk_window_move_resize (gtk_widget_get_window (widget),
1403                               allocation->x, allocation->y,
1404                               allocation->width, allocation->height);
1405       gdk_window_resize (icon_view->priv->bin_window,
1406                          MAX (icon_view->priv->width, allocation->width),
1407                          MAX (icon_view->priv->height, allocation->height));
1408     }
1409
1410   gtk_icon_view_layout (icon_view);
1411   
1412   gtk_icon_view_allocate_children (icon_view);
1413
1414   /* Delay signal emission */
1415   g_object_freeze_notify (G_OBJECT (icon_view->priv->hadjustment));
1416   g_object_freeze_notify (G_OBJECT (icon_view->priv->vadjustment));
1417
1418   gtk_icon_view_set_hadjustment_values (icon_view);
1419   gtk_icon_view_set_vadjustment_values (icon_view);
1420
1421   if (gtk_widget_get_realized (widget) &&
1422       icon_view->priv->scroll_to_path)
1423     {
1424       GtkTreePath *path;
1425       path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
1426       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1427       icon_view->priv->scroll_to_path = NULL;
1428
1429       gtk_icon_view_scroll_to_path (icon_view, path,
1430                                     icon_view->priv->scroll_to_use_align,
1431                                     icon_view->priv->scroll_to_row_align,
1432                                     icon_view->priv->scroll_to_col_align);
1433       gtk_tree_path_free (path);
1434     }
1435
1436   /* Emit any pending signals now */
1437   g_object_thaw_notify (G_OBJECT (icon_view->priv->hadjustment));
1438   g_object_thaw_notify (G_OBJECT (icon_view->priv->vadjustment));
1439 }
1440
1441 static gboolean
1442 gtk_icon_view_draw (GtkWidget *widget,
1443                     cairo_t   *cr)
1444 {
1445   GtkIconView *icon_view;
1446   GList *icons;
1447   GtkTreePath *path;
1448   gint dest_index;
1449   GtkIconViewDropPosition dest_pos;
1450   GtkIconViewItem *dest_item = NULL;
1451
1452   icon_view = GTK_ICON_VIEW (widget);
1453
1454   if (!gtk_cairo_should_draw_window (cr, icon_view->priv->bin_window))
1455     return FALSE;
1456
1457   cairo_save (cr);
1458
1459   gtk_cairo_transform_to_window (cr, widget, icon_view->priv->bin_window);
1460
1461   cairo_set_line_width (cr, 1.);
1462
1463   gtk_icon_view_get_drag_dest_item (icon_view, &path, &dest_pos);
1464
1465   if (path)
1466     {
1467       dest_index = gtk_tree_path_get_indices (path)[0];
1468       gtk_tree_path_free (path);
1469     }
1470   else
1471     dest_index = -1;
1472
1473   for (icons = icon_view->priv->items; icons; icons = icons->next)
1474     {
1475       GtkIconViewItem *item = icons->data;
1476       GdkRectangle paint_area;
1477
1478       paint_area.x      = ((GdkRectangle *)item)->x      - icon_view->priv->item_padding;
1479       paint_area.y      = ((GdkRectangle *)item)->y      - icon_view->priv->item_padding;
1480       paint_area.width  = ((GdkRectangle *)item)->width  + icon_view->priv->item_padding * 2;
1481       paint_area.height = ((GdkRectangle *)item)->height + icon_view->priv->item_padding * 2;
1482       
1483       cairo_save (cr);
1484
1485       cairo_rectangle (cr, paint_area.x, paint_area.y, paint_area.width, paint_area.height);
1486       cairo_clip (cr);
1487
1488       if (gdk_cairo_get_clip_rectangle (cr, NULL))
1489         {
1490           gtk_icon_view_paint_item (icon_view, cr, item,
1491                                     ((GdkRectangle *)item)->x, ((GdkRectangle *)item)->y,
1492                                     icon_view->priv->draw_focus);
1493
1494           if (dest_index == item->index)
1495             dest_item = item;
1496         }
1497
1498       cairo_restore (cr);
1499     }
1500
1501   if (dest_item &&
1502       dest_pos != GTK_ICON_VIEW_NO_DROP)
1503     {
1504       GtkStyleContext *context;
1505       GdkRectangle rect = { 0 };
1506
1507       context = gtk_widget_get_style_context (widget);
1508
1509       switch (dest_pos)
1510         {
1511         case GTK_ICON_VIEW_DROP_INTO:
1512           rect = dest_item->cell_area;
1513           break;
1514         case GTK_ICON_VIEW_DROP_ABOVE:
1515           rect.x = dest_item->cell_area.x;
1516           rect.y = dest_item->cell_area.y - 1;
1517           rect.width = dest_item->cell_area.width;
1518           rect.height = 2;
1519           break;
1520         case GTK_ICON_VIEW_DROP_LEFT:
1521           rect.x = dest_item->cell_area.x - 1;
1522           rect.y = dest_item->cell_area.y;
1523           rect.width = 2;
1524           rect.height = dest_item->cell_area.height;
1525           break;
1526         case GTK_ICON_VIEW_DROP_BELOW:
1527           rect.x = dest_item->cell_area.x;
1528           rect.y = dest_item->cell_area.y + dest_item->cell_area.height - 1;
1529           rect.width = dest_item->cell_area.width;
1530           rect.height = 2;
1531           break;
1532         case GTK_ICON_VIEW_DROP_RIGHT:
1533           rect.x = dest_item->cell_area.x + dest_item->cell_area.width - 1;
1534           rect.y = dest_item->cell_area.y;
1535           rect.width = 2;
1536           rect.height = dest_item->cell_area.height;
1537         case GTK_ICON_VIEW_NO_DROP: ;
1538           break;
1539         }
1540
1541       gtk_render_focus (context, cr,
1542                         rect.x, rect.y,
1543                         rect.width, rect.height);
1544     }
1545
1546   if (icon_view->priv->doing_rubberband)
1547     gtk_icon_view_paint_rubberband (icon_view, cr);
1548
1549   cairo_restore (cr);
1550
1551   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->draw (widget, cr);
1552 }
1553
1554 static gboolean
1555 rubberband_scroll_timeout (gpointer data)
1556 {
1557   GtkIconView *icon_view = data;
1558
1559   gtk_adjustment_set_value (icon_view->priv->vadjustment,
1560                             gtk_adjustment_get_value (icon_view->priv->vadjustment) +
1561                             icon_view->priv->scroll_value_diff);
1562
1563   gtk_icon_view_update_rubberband (icon_view);
1564   
1565   return TRUE;
1566 }
1567
1568 static gboolean
1569 gtk_icon_view_motion (GtkWidget      *widget,
1570                       GdkEventMotion *event)
1571 {
1572   GtkAllocation allocation;
1573   GtkIconView *icon_view;
1574   gint abs_y;
1575   
1576   icon_view = GTK_ICON_VIEW (widget);
1577
1578   gtk_icon_view_maybe_begin_drag (icon_view, event);
1579
1580   if (icon_view->priv->doing_rubberband)
1581     {
1582       gtk_icon_view_update_rubberband (widget);
1583       
1584       abs_y = event->y - icon_view->priv->height *
1585         (gtk_adjustment_get_value (icon_view->priv->vadjustment) /
1586          (gtk_adjustment_get_upper (icon_view->priv->vadjustment) -
1587           gtk_adjustment_get_lower (icon_view->priv->vadjustment)));
1588
1589       gtk_widget_get_allocation (widget, &allocation);
1590
1591       if (abs_y < 0 || abs_y > allocation.height)
1592         {
1593           if (abs_y < 0)
1594             icon_view->priv->scroll_value_diff = abs_y;
1595           else
1596             icon_view->priv->scroll_value_diff = abs_y - allocation.height;
1597
1598           icon_view->priv->event_last_x = event->x;
1599           icon_view->priv->event_last_y = event->y;
1600
1601           if (icon_view->priv->scroll_timeout_id == 0)
1602             icon_view->priv->scroll_timeout_id = gdk_threads_add_timeout (30, rubberband_scroll_timeout, 
1603                                                                 icon_view);
1604         }
1605       else 
1606         remove_scroll_timeout (icon_view);
1607     }
1608   else
1609     {
1610       GtkIconViewItem *item, *last_prelight_item;
1611       GtkCellRenderer *cell = NULL;
1612
1613       last_prelight_item = icon_view->priv->last_prelight;
1614       item = _gtk_icon_view_get_item_at_coords (icon_view,
1615                                                event->x, event->y,
1616                                                FALSE,
1617                                                &cell);
1618
1619       if (item != NULL)
1620         {
1621           item->prelight = TRUE;
1622           gtk_icon_view_queue_draw_item (icon_view, item);
1623         }
1624
1625       if (last_prelight_item != NULL &&
1626           last_prelight_item != item)
1627         {
1628           last_prelight_item->prelight = FALSE;
1629           gtk_icon_view_queue_draw_item (icon_view,
1630                                          icon_view->priv->last_prelight);
1631         }
1632
1633       icon_view->priv->last_prelight = item;
1634     }
1635   
1636   return TRUE;
1637 }
1638
1639 static void
1640 gtk_icon_view_remove (GtkContainer *container,
1641                       GtkWidget    *widget)
1642 {
1643   GtkIconView *icon_view;
1644   GtkIconViewChild *child = NULL;
1645   GList *tmp_list;
1646
1647   icon_view = GTK_ICON_VIEW (container);
1648   
1649   tmp_list = icon_view->priv->children;
1650   while (tmp_list)
1651     {
1652       child = tmp_list->data;
1653       if (child->widget == widget)
1654         {
1655           gtk_widget_unparent (widget);
1656
1657           icon_view->priv->children = g_list_remove_link (icon_view->priv->children, tmp_list);
1658           g_list_free_1 (tmp_list);
1659           g_free (child);
1660           return;
1661         }
1662
1663       tmp_list = tmp_list->next;
1664     }
1665 }
1666
1667 static void
1668 gtk_icon_view_forall (GtkContainer *container,
1669                       gboolean      include_internals,
1670                       GtkCallback   callback,
1671                       gpointer      callback_data)
1672 {
1673   GtkIconView *icon_view;
1674   GtkIconViewChild *child = NULL;
1675   GList *tmp_list;
1676
1677   icon_view = GTK_ICON_VIEW (container);
1678
1679   tmp_list = icon_view->priv->children;
1680   while (tmp_list)
1681     {
1682       child = tmp_list->data;
1683       tmp_list = tmp_list->next;
1684
1685       (* callback) (child->widget, callback_data);
1686     }
1687 }
1688
1689 static void 
1690 gtk_icon_view_item_selected_changed (GtkIconView      *icon_view,
1691                                      GtkIconViewItem  *item)
1692 {
1693   AtkObject *obj;
1694   AtkObject *item_obj;
1695
1696   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
1697   if (obj != NULL)
1698     {
1699       item_obj = atk_object_ref_accessible_child (obj, item->index);
1700       if (item_obj != NULL)
1701         {
1702           atk_object_notify_state_change (item_obj, ATK_STATE_SELECTED, item->selected);
1703           g_object_unref (item_obj);
1704         }
1705     }
1706 }
1707
1708 static void
1709 gtk_icon_view_add_editable (GtkCellArea            *area,
1710                             GtkCellRenderer        *renderer,
1711                             GtkCellEditable        *editable,
1712                             GdkRectangle           *cell_area,
1713                             const gchar            *path,
1714                             GtkIconView            *icon_view)
1715 {
1716   GtkIconViewChild *child;
1717   GtkWidget *widget = GTK_WIDGET (editable);
1718   
1719   child = g_new (GtkIconViewChild, 1);
1720   
1721   child->widget      = widget;
1722   child->area.x      = cell_area->x;
1723   child->area.y      = cell_area->y;
1724   child->area.width  = cell_area->width;
1725   child->area.height = cell_area->height;
1726
1727   icon_view->priv->children = g_list_append (icon_view->priv->children, child);
1728
1729   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
1730     gtk_widget_set_parent_window (child->widget, icon_view->priv->bin_window);
1731   
1732   gtk_widget_set_parent (widget, GTK_WIDGET (icon_view));
1733 }
1734
1735 static void
1736 gtk_icon_view_remove_editable (GtkCellArea            *area,
1737                                GtkCellRenderer        *renderer,
1738                                GtkCellEditable        *editable,
1739                                GtkIconView            *icon_view)
1740 {
1741   GtkTreePath *path;
1742
1743   if (gtk_widget_has_focus (GTK_WIDGET (editable)))
1744     gtk_widget_grab_focus (GTK_WIDGET (icon_view));
1745   
1746   gtk_container_remove (GTK_CONTAINER (icon_view),
1747                         GTK_WIDGET (editable));  
1748
1749   path = gtk_tree_path_new_from_string (gtk_cell_area_get_current_path_string (area));
1750   gtk_icon_view_queue_draw_path (icon_view, path);
1751   gtk_tree_path_free (path);
1752 }
1753
1754 static void
1755 gtk_icon_view_context_changed (GtkCellAreaContext     *context,
1756                                GParamSpec             *pspec,
1757                                GtkIconView            *icon_view)
1758 {
1759   if (!strcmp (pspec->name, "minimum-width") ||
1760       !strcmp (pspec->name, "natural-width") ||
1761       !strcmp (pspec->name, "minimum-height") ||
1762       !strcmp (pspec->name, "natural-height"))
1763     gtk_icon_view_invalidate_sizes (icon_view);
1764 }
1765
1766 /**
1767  * gtk_icon_view_set_cursor:
1768  * @icon_view: A #GtkIconView
1769  * @path: A #GtkTreePath
1770  * @cell: (allow-none): One of the cell renderers of @icon_view, or %NULL
1771  * @start_editing: %TRUE if the specified cell should start being edited.
1772  *
1773  * Sets the current keyboard focus to be at @path, and selects it.  This is
1774  * useful when you want to focus the user's attention on a particular item.
1775  * If @cell is not %NULL, then focus is given to the cell specified by 
1776  * it. Additionally, if @start_editing is %TRUE, then editing should be 
1777  * started in the specified cell.  
1778  *
1779  * This function is often followed by <literal>gtk_widget_grab_focus 
1780  * (icon_view)</literal> in order to give keyboard focus to the widget.  
1781  * Please note that editing can only happen when the widget is realized.
1782  *
1783  * Since: 2.8
1784  **/
1785 void
1786 gtk_icon_view_set_cursor (GtkIconView     *icon_view,
1787                           GtkTreePath     *path,
1788                           GtkCellRenderer *cell,
1789                           gboolean         start_editing)
1790 {
1791   GtkIconViewItem *item = NULL;
1792
1793   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
1794   g_return_if_fail (path != NULL);
1795   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
1796
1797   if (icon_view->priv->cell_area)
1798     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
1799
1800   if (gtk_tree_path_get_depth (path) == 1)
1801     item = g_list_nth_data (icon_view->priv->items,
1802                             gtk_tree_path_get_indices(path)[0]);
1803   
1804   if (!item)
1805     return;
1806   
1807   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
1808   gtk_icon_view_scroll_to_path (icon_view, path, FALSE, 0.0, 0.0);
1809
1810   if (start_editing && 
1811       icon_view->priv->cell_area)
1812     {
1813       GtkCellAreaContext *context;
1814
1815       context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
1816       _gtk_icon_view_set_cell_data (icon_view, item);
1817       gtk_cell_area_activate (icon_view->priv->cell_area, context, 
1818                               GTK_WIDGET (icon_view), (GdkRectangle *)item, 
1819                               0 /* XXX flags */, TRUE);
1820     }
1821 }
1822
1823 /**
1824  * gtk_icon_view_get_cursor:
1825  * @icon_view: A #GtkIconView
1826  * @path: (out) (allow-none): Return location for the current cursor path,
1827  *        or %NULL
1828  * @cell: (out) (allow-none): Return location the current focus cell, or %NULL
1829  *
1830  * Fills in @path and @cell with the current cursor path and cell. 
1831  * If the cursor isn't currently set, then *@path will be %NULL.  
1832  * If no cell currently has focus, then *@cell will be %NULL.
1833  *
1834  * The returned #GtkTreePath must be freed with gtk_tree_path_free().
1835  *
1836  * Return value: %TRUE if the cursor is set.
1837  *
1838  * Since: 2.8
1839  **/
1840 gboolean
1841 gtk_icon_view_get_cursor (GtkIconView      *icon_view,
1842                           GtkTreePath     **path,
1843                           GtkCellRenderer **cell)
1844 {
1845   GtkIconViewItem *item;
1846
1847   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
1848
1849   item = icon_view->priv->cursor_item;
1850
1851   if (path != NULL)
1852     {
1853       if (item != NULL)
1854         *path = gtk_tree_path_new_from_indices (item->index, -1);
1855       else
1856         *path = NULL;
1857     }
1858
1859   if (cell != NULL && item != NULL && icon_view->priv->cell_area != NULL)
1860     *cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
1861
1862   return (item != NULL);
1863 }
1864
1865 static gboolean
1866 gtk_icon_view_button_press (GtkWidget      *widget,
1867                             GdkEventButton *event)
1868 {
1869   GtkIconView *icon_view;
1870   GtkIconViewItem *item;
1871   gboolean dirty = FALSE;
1872   GtkCellRenderer *cell = NULL, *cursor_cell = NULL;
1873
1874   icon_view = GTK_ICON_VIEW (widget);
1875
1876   if (event->window != icon_view->priv->bin_window)
1877     return FALSE;
1878
1879   if (!gtk_widget_has_focus (widget))
1880     gtk_widget_grab_focus (widget);
1881
1882   if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
1883     {
1884       GdkModifierType extend_mod_mask;
1885       GdkModifierType modify_mod_mask;
1886
1887       extend_mod_mask =
1888         gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_EXTEND_SELECTION);
1889
1890       modify_mod_mask =
1891         gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_MODIFY_SELECTION);
1892
1893       item = _gtk_icon_view_get_item_at_coords (icon_view, 
1894                                                event->x, event->y,
1895                                                FALSE,
1896                                                &cell);
1897
1898       /*
1899        * We consider only the the cells' area as the item area if the
1900        * item is not selected, but if it *is* selected, the complete
1901        * selection rectangle is considered to be part of the item.
1902        */
1903       if (item != NULL && (cell != NULL || item->selected))
1904         {
1905           if (cell != NULL)
1906             {
1907               if (gtk_cell_renderer_is_activatable (cell))
1908                 cursor_cell = cell;
1909             }
1910
1911           gtk_icon_view_scroll_to_item (icon_view, item);
1912           
1913           if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
1914             {
1915               _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
1916             }
1917           else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
1918                    (event->state & extend_mod_mask))
1919             {
1920               gtk_icon_view_unselect_all_internal (icon_view);
1921
1922               _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
1923               if (!icon_view->priv->anchor_item)
1924                 icon_view->priv->anchor_item = item;
1925               else 
1926                 gtk_icon_view_select_all_between (icon_view,
1927                                                   icon_view->priv->anchor_item,
1928                                                   item);
1929               dirty = TRUE;
1930             }
1931           else 
1932             {
1933               if ((icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE ||
1934                   ((icon_view->priv->selection_mode == GTK_SELECTION_SINGLE) && item->selected)) &&
1935                   (event->state & modify_mod_mask))
1936                 {
1937                   item->selected = !item->selected;
1938                   gtk_icon_view_queue_draw_item (icon_view, item);
1939                   dirty = TRUE;
1940                 }
1941               else
1942                 {
1943                   gtk_icon_view_unselect_all_internal (icon_view);
1944
1945                   item->selected = TRUE;
1946                   gtk_icon_view_queue_draw_item (icon_view, item);
1947                   dirty = TRUE;
1948                 }
1949               _gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
1950               icon_view->priv->anchor_item = item;
1951             }
1952
1953           /* Save press to possibly begin a drag */
1954           if (icon_view->priv->pressed_button < 0)
1955             {
1956               icon_view->priv->pressed_button = event->button;
1957               icon_view->priv->press_start_x = event->x;
1958               icon_view->priv->press_start_y = event->y;
1959             }
1960
1961           if (!icon_view->priv->last_single_clicked)
1962             icon_view->priv->last_single_clicked = item;
1963
1964           /* cancel the current editing, if it exists */
1965           gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
1966
1967           if (cell != NULL && gtk_cell_renderer_is_activatable (cell))
1968             {
1969               GtkCellAreaContext *context;
1970
1971               context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
1972
1973               _gtk_icon_view_set_cell_data (icon_view, item);
1974               gtk_cell_area_activate (icon_view->priv->cell_area, context,
1975                                       GTK_WIDGET (icon_view),
1976                                       (GdkRectangle *)item, 0/* XXX flags */, FALSE);
1977             }
1978         }
1979       else
1980         {
1981           if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
1982               !(event->state & modify_mod_mask))
1983             {
1984               dirty = gtk_icon_view_unselect_all_internal (icon_view);
1985             }
1986           
1987           if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
1988             gtk_icon_view_start_rubberbanding (icon_view, event->device, event->x, event->y);
1989         }
1990
1991       /* don't draw keyboard focus around an clicked-on item */
1992       icon_view->priv->draw_focus = FALSE;
1993     }
1994
1995   if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1996     {
1997       item = _gtk_icon_view_get_item_at_coords (icon_view,
1998                                                event->x, event->y,
1999                                                FALSE,
2000                                                NULL);
2001
2002       if (item && item == icon_view->priv->last_single_clicked)
2003         {
2004           GtkTreePath *path;
2005
2006           path = gtk_tree_path_new_from_indices (item->index, -1);
2007           gtk_icon_view_item_activated (icon_view, path);
2008           gtk_tree_path_free (path);
2009         }
2010
2011       icon_view->priv->last_single_clicked = NULL;
2012       icon_view->priv->pressed_button = -1;
2013     }
2014   
2015   if (dirty)
2016     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2017
2018   return event->button == 1;
2019 }
2020
2021 static gboolean
2022 gtk_icon_view_button_release (GtkWidget      *widget,
2023                               GdkEventButton *event)
2024 {
2025   GtkIconView *icon_view;
2026
2027   icon_view = GTK_ICON_VIEW (widget);
2028   
2029   if (icon_view->priv->pressed_button == event->button)
2030     icon_view->priv->pressed_button = -1;
2031
2032   gtk_icon_view_stop_rubberbanding (icon_view);
2033
2034   remove_scroll_timeout (icon_view);
2035
2036   return TRUE;
2037 }
2038
2039 static gboolean
2040 gtk_icon_view_key_press (GtkWidget      *widget,
2041                          GdkEventKey    *event)
2042 {
2043   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2044
2045   if (icon_view->priv->doing_rubberband)
2046     {
2047       if (event->keyval == GDK_KEY_Escape)
2048         gtk_icon_view_stop_rubberbanding (icon_view);
2049
2050       return TRUE;
2051     }
2052
2053   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2054 }
2055
2056 static gboolean
2057 gtk_icon_view_key_release (GtkWidget      *widget,
2058                            GdkEventKey    *event)
2059 {
2060   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
2061
2062   if (icon_view->priv->doing_rubberband)
2063     return TRUE;
2064
2065   return GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->key_press_event (widget, event);
2066 }
2067
2068 static void
2069 gtk_icon_view_update_rubberband (gpointer data)
2070 {
2071   GtkIconView *icon_view;
2072   gint x, y;
2073   GdkRectangle old_area;
2074   GdkRectangle new_area;
2075   GdkRectangle common;
2076   cairo_region_t *invalid_region;
2077   
2078   icon_view = GTK_ICON_VIEW (data);
2079
2080   gdk_window_get_device_position (icon_view->priv->bin_window,
2081                                   icon_view->priv->rubberband_device,
2082                                   &x, &y, NULL);
2083
2084   x = MAX (x, 0);
2085   y = MAX (y, 0);
2086
2087   old_area.x = MIN (icon_view->priv->rubberband_x1,
2088                     icon_view->priv->rubberband_x2);
2089   old_area.y = MIN (icon_view->priv->rubberband_y1,
2090                     icon_view->priv->rubberband_y2);
2091   old_area.width = ABS (icon_view->priv->rubberband_x2 -
2092                         icon_view->priv->rubberband_x1) + 1;
2093   old_area.height = ABS (icon_view->priv->rubberband_y2 -
2094                          icon_view->priv->rubberband_y1) + 1;
2095   
2096   new_area.x = MIN (icon_view->priv->rubberband_x1, x);
2097   new_area.y = MIN (icon_view->priv->rubberband_y1, y);
2098   new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
2099   new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
2100
2101   invalid_region = cairo_region_create_rectangle (&old_area);
2102   cairo_region_union_rectangle (invalid_region, &new_area);
2103
2104   gdk_rectangle_intersect (&old_area, &new_area, &common);
2105   if (common.width > 2 && common.height > 2)
2106     {
2107       cairo_region_t *common_region;
2108
2109       /* make sure the border is invalidated */
2110       common.x += 1;
2111       common.y += 1;
2112       common.width -= 2;
2113       common.height -= 2;
2114       
2115       common_region = cairo_region_create_rectangle (&common);
2116
2117       cairo_region_subtract (invalid_region, common_region);
2118       cairo_region_destroy (common_region);
2119     }
2120   
2121   gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
2122     
2123   cairo_region_destroy (invalid_region);
2124
2125   icon_view->priv->rubberband_x2 = x;
2126   icon_view->priv->rubberband_y2 = y;  
2127
2128   gtk_icon_view_update_rubberband_selection (icon_view);
2129 }
2130
2131 static void
2132 gtk_icon_view_start_rubberbanding (GtkIconView  *icon_view,
2133                                    GdkDevice    *device,
2134                                    gint          x,
2135                                    gint          y)
2136 {
2137   GList *items;
2138
2139   if (icon_view->priv->rubberband_device)
2140     return;
2141
2142   for (items = icon_view->priv->items; items; items = items->next)
2143     {
2144       GtkIconViewItem *item = items->data;
2145
2146       item->selected_before_rubberbanding = item->selected;
2147     }
2148   
2149   icon_view->priv->rubberband_x1 = x;
2150   icon_view->priv->rubberband_y1 = y;
2151   icon_view->priv->rubberband_x2 = x;
2152   icon_view->priv->rubberband_y2 = y;
2153
2154   icon_view->priv->doing_rubberband = TRUE;
2155   icon_view->priv->rubberband_device = device;
2156
2157   gtk_device_grab_add (GTK_WIDGET (icon_view), device, TRUE);
2158 }
2159
2160 static void
2161 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
2162 {
2163   if (!icon_view->priv->doing_rubberband)
2164     return;
2165
2166   gtk_device_grab_remove (GTK_WIDGET (icon_view),
2167                           icon_view->priv->rubberband_device);
2168
2169   icon_view->priv->doing_rubberband = FALSE;
2170   icon_view->priv->rubberband_device = NULL;
2171
2172   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2173 }
2174
2175 static void
2176 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
2177 {
2178   GList *items;
2179   gint x, y, width, height;
2180   gboolean dirty = FALSE;
2181   
2182   x = MIN (icon_view->priv->rubberband_x1,
2183            icon_view->priv->rubberband_x2);
2184   y = MIN (icon_view->priv->rubberband_y1,
2185            icon_view->priv->rubberband_y2);
2186   width = ABS (icon_view->priv->rubberband_x1 - 
2187                icon_view->priv->rubberband_x2);
2188   height = ABS (icon_view->priv->rubberband_y1 - 
2189                 icon_view->priv->rubberband_y2);
2190   
2191   for (items = icon_view->priv->items; items; items = items->next)
2192     {
2193       GtkIconViewItem *item = items->data;
2194       gboolean is_in;
2195       gboolean selected;
2196       
2197       is_in = gtk_icon_view_item_hit_test (icon_view, item, 
2198                                            x, y, width, height);
2199
2200       selected = is_in ^ item->selected_before_rubberbanding;
2201
2202       if (item->selected != selected)
2203         {
2204           item->selected = selected;
2205           dirty = TRUE;
2206           gtk_icon_view_queue_draw_item (icon_view, item);
2207         }
2208     }
2209
2210   if (dirty)
2211     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2212 }
2213
2214
2215 typedef struct {
2216   GdkRectangle hit_rect;
2217   gboolean     hit;
2218 } HitTestData;
2219
2220 static gboolean 
2221 hit_test (GtkCellRenderer    *renderer,
2222           const GdkRectangle *cell_area,
2223           const GdkRectangle *cell_background,
2224           HitTestData        *data)
2225 {
2226   if (MIN (data->hit_rect.x + data->hit_rect.width, cell_area->x + cell_area->width) - 
2227       MAX (data->hit_rect.x, cell_area->x) > 0 &&
2228       MIN (data->hit_rect.y + data->hit_rect.height, cell_area->y + cell_area->height) - 
2229       MAX (data->hit_rect.y, cell_area->y) > 0)
2230     data->hit = TRUE;
2231   
2232   return (data->hit != FALSE);
2233 }
2234
2235 static gboolean
2236 gtk_icon_view_item_hit_test (GtkIconView      *icon_view,
2237                              GtkIconViewItem  *item,
2238                              gint              x,
2239                              gint              y,
2240                              gint              width,
2241                              gint              height)
2242 {
2243   HitTestData data = { { x, y, width, height }, FALSE };
2244   GtkCellAreaContext *context;
2245   GdkRectangle *item_area = (GdkRectangle *)item;
2246    
2247   if (MIN (x + width, item_area->x + item_area->width) - MAX (x, item_area->x) <= 0 ||
2248       MIN (y + height, item_area->y + item_area->height) - MAX (y, item_area->y) <= 0)
2249     return FALSE;
2250
2251   context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
2252
2253   _gtk_icon_view_set_cell_data (icon_view, item);
2254   gtk_cell_area_foreach_alloc (icon_view->priv->cell_area, context,
2255                                GTK_WIDGET (icon_view),
2256                                item_area, item_area,
2257                                (GtkCellAllocCallback)hit_test, &data);
2258
2259   return data.hit;
2260 }
2261
2262 static gboolean
2263 gtk_icon_view_unselect_all_internal (GtkIconView  *icon_view)
2264 {
2265   gboolean dirty = FALSE;
2266   GList *items;
2267
2268   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2269     return FALSE;
2270
2271   for (items = icon_view->priv->items; items; items = items->next)
2272     {
2273       GtkIconViewItem *item = items->data;
2274
2275       if (item->selected)
2276         {
2277           item->selected = FALSE;
2278           dirty = TRUE;
2279           gtk_icon_view_queue_draw_item (icon_view, item);
2280           gtk_icon_view_item_selected_changed (icon_view, item);
2281         }
2282     }
2283
2284   return dirty;
2285 }
2286
2287
2288 /* GtkIconView signals */
2289 static void
2290 gtk_icon_view_real_select_all (GtkIconView *icon_view)
2291 {
2292   gtk_icon_view_select_all (icon_view);
2293 }
2294
2295 static void
2296 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
2297 {
2298   gtk_icon_view_unselect_all (icon_view);
2299 }
2300
2301 static void
2302 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
2303 {
2304   gtk_icon_view_unselect_all (icon_view);
2305
2306   if (icon_view->priv->cursor_item != NULL)
2307     _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2308 }
2309
2310 static gboolean
2311 gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view)
2312 {
2313   GtkTreePath *path;
2314   GtkCellAreaContext *context;
2315
2316   if (!icon_view->priv->cursor_item)
2317     return FALSE;
2318
2319   context = g_ptr_array_index (icon_view->priv->row_contexts, icon_view->priv->cursor_item->row);
2320
2321   _gtk_icon_view_set_cell_data (icon_view, icon_view->priv->cursor_item);
2322   gtk_cell_area_activate (icon_view->priv->cell_area, context,
2323                           GTK_WIDGET (icon_view),
2324                           (GdkRectangle *)icon_view->priv->cursor_item,
2325                           0 /* XXX flags */,
2326                           FALSE);
2327
2328   path = gtk_tree_path_new_from_indices (icon_view->priv->cursor_item->index, -1);
2329   gtk_icon_view_item_activated (icon_view, path);
2330   gtk_tree_path_free (path);
2331
2332   return TRUE;
2333 }
2334
2335 static void
2336 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
2337 {
2338   if (!icon_view->priv->cursor_item)
2339     return;
2340
2341   switch (icon_view->priv->selection_mode)
2342     {
2343     case GTK_SELECTION_NONE:
2344       break;
2345     case GTK_SELECTION_BROWSE:
2346       _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2347       break;
2348     case GTK_SELECTION_SINGLE:
2349       if (icon_view->priv->cursor_item->selected)
2350         _gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
2351       else
2352         _gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2353       break;
2354     case GTK_SELECTION_MULTIPLE:
2355       icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
2356       g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0); 
2357       
2358       gtk_icon_view_item_selected_changed (icon_view, icon_view->priv->cursor_item);      
2359       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2360       break;
2361     }
2362 }
2363
2364 /* Internal functions */
2365 static void
2366 gtk_icon_view_process_updates (GtkIconView *icon_view)
2367 {
2368   /* Prior to drawing, we check if a layout has been scheduled.  If so,
2369    * do it now that all cell view items have valid sizes before we proceeed
2370    * (and resize the bin_window if required).
2371    */
2372   if (icon_view->priv->layout_idle_id != 0)
2373     gtk_icon_view_layout (icon_view);
2374
2375   gdk_window_process_updates (icon_view->priv->bin_window, TRUE);
2376 }
2377
2378 static void
2379 gtk_icon_view_set_hadjustment_values (GtkIconView *icon_view)
2380 {
2381   GtkAllocation  allocation;
2382   GtkAdjustment *adj = icon_view->priv->hadjustment;
2383   gdouble old_page_size;
2384   gdouble old_upper;
2385   gdouble old_value;
2386   gdouble new_value;
2387   gdouble new_upper;
2388
2389   gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2390
2391   old_value = gtk_adjustment_get_value (adj);
2392   old_upper = gtk_adjustment_get_upper (adj);
2393   old_page_size = gtk_adjustment_get_page_size (adj);
2394   new_upper = MAX (allocation.width, icon_view->priv->width);
2395
2396   if (gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL)
2397     {
2398       /* Make sure no scrolling occurs for RTL locales also (if possible) */
2399       /* Quick explanation:
2400        *   In LTR locales, leftmost portion of visible rectangle should stay
2401        *   fixed, which means left edge of scrollbar thumb should remain fixed
2402        *   and thus adjustment's value should stay the same.
2403        *
2404        *   In RTL locales, we want to keep rightmost portion of visible
2405        *   rectangle fixed. This means right edge of thumb should remain fixed.
2406        *   In this case, upper - value - page_size should remain constant.
2407        */
2408       new_value = (new_upper - allocation.width) -
2409                   (old_upper - old_value - old_page_size);
2410       new_value = CLAMP (new_value, 0, new_upper - allocation.width);
2411     }
2412   else
2413     new_value = CLAMP (old_value, 0, new_upper - allocation.width);
2414
2415   gtk_adjustment_configure (adj,
2416                             new_value,
2417                             0.0,
2418                             new_upper,
2419                             allocation.width * 0.1,
2420                             allocation.width * 0.9,
2421                             allocation.width);
2422 }
2423
2424 static void
2425 gtk_icon_view_set_vadjustment_values (GtkIconView *icon_view)
2426 {
2427   GtkAllocation  allocation;
2428   GtkAdjustment *adj = icon_view->priv->vadjustment;
2429
2430   gtk_widget_get_allocation (GTK_WIDGET (icon_view), &allocation);
2431
2432   gtk_adjustment_configure (adj,
2433                             gtk_adjustment_get_value (adj),
2434                             0.0,
2435                             MAX (allocation.height, icon_view->priv->height),
2436                             allocation.height * 0.1,
2437                             allocation.height * 0.9,
2438                             allocation.height);
2439 }
2440
2441 static void
2442 gtk_icon_view_set_hadjustment (GtkIconView   *icon_view,
2443                                GtkAdjustment *adjustment)
2444 {
2445   GtkIconViewPrivate *priv = icon_view->priv;
2446
2447   if (adjustment && priv->hadjustment == adjustment)
2448     return;
2449
2450   if (priv->hadjustment != NULL)
2451     {
2452       g_signal_handlers_disconnect_matched (priv->hadjustment,
2453                                             G_SIGNAL_MATCH_DATA,
2454                                             0, 0, NULL, NULL, icon_view);
2455       g_object_unref (priv->hadjustment);
2456     }
2457
2458   if (!adjustment)
2459     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2460                                      0.0, 0.0, 0.0);
2461
2462   g_signal_connect (adjustment, "value-changed",
2463                     G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2464   priv->hadjustment = g_object_ref_sink (adjustment);
2465   gtk_icon_view_set_hadjustment_values (icon_view);
2466
2467   g_object_notify (G_OBJECT (icon_view), "hadjustment");
2468 }
2469
2470 static void
2471 gtk_icon_view_set_vadjustment (GtkIconView   *icon_view,
2472                                GtkAdjustment *adjustment)
2473 {
2474   GtkIconViewPrivate *priv = icon_view->priv;
2475
2476   if (adjustment && priv->vadjustment == adjustment)
2477     return;
2478
2479   if (priv->vadjustment != NULL)
2480     {
2481       g_signal_handlers_disconnect_matched (priv->vadjustment,
2482                                             G_SIGNAL_MATCH_DATA,
2483                                             0, 0, NULL, NULL, icon_view);
2484       g_object_unref (priv->vadjustment);
2485     }
2486
2487   if (!adjustment)
2488     adjustment = gtk_adjustment_new (0.0, 0.0, 0.0,
2489                                      0.0, 0.0, 0.0);
2490
2491   g_signal_connect (adjustment, "value-changed",
2492                     G_CALLBACK (gtk_icon_view_adjustment_changed), icon_view);
2493   priv->vadjustment = g_object_ref_sink (adjustment);
2494   gtk_icon_view_set_vadjustment_values (icon_view);
2495
2496   g_object_notify (G_OBJECT (icon_view), "vadjustment");
2497 }
2498
2499 static void
2500 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
2501                                   GtkIconView   *icon_view)
2502 {
2503   GtkIconViewPrivate *priv = icon_view->priv;
2504
2505   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2506     {
2507       gdk_window_move (priv->bin_window,
2508                        - gtk_adjustment_get_value (priv->hadjustment),
2509                        - gtk_adjustment_get_value (priv->vadjustment));
2510
2511       if (icon_view->priv->doing_rubberband)
2512         gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
2513       
2514       _gtk_icon_view_accessible_adjustment_changed (icon_view);
2515
2516       gtk_icon_view_process_updates (icon_view);
2517     }
2518 }
2519
2520 static GList *
2521 gtk_icon_view_layout_single_row (GtkIconView *icon_view, 
2522                                  GList       *first_item, 
2523                                  gint         item_width,
2524                                  gint         row,
2525                                  gint        *y, 
2526                                  gint        *maximum_width)
2527 {
2528   GtkAllocation allocation;
2529   GtkCellAreaContext *context;
2530   GtkIconViewPrivate *priv = icon_view->priv;
2531   GtkWidget *widget = GTK_WIDGET (icon_view);
2532   gint x, current_width;
2533   GList *items, *last_item;
2534   gint col;
2535   gint max_height = 0;
2536   gboolean rtl;
2537
2538   rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
2539
2540   x = 0;
2541   col = 0;
2542   items = first_item;
2543   current_width = 0;
2544
2545   x += priv->margin;
2546   current_width += 2 * priv->margin;
2547
2548   gtk_widget_get_allocation (widget, &allocation);
2549
2550   context = gtk_cell_area_copy_context (priv->cell_area, priv->cell_area_context);
2551   g_ptr_array_add (priv->row_contexts, context);
2552
2553   /* In the first loop we iterate horizontally until we hit allocation width
2554    * and collect the aligned height-for-width */
2555   items = first_item;
2556   while (items)
2557     {
2558       GtkIconViewItem *item = items->data;
2559       GdkRectangle    *item_area = (GdkRectangle *)item;
2560
2561       item_area->width = item_width;
2562
2563       current_width += item_area->width + icon_view->priv->item_padding * 2;
2564
2565       if (items != first_item)
2566         {
2567           if ((icon_view->priv->columns <= 0 && current_width > allocation.width) ||
2568               (icon_view->priv->columns > 0 && col >= icon_view->priv->columns))
2569             break;
2570         }
2571
2572       /* Get this item's particular width & height (all alignments are cached by now) */
2573       _gtk_icon_view_set_cell_data (icon_view, item);
2574       gtk_cell_area_get_preferred_height_for_width (priv->cell_area,
2575                                                     context,
2576                                                     widget, item_width, 
2577                                                     NULL, NULL);
2578
2579       current_width += icon_view->priv->column_spacing;
2580
2581       item_area->y = *y + icon_view->priv->item_padding;
2582       item_area->x = x  + icon_view->priv->item_padding;
2583
2584       x = current_width - icon_view->priv->margin; 
2585               
2586       if (current_width > *maximum_width)
2587         *maximum_width = current_width;
2588
2589       item->row = row;
2590       item->col = col;
2591
2592       col ++;
2593       items = items->next;
2594     }
2595
2596   last_item = items;
2597
2598   gtk_cell_area_context_get_preferred_height_for_width (context, item_width, &max_height, NULL);
2599   gtk_cell_area_context_allocate (context, item_width, max_height);
2600
2601   /* In the second loop the item height has been aligned and derived and
2602    * we just set the height and handle rtl layout */
2603   for (items = first_item; items != last_item; items = items->next)
2604     {
2605       GtkIconViewItem *item = items->data;
2606       GdkRectangle    *item_area = (GdkRectangle *)item;
2607
2608       if (rtl)
2609         {
2610           item_area->x = *maximum_width - item_area->width - item_area->x;
2611           item->col = col - 1 - item->col;
2612         }
2613
2614       /* All items in the same row get the same height */
2615       item_area->height = max_height;
2616     }
2617
2618   /* Adjust the new y coordinate. */
2619   *y += max_height + icon_view->priv->row_spacing + icon_view->priv->item_padding * 2;
2620   
2621   return last_item;
2622 }
2623
2624 static void
2625 adjust_wrap_width (GtkIconView *icon_view)
2626 {
2627   if (icon_view->priv->text_cell)
2628     {
2629       gint wrap_width = 50;
2630
2631       /* Here we go with the same old guess, try the icon size and set double
2632        * the size of the first icon found in the list, naive but works much
2633        * of the time */
2634       if (icon_view->priv->items && icon_view->priv->pixbuf_cell)
2635         {
2636           _gtk_icon_view_set_cell_data (icon_view, icon_view->priv->items->data);
2637           gtk_cell_renderer_get_preferred_width (icon_view->priv->pixbuf_cell,
2638                                                  GTK_WIDGET (icon_view),
2639                                                  &wrap_width, NULL);
2640           
2641           wrap_width = MAX (wrap_width * 2, 50);
2642         }
2643       
2644       g_object_set (icon_view->priv->text_cell, "wrap-width", wrap_width, NULL);
2645       g_object_set (icon_view->priv->text_cell, "width", wrap_width, NULL);
2646     }
2647 }
2648
2649 static void
2650 gtk_icon_view_layout (GtkIconView *icon_view)
2651 {
2652   GtkAllocation allocation;
2653   GtkWidget *widget;
2654   GList *icons;
2655   gint y = 0, maximum_width = 0;
2656   gint row;
2657   gint item_width;
2658   gboolean size_changed = FALSE;
2659
2660   if (icon_view->priv->layout_idle_id != 0)
2661     {
2662       g_source_remove (icon_view->priv->layout_idle_id);
2663       icon_view->priv->layout_idle_id = 0;
2664     }
2665   
2666   if (icon_view->priv->model == NULL)
2667     return;
2668
2669   widget = GTK_WIDGET (icon_view);
2670
2671   item_width = icon_view->priv->item_width;
2672
2673   /* Update the wrap width for the text cell before going and requesting sizes */
2674   adjust_wrap_width (icon_view);
2675
2676   /* Update the context widths for any invalidated items */
2677   gtk_icon_view_cache_widths (icon_view);
2678
2679   /* Fetch the new item width if needed */
2680   if (item_width < 0)
2681     gtk_cell_area_context_get_preferred_width (icon_view->priv->cell_area_context, 
2682                                                &item_width, NULL);
2683
2684   gtk_cell_area_context_allocate (icon_view->priv->cell_area_context, item_width, -1);
2685
2686   icons = icon_view->priv->items;
2687   y += icon_view->priv->margin;
2688   row = 0;
2689
2690   /* Clear the per row contexts */
2691   g_ptr_array_set_size (icon_view->priv->row_contexts, 0);
2692   
2693   do
2694     {
2695       icons = gtk_icon_view_layout_single_row (icon_view, icons, 
2696                                                item_width, row,
2697                                                &y, &maximum_width);
2698       row++;
2699     }
2700   while (icons != NULL);
2701
2702   if (maximum_width != icon_view->priv->width)
2703     {
2704       icon_view->priv->width = maximum_width;
2705       size_changed = TRUE;
2706     }
2707
2708   y += icon_view->priv->margin;
2709   
2710   if (y != icon_view->priv->height)
2711     {
2712       icon_view->priv->height = y;
2713       size_changed = TRUE;
2714     }
2715
2716   gtk_icon_view_set_hadjustment_values (icon_view);
2717   gtk_icon_view_set_vadjustment_values (icon_view);
2718
2719   if (size_changed)
2720     gtk_widget_queue_resize_no_redraw (widget);
2721
2722   gtk_widget_get_allocation (widget, &allocation);
2723   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
2724     gdk_window_resize (icon_view->priv->bin_window,
2725                        MAX (icon_view->priv->width, allocation.width),
2726                        MAX (icon_view->priv->height, allocation.height));
2727
2728   if (icon_view->priv->scroll_to_path)
2729     {
2730       GtkTreePath *path;
2731
2732       path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
2733       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
2734       icon_view->priv->scroll_to_path = NULL;
2735       
2736       gtk_icon_view_scroll_to_path (icon_view, path,
2737                                     icon_view->priv->scroll_to_use_align,
2738                                     icon_view->priv->scroll_to_row_align,
2739                                     icon_view->priv->scroll_to_col_align);
2740       gtk_tree_path_free (path);
2741     }
2742   
2743   gtk_widget_queue_draw (widget);
2744 }
2745
2746 /* This ensures that all widths have been cached in the
2747  * context and we have proper alignments to go on.
2748  */
2749 static void
2750 gtk_icon_view_cache_widths (GtkIconView *icon_view)
2751 {
2752   GList *items;
2753
2754   g_signal_handler_block (icon_view->priv->cell_area_context, 
2755                           icon_view->priv->context_changed_id);
2756
2757   for (items = icon_view->priv->items; items; items = items->next)
2758     {
2759       GtkIconViewItem *item = items->data;
2760
2761       /* Only fetch the width of items with invalidated sizes */
2762       if (item->cell_area.width < 0)
2763         {
2764           _gtk_icon_view_set_cell_data (icon_view, item);
2765           gtk_cell_area_get_preferred_width (icon_view->priv->cell_area, 
2766                                              icon_view->priv->cell_area_context,
2767                                              GTK_WIDGET (icon_view), NULL, NULL);
2768         }
2769     }
2770
2771   g_signal_handler_unblock (icon_view->priv->cell_area_context, 
2772                             icon_view->priv->context_changed_id);
2773 }
2774
2775 static void
2776 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
2777 {
2778   /* Clear all item sizes */
2779   g_list_foreach (icon_view->priv->items,
2780                   (GFunc)gtk_icon_view_item_invalidate_size, NULL);
2781
2782   /* Reset the context */
2783   if (icon_view->priv->cell_area_context)
2784     {
2785       g_signal_handler_block (icon_view->priv->cell_area_context, 
2786                               icon_view->priv->context_changed_id);
2787       gtk_cell_area_context_reset (icon_view->priv->cell_area_context);
2788       g_signal_handler_unblock (icon_view->priv->cell_area_context, 
2789                                 icon_view->priv->context_changed_id);
2790     }
2791
2792   /* Re-layout the items */
2793   gtk_icon_view_queue_layout (icon_view);
2794 }
2795
2796 static void
2797 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
2798 {
2799   item->cell_area.width = -1;
2800   item->cell_area.height = -1;
2801 }
2802
2803 static void
2804 gtk_icon_view_paint_item (GtkIconView     *icon_view,
2805                           cairo_t         *cr,
2806                           GtkIconViewItem *item,
2807                           gint             x,
2808                           gint             y,
2809                           gboolean         draw_focus)
2810 {
2811   GdkRectangle cell_area;
2812   GtkStateFlags state = 0;
2813   GtkCellRendererState flags = 0;
2814   GtkStyleContext *style_context;
2815   GtkWidget *widget = GTK_WIDGET (icon_view);
2816   GtkIconViewPrivate *priv = icon_view->priv;
2817   GtkCellAreaContext *context;
2818
2819   if (priv->model == NULL)
2820     return;
2821
2822   _gtk_icon_view_set_cell_data (icon_view, item);
2823
2824   style_context = gtk_widget_get_style_context (widget);
2825   state = gtk_widget_get_state_flags (widget);
2826
2827   gtk_style_context_save (style_context);
2828   gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_VIEW);
2829   gtk_style_context_add_class (style_context, GTK_STYLE_CLASS_CELL);
2830
2831   state &= ~(GTK_STATE_FLAG_SELECTED | GTK_STATE_FLAG_PRELIGHT);
2832
2833   if (item->selected)
2834     {
2835       if ((state & GTK_STATE_FLAG_FOCUSED) &&
2836           item == icon_view->priv->cursor_item)
2837         {
2838           flags |= GTK_CELL_RENDERER_FOCUSED;
2839         }
2840
2841       state |= GTK_STATE_FLAG_SELECTED;
2842       flags |= GTK_CELL_RENDERER_SELECTED;
2843     }
2844
2845   if (item->prelight)
2846     {
2847       state |= GTK_STATE_FLAG_PRELIGHT;
2848       flags |= GTK_CELL_RENDERER_PRELIT;
2849     }
2850
2851   gtk_style_context_set_state (style_context, state);
2852
2853   if (item->selected)
2854     {
2855       gtk_render_background (style_context, cr,
2856                              x - icon_view->priv->item_padding,
2857                              y - icon_view->priv->item_padding,
2858                              item->cell_area.width  + icon_view->priv->item_padding * 2,
2859                              item->cell_area.height + icon_view->priv->item_padding * 2);
2860       gtk_render_frame (style_context, cr,
2861                         x - icon_view->priv->item_padding,
2862                         y - icon_view->priv->item_padding,
2863                         item->cell_area.width  + icon_view->priv->item_padding * 2,
2864                         item->cell_area.height + icon_view->priv->item_padding * 2);
2865     }
2866
2867   cell_area.x      = x;
2868   cell_area.y      = y;
2869   cell_area.width  = item->cell_area.width;
2870   cell_area.height = item->cell_area.height;
2871
2872   context = g_ptr_array_index (priv->row_contexts, item->row);
2873   gtk_cell_area_render (priv->cell_area, context,
2874                         widget, cr, &cell_area, &cell_area, flags,
2875                         draw_focus);
2876
2877   gtk_style_context_restore (style_context);
2878 }
2879
2880 static void
2881 gtk_icon_view_paint_rubberband (GtkIconView     *icon_view,
2882                                 cairo_t         *cr)
2883 {
2884   GtkStyleContext *context;
2885   GdkRectangle rect;
2886
2887   cairo_save (cr);
2888
2889   rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
2890   rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
2891   rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
2892   rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
2893
2894   context = gtk_widget_get_style_context (GTK_WIDGET (icon_view));
2895
2896   gtk_style_context_save (context);
2897   gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
2898
2899   gdk_cairo_rectangle (cr, &rect);
2900   cairo_clip (cr);
2901
2902   gtk_render_background (context, cr,
2903                          rect.x, rect.y,
2904                          rect.width, rect.height);
2905   gtk_render_frame (context, cr,
2906                     rect.x, rect.y,
2907                     rect.width, rect.height);
2908
2909   gtk_style_context_restore (context);
2910   cairo_restore (cr);
2911 }
2912
2913 static void
2914 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
2915                                GtkTreePath *path)
2916 {
2917   GList *l;
2918   gint index;
2919
2920   index = gtk_tree_path_get_indices (path)[0];
2921
2922   for (l = icon_view->priv->items; l; l = l->next) 
2923     {
2924       GtkIconViewItem *item = l->data;
2925
2926       if (item->index == index)
2927         {
2928           gtk_icon_view_queue_draw_item (icon_view, item);
2929           break;
2930         }
2931     }
2932 }
2933
2934 static void
2935 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
2936                                GtkIconViewItem *item)
2937 {
2938   GdkRectangle  rect;
2939   GdkRectangle *item_area = (GdkRectangle *)item;
2940
2941   rect.x      = item_area->x - icon_view->priv->item_padding;
2942   rect.y      = item_area->y - icon_view->priv->item_padding;
2943   rect.width  = item_area->width  + icon_view->priv->item_padding * 2;
2944   rect.height = item_area->height + icon_view->priv->item_padding * 2;
2945
2946   if (icon_view->priv->bin_window)
2947     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
2948 }
2949
2950 static gboolean
2951 layout_callback (gpointer user_data)
2952 {
2953   GtkIconView *icon_view;
2954
2955   icon_view = GTK_ICON_VIEW (user_data);
2956   
2957   icon_view->priv->layout_idle_id = 0;
2958
2959   gtk_icon_view_layout (icon_view);
2960   
2961   return FALSE;
2962 }
2963
2964 static void
2965 gtk_icon_view_queue_layout (GtkIconView *icon_view)
2966 {
2967   if (icon_view->priv->layout_idle_id != 0)
2968     return;
2969
2970   icon_view->priv->layout_idle_id =
2971       gdk_threads_add_idle_full (GTK_ICON_VIEW_PRIORITY_LAYOUT,
2972                                  layout_callback, icon_view, NULL);
2973 }
2974
2975 void
2976 _gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
2977                                 GtkIconViewItem *item,
2978                                 GtkCellRenderer *cursor_cell)
2979 {
2980   AtkObject *obj;
2981   AtkObject *item_obj;
2982   AtkObject *cursor_item_obj;
2983
2984   /* When hitting this path from keynav, the focus cell is
2985    * already set, we dont need to notify the atk object
2986    * but we still need to queue the draw here (in the case
2987    * that the focus cell changes but not the cursor item).
2988    */
2989   gtk_icon_view_queue_draw_item (icon_view, item);
2990
2991   if (icon_view->priv->cursor_item == item &&
2992       (cursor_cell == NULL || cursor_cell == gtk_cell_area_get_focus_cell (icon_view->priv->cell_area)))
2993     return;
2994
2995   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
2996   if (icon_view->priv->cursor_item != NULL)
2997     {
2998       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2999       if (obj != NULL)
3000         {
3001           cursor_item_obj = atk_object_ref_accessible_child (obj, icon_view->priv->cursor_item->index);
3002           if (cursor_item_obj != NULL)
3003             atk_object_notify_state_change (cursor_item_obj, ATK_STATE_FOCUSED, FALSE);
3004         }
3005     }
3006   icon_view->priv->cursor_item = item;
3007
3008   if (cursor_cell)
3009     gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cursor_cell);
3010   else
3011     {
3012       /* Make sure there is a cell in focus initially */
3013       if (!gtk_cell_area_get_focus_cell (icon_view->priv->cell_area))
3014         gtk_cell_area_focus (icon_view->priv->cell_area, GTK_DIR_TAB_FORWARD);
3015     }
3016   
3017   /* Notify that accessible focus object has changed */
3018   item_obj = atk_object_ref_accessible_child (obj, item->index);
3019
3020   if (item_obj != NULL)
3021     {
3022       atk_focus_tracker_notify (item_obj);
3023       atk_object_notify_state_change (item_obj, ATK_STATE_FOCUSED, TRUE);
3024       g_object_unref (item_obj); 
3025     }
3026 }
3027
3028
3029 static GtkIconViewItem *
3030 gtk_icon_view_item_new (void)
3031 {
3032   GtkIconViewItem *item;
3033
3034   item = g_slice_new0 (GtkIconViewItem);
3035
3036   item->cell_area.width  = -1;
3037   item->cell_area.height = -1;
3038   
3039   return item;
3040 }
3041
3042 static void
3043 gtk_icon_view_item_free (GtkIconViewItem *item)
3044 {
3045   g_return_if_fail (item != NULL);
3046
3047   g_slice_free (GtkIconViewItem, item);
3048 }
3049
3050 GtkIconViewItem *
3051 _gtk_icon_view_get_item_at_coords (GtkIconView          *icon_view,
3052                                    gint                  x,
3053                                    gint                  y,
3054                                    gboolean              only_in_cell,
3055                                    GtkCellRenderer     **cell_at_pos)
3056 {
3057   GList *items;
3058
3059   if (cell_at_pos)
3060     *cell_at_pos = NULL;
3061
3062   for (items = icon_view->priv->items; items; items = items->next)
3063     {
3064       GtkIconViewItem *item = items->data;
3065       GdkRectangle    *item_area = (GdkRectangle *)item;
3066
3067       if (x >= item_area->x - icon_view->priv->column_spacing/2 && 
3068           x <= item_area->x + item_area->width + icon_view->priv->column_spacing/2 &&
3069           y >= item_area->y - icon_view->priv->row_spacing/2 && 
3070           y <= item_area->y + item_area->height + icon_view->priv->row_spacing/2)
3071         {
3072           if (only_in_cell || cell_at_pos)
3073             {
3074               GtkCellRenderer *cell = NULL;
3075               GtkCellAreaContext *context;
3076
3077               context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
3078               _gtk_icon_view_set_cell_data (icon_view, item);
3079
3080               if (x >= item_area->x && x <= item_area->x + item_area->width &&
3081                   y >= item_area->y && y <= item_area->y + item_area->height)
3082                 cell = gtk_cell_area_get_cell_at_position (icon_view->priv->cell_area, context,
3083                                                            GTK_WIDGET (icon_view),
3084                                                            item_area,
3085                                                            x, y, NULL);
3086
3087               if (cell_at_pos)
3088                 *cell_at_pos = cell;
3089
3090               if (only_in_cell)
3091                 return cell != NULL ? item : NULL;
3092               else
3093                 return item;
3094             }
3095           return item;
3096         }
3097     }
3098   return NULL;
3099 }
3100
3101 void
3102 _gtk_icon_view_select_item (GtkIconView      *icon_view,
3103                             GtkIconViewItem  *item)
3104 {
3105   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3106   g_return_if_fail (item != NULL);
3107
3108   if (item->selected)
3109     return;
3110   
3111   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3112     return;
3113   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3114     gtk_icon_view_unselect_all_internal (icon_view);
3115
3116   item->selected = TRUE;
3117
3118   gtk_icon_view_item_selected_changed (icon_view, item);
3119   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3120
3121   gtk_icon_view_queue_draw_item (icon_view, item);
3122 }
3123
3124
3125 void
3126 _gtk_icon_view_unselect_item (GtkIconView      *icon_view,
3127                               GtkIconViewItem  *item)
3128 {
3129   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3130   g_return_if_fail (item != NULL);
3131
3132   if (!item->selected)
3133     return;
3134   
3135   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3136       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3137     return;
3138   
3139   item->selected = FALSE;
3140
3141   gtk_icon_view_item_selected_changed (icon_view, item);
3142   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3143
3144   gtk_icon_view_queue_draw_item (icon_view, item);
3145 }
3146
3147 static void
3148 verify_items (GtkIconView *icon_view)
3149 {
3150   GList *items;
3151   int i = 0;
3152
3153   for (items = icon_view->priv->items; items; items = items->next)
3154     {
3155       GtkIconViewItem *item = items->data;
3156
3157       if (item->index != i)
3158         g_error ("List item does not match its index: "
3159                  "item index %d and list index %d\n", item->index, i);
3160
3161       i++;
3162     }
3163 }
3164
3165 static void
3166 gtk_icon_view_row_changed (GtkTreeModel *model,
3167                            GtkTreePath  *path,
3168                            GtkTreeIter  *iter,
3169                            gpointer      data)
3170 {
3171   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3172
3173   /* ignore changes in branches */
3174   if (gtk_tree_path_get_depth (path) > 1)
3175     return;
3176
3177   /* An icon view subclass might add it's own model and populate
3178    * things at init() time instead of waiting for the constructor() 
3179    * to be called 
3180    */
3181   if (icon_view->priv->cell_area)
3182     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3183
3184   /* Here we can use a "grow-only" strategy for optimization
3185    * and only invalidate a single item and queue a relayout
3186    * instead of invalidating the whole thing.
3187    *
3188    * For now GtkIconView still cant deal with huge models
3189    * so just invalidate the whole thing when the model
3190    * changes.
3191    */
3192   gtk_icon_view_invalidate_sizes (icon_view);
3193
3194   verify_items (icon_view);
3195 }
3196
3197 static void
3198 gtk_icon_view_row_inserted (GtkTreeModel *model,
3199                             GtkTreePath  *path,
3200                             GtkTreeIter  *iter,
3201                             gpointer      data)
3202 {
3203   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3204   gint index;
3205   GtkIconViewItem *item;
3206   gboolean iters_persist;
3207   GList *list;
3208
3209   /* ignore changes in branches */
3210   if (gtk_tree_path_get_depth (path) > 1)
3211     return;
3212
3213   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3214   
3215   index = gtk_tree_path_get_indices(path)[0];
3216
3217   item = gtk_icon_view_item_new ();
3218
3219   if (iters_persist)
3220     item->iter = *iter;
3221
3222   item->index = index;
3223
3224   /* FIXME: We can be more efficient here,
3225      we can store a tail pointer and use that when
3226      appending (which is a rather common operation)
3227   */
3228   icon_view->priv->items = g_list_insert (icon_view->priv->items,
3229                                          item, index);
3230   
3231   list = g_list_nth (icon_view->priv->items, index + 1);
3232   for (; list; list = list->next)
3233     {
3234       item = list->data;
3235
3236       item->index++;
3237     }
3238     
3239   verify_items (icon_view);
3240
3241   gtk_icon_view_queue_layout (icon_view);
3242 }
3243
3244 static void
3245 gtk_icon_view_row_deleted (GtkTreeModel *model,
3246                            GtkTreePath  *path,
3247                            gpointer      data)
3248 {
3249   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3250   gint index;
3251   GtkIconViewItem *item;
3252   GList *list, *next;
3253   gboolean emit = FALSE;
3254
3255   /* ignore changes in branches */
3256   if (gtk_tree_path_get_depth (path) > 1)
3257     return;
3258
3259   index = gtk_tree_path_get_indices(path)[0];
3260
3261   list = g_list_nth (icon_view->priv->items, index);
3262   item = list->data;
3263
3264   if (icon_view->priv->cell_area)
3265     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3266
3267   if (item == icon_view->priv->anchor_item)
3268     icon_view->priv->anchor_item = NULL;
3269
3270   if (item == icon_view->priv->cursor_item)
3271     icon_view->priv->cursor_item = NULL;
3272
3273   if (item == icon_view->priv->last_prelight)
3274     icon_view->priv->last_prelight = NULL;
3275
3276   if (item->selected)
3277     emit = TRUE;
3278   
3279   gtk_icon_view_item_free (item);
3280
3281   for (next = list->next; next; next = next->next)
3282     {
3283       item = next->data;
3284
3285       item->index--;
3286     }
3287   
3288   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3289
3290   verify_items (icon_view);  
3291   
3292   gtk_icon_view_queue_layout (icon_view);
3293
3294   if (emit)
3295     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3296 }
3297
3298 static void
3299 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3300                               GtkTreePath  *parent,
3301                               GtkTreeIter  *iter,
3302                               gint         *new_order,
3303                               gpointer      data)
3304 {
3305   GtkIconView *icon_view = GTK_ICON_VIEW (data);
3306   int i;
3307   int length;
3308   GList *items = NULL, *list;
3309   GtkIconViewItem **item_array;
3310   gint *order;
3311
3312   /* ignore changes in branches */
3313   if (iter != NULL)
3314     return;
3315
3316   if (icon_view->priv->cell_area)
3317     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
3318
3319   length = gtk_tree_model_iter_n_children (model, NULL);
3320
3321   order = g_new (gint, length);
3322   for (i = 0; i < length; i++)
3323     order [new_order[i]] = i;
3324
3325   item_array = g_new (GtkIconViewItem *, length);
3326   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3327     item_array[order[i]] = list->data;
3328   g_free (order);
3329
3330   for (i = length - 1; i >= 0; i--)
3331     {
3332       item_array[i]->index = i;
3333       items = g_list_prepend (items, item_array[i]);
3334     }
3335   
3336   g_free (item_array);
3337   g_list_free (icon_view->priv->items);
3338   icon_view->priv->items = items;
3339
3340   gtk_icon_view_queue_layout (icon_view);
3341
3342   verify_items (icon_view);  
3343 }
3344
3345 static void
3346 gtk_icon_view_build_items (GtkIconView *icon_view)
3347 {
3348   GtkTreeIter iter;
3349   int i;
3350   gboolean iters_persist;
3351   GList *items = NULL;
3352
3353   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3354   
3355   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3356                                       &iter))
3357     return;
3358
3359   i = 0;
3360   
3361   do
3362     {
3363       GtkIconViewItem *item = gtk_icon_view_item_new ();
3364
3365       if (iters_persist)
3366         item->iter = iter;
3367
3368       item->index = i;
3369       
3370       i++;
3371
3372       items = g_list_prepend (items, item);
3373       
3374     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3375
3376   icon_view->priv->items = g_list_reverse (items);
3377 }
3378
3379 static void
3380 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
3381                                 guint           keyval,
3382                                 guint           modmask,
3383                                 GtkMovementStep step,
3384                                 gint            count)
3385 {
3386   
3387   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3388                                 I_("move-cursor"), 2,
3389                                 G_TYPE_ENUM, step,
3390                                 G_TYPE_INT, count);
3391
3392   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3393                                 "move-cursor", 2,
3394                                 G_TYPE_ENUM, step,
3395                                 G_TYPE_INT, count);
3396
3397   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3398    return;
3399
3400   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3401                                 "move-cursor", 2,
3402                                 G_TYPE_ENUM, step,
3403                                 G_TYPE_INT, count);
3404
3405   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3406                                 "move-cursor", 2,
3407                                 G_TYPE_ENUM, step,
3408                                 G_TYPE_INT, count);
3409 }
3410
3411 static gboolean
3412 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
3413                                 GtkMovementStep  step,
3414                                 gint             count)
3415 {
3416   GdkModifierType state;
3417
3418   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3419   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3420                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3421                         step == GTK_MOVEMENT_DISPLAY_LINES ||
3422                         step == GTK_MOVEMENT_PAGES ||
3423                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3424
3425   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3426     return FALSE;
3427
3428   gtk_cell_area_stop_editing (icon_view->priv->cell_area, FALSE);
3429   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3430
3431   if (gtk_get_current_event_state (&state))
3432     {
3433       GdkModifierType extend_mod_mask;
3434       GdkModifierType modify_mod_mask;
3435
3436       extend_mod_mask =
3437         gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3438                                       GDK_MODIFIER_INTENT_EXTEND_SELECTION);
3439       modify_mod_mask =
3440         gtk_widget_get_modifier_mask (GTK_WIDGET (icon_view),
3441                                       GDK_MODIFIER_INTENT_MODIFY_SELECTION);
3442
3443       if ((state & modify_mod_mask) == modify_mod_mask)
3444         icon_view->priv->modify_selection_pressed = TRUE;
3445       if ((state & extend_mod_mask) == extend_mod_mask)
3446         icon_view->priv->extend_selection_pressed = TRUE;
3447     }
3448   /* else we assume not pressed */
3449
3450   switch (step)
3451     {
3452     case GTK_MOVEMENT_LOGICAL_POSITIONS:
3453     case GTK_MOVEMENT_VISUAL_POSITIONS:
3454       gtk_icon_view_move_cursor_left_right (icon_view, count);
3455       break;
3456     case GTK_MOVEMENT_DISPLAY_LINES:
3457       gtk_icon_view_move_cursor_up_down (icon_view, count);
3458       break;
3459     case GTK_MOVEMENT_PAGES:
3460       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3461       break;
3462     case GTK_MOVEMENT_BUFFER_ENDS:
3463       gtk_icon_view_move_cursor_start_end (icon_view, count);
3464       break;
3465     default:
3466       g_assert_not_reached ();
3467     }
3468
3469   icon_view->priv->modify_selection_pressed = FALSE;
3470   icon_view->priv->extend_selection_pressed = FALSE;
3471
3472   icon_view->priv->draw_focus = TRUE;
3473
3474   return TRUE;
3475 }
3476
3477 static GtkIconViewItem *
3478 find_item (GtkIconView     *icon_view,
3479            GtkIconViewItem *current,
3480            gint             row_ofs,
3481            gint             col_ofs)
3482 {
3483   gint row, col;
3484   GList *items;
3485   GtkIconViewItem *item;
3486
3487   /* FIXME: this could be more efficient 
3488    */
3489   row = current->row + row_ofs;
3490   col = current->col + col_ofs;
3491
3492   for (items = icon_view->priv->items; items; items = items->next)
3493     {
3494       item = items->data;
3495       if (item->row == row && item->col == col)
3496         return item;
3497     }
3498   
3499   return NULL;
3500 }
3501
3502 static GtkIconViewItem *
3503 find_item_page_up_down (GtkIconView     *icon_view,
3504                         GtkIconViewItem *current,
3505                         gint             count)
3506 {
3507   GList *item, *next;
3508   gint y, col;
3509   
3510   col = current->col;
3511   y = current->cell_area.y + count * gtk_adjustment_get_page_size (icon_view->priv->vadjustment);
3512
3513   item = g_list_find (icon_view->priv->items, current);
3514   if (count > 0)
3515     {
3516       while (item)
3517         {
3518           for (next = item->next; next; next = next->next)
3519             {
3520               if (((GtkIconViewItem *)next->data)->col == col)
3521                 break;
3522             }
3523           if (!next || ((GtkIconViewItem *)next->data)->cell_area.y > y)
3524             break;
3525
3526           item = next;
3527         }
3528     }
3529   else 
3530     {
3531       while (item)
3532         {
3533           for (next = item->prev; next; next = next->prev)
3534             {
3535               if (((GtkIconViewItem *)next->data)->col == col)
3536                 break;
3537             }
3538           if (!next || ((GtkIconViewItem *)next->data)->cell_area.y < y)
3539             break;
3540
3541           item = next;
3542         }
3543     }
3544
3545   if (item)
3546     return item->data;
3547
3548   return NULL;
3549 }
3550
3551 static gboolean
3552 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
3553                                   GtkIconViewItem *anchor,
3554                                   GtkIconViewItem *cursor)
3555 {
3556   GList *items;
3557   GtkIconViewItem *item;
3558   gint row1, row2, col1, col2;
3559   gboolean dirty = FALSE;
3560   
3561   if (anchor->row < cursor->row)
3562     {
3563       row1 = anchor->row;
3564       row2 = cursor->row;
3565     }
3566   else
3567     {
3568       row1 = cursor->row;
3569       row2 = anchor->row;
3570     }
3571
3572   if (anchor->col < cursor->col)
3573     {
3574       col1 = anchor->col;
3575       col2 = cursor->col;
3576     }
3577   else
3578     {
3579       col1 = cursor->col;
3580       col2 = anchor->col;
3581     }
3582
3583   for (items = icon_view->priv->items; items; items = items->next)
3584     {
3585       item = items->data;
3586
3587       if (row1 <= item->row && item->row <= row2 &&
3588           col1 <= item->col && item->col <= col2)
3589         {
3590           if (!item->selected)
3591             {
3592               dirty = TRUE;
3593               item->selected = TRUE;
3594               gtk_icon_view_item_selected_changed (icon_view, item);
3595             }
3596           gtk_icon_view_queue_draw_item (icon_view, item);
3597         }
3598     }
3599
3600   return dirty;
3601 }
3602
3603 static void 
3604 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3605                                    gint         count)
3606 {
3607   GtkIconViewItem *item;
3608   GtkCellRenderer *cell = NULL;
3609   gboolean dirty = FALSE;
3610   gint step;
3611   GtkDirectionType direction;
3612
3613   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3614     return;
3615
3616   direction = count < 0 ? GTK_DIR_UP : GTK_DIR_DOWN;
3617
3618   if (!icon_view->priv->cursor_item)
3619     {
3620       GList *list;
3621
3622       if (count > 0)
3623         list = icon_view->priv->items;
3624       else
3625         list = g_list_last (icon_view->priv->items);
3626
3627       if (list)
3628         {
3629           item = list->data;
3630
3631           /* Give focus to the first cell initially */
3632           _gtk_icon_view_set_cell_data (icon_view, item);
3633           gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3634         }
3635       else
3636         {
3637           item = NULL;
3638         }
3639     }
3640   else
3641     {
3642       item = icon_view->priv->cursor_item;
3643       step = count > 0 ? 1 : -1;      
3644
3645       /* Save the current focus cell in case we hit the edge */
3646       cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3647
3648       while (item)
3649         {
3650           _gtk_icon_view_set_cell_data (icon_view, item);
3651
3652           if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3653             break;
3654
3655           item = find_item (icon_view, item, step, 0);
3656         }
3657     }
3658
3659   if (!item)
3660     {
3661       if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3662         {
3663           GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3664           if (toplevel)
3665             gtk_widget_child_focus (toplevel,
3666                                     direction == GTK_DIR_UP ?
3667                                     GTK_DIR_TAB_BACKWARD :
3668                                     GTK_DIR_TAB_FORWARD);
3669
3670         }
3671
3672       gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3673       return;
3674     }
3675
3676   if (icon_view->priv->modify_selection_pressed ||
3677       !icon_view->priv->extend_selection_pressed ||
3678       !icon_view->priv->anchor_item ||
3679       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3680     icon_view->priv->anchor_item = item;
3681
3682   cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3683   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3684
3685   if (!icon_view->priv->modify_selection_pressed &&
3686       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3687     {
3688       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3689       dirty = gtk_icon_view_select_all_between (icon_view, 
3690                                                 icon_view->priv->anchor_item,
3691                                                 item) || dirty;
3692     }
3693
3694   gtk_icon_view_scroll_to_item (icon_view, item);
3695
3696   if (dirty)
3697     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3698 }
3699
3700 static void 
3701 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3702                                         gint         count)
3703 {
3704   GtkIconViewItem *item;
3705   gboolean dirty = FALSE;
3706   
3707   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3708     return;
3709   
3710   if (!icon_view->priv->cursor_item)
3711     {
3712       GList *list;
3713
3714       if (count > 0)
3715         list = icon_view->priv->items;
3716       else
3717         list = g_list_last (icon_view->priv->items);
3718
3719       item = list ? list->data : NULL;
3720     }
3721   else
3722     item = find_item_page_up_down (icon_view, 
3723                                    icon_view->priv->cursor_item,
3724                                    count);
3725
3726   if (item == icon_view->priv->cursor_item)
3727     gtk_widget_error_bell (GTK_WIDGET (icon_view));
3728
3729   if (!item)
3730     return;
3731
3732   if (icon_view->priv->modify_selection_pressed ||
3733       !icon_view->priv->extend_selection_pressed ||
3734       !icon_view->priv->anchor_item ||
3735       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3736     icon_view->priv->anchor_item = item;
3737
3738   _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
3739
3740   if (!icon_view->priv->modify_selection_pressed &&
3741       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3742     {
3743       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3744       dirty = gtk_icon_view_select_all_between (icon_view, 
3745                                                 icon_view->priv->anchor_item,
3746                                                 item) || dirty;
3747     }
3748
3749   gtk_icon_view_scroll_to_item (icon_view, item);
3750
3751   if (dirty)
3752     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);  
3753 }
3754
3755 static void 
3756 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
3757                                       gint         count)
3758 {
3759   GtkIconViewItem *item;
3760   GtkCellRenderer *cell = NULL;
3761   gboolean dirty = FALSE;
3762   gint step;
3763   GtkDirectionType direction;
3764
3765   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3766     return;
3767
3768   direction = count < 0 ? GTK_DIR_LEFT : GTK_DIR_RIGHT;
3769
3770   if (!icon_view->priv->cursor_item)
3771     {
3772       GList *list;
3773
3774       if (count > 0)
3775         list = icon_view->priv->items;
3776       else
3777         list = g_list_last (icon_view->priv->items);
3778
3779       if (list)
3780         {
3781           item = list->data;
3782
3783           /* Give focus to the first cell initially */
3784           _gtk_icon_view_set_cell_data (icon_view, item);
3785           gtk_cell_area_focus (icon_view->priv->cell_area, direction);
3786         }
3787       else
3788         {
3789           item = NULL;
3790         }
3791     }
3792   else
3793     {
3794       item = icon_view->priv->cursor_item;
3795       step = count > 0 ? 1 : -1;
3796
3797       /* Save the current focus cell in case we hit the edge */
3798       cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3799
3800       while (item)
3801         {
3802           _gtk_icon_view_set_cell_data (icon_view, item);
3803
3804           if (gtk_cell_area_focus (icon_view->priv->cell_area, direction))
3805             break;
3806           
3807           item = find_item (icon_view, item, 0, step);
3808         }
3809     }
3810
3811   if (!item)
3812     {
3813       if (!gtk_widget_keynav_failed (GTK_WIDGET (icon_view), direction))
3814         {
3815           GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (icon_view));
3816           if (toplevel)
3817             gtk_widget_child_focus (toplevel,
3818                                     direction == GTK_DIR_LEFT ?
3819                                     GTK_DIR_TAB_BACKWARD :
3820                                     GTK_DIR_TAB_FORWARD);
3821
3822         }
3823
3824       gtk_cell_area_set_focus_cell (icon_view->priv->cell_area, cell);
3825       return;
3826     }
3827
3828   if (icon_view->priv->modify_selection_pressed ||
3829       !icon_view->priv->extend_selection_pressed ||
3830       !icon_view->priv->anchor_item ||
3831       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3832     icon_view->priv->anchor_item = item;
3833
3834   cell = gtk_cell_area_get_focus_cell (icon_view->priv->cell_area);
3835   _gtk_icon_view_set_cursor_item (icon_view, item, cell);
3836
3837   if (!icon_view->priv->modify_selection_pressed &&
3838       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3839     {
3840       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3841       dirty = gtk_icon_view_select_all_between (icon_view, 
3842                                                 icon_view->priv->anchor_item,
3843                                                 item) || dirty;
3844     }
3845
3846   gtk_icon_view_scroll_to_item (icon_view, item);
3847
3848   if (dirty)
3849     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3850 }
3851
3852 static void 
3853 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
3854                                      gint         count)
3855 {
3856   GtkIconViewItem *item;
3857   GList *list;
3858   gboolean dirty = FALSE;
3859   
3860   if (!gtk_widget_has_focus (GTK_WIDGET (icon_view)))
3861     return;
3862   
3863   if (count < 0)
3864     list = icon_view->priv->items;
3865   else
3866     list = g_list_last (icon_view->priv->items);
3867   
3868   item = list ? list->data : NULL;
3869
3870   if (item == icon_view->priv->cursor_item)
3871     gtk_widget_error_bell (GTK_WIDGET (icon_view));
3872
3873   if (!item)
3874     return;
3875
3876   if (icon_view->priv->modify_selection_pressed ||
3877       !icon_view->priv->extend_selection_pressed ||
3878       !icon_view->priv->anchor_item ||
3879       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3880     icon_view->priv->anchor_item = item;
3881
3882   _gtk_icon_view_set_cursor_item (icon_view, item, NULL);
3883
3884   if (!icon_view->priv->modify_selection_pressed &&
3885       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3886     {
3887       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3888       dirty = gtk_icon_view_select_all_between (icon_view, 
3889                                                 icon_view->priv->anchor_item,
3890                                                 item) || dirty;
3891     }
3892
3893   gtk_icon_view_scroll_to_item (icon_view, item);
3894
3895   if (dirty)
3896     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3897 }
3898
3899 /**
3900  * gtk_icon_view_scroll_to_path:
3901  * @icon_view: A #GtkIconView.
3902  * @path: The path of the item to move to.
3903  * @use_align: whether to use alignment arguments, or %FALSE.
3904  * @row_align: The vertical alignment of the item specified by @path.
3905  * @col_align: The horizontal alignment of the item specified by @path.
3906  *
3907  * Moves the alignments of @icon_view to the position specified by @path.  
3908  * @row_align determines where the row is placed, and @col_align determines 
3909  * where @column is placed.  Both are expected to be between 0.0 and 1.0. 
3910  * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means 
3911  * center.
3912  *
3913  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
3914  * tree does the minimum amount of work to scroll the item onto the screen.
3915  * This means that the item will be scrolled to the edge closest to its current
3916  * position.  If the item is currently visible on the screen, nothing is done.
3917  *
3918  * This function only works if the model is set, and @path is a valid row on 
3919  * the model. If the model changes before the @icon_view is realized, the 
3920  * centered path will be modified to reflect this change.
3921  *
3922  * Since: 2.8
3923  **/
3924 void
3925 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
3926                               GtkTreePath *path,
3927                               gboolean     use_align,
3928                               gfloat       row_align,
3929                               gfloat       col_align)
3930 {
3931   GtkIconViewItem *item = NULL;
3932   GtkWidget *widget;
3933
3934   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3935   g_return_if_fail (path != NULL);
3936   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
3937   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
3938
3939   widget = GTK_WIDGET (icon_view);
3940
3941   if (gtk_tree_path_get_depth (path) > 0)
3942     item = g_list_nth_data (icon_view->priv->items,
3943                             gtk_tree_path_get_indices(path)[0]);
3944   
3945   if (!item || item->cell_area.width < 0 ||
3946       !gtk_widget_get_realized (widget))
3947     {
3948       if (icon_view->priv->scroll_to_path)
3949         gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
3950
3951       icon_view->priv->scroll_to_path = NULL;
3952
3953       if (path)
3954         icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
3955
3956       icon_view->priv->scroll_to_use_align = use_align;
3957       icon_view->priv->scroll_to_row_align = row_align;
3958       icon_view->priv->scroll_to_col_align = col_align;
3959
3960       return;
3961     }
3962
3963   if (use_align)
3964     {
3965       GtkAllocation allocation;
3966       gint x, y;
3967       gfloat offset;
3968       GdkRectangle item_area = 
3969         { 
3970           item->cell_area.x - icon_view->priv->item_padding, 
3971           item->cell_area.y - icon_view->priv->item_padding, 
3972           item->cell_area.width  + icon_view->priv->item_padding * 2, 
3973           item->cell_area.height + icon_view->priv->item_padding * 2 
3974         };
3975
3976       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
3977
3978       gtk_widget_get_allocation (widget, &allocation);
3979
3980       offset = y + item_area.y - row_align * (allocation.height - item_area.height);
3981
3982       gtk_adjustment_set_value (icon_view->priv->vadjustment,
3983                                 gtk_adjustment_get_value (icon_view->priv->vadjustment) + offset);
3984
3985       offset = x + item_area.x - col_align * (allocation.width - item_area.width);
3986
3987       gtk_adjustment_set_value (icon_view->priv->hadjustment,
3988                                 gtk_adjustment_get_value (icon_view->priv->hadjustment) + offset);
3989
3990       gtk_adjustment_changed (icon_view->priv->hadjustment);
3991       gtk_adjustment_changed (icon_view->priv->vadjustment);
3992     }
3993   else
3994     gtk_icon_view_scroll_to_item (icon_view, item);
3995 }
3996
3997
3998 static void
3999 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view,
4000                               GtkIconViewItem *item)
4001 {
4002   GtkIconViewPrivate *priv = icon_view->priv;
4003   GtkWidget *widget = GTK_WIDGET (icon_view);
4004   GtkAdjustment *hadj, *vadj;
4005   GtkAllocation allocation;
4006   gint x, y;
4007   GdkRectangle item_area;
4008
4009   item_area.x = item->cell_area.x - priv->item_padding;
4010   item_area.y = item->cell_area.y - priv->item_padding;
4011   item_area.width = item->cell_area.width  + priv->item_padding * 2;
4012   item_area.height = item->cell_area.height + priv->item_padding * 2;
4013
4014   gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4015   gtk_widget_get_allocation (widget, &allocation);
4016
4017   hadj = icon_view->priv->hadjustment;
4018   vadj = icon_view->priv->vadjustment;
4019
4020   if (y + item_area.y < 0)
4021     gtk_adjustment_set_value (vadj,
4022                               gtk_adjustment_get_value (vadj)
4023                                 + y + item_area.y);
4024   else if (y + item_area.y + item_area.height > allocation.height)
4025     gtk_adjustment_set_value (vadj,
4026                               gtk_adjustment_get_value (vadj)
4027                                 + y + item_area.y + item_area.height - allocation.height);
4028
4029   if (x + item_area.x < 0)
4030     gtk_adjustment_set_value (hadj,
4031                               gtk_adjustment_get_value (hadj)
4032                                 + x + item_area.x);
4033   else if (x + item_area.x + item_area.width > allocation.width)
4034     gtk_adjustment_set_value (hadj,
4035                               gtk_adjustment_get_value (hadj)
4036                                 + x + item_area.x + item_area.width - allocation.width);
4037
4038   gtk_adjustment_changed (hadj);
4039   gtk_adjustment_changed (vadj);
4040 }
4041
4042 /* GtkCellLayout implementation */
4043
4044 static void
4045 gtk_icon_view_ensure_cell_area (GtkIconView *icon_view,
4046                                 GtkCellArea *cell_area)
4047 {
4048   GtkIconViewPrivate *priv = icon_view->priv;
4049
4050   if (priv->cell_area)
4051     return;
4052
4053   if (cell_area)
4054     priv->cell_area = cell_area;
4055   else
4056     priv->cell_area = gtk_cell_area_box_new ();
4057
4058   g_object_ref_sink (priv->cell_area);
4059
4060   if (GTK_IS_ORIENTABLE (priv->cell_area))
4061     gtk_orientable_set_orientation (GTK_ORIENTABLE (priv->cell_area), priv->item_orientation);
4062
4063   priv->cell_area_context = gtk_cell_area_create_context (priv->cell_area);
4064
4065   priv->add_editable_id =
4066     g_signal_connect (priv->cell_area, "add-editable",
4067                       G_CALLBACK (gtk_icon_view_add_editable), icon_view);
4068   priv->remove_editable_id =
4069     g_signal_connect (priv->cell_area, "remove-editable",
4070                       G_CALLBACK (gtk_icon_view_remove_editable), icon_view);
4071   priv->context_changed_id =
4072     g_signal_connect (priv->cell_area_context, "notify",
4073                       G_CALLBACK (gtk_icon_view_context_changed), icon_view);
4074
4075   update_text_cell (icon_view);
4076   update_pixbuf_cell (icon_view);
4077 }
4078
4079 static GtkCellArea *
4080 gtk_icon_view_cell_layout_get_area (GtkCellLayout *cell_layout)
4081 {
4082   GtkIconView *icon_view = GTK_ICON_VIEW (cell_layout);
4083   GtkIconViewPrivate *priv = icon_view->priv;
4084
4085   if (G_UNLIKELY (!priv->cell_area))
4086     gtk_icon_view_ensure_cell_area (icon_view, NULL);
4087
4088   return icon_view->priv->cell_area;
4089 }
4090
4091 void
4092 _gtk_icon_view_set_cell_data (GtkIconView     *icon_view,
4093                               GtkIconViewItem *item)
4094 {
4095   gboolean iters_persist;
4096   GtkTreeIter iter;
4097
4098   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
4099   
4100   if (!iters_persist)
4101     {
4102       GtkTreePath *path;
4103
4104       path = gtk_tree_path_new_from_indices (item->index, -1);
4105       if (!gtk_tree_model_get_iter (icon_view->priv->model, &iter, path))
4106         return;
4107       gtk_tree_path_free (path);
4108     }
4109   else
4110     iter = item->iter;
4111
4112   gtk_cell_area_apply_attributes (icon_view->priv->cell_area, 
4113                                   icon_view->priv->model,
4114                                   &iter, FALSE, FALSE);
4115 }
4116
4117
4118
4119 /* Public API */
4120
4121
4122 /**
4123  * gtk_icon_view_new:
4124  * 
4125  * Creates a new #GtkIconView widget
4126  * 
4127  * Return value: A newly created #GtkIconView widget
4128  *
4129  * Since: 2.6
4130  **/
4131 GtkWidget *
4132 gtk_icon_view_new (void)
4133 {
4134   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4135 }
4136
4137 /**
4138  * gtk_icon_view_new_with_area:
4139  * @area: the #GtkCellArea to use to layout cells
4140  * 
4141  * Creates a new #GtkIconView widget using the
4142  * specified @area to layout cells inside the icons.
4143  * 
4144  * Return value: A newly created #GtkIconView widget
4145  *
4146  * Since: 3.0
4147  **/
4148 GtkWidget *
4149 gtk_icon_view_new_with_area (GtkCellArea *area)
4150 {
4151   return g_object_new (GTK_TYPE_ICON_VIEW, "cell-area", area, NULL);
4152 }
4153
4154 /**
4155  * gtk_icon_view_new_with_model:
4156  * @model: The model.
4157  * 
4158  * Creates a new #GtkIconView widget with the model @model.
4159  * 
4160  * Return value: A newly created #GtkIconView widget.
4161  *
4162  * Since: 2.6 
4163  **/
4164 GtkWidget *
4165 gtk_icon_view_new_with_model (GtkTreeModel *model)
4166 {
4167   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4168 }
4169
4170 /**
4171  * gtk_icon_view_convert_widget_to_bin_window_coords:
4172  * @icon_view: a #GtkIconView 
4173  * @wx: X coordinate relative to the widget
4174  * @wy: Y coordinate relative to the widget
4175  * @bx: (out): return location for bin_window X coordinate
4176  * @by: (out): return location for bin_window Y coordinate
4177  * 
4178  * Converts widget coordinates to coordinates for the bin_window,
4179  * as expected by e.g. gtk_icon_view_get_path_at_pos(). 
4180  *
4181  * Since: 2.12
4182  */
4183 void
4184 gtk_icon_view_convert_widget_to_bin_window_coords (GtkIconView *icon_view,
4185                                                    gint         wx,
4186                                                    gint         wy, 
4187                                                    gint        *bx,
4188                                                    gint        *by)
4189 {
4190   gint x, y;
4191
4192   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4193
4194   if (icon_view->priv->bin_window) 
4195     gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4196   else
4197     x = y = 0;
4198  
4199   if (bx)
4200     *bx = wx - x;
4201   if (by)
4202     *by = wy - y;
4203 }
4204
4205 /**
4206  * gtk_icon_view_get_path_at_pos:
4207  * @icon_view: A #GtkIconView.
4208  * @x: The x position to be identified
4209  * @y: The y position to be identified
4210  * 
4211  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4212  * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4213  * the cell at the specified position. 
4214  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4215  * widget coordinates to bin_window coordinates.
4216  * 
4217  * Return value: The #GtkTreePath corresponding to the icon or %NULL
4218  * if no icon exists at that position.
4219  *
4220  * Since: 2.6 
4221  **/
4222 GtkTreePath *
4223 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4224                                gint         x,
4225                                gint         y)
4226 {
4227   GtkIconViewItem *item;
4228   GtkTreePath *path;
4229   
4230   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4231
4232   item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4233
4234   if (!item)
4235     return NULL;
4236
4237   path = gtk_tree_path_new_from_indices (item->index, -1);
4238
4239   return path;
4240 }
4241
4242 /**
4243  * gtk_icon_view_get_item_at_pos:
4244  * @icon_view: A #GtkIconView.
4245  * @x: The x position to be identified
4246  * @y: The y position to be identified
4247  * @path: (out) (allow-none): Return location for the path, or %NULL
4248  * @cell: (out) (allow-none): Return location for the renderer
4249  *   responsible for the cell at (@x, @y), or %NULL
4250  * 
4251  * Finds the path at the point (@x, @y), relative to bin_window coordinates.
4252  * In contrast to gtk_icon_view_get_path_at_pos(), this function also 
4253  * obtains the cell at the specified position. The returned path should
4254  * be freed with gtk_tree_path_free().
4255  * See gtk_icon_view_convert_widget_to_bin_window_coords() for converting
4256  * widget coordinates to bin_window coordinates.
4257  * 
4258  * Return value: %TRUE if an item exists at the specified position
4259  *
4260  * Since: 2.8
4261  **/
4262 gboolean 
4263 gtk_icon_view_get_item_at_pos (GtkIconView      *icon_view,
4264                                gint              x,
4265                                gint              y,
4266                                GtkTreePath     **path,
4267                                GtkCellRenderer **cell)
4268 {
4269   GtkIconViewItem *item;
4270   GtkCellRenderer *renderer = NULL;
4271   
4272   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4273
4274   item = _gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &renderer);
4275
4276   if (path != NULL)
4277     {
4278       if (item != NULL)
4279         *path = gtk_tree_path_new_from_indices (item->index, -1);
4280       else
4281         *path = NULL;
4282     }
4283
4284   if (cell != NULL)
4285     *cell = renderer;
4286
4287   return (item != NULL);
4288 }
4289
4290 /**
4291  * gtk_icon_view_set_tooltip_item:
4292  * @icon_view: a #GtkIconView
4293  * @tooltip: a #GtkTooltip
4294  * @path: a #GtkTreePath
4295  * 
4296  * Sets the tip area of @tooltip to be the area covered by the item at @path.
4297  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4298  * See also gtk_tooltip_set_tip_area().
4299  * 
4300  * Since: 2.12
4301  */
4302 void 
4303 gtk_icon_view_set_tooltip_item (GtkIconView     *icon_view,
4304                                 GtkTooltip      *tooltip,
4305                                 GtkTreePath     *path)
4306 {
4307   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4308   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4309
4310   gtk_icon_view_set_tooltip_cell (icon_view, tooltip, path, NULL);
4311 }
4312
4313 /**
4314  * gtk_icon_view_set_tooltip_cell:
4315  * @icon_view: a #GtkIconView
4316  * @tooltip: a #GtkTooltip
4317  * @path: a #GtkTreePath
4318  * @cell: (allow-none): a #GtkCellRenderer or %NULL
4319  *
4320  * Sets the tip area of @tooltip to the area which @cell occupies in
4321  * the item pointed to by @path. See also gtk_tooltip_set_tip_area().
4322  *
4323  * See also gtk_icon_view_set_tooltip_column() for a simpler alternative.
4324  *
4325  * Since: 2.12
4326  */
4327 void
4328 gtk_icon_view_set_tooltip_cell (GtkIconView     *icon_view,
4329                                 GtkTooltip      *tooltip,
4330                                 GtkTreePath     *path,
4331                                 GtkCellRenderer *cell)
4332 {
4333   GdkRectangle rect;
4334   GtkIconViewItem *item = NULL;
4335   gint x, y;
4336  
4337   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4338   g_return_if_fail (GTK_IS_TOOLTIP (tooltip));
4339   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
4340
4341   if (gtk_tree_path_get_depth (path) > 0)
4342     item = g_list_nth_data (icon_view->priv->items,
4343                             gtk_tree_path_get_indices(path)[0]);
4344  
4345   if (!item)
4346     return;
4347
4348   if (cell)
4349     {
4350       GtkCellAreaContext *context;
4351
4352       context = g_ptr_array_index (icon_view->priv->row_contexts, item->row);
4353       _gtk_icon_view_set_cell_data (icon_view, item);
4354       gtk_cell_area_get_cell_allocation (icon_view->priv->cell_area, context,
4355                                          GTK_WIDGET (icon_view),
4356                                          cell, (GdkRectangle *)item, &rect);
4357     }
4358   else
4359     {
4360       rect.x = item->cell_area.x - icon_view->priv->item_padding;
4361       rect.y = item->cell_area.y - icon_view->priv->item_padding;
4362       rect.width  = item->cell_area.width  + icon_view->priv->item_padding * 2;
4363       rect.height = item->cell_area.height + icon_view->priv->item_padding * 2;
4364     }
4365   
4366   if (icon_view->priv->bin_window)
4367     {
4368       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4369       rect.x += x;
4370       rect.y += y; 
4371     }
4372
4373   gtk_tooltip_set_tip_area (tooltip, &rect); 
4374 }
4375
4376
4377 /**
4378  * gtk_icon_view_get_tooltip_context:
4379  * @icon_view: an #GtkIconView
4380  * @x: (inout): the x coordinate (relative to widget coordinates)
4381  * @y: (inout): the y coordinate (relative to widget coordinates)
4382  * @keyboard_tip: whether this is a keyboard tooltip or not
4383  * @model: (out) (allow-none): a pointer to receive a #GtkTreeModel or %NULL
4384  * @path: (out) (allow-none): a pointer to receive a #GtkTreePath or %NULL
4385  * @iter: (out) (allow-none): a pointer to receive a #GtkTreeIter or %NULL
4386  *
4387  * This function is supposed to be used in a #GtkWidget::query-tooltip
4388  * signal handler for #GtkIconView.  The @x, @y and @keyboard_tip values
4389  * which are received in the signal handler, should be passed to this
4390  * function without modification.
4391  *
4392  * The return value indicates whether there is an icon view item at the given
4393  * coordinates (%TRUE) or not (%FALSE) for mouse tooltips. For keyboard
4394  * tooltips the item returned will be the cursor item. When %TRUE, then any of
4395  * @model, @path and @iter which have been provided will be set to point to
4396  * that row and the corresponding model. @x and @y will always be converted
4397  * to be relative to @icon_view's bin_window if @keyboard_tooltip is %FALSE.
4398  *
4399  * Return value: whether or not the given tooltip context points to a item
4400  *
4401  * Since: 2.12
4402  */
4403 gboolean
4404 gtk_icon_view_get_tooltip_context (GtkIconView   *icon_view,
4405                                    gint          *x,
4406                                    gint          *y,
4407                                    gboolean       keyboard_tip,
4408                                    GtkTreeModel **model,
4409                                    GtkTreePath  **path,
4410                                    GtkTreeIter   *iter)
4411 {
4412   GtkTreePath *tmppath = NULL;
4413
4414   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4415   g_return_val_if_fail (x != NULL, FALSE);
4416   g_return_val_if_fail (y != NULL, FALSE);
4417
4418   if (keyboard_tip)
4419     {
4420       gtk_icon_view_get_cursor (icon_view, &tmppath, NULL);
4421
4422       if (!tmppath)
4423         return FALSE;
4424     }
4425   else
4426     {
4427       gtk_icon_view_convert_widget_to_bin_window_coords (icon_view, *x, *y,
4428                                                          x, y);
4429
4430       if (!gtk_icon_view_get_item_at_pos (icon_view, *x, *y, &tmppath, NULL))
4431         return FALSE;
4432     }
4433
4434   if (model)
4435     *model = gtk_icon_view_get_model (icon_view);
4436
4437   if (iter)
4438     gtk_tree_model_get_iter (gtk_icon_view_get_model (icon_view),
4439                              iter, tmppath);
4440
4441   if (path)
4442     *path = tmppath;
4443   else
4444     gtk_tree_path_free (tmppath);
4445
4446   return TRUE;
4447 }
4448
4449 static gboolean
4450 gtk_icon_view_set_tooltip_query_cb (GtkWidget  *widget,
4451                                     gint        x,
4452                                     gint        y,
4453                                     gboolean    keyboard_tip,
4454                                     GtkTooltip *tooltip,
4455                                     gpointer    data)
4456 {
4457   gchar *str;
4458   GtkTreeIter iter;
4459   GtkTreePath *path;
4460   GtkTreeModel *model;
4461   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
4462
4463   if (!gtk_icon_view_get_tooltip_context (GTK_ICON_VIEW (widget),
4464                                           &x, &y,
4465                                           keyboard_tip,
4466                                           &model, &path, &iter))
4467     return FALSE;
4468
4469   gtk_tree_model_get (model, &iter, icon_view->priv->tooltip_column, &str, -1);
4470
4471   if (!str)
4472     {
4473       gtk_tree_path_free (path);
4474       return FALSE;
4475     }
4476
4477   gtk_tooltip_set_markup (tooltip, str);
4478   gtk_icon_view_set_tooltip_item (icon_view, tooltip, path);
4479
4480   gtk_tree_path_free (path);
4481   g_free (str);
4482
4483   return TRUE;
4484 }
4485
4486
4487 /**
4488  * gtk_icon_view_set_tooltip_column:
4489  * @icon_view: a #GtkIconView
4490  * @column: an integer, which is a valid column number for @icon_view's model
4491  *
4492  * If you only plan to have simple (text-only) tooltips on full items, you
4493  * can use this function to have #GtkIconView handle these automatically
4494  * for you. @column should be set to the column in @icon_view's model
4495  * containing the tooltip texts, or -1 to disable this feature.
4496  *
4497  * When enabled, #GtkWidget::has-tooltip will be set to %TRUE and
4498  * @icon_view will connect a #GtkWidget::query-tooltip signal handler.
4499  *
4500  * Note that the signal handler sets the text with gtk_tooltip_set_markup(),
4501  * so &amp;, &lt;, etc have to be escaped in the text.
4502  *
4503  * Since: 2.12
4504  */
4505 void
4506 gtk_icon_view_set_tooltip_column (GtkIconView *icon_view,
4507                                   gint         column)
4508 {
4509   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4510
4511   if (column == icon_view->priv->tooltip_column)
4512     return;
4513
4514   if (column == -1)
4515     {
4516       g_signal_handlers_disconnect_by_func (icon_view,
4517                                             gtk_icon_view_set_tooltip_query_cb,
4518                                             NULL);
4519       gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), FALSE);
4520     }
4521   else
4522     {
4523       if (icon_view->priv->tooltip_column == -1)
4524         {
4525           g_signal_connect (icon_view, "query-tooltip",
4526                             G_CALLBACK (gtk_icon_view_set_tooltip_query_cb), NULL);
4527           gtk_widget_set_has_tooltip (GTK_WIDGET (icon_view), TRUE);
4528         }
4529     }
4530
4531   icon_view->priv->tooltip_column = column;
4532   g_object_notify (G_OBJECT (icon_view), "tooltip-column");
4533 }
4534
4535 /**
4536  * gtk_icon_view_get_tooltip_column:
4537  * @icon_view: a #GtkIconView
4538  *
4539  * Returns the column of @icon_view's model which is being used for
4540  * displaying tooltips on @icon_view's rows.
4541  *
4542  * Return value: the index of the tooltip column that is currently being
4543  * used, or -1 if this is disabled.
4544  *
4545  * Since: 2.12
4546  */
4547 gint
4548 gtk_icon_view_get_tooltip_column (GtkIconView *icon_view)
4549 {
4550   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 0);
4551
4552   return icon_view->priv->tooltip_column;
4553 }
4554
4555 /**
4556  * gtk_icon_view_get_visible_range:
4557  * @icon_view: A #GtkIconView
4558  * @start_path: (out) (allow-none): Return location for start of region,
4559  *              or %NULL
4560  * @end_path: (out) (allow-none): Return location for end of region, or %NULL
4561  * 
4562  * Sets @start_path and @end_path to be the first and last visible path. 
4563  * Note that there may be invisible paths in between.
4564  * 
4565  * Both paths should be freed with gtk_tree_path_free() after use.
4566  * 
4567  * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4568  *
4569  * Since: 2.8
4570  **/
4571 gboolean
4572 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
4573                                  GtkTreePath **start_path,
4574                                  GtkTreePath **end_path)
4575 {
4576   gint start_index = -1;
4577   gint end_index = -1;
4578   GList *icons;
4579
4580   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4581
4582   if (icon_view->priv->hadjustment == NULL ||
4583       icon_view->priv->vadjustment == NULL)
4584     return FALSE;
4585
4586   if (start_path == NULL && end_path == NULL)
4587     return FALSE;
4588   
4589   for (icons = icon_view->priv->items; icons; icons = icons->next) 
4590     {
4591       GtkIconViewItem *item = icons->data;
4592       GdkRectangle    *item_area = (GdkRectangle *)item;
4593
4594       if ((item_area->x + item_area->width >= (int)gtk_adjustment_get_value (icon_view->priv->hadjustment)) &&
4595           (item_area->y + item_area->height >= (int)gtk_adjustment_get_value (icon_view->priv->vadjustment)) &&
4596           (item_area->x <= 
4597            (int) (gtk_adjustment_get_value (icon_view->priv->hadjustment) + 
4598                   gtk_adjustment_get_page_size (icon_view->priv->hadjustment))) &&
4599           (item_area->y <= 
4600            (int) (gtk_adjustment_get_value (icon_view->priv->vadjustment) + 
4601                   gtk_adjustment_get_page_size (icon_view->priv->vadjustment))))
4602         {
4603           if (start_index == -1)
4604             start_index = item->index;
4605           end_index = item->index;
4606         }
4607     }
4608
4609   if (start_path && start_index != -1)
4610     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4611   if (end_path && end_index != -1)
4612     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4613   
4614   return start_index != -1;
4615 }
4616
4617 /**
4618  * gtk_icon_view_selected_foreach:
4619  * @icon_view: A #GtkIconView.
4620  * @func: (scope call): The function to call for each selected icon.
4621  * @data: User data to pass to the function.
4622  * 
4623  * Calls a function for each selected icon. Note that the model or
4624  * selection cannot be modified from within this function.
4625  *
4626  * Since: 2.6 
4627  **/
4628 void
4629 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
4630                                 GtkIconViewForeachFunc func,
4631                                 gpointer               data)
4632 {
4633   GList *list;
4634   
4635   for (list = icon_view->priv->items; list; list = list->next)
4636     {
4637       GtkIconViewItem *item = list->data;
4638       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4639
4640       if (item->selected)
4641         (* func) (icon_view, path, data);
4642
4643       gtk_tree_path_free (path);
4644     }
4645 }
4646
4647 /**
4648  * gtk_icon_view_set_selection_mode:
4649  * @icon_view: A #GtkIconView.
4650  * @mode: The selection mode
4651  * 
4652  * Sets the selection mode of the @icon_view.
4653  *
4654  * Since: 2.6 
4655  **/
4656 void
4657 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
4658                                   GtkSelectionMode  mode)
4659 {
4660   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4661
4662   if (mode == icon_view->priv->selection_mode)
4663     return;
4664   
4665   if (mode == GTK_SELECTION_NONE ||
4666       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4667     gtk_icon_view_unselect_all (icon_view);
4668   
4669   icon_view->priv->selection_mode = mode;
4670
4671   g_object_notify (G_OBJECT (icon_view), "selection-mode");
4672 }
4673
4674 /**
4675  * gtk_icon_view_get_selection_mode:
4676  * @icon_view: A #GtkIconView.
4677  * 
4678  * Gets the selection mode of the @icon_view.
4679  *
4680  * Return value: the current selection mode
4681  *
4682  * Since: 2.6 
4683  **/
4684 GtkSelectionMode
4685 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4686 {
4687   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4688
4689   return icon_view->priv->selection_mode;
4690 }
4691
4692 /**
4693  * gtk_icon_view_set_model:
4694  * @icon_view: A #GtkIconView.
4695  * @model: (allow-none): The model.
4696  *
4697  * Sets the model for a #GtkIconView.
4698  * If the @icon_view already has a model set, it will remove
4699  * it before setting the new model.  If @model is %NULL, then
4700  * it will unset the old model.
4701  *
4702  * Since: 2.6 
4703  **/
4704 void
4705 gtk_icon_view_set_model (GtkIconView *icon_view,
4706                          GtkTreeModel *model)
4707 {
4708   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4709   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4710   
4711   if (icon_view->priv->model == model)
4712     return;
4713
4714   if (icon_view->priv->scroll_to_path)
4715     {
4716       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4717       icon_view->priv->scroll_to_path = NULL;
4718     }
4719
4720   /* The area can be NULL while disposing */
4721   if (icon_view->priv->cell_area)
4722     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4723
4724   if (model)
4725     {
4726       GType column_type;
4727
4728       if (icon_view->priv->pixbuf_column != -1)
4729         {
4730           column_type = gtk_tree_model_get_column_type (model,
4731                                                         icon_view->priv->pixbuf_column);          
4732
4733           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4734         }
4735
4736       if (icon_view->priv->text_column != -1)
4737         {
4738           column_type = gtk_tree_model_get_column_type (model,
4739                                                         icon_view->priv->text_column);    
4740
4741           g_return_if_fail (column_type == G_TYPE_STRING);
4742         }
4743
4744       if (icon_view->priv->markup_column != -1)
4745         {
4746           column_type = gtk_tree_model_get_column_type (model,
4747                                                         icon_view->priv->markup_column);          
4748
4749           g_return_if_fail (column_type == G_TYPE_STRING);
4750         }
4751       
4752     }
4753   
4754   if (icon_view->priv->model)
4755     {
4756       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4757                                             gtk_icon_view_row_changed,
4758                                             icon_view);
4759       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4760                                             gtk_icon_view_row_inserted,
4761                                             icon_view);
4762       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4763                                             gtk_icon_view_row_deleted,
4764                                             icon_view);
4765       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4766                                             gtk_icon_view_rows_reordered,
4767                                             icon_view);
4768
4769       g_object_unref (icon_view->priv->model);
4770       
4771       g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
4772       g_list_free (icon_view->priv->items);
4773       icon_view->priv->items = NULL;
4774       icon_view->priv->anchor_item = NULL;
4775       icon_view->priv->cursor_item = NULL;
4776       icon_view->priv->last_single_clicked = NULL;
4777       icon_view->priv->last_prelight = NULL;
4778       icon_view->priv->width = 0;
4779       icon_view->priv->height = 0;
4780     }
4781
4782   icon_view->priv->model = model;
4783
4784   if (icon_view->priv->model)
4785     {
4786       g_object_ref (icon_view->priv->model);
4787       g_signal_connect (icon_view->priv->model,
4788                         "row-changed",
4789                         G_CALLBACK (gtk_icon_view_row_changed),
4790                         icon_view);
4791       g_signal_connect (icon_view->priv->model,
4792                         "row-inserted",
4793                         G_CALLBACK (gtk_icon_view_row_inserted),
4794                         icon_view);
4795       g_signal_connect (icon_view->priv->model,
4796                         "row-deleted",
4797                         G_CALLBACK (gtk_icon_view_row_deleted),
4798                         icon_view);
4799       g_signal_connect (icon_view->priv->model,
4800                         "rows-reordered",
4801                         G_CALLBACK (gtk_icon_view_rows_reordered),
4802                         icon_view);
4803
4804       gtk_icon_view_build_items (icon_view);
4805
4806       gtk_icon_view_layout (icon_view);
4807     }
4808
4809   g_object_notify (G_OBJECT (icon_view), "model");  
4810
4811   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
4812     gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4813 }
4814
4815 /**
4816  * gtk_icon_view_get_model:
4817  * @icon_view: a #GtkIconView
4818  *
4819  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
4820  * model is unset.
4821  *
4822  * Return value: (transfer none): A #GtkTreeModel, or %NULL if none is
4823  *     currently being used.
4824  *
4825  * Since: 2.6 
4826  **/
4827 GtkTreeModel *
4828 gtk_icon_view_get_model (GtkIconView *icon_view)
4829 {
4830   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4831
4832   return icon_view->priv->model;
4833 }
4834
4835 static void
4836 update_text_cell (GtkIconView *icon_view)
4837 {
4838   if (!icon_view->priv->cell_area)
4839     return;
4840
4841   if (icon_view->priv->text_column == -1 &&
4842       icon_view->priv->markup_column == -1)
4843     {
4844       if (icon_view->priv->text_cell != NULL)
4845         {
4846           gtk_cell_area_remove (icon_view->priv->cell_area, 
4847                                 icon_view->priv->text_cell);
4848           icon_view->priv->text_cell = NULL;
4849         }
4850     }
4851   else 
4852     {
4853       if (icon_view->priv->text_cell == NULL)
4854         {
4855           icon_view->priv->text_cell = gtk_cell_renderer_text_new ();
4856
4857           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), icon_view->priv->text_cell, FALSE);
4858         }
4859
4860       if (icon_view->priv->markup_column != -1)
4861         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4862                                         icon_view->priv->text_cell, 
4863                                         "markup", icon_view->priv->markup_column, 
4864                                         NULL);
4865       else
4866         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4867                                         icon_view->priv->text_cell, 
4868                                         "text", icon_view->priv->text_column, 
4869                                         NULL);
4870
4871       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
4872         g_object_set (icon_view->priv->text_cell,
4873                       "alignment", PANGO_ALIGN_CENTER,
4874                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
4875                       "xalign", 0.5,
4876                       "yalign", 0.0,
4877                       NULL);
4878       else
4879         g_object_set (icon_view->priv->text_cell,
4880                       "alignment", PANGO_ALIGN_LEFT,
4881                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
4882                       "xalign", 0.0,
4883                       "yalign", 0.5,
4884                       NULL);
4885     }
4886 }
4887
4888 static void
4889 update_pixbuf_cell (GtkIconView *icon_view)
4890 {
4891   if (!icon_view->priv->cell_area)
4892     return;
4893
4894   if (icon_view->priv->pixbuf_column == -1)
4895     {
4896       if (icon_view->priv->pixbuf_cell != NULL)
4897         {
4898           gtk_cell_area_remove (icon_view->priv->cell_area, 
4899                                 icon_view->priv->pixbuf_cell);
4900
4901           icon_view->priv->pixbuf_cell = NULL;
4902         }
4903     }
4904   else 
4905     {
4906       if (icon_view->priv->pixbuf_cell == NULL)
4907         {
4908           icon_view->priv->pixbuf_cell = gtk_cell_renderer_pixbuf_new ();
4909           
4910           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), icon_view->priv->pixbuf_cell, FALSE);
4911         }
4912       
4913       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4914                                       icon_view->priv->pixbuf_cell, 
4915                                       "pixbuf", icon_view->priv->pixbuf_column, 
4916                                       NULL);
4917
4918       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
4919         g_object_set (icon_view->priv->pixbuf_cell,
4920                       "xalign", 0.5,
4921                       "yalign", 1.0,
4922                       NULL);
4923       else
4924         g_object_set (icon_view->priv->pixbuf_cell,
4925                       "xalign", 0.0,
4926                       "yalign", 0.0,
4927                       NULL);
4928     }
4929 }
4930
4931 /**
4932  * gtk_icon_view_set_text_column:
4933  * @icon_view: A #GtkIconView.
4934  * @column: A column in the currently used model, or -1 to display no text
4935  * 
4936  * Sets the column with text for @icon_view to be @column. The text
4937  * column must be of type #G_TYPE_STRING.
4938  *
4939  * Since: 2.6 
4940  **/
4941 void
4942 gtk_icon_view_set_text_column (GtkIconView *icon_view,
4943                                gint          column)
4944 {
4945   if (column == icon_view->priv->text_column)
4946     return;
4947   
4948   if (column == -1)
4949     icon_view->priv->text_column = -1;
4950   else
4951     {
4952       if (icon_view->priv->model != NULL)
4953         {
4954           GType column_type;
4955           
4956           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4957
4958           g_return_if_fail (column_type == G_TYPE_STRING);
4959         }
4960       
4961       icon_view->priv->text_column = column;
4962     }
4963
4964   if (icon_view->priv->cell_area)
4965     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4966
4967   update_text_cell (icon_view);
4968
4969   gtk_icon_view_invalidate_sizes (icon_view);
4970   
4971   g_object_notify (G_OBJECT (icon_view), "text-column");
4972 }
4973
4974 /**
4975  * gtk_icon_view_get_text_column:
4976  * @icon_view: A #GtkIconView.
4977  *
4978  * Returns the column with text for @icon_view.
4979  *
4980  * Returns: the text column, or -1 if it's unset.
4981  *
4982  * Since: 2.6
4983  */
4984 gint
4985 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
4986 {
4987   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4988
4989   return icon_view->priv->text_column;
4990 }
4991
4992 /**
4993  * gtk_icon_view_set_markup_column:
4994  * @icon_view: A #GtkIconView.
4995  * @column: A column in the currently used model, or -1 to display no text
4996  * 
4997  * Sets the column with markup information for @icon_view to be
4998  * @column. The markup column must be of type #G_TYPE_STRING.
4999  * If the markup column is set to something, it overrides
5000  * the text column set by gtk_icon_view_set_text_column().
5001  *
5002  * Since: 2.6
5003  **/
5004 void
5005 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5006                                  gint         column)
5007 {
5008   if (column == icon_view->priv->markup_column)
5009     return;
5010   
5011   if (column == -1)
5012     icon_view->priv->markup_column = -1;
5013   else
5014     {
5015       if (icon_view->priv->model != NULL)
5016         {
5017           GType column_type;
5018           
5019           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5020
5021           g_return_if_fail (column_type == G_TYPE_STRING);
5022         }
5023       
5024       icon_view->priv->markup_column = column;
5025     }
5026
5027   if (icon_view->priv->cell_area)
5028     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5029
5030   update_text_cell (icon_view);
5031
5032   gtk_icon_view_invalidate_sizes (icon_view);
5033   
5034   g_object_notify (G_OBJECT (icon_view), "markup-column");
5035 }
5036
5037 /**
5038  * gtk_icon_view_get_markup_column:
5039  * @icon_view: A #GtkIconView.
5040  *
5041  * Returns the column with markup text for @icon_view.
5042  *
5043  * Returns: the markup column, or -1 if it's unset.
5044  *
5045  * Since: 2.6
5046  */
5047 gint
5048 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
5049 {
5050   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5051
5052   return icon_view->priv->markup_column;
5053 }
5054
5055 /**
5056  * gtk_icon_view_set_pixbuf_column:
5057  * @icon_view: A #GtkIconView.
5058  * @column: A column in the currently used model, or -1 to disable
5059  * 
5060  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5061  * column must be of type #GDK_TYPE_PIXBUF
5062  *
5063  * Since: 2.6 
5064  **/
5065 void
5066 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5067                                  gint         column)
5068 {
5069   if (column == icon_view->priv->pixbuf_column)
5070     return;
5071   
5072   if (column == -1)
5073     icon_view->priv->pixbuf_column = -1;
5074   else
5075     {
5076       if (icon_view->priv->model != NULL)
5077         {
5078           GType column_type;
5079           
5080           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5081
5082           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5083         }
5084       
5085       icon_view->priv->pixbuf_column = column;
5086     }
5087
5088   if (icon_view->priv->cell_area)
5089     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5090
5091   update_pixbuf_cell (icon_view);
5092
5093   gtk_icon_view_invalidate_sizes (icon_view);
5094   
5095   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5096   
5097 }
5098
5099 /**
5100  * gtk_icon_view_get_pixbuf_column:
5101  * @icon_view: A #GtkIconView.
5102  *
5103  * Returns the column with pixbufs for @icon_view.
5104  *
5105  * Returns: the pixbuf column, or -1 if it's unset.
5106  *
5107  * Since: 2.6
5108  */
5109 gint
5110 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
5111 {
5112   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5113
5114   return icon_view->priv->pixbuf_column;
5115 }
5116
5117 /**
5118  * gtk_icon_view_select_path:
5119  * @icon_view: A #GtkIconView.
5120  * @path: The #GtkTreePath to be selected.
5121  * 
5122  * Selects the row at @path.
5123  *
5124  * Since: 2.6
5125  **/
5126 void
5127 gtk_icon_view_select_path (GtkIconView *icon_view,
5128                            GtkTreePath *path)
5129 {
5130   GtkIconViewItem *item = NULL;
5131
5132   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5133   g_return_if_fail (icon_view->priv->model != NULL);
5134   g_return_if_fail (path != NULL);
5135
5136   if (gtk_tree_path_get_depth (path) > 0)
5137     item = g_list_nth_data (icon_view->priv->items,
5138                             gtk_tree_path_get_indices(path)[0]);
5139
5140   if (item)
5141     _gtk_icon_view_select_item (icon_view, item);
5142 }
5143
5144 /**
5145  * gtk_icon_view_unselect_path:
5146  * @icon_view: A #GtkIconView.
5147  * @path: The #GtkTreePath to be unselected.
5148  * 
5149  * Unselects the row at @path.
5150  *
5151  * Since: 2.6
5152  **/
5153 void
5154 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5155                              GtkTreePath *path)
5156 {
5157   GtkIconViewItem *item;
5158   
5159   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5160   g_return_if_fail (icon_view->priv->model != NULL);
5161   g_return_if_fail (path != NULL);
5162
5163   item = g_list_nth_data (icon_view->priv->items,
5164                           gtk_tree_path_get_indices(path)[0]);
5165
5166   if (!item)
5167     return;
5168   
5169   _gtk_icon_view_unselect_item (icon_view, item);
5170 }
5171
5172 /**
5173  * gtk_icon_view_get_selected_items:
5174  * @icon_view: A #GtkIconView.
5175  *
5176  * Creates a list of paths of all selected items. Additionally, if you are
5177  * planning on modifying the model after calling this function, you may
5178  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5179  * To do this, you can use gtk_tree_row_reference_new().
5180  *
5181  * To free the return value, use:
5182  * |[
5183  * g_list_foreach (list, (GFunc)gtk_tree_path_free, NULL);
5184  * g_list_free (list);
5185  * ]|
5186  *
5187  * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
5188  *
5189  * Since: 2.6
5190  **/
5191 GList *
5192 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5193 {
5194   GList *list;
5195   GList *selected = NULL;
5196   
5197   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5198   
5199   for (list = icon_view->priv->items; list != NULL; list = list->next)
5200     {
5201       GtkIconViewItem *item = list->data;
5202
5203       if (item->selected)
5204         {
5205           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5206
5207           selected = g_list_prepend (selected, path);
5208         }
5209     }
5210
5211   return selected;
5212 }
5213
5214 /**
5215  * gtk_icon_view_select_all:
5216  * @icon_view: A #GtkIconView.
5217  * 
5218  * Selects all the icons. @icon_view must has its selection mode set
5219  * to #GTK_SELECTION_MULTIPLE.
5220  *
5221  * Since: 2.6
5222  **/
5223 void
5224 gtk_icon_view_select_all (GtkIconView *icon_view)
5225 {
5226   GList *items;
5227   gboolean dirty = FALSE;
5228   
5229   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5230
5231   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5232     return;
5233
5234   for (items = icon_view->priv->items; items; items = items->next)
5235     {
5236       GtkIconViewItem *item = items->data;
5237       
5238       if (!item->selected)
5239         {
5240           dirty = TRUE;
5241           item->selected = TRUE;
5242           gtk_icon_view_queue_draw_item (icon_view, item);
5243         }
5244     }
5245
5246   if (dirty)
5247     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5248 }
5249
5250 /**
5251  * gtk_icon_view_unselect_all:
5252  * @icon_view: A #GtkIconView.
5253  * 
5254  * Unselects all the icons.
5255  *
5256  * Since: 2.6
5257  **/
5258 void
5259 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5260 {
5261   gboolean dirty = FALSE;
5262   
5263   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5264
5265   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5266     return;
5267
5268   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5269
5270   if (dirty)
5271     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5272 }
5273
5274 /**
5275  * gtk_icon_view_path_is_selected:
5276  * @icon_view: A #GtkIconView.
5277  * @path: A #GtkTreePath to check selection on.
5278  * 
5279  * Returns %TRUE if the icon pointed to by @path is currently
5280  * selected. If @path does not point to a valid location, %FALSE is returned.
5281  * 
5282  * Return value: %TRUE if @path is selected.
5283  *
5284  * Since: 2.6
5285  **/
5286 gboolean
5287 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5288                                 GtkTreePath *path)
5289 {
5290   GtkIconViewItem *item;
5291   
5292   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5293   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5294   g_return_val_if_fail (path != NULL, FALSE);
5295   
5296   item = g_list_nth_data (icon_view->priv->items,
5297                           gtk_tree_path_get_indices(path)[0]);
5298
5299   if (!item)
5300     return FALSE;
5301   
5302   return item->selected;
5303 }
5304
5305 /**
5306  * gtk_icon_view_get_item_row:
5307  * @icon_view: a #GtkIconView
5308  * @path: the #GtkTreePath of the item
5309  *
5310  * Gets the row in which the item @path is currently
5311  * displayed. Row numbers start at 0.
5312  *
5313  * Returns: The row in which the item is displayed
5314  *
5315  * Since: 2.22
5316  */
5317 gint
5318 gtk_icon_view_get_item_row (GtkIconView *icon_view,
5319                             GtkTreePath *path)
5320 {
5321   GtkIconViewItem *item;
5322
5323   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5324   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5325   g_return_val_if_fail (path != NULL, -1);
5326
5327   item = g_list_nth_data (icon_view->priv->items,
5328                           gtk_tree_path_get_indices(path)[0]);
5329
5330   if (!item)
5331     return -1;
5332
5333   return item->row;
5334 }
5335
5336 /**
5337  * gtk_icon_view_get_item_column:
5338  * @icon_view: a #GtkIconView
5339  * @path: the #GtkTreePath of the item
5340  *
5341  * Gets the column in which the item @path is currently
5342  * displayed. Column numbers start at 0.
5343  *
5344  * Returns: The column in which the item is displayed
5345  *
5346  * Since: 2.22
5347  */
5348 gint
5349 gtk_icon_view_get_item_column (GtkIconView *icon_view,
5350                                GtkTreePath *path)
5351 {
5352   GtkIconViewItem *item;
5353
5354   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5355   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5356   g_return_val_if_fail (path != NULL, -1);
5357
5358   item = g_list_nth_data (icon_view->priv->items,
5359                           gtk_tree_path_get_indices(path)[0]);
5360
5361   if (!item)
5362     return -1;
5363
5364   return item->col;
5365 }
5366
5367 /**
5368  * gtk_icon_view_item_activated:
5369  * @icon_view: A #GtkIconView
5370  * @path: The #GtkTreePath to be activated
5371  * 
5372  * Activates the item determined by @path.
5373  *
5374  * Since: 2.6
5375  **/
5376 void
5377 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5378                               GtkTreePath      *path)
5379 {
5380   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5381   g_return_if_fail (path != NULL);
5382   
5383   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5384 }
5385
5386 /**
5387  * gtk_icon_view_set_item_orientation:
5388  * @icon_view: a #GtkIconView
5389  * @orientation: the relative position of texts and icons 
5390  * 
5391  * Sets the ::item-orientation property which determines whether the labels 
5392  * are drawn beside the icons instead of below.
5393  *
5394  * Since: 2.6
5395  **/
5396 void 
5397 gtk_icon_view_set_item_orientation (GtkIconView    *icon_view,
5398                                     GtkOrientation  orientation)
5399 {
5400   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5401
5402   if (icon_view->priv->item_orientation != orientation)
5403     {
5404       icon_view->priv->item_orientation = orientation;
5405
5406       if (icon_view->priv->cell_area)
5407         {
5408           if (GTK_IS_ORIENTABLE (icon_view->priv->cell_area))
5409             gtk_orientable_set_orientation (GTK_ORIENTABLE (icon_view->priv->cell_area), 
5410                                             icon_view->priv->item_orientation);
5411
5412           gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5413         }
5414
5415       gtk_icon_view_invalidate_sizes (icon_view);
5416
5417       update_text_cell (icon_view);
5418       update_pixbuf_cell (icon_view);
5419       
5420       g_object_notify (G_OBJECT (icon_view), "item-orientation");
5421     }
5422 }
5423
5424 /**
5425  * gtk_icon_view_get_item_orientation:
5426  * @icon_view: a #GtkIconView
5427  * 
5428  * Returns the value of the ::item-orientation property which determines 
5429  * whether the labels are drawn beside the icons instead of below. 
5430  * 
5431  * Return value: the relative position of texts and icons 
5432  *
5433  * Since: 2.6
5434  **/
5435 GtkOrientation
5436 gtk_icon_view_get_item_orientation (GtkIconView *icon_view)
5437 {
5438   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5439                         GTK_ORIENTATION_VERTICAL);
5440
5441   return icon_view->priv->item_orientation;
5442 }
5443
5444 /**
5445  * gtk_icon_view_set_columns:
5446  * @icon_view: a #GtkIconView
5447  * @columns: the number of columns
5448  * 
5449  * Sets the ::columns property which determines in how
5450  * many columns the icons are arranged. If @columns is
5451  * -1, the number of columns will be chosen automatically 
5452  * to fill the available area. 
5453  *
5454  * Since: 2.6
5455  */
5456 void 
5457 gtk_icon_view_set_columns (GtkIconView *icon_view,
5458                            gint         columns)
5459 {
5460   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5461   
5462   if (icon_view->priv->columns != columns)
5463     {
5464       icon_view->priv->columns = columns;
5465
5466       if (icon_view->priv->cell_area)
5467         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5468
5469       gtk_icon_view_queue_layout (icon_view);
5470       
5471       g_object_notify (G_OBJECT (icon_view), "columns");
5472     }  
5473 }
5474
5475 /**
5476  * gtk_icon_view_get_columns:
5477  * @icon_view: a #GtkIconView
5478  * 
5479  * Returns the value of the ::columns property.
5480  * 
5481  * Return value: the number of columns, or -1
5482  *
5483  * Since: 2.6
5484  */
5485 gint
5486 gtk_icon_view_get_columns (GtkIconView *icon_view)
5487 {
5488   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5489
5490   return icon_view->priv->columns;
5491 }
5492
5493 /**
5494  * gtk_icon_view_set_item_width:
5495  * @icon_view: a #GtkIconView
5496  * @item_width: the width for each item
5497  * 
5498  * Sets the ::item-width property which specifies the width 
5499  * to use for each item. If it is set to -1, the icon view will 
5500  * automatically determine a suitable item size.
5501  *
5502  * Since: 2.6
5503  */
5504 void 
5505 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5506                               gint         item_width)
5507 {
5508   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5509   
5510   if (icon_view->priv->item_width != item_width)
5511     {
5512       icon_view->priv->item_width = item_width;
5513       
5514       if (icon_view->priv->cell_area)
5515         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5516
5517       gtk_icon_view_invalidate_sizes (icon_view);
5518       
5519       update_text_cell (icon_view);
5520
5521       g_object_notify (G_OBJECT (icon_view), "item-width");
5522     }  
5523 }
5524
5525 /**
5526  * gtk_icon_view_get_item_width:
5527  * @icon_view: a #GtkIconView
5528  * 
5529  * Returns the value of the ::item-width property.
5530  * 
5531  * Return value: the width of a single item, or -1
5532  *
5533  * Since: 2.6
5534  */
5535 gint
5536 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5537 {
5538   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5539
5540   return icon_view->priv->item_width;
5541 }
5542
5543
5544 /**
5545  * gtk_icon_view_set_spacing:
5546  * @icon_view: a #GtkIconView
5547  * @spacing: the spacing
5548  * 
5549  * Sets the ::spacing property which specifies the space 
5550  * which is inserted between the cells (i.e. the icon and 
5551  * the text) of an item.
5552  *
5553  * Since: 2.6
5554  */
5555 void 
5556 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5557                            gint         spacing)
5558 {
5559   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5560   
5561   if (icon_view->priv->spacing != spacing)
5562     {
5563       icon_view->priv->spacing = spacing;
5564
5565       if (icon_view->priv->cell_area)
5566         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5567
5568       gtk_icon_view_invalidate_sizes (icon_view);
5569
5570       g_object_notify (G_OBJECT (icon_view), "spacing");
5571     }  
5572 }
5573
5574 /**
5575  * gtk_icon_view_get_spacing:
5576  * @icon_view: a #GtkIconView
5577  * 
5578  * Returns the value of the ::spacing property.
5579  * 
5580  * Return value: the space between cells 
5581  *
5582  * Since: 2.6
5583  */
5584 gint
5585 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5586 {
5587   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5588
5589   return icon_view->priv->spacing;
5590 }
5591
5592 /**
5593  * gtk_icon_view_set_row_spacing:
5594  * @icon_view: a #GtkIconView
5595  * @row_spacing: the row spacing
5596  * 
5597  * Sets the ::row-spacing property which specifies the space 
5598  * which is inserted between the rows of the icon view.
5599  *
5600  * Since: 2.6
5601  */
5602 void 
5603 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5604                                gint         row_spacing)
5605 {
5606   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5607   
5608   if (icon_view->priv->row_spacing != row_spacing)
5609     {
5610       icon_view->priv->row_spacing = row_spacing;
5611
5612       if (icon_view->priv->cell_area)
5613         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5614
5615       gtk_icon_view_invalidate_sizes (icon_view);
5616
5617       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5618     }  
5619 }
5620
5621 /**
5622  * gtk_icon_view_get_row_spacing:
5623  * @icon_view: a #GtkIconView
5624  * 
5625  * Returns the value of the ::row-spacing property.
5626  * 
5627  * Return value: the space between rows
5628  *
5629  * Since: 2.6
5630  */
5631 gint
5632 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5633 {
5634   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5635
5636   return icon_view->priv->row_spacing;
5637 }
5638
5639 /**
5640  * gtk_icon_view_set_column_spacing:
5641  * @icon_view: a #GtkIconView
5642  * @column_spacing: the column spacing
5643  * 
5644  * Sets the ::column-spacing property which specifies the space 
5645  * which is inserted between the columns of the icon view.
5646  *
5647  * Since: 2.6
5648  */
5649 void 
5650 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5651                                   gint         column_spacing)
5652 {
5653   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5654   
5655   if (icon_view->priv->column_spacing != column_spacing)
5656     {
5657       icon_view->priv->column_spacing = column_spacing;
5658
5659       if (icon_view->priv->cell_area)
5660         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5661
5662       gtk_icon_view_invalidate_sizes (icon_view);
5663
5664       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5665     }  
5666 }
5667
5668 /**
5669  * gtk_icon_view_get_column_spacing:
5670  * @icon_view: a #GtkIconView
5671  * 
5672  * Returns the value of the ::column-spacing property.
5673  * 
5674  * Return value: the space between columns
5675  *
5676  * Since: 2.6
5677  */
5678 gint
5679 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5680 {
5681   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5682
5683   return icon_view->priv->column_spacing;
5684 }
5685
5686 /**
5687  * gtk_icon_view_set_margin:
5688  * @icon_view: a #GtkIconView
5689  * @margin: the margin
5690  * 
5691  * Sets the ::margin property which specifies the space 
5692  * which is inserted at the top, bottom, left and right 
5693  * of the icon view.
5694  *
5695  * Since: 2.6
5696  */
5697 void 
5698 gtk_icon_view_set_margin (GtkIconView *icon_view,
5699                           gint         margin)
5700 {
5701   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5702   
5703   if (icon_view->priv->margin != margin)
5704     {
5705       icon_view->priv->margin = margin;
5706
5707       if (icon_view->priv->cell_area)
5708         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5709
5710       gtk_icon_view_invalidate_sizes (icon_view);
5711
5712       g_object_notify (G_OBJECT (icon_view), "margin");
5713     }  
5714 }
5715
5716 /**
5717  * gtk_icon_view_get_margin:
5718  * @icon_view: a #GtkIconView
5719  * 
5720  * Returns the value of the ::margin property.
5721  * 
5722  * Return value: the space at the borders 
5723  *
5724  * Since: 2.6
5725  */
5726 gint
5727 gtk_icon_view_get_margin (GtkIconView *icon_view)
5728 {
5729   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5730
5731   return icon_view->priv->margin;
5732 }
5733
5734 /**
5735  * gtk_icon_view_set_item_padding:
5736  * @icon_view: a #GtkIconView
5737  * @item_padding: the item padding
5738  *
5739  * Sets the #GtkIconView:item-padding property which specifies the padding
5740  * around each of the icon view's items.
5741  *
5742  * Since: 2.18
5743  */
5744 void
5745 gtk_icon_view_set_item_padding (GtkIconView *icon_view,
5746                                 gint         item_padding)
5747 {
5748   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5749   
5750   if (icon_view->priv->item_padding != item_padding)
5751     {
5752       icon_view->priv->item_padding = item_padding;
5753
5754       if (icon_view->priv->cell_area)
5755         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5756
5757       gtk_icon_view_invalidate_sizes (icon_view);
5758
5759       g_object_notify (G_OBJECT (icon_view), "item-padding");
5760     }  
5761 }
5762
5763 /**
5764  * gtk_icon_view_get_item_padding:
5765  * @icon_view: a #GtkIconView
5766  * 
5767  * Returns the value of the ::item-padding property.
5768  * 
5769  * Return value: the padding around items
5770  *
5771  * Since: 2.18
5772  */
5773 gint
5774 gtk_icon_view_get_item_padding (GtkIconView *icon_view)
5775 {
5776   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5777
5778   return icon_view->priv->item_padding;
5779 }
5780
5781 /* Get/set whether drag_motion requested the drag data and
5782  * drag_data_received should thus not actually insert the data,
5783  * since the data doesn't result from a drop.
5784  */
5785 static void
5786 set_status_pending (GdkDragContext *context,
5787                     GdkDragAction   suggested_action)
5788 {
5789   g_object_set_data (G_OBJECT (context),
5790                      I_("gtk-icon-view-status-pending"),
5791                      GINT_TO_POINTER (suggested_action));
5792 }
5793
5794 static GdkDragAction
5795 get_status_pending (GdkDragContext *context)
5796 {
5797   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5798                                              "gtk-icon-view-status-pending"));
5799 }
5800
5801 static void
5802 unset_reorderable (GtkIconView *icon_view)
5803 {
5804   if (icon_view->priv->reorderable)
5805     {
5806       icon_view->priv->reorderable = FALSE;
5807       g_object_notify (G_OBJECT (icon_view), "reorderable");
5808     }
5809 }
5810
5811 static void
5812 set_source_row (GdkDragContext *context,
5813                 GtkTreeModel   *model,
5814                 GtkTreePath    *source_row)
5815 {
5816   if (source_row)
5817     g_object_set_data_full (G_OBJECT (context),
5818                             I_("gtk-icon-view-source-row"),
5819                             gtk_tree_row_reference_new (model, source_row),
5820                             (GDestroyNotify) gtk_tree_row_reference_free);
5821   else
5822     g_object_set_data_full (G_OBJECT (context),
5823                             I_("gtk-icon-view-source-row"),
5824                             NULL, NULL);
5825 }
5826
5827 static GtkTreePath*
5828 get_source_row (GdkDragContext *context)
5829 {
5830   GtkTreeRowReference *ref;
5831
5832   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5833
5834   if (ref)
5835     return gtk_tree_row_reference_get_path (ref);
5836   else
5837     return NULL;
5838 }
5839
5840 typedef struct
5841 {
5842   GtkTreeRowReference *dest_row;
5843   gboolean             empty_view_drop;
5844   gboolean             drop_append_mode;
5845 } DestRow;
5846
5847 static void
5848 dest_row_free (gpointer data)
5849 {
5850   DestRow *dr = (DestRow *)data;
5851
5852   gtk_tree_row_reference_free (dr->dest_row);
5853   g_free (dr);
5854 }
5855
5856 static void
5857 set_dest_row (GdkDragContext *context,
5858               GtkTreeModel   *model,
5859               GtkTreePath    *dest_row,
5860               gboolean        empty_view_drop,
5861               gboolean        drop_append_mode)
5862 {
5863   DestRow *dr;
5864
5865   if (!dest_row)
5866     {
5867       g_object_set_data_full (G_OBJECT (context),
5868                               I_("gtk-icon-view-dest-row"),
5869                               NULL, NULL);
5870       return;
5871     }
5872   
5873   dr = g_new0 (DestRow, 1);
5874      
5875   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5876   dr->empty_view_drop = empty_view_drop;
5877   dr->drop_append_mode = drop_append_mode;
5878   g_object_set_data_full (G_OBJECT (context),
5879                           I_("gtk-icon-view-dest-row"),
5880                           dr, (GDestroyNotify) dest_row_free);
5881 }
5882
5883 static GtkTreePath*
5884 get_dest_row (GdkDragContext *context)
5885 {
5886   DestRow *dr;
5887
5888   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
5889
5890   if (dr)
5891     {
5892       GtkTreePath *path = NULL;
5893       
5894       if (dr->dest_row)
5895         path = gtk_tree_row_reference_get_path (dr->dest_row);
5896       else if (dr->empty_view_drop)
5897         path = gtk_tree_path_new_from_indices (0, -1);
5898       else
5899         path = NULL;
5900
5901       if (path && dr->drop_append_mode)
5902         gtk_tree_path_next (path);
5903
5904       return path;
5905     }
5906   else
5907     return NULL;
5908 }
5909
5910 static gboolean
5911 check_model_dnd (GtkTreeModel *model,
5912                  GType         required_iface,
5913                  const gchar  *signal)
5914 {
5915   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5916     {
5917       g_warning ("You must override the default '%s' handler "
5918                  "on GtkIconView when using models that don't support "
5919                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5920                  "is to connect to '%s' and call "
5921                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5922                  "the default handler from running. Look at the source code "
5923                  "for the default handler in gtkiconview.c to get an idea what "
5924                  "your handler should do. (gtkiconview.c is in the GTK+ source "
5925                  "code.) If you're using GTK+ from a language other than C, "
5926                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5927                  signal, g_type_name (required_iface), signal);
5928       return FALSE;
5929     }
5930   else
5931     return TRUE;
5932 }
5933
5934 static void
5935 remove_scroll_timeout (GtkIconView *icon_view)
5936 {
5937   if (icon_view->priv->scroll_timeout_id != 0)
5938     {
5939       g_source_remove (icon_view->priv->scroll_timeout_id);
5940
5941       icon_view->priv->scroll_timeout_id = 0;
5942     }
5943 }
5944
5945 static void
5946 gtk_icon_view_autoscroll (GtkIconView *icon_view,
5947                           GdkDevice   *device)
5948 {
5949   GdkWindow *window;
5950   gint px, py, x, y, width, height;
5951   gint hoffset, voffset;
5952
5953   window = gtk_widget_get_window (GTK_WIDGET (icon_view));
5954
5955   gdk_window_get_device_position (window, device, &px, &py, NULL);
5956   gdk_window_get_geometry (window, &x, &y, &width, &height);
5957
5958   /* see if we are near the edge. */
5959   voffset = py - (y + 2 * SCROLL_EDGE_SIZE);
5960   if (voffset > 0)
5961     voffset = MAX (py - (y + height - 2 * SCROLL_EDGE_SIZE), 0);
5962
5963   hoffset = px - (x + 2 * SCROLL_EDGE_SIZE);
5964   if (hoffset > 0)
5965     hoffset = MAX (px - (x + width - 2 * SCROLL_EDGE_SIZE), 0);
5966
5967   if (voffset != 0)
5968     gtk_adjustment_set_value (icon_view->priv->vadjustment,
5969                               gtk_adjustment_get_value (icon_view->priv->vadjustment) + voffset);
5970
5971   if (hoffset != 0)
5972     gtk_adjustment_set_value (icon_view->priv->hadjustment,
5973                               gtk_adjustment_get_value (icon_view->priv->hadjustment) + hoffset);
5974 }
5975
5976 typedef struct {
5977   GtkIconView *icon_view;
5978   GdkDevice   *device;
5979 } DragScrollData;
5980
5981 static gboolean
5982 drag_scroll_timeout (gpointer datap)
5983 {
5984   DragScrollData *data = datap;
5985
5986   gtk_icon_view_autoscroll (data->icon_view, data->device);
5987
5988   return TRUE;
5989 }
5990
5991 static void
5992 drag_scroll_data_free (DragScrollData *data)
5993 {
5994   g_slice_free (DragScrollData, data);
5995 }
5996
5997 static gboolean
5998 set_destination (GtkIconView    *icon_view,
5999                  GdkDragContext *context,
6000                  gint            x,
6001                  gint            y,
6002                  GdkDragAction  *suggested_action,
6003                  GdkAtom        *target)
6004 {
6005   GtkWidget *widget;
6006   GtkTreePath *path = NULL;
6007   GtkIconViewDropPosition pos;
6008   GtkIconViewDropPosition old_pos;
6009   GtkTreePath *old_dest_path = NULL;
6010   gboolean can_drop = FALSE;
6011
6012   widget = GTK_WIDGET (icon_view);
6013
6014   *suggested_action = 0;
6015   *target = GDK_NONE;
6016
6017   if (!icon_view->priv->dest_set)
6018     {
6019       /* someone unset us as a drag dest, note that if
6020        * we return FALSE drag_leave isn't called
6021        */
6022
6023       gtk_icon_view_set_drag_dest_item (icon_view,
6024                                         NULL,
6025                                         GTK_ICON_VIEW_DROP_LEFT);
6026
6027       remove_scroll_timeout (GTK_ICON_VIEW (widget));
6028
6029       return FALSE; /* no longer a drop site */
6030     }
6031
6032   *target = gtk_drag_dest_find_target (widget, context,
6033                                        gtk_drag_dest_get_target_list (widget));
6034   if (*target == GDK_NONE)
6035     return FALSE;
6036
6037   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
6038     {
6039       gint n_children;
6040       GtkTreeModel *model;
6041       
6042       /* the row got dropped on empty space, let's setup a special case
6043        */
6044
6045       if (path)
6046         gtk_tree_path_free (path);
6047
6048       model = gtk_icon_view_get_model (icon_view);
6049
6050       n_children = gtk_tree_model_iter_n_children (model, NULL);
6051       if (n_children)
6052         {
6053           pos = GTK_ICON_VIEW_DROP_BELOW;
6054           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6055         }
6056       else
6057         {
6058           pos = GTK_ICON_VIEW_DROP_ABOVE;
6059           path = gtk_tree_path_new_from_indices (0, -1);
6060         }
6061
6062       can_drop = TRUE;
6063
6064       goto out;
6065     }
6066
6067   g_assert (path);
6068
6069   gtk_icon_view_get_drag_dest_item (icon_view,
6070                                     &old_dest_path,
6071                                     &old_pos);
6072   
6073   if (old_dest_path)
6074     gtk_tree_path_free (old_dest_path);
6075   
6076   if (TRUE /* FIXME if the location droppable predicate */)
6077     {
6078       can_drop = TRUE;
6079     }
6080
6081 out:
6082   if (can_drop)
6083     {
6084       GtkWidget *source_widget;
6085
6086       *suggested_action = gdk_drag_context_get_suggested_action (context);
6087       source_widget = gtk_drag_get_source_widget (context);
6088
6089       if (source_widget == widget)
6090         {
6091           /* Default to MOVE, unless the user has
6092            * pressed ctrl or shift to affect available actions
6093            */
6094           if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6095             *suggested_action = GDK_ACTION_MOVE;
6096         }
6097
6098       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6099                                         path, pos);
6100     }
6101   else
6102     {
6103       /* can't drop here */
6104       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6105                                         NULL,
6106                                         GTK_ICON_VIEW_DROP_LEFT);
6107     }
6108   
6109   if (path)
6110     gtk_tree_path_free (path);
6111   
6112   return TRUE;
6113 }
6114
6115 static GtkTreePath*
6116 get_logical_destination (GtkIconView *icon_view,
6117                          gboolean    *drop_append_mode)
6118 {
6119   /* adjust path to point to the row the drop goes in front of */
6120   GtkTreePath *path = NULL;
6121   GtkIconViewDropPosition pos;
6122   
6123   *drop_append_mode = FALSE;
6124
6125   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6126
6127   if (path == NULL)
6128     return NULL;
6129
6130   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
6131       pos == GTK_ICON_VIEW_DROP_BELOW)
6132     {
6133       GtkTreeIter iter;
6134       GtkTreeModel *model = icon_view->priv->model;
6135
6136       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6137           !gtk_tree_model_iter_next (model, &iter))
6138         *drop_append_mode = TRUE;
6139       else
6140         {
6141           *drop_append_mode = FALSE;
6142           gtk_tree_path_next (path);
6143         }      
6144     }
6145
6146   return path;
6147 }
6148
6149 static gboolean
6150 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
6151                                 GdkEventMotion *event)
6152 {
6153   GtkWidget *widget = GTK_WIDGET (icon_view);
6154   GdkDragContext *context;
6155   GtkTreePath *path = NULL;
6156   gint button;
6157   GtkTreeModel *model;
6158   gboolean retval = FALSE;
6159
6160   if (!icon_view->priv->source_set)
6161     goto out;
6162
6163   if (icon_view->priv->pressed_button < 0)
6164     goto out;
6165
6166   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6167                                  icon_view->priv->press_start_x,
6168                                  icon_view->priv->press_start_y,
6169                                  event->x, event->y))
6170     goto out;
6171
6172   model = gtk_icon_view_get_model (icon_view);
6173
6174   if (model == NULL)
6175     goto out;
6176
6177   button = icon_view->priv->pressed_button;
6178   icon_view->priv->pressed_button = -1;
6179
6180   path = gtk_icon_view_get_path_at_pos (icon_view,
6181                                         icon_view->priv->press_start_x,
6182                                         icon_view->priv->press_start_y);
6183
6184   if (path == NULL)
6185     goto out;
6186
6187   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6188       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6189                                            path))
6190     goto out;
6191
6192   /* FIXME Check whether we're a start button, if not return FALSE and
6193    * free path
6194    */
6195
6196   /* Now we can begin the drag */
6197   
6198   retval = TRUE;
6199
6200   context = gtk_drag_begin (widget,
6201                             gtk_drag_source_get_target_list (widget),
6202                             icon_view->priv->source_actions,
6203                             button,
6204                             (GdkEvent*)event);
6205
6206   set_source_row (context, model, path);
6207   
6208  out:
6209   if (path)
6210     gtk_tree_path_free (path);
6211
6212   return retval;
6213 }
6214
6215 /* Source side drag signals */
6216 static void 
6217 gtk_icon_view_drag_begin (GtkWidget      *widget,
6218                           GdkDragContext *context)
6219 {
6220   GtkIconView *icon_view;
6221   GtkIconViewItem *item;
6222   cairo_surface_t *icon;
6223   gint x, y;
6224   GtkTreePath *path;
6225
6226   icon_view = GTK_ICON_VIEW (widget);
6227
6228   /* if the user uses a custom DnD impl, we don't set the icon here */
6229   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6230     return;
6231
6232   item = _gtk_icon_view_get_item_at_coords (icon_view,
6233                                            icon_view->priv->press_start_x,
6234                                            icon_view->priv->press_start_y,
6235                                            TRUE,
6236                                            NULL);
6237
6238   g_return_if_fail (item != NULL);
6239
6240   x = icon_view->priv->press_start_x - item->cell_area.x + 1;
6241   y = icon_view->priv->press_start_y - item->cell_area.y + 1;
6242   
6243   path = gtk_tree_path_new_from_indices (item->index, -1);
6244   icon = gtk_icon_view_create_drag_icon (icon_view, path);
6245   gtk_tree_path_free (path);
6246
6247   cairo_surface_set_device_offset (icon, -x, -y);
6248
6249   gtk_drag_set_icon_surface (context, icon);
6250
6251   cairo_surface_destroy (icon);
6252 }
6253
6254 static void 
6255 gtk_icon_view_drag_end (GtkWidget      *widget,
6256                         GdkDragContext *context)
6257 {
6258   /* do nothing */
6259 }
6260
6261 static void 
6262 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6263                              GdkDragContext   *context,
6264                              GtkSelectionData *selection_data,
6265                              guint             info,
6266                              guint             time)
6267 {
6268   GtkIconView *icon_view;
6269   GtkTreeModel *model;
6270   GtkTreePath *source_row;
6271
6272   icon_view = GTK_ICON_VIEW (widget);
6273   model = gtk_icon_view_get_model (icon_view);
6274
6275   if (model == NULL)
6276     return;
6277
6278   if (!icon_view->priv->source_set)
6279     return;
6280
6281   source_row = get_source_row (context);
6282
6283   if (source_row == NULL)
6284     return;
6285
6286   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6287    * any model; for DragSource models there are some other targets
6288    * we also support.
6289    */
6290
6291   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6292       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6293                                           source_row,
6294                                           selection_data))
6295     goto done;
6296
6297   /* If drag_data_get does nothing, try providing row data. */
6298   if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6299     gtk_tree_set_row_drag_data (selection_data,
6300                                 model,
6301                                 source_row);
6302
6303  done:
6304   gtk_tree_path_free (source_row);
6305 }
6306
6307 static void 
6308 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6309                                 GdkDragContext *context)
6310 {
6311   GtkTreeModel *model;
6312   GtkIconView *icon_view;
6313   GtkTreePath *source_row;
6314
6315   icon_view = GTK_ICON_VIEW (widget);
6316   model = gtk_icon_view_get_model (icon_view);
6317
6318   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6319     return;
6320
6321   if (!icon_view->priv->source_set)
6322     return;
6323
6324   source_row = get_source_row (context);
6325
6326   if (source_row == NULL)
6327     return;
6328
6329   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6330                                          source_row);
6331
6332   gtk_tree_path_free (source_row);
6333
6334   set_source_row (context, NULL, NULL);
6335 }
6336
6337 /* Target side drag signals */
6338 static void
6339 gtk_icon_view_drag_leave (GtkWidget      *widget,
6340                           GdkDragContext *context,
6341                           guint           time)
6342 {
6343   GtkIconView *icon_view;
6344
6345   icon_view = GTK_ICON_VIEW (widget);
6346
6347   /* unset any highlight row */
6348   gtk_icon_view_set_drag_dest_item (icon_view,
6349                                     NULL,
6350                                     GTK_ICON_VIEW_DROP_LEFT);
6351
6352   remove_scroll_timeout (icon_view);
6353 }
6354
6355 static gboolean 
6356 gtk_icon_view_drag_motion (GtkWidget      *widget,
6357                            GdkDragContext *context,
6358                            gint            x,
6359                            gint            y,
6360                            guint           time)
6361 {
6362   GtkTreePath *path = NULL;
6363   GtkIconViewDropPosition pos;
6364   GtkIconView *icon_view;
6365   GdkDragAction suggested_action = 0;
6366   GdkAtom target;
6367   gboolean empty;
6368
6369   icon_view = GTK_ICON_VIEW (widget);
6370
6371   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6372     return FALSE;
6373
6374   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6375
6376   /* we only know this *after* set_desination_row */
6377   empty = icon_view->priv->empty_view_drop;
6378
6379   if (path == NULL && !empty)
6380     {
6381       /* Can't drop here. */
6382       gdk_drag_status (context, 0, time);
6383     }
6384   else
6385     {
6386       if (icon_view->priv->scroll_timeout_id == 0)
6387         {
6388           DragScrollData *data = g_slice_new (DragScrollData);
6389           data->icon_view = icon_view;
6390           data->device = gdk_drag_context_get_device (context);
6391
6392           icon_view->priv->scroll_timeout_id =
6393             gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 50, drag_scroll_timeout, data, (GDestroyNotify) drag_scroll_data_free);
6394         }
6395
6396       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6397         {
6398           /* Request data so we can use the source row when
6399            * determining whether to accept the drop
6400            */
6401           set_status_pending (context, suggested_action);
6402           gtk_drag_get_data (widget, context, target, time);
6403         }
6404       else
6405         {
6406           set_status_pending (context, 0);
6407           gdk_drag_status (context, suggested_action, time);
6408         }
6409     }
6410
6411   if (path)
6412     gtk_tree_path_free (path);
6413
6414   return TRUE;
6415 }
6416
6417 static gboolean 
6418 gtk_icon_view_drag_drop (GtkWidget      *widget,
6419                          GdkDragContext *context,
6420                          gint            x,
6421                          gint            y,
6422                          guint           time)
6423 {
6424   GtkIconView *icon_view;
6425   GtkTreePath *path;
6426   GdkDragAction suggested_action = 0;
6427   GdkAtom target = GDK_NONE;
6428   GtkTreeModel *model;
6429   gboolean drop_append_mode;
6430
6431   icon_view = GTK_ICON_VIEW (widget);
6432   model = gtk_icon_view_get_model (icon_view);
6433
6434   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6435
6436   if (!icon_view->priv->dest_set)
6437     return FALSE;
6438
6439   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6440     return FALSE;
6441
6442   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6443     return FALSE;
6444   
6445   path = get_logical_destination (icon_view, &drop_append_mode);
6446
6447   if (target != GDK_NONE && path != NULL)
6448     {
6449       /* in case a motion had requested drag data, change things so we
6450        * treat drag data receives as a drop.
6451        */
6452       set_status_pending (context, 0);
6453       set_dest_row (context, model, path, 
6454                     icon_view->priv->empty_view_drop, drop_append_mode);
6455     }
6456
6457   if (path)
6458     gtk_tree_path_free (path);
6459
6460   /* Unset this thing */
6461   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6462
6463   if (target != GDK_NONE)
6464     {
6465       gtk_drag_get_data (widget, context, target, time);
6466       return TRUE;
6467     }
6468   else
6469     return FALSE;
6470 }
6471
6472 static void
6473 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6474                                   GdkDragContext   *context,
6475                                   gint              x,
6476                                   gint              y,
6477                                   GtkSelectionData *selection_data,
6478                                   guint             info,
6479                                   guint             time)
6480 {
6481   GtkTreePath *path;
6482   gboolean accepted = FALSE;
6483   GtkTreeModel *model;
6484   GtkIconView *icon_view;
6485   GtkTreePath *dest_row;
6486   GdkDragAction suggested_action;
6487   gboolean drop_append_mode;
6488   
6489   icon_view = GTK_ICON_VIEW (widget);  
6490   model = gtk_icon_view_get_model (icon_view);
6491
6492   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6493     return;
6494
6495   if (!icon_view->priv->dest_set)
6496     return;
6497
6498   suggested_action = get_status_pending (context);
6499
6500   if (suggested_action)
6501     {
6502       /* We are getting this data due to a request in drag_motion,
6503        * rather than due to a request in drag_drop, so we are just
6504        * supposed to call drag_status, not actually paste in the
6505        * data.
6506        */
6507       path = get_logical_destination (icon_view, &drop_append_mode);
6508
6509       if (path == NULL)
6510         suggested_action = 0;
6511
6512       if (suggested_action)
6513         {
6514           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6515                                                      path,
6516                                                      selection_data))
6517             suggested_action = 0;
6518         }
6519
6520       gdk_drag_status (context, suggested_action, time);
6521
6522       if (path)
6523         gtk_tree_path_free (path);
6524
6525       /* If you can't drop, remove user drop indicator until the next motion */
6526       if (suggested_action == 0)
6527         gtk_icon_view_set_drag_dest_item (icon_view,
6528                                           NULL,
6529                                           GTK_ICON_VIEW_DROP_LEFT);
6530       return;
6531     }
6532   
6533
6534   dest_row = get_dest_row (context);
6535
6536   if (dest_row == NULL)
6537     return;
6538
6539   if (gtk_selection_data_get_length (selection_data) >= 0)
6540     {
6541       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6542                                                  dest_row,
6543                                                  selection_data))
6544         accepted = TRUE;
6545     }
6546
6547   gtk_drag_finish (context,
6548                    accepted,
6549                    (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
6550                    time);
6551
6552   gtk_tree_path_free (dest_row);
6553
6554   /* drop dest_row */
6555   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6556 }
6557
6558 /* Drag-and-Drop support */
6559 /**
6560  * gtk_icon_view_enable_model_drag_source:
6561  * @icon_view: a #GtkIconTreeView
6562  * @start_button_mask: Mask of allowed buttons to start drag
6563  * @targets: (array length=n_targets): the table of targets that the drag will
6564  *           support
6565  * @n_targets: the number of items in @targets
6566  * @actions: the bitmask of possible actions for a drag from this
6567  *    widget
6568  *
6569  * Turns @icon_view into a drag source for automatic DND. Calling this
6570  * method sets #GtkIconView:reorderable to %FALSE.
6571  *
6572  * Since: 2.8
6573  **/
6574 void
6575 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6576                                         GdkModifierType           start_button_mask,
6577                                         const GtkTargetEntry     *targets,
6578                                         gint                      n_targets,
6579                                         GdkDragAction             actions)
6580 {
6581   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6582
6583   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6584
6585   icon_view->priv->start_button_mask = start_button_mask;
6586   icon_view->priv->source_actions = actions;
6587
6588   icon_view->priv->source_set = TRUE;
6589
6590   unset_reorderable (icon_view);
6591 }
6592
6593 /**
6594  * gtk_icon_view_enable_model_drag_dest:
6595  * @icon_view: a #GtkIconView
6596  * @targets: (array length=n_targets): the table of targets that the drag will
6597  *           support
6598  * @n_targets: the number of items in @targets
6599  * @actions: the bitmask of possible actions for a drag to this
6600  *    widget
6601  *
6602  * Turns @icon_view into a drop destination for automatic DND. Calling this
6603  * method sets #GtkIconView:reorderable to %FALSE.
6604  *
6605  * Since: 2.8
6606  **/
6607 void 
6608 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6609                                       const GtkTargetEntry *targets,
6610                                       gint                  n_targets,
6611                                       GdkDragAction         actions)
6612 {
6613   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6614
6615   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6616
6617   icon_view->priv->dest_actions = actions;
6618
6619   icon_view->priv->dest_set = TRUE;
6620
6621   unset_reorderable (icon_view);  
6622 }
6623
6624 /**
6625  * gtk_icon_view_unset_model_drag_source:
6626  * @icon_view: a #GtkIconView
6627  * 
6628  * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
6629  * method sets #GtkIconView:reorderable to %FALSE.
6630  *
6631  * Since: 2.8
6632  **/
6633 void
6634 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6635 {
6636   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6637
6638   if (icon_view->priv->source_set)
6639     {
6640       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6641       icon_view->priv->source_set = FALSE;
6642     }
6643
6644   unset_reorderable (icon_view);
6645 }
6646
6647 /**
6648  * gtk_icon_view_unset_model_drag_dest:
6649  * @icon_view: a #GtkIconView
6650  * 
6651  * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
6652  * method sets #GtkIconView:reorderable to %FALSE.
6653  *
6654  * Since: 2.8
6655  **/
6656 void
6657 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6658 {
6659   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6660
6661   if (icon_view->priv->dest_set)
6662     {
6663       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6664       icon_view->priv->dest_set = FALSE;
6665     }
6666
6667   unset_reorderable (icon_view);
6668 }
6669
6670 /* These are useful to implement your own custom stuff. */
6671 /**
6672  * gtk_icon_view_set_drag_dest_item:
6673  * @icon_view: a #GtkIconView
6674  * @path: (allow-none): The path of the item to highlight, or %NULL.
6675  * @pos: Specifies where to drop, relative to the item
6676  *
6677  * Sets the item that is highlighted for feedback.
6678  *
6679  * Since: 2.8
6680  */
6681 void
6682 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6683                                   GtkTreePath              *path,
6684                                   GtkIconViewDropPosition   pos)
6685 {
6686   /* Note; this function is exported to allow a custom DND
6687    * implementation, so it can't touch TreeViewDragInfo
6688    */
6689
6690   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6691
6692   if (icon_view->priv->dest_item)
6693     {
6694       GtkTreePath *current_path;
6695       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6696       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6697       icon_view->priv->dest_item = NULL;      
6698
6699       gtk_icon_view_queue_draw_path (icon_view, current_path);
6700       gtk_tree_path_free (current_path);
6701     }
6702   
6703   /* special case a drop on an empty model */
6704   icon_view->priv->empty_view_drop = FALSE;
6705   if (pos == GTK_ICON_VIEW_DROP_ABOVE && path
6706       && gtk_tree_path_get_depth (path) == 1
6707       && gtk_tree_path_get_indices (path)[0] == 0)
6708     {
6709       gint n_children;
6710
6711       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6712                                                    NULL);
6713
6714       if (n_children == 0)
6715         icon_view->priv->empty_view_drop = TRUE;
6716     }
6717
6718   icon_view->priv->dest_pos = pos;
6719
6720   if (path)
6721     {
6722       icon_view->priv->dest_item =
6723         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
6724                                           icon_view->priv->model, path);
6725       
6726       gtk_icon_view_queue_draw_path (icon_view, path);
6727     }
6728 }
6729
6730 /**
6731  * gtk_icon_view_get_drag_dest_item:
6732  * @icon_view: a #GtkIconView
6733  * @path: (out) (allow-none): Return location for the path of
6734  *        the highlighted item, or %NULL.
6735  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6736  * 
6737  * Gets information about the item that is highlighted for feedback.
6738  *
6739  * Since: 2.8
6740  **/
6741 void
6742 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
6743                                   GtkTreePath             **path,
6744                                   GtkIconViewDropPosition  *pos)
6745 {
6746   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6747
6748   if (path)
6749     {
6750       if (icon_view->priv->dest_item)
6751         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6752       else
6753         *path = NULL;
6754     }
6755
6756   if (pos)
6757     *pos = icon_view->priv->dest_pos;
6758 }
6759
6760 /**
6761  * gtk_icon_view_get_dest_item_at_pos:
6762  * @icon_view: a #GtkIconView
6763  * @drag_x: the position to determine the destination item for
6764  * @drag_y: the position to determine the destination item for
6765  * @path: (out) (allow-none): Return location for the path of the item,
6766  *    or %NULL.
6767  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6768  * 
6769  * Determines the destination item for a given position.
6770  * 
6771  * Return value: whether there is an item at the given position.
6772  *
6773  * Since: 2.8
6774  **/
6775 gboolean
6776 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
6777                                     gint                      drag_x,
6778                                     gint                      drag_y,
6779                                     GtkTreePath             **path,
6780                                     GtkIconViewDropPosition  *pos)
6781 {
6782   GtkIconViewItem *item;
6783
6784   /* Note; this function is exported to allow a custom DND
6785    * implementation, so it can't touch TreeViewDragInfo
6786    */
6787
6788   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6789   g_return_val_if_fail (drag_x >= 0, FALSE);
6790   g_return_val_if_fail (drag_y >= 0, FALSE);
6791   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6792
6793
6794   if (path)
6795     *path = NULL;
6796
6797   item = _gtk_icon_view_get_item_at_coords (icon_view, 
6798                                            drag_x + gtk_adjustment_get_value (icon_view->priv->hadjustment), 
6799                                            drag_y + gtk_adjustment_get_value (icon_view->priv->vadjustment),
6800                                            FALSE, NULL);
6801
6802   if (item == NULL)
6803     return FALSE;
6804
6805   if (path)
6806     *path = gtk_tree_path_new_from_indices (item->index, -1);
6807
6808   if (pos)
6809     {
6810       if (drag_x < item->cell_area.x + item->cell_area.width / 4)
6811         *pos = GTK_ICON_VIEW_DROP_LEFT;
6812       else if (drag_x > item->cell_area.x + item->cell_area.width * 3 / 4)
6813         *pos = GTK_ICON_VIEW_DROP_RIGHT;
6814       else if (drag_y < item->cell_area.y + item->cell_area.height / 4)
6815         *pos = GTK_ICON_VIEW_DROP_ABOVE;
6816       else if (drag_y > item->cell_area.y + item->cell_area.height * 3 / 4)
6817         *pos = GTK_ICON_VIEW_DROP_BELOW;
6818       else
6819         *pos = GTK_ICON_VIEW_DROP_INTO;
6820     }
6821
6822   return TRUE;
6823 }
6824
6825 /**
6826  * gtk_icon_view_create_drag_icon:
6827  * @icon_view: a #GtkIconView
6828  * @path: a #GtkTreePath in @icon_view
6829  *
6830  * Creates a #cairo_surface_t representation of the item at @path.  
6831  * This image is used for a drag icon.
6832  *
6833  * Return value: (transfer full): a newly-allocated surface of the drag icon.
6834  * 
6835  * Since: 2.8
6836  **/
6837 cairo_surface_t *
6838 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6839                                 GtkTreePath *path)
6840 {
6841   GtkWidget *widget;
6842   GtkStyleContext *context;
6843   cairo_t *cr;
6844   cairo_surface_t *surface;
6845   GList *l;
6846   gint index;
6847
6848   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
6849   g_return_val_if_fail (path != NULL, NULL);
6850
6851   widget = GTK_WIDGET (icon_view);
6852   context = gtk_widget_get_style_context (widget);
6853
6854   if (!gtk_widget_get_realized (widget))
6855     return NULL;
6856
6857   index = gtk_tree_path_get_indices (path)[0];
6858
6859   for (l = icon_view->priv->items; l; l = l->next) 
6860     {
6861       GtkIconViewItem *item = l->data;
6862       
6863       if (index == item->index)
6864         {
6865           GdkRectangle rect = { 
6866             item->cell_area.x - icon_view->priv->item_padding, 
6867             item->cell_area.y - icon_view->priv->item_padding, 
6868             item->cell_area.width  + icon_view->priv->item_padding * 2, 
6869             item->cell_area.height + icon_view->priv->item_padding * 2 
6870           };
6871
6872           surface = gdk_window_create_similar_surface (icon_view->priv->bin_window,
6873                                                        CAIRO_CONTENT_COLOR,
6874                                                        rect.width + 2,
6875                                                        rect.height + 2);
6876
6877           cr = cairo_create (surface);
6878           cairo_set_line_width (cr, 1.);
6879
6880           gtk_render_background (context, cr, 0, 0,
6881                                  rect.width + 2, rect.height + 2);
6882
6883           cairo_save (cr);
6884
6885           cairo_rectangle (cr, 1, 1, rect.width, rect.height);
6886           cairo_clip (cr);
6887
6888           gtk_icon_view_paint_item (icon_view, cr, item, 
6889                                     icon_view->priv->item_padding + 1, 
6890                                     icon_view->priv->item_padding + 1, FALSE);
6891
6892           cairo_restore (cr);
6893
6894           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
6895           cairo_rectangle (cr, 0.5, 0.5, rect.width + 1, rect.height + 1);
6896           cairo_stroke (cr);
6897
6898           cairo_destroy (cr);
6899
6900           return surface;
6901         }
6902     }
6903   
6904   return NULL;
6905 }
6906
6907 /**
6908  * gtk_icon_view_get_reorderable:
6909  * @icon_view: a #GtkIconView
6910  *
6911  * Retrieves whether the user can reorder the list via drag-and-drop. 
6912  * See gtk_icon_view_set_reorderable().
6913  *
6914  * Return value: %TRUE if the list can be reordered.
6915  *
6916  * Since: 2.8
6917  **/
6918 gboolean
6919 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
6920 {
6921   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6922
6923   return icon_view->priv->reorderable;
6924 }
6925
6926 static const GtkTargetEntry item_targets[] = {
6927   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
6928 };
6929
6930
6931 /**
6932  * gtk_icon_view_set_reorderable:
6933  * @icon_view: A #GtkIconView.
6934  * @reorderable: %TRUE, if the list of items can be reordered.
6935  *
6936  * This function is a convenience function to allow you to reorder models that
6937  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
6938  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
6939  * the user can reorder the model by dragging and dropping rows.  The
6940  * developer can listen to these changes by connecting to the model's
6941  * row_inserted and row_deleted signals. The reordering is implemented by setting up
6942  * the icon view as a drag source and destination. Therefore, drag and
6943  * drop can not be used in a reorderable view for any other purpose.
6944  *
6945  * This function does not give you any degree of control over the order -- any
6946  * reordering is allowed.  If more control is needed, you should probably
6947  * handle drag and drop manually.
6948  *
6949  * Since: 2.8
6950  **/
6951 void
6952 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
6953                                gboolean     reorderable)
6954 {
6955   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6956
6957   reorderable = reorderable != FALSE;
6958
6959   if (icon_view->priv->reorderable == reorderable)
6960     return;
6961
6962   if (reorderable)
6963     {
6964       gtk_icon_view_enable_model_drag_source (icon_view,
6965                                               GDK_BUTTON1_MASK,
6966                                               item_targets,
6967                                               G_N_ELEMENTS (item_targets),
6968                                               GDK_ACTION_MOVE);
6969       gtk_icon_view_enable_model_drag_dest (icon_view,
6970                                             item_targets,
6971                                             G_N_ELEMENTS (item_targets),
6972                                             GDK_ACTION_MOVE);
6973     }
6974   else
6975     {
6976       gtk_icon_view_unset_model_drag_source (icon_view);
6977       gtk_icon_view_unset_model_drag_dest (icon_view);
6978     }
6979
6980   icon_view->priv->reorderable = reorderable;
6981
6982   g_object_notify (G_OBJECT (icon_view), "reorderable");
6983 }
6984
6985 static gboolean
6986 gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
6987                                           GtkBuilder    *builder,
6988                                           GObject       *child,
6989                                           const gchar   *tagname,
6990                                           GMarkupParser *parser,
6991                                           gpointer      *data)
6992 {
6993   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6994                                                 tagname, parser, data))
6995     return TRUE;
6996
6997   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6998                                                       tagname, parser, data);
6999 }
7000
7001 static void
7002 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
7003                                         GtkBuilder   *builder,
7004                                         GObject      *child,
7005                                         const gchar  *tagname,
7006                                         gpointer     *data)
7007 {
7008   if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder,
7009                                                   child, tagname, data))
7010     parent_buildable_iface->custom_tag_end (buildable, builder,
7011                                             child, tagname, data);
7012 }