]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
gtkiconview: Use symbolic names for button numbers
[~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 == GDK_BUTTON_PRIMARY && 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 == GDK_BUTTON_PRIMARY && 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 == GDK_BUTTON_PRIMARY;
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_free_full (icon_view->priv->items, (GDestroyNotify) gtk_icon_view_item_free);
4772       icon_view->priv->items = NULL;
4773       icon_view->priv->anchor_item = NULL;
4774       icon_view->priv->cursor_item = NULL;
4775       icon_view->priv->last_single_clicked = NULL;
4776       icon_view->priv->last_prelight = NULL;
4777       icon_view->priv->width = 0;
4778       icon_view->priv->height = 0;
4779     }
4780
4781   icon_view->priv->model = model;
4782
4783   if (icon_view->priv->model)
4784     {
4785       g_object_ref (icon_view->priv->model);
4786       g_signal_connect (icon_view->priv->model,
4787                         "row-changed",
4788                         G_CALLBACK (gtk_icon_view_row_changed),
4789                         icon_view);
4790       g_signal_connect (icon_view->priv->model,
4791                         "row-inserted",
4792                         G_CALLBACK (gtk_icon_view_row_inserted),
4793                         icon_view);
4794       g_signal_connect (icon_view->priv->model,
4795                         "row-deleted",
4796                         G_CALLBACK (gtk_icon_view_row_deleted),
4797                         icon_view);
4798       g_signal_connect (icon_view->priv->model,
4799                         "rows-reordered",
4800                         G_CALLBACK (gtk_icon_view_rows_reordered),
4801                         icon_view);
4802
4803       gtk_icon_view_build_items (icon_view);
4804
4805       gtk_icon_view_layout (icon_view);
4806     }
4807
4808   g_object_notify (G_OBJECT (icon_view), "model");  
4809
4810   if (gtk_widget_get_realized (GTK_WIDGET (icon_view)))
4811     gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4812 }
4813
4814 /**
4815  * gtk_icon_view_get_model:
4816  * @icon_view: a #GtkIconView
4817  *
4818  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
4819  * model is unset.
4820  *
4821  * Return value: (transfer none): A #GtkTreeModel, or %NULL if none is
4822  *     currently being used.
4823  *
4824  * Since: 2.6 
4825  **/
4826 GtkTreeModel *
4827 gtk_icon_view_get_model (GtkIconView *icon_view)
4828 {
4829   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4830
4831   return icon_view->priv->model;
4832 }
4833
4834 static void
4835 update_text_cell (GtkIconView *icon_view)
4836 {
4837   if (!icon_view->priv->cell_area)
4838     return;
4839
4840   if (icon_view->priv->text_column == -1 &&
4841       icon_view->priv->markup_column == -1)
4842     {
4843       if (icon_view->priv->text_cell != NULL)
4844         {
4845           gtk_cell_area_remove (icon_view->priv->cell_area, 
4846                                 icon_view->priv->text_cell);
4847           icon_view->priv->text_cell = NULL;
4848         }
4849     }
4850   else 
4851     {
4852       if (icon_view->priv->text_cell == NULL)
4853         {
4854           icon_view->priv->text_cell = gtk_cell_renderer_text_new ();
4855
4856           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), icon_view->priv->text_cell, FALSE);
4857         }
4858
4859       if (icon_view->priv->markup_column != -1)
4860         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4861                                         icon_view->priv->text_cell, 
4862                                         "markup", icon_view->priv->markup_column, 
4863                                         NULL);
4864       else
4865         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4866                                         icon_view->priv->text_cell, 
4867                                         "text", icon_view->priv->text_column, 
4868                                         NULL);
4869
4870       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
4871         g_object_set (icon_view->priv->text_cell,
4872                       "alignment", PANGO_ALIGN_CENTER,
4873                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
4874                       "xalign", 0.5,
4875                       "yalign", 0.0,
4876                       NULL);
4877       else
4878         g_object_set (icon_view->priv->text_cell,
4879                       "alignment", PANGO_ALIGN_LEFT,
4880                       "wrap-mode", PANGO_WRAP_WORD_CHAR,
4881                       "xalign", 0.0,
4882                       "yalign", 0.5,
4883                       NULL);
4884     }
4885 }
4886
4887 static void
4888 update_pixbuf_cell (GtkIconView *icon_view)
4889 {
4890   if (!icon_view->priv->cell_area)
4891     return;
4892
4893   if (icon_view->priv->pixbuf_column == -1)
4894     {
4895       if (icon_view->priv->pixbuf_cell != NULL)
4896         {
4897           gtk_cell_area_remove (icon_view->priv->cell_area, 
4898                                 icon_view->priv->pixbuf_cell);
4899
4900           icon_view->priv->pixbuf_cell = NULL;
4901         }
4902     }
4903   else 
4904     {
4905       if (icon_view->priv->pixbuf_cell == NULL)
4906         {
4907           icon_view->priv->pixbuf_cell = gtk_cell_renderer_pixbuf_new ();
4908           
4909           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), icon_view->priv->pixbuf_cell, FALSE);
4910         }
4911       
4912       gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4913                                       icon_view->priv->pixbuf_cell, 
4914                                       "pixbuf", icon_view->priv->pixbuf_column, 
4915                                       NULL);
4916
4917       if (icon_view->priv->item_orientation == GTK_ORIENTATION_VERTICAL)
4918         g_object_set (icon_view->priv->pixbuf_cell,
4919                       "xalign", 0.5,
4920                       "yalign", 1.0,
4921                       NULL);
4922       else
4923         g_object_set (icon_view->priv->pixbuf_cell,
4924                       "xalign", 0.0,
4925                       "yalign", 0.0,
4926                       NULL);
4927     }
4928 }
4929
4930 /**
4931  * gtk_icon_view_set_text_column:
4932  * @icon_view: A #GtkIconView.
4933  * @column: A column in the currently used model, or -1 to display no text
4934  * 
4935  * Sets the column with text for @icon_view to be @column. The text
4936  * column must be of type #G_TYPE_STRING.
4937  *
4938  * Since: 2.6 
4939  **/
4940 void
4941 gtk_icon_view_set_text_column (GtkIconView *icon_view,
4942                                gint          column)
4943 {
4944   if (column == icon_view->priv->text_column)
4945     return;
4946   
4947   if (column == -1)
4948     icon_view->priv->text_column = -1;
4949   else
4950     {
4951       if (icon_view->priv->model != NULL)
4952         {
4953           GType column_type;
4954           
4955           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4956
4957           g_return_if_fail (column_type == G_TYPE_STRING);
4958         }
4959       
4960       icon_view->priv->text_column = column;
4961     }
4962
4963   if (icon_view->priv->cell_area)
4964     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
4965
4966   update_text_cell (icon_view);
4967
4968   gtk_icon_view_invalidate_sizes (icon_view);
4969   
4970   g_object_notify (G_OBJECT (icon_view), "text-column");
4971 }
4972
4973 /**
4974  * gtk_icon_view_get_text_column:
4975  * @icon_view: A #GtkIconView.
4976  *
4977  * Returns the column with text for @icon_view.
4978  *
4979  * Returns: the text column, or -1 if it's unset.
4980  *
4981  * Since: 2.6
4982  */
4983 gint
4984 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
4985 {
4986   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4987
4988   return icon_view->priv->text_column;
4989 }
4990
4991 /**
4992  * gtk_icon_view_set_markup_column:
4993  * @icon_view: A #GtkIconView.
4994  * @column: A column in the currently used model, or -1 to display no text
4995  * 
4996  * Sets the column with markup information for @icon_view to be
4997  * @column. The markup column must be of type #G_TYPE_STRING.
4998  * If the markup column is set to something, it overrides
4999  * the text column set by gtk_icon_view_set_text_column().
5000  *
5001  * Since: 2.6
5002  **/
5003 void
5004 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
5005                                  gint         column)
5006 {
5007   if (column == icon_view->priv->markup_column)
5008     return;
5009   
5010   if (column == -1)
5011     icon_view->priv->markup_column = -1;
5012   else
5013     {
5014       if (icon_view->priv->model != NULL)
5015         {
5016           GType column_type;
5017           
5018           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5019
5020           g_return_if_fail (column_type == G_TYPE_STRING);
5021         }
5022       
5023       icon_view->priv->markup_column = column;
5024     }
5025
5026   if (icon_view->priv->cell_area)
5027     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5028
5029   update_text_cell (icon_view);
5030
5031   gtk_icon_view_invalidate_sizes (icon_view);
5032   
5033   g_object_notify (G_OBJECT (icon_view), "markup-column");
5034 }
5035
5036 /**
5037  * gtk_icon_view_get_markup_column:
5038  * @icon_view: A #GtkIconView.
5039  *
5040  * Returns the column with markup text for @icon_view.
5041  *
5042  * Returns: the markup column, or -1 if it's unset.
5043  *
5044  * Since: 2.6
5045  */
5046 gint
5047 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
5048 {
5049   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5050
5051   return icon_view->priv->markup_column;
5052 }
5053
5054 /**
5055  * gtk_icon_view_set_pixbuf_column:
5056  * @icon_view: A #GtkIconView.
5057  * @column: A column in the currently used model, or -1 to disable
5058  * 
5059  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
5060  * column must be of type #GDK_TYPE_PIXBUF
5061  *
5062  * Since: 2.6 
5063  **/
5064 void
5065 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
5066                                  gint         column)
5067 {
5068   if (column == icon_view->priv->pixbuf_column)
5069     return;
5070   
5071   if (column == -1)
5072     icon_view->priv->pixbuf_column = -1;
5073   else
5074     {
5075       if (icon_view->priv->model != NULL)
5076         {
5077           GType column_type;
5078           
5079           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
5080
5081           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
5082         }
5083       
5084       icon_view->priv->pixbuf_column = column;
5085     }
5086
5087   if (icon_view->priv->cell_area)
5088     gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5089
5090   update_pixbuf_cell (icon_view);
5091
5092   gtk_icon_view_invalidate_sizes (icon_view);
5093   
5094   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5095   
5096 }
5097
5098 /**
5099  * gtk_icon_view_get_pixbuf_column:
5100  * @icon_view: A #GtkIconView.
5101  *
5102  * Returns the column with pixbufs for @icon_view.
5103  *
5104  * Returns: the pixbuf column, or -1 if it's unset.
5105  *
5106  * Since: 2.6
5107  */
5108 gint
5109 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
5110 {
5111   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5112
5113   return icon_view->priv->pixbuf_column;
5114 }
5115
5116 /**
5117  * gtk_icon_view_select_path:
5118  * @icon_view: A #GtkIconView.
5119  * @path: The #GtkTreePath to be selected.
5120  * 
5121  * Selects the row at @path.
5122  *
5123  * Since: 2.6
5124  **/
5125 void
5126 gtk_icon_view_select_path (GtkIconView *icon_view,
5127                            GtkTreePath *path)
5128 {
5129   GtkIconViewItem *item = NULL;
5130
5131   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5132   g_return_if_fail (icon_view->priv->model != NULL);
5133   g_return_if_fail (path != NULL);
5134
5135   if (gtk_tree_path_get_depth (path) > 0)
5136     item = g_list_nth_data (icon_view->priv->items,
5137                             gtk_tree_path_get_indices(path)[0]);
5138
5139   if (item)
5140     _gtk_icon_view_select_item (icon_view, item);
5141 }
5142
5143 /**
5144  * gtk_icon_view_unselect_path:
5145  * @icon_view: A #GtkIconView.
5146  * @path: The #GtkTreePath to be unselected.
5147  * 
5148  * Unselects the row at @path.
5149  *
5150  * Since: 2.6
5151  **/
5152 void
5153 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5154                              GtkTreePath *path)
5155 {
5156   GtkIconViewItem *item;
5157   
5158   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5159   g_return_if_fail (icon_view->priv->model != NULL);
5160   g_return_if_fail (path != NULL);
5161
5162   item = g_list_nth_data (icon_view->priv->items,
5163                           gtk_tree_path_get_indices(path)[0]);
5164
5165   if (!item)
5166     return;
5167   
5168   _gtk_icon_view_unselect_item (icon_view, item);
5169 }
5170
5171 /**
5172  * gtk_icon_view_get_selected_items:
5173  * @icon_view: A #GtkIconView.
5174  *
5175  * Creates a list of paths of all selected items. Additionally, if you are
5176  * planning on modifying the model after calling this function, you may
5177  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5178  * To do this, you can use gtk_tree_row_reference_new().
5179  *
5180  * To free the return value, use:
5181  * |[
5182  * g_list_free_full (list, (GDestroyNotify) gtk_tree_patch_free);
5183  * ]|
5184  *
5185  * Return value: (element-type GtkTreePath) (transfer full): A #GList containing a #GtkTreePath for each selected row.
5186  *
5187  * Since: 2.6
5188  **/
5189 GList *
5190 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5191 {
5192   GList *list;
5193   GList *selected = NULL;
5194   
5195   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5196   
5197   for (list = icon_view->priv->items; list != NULL; list = list->next)
5198     {
5199       GtkIconViewItem *item = list->data;
5200
5201       if (item->selected)
5202         {
5203           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5204
5205           selected = g_list_prepend (selected, path);
5206         }
5207     }
5208
5209   return selected;
5210 }
5211
5212 /**
5213  * gtk_icon_view_select_all:
5214  * @icon_view: A #GtkIconView.
5215  * 
5216  * Selects all the icons. @icon_view must has its selection mode set
5217  * to #GTK_SELECTION_MULTIPLE.
5218  *
5219  * Since: 2.6
5220  **/
5221 void
5222 gtk_icon_view_select_all (GtkIconView *icon_view)
5223 {
5224   GList *items;
5225   gboolean dirty = FALSE;
5226   
5227   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5228
5229   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5230     return;
5231
5232   for (items = icon_view->priv->items; items; items = items->next)
5233     {
5234       GtkIconViewItem *item = items->data;
5235       
5236       if (!item->selected)
5237         {
5238           dirty = TRUE;
5239           item->selected = TRUE;
5240           gtk_icon_view_queue_draw_item (icon_view, item);
5241         }
5242     }
5243
5244   if (dirty)
5245     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5246 }
5247
5248 /**
5249  * gtk_icon_view_unselect_all:
5250  * @icon_view: A #GtkIconView.
5251  * 
5252  * Unselects all the icons.
5253  *
5254  * Since: 2.6
5255  **/
5256 void
5257 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5258 {
5259   gboolean dirty = FALSE;
5260   
5261   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5262
5263   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5264     return;
5265
5266   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5267
5268   if (dirty)
5269     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5270 }
5271
5272 /**
5273  * gtk_icon_view_path_is_selected:
5274  * @icon_view: A #GtkIconView.
5275  * @path: A #GtkTreePath to check selection on.
5276  * 
5277  * Returns %TRUE if the icon pointed to by @path is currently
5278  * selected. If @path does not point to a valid location, %FALSE is returned.
5279  * 
5280  * Return value: %TRUE if @path is selected.
5281  *
5282  * Since: 2.6
5283  **/
5284 gboolean
5285 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5286                                 GtkTreePath *path)
5287 {
5288   GtkIconViewItem *item;
5289   
5290   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5291   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5292   g_return_val_if_fail (path != NULL, FALSE);
5293   
5294   item = g_list_nth_data (icon_view->priv->items,
5295                           gtk_tree_path_get_indices(path)[0]);
5296
5297   if (!item)
5298     return FALSE;
5299   
5300   return item->selected;
5301 }
5302
5303 /**
5304  * gtk_icon_view_get_item_row:
5305  * @icon_view: a #GtkIconView
5306  * @path: the #GtkTreePath of the item
5307  *
5308  * Gets the row in which the item @path is currently
5309  * displayed. Row numbers start at 0.
5310  *
5311  * Returns: The row in which the item is displayed
5312  *
5313  * Since: 2.22
5314  */
5315 gint
5316 gtk_icon_view_get_item_row (GtkIconView *icon_view,
5317                             GtkTreePath *path)
5318 {
5319   GtkIconViewItem *item;
5320
5321   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5322   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5323   g_return_val_if_fail (path != NULL, -1);
5324
5325   item = g_list_nth_data (icon_view->priv->items,
5326                           gtk_tree_path_get_indices(path)[0]);
5327
5328   if (!item)
5329     return -1;
5330
5331   return item->row;
5332 }
5333
5334 /**
5335  * gtk_icon_view_get_item_column:
5336  * @icon_view: a #GtkIconView
5337  * @path: the #GtkTreePath of the item
5338  *
5339  * Gets the column in which the item @path is currently
5340  * displayed. Column numbers start at 0.
5341  *
5342  * Returns: The column in which the item is displayed
5343  *
5344  * Since: 2.22
5345  */
5346 gint
5347 gtk_icon_view_get_item_column (GtkIconView *icon_view,
5348                                GtkTreePath *path)
5349 {
5350   GtkIconViewItem *item;
5351
5352   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5353   g_return_val_if_fail (icon_view->priv->model != NULL, -1);
5354   g_return_val_if_fail (path != NULL, -1);
5355
5356   item = g_list_nth_data (icon_view->priv->items,
5357                           gtk_tree_path_get_indices(path)[0]);
5358
5359   if (!item)
5360     return -1;
5361
5362   return item->col;
5363 }
5364
5365 /**
5366  * gtk_icon_view_item_activated:
5367  * @icon_view: A #GtkIconView
5368  * @path: The #GtkTreePath to be activated
5369  * 
5370  * Activates the item determined by @path.
5371  *
5372  * Since: 2.6
5373  **/
5374 void
5375 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5376                               GtkTreePath      *path)
5377 {
5378   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5379   g_return_if_fail (path != NULL);
5380   
5381   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5382 }
5383
5384 /**
5385  * gtk_icon_view_set_item_orientation:
5386  * @icon_view: a #GtkIconView
5387  * @orientation: the relative position of texts and icons 
5388  * 
5389  * Sets the ::item-orientation property which determines whether the labels 
5390  * are drawn beside the icons instead of below.
5391  *
5392  * Since: 2.6
5393  **/
5394 void 
5395 gtk_icon_view_set_item_orientation (GtkIconView    *icon_view,
5396                                     GtkOrientation  orientation)
5397 {
5398   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5399
5400   if (icon_view->priv->item_orientation != orientation)
5401     {
5402       icon_view->priv->item_orientation = orientation;
5403
5404       if (icon_view->priv->cell_area)
5405         {
5406           if (GTK_IS_ORIENTABLE (icon_view->priv->cell_area))
5407             gtk_orientable_set_orientation (GTK_ORIENTABLE (icon_view->priv->cell_area), 
5408                                             icon_view->priv->item_orientation);
5409
5410           gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5411         }
5412
5413       gtk_icon_view_invalidate_sizes (icon_view);
5414
5415       update_text_cell (icon_view);
5416       update_pixbuf_cell (icon_view);
5417       
5418       g_object_notify (G_OBJECT (icon_view), "item-orientation");
5419     }
5420 }
5421
5422 /**
5423  * gtk_icon_view_get_item_orientation:
5424  * @icon_view: a #GtkIconView
5425  * 
5426  * Returns the value of the ::item-orientation property which determines 
5427  * whether the labels are drawn beside the icons instead of below. 
5428  * 
5429  * Return value: the relative position of texts and icons 
5430  *
5431  * Since: 2.6
5432  **/
5433 GtkOrientation
5434 gtk_icon_view_get_item_orientation (GtkIconView *icon_view)
5435 {
5436   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5437                         GTK_ORIENTATION_VERTICAL);
5438
5439   return icon_view->priv->item_orientation;
5440 }
5441
5442 /**
5443  * gtk_icon_view_set_columns:
5444  * @icon_view: a #GtkIconView
5445  * @columns: the number of columns
5446  * 
5447  * Sets the ::columns property which determines in how
5448  * many columns the icons are arranged. If @columns is
5449  * -1, the number of columns will be chosen automatically 
5450  * to fill the available area. 
5451  *
5452  * Since: 2.6
5453  */
5454 void 
5455 gtk_icon_view_set_columns (GtkIconView *icon_view,
5456                            gint         columns)
5457 {
5458   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5459   
5460   if (icon_view->priv->columns != columns)
5461     {
5462       icon_view->priv->columns = columns;
5463
5464       if (icon_view->priv->cell_area)
5465         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5466
5467       gtk_icon_view_queue_layout (icon_view);
5468       
5469       g_object_notify (G_OBJECT (icon_view), "columns");
5470     }  
5471 }
5472
5473 /**
5474  * gtk_icon_view_get_columns:
5475  * @icon_view: a #GtkIconView
5476  * 
5477  * Returns the value of the ::columns property.
5478  * 
5479  * Return value: the number of columns, or -1
5480  *
5481  * Since: 2.6
5482  */
5483 gint
5484 gtk_icon_view_get_columns (GtkIconView *icon_view)
5485 {
5486   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5487
5488   return icon_view->priv->columns;
5489 }
5490
5491 /**
5492  * gtk_icon_view_set_item_width:
5493  * @icon_view: a #GtkIconView
5494  * @item_width: the width for each item
5495  * 
5496  * Sets the ::item-width property which specifies the width 
5497  * to use for each item. If it is set to -1, the icon view will 
5498  * automatically determine a suitable item size.
5499  *
5500  * Since: 2.6
5501  */
5502 void 
5503 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5504                               gint         item_width)
5505 {
5506   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5507   
5508   if (icon_view->priv->item_width != item_width)
5509     {
5510       icon_view->priv->item_width = item_width;
5511       
5512       if (icon_view->priv->cell_area)
5513         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5514
5515       gtk_icon_view_invalidate_sizes (icon_view);
5516       
5517       update_text_cell (icon_view);
5518
5519       g_object_notify (G_OBJECT (icon_view), "item-width");
5520     }  
5521 }
5522
5523 /**
5524  * gtk_icon_view_get_item_width:
5525  * @icon_view: a #GtkIconView
5526  * 
5527  * Returns the value of the ::item-width property.
5528  * 
5529  * Return value: the width of a single item, or -1
5530  *
5531  * Since: 2.6
5532  */
5533 gint
5534 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5535 {
5536   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5537
5538   return icon_view->priv->item_width;
5539 }
5540
5541
5542 /**
5543  * gtk_icon_view_set_spacing:
5544  * @icon_view: a #GtkIconView
5545  * @spacing: the spacing
5546  * 
5547  * Sets the ::spacing property which specifies the space 
5548  * which is inserted between the cells (i.e. the icon and 
5549  * the text) of an item.
5550  *
5551  * Since: 2.6
5552  */
5553 void 
5554 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5555                            gint         spacing)
5556 {
5557   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5558   
5559   if (icon_view->priv->spacing != spacing)
5560     {
5561       icon_view->priv->spacing = spacing;
5562
5563       if (icon_view->priv->cell_area)
5564         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5565
5566       gtk_icon_view_invalidate_sizes (icon_view);
5567
5568       g_object_notify (G_OBJECT (icon_view), "spacing");
5569     }  
5570 }
5571
5572 /**
5573  * gtk_icon_view_get_spacing:
5574  * @icon_view: a #GtkIconView
5575  * 
5576  * Returns the value of the ::spacing property.
5577  * 
5578  * Return value: the space between cells 
5579  *
5580  * Since: 2.6
5581  */
5582 gint
5583 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5584 {
5585   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5586
5587   return icon_view->priv->spacing;
5588 }
5589
5590 /**
5591  * gtk_icon_view_set_row_spacing:
5592  * @icon_view: a #GtkIconView
5593  * @row_spacing: the row spacing
5594  * 
5595  * Sets the ::row-spacing property which specifies the space 
5596  * which is inserted between the rows of the icon view.
5597  *
5598  * Since: 2.6
5599  */
5600 void 
5601 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5602                                gint         row_spacing)
5603 {
5604   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5605   
5606   if (icon_view->priv->row_spacing != row_spacing)
5607     {
5608       icon_view->priv->row_spacing = row_spacing;
5609
5610       if (icon_view->priv->cell_area)
5611         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5612
5613       gtk_icon_view_invalidate_sizes (icon_view);
5614
5615       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5616     }  
5617 }
5618
5619 /**
5620  * gtk_icon_view_get_row_spacing:
5621  * @icon_view: a #GtkIconView
5622  * 
5623  * Returns the value of the ::row-spacing property.
5624  * 
5625  * Return value: the space between rows
5626  *
5627  * Since: 2.6
5628  */
5629 gint
5630 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5631 {
5632   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5633
5634   return icon_view->priv->row_spacing;
5635 }
5636
5637 /**
5638  * gtk_icon_view_set_column_spacing:
5639  * @icon_view: a #GtkIconView
5640  * @column_spacing: the column spacing
5641  * 
5642  * Sets the ::column-spacing property which specifies the space 
5643  * which is inserted between the columns of the icon view.
5644  *
5645  * Since: 2.6
5646  */
5647 void 
5648 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5649                                   gint         column_spacing)
5650 {
5651   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5652   
5653   if (icon_view->priv->column_spacing != column_spacing)
5654     {
5655       icon_view->priv->column_spacing = column_spacing;
5656
5657       if (icon_view->priv->cell_area)
5658         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5659
5660       gtk_icon_view_invalidate_sizes (icon_view);
5661
5662       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5663     }  
5664 }
5665
5666 /**
5667  * gtk_icon_view_get_column_spacing:
5668  * @icon_view: a #GtkIconView
5669  * 
5670  * Returns the value of the ::column-spacing property.
5671  * 
5672  * Return value: the space between columns
5673  *
5674  * Since: 2.6
5675  */
5676 gint
5677 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5678 {
5679   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5680
5681   return icon_view->priv->column_spacing;
5682 }
5683
5684 /**
5685  * gtk_icon_view_set_margin:
5686  * @icon_view: a #GtkIconView
5687  * @margin: the margin
5688  * 
5689  * Sets the ::margin property which specifies the space 
5690  * which is inserted at the top, bottom, left and right 
5691  * of the icon view.
5692  *
5693  * Since: 2.6
5694  */
5695 void 
5696 gtk_icon_view_set_margin (GtkIconView *icon_view,
5697                           gint         margin)
5698 {
5699   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5700   
5701   if (icon_view->priv->margin != margin)
5702     {
5703       icon_view->priv->margin = margin;
5704
5705       if (icon_view->priv->cell_area)
5706         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5707
5708       gtk_icon_view_invalidate_sizes (icon_view);
5709
5710       g_object_notify (G_OBJECT (icon_view), "margin");
5711     }  
5712 }
5713
5714 /**
5715  * gtk_icon_view_get_margin:
5716  * @icon_view: a #GtkIconView
5717  * 
5718  * Returns the value of the ::margin property.
5719  * 
5720  * Return value: the space at the borders 
5721  *
5722  * Since: 2.6
5723  */
5724 gint
5725 gtk_icon_view_get_margin (GtkIconView *icon_view)
5726 {
5727   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5728
5729   return icon_view->priv->margin;
5730 }
5731
5732 /**
5733  * gtk_icon_view_set_item_padding:
5734  * @icon_view: a #GtkIconView
5735  * @item_padding: the item padding
5736  *
5737  * Sets the #GtkIconView:item-padding property which specifies the padding
5738  * around each of the icon view's items.
5739  *
5740  * Since: 2.18
5741  */
5742 void
5743 gtk_icon_view_set_item_padding (GtkIconView *icon_view,
5744                                 gint         item_padding)
5745 {
5746   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5747   
5748   if (icon_view->priv->item_padding != item_padding)
5749     {
5750       icon_view->priv->item_padding = item_padding;
5751
5752       if (icon_view->priv->cell_area)
5753         gtk_cell_area_stop_editing (icon_view->priv->cell_area, TRUE);
5754
5755       gtk_icon_view_invalidate_sizes (icon_view);
5756
5757       g_object_notify (G_OBJECT (icon_view), "item-padding");
5758     }  
5759 }
5760
5761 /**
5762  * gtk_icon_view_get_item_padding:
5763  * @icon_view: a #GtkIconView
5764  * 
5765  * Returns the value of the ::item-padding property.
5766  * 
5767  * Return value: the padding around items
5768  *
5769  * Since: 2.18
5770  */
5771 gint
5772 gtk_icon_view_get_item_padding (GtkIconView *icon_view)
5773 {
5774   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5775
5776   return icon_view->priv->item_padding;
5777 }
5778
5779 /* Get/set whether drag_motion requested the drag data and
5780  * drag_data_received should thus not actually insert the data,
5781  * since the data doesn't result from a drop.
5782  */
5783 static void
5784 set_status_pending (GdkDragContext *context,
5785                     GdkDragAction   suggested_action)
5786 {
5787   g_object_set_data (G_OBJECT (context),
5788                      I_("gtk-icon-view-status-pending"),
5789                      GINT_TO_POINTER (suggested_action));
5790 }
5791
5792 static GdkDragAction
5793 get_status_pending (GdkDragContext *context)
5794 {
5795   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5796                                              "gtk-icon-view-status-pending"));
5797 }
5798
5799 static void
5800 unset_reorderable (GtkIconView *icon_view)
5801 {
5802   if (icon_view->priv->reorderable)
5803     {
5804       icon_view->priv->reorderable = FALSE;
5805       g_object_notify (G_OBJECT (icon_view), "reorderable");
5806     }
5807 }
5808
5809 static void
5810 set_source_row (GdkDragContext *context,
5811                 GtkTreeModel   *model,
5812                 GtkTreePath    *source_row)
5813 {
5814   if (source_row)
5815     g_object_set_data_full (G_OBJECT (context),
5816                             I_("gtk-icon-view-source-row"),
5817                             gtk_tree_row_reference_new (model, source_row),
5818                             (GDestroyNotify) gtk_tree_row_reference_free);
5819   else
5820     g_object_set_data_full (G_OBJECT (context),
5821                             I_("gtk-icon-view-source-row"),
5822                             NULL, NULL);
5823 }
5824
5825 static GtkTreePath*
5826 get_source_row (GdkDragContext *context)
5827 {
5828   GtkTreeRowReference *ref;
5829
5830   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5831
5832   if (ref)
5833     return gtk_tree_row_reference_get_path (ref);
5834   else
5835     return NULL;
5836 }
5837
5838 typedef struct
5839 {
5840   GtkTreeRowReference *dest_row;
5841   gboolean             empty_view_drop;
5842   gboolean             drop_append_mode;
5843 } DestRow;
5844
5845 static void
5846 dest_row_free (gpointer data)
5847 {
5848   DestRow *dr = (DestRow *)data;
5849
5850   gtk_tree_row_reference_free (dr->dest_row);
5851   g_free (dr);
5852 }
5853
5854 static void
5855 set_dest_row (GdkDragContext *context,
5856               GtkTreeModel   *model,
5857               GtkTreePath    *dest_row,
5858               gboolean        empty_view_drop,
5859               gboolean        drop_append_mode)
5860 {
5861   DestRow *dr;
5862
5863   if (!dest_row)
5864     {
5865       g_object_set_data_full (G_OBJECT (context),
5866                               I_("gtk-icon-view-dest-row"),
5867                               NULL, NULL);
5868       return;
5869     }
5870   
5871   dr = g_new0 (DestRow, 1);
5872      
5873   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5874   dr->empty_view_drop = empty_view_drop;
5875   dr->drop_append_mode = drop_append_mode;
5876   g_object_set_data_full (G_OBJECT (context),
5877                           I_("gtk-icon-view-dest-row"),
5878                           dr, (GDestroyNotify) dest_row_free);
5879 }
5880
5881 static GtkTreePath*
5882 get_dest_row (GdkDragContext *context)
5883 {
5884   DestRow *dr;
5885
5886   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
5887
5888   if (dr)
5889     {
5890       GtkTreePath *path = NULL;
5891       
5892       if (dr->dest_row)
5893         path = gtk_tree_row_reference_get_path (dr->dest_row);
5894       else if (dr->empty_view_drop)
5895         path = gtk_tree_path_new_from_indices (0, -1);
5896       else
5897         path = NULL;
5898
5899       if (path && dr->drop_append_mode)
5900         gtk_tree_path_next (path);
5901
5902       return path;
5903     }
5904   else
5905     return NULL;
5906 }
5907
5908 static gboolean
5909 check_model_dnd (GtkTreeModel *model,
5910                  GType         required_iface,
5911                  const gchar  *signal)
5912 {
5913   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5914     {
5915       g_warning ("You must override the default '%s' handler "
5916                  "on GtkIconView when using models that don't support "
5917                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5918                  "is to connect to '%s' and call "
5919                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5920                  "the default handler from running. Look at the source code "
5921                  "for the default handler in gtkiconview.c to get an idea what "
5922                  "your handler should do. (gtkiconview.c is in the GTK+ source "
5923                  "code.) If you're using GTK+ from a language other than C, "
5924                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5925                  signal, g_type_name (required_iface), signal);
5926       return FALSE;
5927     }
5928   else
5929     return TRUE;
5930 }
5931
5932 static void
5933 remove_scroll_timeout (GtkIconView *icon_view)
5934 {
5935   if (icon_view->priv->scroll_timeout_id != 0)
5936     {
5937       g_source_remove (icon_view->priv->scroll_timeout_id);
5938
5939       icon_view->priv->scroll_timeout_id = 0;
5940     }
5941 }
5942
5943 static void
5944 gtk_icon_view_autoscroll (GtkIconView *icon_view,
5945                           GdkDevice   *device)
5946 {
5947   GdkWindow *window;
5948   gint px, py, x, y, width, height;
5949   gint hoffset, voffset;
5950
5951   window = gtk_widget_get_window (GTK_WIDGET (icon_view));
5952
5953   gdk_window_get_device_position (window, device, &px, &py, NULL);
5954   gdk_window_get_geometry (window, &x, &y, &width, &height);
5955
5956   /* see if we are near the edge. */
5957   voffset = py - (y + 2 * SCROLL_EDGE_SIZE);
5958   if (voffset > 0)
5959     voffset = MAX (py - (y + height - 2 * SCROLL_EDGE_SIZE), 0);
5960
5961   hoffset = px - (x + 2 * SCROLL_EDGE_SIZE);
5962   if (hoffset > 0)
5963     hoffset = MAX (px - (x + width - 2 * SCROLL_EDGE_SIZE), 0);
5964
5965   if (voffset != 0)
5966     gtk_adjustment_set_value (icon_view->priv->vadjustment,
5967                               gtk_adjustment_get_value (icon_view->priv->vadjustment) + voffset);
5968
5969   if (hoffset != 0)
5970     gtk_adjustment_set_value (icon_view->priv->hadjustment,
5971                               gtk_adjustment_get_value (icon_view->priv->hadjustment) + hoffset);
5972 }
5973
5974 typedef struct {
5975   GtkIconView *icon_view;
5976   GdkDevice   *device;
5977 } DragScrollData;
5978
5979 static gboolean
5980 drag_scroll_timeout (gpointer datap)
5981 {
5982   DragScrollData *data = datap;
5983
5984   gtk_icon_view_autoscroll (data->icon_view, data->device);
5985
5986   return TRUE;
5987 }
5988
5989 static void
5990 drag_scroll_data_free (DragScrollData *data)
5991 {
5992   g_slice_free (DragScrollData, data);
5993 }
5994
5995 static gboolean
5996 set_destination (GtkIconView    *icon_view,
5997                  GdkDragContext *context,
5998                  gint            x,
5999                  gint            y,
6000                  GdkDragAction  *suggested_action,
6001                  GdkAtom        *target)
6002 {
6003   GtkWidget *widget;
6004   GtkTreePath *path = NULL;
6005   GtkIconViewDropPosition pos;
6006   GtkIconViewDropPosition old_pos;
6007   GtkTreePath *old_dest_path = NULL;
6008   gboolean can_drop = FALSE;
6009
6010   widget = GTK_WIDGET (icon_view);
6011
6012   *suggested_action = 0;
6013   *target = GDK_NONE;
6014
6015   if (!icon_view->priv->dest_set)
6016     {
6017       /* someone unset us as a drag dest, note that if
6018        * we return FALSE drag_leave isn't called
6019        */
6020
6021       gtk_icon_view_set_drag_dest_item (icon_view,
6022                                         NULL,
6023                                         GTK_ICON_VIEW_DROP_LEFT);
6024
6025       remove_scroll_timeout (GTK_ICON_VIEW (widget));
6026
6027       return FALSE; /* no longer a drop site */
6028     }
6029
6030   *target = gtk_drag_dest_find_target (widget, context,
6031                                        gtk_drag_dest_get_target_list (widget));
6032   if (*target == GDK_NONE)
6033     return FALSE;
6034
6035   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
6036     {
6037       gint n_children;
6038       GtkTreeModel *model;
6039       
6040       /* the row got dropped on empty space, let's setup a special case
6041        */
6042
6043       if (path)
6044         gtk_tree_path_free (path);
6045
6046       model = gtk_icon_view_get_model (icon_view);
6047
6048       n_children = gtk_tree_model_iter_n_children (model, NULL);
6049       if (n_children)
6050         {
6051           pos = GTK_ICON_VIEW_DROP_BELOW;
6052           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
6053         }
6054       else
6055         {
6056           pos = GTK_ICON_VIEW_DROP_ABOVE;
6057           path = gtk_tree_path_new_from_indices (0, -1);
6058         }
6059
6060       can_drop = TRUE;
6061
6062       goto out;
6063     }
6064
6065   g_assert (path);
6066
6067   gtk_icon_view_get_drag_dest_item (icon_view,
6068                                     &old_dest_path,
6069                                     &old_pos);
6070   
6071   if (old_dest_path)
6072     gtk_tree_path_free (old_dest_path);
6073   
6074   if (TRUE /* FIXME if the location droppable predicate */)
6075     {
6076       can_drop = TRUE;
6077     }
6078
6079 out:
6080   if (can_drop)
6081     {
6082       GtkWidget *source_widget;
6083
6084       *suggested_action = gdk_drag_context_get_suggested_action (context);
6085       source_widget = gtk_drag_get_source_widget (context);
6086
6087       if (source_widget == widget)
6088         {
6089           /* Default to MOVE, unless the user has
6090            * pressed ctrl or shift to affect available actions
6091            */
6092           if ((gdk_drag_context_get_actions (context) & GDK_ACTION_MOVE) != 0)
6093             *suggested_action = GDK_ACTION_MOVE;
6094         }
6095
6096       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6097                                         path, pos);
6098     }
6099   else
6100     {
6101       /* can't drop here */
6102       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
6103                                         NULL,
6104                                         GTK_ICON_VIEW_DROP_LEFT);
6105     }
6106   
6107   if (path)
6108     gtk_tree_path_free (path);
6109   
6110   return TRUE;
6111 }
6112
6113 static GtkTreePath*
6114 get_logical_destination (GtkIconView *icon_view,
6115                          gboolean    *drop_append_mode)
6116 {
6117   /* adjust path to point to the row the drop goes in front of */
6118   GtkTreePath *path = NULL;
6119   GtkIconViewDropPosition pos;
6120   
6121   *drop_append_mode = FALSE;
6122
6123   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6124
6125   if (path == NULL)
6126     return NULL;
6127
6128   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
6129       pos == GTK_ICON_VIEW_DROP_BELOW)
6130     {
6131       GtkTreeIter iter;
6132       GtkTreeModel *model = icon_view->priv->model;
6133
6134       if (!gtk_tree_model_get_iter (model, &iter, path) ||
6135           !gtk_tree_model_iter_next (model, &iter))
6136         *drop_append_mode = TRUE;
6137       else
6138         {
6139           *drop_append_mode = FALSE;
6140           gtk_tree_path_next (path);
6141         }      
6142     }
6143
6144   return path;
6145 }
6146
6147 static gboolean
6148 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
6149                                 GdkEventMotion *event)
6150 {
6151   GtkWidget *widget = GTK_WIDGET (icon_view);
6152   GdkDragContext *context;
6153   GtkTreePath *path = NULL;
6154   gint button;
6155   GtkTreeModel *model;
6156   gboolean retval = FALSE;
6157
6158   if (!icon_view->priv->source_set)
6159     goto out;
6160
6161   if (icon_view->priv->pressed_button < 0)
6162     goto out;
6163
6164   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
6165                                  icon_view->priv->press_start_x,
6166                                  icon_view->priv->press_start_y,
6167                                  event->x, event->y))
6168     goto out;
6169
6170   model = gtk_icon_view_get_model (icon_view);
6171
6172   if (model == NULL)
6173     goto out;
6174
6175   button = icon_view->priv->pressed_button;
6176   icon_view->priv->pressed_button = -1;
6177
6178   path = gtk_icon_view_get_path_at_pos (icon_view,
6179                                         icon_view->priv->press_start_x,
6180                                         icon_view->priv->press_start_y);
6181
6182   if (path == NULL)
6183     goto out;
6184
6185   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
6186       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
6187                                            path))
6188     goto out;
6189
6190   /* FIXME Check whether we're a start button, if not return FALSE and
6191    * free path
6192    */
6193
6194   /* Now we can begin the drag */
6195   
6196   retval = TRUE;
6197
6198   context = gtk_drag_begin (widget,
6199                             gtk_drag_source_get_target_list (widget),
6200                             icon_view->priv->source_actions,
6201                             button,
6202                             (GdkEvent*)event);
6203
6204   set_source_row (context, model, path);
6205   
6206  out:
6207   if (path)
6208     gtk_tree_path_free (path);
6209
6210   return retval;
6211 }
6212
6213 /* Source side drag signals */
6214 static void 
6215 gtk_icon_view_drag_begin (GtkWidget      *widget,
6216                           GdkDragContext *context)
6217 {
6218   GtkIconView *icon_view;
6219   GtkIconViewItem *item;
6220   cairo_surface_t *icon;
6221   gint x, y;
6222   GtkTreePath *path;
6223
6224   icon_view = GTK_ICON_VIEW (widget);
6225
6226   /* if the user uses a custom DnD impl, we don't set the icon here */
6227   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6228     return;
6229
6230   item = _gtk_icon_view_get_item_at_coords (icon_view,
6231                                            icon_view->priv->press_start_x,
6232                                            icon_view->priv->press_start_y,
6233                                            TRUE,
6234                                            NULL);
6235
6236   g_return_if_fail (item != NULL);
6237
6238   x = icon_view->priv->press_start_x - item->cell_area.x + 1;
6239   y = icon_view->priv->press_start_y - item->cell_area.y + 1;
6240   
6241   path = gtk_tree_path_new_from_indices (item->index, -1);
6242   icon = gtk_icon_view_create_drag_icon (icon_view, path);
6243   gtk_tree_path_free (path);
6244
6245   cairo_surface_set_device_offset (icon, -x, -y);
6246
6247   gtk_drag_set_icon_surface (context, icon);
6248
6249   cairo_surface_destroy (icon);
6250 }
6251
6252 static void 
6253 gtk_icon_view_drag_end (GtkWidget      *widget,
6254                         GdkDragContext *context)
6255 {
6256   /* do nothing */
6257 }
6258
6259 static void 
6260 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6261                              GdkDragContext   *context,
6262                              GtkSelectionData *selection_data,
6263                              guint             info,
6264                              guint             time)
6265 {
6266   GtkIconView *icon_view;
6267   GtkTreeModel *model;
6268   GtkTreePath *source_row;
6269
6270   icon_view = GTK_ICON_VIEW (widget);
6271   model = gtk_icon_view_get_model (icon_view);
6272
6273   if (model == NULL)
6274     return;
6275
6276   if (!icon_view->priv->source_set)
6277     return;
6278
6279   source_row = get_source_row (context);
6280
6281   if (source_row == NULL)
6282     return;
6283
6284   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6285    * any model; for DragSource models there are some other targets
6286    * we also support.
6287    */
6288
6289   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6290       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6291                                           source_row,
6292                                           selection_data))
6293     goto done;
6294
6295   /* If drag_data_get does nothing, try providing row data. */
6296   if (gtk_selection_data_get_target (selection_data) == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6297     gtk_tree_set_row_drag_data (selection_data,
6298                                 model,
6299                                 source_row);
6300
6301  done:
6302   gtk_tree_path_free (source_row);
6303 }
6304
6305 static void 
6306 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6307                                 GdkDragContext *context)
6308 {
6309   GtkTreeModel *model;
6310   GtkIconView *icon_view;
6311   GtkTreePath *source_row;
6312
6313   icon_view = GTK_ICON_VIEW (widget);
6314   model = gtk_icon_view_get_model (icon_view);
6315
6316   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag-data-delete"))
6317     return;
6318
6319   if (!icon_view->priv->source_set)
6320     return;
6321
6322   source_row = get_source_row (context);
6323
6324   if (source_row == NULL)
6325     return;
6326
6327   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6328                                          source_row);
6329
6330   gtk_tree_path_free (source_row);
6331
6332   set_source_row (context, NULL, NULL);
6333 }
6334
6335 /* Target side drag signals */
6336 static void
6337 gtk_icon_view_drag_leave (GtkWidget      *widget,
6338                           GdkDragContext *context,
6339                           guint           time)
6340 {
6341   GtkIconView *icon_view;
6342
6343   icon_view = GTK_ICON_VIEW (widget);
6344
6345   /* unset any highlight row */
6346   gtk_icon_view_set_drag_dest_item (icon_view,
6347                                     NULL,
6348                                     GTK_ICON_VIEW_DROP_LEFT);
6349
6350   remove_scroll_timeout (icon_view);
6351 }
6352
6353 static gboolean 
6354 gtk_icon_view_drag_motion (GtkWidget      *widget,
6355                            GdkDragContext *context,
6356                            gint            x,
6357                            gint            y,
6358                            guint           time)
6359 {
6360   GtkTreePath *path = NULL;
6361   GtkIconViewDropPosition pos;
6362   GtkIconView *icon_view;
6363   GdkDragAction suggested_action = 0;
6364   GdkAtom target;
6365   gboolean empty;
6366
6367   icon_view = GTK_ICON_VIEW (widget);
6368
6369   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6370     return FALSE;
6371
6372   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6373
6374   /* we only know this *after* set_desination_row */
6375   empty = icon_view->priv->empty_view_drop;
6376
6377   if (path == NULL && !empty)
6378     {
6379       /* Can't drop here. */
6380       gdk_drag_status (context, 0, time);
6381     }
6382   else
6383     {
6384       if (icon_view->priv->scroll_timeout_id == 0)
6385         {
6386           DragScrollData *data = g_slice_new (DragScrollData);
6387           data->icon_view = icon_view;
6388           data->device = gdk_drag_context_get_device (context);
6389
6390           icon_view->priv->scroll_timeout_id =
6391             gdk_threads_add_timeout_full (G_PRIORITY_DEFAULT, 50, drag_scroll_timeout, data, (GDestroyNotify) drag_scroll_data_free);
6392         }
6393
6394       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6395         {
6396           /* Request data so we can use the source row when
6397            * determining whether to accept the drop
6398            */
6399           set_status_pending (context, suggested_action);
6400           gtk_drag_get_data (widget, context, target, time);
6401         }
6402       else
6403         {
6404           set_status_pending (context, 0);
6405           gdk_drag_status (context, suggested_action, time);
6406         }
6407     }
6408
6409   if (path)
6410     gtk_tree_path_free (path);
6411
6412   return TRUE;
6413 }
6414
6415 static gboolean 
6416 gtk_icon_view_drag_drop (GtkWidget      *widget,
6417                          GdkDragContext *context,
6418                          gint            x,
6419                          gint            y,
6420                          guint           time)
6421 {
6422   GtkIconView *icon_view;
6423   GtkTreePath *path;
6424   GdkDragAction suggested_action = 0;
6425   GdkAtom target = GDK_NONE;
6426   GtkTreeModel *model;
6427   gboolean drop_append_mode;
6428
6429   icon_view = GTK_ICON_VIEW (widget);
6430   model = gtk_icon_view_get_model (icon_view);
6431
6432   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6433
6434   if (!icon_view->priv->dest_set)
6435     return FALSE;
6436
6437   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-drop"))
6438     return FALSE;
6439
6440   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6441     return FALSE;
6442   
6443   path = get_logical_destination (icon_view, &drop_append_mode);
6444
6445   if (target != GDK_NONE && path != NULL)
6446     {
6447       /* in case a motion had requested drag data, change things so we
6448        * treat drag data receives as a drop.
6449        */
6450       set_status_pending (context, 0);
6451       set_dest_row (context, model, path, 
6452                     icon_view->priv->empty_view_drop, drop_append_mode);
6453     }
6454
6455   if (path)
6456     gtk_tree_path_free (path);
6457
6458   /* Unset this thing */
6459   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6460
6461   if (target != GDK_NONE)
6462     {
6463       gtk_drag_get_data (widget, context, target, time);
6464       return TRUE;
6465     }
6466   else
6467     return FALSE;
6468 }
6469
6470 static void
6471 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6472                                   GdkDragContext   *context,
6473                                   gint              x,
6474                                   gint              y,
6475                                   GtkSelectionData *selection_data,
6476                                   guint             info,
6477                                   guint             time)
6478 {
6479   GtkTreePath *path;
6480   gboolean accepted = FALSE;
6481   GtkTreeModel *model;
6482   GtkIconView *icon_view;
6483   GtkTreePath *dest_row;
6484   GdkDragAction suggested_action;
6485   gboolean drop_append_mode;
6486   
6487   icon_view = GTK_ICON_VIEW (widget);  
6488   model = gtk_icon_view_get_model (icon_view);
6489
6490   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag-data-received"))
6491     return;
6492
6493   if (!icon_view->priv->dest_set)
6494     return;
6495
6496   suggested_action = get_status_pending (context);
6497
6498   if (suggested_action)
6499     {
6500       /* We are getting this data due to a request in drag_motion,
6501        * rather than due to a request in drag_drop, so we are just
6502        * supposed to call drag_status, not actually paste in the
6503        * data.
6504        */
6505       path = get_logical_destination (icon_view, &drop_append_mode);
6506
6507       if (path == NULL)
6508         suggested_action = 0;
6509
6510       if (suggested_action)
6511         {
6512           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6513                                                      path,
6514                                                      selection_data))
6515             suggested_action = 0;
6516         }
6517
6518       gdk_drag_status (context, suggested_action, time);
6519
6520       if (path)
6521         gtk_tree_path_free (path);
6522
6523       /* If you can't drop, remove user drop indicator until the next motion */
6524       if (suggested_action == 0)
6525         gtk_icon_view_set_drag_dest_item (icon_view,
6526                                           NULL,
6527                                           GTK_ICON_VIEW_DROP_LEFT);
6528       return;
6529     }
6530   
6531
6532   dest_row = get_dest_row (context);
6533
6534   if (dest_row == NULL)
6535     return;
6536
6537   if (gtk_selection_data_get_length (selection_data) >= 0)
6538     {
6539       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6540                                                  dest_row,
6541                                                  selection_data))
6542         accepted = TRUE;
6543     }
6544
6545   gtk_drag_finish (context,
6546                    accepted,
6547                    (gdk_drag_context_get_selected_action (context) == GDK_ACTION_MOVE),
6548                    time);
6549
6550   gtk_tree_path_free (dest_row);
6551
6552   /* drop dest_row */
6553   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6554 }
6555
6556 /* Drag-and-Drop support */
6557 /**
6558  * gtk_icon_view_enable_model_drag_source:
6559  * @icon_view: a #GtkIconTreeView
6560  * @start_button_mask: Mask of allowed buttons to start drag
6561  * @targets: (array length=n_targets): the table of targets that the drag will
6562  *           support
6563  * @n_targets: the number of items in @targets
6564  * @actions: the bitmask of possible actions for a drag from this
6565  *    widget
6566  *
6567  * Turns @icon_view into a drag source for automatic DND. Calling this
6568  * method sets #GtkIconView:reorderable to %FALSE.
6569  *
6570  * Since: 2.8
6571  **/
6572 void
6573 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6574                                         GdkModifierType           start_button_mask,
6575                                         const GtkTargetEntry     *targets,
6576                                         gint                      n_targets,
6577                                         GdkDragAction             actions)
6578 {
6579   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6580
6581   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6582
6583   icon_view->priv->start_button_mask = start_button_mask;
6584   icon_view->priv->source_actions = actions;
6585
6586   icon_view->priv->source_set = TRUE;
6587
6588   unset_reorderable (icon_view);
6589 }
6590
6591 /**
6592  * gtk_icon_view_enable_model_drag_dest:
6593  * @icon_view: a #GtkIconView
6594  * @targets: (array length=n_targets): the table of targets that the drag will
6595  *           support
6596  * @n_targets: the number of items in @targets
6597  * @actions: the bitmask of possible actions for a drag to this
6598  *    widget
6599  *
6600  * Turns @icon_view into a drop destination for automatic DND. Calling this
6601  * method sets #GtkIconView:reorderable to %FALSE.
6602  *
6603  * Since: 2.8
6604  **/
6605 void 
6606 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6607                                       const GtkTargetEntry *targets,
6608                                       gint                  n_targets,
6609                                       GdkDragAction         actions)
6610 {
6611   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6612
6613   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, targets, n_targets, actions);
6614
6615   icon_view->priv->dest_actions = actions;
6616
6617   icon_view->priv->dest_set = TRUE;
6618
6619   unset_reorderable (icon_view);  
6620 }
6621
6622 /**
6623  * gtk_icon_view_unset_model_drag_source:
6624  * @icon_view: a #GtkIconView
6625  * 
6626  * Undoes the effect of gtk_icon_view_enable_model_drag_source(). Calling this
6627  * method sets #GtkIconView:reorderable to %FALSE.
6628  *
6629  * Since: 2.8
6630  **/
6631 void
6632 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6633 {
6634   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6635
6636   if (icon_view->priv->source_set)
6637     {
6638       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6639       icon_view->priv->source_set = FALSE;
6640     }
6641
6642   unset_reorderable (icon_view);
6643 }
6644
6645 /**
6646  * gtk_icon_view_unset_model_drag_dest:
6647  * @icon_view: a #GtkIconView
6648  * 
6649  * Undoes the effect of gtk_icon_view_enable_model_drag_dest(). Calling this
6650  * method sets #GtkIconView:reorderable to %FALSE.
6651  *
6652  * Since: 2.8
6653  **/
6654 void
6655 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6656 {
6657   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6658
6659   if (icon_view->priv->dest_set)
6660     {
6661       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6662       icon_view->priv->dest_set = FALSE;
6663     }
6664
6665   unset_reorderable (icon_view);
6666 }
6667
6668 /* These are useful to implement your own custom stuff. */
6669 /**
6670  * gtk_icon_view_set_drag_dest_item:
6671  * @icon_view: a #GtkIconView
6672  * @path: (allow-none): The path of the item to highlight, or %NULL.
6673  * @pos: Specifies where to drop, relative to the item
6674  *
6675  * Sets the item that is highlighted for feedback.
6676  *
6677  * Since: 2.8
6678  */
6679 void
6680 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6681                                   GtkTreePath              *path,
6682                                   GtkIconViewDropPosition   pos)
6683 {
6684   /* Note; this function is exported to allow a custom DND
6685    * implementation, so it can't touch TreeViewDragInfo
6686    */
6687
6688   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6689
6690   if (icon_view->priv->dest_item)
6691     {
6692       GtkTreePath *current_path;
6693       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6694       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6695       icon_view->priv->dest_item = NULL;      
6696
6697       gtk_icon_view_queue_draw_path (icon_view, current_path);
6698       gtk_tree_path_free (current_path);
6699     }
6700   
6701   /* special case a drop on an empty model */
6702   icon_view->priv->empty_view_drop = FALSE;
6703   if (pos == GTK_ICON_VIEW_DROP_ABOVE && path
6704       && gtk_tree_path_get_depth (path) == 1
6705       && gtk_tree_path_get_indices (path)[0] == 0)
6706     {
6707       gint n_children;
6708
6709       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6710                                                    NULL);
6711
6712       if (n_children == 0)
6713         icon_view->priv->empty_view_drop = TRUE;
6714     }
6715
6716   icon_view->priv->dest_pos = pos;
6717
6718   if (path)
6719     {
6720       icon_view->priv->dest_item =
6721         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
6722                                           icon_view->priv->model, path);
6723       
6724       gtk_icon_view_queue_draw_path (icon_view, path);
6725     }
6726 }
6727
6728 /**
6729  * gtk_icon_view_get_drag_dest_item:
6730  * @icon_view: a #GtkIconView
6731  * @path: (out) (allow-none): Return location for the path of
6732  *        the highlighted item, or %NULL.
6733  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6734  * 
6735  * Gets information about the item that is highlighted for feedback.
6736  *
6737  * Since: 2.8
6738  **/
6739 void
6740 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
6741                                   GtkTreePath             **path,
6742                                   GtkIconViewDropPosition  *pos)
6743 {
6744   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6745
6746   if (path)
6747     {
6748       if (icon_view->priv->dest_item)
6749         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6750       else
6751         *path = NULL;
6752     }
6753
6754   if (pos)
6755     *pos = icon_view->priv->dest_pos;
6756 }
6757
6758 /**
6759  * gtk_icon_view_get_dest_item_at_pos:
6760  * @icon_view: a #GtkIconView
6761  * @drag_x: the position to determine the destination item for
6762  * @drag_y: the position to determine the destination item for
6763  * @path: (out) (allow-none): Return location for the path of the item,
6764  *    or %NULL.
6765  * @pos: (out) (allow-none): Return location for the drop position, or %NULL
6766  * 
6767  * Determines the destination item for a given position.
6768  * 
6769  * Return value: whether there is an item at the given position.
6770  *
6771  * Since: 2.8
6772  **/
6773 gboolean
6774 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
6775                                     gint                      drag_x,
6776                                     gint                      drag_y,
6777                                     GtkTreePath             **path,
6778                                     GtkIconViewDropPosition  *pos)
6779 {
6780   GtkIconViewItem *item;
6781
6782   /* Note; this function is exported to allow a custom DND
6783    * implementation, so it can't touch TreeViewDragInfo
6784    */
6785
6786   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6787   g_return_val_if_fail (drag_x >= 0, FALSE);
6788   g_return_val_if_fail (drag_y >= 0, FALSE);
6789   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6790
6791
6792   if (path)
6793     *path = NULL;
6794
6795   item = _gtk_icon_view_get_item_at_coords (icon_view, 
6796                                            drag_x + gtk_adjustment_get_value (icon_view->priv->hadjustment), 
6797                                            drag_y + gtk_adjustment_get_value (icon_view->priv->vadjustment),
6798                                            FALSE, NULL);
6799
6800   if (item == NULL)
6801     return FALSE;
6802
6803   if (path)
6804     *path = gtk_tree_path_new_from_indices (item->index, -1);
6805
6806   if (pos)
6807     {
6808       if (drag_x < item->cell_area.x + item->cell_area.width / 4)
6809         *pos = GTK_ICON_VIEW_DROP_LEFT;
6810       else if (drag_x > item->cell_area.x + item->cell_area.width * 3 / 4)
6811         *pos = GTK_ICON_VIEW_DROP_RIGHT;
6812       else if (drag_y < item->cell_area.y + item->cell_area.height / 4)
6813         *pos = GTK_ICON_VIEW_DROP_ABOVE;
6814       else if (drag_y > item->cell_area.y + item->cell_area.height * 3 / 4)
6815         *pos = GTK_ICON_VIEW_DROP_BELOW;
6816       else
6817         *pos = GTK_ICON_VIEW_DROP_INTO;
6818     }
6819
6820   return TRUE;
6821 }
6822
6823 /**
6824  * gtk_icon_view_create_drag_icon:
6825  * @icon_view: a #GtkIconView
6826  * @path: a #GtkTreePath in @icon_view
6827  *
6828  * Creates a #cairo_surface_t representation of the item at @path.  
6829  * This image is used for a drag icon.
6830  *
6831  * Return value: (transfer full): a newly-allocated surface of the drag icon.
6832  * 
6833  * Since: 2.8
6834  **/
6835 cairo_surface_t *
6836 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6837                                 GtkTreePath *path)
6838 {
6839   GtkWidget *widget;
6840   GtkStyleContext *context;
6841   cairo_t *cr;
6842   cairo_surface_t *surface;
6843   GList *l;
6844   gint index;
6845
6846   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
6847   g_return_val_if_fail (path != NULL, NULL);
6848
6849   widget = GTK_WIDGET (icon_view);
6850   context = gtk_widget_get_style_context (widget);
6851
6852   if (!gtk_widget_get_realized (widget))
6853     return NULL;
6854
6855   index = gtk_tree_path_get_indices (path)[0];
6856
6857   for (l = icon_view->priv->items; l; l = l->next) 
6858     {
6859       GtkIconViewItem *item = l->data;
6860       
6861       if (index == item->index)
6862         {
6863           GdkRectangle rect = { 
6864             item->cell_area.x - icon_view->priv->item_padding, 
6865             item->cell_area.y - icon_view->priv->item_padding, 
6866             item->cell_area.width  + icon_view->priv->item_padding * 2, 
6867             item->cell_area.height + icon_view->priv->item_padding * 2 
6868           };
6869
6870           surface = gdk_window_create_similar_surface (icon_view->priv->bin_window,
6871                                                        CAIRO_CONTENT_COLOR,
6872                                                        rect.width + 2,
6873                                                        rect.height + 2);
6874
6875           cr = cairo_create (surface);
6876           cairo_set_line_width (cr, 1.);
6877
6878           gtk_render_background (context, cr, 0, 0,
6879                                  rect.width + 2, rect.height + 2);
6880
6881           cairo_save (cr);
6882
6883           cairo_rectangle (cr, 1, 1, rect.width, rect.height);
6884           cairo_clip (cr);
6885
6886           gtk_icon_view_paint_item (icon_view, cr, item, 
6887                                     icon_view->priv->item_padding + 1, 
6888                                     icon_view->priv->item_padding + 1, FALSE);
6889
6890           cairo_restore (cr);
6891
6892           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
6893           cairo_rectangle (cr, 0.5, 0.5, rect.width + 1, rect.height + 1);
6894           cairo_stroke (cr);
6895
6896           cairo_destroy (cr);
6897
6898           return surface;
6899         }
6900     }
6901   
6902   return NULL;
6903 }
6904
6905 /**
6906  * gtk_icon_view_get_reorderable:
6907  * @icon_view: a #GtkIconView
6908  *
6909  * Retrieves whether the user can reorder the list via drag-and-drop. 
6910  * See gtk_icon_view_set_reorderable().
6911  *
6912  * Return value: %TRUE if the list can be reordered.
6913  *
6914  * Since: 2.8
6915  **/
6916 gboolean
6917 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
6918 {
6919   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6920
6921   return icon_view->priv->reorderable;
6922 }
6923
6924 static const GtkTargetEntry item_targets[] = {
6925   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
6926 };
6927
6928
6929 /**
6930  * gtk_icon_view_set_reorderable:
6931  * @icon_view: A #GtkIconView.
6932  * @reorderable: %TRUE, if the list of items can be reordered.
6933  *
6934  * This function is a convenience function to allow you to reorder models that
6935  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
6936  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
6937  * the user can reorder the model by dragging and dropping rows.  The
6938  * developer can listen to these changes by connecting to the model's
6939  * row_inserted and row_deleted signals. The reordering is implemented by setting up
6940  * the icon view as a drag source and destination. Therefore, drag and
6941  * drop can not be used in a reorderable view for any other purpose.
6942  *
6943  * This function does not give you any degree of control over the order -- any
6944  * reordering is allowed.  If more control is needed, you should probably
6945  * handle drag and drop manually.
6946  *
6947  * Since: 2.8
6948  **/
6949 void
6950 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
6951                                gboolean     reorderable)
6952 {
6953   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6954
6955   reorderable = reorderable != FALSE;
6956
6957   if (icon_view->priv->reorderable == reorderable)
6958     return;
6959
6960   if (reorderable)
6961     {
6962       gtk_icon_view_enable_model_drag_source (icon_view,
6963                                               GDK_BUTTON1_MASK,
6964                                               item_targets,
6965                                               G_N_ELEMENTS (item_targets),
6966                                               GDK_ACTION_MOVE);
6967       gtk_icon_view_enable_model_drag_dest (icon_view,
6968                                             item_targets,
6969                                             G_N_ELEMENTS (item_targets),
6970                                             GDK_ACTION_MOVE);
6971     }
6972   else
6973     {
6974       gtk_icon_view_unset_model_drag_source (icon_view);
6975       gtk_icon_view_unset_model_drag_dest (icon_view);
6976     }
6977
6978   icon_view->priv->reorderable = reorderable;
6979
6980   g_object_notify (G_OBJECT (icon_view), "reorderable");
6981 }
6982
6983 static gboolean
6984 gtk_icon_view_buildable_custom_tag_start (GtkBuildable  *buildable,
6985                                           GtkBuilder    *builder,
6986                                           GObject       *child,
6987                                           const gchar   *tagname,
6988                                           GMarkupParser *parser,
6989                                           gpointer      *data)
6990 {
6991   if (parent_buildable_iface->custom_tag_start (buildable, builder, child,
6992                                                 tagname, parser, data))
6993     return TRUE;
6994
6995   return _gtk_cell_layout_buildable_custom_tag_start (buildable, builder, child,
6996                                                       tagname, parser, data);
6997 }
6998
6999 static void
7000 gtk_icon_view_buildable_custom_tag_end (GtkBuildable *buildable,
7001                                         GtkBuilder   *builder,
7002                                         GObject      *child,
7003                                         const gchar  *tagname,
7004                                         gpointer     *data)
7005 {
7006   if (!_gtk_cell_layout_buildable_custom_tag_end (buildable, builder,
7007                                                   child, tagname, data))
7008     parent_buildable_iface->custom_tag_end (buildable, builder,
7009                                             child, tagname, data);
7010 }