1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
|
/*
* GNT - The GLib Ncurses Toolkit
*
* GNT is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "gntinternal.h"
#undef GNT_LOG_DOMAIN
#define GNT_LOG_DOMAIN "Style"
#include "gntstyle.h"
#include "gntcolors.h"
#include "gntws.h"
#include <glib.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORKSPACES 99
static GKeyFile *gkfile;
static char * str_styles[GNT_STYLES];
static int int_styles[GNT_STYLES];
static int bool_styles[GNT_STYLES];
const char *gnt_style_get(GntStyle style)
{
return str_styles[style];
}
char *gnt_style_get_from_name(const char *group, const char *key)
{
const char *prg;
/* gkfile is NULL when run by gtkdoc-scanobj or g-ir-scanner */
if (!gkfile)
return NULL;
prg = g_get_prgname();
if ((group == NULL || *group == '\0') && prg &&
g_key_file_has_group(gkfile, prg))
group = prg;
if (!group)
group = "general";
return g_key_file_get_value(gkfile, group, key, NULL);
}
int
gnt_style_get_color(char *group, char *key)
{
int fg = 0, bg = 0;
gsize n;
char **vals;
int ret = 0;
vals = gnt_style_get_string_list(group, key, &n);
if (vals && n == 2) {
fg = gnt_colors_get_color(vals[0]);
bg = gnt_colors_get_color(vals[1]);
ret = gnt_color_add_pair(fg, bg);
}
g_strfreev(vals);
return ret;
}
char **gnt_style_get_string_list(const char *group, const char *key, gsize *length)
{
const char *prg = g_get_prgname();
if ((group == NULL || *group == '\0') && prg &&
g_key_file_has_group(gkfile, prg))
group = prg;
if (!group)
group = "general";
return g_key_file_get_string_list(gkfile, group, key, length, NULL);
}
gboolean gnt_style_get_bool(GntStyle style, gboolean def)
{
const char * str;
if (bool_styles[style] != -1)
return bool_styles[style];
str = gnt_style_get(style);
bool_styles[style] = str ? gnt_style_parse_bool(str) : def;
return bool_styles[style];
}
gboolean gnt_style_parse_bool(const char *str)
{
gboolean def = FALSE;
int i;
if (str)
{
if (g_ascii_strcasecmp(str, "false") == 0)
def = FALSE;
else if (g_ascii_strcasecmp(str, "true") == 0)
def = TRUE;
else if (sscanf(str, "%d", &i) == 1)
{
if (i)
def = TRUE;
else
def = FALSE;
}
}
return def;
}
static void
refine(char *text)
{
char *s = text, *t = text;
while (*s)
{
if (*s == '^' && *(s + 1) == '[')
{
*t = '\033'; /* escape */
s++;
}
else if (*s == '\\')
{
if (*(s + 1) == '\0')
*t = ' ';
else
{
s++;
if (*s == 'r' || *s == 'n')
*t = '\r';
else if (*s == 't')
*t = '\t';
else
*t = *s;
}
}
else
*t = *s;
t++;
s++;
}
*t = '\0';
}
static char *
parse_key(const char *key)
{
return (char *)gnt_key_translate(key);
}
void gnt_style_read_workspaces(GntWM *wm)
{
int i;
gchar *name;
gsize c;
for (i = 1; i < MAX_WORKSPACES; ++i) {
gsize j;
GntWS *ws;
gchar **titles;
char group[32];
g_snprintf(group, sizeof(group), "Workspace-%d", i);
name = g_key_file_get_value(gkfile, group, "name", NULL);
if (!name)
return;
ws = gnt_ws_new(name);
gnt_wm_add_workspace(wm, ws);
g_free(name);
titles = g_key_file_get_string_list(gkfile, group, "window-names", &c, NULL);
if (titles) {
for (j = 0; j < c; ++j)
g_hash_table_replace(wm->name_places, g_strdup(titles[j]), ws);
g_strfreev(titles);
}
titles = g_key_file_get_string_list(gkfile, group, "window-titles", &c, NULL);
if (titles) {
for (j = 0; j < c; ++j)
g_hash_table_replace(wm->title_places, g_strdup(titles[j]), ws);
g_strfreev(titles);
}
}
}
void gnt_style_read_actions(GType type, GntBindableClass *klass)
{
char *name;
GError *error = NULL;
/* gkfile is NULL when run by gtkdoc-scanobj or g-ir-scanner */
if (!gkfile)
return;
name = g_strdup_printf("%s::binding", g_type_name(type));
if (g_key_file_has_group(gkfile, name))
{
gsize len = 0;
char **keys;
keys = g_key_file_get_keys(gkfile, name, &len, &error);
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
g_free(name);
return;
}
while (len--)
{
char *key, *action;
key = g_strdup(keys[len]);
action = g_key_file_get_string(gkfile, name, keys[len], &error);
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
error = NULL;
}
else
{
const char *keycode = parse_key(key);
if (keycode == NULL) {
gnt_warning("Invalid key-binding %s", key);
} else {
gnt_bindable_register_binding(klass, action, keycode, NULL);
}
}
g_free(key);
g_free(action);
}
g_strfreev(keys);
}
g_free(name);
}
gboolean gnt_style_read_menu_accels(const char *name, GHashTable *table)
{
char *kname;
GError *error = NULL;
gboolean ret = FALSE;
kname = g_strdup_printf("%s::menu", name);
if (g_key_file_has_group(gkfile, kname))
{
gsize len = 0;
char **keys;
keys = g_key_file_get_keys(gkfile, kname, &len, &error);
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
g_free(kname);
return ret;
}
while (len--)
{
char *key, *menuid;
key = g_strdup(keys[len]);
menuid = g_key_file_get_string(gkfile, kname, keys[len], &error);
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
error = NULL;
}
else
{
const char *keycode = parse_key(key);
if (keycode == NULL) {
gnt_warning("Invalid key-binding %s", key);
} else {
ret = TRUE;
g_hash_table_replace(table, g_strdup(keycode), menuid);
menuid = NULL;
}
}
g_free(key);
g_free(menuid);
}
g_strfreev(keys);
}
g_free(kname);
return ret;
}
void gnt_styles_get_keyremaps(GType type, GHashTable *hash)
{
char *name;
GError *error = NULL;
name = g_strdup_printf("%s::remap", g_type_name(type));
if (g_key_file_has_group(gkfile, name))
{
gsize len = 0;
char **keys;
keys = g_key_file_get_keys(gkfile, name, &len, &error);
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
g_free(name);
return;
}
while (len--)
{
char *key, *replace;
key = g_strdup(keys[len]);
replace = g_key_file_get_string(gkfile, name, keys[len], &error);
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
error = NULL;
g_free(key);
}
else
{
refine(key);
refine(replace);
g_hash_table_insert(hash, key, replace);
}
}
g_strfreev(keys);
}
g_free(name);
}
static void
read_general_style(GKeyFile *kfile)
{
GError *error = NULL;
gsize nkeys;
const char *prgname = g_get_prgname();
char **keys = NULL;
int i;
struct
{
const char *style;
GntStyle en;
} styles[] = {{"shadow", GNT_STYLE_SHADOW},
{"customcolor", GNT_STYLE_COLOR},
{"mouse", GNT_STYLE_MOUSE},
{"wm", GNT_STYLE_WM},
{"remember_position", GNT_STYLE_REMPOS},
{NULL, 0}};
if (prgname && *prgname)
keys = g_key_file_get_keys(kfile, prgname, &nkeys, NULL);
if (keys == NULL) {
prgname = "general";
keys = g_key_file_get_keys(kfile, prgname, &nkeys, &error);
}
if (error)
{
gnt_warning("%s", error->message);
g_error_free(error);
}
else
{
for (i = 0; styles[i].style; i++)
{
str_styles[styles[i].en] =
g_key_file_get_string(kfile, prgname, styles[i].style, NULL);
}
}
g_strfreev(keys);
}
void gnt_style_read_configure_file(const char *filename)
{
GError *error = NULL;
gkfile = g_key_file_new();
if (!g_key_file_load_from_file(gkfile, filename,
G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, &error))
{
gnt_warning("%s", error->message);
g_error_free(error);
return;
}
gnt_colors_parse(gkfile);
read_general_style(gkfile);
}
void gnt_init_styles()
{
int i;
for (i = 0; i < GNT_STYLES; i++)
{
str_styles[i] = NULL;
int_styles[i] = -1;
bool_styles[i] = -1;
}
}
void gnt_uninit_styles()
{
int i;
for (i = 0; i < GNT_STYLES; i++) {
g_free(str_styles[i]);
str_styles[i] = NULL;
}
g_key_file_free(gkfile);
gkfile = NULL;
}
|