1 // WinButton.cc for Fluxbox Window Manager
2 // Copyright (c) 2003 - 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 // DEALINGS IN THE SOFTWARE.
21 
22 #include "WinButton.hh"
23 #include "Window.hh"
24 #include "WindowCmd.hh"
25 #include "Screen.hh"
26 #include "WinClient.hh"
27 #include "WinButtonTheme.hh"
28 #include "FbTk/App.hh"
29 #include "FbTk/Color.hh"
30 
31 #ifdef SHAPE
32 #include <X11/extensions/shape.h>
33 #endif // SHAPE
34 
35 
WinButton(FluxboxWindow & listen_to,FbTk::ThemeProxy<WinButtonTheme> & theme,FbTk::ThemeProxy<WinButtonTheme> & pressed,Type buttontype,const FbTk::FbWindow & parent,int x,int y,unsigned int width,unsigned int height)36 WinButton::WinButton(FluxboxWindow &listen_to,
37                      FbTk::ThemeProxy<WinButtonTheme> &theme,
38                      FbTk::ThemeProxy<WinButtonTheme> &pressed,
39                      Type buttontype, const FbTk::FbWindow &parent,
40                      int x, int y,
41                      unsigned int width, unsigned int height):
42     FbTk::Button(parent, x, y, width, height),
43     m_type(buttontype), m_listen_to(listen_to),
44     m_theme(theme), m_pressed_theme(pressed),
45     m_icon_pixmap(0), m_icon_mask(0),
46     overrode_bg(false), overrode_pressed(false) {
47 
48     join(theme.reconfigSig(), FbTk::MemFun(*this, &WinButton::updateAll));
49 
50     if (buttontype == MENUICON)
51         updateAll();
52 }
53 
exposeEvent(XExposeEvent & event)54 void WinButton::exposeEvent(XExposeEvent &event) {
55     FbTk::Button::exposeEvent(event);
56     drawType();
57 }
58 
buttonReleaseEvent(XButtonEvent & event)59 void WinButton::buttonReleaseEvent(XButtonEvent &event) {
60     WinClient *old = WindowCmd<void>::client();
61     WindowCmd<void>::setWindow(&m_listen_to);
62     FbTk::Button::buttonReleaseEvent(event);
63     WindowCmd<void>::setClient(old);
64 }
65 
66 // when someone else tries to set the background, we may override it
setBackgroundPixmap(Pixmap pm)67 void WinButton::setBackgroundPixmap(Pixmap pm) {
68     Pixmap my_pm = getBackgroundPixmap();
69 
70     if (my_pm != 0) {
71         overrode_bg = true;
72         pm = my_pm;
73     } else {
74         overrode_bg = false;
75     }
76 
77     FbTk::Button::setBackgroundPixmap(pm);
78 }
79 
setBackgroundColor(const FbTk::Color & color)80 void WinButton::setBackgroundColor(const FbTk::Color &color) {
81     Pixmap my_pm = getBackgroundPixmap();
82 
83     if (my_pm != 0) {
84         overrode_bg = true;
85         FbTk::Button::setBackgroundPixmap(my_pm);
86     } else {
87         overrode_bg = false;
88         FbTk::Button::setBackgroundColor(color);
89     }
90 }
91 
setPressedPixmap(Pixmap pm)92 void WinButton::setPressedPixmap(Pixmap pm) {
93     Pixmap my_pm = getPressedPixmap();
94 
95     if (my_pm != 0) {
96         overrode_pressed = true;
97         pm = my_pm;
98     } else {
99         overrode_pressed = false;
100     }
101 
102     FbTk::Button::setPressedPixmap(pm);
103 }
104 
setPressedColor(const FbTk::Color & color)105 void WinButton::setPressedColor(const FbTk::Color &color) {
106     Pixmap my_pm = getPressedPixmap();
107 
108     if (my_pm != 0) {
109         overrode_pressed = true;
110         FbTk::Button::setPressedPixmap(my_pm);
111     } else {
112         overrode_pressed = false;
113         FbTk::Button::setPressedColor(color);
114     }
115 }
116 
getPixmap(const FbTk::ThemeProxy<WinButtonTheme> & theme) const117 Pixmap WinButton::getPixmap(const FbTk::ThemeProxy<WinButtonTheme> &theme) const {
118     switch(m_type) {
119     case MAXIMIZE:
120         return theme->maximizePixmap().pixmap().drawable();
121     case MINIMIZE:
122         return theme->iconifyPixmap().pixmap().drawable();
123     case STICK:
124         if (m_listen_to.isStuck())
125             return theme->stuckPixmap().pixmap().drawable();
126         else
127             return theme->stickPixmap().pixmap().drawable();
128     case CLOSE:
129         return theme->closePixmap().pixmap().drawable();
130     case SHADE:
131         if (m_listen_to.isShaded())
132             return theme->unshadePixmap().pixmap().drawable();
133         else
134             return theme->shadePixmap().pixmap().drawable();
135     case MENUICON:
136         if (m_icon_pixmap.drawable())
137             return theme->titlePixmap().pixmap().drawable();
138         else
139             return theme->menuiconPixmap().pixmap().drawable();
140     case LEFT_HALF:
141         return theme->leftHalfPixmap().pixmap().drawable();
142     case RIGHT_HALF:
143         return theme->rightHalfPixmap().pixmap().drawable();
144     default:
145         return None;
146     }
147 }
148 
149 // clear is used to force this to clear the window (e.g. called from clear())
drawType()150 void WinButton::drawType() {
151 
152     int w = width();
153     int h = height();
154     int oddW = w % 2; // if it's odd and we're centring, we need to add one
155     int oddH = h % 2;
156     bool is_pressed = pressed();
157 
158     if (is_pressed && overrode_pressed && !m_icon_pixmap.drawable())
159         return;
160     if (!is_pressed && overrode_bg && !m_icon_pixmap.drawable())
161         return;
162     if (gc() == 0)
163         return;
164 
165     // otherwise draw old style imagery
166     switch (m_type) {
167     case MAXIMIZE:
168         if ((w < 6) || (h < 6)) {
169             return;
170         }
171         drawRectangle(gc(), 2, 2, w - 5, h - 5);
172         drawLine(gc(), 2, 3, w - 3, 3);
173         break;
174 
175     case MINIMIZE:
176         if ((w < 6) || (h < 6)) {
177             return;
178         }
179         drawRectangle(gc(), 2, w - 5, h - 5, 2);
180         break;
181 
182     case STICK: {
183             int s = 4;
184             if (!m_listen_to.isStuck())
185                 s = 8;
186 
187             fillRectangle(gc(), (w / 2) - (w / s), (h / 2) - (h / s),
188                                 2*(w / s) + oddW, 2*(h / s) + oddH);
189         }
190         break;
191 
192     case CLOSE:
193         if ((w < 4) || (h < 4)) {
194             return;
195         }
196         drawLine(gc(), 2, 2, w - 3, h - 3);
197         // I can't figure out why this second one needs a y offset of 1?????
198         // but it does - at least on my box:
199         //   XFree86 Version 4.2.1.1 (Debian 4.2.1-12.1 20031003005825)
200         //   (protocol Version 11, revision 0, vendor release 6600)
201         // But not on mine? It's wonky. Put back to the same coords.
202         //  was width-2, 1 in the second drawline
203         // Perhaps some X versions don't draw the endpoint?
204         // Mine:
205         // XFree86 Version 4.3.0.1 (Debian 4.3.0.dfsg.1-1 20040428170728)
206         // (X Protocol Version 11, Revision 0, Release 6.6)
207 
208         drawLine(gc(), 2, w - 3, h - 3, 2);
209         break;
210 
211     case SHADE: {
212         int size = w - 5 - oddW;
213         if (size < 4) {
214             return;
215         }
216 
217         FbTk::FbDrawable::TriangleType dir = (m_listen_to.isShaded() ? FbTk::FbDrawable::DOWN: FbTk::FbDrawable::UP);
218 
219         drawRectangle(gc(), 2, 2, size, 2);
220 
221         // draw a one-quarter triangle below the rectangle
222         drawTriangle(gc(), dir, 4, 6, size-2, size/2 - 1, 100);
223         break;
224     }
225 
226     case MENUICON:
227         if (m_icon_pixmap.drawable()) {
228 
229             Display* disp = m_listen_to.fbWindow().display();
230 
231             if (m_icon_mask.drawable()) {
232                 XSetClipMask(disp, gc(), m_icon_mask.drawable());
233                 XSetClipOrigin(disp, gc(), 2, 2);
234             }
235 
236             copyArea(m_icon_pixmap.drawable(), gc(),
237                      0, 0, 2, 2,
238                      m_icon_pixmap.width(), m_icon_pixmap.height());
239 
240             if (m_icon_mask.drawable())
241                 XSetClipMask(disp, gc(), None);
242         } else {
243             if ((w < 6) || (h < 6)) {
244                 return;
245             }
246 
247             int y = h / 3;
248             for ( ; y <= h; y += 3) {
249                 drawLine(gc(), w / 4, y, w - (w / 4) - 1, y);
250             }
251             drawRectangle(gc(), 2, 2, w - 5, h - 5);
252         }
253         break;
254 
255     case LEFT_HALF:
256         if ((w < 4) || (h < 5)) {
257             return;
258         }
259         fillRectangle(gc(), 2, 2, (w / 2) - oddW, h - 4);
260         break;
261     case RIGHT_HALF:
262         if ((w < 5) || (h < 5)) {
263             return;
264         }
265         fillRectangle(gc(), w / 2, 2, (w / 2) - 2 + oddW, h - 4);
266         break;
267     }
268 }
269 
clear()270 void WinButton::clear() {
271     FbTk::Button::clear();
272     drawType();
273 }
updateAll()274 void WinButton::updateAll() {
275 
276     int w = static_cast<int>(width()) - 4;
277     int h = static_cast<int>(height()) - 4;
278 
279     // update the menu icon
280     if ((w > 0 && h > 0) && m_type == MENUICON && !m_listen_to.empty()) {
281 
282         Display* display = m_listen_to.fbWindow().display();
283         int screen = m_listen_to.screen().screenNumber();
284 
285         Drawable d = m_listen_to.icon().pixmap().drawable();
286         if (d != None) {
287              m_icon_pixmap.copy(d, DefaultDepth(display, screen), screen);
288              m_icon_pixmap.scale(w, h);
289         } else
290             m_icon_pixmap.release();
291 
292         d = m_listen_to.icon().mask().drawable();
293         if (d != None) {
294             m_icon_mask.copy(d, 0, 0);
295             m_icon_mask.scale(w, h);
296         } else
297             m_icon_mask.release();
298 
299     }
300 
301     // pressed_pixmap isn't stateful in any current buttons, so no need
302     // to potentially override that. Just make sure background pm is ok
303     Pixmap my_pm = getBackgroundPixmap();
304     if (my_pm != None)
305         setBackgroundPixmap(my_pm);
306 
307     // incorrect, pressed_pixmap is stateful in shade, so we'll do oneoff for now
308     if (m_type == SHADE || m_type == STICK) {
309         Pixmap p_pm = getPressedPixmap();
310         if (p_pm != None)
311             setPressedPixmap(p_pm);
312     }
313 
314     clear();
315 }
316