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