gtkiconfactory.c 75.9 KB
Newer Older
1
/* GTK - The GIMP Toolkit
Havoc Pennington's avatar
Havoc Pennington committed
2
 * Copyright (C) 2000 Red Hat, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.
 */

/*
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

27
#include <config.h>
Manish Singh's avatar
Manish Singh committed
28 29 30
#include <stdlib.h>
#include <errno.h>
#include <string.h>
31
#include <pango/pango-utils.h>	/* For pango_scan_* */
32 33
#include "gtkiconfactory.h"
#include "stock-icons/gtkstockpixbufs.h"
34
#include "gtkdebug.h"
35
#include "gtkicontheme.h"
36
#include "gtksettings.h"
37
#include "gtkstock.h"
38
#include "gtkwidget.h"
Havoc Pennington's avatar
Havoc Pennington committed
39
#include "gtkintl.h"
40

41 42
static GSList *all_icon_factories = NULL;

43 44 45 46 47 48 49
typedef enum {
  GTK_ICON_SOURCE_EMPTY,
  GTK_ICON_SOURCE_ICON_NAME,
  GTK_ICON_SOURCE_FILENAME,
  GTK_ICON_SOURCE_PIXBUF
} GtkIconSourceType;

50 51
struct _GtkIconSource
{
52 53 54 55 56 57 58 59 60
  GtkIconSourceType type;
  
  union {
    gchar *icon_name;
    gchar *filename;
    GdkPixbuf *pixbuf;
  } source;
  
  GdkPixbuf *filename_pixbuf;
61 62 63 64 65 66 67 68 69 70 71 72 73 74

  GtkTextDirection direction;
  GtkStateType state;
  GtkIconSize size;

  /* If TRUE, then the parameter is wildcarded, and the above
   * fields should be ignored. If FALSE, the parameter is
   * specified, and the above fields should be valid.
   */
  guint any_direction : 1;
  guint any_state : 1;
  guint any_size : 1;
};

75 76 77 78 79 80
static gpointer parent_class = NULL;

static void gtk_icon_factory_init       (GtkIconFactory      *icon_factory);
static void gtk_icon_factory_class_init (GtkIconFactoryClass *klass);
static void gtk_icon_factory_finalize   (GObject             *object);
static void get_default_icons           (GtkIconFactory      *icon_factory);
81
static void icon_source_clear           (GtkIconSource       *source);
82

83 84 85 86
static GtkIconSize icon_size_register_intern (const gchar *name,
					      gint         width,
					      gint         height);

87 88 89 90 91
#define GTK_ICON_SOURCE_INIT(any_direction, any_state, any_size)	\
  { GTK_ICON_SOURCE_EMPTY, { NULL }, NULL,				\
   0, 0, 0,								\
   any_direction, any_state, any_size }

92 93 94
GType
gtk_icon_factory_get_type (void)
{
Manish Singh's avatar
Manish Singh committed
95
  static GType icon_factory_type = 0;
96

Manish Singh's avatar
Manish Singh committed
97
  if (!icon_factory_type)
98
    {
Manish Singh's avatar
Manish Singh committed
99
      static const GTypeInfo icon_factory_info =
100 101
      {
        sizeof (GtkIconFactoryClass),
Manish Singh's avatar
Manish Singh committed
102 103
        NULL,		/* base_init */
        NULL,		/* base_finalize */
104 105 106 107 108 109 110 111
        (GClassInitFunc) gtk_icon_factory_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GtkIconFactory),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_icon_factory_init,
      };
      
Manish Singh's avatar
Manish Singh committed
112 113 114
      icon_factory_type =
	g_type_register_static (G_TYPE_OBJECT, "GtkIconFactory",
				&icon_factory_info, 0);
115 116
    }
  
Manish Singh's avatar
Manish Singh committed
117
  return icon_factory_type;
118 119 120 121 122 123
}

static void
gtk_icon_factory_init (GtkIconFactory *factory)
{
  factory->icons = g_hash_table_new (g_str_hash, g_str_equal);
124
  all_icon_factories = g_slist_prepend (all_icon_factories, factory);
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
}

static void
gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gtk_icon_factory_finalize;
}

static void
free_icon_set (gpointer key, gpointer value, gpointer data)
{
  g_free (key);
  gtk_icon_set_unref (value);
}

static void
gtk_icon_factory_finalize (GObject *object)
{
  GtkIconFactory *factory = GTK_ICON_FACTORY (object);

149 150
  all_icon_factories = g_slist_remove (all_icon_factories, factory);
  
151 152 153 154 155 156 157
  g_hash_table_foreach (factory->icons, free_icon_set, NULL);
  
  g_hash_table_destroy (factory->icons);
  
  G_OBJECT_CLASS (parent_class)->finalize (object);
}

158 159 160 161
/**
 * gtk_icon_factory_new:
 *
 * Creates a new #GtkIconFactory. An icon factory manages a collection
162
 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
163 164 165
 * particular icon (i.e. a #GtkIconSet contains variants for different
 * sizes and widget states). Icons in an icon factory are named by a
 * stock ID, which is a simple string identifying the icon. Each
166
 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
167 168 169 170 171 172 173 174 175 176
 * theme; those icon factories are consulted first when searching for
 * an icon. If the theme doesn't set a particular icon, GTK+ looks for
 * the icon in a list of default icon factories, maintained by
 * gtk_icon_factory_add_default() and
 * gtk_icon_factory_remove_default(). Applications with icons should
 * add a default icon factory with their icons, which will allow
 * themes to override the icons for the application.
 * 
 * Return value: a new #GtkIconFactory
 **/
177 178 179
GtkIconFactory*
gtk_icon_factory_new (void)
{
Manish Singh's avatar
Manish Singh committed
180
  return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
181 182
}

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
/**
 * gtk_icon_factory_add:
 * @factory: a #GtkIconFactory
 * @stock_id: icon name
 * @icon_set: icon set
 *
 * Adds the given @icon_set to the icon factory, under the name
 * @stock_id.  @stock_id should be namespaced for your application,
 * e.g. "myapp-whatever-icon".  Normally applications create a
 * #GtkIconFactory, then add it to the list of default factories with
 * gtk_icon_factory_add_default(). Then they pass the @stock_id to
 * widgets such as #GtkImage to display the icon. Themes can provide
 * an icon with the same name (such as "myapp-whatever-icon") to
 * override your application's default icons. If an icon already
 * existed in @factory for @stock_id, it is unreferenced and replaced
 * with the new @icon_set.
 * 
 **/
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
void
gtk_icon_factory_add (GtkIconFactory *factory,
                      const gchar    *stock_id,
                      GtkIconSet     *icon_set)
{
  gpointer old_key = NULL;
  gpointer old_value = NULL;

  g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
  g_return_if_fail (stock_id != NULL);
  g_return_if_fail (icon_set != NULL);  

  g_hash_table_lookup_extended (factory->icons, stock_id,
                                &old_key, &old_value);

  if (old_value == icon_set)
    return;
  
  gtk_icon_set_ref (icon_set);

  /* GHashTable key memory management is so fantastically broken. */
  if (old_key)
    g_hash_table_insert (factory->icons, old_key, icon_set);
  else
    g_hash_table_insert (factory->icons, g_strdup (stock_id), icon_set);

  if (old_value)
    gtk_icon_set_unref (old_value);
}

