gtkiconfactory.c 86.7 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
 * 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
Javier Jardón's avatar
Javier Jardón committed
15
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16 17 18 19 20 21
 */

/*
 * 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
22
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
23 24
 */

25
#include "config.h"
26

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

31
#include "gtkiconfactory.h"
32
#include "gtkiconcache.h"
33
#include "gtkdebug.h"
34
#include "gtkicontheme.h"
35
#include "gtksettingsprivate.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
#include "gtktypebuiltins.h"
42
#include "deprecated/gtkstyle.h"
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144

/**
 * SECTION:gtkiconfactory
 * @Short_description: Manipulating stock icons
 * @Title: Themeable Stock Images
 *
 * Browse the available stock icons in the list of stock IDs found <link
 * linkend="gtk-Stock-Items">here</link>. You can also use
 * the <application>gtk-demo</application> application for this purpose.
 *
 * An icon factory manages a collection of #GtkIconSet; a #GtkIconSet manages a
 * set of variants of a 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 #GtkStyle has a
 * list of #GtkIconFactory derived from the current 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.
 *
 * To display an icon, always use gtk_style_lookup_icon_set() on the widget that
 * will display the icon, or the convenience function
 * gtk_widget_render_icon(). These functions take the theme into account when
 * looking up the icon to use for a given stock ID.
 *
 * <refsect2 id="GtkIconFactory-BUILDER-UI">
 * <title>GtkIconFactory as GtkBuildable</title>
 * <para>
 * GtkIconFactory supports a custom &lt;sources&gt; element, which can contain
 * multiple &lt;source&gt; elements.
 * The following attributes are allowed:
 * <variablelist>
 * <varlistentry>
 * <term>stock-id</term>
 * <listitem><para>
 * The stock id of the source, a string.
 * This attribute is mandatory
 * </para></listitem>
 * </varlistentry>
 * <varlistentry>
 * <term>filename</term>
 * <listitem><para>
 * The filename of the source, a string.
 * This attribute is optional
 * </para></listitem>
 * </varlistentry>
 * <varlistentry>
 * <term>icon-name</term>
 * <listitem><para>
 * The icon name for the source, a string.
 * This attribute is optional.
 * </para></listitem>
 * </varlistentry>
 * <varlistentry>
 * <term>size</term>
 * <listitem><para>
 * Size of the icon, a #GtkIconSize enum value.
 * This attribute is optional.
 * </para></listitem>
 * </varlistentry>
 * <varlistentry>
 * <term>direction</term>
 * <listitem><para>
 * Direction of the source, a #GtkTextDirection enum value.
 * This attribute is optional.
 * </para></listitem>
 * </varlistentry>
 * <varlistentry>
 * <term>state</term>
 * <listitem><para>
 * State of the source, a #GtkStateType enum value.
 * This attribute is optional.
 * </para></listitem>
 * </varlistentry>
 * </variablelist>
 * <example>
 * <title>A #GtkIconFactory UI definition fragment.</title>
 * <programlisting><![CDATA[
 * <object class="GtkIconFactory" id="iconfactory1">
 *   <sources>
 *     <source stock-id="apple-red" filename="apple-red.png"/>
 *   </sources>
 * </object>
 * <object class="GtkWindow" id="window1">
 *   <child>
 *     <object class="GtkButton" id="apple_button">
 *       <property name="label">apple-red</property>
 *       <property name="use-stock">True</property>
 *     </object>
 *   </child>
 * </object>
 * ]]>
 * </programlisting>
 * </example>
 * </para>
 * </refsect2>
 */


145 146
static GSList *all_icon_factories = NULL;

147
struct _GtkIconFactoryPrivate
148 149 150 151
{
  GHashTable *icons;
};

152 153 154
typedef enum {
  GTK_ICON_SOURCE_EMPTY,
  GTK_ICON_SOURCE_ICON_NAME,
155
  GTK_ICON_SOURCE_STATIC_ICON_NAME,
156 157 158 159
  GTK_ICON_SOURCE_FILENAME,
  GTK_ICON_SOURCE_PIXBUF
} GtkIconSourceType;

