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