231 232 233 234 235 236 237 238 239 240 241 242 243
/**
 * gtk_icon_factory_lookup:
 * @factory: a #GtkIconFactory
 * @stock_id: an icon name
 * 
 * Looks up @stock_id in the icon factory, returning an icon set
 * if found, otherwise %NULL. For display to the user, you should
 * use gtk_style_lookup_icon_set() on the #GtkStyle for the
 * widget that will display the icon, instead of using this
 * function directly, so that themes are taken into account.
 * 
 * Return value: icon set of @stock_id.
 **/
244 245 246 247 248 249 250 251 252 253 254 255 256
GtkIconSet *
gtk_icon_factory_lookup (GtkIconFactory *factory,
                         const gchar    *stock_id)
{
  g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
  g_return_val_if_fail (stock_id != NULL, NULL);
  
  return g_hash_table_lookup (factory->icons, stock_id);
}

static GtkIconFactory *gtk_default_icons = NULL;
static GSList *default_factories = NULL;

257 258 259 260 261 262 263 264 265 266 267 268
/**
 * gtk_icon_factory_add_default:
 * @factory: a #GtkIconFactory
 * 
 * Adds an icon factory to the list of icon factories searched by
 * gtk_style_lookup_icon_set(). This means that, for example,
 * gtk_image_new_from_stock() will be able to find icons in @factory.
 * There will normally be an icon factory added for each library or
 * application that comes with icons. The default icon factories
 * can be overridden by themes.
 * 
 **/
269 270 271 272 273
void
gtk_icon_factory_add_default (GtkIconFactory *factory)
{
  g_return_if_fail (GTK_IS_ICON_FACTORY (factory));

Manish Singh's avatar
Manish Singh committed
274
  g_object_ref (factory);
275 276 277 278
  
  default_factories = g_slist_prepend (default_factories, factory);
}

279 280 281 282 283 284 285 286 287
/**
 * gtk_icon_factory_remove_default:
 * @factory: a #GtkIconFactory previously added with gtk_icon_factory_add_default()
 *
 * Removes an icon factory from the list of default icon
 * factories. Not normally used; you might use it for a library that
 * can be unloaded or shut down.
 * 
 **/
288 289 290 291 292 293 294
void
gtk_icon_factory_remove_default (GtkIconFactory  *factory)
{
  g_return_if_fail (GTK_IS_ICON_FACTORY (factory));

  default_factories = g_slist_remove (default_factories, factory);

Manish Singh's avatar
Manish Singh committed
295
  g_object_unref (factory);
296 297
}

298 299 300 301 302 303 304 305 306 307 308
static void
ensure_default_icons (void)
{
  if (gtk_default_icons == NULL)
    {
      gtk_default_icons = gtk_icon_factory_new ();

      get_default_icons (gtk_default_icons);
    }
}

309 310 311 312 313 314 315 316 317 318 319 320 321
/**
 * gtk_icon_factory_lookup_default:
 * @stock_id: an icon name
 *
 * Looks for an icon in the list of default icon factories.  For
 * display to the user, you should use gtk_style_lookup_icon_set() on
 * the #GtkStyle for the widget that will display the icon, instead of
 * using this function directly, so that themes are taken into
 * account.
 * 
 * 
 * Return value: a #GtkIconSet, or %NULL
 **/
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
GtkIconSet *
gtk_icon_factory_lookup_default (const gchar *stock_id)
{
  GSList *tmp_list;

  g_return_val_if_fail (stock_id != NULL, NULL);
  
  tmp_list = default_factories;
  while (tmp_list != NULL)
    {
      GtkIconSet *icon_set =
        gtk_icon_factory_lookup (GTK_ICON_FACTORY (tmp_list->data),
                                 stock_id);

      if (icon_set)
        return icon_set;
338
      
339 340 341
      tmp_list = g_slist_next (tmp_list);
    }

342
  ensure_default_icons ();
343 344 345 346
  
  return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
}

347
static void
348 349
register_stock_icon (GtkIconFactory *factory,
		     const gchar    *stock_id)
350
{
351 352
  GtkIconSet *set = gtk_icon_set_new ();
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
353

354 355 356
  source.type = GTK_ICON_SOURCE_ICON_NAME;
  source.source.icon_name = (gchar *)stock_id;
  gtk_icon_set_add_source (set, &source);
357
  
358 359
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
360
}
361

362 363 364 365 366
static void
register_bidi_stock_icon (GtkIconFactory *factory,
			  const gchar    *stock_id,
			  const gchar    *stock_id_ltr,
			  const gchar    *stock_id_rtl)
367
{
368 369
  GtkIconSet *set = gtk_icon_set_new ();
  GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
370

371 372
  source.type = GTK_ICON_SOURCE_ICON_NAME;
  source.source.icon_name = (gchar *)stock_id_ltr;
373
  source.direction = GTK_TEXT_DIR_LTR;
374
  gtk_icon_set_add_source (set, &source);
375
  
376 377 378 379
  source.type = GTK_ICON_SOURCE_ICON_NAME;
  source.source.icon_name = (gchar *)stock_id_rtl;
  source.direction = GTK_TEXT_DIR_RTL;
  gtk_icon_set_add_source (set, &source);
380
  
381 382
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
383 384
}

385 386 387 388
static void
add_default_image (const gchar  *stock_id,
		   gint          size,
		   const guchar *inline_data)
389
{
390 391
  GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, inline_data, FALSE, NULL);
  g_assert (pixbuf);
392

393
  gtk_icon_theme_add_builtin_icon (stock_id, size, pixbuf);
394
  
395
  g_object_unref (pixbuf);
396 397 398
}

static void
399 400 401 402
add_icon (GtkIconFactory *factory,
	  const gchar    *stock_id,
	  gint            size,
	  const guchar   *inline_data)
403
{
404
  register_stock_icon (factory, stock_id);
405

406
  add_default_image (stock_id, size, inline_data);
407 408
}

409
static void
410 411 412 413 414 415
add_icon2 (GtkIconFactory *factory,
	   const gchar    *stock_id,
	   gint            size1,
	   const guchar   *inline_data1,
	   gint            size2,
	   const guchar   *inline_data2)
416
{
417
  register_stock_icon (factory, stock_id);
418
  
419 420
  add_default_image (stock_id, size1, inline_data1);
  add_default_image (stock_id, size2, inline_data2);
421 422
}

423
static void
424 425 426 427 428 429 430 431
add_icon_bidi2 (GtkIconFactory *factory,
		const gchar    *stock_id,
		gint            size1,
		const guchar   *inline_data_ltr1,
		const guchar   *inline_data_rtl1,
		gint            size2,
		const guchar   *inline_data_ltr2,
		const guchar   *inline_data_rtl2)
432
{
433 434
  gchar *stock_id_ltr = g_strconcat (stock_id, "-ltr", NULL);
  gchar *stock_id_rtl = g_strconcat (stock_id, "-rtl", NULL);
435
  
436 437
  register_bidi_stock_icon (factory, stock_id,
			    stock_id_ltr, stock_id_rtl);
438
  
439 440
  add_default_image (stock_id_ltr, size1, inline_data_ltr1);
  add_default_image (stock_id_ltr, size2, inline_data_ltr2);
441

442 443 444 445 446
  add_default_image (stock_id_rtl, size1, inline_data_rtl1);
  add_default_image (stock_id_rtl, size2, inline_data_rtl2);
  
  g_free (stock_id_ltr);
  g_free (stock_id_rtl);
447 448 449 450 451 452
}

