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