gtkstock.c 19.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 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

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

27
#include "config.h"
28 29
#include <string.h>

30
#include "gtkstock.h"
31
#include "gtkiconfactory.h"
32 33
#include "gtkintl.h"
#include <gdk/gdkkeysyms.h>
34
#include "gtkalias.h"
35

36
static GHashTable *translate_hash = NULL;
37 38 39
static GHashTable *stock_hash = NULL;
static void init_stock_hash (void);

40 41 42 43 44
/* We use an unused modifier bit to mark stock items which
 * must be freed when they are removed from the hash table.
 */
#define NON_STATIC_MASK (1 << 29)

45 46 47 48 49
typedef struct _GtkStockTranslateFunc GtkStockTranslateFunc;
struct _GtkStockTranslateFunc
{
  GtkTranslateFunc func;
  gpointer data;
50
  GDestroyNotify notify;
51 52
};

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
static void
real_add (const GtkStockItem *items,
          guint               n_items,
          gboolean            copy)
{
  int i;

  init_stock_hash ();

  if (n_items == 0)
    return;

  i = 0;
  while (i < n_items)
    {
      gpointer old_key, old_value;
      const GtkStockItem *item = &items[i];
70 71 72 73 74 75 76

      if (item->modifier & NON_STATIC_MASK)
	{
	  g_warning ("Bit 29 set in stock accelerator.\n");
	  copy = TRUE;
	}

77
      if (copy)
78 79 80 81
	{
	  item = gtk_stock_item_copy (item);
	  ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
	}
82 83 84 85 86

      if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
                                        &old_key, &old_value))
        {
          g_hash_table_remove (stock_hash, old_key);
87 88
	  if (((GtkStockItem *)old_value)->modifier & NON_STATIC_MASK)
	    gtk_stock_item_free (old_value);
89 90 91 92 93 94 95 96 97
        }
      
      g_hash_table_insert (stock_hash,
                           (gchar*)item->stock_id, (GtkStockItem*)item);

      ++i;
    }
}

98 99 100 101 102 103 104 105 106 107 108 109 110
/**
 * gtk_stock_add:
 * @items: a #GtkStockItem or array of items
 * @n_items: number of #GtkStockItem in @items
 *
 * Registers each of the stock items in @items. If an item already
 * exists with the same stock ID as one of the @items, the old item
 * gets replaced. The stock items are copied, so GTK+ does not hold
 * any pointer into @items and @items can be freed. Use
 * gtk_stock_add_static() if @items is persistent and GTK+ need not
 * copy the array.
 * 
 **/
111 112 113 114 115 116 117 118 119
void
gtk_stock_add (const GtkStockItem *items,
               guint               n_items)
{
  g_return_if_fail (items != NULL);

  real_add (items, n_items, TRUE);
}

120 121 122 123 124 125 126 127 128
/**
 * gtk_stock_add_static:
 * @items: a #GtkStockItem or array of #GtkStockItem
 * @n_items: number of items
 *
 * Same as gtk_stock_add(), but doesn't copy @items, so
 * @items must persist until application exit.
 * 
 **/
129 130 131 132 133 134 135 136 137
void
gtk_stock_add_static (const GtkStockItem *items,
                      guint               n_items)
{
  g_return_if_fail (items != NULL);

  real_add (items, n_items, FALSE);
}

138 139 140 141 142 143 144 145 146 147 148
/**
 * gtk_stock_lookup:
 * @stock_id: a stock item name
 * @item: stock item to initialize with values
 * 
 * Fills @item with the registered values for @stock_id, returning %TRUE
 * if @stock_id was known.
 * 
 * 
 * Return value: %TRUE if @item was initialized
 **/
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
gboolean
gtk_stock_lookup (const gchar  *stock_id,
                  GtkStockItem *item)
{
  const GtkStockItem *found;

  g_return_val_if_fail (stock_id != NULL, FALSE);
  g_return_val_if_fail (item != NULL, FALSE);

  init_stock_hash ();

  found = g_hash_table_lookup (stock_hash, stock_id);

  if (found)
    {
      *item = *found;
165
      item->modifier &= ~NON_STATIC_MASK;
166
      if (item->label)
167 168
	{
	  GtkStockTranslateFunc *translate;
169 170 171 172 173 174
	  
	  if (item->translation_domain)
	    translate = (GtkStockTranslateFunc *) 
	      g_hash_table_lookup (translate_hash, item->translation_domain);
	  else
	    translate = NULL;
175 176 177 178
	  
	  if (translate != NULL && translate->func != NULL)
	    item->label = (* translate->func) (item->label, translate->data);
	  else
179
	    item->label = (gchar *) g_dgettext (item->translation_domain, item->label);
180
	}
181 182 183 184 185
    }

  return found != NULL;
}