static void
get_default_icons (GtkIconFactory *factory)
{
  /* KEEP IN SYNC with gtkstock.c */
453 454

  /* Have dialog size */
Matthias Clasen's avatar
Matthias Clasen committed
455
  add_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, 48, stock_dialog_authentication_48);
456 457 458 459
  add_icon (factory, GTK_STOCK_DIALOG_ERROR, 48, stock_dialog_error_48);
  add_icon (factory, GTK_STOCK_DIALOG_INFO, 48, stock_dialog_info_48);
  add_icon (factory, GTK_STOCK_DIALOG_QUESTION, 48, stock_dialog_question_48);
  add_icon (factory, GTK_STOCK_DIALOG_WARNING, 48, stock_dialog_warning_48);
460
  
461
  /* Have dnd size */
462 463
  add_icon (factory, GTK_STOCK_DND, 32, stock_dnd_32);
  add_icon (factory, GTK_STOCK_DND_MULTIPLE, 32, stock_dnd_multiple_32);
464
  
465
  /* Have button sizes */
466 467 468 469 470
  add_icon (factory, GTK_STOCK_APPLY, 20, stock_apply_20);
  add_icon (factory, GTK_STOCK_CANCEL, 20, stock_cancel_20);
  add_icon (factory, GTK_STOCK_NO, 20, stock_no_20);
  add_icon (factory, GTK_STOCK_OK, 20, stock_ok_20);
  add_icon (factory, GTK_STOCK_YES, 20, stock_yes_20);
471 472

  /* Generic + button sizes */
473 474 475
  add_icon2 (factory, GTK_STOCK_CLOSE,
	     20, stock_close_20,
	     24, stock_close_24);
476 477

  /* Generic + menu sizes */  
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 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
  add_icon2 (factory, GTK_STOCK_ADD,
	     16, stock_add_16,
	     24, stock_add_24);

  add_icon2 (factory, GTK_STOCK_JUSTIFY_CENTER,
	     16, stock_align_center_16,
	     24, stock_align_center_24);

  add_icon2 (factory, GTK_STOCK_JUSTIFY_FILL,
	     16, stock_align_justify_16,
	     24, stock_align_justify_24);

  add_icon2 (factory, GTK_STOCK_JUSTIFY_LEFT,
	     16, stock_align_left_16,
	     24, stock_align_left_24);
	     
  add_icon2 (factory, GTK_STOCK_JUSTIFY_RIGHT,
	     16, stock_align_right_16,
	     24, stock_align_right_24);

  add_icon2 (factory, GTK_STOCK_GOTO_BOTTOM,
	     16, stock_bottom_16,
	     24, stock_bottom_24);
	     
  add_icon2 (factory, GTK_STOCK_CDROM,
	     16, stock_cdrom_16,
	     24, stock_cdrom_24);

  add_icon2 (factory, GTK_STOCK_CONVERT,
	     16, stock_convert_16,
	     24, stock_convert_24);

  add_icon2 (factory, GTK_STOCK_COPY,
	     16, stock_copy_16,
	     24, stock_copy_24);

  add_icon2 (factory, GTK_STOCK_CUT,
	     16, stock_cut_16,
	     24, stock_cut_24);

  add_icon2 (factory, GTK_STOCK_GO_DOWN,
	     16, stock_down_arrow_16,
	     24, stock_down_arrow_24);

  add_icon2 (factory, GTK_STOCK_EXECUTE,
	     16, stock_exec_16,
	     24, stock_exec_24);

  add_icon2 (factory, GTK_STOCK_QUIT,
	     16, stock_exit_16,
	     24, stock_exit_24);

  add_icon_bidi2 (factory, GTK_STOCK_GOTO_FIRST,
		  16, stock_first_16, stock_last_16,
		  24, stock_first_24, stock_last_24);

  add_icon2 (factory, GTK_STOCK_SELECT_FONT,
	     16, stock_font_16,
	     24, stock_font_24);

538 539 540 541
  add_icon2 (factory, GTK_STOCK_HARDDISK,
	     16, stock_harddisk_16,
	     24, stock_harddisk_24);

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
  add_icon2 (factory, GTK_STOCK_HELP,
	     16, stock_help_16,
	     24, stock_help_24);

  add_icon2 (factory, GTK_STOCK_HOME,
	     16, stock_home_16,
	     24, stock_home_24);

  add_icon_bidi2 (factory, GTK_STOCK_JUMP_TO,
		  16, stock_jump_to_16, stock_jump_to_rtl_16,
		  24, stock_jump_to_24, stock_jump_to_rtl_24);

  add_icon_bidi2 (factory, GTK_STOCK_GOTO_LAST,
		  16, stock_last_16, stock_first_16,
		  24, stock_last_24, stock_first_24);

  add_icon_bidi2 (factory, GTK_STOCK_GO_BACK,
		  16, stock_left_arrow_16, stock_right_arrow_16,
		  24, stock_left_arrow_24, stock_right_arrow_24);

  add_icon2 (factory, GTK_STOCK_MISSING_IMAGE,
	     16, stock_missing_image_16,
	     24, stock_missing_image_24);

566 567 568 569
  add_icon2 (factory, GTK_STOCK_NETWORK,
	     16, stock_network_16,
	     24, stock_network_24);

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
  add_icon2 (factory, GTK_STOCK_NEW,
	     16, stock_new_16,
	     24, stock_new_24);

  add_icon2 (factory, GTK_STOCK_OPEN,
	     16, stock_open_16,
	     24, stock_open_24);

  add_icon2 (factory, GTK_STOCK_PASTE,
	     16, stock_paste_16,
	     24, stock_paste_24);

  add_icon2 (factory, GTK_STOCK_PREFERENCES,
	     16, stock_preferences_16,
	     24, stock_preferences_24);

  add_icon2 (factory, GTK_STOCK_PRINT,
	     16, stock_print_16,
	     24, stock_print_24);

  add_icon2 (factory, GTK_STOCK_PRINT_PREVIEW,
	     16, stock_print_preview_16,
	     24, stock_print_preview_24);

  add_icon2 (factory, GTK_STOCK_PROPERTIES,
	     16, stock_properties_16,
	     24, stock_properties_24);
  
  add_icon_bidi2 (factory, GTK_STOCK_REDO,
		  16, stock_redo_16, stock_redo_rtl_16,
		  24, stock_redo_24, stock_redo_rtl_24);

  add_icon2 (factory, GTK_STOCK_REMOVE,
	     16, stock_remove_16,
	     24, stock_remove_24);

  add_icon2 (factory, GTK_STOCK_REFRESH,
	     16, stock_refresh_16,
	     24, stock_refresh_24);

  add_icon_bidi2 (factory, GTK_STOCK_REVERT_TO_SAVED,
		  16, stock_revert_16, stock_revert_rtl_16,
		  24, stock_revert_24, stock_revert_rtl_24);

  add_icon_bidi2 (factory, GTK_STOCK_GO_FORWARD,
		  16, stock_right_arrow_16, stock_left_arrow_16,
		  24, stock_right_arrow_24, stock_left_arrow_24);

  add_icon2 (factory, GTK_STOCK_SAVE,
	     16, stock_save_16,
	     24, stock_save_24);

  add_icon2 (factory, GTK_STOCK_FLOPPY,
	     16, stock_save_16,
	     24, stock_save_24);

  add_icon2 (factory, GTK_STOCK_SAVE_AS,
	     16, stock_save_as_16,
	     24, stock_save_as_24);

  add_icon2 (factory, GTK_STOCK_FIND,
	     16, stock_search_16,
	     24, stock_search_24);

  add_icon2 (factory, GTK_STOCK_FIND_AND_REPLACE,
	     16, stock_search_replace_16,
	     24, stock_search_replace_24);

  add_icon2 (factory, GTK_STOCK_SORT_DESCENDING,
	     16, stock_sort_descending_16,
	     24, stock_sort_descending_24);

  add_icon2 (factory, GTK_STOCK_SORT_ASCENDING,
	     16, stock_sort_ascending_16,
	     24, stock_sort_ascending_24);

  add_icon2 (factory, GTK_STOCK_SPELL_CHECK,
	     16, stock_spellcheck_16,
	     24, stock_spellcheck_24);

  add_icon2 (factory, GTK_STOCK_STOP,
	     16, stock_stop_16,
	     24, stock_stop_24);

  add_icon2 (factory, GTK_STOCK_BOLD,
	     16, stock_text_bold_16,
	     24, stock_text_bold_24);

  add_icon2 (factory, GTK_STOCK_ITALIC,
	     16, stock_text_italic_16,
	     24, stock_text_italic_24);

  add_icon2 (factory, GTK_STOCK_STRIKETHROUGH,
	     16, stock_text_strikethrough_16,
	     24, stock_text_strikethrough_24);

  add_icon2 (factory, GTK_STOCK_UNDERLINE,
	     16, stock_text_underline_16,
	     24, stock_text_underline_24);

