gtkiconview.c 66.6 KB
Newer Older
Anders Carlsson's avatar
Anders Carlsson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/* eggiconlist.h
 * Copyright (C) 2002  Anders Carlsson <andersca@gnu.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "eggiconlist.h"

#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtkbindings.h>
#include <gtk/gtkdnd.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>

#include "eggintl.h"
#include "eggmarshalers.h"


#define MINIMUM_ICON_ITEM_WIDTH 100
#define ICON_TEXT_PADDING 3

#define ICON_LIST_ITEM_DATA "egg-icon-list-item-data"

struct _EggIconListItem
{
Anders Carlsson's avatar
Anders Carlsson committed
40
  gint ref_count;
Anders Carlsson's avatar
Anders Carlsson committed
41 42 43 44 45 46
  
  EggIconList *icon_list;
  char *label;
  GdkPixbuf *icon;

  GList *list;
Anders Carlsson's avatar
Anders Carlsson committed
47 48 49

  gpointer user_data;
  GDestroyNotify destroy_notify;
Anders Carlsson's avatar
Anders Carlsson committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  
  /* Bounding boxes */
  gint x, y;
  gint width, height;

  gint pixbuf_x, pixbuf_y;
  gint pixbuf_height, pixbuf_width;

  gint layout_x, layout_y;
  gint layout_width, layout_height;

  guint selected : 1;
  guint selected_before_rubberbanding : 1;
};

struct _EggIconListPrivate
{
  gint width, height;

  GtkSelectionMode selection_mode;

  GdkWindow *bin_window;

  GList *items;
  GList *last_item;
  gint item_count;
  
  GtkAdjustment *hadjustment;
  GtkAdjustment *vadjustment;

  guint layout_idle_id;
  
  gboolean rubberbanding;
  gint rubberband_x1, rubberband_y1;
  gint rubberband_x2, rubberband_y2;
  
  EggIconListItem *cursor_item;
  
  char *typeahead_string;

  /* Sorting */
  gboolean sorted;
  GtkSortType sort_order;
  
  EggIconListItemCompareFunc sort_func;
  gpointer sort_data;
  GDestroyNotify sort_destroy_notify;

  EggIconListItem *last_single_clicked;
  
  /* Drag-and-drop. */
  gint pressed_button;
  gint press_start_x;
  gint press_start_y;

  /* Layout used to draw icon text */
  PangoLayout *layout;
};

/* Signals */
enum
{
  ITEM_ACTIVATED,
  ITEM_ADDED,
  ITEM_REMOVED,
  SELECTION_CHANGED,
  SELECT_ALL,
  UNSELECT_ALL,
  SELECT_CURSOR_ITEM,
  TOGGLE_CURSOR_ITEM,
  MOVE_CURSOR,
  LAST_SIGNAL
};

/* Properties */
enum
{
  PROP_0,
  PROP_SELECTION_MODE,
  PROP_SORTED,
  PROP_SORT_ORDER,
};

/* Icon List Item properties */
enum
{
  PROP_ITEM_0,
  PROP_LABEL,
};

static void egg_icon_list_class_init      (EggIconListClass *klass);
static void egg_icon_list_init            (EggIconList      *icon_list);

/* GObject signals */
static void egg_icon_list_finalize     (GObject      *object);
static void egg_icon_list_set_property (GObject      *object,
					guint         prop_id,
					const GValue *value,
					GParamSpec   *pspec);
static void egg_icon_list_get_property (GObject      *object,
					guint         prop_id,
					GValue       *value,
					GParamSpec   *pspec);


/* GtkWidget signals */
static void     egg_icon_list_realize        (GtkWidget      *widget);
static void     egg_icon_list_unrealize      (GtkWidget      *widget);
static void     egg_icon_list_map            (GtkWidget      *widget);
static void     egg_icon_list_size_request   (GtkWidget      *widget,
					      GtkRequisition *requisition);
static void     egg_icon_list_size_allocate  (GtkWidget      *widget,
					      GtkAllocation  *allocation);
static gboolean egg_icon_list_expose         (GtkWidget      *widget,
					      GdkEventExpose *expose);
static gboolean egg_icon_list_motion         (GtkWidget      *widget,
					      GdkEventMotion *event);
static gboolean egg_icon_list_button_press   (GtkWidget      *widget,
					      GdkEventButton *event);
static gboolean egg_icon_list_button_release (GtkWidget      *widget,
					      GdkEventButton *event);
static gboolean egg_icon_list_key_press      (GtkWidget      *widget,
					      GdkEventKey    *event);


/* EggIconList signals */
static void     egg_icon_list_set_adjustments             (EggIconList             *icon_list,
							   GtkAdjustment           *hadj,
							   GtkAdjustment           *vadj);
static void     egg_icon_list_real_select_all             (EggIconList             *icon_list);
static void     egg_icon_list_real_unselect_all           (EggIconList             *icon_list);
static void     egg_icon_list_real_select_cursor_item     (EggIconList             *icon_list);
static void     egg_icon_list_real_toggle_cursor_item     (EggIconList             *icon_list);

/* Internal functions */
static void     egg_icon_list_adjustment_changed          (GtkAdjustment           *adjustment,
							   EggIconList             *icon_list);
static void     egg_icon_list_layout                      (EggIconList             *icon_list);
static void     egg_icon_list_paint_item                  (EggIconList             *icon_list,
							   EggIconListItem         *item,
							   GdkRectangle            *area);
static void     egg_icon_list_paint_rubberband            (EggIconList             *icon_list,
							   GdkRectangle            *area);
static void     egg_icon_list_queue_draw_item             (EggIconList             *icon_list,
							   EggIconListItem         *item);
static void     egg_icon_list_queue_layout                (EggIconList             *icon_list);
static void     egg_icon_list_set_cursor_item             (EggIconList             *icon_list,
							   EggIconListItem         *item);
static void     egg_icon_list_append_typeahead_string     (EggIconList             *icon_list,
							   const gchar             *string);
static void     egg_icon_list_select_first_matching_item  (EggIconList             *icon_list,
							   const char              *pattern);
static void     egg_icon_list_start_rubberbanding         (EggIconList             *icon_list,
							   gint                     x,
							   gint                     y);
static void     egg_icon_list_stop_rubberbanding          (EggIconList             *icon_list);
static void     egg_icon_list_sort                        (EggIconList             *icon_list);
static gint     egg_icon_list_sort_func                   (EggIconListItem         *a,
							   EggIconListItem         *b,
							   EggIconList             *icon_list);
static void     egg_icon_list_insert_item_sorted          (EggIconList             *icon_list,
							   EggIconListItem         *item);
static void     egg_icon_list_validate                    (EggIconList             *icon_list);
static void     egg_icon_list_update_rubberband_selection (EggIconList             *icon_list);
static gboolean egg_icon_list_item_hit_test               (EggIconListItem         *item,
							   gint                     x,
							   gint                     y,
							   gint                     width,
							   gint                     height);
static gboolean egg_icon_list_maybe_begin_dragging_items  (EggIconList             *icon_list,
							   GdkEventMotion          *event);
static gboolean egg_icon_list_unselect_all_internal       (EggIconList             *icon_list,
							   gboolean                 emit);
static void     egg_icon_list_calculate_item_size         (EggIconList *icon_list, EggIconListItem         *item);
static void     rubberbanding                             (gpointer                 data);


static void     egg_icon_list_item_invalidate_size        (EggIconListItem         *item);

static GtkContainerClass *parent_class = NULL;
static guint icon_list_signals[LAST_SIGNAL] = { 0 };

GType
egg_icon_list_item_get_type (void)
{
Anders Carlsson's avatar
Anders Carlsson committed
235
  static GType boxed_type = 0;
Anders Carlsson's avatar
Anders Carlsson committed
236

Anders Carlsson's avatar
Anders Carlsson committed
237 238 239 240
  if (!boxed_type)
    boxed_type = g_boxed_type_register_static ("EggIconListItem",
					       (GBoxedCopyFunc) egg_icon_list_item_ref,
					       (GBoxedFreeFunc) egg_icon_list_item_unref);
Anders Carlsson's avatar
Anders Carlsson committed
241

Anders Carlsson's avatar
Anders Carlsson committed
242
  return boxed_type;
Anders Carlsson's avatar
Anders Carlsson committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
}

GType
egg_icon_list_get_type (void)
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
	{	  
	  sizeof (EggIconListClass),
	  NULL,		/* base_init */
	  NULL,		/* base_finalize */
	  (GClassInitFunc) egg_icon_list_class_init,
	  NULL,		/* class_finalize */
	  NULL,		/* class_data */
	  sizeof (EggIconList),
	  0,              /* n_preallocs */
	  (GInstanceInitFunc) egg_icon_list_init
	};

      object_type = g_type_register_static (GTK_TYPE_CONTAINER, "EggIconList", &object_info, 0);
    }

  return object_type;
}

