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
);
63 gnt_style_get_color(char *group
, char *key
)
65 #if GLIB_CHECK_VERSION(2,6,0)
70 vals
= gnt_style_get_string_list(group
, key
, &n
);
72 fg
= gnt_colors_get_color(vals
[0]);
73 bg
= gnt_colors_get_color(vals
[1]);
74 ret
= gnt_color_add_pair(fg
, bg
);
83 char **gnt_style_get_string_list(const char *group
, const char *key
, gsize
*length
)
85 #if GLIB_CHECK_VERSION(2,6,0)
86 const char *prg
= g_get_prgname();
87 if ((group
== NULL
|| *group
== '\0') && prg
&&
88 g_key_file_has_group(gkfile
, prg
))
92 return g_key_file_get_string_list(gkfile
, group
, key
, length
, NULL
);
98 gboolean
gnt_style_get_bool(GntStyle style
, gboolean def
)
102 if (bool_styles
[style
] != -1)
103 return bool_styles
[style
];
105 str
= gnt_style_get(style
);
107 bool_styles
[style
] = str
? gnt_style_parse_bool(str
) : def
;
108 return bool_styles
[style
];
111 gboolean
gnt_style_parse_bool(const char *str
)
113 gboolean def
= FALSE
;
118 if (g_ascii_strcasecmp(str
, "false") == 0)
120 else if (g_ascii_strcasecmp(str
, "true") == 0)
122 else if (sscanf(str
, "%d", &i
) == 1)
133 #if GLIB_CHECK_VERSION(2,6,0)
137 char *s
= text
, *t
= text
;
141 if (*s
== '^' && *(s
+ 1) == '[')
143 *t
= '\033'; /* escape */
148 if (*(s
+ 1) == '\0')
153 if (*s
== 'r' || *s
== 'n')
170 parse_key(const char *key
)
172 return (char *)gnt_key_translate(key
);
176 void gnt_style_read_workspaces(GntWM
*wm
)
178 #if GLIB_CHECK_VERSION(2,6,0)
183 for (i
= 1; i
< MAX_WORKSPACES
; ++i
) {
188 g_snprintf(group
, sizeof(group
), "Workspace-%d", i
);
189 name
= g_key_file_get_value(gkfile
, group
, "name", NULL
);
193 ws
= gnt_ws_new(name
);
194 gnt_wm_add_workspace(wm
, ws
);
197 titles
= g_key_file_get_string_list(gkfile
, group
, "window-names", &c
, NULL
);
199 for (j
= 0; j
< c
; ++j
)
200 g_hash_table_replace(wm
->name_places
, g_strdup(titles
[j
]), ws
);
204 titles
= g_key_file_get_string_list(gkfile
, group
, "window-titles", &c
, NULL
);
206 for (j
= 0; j
< c
; ++j
)
207 g_hash_table_replace(wm
->title_places
, g_strdup(titles
[j
]), ws
);
213 void gnt_style_read_actions(GType type
, GntBindableClass
*klass
)
215 #if GLIB_CHECK_VERSION(2,6,0)
217 GError
*error
= NULL
;
219 name
= g_strdup_printf("%s::binding", g_type_name(type
));
221 if (g_key_file_has_group(gkfile
, name
))
226 keys
= g_key_file_get_keys(gkfile
, name
, &len
, &error
);
229 g_printerr("GntStyle: %s\n", error
->message
);
239 key
= g_strdup(keys
[len
]);
240 action
= g_key_file_get_string(gkfile
, name
, keys
[len
], &error
);
244 g_printerr("GntStyle: %s\n", error
->message
);
250 const char *keycode
= parse_key(key
);
251 if (keycode
== NULL
) {
252 g_printerr("GntStyle: Invalid key-binding %s\n", key
);
254 gnt_bindable_register_binding(klass
, action
, keycode
, NULL
);
266 gboolean
gnt_style_read_menu_accels(const char *name
, GHashTable
*table
)
268 #if GLIB_CHECK_VERSION(2,6,0)
270 GError
*error
= NULL
;
271 gboolean ret
= FALSE
;
273 kname
= g_strdup_printf("%s::menu", name
);
275 if (g_key_file_has_group(gkfile
, kname
))
280 keys
= g_key_file_get_keys(gkfile
, kname
, &len
, &error
);
283 g_printerr("GntStyle: %s\n", error
->message
);
293 key
= g_strdup(keys
[len
]);
294 menuid
= g_key_file_get_string(gkfile
, kname
, keys
[len
], &error
);
298 g_printerr("GntStyle: %s\n", error
->message
);
304 const char *keycode
= parse_key(key
);
305 if (keycode
== NULL
) {
306 g_printerr("GntStyle: Invalid key-binding %s\n", key
);
309 g_hash_table_replace(table
, g_strdup(keycode
), menuid
);
325 void gnt_styles_get_keyremaps(GType type
, GHashTable
*hash
)
327 #if GLIB_CHECK_VERSION(2,6,0)
329 GError
*error
= NULL
;
331 name
= g_strdup_printf("%s::remap", g_type_name(type
));
333 if (g_key_file_has_group(gkfile
, name
))
338 keys
= g_key_file_get_keys(gkfile
, name
, &len
, &error
);
341 g_printerr("GntStyle: %s\n", error
->message
);
351 key
= g_strdup(keys
[len
]);
352 replace
= g_key_file_get_string(gkfile
, name
, keys
[len
], &error
);
356 g_printerr("GntStyle: %s\n", error
->message
);
365 g_hash_table_insert(hash
, key
, replace
);
375 #if GLIB_CHECK_VERSION(2,6,0)
377 read_general_style(GKeyFile
*kfile
)
379 GError
*error
= NULL
;
381 const char *prgname
= g_get_prgname();
388 } styles
[] = {{"shadow", GNT_STYLE_SHADOW
},
389 {"customcolor", GNT_STYLE_COLOR
},
390 {"mouse", GNT_STYLE_MOUSE
},
391 {"wm", GNT_STYLE_WM
},
392 {"remember_position", GNT_STYLE_REMPOS
},
395 if (prgname
&& *prgname
)
396 keys
= g_key_file_get_keys(kfile
, prgname
, &nkeys
, NULL
);
400 keys
= g_key_file_get_keys(kfile
, prgname
, &nkeys
, &error
);
405 g_printerr("GntStyle: %s\n", error
->message
);
410 for (i
= 0; styles
[i
].style
; i
++)
412 str_styles
[styles
[i
].en
] =
413 g_key_file_get_string(kfile
, prgname
, styles
[i
].style
, NULL
);
420 void gnt_style_read_configure_file(const char *filename
)
422 #if GLIB_CHECK_VERSION(2,6,0)
423 GError
*error
= NULL
;
424 gkfile
= g_key_file_new();
426 if (!g_key_file_load_from_file(gkfile
, filename
,
427 G_KEY_FILE_KEEP_COMMENTS
| G_KEY_FILE_KEEP_TRANSLATIONS
, &error
))
429 g_printerr("GntStyle: %s\n", error
->message
);
433 gnt_colors_parse(gkfile
);
434 read_general_style(gkfile
);
438 void gnt_init_styles()
441 for (i
= 0; i
< GNT_STYLES
; i
++)
443 str_styles
[i
] = NULL
;
449 void gnt_uninit_styles()
452 for (i
= 0; i
< GNT_STYLES
; i
++) {
453 g_free(str_styles
[i
]);
454 str_styles
[i
] = NULL
;
457 #if GLIB_CHECK_VERSION(2,6,0)
458 g_key_file_free(gkfile
);