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