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