gtkiconfactory.c 83.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Havoc Pennington's avatar
Havoc Pennington committed
2
 * Copyright (C) 2000 Red Hat, Inc.
3
 *               2008 Johan Dahlin
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * 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
Matthias Clasen's avatar
Matthias Clasen committed
24
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
25 26
 */

27
#include "config.h"
28

Manish Singh's avatar
Manish Singh committed
29 30 31
#include <stdlib.h>
#include <errno.h>
#include <string.h>
32

33
#include "gtkiconfactory.h"
34
#include "gtkiconcache.h"
35
#include "gtkdebug.h"
36
#include "gtkicontheme.h"
37
#include "gtksettingsprivate.h"
38
#include "gtkstock.h"
39
#include "gtkwidget.h"
Havoc Pennington's avatar
Havoc Pennington committed
40
#include "gtkintl.h"
41 42
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
43
#include "gtktypebuiltins.h"
44

45 46
static GSList *all_icon_factories = NULL;

47
struct _GtkIconFactoryPrivate
48 49 50 51
{
  GHashTable *icons;
};

52 53 54
typedef enum {
  GTK_ICON_SOURCE_EMPTY,
  GTK_ICON_SOURCE_ICON_NAME,
55
  GTK_ICON_SOURCE_STATIC_ICON_NAME,
56 57 58 59
  GTK_ICON_SOURCE_FILENAME,
  GTK_ICON_SOURCE_PIXBUF
} GtkIconSourceType;

60 61
struct _GtkIconSource
{
62
  GtkIconSourceType type;
Matthias Clasen's avatar
Matthias Clasen committed
63

64 65 66 67 68
  union {
    gchar *icon_name;
    gchar *filename;
    GdkPixbuf *pixbuf;
  } source;
69

70
  GdkPixbuf *filename_pixbuf;
71 72 73 74 75 76 77 78 79 80 81 82 83 84

  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;
};

85

86 87 88 89 90 91 92 93 94 95 96 97 98 99
static void
gtk_icon_factory_buildable_init  (GtkBuildableIface      *iface);

static gboolean gtk_icon_factory_buildable_custom_tag_start (GtkBuildable     *buildable,
							     GtkBuilder       *builder,
							     GObject          *child,
							     const gchar      *tagname,
							     GMarkupParser    *parser,
							     gpointer         *data);
static void gtk_icon_factory_buildable_custom_tag_end (GtkBuildable *buildable,
						       GtkBuilder   *builder,
						       GObject      *child,
						       const gchar  *tagname,
						       gpointer     *user_data);
100 101
static void gtk_icon_factory_finalize   (GObject             *object);
static void get_default_icons           (GtkIconFactory      *icon_factory);
102
static void icon_source_clear           (GtkIconSource       *source);
103

104 105 106 107
static GtkIconSize icon_size_register_intern (const gchar *name,
					      gint         width,
					      gint         height);

108 109 110 111 112
#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 }

113 114 115
G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_icon_factory_buildable_init))
116 117 118 119

static void
gtk_icon_factory_init (GtkIconFactory *factory)
{
120
  GtkIconFactoryPrivate *priv;
121 122 123

  factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
                                               GTK_TYPE_ICON_FACTORY,
124
                                               GtkIconFactoryPrivate);
125 126 127
  priv = factory->priv;

  priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
128
  all_icon_factories = g_slist_prepend (all_icon_factories, factory);
129 130 131 132 133 134
}

static void
gtk_icon_factory_class_init (GtkIconFactoryClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
Matthias Clasen's avatar
Matthias Clasen committed
135

136
  object_class->finalize = gtk_icon_factory_finalize;
137

138
  g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
139 140
}

141 142 143 144 145 146 147
static void
gtk_icon_factory_buildable_init (GtkBuildableIface *iface)
{
  iface->custom_tag_start = gtk_icon_factory_buildable_custom_tag_start;
  iface->custom_tag_end = gtk_icon_factory_buildable_custom_tag_end;
}

148 149 150 151 152 153 154 155 156 157 158
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);
159
  GtkIconFactoryPrivate *priv = factory->priv;
160

161
  all_icon_factories = g_slist_remove (all_icon_factories, factory);
Matthias Clasen's avatar
Matthias Clasen committed
162

163
  g_hash_table_foreach (priv->icons, free_icon_set, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
164

165
  g_hash_table_destroy (priv->icons);
Matthias Clasen's avatar
Matthias Clasen committed
166

Matthias Clasen's avatar
Matthias Clasen committed
167
  G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
168 169
}

170 171 172 173
/**
 * gtk_icon_factory_new:
 *
 * Creates a new #GtkIconFactory. An icon factory manages a collection
174
 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
175 176 177
 * 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
178
 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
179 180 181 182 183 184 185
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
186
 *
187
 * Return value: a new #GtkIconFactory
Matthias Clasen's avatar
Matthias Clasen committed
188
 */
189 190 191
GtkIconFactory*
gtk_icon_factory_new (void)
{
Manish Singh's avatar
Manish Singh committed
192
  return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
193 194
}

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
/**
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
211
 */
