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