670 671 672 673 674 675 676 677
  add_icon2 (factory, GTK_STOCK_INDENT,
	     16, stock_text_indent_16,
	     24, stock_text_indent_24);

  add_icon2 (factory, GTK_STOCK_UNINDENT,
	     16, stock_text_unindent_16,
	     24, stock_text_unindent_24);

678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
  add_icon2 (factory, GTK_STOCK_GOTO_TOP,
	     16, stock_top_16,
	     24, stock_top_24);

  add_icon2 (factory, GTK_STOCK_DELETE,
	     16, stock_trash_16,
	     24, stock_trash_24);

  add_icon_bidi2 (factory, GTK_STOCK_UNDELETE,
		  16, stock_undelete_16, stock_undelete_rtl_16,
		  24, stock_undelete_24, stock_undelete_rtl_24);

  add_icon_bidi2 (factory, GTK_STOCK_UNDO,
		  16, stock_undo_16, stock_undo_rtl_16,
		  24, stock_undo_24, stock_undo_rtl_24);

  add_icon2 (factory, GTK_STOCK_GO_UP,
	     16, stock_up_arrow_16,
	     24, stock_up_arrow_24);

698 699 700 701 702 703 704 705
  add_icon2 (factory, GTK_STOCK_FILE,
	     16, stock_file_16,
	     24, stock_file_24);

  add_icon2 (factory, GTK_STOCK_DIRECTORY,
	     16, stock_directory_16,
	     24, stock_directory_24);

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
  add_icon2 (factory, GTK_STOCK_ABOUT,
	     16, stock_about_16,
	     24, stock_about_24);

  add_icon2 (factory, GTK_STOCK_CONNECT,
	     16, stock_connect_16,
	     24, stock_connect_24);

  add_icon2 (factory, GTK_STOCK_DISCONNECT,
	     16, stock_disconnect_16,
	     24, stock_disconnect_24);

  add_icon2 (factory, GTK_STOCK_EDIT,
	     16, stock_edit_16,
	     24, stock_edit_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_FORWARD,
	     16, stock_media_forward_16,
	     24, stock_media_forward_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_NEXT,
	     16, stock_media_next_16,
	     24, stock_media_next_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_PAUSE,
	     16, stock_media_pause_16,
	     24, stock_media_pause_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_PLAY,
	     16, stock_media_play_16,
	     24, stock_media_play_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_PREVIOUS,
	     16, stock_media_previous_16,
	     24, stock_media_previous_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_RECORD,
	     16, stock_media_record_16,
	     24, stock_media_record_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_REWIND,
	     16, stock_media_rewind_16,
	     24, stock_media_rewind_24);

  add_icon2 (factory, GTK_STOCK_MEDIA_STOP,
	     16, stock_media_stop_16,
	     24, stock_media_stop_24);

754 755 756 757 758 759 760 761 762 763
  /* Generic size only */

  add_icon (factory, GTK_STOCK_CLEAR, 24, stock_clear_24);
  add_icon (factory, GTK_STOCK_SELECT_COLOR, 24, stock_colorselector_24);
  add_icon (factory, GTK_STOCK_COLOR_PICKER, 25, stock_color_picker_25);
  add_icon (factory, GTK_STOCK_INDEX, 24, stock_index_24);
  add_icon (factory, GTK_STOCK_ZOOM_100, 24, stock_zoom_1_24);
  add_icon (factory, GTK_STOCK_ZOOM_FIT, 24, stock_zoom_fit_24);
  add_icon (factory, GTK_STOCK_ZOOM_IN, 24, stock_zoom_in_24);
  add_icon (factory, GTK_STOCK_ZOOM_OUT, 24, stock_zoom_out_24);
764 765
}

766 767 768
/************************************************************
 *                    Icon size handling                    *
 ************************************************************/
769 770 771 772 773

typedef struct _IconSize IconSize;

struct _IconSize
{
774
  gint size;
775 776
  gchar *name;
  
777 778
  gint width;
  gint height;
779 780
};

781
typedef struct _IconAlias IconAlias;
782

783
struct _IconAlias
784
{
785 786 787
  gchar *name;
  gint   target;
};
788

789 790 791 792 793 794 795 796
typedef struct _SettingsIconSize SettingsIconSize;

struct _SettingsIconSize
{
  gint width;
  gint height;
};

797 798 799 800
static GHashTable *icon_aliases = NULL;
static IconSize *icon_sizes = NULL;
static gint      icon_sizes_allocated = 0;
static gint      icon_sizes_used = 0;
801 802

static void
803
init_icon_sizes (void)
804
{
805
  if (icon_sizes == NULL)
806
    {
807
#define NUM_BUILTIN_SIZES 7
808
      gint i;
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
      icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
      
      icon_sizes = g_new (IconSize, NUM_BUILTIN_SIZES);
      icon_sizes_allocated = NUM_BUILTIN_SIZES;
      icon_sizes_used = NUM_BUILTIN_SIZES;

      icon_sizes[GTK_ICON_SIZE_INVALID].size = 0;
      icon_sizes[GTK_ICON_SIZE_INVALID].name = NULL;
      icon_sizes[GTK_ICON_SIZE_INVALID].width = 0;
      icon_sizes[GTK_ICON_SIZE_INVALID].height = 0;

      /* the name strings aren't copied since we don't ever remove
       * icon sizes, so we don't need to know whether they're static.
       * Even if we did I suppose removing the builtin sizes would be
       * disallowed.
       */
      
      icon_sizes[GTK_ICON_SIZE_MENU].size = GTK_ICON_SIZE_MENU;
      icon_sizes[GTK_ICON_SIZE_MENU].name = "gtk-menu";
      icon_sizes[GTK_ICON_SIZE_MENU].width = 16;
      icon_sizes[GTK_ICON_SIZE_MENU].height = 16;

      icon_sizes[GTK_ICON_SIZE_BUTTON].size = GTK_ICON_SIZE_BUTTON;
      icon_sizes[GTK_ICON_SIZE_BUTTON].name = "gtk-button";
Anders Carlsson's avatar
Anders Carlsson committed
834 835
      icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
      icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
836 837 838 839 840 841 842 843 844 845

      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].size = GTK_ICON_SIZE_SMALL_TOOLBAR;
      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].name = "gtk-small-toolbar";
      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].width = 18;
      icon_sizes[GTK_ICON_SIZE_SMALL_TOOLBAR].height = 18;
      
      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].size = GTK_ICON_SIZE_LARGE_TOOLBAR;
      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].name = "gtk-large-toolbar";
      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].width = 24;
      icon_sizes[GTK_ICON_SIZE_LARGE_TOOLBAR].height = 24;
