]> icculus.org git repositories - mikachu/openbox.git/blob - engines/openbox/theme.c
cast the pixmap data to a char* for the pixmap_mask_new
[mikachu/openbox.git] / engines / openbox / theme.c
1 #include "openbox.h"
2 #include "../../kernel/themerc.h"
3 #include "../../kernel/openbox.h"
4
5 #include <glib.h>
6 #include <X11/Xlib.h>
7 #include <X11/Xresource.h>
8 #ifdef HAVE_STDLIB_H
9 #  include <stdlib.h>
10 #endif
11 #ifdef HAVE_CTYPE_H
12 #  include <ctype.h>
13 #endif
14 #ifdef HAVE_STRING_H
15 #  include <string.h>
16 #endif
17
18 static XrmDatabase loaddb(char *theme)
19 {
20     XrmDatabase db;
21
22     db = XrmGetFileDatabase(theme);
23     if (db == NULL) {
24         char *s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
25                                    "openbox", theme, NULL);
26         db = XrmGetFileDatabase(s);
27         g_free(s);
28     }
29     if (db == NULL) {
30         char *s = g_build_filename(THEMEDIR, theme, NULL);
31         db = XrmGetFileDatabase(s);
32         g_free(s);
33     }
34     return db;
35 }
36
37 static char *create_class_name(char *rname)
38 {
39     char *rclass = g_strdup(rname);
40     char *p = rclass;
41
42     while (TRUE) {
43         *p = toupper(*p);
44         p = strchr(p+1, '.');
45         if (p == NULL) break;
46         ++p;
47         if (*p == '\0') break;
48     }
49     return rclass;
50 }
51
52 gboolean read_bool(XrmDatabase db, char *rname, gboolean *value)
53 {
54     gboolean ret = FALSE;
55     char *rclass = create_class_name(rname);
56     char *rettype;
57     XrmValue retvalue;
58   
59     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
60         retvalue.addr != NULL) {
61         if (!g_ascii_strcasecmp(retvalue.addr, "true"))
62             *value = TRUE;
63         else
64             *value = FALSE;
65         ret = TRUE;
66     }
67
68     g_free(rclass);
69     return ret;
70 }
71
72 gboolean read_int(XrmDatabase db, char *rname, int *value)
73 {
74     gboolean ret = FALSE;
75     char *rclass = create_class_name(rname);
76     char *rettype, *end;
77     XrmValue retvalue;
78   
79     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
80         retvalue.addr != NULL) {
81         *value = (int)strtol(retvalue.addr, &end, 10);
82         if (end != retvalue.addr)
83             ret = TRUE;
84     }
85
86     g_free(rclass);
87     return ret;
88 }
89
90 gboolean read_string(XrmDatabase db, char *rname, char **value)
91 {
92     gboolean ret = FALSE;
93     char *rclass = create_class_name(rname);
94     char *rettype;
95     XrmValue retvalue;
96   
97     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
98         retvalue.addr != NULL) {
99         *value = g_strdup(retvalue.addr);
100         ret = TRUE;
101     }
102
103     g_free(rclass);
104     return ret;
105 }
106
107 gboolean read_color(XrmDatabase db, char *rname, color_rgb **value)
108 {
109     gboolean ret = FALSE;
110     char *rclass = create_class_name(rname);
111     char *rettype;
112     XrmValue retvalue;
113   
114     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
115         retvalue.addr != NULL) {
116         color_rgb *c = color_parse(retvalue.addr);
117         if (c != NULL) {
118             *value = c;
119             ret = TRUE;
120         }
121     }
122
123     g_free(rclass);
124     return ret;
125 }
126
127 gboolean read_mask(XrmDatabase db, char *rname, pixmap_mask **value)
128 {
129     gboolean ret = FALSE;
130     char *rclass = create_class_name(rname);
131     char *rettype;
132     char *s;
133     char *button_dir;
134     XrmValue retvalue;
135     int hx, hy; /* ignored */
136     unsigned int w, h;
137     unsigned char *b;
138   
139     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
140         retvalue.addr != NULL) {
141         button_dir = g_strdup_printf("%s_buttons", themerc_theme);
142
143         s = g_build_filename(g_get_home_dir(), ".openbox", "themes",
144                              "openbox", button_dir, retvalue.addr, NULL);
145
146         if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess)
147             ret = TRUE;
148         else {
149             g_free(s);
150             s = g_build_filename(THEMEDIR, button_dir, retvalue.addr, NULL);
151         
152             if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) == BitmapSuccess) 
153                 ret = TRUE;
154             else {
155                 g_free(s);
156                 s = g_strdup_printf("%s_buttons/%s", themerc_theme, 
157                                     themerc_theme);
158                 if (XReadBitmapFileData(s, &w, &h, &b, &hx, &hy) ==
159                     BitmapSuccess) 
160                     ret = TRUE;
161                 else
162                     g_message("Unable to find bitmap '%s'", s);
163             }
164         }
165
166         if (ret) {
167             *value = pixmap_mask_new(w, h, (char*)b);
168             XFree(b);
169         }
170       
171         g_free(s);
172         g_free(button_dir);
173     }
174
175     g_free(rclass);
176     return ret;
177 }
178
179 static void parse_appearance(char *tex, SurfaceColorType *grad,
180                              ReliefType *relief, BevelType *bevel,
181                              gboolean *interlaced, gboolean *border)
182 {
183     char *t;
184
185     /* convert to all lowercase */
186     for (t = tex; *t != '\0'; ++t)
187         *t = g_ascii_tolower(*t);
188
189     if (strstr(tex, "parentrelative") != NULL) {
190         *grad = Background_ParentRelative;
191     } else {
192         if (strstr(tex, "gradient") != NULL) {
193             if (strstr(tex, "crossdiagonal") != NULL)
194                 *grad = Background_CrossDiagonal;
195             else if (strstr(tex, "rectangle") != NULL)
196                 *grad = Background_Rectangle;
197             else if (strstr(tex, "pyramid") != NULL)
198                 *grad = Background_Pyramid;
199             else if (strstr(tex, "pipecross") != NULL)
200                 *grad = Background_PipeCross;
201             else if (strstr(tex, "elliptic") != NULL)
202                 *grad = Background_Elliptic;
203             else if (strstr(tex, "horizontal") != NULL)
204                 *grad = Background_Horizontal;
205             else if (strstr(tex, "vertical") != NULL)
206                 *grad = Background_Vertical;
207             else
208                 *grad = Background_Diagonal;
209         } else {
210             *grad = Background_Solid;
211         }
212
213         if (strstr(tex, "sunken") != NULL)
214             *relief = Sunken;
215         else if (strstr(tex, "flat") != NULL)
216             *relief = Flat;
217         else
218             *relief = Raised;
219         
220         *border = FALSE;
221         if (*relief == Flat) {
222             if (strstr(tex, "border") != NULL)
223                 *border = TRUE;
224         } else {
225             if (strstr(tex, "bevel2") != NULL)
226                 *bevel = Bevel2;
227             else
228                 *bevel = Bevel1;
229         }
230
231         if (strstr(tex, "interlaced") != NULL)
232             *interlaced = TRUE;
233         else
234             *interlaced = FALSE;
235     }
236 }
237
238
239 gboolean read_appearance(XrmDatabase db, char *rname, Appearance *value)
240 {
241     gboolean ret = FALSE;
242     char *rclass = create_class_name(rname), *cname, *ctoname, *bcname;
243     char *rettype;
244     XrmValue retvalue;
245
246     cname = g_strconcat(rname, ".color", NULL);
247     ctoname = g_strconcat(rname, ".colorTo", NULL);
248     bcname = g_strconcat(rname, ".borderColor", NULL);
249
250     if (XrmGetResource(db, rname, rclass, &rettype, &retvalue) &&
251         retvalue.addr != NULL) {
252         parse_appearance(retvalue.addr,
253                          &value->surface.data.planar.grad,
254                          &value->surface.data.planar.relief,
255                          &value->surface.data.planar.bevel,
256                          &value->surface.data.planar.interlaced,
257                          &value->surface.data.planar.border);
258         if (!read_color(db, cname, &value->surface.data.planar.primary))
259             value->surface.data.planar.primary = color_new(0, 0, 0);
260         if (!read_color(db, ctoname, &value->surface.data.planar.secondary))
261             value->surface.data.planar.secondary = color_new(0, 0, 0);
262         if (value->surface.data.planar.border)
263             if (!read_color(db, bcname,
264                             &value->surface.data.planar.border_color))
265                 value->surface.data.planar.border_color = color_new(0, 0, 0);
266         ret = TRUE;
267     }
268
269     g_free(bcname);
270     g_free(ctoname);
271     g_free(cname);
272     g_free(rclass);
273     return ret;
274 }
275
276 void set_default_appearance(Appearance *a)
277 {
278     a->surface.data.planar.grad = Background_Solid;
279     a->surface.data.planar.relief = Flat;
280     a->surface.data.planar.bevel = Bevel1;
281     a->surface.data.planar.interlaced = FALSE;
282     a->surface.data.planar.border = FALSE;
283     a->surface.data.planar.primary = color_new(0, 0, 0);
284     a->surface.data.planar.secondary = color_new(0, 0, 0);
285 }
286
287 gboolean load()
288 {
289     XrmDatabase db = NULL;
290     Justify winjust;
291     char *winjuststr;
292
293     if (themerc_theme != NULL) {
294         db = loaddb(themerc_theme);
295         if (db == NULL) {
296             g_warning("Failed to load the theme '%s'", themerc_theme);
297             g_message("Falling back to the default: '%s'", DEFAULT_THEME);
298         }
299     }
300     if (db == NULL) {
301         db = loaddb(DEFAULT_THEME);
302         if (db == NULL) {
303             g_warning("Failed to load the theme '%s'.", DEFAULT_THEME);
304             return FALSE;
305         }
306         /* change to reflect what was actually loaded */
307         g_free(themerc_theme);
308         themerc_theme = g_strdup(DEFAULT_THEME);
309     }
310
311     /* load the font, not from the theme file tho, its in themerc_font */
312     s_winfont_shadow = 1; /* XXX read from themrc */
313     s_winfont_shadow_offset = 1; /* XXX read from themerc */
314     s_winfont = font_open(themerc_font);
315     s_winfont_height = font_height(s_winfont, s_winfont_shadow,
316                                    s_winfont_shadow_offset);
317
318     winjust = Justify_Left;
319     if (read_string(db, "window.justify", &winjuststr)) {
320         if (!g_ascii_strcasecmp(winjuststr, "right"))
321             winjust = Justify_Right;
322         else if (!g_ascii_strcasecmp(winjuststr, "center"))
323             winjust = Justify_Center;
324         g_free(winjuststr);
325     }
326
327     if (!read_int(db, "handleWidth", &s_handle_height) ||
328         s_handle_height < 0 || s_handle_height > 100) s_handle_height = 6;
329     if (!read_int(db, "bevelWidth", &s_bevel) ||
330         s_bevel <= 0 || s_bevel > 100) s_bevel = 3;
331     if (!read_int(db, "borderWidth", &s_bwidth) ||
332         s_bwidth < 0 || s_bwidth > 100) s_bwidth = 1;
333     if (!read_int(db, "frameWidth", &s_cbwidth) ||
334         s_cbwidth < 0 || s_cbwidth > 100) s_cbwidth = s_bevel;
335
336     if (!read_color(db, "borderColor", &s_b_color))
337         s_b_color = color_new(0, 0, 0);
338     if (!read_color(db, "window.frame.focusColor", &s_cb_focused_color))
339         s_cb_focused_color = color_new(0xff, 0xff, 0xff);
340     if (!read_color(db, "window.frame.unfocusColor", &s_cb_unfocused_color))
341         s_cb_unfocused_color = color_new(0xff, 0xff, 0xff);
342     if (!read_color(db, "window.label.focus.textColor",
343                     &s_title_focused_color))
344         s_title_focused_color = color_new(0xff, 0xff, 0xff);
345     if (!read_color(db, "window.label.unfocus.textColor",
346                     &s_title_unfocused_color))
347         s_title_unfocused_color = color_new(0xff, 0xff, 0xff);
348     if (!read_color(db, "window.button.focus.picColor",
349                     &s_titlebut_focused_color))
350         s_titlebut_focused_color = color_new(0, 0, 0);
351     if (!read_color(db, "window.button.unfocus.picColor",
352                     &s_titlebut_unfocused_color))
353         s_titlebut_unfocused_color = color_new(0xff, 0xff, 0xff);
354
355     if (!read_mask(db, "window.button.max.mask", &s_max_mask)) {
356         char data[] = { 0x7c, 0x44, 0x47, 0x47, 0x7f, 0x1f, 0x1f  };
357         s_max_mask = pixmap_mask_new(7, 7, data);
358     }
359     if (!read_mask(db, "window.button.icon.mask", &s_icon_mask)) {
360         char data[] = { 0x00, 0x00, 0x00, 0x00, 0x3e, 0x3e, 0x3e };
361         s_icon_mask = pixmap_mask_new(7, 7, data);
362     }
363     if (!read_mask(db, "window.button.stick.mask", &s_desk_mask)) {
364         char data[] = { 0x00, 0x36, 0x36, 0x00, 0x36, 0x36, 0x00 };
365         s_desk_mask = pixmap_mask_new(7, 7, data);
366     }
367     if (!read_mask(db, "window.button.close.mask", &s_close_mask)) {
368         char data[] = { 0x22, 0x77, 0x3e, 0x1c, 0x3e, 0x77, 0x22 };
369         s_close_mask = pixmap_mask_new(7, 7, data);
370     }        
371
372     if (!read_appearance(db, "window.title.focus", a_focused_title))
373         set_default_appearance(a_focused_title);
374     if (!read_appearance(db, "window.title.unfocus", a_unfocused_title))
375         set_default_appearance(a_unfocused_title);
376     if (!read_appearance(db, "window.label.focus", a_focused_label))
377         set_default_appearance(a_focused_label);
378     if (!read_appearance(db, "window.label.unfocus", a_unfocused_label))
379         set_default_appearance(a_unfocused_label);
380     if (!read_appearance(db, "window.handle.focus", a_focused_handle))
381         set_default_appearance(a_focused_handle);
382     if (!read_appearance(db, "window.handle.unfocus", a_unfocused_handle))
383         set_default_appearance(a_unfocused_handle);
384     if (!read_appearance(db, "window.grip.focus", a_focused_grip))
385         set_default_appearance(a_focused_grip);
386     if (!read_appearance(db, "window.grip.unfocus", a_unfocused_grip))
387         set_default_appearance(a_unfocused_grip);
388
389     if (!read_appearance(db, "window.button.pressed.focus",
390                          a_focused_pressed_max))
391         if (!read_appearance(db, "window.button.pressed",
392                              a_focused_pressed_max))
393             set_default_appearance(a_focused_pressed_max);
394     if (!read_appearance(db, "window.button.pressed.unfocus",
395                          a_unfocused_pressed_max))
396         if (!read_appearance(db, "window.button.pressed",
397                              a_unfocused_pressed_max))
398             set_default_appearance(a_unfocused_pressed_max);
399     if (!read_appearance(db, "window.button.focus",
400                          a_focused_unpressed_max))
401         set_default_appearance(a_focused_unpressed_max);
402     if (!read_appearance(db, "window.button.unfocus",
403                          a_unfocused_unpressed_max))
404         set_default_appearance(a_unfocused_unpressed_max);
405
406     a_unfocused_unpressed_close = appearance_copy(a_unfocused_unpressed_max);
407     a_unfocused_pressed_close = appearance_copy(a_unfocused_pressed_max);
408     a_focused_unpressed_close = appearance_copy(a_focused_unpressed_max);
409     a_focused_pressed_close = appearance_copy(a_focused_pressed_max);
410     a_unfocused_unpressed_desk = appearance_copy(a_unfocused_unpressed_max);
411     a_unfocused_pressed_desk = appearance_copy(a_unfocused_pressed_max);
412     a_focused_unpressed_desk = appearance_copy(a_focused_unpressed_max);
413     a_focused_pressed_desk = appearance_copy(a_focused_pressed_max);
414     a_unfocused_unpressed_iconify = appearance_copy(a_unfocused_unpressed_max);
415     a_unfocused_pressed_iconify = appearance_copy(a_unfocused_pressed_max);
416     a_focused_unpressed_iconify = appearance_copy(a_focused_unpressed_max);
417     a_focused_pressed_iconify = appearance_copy(a_focused_pressed_max);
418
419     a_icon->surface.data.planar.grad = Background_ParentRelative;
420
421     /* set up the textures */
422     a_focused_label->texture[0].type = Text;
423     a_focused_label->texture[0].data.text.justify = winjust;
424     a_focused_label->texture[0].data.text.font = s_winfont;
425     a_focused_label->texture[0].data.text.shadow = s_winfont_shadow;
426     a_focused_label->texture[0].data.text.offset = s_winfont_shadow_offset;
427     a_focused_label->texture[0].data.text.color = s_title_focused_color;
428
429     a_unfocused_label->texture[0].type = Text;
430     a_unfocused_label->texture[0].data.text.justify = winjust;
431     a_unfocused_label->texture[0].data.text.font = s_winfont;
432     a_unfocused_label->texture[0].data.text.shadow = s_winfont_shadow;
433     a_unfocused_label->texture[0].data.text.offset = s_winfont_shadow_offset;
434     a_unfocused_label->texture[0].data.text.color = s_title_unfocused_color;
435
436     a_focused_unpressed_max->texture[0].type = 
437         a_focused_pressed_max->texture[0].type = 
438         a_unfocused_unpressed_max->texture[0].type = 
439         a_unfocused_pressed_max->texture[0].type = 
440         a_focused_unpressed_close->texture[0].type = 
441         a_focused_pressed_close->texture[0].type = 
442         a_unfocused_unpressed_close->texture[0].type = 
443         a_unfocused_pressed_close->texture[0].type = 
444         a_focused_unpressed_desk->texture[0].type = 
445         a_focused_pressed_desk->texture[0].type = 
446         a_unfocused_unpressed_desk->texture[0].type = 
447         a_unfocused_pressed_desk->texture[0].type = 
448         a_focused_unpressed_iconify->texture[0].type = 
449         a_focused_pressed_iconify->texture[0].type = 
450         a_unfocused_unpressed_iconify->texture[0].type = 
451         a_unfocused_pressed_iconify->texture[0].type = Bitmask;
452     a_focused_unpressed_max->texture[0].data.mask.mask = 
453         a_focused_pressed_max->texture[0].data.mask.mask = 
454         a_unfocused_unpressed_max->texture[0].data.mask.mask = 
455         a_unfocused_pressed_max->texture[0].data.mask.mask = s_max_mask;
456     a_focused_unpressed_close->texture[0].data.mask.mask = 
457         a_focused_pressed_close->texture[0].data.mask.mask = 
458         a_unfocused_unpressed_close->texture[0].data.mask.mask = 
459         a_unfocused_pressed_close->texture[0].data.mask.mask = s_close_mask;
460     a_focused_unpressed_desk->texture[0].data.mask.mask = 
461         a_focused_pressed_desk->texture[0].data.mask.mask = 
462         a_unfocused_unpressed_desk->texture[0].data.mask.mask = 
463         a_unfocused_pressed_desk->texture[0].data.mask.mask = s_desk_mask;
464     a_focused_unpressed_iconify->texture[0].data.mask.mask = 
465         a_focused_pressed_iconify->texture[0].data.mask.mask = 
466         a_unfocused_unpressed_iconify->texture[0].data.mask.mask = 
467         a_unfocused_pressed_iconify->texture[0].data.mask.mask = s_icon_mask;
468     a_focused_unpressed_max->texture[0].data.mask.color = 
469         a_focused_pressed_max->texture[0].data.mask.color = 
470         a_focused_unpressed_close->texture[0].data.mask.color = 
471         a_focused_pressed_close->texture[0].data.mask.color = 
472         a_focused_unpressed_desk->texture[0].data.mask.color = 
473         a_focused_pressed_desk->texture[0].data.mask.color = 
474         a_focused_unpressed_iconify->texture[0].data.mask.color = 
475         a_focused_pressed_iconify->texture[0].data.mask.color =
476         s_titlebut_focused_color;
477     a_unfocused_unpressed_max->texture[0].data.mask.color = 
478         a_unfocused_pressed_max->texture[0].data.mask.color = 
479         a_unfocused_unpressed_close->texture[0].data.mask.color = 
480         a_unfocused_pressed_close->texture[0].data.mask.color = 
481         a_unfocused_unpressed_desk->texture[0].data.mask.color = 
482         a_unfocused_pressed_desk->texture[0].data.mask.color = 
483         a_unfocused_unpressed_iconify->texture[0].data.mask.color = 
484         a_unfocused_pressed_iconify->texture[0].data.mask.color =
485         s_titlebut_unfocused_color;
486
487     XrmDestroyDatabase(db);
488     return TRUE;
489 }
490
491