160 161
struct _GtkIconSource
{
162
  GtkIconSourceType type;
Matthias Clasen's avatar
Matthias Clasen committed
163

164 165 166 167 168
  union {
    gchar *icon_name;
    gchar *filename;
    GdkPixbuf *pixbuf;
  } source;
169

170
  GdkPixbuf *filename_pixbuf;
171 172 173 174 175 176 177 178 179 180 181 182 183 184

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

185

186 187 188 189 190 191 192 193 194 195 196 197 198 199
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);
200 201
static void gtk_icon_factory_finalize   (GObject             *object);
static void get_default_icons           (GtkIconFactory      *icon_factory);
202
static void icon_source_clear           (GtkIconSource       *source);
203

204 205 206 207
static GtkIconSize icon_size_register_intern (const gchar *name,
					      gint         width,
					      gint         height);

208 209 210 211 212
#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 }

213 214 215
G_DEFINE_TYPE_WITH_CODE (GtkIconFactory, gtk_icon_factory, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_icon_factory_buildable_init))
216 217 218 219

static void
gtk_icon_factory_init (GtkIconFactory *factory)
{
220
  GtkIconFactoryPrivate *priv;
221 222 223

  factory->priv = G_TYPE_INSTANCE_GET_PRIVATE (factory,
                                               GTK_TYPE_ICON_FACTORY,
224
                                               GtkIconFactoryPrivate);
225 226 227
  priv = factory->priv;

  priv->icons = g_hash_table_new (g_str_hash, g_str_equal);
228
  all_icon_factories = g_slist_prepend (all_icon_factories, factory);
229 230 231 232 233 234
}

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

236
  object_class->finalize = gtk_icon_factory_finalize;
237

238
  g_type_class_add_private (klass, sizeof (GtkIconFactoryPrivate));
239 240
}

241 242 243 244 245 246 247
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;
}

248 249 250 251 252 253 254 255 256 257 258
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);
259
  GtkIconFactoryPrivate *priv = factory->priv;
260

261
  all_icon_factories = g_slist_remove (all_icon_factories, factory);
Matthias Clasen's avatar
Matthias Clasen committed
262

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

265
  g_hash_table_destroy (priv->icons);
Matthias Clasen's avatar
Matthias Clasen committed
266

Matthias Clasen's avatar
Matthias Clasen committed
267
  G_OBJECT_CLASS (gtk_icon_factory_parent_class)->finalize (object);
268 269
}

270 271 272 273
/**
 * gtk_icon_factory_new:
 *
 * Creates a new #GtkIconFactory. An icon factory manages a collection
274
 * of #GtkIconSet<!-- -->s; a #GtkIconSet manages a set of variants of a
275 276 277
 * 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
278
 * #GtkStyle has a list of #GtkIconFactory<!-- -->s derived from the current
279 280 281 282 283 284 285
 * 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
286
 *
287
 * Return value: a new #GtkIconFactory
Matthias Clasen's avatar
Matthias Clasen committed
288
 */
289 290 291
GtkIconFactory*
gtk_icon_factory_new (void)
{
Manish Singh's avatar
Manish Singh committed
292
  return g_object_new (GTK_TYPE_ICON_FACTORY, NULL);
293 294
}

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
/**
 * 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
311
 */
312 313 314 315 316
void
gtk_icon_factory_add (GtkIconFactory *factory,
                      const gchar    *stock_id,
                      GtkIconSet     *icon_set)
{
317
  GtkIconFactoryPrivate *priv = factory->priv;
318 319 320 321 322
  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
323
  g_return_if_fail (icon_set != NULL);
324

325
  g_hash_table_lookup_extended (priv->icons, stock_id,
326 327 328 329
                                &old_key, &old_value);

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

331 332 333 334
  gtk_icon_set_ref (icon_set);

  /* GHashTable key memory management is so fantastically broken. */
  if (old_key)
335
    g_hash_table_insert (priv->icons, old_key, icon_set);
336
  else
337
    g_hash_table_insert (priv->icons, g_strdup (stock_id), icon_set);
338 339 340 341 342

  if (old_value)
    gtk_icon_set_unref (old_value);
}

