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