static void
egg_icon_list_class_init (EggIconListClass *klass)
{
  GObjectClass *gobject_class;
  GtkWidgetClass *widget_class;
  GtkBindingSet *binding_set;
  
  parent_class = g_type_class_peek_parent (klass);
  binding_set = gtk_binding_set_by_class (klass);
  
  gobject_class = (GObjectClass *) klass;
  widget_class = (GtkWidgetClass *) klass;

  gobject_class->finalize = egg_icon_list_finalize;
  gobject_class->set_property = egg_icon_list_set_property;
  gobject_class->get_property = egg_icon_list_get_property;

  widget_class->realize = egg_icon_list_realize;
  widget_class->unrealize = egg_icon_list_unrealize;
  widget_class->map = egg_icon_list_map;
  widget_class->size_request = egg_icon_list_size_request;
  widget_class->size_allocate = egg_icon_list_size_allocate;
  widget_class->expose_event = egg_icon_list_expose;
  widget_class->motion_notify_event = egg_icon_list_motion;
  widget_class->button_press_event = egg_icon_list_button_press;
  widget_class->button_release_event = egg_icon_list_button_release;
  widget_class->key_press_event = egg_icon_list_key_press;
  
  klass->set_scroll_adjustments = egg_icon_list_set_adjustments;
  klass->select_all = egg_icon_list_real_select_all;
  klass->unselect_all = egg_icon_list_real_unselect_all;
  klass->select_cursor_item = egg_icon_list_real_select_cursor_item;
  klass->toggle_cursor_item = egg_icon_list_real_toggle_cursor_item;
  
  /* Properties */
  g_object_class_install_property (gobject_class,
				   PROP_SELECTION_MODE,
				   g_param_spec_enum ("selection_mode",
						      _("Selection mode"),
						      _("The selection mode"),
						      GTK_TYPE_SELECTION_MODE,
						      GTK_SELECTION_SINGLE,
						      G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
				   PROP_SORTED,
				   g_param_spec_boolean ("sorted",
							 _("Sorted"),
							 _("Icon list is sorted"),
							 FALSE,
							 G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_SORT_ORDER,
                                   g_param_spec_enum ("sort_order",
                                                      _("Sort order"),
                                                      _("Sort direction the icon list should use"),
                                                      GTK_TYPE_SORT_TYPE,
                                                      GTK_SORT_ASCENDING,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));

  /* Style properties */
#define _ICON_LIST_TOP_MARGIN 6
#define _ICON_LIST_BOTTOM_MARGIN 6
#define _ICON_LIST_LEFT_MARGIN 6
#define _ICON_LIST_RIGHT_MARGIN 6
#define _ICON_LIST_ICON_PADDING 6

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("icon_padding",
							     _("Icon padding"),
							     _("Number of pixels between icons"),
							     0,
							     G_MAXINT,
							     _ICON_LIST_ICON_PADDING,
							     G_PARAM_READABLE));
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("top_margin",
							     _("Top margin"),
							     _("Number of pixels in top margin"),
							     0,
							     G_MAXINT,
							     _ICON_LIST_TOP_MARGIN,
							     G_PARAM_READABLE));
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("bottom_margin",
							     _("Bottom margin"),
							     _("Number of pixels in bottom margin"),
							     0,
							     G_MAXINT,
							     _ICON_LIST_BOTTOM_MARGIN,
							     G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("left_margin",
							     _("Left margin"),
							     _("Number of pixels in left margin"),
							     0,
							     G_MAXINT,
							     _ICON_LIST_LEFT_MARGIN,
							     G_PARAM_READABLE));
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_int ("right_margin",
							     _("Right margin"),
							     _("Number of pixels in right margin"),
							     0,
							     G_MAXINT,
							     _ICON_LIST_RIGHT_MARGIN,
							     G_PARAM_READABLE));

380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_boxed ("selection_box_color",
                                                               _("Selection Box Color"),
                                                               _("Color of the selection box"),
                                                               GDK_TYPE_COLOR,
                                                               G_PARAM_READABLE));

  gtk_widget_class_install_style_property (widget_class,
                                           g_param_spec_uchar ("selection_box_alpha",
                                                               _("Selection Box Alpha"),
                                                               _("Opacity of the selection box"),
                                                               0, 0xff,
                                                               0x40,
                                                               G_PARAM_READABLE));

Anders Carlsson's avatar
Anders Carlsson committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
  /* Signals */
  widget_class->set_scroll_adjustments_signal =
    g_signal_new ("set_scroll_adjustments",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (EggIconListClass, set_scroll_adjustments),
		  NULL, NULL, 
		  _egg_marshal_VOID__OBJECT_OBJECT,
		  G_TYPE_NONE, 2,
		  GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);

  icon_list_signals[ITEM_ACTIVATED] =
    g_signal_new ("item_activated",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (EggIconListClass, item_activated),
		  NULL, NULL,
Anders Carlsson's avatar
Anders Carlsson committed
412
		  g_cclosure_marshal_VOID__BOXED,
Anders Carlsson's avatar
Anders Carlsson committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
		  G_TYPE_NONE, 1,
		  EGG_TYPE_ICON_LIST_ITEM);

  icon_list_signals[SELECTION_CHANGED] =
    g_signal_new ("selection_changed",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_FIRST,
		  G_STRUCT_OFFSET (EggIconListClass, selection_changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  
  icon_list_signals[ITEM_ADDED] =
    g_signal_new ("item_added",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (EggIconListClass, item_added),
		  NULL, NULL,
Anders Carlsson's avatar
Anders Carlsson committed
431
		  g_cclosure_marshal_VOID__BOXED,
Anders Carlsson's avatar
Anders Carlsson committed
432 433 434 435 436 437 438 439
		  G_TYPE_NONE, 1, EGG_TYPE_ICON_LIST_ITEM);

  icon_list_signals[ITEM_REMOVED] =
    g_signal_new ("item_removed",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (EggIconListClass, item_removed),
		  NULL, NULL,
Anders Carlsson's avatar
Anders Carlsson committed
440
		  g_cclosure_marshal_VOID__BOXED,
Anders Carlsson's avatar
Anders Carlsson committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
		  G_TYPE_NONE, 1, EGG_TYPE_ICON_LIST_ITEM);
  
  icon_list_signals[SELECT_ALL] =
    g_signal_new ("select_all",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (EggIconListClass, select_all),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
  
  icon_list_signals[UNSELECT_ALL] =
    g_signal_new ("unselect_all",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (EggIconListClass, unselect_all),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  icon_list_signals[SELECT_CURSOR_ITEM] =
    g_signal_new ("select_cursor_item",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (EggIconListClass, select_cursor_item),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  icon_list_signals[SELECT_CURSOR_ITEM] =
    g_signal_new ("toggle_cursor_item",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (EggIconListClass, toggle_cursor_item),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);

  /* Key bindings */
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK, "select_all", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "unselect_all", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_space, 0, "select_cursor_item", 0);
  gtk_binding_entry_add_signal (binding_set, GDK_space, GDK_CONTROL_MASK, "toggle_cursor_item", 0);
}


static void
egg_icon_list_init (EggIconList *icon_list)
{
  icon_list->priv = g_new0 (EggIconListPrivate, 1);
  GTK_WIDGET_SET_FLAGS (icon_list, GTK_CAN_FOCUS);

  icon_list->priv->width = 0;
  icon_list->priv->height = 0;
  icon_list->priv->selection_mode = GTK_SELECTION_SINGLE;
  icon_list->priv->sort_order = GTK_SORT_ASCENDING;
  icon_list->priv->pressed_button = -1;
  icon_list->priv->press_start_x = -1;
  icon_list->priv->press_start_y = -1;
  icon_list->priv->layout = gtk_widget_create_pango_layout (GTK_WIDGET (icon_list), NULL);
Anders Carlsson's avatar
Anders Carlsson committed
501
  pango_layout_set_wrap (icon_list->priv->layout, PANGO_WRAP_CHAR);
Anders Carlsson's avatar
Anders Carlsson committed
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  
  egg_icon_list_set_adjustments (icon_list, NULL, NULL);
}


/* GObject methods */
static void
egg_icon_list_finalize (GObject *object)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (object);

  if (icon_list->priv->layout_idle_id != 0)
    g_source_remove (icon_list->priv->layout_idle_id);

  g_free (icon_list->priv);
  
  (G_OBJECT_CLASS (parent_class)->finalize) (object);
}


static void
egg_icon_list_set_property (GObject      *object,
			    guint         prop_id,
			    const GValue *value,
			    GParamSpec   *pspec)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (object);

  switch (prop_id)
    {
    case PROP_SELECTION_MODE:
      egg_icon_list_set_selection_mode (icon_list, g_value_get_enum (value));
      break;
    case PROP_SORTED:
      egg_icon_list_set_sorted (icon_list, g_value_get_boolean (value));
      break;
    case PROP_SORT_ORDER:
      egg_icon_list_set_sort_order (icon_list, g_value_get_enum (value));
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
egg_icon_list_get_property (GObject      *object,
			    guint         prop_id,
			    GValue       *value,
			    GParamSpec   *pspec)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (object);

  switch (prop_id)
    {
    case PROP_SELECTION_MODE:
      g_value_set_enum (value, icon_list->priv->selection_mode);
      break;
    case PROP_SORTED:
      g_value_set_boolean (value, icon_list->priv->sorted);
      break;
    case PROP_SORT_ORDER:
      g_value_set_enum (value, icon_list->priv->sort_order);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

/* GtkWidget signals */
static void
egg_icon_list_realize (GtkWidget *widget)
{
  EggIconList *icon_list;
  GdkWindowAttr attributes;
  gint attributes_mask;
  
  icon_list = EGG_ICON_LIST (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

  /* Make the main, clipping window */
  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);

  /* Make the window for the icon list */
  attributes.x = 0;
  attributes.y = 0;
  attributes.width = MAX (icon_list->priv->width, widget->allocation.width);
  attributes.height = MAX (icon_list->priv->height, widget->allocation.height);
  attributes.event_mask = (GDK_EXPOSURE_MASK |
			   GDK_SCROLL_MASK |
			   GDK_POINTER_MOTION_MASK |
			   GDK_BUTTON_PRESS_MASK |
			   GDK_BUTTON_RELEASE_MASK |
			   GDK_KEY_PRESS_MASK |
			   GDK_KEY_RELEASE_MASK) |
    gtk_widget_get_events (widget);
  
  icon_list->priv->bin_window = gdk_window_new (widget->window,
					  &attributes, attributes_mask);
  gdk_window_set_user_data (icon_list->priv->bin_window, widget);

  widget->style = gtk_style_attach (widget->style, widget->window);
  gdk_window_set_background (icon_list->priv->bin_window, &widget->style->base[widget->state]);
  gdk_window_set_background (widget->window, &widget->style->base[widget->state]);

  
}

static void
egg_icon_list_unrealize (GtkWidget *widget)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (widget);

  gdk_window_set_user_data (icon_list->priv->bin_window, NULL);
  gdk_window_destroy (icon_list->priv->bin_window);
  icon_list->priv->bin_window = NULL;

  /* GtkWidget::unrealize destroys children and widget->window */
  if (GTK_WIDGET_CLASS (parent_class)->unrealize)
    (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

static void
egg_icon_list_map (GtkWidget *widget)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  gdk_window_show (icon_list->priv->bin_window);
  gdk_window_show (widget->window);
}

static void
egg_icon_list_size_request (GtkWidget      *widget,
			    GtkRequisition *requisition)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (widget);

  requisition->width = icon_list->priv->width;
  requisition->height = icon_list->priv->height;
}

static void
egg_icon_list_size_allocate (GtkWidget      *widget,
			     GtkAllocation  *allocation)
{
  EggIconList *icon_list;

  widget->allocation = *allocation;
  
  icon_list = EGG_ICON_LIST (widget);
  
  if (GTK_WIDGET_REALIZED (widget))
    {
      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
      gdk_window_resize (icon_list->priv->bin_window,
			 MAX (icon_list->priv->width, allocation->width),
			 MAX (icon_list->priv->height, allocation->height));
    }

  icon_list->priv->hadjustment->page_size = allocation->width;
  icon_list->priv->hadjustment->page_increment = allocation->width * 0.9;
  icon_list->priv->hadjustment->step_increment = allocation->width * 0.1;
  icon_list->priv->hadjustment->lower = 0;
  icon_list->priv->hadjustment->upper = MAX (allocation->width, icon_list->priv->width);
Anders Carlsson's avatar
Anders Carlsson committed
698
  gtk_adjustment_changed (icon_list->priv->hadjustment);
Anders Carlsson's avatar
Anders Carlsson committed
699 700 701 702 703 704

  icon_list->priv->vadjustment->page_size = allocation->height;
  icon_list->priv->vadjustment->page_increment = allocation->height * 0.9;
  icon_list->priv->vadjustment->step_increment = allocation->width * 0.1;
  icon_list->priv->vadjustment->lower = 0;
  icon_list->priv->vadjustment->upper = MAX (allocation->height, icon_list->priv->height);
Anders Carlsson's avatar
Anders Carlsson committed
705
  gtk_adjustment_changed (icon_list->priv->vadjustment);
Anders Carlsson's avatar
Anders Carlsson committed
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945

  egg_icon_list_layout (icon_list);
}

static gboolean
egg_icon_list_expose (GtkWidget *widget,
		      GdkEventExpose *expose)
{
  EggIconList *icon_list;
  GList *icons;

  icon_list = EGG_ICON_LIST (widget);

  if (expose->window != icon_list->priv->bin_window)
    return FALSE;

  for (icons = icon_list->priv->items; icons; icons = icons->next) {
    EggIconListItem *item = icons->data;
    GdkRectangle item_rectangle;

    item_rectangle.x = item->x;
    item_rectangle.y = item->y;
    item_rectangle.width = item->width;
    item_rectangle.height = item->height;

    if (gdk_region_rect_in (expose->region, &item_rectangle) == GDK_OVERLAP_RECTANGLE_OUT)
      continue;

    egg_icon_list_paint_item (icon_list, item, &expose->area);
  }

  if (icon_list->priv->rubberbanding)
    {
      GdkRectangle *rectangles;
      gint n_rectangles;
      
      gdk_region_get_rectangles (expose->region,
				 &rectangles,
				 &n_rectangles);
      
      while (n_rectangles--)
	egg_icon_list_paint_rubberband (icon_list, &rectangles[n_rectangles]);

      g_free (rectangles);
    }

  return TRUE;
}

static gboolean
egg_icon_list_motion (GtkWidget      *widget,
		      GdkEventMotion *event)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (widget);

  egg_icon_list_maybe_begin_dragging_items (icon_list, event);

  if (icon_list->priv->rubberbanding)
    rubberbanding (widget);
  
  return TRUE;
}

static gboolean
egg_icon_list_button_press (GtkWidget      *widget,
			    GdkEventButton *event)
{
  EggIconList *icon_list;
  EggIconListItem *item;
  gboolean dirty = FALSE;
  
  icon_list = EGG_ICON_LIST (widget);

  if (event->window != icon_list->priv->bin_window)
    return FALSE;

  if (!GTK_WIDGET_HAS_FOCUS (widget))
    gtk_widget_grab_focus (widget);

  if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
    {

      if (icon_list->priv->selection_mode == GTK_SELECTION_NONE)
	return TRUE;
      
      item = egg_icon_list_get_item_at_pos (icon_list,
					    event->x, event->y);

      if (item != NULL)
	{
	  if (icon_list->priv->selection_mode == GTK_SELECTION_MULTIPLE &&
	      (event->state & GDK_CONTROL_MASK))
	    {
	      item->selected = !item->selected;
	      dirty = TRUE;
	    }
	  else
	    {
	      if (!item->selected)
		{
		  egg_icon_list_unselect_all_internal (icon_list, FALSE);
	      
		  item->selected = TRUE;
		  dirty = TRUE;
		}
	    }
	  
	  egg_icon_list_set_cursor_item (icon_list, item);
	  egg_icon_list_queue_draw_item (icon_list, item);
	  
	  /* Save press to possibly begin a drag */
	  if (icon_list->priv->pressed_button < 0)
	    {
	      icon_list->priv->pressed_button = event->button;
	      icon_list->priv->press_start_x = event->x;
	      icon_list->priv->press_start_y = event->y;
	    }

	  if (!icon_list->priv->last_single_clicked)
	    icon_list->priv->last_single_clicked = item;
	}
      else
	{
	  if (icon_list->priv->selection_mode != GTK_SELECTION_BROWSE &&
	      !(event->state & GDK_CONTROL_MASK))
	    {
	      dirty = egg_icon_list_unselect_all_internal (icon_list, FALSE);
	    }
	  
	  if (icon_list->priv->selection_mode == GTK_SELECTION_MULTIPLE)
	    egg_icon_list_start_rubberbanding (icon_list, event->x, event->y);
	}

    }

  if (event->button == 1 && event->type == GDK_2BUTTON_PRESS)
    {
      item = egg_icon_list_get_item_at_pos (icon_list,
					    event->x, event->y);

      if (item && item == icon_list->priv->last_single_clicked)
	{
	  egg_icon_list_item_activated (icon_list, item);
	}

      icon_list->priv->last_single_clicked = NULL;
    }
  
  if (dirty)
    g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);

  return TRUE;
}

static gboolean
egg_icon_list_button_release (GtkWidget      *widget,
			      GdkEventButton *event)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (widget);

  if (icon_list->priv->pressed_button == event->button)
    icon_list->priv->pressed_button = -1;

  egg_icon_list_stop_rubberbanding (icon_list);

  return TRUE;
}


