]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
Fix row_spacing/column_spacing confusion. (#330732, Donald Straney)
[~andy/gtk] / gtk / gtkiconview.c
1 /* gtkiconview.c
2  * Copyright (C) 2002, 2004  Anders Carlsson <andersca@gnu.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include <config.h>
21 #include <string.h>
22
23 #include <atk/atk.h>
24
25 #include <gdk/gdkkeysyms.h>
26
27 #include "gtkiconview.h"
28 #include "gtkcelllayout.h"
29 #include "gtkcellrenderer.h"
30 #include "gtkcellrenderertext.h"
31 #include "gtkcellrendererpixbuf.h"
32 #include "gtkmarshalers.h"
33 #include "gtkbindings.h"
34 #include "gtkdnd.h"
35 #include "gtkmain.h"
36 #include "gtksignal.h"
37 #include "gtkintl.h"
38 #include "gtkaccessible.h"
39 #include "gtkwindow.h"
40 #include "gtkentry.h"
41 #include "gtkcombobox.h"
42 #include "gtktextbuffer.h"
43 #include "gtktreednd.h"
44 #include "gtkprivate.h"
45 #include "gtkalias.h"
46
47 #undef DEBUG_ICON_VIEW
48
49 #define SCROLL_EDGE_SIZE 15
50
51 #define GTK_ICON_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ICON_VIEW, GtkIconViewPrivate))
52
53 typedef struct _GtkIconViewItem GtkIconViewItem;
54 struct _GtkIconViewItem
55 {
56   GtkTreeIter iter;
57   gint index;
58   
59   gint row, col;
60
61   /* Bounding box */
62   gint x, y, width, height;
63
64   /* Individual cells.
65    * box[i] is the actual area occupied by cell i,
66    * before, after are used to calculate the cell 
67    * area relative to the box. 
68    * See gtk_icon_view_get_cell_area().
69    */
70   gint n_cells;
71   GdkRectangle *box;
72   gint *before;
73   gint *after;
74
75   guint selected : 1;
76   guint selected_before_rubberbanding : 1;
77
78 };
79
80 typedef struct _GtkIconViewCellInfo GtkIconViewCellInfo;
81 struct _GtkIconViewCellInfo
82 {
83   GtkCellRenderer *cell;
84
85   guint expand : 1;
86   guint pack : 1;
87   guint editing : 1;
88
89   gint position;
90
91   GSList *attributes;
92
93   GtkCellLayoutDataFunc func;
94   gpointer func_data;
95   GDestroyNotify destroy;
96 };
97
98 typedef struct _GtkIconViewChild GtkIconViewChild;
99 struct _GtkIconViewChild
100 {
101   GtkWidget *widget;
102   GtkIconViewItem *item;
103   gint cell;
104 };
105
106 struct _GtkIconViewPrivate
107 {
108   gint width, height;
109
110   GtkSelectionMode selection_mode;
111
112   GdkWindow *bin_window;
113
114   GList *children;
115
116   GtkTreeModel *model;
117   
118   GList *items;
119   
120   GtkAdjustment *hadjustment;
121   GtkAdjustment *vadjustment;
122
123   guint layout_idle_id;
124   
125   gboolean doing_rubberband;
126   gint rubberband_x1, rubberband_y1;
127   gint rubberband_x2, rubberband_y2;
128
129   guint scroll_timeout_id;
130   gint scroll_value_diff;
131   gint event_last_x, event_last_y;
132
133   GtkIconViewItem *anchor_item;
134   GtkIconViewItem *cursor_item;
135   GtkIconViewItem *edited_item;
136   GtkCellEditable *editable;
137
138   GtkIconViewItem *last_single_clicked;
139
140   GList *cell_list;
141   guint n_cells;
142
143   gint cursor_cell;
144
145   GtkOrientation orientation;
146
147   gint columns;
148   gint item_width;
149   gint spacing;
150   gint row_spacing;
151   gint column_spacing;
152   gint margin;
153
154   gint text_column;
155   gint markup_column;
156   gint pixbuf_column;
157
158   gint pixbuf_cell;
159   gint text_cell;
160
161   /* Drag-and-drop. */
162   GdkModifierType start_button_mask;
163   gint pressed_button;
164   gint press_start_x;
165   gint press_start_y;
166
167   GtkTargetList *source_targets;
168   GdkDragAction source_actions;
169
170   GtkTargetList *dest_targets;
171   GdkDragAction dest_actions;
172
173   GtkTreeRowReference *dest_item;
174   GtkIconViewDropPosition dest_pos;
175
176   /* scroll to */
177   GtkTreeRowReference *scroll_to_path;
178   gfloat scroll_to_row_align;
179   gfloat scroll_to_col_align;
180   guint scroll_to_use_align : 1;
181
182   guint source_set : 1;
183   guint dest_set : 1;
184   guint reorderable : 1;
185   guint empty_view_drop :1;
186
187   guint ctrl_pressed : 1;
188   guint shift_pressed : 1;  
189 };
190
191 /* Signals */
192 enum
193 {
194   ITEM_ACTIVATED,
195   SELECTION_CHANGED,
196   SELECT_ALL,
197   UNSELECT_ALL,
198   SELECT_CURSOR_ITEM,
199   TOGGLE_CURSOR_ITEM,
200   MOVE_CURSOR,
201   ACTIVATE_CURSOR_ITEM,
202   LAST_SIGNAL
203 };
204
205 /* Properties */
206 enum
207 {
208   PROP_0,
209   PROP_PIXBUF_COLUMN,
210   PROP_TEXT_COLUMN,
211   PROP_MARKUP_COLUMN,  
212   PROP_SELECTION_MODE,
213   PROP_ORIENTATION,
214   PROP_MODEL,
215   PROP_COLUMNS,
216   PROP_ITEM_WIDTH,
217   PROP_SPACING,
218   PROP_ROW_SPACING,
219   PROP_COLUMN_SPACING,
220   PROP_MARGIN,
221   PROP_REORDERABLE
222 };
223
224 /* GObject vfuncs */
225 static void             gtk_icon_view_class_init                (GtkIconViewClass   *klass);
226 static void             gtk_icon_view_cell_layout_init          (GtkCellLayoutIface *iface);
227 static void             gtk_icon_view_finalize                  (GObject            *object);
228 static void             gtk_icon_view_set_property              (GObject            *object,
229                                                                  guint               prop_id,
230                                                                  const GValue       *value,
231                                                                  GParamSpec         *pspec);
232 static void             gtk_icon_view_get_property              (GObject            *object,
233                                                                  guint               prop_id,
234                                                                  GValue             *value,
235                                                                  GParamSpec         *pspec);
236
237 /* GtkObject vfuncs */
238 static void             gtk_icon_view_destroy                   (GtkObject          *object);
239
240 /* GtkWidget vfuncs */
241 static void             gtk_icon_view_realize                   (GtkWidget          *widget);
242 static void             gtk_icon_view_unrealize                 (GtkWidget          *widget);
243 static void             gtk_icon_view_size_request              (GtkWidget          *widget,
244                                                                  GtkRequisition     *requisition);
245 static void             gtk_icon_view_size_allocate             (GtkWidget          *widget,
246                                                                  GtkAllocation      *allocation);
247 static gboolean         gtk_icon_view_expose                    (GtkWidget          *widget,
248                                                                  GdkEventExpose     *expose);
249 static gboolean         gtk_icon_view_motion                    (GtkWidget          *widget,
250                                                                  GdkEventMotion     *event);
251 static gboolean         gtk_icon_view_button_press              (GtkWidget          *widget,
252                                                                  GdkEventButton     *event);
253 static gboolean         gtk_icon_view_button_release            (GtkWidget          *widget,
254                                                                  GdkEventButton     *event);
255 static AtkObject       *gtk_icon_view_get_accessible            (GtkWidget          *widget);
256
257
258 /* GtkContainer vfuncs */
259 static void             gtk_icon_view_remove                    (GtkContainer       *container,
260                                                                  GtkWidget          *widget);
261 static void             gtk_icon_view_forall                    (GtkContainer       *container,
262                                                                  gboolean            include_internals,
263                                                                  GtkCallback         callback,
264                                                                  gpointer            callback_data);
265
266 /* GtkIconView vfuncs */
267 static void             gtk_icon_view_set_adjustments           (GtkIconView        *icon_view,
268                                                                  GtkAdjustment      *hadj,
269                                                                  GtkAdjustment      *vadj);
270 static void             gtk_icon_view_real_select_all           (GtkIconView        *icon_view);
271 static void             gtk_icon_view_real_unselect_all         (GtkIconView        *icon_view);
272 static void             gtk_icon_view_real_select_cursor_item   (GtkIconView        *icon_view);
273 static void             gtk_icon_view_real_toggle_cursor_item   (GtkIconView        *icon_view);
274 static gboolean         gtk_icon_view_real_activate_cursor_item (GtkIconView        *icon_view);
275
276  /* Internal functions */
277 static void                 gtk_icon_view_adjustment_changed             (GtkAdjustment          *adjustment,
278                                                                           GtkIconView            *icon_view);
279 static void                 gtk_icon_view_layout                         (GtkIconView            *icon_view);
280 static void                 gtk_icon_view_paint_item                     (GtkIconView            *icon_view,
281                                                                           cairo_t *cr,
282
283                                                                           GtkIconViewItem        *item,
284                                                                           GdkRectangle           *area,
285                                                                           GdkDrawable *drawable,
286                                                                           gint         x,
287                                                                           gint         y,
288                                                                           gboolean     draw_focus);
289 static void                 gtk_icon_view_paint_rubberband               (GtkIconView            *icon_view,
290                                                                           cairo_t *cr,
291                                                                           GdkRectangle           *area);
292 static void                 gtk_icon_view_queue_draw_path                (GtkIconView *icon_view,
293                                                                           GtkTreePath *path);
294 static void                 gtk_icon_view_queue_draw_item                (GtkIconView            *icon_view,
295                                                                           GtkIconViewItem        *item);
296 static void                 gtk_icon_view_queue_layout                   (GtkIconView            *icon_view);
297 static void                 gtk_icon_view_set_cursor_item                (GtkIconView            *icon_view,
298                                                                           GtkIconViewItem        *item,
299                                                                           gint                    cursor_cell);
300 static void                 gtk_icon_view_start_rubberbanding            (GtkIconView            *icon_view,
301                                                                           gint                    x,
302                                                                           gint                    y);
303 static void                 gtk_icon_view_stop_rubberbanding             (GtkIconView            *icon_view);
304 static void                 gtk_icon_view_update_rubberband_selection    (GtkIconView            *icon_view);
305 static gboolean             gtk_icon_view_item_hit_test                  (GtkIconView            *icon_view,
306                                                                           GtkIconViewItem        *item,
307                                                                           gint                    x,
308                                                                           gint                    y,
309                                                                           gint                    width,
310                                                                           gint                    height);
311 static gboolean             gtk_icon_view_unselect_all_internal          (GtkIconView            *icon_view);
312 static void                 gtk_icon_view_calculate_item_size            (GtkIconView            *icon_view,
313                                                                           GtkIconViewItem        *item);
314 static void                 gtk_icon_view_calculate_item_size2           (GtkIconView            *icon_view,
315                                                                           GtkIconViewItem        *item,
316                                                                           gint                   *max_height);
317 static void                 gtk_icon_view_update_rubberband              (gpointer                data);
318 static void                 gtk_icon_view_item_invalidate_size           (GtkIconViewItem        *item);
319 static void                 gtk_icon_view_invalidate_sizes               (GtkIconView            *icon_view);
320 static void                 gtk_icon_view_add_move_binding               (GtkBindingSet          *binding_set,
321                                                                           guint                   keyval,
322                                                                           guint                   modmask,
323                                                                           GtkMovementStep         step,
324                                                                           gint                    count);
325 static gboolean             gtk_icon_view_real_move_cursor               (GtkIconView            *icon_view,
326                                                                           GtkMovementStep         step,
327                                                                           gint                    count);
328 static void                 gtk_icon_view_move_cursor_up_down            (GtkIconView            *icon_view,
329                                                                           gint                    count);
330 static void                 gtk_icon_view_move_cursor_page_up_down       (GtkIconView            *icon_view,
331                                                                           gint                    count);
332 static void                 gtk_icon_view_move_cursor_left_right         (GtkIconView            *icon_view,
333                                                                           gint                    count);
334 static void                 gtk_icon_view_move_cursor_start_end          (GtkIconView            *icon_view,
335                                                                           gint                    count);
336 static void                 gtk_icon_view_scroll_to_item                 (GtkIconView            *icon_view,
337                                                                           GtkIconViewItem        *item);
338 static void                 gtk_icon_view_select_item                    (GtkIconView            *icon_view,
339                                                                           GtkIconViewItem        *item);
340 static void                 gtk_icon_view_unselect_item                  (GtkIconView            *icon_view,
341                                                                           GtkIconViewItem        *item);
342 static gboolean             gtk_icon_view_select_all_between             (GtkIconView            *icon_view,
343                                                                           GtkIconViewItem        *anchor,
344                                                                           GtkIconViewItem        *cursor);
345 static GtkIconViewItem *    gtk_icon_view_get_item_at_coords             (GtkIconView            *icon_view,
346                                                                           gint                    x,
347                                                                           gint                    y,
348                                                                           gboolean                only_in_cell,
349                                                                           GtkIconViewCellInfo   **cell_at_pos);
350 static void                 gtk_icon_view_get_cell_area                  (GtkIconView            *icon_view,
351                                                                           GtkIconViewItem        *item,
352                                                                           GtkIconViewCellInfo    *cell_info,
353                                                                           GdkRectangle           *cell_area);
354 static void                 gtk_icon_view_get_cell_box                   (GtkIconView            *icon_view,
355                                                                           GtkIconViewItem        *item,
356                                                                           GtkIconViewCellInfo    *info,
357                                                                           GdkRectangle           *box);
358 static GtkIconViewCellInfo *gtk_icon_view_get_cell_info                  (GtkIconView            *icon_view,
359                                                                           GtkCellRenderer        *renderer);
360 static void                 gtk_icon_view_set_cell_data                  (GtkIconView            *icon_view,
361                                                                           GtkIconViewItem        *item);
362 static void                 gtk_icon_view_cell_layout_pack_start         (GtkCellLayout          *layout,
363                                                                           GtkCellRenderer        *renderer,
364                                                                           gboolean                expand);
365 static void                 gtk_icon_view_cell_layout_pack_end           (GtkCellLayout          *layout,
366                                                                           GtkCellRenderer        *renderer,
367                                                                           gboolean                expand);
368 static void                 gtk_icon_view_cell_layout_add_attribute      (GtkCellLayout          *layout,
369                                                                           GtkCellRenderer        *renderer,
370                                                                           const gchar            *attribute,
371                                                                           gint                    column);
372 static void                 gtk_icon_view_cell_layout_clear              (GtkCellLayout          *layout);
373 static void                 gtk_icon_view_cell_layout_clear_attributes   (GtkCellLayout          *layout,
374                                                                           GtkCellRenderer        *renderer);
375 static void                 gtk_icon_view_cell_layout_set_cell_data_func (GtkCellLayout          *layout,
376                                                                           GtkCellRenderer        *cell,
377                                                                           GtkCellLayoutDataFunc   func,
378                                                                           gpointer                func_data,
379                                                                           GDestroyNotify          destroy);
380 static void                 gtk_icon_view_cell_layout_reorder            (GtkCellLayout          *layout,
381                                                                           GtkCellRenderer        *cell,
382                                                                           gint                    position);
383 static void                 gtk_icon_view_item_activate_cell             (GtkIconView            *icon_view,
384                                                                           GtkIconViewItem        *item,
385                                                                           GtkIconViewCellInfo    *cell_info,
386                                                                           GdkEvent               *event);
387 static void                 gtk_icon_view_put                            (GtkIconView            *icon_view,
388                                                                           GtkWidget              *widget,
389                                                                           GtkIconViewItem        *item,
390                                                                           gint                    cell);
391 static void                 gtk_icon_view_remove_widget                  (GtkCellEditable        *editable,
392                                                                           GtkIconView            *icon_view);
393 static void                 gtk_icon_view_start_editing                  (GtkIconView            *icon_view,
394                                                                           GtkIconViewItem        *item,
395                                                                           GtkIconViewCellInfo    *cell_info,
396                                                                           GdkEvent               *event);
397 static void                 gtk_icon_view_stop_editing                   (GtkIconView            *icon_view,
398                                                                           gboolean                cancel_editing);
399
400 /* Source side drag signals */
401 static void gtk_icon_view_drag_begin       (GtkWidget        *widget,
402                                             GdkDragContext   *context);
403 static void gtk_icon_view_drag_end         (GtkWidget        *widget,
404                                             GdkDragContext   *context);
405 static void gtk_icon_view_drag_data_get    (GtkWidget        *widget,
406                                             GdkDragContext   *context,
407                                             GtkSelectionData *selection_data,
408                                             guint             info,
409                                             guint             time);
410 static void gtk_icon_view_drag_data_delete (GtkWidget        *widget,
411                                             GdkDragContext   *context);
412
413 /* Target side drag signals */
414 static void     gtk_icon_view_drag_leave         (GtkWidget        *widget,
415                                                   GdkDragContext   *context,
416                                                   guint             time);
417 static gboolean gtk_icon_view_drag_motion        (GtkWidget        *widget,
418                                                   GdkDragContext   *context,
419                                                   gint              x,
420                                                   gint              y,
421                                                   guint             time);
422 static gboolean gtk_icon_view_drag_drop          (GtkWidget        *widget,
423                                                   GdkDragContext   *context,
424                                                   gint              x,
425                                                   gint              y,
426                                                   guint             time);
427 static void     gtk_icon_view_drag_data_received (GtkWidget        *widget,
428                                                   GdkDragContext   *context,
429                                                   gint              x,
430                                                   gint              y,
431                                                   GtkSelectionData *selection_data,
432                                                   guint             info,
433                                                   guint             time);
434 static gboolean gtk_icon_view_maybe_begin_drag   (GtkIconView             *icon_view,
435                                                   GdkEventMotion          *event);
436
437 static void     remove_scroll_timeout            (GtkIconView *icon_view);
438
439
440 static guint icon_view_signals[LAST_SIGNAL] = { 0 };
441
442 G_DEFINE_TYPE_WITH_CODE (GtkIconView, gtk_icon_view, GTK_TYPE_CONTAINER,
443                          G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
444                                                 gtk_icon_view_cell_layout_init));
445
446 static void
447 gtk_icon_view_class_init (GtkIconViewClass *klass)
448 {
449   GObjectClass *gobject_class;
450   GtkObjectClass *object_class;
451   GtkWidgetClass *widget_class;
452   GtkContainerClass *container_class;
453   GtkBindingSet *binding_set;
454   
455   binding_set = gtk_binding_set_by_class (klass);
456
457   g_type_class_add_private (klass, sizeof (GtkIconViewPrivate));
458
459   gobject_class = (GObjectClass *) klass;
460   object_class = (GtkObjectClass *) klass;
461   widget_class = (GtkWidgetClass *) klass;
462   container_class = (GtkContainerClass *) klass;
463
464   gobject_class->finalize = gtk_icon_view_finalize;
465   gobject_class->set_property = gtk_icon_view_set_property;
466   gobject_class->get_property = gtk_icon_view_get_property;
467
468   object_class->destroy = gtk_icon_view_destroy;
469   
470   widget_class->realize = gtk_icon_view_realize;
471   widget_class->unrealize = gtk_icon_view_unrealize;
472   widget_class->size_request = gtk_icon_view_size_request;
473   widget_class->size_allocate = gtk_icon_view_size_allocate;
474   widget_class->expose_event = gtk_icon_view_expose;
475   widget_class->motion_notify_event = gtk_icon_view_motion;
476   widget_class->button_press_event = gtk_icon_view_button_press;
477   widget_class->button_release_event = gtk_icon_view_button_release;
478   widget_class->drag_begin = gtk_icon_view_drag_begin;
479   widget_class->drag_end = gtk_icon_view_drag_end;
480   widget_class->drag_data_get = gtk_icon_view_drag_data_get;
481   widget_class->drag_data_delete = gtk_icon_view_drag_data_delete;
482   widget_class->drag_leave = gtk_icon_view_drag_leave;
483   widget_class->drag_motion = gtk_icon_view_drag_motion;
484   widget_class->drag_drop = gtk_icon_view_drag_drop;
485   widget_class->drag_data_received = gtk_icon_view_drag_data_received;
486   widget_class->get_accessible = gtk_icon_view_get_accessible;
487
488   container_class->remove = gtk_icon_view_remove;
489   container_class->forall = gtk_icon_view_forall;
490
491   klass->set_scroll_adjustments = gtk_icon_view_set_adjustments;
492   klass->select_all = gtk_icon_view_real_select_all;
493   klass->unselect_all = gtk_icon_view_real_unselect_all;
494   klass->select_cursor_item = gtk_icon_view_real_select_cursor_item;
495   klass->toggle_cursor_item = gtk_icon_view_real_toggle_cursor_item;
496   klass->activate_cursor_item = gtk_icon_view_real_activate_cursor_item;  
497   klass->move_cursor = gtk_icon_view_real_move_cursor;
498   
499   /* Properties */
500   /**
501    * GtkIconView:selection-mode:
502    * 
503    * The ::selection-mode property specifies the selection mode of
504    * icon view. If the mode is #GTK_SELECTION_MULTIPLE, rubberband selection
505    * is enabled, for the other modes, only keyboard selection is possible.
506    *
507    * Since: 2.6
508    */
509   g_object_class_install_property (gobject_class,
510                                    PROP_SELECTION_MODE,
511                                    g_param_spec_enum ("selection-mode",
512                                                       P_("Selection mode"),
513                                                       P_("The selection mode"),
514                                                       GTK_TYPE_SELECTION_MODE,
515                                                       GTK_SELECTION_SINGLE,
516                                                       GTK_PARAM_READWRITE));
517
518   /**
519    * GtkIconView:pixbuf-column:
520    *
521    * The ::pixbuf-column property contains the number of the model column
522    * containing the pixbufs which are displayed. The pixbuf column must be 
523    * of type #GDK_TYPE_PIXBUF. Setting this property to -1 turns off the
524    * display of pixbufs.
525    *
526    * Since: 2.6
527    */
528   g_object_class_install_property (gobject_class,
529                                    PROP_PIXBUF_COLUMN,
530                                    g_param_spec_int ("pixbuf-column",
531                                                      P_("Pixbuf column"),
532                                                      P_("Model column used to retrieve the icon pixbuf from"),
533                                                      -1, G_MAXINT, -1,
534                                                      GTK_PARAM_READWRITE));
535
536   /**
537    * GtkIconView:text-column:
538    *
539    * The ::text-column property contains the number of the model column
540    * containing the texts which are displayed. The text column must be 
541    * of type #G_TYPE_STRING. If this property and the :markup-column 
542    * property are both set to -1, no texts are displayed.   
543    *
544    * Since: 2.6
545    */
546   g_object_class_install_property (gobject_class,
547                                    PROP_TEXT_COLUMN,
548                                    g_param_spec_int ("text-column",
549                                                      P_("Text column"),
550                                                      P_("Model column used to retrieve the text from"),
551                                                      -1, G_MAXINT, -1,
552                                                      GTK_PARAM_READWRITE));
553
554   
555   /**
556    * GtkIconView:markup-column:
557    *
558    * The ::markup-column property contains the number of the model column
559    * containing markup information to be displayed. The markup column must be 
560    * of type #G_TYPE_STRING. If this property and the :text-column property 
561    * are both set to column numbers, it overrides the text column.
562    * If both are set to -1, no texts are displayed.   
563    *
564    * Since: 2.6
565    */
566   g_object_class_install_property (gobject_class,
567                                    PROP_MARKUP_COLUMN,
568                                    g_param_spec_int ("markup-column",
569                                                      P_("Markup column"),
570                                                      P_("Model column used to retrieve the text if using Pango markup"),
571                                                      -1, G_MAXINT, -1,
572                                                      GTK_PARAM_READWRITE));
573   
574   g_object_class_install_property (gobject_class,
575                                    PROP_MODEL,
576                                    g_param_spec_object ("model",
577                                                         P_("Icon View Model"),
578                                                         P_("The model for the icon view"),
579                                                         GTK_TYPE_TREE_MODEL,
580                                                         GTK_PARAM_READWRITE));
581   
582   /**
583    * GtkIconView:columns:
584    *
585    * The columns property contains the number of the columns in which the
586    * items should be displayed. If it is -1, the number of columns will
587    * be chosen automatically to fill the available area.
588    *
589    * Since: 2.6
590    */
591   g_object_class_install_property (gobject_class,
592                                    PROP_COLUMNS,
593                                    g_param_spec_int ("columns",
594                                                      P_("Number of columns"),
595                                                      P_("Number of columns to display"),
596                                                      -1, G_MAXINT, -1,
597                                                      GTK_PARAM_READWRITE));
598   
599
600   /**
601    * GtkIconView::item-width:
602    *
603    * The item-width property specifies the width to use for each item. 
604    * If it is set to -1, the icon view will automatically determine a 
605    * suitable item size.
606    *
607    * Since: 2.6
608    */
609   g_object_class_install_property (gobject_class,
610                                    PROP_ITEM_WIDTH,
611                                    g_param_spec_int ("item-width",
612                                                      P_("Width for each item"),
613                                                      P_("The width used for each item"),
614                                                      -1, G_MAXINT, -1,
615                                                      GTK_PARAM_READWRITE));  
616
617   /**
618    * GtkIconView::spacing:
619    *
620    * The spacing property specifies the space which is inserted between
621    * the cells (i.e. the icon and the text) of an item.
622    *
623    * Since: 2.6
624    */
625   g_object_class_install_property (gobject_class,
626                                    PROP_SPACING,
627                                    g_param_spec_int ("spacing",
628                                                      P_("Spacing"),
629                                                      P_("Space which is inserted between cells of an item"),
630                                                      0, G_MAXINT, 0,
631                                                      GTK_PARAM_READWRITE));
632
633   /**
634    * GtkIconView::row-spacing:
635    *
636    * The row-spacing property specifies the space which is inserted between
637    * the rows of the icon view.
638    *
639    * Since: 2.6
640    */
641   g_object_class_install_property (gobject_class,
642                                    PROP_ROW_SPACING,
643                                    g_param_spec_int ("row-spacing",
644                                                      P_("Row Spacing"),
645                                                      P_("Space which is inserted between grid rows"),
646                                                      0, G_MAXINT, 6,
647                                                      GTK_PARAM_READWRITE));
648
649   /**
650    * GtkIconView::column-spacing:
651    *
652    * The column-spacing property specifies the space which is inserted between
653    * the columns of the icon view.
654    *
655    * Since: 2.6
656    */
657   g_object_class_install_property (gobject_class,
658                                    PROP_COLUMN_SPACING,
659                                    g_param_spec_int ("column-spacing",
660                                                      P_("Column Spacing"),
661                                                      P_("Space which is inserted between grid columns"),
662                                                      0, G_MAXINT, 6,
663                                                      GTK_PARAM_READWRITE));
664
665   /**
666    * GtkIconView::margin:
667    *
668    * The margin property specifies the space which is inserted 
669    * at the edges of the icon view.
670    *
671    * Since: 2.6
672    */
673   g_object_class_install_property (gobject_class,
674                                    PROP_MARGIN,
675                                    g_param_spec_int ("margin",
676                                                      P_("Margin"),
677                                                      P_("Space which is inserted at the edges of the icon view"),
678                                                      0, G_MAXINT, 6,
679                                                      GTK_PARAM_READWRITE));
680
681
682   /**
683    * GtkIconView::orientation:
684    *
685    * The orientation property specifies how the cells (i.e. the icon and 
686    * the text) of the item are positioned relative to each other.
687    *
688    * Since: 2.6
689    */
690   g_object_class_install_property (gobject_class,
691                                    PROP_ORIENTATION,
692                                    g_param_spec_enum ("orientation",
693                                                       P_("Orientation"),
694                                                       P_("How the text and icon of each item are positioned relative to each other"),
695                                                       GTK_TYPE_ORIENTATION,
696                                                       GTK_ORIENTATION_VERTICAL,
697                                                       GTK_PARAM_READWRITE));
698
699   /**
700    * GtkIconView:reorderable:
701    *
702    * The reorderable property specifies if the items can be reordered
703    * by DND.
704    *
705    * Since: 2.8
706    */
707   g_object_class_install_property (gobject_class,
708                                    PROP_REORDERABLE,
709                                    g_param_spec_boolean ("reorderable",
710                                                          P_("Reorderable"),
711                                                          P_("View is reorderable"),
712                                                          FALSE,
713                                                          G_PARAM_READWRITE));
714
715   /* Style properties */
716   gtk_widget_class_install_style_property (widget_class,
717                                            g_param_spec_boxed ("selection-box-color",
718                                                                P_("Selection Box Color"),
719                                                                P_("Color of the selection box"),
720                                                                GDK_TYPE_COLOR,
721                                                                GTK_PARAM_READABLE));
722
723   gtk_widget_class_install_style_property (widget_class,
724                                            g_param_spec_uchar ("selection-box-alpha",
725                                                                P_("Selection Box Alpha"),
726                                                                P_("Opacity of the selection box"),
727                                                                0, 0xff,
728                                                                0x40,
729                                                                GTK_PARAM_READABLE));
730
731   /* Signals */
732   widget_class->set_scroll_adjustments_signal =
733     g_signal_new (I_("set_scroll_adjustments"),
734                   G_TYPE_FROM_CLASS (gobject_class),
735                   G_SIGNAL_RUN_LAST,
736                   G_STRUCT_OFFSET (GtkIconViewClass, set_scroll_adjustments),
737                   NULL, NULL, 
738                   _gtk_marshal_VOID__OBJECT_OBJECT,
739                   G_TYPE_NONE, 2,
740                   GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
741
742   icon_view_signals[ITEM_ACTIVATED] =
743     g_signal_new (I_("item_activated"),
744                   G_TYPE_FROM_CLASS (gobject_class),
745                   G_SIGNAL_RUN_LAST,
746                   G_STRUCT_OFFSET (GtkIconViewClass, item_activated),
747                   NULL, NULL,
748                   g_cclosure_marshal_VOID__BOXED,
749                   G_TYPE_NONE, 1,
750                   GTK_TYPE_TREE_PATH);
751
752   icon_view_signals[SELECTION_CHANGED] =
753     g_signal_new (I_("selection_changed"),
754                   G_TYPE_FROM_CLASS (gobject_class),
755                   G_SIGNAL_RUN_FIRST,
756                   G_STRUCT_OFFSET (GtkIconViewClass, selection_changed),
757                   NULL, NULL,
758                   g_cclosure_marshal_VOID__VOID,
759                   G_TYPE_NONE, 0);
760   
761   icon_view_signals[SELECT_ALL] =
762     g_signal_new (I_("select_all"),
763                   G_TYPE_FROM_CLASS (gobject_class),
764                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
765                   G_STRUCT_OFFSET (GtkIconViewClass, select_all),
766                   NULL, NULL,
767                   g_cclosure_marshal_VOID__VOID,
768                   G_TYPE_NONE, 0);
769   
770   icon_view_signals[UNSELECT_ALL] =
771     g_signal_new (I_("unselect_all"),
772                   G_TYPE_FROM_CLASS (gobject_class),
773                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
774                   G_STRUCT_OFFSET (GtkIconViewClass, unselect_all),
775                   NULL, NULL,
776                   g_cclosure_marshal_VOID__VOID,
777                   G_TYPE_NONE, 0);
778
779   icon_view_signals[SELECT_CURSOR_ITEM] =
780     g_signal_new (I_("select_cursor_item"),
781                   G_TYPE_FROM_CLASS (gobject_class),
782                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
783                   G_STRUCT_OFFSET (GtkIconViewClass, select_cursor_item),
784                   NULL, NULL,
785                   g_cclosure_marshal_VOID__VOID,
786                   G_TYPE_NONE, 0);
787
788   icon_view_signals[TOGGLE_CURSOR_ITEM] =
789     g_signal_new (I_("toggle_cursor_item"),
790                   G_TYPE_FROM_CLASS (gobject_class),
791                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
792                   G_STRUCT_OFFSET (GtkIconViewClass, toggle_cursor_item),
793                   NULL, NULL,
794                   g_cclosure_marshal_VOID__VOID,
795                   G_TYPE_NONE, 0);
796
797   icon_view_signals[ACTIVATE_CURSOR_ITEM] =
798     g_signal_new (I_("activate_cursor_item"),
799                   G_TYPE_FROM_CLASS (gobject_class),
800                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
801                   G_STRUCT_OFFSET (GtkIconViewClass, activate_cursor_item),
802                   NULL, NULL,
803                   _gtk_marshal_BOOLEAN__VOID,
804                   G_TYPE_BOOLEAN, 0);
805   
806   icon_view_signals[MOVE_CURSOR] =
807     g_signal_new (I_("move_cursor"),
808                   G_TYPE_FROM_CLASS (gobject_class),
809                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
810                   G_STRUCT_OFFSET (GtkIconViewClass, move_cursor),
811                   NULL, NULL,
812                   _gtk_marshal_BOOLEAN__ENUM_INT,
813                   G_TYPE_BOOLEAN, 2,
814                   GTK_TYPE_MOVEMENT_STEP,
815                   G_TYPE_INT);
816
817   /* Key bindings */
818   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK, 
819                                 "select_all", 0);
820   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK, 
821                                 "unselect_all", 0);
822   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK, 
823                                 "toggle_cursor_item", 0);
824
825   gtk_binding_entry_add_signal (binding_set, GDK_space, 0, 
826                                 "activate_cursor_item", 0);
827   gtk_binding_entry_add_signal (binding_set, GDK_Return, 0, 
828                                 "activate_cursor_item", 0);
829   gtk_binding_entry_add_signal (binding_set, GDK_ISO_Enter, 0, 
830                                 "activate_cursor_item", 0);
831   gtk_binding_entry_add_signal (binding_set, GDK_KP_Enter, 0, 
832                                 "activate_cursor_item", 0);
833
834   gtk_icon_view_add_move_binding (binding_set, GDK_Up, 0,
835                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
836   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Up, 0,
837                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
838
839   gtk_icon_view_add_move_binding (binding_set, GDK_Down, 0,
840                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
841   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Down, 0,
842                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
843
844   gtk_icon_view_add_move_binding (binding_set, GDK_p, GDK_CONTROL_MASK,
845                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
846
847   gtk_icon_view_add_move_binding (binding_set, GDK_n, GDK_CONTROL_MASK,
848                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
849
850   gtk_icon_view_add_move_binding (binding_set, GDK_Home, 0,
851                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
852   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Home, 0,
853                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
854
855   gtk_icon_view_add_move_binding (binding_set, GDK_End, 0,
856                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
857   gtk_icon_view_add_move_binding (binding_set, GDK_KP_End, 0,
858                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
859
860   gtk_icon_view_add_move_binding (binding_set, GDK_Page_Up, 0,
861                                   GTK_MOVEMENT_PAGES, -1);
862   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Page_Up, 0,
863                                   GTK_MOVEMENT_PAGES, -1);
864
865   gtk_icon_view_add_move_binding (binding_set, GDK_Page_Down, 0,
866                                   GTK_MOVEMENT_PAGES, 1);
867   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Page_Down, 0,
868                                   GTK_MOVEMENT_PAGES, 1);
869
870   gtk_icon_view_add_move_binding (binding_set, GDK_Right, 0, 
871                                   GTK_MOVEMENT_VISUAL_POSITIONS, 1);
872   gtk_icon_view_add_move_binding (binding_set, GDK_Left, 0, 
873                                   GTK_MOVEMENT_VISUAL_POSITIONS, -1);
874
875   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Right, 0, 
876                                   GTK_MOVEMENT_VISUAL_POSITIONS, 1);
877   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Left, 0, 
878                                   GTK_MOVEMENT_VISUAL_POSITIONS, -1);
879 }
880
881 static void
882 gtk_icon_view_cell_layout_init (GtkCellLayoutIface *iface)
883 {
884   iface->pack_start = gtk_icon_view_cell_layout_pack_start;
885   iface->pack_end = gtk_icon_view_cell_layout_pack_end;
886   iface->clear = gtk_icon_view_cell_layout_clear;
887   iface->add_attribute = gtk_icon_view_cell_layout_add_attribute;
888   iface->set_cell_data_func = gtk_icon_view_cell_layout_set_cell_data_func;
889   iface->clear_attributes = gtk_icon_view_cell_layout_clear_attributes;
890   iface->reorder = gtk_icon_view_cell_layout_reorder;
891 }
892
893 static void
894 gtk_icon_view_init (GtkIconView *icon_view)
895 {
896   icon_view->priv = GTK_ICON_VIEW_GET_PRIVATE (icon_view);
897   
898   icon_view->priv->width = 0;
899   icon_view->priv->height = 0;
900   icon_view->priv->selection_mode = GTK_SELECTION_SINGLE;
901   icon_view->priv->pressed_button = -1;
902   icon_view->priv->press_start_x = -1;
903   icon_view->priv->press_start_y = -1;
904   icon_view->priv->text_column = -1;
905   icon_view->priv->markup_column = -1;  
906   icon_view->priv->pixbuf_column = -1;
907   icon_view->priv->text_cell = -1;
908   icon_view->priv->pixbuf_cell = -1;  
909
910   GTK_WIDGET_SET_FLAGS (icon_view, GTK_CAN_FOCUS);
911   
912   gtk_icon_view_set_adjustments (icon_view, NULL, NULL);
913
914   icon_view->priv->cell_list = NULL;
915   icon_view->priv->n_cells = 0;
916   icon_view->priv->cursor_cell = -1;
917
918   icon_view->priv->orientation = GTK_ORIENTATION_VERTICAL;
919
920   icon_view->priv->columns = -1;
921   icon_view->priv->item_width = -1;
922   icon_view->priv->spacing = 0;
923   icon_view->priv->row_spacing = 6;
924   icon_view->priv->column_spacing = 6;
925   icon_view->priv->margin = 6;
926 }
927
928 static void
929 gtk_icon_view_destroy (GtkObject *object)
930 {
931   GtkIconView *icon_view;
932
933   icon_view = GTK_ICON_VIEW (object);
934   
935   gtk_icon_view_stop_editing (icon_view, TRUE);
936
937   gtk_icon_view_set_model (icon_view, NULL);
938   
939   if (icon_view->priv->layout_idle_id != 0)
940     {
941       g_source_remove (icon_view->priv->layout_idle_id);
942       icon_view->priv->layout_idle_id = 0;
943     }
944
945   if (icon_view->priv->scroll_to_path != NULL)
946     {
947       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
948       icon_view->priv->scroll_to_path = NULL;
949     }
950
951   remove_scroll_timeout (icon_view);
952   
953   (* GTK_OBJECT_CLASS (gtk_icon_view_parent_class)->destroy) (object);
954 }
955
956 /* GObject methods */
957 static void
958 gtk_icon_view_finalize (GObject *object)
959 {
960   gtk_icon_view_cell_layout_clear (GTK_CELL_LAYOUT (object));
961
962   (* G_OBJECT_CLASS (gtk_icon_view_parent_class)->finalize) (object);
963 }
964
965
966 static void
967 gtk_icon_view_set_property (GObject      *object,
968                             guint         prop_id,
969                             const GValue *value,
970                             GParamSpec   *pspec)
971 {
972   GtkIconView *icon_view;
973
974   icon_view = GTK_ICON_VIEW (object);
975
976   switch (prop_id)
977     {
978     case PROP_SELECTION_MODE:
979       gtk_icon_view_set_selection_mode (icon_view, g_value_get_enum (value));
980       break;
981     case PROP_PIXBUF_COLUMN:
982       gtk_icon_view_set_pixbuf_column (icon_view, g_value_get_int (value));
983       break;
984     case PROP_TEXT_COLUMN:
985       gtk_icon_view_set_text_column (icon_view, g_value_get_int (value));
986       break;
987     case PROP_MARKUP_COLUMN:
988       gtk_icon_view_set_markup_column (icon_view, g_value_get_int (value));
989       break;
990     case PROP_MODEL:
991       gtk_icon_view_set_model (icon_view, g_value_get_object (value));
992       break;
993     case PROP_ORIENTATION:
994       gtk_icon_view_set_orientation (icon_view, g_value_get_enum (value));
995       break;
996     case PROP_COLUMNS:
997       gtk_icon_view_set_columns (icon_view, g_value_get_int (value));
998       break;
999     case PROP_ITEM_WIDTH:
1000       gtk_icon_view_set_item_width (icon_view, g_value_get_int (value));
1001       break;
1002     case PROP_SPACING:
1003       gtk_icon_view_set_spacing (icon_view, g_value_get_int (value));
1004       break;
1005     case PROP_ROW_SPACING:
1006       gtk_icon_view_set_row_spacing (icon_view, g_value_get_int (value));
1007       break;
1008     case PROP_COLUMN_SPACING:
1009       gtk_icon_view_set_column_spacing (icon_view, g_value_get_int (value));
1010       break;
1011     case PROP_MARGIN:
1012       gtk_icon_view_set_margin (icon_view, g_value_get_int (value));
1013       break;
1014     case PROP_REORDERABLE:
1015       gtk_icon_view_set_reorderable (icon_view, g_value_get_boolean (value));
1016       break;
1017       
1018     default:
1019       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1020       break;
1021     }
1022 }
1023
1024 static void
1025 gtk_icon_view_get_property (GObject      *object,
1026                             guint         prop_id,
1027                             GValue       *value,
1028                             GParamSpec   *pspec)
1029 {
1030   GtkIconView *icon_view;
1031
1032   icon_view = GTK_ICON_VIEW (object);
1033
1034   switch (prop_id)
1035     {
1036     case PROP_SELECTION_MODE:
1037       g_value_set_enum (value, icon_view->priv->selection_mode);
1038       break;
1039     case PROP_PIXBUF_COLUMN:
1040       g_value_set_int (value, icon_view->priv->pixbuf_column);
1041       break;
1042     case PROP_TEXT_COLUMN:
1043       g_value_set_int (value, icon_view->priv->text_column);
1044       break;
1045     case PROP_MARKUP_COLUMN:
1046       g_value_set_int (value, icon_view->priv->markup_column);
1047       break;
1048     case PROP_MODEL:
1049       g_value_set_object (value, icon_view->priv->model);
1050       break;
1051     case PROP_ORIENTATION:
1052       g_value_set_enum (value, icon_view->priv->orientation);
1053       break;
1054     case PROP_COLUMNS:
1055       g_value_set_int (value, icon_view->priv->columns);
1056       break;
1057     case PROP_ITEM_WIDTH:
1058       g_value_set_int (value, icon_view->priv->item_width);
1059       break;
1060     case PROP_SPACING:
1061       g_value_set_int (value, icon_view->priv->spacing);
1062       break;
1063     case PROP_ROW_SPACING:
1064       g_value_set_int (value, icon_view->priv->row_spacing);
1065       break;
1066     case PROP_COLUMN_SPACING:
1067       g_value_set_int (value, icon_view->priv->column_spacing);
1068       break;
1069     case PROP_MARGIN:
1070       g_value_set_int (value, icon_view->priv->margin);
1071       break;
1072     case PROP_REORDERABLE:
1073       g_value_set_boolean (value, icon_view->priv->reorderable);
1074       break;
1075
1076     default:
1077       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1078       break;
1079     }
1080 }
1081
1082 /* GtkWidget signals */
1083 static void
1084 gtk_icon_view_realize (GtkWidget *widget)
1085 {
1086   GtkIconView *icon_view;
1087   GdkWindowAttr attributes;
1088   gint attributes_mask;
1089   
1090   icon_view = GTK_ICON_VIEW (widget);
1091
1092   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
1093
1094   /* Make the main, clipping window */
1095   attributes.window_type = GDK_WINDOW_CHILD;
1096   attributes.x = widget->allocation.x;
1097   attributes.y = widget->allocation.y;
1098   attributes.width = widget->allocation.width;
1099   attributes.height = widget->allocation.height;
1100   attributes.wclass = GDK_INPUT_OUTPUT;
1101   attributes.visual = gtk_widget_get_visual (widget);
1102   attributes.colormap = gtk_widget_get_colormap (widget);
1103   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
1104
1105   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
1106
1107   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
1108                                    &attributes, attributes_mask);
1109   gdk_window_set_user_data (widget->window, widget);
1110
1111   /* Make the window for the icon view */
1112   attributes.x = 0;
1113   attributes.y = 0;
1114   attributes.width = MAX (icon_view->priv->width, widget->allocation.width);
1115   attributes.height = MAX (icon_view->priv->height, widget->allocation.height);
1116   attributes.event_mask = (GDK_EXPOSURE_MASK |
1117                            GDK_SCROLL_MASK |
1118                            GDK_POINTER_MOTION_MASK |
1119                            GDK_BUTTON_PRESS_MASK |
1120                            GDK_BUTTON_RELEASE_MASK |
1121                            GDK_KEY_PRESS_MASK |
1122                            GDK_KEY_RELEASE_MASK) |
1123     gtk_widget_get_events (widget);
1124   
1125   icon_view->priv->bin_window = gdk_window_new (widget->window,
1126                                                 &attributes, attributes_mask);
1127   gdk_window_set_user_data (icon_view->priv->bin_window, widget);
1128
1129   widget->style = gtk_style_attach (widget->style, widget->window);
1130   gdk_window_set_background (icon_view->priv->bin_window, &widget->style->base[widget->state]);
1131   gdk_window_set_background (widget->window, &widget->style->base[widget->state]);
1132
1133   gdk_window_show (icon_view->priv->bin_window);
1134 }
1135
1136 static void
1137 gtk_icon_view_unrealize (GtkWidget *widget)
1138 {
1139   GtkIconView *icon_view;
1140
1141   icon_view = GTK_ICON_VIEW (widget);
1142
1143   gdk_window_set_user_data (icon_view->priv->bin_window, NULL);
1144   gdk_window_destroy (icon_view->priv->bin_window);
1145   icon_view->priv->bin_window = NULL;
1146
1147   /* GtkWidget::unrealize destroys children and widget->window */
1148   if (GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->unrealize)
1149     (* GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->unrealize) (widget);
1150 }
1151
1152 static void
1153 gtk_icon_view_size_request (GtkWidget      *widget,
1154                             GtkRequisition *requisition)
1155 {
1156   GtkIconView *icon_view;
1157   GList *tmp_list;
1158
1159   icon_view = GTK_ICON_VIEW (widget);
1160
1161   requisition->width = icon_view->priv->width;
1162   requisition->height = icon_view->priv->height;
1163
1164   tmp_list = icon_view->priv->children;
1165
1166   while (tmp_list)
1167     {
1168       GtkIconViewChild *child = tmp_list->data;
1169       GtkRequisition child_requisition;
1170
1171       tmp_list = tmp_list->next;
1172
1173       if (GTK_WIDGET_VISIBLE (child->widget))
1174         gtk_widget_size_request (child->widget, &child_requisition);
1175     }  
1176 }
1177
1178 static void
1179 gtk_icon_view_allocate_children (GtkIconView *icon_view)
1180 {
1181   GList *list;
1182
1183   for (list = icon_view->priv->children; list; list = list->next)
1184     {
1185       GtkIconViewChild *child = list->data;
1186       GtkAllocation allocation;
1187
1188       /* totally ignore our child's requisition */
1189       if (child->cell < 0)
1190         {
1191           allocation.x = child->item->x;
1192           allocation.y = child->item->y;
1193           allocation.width = child->item->width;
1194           allocation.height = child->item->height;
1195         }
1196       else
1197         {
1198           GdkRectangle *box = &child->item->box[child->cell];
1199
1200           allocation.x = box->x;
1201           allocation.y = box->y;
1202           allocation.width = box->width;
1203           allocation.height = box->height;
1204         }
1205
1206       gtk_widget_size_allocate (child->widget, &allocation);
1207     }
1208 }
1209
1210 static void
1211 gtk_icon_view_size_allocate (GtkWidget      *widget,
1212                              GtkAllocation  *allocation)
1213 {
1214   GtkIconView *icon_view = GTK_ICON_VIEW (widget);
1215
1216   GtkAdjustment *hadjustment, *vadjustment;
1217
1218   widget->allocation = *allocation;
1219   
1220   if (GTK_WIDGET_REALIZED (widget))
1221     {
1222       gdk_window_move_resize (widget->window,
1223                               allocation->x, allocation->y,
1224                               allocation->width, allocation->height);
1225       gdk_window_resize (icon_view->priv->bin_window,
1226                          MAX (icon_view->priv->width, allocation->width),
1227                          MAX (icon_view->priv->height, allocation->height));
1228     }
1229
1230   gtk_icon_view_layout (icon_view);
1231   
1232   gtk_icon_view_allocate_children (icon_view);
1233
1234   hadjustment = icon_view->priv->hadjustment;
1235   vadjustment = icon_view->priv->vadjustment;
1236
1237   hadjustment->page_size = allocation->width;
1238   hadjustment->page_increment = allocation->width * 0.9;
1239   hadjustment->step_increment = allocation->width * 0.1;
1240   hadjustment->lower = 0;
1241   hadjustment->upper = MAX (allocation->width, icon_view->priv->width);
1242
1243   if (hadjustment->value > hadjustment->upper - hadjustment->page_size)
1244     gtk_adjustment_set_value (hadjustment, MAX (0, hadjustment->upper - hadjustment->page_size));
1245
1246   vadjustment->page_size = allocation->height;
1247   vadjustment->page_increment = allocation->height * 0.9;
1248   vadjustment->step_increment = allocation->height * 0.1;
1249   vadjustment->lower = 0;
1250   vadjustment->upper = MAX (allocation->height, icon_view->priv->height);
1251
1252   if (vadjustment->value > vadjustment->upper - vadjustment->page_size)
1253     gtk_adjustment_set_value (vadjustment, MAX (0, vadjustment->upper - vadjustment->page_size));
1254
1255   if (GTK_WIDGET_REALIZED (widget) &&
1256       icon_view->priv->scroll_to_path)
1257     {
1258       GtkTreePath *path;
1259       path = gtk_tree_row_reference_get_path (icon_view->priv->scroll_to_path);
1260       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
1261       icon_view->priv->scroll_to_path = NULL;
1262
1263       gtk_icon_view_scroll_to_path (icon_view, path,
1264                                     icon_view->priv->scroll_to_use_align,
1265                                     icon_view->priv->scroll_to_row_align,
1266                                     icon_view->priv->scroll_to_col_align);
1267       gtk_tree_path_free (path);
1268     }
1269   else
1270     {
1271       gtk_adjustment_changed (hadjustment);
1272       gtk_adjustment_changed (vadjustment);
1273     }
1274 }
1275
1276 static gboolean
1277 gtk_icon_view_expose (GtkWidget *widget,
1278                       GdkEventExpose *expose)
1279 {
1280   GtkIconView *icon_view;
1281   GList *icons;
1282   cairo_t *cr;
1283   GtkTreePath *path;
1284   gint dest_index;
1285   GtkIconViewDropPosition dest_pos;
1286   GtkIconViewItem *dest_item = NULL;
1287
1288   icon_view = GTK_ICON_VIEW (widget);
1289
1290   if (expose->window != icon_view->priv->bin_window)
1291     return FALSE;
1292
1293   cr = gdk_cairo_create (icon_view->priv->bin_window);
1294   cairo_set_line_width (cr, 1.);
1295
1296   gtk_icon_view_get_drag_dest_item (icon_view, &path, &dest_pos);
1297
1298   if (path)
1299     {
1300       dest_index = gtk_tree_path_get_indices (path)[0];
1301       gtk_tree_path_free (path);
1302     }
1303   else
1304     dest_index = -1;
1305
1306   for (icons = icon_view->priv->items; icons; icons = icons->next) 
1307     {
1308       GtkIconViewItem *item = icons->data;
1309       GdkRectangle area;
1310       
1311       area.x = item->x;
1312       area.y = item->y;
1313       area.width = item->width;
1314       area.height = item->height;
1315         
1316       if (gdk_region_rect_in (expose->region, &area) == GDK_OVERLAP_RECTANGLE_OUT)
1317         continue;
1318       
1319       gtk_icon_view_paint_item (icon_view, cr, item, &expose->area, 
1320                                 icon_view->priv->bin_window,
1321                                 item->x, item->y, TRUE); 
1322  
1323       if (dest_index == item->index)
1324         dest_item = item;
1325     }
1326
1327   if (dest_item)
1328     {
1329       switch (dest_pos)
1330         {
1331         case GTK_ICON_VIEW_DROP_INTO:
1332           gtk_paint_focus (widget->style,
1333                            icon_view->priv->bin_window,
1334                            GTK_WIDGET_STATE (widget),
1335                            NULL,
1336                            widget,
1337                            "iconview-drop-indicator",
1338                            dest_item->x, dest_item->y,
1339                            dest_item->width, dest_item->height);
1340           break;
1341         case GTK_ICON_VIEW_DROP_ABOVE:
1342           gtk_paint_focus (widget->style,
1343                            icon_view->priv->bin_window,
1344                            GTK_WIDGET_STATE (widget),
1345                            NULL,
1346                            widget,
1347                            "iconview-drop-indicator",
1348                            dest_item->x, dest_item->y - 1,
1349                            dest_item->width, 2);
1350           break;
1351         case GTK_ICON_VIEW_DROP_LEFT:
1352           gtk_paint_focus (widget->style,
1353                            icon_view->priv->bin_window,
1354                            GTK_WIDGET_STATE (widget),
1355                            NULL,
1356                            widget,
1357                            "iconview-drop-indicator",
1358                            dest_item->x - 1, dest_item->y,
1359                            2, dest_item->height);
1360           break;
1361         case GTK_ICON_VIEW_DROP_BELOW:
1362           gtk_paint_focus (widget->style,
1363                            icon_view->priv->bin_window,
1364                            GTK_WIDGET_STATE (widget),
1365                            NULL,
1366                            widget,
1367                            "iconview-drop-indicator",
1368                            dest_item->x, dest_item->y + dest_item->height - 1,
1369                            dest_item->width, 2);
1370           break;
1371         case GTK_ICON_VIEW_DROP_RIGHT:
1372           gtk_paint_focus (widget->style,
1373                            icon_view->priv->bin_window,
1374                            GTK_WIDGET_STATE (widget),
1375                            NULL,
1376                            widget,
1377                            "iconview-drop-indicator",
1378                            dest_item->x + dest_item->width - 1, dest_item->y,
1379                            2, dest_item->height);
1380         case GTK_ICON_VIEW_NO_DROP: ;
1381           break;
1382         }
1383     }
1384   
1385   if (icon_view->priv->doing_rubberband)
1386     {
1387       GdkRectangle *rectangles;
1388       gint n_rectangles;
1389       
1390       gdk_region_get_rectangles (expose->region,
1391                                  &rectangles,
1392                                  &n_rectangles);
1393       
1394       while (n_rectangles--)
1395         gtk_icon_view_paint_rubberband (icon_view, cr, &rectangles[n_rectangles]);
1396
1397       g_free (rectangles);
1398     }
1399
1400   cairo_destroy (cr);
1401
1402   (* GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->expose_event) (widget, expose);
1403
1404   return TRUE;
1405 }
1406
1407 static gboolean
1408 rubberband_scroll_timeout (gpointer data)
1409 {
1410   GtkIconView *icon_view;
1411   gdouble value;
1412
1413   GDK_THREADS_ENTER ();
1414   
1415   icon_view = data;
1416
1417   value = MIN (icon_view->priv->vadjustment->value +
1418                icon_view->priv->scroll_value_diff,
1419                icon_view->priv->vadjustment->upper -
1420                icon_view->priv->vadjustment->page_size);
1421
1422   gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
1423
1424   gtk_icon_view_update_rubberband (icon_view);
1425   
1426   GDK_THREADS_LEAVE ();
1427
1428   return TRUE;
1429 }
1430
1431 static gboolean
1432 gtk_icon_view_motion (GtkWidget      *widget,
1433                       GdkEventMotion *event)
1434 {
1435   GtkIconView *icon_view;
1436   gint abs_y;
1437   
1438   icon_view = GTK_ICON_VIEW (widget);
1439
1440   gtk_icon_view_maybe_begin_drag (icon_view, event);
1441
1442   if (icon_view->priv->doing_rubberband)
1443     {
1444       gtk_icon_view_update_rubberband (widget);
1445       
1446       abs_y = event->y - icon_view->priv->height *
1447         (icon_view->priv->vadjustment->value /
1448          (icon_view->priv->vadjustment->upper -
1449           icon_view->priv->vadjustment->lower));
1450
1451       if (abs_y < 0 || abs_y > widget->allocation.height)
1452         {
1453           if (abs_y < 0)
1454             icon_view->priv->scroll_value_diff = abs_y;
1455           else
1456             icon_view->priv->scroll_value_diff = abs_y - widget->allocation.height;
1457
1458           icon_view->priv->event_last_x = event->x;
1459           icon_view->priv->event_last_y = event->y;
1460
1461           if (icon_view->priv->scroll_timeout_id == 0)
1462             icon_view->priv->scroll_timeout_id = g_timeout_add (30, rubberband_scroll_timeout, 
1463                                                                 icon_view);
1464         }
1465       else 
1466         remove_scroll_timeout (icon_view);
1467     }
1468   
1469   return TRUE;
1470 }
1471
1472 static void
1473 gtk_icon_view_remove (GtkContainer *container,
1474                       GtkWidget    *widget)
1475 {
1476   GtkIconView *icon_view;
1477   GtkIconViewChild *child = NULL;
1478   GList *tmp_list;
1479
1480   icon_view = GTK_ICON_VIEW (container);
1481   
1482   tmp_list = icon_view->priv->children;
1483   while (tmp_list)
1484     {
1485       child = tmp_list->data;
1486       if (child->widget == widget)
1487         {
1488           gtk_widget_unparent (widget);
1489
1490           icon_view->priv->children = g_list_remove_link (icon_view->priv->children, tmp_list);
1491           g_list_free_1 (tmp_list);
1492           g_free (child);
1493           return;
1494         }
1495
1496       tmp_list = tmp_list->next;
1497     }
1498 }
1499
1500 static void
1501 gtk_icon_view_forall (GtkContainer *container,
1502                       gboolean      include_internals,
1503                       GtkCallback   callback,
1504                       gpointer      callback_data)
1505 {
1506   GtkIconView *icon_view;
1507   GtkIconViewChild *child = NULL;
1508   GList *tmp_list;
1509
1510   icon_view = GTK_ICON_VIEW (container);
1511
1512   tmp_list = icon_view->priv->children;
1513   while (tmp_list)
1514     {
1515       child = tmp_list->data;
1516       tmp_list = tmp_list->next;
1517
1518       (* callback) (child->widget, callback_data);
1519     }
1520 }
1521
1522 static void
1523 gtk_icon_view_item_activate_cell (GtkIconView         *icon_view, 
1524                                   GtkIconViewItem     *item, 
1525                                   GtkIconViewCellInfo *info,
1526                                   GdkEvent            *event)
1527 {
1528   GtkTreePath *path;  
1529   gchar *path_string;
1530   GdkRectangle cell_area;
1531   gboolean visible, mode;
1532
1533   gtk_icon_view_set_cell_data (icon_view, item);
1534
1535   g_object_get (info->cell,
1536                 "visible", &visible,
1537                 "mode", &mode,
1538                 NULL);
1539
1540   if (visible && mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
1541     {
1542       gtk_icon_view_get_cell_area (icon_view, item, info, &cell_area);
1543
1544       path = gtk_tree_path_new_from_indices (item->index, -1);
1545       path_string = gtk_tree_path_to_string (path);
1546       gtk_tree_path_free (path);
1547
1548       gtk_cell_renderer_activate (info->cell, 
1549                                   event, 
1550                                   GTK_WIDGET (icon_view),
1551                                   path_string, 
1552                                   &cell_area, 
1553                                   &cell_area, 
1554                                   0);
1555
1556       g_free (path_string);      
1557     }
1558 }
1559
1560 static void 
1561 gtk_icon_view_put (GtkIconView     *icon_view,
1562                    GtkWidget       *widget,
1563                    GtkIconViewItem *item,
1564                    gint             cell)
1565 {
1566   GtkIconViewChild *child;
1567   
1568   child = g_new (GtkIconViewChild, 1);
1569   
1570   child->widget = widget;
1571   child->item = item;
1572   child->cell = cell;
1573
1574   icon_view->priv->children = g_list_append (icon_view->priv->children, child);
1575
1576   if (GTK_WIDGET_REALIZED (icon_view))
1577     gtk_widget_set_parent_window (child->widget, icon_view->priv->bin_window);
1578   
1579   gtk_widget_set_parent (widget, GTK_WIDGET (icon_view));
1580 }
1581
1582 static void
1583 gtk_icon_view_remove_widget (GtkCellEditable *editable,
1584                              GtkIconView     *icon_view)
1585 {
1586   GList *l;
1587   GtkIconViewItem *item;
1588
1589   if (icon_view->priv->edited_item == NULL)
1590     return;
1591
1592   item = icon_view->priv->edited_item;
1593   icon_view->priv->edited_item = NULL;
1594   icon_view->priv->editable = NULL;
1595   for (l = icon_view->priv->cell_list; l; l = l->next)
1596     {
1597       GtkIconViewCellInfo *info = l->data;
1598
1599       info->editing = FALSE;
1600     }
1601
1602   if (GTK_WIDGET_HAS_FOCUS (editable))
1603     gtk_widget_grab_focus (GTK_WIDGET (icon_view));
1604   
1605   g_signal_handlers_disconnect_by_func (editable,
1606                                         gtk_icon_view_remove_widget,
1607                                         icon_view);
1608
1609   gtk_container_remove (GTK_CONTAINER (icon_view),
1610                         GTK_WIDGET (editable));  
1611
1612   gtk_icon_view_queue_draw_item (icon_view, item);
1613 }
1614
1615
1616 static void
1617 gtk_icon_view_start_editing (GtkIconView         *icon_view, 
1618                              GtkIconViewItem     *item, 
1619                              GtkIconViewCellInfo *info,
1620                              GdkEvent            *event)
1621 {
1622   GtkTreePath *path;  
1623   gchar *path_string;
1624   GdkRectangle cell_area;
1625   gboolean visible, mode;
1626   GtkCellEditable *editable;
1627
1628   gtk_icon_view_set_cell_data (icon_view, item);
1629
1630   g_object_get (info->cell,
1631                 "visible", &visible,
1632                 "mode", &mode,
1633                 NULL);
1634   if (visible && mode == GTK_CELL_RENDERER_MODE_EDITABLE)
1635     {
1636       gtk_icon_view_get_cell_area (icon_view, item, info, &cell_area);
1637
1638       path = gtk_tree_path_new_from_indices (item->index, -1);
1639       path_string = gtk_tree_path_to_string (path);
1640       gtk_tree_path_free (path);
1641
1642       editable = gtk_cell_renderer_start_editing (info->cell, 
1643                                                   event, 
1644                                                   GTK_WIDGET (icon_view),
1645                                                   path_string, 
1646                                                   &cell_area, 
1647                                                   &cell_area, 
1648                                                   0);
1649       g_free (path_string);      
1650
1651       /* FIXME ugly special case */ 
1652       if (GTK_IS_ENTRY (editable) || GTK_IS_COMBO_BOX (editable))
1653         g_object_set (editable, "has_frame", TRUE, NULL);
1654
1655       /* the rest corresponds to tree_view_real_start_editing... */
1656       icon_view->priv->edited_item = item;
1657       icon_view->priv->editable = editable;
1658       info->editing = TRUE;
1659
1660       gtk_icon_view_put (icon_view, GTK_WIDGET (editable), item, 
1661                          info->position);
1662       gtk_cell_editable_start_editing (GTK_CELL_EDITABLE (editable), 
1663                                        (GdkEvent *)event);
1664       gtk_widget_grab_focus (GTK_WIDGET (editable));
1665       g_signal_connect (editable, "remove_widget",
1666                         G_CALLBACK (gtk_icon_view_remove_widget), 
1667                         icon_view);
1668
1669     }
1670 }
1671
1672 static void
1673 gtk_icon_view_stop_editing (GtkIconView *icon_view,
1674                             gboolean     cancel_editing)
1675 {
1676   GtkCellRenderer *cell = NULL;
1677   GtkIconViewItem *item;
1678   GList *l;
1679
1680   if (icon_view->priv->edited_item == NULL)
1681     return;
1682
1683   /*
1684    * This is very evil. We need to do this, because
1685    * gtk_cell_editable_editing_done may trigger gtk_icon_view_row_changed
1686    * later on. If gtk_icon_view_row_changed notices
1687    * icon_view->priv->edited_item != NULL, it'll call
1688    * gtk_icon_view_stop_editing again. Bad things will happen then.
1689    *
1690    * Please read that again if you intend to modify anything here.
1691    */
1692
1693   item = icon_view->priv->edited_item;
1694   icon_view->priv->edited_item = NULL;
1695
1696   for (l = icon_view->priv->cell_list; l; l = l->next)
1697     {
1698       GtkIconViewCellInfo *info = l->data;
1699
1700       if (info->editing)
1701         {
1702           cell = info->cell;
1703           break;
1704         }
1705     }
1706
1707   if (cell == NULL)
1708     return;
1709
1710   gtk_cell_renderer_stop_editing (cell, cancel_editing);
1711   if (!cancel_editing)
1712     gtk_cell_editable_editing_done (icon_view->priv->editable);
1713
1714   icon_view->priv->edited_item = item;
1715
1716   gtk_cell_editable_remove_widget (icon_view->priv->editable);
1717 }
1718
1719 /**
1720  * gtk_icon_view_set_cursor:
1721  * @icon_view: A #GtkIconView
1722  * @path: A #GtkTreePath
1723  * @cell: A #GtkCellRenderer or %NULL
1724  * @start_editing: %TRUE if the specified cell should start being edited.
1725  *
1726  * Sets the current keyboard focus to be at @path, and selects it.  This is
1727  * useful when you want to focus the user's attention on a particular item.  
1728  * If @cell is not %NULL, then focus is given to the cell specified by 
1729  * it. Additionally, if @start_editing is %TRUE, then editing should be 
1730  * started in the specified cell.  
1731  *
1732  * This function is often followed by <literal>gtk_widget_grab_focus 
1733  * (icon_view)</literal> in order to give keyboard focus to the widget.  
1734  * Please note that editing can only happen when the widget is realized.
1735  *
1736  * Since: 2.8
1737  **/
1738 void
1739 gtk_icon_view_set_cursor (GtkIconView     *icon_view,
1740                           GtkTreePath     *path,
1741                           GtkCellRenderer *cell,
1742                           gboolean         start_editing)
1743 {
1744   GtkIconViewItem *item = NULL;
1745   GtkIconViewCellInfo *info =  NULL;
1746   GList *l;
1747   gint i, cell_pos;
1748
1749   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
1750   g_return_if_fail (path != NULL);
1751   g_return_if_fail (cell == NULL || GTK_IS_CELL_RENDERER (cell));
1752
1753   gtk_icon_view_stop_editing (icon_view, TRUE);
1754
1755   if (gtk_tree_path_get_depth (path) == 1)
1756     item = g_list_nth_data (icon_view->priv->items,
1757                             gtk_tree_path_get_indices(path)[0]);
1758   
1759   if (!item)
1760     return;
1761   
1762   cell_pos = -1;
1763   for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
1764     {
1765       info = l->data;
1766
1767       if (info->cell == cell)
1768         {
1769           cell_pos = i;
1770           break;
1771         }
1772
1773       info = NULL;
1774     }
1775
1776   gtk_icon_view_set_cursor_item (icon_view, item, cell_pos);
1777   if (FALSE && GTK_WIDGET_REALIZED (icon_view))
1778     gtk_icon_view_scroll_to_item (icon_view, item);
1779   else
1780     gtk_icon_view_scroll_to_path (icon_view, path,
1781                                   FALSE, 0.0, 0.0);
1782
1783   if (start_editing)
1784     gtk_icon_view_start_editing (icon_view, item, info, NULL);
1785 }
1786
1787 /**
1788  * gtk_icon_view_get_cursor:
1789  * @icon_view: A #GtkIconView
1790  * @path: Return location for the current cursor path, or %NULL
1791  * @cell: Return location the current focus cell, or %NULL
1792  *
1793  * Fills in @path and @cell with the current cursor path and cell. 
1794  * If the cursor isn't currently set, then *@path will be %NULL.  
1795  * If no cell currently has focus, then *@cell will be %NULL.
1796  *
1797  * The returned #GtkTreePath must be freed with gtk_tree_path_free().
1798  *
1799  * Return value: %TRUE if the cursor is set.
1800  *
1801  * Since: 2.8
1802  **/
1803 gboolean
1804 gtk_icon_view_get_cursor (GtkIconView      *icon_view,
1805                           GtkTreePath     **path,
1806                           GtkCellRenderer **cell)
1807 {
1808   GtkIconViewItem *item;
1809   GtkIconViewCellInfo *info;
1810
1811   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
1812
1813   item = icon_view->priv->cursor_item;
1814   if (icon_view->priv->cursor_cell < 0)
1815     info = NULL;
1816   else
1817     info = g_list_nth_data (icon_view->priv->cell_list, 
1818                             icon_view->priv->cursor_cell);
1819
1820   if (path != NULL)
1821     {
1822       if (item != NULL)
1823         *path = gtk_tree_path_new_from_indices (item->index, -1);
1824       else
1825         *path = NULL;
1826     }
1827
1828   if (cell != NULL)
1829     {
1830       if (info != NULL)
1831         *cell = info->cell;
1832       else 
1833         *cell = NULL;
1834     }
1835
1836   return (item != NULL);
1837 }
1838
1839 static gboolean
1840 gtk_icon_view_button_press (GtkWidget      *widget,
1841                             GdkEventButton *event)
1842 {
1843   GtkIconView *icon_view;
1844   GtkIconViewItem *item;
1845   GtkIconViewCellInfo *info = NULL;
1846   gboolean dirty = FALSE;
1847   GtkCellRendererMode mode;
1848   gint cursor_cell;
1849
1850   icon_view = GTK_ICON_VIEW (widget);
1851
1852   if (event->window != icon_view->priv->bin_window)
1853     return FALSE;
1854
1855   if (!GTK_WIDGET_HAS_FOCUS (widget))
1856     gtk_widget_grab_focus (widget);
1857
1858   if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
1859     {
1860       item = gtk_icon_view_get_item_at_coords (icon_view, 
1861                                                event->x, event->y,
1862                                                TRUE,
1863                                                &info);      
1864       if (item != NULL)
1865         {
1866           g_object_get (info->cell, "mode", &mode, NULL);
1867           
1868           if (mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE ||
1869               mode == GTK_CELL_RENDERER_MODE_EDITABLE)
1870             cursor_cell = g_list_index (icon_view->priv->cell_list, info);
1871           else
1872             cursor_cell = -1;
1873           
1874           gtk_icon_view_scroll_to_item (icon_view, item);
1875           
1876           if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
1877             {
1878               gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
1879             }
1880           else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
1881                    (event->state & GDK_SHIFT_MASK))
1882             {
1883               gtk_icon_view_unselect_all_internal (icon_view);
1884
1885               gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
1886               if (!icon_view->priv->anchor_item)
1887                 icon_view->priv->anchor_item = item;
1888               else 
1889                 gtk_icon_view_select_all_between (icon_view,
1890                                                   icon_view->priv->anchor_item,
1891                                                   item);
1892               dirty = TRUE;
1893             }
1894           else 
1895             {
1896               if ((icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE ||
1897                   ((icon_view->priv->selection_mode == GTK_SELECTION_SINGLE) && item->selected)) &&
1898                   (event->state & GDK_CONTROL_MASK))
1899                 {
1900                   item->selected = !item->selected;
1901                   gtk_icon_view_queue_draw_item (icon_view, item);
1902                   dirty = TRUE;
1903                 }
1904               else
1905                 {
1906                   if (!item->selected)
1907                     {
1908                       gtk_icon_view_unselect_all_internal (icon_view);
1909                       
1910                       item->selected = TRUE;
1911                       gtk_icon_view_queue_draw_item (icon_view, item);
1912                       dirty = TRUE;
1913                     }
1914                 }
1915               gtk_icon_view_set_cursor_item (icon_view, item, cursor_cell);
1916               icon_view->priv->anchor_item = item;
1917             }
1918
1919           /* Save press to possibly begin a drag */
1920           if (icon_view->priv->pressed_button < 0)
1921             {
1922               icon_view->priv->pressed_button = event->button;
1923               icon_view->priv->press_start_x = event->x;
1924               icon_view->priv->press_start_y = event->y;
1925             }
1926
1927           if (!icon_view->priv->last_single_clicked)
1928             icon_view->priv->last_single_clicked = item;
1929
1930           /* cancel the current editing, if it exists */
1931           gtk_icon_view_stop_editing (icon_view, TRUE);
1932           
1933           if (mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
1934             gtk_icon_view_item_activate_cell (icon_view, item, info, 
1935                                               (GdkEvent *)event);
1936           else if (mode == GTK_CELL_RENDERER_MODE_EDITABLE)
1937             gtk_icon_view_start_editing (icon_view, item, info, 
1938                                          (GdkEvent *)event);      
1939         }
1940       else
1941         {
1942           if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
1943               !(event->state & GDK_CONTROL_MASK))
1944             {
1945               dirty = gtk_icon_view_unselect_all_internal (icon_view);
1946             }
1947           
1948           if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
1949             gtk_icon_view_start_rubberbanding (icon_view, event->x, event->y);
1950         }
1951
1952     }
1953
1954   if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
1955     {
1956       item = gtk_icon_view_get_item_at_coords (icon_view,
1957                                                event->x, event->y,
1958                                                TRUE,
1959                                                NULL);
1960
1961       if (item && item == icon_view->priv->last_single_clicked)
1962         {
1963           GtkTreePath *path;
1964
1965           path = gtk_tree_path_new_from_indices (item->index, -1);
1966           gtk_icon_view_item_activated (icon_view, path);
1967           gtk_tree_path_free (path);
1968         }
1969
1970       icon_view->priv->last_single_clicked = NULL;
1971       icon_view->priv->pressed_button = -1;
1972     }
1973   
1974   if (dirty)
1975     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
1976
1977   return event->button == 1;
1978 }
1979
1980 static gboolean
1981 gtk_icon_view_button_release (GtkWidget      *widget,
1982                               GdkEventButton *event)
1983 {
1984   GtkIconView *icon_view;
1985
1986   icon_view = GTK_ICON_VIEW (widget);
1987   
1988   if (icon_view->priv->pressed_button == event->button)
1989     icon_view->priv->pressed_button = -1;
1990
1991   gtk_icon_view_stop_rubberbanding (icon_view);
1992
1993   remove_scroll_timeout (icon_view);
1994
1995   return TRUE;
1996 }
1997
1998 static void
1999 gtk_icon_view_update_rubberband (gpointer data)
2000 {
2001   GtkIconView *icon_view;
2002   gint x, y;
2003   GdkRectangle old_area;
2004   GdkRectangle new_area;
2005   GdkRectangle common;
2006   GdkRegion *invalid_region;
2007   
2008   icon_view = GTK_ICON_VIEW (data);
2009
2010   gdk_window_get_pointer (icon_view->priv->bin_window, &x, &y, NULL);
2011
2012   x = MAX (x, 0);
2013   y = MAX (y, 0);
2014
2015   old_area.x = MIN (icon_view->priv->rubberband_x1,
2016                     icon_view->priv->rubberband_x2);
2017   old_area.y = MIN (icon_view->priv->rubberband_y1,
2018                     icon_view->priv->rubberband_y2);
2019   old_area.width = ABS (icon_view->priv->rubberband_x2 -
2020                         icon_view->priv->rubberband_x1) + 1;
2021   old_area.height = ABS (icon_view->priv->rubberband_y2 -
2022                          icon_view->priv->rubberband_y1) + 1;
2023   
2024   new_area.x = MIN (icon_view->priv->rubberband_x1, x);
2025   new_area.y = MIN (icon_view->priv->rubberband_y1, y);
2026   new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
2027   new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
2028
2029   invalid_region = gdk_region_rectangle (&old_area);
2030   gdk_region_union_with_rect (invalid_region, &new_area);
2031
2032   gdk_rectangle_intersect (&old_area, &new_area, &common);
2033   if (common.width > 2 && common.height > 2)
2034     {
2035       GdkRegion *common_region;
2036
2037       /* make sure the border is invalidated */
2038       common.x += 1;
2039       common.y += 1;
2040       common.width -= 2;
2041       common.height -= 2;
2042       
2043       common_region = gdk_region_rectangle (&common);
2044
2045       gdk_region_subtract (invalid_region, common_region);
2046       gdk_region_destroy (common_region);
2047     }
2048   
2049   gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
2050     
2051   gdk_region_destroy (invalid_region);
2052
2053   icon_view->priv->rubberband_x2 = x;
2054   icon_view->priv->rubberband_y2 = y;  
2055
2056   gtk_icon_view_update_rubberband_selection (icon_view);
2057 }
2058
2059 static void
2060 gtk_icon_view_start_rubberbanding (GtkIconView  *icon_view,
2061                                    gint          x,
2062                                    gint          y)
2063 {
2064   GList *items;
2065
2066   g_assert (!icon_view->priv->doing_rubberband);
2067
2068   for (items = icon_view->priv->items; items; items = items->next)
2069     {
2070       GtkIconViewItem *item = items->data;
2071
2072       item->selected_before_rubberbanding = item->selected;
2073     }
2074   
2075   icon_view->priv->rubberband_x1 = x;
2076   icon_view->priv->rubberband_y1 = y;
2077   icon_view->priv->rubberband_x2 = x;
2078   icon_view->priv->rubberband_y2 = y;
2079
2080   icon_view->priv->doing_rubberband = TRUE;
2081
2082   gtk_grab_add (GTK_WIDGET (icon_view));
2083 }
2084
2085 static void
2086 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
2087 {
2088   if (!icon_view->priv->doing_rubberband)
2089     return;
2090
2091   icon_view->priv->doing_rubberband = FALSE;
2092
2093   gtk_grab_remove (GTK_WIDGET (icon_view));
2094   
2095   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2096 }
2097
2098 static void
2099 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
2100 {
2101   GList *items;
2102   gint x, y, width, height;
2103   gboolean dirty = FALSE;
2104   
2105   x = MIN (icon_view->priv->rubberband_x1,
2106            icon_view->priv->rubberband_x2);
2107   y = MIN (icon_view->priv->rubberband_y1,
2108            icon_view->priv->rubberband_y2);
2109   width = ABS (icon_view->priv->rubberband_x1 - 
2110                icon_view->priv->rubberband_x2);
2111   height = ABS (icon_view->priv->rubberband_y1 - 
2112                 icon_view->priv->rubberband_y2);
2113   
2114   for (items = icon_view->priv->items; items; items = items->next)
2115     {
2116       GtkIconViewItem *item = items->data;
2117       gboolean is_in;
2118       gboolean selected;
2119       
2120       is_in = gtk_icon_view_item_hit_test (icon_view, item, 
2121                                            x, y, width, height);
2122
2123       selected = is_in ^ item->selected_before_rubberbanding;
2124
2125       if (item->selected != selected)
2126         {
2127           item->selected = selected;
2128           dirty = TRUE;
2129           gtk_icon_view_queue_draw_item (icon_view, item);
2130         }
2131     }
2132
2133   if (dirty)
2134     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2135 }
2136
2137 static gboolean
2138 gtk_icon_view_item_hit_test (GtkIconView      *icon_view,
2139                              GtkIconViewItem  *item,
2140                              gint              x,
2141                              gint              y,
2142                              gint              width,
2143                              gint              height)
2144 {
2145   GList *l;
2146   GdkRectangle box;
2147  
2148   for (l = icon_view->priv->cell_list; l; l = l->next)
2149     {
2150       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2151       
2152       if (!info->cell->visible)
2153         continue;
2154       
2155       gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2156
2157       if (MIN (x + width, box.x + box.width) - MAX (x, box.x) > 0 &&
2158         MIN (y + height, box.y + box.height) - MAX (y, box.y) > 0)
2159         return TRUE;
2160     }
2161
2162   return FALSE;
2163 }
2164
2165 static gboolean
2166 gtk_icon_view_unselect_all_internal (GtkIconView  *icon_view)
2167 {
2168   gboolean dirty = FALSE;
2169   GList *items;
2170
2171   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
2172     return FALSE;
2173
2174   for (items = icon_view->priv->items; items; items = items->next)
2175     {
2176       GtkIconViewItem *item = items->data;
2177
2178       if (item->selected)
2179         {
2180           item->selected = FALSE;
2181           dirty = TRUE;
2182           gtk_icon_view_queue_draw_item (icon_view, item);
2183         }
2184     }
2185
2186   return dirty;
2187 }
2188
2189
2190 /* GtkIconView signals */
2191 static void
2192 gtk_icon_view_set_adjustments (GtkIconView   *icon_view,
2193                                GtkAdjustment *hadj,
2194                                GtkAdjustment *vadj)
2195 {
2196   gboolean need_adjust = FALSE;
2197
2198   if (hadj)
2199     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
2200   else
2201     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
2202   if (vadj)
2203     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
2204   else
2205     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
2206
2207   if (icon_view->priv->hadjustment && (icon_view->priv->hadjustment != hadj))
2208     {
2209       g_signal_handlers_disconnect_matched (icon_view->priv->hadjustment, G_SIGNAL_MATCH_DATA,
2210                                            0, 0, NULL, NULL, icon_view);
2211       g_object_unref (icon_view->priv->hadjustment);
2212     }
2213
2214   if (icon_view->priv->vadjustment && (icon_view->priv->vadjustment != vadj))
2215     {
2216       g_signal_handlers_disconnect_matched (icon_view->priv->vadjustment, G_SIGNAL_MATCH_DATA,
2217                                             0, 0, NULL, NULL, icon_view);
2218       g_object_unref (icon_view->priv->vadjustment);
2219     }
2220
2221   if (icon_view->priv->hadjustment != hadj)
2222     {
2223       icon_view->priv->hadjustment = hadj;
2224       g_object_ref_sink (icon_view->priv->hadjustment);
2225
2226       g_signal_connect (icon_view->priv->hadjustment, "value_changed",
2227                         G_CALLBACK (gtk_icon_view_adjustment_changed),
2228                         icon_view);
2229       need_adjust = TRUE;
2230     }
2231
2232   if (icon_view->priv->vadjustment != vadj)
2233     {
2234       icon_view->priv->vadjustment = vadj;
2235       g_object_ref_sink (icon_view->priv->vadjustment);
2236
2237       g_signal_connect (icon_view->priv->vadjustment, "value_changed",
2238                         G_CALLBACK (gtk_icon_view_adjustment_changed),
2239                         icon_view);
2240       need_adjust = TRUE;
2241     }
2242
2243   if (need_adjust)
2244     gtk_icon_view_adjustment_changed (NULL, icon_view);
2245 }
2246
2247 static void
2248 gtk_icon_view_real_select_all (GtkIconView *icon_view)
2249 {
2250   gtk_icon_view_select_all (icon_view);
2251 }
2252
2253 static void
2254 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
2255 {
2256   gtk_icon_view_unselect_all (icon_view);
2257 }
2258
2259 static void
2260 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
2261 {
2262   gtk_icon_view_unselect_all (icon_view);
2263   
2264   if (icon_view->priv->cursor_item != NULL)
2265     gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2266 }
2267
2268 static gboolean
2269 gtk_icon_view_real_activate_cursor_item (GtkIconView *icon_view)
2270 {
2271   GtkTreePath *path;
2272   GtkCellRendererMode mode;
2273   GtkIconViewCellInfo *info = NULL;
2274   
2275   if (!icon_view->priv->cursor_item)
2276     return FALSE;
2277
2278   info = g_list_nth_data (icon_view->priv->cell_list, 
2279                           icon_view->priv->cursor_cell);
2280
2281   if (info) 
2282     {  
2283       g_object_get (info->cell, "mode", &mode, NULL);
2284
2285       if (mode == GTK_CELL_RENDERER_MODE_ACTIVATABLE)
2286         {
2287           gtk_icon_view_item_activate_cell (icon_view, 
2288                                             icon_view->priv->cursor_item, 
2289                                             info, NULL);
2290           return TRUE;
2291         }
2292       else if (mode == GTK_CELL_RENDERER_MODE_EDITABLE)
2293         {
2294           gtk_icon_view_start_editing (icon_view, 
2295                                        icon_view->priv->cursor_item, 
2296                                        info, NULL);
2297           return TRUE;
2298         }
2299     }
2300   
2301   path = gtk_tree_path_new_from_indices (icon_view->priv->cursor_item->index, -1);
2302   gtk_icon_view_item_activated (icon_view, path);
2303   gtk_tree_path_free (path);
2304
2305   return TRUE;
2306 }
2307
2308 static void
2309 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
2310 {
2311   if (!icon_view->priv->cursor_item)
2312     return;
2313
2314   switch (icon_view->priv->selection_mode)
2315     {
2316     case GTK_SELECTION_NONE:
2317       break;
2318     case GTK_SELECTION_BROWSE:
2319       gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2320       break;
2321     case GTK_SELECTION_SINGLE:
2322       if (icon_view->priv->cursor_item->selected)
2323         gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
2324       else
2325         gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
2326       break;
2327     case GTK_SELECTION_MULTIPLE:
2328       icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
2329       g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0); 
2330       
2331       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
2332       break;
2333     }
2334 }
2335
2336 /* Internal functions */
2337 static void
2338 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
2339                                   GtkIconView   *icon_view)
2340 {
2341   if (GTK_WIDGET_REALIZED (icon_view))
2342     {
2343       gdk_window_move (icon_view->priv->bin_window,
2344                        - icon_view->priv->hadjustment->value,
2345                        - icon_view->priv->vadjustment->value);
2346
2347       if (icon_view->priv->doing_rubberband)
2348         gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
2349
2350       gdk_window_process_updates (icon_view->priv->bin_window, TRUE);
2351     }
2352 }
2353
2354 static GList *
2355 gtk_icon_view_layout_single_row (GtkIconView *icon_view, 
2356                                  GList       *first_item, 
2357                                  gint         item_width,
2358                                  gint         row,
2359                                  gint        *y, 
2360                                  gint        *maximum_width)
2361 {
2362   gint focus_width;
2363   gint x, current_width;
2364   GList *items, *last_item;
2365   gint col;
2366   gint colspan;
2367   gint *max_height;
2368   gint i;
2369   gboolean rtl;
2370
2371   rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2372   max_height = g_new0 (gint, icon_view->priv->n_cells);
2373
2374   x = 0;
2375   col = 0;
2376   items = first_item;
2377   current_width = 0;
2378
2379   gtk_widget_style_get (GTK_WIDGET (icon_view),
2380                         "focus-line-width", &focus_width,
2381                         NULL);
2382
2383   x += icon_view->priv->margin + focus_width;
2384   current_width += 2 * (icon_view->priv->margin + focus_width);
2385
2386   items = first_item;
2387   while (items)
2388     {
2389       GtkIconViewItem *item = items->data;
2390
2391       gtk_icon_view_calculate_item_size (icon_view, item);
2392       colspan = 1 + (item->width - 1) / (item_width + icon_view->priv->column_spacing);
2393
2394       item->width = colspan * item_width + (colspan - 1) * icon_view->priv->column_spacing;
2395
2396       current_width += item->width;
2397
2398       if (items != first_item)
2399         {
2400           if ((icon_view->priv->columns <= 0 && current_width > GTK_WIDGET (icon_view)->allocation.width) ||
2401               (icon_view->priv->columns > 0 && col >= icon_view->priv->columns))
2402             break;
2403         }
2404
2405       current_width += icon_view->priv->column_spacing + 2 * focus_width;
2406
2407       item->y = *y + focus_width;
2408       item->x = rtl ? GTK_WIDGET (icon_view)->allocation.width - item->width - x : x;
2409
2410       x = current_width - (icon_view->priv->margin + focus_width); 
2411
2412       for (i = 0; i < icon_view->priv->n_cells; i++)
2413         max_height[i] = MAX (max_height[i], item->box[i].height);
2414               
2415       if (current_width > *maximum_width)
2416         *maximum_width = current_width;
2417
2418       item->row = row;
2419       item->col = col;
2420
2421       col += colspan;
2422       items = items->next;
2423     }
2424
2425   last_item = items;
2426
2427   /* Now go through the row again and align the icons */
2428   for (items = first_item; items != last_item; items = items->next)
2429     {
2430       GtkIconViewItem *item = items->data;
2431
2432       gtk_icon_view_calculate_item_size2 (icon_view, item, max_height);
2433
2434       /* We may want to readjust the new y coordinate. */
2435       if (item->y + item->height + focus_width + icon_view->priv->row_spacing > *y)
2436         *y = item->y + item->height + focus_width + icon_view->priv->row_spacing;
2437
2438       if (rtl)
2439         item->col = col - 1 - item->col;
2440     }
2441
2442   g_free (max_height);
2443   
2444   return last_item;
2445 }
2446
2447 static void
2448 gtk_icon_view_set_adjustment_upper (GtkAdjustment *adj,
2449                                     gdouble        upper)
2450 {
2451   if (upper != adj->upper)
2452     {
2453       gdouble min = MAX (0.0, upper - adj->page_size);
2454       gboolean value_changed = FALSE;
2455       
2456       adj->upper = upper;
2457
2458       if (adj->value > min)
2459         {
2460           adj->value = min;
2461           value_changed = TRUE;
2462         }
2463       
2464       gtk_adjustment_changed (adj);
2465       
2466       if (value_changed)
2467         gtk_adjustment_value_changed (adj);
2468     }
2469 }
2470
2471 static void
2472 gtk_icon_view_layout (GtkIconView *icon_view)
2473 {
2474   gint y = 0, maximum_width = 0;
2475   GList *icons;
2476   GtkWidget *widget;
2477   gint row;
2478   gint item_width;
2479
2480   if (icon_view->priv->model == NULL)
2481     return;
2482
2483   widget = GTK_WIDGET (icon_view);
2484
2485   item_width = icon_view->priv->item_width;
2486
2487   if (item_width < 0)
2488     {
2489       for (icons = icon_view->priv->items; icons; icons = icons->next)
2490         {
2491           GtkIconViewItem *item = icons->data;
2492           gtk_icon_view_calculate_item_size (icon_view, item);
2493           item_width = MAX (item_width, item->width);
2494         }
2495     }
2496
2497   icons = icon_view->priv->items;
2498   y += icon_view->priv->margin;
2499   row = 0;
2500   
2501   do
2502     {
2503       icons = gtk_icon_view_layout_single_row (icon_view, icons, 
2504                                                item_width, row,
2505                                                &y, &maximum_width);
2506       row++;
2507     }
2508   while (icons != NULL);
2509
2510   if (maximum_width != icon_view->priv->width)
2511     {
2512       icon_view->priv->width = maximum_width;
2513     }
2514   y += icon_view->priv->margin;
2515   
2516   if (y != icon_view->priv->height)
2517     {
2518       icon_view->priv->height = y;
2519     }
2520
2521   gtk_icon_view_set_adjustment_upper (icon_view->priv->hadjustment, icon_view->priv->width);
2522   gtk_icon_view_set_adjustment_upper (icon_view->priv->vadjustment, icon_view->priv->height);
2523
2524   if (GTK_WIDGET_REALIZED (icon_view))
2525     {
2526       gdk_window_resize (icon_view->priv->bin_window,
2527                          MAX (icon_view->priv->width, widget->allocation.width),
2528                          MAX (icon_view->priv->height, widget->allocation.height));
2529     }
2530
2531   if (icon_view->priv->layout_idle_id != 0)
2532     {
2533       g_source_remove (icon_view->priv->layout_idle_id);
2534       icon_view->priv->layout_idle_id = 0;
2535     }
2536
2537   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
2538 }
2539
2540 static void 
2541 gtk_icon_view_get_cell_area (GtkIconView         *icon_view,
2542                              GtkIconViewItem     *item,
2543                              GtkIconViewCellInfo *info,
2544                              GdkRectangle        *cell_area)
2545 {
2546   if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2547     {
2548       cell_area->x = item->box[info->position].x - item->before[info->position];
2549       cell_area->y = item->y;
2550       cell_area->width = item->box[info->position].width + 
2551         item->before[info->position] + item->after[info->position];
2552       cell_area->height = item->height;
2553     }
2554   else
2555     {
2556       cell_area->x = item->x;
2557       cell_area->y = item->box[info->position].y - item->before[info->position];
2558       cell_area->width = item->width;
2559       cell_area->height = item->box[info->position].height + 
2560         item->before[info->position] + item->after[info->position];
2561     }
2562 }
2563
2564 static void 
2565 gtk_icon_view_get_cell_box (GtkIconView         *icon_view,
2566                             GtkIconViewItem     *item,
2567                             GtkIconViewCellInfo *info,
2568                             GdkRectangle        *box)
2569 {
2570   *box = item->box[info->position];
2571 }
2572
2573 /* try to guess a reasonable wrap width for an implicit text cell renderer
2574  */
2575 static void
2576 adjust_wrap_width (GtkIconView     *icon_view,
2577                    GtkIconViewItem *item)
2578 {
2579   GtkIconViewCellInfo *text_info;
2580   GtkIconViewCellInfo *pixbuf_info;
2581   gint pixbuf_width, wrap_width;
2582       
2583   if (icon_view->priv->text_cell != -1 &&
2584       icon_view->priv->pixbuf_cell != -1)
2585     {
2586       text_info = g_list_nth_data (icon_view->priv->cell_list,
2587                                    icon_view->priv->text_cell);
2588       pixbuf_info = g_list_nth_data (icon_view->priv->cell_list,
2589                                      icon_view->priv->pixbuf_cell);
2590       
2591       gtk_cell_renderer_get_size (pixbuf_info->cell, 
2592                                   GTK_WIDGET (icon_view), 
2593                                   NULL, NULL, NULL,
2594                                   &pixbuf_width, 
2595                                   NULL);
2596           
2597       if (item->width == -1)
2598         wrap_width = pixbuf_width;
2599       else if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
2600         wrap_width = item->width;
2601       else
2602         wrap_width = item->width - pixbuf_width - icon_view->priv->spacing;
2603
2604       g_object_set (text_info->cell, "wrap-width", wrap_width, NULL);
2605     }
2606 }
2607
2608 static void
2609 gtk_icon_view_calculate_item_size (GtkIconView     *icon_view,
2610                                    GtkIconViewItem *item)
2611 {
2612   gint spacing;
2613   GList *l;
2614
2615   if (item->width != -1 && item->height != -1) 
2616     return;
2617
2618   if (item->n_cells != icon_view->priv->n_cells)
2619     {
2620       g_free (item->before);
2621       g_free (item->after);
2622       g_free (item->box);
2623       
2624       item->before = g_new0 (gint, icon_view->priv->n_cells);
2625       item->after = g_new0 (gint, icon_view->priv->n_cells);
2626       item->box = g_new0 (GdkRectangle, icon_view->priv->n_cells);
2627
2628       item->n_cells = icon_view->priv->n_cells;
2629     }
2630
2631   gtk_icon_view_set_cell_data (icon_view, item);
2632
2633   spacing = icon_view->priv->spacing;
2634
2635   item->width = 0;
2636   item->height = 0;
2637   for (l = icon_view->priv->cell_list; l; l = l->next)
2638     {
2639       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2640       
2641       if (!info->cell->visible)
2642         continue;
2643       
2644       gtk_cell_renderer_get_size (info->cell, GTK_WIDGET (icon_view), 
2645                                   NULL, NULL, NULL,
2646                                   &item->box[info->position].width, 
2647                                   &item->box[info->position].height);
2648       
2649       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2650         {
2651           item->width += item->box[info->position].width 
2652             + (info->position > 0 ? spacing : 0);
2653           item->height = MAX (item->height, item->box[info->position].height);
2654         }
2655       else
2656         {
2657           item->width = MAX (item->width, item->box[info->position].width);
2658           item->height += item->box[info->position].height + (info->position > 0 ? spacing : 0);
2659         }
2660     }
2661 }
2662
2663 static void
2664 gtk_icon_view_calculate_item_size2 (GtkIconView     *icon_view,
2665                                     GtkIconViewItem *item,
2666                                     gint            *max_height)
2667 {
2668   GdkRectangle cell_area;
2669   gint spacing;
2670   GList *l;
2671   gint i, k;
2672   gboolean rtl;
2673
2674   rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
2675
2676   gtk_icon_view_set_cell_data (icon_view, item);
2677
2678   spacing = icon_view->priv->spacing;
2679
2680   item->height = 0;
2681   for (i = 0; i < icon_view->priv->n_cells; i++)
2682     {
2683       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2684         item->height = MAX (item->height, max_height[i]);
2685       else
2686         item->height += max_height[i] + (i > 0 ? spacing : 0);
2687     }
2688
2689   cell_area.x = item->x;
2690   cell_area.y = item->y;
2691       
2692   for (k = 0; k < 2; k++)
2693     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
2694       {
2695         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2696         
2697         if (info->pack == (k ? GTK_PACK_START : GTK_PACK_END))
2698           continue;
2699
2700         if (!info->cell->visible)
2701         continue;
2702
2703       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2704         {
2705           cell_area.width = item->box[info->position].width;
2706           cell_area.height = item->height;
2707         }
2708       else
2709         {
2710           cell_area.width = item->width;
2711           cell_area.height = max_height[i];
2712         }
2713
2714       gtk_cell_renderer_get_size (info->cell, GTK_WIDGET (icon_view), 
2715                                   &cell_area,
2716                                   &item->box[info->position].x, &item->box[info->position].y,
2717                                   &item->box[info->position].width, &item->box[info->position].height);
2718
2719       item->box[info->position].x += cell_area.x;
2720       item->box[info->position].y += cell_area.y;
2721       if (icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2722         {
2723           item->before[info->position] = item->box[info->position].x - cell_area.x;
2724           item->after[info->position] = cell_area.width - item->box[info->position].width - item->before[info->position];
2725           cell_area.x += cell_area.width + spacing;
2726         }
2727       else
2728         {
2729           if (item->box[info->position].width > item->width)
2730             {
2731               item->width = item->box[info->position].width;
2732               cell_area.width = item->width;
2733             }
2734           item->before[info->position] = item->box[info->position].y - cell_area.y;
2735           item->after[info->position] = cell_area.height - item->box[info->position].height - item->before[info->position];
2736           cell_area.y += cell_area.height + spacing;
2737         }
2738     }
2739
2740   if (rtl && icon_view->priv->orientation == GTK_ORIENTATION_HORIZONTAL)
2741     {
2742       for (i = 0; i < icon_view->priv->n_cells; i++)
2743         {
2744           item->box[i].x = item->x + item->width - 
2745             (item->box[i].x + item->box[i].width - item->x);
2746         }      
2747     }   
2748 }
2749
2750 static void
2751 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
2752 {
2753   g_list_foreach (icon_view->priv->items,
2754                   (GFunc)gtk_icon_view_item_invalidate_size, NULL);
2755 }
2756
2757 static void
2758 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
2759 {
2760   item->width = -1;
2761   item->height = -1;
2762 }
2763
2764 static void
2765 gtk_icon_view_paint_item (GtkIconView     *icon_view,
2766                           cairo_t         *cr,
2767                           GtkIconViewItem *item,
2768                           GdkRectangle    *area,
2769                           GdkDrawable     *drawable,
2770                           gint             x,
2771                           gint             y,
2772                           gboolean         draw_focus)
2773 {
2774   gint focus_width, focus_pad;
2775   gint padding;
2776   GdkRectangle cell_area, box;
2777   GList *l;
2778   gint i;
2779   GtkStateType state;
2780   GtkCellRendererState flags;
2781       
2782   if (icon_view->priv->model == NULL)
2783     return;
2784   
2785   gtk_icon_view_set_cell_data (icon_view, item);
2786
2787   gtk_widget_style_get (GTK_WIDGET (icon_view),
2788                         "focus-line-width", &focus_width,
2789                         "focus-padding", &focus_pad,
2790                         NULL);
2791   
2792   padding = focus_width; 
2793   
2794   if (item->selected)
2795     {
2796       flags = GTK_CELL_RENDERER_SELECTED;
2797       if (GTK_WIDGET_HAS_FOCUS (icon_view))
2798         state = GTK_STATE_SELECTED;
2799       else
2800         state = GTK_STATE_ACTIVE;
2801     }
2802   else
2803     {
2804       flags = 0;
2805       state = GTK_STATE_NORMAL;
2806     }
2807   
2808 #ifdef DEBUG_ICON_VIEW
2809   gdk_draw_rectangle (drawable,
2810                       GTK_WIDGET (icon_view)->style->black_gc,
2811                       FALSE,
2812                       x, y, 
2813                       item->width, item->height);
2814 #endif
2815
2816   if (item->selected)
2817     for (l = icon_view->priv->cell_list; l; l = l->next)
2818       {
2819         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2820         
2821         if (!info->cell->visible)
2822           continue;
2823         
2824         gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2825
2826         /* FIXME we hardwire background drawing behind text
2827          * cell renderers here 
2828          */
2829         if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
2830           {
2831             gdk_cairo_set_source_color (cr, &GTK_WIDGET (icon_view)->style->base[state]);
2832             cairo_rectangle (cr,
2833                              x - item->x + box.x, 
2834                              y - item->y + box.y,
2835                              box.width, box.height);
2836             cairo_fill (cr);
2837           }
2838       }
2839   
2840   for (l = icon_view->priv->cell_list; l; l = l->next)
2841     {
2842       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2843       
2844       if (!info->cell->visible)
2845         continue;
2846       
2847       gtk_icon_view_get_cell_area (icon_view, item, info, &cell_area);
2848       
2849 #ifdef DEBUG_ICON_VIEW
2850       gdk_draw_rectangle (drawable,
2851                           GTK_WIDGET (icon_view)->style->black_gc,
2852                           FALSE,
2853                           x - item->x + cell_area.x, 
2854                           y - item->y + cell_area.y, 
2855                           cell_area.width, cell_area.height);
2856
2857       gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2858           
2859       gdk_draw_rectangle (drawable,
2860                           GTK_WIDGET (icon_view)->style->black_gc,
2861                           FALSE,
2862                           x - item->x + box.x, 
2863                           y - item->y + box.y, 
2864                           box.width, box.height);
2865 #endif
2866
2867       cell_area.x = x - item->x + cell_area.x;
2868       cell_area.y = y - item->y + cell_area.y;
2869
2870       gtk_cell_renderer_render (info->cell,
2871                                 drawable,
2872                                 GTK_WIDGET (icon_view),
2873                                 &cell_area, &cell_area, area, flags);
2874       
2875     }
2876
2877   /* FIXME where to draw the focus with generic cell renderers ? */
2878   if (draw_focus && 
2879       GTK_WIDGET_HAS_FOCUS (icon_view) &&
2880       item == icon_view->priv->cursor_item)
2881     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
2882       {
2883         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
2884         
2885         if (!info->cell->visible)
2886           continue;
2887         
2888         if (icon_view->priv->cursor_cell < 0 &&
2889             (info->cell->mode != GTK_CELL_RENDERER_MODE_INERT ||
2890              GTK_IS_CELL_RENDERER_TEXT (info->cell)))
2891           icon_view->priv->cursor_cell = i;
2892         
2893         if (i == icon_view->priv->cursor_cell)
2894           {
2895             gtk_icon_view_get_cell_box (icon_view, item, info, &box);
2896
2897             gtk_paint_focus (GTK_WIDGET (icon_view)->style,
2898                              drawable,
2899                              GTK_STATE_NORMAL,
2900                              area,
2901                              GTK_WIDGET (icon_view),
2902                              "icon_view",
2903                              x - item->x + box.x - padding,
2904                              y - item->y + box.y - padding,
2905                              box.width + 2 * padding,
2906                              box.height + 2 * padding);
2907             break;
2908           }
2909       }
2910 }
2911
2912 static void
2913 gtk_icon_view_paint_rubberband (GtkIconView     *icon_view,
2914                                 cairo_t         *cr,
2915                                 GdkRectangle    *area)
2916 {
2917   GdkRectangle rect;
2918   GdkRectangle rubber_rect;
2919   GdkColor *fill_color_gdk;
2920   guchar fill_color_alpha;
2921
2922   rubber_rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
2923   rubber_rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
2924   rubber_rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
2925   rubber_rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
2926
2927   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
2928     return;
2929
2930   gtk_widget_style_get (GTK_WIDGET (icon_view),
2931                         "selection-box-color", &fill_color_gdk,
2932                         "selection-box-alpha", &fill_color_alpha,
2933                         NULL);
2934
2935   if (!fill_color_gdk)
2936     fill_color_gdk = gdk_color_copy (&GTK_WIDGET (icon_view)->style->base[GTK_STATE_SELECTED]);
2937
2938   cairo_set_source_rgba (cr,
2939                          fill_color_gdk->red / 65535.,
2940                          fill_color_gdk->green / 65535.,
2941                          fill_color_gdk->blue / 65535.,
2942                          fill_color_alpha / 255.);
2943
2944   cairo_save (cr);
2945   gdk_cairo_rectangle (cr, &rect);
2946   cairo_clip (cr);
2947   cairo_paint (cr);
2948
2949   /* Draw the border without alpha */
2950   cairo_set_source_rgb (cr,
2951                         fill_color_gdk->red / 65535.,
2952                         fill_color_gdk->green / 65535.,
2953                         fill_color_gdk->blue / 65535.);
2954   cairo_rectangle (cr, 
2955                    rubber_rect.x + 0.5, rubber_rect.y + 0.5,
2956                    rubber_rect.width - 1, rubber_rect.height - 1);
2957   cairo_stroke (cr);
2958   cairo_restore (cr);
2959
2960   gdk_color_free (fill_color_gdk);
2961 }
2962
2963 static void
2964 gtk_icon_view_queue_draw_path (GtkIconView *icon_view,
2965                                GtkTreePath *path)
2966 {
2967   GList *l;
2968   gint index;
2969
2970   index = gtk_tree_path_get_indices (path)[0];
2971
2972   for (l = icon_view->priv->items; l; l = l->next) 
2973     {
2974       GtkIconViewItem *item = l->data;
2975
2976       if (item->index == index)
2977         {
2978           gtk_icon_view_queue_draw_item (icon_view, item);
2979           break;
2980         }
2981     }
2982 }
2983
2984 static void
2985 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
2986                                GtkIconViewItem *item)
2987 {
2988   gint focus_width;
2989   GdkRectangle rect;
2990
2991   gtk_widget_style_get (GTK_WIDGET (icon_view),
2992                         "focus-line-width", &focus_width,
2993                         NULL);
2994
2995   rect.x = item->x - focus_width;
2996   rect.y = item->y - focus_width;
2997   rect.width = item->width + 2 * focus_width;
2998   rect.height = item->height + 2 * focus_width;
2999
3000   if (icon_view->priv->bin_window)
3001     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
3002 }
3003
3004 static gboolean
3005 layout_callback (gpointer user_data)
3006 {
3007   GtkIconView *icon_view;
3008
3009   GDK_THREADS_ENTER ();
3010
3011   icon_view = GTK_ICON_VIEW (user_data);
3012   
3013   icon_view->priv->layout_idle_id = 0;
3014
3015   gtk_icon_view_layout (icon_view);
3016   
3017   GDK_THREADS_LEAVE();
3018
3019   return FALSE;
3020 }
3021
3022 static void
3023 gtk_icon_view_queue_layout (GtkIconView *icon_view)
3024 {
3025   if (icon_view->priv->layout_idle_id != 0)
3026     return;
3027
3028   icon_view->priv->layout_idle_id = g_idle_add (layout_callback, icon_view);
3029 }
3030
3031 static void
3032 gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
3033                                GtkIconViewItem *item,
3034                                gint             cursor_cell)
3035 {
3036   AtkObject *obj;
3037   AtkObject *item_obj;
3038
3039   if (icon_view->priv->cursor_item == item &&
3040       (cursor_cell < 0 || cursor_cell == icon_view->priv->cursor_cell))
3041     return;
3042
3043   if (icon_view->priv->cursor_item != NULL)
3044     gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
3045   
3046   icon_view->priv->cursor_item = item;
3047   if (cursor_cell >= 0)
3048     icon_view->priv->cursor_cell = cursor_cell;
3049
3050   gtk_icon_view_queue_draw_item (icon_view, item);
3051   
3052   /* Notify that accessible focus object has changed */
3053   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
3054   item_obj = atk_object_ref_accessible_child (obj, item->index);
3055
3056   if (item_obj != NULL)
3057     {
3058       atk_focus_tracker_notify (item_obj);
3059       g_object_unref (item_obj); 
3060     }
3061 }
3062
3063
3064 static GtkIconViewItem *
3065 gtk_icon_view_item_new (void)
3066 {
3067   GtkIconViewItem *item;
3068
3069   item = g_new0 (GtkIconViewItem, 1);
3070
3071   item->width = -1;
3072   item->height = -1;
3073   
3074   return item;
3075 }
3076
3077 static void
3078 gtk_icon_view_item_free (GtkIconViewItem *item)
3079 {
3080   g_return_if_fail (item != NULL);
3081
3082   g_free (item->before);
3083   g_free (item->after);
3084   g_free (item->box);
3085
3086   g_free (item);
3087 }
3088
3089
3090 static GtkIconViewItem *
3091 gtk_icon_view_get_item_at_coords (GtkIconView          *icon_view,
3092                                   gint                  x,
3093                                   gint                  y,
3094                                   gboolean              only_in_cell,
3095                                   GtkIconViewCellInfo **cell_at_pos)
3096 {
3097   GList *items, *l;
3098   GdkRectangle box;
3099
3100   for (items = icon_view->priv->items; items; items = items->next)
3101     {
3102       GtkIconViewItem *item = items->data;
3103
3104       if (x >= item->x - icon_view->priv->column_spacing/2 && x <= item->x + item->width + icon_view->priv->column_spacing/2 &&
3105           y >= item->y - icon_view->priv->row_spacing/2 && y <= item->y + item->height + icon_view->priv->row_spacing/2)
3106         {
3107           if (only_in_cell || cell_at_pos)
3108             {
3109               gtk_icon_view_set_cell_data (icon_view, item);
3110
3111               for (l = icon_view->priv->cell_list; l; l = l->next)
3112                 {
3113                   GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3114                   
3115                   if (!info->cell->visible)
3116                     continue;
3117                   
3118                   gtk_icon_view_get_cell_box (icon_view, item, info, &box);
3119                   
3120                   if ((x >= box.x && x <= box.x + box.width &&
3121                        y >= box.y && y <= box.y + box.height) ||
3122                       (x >= box.x  &&
3123                        x <= box.x + box.width &&
3124                        y >= box.y &&
3125                        y <= box.y + box.height))
3126                     {
3127                       if (cell_at_pos)
3128                         *cell_at_pos = info;
3129                       
3130                       return item;
3131                     }
3132                 }
3133
3134               if (only_in_cell)
3135                 return NULL;
3136               
3137               if (cell_at_pos)
3138                 *cell_at_pos = NULL;
3139             }
3140
3141           return item;
3142         }
3143     }
3144
3145   return NULL;
3146 }
3147
3148 static void
3149 gtk_icon_view_select_item (GtkIconView      *icon_view,
3150                            GtkIconViewItem  *item)
3151 {
3152   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3153   g_return_if_fail (item != NULL);
3154
3155   if (item->selected)
3156     return;
3157   
3158   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
3159     return;
3160   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3161     gtk_icon_view_unselect_all_internal (icon_view);
3162
3163   item->selected = TRUE;
3164
3165   gtk_icon_view_queue_draw_item (icon_view, item);
3166
3167   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3168 }
3169
3170
3171 static void
3172 gtk_icon_view_unselect_item (GtkIconView      *icon_view,
3173                              GtkIconViewItem  *item)
3174 {
3175   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3176   g_return_if_fail (item != NULL);
3177
3178   if (!item->selected)
3179     return;
3180   
3181   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
3182       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
3183     return;
3184   
3185   item->selected = FALSE;
3186
3187   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3188
3189   gtk_icon_view_queue_draw_item (icon_view, item);
3190 }
3191
3192 static void
3193 verify_items (GtkIconView *icon_view)
3194 {
3195   GList *items;
3196   int i = 0;
3197
3198   for (items = icon_view->priv->items; items; items = items->next)
3199     {
3200       GtkIconViewItem *item = items->data;
3201
3202       if (item->index != i)
3203         g_error ("List item does not match its index: "
3204                  "item index %d and list index %d\n", item->index, i);
3205
3206       i++;
3207     }
3208 }
3209
3210 static void
3211 gtk_icon_view_row_changed (GtkTreeModel *model,
3212                            GtkTreePath  *path,
3213                            GtkTreeIter  *iter,
3214                            gpointer      data)
3215 {
3216   GtkIconViewItem *item;
3217   gint index;
3218   GtkIconView *icon_view;
3219
3220   icon_view = GTK_ICON_VIEW (data);
3221
3222   gtk_icon_view_stop_editing (icon_view, TRUE);
3223   
3224   index = gtk_tree_path_get_indices(path)[0];
3225   item = g_list_nth_data (icon_view->priv->items, index);
3226
3227   gtk_icon_view_item_invalidate_size (item);
3228   gtk_icon_view_queue_layout (icon_view);
3229
3230   verify_items (icon_view);
3231 }
3232
3233 static void
3234 gtk_icon_view_row_inserted (GtkTreeModel *model,
3235                             GtkTreePath  *path,
3236                             GtkTreeIter  *iter,
3237                             gpointer      data)
3238 {
3239   gint index;
3240   GtkIconViewItem *item;
3241   gboolean iters_persist;
3242   GtkIconView *icon_view;
3243   GList *list;
3244   
3245   icon_view = GTK_ICON_VIEW (data);
3246
3247   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3248   
3249   index = gtk_tree_path_get_indices(path)[0];
3250
3251   item = gtk_icon_view_item_new ();
3252
3253   if (iters_persist)
3254     item->iter = *iter;
3255
3256   item->index = index;
3257
3258   /* FIXME: We can be more efficient here,
3259      we can store a tail pointer and use that when
3260      appending (which is a rather common operation)
3261   */
3262   icon_view->priv->items = g_list_insert (icon_view->priv->items,
3263                                          item, index);
3264   
3265   list = g_list_nth (icon_view->priv->items, index + 1);
3266   for (; list; list = list->next)
3267     {
3268       item = list->data;
3269
3270       item->index++;
3271     }
3272     
3273   verify_items (icon_view);
3274 }
3275
3276 static void
3277 gtk_icon_view_row_deleted (GtkTreeModel *model,
3278                            GtkTreePath  *path,
3279                            gpointer      data)
3280 {
3281   gint index;
3282   GtkIconView *icon_view;
3283   GtkIconViewItem *item;
3284   GList *list, *next;
3285   gboolean emit = FALSE;
3286   
3287   icon_view = GTK_ICON_VIEW (data);
3288
3289   index = gtk_tree_path_get_indices(path)[0];
3290
3291   list = g_list_nth (icon_view->priv->items, index);
3292   item = list->data;
3293
3294   gtk_icon_view_stop_editing (icon_view, TRUE);
3295
3296   if (item == icon_view->priv->anchor_item)
3297     icon_view->priv->anchor_item = NULL;
3298
3299   if (item == icon_view->priv->cursor_item)
3300     icon_view->priv->cursor_item = NULL;
3301
3302   if (item->selected)
3303     emit = TRUE;
3304   
3305   gtk_icon_view_item_free (item);
3306
3307   for (next = list->next; next; next = next->next)
3308     {
3309       item = next->data;
3310
3311       item->index--;
3312     }
3313   
3314   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
3315
3316   gtk_icon_view_queue_layout (icon_view);
3317
3318   verify_items (icon_view);  
3319   
3320   if (emit)
3321     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3322 }
3323
3324 static void
3325 gtk_icon_view_rows_reordered (GtkTreeModel *model,
3326                               GtkTreePath  *parent,
3327                               GtkTreeIter  *iter,
3328                               gint         *new_order,
3329                               gpointer      data)
3330 {
3331   int i;
3332   int length;
3333   GtkIconView *icon_view;
3334   GList *items = NULL, *list;
3335   GtkIconViewItem **item_array;
3336   gint *order;
3337   
3338   icon_view = GTK_ICON_VIEW (data);
3339
3340   gtk_icon_view_stop_editing (icon_view, TRUE);
3341
3342   length = gtk_tree_model_iter_n_children (model, NULL);
3343
3344   order = g_new (gint, length);
3345   for (i = 0; i < length; i++)
3346     order [new_order[i]] = i;
3347
3348   item_array = g_new (GtkIconViewItem *, length);
3349   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
3350     item_array[order[i]] = list->data;
3351   g_free (order);
3352
3353   for (i = length - 1; i >= 0; i--)
3354     {
3355       item_array[i]->index = i;
3356       items = g_list_prepend (items, item_array[i]);
3357     }
3358   
3359   g_free (item_array);
3360   g_list_free (icon_view->priv->items);
3361   icon_view->priv->items = items;
3362
3363   gtk_icon_view_queue_layout (icon_view);
3364
3365   verify_items (icon_view);  
3366 }
3367
3368 static void
3369 gtk_icon_view_build_items (GtkIconView *icon_view)
3370 {
3371   GtkTreeIter iter;
3372   int i;
3373   gboolean iters_persist;
3374   GList *items = NULL;
3375
3376   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
3377   
3378   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
3379                                       &iter))
3380     return;
3381
3382   i = 0;
3383   
3384   do
3385     {
3386       GtkIconViewItem *item = gtk_icon_view_item_new ();
3387
3388       if (iters_persist)
3389         item->iter = iter;
3390
3391       item->index = i;
3392       
3393       i++;
3394
3395       items = g_list_prepend (items, item);
3396       
3397     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
3398
3399   icon_view->priv->items = g_list_reverse (items);
3400 }
3401
3402 static void
3403 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
3404                                 guint           keyval,
3405                                 guint           modmask,
3406                                 GtkMovementStep step,
3407                                 gint            count)
3408 {
3409   
3410   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
3411                                 I_("move_cursor"), 2,
3412                                 G_TYPE_ENUM, step,
3413                                 G_TYPE_INT, count);
3414
3415   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
3416                                 "move_cursor", 2,
3417                                 G_TYPE_ENUM, step,
3418                                 G_TYPE_INT, count);
3419
3420   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3421    return;
3422
3423   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
3424                                 "move_cursor", 2,
3425                                 G_TYPE_ENUM, step,
3426                                 G_TYPE_INT, count);
3427
3428   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
3429                                 "move_cursor", 2,
3430                                 G_TYPE_ENUM, step,
3431                                 G_TYPE_INT, count);
3432 }
3433
3434 static gboolean
3435 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
3436                                 GtkMovementStep  step,
3437                                 gint             count)
3438 {
3439   GdkModifierType state;
3440
3441   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
3442   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
3443                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
3444                         step == GTK_MOVEMENT_DISPLAY_LINES ||
3445                         step == GTK_MOVEMENT_PAGES ||
3446                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
3447
3448   if (!GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (icon_view)))
3449     return FALSE;
3450
3451   gtk_icon_view_stop_editing (icon_view, FALSE);
3452   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
3453
3454   if (gtk_get_current_event_state (&state))
3455     {
3456       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
3457         icon_view->priv->ctrl_pressed = TRUE;
3458       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
3459         icon_view->priv->shift_pressed = TRUE;
3460     }
3461   /* else we assume not pressed */
3462
3463   switch (step)
3464     {
3465     case GTK_MOVEMENT_LOGICAL_POSITIONS:
3466     case GTK_MOVEMENT_VISUAL_POSITIONS:
3467       gtk_icon_view_move_cursor_left_right (icon_view, count);
3468       break;
3469     case GTK_MOVEMENT_DISPLAY_LINES:
3470       gtk_icon_view_move_cursor_up_down (icon_view, count);
3471       break;
3472     case GTK_MOVEMENT_PAGES:
3473       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
3474       break;
3475     case GTK_MOVEMENT_BUFFER_ENDS:
3476       gtk_icon_view_move_cursor_start_end (icon_view, count);
3477       break;
3478     default:
3479       g_assert_not_reached ();
3480     }
3481
3482   icon_view->priv->ctrl_pressed = FALSE;
3483   icon_view->priv->shift_pressed = FALSE;
3484
3485   return TRUE;
3486 }
3487
3488 static GtkIconViewItem *
3489 find_item (GtkIconView     *icon_view,
3490            GtkIconViewItem *current,
3491            gint             row_ofs,
3492            gint             col_ofs)
3493 {
3494   gint row, col;
3495   GList *items;
3496   GtkIconViewItem *item;
3497
3498   /* FIXME: this could be more efficient 
3499    */
3500   row = current->row + row_ofs;
3501   col = current->col + col_ofs;
3502
3503   for (items = icon_view->priv->items; items; items = items->next)
3504     {
3505       item = items->data;
3506       if (item->row == row && item->col == col)
3507         return item;
3508     }
3509   
3510   return NULL;
3511 }
3512
3513 static gint
3514 find_cell (GtkIconView     *icon_view,
3515            GtkIconViewItem *item,
3516            gint             cell,
3517            GtkOrientation   orientation,
3518            gint             step,
3519            gint            *count)
3520 {
3521   gint n_focusable;
3522   gint *focusable;
3523   gint first_text;
3524   gint current;
3525   gint i, k;
3526   GList *l;
3527
3528   if (icon_view->priv->orientation != orientation)
3529     return cell;
3530
3531   gtk_icon_view_set_cell_data (icon_view, item);
3532
3533   focusable = g_new0 (gint, icon_view->priv->n_cells);
3534   n_focusable = 0;
3535
3536   first_text = 0;
3537   current = 0;
3538   for (k = 0; k < 2; k++)
3539     for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
3540       {
3541         GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)l->data;
3542         
3543         if (info->pack == (k ? GTK_PACK_START : GTK_PACK_END))
3544           continue;
3545         
3546         if (!info->cell->visible)
3547           continue;
3548         
3549         if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
3550           first_text = i;
3551
3552         if (info->cell->mode != GTK_CELL_RENDERER_MODE_INERT)
3553           {
3554             if (cell == i)
3555               current = n_focusable;
3556
3557             focusable[n_focusable] = i;
3558
3559             n_focusable++;
3560           }
3561       }
3562   
3563   if (n_focusable == 0)
3564     focusable[n_focusable++] = first_text;
3565
3566   if (cell < 0)
3567     {
3568       current = step > 0 ? 0 : n_focusable - 1;
3569       cell = focusable[current];
3570     }
3571
3572   if (current + *count < 0)
3573     {
3574       cell = -1;
3575       *count = current + *count;
3576     }
3577   else if (current + *count > n_focusable - 1)
3578     {
3579       cell = -1;
3580       *count = current + *count - (n_focusable - 1);
3581     }
3582   else
3583     {
3584       cell = focusable[current + *count];
3585       *count = 0;
3586     }
3587   
3588   g_free (focusable);
3589   
3590   return cell;
3591 }
3592
3593 static GtkIconViewItem *
3594 find_item_page_up_down (GtkIconView     *icon_view,
3595                         GtkIconViewItem *current,
3596                         gint             count)
3597 {
3598   GList *item, *next;
3599   gint y, col;
3600   
3601   col = current->col;
3602   y = current->y + count * icon_view->priv->vadjustment->page_size;
3603
3604   item = g_list_find (icon_view->priv->items, current);
3605   if (count > 0)
3606     {
3607       while (item)
3608         {
3609           for (next = item->next; next; next = next->next)
3610             {
3611               if (((GtkIconViewItem *)next->data)->col == col)
3612                 break;
3613             }
3614           if (!next || ((GtkIconViewItem *)next->data)->y > y)
3615             break;
3616
3617           item = next;
3618         }
3619     }
3620   else 
3621     {
3622       while (item)
3623         {
3624           for (next = item->prev; next; next = next->prev)
3625             {
3626               if (((GtkIconViewItem *)next->data)->col == col)
3627                 break;
3628             }
3629           if (!next || ((GtkIconViewItem *)next->data)->y < y)
3630             break;
3631
3632           item = next;
3633         }
3634     }
3635
3636   if (item)
3637     return item->data;
3638
3639   return NULL;
3640 }
3641
3642 static gboolean
3643 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
3644                                   GtkIconViewItem *anchor,
3645                                   GtkIconViewItem *cursor)
3646 {
3647   GList *items;
3648   GtkIconViewItem *item;
3649   gint row1, row2, col1, col2;
3650   gboolean dirty = FALSE;
3651   
3652   if (anchor->row < cursor->row)
3653     {
3654       row1 = anchor->row;
3655       row2 = cursor->row;
3656     }
3657   else
3658     {
3659       row1 = cursor->row;
3660       row2 = anchor->row;
3661     }
3662
3663   if (anchor->col < cursor->col)
3664     {
3665       col1 = anchor->col;
3666       col2 = cursor->col;
3667     }
3668   else
3669     {
3670       col1 = cursor->col;
3671       col2 = anchor->col;
3672     }
3673
3674   for (items = icon_view->priv->items; items; items = items->next)
3675     {
3676       item = items->data;
3677
3678       if (row1 <= item->row && item->row <= row2 &&
3679           col1 <= item->col && item->col <= col2)
3680         {
3681           if (!item->selected)
3682             dirty = TRUE;
3683
3684           item->selected = TRUE;
3685           
3686           gtk_icon_view_queue_draw_item (icon_view, item);
3687         }
3688     }
3689
3690   return dirty;
3691 }
3692
3693 static void 
3694 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
3695                                    gint         count)
3696 {
3697   GtkIconViewItem *item;
3698   gint cell;
3699   gboolean dirty = FALSE;
3700   gint step;
3701   
3702   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3703     return;
3704   
3705   if (!icon_view->priv->cursor_item)
3706     {
3707       GList *list;
3708
3709       if (count > 0)
3710         list = icon_view->priv->items;
3711       else
3712         list = g_list_last (icon_view->priv->items);
3713
3714       item = list ? list->data : NULL;
3715       cell = -1;
3716     }
3717   else
3718     {
3719       item = icon_view->priv->cursor_item;
3720       cell = icon_view->priv->cursor_cell;
3721       step = count > 0 ? 1 : -1;      
3722       while (item)
3723         {
3724           cell = find_cell (icon_view, item, cell,
3725                             GTK_ORIENTATION_VERTICAL, 
3726                             step, &count);
3727           if (count == 0)
3728             break;
3729
3730           item = find_item (icon_view, item, step, 0);
3731           count = count - step;
3732         }
3733     }
3734
3735   if (!item)
3736     return;
3737
3738   if (icon_view->priv->ctrl_pressed ||
3739       !icon_view->priv->shift_pressed ||
3740       !icon_view->priv->anchor_item ||
3741       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3742     icon_view->priv->anchor_item = item;
3743
3744   gtk_icon_view_set_cursor_item (icon_view, item, cell);
3745
3746   if (!icon_view->priv->ctrl_pressed &&
3747       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3748     {
3749       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3750       dirty = gtk_icon_view_select_all_between (icon_view, 
3751                                                 icon_view->priv->anchor_item,
3752                                                 item) || dirty;
3753     }
3754
3755   gtk_icon_view_scroll_to_item (icon_view, item);
3756
3757   if (dirty)
3758     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3759 }
3760
3761 static void 
3762 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
3763                                         gint         count)
3764 {
3765   GtkIconViewItem *item;
3766   gboolean dirty = FALSE;
3767   
3768   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3769     return;
3770   
3771   if (!icon_view->priv->cursor_item)
3772     {
3773       GList *list;
3774
3775       if (count > 0)
3776         list = icon_view->priv->items;
3777       else
3778         list = g_list_last (icon_view->priv->items);
3779
3780       item = list ? list->data : NULL;
3781     }
3782   else
3783     item = find_item_page_up_down (icon_view, 
3784                                    icon_view->priv->cursor_item,
3785                                    count);
3786
3787   if (!item)
3788     return;
3789
3790   if (icon_view->priv->ctrl_pressed ||
3791       !icon_view->priv->shift_pressed ||
3792       !icon_view->priv->anchor_item ||
3793       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3794     icon_view->priv->anchor_item = item;
3795
3796   gtk_icon_view_set_cursor_item (icon_view, item, -1);
3797
3798   if (!icon_view->priv->ctrl_pressed &&
3799       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3800     {
3801       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3802       dirty = gtk_icon_view_select_all_between (icon_view, 
3803                                                 icon_view->priv->anchor_item,
3804                                                 item) || dirty;
3805     }
3806
3807   gtk_icon_view_scroll_to_item (icon_view, item);
3808
3809   if (dirty)
3810     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);  
3811 }
3812
3813 static void 
3814 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
3815                                       gint         count)
3816 {
3817   GtkIconViewItem *item;
3818   gint cell = -1;
3819   gboolean dirty = FALSE;
3820   gint step;
3821   
3822   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3823     return;
3824   
3825   if (!icon_view->priv->cursor_item)
3826     {
3827       GList *list;
3828
3829       if (count > 0)
3830         list = icon_view->priv->items;
3831       else
3832         list = g_list_last (icon_view->priv->items);
3833
3834       item = list ? list->data : NULL;
3835     }
3836   else
3837     {
3838       item = icon_view->priv->cursor_item;
3839       cell = icon_view->priv->cursor_cell;
3840       step = count > 0 ? 1 : -1;
3841       while (item)
3842         {
3843           cell = find_cell (icon_view, item, cell,
3844                             GTK_ORIENTATION_HORIZONTAL, 
3845                             step, &count);
3846           if (count == 0)
3847             break;
3848           
3849           item = find_item (icon_view, item, 0, step);
3850           count = count - step;
3851         }
3852     }
3853
3854   if (!item)
3855     return;
3856
3857   if (icon_view->priv->ctrl_pressed ||
3858       !icon_view->priv->shift_pressed ||
3859       !icon_view->priv->anchor_item ||
3860       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3861     icon_view->priv->anchor_item = item;
3862
3863   gtk_icon_view_set_cursor_item (icon_view, item, cell);
3864
3865   if (!icon_view->priv->ctrl_pressed &&
3866       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3867     {
3868       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3869       dirty = gtk_icon_view_select_all_between (icon_view, 
3870                                                 icon_view->priv->anchor_item,
3871                                                 item) || dirty;
3872     }
3873
3874   gtk_icon_view_scroll_to_item (icon_view, item);
3875
3876   if (dirty)
3877     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3878 }
3879
3880 static void 
3881 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
3882                                      gint         count)
3883 {
3884   GtkIconViewItem *item;
3885   GList *list;
3886   gboolean dirty = FALSE;
3887   
3888   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
3889     return;
3890   
3891   if (count < 0)
3892     list = icon_view->priv->items;
3893   else
3894     list = g_list_last (icon_view->priv->items);
3895   
3896   item = list ? list->data : NULL;
3897
3898   if (!item)
3899     return;
3900
3901   if (icon_view->priv->ctrl_pressed ||
3902       !icon_view->priv->shift_pressed ||
3903       !icon_view->priv->anchor_item ||
3904       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3905     icon_view->priv->anchor_item = item;
3906
3907   gtk_icon_view_set_cursor_item (icon_view, item, -1);
3908
3909   if (!icon_view->priv->ctrl_pressed &&
3910       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
3911     {
3912       dirty = gtk_icon_view_unselect_all_internal (icon_view);
3913       dirty = gtk_icon_view_select_all_between (icon_view, 
3914                                                 icon_view->priv->anchor_item,
3915                                                 item) || dirty;
3916     }
3917
3918   gtk_icon_view_scroll_to_item (icon_view, item);
3919
3920   if (dirty)
3921     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3922 }
3923
3924 /**
3925  * gtk_icon_view_scroll_to_path:
3926  * @icon_view: A #GtkIconView.
3927  * @path: The path of the item to move to.
3928  * @use_align: whether to use alignment arguments, or %FALSE.
3929  * @row_align: The vertical alignment of the item specified by @path.
3930  * @col_align: The horizontal alignment of the item specified by @path.
3931  *
3932  * Moves the alignments of @icon_view to the position specified by @path.  
3933  * @row_align determines where the row is placed, and @col_align determines where 
3934  * @column is placed.  Both are expected to be between 0.0 and 1.0. 
3935  * 0.0 means left/top alignment, 1.0 means right/bottom alignment, 0.5 means center.
3936  *
3937  * If @use_align is %FALSE, then the alignment arguments are ignored, and the
3938  * tree does the minimum amount of work to scroll the item onto the screen.
3939  * This means that the item will be scrolled to the edge closest to its current
3940  * position.  If the item is currently visible on the screen, nothing is done.
3941  *
3942  * This function only works if the model is set, and @path is a valid row on the
3943  * model.  If the model changes before the @icon_view is realized, the centered
3944  * path will be modified to reflect this change.
3945  *
3946  * Since: 2.8
3947  **/
3948 void
3949 gtk_icon_view_scroll_to_path (GtkIconView *icon_view,
3950                               GtkTreePath *path,
3951                               gboolean     use_align,
3952                               gfloat       row_align,
3953                               gfloat       col_align)
3954 {
3955   GtkIconViewItem *item = NULL;
3956
3957   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3958   g_return_if_fail (path != NULL);
3959   g_return_if_fail (row_align >= 0.0 && row_align <= 1.0);
3960   g_return_if_fail (col_align >= 0.0 && col_align <= 1.0);
3961
3962   if (! GTK_WIDGET_REALIZED (icon_view))
3963     {
3964       if (icon_view->priv->scroll_to_path)
3965         gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
3966
3967       icon_view->priv->scroll_to_path = NULL;
3968
3969       if (path)
3970         icon_view->priv->scroll_to_path = gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), icon_view->priv->model, path);
3971
3972       icon_view->priv->scroll_to_use_align = use_align;
3973       icon_view->priv->scroll_to_row_align = row_align;
3974       icon_view->priv->scroll_to_col_align = col_align;
3975
3976       return;
3977     }
3978
3979   if (gtk_tree_path_get_depth (path) > 0)
3980     item = g_list_nth_data (icon_view->priv->items,
3981                             gtk_tree_path_get_indices(path)[0]);
3982   
3983   if (!item)
3984     return;
3985
3986   if (use_align)
3987     {
3988       gint x, y;
3989       gint focus_width;
3990       gfloat offset, value;
3991
3992       gtk_widget_style_get (GTK_WIDGET (icon_view),
3993                             "focus-line-width", &focus_width,
3994                             NULL);
3995       
3996       gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
3997       
3998       offset =  y + item->y - focus_width - 
3999         row_align * (GTK_WIDGET (icon_view)->allocation.height - item->height);
4000       value = CLAMP (icon_view->priv->vadjustment->value + offset, 
4001                      icon_view->priv->vadjustment->lower,
4002                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
4003       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
4004
4005       offset = x + item->x - focus_width - 
4006         col_align * (GTK_WIDGET (icon_view)->allocation.width - item->width);
4007       value = CLAMP (icon_view->priv->hadjustment->value + offset, 
4008                      icon_view->priv->hadjustment->lower,
4009                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
4010       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
4011
4012       gtk_adjustment_changed (icon_view->priv->hadjustment);
4013       gtk_adjustment_changed (icon_view->priv->vadjustment);
4014     }
4015   else
4016     gtk_icon_view_scroll_to_item (icon_view, item);
4017 }
4018
4019
4020 static void     
4021 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view, 
4022                               GtkIconViewItem *item)
4023 {
4024   gint x, y, width, height;
4025   gint focus_width;
4026
4027   gtk_widget_style_get (GTK_WIDGET (icon_view),
4028                         "focus-line-width", &focus_width,
4029                         NULL);
4030
4031   gdk_drawable_get_size (GDK_DRAWABLE (icon_view->priv->bin_window), 
4032                          &width, &height);
4033   gdk_window_get_position (icon_view->priv->bin_window, &x, &y);
4034
4035   if (y + item->y - focus_width < 0)
4036     gtk_adjustment_set_value (icon_view->priv->vadjustment, 
4037                               icon_view->priv->vadjustment->value + y + item->y - focus_width);
4038   else if (y + item->y + item->height + focus_width > GTK_WIDGET (icon_view)->allocation.height)
4039     gtk_adjustment_set_value (icon_view->priv->vadjustment, 
4040                               icon_view->priv->vadjustment->value + y + item->y + item->height 
4041                               + focus_width - GTK_WIDGET (icon_view)->allocation.height);
4042
4043   if (x + item->x - focus_width < 0)
4044     gtk_adjustment_set_value (icon_view->priv->hadjustment, 
4045                               icon_view->priv->hadjustment->value + x + item->x - focus_width);
4046   else if (x + item->x + item->width + focus_width > GTK_WIDGET (icon_view)->allocation.width)
4047     gtk_adjustment_set_value (icon_view->priv->hadjustment, 
4048                               icon_view->priv->hadjustment->value + x + item->x + item->width 
4049                               + focus_width - GTK_WIDGET (icon_view)->allocation.width);
4050
4051   gtk_adjustment_changed (icon_view->priv->hadjustment);
4052   gtk_adjustment_changed (icon_view->priv->vadjustment);
4053 }
4054
4055 /* GtkCellLayout implementation */
4056 static GtkIconViewCellInfo *
4057 gtk_icon_view_get_cell_info (GtkIconView     *icon_view,
4058                              GtkCellRenderer *renderer)
4059 {
4060   GList *i;
4061
4062   for (i = icon_view->priv->cell_list; i; i = i->next)
4063     {
4064       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)i->data;
4065
4066       if (info->cell == renderer)
4067         return info;
4068     }
4069
4070   return NULL;
4071 }
4072
4073 static void
4074 gtk_icon_view_set_cell_data (GtkIconView     *icon_view, 
4075                              GtkIconViewItem *item)
4076 {
4077   GList *i;
4078   gboolean iters_persist;
4079   GtkTreeIter iter;
4080   
4081   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
4082   
4083   if (!iters_persist)
4084     {
4085       GtkTreePath *path;
4086
4087       path = gtk_tree_path_new_from_indices (item->index, -1);
4088       gtk_tree_model_get_iter (icon_view->priv->model, &iter, path);
4089       gtk_tree_path_free (path);
4090     }
4091   else
4092     iter = item->iter;
4093   
4094   for (i = icon_view->priv->cell_list; i; i = i->next)
4095     {
4096       GSList *j;
4097       GtkIconViewCellInfo *info = i->data;
4098
4099       g_object_freeze_notify (G_OBJECT (info->cell));
4100
4101       for (j = info->attributes; j && j->next; j = j->next->next)
4102         {
4103           gchar *property = j->data;
4104           gint column = GPOINTER_TO_INT (j->next->data);
4105           GValue value = {0, };
4106
4107           gtk_tree_model_get_value (icon_view->priv->model, &iter,
4108                                     column, &value);
4109           g_object_set_property (G_OBJECT (info->cell),
4110                                  property, &value);
4111           g_value_unset (&value);
4112         }
4113
4114       if (info->func)
4115         (* info->func) (GTK_CELL_LAYOUT (icon_view),
4116                         info->cell,
4117                         icon_view->priv->model,
4118                         &iter,
4119                         info->func_data);
4120       
4121       g_object_thaw_notify (G_OBJECT (info->cell));
4122     }  
4123   
4124   adjust_wrap_width (icon_view, item);
4125 }
4126
4127 static void 
4128 free_cell_attributes (GtkIconViewCellInfo *info)
4129
4130   GSList *list;
4131
4132   list = info->attributes;
4133   while (list && list->next)
4134     {
4135       g_free (list->data);
4136       list = list->next->next;
4137     }
4138   
4139   g_slist_free (info->attributes);
4140   info->attributes = NULL;
4141 }
4142
4143 static void
4144 free_cell_info (GtkIconViewCellInfo *info)
4145 {
4146   free_cell_attributes (info);
4147
4148   g_object_unref (info->cell);
4149   
4150   if (info->destroy)
4151     (* info->destroy) (info->func_data);
4152
4153   g_free (info);
4154 }
4155
4156 static void
4157 gtk_icon_view_cell_layout_pack_start (GtkCellLayout   *layout,
4158                                       GtkCellRenderer *renderer,
4159                                       gboolean         expand)
4160 {
4161   GtkIconViewCellInfo *info;
4162   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4163
4164   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
4165   g_return_if_fail (!gtk_icon_view_get_cell_info (icon_view, renderer));
4166
4167   g_object_ref_sink (renderer);
4168
4169   info = g_new0 (GtkIconViewCellInfo, 1);
4170   info->cell = renderer;
4171   info->expand = expand ? TRUE : FALSE;
4172   info->pack = GTK_PACK_START;
4173   info->position = icon_view->priv->n_cells;
4174   
4175   icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
4176   icon_view->priv->n_cells++;
4177 }
4178
4179 static void
4180 gtk_icon_view_cell_layout_pack_end (GtkCellLayout   *layout,
4181                                     GtkCellRenderer *renderer,
4182                                     gboolean         expand)
4183 {
4184   GtkIconViewCellInfo *info;
4185   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4186
4187   g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
4188   g_return_if_fail (!gtk_icon_view_get_cell_info (icon_view, renderer));
4189
4190   g_object_ref_sink (renderer);
4191
4192   info = g_new0 (GtkIconViewCellInfo, 1);
4193   info->cell = renderer;
4194   info->expand = expand ? TRUE : FALSE;
4195   info->pack = GTK_PACK_END;
4196   info->position = icon_view->priv->n_cells;
4197
4198   icon_view->priv->cell_list = g_list_append (icon_view->priv->cell_list, info);
4199   icon_view->priv->n_cells++;
4200 }
4201
4202 static void
4203 gtk_icon_view_cell_layout_add_attribute (GtkCellLayout   *layout,
4204                                          GtkCellRenderer *renderer,
4205                                          const gchar     *attribute,
4206                                          gint             column)
4207 {
4208   GtkIconViewCellInfo *info;
4209   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4210
4211   info = gtk_icon_view_get_cell_info (icon_view, renderer);
4212   g_return_if_fail (info != NULL);
4213
4214   info->attributes = g_slist_prepend (info->attributes,
4215                                       GINT_TO_POINTER (column));
4216   info->attributes = g_slist_prepend (info->attributes,
4217                                       g_strdup (attribute));
4218 }
4219
4220 static void
4221 gtk_icon_view_cell_layout_clear (GtkCellLayout *layout)
4222 {
4223   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4224
4225   while (icon_view->priv->cell_list)
4226     {
4227       GtkIconViewCellInfo *info = (GtkIconViewCellInfo *)icon_view->priv->cell_list->data;
4228       free_cell_info (info);
4229       icon_view->priv->cell_list = g_list_delete_link (icon_view->priv->cell_list, 
4230                                                        icon_view->priv->cell_list);
4231     }
4232
4233   icon_view->priv->n_cells = 0;
4234 }
4235
4236 static void
4237 gtk_icon_view_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
4238                                               GtkCellRenderer       *cell,
4239                                               GtkCellLayoutDataFunc  func,
4240                                               gpointer               func_data,
4241                                               GDestroyNotify         destroy)
4242 {
4243   GtkIconViewCellInfo *info;
4244   GtkIconView *icon_view = GTK_ICON_VIEW (layout);
4245
4246   info = gtk_icon_view_get_cell_info (icon_view, cell);
4247   g_return_if_fail (info != NULL);
4248
4249   if (info->destroy)
4250     {
4251       GDestroyNotify d = info->destroy;
4252
4253       info->destroy = NULL;
4254       d (info->func_data);
4255     }
4256
4257   info->func = func;
4258   info->func_data = func_data;
4259   info->destroy = destroy;
4260 }
4261
4262 static void
4263 gtk_icon_view_cell_layout_clear_attributes (GtkCellLayout   *layout,
4264                                             GtkCellRenderer *renderer)
4265 {
4266   GtkIconViewCellInfo *info;
4267
4268   info = gtk_icon_view_get_cell_info (GTK_ICON_VIEW (layout), renderer);
4269   if (info != NULL)
4270     free_cell_attributes (info);
4271 }
4272
4273 static void
4274 gtk_icon_view_cell_layout_reorder (GtkCellLayout   *layout,
4275                                    GtkCellRenderer *cell,
4276                                    gint             position)
4277 {
4278   GtkIconView *icon_view;
4279   GList *link, *l;
4280   GtkIconViewCellInfo *info;
4281   gint i;
4282
4283   icon_view = GTK_ICON_VIEW (layout);
4284
4285   g_return_if_fail (GTK_IS_CELL_RENDERER (cell));
4286
4287   info = gtk_icon_view_get_cell_info (icon_view, cell);
4288
4289   g_return_if_fail (info != NULL);
4290   g_return_if_fail (position >= 0);
4291
4292   link = g_list_find (icon_view->priv->cell_list, info);
4293
4294   g_return_if_fail (link != NULL);
4295
4296   icon_view->priv->cell_list = g_list_remove_link (icon_view->priv->cell_list,
4297                                                   link);
4298   icon_view->priv->cell_list = g_list_insert (icon_view->priv->cell_list,
4299                                              info, position);
4300
4301   for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4302     {
4303       info = l->data;
4304
4305       info->position = i;
4306     }
4307
4308   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
4309 }
4310
4311 /* Public API */
4312
4313
4314 /**
4315  * gtk_icon_view_new:
4316  * 
4317  * Creates a new #GtkIconView widget
4318  * 
4319  * Return value: A newly created #GtkIconView widget
4320  *
4321  * Since: 2.6
4322  **/
4323 GtkWidget *
4324 gtk_icon_view_new (void)
4325 {
4326   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
4327 }
4328
4329 /**
4330  * gtk_icon_view_new_with_model:
4331  * @model: The model.
4332  * 
4333  * Creates a new #GtkIconView widget with the model @model.
4334  * 
4335  * Return value: A newly created #GtkIconView widget.
4336  *
4337  * Since: 2.6 
4338  **/
4339 GtkWidget *
4340 gtk_icon_view_new_with_model (GtkTreeModel *model)
4341 {
4342   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
4343 }
4344
4345
4346 /**
4347  * gtk_icon_view_get_path_at_pos:
4348  * @icon_view: A #GtkIconView.
4349  * @x: The x position to be identified
4350  * @y: The y position to be identified
4351  * 
4352  * Finds the path at the point (@x, @y), relative to widget coordinates.
4353  * See gtk_icon_view_get_item_at_pos(), if you are also interested in
4354  * the cell at the specified position.
4355  * 
4356  * Return value: The #GtkTreePath corresponding to the icon or %NULL
4357  * if no icon exists at that position.
4358  *
4359  * Since: 2.6 
4360  **/
4361 GtkTreePath *
4362 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
4363                                gint         x,
4364                                gint         y)
4365 {
4366   GtkIconViewItem *item;
4367   GtkTreePath *path;
4368   
4369   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4370
4371   item = gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, NULL);
4372
4373   if (!item)
4374     return NULL;
4375
4376   path = gtk_tree_path_new_from_indices (item->index, -1);
4377
4378   return path;
4379 }
4380
4381 /**
4382  * gtk_icon_view_get_item_at_pos:
4383  * @icon_view: A #GtkIconView.
4384  * @x: The x position to be identified
4385  * @y: The y position to be identified
4386  * @path: Return location for the path, or %NULL
4387  * @cell: Return location for the renderer responsible for the cell
4388  *   at (@x, @y), or %NULL
4389  * 
4390  * Finds the path at the point (@x, @y), relative to widget coordinates.
4391  * In contrast to gtk_icon_view_get_path_at_pos(), this function also 
4392  * obtains the cell at the specified position. The returned path should
4393  * be freed with gtk_tree_path_free().
4394  * 
4395  * Return value: %TRUE if an item exists at the specified position
4396  *
4397  * Since: 2.8
4398  **/
4399 gboolean 
4400 gtk_icon_view_get_item_at_pos (GtkIconView      *icon_view,
4401                                gint              x,
4402                                gint              y,
4403                                GtkTreePath     **path,
4404                                GtkCellRenderer **cell)
4405 {
4406   GtkIconViewItem *item;
4407   GtkIconViewCellInfo *info;
4408   
4409   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4410
4411   item = gtk_icon_view_get_item_at_coords (icon_view, x, y, TRUE, &info);
4412
4413   if (path != NULL)
4414     {
4415       if (item != NULL)
4416         *path = gtk_tree_path_new_from_indices (item->index, -1);
4417       else
4418         *path = NULL;
4419     }
4420
4421   if (cell != NULL)
4422     {
4423       if (info != NULL)
4424         *cell = info->cell;
4425       else 
4426         *cell = NULL;
4427     }
4428
4429   return (item != NULL);
4430 }
4431
4432
4433 /**
4434  * gtk_icon_view_get_visible_range:
4435  * @icon_view: A #GtkIconView
4436  * @start_path: Return location for start of region, or %NULL
4437  * @end_path: Return location for end of region, or %NULL
4438  * 
4439  * Sets @start_path and @end_path to be the first and last visible path. 
4440  * Note that there may be invisible paths in between.
4441  * 
4442  * Both paths should be freed with gtk_tree_path_free() after use.
4443  * 
4444  * Return value: %TRUE, if valid paths were placed in @start_path and @end_path
4445  *
4446  * Since: 2.8
4447  **/
4448 gboolean
4449 gtk_icon_view_get_visible_range (GtkIconView  *icon_view,
4450                                  GtkTreePath **start_path,
4451                                  GtkTreePath **end_path)
4452 {
4453   gint start_index = -1;
4454   gint end_index = -1;
4455   GList *icons;
4456
4457   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
4458
4459   if (icon_view->priv->hadjustment == NULL ||
4460       icon_view->priv->vadjustment == NULL)
4461     return FALSE;
4462
4463   if (start_path == NULL && end_path == NULL)
4464     return FALSE;
4465   
4466   for (icons = icon_view->priv->items; icons; icons = icons->next) 
4467     {
4468       GtkIconViewItem *item = icons->data;
4469
4470       if ((item->x + item->width >= (int)icon_view->priv->hadjustment->value) &&
4471           (item->y + item->height >= (int)icon_view->priv->vadjustment->value) &&
4472           (item->x <= (int) (icon_view->priv->hadjustment->value + icon_view->priv->hadjustment->page_size)) &&
4473           (item->y <= (int) (icon_view->priv->vadjustment->value + icon_view->priv->vadjustment->page_size)))
4474         {
4475           if (start_index == -1)
4476             start_index = item->index;
4477           end_index = item->index;
4478         }
4479     }
4480
4481   if (start_path && start_index != -1)
4482     *start_path = gtk_tree_path_new_from_indices (start_index, -1);
4483   if (end_path && end_index != -1)
4484     *end_path = gtk_tree_path_new_from_indices (end_index, -1);
4485   
4486   return start_index != -1;
4487 }
4488
4489 /**
4490  * gtk_icon_view_selected_foreach:
4491  * @icon_view: A #GtkIconView.
4492  * @func: The funcion to call for each selected icon.
4493  * @data: User data to pass to the function.
4494  * 
4495  * Calls a function for each selected icon. Note that the model or
4496  * selection cannot be modified from within this function.
4497  *
4498  * Since: 2.6 
4499  **/
4500 void
4501 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
4502                                 GtkIconViewForeachFunc func,
4503                                 gpointer               data)
4504 {
4505   GList *list;
4506   
4507   for (list = icon_view->priv->items; list; list = list->next)
4508     {
4509       GtkIconViewItem *item = list->data;
4510       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
4511
4512       if (item->selected)
4513         (* func) (icon_view, path, data);
4514
4515       gtk_tree_path_free (path);
4516     }
4517 }
4518
4519 /**
4520  * gtk_icon_view_set_selection_mode:
4521  * @icon_view: A #GtkIconView.
4522  * @mode: The selection mode
4523  * 
4524  * Sets the selection mode of the @icon_view.
4525  *
4526  * Since: 2.6 
4527  **/
4528 void
4529 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
4530                                   GtkSelectionMode  mode)
4531 {
4532   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4533
4534   if (mode == icon_view->priv->selection_mode)
4535     return;
4536   
4537   if (mode == GTK_SELECTION_NONE ||
4538       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
4539     gtk_icon_view_unselect_all (icon_view);
4540   
4541   icon_view->priv->selection_mode = mode;
4542
4543   g_object_notify (G_OBJECT (icon_view), "selection-mode");
4544 }
4545
4546 /**
4547  * gtk_icon_view_get_selection_mode:
4548  * @icon_view: A #GtkIconView.
4549  * 
4550  * Gets the selection mode of the @icon_view.
4551  *
4552  * Return value: the current selection mode
4553  *
4554  * Since: 2.6 
4555  **/
4556 GtkSelectionMode
4557 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
4558 {
4559   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
4560
4561   return icon_view->priv->selection_mode;
4562 }
4563
4564 /**
4565  * gtk_icon_view_set_model:
4566  * @icon_view: A #GtkIconView.
4567  * @model: The model.
4568  *
4569  * Sets the model for a #GtkIconView.  
4570  * If the @icon_view already has a model set, it will remove 
4571  * it before setting the new model.  If @model is %NULL, then
4572  * it will unset the old model.
4573  *
4574  * Since: 2.6 
4575  **/
4576 void
4577 gtk_icon_view_set_model (GtkIconView *icon_view,
4578                          GtkTreeModel *model)
4579 {
4580   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
4581   g_return_if_fail (model == NULL || GTK_IS_TREE_MODEL (model));
4582   
4583   if (icon_view->priv->model == model)
4584     return;
4585
4586   if (icon_view->priv->scroll_to_path)
4587     {
4588       gtk_tree_row_reference_free (icon_view->priv->scroll_to_path);
4589       icon_view->priv->scroll_to_path = NULL;
4590     }
4591
4592   gtk_icon_view_stop_editing (icon_view, TRUE);
4593
4594   if (model)
4595     {
4596       GType column_type;
4597       
4598       g_return_if_fail (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY);
4599
4600       if (icon_view->priv->pixbuf_column != -1)
4601         {
4602           column_type = gtk_tree_model_get_column_type (model,
4603                                                         icon_view->priv->pixbuf_column);          
4604
4605           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4606         }
4607
4608       if (icon_view->priv->text_column != -1)
4609         {
4610           column_type = gtk_tree_model_get_column_type (model,
4611                                                         icon_view->priv->text_column);    
4612
4613           g_return_if_fail (column_type == G_TYPE_STRING);
4614         }
4615
4616       if (icon_view->priv->markup_column != -1)
4617         {
4618           column_type = gtk_tree_model_get_column_type (model,
4619                                                         icon_view->priv->markup_column);          
4620
4621           g_return_if_fail (column_type == G_TYPE_STRING);
4622         }
4623       
4624     }
4625   
4626   if (icon_view->priv->model)
4627     {
4628       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4629                                             gtk_icon_view_row_changed,
4630                                             icon_view);
4631       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4632                                             gtk_icon_view_row_inserted,
4633                                             icon_view);
4634       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4635                                             gtk_icon_view_row_deleted,
4636                                             icon_view);
4637       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
4638                                             gtk_icon_view_rows_reordered,
4639                                             icon_view);
4640
4641       g_object_unref (icon_view->priv->model);
4642       
4643       g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
4644       g_list_free (icon_view->priv->items);
4645       icon_view->priv->items = NULL;
4646       icon_view->priv->anchor_item = NULL;
4647       icon_view->priv->cursor_item = NULL;
4648       icon_view->priv->last_single_clicked = NULL;
4649       icon_view->priv->width = 0;
4650       icon_view->priv->height = 0;
4651     }
4652
4653   icon_view->priv->model = model;
4654
4655   if (icon_view->priv->model)
4656     {
4657       g_object_ref (icon_view->priv->model);
4658       g_signal_connect (icon_view->priv->model,
4659                         "row_changed",
4660                         G_CALLBACK (gtk_icon_view_row_changed),
4661                         icon_view);
4662       g_signal_connect (icon_view->priv->model,
4663                         "row_inserted",
4664                         G_CALLBACK (gtk_icon_view_row_inserted),
4665                         icon_view);
4666       g_signal_connect (icon_view->priv->model,
4667                         "row_deleted",
4668                         G_CALLBACK (gtk_icon_view_row_deleted),
4669                         icon_view);
4670       g_signal_connect (icon_view->priv->model,
4671                         "rows_reordered",
4672                         G_CALLBACK (gtk_icon_view_rows_reordered),
4673                         icon_view);
4674
4675       gtk_icon_view_build_items (icon_view);
4676
4677       gtk_icon_view_queue_layout (icon_view);
4678     }
4679
4680   g_object_notify (G_OBJECT (icon_view), "model");  
4681
4682   if (GTK_WIDGET_REALIZED (icon_view))
4683     gtk_widget_queue_resize (GTK_WIDGET (icon_view));
4684 }
4685
4686 /**
4687  * gtk_icon_view_get_model:
4688  * @icon_view: a #GtkIconView
4689  *
4690  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
4691  * model is unset.
4692  *
4693  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
4694  *
4695  * Since: 2.6 
4696  **/
4697 GtkTreeModel *
4698 gtk_icon_view_get_model (GtkIconView *icon_view)
4699 {
4700   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
4701
4702   return icon_view->priv->model;
4703 }
4704
4705 static void
4706 update_text_cell (GtkIconView *icon_view)
4707 {
4708   GtkIconViewCellInfo *info;
4709   GList *l;
4710   gint i;
4711           
4712   if (icon_view->priv->text_column == -1 &&
4713       icon_view->priv->markup_column == -1)
4714     {
4715       if (icon_view->priv->text_cell != -1)
4716         {
4717           if (icon_view->priv->pixbuf_cell > icon_view->priv->text_cell)
4718             icon_view->priv->pixbuf_cell--;
4719
4720           info = g_list_nth_data (icon_view->priv->cell_list, 
4721                                   icon_view->priv->text_cell);
4722           
4723           icon_view->priv->cell_list = g_list_remove (icon_view->priv->cell_list, info);
4724           
4725           free_cell_info (info);
4726           
4727           icon_view->priv->n_cells--;
4728           icon_view->priv->text_cell = -1;
4729         }
4730     }
4731   else 
4732     {
4733       if (icon_view->priv->text_cell == -1)
4734         {
4735           GtkCellRenderer *cell = gtk_cell_renderer_text_new ();
4736           gtk_cell_layout_pack_end (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
4737           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4738             {
4739               info = l->data;
4740               if (info->cell == cell)
4741                 {
4742                   icon_view->priv->text_cell = i;
4743                   break;
4744                 }
4745             }
4746         }
4747       
4748       info = g_list_nth_data (icon_view->priv->cell_list,
4749                               icon_view->priv->text_cell);
4750
4751       if (icon_view->priv->markup_column != -1)
4752         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4753                                         info->cell, 
4754                                         "markup", icon_view->priv->markup_column, 
4755                                         NULL);
4756       else
4757         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4758                                         info->cell, 
4759                                         "text", icon_view->priv->text_column, 
4760                                         NULL);
4761
4762       if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
4763         g_object_set (info->cell,
4764                       "wrap_mode", PANGO_WRAP_CHAR,
4765                       "wrap_width", icon_view->priv->item_width,
4766                       "xalign", 0.5,
4767                       "yalign", 0.0,
4768                       NULL);
4769       else
4770         g_object_set (info->cell,
4771                       "wrap_mode", PANGO_WRAP_CHAR,
4772                       "wrap_width", icon_view->priv->item_width,
4773                       "xalign", 0.0,
4774                       "yalign", 0.0,
4775                       NULL);
4776     }
4777 }
4778
4779 static void
4780 update_pixbuf_cell (GtkIconView *icon_view)
4781 {
4782   GtkIconViewCellInfo *info;
4783   GList *l;
4784   gint i;
4785
4786   if (icon_view->priv->pixbuf_column == -1)
4787     {
4788       if (icon_view->priv->pixbuf_cell != -1)
4789         {
4790           if (icon_view->priv->text_cell > icon_view->priv->pixbuf_cell)
4791             icon_view->priv->text_cell--;
4792
4793           info = g_list_nth_data (icon_view->priv->cell_list, 
4794                                   icon_view->priv->pixbuf_cell);
4795           
4796           icon_view->priv->cell_list = g_list_remove (icon_view->priv->cell_list, info);
4797           
4798           free_cell_info (info);
4799           
4800           icon_view->priv->n_cells--;
4801           icon_view->priv->pixbuf_cell = -1;
4802         }
4803     }
4804   else 
4805     {
4806       if (icon_view->priv->pixbuf_cell == -1)
4807         {
4808           GtkCellRenderer *cell = gtk_cell_renderer_pixbuf_new ();
4809           
4810           gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (icon_view), cell, FALSE);
4811           for (l = icon_view->priv->cell_list, i = 0; l; l = l->next, i++)
4812             {
4813               info = l->data;
4814               if (info->cell == cell)
4815                 {
4816                   icon_view->priv->pixbuf_cell = i;
4817                   break;
4818                 }
4819             }
4820         }
4821       
4822         info = g_list_nth_data (icon_view->priv->cell_list, 
4823                                 icon_view->priv->pixbuf_cell);
4824         
4825         gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (icon_view),
4826                                         info->cell, 
4827                                         "pixbuf", icon_view->priv->pixbuf_column, 
4828                                         NULL);
4829
4830         if (icon_view->priv->orientation == GTK_ORIENTATION_VERTICAL)
4831           g_object_set (info->cell,
4832                         "follow_state", TRUE, 
4833                         "xalign", 0.5,
4834                         "yalign", 1.0,
4835                         NULL);
4836         else
4837           g_object_set (info->cell,
4838                         "follow_state", TRUE, 
4839                         "xalign", 0.0,
4840                         "yalign", 0.0,
4841                         NULL);
4842     }
4843 }
4844
4845 /**
4846  * gtk_icon_view_set_text_column:
4847  * @icon_view: A #GtkIconView.
4848  * @column: A column in the currently used model.
4849  * 
4850  * Sets the column with text for @icon_view to be @column. The text
4851  * column must be of type #G_TYPE_STRING.
4852  *
4853  * Since: 2.6 
4854  **/
4855 void
4856 gtk_icon_view_set_text_column (GtkIconView *icon_view,
4857                                gint          column)
4858 {
4859   if (column == icon_view->priv->text_column)
4860     return;
4861   
4862   if (column == -1)
4863     icon_view->priv->text_column = -1;
4864   else
4865     {
4866       if (icon_view->priv->model != NULL)
4867         {
4868           GType column_type;
4869           
4870           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4871
4872           g_return_if_fail (column_type == G_TYPE_STRING);
4873         }
4874       
4875       icon_view->priv->text_column = column;
4876     }
4877
4878   gtk_icon_view_stop_editing (icon_view, TRUE);
4879
4880   update_text_cell (icon_view);
4881
4882   gtk_icon_view_invalidate_sizes (icon_view);
4883   gtk_icon_view_queue_layout (icon_view);
4884   
4885   g_object_notify (G_OBJECT (icon_view), "text-column");
4886 }
4887
4888 /**
4889  * gtk_icon_view_get_text_column:
4890  * @icon_view: A #GtkIconView.
4891  *
4892  * Returns the column with text for @icon_view.
4893  *
4894  * Returns: the text column, or -1 if it's unset.
4895  *
4896  * Since: 2.6
4897  */
4898 gint
4899 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
4900 {
4901   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4902
4903   return icon_view->priv->text_column;
4904 }
4905
4906 /**
4907  * gtk_icon_view_set_markup_column:
4908  * @icon_view: A #GtkIconView.
4909  * @column: A column in the currently used model.
4910  * 
4911  * Sets the column with markup information for @icon_view to be
4912  * @column. The markup column must be of type #G_TYPE_STRING.
4913  * If the markup column is set to something, it overrides
4914  * the text column set by gtk_icon_view_set_text_column().
4915  *
4916  * Since: 2.6
4917  **/
4918 void
4919 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
4920                                  gint         column)
4921 {
4922   if (column == icon_view->priv->markup_column)
4923     return;
4924   
4925   if (column == -1)
4926     icon_view->priv->markup_column = -1;
4927   else
4928     {
4929       if (icon_view->priv->model != NULL)
4930         {
4931           GType column_type;
4932           
4933           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4934
4935           g_return_if_fail (column_type == G_TYPE_STRING);
4936         }
4937       
4938       icon_view->priv->markup_column = column;
4939     }
4940
4941   gtk_icon_view_stop_editing (icon_view, TRUE);
4942
4943   update_text_cell (icon_view);
4944
4945   gtk_icon_view_invalidate_sizes (icon_view);
4946   gtk_icon_view_queue_layout (icon_view);
4947   
4948   g_object_notify (G_OBJECT (icon_view), "markup-column");
4949 }
4950
4951 /**
4952  * gtk_icon_view_get_markup_column:
4953  * @icon_view: A #GtkIconView.
4954  *
4955  * Returns the column with markup text for @icon_view.
4956  *
4957  * Returns: the markup column, or -1 if it's unset.
4958  *
4959  * Since: 2.6
4960  */
4961 gint
4962 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
4963 {
4964   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
4965
4966   return icon_view->priv->markup_column;
4967 }
4968
4969 /**
4970  * gtk_icon_view_set_pixbuf_column:
4971  * @icon_view: A #GtkIconView.
4972  * @column: A column in the currently used model.
4973  * 
4974  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
4975  * column must be of type #GDK_TYPE_PIXBUF
4976  *
4977  * Since: 2.6 
4978  **/
4979 void
4980 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
4981                                  gint         column)
4982 {
4983   if (column == icon_view->priv->pixbuf_column)
4984     return;
4985   
4986   if (column == -1)
4987     icon_view->priv->pixbuf_column = -1;
4988   else
4989     {
4990       if (icon_view->priv->model != NULL)
4991         {
4992           GType column_type;
4993           
4994           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
4995
4996           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
4997         }
4998       
4999       icon_view->priv->pixbuf_column = column;
5000     }
5001
5002   gtk_icon_view_stop_editing (icon_view, TRUE);
5003
5004   update_pixbuf_cell (icon_view);
5005
5006   gtk_icon_view_invalidate_sizes (icon_view);
5007   gtk_icon_view_queue_layout (icon_view);
5008   
5009   g_object_notify (G_OBJECT (icon_view), "pixbuf-column");
5010   
5011 }
5012
5013 /**
5014  * gtk_icon_view_get_pixbuf_column:
5015  * @icon_view: A #GtkIconView.
5016  *
5017  * Returns the column with pixbufs for @icon_view.
5018  *
5019  * Returns: the pixbuf column, or -1 if it's unset.
5020  *
5021  * Since: 2.6
5022  */
5023 gint
5024 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
5025 {
5026   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5027
5028   return icon_view->priv->pixbuf_column;
5029 }
5030
5031 /**
5032  * gtk_icon_view_select_path:
5033  * @icon_view: A #GtkIconView.
5034  * @path: The #GtkTreePath to be selected.
5035  * 
5036  * Selects the row at @path.
5037  *
5038  * Since: 2.6
5039  **/
5040 void
5041 gtk_icon_view_select_path (GtkIconView *icon_view,
5042                            GtkTreePath *path)
5043 {
5044   GtkIconViewItem *item = NULL;
5045
5046   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5047   g_return_if_fail (icon_view->priv->model != NULL);
5048   g_return_if_fail (path != NULL);
5049
5050   if (gtk_tree_path_get_depth (path) > 0)
5051     item = g_list_nth_data (icon_view->priv->items,
5052                             gtk_tree_path_get_indices(path)[0]);
5053
5054   if (item)
5055     gtk_icon_view_select_item (icon_view, item);
5056 }
5057
5058 /**
5059  * gtk_icon_view_unselect_path:
5060  * @icon_view: A #GtkIconView.
5061  * @path: The #GtkTreePath to be unselected.
5062  * 
5063  * Unselects the row at @path.
5064  *
5065  * Since: 2.6
5066  **/
5067 void
5068 gtk_icon_view_unselect_path (GtkIconView *icon_view,
5069                              GtkTreePath *path)
5070 {
5071   GtkIconViewItem *item;
5072   
5073   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5074   g_return_if_fail (icon_view->priv->model != NULL);
5075   g_return_if_fail (path != NULL);
5076
5077   item = g_list_nth_data (icon_view->priv->items,
5078                           gtk_tree_path_get_indices(path)[0]);
5079
5080   if (!item)
5081     return;
5082   
5083   gtk_icon_view_unselect_item (icon_view, item);
5084 }
5085
5086 /**
5087  * gtk_icon_view_get_selected_items:
5088  * @icon_view: A #GtkIconView.
5089  *
5090  * Creates a list of paths of all selected items. Additionally, if you are
5091  * planning on modifying the model after calling this function, you may
5092  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
5093  * To do this, you can use gtk_tree_row_reference_new().
5094  *
5095  * To free the return value, use:
5096  * <informalexample><programlisting>
5097  * g_list_foreach (list, gtk_tree_path_free, NULL);
5098  * g_list_free (list);
5099  * </programlisting></informalexample>
5100  *
5101  * Return value: A #GList containing a #GtkTreePath for each selected row.
5102  *
5103  * Since: 2.6
5104  **/
5105 GList *
5106 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
5107 {
5108   GList *list;
5109   GList *selected = NULL;
5110   
5111   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
5112   
5113   for (list = icon_view->priv->items; list != NULL; list = list->next)
5114     {
5115       GtkIconViewItem *item = list->data;
5116
5117       if (item->selected)
5118         {
5119           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
5120
5121           selected = g_list_prepend (selected, path);
5122         }
5123     }
5124
5125   return selected;
5126 }
5127
5128 /**
5129  * gtk_icon_view_select_all:
5130  * @icon_view: A #GtkIconView.
5131  * 
5132  * Selects all the icons. @icon_view must has its selection mode set
5133  * to #GTK_SELECTION_MULTIPLE.
5134  *
5135  * Since: 2.6
5136  **/
5137 void
5138 gtk_icon_view_select_all (GtkIconView *icon_view)
5139 {
5140   GList *items;
5141   gboolean dirty = FALSE;
5142   
5143   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5144
5145   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
5146     return;
5147
5148   for (items = icon_view->priv->items; items; items = items->next)
5149     {
5150       GtkIconViewItem *item = items->data;
5151       
5152       if (!item->selected)
5153         {
5154           dirty = TRUE;
5155           item->selected = TRUE;
5156           gtk_icon_view_queue_draw_item (icon_view, item);
5157         }
5158     }
5159
5160   if (dirty)
5161     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5162 }
5163
5164 /**
5165  * gtk_icon_view_unselect_all:
5166  * @icon_view: A #GtkIconView.
5167  * 
5168  * Unselects all the icons.
5169  *
5170  * Since: 2.6
5171  **/
5172 void
5173 gtk_icon_view_unselect_all (GtkIconView *icon_view)
5174 {
5175   gboolean dirty = FALSE;
5176   
5177   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5178
5179   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
5180     return;
5181
5182   dirty = gtk_icon_view_unselect_all_internal (icon_view);
5183
5184   if (dirty)
5185     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
5186 }
5187
5188 /**
5189  * gtk_icon_view_path_is_selected:
5190  * @icon_view: A #GtkIconView.
5191  * @path: A #GtkTreePath to check selection on.
5192  * 
5193  * Returns %TRUE if the icon pointed to by @path is currently
5194  * selected. If @icon does not point to a valid location, %FALSE is returned.
5195  * 
5196  * Return value: %TRUE if @path is selected.
5197  *
5198  * Since: 2.6
5199  **/
5200 gboolean
5201 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
5202                                 GtkTreePath *path)
5203 {
5204   GtkIconViewItem *item;
5205   
5206   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
5207   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
5208   g_return_val_if_fail (path != NULL, FALSE);
5209   
5210   item = g_list_nth_data (icon_view->priv->items,
5211                           gtk_tree_path_get_indices(path)[0]);
5212
5213   if (!item)
5214     return FALSE;
5215   
5216   return item->selected;
5217 }
5218
5219 /**
5220  * gtk_icon_view_item_activated:
5221  * @icon_view: A #GtkIconView
5222  * @path: The #GtkTreePath to be activated
5223  * 
5224  * Activates the item determined by @path.
5225  *
5226  * Since: 2.6
5227  **/
5228 void
5229 gtk_icon_view_item_activated (GtkIconView      *icon_view,
5230                               GtkTreePath      *path)
5231 {
5232   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5233   g_return_if_fail (path != NULL);
5234   
5235   g_signal_emit (icon_view, icon_view_signals[ITEM_ACTIVATED], 0, path);
5236 }
5237
5238 /**
5239  * gtk_icon_view_set_orientation:
5240  * @icon_view: a #GtkIconView
5241  * @orientation: the relative position of texts and icons 
5242  * 
5243  * Sets the ::orientation property which determines whether the labels 
5244  * are drawn beside the icons instead of below.
5245  *
5246  * Since: 2.6
5247  **/
5248 void 
5249 gtk_icon_view_set_orientation (GtkIconView    *icon_view,
5250                                GtkOrientation  orientation)
5251 {
5252   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5253
5254   if (icon_view->priv->orientation != orientation)
5255     {
5256       icon_view->priv->orientation = orientation;
5257
5258       gtk_icon_view_stop_editing (icon_view, TRUE);
5259       gtk_icon_view_invalidate_sizes (icon_view);
5260       gtk_icon_view_queue_layout (icon_view);
5261
5262       update_text_cell (icon_view);
5263       update_pixbuf_cell (icon_view);
5264       
5265       g_object_notify (G_OBJECT (icon_view), "orientation");
5266     }
5267 }
5268
5269 /**
5270  * gtk_icon_view_get_orientation:
5271  * @icon_view: a #GtkIconView
5272  * 
5273  * Returns the value of the ::orientation property which determines 
5274  * whether the labels are drawn beside the icons instead of below. 
5275  * 
5276  * Return value: the relative position of texts and icons 
5277  *
5278  * Since: 2.6
5279  **/
5280 GtkOrientation
5281 gtk_icon_view_get_orientation (GtkIconView *icon_view)
5282 {
5283   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), 
5284                         GTK_ORIENTATION_VERTICAL);
5285
5286   return icon_view->priv->orientation;
5287 }
5288
5289 /**
5290  * gtk_icon_view_set_columns:
5291  * @icon_view: a #GtkIconView
5292  * @columns: the number of columns
5293  * 
5294  * Sets the ::columns property which determines in how
5295  * many columns the icons are arranged. If @columns is
5296  * -1, the number of columns will be chosen automatically 
5297  * to fill the available area. 
5298  *
5299  * Since: 2.6
5300  */
5301 void 
5302 gtk_icon_view_set_columns (GtkIconView *icon_view,
5303                            gint         columns)
5304 {
5305   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5306   
5307   if (icon_view->priv->columns != columns)
5308     {
5309       icon_view->priv->columns = columns;
5310
5311       gtk_icon_view_stop_editing (icon_view, TRUE);
5312       gtk_icon_view_queue_layout (icon_view);
5313       
5314       g_object_notify (G_OBJECT (icon_view), "columns");
5315     }  
5316 }
5317
5318 /**
5319  * gtk_icon_view_get_columns:
5320  * @icon_view: a #GtkIconView
5321  * 
5322  * Returns the value of the ::columns property.
5323  * 
5324  * Return value: the number of columns, or -1
5325  *
5326  * Since: 2.6
5327  */
5328 gint
5329 gtk_icon_view_get_columns (GtkIconView *icon_view)
5330 {
5331   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5332
5333   return icon_view->priv->columns;
5334 }
5335
5336 /**
5337  * gtk_icon_view_set_item_width:
5338  * @icon_view: a #GtkIconView
5339  * @item_width: the width for each item
5340  * 
5341  * Sets the ::item-width property which specifies the width 
5342  * to use for each item. If it is set to -1, the icon view will 
5343  * automatically determine a suitable item size.
5344  *
5345  * Since: 2.6
5346  */
5347 void 
5348 gtk_icon_view_set_item_width (GtkIconView *icon_view,
5349                               gint         item_width)
5350 {
5351   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5352   
5353   if (icon_view->priv->item_width != item_width)
5354     {
5355       icon_view->priv->item_width = item_width;
5356       
5357       gtk_icon_view_stop_editing (icon_view, TRUE);
5358       gtk_icon_view_invalidate_sizes (icon_view);
5359       gtk_icon_view_queue_layout (icon_view);
5360       
5361       update_text_cell (icon_view);
5362
5363       g_object_notify (G_OBJECT (icon_view), "item-width");
5364     }  
5365 }
5366
5367 /**
5368  * gtk_icon_view_get_item_width:
5369  * @icon_view: a #GtkIconView
5370  * 
5371  * Returns the value of the ::item-width property.
5372  * 
5373  * Return value: the width of a single item, or -1
5374  *
5375  * Since: 2.6
5376  */
5377 gint
5378 gtk_icon_view_get_item_width (GtkIconView *icon_view)
5379 {
5380   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5381
5382   return icon_view->priv->item_width;
5383 }
5384
5385
5386 /**
5387  * gtk_icon_view_set_spacing:
5388  * @icon_view: a #GtkIconView
5389  * @spacing: the spacing
5390  * 
5391  * Sets the ::spacing property which specifies the space 
5392  * which is inserted between the cells (i.e. the icon and 
5393  * the text) of an item.
5394  *
5395  * Since: 2.6
5396  */
5397 void 
5398 gtk_icon_view_set_spacing (GtkIconView *icon_view,
5399                            gint         spacing)
5400 {
5401   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5402   
5403   if (icon_view->priv->spacing != spacing)
5404     {
5405       icon_view->priv->spacing = spacing;
5406
5407       gtk_icon_view_stop_editing (icon_view, TRUE);
5408       gtk_icon_view_invalidate_sizes (icon_view);
5409       gtk_icon_view_queue_layout (icon_view);
5410       
5411       g_object_notify (G_OBJECT (icon_view), "spacing");
5412     }  
5413 }
5414
5415 /**
5416  * gtk_icon_view_get_spacing:
5417  * @icon_view: a #GtkIconView
5418  * 
5419  * Returns the value of the ::spacing property.
5420  * 
5421  * Return value: the space between cells 
5422  *
5423  * Since: 2.6
5424  */
5425 gint
5426 gtk_icon_view_get_spacing (GtkIconView *icon_view)
5427 {
5428   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5429
5430   return icon_view->priv->spacing;
5431 }
5432
5433 /**
5434  * gtk_icon_view_set_row_spacing:
5435  * @icon_view: a #GtkIconView
5436  * @row_spacing: the row spacing
5437  * 
5438  * Sets the ::row-spacing property which specifies the space 
5439  * which is inserted between the rows of the icon view.
5440  *
5441  * Since: 2.6
5442  */
5443 void 
5444 gtk_icon_view_set_row_spacing (GtkIconView *icon_view,
5445                                gint         row_spacing)
5446 {
5447   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5448   
5449   if (icon_view->priv->row_spacing != row_spacing)
5450     {
5451       icon_view->priv->row_spacing = row_spacing;
5452
5453       gtk_icon_view_stop_editing (icon_view, TRUE);
5454       gtk_icon_view_invalidate_sizes (icon_view);
5455       gtk_icon_view_queue_layout (icon_view);
5456       
5457       g_object_notify (G_OBJECT (icon_view), "row-spacing");
5458     }  
5459 }
5460
5461 /**
5462  * gtk_icon_view_get_row_spacing:
5463  * @icon_view: a #GtkIconView
5464  * 
5465  * Returns the value of the ::row-spacing property.
5466  * 
5467  * Return value: the space between rows
5468  *
5469  * Since: 2.6
5470  */
5471 gint
5472 gtk_icon_view_get_row_spacing (GtkIconView *icon_view)
5473 {
5474   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5475
5476   return icon_view->priv->row_spacing;
5477 }
5478
5479 /**
5480  * gtk_icon_view_set_column_spacing:
5481  * @icon_view: a #GtkIconView
5482  * @column_spacing: the column spacing
5483  * 
5484  * Sets the ::column-spacing property which specifies the space 
5485  * which is inserted between the columns of the icon view.
5486  *
5487  * Since: 2.6
5488  */
5489 void 
5490 gtk_icon_view_set_column_spacing (GtkIconView *icon_view,
5491                                   gint         column_spacing)
5492 {
5493   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5494   
5495   if (icon_view->priv->column_spacing != column_spacing)
5496     {
5497       icon_view->priv->column_spacing = column_spacing;
5498
5499       gtk_icon_view_stop_editing (icon_view, TRUE);
5500       gtk_icon_view_invalidate_sizes (icon_view);
5501       gtk_icon_view_queue_layout (icon_view);
5502       
5503       g_object_notify (G_OBJECT (icon_view), "column-spacing");
5504     }  
5505 }
5506
5507 /**
5508  * gtk_icon_view_get_column_spacing:
5509  * @icon_view: a #GtkIconView
5510  * 
5511  * Returns the value of the ::column-spacing property.
5512  * 
5513  * Return value: the space between columns
5514  *
5515  * Since: 2.6
5516  */
5517 gint
5518 gtk_icon_view_get_column_spacing (GtkIconView *icon_view)
5519 {
5520   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5521
5522   return icon_view->priv->column_spacing;
5523 }
5524
5525 /**
5526  * gtk_icon_view_set_margin:
5527  * @icon_view: a #GtkIconView
5528  * @margin: the margin
5529  * 
5530  * Sets the ::margin property which specifies the space 
5531  * which is inserted at the top, bottom, left and right 
5532  * of the icon view.
5533  *
5534  * Since: 2.6
5535  */
5536 void 
5537 gtk_icon_view_set_margin (GtkIconView *icon_view,
5538                           gint         margin)
5539 {
5540   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
5541   
5542   if (icon_view->priv->margin != margin)
5543     {
5544       icon_view->priv->margin = margin;
5545
5546       gtk_icon_view_stop_editing (icon_view, TRUE);
5547       gtk_icon_view_invalidate_sizes (icon_view);
5548       gtk_icon_view_queue_layout (icon_view);
5549       
5550       g_object_notify (G_OBJECT (icon_view), "margin");
5551     }  
5552 }
5553
5554 /**
5555  * gtk_icon_view_get_margin:
5556  * @icon_view: a #GtkIconView
5557  * 
5558  * Returns the value of the ::margin property.
5559  * 
5560  * Return value: the space at the borders 
5561  *
5562  * Since: 2.6
5563  */
5564 gint
5565 gtk_icon_view_get_margin (GtkIconView *icon_view)
5566 {
5567   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
5568
5569   return icon_view->priv->margin;
5570 }
5571
5572
5573 /* Get/set whether drag_motion requested the drag data and
5574  * drag_data_received should thus not actually insert the data,
5575  * since the data doesn't result from a drop.
5576  */
5577 static void
5578 set_status_pending (GdkDragContext *context,
5579                     GdkDragAction   suggested_action)
5580 {
5581   g_object_set_data (G_OBJECT (context),
5582                      I_("gtk-icon-view-status-pending"),
5583                      GINT_TO_POINTER (suggested_action));
5584 }
5585
5586 static GdkDragAction
5587 get_status_pending (GdkDragContext *context)
5588 {
5589   return GPOINTER_TO_INT (g_object_get_data (G_OBJECT (context),
5590                                              "gtk-icon-view-status-pending"));
5591 }
5592
5593 static void
5594 unset_reorderable (GtkIconView *icon_view)
5595 {
5596   if (icon_view->priv->reorderable)
5597     {
5598       icon_view->priv->reorderable = FALSE;
5599       g_object_notify (G_OBJECT (icon_view), "reorderable");
5600     }
5601 }
5602
5603 static void
5604 clear_source_info (GtkIconView *icon_view)
5605 {
5606   if (icon_view->priv->source_targets)
5607     gtk_target_list_unref (icon_view->priv->source_targets);
5608   icon_view->priv->source_targets = NULL;
5609
5610   icon_view->priv->source_set = FALSE;
5611 }
5612
5613 static void
5614 clear_dest_info (GtkIconView *icon_view)
5615 {
5616   if (icon_view->priv->dest_targets)
5617     gtk_target_list_unref (icon_view->priv->dest_targets);
5618   icon_view->priv->dest_targets = NULL;
5619
5620   icon_view->priv->dest_set = FALSE;
5621 }
5622
5623 static void
5624 set_source_row (GdkDragContext *context,
5625                 GtkTreeModel   *model,
5626                 GtkTreePath    *source_row)
5627 {
5628   if (source_row)
5629     g_object_set_data_full (G_OBJECT (context),
5630                             I_("gtk-icon-view-source-row"),
5631                             gtk_tree_row_reference_new (model, source_row),
5632                             (GDestroyNotify) gtk_tree_row_reference_free);
5633   else
5634     g_object_set_data_full (G_OBJECT (context),
5635                             I_("gtk-icon-view-source-row"),
5636                             NULL, NULL);
5637 }
5638
5639 static GtkTreePath*
5640 get_source_row (GdkDragContext *context)
5641 {
5642   GtkTreeRowReference *ref;
5643
5644   ref = g_object_get_data (G_OBJECT (context), "gtk-icon-view-source-row");
5645
5646   if (ref)
5647     return gtk_tree_row_reference_get_path (ref);
5648   else
5649     return NULL;
5650 }
5651
5652 typedef struct
5653 {
5654   GtkTreeRowReference *dest_row;
5655   gboolean             empty_view_drop;
5656   gboolean             drop_append_mode;
5657 } DestRow;
5658
5659 static void
5660 dest_row_free (gpointer data)
5661 {
5662   DestRow *dr = (DestRow *)data;
5663
5664   gtk_tree_row_reference_free (dr->dest_row);
5665   g_free (dr);
5666 }
5667
5668 static void
5669 set_dest_row (GdkDragContext *context,
5670               GtkTreeModel   *model,
5671               GtkTreePath    *dest_row,
5672               gboolean        empty_view_drop,
5673               gboolean        drop_append_mode)
5674 {
5675   DestRow *dr;
5676
5677   if (!dest_row)
5678     {
5679       g_object_set_data_full (G_OBJECT (context),
5680                               I_("gtk-icon-view-dest-row"),
5681                               NULL, NULL);
5682       return;
5683     }
5684   
5685   dr = g_new0 (DestRow, 1);
5686      
5687   dr->dest_row = gtk_tree_row_reference_new (model, dest_row);
5688   dr->empty_view_drop = empty_view_drop;
5689   dr->drop_append_mode = drop_append_mode;
5690   g_object_set_data_full (G_OBJECT (context),
5691                           I_("gtk-icon-view-dest-row"),
5692                           dr, (GDestroyNotify) dest_row_free);
5693 }
5694
5695 static GtkTreePath*
5696 get_dest_row (GdkDragContext *context)
5697 {
5698   DestRow *dr;
5699
5700   dr = g_object_get_data (G_OBJECT (context), "gtk-icon-view-dest-row");
5701
5702   if (dr)
5703     {
5704       GtkTreePath *path = NULL;
5705       
5706       if (dr->dest_row)
5707         path = gtk_tree_row_reference_get_path (dr->dest_row);
5708       else if (dr->empty_view_drop)
5709         path = gtk_tree_path_new_from_indices (0, -1);
5710       else
5711         path = NULL;
5712
5713       if (path && dr->drop_append_mode)
5714         gtk_tree_path_next (path);
5715
5716       return path;
5717     }
5718   else
5719     return NULL;
5720 }
5721
5722 static gboolean
5723 check_model_dnd (GtkTreeModel *model,
5724                  GType         required_iface,
5725                  const gchar  *signal)
5726 {
5727   if (model == NULL || !G_TYPE_CHECK_INSTANCE_TYPE ((model), required_iface))
5728     {
5729       g_warning ("You must override the default '%s' handler "
5730                  "on GtkIconView when using models that don't support "
5731                  "the %s interface and enabling drag-and-drop. The simplest way to do this "
5732                  "is to connect to '%s' and call "
5733                  "g_signal_stop_emission_by_name() in your signal handler to prevent "
5734                  "the default handler from running. Look at the source code "
5735                  "for the default handler in gtkiconview.c to get an idea what "
5736                  "your handler should do. (gtkiconview.c is in the GTK+ source "
5737                  "code.) If you're using GTK+ from a language other than C, "
5738                  "there may be a more natural way to override default handlers, e.g. via derivation.",
5739                  signal, g_type_name (required_iface), signal);
5740       return FALSE;
5741     }
5742   else
5743     return TRUE;
5744 }
5745
5746 static void
5747 remove_scroll_timeout (GtkIconView *icon_view)
5748 {
5749   if (icon_view->priv->scroll_timeout_id != 0)
5750     {
5751       g_source_remove (icon_view->priv->scroll_timeout_id);
5752
5753       icon_view->priv->scroll_timeout_id = 0;
5754     }
5755 }
5756
5757 static void
5758 gtk_icon_view_autoscroll (GtkIconView *icon_view)
5759 {
5760   gint px, py, x, y, width, height;
5761   gint hoffset, voffset;
5762   gfloat value;
5763
5764   gdk_window_get_pointer (GTK_WIDGET (icon_view)->window, &px, &py, NULL);
5765   gdk_window_get_geometry (GTK_WIDGET (icon_view)->window, &x, &y, &width, &height, NULL);
5766
5767   /* see if we are near the edge. */
5768   voffset = py - (y + 2 * SCROLL_EDGE_SIZE);
5769   if (voffset > 0)
5770     voffset = MAX (py - (y + height - 2 * SCROLL_EDGE_SIZE), 0);
5771
5772   hoffset = px - (x + 2 * SCROLL_EDGE_SIZE);
5773   if (hoffset > 0)
5774     hoffset = MAX (px - (x + width - 2 * SCROLL_EDGE_SIZE), 0);
5775
5776   if (voffset != 0)
5777     {
5778       value = CLAMP (icon_view->priv->vadjustment->value + voffset, 
5779                      icon_view->priv->vadjustment->lower,
5780                      icon_view->priv->vadjustment->upper - icon_view->priv->vadjustment->page_size);
5781       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
5782     }
5783   if (hoffset != 0)
5784     {
5785       value = CLAMP (icon_view->priv->hadjustment->value + hoffset, 
5786                      icon_view->priv->hadjustment->lower,
5787                      icon_view->priv->hadjustment->upper - icon_view->priv->hadjustment->page_size);
5788       gtk_adjustment_set_value (icon_view->priv->hadjustment, value);
5789     }
5790 }
5791
5792
5793 static gboolean
5794 drag_scroll_timeout (gpointer data)
5795 {
5796   GtkIconView *icon_view = GTK_ICON_VIEW (data);
5797
5798   GDK_THREADS_ENTER ();
5799
5800   gtk_icon_view_autoscroll (icon_view);
5801
5802   GDK_THREADS_LEAVE ();
5803
5804   return TRUE;
5805 }
5806
5807
5808 static gboolean
5809 set_destination (GtkIconView    *icon_view,
5810                  GdkDragContext *context,
5811                  gint            x,
5812                  gint            y,
5813                  GdkDragAction  *suggested_action,
5814                  GdkAtom        *target)
5815 {
5816   GtkWidget *widget;
5817   GtkTreePath *path = NULL;
5818   GtkIconViewDropPosition pos;
5819   GtkIconViewDropPosition old_pos;
5820   GtkTreePath *old_dest_path = NULL;
5821   gboolean can_drop = FALSE;
5822
5823   widget = GTK_WIDGET (icon_view);
5824
5825   *suggested_action = 0;
5826   *target = GDK_NONE;
5827
5828   if (!icon_view->priv->dest_set)
5829     {
5830       /* someone unset us as a drag dest, note that if
5831        * we return FALSE drag_leave isn't called
5832        */
5833
5834       gtk_icon_view_set_drag_dest_item (icon_view,
5835                                         NULL,
5836                                         GTK_ICON_VIEW_DROP_LEFT);
5837
5838       remove_scroll_timeout (GTK_ICON_VIEW (widget));
5839
5840       return FALSE; /* no longer a drop site */
5841     }
5842
5843   *target = gtk_drag_dest_find_target (widget, context, icon_view->priv->dest_targets);
5844   if (*target == GDK_NONE)
5845     return FALSE;
5846
5847   if (!gtk_icon_view_get_dest_item_at_pos (icon_view, x, y, &path, &pos)) 
5848     {
5849       gint n_children;
5850       GtkTreeModel *model;
5851       
5852       /* the row got dropped on empty space, let's setup a special case
5853        */
5854
5855       if (path)
5856         gtk_tree_path_free (path);
5857
5858       model = gtk_icon_view_get_model (icon_view);
5859
5860       n_children = gtk_tree_model_iter_n_children (model, NULL);
5861       if (n_children)
5862         {
5863           pos = GTK_ICON_VIEW_DROP_BELOW;
5864           path = gtk_tree_path_new_from_indices (n_children - 1, -1);
5865         }
5866       else
5867         {
5868           pos = GTK_ICON_VIEW_DROP_ABOVE;
5869           path = gtk_tree_path_new_from_indices (0, -1);
5870         }
5871
5872       can_drop = TRUE;
5873
5874       goto out;
5875     }
5876
5877   g_assert (path);
5878
5879   gtk_icon_view_get_drag_dest_item (icon_view,
5880                                     &old_dest_path,
5881                                     &old_pos);
5882   
5883   if (old_dest_path)
5884     gtk_tree_path_free (old_dest_path);
5885   
5886   if (TRUE /* FIXME if the location droppable predicate */)
5887     {
5888       can_drop = TRUE;
5889     }
5890
5891 out:
5892   if (can_drop)
5893     {
5894       GtkWidget *source_widget;
5895
5896       *suggested_action = context->suggested_action;
5897       source_widget = gtk_drag_get_source_widget (context);
5898
5899       if (source_widget == widget)
5900         {
5901           /* Default to MOVE, unless the user has
5902            * pressed ctrl or shift to affect available actions
5903            */
5904           if ((context->actions & GDK_ACTION_MOVE) != 0)
5905             *suggested_action = GDK_ACTION_MOVE;
5906         }
5907
5908       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
5909                                         path, pos);
5910     }
5911   else
5912     {
5913       /* can't drop here */
5914       gtk_icon_view_set_drag_dest_item (GTK_ICON_VIEW (widget),
5915                                         NULL,
5916                                         GTK_ICON_VIEW_DROP_LEFT);
5917     }
5918   
5919   if (path)
5920     gtk_tree_path_free (path);
5921   
5922   return TRUE;
5923 }
5924
5925 static GtkTreePath*
5926 get_logical_destination (GtkIconView *icon_view,
5927                          gboolean    *drop_append_mode)
5928 {
5929   /* adjust path to point to the row the drop goes in front of */
5930   GtkTreePath *path = NULL;
5931   GtkIconViewDropPosition pos;
5932   
5933   *drop_append_mode = FALSE;
5934
5935   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
5936
5937   if (path == NULL)
5938     return NULL;
5939
5940   if (pos == GTK_ICON_VIEW_DROP_RIGHT || 
5941       pos == GTK_ICON_VIEW_DROP_BELOW)
5942     {
5943       GtkTreeIter iter;
5944       GtkTreeModel *model = icon_view->priv->model;
5945
5946       if (!gtk_tree_model_get_iter (model, &iter, path) ||
5947           !gtk_tree_model_iter_next (model, &iter))
5948         *drop_append_mode = TRUE;
5949       else
5950         {
5951           *drop_append_mode = FALSE;
5952           gtk_tree_path_next (path);
5953         }      
5954     }
5955
5956   return path;
5957 }
5958
5959 static gboolean
5960 gtk_icon_view_maybe_begin_drag (GtkIconView    *icon_view,
5961                                 GdkEventMotion *event)
5962 {
5963   GdkDragContext *context;
5964   GtkTreePath *path = NULL;
5965   gint button;
5966   GtkTreeModel *model;
5967   gboolean retval = FALSE;
5968
5969   if (!icon_view->priv->source_set)
5970     goto out;
5971
5972   if (icon_view->priv->pressed_button < 0)
5973     goto out;
5974
5975   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
5976                                  icon_view->priv->press_start_x,
5977                                  icon_view->priv->press_start_y,
5978                                  event->x, event->y))
5979     goto out;
5980
5981   model = gtk_icon_view_get_model (icon_view);
5982
5983   if (model == NULL)
5984     goto out;
5985
5986   button = icon_view->priv->pressed_button;
5987   icon_view->priv->pressed_button = -1;
5988
5989   path = gtk_icon_view_get_path_at_pos (icon_view,
5990                                         icon_view->priv->press_start_x,
5991                                         icon_view->priv->press_start_y);
5992
5993   if (path == NULL)
5994     goto out;
5995
5996   if (!GTK_IS_TREE_DRAG_SOURCE (model) ||
5997       !gtk_tree_drag_source_row_draggable (GTK_TREE_DRAG_SOURCE (model),
5998                                            path))
5999     goto out;
6000
6001   /* FIXME Check whether we're a start button, if not return FALSE and
6002    * free path
6003    */
6004
6005   /* Now we can begin the drag */
6006   
6007   retval = TRUE;
6008
6009   context = gtk_drag_begin (GTK_WIDGET (icon_view),
6010                             icon_view->priv->source_targets,
6011                             icon_view->priv->source_actions,
6012                             button,
6013                             (GdkEvent*)event);
6014
6015   set_source_row (context, model, path);
6016   
6017  out:
6018   if (path)
6019     gtk_tree_path_free (path);
6020
6021   return retval;
6022 }
6023
6024 /* Source side drag signals */
6025 static void 
6026 gtk_icon_view_drag_begin (GtkWidget      *widget,
6027                           GdkDragContext *context)
6028 {
6029   GtkIconView *icon_view;
6030   GtkIconViewItem *item;
6031   GdkPixmap *icon;
6032   gint x, y;
6033   GtkTreePath *path;
6034
6035   icon_view = GTK_ICON_VIEW (widget);
6036
6037   /* if the user uses a custom DnD impl, we don't set the icon here */
6038   if (!icon_view->priv->dest_set && !icon_view->priv->source_set)
6039     return;
6040
6041   item = gtk_icon_view_get_item_at_coords (icon_view,
6042                                            icon_view->priv->press_start_x,
6043                                            icon_view->priv->press_start_y,
6044                                            TRUE,
6045                                            NULL);
6046
6047   g_return_if_fail (item != NULL);
6048
6049   x = icon_view->priv->press_start_x - item->x + 1;
6050   y = icon_view->priv->press_start_y - item->y + 1;
6051   
6052   path = gtk_tree_path_new_from_indices (item->index, -1);
6053   icon = gtk_icon_view_create_drag_icon (icon_view, path);
6054   gtk_tree_path_free (path);
6055
6056   gtk_drag_set_icon_pixmap (context, 
6057                             gdk_drawable_get_colormap (icon),
6058                             icon, 
6059                             NULL, 
6060                             x, y);
6061
6062   g_object_unref (icon);
6063 }
6064
6065 static void 
6066 gtk_icon_view_drag_end (GtkWidget      *widget,
6067                         GdkDragContext *context)
6068 {
6069   /* do nothing */
6070 }
6071
6072 static void 
6073 gtk_icon_view_drag_data_get (GtkWidget        *widget,
6074                              GdkDragContext   *context,
6075                              GtkSelectionData *selection_data,
6076                              guint             info,
6077                              guint             time)
6078 {
6079   GtkIconView *icon_view;
6080   GtkTreeModel *model;
6081   GtkTreePath *source_row;
6082
6083   icon_view = GTK_ICON_VIEW (widget);
6084   model = gtk_icon_view_get_model (icon_view);
6085
6086   if (model == NULL)
6087     return;
6088
6089   if (!icon_view->priv->dest_set)
6090     return;
6091
6092   source_row = get_source_row (context);
6093
6094   if (source_row == NULL)
6095     return;
6096
6097   /* We can implement the GTK_TREE_MODEL_ROW target generically for
6098    * any model; for DragSource models there are some other targets
6099    * we also support.
6100    */
6101
6102   if (GTK_IS_TREE_DRAG_SOURCE (model) &&
6103       gtk_tree_drag_source_drag_data_get (GTK_TREE_DRAG_SOURCE (model),
6104                                           source_row,
6105                                           selection_data))
6106     goto done;
6107
6108   /* If drag_data_get does nothing, try providing row data. */
6109   if (selection_data->target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6110     gtk_tree_set_row_drag_data (selection_data,
6111                                 model,
6112                                 source_row);
6113
6114  done:
6115   gtk_tree_path_free (source_row);
6116 }
6117
6118 static void 
6119 gtk_icon_view_drag_data_delete (GtkWidget      *widget,
6120                                 GdkDragContext *context)
6121 {
6122   GtkTreeModel *model;
6123   GtkIconView *icon_view;
6124   GtkTreePath *source_row;
6125
6126   icon_view = GTK_ICON_VIEW (widget);
6127   model = gtk_icon_view_get_model (icon_view);
6128
6129   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_SOURCE, "drag_data_delete"))
6130     return;
6131
6132   if (!icon_view->priv->dest_set)
6133     return;
6134
6135   source_row = get_source_row (context);
6136
6137   if (source_row == NULL)
6138     return;
6139
6140   gtk_tree_drag_source_drag_data_delete (GTK_TREE_DRAG_SOURCE (model),
6141                                          source_row);
6142
6143   gtk_tree_path_free (source_row);
6144
6145   set_source_row (context, NULL, NULL);
6146 }
6147
6148 /* Target side drag signals */
6149 static void
6150 gtk_icon_view_drag_leave (GtkWidget      *widget,
6151                           GdkDragContext *context,
6152                           guint           time)
6153 {
6154   GtkIconView *icon_view;
6155
6156   icon_view = GTK_ICON_VIEW (widget);
6157
6158   /* unset any highlight row */
6159   gtk_icon_view_set_drag_dest_item (icon_view,
6160                                     NULL,
6161                                     GTK_ICON_VIEW_DROP_LEFT);
6162
6163   remove_scroll_timeout (icon_view);
6164 }
6165
6166 static gboolean 
6167 gtk_icon_view_drag_motion (GtkWidget      *widget,
6168                            GdkDragContext *context,
6169                            gint            x,
6170                            gint            y,
6171                            guint           time)
6172 {
6173   GtkTreePath *path = NULL;
6174   GtkIconViewDropPosition pos;
6175   GtkIconView *icon_view;
6176   GdkDragAction suggested_action = 0;
6177   GdkAtom target;
6178   gboolean empty;
6179
6180   icon_view = GTK_ICON_VIEW (widget);
6181
6182   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6183     return FALSE;
6184
6185   gtk_icon_view_get_drag_dest_item (icon_view, &path, &pos);
6186
6187   /* we only know this *after* set_desination_row */
6188   empty = icon_view->priv->empty_view_drop;
6189
6190   if (path == NULL && !empty)
6191     {
6192       /* Can't drop here. */
6193       gdk_drag_status (context, 0, time);
6194     }
6195   else
6196     {
6197       if (icon_view->priv->scroll_timeout_id == 0)
6198         {
6199           icon_view->priv->scroll_timeout_id =
6200             g_timeout_add (50, drag_scroll_timeout, icon_view);
6201         }
6202
6203       if (target == gdk_atom_intern_static_string ("GTK_TREE_MODEL_ROW"))
6204         {
6205           /* Request data so we can use the source row when
6206            * determining whether to accept the drop
6207            */
6208           set_status_pending (context, suggested_action);
6209           gtk_drag_get_data (widget, context, target, time);
6210         }
6211       else
6212         {
6213           set_status_pending (context, 0);
6214           gdk_drag_status (context, suggested_action, time);
6215         }
6216     }
6217
6218   if (path)
6219     gtk_tree_path_free (path);
6220
6221   return TRUE;
6222 }
6223
6224 static gboolean 
6225 gtk_icon_view_drag_drop (GtkWidget      *widget,
6226                          GdkDragContext *context,
6227                          gint            x,
6228                          gint            y,
6229                          guint           time)
6230 {
6231   GtkIconView *icon_view;
6232   GtkTreePath *path;
6233   GdkDragAction suggested_action = 0;
6234   GdkAtom target = GDK_NONE;
6235   GtkTreeModel *model;
6236   gboolean drop_append_mode;
6237
6238   icon_view = GTK_ICON_VIEW (widget);
6239   model = gtk_icon_view_get_model (icon_view);
6240
6241   remove_scroll_timeout (GTK_ICON_VIEW (widget));
6242
6243   if (!icon_view->priv->dest_set)
6244     return FALSE;
6245
6246   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_drop"))
6247     return FALSE;
6248
6249   if (!set_destination (icon_view, context, x, y, &suggested_action, &target))
6250     return FALSE;
6251   
6252   path = get_logical_destination (icon_view, &drop_append_mode);
6253
6254   if (target != GDK_NONE && path != NULL)
6255     {
6256       /* in case a motion had requested drag data, change things so we
6257        * treat drag data receives as a drop.
6258        */
6259       set_status_pending (context, 0);
6260       set_dest_row (context, model, path, 
6261                     icon_view->priv->empty_view_drop, drop_append_mode);
6262     }
6263
6264   if (path)
6265     gtk_tree_path_free (path);
6266
6267   /* Unset this thing */
6268   gtk_icon_view_set_drag_dest_item (icon_view, NULL, GTK_ICON_VIEW_DROP_LEFT);
6269
6270   if (target != GDK_NONE)
6271     {
6272       gtk_drag_get_data (widget, context, target, time);
6273       return TRUE;
6274     }
6275   else
6276     return FALSE;
6277 }
6278
6279 static void
6280 gtk_icon_view_drag_data_received (GtkWidget        *widget,
6281                                   GdkDragContext   *context,
6282                                   gint              x,
6283                                   gint              y,
6284                                   GtkSelectionData *selection_data,
6285                                   guint             info,
6286                                   guint             time)
6287 {
6288   GtkTreePath *path;
6289   gboolean accepted = FALSE;
6290   GtkTreeModel *model;
6291   GtkIconView *icon_view;
6292   GtkTreePath *dest_row;
6293   GdkDragAction suggested_action;
6294   gboolean drop_append_mode;
6295   
6296   icon_view = GTK_ICON_VIEW (widget);  
6297   model = gtk_icon_view_get_model (icon_view);
6298
6299   if (!check_model_dnd (model, GTK_TYPE_TREE_DRAG_DEST, "drag_data_received"))
6300     return;
6301
6302   if (!icon_view->priv->dest_set)
6303     return;
6304
6305   suggested_action = get_status_pending (context);
6306
6307   if (suggested_action)
6308     {
6309       /* We are getting this data due to a request in drag_motion,
6310        * rather than due to a request in drag_drop, so we are just
6311        * supposed to call drag_status, not actually paste in the
6312        * data.
6313        */
6314       path = get_logical_destination (icon_view, &drop_append_mode);
6315
6316       if (path == NULL)
6317         suggested_action = 0;
6318
6319       if (suggested_action)
6320         {
6321           if (!gtk_tree_drag_dest_row_drop_possible (GTK_TREE_DRAG_DEST (model),
6322                                                      path,
6323                                                      selection_data))
6324             suggested_action = 0;
6325         }
6326
6327       gdk_drag_status (context, suggested_action, time);
6328
6329       if (path)
6330         gtk_tree_path_free (path);
6331
6332       /* If you can't drop, remove user drop indicator until the next motion */
6333       if (suggested_action == 0)
6334         gtk_icon_view_set_drag_dest_item (icon_view,
6335                                           NULL,
6336                                           GTK_ICON_VIEW_DROP_LEFT);
6337       return;
6338     }
6339   
6340
6341   dest_row = get_dest_row (context);
6342
6343   if (dest_row == NULL)
6344     return;
6345
6346   if (selection_data->length >= 0)
6347     {
6348       if (gtk_tree_drag_dest_drag_data_received (GTK_TREE_DRAG_DEST (model),
6349                                                  dest_row,
6350                                                  selection_data))
6351         accepted = TRUE;
6352     }
6353
6354   gtk_drag_finish (context,
6355                    accepted,
6356                    (context->action == GDK_ACTION_MOVE),
6357                    time);
6358
6359   gtk_tree_path_free (dest_row);
6360
6361   /* drop dest_row */
6362   set_dest_row (context, NULL, NULL, FALSE, FALSE);
6363 }
6364
6365 /* Drag-and-Drop support */
6366 /**
6367  * gtk_icon_view_enable_model_drag_source:
6368  * @icon_view: a #GtkIconTreeView
6369  * @start_button_mask: Mask of allowed buttons to start drag
6370  * @targets: the table of targets that the drag will support
6371  * @n_targets: the number of items in @targets
6372  * @actions: the bitmask of possible actions for a drag from this
6373  *    widget
6374  * 
6375  * Turns @icon_view into a drag source for automatic DND.
6376  *
6377  * Since: 2.8
6378  **/
6379 void
6380 gtk_icon_view_enable_model_drag_source (GtkIconView              *icon_view,
6381                                         GdkModifierType           start_button_mask,
6382                                         const GtkTargetEntry     *targets,
6383                                         gint                      n_targets,
6384                                         GdkDragAction             actions)
6385 {
6386   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6387
6388   gtk_drag_source_set (GTK_WIDGET (icon_view), 0, NULL, 0, actions);
6389
6390   clear_source_info (icon_view);
6391   icon_view->priv->start_button_mask = start_button_mask;
6392   icon_view->priv->source_targets = gtk_target_list_new (targets, n_targets);
6393   icon_view->priv->source_actions = actions;
6394
6395   icon_view->priv->source_set = TRUE;
6396
6397   unset_reorderable (icon_view);
6398 }
6399
6400 /**
6401  * gtk_icon_view_enable_model_drag_dest:
6402  * @icon_view: a #GtkIconView
6403  * @targets: the table of targets that the drag will support
6404  * @n_targets: the number of items in @targets
6405  * @actions: the bitmask of possible actions for a drag from this
6406  *    widget
6407  * 
6408  * Turns @icon_view into a drop destination for automatic DND.
6409  *
6410  * Since: 2.8
6411  **/
6412 void 
6413 gtk_icon_view_enable_model_drag_dest (GtkIconView          *icon_view,
6414                                       const GtkTargetEntry *targets,
6415                                       gint                  n_targets,
6416                                       GdkDragAction         actions)
6417 {
6418   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6419
6420   gtk_drag_dest_set (GTK_WIDGET (icon_view), 0, NULL, 0, actions);
6421
6422   clear_dest_info (icon_view);
6423
6424   icon_view->priv->dest_targets = gtk_target_list_new (targets, n_targets);
6425   icon_view->priv->dest_actions = actions;
6426
6427   icon_view->priv->dest_set = TRUE;
6428
6429   unset_reorderable (icon_view);  
6430 }
6431
6432 /**
6433  * gtk_icon_view_unset_model_drag_source:
6434  * @icon_view: a #GtkIconView
6435  * 
6436  * Undoes the effect of gtk_icon_view_enable_model_drag_source().
6437  *
6438  * Since: 2.8
6439  **/
6440 void
6441 gtk_icon_view_unset_model_drag_source (GtkIconView *icon_view)
6442 {
6443   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6444
6445   if (icon_view->priv->source_set)
6446     {
6447       gtk_drag_source_unset (GTK_WIDGET (icon_view));
6448       clear_source_info (icon_view);
6449     }
6450
6451   unset_reorderable (icon_view);
6452 }
6453
6454 /**
6455  * gtk_icon_view_unset_model_drag_dest:
6456  * @icon_view: a #GtkIconView
6457  * 
6458  * Undoes the effect of gtk_icon_view_enable_model_drag_dest().
6459  *
6460  * Since: 2.8
6461  **/
6462 void
6463 gtk_icon_view_unset_model_drag_dest (GtkIconView *icon_view)
6464 {
6465   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6466
6467   if (icon_view->priv->dest_set)
6468     {
6469       gtk_drag_dest_unset (GTK_WIDGET (icon_view));
6470       clear_dest_info (icon_view);
6471     }
6472
6473   unset_reorderable (icon_view);
6474 }
6475
6476 /* These are useful to implement your own custom stuff. */
6477 /**
6478  * gtk_icon_view_set_drag_dest_item:
6479  * @icon_view: a #GtkIconView
6480  * @path: The path of the item to highlight, or %NULL.
6481  * @pos: Specifies where to drop, relative to the item
6482  * 
6483  * Sets the item that is highlighted for feedback.
6484  *
6485  * Since: 2.8
6486  */
6487 void
6488 gtk_icon_view_set_drag_dest_item (GtkIconView              *icon_view,
6489                                   GtkTreePath              *path,
6490                                   GtkIconViewDropPosition   pos)
6491 {
6492   /* Note; this function is exported to allow a custom DND
6493    * implementation, so it can't touch TreeViewDragInfo
6494    */
6495
6496   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6497
6498   if (icon_view->priv->dest_item)
6499     {
6500       GtkTreePath *current_path;
6501       current_path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6502       gtk_tree_row_reference_free (icon_view->priv->dest_item);
6503       icon_view->priv->dest_item = NULL;      
6504
6505       gtk_icon_view_queue_draw_path (icon_view, current_path);
6506       gtk_tree_path_free (current_path);
6507     }
6508   
6509   /* special case a drop on an empty model */
6510   icon_view->priv->empty_view_drop = FALSE;
6511   if (pos == GTK_TREE_VIEW_DROP_BEFORE && path
6512       && gtk_tree_path_get_depth (path) == 1
6513       && gtk_tree_path_get_indices (path)[0] == 0)
6514     {
6515       gint n_children;
6516
6517       n_children = gtk_tree_model_iter_n_children (icon_view->priv->model,
6518                                                    NULL);
6519
6520       if (n_children == 0)
6521         icon_view->priv->empty_view_drop = TRUE;
6522     }
6523
6524   icon_view->priv->dest_pos = pos;
6525
6526   if (path)
6527     {
6528       icon_view->priv->dest_item =
6529         gtk_tree_row_reference_new_proxy (G_OBJECT (icon_view), 
6530                                           icon_view->priv->model, path);
6531       
6532       gtk_icon_view_queue_draw_path (icon_view, path);
6533     }
6534 }
6535
6536 /**
6537  * gtk_icon_view_get_drag_dest_item:
6538  * @icon_view: a #GtkIconView
6539  * @path: Return location for the path of the highlighted item, or %NULL.
6540  * @pos: Return location for the drop position, or %NULL
6541  * 
6542  * Gets information about the item that is highlighted for feedback.
6543  *
6544  * Since: 2.8
6545  **/
6546 void
6547 gtk_icon_view_get_drag_dest_item (GtkIconView              *icon_view,
6548                                   GtkTreePath             **path,
6549                                   GtkIconViewDropPosition  *pos)
6550 {
6551   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6552
6553   if (path)
6554     {
6555       if (icon_view->priv->dest_item)
6556         *path = gtk_tree_row_reference_get_path (icon_view->priv->dest_item);
6557       else
6558         *path = NULL;
6559     }
6560
6561   if (pos)
6562     *pos = icon_view->priv->dest_pos;
6563 }
6564
6565 /**
6566  * gtk_icon_view_get_dest_item_at_pos:
6567  * @icon_view: a #GtkIconView
6568  * @drag_x: the position to determine the destination item for
6569  * @drag_y: the position to determine the destination item for
6570  * @path: Return location for the path of the item, or %NULL.
6571  * @pos: Return location for the drop position, or %NULL
6572  * 
6573  * Determines the destination item for a given position.
6574  * 
6575  * Return value: whether there is an item at the given position.
6576  *
6577  * Since: 2.8
6578  **/
6579 gboolean
6580 gtk_icon_view_get_dest_item_at_pos (GtkIconView              *icon_view,
6581                                     gint                      drag_x,
6582                                     gint                      drag_y,
6583                                     GtkTreePath             **path,
6584                                     GtkIconViewDropPosition  *pos)
6585 {
6586   GtkIconViewItem *item;
6587
6588   /* Note; this function is exported to allow a custom DND
6589    * implementation, so it can't touch TreeViewDragInfo
6590    */
6591
6592   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6593   g_return_val_if_fail (drag_x >= 0, FALSE);
6594   g_return_val_if_fail (drag_y >= 0, FALSE);
6595   g_return_val_if_fail (icon_view->priv->bin_window != NULL, FALSE);
6596
6597
6598   if (path)
6599     *path = NULL;
6600
6601   item = gtk_icon_view_get_item_at_coords (icon_view, 
6602                                            drag_x + icon_view->priv->hadjustment->value, 
6603                                            drag_y + icon_view->priv->vadjustment->value,
6604                                            FALSE, NULL);
6605
6606   if (item == NULL)
6607     return FALSE;
6608
6609   if (path)
6610     *path = gtk_tree_path_new_from_indices (item->index, -1);
6611
6612   if (pos)
6613     {
6614       if (drag_x < item->x + item->width / 4)
6615         *pos = GTK_ICON_VIEW_DROP_LEFT;
6616       else if (drag_x > item->x + item->width * 3 / 4)
6617         *pos = GTK_ICON_VIEW_DROP_RIGHT;
6618       else if (drag_y < item->y + item->height / 4)
6619         *pos = GTK_ICON_VIEW_DROP_ABOVE;
6620       else if (drag_y > item->y + item->height * 3 / 4)
6621         *pos = GTK_ICON_VIEW_DROP_BELOW;
6622       else
6623         *pos = GTK_ICON_VIEW_DROP_INTO;
6624     }
6625
6626   return TRUE;
6627 }
6628
6629 /**
6630  * gtk_icon_view_create_drag_icon:
6631  * @icon_view: a #GtkIconView
6632  * @path: a #GtkTreePath in @icon_view
6633  *
6634  * Creates a #GdkPixmap representation of the item at @path.  
6635  * This image is used for a drag icon.
6636  *
6637  * Return value: a newly-allocated pixmap of the drag icon.
6638  * 
6639  * Since: 2.8
6640  **/
6641 GdkPixmap *
6642 gtk_icon_view_create_drag_icon (GtkIconView *icon_view,
6643                                 GtkTreePath *path)
6644 {
6645   GtkWidget *widget;
6646   cairo_t *cr;
6647   GdkPixmap *drawable;
6648   GList *l;
6649   gint index;
6650   GdkRectangle area;
6651
6652   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
6653   g_return_val_if_fail (path != NULL, NULL);
6654
6655   widget = GTK_WIDGET (icon_view);
6656
6657   if (!GTK_WIDGET_REALIZED (widget))
6658     return NULL;
6659
6660   index = gtk_tree_path_get_indices (path)[0];
6661
6662   for (l = icon_view->priv->items; l; l = l->next) 
6663     {
6664       GtkIconViewItem *item = l->data;
6665       
6666       if (index == item->index)
6667         {
6668           drawable = gdk_pixmap_new (icon_view->priv->bin_window,
6669                                      item->width + 2,
6670                                      item->height + 2,
6671                                      -1);
6672
6673           cr = gdk_cairo_create (drawable);
6674           cairo_set_line_width (cr, 1.);
6675
6676           gdk_cairo_set_source_color
6677             (cr, &widget->style->base[GTK_WIDGET_STATE (widget)]);
6678           cairo_rectangle (cr, 0, 0, item->width + 2, item->height + 2);
6679           cairo_fill (cr);
6680
6681           area.x = 0;
6682           area.y = 0;
6683           area.width = item->width;
6684           area.height = item->height;
6685
6686           gtk_icon_view_paint_item (icon_view, cr, item, &area, 
6687                                     drawable, 1, 1, FALSE); 
6688
6689           cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
6690           cairo_rectangle (cr, 0.5, 0.5, item->width + 1, item->height + 1);
6691           cairo_stroke (cr);
6692
6693           cairo_destroy (cr);
6694
6695           return drawable;
6696         }
6697     }
6698   
6699   return NULL;
6700 }
6701
6702 /**
6703  * gtk_icon_view_get_reorderable:
6704  * @icon_view: a #GtkIconView
6705  *
6706  * Retrieves whether the user can reorder the list via drag-and-drop. 
6707  * See gtk_icon_view_set_reorderable().
6708  *
6709  * Return value: %TRUE if the list can be reordered.
6710  *
6711  * Since: 2.8
6712  **/
6713 gboolean
6714 gtk_icon_view_get_reorderable (GtkIconView *icon_view)
6715 {
6716   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
6717
6718   return icon_view->priv->reorderable;
6719 }
6720
6721 static const GtkTargetEntry item_targets[] = {
6722   { "GTK_TREE_MODEL_ROW", GTK_TARGET_SAME_WIDGET, 0 }
6723 };
6724
6725
6726 /**
6727  * gtk_icon_view_set_reorderable:
6728  * @icon_view: A #GtkIconView.
6729  * @reorderable: %TRUE, if the list of items can be reordered.
6730  *
6731  * This function is a convenience function to allow you to reorder models that
6732  * support the #GtkTreeDragSourceIface and the #GtkTreeDragDestIface.  Both
6733  * #GtkTreeStore and #GtkListStore support these.  If @reorderable is %TRUE, then
6734  * the user can reorder the model by dragging and dropping rows.  The
6735  * developer can listen to these changes by connecting to the model's
6736  * row_inserted and row_deleted signals.
6737  *
6738  * This function does not give you any degree of control over the order -- any
6739  * reordering is allowed.  If more control is needed, you should probably
6740  * handle drag and drop manually.
6741  *
6742  * Since: 2.8
6743  **/
6744 void
6745 gtk_icon_view_set_reorderable (GtkIconView *icon_view,
6746                                gboolean     reorderable)
6747 {
6748   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
6749
6750   reorderable = reorderable != FALSE;
6751
6752   if (icon_view->priv->reorderable == reorderable)
6753     return;
6754
6755   if (reorderable)
6756     {
6757       gtk_icon_view_enable_model_drag_source (icon_view,
6758                                               GDK_BUTTON1_MASK,
6759                                               item_targets,
6760                                               G_N_ELEMENTS (item_targets),
6761                                               GDK_ACTION_MOVE);
6762       gtk_icon_view_enable_model_drag_dest (icon_view,
6763                                             item_targets,
6764                                             G_N_ELEMENTS (item_targets),
6765                                             GDK_ACTION_MOVE);
6766     }
6767   else
6768     {
6769       gtk_icon_view_unset_model_drag_source (icon_view);
6770       gtk_icon_view_unset_model_drag_dest (icon_view);
6771     }
6772
6773   icon_view->priv->reorderable = reorderable;
6774
6775   g_object_notify (G_OBJECT (icon_view), "reorderable");
6776 }
6777
6778
6779 /* Accessibility Support */
6780
6781 static gpointer accessible_parent_class;
6782 static gpointer accessible_item_parent_class;
6783 static GQuark accessible_private_data_quark = 0;
6784
6785 #define GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE      (gtk_icon_view_item_accessible_get_type ())
6786 #define GTK_ICON_VIEW_ITEM_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE, GtkIconViewItemAccessible))
6787 #define GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ITEM_ACCESSIBLE))
6788
6789 static GType gtk_icon_view_item_accessible_get_type (void);
6790
6791 enum {
6792     ACTION_ACTIVATE,
6793     LAST_ACTION
6794 };
6795
6796 typedef struct
6797 {
6798   AtkObject parent;
6799
6800   GtkIconViewItem *item;
6801
6802   GtkWidget *widget;
6803
6804   AtkStateSet *state_set;
6805
6806   gchar *text;
6807
6808   GtkTextBuffer *text_buffer;
6809
6810   gchar *action_descriptions[LAST_ACTION];
6811   gchar *image_description;
6812   guint action_idle_handler;
6813 } GtkIconViewItemAccessible;
6814
6815 static const gchar *const gtk_icon_view_item_accessible_action_names[] = 
6816 {
6817   "activate",
6818   NULL
6819 };
6820
6821 static const gchar *const gtk_icon_view_item_accessible_action_descriptions[] =
6822 {
6823   "Activate item",
6824   NULL
6825 };
6826 typedef struct _GtkIconViewItemAccessibleClass
6827 {
6828   AtkObjectClass parent_class;
6829
6830 } GtkIconViewItemAccessibleClass;
6831
6832 static gboolean gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item);
6833
6834 static gboolean
6835 gtk_icon_view_item_accessible_idle_do_action (gpointer data)
6836 {
6837   GtkIconViewItemAccessible *item;
6838   GtkIconView *icon_view;
6839   GtkTreePath *path;
6840
6841   GDK_THREADS_ENTER ();
6842
6843   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (data);
6844   item->action_idle_handler = 0;
6845
6846   if (item->widget != NULL)
6847     {
6848       icon_view = GTK_ICON_VIEW (item->widget);
6849       path = gtk_tree_path_new_from_indices (item->item->index, -1);
6850       gtk_icon_view_item_activated (icon_view, path);
6851       gtk_tree_path_free (path);
6852     }
6853
6854   GDK_THREADS_LEAVE ();
6855
6856   return FALSE;
6857 }
6858
6859 static gboolean
6860 gtk_icon_view_item_accessible_action_do_action (AtkAction *action,
6861                                                 gint       i)
6862 {
6863   GtkIconViewItemAccessible *item;
6864
6865   if (i < 0 || i >= LAST_ACTION) 
6866     return FALSE;
6867
6868   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6869
6870   if (!GTK_IS_ICON_VIEW (item->widget))
6871     return FALSE;
6872
6873   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
6874     return FALSE;
6875
6876   switch (i)
6877     {
6878     case ACTION_ACTIVATE:
6879       if (!item->action_idle_handler)
6880         item->action_idle_handler = g_idle_add (gtk_icon_view_item_accessible_idle_do_action, item);
6881       break;
6882     default:
6883       g_assert_not_reached ();
6884       return FALSE;
6885
6886     }        
6887   return TRUE;
6888 }
6889
6890 static gint
6891 gtk_icon_view_item_accessible_action_get_n_actions (AtkAction *action)
6892 {
6893         return LAST_ACTION;
6894 }
6895
6896 static const gchar *
6897 gtk_icon_view_item_accessible_action_get_description (AtkAction *action,
6898                                                       gint       i)
6899 {
6900   GtkIconViewItemAccessible *item;
6901
6902   if (i < 0 || i >= LAST_ACTION) 
6903     return NULL;
6904
6905   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6906
6907   if (item->action_descriptions[i])
6908     return item->action_descriptions[i];
6909   else
6910     return gtk_icon_view_item_accessible_action_descriptions[i];
6911 }
6912
6913 static const gchar *
6914 gtk_icon_view_item_accessible_action_get_name (AtkAction *action,
6915                                                gint       i)
6916 {
6917   if (i < 0 || i >= LAST_ACTION) 
6918     return NULL;
6919
6920   return gtk_icon_view_item_accessible_action_names[i];
6921 }
6922
6923 static gboolean
6924 gtk_icon_view_item_accessible_action_set_description (AtkAction   *action,
6925                                                       gint         i,
6926                                                       const gchar *description)
6927 {
6928   GtkIconViewItemAccessible *item;
6929
6930   if (i < 0 || i >= LAST_ACTION) 
6931     return FALSE;
6932
6933   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (action);
6934
6935   if (item->action_descriptions[i])
6936     g_free (item->action_descriptions[i]);
6937
6938   item->action_descriptions[i] = g_strdup (description);
6939
6940   return TRUE;
6941 }
6942
6943 static void
6944 atk_action_item_interface_init (AtkActionIface *iface)
6945 {
6946   iface->do_action = gtk_icon_view_item_accessible_action_do_action;
6947   iface->get_n_actions = gtk_icon_view_item_accessible_action_get_n_actions;
6948   iface->get_description = gtk_icon_view_item_accessible_action_get_description;
6949   iface->get_name = gtk_icon_view_item_accessible_action_get_name;
6950   iface->set_description = gtk_icon_view_item_accessible_action_set_description;
6951 }
6952
6953 static const gchar *
6954 gtk_icon_view_item_accessible_image_get_image_description (AtkImage *image)
6955 {
6956   GtkIconViewItemAccessible *item;
6957
6958   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6959
6960   return item->image_description;
6961 }
6962
6963 static gboolean
6964 gtk_icon_view_item_accessible_image_set_image_description (AtkImage    *image,
6965                                                            const gchar *description)
6966 {
6967   GtkIconViewItemAccessible *item;
6968
6969   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
6970
6971   g_free (item->image_description);
6972   item->image_description = g_strdup (item->image_description);
6973
6974   return TRUE;
6975 }
6976
6977 static gboolean
6978 get_pixbuf_box (GtkIconView     *icon_view,
6979                 GtkIconViewItem *item,
6980                 GdkRectangle    *box)
6981 {
6982   GList *l;
6983
6984   for (l = icon_view->priv->cell_list; l; l = l->next)
6985     {
6986       GtkIconViewCellInfo *info = l->data;
6987       
6988       if (GTK_IS_CELL_RENDERER_PIXBUF (info->cell))
6989         {
6990           gtk_icon_view_get_cell_box (icon_view, item, info, box);
6991
6992           return TRUE;
6993         }
6994     }
6995
6996   return FALSE;
6997 }
6998
6999 static gchar *
7000 get_text (GtkIconView     *icon_view,
7001           GtkIconViewItem *item)
7002 {
7003   GList *l;
7004   gchar *text;
7005
7006   for (l = icon_view->priv->cell_list; l; l = l->next)
7007     {
7008       GtkIconViewCellInfo *info = l->data;
7009       
7010       if (GTK_IS_CELL_RENDERER_TEXT (info->cell))
7011         {
7012           g_object_get (info->cell, "text", &text, NULL);
7013           
7014           return text;
7015         }
7016     }
7017
7018   return NULL;
7019 }
7020
7021 static void
7022 gtk_icon_view_item_accessible_image_get_image_size (AtkImage *image,
7023                                                     gint     *width,
7024                                                     gint     *height)
7025 {
7026   GtkIconViewItemAccessible *item;
7027   GdkRectangle box;
7028
7029   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
7030
7031   if (!GTK_IS_ICON_VIEW (item->widget))
7032     return;
7033
7034   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7035     return;
7036
7037   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
7038     {
7039       *width = box.width;
7040       *height = box.height;  
7041     }
7042 }
7043
7044 static void
7045 gtk_icon_view_item_accessible_image_get_image_position (AtkImage    *image,
7046                                                         gint        *x,
7047                                                         gint        *y,
7048                                                         AtkCoordType coord_type)
7049 {
7050   GtkIconViewItemAccessible *item;
7051   GdkRectangle box;
7052
7053   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (image);
7054
7055   if (!GTK_IS_ICON_VIEW (item->widget))
7056     return;
7057
7058   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7059     return;
7060
7061   atk_component_get_position (ATK_COMPONENT (image), x, y, coord_type);
7062
7063   if (get_pixbuf_box (GTK_ICON_VIEW (item->widget), item->item, &box))
7064     {
7065       *x+= box.x - item->item->x;
7066       *y+= box.y - item->item->y;
7067     }
7068
7069 }
7070
7071 static void
7072 atk_image_item_interface_init (AtkImageIface *iface)
7073 {
7074   iface->get_image_description = gtk_icon_view_item_accessible_image_get_image_description;
7075   iface->set_image_description = gtk_icon_view_item_accessible_image_set_image_description;
7076   iface->get_image_size = gtk_icon_view_item_accessible_image_get_image_size;
7077   iface->get_image_position = gtk_icon_view_item_accessible_image_get_image_position;
7078 }
7079
7080 static gchar *
7081 gtk_icon_view_item_accessible_text_get_text (AtkText *text,
7082                                              gint     start_pos,
7083                                              gint     end_pos)
7084 {
7085   GtkIconViewItemAccessible *item;
7086   GtkTextIter start, end;
7087   GtkTextBuffer *buffer;
7088
7089   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7090
7091   if (!GTK_IS_ICON_VIEW (item->widget))
7092     return NULL;
7093
7094   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7095     return NULL;
7096
7097   buffer = item->text_buffer;
7098   gtk_text_buffer_get_iter_at_offset (buffer, &start, start_pos);
7099   if (end_pos < 0)
7100     gtk_text_buffer_get_end_iter (buffer, &end);
7101   else
7102     gtk_text_buffer_get_iter_at_offset (buffer, &end, end_pos);
7103
7104   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7105 }
7106
7107 static gunichar
7108 gtk_icon_view_item_accessible_text_get_character_at_offset (AtkText *text,
7109                                                             gint     offset)
7110 {
7111   GtkIconViewItemAccessible *item;
7112   GtkTextIter start, end;
7113   GtkTextBuffer *buffer;
7114   gchar *string;
7115   gunichar unichar;
7116
7117   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7118
7119   if (!GTK_IS_ICON_VIEW (item->widget))
7120     return '\0';
7121
7122   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7123     return '\0';
7124
7125   buffer = item->text_buffer;
7126   if (offset >= gtk_text_buffer_get_char_count (buffer))
7127     return '\0';
7128
7129   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7130   end = start;
7131   gtk_text_iter_forward_char (&end);
7132   string = gtk_text_buffer_get_slice (buffer, &start, &end, FALSE);
7133   unichar = g_utf8_get_char (string);
7134   g_free(string);
7135
7136   return unichar;
7137 }
7138
7139 #if 0
7140 static void
7141 get_pango_text_offsets (PangoLayout     *layout,
7142                         GtkTextBuffer   *buffer,
7143                         gint             function,
7144                         AtkTextBoundary  boundary_type,
7145                         gint             offset,
7146                         gint            *start_offset,
7147                         gint            *end_offset,
7148                         GtkTextIter     *start_iter,
7149                         GtkTextIter     *end_iter)
7150 {
7151   PangoLayoutIter *iter;
7152   PangoLayoutLine *line, *prev_line = NULL, *prev_prev_line = NULL;
7153   gint index, start_index, end_index;
7154   const gchar *text;
7155   gboolean found = FALSE;
7156
7157   text = pango_layout_get_text (layout);
7158   index = g_utf8_offset_to_pointer (text, offset) - text;
7159   iter = pango_layout_get_iter (layout);
7160   do
7161     {
7162       line = pango_layout_iter_get_line (iter);
7163       start_index = line->start_index;
7164       end_index = start_index + line->length;
7165
7166       if (index >= start_index && index <= end_index)
7167         {
7168           /*
7169            * Found line for offset
7170            */
7171           switch (function)
7172             {
7173             case 0:
7174                   /*
7175                    * We want the previous line
7176                    */
7177               if (prev_line)
7178                 {
7179                   switch (boundary_type)
7180                     {
7181                     case ATK_TEXT_BOUNDARY_LINE_START:
7182                       end_index = start_index;
7183                       start_index = prev_line->start_index;
7184                       break;
7185                     case ATK_TEXT_BOUNDARY_LINE_END:
7186                       if (prev_prev_line)
7187                         start_index = prev_prev_line->start_index + 
7188                                   prev_prev_line->length;
7189                       end_index = prev_line->start_index + prev_line->length;
7190                       break;
7191                     default:
7192                       g_assert_not_reached();
7193                     }
7194                 }
7195               else
7196                 start_index = end_index = 0;
7197               break;
7198             case 1:
7199               switch (boundary_type)
7200                 {
7201                 case ATK_TEXT_BOUNDARY_LINE_START:
7202                   if (pango_layout_iter_next_line (iter))
7203                     end_index = pango_layout_iter_get_line (iter)->start_index;
7204                   break;
7205                 case ATK_TEXT_BOUNDARY_LINE_END:
7206                   if (prev_line)
7207                     start_index = prev_line->start_index + 
7208                                   prev_line->length;
7209                   break;
7210                 default:
7211                   g_assert_not_reached();
7212                 }
7213               break;
7214             case 2:
7215                /*
7216                 * We want the next line
7217                 */
7218               if (pango_layout_iter_next_line (iter))
7219                 {
7220                   line = pango_layout_iter_get_line (iter);
7221                   switch (boundary_type)
7222                     {
7223                     case ATK_TEXT_BOUNDARY_LINE_START:
7224                       start_index = line->start_index;
7225                       if (pango_layout_iter_next_line (iter))
7226                         end_index = pango_layout_iter_get_line (iter)->start_index;
7227                       else
7228                         end_index = start_index + line->length;
7229                       break;
7230                     case ATK_TEXT_BOUNDARY_LINE_END:
7231                       start_index = end_index;
7232                       end_index = line->start_index + line->length;
7233                       break;
7234                     default:
7235                       g_assert_not_reached();
7236                     }
7237                 }
7238               else
7239                 start_index = end_index;
7240               break;
7241             }
7242           found = TRUE;
7243           break;
7244         }
7245       prev_prev_line = prev_line; 
7246       prev_line = line; 
7247     }
7248   while (pango_layout_iter_next_line (iter));
7249
7250   if (!found)
7251     {
7252       start_index = prev_line->start_index + prev_line->length;
7253       end_index = start_index;
7254     }
7255   pango_layout_iter_free (iter);
7256   *start_offset = g_utf8_pointer_to_offset (text, text + start_index);
7257   *end_offset = g_utf8_pointer_to_offset (text, text + end_index);
7258  
7259   gtk_text_buffer_get_iter_at_offset (buffer, start_iter, *start_offset);
7260   gtk_text_buffer_get_iter_at_offset (buffer, end_iter, *end_offset);
7261 }
7262 #endif
7263
7264 static gchar*
7265 gtk_icon_view_item_accessible_text_get_text_before_offset (AtkText         *text,
7266                                                            gint            offset,
7267                                                            AtkTextBoundary boundary_type,
7268                                                            gint            *start_offset,
7269                                                            gint            *end_offset)
7270 {
7271   GtkIconViewItemAccessible *item;
7272   GtkTextIter start, end;
7273   GtkTextBuffer *buffer;
7274 #if 0
7275   GtkIconView *icon_view;
7276 #endif
7277
7278   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7279
7280   if (!GTK_IS_ICON_VIEW (item->widget))
7281     return NULL;
7282
7283   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7284     return NULL;
7285
7286   buffer = item->text_buffer;
7287
7288   if (!gtk_text_buffer_get_char_count (buffer))
7289     {
7290       *start_offset = 0;
7291       *end_offset = 0;
7292       return g_strdup ("");
7293     }
7294   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7295    
7296   end = start;
7297
7298   switch (boundary_type)
7299     {
7300     case ATK_TEXT_BOUNDARY_CHAR:
7301       gtk_text_iter_backward_char(&start);
7302       break;
7303     case ATK_TEXT_BOUNDARY_WORD_START:
7304       if (!gtk_text_iter_starts_word (&start))
7305         gtk_text_iter_backward_word_start (&start);
7306       end = start;
7307       gtk_text_iter_backward_word_start(&start);
7308       break;
7309     case ATK_TEXT_BOUNDARY_WORD_END:
7310       if (gtk_text_iter_inside_word (&start) &&
7311           !gtk_text_iter_starts_word (&start))
7312         gtk_text_iter_backward_word_start (&start);
7313       while (!gtk_text_iter_ends_word (&start))
7314         {
7315           if (!gtk_text_iter_backward_char (&start))
7316             break;
7317         }
7318       end = start;
7319       gtk_text_iter_backward_word_start(&start);
7320       while (!gtk_text_iter_ends_word (&start))
7321         {
7322           if (!gtk_text_iter_backward_char (&start))
7323             break;
7324         }
7325       break;
7326     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7327       if (!gtk_text_iter_starts_sentence (&start))
7328         gtk_text_iter_backward_sentence_start (&start);
7329       end = start;
7330       gtk_text_iter_backward_sentence_start (&start);
7331       break;
7332     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7333       if (gtk_text_iter_inside_sentence (&start) &&
7334           !gtk_text_iter_starts_sentence (&start))
7335         gtk_text_iter_backward_sentence_start (&start);
7336       while (!gtk_text_iter_ends_sentence (&start))
7337         {
7338           if (!gtk_text_iter_backward_char (&start))
7339             break;
7340         }
7341       end = start;
7342       gtk_text_iter_backward_sentence_start (&start);
7343       while (!gtk_text_iter_ends_sentence (&start))
7344         {
7345           if (!gtk_text_iter_backward_char (&start))
7346             break;
7347         }
7348       break;
7349    case ATK_TEXT_BOUNDARY_LINE_START:
7350    case ATK_TEXT_BOUNDARY_LINE_END:
7351 #if 0
7352       icon_view = GTK_ICON_VIEW (item->widget);
7353       /* FIXME we probably have to use GailTextCell to salvage this */
7354       gtk_icon_view_update_item_text (icon_view, item->item);
7355       get_pango_text_offsets (icon_view->priv->layout,
7356                               buffer,
7357                               0,
7358                               boundary_type,
7359                               offset,
7360                               start_offset,
7361                               end_offset,
7362                               &start,
7363                               &end);
7364 #endif
7365       break;
7366     }
7367
7368   *start_offset = gtk_text_iter_get_offset (&start);
7369   *end_offset = gtk_text_iter_get_offset (&end);
7370
7371   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7372 }
7373
7374 static gchar*
7375 gtk_icon_view_item_accessible_text_get_text_at_offset (AtkText         *text,
7376                                                        gint            offset,
7377                                                        AtkTextBoundary boundary_type,
7378                                                        gint            *start_offset,
7379                                                        gint            *end_offset)
7380 {
7381   GtkIconViewItemAccessible *item;
7382   GtkTextIter start, end;
7383   GtkTextBuffer *buffer;
7384 #if 0
7385   GtkIconView *icon_view;
7386 #endif
7387
7388   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7389
7390   if (!GTK_IS_ICON_VIEW (item->widget))
7391     return NULL;
7392
7393   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7394     return NULL;
7395
7396   buffer = item->text_buffer;
7397
7398   if (!gtk_text_buffer_get_char_count (buffer))
7399     {
7400       *start_offset = 0;
7401       *end_offset = 0;
7402       return g_strdup ("");
7403     }
7404   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7405    
7406   end = start;
7407
7408   switch (boundary_type)
7409     {
7410     case ATK_TEXT_BOUNDARY_CHAR:
7411       gtk_text_iter_forward_char (&end);
7412       break;
7413     case ATK_TEXT_BOUNDARY_WORD_START:
7414       if (!gtk_text_iter_starts_word (&start))
7415         gtk_text_iter_backward_word_start (&start);
7416       if (gtk_text_iter_inside_word (&end))
7417         gtk_text_iter_forward_word_end (&end);
7418       while (!gtk_text_iter_starts_word (&end))
7419         {
7420           if (!gtk_text_iter_forward_char (&end))
7421             break;
7422         }
7423       break;
7424     case ATK_TEXT_BOUNDARY_WORD_END:
7425       if (gtk_text_iter_inside_word (&start) &&
7426           !gtk_text_iter_starts_word (&start))
7427         gtk_text_iter_backward_word_start (&start);
7428       while (!gtk_text_iter_ends_word (&start))
7429         {
7430           if (!gtk_text_iter_backward_char (&start))
7431             break;
7432         }
7433       gtk_text_iter_forward_word_end (&end);
7434       break;
7435     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7436       if (!gtk_text_iter_starts_sentence (&start))
7437         gtk_text_iter_backward_sentence_start (&start);
7438       if (gtk_text_iter_inside_sentence (&end))
7439         gtk_text_iter_forward_sentence_end (&end);
7440       while (!gtk_text_iter_starts_sentence (&end))
7441         {
7442           if (!gtk_text_iter_forward_char (&end))
7443             break;
7444         }
7445       break;
7446     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7447       if (gtk_text_iter_inside_sentence (&start) &&
7448           !gtk_text_iter_starts_sentence (&start))
7449         gtk_text_iter_backward_sentence_start (&start);
7450       while (!gtk_text_iter_ends_sentence (&start))
7451         {
7452           if (!gtk_text_iter_backward_char (&start))
7453             break;
7454         }
7455       gtk_text_iter_forward_sentence_end (&end);
7456       break;
7457    case ATK_TEXT_BOUNDARY_LINE_START:
7458    case ATK_TEXT_BOUNDARY_LINE_END:
7459 #if 0
7460       icon_view = GTK_ICON_VIEW (item->widget);
7461       /* FIXME we probably have to use GailTextCell to salvage this */
7462       gtk_icon_view_update_item_text (icon_view, item->item);
7463       get_pango_text_offsets (icon_view->priv->layout,
7464                               buffer,
7465                               1,
7466                               boundary_type,
7467                               offset,
7468                               start_offset,
7469                               end_offset,
7470                               &start,
7471                               &end);
7472 #endif
7473       break;
7474     }
7475
7476
7477   *start_offset = gtk_text_iter_get_offset (&start);
7478   *end_offset = gtk_text_iter_get_offset (&end);
7479
7480   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7481 }
7482
7483 static gchar*
7484 gtk_icon_view_item_accessible_text_get_text_after_offset (AtkText         *text,
7485                                                           gint            offset,
7486                                                           AtkTextBoundary boundary_type,
7487                                                           gint            *start_offset,
7488                                                           gint            *end_offset)
7489 {
7490   GtkIconViewItemAccessible *item;
7491   GtkTextIter start, end;
7492   GtkTextBuffer *buffer;
7493 #if 0
7494   GtkIconView *icon_view;
7495 #endif
7496
7497   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7498
7499   if (!GTK_IS_ICON_VIEW (item->widget))
7500     return NULL;
7501
7502   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7503     return NULL;
7504
7505   buffer = item->text_buffer;
7506
7507   if (!gtk_text_buffer_get_char_count (buffer))
7508     {
7509       *start_offset = 0;
7510       *end_offset = 0;
7511       return g_strdup ("");
7512     }
7513   gtk_text_buffer_get_iter_at_offset (buffer, &start, offset);
7514    
7515   end = start;
7516
7517   switch (boundary_type)
7518     {
7519     case ATK_TEXT_BOUNDARY_CHAR:
7520       gtk_text_iter_forward_char(&start);
7521       gtk_text_iter_forward_chars(&end, 2);
7522       break;
7523     case ATK_TEXT_BOUNDARY_WORD_START:
7524       if (gtk_text_iter_inside_word (&end))
7525         gtk_text_iter_forward_word_end (&end);
7526       while (!gtk_text_iter_starts_word (&end))
7527         {
7528           if (!gtk_text_iter_forward_char (&end))
7529             break;
7530         }
7531       start = end;
7532       if (!gtk_text_iter_is_end (&end))
7533         {
7534           gtk_text_iter_forward_word_end (&end);
7535           while (!gtk_text_iter_starts_word (&end))
7536             {
7537               if (!gtk_text_iter_forward_char (&end))
7538                 break;
7539             }
7540         }
7541       break;
7542     case ATK_TEXT_BOUNDARY_WORD_END:
7543       gtk_text_iter_forward_word_end (&end);
7544       start = end;
7545       if (!gtk_text_iter_is_end (&end))
7546         gtk_text_iter_forward_word_end (&end);
7547       break;
7548     case ATK_TEXT_BOUNDARY_SENTENCE_START:
7549       if (gtk_text_iter_inside_sentence (&end))
7550         gtk_text_iter_forward_sentence_end (&end);
7551       while (!gtk_text_iter_starts_sentence (&end))
7552         {
7553           if (!gtk_text_iter_forward_char (&end))
7554             break;
7555         }
7556       start = end;
7557       if (!gtk_text_iter_is_end (&end))
7558         {
7559           gtk_text_iter_forward_sentence_end (&end);
7560           while (!gtk_text_iter_starts_sentence (&end))
7561             {
7562               if (!gtk_text_iter_forward_char (&end))
7563                 break;
7564             }
7565         }
7566       break;
7567     case ATK_TEXT_BOUNDARY_SENTENCE_END:
7568       gtk_text_iter_forward_sentence_end (&end);
7569       start = end;
7570       if (!gtk_text_iter_is_end (&end))
7571         gtk_text_iter_forward_sentence_end (&end);
7572       break;
7573    case ATK_TEXT_BOUNDARY_LINE_START:
7574    case ATK_TEXT_BOUNDARY_LINE_END:
7575 #if 0
7576       icon_view = GTK_ICON_VIEW (item->widget);
7577       /* FIXME we probably have to use GailTextCell to salvage this */
7578       gtk_icon_view_update_item_text (icon_view, item->item);
7579       get_pango_text_offsets (icon_view->priv->layout,
7580                               buffer,
7581                               2,
7582                               boundary_type,
7583                               offset,
7584                               start_offset,
7585                               end_offset,
7586                               &start,
7587                               &end);
7588 #endif
7589       break;
7590     }
7591   *start_offset = gtk_text_iter_get_offset (&start);
7592   *end_offset = gtk_text_iter_get_offset (&end);
7593
7594   return gtk_text_buffer_get_text (buffer, &start, &end, FALSE);
7595 }
7596
7597 static gint
7598 gtk_icon_view_item_accessible_text_get_character_count (AtkText *text)
7599 {
7600   GtkIconViewItemAccessible *item;
7601
7602   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7603
7604   if (!GTK_IS_ICON_VIEW (item->widget))
7605     return 0;
7606
7607   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7608     return 0;
7609
7610   return gtk_text_buffer_get_char_count (item->text_buffer);
7611 }
7612
7613 static void
7614 gtk_icon_view_item_accessible_text_get_character_extents (AtkText      *text,
7615                                                           gint         offset,
7616                                                           gint         *x,
7617                                                           gint         *y,
7618                                                           gint         *width,
7619                                                           gint         *height,
7620                                                           AtkCoordType coord_type)
7621 {
7622   GtkIconViewItemAccessible *item;
7623 #if 0
7624   GtkIconView *icon_view;
7625   PangoRectangle char_rect;
7626   const gchar *item_text;
7627   gint index;
7628 #endif
7629
7630   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7631
7632   if (!GTK_IS_ICON_VIEW (item->widget))
7633     return;
7634
7635   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7636     return;
7637
7638 #if 0
7639   icon_view = GTK_ICON_VIEW (item->widget);
7640       /* FIXME we probably have to use GailTextCell to salvage this */
7641   gtk_icon_view_update_item_text (icon_view, item->item);
7642   item_text = pango_layout_get_text (icon_view->priv->layout);
7643   index = g_utf8_offset_to_pointer (item_text, offset) - item_text;
7644   pango_layout_index_to_pos (icon_view->priv->layout, index, &char_rect);
7645
7646   atk_component_get_position (ATK_COMPONENT (text), x, y, coord_type);
7647   *x += item->item->layout_x - item->item->x + char_rect.x / PANGO_SCALE;
7648   /* Look at gtk_icon_view_paint_item() to see where the text is. */
7649   *x -=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
7650   *y += item->item->layout_y - item->item->y + char_rect.y / PANGO_SCALE;
7651   *width = char_rect.width / PANGO_SCALE;
7652   *height = char_rect.height / PANGO_SCALE;
7653 #endif
7654 }
7655
7656 static gint
7657 gtk_icon_view_item_accessible_text_get_offset_at_point (AtkText      *text,
7658                                                         gint          x,
7659                                                         gint          y,
7660                                                         AtkCoordType coord_type)
7661 {
7662   GtkIconViewItemAccessible *item;
7663   gint offset = 0;
7664 #if 0
7665   GtkIconView *icon_view;
7666   const gchar *item_text;
7667   gint index;
7668   gint l_x, l_y;
7669 #endif
7670
7671   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (text);
7672
7673   if (!GTK_IS_ICON_VIEW (item->widget))
7674     return -1;
7675
7676   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7677     return -1;
7678
7679 #if 0
7680   icon_view = GTK_ICON_VIEW (item->widget);
7681       /* FIXME we probably have to use GailTextCell to salvage this */
7682   gtk_icon_view_update_item_text (icon_view, item->item);
7683   atk_component_get_position (ATK_COMPONENT (text), &l_x, &l_y, coord_type);
7684   x -= l_x + item->item->layout_x - item->item->x;
7685   x +=  ((item->item->width - item->item->layout_width) / 2) + (MAX (item->item->pixbuf_width, icon_view->priv->item_width) - item->item->width) / 2,
7686   y -= l_y + item->item->layout_y - item->item->y;
7687   item_text = pango_layout_get_text (icon_view->priv->layout);
7688   if (!pango_layout_xy_to_index (icon_view->priv->layout, 
7689                                 x * PANGO_SCALE,
7690                                 y * PANGO_SCALE,
7691                                 &index, NULL))
7692     {
7693       if (x < 0 || y < 0)
7694         index = 0;
7695       else
7696         index = -1;
7697     } 
7698   if (index == -1)
7699     offset = g_utf8_strlen (item_text, -1);
7700   else
7701     offset = g_utf8_pointer_to_offset (item_text, item_text + index);
7702 #endif
7703   return offset;
7704 }
7705
7706 static void
7707 atk_text_item_interface_init (AtkTextIface *iface)
7708 {
7709   iface->get_text = gtk_icon_view_item_accessible_text_get_text;
7710   iface->get_character_at_offset = gtk_icon_view_item_accessible_text_get_character_at_offset;
7711   iface->get_text_before_offset = gtk_icon_view_item_accessible_text_get_text_before_offset;
7712   iface->get_text_at_offset = gtk_icon_view_item_accessible_text_get_text_at_offset;
7713   iface->get_text_after_offset = gtk_icon_view_item_accessible_text_get_text_after_offset;
7714   iface->get_character_count = gtk_icon_view_item_accessible_text_get_character_count;
7715   iface->get_character_extents = gtk_icon_view_item_accessible_text_get_character_extents;
7716   iface->get_offset_at_point = gtk_icon_view_item_accessible_text_get_offset_at_point;
7717 }
7718
7719 static void
7720 gtk_icon_view_item_accessible_get_extents (AtkComponent *component,
7721                                            gint         *x,
7722                                            gint         *y,
7723                                            gint         *width,
7724                                            gint         *height,
7725                                            AtkCoordType  coord_type)
7726 {
7727   GtkIconViewItemAccessible *item;
7728   AtkObject *parent_obj;
7729   gint l_x, l_y;
7730
7731   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component));
7732
7733   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
7734   if (!GTK_IS_WIDGET (item->widget))
7735     return;
7736
7737   if (atk_state_set_contains_state (item->state_set, ATK_STATE_DEFUNCT))
7738     return;
7739
7740   *width = item->item->width;
7741   *height = item->item->height;
7742   if (gtk_icon_view_item_accessible_is_showing (item))
7743     {
7744       parent_obj = gtk_widget_get_accessible (item->widget);
7745       atk_component_get_position (ATK_COMPONENT (parent_obj), &l_x, &l_y, coord_type);
7746       *x = l_x + item->item->x;
7747       *y = l_y + item->item->y;
7748     }
7749   else
7750     {
7751       *x = G_MININT;
7752       *y = G_MININT;
7753     }
7754 }
7755
7756 static gboolean
7757 gtk_icon_view_item_accessible_grab_focus (AtkComponent *component)
7758 {
7759   GtkIconViewItemAccessible *item;
7760   GtkWidget *toplevel;
7761
7762   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (component), FALSE);
7763
7764   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (component);
7765   if (!GTK_IS_WIDGET (item->widget))
7766     return FALSE;
7767
7768   gtk_widget_grab_focus (item->widget);
7769   gtk_icon_view_set_cursor_item (GTK_ICON_VIEW (item->widget), item->item, -1);
7770   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (item->widget));
7771   if (GTK_WIDGET_TOPLEVEL (toplevel))
7772     gtk_window_present (GTK_WINDOW (toplevel));
7773
7774   return TRUE;
7775 }
7776
7777 static void
7778 atk_component_item_interface_init (AtkComponentIface *iface)
7779 {
7780   iface->get_extents = gtk_icon_view_item_accessible_get_extents;
7781   iface->grab_focus = gtk_icon_view_item_accessible_grab_focus;
7782 }
7783
7784 static gboolean
7785 gtk_icon_view_item_accessible_add_state (GtkIconViewItemAccessible *item,
7786                                          AtkStateType               state_type,
7787                                          gboolean                   emit_signal)
7788 {
7789   gboolean rc;
7790
7791   rc = atk_state_set_add_state (item->state_set, state_type);
7792   /*
7793    * The signal should only be generated if the value changed,
7794    * not when the item is set up.  So states that are set
7795    * initially should pass FALSE as the emit_signal argument.
7796    */
7797
7798   if (emit_signal)
7799     {
7800       atk_object_notify_state_change (ATK_OBJECT (item), state_type, TRUE);
7801       /* If state_type is ATK_STATE_VISIBLE, additional notification */
7802       if (state_type == ATK_STATE_VISIBLE)
7803         g_signal_emit_by_name (item, "visible_data_changed");
7804     }
7805
7806   return rc;
7807 }
7808
7809 static gboolean
7810 gtk_icon_view_item_accessible_remove_state (GtkIconViewItemAccessible *item,
7811                                             AtkStateType               state_type,
7812                                             gboolean                   emit_signal)
7813 {
7814   if (atk_state_set_contains_state (item->state_set, state_type))
7815     {
7816       gboolean rc;
7817
7818       rc = atk_state_set_remove_state (item->state_set, state_type);
7819       /*
7820        * The signal should only be generated if the value changed,
7821        * not when the item is set up.  So states that are set
7822        * initially should pass FALSE as the emit_signal argument.
7823        */
7824
7825       if (emit_signal)
7826         {
7827           atk_object_notify_state_change (ATK_OBJECT (item), state_type, FALSE);
7828           /* If state_type is ATK_STATE_VISIBLE, additional notification */
7829           if (state_type == ATK_STATE_VISIBLE)
7830             g_signal_emit_by_name (item, "visible_data_changed");
7831         }
7832
7833       return rc;
7834     }
7835   else
7836     return FALSE;
7837 }
7838
7839 static gboolean
7840 gtk_icon_view_item_accessible_is_showing (GtkIconViewItemAccessible *item)
7841 {
7842   GtkIconView *icon_view;
7843   GdkRectangle visible_rect;
7844   gboolean is_showing;
7845
7846   /*
7847    * An item is considered "SHOWING" if any part of the item is in the
7848    * visible rectangle.
7849    */
7850
7851   if (!GTK_IS_ICON_VIEW (item->widget))
7852     return FALSE;
7853
7854   if (item->item == NULL)
7855     return FALSE;
7856
7857   icon_view = GTK_ICON_VIEW (item->widget);
7858   visible_rect.x = 0;
7859   if (icon_view->priv->hadjustment)
7860     visible_rect.x += icon_view->priv->hadjustment->value;
7861   visible_rect.y = 0;
7862   if (icon_view->priv->hadjustment)
7863     visible_rect.y += icon_view->priv->vadjustment->value;
7864   visible_rect.width = item->widget->allocation.width;
7865   visible_rect.height = item->widget->allocation.height;
7866
7867   if (((item->item->x + item->item->width) < visible_rect.x) ||
7868      ((item->item->y + item->item->height) < (visible_rect.y)) ||
7869      (item->item->x > (visible_rect.x + visible_rect.width)) ||
7870      (item->item->y > (visible_rect.y + visible_rect.height)))
7871     is_showing =  FALSE;
7872   else
7873     is_showing = TRUE;
7874
7875   return is_showing;
7876 }
7877
7878 static gboolean
7879 gtk_icon_view_item_accessible_set_visibility (GtkIconViewItemAccessible *item,
7880                                               gboolean                   emit_signal)
7881 {
7882   if (gtk_icon_view_item_accessible_is_showing (item))
7883     return gtk_icon_view_item_accessible_add_state (item, ATK_STATE_SHOWING,
7884                                                     emit_signal);
7885   else
7886     return gtk_icon_view_item_accessible_remove_state (item, ATK_STATE_SHOWING,
7887                                                        emit_signal);
7888 }
7889
7890 static void
7891 gtk_icon_view_item_accessible_object_init (GtkIconViewItemAccessible *item)
7892 {
7893   gint i;
7894
7895   item->state_set = atk_state_set_new ();
7896
7897   atk_state_set_add_state (item->state_set, ATK_STATE_ENABLED);
7898   atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSABLE);
7899   atk_state_set_add_state (item->state_set, ATK_STATE_SENSITIVE);
7900   atk_state_set_add_state (item->state_set, ATK_STATE_SELECTABLE);
7901   atk_state_set_add_state (item->state_set, ATK_STATE_VISIBLE);
7902
7903   for (i = 0; i < LAST_ACTION; i++)
7904     item->action_descriptions[i] = NULL;
7905
7906   item->image_description = NULL;
7907
7908   item->action_idle_handler = 0;
7909 }
7910
7911 static void
7912 gtk_icon_view_item_accessible_finalize (GObject *object)
7913 {
7914   GtkIconViewItemAccessible *item;
7915   gint i;
7916
7917   g_return_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (object));
7918
7919   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (object);
7920
7921   if (item->widget)
7922     g_object_remove_weak_pointer (G_OBJECT (item->widget), (gpointer) &item->widget);
7923
7924   if (item->state_set)
7925     g_object_unref (item->state_set);
7926
7927   if (item->text_buffer)
7928      g_object_unref (item->text_buffer);
7929
7930   for (i = 0; i < LAST_ACTION; i++)
7931     g_free (item->action_descriptions[i]);
7932
7933   g_free (item->image_description);
7934
7935   if (item->action_idle_handler)
7936     {
7937       g_source_remove (item->action_idle_handler);
7938       item->action_idle_handler = 0;
7939     }
7940
7941   G_OBJECT_CLASS (accessible_item_parent_class)->finalize (object);
7942 }
7943
7944 static G_CONST_RETURN gchar*
7945 gtk_icon_view_item_accessible_get_name (AtkObject *obj)
7946 {
7947   if (obj->name)
7948     return obj->name;
7949   else
7950     {
7951       GtkIconViewItemAccessible *item;
7952       GtkTextIter start_iter;
7953       GtkTextIter end_iter;
7954
7955       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7956  
7957       gtk_text_buffer_get_start_iter (item->text_buffer, &start_iter); 
7958       gtk_text_buffer_get_end_iter (item->text_buffer, &end_iter); 
7959
7960       return gtk_text_buffer_get_text (item->text_buffer, &start_iter, &end_iter, FALSE);
7961     }
7962 }
7963
7964 static AtkObject*
7965 gtk_icon_view_item_accessible_get_parent (AtkObject *obj)
7966 {
7967   GtkIconViewItemAccessible *item;
7968
7969   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), NULL);
7970   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7971
7972   if (item->widget)
7973     return gtk_widget_get_accessible (item->widget);
7974   else
7975     return NULL;
7976 }
7977
7978 static gint
7979 gtk_icon_view_item_accessible_get_index_in_parent (AtkObject *obj)
7980 {
7981   GtkIconViewItemAccessible *item;
7982
7983   g_return_val_if_fail (GTK_IS_ICON_VIEW_ITEM_ACCESSIBLE (obj), 0);
7984   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7985
7986   return item->item->index; 
7987 }
7988
7989 static AtkStateSet *
7990 gtk_icon_view_item_accessible_ref_state_set (AtkObject *obj)
7991 {
7992   GtkIconViewItemAccessible *item;
7993   GtkIconView *icon_view;
7994
7995   item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
7996   g_return_val_if_fail (item->state_set, NULL);
7997
7998   if (!item->widget)
7999     return NULL;
8000
8001   icon_view = GTK_ICON_VIEW (item->widget);
8002   if (icon_view->priv->cursor_item == item->item)
8003     atk_state_set_add_state (item->state_set, ATK_STATE_FOCUSED);
8004   else
8005     atk_state_set_remove_state (item->state_set, ATK_STATE_FOCUSED);
8006
8007   return g_object_ref (item->state_set);
8008 }
8009
8010 static void
8011 gtk_icon_view_item_accessible_class_init (AtkObjectClass *klass)
8012 {
8013   GObjectClass *gobject_class;
8014
8015   accessible_item_parent_class = g_type_class_peek_parent (klass);
8016
8017   gobject_class = (GObjectClass *)klass;
8018
8019   gobject_class->finalize = gtk_icon_view_item_accessible_finalize;
8020
8021   klass->get_index_in_parent = gtk_icon_view_item_accessible_get_index_in_parent; 
8022   klass->get_name = gtk_icon_view_item_accessible_get_name; 
8023   klass->get_parent = gtk_icon_view_item_accessible_get_parent; 
8024   klass->ref_state_set = gtk_icon_view_item_accessible_ref_state_set; 
8025 }
8026
8027 static GType
8028 gtk_icon_view_item_accessible_get_type (void)
8029 {
8030   static GType type = 0;
8031
8032   if (!type)
8033     {
8034       static const GTypeInfo tinfo =
8035       {
8036         sizeof (GtkIconViewItemAccessibleClass),
8037         (GBaseInitFunc) NULL, /* base init */
8038         (GBaseFinalizeFunc) NULL, /* base finalize */
8039         (GClassInitFunc) gtk_icon_view_item_accessible_class_init, /* class init */
8040         (GClassFinalizeFunc) NULL, /* class finalize */
8041         NULL, /* class data */
8042         sizeof (GtkIconViewItemAccessible), /* instance size */
8043         0, /* nb preallocs */
8044         (GInstanceInitFunc) gtk_icon_view_item_accessible_object_init, /* instance init */
8045         NULL /* value table */
8046       };
8047
8048       static const GInterfaceInfo atk_component_info =
8049       {
8050         (GInterfaceInitFunc) atk_component_item_interface_init,
8051         (GInterfaceFinalizeFunc) NULL,
8052         NULL
8053       };
8054       static const GInterfaceInfo atk_action_info =
8055       {
8056         (GInterfaceInitFunc) atk_action_item_interface_init,
8057         (GInterfaceFinalizeFunc) NULL,
8058         NULL
8059       };
8060       static const GInterfaceInfo atk_image_info =
8061       {
8062         (GInterfaceInitFunc) atk_image_item_interface_init,
8063         (GInterfaceFinalizeFunc) NULL,
8064         NULL
8065       };
8066       static const GInterfaceInfo atk_text_info =
8067       {
8068         (GInterfaceInitFunc) atk_text_item_interface_init,
8069         (GInterfaceFinalizeFunc) NULL,
8070         NULL
8071       };
8072
8073       type = g_type_register_static (ATK_TYPE_OBJECT,
8074                                      I_("GtkIconViewItemAccessible"), &tinfo, 0);
8075       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
8076                                    &atk_component_info);
8077       g_type_add_interface_static (type, ATK_TYPE_ACTION,
8078                                    &atk_action_info);
8079       g_type_add_interface_static (type, ATK_TYPE_IMAGE,
8080                                    &atk_image_info);
8081       g_type_add_interface_static (type, ATK_TYPE_TEXT,
8082                                    &atk_text_info);
8083     }
8084
8085   return type;
8086 }
8087
8088 #define GTK_TYPE_ICON_VIEW_ACCESSIBLE      (gtk_icon_view_accessible_get_type ())
8089 #define GTK_ICON_VIEW_ACCESSIBLE(obj)      (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE, GtkIconViewAccessible))
8090 #define GTK_IS_ICON_VIEW_ACCESSIBLE(obj)   (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ICON_VIEW_ACCESSIBLE))
8091
8092 static GType gtk_icon_view_accessible_get_type (void);
8093
8094 typedef struct
8095 {
8096    AtkObject parent;
8097 } GtkIconViewAccessible;
8098
8099 typedef struct
8100 {
8101   AtkObject *item;
8102   gint       index;
8103 } GtkIconViewItemAccessibleInfo;
8104
8105 typedef struct
8106 {
8107   GList *items;
8108
8109   GtkAdjustment *old_hadj;
8110   GtkAdjustment *old_vadj;
8111
8112   GtkTreeModel *model;
8113
8114 } GtkIconViewAccessiblePrivate;
8115
8116 static GtkIconViewAccessiblePrivate *
8117 gtk_icon_view_accessible_get_priv (AtkObject *accessible)
8118 {
8119   return g_object_get_qdata (G_OBJECT (accessible),
8120                              accessible_private_data_quark);
8121 }
8122
8123 static void
8124 gtk_icon_view_item_accessible_info_new (AtkObject *accessible,
8125                                         AtkObject *item,
8126                                         gint       index)
8127 {
8128   GtkIconViewItemAccessibleInfo *info;
8129   GtkIconViewItemAccessibleInfo *tmp_info;
8130   GtkIconViewAccessiblePrivate *priv;
8131   GList *items;
8132
8133   info = g_new (GtkIconViewItemAccessibleInfo, 1);
8134   info->item = item;
8135   info->index = index;
8136
8137   priv = gtk_icon_view_accessible_get_priv (accessible);
8138   items = priv->items;
8139   while (items)
8140     {
8141       tmp_info = items->data;
8142       if (tmp_info->index > index)
8143         break;
8144       items = items->next;
8145     }
8146   priv->items = g_list_insert_before (priv->items, items, info);
8147   priv->old_hadj = NULL;
8148   priv->old_vadj = NULL;
8149 }
8150
8151 static gint
8152 gtk_icon_view_accessible_get_n_children (AtkObject *accessible)
8153 {
8154   GtkIconView *icon_view;
8155   GtkWidget *widget;
8156
8157   widget = GTK_ACCESSIBLE (accessible)->widget;
8158   if (!widget)
8159       return 0;
8160
8161   icon_view = GTK_ICON_VIEW (widget);
8162
8163   return g_list_length (icon_view->priv->items);
8164 }
8165
8166 static AtkObject *
8167 gtk_icon_view_accessible_find_child (AtkObject *accessible,
8168                                      gint       index)
8169 {
8170   GtkIconViewAccessiblePrivate *priv;
8171   GtkIconViewItemAccessibleInfo *info;
8172   GList *items;
8173
8174   priv = gtk_icon_view_accessible_get_priv (accessible);
8175   items = priv->items;
8176
8177   while (items)
8178     {
8179       info = items->data;
8180       if (info->index == index)
8181         return info->item;
8182       items = items->next; 
8183     }
8184   return NULL;
8185 }
8186
8187 static AtkObject *
8188 gtk_icon_view_accessible_ref_child (AtkObject *accessible,
8189                                     gint       index)
8190 {
8191   GtkIconView *icon_view;
8192   GtkWidget *widget;
8193   GList *icons;
8194   AtkObject *obj;
8195   GtkIconViewItemAccessible *a11y_item;
8196
8197   widget = GTK_ACCESSIBLE (accessible)->widget;
8198   if (!widget)
8199     return NULL;
8200
8201   icon_view = GTK_ICON_VIEW (widget);
8202   icons = g_list_nth (icon_view->priv->items, index);
8203   obj = NULL;
8204   if (icons)
8205     {
8206       GtkIconViewItem *item = icons->data;
8207    
8208       g_return_val_if_fail (item->index == index, NULL);
8209       obj = gtk_icon_view_accessible_find_child (accessible, index);
8210       if (!obj)
8211         {
8212           obj = g_object_new (gtk_icon_view_item_accessible_get_type (), NULL);
8213           gtk_icon_view_item_accessible_info_new (accessible,
8214                                                   obj,
8215                                                   index);
8216           obj->role = ATK_ROLE_ICON;
8217           a11y_item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (obj);
8218           a11y_item->item = item;
8219           a11y_item->widget = widget;
8220           a11y_item->text_buffer = gtk_text_buffer_new (NULL);
8221
8222           gtk_icon_view_set_cell_data (icon_view, item);
8223           gtk_text_buffer_set_text (a11y_item->text_buffer, 
8224                                     get_text (icon_view, item), -1);
8225
8226           gtk_icon_view_item_accessible_set_visibility (a11y_item, FALSE);
8227           g_object_add_weak_pointer (G_OBJECT (widget), (gpointer) &(a11y_item->widget));
8228        }
8229       g_object_ref (obj);
8230     }
8231   return obj;
8232 }
8233
8234 static void
8235 gtk_icon_view_accessible_traverse_items (GtkIconViewAccessible *view,
8236                                          GList                 *list)
8237 {
8238   GtkIconViewAccessiblePrivate *priv;
8239   GtkIconViewItemAccessibleInfo *info;
8240   GtkIconViewItemAccessible *item;
8241   GList *items;
8242   
8243   priv =  gtk_icon_view_accessible_get_priv (ATK_OBJECT (view));
8244   if (priv->items)
8245     {
8246       GtkWidget *widget;
8247       gboolean act_on_item;
8248
8249       widget = GTK_ACCESSIBLE (view)->widget;
8250       if (widget == NULL)
8251         return;
8252
8253       items = priv->items;
8254
8255       act_on_item = (list == NULL);
8256
8257       while (items)
8258         {
8259
8260           info = (GtkIconViewItemAccessibleInfo *)items->data;
8261           item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8262
8263           if (act_on_item == FALSE && list == items)
8264             act_on_item = TRUE;
8265
8266           if (act_on_item)
8267             gtk_icon_view_item_accessible_set_visibility (item, TRUE);
8268
8269           items = items->next;
8270        }
8271    }
8272 }
8273
8274 static void
8275 gtk_icon_view_accessible_adjustment_changed (GtkAdjustment *adjustment,
8276                                              GtkIconView   *icon_view)
8277 {
8278   AtkObject *obj;
8279   GtkIconViewAccessible *view;
8280
8281   /*
8282    * The scrollbars have changed
8283    */
8284   obj = gtk_widget_get_accessible (GTK_WIDGET (icon_view));
8285   view = GTK_ICON_VIEW_ACCESSIBLE (obj);
8286
8287   gtk_icon_view_accessible_traverse_items (view, NULL);
8288 }
8289
8290 static void
8291 gtk_icon_view_accessible_set_scroll_adjustments (GtkWidget      *widget,
8292                                                  GtkAdjustment *hadj,
8293                                                  GtkAdjustment *vadj)
8294 {
8295   AtkObject *atk_obj;
8296   GtkIconViewAccessiblePrivate *priv;
8297
8298   atk_obj = gtk_widget_get_accessible (widget);
8299   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8300
8301   if (priv->old_hadj != hadj)
8302     {
8303       if (priv->old_hadj)
8304         {
8305           g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8306                                         (gpointer *)&priv->old_hadj);
8307           
8308           g_signal_handlers_disconnect_by_func (priv->old_hadj,
8309                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8310                                                 widget);
8311         }
8312       priv->old_hadj = hadj;
8313       if (priv->old_hadj)
8314         {
8315           g_object_add_weak_pointer (G_OBJECT (priv->old_hadj),
8316                                      (gpointer *)&priv->old_hadj);
8317           g_signal_connect (hadj,
8318                             "value-changed",
8319                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8320                             widget);
8321         }
8322     }
8323   if (priv->old_vadj != vadj)
8324     {
8325       if (priv->old_vadj)
8326         {
8327           g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8328                                         (gpointer *)&priv->old_vadj);
8329           
8330           g_signal_handlers_disconnect_by_func (priv->old_vadj,
8331                                                 (gpointer) gtk_icon_view_accessible_adjustment_changed,
8332                                                 widget);
8333         }
8334       priv->old_vadj = vadj;
8335       if (priv->old_vadj)
8336         {
8337           g_object_add_weak_pointer (G_OBJECT (priv->old_vadj),
8338                                      (gpointer *)&priv->old_vadj);
8339           g_signal_connect (vadj,
8340                             "value-changed",
8341                             G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8342                             widget);
8343         }
8344     }
8345 }
8346
8347 static void
8348 gtk_icon_view_accessible_model_row_changed (GtkTreeModel *tree_model,
8349                                             GtkTreePath  *path,
8350                                             GtkTreeIter  *iter,
8351                                             gpointer     user_data)
8352 {
8353   AtkObject *atk_obj;
8354
8355   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8356   g_signal_emit_by_name (atk_obj, "visible-data-changed");
8357
8358   return;
8359 }
8360
8361 static void
8362 gtk_icon_view_accessible_model_row_inserted (GtkTreeModel *tree_model,
8363                                              GtkTreePath  *path,
8364                                              GtkTreeIter  *iter,
8365                                              gpointer     user_data)
8366 {
8367   GtkIconViewAccessiblePrivate *priv;
8368   GtkIconViewItemAccessibleInfo *info;
8369   GtkIconViewAccessible *view;
8370   GtkIconViewItemAccessible *item;
8371   GList *items;
8372   GList *tmp_list;
8373   AtkObject *atk_obj;
8374   gint index;
8375
8376   index = gtk_tree_path_get_indices(path)[0];
8377   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8378   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8379   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8380
8381   items = priv->items;
8382   tmp_list = NULL;
8383   while (items)
8384     {
8385       info = items->data;
8386       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8387       if (info->index != item->item->index)
8388         {
8389           if (info->index < index)
8390             g_warning ("Unexpected index value on insertion %d %d", index, info->index);
8391  
8392           if (tmp_list == NULL)
8393             tmp_list = items;
8394    
8395           info->index = item->item->index;
8396         }
8397
8398       items = items->next;
8399     }
8400   gtk_icon_view_accessible_traverse_items (view, tmp_list);
8401   g_signal_emit_by_name (atk_obj, "children_changed::add",
8402                          index, NULL, NULL);
8403   return;
8404 }
8405
8406 static void
8407 gtk_icon_view_accessible_model_row_deleted (GtkTreeModel *tree_model,
8408                                             GtkTreePath  *path,
8409                                             gpointer     user_data)
8410 {
8411   GtkIconViewAccessiblePrivate *priv;
8412   GtkIconViewItemAccessibleInfo *info;
8413   GtkIconViewAccessible *view;
8414   GtkIconViewItemAccessible *item;
8415   GList *items;
8416   GList *tmp_list;
8417   GList *deleted_item;
8418   AtkObject *atk_obj;
8419   gint index;
8420
8421   index = gtk_tree_path_get_indices(path)[0];
8422   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8423   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8424   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8425
8426   items = priv->items;
8427   tmp_list = NULL;
8428   deleted_item = NULL;
8429   info = NULL;
8430   while (items)
8431     {
8432       info = items->data;
8433       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8434       if (info->index == index)
8435         {
8436           deleted_item = items;
8437         }
8438       if (info->index != item->item->index)
8439         {
8440           if (tmp_list == NULL)
8441             tmp_list = items;
8442           else    
8443             info->index = item->item->index;
8444         }
8445
8446       items = items->next;
8447     }
8448   gtk_icon_view_accessible_traverse_items (view, tmp_list);
8449   if (deleted_item)
8450     {
8451       info = deleted_item->data;
8452       gtk_icon_view_item_accessible_add_state (GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item), ATK_STATE_DEFUNCT, TRUE);
8453     }
8454   g_signal_emit_by_name (atk_obj, "children_changed::remove",
8455                          index, NULL, NULL);
8456   if (deleted_item)
8457     {
8458       priv->items = g_list_remove_link (priv->items, deleted_item);
8459       g_free (info);
8460     }
8461
8462   return;
8463 }
8464
8465 static gint
8466 gtk_icon_view_accessible_item_compare (GtkIconViewItemAccessibleInfo *i1,
8467                                        GtkIconViewItemAccessibleInfo *i2)
8468 {
8469   return i1->index - i2->index;
8470 }
8471
8472 static void
8473 gtk_icon_view_accessible_model_rows_reordered (GtkTreeModel *tree_model,
8474                                                GtkTreePath  *path,
8475                                                GtkTreeIter  *iter,
8476                                                gint         *new_order,
8477                                                gpointer     user_data)
8478 {
8479   GtkIconViewAccessiblePrivate *priv;
8480   GtkIconViewItemAccessibleInfo *info;
8481   GtkIconViewAccessible *view;
8482   GtkIconView *icon_view;
8483   GtkIconViewItemAccessible *item;
8484   GList *items;
8485   AtkObject *atk_obj;
8486
8487   atk_obj = gtk_widget_get_accessible (GTK_WIDGET (user_data));
8488   icon_view = GTK_ICON_VIEW (user_data);
8489   view = GTK_ICON_VIEW_ACCESSIBLE (atk_obj);
8490   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8491
8492   items = priv->items;
8493   while (items)
8494     {
8495       info = items->data;
8496       item = GTK_ICON_VIEW_ITEM_ACCESSIBLE (info->item);
8497       info->index = new_order[info->index];
8498       item->item = g_list_nth_data (icon_view->priv->items, info->index);
8499       items = items->next;
8500     }
8501   priv->items = g_list_sort (priv->items, 
8502                              (GCompareFunc)gtk_icon_view_accessible_item_compare);
8503
8504   return;
8505 }
8506
8507 static void
8508 gtk_icon_view_accessible_disconnect_model_signals (GtkTreeModel *model,
8509                                                    GtkWidget *widget)
8510 {
8511   GObject *obj;
8512
8513   obj = G_OBJECT (model);
8514   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_changed, widget);
8515   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_inserted, widget);
8516   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_row_deleted, widget);
8517   g_signal_handlers_disconnect_by_func (obj, (gpointer) gtk_icon_view_accessible_model_rows_reordered, widget);
8518 }
8519
8520 static void
8521 gtk_icon_view_accessible_connect_model_signals (GtkIconView *icon_view)
8522 {
8523   GObject *obj;
8524
8525   obj = G_OBJECT (icon_view->priv->model);
8526   g_signal_connect_data (obj, "row-changed",
8527                          (GCallback) gtk_icon_view_accessible_model_row_changed,
8528                          icon_view, NULL, 0);
8529   g_signal_connect_data (obj, "row-inserted",
8530                          (GCallback) gtk_icon_view_accessible_model_row_inserted, 
8531                          icon_view, NULL, G_CONNECT_AFTER);
8532   g_signal_connect_data (obj, "row-deleted",
8533                          (GCallback) gtk_icon_view_accessible_model_row_deleted, 
8534                          icon_view, NULL, G_CONNECT_AFTER);
8535   g_signal_connect_data (obj, "rows-reordered",
8536                          (GCallback) gtk_icon_view_accessible_model_rows_reordered, 
8537                          icon_view, NULL, G_CONNECT_AFTER);
8538 }
8539
8540 static void
8541 gtk_icon_view_accessible_clear_cache (GtkIconViewAccessiblePrivate *priv)
8542 {
8543   GtkIconViewItemAccessibleInfo *info;
8544   GList *items;
8545
8546   items = priv->items;
8547   while (items)
8548     {
8549       info = (GtkIconViewItemAccessibleInfo *) items->data;
8550       g_object_unref (info->item);
8551       g_free (items->data);
8552       items = items->next;
8553     }
8554   g_list_free (priv->items);
8555   priv->items = NULL;
8556 }
8557
8558 static void
8559 gtk_icon_view_accessible_notify_gtk (GObject *obj,
8560                                      GParamSpec *pspec)
8561 {
8562   GtkIconView *icon_view;
8563   GtkWidget *widget;
8564   AtkObject *atk_obj;
8565   GtkIconViewAccessiblePrivate *priv;
8566
8567   if (strcmp (pspec->name, "model") == 0)
8568     {
8569       widget = GTK_WIDGET (obj); 
8570       atk_obj = gtk_widget_get_accessible (widget);
8571       priv = gtk_icon_view_accessible_get_priv (atk_obj);
8572       if (priv->model)
8573         {
8574           g_object_remove_weak_pointer (G_OBJECT (priv->model),
8575                                         (gpointer *)&priv->model);
8576           gtk_icon_view_accessible_disconnect_model_signals (priv->model, widget);
8577         }
8578       gtk_icon_view_accessible_clear_cache (priv);
8579
8580       icon_view = GTK_ICON_VIEW (obj);
8581       priv->model = icon_view->priv->model;
8582       /* If there is no model the GtkIconView is probably being destroyed */
8583       if (priv->model)
8584         {
8585           g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
8586           gtk_icon_view_accessible_connect_model_signals (icon_view);
8587         }
8588     }
8589
8590   return;
8591 }
8592
8593 static void
8594 gtk_icon_view_accessible_initialize (AtkObject *accessible,
8595                                      gpointer   data)
8596 {
8597   GtkIconViewAccessiblePrivate *priv;
8598   GtkIconView *icon_view;
8599
8600   if (ATK_OBJECT_CLASS (accessible_parent_class)->initialize)
8601     ATK_OBJECT_CLASS (accessible_parent_class)->initialize (accessible, data);
8602
8603   priv = g_new0 (GtkIconViewAccessiblePrivate, 1);
8604   g_object_set_qdata (G_OBJECT (accessible),
8605                       accessible_private_data_quark,
8606                       priv);
8607
8608   icon_view = GTK_ICON_VIEW (data);
8609   if (icon_view->priv->hadjustment)
8610     {
8611       priv->old_hadj = icon_view->priv->hadjustment;
8612       g_object_add_weak_pointer (G_OBJECT (priv->old_hadj), (gpointer *)&priv->old_hadj);
8613       g_signal_connect (icon_view->priv->hadjustment,
8614                         "value-changed",
8615                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8616                         icon_view);
8617     } 
8618   if (icon_view->priv->vadjustment)
8619     {
8620       priv->old_vadj = icon_view->priv->vadjustment;
8621       g_object_add_weak_pointer (G_OBJECT (priv->old_vadj), (gpointer *)&priv->old_vadj);
8622       g_signal_connect (icon_view->priv->vadjustment,
8623                         "value-changed",
8624                         G_CALLBACK (gtk_icon_view_accessible_adjustment_changed),
8625                         icon_view);
8626     }
8627   g_signal_connect_after (data,
8628                           "set_scroll_adjustments",
8629                           G_CALLBACK (gtk_icon_view_accessible_set_scroll_adjustments),
8630                           NULL);
8631   g_signal_connect (data,
8632                     "notify",
8633                     G_CALLBACK (gtk_icon_view_accessible_notify_gtk),
8634                     NULL);
8635
8636   priv->model = icon_view->priv->model;
8637   if (priv->model)
8638     {
8639       g_object_add_weak_pointer (G_OBJECT (priv->model), (gpointer *)&priv->model);
8640       gtk_icon_view_accessible_connect_model_signals (icon_view);
8641     }
8642                           
8643   accessible->role = ATK_ROLE_LAYERED_PANE;
8644 }
8645
8646 static void
8647 gtk_icon_view_accessible_finalize (GObject *object)
8648 {
8649   GtkIconViewAccessiblePrivate *priv;
8650
8651   priv = gtk_icon_view_accessible_get_priv (ATK_OBJECT (object));
8652   gtk_icon_view_accessible_clear_cache (priv);
8653
8654   g_free (priv);
8655
8656   G_OBJECT_CLASS (accessible_parent_class)->finalize (object);
8657 }
8658
8659 static void
8660 gtk_icon_view_accessible_destroyed (GtkWidget *widget,
8661                                     GtkAccessible *accessible)
8662 {
8663   AtkObject *atk_obj;
8664   GtkIconViewAccessiblePrivate *priv;
8665
8666   atk_obj = ATK_OBJECT (accessible);
8667   priv = gtk_icon_view_accessible_get_priv (atk_obj);
8668   if (priv->old_hadj)
8669     {
8670       g_object_remove_weak_pointer (G_OBJECT (priv->old_hadj),
8671                                     (gpointer *)&priv->old_hadj);
8672           
8673       g_signal_handlers_disconnect_by_func (priv->old_hadj,
8674                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
8675                                             widget);
8676       priv->old_hadj = NULL;
8677     }
8678   if (priv->old_vadj)
8679     {
8680       g_object_remove_weak_pointer (G_OBJECT (priv->old_vadj),
8681                                     (gpointer *)&priv->old_vadj);
8682           
8683       g_signal_handlers_disconnect_by_func (priv->old_vadj,
8684                                             (gpointer) gtk_icon_view_accessible_adjustment_changed,
8685                                             widget);
8686       priv->old_vadj = NULL;
8687     }
8688 }
8689
8690 static void
8691 gtk_icon_view_accessible_connect_widget_destroyed (GtkAccessible *accessible)
8692 {
8693   if (accessible->widget)
8694     {
8695       g_signal_connect_after (accessible->widget,
8696                               "destroy",
8697                               G_CALLBACK (gtk_icon_view_accessible_destroyed),
8698                               accessible);
8699     }
8700   GTK_ACCESSIBLE_CLASS (accessible_parent_class)->connect_widget_destroyed (accessible);
8701 }
8702
8703 static void
8704 gtk_icon_view_accessible_class_init (AtkObjectClass *klass)
8705 {
8706   GObjectClass *gobject_class;
8707   GtkAccessibleClass *accessible_class;
8708
8709   accessible_parent_class = g_type_class_peek_parent (klass);
8710
8711   gobject_class = (GObjectClass *)klass;
8712   accessible_class = (GtkAccessibleClass *)klass;
8713
8714   gobject_class->finalize = gtk_icon_view_accessible_finalize;
8715
8716   klass->get_n_children = gtk_icon_view_accessible_get_n_children;
8717   klass->ref_child = gtk_icon_view_accessible_ref_child;
8718   klass->initialize = gtk_icon_view_accessible_initialize;
8719
8720   accessible_class->connect_widget_destroyed = gtk_icon_view_accessible_connect_widget_destroyed;
8721
8722   accessible_private_data_quark = g_quark_from_static_string ("icon_view-accessible-private-data");
8723 }
8724
8725 static AtkObject*
8726 gtk_icon_view_accessible_ref_accessible_at_point (AtkComponent *component,
8727                                                   gint          x,
8728                                                   gint          y,
8729                                                   AtkCoordType  coord_type)
8730 {
8731   GtkWidget *widget;
8732   GtkIconView *icon_view;
8733   GtkIconViewItem *item;
8734   gint x_pos, y_pos;
8735
8736   widget = GTK_ACCESSIBLE (component)->widget;
8737   if (widget == NULL)
8738   /* State is defunct */
8739     return NULL;
8740
8741   icon_view = GTK_ICON_VIEW (widget);
8742   atk_component_get_extents (component, &x_pos, &y_pos, NULL, NULL, coord_type);
8743   item = gtk_icon_view_get_item_at_coords (icon_view, x - x_pos, y - y_pos, TRUE, NULL);
8744   if (item)
8745     return gtk_icon_view_accessible_ref_child (ATK_OBJECT (component), item->index);
8746
8747   return NULL;
8748 }
8749
8750 static void
8751 atk_component_interface_init (AtkComponentIface *iface)
8752 {
8753   iface->ref_accessible_at_point = gtk_icon_view_accessible_ref_accessible_at_point;
8754 }
8755
8756 static gboolean
8757 gtk_icon_view_accessible_add_selection (AtkSelection *selection,
8758                                         gint i)
8759 {
8760   GtkWidget *widget;
8761   GtkIconView *icon_view;
8762   GtkIconViewItem *item;
8763
8764   widget = GTK_ACCESSIBLE (selection)->widget;
8765   if (widget == NULL)
8766     return FALSE;
8767
8768   icon_view = GTK_ICON_VIEW (widget);
8769
8770   item = g_list_nth_data (icon_view->priv->items, i);
8771
8772   if (!item)
8773     return FALSE;
8774
8775   gtk_icon_view_select_item (icon_view, item);
8776
8777   return TRUE;
8778 }
8779
8780 static gboolean
8781 gtk_icon_view_accessible_clear_selection (AtkSelection *selection)
8782 {
8783   GtkWidget *widget;
8784   GtkIconView *icon_view;
8785
8786   widget = GTK_ACCESSIBLE (selection)->widget;
8787   if (widget == NULL)
8788     return FALSE;
8789
8790   icon_view = GTK_ICON_VIEW (widget);
8791   gtk_icon_view_unselect_all (icon_view);
8792
8793   return TRUE;
8794 }
8795
8796 static AtkObject*
8797 gtk_icon_view_accessible_ref_selection (AtkSelection *selection,
8798                                         gint          i)
8799 {
8800   GtkWidget *widget;
8801   GtkIconView *icon_view;
8802   GtkIconViewItem *item;
8803   GList *l;
8804
8805   widget = GTK_ACCESSIBLE (selection)->widget;
8806   if (widget == NULL)
8807     return NULL;
8808
8809   icon_view = GTK_ICON_VIEW (widget);
8810
8811   l = icon_view->priv->items;
8812   while (l)
8813     {
8814       item = l->data;
8815       if (item->selected)
8816         {
8817           if (i == 0)
8818             return atk_object_ref_accessible_child (gtk_widget_get_accessible (widget), item->index);
8819           else
8820             i--;
8821         }
8822       l = l->next;
8823     }
8824
8825   return NULL;
8826 }
8827
8828 static gint
8829 gtk_icon_view_accessible_get_selection_count (AtkSelection *selection)
8830 {
8831   GtkWidget *widget;
8832   GtkIconView *icon_view;
8833   GtkIconViewItem *item;
8834   GList *l;
8835   gint count;
8836
8837   widget = GTK_ACCESSIBLE (selection)->widget;
8838   if (widget == NULL)
8839     return 0;
8840
8841   icon_view = GTK_ICON_VIEW (widget);
8842
8843   l = icon_view->priv->items;
8844   count = 0;
8845   while (l)
8846     {
8847       item = l->data;
8848
8849       if (item->selected)
8850         count++;
8851
8852       l = l->next;
8853     }
8854
8855   return count;
8856 }
8857
8858 static gboolean
8859 gtk_icon_view_accessible_is_child_selected (AtkSelection *selection,
8860                                             gint          i)
8861 {
8862   GtkWidget *widget;
8863   GtkIconView *icon_view;
8864   GtkIconViewItem *item;
8865   GList *l;
8866
8867   widget = GTK_ACCESSIBLE (selection)->widget;
8868   if (widget == NULL)
8869     return FALSE;
8870
8871   icon_view = GTK_ICON_VIEW (widget);
8872
8873   item = g_list_nth_data (icon_view->priv->items, i);
8874   if (!item)
8875     return FALSE;
8876
8877   return item->selected;
8878 }
8879
8880 static gboolean
8881 gtk_icon_view_accessible_remove_selection (AtkSelection *selection,
8882                                            gint          i)
8883 {
8884   GtkWidget *widget;
8885   GtkIconView *icon_view;
8886   GtkIconViewItem *item;
8887   GList *l;
8888   gint count;
8889
8890   widget = GTK_ACCESSIBLE (selection)->widget;
8891   if (widget == NULL)
8892     return FALSE;
8893
8894   icon_view = GTK_ICON_VIEW (widget);
8895   l = icon_view->priv->items;
8896   count = 0;
8897   while (l)
8898     {
8899       item = l->data;
8900       if (item->selected)
8901         {
8902           if (count == i)
8903             {
8904               gtk_icon_view_unselect_item (icon_view, item);
8905               return TRUE;
8906             }
8907           count++;
8908         }
8909       l = l->next;
8910     }
8911
8912   return FALSE;
8913 }
8914  
8915 static gboolean
8916 gtk_icon_view_accessible_select_all_selection (AtkSelection *selection)
8917 {
8918   GtkWidget *widget;
8919   GtkIconView *icon_view;
8920
8921   widget = GTK_ACCESSIBLE (selection)->widget;
8922   if (widget == NULL)
8923     return FALSE;
8924
8925   icon_view = GTK_ICON_VIEW (widget);
8926   gtk_icon_view_select_all (icon_view);
8927   return TRUE;
8928 }
8929
8930 static void
8931 gtk_icon_view_accessible_selection_interface_init (AtkSelectionIface *iface)
8932 {
8933   iface->add_selection = gtk_icon_view_accessible_add_selection;
8934   iface->clear_selection = gtk_icon_view_accessible_clear_selection;
8935   iface->ref_selection = gtk_icon_view_accessible_ref_selection;
8936   iface->get_selection_count = gtk_icon_view_accessible_get_selection_count;
8937   iface->is_child_selected = gtk_icon_view_accessible_is_child_selected;
8938   iface->remove_selection = gtk_icon_view_accessible_remove_selection;
8939   iface->select_all_selection = gtk_icon_view_accessible_select_all_selection;
8940 }
8941
8942 static GType
8943 gtk_icon_view_accessible_get_type (void)
8944 {
8945   static GType type = 0;
8946
8947   if (!type)
8948     {
8949       static GTypeInfo tinfo =
8950       {
8951         0, /* class size */
8952         (GBaseInitFunc) NULL, /* base init */
8953         (GBaseFinalizeFunc) NULL, /* base finalize */
8954         (GClassInitFunc) gtk_icon_view_accessible_class_init,
8955         (GClassFinalizeFunc) NULL, /* class finalize */
8956         NULL, /* class data */
8957         0, /* instance size */
8958         0, /* nb preallocs */
8959         (GInstanceInitFunc) NULL, /* instance init */
8960         NULL /* value table */
8961       };
8962       static const GInterfaceInfo atk_component_info =
8963       {
8964         (GInterfaceInitFunc) atk_component_interface_init,
8965         (GInterfaceFinalizeFunc) NULL,
8966         NULL
8967       };
8968       static const GInterfaceInfo atk_selection_info = 
8969       {
8970         (GInterfaceInitFunc) gtk_icon_view_accessible_selection_interface_init,
8971         (GInterfaceFinalizeFunc) NULL,
8972         NULL
8973       };
8974
8975       /*
8976        * Figure out the size of the class and instance
8977        * we are deriving from
8978        */
8979       AtkObjectFactory *factory;
8980       GType derived_type;
8981       GTypeQuery query;
8982       GType derived_atk_type;
8983
8984       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
8985       factory = atk_registry_get_factory (atk_get_default_registry (), 
8986                                           derived_type);
8987       derived_atk_type = atk_object_factory_get_accessible_type (factory);
8988       g_type_query (derived_atk_type, &query);
8989       tinfo.class_size = query.class_size;
8990       tinfo.instance_size = query.instance_size;
8991  
8992       type = g_type_register_static (derived_atk_type, 
8993                                      "GtkIconViewAccessible", 
8994                                      &tinfo, 0);
8995       g_type_add_interface_static (type, ATK_TYPE_COMPONENT,
8996                                    &atk_component_info);
8997       g_type_add_interface_static (type, ATK_TYPE_SELECTION,
8998                                    &atk_selection_info);
8999     }
9000   return type;
9001 }
9002
9003 static AtkObject *
9004 gtk_icon_view_accessible_new (GObject *obj)
9005 {
9006   AtkObject *accessible;
9007
9008   g_return_val_if_fail (GTK_IS_WIDGET (obj), NULL);
9009
9010   accessible = g_object_new (gtk_icon_view_accessible_get_type (), NULL);
9011   atk_object_initialize (accessible, obj);
9012
9013   return accessible;
9014 }
9015
9016 static GType
9017 gtk_icon_view_accessible_factory_get_accessible_type (void)
9018 {
9019   return gtk_icon_view_accessible_get_type ();
9020 }
9021
9022 static AtkObject*
9023 gtk_icon_view_accessible_factory_create_accessible (GObject *obj)
9024 {
9025   return gtk_icon_view_accessible_new (obj);
9026 }
9027
9028 static void
9029 gtk_icon_view_accessible_factory_class_init (AtkObjectFactoryClass *klass)
9030 {
9031   klass->create_accessible = gtk_icon_view_accessible_factory_create_accessible;
9032   klass->get_accessible_type = gtk_icon_view_accessible_factory_get_accessible_type;
9033 }
9034
9035 static GType
9036 gtk_icon_view_accessible_factory_get_type (void)
9037 {
9038   static GType type = 0;
9039
9040   if (!type)
9041     {
9042       static const GTypeInfo tinfo =
9043       {
9044         sizeof (AtkObjectFactoryClass),
9045         NULL,           /* base_init */
9046         NULL,           /* base_finalize */
9047         (GClassInitFunc) gtk_icon_view_accessible_factory_class_init,
9048         NULL,           /* class_finalize */
9049         NULL,           /* class_data */
9050         sizeof (AtkObjectFactory),
9051         0,             /* n_preallocs */
9052         NULL, NULL
9053       };
9054
9055       type = g_type_register_static (ATK_TYPE_OBJECT_FACTORY, 
9056                                     "GtkIconViewAccessibleFactory",
9057                                     &tinfo, 0);
9058     }
9059   return type;
9060 }
9061
9062
9063 static AtkObject *
9064 gtk_icon_view_get_accessible (GtkWidget *widget)
9065 {
9066   static gboolean first_time = TRUE;
9067
9068   if (first_time)
9069     {
9070       AtkObjectFactory *factory;
9071       AtkRegistry *registry;
9072       GType derived_type; 
9073       GType derived_atk_type; 
9074
9075       /*
9076        * Figure out whether accessibility is enabled by looking at the
9077        * type of the accessible object which would be created for
9078        * the parent type of GtkIconView.
9079        */
9080       derived_type = g_type_parent (GTK_TYPE_ICON_VIEW);
9081
9082       registry = atk_get_default_registry ();
9083       factory = atk_registry_get_factory (registry,
9084                                           derived_type);
9085       derived_atk_type = atk_object_factory_get_accessible_type (factory);
9086       if (g_type_is_a (derived_atk_type, GTK_TYPE_ACCESSIBLE)) 
9087         atk_registry_set_factory_type (registry, 
9088                                        GTK_TYPE_ICON_VIEW,
9089                                        gtk_icon_view_accessible_factory_get_type ());
9090       first_time = FALSE;
9091     } 
9092   return (* GTK_WIDGET_CLASS (gtk_icon_view_parent_class)->get_accessible) (widget);
9093 }
9094
9095 #define __GTK_ICON_VIEW_C__
9096 #include "gtkaliasdef.c"