Update to 42565dfdfb1d0477d08281c1c37e99bba59c56fc
[gnt.git] / gntstyle.c
blob6d5e605c117f4de122f31b87360df42eb507ce4c
1 /**
2 * GNT - The GLib Ncurses Toolkit
4 * GNT is the legal property of its developers, whose names are too numerous
5 * to list here. Please refer to the COPYRIGHT file distributed with this
6 * source distribution.
8 * This library is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
23 #include "gntstyle.h"
24 #include "gntcolors.h"
25 #include "gntws.h"
27 #include <glib.h>
28 #include <ctype.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #define MAX_WORKSPACES 99
34 #if GLIB_CHECK_VERSION(2,6,0)
35 static GKeyFile *gkfile;
36 #endif
38 static char * str_styles[GNT_STYLES];
39 static int int_styles[GNT_STYLES];
40 static int bool_styles[GNT_STYLES];
42 const char *gnt_style_get(GntStyle style)
44 return str_styles[style];
47 char *gnt_style_get_from_name(const char *group, const char *key)
49 #if GLIB_CHECK_VERSION(2,6,0)
50 const char *prg = g_get_prgname();
51 if ((group == NULL || *group == '\0') && prg &&
52 g_key_file_has_group(gkfile, prg))
53 group = prg;
54 if (!group)
55 group = "general";
56 return g_key_file_get_value(gkfile, group, key, NULL);
57 #else
58 return NULL;
59 #endif
62 gboolean gnt_style_get_bool(GntStyle style, gboolean def)
64 const char * str;
66 if (bool_styles[style] != -1)
67 return bool_styles[style];
69 str = gnt_style_get(style);
71 bool_styles[style] = str ? gnt_style_parse_bool(str) : def;
72 return bool_styles[style];
75 gboolean gnt_style_parse_bool(const char *str)
77 gboolean def = FALSE;
78 int i;
80 if (str)
82 if (g_ascii_strcasecmp(str, "false") == 0)
83 def = FALSE;
84 else if (g_ascii_strcasecmp(str, "true") == 0)
85 def = TRUE;
86 else if (sscanf(str, "%d", &i) == 1)
88 if (i)
89 def = TRUE;
90 else
91 def = FALSE;
94 return def;
97 #if GLIB_CHECK_VERSION(2,6,0)
98 static void
99 refine(char *text)
101 char *s = text, *t = text;
103 while (*s)
105 if (*s == '^' && *(s + 1) == '[')
107 *t = '\033'; /* escape */
108 s++;
110 else if (*s == '\\')
112 if (*(s + 1) == '\0')
113 *t = ' ';
114 else
116 s++;
117 if (*s == 'r' || *s == 'n')
118 *t = '\r';
119 else if (*s == 't')
120 *t = '\t';
121 else
122 *t = *s;
125 else
126 *t = *s;
127 t++;
128 s++;
130 *t = '\0';
133 static char *
134 parse_key(const char *key)
136 return (char *)gnt_key_translate(key);
138 #endif
140 void gnt_style_read_workspaces(GntWM *wm)
142 #if GLIB_CHECK_VERSION(2,6,0)
143 int i;
144 gchar *name;
145 gsize c;
147 for (i = 1; i < MAX_WORKSPACES; ++i) {
148 int j;
149 GntWS *ws;
150 gchar **titles;
151 char group[32];
152 g_snprintf(group, sizeof(group), "Workspace-%d", i);
153 name = g_key_file_get_value(gkfile, group, "name", NULL);
154 if (!name)
155 return;
157 ws = gnt_ws_new(name);
158 gnt_wm_add_workspace(wm, ws);
159 g_free(name);
161 titles = g_key_file_get_string_list(gkfile, group, "window-names", &c, NULL);
162 if (titles) {
163 for (j = 0; j < c; ++j)
164 g_hash_table_replace(wm->name_places, g_strdup(titles[j]), ws);
165 g_strfreev(titles);
168 titles = g_key_file_get_string_list(gkfile, group, "window-titles", &c, NULL);
169 if (titles) {
170 for (j = 0; j < c; ++j)
171 g_hash_table_replace(wm->title_places, g_strdup(titles[j]), ws);
172 g_strfreev(titles);
175 #endif
177 void gnt_style_read_actions(GType type, GntBindableClass *klass)
179 #if GLIB_CHECK_VERSION(2,6,0)
180 char *name;
181 GError *error = NULL;
183 name = g_strdup_printf("%s::binding", g_type_name(type));
185 if (g_key_file_has_group(gkfile, name))
187 gsize len = 0;
188 char **keys;
190 keys = g_key_file_get_keys(gkfile, name, &len, &error);
191 if (error)
193 g_printerr("GntStyle: %s\n", error->message);
194 g_error_free(error);
195 g_free(name);
196 return;
199 while (len--)
201 char *key, *action;
203 key = g_strdup(keys[len]);
204 action = g_key_file_get_string(gkfile, name, keys[len], &error);
206 if (error)
208 g_printerr("GntStyle: %s\n", error->message);
209 g_error_free(error);
210 error = NULL;
212 else
214 const char *keycode = parse_key(key);
215 if (keycode == NULL) {
216 g_printerr("GntStyle: Invalid key-binding %s\n", key);
217 } else {
218 gnt_bindable_register_binding(klass, action, keycode, NULL);
221 g_free(key);
222 g_free(action);
224 g_strfreev(keys);
226 g_free(name);
227 #endif
230 gboolean gnt_style_read_menu_accels(const char *name, GHashTable *table)
232 #if GLIB_CHECK_VERSION(2,6,0)
233 char *kname;
234 GError *error = NULL;
235 gboolean ret = FALSE;
237 kname = g_strdup_printf("%s::menu", name);
239 if (g_key_file_has_group(gkfile, kname))
241 gsize len = 0;
242 char **keys;
244 keys = g_key_file_get_keys(gkfile, kname, &len, &error);
245 if (error)
247 g_printerr("GntStyle: %s\n", error->message);
248 g_error_free(error);
249 g_free(kname);
250 return ret;
253 while (len--)
255 char *key, *menuid;
257 key = g_strdup(keys[len]);
258 menuid = g_key_file_get_string(gkfile, kname, keys[len], &error);
260 if (error)
262 g_printerr("GntStyle: %s\n", error->message);
263 g_error_free(error);
264 error = NULL;
266 else
268 const char *keycode = parse_key(key);
269 if (keycode == NULL) {
270 g_printerr("GntStyle: Invalid key-binding %s\n", key);
271 } else {
272 ret = TRUE;
273 g_hash_table_replace(table, g_strdup(keycode), menuid);
274 menuid = NULL;
277 g_free(key);
278 g_free(menuid);
280 g_strfreev(keys);
283 g_free(kname);
284 return ret;
285 #endif
286 return FALSE;
289 void gnt_styles_get_keyremaps(GType type, GHashTable *hash)
291 #if GLIB_CHECK_VERSION(2,6,0)
292 char *name;
293 GError *error = NULL;
295 name = g_strdup_printf("%s::remap", g_type_name(type));
297 if (g_key_file_has_group(gkfile, name))
299 gsize len = 0;
300 char **keys;
302 keys = g_key_file_get_keys(gkfile, name, &len, &error);
303 if (error)
305 g_printerr("GntStyle: %s\n", error->message);
306 g_error_free(error);
307 g_free(name);
308 return;
311 while (len--)
313 char *key, *replace;
315 key = g_strdup(keys[len]);
316 replace = g_key_file_get_string(gkfile, name, keys[len], &error);
318 if (error)
320 g_printerr("GntStyle: %s\n", error->message);
321 g_error_free(error);
322 error = NULL;
323 g_free(key);
325 else
327 refine(key);
328 refine(replace);
329 g_hash_table_insert(hash, key, replace);
332 g_strfreev(keys);
335 g_free(name);
336 #endif
339 #if GLIB_CHECK_VERSION(2,6,0)
340 static void
341 read_general_style(GKeyFile *kfile)
343 GError *error = NULL;
344 gsize nkeys;
345 const char *prgname = g_get_prgname();
346 char **keys = NULL;
347 int i;
348 struct
350 const char *style;
351 GntStyle en;
352 } styles[] = {{"shadow", GNT_STYLE_SHADOW},
353 {"customcolor", GNT_STYLE_COLOR},
354 {"mouse", GNT_STYLE_MOUSE},
355 {"wm", GNT_STYLE_WM},
356 {"remember_position", GNT_STYLE_REMPOS},
357 {NULL, 0}};
359 if (prgname && *prgname)
360 keys = g_key_file_get_keys(kfile, prgname, &nkeys, NULL);
362 if (keys == NULL) {
363 prgname = "general";
364 keys = g_key_file_get_keys(kfile, prgname, &nkeys, &error);
367 if (error)
369 g_printerr("GntStyle: %s\n", error->message);
370 g_error_free(error);
372 else
374 for (i = 0; styles[i].style; i++)
376 str_styles[styles[i].en] =
377 g_key_file_get_string(kfile, prgname, styles[i].style, NULL);
380 g_strfreev(keys);
382 #endif
384 void gnt_style_read_configure_file(const char *filename)
386 #if GLIB_CHECK_VERSION(2,6,0)
387 GError *error = NULL;
388 gkfile = g_key_file_new();
390 if (!g_key_file_load_from_file(gkfile, filename,
391 G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
393 g_printerr("GntStyle: %s\n", error->message);
394 g_error_free(error);
395 return;
397 gnt_colors_parse(gkfile);
398 read_general_style(gkfile);
399 #endif
402 void gnt_init_styles()
404 int i;
405 for (i = 0; i < GNT_STYLES; i++)
407 str_styles[i] = NULL;
408 int_styles[i] = -1;
409 bool_styles[i] = -1;
413 void gnt_uninit_styles()
415 int i;
416 for (i = 0; i < GNT_STYLES; i++) {
417 g_free(str_styles[i]);
418 str_styles[i] = NULL;
421 #if GLIB_CHECK_VERSION(2,6,0)
422 g_key_file_free(gkfile);
423 gkfile = NULL;
424 #endif