846

847 848 849 850 851
      icon_sizes[GTK_ICON_SIZE_DND].size = GTK_ICON_SIZE_DND;
      icon_sizes[GTK_ICON_SIZE_DND].name = "gtk-dnd";
      icon_sizes[GTK_ICON_SIZE_DND].width = 32;
      icon_sizes[GTK_ICON_SIZE_DND].height = 32;

852 853 854 855
      icon_sizes[GTK_ICON_SIZE_DIALOG].size = GTK_ICON_SIZE_DIALOG;
      icon_sizes[GTK_ICON_SIZE_DIALOG].name = "gtk-dialog";
      icon_sizes[GTK_ICON_SIZE_DIALOG].width = 48;
      icon_sizes[GTK_ICON_SIZE_DIALOG].height = 48;
856

857
      g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
858

859 860 861 862 863 864 865 866 867 868
      /* Alias everything to itself. */
      i = 1; /* skip invalid size */
      while (i < NUM_BUILTIN_SIZES)
        {
          gtk_icon_size_register_alias (icon_sizes[i].name, icon_sizes[i].size);
          
          ++i;
        }
      
#undef NUM_BUILTIN_SIZES
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 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 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 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
static void
free_settings_sizes (gpointer data)
{
  g_array_free (data, TRUE);
}

static GArray *
get_settings_sizes (GtkSettings *settings,
		    gboolean    *created)
{
  GArray *settings_sizes;
  static GQuark sizes_quark = 0;

  if (!sizes_quark)
    sizes_quark = g_quark_from_static_string ("gtk-icon-sizes");

  settings_sizes = g_object_get_qdata (G_OBJECT (settings), sizes_quark);
  if (!settings_sizes)
    {
      settings_sizes = g_array_new (FALSE, FALSE, sizeof (SettingsIconSize));
      g_object_set_qdata_full (G_OBJECT (settings), sizes_quark,
			       settings_sizes, free_settings_sizes);
      if (created)
	*created = TRUE;
    }

  return settings_sizes;
}

static void
icon_size_set_for_settings (GtkSettings *settings,
			    const gchar *size_name,
			    gint         width,
			    gint         height)
{
  GtkIconSize size;
  GArray *settings_sizes;
  SettingsIconSize *settings_size;

  g_return_if_fail (size_name != NULL);

  size = gtk_icon_size_from_name (size_name);
  if (size == GTK_ICON_SIZE_INVALID)
    /* Reserve a place */
    size = icon_size_register_intern (size_name, -1, -1);
  
  settings_sizes = get_settings_sizes (settings, NULL);
  if (size >= settings_sizes->len)
    {
      SettingsIconSize unset = { -1, -1 };
      gint i;

      for (i = settings_sizes->len; i <= size; i++)
	g_array_append_val (settings_sizes, unset);
    }

  settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
  
  settings_size->width = width;
  settings_size->height = height;
}

/* Like pango_parse_word, but accept - as well
 */
static gboolean
scan_icon_size_name (const char **pos, GString *out)
{
  const char *p = *pos;

  while (g_ascii_isspace (*p))
    p++;
  
  if (!((*p >= 'A' && *p <= 'Z') ||
	(*p >= 'a' && *p <= 'z') ||
	*p == '_' || *p == '-'))
    return FALSE;

  g_string_truncate (out, 0);
  g_string_append_c (out, *p);
  p++;

  while ((*p >= 'A' && *p <= 'Z') ||
	 (*p >= 'a' && *p <= 'z') ||
	 (*p >= '0' && *p <= '9') ||
	 *p == '_' || *p == '-')
    {
      g_string_append_c (out, *p);
      p++;
    }

  *pos = p;

  return TRUE;
}

static void
icon_size_setting_parse (GtkSettings *settings,
			 const gchar *icon_size_string)
{
  GString *name_buf = g_string_new (NULL);
  const gchar *p = icon_size_string;

  while (pango_skip_space (&p))
    {
      gint width, height;
      
      if (!scan_icon_size_name (&p, name_buf))
	goto err;

      if (!pango_skip_space (&p))
	goto err;

      if (*p != '=')
	goto err;

      p++;

      if (!pango_scan_int (&p, &width))
	goto err;

      if (!pango_skip_space (&p))
	goto err;

      if (*p != ',')
	goto err;

      p++;

      if (!pango_scan_int (&p, &height))
	goto err;

      if (width > 0 && height > 0)
	{
	  icon_size_set_for_settings (settings, name_buf->str,
				      width, height);
	}
      else
	{
	  g_warning ("Invalid size in gtk-icon-sizes: %d,%d\n", width, height);
	}

      pango_skip_space (&p);
      if (*p == '\0')
	break;
      if (*p == ':')
	p++;
      else
	goto err;
    }

  g_string_free (name_buf, TRUE);
  return;

 err:
  g_warning ("Error parsing gtk-icon-sizes string:\n\t'%s'", icon_size_string);
  g_string_free (name_buf, TRUE);
}

static void
icon_size_set_all_from_settings (GtkSettings *settings)
{
  GArray *settings_sizes;
  gchar *icon_size_string;

  /* Reset old settings */
  settings_sizes = get_settings_sizes (settings, NULL);
  g_array_set_size (settings_sizes, 0);

  g_object_get (settings,
		"gtk-icon-sizes", &icon_size_string,
		NULL);

  if (icon_size_string)
    {
      icon_size_setting_parse (settings, icon_size_string);
      g_free (icon_size_string);
    }
}

static void
icon_size_settings_changed (GtkSettings  *settings,
			    GParamSpec   *pspec)
{
  icon_size_set_all_from_settings (settings);

1057
  gtk_rc_reset_styles (settings);
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
}

static void
icon_sizes_init_for_settings (GtkSettings *settings)
{
  g_signal_connect (settings,
		    "notify::gtk-icon-sizes",
		    G_CALLBACK (icon_size_settings_changed),
		    NULL);
  
  icon_size_set_all_from_settings (settings);
}
     
1071
static gboolean
1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
icon_size_lookup_intern (GtkSettings *settings,
			 GtkIconSize  size,
			 gint        *widthp,
			 gint        *heightp)
{
  GArray *settings_sizes;
  gint width_for_settings = -1;
  gint height_for_settings = -1;
  
  init_icon_sizes ();

1083
  if (size >= icon_sizes_used)
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
    return FALSE;

  if (size == GTK_ICON_SIZE_INVALID)
    return FALSE;

  if (settings)
    {
      gboolean initial = FALSE;
      
      settings_sizes = get_settings_sizes (settings, &initial);
      if (initial)
	icon_sizes_init_for_settings (settings);
  
      if (size < settings_sizes->len)
	{
	  SettingsIconSize *settings_size;
	  
	  settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
	  
	  width_for_settings = settings_size->width;
	  height_for_settings = settings_size->height;
	}
    }

  if (widthp)
    *widthp = width_for_settings >= 0 ? width_for_settings : icon_sizes[size].width;

  if (heightp)
    *heightp = height_for_settings >= 0 ? height_for_settings : icon_sizes[size].height;

  return TRUE;
}