343 344 345 346
/**
 * gtk_icon_factory_lookup:
 * @factory: a #GtkIconFactory
 * @stock_id: an icon name
Matthias Clasen's avatar
Matthias Clasen committed
347
 *
348 349 350 351 352
 * 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
353
 *
354
 * Return value: (transfer none): icon set of @stock_id.
Matthias Clasen's avatar
Matthias Clasen committed
355
 */
356 357 358 359
GtkIconSet *
gtk_icon_factory_lookup (GtkIconFactory *factory,
                         const gchar    *stock_id)
{
360
  GtkIconFactoryPrivate *priv;
361

362 363
  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
364

365 366 367
  priv = factory->priv;

  return g_hash_table_lookup (priv->icons, stock_id);
368 369 370 371 372
}

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

373 374 375
/**
 * gtk_icon_factory_add_default:
 * @factory: a #GtkIconFactory
Matthias Clasen's avatar
Matthias Clasen committed
376
 *
377 378 379 380 381 382
 * 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
383
 */
384 385 386 387 388
void
gtk_icon_factory_add_default (GtkIconFactory *factory)
{
  g_return_if_fail (GTK_IS_ICON_FACTORY (factory));

Manish Singh's avatar
Manish Singh committed
389
  g_object_ref (factory);
Matthias Clasen's avatar
Matthias Clasen committed
390

391 392 393
  default_factories = g_slist_prepend (default_factories, factory);
}

394 395 396 397 398 399 400
/**
 * 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
401
 */
402 403 404 405 406 407 408
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
409
  g_object_unref (factory);
410 411
}

412 413
void
_gtk_icon_factory_ensure_default_icons (void)
414 415 416 417 418 419 420 421 422
{
  if (gtk_default_icons == NULL)
    {
      gtk_default_icons = gtk_icon_factory_new ();

      get_default_icons (gtk_default_icons);
    }
}

423 424 425 426 427 428 429 430 431
/**
 * 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
432
 *
433
 * Return value: (transfer none): a #GtkIconSet, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
434
 */
435 436 437 438 439 440
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
441

442 443 444 445 446 447 448 449 450
  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
451

452 453 454
      tmp_list = g_slist_next (tmp_list);
    }

455
  _gtk_icon_factory_ensure_default_icons ();
Matthias Clasen's avatar
Matthias Clasen committed
456

457 458 459
  return gtk_icon_factory_lookup (gtk_default_icons, stock_id);
}

460
static void
461
register_stock_icon (GtkIconFactory *factory,
462 463
		     const gchar    *stock_id,
                     const gchar    *icon_name)
464
{
465 466
  GtkIconSet *set = gtk_icon_set_new ();
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
467

468
  source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
469 470
  source.source.icon_name = (gchar *)icon_name;
  source.direction = GTK_TEXT_DIR_NONE;
471
  gtk_icon_set_add_source (set, &source);
Matthias Clasen's avatar
Matthias Clasen committed
472

473 474
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
475
}
476

477 478 479
static void
register_bidi_stock_icon (GtkIconFactory *factory,
			  const gchar    *stock_id,
480
                          const gchar    *icon_name)
481
{
482 483
  GtkIconSet *set = gtk_icon_set_new ();
  GtkIconSource source = GTK_ICON_SOURCE_INIT (FALSE, TRUE, TRUE);
484

485
  source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
486
  source.source.icon_name = (gchar *)icon_name;
487
  source.direction = GTK_TEXT_DIR_LTR;
488
  gtk_icon_set_add_source (set, &source);
Matthias Clasen's avatar
Matthias Clasen committed
489

490
  source.type = GTK_ICON_SOURCE_STATIC_ICON_NAME;
491
  source.source.icon_name = (gchar *)icon_name;
492 493
  source.direction = GTK_TEXT_DIR_RTL;
  gtk_icon_set_add_source (set, &source);
Matthias Clasen's avatar
Matthias Clasen committed
494

495 496
  gtk_icon_factory_add (factory, stock_id, set);
  gtk_icon_set_unref (set);
497 498
}

