don't exit in the render lib, rather return a NULL font/theme on failure
[dana/openbox.git] / render / theme.c
1 #include "render.h"
2 #include "color.h"
3 #include "font.h"
4 #include "mask.h"
5 #include "theme.h"
6
7 #include <X11/Xlib.h>
8 #include <X11/Xresource.h>
9
10 static XrmDatabase loaddb(char *theme);
11 static gboolean read_int(XrmDatabase db, char *rname, int *value);
12 static gboolean read_string(XrmDatabase db, char *rname, char **value);
13 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
14                            gchar *rname, RrColor **value);
15 static gboolean read_mask(XrmDatabase db, const RrInstance *inst,
16                           gchar *rname, gchar *theme,
17                           RrPixmapMask **value);
18 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
19                                 gchar *rname, RrAppearance *value);
20 static void set_default_appearance(RrAppearance *a);
21
22 RrTheme* RrThemeNew(const RrInstance *inst, gchar *name)
23 {
24     XrmDatabase db = NULL;
25     RrJustify winjust, mtitlejust, mjust;
26     gchar *str;
27     gchar *font_str;
28     RrTheme *theme;
29
30     theme = g_new0(RrTheme, 1);
31
32     theme->inst = inst;
33
34     theme->b_color = theme->cb_unfocused_color = theme->cb_focused_color = 
35         theme->title_unfocused_color = theme->title_focused_color = 
36         theme->titlebut_unfocused_color = theme->titlebut_focused_color = 
37         theme->menu_color = theme->menu_title_color =
38         theme->menu_disabled_color = theme->menu_hilite_color = NULL;
39     theme->winfont = theme->mtitlefont = theme->mfont = NULL;
40     theme->title_layout = NULL;
41     theme->max_set_mask = theme->max_unset_mask = NULL;
42     theme->desk_set_mask = theme->desk_unset_mask = NULL;
43     theme->shade_set_mask = theme->shade_unset_mask = NULL;
44     theme->iconify_mask = theme->close_mask = NULL;
45
46     theme->a_focused_unpressed_max = RrAppearanceNew(inst, 1);
47     theme->a_focused_pressed_max = RrAppearanceNew(inst, 1);
48     theme->a_focused_pressed_set_max = RrAppearanceNew(inst, 1);
49     theme->a_unfocused_unpressed_max = RrAppearanceNew(inst, 1);
50     theme->a_unfocused_pressed_max = RrAppearanceNew(inst, 1);
51     theme->a_unfocused_pressed_set_max = RrAppearanceNew(inst, 1);
52     theme->a_focused_unpressed_close = NULL;
53     theme->a_focused_pressed_close = NULL;
54     theme->a_unfocused_unpressed_close = NULL;
55     theme->a_unfocused_pressed_close = NULL;
56     theme->a_focused_unpressed_desk = NULL;
57     theme->a_focused_pressed_desk = NULL;
58     theme->a_focused_pressed_set_desk = NULL;
59     theme->a_unfocused_unpressed_desk = NULL;
60     theme->a_unfocused_pressed_desk = NULL;
61     theme->a_unfocused_pressed_set_desk = NULL;
62     theme->a_focused_unpressed_shade = NULL;
63     theme->a_focused_pressed_shade = NULL;
64     theme->a_focused_pressed_set_shade = NULL;
65     theme->a_unfocused_unpressed_shade = NULL;
66     theme->a_unfocused_pressed_shade = NULL;
67     theme->a_unfocused_pressed_set_shade = NULL;
68     theme->a_focused_unpressed_iconify = NULL;
69     theme->a_focused_pressed_iconify = NULL;
70     theme->a_unfocused_unpressed_iconify = NULL;
71     theme->a_unfocused_pressed_iconify = NULL;
72     theme->a_focused_grip = RrAppearanceNew(inst, 0);
73     theme->a_unfocused_grip = RrAppearanceNew(inst, 0);
74     theme->a_focused_title = RrAppearanceNew(inst, 0);
75     theme->a_unfocused_title = RrAppearanceNew(inst, 0);
76     theme->a_focused_label = RrAppearanceNew(inst, 1);
77     theme->a_unfocused_label = RrAppearanceNew(inst, 1);
78     theme->a_icon = RrAppearanceNew(inst, 1);
79     theme->a_focused_handle = RrAppearanceNew(inst, 0);
80     theme->a_unfocused_handle = RrAppearanceNew(inst, 0);
81     theme->a_menu = RrAppearanceNew(inst, 0);
82     theme->a_menu_title = RrAppearanceNew(inst, 1);
83     theme->a_menu_item = RrAppearanceNew(inst, 1);
84     theme->a_menu_disabled = RrAppearanceNew(inst, 1);
85     theme->a_menu_hilite = RrAppearanceNew(inst, 1);
86
87     theme->app_hilite_bg = RrAppearanceNew(inst, 0);
88     theme->app_unhilite_bg = RrAppearanceNew(inst, 0);
89     theme->app_hilite_label = RrAppearanceNew(inst, 1);
90     theme->app_unhilite_label = RrAppearanceNew(inst, 1);
91     theme->app_icon = RrAppearanceNew(inst, 1);
92
93     if (name) {
94         db = loaddb(name);
95         if (db == NULL) {
96             g_warning("Failed to load the theme '%s'", name);
97             g_message("Falling back to the default: '%s'", DEFAULT_THEME);
98         } else
99             theme->name = g_strdup(name);
100     }
101     if (db == NULL) {
102         db = loaddb(DEFAULT_THEME);
103         if (db == NULL) {
104             g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
105             return NULL;
106         } else
107             theme->name = g_strdup(DEFAULT_THEME);
108     }
109
110     /* load the font stuff */
111     font_str = "arial:bold:pixelsize=10";
112
113     theme->winfont_shadow = FALSE;
114     if (read_string(db, "window.xft.flags", &str)) {
115         if (g_strrstr(str, "shadow"))
116             theme->winfont_shadow = TRUE;
117         g_free(str);
118     }
119  
120     if (!read_int(db, "window.xft.shadow.offset",
121                   &theme->winfont_shadow_offset))
122         theme->winfont_shadow_offset = 1;
123     if (!read_int(db, "window.xft.shadow.tint",
124                   &theme->winfont_shadow_tint) ||
125         theme->winfont_shadow_tint < 100 || theme->winfont_shadow_tint > 100)
126         theme->winfont_shadow_tint = 25;
127
128     if (!(theme->winfont = RrFontOpen(inst, font_str))) {
129         RrThemeFree(theme);
130         return NULL;
131     }
132     theme->winfont_height = RrFontHeight(theme->winfont, theme->winfont_shadow,
133                                          theme->winfont_shadow_offset);
134
135     winjust = RR_JUSTIFY_LEFT;
136     if (read_string(db, "window.justify", &str)) {
137         if (!g_ascii_strcasecmp(str, "right"))
138             winjust = RR_JUSTIFY_RIGHT;
139         else if (!g_ascii_strcasecmp(str, "center"))
140             winjust = RR_JUSTIFY_CENTER;
141         g_free(str);
142     }
143
144     font_str = "arial-10:bold";
145
146     theme->mtitlefont_shadow = FALSE;
147     if (read_string(db, "menu.title.xft.flags", &str)) {
148         if (g_strrstr(str, "shadow"))
149             theme->mtitlefont_shadow = TRUE;
150         g_free(str);
151     }
152  
153     if (!read_int(db, "menu.title.xft.shadow.offset",
154                   &theme->mtitlefont_shadow_offset))
155         theme->mtitlefont_shadow_offset = 1;
156     if (!read_int(db, "menu.title.xft.shadow.tint",
157                   &theme->mtitlefont_shadow_tint) ||
158         theme->mtitlefont_shadow_tint < 100 ||
159         theme->mtitlefont_shadow_tint > 100)
160         theme->mtitlefont_shadow_tint = 25;
161
162     if (!(theme->mtitlefont = RrFontOpen(inst, font_str))) {
163         RrThemeFree(theme);
164         return NULL;
165     }
166     theme->mtitlefont_height = RrFontHeight(theme->mtitlefont,
167                                             theme->mtitlefont_shadow,
168                                             theme->mtitlefont_shadow_offset);
169
170     mtitlejust = RR_JUSTIFY_LEFT;
171     if (read_string(db, "menu.title.justify", &str)) {
172         if (!g_ascii_strcasecmp(str, "right"))
173             mtitlejust = RR_JUSTIFY_RIGHT;
174         else if (!g_ascii_strcasecmp(str, "center"))
175             mtitlejust = RR_JUSTIFY_CENTER;
176         g_free(str);
177     }
178
179     font_str = "arial-8";
180
181     theme->mfont_shadow = FALSE;
182     if (read_string(db, "menu.frame.xft.flags", &str)) {
183         if (g_strrstr(str, "shadow"))
184             theme->mfont_shadow = TRUE;
185         g_free(str);
186     }
187  
188     if (!read_int(db, "menu.frame.xft.shadow.offset",
189                   &theme->mfont_shadow_offset))
190         theme->mfont_shadow_offset = 1;
191     if (!read_int(db, "menu.frame.xft.shadow.tint",
192                   &theme->mfont_shadow_tint) ||
193         theme->mfont_shadow_tint < 100 ||
194         theme->mfont_shadow_tint > 100)
195         theme->mfont_shadow_tint = 25;
196
197     if (!(theme->mfont = RrFontOpen(inst, font_str))) {
198         RrThemeFree(theme);
199         return NULL;
200     }
201     theme->mfont_height = RrFontHeight(theme->mfont, theme->mfont_shadow,
202                                        theme->mfont_shadow_offset);
203
204     mjust = RR_JUSTIFY_LEFT;
205     if (read_string(db, "menu.frame.justify", &str)) {
206         if (!g_ascii_strcasecmp(str, "right"))
207             mjust = RR_JUSTIFY_RIGHT;
208         else if (!g_ascii_strcasecmp(str, "center"))
209             mjust = RR_JUSTIFY_CENTER;
210         g_free(str);
211     }
212
213     /* load the title layout */
214     theme->title_layout = g_strdup("NLIMC");
215
216     if (!read_int(db, "handleWidth", &theme->handle_height) ||
217         theme->handle_height < 0 || theme->handle_height > 100)
218         theme->handle_height = 6;
219     if (!read_int(db, "bevelWidth", &theme->bevel) ||
220         theme->bevel <= 0 || theme->bevel > 100) theme->bevel = 3;
221     if (!read_int(db, "borderWidth", &theme->bwidth) ||
222         theme->bwidth < 0 || theme->bwidth > 100) theme->bwidth = 1;
223     if (!read_int(db, "frameWidth", &theme->cbwidth) ||
224         theme->cbwidth < 0 || theme->cbwidth > 100)
225         theme->cbwidth = theme->bevel;
226
227     /* load colors */
228     if (!read_color(db, inst,
229                     "borderColor", &theme->b_color))
230         theme->b_color = RrColorNew(inst, 0, 0, 0);
231     if (!read_color(db, inst,
232                     "window.frame.focusColor", &theme->cb_focused_color))
233         theme->cb_focused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
234     if (!read_color(db, inst,
235                     "window.frame.unfocusColor",&theme->cb_unfocused_color))
236         theme->cb_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
237     if (!read_color(db, inst,
238                     "window.label.focus.textColor",
239                     &theme->title_focused_color))
240         theme->title_focused_color = RrColorNew(inst, 0x0, 0x0, 0x0);
241     if (!read_color(db, inst,
242                     "window.label.unfocus.textColor",
243                     &theme->title_unfocused_color))
244         theme->title_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
245     if (!read_color(db, inst,
246                     "window.button.focus.picColor",
247                     &theme->titlebut_focused_color))
248         theme->titlebut_focused_color = RrColorNew(inst, 0, 0, 0);
249     if (!read_color(db, inst,
250                     "window.button.unfocus.picColor",
251                     &theme->titlebut_unfocused_color))
252         theme->titlebut_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
253     if (!read_color(db, inst,
254                     "menu.title.textColor", &theme->menu_title_color))
255         theme->menu_title_color = RrColorNew(inst, 0, 0, 0);
256     if (!read_color(db, inst,
257                     "menu.frame.textColor", &theme->menu_color))
258         theme->menu_color = RrColorNew(inst, 0xff, 0xff, 0xff);
259     if (!read_color(db, inst,
260                     "menu.frame.disableColor", &theme->menu_disabled_color))
261         theme->menu_disabled_color = RrColorNew(inst, 0, 0, 0);
262     if (!read_color(db, inst,
263                     "menu.hilite.textColor", &theme->menu_hilite_color))
264         theme->menu_hilite_color = RrColorNew(inst, 0, 0, 0);
265
266     if (read_mask(db, inst,
267                   "window.button.max.mask", name, &theme->max_unset_mask)){
268         if (!read_mask(db, inst,
269                        "window.button.max.toggled.mask", name,
270                        &theme->max_set_mask)) {
271             theme->max_set_mask = RrPixmapMaskCopy(theme->max_unset_mask);
272         }
273     } else {
274         {
275             char data[] = { 0x7f, 0x7f, 0x7f, 0x41, 0x41, 0x41, 0x7f };
276             theme->max_unset_mask = RrPixmapMaskNew(inst, 7, 7, data);
277         }
278         {
279             char data[] = { 0x7c, 0x44, 0x47, 0x47, 0x7f, 0x1f, 0x1f };
280             theme->max_set_mask = RrPixmapMaskNew(inst, 7, 7, data);
281         }
282     }
283
284     if (!read_mask(db, inst,
285                    "window.button.icon.mask", name,
286                    &theme->iconify_mask)) {
287         char data[] = { 0x00, 0x00, 0x00, 0x00, 0x7f, 0x7f, 0x7f };
288         theme->iconify_mask = RrPixmapMaskNew(inst, 7, 7, data);
289     }
290
291     if (read_mask(db, inst,
292                   "window.button.stick.mask", name,
293                    &theme->desk_unset_mask)) {
294         if (!read_mask(db, inst, "window.button.stick.toggled.mask", name,
295                        &theme->desk_set_mask)) {
296             theme->desk_set_mask =
297                 RrPixmapMaskCopy(theme->desk_unset_mask);
298         }
299     } else {
300         {
301             char data[] = { 0x63, 0x63, 0x00, 0x00, 0x00, 0x63, 0x63 };
302             theme->desk_unset_mask = RrPixmapMaskNew(inst, 7, 7, data);
303         }
304         {
305             char data[] = { 0x00, 0x36, 0x36, 0x08, 0x36, 0x36, 0x00 };
306             theme->desk_set_mask = RrPixmapMaskNew(inst, 7, 7, data);
307         }
308     }
309
310     if (read_mask(db, inst, "window.button.shade.mask", name,
311                    &theme->shade_unset_mask)) {
312         if (!read_mask(db, inst, "window.button.shade.toggled.mask", name,
313                        &theme->shade_set_mask)) {
314             theme->shade_set_mask =
315                 RrPixmapMaskCopy(theme->shade_unset_mask);
316         }
317     } else {
318         {
319             char data[] = { 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00 };
320             theme->shade_unset_mask = RrPixmapMaskNew(inst, 7, 7, data);
321         }
322         {
323             char data[] = { 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x7f };
324             theme->shade_set_mask = RrPixmapMaskNew(inst, 7, 7, data);
325         }
326     }
327
328     if (!read_mask(db, inst, "window.button.close.mask", name,
329                    &theme->close_mask)) {
330         char data[] = { 0x63, 0x77, 0x3e, 0x1c, 0x3e, 0x77, 0x63 };
331         theme->close_mask = RrPixmapMaskNew(inst, 7, 7, data);
332     }        
333
334     /* read the decoration textures */
335     if (!read_appearance(db, inst,
336                          "window.title.focus", theme->a_focused_title))
337         set_default_appearance(theme->a_focused_title);
338     if (!read_appearance(db, inst,
339                          "window.title.unfocus", theme->a_unfocused_title))
340         set_default_appearance(theme->a_unfocused_title);
341     if (!read_appearance(db, inst,
342                          "window.label.focus", theme->a_focused_label))
343         set_default_appearance(theme->a_focused_label);
344     if (!read_appearance(db, inst,
345                          "window.label.unfocus", theme->a_unfocused_label))
346         set_default_appearance(theme->a_unfocused_label);
347     if (!read_appearance(db, inst,
348                          "window.handle.focus", theme->a_focused_handle))
349         set_default_appearance(theme->a_focused_handle);
350     if (!read_appearance(db, inst,
351                          "window.handle.unfocus",theme->a_unfocused_handle))
352         set_default_appearance(theme->a_unfocused_handle);
353     if (!read_appearance(db, inst,
354                          "window.grip.focus", theme->a_focused_grip))
355         set_default_appearance(theme->a_focused_grip);
356     if (!read_appearance(db, inst,
357                          "window.grip.unfocus", theme->a_unfocused_grip))
358         set_default_appearance(theme->a_unfocused_grip);
359     if (!read_appearance(db, inst,
360                          "menu.frame", theme->a_menu))
361         set_default_appearance(theme->a_menu);
362     if (!read_appearance(db, inst,
363                          "menu.title", theme->a_menu_title))
364         set_default_appearance(theme->a_menu_title);
365     if (!read_appearance(db, inst,
366                          "menu.hilite", theme->a_menu_hilite))
367         set_default_appearance(theme->a_menu_hilite);
368
369     /* read the appearances for rendering non-decorations */
370     if (!read_appearance(db, inst,
371                          "window.title.focus", theme->app_hilite_bg))
372         set_default_appearance(theme->app_hilite_bg);
373     if (!read_appearance(db, inst,
374                          "window.label.focus", theme->app_hilite_label))
375         set_default_appearance(theme->app_hilite_label);
376     if (!read_appearance(db, inst,
377                          "window.title.unfocus", theme->app_unhilite_bg))
378         set_default_appearance(theme->app_unhilite_bg);
379     if (!read_appearance(db, inst,
380                          "window.label.unfocus", theme->app_unhilite_label))
381         set_default_appearance(theme->app_unhilite_label);
382
383     /* read buttons textures */
384     if (!read_appearance(db, inst,
385                          "window.button.pressed.focus",
386                          theme->a_focused_pressed_max))
387         if (!read_appearance(db, inst,
388                              "window.button.pressed",
389                              theme->a_focused_pressed_max))
390             set_default_appearance(theme->a_focused_pressed_max);
391     if (!read_appearance(db, inst,
392                          "window.button.pressed.unfocus",
393                          theme->a_unfocused_pressed_max))
394         if (!read_appearance(db, inst,
395                              "window.button.pressed",
396                              theme->a_unfocused_pressed_max))
397             set_default_appearance(theme->a_unfocused_pressed_max);
398     if (!read_appearance(db, inst,
399                          "window.button.focus",
400                          theme->a_focused_unpressed_max))
401         set_default_appearance(theme->a_focused_unpressed_max);
402     if (!read_appearance(db, inst,
403                          "window.button.unfocus",
404                          theme->a_unfocused_unpressed_max))
405         set_default_appearance(theme->a_unfocused_unpressed_max);
406
407     theme->a_unfocused_unpressed_close =
408         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
409     theme->a_unfocused_pressed_close =
410         RrAppearanceCopy(theme->a_unfocused_pressed_max);
411     theme->a_focused_unpressed_close =
412         RrAppearanceCopy(theme->a_focused_unpressed_max);
413     theme->a_focused_pressed_close =
414         RrAppearanceCopy(theme->a_focused_pressed_max);
415     theme->a_unfocused_unpressed_desk =
416         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
417     theme->a_unfocused_pressed_desk =
418         RrAppearanceCopy(theme->a_unfocused_pressed_max);
419     theme->a_unfocused_pressed_set_desk =
420         RrAppearanceCopy(theme->a_unfocused_pressed_max);
421     theme->a_focused_unpressed_desk =
422         RrAppearanceCopy(theme->a_focused_unpressed_max);
423     theme->a_focused_pressed_desk =
424         RrAppearanceCopy(theme->a_focused_pressed_max);
425     theme->a_focused_pressed_set_desk =
426         RrAppearanceCopy(theme->a_focused_pressed_max);
427     theme->a_unfocused_unpressed_shade =
428         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
429     theme->a_unfocused_pressed_shade =
430         RrAppearanceCopy(theme->a_unfocused_pressed_max);
431     theme->a_unfocused_pressed_set_shade =
432         RrAppearanceCopy(theme->a_unfocused_pressed_max);
433     theme->a_focused_unpressed_shade =
434         RrAppearanceCopy(theme->a_focused_unpressed_max);
435     theme->a_focused_pressed_shade =
436         RrAppearanceCopy(theme->a_focused_pressed_max);
437     theme->a_focused_pressed_set_shade =
438         RrAppearanceCopy(theme->a_focused_pressed_max);
439     theme->a_unfocused_unpressed_iconify =
440         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
441     theme->a_unfocused_pressed_iconify =
442         RrAppearanceCopy(theme->a_unfocused_pressed_max);
443     theme->a_focused_unpressed_iconify =
444         RrAppearanceCopy(theme->a_focused_unpressed_max);
445     theme->a_focused_pressed_iconify =
446         RrAppearanceCopy(theme->a_focused_pressed_max);
447     theme->a_unfocused_pressed_set_max =
448         RrAppearanceCopy(theme->a_unfocused_pressed_max);
449     theme->a_focused_pressed_set_max =
450         RrAppearanceCopy(theme->a_focused_pressed_max);
451
452     theme->a_icon->surface.grad = RR_SURFACE_PARENTREL;
453
454     /* set up the textures */
455     theme->a_focused_label->texture[0].type = 
456         theme->app_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
457     theme->a_focused_label->texture[0].data.text.justify = winjust;
458     theme->app_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
459     theme->a_focused_label->texture[0].data.text.font =
460         theme->app_hilite_label->texture[0].data.text.font = theme->winfont;
461     theme->a_focused_label->texture[0].data.text.shadow =
462         theme->app_hilite_label->texture[0].data.text.shadow =
463         theme->winfont_shadow;
464     theme->a_focused_label->texture[0].data.text.offset =
465         theme->app_hilite_label->texture[0].data.text.offset =
466         theme->winfont_shadow_offset;
467     theme->a_focused_label->texture[0].data.text.tint =
468         theme->app_hilite_label->texture[0].data.text.tint =
469         theme->winfont_shadow_tint;
470     theme->a_focused_label->texture[0].data.text.color =
471         theme->app_hilite_label->texture[0].data.text.color =
472         theme->title_focused_color;
473
474     theme->a_unfocused_label->texture[0].type =
475         theme->app_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
476     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
477     theme->app_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
478     theme->a_unfocused_label->texture[0].data.text.font =
479         theme->app_unhilite_label->texture[0].data.text.font = theme->winfont;
480     theme->a_unfocused_label->texture[0].data.text.shadow =
481         theme->app_unhilite_label->texture[0].data.text.shadow =
482         theme->winfont_shadow;
483     theme->a_unfocused_label->texture[0].data.text.offset =
484         theme->app_unhilite_label->texture[0].data.text.offset =
485         theme->winfont_shadow_offset;
486     theme->a_unfocused_label->texture[0].data.text.tint =
487         theme->app_unhilite_label->texture[0].data.text.tint =
488         theme->winfont_shadow_tint;
489     theme->a_unfocused_label->texture[0].data.text.color =
490         theme->app_unhilite_label->texture[0].data.text.color =
491         theme->title_unfocused_color;
492
493     theme->a_menu_title->texture[0].type = RR_TEXTURE_TEXT;
494     theme->a_menu_title->texture[0].data.text.justify = mtitlejust;
495     theme->a_menu_title->texture[0].data.text.font = theme->mtitlefont;
496     theme->a_menu_title->texture[0].data.text.shadow = theme->mtitlefont_shadow;
497     theme->a_menu_title->texture[0].data.text.offset =
498         theme->mtitlefont_shadow_offset;
499     theme->a_menu_title->texture[0].data.text.tint =
500         theme->mtitlefont_shadow_tint;
501     theme->a_menu_title->texture[0].data.text.color = theme->menu_title_color;
502
503     theme->a_menu_item->surface.grad = 
504         theme->a_menu_disabled->surface.grad =
505         theme->app_icon->surface.grad = RR_SURFACE_PARENTREL;
506
507     theme->a_menu_item->texture[0].type =
508         theme->a_menu_disabled->texture[0].type = 
509         theme->a_menu_hilite->texture[0].type = RR_TEXTURE_TEXT;
510     theme->a_menu_item->texture[0].data.text.justify = 
511         theme->a_menu_disabled->texture[0].data.text.justify = 
512         theme->a_menu_hilite->texture[0].data.text.justify = mjust;
513     theme->a_menu_item->texture[0].data.text.font =
514         theme->a_menu_disabled->texture[0].data.text.font =
515         theme->a_menu_hilite->texture[0].data.text.font = theme->mfont;
516     theme->a_menu_item->texture[0].data.text.shadow = 
517         theme->a_menu_disabled->texture[0].data.text.shadow = 
518         theme->a_menu_hilite->texture[0].data.text.shadow =
519         theme->mfont_shadow;
520     theme->a_menu_item->texture[0].data.text.offset =
521         theme->a_menu_disabled->texture[0].data.text.offset = 
522         theme->a_menu_hilite->texture[0].data.text.offset = 
523         theme->mfont_shadow_offset;
524     theme->a_menu_item->texture[0].data.text.tint =
525         theme->a_menu_disabled->texture[0].data.text.tint =
526         theme->a_menu_hilite->texture[0].data.text.tint =
527         theme->mfont_shadow_tint;
528     theme->a_menu_item->texture[0].data.text.color = theme->menu_color;
529     theme->a_menu_disabled->texture[0].data.text.color =
530         theme->menu_disabled_color;
531     theme->a_menu_hilite->texture[0].data.text.color =
532         theme->menu_hilite_color;
533
534     theme->a_focused_unpressed_max->texture[0].type = 
535         theme->a_focused_pressed_max->texture[0].type = 
536         theme->a_focused_pressed_set_max->texture[0].type =  
537         theme->a_unfocused_unpressed_max->texture[0].type = 
538         theme->a_unfocused_pressed_max->texture[0].type = 
539         theme->a_unfocused_pressed_set_max->texture[0].type = 
540         theme->a_focused_unpressed_close->texture[0].type = 
541         theme->a_focused_pressed_close->texture[0].type = 
542         theme->a_unfocused_unpressed_close->texture[0].type = 
543         theme->a_unfocused_pressed_close->texture[0].type = 
544         theme->a_focused_unpressed_desk->texture[0].type = 
545         theme->a_focused_pressed_desk->texture[0].type = 
546         theme->a_focused_pressed_set_desk->texture[0].type = 
547         theme->a_unfocused_unpressed_desk->texture[0].type = 
548         theme->a_unfocused_pressed_desk->texture[0].type = 
549         theme->a_unfocused_pressed_set_desk->texture[0].type = 
550         theme->a_focused_unpressed_shade->texture[0].type = 
551         theme->a_focused_pressed_shade->texture[0].type = 
552         theme->a_focused_pressed_set_shade->texture[0].type = 
553         theme->a_unfocused_unpressed_shade->texture[0].type = 
554         theme->a_unfocused_pressed_shade->texture[0].type = 
555         theme->a_unfocused_pressed_set_shade->texture[0].type = 
556         theme->a_focused_unpressed_iconify->texture[0].type = 
557         theme->a_focused_pressed_iconify->texture[0].type = 
558         theme->a_unfocused_unpressed_iconify->texture[0].type = 
559         theme->a_unfocused_pressed_iconify->texture[0].type = RR_TEXTURE_MASK;
560     theme->a_focused_unpressed_max->texture[0].data.mask.mask = 
561         theme->a_unfocused_unpressed_max->texture[0].data.mask.mask = 
562         theme->a_focused_pressed_max->texture[0].data.mask.mask = 
563         theme->a_unfocused_pressed_max->texture[0].data.mask.mask =
564         theme->max_unset_mask;
565     theme->a_focused_pressed_set_max->texture[0].data.mask.mask = 
566         theme->a_unfocused_pressed_set_max->texture[0].data.mask.mask =
567         theme->max_set_mask;
568     theme->a_focused_pressed_close->texture[0].data.mask.mask = 
569         theme->a_unfocused_pressed_close->texture[0].data.mask.mask =
570         theme->a_focused_unpressed_close->texture[0].data.mask.mask = 
571         theme->a_unfocused_unpressed_close->texture[0].data.mask.mask =
572         theme->close_mask;
573     theme->a_focused_unpressed_desk->texture[0].data.mask.mask = 
574         theme->a_unfocused_unpressed_desk->texture[0].data.mask.mask = 
575         theme->a_focused_pressed_desk->texture[0].data.mask.mask = 
576         theme->a_unfocused_pressed_desk->texture[0].data.mask.mask =
577         theme->desk_unset_mask;
578     theme->a_focused_pressed_set_desk->texture[0].data.mask.mask = 
579         theme->a_unfocused_pressed_set_desk->texture[0].data.mask.mask =
580         theme->desk_set_mask;
581     theme->a_focused_unpressed_shade->texture[0].data.mask.mask = 
582         theme->a_unfocused_unpressed_shade->texture[0].data.mask.mask = 
583         theme->a_focused_pressed_shade->texture[0].data.mask.mask = 
584         theme->a_unfocused_pressed_shade->texture[0].data.mask.mask =
585         theme->shade_unset_mask;
586     theme->a_focused_pressed_set_shade->texture[0].data.mask.mask = 
587         theme->a_unfocused_pressed_set_shade->texture[0].data.mask.mask =
588         theme->shade_set_mask;
589     theme->a_focused_unpressed_iconify->texture[0].data.mask.mask = 
590         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.mask = 
591         theme->a_focused_pressed_iconify->texture[0].data.mask.mask = 
592         theme->a_unfocused_pressed_iconify->texture[0].data.mask.mask =
593         theme->iconify_mask;
594     theme->a_focused_unpressed_max->texture[0].data.mask.color = 
595         theme->a_focused_pressed_max->texture[0].data.mask.color = 
596         theme->a_focused_pressed_set_max->texture[0].data.mask.color = 
597         theme->a_focused_unpressed_close->texture[0].data.mask.color = 
598         theme->a_focused_pressed_close->texture[0].data.mask.color = 
599         theme->a_focused_unpressed_desk->texture[0].data.mask.color = 
600         theme->a_focused_pressed_desk->texture[0].data.mask.color = 
601         theme->a_focused_pressed_set_desk->texture[0].data.mask.color = 
602         theme->a_focused_unpressed_shade->texture[0].data.mask.color = 
603         theme->a_focused_pressed_shade->texture[0].data.mask.color = 
604         theme->a_focused_pressed_set_shade->texture[0].data.mask.color = 
605         theme->a_focused_unpressed_iconify->texture[0].data.mask.color = 
606         theme->a_focused_pressed_iconify->texture[0].data.mask.color =
607         theme->titlebut_focused_color;
608     theme->a_unfocused_unpressed_max->texture[0].data.mask.color = 
609         theme->a_unfocused_pressed_max->texture[0].data.mask.color = 
610         theme->a_unfocused_pressed_set_max->texture[0].data.mask.color = 
611         theme->a_unfocused_unpressed_close->texture[0].data.mask.color = 
612         theme->a_unfocused_pressed_close->texture[0].data.mask.color = 
613         theme->a_unfocused_unpressed_desk->texture[0].data.mask.color = 
614         theme->a_unfocused_pressed_desk->texture[0].data.mask.color = 
615         theme->a_unfocused_pressed_set_desk->texture[0].data.mask.color = 
616         theme->a_unfocused_unpressed_shade->texture[0].data.mask.color = 
617         theme->a_unfocused_pressed_shade->texture[0].data.mask.color = 
618         theme->a_unfocused_pressed_set_shade->texture[0].data.mask.color = 
619         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.color = 
620         theme->a_unfocused_pressed_iconify->texture[0].data.mask.color =
621         theme->titlebut_unfocused_color;
622
623     XrmDestroyDatabase(db);
624
625     theme->label_height = theme->winfont_height;
626     theme->title_height = theme->label_height + theme->bevel * 2;
627     theme->button_size = theme->label_height - 2;
628     theme->grip_width = theme->button_size * 2;
629
630     return theme;
631 }
632
633 void RrThemeFree(RrTheme *theme)
634 {
635     if (theme) {
636         g_free(theme->name);
637
638         RrColorFree(theme->b_color);
639         RrColorFree(theme->cb_unfocused_color);
640         RrColorFree(theme->cb_focused_color);
641         RrColorFree(theme->title_unfocused_color);
642         RrColorFree(theme->title_focused_color);
643         RrColorFree(theme->titlebut_unfocused_color);
644         RrColorFree(theme->titlebut_focused_color);
645         RrColorFree(theme->menu_color);
646         RrColorFree(theme->menu_title_color);
647         RrColorFree(theme->menu_disabled_color);
648         RrColorFree(theme->menu_hilite_color);
649
650         RrPixmapMaskFree(theme->max_set_mask);
651         RrPixmapMaskFree(theme->max_unset_mask);
652         RrPixmapMaskFree(theme->desk_set_mask);
653         RrPixmapMaskFree(theme->desk_unset_mask);
654         RrPixmapMaskFree(theme->shade_set_mask);
655         RrPixmapMaskFree(theme->shade_unset_mask);
656         RrPixmapMaskFree(theme->iconify_mask);
657         RrPixmapMaskFree(theme->close_mask);
658
659         RrFontClose(theme->winfont);
660         RrFontClose(theme->mtitlefont);
661         RrFontClose(theme->mfont);
662
663         g_free(theme->title_layout);
664
665         RrAppearanceFree(theme->a_focused_unpressed_max);
666         RrAppearanceFree(theme->a_focused_pressed_max);
667         RrAppearanceFree(theme->a_focused_pressed_set_max);
668         RrAppearanceFree(theme->a_unfocused_unpressed_max);
669         RrAppearanceFree(theme->a_unfocused_pressed_max);
670         RrAppearanceFree(theme->a_unfocused_pressed_set_max);
671         RrAppearanceFree(theme->a_focused_unpressed_close);
672         RrAppearanceFree(theme->a_focused_pressed_close);
673         RrAppearanceFree(theme->a_unfocused_unpressed_close);
674         RrAppearanceFree(theme->a_unfocused_pressed_close);
675         RrAppearanceFree(theme->a_focused_unpressed_desk);
676         RrAppearanceFree(theme->a_focused_pressed_desk);
677         RrAppearanceFree(theme->a_unfocused_unpressed_desk);
678         RrAppearanceFree(theme->a_unfocused_pressed_desk);
679         RrAppearanceFree(theme->a_focused_unpressed_shade);
680         RrAppearanceFree(theme->a_focused_pressed_shade);
681         RrAppearanceFree(theme->a_unfocused_unpressed_shade);
682         RrAppearanceFree(theme->a_unfocused_pressed_shade);
683         RrAppearanceFree(theme->a_focused_unpressed_iconify);
684         RrAppearanceFree(theme->a_focused_pressed_iconify);
685         RrAppearanceFree(theme->a_unfocused_unpressed_iconify);
686         RrAppearanceFree(theme->a_unfocused_pressed_iconify);
687         RrAppearanceFree(theme->a_focused_grip);
688         RrAppearanceFree(theme->a_unfocused_grip);
689         RrAppearanceFree(theme->a_focused_title);
690         RrAppearanceFree(theme->a_unfocused_title);
691         RrAppearanceFree(theme->a_focused_label);
692         RrAppearanceFree(theme->a_unfocused_label);
693         RrAppearanceFree(theme->a_icon);
694         RrAppearanceFree(theme->a_focused_handle);
695         RrAppearanceFree(theme->a_unfocused_handle);
696         RrAppearanceFree(theme->a_menu);
697         RrAppearanceFree(theme->a_menu_title);
698         RrAppearanceFree(theme->a_menu_item);
699         RrAppearanceFree(theme->a_menu_disabled);
700         RrAppearanceFree(theme->a_menu_hilite);
701         RrAppearanceFree(theme->app_hilite_bg);
702         RrAppearanceFree(theme->app_unhilite_bg);
703         RrAppearanceFree(theme->app_hilite_label);
704         RrAppearanceFree(theme->app_unhilite_label);
705         RrAppearanceFree(theme->app_icon);
706     }
707 }
708
709 static XrmDatabase loaddb(char *theme)
710 {
711     XrmDatabase db;
712
713     db = XrmGetFileDatabase(theme);
714     if (db == NULL) {
715         char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
716                                    theme, NULL);
717         db = XrmGetFileDatabase(s);
718         g_free(s);
719     }
720     if (db == NULL) {
721         char *s = g_build_filename(THEMEDIR, theme, NULL);
722         db = XrmGetFileDatabase(s);
723         g_free(s);
724     }
725     return db;
726 }
727
728 static char *create_class_name(char *rname)
729 {
730     char *rclass = g_strdup(rname);
731     char *p = rclass;
732
733     while (TRUE) {
734         *p = toupper(*p);
735         p = strchr(p+1, '.');
736         if (p == NULL) break;
737         ++p;
738         if (*p == '\0') break;
739     }
740     return rclass;
741 }
742
743 static gboolean read_int(XrmDatabase db, char *rname, int *value)
744 {
745     gboolean ret = FALSE;
746     char *rclass = create_class_name(rname);
747     char *rettype, *end;
748     XrmValue retvalue;
749   
750     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
751         retvalue.addr != NULL) {
752         *value = (int)strtol(retvalue.addr, &end, 10);
753         if (end != retvalue.addr)
754             ret = TRUE;
755     }
756
757     g_free(rclass);
758     return ret;
759 }
760
761 static gboolean read_string(XrmDatabase db, char *rname, char **value)
762 {
763     gboolean ret = FALSE;
764     char *rclass = create_class_name(rname);
765     char *rettype;
766     XrmValue retvalue;
767   
768     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
769         retvalue.addr != NULL) {
770         *value = g_strdup(retvalue.addr);
771         ret = TRUE;
772     }
773
774     g_free(rclass);
775     return ret;
776 }
777
778 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
779                            gchar *rname, RrColor **value)
780 {
781     gboolean ret = FALSE;
782     char *rclass = create_class_name(rname);
783     char *rettype;
784     XrmValue retvalue;
785   
786     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
787         retvalue.addr != NULL) {
788         RrColor *c = RrColorParse(inst, retvalue.addr);
789         if (c != NULL) {
790             *value = c;
791             ret = TRUE;
792         }
793     }
794
795     g_free(rclass);
796     return ret;
797 }
798
799 static gboolean read_mask(XrmDatabase db, const RrInstance *inst,
800                           gchar *rname, gchar *theme,
801                           RrPixmapMask **value)
802 {
803     gboolean ret = FALSE;
804     char *rclass = create_class_name(rname);
805     char *rettype;
806     char *s;
807     char *button_dir;
808     XrmValue retvalue;
809     int hx, hy; /* ignored */
810     unsigned int w, h;
811     unsigned char *b;
812   
813     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
814         retvalue.addr != NULL) {
815
816         button_dir = g_strdup_printf("%s_data", theme);
817
818         s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
819                              button_dir, retvalue.addr, NULL);
820
821         if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess)
822             ret = TRUE;
823         else {
824             g_free(s);
825             s = g_build_filename(THEMEDIR, button_dir, retvalue.addr, NULL);
826         
827             if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) 
828                 ret = TRUE;
829             else {
830                 char *themename;
831
832                 g_free(s);
833                 themename = g_path_get_basename(theme);
834                 s = g_strdup_printf("%s/%s_data/%s", theme,
835                                     themename, retvalue.addr);
836                 g_free(themename);
837                 if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) ==
838                     BitmapSuccess) 
839                     ret = TRUE;
840                 else
841                     g_message("Unable to find bitmap '%s'", retvalue.addr);
842             }
843         }
844
845         if (ret) {
846             *value = RrPixmapMaskNew(inst, w, h, (char*)b);
847             XFree(b);
848         }
849       
850         g_free(s);
851         g_free(button_dir);
852     }
853
854     g_free(rclass);
855     return ret;
856 }
857
858 static void parse_appearance(gchar *tex, RrSurfaceColorType *grad,
859                              RrReliefType *relief, RrBevelType *bevel,
860                              gboolean *interlaced, gboolean *border)
861 {
862     char *t;
863
864     /* convert to all lowercase */
865     for (t = tex; *t != '\0'; ++t)
866         *t = g_ascii_tolower(*t);
867
868     if (strstr(tex, "parentrelative") != NULL) {
869         *grad = RR_SURFACE_PARENTREL;
870     } else {
871         if (strstr(tex, "gradient") != NULL) {
872             if (strstr(tex, "crossdiagonal") != NULL)
873                 *grad = RR_SURFACE_CROSS_DIAGONAL;
874             else if (strstr(tex, "rectangle") != NULL)
875                 *grad = RR_SURFACE_RECTANGLE;
876             else if (strstr(tex, "pyramid") != NULL)
877                 *grad = RR_SURFACE_PYRAMID;
878             else if (strstr(tex, "pipecross") != NULL)
879                 *grad = RR_SURFACE_PIPECROSS;
880             else if (strstr(tex, "elliptic") != NULL)
881                 *grad = RR_SURFACE_PIPECROSS;
882             else if (strstr(tex, "horizontal") != NULL)
883                 *grad = RR_SURFACE_HORIZONTAL;
884             else if (strstr(tex, "vertical") != NULL)
885                 *grad = RR_SURFACE_VERTICAL;
886             else
887                 *grad = RR_SURFACE_DIAGONAL;
888         } else {
889             *grad = RR_SURFACE_SOLID;
890         }
891
892         if (strstr(tex, "sunken") != NULL)
893             *relief = RR_RELIEF_SUNKEN;
894         else if (strstr(tex, "flat") != NULL)
895             *relief = RR_RELIEF_FLAT;
896         else
897             *relief = RR_RELIEF_RAISED;
898         
899         *border = FALSE;
900         if (*relief == RR_RELIEF_FLAT) {
901             if (strstr(tex, "border") != NULL)
902                 *border = TRUE;
903         } else {
904             if (strstr(tex, "bevel2") != NULL)
905                 *bevel = RR_BEVEL_2;
906             else
907                 *bevel = RR_BEVEL_1;
908         }
909
910         if (strstr(tex, "interlaced") != NULL)
911             *interlaced = TRUE;
912         else
913             *interlaced = FALSE;
914     }
915 }
916
917
918 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
919                            gchar *rname, RrAppearance *value)
920 {
921     gboolean ret = FALSE;
922     char *rclass = create_class_name(rname), *cname, *ctoname, *bcname;
923     char *rettype;
924     XrmValue retvalue;
925
926     cname = g_strconcat(rname, ".color", NULL);
927     ctoname = g_strconcat(rname, ".colorTo", NULL);
928     bcname = g_strconcat(rname, ".borderColor", NULL);
929
930     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
931         retvalue.addr != NULL) {
932         parse_appearance(retvalue.addr,
933                          &value->surface.grad,
934                          &value->surface.relief,
935                          &value->surface.bevel,
936                          &value->surface.interlaced,
937                          &value->surface.border);
938         if (!read_color(db, inst, cname, &value->surface.primary))
939             value->surface.primary = RrColorNew(inst, 0, 0, 0);
940         if (!read_color(db, inst, ctoname, &value->surface.secondary))
941             value->surface.secondary = RrColorNew(inst, 0, 0, 0);
942         if (value->surface.border)
943             if (!read_color(db, inst, bcname,
944                             &value->surface.border_color))
945                 value->surface.border_color = RrColorNew(inst, 0, 0, 0);
946         ret = TRUE;
947     }
948
949     g_free(bcname);
950     g_free(ctoname);
951     g_free(cname);
952     g_free(rclass);
953     return ret;
954 }
955
956 static void set_default_appearance(RrAppearance *a)
957 {
958     a->surface.grad = RR_SURFACE_SOLID;
959     a->surface.relief = RR_RELIEF_FLAT;
960     a->surface.bevel = RR_BEVEL_1;
961     a->surface.interlaced = FALSE;
962     a->surface.border = FALSE;
963     a->surface.primary = RrColorNew(a->inst, 0, 0, 0);
964     a->surface.secondary = RrColorNew(a->inst, 0, 0, 0);
965 }