/**
 * gtk_icon_size_lookup_for_settings:
1119
 * @settings: a #GtkSettings object, used to determine
1120 1121 1122 1123 1124 1125
 *   which set of user preferences to used.
 * @size: an icon size
 * @width: location to store icon width
 * @height: location to store icon height
 *
 * Obtains the pixel size of a semantic icon size, possibly
Matthias Clasen's avatar
Matthias Clasen committed
1126 1127
 * modified by user preferences for a particular 
 * #GtkSettings. Normally @size would be
1128 1129 1130 1131 1132 1133 1134 1135 1136
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
 * isn't normally needed, gtk_widget_render_icon() is the usual
 * way to get an icon for rendering, then just look at the size of
 * the rendered pixbuf. The rendered pixbuf may not even correspond to
 * the width/height returned by gtk_icon_size_lookup(), because themes
 * are free to render the pixbuf however they like, including changing
 * the usual size.
 * 
 * Return value: %TRUE if @size was a valid size
1137 1138
 *
 * Since: 2.2
1139 1140 1141 1142
 **/
gboolean
gtk_icon_size_lookup_for_settings (GtkSettings *settings,
				   GtkIconSize  size,
1143 1144
				   gint        *width,
				   gint        *height)
1145 1146 1147
{
  g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);

1148
  return icon_size_lookup_intern (settings, size, width, height);
1149 1150
}

1151 1152
/**
 * gtk_icon_size_lookup:
1153
 * @size: an icon size
1154 1155 1156
 * @width: location to store icon width
 * @height: location to store icon height
 *
1157 1158 1159 1160
 * Obtains the pixel size of a semantic icon size, possibly
 * modified by user preferences for the default #GtkSettings.
 * (See gtk_icon_size_lookup_for_settings().)
 * Normally @size would be
1161 1162 1163 1164 1165
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
 * isn't normally needed, gtk_widget_render_icon() is the usual
 * way to get an icon for rendering, then just look at the size of
 * the rendered pixbuf. The rendered pixbuf may not even correspond to
 * the width/height returned by gtk_icon_size_lookup(), because themes
1166 1167
 * are free to render the pixbuf however they like, including changing
 * the usual size.
1168
 * 
1169
 * Return value: %TRUE if @size was a valid size
1170
 **/
1171
gboolean
1172
gtk_icon_size_lookup (GtkIconSize  size,
1173 1174 1175
                      gint        *widthp,
                      gint        *heightp)
{
1176 1177
  GTK_NOTE (MULTIHEAD,
	    g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1178

1179 1180 1181
  return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
					    size, widthp, heightp);
}
1182

1183 1184 1185 1186 1187 1188 1189
static GtkIconSize
icon_size_register_intern (const gchar *name,
			   gint         width,
			   gint         height)
{
  IconAlias *old_alias;
  GtkIconSize size;
1190
  
1191
  init_icon_sizes ();
1192

1193 1194 1195 1196 1197 1198
  old_alias = g_hash_table_lookup (icon_aliases, name);
  if (old_alias && icon_sizes[old_alias->target].width > 0)
    {
      g_warning ("Icon size name '%s' already exists", name);
      return GTK_ICON_SIZE_INVALID;
    }
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
  if (old_alias)
    {
      size = old_alias->target;
    }
  else
    {
      if (icon_sizes_used == icon_sizes_allocated)
	{
	  icon_sizes_allocated *= 2;
	  icon_sizes = g_renew (IconSize, icon_sizes, icon_sizes_allocated);
	}

      size = icon_sizes_used++;

      /* alias to self. */
      gtk_icon_size_register_alias (name, size);

      icon_sizes[size].size = size;
      icon_sizes[size].name = g_strdup (name);
    }

  icon_sizes[size].width = width;
  icon_sizes[size].height = height;

  return size;
1225 1226
}

1227 1228
/**
 * gtk_icon_size_register:
1229
 * @name: name of the icon size
1230 1231 1232 1233
 * @width: the icon width
 * @height: the icon height
 *
 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1234 1235 1236
 * etc. Returns the integer value for the size.
 *
 * Returns: integer value representing the size
1237 1238
 * 
 **/
1239 1240
GtkIconSize
gtk_icon_size_register (const gchar *name,
1241 1242 1243
                        gint         width,
                        gint         height)
{
1244 1245 1246
  g_return_val_if_fail (name != NULL, 0);
  g_return_val_if_fail (width > 0, 0);
  g_return_val_if_fail (height > 0, 0);
1247
  
1248
  return icon_size_register_intern (name, width, height);
1249 1250
}

1251 1252 1253
/**
 * gtk_icon_size_register_alias:
 * @alias: an alias for @target
1254 1255 1256 1257 1258
 * @target: an existing icon size
 *
 * Registers @alias as another name for @target.
 * So calling gtk_icon_size_from_name() with @alias as argument
 * will return @target.
1259 1260
 *
 **/
1261 1262
void
gtk_icon_size_register_alias (const gchar *alias,
1263
                              GtkIconSize  target)
1264
{
1265 1266
  IconAlias *ia;
  
1267 1268 1269 1270
  g_return_if_fail (alias != NULL);

  init_icon_sizes ();

1271
  if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1272 1273
    g_warning ("gtk_icon_size_register_alias: Icon size %d does not exist", target);

1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293
  ia = g_hash_table_lookup (icon_aliases, alias);
  if (ia)
    {
      if (icon_sizes[ia->target].width > 0)
	{
	  g_warning ("gtk_icon_size_register_alias: Icon size name '%s' already exists", alias);
	  return;
	}

      ia->target = target;
    }

  if (!ia)
    {
      ia = g_new (IconAlias, 1);
      ia->name = g_strdup (alias);
      ia->target = target;

      g_hash_table_insert (icon_aliases, ia->name, ia);
    }
1294 1295
}

Matthias Clasen's avatar
Matthias Clasen committed
1296 1297 1298 1299 1300 1301 1302
/** 
 * gtk_icon_size_from_name:
 * @name: the name to look up.
 * @returns: the icon size with the given name.
 * 
 * Looks up the icon size associated with @name.
 **/
1303 1304 1305 1306 1307 1308 1309 1310 1311
GtkIconSize
gtk_icon_size_from_name (const gchar *name)
{
  IconAlias *ia;

  init_icon_sizes ();
  
  ia = g_hash_table_lookup (icon_aliases, name);

1312
  if (ia && icon_sizes[ia->target].width > 0)
1313 1314 1315 1316 1317
    return ia->target;
  else
    return GTK_ICON_SIZE_INVALID;
}

Matthias Clasen's avatar
Matthias Clasen committed
1318 1319 1320 1321 1322 1323 1324 1325
/**
 * gtk_icon_size_get_name:
 * @size: a #GtkIconSize.
 * @returns: the name of the given icon size.
 * 
 * Gets the canonical name of the given icon size. The returned string 
 * is statically allocated and should not be freed.
 **/
1326 1327 1328 1329 1330 1331 1332
G_CONST_RETURN gchar*
gtk_icon_size_get_name (GtkIconSize  size)
{
  if (size >= icon_sizes_used)
    return NULL;
  else
    return icon_sizes[size].name;
1333 1334
}

1335 1336
/************************************************************/

1337 1338 1339 1340 1341 1342 1343
/* Icon Set */


