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