212 213 214 215 216
void
gtk_icon_factory_add (GtkIconFactory *factory,
                      const gchar    *stock_id,
                      GtkIconSet     *icon_set)
{
217
  GtkIconFactoryPrivate *priv = factory->priv;
218 219 220 221 222
  gpointer old_key = NULL;
  gpointer old_value = NULL;

  g_return_if_fail (GTK_IS_ICON_FACTORY (factory));
  g_return_if_fail (stock_id != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
223
  g_return_if_fail (icon_set != NULL);
224

225
  g_hash_table_lookup_extended (priv->icons, stock_id,
226 227 228 229
                                &old_key, &old_value);

  if (old_value == icon_set)
    return;
Matthias Clasen's avatar
Matthias Clasen committed
230

231 232 233 234
  gtk_icon_set_ref (icon_set);

  /* GHashTable key memory management is so fantastically broken. */
  if (old_key)
235
    g_hash_table_insert (priv->icons, old_key, icon_set);
236
  else
237
    g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
238 239 240 241 242

  if (old_value)
    gtk_icon_set_unref (old_value);
}

243 244 245 246
/**
 * gtk_icon_factory_lookup:
 * @factory: a #GtkIconFactory
 * @stock_id: an icon name
Matthias Clasen's avatar
Matthias Clasen committed
247
 *
248 249 250 251 252
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
253
 *
254
 * Return value: icon set of @stock_id.
Matthias Clasen's avatar
Matthias Clasen committed
255
 */
256 257 258 259
GtkIconSet *
gtk_icon_factory_lookup (GtkIconFactory *factory,
                         const gchar    *stock_id)
{
260
  GtkIconFactoryPrivate *priv;
261

262 263
  g_return_val_if_fail (GTK_IS_ICON_FACTORY (factory), NULL);
  g_return_val_if_fail (stock_id != NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
264

265 266 267
  priv = factory->priv;

  return g_hash_table_lookup (priv->icons, stock_id);
268 269 270 271 272
}

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

273 274 275
/**
 * gtk_icon_factory_add_default:
 * @factory: a #GtkIconFactory
Matthias Clasen's avatar
Matthias Clasen committed
276
 *
277 278 279 280 281 282
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
283
 */
284 285 286 287 288
void
gtk_icon_factory_add_default (GtkIconFactory *factory)
{
  g_return_if_fail (GTK_IS_ICON_FACTORY (factory));

Manish Singh's avatar
Manish Singh committed
289
  g_object_ref (factory);
Matthias Clasen's avatar
Matthias Clasen committed
290

291 292 293
  default_factories = g_slist_prepend (default_factories, factory);
}

294 295 296 297 298 299 300
/**
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
301
 */
302 303 304 305 306 307 308
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
309
  g_object_unref (factory);
310 311
}

312 313
void
_gtk_icon_factory_ensure_default_icons (void)
314 315 316 317 318 319 320 321 322
{
  if (gtk_default_icons == NULL)
    {
      gtk_default_icons = gtk_icon_factory_new ();

      get_default_icons (gtk_default_icons);
    }
}

323 324 325 326 327 328 329 330 331
/**
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
332
 *
333
 * Return value: a #GtkIconSet, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
334
 */
335 336 337 338 339 340
GtkIconSet *
gtk_icon_factory_lookup_default (const gchar *stock_id)
{
  GSList *tmp_list;

  g_return_val_if_fail (stock_id != NULL, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
341

342 343 344 345 346 347 348 349 350
  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;
Matthias Clasen's avatar
Matthias Clasen committed
351

352 353 354
      tmp_list = g_slist_next (tmp_list);
    }

355
  _gtk_icon_factory_ensure_default_icons ();
Matthias Clasen's avatar
Matthias Clasen committed
356

357 358 359
  return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
}

360
static void
361
register_stock_icon (GtkIconFactory *factory,
362 363
		     const gchar    *stock_id,
                     const gchar    *icon_name)
364
{
365 366
  GtkIconSet *set = gtk_icon_set_new ();
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
367

368
  source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
369 370
  source.source.icon_name = (gchar *)icon_name;
  source.direction = GTK_TEXT_DIR_NONE;
371
  gtk_icon_set_add_source (set, &source);
Matthias Clasen's avatar
Matthias Clasen committed
372

373 374
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
375
}
376

377 378 379
static void
register_bidi_stock_icon (GtkIconFactory *factory,
			  const gchar    *stock_id,
380
                          const gchar    *icon_name)
381
{
382 383
  GtkIconSet *set = gtk_icon_set_new ();
  GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
384

385
  source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
386
  source.source.icon_name = (gchar *)icon_name;
387
  source.direction = GTK_TEXT_DIR_LTR;
388
  gtk_icon_set_add_source (set, &source);
Matthias Clasen's avatar
Matthias Clasen committed
389

390
  source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
391
  source.source.icon_name = (gchar *)icon_name;
392 393
  source.direction = GTK_TEXT_DIR_RTL;
  gtk_icon_set_add_source (set, &source);
Matthias Clasen's avatar
Matthias Clasen committed
394

395 396
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
397 398
}

399 400 401 402
static void
get_default_icons (GtkIconFactory *factory)
{
  /* KEEP IN SYNC with gtkstock.c */
403

404 405
  register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
  register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
406
  register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
  register_stock_icon (factory, GTK_STOCK_DIALOG_QUESTION, "dialog-question");
  register_stock_icon (factory, GTK_STOCK_DIALOG_WARNING, "dialog-warning");
  register_stock_icon (factory, GTK_STOCK_DND, GTK_STOCK_DND);
  register_stock_icon (factory, GTK_STOCK_DND_MULTIPLE, GTK_STOCK_DND_MULTIPLE);
  register_stock_icon (factory, GTK_STOCK_APPLY, GTK_STOCK_APPLY);
  register_stock_icon (factory, GTK_STOCK_CANCEL, GTK_STOCK_CANCEL);
  register_stock_icon (factory, GTK_STOCK_NO, GTK_STOCK_NO);
  register_stock_icon (factory, GTK_STOCK_OK, GTK_STOCK_OK);
  register_stock_icon (factory, GTK_STOCK_YES, GTK_STOCK_YES);
  register_stock_icon (factory, GTK_STOCK_CLOSE, "window-close");
  register_stock_icon (factory, GTK_STOCK_ADD, "list-add");
  register_stock_icon (factory, GTK_STOCK_JUSTIFY_CENTER, "format-justify-center");
  register_stock_icon (factory, GTK_STOCK_JUSTIFY_FILL, "format-justify-fill");
  register_stock_icon (factory, GTK_STOCK_JUSTIFY_LEFT, "format-justify-left");
  register_stock_icon (factory, GTK_STOCK_JUSTIFY_RIGHT, "format-justify-right");
  register_stock_icon (factory, GTK_STOCK_GOTO_BOTTOM, "go-bottom");
  register_stock_icon (factory, GTK_STOCK_CDROM, "media-optical");
  register_stock_icon (factory, GTK_STOCK_CONVERT, GTK_STOCK_CONVERT);
  register_stock_icon (factory, GTK_STOCK_COPY, "edit-copy");
  register_stock_icon (factory, GTK_STOCK_CUT, "edit-cut");
  register_stock_icon (factory, GTK_STOCK_GO_DOWN, "go-down");
  register_stock_icon (factory, GTK_STOCK_EXECUTE, "system-run");
  register_stock_icon (factory, GTK_STOCK_QUIT, "application-exit");
  register_bidi_stock_icon (factory, GTK_STOCK_GOTO_FIRST, "go-first");
  register_stock_icon (factory, GTK_STOCK_SELECT_FONT, GTK_STOCK_SELECT_FONT);
  register_stock_icon (factory, GTK_STOCK_FULLSCREEN, "view-fullscreen");
  register_stock_icon (factory, GTK_STOCK_LEAVE_FULLSCREEN, "view-restore");
  register_stock_icon (factory, GTK_STOCK_HARDDISK, "drive-harddisk");
  register_stock_icon (factory, GTK_STOCK_HELP, "help-contents");
  register_stock_icon (factory, GTK_STOCK_HOME, "go-home");
437
  register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
438 439 440 441 442 443 444 445 446 447 448 449 450
  register_bidi_stock_icon (factory, GTK_STOCK_JUMP_TO, "go-jump");
  register_bidi_stock_icon (factory, GTK_STOCK_GOTO_LAST, "go-last");
  register_bidi_stock_icon (factory, GTK_STOCK_GO_BACK, "go-previous");
  register_stock_icon (factory, GTK_STOCK_MISSING_IMAGE, "image-missing");
  register_stock_icon (factory, GTK_STOCK_NETWORK, "network-idle");
  register_stock_icon (factory, GTK_STOCK_NEW, "document-new");
  register_stock_icon (factory, GTK_STOCK_OPEN, "document-open");
  register_stock_icon (factory, GTK_STOCK_ORIENTATION_PORTRAIT, GTK_STOCK_ORIENTATION_PORTRAIT);
  register_stock_icon (factory, GTK_STOCK_ORIENTATION_LANDSCAPE, GTK_STOCK_ORIENTATION_LANDSCAPE);
  register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
  register_stock_icon (factory, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
  register_stock_icon (factory, GTK_STOCK_PAGE_SETUP, GTK_STOCK_PAGE_SETUP);
  register_stock_icon (factory, GTK_STOCK_PASTE, "edit-paste");
451
  register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
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 501 502 503 504 505 506 507
  register_stock_icon (factory, GTK_STOCK_PRINT, "document-print");
  register_stock_icon (factory, GTK_STOCK_PRINT_ERROR, "printer-error");
  register_stock_icon (factory, GTK_STOCK_PRINT_PAUSED, "printer-paused");
  register_stock_icon (factory, GTK_STOCK_PRINT_PREVIEW, "document-print-preview");
  register_stock_icon (factory, GTK_STOCK_PRINT_REPORT, "printer-info");
  register_stock_icon (factory, GTK_STOCK_PRINT_WARNING, "printer-warning");
  register_stock_icon (factory, GTK_STOCK_PROPERTIES, "document-properties");
  register_bidi_stock_icon (factory, GTK_STOCK_REDO, "edit-redo");
  register_stock_icon (factory, GTK_STOCK_REMOVE, "list-remove");
  register_stock_icon (factory, GTK_STOCK_REFRESH, "view-refresh");
  register_bidi_stock_icon (factory, GTK_STOCK_REVERT_TO_SAVED, "document-revert");
  register_bidi_stock_icon (factory, GTK_STOCK_GO_FORWARD, "go-next");
  register_stock_icon (factory, GTK_STOCK_SAVE, "document-save");
  register_stock_icon (factory, GTK_STOCK_FLOPPY, "media-floppy");
  register_stock_icon (factory, GTK_STOCK_SAVE_AS, "document-save-as");
  register_stock_icon (factory, GTK_STOCK_FIND, "edit-find");
  register_stock_icon (factory, GTK_STOCK_FIND_AND_REPLACE, "edit-find-replace");
  register_stock_icon (factory, GTK_STOCK_SORT_DESCENDING, "view-sort-descending");
  register_stock_icon (factory, GTK_STOCK_SORT_ASCENDING, "view-sort-ascending");
  register_stock_icon (factory, GTK_STOCK_SPELL_CHECK, "tools-check-spelling");
  register_stock_icon (factory, GTK_STOCK_STOP, "process-stop");
  register_stock_icon (factory, GTK_STOCK_BOLD, "format-text-bold");
  register_stock_icon (factory, GTK_STOCK_ITALIC, "format-text-italic");
  register_stock_icon (factory, GTK_STOCK_STRIKETHROUGH, "format-text-strikethrough");
  register_stock_icon (factory, GTK_STOCK_UNDERLINE, "format-text-underline");
  register_bidi_stock_icon (factory, GTK_STOCK_INDENT, "format-indent-more");
  register_bidi_stock_icon (factory, GTK_STOCK_UNINDENT, "format-indent-less");
  register_stock_icon (factory, GTK_STOCK_GOTO_TOP, "go-top");
  register_stock_icon (factory, GTK_STOCK_DELETE, "edit-delete");
  register_bidi_stock_icon (factory, GTK_STOCK_UNDELETE, GTK_STOCK_UNDELETE);
  register_bidi_stock_icon (factory, GTK_STOCK_UNDO, "edit-undo");
  register_stock_icon (factory, GTK_STOCK_GO_UP, "go-up");
  register_stock_icon (factory, GTK_STOCK_FILE, "document-x-generic");
  register_stock_icon (factory, GTK_STOCK_DIRECTORY, "folder");
  register_stock_icon (factory, GTK_STOCK_ABOUT, "help-about");
  register_stock_icon (factory, GTK_STOCK_CONNECT, GTK_STOCK_CONNECT);
  register_stock_icon (factory, GTK_STOCK_DISCONNECT, GTK_STOCK_DISCONNECT);
  register_stock_icon (factory, GTK_STOCK_EDIT, GTK_STOCK_EDIT);
  register_stock_icon (factory, GTK_STOCK_CAPS_LOCK_WARNING, GTK_STOCK_CAPS_LOCK_WARNING);
  register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_FORWARD, "media-seek-forward");
  register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_NEXT, "media-skip-forward");
  register_stock_icon (factory, GTK_STOCK_MEDIA_PAUSE, "media-playback-pause");
  register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PLAY, "media-playback-start");
  register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_PREVIOUS, "media-skip-backward");
  register_stock_icon (factory, GTK_STOCK_MEDIA_RECORD, "media-record");
  register_bidi_stock_icon (factory, GTK_STOCK_MEDIA_REWIND, "media-seek-backward");
  register_stock_icon (factory, GTK_STOCK_MEDIA_STOP, "media-playback-stop");
  register_stock_icon (factory, GTK_STOCK_INDEX, GTK_STOCK_INDEX);
  register_stock_icon (factory, GTK_STOCK_ZOOM_100, "zoom-original");
  register_stock_icon (factory, GTK_STOCK_ZOOM_IN, "zoom-in");
  register_stock_icon (factory, GTK_STOCK_ZOOM_OUT, "zoom-out");
  register_stock_icon (factory, GTK_STOCK_ZOOM_FIT, "zoom-fit-best");
  register_stock_icon (factory, GTK_STOCK_SELECT_ALL, "edit-select-all");
  register_stock_icon (factory, GTK_STOCK_CLEAR, "edit-clear");
  register_stock_icon (factory, GTK_STOCK_SELECT_COLOR, GTK_STOCK_SELECT_COLOR);
  register_stock_icon (factory, GTK_STOCK_COLOR_PICKER, GTK_STOCK_COLOR_PICKER);
