add menu.bullet.selected.imageColor
[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 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 static XrmDatabase loaddb(RrTheme *theme, char *name);
14 static gboolean read_int(XrmDatabase db, char *rname, int *value);
15 static gboolean read_string(XrmDatabase db, char *rname, char **value);
16 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
17                            gchar *rname, RrColor **value);
18 static gboolean read_mask(const RrInstance *inst,
19                           gchar *maskname, RrTheme *theme,
20                           RrPixmapMask **value);
21 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
22                                 gchar *rname, RrAppearance *value,
23                                 gboolean allow_trans);
24 static void set_default_appearance(RrAppearance *a);
25
26 RrTheme* RrThemeNew(const RrInstance *inst, gchar *name)
27 {
28     XrmDatabase db = NULL;
29     RrJustify winjust, mtitlejust;
30     gchar *str;
31     gchar *font_str;
32     RrTheme *theme;
33
34     theme = g_new0(RrTheme, 1);
35
36     theme->inst = inst;
37
38     theme->show_handle = TRUE;
39
40     theme->a_disabled_focused_max = RrAppearanceNew(inst, 1);
41     theme->a_disabled_unfocused_max = RrAppearanceNew(inst, 1);
42     theme->a_hover_focused_max = RrAppearanceNew(inst, 1);
43     theme->a_hover_unfocused_max = RrAppearanceNew(inst, 1);
44     theme->a_toggled_focused_max = RrAppearanceNew(inst, 1);
45     theme->a_toggled_unfocused_max = RrAppearanceNew(inst, 1);
46     theme->a_focused_unpressed_max = RrAppearanceNew(inst, 1);
47     theme->a_focused_pressed_max = RrAppearanceNew(inst, 1);
48     theme->a_unfocused_unpressed_max = RrAppearanceNew(inst, 1);
49     theme->a_unfocused_pressed_max = RrAppearanceNew(inst, 1);
50     theme->a_focused_grip = RrAppearanceNew(inst, 0);
51     theme->a_unfocused_grip = RrAppearanceNew(inst, 0);
52     theme->a_focused_title = RrAppearanceNew(inst, 0);
53     theme->a_unfocused_title = RrAppearanceNew(inst, 0);
54     theme->a_focused_label = RrAppearanceNew(inst, 1);
55     theme->a_unfocused_label = RrAppearanceNew(inst, 1);
56     theme->a_icon = RrAppearanceNew(inst, 1);
57     theme->a_focused_handle = RrAppearanceNew(inst, 0);
58     theme->a_unfocused_handle = RrAppearanceNew(inst, 0);
59     theme->a_menu = RrAppearanceNew(inst, 0);
60     theme->a_menu_title = RrAppearanceNew(inst, 1);
61     theme->a_menu_normal = RrAppearanceNew(inst, 0);
62     theme->a_menu_disabled = RrAppearanceNew(inst, 0);
63     theme->a_menu_selected = RrAppearanceNew(inst, 0);
64     theme->a_menu_text_normal = RrAppearanceNew(inst, 1);
65     theme->a_menu_text_disabled = RrAppearanceNew(inst, 1);
66     theme->a_menu_text_selected = RrAppearanceNew(inst, 1);
67     theme->a_menu_bullet_normal = RrAppearanceNew(inst, 1);
68     theme->a_menu_bullet_selected = RrAppearanceNew(inst, 1);
69     theme->a_clear = RrAppearanceNew(inst, 0);
70     theme->a_clear_tex = RrAppearanceNew(inst, 1);
71
72     theme->app_hilite_bg = RrAppearanceNew(inst, 0);
73     theme->app_unhilite_bg = RrAppearanceNew(inst, 0);
74     theme->app_hilite_label = RrAppearanceNew(inst, 1);
75     theme->app_unhilite_label = RrAppearanceNew(inst, 1);
76
77     if (name) {
78         db = loaddb(theme, name);
79         if (db == NULL) {
80             g_warning("Failed to load the theme '%s'\n"
81                       "Falling back to the default: '%s'",
82                       name, DEFAULT_THEME);
83         } else
84             theme->name = g_path_get_basename(name);
85     }
86     if (db == NULL) {
87         db = loaddb(theme, DEFAULT_THEME);
88         if (db == NULL) {
89             g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
90             return NULL;
91         } else
92             theme->name = g_path_get_basename(DEFAULT_THEME);
93     }
94
95     /* load the font stuff */
96     if (!read_string(db, "window.label.focus.font", &font_str))
97         font_str = "arial,sans:bold:pixelsize=10:shadow=y:shadowtint=50";
98
99     if (!(theme->winfont_focused = RrFontOpen(inst, font_str))) {
100         RrThemeFree(theme);
101         return NULL;
102     }
103     theme->winfont_height = RrFontHeight(theme->winfont_focused);
104
105     if (!read_string(db, "window.label.unfocus.font", &font_str))
106         /* font_str will already be set to the last one */;
107
108     if (!(theme->winfont_unfocused = RrFontOpen(inst, font_str))) {
109         RrThemeFree(theme);
110         return NULL;
111     }
112     theme->winfont_height = MAX(theme->winfont_height,
113                                 RrFontHeight(theme->winfont_unfocused));
114
115     winjust = RR_JUSTIFY_LEFT;
116     if (read_string(db, "window.label.justify", &str)) {
117         if (!g_ascii_strcasecmp(str, "right"))
118             winjust = RR_JUSTIFY_RIGHT;
119         else if (!g_ascii_strcasecmp(str, "center"))
120             winjust = RR_JUSTIFY_CENTER;
121     }
122
123     if (!read_string(db, "menu.title.font", &font_str))
124         font_str = "arial,sans:bold:pixelsize=12:shadow=y";
125
126     if (!(theme->mtitlefont = RrFontOpen(inst, font_str))) {
127         RrThemeFree(theme);
128         return NULL;
129     }
130     theme->mtitlefont_height = RrFontHeight(theme->mtitlefont);
131
132     mtitlejust = RR_JUSTIFY_LEFT;
133     if (read_string(db, "menu.title.justify", &str)) {
134         if (!g_ascii_strcasecmp(str, "right"))
135             mtitlejust = RR_JUSTIFY_RIGHT;
136         else if (!g_ascii_strcasecmp(str, "center"))
137             mtitlejust = RR_JUSTIFY_CENTER;
138     }
139
140     if (!read_string(db, "menu.items.font", &font_str))
141         font_str = "arial,sans:bold:pixelsize=11:shadow=y";
142
143     if (!(theme->mfont = RrFontOpen(inst, font_str))) {
144         RrThemeFree(theme);
145         return NULL;
146     }
147     theme->mfont_height = RrFontHeight(theme->mfont);
148
149     /* load direct dimensions */
150     if (!read_int(db, "menuOverlap", &theme->menu_overlap) ||
151         theme->menu_overlap < 0 || theme->menu_overlap > 20)
152         theme->menu_overlap = 0;
153     if (!read_int(db, "handleWidth", &theme->handle_height))
154         theme->handle_height = 6;
155     if (!theme->handle_height)
156         theme->show_handle = FALSE;
157     if (theme->handle_height <= 0 || theme->handle_height > 100)
158         theme->handle_height = 6;
159     if (!read_int(db, "bevelWidth", &theme->bevel) ||
160         theme->bevel <= 0 || theme->bevel > 100)
161         theme->bevel = 3;
162     if (!read_int(db, "borderWidth", &theme->bwidth) ||
163         theme->bwidth < 0 || theme->bwidth > 100)
164         theme->bwidth = 1;
165     if (!read_int(db, "frameWidth", &theme->cbwidth) ||
166         theme->cbwidth < 0 || theme->cbwidth > 100)
167         theme->cbwidth = theme->bevel;
168
169     /* load colors */
170     if (!read_color(db, inst,
171                     "borderColor", &theme->b_color))
172         theme->b_color = RrColorNew(inst, 0, 0, 0);
173     if (!read_color(db, inst,
174                     "window.frame.focusColor", &theme->cb_focused_color))
175         theme->cb_focused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
176     if (!read_color(db, inst,
177                     "window.frame.unfocusColor",&theme->cb_unfocused_color))
178         theme->cb_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
179     if (!read_color(db, inst,
180                     "window.label.focus.textColor",
181                     &theme->title_focused_color))
182         theme->title_focused_color = RrColorNew(inst, 0x0, 0x0, 0x0);
183     if (!read_color(db, inst,
184                     "window.label.unfocus.textColor",
185                     &theme->title_unfocused_color))
186         theme->title_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
187     if (!read_color(db, inst,
188                     "window.button.focus.imageColor",
189                     &theme->titlebut_focused_unpressed_color))
190         theme->titlebut_focused_unpressed_color = RrColorNew(inst, 0, 0, 0);
191     if (!read_color(db, inst,
192                     "window.button.unfocus.imageColor",
193                     &theme->titlebut_unfocused_unpressed_color))
194         theme->titlebut_unfocused_unpressed_color =
195             RrColorNew(inst, 0xff, 0xff, 0xff);
196     if (!read_color(db, inst,
197                     "window.button.pressed.focus.imageColor",
198                     &theme->titlebut_focused_pressed_color))
199         theme->titlebut_focused_pressed_color =
200             RrColorNew(inst,
201                        theme->titlebut_focused_unpressed_color->r,
202                        theme->titlebut_focused_unpressed_color->g,
203                        theme->titlebut_focused_unpressed_color->b);
204     if (!read_color(db, inst,
205                     "window.button.pressed.unfocus.imageColor",
206                     &theme->titlebut_unfocused_pressed_color))
207         theme->titlebut_unfocused_pressed_color =
208             RrColorNew(inst,
209                        theme->titlebut_unfocused_unpressed_color->r,
210                        theme->titlebut_unfocused_unpressed_color->g,
211                        theme->titlebut_unfocused_unpressed_color->b);
212     if (!read_color(db, inst,
213                     "window.button.disabled.focus.imageColor",
214                     &theme->titlebut_disabled_focused_color))
215         theme->titlebut_disabled_focused_color =
216             RrColorNew(inst, 0xff, 0xff, 0xff);
217     if (!read_color(db, inst,
218                     "window.button.disabled.unfocus.imageColor",
219                     &theme->titlebut_disabled_unfocused_color))
220         theme->titlebut_disabled_unfocused_color = RrColorNew(inst, 0, 0, 0);
221     if (!read_color(db, inst,
222                     "window.button.hover.focus.imageColor",
223                     &theme->titlebut_hover_focused_color))
224         theme->titlebut_hover_focused_color =
225             RrColorNew(inst,
226                        theme->titlebut_focused_unpressed_color->r,
227                        theme->titlebut_focused_unpressed_color->g,
228                        theme->titlebut_focused_unpressed_color->b);
229     if (!read_color(db, inst,
230                     "window.button.hover.unfocus.imageColor",
231                     &theme->titlebut_hover_unfocused_color))
232         theme->titlebut_hover_unfocused_color =
233             RrColorNew(inst,
234                        theme->titlebut_unfocused_unpressed_color->r,
235                        theme->titlebut_unfocused_unpressed_color->g,
236                        theme->titlebut_unfocused_unpressed_color->b);
237     if (!read_color(db, inst,
238                     "window.button.toggled.focus.imageColor",
239                     &theme->titlebut_toggled_focused_color))
240         theme->titlebut_toggled_focused_color =
241             RrColorNew(inst,
242                        theme->titlebut_focused_pressed_color->r,
243                        theme->titlebut_focused_pressed_color->g,
244                        theme->titlebut_focused_pressed_color->b);
245     if (!read_color(db, inst,
246                     "window.button.toggled.unfocus.imageColor",
247                     &theme->titlebut_toggled_unfocused_color))
248         theme->titlebut_toggled_unfocused_color =
249             RrColorNew(inst,
250                        theme->titlebut_unfocused_pressed_color->r,
251                        theme->titlebut_unfocused_pressed_color->g,
252                        theme->titlebut_unfocused_pressed_color->b);
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.items.textColor", &theme->menu_color))
258         theme->menu_color = RrColorNew(inst, 0xff, 0xff, 0xff);
259     if (!read_color(db, inst,
260                     "menu.disabled.textColor", &theme->menu_disabled_color))
261         theme->menu_disabled_color = RrColorNew(inst, 0, 0, 0);
262     if (!read_color(db, inst,
263                     "menu.selected.textColor", &theme->menu_selected_color))
264         theme->menu_selected_color = RrColorNew(inst, 0, 0, 0);
265     if (!read_color(db, inst,
266                     "menu.bullet.imageColor",
267                     &theme->menu_bullet_normal_color))
268         theme->menu_bullet_normal_color = RrColorNew(inst,
269                                                      theme->menu_color->r,
270                                                      theme->menu_color->g,
271                                                      theme->menu_color->b);
272     if (!read_color(db, inst,
273                     "menu.bullet.selected.imageColor",
274                     &theme->menu_bullet_selected_color))
275         theme->menu_bullet_selected_color =
276             RrColorNew(inst,
277                        theme->menu_selected_color->r,
278                        theme->menu_selected_color->g,
279                        theme->menu_selected_color->b);
280
281     
282     if (read_mask(inst, "max.xbm", theme, &theme->max_mask)) {
283         if (!read_mask(inst, "max_pressed.xbm", theme,
284                        &theme->max_pressed_mask)) {
285             theme->max_pressed_mask = RrPixmapMaskCopy(theme->max_mask);
286         } 
287         if (!read_mask(inst, "max_toggled.xbm", theme,
288                        &theme->max_toggled_mask)) {
289             theme->max_toggled_mask =
290                 RrPixmapMaskCopy(theme->max_pressed_mask);
291         }
292         if (!read_mask(inst, "max_disabled.xbm", theme,
293                        &theme->max_disabled_mask)) {
294             theme->max_disabled_mask = RrPixmapMaskCopy(theme->max_mask);
295         } 
296         if (!read_mask(inst, "max_hover.xbm", theme, &theme->max_hover_mask)) {
297             theme->max_hover_mask = RrPixmapMaskCopy(theme->max_mask);
298         }
299    } else {
300         {
301             guchar data[] = { 0x7f, 0x7f, 0x7f, 0x41, 0x41, 0x41, 0x7f };
302             theme->max_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
303         }
304         {
305             guchar data[] = { 0x7c, 0x44, 0x47, 0x47, 0x7f, 0x1f, 0x1f };
306             theme->max_toggled_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
307         }
308         theme->max_pressed_mask = RrPixmapMaskCopy(theme->max_mask);
309         theme->max_disabled_mask = RrPixmapMaskCopy(theme->max_mask);
310         theme->max_hover_mask = RrPixmapMaskCopy(theme->max_mask);
311     }
312
313     if (read_mask(inst, "iconify.xbm", theme, &theme->iconify_mask)) {
314         if (!read_mask(inst, "iconify_pressed.xbm", theme,
315                        &theme->iconify_pressed_mask)) {
316             theme->iconify_pressed_mask =
317                 RrPixmapMaskCopy(theme->iconify_mask);
318         } 
319         if (!read_mask(inst, "iconify_disabled.xbm", theme,
320                        &theme->iconify_disabled_mask)) {
321             theme->iconify_disabled_mask =
322                 RrPixmapMaskCopy(theme->iconify_mask);
323         } 
324         if (!read_mask(inst, "iconify_hover.xbm", theme,
325                        &theme->iconify_hover_mask)) {
326             theme->iconify_hover_mask = RrPixmapMaskCopy(theme->iconify_mask);
327         }
328     } else {
329         {
330             guchar data[] = { 0x00, 0x00, 0x00, 0x00, 0x7f, 0x7f, 0x7f };
331             theme->iconify_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
332         }
333         theme->iconify_pressed_mask = RrPixmapMaskCopy(theme->iconify_mask);
334         theme->iconify_disabled_mask = RrPixmapMaskCopy(theme->iconify_mask);
335         theme->iconify_hover_mask = RrPixmapMaskCopy(theme->iconify_mask);
336     }
337
338     if (read_mask(inst, "desk.xbm", theme, &theme->desk_mask)) {
339         if (!read_mask(inst, "desk_pressed.xbm", theme,
340                        &theme->desk_pressed_mask)) {
341             theme->desk_pressed_mask = RrPixmapMaskCopy(theme->desk_mask);
342         } 
343         if (!read_mask(inst, "desk_toggled.xbm", theme,
344                        &theme->desk_toggled_mask)) {
345             theme->desk_toggled_mask =
346                 RrPixmapMaskCopy(theme->desk_pressed_mask);
347         }
348         if (!read_mask(inst, "desk_disabled.xbm", theme,
349                        &theme->desk_disabled_mask)) {
350             theme->desk_disabled_mask = RrPixmapMaskCopy(theme->desk_mask);
351         } 
352         if (!read_mask(inst, "desk_hover.xbm", theme, 
353                        &theme->desk_hover_mask)) {
354             theme->desk_hover_mask = RrPixmapMaskCopy(theme->desk_mask);
355         }
356     } else {
357         {
358             guchar data[] = { 0x63, 0x63, 0x00, 0x00, 0x00, 0x63, 0x63 };
359             theme->desk_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
360         }
361         {
362             guchar data[] = { 0x00, 0x36, 0x36, 0x08, 0x36, 0x36, 0x00 };
363             theme->desk_toggled_mask = RrPixmapMaskNew(inst, 7, 7,
364                                                        (char*)data);
365         }
366         theme->desk_pressed_mask = RrPixmapMaskCopy(theme->desk_mask);
367         theme->desk_disabled_mask = RrPixmapMaskCopy(theme->desk_mask);
368         theme->desk_hover_mask = RrPixmapMaskCopy(theme->desk_mask);
369     }
370
371     if (read_mask(inst, "shade.xbm", theme, &theme->shade_mask)) {
372         if (!read_mask(inst, "shade_pressed.xbm", theme,
373                        &theme->shade_pressed_mask)) {
374             theme->shade_pressed_mask = RrPixmapMaskCopy(theme->shade_mask);
375         } 
376         if (!read_mask(inst, "shade_toggled.xbm", theme,
377                        &theme->shade_toggled_mask)) {
378             theme->shade_toggled_mask =
379                 RrPixmapMaskCopy(theme->shade_pressed_mask);
380         }
381         if (!read_mask(inst, "shade_disabled.xbm", theme,
382                        &theme->shade_disabled_mask)) {
383             theme->shade_disabled_mask = RrPixmapMaskCopy(theme->shade_mask);
384         } 
385         if (!read_mask(inst, "shade_hover.xbm", theme, 
386                        &theme->shade_hover_mask)) {
387             theme->shade_hover_mask = RrPixmapMaskCopy(theme->shade_mask);
388         }
389     } else {
390         {
391             guchar data[] = { 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00 };
392             theme->shade_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
393         }
394         {
395             guchar data[] = { 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x7f };
396             theme->shade_toggled_mask = RrPixmapMaskNew(inst, 7, 7,
397                                                         (char*)data);
398         }
399         theme->shade_pressed_mask = RrPixmapMaskCopy(theme->shade_mask);
400         theme->shade_disabled_mask = RrPixmapMaskCopy(theme->shade_mask);
401         theme->shade_hover_mask = RrPixmapMaskCopy(theme->shade_mask);
402     }
403
404     if (read_mask(inst, "close.xbm", theme, &theme->close_mask)) {
405         if (!read_mask(inst, "close_pressed.xbm", theme,
406                        &theme->close_pressed_mask)) {
407             theme->close_pressed_mask = RrPixmapMaskCopy(theme->close_mask);
408         } 
409         if (!read_mask(inst, "close_disabled.xbm", theme,
410                        &theme->close_disabled_mask)) {
411             theme->close_disabled_mask = RrPixmapMaskCopy(theme->close_mask);
412         } 
413         if (!read_mask(inst, "close_hover.xbm", theme,
414                        &theme->close_hover_mask)) {
415             theme->close_hover_mask = RrPixmapMaskCopy(theme->close_mask);
416         }
417     } else {
418         {
419             guchar data[] = { 0x63, 0x77, 0x3e, 0x1c, 0x3e, 0x77, 0x63 };
420             theme->close_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
421         }
422         theme->close_pressed_mask = RrPixmapMaskCopy(theme->close_mask);
423         theme->close_disabled_mask = RrPixmapMaskCopy(theme->close_mask);
424         theme->close_hover_mask = RrPixmapMaskCopy(theme->close_mask);
425     }
426
427     if (!read_mask(inst, "bullet.xbm", theme, &theme->menu_bullet_mask)) {
428         guchar data[] = { 0x01, 0x03, 0x07, 0x0f, 0x07, 0x03, 0x01 };
429         theme->menu_bullet_mask = RrPixmapMaskNew(inst, 4, 7, (char*)data);
430     }
431
432     /* read the decoration textures */
433     if (!read_appearance(db, inst,
434                          "window.title.focus", theme->a_focused_title,
435                          FALSE))
436         set_default_appearance(theme->a_focused_title);
437     if (!read_appearance(db, inst,
438                          "window.title.unfocus", theme->a_unfocused_title,
439                          FALSE))
440         set_default_appearance(theme->a_unfocused_title);
441     if (!read_appearance(db, inst,
442                          "window.label.focus", theme->a_focused_label,
443                          TRUE))
444         set_default_appearance(theme->a_focused_label);
445     if (!read_appearance(db, inst,
446                          "window.label.unfocus", theme->a_unfocused_label,
447                          TRUE))
448         set_default_appearance(theme->a_unfocused_label);
449     if (!read_appearance(db, inst,
450                          "window.handle.focus", theme->a_focused_handle,
451                          FALSE))
452         set_default_appearance(theme->a_focused_handle);
453     if (!read_appearance(db, inst,
454                          "window.handle.unfocus",theme->a_unfocused_handle,
455                          FALSE))
456         set_default_appearance(theme->a_unfocused_handle);
457     if (!read_appearance(db, inst,
458                          "window.grip.focus", theme->a_focused_grip,
459                          TRUE))
460         set_default_appearance(theme->a_focused_grip);
461     if (!read_appearance(db, inst,
462                          "window.grip.unfocus", theme->a_unfocused_grip,
463                          TRUE))
464         set_default_appearance(theme->a_unfocused_grip);
465     if (!read_appearance(db, inst,
466                          "menu.items", theme->a_menu,
467                          FALSE))
468         set_default_appearance(theme->a_menu);
469     if (!read_appearance(db, inst,
470                          "menu.title", theme->a_menu_title,
471                          FALSE))
472         set_default_appearance(theme->a_menu_title);
473     if (!read_appearance(db, inst,
474                          "menu.selected", theme->a_menu_selected,
475                          TRUE))
476         set_default_appearance(theme->a_menu_selected);
477
478     /* read the appearances for rendering non-decorations */
479     if (!read_appearance(db, inst,
480                          "window.title.focus", theme->app_hilite_bg,
481                          FALSE))
482         set_default_appearance(theme->app_hilite_bg);
483     if (!read_appearance(db, inst,
484                          "window.label.focus", theme->app_hilite_label,
485                          TRUE))
486         set_default_appearance(theme->app_hilite_label);
487     if (!read_appearance(db, inst,
488                          "window.title.unfocus", theme->app_unhilite_bg,
489                          FALSE))
490         set_default_appearance(theme->app_unhilite_bg);
491     if (!read_appearance(db, inst,
492                          "window.label.unfocus", theme->app_unhilite_label,
493                          TRUE))
494         set_default_appearance(theme->app_unhilite_label);
495
496     /* read buttons textures */
497     if (!read_appearance(db, inst,
498                          "window.button.disabled.focus",
499                          theme->a_disabled_focused_max,
500                          TRUE))
501         set_default_appearance(theme->a_disabled_focused_max);
502     if (!read_appearance(db, inst,
503                          "window.button.disabled.unfocus",
504                          theme->a_disabled_unfocused_max,
505                          TRUE))
506         set_default_appearance(theme->a_disabled_unfocused_max);
507     if (!read_appearance(db, inst,
508                          "window.button.pressed.focus",
509                          theme->a_focused_pressed_max,
510                          TRUE))
511         set_default_appearance(theme->a_focused_pressed_max);
512     if (!read_appearance(db, inst,
513                          "window.button.pressed.unfocus",
514                          theme->a_unfocused_pressed_max,
515                          TRUE))
516         set_default_appearance(theme->a_unfocused_pressed_max);
517     if (!read_appearance(db, inst,
518                          "window.button.toggled.focus",
519                          theme->a_toggled_focused_max,
520                          TRUE))
521     {
522         RrAppearanceFree(theme->a_toggled_focused_max);
523         theme->a_toggled_focused_max =
524             RrAppearanceCopy(theme->a_focused_pressed_max);
525     }
526     if (!read_appearance(db, inst,
527                          "window.button.toggled.unfocus",
528                          theme->a_toggled_unfocused_max,
529                          TRUE))
530     {
531         RrAppearanceFree(theme->a_toggled_unfocused_max);
532         theme->a_toggled_unfocused_max =
533             RrAppearanceCopy(theme->a_unfocused_pressed_max);
534     }
535     if (!read_appearance(db, inst,
536                          "window.button.focus",
537                          theme->a_focused_unpressed_max,
538                          TRUE))
539         set_default_appearance(theme->a_focused_unpressed_max);
540     if (!read_appearance(db, inst,
541                          "window.button.unfocus",
542                          theme->a_unfocused_unpressed_max,
543                          TRUE))
544         set_default_appearance(theme->a_unfocused_unpressed_max);
545     if (!read_appearance(db, inst,
546                          "window.button.hover.focus",
547                          theme->a_hover_focused_max,
548                          TRUE))
549     {
550         RrAppearanceFree(theme->a_hover_focused_max);
551         theme->a_hover_focused_max =
552             RrAppearanceCopy(theme->a_focused_unpressed_max);
553     }
554     if (!read_appearance(db, inst,
555                          "window.button.hover.unfocus",
556                          theme->a_hover_unfocused_max,
557                          TRUE))
558     {
559         RrAppearanceFree(theme->a_hover_unfocused_max);
560         theme->a_hover_unfocused_max =
561             RrAppearanceCopy(theme->a_unfocused_unpressed_max);
562     }
563
564     theme->a_disabled_focused_close =
565         RrAppearanceCopy(theme->a_disabled_focused_max);
566     theme->a_disabled_unfocused_close =
567         RrAppearanceCopy(theme->a_disabled_unfocused_max);
568     theme->a_hover_focused_close =
569         RrAppearanceCopy(theme->a_hover_focused_max);
570     theme->a_hover_unfocused_close =
571         RrAppearanceCopy(theme->a_hover_unfocused_max);
572     theme->a_unfocused_unpressed_close =
573         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
574     theme->a_unfocused_pressed_close =
575         RrAppearanceCopy(theme->a_unfocused_pressed_max);
576     theme->a_focused_unpressed_close =
577         RrAppearanceCopy(theme->a_focused_unpressed_max);
578     theme->a_focused_pressed_close =
579         RrAppearanceCopy(theme->a_focused_pressed_max);
580     theme->a_disabled_focused_desk =
581         RrAppearanceCopy(theme->a_disabled_focused_max);
582     theme->a_disabled_unfocused_desk =
583         RrAppearanceCopy(theme->a_disabled_unfocused_max);
584     theme->a_hover_focused_desk =
585         RrAppearanceCopy(theme->a_hover_focused_max);
586     theme->a_hover_unfocused_desk =
587         RrAppearanceCopy(theme->a_hover_unfocused_max); 
588     theme->a_toggled_focused_desk =
589         RrAppearanceCopy(theme->a_toggled_focused_max);
590    theme->a_toggled_unfocused_desk =
591         RrAppearanceCopy(theme->a_toggled_unfocused_max);
592     theme->a_unfocused_unpressed_desk =
593         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
594     theme->a_unfocused_pressed_desk =
595         RrAppearanceCopy(theme->a_unfocused_pressed_max);
596     theme->a_focused_unpressed_desk =
597         RrAppearanceCopy(theme->a_focused_unpressed_max);
598     theme->a_focused_pressed_desk =
599         RrAppearanceCopy(theme->a_focused_pressed_max);
600     theme->a_disabled_focused_shade =
601         RrAppearanceCopy(theme->a_disabled_focused_max);
602     theme->a_disabled_unfocused_shade =
603         RrAppearanceCopy(theme->a_disabled_unfocused_max);
604     theme->a_hover_focused_shade =
605         RrAppearanceCopy(theme->a_hover_focused_max);
606     theme->a_hover_unfocused_shade =
607         RrAppearanceCopy(theme->a_hover_unfocused_max);
608     theme->a_toggled_focused_shade =
609         RrAppearanceCopy(theme->a_toggled_focused_max);
610     theme->a_toggled_unfocused_shade =
611         RrAppearanceCopy(theme->a_toggled_unfocused_max);
612     theme->a_unfocused_unpressed_shade =
613         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
614     theme->a_unfocused_pressed_shade =
615         RrAppearanceCopy(theme->a_unfocused_pressed_max);
616     theme->a_focused_unpressed_shade =
617         RrAppearanceCopy(theme->a_focused_unpressed_max);
618     theme->a_focused_pressed_shade =
619         RrAppearanceCopy(theme->a_focused_pressed_max);
620     theme->a_disabled_focused_iconify =
621         RrAppearanceCopy(theme->a_disabled_focused_max);
622     theme->a_disabled_unfocused_iconify =
623         RrAppearanceCopy(theme->a_disabled_focused_max);
624     theme->a_hover_focused_iconify =
625         RrAppearanceCopy(theme->a_hover_focused_max);
626     theme->a_hover_unfocused_iconify =
627         RrAppearanceCopy(theme->a_hover_unfocused_max);
628     theme->a_unfocused_unpressed_iconify =
629         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
630     theme->a_unfocused_pressed_iconify =
631         RrAppearanceCopy(theme->a_unfocused_pressed_max);
632     theme->a_focused_unpressed_iconify =
633         RrAppearanceCopy(theme->a_focused_unpressed_max);
634     theme->a_focused_pressed_iconify =
635         RrAppearanceCopy(theme->a_focused_pressed_max);
636
637     theme->a_icon->surface.grad =
638         theme->a_clear->surface.grad =
639         theme->a_clear_tex->surface.grad =
640         theme->a_menu_normal->surface.grad =
641         theme->a_menu_disabled->surface.grad =
642         theme->a_menu_text_normal->surface.grad =
643         theme->a_menu_text_disabled->surface.grad =
644         theme->a_menu_text_selected->surface.grad =
645         theme->a_menu_bullet_normal->surface.grad =
646         theme->a_menu_bullet_selected->surface.grad = RR_SURFACE_PARENTREL;
647
648     /* set up the textures */
649     theme->a_focused_label->texture[0].type = 
650         theme->app_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
651     theme->a_focused_label->texture[0].data.text.justify = winjust;
652     theme->app_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
653     theme->a_focused_label->texture[0].data.text.font =
654         theme->app_hilite_label->texture[0].data.text.font =
655         theme->winfont_focused;
656     theme->a_focused_label->texture[0].data.text.color =
657         theme->app_hilite_label->texture[0].data.text.color =
658         theme->title_focused_color;
659
660     theme->a_unfocused_label->texture[0].type =
661         theme->app_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
662     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
663     theme->app_unhilite_label->texture[0].data.text.justify =
664         RR_JUSTIFY_LEFT;
665     theme->a_unfocused_label->texture[0].data.text.font =
666         theme->app_unhilite_label->texture[0].data.text.font =
667         theme->winfont_unfocused;
668     theme->a_unfocused_label->texture[0].data.text.color =
669         theme->app_unhilite_label->texture[0].data.text.color =
670         theme->title_unfocused_color;
671
672     theme->a_menu_title->texture[0].type = RR_TEXTURE_TEXT;
673     theme->a_menu_title->texture[0].data.text.justify = mtitlejust;
674     theme->a_menu_title->texture[0].data.text.font = theme->mtitlefont;
675     theme->a_menu_title->texture[0].data.text.color = theme->menu_title_color;
676
677     theme->a_menu_text_normal->texture[0].type =
678         theme->a_menu_text_disabled->texture[0].type = 
679         theme->a_menu_text_selected->texture[0].type = RR_TEXTURE_TEXT;
680     theme->a_menu_text_normal->texture[0].data.text.justify = 
681         theme->a_menu_text_disabled->texture[0].data.text.justify = 
682         theme->a_menu_text_selected->texture[0].data.text.justify =
683         RR_JUSTIFY_LEFT;
684     theme->a_menu_text_normal->texture[0].data.text.font =
685         theme->a_menu_text_disabled->texture[0].data.text.font =
686         theme->a_menu_text_selected->texture[0].data.text.font = theme->mfont;
687     theme->a_menu_text_normal->texture[0].data.text.color = theme->menu_color;
688     theme->a_menu_text_disabled->texture[0].data.text.color =
689         theme->menu_disabled_color;
690     theme->a_menu_text_selected->texture[0].data.text.color =
691         theme->menu_selected_color;
692     theme->a_menu_bullet_normal->texture[0].data.mask.color =
693         theme->menu_bullet_normal_color;
694     theme->a_menu_bullet_selected->texture[0].data.mask.color =
695         theme->menu_bullet_selected_color;
696
697     theme->a_disabled_focused_max->texture[0].type = 
698         theme->a_disabled_unfocused_max->texture[0].type = 
699         theme->a_hover_focused_max->texture[0].type = 
700         theme->a_hover_unfocused_max->texture[0].type = 
701         theme->a_toggled_focused_max->texture[0].type = 
702         theme->a_toggled_unfocused_max->texture[0].type = 
703         theme->a_focused_unpressed_max->texture[0].type = 
704         theme->a_focused_pressed_max->texture[0].type = 
705         theme->a_unfocused_unpressed_max->texture[0].type = 
706         theme->a_unfocused_pressed_max->texture[0].type = 
707         theme->a_disabled_focused_close->texture[0].type = 
708         theme->a_disabled_unfocused_close->texture[0].type = 
709         theme->a_hover_focused_close->texture[0].type = 
710         theme->a_hover_unfocused_close->texture[0].type = 
711         theme->a_focused_unpressed_close->texture[0].type = 
712         theme->a_focused_pressed_close->texture[0].type = 
713         theme->a_unfocused_unpressed_close->texture[0].type = 
714         theme->a_unfocused_pressed_close->texture[0].type = 
715         theme->a_disabled_focused_desk->texture[0].type = 
716         theme->a_disabled_unfocused_desk->texture[0].type = 
717         theme->a_hover_focused_desk->texture[0].type = 
718         theme->a_hover_unfocused_desk->texture[0].type = 
719         theme->a_toggled_focused_desk->texture[0].type = 
720         theme->a_toggled_unfocused_desk->texture[0].type = 
721         theme->a_focused_unpressed_desk->texture[0].type = 
722         theme->a_focused_pressed_desk->texture[0].type = 
723         theme->a_unfocused_unpressed_desk->texture[0].type = 
724         theme->a_unfocused_pressed_desk->texture[0].type = 
725         theme->a_disabled_focused_shade->texture[0].type = 
726         theme->a_disabled_unfocused_shade->texture[0].type = 
727         theme->a_hover_focused_shade->texture[0].type = 
728         theme->a_hover_unfocused_shade->texture[0].type = 
729         theme->a_toggled_focused_shade->texture[0].type = 
730         theme->a_toggled_unfocused_shade->texture[0].type = 
731         theme->a_focused_unpressed_shade->texture[0].type = 
732         theme->a_focused_pressed_shade->texture[0].type = 
733         theme->a_unfocused_unpressed_shade->texture[0].type = 
734         theme->a_unfocused_pressed_shade->texture[0].type = 
735         theme->a_disabled_focused_iconify->texture[0].type = 
736         theme->a_disabled_unfocused_iconify->texture[0].type = 
737         theme->a_hover_focused_iconify->texture[0].type = 
738         theme->a_hover_unfocused_iconify->texture[0].type = 
739         theme->a_focused_unpressed_iconify->texture[0].type = 
740         theme->a_focused_pressed_iconify->texture[0].type = 
741         theme->a_unfocused_unpressed_iconify->texture[0].type = 
742         theme->a_unfocused_pressed_iconify->texture[0].type =
743         theme->a_menu_bullet_normal->texture[0].type =
744         theme->a_menu_bullet_selected->texture[0].type = RR_TEXTURE_MASK;
745     
746     theme->a_disabled_focused_max->texture[0].data.mask.mask = 
747         theme->a_disabled_unfocused_max->texture[0].data.mask.mask = 
748         theme->max_disabled_mask;
749     theme->a_hover_focused_max->texture[0].data.mask.mask = 
750         theme->a_hover_unfocused_max->texture[0].data.mask.mask = 
751         theme->max_hover_mask;
752     theme->a_focused_pressed_max->texture[0].data.mask.mask = 
753         theme->a_unfocused_pressed_max->texture[0].data.mask.mask =
754         theme->max_pressed_mask;
755     theme->a_focused_unpressed_max->texture[0].data.mask.mask = 
756         theme->a_unfocused_unpressed_max->texture[0].data.mask.mask = 
757         theme->max_mask;
758     theme->a_toggled_focused_max->texture[0].data.mask.mask = 
759         theme->a_toggled_unfocused_max->texture[0].data.mask.mask =
760         theme->max_toggled_mask;
761     theme->a_disabled_focused_close->texture[0].data.mask.mask = 
762         theme->a_disabled_unfocused_close->texture[0].data.mask.mask = 
763         theme->close_disabled_mask;
764     theme->a_hover_focused_close->texture[0].data.mask.mask = 
765         theme->a_hover_unfocused_close->texture[0].data.mask.mask = 
766         theme->close_hover_mask;
767     theme->a_focused_pressed_close->texture[0].data.mask.mask = 
768         theme->a_unfocused_pressed_close->texture[0].data.mask.mask =
769         theme->close_pressed_mask;
770     theme->a_focused_unpressed_close->texture[0].data.mask.mask = 
771         theme->a_unfocused_unpressed_close->texture[0].data.mask.mask =
772         theme->close_mask;
773     theme->a_disabled_focused_desk->texture[0].data.mask.mask = 
774         theme->a_disabled_unfocused_desk->texture[0].data.mask.mask = 
775         theme->desk_disabled_mask;
776     theme->a_hover_focused_desk->texture[0].data.mask.mask = 
777         theme->a_hover_unfocused_desk->texture[0].data.mask.mask = 
778         theme->desk_hover_mask;
779     theme->a_focused_pressed_desk->texture[0].data.mask.mask = 
780         theme->a_unfocused_pressed_desk->texture[0].data.mask.mask =
781         theme->desk_pressed_mask;
782     theme->a_focused_unpressed_desk->texture[0].data.mask.mask = 
783         theme->a_unfocused_unpressed_desk->texture[0].data.mask.mask = 
784         theme->desk_mask;
785     theme->a_toggled_focused_desk->texture[0].data.mask.mask = 
786         theme->a_toggled_unfocused_desk->texture[0].data.mask.mask =
787         theme->desk_toggled_mask;
788     theme->a_disabled_focused_shade->texture[0].data.mask.mask = 
789         theme->a_disabled_unfocused_shade->texture[0].data.mask.mask = 
790         theme->shade_disabled_mask;
791     theme->a_hover_focused_shade->texture[0].data.mask.mask = 
792         theme->a_hover_unfocused_shade->texture[0].data.mask.mask = 
793         theme->shade_hover_mask;
794     theme->a_focused_pressed_shade->texture[0].data.mask.mask = 
795         theme->a_unfocused_pressed_shade->texture[0].data.mask.mask =
796         theme->shade_pressed_mask;
797     theme->a_focused_unpressed_shade->texture[0].data.mask.mask = 
798         theme->a_unfocused_unpressed_shade->texture[0].data.mask.mask = 
799         theme->shade_mask;
800     theme->a_toggled_focused_shade->texture[0].data.mask.mask = 
801         theme->a_toggled_unfocused_shade->texture[0].data.mask.mask =
802         theme->shade_toggled_mask;
803     theme->a_disabled_focused_iconify->texture[0].data.mask.mask = 
804         theme->a_disabled_unfocused_iconify->texture[0].data.mask.mask = 
805         theme->iconify_disabled_mask;
806     theme->a_hover_focused_iconify->texture[0].data.mask.mask = 
807         theme->a_hover_unfocused_iconify->texture[0].data.mask.mask = 
808         theme->iconify_hover_mask;
809     theme->a_focused_pressed_iconify->texture[0].data.mask.mask = 
810         theme->a_unfocused_pressed_iconify->texture[0].data.mask.mask =
811         theme->iconify_pressed_mask;
812     theme->a_focused_unpressed_iconify->texture[0].data.mask.mask = 
813         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.mask = 
814         theme->iconify_mask;
815     theme->a_menu_bullet_normal->texture[0].data.mask.mask = 
816     theme->a_menu_bullet_selected->texture[0].data.mask.mask = 
817         theme->menu_bullet_mask;
818     theme->a_disabled_focused_max->texture[0].data.mask.color = 
819         theme->a_disabled_focused_close->texture[0].data.mask.color = 
820         theme->a_disabled_focused_desk->texture[0].data.mask.color = 
821         theme->a_disabled_focused_shade->texture[0].data.mask.color = 
822         theme->a_disabled_focused_iconify->texture[0].data.mask.color = 
823         theme->titlebut_disabled_focused_color;
824     theme->a_disabled_unfocused_max->texture[0].data.mask.color = 
825         theme->a_disabled_unfocused_close->texture[0].data.mask.color = 
826         theme->a_disabled_unfocused_desk->texture[0].data.mask.color = 
827         theme->a_disabled_unfocused_shade->texture[0].data.mask.color = 
828         theme->a_disabled_unfocused_iconify->texture[0].data.mask.color = 
829         theme->titlebut_disabled_unfocused_color;
830     theme->a_hover_focused_max->texture[0].data.mask.color = 
831         theme->a_hover_focused_close->texture[0].data.mask.color = 
832         theme->a_hover_focused_desk->texture[0].data.mask.color = 
833         theme->a_hover_focused_shade->texture[0].data.mask.color = 
834         theme->a_hover_focused_iconify->texture[0].data.mask.color = 
835         theme->titlebut_hover_focused_color;
836     theme->a_hover_unfocused_max->texture[0].data.mask.color = 
837         theme->a_hover_unfocused_close->texture[0].data.mask.color = 
838         theme->a_hover_unfocused_desk->texture[0].data.mask.color = 
839         theme->a_hover_unfocused_shade->texture[0].data.mask.color = 
840         theme->a_hover_unfocused_iconify->texture[0].data.mask.color = 
841         theme->titlebut_hover_unfocused_color;
842     theme->a_toggled_focused_max->texture[0].data.mask.color = 
843         theme->a_toggled_focused_desk->texture[0].data.mask.color = 
844         theme->a_toggled_focused_shade->texture[0].data.mask.color = 
845         theme->titlebut_toggled_focused_color;
846     theme->a_toggled_unfocused_max->texture[0].data.mask.color = 
847         theme->a_toggled_unfocused_desk->texture[0].data.mask.color = 
848         theme->a_toggled_unfocused_shade->texture[0].data.mask.color = 
849         theme->titlebut_toggled_unfocused_color;
850     theme->a_focused_unpressed_max->texture[0].data.mask.color = 
851         theme->a_focused_unpressed_close->texture[0].data.mask.color = 
852         theme->a_focused_unpressed_desk->texture[0].data.mask.color = 
853         theme->a_focused_unpressed_shade->texture[0].data.mask.color = 
854         theme->a_focused_unpressed_iconify->texture[0].data.mask.color = 
855         theme->titlebut_focused_unpressed_color;
856     theme->a_focused_pressed_max->texture[0].data.mask.color = 
857         theme->a_focused_pressed_close->texture[0].data.mask.color = 
858         theme->a_focused_pressed_desk->texture[0].data.mask.color = 
859         theme->a_focused_pressed_shade->texture[0].data.mask.color = 
860         theme->a_focused_pressed_iconify->texture[0].data.mask.color =
861         theme->titlebut_focused_pressed_color;
862     theme->a_unfocused_unpressed_max->texture[0].data.mask.color = 
863         theme->a_unfocused_unpressed_close->texture[0].data.mask.color = 
864         theme->a_unfocused_unpressed_desk->texture[0].data.mask.color = 
865         theme->a_unfocused_unpressed_shade->texture[0].data.mask.color = 
866         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.color = 
867         theme->titlebut_unfocused_unpressed_color;
868         theme->a_unfocused_pressed_max->texture[0].data.mask.color = 
869         theme->a_unfocused_pressed_close->texture[0].data.mask.color = 
870         theme->a_unfocused_pressed_desk->texture[0].data.mask.color = 
871         theme->a_unfocused_pressed_shade->texture[0].data.mask.color = 
872         theme->a_unfocused_pressed_iconify->texture[0].data.mask.color =
873         theme->titlebut_unfocused_pressed_color;
874     theme->a_menu_bullet_normal->texture[0].data.mask.color = 
875         theme->menu_bullet_normal_color;
876     theme->a_menu_bullet_selected->texture[0].data.mask.color = 
877         theme->menu_bullet_selected_color;
878
879     XrmDestroyDatabase(db);
880
881     theme->label_height = theme->winfont_height;
882     theme->title_height = theme->label_height + theme->bevel * 2;
883     theme->button_size = theme->label_height - 2;
884     theme->grip_width = theme->title_height * 1.5;
885
886     return theme;
887 }
888
889 void RrThemeFree(RrTheme *theme)
890 {
891     if (theme) {
892         g_free(theme->name);
893
894         RrColorFree(theme->b_color);
895         RrColorFree(theme->cb_unfocused_color);
896         RrColorFree(theme->cb_focused_color);
897         RrColorFree(theme->title_unfocused_color);
898         RrColorFree(theme->title_focused_color);
899         RrColorFree(theme->titlebut_disabled_focused_color);
900         RrColorFree(theme->titlebut_disabled_unfocused_color);
901         RrColorFree(theme->titlebut_hover_focused_color);
902         RrColorFree(theme->titlebut_hover_unfocused_color);
903         RrColorFree(theme->titlebut_toggled_focused_color);
904         RrColorFree(theme->titlebut_toggled_unfocused_color);
905         RrColorFree(theme->titlebut_unfocused_pressed_color);
906         RrColorFree(theme->titlebut_focused_pressed_color);
907         RrColorFree(theme->titlebut_unfocused_unpressed_color);
908         RrColorFree(theme->titlebut_focused_unpressed_color);
909         RrColorFree(theme->menu_color);
910         RrColorFree(theme->menu_title_color);
911         RrColorFree(theme->menu_disabled_color);
912         RrColorFree(theme->menu_selected_color);
913         RrColorFree(theme->menu_bullet_normal_color);
914         RrColorFree(theme->menu_bullet_selected_color);
915
916         RrPixmapMaskFree(theme->max_mask);
917         RrPixmapMaskFree(theme->max_toggled_mask);
918         RrPixmapMaskFree(theme->max_disabled_mask);
919         RrPixmapMaskFree(theme->max_hover_mask);
920         RrPixmapMaskFree(theme->max_pressed_mask);
921         RrPixmapMaskFree(theme->desk_mask);
922         RrPixmapMaskFree(theme->desk_toggled_mask);
923         RrPixmapMaskFree(theme->desk_disabled_mask);
924         RrPixmapMaskFree(theme->desk_hover_mask);
925         RrPixmapMaskFree(theme->desk_pressed_mask);
926         RrPixmapMaskFree(theme->shade_mask);
927         RrPixmapMaskFree(theme->shade_toggled_mask);
928         RrPixmapMaskFree(theme->shade_disabled_mask);
929         RrPixmapMaskFree(theme->shade_hover_mask);
930         RrPixmapMaskFree(theme->shade_pressed_mask);
931         RrPixmapMaskFree(theme->iconify_mask);
932         RrPixmapMaskFree(theme->iconify_disabled_mask);
933         RrPixmapMaskFree(theme->iconify_hover_mask);
934         RrPixmapMaskFree(theme->iconify_pressed_mask);
935         RrPixmapMaskFree(theme->close_mask);
936         RrPixmapMaskFree(theme->close_disabled_mask);
937         RrPixmapMaskFree(theme->close_hover_mask);
938         RrPixmapMaskFree(theme->close_pressed_mask);
939         RrPixmapMaskFree(theme->menu_bullet_mask);
940
941         RrFontClose(theme->winfont_focused); 
942         RrFontClose(theme->winfont_unfocused);
943         RrFontClose(theme->mtitlefont);
944         RrFontClose(theme->mfont);
945
946         RrAppearanceFree(theme->a_disabled_focused_max);
947         RrAppearanceFree(theme->a_disabled_unfocused_max);
948         RrAppearanceFree(theme->a_hover_focused_max);
949         RrAppearanceFree(theme->a_hover_unfocused_max);
950         RrAppearanceFree(theme->a_toggled_focused_max);
951         RrAppearanceFree(theme->a_toggled_unfocused_max);
952         RrAppearanceFree(theme->a_focused_unpressed_max);
953         RrAppearanceFree(theme->a_focused_pressed_max);
954         RrAppearanceFree(theme->a_unfocused_unpressed_max);
955         RrAppearanceFree(theme->a_unfocused_pressed_max);
956         RrAppearanceFree(theme->a_disabled_focused_close);
957         RrAppearanceFree(theme->a_disabled_unfocused_close);
958         RrAppearanceFree(theme->a_hover_focused_close);
959         RrAppearanceFree(theme->a_hover_unfocused_close);
960         RrAppearanceFree(theme->a_focused_unpressed_close);
961         RrAppearanceFree(theme->a_focused_pressed_close);
962         RrAppearanceFree(theme->a_unfocused_unpressed_close);
963         RrAppearanceFree(theme->a_unfocused_pressed_close);
964         RrAppearanceFree(theme->a_disabled_focused_desk);
965         RrAppearanceFree(theme->a_disabled_unfocused_desk);
966         RrAppearanceFree(theme->a_hover_focused_desk);
967         RrAppearanceFree(theme->a_hover_unfocused_desk);
968         RrAppearanceFree(theme->a_toggled_focused_desk);
969         RrAppearanceFree(theme->a_toggled_unfocused_desk);
970         RrAppearanceFree(theme->a_focused_unpressed_desk);
971         RrAppearanceFree(theme->a_focused_pressed_desk);
972         RrAppearanceFree(theme->a_unfocused_unpressed_desk);
973         RrAppearanceFree(theme->a_unfocused_pressed_desk);
974         RrAppearanceFree(theme->a_disabled_focused_shade);
975         RrAppearanceFree(theme->a_disabled_unfocused_shade);
976         RrAppearanceFree(theme->a_hover_focused_shade);
977         RrAppearanceFree(theme->a_hover_unfocused_shade);
978         RrAppearanceFree(theme->a_toggled_focused_shade);
979         RrAppearanceFree(theme->a_toggled_unfocused_shade);
980         RrAppearanceFree(theme->a_focused_unpressed_shade);
981         RrAppearanceFree(theme->a_focused_pressed_shade);
982         RrAppearanceFree(theme->a_unfocused_unpressed_shade);
983         RrAppearanceFree(theme->a_unfocused_pressed_shade);
984         RrAppearanceFree(theme->a_disabled_focused_iconify);
985         RrAppearanceFree(theme->a_disabled_unfocused_iconify);
986         RrAppearanceFree(theme->a_hover_focused_iconify);
987         RrAppearanceFree(theme->a_hover_unfocused_iconify);
988         RrAppearanceFree(theme->a_focused_unpressed_iconify);
989         RrAppearanceFree(theme->a_focused_pressed_iconify);
990         RrAppearanceFree(theme->a_unfocused_unpressed_iconify);
991         RrAppearanceFree(theme->a_unfocused_pressed_iconify);
992         RrAppearanceFree(theme->a_focused_grip);
993         RrAppearanceFree(theme->a_unfocused_grip);
994         RrAppearanceFree(theme->a_focused_title);
995         RrAppearanceFree(theme->a_unfocused_title);
996         RrAppearanceFree(theme->a_focused_label);
997         RrAppearanceFree(theme->a_unfocused_label);
998         RrAppearanceFree(theme->a_icon);
999         RrAppearanceFree(theme->a_focused_handle);
1000         RrAppearanceFree(theme->a_unfocused_handle);
1001         RrAppearanceFree(theme->a_menu);
1002         RrAppearanceFree(theme->a_menu_title);
1003         RrAppearanceFree(theme->a_menu_normal);
1004         RrAppearanceFree(theme->a_menu_disabled);
1005         RrAppearanceFree(theme->a_menu_selected);
1006         RrAppearanceFree(theme->a_menu_text_normal);
1007         RrAppearanceFree(theme->a_menu_text_disabled);
1008         RrAppearanceFree(theme->a_menu_text_selected);
1009         RrAppearanceFree(theme->a_menu_bullet_normal);
1010         RrAppearanceFree(theme->a_menu_bullet_selected);
1011         RrAppearanceFree(theme->a_clear);
1012         RrAppearanceFree(theme->a_clear_tex);
1013         RrAppearanceFree(theme->app_hilite_bg);
1014         RrAppearanceFree(theme->app_unhilite_bg);
1015         RrAppearanceFree(theme->app_hilite_label);
1016         RrAppearanceFree(theme->app_unhilite_label);
1017     }
1018 }
1019
1020 static XrmDatabase loaddb(RrTheme *theme, char *name)
1021 {
1022     XrmDatabase db;
1023
1024     char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
1025                                name, "themerc", NULL);
1026     if ((db = XrmGetFileDatabase(s)))
1027         theme->path = g_path_get_dirname(s);
1028     g_free(s);
1029     if (db == NULL) {
1030         char *s = g_build_filename(THEMEDIR, name, "themerc", NULL);
1031         if ((db = XrmGetFileDatabase(s)))
1032             theme->path = g_path_get_dirname(s);
1033         g_free(s);
1034     }
1035     if (db == NULL) {
1036         char *s = g_build_filename(name, "themerc", NULL);
1037         if ((db = XrmGetFileDatabase(s)))
1038             theme->path = g_path_get_dirname(s);
1039         g_free(s);
1040     }
1041
1042     return db;
1043 }
1044
1045 static char *create_class_name(char *rname)
1046 {
1047     char *rclass = g_strdup(rname);
1048     char *p = rclass;
1049
1050     while (TRUE) {
1051         *p = toupper(*p);
1052         p = strchr(p+1, '.');
1053         if (p == NULL) break;
1054         ++p;
1055         if (*p == '\0') break;
1056     }
1057     return rclass;
1058 }
1059
1060 static gboolean read_int(XrmDatabase db, char *rname, int *value)
1061 {
1062     gboolean ret = FALSE;
1063     char *rclass = create_class_name(rname);
1064     char *rettype, *end;
1065     XrmValue retvalue;
1066   
1067     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1068         retvalue.addr != NULL) {
1069         *value = (int)strtol(retvalue.addr, &end, 10);
1070         if (end != retvalue.addr)
1071             ret = TRUE;
1072     }
1073
1074     g_free(rclass);
1075     return ret;
1076 }
1077
1078 static gboolean read_string(XrmDatabase db, char *rname, char **value)
1079 {
1080     gboolean ret = FALSE;
1081     char *rclass = create_class_name(rname);
1082     char *rettype;
1083     XrmValue retvalue;
1084   
1085     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1086         retvalue.addr != NULL) {
1087         *value = retvalue.addr;
1088         ret = TRUE;
1089     }
1090
1091     g_free(rclass);
1092     return ret;
1093 }
1094
1095 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
1096                            gchar *rname, RrColor **value)
1097 {
1098     gboolean ret = FALSE;
1099     char *rclass = create_class_name(rname);
1100     char *rettype;
1101     XrmValue retvalue;
1102   
1103     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1104         retvalue.addr != NULL) {
1105         RrColor *c = RrColorParse(inst, retvalue.addr);
1106         if (c != NULL) {
1107             *value = c;
1108             ret = TRUE;
1109         }
1110     }
1111
1112     g_free(rclass);
1113     return ret;
1114 }
1115
1116 static gboolean read_mask(const RrInstance *inst,
1117                           gchar *maskname, RrTheme *theme,
1118                           RrPixmapMask **value)
1119 {
1120     gboolean ret = FALSE;
1121     char *s;
1122     int hx, hy; /* ignored */
1123     unsigned int w, h;
1124     unsigned char *b;
1125
1126     s = g_build_filename(theme->path, maskname, NULL);
1127     if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) {
1128         ret = TRUE;
1129         *value = RrPixmapMaskNew(inst, w, h, (char*)b);
1130         XFree(b);
1131     }
1132     g_free(s);
1133
1134     return ret;
1135 }
1136
1137 static void parse_appearance(gchar *tex, RrSurfaceColorType *grad,
1138                              RrReliefType *relief, RrBevelType *bevel,
1139                              gboolean *interlaced, gboolean *border,
1140                              gboolean allow_trans)
1141 {
1142     char *t;
1143
1144     /* convert to all lowercase */
1145     for (t = tex; *t != '\0'; ++t)
1146         *t = g_ascii_tolower(*t);
1147
1148     if (allow_trans && strstr(tex, "parentrelative") != NULL) {
1149         *grad = RR_SURFACE_PARENTREL;
1150     } else {
1151         if (strstr(tex, "gradient") != NULL) {
1152             if (strstr(tex, "crossdiagonal") != NULL)
1153                 *grad = RR_SURFACE_CROSS_DIAGONAL;
1154             else if (strstr(tex, "pyramid") != NULL)
1155                 *grad = RR_SURFACE_PYRAMID;
1156             else if (strstr(tex, "horizontal") != NULL)
1157                 *grad = RR_SURFACE_HORIZONTAL;
1158             else if (strstr(tex, "vertical") != NULL)
1159                 *grad = RR_SURFACE_VERTICAL;
1160             else
1161                 *grad = RR_SURFACE_DIAGONAL;
1162         } else {
1163             *grad = RR_SURFACE_SOLID;
1164         }
1165
1166         if (strstr(tex, "sunken") != NULL)
1167             *relief = RR_RELIEF_SUNKEN;
1168         else if (strstr(tex, "flat") != NULL)
1169             *relief = RR_RELIEF_FLAT;
1170         else
1171             *relief = RR_RELIEF_RAISED;
1172         
1173         *border = FALSE;
1174         if (*relief == RR_RELIEF_FLAT) {
1175             if (strstr(tex, "border") != NULL)
1176                 *border = TRUE;
1177         } else {
1178             if (strstr(tex, "bevel2") != NULL)
1179                 *bevel = RR_BEVEL_2;
1180             else
1181                 *bevel = RR_BEVEL_1;
1182         }
1183
1184         if (strstr(tex, "interlaced") != NULL)
1185             *interlaced = TRUE;
1186         else
1187             *interlaced = FALSE;
1188     }
1189 }
1190
1191
1192 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
1193                                 gchar *rname, RrAppearance *value,
1194                                 gboolean allow_trans)
1195 {
1196     gboolean ret = FALSE;
1197     char *rclass = create_class_name(rname);
1198     char *cname, *ctoname, *bcname, *icname;
1199     char *rettype;
1200     XrmValue retvalue;
1201
1202     cname = g_strconcat(rname, ".color", NULL);
1203     ctoname = g_strconcat(rname, ".colorTo", NULL);
1204     bcname = g_strconcat(rname, ".borderColor", NULL);
1205     icname = g_strconcat(rname, ".interlaceColor", NULL);
1206
1207     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1208         retvalue.addr != NULL) {
1209         parse_appearance(retvalue.addr,
1210                          &value->surface.grad,
1211                          &value->surface.relief,
1212                          &value->surface.bevel,
1213                          &value->surface.interlaced,
1214                          &value->surface.border,
1215                          allow_trans);
1216         if (!read_color(db, inst, cname, &value->surface.primary))
1217             value->surface.primary = RrColorNew(inst, 0, 0, 0);
1218         if (!read_color(db, inst, ctoname, &value->surface.secondary))
1219             value->surface.secondary = RrColorNew(inst, 0, 0, 0);
1220         if (value->surface.border)
1221             if (!read_color(db, inst, bcname,
1222                             &value->surface.border_color))
1223                 value->surface.border_color = RrColorNew(inst, 0, 0, 0);
1224         if (value->surface.interlaced)
1225             if (!read_color(db, inst, icname,
1226                             &value->surface.interlace_color))
1227                 value->surface.interlace_color = RrColorNew(inst, 0, 0, 0);
1228         ret = TRUE;
1229     }
1230
1231     g_free(icname);
1232     g_free(bcname);
1233     g_free(ctoname);
1234     g_free(cname);
1235     g_free(rclass);
1236     return ret;
1237 }
1238
1239 static void set_default_appearance(RrAppearance *a)
1240 {
1241     a->surface.grad = RR_SURFACE_SOLID;
1242     a->surface.relief = RR_RELIEF_FLAT;
1243     a->surface.bevel = RR_BEVEL_1;
1244     a->surface.interlaced = FALSE;
1245     a->surface.border = FALSE;
1246     a->surface.primary = RrColorNew(a->inst, 0, 0, 0);
1247     a->surface.secondary = RrColorNew(a->inst, 0, 0, 0);
1248 }