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