static gboolean
egg_icon_list_key_press (GtkWidget    *widget,
			 GdkEventKey  *event)
{
  if ((* GTK_WIDGET_CLASS (parent_class)->key_press_event) (widget, event))
    return TRUE;

  return FALSE;
  
  if ((event->state & (GDK_CONTROL_MASK | GDK_MOD1_MASK)) == 0)
    egg_icon_list_append_typeahead_string (EGG_ICON_LIST (widget), event->string);

  return TRUE;
}

static void
egg_icon_list_select_first_matching_item (EggIconList  *icon_list,
					  const char   *pattern)
{
  GList *items;

  if (pattern == NULL)
    return;
  
  for (items = icon_list->priv->items; items; items = items->next)
    {
      EggIconListItem *item = items->data;

      if (strncmp (pattern, item->label, strlen (pattern)) == 0)
	{
	  egg_icon_list_select_item (icon_list, item);
	  break;
	}
    }
}

static void
rubberbanding (gpointer data)
{
  EggIconList *icon_list;
  gint x, y;
  GdkRectangle old_area;
  GdkRectangle new_area;
  GdkRectangle common;
  GdkRegion *invalid_region;
  
  icon_list = EGG_ICON_LIST (data);

  gdk_window_get_pointer (icon_list->priv->bin_window, &x, &y, NULL);

  x = MAX (x, 0);
  y = MAX (y, 0);

  old_area.x = MIN (icon_list->priv->rubberband_x1,
		    icon_list->priv->rubberband_x2);
  old_area.y = MIN (icon_list->priv->rubberband_y1,
		    icon_list->priv->rubberband_y2);
  old_area.width = ABS (icon_list->priv->rubberband_x2 -
			icon_list->priv->rubberband_x1) + 1;
  old_area.height = ABS (icon_list->priv->rubberband_y2 -
			 icon_list->priv->rubberband_y1) + 1;
  
  new_area.x = MIN (icon_list->priv->rubberband_x1, x);
  new_area.y = MIN (icon_list->priv->rubberband_y1, y);
  new_area.width = ABS (x - icon_list->priv->rubberband_x1) + 1;
  new_area.height = ABS (y - icon_list->priv->rubberband_y1) + 1;

946 947 948
  invalid_region = gdk_region_rectangle (&old_area);
  gdk_region_union_with_rect (invalid_region, &new_area);

Anders Carlsson's avatar
Anders Carlsson committed
949
  gdk_rectangle_intersect (&old_area, &new_area, &common);
950
  if (common.width > 2 && common.height > 2)
Anders Carlsson's avatar
Anders Carlsson committed
951
    {
952 953 954
      GdkRegion *common_region;

      /* make sure the border is invalidated */
Anders Carlsson's avatar
Anders Carlsson committed
955 956
      common.x += 1;
      common.y += 1;
957
      common.width -= 2;
Anders Carlsson's avatar
Anders Carlsson committed
958
      common.height -= 2;
959 960
      
      common_region = gdk_region_rectangle (&common);
Anders Carlsson's avatar
Anders Carlsson committed
961

962 963 964
      gdk_region_subtract (invalid_region, common_region);
      gdk_region_destroy (common_region);
    }
Anders Carlsson's avatar
Anders Carlsson committed
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
  
  gdk_window_invalidate_region (icon_list->priv->bin_window, invalid_region, TRUE);
    
  gdk_region_destroy (invalid_region);

  icon_list->priv->rubberband_x2 = x;
  icon_list->priv->rubberband_y2 = y;  

  egg_icon_list_update_rubberband_selection (icon_list);
}

