]> icculus.org git repositories - dana/openbox.git/blob - render/theme.c
Some fixes and new bugs. Someone else can do the menus because it is
[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, 0x00, 0x00, 0x00);
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         unsigned char data[] =
402             { 0x18, 0x30, 0x60, 0xfe, 0xfe, 0x60, 0x30, 0x18 };
403         theme->menu_bullet_mask = RrPixmapMaskNew(inst, 8, 8,
404                                                   (gchar *)data);
405     }
406
407     /* read the decoration textures */
408     if (!read_appearance(db, inst,
409                          "window.title.focus", theme->a_focused_title,
410                          FALSE))
411         set_default_appearance(theme->a_focused_title);
412     if (!read_appearance(db, inst,
413                          "window.title.unfocus", theme->a_unfocused_title,
414                          FALSE))
415         set_default_appearance(theme->a_unfocused_title);
416     if (!read_appearance(db, inst,
417                          "window.label.focus", theme->a_focused_label,
418                          TRUE))
419         set_default_appearance(theme->a_focused_label);
420     if (!read_appearance(db, inst,
421                          "window.label.unfocus", theme->a_unfocused_label,
422                          TRUE))
423         set_default_appearance(theme->a_unfocused_label);
424     if (!read_appearance(db, inst,
425                          "window.handle.focus", theme->a_focused_handle,
426                          FALSE))
427         set_default_appearance(theme->a_focused_handle);
428     if (!read_appearance(db, inst,
429                          "window.handle.unfocus",theme->a_unfocused_handle,
430                          FALSE))
431         set_default_appearance(theme->a_unfocused_handle);
432     if (!read_appearance(db, inst,
433                          "window.grip.focus", theme->a_focused_grip,
434                          TRUE))
435         set_default_appearance(theme->a_focused_grip);
436     if (!read_appearance(db, inst,
437                          "window.grip.unfocus", theme->a_unfocused_grip,
438                          TRUE))
439         set_default_appearance(theme->a_unfocused_grip);
440     if (!read_appearance(db, inst,
441                          "menu.frame", theme->a_menu,
442                          FALSE))
443         set_default_appearance(theme->a_menu);
444     if (!read_appearance(db, inst,
445                          "menu.title", theme->a_menu_title,
446                          FALSE))
447         set_default_appearance(theme->a_menu_title);
448     if (!read_appearance(db, inst,
449                          "menu.hilite", theme->a_menu_hilite,
450                          TRUE))
451         set_default_appearance(theme->a_menu_hilite);
452
453     /* read the appearances for rendering non-decorations */
454     if (!read_appearance(db, inst,
455                          "window.title.focus", theme->app_hilite_bg,
456                          FALSE))
457         set_default_appearance(theme->app_hilite_bg);
458     if (!read_appearance(db, inst,
459                          "window.label.focus", theme->app_hilite_label,
460                          TRUE))
461         set_default_appearance(theme->app_hilite_label);
462     if (!read_appearance(db, inst,
463                          "window.title.unfocus", theme->app_unhilite_bg,
464                          FALSE))
465         set_default_appearance(theme->app_unhilite_bg);
466     if (!read_appearance(db, inst,
467                          "window.label.unfocus", theme->app_unhilite_label,
468                          TRUE))
469         set_default_appearance(theme->app_unhilite_label);
470
471     /* read buttons textures */
472     if (!read_appearance(db, inst,
473                          "window.button.disabled.focus",
474                          theme->a_disabled_focused_max,
475                          TRUE))
476         set_default_appearance(theme->a_disabled_focused_max);
477     if (!read_appearance(db, inst,
478                          "window.button.disabled.unfocus",
479                          theme->a_disabled_unfocused_max,
480                          TRUE))
481         set_default_appearance(theme->a_disabled_unfocused_max);
482     if (!read_appearance(db, inst,
483                          "window.button.pressed.focus",
484                          theme->a_focused_pressed_max,
485                          TRUE))
486         set_default_appearance(theme->a_focused_pressed_max);
487     if (!read_appearance(db, inst,
488                          "window.button.pressed.unfocus",
489                          theme->a_unfocused_pressed_max,
490                          TRUE))
491         set_default_appearance(theme->a_unfocused_pressed_max);
492     if (!read_appearance(db, inst,
493                          "window.button.toggled.focus",
494                          theme->a_toggled_focused_max,
495                          TRUE))
496         theme->a_toggled_focused_max =
497             RrAppearanceCopy(theme->a_focused_pressed_max);
498     if (!read_appearance(db, inst,
499                          "window.button.toggled.unfocus",
500                          theme->a_toggled_unfocused_max,
501                          TRUE))
502         theme->a_toggled_unfocused_max =
503             RrAppearanceCopy(theme->a_unfocused_pressed_max);
504     if (!read_appearance(db, inst,
505                          "window.button.focus",
506                          theme->a_focused_unpressed_max,
507                          TRUE))
508         set_default_appearance(theme->a_focused_unpressed_max);
509     if (!read_appearance(db, inst,
510                          "window.button.unfocus",
511                          theme->a_unfocused_unpressed_max,
512                          TRUE))
513         set_default_appearance(theme->a_unfocused_unpressed_max);
514     if (!read_appearance(db, inst,
515                          "window.button.hover.focus",
516                          theme->a_hover_focused_max,
517                          TRUE))
518         theme->a_hover_focused_max =
519             RrAppearanceCopy(theme->a_focused_unpressed_max);
520     if (!read_appearance(db, inst,
521                          "window.button.hover.unfocus",
522                          theme->a_hover_unfocused_max,
523                          TRUE))
524         theme->a_hover_unfocused_max =
525             RrAppearanceCopy(theme->a_unfocused_unpressed_max);
526
527     theme->a_disabled_focused_close =
528         RrAppearanceCopy(theme->a_disabled_focused_max);
529     theme->a_disabled_unfocused_close =
530         RrAppearanceCopy(theme->a_disabled_unfocused_max);
531     theme->a_hover_focused_close =
532         RrAppearanceCopy(theme->a_hover_focused_max);
533     theme->a_hover_unfocused_close =
534         RrAppearanceCopy(theme->a_hover_unfocused_max);
535     theme->a_unfocused_unpressed_close =
536         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
537     theme->a_unfocused_pressed_close =
538         RrAppearanceCopy(theme->a_unfocused_pressed_max);
539     theme->a_focused_unpressed_close =
540         RrAppearanceCopy(theme->a_focused_unpressed_max);
541     theme->a_focused_pressed_close =
542         RrAppearanceCopy(theme->a_focused_pressed_max);
543     theme->a_disabled_focused_desk =
544         RrAppearanceCopy(theme->a_disabled_focused_max);
545     theme->a_disabled_unfocused_desk =
546         RrAppearanceCopy(theme->a_disabled_unfocused_max);
547     theme->a_hover_focused_desk =
548         RrAppearanceCopy(theme->a_hover_focused_max);
549     theme->a_hover_unfocused_desk =
550         RrAppearanceCopy(theme->a_hover_unfocused_max); 
551     theme->a_toggled_focused_desk =
552         RrAppearanceCopy(theme->a_toggled_focused_max);
553    theme->a_toggled_unfocused_desk =
554         RrAppearanceCopy(theme->a_toggled_unfocused_max);
555     theme->a_unfocused_unpressed_desk =
556         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
557     theme->a_unfocused_pressed_desk =
558         RrAppearanceCopy(theme->a_unfocused_pressed_max);
559     theme->a_focused_unpressed_desk =
560         RrAppearanceCopy(theme->a_focused_unpressed_max);
561     theme->a_focused_pressed_desk =
562         RrAppearanceCopy(theme->a_focused_pressed_max);
563     theme->a_disabled_focused_shade =
564         RrAppearanceCopy(theme->a_disabled_focused_max);
565     theme->a_disabled_unfocused_shade =
566         RrAppearanceCopy(theme->a_disabled_unfocused_max);
567     theme->a_hover_focused_shade =
568         RrAppearanceCopy(theme->a_hover_focused_max);
569     theme->a_hover_unfocused_shade =
570         RrAppearanceCopy(theme->a_hover_unfocused_max);
571     theme->a_toggled_focused_shade =
572         RrAppearanceCopy(theme->a_toggled_focused_max);
573     theme->a_toggled_unfocused_shade =
574         RrAppearanceCopy(theme->a_toggled_unfocused_max);
575     theme->a_unfocused_unpressed_shade =
576         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
577     theme->a_unfocused_pressed_shade =
578         RrAppearanceCopy(theme->a_unfocused_pressed_max);
579     theme->a_focused_unpressed_shade =
580         RrAppearanceCopy(theme->a_focused_unpressed_max);
581     theme->a_focused_pressed_shade =
582         RrAppearanceCopy(theme->a_focused_pressed_max);
583     theme->a_disabled_focused_iconify =
584         RrAppearanceCopy(theme->a_disabled_focused_max);
585     theme->a_disabled_unfocused_iconify =
586         RrAppearanceCopy(theme->a_disabled_focused_max);
587     theme->a_hover_focused_iconify =
588         RrAppearanceCopy(theme->a_hover_focused_max);
589     theme->a_hover_unfocused_iconify =
590         RrAppearanceCopy(theme->a_hover_unfocused_max);
591     theme->a_unfocused_unpressed_iconify =
592         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
593     theme->a_unfocused_pressed_iconify =
594         RrAppearanceCopy(theme->a_unfocused_pressed_max);
595     theme->a_focused_unpressed_iconify =
596         RrAppearanceCopy(theme->a_focused_unpressed_max);
597     theme->a_focused_pressed_iconify =
598         RrAppearanceCopy(theme->a_focused_pressed_max);
599
600     theme->a_icon->surface.grad = RR_SURFACE_PARENTREL;
601     theme->a_clear->surface.grad = RR_SURFACE_PARENTREL;
602
603     /* set up the textures */
604     theme->a_focused_label->texture[0].type = 
605         theme->app_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
606     theme->a_focused_label->texture[0].data.text.justify = winjust;
607     theme->app_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
608     theme->a_focused_label->texture[0].data.text.font =
609         theme->app_hilite_label->texture[0].data.text.font = theme->winfont;
610     theme->a_focused_label->texture[0].data.text.color =
611         theme->app_hilite_label->texture[0].data.text.color =
612         theme->title_focused_color;
613
614     theme->a_unfocused_label->texture[0].type =
615         theme->app_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
616     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
617     theme->app_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
618     theme->a_unfocused_label->texture[0].data.text.font =
619         theme->app_unhilite_label->texture[0].data.text.font = theme->winfont;
620     theme->a_unfocused_label->texture[0].data.text.color =
621         theme->app_unhilite_label->texture[0].data.text.color =
622         theme->title_unfocused_color;
623
624     theme->a_menu_title->texture[0].type = RR_TEXTURE_TEXT;
625     theme->a_menu_title->texture[0].data.text.justify = mtitlejust;
626     theme->a_menu_title->texture[0].data.text.font = theme->mtitlefont;
627     theme->a_menu_title->texture[0].data.text.color = theme->menu_title_color;
628
629     theme->a_menu_item->surface.grad = 
630         theme->a_menu_disabled->surface.grad =
631         theme->a_menu_bullet->surface.grad =
632         theme->app_icon->surface.grad = RR_SURFACE_PARENTREL;
633
634     theme->a_menu_item->texture[0].type =
635         theme->a_menu_disabled->texture[0].type = 
636         theme->a_menu_hilite->texture[0].type = RR_TEXTURE_TEXT;
637     theme->a_menu_item->texture[0].data.text.justify = 
638         theme->a_menu_disabled->texture[0].data.text.justify = 
639         theme->a_menu_hilite->texture[0].data.text.justify = mjust;
640     theme->a_menu_item->texture[0].data.text.font =
641         theme->a_menu_disabled->texture[0].data.text.font =
642         theme->a_menu_hilite->texture[0].data.text.font = theme->mfont;
643     theme->a_menu_item->texture[0].data.text.color = theme->menu_color;
644     theme->a_menu_disabled->texture[0].data.text.color =
645         theme->menu_disabled_color;
646     theme->a_menu_hilite->texture[0].data.text.color =
647         theme->menu_hilite_color;
648     theme->a_menu_bullet->texture[0].data.mask.color =
649         theme->menu_bullet_color;
650
651     theme->a_disabled_focused_max->texture[0].type = 
652         theme->a_disabled_unfocused_max->texture[0].type = 
653         theme->a_hover_focused_max->texture[0].type = 
654         theme->a_hover_unfocused_max->texture[0].type = 
655         theme->a_toggled_focused_max->texture[0].type = 
656         theme->a_toggled_unfocused_max->texture[0].type = 
657         theme->a_focused_unpressed_max->texture[0].type = 
658         theme->a_focused_pressed_max->texture[0].type = 
659         theme->a_unfocused_unpressed_max->texture[0].type = 
660         theme->a_unfocused_pressed_max->texture[0].type = 
661         theme->a_disabled_focused_close->texture[0].type = 
662         theme->a_disabled_unfocused_close->texture[0].type = 
663         theme->a_hover_focused_close->texture[0].type = 
664         theme->a_hover_unfocused_close->texture[0].type = 
665         theme->a_focused_unpressed_close->texture[0].type = 
666         theme->a_focused_pressed_close->texture[0].type = 
667         theme->a_unfocused_unpressed_close->texture[0].type = 
668         theme->a_unfocused_pressed_close->texture[0].type = 
669         theme->a_disabled_focused_desk->texture[0].type = 
670         theme->a_disabled_unfocused_desk->texture[0].type = 
671         theme->a_hover_focused_desk->texture[0].type = 
672         theme->a_hover_unfocused_desk->texture[0].type = 
673         theme->a_toggled_focused_desk->texture[0].type = 
674         theme->a_toggled_unfocused_desk->texture[0].type = 
675         theme->a_focused_unpressed_desk->texture[0].type = 
676         theme->a_focused_pressed_desk->texture[0].type = 
677         theme->a_unfocused_unpressed_desk->texture[0].type = 
678         theme->a_unfocused_pressed_desk->texture[0].type = 
679         theme->a_disabled_focused_shade->texture[0].type = 
680         theme->a_disabled_unfocused_shade->texture[0].type = 
681         theme->a_hover_focused_shade->texture[0].type = 
682         theme->a_hover_unfocused_shade->texture[0].type = 
683         theme->a_toggled_focused_shade->texture[0].type = 
684         theme->a_toggled_unfocused_shade->texture[0].type = 
685         theme->a_focused_unpressed_shade->texture[0].type = 
686         theme->a_focused_pressed_shade->texture[0].type = 
687         theme->a_unfocused_unpressed_shade->texture[0].type = 
688         theme->a_unfocused_pressed_shade->texture[0].type = 
689         theme->a_disabled_focused_iconify->texture[0].type = 
690         theme->a_disabled_unfocused_iconify->texture[0].type = 
691         theme->a_hover_focused_iconify->texture[0].type = 
692         theme->a_hover_unfocused_iconify->texture[0].type = 
693         theme->a_focused_unpressed_iconify->texture[0].type = 
694         theme->a_focused_pressed_iconify->texture[0].type = 
695         theme->a_unfocused_unpressed_iconify->texture[0].type = 
696         theme->a_unfocused_pressed_iconify->texture[0].type =
697         theme->a_menu_bullet->texture[0].type = RR_TEXTURE_MASK;
698     
699     theme->a_disabled_focused_max->texture[0].data.mask.mask = 
700         theme->a_disabled_unfocused_max->texture[0].data.mask.mask = 
701         theme->max_disabled_mask;
702     theme->a_hover_focused_max->texture[0].data.mask.mask = 
703         theme->a_hover_unfocused_max->texture[0].data.mask.mask = 
704         theme->max_hover_mask;
705     theme->a_focused_pressed_max->texture[0].data.mask.mask = 
706         theme->a_unfocused_pressed_max->texture[0].data.mask.mask =
707         theme->max_pressed_mask;
708     theme->a_focused_unpressed_max->texture[0].data.mask.mask = 
709         theme->a_unfocused_unpressed_max->texture[0].data.mask.mask = 
710         theme->max_mask;
711     theme->a_toggled_focused_max->texture[0].data.mask.mask = 
712         theme->a_toggled_unfocused_max->texture[0].data.mask.mask =
713         theme->max_toggled_mask;
714     theme->a_disabled_focused_close->texture[0].data.mask.mask = 
715         theme->a_disabled_unfocused_close->texture[0].data.mask.mask = 
716         theme->close_disabled_mask;
717     theme->a_hover_focused_close->texture[0].data.mask.mask = 
718         theme->a_hover_unfocused_close->texture[0].data.mask.mask = 
719         theme->close_hover_mask;
720     theme->a_focused_pressed_close->texture[0].data.mask.mask = 
721         theme->a_unfocused_pressed_close->texture[0].data.mask.mask =
722         theme->close_pressed_mask;
723     theme->a_focused_unpressed_close->texture[0].data.mask.mask = 
724         theme->a_unfocused_unpressed_close->texture[0].data.mask.mask =
725         theme->close_mask;
726     theme->a_disabled_focused_desk->texture[0].data.mask.mask = 
727         theme->a_disabled_unfocused_desk->texture[0].data.mask.mask = 
728         theme->desk_disabled_mask;
729     theme->a_hover_focused_desk->texture[0].data.mask.mask = 
730         theme->a_hover_unfocused_desk->texture[0].data.mask.mask = 
731         theme->desk_hover_mask;
732     theme->a_focused_pressed_desk->texture[0].data.mask.mask = 
733         theme->a_unfocused_pressed_desk->texture[0].data.mask.mask =
734         theme->desk_pressed_mask;
735     theme->a_focused_unpressed_desk->texture[0].data.mask.mask = 
736         theme->a_unfocused_unpressed_desk->texture[0].data.mask.mask = 
737         theme->desk_mask;
738     theme->a_toggled_focused_desk->texture[0].data.mask.mask = 
739         theme->a_toggled_unfocused_desk->texture[0].data.mask.mask =
740         theme->desk_toggled_mask;
741     theme->a_disabled_focused_shade->texture[0].data.mask.mask = 
742         theme->a_disabled_unfocused_shade->texture[0].data.mask.mask = 
743         theme->shade_disabled_mask;
744     theme->a_hover_focused_shade->texture[0].data.mask.mask = 
745         theme->a_hover_unfocused_shade->texture[0].data.mask.mask = 
746         theme->shade_hover_mask;
747     theme->a_focused_pressed_shade->texture[0].data.mask.mask = 
748         theme->a_unfocused_pressed_shade->texture[0].data.mask.mask =
749         theme->shade_pressed_mask;
750     theme->a_focused_unpressed_shade->texture[0].data.mask.mask = 
751         theme->a_unfocused_unpressed_shade->texture[0].data.mask.mask = 
752         theme->shade_mask;
753     theme->a_toggled_focused_shade->texture[0].data.mask.mask = 
754         theme->a_toggled_unfocused_shade->texture[0].data.mask.mask =
755         theme->shade_toggled_mask;
756     theme->a_disabled_focused_iconify->texture[0].data.mask.mask = 
757         theme->a_disabled_unfocused_iconify->texture[0].data.mask.mask = 
758         theme->iconify_disabled_mask;
759     theme->a_hover_focused_iconify->texture[0].data.mask.mask = 
760         theme->a_hover_unfocused_iconify->texture[0].data.mask.mask = 
761         theme->iconify_hover_mask;
762     theme->a_focused_pressed_iconify->texture[0].data.mask.mask = 
763         theme->a_unfocused_pressed_iconify->texture[0].data.mask.mask =
764         theme->iconify_pressed_mask;
765     theme->a_focused_unpressed_iconify->texture[0].data.mask.mask = 
766         theme->a_unfocused_unpressed_iconify->texture[0].data.mask.mask = 
767         theme->iconify_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
825     XrmDestroyDatabase(db);
826
827     theme->label_height = theme->winfont_height;
828     theme->title_height = theme->label_height + theme->bevel * 2;
829     theme->button_size = theme->label_height - 2;
830     theme->grip_width = theme->button_size * 2;
831
832     return theme;
833 }
834
835 void RrThemeFree(RrTheme *theme)
836 {
837     if (theme) {
838         g_free(theme->name);
839
840         RrColorFree(theme->b_color);
841         RrColorFree(theme->cb_unfocused_color);
842         RrColorFree(theme->cb_focused_color);
843         RrColorFree(theme->title_unfocused_color);
844         RrColorFree(theme->title_focused_color);
845         RrColorFree(theme->titlebut_disabled_focused_color);
846         RrColorFree(theme->titlebut_disabled_unfocused_color);
847         RrColorFree(theme->titlebut_hover_focused_color);
848         RrColorFree(theme->titlebut_hover_unfocused_color);
849         RrColorFree(theme->titlebut_toggled_focused_color);
850         RrColorFree(theme->titlebut_toggled_unfocused_color);
851         RrColorFree(theme->titlebut_unfocused_pressed_color);
852         RrColorFree(theme->titlebut_focused_pressed_color);
853         RrColorFree(theme->titlebut_unfocused_unpressed_color);
854         RrColorFree(theme->titlebut_focused_unpressed_color);
855         RrColorFree(theme->menu_color);
856         RrColorFree(theme->menu_title_color);
857         RrColorFree(theme->menu_disabled_color);
858         RrColorFree(theme->menu_hilite_color);
859
860         RrPixmapMaskFree(theme->max_mask);
861         RrPixmapMaskFree(theme->max_toggled_mask);
862         RrPixmapMaskFree(theme->max_disabled_mask);
863         RrPixmapMaskFree(theme->max_hover_mask);
864         RrPixmapMaskFree(theme->max_pressed_mask);
865         RrPixmapMaskFree(theme->desk_mask);
866         RrPixmapMaskFree(theme->desk_toggled_mask);
867         RrPixmapMaskFree(theme->desk_disabled_mask);
868         RrPixmapMaskFree(theme->desk_hover_mask);
869         RrPixmapMaskFree(theme->desk_pressed_mask);
870         RrPixmapMaskFree(theme->shade_mask);
871         RrPixmapMaskFree(theme->shade_toggled_mask);
872         RrPixmapMaskFree(theme->shade_disabled_mask);
873         RrPixmapMaskFree(theme->shade_hover_mask);
874         RrPixmapMaskFree(theme->shade_pressed_mask);
875         RrPixmapMaskFree(theme->iconify_mask);
876         RrPixmapMaskFree(theme->iconify_disabled_mask);
877         RrPixmapMaskFree(theme->iconify_hover_mask);
878         RrPixmapMaskFree(theme->iconify_pressed_mask);
879         RrPixmapMaskFree(theme->close_mask);
880         RrPixmapMaskFree(theme->close_disabled_mask);
881         RrPixmapMaskFree(theme->close_hover_mask);
882         RrPixmapMaskFree(theme->close_pressed_mask);
883         RrPixmapMaskFree(theme->menu_bullet_mask);
884
885         RrFontClose(theme->winfont);
886         RrFontClose(theme->mtitlefont);
887         RrFontClose(theme->mfont);
888
889         RrAppearanceFree(theme->a_disabled_focused_max);
890         RrAppearanceFree(theme->a_disabled_unfocused_max);
891         RrAppearanceFree(theme->a_hover_focused_max);
892         RrAppearanceFree(theme->a_hover_unfocused_max);
893         RrAppearanceFree(theme->a_toggled_focused_max);
894         RrAppearanceFree(theme->a_toggled_unfocused_max);
895         RrAppearanceFree(theme->a_focused_unpressed_max);
896         RrAppearanceFree(theme->a_focused_pressed_max);
897         RrAppearanceFree(theme->a_unfocused_unpressed_max);
898         RrAppearanceFree(theme->a_unfocused_pressed_max);
899         RrAppearanceFree(theme->a_disabled_focused_close);
900         RrAppearanceFree(theme->a_disabled_unfocused_close);
901         RrAppearanceFree(theme->a_hover_focused_close);
902         RrAppearanceFree(theme->a_hover_unfocused_close);
903         RrAppearanceFree(theme->a_focused_unpressed_close);
904         RrAppearanceFree(theme->a_focused_pressed_close);
905         RrAppearanceFree(theme->a_unfocused_unpressed_close);
906         RrAppearanceFree(theme->a_unfocused_pressed_close);
907         RrAppearanceFree(theme->a_disabled_focused_desk);
908         RrAppearanceFree(theme->a_disabled_unfocused_desk);
909         RrAppearanceFree(theme->a_hover_focused_desk);
910         RrAppearanceFree(theme->a_hover_unfocused_desk);
911         RrAppearanceFree(theme->a_toggled_focused_desk);
912         RrAppearanceFree(theme->a_toggled_unfocused_desk);
913         RrAppearanceFree(theme->a_focused_unpressed_desk);
914         RrAppearanceFree(theme->a_focused_pressed_desk);
915         RrAppearanceFree(theme->a_unfocused_unpressed_desk);
916         RrAppearanceFree(theme->a_unfocused_pressed_desk);
917         RrAppearanceFree(theme->a_disabled_focused_shade);
918         RrAppearanceFree(theme->a_disabled_unfocused_shade);
919         RrAppearanceFree(theme->a_hover_focused_shade);
920         RrAppearanceFree(theme->a_hover_unfocused_shade);
921         RrAppearanceFree(theme->a_toggled_focused_shade);
922         RrAppearanceFree(theme->a_toggled_unfocused_shade);
923         RrAppearanceFree(theme->a_focused_unpressed_shade);
924         RrAppearanceFree(theme->a_focused_pressed_shade);
925         RrAppearanceFree(theme->a_unfocused_unpressed_shade);
926         RrAppearanceFree(theme->a_unfocused_pressed_shade);
927         RrAppearanceFree(theme->a_disabled_focused_iconify);
928         RrAppearanceFree(theme->a_disabled_unfocused_iconify);
929         RrAppearanceFree(theme->a_hover_focused_iconify);
930         RrAppearanceFree(theme->a_hover_unfocused_iconify);
931         RrAppearanceFree(theme->a_focused_unpressed_iconify);
932         RrAppearanceFree(theme->a_focused_pressed_iconify);
933         RrAppearanceFree(theme->a_unfocused_unpressed_iconify);
934         RrAppearanceFree(theme->a_unfocused_pressed_iconify);
935         RrAppearanceFree(theme->a_focused_grip);
936         RrAppearanceFree(theme->a_unfocused_grip);
937         RrAppearanceFree(theme->a_focused_title);
938         RrAppearanceFree(theme->a_unfocused_title);
939         RrAppearanceFree(theme->a_focused_label);
940         RrAppearanceFree(theme->a_unfocused_label);
941         RrAppearanceFree(theme->a_icon);
942         RrAppearanceFree(theme->a_focused_handle);
943         RrAppearanceFree(theme->a_unfocused_handle);
944         RrAppearanceFree(theme->a_menu);
945         RrAppearanceFree(theme->a_menu_title);
946         RrAppearanceFree(theme->a_menu_item);
947         RrAppearanceFree(theme->a_menu_disabled);
948         RrAppearanceFree(theme->a_menu_hilite);
949         RrAppearanceFree(theme->a_clear);
950         RrAppearanceFree(theme->app_hilite_bg);
951         RrAppearanceFree(theme->app_unhilite_bg);
952         RrAppearanceFree(theme->app_hilite_label);
953         RrAppearanceFree(theme->app_unhilite_label);
954         RrAppearanceFree(theme->app_icon);
955     }
956 }
957
958 static XrmDatabase loaddb(RrTheme *theme, char *name)
959 {
960     XrmDatabase db;
961
962     char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
963                                name, "themerc", NULL);
964     if ((db = XrmGetFileDatabase(s)))
965         theme->path = g_path_get_dirname(s);
966     g_free(s);
967     if (db == NULL) {
968         char *s = g_build_filename(THEMEDIR, name, "themerc", NULL);
969         if ((db = XrmGetFileDatabase(s)))
970             theme->path = g_path_get_dirname(s);
971         g_free(s);
972     }
973     if (db == NULL) {
974         char *s = g_build_filename(name, "themerc", NULL);
975         if ((db = XrmGetFileDatabase(s)))
976             theme->path = g_path_get_dirname(s);
977         g_free(s);
978     }
979
980     return db;
981 }
982
983 static char *create_class_name(char *rname)
984 {
985     char *rclass = g_strdup(rname);
986     char *p = rclass;
987
988     while (TRUE) {
989         *p = toupper(*p);
990         p = strchr(p+1, '.');
991         if (p == NULL) break;
992         ++p;
993         if (*p == '\0') break;
994     }
995     return rclass;
996 }
997
998 static gboolean read_int(XrmDatabase db, char *rname, int *value)
999 {
1000     gboolean ret = FALSE;
1001     char *rclass = create_class_name(rname);
1002     char *rettype, *end;
1003     XrmValue retvalue;
1004   
1005     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1006         retvalue.addr != NULL) {
1007         *value = (int)strtol(retvalue.addr, &end, 10);
1008         if (end != retvalue.addr)
1009             ret = TRUE;
1010     }
1011
1012     g_free(rclass);
1013     return ret;
1014 }
1015
1016 static gboolean read_string(XrmDatabase db, char *rname, char **value)
1017 {
1018     gboolean ret = FALSE;
1019     char *rclass = create_class_name(rname);
1020     char *rettype;
1021     XrmValue retvalue;
1022   
1023     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1024         retvalue.addr != NULL) {
1025         *value = retvalue.addr;
1026         ret = TRUE;
1027     }
1028
1029     g_free(rclass);
1030     return ret;
1031 }
1032
1033 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
1034                            gchar *rname, RrColor **value)
1035 {
1036     gboolean ret = FALSE;
1037     char *rclass = create_class_name(rname);
1038     char *rettype;
1039     XrmValue retvalue;
1040   
1041     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1042         retvalue.addr != NULL) {
1043         RrColor *c = RrColorParse(inst, retvalue.addr);
1044         if (c != NULL) {
1045             *value = c;
1046             ret = TRUE;
1047         }
1048     }
1049
1050     g_free(rclass);
1051     return ret;
1052 }
1053
1054 static gboolean read_mask(const RrInstance *inst,
1055                           gchar *maskname, RrTheme *theme,
1056                           RrPixmapMask **value)
1057 {
1058     gboolean ret = FALSE;
1059     char *s;
1060     int hx, hy; /* ignored */
1061     unsigned int w, h;
1062     unsigned char *b;
1063
1064     s = g_build_filename(theme->path, maskname, NULL);
1065     if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) {
1066         ret = TRUE;
1067         *value = RrPixmapMaskNew(inst, w, h, (char*)b);
1068         XFree(b);
1069     }
1070     g_free(s);
1071
1072     return ret;
1073 }
1074
1075 static void parse_appearance(gchar *tex, RrSurfaceColorType *grad,
1076                              RrReliefType *relief, RrBevelType *bevel,
1077                              gboolean *interlaced, gboolean *border,
1078                              gboolean allow_trans)
1079 {
1080     char *t;
1081
1082     /* convert to all lowercase */
1083     for (t = tex; *t != '\0'; ++t)
1084         *t = g_ascii_tolower(*t);
1085
1086     if (allow_trans && strstr(tex, "parentrelative") != NULL) {
1087         *grad = RR_SURFACE_PARENTREL;
1088     } else {
1089         if (strstr(tex, "gradient") != NULL) {
1090             if (strstr(tex, "crossdiagonal") != NULL)
1091                 *grad = RR_SURFACE_CROSS_DIAGONAL;
1092             else if (strstr(tex, "pyramid") != NULL)
1093                 *grad = RR_SURFACE_PYRAMID;
1094             else if (strstr(tex, "horizontal") != NULL)
1095                 *grad = RR_SURFACE_HORIZONTAL;
1096             else if (strstr(tex, "vertical") != NULL)
1097                 *grad = RR_SURFACE_VERTICAL;
1098             else
1099                 *grad = RR_SURFACE_DIAGONAL;
1100         } else {
1101             *grad = RR_SURFACE_SOLID;
1102         }
1103
1104         if (strstr(tex, "sunken") != NULL)
1105             *relief = RR_RELIEF_SUNKEN;
1106         else if (strstr(tex, "flat") != NULL)
1107             *relief = RR_RELIEF_FLAT;
1108         else
1109             *relief = RR_RELIEF_RAISED;
1110         
1111         *border = FALSE;
1112         if (*relief == RR_RELIEF_FLAT) {
1113             if (strstr(tex, "border") != NULL)
1114                 *border = TRUE;
1115         } else {
1116             if (strstr(tex, "bevel2") != NULL)
1117                 *bevel = RR_BEVEL_2;
1118             else
1119                 *bevel = RR_BEVEL_1;
1120         }
1121
1122         if (strstr(tex, "interlaced") != NULL)
1123             *interlaced = TRUE;
1124         else
1125             *interlaced = FALSE;
1126     }
1127 }
1128
1129
1130 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
1131                                 gchar *rname, RrAppearance *value,
1132                                 gboolean allow_trans)
1133 {
1134     gboolean ret = FALSE;
1135     char *rclass = create_class_name(rname), *cname, *ctoname, *bcname;
1136     char *rettype;
1137     XrmValue retvalue;
1138
1139     cname = g_strconcat(rname, ".color", NULL);
1140     ctoname = g_strconcat(rname, ".colorTo", NULL);
1141     bcname = g_strconcat(rname, ".borderColor", NULL);
1142
1143     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
1144         retvalue.addr != NULL) {
1145         parse_appearance(retvalue.addr,
1146                          &value->surface.grad,
1147                          &value->surface.relief,
1148                          &value->surface.bevel,
1149                          &value->surface.interlaced,
1150                          &value->surface.border,
1151                          allow_trans);
1152         if (!read_color(db, inst, cname, &value->surface.primary))
1153             value->surface.primary = RrColorNew(inst, 0, 0, 0);
1154         if (!read_color(db, inst, ctoname, &value->surface.secondary))
1155             value->surface.secondary = RrColorNew(inst, 0, 0, 0);
1156         if (value->surface.border)
1157             if (!read_color(db, inst, bcname,
1158                             &value->surface.border_color))
1159                 value->surface.border_color = RrColorNew(inst, 0, 0, 0);
1160         ret = TRUE;
1161     }
1162
1163     g_free(bcname);
1164     g_free(ctoname);
1165     g_free(cname);
1166     g_free(rclass);
1167     return ret;
1168 }
1169
1170 static void set_default_appearance(RrAppearance *a)
1171 {
1172     a->surface.grad = RR_SURFACE_SOLID;
1173     a->surface.relief = RR_RELIEF_FLAT;
1174     a->surface.bevel = RR_BEVEL_1;
1175     a->surface.interlaced = FALSE;
1176     a->surface.border = FALSE;
1177     a->surface.primary = RrColorNew(a->inst, 0, 0, 0);
1178     a->surface.secondary = RrColorNew(a->inst, 0, 0, 0);
1179 }