]> icculus.org git repositories - mikachu/openbox.git/blob - otk/style.cc
use otk::Property to get the atom values
[mikachu/openbox.git] / otk / style.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 #include <assert.h>
8 #include <iostream>
9
10 #include "display.hh"
11 #include "util.hh"
12 #include "style.hh"
13
14 namespace otk {
15
16 Style::Style() : font(NULL)
17 {
18 }
19
20 Style::Style(ImageControl *ctrl)
21   : image_control(ctrl), font(0),
22     screen_number(ctrl->getScreenInfo()->screen())
23 {
24 }
25
26 Style::~Style() {
27   if (font)
28     delete font;
29
30   if (close_button.mask != None)
31     XFreePixmap(**display, close_button.mask);
32   if (max_button.mask != None)
33     XFreePixmap(**display, max_button.mask);
34   if (icon_button.mask != None)
35     XFreePixmap(**display, icon_button.mask);
36   if (stick_button.mask != None)
37     XFreePixmap(**display, stick_button.mask);
38
39   max_button.mask = None;
40   close_button.mask = None;
41   icon_button.mask = None;
42   stick_button.mask = None;
43 }
44
45 void Style::load(const Configuration &style) {
46   std::string s;
47
48   // load fonts/fontsets
49   if (font)
50     delete font;
51
52   font = readDatabaseFont("window.", style);
53
54   // load window config
55   t_focus = readDatabaseTexture("window.title.focus", "white", style);
56   t_unfocus = readDatabaseTexture("window.title.unfocus", "black", style);
57
58   l_focus = readDatabaseTexture("window.label.focus", "white", style);
59   l_unfocus = readDatabaseTexture("window.label.unfocus", "black", style);
60
61   h_focus = readDatabaseTexture("window.handle.focus", "white", style);
62   h_unfocus = readDatabaseTexture("window.handle.unfocus", "black", style);
63
64   g_focus = readDatabaseTexture("window.grip.focus", "white", style);
65   g_unfocus = readDatabaseTexture("window.grip.unfocus", "black", style);
66
67   b_focus = readDatabaseTexture("window.button.focus", "white", style);
68   b_unfocus = readDatabaseTexture("window.button.unfocus", "black", style);
69
70   //if neither of these can be found, we will use the previous resource
71   b_pressed_focus = readDatabaseTexture("window.button.pressed.focus",
72                                         "black", style, true);
73   if (b_pressed_focus.texture() == Texture::NoTexture) {
74     b_pressed_focus = readDatabaseTexture("window.button.pressed", "black",
75                                           style);
76   }
77     
78   b_pressed_unfocus = readDatabaseTexture("window.button.pressed.unfocus",
79                                           "black", style, true);
80   if (b_pressed_unfocus.texture() == Texture::NoTexture) {
81     b_pressed_unfocus = readDatabaseTexture("window.button.pressed", "black",
82                                             style);
83   }
84
85   if (close_button.mask != None)
86     XFreePixmap(**display, close_button.mask);
87   if (max_button.mask != None)
88     XFreePixmap(**display, max_button.mask);
89   if (icon_button.mask != None)
90     XFreePixmap(**display, icon_button.mask);
91   if (stick_button.mask != None)
92     XFreePixmap(**display, stick_button.mask);
93
94   close_button.mask = max_button.mask = icon_button.mask
95                     = icon_button.mask = None;
96   
97   readDatabaseMask("window.button.close.mask", close_button, style);
98   readDatabaseMask("window.button.max.mask", max_button, style);
99   readDatabaseMask("window.button.icon.mask", icon_button, style);
100   readDatabaseMask("window.button.stick.mask", stick_button, style);
101
102   // we create the window.frame texture by hand because it exists only to
103   // make the code cleaner and is not actually used for display
104   Color color = readDatabaseColor("window.frame.focusColor", "white",
105                                         style);
106   f_focus = Texture("solid flat", screen_number, image_control);
107   f_focus.setColor(color);
108
109   color = readDatabaseColor("window.frame.unfocusColor", "white", style);
110   f_unfocus = Texture("solid flat", screen_number, image_control);
111   f_unfocus.setColor(color);
112
113   l_text_focus = readDatabaseColor("window.label.focus.textColor",
114                                    "black", style);
115   l_text_unfocus = readDatabaseColor("window.label.unfocus.textColor",
116                                      "white", style);
117
118   b_pic_focus = readDatabaseColor("window.button.focus.picColor",
119                                   "black", style);
120   b_pic_unfocus = readDatabaseColor("window.button.unfocus.picColor",
121                                     "white", style);
122
123   justify = LeftJustify;
124
125   if (style.getValue("window.justify", s)) {
126     if (s == "right" || s == "Right")
127       justify = RightJustify;
128     else if (s == "center" || s == "Center")
129       justify = CenterJustify;
130   }
131
132   // sanity checks
133   if (t_focus.texture() == Texture::Parent_Relative)
134     t_focus = f_focus;
135   if (t_unfocus.texture() == Texture::Parent_Relative)
136     t_unfocus = f_unfocus;
137   if (h_focus.texture() == Texture::Parent_Relative)
138     h_focus = f_focus;
139   if (h_unfocus.texture() == Texture::Parent_Relative)
140     h_unfocus = f_unfocus;
141
142   border_color = readDatabaseColor("borderColor", "black", style);
143
144   // load bevel, border and handle widths
145
146   const ScreenInfo *s_info = display->screenInfo(screen_number);
147   unsigned int width = s_info->rect().width();
148
149   if (! style.getValue("handleWidth", handle_width) ||
150       handle_width > width/2 || handle_width == 0)
151     handle_width = 6;
152
153   if (! style.getValue("borderWidth", border_width))
154     border_width = 1;
155
156   if (! style.getValue("bevelWidth", bevel_width)
157       || bevel_width > width/2 || bevel_width == 0)
158     bevel_width = 3;
159
160   if (! style.getValue("frameWidth", frame_width)
161       || frame_width > width/2)
162     frame_width = bevel_width;
163
164   if (style.getValue("rootCommand", s))
165     bexec(s, s_info->displayString());
166 }
167
168
169 void Style::readDatabaseMask(const std::string &rname, PixmapMask &pixmapMask,
170                              const Configuration &style) {
171   Window root_window = display->screenInfo(screen_number)->rootWindow();
172   std::string s;
173   int hx, hy; //ignored
174   int ret = BitmapOpenFailed; //default to failure.
175   
176   if (style.getValue(rname, s)) {
177     if (s[0] != '/' && s[0] != '~') {
178       std::string xbmFile = std::string("~/.openbox/buttons/") + s;
179       ret = XReadBitmapFile(**display, root_window,
180                             expandTilde(xbmFile).c_str(), &pixmapMask.w,
181                             &pixmapMask.h, &pixmapMask.mask, &hx, &hy);
182       if (ret != BitmapSuccess) {
183         xbmFile = std::string(BUTTONSDIR) + "/" + s;
184         ret = XReadBitmapFile(**display, root_window,
185                               xbmFile.c_str(), &pixmapMask.w,
186                               &pixmapMask.h, &pixmapMask.mask, &hx, &hy);
187       }
188     } else
189       ret = XReadBitmapFile(**display, root_window,
190                             expandTilde(s).c_str(), &pixmapMask.w,
191                             &pixmapMask.h, &pixmapMask.mask, &hx, &hy);
192     
193     if (ret == BitmapSuccess)
194       return;
195   }
196
197   pixmapMask.mask = None;
198   pixmapMask.w = pixmapMask.h = 0;
199 }
200
201
202 Texture Style::readDatabaseTexture(const std::string &rname,
203                                    const std::string &default_color,
204                                    const Configuration &style, 
205                                    bool allowNoTexture)
206 {
207   Texture texture;
208   std::string s;
209
210   if (style.getValue(rname, s))
211     texture = Texture(s);
212   else if (allowNoTexture) //no default
213     texture.setTexture(Texture::NoTexture);
214   else
215     texture.setTexture(Texture::Solid | Texture::Flat);
216
217   // associate this texture with this screen
218   texture.setScreen(screen_number);
219   texture.setImageControl(image_control);
220
221   if (texture.texture() != Texture::NoTexture) {
222     texture.setColor(readDatabaseColor(rname + ".color", default_color,
223                                        style));
224     texture.setColorTo(readDatabaseColor(rname + ".colorTo", default_color,
225                                          style));
226     texture.setBorderColor(readDatabaseColor(rname + ".borderColor",
227                                              default_color, style));
228   }
229
230   return texture;
231 }
232
233
234 Color Style::readDatabaseColor(const std::string &rname,
235                                const std::string &default_color,
236                                const Configuration &style) {
237   Color color;
238   std::string s;
239   if (style.getValue(rname, s))
240     color = Color(s, screen_number);
241   else
242     color = Color(default_color, screen_number);
243   return color;
244 }
245
246
247 Font *Style::readDatabaseFont(const std::string &rbasename,
248                               const Configuration &style) {
249   std::string fontstring, s;
250
251   // XXX: load all this font stuff from the style...
252
253   bool dropShadow = True;
254     
255   unsigned char offset = 1;
256   if (style.getValue(rbasename + "xft.shadow.offset", s)) {
257     offset = atoi(s.c_str()); //doesn't detect errors
258     if (offset > CHAR_MAX)
259       offset = 1;
260   }
261
262   unsigned char tint = 0x40;
263   if (style.getValue(rbasename + "xft.shadow.tint", s)) {
264     tint = atoi(s.c_str());
265   }
266
267   fontstring = "Arial,Sans-9:bold";
268
269   // if this fails, it ::exit()'s
270   return new Font(screen_number, fontstring, dropShadow, offset, tint);
271 }
272
273 }