static void
egg_icon_list_start_rubberbanding (EggIconList  *icon_list,
				   gint          x,
				   gint          y)
{
  GList *items;

  g_assert (!icon_list->priv->rubberbanding);

  for (items = icon_list->priv->items; items; items = items->next)
    {
      EggIconListItem *item = items->data;

      item->selected_before_rubberbanding = item->selected;
    }
  
  icon_list->priv->rubberband_x1 = x;
  icon_list->priv->rubberband_y1 = y;
  icon_list->priv->rubberband_x2 = x;
  icon_list->priv->rubberband_y2 = y;

  icon_list->priv->rubberbanding = TRUE;

  gtk_grab_add (GTK_WIDGET (icon_list));
}

static void
egg_icon_list_stop_rubberbanding (EggIconList *icon_list)
{
  if (!icon_list->priv->rubberbanding)
    return;

  icon_list->priv->rubberbanding = FALSE;

  gtk_grab_remove (GTK_WIDGET (icon_list));
  
  gtk_widget_queue_draw (GTK_WIDGET (icon_list));
}

static gint
egg_icon_list_sort_func (EggIconListItem  *a,
			 EggIconListItem  *b,
			 EggIconList      *icon_list)
{
  gint result;

  result = (* icon_list->priv->sort_func) (icon_list, a, b,
					   icon_list->priv->sort_data);

  if (icon_list->priv->sort_order == GTK_SORT_DESCENDING)
    result = -result;

  return result;
}

static void
egg_icon_list_insert_item_sorted (EggIconList      *icon_list,
				  EggIconListItem  *item)
{
  GList *list;
  GList *tmp_list = icon_list->priv->items;
  gint cmp;
  
  egg_icon_list_validate (icon_list);

  list = g_list_alloc ();
  item->list = list;
  item->icon_list = icon_list;
  list->data = item;
Anders Carlsson's avatar
Anders Carlsson committed
1045
  egg_icon_list_item_ref (item);
Anders Carlsson's avatar
Anders Carlsson committed
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
  
  if (!icon_list->priv->items)
    {
      icon_list->priv->items = list;
      icon_list->priv->last_item = list;
      icon_list->priv->item_count += 1;

      egg_icon_list_validate (icon_list);
      
      return;
    }

  cmp = egg_icon_list_sort_func (item, tmp_list->data, icon_list);

  while ((tmp_list->next) && (cmp > 0))
    {
      tmp_list = tmp_list->next;
      cmp = egg_icon_list_sort_func (item, tmp_list->data, icon_list);
    }

  if ((!tmp_list->next) && (cmp > 0))
    {
      tmp_list->next = list;
      list->prev = tmp_list;
      icon_list->priv->last_item = list;
      icon_list->priv->item_count += 1;
      egg_icon_list_validate (icon_list);
      
      return;
    }

  if (tmp_list->prev)
    {
      tmp_list->prev->next = list;
      list->prev = tmp_list->prev;
    }
  
  list->next = tmp_list;
  tmp_list->prev = list;

  if (tmp_list == icon_list->priv->items)
    icon_list->priv->items = list;
  
  icon_list->priv->item_count += 1;
  egg_icon_list_validate (icon_list);

  egg_icon_list_queue_layout (icon_list);
}


static void
egg_icon_list_sort (EggIconList *icon_list)
{
  egg_icon_list_validate (icon_list);

  /* FIXME: We can optimize this */
  icon_list->priv->items = g_list_sort_with_data (icon_list->priv->items,
						  (GCompareDataFunc)egg_icon_list_sort_func,
						  icon_list);
  icon_list->priv->last_item = g_list_last (icon_list->priv->items);
  
  egg_icon_list_validate (icon_list);
  egg_icon_list_queue_layout (icon_list);
}


static void
egg_icon_list_validate (EggIconList *icon_list)
{
#if 0
  GList *list;

  g_print ("----\n");
  for (list = icon_list->priv->items; list; list = list->next)
    {
      EggIconListItem *item = list->data;

      g_print ("%s\n", egg_icon_list_item_get_label (item));
    }
  g_print ("----\n");
#endif
  
  g_assert (g_list_length (icon_list->priv->items) == icon_list->priv->item_count);
  g_assert (g_list_last (icon_list->priv->items) == icon_list->priv->last_item);
  g_assert (g_list_first (icon_list->priv->last_item) == icon_list->priv->items);
}

static void
egg_icon_list_update_rubberband_selection (EggIconList *icon_list)
{
  GList *items;
  gint x, y, width, height;
  gboolean dirty = FALSE;
  
  x = MIN (icon_list->priv->rubberband_x1,
	   icon_list->priv->rubberband_x2);
  y = MIN (icon_list->priv->rubberband_y1,
	   icon_list->priv->rubberband_y2);
  width = ABS (icon_list->priv->rubberband_x1 - 
	       icon_list->priv->rubberband_x2);
  height = ABS (icon_list->priv->rubberband_y1 - 
		icon_list->priv->rubberband_y2);
  
  for (items = icon_list->priv->items; items; items = items->next)
    {
      EggIconListItem *item = items->data;
      gboolean is_in;
      gboolean selected;
      
      is_in = egg_icon_list_item_hit_test (item, x, y, width, height);

      selected = is_in ^ item->selected_before_rubberbanding;

      if (item->selected != selected)
	{
	  item->selected = selected;
	  dirty = TRUE;
	  egg_icon_list_queue_draw_item (icon_list, item);
	}
    }

  if (dirty)
    g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);
}

static gboolean
egg_icon_list_item_hit_test (EggIconListItem  *item,
			     gint              x,
			     gint              y,
			     gint              width,
			     gint              height)
{
  /* First try the pixbuf */
  if (MIN (x + width, item->pixbuf_x + item->pixbuf_width) - MAX (x, item->pixbuf_x) > 0 &&
      MIN (y + height, item->pixbuf_y + item->pixbuf_height) - MAX (y, item->pixbuf_y) > 0)
    return TRUE;

  /* Then try the text */
  if (MIN (x + width, item->layout_x + item->layout_width) - MAX (x, item->layout_x) > 0 &&
      MIN (y + height, item->layout_y + item->layout_height) - MAX (y, item->layout_y) > 0)
    return TRUE;
  
  return FALSE;
}

static gboolean
egg_icon_list_maybe_begin_dragging_items (EggIconList     *icon_list,
					  GdkEventMotion  *event)
{
  gboolean retval = FALSE;
  gint button;
  if (icon_list->priv->pressed_button < 0)
    return retval;

  if (!gtk_drag_check_threshold (GTK_WIDGET (icon_list),
				 icon_list->priv->press_start_x,
				 icon_list->priv->press_start_y,
				 event->x, event->y))
    return retval;

  button = icon_list->priv->pressed_button;
  icon_list->priv->pressed_button = -1;
  
  {
    static GtkTargetEntry row_targets[] = {
      { "EGG_ICON_LIST_ITEMS", GTK_TARGET_SAME_APP, 0 }
    };
    GtkTargetList *target_list;
    GdkDragContext *context;
    EggIconListItem *item;
    
    retval = TRUE;
    
    target_list = gtk_target_list_new (row_targets, G_N_ELEMENTS (row_targets));

    context = gtk_drag_begin (GTK_WIDGET (icon_list),
			      target_list, GDK_ACTION_MOVE,
			      button,
			      (GdkEvent *)event);

    item = egg_icon_list_get_item_at_pos (icon_list,
					  icon_list->priv->press_start_x,
					  icon_list->priv->press_start_y);
    g_assert (item != NULL);
    gtk_drag_set_icon_pixbuf (context, egg_icon_list_item_get_icon (item),
			      event->x - item->x,
			      event->y - item->y);
  }
  
  return retval;
}


static gboolean
egg_icon_list_unselect_all_internal (EggIconList  *icon_list,
				     gboolean      emit)
{
  gboolean dirty = FALSE;
  GList *items;
  
  for (items = icon_list->priv->items; items; items = items->next)
    {
      EggIconListItem *item = items->data;

      if (item->selected)
	{
	  item->selected = FALSE;
	  dirty = TRUE;
	  egg_icon_list_queue_draw_item (icon_list, item);
	}
    }

  if (emit && dirty)
    g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);

  return dirty;
}


/* EggIconList signals */
static void
egg_icon_list_set_adjustments (EggIconList   *icon_list,
			       GtkAdjustment *hadj,
			       GtkAdjustment *vadj)
{
  gboolean need_adjust = FALSE;
Anders Carlsson's avatar
Anders Carlsson committed
1272

Anders Carlsson's avatar
Anders Carlsson committed
1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474
  if (hadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
  else
    hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
  if (vadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
  else
    vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));

  if (icon_list->priv->hadjustment && (icon_list->priv->hadjustment != hadj))
    {
      g_signal_handlers_disconnect_matched (icon_list->priv->hadjustment, G_SIGNAL_MATCH_DATA,
					   0, 0, NULL, NULL, icon_list);
      g_object_unref (icon_list->priv->hadjustment);
    }

  if (icon_list->priv->vadjustment && (icon_list->priv->vadjustment != vadj))
    {
      g_signal_handlers_disconnect_matched (icon_list->priv->vadjustment, G_SIGNAL_MATCH_DATA,
					    0, 0, NULL, NULL, icon_list);
      g_object_unref (icon_list->priv->vadjustment);
    }

  if (icon_list->priv->hadjustment != hadj)
    {
      icon_list->priv->hadjustment = hadj;
      g_object_ref (icon_list->priv->hadjustment);
      gtk_object_sink (GTK_OBJECT (icon_list->priv->hadjustment));

      g_signal_connect (icon_list->priv->hadjustment, "value_changed",
			G_CALLBACK (egg_icon_list_adjustment_changed),
			icon_list);
      need_adjust = TRUE;
    }

  if (icon_list->priv->vadjustment != vadj)
    {
      icon_list->priv->vadjustment = vadj;
      g_object_ref (icon_list->priv->vadjustment);
      gtk_object_sink (GTK_OBJECT (icon_list->priv->vadjustment));

      g_signal_connect (icon_list->priv->vadjustment, "value_changed",
			G_CALLBACK (egg_icon_list_adjustment_changed),
			icon_list);
      need_adjust = TRUE;
    }

  if (need_adjust)
    egg_icon_list_adjustment_changed (NULL, icon_list);
}