508 509
}

510 511 512
/************************************************************
 *                    Icon size handling                    *
 ************************************************************/
513 514 515 516 517

typedef struct _IconSize IconSize;

struct _IconSize
{
518
  gint size;
519
  gchar *name;
Matthias Clasen's avatar
Matthias Clasen committed
520

521 522
  gint width;
  gint height;
523 524
};

525
typedef struct _IconAlias IconAlias;
526

527
struct _IconAlias
528
{
529 530 531
  gchar *name;
  gint   target;
};
532

533 534 535 536 537 538 539 540
typedef struct _SettingsIconSize SettingsIconSize;

struct _SettingsIconSize
{
  gint width;
  gint height;
};

541 542 543 544
static GHashTable *icon_aliases = NULL;
static IconSize *icon_sizes = NULL;
static gint      icon_sizes_allocated = 0;
static gint      icon_sizes_used = 0;
545 546

static void
547
init_icon_sizes (void)
548
{
549
  if (icon_sizes == NULL)
550
    {
551
#define NUM_BUILTIN_SIZES 7
552
      gint i;
553

554
      icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
Matthias Clasen's avatar
Matthias Clasen committed
555

556 557 558 559 560 561 562 563 564 565 566 567 568 569
      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.
       */
Matthias Clasen's avatar
Matthias Clasen committed
570

571 572 573 574 575 576 577
      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
578 579
      icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
      icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
580 581 582 583 584

      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;
Matthias Clasen's avatar
Matthias Clasen committed
585

586 587 588 589
      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;
590

591 592 593 594 595
      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;

596 597 598 599
      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;
600

601
      g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
602

603 604 605 606 607
      /* 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);
Matthias Clasen's avatar
Matthias Clasen committed
608

609 610
          ++i;
        }
Matthias Clasen's avatar
Matthias Clasen committed
611

612
#undef NUM_BUILTIN_SIZES
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
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);
Matthias Clasen's avatar
Matthias Clasen committed
661

662 663 664 665 666 667 668 669 670 671 672
  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);
Matthias Clasen's avatar
Matthias Clasen committed
673

674 675 676 677 678 679 680 681 682 683 684 685 686
  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++;
Matthias Clasen's avatar
Matthias Clasen committed
687

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
  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;
Matthias Clasen's avatar
Matthias Clasen committed
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
      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);

801
  gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
802 803 804 805 806 807 808 809 810
}

static void
icon_sizes_init_for_settings (GtkSettings *settings)
{
  g_signal_connect (settings,
		    "notify::gtk-icon-sizes",
		    G_CALLBACK (icon_size_settings_changed),
		    NULL);
Matthias Clasen's avatar
Matthias Clasen committed
811

812 813
  icon_size_set_all_from_settings (settings);
}
Matthias Clasen's avatar
Matthias Clasen committed
814

815
static gboolean
816 817 818 819 820 821 822 823
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;
Matthias Clasen's avatar
Matthias Clasen committed
824

825 826
  init_icon_sizes ();

827
  if (size == (GtkIconSize)-1)
828
    return FALSE;
829

830
  if (size >= icon_sizes_used)
831 832 833 834 835 836 837 838
    return FALSE;

  if (size == GTK_ICON_SIZE_INVALID)
    return FALSE;

  if (settings)
    {
      gboolean initial = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
839

840 841
      settings_sizes = get_settings_sizes (settings, &initial);

842 843
      if (initial)
	icon_sizes_init_for_settings (settings);
Matthias Clasen's avatar
Matthias Clasen committed
844

845 846 847
      if (size < settings_sizes->len)
	{
	  SettingsIconSize *settings_size;
Matthias Clasen's avatar
Matthias Clasen committed
848

849
	  settings_size = &g_array_index (settings_sizes, SettingsIconSize, size);
Matthias Clasen's avatar
Matthias Clasen committed
850

851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
	  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:
867
 * @settings: a #GtkSettings object, used to determine
868
 *   which set of user preferences to used.
869
 * @size: (type int): an icon size
870 871
 * @width: (out): location to store icon width
 * @height: (out): location to store icon height
872 873
 *
 * Obtains the pixel size of a semantic icon size, possibly
Matthias Clasen's avatar
Matthias Clasen committed
874
 * modified by user preferences for a particular
Matthias Clasen's avatar
Matthias Clasen committed
875
 * #GtkSettings. Normally @size would be
876
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
877
 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
878 879 880 881 882
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
883
 *
884
 * Return value: %TRUE if @size was a valid size
885 886
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
887
 */
888 889 890
gboolean
gtk_icon_size_lookup_for_settings (GtkSettings *settings,
				   GtkIconSize  size,
891 892
				   gint        *width,
				   gint        *height)
893 894 895
{
  g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);

896
  return icon_size_lookup_intern (settings, size, width, height);
897 898
}

899 900
/**
 * gtk_icon_size_lookup:
901
 * @size: (type int): an icon size
902 903
 * @width: (out): location to store icon width
 * @height: (out): location to store icon height
904
 *
905 906 907 908
 * 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
909
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
910
 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
911 912 913
 * 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
914 915
 * are free to render the pixbuf however they like, including changing
 * the usual size.
Matthias Clasen's avatar
Matthias Clasen committed
916
 *
917
 * Return value: %TRUE if @size was a valid size
Matthias Clasen's avatar
Matthias Clasen committed
918
 */
919
gboolean
920
gtk_icon_size_lookup (GtkIconSize  size,
921 922 923
                      gint        *widthp,
                      gint        *heightp)
{
924 925
  GTK_NOTE (MULTIHEAD,
	    g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
926

927 928 929
  return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
					    size, widthp, heightp);
}
930

931 932 933 934 935 936 937
static GtkIconSize
icon_size_register_intern (const gchar *name,
			   gint         width,
			   gint         height)
{
  IconAlias *old_alias;
  GtkIconSize size;
Matthias Clasen's avatar
Matthias Clasen committed
938

939
  init_icon_sizes ();
940

941 942 943 944 945 946
  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;
    }
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
  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;
973 974
}

