]> icculus.org git repositories - mikachu/openbox.git/blob - obrender/theme.c
Special case for unusual monitor setup
[mikachu/openbox.git] / obrender / theme.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    theme.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "render.h"
21 #include "color.h"
22 #include "font.h"
23 #include "mask.h"
24 #include "theme.h"
25 #ifdef MIKACHU
26 # include "icon-mikamika.h"
27 #else
28 # include "icon.h"
29 #endif
30 #include "obt/paths.h"
31
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <ctype.h>
35 #include <stdlib.h>
36 #include <string.h>
37
38 struct fallbacks {
39     RrAppearance *focused_disabled;
40     RrAppearance *unfocused_disabled;
41     RrAppearance *focused_hover;
42     RrAppearance *unfocused_hover;
43     RrAppearance *focused_unpressed;
44     RrAppearance *focused_pressed;
45     RrAppearance *unfocused_unpressed;
46     RrAppearance *unfocused_pressed;
47     RrAppearance *focused_hover_toggled;
48     RrAppearance *unfocused_hover_toggled;
49     RrAppearance *focused_unpressed_toggled;
50     RrAppearance *focused_pressed_toggled;
51     RrAppearance *unfocused_unpressed_toggled;
52     RrAppearance *unfocused_pressed_toggled;
53 };
54
55 static XrmDatabase loaddb(const gchar *name, gchar **path);
56 static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value);
57 static gboolean read_string(XrmDatabase db, const gchar *rname, gchar **value);
58 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
59                            const gchar *rname, RrColor **value);
60 static gboolean read_mask(const RrInstance *inst, const gchar *path,
61                           const gchar *maskname, RrPixmapMask **value);
62 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
63                                 const gchar *rname, RrAppearance *value,
64                                 gboolean allow_trans);
65 static int parse_inline_number(const char *p);
66 static RrPixel32* read_c_image(gint width, gint height, const guint8 *data);
67 static void set_default_appearance(RrAppearance *a);
68 static void read_button_styles(XrmDatabase db, const RrInstance *inst, 
69                                gchar *path,
70                                const RrTheme *theme, RrButton *btn, 
71                                const gchar *btnname,
72                                struct fallbacks *fbs,
73                                guchar *normal_mask,
74                                guchar *toggled_mask);
75
76 static RrFont *get_font(RrFont *target, RrFont **default_font,
77                         const RrInstance *inst)
78 {
79     if (target) {
80         RrFontRef(target);
81         return target;
82     } else {
83         /* Only load the default font once */
84         if (*default_font) {
85             RrFontRef(*default_font);
86         } else {
87             *default_font = RrFontOpenDefault(inst);
88         }
89         return *default_font;
90     }
91 }
92
93 #define READ_INT(x_resstr, x_var, x_min, x_max, x_def) \
94     if (!read_int(db, x_resstr, & x_var) || \
95             x_var < x_min || x_var > x_max) \
96         x_var = x_def;
97
98 #define READ_COLOR(x_resstr, x_var, x_def) \
99     if (!read_color(db, inst, x_resstr, & x_var)) \
100         x_var = x_def;
101
102 #define READ_COLOR_(x_res1, x_res2, x_var, x_def) \
103     if (!read_color(db, inst, x_res1, & x_var) && \
104         !read_color(db, inst, x_res2, & x_var)) \
105         x_var = x_def;
106
107 #define READ_MASK_COPY(x_file, x_var, x_copysrc) \
108     if (!read_mask(inst, path, x_file, & x_var)) \
109         x_var = RrPixmapMaskCopy(x_copysrc);
110
111 #define READ_APPEARANCE(x_resstr, x_var, x_parrel) \
112     if (!read_appearance(db, inst, x_resstr, x_var, x_parrel)) \
113         set_default_appearance(x_var);
114
115 #define READ_APPEARANCE_COPY(x_resstr, x_var, x_parrel, x_defval) \
116     if (!read_appearance(db, inst, x_resstr, x_var, x_parrel)) {\
117         RrAppearanceFree(x_var); \
118         x_var = RrAppearanceCopy(x_defval); }
119
120 #define READ_APPEARANCE_COPY_TEXTURES(x_resstr, x_var, x_parrel, x_defval, n_tex) \
121     if (!read_appearance(db, inst, x_resstr, x_var, x_parrel)) {\
122         RrAppearanceFree(x_var); \
123         x_var = RrAppearanceCopy(x_defval); \
124         RrAppearanceRemoveTextures(x_var); \
125         RrAppearanceAddTextures(x_var, 5); }
126
127 #define READ_APPEARANCE_(x_res1, x_res2, x_var, x_parrel, x_defval) \
128     if (!read_appearance(db, inst, x_res1, x_var, x_parrel) && \
129         !read_appearance(db, inst, x_res2, x_var, x_parrel)) {\
130         RrAppearanceFree(x_var); \
131         x_var = RrAppearanceCopy(x_defval); }
132
133 RrTheme* RrThemeNew(const RrInstance *inst, const gchar *name,
134                     gboolean allow_fallback,
135                     RrFont *active_window_font, RrFont *inactive_window_font,
136                     RrFont *menu_title_font, RrFont *menu_item_font,
137                     RrFont *active_osd_font, RrFont *inactive_osd_font)
138 {
139     XrmDatabase db = NULL;
140     RrJustify winjust, mtitlejust;
141     gchar *str;
142     RrTheme *theme;
143     RrFont *default_font = NULL;
144     gchar *path;
145     gint menu_overlap = 0;
146     struct fallbacks fbs;
147
148     if (name) {
149         db = loaddb(name, &path);
150         if (db == NULL) {
151             g_message("Unable to load the theme '%s'", name);
152             if (allow_fallback)
153                 g_message("Falling back to the default theme '%s'",
154                           DEFAULT_THEME);
155             /* fallback to the default theme */
156             name = NULL;
157         }
158     }
159     if (name == NULL) {
160         if (allow_fallback) {
161             db = loaddb(DEFAULT_THEME, &path);
162             if (db == NULL) {
163                 g_message("Unable to load the theme '%s'", DEFAULT_THEME);
164                 return NULL;
165             }
166         } else
167             return NULL;
168     }
169
170     /* initialize temp reading textures */
171     fbs.focused_disabled = RrAppearanceNew(inst, 1);
172     fbs.unfocused_disabled = RrAppearanceNew(inst, 1);
173     fbs.focused_hover = RrAppearanceNew(inst, 1);
174     fbs.unfocused_hover = RrAppearanceNew(inst, 1);
175     fbs.focused_unpressed_toggled = RrAppearanceNew(inst, 1);
176     fbs.unfocused_unpressed_toggled = RrAppearanceNew(inst, 1);
177     fbs.focused_hover_toggled = RrAppearanceNew(inst, 1);
178     fbs.unfocused_hover_toggled = RrAppearanceNew(inst, 1);
179     fbs.focused_pressed_toggled = RrAppearanceNew(inst, 1);
180     fbs.unfocused_pressed_toggled = RrAppearanceNew(inst, 1);
181     fbs.focused_unpressed = RrAppearanceNew(inst, 1);
182     fbs.focused_pressed = RrAppearanceNew(inst, 1);
183     fbs.unfocused_unpressed = RrAppearanceNew(inst, 1);
184     fbs.unfocused_pressed = RrAppearanceNew(inst, 1);
185
186     /* initialize theme */
187     theme = g_slice_new0(RrTheme);
188
189     theme->inst = inst;
190     theme->name = g_strdup(name ? name : DEFAULT_THEME);
191
192     /* init buttons */
193     theme->btn_max = RrButtonNew(inst);
194     theme->btn_close = RrButtonNew(inst);
195     theme->btn_desk = RrButtonNew(inst);
196     theme->btn_shade = RrButtonNew(inst);
197     theme->btn_iconify = RrButtonNew(inst);
198
199     /* init appearances */
200     theme->a_focused_grip = RrAppearanceNew(inst, 0);
201     theme->a_unfocused_grip = RrAppearanceNew(inst, 0);
202     theme->a_focused_title = RrAppearanceNew(inst, 0);
203     theme->a_unfocused_title = RrAppearanceNew(inst, 0);
204     theme->a_focused_label = RrAppearanceNew(inst, 1);
205     theme->a_unfocused_label = RrAppearanceNew(inst, 1);
206     theme->a_icon = RrAppearanceNew(inst, 1);
207     theme->a_focused_handle = RrAppearanceNew(inst, 0);
208     theme->a_unfocused_handle = RrAppearanceNew(inst, 0);
209     theme->a_menu = RrAppearanceNew(inst, 0);
210     theme->a_menu_title = RrAppearanceNew(inst, 0);
211     theme->a_menu_text_title = RrAppearanceNew(inst, 1);
212     theme->a_menu_normal = RrAppearanceNew(inst, 0);
213     theme->a_menu_selected = RrAppearanceNew(inst, 0);
214     theme->a_menu_disabled = RrAppearanceNew(inst, 0);
215     /* a_menu_disabled_selected is copied from a_menu_selected */
216     theme->a_menu_text_normal = RrAppearanceNew(inst, 1);
217     theme->a_menu_text_selected = RrAppearanceNew(inst, 1);
218     theme->a_menu_text_disabled = RrAppearanceNew(inst, 1);
219     theme->a_menu_text_disabled_selected = RrAppearanceNew(inst, 1);
220     theme->a_menu_bullet_normal = RrAppearanceNew(inst, 1);
221     theme->a_menu_bullet_selected = RrAppearanceNew(inst, 1);
222     theme->a_clear = RrAppearanceNew(inst, 0);
223     theme->a_clear_tex = RrAppearanceNew(inst, 1);
224     theme->osd_bg = RrAppearanceNew(inst, 0);
225     theme->osd_hilite_label = RrAppearanceNew(inst, 1);
226     theme->osd_hilite_bg = RrAppearanceNew(inst, 0);
227     theme->osd_unhilite_label = RrAppearanceNew(inst, 1);
228     theme->osd_unhilite_bg = RrAppearanceNew(inst, 0);
229     theme->osd_unpressed_button = RrAppearanceNew(inst, 1);
230     theme->osd_pressed_button = RrAppearanceNew(inst, 5);
231     theme->osd_focused_button = RrAppearanceNew(inst, 5);
232
233     /* load the font stuff */
234     theme->win_font_focused = get_font(active_window_font,
235                                        &default_font, inst);
236     theme->win_font_unfocused = get_font(inactive_window_font,
237                                          &default_font, inst);
238
239     winjust = RR_JUSTIFY_LEFT;
240     if (read_string(db, "window.label.text.justify", &str)) {
241         if (!g_ascii_strcasecmp(str, "right"))
242             winjust = RR_JUSTIFY_RIGHT;
243         else if (!g_ascii_strcasecmp(str, "center"))
244             winjust = RR_JUSTIFY_CENTER;
245     }
246
247     theme->menu_title_font = get_font(menu_title_font, &default_font, inst);
248
249     mtitlejust = RR_JUSTIFY_LEFT;
250     if (read_string(db, "menu.title.text.justify", &str)) {
251         if (!g_ascii_strcasecmp(str, "right"))
252             mtitlejust = RR_JUSTIFY_RIGHT;
253         else if (!g_ascii_strcasecmp(str, "center"))
254             mtitlejust = RR_JUSTIFY_CENTER;
255     }
256
257     theme->menu_font = get_font(menu_item_font, &default_font, inst);
258
259     theme->osd_font_hilite = get_font(active_osd_font, &default_font, inst);
260     theme->osd_font_unhilite = get_font(inactive_osd_font, &default_font,inst);
261
262     /* load direct dimensions */
263     READ_INT("menu.overlap", menu_overlap, -100, 100, 0);
264     READ_INT("menu.overlap.x", theme->menu_overlap_x, -100, 100, menu_overlap);
265     READ_INT("menu.overlap.y", theme->menu_overlap_y, -100, 100, menu_overlap);
266     READ_INT("window.handle.width", theme->handle_height, 0, 100, 6);
267     READ_INT("padding.width", theme->paddingx, 0, 100, 3);
268     READ_INT("padding.height", theme->paddingy, 0, 100, theme->paddingx);
269     READ_INT("border.width", theme->fbwidth, 0, 100, 1);
270     READ_INT("menu.border.width", theme->mbwidth, 0, 100, theme->fbwidth);
271     READ_INT("osd.border.width", theme->obwidth, 0, 100, theme->fbwidth);
272     READ_INT("undecorated.border.width", theme->ubwidth, 0, 100, theme->fbwidth);
273     READ_INT("menu.separator.width", theme->menu_sep_width, 1, 100, 1);
274     READ_INT("menu.separator.padding.width", theme->menu_sep_paddingx, 0, 100, 6);
275     READ_INT("menu.separator.padding.height", theme->menu_sep_paddingy, 0, 100, 3);
276     READ_INT("window.client.padding.width", theme->cbwidthx, 0, 100, theme->paddingx);
277     READ_INT("window.client.padding.height", theme->cbwidthy, 0, 100, theme->cbwidthx);
278
279     /* load colors */
280     READ_COLOR_("window.active.border.color",
281                 "border.color",
282                 theme->frame_focused_border_color,
283                 RrColorNew(inst, 0, 0, 0));
284     READ_COLOR("window.undecorated.active.border.color",
285                theme->frame_undecorated_focused_border_color,
286                RrColorCopy(theme->frame_focused_border_color));
287     READ_COLOR("window.active.title.separator.color",
288                theme->title_separator_focused_color,
289                RrColorCopy(theme->frame_focused_border_color));
290
291     READ_COLOR("window.inactive.border.color",
292                theme->frame_unfocused_border_color,
293                RrColorCopy(theme->frame_focused_border_color));
294
295     READ_COLOR("window.undecorated.inactive.border.color",
296                theme->frame_undecorated_unfocused_border_color,
297                RrColorCopy(theme->frame_unfocused_border_color));
298
299     READ_COLOR("window.inactive.title.separator.color",
300                theme->title_separator_unfocused_color,
301                RrColorCopy(theme->frame_unfocused_border_color));
302
303     READ_COLOR("menu.border.color",
304                theme->menu_border_color,
305                RrColorCopy(theme->frame_focused_border_color));
306
307     READ_COLOR("osd.border.color", 
308                theme->osd_border_color,
309                RrColorCopy(theme->frame_focused_border_color));
310
311     READ_COLOR("window.active.client.color",
312                theme->cb_focused_color,
313                RrColorNew(inst, 0xff, 0xff, 0xff));
314
315     READ_COLOR("window.inactive.client.color",
316                theme->cb_unfocused_color,
317                RrColorNew(inst, 0xff, 0xff, 0xff));
318
319     READ_COLOR("window.active.label.text.color",
320                theme->title_focused_color,
321                RrColorNew(inst, 0x0, 0x0, 0x0));
322
323     READ_COLOR("window.inactive.label.text.color",
324                theme->title_unfocused_color,
325                RrColorNew(inst, 0xff, 0xff, 0xff));
326
327     READ_COLOR_("osd.active.label.text.color",
328                 "osd.label.text.color",
329                 theme->osd_text_active_color,
330                 RrColorCopy(theme->title_focused_color));
331
332     READ_COLOR_("osd.inactive.label.text.color",
333                 "osd.label.text.color",
334                 theme->osd_text_inactive_color,
335                 RrColorCopy(theme->title_unfocused_color));
336
337     READ_COLOR("window.active.button.unpressed.image.color",
338                theme->titlebut_focused_unpressed_color,
339                RrColorNew(inst, 0, 0, 0));
340
341     READ_COLOR("window.inactive.button.unpressed.image.color",
342                theme->titlebut_unfocused_unpressed_color,
343                RrColorNew(inst, 0xff, 0xff, 0xff));
344
345     READ_COLOR("window.active.button.pressed.image.color",
346                theme->titlebut_focused_pressed_color,
347                RrColorCopy(theme->titlebut_focused_unpressed_color));
348
349     READ_COLOR("window.inactive.button.pressed.image.color",
350                theme->titlebut_unfocused_pressed_color,
351                RrColorCopy(theme->titlebut_unfocused_unpressed_color));
352
353     READ_COLOR("window.active.button.disabled.image.color",
354                theme->titlebut_focused_disabled_color,
355                RrColorNew(inst, 0xff, 0xff, 0xff));
356
357     READ_COLOR("window.inactive.button.disabled.image.color",
358                theme->titlebut_unfocused_disabled_color,
359                RrColorNew(inst, 0, 0, 0));
360
361     READ_COLOR("window.active.button.hover.image.color",
362                theme->titlebut_focused_hover_color,
363                RrColorCopy(theme->titlebut_focused_unpressed_color));
364
365     READ_COLOR("window.inactive.button.hover.image.color",
366                theme->titlebut_unfocused_hover_color,
367                RrColorCopy(theme->titlebut_unfocused_unpressed_color));
368
369     READ_COLOR_("window.active.button.toggled.unpressed.image.color",
370                 "window.active.button.toggled.image.color",
371                 theme->titlebut_focused_unpressed_toggled_color,
372                 RrColorCopy(theme->titlebut_focused_pressed_color));
373
374     READ_COLOR_("window.inactive.button.toggled.unpressed.image.color",
375                 "window.inactive.button.toggled.image.color",
376                 theme->titlebut_unfocused_unpressed_toggled_color,
377                 RrColorCopy(theme->titlebut_unfocused_pressed_color));
378
379     READ_COLOR("window.active.button.toggled.hover.image.color",
380                theme->titlebut_focused_hover_toggled_color,
381                RrColorCopy(theme->titlebut_focused_unpressed_toggled_color));
382
383     READ_COLOR("window.inactive.button.toggled.hover.image.color",
384                theme->titlebut_unfocused_hover_toggled_color,
385                RrColorCopy(theme->titlebut_unfocused_unpressed_toggled_color));
386
387     READ_COLOR("window.active.button.toggled.pressed.image.color",
388                theme->titlebut_focused_pressed_toggled_color,
389                RrColorCopy(theme->titlebut_focused_pressed_color));
390
391     READ_COLOR("window.inactive.button.toggled.pressed.image.color",
392                theme->titlebut_unfocused_pressed_toggled_color,
393                RrColorCopy(theme->titlebut_unfocused_pressed_color));
394
395     READ_COLOR("menu.title.text.color",
396                theme->menu_title_color,
397                RrColorNew(inst, 0, 0, 0));
398
399     READ_COLOR("menu.items.text.color",
400                theme->menu_color,
401                RrColorNew(inst, 0xff, 0xff, 0xff));
402
403     READ_COLOR("menu.bullet.image.color",
404                theme->menu_bullet_color,
405                RrColorCopy(theme->menu_color));
406    
407     READ_COLOR("menu.items.disabled.text.color",
408                theme->menu_disabled_color,
409                RrColorNew(inst, 0, 0, 0));
410
411     READ_COLOR("menu.items.active.disabled.text.color",
412                theme->menu_disabled_selected_color,
413                RrColorCopy(theme->menu_disabled_color));
414
415     READ_COLOR("menu.items.active.text.color",
416                theme->menu_selected_color,
417                RrColorNew(inst, 0, 0, 0));
418
419     READ_COLOR("menu.separator.color",
420                theme->menu_sep_color,
421                RrColorCopy(theme->menu_color));
422     
423     READ_COLOR("menu.bullet.selected.image.color", 
424                theme->menu_bullet_selected_color,
425                RrColorCopy(theme->menu_selected_color));
426
427     READ_COLOR("osd.button.unpressed.text.color",
428                theme->osd_unpressed_color,
429                RrColorCopy(theme->osd_text_active_color));
430     READ_COLOR("osd.button.pressed.text.color",
431                theme->osd_pressed_color,
432                RrColorCopy(theme->osd_text_active_color));
433     READ_COLOR("osd.button.focused.text.color",
434                theme->osd_focused_color,
435                RrColorCopy(theme->osd_text_active_color));
436     READ_COLOR("osd.button.pressed.box.color",
437                theme->osd_pressed_lineart,
438                RrColorCopy(theme->titlebut_focused_pressed_color));
439     READ_COLOR("osd.button.focused.box.color",
440                theme->osd_focused_lineart,
441                RrColorCopy(theme->titlebut_focused_hover_color));
442  
443     /* load window buttons */
444
445     /* bases: unpressed, pressed, disabled */
446     READ_APPEARANCE("window.active.button.unpressed.bg", fbs.focused_unpressed, TRUE);
447     READ_APPEARANCE("window.inactive.button.unpressed.bg", fbs.unfocused_unpressed, TRUE);
448     READ_APPEARANCE("window.active.button.pressed.bg", fbs.focused_pressed, TRUE);
449     READ_APPEARANCE("window.inactive.button.pressed.bg", fbs.unfocused_pressed, TRUE);
450     READ_APPEARANCE("window.active.button.disabled.bg", fbs.focused_disabled, TRUE);
451     READ_APPEARANCE("window.inactive.button.disabled.bg", fbs.unfocused_disabled, TRUE);
452
453     /* hover */
454     READ_APPEARANCE_COPY("window.active.button.hover.bg",
455                          fbs.focused_hover, TRUE,
456                          fbs.focused_unpressed);
457     READ_APPEARANCE_COPY("window.inactive.button.hover.bg",
458                          fbs.unfocused_hover, TRUE,
459                          fbs.unfocused_unpressed);
460
461     /* toggled unpressed */
462     READ_APPEARANCE_("window.active.button.toggled.unpressed.bg",
463                      "window.active.button.toggled.bg",
464                      fbs.focused_unpressed_toggled, TRUE,
465                      fbs.focused_pressed);
466     READ_APPEARANCE_("window.inactive.button.toggled.unpressed.bg",
467                      "window.inactive.button.toggled.bg",
468                      fbs.unfocused_unpressed_toggled, TRUE,
469                      fbs.unfocused_pressed);
470
471     /* toggled pressed */
472     READ_APPEARANCE_COPY("window.active.button.toggled.pressed.bg",
473                          fbs.focused_pressed_toggled, TRUE,
474                          fbs.focused_pressed);
475     READ_APPEARANCE_COPY("window.inactive.button.toggled.pressed.bg",
476                          fbs.unfocused_pressed_toggled, TRUE,
477                          fbs.unfocused_pressed);
478
479     /* toggled hover */
480     READ_APPEARANCE_COPY("window.active.button.toggled.hover.bg",
481                          fbs.focused_hover_toggled, TRUE,
482                          fbs.focused_unpressed_toggled);
483     READ_APPEARANCE_COPY("window.inactive.button.toggled.hover.bg",
484                          fbs.unfocused_hover_toggled, TRUE,
485                          fbs.unfocused_unpressed_toggled);
486
487
488     /* now do individual buttons, if specified */
489
490     /* max button */
491     {
492         guchar normal_mask[] =  { 0x3f, 0x3f, 0x21, 0x21, 0x21, 0x3f };
493         guchar toggled_mask[] = { 0x3e, 0x22, 0x2f, 0x29, 0x39, 0x0f };
494         read_button_styles(db, inst, path, theme, theme->btn_max, "max",
495                            &fbs, normal_mask, toggled_mask);
496     }
497
498     /* close button */
499     {
500         guchar normal_mask[] = { 0x33, 0x3f, 0x1e, 0x1e, 0x3f, 0x33 };
501         read_button_styles(db, inst, path, theme, theme->btn_close, "close",
502                            &fbs, normal_mask, NULL);
503     }
504
505     /* all desktops button */
506     {
507         guchar normal_mask[] =  { 0x33, 0x33, 0x00, 0x00, 0x33, 0x33 };
508         guchar toggled_mask[] = { 0x00, 0x1e, 0x1a, 0x16, 0x1e, 0x00 };
509         read_button_styles(db, inst, path, theme, theme->btn_desk, "desk",
510                            &fbs, normal_mask, toggled_mask);
511     }
512
513     /* shade button */
514     {
515         guchar normal_mask[] = { 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00 };
516         read_button_styles(db, inst, path, theme, theme->btn_shade, "shade",
517                            &fbs, normal_mask, normal_mask);
518     }
519
520     /* iconify button */
521     {
522         guchar normal_mask[] = { 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f };
523         read_button_styles(db, inst, path, theme, theme->btn_iconify, "iconify",
524                            &fbs, normal_mask, NULL);
525     }
526
527     /* submenu bullet mask */
528     if (!read_mask(inst, path, "bullet.xbm", &theme->menu_bullet_mask))
529     {
530         guchar data[] = { 0x01, 0x03, 0x07, 0x0f, 0x07, 0x03, 0x01 };
531         theme->menu_bullet_mask = RrPixmapMaskNew(inst, 4, 7, (gchar*)data);
532     }
533
534     /* up and down arrows */
535     {
536         guchar data[] = { 0xfe, 0x00, 0x7c, 0x00, 0x38, 0x00, 0x10, 0x00 };
537         theme->down_arrow_mask = RrPixmapMaskNew(inst, 9, 4, (gchar*)data);
538     }
539     {
540         guchar data[] = { 0x10, 0x00, 0x38, 0x00, 0x7c, 0x00, 0xfe, 0x00 };
541         theme->up_arrow_mask = RrPixmapMaskNew(inst, 9, 4, (gchar*)data);
542     }
543
544     /* setup the default window icon */
545     theme->def_win_icon = read_c_image(OB_DEFAULT_ICON_WIDTH,
546                                        OB_DEFAULT_ICON_HEIGHT,
547                                        OB_DEFAULT_ICON_pixel_data);
548     theme->def_win_icon_w = OB_DEFAULT_ICON_WIDTH;
549     theme->def_win_icon_h = OB_DEFAULT_ICON_HEIGHT;
550
551     /* read the decoration textures */
552     READ_APPEARANCE("window.active.title.bg", theme->a_focused_title, FALSE);
553     READ_APPEARANCE("window.inactive.title.bg", theme->a_unfocused_title, FALSE);
554     READ_APPEARANCE("window.active.label.bg", theme->a_focused_label, TRUE);
555     READ_APPEARANCE("window.inactive.label.bg", theme->a_unfocused_label, TRUE);
556     READ_APPEARANCE("window.active.handle.bg", theme->a_focused_handle, FALSE);
557     READ_APPEARANCE("window.inactive.handle.bg",theme->a_unfocused_handle, FALSE);
558     READ_APPEARANCE("window.active.grip.bg", theme->a_focused_grip, TRUE);
559     READ_APPEARANCE("window.inactive.grip.bg", theme->a_unfocused_grip, TRUE);
560     READ_APPEARANCE("menu.items.bg", theme->a_menu, FALSE);
561     READ_APPEARANCE("menu.title.bg", theme->a_menu_title, TRUE);
562     READ_APPEARANCE("menu.items.active.bg", theme->a_menu_selected, TRUE);
563
564     theme->a_menu_disabled_selected =
565         RrAppearanceCopy(theme->a_menu_selected);
566
567     /* read appearances for non-decorations (on-screen-display) */
568     if (!read_appearance(db, inst, "osd.bg", theme->osd_bg, FALSE))
569     {
570         RrAppearanceFree(theme->osd_bg);
571         theme->osd_bg = RrAppearanceCopy(theme->a_focused_title);
572     }
573     if (!read_appearance(db, inst, "osd.active.label.bg",
574                          theme->osd_hilite_label, TRUE) &&
575         !read_appearance(db, inst, "osd.label.bg",
576                          theme->osd_hilite_label, TRUE))
577     {
578         RrAppearanceFree(theme->osd_hilite_label);
579         theme->osd_hilite_label = RrAppearanceCopy(theme->a_focused_label);
580     }
581     if (!read_appearance(db, inst, "osd.inactive.label.bg",
582                          theme->osd_unhilite_label, TRUE))
583     {
584         RrAppearanceFree(theme->osd_unhilite_label);
585         theme->osd_unhilite_label = RrAppearanceCopy(theme->a_unfocused_label);
586     }
587     /* osd_hilite_fg can't be parentrel */
588     if (!read_appearance(db, inst, "osd.hilight.bg",
589                          theme->osd_hilite_bg, FALSE))
590     {
591         RrAppearanceFree(theme->osd_hilite_bg);
592         if (theme->a_focused_label->surface.grad != RR_SURFACE_PARENTREL)
593             theme->osd_hilite_bg = RrAppearanceCopy(theme->a_focused_label);
594         else
595             theme->osd_hilite_bg = RrAppearanceCopy(theme->a_focused_title);
596     }
597     /* osd_unhilite_fg can't be parentrel either */
598     if (!read_appearance(db, inst, "osd.unhilight.bg",
599                          theme->osd_unhilite_bg, FALSE))
600     {
601         RrAppearanceFree(theme->osd_unhilite_bg);
602         if (theme->a_unfocused_label->surface.grad != RR_SURFACE_PARENTREL)
603             theme->osd_unhilite_bg=RrAppearanceCopy(theme->a_unfocused_label);
604         else
605             theme->osd_unhilite_bg=RrAppearanceCopy(theme->a_unfocused_title);
606     }
607
608     /* osd buttons */
609     READ_APPEARANCE_COPY("osd.button.unpressed.bg", theme->osd_unpressed_button, TRUE, fbs.focused_unpressed);
610     READ_APPEARANCE_COPY_TEXTURES("osd.button.pressed.bg", theme->osd_pressed_button, TRUE, fbs.focused_pressed, 5);
611     READ_APPEARANCE_COPY_TEXTURES("osd.button.focused.bg", theme->osd_focused_button, TRUE, fbs.focused_unpressed, 5);
612
613     theme->a_icon->surface.grad =
614         theme->a_clear->surface.grad =
615         theme->a_clear_tex->surface.grad =
616         theme->a_menu_text_title->surface.grad =
617         theme->a_menu_normal->surface.grad =
618         theme->a_menu_disabled->surface.grad =
619         theme->a_menu_text_normal->surface.grad =
620         theme->a_menu_text_selected->surface.grad =
621         theme->a_menu_text_disabled->surface.grad =
622         theme->a_menu_text_disabled_selected->surface.grad =
623         theme->a_menu_bullet_normal->surface.grad =
624         theme->a_menu_bullet_selected->surface.grad = RR_SURFACE_PARENTREL;
625
626     /* set up the textures */
627     theme->a_focused_label->texture[0].type = RR_TEXTURE_TEXT;
628     theme->a_focused_label->texture[0].data.text.justify = winjust;
629     theme->a_focused_label->texture[0].data.text.font=theme->win_font_focused;
630     theme->a_focused_label->texture[0].data.text.color =
631         theme->title_focused_color;
632
633     if (read_string(db, "window.active.label.text.font", &str)) {
634         char *p;
635         gint i = 0;
636         gint j;
637         if (strstr(str, "shadow=y")) {
638             if ((p = strstr(str, "shadowoffset=")))
639                 i = parse_inline_number(p + strlen("shadowoffset="));
640             else
641                 i = 1;
642             theme->a_focused_label->texture[0].data.text.shadow_offset_x = i;
643             theme->a_focused_label->texture[0].data.text.shadow_offset_y = i;
644         }
645         if ((p = strstr(str, "shadowtint="))) {
646             i = parse_inline_number(p + strlen("shadowtint="));
647             j = (i > 0 ? 0 : 255);
648             i = ABS(i*255/100);
649
650             theme->title_focused_shadow_color = RrColorNew(inst, j, j, j);
651             theme->title_focused_shadow_alpha = i;
652         } else {
653             theme->title_focused_shadow_color = RrColorNew(inst, 0, 0, 0);
654             theme->title_focused_shadow_alpha = 50;
655         }
656     }
657
658     theme->a_focused_label->texture[0].data.text.shadow_color = theme->title_focused_shadow_color;
659     theme->a_focused_label->texture[0].data.text.shadow_alpha = theme->title_focused_shadow_alpha;
660
661     theme->osd_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
662     theme->osd_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
663     theme->osd_hilite_label->texture[0].data.text.font = theme->osd_font_hilite;
664     theme->osd_hilite_label->texture[0].data.text.color = theme->osd_text_active_color;
665
666     if (read_string(db, "osd.active.label.text.font", &str) ||
667         read_string(db, "osd.label.text.font", &str))
668     {
669         char *p;
670         gint i = 0;
671         gint j;
672         if (strstr(str, "shadow=y")) {
673             if ((p = strstr(str, "shadowoffset=")))
674                 i = parse_inline_number(p + strlen("shadowoffset="));
675             else
676                 i = 1;
677             theme->osd_hilite_label->texture[0].data.text.shadow_offset_x = i;
678             theme->osd_hilite_label->texture[0].data.text.shadow_offset_y = i;
679         }
680         if ((p = strstr(str, "shadowtint="))) {
681             i = parse_inline_number(p + strlen("shadowtint="));
682             j = (i > 0 ? 0 : 255);
683             i = ABS(i*255/100);
684
685             theme->osd_text_active_shadow_color = RrColorNew(inst, j, j, j);
686             theme->osd_text_active_shadow_alpha = i;
687         } else {
688             theme->osd_text_active_shadow_color = RrColorNew(inst, 0, 0, 0);
689             theme->osd_text_active_shadow_alpha = 50;
690         }
691     } else {
692         /* inherit the font settings from the focused label */
693         theme->osd_hilite_label->texture[0].data.text.shadow_offset_x =
694             theme->a_focused_label->texture[0].data.text.shadow_offset_x;
695         theme->osd_hilite_label->texture[0].data.text.shadow_offset_y =
696             theme->a_focused_label->texture[0].data.text.shadow_offset_y;
697         if (theme->title_focused_shadow_color)
698             theme->osd_text_active_shadow_color =
699                 RrColorCopy(theme->title_focused_shadow_color);
700         else
701             theme->osd_text_active_shadow_color = RrColorNew(inst, 0, 0, 0);
702         theme->osd_text_active_shadow_alpha =
703             theme->title_focused_shadow_alpha;
704     }
705
706     theme->osd_hilite_label->texture[0].data.text.shadow_color =
707         theme->osd_text_active_shadow_color;
708     theme->osd_hilite_label->texture[0].data.text.shadow_alpha =
709         theme->osd_text_active_shadow_alpha;
710
711     theme->osd_unpressed_button->texture[0].type =
712         theme->osd_pressed_button->texture[0].type =
713         theme->osd_focused_button->texture[0].type =
714         RR_TEXTURE_TEXT;
715
716     theme->osd_unpressed_button->texture[0].data.text.justify =
717         theme->osd_pressed_button->texture[0].data.text.justify =
718         theme->osd_focused_button->texture[0].data.text.justify =
719         RR_JUSTIFY_CENTER;
720
721     theme->osd_unpressed_button->texture[0].data.text.font =
722         theme->osd_pressed_button->texture[0].data.text.font =
723         theme->osd_focused_button->texture[0].data.text.font =
724         theme->osd_font_hilite;
725
726     theme->osd_unpressed_button->texture[0].data.text.color =
727         theme->osd_unpressed_color;
728     theme->osd_pressed_button->texture[0].data.text.color =
729         theme->osd_pressed_color;
730     theme->osd_focused_button->texture[0].data.text.color =
731         theme->osd_focused_color;
732
733     theme->osd_pressed_button->texture[1].data.lineart.color =
734         theme->osd_pressed_button->texture[2].data.lineart.color =
735         theme->osd_pressed_button->texture[3].data.lineart.color =
736         theme->osd_pressed_button->texture[4].data.lineart.color =
737         theme->osd_pressed_lineart;
738
739     theme->osd_focused_button->texture[1].data.lineart.color =
740         theme->osd_focused_button->texture[2].data.lineart.color =
741         theme->osd_focused_button->texture[3].data.lineart.color =
742         theme->osd_focused_button->texture[4].data.lineart.color =
743         theme->osd_focused_lineart;
744
745     theme->a_unfocused_label->texture[0].type = RR_TEXTURE_TEXT;
746     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
747     theme->a_unfocused_label->texture[0].data.text.font = theme->win_font_unfocused;
748     theme->a_unfocused_label->texture[0].data.text.color = theme->title_unfocused_color;
749
750     if (read_string(db, "window.inactive.label.text.font", &str)) {
751         char *p;
752         gint i = 0;
753         gint j;
754         if (strstr(str, "shadow=y")) {
755             if ((p = strstr(str, "shadowoffset=")))
756                 i = parse_inline_number(p + strlen("shadowoffset="));
757             else
758                 i = 1;
759             theme->a_unfocused_label->texture[0].data.text.shadow_offset_x = i;
760             theme->a_unfocused_label->texture[0].data.text.shadow_offset_y = i;
761         }
762         if ((p = strstr(str, "shadowtint="))) {
763             i = parse_inline_number(p + strlen("shadowtint="));
764             j = (i > 0 ? 0 : 255);
765             i = ABS(i*255/100);
766
767             theme->title_unfocused_shadow_color = RrColorNew(inst, j, j, j);
768             theme->title_unfocused_shadow_alpha = i;
769         } else {
770             theme->title_unfocused_shadow_color = RrColorNew(inst, 0, 0, 0);
771             theme->title_unfocused_shadow_alpha = 50;
772         }
773     }
774
775     theme->a_unfocused_label->texture[0].data.text.shadow_color =
776         theme->title_unfocused_shadow_color;
777     theme->a_unfocused_label->texture[0].data.text.shadow_alpha =
778         theme->title_unfocused_shadow_alpha;
779
780     theme->osd_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
781     theme->osd_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
782     theme->osd_unhilite_label->texture[0].data.text.font =
783         theme->osd_font_unhilite;
784     theme->osd_unhilite_label->texture[0].data.text.color =
785         theme->osd_text_inactive_color;
786
787     if (read_string(db, "osd.inactive.label.text.font", &str)) {
788         char *p;
789         gint i = 0;
790         gint j;
791         if (strstr(str, "shadow=y")) {
792             if ((p = strstr(str, "shadowoffset=")))
793                 i = parse_inline_number(p + strlen("shadowoffset="));
794             else
795                 i = 1;
796             theme->osd_unhilite_label->texture[0].data.text.shadow_offset_x=i;
797             theme->osd_unhilite_label->texture[0].data.text.shadow_offset_y=i;
798         }
799         if ((p = strstr(str, "shadowtint="))) {
800             i = parse_inline_number(p + strlen("shadowtint="));
801             j = (i > 0 ? 0 : 255);
802             i = ABS(i*255/100);
803
804             theme->osd_text_inactive_shadow_color = RrColorNew(inst, j, j, j);
805             theme->osd_text_inactive_shadow_alpha = i;
806         } else {
807             theme->osd_text_inactive_shadow_color = RrColorNew(inst, 0, 0, 0);
808             theme->osd_text_inactive_shadow_alpha = 50;
809         }
810     } else {
811         /* inherit the font settings from the unfocused label */
812         theme->osd_unhilite_label->texture[0].data.text.shadow_offset_x =
813             theme->a_unfocused_label->texture[0].data.text.shadow_offset_x;
814         theme->osd_unhilite_label->texture[0].data.text.shadow_offset_y =
815             theme->a_unfocused_label->texture[0].data.text.shadow_offset_y;
816         if (theme->title_unfocused_shadow_color)
817             theme->osd_text_inactive_shadow_color =
818                 RrColorCopy(theme->title_unfocused_shadow_color);
819         else
820             theme->osd_text_inactive_shadow_color = RrColorNew(inst, 0, 0, 0);
821         theme->osd_text_inactive_shadow_alpha =
822             theme->title_unfocused_shadow_alpha;
823     }
824
825     theme->osd_unhilite_label->texture[0].data.text.shadow_color =
826         theme->osd_text_inactive_shadow_color;
827     theme->osd_unhilite_label->texture[0].data.text.shadow_alpha =
828         theme->osd_text_inactive_shadow_alpha;
829
830     theme->a_menu_text_title->texture[0].type = RR_TEXTURE_TEXT;
831     theme->a_menu_text_title->texture[0].data.text.justify = mtitlejust;
832     theme->a_menu_text_title->texture[0].data.text.font = theme->menu_title_font;
833     theme->a_menu_text_title->texture[0].data.text.color = theme->menu_title_color;
834
835     if (read_string(db, "menu.title.text.font", &str)) {
836         char *p;
837         gint i = 0;
838         gint j;
839         if (strstr(str, "shadow=y")) {
840             if ((p = strstr(str, "shadowoffset=")))
841                 i = parse_inline_number(p + strlen("shadowoffset="));
842             else
843                 i = 1;
844             theme->a_menu_text_title->texture[0].data.text.shadow_offset_x = i;
845             theme->a_menu_text_title->texture[0].data.text.shadow_offset_y = i;
846         }
847         if ((p = strstr(str, "shadowtint="))) {
848             i = parse_inline_number(p + strlen("shadowtint="));
849             j = (i > 0 ? 0 : 255);
850             i = ABS(i*255/100);
851
852             theme->menu_title_shadow_color = RrColorNew(inst, j, j, j);
853         } else {
854             theme->menu_title_shadow_color = RrColorNew(inst, 0, 0, 0);
855             i = 50;
856         }
857
858         theme->a_menu_text_title->texture[0].data.text.shadow_color =
859             theme->menu_title_shadow_color;
860         theme->a_menu_text_title->texture[0].data.text.shadow_alpha =
861             i;
862     }
863
864     theme->a_menu_text_normal->texture[0].type =
865         theme->a_menu_text_selected->texture[0].type =
866         theme->a_menu_text_disabled->texture[0].type =
867         theme->a_menu_text_disabled_selected->texture[0].type =
868         RR_TEXTURE_TEXT;
869     theme->a_menu_text_normal->texture[0].data.text.justify =
870         theme->a_menu_text_selected->texture[0].data.text.justify =
871         theme->a_menu_text_disabled->texture[0].data.text.justify =
872         theme->a_menu_text_disabled_selected->texture[0].data.text.justify =
873         RR_JUSTIFY_LEFT;
874     theme->a_menu_text_normal->texture[0].data.text.font =
875         theme->a_menu_text_selected->texture[0].data.text.font =
876         theme->a_menu_text_disabled->texture[0].data.text.font =
877         theme->a_menu_text_disabled_selected->texture[0].data.text.font =
878         theme->menu_font;
879     theme->a_menu_text_normal->texture[0].data.text.color = theme->menu_color;
880     theme->a_menu_text_selected->texture[0].data.text.color =
881         theme->menu_selected_color;
882     theme->a_menu_text_disabled->texture[0].data.text.color =
883         theme->menu_disabled_color;
884     theme->a_menu_text_disabled_selected->texture[0].data.text.color =
885         theme->menu_disabled_selected_color;
886
887     if (read_string(db, "menu.items.font", &str)) {
888         char *p;
889         gint i = 0;
890         gint j;
891         if (strstr(str, "shadow=y")) {
892             if ((p = strstr(str, "shadowoffset=")))
893                 i = parse_inline_number(p + strlen("shadowoffset="));
894             else
895                 i = 1;
896             theme->a_menu_text_normal->
897                 texture[0].data.text.shadow_offset_x = i;
898             theme->a_menu_text_normal->
899                 texture[0].data.text.shadow_offset_y = i;
900             theme->a_menu_text_selected->
901                 texture[0].data.text.shadow_offset_x = i;
902             theme->a_menu_text_selected->
903                 texture[0].data.text.shadow_offset_y = i;
904             theme->a_menu_text_disabled->
905                 texture[0].data.text.shadow_offset_x = i;
906             theme->a_menu_text_disabled->
907                 texture[0].data.text.shadow_offset_y = i;
908             theme->a_menu_text_disabled_selected->
909                 texture[0].data.text.shadow_offset_x = i;
910             theme->a_menu_text_disabled_selected->
911                 texture[0].data.text.shadow_offset_y = i;
912         }
913         if ((p = strstr(str, "shadowtint="))) {
914             i = parse_inline_number(p + strlen("shadowtint="));
915             j = (i > 0 ? 0 : 255);
916             i = ABS(i*255/100);
917
918             theme->menu_text_shadow_color = RrColorNew(inst, j, j, j);
919         } else {
920             theme->menu_text_shadow_color = RrColorNew(inst, 0, 0, 0);
921             i = 50;
922         }
923
924         theme->a_menu_text_normal->texture[0].data.text.shadow_color =
925             theme->a_menu_text_selected->texture[0].data.text.shadow_color =
926             theme->a_menu_text_disabled->texture[0].data.text.shadow_color =
927             theme->a_menu_text_disabled_selected->texture[0].data.text.shadow_color =
928             theme->menu_text_shadow_color;
929
930         theme->a_menu_text_normal->texture[0].data.text.shadow_alpha =
931             theme->a_menu_text_selected->texture[0].data.text.shadow_alpha =
932             theme->a_menu_text_disabled->texture[0].data.text.shadow_alpha =
933             theme->a_menu_text_disabled_selected->texture[0].data.text.shadow_alpha =
934             i;
935     }
936
937     theme->a_menu_bullet_normal->texture[0].type =
938         theme->a_menu_bullet_selected->texture[0].type = RR_TEXTURE_MASK;
939     theme->a_menu_bullet_normal->texture[0].data.mask.mask =
940     theme->a_menu_bullet_selected->texture[0].data.mask.mask =
941         theme->menu_bullet_mask;
942     theme->a_menu_bullet_normal->texture[0].data.mask.color =
943         theme->menu_bullet_color;
944     theme->a_menu_bullet_selected->texture[0].data.mask.color =
945         theme->menu_bullet_selected_color;
946
947     g_free(path);
948     XrmDestroyDatabase(db);
949
950     /* set the font heights */
951     theme->win_font_height = RrFontHeight(theme->win_font_focused,
952         theme->a_focused_label->texture[0].data.text.shadow_offset_y);
953     theme->win_font_height =
954         MAX(theme->win_font_height,
955             RrFontHeight(theme->win_font_focused,
956                 theme->a_unfocused_label->texture[0].data.text.shadow_offset_y));
957     theme->menu_title_font_height = RrFontHeight(theme->menu_title_font,
958         theme->a_menu_text_title->texture[0].data.text.shadow_offset_y);
959     theme->menu_font_height = RrFontHeight(theme->menu_font,
960         theme->a_menu_text_normal->texture[0].data.text.shadow_offset_y);
961
962     /* calculate some last extents */
963     {
964         gint ft, fb, fl, fr, ut, ub, ul, ur;
965
966         RrMargins(theme->a_focused_label, &fl, &ft, &fr, &fb);
967         RrMargins(theme->a_unfocused_label, &ul, &ut, &ur, &ub);
968         theme->label_height = theme->win_font_height + MAX(ft + fb, ut + ub) + 1;
969
970         /* this would be nice I think, since padding.width can now be 0,
971            but it breaks frame.c horribly and I don't feel like fixing that
972            right now, so if anyone complains, here is how to keep text from
973            going over the title's bevel/border with a padding.width of 0 and a
974            bevelless/borderless label
975            RrMargins(theme->a_focused_title, &fl, &ft, &fr, &fb);
976            RrMargins(theme->a_unfocused_title, &ul, &ut, &ur, &ub);
977            theme->title_height = theme->label_height +
978            MAX(MAX(theme->padding * 2, ft + fb),
979            MAX(theme->padding * 2, ut + ub));
980         */
981         theme->title_height = theme->label_height + theme->paddingy * 2;
982
983         RrMargins(theme->a_menu_title, &ul, &ut, &ur, &ub);
984         theme->menu_title_label_height = theme->menu_title_font_height+ut+ub;
985         theme->menu_title_height = theme->menu_title_label_height +
986             theme->paddingy * 2;
987     }
988     theme->button_size = theme->label_height - 2;
989     theme->grip_width = 25;
990
991     RrAppearanceFree(fbs.focused_disabled);
992     RrAppearanceFree(fbs.unfocused_disabled);
993     RrAppearanceFree(fbs.focused_hover);
994     RrAppearanceFree(fbs.unfocused_hover);
995     RrAppearanceFree(fbs.focused_unpressed);
996     RrAppearanceFree(fbs.focused_pressed);
997     RrAppearanceFree(fbs.unfocused_unpressed);
998     RrAppearanceFree(fbs.unfocused_pressed);
999     RrAppearanceFree(fbs.focused_hover_toggled);
1000     RrAppearanceFree(fbs.unfocused_hover_toggled);
1001     RrAppearanceFree(fbs.focused_unpressed_toggled);
1002     RrAppearanceFree(fbs.focused_pressed_toggled);
1003     RrAppearanceFree(fbs.unfocused_unpressed_toggled);
1004     RrAppearanceFree(fbs.unfocused_pressed_toggled);
1005
1006     return theme;
1007 }
1008
1009 void RrThemeFree(RrTheme *theme)
1010 {
1011     if (theme) {
1012         g_free(theme->name);
1013
1014         RrButtonFree(theme->btn_max);
1015         RrButtonFree(theme->btn_close);
1016         RrButtonFree(theme->btn_desk);
1017         RrButtonFree(theme->btn_shade);
1018         RrButtonFree(theme->btn_iconify);
1019
1020         RrColorFree(theme->menu_border_color);
1021         RrColorFree(theme->osd_border_color);
1022         RrColorFree(theme->frame_focused_border_color);
1023         RrColorFree(theme->frame_undecorated_focused_border_color);
1024         RrColorFree(theme->frame_unfocused_border_color);
1025         RrColorFree(theme->frame_undecorated_unfocused_border_color);
1026         RrColorFree(theme->title_separator_focused_color);
1027         RrColorFree(theme->title_separator_unfocused_color);
1028         RrColorFree(theme->cb_unfocused_color);
1029         RrColorFree(theme->cb_focused_color);
1030         RrColorFree(theme->title_focused_color);
1031         RrColorFree(theme->title_unfocused_color);
1032         RrColorFree(theme->titlebut_focused_disabled_color);
1033         RrColorFree(theme->titlebut_unfocused_disabled_color);
1034         RrColorFree(theme->titlebut_focused_hover_color);
1035         RrColorFree(theme->titlebut_unfocused_hover_color);
1036         RrColorFree(theme->titlebut_focused_hover_toggled_color);
1037         RrColorFree(theme->titlebut_unfocused_hover_toggled_color);
1038         RrColorFree(theme->titlebut_focused_pressed_toggled_color);
1039         RrColorFree(theme->titlebut_unfocused_pressed_toggled_color);
1040         RrColorFree(theme->titlebut_focused_unpressed_toggled_color);
1041         RrColorFree(theme->titlebut_unfocused_unpressed_toggled_color);
1042         RrColorFree(theme->titlebut_focused_pressed_color);
1043         RrColorFree(theme->titlebut_unfocused_pressed_color);
1044         RrColorFree(theme->titlebut_focused_unpressed_color);
1045         RrColorFree(theme->titlebut_unfocused_unpressed_color);
1046         RrColorFree(theme->menu_title_color);
1047         RrColorFree(theme->menu_sep_color);
1048         RrColorFree(theme->menu_color);
1049         RrColorFree(theme->menu_bullet_color);
1050         RrColorFree(theme->menu_bullet_selected_color);
1051         RrColorFree(theme->menu_selected_color);
1052         RrColorFree(theme->menu_disabled_color);
1053         RrColorFree(theme->menu_disabled_selected_color);
1054         RrColorFree(theme->title_focused_shadow_color);
1055         RrColorFree(theme->title_unfocused_shadow_color);
1056         RrColorFree(theme->osd_text_active_color);
1057         RrColorFree(theme->osd_text_inactive_color);
1058         RrColorFree(theme->osd_text_active_shadow_color);
1059         RrColorFree(theme->osd_text_inactive_shadow_color);
1060         RrColorFree(theme->osd_pressed_color);
1061         RrColorFree(theme->osd_unpressed_color);
1062         RrColorFree(theme->osd_focused_color);
1063         RrColorFree(theme->osd_pressed_lineart);
1064         RrColorFree(theme->osd_focused_lineart);
1065         RrColorFree(theme->menu_title_shadow_color);
1066         RrColorFree(theme->menu_text_shadow_color);
1067
1068         g_free(theme->def_win_icon);
1069         
1070         RrPixmapMaskFree(theme->menu_bullet_mask);
1071         RrPixmapMaskFree(theme->down_arrow_mask);
1072         RrPixmapMaskFree(theme->up_arrow_mask);
1073
1074         RrFontClose(theme->win_font_focused);
1075         RrFontClose(theme->win_font_unfocused);
1076         RrFontClose(theme->menu_title_font);
1077         RrFontClose(theme->menu_font);
1078         RrFontClose(theme->osd_font_hilite);
1079         RrFontClose(theme->osd_font_unhilite);
1080
1081         RrAppearanceFree(theme->a_focused_grip);
1082         RrAppearanceFree(theme->a_unfocused_grip);
1083         RrAppearanceFree(theme->a_focused_title);
1084         RrAppearanceFree(theme->a_unfocused_title);
1085         RrAppearanceFree(theme->a_focused_label);
1086         RrAppearanceFree(theme->a_unfocused_label);
1087         RrAppearanceFree(theme->a_icon);
1088         RrAppearanceFree(theme->a_focused_handle);
1089         RrAppearanceFree(theme->a_unfocused_handle);
1090         RrAppearanceFree(theme->a_menu);
1091         RrAppearanceFree(theme->a_menu_title);
1092         RrAppearanceFree(theme->a_menu_text_title);
1093         RrAppearanceFree(theme->a_menu_normal);
1094         RrAppearanceFree(theme->a_menu_selected);
1095         RrAppearanceFree(theme->a_menu_disabled);
1096         RrAppearanceFree(theme->a_menu_disabled_selected);
1097         RrAppearanceFree(theme->a_menu_text_normal);
1098         RrAppearanceFree(theme->a_menu_text_selected);
1099         RrAppearanceFree(theme->a_menu_text_disabled);
1100         RrAppearanceFree(theme->a_menu_text_disabled_selected);
1101         RrAppearanceFree(theme->a_menu_bullet_normal);
1102         RrAppearanceFree(theme->a_menu_bullet_selected);
1103         RrAppearanceFree(theme->a_clear);
1104         RrAppearanceFree(theme->a_clear_tex);
1105         RrAppearanceFree(theme->osd_bg);
1106         RrAppearanceFree(theme->osd_hilite_bg);
1107         RrAppearanceFree(theme->osd_hilite_label);
1108         RrAppearanceFree(theme->osd_unhilite_bg);
1109         RrAppearanceFree(theme->osd_unhilite_label);
1110         RrAppearanceFree(theme->osd_pressed_button);
1111         RrAppearanceFree(theme->osd_unpressed_button);
1112         RrAppearanceFree(theme->osd_focused_button);
1113
1114         g_slice_free(RrTheme, theme);
1115     }
1116 }
1117
1118 static XrmDatabase loaddb(const gchar *name, gchar **path)
1119 {
1120     GSList *it;
1121     XrmDatabase db = NULL;
1122     gchar *s;
1123
1124     if (name[0] == '/') {
1125         s = g_build_filename(name, "openbox-3", "themerc", NULL);
1126         if ((db = XrmGetFileDatabase(s)))
1127             *path = g_path_get_dirname(s);
1128         g_free(s);
1129     } else {
1130         ObtPaths *p;
1131
1132         p = obt_paths_new();
1133
1134         /* XXX backwards compatibility, remove me sometime later */
1135         s = g_build_filename(g_get_home_dir(), ".themes", name,
1136                              "openbox-3", "themerc", NULL);
1137         if ((db = XrmGetFileDatabase(s)))
1138             *path = g_path_get_dirname(s);
1139         g_free(s);
1140
1141         for (it = obt_paths_data_dirs(p); !db && it; it = g_slist_next(it))
1142         {
1143             s = g_build_filename(it->data, "themes", name,
1144                                  "openbox-3", "themerc", NULL);
1145             if ((db = XrmGetFileDatabase(s)))
1146                 *path = g_path_get_dirname(s);
1147             g_free(s);
1148         }
1149
1150         obt_paths_unref(p);
1151     }
1152
1153     if (db == NULL) {
1154         s = g_build_filename(name, "themerc", NULL);
1155         if ((db = XrmGetFileDatabase(s)))
1156             *path = g_path_get_dirname(s);
1157         g_free(s);
1158     }
1159
1160     return db;
1161 }
1162
1163 static gchar *create_class_name(const gchar *rname)
1164 {
1165     gchar *rclass = g_strdup(rname);
1166     gchar *p = rclass;
1167
1168     while (TRUE) {
1169         *p = toupper(*p);
1170         p = strchr(p+1, '.');
1171         if (p == NULL) break;
1172         ++p;
1173         if (*p == '\0') break;
1174     }
1175     return rclass;
1176 }
1177
1178 static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value)
1179 {
1180     gboolean ret = FALSE;
1181     gchar *rclass = create_class_name(rname);
1182     gchar *rettype, *end;
1183     XrmValue retvalue;
1184
1185     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1186         retvalue.addr != NULL) {
1187         *value = (gint)strtol(retvalue.addr, &end, 10);
1188         if (end != retvalue.addr)
1189             ret = TRUE;
1190     }
1191
1192     g_free(rclass);
1193     return ret;
1194 }
1195
1196 static gboolean read_string(XrmDatabase db, const gchar *rname, gchar **value)
1197 {
1198     gboolean ret = FALSE;
1199     gchar *rclass = create_class_name(rname);
1200     gchar *rettype;
1201     XrmValue retvalue;
1202
1203     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1204         retvalue.addr != NULL) {
1205         g_strstrip(retvalue.addr);
1206         *value = retvalue.addr;
1207         ret = TRUE;
1208     }
1209
1210     g_free(rclass);
1211     return ret;
1212 }
1213
1214 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
1215                            const gchar *rname, RrColor **value)
1216 {
1217     gboolean ret = FALSE;
1218     gchar *rclass = create_class_name(rname);
1219     gchar *rettype;
1220     XrmValue retvalue;
1221
1222     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1223         retvalue.addr != NULL) {
1224         RrColor *c;
1225
1226         /* retvalue.addr is inside the xrdb database so we can't destroy it
1227            but we can edit it in place, as g_strstrip does. */
1228         g_strstrip(retvalue.addr);
1229         c = RrColorParse(inst, retvalue.addr);
1230         if (c != NULL) {
1231             *value = c;
1232             ret = TRUE;
1233         }
1234     }
1235
1236     g_free(rclass);
1237     return ret;
1238 }
1239
1240 static gboolean read_mask(const RrInstance *inst, const gchar *path,
1241                           const gchar *maskname, RrPixmapMask **value)
1242 {
1243     gboolean ret = FALSE;
1244     gchar *s;
1245     gint hx, hy; /* ignored */
1246     guint w, h;
1247     guchar *b;
1248
1249     s = g_build_filename(path, maskname, NULL);
1250     if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) {
1251         ret = TRUE;
1252         *value = RrPixmapMaskNew(inst, w, h, (gchar*)b);
1253         XFree(b);
1254     }
1255     g_free(s);
1256
1257     return ret;
1258 }
1259
1260 static void parse_appearance(gchar *tex, RrSurfaceColorType *grad,
1261                              RrReliefType *relief, RrBevelType *bevel,
1262                              gboolean *interlaced, gboolean *border,
1263                              gboolean allow_trans)
1264 {
1265     gchar *t;
1266
1267     /* convert to all lowercase */
1268     for (t = tex; *t != '\0'; ++t)
1269         *t = g_ascii_tolower(*t);
1270
1271     if (allow_trans && strstr(tex, "parentrelative") != NULL) {
1272         *grad = RR_SURFACE_PARENTREL;
1273     } else {
1274         if (strstr(tex, "gradient") != NULL) {
1275             if (strstr(tex, "crossdiagonal") != NULL)
1276                 *grad = RR_SURFACE_CROSS_DIAGONAL;
1277             else if (strstr(tex, "pyramid") != NULL)
1278                 *grad = RR_SURFACE_PYRAMID;
1279             else if (strstr(tex, "mirrorhorizontal") != NULL)
1280                 *grad = RR_SURFACE_MIRROR_HORIZONTAL;
1281             else if (strstr(tex, "horizontal") != NULL)
1282                 *grad = RR_SURFACE_HORIZONTAL;
1283             else if (strstr(tex, "splitvertical") != NULL)
1284                 *grad = RR_SURFACE_SPLIT_VERTICAL;
1285             else if (strstr(tex, "vertical") != NULL)
1286                 *grad = RR_SURFACE_VERTICAL;
1287             else
1288                 *grad = RR_SURFACE_DIAGONAL;
1289         } else {
1290             *grad = RR_SURFACE_SOLID;
1291         }
1292     }
1293
1294     if (strstr(tex, "sunken") != NULL)
1295         *relief = RR_RELIEF_SUNKEN;
1296     else if (strstr(tex, "flat") != NULL)
1297         *relief = RR_RELIEF_FLAT;
1298     else if (strstr(tex, "raised") != NULL)
1299         *relief = RR_RELIEF_RAISED;
1300     else
1301         *relief = (*grad == RR_SURFACE_PARENTREL) ?
1302                   RR_RELIEF_FLAT : RR_RELIEF_RAISED;
1303
1304     *border = FALSE;
1305     if (*relief == RR_RELIEF_FLAT) {
1306         if (strstr(tex, "border") != NULL)
1307             *border = TRUE;
1308     } else {
1309         if (strstr(tex, "bevel2") != NULL)
1310             *bevel = RR_BEVEL_2;
1311         else
1312             *bevel = RR_BEVEL_1;
1313     }
1314
1315     if (strstr(tex, "interlaced") != NULL)
1316         *interlaced = TRUE;
1317     else
1318         *interlaced = FALSE;
1319 }
1320
1321 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
1322                                 const gchar *rname, RrAppearance *value,
1323                                 gboolean allow_trans)
1324 {
1325     gboolean ret = FALSE;
1326     gchar *rclass = create_class_name(rname);
1327     gchar *cname, *ctoname, *bcname, *icname, *hname, *sname;
1328     gchar *csplitname, *ctosplitname;
1329     gchar *rettype;
1330     XrmValue retvalue;
1331     gint i;
1332
1333     cname = g_strconcat(rname, ".color", NULL);
1334     ctoname = g_strconcat(rname, ".colorTo", NULL);
1335     bcname = g_strconcat(rname, ".border.color", NULL);
1336     icname = g_strconcat(rname, ".interlace.color", NULL);
1337     hname = g_strconcat(rname, ".highlight", NULL);
1338     sname = g_strconcat(rname, ".shadow", NULL);
1339     csplitname = g_strconcat(rname, ".color.splitTo", NULL);
1340     ctosplitname = g_strconcat(rname, ".colorTo.splitTo", NULL);
1341
1342     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1343         retvalue.addr != NULL) {
1344         parse_appearance(retvalue.addr,
1345                          &value->surface.grad,
1346                          &value->surface.relief,
1347                          &value->surface.bevel,
1348                          &value->surface.interlaced,
1349                          &value->surface.border,
1350                          allow_trans);
1351         if (!read_color(db, inst, cname, &value->surface.primary))
1352             value->surface.primary = RrColorNew(inst, 0, 0, 0);
1353         if (!read_color(db, inst, ctoname, &value->surface.secondary))
1354             value->surface.secondary = RrColorNew(inst, 0, 0, 0);
1355         if (value->surface.border)
1356             if (!read_color(db, inst, bcname,
1357                             &value->surface.border_color))
1358                 value->surface.border_color = RrColorNew(inst, 0, 0, 0);
1359         if (value->surface.interlaced)
1360             if (!read_color(db, inst, icname,
1361                             &value->surface.interlace_color))
1362                 value->surface.interlace_color = RrColorNew(inst, 0, 0, 0);
1363         if (read_int(db, hname, &i) && i >= 0)
1364             value->surface.bevel_light_adjust = i;
1365         if (read_int(db, sname, &i) && i >= 0 && i <= 256)
1366             value->surface.bevel_dark_adjust = i;
1367
1368         if (value->surface.grad == RR_SURFACE_SPLIT_VERTICAL) {
1369             gint r, g, b;
1370
1371             if (!read_color(db, inst, csplitname,
1372                             &value->surface.split_primary))
1373             {
1374                 r = value->surface.primary->r;
1375                 r += r >> 2;
1376                 g = value->surface.primary->g;
1377                 g += g >> 2;
1378                 b = value->surface.primary->b;
1379                 b += b >> 2;
1380                 if (r > 0xFF) r = 0xFF;
1381                 if (g > 0xFF) g = 0xFF;
1382                 if (b > 0xFF) b = 0xFF;
1383                 value->surface.split_primary = RrColorNew(inst, r, g, b);
1384             }
1385
1386             if (!read_color(db, inst, ctosplitname,
1387                             &value->surface.split_secondary))
1388             {
1389                 r = value->surface.secondary->r;
1390                 r += r >> 4;
1391                 g = value->surface.secondary->g;
1392                 g += g >> 4;
1393                 b = value->surface.secondary->b;
1394                 b += b >> 4;
1395                 if (r > 0xFF) r = 0xFF;
1396                 if (g > 0xFF) g = 0xFF;
1397                 if (b > 0xFF) b = 0xFF;
1398                 value->surface.split_secondary = RrColorNew(inst, r, g, b);
1399             }
1400         }
1401
1402         ret = TRUE;
1403     }
1404
1405     g_free(ctosplitname);
1406     g_free(csplitname);
1407     g_free(sname);
1408     g_free(hname);
1409     g_free(icname);
1410     g_free(bcname);
1411     g_free(ctoname);
1412     g_free(cname);
1413     g_free(rclass);
1414     return ret;
1415 }
1416
1417 static int parse_inline_number(const char *p)
1418 {
1419     int neg = 1;
1420     int res = 0;
1421     if (*p == '-') {
1422         neg = -1;
1423         ++p;
1424     }
1425     for (; isdigit(*p); ++p)
1426         res = res * 10 + *p - '0';
1427     res *= neg;
1428     return res;
1429 }
1430
1431 static void set_default_appearance(RrAppearance *a)
1432 {
1433     a->surface.grad = RR_SURFACE_SOLID;
1434     a->surface.relief = RR_RELIEF_FLAT;
1435     a->surface.bevel = RR_BEVEL_1;
1436     a->surface.interlaced = FALSE;
1437     a->surface.border = FALSE;
1438     a->surface.primary = RrColorNew(a->inst, 0, 0, 0);
1439     a->surface.secondary = RrColorNew(a->inst, 0, 0, 0);
1440 }
1441
1442 /* Reads the output from gimp's C-Source file format into valid RGBA data for
1443    an RrTextureRGBA. */
1444 static RrPixel32* read_c_image(gint width, gint height, const guint8 *data)
1445 {
1446     RrPixel32 *im, *p;
1447     gint i;
1448
1449     p = im = g_memdup(data, width * height * sizeof(RrPixel32));
1450
1451     for (i = 0; i < width * height; ++i) {
1452         guchar a = ((*p >> 24) & 0xff);
1453         guchar b = ((*p >> 16) & 0xff);
1454         guchar g = ((*p >>  8) & 0xff);
1455         guchar r = ((*p >>  0) & 0xff);
1456
1457         *p = ((r << RrDefaultRedOffset) +
1458               (g << RrDefaultGreenOffset) +
1459               (b << RrDefaultBlueOffset) +
1460               (a << RrDefaultAlphaOffset));
1461         p++;
1462     }
1463
1464     return im;
1465 }
1466
1467 static void read_button_styles(XrmDatabase db, const RrInstance *inst, 
1468                                gchar *path,
1469                                const RrTheme *theme, RrButton *btn, 
1470                                const gchar *btnname,
1471                                struct fallbacks *fbs,
1472                                guchar *normal_mask,
1473                                guchar *toggled_mask)
1474 {
1475     gchar name[128], name2[128];
1476     gboolean userdef = TRUE;
1477
1478     g_snprintf(name, 128, "%s.xbm", btnname);
1479     if (!read_mask(inst, path, name, &btn->unpressed_mask) && normal_mask)
1480     {
1481         btn->unpressed_mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)normal_mask);
1482         userdef = FALSE;
1483     }
1484     g_snprintf(name, 128, "%s_toggled.xbm", btnname);
1485     if (toggled_mask && !read_mask(inst, path, name, &btn->unpressed_toggled_mask))
1486     {
1487         if (userdef)
1488             btn->unpressed_toggled_mask = RrPixmapMaskCopy(btn->unpressed_mask);
1489         else
1490             btn->unpressed_toggled_mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)toggled_mask);
1491     }
1492 #define READ_BUTTON_MASK_COPY(type, fallback) \
1493     g_snprintf(name, 128, "%s_" #type ".xbm", btnname); \
1494     READ_MASK_COPY(name, btn->type##_mask, fallback);
1495
1496     READ_BUTTON_MASK_COPY(pressed, btn->unpressed_mask);
1497     READ_BUTTON_MASK_COPY(disabled, btn->unpressed_mask);
1498     READ_BUTTON_MASK_COPY(hover, btn->unpressed_mask);
1499     if (toggled_mask) {
1500         READ_BUTTON_MASK_COPY(pressed_toggled, btn->unpressed_toggled_mask);
1501         READ_BUTTON_MASK_COPY(hover_toggled, btn->unpressed_toggled_mask);
1502     }
1503
1504 #define READ_BUTTON_APPEARANCE(typedots, type, fallback) \
1505     g_snprintf(name, 128, "window.active.button.%s." typedots ".image.color", btnname); \
1506     READ_COLOR(name, btn->focused_##type##_color, RrColorCopy(theme->titlebut_focused_##type##_color)); \
1507     g_snprintf(name, 128, "window.inactive.button.%s." typedots ".image.color", btnname); \
1508     READ_COLOR(name, btn->unfocused_##type##_color, RrColorCopy(theme->titlebut_unfocused_##type##_color)); \
1509     if (fallback) { \
1510         g_snprintf(name, 128, "window.active.button.%s." typedots ".bg", btnname); \
1511         g_snprintf(name2, 128, "window.active.button.%s.toggled.bg", btnname); \
1512         READ_APPEARANCE_(name, name2, btn->a_focused_##type, TRUE, fbs->focused_##type); \
1513         g_snprintf(name, 128, "window.inactive.button.%s." typedots ".bg", btnname); \
1514         g_snprintf(name2, 128, "window.inactive.button.%s.toggled.bg", btnname); \
1515         READ_APPEARANCE_(name, name2, btn->a_unfocused_##type, TRUE, fbs->unfocused_##type); \
1516     } else { \
1517         g_snprintf(name, 128, "window.active.button.%s." typedots ".bg", btnname); \
1518         READ_APPEARANCE_COPY(name, btn->a_focused_##type, TRUE, fbs->focused_##type); \
1519         g_snprintf(name, 128, "window.inactive.button.%s." typedots ".bg", btnname); \
1520         READ_APPEARANCE_COPY(name, btn->a_unfocused_##type, TRUE, fbs->unfocused_##type); \
1521     } \
1522     btn->a_unfocused_##type->texture[0].typ##e = \
1523       btn->a_focused_##type->texture[0].typ##e = \
1524         RR_TEXTURE_MASK; \
1525     btn->a_unfocused_##type->texture[0].data.mask.mask = \
1526       btn->a_focused_##type->texture[0].data.mask.mask = \
1527         btn->type##_mask; \
1528     btn->a_unfocused_##type->texture[0].data.mask.color = \
1529         btn->unfocused_##type##_color; \
1530     btn->a_focused_##type->texture[0].data.mask.color = \
1531         btn->focused_##type##_color;
1532
1533     READ_BUTTON_APPEARANCE("unpressed", unpressed, 0);
1534     READ_BUTTON_APPEARANCE("pressed", pressed, 0);
1535     READ_BUTTON_APPEARANCE("disabled", disabled, 0);
1536     READ_BUTTON_APPEARANCE("hover", hover, 0);
1537     if (toggled_mask) {
1538         READ_BUTTON_APPEARANCE("unpressed.toggled", unpressed_toggled, 1);
1539         READ_BUTTON_APPEARANCE("pressed.toggled", pressed_toggled, 0);
1540         READ_BUTTON_APPEARANCE("hover.toggled", hover_toggled, 0);
1541     }
1542 }