static void
egg_icon_list_real_select_all (EggIconList *icon_list)
{
  if (icon_list->priv->selection_mode != GTK_SELECTION_MULTIPLE)
    return;

  egg_icon_list_select_all (icon_list);
}

static void
egg_icon_list_real_unselect_all (EggIconList *icon_list)
{
  if (icon_list->priv->selection_mode == GTK_SELECTION_BROWSE)
    return;

  egg_icon_list_unselect_all (icon_list);
}

static void
egg_icon_list_real_select_cursor_item (EggIconList *icon_list)
{
  egg_icon_list_unselect_all (icon_list);
  
  if (icon_list->priv->cursor_item != NULL)
    egg_icon_list_select_item (icon_list, icon_list->priv->cursor_item);
}

static void
egg_icon_list_real_toggle_cursor_item (EggIconList *icon_list)
{
  if (icon_list->priv->selection_mode == GTK_SELECTION_NONE)
    return;

  /* FIXME: Use another function here */
  if (icon_list->priv->cursor_item != NULL)
    {
      if (icon_list->priv->selection_mode == GTK_SELECTION_BROWSE)
	icon_list->priv->cursor_item->selected = TRUE;
      else
	icon_list->priv->cursor_item->selected = !icon_list->priv->cursor_item->selected;
      
      egg_icon_list_queue_draw_item (icon_list, icon_list->priv->cursor_item);
    }
}

/* Internal functions */
static void
egg_icon_list_adjustment_changed (GtkAdjustment *adjustment,
				  EggIconList   *icon_list)
{
  if (GTK_WIDGET_REALIZED (icon_list))
    {
      gdk_window_move (icon_list->priv->bin_window,
		       - icon_list->priv->hadjustment->value,
		       - icon_list->priv->vadjustment->value);
      gdk_window_process_updates (icon_list->priv->bin_window, TRUE);
    }
}

static GList *
egg_icon_list_layout_single_row (EggIconList *icon_list, GList *first_item, gint *y, gint *maximum_width)
{
  gint x, current_width, max_height, max_pixbuf_height;
  GList *items, *last_item;
  gint icon_padding;
  gint left_margin, right_margin;
  gint maximum_layout_width;
  
  x = 0;
  max_height = 0;
  max_pixbuf_height = 0;
  items = first_item;
  current_width = 0;

  gtk_widget_style_get (GTK_WIDGET (icon_list),
			"icon_padding", &icon_padding,
			"left_margin", &left_margin,
			"right_margin", &right_margin,
			NULL);
  
  x += left_margin;
  current_width += left_margin + right_margin;
  items = first_item;

  while (items)
    {
      EggIconListItem *item = items->data;

      egg_icon_list_calculate_item_size (icon_list, item);

      current_width += MAX (item->width, MINIMUM_ICON_ITEM_WIDTH);

      /* Don't add padding to the first or last icon */
      
      if (current_width > GTK_WIDGET (icon_list)->allocation.width &&
	  items != first_item)
	break;

      maximum_layout_width = MAX (item->pixbuf_width, MINIMUM_ICON_ITEM_WIDTH);

      item->y = *y;
      item->x = x;
      
      if (item->width < MINIMUM_ICON_ITEM_WIDTH) {
	item->x += (MINIMUM_ICON_ITEM_WIDTH - item->width) / 2;
	x += (MINIMUM_ICON_ITEM_WIDTH - item->width);
      }

      item->pixbuf_x = item->x + (item->width - item->pixbuf_width) / 2;
      item->layout_x = item->x + (item->width - item->layout_width) / 2;

      x += item->width;

      max_height = MAX (max_height, item->height);
      max_pixbuf_height = MAX (max_pixbuf_height, item->pixbuf_height);
      
      if (current_width > *maximum_width)
	*maximum_width = current_width;

      items = items->next;
    }

  last_item = items;

  *y += max_height + icon_padding;

  /* Now go through the row again and align the icons */
  for (items = first_item; items != last_item; items = items->next)
    {
      EggIconListItem *item = items->data;

      item->pixbuf_y = item->y + (max_pixbuf_height - item->pixbuf_height);
      item->layout_y = item->pixbuf_y + item->pixbuf_height + ICON_TEXT_PADDING;

      /* Update the bounding box */
      item->y = item->pixbuf_y;

      /* We may want to readjust the new y coordinate. */
      if (item->y + item->height > *y)
	*y = item->y + item->height;
    }
  
  return last_item;
}

static void
egg_icon_list_set_adjustment_upper (GtkAdjustment *adj,
				    gdouble        upper)
{
  if (upper != adj->upper)
    {
Anders Carlsson's avatar
Anders Carlsson committed
1475
      gdouble min = MAX (0.0, upper - adj->page_size);
Anders Carlsson's avatar
Anders Carlsson committed
1476 1477 1478
      gboolean value_changed = FALSE;
      
      adj->upper = upper;
Anders Carlsson's avatar
Anders Carlsson committed
1479

Anders Carlsson's avatar
Anders Carlsson committed
1480 1481 1482 1483 1484 1485
      if (adj->value > min)
	{
	  adj->value = min;
	  value_changed = TRUE;
	}
      
Anders Carlsson's avatar
Anders Carlsson committed
1486
      gtk_adjustment_changed (adj);
Anders Carlsson's avatar
Anders Carlsson committed
1487 1488
      
      if (value_changed)
Anders Carlsson's avatar
Anders Carlsson committed
1489
	gtk_adjustment_value_changed (adj);
Anders Carlsson's avatar
Anders Carlsson committed
1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
    }
}

static void
egg_icon_list_layout (EggIconList *icon_list)
{
  gint y = 0, maximum_width = 0;
  GList *icons;
  GtkWidget *widget;
  gint top_margin, bottom_margin;
  
  widget = GTK_WIDGET (icon_list);
  icons = icon_list->priv->items;

  gtk_widget_style_get (widget,
			"top_margin", &top_margin,
			"bottom_margin", &bottom_margin,
			NULL);
  y += top_margin;
  
  do
    {
      icons = egg_icon_list_layout_single_row (icon_list, icons, &y, &maximum_width);
    }
  while (icons != NULL);

  if (maximum_width != icon_list->priv->width)
    {
      icon_list->priv->width = maximum_width;
    }
  y += bottom_margin;
  
  if (y != icon_list->priv->height)
    {
      icon_list->priv->height = y;
    }

  egg_icon_list_set_adjustment_upper (icon_list->priv->hadjustment, icon_list->priv->width);
  egg_icon_list_set_adjustment_upper (icon_list->priv->vadjustment, icon_list->priv->height);

  if (GTK_WIDGET_REALIZED (icon_list))
    {
      gdk_window_resize (icon_list->priv->bin_window,
			 MAX (icon_list->priv->width, widget->allocation.width),
			 MAX (icon_list->priv->height, widget->allocation.height));
    }

  if (icon_list->priv->layout_idle_id != 0)
    {
      g_source_remove (icon_list->priv->layout_idle_id);
      icon_list->priv->layout_idle_id = 0;
    }

  gtk_widget_queue_draw (GTK_WIDGET (icon_list));
}

/* Creates or updates the pango layout and calculates the size */
static void
egg_icon_list_calculate_item_size (EggIconList *icon_list, EggIconListItem *item)
{
  int layout_width, layout_height;
  int maximum_layout_width;
  
  if (item->width != -1 && item->width != -1) 
    return;

  item->pixbuf_width = gdk_pixbuf_get_width (item->icon);
  item->pixbuf_height = gdk_pixbuf_get_height (item->icon);

  maximum_layout_width = MAX (item->pixbuf_width, MINIMUM_ICON_ITEM_WIDTH);

  pango_layout_set_text (icon_list->priv->layout, item->label, -1);

  pango_layout_set_alignment (icon_list->priv->layout, PANGO_ALIGN_CENTER);
  pango_layout_set_width (icon_list->priv->layout, maximum_layout_width * PANGO_SCALE);
  
  pango_layout_get_pixel_size (icon_list->priv->layout, &layout_width, &layout_height);

  item->width = MAX ((layout_width + 2 * ICON_TEXT_PADDING), item->pixbuf_width);
  item->height = layout_height + 2 * ICON_TEXT_PADDING + item->pixbuf_height;
  item->layout_width = layout_width;
  item->layout_height = layout_height;
}

static void
egg_icon_list_item_invalidate_size (EggIconListItem *item)
{
  item->width = -1;
  item->height = -1;
}

