diff options
Diffstat (limited to 'src/Shape.cc')
-rw-r--r-- | src/Shape.cc | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/src/Shape.cc b/src/Shape.cc new file mode 100644 index 0000000..b4ac635 --- /dev/null +++ b/src/Shape.cc | |||
@@ -0,0 +1,215 @@ | |||
1 | // Shape.cc | ||
2 | // Copyright (c) 2003 Henrik Kinnunen (fluxgen(at)users.sourceforge.net) | ||
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 | // $Id: Shape.cc,v 1.7 2003/10/06 06:22:42 rathnor Exp $ | ||
23 | |||
24 | #include "Shape.hh" | ||
25 | #include "FbTk/FbWindow.hh" | ||
26 | #include "FbTk/App.hh" | ||
27 | #include "FbTk/GContext.hh" | ||
28 | |||
29 | #include <cstring> | ||
30 | |||
31 | #include <X11/Xutil.h> | ||
32 | #ifdef SHAPE | ||
33 | #include <X11/extensions/shape.h> | ||
34 | #endif // SHAPE | ||
35 | |||
36 | #include <iostream> | ||
37 | #include <algorithm> | ||
38 | using namespace std; | ||
39 | |||
40 | namespace { | ||
41 | |||
42 | Pixmap createShape(FbTk::FbWindow &win, int place) { | ||
43 | if (win.window() == 0 || place == 0 || | ||
44 | win.width() < 3 || win.height() < 3) | ||
45 | return 0; | ||
46 | |||
47 | static char left_bits[] = { 0xc0, 0xf8, 0xfc, 0xfe, 0xfe, 0xfe, 0xff, 0xff }; | ||
48 | static char right_bits[] = { 0x03, 0x1f, 0x3f, 0x7f, 0x7f, 0x7f, 0xff, 0xff}; | ||
49 | static char bottom_left_bits[] = { 0xff, 0xff, 0xfe, 0xfe, 0xfe, 0xfc, 0xf8, 0xc0 }; | ||
50 | static char bottom_right_bits[] = { 0xff, 0xff, 0x7f, 0x7f, 0x7f, 0x3f, 0x1f, 0x03 }; | ||
51 | |||
52 | const int borderw = win.borderWidth(); | ||
53 | const int win_width = win.width() + 2*borderw; | ||
54 | const int win_height = win.height() + 2*borderw; | ||
55 | const int pixmap_width = min(8, win_width); | ||
56 | const int pixmap_height = min(8, win_height); | ||
57 | |||
58 | Display *disp = FbTk::App::instance()->display(); | ||
59 | const size_t data_size = win_width * win_height; | ||
60 | char *data = new char[data_size]; | ||
61 | memset(data, 0xFF, data_size); | ||
62 | |||
63 | XImage *ximage = XCreateImage(disp, | ||
64 | DefaultVisual(disp, win.screenNumber()), | ||
65 | 1, | ||
66 | XYPixmap, 0, | ||
67 | data, | ||
68 | win_width, win_height, | ||
69 | 32, 0); | ||
70 | if (ximage == 0) | ||
71 | return 0; | ||
72 | |||
73 | XInitImage(ximage); | ||
74 | |||
75 | // shape corners | ||
76 | |||
77 | if (place & Shape::TOPLEFT) { | ||
78 | for (int y=0; y<pixmap_height; y++) { | ||
79 | for (int x=0; x<pixmap_width; x++) { | ||
80 | XPutPixel(ximage, x, y, (left_bits[y] & (0x01 << x)) ? 1 : 0); | ||
81 | } | ||
82 | } | ||
83 | } | ||
84 | |||
85 | if (place & Shape::TOPRIGHT) { | ||
86 | for (int y=0; y<pixmap_height; y++) { | ||
87 | for (int x=0; x<pixmap_width; x++) { | ||
88 | XPutPixel(ximage, x + win_width - pixmap_width, y, | ||
89 | (right_bits[y] & (0x01 << x)) ? 1 : 0); | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | |||
94 | if (place & Shape::BOTTOMLEFT) { | ||
95 | for (int y=0; y<pixmap_height; y++) { | ||
96 | for (int x=0; x<pixmap_width; x++) { | ||
97 | XPutPixel(ximage, x, y + win_height - pixmap_height, | ||
98 | (bottom_left_bits[y] & (0x01 << x)) ? 1 : 0); | ||
99 | } | ||
100 | } | ||
101 | } | ||
102 | |||
103 | if (place & Shape::BOTTOMRIGHT) { | ||
104 | for (int y=0; y<pixmap_height; y++) { | ||
105 | for (int x=0; x<pixmap_width; x++) { | ||
106 | XPutPixel(ximage, x + win_width - pixmap_width, y + win_height - pixmap_height, | ||
107 | (bottom_right_bits[y] & (0x01 << x)) ? 1 : 0); | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | Pixmap pm = XCreatePixmap(disp, win.window(), win_width, win_height, 1); | ||
113 | |||
114 | FbTk::GContext gc(pm); | ||
115 | |||
116 | XPutImage(disp, pm, gc.gc(), ximage, 0, 0, 0, 0, | ||
117 | win_width, win_height); | ||
118 | |||
119 | XDestroyImage(ximage); | ||
120 | |||
121 | return pm; | ||
122 | |||
123 | |||
124 | } | ||
125 | |||
126 | }; // end anonymous namespace | ||
127 | |||
128 | Shape::Shape(FbTk::FbWindow &win, int shapeplaces): | ||
129 | m_win(&win), | ||
130 | m_shapeplaces(shapeplaces) { | ||
131 | |||
132 | m_shape = createShape(win, shapeplaces); | ||
133 | m_width = win.width(); | ||
134 | m_height = win.height(); | ||
135 | |||
136 | } | ||
137 | |||
138 | Shape::~Shape() { | ||
139 | if (m_shape != 0) | ||
140 | XFreePixmap(FbTk::App::instance()->display(), m_shape); | ||
141 | |||
142 | #ifdef SHAPE | ||
143 | if (m_win != 0 && m_win->window()) { | ||
144 | // reset shape of window | ||
145 | XShapeCombineMask(FbTk::App::instance()->display(), | ||
146 | m_win->window(), | ||
147 | ShapeBounding, | ||
148 | 0, 0, | ||
149 | 0, | ||
150 | ShapeSet); | ||
151 | } | ||
152 | #endif // SHAPE | ||
153 | } | ||
154 | |||
155 | void Shape::setPlaces(int shapeplaces) { | ||
156 | m_shapeplaces = shapeplaces; | ||
157 | } | ||
158 | |||
159 | void Shape::update() { | ||
160 | if (m_win == 0 || m_win->window() == 0) | ||
161 | return; | ||
162 | #ifdef SHAPE | ||
163 | if (m_win->width() != m_width || | ||
164 | m_win->height() != m_height) { | ||
165 | if (m_shape != 0) | ||
166 | XFreePixmap(FbTk::App::instance()->display(), m_shape); | ||
167 | m_shape = createShape(*m_win, m_shapeplaces); | ||
168 | m_width = m_win->width(); | ||
169 | m_height = m_win->height(); | ||
170 | |||
171 | } | ||
172 | |||
173 | // the m_shape can be = 0 which will just reset the shape mask | ||
174 | // and make the window normal | ||
175 | XShapeCombineMask(FbTk::App::instance()->display(), | ||
176 | m_win->window(), | ||
177 | ShapeBounding, | ||
178 | -m_win->borderWidth(), -m_win->borderWidth(), | ||
179 | m_shape, | ||
180 | ShapeSet); | ||
181 | |||
182 | #endif // SHAPE | ||
183 | |||
184 | } | ||
185 | |||
186 | void Shape::setWindow(FbTk::FbWindow &win) { | ||
187 | m_win = &win; | ||
188 | update(); | ||
189 | } | ||
190 | |||
191 | void Shape::setShapeNotify(const FbTk::FbWindow &win) { | ||
192 | #ifdef SHAPE | ||
193 | XShapeSelectInput(FbTk::App::instance()->display(), | ||
194 | win.window(), ShapeNotifyMask); | ||
195 | #endif // SHAPE | ||
196 | } | ||
197 | |||
198 | bool Shape::isShaped(const FbTk::FbWindow &win) { | ||
199 | int shaped = 0; | ||
200 | |||
201 | #ifdef SHAPE | ||
202 | int not_used; | ||
203 | unsigned int not_used2; | ||
204 | XShapeQueryExtents(FbTk::App::instance()->display(), | ||
205 | win.window(), | ||
206 | &shaped, /// bShaped | ||
207 | ¬_used, ¬_used, // xbs, ybs | ||
208 | ¬_used2, ¬_used2, // wbs, hbs | ||
209 | ¬_used, // cShaped | ||
210 | ¬_used, ¬_used, // xcs, ycs | ||
211 | ¬_used2, ¬_used2); // wcs, hcs | ||
212 | #endif // SHAPE | ||
213 | |||
214 | return (shaped != 0 ? true : false); | ||
215 | } | ||