static GdkPixbuf *find_in_cache     (GtkIconSet       *icon_set,
                                     GtkStyle         *style,
                                     GtkTextDirection  direction,
                                     GtkStateType      state,
1344
                                     GtkIconSize       size);
1345 1346 1347 1348
static void       add_to_cache      (GtkIconSet       *icon_set,
                                     GtkStyle         *style,
                                     GtkTextDirection  direction,
                                     GtkStateType      state,
1349
                                     GtkIconSize       size,
1350
                                     GdkPixbuf        *pixbuf);
1351 1352 1353 1354
/* Clear icon set contents, drop references to all contained
 * GdkPixbuf objects and forget all GtkIconSources. Used to
 * recycle an icon set.
 */
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
static void       clear_cache       (GtkIconSet       *icon_set,
                                     gboolean          style_detach);
static GSList*    copy_cache        (GtkIconSet       *icon_set,
                                     GtkIconSet       *copy_recipient);
static void       attach_to_style   (GtkIconSet       *icon_set,
                                     GtkStyle         *style);
static void       detach_from_style (GtkIconSet       *icon_set,
                                     GtkStyle         *style);
static void       style_dnotify     (gpointer          data);

struct _GtkIconSet
{
  guint ref_count;

  GSList *sources;

  /* Cache of the last few rendered versions of the icon. */
  GSList *cache;

  guint cache_size;

  guint cache_serial;
};

static guint cache_serial = 0;

1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
/**
 * gtk_icon_set_new:
 * 
 * Creates a new #GtkIconSet. A #GtkIconSet represents a single icon
 * in various sizes and widget states. It can provide a #GdkPixbuf
 * for a given size and state on request, and automatically caches
 * some of the rendered #GdkPixbuf objects.
 *
 * Normally you would use gtk_widget_render_icon() instead of
 * using #GtkIconSet directly. The one case where you'd use
 * #GtkIconSet is to create application-specific icon sets to place in
 * a #GtkIconFactory.
 * 
 * Return value: a new #GtkIconSet
 **/
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411
GtkIconSet*
gtk_icon_set_new (void)
{
  GtkIconSet *icon_set;

  icon_set = g_new (GtkIconSet, 1);

  icon_set->ref_count = 1;
  icon_set->sources = NULL;
  icon_set->cache = NULL;
  icon_set->cache_size = 0;
  icon_set->cache_serial = cache_serial;
  
  return icon_set;
}

1412 1413 1414 1415
/**
 * gtk_icon_set_new_from_pixbuf:
 * @pixbuf: a #GdkPixbuf
 * 
1416 1417 1418 1419 1420
 * Creates a new #GtkIconSet with @pixbuf as the default/fallback
 * source image. If you don't add any additional #GtkIconSource to the
 * icon set, all variants of the icon will be created from @pixbuf,
 * using scaling, pixelation, etc. as required to adjust the icon size
 * or make the icon look insensitive/prelighted.
1421 1422 1423 1424 1425 1426 1427 1428
 * 
 * Return value: a new #GtkIconSet
 **/
GtkIconSet *
gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  GtkIconSet *set;

1429
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1430 1431 1432 1433 1434

  g_return_val_if_fail (pixbuf != NULL, NULL);

  set = gtk_icon_set_new ();

1435
  gtk_icon_source_set_pixbuf (&source, pixbuf);
1436
  gtk_icon_set_add_source (set, &source);
1437
  gtk_icon_source_set_pixbuf (&source, NULL);
1438 1439 1440 1441 1442
  
  return set;
}


1443 1444
/**
 * gtk_icon_set_ref:
1445
 * @icon_set: a #GtkIconSet.
1446
 * 
1447
 * Increments the reference count on @icon_set.
1448
 * 
1449
 * Return value: @icon_set.
1450
 **/
1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
GtkIconSet*
gtk_icon_set_ref (GtkIconSet *icon_set)
{
  g_return_val_if_fail (icon_set != NULL, NULL);
  g_return_val_if_fail (icon_set->ref_count > 0, NULL);

  icon_set->ref_count += 1;

  return icon_set;
}

1462 1463 1464 1465 1466 1467 1468
/**
 * gtk_icon_set_unref:
 * @icon_set: a #GtkIconSet
 * 
 * Decrements the reference count on @icon_set, and frees memory
 * if the reference count reaches 0.
 **/
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485
void
gtk_icon_set_unref (GtkIconSet *icon_set)
{
  g_return_if_fail (icon_set != NULL);
  g_return_if_fail (icon_set->ref_count > 0);

  icon_set->ref_count -= 1;

  if (icon_set->ref_count == 0)
    {
      GSList *tmp_list = icon_set->sources;
      while (tmp_list != NULL)
        {
          gtk_icon_source_free (tmp_list->data);

          tmp_list = g_slist_next (tmp_list);
        }
1486
      g_slist_free (icon_set->sources);
1487 1488 1489 1490 1491 1492 1493

      clear_cache (icon_set, TRUE);

      g_free (icon_set);
    }
}

1494 1495 1496 1497 1498 1499
GType
gtk_icon_set_get_type (void)
{
  static GType our_type = 0;
  
  if (our_type == 0)
1500
    our_type = g_boxed_type_register_static ("GtkIconSet",
1501 1502 1503 1504 1505 1506
					     (GBoxedCopyFunc) gtk_icon_set_ref,
					     (GBoxedFreeFunc) gtk_icon_set_unref);

  return our_type;
}

1507 1508 1509 1510 1511 1512 1513 1514
/**
 * gtk_icon_set_copy:
 * @icon_set: a #GtkIconSet
 * 
 * Copies @icon_set by value. 
 * 
 * Return value: a new #GtkIconSet identical to the first.
 **/
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
GtkIconSet*
gtk_icon_set_copy (GtkIconSet *icon_set)
{
  GtkIconSet *copy;
  GSList *tmp_list;
  
  copy = gtk_icon_set_new ();

  tmp_list = icon_set->sources;
  while (tmp_list != NULL)
    {
      copy->sources = g_slist_prepend (copy->sources,
                                       gtk_icon_source_copy (tmp_list->data));

      tmp_list = g_slist_next (tmp_list);
    }

  copy->sources = g_slist_reverse (copy->sources);

  copy->cache = copy_cache (icon_set, copy);
  copy->cache_size = icon_set->cache_size;
  copy->cache_serial = icon_set->cache_serial;
  
  return copy;
}

static gboolean
1542 1543
sizes_equivalent (GtkIconSize lhs,
                  GtkIconSize rhs)
1544
{
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
  /* We used to consider sizes equivalent if they were
   * the same pixel size, but we don't have the GtkSettings
   * here, so we can't do that. Plus, it's not clear that
   * it is right... it was just a workaround for the fact
   * that we register icons by logical size, not pixel size.
   */
#if 1
  return lhs == rhs;
#else  
  
1555 1556
  gint r_w, r_h, l_w, l_h;

1557 1558
  icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
  icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1559 1560

  return r_w == l_w && r_h == l_h;
1561
#endif
1562 1563
}

1564 1565 1566 1567 1568 1569
static GtkIconSource *
find_best_matching_source (GtkIconSet       *icon_set,
			   GtkTextDirection  direction,
			   GtkStateType      state,
			   GtkIconSize       size,
			   GSList           *failed)
