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