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