975 976
/**
 * gtk_icon_size_register:
977
 * @name: name of the icon size
978 979 980 981
 * @width: the icon width
 * @height: the icon height
 *
 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
982 983
 * etc. Returns the integer value for the size.
 *
984
 * Returns: (type int): integer value representing the size
Matthias Clasen's avatar
Matthias Clasen committed
985
 */
986 987
GtkIconSize
gtk_icon_size_register (const gchar *name,
988 989 990
                        gint         width,
                        gint         height)
{
991 992 993
  g_return_val_if_fail (name != NULL, 0);
  g_return_val_if_fail (width > 0, 0);
  g_return_val_if_fail (height > 0, 0);
Matthias Clasen's avatar
Matthias Clasen committed
994

995
  return icon_size_register_intern (name, width, height);
996 997
}

998 999 1000
/**
 * gtk_icon_size_register_alias:
 * @alias: an alias for @target
1001
 * @target: (type int): an existing icon size
1002 1003 1004 1005
 *
 * Registers @alias as another name for @target.
 * So calling gtk_icon_size_from_name() with @alias as argument
 * will return @target.
Matthias Clasen's avatar
Matthias Clasen committed
1006
 */
1007 1008
void
gtk_icon_size_register_alias (const gchar *alias,
1009
                              GtkIconSize  target)
