]> Pileus Git - ~andy/gtk/blob - gtk/gtkiconview.c
gtk/gtkiconview.c Add GtkIconView to the build, remove debugging output.
[~andy/gtk] / gtk / gtkiconview.c
1 /* gtkiconview.h
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 <gdk/gdkkeysyms.h>
24
25 #include "gtkiconview.h"
26 #include "gtkmarshalers.h"
27 #include "gtkbindings.h"
28 #include "gtkdnd.h"
29 #include "gtkmain.h"
30 #include "gtksignal.h"
31 #include "gtkintl.h"
32
33 #define MINIMUM_ICON_ITEM_WIDTH 100
34 #define ICON_TEXT_PADDING 3
35
36 #define ICON_VIEW_TOP_MARGIN 6
37 #define ICON_VIEW_BOTTOM_MARGIN 6
38 #define ICON_VIEW_LEFT_MARGIN 6
39 #define ICON_VIEW_RIGHT_MARGIN 6
40 #define ICON_VIEW_ICON_PADDING 6
41
42 #define GTK_ICON_VIEW_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ICON_VIEW, GtkIconViewPrivate))
43 #define VALID_MODEL_AND_COLUMNS(obj) ((obj)->priv->model != NULL && \
44                                       ((obj)->priv->pixbuf_column != -1 || \
45                                        (obj)->priv->text_column != -1 || \
46                                        (obj)->priv->markup_column != -1))
47
48 struct _GtkIconViewItem
49 {
50   gint ref_count;
51
52   GtkTreeIter iter;
53   int index;
54   
55   gint row, col;
56
57   /* Bounding boxes */
58   gint x, y;
59   gint width, height;
60
61   gint pixbuf_x, pixbuf_y;
62   gint pixbuf_height, pixbuf_width;
63
64   gint layout_x, layout_y;
65   gint layout_width, layout_height;
66
67   guint selected : 1;
68   guint selected_before_rubberbanding : 1;
69 };
70
71 struct _GtkIconViewPrivate
72 {
73   gint width, height;
74
75   gint text_column;
76   gint markup_column;
77   gint pixbuf_column;
78   
79   GtkSelectionMode selection_mode;
80
81   GdkWindow *bin_window;
82
83   GtkTreeModel *model;
84   
85   GList *items;
86   
87   GtkAdjustment *hadjustment;
88   GtkAdjustment *vadjustment;
89
90   guint layout_idle_id;
91   
92   gboolean doing_rubberband;
93   gint rubberband_x1, rubberband_y1;
94   gint rubberband_x2, rubberband_y2;
95
96   guint scroll_timeout_id;
97   gint scroll_value_diff;
98   gint event_last_x, event_last_y;
99
100   GtkIconViewItem *anchor_item;
101   GtkIconViewItem *cursor_item;
102
103   guint ctrl_pressed : 1;
104   guint shift_pressed : 1;
105   
106   GtkIconViewItem *last_single_clicked;
107
108 #ifdef DND_WORKS
109   /* Drag-and-drop. */
110   gint pressed_button;
111   gint press_start_x;
112   gint press_start_y;
113 #endif
114   
115   /* Layout used to draw icon text */
116   PangoLayout *layout;
117 };
118
119 /* Signals */
120 enum
121 {
122   ITEM_ACTIVATED,
123   SELECTION_CHANGED,
124   SELECT_ALL,
125   UNSELECT_ALL,
126   SELECT_CURSOR_ITEM,
127   TOGGLE_CURSOR_ITEM,
128   MOVE_CURSOR,
129   LAST_SIGNAL
130 };
131
132 /* Properties */
133 enum
134 {
135   PROP_0,
136   PROP_PIXBUF_COLUMN,
137   PROP_TEXT_COLUMN,
138   PROP_MARKUP_COLUMN,  
139   PROP_SELECTION_MODE,
140   PROP_MODEL,
141 };
142
143 /* GObject signals */
144 static void gtk_icon_view_finalize     (GObject      *object);
145 static void gtk_icon_view_set_property (GObject      *object,
146                                         guint         prop_id,
147                                         const GValue *value,
148                                         GParamSpec   *pspec);
149 static void gtk_icon_view_get_property (GObject      *object,
150                                         guint         prop_id,
151                                         GValue       *value,
152                                         GParamSpec   *pspec);
153
154
155 /* GtkObject signals */
156 static void gtk_icon_view_destroy (GtkObject *object);
157
158 /* GtkWidget signals */
159 static void     gtk_icon_view_realize        (GtkWidget      *widget);
160 static void     gtk_icon_view_unrealize      (GtkWidget      *widget);
161 static void     gtk_icon_view_map            (GtkWidget      *widget);
162 static void     gtk_icon_view_size_request   (GtkWidget      *widget,
163                                               GtkRequisition *requisition);
164 static void     gtk_icon_view_size_allocate  (GtkWidget      *widget,
165                                               GtkAllocation  *allocation);
166 static gboolean gtk_icon_view_expose         (GtkWidget      *widget,
167                                               GdkEventExpose *expose);
168 static gboolean gtk_icon_view_motion         (GtkWidget      *widget,
169                                               GdkEventMotion *event);
170 static gboolean gtk_icon_view_button_press   (GtkWidget      *widget,
171                                               GdkEventButton *event);
172 static gboolean gtk_icon_view_button_release (GtkWidget      *widget,
173                                               GdkEventButton *event);
174
175 /* GtkIconView signals */
176 static void     gtk_icon_view_set_adjustments             (GtkIconView             *icon_view,
177                                                            GtkAdjustment           *hadj,
178                                                            GtkAdjustment           *vadj);
179 static void     gtk_icon_view_real_select_all             (GtkIconView             *icon_view);
180 static void     gtk_icon_view_real_unselect_all           (GtkIconView             *icon_view);
181 static void     gtk_icon_view_real_select_cursor_item     (GtkIconView             *icon_view);
182 static void     gtk_icon_view_real_toggle_cursor_item     (GtkIconView             *icon_view);
183
184 /* Internal functions */
185 static void       gtk_icon_view_adjustment_changed          (GtkAdjustment   *adjustment,
186                                                              GtkIconView     *icon_view);
187 static void       gtk_icon_view_layout                      (GtkIconView     *icon_view);
188 static void       gtk_icon_view_paint_item                  (GtkIconView     *icon_view,
189                                                              GtkIconViewItem *item,
190                                                              GdkRectangle    *area);
191 static void       gtk_icon_view_paint_rubberband            (GtkIconView     *icon_view,
192                                                              GdkRectangle    *area);
193 static void       gtk_icon_view_queue_draw_item             (GtkIconView     *icon_view,
194                                                              GtkIconViewItem *item);
195 static void       gtk_icon_view_queue_layout                (GtkIconView     *icon_view);
196 static void       gtk_icon_view_set_cursor_item             (GtkIconView     *icon_view,
197                                                              GtkIconViewItem *item);
198 static void       gtk_icon_view_start_rubberbanding         (GtkIconView     *icon_view,
199                                                              gint             x,
200                                                              gint             y);
201 static void       gtk_icon_view_stop_rubberbanding          (GtkIconView     *icon_view);
202 static void       gtk_icon_view_update_rubberband_selection (GtkIconView     *icon_view);
203 static gboolean   gtk_icon_view_item_hit_test               (GtkIconViewItem *item,
204                                                              gint             x,
205                                                              gint             y,
206                                                              gint             width,
207                                                              gint             height);
208 #ifdef DND_WORKS
209 static gboolean   gtk_icon_view_maybe_begin_dragging_items  (GtkIconView     *icon_view,
210                                                              GdkEventMotion  *event);
211 #endif
212 static gboolean   gtk_icon_view_unselect_all_internal       (GtkIconView     *icon_view);
213 static void       gtk_icon_view_calculate_item_size         (GtkIconView     *icon_view,
214                                                              GtkIconViewItem *item);
215 static void       gtk_icon_view_update_rubberband           (gpointer         data);
216 static void       gtk_icon_view_item_invalidate_size        (GtkIconViewItem *item);
217 static void       gtk_icon_view_invalidate_sizes            (GtkIconView     *icon_view);
218 static void       gtk_icon_view_add_move_binding            (GtkBindingSet   *binding_set,
219                                                              guint            keyval,
220                                                              guint            modmask,
221                                                              GtkMovementStep  step,
222                                                              gint             count);
223 static gboolean   gtk_icon_view_real_move_cursor            (GtkIconView     *icon_view,
224                                                              GtkMovementStep  step,
225                                                              gint             count);
226 static void       gtk_icon_view_move_cursor_up_down         (GtkIconView     *icon_view,
227                                                              gint             count);
228 static void       gtk_icon_view_move_cursor_page_up_down    (GtkIconView     *icon_view,
229                                                              gint             count);
230 static void       gtk_icon_view_move_cursor_left_right      (GtkIconView     *icon_view,
231                                                              gint             count);
232 static void       gtk_icon_view_move_cursor_start_end       (GtkIconView     *icon_view,
233                                                              gint             count);
234 static void       gtk_icon_view_scroll_to_item              (GtkIconView     *icon_view,
235                                                              GtkIconViewItem *item);
236 static GdkPixbuf *gtk_icon_view_get_item_icon               (GtkIconView     *icon_view,
237                                                              GtkIconViewItem *item);
238 static void       gtk_icon_view_update_item_text            (GtkIconView     *icon_view,
239                                                              GtkIconViewItem *item);
240 static void       gtk_icon_view_select_item                 (GtkIconView     *icon_view,
241                                                              GtkIconViewItem *item);
242 static void       gtk_icon_view_unselect_item               (GtkIconView     *icon_view,
243                                                              GtkIconViewItem *item);
244 static gboolean gtk_icon_view_select_all_between            (GtkIconView     *icon_view,
245                                                              GtkIconViewItem *anchor,
246                                                              GtkIconViewItem *cursor);
247
248 static GtkIconViewItem *gtk_icon_view_get_item_at_pos (GtkIconView *icon_view,
249                                                        gint         x,
250                                                        gint         y);
251
252
253
254
255
256 static GtkContainerClass *parent_class = NULL;
257 static guint icon_view_signals[LAST_SIGNAL] = { 0 };
258
259 G_DEFINE_TYPE (GtkIconView, gtk_icon_view, GTK_TYPE_CONTAINER);
260
261 static void
262 gtk_icon_view_class_init (GtkIconViewClass *klass)
263 {
264   GObjectClass *gobject_class;
265   GtkObjectClass *object_class;
266   GtkWidgetClass *widget_class;
267   GtkBindingSet *binding_set;
268   
269   parent_class = g_type_class_peek_parent (klass);
270   binding_set = gtk_binding_set_by_class (klass);
271
272   g_type_class_add_private (klass, sizeof (GtkIconViewPrivate));
273
274   gobject_class = (GObjectClass *) klass;
275   object_class = (GtkObjectClass *) klass;
276   widget_class = (GtkWidgetClass *) klass;
277
278   gobject_class->finalize = gtk_icon_view_finalize;
279   gobject_class->set_property = gtk_icon_view_set_property;
280   gobject_class->get_property = gtk_icon_view_get_property;
281
282   object_class->destroy = gtk_icon_view_destroy;
283   
284   widget_class->realize = gtk_icon_view_realize;
285   widget_class->unrealize = gtk_icon_view_unrealize;
286   widget_class->map = gtk_icon_view_map;
287   widget_class->size_request = gtk_icon_view_size_request;
288   widget_class->size_allocate = gtk_icon_view_size_allocate;
289   widget_class->expose_event = gtk_icon_view_expose;
290   widget_class->motion_notify_event = gtk_icon_view_motion;
291   widget_class->button_press_event = gtk_icon_view_button_press;
292   widget_class->button_release_event = gtk_icon_view_button_release;
293   
294   klass->set_scroll_adjustments = gtk_icon_view_set_adjustments;
295   klass->select_all = gtk_icon_view_real_select_all;
296   klass->unselect_all = gtk_icon_view_real_unselect_all;
297   klass->select_cursor_item = gtk_icon_view_real_select_cursor_item;
298   klass->toggle_cursor_item = gtk_icon_view_real_toggle_cursor_item;
299   klass->move_cursor = gtk_icon_view_real_move_cursor;
300   
301   /* Properties */
302   g_object_class_install_property (gobject_class,
303                                    PROP_SELECTION_MODE,
304                                    g_param_spec_enum ("selection_mode",
305                                                       P_("Selection mode"),
306                                                       P_("The selection mode"),
307                                                       GTK_TYPE_SELECTION_MODE,
308                                                       GTK_SELECTION_SINGLE,
309                                                       G_PARAM_READWRITE));
310
311   g_object_class_install_property (gobject_class,
312                                    PROP_PIXBUF_COLUMN,
313                                    g_param_spec_int ("pixbuf_column",
314                                                      P_("Pixbuf column"),
315                                                      P_("Model column used to retrieve the icon pixbuf from"),
316                                                      -1, G_MAXINT, -1,
317                                                      G_PARAM_READWRITE));
318
319   g_object_class_install_property (gobject_class,
320                                    PROP_TEXT_COLUMN,
321                                    g_param_spec_int ("text_column",
322                                                      P_("Text column"),
323                                                      P_("Model column used to retrieve the text from"),
324                                                      -1, G_MAXINT, -1,
325                                                      G_PARAM_READWRITE));
326
327   g_object_class_install_property (gobject_class,
328                                    PROP_MARKUP_COLUMN,
329                                    g_param_spec_int ("markup_column",
330                                                      P_("Markup column"),
331                                                      P_("Model column used to retrieve the text if using pango markup"),
332                                                      -1, G_MAXINT, -1,
333                                                      G_PARAM_READWRITE));
334   
335   g_object_class_install_property (gobject_class,
336                                    PROP_MODEL,
337                                    g_param_spec_object ("model",
338                                                         P_("Icon List Model"),
339                                                         P_("The model for the icon list"),
340                                                         GTK_TYPE_TREE_MODEL,
341                                                         G_PARAM_READWRITE));
342   
343
344   /* Style properties */
345   gtk_widget_class_install_style_property (widget_class,
346                                            g_param_spec_boxed ("selection_box_color",
347                                                                P_("Selection Box Color"),
348                                                                P_("Color of the selection box"),
349                                                                GDK_TYPE_COLOR,
350                                                                G_PARAM_READABLE));
351
352   gtk_widget_class_install_style_property (widget_class,
353                                            g_param_spec_uchar ("selection_box_alpha",
354                                                                P_("Selection Box Alpha"),
355                                                                P_("Opacity of the selection box"),
356                                                                0, 0xff,
357                                                                0x40,
358                                                                G_PARAM_READABLE));
359
360   /* Signals */
361   widget_class->set_scroll_adjustments_signal =
362     g_signal_new ("set_scroll_adjustments",
363                   G_TYPE_FROM_CLASS (gobject_class),
364                   G_SIGNAL_RUN_LAST,
365                   G_STRUCT_OFFSET (GtkIconViewClass, set_scroll_adjustments),
366                   NULL, NULL, 
367                   _gtk_marshal_VOID__OBJECT_OBJECT,
368                   G_TYPE_NONE, 2,
369                   GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);
370
371   icon_view_signals[ITEM_ACTIVATED] =
372     g_signal_new ("item_activated",
373                   G_TYPE_FROM_CLASS (gobject_class),
374                   G_SIGNAL_RUN_LAST,
375                   G_STRUCT_OFFSET (GtkIconViewClass, item_activated),
376                   NULL, NULL,
377                   g_cclosure_marshal_VOID__BOXED,
378                   G_TYPE_NONE, 1,
379                   GTK_TYPE_TREE_PATH);
380
381   icon_view_signals[SELECTION_CHANGED] =
382     g_signal_new ("selection_changed",
383                   G_TYPE_FROM_CLASS (gobject_class),
384                   G_SIGNAL_RUN_FIRST,
385                   G_STRUCT_OFFSET (GtkIconViewClass, selection_changed),
386                   NULL, NULL,
387                   g_cclosure_marshal_VOID__VOID,
388                   G_TYPE_NONE, 0);
389   
390   icon_view_signals[SELECT_ALL] =
391     g_signal_new ("select_all",
392                   G_TYPE_FROM_CLASS (gobject_class),
393                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
394                   G_STRUCT_OFFSET (GtkIconViewClass, select_all),
395                   NULL, NULL,
396                   g_cclosure_marshal_VOID__VOID,
397                   G_TYPE_NONE, 0);
398   
399   icon_view_signals[UNSELECT_ALL] =
400     g_signal_new ("unselect_all",
401                   G_TYPE_FROM_CLASS (gobject_class),
402                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
403                   G_STRUCT_OFFSET (GtkIconViewClass, unselect_all),
404                   NULL, NULL,
405                   g_cclosure_marshal_VOID__VOID,
406                   G_TYPE_NONE, 0);
407
408   icon_view_signals[SELECT_CURSOR_ITEM] =
409     g_signal_new ("select_cursor_item",
410                   G_TYPE_FROM_CLASS (gobject_class),
411                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
412                   G_STRUCT_OFFSET (GtkIconViewClass, select_cursor_item),
413                   NULL, NULL,
414                   g_cclosure_marshal_VOID__VOID,
415                   G_TYPE_NONE, 0);
416
417   icon_view_signals[SELECT_CURSOR_ITEM] =
418     g_signal_new ("toggle_cursor_item",
419                   G_TYPE_FROM_CLASS (gobject_class),
420                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
421                   G_STRUCT_OFFSET (GtkIconViewClass, toggle_cursor_item),
422                   NULL, NULL,
423                   g_cclosure_marshal_VOID__VOID,
424                   G_TYPE_NONE, 0);
425
426   icon_view_signals[MOVE_CURSOR] =
427     g_signal_new ("move_cursor",
428                   G_TYPE_FROM_CLASS (gobject_class),
429                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
430                   G_STRUCT_OFFSET (GtkIconViewClass, move_cursor),
431                   NULL, NULL,
432                   _gtk_marshal_BOOLEAN__ENUM_INT,
433                   G_TYPE_BOOLEAN, 2,
434                   GTK_TYPE_MOVEMENT_STEP,
435                   G_TYPE_INT);
436
437   /* Key bindings */
438   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK, "select_all", 0);
439   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "unselect_all", 0);
440   gtk_binding_entry_add_signal (binding_set, GDK_space, 0, "select_cursor_item", 0);
441   gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK, "toggle_cursor_item", 0);
442
443   gtk_icon_view_add_move_binding (binding_set, GDK_Up, 0,
444                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
445   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Up, 0,
446                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
447
448   gtk_icon_view_add_move_binding (binding_set, GDK_Down, 0,
449                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
450   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Down, 0,
451                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
452
453   gtk_icon_view_add_move_binding (binding_set, GDK_p, GDK_CONTROL_MASK,
454                                   GTK_MOVEMENT_DISPLAY_LINES, -1);
455
456   gtk_icon_view_add_move_binding (binding_set, GDK_n, GDK_CONTROL_MASK,
457                                   GTK_MOVEMENT_DISPLAY_LINES, 1);
458
459   gtk_icon_view_add_move_binding (binding_set, GDK_Home, 0,
460                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
461   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Home, 0,
462                                   GTK_MOVEMENT_BUFFER_ENDS, -1);
463
464   gtk_icon_view_add_move_binding (binding_set, GDK_End, 0,
465                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
466   gtk_icon_view_add_move_binding (binding_set, GDK_KP_End, 0,
467                                   GTK_MOVEMENT_BUFFER_ENDS, 1);
468
469   gtk_icon_view_add_move_binding (binding_set, GDK_Page_Up, 0,
470                                   GTK_MOVEMENT_PAGES, -1);
471   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Page_Up, 0,
472                                   GTK_MOVEMENT_PAGES, -1);
473
474   gtk_icon_view_add_move_binding (binding_set, GDK_Page_Down, 0,
475                                   GTK_MOVEMENT_PAGES, 1);
476   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Page_Down, 0,
477                                   GTK_MOVEMENT_PAGES, 1);
478
479   gtk_icon_view_add_move_binding (binding_set, GDK_Right, 0, 
480                                   GTK_MOVEMENT_VISUAL_POSITIONS, 1);
481   gtk_icon_view_add_move_binding (binding_set, GDK_Left, 0, 
482                                   GTK_MOVEMENT_VISUAL_POSITIONS, -1);
483
484   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Right, 0, 
485                                   GTK_MOVEMENT_VISUAL_POSITIONS, 1);
486   gtk_icon_view_add_move_binding (binding_set, GDK_KP_Left, 0, 
487                                   GTK_MOVEMENT_VISUAL_POSITIONS, -1);
488 }
489
490 static void
491 gtk_icon_view_init (GtkIconView *icon_view)
492 {
493   icon_view->priv = GTK_ICON_VIEW_GET_PRIVATE (icon_view);
494   
495   icon_view->priv->width = 0;
496   icon_view->priv->height = 0;
497   icon_view->priv->selection_mode = GTK_SELECTION_SINGLE;
498 #ifdef DND_WORKS
499   icon_view->priv->pressed_button = -1;
500   icon_view->priv->press_start_x = -1;
501   icon_view->priv->press_start_y = -1;
502 #endif
503   icon_view->priv->text_column = -1;
504   icon_view->priv->markup_column = -1;  
505   icon_view->priv->pixbuf_column = -1;
506   
507   icon_view->priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (icon_view), NULL);
508
509   pango_layout_set_wrap (icon_view->priv->layout, PANGO_WRAP_WORD_CHAR);
510
511   GTK_WIDGET_SET_FLAGS (icon_view, GTK_CAN_FOCUS);
512   
513   gtk_icon_view_set_adjustments (icon_view, NULL, NULL);
514 }
515
516 static void
517 gtk_icon_view_destroy (GtkObject *object)
518 {
519   GtkIconView *icon_view;
520
521   icon_view = GTK_ICON_VIEW (object);
522   
523   gtk_icon_view_set_model (icon_view, NULL);
524   
525   if (icon_view->priv->layout_idle_id != 0)
526     g_source_remove (icon_view->priv->layout_idle_id);
527
528   if (icon_view->priv->scroll_timeout_id != 0)
529     g_source_remove (icon_view->priv->scroll_timeout_id);
530   
531   (GTK_OBJECT_CLASS (parent_class)->destroy) (object);
532 }
533
534 /* GObject methods */
535 static void
536 gtk_icon_view_finalize (GObject *object)
537 {
538   GtkIconView *icon_view;
539
540   icon_view = GTK_ICON_VIEW (object);
541
542   g_object_unref (icon_view->priv->layout);
543   
544   (G_OBJECT_CLASS (parent_class)->finalize) (object);
545 }
546
547
548 static void
549 gtk_icon_view_set_property (GObject      *object,
550                             guint         prop_id,
551                             const GValue *value,
552                             GParamSpec   *pspec)
553 {
554   GtkIconView *icon_view;
555
556   icon_view = GTK_ICON_VIEW (object);
557
558   switch (prop_id)
559     {
560     case PROP_SELECTION_MODE:
561       gtk_icon_view_set_selection_mode (icon_view, g_value_get_enum (value));
562       break;
563     case PROP_PIXBUF_COLUMN:
564       gtk_icon_view_set_pixbuf_column (icon_view, g_value_get_int (value));
565       break;
566     case PROP_TEXT_COLUMN:
567       gtk_icon_view_set_text_column (icon_view, g_value_get_int (value));
568       break;
569     case PROP_MARKUP_COLUMN:
570       gtk_icon_view_set_markup_column (icon_view, g_value_get_int (value));
571       break;
572     case PROP_MODEL:
573       gtk_icon_view_set_model (icon_view, g_value_get_object (value));
574       break;
575       
576     default:
577       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
578       break;
579     }
580 }
581
582 static void
583 gtk_icon_view_get_property (GObject      *object,
584                             guint         prop_id,
585                             GValue       *value,
586                             GParamSpec   *pspec)
587 {
588   GtkIconView *icon_view;
589
590   icon_view = GTK_ICON_VIEW (object);
591
592   switch (prop_id)
593     {
594     case PROP_SELECTION_MODE:
595       g_value_set_enum (value, icon_view->priv->selection_mode);
596       break;
597     case PROP_PIXBUF_COLUMN:
598       g_value_set_int (value, icon_view->priv->pixbuf_column);
599       break;
600     case PROP_TEXT_COLUMN:
601       g_value_set_int (value, icon_view->priv->text_column);
602       break;
603     case PROP_MARKUP_COLUMN:
604       g_value_set_int (value, icon_view->priv->markup_column);
605       break;
606     case PROP_MODEL:
607       g_value_set_object (value, icon_view->priv->model);
608       break;
609     default:
610       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
611       break;
612     }
613 }
614
615 /* GtkWidget signals */
616 static void
617 gtk_icon_view_realize (GtkWidget *widget)
618 {
619   GtkIconView *icon_view;
620   GdkWindowAttr attributes;
621   gint attributes_mask;
622   
623   icon_view = GTK_ICON_VIEW (widget);
624
625   GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);
626
627   /* Make the main, clipping window */
628   attributes.window_type = GDK_WINDOW_CHILD;
629   attributes.x = widget->allocation.x;
630   attributes.y = widget->allocation.y;
631   attributes.width = widget->allocation.width;
632   attributes.height = widget->allocation.height;
633   attributes.wclass = GDK_INPUT_OUTPUT;
634   attributes.visual = gtk_widget_get_visual (widget);
635   attributes.colormap = gtk_widget_get_colormap (widget);
636   attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;
637
638   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
639
640   widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
641                                    &attributes, attributes_mask);
642   gdk_window_set_user_data (widget->window, widget);
643
644   /* Make the window for the icon list */
645   attributes.x = 0;
646   attributes.y = 0;
647   attributes.width = MAX (icon_view->priv->width, widget->allocation.width);
648   attributes.height = MAX (icon_view->priv->height, widget->allocation.height);
649   attributes.event_mask = (GDK_EXPOSURE_MASK |
650                            GDK_SCROLL_MASK |
651                            GDK_POINTER_MOTION_MASK |
652                            GDK_BUTTON_PRESS_MASK |
653                            GDK_BUTTON_RELEASE_MASK |
654                            GDK_KEY_PRESS_MASK |
655                            GDK_KEY_RELEASE_MASK) |
656     gtk_widget_get_events (widget);
657   
658   icon_view->priv->bin_window = gdk_window_new (widget->window,
659                                           &attributes, attributes_mask);
660   gdk_window_set_user_data (icon_view->priv->bin_window, widget);
661
662   widget->style = gtk_style_attach (widget->style, widget->window);
663   gdk_window_set_background (icon_view->priv->bin_window, &widget->style->base[widget->state]);
664   gdk_window_set_background (widget->window, &widget->style->base[widget->state]);
665
666   
667 }
668
669 static void
670 gtk_icon_view_unrealize (GtkWidget *widget)
671 {
672   GtkIconView *icon_view;
673
674   icon_view = GTK_ICON_VIEW (widget);
675
676   gdk_window_set_user_data (icon_view->priv->bin_window, NULL);
677   gdk_window_destroy (icon_view->priv->bin_window);
678   icon_view->priv->bin_window = NULL;
679
680   /* GtkWidget::unrealize destroys children and widget->window */
681   if (GTK_WIDGET_CLASS (parent_class)->unrealize)
682     (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
683 }
684
685 static void
686 gtk_icon_view_map (GtkWidget *widget)
687 {
688   GtkIconView *icon_view;
689
690   icon_view = GTK_ICON_VIEW (widget);
691
692   GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);
693
694   gdk_window_show (icon_view->priv->bin_window);
695   gdk_window_show (widget->window);
696 }
697
698 static void
699 gtk_icon_view_size_request (GtkWidget      *widget,
700                             GtkRequisition *requisition)
701 {
702   GtkIconView *icon_view;
703
704   icon_view = GTK_ICON_VIEW (widget);
705
706   requisition->width = icon_view->priv->width;
707   requisition->height = icon_view->priv->height;
708 }
709
710 static void
711 gtk_icon_view_size_allocate (GtkWidget      *widget,
712                              GtkAllocation  *allocation)
713 {
714   GtkIconView *icon_view;
715
716   widget->allocation = *allocation;
717   
718   icon_view = GTK_ICON_VIEW (widget);
719   
720   if (GTK_WIDGET_REALIZED (widget))
721     {
722       gdk_window_move_resize (widget->window,
723                               allocation->x, allocation->y,
724                               allocation->width, allocation->height);
725       gdk_window_resize (icon_view->priv->bin_window,
726                          MAX (icon_view->priv->width, allocation->width),
727                          MAX (icon_view->priv->height, allocation->height));
728     }
729
730   icon_view->priv->hadjustment->page_size = allocation->width;
731   icon_view->priv->hadjustment->page_increment = allocation->width * 0.9;
732   icon_view->priv->hadjustment->step_increment = allocation->width * 0.1;
733   icon_view->priv->hadjustment->lower = 0;
734   icon_view->priv->hadjustment->upper = MAX (allocation->width, icon_view->priv->width);
735   gtk_adjustment_changed (icon_view->priv->hadjustment);
736
737   icon_view->priv->vadjustment->page_size = allocation->height;
738   icon_view->priv->vadjustment->page_increment = allocation->height * 0.9;
739   icon_view->priv->vadjustment->step_increment = allocation->width * 0.1;
740   icon_view->priv->vadjustment->lower = 0;
741   icon_view->priv->vadjustment->upper = MAX (allocation->height, icon_view->priv->height);
742   gtk_adjustment_changed (icon_view->priv->vadjustment);
743
744   gtk_icon_view_layout (icon_view);
745 }
746
747 static gboolean
748 gtk_icon_view_expose (GtkWidget *widget,
749                       GdkEventExpose *expose)
750 {
751   GtkIconView *icon_view;
752   GList *icons;
753
754   icon_view = GTK_ICON_VIEW (widget);
755
756   if (expose->window != icon_view->priv->bin_window)
757     return FALSE;
758
759   for (icons = icon_view->priv->items; icons; icons = icons->next) {
760     GtkIconViewItem *item = icons->data;
761     GdkRectangle item_rectangle;
762
763     item_rectangle.x = item->x;
764     item_rectangle.y = item->y;
765     item_rectangle.width = item->width;
766     item_rectangle.height = item->height;
767
768     if (gdk_region_rect_in (expose->region, &item_rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
769       continue;
770
771     gtk_icon_view_paint_item (icon_view, item, &expose->area);
772   }
773
774   if (icon_view->priv->doing_rubberband)
775     {
776       GdkRectangle *rectangles;
777       gint n_rectangles;
778       
779       gdk_region_get_rectangles (expose->region,
780                                  &rectangles,
781                                  &n_rectangles);
782       
783       while (n_rectangles--)
784         gtk_icon_view_paint_rubberband (icon_view, &rectangles[n_rectangles]);
785
786       g_free (rectangles);
787     }
788
789   return TRUE;
790 }
791
792 static gboolean
793 scroll_timeout (gpointer data)
794 {
795   GtkIconView *icon_view;
796   gdouble value;
797   
798   icon_view = data;
799
800   value = MIN (icon_view->priv->vadjustment->value +
801                icon_view->priv->scroll_value_diff,
802                icon_view->priv->vadjustment->upper -
803                icon_view->priv->vadjustment->page_size);
804
805   gtk_adjustment_set_value (icon_view->priv->vadjustment,
806                             value);
807
808   gtk_icon_view_update_rubberband (icon_view);
809   
810   return TRUE;
811 }
812
813 static gboolean
814 gtk_icon_view_motion (GtkWidget      *widget,
815                       GdkEventMotion *event)
816 {
817   GtkIconView *icon_view;
818   gint abs_y;
819   
820   icon_view = GTK_ICON_VIEW (widget);
821 #ifdef DND_WORKS
822   gtk_icon_view_maybe_begin_dragging_items (icon_view, event);
823 #endif
824   if (icon_view->priv->doing_rubberband)
825     {
826       gtk_icon_view_update_rubberband (widget);
827       
828       abs_y = event->y - icon_view->priv->height *
829         (icon_view->priv->vadjustment->value /
830          (icon_view->priv->vadjustment->upper -
831           icon_view->priv->vadjustment->lower));
832
833       if (abs_y < 0 || abs_y > widget->allocation.height)
834         {
835           if (icon_view->priv->scroll_timeout_id == 0)
836             icon_view->priv->scroll_timeout_id = g_timeout_add (30, scroll_timeout, icon_view);
837
838           if (abs_y < 0)
839             icon_view->priv->scroll_value_diff = abs_y;
840           else
841             icon_view->priv->scroll_value_diff = abs_y - widget->allocation.height;
842
843           icon_view->priv->event_last_x = event->x;
844           icon_view->priv->event_last_y = event->y;
845         }
846       else if (icon_view->priv->scroll_timeout_id != 0)
847         {
848           g_source_remove (icon_view->priv->scroll_timeout_id);
849
850           icon_view->priv->scroll_timeout_id = 0;
851         }
852     }
853   
854   return TRUE;
855 }
856
857 static gboolean
858 gtk_icon_view_button_press (GtkWidget      *widget,
859                             GdkEventButton *event)
860 {
861   GtkIconView *icon_view;
862   GtkIconViewItem *item;
863   gboolean dirty = FALSE;
864
865   icon_view = GTK_ICON_VIEW (widget);
866
867   if (event->window != icon_view->priv->bin_window)
868     return FALSE;
869
870   if (!GTK_WIDGET_HAS_FOCUS (widget))
871     gtk_widget_grab_focus (widget);
872
873   if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
874     {
875
876       item = gtk_icon_view_get_item_at_pos (icon_view,
877                                             event->x, event->y);
878       
879       if (item != NULL)
880         {
881           gtk_icon_view_scroll_to_item (icon_view, item);
882           
883           if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
884             {
885               gtk_icon_view_set_cursor_item (icon_view, item);
886             }
887           else if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
888                    (event->state & GDK_SHIFT_MASK))
889             {
890               gtk_icon_view_unselect_all_internal (icon_view);
891
892               gtk_icon_view_set_cursor_item (icon_view, item);
893               if (!icon_view->priv->anchor_item)
894                 icon_view->priv->anchor_item = item;
895               else 
896                 gtk_icon_view_select_all_between (icon_view,
897                                                   icon_view->priv->anchor_item,
898                                                   item);
899               dirty = TRUE;
900             }
901           else 
902             {
903               if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
904                   (event->state & GDK_CONTROL_MASK))
905                 {
906                   item->selected = !item->selected;
907                   gtk_icon_view_queue_draw_item (icon_view, item);
908                   dirty = TRUE;
909                 }
910               else
911                 {
912                   if (!item->selected)
913                     {
914                       gtk_icon_view_unselect_all_internal (icon_view);
915                       
916                       item->selected = TRUE;
917                       gtk_icon_view_queue_draw_item (icon_view, item);
918                       dirty = TRUE;
919                     }
920                 }
921               gtk_icon_view_set_cursor_item (icon_view, item);
922               icon_view->priv->anchor_item = item;
923             }
924 #ifdef DND_WORKS
925           /* Save press to possibly begin a drag */
926           if (icon_view->priv->pressed_button < 0)
927             {
928               icon_view->priv->pressed_button = event->button;
929               icon_view->priv->press_start_x = event->x;
930               icon_view->priv->press_start_y = event->y;
931             }
932 #endif
933           if (!icon_view->priv->last_single_clicked)
934             icon_view->priv->last_single_clicked = item;
935         }
936       else
937         {
938           if (icon_view->priv->selection_mode != GTK_SELECTION_BROWSE &&
939               !(event->state & GDK_CONTROL_MASK))
940             {
941               dirty = gtk_icon_view_unselect_all_internal (icon_view);
942             }
943           
944           if (icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
945             gtk_icon_view_start_rubberbanding (icon_view, event->x, event->y);
946         }
947
948     }
949
950   if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
951     {
952       item = gtk_icon_view_get_item_at_pos (icon_view,
953                                             event->x, event->y);
954
955       if (item && item == icon_view->priv->last_single_clicked)
956         {
957           GtkTreePath *path;
958
959           path = gtk_tree_path_new_from_indices (item->index, -1);
960           gtk_icon_view_item_activated (icon_view, path);
961           gtk_tree_path_free (path);
962         }
963
964       icon_view->priv->last_single_clicked = NULL;
965     }
966   
967   if (dirty)
968     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
969
970   return TRUE;
971 }
972
973 static gboolean
974 gtk_icon_view_button_release (GtkWidget      *widget,
975                               GdkEventButton *event)
976 {
977   GtkIconView *icon_view;
978
979   icon_view = GTK_ICON_VIEW (widget);
980   
981 #ifdef DND_WORKS
982   if (icon_view->priv->pressed_button == event->button)
983     icon_view->priv->pressed_button = -1;
984 #endif
985   gtk_icon_view_stop_rubberbanding (icon_view);
986
987   if (icon_view->priv->scroll_timeout_id != 0)
988     {
989       g_source_remove (icon_view->priv->scroll_timeout_id);
990       icon_view->priv->scroll_timeout_id = 0;
991     }
992
993   return TRUE;
994 }
995
996
997 static void
998 gtk_icon_view_update_rubberband (gpointer data)
999 {
1000   GtkIconView *icon_view;
1001   gint x, y;
1002   GdkRectangle old_area;
1003   GdkRectangle new_area;
1004   GdkRectangle common;
1005   GdkRegion *invalid_region;
1006   
1007   icon_view = GTK_ICON_VIEW (data);
1008
1009   gdk_window_get_pointer (icon_view->priv->bin_window, &x, &y, NULL);
1010
1011   x = MAX (x, 0);
1012   y = MAX (y, 0);
1013
1014   old_area.x = MIN (icon_view->priv->rubberband_x1,
1015                     icon_view->priv->rubberband_x2);
1016   old_area.y = MIN (icon_view->priv->rubberband_y1,
1017                     icon_view->priv->rubberband_y2);
1018   old_area.width = ABS (icon_view->priv->rubberband_x2 -
1019                         icon_view->priv->rubberband_x1) + 1;
1020   old_area.height = ABS (icon_view->priv->rubberband_y2 -
1021                          icon_view->priv->rubberband_y1) + 1;
1022   
1023   new_area.x = MIN (icon_view->priv->rubberband_x1, x);
1024   new_area.y = MIN (icon_view->priv->rubberband_y1, y);
1025   new_area.width = ABS (x - icon_view->priv->rubberband_x1) + 1;
1026   new_area.height = ABS (y - icon_view->priv->rubberband_y1) + 1;
1027
1028   invalid_region = gdk_region_rectangle (&old_area);
1029   gdk_region_union_with_rect (invalid_region, &new_area);
1030
1031   gdk_rectangle_intersect (&old_area, &new_area, &common);
1032   if (common.width > 2 && common.height > 2)
1033     {
1034       GdkRegion *common_region;
1035
1036       /* make sure the border is invalidated */
1037       common.x += 1;
1038       common.y += 1;
1039       common.width -= 2;
1040       common.height -= 2;
1041       
1042       common_region = gdk_region_rectangle (&common);
1043
1044       gdk_region_subtract (invalid_region, common_region);
1045       gdk_region_destroy (common_region);
1046     }
1047   
1048   gdk_window_invalidate_region (icon_view->priv->bin_window, invalid_region, TRUE);
1049     
1050   gdk_region_destroy (invalid_region);
1051
1052   icon_view->priv->rubberband_x2 = x;
1053   icon_view->priv->rubberband_y2 = y;  
1054
1055   gtk_icon_view_update_rubberband_selection (icon_view);
1056 }
1057
1058 static void
1059 gtk_icon_view_start_rubberbanding (GtkIconView  *icon_view,
1060                                    gint          x,
1061                                    gint          y)
1062 {
1063   GList *items;
1064
1065   g_assert (!icon_view->priv->doing_rubberband);
1066
1067   for (items = icon_view->priv->items; items; items = items->next)
1068     {
1069       GtkIconViewItem *item = items->data;
1070
1071       item->selected_before_rubberbanding = item->selected;
1072     }
1073   
1074   icon_view->priv->rubberband_x1 = x;
1075   icon_view->priv->rubberband_y1 = y;
1076   icon_view->priv->rubberband_x2 = x;
1077   icon_view->priv->rubberband_y2 = y;
1078
1079   icon_view->priv->doing_rubberband = TRUE;
1080
1081   gtk_grab_add (GTK_WIDGET (icon_view));
1082 }
1083
1084 static void
1085 gtk_icon_view_stop_rubberbanding (GtkIconView *icon_view)
1086 {
1087   if (!icon_view->priv->doing_rubberband)
1088     return;
1089
1090   icon_view->priv->doing_rubberband = FALSE;
1091
1092   gtk_grab_remove (GTK_WIDGET (icon_view));
1093   
1094   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
1095 }
1096
1097 static void
1098 gtk_icon_view_update_rubberband_selection (GtkIconView *icon_view)
1099 {
1100   GList *items;
1101   gint x, y, width, height;
1102   gboolean dirty = FALSE;
1103   
1104   x = MIN (icon_view->priv->rubberband_x1,
1105            icon_view->priv->rubberband_x2);
1106   y = MIN (icon_view->priv->rubberband_y1,
1107            icon_view->priv->rubberband_y2);
1108   width = ABS (icon_view->priv->rubberband_x1 - 
1109                icon_view->priv->rubberband_x2);
1110   height = ABS (icon_view->priv->rubberband_y1 - 
1111                 icon_view->priv->rubberband_y2);
1112   
1113   for (items = icon_view->priv->items; items; items = items->next)
1114     {
1115       GtkIconViewItem *item = items->data;
1116       gboolean is_in;
1117       gboolean selected;
1118       
1119       is_in = gtk_icon_view_item_hit_test (item, x, y, width, height);
1120
1121       selected = is_in ^ item->selected_before_rubberbanding;
1122
1123       if (item->selected != selected)
1124         {
1125           item->selected = selected;
1126           dirty = TRUE;
1127           gtk_icon_view_queue_draw_item (icon_view, item);
1128         }
1129     }
1130
1131   if (dirty)
1132     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
1133 }
1134
1135 static gboolean
1136 gtk_icon_view_item_hit_test (GtkIconViewItem  *item,
1137                              gint              x,
1138                              gint              y,
1139                              gint              width,
1140                              gint              height)
1141 {
1142   /* First try the pixbuf */
1143   if (MIN (x + width, item->pixbuf_x + item->pixbuf_width) - MAX (x, item->pixbuf_x) > 0 &&
1144       MIN (y + height, item->pixbuf_y + item->pixbuf_height) - MAX (y, item->pixbuf_y) > 0)
1145     return TRUE;
1146
1147   /* Then try the text */
1148   if (MIN (x + width, item->layout_x + item->layout_width) - MAX (x, item->layout_x) > 0 &&
1149       MIN (y + height, item->layout_y + item->layout_height) - MAX (y, item->layout_y) > 0)
1150     return TRUE;
1151   
1152   return FALSE;
1153 }
1154
1155 #ifdef DND_WORKS
1156 static gboolean
1157 gtk_icon_view_maybe_begin_dragging_items (GtkIconView     *icon_view,
1158                                           GdkEventMotion  *event)
1159 {
1160   gboolean retval = FALSE;
1161   gint button;
1162   if (icon_view->priv->pressed_button < 0)
1163     return retval;
1164
1165   if (!gtk_drag_check_threshold (GTK_WIDGET (icon_view),
1166                                  icon_view->priv->press_start_x,
1167                                  icon_view->priv->press_start_y,
1168                                  event->x, event->y))
1169     return retval;
1170
1171   button = icon_view->priv->pressed_button;
1172   icon_view->priv->pressed_button = -1;
1173   
1174   {
1175     static GtkTargetEntry row_targets[] = {
1176       { "GTK_ICON_VIEW_ITEMS", GTK_TARGET_SAME_APP, 0 }
1177     };
1178     GtkTargetList *target_list;
1179     GdkDragContext *context;
1180     GtkIconViewItem *item;
1181     
1182     retval = TRUE;
1183     
1184     target_list = gtk_target_list_new (row_targets, G_N_ELEMENTS (row_targets));
1185
1186     context = gtk_drag_begin (GTK_WIDGET (icon_view),
1187                               target_list, GDK_ACTION_MOVE,
1188                               button,
1189                               (GdkEvent *)event);
1190
1191     item = gtk_icon_view_get_item_at_pos (icon_view,
1192                                           icon_view->priv->press_start_x,
1193                                           icon_view->priv->press_start_y);
1194     g_assert (item != NULL);
1195     gtk_drag_set_icon_pixbuf (context, gtk_icon_view_get_item_icon (icon_view, item),
1196                               event->x - item->x,
1197                               event->y - item->y);
1198   }
1199   
1200   return retval;
1201 }
1202 #endif
1203
1204 static gboolean
1205 gtk_icon_view_unselect_all_internal (GtkIconView  *icon_view)
1206 {
1207   gboolean dirty = FALSE;
1208   GList *items;
1209
1210   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
1211       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
1212     return FALSE;
1213   
1214   for (items = icon_view->priv->items; items; items = items->next)
1215     {
1216       GtkIconViewItem *item = items->data;
1217
1218       if (item->selected)
1219         {
1220           item->selected = FALSE;
1221           dirty = TRUE;
1222           gtk_icon_view_queue_draw_item (icon_view, item);
1223         }
1224     }
1225
1226   return dirty;
1227 }
1228
1229
1230 /* GtkIconView signals */
1231 static void
1232 gtk_icon_view_set_adjustments (GtkIconView   *icon_view,
1233                                GtkAdjustment *hadj,
1234                                GtkAdjustment *vadj)
1235 {
1236   gboolean need_adjust = FALSE;
1237
1238   if (hadj)
1239     g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
1240   else
1241     hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
1242   if (vadj)
1243     g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
1244   else
1245     vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
1246
1247   if (icon_view->priv->hadjustment && (icon_view->priv->hadjustment != hadj))
1248     {
1249       g_signal_handlers_disconnect_matched (icon_view->priv->hadjustment, G_SIGNAL_MATCH_DATA,
1250                                            0, 0, NULL, NULL, icon_view);
1251       g_object_unref (icon_view->priv->hadjustment);
1252     }
1253
1254   if (icon_view->priv->vadjustment && (icon_view->priv->vadjustment != vadj))
1255     {
1256       g_signal_handlers_disconnect_matched (icon_view->priv->vadjustment, G_SIGNAL_MATCH_DATA,
1257                                             0, 0, NULL, NULL, icon_view);
1258       g_object_unref (icon_view->priv->vadjustment);
1259     }
1260
1261   if (icon_view->priv->hadjustment != hadj)
1262     {
1263       icon_view->priv->hadjustment = hadj;
1264       g_object_ref (icon_view->priv->hadjustment);
1265       gtk_object_sink (GTK_OBJECT (icon_view->priv->hadjustment));
1266
1267       g_signal_connect (icon_view->priv->hadjustment, "value_changed",
1268                         G_CALLBACK (gtk_icon_view_adjustment_changed),
1269                         icon_view);
1270       need_adjust = TRUE;
1271     }
1272
1273   if (icon_view->priv->vadjustment != vadj)
1274     {
1275       icon_view->priv->vadjustment = vadj;
1276       g_object_ref (icon_view->priv->vadjustment);
1277       gtk_object_sink (GTK_OBJECT (icon_view->priv->vadjustment));
1278
1279       g_signal_connect (icon_view->priv->vadjustment, "value_changed",
1280                         G_CALLBACK (gtk_icon_view_adjustment_changed),
1281                         icon_view);
1282       need_adjust = TRUE;
1283     }
1284
1285   if (need_adjust)
1286     gtk_icon_view_adjustment_changed (NULL, icon_view);
1287 }
1288
1289 static void
1290 gtk_icon_view_real_select_all (GtkIconView *icon_view)
1291 {
1292   gtk_icon_view_select_all (icon_view);
1293 }
1294
1295 static void
1296 gtk_icon_view_real_unselect_all (GtkIconView *icon_view)
1297 {
1298   if (icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
1299     return;
1300
1301   gtk_icon_view_unselect_all (icon_view);
1302 }
1303
1304 static void
1305 gtk_icon_view_real_select_cursor_item (GtkIconView *icon_view)
1306 {
1307   gtk_icon_view_unselect_all (icon_view);
1308   
1309   if (icon_view->priv->cursor_item != NULL)
1310     gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
1311 }
1312
1313 static void
1314 gtk_icon_view_real_toggle_cursor_item (GtkIconView *icon_view)
1315 {
1316   if (!icon_view->priv->cursor_item)
1317     return;
1318
1319   switch (icon_view->priv->selection_mode)
1320     {
1321     case GTK_SELECTION_NONE:
1322       break;
1323     case GTK_SELECTION_BROWSE:
1324       gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
1325       break;
1326     case GTK_SELECTION_SINGLE:
1327       if (icon_view->priv->cursor_item->selected)
1328         gtk_icon_view_unselect_item (icon_view, icon_view->priv->cursor_item);
1329       else
1330         gtk_icon_view_select_item (icon_view, icon_view->priv->cursor_item);
1331       break;
1332     case GTK_SELECTION_MULTIPLE:
1333       icon_view->priv->cursor_item->selected = !icon_view->priv->cursor_item->selected;
1334       g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0); 
1335       
1336       gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
1337       break;
1338     }
1339 }
1340
1341 /* Internal functions */
1342 static void
1343 gtk_icon_view_adjustment_changed (GtkAdjustment *adjustment,
1344                                   GtkIconView   *icon_view)
1345 {
1346   if (GTK_WIDGET_REALIZED (icon_view))
1347     {
1348       gdk_window_move (icon_view->priv->bin_window,
1349                        - icon_view->priv->hadjustment->value,
1350                        - icon_view->priv->vadjustment->value);
1351
1352       if (icon_view->priv->doing_rubberband)
1353         gtk_icon_view_update_rubberband (GTK_WIDGET (icon_view));
1354
1355       gdk_window_process_updates (icon_view->priv->bin_window, TRUE);
1356     }
1357 }
1358
1359 static GList *
1360 gtk_icon_view_layout_single_row (GtkIconView *icon_view, GList *first_item, gint *y, gint *maximum_width, gint row)
1361 {
1362   gint x, current_width, max_height, max_pixbuf_height;
1363   GList *items, *last_item;
1364   gint maximum_layout_width;
1365   gint col;
1366   gboolean rtl = gtk_widget_get_direction (GTK_WIDGET (icon_view)) == GTK_TEXT_DIR_RTL;
1367
1368   x = 0;
1369   col = 0;
1370   max_height = 0;
1371   max_pixbuf_height = 0;
1372   items = first_item;
1373   current_width = 0;
1374
1375   x += ICON_VIEW_LEFT_MARGIN;
1376   current_width += ICON_VIEW_LEFT_MARGIN + ICON_VIEW_RIGHT_MARGIN;
1377   items = first_item;
1378
1379   while (items)
1380     {
1381       GtkIconViewItem *item = items->data;
1382
1383       gtk_icon_view_calculate_item_size (icon_view, item);
1384
1385       current_width += MAX (item->width, MINIMUM_ICON_ITEM_WIDTH);
1386
1387       /* Don't add padding to the first or last icon */
1388       
1389       if (current_width > GTK_WIDGET (icon_view)->allocation.width &&
1390           items != first_item)
1391         break;
1392
1393       maximum_layout_width = MAX (item->pixbuf_width, MINIMUM_ICON_ITEM_WIDTH);
1394
1395       item->y = *y;
1396       item->x = rtl ? GTK_WIDGET (icon_view)->allocation.width - item->width - x : x;
1397       if (item->width < MINIMUM_ICON_ITEM_WIDTH) {
1398         if (rtl)
1399           item->x -= (MINIMUM_ICON_ITEM_WIDTH - item->width) / 2;
1400         else
1401           item->x += (MINIMUM_ICON_ITEM_WIDTH - item->width) / 2;
1402         x += (MINIMUM_ICON_ITEM_WIDTH - item->width);
1403       }
1404
1405       item->pixbuf_x = item->x + (item->width - item->pixbuf_width) / 2;
1406       item->layout_x = item->x + (item->width - item->layout_width) / 2;
1407
1408       x += item->width;
1409
1410       max_height = MAX (max_height, item->height);
1411       max_pixbuf_height = MAX (max_pixbuf_height, item->pixbuf_height);
1412       
1413       if (current_width > *maximum_width)
1414         *maximum_width = current_width;
1415
1416       item->row = row;
1417       item->col = col;
1418
1419       col++;
1420       items = items->next;
1421     }
1422
1423   last_item = items;
1424
1425   *y += max_height + ICON_VIEW_ICON_PADDING;
1426
1427   /* Now go through the row again and align the icons */
1428   for (items = first_item; items != last_item; items = items->next)
1429     {
1430       GtkIconViewItem *item = items->data;
1431
1432       item->pixbuf_y = item->y + (max_pixbuf_height - item->pixbuf_height);
1433       item->layout_y = item->pixbuf_y + item->pixbuf_height + ICON_TEXT_PADDING;
1434       /* Update the bounding box */
1435       item->y = item->pixbuf_y;
1436
1437       /* We may want to readjust the new y coordinate. */
1438       if (item->y + item->height > *y)
1439         *y = item->y + item->height;
1440
1441       if (rtl)
1442         item->col = col - 1 - item->col;
1443     }
1444   
1445   return last_item;
1446 }
1447
1448 static void
1449 gtk_icon_view_set_adjustment_upper (GtkAdjustment *adj,
1450                                     gdouble        upper)
1451 {
1452   if (upper != adj->upper)
1453     {
1454       gdouble min = MAX (0.0, upper - adj->page_size);
1455       gboolean value_changed = FALSE;
1456       
1457       adj->upper = upper;
1458
1459       if (adj->value > min)
1460         {
1461           adj->value = min;
1462           value_changed = TRUE;
1463         }
1464       
1465       gtk_adjustment_changed (adj);
1466       
1467       if (value_changed)
1468         gtk_adjustment_value_changed (adj);
1469     }
1470 }
1471
1472 static void
1473 gtk_icon_view_layout (GtkIconView *icon_view)
1474 {
1475   gint y = 0, maximum_width = 0;
1476   GList *icons;
1477   GtkWidget *widget;
1478   gint row;
1479   
1480   widget = GTK_WIDGET (icon_view);
1481   icons = icon_view->priv->items;
1482
1483   y += ICON_VIEW_TOP_MARGIN;
1484   row = 0;
1485
1486   do
1487     {
1488       icons = gtk_icon_view_layout_single_row (icon_view, icons, &y, &maximum_width, row);
1489       row++;
1490     }
1491   while (icons != NULL);
1492
1493   if (maximum_width != icon_view->priv->width)
1494     {
1495       icon_view->priv->width = maximum_width;
1496     }
1497   y += ICON_VIEW_BOTTOM_MARGIN;
1498   
1499   if (y != icon_view->priv->height)
1500     {
1501       icon_view->priv->height = y;
1502     }
1503
1504   gtk_icon_view_set_adjustment_upper (icon_view->priv->hadjustment, icon_view->priv->width);
1505   gtk_icon_view_set_adjustment_upper (icon_view->priv->vadjustment, icon_view->priv->height);
1506
1507   if (GTK_WIDGET_REALIZED (icon_view))
1508     {
1509       gdk_window_resize (icon_view->priv->bin_window,
1510                          MAX (icon_view->priv->width, widget->allocation.width),
1511                          MAX (icon_view->priv->height, widget->allocation.height));
1512     }
1513
1514   if (icon_view->priv->layout_idle_id != 0)
1515     {
1516       g_source_remove (icon_view->priv->layout_idle_id);
1517       icon_view->priv->layout_idle_id = 0;
1518     }
1519
1520   gtk_widget_queue_draw (GTK_WIDGET (icon_view));
1521 }
1522
1523 /* Updates the pango layout and calculates the size */
1524 static void
1525 gtk_icon_view_calculate_item_size (GtkIconView *icon_view,
1526                                    GtkIconViewItem *item)
1527 {
1528   int layout_width, layout_height;
1529   int maximum_layout_width;
1530   GdkPixbuf *pixbuf;
1531   
1532   if (item->width != -1 && item->width != -1) 
1533     return;
1534
1535   if (icon_view->priv->pixbuf_column != -1)
1536     {
1537       pixbuf = gtk_icon_view_get_item_icon (icon_view, item);
1538       item->pixbuf_width = gdk_pixbuf_get_width (pixbuf);
1539       item->pixbuf_height = gdk_pixbuf_get_height (pixbuf);
1540       g_object_unref (pixbuf);
1541     }
1542   else
1543     {
1544       item->pixbuf_width = 0;
1545       item->pixbuf_height = 0;
1546     }
1547   
1548   maximum_layout_width = MAX (item->pixbuf_width, MINIMUM_ICON_ITEM_WIDTH);
1549
1550   if (icon_view->priv->markup_column != 1 ||
1551       icon_view->priv->text_column != -1)
1552     {
1553       gtk_icon_view_update_item_text (icon_view, item);
1554
1555       pango_layout_set_alignment (icon_view->priv->layout, PANGO_ALIGN_CENTER);
1556       pango_layout_set_width (icon_view->priv->layout, maximum_layout_width * PANGO_SCALE);
1557       
1558       pango_layout_get_pixel_size (icon_view->priv->layout, &layout_width, &layout_height);
1559       
1560       item->width = MAX ((layout_width + 2 * ICON_TEXT_PADDING), item->pixbuf_width);
1561       item->height = layout_height + 2 * ICON_TEXT_PADDING + item->pixbuf_height;
1562       item->layout_width = layout_width;
1563       item->layout_height = layout_height;
1564     }
1565   else
1566     {
1567       item->layout_width = 0;
1568       item->layout_height = 0;
1569     }
1570 }
1571
1572 static void
1573 gtk_icon_view_invalidate_sizes (GtkIconView *icon_view)
1574 {
1575   g_list_foreach (icon_view->priv->items,
1576                   (GFunc)gtk_icon_view_item_invalidate_size, NULL);
1577 }
1578
1579 static void
1580 gtk_icon_view_item_invalidate_size (GtkIconViewItem *item)
1581 {
1582   item->width = -1;
1583   item->height = -1;
1584 }
1585
1586 static GdkPixbuf *
1587 create_colorized_pixbuf (GdkPixbuf *src, GdkColor *new_color)
1588 {
1589         gint i, j;
1590         gint width, height, has_alpha, src_row_stride, dst_row_stride;
1591         gint red_value, green_value, blue_value;
1592         guchar *target_pixels;
1593         guchar *original_pixels;
1594         guchar *pixsrc;
1595         guchar *pixdest;
1596         GdkPixbuf *dest;
1597
1598         red_value = new_color->red / 255.0;
1599         green_value = new_color->green / 255.0;
1600         blue_value = new_color->blue / 255.0;
1601
1602         dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
1603                                gdk_pixbuf_get_has_alpha (src),
1604                                gdk_pixbuf_get_bits_per_sample (src),
1605                                gdk_pixbuf_get_width (src),
1606                                gdk_pixbuf_get_height (src));
1607         
1608         has_alpha = gdk_pixbuf_get_has_alpha (src);
1609         width = gdk_pixbuf_get_width (src);
1610         height = gdk_pixbuf_get_height (src);
1611         src_row_stride = gdk_pixbuf_get_rowstride (src);
1612         dst_row_stride = gdk_pixbuf_get_rowstride (dest);
1613         target_pixels = gdk_pixbuf_get_pixels (dest);
1614         original_pixels = gdk_pixbuf_get_pixels (src);
1615
1616         for (i = 0; i < height; i++) {
1617                 pixdest = target_pixels + i*dst_row_stride;
1618                 pixsrc = original_pixels + i*src_row_stride;
1619                 for (j = 0; j < width; j++) {           
1620                         *pixdest++ = (*pixsrc++ * red_value) >> 8;
1621                         *pixdest++ = (*pixsrc++ * green_value) >> 8;
1622                         *pixdest++ = (*pixsrc++ * blue_value) >> 8;
1623                         if (has_alpha) {
1624                                 *pixdest++ = *pixsrc++;
1625                         }
1626                 }
1627         }
1628         return dest;
1629 }
1630
1631 static void
1632 gtk_icon_view_paint_item (GtkIconView     *icon_view,
1633                           GtkIconViewItem *item,
1634                           GdkRectangle    *area)
1635 {
1636   GdkPixbuf *pixbuf, *tmp;
1637   GtkStateType state;
1638   
1639   if (!VALID_MODEL_AND_COLUMNS (icon_view))
1640     return;
1641   
1642   if (GTK_WIDGET_HAS_FOCUS (icon_view))
1643     state = GTK_STATE_SELECTED;
1644   else
1645     state = GTK_STATE_ACTIVE;
1646
1647   if (icon_view->priv->pixbuf_column != -1)
1648     {
1649       tmp = gtk_icon_view_get_item_icon (icon_view, item);
1650       if (item->selected)
1651         {
1652           pixbuf = create_colorized_pixbuf (tmp,
1653                                             &GTK_WIDGET (icon_view)->style->base[state]);
1654           g_object_unref (tmp);
1655         }
1656       else
1657         pixbuf = tmp;
1658       
1659       gdk_draw_pixbuf (icon_view->priv->bin_window, NULL, pixbuf,
1660                        0, 0,
1661                        item->pixbuf_x, item->pixbuf_y,
1662                        item->pixbuf_width, item->pixbuf_height,
1663                        GDK_RGB_DITHER_NORMAL,
1664                        item->pixbuf_width, item->pixbuf_height);
1665       g_object_unref (pixbuf);
1666     }
1667
1668   if (icon_view->priv->text_column != -1)
1669     {
1670       if (item->selected)
1671         {
1672           gdk_draw_rectangle (icon_view->priv->bin_window,
1673                               GTK_WIDGET (icon_view)->style->base_gc[state],
1674                               TRUE,
1675                               item->layout_x - ICON_TEXT_PADDING,
1676                               item->layout_y - ICON_TEXT_PADDING,
1677                               item->layout_width + 2 * ICON_TEXT_PADDING,
1678                               item->layout_height + 2 * ICON_TEXT_PADDING);
1679         }
1680
1681       gtk_icon_view_update_item_text (icon_view, item);
1682       gtk_paint_layout (GTK_WIDGET (icon_view)->style,
1683                         icon_view->priv->bin_window,
1684                         item->selected ? state : GTK_STATE_NORMAL,
1685                         TRUE, area, GTK_WIDGET (icon_view), "icon_view",
1686                         item->layout_x - ((item->width - item->layout_width) / 2) - (MAX (item->pixbuf_width, MINIMUM_ICON_ITEM_WIDTH) - item->width) / 2,
1687                         item->layout_y,
1688                         icon_view->priv->layout);
1689
1690       if (GTK_WIDGET_HAS_FOCUS (icon_view) &&
1691           item == icon_view->priv->cursor_item)
1692         gtk_paint_focus (GTK_WIDGET (icon_view)->style,
1693                          icon_view->priv->bin_window,
1694                          item->selected ? GTK_STATE_SELECTED : GTK_STATE_NORMAL,
1695                          area,
1696                          GTK_WIDGET (icon_view),
1697                          "icon_view",
1698                          item->layout_x - ICON_TEXT_PADDING,
1699                          item->layout_y - ICON_TEXT_PADDING,
1700                          item->layout_width + 2 * ICON_TEXT_PADDING,
1701                          item->layout_height + 2 * ICON_TEXT_PADDING);
1702     }
1703 }
1704
1705 static guint32
1706 gtk_gdk_color_to_rgb (const GdkColor *color)
1707 {
1708   guint32 result;
1709   result = (0xff0000 | (color->red & 0xff00));
1710   result <<= 8;
1711   result |= ((color->green & 0xff00) | (color->blue >> 8));
1712   return result;
1713 }
1714
1715 static void
1716 gtk_icon_view_paint_rubberband (GtkIconView     *icon_view,
1717                                 GdkRectangle    *area)
1718 {
1719   GdkRectangle rect;
1720   GdkPixbuf *pixbuf;
1721   GdkGC *gc;
1722   GdkRectangle rubber_rect;
1723   GdkColor *fill_color_gdk;
1724   guint fill_color;
1725   guchar fill_color_alpha;
1726
1727   rubber_rect.x = MIN (icon_view->priv->rubberband_x1, icon_view->priv->rubberband_x2);
1728   rubber_rect.y = MIN (icon_view->priv->rubberband_y1, icon_view->priv->rubberband_y2);
1729   rubber_rect.width = ABS (icon_view->priv->rubberband_x1 - icon_view->priv->rubberband_x2) + 1;
1730   rubber_rect.height = ABS (icon_view->priv->rubberband_y1 - icon_view->priv->rubberband_y2) + 1;
1731
1732   if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
1733     return;
1734
1735   gtk_widget_style_get (GTK_WIDGET (icon_view),
1736                         "selection_box_color", &fill_color_gdk,
1737                         "selection_box_alpha", &fill_color_alpha,
1738                         NULL);
1739
1740   if (!fill_color_gdk) {
1741     fill_color_gdk = gdk_color_copy (&GTK_WIDGET (icon_view)->style->base[GTK_STATE_SELECTED]);
1742   }
1743
1744   fill_color = gtk_gdk_color_to_rgb (fill_color_gdk) << 8 | fill_color_alpha;
1745
1746   pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, rect.width, rect.height);
1747   gdk_pixbuf_fill (pixbuf, fill_color);
1748
1749   gdk_draw_pixbuf (icon_view->priv->bin_window, NULL, pixbuf,
1750                    0, 0, 
1751                    rect.x,rect.y,
1752                    rect.width, rect.height,
1753                    GDK_RGB_DITHER_NONE,
1754                    0, 0);
1755   g_object_unref (pixbuf);
1756   gc = gdk_gc_new (icon_view->priv->bin_window);
1757   gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
1758   gdk_gc_set_clip_rectangle (gc, &rect);
1759   gdk_draw_rectangle (icon_view->priv->bin_window,
1760                       gc, FALSE,
1761                       rubber_rect.x, rubber_rect.y,
1762                       rubber_rect.width - 1, rubber_rect.height - 1);
1763   gdk_color_free (fill_color_gdk);
1764   g_object_unref (gc);
1765 }
1766
1767 static void
1768 gtk_icon_view_queue_draw_item (GtkIconView     *icon_view,
1769                                GtkIconViewItem *item)
1770 {
1771   GdkRectangle rect;
1772
1773   rect.x = item->x;
1774   rect.y = item->y;
1775   rect.width = item->width;
1776   rect.height = item->height;
1777
1778   if (icon_view->priv->bin_window)
1779     gdk_window_invalidate_rect (icon_view->priv->bin_window, &rect, TRUE);
1780 }
1781
1782 static gboolean
1783 layout_callback (gpointer user_data)
1784 {
1785   GtkIconView *icon_view;
1786
1787   icon_view = GTK_ICON_VIEW (user_data);
1788   
1789   icon_view->priv->layout_idle_id = 0;
1790
1791   gtk_icon_view_layout (icon_view);
1792   
1793   return FALSE;
1794 }
1795
1796 static void
1797 gtk_icon_view_queue_layout (GtkIconView *icon_view)
1798 {
1799   if (icon_view->priv->layout_idle_id != 0)
1800     return;
1801
1802   icon_view->priv->layout_idle_id = g_idle_add (layout_callback, icon_view);
1803 }
1804
1805 static void
1806 gtk_icon_view_set_cursor_item (GtkIconView     *icon_view,
1807                                GtkIconViewItem *item)
1808 {
1809   if (icon_view->priv->cursor_item == item)
1810     return;
1811
1812   if (icon_view->priv->cursor_item != NULL)
1813     gtk_icon_view_queue_draw_item (icon_view, icon_view->priv->cursor_item);
1814   
1815   icon_view->priv->cursor_item = item;
1816   gtk_icon_view_queue_draw_item (icon_view, item);
1817 }
1818
1819
1820 static GtkIconViewItem *
1821 gtk_icon_view_item_new (void)
1822 {
1823   GtkIconViewItem *item;
1824
1825   item = g_new0 (GtkIconViewItem, 1);
1826
1827   item->ref_count = 1;
1828   item->width = -1;
1829   item->height = -1;
1830   
1831   return item;
1832 }
1833
1834 static void
1835 gtk_icon_view_item_free (GtkIconViewItem *item)
1836 {
1837   g_return_if_fail (item != NULL);
1838
1839   g_free (item);
1840 }
1841
1842 static void
1843 gtk_icon_view_update_item_text (GtkIconView     *icon_view,
1844                                 GtkIconViewItem *item)
1845 {
1846   gboolean iters_persist;
1847   GtkTreeIter iter;
1848   GtkTreePath *path;
1849   gchar *text;
1850   
1851   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
1852   
1853   if (!iters_persist)
1854     {
1855       path = gtk_tree_path_new_from_indices (item->index, -1);
1856       gtk_tree_model_get_iter (icon_view->priv->model, &iter, path);
1857       gtk_tree_path_free (path);
1858     }
1859   else
1860     iter = item->iter;
1861
1862   if (icon_view->priv->markup_column != -1)
1863     {
1864       gtk_tree_model_get (icon_view->priv->model, &iter,
1865                           icon_view->priv->markup_column, &text,
1866                           -1);
1867       pango_layout_set_markup (icon_view->priv->layout, text, -1);
1868     }
1869   else
1870     {
1871       gtk_tree_model_get (icon_view->priv->model, &iter,
1872                           icon_view->priv->text_column, &text,
1873                           -1);
1874       pango_layout_set_text (icon_view->priv->layout, text, -1);
1875     }
1876
1877   g_free (text);        
1878 }
1879
1880 static GdkPixbuf *
1881 gtk_icon_view_get_item_icon (GtkIconView      *icon_view,
1882                              GtkIconViewItem  *item)
1883 {
1884   gboolean iters_persist;
1885   GtkTreeIter iter;
1886   GtkTreePath *path;
1887   GdkPixbuf *pixbuf;
1888   
1889   g_return_val_if_fail (item != NULL, NULL);
1890
1891   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
1892   
1893   if (!iters_persist)
1894     {
1895       path = gtk_tree_path_new_from_indices (item->index, -1);
1896       gtk_tree_model_get_iter (icon_view->priv->model, &iter, path);
1897       gtk_tree_path_free (path);
1898     }
1899   else
1900     iter = item->iter;
1901   
1902   gtk_tree_model_get (icon_view->priv->model, &iter,
1903                       icon_view->priv->pixbuf_column, &pixbuf,
1904                       -1);
1905
1906   return pixbuf;
1907 }
1908
1909
1910 static GtkIconViewItem *
1911 gtk_icon_view_get_item_at_pos (GtkIconView *icon_view,
1912                                gint         x,
1913                                gint         y)
1914 {
1915   GList *items;
1916   
1917   for (items = icon_view->priv->items; items; items = items->next)
1918     {
1919       GtkIconViewItem *item = items->data;
1920       
1921       if (x > item->x && x < item->x + item->width &&
1922           y > item->y && y < item->y + item->height)
1923         {
1924           gint layout_x = item->x + (item->width - item->layout_width) / 2;
1925           /* Check if the mouse is inside the icon or the label */
1926           if ((x > item->pixbuf_x && x < item->pixbuf_x + item->pixbuf_width &&
1927                y > item->pixbuf_y && y < item->pixbuf_y + item->pixbuf_height) ||
1928               (x > layout_x - ICON_TEXT_PADDING &&
1929                x < layout_x + item->layout_width + ICON_TEXT_PADDING * 2 &&
1930                y > item->layout_y - ICON_TEXT_PADDING
1931                && y < item->layout_y + item->layout_height + ICON_TEXT_PADDING * 2))
1932             return item;
1933         }
1934     }
1935
1936   return NULL;
1937 }
1938
1939 static void
1940 gtk_icon_view_select_item (GtkIconView      *icon_view,
1941                            GtkIconViewItem  *item)
1942 {
1943   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
1944   g_return_if_fail (item != NULL);
1945
1946   if (item->selected)
1947     return;
1948   
1949   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE)
1950     return;
1951   else if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
1952     gtk_icon_view_unselect_all_internal (icon_view);
1953
1954   item->selected = TRUE;
1955
1956   gtk_icon_view_queue_draw_item (icon_view, item);
1957
1958   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
1959 }
1960
1961
1962 static void
1963 gtk_icon_view_unselect_item (GtkIconView      *icon_view,
1964                              GtkIconViewItem  *item)
1965 {
1966   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
1967   g_return_if_fail (item != NULL);
1968
1969   if (!item->selected)
1970     return;
1971   
1972   if (icon_view->priv->selection_mode == GTK_SELECTION_NONE ||
1973       icon_view->priv->selection_mode == GTK_SELECTION_BROWSE)
1974     return;
1975   
1976   item->selected = FALSE;
1977
1978   g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
1979
1980   gtk_icon_view_queue_draw_item (icon_view, item);
1981 }
1982
1983 static void
1984 verify_items (GtkIconView *icon_view)
1985 {
1986   GList *items;
1987   int i = 0;
1988
1989   for (items = icon_view->priv->items; items; items = items->next)
1990     {
1991       GtkIconViewItem *item = items->data;
1992
1993       if (item->index != i)
1994         g_error ("List item does not match its index: item index %d and list index %d\n", item->index, i);
1995
1996       i++;
1997     }
1998 }
1999
2000 static void
2001 gtk_icon_view_row_changed (GtkTreeModel *model,
2002                            GtkTreePath  *path,
2003                            GtkTreeIter  *iter,
2004                            gpointer      data)
2005 {
2006   GtkIconViewItem *item;
2007   gint index;
2008   GtkIconView *icon_view;
2009
2010   icon_view = GTK_ICON_VIEW (data);
2011   
2012   index = gtk_tree_path_get_indices(path)[0];
2013   item = g_list_nth (icon_view->priv->items, index)->data;
2014
2015   gtk_icon_view_item_invalidate_size (item);
2016   gtk_icon_view_queue_layout (icon_view);
2017
2018   verify_items (icon_view);
2019 }
2020
2021 static void
2022 gtk_icon_view_row_inserted (GtkTreeModel *model,
2023                             GtkTreePath  *path,
2024                             GtkTreeIter  *iter,
2025                             gpointer      data)
2026 {
2027   gint length, index;
2028   GtkIconViewItem *item;
2029   gboolean iters_persist;
2030   GtkIconView *icon_view;
2031   GList *list;
2032   
2033   icon_view = GTK_ICON_VIEW (data);
2034   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
2035   
2036   length = gtk_tree_model_iter_n_children (model, NULL);
2037   index = gtk_tree_path_get_indices(path)[0];
2038
2039   item = gtk_icon_view_item_new ();
2040
2041   if (iters_persist)
2042     item->iter = *iter;
2043
2044   item->index = index;
2045
2046   /* FIXME: We can be more efficient here,
2047      we can store a tail pointer and use that when
2048      appending (which is a rather common operation)
2049   */
2050   icon_view->priv->items = g_list_insert (icon_view->priv->items,
2051                                          item, index);
2052   
2053   list = g_list_nth (icon_view->priv->items, index + 1);
2054   for (; list; list = list->next)
2055     {
2056       item = list->data;
2057
2058       item->index++;
2059     }
2060     
2061   verify_items (icon_view);
2062 }
2063
2064 static void
2065 gtk_icon_view_row_deleted (GtkTreeModel *model,
2066                            GtkTreePath  *path,
2067                            gpointer      data)
2068 {
2069   gint index;
2070   GtkIconView *icon_view;
2071   GtkIconViewItem *item;
2072   GList *list, *next;
2073   gboolean emit = FALSE;
2074   
2075   icon_view = GTK_ICON_VIEW (data);
2076
2077   index = gtk_tree_path_get_indices(path)[0];
2078
2079   list = g_list_nth (icon_view->priv->items, index);
2080   item = list->data;
2081
2082   if (item == icon_view->priv->anchor_item)
2083     icon_view->priv->anchor_item = NULL;
2084
2085   if (item == icon_view->priv->cursor_item)
2086     icon_view->priv->cursor_item = NULL;
2087
2088   if (item->selected)
2089     emit = TRUE;
2090   
2091   gtk_icon_view_item_free (item);
2092
2093   for (next = list->next; next; next = next->next)
2094     {
2095       item = next->data;
2096
2097       item->index--;
2098     }
2099   
2100   icon_view->priv->items = g_list_delete_link (icon_view->priv->items, list);
2101
2102   gtk_icon_view_queue_layout (icon_view);
2103
2104   verify_items (icon_view);  
2105   
2106   if (emit)
2107     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2108 }
2109
2110 static void
2111 gtk_icon_view_rows_reordered (GtkTreeModel *model,
2112                               GtkTreePath  *parent,
2113                               GtkTreeIter  *iter,
2114                               gint         *new_order,
2115                               gpointer      data)
2116 {
2117   int i;
2118   int length;
2119   GtkIconView *icon_view;
2120   GList *items = NULL, *list;
2121   gint *inverted_order;
2122   GtkIconViewItem **item_array;
2123   
2124   icon_view = GTK_ICON_VIEW (data);
2125
2126   length = gtk_tree_model_iter_n_children (model, NULL);
2127   inverted_order = g_new (gint, length);
2128
2129   /* Invert the array */
2130   for (i = 0; i < length; i++)
2131     inverted_order[new_order[i]] = i;
2132
2133   item_array = g_new (GtkIconViewItem *, length);
2134   for (i = 0, list = icon_view->priv->items; list != NULL; list = list->next, i++)
2135     item_array[inverted_order[i]] = list->data;
2136
2137   g_free (inverted_order);
2138   for (i = 0; i < length; i++)
2139     {
2140       item_array[i]->index = i;
2141       items = g_list_prepend (items, item_array[i]);
2142     }
2143   
2144   g_free (item_array);
2145   g_list_free (icon_view->priv->items);
2146   icon_view->priv->items = g_list_reverse (items);
2147
2148   verify_items (icon_view);  
2149 }
2150
2151 static void
2152 gtk_icon_view_build_items (GtkIconView *icon_view)
2153 {
2154   GtkTreeIter iter;
2155   int i;
2156   gboolean iters_persist;
2157   GList *items = NULL;
2158
2159   iters_persist = gtk_tree_model_get_flags (icon_view->priv->model) & GTK_TREE_MODEL_ITERS_PERSIST;
2160   
2161   if (!gtk_tree_model_get_iter_first (icon_view->priv->model,
2162                                       &iter))
2163     return;
2164
2165   i = 0;
2166   
2167   do
2168     {
2169       GtkIconViewItem *item = gtk_icon_view_item_new ();
2170
2171       if (iters_persist)
2172         item->iter = iter;
2173
2174       item->index = i;
2175       
2176       i++;
2177
2178       items = g_list_prepend (items, item);
2179       
2180     } while (gtk_tree_model_iter_next (icon_view->priv->model, &iter));
2181
2182   icon_view->priv->items = g_list_reverse (items);
2183 }
2184
2185 static void
2186 gtk_icon_view_add_move_binding (GtkBindingSet  *binding_set,
2187                                 guint           keyval,
2188                                 guint           modmask,
2189                                 GtkMovementStep step,
2190                                 gint            count)
2191 {
2192   
2193   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
2194                                 "move_cursor", 2,
2195                                 G_TYPE_ENUM, step,
2196                                 G_TYPE_INT, count);
2197
2198   gtk_binding_entry_add_signal (binding_set, keyval, GDK_SHIFT_MASK,
2199                                 "move_cursor", 2,
2200                                 G_TYPE_ENUM, step,
2201                                 G_TYPE_INT, count);
2202
2203   if ((modmask & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2204    return;
2205
2206   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
2207                                 "move_cursor", 2,
2208                                 G_TYPE_ENUM, step,
2209                                 G_TYPE_INT, count);
2210
2211   gtk_binding_entry_add_signal (binding_set, keyval, GDK_CONTROL_MASK,
2212                                 "move_cursor", 2,
2213                                 G_TYPE_ENUM, step,
2214                                 G_TYPE_INT, count);
2215 }
2216
2217 static gboolean
2218 gtk_icon_view_real_move_cursor (GtkIconView     *icon_view,
2219                                 GtkMovementStep  step,
2220                                 gint             count)
2221 {
2222   GdkModifierType state;
2223
2224   g_return_val_if_fail (GTK_ICON_VIEW (icon_view), FALSE);
2225   g_return_val_if_fail (step == GTK_MOVEMENT_LOGICAL_POSITIONS ||
2226                         step == GTK_MOVEMENT_VISUAL_POSITIONS ||
2227                         step == GTK_MOVEMENT_DISPLAY_LINES ||
2228                         step == GTK_MOVEMENT_PAGES ||
2229                         step == GTK_MOVEMENT_BUFFER_ENDS, FALSE);
2230
2231   if (!GTK_WIDGET_HAS_FOCUS (GTK_WIDGET (icon_view)))
2232     return FALSE;
2233
2234   gtk_widget_grab_focus (GTK_WIDGET (icon_view));
2235
2236   if (gtk_get_current_event_state (&state))
2237     {
2238       if ((state & GDK_CONTROL_MASK) == GDK_CONTROL_MASK)
2239         icon_view->priv->ctrl_pressed = TRUE;
2240       if ((state & GDK_SHIFT_MASK) == GDK_SHIFT_MASK)
2241         icon_view->priv->shift_pressed = TRUE;
2242     }
2243   /* else we assume not pressed */
2244
2245   switch (step)
2246     {
2247     case GTK_MOVEMENT_LOGICAL_POSITIONS:
2248     case GTK_MOVEMENT_VISUAL_POSITIONS:
2249       gtk_icon_view_move_cursor_left_right (icon_view, count);
2250       break;
2251     case GTK_MOVEMENT_DISPLAY_LINES:
2252       gtk_icon_view_move_cursor_up_down (icon_view, count);
2253       break;
2254     case GTK_MOVEMENT_PAGES:
2255       gtk_icon_view_move_cursor_page_up_down (icon_view, count);
2256       break;
2257     case GTK_MOVEMENT_BUFFER_ENDS:
2258       gtk_icon_view_move_cursor_start_end (icon_view, count);
2259       break;
2260     default:
2261       g_assert_not_reached ();
2262     }
2263
2264   icon_view->priv->ctrl_pressed = FALSE;
2265   icon_view->priv->shift_pressed = FALSE;
2266
2267   return TRUE;
2268 }
2269
2270 static GtkIconViewItem *
2271 find_item (GtkIconView     *icon_view,
2272            GtkIconViewItem *current,
2273            gint             row_ofs,
2274            gint             col_ofs)
2275 {
2276   gint row, col;
2277   GList *items;
2278   GtkIconViewItem *item;
2279
2280   /* FIXME: this could be more efficient 
2281    */
2282   row = current->row + row_ofs;
2283   col = current->col + col_ofs;
2284
2285   for (items = icon_view->priv->items; items; items = items->next)
2286     {
2287       item = items->data;
2288       if (item->row == row && item->col == col)
2289         return item;
2290     }
2291   
2292   return NULL;
2293 }
2294
2295
2296 static GtkIconViewItem *
2297 find_item_page_up_down (GtkIconView     *icon_view,
2298                         GtkIconViewItem *current,
2299                         gint             count)
2300 {
2301   GList *item, *next;
2302   gint y, col;
2303   
2304   col = current->col;
2305   y = current->y + count * icon_view->priv->vadjustment->page_size;
2306
2307   item = g_list_find (icon_view->priv->items, current);
2308   if (count > 0)
2309     {
2310       while (item)
2311         {
2312           for (next = item->next; next; next = next->next)
2313             {
2314               if (((GtkIconViewItem *)next->data)->col == col)
2315                 break;
2316             }
2317           if (!next || ((GtkIconViewItem *)next->data)->y > y)
2318             break;
2319
2320           item = next;
2321         }
2322     }
2323   else 
2324     {
2325       while (item)
2326         {
2327           for (next = item->prev; next; next = next->prev)
2328             {
2329               if (((GtkIconViewItem *)next->data)->col == col)
2330                 break;
2331             }
2332           if (!next || ((GtkIconViewItem *)next->data)->y < y)
2333             break;
2334
2335           item = next;
2336         }
2337     }
2338
2339   if (item)
2340     return item->data;
2341
2342   return NULL;
2343 }
2344
2345 static gboolean
2346 gtk_icon_view_select_all_between (GtkIconView     *icon_view,
2347                                   GtkIconViewItem *anchor,
2348                                   GtkIconViewItem *cursor)
2349 {
2350   GList *items;
2351   GtkIconViewItem *item;
2352   gint row1, row2, col1, col2;
2353   gboolean dirty = FALSE;
2354   
2355   if (anchor->row < cursor->row)
2356     {
2357       row1 = anchor->row;
2358       row2 = cursor->row;
2359     }
2360   else
2361     {
2362       row1 = cursor->row;
2363       row2 = anchor->row;
2364     }
2365
2366   if (anchor->col < cursor->col)
2367     {
2368       col1 = anchor->col;
2369       col2 = cursor->col;
2370     }
2371   else
2372     {
2373       col1 = cursor->col;
2374       col2 = anchor->col;
2375     }
2376
2377   for (items = icon_view->priv->items; items; items = items->next)
2378     {
2379       item = items->data;
2380
2381       if (row1 <= item->row && item->row <= row2 &&
2382           col1 <= item->col && item->col <= col2)
2383         {
2384           if (!item->selected)
2385             dirty = TRUE;
2386
2387           item->selected = TRUE;
2388           
2389           gtk_icon_view_queue_draw_item (icon_view, item);
2390         }
2391     }
2392
2393   return dirty;
2394 }
2395
2396 static void 
2397 gtk_icon_view_move_cursor_up_down (GtkIconView *icon_view,
2398                                    gint         count)
2399 {
2400   GtkIconViewItem *item;
2401   gboolean dirty = FALSE;
2402   
2403   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
2404     return;
2405   
2406   if (!icon_view->priv->cursor_item)
2407     {
2408       GList *list;
2409
2410       if (count > 0)
2411         list = icon_view->priv->items;
2412       else
2413         list = g_list_last (icon_view->priv->items);
2414
2415       item = list->data;
2416     }
2417   else
2418     item = find_item (icon_view, 
2419                       icon_view->priv->cursor_item,
2420                       count, 0);
2421
2422   if (!item)
2423     return;
2424
2425   if (icon_view->priv->ctrl_pressed ||
2426       !icon_view->priv->shift_pressed ||
2427       !icon_view->priv->anchor_item ||
2428       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
2429     icon_view->priv->anchor_item = item;
2430
2431   gtk_icon_view_set_cursor_item (icon_view, item);
2432
2433   if (!icon_view->priv->ctrl_pressed &&
2434       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
2435     {
2436       gtk_icon_view_unselect_all_internal (icon_view);
2437       dirty = gtk_icon_view_select_all_between (icon_view, 
2438                                                 icon_view->priv->anchor_item,
2439                                                 item);
2440     }
2441
2442   gtk_icon_view_scroll_to_item (icon_view, item);
2443
2444   if (dirty)
2445     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2446 }
2447
2448 static void 
2449 gtk_icon_view_move_cursor_page_up_down (GtkIconView *icon_view,
2450                                         gint         count)
2451 {
2452   GtkIconViewItem *item;
2453   gboolean dirty = FALSE;
2454   
2455   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
2456     return;
2457   
2458   if (!icon_view->priv->cursor_item)
2459     {
2460       GList *list;
2461
2462       if (count > 0)
2463         list = icon_view->priv->items;
2464       else
2465         list = g_list_last (icon_view->priv->items);
2466
2467       item = list->data;
2468     }
2469   else
2470     item = find_item_page_up_down (icon_view, 
2471                                    icon_view->priv->cursor_item,
2472                                    count);
2473
2474   if (!item)
2475     return;
2476
2477   if (icon_view->priv->ctrl_pressed ||
2478       !icon_view->priv->shift_pressed ||
2479       !icon_view->priv->anchor_item ||
2480       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
2481     icon_view->priv->anchor_item = item;
2482
2483   gtk_icon_view_set_cursor_item (icon_view, item);
2484
2485   if (!icon_view->priv->ctrl_pressed &&
2486       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
2487     {
2488       gtk_icon_view_unselect_all_internal (icon_view);
2489       dirty = gtk_icon_view_select_all_between (icon_view, 
2490                                                 icon_view->priv->anchor_item,
2491                                                 item);
2492     }
2493
2494   gtk_icon_view_scroll_to_item (icon_view, item);
2495
2496   if (dirty)
2497     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);  
2498 }
2499
2500 static void 
2501 gtk_icon_view_move_cursor_left_right (GtkIconView *icon_view,
2502                                       gint         count)
2503 {
2504   GtkIconViewItem *item;
2505   gboolean dirty = FALSE;
2506   
2507   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
2508     return;
2509   
2510   if (!icon_view->priv->cursor_item)
2511     {
2512       GList *list;
2513
2514       if (count > 0)
2515         list = icon_view->priv->items;
2516       else
2517         list = g_list_last (icon_view->priv->items);
2518
2519       item = list->data;
2520     }
2521   else
2522     item = find_item (icon_view, 
2523                       icon_view->priv->cursor_item,
2524                       0, count);
2525
2526   if (!item)
2527     return;
2528
2529   if (icon_view->priv->ctrl_pressed ||
2530       !icon_view->priv->shift_pressed ||
2531       !icon_view->priv->anchor_item ||
2532       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
2533     icon_view->priv->anchor_item = item;
2534
2535   gtk_icon_view_set_cursor_item (icon_view, item);
2536
2537   if (!icon_view->priv->ctrl_pressed &&
2538       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
2539     {
2540       gtk_icon_view_unselect_all_internal (icon_view);
2541       dirty = gtk_icon_view_select_all_between (icon_view, 
2542                                                 icon_view->priv->anchor_item,
2543                                                 item);
2544     }
2545
2546   gtk_icon_view_scroll_to_item (icon_view, item);
2547
2548   if (dirty)
2549     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2550 }
2551
2552 static void 
2553 gtk_icon_view_move_cursor_start_end (GtkIconView *icon_view,
2554                                      gint         count)
2555 {
2556   GtkIconViewItem *item;
2557   GList *list;
2558   gboolean dirty = FALSE;
2559   
2560   if (!GTK_WIDGET_HAS_FOCUS (icon_view))
2561     return;
2562   
2563   if (count < 0)
2564     list = icon_view->priv->items;
2565   else
2566     list = g_list_last (icon_view->priv->items);
2567   
2568   item = list->data;
2569
2570   if (!item)
2571     return;
2572
2573   if (icon_view->priv->ctrl_pressed ||
2574       !icon_view->priv->shift_pressed ||
2575       !icon_view->priv->anchor_item ||
2576       icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
2577     icon_view->priv->anchor_item = item;
2578
2579   gtk_icon_view_set_cursor_item (icon_view, item);
2580
2581   if (!icon_view->priv->ctrl_pressed &&
2582       icon_view->priv->selection_mode != GTK_SELECTION_NONE)
2583     {
2584       gtk_icon_view_unselect_all (icon_view);
2585       dirty = gtk_icon_view_select_all_between (icon_view, 
2586                                                 icon_view->priv->anchor_item,
2587                                                 item);
2588     }
2589
2590   gtk_icon_view_scroll_to_item (icon_view, item);
2591
2592   if (dirty)
2593     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
2594 }
2595
2596 static void     
2597 gtk_icon_view_scroll_to_item (GtkIconView     *icon_view, 
2598                               GtkIconViewItem *item)
2599 {
2600   gint y, height;
2601   gdouble value;
2602
2603   gdk_window_get_geometry (icon_view->priv->bin_window, NULL, &y, NULL, &height, NULL);
2604
2605   if (y + item->y < 0)
2606     {
2607       value = icon_view->priv->vadjustment->value + y + item->y;
2608       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
2609     }
2610   else if (y + item->y + item->height > GTK_WIDGET (icon_view)->allocation.height)
2611     {
2612       value = icon_view->priv->vadjustment->value + y + item->y + item->height 
2613         - GTK_WIDGET (icon_view)->allocation.height;
2614       gtk_adjustment_set_value (icon_view->priv->vadjustment, value);
2615     }
2616 }
2617
2618 /* Public API */
2619
2620
2621 /**
2622  * gtk_icon_view_new:
2623  * 
2624  * Creates a new #GtkIconView widget
2625  * 
2626  * Return value: A newly created #GtkIconView widget
2627  **/
2628 GtkWidget *
2629 gtk_icon_view_new (void)
2630 {
2631   return g_object_new (GTK_TYPE_ICON_VIEW, NULL);
2632 }
2633
2634 /**
2635  * gtk_icon_view_new_with_model:
2636  * @model: The model.
2637  * 
2638  * Creates a new #GtkIconView widget with the model initialized @model.
2639  * 
2640  * Return value: A newly created #GtkIconView widget.
2641  *
2642  * Since: 2.6 
2643  **/
2644 GtkWidget *
2645 gtk_icon_view_new_with_model (GtkTreeModel *model)
2646 {
2647   return g_object_new (GTK_TYPE_ICON_VIEW, "model", model, NULL);
2648 }
2649
2650
2651 /**
2652  * gtk_icon_view_get_path_at_pos:
2653  * @icon_view: A #GtkIconView.
2654  * @x: The x position to be identified.
2655  * @y: The y position to be identified
2656  * 
2657  * Finds the path at the point (@x, @y), relative to widget coordinates.
2658  * 
2659  * Return value: The #GtkTreePath corresponding to the icon or %NULL
2660  * if no icon exists at that coordinate.
2661  *
2662  * Since: 2.6 
2663  **/
2664 GtkTreePath *
2665 gtk_icon_view_get_path_at_pos (GtkIconView *icon_view,
2666                                gint         x,
2667                                gint         y)
2668 {
2669   GtkIconViewItem *item;
2670   GtkTreePath *path;
2671   
2672   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
2673
2674   item = gtk_icon_view_get_item_at_pos (icon_view, x, y);
2675
2676   if (!item)
2677     return NULL;
2678
2679   path = gtk_tree_path_new_from_indices (item->index, -1);
2680
2681   return path;
2682 }
2683
2684 /**
2685  * gtk_icon_view_selected_foreach:
2686  * @icon_view: A #GtkIconView.
2687  * @func: The funcion to call for each selected icon.
2688  * @data: User data to pass to the function.
2689  * 
2690  * Calls a function for each selected icon. Note that the tree or
2691  * selection cannot be modified from within this function.
2692  *
2693  * Since: 2.6 
2694  **/
2695 void
2696 gtk_icon_view_selected_foreach (GtkIconView           *icon_view,
2697                                 GtkIconViewForeachFunc func,
2698                                 gpointer               data)
2699 {
2700   GList *list;
2701   
2702   for (list = icon_view->priv->items; list; list = list->next)
2703     {
2704       GtkIconViewItem *item = list->data;
2705       GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
2706
2707       if (item->selected)
2708         (* func) (icon_view, path, data);
2709
2710       gtk_tree_path_free (path);
2711     }
2712 }
2713
2714 /**
2715  * gtk_icon_view_set_selection_mode:
2716  * @icon_view: A #GtkIconView.
2717  * @mode: The selection mode
2718  * 
2719  * Sets the selection mode of the @icon_view.
2720  *
2721  * Since: 2.6 
2722  **/
2723 void
2724 gtk_icon_view_set_selection_mode (GtkIconView      *icon_view,
2725                                   GtkSelectionMode  mode)
2726 {
2727   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
2728
2729   if (mode == icon_view->priv->selection_mode)
2730     return;
2731   
2732   if (mode == GTK_SELECTION_NONE ||
2733       icon_view->priv->selection_mode == GTK_SELECTION_MULTIPLE)
2734     gtk_icon_view_unselect_all (icon_view);
2735   
2736   icon_view->priv->selection_mode = mode;
2737
2738   g_object_notify (G_OBJECT (icon_view), "selection_mode");
2739 }
2740
2741 /**
2742  * gtk_icon_view_get_selection_mode:
2743  * @icon_view: A #GtkIconView.
2744  * 
2745  * Sets the selection mode of the @icon_view.
2746  *
2747  * Return value: the current selection mode
2748  *
2749  * Since: 2.6 
2750  **/
2751 GtkSelectionMode
2752 gtk_icon_view_get_selection_mode (GtkIconView *icon_view)
2753 {
2754   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), GTK_SELECTION_SINGLE);
2755
2756   return icon_view->priv->selection_mode;
2757 }
2758
2759 /**
2760  * gtk_icon_view_set_model:
2761  * @icon_view: A #GtkIconView.
2762  * @model: The model.
2763  *
2764  * Sets the model for a #GtkIconView.  If the @icon_view already has a model
2765  * set, it will remove it before setting the new model.  If @model is %NULL, then
2766  * it will unset the old model.
2767  *
2768  * Since: 2.6 
2769  **/
2770 void
2771 gtk_icon_view_set_model (GtkIconView *icon_view,
2772                          GtkTreeModel *model)
2773 {
2774   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
2775
2776   if (model != NULL)
2777     g_return_if_fail (GTK_IS_TREE_MODEL (model));
2778   
2779   if (icon_view->priv->model == model)
2780     return;
2781
2782   if (model)
2783     {
2784       GType pixbuf_column_type, text_column_type;
2785       
2786       g_return_if_fail (gtk_tree_model_get_flags (model) & GTK_TREE_MODEL_LIST_ONLY);
2787
2788       if (icon_view->priv->pixbuf_column != -1)
2789         {
2790           pixbuf_column_type = gtk_tree_model_get_column_type (icon_view->priv->model,
2791                                                                icon_view->priv->pixbuf_column);   
2792
2793           g_return_if_fail (pixbuf_column_type == GDK_TYPE_PIXBUF);
2794         }
2795
2796       if (icon_view->priv->text_column != -1)
2797         {
2798           text_column_type = gtk_tree_model_get_column_type (icon_view->priv->model,
2799                                                              icon_view->priv->pixbuf_column);     
2800
2801           g_return_if_fail (text_column_type == G_TYPE_STRING);
2802         }
2803       
2804     }
2805   
2806   if (icon_view->priv->model)
2807     {
2808       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
2809                                             gtk_icon_view_row_changed,
2810                                             icon_view);
2811       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
2812                                             gtk_icon_view_row_inserted,
2813                                             icon_view);
2814       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
2815                                             gtk_icon_view_row_deleted,
2816                                             icon_view);
2817       g_signal_handlers_disconnect_by_func (icon_view->priv->model,
2818                                             gtk_icon_view_rows_reordered,
2819                                             icon_view);
2820
2821       g_object_unref (icon_view->priv->model);
2822       
2823       g_list_foreach (icon_view->priv->items, (GFunc)gtk_icon_view_item_free, NULL);
2824       g_list_free (icon_view->priv->items);
2825       icon_view->priv->items = NULL;
2826     }
2827
2828   icon_view->priv->model = model;
2829
2830   if (icon_view->priv->model)
2831     {
2832       g_object_ref (icon_view->priv->model);
2833       g_signal_connect (icon_view->priv->model,
2834                         "row_changed",
2835                         G_CALLBACK (gtk_icon_view_row_changed),
2836                         icon_view);
2837       g_signal_connect (icon_view->priv->model,
2838                         "row_inserted",
2839                         G_CALLBACK (gtk_icon_view_row_inserted),
2840                         icon_view);
2841       g_signal_connect (icon_view->priv->model,
2842                         "row_deleted",
2843                         G_CALLBACK (gtk_icon_view_row_deleted),
2844                         icon_view);
2845       g_signal_connect (icon_view->priv->model,
2846                         "rows_reordered",
2847                         G_CALLBACK (gtk_icon_view_rows_reordered),
2848                         icon_view);
2849
2850       gtk_icon_view_build_items (icon_view);
2851     }
2852
2853   g_object_notify (G_OBJECT (icon_view), "model");  
2854 }
2855
2856 /**
2857  * gtk_icon_view_get_model:
2858  * @icon_view: a #GtkIconView
2859  *
2860  * Returns the model the #GtkIconView is based on.  Returns %NULL if the
2861  * model is unset.
2862  *
2863  * Return value: A #GtkTreeModel, or %NULL if none is currently being used.
2864  *
2865  * Since: 2.6 
2866  **/
2867 GtkTreeModel *
2868 gtk_icon_view_get_model (GtkIconView *icon_view)
2869 {
2870   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
2871
2872   return icon_view->priv->model;
2873 }
2874
2875 /**
2876  * gtk_icon_view_set_text_column:
2877  * @icon_view: A #GtkIconView.
2878  * @column: A column in the currently used model.
2879  * 
2880  * Sets the column with text for @icon_view to be @column. The text
2881  * column must be of type #G_TYPE_STRING.
2882  *
2883  * Since: 2.6 
2884  **/
2885 void
2886 gtk_icon_view_set_text_column (GtkIconView *icon_view,
2887                                int          column)
2888 {
2889   if (column == icon_view->priv->text_column)
2890     return;
2891   
2892   if (column == -1)
2893     icon_view->priv->text_column = -1;
2894   else
2895     {
2896       if (icon_view->priv->model != NULL)
2897         {
2898           GType column_type;
2899           
2900           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
2901
2902           g_return_if_fail (column_type == G_TYPE_STRING);
2903         }
2904       
2905       icon_view->priv->text_column = column;
2906     }
2907
2908   gtk_icon_view_invalidate_sizes (icon_view);
2909   gtk_icon_view_queue_layout (icon_view);
2910   
2911   g_object_notify (G_OBJECT (icon_view), "text_column");
2912 }
2913
2914 /**
2915  * gtk_icon_view_get_text_column:
2916  * @icon_view: A #GtkIconView.
2917  *
2918  * Returns the column with text for @icon_view.
2919  *
2920  * Returns: the text column, or -1 if it's unset.
2921  *
2922  * Since: 2.6
2923  */
2924 gint
2925 gtk_icon_view_get_text_column (GtkIconView  *icon_view)
2926 {
2927   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
2928
2929   return icon_view->priv->text_column;
2930 }
2931
2932 /**
2933  * gtk_icon_view_set_markup_column:
2934  * @icon_view: A #GtkIconView.
2935  * @column: A column in the currently used model.
2936  * 
2937  * Sets the column with markup information for @icon_view to be
2938  * @column. The markup column must be of type #G_TYPE_STRING.
2939  * If the markup column is set to something, it overrides
2940  * the text column set by #gtk_icon_view_set_text_column.
2941  *
2942  * Since: 2.6
2943  **/
2944 void
2945 gtk_icon_view_set_markup_column (GtkIconView *icon_view,
2946                                  int          column)
2947 {
2948   if (column == icon_view->priv->markup_column)
2949     return;
2950   
2951   if (column == -1)
2952     icon_view->priv->markup_column = -1;
2953   else
2954     {
2955       if (icon_view->priv->model != NULL)
2956         {
2957           GType column_type;
2958           
2959           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
2960
2961           g_return_if_fail (column_type == G_TYPE_STRING);
2962         }
2963       
2964       icon_view->priv->markup_column = column;
2965     }
2966
2967   gtk_icon_view_invalidate_sizes (icon_view);
2968   gtk_icon_view_queue_layout (icon_view);
2969   
2970   g_object_notify (G_OBJECT (icon_view), "markup_column");
2971 }
2972
2973 /**
2974  * gtk_icon_view_get_markup_column:
2975  * @icon_view: A #GtkIconView.
2976  *
2977  * Returns the column with markup text for @icon_view.
2978  *
2979  * Returns: the markup column, or -1 if it's unset.
2980  *
2981  * Since: 2.6
2982  */
2983 gint
2984 gtk_icon_view_get_markup_column (GtkIconView  *icon_view)
2985 {
2986   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
2987
2988   return icon_view->priv->markup_column;
2989 }
2990
2991 /**
2992  * gtk_icon_view_set_pixbuf_column:
2993  * @icon_view: A #GtkIconView.
2994  * @column: A column in the currently used model.
2995  * 
2996  * Sets the column with pixbufs for @icon_view to be @column. The pixbuf
2997  * column must be of type #GDK_TYPE_PIXBUF
2998  *
2999  * Since: 2.6 
3000  **/
3001 void
3002 gtk_icon_view_set_pixbuf_column (GtkIconView *icon_view,
3003                                  int          column)
3004 {
3005   if (column == icon_view->priv->pixbuf_column)
3006     return;
3007   
3008   if (column == -1)
3009     icon_view->priv->pixbuf_column = -1;
3010   else
3011     {
3012       if (icon_view->priv->model != NULL)
3013         {
3014           GType column_type;
3015           
3016           column_type = gtk_tree_model_get_column_type (icon_view->priv->model, column);
3017
3018           g_return_if_fail (column_type == GDK_TYPE_PIXBUF);
3019         }
3020       
3021       icon_view->priv->pixbuf_column = column;
3022     }
3023
3024   gtk_icon_view_invalidate_sizes (icon_view);
3025   gtk_icon_view_queue_layout (icon_view);
3026   
3027   g_object_notify (G_OBJECT (icon_view), "pixbuf_column");
3028   
3029 }
3030
3031 /**
3032  * gtk_icon_view_get_pixbuf_column:
3033  * @icon_view: A #GtkIconView.
3034  *
3035  * Returns the column with pixbufs for @icon_view.
3036  *
3037  * Returns: the pixbuf column, or -1 if it's unset.
3038  *
3039  * Since: 2.6
3040  */
3041 gint
3042 gtk_icon_view_get_pixbuf_column (GtkIconView  *icon_view)
3043 {
3044   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), -1);
3045
3046   return icon_view->priv->pixbuf_column;
3047 }
3048
3049 /**
3050  * gtk_icon_view_select_path:
3051  * @icon_view: A #GtkIconView.
3052  * @path: The #GtkTreePath to be selected.
3053  * 
3054  * Selects the row at @path.
3055  **/
3056 void
3057 gtk_icon_view_select_path (GtkIconView *icon_view,
3058                            GtkTreePath *path)
3059 {
3060   GtkIconViewItem *item;
3061   
3062   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3063   g_return_if_fail (icon_view->priv->model != NULL);
3064   g_return_if_fail (path != NULL);
3065
3066   item = g_list_nth (icon_view->priv->items,
3067                      gtk_tree_path_get_indices(path)[0])->data;
3068
3069   if (!item)
3070     return;
3071   
3072   gtk_icon_view_select_item (icon_view, item);
3073 }
3074
3075 /**
3076  * gtk_icon_view_unselect_path:
3077  * @icon_view: A #GtkIconView.
3078  * @path: The #GtkTreePath to be unselected.
3079  * 
3080  * Unselects the row at @path.
3081  **/
3082 void
3083 gtk_icon_view_unselect_path (GtkIconView *icon_view,
3084                              GtkTreePath *path)
3085 {
3086   GtkIconViewItem *item;
3087   
3088   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3089   g_return_if_fail (icon_view->priv->model != NULL);
3090   g_return_if_fail (path != NULL);
3091
3092   item = g_list_nth (icon_view->priv->items,
3093                      gtk_tree_path_get_indices(path)[0])->data;
3094
3095   if (!item)
3096     return;
3097   
3098   gtk_icon_view_unselect_item (icon_view, item);
3099 }
3100
3101 /**
3102  * gtk_icon_view_get_selected_items:
3103  * @icon_view: A #GtkIconView.
3104  *
3105  * Creates a list of path of all selected items. Additionally, if you are
3106  * planning on modifying the model after calling this function, you may
3107  * want to convert the returned list into a list of #GtkTreeRowReference<!-- -->s.
3108  * To do this, you can use gtk_tree_row_reference_new().
3109  *
3110  * To free the return value, use:
3111  * <informalexample><programlisting>
3112  * g_list_foreach (list, gtk_tree_path_free, NULL);
3113  * g_list_free (list);
3114  * </programlisting></informalexample>
3115  *
3116  * Return value: A #GList containing a #GtkTreePath for each selected row.
3117  *
3118  * Since: 2.6
3119  **/
3120 GList *
3121 gtk_icon_view_get_selected_items (GtkIconView *icon_view)
3122 {
3123   GList *list;
3124   GList *selected = NULL;
3125   
3126   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), NULL);
3127   
3128   for (list = icon_view->priv->items; list != NULL; list = list->next)
3129     {
3130       GtkIconViewItem *item = list->data;
3131
3132       if (item->selected)
3133         {
3134           GtkTreePath *path = gtk_tree_path_new_from_indices (item->index, -1);
3135
3136           selected = g_list_prepend (selected, path);
3137         }
3138     }
3139
3140   return selected;
3141 }
3142
3143 /**
3144  * gtk_icon_view_select_all:
3145  * @icon_view: A #GtkIconView.
3146  * 
3147  * Selects all the icons. @icon_view must has its selection mode set
3148  * to #GTK_SELECTION_MULTIPLE.
3149  **/
3150 void
3151 gtk_icon_view_select_all (GtkIconView *icon_view)
3152 {
3153   GList *items;
3154   gboolean dirty = FALSE;
3155   
3156   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3157
3158   if (icon_view->priv->selection_mode != GTK_SELECTION_MULTIPLE)
3159     return;
3160
3161   for (items = icon_view->priv->items; items; items = items->next)
3162     {
3163       GtkIconViewItem *item = items->data;
3164       
3165       if (!item->selected)
3166         {
3167           dirty = TRUE;
3168           item->selected = TRUE;
3169           gtk_icon_view_queue_draw_item (icon_view, item);
3170         }
3171     }
3172
3173   if (dirty)
3174     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3175 }
3176
3177 /**
3178  * gtk_icon_view_unselect_all:
3179  * @icon_view: A #GtkIconView.
3180  * 
3181  * Unselects all the icons.
3182  **/
3183 void
3184 gtk_icon_view_unselect_all (GtkIconView *icon_view)
3185 {
3186   gboolean dirty = FALSE;
3187   
3188   g_return_if_fail (GTK_IS_ICON_VIEW (icon_view));
3189
3190   dirty = gtk_icon_view_unselect_all_internal (icon_view);
3191
3192   if (dirty)
3193     g_signal_emit (icon_view, icon_view_signals[SELECTION_CHANGED], 0);
3194 }
3195
3196 /**
3197  * gtk_icon_view_path_is_selected:
3198  * @icon_view: A #GtkIconView.
3199  * @path: A #GtkTreePath to check selection on.
3200  * 
3201  * Returns %TRUE if the icon pointed to by @path is currently
3202  * selected. If @icon does not point to a valid location, %FALSE is returned.
3203  * 
3204  * Return value: %TRUE if @path is selected.
3205  **/
3206 gboolean
3207 gtk_icon_view_path_is_selected (GtkIconView *icon_view,
3208                                 GtkTreePath *path)
3209 {
3210   GtkIconViewItem *item;
3211   
3212   g_return_val_if_fail (GTK_IS_ICON_VIEW (icon_view), FALSE);
3213   g_return_val_if_fail (icon_view->priv->model != NULL, FALSE);
3214   g_return_val_if_fail (path != NULL, FALSE);
3215   
3216   item = g_list_nth (icon_view->priv->items,
3217                      gtk_tree_path_get_indices(path)[0])->data;
3218
3219   if (!item)
3220     return FALSE;
3221   
3222   return item->selected;
3223 }
3224
3225 /**
3226  * gtk_icon_view_item_activated:
3227  * @icon_view: A #GtkIconView
3228  * @path: The #GtkTreePath to be activated
3229  * 
3230  * Activates the item determined by @path.
3231  **/
3232 void
3233 gtk_icon_view_item_activated (GtkIconView      *icon_view,
3234                               GtkTreePath      *path)
3235 {
3236   g_signal_emit (G_OBJECT (icon_view), icon_view_signals[ITEM_ACTIVATED], 0, path);
3237 }