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