]> Pileus Git - ~andy/gtk/blob - gtk/gtkstock.c
Move more text widget headers into the private header list
[~andy/gtk] / gtk / gtkstock.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "gtkstock.h"
28 #include "gtkintl.h"
29 #include <gdk/gdkkeysyms.h>
30
31 static GHashTable *stock_hash = NULL;
32 static void init_stock_hash (void);
33
34 static void
35 real_add (const GtkStockItem *items,
36           guint               n_items,
37           gboolean            copy)
38 {
39   int i;
40
41   init_stock_hash ();
42
43   if (n_items == 0)
44     return;
45
46   i = 0;
47   while (i < n_items)
48     {
49       gpointer old_key, old_value;
50       const GtkStockItem *item = &items[i];
51       if (copy)
52         item = gtk_stock_item_copy (item);
53
54       if (g_hash_table_lookup_extended (stock_hash, item->stock_id,
55                                         &old_key, &old_value))
56         {
57           g_hash_table_remove (stock_hash, old_key);
58           gtk_stock_item_free (old_value);
59         }
60       
61       g_hash_table_insert (stock_hash,
62                            (gchar*)item->stock_id, (GtkStockItem*)item);
63
64       ++i;
65     }
66 }
67
68 void
69 gtk_stock_add (const GtkStockItem *items,
70                guint               n_items)
71 {
72   g_return_if_fail (items != NULL);
73
74   real_add (items, n_items, TRUE);
75 }
76
77 void
78 gtk_stock_add_static (const GtkStockItem *items,
79                       guint               n_items)
80 {
81   g_return_if_fail (items != NULL);
82
83   real_add (items, n_items, FALSE);
84 }
85
86 gboolean
87 gtk_stock_lookup (const gchar  *stock_id,
88                   GtkStockItem *item)
89 {
90   const GtkStockItem *found;
91
92   g_return_val_if_fail (stock_id != NULL, FALSE);
93   g_return_val_if_fail (item != NULL, FALSE);
94
95   init_stock_hash ();
96
97   found = g_hash_table_lookup (stock_hash, stock_id);
98
99   if (found)
100     {
101       *item = *found;
102       if (item->label)
103         item->label = dgettext (item->translation_domain, item->label);
104     }
105
106   return found != NULL;
107 }
108
109 static void
110 listify_foreach (gpointer key, gpointer value, gpointer data)
111 {
112   GSList **list = data;
113
114   *list = g_slist_prepend (*list, value);
115 }
116
117 static GSList *
118 g_hash_table_get_values (GHashTable *table)
119 {
120   GSList *list = NULL;
121
122   g_hash_table_foreach (table, listify_foreach, &list);
123
124   return list;
125 }
126
127 GSList *
128 gtk_stock_list_items (void)
129 {
130   init_stock_hash ();
131
132   return g_hash_table_get_values (stock_hash);
133 }
134
135 GtkStockItem *
136 gtk_stock_item_copy (const GtkStockItem *item)
137 {
138   GtkStockItem *copy;
139
140   g_return_val_if_fail (item != NULL, NULL);
141
142   copy = g_new (GtkStockItem, 1);
143
144   *copy = *item;
145
146   copy->stock_id = g_strdup (item->stock_id);
147   copy->label = g_strdup (item->label);
148   copy->translation_domain = g_strdup (item->translation_domain);
149
150   return copy;
151 }
152
153 void
154 gtk_stock_item_free (GtkStockItem *item)
155 {
156   g_return_if_fail (item != NULL);
157
158   g_free ((gchar*)item->stock_id);
159   g_free ((gchar*)item->label);
160   g_free ((gchar*)item->translation_domain);
161
162   g_free (item);
163 }
164
165 static GtkStockItem builtin_items [] =
166 {
167   /* KEEP IN SYNC with gtkiconfactory.c stock icons */ 
168  
169   { GTK_STOCK_DIALOG_INFO, N_("Information"), 0, 0, GETTEXT_PACKAGE },
170   { GTK_STOCK_DIALOG_WARNING, N_("Warning"), 0, 0, GETTEXT_PACKAGE },
171   { GTK_STOCK_DIALOG_ERROR, N_("Error"), 0, 0, GETTEXT_PACKAGE },
172   { GTK_STOCK_DIALOG_QUESTION, N_("Question"), 0, 0, GETTEXT_PACKAGE },
173
174   { GTK_STOCK_BUTTON_APPLY, N_("_Apply"), 0, 0, GETTEXT_PACKAGE },
175   { GTK_STOCK_BUTTON_OK, N_("OK"), 0, 0, GETTEXT_PACKAGE },
176   { GTK_STOCK_BUTTON_CANCEL, N_("Cancel"), 0, 0, GETTEXT_PACKAGE },
177   { GTK_STOCK_BUTTON_CLOSE, N_("_Close"), 0, 0, GETTEXT_PACKAGE },
178   { GTK_STOCK_BUTTON_YES, N_("_Yes"), 0, 0, GETTEXT_PACKAGE },
179   { GTK_STOCK_BUTTON_NO, N_("_No"), 0, 0, GETTEXT_PACKAGE },
180
181   { GTK_STOCK_CLOSE, N_("Close"), GDK_CONTROL_MASK, 'w', GETTEXT_PACKAGE },
182   { GTK_STOCK_QUIT, N_("Quit"), GDK_CONTROL_MASK, 'q', GETTEXT_PACKAGE },
183   { GTK_STOCK_HELP, N_("Help"), GDK_CONTROL_MASK, 'h', GETTEXT_PACKAGE },
184   { GTK_STOCK_NEW, N_("New"), GDK_CONTROL_MASK, 'n', GETTEXT_PACKAGE },
185   { GTK_STOCK_OPEN, N_("Open"), GDK_CONTROL_MASK, 'o', GETTEXT_PACKAGE },
186   { GTK_STOCK_SAVE, N_("Save"), GDK_CONTROL_MASK, 's', GETTEXT_PACKAGE }
187 };
188
189 static void
190 init_stock_hash (void)
191 {
192   if (stock_hash == NULL)
193     {
194       stock_hash = g_hash_table_new (g_str_hash, g_str_equal);
195
196       gtk_stock_add_static (builtin_items, G_N_ELEMENTS (builtin_items));
197     }
198 }