fix a bunch of memleaks from valgrind and stuff
[dana/openbox.git] / render / theme.c
1 #include "render.h"
2 #include "color.h"
3 #include "font.h"
4 #include "mask.h"
5 #include "theme.h"
6
7 #include <X11/Xlib.h>
8 #include <X11/Xresource.h>
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 static XrmDatabase loaddb(RrTheme *theme, char *name);
14 static gboolean read_int(XrmDatabase db, char *rname, int *value);
15 static gboolean read_string(XrmDatabase db, char *rname, char **value);
16 static gboolean read_color(XrmDatabase db, const RrInstance *inst,
17                            gchar *rname, RrColor **value);
18 static gboolean read_mask(const RrInstance *inst,
19                           gchar *maskname, RrTheme *theme,
20                           RrPixmapMask **value);
21 static gboolean read_appearance(XrmDatabase db, const RrInstance *inst,
22                                 gchar *rname, RrAppearance *value,
23                                 gboolean allow_trans);
24 static void set_default_appearance(RrAppearance *a);
25
26 RrTheme* RrThemeNew(const RrInstance *inst, gchar *name)
27 {
28     XrmDatabase db = NULL;
29     RrJustify winjust, mtitlejust, mjust;
30     gchar *str;
31     gchar *font_str;
32     RrTheme *theme;
33
34     theme = g_new0(RrTheme, 1);
35
36     theme->inst = inst;
37
38     theme->a_disabled_focused_max = RrAppearanceNew(inst, 1);
39     theme->a_disabled_unfocused_max = RrAppearanceNew(inst, 1);
40     theme->a_hover_focused_max = RrAppearanceNew(inst, 1);
41     theme->a_hover_unfocused_max = RrAppearanceNew(inst, 1);
42     theme->a_toggled_focused_max = RrAppearanceNew(inst, 1);
43     theme->a_toggled_unfocused_max = RrAppearanceNew(inst, 1);
44     theme->a_focused_unpressed_max = RrAppearanceNew(inst, 1);
45     theme->a_focused_pressed_max = RrAppearanceNew(inst, 1);
46     theme->a_unfocused_unpressed_max = RrAppearanceNew(inst, 1);
47     theme->a_unfocused_pressed_max = RrAppearanceNew(inst, 1);
48     theme->a_focused_grip = RrAppearanceNew(inst, 0);
49     theme->a_unfocused_grip = RrAppearanceNew(inst, 0);
50     theme->a_focused_title = RrAppearanceNew(inst, 0);
51     theme->a_unfocused_title = RrAppearanceNew(inst, 0);
52     theme->a_focused_label = RrAppearanceNew(inst, 1);
53     theme->a_unfocused_label = RrAppearanceNew(inst, 1);
54     theme->a_icon = RrAppearanceNew(inst, 1);
55     theme->a_focused_handle = RrAppearanceNew(inst, 0);
56     theme->a_unfocused_handle = RrAppearanceNew(inst, 0);
57     theme->a_menu = RrAppearanceNew(inst, 0);
58     theme->a_menu_title = RrAppearanceNew(inst, 1);
59     theme->a_menu_item = RrAppearanceNew(inst, 0);
60     theme->a_menu_disabled = RrAppearanceNew(inst, 0);
61     theme->a_menu_hilite = RrAppearanceNew(inst, 0);
62     theme->a_menu_text_item = RrAppearanceNew(inst, 1);
63     theme->a_menu_text_disabled = RrAppearanceNew(inst, 1);
64     theme->a_menu_text_hilite = RrAppearanceNew(inst, 1);
65     theme->a_menu_bullet = RrAppearanceNew(inst, 1);
66     theme->a_clear = RrAppearanceNew(inst, 0);
67     theme->a_clear_tex = RrAppearanceNew(inst, 1);
68
69     theme->app_hilite_bg = RrAppearanceNew(inst, 0);
70     theme->app_unhilite_bg = RrAppearanceNew(inst, 0);
71     theme->app_hilite_label = RrAppearanceNew(inst, 1);
72     theme->app_unhilite_label = RrAppearanceNew(inst, 1);
73
74     if (name) {
75         db = loaddb(theme, name);
76         if (db == NULL) {
77             g_warning("Failed to load the theme '%s'\n"
78                       "Falling back to the default: '%s'",
79                       name, DEFAULT_THEME);
80         } else
81             theme->name = g_path_get_basename(name);
82     }
83     if (db == NULL) {
84         db = loaddb(theme, DEFAULT_THEME);
85         if (db == NULL) {
86             g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
87             return NULL;
88         } else
89             theme->name = g_path_get_basename(DEFAULT_THEME);
90     }
91
92     /* load the font stuff */
93     if (!read_string(db, "window.focus.font", &font_str))
94         font_str = "arial,sans:bold:pixelsize=10:shadow=y:shadowtint=50";
95
96     if (!(theme->winfont_focused = RrFontOpen(inst, font_str))) {
97         RrThemeFree(theme);
98         return NULL;
99     }
100     theme->winfont_height = RrFontHeight(theme->winfont_focused);
101
102     if (!read_string(db, "window.unfocus.font", &font_str))
103         /* font_str will already be set to the last one */;
104
105     if (!(theme->winfont_unfocused = RrFontOpen(inst, font_str))) {
106         RrThemeFree(theme);
107         return NULL;
108     }
109     theme->winfont_height = MAX(theme->winfont_height,
110                                 RrFontHeight(theme->winfont_unfocused));
111
112     winjust = RR_JUSTIFY_LEFT;
113     if (read_string(db, "window.justify", &str)) {
114         if (!g_ascii_strcasecmp(str, "right"))
115             winjust = RR_JUSTIFY_RIGHT;
116         else if (!g_ascii_strcasecmp(str, "center"))
117             winjust = RR_JUSTIFY_CENTER;
118     }
119
120     if (!read_string(db, "menu.title.font", &font_str))
121         font_str = "arial,sans:bold:pixelsize=12:shadow=y";
122
123     if (!(theme->mtitlefont = RrFontOpen(inst, font_str))) {
124         RrThemeFree(theme);
125         return NULL;
126     }
127     theme->mtitlefont_height = RrFontHeight(theme->mtitlefont);
128
129     mtitlejust = RR_JUSTIFY_LEFT;
130     if (read_string(db, "menu.title.justify", &str)) {
131         if (!g_ascii_strcasecmp(str, "right"))
132             mtitlejust = RR_JUSTIFY_RIGHT;
133         else if (!g_ascii_strcasecmp(str, "center"))
134             mtitlejust = RR_JUSTIFY_CENTER;
135     }
136
137     if (!read_string(db, "menu.frame.font", &font_str))
138         font_str = "arial,sans:bold:pixelsize=11:shadow=y";
139
140     if (!(theme->mfont = RrFontOpen(inst, font_str))) {
141         RrThemeFree(theme);
142         return NULL;
143     }
144     theme->mfont_height = RrFontHeight(theme->mfont);
145
146     mjust = RR_JUSTIFY_LEFT;
147     if (read_string(db, "menu.frame.justify", &str)) {
148         if (!g_ascii_strcasecmp(str, "right"))
149             mjust = RR_JUSTIFY_RIGHT;
150         else if (!g_ascii_strcasecmp(str, "center"))
151             mjust = RR_JUSTIFY_CENTER;
152     }
153
154     /* load direct dimensions */
155     if (!read_int(db, "menuOverlap", &theme->menu_overlap) ||
156         theme->menu_overlap < 0 || theme->menu_overlap > 20)
157         theme->handle_height = 0;
158     if (!read_int(db, "handleWidth", &theme->handle_height) ||
159         theme->handle_height < 0 || theme->handle_height > 100)
160         theme->handle_height = 6;
161     if (!read_int(db, "bevelWidth", &theme->bevel) ||
162         theme->bevel <= 0 || theme->bevel > 100) theme->bevel = 3;
163     if (!read_int(db, "borderWidth", &theme->bwidth) ||
164         theme->bwidth < 0 || theme->bwidth > 100) theme->bwidth = 1;
165     if (!read_int(db, "frameWidth", &theme->cbwidth) ||
166         theme->cbwidth < 0 || theme->cbwidth > 100)
167         theme->cbwidth = theme->bevel;
168
169     /* load colors */
170     if (!read_color(db, inst,
171                     "borderColor", &theme->b_color))
172         theme->b_color = RrColorNew(inst, 0, 0, 0);
173     if (!read_color(db, inst,
174                     "window.frame.focusColor", &theme->cb_focused_color))
175         theme->cb_focused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
176     if (!read_color(db, inst,
177                     "window.frame.unfocusColor",&theme->cb_unfocused_color))
178         theme->cb_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
179     if (!read_color(db, inst,
180                     "window.label.focus.textColor",
181                     &theme->title_focused_color))
182         theme->title_focused_color = RrColorNew(inst, 0x0, 0x0, 0x0);
183     if (!read_color(db, inst,
184                     "window.label.unfocus.textColor",
185                     &theme->title_unfocused_color))
186         theme->title_unfocused_color = RrColorNew(inst, 0xff, 0xff, 0xff);
187     if (!read_color(db, inst,
188                     "window.button.focus.picColor",
189                     &theme->titlebut_focused_unpressed_color))
190         theme->titlebut_focused_unpressed_color = RrColorNew(inst, 0, 0, 0);
191     if (!read_color(db, inst,
192                     "window.button.unfocus.picColor",
193                     &theme->titlebut_unfocused_unpressed_color))
194         theme->titlebut_unfocused_unpressed_color =
195             RrColorNew(inst, 0xff, 0xff, 0xff);
196     if (!read_color(db, inst,
197                     "window.button.pressed.focus.picColor",
198                     &theme->titlebut_focused_pressed_color))
199         theme->titlebut_focused_pressed_color =
200             RrColorNew(inst,
201                        theme->titlebut_focused_unpressed_color->r,
202                        theme->titlebut_focused_unpressed_color->g,
203                        theme->titlebut_focused_unpressed_color->b);
204     if (!read_color(db, inst,
205                     "window.button.pressed.unfocus.picColor",
206                     &theme->titlebut_unfocused_pressed_color))
207         theme->titlebut_unfocused_pressed_color =
208             RrColorNew(inst,
209                        theme->titlebut_unfocused_unpressed_color->r,
210                        theme->titlebut_unfocused_unpressed_color->g,
211                        theme->titlebut_unfocused_unpressed_color->b);
212     if (!read_color(db, inst,
213                     "window.button.disabled.focus.picColor",
214                     &theme->titlebut_disabled_focused_color))
215         theme->titlebut_disabled_focused_color =
216             RrColorNew(inst, 0xff, 0xff, 0xff);
217     if (!read_color(db, inst,
218                     "window.button.disabled.unfocus.picColor",
219                     &theme->titlebut_disabled_unfocused_color))
220         theme->titlebut_disabled_unfocused_color = RrColorNew(inst, 0, 0, 0);
221     if (!read_color(db, inst,
222                     "window.button.hover.focus.picColor",
223                     &theme->titlebut_hover_focused_color))
224         theme->titlebut_hover_focused_color =
225             RrColorNew(inst,
226                        theme->titlebut_focused_unpressed_color->r,
227                        theme->titlebut_focused_unpressed_color->g,
228                        theme->titlebut_focused_unpressed_color->b);
229     if (!read_color(db, inst,
230                     "window.button.hover.unfocus.picColor",
231                     &theme->titlebut_hover_unfocused_color))
232         theme->titlebut_hover_unfocused_color =
233             RrColorNew(inst,
234                        theme->titlebut_unfocused_unpressed_color->r,
235                        theme->titlebut_unfocused_unpressed_color->g,
236                        theme->titlebut_unfocused_unpressed_color->b);
237     if (!read_color(db, inst,
238                     "window.button.toggled.focus.picColor",
239                     &theme->titlebut_toggled_focused_color))
240         theme->titlebut_toggled_focused_color =
241             RrColorNew(inst,
242                        theme->titlebut_focused_pressed_color->r,
243                        theme->titlebut_focused_pressed_color->g,
244                        theme->titlebut_focused_pressed_color->b);
245     if (!read_color(db, inst,
246                     "window.button.toggled.unfocus.picColor",
247                     &theme->titlebut_toggled_unfocused_color))
248         theme->titlebut_toggled_unfocused_color =
249             RrColorNew(inst,
250                        theme->titlebut_unfocused_pressed_color->r,
251                        theme->titlebut_unfocused_pressed_color->g,
252                        theme->titlebut_unfocused_pressed_color->b);
253     if (!read_color(db, inst,
254                     "menu.title.textColor", &theme->menu_title_color))
255         theme->menu_title_color = RrColorNew(inst, 0, 0, 0);
256     if (!read_color(db, inst,
257                     "menu.frame.textColor", &theme->menu_color))
258         theme->menu_color = RrColorNew(inst, 0xff, 0xff, 0xff);
259     if (!read_color(db, inst,
260                     "menu.bullet.picColor", &theme->menu_bullet_color))
261         theme->menu_bullet_color = RrColorNew(inst, 0, 0, 0);
262     if (!read_color(db, inst,
263                     "menu.frame.disableColor", &theme->menu_disabled_color))
264         theme->menu_disabled_color = RrColorNew(inst, 0, 0, 0);
265     if (!read_color(db, inst,
266                     "menu.hilite.textColor", &theme->menu_hilite_color))
267         theme->menu_hilite_color = RrColorNew(inst, 0, 0, 0);
268
269     
270     if (read_mask(inst, "max.xbm", theme, &theme->max_mask)) {
271         if (!read_mask(inst, "max_pressed.xbm", theme,
272                        &theme->max_pressed_mask)) {
273             theme->max_pressed_mask = RrPixmapMaskCopy(theme->max_mask);
274         } 
275         if (!read_mask(inst, "max_toggled.xbm", theme,
276                        &theme->max_toggled_mask)) {
277             theme->max_toggled_mask =
278                 RrPixmapMaskCopy(theme->max_pressed_mask);
279         }
280         if (!read_mask(inst, "max_disabled.xbm", theme,
281                        &theme->max_disabled_mask)) {
282             theme->max_disabled_mask = RrPixmapMaskCopy(theme->max_mask);
283         } 
284         if (!read_mask(inst, "max_hover.xbm", theme, &theme->max_hover_mask)) {
285             theme->max_hover_mask = RrPixmapMaskCopy(theme->max_mask);
286         }
287    } else {
288         {
289             guchar data[] = { 0x7f, 0x7f, 0x7f, 0x41, 0x41, 0x41, 0x7f };
290             theme->max_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
291         }
292         {
293             guchar data[] = { 0x7c, 0x44, 0x47, 0x47, 0x7f, 0x1f, 0x1f };
294             theme->max_toggled_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
295         }
296         theme->max_pressed_mask = RrPixmapMaskCopy(theme->max_mask);
297         theme->max_disabled_mask = RrPixmapMaskCopy(theme->max_mask);
298         theme->max_hover_mask = RrPixmapMaskCopy(theme->max_mask);
299     }
300
301     if (read_mask(inst, "iconify.xbm", theme, &theme->iconify_mask)) {
302         if (!read_mask(inst, "iconify_pressed.xbm", theme,
303                        &theme->iconify_pressed_mask)) {
304             theme->iconify_pressed_mask =
305                 RrPixmapMaskCopy(theme->iconify_mask);
306         } 
307         if (!read_mask(inst, "iconify_disabled.xbm", theme,
308                        &theme->iconify_disabled_mask)) {
309             theme->iconify_disabled_mask =
310                 RrPixmapMaskCopy(theme->iconify_mask);
311         } 
312         if (!read_mask(inst, "iconify_hover.xbm", theme,
313                        &theme->iconify_hover_mask)) {
314             theme->iconify_hover_mask = RrPixmapMaskCopy(theme->iconify_mask);
315         }
316     } else {
317         {
318             guchar data[] = { 0x00, 0x00, 0x00, 0x00, 0x7f, 0x7f, 0x7f };
319             theme->iconify_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
320         }
321         theme->iconify_pressed_mask = RrPixmapMaskCopy(theme->iconify_mask);
322         theme->iconify_disabled_mask = RrPixmapMaskCopy(theme->iconify_mask);
323         theme->iconify_hover_mask = RrPixmapMaskCopy(theme->iconify_mask);
324     }
325
326     if (read_mask(inst, "desk.xbm", theme, &theme->desk_mask)) {
327         if (!read_mask(inst, "desk_pressed.xbm", theme,
328                        &theme->desk_pressed_mask)) {
329             theme->desk_pressed_mask = RrPixmapMaskCopy(theme->desk_mask);
330         } 
331         if (!read_mask(inst, "desk_toggled.xbm", theme,
332                        &theme->desk_toggled_mask)) {
333             theme->desk_toggled_mask =
334                 RrPixmapMaskCopy(theme->desk_pressed_mask);
335         }
336         if (!read_mask(inst, "desk_disabled.xbm", theme,
337                        &theme->desk_disabled_mask)) {
338             theme->desk_disabled_mask = RrPixmapMaskCopy(theme->desk_mask);
339         } 
340         if (!read_mask(inst, "desk_hover.xbm", theme, 
341                        &theme->desk_hover_mask)) {
342             theme->desk_hover_mask = RrPixmapMaskCopy(theme->desk_mask);
343         }
344     } else {
345         {
346             guchar data[] = { 0x63, 0x63, 0x00, 0x00, 0x00, 0x63, 0x63 };
347             theme->desk_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
348         }
349         {
350             guchar data[] = { 0x00, 0x36, 0x36, 0x08, 0x36, 0x36, 0x00 };
351             theme->desk_toggled_mask = RrPixmapMaskNew(inst, 7, 7,
352                                                        (char*)data);
353         }
354         theme->desk_pressed_mask = RrPixmapMaskCopy(theme->desk_mask);
355         theme->desk_disabled_mask = RrPixmapMaskCopy(theme->desk_mask);
356         theme->desk_hover_mask = RrPixmapMaskCopy(theme->desk_mask);
357     }
358
359     if (read_mask(inst, "shade.xbm", theme, &theme->shade_mask)) {
360         if (!read_mask(inst, "shade_pressed.xbm", theme,
361                        &theme->shade_pressed_mask)) {
362             theme->shade_pressed_mask = RrPixmapMaskCopy(theme->shade_mask);
363         } 
364         if (!read_mask(inst, "shade_toggled.xbm", theme,
365                        &theme->shade_toggled_mask)) {
366             theme->shade_toggled_mask =
367                 RrPixmapMaskCopy(theme->shade_pressed_mask);
368         }
369         if (!read_mask(inst, "shade_disabled.xbm", theme,
370                        &theme->shade_disabled_mask)) {
371             theme->shade_disabled_mask = RrPixmapMaskCopy(theme->shade_mask);
372         } 
373         if (!read_mask(inst, "shade_hover.xbm", theme, 
374                        &theme->shade_hover_mask)) {
375             theme->shade_hover_mask = RrPixmapMaskCopy(theme->shade_mask);
376         }
377     } else {
378         {
379             guchar data[] = { 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x00 };
380             theme->shade_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
381         }
382         {
383             guchar data[] = { 0x7f, 0x7f, 0x7f, 0x00, 0x00, 0x00, 0x7f };
384             theme->shade_toggled_mask = RrPixmapMaskNew(inst, 7, 7,
385                                                         (char*)data);
386         }
387         theme->shade_pressed_mask = RrPixmapMaskCopy(theme->shade_mask);
388         theme->shade_disabled_mask = RrPixmapMaskCopy(theme->shade_mask);
389         theme->shade_hover_mask = RrPixmapMaskCopy(theme->shade_mask);
390     }
391
392     if (read_mask(inst, "close.xbm", theme, &theme->close_mask)) {
393         if (!read_mask(inst, "close_pressed.xbm", theme,
394                        &theme->close_pressed_mask)) {
395             theme->close_pressed_mask = RrPixmapMaskCopy(theme->close_mask);
396         } 
397         if (!read_mask(inst, "close_disabled.xbm", theme,
398                        &theme->close_disabled_mask)) {
399             theme->close_disabled_mask = RrPixmapMaskCopy(theme->close_mask);
400         } 
401         if (!read_mask(inst, "close_hover.xbm", theme,
402                        &theme->close_hover_mask)) {
403             theme->close_hover_mask = RrPixmapMaskCopy(theme->close_mask);
404         }
405     } else {
406         {
407             guchar data[] = { 0x63, 0x77, 0x3e, 0x1c, 0x3e, 0x77, 0x63 };
408             theme->close_mask = RrPixmapMaskNew(inst, 7, 7, (char*)data);
409         }
410         theme->close_pressed_mask = RrPixmapMaskCopy(theme->close_mask);
411         theme->close_disabled_mask = RrPixmapMaskCopy(theme->close_mask);
412         theme->close_hover_mask = RrPixmapMaskCopy(theme->close_mask);
413     }
414
415     if (!read_mask(inst, "bullet.xbm", theme, &theme->menu_bullet_mask)) {
416         guchar data[] = { 0x18, 0x30, 0x60, 0xfe, 0xfe, 0x60, 0x30, 0x18 };
417         theme->menu_bullet_mask = RrPixmapMaskNew(inst, 8, 8, (char*)data);
418     }
419
420     /* read the decoration textures */
421     if (!read_appearance(db, inst,
422                          "window.title.focus", theme->a_focused_title,
423                          FALSE))
424         set_default_appearance(theme->a_focused_title);
425     if (!read_appearance(db, inst,
426                          "window.title.unfocus", theme->a_unfocused_title,
427                          FALSE))
428         set_default_appearance(theme->a_unfocused_title);
429     if (!read_appearance(db, inst,
430                          "window.label.focus", theme->a_focused_label,
431                          TRUE))
432         set_default_appearance(theme->a_focused_label);
433     if (!read_appearance(db, inst,
434                          "window.label.unfocus", theme->a_unfocused_label,
435                          TRUE))
436         set_default_appearance(theme->a_unfocused_label);
437     if (!read_appearance(db, inst,
438                          "window.handle.focus", theme->a_focused_handle,
439                          FALSE))
440         set_default_appearance(theme->a_focused_handle);
441     if (!read_appearance(db, inst,
442                          "window.handle.unfocus",theme->a_unfocused_handle,
443                          FALSE))
444         set_default_appearance(theme->a_unfocused_handle);
445     if (!read_appearance(db, inst,
446                          "window.grip.focus", theme->a_focused_grip,
447                          TRUE))
448         set_default_appearance(theme->a_focused_grip);
449     if (!read_appearance(db, inst,
450                          "window.grip.unfocus", theme->a_unfocused_grip,
451                          TRUE))
452         set_default_appearance(theme->a_unfocused_grip);
453     if (!read_appearance(db, inst,
454                          "menu.frame", theme->a_menu,
455                          FALSE))
456         set_default_appearance(theme->a_menu);
457     if (!read_appearance(db, inst,
458                          "menu.title", theme->a_menu_title,
459                          FALSE))
460         set_default_appearance(theme->a_menu_title);
461     if (!read_appearance(db, inst,
462                          "menu.hilite", theme->a_menu_hilite,
463                          TRUE))
464         set_default_appearance(theme->a_menu_hilite);
465
466     /* read the appearances for rendering non-decorations */
467     if (!read_appearance(db, inst,
468                          "window.title.focus", theme->app_hilite_bg,
469                          FALSE))
470         set_default_appearance(theme->app_hilite_bg);
471     if (!read_appearance(db, inst,
472                          "window.label.focus", theme->app_hilite_label,
473                          TRUE))
474         set_default_appearance(theme->app_hilite_label);
475     if (!read_appearance(db, inst,
476                          "window.title.unfocus", theme->app_unhilite_bg,
477                          FALSE))
478         set_default_appearance(theme->app_unhilite_bg);
479     if (!read_appearance(db, inst,
480                          "window.label.unfocus", theme->app_unhilite_label,
481                          TRUE))
482         set_default_appearance(theme->app_unhilite_label);
483
484     /* read buttons textures */
485     if (!read_appearance(db, inst,
486                          "window.button.disabled.focus",
487                          theme->a_disabled_focused_max,
488                          TRUE))
489         set_default_appearance(theme->a_disabled_focused_max);
490     if (!read_appearance(db, inst,
491                          "window.button.disabled.unfocus",
492                          theme->a_disabled_unfocused_max,
493                          TRUE))
494         set_default_appearance(theme->a_disabled_unfocused_max);
495     if (!read_appearance(db, inst,
496                          "window.button.pressed.focus",
497                          theme->a_focused_pressed_max,
498                          TRUE))
499         set_default_appearance(theme->a_focused_pressed_max);
500     if (!read_appearance(db, inst,
501                          "window.button.pressed.unfocus",
502                          theme->a_unfocused_pressed_max,
503                          TRUE))
504         set_default_appearance(theme->a_unfocused_pressed_max);
505     if (!read_appearance(db, inst,
506                          "window.button.toggled.focus",
507                          theme->a_toggled_focused_max,
508                          TRUE))
509     {
510         RrAppearanceFree(theme->a_toggled_focused_max);
511         theme->a_toggled_focused_max =
512             RrAppearanceCopy(theme->a_focused_pressed_max);
513     }
514     if (!read_appearance(db, inst,
515                          "window.button.toggled.unfocus",
516                          theme->a_toggled_unfocused_max,
517                          TRUE))
518     {
519         RrAppearanceFree(theme->a_toggled_unfocused_max);
520         theme->a_toggled_unfocused_max =
521             RrAppearanceCopy(theme->a_unfocused_pressed_max);
522     }
523     if (!read_appearance(db, inst,
524                          "window.button.focus",
525                          theme->a_focused_unpressed_max,
526                          TRUE))
527         set_default_appearance(theme->a_focused_unpressed_max);
528     if (!read_appearance(db, inst,
529                          "window.button.unfocus",
530                          theme->a_unfocused_unpressed_max,
531                          TRUE))
532         set_default_appearance(theme->a_unfocused_unpressed_max);
533     if (!read_appearance(db, inst,
534                          "window.button.hover.focus",
535                          theme->a_hover_focused_max,
536                          TRUE))
537     {
538         RrAppearanceFree(theme->a_hover_focused_max);
539         theme->a_hover_focused_max =
540             RrAppearanceCopy(theme->a_focused_unpressed_max);
541     }
542     if (!read_appearance(db, inst,
543                          "window.button.hover.unfocus",
544                          theme->a_hover_unfocused_max,
545                          TRUE))
546     {
547         RrAppearanceFree(theme->a_hover_unfocused_max);
548         theme->a_hover_unfocused_max =
549             RrAppearanceCopy(theme->a_unfocused_unpressed_max);
550     }
551
552     theme->a_disabled_focused_close =
553         RrAppearanceCopy(theme->a_disabled_focused_max);
554     theme->a_disabled_unfocused_close =
555         RrAppearanceCopy(theme->a_disabled_unfocused_max);
556     theme->a_hover_focused_close =
557         RrAppearanceCopy(theme->a_hover_focused_max);
558     theme->a_hover_unfocused_close =
559         RrAppearanceCopy(theme->a_hover_unfocused_max);
560     theme->a_unfocused_unpressed_close =
561         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
562     theme->a_unfocused_pressed_close =
563         RrAppearanceCopy(theme->a_unfocused_pressed_max);
564     theme->a_focused_unpressed_close =
565         RrAppearanceCopy(theme->a_focused_unpressed_max);
566     theme->a_focused_pressed_close =
567         RrAppearanceCopy(theme->a_focused_pressed_max);
568     theme->a_disabled_focused_desk =
569         RrAppearanceCopy(theme->a_disabled_focused_max);
570     theme->a_disabled_unfocused_desk =
571         RrAppearanceCopy(theme->a_disabled_unfocused_max);
572     theme->a_hover_focused_desk =
573         RrAppearanceCopy(theme->a_hover_focused_max);
574     theme->a_hover_unfocused_desk =
575         RrAppearanceCopy(theme->a_hover_unfocused_max); 
576     theme->a_toggled_focused_desk =
577         RrAppearanceCopy(theme->a_toggled_focused_max);
578    theme->a_toggled_unfocused_desk =
579         RrAppearanceCopy(theme->a_toggled_unfocused_max);
580     theme->a_unfocused_unpressed_desk =
581         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
582     theme->a_unfocused_pressed_desk =
583         RrAppearanceCopy(theme->a_unfocused_pressed_max);
584     theme->a_focused_unpressed_desk =
585         RrAppearanceCopy(theme->a_focused_unpressed_max);
586     theme->a_focused_pressed_desk =
587         RrAppearanceCopy(theme->a_focused_pressed_max);
588     theme->a_disabled_focused_shade =
589         RrAppearanceCopy(theme->a_disabled_focused_max);
590     theme->a_disabled_unfocused_shade =
591         RrAppearanceCopy(theme->a_disabled_unfocused_max);
592     theme->a_hover_focused_shade =
593         RrAppearanceCopy(theme->a_hover_focused_max);
594     theme->a_hover_unfocused_shade =
595         RrAppearanceCopy(theme->a_hover_unfocused_max);
596     theme->a_toggled_focused_shade =
597         RrAppearanceCopy(theme->a_toggled_focused_max);
598     theme->a_toggled_unfocused_shade =
599         RrAppearanceCopy(theme->a_toggled_unfocused_max);
600     theme->a_unfocused_unpressed_shade =
601         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
602     theme->a_unfocused_pressed_shade =
603         RrAppearanceCopy(theme->a_unfocused_pressed_max);
604     theme->a_focused_unpressed_shade =
605         RrAppearanceCopy(theme->a_focused_unpressed_max);
606     theme->a_focused_pressed_shade =
607         RrAppearanceCopy(theme->a_focused_pressed_max);
608     theme->a_disabled_focused_iconify =
609         RrAppearanceCopy(theme->a_disabled_focused_max);
610     theme->a_disabled_unfocused_iconify =
611         RrAppearanceCopy(theme->a_disabled_focused_max);
612     theme->a_hover_focused_iconify =
613         RrAppearanceCopy(theme->a_hover_focused_max);
614     theme->a_hover_unfocused_iconify =
615         RrAppearanceCopy(theme->a_hover_unfocused_max);
616     theme->a_unfocused_unpressed_iconify =
617         RrAppearanceCopy(theme->a_unfocused_unpressed_max);
618     theme->a_unfocused_pressed_iconify =
619         RrAppearanceCopy(theme->a_unfocused_pressed_max);
620     theme->a_focused_unpressed_iconify =
621         RrAppearanceCopy(theme->a_focused_unpressed_max);
622     theme->a_focused_pressed_iconify =
623         RrAppearanceCopy(theme->a_focused_pressed_max);
624
625     theme->a_icon->surface.grad =
626         theme->a_clear->surface.grad =
627         theme->a_clear_tex->surface.grad =
628         theme->a_menu_item->surface.grad =
629         theme->a_menu_disabled->surface.grad =
630         theme->a_menu_text_item->surface.grad =
631         theme->a_menu_text_disabled->surface.grad =
632         theme->a_menu_text_hilite->surface.grad =
633         theme->a_menu_bullet->surface.grad = RR_SURFACE_PARENTREL;
634
635     /* set up the textures */
636     theme->a_focused_label->texture[0].type = 
637         theme->app_hilite_label->texture[0].type = RR_TEXTURE_TEXT;
638     theme->a_focused_label->texture[0].data.text.justify = winjust;
639     theme->app_hilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
640     theme->a_focused_label->texture[0].data.text.font =
641         theme->app_hilite_label->texture[0].data.text.font =
642         theme->winfont_focused;
643     theme->a_focused_label->texture[0].data.text.color =
644         theme->app_hilite_label->texture[0].data.text.color =
645         theme->title_focused_color;
646
647     theme->a_unfocused_label->texture[0].type =
648         theme->app_unhilite_label->texture[0].type = RR_TEXTURE_TEXT;
649     theme->a_unfocused_label->texture[0].data.text.justify = winjust;
650     theme->app_unhilite_label->texture[0].data.text.justify = RR_JUSTIFY_LEFT;
651     theme->a_unfocused_label->texture[0].data.text.font =
652         theme->app_unhilite_label->texture[0].data.text.font =
653         theme->winfont_unfocused;
654     theme->a_unfocused_label->texture[0].data.text.color =
655         theme->app_unhilite_label->texture[0].data.text.color =
656         theme->title_unfocused_color;
657
658     theme->a_menu_title->texture[0].type = RR_TEXTURE_TEXT;
659     theme->a_menu_title->texture[0].data.text.justify = mtitlejust;
660     theme->a_menu_title->texture[0].data.text.font = theme->mtitlefont;
661     theme->a_menu_title->texture[0].data.text.color = theme->menu_title_color;
662
663     theme->a_menu_text_item->texture[0].type =
664         theme->a_menu_text_disabled->texture[0].type = 
665         theme->a_menu_text_hilite->texture[0].type = RR_TEXTURE_TEXT;
666     theme->a_menu_text_item->texture[0].data.text.justify = 
667         theme->a_menu_text_disabled->texture[0].data.text.justify = 
668         theme->a_menu_text_hilite->texture[0].data.text.justify = mjust;
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 }