186
/**
187
 * gtk_stock_list_ids:
188
 * 
189 190 191
 * Retrieves a list of all known stock IDs added to a #GtkIconFactory
 * or registered with gtk_stock_add(). The list must be freed with g_slist_free(),
 * and each string in the list must be freed with g_free().
192
 * 
193
 * Return value: a list of known stock IDs
194
 **/
195 196
GSList*
gtk_stock_list_ids (void)
197
{
198 199
  GList *ids;
  GList *icon_ids;
200 201 202
  GSList *retval;
  const gchar *last_id;
  
203 204
  init_stock_hash ();

205 206
  ids = g_hash_table_get_keys (stock_hash);
  icon_ids = _gtk_icon_factory_list_ids ();
207
  ids = g_list_concat (ids, icon_ids);
208

209
  ids = g_list_sort (ids, (GCompareFunc)strcmp);
210 211 212

  last_id = NULL;
  retval = NULL;
213
  while (ids != NULL)
214
    {
215
      GList *next;
216

217
      next = g_list_next (ids);
218

219
      if (last_id && strcmp (ids->data, last_id) == 0)
220 221 222 223 224
        {
          /* duplicate, ignore */
        }
      else
        {
225 226
          retval = g_slist_prepend (retval, g_strdup (ids->data));
          last_id = ids->data;
227 228
        }

229
      g_list_free_1 (ids);
230
      
231
      ids = next;
232 233 234
    }

  return retval;
235 236
}

237 238 239 240 241 242 243 244
/**
 * gtk_stock_item_copy:
 * @item: a #GtkStockItem
 * 
 * Copies a stock item, mostly useful for language bindings and not in applications.
 * 
 * Return value: a new #GtkStockItem
 **/
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
GtkStockItem *
gtk_stock_item_copy (const GtkStockItem *item)
{
  GtkStockItem *copy;

  g_return_val_if_fail (item != NULL, NULL);

  copy = g_new (GtkStockItem, 1);

  *copy = *item;

  copy->stock_id = g_strdup (item->stock_id);
  copy->label = g_strdup (item->label);
  copy->translation_domain = g_strdup (item->translation_domain);

  return copy;
}

263 264 265 266 267 268 269 270 271
/**
 * gtk_stock_item_free:
 * @item: a #GtkStockItem
 *
 * Frees a stock item allocated on the heap, such as one returned by
 * gtk_stock_item_copy(). Also frees the fields inside the stock item,
 * if they are not %NULL.
 * 
 **/
272 273 274 275 276 277 278 279 280 281 282 283
void
gtk_stock_item_free (GtkStockItem *item)
{
  g_return_if_fail (item != NULL);

  g_free ((gchar*)item->stock_id);
  g_free ((gchar*)item->label);
  g_free ((gchar*)item->translation_domain);

  g_free (item);
}

