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