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
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
24 #include "gntcolors.h"
32 #define MAX_WORKSPACES 99
34 #if GLIB_CHECK_VERSION(2,6,0)
35 static GKeyFile
*gkfile
;
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
))
56 return g_key_file_get_value(gkfile
, group
, key
, NULL
);
62 gboolean
gnt_style_get_bool(GntStyle style
, gboolean def
)
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
)
82 if (g_ascii_strcasecmp(str
, "false") == 0)
84 else if (g_ascii_strcasecmp(str
, "true") == 0)
86 else if (sscanf(str
, "%d", &i
) == 1)
97 #if GLIB_CHECK_VERSION(2,6,0)
101 char *s
= text
, *t
= text
;
105 if (*s
== '^' && *(s
+ 1) == '[')
107 *t
= '\033'; /* escape */
112 if (*(s
+ 1) == '\0')
117 if (*s
== 'r' || *s
== 'n')
134 parse_key(const char *key
)
136 return (char *)gnt_key_translate(key
);
140 void gnt_style_read_workspaces(GntWM
*wm
)
142 #if GLIB_CHECK_VERSION(2,6,0)
147 for (i
= 1; i
< MAX_WORKSPACES
; ++i
) {
152 g_snprintf(group
, sizeof(group
), "Workspace-%d", i
);
153 name
= g_key_file_get_value(gkfile
, group
, "name", NULL
);
157 ws
= gnt_ws_new(name
);
158 gnt_wm_add_workspace(wm
, ws
);
161 titles
= g_key_file_get_string_list(gkfile
, group
, "window-names", &c
, NULL
);
163 for (j
= 0; j
< c
; ++j
)
164 g_hash_table_replace(wm
->name_places
, g_strdup(titles
[j
]), ws
);
168 titles
= g_key_file_get_string_list(gkfile
, group
, "window-titles", &c
, NULL
);
170 for (j
= 0; j
< c
; ++j
)
171 g_hash_table_replace(wm
->title_places
, g_strdup(titles
[j
]), ws
);
177 void gnt_style_read_actions(GType type
, GntBindableClass
*klass
)
179 #if GLIB_CHECK_VERSION(2,6,0)
181 GError
*error
= NULL
;
183 name
= g_strdup_printf("%s::binding", g_type_name(type
));
185 if (g_key_file_has_group(gkfile
, name
))
190 keys
= g_key_file_get_keys(gkfile
, name
, &len
, &error
);
193 g_printerr("GntStyle: %s\n", error
->message
);
203 key
= g_strdup(keys
[len
]);
204 action
= g_key_file_get_string(gkfile
, name
, keys
[len
], &error
);
208 g_printerr("GntStyle: %s\n", error
->message
);
214 const char *keycode
= parse_key(key
);
215 if (keycode
== NULL
) {
216 g_printerr("GntStyle: Invalid key-binding %s\n", key
);
218 gnt_bindable_register_binding(klass
, action
, keycode
, NULL
);
230 gboolean
gnt_style_read_menu_accels(const char *name
, GHashTable
*table
)
232 #if GLIB_CHECK_VERSION(2,6,0)
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
))
244 keys
= g_key_file_get_keys(gkfile
, kname
, &len
, &error
);
247 g_printerr("GntStyle: %s\n", error
->message
);
257 key
= g_strdup(keys
[len
]);
258 menuid
= g_key_file_get_string(gkfile
, kname
, keys
[len
], &error
);
262 g_printerr("GntStyle: %s\n", error
->message
);
268 const char *keycode
= parse_key(key
);
269 if (keycode
== NULL
) {
270 g_printerr("GntStyle: Invalid key-binding %s\n", key
);
273 g_hash_table_replace(table
, g_strdup(keycode
), menuid
);
289 void gnt_styles_get_keyremaps(GType type
, GHashTable
*hash
)
291 #if GLIB_CHECK_VERSION(2,6,0)
293 GError
*error
= NULL
;
295 name
= g_strdup_printf("%s::remap", g_type_name(type
));
297 if (g_key_file_has_group(gkfile
, name
))
302 keys
= g_key_file_get_keys(gkfile
, name
, &len
, &error
);
305 g_printerr("GntStyle: %s\n", error
->message
);
315 key
= g_strdup(keys
[len
]);
316 replace
= g_key_file_get_string(gkfile
, name
, keys
[len
], &error
);
320 g_printerr("GntStyle: %s\n", error
->message
);
329 g_hash_table_insert(hash
, key
, replace
);
339 #if GLIB_CHECK_VERSION(2,6,0)
341 read_general_style(GKeyFile
*kfile
)
343 GError
*error
= NULL
;
345 const char *prgname
= g_get_prgname();
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
},
359 if (prgname
&& *prgname
)
360 keys
= g_key_file_get_keys(kfile
, prgname
, &nkeys
, NULL
);
364 keys
= g_key_file_get_keys(kfile
, prgname
, &nkeys
, &error
);
369 g_printerr("GntStyle: %s\n", error
->message
);
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
);
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
);
397 gnt_colors_parse(gkfile
);
398 read_general_style(gkfile
);
402 void gnt_init_styles()
405 for (i
= 0; i
< GNT_STYLES
; i
++)
407 str_styles
[i
] = NULL
;
413 void gnt_uninit_styles()
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
);