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