4ac4c5b9021e59f2f6b80ce5359da47971629a94
[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 #include "icon.h"
26 #include "obt/paths.h"
27
28 #include <X11/Xlib.h>
29 #include <X11/Xresource.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 static XrmDatabase loaddb(const gchar *name, gchar **path);
35 static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value);
36 static gboolean read_string(XrmDatabase db, const gchar *rname, gchar **value);
37 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
38                            const gchar *rname, RrColor **value);
39 static gboolean read_mask(const RrInstance *inst, const gchar *path,
40                           RrTheme *theme, const gchar *maskname,
41                           RrPixmapMask **value);
42 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
43                                 const gchar *rname, RrAppearance *value,
44                                 gboolean allow_trans);
45 static int parse_inline_number(const char *p);
46 static RrPixel32* read_c_image(gint width, gint height, const guint8 *data);
47 static void set_default_appearance(RrAppearance *a);
48 static void read_button_colors(XrmDatabase db, const RrInstance *inst, 
49                                const RrTheme *theme, RrButton *btn, 
50                                const gchar *btnname);
51
52 static RrFont *get_font(RrFont *target, RrFont **default_font,
53                         const RrInstance *inst)
54 {
55     if (target) {
56         RrFontRef(target);
57         return target;
58     } else {
59         /* Only load the default font once */
60         if (*default_font) {
61             RrFontRef(*default_font);
62         } else {
63             *default_font = RrFontOpenDefault(inst);
64         }
65         return *default_font;
66     }
67 }
68
69 #define READ_INT(x_resstr, x_var, x_min, x_max, x_def) \
70     if (!read_int(db, x_resstr, & x_var) || \
71             x_var < x_min || x_var > x_max) \
72         x_var = x_def;
73
74 #define READ_COLOR(x_resstr, x_var, x_def) \
75     if (!read_color(db, inst, x_resstr, & x_var)) \
76         x_var = x_def;
77
78 #define READ_COLOR_(x_res1, x_res2, x_var, x_def) \
79     if (!read_color(db, inst, x_res1, & x_var) && \
80         !read_color(db, inst, x_res2, & x_var)) \
81         x_var = x_def;
82
83 #define READ_MASK_COPY(x_file, x_var, x_copysrc) \
84     if (!read_mask(inst, path, theme, x_file, & x_var)) \
85         x_var = RrPixmapMaskCopy(x_copysrc);
86
87 #define READ_APPEARANCE(x_resstr, x_var, x_parrel) \
88     if (!read_appearance(db, inst, x_resstr, x_var, x_parrel)) \
89         set_default_appearance(x_var);
90
91 #define READ_APPEARANCE_COPY(x_resstr, x_var, x_parrel, x_defval) \
92     if (!read_appearance(db, inst, x_resstr, x_var, x_parrel)) {\
93         RrAppearanceFree(x_var); \
94         x_var = RrAppearanceCopy(x_defval); }
95
96 #define READ_APPEARANCE_COPY_TEXTURES(x_resstr, x_var, x_parrel, x_defval, n_tex) \
97     if (!read_appearance(db, inst, x_resstr, x_var, x_parrel)) {\
98         RrAppearanceFree(x_var); \
99         x_var = RrAppearanceCopy(x_defval); \
100         RrAppearanceRemoveTextures(x_var); \
101         RrAppearanceAddTextures(x_var, 5); }
102
103 #define READ_APPEARANCE_(x_res1, x_res2, x_var, x_parrel, x_defval) \
104     if (!read_appearance(db, inst, x_res1, x_var, x_parrel) && \
105         !read_appearance(db, inst, x_res2, x_var, x_parrel)) {\
106         RrAppearanceFree(x_var); \
107         x_var = RrAppearanceCopy(x_defval); }
108
109 RrTheme* RrThemeNew(const RrInstance *inst, const gchar *name,
110                     gboolean allow_fallback,
111                     RrFont *active_window_font, RrFont *inactive_window_font,
112                     RrFont *menu_title_font, RrFont *menu_item_font,
113                     RrFont *active_osd_font, RrFont *inactive_osd_font)
114 {
115     XrmDatabase db = NULL;
116     RrJustify winjust, mtitlejust;
117     gchar *str;
118     RrTheme *theme;
119     RrFont *default_font = NULL;
120     gchar *path;
121     gboolean userdef;
122     gint menu_overlap = 0;
123     RrAppearance *a_disabled_focused_tmp;
124     RrAppearance *a_disabled_unfocused_tmp;
125     RrAppearance *a_hover_focused_tmp;
126     RrAppearance *a_hover_unfocused_tmp;
127     RrAppearance *a_focused_unpressed_tmp;
128     RrAppearance *a_focused_pressed_tmp;
129     RrAppearance *a_unfocused_unpressed_tmp;
130     RrAppearance *a_unfocused_pressed_tmp;
131     RrAppearance *a_toggled_hover_focused_tmp;
132     RrAppearance *a_toggled_hover_unfocused_tmp;
133     RrAppearance *a_toggled_focused_unpressed_tmp;
134     RrAppearance *a_toggled_focused_pressed_tmp;
135     RrAppearance *a_toggled_unfocused_unpressed_tmp;
136     RrAppearance *a_toggled_unfocused_pressed_tmp;
137
138     if (name) {
139         db = loaddb(name, &path);
140         if (db == NULL) {
141             g_message("Unable to load the theme '%s'", name);
142             if (allow_fallback)
143                 g_message("Falling back to the default theme '%s'",
144                           DEFAULT_THEME);
145             /* fallback to the default theme */
146             name = NULL;
147         }
148     }
149     if (name == NULL) {
150         if (allow_fallback) {
151             db = loaddb(DEFAULT_THEME, &path);
152             if (db == NULL) {
153                 g_message("Unable to load the theme '%s'", DEFAULT_THEME);
154                 return NULL;
155             }
156         } else
157             return NULL;
158     }
159
160     /* initialize temp reading textures */
161     a_disabled_focused_tmp = RrAppearanceNew(inst, 1);
162     a_disabled_unfocused_tmp = RrAppearanceNew(inst, 1);
163     a_hover_focused_tmp = RrAppearanceNew(inst, 1);
164     a_hover_unfocused_tmp = RrAppearanceNew(inst, 1);
165     a_toggled_focused_unpressed_tmp = RrAppearanceNew(inst, 1);
166     a_toggled_unfocused_unpressed_tmp = RrAppearanceNew(inst, 1);
167     a_toggled_hover_focused_tmp = RrAppearanceNew(inst, 1);
168     a_toggled_hover_unfocused_tmp = RrAppearanceNew(inst, 1);
169     a_toggled_focused_pressed_tmp = RrAppearanceNew(inst, 1);
170     a_toggled_unfocused_pressed_tmp = RrAppearanceNew(inst, 1);
171     a_focused_unpressed_tmp = RrAppearanceNew(inst, 1);
172     a_focused_pressed_tmp = RrAppearanceNew(inst, 1);
173     a_unfocused_unpressed_tmp = RrAppearanceNew(inst, 1);
174     a_unfocused_pressed_tmp = RrAppearanceNew(inst, 1);
175
176     /* initialize theme */
177     theme = g_slice_new0(RrTheme);
178
179     theme->inst = inst;
180     theme->name = g_strdup(name ? name : DEFAULT_THEME);
181
182     /* init buttons */
183     theme->btn_max = RrButtonNew(inst);
184     theme->btn_close = RrButtonNew(inst);
185     theme->btn_desk = RrButtonNew(inst);
186     theme->btn_shade = RrButtonNew(inst);
187     theme->btn_iconify = RrButtonNew(inst);
188
189     /* init appearances */
190     theme->a_focused_grip = RrAppearanceNew(inst, 0);
191     theme->a_unfocused_grip = RrAppearanceNew(inst, 0);
192     theme->a_focused_title = RrAppearanceNew(inst, 0);
193     theme->a_unfocused_title = RrAppearanceNew(inst, 0);
194     theme->a_focused_label = RrAppearanceNew(inst, 1);
195     theme->a_unfocused_label = RrAppearanceNew(inst, 1);
196     theme->a_icon = RrAppearanceNew(inst, 1);
197     theme->a_focused_handle = RrAppearanceNew(inst, 0);
198     theme->a_unfocused_handle = RrAppearanceNew(inst, 0);
199     theme->a_menu = RrAppearanceNew(inst, 0);
200     theme->a_menu_title = RrAppearanceNew(inst, 0);
201     theme->a_menu_text_title = RrAppearanceNew(inst, 1);
202     theme->a_menu_normal = RrAppearanceNew(inst, 0);
203     theme->a_menu_selected = RrAppearanceNew(inst, 0);
204     theme->a_menu_disabled = RrAppearanceNew(inst, 0);
205     /* a_menu_disabled_selected is copied from a_menu_selected */
206     theme->a_menu_text_normal = RrAppearanceNew(inst, 1);
207     theme->a_menu_text_selected = RrAppearanceNew(inst, 1);
208     theme->a_menu_text_disabled = RrAppearanceNew(inst, 1);
209     theme->a_menu_text_disabled_selected = RrAppearanceNew(inst, 1);
210     theme->a_menu_bullet_normal = RrAppearanceNew(inst, 1);
211     theme->a_menu_bullet_selected = RrAppearanceNew(inst, 1);
212     theme->a_clear = RrAppearanceNew(inst, 0);
213     theme->a_clear_tex = RrAppearanceNew(inst, 1);
214     theme->osd_bg = RrAppearanceNew(inst, 0);
215     theme->osd_hilite_label = RrAppearanceNew(inst, 1);
216     theme->osd_hilite_bg = RrAppearanceNew(inst, 0);
217     theme->osd_unhilite_label = RrAppearanceNew(inst, 1);
218     theme->osd_unhilite_bg = RrAppearanceNew(inst, 0);
219     theme->osd_unpressed_button = RrAppearanceNew(inst, 1);
220     theme->osd_pressed_button = RrAppearanceNew(inst, 5);
221     theme->osd_focused_button = RrAppearanceNew(inst, 5);
222
223     /* load the font stuff */
224     theme->win_font_focused = get_font(active_window_font,
225                                        &default_font, inst);
226     theme->win_font_unfocused = get_font(inactive_window_font,
227                                          &default_font, inst);
228
229     winjust = RR_JUSTIFY_LEFT;
230     if (read_string(db, "window.label.text.justify", &str)) {
231         if (!g_ascii_strcasecmp(str, "right"))
232             winjust = RR_JUSTIFY_RIGHT;
233         else if (!g_ascii_strcasecmp(str, "center"))
234             winjust = RR_JUSTIFY_CENTER;
235     }
236
237     theme->menu_title_font = get_font(menu_title_font, &default_font, inst);
238
239     mtitlejust = RR_JUSTIFY_LEFT;
240     if (read_string(db, "menu.title.text.justify", &str)) {
241         if (!g_ascii_strcasecmp(str, "right"))
242             mtitlejust = RR_JUSTIFY_RIGHT;
243         else if (!g_ascii_strcasecmp(str, "center"))
244             mtitlejust = RR_JUSTIFY_CENTER;
245     }
246
247     theme->menu_font = get_font(menu_item_font, &default_font, inst);
248
249     theme->osd_font_hilite = get_font(active_osd_font, &default_font, inst);
250     theme->osd_font_unhilite = get_font(inactive_osd_font, &default_font,inst);
251
252     /* load direct dimensions */
253     READ_INT("menu.overlap", menu_overlap, -100, 100, 0);
254     READ_INT("menu.overlap.x", theme->menu_overlap_x, -100, 100, menu_overlap);
255     READ_INT("menu.overlap.y", theme->menu_overlap_y, -100, 100, menu_overlap);
256     READ_INT("window.handle.width", theme->handle_height, 0, 100, 6);
257     READ_INT("padding.width", theme->paddingx, 0, 100, 3);
258     READ_INT("padding.height", theme->paddingy, 0, 100, theme->paddingx);
259     READ_INT("border.width", theme->fbwidth, 0, 100, 1);
260     READ_INT("menu.border.width", theme->mbwidth, 0, 100, theme->fbwidth);
261     READ_INT("osd.border.width", theme->obwidth, 0, 100, theme->fbwidth);
262     READ_INT("undecorated.border.width", theme->ubwidth, 0, 100,
263              theme->fbwidth);
264     READ_INT("menu.separator.width", theme->menu_sep_width, 1, 100, 1);
265     READ_INT("menu.separator.padding.width", theme->menu_sep_paddingx,
266              0, 100, 6);
267     READ_INT("menu.separator.padding.height", theme->menu_sep_paddingy,
268              0, 100, 3);
269     READ_INT("window.client.padding.width", theme->cbwidthx, 0, 100,
270              theme->paddingx);
271     READ_INT("window.client.padding.height", theme->cbwidthy, 0, 100,
272              theme->cbwidthx);
273
274     /* load colors */
275     READ_COLOR_("window.active.border.color", "border.color",
276                 theme->frame_focused_border_color, RrColorNew(inst, 0, 0, 0));
277     /* undecorated focused border color inherits from frame focused border
278        color */
279     READ_COLOR("window.undecorated.active.border.color",
280                theme->frame_undecorated_focused_border_color,
281                RrColorCopy(theme->frame_focused_border_color));
282     /* title separator focused color inherits from focused border color */
283     READ_COLOR("window.active.title.separator.color",
284                theme->title_separator_focused_color,
285                RrColorCopy(theme->frame_focused_border_color));
286
287     /* unfocused border color inherits from frame focused border color */
288     READ_COLOR("window.inactive.border.color",
289                theme->frame_unfocused_border_color,
290                RrColorCopy(theme->frame_focused_border_color));
291
292     /* undecorated unfocused border color inherits from frame unfocused border
293        color */
294     READ_COLOR("window.undecorated.inactive.border.color",
295                theme->frame_undecorated_unfocused_border_color,
296                RrColorCopy(theme->frame_unfocused_border_color));
297
298     /* title separator unfocused color inherits from unfocused border color */
299     READ_COLOR("window.inactive.title.separator.color",
300                theme->title_separator_unfocused_color,
301                RrColorCopy(theme->frame_unfocused_border_color));
302
303     /* menu border color inherits from frame focused border color */
304     READ_COLOR("menu.border.color", theme->menu_border_color,
305                RrColorCopy(theme->frame_focused_border_color));
306
307     /* osd border color inherits from frame focused border color */
308     READ_COLOR("osd.border.color", theme->osd_border_color,
309                RrColorCopy(theme->frame_focused_border_color));
310
311     READ_COLOR("window.active.client.color", theme->cb_focused_color,
312                RrColorNew(inst, 0xff, 0xff, 0xff));
313
314     READ_COLOR("window.inactive.client.color", theme->cb_unfocused_color,
315                RrColorNew(inst, 0xff, 0xff, 0xff));
316
317     READ_COLOR("window.active.label.text.color", theme->title_focused_color,
318                RrColorNew(inst, 0x0, 0x0, 0x0));
319
320     READ_COLOR("window.inactive.label.text.color", theme->title_unfocused_color,
321                RrColorNew(inst, 0xff, 0xff, 0xff));
322
323     READ_COLOR_("osd.active.label.text.color",
324                 "osd.label.text.color",
325                 theme->osd_text_active_color, RrColorCopy(theme->title_focused_color));
326
327     READ_COLOR_("osd.inactive.label.text.color",
328                 "osd.label.text.color",
329                 theme->osd_text_inactive_color, RrColorCopy(theme->title_unfocused_color));
330
331     READ_COLOR("window.active.button.unpressed.image.color",
332                theme->titlebut_focused_unpressed_color,
333                RrColorNew(inst, 0, 0, 0));
334
335     READ_COLOR("window.inactive.button.unpressed.image.color",
336                theme->titlebut_unfocused_unpressed_color,
337                RrColorNew(inst, 0xff, 0xff, 0xff));
338
339     READ_COLOR("window.active.button.pressed.image.color",
340                theme->titlebut_focused_pressed_color,
341                RrColorCopy(theme->titlebut_focused_unpressed_color));
342
343     READ_COLOR("window.inactive.button.pressed.image.color",
344                theme->titlebut_unfocused_pressed_color,
345                RrColorCopy(theme->titlebut_unfocused_unpressed_color));
346
347     READ_COLOR("window.active.button.disabled.image.color",
348                theme->titlebut_disabled_focused_color,
349                RrColorNew(inst, 0xff, 0xff, 0xff));
350
351     READ_COLOR("window.inactive.button.disabled.image.color",
352                theme->titlebut_disabled_unfocused_color,
353                RrColorNew(inst, 0, 0, 0));
354
355     READ_COLOR("window.active.button.hover.image.color",
356                theme->titlebut_hover_focused_color,
357                RrColorCopy(theme->titlebut_focused_unpressed_color));
358
359     READ_COLOR("window.inactive.button.hover.image.color",
360                theme->titlebut_hover_unfocused_color,
361                RrColorCopy(theme->titlebut_unfocused_unpressed_color));
362
363     READ_COLOR_("window.active.button.toggled.unpressed.image.color",
364                 "window.active.button.toggled.image.color",
365                 theme->titlebut_toggled_focused_unpressed_color,
366                 RrColorCopy(theme->titlebut_focused_pressed_color));
367
368     READ_COLOR_("window.inactive.button.toggled.unpressed.image.color",
369                 "window.inactive.button.toggled.image.color",
370                 theme->titlebut_toggled_unfocused_unpressed_color,
371                 RrColorCopy(theme->titlebut_unfocused_pressed_color));
372
373     READ_COLOR("window.active.button.toggled.hover.image.color",
374                theme->titlebut_toggled_hover_focused_color,
375                RrColorCopy(theme->titlebut_toggled_focused_unpressed_color));
376
377     READ_COLOR("window.inactive.button.toggled.hover.image.color",
378                theme->titlebut_toggled_hover_unfocused_color,
379                RrColorCopy(theme->titlebut_toggled_unfocused_unpressed_color));
380
381     READ_COLOR("window.active.button.toggled.pressed.image.color",
382                theme->titlebut_toggled_focused_pressed_color,
383                RrColorCopy(theme->titlebut_focused_pressed_color));
384
385     READ_COLOR("window.inactive.button.toggled.pressed.image.color",
386                theme->titlebut_toggled_unfocused_pressed_color,
387                RrColorCopy(theme->titlebut_unfocused_pressed_color));
388
389     READ_COLOR("menu.title.text.color", theme->menu_title_color,
390                RrColorNew(inst, 0, 0, 0));
391
392     READ_COLOR("menu.items.text.color", theme->menu_color,
393                RrColorNew(inst, 0xff, 0xff, 0xff));
394
395     READ_COLOR("menu.bullet.image.color", theme->menu_bullet_color,
396                RrColorCopy(theme->menu_color));
397    
398     READ_COLOR("menu.items.disabled.text.color", theme->menu_disabled_color,
399                RrColorNew(inst, 0, 0, 0));
400
401     READ_COLOR("menu.items.active.disabled.text.color",
402                theme->menu_disabled_selected_color,
403                RrColorCopy(theme->menu_disabled_color));
404
405     READ_COLOR("menu.items.active.text.color", theme->menu_selected_color,
406                RrColorNew(inst, 0, 0, 0));
407
408     READ_COLOR("menu.separator.color", theme->menu_sep_color,
409                RrColorCopy(theme->menu_color));
410     
411     READ_COLOR("menu.bullet.selected.image.color", 
412                theme->menu_bullet_selected_color,
413                RrColorCopy(theme->menu_selected_color));
414
415     READ_COLOR("osd.button.unpressed.text.color", theme->osd_unpressed_color,
416                RrColorCopy(theme->osd_text_active_color));
417     READ_COLOR("osd.button.pressed.text.color", theme->osd_pressed_color,
418                RrColorCopy(theme->osd_text_active_color));
419     READ_COLOR("osd.button.focused.text.color", theme->osd_focused_color,
420                RrColorCopy(theme->osd_text_active_color));
421     READ_COLOR("osd.button.pressed.box.color", theme->osd_pressed_lineart,
422                RrColorCopy(theme->titlebut_focused_pressed_color));
423     READ_COLOR("osd.button.focused.box.color", theme->osd_focused_lineart,
424                RrColorCopy(theme->titlebut_hover_focused_color));
425  
426     /* load the image masks */
427
428     /* maximize button masks */
429     userdef = TRUE;
430     if (!read_mask(inst, path, theme, "max.xbm", &theme->btn_max->mask)) {
431             guchar data[] = { 0x3f, 0x3f, 0x21, 0x21, 0x21, 0x3f };
432             theme->btn_max->mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)data);
433             userdef = FALSE;
434     }
435     if (!read_mask(inst, path, theme, "max_toggled.xbm",
436                    &theme->btn_max->toggled_mask))
437     {
438         if (userdef)
439             theme->btn_max->toggled_mask = RrPixmapMaskCopy(theme->btn_max->mask);
440         else {
441             guchar data[] = { 0x3e, 0x22, 0x2f, 0x29, 0x39, 0x0f };
442             theme->btn_max->toggled_mask = RrPixmapMaskNew(inst, 6, 6,(gchar*)data);
443         }
444     }
445     READ_MASK_COPY("max_pressed.xbm", theme->btn_max->pressed_mask,
446                    theme->btn_max->mask);
447     READ_MASK_COPY("max_disabled.xbm", theme->btn_max->disabled_mask,
448                    theme->btn_max->mask);
449     READ_MASK_COPY("max_hover.xbm", theme->btn_max->hover_mask, 
450                    theme->btn_max->mask);
451     READ_MASK_COPY("max_toggled_pressed.xbm", 
452                    theme->btn_max->toggled_pressed_mask, 
453                    theme->btn_max->toggled_mask);
454     READ_MASK_COPY("max_toggled_hover.xbm", 
455                    theme->btn_max->toggled_hover_mask,
456                    theme->btn_max->toggled_mask);
457
458     /* iconify button masks */
459     if (!read_mask(inst, path, theme, "iconify.xbm", &theme->btn_iconify->mask)) {
460         guchar data[] = { 0x00, 0x00, 0x00, 0x00, 0x3f, 0x3f };
461         theme->btn_iconify->mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)data);
462     }
463     READ_MASK_COPY("iconify_pressed.xbm", theme->btn_iconify->pressed_mask,
464                    theme->btn_iconify->mask);
465     READ_MASK_COPY("iconify_disabled.xbm", theme->btn_iconify->disabled_mask,
466                    theme->btn_iconify->mask);
467     READ_MASK_COPY("iconify_hover.xbm", theme->btn_iconify->hover_mask,
468                    theme->btn_iconify->mask);
469
470     /* all desktops button masks */
471     userdef = TRUE;
472     if (!read_mask(inst, path, theme, "desk.xbm", &theme->btn_desk->mask)) {
473         guchar data[] = { 0x33, 0x33, 0x00, 0x00, 0x33, 0x33 };
474         theme->btn_desk->mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)data);
475         userdef = FALSE;
476     }
477     if (!read_mask(inst, path, theme, "desk_toggled.xbm",
478                    &theme->btn_desk->toggled_mask)) {
479         if (userdef)
480             theme->btn_desk->toggled_mask = RrPixmapMaskCopy(theme->btn_desk->mask);
481         else {
482             guchar data[] = { 0x00, 0x1e, 0x1a, 0x16, 0x1e, 0x00 };
483             theme->btn_desk->toggled_mask =
484                 RrPixmapMaskNew(inst, 6, 6, (gchar*)data);
485         }
486     }
487     READ_MASK_COPY("desk_pressed.xbm", theme->btn_desk->pressed_mask,
488                    theme->btn_desk->mask);
489     READ_MASK_COPY("desk_disabled.xbm", theme->btn_desk->disabled_mask,
490                    theme->btn_desk->mask);
491     READ_MASK_COPY("desk_hover.xbm", theme->btn_desk->hover_mask, theme->btn_desk->mask);
492     READ_MASK_COPY("desk_toggled_pressed.xbm",
493                    theme->btn_desk->toggled_pressed_mask, theme->btn_desk->toggled_mask);
494     READ_MASK_COPY("desk_toggled_hover.xbm", theme->btn_desk->toggled_hover_mask,
495                    theme->btn_desk->toggled_mask);
496
497     /* shade button masks */
498     if (!read_mask(inst, path, theme, "shade.xbm", &theme->btn_shade->mask)) {
499         guchar data[] = { 0x3f, 0x3f, 0x00, 0x00, 0x00, 0x00 };
500         theme->btn_shade->mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)data);
501     }
502     READ_MASK_COPY("shade_toggled.xbm", theme->btn_shade->toggled_mask,
503                    theme->btn_shade->mask);
504     READ_MASK_COPY("shade_pressed.xbm", theme->btn_shade->pressed_mask,
505                    theme->btn_shade->mask);
506     READ_MASK_COPY("shade_disabled.xbm", theme->btn_shade->disabled_mask,
507                    theme->btn_shade->mask);
508     READ_MASK_COPY("shade_hover.xbm", theme->btn_shade->hover_mask,
509                    theme->btn_shade->mask);
510     READ_MASK_COPY("shade_toggled_pressed.xbm",
511                    theme->btn_shade->toggled_pressed_mask,
512                    theme->btn_shade->toggled_mask);
513     READ_MASK_COPY("shade_toggled_hover.xbm",
514                    theme->btn_shade->toggled_hover_mask, 
515                    theme->btn_shade->toggled_mask);
516
517     /* close button masks */
518     if (!read_mask(inst, path, theme, "close.xbm", &theme->btn_close->mask)) {
519         guchar data[] = { 0x33, 0x3f, 0x1e, 0x1e, 0x3f, 0x33 };
520         theme->btn_close->mask = RrPixmapMaskNew(inst, 6, 6, (gchar*)data);
521     }
522     READ_MASK_COPY("close_pressed.xbm", theme->btn_close->pressed_mask,
523                    theme->btn_close->mask);
524     READ_MASK_COPY("close_disabled.xbm", theme->btn_close->disabled_mask,
525                    theme->btn_close->mask);
526     READ_MASK_COPY("close_hover.xbm", theme->btn_close->hover_mask,
527                    theme->btn_close->mask);
528
529     /* submenu bullet mask */
530     if (!read_mask(inst, path, theme, "bullet.xbm", &theme->menu_bullet_mask))
531     {
532         guchar data[] = { 0x01, 0x03, 0x07, 0x0f, 0x07, 0x03, 0x01 };
533         theme->menu_bullet_mask = RrPixmapMaskNew(inst, 4, 7, (gchar*)data);
534     }
535
536     /* up and down arrows */
537     {
538         guchar data[] = { 0xfe, 0x00, 0x7c, 0x00, 0x38, 0x00, 0x10, 0x00 };
539         theme->down_arrow_mask = RrPixmapMaskNew(inst, 9, 4, (gchar*)data);
540     }
541     {
542         guchar data[] = { 0x10, 0x00, 0x38, 0x00, 0x7c, 0x00, 0xfe, 0x00 };
543         theme->up_arrow_mask = RrPixmapMaskNew(inst, 9, 4, (gchar*)data);
544     }
545
546     /* setup the default window icon */
547     theme->def_win_icon = read_c_image(OB_DEFAULT_ICON_WIDTH,
548                                        OB_DEFAULT_ICON_HEIGHT,
549                                        OB_DEFAULT_ICON_pixel_data);
550     theme->def_win_icon_w = OB_DEFAULT_ICON_WIDTH;
551     theme->def_win_icon_h = OB_DEFAULT_ICON_HEIGHT;
552
553     /* read the decoration textures */
554     READ_APPEARANCE("window.active.title.bg", theme->a_focused_title, FALSE);
555     READ_APPEARANCE("window.inactive.title.bg", theme->a_unfocused_title,
556                     FALSE);
557     READ_APPEARANCE("window.active.label.bg", theme->a_focused_label, TRUE);
558     READ_APPEARANCE("window.inactive.label.bg", theme->a_unfocused_label,
559                     TRUE);
560     READ_APPEARANCE("window.active.handle.bg", theme->a_focused_handle, FALSE);
561     READ_APPEARANCE("window.inactive.handle.bg",theme->a_unfocused_handle,
562                     FALSE);
563     READ_APPEARANCE("window.active.grip.bg", theme->a_focused_grip, TRUE);
564     READ_APPEARANCE("window.inactive.grip.bg", theme->a_unfocused_grip, TRUE);
565     READ_APPEARANCE("menu.items.bg", theme->a_menu, FALSE);
566     READ_APPEARANCE("menu.title.bg", theme->a_menu_title, TRUE);
567     READ_APPEARANCE("menu.items.active.bg", theme->a_menu_selected, TRUE);
568
569     theme->a_menu_disabled_selected =
570         RrAppearanceCopy(theme->a_menu_selected);
571
572     /* read appearances for non-decorations (on-screen-display) */
573     if (!read_appearance(db, inst, "osd.bg", theme->osd_bg, FALSE)) {
574         RrAppearanceFree(theme->osd_bg);
575         theme->osd_bg = RrAppearanceCopy(theme->a_focused_title);
576     }
577     if (!read_appearance(db, inst, "osd.active.label.bg",
578                          theme->osd_hilite_label, TRUE) &&
579         !read_appearance(db, inst, "osd.label.bg",
580                          theme->osd_hilite_label, TRUE)) {
581         RrAppearanceFree(theme->osd_hilite_label);
582         theme->osd_hilite_label = RrAppearanceCopy(theme->a_focused_label);
583     }
584     if (!read_appearance(db, inst, "osd.inactive.label.bg",
585                          theme->osd_unhilite_label, TRUE)) {
586         RrAppearanceFree(theme->osd_unhilite_label);
587         theme->osd_unhilite_label = RrAppearanceCopy(theme->a_unfocused_label);
588     }
589     /* osd_hilite_fg can't be parentrel */
590     if (!read_appearance(db, inst, "osd.hilight.bg",
591                          theme->osd_hilite_bg, FALSE)) {
592         RrAppearanceFree(theme->osd_hilite_bg);
593         if (theme->a_focused_label->surface.grad != RR_SURFACE_PARENTREL)
594             theme->osd_hilite_bg = RrAppearanceCopy(theme->a_focused_label);
595         else
596             theme->osd_hilite_bg = RrAppearanceCopy(theme->a_focused_title);
597     }
598     /* osd_unhilite_fg can't be parentrel either */
599     if (!read_appearance(db, inst, "osd.unhilight.bg",
600                          theme->osd_unhilite_bg, FALSE)) {
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     /* read buttons textures */
609
610     /* bases: unpressed, pressed, disabled */
611     READ_APPEARANCE("window.active.button.unpressed.bg",
612                     a_focused_unpressed_tmp, TRUE);
613     READ_APPEARANCE("window.inactive.button.unpressed.bg",
614                     a_unfocused_unpressed_tmp, TRUE);
615     READ_APPEARANCE("window.active.button.pressed.bg",
616                     a_focused_pressed_tmp, TRUE);
617     READ_APPEARANCE("window.inactive.button.pressed.bg",
618                     a_unfocused_pressed_tmp, TRUE);
619     READ_APPEARANCE("window.active.button.disabled.bg",
620                     a_disabled_focused_tmp, TRUE);
621     READ_APPEARANCE("window.inactive.button.disabled.bg",
622                     a_disabled_unfocused_tmp, TRUE);
623
624     /* hover */
625     READ_APPEARANCE_COPY("window.active.button.hover.bg",
626                          a_hover_focused_tmp, TRUE,
627                          a_focused_unpressed_tmp);
628     READ_APPEARANCE_COPY("window.inactive.button.hover.bg",
629                          a_hover_unfocused_tmp, TRUE,
630                          a_unfocused_unpressed_tmp);
631
632     /* toggled unpressed */
633     READ_APPEARANCE_("window.active.button.toggled.unpressed.bg",
634                      "window.active.button.toggled.bg",
635                      a_toggled_focused_unpressed_tmp, TRUE,
636                      a_focused_pressed_tmp);
637     READ_APPEARANCE_("window.inactive.button.toggled.unpressed.bg",
638                      "window.inactive.button.toggled.bg",
639                      a_toggled_unfocused_unpressed_tmp, TRUE,
640                      a_unfocused_pressed_tmp);
641
642     /* toggled pressed */
643     READ_APPEARANCE_COPY("window.active.button.toggled.pressed.bg",
644                          a_toggled_focused_pressed_tmp, TRUE,
645                          a_focused_pressed_tmp);
646     READ_APPEARANCE_COPY("window.inactive.button.toggled.pressed.bg",
647                          a_toggled_unfocused_pressed_tmp, TRUE,
648                          a_unfocused_pressed_tmp);
649
650     /* toggled hover */
651     READ_APPEARANCE_COPY("window.active.button.toggled.hover.bg",
652                          a_toggled_hover_focused_tmp, TRUE,
653                          a_toggled_focused_unpressed_tmp);
654     READ_APPEARANCE_COPY("window.inactive.button.toggled.hover.bg",
655                          a_toggled_hover_unfocused_tmp, TRUE,
656                          a_toggled_unfocused_unpressed_tmp);
657
658
659     /* now do individual buttons, if specified */
660
661     /* max button */
662     read_button_colors(db, inst, theme, theme->btn_max, "max");
663
664     /* bases:  unpressed, pressed, disabled */
665     READ_APPEARANCE_COPY("window.active.button.max.unpressed.bg",
666                          theme->btn_max->a_focused_unpressed, TRUE,
667                          a_focused_unpressed_tmp);
668     READ_APPEARANCE_COPY("window.inactive.button.max.unpressed.bg",
669                          theme->btn_max->a_unfocused_unpressed, TRUE,
670                          a_unfocused_unpressed_tmp);
671     READ_APPEARANCE_COPY("window.active.button.max.pressed.bg",
672                          theme->btn_max->a_focused_pressed, TRUE,
673                          a_focused_pressed_tmp);
674     READ_APPEARANCE_COPY("window.inactive.button.max.pressed.bg",
675                          theme->btn_max->a_unfocused_pressed, TRUE,
676                          a_unfocused_pressed_tmp);
677     READ_APPEARANCE_COPY("window.active.button.max.disabled.bg",
678                          theme->btn_max->a_disabled_focused, TRUE,
679                          a_disabled_focused_tmp);
680     READ_APPEARANCE_COPY("window.inactive.button.max.disabled.bg",
681                          theme->btn_max->a_disabled_unfocused, TRUE,
682                          a_disabled_unfocused_tmp);
683
684     /* hover */
685     READ_APPEARANCE_COPY("window.active.button.max.hover.bg",
686                          theme->btn_max->a_hover_focused, TRUE,
687                          a_hover_focused_tmp);
688     READ_APPEARANCE_COPY("window.inactive.button.max.hover.bg",
689                          theme->btn_max->a_hover_unfocused, TRUE,
690                          a_hover_unfocused_tmp);
691
692     /* toggled unpressed */
693     READ_APPEARANCE_("window.active.button.max.toggled.unpressed.bg",
694                      "window.active.button.max.toggled.bg",
695                      theme->btn_max->a_toggled_focused_unpressed, TRUE,
696                      a_toggled_focused_unpressed_tmp);
697     READ_APPEARANCE_("window.inactive.button.max.toggled.unpressed.bg",
698                      "window.inactive.button.max.toggled.bg",
699                      theme->btn_max->a_toggled_unfocused_unpressed, TRUE,
700                      a_toggled_unfocused_unpressed_tmp);
701
702     /* toggled pressed */
703     READ_APPEARANCE_COPY("window.active.button.max.toggled.pressed.bg",
704                          theme->btn_max->a_toggled_focused_pressed, TRUE,
705                          a_toggled_focused_pressed_tmp);
706     READ_APPEARANCE_COPY("window.inactive.button.max.toggled.pressed.bg",
707                          theme->btn_max->a_toggled_unfocused_pressed, TRUE,
708                          a_toggled_unfocused_pressed_tmp);
709
710     /* toggled hover */
711     READ_APPEARANCE_COPY("window.active.button.max.toggled.hover.bg",
712                          theme->btn_max->a_toggled_hover_focused, TRUE,
713                          a_toggled_hover_focused_tmp);
714     READ_APPEARANCE_COPY("window.inactive.button.max.toggled.hover.bg",
715                          theme->btn_max->a_toggled_hover_unfocused, TRUE,
716                          a_toggled_hover_unfocused_tmp);
717
718     /* close button */
719     read_button_colors(db, inst, theme, theme->btn_close, "close");
720
721     READ_APPEARANCE_COPY("window.active.button.close.unpressed.bg",
722                          theme->btn_close->a_focused_unpressed, TRUE,
723                          a_focused_unpressed_tmp);
724     READ_APPEARANCE_COPY("window.inactive.button.close.unpressed.bg",
725                          theme->btn_close->a_unfocused_unpressed, TRUE,
726                          a_unfocused_unpressed_tmp);
727     READ_APPEARANCE_COPY("window.active.button.close.pressed.bg",
728                          theme->btn_close->a_focused_pressed, TRUE,
729                          a_focused_pressed_tmp);
730     READ_APPEARANCE_COPY("window.inactive.button.close.pressed.bg",
731                          theme->btn_close->a_unfocused_pressed, TRUE,
732                          a_unfocused_pressed_tmp);
733     READ_APPEARANCE_COPY("window.active.button.close.disabled.bg",
734                          theme->btn_close->a_disabled_focused, TRUE,
735                          a_disabled_focused_tmp);
736     READ_APPEARANCE_COPY("window.inactive.button.close.disabled.bg",
737                          theme->btn_close->a_disabled_unfocused, TRUE,
738                          a_disabled_unfocused_tmp);
739     READ_APPEARANCE_COPY("window.active.button.close.hover.bg",
740                          theme->btn_close->a_hover_focused, TRUE,
741                          a_hover_focused_tmp);
742     READ_APPEARANCE_COPY("window.inactive.button.close.hover.bg",
743                          theme->btn_close->a_hover_unfocused, TRUE,
744                          a_hover_unfocused_tmp);
745
746     /* desk button */
747     read_button_colors(db, inst, theme, theme->btn_desk, "desk");
748
749     /* bases:  unpressed, pressed, disabled */
750     READ_APPEARANCE_COPY("window.active.button.desk.unpressed.bg",
751                          theme->btn_desk->a_focused_unpressed, TRUE,
752                          a_focused_unpressed_tmp);
753     READ_APPEARANCE_COPY("window.inactive.button.desk.unpressed.bg",
754                          theme->btn_desk->a_unfocused_unpressed, TRUE,
755                          a_unfocused_unpressed_tmp);
756     READ_APPEARANCE_COPY("window.active.button.desk.pressed.bg",
757                          theme->btn_desk->a_focused_pressed, TRUE,
758                          a_focused_pressed_tmp);
759     READ_APPEARANCE_COPY("window.inactive.button.desk.pressed.bg",
760                          theme->btn_desk->a_unfocused_pressed, TRUE,
761                          a_unfocused_pressed_tmp);
762     READ_APPEARANCE_COPY("window.active.button.desk.disabled.bg",
763                          theme->btn_desk->a_disabled_focused, TRUE,
764                          a_disabled_focused_tmp);
765     READ_APPEARANCE_COPY("window.inactive.button.desk.disabled.bg",
766                          theme->btn_desk->a_disabled_unfocused, TRUE,
767                          a_disabled_unfocused_tmp);
768
769     /* hover */
770     READ_APPEARANCE_COPY("window.active.button.desk.hover.bg",
771                          theme->btn_desk->a_hover_focused, TRUE,
772                          a_hover_focused_tmp);
773     READ_APPEARANCE_COPY("window.inactive.button.desk.hover.bg",
774                          theme->btn_desk->a_hover_unfocused, TRUE,
775                          a_hover_unfocused_tmp);
776
777     /* toggled unpressed */
778     READ_APPEARANCE_("window.active.button.desk.toggled.unpressed.bg",
779                      "window.active.button.desk.toggled.bg",
780                      theme->btn_desk->a_toggled_focused_unpressed, TRUE,
781                      a_toggled_focused_unpressed_tmp);
782     READ_APPEARANCE_("window.inactive.button.desk.toggled.unpressed.bg",
783                      "window.inactive.button.desk.toggled.bg",
784                      theme->btn_desk->a_toggled_unfocused_unpressed, TRUE,
785                      a_toggled_unfocused_unpressed_tmp);
786
787     /* toggled pressed */
788     READ_APPEARANCE_COPY("window.active.button.desk.toggled.pressed.bg",
789                          theme->btn_desk->a_toggled_focused_pressed, TRUE,
790                          a_toggled_focused_pressed_tmp);
791     READ_APPEARANCE_COPY("window.inactive.button.desk.toggled.pressed.bg",
792                          theme->btn_desk->a_toggled_unfocused_pressed, TRUE,
793                          a_toggled_unfocused_pressed_tmp);
794
795     /* toggled hover */
796     READ_APPEARANCE_COPY("window.active.button.desk.toggled.hover.bg",
797                          theme->btn_desk->a_toggled_hover_focused, TRUE,
798                          a_toggled_hover_focused_tmp);
799     READ_APPEARANCE_COPY("window.inactive.button.desk.toggled.hover.bg",
800                          theme->btn_desk->a_toggled_hover_unfocused, TRUE,
801                          a_toggled_hover_unfocused_tmp);
802
803     /* shade button */
804     read_button_colors(db, inst, theme, theme->btn_shade, "shade");
805
806     /* bases:  unpressed, pressed, disabled */
807     READ_APPEARANCE_COPY("window.active.button.shade.unpressed.bg",
808                          theme->btn_shade->a_focused_unpressed, TRUE,
809                          a_focused_unpressed_tmp);
810     READ_APPEARANCE_COPY("window.inactive.button.shade.unpressed.bg",
811                          theme->btn_shade->a_unfocused_unpressed, TRUE,
812                          a_unfocused_unpressed_tmp);
813     READ_APPEARANCE_COPY("window.active.button.shade.pressed.bg",
814                          theme->btn_shade->a_focused_pressed, TRUE,
815                          a_focused_pressed_tmp);
816     READ_APPEARANCE_COPY("window.inactive.button.shade.pressed.bg",
817                          theme->btn_shade->a_unfocused_pressed, TRUE,
818                          a_unfocused_pressed_tmp);
819     READ_APPEARANCE_COPY("window.active.button.shade.disabled.bg",
820                          theme->btn_shade->a_disabled_focused, TRUE,
821                          a_disabled_focused_tmp);
822     READ_APPEARANCE_COPY("window.inactive.button.shade.disabled.bg",
823                          theme->btn_shade->a_disabled_unfocused, TRUE,
824                          a_disabled_unfocused_tmp);
825
826     /* hover */
827     READ_APPEARANCE_COPY("window.active.button.shade.hover.bg",
828                          theme->btn_shade->a_hover_focused, TRUE,
829                          a_hover_focused_tmp);
830     READ_APPEARANCE_COPY("window.inactive.button.shade.hover.bg",
831                          theme->btn_shade->a_hover_unfocused, TRUE,
832                          a_hover_unfocused_tmp);
833
834     /* toggled unpressed */
835     READ_APPEARANCE_("window.active.button.shade.toggled.unpressed.bg",
836                      "window.active.button.shade.toggled.bg",
837                      theme->btn_shade->a_toggled_focused_unpressed, TRUE,
838                      a_toggled_focused_unpressed_tmp);
839     READ_APPEARANCE_("window.inactive.button.shade.toggled.unpressed.bg",
840                      "window.inactive.button.shade.toggled.bg",
841                      theme->btn_shade->a_toggled_unfocused_unpressed, TRUE,
842                      a_toggled_unfocused_unpressed_tmp);
843
844     /* toggled pressed */
845     READ_APPEARANCE_COPY("window.active.button.shade.toggled.pressed.bg",
846                          theme->btn_shade->a_toggled_focused_pressed, TRUE,
847                          a_toggled_focused_pressed_tmp);
848     READ_APPEARANCE_COPY("window.inactive.button.shade.toggled.pressed.bg",
849                          theme->btn_shade->a_toggled_unfocused_pressed, TRUE,
850                          a_toggled_unfocused_pressed_tmp);
851
852     /* toggled hover */
853     READ_APPEARANCE_COPY("window.active.button.shade.toggled.hover.bg",
854                          theme->btn_shade->a_toggled_hover_focused, TRUE,
855                          a_toggled_hover_focused_tmp);
856     READ_APPEARANCE_COPY("window.inactive.button.shade.toggled.hover.bg",
857                          theme->btn_shade->a_toggled_hover_unfocused, TRUE,
858                          a_toggled_hover_unfocused_tmp);
859
860     /* iconify button */
861     read_button_colors(db, inst, theme, theme->btn_iconify, "iconify");
862
863     READ_APPEARANCE_COPY("window.active.button.iconify.unpressed.bg",
864                          theme->btn_iconify->a_focused_unpressed, TRUE,
865                          a_focused_unpressed_tmp);
866     READ_APPEARANCE_COPY("window.inactive.button.iconify.unpressed.bg",
867                          theme->btn_iconify->a_unfocused_unpressed, TRUE,
868                          a_unfocused_unpressed_tmp);
869     READ_APPEARANCE_COPY("window.active.button.iconify.pressed.bg",
870                          theme->btn_iconify->a_focused_pressed, TRUE,
871                          a_focused_pressed_tmp);
872     READ_APPEARANCE_COPY("window.inactive.button.iconify.pressed.bg",
873                          theme->btn_iconify->a_unfocused_pressed, TRUE,
874                          a_unfocused_pressed_tmp);
875     READ_APPEARANCE_COPY("window.active.button.iconify.disabled.bg",
876                          theme->btn_iconify->a_disabled_focused, TRUE,
877                          a_disabled_focused_tmp);
878     READ_APPEARANCE_COPY("window.inactive.button.iconify.disabled.bg",
879                          theme->btn_iconify->a_disabled_unfocused, TRUE,
880                          a_disabled_unfocused_tmp);
881     READ_APPEARANCE_COPY("window.active.button.iconify.hover.bg",
882                          theme->btn_iconify->a_hover_focused, TRUE,
883                          a_hover_focused_tmp);
884     READ_APPEARANCE_COPY("window.inactive.button.iconify.hover.bg",
885                          theme->btn_iconify->a_hover_unfocused, TRUE,
886                          a_hover_unfocused_tmp);
887
888     /* osd buttons */
889     READ_APPEARANCE_COPY("osd.button.unpressed.bg", theme->osd_unpressed_button, TRUE, a_focused_unpressed_tmp);
890     READ_APPEARANCE_COPY_TEXTURES("osd.button.pressed.bg", theme->osd_pressed_button, TRUE, a_focused_pressed_tmp, 5);
891     READ_APPEARANCE_COPY_TEXTURES("osd.button.focused.bg", theme->osd_focused_button, TRUE, a_focused_unpressed_tmp, 5);
892
893     theme->a_icon->surface.grad =
894         theme->a_clear->surface.grad =
895         theme->a_clear_tex->surface.grad =
896         theme->a_menu_text_title->surface.grad =
897         theme->a_menu_normal->surface.grad =
898         theme->a_menu_disabled->surface.grad =
899         theme->a_menu_text_normal->surface.grad =
900         theme->a_menu_text_selected->surface.grad =
901         theme->a_menu_text_disabled->surface.grad =
902         theme->a_menu_text_disabled_selected->surface.grad =
903         theme->a_menu_bullet_normal->surface.grad =
904         theme->a_menu_bullet_selected->surface.grad = RR_SURFACE_PARENTREL;
905
906     /* set up the textures */
907     theme->a_focused_label->texture[0].type = RR_TEXTURE_TEXT;
908     theme->a_focused_label->texture[0].data.text.justify = winjust;
909     theme->a_focused_label->texture[0].data.text.font=theme->win_font_focused;
910     theme->a_focused_label->texture[0].data.text.color =
911         theme->title_focused_color;
912
913     if (read_string(db, "window.active.label.text.font", &str)) {
914         char *p;
915         gint i = 0;
916         gint j;
917         if (strstr(str, "shadow=y")) {
918             if ((p = strstr(str, "shadowoffset=")))
919                 i = parse_inline_number(p + strlen("shadowoffset="));
920             else
921                 i = 1;
922             theme->a_focused_label->texture[0].data.text.shadow_offset_x = i;
923             theme->a_focused_label->texture[0].data.text.shadow_offset_y = i;
924         }
925         if ((p = strstr(str, "shadowtint=")))
926         {
927             i = parse_inline_number(p + strlen("shadowtint="));
928             j = (i > 0 ? 0 : 255);
929             i = ABS(i*255/100);
930
931             theme->title_focused_shadow_color = RrColorNew(inst, j, j, j);
932             theme->title_focused_shadow_alpha = i;
933         } else {
934             theme->title_focused_shadow_color = RrColorNew(inst, 0, 0, 0);
935             theme->title_focused_shadow_alpha = 50;
936         }
937     }
938
939     theme->a_focused_label->texture[0].data.text.shadow_color =
940         theme->title_focused_shadow_color;
941     theme->a_focused_label->texture[0].data.text.shadow_alpha =
942         theme->title_focused_shadow_alpha;
943
944     theme->osd_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
945     theme->osd_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
946     theme->osd_hilite_label->texture[0].data.text.font =
947         theme->osd_font_hilite;
948     theme->osd_hilite_label->texture[0].data.text.color =
949         theme->osd_text_active_color;
950
951     if (read_string(db, "osd.active.label.text.font", &str) ||
952         read_string(db, "osd.label.text.font", &str))
953     {
954         char *p;
955         gint i = 0;
956         gint j;
957         if (strstr(str, "shadow=y")) {
958             if ((p = strstr(str, "shadowoffset=")))
959                 i = parse_inline_number(p + strlen("shadowoffset="));
960             else
961                 i = 1;
962             theme->osd_hilite_label->texture[0].data.text.shadow_offset_x = i;
963             theme->osd_hilite_label->texture[0].data.text.shadow_offset_y = i;
964         }
965         if ((p = strstr(str, "shadowtint=")))
966         {
967             i = parse_inline_number(p + strlen("shadowtint="));
968             j = (i > 0 ? 0 : 255);
969             i = ABS(i*255/100);
970
971             theme->osd_text_active_shadow_color = RrColorNew(inst, j, j, j);
972             theme->osd_text_active_shadow_alpha = i;
973         } else {
974             theme->osd_text_active_shadow_color = RrColorNew(inst, 0, 0, 0);
975             theme->osd_text_active_shadow_alpha = 50;
976         }
977     } else {
978         /* inherit the font settings from the focused label */
979         theme->osd_hilite_label->texture[0].data.text.shadow_offset_x =
980             theme->a_focused_label->texture[0].data.text.shadow_offset_x;
981         theme->osd_hilite_label->texture[0].data.text.shadow_offset_y =
982             theme->a_focused_label->texture[0].data.text.shadow_offset_y;
983         if (theme->title_focused_shadow_color)
984             theme->osd_text_active_shadow_color =
985                 RrColorCopy(theme->title_focused_shadow_color);
986         else
987             theme->osd_text_active_shadow_color = RrColorNew(inst, 0, 0, 0);
988         theme->osd_text_active_shadow_alpha =
989             theme->title_focused_shadow_alpha;
990     }
991
992     theme->osd_hilite_label->texture[0].data.text.shadow_color =
993         theme->osd_text_active_shadow_color;
994     theme->osd_hilite_label->texture[0].data.text.shadow_alpha =
995         theme->osd_text_active_shadow_alpha;
996
997     theme->osd_unpressed_button->texture[0].type =
998         theme->osd_pressed_button->texture[0].type =
999         theme->osd_focused_button->texture[0].type =
1000         RR_TEXTURE_TEXT;
1001
1002     theme->osd_unpressed_button->texture[0].data.text.justify =
1003         theme->osd_pressed_button->texture[0].data.text.justify =
1004         theme->osd_focused_button->texture[0].data.text.justify =
1005         RR_JUSTIFY_CENTER;
1006
1007     theme->osd_unpressed_button->texture[0].data.text.font =
1008         theme->osd_pressed_button->texture[0].data.text.font =
1009         theme->osd_focused_button->texture[0].data.text.font =
1010         theme->osd_font_hilite;
1011
1012     theme->osd_unpressed_button->texture[0].data.text.color =
1013         theme->osd_unpressed_color;
1014     theme->osd_pressed_button->texture[0].data.text.color =
1015         theme->osd_pressed_color;
1016     theme->osd_focused_button->texture[0].data.text.color =
1017         theme->osd_focused_color;
1018
1019     theme->osd_pressed_button->texture[1].data.lineart.color =
1020         theme->osd_pressed_button->texture[2].data.lineart.color =
1021         theme->osd_pressed_button->texture[3].data.lineart.color =
1022         theme->osd_pressed_button->texture[4].data.lineart.color =
1023         theme->osd_pressed_lineart;
1024
1025     theme->osd_focused_button->texture[1].data.lineart.color =
1026         theme->osd_focused_button->texture[2].data.lineart.color =
1027         theme->osd_focused_button->texture[3].data.lineart.color =
1028         theme->osd_focused_button->texture[4].data.lineart.color =
1029         theme->osd_focused_lineart;
1030
1031     theme->a_unfocused_label->texture[0].type = RR_TEXTURE_TEXT;
1032     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
1033     theme->a_unfocused_label->texture[0].data.text.font =
1034         theme->win_font_unfocused;
1035     theme->a_unfocused_label->texture[0].data.text.color =
1036         theme->title_unfocused_color;
1037
1038     if (read_string(db, "window.inactive.label.text.font", &str)) {
1039         char *p;
1040         gint i = 0;
1041         gint j;
1042         if (strstr(str, "shadow=y")) {
1043             if ((p = strstr(str, "shadowoffset=")))
1044                 i = parse_inline_number(p + strlen("shadowoffset="));
1045             else
1046                 i = 1;
1047             theme->a_unfocused_label->texture[0].data.text.shadow_offset_x = i;
1048             theme->a_unfocused_label->texture[0].data.text.shadow_offset_y = i;
1049         }
1050         if ((p = strstr(str, "shadowtint=")))
1051         {
1052             i = parse_inline_number(p + strlen("shadowtint="));
1053             j = (i > 0 ? 0 : 255);
1054             i = ABS(i*255/100);
1055
1056             theme->title_unfocused_shadow_color = RrColorNew(inst, j, j, j);
1057             theme->title_unfocused_shadow_alpha = i;
1058         } else {
1059             theme->title_unfocused_shadow_color = RrColorNew(inst, 0, 0, 0);
1060             theme->title_unfocused_shadow_alpha = 50;
1061         }
1062     }
1063
1064     theme->a_unfocused_label->texture[0].data.text.shadow_color =
1065         theme->title_unfocused_shadow_color;
1066     theme->a_unfocused_label->texture[0].data.text.shadow_alpha =
1067         theme->title_unfocused_shadow_alpha;
1068
1069     theme->osd_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
1070     theme->osd_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
1071     theme->osd_unhilite_label->texture[0].data.text.font =
1072         theme->osd_font_unhilite;
1073     theme->osd_unhilite_label->texture[0].data.text.color =
1074         theme->osd_text_inactive_color;
1075
1076     if (read_string(db, "osd.inactive.label.text.font", &str))
1077     {
1078         char *p;
1079         gint i = 0;
1080         gint j;
1081         if (strstr(str, "shadow=y")) {
1082             if ((p = strstr(str, "shadowoffset=")))
1083                 i = parse_inline_number(p + strlen("shadowoffset="));
1084             else
1085                 i = 1;
1086             theme->osd_unhilite_label->texture[0].data.text.shadow_offset_x=i;
1087             theme->osd_unhilite_label->texture[0].data.text.shadow_offset_y=i;
1088         }
1089         if ((p = strstr(str, "shadowtint=")))
1090         {
1091             i = parse_inline_number(p + strlen("shadowtint="));
1092             j = (i > 0 ? 0 : 255);
1093             i = ABS(i*255/100);
1094
1095             theme->osd_text_inactive_shadow_color = RrColorNew(inst, j, j, j);
1096             theme->osd_text_inactive_shadow_alpha = i;
1097         } else {
1098             theme->osd_text_inactive_shadow_color = RrColorNew(inst, 0, 0, 0);
1099             theme->osd_text_inactive_shadow_alpha = 50;
1100         }
1101     } else {
1102         /* inherit the font settings from the unfocused label */
1103         theme->osd_unhilite_label->texture[0].data.text.shadow_offset_x =
1104             theme->a_unfocused_label->texture[0].data.text.shadow_offset_x;
1105         theme->osd_unhilite_label->texture[0].data.text.shadow_offset_y =
1106             theme->a_unfocused_label->texture[0].data.text.shadow_offset_y;
1107         if (theme->title_unfocused_shadow_color)
1108             theme->osd_text_inactive_shadow_color =
1109                 RrColorCopy(theme->title_unfocused_shadow_color);
1110         else
1111             theme->osd_text_inactive_shadow_color = RrColorNew(inst, 0, 0, 0);
1112         theme->osd_text_inactive_shadow_alpha =
1113             theme->title_unfocused_shadow_alpha;
1114     }
1115
1116     theme->osd_unhilite_label->texture[0].data.text.shadow_color =
1117         theme->osd_text_inactive_shadow_color;
1118     theme->osd_unhilite_label->texture[0].data.text.shadow_alpha =
1119         theme->osd_text_inactive_shadow_alpha;
1120
1121     theme->a_menu_text_title->texture[0].type = RR_TEXTURE_TEXT;
1122     theme->a_menu_text_title->texture[0].data.text.justify = mtitlejust;
1123     theme->a_menu_text_title->texture[0].data.text.font =
1124         theme->menu_title_font;
1125     theme->a_menu_text_title->texture[0].data.text.color =
1126         theme->menu_title_color;
1127
1128     if (read_string(db, "menu.title.text.font", &str)) {
1129         char *p;
1130         gint i = 0;
1131         gint j;
1132         if (strstr(str, "shadow=y")) {
1133             if ((p = strstr(str, "shadowoffset=")))
1134                 i = parse_inline_number(p + strlen("shadowoffset="));
1135             else
1136                 i = 1;
1137             theme->a_menu_text_title->texture[0].data.text.shadow_offset_x = i;
1138             theme->a_menu_text_title->texture[0].data.text.shadow_offset_y = i;
1139         }
1140         if ((p = strstr(str, "shadowtint=")))
1141         {
1142             i = parse_inline_number(p + strlen("shadowtint="));
1143             j = (i > 0 ? 0 : 255);
1144             i = ABS(i*255/100);
1145
1146             theme->menu_title_shadow_color = RrColorNew(inst, j, j, j);
1147             theme->menu_title_shadow_alpha = i;
1148         } else {
1149             theme->menu_title_shadow_color = RrColorNew(inst, 0, 0, 0);
1150             theme->menu_title_shadow_alpha = 50;
1151         }
1152     }
1153
1154     theme->a_menu_text_title->texture[0].data.text.shadow_color =
1155         theme->menu_title_shadow_color;
1156     theme->a_menu_text_title->texture[0].data.text.shadow_alpha =
1157         theme->menu_title_shadow_alpha;
1158
1159     theme->a_menu_text_normal->texture[0].type =
1160         theme->a_menu_text_selected->texture[0].type =
1161         theme->a_menu_text_disabled->texture[0].type =
1162         theme->a_menu_text_disabled_selected->texture[0].type =
1163         RR_TEXTURE_TEXT;
1164     theme->a_menu_text_normal->texture[0].data.text.justify =
1165         theme->a_menu_text_selected->texture[0].data.text.justify =
1166         theme->a_menu_text_disabled->texture[0].data.text.justify =
1167         theme->a_menu_text_disabled_selected->texture[0].data.text.justify =
1168         RR_JUSTIFY_LEFT;
1169     theme->a_menu_text_normal->texture[0].data.text.font =
1170         theme->a_menu_text_selected->texture[0].data.text.font =
1171         theme->a_menu_text_disabled->texture[0].data.text.font =
1172         theme->a_menu_text_disabled_selected->texture[0].data.text.font =
1173         theme->menu_font;
1174     theme->a_menu_text_normal->texture[0].data.text.color = theme->menu_color;
1175     theme->a_menu_text_selected->texture[0].data.text.color =
1176         theme->menu_selected_color;
1177     theme->a_menu_text_disabled->texture[0].data.text.color =
1178         theme->menu_disabled_color;
1179     theme->a_menu_text_disabled_selected->texture[0].data.text.color =
1180         theme->menu_disabled_selected_color;
1181
1182     if (read_string(db, "menu.items.font", &str)) {
1183         char *p;
1184         gint i = 0;
1185         gint j;
1186         if (strstr(str, "shadow=y")) {
1187             if ((p = strstr(str, "shadowoffset=")))
1188                 i = parse_inline_number(p + strlen("shadowoffset="));
1189             else
1190                 i = 1;
1191             theme->a_menu_text_normal->
1192                 texture[0].data.text.shadow_offset_x = i;
1193             theme->a_menu_text_normal->
1194                 texture[0].data.text.shadow_offset_y = i;
1195             theme->a_menu_text_selected->
1196                 texture[0].data.text.shadow_offset_x = i;
1197             theme->a_menu_text_selected->
1198                 texture[0].data.text.shadow_offset_y = i;
1199             theme->a_menu_text_disabled->
1200                 texture[0].data.text.shadow_offset_x = i;
1201             theme->a_menu_text_disabled->
1202                 texture[0].data.text.shadow_offset_y = i;
1203             theme->a_menu_text_disabled_selected->
1204                 texture[0].data.text.shadow_offset_x = i;
1205             theme->a_menu_text_disabled_selected->
1206                 texture[0].data.text.shadow_offset_y = i;
1207         }
1208         if ((p = strstr(str, "shadowtint=")))
1209         {
1210             i = parse_inline_number(p + strlen("shadowtint="));
1211             j = (i > 0 ? 0 : 255);
1212             i = ABS(i*255/100);
1213
1214             theme->menu_text_normal_shadow_color = RrColorNew(inst, j, j, j);
1215             theme->menu_text_selected_shadow_color = RrColorNew(inst, j, j, j);
1216             theme->menu_text_disabled_shadow_color = RrColorNew(inst, j, j, j);
1217             theme->menu_text_normal_shadow_alpha = i;
1218             theme->menu_text_selected_shadow_alpha = i;
1219             theme->menu_text_disabled_shadow_alpha = i;
1220             theme->menu_text_disabled_selected_shadow_alpha = i;
1221         } else {
1222             theme->menu_text_normal_shadow_color = RrColorNew(inst, 0, 0, 0);
1223             theme->menu_text_selected_shadow_color = RrColorNew(inst, 0, 0, 0);
1224             theme->menu_text_disabled_shadow_color = RrColorNew(inst, 0, 0, 0);
1225             theme->menu_text_normal_shadow_alpha = 50;
1226             theme->menu_text_selected_shadow_alpha = 50;
1227             theme->menu_text_disabled_selected_shadow_alpha = 50;
1228         }
1229     }
1230
1231     theme->a_menu_text_normal->texture[0].data.text.shadow_color =
1232         theme->menu_text_normal_shadow_color;
1233     theme->a_menu_text_normal->texture[0].data.text.shadow_alpha =
1234         theme->menu_text_normal_shadow_alpha;
1235     theme->a_menu_text_selected->texture[0].data.text.shadow_color =
1236         theme->menu_text_selected_shadow_color;
1237     theme->a_menu_text_selected->texture[0].data.text.shadow_alpha =
1238         theme->menu_text_selected_shadow_alpha;
1239     theme->a_menu_text_disabled->texture[0].data.text.shadow_color =
1240         theme->menu_text_disabled_shadow_color;
1241     theme->a_menu_text_disabled->texture[0].data.text.shadow_alpha =
1242         theme->menu_text_disabled_shadow_alpha;
1243     theme->a_menu_text_disabled_selected->texture[0].data.text.shadow_color =
1244         theme->menu_text_disabled_shadow_color;
1245     theme->a_menu_text_disabled_selected->texture[0].data.text.shadow_alpha =
1246         theme->menu_text_disabled_shadow_alpha;
1247
1248     theme->btn_max->a_disabled_focused->texture[0].type =
1249         theme->btn_max->a_disabled_unfocused->texture[0].type =
1250         theme->btn_max->a_hover_focused->texture[0].type =
1251         theme->btn_max->a_hover_unfocused->texture[0].type =
1252         theme->btn_max->a_toggled_hover_focused->texture[0].type =
1253         theme->btn_max->a_toggled_hover_unfocused->texture[0].type =
1254         theme->btn_max->a_toggled_focused_unpressed->texture[0].type =
1255         theme->btn_max->a_toggled_unfocused_unpressed->texture[0].type =
1256         theme->btn_max->a_toggled_focused_pressed->texture[0].type =
1257         theme->btn_max->a_toggled_unfocused_pressed->texture[0].type =
1258         theme->btn_max->a_focused_unpressed->texture[0].type =
1259         theme->btn_max->a_focused_pressed->texture[0].type =
1260         theme->btn_max->a_unfocused_unpressed->texture[0].type =
1261         theme->btn_max->a_unfocused_pressed->texture[0].type =
1262         theme->btn_close->a_disabled_focused->texture[0].type =
1263         theme->btn_close->a_disabled_unfocused->texture[0].type =
1264         theme->btn_close->a_hover_focused->texture[0].type =
1265         theme->btn_close->a_hover_unfocused->texture[0].type =
1266         theme->btn_close->a_focused_unpressed->texture[0].type =
1267         theme->btn_close->a_focused_pressed->texture[0].type =
1268         theme->btn_close->a_unfocused_unpressed->texture[0].type =
1269         theme->btn_close->a_unfocused_pressed->texture[0].type =
1270         theme->btn_desk->a_disabled_focused->texture[0].type =
1271         theme->btn_desk->a_disabled_unfocused->texture[0].type =
1272         theme->btn_desk->a_hover_focused->texture[0].type =
1273         theme->btn_desk->a_hover_unfocused->texture[0].type =
1274         theme->btn_desk->a_toggled_hover_focused->texture[0].type =
1275         theme->btn_desk->a_toggled_hover_unfocused->texture[0].type =
1276         theme->btn_desk->a_toggled_focused_unpressed->texture[0].type =
1277         theme->btn_desk->a_toggled_unfocused_unpressed->texture[0].type =
1278         theme->btn_desk->a_toggled_focused_pressed->texture[0].type =
1279         theme->btn_desk->a_toggled_unfocused_pressed->texture[0].type =
1280         theme->btn_desk->a_focused_unpressed->texture[0].type =
1281         theme->btn_desk->a_focused_pressed->texture[0].type =
1282         theme->btn_desk->a_unfocused_unpressed->texture[0].type =
1283         theme->btn_desk->a_unfocused_pressed->texture[0].type =
1284         theme->btn_shade->a_disabled_focused->texture[0].type =
1285         theme->btn_shade->a_disabled_unfocused->texture[0].type =
1286         theme->btn_shade->a_hover_focused->texture[0].type =
1287         theme->btn_shade->a_hover_unfocused->texture[0].type =
1288         theme->btn_shade->a_toggled_hover_focused->texture[0].type =
1289         theme->btn_shade->a_toggled_hover_unfocused->texture[0].type =
1290         theme->btn_shade->a_toggled_focused_unpressed->texture[0].type =
1291         theme->btn_shade->a_toggled_unfocused_unpressed->texture[0].type =
1292         theme->btn_shade->a_toggled_focused_pressed->texture[0].type =
1293         theme->btn_shade->a_toggled_unfocused_pressed->texture[0].type =
1294         theme->btn_shade->a_focused_unpressed->texture[0].type =
1295         theme->btn_shade->a_focused_pressed->texture[0].type =
1296         theme->btn_shade->a_unfocused_unpressed->texture[0].type =
1297         theme->btn_shade->a_unfocused_pressed->texture[0].type =
1298         theme->btn_iconify->a_disabled_focused->texture[0].type =
1299         theme->btn_iconify->a_disabled_unfocused->texture[0].type =
1300         theme->btn_iconify->a_hover_focused->texture[0].type =
1301         theme->btn_iconify->a_hover_unfocused->texture[0].type =
1302         theme->btn_iconify->a_focused_unpressed->texture[0].type =
1303         theme->btn_iconify->a_focused_pressed->texture[0].type =
1304         theme->btn_iconify->a_unfocused_unpressed->texture[0].type =
1305         theme->btn_iconify->a_unfocused_pressed->texture[0].type =
1306         theme->a_menu_bullet_normal->texture[0].type =
1307         theme->a_menu_bullet_selected->texture[0].type = RR_TEXTURE_MASK;
1308
1309     theme->btn_max->a_disabled_focused->texture[0].data.mask.mask =
1310         theme->btn_max->a_disabled_unfocused->texture[0].data.mask.mask =
1311         theme->btn_max->disabled_mask;
1312     theme->btn_max->a_hover_focused->texture[0].data.mask.mask =
1313         theme->btn_max->a_hover_unfocused->texture[0].data.mask.mask =
1314         theme->btn_max->hover_mask;
1315     theme->btn_max->a_focused_pressed->texture[0].data.mask.mask =
1316         theme->btn_max->a_unfocused_pressed->texture[0].data.mask.mask =
1317         theme->btn_max->pressed_mask;
1318     theme->btn_max->a_focused_unpressed->texture[0].data.mask.mask =
1319         theme->btn_max->a_unfocused_unpressed->texture[0].data.mask.mask =
1320         theme->btn_max->mask;
1321     theme->btn_max->a_toggled_hover_focused->texture[0].data.mask.mask =
1322         theme->btn_max->a_toggled_hover_unfocused->texture[0].data.mask.mask =
1323         theme->btn_max->toggled_hover_mask;
1324     theme->btn_max->a_toggled_focused_unpressed->texture[0].data.mask.mask =
1325         theme->btn_max->a_toggled_unfocused_unpressed->
1326         texture[0].data.mask.mask = theme->btn_max->toggled_mask;
1327     theme->btn_max->a_toggled_focused_pressed->texture[0].data.mask.mask =
1328         theme->btn_max->a_toggled_unfocused_pressed->texture[0].data.mask.mask
1329         = theme->btn_max->toggled_pressed_mask;
1330     theme->btn_close->a_disabled_focused->texture[0].data.mask.mask =
1331         theme->btn_close->a_disabled_unfocused->texture[0].data.mask.mask =
1332         theme->btn_close->disabled_mask;
1333     theme->btn_close->a_hover_focused->texture[0].data.mask.mask =
1334         theme->btn_close->a_hover_unfocused->texture[0].data.mask.mask =
1335         theme->btn_close->hover_mask;
1336     theme->btn_close->a_focused_pressed->texture[0].data.mask.mask =
1337         theme->btn_close->a_unfocused_pressed->texture[0].data.mask.mask =
1338         theme->btn_close->pressed_mask;
1339     theme->btn_close->a_focused_unpressed->texture[0].data.mask.mask =
1340         theme->btn_close->a_unfocused_unpressed->texture[0].data.mask.mask =
1341         theme->btn_close->mask;
1342     theme->btn_desk->a_disabled_focused->texture[0].data.mask.mask =
1343         theme->btn_desk->a_disabled_unfocused->texture[0].data.mask.mask =
1344         theme->btn_desk->disabled_mask;
1345     theme->btn_desk->a_hover_focused->texture[0].data.mask.mask =
1346         theme->btn_desk->a_hover_unfocused->texture[0].data.mask.mask =
1347         theme->btn_desk->hover_mask;
1348     theme->btn_desk->a_focused_pressed->texture[0].data.mask.mask =
1349         theme->btn_desk->a_unfocused_pressed->texture[0].data.mask.mask =
1350         theme->btn_desk->pressed_mask;
1351     theme->btn_desk->a_focused_unpressed->texture[0].data.mask.mask =
1352         theme->btn_desk->a_unfocused_unpressed->texture[0].data.mask.mask =
1353         theme->btn_desk->mask;
1354     theme->btn_desk->a_toggled_hover_focused->texture[0].data.mask.mask =
1355         theme->btn_desk->a_toggled_hover_unfocused->texture[0].data.mask.mask =
1356         theme->btn_desk->toggled_hover_mask;
1357     theme->btn_desk->a_toggled_focused_unpressed->texture[0].data.mask.mask =
1358         theme->btn_desk->a_toggled_unfocused_unpressed->
1359         texture[0].data.mask.mask = theme->btn_desk->toggled_mask;
1360     theme->btn_desk->a_toggled_focused_pressed->texture[0].data.mask.mask =
1361         theme->btn_desk->a_toggled_unfocused_pressed->texture[0].data.mask.mask
1362         = theme->btn_desk->toggled_pressed_mask;
1363     theme->btn_shade->a_disabled_focused->texture[0].data.mask.mask =
1364         theme->btn_shade->a_disabled_unfocused->texture[0].data.mask.mask =
1365         theme->btn_shade->disabled_mask;
1366     theme->btn_shade->a_hover_focused->texture[0].data.mask.mask =
1367         theme->btn_shade->a_hover_unfocused->texture[0].data.mask.mask =
1368         theme->btn_shade->hover_mask;
1369     theme->btn_shade->a_focused_pressed->texture[0].data.mask.mask =
1370         theme->btn_shade->a_unfocused_pressed->texture[0].data.mask.mask =
1371         theme->btn_shade->pressed_mask;
1372     theme->btn_shade->a_focused_unpressed->texture[0].data.mask.mask =
1373         theme->btn_shade->a_unfocused_unpressed->texture[0].data.mask.mask =
1374         theme->btn_shade->mask;
1375     theme->btn_shade->a_toggled_hover_focused->texture[0].data.mask.mask =
1376         theme->btn_shade->a_toggled_hover_unfocused->texture[0].data.mask.mask
1377         = theme->btn_shade->toggled_hover_mask;
1378     theme->btn_shade->a_toggled_focused_unpressed->texture[0].data.mask.mask =
1379         theme->btn_shade->a_toggled_unfocused_unpressed->
1380         texture[0].data.mask.mask = theme->btn_shade->toggled_mask;
1381     theme->btn_shade->a_toggled_focused_pressed->texture[0].data.mask.mask =
1382         theme->btn_shade->a_toggled_unfocused_pressed->
1383         texture[0].data.mask.mask = theme->btn_shade->toggled_pressed_mask;
1384     theme->btn_iconify->a_disabled_focused->texture[0].data.mask.mask =
1385         theme->btn_iconify->a_disabled_unfocused->texture[0].data.mask.mask =
1386         theme->btn_iconify->disabled_mask;
1387     theme->btn_iconify->a_hover_focused->texture[0].data.mask.mask =
1388         theme->btn_iconify->a_hover_unfocused->texture[0].data.mask.mask =
1389         theme->btn_iconify->hover_mask;
1390     theme->btn_iconify->a_focused_pressed->texture[0].data.mask.mask =
1391         theme->btn_iconify->a_unfocused_pressed->texture[0].data.mask.mask =
1392         theme->btn_iconify->pressed_mask;
1393     theme->btn_iconify->a_focused_unpressed->texture[0].data.mask.mask =
1394         theme->btn_iconify->a_unfocused_unpressed->texture[0].data.mask.mask =
1395         theme->btn_iconify->mask;
1396     theme->a_menu_bullet_normal->texture[0].data.mask.mask =
1397     theme->a_menu_bullet_selected->texture[0].data.mask.mask =
1398         theme->menu_bullet_mask;
1399     theme->btn_max->a_disabled_focused->texture[0].data.mask.color = 
1400         theme->btn_max->disabled_focused_color;
1401     theme->btn_close->a_disabled_focused->texture[0].data.mask.color = 
1402         theme->btn_close->disabled_focused_color;
1403     theme->btn_desk->a_disabled_focused->texture[0].data.mask.color = 
1404         theme->btn_desk->disabled_focused_color;
1405     theme->btn_shade->a_disabled_focused->texture[0].data.mask.color = 
1406         theme->btn_shade->disabled_focused_color;
1407     theme->btn_iconify->a_disabled_focused->texture[0].data.mask.color = 
1408         theme->btn_iconify->disabled_focused_color;
1409     theme->btn_max->a_disabled_unfocused->texture[0].data.mask.color = 
1410         theme->btn_max->disabled_unfocused_color;
1411     theme->btn_close->a_disabled_unfocused->texture[0].data.mask.color = 
1412         theme->btn_close->disabled_unfocused_color;
1413     theme->btn_desk->a_disabled_unfocused->texture[0].data.mask.color = 
1414         theme->btn_desk->disabled_unfocused_color;
1415     theme->btn_shade->a_disabled_unfocused->texture[0].data.mask.color = 
1416         theme->btn_shade->disabled_unfocused_color;
1417     theme->btn_iconify->a_disabled_unfocused->texture[0].data.mask.color = 
1418         theme->btn_iconify->disabled_unfocused_color;
1419     theme->btn_max->a_hover_focused->texture[0].data.mask.color = 
1420         theme->btn_max->hover_focused_color;
1421     theme->btn_close->a_hover_focused->texture[0].data.mask.color = 
1422         theme->btn_close->hover_focused_color;
1423     theme->btn_desk->a_hover_focused->texture[0].data.mask.color = 
1424         theme->btn_desk->hover_focused_color;
1425     theme->btn_shade->a_hover_focused->texture[0].data.mask.color = 
1426         theme->btn_shade->hover_focused_color;
1427     theme->btn_iconify->a_hover_focused->texture[0].data.mask.color = 
1428         theme->btn_iconify->hover_focused_color;
1429     theme->btn_max->a_hover_unfocused->texture[0].data.mask.color = 
1430         theme->btn_max->hover_unfocused_color;
1431     theme->btn_close->a_hover_unfocused->texture[0].data.mask.color = 
1432         theme->btn_close->hover_unfocused_color;
1433     theme->btn_desk->a_hover_unfocused->texture[0].data.mask.color = 
1434         theme->btn_desk->hover_unfocused_color;
1435     theme->btn_shade->a_hover_unfocused->texture[0].data.mask.color = 
1436         theme->btn_shade->hover_unfocused_color;
1437     theme->btn_iconify->a_hover_unfocused->texture[0].data.mask.color = 
1438         theme->btn_iconify->hover_unfocused_color;
1439     theme->btn_max->a_toggled_hover_focused->texture[0].data.mask.color = 
1440         theme->btn_max->toggled_hover_focused_color;
1441     theme->btn_desk->a_toggled_hover_focused->texture[0].data.mask.color = 
1442         theme->btn_desk->toggled_hover_focused_color;
1443     theme->btn_shade->a_toggled_hover_focused->texture[0].data.mask.color = 
1444         theme->btn_shade->toggled_hover_focused_color;
1445     theme->btn_max->a_toggled_hover_unfocused->texture[0].data.mask.color = 
1446         theme->btn_max->toggled_hover_unfocused_color;
1447     theme->btn_desk->a_toggled_hover_unfocused->texture[0].data.mask.color = 
1448         theme->btn_desk->toggled_hover_unfocused_color;
1449     theme->btn_shade->a_toggled_hover_unfocused->texture[0].data.mask.color = 
1450         theme->btn_shade->toggled_hover_unfocused_color;
1451     theme->btn_max->a_toggled_focused_unpressed->texture[0].data.mask.color = 
1452         theme->btn_max->toggled_focused_unpressed_color;
1453     theme->btn_desk->a_toggled_focused_unpressed->texture[0].data.mask.color = 
1454         theme->btn_desk->toggled_focused_unpressed_color;
1455     theme->btn_shade->a_toggled_focused_unpressed->texture[0].data.mask.color = 
1456         theme->btn_shade->toggled_focused_unpressed_color;
1457     theme->btn_max->a_toggled_unfocused_unpressed->texture[0].data.mask.color = 
1458         theme->btn_max->toggled_unfocused_unpressed_color;
1459     theme->btn_desk->a_toggled_unfocused_unpressed->texture[0].data.mask.color
1460         = theme->btn_desk->toggled_unfocused_unpressed_color;
1461     theme->btn_shade->a_toggled_unfocused_unpressed->texture[0].data.mask.color
1462         = theme->btn_shade->toggled_unfocused_unpressed_color;
1463     theme->btn_max->a_toggled_focused_pressed->texture[0].data.mask.color = 
1464         theme->btn_max->toggled_focused_pressed_color;
1465     theme->btn_desk->a_toggled_focused_pressed->texture[0].data.mask.color = 
1466         theme->btn_desk->toggled_focused_pressed_color;
1467     theme->btn_shade->a_toggled_focused_pressed->texture[0].data.mask.color = 
1468         theme->btn_shade->toggled_focused_pressed_color;
1469     theme->btn_max->a_toggled_unfocused_pressed->texture[0].data.mask.color = 
1470         theme->btn_max->toggled_unfocused_pressed_color;
1471     theme->btn_desk->a_toggled_unfocused_pressed->texture[0].data.mask.color = 
1472         theme->btn_desk->toggled_unfocused_pressed_color;
1473     theme->btn_shade->a_toggled_unfocused_pressed->texture[0].data.mask.color = 
1474         theme->btn_shade->toggled_unfocused_pressed_color;
1475     theme->btn_max->a_focused_unpressed->texture[0].data.mask.color = 
1476         theme->btn_max->focused_unpressed_color;
1477     theme->btn_close->a_focused_unpressed->texture[0].data.mask.color = 
1478         theme->btn_close->focused_unpressed_color;
1479     theme->btn_desk->a_focused_unpressed->texture[0].data.mask.color = 
1480         theme->btn_desk->focused_unpressed_color;
1481     theme->btn_shade->a_focused_unpressed->texture[0].data.mask.color = 
1482         theme->btn_shade->focused_unpressed_color;
1483     theme->btn_iconify->a_focused_unpressed->texture[0].data.mask.color = 
1484         theme->btn_iconify->focused_unpressed_color;
1485     theme->btn_max->a_focused_pressed->texture[0].data.mask.color = 
1486         theme->btn_max->focused_pressed_color;
1487     theme->btn_close->a_focused_pressed->texture[0].data.mask.color = 
1488         theme->btn_close->focused_pressed_color;
1489     theme->btn_desk->a_focused_pressed->texture[0].data.mask.color = 
1490         theme->btn_desk->focused_pressed_color;
1491     theme->btn_shade->a_focused_pressed->texture[0].data.mask.color = 
1492         theme->btn_shade->focused_pressed_color;
1493     theme->btn_iconify->a_focused_pressed->texture[0].data.mask.color = 
1494         theme->btn_iconify->focused_pressed_color;
1495     theme->btn_max->a_unfocused_unpressed->texture[0].data.mask.color = 
1496         theme->btn_max->unfocused_unpressed_color;
1497     theme->btn_close->a_unfocused_unpressed->texture[0].data.mask.color = 
1498         theme->btn_close->unfocused_unpressed_color;
1499     theme->btn_desk->a_unfocused_unpressed->texture[0].data.mask.color = 
1500         theme->btn_desk->unfocused_unpressed_color;
1501     theme->btn_shade->a_unfocused_unpressed->texture[0].data.mask.color = 
1502         theme->btn_shade->unfocused_unpressed_color;
1503     theme->btn_iconify->a_unfocused_unpressed->texture[0].data.mask.color = 
1504         theme->btn_iconify->unfocused_unpressed_color;
1505     theme->btn_max->a_unfocused_pressed->texture[0].data.mask.color = 
1506         theme->btn_max->unfocused_pressed_color;
1507     theme->btn_close->a_unfocused_pressed->texture[0].data.mask.color = 
1508         theme->btn_close->unfocused_pressed_color;
1509     theme->btn_desk->a_unfocused_pressed->texture[0].data.mask.color = 
1510         theme->btn_desk->unfocused_pressed_color;
1511     theme->btn_shade->a_unfocused_pressed->texture[0].data.mask.color = 
1512         theme->btn_shade->unfocused_pressed_color;
1513     theme->btn_iconify->a_unfocused_pressed->texture[0].data.mask.color = 
1514         theme->btn_iconify->unfocused_pressed_color;
1515     theme->a_menu_bullet_normal->texture[0].data.mask.color =
1516         theme->menu_bullet_color;
1517     theme->a_menu_bullet_selected->texture[0].data.mask.color =
1518         theme->menu_bullet_selected_color;
1519
1520     g_free(path);
1521     XrmDestroyDatabase(db);
1522
1523     /* set the font heights */
1524     theme->win_font_height = RrFontHeight
1525         (theme->win_font_focused,
1526          theme->a_focused_label->texture[0].data.text.shadow_offset_y);
1527     theme->win_font_height =
1528         MAX(theme->win_font_height,
1529             RrFontHeight
1530             (theme->win_font_focused,
1531              theme->a_unfocused_label->texture[0].data.text.shadow_offset_y));
1532     theme->menu_title_font_height = RrFontHeight
1533         (theme->menu_title_font,
1534          theme->a_menu_text_title->texture[0].data.text.shadow_offset_y);
1535     theme->menu_font_height = RrFontHeight
1536         (theme->menu_font,
1537          theme->a_menu_text_normal->texture[0].data.text.shadow_offset_y);
1538
1539     /* calculate some last extents */
1540     {
1541         gint ft, fb, fl, fr, ut, ub, ul, ur;
1542
1543         RrMargins(theme->a_focused_label, &fl, &ft, &fr, &fb);
1544         RrMargins(theme->a_unfocused_label, &ul, &ut, &ur, &ub);
1545         theme->label_height = theme->win_font_height + MAX(ft + fb, ut + ub);
1546         theme->label_height += theme->label_height % 2;
1547
1548         /* this would be nice I think, since padding.width can now be 0,
1549            but it breaks frame.c horribly and I don't feel like fixing that
1550            right now, so if anyone complains, here is how to keep text from
1551            going over the title's bevel/border with a padding.width of 0 and a
1552            bevelless/borderless label
1553            RrMargins(theme->a_focused_title, &fl, &ft, &fr, &fb);
1554            RrMargins(theme->a_unfocused_title, &ul, &ut, &ur, &ub);
1555            theme->title_height = theme->label_height +
1556            MAX(MAX(theme->padding * 2, ft + fb),
1557            MAX(theme->padding * 2, ut + ub));
1558         */
1559         theme->title_height = theme->label_height + theme->paddingy * 2;
1560
1561         RrMargins(theme->a_menu_title, &ul, &ut, &ur, &ub);
1562         theme->menu_title_label_height = theme->menu_title_font_height+ut+ub;
1563         theme->menu_title_height = theme->menu_title_label_height +
1564             theme->paddingy * 2;
1565     }
1566     theme->button_size = theme->label_height - 2;
1567     theme->grip_width = 25;
1568
1569     RrAppearanceFree(a_disabled_focused_tmp);
1570     RrAppearanceFree(a_disabled_unfocused_tmp);
1571     RrAppearanceFree(a_hover_focused_tmp);
1572     RrAppearanceFree(a_hover_unfocused_tmp);
1573     RrAppearanceFree(a_focused_unpressed_tmp);
1574     RrAppearanceFree(a_focused_pressed_tmp);
1575     RrAppearanceFree(a_unfocused_unpressed_tmp);
1576     RrAppearanceFree(a_unfocused_pressed_tmp);
1577     RrAppearanceFree(a_toggled_hover_focused_tmp);
1578     RrAppearanceFree(a_toggled_hover_unfocused_tmp);
1579     RrAppearanceFree(a_toggled_focused_unpressed_tmp);
1580     RrAppearanceFree(a_toggled_focused_pressed_tmp);
1581     RrAppearanceFree(a_toggled_unfocused_unpressed_tmp);
1582     RrAppearanceFree(a_toggled_unfocused_pressed_tmp);
1583
1584     return theme;
1585 }
1586
1587 void RrThemeFree(RrTheme *theme)
1588 {
1589     if (theme) {
1590         g_free(theme->name);
1591
1592         RrButtonFree(theme->btn_max);
1593         RrButtonFree(theme->btn_close);
1594         RrButtonFree(theme->btn_desk);
1595         RrButtonFree(theme->btn_shade);
1596         RrButtonFree(theme->btn_iconify);
1597
1598         RrColorFree(theme->menu_border_color);
1599         RrColorFree(theme->osd_border_color);
1600         RrColorFree(theme->frame_focused_border_color);
1601         RrColorFree(theme->frame_undecorated_focused_border_color);
1602         RrColorFree(theme->frame_unfocused_border_color);
1603         RrColorFree(theme->frame_undecorated_unfocused_border_color);
1604         RrColorFree(theme->title_separator_focused_color);
1605         RrColorFree(theme->title_separator_unfocused_color);
1606         RrColorFree(theme->cb_unfocused_color);
1607         RrColorFree(theme->cb_focused_color);
1608         RrColorFree(theme->title_focused_color);
1609         RrColorFree(theme->title_unfocused_color);
1610         RrColorFree(theme->titlebut_disabled_focused_color);
1611         RrColorFree(theme->titlebut_disabled_unfocused_color);
1612         RrColorFree(theme->titlebut_hover_focused_color);
1613         RrColorFree(theme->titlebut_hover_unfocused_color);
1614         RrColorFree(theme->titlebut_toggled_hover_focused_color);
1615         RrColorFree(theme->titlebut_toggled_hover_unfocused_color);
1616         RrColorFree(theme->titlebut_toggled_focused_pressed_color);
1617         RrColorFree(theme->titlebut_toggled_unfocused_pressed_color);
1618         RrColorFree(theme->titlebut_toggled_focused_unpressed_color);
1619         RrColorFree(theme->titlebut_toggled_unfocused_unpressed_color);
1620         RrColorFree(theme->titlebut_focused_pressed_color);
1621         RrColorFree(theme->titlebut_unfocused_pressed_color);
1622         RrColorFree(theme->titlebut_focused_unpressed_color);
1623         RrColorFree(theme->titlebut_unfocused_unpressed_color);
1624         RrColorFree(theme->menu_title_color);
1625         RrColorFree(theme->menu_sep_color);
1626         RrColorFree(theme->menu_color);
1627         RrColorFree(theme->menu_bullet_color);
1628         RrColorFree(theme->menu_bullet_selected_color);
1629         RrColorFree(theme->menu_selected_color);
1630         RrColorFree(theme->menu_disabled_color);
1631         RrColorFree(theme->menu_disabled_selected_color);
1632         RrColorFree(theme->title_focused_shadow_color);
1633         RrColorFree(theme->title_unfocused_shadow_color);
1634         RrColorFree(theme->osd_text_active_color);
1635         RrColorFree(theme->osd_text_inactive_color);
1636         RrColorFree(theme->osd_text_active_shadow_color);
1637         RrColorFree(theme->osd_text_inactive_shadow_color);
1638         RrColorFree(theme->osd_pressed_color);
1639         RrColorFree(theme->osd_unpressed_color);
1640         RrColorFree(theme->osd_focused_color);
1641         RrColorFree(theme->osd_pressed_lineart);
1642         RrColorFree(theme->osd_focused_lineart);
1643         RrColorFree(theme->menu_title_shadow_color);
1644         RrColorFree(theme->menu_text_normal_shadow_color);
1645         RrColorFree(theme->menu_text_selected_shadow_color);
1646         RrColorFree(theme->menu_text_disabled_shadow_color);
1647         RrColorFree(theme->menu_text_disabled_selected_shadow_color);
1648
1649         g_free(theme->def_win_icon);
1650         
1651         RrPixmapMaskFree(theme->menu_bullet_mask);
1652         RrPixmapMaskFree(theme->down_arrow_mask);
1653         RrPixmapMaskFree(theme->up_arrow_mask);
1654
1655         RrFontClose(theme->win_font_focused);
1656         RrFontClose(theme->win_font_unfocused);
1657         RrFontClose(theme->menu_title_font);
1658         RrFontClose(theme->menu_font);
1659         RrFontClose(theme->osd_font_hilite);
1660         RrFontClose(theme->osd_font_unhilite);
1661
1662         RrAppearanceFree(theme->a_focused_grip);
1663         RrAppearanceFree(theme->a_unfocused_grip);
1664         RrAppearanceFree(theme->a_focused_title);
1665         RrAppearanceFree(theme->a_unfocused_title);
1666         RrAppearanceFree(theme->a_focused_label);
1667         RrAppearanceFree(theme->a_unfocused_label);
1668         RrAppearanceFree(theme->a_icon);
1669         RrAppearanceFree(theme->a_focused_handle);
1670         RrAppearanceFree(theme->a_unfocused_handle);
1671         RrAppearanceFree(theme->a_menu);
1672         RrAppearanceFree(theme->a_menu_title);
1673         RrAppearanceFree(theme->a_menu_text_title);
1674         RrAppearanceFree(theme->a_menu_normal);
1675         RrAppearanceFree(theme->a_menu_selected);
1676         RrAppearanceFree(theme->a_menu_disabled);
1677         RrAppearanceFree(theme->a_menu_disabled_selected);
1678         RrAppearanceFree(theme->a_menu_text_normal);
1679         RrAppearanceFree(theme->a_menu_text_selected);
1680         RrAppearanceFree(theme->a_menu_text_disabled);
1681         RrAppearanceFree(theme->a_menu_text_disabled_selected);
1682         RrAppearanceFree(theme->a_menu_bullet_normal);
1683         RrAppearanceFree(theme->a_menu_bullet_selected);
1684         RrAppearanceFree(theme->a_clear);
1685         RrAppearanceFree(theme->a_clear_tex);
1686         RrAppearanceFree(theme->osd_bg);
1687         RrAppearanceFree(theme->osd_hilite_bg);
1688         RrAppearanceFree(theme->osd_hilite_label);
1689         RrAppearanceFree(theme->osd_unhilite_bg);
1690         RrAppearanceFree(theme->osd_unhilite_label);
1691         RrAppearanceFree(theme->osd_pressed_button);
1692         RrAppearanceFree(theme->osd_unpressed_button);
1693         RrAppearanceFree(theme->osd_focused_button);
1694
1695         g_slice_free(RrTheme, theme);
1696     }
1697 }
1698
1699 static XrmDatabase loaddb(const gchar *name, gchar **path)
1700 {
1701     GSList *it;
1702     XrmDatabase db = NULL;
1703     gchar *s;
1704
1705     if (name[0] == '/') {
1706         s = g_build_filename(name, "openbox-3", "themerc", NULL);
1707         if ((db = XrmGetFileDatabase(s)))
1708             *path = g_path_get_dirname(s);
1709         g_free(s);
1710     } else {
1711         ObtPaths *p;
1712
1713         p = obt_paths_new();
1714
1715         /* XXX backwards compatibility, remove me sometime later */
1716         s = g_build_filename(g_get_home_dir(), ".themes", name,
1717                              "openbox-3", "themerc", NULL);
1718         if ((db = XrmGetFileDatabase(s)))
1719             *path = g_path_get_dirname(s);
1720         g_free(s);
1721
1722         for (it = obt_paths_data_dirs(p); !db && it; it = g_slist_next(it))
1723         {
1724             s = g_build_filename(it->data, "themes", name,
1725                                  "openbox-3", "themerc", NULL);
1726             if ((db = XrmGetFileDatabase(s)))
1727                 *path = g_path_get_dirname(s);
1728             g_free(s);
1729         }
1730
1731         obt_paths_unref(p);
1732     }
1733
1734     if (db == NULL) {
1735         s = g_build_filename(name, "themerc", NULL);
1736         if ((db = XrmGetFileDatabase(s)))
1737             *path = g_path_get_dirname(s);
1738         g_free(s);
1739     }
1740
1741     return db;
1742 }
1743
1744 static gchar *create_class_name(const gchar *rname)
1745 {
1746     gchar *rclass = g_strdup(rname);
1747     gchar *p = rclass;
1748
1749     while (TRUE) {
1750         *p = toupper(*p);
1751         p = strchr(p+1, '.');
1752         if (p == NULL) break;
1753         ++p;
1754         if (*p == '\0') break;
1755     }
1756     return rclass;
1757 }
1758
1759 static gboolean read_int(XrmDatabase db, const gchar *rname, gint *value)
1760 {
1761     gboolean ret = FALSE;
1762     gchar *rclass = create_class_name(rname);
1763     gchar *rettype, *end;
1764     XrmValue retvalue;
1765
1766     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1767         retvalue.addr != NULL) {
1768         *value = (gint)strtol(retvalue.addr, &end, 10);
1769         if (end != retvalue.addr)
1770             ret = TRUE;
1771     }
1772
1773     g_free(rclass);
1774     return ret;
1775 }
1776
1777 static gboolean read_string(XrmDatabase db, const gchar *rname, gchar **value)
1778 {
1779     gboolean ret = FALSE;
1780     gchar *rclass = create_class_name(rname);
1781     gchar *rettype;
1782     XrmValue retvalue;
1783
1784     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1785         retvalue.addr != NULL) {
1786         g_strstrip(retvalue.addr);
1787         *value = retvalue.addr;
1788         ret = TRUE;
1789     }
1790
1791     g_free(rclass);
1792     return ret;
1793 }
1794
1795 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
1796                            const gchar *rname, RrColor **value)
1797 {
1798     gboolean ret = FALSE;
1799     gchar *rclass = create_class_name(rname);
1800     gchar *rettype;
1801     XrmValue retvalue;
1802
1803     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1804         retvalue.addr != NULL) {
1805         RrColor *c;
1806
1807         /* retvalue.addr is inside the xrdb database so we can't destroy it
1808            but we can edit it in place, as g_strstrip does. */
1809         g_strstrip(retvalue.addr);
1810         c = RrColorParse(inst, retvalue.addr);
1811         if (c != NULL) {
1812             *value = c;
1813             ret = TRUE;
1814         }
1815     }
1816
1817     g_free(rclass);
1818     return ret;
1819 }
1820
1821 static gboolean read_mask(const RrInstance *inst, const gchar *path,
1822                           RrTheme *theme, const gchar *maskname,
1823                           RrPixmapMask **value)
1824 {
1825     gboolean ret = FALSE;
1826     gchar *s;
1827     gint hx, hy; /* ignored */
1828     guint w, h;
1829     guchar *b;
1830
1831     s = g_build_filename(path, maskname, NULL);
1832     if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) {
1833         ret = TRUE;
1834         *value = RrPixmapMaskNew(inst, w, h, (gchar*)b);
1835         XFree(b);
1836     }
1837     g_free(s);
1838
1839     return ret;
1840 }
1841
1842 static void parse_appearance(gchar *tex, RrSurfaceColorType *grad,
1843                              RrReliefType *relief, RrBevelType *bevel,
1844                              gboolean *interlaced, gboolean *border,
1845                              gboolean allow_trans)
1846 {
1847     gchar *t;
1848
1849     /* convert to all lowercase */
1850     for (t = tex; *t != '\0'; ++t)
1851         *t = g_ascii_tolower(*t);
1852
1853     if (allow_trans && strstr(tex, "parentrelative") != NULL) {
1854         *grad = RR_SURFACE_PARENTREL;
1855     } else {
1856         if (strstr(tex, "gradient") != NULL) {
1857             if (strstr(tex, "crossdiagonal") != NULL)
1858                 *grad = RR_SURFACE_CROSS_DIAGONAL;
1859             else if (strstr(tex, "pyramid") != NULL)
1860                 *grad = RR_SURFACE_PYRAMID;
1861             else if (strstr(tex, "mirrorhorizontal") != NULL)
1862                 *grad = RR_SURFACE_MIRROR_HORIZONTAL;
1863             else if (strstr(tex, "horizontal") != NULL)
1864                 *grad = RR_SURFACE_HORIZONTAL;
1865             else if (strstr(tex, "splitvertical") != NULL)
1866                 *grad = RR_SURFACE_SPLIT_VERTICAL;
1867             else if (strstr(tex, "vertical") != NULL)
1868                 *grad = RR_SURFACE_VERTICAL;
1869             else
1870                 *grad = RR_SURFACE_DIAGONAL;
1871         } else {
1872             *grad = RR_SURFACE_SOLID;
1873         }
1874     }
1875
1876     if (strstr(tex, "sunken") != NULL)
1877         *relief = RR_RELIEF_SUNKEN;
1878     else if (strstr(tex, "flat") != NULL)
1879         *relief = RR_RELIEF_FLAT;
1880     else if (strstr(tex, "raised") != NULL)
1881         *relief = RR_RELIEF_RAISED;
1882     else
1883         *relief = (*grad == RR_SURFACE_PARENTREL) ?
1884                   RR_RELIEF_FLAT : RR_RELIEF_RAISED;
1885
1886     *border = FALSE;
1887     if (*relief == RR_RELIEF_FLAT) {
1888         if (strstr(tex, "border") != NULL)
1889             *border = TRUE;
1890     } else {
1891         if (strstr(tex, "bevel2") != NULL)
1892             *bevel = RR_BEVEL_2;
1893         else
1894             *bevel = RR_BEVEL_1;
1895     }
1896
1897     if (strstr(tex, "interlaced") != NULL)
1898         *interlaced = TRUE;
1899     else
1900         *interlaced = FALSE;
1901 }
1902
1903 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
1904                                 const gchar *rname, RrAppearance *value,
1905                                 gboolean allow_trans)
1906 {
1907     gboolean ret = FALSE;
1908     gchar *rclass = create_class_name(rname);
1909     gchar *cname, *ctoname, *bcname, *icname, *hname, *sname;
1910     gchar *csplitname, *ctosplitname;
1911     gchar *rettype;
1912     XrmValue retvalue;
1913     gint i;
1914
1915     cname = g_strconcat(rname, ".color", NULL);
1916     ctoname = g_strconcat(rname, ".colorTo", NULL);
1917     bcname = g_strconcat(rname, ".border.color", NULL);
1918     icname = g_strconcat(rname, ".interlace.color", NULL);
1919     hname = g_strconcat(rname, ".highlight", NULL);
1920     sname = g_strconcat(rname, ".shadow", NULL);
1921     csplitname = g_strconcat(rname, ".color.splitTo", NULL);
1922     ctosplitname = g_strconcat(rname, ".colorTo.splitTo", NULL);
1923
1924     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1925         retvalue.addr != NULL) {
1926         parse_appearance(retvalue.addr,
1927                          &value->surface.grad,
1928                          &value->surface.relief,
1929                          &value->surface.bevel,
1930                          &value->surface.interlaced,
1931                          &value->surface.border,
1932                          allow_trans);
1933         if (!read_color(db, inst, cname, &value->surface.primary))
1934             value->surface.primary = RrColorNew(inst, 0, 0, 0);
1935         if (!read_color(db, inst, ctoname, &value->surface.secondary))
1936             value->surface.secondary = RrColorNew(inst, 0, 0, 0);
1937         if (value->surface.border)
1938             if (!read_color(db, inst, bcname,
1939                             &value->surface.border_color))
1940                 value->surface.border_color = RrColorNew(inst, 0, 0, 0);
1941         if (value->surface.interlaced)
1942             if (!read_color(db, inst, icname,
1943                             &value->surface.interlace_color))
1944                 value->surface.interlace_color = RrColorNew(inst, 0, 0, 0);
1945         if (read_int(db, hname, &i) && i >= 0)
1946             value->surface.bevel_light_adjust = i;
1947         if (read_int(db, sname, &i) && i >= 0 && i <= 256)
1948             value->surface.bevel_dark_adjust = i;
1949
1950         if (value->surface.grad == RR_SURFACE_SPLIT_VERTICAL) {
1951             gint r, g, b;
1952
1953             if (!read_color(db, inst, csplitname,
1954                             &value->surface.split_primary))
1955             {
1956                 r = value->surface.primary->r;
1957                 r += r >> 2;
1958                 g = value->surface.primary->g;
1959                 g += g >> 2;
1960                 b = value->surface.primary->b;
1961                 b += b >> 2;
1962                 if (r > 0xFF) r = 0xFF;
1963                 if (g > 0xFF) g = 0xFF;
1964                 if (b > 0xFF) b = 0xFF;
1965                 value->surface.split_primary = RrColorNew(inst, r, g, b);
1966             }
1967
1968             if (!read_color(db, inst, ctosplitname,
1969                             &value->surface.split_secondary))
1970             {
1971                 r = value->surface.secondary->r;
1972                 r += r >> 4;
1973                 g = value->surface.secondary->g;
1974                 g += g >> 4;
1975                 b = value->surface.secondary->b;
1976                 b += b >> 4;
1977                 if (r > 0xFF) r = 0xFF;
1978                 if (g > 0xFF) g = 0xFF;
1979                 if (b > 0xFF) b = 0xFF;
1980                 value->surface.split_secondary = RrColorNew(inst, r, g, b);
1981             }
1982         }
1983
1984         ret = TRUE;
1985     }
1986
1987     g_free(ctosplitname);
1988     g_free(csplitname);
1989     g_free(sname);
1990     g_free(hname);
1991     g_free(icname);
1992     g_free(bcname);
1993     g_free(ctoname);
1994     g_free(cname);
1995     g_free(rclass);
1996     return ret;
1997 }
1998
1999 static int parse_inline_number(const char *p)
2000 {
2001     int neg = 1;
2002     int res = 0;
2003     if (*p == '-') {
2004         neg = -1;
2005         ++p;
2006     }
2007     for (; isdigit(*p); ++p)
2008         res = res * 10 + *p - '0';
2009     res *= neg;
2010     return res;
2011 }
2012
2013 static void set_default_appearance(RrAppearance *a)
2014 {
2015     a->surface.grad = RR_SURFACE_SOLID;
2016     a->surface.relief = RR_RELIEF_FLAT;
2017     a->surface.bevel = RR_BEVEL_1;
2018     a->surface.interlaced = FALSE;
2019     a->surface.border = FALSE;
2020     a->surface.primary = RrColorNew(a->inst, 0, 0, 0);
2021     a->surface.secondary = RrColorNew(a->inst, 0, 0, 0);
2022 }
2023
2024 /* Reads the output from gimp's C-Source file format into valid RGBA data for
2025    an RrTextureRGBA. */
2026 static RrPixel32* read_c_image(gint width, gint height, const guint8 *data)
2027 {
2028     RrPixel32 *im, *p;
2029     gint i;
2030
2031     p = im = g_memdup(data, width * height * sizeof(RrPixel32));
2032
2033     for (i = 0; i < width * height; ++i) {
2034         guchar a = ((*p >> 24) & 0xff);
2035         guchar b = ((*p >> 16) & 0xff);
2036         guchar g = ((*p >>  8) & 0xff);
2037         guchar r = ((*p >>  0) & 0xff);
2038
2039         *p = ((r << RrDefaultRedOffset) +
2040               (g << RrDefaultGreenOffset) +
2041               (b << RrDefaultBlueOffset) +
2042               (a << RrDefaultAlphaOffset));
2043         p++;
2044     }
2045
2046     return im;
2047 }
2048
2049 static void read_button_colors(XrmDatabase db, const RrInstance *inst, 
2050                                const RrTheme *theme, RrButton *btn, 
2051                                const gchar *btnname)
2052 {
2053     gchar *name;
2054
2055     /* active unpressed */
2056     name = g_strdup_printf("window.active.button.%s.unpressed.image.color",
2057                            btnname);
2058     READ_COLOR(name, btn->focused_unpressed_color,
2059                RrColorCopy(theme->titlebut_focused_unpressed_color));
2060     g_free(name);
2061
2062     /* inactive unpressed */
2063     name = g_strdup_printf("window.inactive.button.%s.unpressed.image.color",
2064                            btnname);
2065     READ_COLOR(name, btn->unfocused_unpressed_color,
2066                RrColorCopy(theme->titlebut_unfocused_unpressed_color));
2067     g_free(name);
2068
2069     /* active pressed */
2070     name = g_strdup_printf("window.active.button.%s.pressed.image.color",
2071                            btnname);
2072     READ_COLOR(name, btn->focused_pressed_color,
2073                RrColorCopy(theme->titlebut_focused_pressed_color));
2074     g_free(name);
2075
2076     /* inactive pressed */
2077     name = g_strdup_printf("window.inactive.button.%s.pressed.image.color",
2078                           btnname);
2079     READ_COLOR(name, btn->unfocused_pressed_color,
2080                RrColorCopy(theme->titlebut_unfocused_pressed_color));
2081     g_free(name);
2082
2083     /* active disabled */
2084     name = g_strdup_printf("window.active.button.%s.disabled.image.color",
2085                            btnname);
2086     READ_COLOR(name, btn->disabled_focused_color,
2087                RrColorCopy(theme->titlebut_disabled_focused_color));
2088     g_free(name);
2089
2090     /* inactive disabled */
2091     name = g_strdup_printf("window.inactive.button.%s.disabled.image.color",
2092                            btnname);
2093     READ_COLOR(name, btn->disabled_unfocused_color,
2094                RrColorCopy(theme->titlebut_disabled_unfocused_color));
2095     g_free(name);
2096
2097     /* active hover */
2098     name = g_strdup_printf("window.active.button.%s.hover.image.color",
2099                            btnname);
2100     READ_COLOR(name, btn->hover_focused_color, 
2101                RrColorCopy(theme->titlebut_hover_focused_color));
2102     g_free(name);
2103
2104     /* inactive hover */
2105     name = g_strdup_printf("window.inactive.button.%s.hover.image.color",
2106                            btnname);
2107     READ_COLOR(name, btn->hover_unfocused_color,
2108                RrColorCopy(theme->titlebut_hover_unfocused_color));
2109     g_free(name);
2110
2111     /* active toggled unpressed */
2112     name = g_strdup_printf("window.active.button.%s.toggled."
2113                           "unpressed.image.color", btnname);
2114     READ_COLOR(name, btn->toggled_focused_unpressed_color,
2115                RrColorCopy(theme->titlebut_toggled_focused_unpressed_color));
2116     g_free(name);
2117
2118     /* inactive toggled unpressed */
2119     name = g_strdup_printf("window.inactive.button.%s.toggled."
2120                            "unpressed.image.color", btnname);
2121     READ_COLOR(name, btn->toggled_unfocused_unpressed_color,
2122                RrColorCopy(theme->titlebut_toggled_unfocused_unpressed_color));
2123     g_free(name);
2124
2125     /* active toggled hover */
2126     name = g_strdup_printf("window.active.button.%s.toggled.hover.image.color",
2127                            btnname);
2128     READ_COLOR(name, btn->toggled_hover_focused_color,
2129                RrColorCopy(theme->titlebut_toggled_hover_focused_color));
2130
2131     g_free(name);
2132
2133     /* inactive toggled hover */
2134     name = g_strdup_printf("window.inactive.button.%s.toggled.hover."
2135                            "image.color", btnname);
2136     READ_COLOR(name, btn->toggled_hover_unfocused_color,
2137                RrColorCopy(theme->titlebut_toggled_hover_unfocused_color));
2138     g_free(name);
2139
2140     /* active toggled pressed */
2141     name = g_strdup_printf("window.active.button.%s.toggled.pressed."
2142                            "image.color", btnname);
2143     READ_COLOR(name, btn->toggled_focused_pressed_color, 
2144                RrColorCopy(theme->titlebut_toggled_focused_pressed_color));
2145     g_free(name);
2146    
2147     /* inactive toggled pressed */
2148     name = g_strdup_printf("window.inactive.button.%s.toggled.pressed."
2149                            "image.color", btnname);
2150     READ_COLOR(name, btn->toggled_unfocused_pressed_color,
2151                RrColorCopy(theme->titlebut_toggled_unfocused_pressed_color));
2152     g_free(name);
2153 }
2154
2155