284
static const GtkStockItem builtin_items [] =
285
{
286
  /* KEEP IN SYNC with gtkiconfactory.c stock icons, when appropriate */ 
287
 
288 289 290 291
  { GTK_STOCK_DIALOG_INFO, NC_("Stock label", "Information"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_DIALOG_WARNING, NC_("Stock label", "Warning"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_DIALOG_ERROR, NC_("Stock label", "Error"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_DIALOG_QUESTION, NC_("Stock label", "Question"), 0, 0, GETTEXT_PACKAGE },
292

293 294 295
  /*  FIXME these need accelerators when appropriate, and
   * need the mnemonics to be rationalized
   */
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
  { GTK_STOCK_ABOUT, NC_("Stock label", "_About"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_ADD, NC_("Stock label", "_Add"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_APPLY, NC_("Stock label", "_Apply"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_BOLD, NC_("Stock label", "_Bold"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_CANCEL, NC_("Stock label", "_Cancel"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_CDROM, NC_("Stock label", "_CD-Rom"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_CLEAR, NC_("Stock label", "_Clear"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_CLOSE, NC_("Stock label", "_Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
  { GTK_STOCK_CONNECT, NC_("Stock label", "C_onnect"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_CONVERT, NC_("Stock label", "_Convert"), 0, 0, GETTEXT_PACKAGE },
   { GTK_STOCK_COPY, NC_("Stock label", "_Copy"), GDK_CONTROL_MASK, 'c', GETTEXT_PACKAGE },
  { GTK_STOCK_CUT, NC_("Stock label", "Cu_t"), GDK_CONTROL_MASK, 'x', GETTEXT_PACKAGE },
  { GTK_STOCK_DELETE, NC_("Stock label", "_Delete"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_DISCARD, NC_("Stock label", "_Discard"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_DISCONNECT, NC_("Stock label", "_Disconnect"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_EXECUTE, NC_("Stock label", "_Execute"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_EDIT, NC_("Stock label", "_Edit"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_FIND, NC_("Stock label", "_Find"), GDK_CONTROL_MASK, 'f', GETTEXT_PACKAGE },
  { GTK_STOCK_FIND_AND_REPLACE, NC_("Stock label", "Find and _Replace"), GDK_CONTROL_MASK, 'r', GETTEXT_PACKAGE },
  { GTK_STOCK_FLOPPY, NC_("Stock label", "_Floppy"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_FULLSCREEN, NC_("Stock label", "_Fullscreen"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_LEAVE_FULLSCREEN, NC_("Stock label", "_Leave Fullscreen"), 0, 0, GETTEXT_PACKAGE },
  /* This is a navigation label as in "go to the bottom of the page" */
319
  { GTK_STOCK_GOTO_BOTTOM, NC_("Stock label, navigation", "_Bottom"), 0, 0, GETTEXT_PACKAGE "-navigation" },
320
  /* This is a navigation label as in "go to the first page" */
321
  { GTK_STOCK_GOTO_FIRST, NC_("Stock label, navigation", "_First"), 0, 0, GETTEXT_PACKAGE "-navigation" },
322
  /* This is a navigation label as in "go to the last page" */
323
  { GTK_STOCK_GOTO_LAST, NC_("Stock label, navigation", "_Last"), 0, 0, GETTEXT_PACKAGE "-navigation" },
324
  /* This is a navigation label as in "go to the top of the page" */
325
  { GTK_STOCK_GOTO_TOP, NC_("Stock label, navigation", "_Top"), 0, 0, GETTEXT_PACKAGE "-navigation" },
326
  /* This is a navigation label as in "go back" */
327
  { GTK_STOCK_GO_BACK, NC_("Stock label, navigation", "_Back"), 0, 0, GETTEXT_PACKAGE "-navigation" },
328
  /* This is a navigation label as in "go down" */
329
  { GTK_STOCK_GO_DOWN, NC_("Stock label, navigation", "_Down"), 0, 0, GETTEXT_PACKAGE "-navigation" },
330
  /* This is a navigation label as in "go forward" */
331
  { GTK_STOCK_GO_FORWARD, NC_("Stock label, navigation", "_Forward"), 0, 0, GETTEXT_PACKAGE "-navigation" },
332
  /* This is a navigation label as in "go up" */
333
  { GTK_STOCK_GO_UP, NC_("Stock label, navigation", "_Up"), 0, 0, GETTEXT_PACKAGE "-navigation" },
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  { GTK_STOCK_HARDDISK, NC_("Stock label", "_Harddisk"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_HELP, NC_("Stock label", "_Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
  { GTK_STOCK_HOME, NC_("Stock label", "_Home"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_INDENT, NC_("Stock label", "Increase Indent"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_UNINDENT, NC_("Stock label", "Decrease Indent"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_INDEX, NC_("Stock label", "_Index"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_INFO, NC_("Stock label", "_Information"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_ITALIC, NC_("Stock label", "_Italic"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_JUMP_TO, NC_("Stock label", "_Jump to"), 0, 0, GETTEXT_PACKAGE },
  /* This is about text justification, "centered text" */
  { GTK_STOCK_JUSTIFY_CENTER, NC_("Stock label", "_Center"), 0, 0, GETTEXT_PACKAGE },
  /* This is about text justification */
  { GTK_STOCK_JUSTIFY_FILL, NC_("Stock label", "_Fill"), 0, 0, GETTEXT_PACKAGE },
  /* This is about text justification, "left-justified text" */
  { GTK_STOCK_JUSTIFY_LEFT, NC_("Stock label", "_Left"), 0, 0, GETTEXT_PACKAGE },
  /* This is about text justification, "right-justified text" */
  { GTK_STOCK_JUSTIFY_RIGHT, NC_("Stock label", "_Right"), 0, 0, GETTEXT_PACKAGE },

  /* Media label, as in "fast forward" */
353
  { GTK_STOCK_MEDIA_FORWARD, NC_("Stock label, media", "_Forward"), 0, 0, GETTEXT_PACKAGE "-media" },
354
  /* Media label, as in "next song" */
355
  { GTK_STOCK_MEDIA_NEXT, NC_("Stock label, media", "_Next"), 0, 0, GETTEXT_PACKAGE "-media" },
356
  /* Media label, as in "pause music" */
357
  { GTK_STOCK_MEDIA_PAUSE, NC_("Stock label, media", "P_ause"), 0, 0, GETTEXT_PACKAGE "-media" },
358
  /* Media label, as in "play music" */
359
  { GTK_STOCK_MEDIA_PLAY, NC_("Stock label, media", "_Play"), 0, 0, GETTEXT_PACKAGE "-media" },
360
  /* Media label, as in  "previous song" */
361
  { GTK_STOCK_MEDIA_PREVIOUS, NC_("Stock label, media", "Pre_vious"), 0, 0, GETTEXT_PACKAGE "-media" },
362
  /* Media label */
363
  { GTK_STOCK_MEDIA_RECORD, NC_("Stock label, media", "_Record"), 0, 0, GETTEXT_PACKAGE "-media" },
364
  /* Media label */
365
  { GTK_STOCK_MEDIA_REWIND, NC_("Stock label, media", "R_ewind"), 0, 0, GETTEXT_PACKAGE "-media" },
366
  /* Media label */
367
  { GTK_STOCK_MEDIA_STOP, NC_("Stock label, media", "_Stop"), 0, 0, GETTEXT_PACKAGE "-media" },
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
  { GTK_STOCK_NETWORK, NC_("Stock label", "_Network"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_NEW, NC_("Stock label", "_New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
  { GTK_STOCK_NO, NC_("Stock label", "_No"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_OK, NC_("Stock label", "_OK"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_OPEN, NC_("Stock label", "_Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
  /* Page orientation */
  { GTK_STOCK_ORIENTATION_LANDSCAPE, NC_("Stock label", "Landscape"), 0, 0, GETTEXT_PACKAGE },
  /* Page orientation */
  { GTK_STOCK_ORIENTATION_PORTRAIT, NC_("Stock label", "Portrait"), 0, 0, GETTEXT_PACKAGE },
  /* Page orientation */
  { GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, NC_("Stock label", "Reverse landscape"), 0, 0, GETTEXT_PACKAGE },
  /* Page orientation */
  { GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, NC_("Stock label", "Reverse portrait"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_PAGE_SETUP, NC_("Stock label", "Page Set_up"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_PASTE, NC_("Stock label", "_Paste"), GDK_CONTROL_MASK, 'v', GETTEXT_PACKAGE },
  { GTK_STOCK_PREFERENCES, NC_("Stock label", "_Preferences"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_PRINT, NC_("Stock label", "_Print"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_PRINT_PREVIEW, NC_("Stock label", "Print Pre_view"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_PROPERTIES, NC_("Stock label", "_Properties"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_QUIT, NC_("Stock label", "_Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
  { GTK_STOCK_REDO, NC_("Stock label", "_Redo"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_REFRESH, NC_("Stock label", "_Refresh"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_REMOVE, NC_("Stock label", "_Remove"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_REVERT_TO_SAVED, NC_("Stock label", "_Revert"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_SAVE, NC_("Stock label", "_Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE },
  { GTK_STOCK_SAVE_AS, NC_("Stock label", "Save _As"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_SELECT_ALL, NC_("Stock label", "Select _All"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_SELECT_COLOR, NC_("Stock label", "_Color"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_SELECT_FONT, NC_("Stock label", "_Font"), 0, 0, GETTEXT_PACKAGE },
  /* Sorting direction */
  { GTK_STOCK_SORT_ASCENDING, NC_("Stock label", "_Ascending"), 0, 0, GETTEXT_PACKAGE },
  /* Sorting direction */
  { GTK_STOCK_SORT_DESCENDING, NC_("Stock label", "_Descending"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_SPELL_CHECK, NC_("Stock label", "_Spell Check"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_STOP, NC_("Stock label", "_Stop"), 0, 0, GETTEXT_PACKAGE },
  /* Font variant */
  { GTK_STOCK_STRIKETHROUGH, NC_("Stock label", "_Strikethrough"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_UNDELETE, NC_("Stock label", "_Undelete"), 0, 0, GETTEXT_PACKAGE },
  /* Font variant */
  { GTK_STOCK_UNDERLINE, NC_("Stock label", "_Underline"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_UNDO, NC_("Stock label", "_Undo"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_YES, NC_("Stock label", "_Yes"), 0, 0, GETTEXT_PACKAGE },
  /* Zoom */
  { GTK_STOCK_ZOOM_100, NC_("Stock label", "_Normal Size"), 0, 0, GETTEXT_PACKAGE },
  /* Zoom */
  { GTK_STOCK_ZOOM_FIT, NC_("Stock label", "Best _Fit"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_ZOOM_IN, NC_("Stock label", "Zoom _In"), 0, 0, GETTEXT_PACKAGE },
  { GTK_STOCK_ZOOM_OUT, NC_("Stock label", "Zoom _Out"), 0, 0, GETTEXT_PACKAGE }
416 417
};

418 419
/**
 * gtk_stock_set_translate_func: 
420
 * @domain: the translation domain for which @func shall be used
421 422
 * @func: a #GtkTranslateFunc 
 * @data: data to pass to @func
423
 * @notify: a #GDestroyNotify that is called when @data is
424 425 426 427
 *   no longer needed
 *
 * Sets a function to be used for translating the @label of 
 * a stock item.
428
 * 
429
 * If no function is registered for a translation domain,
430
 * g_dgettext() is used.
431 432 433 434 435 436 437
 * 
 * The function is used for all stock items whose
 * @translation_domain matches @domain. Note that it is possible
 * to use strings different from the actual gettext translation domain
 * of your application for this, as long as your #GtkTranslateFunc uses
 * the correct domain when calling dgettext(). This can be useful, e.g.
 * when dealing with message contexts:
438
 *
439 440 441 442 443 444 445 446 447 448 449
 * |[
 * GtkStockItem items[] = { 
 *  { MY_ITEM1, NC_("odd items", "Item 1"), 0, 0, "odd-item-domain" },
 *  { MY_ITEM2, NC_("even items", "Item 2"), 0, 0, "even-item-domain" },
 * };
 *
 * gchar *
 * my_translate_func (const gchar *msgid,
 *                    gpointer     data)
 * {
 *   gchar *msgctxt = data;
450
 * 
451 452 453
 *   return (gchar*)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
 * }
 *
Matthias Clasen's avatar
Matthias Clasen committed
454
 * /&ast; ... &ast;/
455 456
 *
 * gtk_stock_add (items, G_N_ELEMENTS (items));
Matthias Clasen's avatar
Matthias Clasen committed
457 458
 * gtk_stock_set_translate_func ("odd-item-domain", my_translate_func, "odd items"); 
 * gtk_stock_set_translate_func ("even-item-domain", my_translate_func, "even items"); 
459 460 461
 * ]|
 * 
 * Since: 2.8
462 463 464 465 466
 */
void
gtk_stock_set_translate_func (const gchar      *domain,
			      GtkTranslateFunc  func,
			      gpointer          data,
467
			      GDestroyNotify    notify)
468 469
{
  GtkStockTranslateFunc *translate;
Manish Singh's avatar
Manish Singh committed
470 471 472 473
  gchar *domainname;
 
  domainname = g_strdup (domain);

474
  translate = (GtkStockTranslateFunc *) 
Manish Singh's avatar
Manish Singh committed
475
    g_hash_table_lookup (translate_hash, domainname);
476 477 478 479 480 481 482 483 484 485 486 487 488

  if (translate)
    {
      if (translate->notify)
	(* translate->notify) (translate->data);
    }
  else
    translate = g_new0 (GtkStockTranslateFunc, 1);
    
  translate->func = func;
  translate->data = data;
  translate->notify = notify;
      
Manish Singh's avatar
Manish Singh committed
489
  g_hash_table_insert (translate_hash, domainname, translate);
490 491 492 493
}

static gchar *
sgettext_swapped (const gchar *msgid, 
Manish Singh's avatar
Manish Singh committed
494
		  gpointer     data)
495
{
496
  gchar *msgctxt = data;
497

498
  return (gchar *)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
499 500
}

501 502 503 504 505 506 507 508 509
static void
init_stock_hash (void)
{
  if (stock_hash == NULL)
    {
      stock_hash = g_hash_table_new (g_str_hash, g_str_equal);

      gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
    }
510 511 512

  if (translate_hash == NULL)
    {
Manish Singh's avatar
Manish Singh committed
513 514
      translate_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
	                                      g_free, NULL);
515 516 517

      gtk_stock_set_translate_func (GETTEXT_PACKAGE, 
				    sgettext_swapped,
518
				    "Stock label",
519
				    NULL);
520
      gtk_stock_set_translate_func (GETTEXT_PACKAGE "-navigation", 
521 522
				    sgettext_swapped,
				    "Stock label, navigation",
523 524
				    NULL);
      gtk_stock_set_translate_func (GETTEXT_PACKAGE "-media", 
525 526
				    sgettext_swapped,
				    "Stock label, media",
527
				    NULL);
528
    }
529
}
530 531 532

#define __GTK_STOCK_C__
#include "gtkaliasdef.c"