static GdkPixbuf *
create_colorized_pixbuf (GdkPixbuf *src, GdkColor *new_color)
{
	gint i, j;
	gint width, height, has_alpha, src_row_stride, dst_row_stride;
	gint red_value, green_value, blue_value;
	guchar *target_pixels;
	guchar *original_pixels;
	guchar *pixsrc;
	guchar *pixdest;
	GdkPixbuf *dest;

	red_value = new_color->red / 255.0;
	green_value = new_color->green / 255.0;
	blue_value = new_color->blue / 255.0;

	dest = gdk_pixbuf_new (gdk_pixbuf_get_colorspace (src),
			       gdk_pixbuf_get_has_alpha (src),
			       gdk_pixbuf_get_bits_per_sample (src),
			       gdk_pixbuf_get_width (src),
			       gdk_pixbuf_get_height (src));
	
	has_alpha = gdk_pixbuf_get_has_alpha (src);
	width = gdk_pixbuf_get_width (src);
	height = gdk_pixbuf_get_height (src);
	src_row_stride = gdk_pixbuf_get_rowstride (src);
	dst_row_stride = gdk_pixbuf_get_rowstride (dest);
	target_pixels = gdk_pixbuf_get_pixels (dest);
	original_pixels = gdk_pixbuf_get_pixels (src);

	for (i = 0; i < height; i++) {
		pixdest = target_pixels + i*dst_row_stride;
		pixsrc = original_pixels + i*src_row_stride;
		for (j = 0; j < width; j++) {		
			*pixdest++ = (*pixsrc++ * red_value) >> 8;
			*pixdest++ = (*pixsrc++ * green_value) >> 8;
			*pixdest++ = (*pixsrc++ * blue_value) >> 8;
			if (has_alpha) {
				*pixdest++ = *pixsrc++;
			}
		}
	}
	return dest;
}

static void
egg_icon_list_paint_item (EggIconList     *icon_list,
			  EggIconListItem *item,
			  GdkRectangle    *area)
{
  GdkPixbuf *pixbuf;
  GtkStateType state;
  
  if (GTK_WIDGET_HAS_FOCUS (icon_list))
    state = GTK_STATE_SELECTED;
  else
    state = GTK_STATE_ACTIVE;

  if (item->selected)
    pixbuf = create_colorized_pixbuf (item->icon,
				      &GTK_WIDGET (icon_list)->style->base[state]);
  else
1643
    pixbuf = g_object_ref (item->icon);
Anders Carlsson's avatar
Anders Carlsson committed
1644 1645 1646 1647 1648 1649 1650

  gdk_draw_pixbuf (icon_list->priv->bin_window, NULL, pixbuf,
		   0, 0,
		   item->pixbuf_x, item->pixbuf_y,
		   item->pixbuf_width, item->pixbuf_height,
		   GDK_RGB_DITHER_NORMAL,
		   item->pixbuf_width, item->pixbuf_height);
Anders Carlsson's avatar
Anders Carlsson committed
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684
  g_object_unref (pixbuf);

  if (item->selected)
    {
      gdk_draw_rectangle (icon_list->priv->bin_window,
			  GTK_WIDGET (icon_list)->style->base_gc[state],
			  TRUE,
			  item->layout_x - ICON_TEXT_PADDING,
			  item->layout_y - ICON_TEXT_PADDING,
			  item->layout_width + 2 * ICON_TEXT_PADDING,
			  item->layout_height + 2 * ICON_TEXT_PADDING);
    }

  pango_layout_set_text (icon_list->priv->layout, item->label, -1);
  gdk_draw_layout (icon_list->priv->bin_window,
		   GTK_WIDGET (icon_list)->style->text_gc[item->selected ? state : GTK_STATE_NORMAL],
		   item->layout_x - ((item->width - item->layout_width) / 2) - (MAX (item->pixbuf_width, MINIMUM_ICON_ITEM_WIDTH) - item->width) / 2,
		   item->layout_y,
		   icon_list->priv->layout);

  if (GTK_WIDGET_HAS_FOCUS (icon_list) &&
      item == icon_list->priv->cursor_item)
    gtk_paint_focus (GTK_WIDGET (icon_list)->style,
		     icon_list->priv->bin_window,
		     item->selected ? GTK_STATE_SELECTED : GTK_STATE_NORMAL,
		     area,
		     GTK_WIDGET (icon_list),
		     "iconlist",
		     item->layout_x - ICON_TEXT_PADDING,
		     item->layout_y - ICON_TEXT_PADDING,
		     item->layout_width + 2 * ICON_TEXT_PADDING,
		     item->layout_height + 2 * ICON_TEXT_PADDING);
}

1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
static guint32
egg_gdk_color_to_rgb (const GdkColor *color)
{
  guint32 result;
  result = (0xff0000 | (color->red & 0xff00));
  result <<= 8;
  result |= ((color->green & 0xff00) | (color->blue >> 8));
  return result;
}

Anders Carlsson's avatar
Anders Carlsson committed
1695 1696 1697 1698 1699 1700 1701 1702
static void
egg_icon_list_paint_rubberband (EggIconList     *icon_list,
				GdkRectangle    *area)
{
  GdkRectangle rect;
  GdkPixbuf *pixbuf;
  GdkGC *gc;
  GdkRectangle rubber_rect;
1703 1704 1705 1706
  GdkColor *fill_color_gdk;
  guint fill_color;
  guchar fill_color_alpha;

Anders Carlsson's avatar
Anders Carlsson committed
1707 1708 1709 1710 1711 1712 1713 1714
  rubber_rect.x = MIN (icon_list->priv->rubberband_x1, icon_list->priv->rubberband_x2);
  rubber_rect.y = MIN (icon_list->priv->rubberband_y1, icon_list->priv->rubberband_y2);
  rubber_rect.width = ABS (icon_list->priv->rubberband_x1 - icon_list->priv->rubberband_x2) + 1;
  rubber_rect.height = ABS (icon_list->priv->rubberband_y1 - icon_list->priv->rubberband_y2) + 1;

  if (!gdk_rectangle_intersect (&rubber_rect, area, &rect))
    return;

1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725
  gtk_widget_style_get (GTK_WIDGET (icon_list),
                        "selection_box_color", &fill_color_gdk,
                        "selection_box_alpha", &fill_color_alpha,
                        NULL);

  if (!fill_color_gdk) {
    fill_color_gdk = gdk_color_copy (&GTK_WIDGET (icon_list)->style->base[GTK_STATE_SELECTED]);
  }

  fill_color = egg_gdk_color_to_rgb (fill_color_gdk) << 8 | fill_color_alpha;

Anders Carlsson's avatar
Anders Carlsson committed
1726
  pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, rect.width, rect.height);
1727
  gdk_pixbuf_fill (pixbuf, fill_color);
Anders Carlsson's avatar
Anders Carlsson committed
1728

Anders Carlsson's avatar
Anders Carlsson committed
1729 1730 1731 1732 1733 1734
  gdk_draw_pixbuf (icon_list->priv->bin_window, NULL, pixbuf,
		   0, 0, 
		   rect.x,rect.y,
		   rect.width, rect.height,
		   GDK_RGB_DITHER_NONE,
		   0, 0);
Anders Carlsson's avatar
Anders Carlsson committed
1735 1736
  g_object_unref (pixbuf);
  gc = gdk_gc_new (icon_list->priv->bin_window);
1737
  gdk_gc_set_rgb_fg_color (gc, fill_color_gdk);
Anders Carlsson's avatar
Anders Carlsson committed
1738 1739 1740 1741 1742
  gdk_gc_set_clip_rectangle (gc, &rect);
  gdk_draw_rectangle (icon_list->priv->bin_window,
		      gc, FALSE,
		      rubber_rect.x, rubber_rect.y,
		      rubber_rect.width - 1, rubber_rect.height - 1);
1743
  gdk_color_free (fill_color_gdk);
Anders Carlsson's avatar
Anders Carlsson committed
1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
  g_object_unref (gc);
}

static void
egg_icon_list_queue_draw_item (EggIconList     *icon_list,
			       EggIconListItem *item)
{
  GdkRectangle rect;

  rect.x = item->x;
  rect.y = item->y;
  rect.width = item->width;
  rect.height = item->height;

  gdk_window_invalidate_rect (icon_list->priv->bin_window, &rect, TRUE);
}

static gboolean
layout_callback (gpointer user_data)
{
  EggIconList *icon_list;

  icon_list = EGG_ICON_LIST (user_data);
  
  icon_list->priv->layout_idle_id = 0;

  egg_icon_list_layout (icon_list);
  
  return FALSE;
}

static void
egg_icon_list_queue_layout (EggIconList *icon_list)
{
  if (icon_list->priv->layout_idle_id != 0)
    return;

  icon_list->priv->layout_idle_id = g_idle_add (layout_callback, icon_list);
}

static void
egg_icon_list_set_cursor_item (EggIconList     *icon_list,
			       EggIconListItem *item)
{
  if (icon_list->priv->cursor_item == item)
    return;

  if (icon_list->priv->cursor_item != NULL)
    egg_icon_list_queue_draw_item (icon_list, icon_list->priv->cursor_item);
  
  icon_list->priv->cursor_item = item;
  egg_icon_list_queue_draw_item (icon_list, item);
}

static void
egg_icon_list_append_typeahead_string (EggIconList     *icon_list,
				       const gchar     *string)
{
  int i;
  char *typeahead_string;

  if (strlen (string) == 0)
    return;

  for (i = 0; i < strlen (string); i++)
    {
      if (!g_ascii_isprint (string[i]))
	return;
    }

  typeahead_string = g_strconcat (icon_list->priv->typeahead_string ?
				  icon_list->priv->typeahead_string : "",
				  string, NULL);
  g_free (icon_list->priv->typeahead_string);
  icon_list->priv->typeahead_string = typeahead_string;

  egg_icon_list_select_first_matching_item (icon_list,
					    icon_list->priv->typeahead_string);
  
  g_print ("wooo: \"%s\"\n", typeahead_string);
}

/* Public API */
GtkWidget *
egg_icon_list_new (void)
{
  EggIconList *icon_list;

  icon_list = g_object_new (EGG_TYPE_ICON_LIST, NULL);

  return GTK_WIDGET (icon_list);
}

EggIconListItem *
egg_icon_list_item_new (GdkPixbuf   *icon,
			const char  *label)
{
  EggIconListItem *item;

Anders Carlsson's avatar
Anders Carlsson committed
1843 1844 1845 1846 1847
  item = g_new0 (EggIconListItem, 1);

  item->ref_count = 1;
  item->width = -1;
  item->height = -1;
Anders Carlsson's avatar
Anders Carlsson committed
1848 1849 1850 1851 1852 1853
  item->label = g_strdup (label);
  item->icon = g_object_ref (icon);
  
  return item;
}