1010
{
1011
  IconAlias *ia;
Matthias Clasen's avatar
Matthias Clasen committed
1012

1013 1014 1015 1016
  g_return_if_fail (alias != NULL);

  init_icon_sizes ();

1017
  if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1018
    g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
  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);
    }
1040 1041
}

Matthias Clasen's avatar
Matthias Clasen committed
1042
/**
Matthias Clasen's avatar
Matthias Clasen committed
1043 1044
 * gtk_icon_size_from_name:
 * @name: the name to look up.
Matthias Clasen's avatar
Matthias Clasen committed
1045
 *
Matthias Clasen's avatar
Matthias Clasen committed
1046
 * Looks up the icon size associated with @name.
1047 1048
 *
 * Return value: (type int): the icon size
Matthias Clasen's avatar
Matthias Clasen committed
1049
 */
1050 1051 1052 1053 1054 1055
GtkIconSize
gtk_icon_size_from_name (const gchar *name)
{
  IconAlias *ia;

  init_icon_sizes ();
Matthias Clasen's avatar
Matthias Clasen committed
1056

1057 1058
  ia = g_hash_table_lookup (icon_aliases, name);

1059
  if (ia && icon_sizes[ia->target].width > 0)
1060 1061 1062 1063 1064
    return ia->target;
  else
    return GTK_ICON_SIZE_INVALID;
}

