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