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