Matthias Clasen's avatar
Matthias Clasen committed
1065 1066
/**
 * gtk_icon_size_get_name:
1067
 * @size: (type int): a #GtkIconSize.
Matthias Clasen's avatar
Matthias Clasen committed
1068
 * @returns: the name of the given icon size.
Matthias Clasen's avatar
Matthias Clasen committed
1069 1070
 *
 * Gets the canonical name of the given icon size. The returned string
Matthias Clasen's avatar
Matthias Clasen committed
1071
 * is statically allocated and should not be freed.
Matthias Clasen's avatar
Matthias Clasen committed
1072
 */
1073 1074 1075 1076 1077 1078 1079
G_CONST_RETURN gchar*
gtk_icon_size_get_name (GtkIconSize  size)
{
  if (size >= icon_sizes_used)
    return NULL;
  else
    return icon_sizes[size].name;
1080 1081
}

1082 1083
/************************************************************/

1084 1085 1086 1087
/* Icon Set */


static GdkPixbuf *find_in_cache     (GtkIconSet       *icon_set,
1088
                                     GtkStyleContext  *style_context,
1089 1090
                                     GtkTextDirection  direction,
                                     GtkStateType      state,
1091
                                     GtkIconSize       size);
1092
static void       add_to_cache      (GtkIconSet       *icon_set,
1093
                                     GtkStyleContext  *style_context,
1094 1095
                                     GtkTextDirection  direction,
                                     GtkStateType      state,
1096
                                     GtkIconSize       size,
1097
                                     GdkPixbuf        *pixbuf);
1098 1099 1100 1101
/* Clear icon set contents, drop references to all contained
 * GdkPixbuf objects and forget all GtkIconSources. Used to
 * recycle an icon set.
 */
1102 1103 1104 1105 1106
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,
1107
                                     GtkStyleContext  *style_context);
1108
static void       detach_from_style (GtkIconSet       *icon_set,
1109
                                     GtkStyleContext  *style_context);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
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;

1128 1129
/**
 * gtk_icon_set_new:
Matthias Clasen's avatar
Matthias Clasen committed
1130
 *
1131 1132 1133 1134 1135
 * 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.
 *
1136
 * Normally you would use gtk_widget_render_icon_pixbuf() instead of
1137 1138 1139
 * using #GtkIconSet directly. The one case where you'd use
 * #GtkIconSet is to create application-specific icon sets to place in
 * a #GtkIconFactory.
Matthias Clasen's avatar
Matthias Clasen committed
1140
 *
1141
 * Return value: a new #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1142
 */
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
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;
Matthias Clasen's avatar
Matthias Clasen committed
1155

