]> icculus.org git repositories - mikachu/openbox.git/blob - otk/style.cc
xft2 works. and works good.
[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(BImageControl *ctrl)
21   : image_control(ctrl), font(0),
22     screen_number(ctrl->getScreenInfo()->getScreenNumber())
23 {
24 }
25
26 Style::~Style() {
27   if (font)
28     delete font;
29
30   if (close_button.mask != None)
31     XFreePixmap(OBDisplay::display, close_button.mask);
32   if (max_button.mask != None)
33     XFreePixmap(OBDisplay::display, max_button.mask);
34   if (icon_button.mask != None)
35     XFreePixmap(OBDisplay::display, icon_button.mask);
36   if (stick_button.mask != None)
37     XFreePixmap(OBDisplay::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   b_pressed = readDatabaseTexture("window.button.pressed", "black", style);
70
71   //if neither of these can be found, we will use the previous resource
72   b_pressed_focus = readDatabaseTexture("window.button.pressed.focus",
73                                         "black", style, true);
74   b_pressed_unfocus = readDatabaseTexture("window.button.pressed.unfocus",
75                                           "black", style, true);
76
77   if (close_button.mask != None)
78     XFreePixmap(OBDisplay::display, close_button.mask);
79   if (max_button.mask != None)
80     XFreePixmap(OBDisplay::display, max_button.mask);
81   if (icon_button.mask != None)
82     XFreePixmap(OBDisplay::display, icon_button.mask);
83   if (stick_button.mask != None)
84     XFreePixmap(OBDisplay::display, stick_button.mask);
85
86   close_button.mask = max_button.mask = icon_button.mask
87                     = icon_button.mask = None;
88   
89   readDatabaseMask("window.button.close.mask", close_button, style);
90   readDatabaseMask("window.button.max.mask", max_button, style);
91   readDatabaseMask("window.button.icon.mask", icon_button, style);
92   readDatabaseMask("window.button.stick.mask", stick_button, style);
93
94   // we create the window.frame texture by hand because it exists only to
95   // make the code cleaner and is not actually used for display
96   BColor color = readDatabaseColor("window.frame.focusColor", "white",
97                                         style);
98   f_focus = BTexture("solid flat", screen_number, image_control);
99   f_focus.setColor(color);
100
101   color = readDatabaseColor("window.frame.unfocusColor", "white", style);
102   f_unfocus = BTexture("solid flat", screen_number, image_control);
103   f_unfocus.setColor(color);
104
105   l_text_focus = readDatabaseColor("window.label.focus.textColor",
106                                    "black", style);
107   l_text_unfocus = readDatabaseColor("window.label.unfocus.textColor",
108                                      "white", style);
109
110   b_pic_focus = readDatabaseColor("window.button.focus.picColor",
111                                   "black", style);
112   b_pic_unfocus = readDatabaseColor("window.button.unfocus.picColor",
113                                     "white", style);
114
115   justify = LeftJustify;
116
117   if (style.getValue("window.justify", s)) {
118     if (s == "right" || s == "Right")
119       justify = RightJustify;
120     else if (s == "center" || s == "Center")
121       justify = CenterJustify;
122   }
123
124   // sanity checks
125   if (t_focus.texture() == BTexture::Parent_Relative)
126     t_focus = f_focus;
127   if (t_unfocus.texture() == BTexture::Parent_Relative)
128     t_unfocus = f_unfocus;
129   if (h_focus.texture() == BTexture::Parent_Relative)
130     h_focus = f_focus;
131   if (h_unfocus.texture() == BTexture::Parent_Relative)
132     h_unfocus = f_unfocus;
133
134   border_color = readDatabaseColor("borderColor", "black", style);
135
136   // load bevel, border and handle widths
137
138   const ScreenInfo *s_info = OBDisplay::screenInfo(screen_number);
139   unsigned int width = s_info->getRect().width();
140
141   if (! style.getValue("handleWidth", handle_width) ||
142       handle_width > width/2 || handle_width == 0)
143     handle_width = 6;
144
145   if (! style.getValue("borderWidth", border_width))
146     border_width = 1;
147
148   if (! style.getValue("bevelWidth", bevel_width)
149       || bevel_width > width/2 || bevel_width == 0)
150     bevel_width = 3;
151
152   if (! style.getValue("frameWidth", frame_width)
153       || frame_width > width/2)
154     frame_width = bevel_width;
155
156   if (style.getValue("rootCommand", s))
157     bexec(s, s_info->displayString());
158 }
159
160
161 void Style::readDatabaseMask(const std::string &rname, PixmapMask &pixmapMask,
162                              const Configuration &style) {
163   Window root_window = OBDisplay::screenInfo(screen_number)->getRootWindow();
164   std::string s;
165   int hx, hy; //ignored
166   int ret = BitmapOpenFailed; //default to failure.
167   
168   if (style.getValue(rname, s))
169   {
170     if (s[0] != '/' && s[0] != '~')
171     {
172       std::string xbmFile = std::string("~/.openbox/buttons/") + s;
173       ret = XReadBitmapFile(OBDisplay::display, root_window,
174                             expandTilde(xbmFile).c_str(), &pixmapMask.w,
175                             &pixmapMask.h, &pixmapMask.mask, &hx, &hy);
176     } else
177       ret = XReadBitmapFile(OBDisplay::display, root_window,
178                             expandTilde(s).c_str(), &pixmapMask.w,
179                             &pixmapMask.h, &pixmapMask.mask, &hx, &hy);
180     
181     if (ret == BitmapSuccess)
182       return;
183   }
184
185   pixmapMask.mask = None;
186   pixmapMask.w = pixmapMask.h = 0;
187 }
188
189
190 BTexture Style::readDatabaseTexture(const std::string &rname,
191                                          const std::string &default_color,
192                                          const Configuration &style, 
193                                          bool allowNoTexture)
194 {
195   BTexture texture;
196   std::string s;
197
198   if (style.getValue(rname, s))
199     texture = BTexture(s);
200   else if (allowNoTexture) //no default
201     texture.setTexture(BTexture::NoTexture);
202   else
203     texture.setTexture(BTexture::Solid | BTexture::Flat);
204
205   // associate this texture with this screen
206   texture.setScreen(screen_number);
207   texture.setImageControl(image_control);
208
209   if (texture.texture() != BTexture::NoTexture) {
210     texture.setColor(readDatabaseColor(rname + ".color", default_color,
211                                        style));
212     texture.setColorTo(readDatabaseColor(rname + ".colorTo", default_color,
213                                          style));
214     texture.setBorderColor(readDatabaseColor(rname + ".borderColor",
215                                              default_color, style));
216   }
217
218   return texture;
219 }
220
221
222 BColor Style::readDatabaseColor(const std::string &rname,
223                                      const std::string &default_color,
224                                      const Configuration &style) {
225   BColor color;
226   std::string s;
227   if (style.getValue(rname, s))
228     color = BColor(s, screen_number);
229   else
230     color = BColor(default_color, screen_number);
231   return color;
232 }
233
234
235 BFont *Style::readDatabaseFont(const std::string &rbasename,
236                                const Configuration &style) {
237   std::string fontstring, s;
238
239   // XXX: load all this font stuff from the style...
240
241   bool dropShadow = True;
242     
243   unsigned char offset = 1;
244   if (style.getValue(rbasename + "xft.shadow.offset", s)) {
245     offset = atoi(s.c_str()); //doesn't detect errors
246     if (offset > CHAR_MAX)
247       offset = 1;
248   }
249
250   unsigned char tint = 0x40;
251   if (style.getValue(rbasename + "xft.shadow.tint", s)) {
252     tint = atoi(s.c_str());
253   }
254
255   fontstring = "Arial,Sans-8:bold";
256
257   // if this fails, it ::exit()'s
258   return new BFont(screen_number, fontstring, dropShadow, offset, tint);
259 }
260
261 }