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