499 500 501 502
static void
get_default_icons (GtkIconFactory *factory)
{
  /* KEEP IN SYNC with gtkstock.c */
503

504 505
  register_stock_icon (factory, GTK_STOCK_DIALOG_AUTHENTICATION, "dialog-password");
  register_stock_icon (factory, GTK_STOCK_DIALOG_ERROR, "dialog-error");
506
  register_stock_icon (factory, GTK_STOCK_DIALOG_INFO, "dialog-information");
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  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");
537
  register_stock_icon (factory, GTK_STOCK_INFO, "dialog-information");
538 539 540 541 542 543 544 545 546 547 548 549 550
  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");
551
  register_stock_icon (factory, GTK_STOCK_PREFERENCES, GTK_STOCK_PREFERENCES);
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
  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");
584
  register_stock_icon (factory, GTK_STOCK_FILE, "text-x-generic");
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
  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);
608 609
}

610 611 612
/************************************************************
 *                    Icon size handling                    *
 ************************************************************/
613 614 615 616 617

typedef struct _IconSize IconSize;

struct _IconSize
{
618
  gint size;
619
  gchar *name;
Matthias Clasen's avatar
Matthias Clasen committed
620

621 622
  gint width;
  gint height;
623 624
};

625
typedef struct _IconAlias IconAlias;
626

627
struct _IconAlias
628
{
629 630 631
  gchar *name;
  gint   target;
};
632

633 634 635 636 637 638 639 640
typedef struct _SettingsIconSize SettingsIconSize;

struct _SettingsIconSize
{
  gint width;
  gint height;
};

641 642 643 644
static GHashTable *icon_aliases = NULL;
static IconSize *icon_sizes = NULL;
static gint      icon_sizes_allocated = 0;
static gint      icon_sizes_used = 0;
645 646

