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