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