Anders Carlsson's avatar
Anders Carlsson committed
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
void
egg_icon_list_item_ref (EggIconListItem *item)
{
  g_return_if_fail (item != NULL);

  item->ref_count += 1;
}

void
egg_icon_list_item_unref (EggIconListItem *item)
{
  g_return_if_fail (item != NULL);

  item->ref_count -= 1;

  if (item->ref_count == 0)
    {
Anders Carlsson's avatar
Anders Carlsson committed
1871 1872 1873
      if (item->destroy_notify)
	item->destroy_notify (item->user_data);
	
Anders Carlsson's avatar
Anders Carlsson committed
1874 1875 1876 1877 1878 1879 1880
      g_free (item->label);
      g_object_unref (item->icon);
      g_free (item);
    }
  
}

Anders Carlsson's avatar
Anders Carlsson committed
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
void
egg_icon_list_item_set_data (EggIconListItem  *item,
			     gpointer          data)
{
  egg_icon_list_item_set_data_full (item, data, NULL);
}

void
egg_icon_list_item_set_data_full (EggIconListItem  *item,
				  gpointer          data, 
				  GDestroyNotify    destroy_notify)
{
Anders Carlsson's avatar
Anders Carlsson committed
1893 1894 1895 1896
  g_return_if_fail (item != NULL);

  if (item->destroy_notify)
    item->destroy_notify (item->user_data);
Anders Carlsson's avatar
Anders Carlsson committed
1897

Anders Carlsson's avatar
Anders Carlsson committed
1898 1899
  item->destroy_notify = destroy_notify;
  item->user_data = data;
Anders Carlsson's avatar
Anders Carlsson committed
1900 1901 1902 1903 1904
}

gpointer
egg_icon_list_item_get_data (EggIconListItem *item)
{
Anders Carlsson's avatar
Anders Carlsson committed
1905 1906 1907
  g_return_val_if_fail (item != NULL, NULL);
  
  return item->user_data;
Anders Carlsson's avatar
Anders Carlsson committed
1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
}

void
egg_icon_list_item_set_label (EggIconListItem  *item,
			      const char       *label)
{
  g_return_if_fail (item != NULL);
  g_return_if_fail (label != NULL);

  if (strcmp (item->label, label) == 0)
    return;

  g_free (item->label);
  item->label = g_strdup (label);
  egg_icon_list_item_invalidate_size (item);

  egg_icon_list_queue_layout (item->icon_list);

  g_object_notify (G_OBJECT (item), "label");
}

G_CONST_RETURN gchar *
egg_icon_list_item_get_label (EggIconListItem *item)
{
  g_return_val_if_fail (item != NULL, NULL);

  return item->label;
}

void
egg_icon_list_item_set_icon (EggIconListItem  *item,
			     GdkPixbuf        *icon)
{
  g_return_if_fail (item != NULL);

  if (icon == item->icon)
    return;

  g_object_unref (item->icon);
  item->icon = g_object_ref (icon);

  egg_icon_list_item_invalidate_size (item);

  egg_icon_list_queue_layout (item->icon_list);
}

GdkPixbuf *
egg_icon_list_item_get_icon (EggIconListItem  *item)
{
  g_return_val_if_fail (item != NULL, NULL);

  return item->icon;
}

void
egg_icon_list_append_item (EggIconList     *icon_list,
			   EggIconListItem *item)
{
  GList *list;
  
Anders Carlsson's avatar
Anders Carlsson committed
1968
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983
  g_return_if_fail (item != NULL);
  g_return_if_fail (item->icon_list == NULL);
  
  if (icon_list->priv->sorted)
    {
      egg_icon_list_insert_item_sorted (icon_list, item);
      return;
    }

  egg_icon_list_validate (icon_list);
  
  list = g_list_alloc ();
  item->list = list;
  item->icon_list = icon_list;
  list->data = item;
Anders Carlsson's avatar
Anders Carlsson committed
1984
  egg_icon_list_item_ref (item);
Anders Carlsson's avatar
Anders Carlsson committed
1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009
  
  if (icon_list->priv->last_item)
    {
      icon_list->priv->last_item->next = list;
      list->prev = icon_list->priv->last_item;
    }
  else
      icon_list->priv->items = list;

  icon_list->priv->last_item = list;
  icon_list->priv->item_count += 1;
  
  egg_icon_list_validate (icon_list);

  g_signal_emit (icon_list, icon_list_signals[ITEM_ADDED], 0, item);
  
  egg_icon_list_queue_layout (icon_list);
}

void
egg_icon_list_prepend_item (EggIconList      *icon_list,
			    EggIconListItem  *item)
{
  GList *list;

Anders Carlsson's avatar
Anders Carlsson committed
2010
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2011 2012 2013 2014 2015 2016 2017 2018 2019
  g_return_if_fail (item != NULL);
  g_return_if_fail (item->icon_list == NULL);
  
  egg_icon_list_validate (icon_list);

  list = g_list_alloc ();
  item->list = list;
  item->icon_list = icon_list;
  list->data = item;
Anders Carlsson's avatar
Anders Carlsson committed
2020
  egg_icon_list_item_ref (item);
Anders Carlsson's avatar
Anders Carlsson committed
2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047
  
  if (icon_list->priv->last_item == NULL)
    icon_list->priv->last_item = list;
  
  if (icon_list->priv->items)
      icon_list->priv->items->prev = list;
  
  list->next = icon_list->priv->items;
  icon_list->priv->items = list;
  icon_list->priv->item_count += 1;
  
  egg_icon_list_validate (icon_list);

  g_signal_emit (icon_list, icon_list_signals[ITEM_ADDED], 0, item);

  egg_icon_list_queue_layout (icon_list);

}


void
egg_icon_list_insert_item_before (EggIconList      *icon_list,
				  EggIconListItem  *sibling,
				  EggIconListItem  *item)
{
  GList *list;
  
Anders Carlsson's avatar
Anders Carlsson committed
2048
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066
  g_return_if_fail (item != NULL);
  g_return_if_fail (item->icon_list == NULL);
  
  if (icon_list->priv->sorted)
    {
      egg_icon_list_insert_item_sorted (icon_list, item);
      return;
    }
  
  if (sibling == NULL)
    egg_icon_list_append_item (icon_list, item);
  
  egg_icon_list_validate (icon_list);

  list = g_list_alloc ();
  item->list = list;
  item->icon_list = icon_list;
  list->data = item;
Anders Carlsson's avatar
Anders Carlsson committed
2067
  egg_icon_list_item_ref (item);
Anders Carlsson's avatar
Anders Carlsson committed
2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
  
  list->prev = sibling->list->prev;
  list->next = sibling->list;
  sibling->list->prev->next = list;
  sibling->list->prev = list;

  if (sibling->list == icon_list->priv->items)
    icon_list->priv->items = list;

  icon_list->priv->item_count += 1;
  egg_icon_list_validate (icon_list);

  g_signal_emit (icon_list, icon_list_signals[ITEM_ADDED], 0, item);
  
  egg_icon_list_queue_layout (icon_list);
}

void
egg_icon_list_insert_item_after  (EggIconList      *icon_list,
				  EggIconListItem  *sibling,
				  EggIconListItem  *item)
{
  GList *list;

Anders Carlsson's avatar
Anders Carlsson committed
2092
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113
  g_return_if_fail (item != NULL);
  g_return_if_fail (item->icon_list == NULL);
  
  if (icon_list->priv->sorted)
    {
      egg_icon_list_insert_item_sorted (icon_list, item);
      return;
    }

  if (sibling == NULL)
    {
      egg_icon_list_prepend_item (icon_list, item);
      return;
    }

  egg_icon_list_validate (icon_list);

  list = g_list_alloc ();
  item->list = list;
  item->icon_list = icon_list;
  list->data = item;
Anders Carlsson's avatar
Anders Carlsson committed
2114
  egg_icon_list_item_ref (item);
Anders Carlsson's avatar
Anders Carlsson committed
2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134
  
  list->next = sibling->list->next;
  list->prev = sibling->list;
  sibling->list->next->prev = list;
  sibling->list->next = list;

  if (sibling->list == icon_list->priv->last_item)
    icon_list->priv->last_item = list;

  icon_list->priv->item_count += 1;
  egg_icon_list_validate (icon_list);
  g_signal_emit (icon_list, icon_list_signals[ITEM_ADDED], 0, item);
  
  egg_icon_list_queue_layout (icon_list);
}

void
egg_icon_list_remove_item (EggIconList      *icon_list,
			   EggIconListItem  *item)
{
Anders Carlsson's avatar
Anders Carlsson committed
2135
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167
  g_return_if_fail (item != NULL);
  g_return_if_fail (item->icon_list == icon_list);
  
  egg_icon_list_validate (icon_list);

  if (item->list->prev)
    item->list->prev->next = item->list->next;
  if (item->list->next)
    item->list->next->prev = item->list->prev;

  if (item->list == icon_list->priv->items)
    icon_list->priv->items = item->list->next;
  if (item->list == icon_list->priv->last_item)
    icon_list->priv->last_item = item->list->prev;

  g_list_free_1 (item->list);
  item->list = NULL;
  item->icon_list = NULL;
  egg_icon_list_item_invalidate_size (item);
  
  icon_list->priv->item_count -= 1;
  egg_icon_list_validate (icon_list);

  g_signal_emit (icon_list, icon_list_signals[ITEM_REMOVED], 0, item);

  if (item->selected)
    {
      item->selected = FALSE;

      g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);
    }

Anders Carlsson's avatar
Anders Carlsson committed
2168
#if 0
Anders Carlsson's avatar
Anders Carlsson committed
2169 2170
  if (icon_list->priv->cursor_item == item)
    g_error ("FIXME: Move to first focused item");
Anders Carlsson's avatar
Anders Carlsson committed
2171 2172
#endif
  
Anders Carlsson's avatar
Anders Carlsson committed
2173 2174 2175
  if (icon_list->priv->last_single_clicked == item)
    icon_list->priv->last_single_clicked = NULL;
  
