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