]> icculus.org git repositories - mikachu/openbox.git/blob - render/theme.c
free rgba texture caches
[mikachu/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, mjust;
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->a_disabled_focused_max = RrAppearanceNew(inst, 1);
39     theme->a_disabled_unfocused_max = RrAppearanceNew(inst, 1);
40     theme->a_hover_focused_max = RrAppearanceNew(inst, 1);
41     theme->a_hover_unfocused_max = RrAppearanceNew(inst, 1);
42     theme->a_toggled_focused_max = RrAppearanceNew(inst, 1);
43     theme->a_toggled_unfocused_max = RrAppearanceNew(inst, 1);
44     theme->a_focused_unpressed_max = RrAppearanceNew(inst, 1);
45     theme->a_focused_pressed_max = RrAppearanceNew(inst, 1);
46     theme->a_unfocused_unpressed_max = RrAppearanceNew(inst, 1);
47     theme->a_unfocused_pressed_max = RrAppearanceNew(inst, 1);
48     theme->a_focused_grip = RrAppearanceNew(inst, 0);
49     theme->a_unfocused_grip = RrAppearanceNew(inst, 0);
50     theme->a_focused_title = RrAppearanceNew(inst, 0);
51     theme->a_unfocused_title = RrAppearanceNew(inst, 0);
52     theme->a_focused_label = RrAppearanceNew(inst, 1);
53     theme->a_unfocused_label = RrAppearanceNew(inst, 1);
54     theme->a_icon = RrAppearanceNew(inst, 1);
55     theme->a_focused_handle = RrAppearanceNew(inst, 0);
56     theme->a_unfocused_handle = RrAppearanceNew(inst, 0);
57     theme->a_menu = RrAppearanceNew(inst, 0);
58     theme->a_menu_title = RrAppearanceNew(inst, 1);
59     theme->a_menu_item = RrAppearanceNew(inst, 0);
60     theme->a_menu_disabled = RrAppearanceNew(inst, 0);
61     theme->a_menu_hilite = RrAppearanceNew(inst, 0);
62     theme->a_menu_text_item = RrAppearanceNew(inst, 1);
63     theme->a_menu_text_disabled = RrAppearanceNew(inst, 1);
64     theme->a_menu_text_hilite = RrAppearanceNew(inst, 1);
65     theme->a_menu_bullet = RrAppearanceNew(inst, 1);
66     theme->a_clear = RrAppearanceNew(inst, 0);
67     theme->a_clear_tex = RrAppearanceNew(inst, 1);
68
69     theme->app_hilite_bg = RrAppearanceNew(inst, 0);
70     theme->app_unhilite_bg = RrAppearanceNew(inst, 0);
71     theme->app_hilite_label = RrAppearanceNew(inst, 1);
72     theme->app_unhilite_label = RrAppearanceNew(inst, 1);
73
74     if (name) {
75         db = loaddb(theme, name);
76         if (db == NULL) {
77             g_warning("Failed to load the theme '%s'\n"
78                       "Falling back to the default: '%s'",
79                       name, DEFAULT_THEME);
80         } else
81             theme->name = g_path_get_basename(name);
82     }
83     if (db == NULL) {
84         db = loaddb(theme, DEFAULT_THEME);
85         if (db == NULL) {
86             g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
87             return NULL;
88         } else
89             theme->name = g_path_get_basename(DEFAULT_THEME);
90     }
91
92     /* load the font stuff */
93     if (!read_string(db, "window.focus.font", &font_str))
94         font_str = "arial,sans:bold:pixelsize=10:shadow=y:shadowtint=50";
95
96     if (!(theme->winfont_focused = RrFontOpen(inst, font_str))) {
97         RrThemeFree(theme);
98         return NULL;
99     }
100     theme->winfont_height = RrFontHeight(theme->winfont_focused);
101
102     if (!read_string(db, "window.unfocus.font", &font_str))
103         /* font_str will already be set to the last one */;
104
105     if (!(theme->winfont_unfocused = RrFontOpen(inst, font_str))) {
106         RrThemeFree(theme);
107         return NULL;
108     }
109     theme->winfont_height = MAX(theme->winfont_height,
110                                 RrFontHeight(theme->winfont_unfocused));
111
112     winjust = RR_JUSTIFY_LEFT;
113     if (read_string(db, "window.justify", &str)) {
114         if (!g_ascii_strcasecmp(str, "right"))
115             winjust = RR_JUSTIFY_RIGHT;
116         else if (!g_ascii_strcasecmp(str, "center"))
117             winjust = RR_JUSTIFY_CENTER;
118     }
119
120     if (!read_string(db, "menu.title.font", &font_str))
121         font_str = "arial,sans:bold:pixelsize=12:shadow=y";
122
123     if (!(theme->mtitlefont = RrFontOpen(inst, font_str))) {
124         RrThemeFree(theme);
125         return NULL;
126     }
127     theme->mtitlefont_height = RrFontHeight(theme->mtitlefont);
128
129     mtitlejust = RR_JUSTIFY_LEFT;
130     if (read_string(db, "menu.title.justify", &str)) {
131         if (!g_ascii_strcasecmp(str, "right"))
132             mtitlejust = RR_JUSTIFY_RIGHT;
133         else if (!g_ascii_strcasecmp(str, "center"))
134             mtitlejust = RR_JUSTIFY_CENTER;
135     }
136
137     if (!read_string(db, "menu.frame.font", &font_str))
138         font_str = "arial,sans:bold:pixelsize=11:shadow=y";
139
140     if (!(theme->mfont = RrFontOpen(inst, font_str))) {
141         RrThemeFree(theme);
142         return NULL;
143     }
144     theme->mfont_height = RrFontHeight(theme->mfont);
145
146     mjust = RR_JUSTIFY_LEFT;
147     if (read_string(db, "menu.frame.justify", &str)) {
148         if (!g_ascii_strcasecmp(str, "right"))
149             mjust = RR_JUSTIFY_RIGHT;
150         else if (!g_ascii_strcasecmp(str, "center"))
151             mjust = RR_JUSTIFY_CENTER;
152     }
153
154     /* load direct dimensions */
155     if (!read_int(db, "menuOverlap", &theme->menu_overlap) ||
156         theme->menu_overlap < 0 || theme->menu_overlap > 20)
157         theme->handle_height = 0;
158     if (!read_int(db, "handleWidth", &theme->handle_height) ||
159         theme->handle_height < 0 || theme->handle_height > 100)
160         theme->handle_height = 6;
161     if (!read_int(db, "bevelWidth", &theme->bevel) ||
162         theme->bevel <= 0 || theme->bevel > 100) theme->bevel = 3;
163     if (!read_int(db, "borderWidth", &theme->bwidth) ||
164         theme->bwidth < 0 || theme->bwidth > 100) 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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.picColor",
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.frame.textColor", &theme->menu_color))
258         theme->menu_color = RrColorNew(inst, 0xff, 0xff, 0xff);
259     if (!read_color(db, inst,
260                     "menu.bullet.picColor", &theme->menu_bullet_color))
261         theme->menu_bullet_color = RrColorNew(inst, 0, 0, 0);
262     if (!read_color(db, inst,
263                     "menu.frame.disableColor", &theme->menu_disabled_color))
264         theme->menu_disabled_color = RrColorNew(inst, 0, 0, 0);
265     if (!read_color(db, inst,
266                     "menu.hilite.textColor", &theme->menu_hilite_color))
267         theme->menu_hilite_color = RrColorNew(inst, 0, 0, 0);
268
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[] = { 0x18, 0x30, 0x60, 0xfe, 0xfe, 0x60, 0x30, 0x18 };
417         theme->menu_bullet_mask = RrPixmapMaskNew(inst, 8, 8, (char*)data);
418     }
419
420     /* read the decoration textures */
421     if (!read_appearance(db, inst,
422                          "window.title.focus", theme->a_focused_title,
423                          FALSE))
424         set_default_appearance(theme->a_focused_title);
425     if (!read_appearance(db, inst,
426                          "window.title.unfocus", theme->a_unfocused_title,
427                          FALSE))
428         set_default_appearance(theme->a_unfocused_title);
429     if (!read_appearance(db, inst,
430                          "window.label.focus", theme->a_focused_label,
431                          TRUE))
432         set_default_appearance(theme->a_focused_label);
433     if (!read_appearance(db, inst,
434                          "window.label.unfocus", theme->a_unfocused_label,
435                          TRUE))
436         set_default_appearance(theme->a_unfocused_label);
437     if (!read_appearance(db, inst,
438                          "window.handle.focus", theme->a_focused_handle,
439                          FALSE))
440         set_default_appearance(theme->a_focused_handle);
441     if (!read_appearance(db, inst,
442                          "window.handle.unfocus",theme->a_unfocused_handle,
443                          FALSE))
444         set_default_appearance(theme->a_unfocused_handle);
445     if (!read_appearance(db, inst,
446                          "window.grip.focus", theme->a_focused_grip,
447                          TRUE))
448         set_default_appearance(theme->a_focused_grip);
449     if (!read_appearance(db, inst,
450                          "window.grip.unfocus", theme->a_unfocused_grip,
451                          TRUE))
452         set_default_appearance(theme->a_unfocused_grip);
453     if (!read_appearance(db, inst,
454                          "menu.frame", theme->a_menu,
455                          FALSE))
456         set_default_appearance(theme->a_menu);
457     if (!read_appearance(db, inst,
458                          "menu.title", theme->a_menu_title,
459                          FALSE))
460         set_default_appearance(theme->a_menu_title);
461     if (!read_appearance(db, inst,
462                          "menu.hilite", theme->a_menu_hilite,
463                          TRUE))
464         set_default_appearance(theme->a_menu_hilite);
465
466     /* read the appearances for rendering non-decorations */
467     if (!read_appearance(db, inst,
468                          "window.title.focus", theme->app_hilite_bg,
469                          FALSE))
470         set_default_appearance(theme->app_hilite_bg);
471     if (!read_appearance(db, inst,
472                          "window.label.focus", theme->app_hilite_label,
473                          TRUE))
474         set_default_appearance(theme->app_hilite_label);
475     if (!read_appearance(db, inst,
476                          "window.title.unfocus", theme->app_unhilite_bg,
477                          FALSE))
478         set_default_appearance(theme->app_unhilite_bg);
479     if (!read_appearance(db, inst,
480                          "window.label.unfocus", 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.button.disabled.focus",
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.button.disabled.unfocus",
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.button.pressed.focus",
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.button.pressed.unfocus",
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.button.toggled.focus",
507                          theme->a_toggled_focused_max,
508                          TRUE))
509         theme->a_toggled_focused_max =
510             RrAppearanceCopy(theme->a_focused_pressed_max);
511     if (!read_appearance(db, inst,
512                          "window.button.toggled.unfocus",
513                          theme->a_toggled_unfocused_max,
514                          TRUE))
515         theme->a_toggled_unfocused_max =
516             RrAppearanceCopy(theme->a_unfocused_pressed_max);
517     if (!read_appearance(db, inst,
518                          "window.button.focus",
519                          theme->a_focused_unpressed_max,
520                          TRUE))
521         set_default_appearance(theme->a_focused_unpressed_max);
522     if (!read_appearance(db, inst,
523                          "window.button.unfocus",
524                          theme->a_unfocused_unpressed_max,
525                          TRUE))
526         set_default_appearance(theme->a_unfocused_unpressed_max);
527     if (!read_appearance(db, inst,
528                          "window.button.hover.focus",
529                          theme->a_hover_focused_max,
530                          TRUE))
531         theme->a_hover_focused_max =
532             RrAppearanceCopy(theme->a_focused_unpressed_max);
533     if (!read_appearance(db, inst,
534                          "window.button.hover.unfocus",
535                          theme->a_hover_unfocused_max,
536                          TRUE))
537         theme->a_hover_unfocused_max =
538             RrAppearanceCopy(theme->a_unfocused_unpressed_max);
539
540     theme->a_disabled_focused_close =
541         RrAppearanceCopy(theme->a_disabled_focused_max);
542     theme->a_disabled_unfocused_close =
543         RrAppearanceCopy(theme->a_disabled_unfocused_max);
544     theme->a_hover_focused_close =
545         RrAppearanceCopy(theme->a_hover_focused_max);
546     theme->a_hover_unfocused_close =
547         RrAppearanceCopy(theme->a_hover_unfocused_max);
548     theme->a_unfocused_unpressed_close =
549         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
550     theme->a_unfocused_pressed_close =
551         RrAppearanceCopy(theme->a_unfocused_pressed_max);
552     theme->a_focused_unpressed_close =
553         RrAppearanceCopy(theme->a_focused_unpressed_max);
554     theme->a_focused_pressed_close =
555         RrAppearanceCopy(theme->a_focused_pressed_max);
556     theme->a_disabled_focused_desk =
557         RrAppearanceCopy(theme->a_disabled_focused_max);
558     theme->a_disabled_unfocused_desk =
559         RrAppearanceCopy(theme->a_disabled_unfocused_max);
560     theme->a_hover_focused_desk =
561         RrAppearanceCopy(theme->a_hover_focused_max);
562     theme->a_hover_unfocused_desk =
563         RrAppearanceCopy(theme->a_hover_unfocused_max); 
564     theme->a_toggled_focused_desk =
565         RrAppearanceCopy(theme->a_toggled_focused_max);
566    theme->a_toggled_unfocused_desk =
567         RrAppearanceCopy(theme->a_toggled_unfocused_max);
568     theme->a_unfocused_unpressed_desk =
569         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
570     theme->a_unfocused_pressed_desk =
571         RrAppearanceCopy(theme->a_unfocused_pressed_max);
572     theme->a_focused_unpressed_desk =
573         RrAppearanceCopy(theme->a_focused_unpressed_max);
574     theme->a_focused_pressed_desk =
575         RrAppearanceCopy(theme->a_focused_pressed_max);
576     theme->a_disabled_focused_shade =
577         RrAppearanceCopy(theme->a_disabled_focused_max);
578     theme->a_disabled_unfocused_shade =
579         RrAppearanceCopy(theme->a_disabled_unfocused_max);
580     theme->a_hover_focused_shade =
581         RrAppearanceCopy(theme->a_hover_focused_max);
582     theme->a_hover_unfocused_shade =
583         RrAppearanceCopy(theme->a_hover_unfocused_max);
584     theme->a_toggled_focused_shade =
585         RrAppearanceCopy(theme->a_toggled_focused_max);
586     theme->a_toggled_unfocused_shade =
587         RrAppearanceCopy(theme->a_toggled_unfocused_max);
588     theme->a_unfocused_unpressed_shade =
589         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
590     theme->a_unfocused_pressed_shade =
591         RrAppearanceCopy(theme->a_unfocused_pressed_max);
592     theme->a_focused_unpressed_shade =
593         RrAppearanceCopy(theme->a_focused_unpressed_max);
594     theme->a_focused_pressed_shade =
595         RrAppearanceCopy(theme->a_focused_pressed_max);
596     theme->a_disabled_focused_iconify =
597         RrAppearanceCopy(theme->a_disabled_focused_max);
598     theme->a_disabled_unfocused_iconify =
599         RrAppearanceCopy(theme->a_disabled_focused_max);
600     theme->a_hover_focused_iconify =
601         RrAppearanceCopy(theme->a_hover_focused_max);
602     theme->a_hover_unfocused_iconify =
603         RrAppearanceCopy(theme->a_hover_unfocused_max);
604     theme->a_unfocused_unpressed_iconify =
605         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
606     theme->a_unfocused_pressed_iconify =
607         RrAppearanceCopy(theme->a_unfocused_pressed_max);
608     theme->a_focused_unpressed_iconify =
609         RrAppearanceCopy(theme->a_focused_unpressed_max);
610     theme->a_focused_pressed_iconify =
611         RrAppearanceCopy(theme->a_focused_pressed_max);
612
613     theme->a_icon->surface.grad =
614         theme->a_clear->surface.grad =
615         theme->a_clear_tex->surface.grad =
616         theme->a_menu_item->surface.grad =
617         theme->a_menu_disabled->surface.grad =
618         theme->a_menu_text_item->surface.grad =
619         theme->a_menu_text_disabled->surface.grad =
620         theme->a_menu_text_hilite->surface.grad =
621         theme->a_menu_bullet->surface.grad = RR_SURFACE_PARENTREL;
622
623     /* set up the textures */
624     theme->a_focused_label->texture[0].type = 
625         theme->app_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
626     theme->a_focused_label->texture[0].data.text.justify = winjust;
627     theme->app_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
628     theme->a_focused_label->texture[0].data.text.font =
629         theme->app_hilite_label->texture[0].data.text.font =
630         theme->winfont_focused;
631     theme->a_focused_label->texture[0].data.text.color =
632         theme->app_hilite_label->texture[0].data.text.color =
633         theme->title_focused_color;
634
635     theme->a_unfocused_label->texture[0].type =
636         theme->app_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
637     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
638     theme->app_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
639     theme->a_unfocused_label->texture[0].data.text.font =
640         theme->app_unhilite_label->texture[0].data.text.font =
641         theme->winfont_unfocused;
642     theme->a_unfocused_label->texture[0].data.text.color =
643         theme->app_unhilite_label->texture[0].data.text.color =
644         theme->title_unfocused_color;
645
646     theme->a_menu_title->texture[0].type = RR_TEXTURE_TEXT;
647     theme->a_menu_title->texture[0].data.text.justify = mtitlejust;
648     theme->a_menu_title->texture[0].data.text.font = theme->mtitlefont;
649     theme->a_menu_title->texture[0].data.text.color = theme->menu_title_color;
650
651     theme->a_menu_text_item->texture[0].type =
652         theme->a_menu_text_disabled->texture[0].type = 
653         theme->a_menu_text_hilite->texture[0].type = RR_TEXTURE_TEXT;
654     theme->a_menu_text_item->texture[0].data.text.justify = 
655         theme->a_menu_text_disabled->texture[0].data.text.justify = 
656         theme->a_menu_text_hilite->texture[0].data.text.justify = mjust;
657     theme->a_menu_text_item->texture[0].data.text.font =
658         theme->a_menu_text_disabled->texture[0].data.text.font =
659         theme->a_menu_text_hilite->texture[0].data.text.font = theme->mfont;
660     theme->a_menu_text_item->texture[0].data.text.color = theme->menu_color;
661     theme->a_menu_text_disabled->texture[0].data.text.color =
662         theme->menu_disabled_color;
663     theme->a_menu_text_hilite->texture[0].data.text.color =
664         theme->menu_hilite_color;
665     theme->a_menu_bullet->texture[0].data.mask.color =
666         theme->menu_bullet_color;
667
668     theme->a_disabled_focused_max->texture[0].type = 
669         theme->a_disabled_unfocused_max->texture[0].type = 
670         theme->a_hover_focused_max->texture[0].type = 
671         theme->a_hover_unfocused_max->texture[0].type = 
672         theme->a_toggled_focused_max->texture[0].type = 
673         theme->a_toggled_unfocused_max->texture[0].type = 
674         theme->a_focused_unpressed_max->texture[0].type = 
675         theme->a_focused_pressed_max->texture[0].type = 
676         theme->a_unfocused_unpressed_max->texture[0].type = 
677         theme->a_unfocused_pressed_max->texture[0].type = 
678         theme->a_disabled_focused_close->texture[0].type = 
679         theme->a_disabled_unfocused_close->texture[0].type = 
680         theme->a_hover_focused_close->texture[0].type = 
681         theme->a_hover_unfocused_close->texture[0].type = 
682         theme->a_focused_unpressed_close->texture[0].type = 
683         theme->a_focused_pressed_close->texture[0].type = 
684         theme->a_unfocused_unpressed_close->texture[0].type = 
685         theme->a_unfocused_pressed_close->texture[0].type = 
686         theme->a_disabled_focused_desk->texture[0].type = 
687         theme->a_disabled_unfocused_desk->texture[0].type = 
688         theme->a_hover_focused_desk->texture[0].type = 
689         theme->a_hover_unfocused_desk->texture[0].type = 
690         theme->a_toggled_focused_desk->texture[0].type = 
691         theme->a_toggled_unfocused_desk->texture[0].type = 
692         theme->a_focused_unpressed_desk->texture[0].type = 
693         theme->a_focused_pressed_desk->texture[0].type = 
694         theme->a_unfocused_unpressed_desk->texture[0].type = 
695         theme->a_unfocused_pressed_desk->texture[0].type = 
696         theme->a_disabled_focused_shade->texture[0].type = 
697         theme->a_disabled_unfocused_shade->texture[0].type = 
698         theme->a_hover_focused_shade->texture[0].type = 
699         theme->a_hover_unfocused_shade->texture[0].type = 
700         theme->a_toggled_focused_shade->texture[0].type = 
701         theme->a_toggled_unfocused_shade->texture[0].type = 
702         theme->a_focused_unpressed_shade->texture[0].type = 
703         theme->a_focused_pressed_shade->texture[0].type = 
704         theme->a_unfocused_unpressed_shade->texture[0].type = 
705         theme->a_unfocused_pressed_shade->texture[0].type = 
706         theme->a_disabled_focused_iconify->texture[0].type = 
707         theme->a_disabled_unfocused_iconify->texture[0].type = 
708         theme->a_hover_focused_iconify->texture[0].type = 
709         theme->a_hover_unfocused_iconify->texture[0].type = 
710         theme->a_focused_unpressed_iconify->texture[0].type = 
711         theme->a_focused_pressed_iconify->texture[0].type = 
712         theme->a_unfocused_unpressed_iconify->texture[0].type = 
713         theme->a_unfocused_pressed_iconify->texture[0].type =
714         theme->a_menu_bullet->texture[0].type = RR_TEXTURE_MASK;
715     
716     theme->a_disabled_focused_max->texture[0].data.mask.mask = 
717         theme->a_disabled_unfocused_max->texture[0].data.mask.mask = 
718         theme->max_disabled_mask;
719     theme->a_hover_focused_max->texture[0].data.mask.mask = 
720         theme->a_hover_unfocused_max->texture[0].data.mask.mask = 
721         theme->max_hover_mask;
722     theme->a_focused_pressed_max->texture[0].data.mask.mask = 
723         theme->a_unfocused_pressed_max->texture[0].data.mask.mask =
724         theme->max_pressed_mask;
725     theme->a_focused_unpressed_max->texture[0].data.mask.mask = 
726         theme->a_unfocused_unpressed_max->texture[0].data.mask.mask = 
727         theme->max_mask;
728     theme->a_toggled_focused_max->texture[0].data.mask.mask = 
729         theme->a_toggled_unfocused_max->texture[0].data.mask.mask =
730         theme->max_toggled_mask;
731     theme->a_disabled_focused_close->texture[0].data.mask.mask = 
732         theme->a_disabled_unfocused_close->texture[0].data.mask.mask = 
733         theme->close_disabled_mask;
734     theme->a_hover_focused_close->texture[0].data.mask.mask = 
735         theme->a_hover_unfocused_close->texture[0].data.mask.mask = 
736         theme->close_hover_mask;
737     theme->a_focused_pressed_close->texture[0].data.mask.mask = 
738         theme->a_unfocused_pressed_close->texture[0].data.mask.mask =
739         theme->close_pressed_mask;
740     theme->a_focused_unpressed_close->texture[0].data.mask.mask = 
741         theme->a_unfocused_unpressed_close->texture[0].data.mask.mask =
742         theme->close_mask;
743     theme->a_disabled_focused_desk->texture[0].data.mask.mask = 
744         theme->a_disabled_unfocused_desk->texture[0].data.mask.mask = 
745         theme->desk_disabled_mask;
746     theme->a_hover_focused_desk->texture[0].data.mask.mask = 
747         theme->a_hover_unfocused_desk->texture[0].data.mask.mask = 
748         theme->desk_hover_mask;
749     theme->a_focused_pressed_desk->texture[0].data.mask.mask = 
750         theme->a_unfocused_pressed_desk->texture[0].data.mask.mask =
751         theme->desk_pressed_mask;
752     theme->a_focused_unpressed_desk->texture[0].data.mask.mask = 
753         theme->a_unfocused_unpressed_desk->texture[0].data.mask.mask = 
754         theme->desk_mask;
755     theme->a_toggled_focused_desk->texture[0].data.mask.mask = 
756         theme->a_toggled_unfocused_desk->texture[0].data.mask.mask =
757         theme->desk_toggled_mask;
758     theme->a_disabled_focused_shade->texture[0].data.mask.mask = 
759         theme->a_disabled_unfocused_shade->texture[0].data.mask.mask = 
760         theme->shade_disabled_mask;
761     theme->a_hover_focused_shade->texture[0].data.mask.mask = 
762         theme->a_hover_unfocused_shade->texture[0].data.mask.mask = 
763         theme->shade_hover_mask;
764     theme->a_focused_pressed_shade->texture[0].data.mask.mask = 
765         theme->a_unfocused_pressed_shade->texture[0].data.mask.mask =
766         theme->shade_pressed_mask;
767     theme->a_focused_unpressed_shade->texture[0].data.mask.mask = 
768         theme->a_unfocused_unpressed_shade->texture[0].data.mask.mask = 
769         theme->shade_mask;
770     theme->a_toggled_focused_shade->texture[0].data.mask.mask = 
771         theme->a_toggled_unfocused_shade->texture[0].data.mask.mask =
772         theme->shade_toggled_mask;
773     theme->a_disabled_focused_iconify->texture[0].data.mask.mask = 
774         theme->a_disabled_unfocused_iconify->texture[0].data.mask.mask = 
775         theme->iconify_disabled_mask;
776     theme->a_hover_focused_iconify->texture[0].data.mask.mask = 
777         theme->a_hover_unfocused_iconify->texture[0].data.mask.mask = 
778         theme->iconify_hover_mask;
779     theme->a_focused_pressed_iconify->texture[0].data.mask.mask = 
780         theme->a_unfocused_pressed_iconify->texture[0].data.mask.mask =
781         theme->iconify_pressed_mask;
782     theme->a_focused_unpressed_iconify->texture[0].data.mask.mask = 
783         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.mask = 
784         theme->iconify_mask;
785     theme->a_menu_bullet->texture[0].data.mask.mask = 
786         theme->menu_bullet_mask;
787     theme->a_disabled_focused_max->texture[0].data.mask.color = 
788         theme->a_disabled_focused_close->texture[0].data.mask.color = 
789         theme->a_disabled_focused_desk->texture[0].data.mask.color = 
790         theme->a_disabled_focused_shade->texture[0].data.mask.color = 
791         theme->a_disabled_focused_iconify->texture[0].data.mask.color = 
792         theme->titlebut_disabled_focused_color;
793     theme->a_disabled_unfocused_max->texture[0].data.mask.color = 
794         theme->a_disabled_unfocused_close->texture[0].data.mask.color = 
795         theme->a_disabled_unfocused_desk->texture[0].data.mask.color = 
796         theme->a_disabled_unfocused_shade->texture[0].data.mask.color = 
797         theme->a_disabled_unfocused_iconify->texture[0].data.mask.color = 
798         theme->titlebut_disabled_unfocused_color;
799     theme->a_hover_focused_max->texture[0].data.mask.color = 
800         theme->a_hover_focused_close->texture[0].data.mask.color = 
801         theme->a_hover_focused_desk->texture[0].data.mask.color = 
802         theme->a_hover_focused_shade->texture[0].data.mask.color = 
803         theme->a_hover_focused_iconify->texture[0].data.mask.color = 
804         theme->titlebut_hover_focused_color;
805     theme->a_hover_unfocused_max->texture[0].data.mask.color = 
806         theme->a_hover_unfocused_close->texture[0].data.mask.color = 
807         theme->a_hover_unfocused_desk->texture[0].data.mask.color = 
808         theme->a_hover_unfocused_shade->texture[0].data.mask.color = 
809         theme->a_hover_unfocused_iconify->texture[0].data.mask.color = 
810         theme->titlebut_hover_unfocused_color;
811     theme->a_toggled_focused_max->texture[0].data.mask.color = 
812         theme->a_toggled_focused_desk->texture[0].data.mask.color = 
813         theme->a_toggled_focused_shade->texture[0].data.mask.color = 
814         theme->titlebut_toggled_focused_color;
815     theme->a_toggled_unfocused_max->texture[0].data.mask.color = 
816         theme->a_toggled_unfocused_desk->texture[0].data.mask.color = 
817         theme->a_toggled_unfocused_shade->texture[0].data.mask.color = 
818         theme->titlebut_toggled_unfocused_color;
819     theme->a_focused_unpressed_max->texture[0].data.mask.color = 
820         theme->a_focused_unpressed_close->texture[0].data.mask.color = 
821         theme->a_focused_unpressed_desk->texture[0].data.mask.color = 
822         theme->a_focused_unpressed_shade->texture[0].data.mask.color = 
823         theme->a_focused_unpressed_iconify->texture[0].data.mask.color = 
824         theme->titlebut_focused_unpressed_color;
825     theme->a_focused_pressed_max->texture[0].data.mask.color = 
826         theme->a_focused_pressed_close->texture[0].data.mask.color = 
827         theme->a_focused_pressed_desk->texture[0].data.mask.color = 
828         theme->a_focused_pressed_shade->texture[0].data.mask.color = 
829         theme->a_focused_pressed_iconify->texture[0].data.mask.color =
830         theme->titlebut_focused_pressed_color;
831     theme->a_unfocused_unpressed_max->texture[0].data.mask.color = 
832         theme->a_unfocused_unpressed_close->texture[0].data.mask.color = 
833         theme->a_unfocused_unpressed_desk->texture[0].data.mask.color = 
834         theme->a_unfocused_unpressed_shade->texture[0].data.mask.color = 
835         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.color = 
836         theme->titlebut_unfocused_unpressed_color;
837         theme->a_unfocused_pressed_max->texture[0].data.mask.color = 
838         theme->a_unfocused_pressed_close->texture[0].data.mask.color = 
839         theme->a_unfocused_pressed_desk->texture[0].data.mask.color = 
840         theme->a_unfocused_pressed_shade->texture[0].data.mask.color = 
841         theme->a_unfocused_pressed_iconify->texture[0].data.mask.color =
842         theme->titlebut_unfocused_pressed_color;
843     theme->a_menu_bullet->texture[0].data.mask.color = 
844         theme->menu_bullet_color;
845
846     XrmDestroyDatabase(db);
847
848     theme->label_height = theme->winfont_height;
849     theme->title_height = theme->label_height + theme->bevel * 2;
850     theme->button_size = theme->label_height - 2;
851     theme->grip_width = theme->title_height * 1.5;
852
853     return theme;
854 }
855
856 void RrThemeFree(RrTheme *theme)
857 {
858     if (theme) {
859         g_free(theme->name);
860
861         RrColorFree(theme->b_color);
862         RrColorFree(theme->cb_unfocused_color);
863         RrColorFree(theme->cb_focused_color);
864         RrColorFree(theme->title_unfocused_color);
865         RrColorFree(theme->title_focused_color);
866         RrColorFree(theme->titlebut_disabled_focused_color);
867         RrColorFree(theme->titlebut_disabled_unfocused_color);
868         RrColorFree(theme->titlebut_hover_focused_color);
869         RrColorFree(theme->titlebut_hover_unfocused_color);
870         RrColorFree(theme->titlebut_toggled_focused_color);
871         RrColorFree(theme->titlebut_toggled_unfocused_color);
872         RrColorFree(theme->titlebut_unfocused_pressed_color);
873         RrColorFree(theme->titlebut_focused_pressed_color);
874         RrColorFree(theme->titlebut_unfocused_unpressed_color);
875         RrColorFree(theme->titlebut_focused_unpressed_color);
876         RrColorFree(theme->menu_color);
877         RrColorFree(theme->menu_title_color);
878         RrColorFree(theme->menu_disabled_color);
879         RrColorFree(theme->menu_hilite_color);
880
881         RrPixmapMaskFree(theme->max_mask);
882         RrPixmapMaskFree(theme->max_toggled_mask);
883         RrPixmapMaskFree(theme->max_disabled_mask);
884         RrPixmapMaskFree(theme->max_hover_mask);
885         RrPixmapMaskFree(theme->max_pressed_mask);
886         RrPixmapMaskFree(theme->desk_mask);
887         RrPixmapMaskFree(theme->desk_toggled_mask);
888         RrPixmapMaskFree(theme->desk_disabled_mask);
889         RrPixmapMaskFree(theme->desk_hover_mask);
890         RrPixmapMaskFree(theme->desk_pressed_mask);
891         RrPixmapMaskFree(theme->shade_mask);
892         RrPixmapMaskFree(theme->shade_toggled_mask);
893         RrPixmapMaskFree(theme->shade_disabled_mask);
894         RrPixmapMaskFree(theme->shade_hover_mask);
895         RrPixmapMaskFree(theme->shade_pressed_mask);
896         RrPixmapMaskFree(theme->iconify_mask);
897         RrPixmapMaskFree(theme->iconify_disabled_mask);
898         RrPixmapMaskFree(theme->iconify_hover_mask);
899         RrPixmapMaskFree(theme->iconify_pressed_mask);
900         RrPixmapMaskFree(theme->close_mask);
901         RrPixmapMaskFree(theme->close_disabled_mask);
902         RrPixmapMaskFree(theme->close_hover_mask);
903         RrPixmapMaskFree(theme->close_pressed_mask);
904         RrPixmapMaskFree(theme->menu_bullet_mask);
905
906         RrFontClose(theme->winfont_focused); 
907         RrFontClose(theme->winfont_unfocused);
908         RrFontClose(theme->mtitlefont);
909         RrFontClose(theme->mfont);
910
911         RrAppearanceFree(theme->a_disabled_focused_max);
912         RrAppearanceFree(theme->a_disabled_unfocused_max);
913         RrAppearanceFree(theme->a_hover_focused_max);
914         RrAppearanceFree(theme->a_hover_unfocused_max);
915         RrAppearanceFree(theme->a_toggled_focused_max);
916         RrAppearanceFree(theme->a_toggled_unfocused_max);
917         RrAppearanceFree(theme->a_focused_unpressed_max);
918         RrAppearanceFree(theme->a_focused_pressed_max);
919         RrAppearanceFree(theme->a_unfocused_unpressed_max);
920         RrAppearanceFree(theme->a_unfocused_pressed_max);
921         RrAppearanceFree(theme->a_disabled_focused_close);
922         RrAppearanceFree(theme->a_disabled_unfocused_close);
923         RrAppearanceFree(theme->a_hover_focused_close);
924         RrAppearanceFree(theme->a_hover_unfocused_close);
925         RrAppearanceFree(theme->a_focused_unpressed_close);
926         RrAppearanceFree(theme->a_focused_pressed_close);
927         RrAppearanceFree(theme->a_unfocused_unpressed_close);
928         RrAppearanceFree(theme->a_unfocused_pressed_close);
929         RrAppearanceFree(theme->a_disabled_focused_desk);
930         RrAppearanceFree(theme->a_disabled_unfocused_desk);
931         RrAppearanceFree(theme->a_hover_focused_desk);
932         RrAppearanceFree(theme->a_hover_unfocused_desk);
933         RrAppearanceFree(theme->a_toggled_focused_desk);
934         RrAppearanceFree(theme->a_toggled_unfocused_desk);
935         RrAppearanceFree(theme->a_focused_unpressed_desk);
936         RrAppearanceFree(theme->a_focused_pressed_desk);
937         RrAppearanceFree(theme->a_unfocused_unpressed_desk);
938         RrAppearanceFree(theme->a_unfocused_pressed_desk);
939         RrAppearanceFree(theme->a_disabled_focused_shade);
940         RrAppearanceFree(theme->a_disabled_unfocused_shade);
941         RrAppearanceFree(theme->a_hover_focused_shade);
942         RrAppearanceFree(theme->a_hover_unfocused_shade);
943         RrAppearanceFree(theme->a_toggled_focused_shade);
944         RrAppearanceFree(theme->a_toggled_unfocused_shade);
945         RrAppearanceFree(theme->a_focused_unpressed_shade);
946         RrAppearanceFree(theme->a_focused_pressed_shade);
947         RrAppearanceFree(theme->a_unfocused_unpressed_shade);
948         RrAppearanceFree(theme->a_unfocused_pressed_shade);
949         RrAppearanceFree(theme->a_disabled_focused_iconify);
950         RrAppearanceFree(theme->a_disabled_unfocused_iconify);
951         RrAppearanceFree(theme->a_hover_focused_iconify);
952         RrAppearanceFree(theme->a_hover_unfocused_iconify);
953         RrAppearanceFree(theme->a_focused_unpressed_iconify);
954         RrAppearanceFree(theme->a_focused_pressed_iconify);
955         RrAppearanceFree(theme->a_unfocused_unpressed_iconify);
956         RrAppearanceFree(theme->a_unfocused_pressed_iconify);
957         RrAppearanceFree(theme->a_focused_grip);
958         RrAppearanceFree(theme->a_unfocused_grip);
959         RrAppearanceFree(theme->a_focused_title);
960         RrAppearanceFree(theme->a_unfocused_title);
961         RrAppearanceFree(theme->a_focused_label);
962         RrAppearanceFree(theme->a_unfocused_label);
963         RrAppearanceFree(theme->a_icon);
964         RrAppearanceFree(theme->a_focused_handle);
965         RrAppearanceFree(theme->a_unfocused_handle);
966         RrAppearanceFree(theme->a_menu);
967         RrAppearanceFree(theme->a_menu_title);
968         RrAppearanceFree(theme->a_menu_item);
969         RrAppearanceFree(theme->a_menu_disabled);
970         RrAppearanceFree(theme->a_menu_hilite);
971         RrAppearanceFree(theme->a_menu_text_item);
972         RrAppearanceFree(theme->a_menu_text_disabled);
973         RrAppearanceFree(theme->a_menu_text_hilite);
974         RrAppearanceFree(theme->a_clear);
975         RrAppearanceFree(theme->a_clear_tex);
976         RrAppearanceFree(theme->app_hilite_bg);
977         RrAppearanceFree(theme->app_unhilite_bg);
978         RrAppearanceFree(theme->app_hilite_label);
979         RrAppearanceFree(theme->app_unhilite_label);
980     }
981 }
982
983 static XrmDatabase loaddb(RrTheme *theme, char *name)
984 {
985     XrmDatabase db;
986
987     char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
988                                name, "themerc", NULL);
989     if ((db = XrmGetFileDatabase(s)))
990         theme->path = g_path_get_dirname(s);
991     g_free(s);
992     if (db == NULL) {
993         char *s = g_build_filename(THEMEDIR, name, "themerc", NULL);
994         if ((db = XrmGetFileDatabase(s)))
995             theme->path = g_path_get_dirname(s);
996         g_free(s);
997     }
998     if (db == NULL) {
999         char *s = g_build_filename(name, "themerc", NULL);
1000         if ((db = XrmGetFileDatabase(s)))
1001             theme->path = g_path_get_dirname(s);
1002         g_free(s);
1003     }
1004
1005     return db;
1006 }
1007
1008 static char *create_class_name(char *rname)
1009 {
1010     char *rclass = g_strdup(rname);
1011     char *p = rclass;
1012
1013     while (TRUE) {
1014         *p = toupper(*p);
1015         p = strchr(p+1, '.');
1016         if (p == NULL) break;
1017         ++p;
1018         if (*p == '\0') break;
1019     }
1020     return rclass;
1021 }
1022
1023 static gboolean read_int(XrmDatabase db, char *rname, int *value)
1024 {
1025     gboolean ret = FALSE;
1026     char *rclass = create_class_name(rname);
1027     char *rettype, *end;
1028     XrmValue retvalue;
1029   
1030     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1031         retvalue.addr != NULL) {
1032         *value = (int)strtol(retvalue.addr, &end, 10);
1033         if (end != retvalue.addr)
1034             ret = TRUE;
1035     }
1036
1037     g_free(rclass);
1038     return ret;
1039 }
1040
1041 static gboolean read_string(XrmDatabase db, char *rname, char **value)
1042 {
1043     gboolean ret = FALSE;
1044     char *rclass = create_class_name(rname);
1045     char *rettype;
1046     XrmValue retvalue;
1047   
1048     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1049         retvalue.addr != NULL) {
1050         *value = retvalue.addr;
1051         ret = TRUE;
1052     }
1053
1054     g_free(rclass);
1055     return ret;
1056 }
1057
1058 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
1059                            gchar *rname, RrColor **value)
1060 {
1061     gboolean ret = FALSE;
1062     char *rclass = create_class_name(rname);
1063     char *rettype;
1064     XrmValue retvalue;
1065   
1066     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1067         retvalue.addr != NULL) {
1068         RrColor *c = RrColorParse(inst, retvalue.addr);
1069         if (c != NULL) {
1070             *value = c;
1071             ret = TRUE;
1072         }
1073     }
1074
1075     g_free(rclass);
1076     return ret;
1077 }
1078
1079 static gboolean read_mask(const RrInstance *inst,
1080                           gchar *maskname, RrTheme *theme,
1081                           RrPixmapMask **value)
1082 {
1083     gboolean ret = FALSE;
1084     char *s;
1085     int hx, hy; /* ignored */
1086     unsigned int w, h;
1087     unsigned char *b;
1088
1089     s = g_build_filename(theme->path, maskname, NULL);
1090     if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) {
1091         ret = TRUE;
1092         *value = RrPixmapMaskNew(inst, w, h, (char*)b);
1093         XFree(b);
1094     }
1095     g_free(s);
1096
1097     return ret;
1098 }
1099
1100 static void parse_appearance(gchar *tex, RrSurfaceColorType *grad,
1101                              RrReliefType *relief, RrBevelType *bevel,
1102                              gboolean *interlaced, gboolean *border,
1103                              gboolean allow_trans)
1104 {
1105     char *t;
1106
1107     /* convert to all lowercase */
1108     for (t = tex; *t != '\0'; ++t)
1109         *t = g_ascii_tolower(*t);
1110
1111     if (allow_trans && strstr(tex, "parentrelative") != NULL) {
1112         *grad = RR_SURFACE_PARENTREL;
1113     } else {
1114         if (strstr(tex, "gradient") != NULL) {
1115             if (strstr(tex, "crossdiagonal") != NULL)
1116                 *grad = RR_SURFACE_CROSS_DIAGONAL;
1117             else if (strstr(tex, "pyramid") != NULL)
1118                 *grad = RR_SURFACE_PYRAMID;
1119             else if (strstr(tex, "horizontal") != NULL)
1120                 *grad = RR_SURFACE_HORIZONTAL;
1121             else if (strstr(tex, "vertical") != NULL)
1122                 *grad = RR_SURFACE_VERTICAL;
1123             else
1124                 *grad = RR_SURFACE_DIAGONAL;
1125         } else {
1126             *grad = RR_SURFACE_SOLID;
1127         }
1128
1129         if (strstr(tex, "sunken") != NULL)
1130             *relief = RR_RELIEF_SUNKEN;
1131         else if (strstr(tex, "flat") != NULL)
1132             *relief = RR_RELIEF_FLAT;
1133         else
1134             *relief = RR_RELIEF_RAISED;
1135         
1136         *border = FALSE;
1137         if (*relief == RR_RELIEF_FLAT) {
1138             if (strstr(tex, "border") != NULL)
1139                 *border = TRUE;
1140         } else {
1141             if (strstr(tex, "bevel2") != NULL)
1142                 *bevel = RR_BEVEL_2;
1143             else
1144                 *bevel = RR_BEVEL_1;
1145         }
1146
1147         if (strstr(tex, "interlaced") != NULL)
1148             *interlaced = TRUE;
1149         else
1150             *interlaced = FALSE;
1151     }
1152 }
1153
1154
1155 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
1156                                 gchar *rname, RrAppearance *value,
1157                                 gboolean allow_trans)
1158 {
1159     gboolean ret = FALSE;
1160     char *rclass = create_class_name(rname), *cname, *ctoname, *bcname;
1161     char *rettype;
1162     XrmValue retvalue;
1163
1164     cname = g_strconcat(rname, ".color", NULL);
1165     ctoname = g_strconcat(rname, ".colorTo", NULL);
1166     bcname = g_strconcat(rname, ".borderColor", NULL);
1167
1168     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1169         retvalue.addr != NULL) {
1170         parse_appearance(retvalue.addr,
1171                          &value->surface.grad,
1172                          &value->surface.relief,
1173                          &value->surface.bevel,
1174                          &value->surface.interlaced,
1175                          &value->surface.border,
1176                          allow_trans);
1177         if (!read_color(db, inst, cname, &value->surface.primary))
1178             value->surface.primary = RrColorNew(inst, 0, 0, 0);
1179         if (!read_color(db, inst, ctoname, &value->surface.secondary))
1180             value->surface.secondary = RrColorNew(inst, 0, 0, 0);
1181         if (value->surface.border)
1182             if (!read_color(db, inst, bcname,
1183                             &value->surface.border_color))
1184                 value->surface.border_color = RrColorNew(inst, 0, 0, 0);
1185         ret = TRUE;
1186     }
1187
1188     g_free(bcname);
1189     g_free(ctoname);
1190     g_free(cname);
1191     g_free(rclass);
1192     return ret;
1193 }
1194
1195 static void set_default_appearance(RrAppearance *a)
1196 {
1197     a->surface.grad = RR_SURFACE_SOLID;
1198     a->surface.relief = RR_RELIEF_FLAT;
1199     a->surface.bevel = RR_BEVEL_1;
1200     a->surface.interlaced = FALSE;
1201     a->surface.border = FALSE;
1202     a->surface.primary = RrColorNew(a->inst, 0, 0, 0);
1203     a->surface.secondary = RrColorNew(a->inst, 0, 0, 0);
1204 }