diff options
Diffstat (limited to 'src/FbTk/FbWindow.cc')
-rw-r--r-- | src/FbTk/FbWindow.cc | 458 |
1 files changed, 458 insertions, 0 deletions
diff --git a/src/FbTk/FbWindow.cc b/src/FbTk/FbWindow.cc new file mode 100644 index 0000000..aa1705d --- /dev/null +++ b/src/FbTk/FbWindow.cc | |||
@@ -0,0 +1,458 @@ | |||
1 | // FbWindow.cc for FbTk - fluxbox toolkit | ||
2 | // Copyright (c) 2002-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: FbWindow.cc,v 1.31 2004/01/21 20:07:41 fluxgen Exp $ | ||
23 | |||
24 | #include "FbWindow.hh" | ||
25 | |||
26 | #include "EventManager.hh" | ||
27 | #include "Color.hh" | ||
28 | #include "App.hh" | ||
29 | #include "Transparent.hh" | ||
30 | |||
31 | #ifdef HAVE_CONFIG_H | ||
32 | #include "config.h" | ||
33 | #endif // HAVE_CONFIG_H | ||
34 | |||
35 | #include <X11/Xatom.h> | ||
36 | |||
37 | #include <cassert> | ||
38 | #include <iostream> | ||
39 | using namespace std; | ||
40 | |||
41 | namespace FbTk { | ||
42 | |||
43 | namespace { | ||
44 | Pixmap getRootPixmap(int screen_num) { | ||
45 | Pixmap root_pm = 0; | ||
46 | // get root pixmap for transparency | ||
47 | Display *disp = FbTk::App::instance()->display(); | ||
48 | Atom real_type; | ||
49 | int real_format; | ||
50 | unsigned long items_read, items_left; | ||
51 | unsigned int *data; | ||
52 | if (XGetWindowProperty(disp, RootWindow(disp, screen_num), | ||
53 | XInternAtom(disp, "_XROOTPMAP_ID", false), | ||
54 | 0L, 1L, | ||
55 | false, XA_PIXMAP, &real_type, | ||
56 | &real_format, &items_read, &items_left, | ||
57 | (unsigned char **) &data) == Success && | ||
58 | items_read) { | ||
59 | root_pm = (Pixmap) (*data); | ||
60 | XFree(data); | ||
61 | } | ||
62 | |||
63 | return root_pm; | ||
64 | } | ||
65 | |||
66 | }; // end anonymous namespace | ||
67 | |||
68 | Display *FbWindow::s_display = 0; | ||
69 | |||
70 | FbWindow::FbWindow():m_parent(0), m_screen_num(0), m_window(0), m_x(0), m_y(0), | ||
71 | m_width(0), m_height(0), m_border_width(0), m_depth(0), m_destroy(true), | ||
72 | m_buffer_pm(0) { | ||
73 | |||
74 | if (s_display == 0) | ||
75 | s_display = App::instance()->display(); | ||
76 | } | ||
77 | |||
78 | FbWindow::FbWindow(const FbWindow& the_copy):m_parent(the_copy.parent()), | ||
79 | m_screen_num(the_copy.screenNumber()), m_window(the_copy.window()), | ||
80 | m_x(the_copy.x()), m_y(the_copy.y()), | ||
81 | m_width(the_copy.width()), m_height(the_copy.height()), | ||
82 | m_border_width(the_copy.borderWidth()), | ||
83 | m_depth(the_copy.depth()), m_destroy(true), | ||
84 | m_buffer_pm(0) { | ||
85 | if (s_display == 0) | ||
86 | s_display = App::instance()->display(); | ||
87 | |||
88 | the_copy.m_window = 0; | ||
89 | } | ||
90 | |||
91 | FbWindow::FbWindow(int screen_num, | ||
92 | int x, int y, | ||
93 | unsigned int width, unsigned int height, | ||
94 | long eventmask, | ||
95 | bool override_redirect, | ||
96 | int depth, | ||
97 | int class_type): | ||
98 | m_parent(0), | ||
99 | m_screen_num(screen_num), | ||
100 | m_destroy(true), | ||
101 | m_buffer_pm(0) { | ||
102 | |||
103 | create(RootWindow(FbTk::App::instance()->display(), screen_num), | ||
104 | x, y, width, height, eventmask, | ||
105 | override_redirect, depth, class_type); | ||
106 | }; | ||
107 | |||
108 | FbWindow::FbWindow(const FbWindow &parent, | ||
109 | int x, int y, unsigned int width, unsigned int height, | ||
110 | long eventmask, | ||
111 | bool override_redirect, | ||
112 | int depth, int class_type): | ||
113 | m_parent(&parent), | ||
114 | m_screen_num(parent.screenNumber()), | ||
115 | m_destroy(true), | ||
116 | m_buffer_pm(0) { | ||
117 | |||
118 | create(parent.window(), x, y, width, height, eventmask, | ||
119 | override_redirect, depth, class_type); | ||
120 | |||
121 | |||
122 | }; | ||
123 | |||
124 | FbWindow::FbWindow(Window client):m_parent(0), | ||
125 | m_screen_num(0), | ||
126 | m_window(0), | ||
127 | m_x(0), m_y(0), | ||
128 | m_width(1), m_height(1), | ||
129 | m_border_width(0), | ||
130 | m_depth(0), | ||
131 | m_destroy(false), // don't destroy this window | ||
132 | m_buffer_pm(0) { | ||
133 | |||
134 | if (s_display == 0) | ||
135 | s_display = App::instance()->display(); | ||
136 | |||
137 | setNew(client); | ||
138 | } | ||
139 | |||
140 | FbWindow::~FbWindow() { | ||
141 | if (m_window != 0 && m_destroy) { | ||
142 | FbTk::EventManager::instance()->remove(m_window); | ||
143 | XDestroyWindow(s_display, m_window); | ||
144 | } | ||
145 | } | ||
146 | |||
147 | |||
148 | void FbWindow::setBackgroundColor(const FbTk::Color &bg_color) { | ||
149 | XSetWindowBackground(s_display, m_window, bg_color.pixel()); | ||
150 | } | ||
151 | |||
152 | void FbWindow::setBackgroundPixmap(Pixmap bg_pixmap) { | ||
153 | XSetWindowBackgroundPixmap(s_display, m_window, bg_pixmap); | ||
154 | } | ||
155 | |||
156 | void FbWindow::setBorderColor(const FbTk::Color &border_color) { | ||
157 | XSetWindowBorder(s_display, m_window, border_color.pixel()); | ||
158 | } | ||
159 | |||
160 | void FbWindow::setBorderWidth(unsigned int size) { | ||
161 | XSetWindowBorderWidth(s_display, m_window, size); | ||
162 | m_border_width = size; | ||
163 | } | ||
164 | |||
165 | void FbWindow::setName(const char *name) { | ||
166 | XStoreName(s_display, m_window, name); | ||
167 | } | ||
168 | |||
169 | void FbWindow::setEventMask(long mask) { | ||
170 | XSelectInput(s_display, m_window, mask); | ||
171 | } | ||
172 | |||
173 | void FbWindow::clear() { | ||
174 | XClearWindow(s_display, m_window); | ||
175 | updateTransparent(); | ||
176 | } | ||
177 | |||
178 | void FbWindow::clearArea(int x, int y, | ||
179 | unsigned int width, unsigned int height, | ||
180 | bool exposures) { | ||
181 | XClearArea(s_display, window(), x, y, width, height, exposures); | ||
182 | updateTransparent(x, y, width, height); | ||
183 | } | ||
184 | |||
185 | void FbWindow::updateTransparent(int the_x, int the_y, unsigned int the_width, unsigned int the_height) { | ||
186 | #ifdef HAVE_XRENDER | ||
187 | if (width() == 0 || height() == 0) | ||
188 | return; | ||
189 | |||
190 | if (the_width == 0 || the_height == 0) { | ||
191 | the_width = width(); | ||
192 | the_height = height(); | ||
193 | } | ||
194 | |||
195 | if (the_x < 0 || the_y < 0) { | ||
196 | the_x = 0; | ||
197 | the_y = 0; | ||
198 | } | ||
199 | |||
200 | if (!m_transparent.get()) | ||
201 | return; | ||
202 | |||
203 | // update source and destination if needed | ||
204 | Pixmap root = getRootPixmap(screenNumber()); | ||
205 | if (m_transparent->source() != root) | ||
206 | m_transparent->setSource(root, screenNumber()); | ||
207 | |||
208 | if (m_buffer_pm) { | ||
209 | if (m_transparent->dest() != m_buffer_pm) { | ||
210 | m_transparent->setDest(m_buffer_pm, screenNumber()); | ||
211 | } | ||
212 | } else if (m_transparent->dest() != window()) | ||
213 | m_transparent->setDest(window(), screenNumber()); | ||
214 | |||
215 | const FbWindow *root_parent = parent(); | ||
216 | // our position in parent ("root") | ||
217 | int root_x = x() + borderWidth(), root_y = y() + borderWidth(); | ||
218 | if (root_parent != 0) { | ||
219 | root_x += root_parent->x() + root_parent->borderWidth(); | ||
220 | root_y += root_parent->y() + root_parent->borderWidth(); | ||
221 | while (root_parent->parent() != 0) { | ||
222 | root_parent = root_parent->parent(); | ||
223 | root_x += root_parent->x() + root_parent->borderWidth(); | ||
224 | root_y += root_parent->y() + root_parent->borderWidth(); | ||
225 | } | ||
226 | |||
227 | } // else toplevel window so we already have x, y set | ||
228 | /* | ||
229 | // get root position | ||
230 | int root_x = 0, root_y = 0; | ||
231 | Window child_return; | ||
232 | XTranslateCoordinates(s_display, | ||
233 | RootWindow(s_display, screenNumber()), // dest | ||
234 | window(), // src | ||
235 | borderWidth(), borderWidth(), | ||
236 | &root_x, &root_y, | ||
237 | &child_return); | ||
238 | |||
239 | root_x = -root_x; | ||
240 | root_y = -root_y; | ||
241 | */ | ||
242 | // render background image from root pos to our window | ||
243 | m_transparent->render(root_x + the_x, root_y + the_y, | ||
244 | the_x, the_y, | ||
245 | the_width, the_height); | ||
246 | #endif // HAVE_XRENDER | ||
247 | } | ||
248 | |||
249 | void FbWindow::setAlpha(unsigned char alpha) { | ||
250 | #ifdef HAVE_XRENDER | ||
251 | if (m_transparent.get() == 0 && alpha != 0) { | ||
252 | m_transparent.reset(new Transparent(getRootPixmap(screenNumber()), window(), alpha, screenNumber())); | ||
253 | } else if (alpha != 0 && alpha != m_transparent->alpha()) | ||
254 | m_transparent->setAlpha(alpha); | ||
255 | else if (alpha == 0) | ||
256 | m_transparent.reset(0); // destroy transparent object | ||
257 | #endif // HAVE_XRENDER | ||
258 | } | ||
259 | |||
260 | |||
261 | FbWindow &FbWindow::operator = (const FbWindow &win) { | ||
262 | m_parent = win.parent(); | ||
263 | m_screen_num = win.screenNumber(); | ||
264 | m_window = win.window(); | ||
265 | m_x = win.x(); | ||
266 | m_y = win.y(); | ||
267 | m_width = win.width(); | ||
268 | m_height = win.height(); | ||
269 | m_border_width = win.borderWidth(); | ||
270 | m_depth = win.depth(); | ||
271 | // take over this window | ||
272 | win.m_window = 0; | ||
273 | return *this; | ||
274 | } | ||
275 | |||
276 | FbWindow &FbWindow::operator = (Window win) { | ||
277 | setNew(win); | ||
278 | return *this; | ||
279 | } | ||
280 | |||
281 | void FbWindow::setNew(Window win) { | ||
282 | if (s_display == 0) | ||
283 | s_display = App::instance()->display(); | ||
284 | |||
285 | if (m_window != 0 && m_destroy) | ||
286 | XDestroyWindow(s_display, m_window); | ||
287 | |||
288 | m_window = win; | ||
289 | |||
290 | if (m_window != 0) { | ||
291 | updateGeometry(); | ||
292 | XWindowAttributes attr; | ||
293 | attr.screen = 0; | ||
294 | //get screen number | ||
295 | if (XGetWindowAttributes(s_display, | ||
296 | m_window, | ||
297 | &attr) != 0 && attr.screen != 0) { | ||
298 | m_screen_num = XScreenNumberOfScreen(attr.screen); | ||
299 | if (attr.width <= 0) | ||
300 | m_width = 1; | ||
301 | else | ||
302 | m_width = attr.width; | ||
303 | |||
304 | if (attr.height <= 0) | ||
305 | m_height = 1; | ||
306 | else | ||
307 | m_height = attr.height; | ||
308 | |||
309 | m_x = attr.x; | ||
310 | m_y = attr.y; | ||
311 | m_depth = attr.depth; | ||
312 | m_border_width = attr.border_width; | ||
313 | } | ||
314 | |||
315 | } | ||
316 | } | ||
317 | |||
318 | void FbWindow::show() { | ||
319 | XMapWindow(s_display, m_window); | ||
320 | } | ||
321 | |||
322 | void FbWindow::showSubwindows() { | ||
323 | XMapSubwindows(s_display, m_window); | ||
324 | } | ||
325 | |||
326 | void FbWindow::hide() { | ||
327 | XUnmapWindow(s_display, m_window); | ||
328 | } | ||
329 | |||
330 | void FbWindow::lower() { | ||
331 | XLowerWindow(s_display, window()); | ||
332 | } | ||
333 | |||
334 | void FbWindow::raise() { | ||
335 | XRaiseWindow(s_display, window()); | ||
336 | } | ||
337 | |||
338 | void FbWindow::setInputFocus(int revert_to, int time) { | ||
339 | XSetInputFocus(s_display, window(), revert_to, time); | ||
340 | } | ||
341 | |||
342 | void FbWindow::setCursor(Cursor cur) { | ||
343 | XDefineCursor(s_display, window(), cur); | ||
344 | } | ||
345 | |||
346 | void FbWindow::unsetCursor() { | ||
347 | XUndefineCursor(s_display, window()); | ||
348 | } | ||
349 | |||
350 | void FbWindow::reparent(const FbWindow &parent, int x, int y) { | ||
351 | XReparentWindow(s_display, window(), parent.window(), x, y); | ||
352 | m_parent = &parent; | ||
353 | updateGeometry(); | ||
354 | } | ||
355 | |||
356 | bool FbWindow::property(Atom property, | ||
357 | long long_offset, long long_length, | ||
358 | bool do_delete, | ||
359 | Atom req_type, | ||
360 | Atom *actual_type_return, | ||
361 | int *actual_format_return, | ||
362 | unsigned long *nitems_return, | ||
363 | unsigned long *bytes_after_return, | ||
364 | unsigned char **prop_return) const { | ||
365 | if (XGetWindowProperty(s_display, window(), | ||
366 | property, long_offset, long_length, do_delete, | ||
367 | req_type, actual_type_return, | ||
368 | actual_format_return, nitems_return, | ||
369 | bytes_after_return, prop_return) == Success) | ||
370 | return true; | ||
371 | |||
372 | return false; | ||
373 | } | ||
374 | |||
375 | void FbWindow::changeProperty(Atom property, Atom type, | ||
376 | int format, | ||
377 | int mode, | ||
378 | unsigned char *data, | ||
379 | int nelements) { | ||
380 | |||
381 | XChangeProperty(s_display, m_window, property, type, | ||
382 | format, mode, | ||
383 | data, nelements); | ||
384 | } | ||
385 | |||
386 | int FbWindow::screenNumber() const { | ||
387 | return m_screen_num; | ||
388 | } | ||
389 | |||
390 | long FbWindow::eventMask() const { | ||
391 | XWindowAttributes attrib; | ||
392 | if (XGetWindowAttributes(s_display, window(), | ||
393 | &attrib) == Success) { | ||
394 | return attrib.your_event_mask; | ||
395 | } | ||
396 | return 0; | ||
397 | } | ||
398 | |||
399 | unsigned char FbWindow::alpha() const { | ||
400 | if (m_transparent.get()) | ||
401 | return m_transparent->alpha(); | ||
402 | return 255; | ||
403 | } | ||
404 | void FbWindow::setBufferPixmap(Pixmap pm) { | ||
405 | m_buffer_pm = pm; | ||
406 | } | ||
407 | |||
408 | void FbWindow::updateGeometry() { | ||
409 | if (m_window == 0) | ||
410 | return; | ||
411 | |||
412 | Window root; | ||
413 | unsigned int border_width, depth; | ||
414 | XGetGeometry(s_display, m_window, &root, &m_x, &m_y, | ||
415 | (unsigned int *)&m_width, (unsigned int *)&m_height, | ||
416 | &border_width, &depth); | ||
417 | m_depth = depth; | ||
418 | } | ||
419 | |||
420 | void FbWindow::create(Window parent, int x, int y, | ||
421 | unsigned int width, unsigned int height, | ||
422 | long eventmask, bool override_redirect, | ||
423 | int depth, int class_type) { | ||
424 | |||
425 | |||
426 | if (s_display == 0) | ||
427 | s_display = FbTk::App::instance()->display(); | ||
428 | |||
429 | m_border_width = 0; | ||
430 | |||
431 | long valmask = CWEventMask; | ||
432 | XSetWindowAttributes values; | ||
433 | values.event_mask = eventmask; | ||
434 | |||
435 | if (override_redirect) { | ||
436 | valmask |= CWOverrideRedirect; | ||
437 | values.override_redirect = True; | ||
438 | } | ||
439 | |||
440 | m_window = XCreateWindow(s_display, parent, x, y, width, height, | ||
441 | 0, // border width | ||
442 | depth, // depth | ||
443 | class_type, // class | ||
444 | CopyFromParent, // visual | ||
445 | valmask, // create mask | ||
446 | &values); // create atrribs | ||
447 | |||
448 | assert(m_window); | ||
449 | |||
450 | updateGeometry(); | ||
451 | FbWindow::setBackgroundColor(Color("gray", screenNumber())); | ||
452 | } | ||
453 | |||
454 | bool operator == (Window win, const FbWindow &fbwin) { | ||
455 | return win == fbwin.window(); | ||
456 | } | ||
457 | |||
458 | }; | ||