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