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