gtkiconfactory.c 83.1 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"
Manish Singh's avatar
Manish Singh committed
28 29 30
#include <stdlib.h>
#include <errno.h>
#include <string.h>
31
#include "gtkiconfactory.h"
32
#include "gtkiconcache.h"
33
#include "gtkdebug.h"
34
#include "gtkicontheme.h"
35
#include "gtksettings.h"
36
#include "gtkstock.h"
37
#include "gtkwidget.h"
Havoc Pennington's avatar
Havoc Pennington committed
38
#include "gtkintl.h"
39 40
#include "gtkbuildable.h"
#include "gtkbuilderprivate.h"
41

42

43 44
static GSList *all_icon_factories = NULL;

45
struct _GtkIconFactoryPrivate
46 47 48 49
{
  GHashTable *icons;
};

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

58 59
struct _GtkIconSource
{
60
  GtkIconSourceType type;
Matthias Clasen's avatar
Matthias Clasen committed
61

62 63 64 65 66
  union {
    gchar *icon_name;
    gchar *filename;
    GdkPixbuf *pixbuf;
  } source;
67

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

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

83

84 85 86 87 88 89 90 91 92 93 94 95 96 97
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);
98 99
static void gtk_icon_factory_finalize   (GObject             *object);
static void get_default_icons           (GtkIconFactory      *icon_factory);
100
static void icon_source_clear           (GtkIconSource       *source);
101

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

106 107 108 109 110
#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 }

111 112 113
G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_icon_factory_buildable_init))
114 115 116 117

static void
gtk_icon_factory_init (GtkIconFactory *factory)
{
118
  GtkIconFactoryPrivate *priv;
119 120 121

  factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
                                               GTK_TYPE_ICON_FACTORY,
122
                                               GtkIconFactoryPrivate);
123 124 125
  priv = factory->priv;

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

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

134
  object_class->finalize = gtk_icon_factory_finalize;
135

136
  g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
137 138
}

139 140 141 142 143 144 145
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;
}

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

159
  all_icon_factories = g_slist_remove (all_icon_factories, factory);
Matthias Clasen's avatar
Matthias Clasen committed
160

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

163
  g_hash_table_destroy (priv->icons);
Matthias Clasen's avatar
Matthias Clasen committed
164

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

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

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

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

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

229 230 231 232
  gtk_icon_set_ref (icon_set);

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

  if (old_value)
    gtk_icon_set_unref (old_value);
}

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

260 261
  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
262

263 264 265
  priv = factory->priv;

  return g_hash_table_lookup (priv->icons, stock_id);
266 267 268 269 270
}

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

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

Manish Singh's avatar
Manish Singh committed
287
  g_object_ref (factory);
Matthias Clasen's avatar
Matthias Clasen committed
288

289 290 291
  default_factories = g_slist_prepend (default_factories, factory);
}

292 293 294 295 296 297 298
/**
 * 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
299
 */
300 301 302 303 304 305 306
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
307
  g_object_unref (factory);
308 309
}

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

      get_default_icons (gtk_default_icons);
    }
}

321 322 323 324 325 326 327 328 329
/**
 * 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
330
 *
331
 * Return value: a #GtkIconSet, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
332
 */
333 334 335 336 337 338
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
339

340 341 342 343 344 345 346 347 348
  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
349

350 351 352
      tmp_list = g_slist_next (tmp_list);
    }

353
  _gtk_icon_factory_ensure_default_icons ();
Matthias Clasen's avatar
Matthias Clasen committed
354

355 356 357
  return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
}

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

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

371 372
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
373
}
374

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

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

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

393 394
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
395 396
}

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

402 403
  register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
  register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
404
  register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
405 406 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
  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");
435
  register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
436 437 438 439 440 441 442 443 444 445 446 447 448
  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");
449
  register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  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);
506 507
}

508 509 510
/************************************************************
 *                    Icon size handling                    *
 ************************************************************/
511 512 513 514 515

typedef struct _IconSize IconSize;

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

519 520
  gint width;
  gint height;
521 522
};

523
typedef struct _IconAlias IconAlias;
524

525
struct _IconAlias
526
{
527 528 529
  gchar *name;
  gint   target;
};
530

531 532 533 534 535 536 537 538
typedef struct _SettingsIconSize SettingsIconSize;

struct _SettingsIconSize
{
  gint width;
  gint height;
};

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

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

552
      icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
Matthias Clasen's avatar
Matthias Clasen committed
553

554 555 556 557 558 559 560 561 562 563 564 565 566 567
      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
568

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

      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
583

584 585 586 587
      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;
588

589 590 591 592 593
      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;

594 595 596 597
      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;
598

599
      g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
600

