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