static void
647
init_icon_sizes (void)
648
{
649
  if (icon_sizes == NULL)
650
    {
651
#define NUM_BUILTIN_SIZES 7
652
      gint i;
653

654
      icon_aliases = g_hash_table_new (g_str_hash, g_str_equal);
Matthias Clasen's avatar
Matthias Clasen committed
655

656 657 658 659 660 661 662 663 664 665 666 667 668 669
      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
670

671 672 673 674 675 676 677
      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
678 679
      icon_sizes[GTK_ICON_SIZE_BUTTON].width = 20;
      icon_sizes[GTK_ICON_SIZE_BUTTON].height = 20;
680 681 682 683 684

      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
685

686 687 688 689
      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;
690

691 692 693 694 695
      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;

696 697 698 699
      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;
700

701
      g_assert ((GTK_ICON_SIZE_DIALOG + 1) == NUM_BUILTIN_SIZES);
702

703 704 705 706 707
      /* 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
708

709 710
          ++i;
        }
Matthias Clasen's avatar
Matthias Clasen committed
711

712
#undef NUM_BUILTIN_SIZES
713 714 715
    }
}

716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
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
761

762 763 764 765 766 767 768 769 770 771 772
  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
773

774 775 776 777 778 779 780 781 782 783 784 785 786
  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
787

788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820
  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
821

822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900
      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);

901
  gtk_style_context_reset_widgets (_gtk_settings_get_screen (settings));
902 903 904 905 906 907 908 909 910
}

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
911

912 913
  icon_size_set_all_from_settings (settings);
}
Matthias Clasen's avatar
Matthias Clasen committed
914

915
static gboolean
916 917 918 919 920 921 922 923
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
924

925 926
  init_icon_sizes ();

927
  if (size == (GtkIconSize)-1)
928
    return FALSE;
929

930
  if (size >= icon_sizes_used)
931 932 933 934 935 936 937 938
    return FALSE;

  if (size == GTK_ICON_SIZE_INVALID)
    return FALSE;

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

940 941
      settings_sizes = get_settings_sizes (settings, &initial);

942 943
      if (initial)
	icon_sizes_init_for_settings (settings);
Matthias Clasen's avatar
Matthias Clasen committed
944

945 946 947
      if (size < settings_sizes->len)
	{
	  SettingsIconSize *settings_size;
Matthias Clasen's avatar
Matthias Clasen committed
948

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

951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
	  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:
967
 * @settings: a #GtkSettings object, used to determine
968
 *   which set of user preferences to used.
969
 * @size: (type int): an icon size
970 971
 * @width: (out): location to store icon width
 * @height: (out): location to store icon height
972 973
 *
 * Obtains the pixel size of a semantic icon size, possibly
Matthias Clasen's avatar
Matthias Clasen committed
974
 * modified by user preferences for a particular
Matthias Clasen's avatar
Matthias Clasen committed
975
 * #GtkSettings. Normally @size would be
976
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
977
 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
978 979 980 981 982
 * 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
983
 *
984
 * Return value: %TRUE if @size was a valid size
985 986
 *
 * Since: 2.2
Matthias Clasen's avatar
Matthias Clasen committed
987
 */
988 989 990
gboolean
gtk_icon_size_lookup_for_settings (GtkSettings *settings,
				   GtkIconSize  size,
991 992
				   gint        *width,
				   gint        *height)
993 994 995
{
  g_return_val_if_fail (GTK_IS_SETTINGS (settings), FALSE);

996
  return icon_size_lookup_intern (settings, size, width, height);
997 998
}

999 1000
/**
 * gtk_icon_size_lookup:
1001
 * @size: (type int): an icon size
1002 1003
 * @width: (out): location to store icon width
 * @height: (out): location to store icon height
1004
 *
1005 1006 1007 1008
 * 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
1009
 * #GTK_ICON_SIZE_MENU, #GTK_ICON_SIZE_BUTTON, etc.  This function
1010
 * isn't normally needed, gtk_widget_render_icon_pixbuf() is the usual
1011 1012 1013
 * 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
1014 1015
 * are free to render the pixbuf however they like, including changing
 * the usual size.
Matthias Clasen's avatar
Matthias Clasen committed
1016
 *
1017
 * Return value: %TRUE if @size was a valid size
Matthias Clasen's avatar
Matthias Clasen committed
1018
 */
1019
gboolean
1020
gtk_icon_size_lookup (GtkIconSize  size,
1021 1022 1023
                      gint        *widthp,
                      gint        *heightp)
{
1024 1025
  GTK_NOTE (MULTIHEAD,
	    g_warning ("gtk_icon_size_lookup ()) is not multihead safe"));
1026

1027 1028 1029
  return gtk_icon_size_lookup_for_settings (gtk_settings_get_default (),
					    size, widthp, heightp);
}
1030

1031 1032 1033 1034 1035 1036 1037
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
1038

1039
  init_icon_sizes ();
1040

1041 1042 1043 1044 1045 1046
  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;
    }
1047

1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
  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;
1073 1074
}

1075 1076
/**
 * gtk_icon_size_register:
1077
 * @name: name of the icon size
1078 1079 1080 1081
 * @width: the icon width
 * @height: the icon height
 *
 * Registers a new icon size, along the same lines as #GTK_ICON_SIZE_MENU,
1082 1083
 * etc. Returns the integer value for the size.
 *
1084
 * Returns: (type int): integer value representing the size
Matthias Clasen's avatar
Matthias Clasen committed
1085
 */
1086 1087
GtkIconSize
gtk_icon_size_register (const gchar *name,
1088 1089 1090
                        gint         width,
                        gint         height)
{
1091 1092 1093
  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
1094

1095
  return icon_size_register_intern (name, width, height);
1096 1097
}

1098 1099 1100
/**
 * gtk_icon_size_register_alias:
 * @alias: an alias for @target
1101
 * @target: (type int): an existing icon size
1102 1103 1104 1105
 *
 * 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
1106
 */
1107 1108
void
gtk_icon_size_register_alias (const gchar *alias,
1109
                              GtkIconSize  target)