1570 1571 1572
{
  GtkIconSource *source;
  GSList *tmp_list;
1573
  
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
  /* We need to find the best icon source.  Direction matters more
   * than state, state matters more than size. icon_set->sources
   * is sorted according to wildness, so if we take the first
   * match we find it will be the least-wild match (if there are
   * multiple matches for a given "wildness" then the RC file contained
   * dumb stuff, and we end up with an arbitrary matching source)
   */
  
  source = NULL;
  tmp_list = icon_set->sources;
  while (tmp_list != NULL)
    {
      GtkIconSource *s = tmp_list->data;
      
      if ((s->any_direction || (s->direction == direction)) &&
          (s->any_state || (s->state == state)) &&
          (s->any_size || (sizes_equivalent (size, s->size))))
        {
1592 1593 1594 1595 1596 1597 1598
	  if (!g_slist_find (failed, s))
	    {
	      source = s;
	      break;
	    }
	}
	  
1599 1600 1601
      tmp_list = g_slist_next (tmp_list);
    }

1602 1603
  return source;
}
1604
  
1605 1606 1607 1608 1609
static gboolean
ensure_filename_pixbuf (GtkIconSet    *icon_set,
			GtkIconSource *source)
{
  if (source->filename_pixbuf == NULL)
1610
    {
1611
      GError *error = NULL;
1612
      
1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625
      source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
      
      if (source->filename_pixbuf == NULL)
	{
	  /* Remove this icon source so we don't keep trying to
	   * load it.
	   */
	  g_warning (_("Error loading icon: %s"), error->message);
	  g_error_free (error);
	  
	  icon_set->sources = g_slist_remove (icon_set->sources, source);
	  
	  gtk_icon_source_free (source);
1626

1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
	  return FALSE;
	}
    }
  
  return TRUE;
}
 
static GdkPixbuf *
render_icon_name_pixbuf (GtkIconSource    *icon_source,
			 GtkStyle         *style,
			 GtkTextDirection  direction,
			 GtkStateType      state,
			 GtkIconSize       size,
			 GtkWidget         *widget,
			 const char        *detail)
{
  GdkPixbuf *pixbuf;
  GdkPixbuf *tmp_pixbuf;
  GtkIconSource tmp_source;
  GdkScreen *screen;
  GtkIconTheme *icon_theme;
  GtkSettings *settings;
  gint width, height, pixel_size;
  GError *error = NULL;
  
  if (widget && gtk_widget_has_screen (widget))
    screen = gtk_widget_get_screen (widget);
Matthias Clasen's avatar
Matthias Clasen committed
1654
  else if (style && style->colormap)
1655 1656 1657 1658 1659 1660
    screen = gdk_colormap_get_screen (style->colormap);
  else
    {
      screen = gdk_screen_get_default ();
      GTK_NOTE (MULTIHEAD,
		g_warning ("Using the default screen for gtk_icon_source_render_icon()"));
1661 1662
    }

1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
  icon_theme = gtk_icon_theme_get_for_screen (screen);
  settings = gtk_settings_get_for_screen (screen);

  if (!gtk_icon_size_lookup_for_settings (settings, size, &width, &height))
    {
      g_warning ("Invalid icon size %d\n", size);
      width = height = 24;
    }

  pixel_size = MIN (width, height);

  tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
					 icon_source->source.icon_name,
					 pixel_size, 0,
					 &error);

  if (!tmp_pixbuf)
    {
      g_warning ("Error loading theme icon for stock: %s", error->message);
1682
      g_error_free (error);
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
      return NULL;
    }
  
  tmp_source = *icon_source;
  tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
  tmp_source.source.pixbuf = tmp_pixbuf;

  pixbuf = gtk_style_render_icon (style, &tmp_source,
				  direction, state, -1,
				  widget, detail);

  if (!pixbuf)
    g_warning ("Failed to render icon");

  g_object_unref (tmp_pixbuf);

  return pixbuf;
}

static GdkPixbuf *
find_and_render_icon_source (GtkIconSet       *icon_set,
			     GtkStyle         *style,
			     GtkTextDirection  direction,
			     GtkStateType      state,
			     GtkIconSize       size,
			     GtkWidget         *widget,
			     const char        *detail)
{
  GSList *failed = NULL;
  GdkPixbuf *pixbuf = NULL;

  /* We treat failure in two different ways:
   *
   *  A) If loading a source that specifies a filename fails,
   *     we treat that as permanent, and remove the source
   *     from the GtkIconSet. (in ensure_filename_pixbuf ()
   *  B) If loading a themed icon fails, or scaling an icon
   *     fails, we treat that as transient and will try
   *     again next time the icon falls out of the cache
   *     and we need to recreate it.
   */
  while (pixbuf == NULL)
    {
      GtkIconSource *source = find_best_matching_source (icon_set, direction, state, size, failed);
      
      if (source == NULL)
	break;

      switch (source->type)
	{
	case GTK_ICON_SOURCE_FILENAME:
	  if (!ensure_filename_pixbuf (icon_set, source))
	    break;
	  /* Fall through */
	case GTK_ICON_SOURCE_PIXBUF:
	  pixbuf = gtk_style_render_icon (style, source,
					  direction, state, size,
					  widget, detail);
	  if (!pixbuf)
	    {
	      g_warning ("Failed to render icon");
	      failed = g_slist_prepend (failed, source);
	    }
	  break;
	case GTK_ICON_SOURCE_ICON_NAME:
	  pixbuf = render_icon_name_pixbuf (source, style,
					    direction, state, size,
					    widget, detail);
	  if (!pixbuf)
	    failed = g_slist_prepend (failed, source);
	  break;
	case GTK_ICON_SOURCE_EMPTY:
	  g_assert_not_reached ();
	}
    }

  g_slist_free (failed);

  return pixbuf;
1762 1763
}

1764
static GdkPixbuf*
1765 1766 1767 1768 1769 1770
render_fallback_image (GtkStyle          *style,
                       GtkTextDirection   direction,
                       GtkStateType       state,
                       GtkIconSize        size,
                       GtkWidget         *widget,
                       const char        *detail)
1771 1772
{
  /* This icon can be used for any direction/state/size */
1773
  static GtkIconSource fallback_source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1774

1775 1776 1777 1778 1779 1780
  if (fallback_source.type == GTK_ICON_SOURCE_EMPTY)
    {
      GdkPixbuf *pixbuf = gdk_pixbuf_new_from_inline (-1, stock_missing_image_24, FALSE, NULL);
      gtk_icon_source_set_pixbuf (&fallback_source, pixbuf);
      g_object_unref (pixbuf);
    }
1781 1782 1783 1784 1785 1786 1787 1788
  
  return gtk_style_render_icon (style,
                                &fallback_source,
                                direction,
                                state,
                                size,
                                widget,
                                detail);
1789 1790
}

1791 1792 1793 1794 1795 1796
/**
 * gtk_icon_set_render_icon:
 * @icon_set: a #GtkIconSet
 * @style: a #GtkStyle associated with @widget, or %NULL
 * @direction: text direction
 * @state: widget state
1797 1798
 * @size: icon size. A size of (GtkIconSize)-1
 *        means render at the size of the source and don't scale.
1799 1800 1801 1802 1803 1804
 * @widget: widget that will display the icon, or %NULL.
 *          The only use that is typically made of this
 *          is to determine the appropriate #GdkScreen.
 * @detail: detail to pass to the theme engine, or %NULL.
 *          Note that passing a detail of anything but %NULL
 *          will disable caching.
1805 1806
 * 
 * Renders an icon using gtk_style_render_icon(). In most cases,
Havoc Pennington's avatar