1156 1157 1158
  return icon_set;
}

1159 1160 1161
/**
 * gtk_icon_set_new_from_pixbuf:
 * @pixbuf: a #GdkPixbuf
Matthias Clasen's avatar
Matthias Clasen committed
1162
 *
1163 1164 1165 1166 1167
 * 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.
Matthias Clasen's avatar
Matthias Clasen committed
1168
 *
1169
 * Return value: a new #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1170
 */
1171 1172 1173 1174 1175
GtkIconSet *
gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  GtkIconSet *set;

1176
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1177 1178 1179 1180 1181

  g_return_val_if_fail (pixbuf != NULL, NULL);

  set = gtk_icon_set_new ();

1182
  gtk_icon_source_set_pixbuf (&source, pixbuf);
1183
  gtk_icon_set_add_source (set, &source);
1184
  gtk_icon_source_set_pixbuf (&source, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1185

1186 1187 1188 1189
  return set;
}


1190 1191
/**
 * gtk_icon_set_ref:
1192
 * @icon_set: a #GtkIconSet.
Matthias Clasen's avatar
Matthias Clasen committed
1193
 *
1194
 * Increments the reference count on @icon_set.
Matthias Clasen's avatar
Matthias Clasen committed
1195
 *
1196
 * Return value: @icon_set.
Matthias Clasen's avatar
Matthias Clasen committed
1197
 */
1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
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;
}

1209 1210 1211
/**
 * gtk_icon_set_unref:
 * @icon_set: a #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1212
 *
1213 1214
 * Decrements the reference count on @icon_set, and frees memory
 * if the reference count reaches 0.
Matthias Clasen's avatar
Matthias Clasen committed
1215
 */
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232
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);
        }
1233
      g_slist_free (icon_set->sources);
1234 1235 1236 1237 1238 1239 1240

      clear_cache (icon_set, TRUE);

      g_free (icon_set);
    }
}

1241 1242 1243
G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
                     gtk_icon_set_ref,
                     gtk_icon_set_unref)
1244

1245 1246 1247
/**
 * gtk_icon_set_copy:
 * @icon_set: a #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1248 1249 1250
 *
 * Copies @icon_set by value.
 *
1251 1252
 * Return value: a new #GtkIconSet identical to the first.
 **/
1253 1254 1255 1256 1257
GtkIconSet*
gtk_icon_set_copy (GtkIconSet *icon_set)
{
  GtkIconSet *copy;
  GSList *tmp_list;
Matthias Clasen's avatar
Matthias Clasen committed
1258

1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
  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;
Matthias Clasen's avatar
Matthias Clasen committed
1275

1276 1277 1278 1279
  return copy;
}

static gboolean
1280 1281
sizes_equivalent (GtkIconSize lhs,
                  GtkIconSize rhs)
1282
{
1283 1284 1285 1286 1287 1288 1289 1290
  /* 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;
Matthias Clasen's avatar
Matthias Clasen committed
1291 1292
#else

1293 1294
  gint r_w, r_h, l_w, l_h;

1295 1296
  icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
  icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1297 1298

  return r_w == l_w && r_h == l_h;
1299
#endif
1300 1301
}

1302 1303 1304 1305 1306 1307
static GtkIconSource *
find_best_matching_source (GtkIconSet       *icon_set,
			   GtkTextDirection  direction,
			   GtkStateType      state,
			   GtkIconSize       size,
			   GSList           *failed)
1308 1309 1310
{
  GtkIconSource *source;
  GSList *tmp_list;
Matthias Clasen's avatar
Matthias Clasen committed
1311

1312 1313 1314 1315 1316 1317 1318
  /* 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)
   */
Matthias Clasen's avatar
Matthias Clasen committed
1319

1320 1321 1322 1323 1324
  source = NULL;
  tmp_list = icon_set->sources;
  while (tmp_list != NULL)
    {
      GtkIconSource *s = tmp_list->data;
Matthias Clasen's avatar
Matthias Clasen committed
1325

1326 1327
      if ((s->any_direction || (s->direction == direction)) &&
          (s->any_state || (s->state == state)) &&
1328
          (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1329
        {
1330 1331 1332 1333 1334 1335
	  if (!g_slist_find (failed, s))
	    {
	      source = s;
	      break;
	    }
	}
Matthias Clasen's avatar
Matthias Clasen committed
1336

1337 1338 1339
      tmp_list = g_slist_next (tmp_list);
    }

1340 1341
  return source;
}
Matthias Clasen's avatar
Matthias Clasen committed
1342

1343 1344 1345 1346 1347
static gboolean
ensure_filename_pixbuf (GtkIconSet    *icon_set,
			GtkIconSource *source)
{
  if (source->filename_pixbuf == NULL)
1348
    {
1349
      GError *error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1350

1351
      source->filename_pixbuf = gdk_pixbuf_new_from_file (source->source.filename, &error);
Matthias Clasen's avatar
Matthias Clasen committed
1352

1353 1354 1355 1356 1357 1358 1359
      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);
Matthias Clasen's avatar
Matthias Clasen committed
1360

1361
	  icon_set->sources = g_slist_remove (icon_set->sources, source);
Matthias Clasen's avatar
Matthias Clasen committed
1362

1363
	  gtk_icon_source_free (source);
1364

1365 1366 1367
	  return FALSE;
	}
    }
Matthias Clasen's avatar
Matthias Clasen committed
1368

1369 1370
  return TRUE;
}
Matthias Clasen's avatar
Matthias Clasen committed
1371

