]> Pileus Git - ~andy/gtk/blob - gtk/gtkstock.c
stylecontext: Do invalidation on first resize container
[~andy/gtk] / gtk / gtkstock.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 2000 Red Hat, Inc. 
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /*
19  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
20  * file for a list of people on the GTK+ Team.  See the ChangeLog
21  * files for a list of changes.  These files are distributed with
22  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include "gtkprivate.h"
30 #include "gtkstock.h"
31 #include "gtkiconfactory.h"
32 #include "gtkintl.h"
33
34 /**
35  * SECTION:gtkstock
36  * @Short_description:
37 Prebuilt common menu/toolbar items and corresponding icons
38  * @Title: Stock Items
39  *
40  * Stock items represent commonly-used menu or toolbar items such as
41  * "Open" or "Exit". Each stock item is identified by a stock ID;
42  * stock IDs are just strings, but macros such as #GTK_STOCK_OPEN are
43  * provided to avoid typing mistakes in the strings.
44  * Applications can register their own stock items in addition to those
45  * built-in to GTK+.
46  *
47  * Each stock ID can be associated with a #GtkStockItem, which contains
48  * the user-visible label, keyboard accelerator, and translation domain
49  * of the menu or toolbar item; and/or with an icon stored in a
50  * #GtkIconFactory. See <link
51  * linkend="gtk3-Themeable-Stock-Images">GtkIconFactory</link> for
52  * more information on stock icons. The connection between a
53  * #GtkStockItem and stock icons is purely conventional (by virtue of
54  * using the same stock ID); it's possible to register a stock item but
55  * no icon, and vice versa. Stock icons may have a RTL variant which gets
56  * used for right-to-left locales.
57  */
58
59 static GHashTable *translate_hash = NULL;
60 static GHashTable *stock_hash = NULL;
61 static void init_stock_hash (void);
62
63 /* We use an unused modifier bit to mark stock items which
64  * must be freed when they are removed from the hash table.
65  */
66 #define NON_STATIC_MASK (1 << 29)
67
68 /* Magic value which is automatically replaced by the primary accel modifier */
69 #define PRIMARY_MODIFIER 0xffffffff
70
71 typedef struct _GtkStockTranslateFunc GtkStockTranslateFunc;
72 struct _GtkStockTranslateFunc
73 {
74   GtkTranslateFunc func;
75   gpointer data;
76   GDestroyNotify notify;
77 };
78
79 static void
80 real_add (const GtkStockItem *items,
81           guint               n_items,
82           gboolean            copy,
83           gboolean            replace_primary)
84 {
85   int i;
86
87   init_stock_hash ();
88
89   if (n_items == 0)
90     return;
91
92   i = 0;
93   while (i < n_items)
94     {
95       gpointer old_key, old_value;
96       const GtkStockItem *item = &items[i];
97
98       if (replace_primary && item->modifier == PRIMARY_MODIFIER)
99         {
100           item = gtk_stock_item_copy (item);
101           ((GtkStockItem *)item)->modifier = (NON_STATIC_MASK |
102                                               _gtk_get_primary_accel_mod ());
103         }
104       else
105         {
106           if (item->modifier & NON_STATIC_MASK)
107             {
108               g_warning ("Bit 29 set in stock accelerator.\n");
109               copy = TRUE;
110             }
111
112           if (copy)
113             {
114               item = gtk_stock_item_copy (item);
115               ((GtkStockItem *)item)->modifier |= NON_STATIC_MASK;
116             }
117         }
118
119       if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
120                                         &old_key, &old_value))
121         {
122           g_hash_table_remove (stock_hash, old_key);
123           if (((GtkStockItem *)old_value)->modifier & NON_STATIC_MASK)
124             gtk_stock_item_free (old_value);
125         }
126       
127       g_hash_table_insert (stock_hash,
128                            (gchar*)item->stock_id, (GtkStockItem*)item);
129
130       ++i;
131     }
132 }
133
134 /**
135  * gtk_stock_add:
136  * @items: (array length=n_items): a #GtkStockItem or array of items
137  * @n_items: number of #GtkStockItem in @items
138  *
139  * Registers each of the stock items in @items. If an item already
140  * exists with the same stock ID as one of the @items, the old item
141  * gets replaced. The stock items are copied, so GTK+ does not hold
142  * any pointer into @items and @items can be freed. Use
143  * gtk_stock_add_static() if @items is persistent and GTK+ need not
144  * copy the array.
145  * 
146  **/
147 void
148 gtk_stock_add (const GtkStockItem *items,
149                guint               n_items)
150 {
151   g_return_if_fail (items != NULL);
152
153   real_add (items, n_items, TRUE, FALSE);
154 }
155
156 /**
157  * gtk_stock_add_static:
158  * @items: (array length=n_items): a #GtkStockItem or array of #GtkStockItem
159  * @n_items: number of items
160  *
161  * Same as gtk_stock_add(), but doesn't copy @items, so
162  * @items must persist until application exit.
163  * 
164  **/
165 void
166 gtk_stock_add_static (const GtkStockItem *items,
167                       guint               n_items)
168 {
169   g_return_if_fail (items != NULL);
170
171   real_add (items, n_items, FALSE, FALSE);
172 }
173
174 /**
175  * gtk_stock_lookup:
176  * @stock_id: a stock item name
177  * @item: (out): stock item to initialize with values
178  * 
179  * Fills @item with the registered values for @stock_id, returning %TRUE
180  * if @stock_id was known.
181  * 
182  * 
183  * Return value: %TRUE if @item was initialized
184  **/
185 gboolean
186 gtk_stock_lookup (const gchar  *stock_id,
187                   GtkStockItem *item)
188 {
189   const GtkStockItem *found;
190
191   g_return_val_if_fail (stock_id != NULL, FALSE);
192   g_return_val_if_fail (item != NULL, FALSE);
193
194   init_stock_hash ();
195
196   found = g_hash_table_lookup (stock_hash, stock_id);
197
198   if (found)
199     {
200       *item = *found;
201       item->modifier &= ~NON_STATIC_MASK;
202       if (item->label)
203         {
204           GtkStockTranslateFunc *translate;
205           
206           if (item->translation_domain)
207             translate = (GtkStockTranslateFunc *) 
208               g_hash_table_lookup (translate_hash, item->translation_domain);
209           else
210             translate = NULL;
211           
212           if (translate != NULL && translate->func != NULL)
213             item->label = (* translate->func) (item->label, translate->data);
214           else
215             item->label = (gchar *) g_dgettext (item->translation_domain, item->label);
216         }
217     }
218
219   return found != NULL;
220 }
221
222 /**
223  * gtk_stock_list_ids:
224  * 
225  * Retrieves a list of all known stock IDs added to a #GtkIconFactory
226  * or registered with gtk_stock_add(). The list must be freed with g_slist_free(),
227  * and each string in the list must be freed with g_free().
228  *
229  * Return value: (element-type utf8) (transfer full): a list of known stock IDs
230  **/
231 GSList*
232 gtk_stock_list_ids (void)
233 {
234   GList *ids;
235   GList *icon_ids;
236   GSList *retval;
237   const gchar *last_id;
238   
239   init_stock_hash ();
240
241   ids = g_hash_table_get_keys (stock_hash);
242   icon_ids = _gtk_icon_factory_list_ids ();
243   ids = g_list_concat (ids, icon_ids);
244
245   ids = g_list_sort (ids, (GCompareFunc)strcmp);
246
247   last_id = NULL;
248   retval = NULL;
249   while (ids != NULL)
250     {
251       GList *next;
252
253       next = g_list_next (ids);
254
255       if (last_id && strcmp (ids->data, last_id) == 0)
256         {
257           /* duplicate, ignore */
258         }
259       else
260         {
261           retval = g_slist_prepend (retval, g_strdup (ids->data));
262           last_id = ids->data;
263         }
264
265       g_list_free_1 (ids);
266       
267       ids = next;
268     }
269
270   return retval;
271 }
272
273 /**
274  * gtk_stock_item_copy: (skip)
275  * @item: a #GtkStockItem
276  * 
277  * Copies a stock item, mostly useful for language bindings and not in applications.
278  * 
279  * Return value: a new #GtkStockItem
280  **/
281 GtkStockItem *
282 gtk_stock_item_copy (const GtkStockItem *item)
283 {
284   GtkStockItem *copy;
285
286   g_return_val_if_fail (item != NULL, NULL);
287
288   copy = g_new (GtkStockItem, 1);
289
290   *copy = *item;
291
292   copy->stock_id = g_strdup (item->stock_id);
293   copy->label = g_strdup (item->label);
294   copy->translation_domain = g_strdup (item->translation_domain);
295
296   return copy;
297 }
298
299 /**
300  * gtk_stock_item_free:
301  * @item: a #GtkStockItem
302  *
303  * Frees a stock item allocated on the heap, such as one returned by
304  * gtk_stock_item_copy(). Also frees the fields inside the stock item,
305  * if they are not %NULL.
306  * 
307  **/
308 void
309 gtk_stock_item_free (GtkStockItem *item)
310 {
311   g_return_if_fail (item != NULL);
312
313   g_free ((gchar*)item->stock_id);
314   g_free ((gchar*)item->label);
315   g_free ((gchar*)item->translation_domain);
316
317   g_free (item);
318 }
319
320 static const GtkStockItem builtin_items [] =
321 {
322   /* KEEP IN SYNC with gtkiconfactory.c stock icons, when appropriate */ 
323  
324   { GTK_STOCK_DIALOG_INFO, NC_("Stock label", "Information"), 0, 0, GETTEXT_PACKAGE },
325   { GTK_STOCK_DIALOG_WARNING, NC_("Stock label", "Warning"), 0, 0, GETTEXT_PACKAGE },
326   { GTK_STOCK_DIALOG_ERROR, NC_("Stock label", "Error"), 0, 0, GETTEXT_PACKAGE },
327   { GTK_STOCK_DIALOG_QUESTION, NC_("Stock label", "Question"), 0, 0, GETTEXT_PACKAGE },
328
329   /*  FIXME these need accelerators when appropriate, and
330    * need the mnemonics to be rationalized
331    */
332   { GTK_STOCK_ABOUT, NC_("Stock label", "_About"), 0, 0, GETTEXT_PACKAGE },
333   { GTK_STOCK_ADD, NC_("Stock label", "_Add"), 0, 0, GETTEXT_PACKAGE },
334   { GTK_STOCK_APPLY, NC_("Stock label", "_Apply"), 0, 0, GETTEXT_PACKAGE },
335   { GTK_STOCK_BOLD, NC_("Stock label", "_Bold"), 0, 0, GETTEXT_PACKAGE },
336   { GTK_STOCK_CANCEL, NC_("Stock label", "_Cancel"), 0, 0, GETTEXT_PACKAGE },
337   { GTK_STOCK_CDROM, NC_("Stock label", "_CD-ROM"), 0, 0, GETTEXT_PACKAGE },
338   { GTK_STOCK_CLEAR, NC_("Stock label", "_Clear"), 0, 0, GETTEXT_PACKAGE },
339   { GTK_STOCK_CLOSE, NC_("Stock label", "_Close"), PRIMARY_MODIFIER, 'w', GETTEXT_PACKAGE },
340   { GTK_STOCK_CONNECT, NC_("Stock label", "C_onnect"), 0, 0, GETTEXT_PACKAGE },
341   { GTK_STOCK_CONVERT, NC_("Stock label", "_Convert"), 0, 0, GETTEXT_PACKAGE },
342    { GTK_STOCK_COPY, NC_("Stock label", "_Copy"), PRIMARY_MODIFIER, 'c', GETTEXT_PACKAGE },
343   { GTK_STOCK_CUT, NC_("Stock label", "Cu_t"), PRIMARY_MODIFIER, 'x', GETTEXT_PACKAGE },
344   { GTK_STOCK_DELETE, NC_("Stock label", "_Delete"), 0, 0, GETTEXT_PACKAGE },
345   { GTK_STOCK_DISCARD, NC_("Stock label", "_Discard"), 0, 0, GETTEXT_PACKAGE },
346   { GTK_STOCK_DISCONNECT, NC_("Stock label", "_Disconnect"), 0, 0, GETTEXT_PACKAGE },
347   { GTK_STOCK_EXECUTE, NC_("Stock label", "_Execute"), 0, 0, GETTEXT_PACKAGE },
348   { GTK_STOCK_EDIT, NC_("Stock label", "_Edit"), 0, 0, GETTEXT_PACKAGE },
349   { GTK_STOCK_FILE, NC_("Stock label", "_File"), 0, 0, GETTEXT_PACKAGE },
350   { GTK_STOCK_FIND, NC_("Stock label", "_Find"), PRIMARY_MODIFIER, 'f', GETTEXT_PACKAGE },
351   { GTK_STOCK_FIND_AND_REPLACE, NC_("Stock label", "Find and _Replace"), PRIMARY_MODIFIER, 'r', GETTEXT_PACKAGE },
352   { GTK_STOCK_FLOPPY, NC_("Stock label", "_Floppy"), 0, 0, GETTEXT_PACKAGE },
353   { GTK_STOCK_FULLSCREEN, NC_("Stock label", "_Fullscreen"), 0, 0, GETTEXT_PACKAGE },
354   { GTK_STOCK_LEAVE_FULLSCREEN, NC_("Stock label", "_Leave Fullscreen"), 0, 0, GETTEXT_PACKAGE },
355   /* This is a navigation label as in "go to the bottom of the page" */
356   { GTK_STOCK_GOTO_BOTTOM, NC_("Stock label, navigation", "_Bottom"), 0, 0, GETTEXT_PACKAGE "-navigation" },
357   /* This is a navigation label as in "go to the first page" */
358   { GTK_STOCK_GOTO_FIRST, NC_("Stock label, navigation", "_First"), 0, 0, GETTEXT_PACKAGE "-navigation" },
359   /* This is a navigation label as in "go to the last page" */
360   { GTK_STOCK_GOTO_LAST, NC_("Stock label, navigation", "_Last"), 0, 0, GETTEXT_PACKAGE "-navigation" },
361   /* This is a navigation label as in "go to the top of the page" */
362   { GTK_STOCK_GOTO_TOP, NC_("Stock label, navigation", "_Top"), 0, 0, GETTEXT_PACKAGE "-navigation" },
363   /* This is a navigation label as in "go back" */
364   { GTK_STOCK_GO_BACK, NC_("Stock label, navigation", "_Back"), 0, 0, GETTEXT_PACKAGE "-navigation" },
365   /* This is a navigation label as in "go down" */
366   { GTK_STOCK_GO_DOWN, NC_("Stock label, navigation", "_Down"), 0, 0, GETTEXT_PACKAGE "-navigation" },
367   /* This is a navigation label as in "go forward" */
368   { GTK_STOCK_GO_FORWARD, NC_("Stock label, navigation", "_Forward"), 0, 0, GETTEXT_PACKAGE "-navigation" },
369   /* This is a navigation label as in "go up" */
370   { GTK_STOCK_GO_UP, NC_("Stock label, navigation", "_Up"), 0, 0, GETTEXT_PACKAGE "-navigation" },
371   { GTK_STOCK_HARDDISK, NC_("Stock label", "_Hard Disk"), 0, 0, GETTEXT_PACKAGE },
372   { GTK_STOCK_HELP, NC_("Stock label", "_Help"), 0, GDK_KEY_F1, GETTEXT_PACKAGE },
373   { GTK_STOCK_HOME, NC_("Stock label", "_Home"), 0, 0, GETTEXT_PACKAGE },
374   { GTK_STOCK_INDENT, NC_("Stock label", "Increase Indent"), 0, 0, GETTEXT_PACKAGE },
375   { GTK_STOCK_UNINDENT, NC_("Stock label", "Decrease Indent"), 0, 0, GETTEXT_PACKAGE },
376   { GTK_STOCK_INDEX, NC_("Stock label", "_Index"), 0, 0, GETTEXT_PACKAGE },
377   { GTK_STOCK_INFO, NC_("Stock label", "_Information"), 0, 0, GETTEXT_PACKAGE },
378   { GTK_STOCK_ITALIC, NC_("Stock label", "_Italic"), 0, 0, GETTEXT_PACKAGE },
379   { GTK_STOCK_JUMP_TO, NC_("Stock label", "_Jump to"), 0, 0, GETTEXT_PACKAGE },
380   /* This is about text justification, "centered text" */
381   { GTK_STOCK_JUSTIFY_CENTER, NC_("Stock label", "_Center"), 0, 0, GETTEXT_PACKAGE },
382   /* This is about text justification */
383   { GTK_STOCK_JUSTIFY_FILL, NC_("Stock label", "_Fill"), 0, 0, GETTEXT_PACKAGE },
384   /* This is about text justification, "left-justified text" */
385   { GTK_STOCK_JUSTIFY_LEFT, NC_("Stock label", "_Left"), 0, 0, GETTEXT_PACKAGE },
386   /* This is about text justification, "right-justified text" */
387   { GTK_STOCK_JUSTIFY_RIGHT, NC_("Stock label", "_Right"), 0, 0, GETTEXT_PACKAGE },
388
389   /* Media label, as in "fast forward" */
390   { GTK_STOCK_MEDIA_FORWARD, NC_("Stock label, media", "_Forward"), 0, 0, GETTEXT_PACKAGE "-media" },
391   /* Media label, as in "next song" */
392   { GTK_STOCK_MEDIA_NEXT, NC_("Stock label, media", "_Next"), 0, 0, GETTEXT_PACKAGE "-media" },
393   /* Media label, as in "pause music" */
394   { GTK_STOCK_MEDIA_PAUSE, NC_("Stock label, media", "P_ause"), 0, 0, GETTEXT_PACKAGE "-media" },
395   /* Media label, as in "play music" */
396   { GTK_STOCK_MEDIA_PLAY, NC_("Stock label, media", "_Play"), 0, 0, GETTEXT_PACKAGE "-media" },
397   /* Media label, as in  "previous song" */
398   { GTK_STOCK_MEDIA_PREVIOUS, NC_("Stock label, media", "Pre_vious"), 0, 0, GETTEXT_PACKAGE "-media" },
399   /* Media label */
400   { GTK_STOCK_MEDIA_RECORD, NC_("Stock label, media", "_Record"), 0, 0, GETTEXT_PACKAGE "-media" },
401   /* Media label */
402   { GTK_STOCK_MEDIA_REWIND, NC_("Stock label, media", "R_ewind"), 0, 0, GETTEXT_PACKAGE "-media" },
403   /* Media label */
404   { GTK_STOCK_MEDIA_STOP, NC_("Stock label, media", "_Stop"), 0, 0, GETTEXT_PACKAGE "-media" },
405   { GTK_STOCK_NETWORK, NC_("Stock label", "_Network"), 0, 0, GETTEXT_PACKAGE },
406   { GTK_STOCK_NEW, NC_("Stock label", "_New"), PRIMARY_MODIFIER, 'n', GETTEXT_PACKAGE },
407   { GTK_STOCK_NO, NC_("Stock label", "_No"), 0, 0, GETTEXT_PACKAGE },
408   { GTK_STOCK_OK, NC_("Stock label", "_OK"), 0, 0, GETTEXT_PACKAGE },
409   { GTK_STOCK_OPEN, NC_("Stock label", "_Open"), PRIMARY_MODIFIER, 'o', GETTEXT_PACKAGE },
410   /* Page orientation */
411   { GTK_STOCK_ORIENTATION_LANDSCAPE, NC_("Stock label", "Landscape"), 0, 0, GETTEXT_PACKAGE },
412   /* Page orientation */
413   { GTK_STOCK_ORIENTATION_PORTRAIT, NC_("Stock label", "Portrait"), 0, 0, GETTEXT_PACKAGE },
414   /* Page orientation */
415   { GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE, NC_("Stock label", "Reverse landscape"), 0, 0, GETTEXT_PACKAGE },
416   /* Page orientation */
417   { GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT, NC_("Stock label", "Reverse portrait"), 0, 0, GETTEXT_PACKAGE },
418   { GTK_STOCK_PAGE_SETUP, NC_("Stock label", "Page Set_up"), 0, 0, GETTEXT_PACKAGE },
419   { GTK_STOCK_PASTE, NC_("Stock label", "_Paste"), PRIMARY_MODIFIER, 'v', GETTEXT_PACKAGE },
420   { GTK_STOCK_PREFERENCES, NC_("Stock label", "_Preferences"), 0, 0, GETTEXT_PACKAGE },
421   { GTK_STOCK_PRINT, NC_("Stock label", "_Print"), 0, 0, GETTEXT_PACKAGE },
422   { GTK_STOCK_PRINT_PREVIEW, NC_("Stock label", "Print Pre_view"), 0, 0, GETTEXT_PACKAGE },
423   { GTK_STOCK_PROPERTIES, NC_("Stock label", "_Properties"), 0, 0, GETTEXT_PACKAGE },
424   { GTK_STOCK_QUIT, NC_("Stock label", "_Quit"), PRIMARY_MODIFIER, 'q', GETTEXT_PACKAGE },
425   { GTK_STOCK_REDO, NC_("Stock label", "_Redo"), 0, 0, GETTEXT_PACKAGE },
426   { GTK_STOCK_REFRESH, NC_("Stock label", "_Refresh"), 0, 0, GETTEXT_PACKAGE },
427   { GTK_STOCK_REMOVE, NC_("Stock label", "_Remove"), 0, 0, GETTEXT_PACKAGE },
428   { GTK_STOCK_REVERT_TO_SAVED, NC_("Stock label", "_Revert"), 0, 0, GETTEXT_PACKAGE },
429   { GTK_STOCK_SAVE, NC_("Stock label", "_Save"), PRIMARY_MODIFIER, 's', GETTEXT_PACKAGE },
430   { GTK_STOCK_SAVE_AS, NC_("Stock label", "Save _As"), 0, 0, GETTEXT_PACKAGE },
431   { GTK_STOCK_SELECT_ALL, NC_("Stock label", "Select _All"), 0, 0, GETTEXT_PACKAGE },
432   { GTK_STOCK_SELECT_COLOR, NC_("Stock label", "_Color"), 0, 0, GETTEXT_PACKAGE },
433   { GTK_STOCK_SELECT_FONT, NC_("Stock label", "_Font"), 0, 0, GETTEXT_PACKAGE },
434   /* Sorting direction */
435   { GTK_STOCK_SORT_ASCENDING, NC_("Stock label", "_Ascending"), 0, 0, GETTEXT_PACKAGE },
436   /* Sorting direction */
437   { GTK_STOCK_SORT_DESCENDING, NC_("Stock label", "_Descending"), 0, 0, GETTEXT_PACKAGE },
438   { GTK_STOCK_SPELL_CHECK, NC_("Stock label", "_Spell Check"), 0, 0, GETTEXT_PACKAGE },
439   { GTK_STOCK_STOP, NC_("Stock label", "_Stop"), 0, 0, GETTEXT_PACKAGE },
440   /* Font variant */
441   { GTK_STOCK_STRIKETHROUGH, NC_("Stock label", "_Strikethrough"), 0, 0, GETTEXT_PACKAGE },
442   { GTK_STOCK_UNDELETE, NC_("Stock label", "_Undelete"), 0, 0, GETTEXT_PACKAGE },
443   /* Font variant */
444   { GTK_STOCK_UNDERLINE, NC_("Stock label", "_Underline"), 0, 0, GETTEXT_PACKAGE },
445   { GTK_STOCK_UNDO, NC_("Stock label", "_Undo"), 0, 0, GETTEXT_PACKAGE },
446   { GTK_STOCK_YES, NC_("Stock label", "_Yes"), 0, 0, GETTEXT_PACKAGE },
447   /* Zoom */
448   { GTK_STOCK_ZOOM_100, NC_("Stock label", "_Normal Size"), 0, 0, GETTEXT_PACKAGE },
449   /* Zoom */
450   { GTK_STOCK_ZOOM_FIT, NC_("Stock label", "Best _Fit"), 0, 0, GETTEXT_PACKAGE },
451   { GTK_STOCK_ZOOM_IN, NC_("Stock label", "Zoom _In"), 0, 0, GETTEXT_PACKAGE },
452   { GTK_STOCK_ZOOM_OUT, NC_("Stock label", "Zoom _Out"), 0, 0, GETTEXT_PACKAGE }
453 };
454
455 /**
456  * gtk_stock_set_translate_func: 
457  * @domain: the translation domain for which @func shall be used
458  * @func: a #GtkTranslateFunc 
459  * @data: data to pass to @func
460  * @notify: a #GDestroyNotify that is called when @data is
461  *   no longer needed
462  *
463  * Sets a function to be used for translating the @label of 
464  * a stock item.
465  * 
466  * If no function is registered for a translation domain,
467  * g_dgettext() is used.
468  * 
469  * The function is used for all stock items whose
470  * @translation_domain matches @domain. Note that it is possible
471  * to use strings different from the actual gettext translation domain
472  * of your application for this, as long as your #GtkTranslateFunc uses
473  * the correct domain when calling dgettext(). This can be useful, e.g.
474  * when dealing with message contexts:
475  *
476  * |[
477  * GtkStockItem items[] = { 
478  *  { MY_ITEM1, NC_("odd items", "Item 1"), 0, 0, "odd-item-domain" },
479  *  { MY_ITEM2, NC_("even items", "Item 2"), 0, 0, "even-item-domain" },
480  * };
481  *
482  * gchar *
483  * my_translate_func (const gchar *msgid,
484  *                    gpointer     data)
485  * {
486  *   gchar *msgctxt = data;
487  * 
488  *   return (gchar*)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
489  * }
490  *
491  * /&ast; ... &ast;/
492  *
493  * gtk_stock_add (items, G_N_ELEMENTS (items));
494  * gtk_stock_set_translate_func ("odd-item-domain", my_translate_func, "odd items"); 
495  * gtk_stock_set_translate_func ("even-item-domain", my_translate_func, "even items"); 
496  * ]|
497  * 
498  * Since: 2.8
499  */
500 void
501 gtk_stock_set_translate_func (const gchar      *domain,
502                               GtkTranslateFunc  func,
503                               gpointer          data,
504                               GDestroyNotify    notify)
505 {
506   GtkStockTranslateFunc *translate;
507   gchar *domainname;
508  
509   domainname = g_strdup (domain);
510
511   translate = (GtkStockTranslateFunc *) 
512     g_hash_table_lookup (translate_hash, domainname);
513
514   if (translate)
515     {
516       if (translate->notify)
517         (* translate->notify) (translate->data);
518     }
519   else
520     translate = g_new0 (GtkStockTranslateFunc, 1);
521     
522   translate->func = func;
523   translate->data = data;
524   translate->notify = notify;
525       
526   g_hash_table_insert (translate_hash, domainname, translate);
527 }
528
529 static gchar *
530 sgettext_swapped (const gchar *msgid, 
531                   gpointer     data)
532 {
533   gchar *msgctxt = data;
534
535   return (gchar *)g_dpgettext2 (GETTEXT_PACKAGE, msgctxt, msgid);
536 }
537
538 static void
539 init_stock_hash (void)
540 {
541   if (stock_hash == NULL)
542     {
543       stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
544
545       real_add (builtin_items, G_N_ELEMENTS (builtin_items), FALSE, TRUE);
546     }
547
548   if (translate_hash == NULL)
549     {
550       translate_hash = g_hash_table_new_full (g_str_hash, g_str_equal,
551                                               g_free, NULL);
552
553       gtk_stock_set_translate_func (GETTEXT_PACKAGE, 
554                                     sgettext_swapped,
555                                     "Stock label",
556                                     NULL);
557       gtk_stock_set_translate_func (GETTEXT_PACKAGE "-navigation", 
558                                     sgettext_swapped,
559                                     "Stock label, navigation",
560                                     NULL);
561       gtk_stock_set_translate_func (GETTEXT_PACKAGE "-media", 
562                                     sgettext_swapped,
563                                     "Stock label, media",
564                                     NULL);
565     }
566 }