2 #include "../../kernel/config.h"
3 #include "../../kernel/openbox.h"
7 #include <X11/Xresource.h>
18 static XrmDatabase loaddb(char *theme)
22 db = XrmGetFileDatabase(theme);
24 char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
25 "openbox", theme, NULL);
26 db = XrmGetFileDatabase(s);
30 char *s = g_build_filename(THEMEDIR, theme, NULL);
31 db = XrmGetFileDatabase(s);
37 static char *create_class_name(char *rname)
39 char *rclass = g_strdup(rname);
47 if (*p == '\0') break;
52 gboolean read_bool(XrmDatabase db, char *rname, gboolean *value)
55 char *rclass = create_class_name(rname);
59 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
60 retvalue.addr != NULL) {
61 if (!g_ascii_strcasecmp(retvalue.addr, "true"))
72 gboolean read_int(XrmDatabase db, char *rname, int *value)
75 char *rclass = create_class_name(rname);
79 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
80 retvalue.addr != NULL) {
81 *value = (int)strtol(retvalue.addr, &end, 10);
82 if (end != retvalue.addr)
90 gboolean read_string(XrmDatabase db, char *rname, char **value)
93 char *rclass = create_class_name(rname);
97 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
98 retvalue.addr != NULL) {
99 *value = g_strdup(retvalue.addr);
107 gboolean read_color(XrmDatabase db, char *rname, color_rgb **value)
109 gboolean ret = FALSE;
110 char *rclass = create_class_name(rname);
114 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
115 retvalue.addr != NULL) {
116 color_rgb *c = color_parse(retvalue.addr);
127 gboolean read_mask(XrmDatabase db, char *rname, pixmap_mask **value)
129 gboolean ret = FALSE;
130 char *rclass = create_class_name(rname);
135 int hx, hy; /* ignored */
140 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
141 retvalue.addr != NULL) {
142 if (!config_get("theme", Config_String, &theme))
143 g_assert_not_reached(); /* where's the default!? its not set? */
145 button_dir = g_strdup_printf("%s_buttons", theme.string);
147 s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
148 "openbox", button_dir, retvalue.addr, NULL);
150 if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess)
154 s = g_build_filename(THEMEDIR, button_dir, retvalue.addr, NULL);
156 if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess)
160 s = g_strdup_printf("%s_buttons/%s", theme.string,
162 if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) ==
166 g_message("Unable to find bitmap '%s'", s);
171 *value = pixmap_mask_new(w, h, (char*)b);
183 static void parse_appearance(char *tex, SurfaceColorType *grad,
184 ReliefType *relief, BevelType *bevel,
185 gboolean *interlaced, gboolean *border)
189 /* convert to all lowercase */
190 for (t = tex; *t != '\0'; ++t)
191 *t = g_ascii_tolower(*t);
193 if (strstr(tex, "parentrelative") != NULL) {
194 *grad = Background_ParentRelative;
196 if (strstr(tex, "gradient") != NULL) {
197 if (strstr(tex, "crossdiagonal") != NULL)
198 *grad = Background_CrossDiagonal;
199 else if (strstr(tex, "rectangle") != NULL)
200 *grad = Background_Rectangle;
201 else if (strstr(tex, "pyramid") != NULL)
202 *grad = Background_Pyramid;
203 else if (strstr(tex, "pipecross") != NULL)
204 *grad = Background_PipeCross;
205 else if (strstr(tex, "elliptic") != NULL)
206 *grad = Background_Elliptic;
207 else if (strstr(tex, "horizontal") != NULL)
208 *grad = Background_Horizontal;
209 else if (strstr(tex, "vertical") != NULL)
210 *grad = Background_Vertical;
212 *grad = Background_Diagonal;
214 *grad = Background_Solid;
217 if (strstr(tex, "sunken") != NULL)
219 else if (strstr(tex, "flat") != NULL)
225 if (*relief == Flat) {
226 if (strstr(tex, "border") != NULL)
229 if (strstr(tex, "bevel2") != NULL)
235 if (strstr(tex, "interlaced") != NULL)
243 gboolean read_appearance(XrmDatabase db, char *rname, Appearance *value)
245 gboolean ret = FALSE;
246 char *rclass = create_class_name(rname), *cname, *ctoname, *bcname;
250 cname = g_strconcat(rname, ".color", NULL);
251 ctoname = g_strconcat(rname, ".colorTo", NULL);
252 bcname = g_strconcat(rname, ".borderColor", NULL);
254 if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
255 retvalue.addr != NULL) {
256 parse_appearance(retvalue.addr,
257 &value->surface.data.planar.grad,
258 &value->surface.data.planar.relief,
259 &value->surface.data.planar.bevel,
260 &value->surface.data.planar.interlaced,
261 &value->surface.data.planar.border);
262 if (!read_color(db, cname, &value->surface.data.planar.primary))
263 value->surface.data.planar.primary = color_new(0, 0, 0);
264 if (!read_color(db, ctoname, &value->surface.data.planar.secondary))
265 value->surface.data.planar.secondary = color_new(0, 0, 0);
266 if (value->surface.data.planar.border)
267 if (!read_color(db, bcname,
268 &value->surface.data.planar.border_color))
269 value->surface.data.planar.border_color = color_new(0, 0, 0);
280 void set_default_appearance(Appearance *a)
282 a->surface.data.planar.grad = Background_Solid;
283 a->surface.data.planar.relief = Flat;
284 a->surface.data.planar.bevel = Bevel1;
285 a->surface.data.planar.interlaced = FALSE;
286 a->surface.data.planar.border = FALSE;
287 a->surface.data.planar.primary = color_new(0, 0, 0);
288 a->surface.data.planar.secondary = color_new(0, 0, 0);
293 XrmDatabase db = NULL;
296 ConfigValue theme, shadow, offset, font;
298 if (config_get("theme", Config_String, &theme)) {
299 db = loaddb(theme.string);
301 g_warning("Failed to load the theme '%s'", theme.string);
302 g_message("Falling back to the default: '%s'", DEFAULT_THEME);
306 db = loaddb(DEFAULT_THEME);
308 g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
311 /* change to reflect what was actually loaded */
312 theme.string = DEFAULT_THEME;
313 config_set("theme", Config_String, theme);
316 /* load the font, not from the theme file tho, its in the config */
318 if (!config_get("font.shadow", Config_Bool, &shadow)) {
319 shadow.bool = TRUE; /* default */
320 config_set("font.shadow", Config_Bool, shadow);
322 s_winfont_shadow = shadow.bool;
323 if (!config_get("font.shadow.offset", Config_Integer, &offset) ||
324 offset.integer < 0 || offset.integer >= 10) {
325 offset.integer = 1; /* default */
326 config_set("font.shadow.offset", Config_Integer, offset);
328 s_winfont_shadow_offset = offset.integer;
329 if (!config_get("font", Config_String, &font)) {
330 font.string = DEFAULT_FONT;
331 config_set("font", Config_String, font);
333 s_winfont = font_open(font.string);
334 s_winfont_height = font_height(s_winfont, s_winfont_shadow,
335 s_winfont_shadow_offset);
337 winjust = Justify_Left;
338 if (read_string(db, "window.justify", &winjuststr)) {
339 if (!g_ascii_strcasecmp(winjuststr, "right"))
340 winjust = Justify_Right;
341 else if (!g_ascii_strcasecmp(winjuststr, "center"))
342 winjust = Justify_Center;
346 if (!read_int(db, "handleWidth", &s_handle_height) ||
347 s_handle_height < 0 || s_handle_height > 100) s_handle_height = 6;
348 if (!read_int(db, "bevelWidth", &s_bevel) ||
349 s_bevel <= 0 || s_bevel > 100) s_bevel = 3;
350 if (!read_int(db, "borderWidth", &s_bwidth) ||
351 s_bwidth < 0 || s_bwidth > 100) s_bwidth = 1;
352 if (!read_int(db, "frameWidth", &s_cbwidth) ||
353 s_cbwidth < 0 || s_cbwidth > 100) s_cbwidth = s_bevel;
355 if (!read_color(db, "borderColor", &s_b_color))
356 s_b_color = color_new(0, 0, 0);
357 if (!read_color(db, "window.frame.focusColor", &s_cb_focused_color))
358 s_cb_focused_color = color_new(0xff, 0xff, 0xff);
359 if (!read_color(db, "window.frame.unfocusColor", &s_cb_unfocused_color))
360 s_cb_unfocused_color = color_new(0xff, 0xff, 0xff);
361 if (!read_color(db, "window.label.focus.textColor",
362 &s_title_focused_color))
363 s_title_focused_color = color_new(0xff, 0xff, 0xff);
364 if (!read_color(db, "window.label.unfocus.textColor",
365 &s_title_unfocused_color))
366 s_title_unfocused_color = color_new(0xff, 0xff, 0xff);
367 if (!read_color(db, "window.button.focus.picColor",
368 &s_titlebut_focused_color))
369 s_titlebut_focused_color = color_new(0, 0, 0);
370 if (!read_color(db, "window.button.unfocus.picColor",
371 &s_titlebut_unfocused_color))
372 s_titlebut_unfocused_color = color_new(0xff, 0xff, 0xff);
374 if (!read_mask(db, "window.button.max.mask", &s_max_mask)) {
375 char data[] = { 0x7c, 0x44, 0x47, 0x47, 0x7f, 0x1f, 0x1f };
376 s_max_mask = pixmap_mask_new(7, 7, data);
378 if (!read_mask(db, "window.button.icon.mask", &s_icon_mask)) {
379 char data[] = { 0x00, 0x00, 0x00, 0x00, 0x3e, 0x3e, 0x3e };
380 s_icon_mask = pixmap_mask_new(7, 7, data);
382 if (!read_mask(db, "window.button.stick.mask", &s_desk_mask)) {
383 char data[] = { 0x00, 0x36, 0x36, 0x00, 0x36, 0x36, 0x00 };
384 s_desk_mask = pixmap_mask_new(7, 7, data);
386 if (!read_mask(db, "window.button.close.mask", &s_close_mask)) {
387 char data[] = { 0x22, 0x77, 0x3e, 0x1c, 0x3e, 0x77, 0x22 };
388 s_close_mask = pixmap_mask_new(7, 7, data);
391 if (!read_appearance(db, "window.title.focus", a_focused_title))
392 set_default_appearance(a_focused_title);
393 if (!read_appearance(db, "window.title.unfocus", a_unfocused_title))
394 set_default_appearance(a_unfocused_title);
395 if (!read_appearance(db, "window.label.focus", a_focused_label))
396 set_default_appearance(a_focused_label);
397 if (!read_appearance(db, "window.label.unfocus", a_unfocused_label))
398 set_default_appearance(a_unfocused_label);
399 if (!read_appearance(db, "window.handle.focus", a_focused_handle))
400 set_default_appearance(a_focused_handle);
401 if (!read_appearance(db, "window.handle.unfocus", a_unfocused_handle))
402 set_default_appearance(a_unfocused_handle);
403 if (!read_appearance(db, "window.grip.focus", a_focused_grip))
404 set_default_appearance(a_focused_grip);
405 if (!read_appearance(db, "window.grip.unfocus", a_unfocused_grip))
406 set_default_appearance(a_unfocused_grip);
408 if (!read_appearance(db, "window.button.pressed.focus",
409 a_focused_pressed_max))
410 if (!read_appearance(db, "window.button.pressed",
411 a_focused_pressed_max))
412 set_default_appearance(a_focused_pressed_max);
413 if (!read_appearance(db, "window.button.pressed.unfocus",
414 a_unfocused_pressed_max))
415 if (!read_appearance(db, "window.button.pressed",
416 a_unfocused_pressed_max))
417 set_default_appearance(a_unfocused_pressed_max);
418 if (!read_appearance(db, "window.button.focus",
419 a_focused_unpressed_max))
420 set_default_appearance(a_focused_unpressed_max);
421 if (!read_appearance(db, "window.button.unfocus",
422 a_unfocused_unpressed_max))
423 set_default_appearance(a_unfocused_unpressed_max);
425 a_unfocused_unpressed_close = appearance_copy(a_unfocused_unpressed_max);
426 a_unfocused_pressed_close = appearance_copy(a_unfocused_pressed_max);
427 a_focused_unpressed_close = appearance_copy(a_focused_unpressed_max);
428 a_focused_pressed_close = appearance_copy(a_focused_pressed_max);
429 a_unfocused_unpressed_desk = appearance_copy(a_unfocused_unpressed_max);
430 a_unfocused_pressed_desk = appearance_copy(a_unfocused_pressed_max);
431 a_focused_unpressed_desk = appearance_copy(a_focused_unpressed_max);
432 a_focused_pressed_desk = appearance_copy(a_focused_pressed_max);
433 a_unfocused_unpressed_iconify = appearance_copy(a_unfocused_unpressed_max);
434 a_unfocused_pressed_iconify = appearance_copy(a_unfocused_pressed_max);
435 a_focused_unpressed_iconify = appearance_copy(a_focused_unpressed_max);
436 a_focused_pressed_iconify = appearance_copy(a_focused_pressed_max);
438 a_icon->surface.data.planar.grad = Background_ParentRelative;
440 /* set up the textures */
441 a_focused_label->texture[0].type = Text;
442 a_focused_label->texture[0].data.text.justify = winjust;
443 a_focused_label->texture[0].data.text.font = s_winfont;
444 a_focused_label->texture[0].data.text.shadow = s_winfont_shadow;
445 a_focused_label->texture[0].data.text.offset = s_winfont_shadow_offset;
446 a_focused_label->texture[0].data.text.color = s_title_focused_color;
448 a_unfocused_label->texture[0].type = Text;
449 a_unfocused_label->texture[0].data.text.justify = winjust;
450 a_unfocused_label->texture[0].data.text.font = s_winfont;
451 a_unfocused_label->texture[0].data.text.shadow = s_winfont_shadow;
452 a_unfocused_label->texture[0].data.text.offset = s_winfont_shadow_offset;
453 a_unfocused_label->texture[0].data.text.color = s_title_unfocused_color;
455 a_focused_unpressed_max->texture[0].type =
456 a_focused_pressed_max->texture[0].type =
457 a_unfocused_unpressed_max->texture[0].type =
458 a_unfocused_pressed_max->texture[0].type =
459 a_focused_unpressed_close->texture[0].type =
460 a_focused_pressed_close->texture[0].type =
461 a_unfocused_unpressed_close->texture[0].type =
462 a_unfocused_pressed_close->texture[0].type =
463 a_focused_unpressed_desk->texture[0].type =
464 a_focused_pressed_desk->texture[0].type =
465 a_unfocused_unpressed_desk->texture[0].type =
466 a_unfocused_pressed_desk->texture[0].type =
467 a_focused_unpressed_iconify->texture[0].type =
468 a_focused_pressed_iconify->texture[0].type =
469 a_unfocused_unpressed_iconify->texture[0].type =
470 a_unfocused_pressed_iconify->texture[0].type = Bitmask;
471 a_focused_unpressed_max->texture[0].data.mask.mask =
472 a_focused_pressed_max->texture[0].data.mask.mask =
473 a_unfocused_unpressed_max->texture[0].data.mask.mask =
474 a_unfocused_pressed_max->texture[0].data.mask.mask = s_max_mask;
475 a_focused_unpressed_close->texture[0].data.mask.mask =
476 a_focused_pressed_close->texture[0].data.mask.mask =
477 a_unfocused_unpressed_close->texture[0].data.mask.mask =
478 a_unfocused_pressed_close->texture[0].data.mask.mask = s_close_mask;
479 a_focused_unpressed_desk->texture[0].data.mask.mask =
480 a_focused_pressed_desk->texture[0].data.mask.mask =
481 a_unfocused_unpressed_desk->texture[0].data.mask.mask =
482 a_unfocused_pressed_desk->texture[0].data.mask.mask = s_desk_mask;
483 a_focused_unpressed_iconify->texture[0].data.mask.mask =
484 a_focused_pressed_iconify->texture[0].data.mask.mask =
485 a_unfocused_unpressed_iconify->texture[0].data.mask.mask =
486 a_unfocused_pressed_iconify->texture[0].data.mask.mask = s_icon_mask;
487 a_focused_unpressed_max->texture[0].data.mask.color =
488 a_focused_pressed_max->texture[0].data.mask.color =
489 a_focused_unpressed_close->texture[0].data.mask.color =
490 a_focused_pressed_close->texture[0].data.mask.color =
491 a_focused_unpressed_desk->texture[0].data.mask.color =
492 a_focused_pressed_desk->texture[0].data.mask.color =
493 a_focused_unpressed_iconify->texture[0].data.mask.color =
494 a_focused_pressed_iconify->texture[0].data.mask.color =
495 s_titlebut_focused_color;
496 a_unfocused_unpressed_max->texture[0].data.mask.color =
497 a_unfocused_pressed_max->texture[0].data.mask.color =
498 a_unfocused_unpressed_close->texture[0].data.mask.color =
499 a_unfocused_pressed_close->texture[0].data.mask.color =
500 a_unfocused_unpressed_desk->texture[0].data.mask.color =
501 a_unfocused_pressed_desk->texture[0].data.mask.color =
502 a_unfocused_unpressed_iconify->texture[0].data.mask.color =
503 a_unfocused_pressed_iconify->texture[0].data.mask.color =
504 s_titlebut_unfocused_color;
506 XrmDestroyDatabase(db);