1372 1373
static GdkPixbuf *
render_icon_name_pixbuf (GtkIconSource    *icon_source,
1374 1375
			 GtkStyleContext  *context,
			 GtkIconSize       size)
1376 1377 1378 1379 1380 1381 1382 1383
{
  GdkPixbuf *pixbuf;
  GdkPixbuf *tmp_pixbuf;
  GtkIconSource tmp_source;
  GdkScreen *screen;
  GtkIconTheme *icon_theme;
  GtkSettings *settings;
  gint width, height, pixel_size;
Matthias Clasen's avatar
Matthias Clasen committed
1384
  gint *sizes, *s, dist;
1385
  GError *error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1386

1387
  screen = gtk_style_context_get_screen (context);
1388 1389 1390 1391 1392
  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))
    {
1393
      if (size == (GtkIconSize)-1)
Matthias Clasen's avatar
Matthias Clasen committed
1394
	{
Matthias Clasen's avatar
Matthias Clasen committed
1395
	  /* Find an available size close to 48 */
Matthias Clasen's avatar
Matthias Clasen committed
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	  sizes = gtk_icon_theme_get_icon_sizes (icon_theme, icon_source->source.icon_name);
	  dist = 1000;
	  width = height = 48;
	  for (s = sizes; *s; s++)
	    {
	      if (*s == -1)
		{
		  width = height = 48;
		  break;
		}
	      if (*s < 48)
		{
		  if (48 - *s < dist)
		    {
		      width = height = *s;
		      dist = 48 - *s;
		    }
		}
Matthias Clasen's avatar
Matthias Clasen committed
1414
	      else
Matthias Clasen's avatar
Matthias Clasen committed
1415 1416 1417 1418 1419 1420 1421 1422
		{
		  if (*s - 48 < dist)
		    {
		      width = height = *s;
		      dist = *s - 48;
		    }
		}
	    }
Matthias Clasen's avatar
Matthias Clasen committed
1423

Matthias Clasen's avatar
Matthias Clasen committed
1424 1425 1426 1427
	  g_free (sizes);
	}
      else
	{
1428
	  g_warning ("Invalid icon size %u\n", size);
Matthias Clasen's avatar
Matthias Clasen committed
1429 1430
	  width = height = 24;
	}
1431 1432 1433 1434
    }

  pixel_size = MIN (width, height);

1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445
  if (icon_source->direction != GTK_TEXT_DIR_NONE)
    {
      gchar *suffix[3] = { NULL, "-ltr", "-rtl" };
      gchar *names[3];
      GtkIconInfo *info;

      names[0] = g_strconcat (icon_source->source.icon_name, suffix[icon_source->direction], NULL);
      names[1] = icon_source->source.icon_name;
      names[2] = NULL;

      info = gtk_icon_theme_choose_icon (icon_theme,
Benjamin Otte's avatar
Benjamin Otte committed
1446 1447
                                         (const char **) names,
                                         pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1448 1449 1450 1451 1452 1453
      g_free (names[0]);
      if (info)
        {
          tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
          gtk_icon_info_free (info);
        }
1454 1455
      else
        tmp_pixbuf = NULL;
1456 1457 1458 1459 1460 1461 1462 1463
    }
  else
    {
      tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
                                             icon_source->source.icon_name,
                                             pixel_size, 0,
                                             &error);
    }
1464 1465 1466

  if (!tmp_pixbuf)
    {
1467 1468 1469 1470
      g_warning ("Error loading theme icon '%s' for stock: %s",
                 icon_source->source.icon_name, error ? error->message : "");
      if (error)
        g_error_free (error);
1471 1472
      return NULL;
    }
Matthias Clasen's avatar
Matthias Clasen committed
1473

1474 1475 1476 1477
  tmp_source = *icon_source;
  tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
  tmp_source.source.pixbuf = tmp_pixbuf;

1478
  pixbuf = gtk_render_icon_pixbuf (context, &tmp_source, -1);
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489

  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,
1490
			     GtkStyleContext  *context,
1491 1492
			     GtkTextDirection  direction,
			     GtkStateType      state,
1493
			     GtkIconSize       size)
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
{
  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);
Matthias Clasen's avatar
Matthias Clasen committed
1511

1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
      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:
1522
          pixbuf = gtk_render_icon_pixbuf (context, source, size);
1523 1524 1525 1526 1527 1528 1529
	  if (!pixbuf)
	    {
	      g_warning ("Failed to render icon");
	      failed = g_slist_prepend (failed, source);
	    }
	  break;
	case GTK_ICON_SOURCE_ICON_NAME:
1530
	case GTK_ICON_SOURCE_STATIC_ICON_NAME:
1531
          pixbuf = render_icon_name_pixbuf (source, context, size);
1532 1533 1534 1535 1536 1537 <