Anders Carlsson's avatar
Anders Carlsson committed
2176
  egg_icon_list_item_unref (item);
Anders Carlsson's avatar
Anders Carlsson committed
2177 2178 2179 2180
  
  egg_icon_list_queue_layout (icon_list);
}

Anders Carlsson's avatar
Anders Carlsson committed
2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199
void
egg_icon_list_clear (EggIconList *icon_list)
{
  GList *items, *p;

  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
  
  items = g_list_copy (icon_list->priv->items);
  p = items;
  while (items)
    {
      EggIconListItem *item = items->data;

      egg_icon_list_remove_item (icon_list, item);
      items = items->next;
    }

  g_list_free (p);
}
Anders Carlsson's avatar
Anders Carlsson committed
2200 2201 2202 2203 2204 2205 2206 2207

EggIconListItem *
egg_icon_list_get_item_at_pos (EggIconList *icon_list,
			       gint         x,
			       gint         y)
{
  GList *items;
  
Anders Carlsson's avatar
Anders Carlsson committed
2208
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), NULL);
Anders Carlsson's avatar
Anders Carlsson committed
2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234

  for (items = icon_list->priv->items; items; items = items->next)
    {
      EggIconListItem *item = items->data;
      
      if (x > item->x && x < item->x + item->width &&
	  y > item->y && y < item->y + item->height)
	{
	  gint layout_x = item->x +  (item->width - item->layout_width) / 2;
	  /* Check if the mouse is inside the icon or the label */
	  if ((x > item->pixbuf_x && x < item->pixbuf_x + item->pixbuf_width &&
	       y > item->pixbuf_y && y < item->pixbuf_y + item->pixbuf_height) ||
	      (x > layout_x - ICON_TEXT_PADDING &&
	       x < layout_x + item->layout_width + ICON_TEXT_PADDING * 2 &&
	       y > item->layout_y - ICON_TEXT_PADDING
	       && y < item->layout_y + item->layout_height + ICON_TEXT_PADDING * 2))
	    return item;
	}
    }

  return NULL;
}

gint
egg_icon_list_get_item_count (EggIconList *icon_list)
{
Anders Carlsson's avatar
Anders Carlsson committed
2235
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), 0);
Anders Carlsson's avatar
Anders Carlsson committed
2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271

  return icon_list->priv->item_count;
}

void
egg_icon_list_foreach (EggIconList           *icon_list,
		       EggIconListForeachFunc func,
		       gpointer               data)
{
  GList *list;

  for (list = icon_list->priv->items; list; list = list->next)
    (* func) (icon_list, list->data, data);
}

void
egg_icon_list_selected_foreach (EggIconList           *icon_list,
				EggIconListForeachFunc func,
				gpointer               data)
{
  GList *list;

  for (list = icon_list->priv->items; list; list = list->next)
    {
      EggIconListItem *item = list->data;

      if (item->selected)
	(* func) (icon_list, list->data, data);
    }
}

GList *
egg_icon_list_get_selected (EggIconList  *icon_list)
{
  GList *list, *selected = NULL;

Anders Carlsson's avatar
Anders Carlsson committed
2272
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), NULL);
Anders Carlsson's avatar
Anders Carlsson committed
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288
  
  for (list = icon_list->priv->items; list; list = list->next)
    {
      EggIconListItem *item = list->data;
      
      if (item->selected)
	selected = g_list_prepend (selected, item);
    }

  return g_list_reverse (selected);
}

void
egg_icon_list_set_selection_mode (EggIconList      *icon_list,
				  GtkSelectionMode  mode)
{
Anders Carlsson's avatar
Anders Carlsson committed
2289
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305

  if (mode == icon_list->priv->selection_mode)
    return;
  
  if (mode == GTK_SELECTION_NONE ||
      icon_list->priv->selection_mode == GTK_SELECTION_MULTIPLE)
    egg_icon_list_unselect_all (icon_list);
  
  icon_list->priv->selection_mode = mode;

  g_object_notify (G_OBJECT (icon_list), "selection_mode");
}

GtkSelectionMode
egg_icon_list_get_selection_mode (EggIconList *icon_list)
{
Anders Carlsson's avatar
Anders Carlsson committed
2306
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), GTK_SELECTION_SINGLE);
Anders Carlsson's avatar
Anders Carlsson committed
2307 2308 2309 2310 2311 2312 2313 2314

  return icon_list->priv->selection_mode;
}

void
egg_icon_list_select_item (EggIconList      *icon_list,
			   EggIconListItem  *item)
{
Anders Carlsson's avatar
Anders Carlsson committed
2315
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337
  g_return_if_fail (item != NULL);

  if (item->selected)
    return;
  
  if (icon_list->priv->selection_mode == GTK_SELECTION_NONE)
    return;
  else if (icon_list->priv->selection_mode != GTK_SELECTION_MULTIPLE)
    egg_icon_list_unselect_all_internal (icon_list, FALSE);

  item->selected = TRUE;

  g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);
  
  egg_icon_list_queue_draw_item (icon_list, item);
}


void
egg_icon_list_unselect_item (EggIconList      *icon_list,
			     EggIconListItem  *item)
{
Anders Carlsson's avatar
Anders Carlsson committed
2338
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365
  g_return_if_fail (item != NULL);

  if (!item->selected)
    return;
  
  if (icon_list->priv->selection_mode == GTK_SELECTION_NONE ||
      icon_list->priv->selection_mode == GTK_SELECTION_BROWSE)
    return;
  
  item->selected = FALSE;

  g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);

  egg_icon_list_queue_draw_item (icon_list, item);
}

gboolean
egg_icon_list_item_is_selected (EggIconListItem *item)
{
  g_return_val_if_fail (item != NULL, FALSE);

  return item->selected;
}

void
egg_icon_list_unselect_all (EggIconList *icon_list)
{
Anders Carlsson's avatar
Anders Carlsson committed
2366
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2367 2368 2369 2370 2371 2372 2373 2374 2375 2376

  egg_icon_list_unselect_all_internal (icon_list, TRUE);
}

void
egg_icon_list_select_all (EggIconList *icon_list)
{
  GList *items;
  gboolean dirty = FALSE;
  
Anders Carlsson's avatar
Anders Carlsson committed
2377
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398

  for (items = icon_list->priv->items; items; items = items->next)
    {
      EggIconListItem *item = items->data;
      
      if (!item->selected)
	{
	  dirty = TRUE;
	  item->selected = TRUE;
	  egg_icon_list_queue_draw_item (icon_list, item);
	}
    }

  if (dirty)
    g_signal_emit (icon_list, icon_list_signals[SELECTION_CHANGED], 0);
}

void
egg_icon_list_set_sorted (EggIconList *icon_list,
			  gboolean     sorted)
{
Anders Carlsson's avatar
Anders Carlsson committed
2399
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414
  g_return_if_fail (icon_list->priv->sort_func != NULL);

  if (icon_list->priv->sorted == sorted)
    return;

  icon_list->priv->sorted = sorted;
  g_object_notify (G_OBJECT (icon_list), "sorted");
  
  if (icon_list->priv->sorted)
    egg_icon_list_sort (icon_list);
}

gboolean
egg_icon_list_get_sorted (EggIconList *icon_list)
{
Anders Carlsson's avatar
Anders Carlsson committed
2415
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), FALSE);
Anders Carlsson's avatar
Anders Carlsson committed
2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
  
  return icon_list->priv->sorted;
}

void
egg_icon_list_set_sort_func (EggIconList                *icon_list,
			     EggIconListItemCompareFunc  func,
			     gpointer                    data,
			     GDestroyNotify              destroy_notify)
{
Anders Carlsson's avatar
Anders Carlsson committed
2426
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441
  g_return_if_fail (func != NULL);

  if (icon_list->priv->sort_destroy_notify &&
      icon_list->priv->sort_data)
    (* icon_list->priv->sort_destroy_notify) (icon_list->priv->sort_data);

  icon_list->priv->sort_func = func;
  icon_list->priv->sort_data = data;
  icon_list->priv->sort_destroy_notify = destroy_notify;
}

void
egg_icon_list_set_sort_order (EggIconList  *icon_list,
			      GtkSortType   order)
{
Anders Carlsson's avatar
Anders Carlsson committed
2442
  g_return_if_fail (EGG_IS_ICON_LIST (icon_list));
Anders Carlsson's avatar
Anders Carlsson committed
2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457

  if (icon_list->priv->sort_order == order)
    return;

  icon_list->priv->sort_order = order;

  if (icon_list->priv->sorted)
    egg_icon_list_sort (icon_list);
  
  g_object_notify (G_OBJECT (icon_list), "sort_order");
}

GtkSortType
egg_icon_list_get_sort_order (EggIconList  *icon_list)
{
Anders Carlsson's avatar
Anders Carlsson committed
2458
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), GTK_SORT_ASCENDING);
Anders Carlsson's avatar
Anders Carlsson committed
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472

  return icon_list->priv->sort_order;
}

void
egg_icon_list_item_activated (EggIconList      *icon_list,
			      EggIconListItem  *item)
{
  g_signal_emit (G_OBJECT (icon_list), icon_list_signals[ITEM_ACTIVATED], 0, item);
}

GList *
egg_icon_list_get_items (EggIconList *icon_list)
{
Anders Carlsson's avatar
Anders Carlsson committed
2473
  g_return_val_if_fail (EGG_IS_ICON_LIST (icon_list), NULL);
Anders Carlsson's avatar
Anders Carlsson committed
2474 2475 2476 2477 2478 2479 2480

  return icon_list->priv->items;
}

EggIconList *
egg_icon_list_item_get_icon_list (EggIconListItem *item)
{
Anders Carlsson's avatar
Anders Carlsson committed
2481
  g_return_val_if_fail (item != NULL, NULL);
Anders Carlsson's avatar
Anders Carlsson committed
2482 2483 2484

  return item->icon_list;
}