1110
{
1111
  IconAlias *ia;
Matthias Clasen's avatar
Matthias Clasen committed
1112

1113 1114 1115 1116
  g_return_if_fail (alias != NULL);

  init_icon_sizes ();

1117
  if (!icon_size_lookup_intern (NULL, target, NULL, NULL))
1118
    g_warning ("gtk_icon_size_register_alias: Icon size %u does not exist", target);
1119

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
  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);
    }
1140 1141
}

Matthias Clasen's avatar
Matthias Clasen committed
1142
/**
Matthias Clasen's avatar
Matthias Clasen committed
1143 1144
 * gtk_icon_size_from_name:
 * @name: the name to look up.
Matthias Clasen's avatar
Matthias Clasen committed
1145
 *
Matthias Clasen's avatar
Matthias Clasen committed
1146
 * Looks up the icon size associated with @name.
1147 1148
 *
 * Return value: (type int): the icon size
Matthias Clasen's avatar
Matthias Clasen committed
1149
 */
1150 1151 1152 1153 1154 1155
GtkIconSize
gtk_icon_size_from_name (const gchar *name)
{
  IconAlias *ia;

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

1157 1158
  ia = g_hash_table_lookup (icon_aliases, name);

1159
  if (ia && icon_sizes[ia->target].width > 0)
1160 1161 1162 1163 1164
    return ia->target;
  else
    return GTK_ICON_SIZE_INVALID;
}

Matthias Clasen's avatar
Matthias Clasen committed
1165 1166
/**
 * gtk_icon_size_get_name:
1167
 * @size: (type int): a #GtkIconSize.
Matthias Clasen's avatar
Matthias Clasen committed
1168 1169
 *
 * Gets the canonical name of the given icon size. The returned string
Matthias Clasen's avatar
Matthias Clasen committed
1170
 * is statically allocated and should not be freed.
Matthias Clasen's avatar
Matthias Clasen committed
1171 1172
 *
 * Returns: the name of the given icon size.
Matthias Clasen's avatar
Matthias Clasen committed
1173
 */
1174
const gchar*
1175 1176 1177 1178 1179 1180
gtk_icon_size_get_name (GtkIconSize  size)
{
  if (size >= icon_sizes_used)
    return NULL;
  else
    return icon_sizes[size].name;
1181 1182
}

1183 1184
/************************************************************/

1185 1186 1187 1188
/* Icon Set */


static GdkPixbuf *find_in_cache     (GtkIconSet       *icon_set,
1189
                                     GtkStyleContext  *style_context,
1190 1191
                                     GtkTextDirection  direction,
                                     GtkStateType      state,
1192
                                     GtkIconSize       size);
1193
static void       add_to_cache      (GtkIconSet       *icon_set,
1194
                                     GtkStyleContext  *style_context,
1195 1196
                                     GtkTextDirection  direction,
                                     GtkStateType      state,
1197
                                     GtkIconSize       size,
1198
                                     GdkPixbuf        *pixbuf);
1199 1200 1201 1202
/* Clear icon set contents, drop references to all contained
 * GdkPixbuf objects and forget all GtkIconSources. Used to
 * recycle an icon set.
 */
1203 1204 1205 1206 1207
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,
1208
                                     GtkStyleContext  *style_context);
1209
static void       detach_from_style (GtkIconSet       *icon_set,
1210
                                     GtkStyleContext  *style_context);
1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228
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;

1229 1230
/**
 * gtk_icon_set_new:
Matthias Clasen's avatar
Matthias Clasen committed
1231
 *
1232 1233 1234 1235 1236
 * 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.
 *
1237
 * Normally you would use gtk_widget_render_icon_pixbuf() instead of
1238 1239 1240
 * 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
1241
 *
1242
 * Return value: a new #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1243
 */
1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
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
1256

1257 1258 1259
  return icon_set;
}