601 602 603 604 605
      /* 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
606

607 608
          ++i;
        }
Matthias Clasen's avatar
Matthias Clasen committed
609

610
#undef NUM_BUILTIN_SIZES
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
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
659

660 661 662 663 664 665 666 667 668 669 670
  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
671

672 673 674 675 676 677 678 679 680 681 682 683 684
  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
685

686 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
  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
719

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
      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);

799
  gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
800 801 802 803 804 805 806 807 808
}

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
809

810 811
  icon_size_set_all_from_settings (settings);
}
Matthias Clasen's avatar
Matthias Clasen committed
812

813
static gboolean
814 815 816 817 818 819 820 821
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
822

823 824
  init_icon_sizes ();

825
  if (size == (GtkIconSize)-1)
826
    return FALSE;
827

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

  if (size == GTK_ICON_SIZE_INVALID)
    return FALSE;

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

838 839
      settings_sizes = get_settings_sizes (settings, &initial);

840 841
      if (initial)
	icon_sizes_init_for_settings (settings);
Matthias Clasen's avatar
Matthias Clasen committed
842

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

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

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

894
  return icon_size_lookup_intern (settings, size, width, height);
895 896
}

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

925 926 927
  return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
					    size, widthp, heightp);
}
928

929 930 931 932 933 934 935
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
936

937
  init_icon_sizes ();
938

939 940 941 942 943 944
  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;
    }
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
  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;
971 972
}

973 974
/**
 * gtk_icon_size_register:
975
 * @name: name of the icon size
976 977 978 979
 * @width: the icon width
 * @height: the icon height
 *
 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
980 981
 * etc. Returns the integer value for the size.
 *
982
 * Returns: (type int): integer value representing the size
Matthias Clasen's avatar
Matthias Clasen committed
983
 */
984 985
GtkIconSize
gtk_icon_size_register (const gchar *name,
986 987 988
                        gint         width,
                        gint         height)
{
989 990 991
  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
992

993
  return icon_size_register_intern (name, width, height);
994 995
}

996 997 998
/**
 * gtk_icon_size_register_alias:
 * @alias: an alias for @target
999
 * @target: (type int): an existing icon size
1000 1001 1002 1003
 *
 * 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
1004
 */
1005 1006
void
gtk_icon_size_register_alias (const gchar *alias,
1007
                              GtkIconSize  target)
1008
{
1009
  IconAlias *ia;
Matthias Clasen's avatar
Matthias Clasen committed
1010

1011 1012 1013 1014
  g_return_if_fail (alias != NULL);

  init_icon_sizes ();

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

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

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

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

1055 1056
  ia = g_hash_table_lookup (icon_aliases, name);

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

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

1080 1081
/************************************************************/

1082 1083 1084 1085
/* Icon Set */


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

1126 1127
/**
 * gtk_icon_set_new:
Matthias Clasen's avatar
Matthias Clasen committed
1128
 *
1129 1130 1131 1132 1133
 * 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.
 *
1134
 * Normally you would use gtk_widget_render_icon_pixbuf() instead of
1135 1136 1137
 * 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
1138
 *
1139
 * Return value: a new #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1140
 */
1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
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
1153

1154 1155 1156
  return icon_set;
}

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

1174
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1175 1176 1177 1178 1179

  g_return_val_if_fail (pixbuf != NULL, NULL);

  set = gtk_icon_set_new ();

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

1184 1185 1186 1187
  return set;
}


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

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

      clear_cache (icon_set, TRUE);

      g_free (icon_set);
    }
}

1239 1240 1241
G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
                     gtk_icon_set_ref,
                     gtk_icon_set_unref)
1242

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

1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
  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
1273

1274 1275 1276 1277
  return copy;
}

static gboolean
1278 1279
sizes_equivalent (GtkIconSize lhs,
                  GtkIconSize rhs)
1280
{
1281 1282 1283 1284 1285 1286 1287 1288
  /* 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
1289 1290
#else

1291 1292
  gint r_w, r_h, l_w, l_h;

1293 1294
  icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
  icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1295 1296

  return r_w == l_w && r_h == l_h;
1297
#endif
1298 1299
}

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

1310 1311 1312 1313 1314 1315 1316
  /* 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
1317

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

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

1335 1336 1337
      tmp_list = g_slist_next (tmp_list);
    }

1338 1339
  return source;
}
Matthias Clasen's avatar
Matthias Clasen committed
1340

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

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

1351 1352 1353 1354 1355 1356 1357
      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
1358

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

1361
	  gtk_icon_source_free (source);
1362

1363 1364 1365
	  return FALSE;
	}
    }
Matthias Clasen's avatar
Matthias Clasen committed
1366

1367 1368
  return TRUE;
}
Matthias Clasen's avatar
Matthias Clasen committed
1369

1370 1371
static GdkPixbuf *
render_icon_name_pixbuf (GtkIconSource    *icon_source,
1372 1373
			 GtkStyleContext  *context,
			 GtkIconSize       size)
1374 1375 1376 1377 1378 1379 1380 1381
{
  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
1382
  gint *sizes, *s, dist;
1383
  GError *error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1384

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

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

  pixel_size = MIN (width, height);

1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443
  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
1444 1445
                                         (const char **) names,
                                         pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1446 1447 1448 1449 1450 1451
      g_free (names[0]);
      if (info)
        {
          tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
          gtk_icon_info_free (info);
        }
1452 1453
      else
        tmp_pixbuf = NULL;
1454 1455 1456 1457 1458 1459 1460 1461
    }
  else
    {
      tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
                                             icon_source->source.icon_name,
                                             pixel_size, 0,
                                             &error);
    }
1462 1463 1464

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

1472 1473 1474 1475
  tmp_source = *icon_source;
  tmp_source.type = GTK_ICON_SOURCE_PIXBUF;
  tmp_source.source.pixbuf = tmp_pixbuf;

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

  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,
1488
			     GtkStyleContext  *context,
1489 1490
			     GtkTextDirection  direction,
			     GtkStateType      state,
1491
			     GtkIconSize       size)
1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
{
  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
1509

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