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