1260 1261 1262
/**
 * gtk_icon_set_new_from_pixbuf:
 * @pixbuf: a #GdkPixbuf
Matthias Clasen's avatar
Matthias Clasen committed
1263
 *
1264 1265 1266 1267 1268
 * 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
1269
 *
1270
 * Return value: a new #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1271
 */
1272 1273 1274 1275 1276
GtkIconSet *
gtk_icon_set_new_from_pixbuf (GdkPixbuf *pixbuf)
{
  GtkIconSet *set;

1277
  GtkIconSource source = GTK_ICON_SOURCE_INIT (TRUE, TRUE, TRUE);
1278 1279 1280 1281 1282

  g_return_val_if_fail (pixbuf != NULL, NULL);

  set = gtk_icon_set_new ();

1283
  gtk_icon_source_set_pixbuf (&source, pixbuf);
1284
  gtk_icon_set_add_source (set, &source);
1285
  gtk_icon_source_set_pixbuf (&source, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1286

1287 1288 1289 1290
  return set;
}


1291 1292
/**
 * gtk_icon_set_ref:
1293
 * @icon_set: a #GtkIconSet.
Matthias Clasen's avatar
Matthias Clasen committed
1294
 *
1295
 * Increments the reference count on @icon_set.
Matthias Clasen's avatar
Matthias Clasen committed
1296
 *
1297
 * Return value: @icon_set.
Matthias Clasen's avatar
Matthias Clasen committed
1298
 */
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309
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;
}

1310 1311 1312
/**
 * gtk_icon_set_unref:
 * @icon_set: a #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1313
 *
1314 1315
 * Decrements the reference count on @icon_set, and frees memory
 * if the reference count reaches 0.
Matthias Clasen's avatar
Matthias Clasen committed
1316
 */
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
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);
        }
1334
      g_slist_free (icon_set->sources);
1335 1336 1337 1338 1339 1340 1341

      clear_cache (icon_set, TRUE);

      g_free (icon_set);
    }
}

1342 1343 1344
G_DEFINE_BOXED_TYPE (GtkIconSet, gtk_icon_set,
                     gtk_icon_set_ref,
                     gtk_icon_set_unref)
1345

1346 1347 1348
/**
 * gtk_icon_set_copy:
 * @icon_set: a #GtkIconSet
Matthias Clasen's avatar
Matthias Clasen committed
1349 1350 1351
 *
 * Copies @icon_set by value.
 *
1352 1353
 * Return value: a new #GtkIconSet identical to the first.
 **/
1354 1355 1356 1357 1358
GtkIconSet*
gtk_icon_set_copy (GtkIconSet *icon_set)
{
  GtkIconSet *copy;
  GSList *tmp_list;
Matthias Clasen's avatar
Matthias Clasen committed
1359

1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375
  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
1376

1377 1378 1379 1380
  return copy;
}

static gboolean
1381 1382
sizes_equivalent (GtkIconSize lhs,
                  GtkIconSize rhs)
1383
{
1384 1385 1386 1387 1388 1389 1390 1391
  /* 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
1392 1393
#else

1394 1395
  gint r_w, r_h, l_w, l_h;

1396 1397
  icon_size_lookup_intern (NULL, rhs, &r_w, &r_h);
  icon_size_lookup_intern (NULL, lhs, &l_w, &l_h);
1398 1399

  return r_w == l_w && r_h == l_h;
1400
#endif
1401 1402
}

1403 1404 1405 1406 1407 1408
static GtkIconSource *
find_best_matching_source (GtkIconSet       *icon_set,
			   GtkTextDirection  direction,
			   GtkStateType      state,
			   GtkIconSize       size,
			   GSList           *failed)
1409 1410 1411
{
  GtkIconSource *source;
  GSList *tmp_list;
Matthias Clasen's avatar
Matthias Clasen committed
1412

1413 1414 1415 1416 1417 1418 1419
  /* 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
1420

1421 1422 1423 1424 1425
  source = NULL;
  tmp_list = icon_set->sources;
  while (tmp_list != NULL)
    {
      GtkIconSource *s = tmp_list->data;
Matthias Clasen's avatar
Matthias Clasen committed
1426

1427 1428
      if ((s->any_direction || (s->direction == direction)) &&
          (s->any_state || (s->state == state)) &&
1429
          (s->any_size || size == (GtkIconSize)-1 || (sizes_equivalent (size, s->size))))
1430
        {
1431 1432 1433 1434 1435 1436
	  if (!g_slist_find (failed, s))
	    {
	      source = s;
	      break;
	    }
	}
Matthias Clasen's avatar
Matthias Clasen committed
1437

1438 1439 1440
      tmp_list = g_slist_next (tmp_list);
    }

1441 1442
  return source;
}
Matthias Clasen's avatar
Matthias Clasen committed
1443

1444 1445 1446 1447 1448
static gboolean
ensure_filename_pixbuf (GtkIconSet    *icon_set,
			GtkIconSource *source)
{
  if (source->filename_pixbuf == NULL)
1449
    {
1450
      GError *error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1451

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

1454 1455 1456 1457 1458
      if (source->filename_pixbuf == NULL)
	{
	  /* Remove this icon source so we don't keep trying to
	   * load it.
	   */
1459
	  g_warning ("Error loading icon: %s", error->message);
1460
	  g_error_free (error);
Matthias Clasen's avatar
Matthias Clasen committed
1461

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

1464
	  gtk_icon_source_free (source);
1465

1466 1467 1468
	  return FALSE;
	}
    }
Matthias Clasen's avatar
Matthias Clasen committed
1469

1470 1471
  return TRUE;
}
Matthias Clasen's avatar
Matthias Clasen committed
1472

1473 1474
static GdkPixbuf *
render_icon_name_pixbuf (GtkIconSource    *icon_source,
1475 1476
			 GtkStyleContext  *context,
			 GtkIconSize       size)
1477 1478 1479 1480 1481 1482 1483 1484
{
  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
1485
  gint *sizes, *s, dist;
1486
  GError *error = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1487

1488
  screen = gtk_style_context_get_screen (context);
1489 1490 1491 1492 1493
  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))
    {
1494
      if (size == (GtkIconSize)-1)
Matthias Clasen's avatar
Matthias Clasen committed
1495
	{
Matthias Clasen's avatar
Matthias Clasen committed
1496
	  /* Find an available size close to 48 */
Matthias Clasen's avatar
Matthias Clasen committed
1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
	  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
1515
	      else
Matthias Clasen's avatar
Matthias Clasen committed
1516 1517 1518 1519 1520 1521 1522 1523
		{
		  if (*s - 48 < dist)
		    {
		      width = height = *s;
		      dist = *s - 48;
		    }
		}
	    }
Matthias Clasen's avatar
Matthias Clasen committed
1524

Matthias Clasen's avatar
Matthias Clasen committed
1525 1526 1527 1528
	  g_free (sizes);
	}
      else
	{
1529
	  g_warning ("Invalid icon size %u\n", size);
Matthias Clasen's avatar
Matthias Clasen committed
1530 1531
	  width = height = 24;
	}
1532 1533 1534 1535
    }

  pixel_size = MIN (width, height);

1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546
  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
1547 1548
                                         (const char **) names,
                                         pixel_size, GTK_ICON_LOOKUP_USE_BUILTIN);
1549 1550 1551 1552 1553 1554
      g_free (names[0]);
      if (info)
        {
          tmp_pixbuf = gtk_icon_info_load_icon (info, &error);
          gtk_icon_info_free (info);
        }
1555 1556
      else
        tmp_pixbuf = NULL;
1557 1558 1559 1560 1561 1562 1563 1564
    }
  else
    {
      tmp_pixbuf = gtk_icon_theme_load_icon (icon_theme,
                                             icon_source->source.icon_name,
                                             pixel_size, 0,
                                             &error);
    }
1565 1566 1567

  if (!tmp_pixbuf)
    {
1568 1569 1570 1571
      g_warning ("Error loading theme icon '%s' for stock: %s",
                 icon_source->source.icon_name, error ? error->message : "");
      if (error)
        g_error_free (error);