aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/FbWindow.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/FbWindow.cc')
-rw-r--r--src/FbTk/FbWindow.cc451
1 files changed, 451 insertions, 0 deletions
diff --git a/src/FbTk/FbWindow.cc b/src/FbTk/FbWindow.cc
new file mode 100644
index 0000000..a16fbb6
--- /dev/null
+++ b/src/FbTk/FbWindow.cc
@@ -0,0 +1,451 @@
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.28 2003/10/26 20:52:28 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
39namespace FbTk {
40
41namespace {
42Pixmap 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
66Display *FbWindow::s_display = 0;
67
68FbWindow::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
76FbWindow::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
89FbWindow::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
105FbWindow::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
120FbWindow::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
135FbWindow::~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
146void FbWindow::setBackgroundColor(const FbTk::Color &bg_color) {
147 XSetWindowBackground(s_display, m_window, bg_color.pixel());
148}
149
150void FbWindow::setBackgroundPixmap(Pixmap bg_pixmap) {
151 XSetWindowBackgroundPixmap(s_display, m_window, bg_pixmap);
152}
153
154void FbWindow::setBorderColor(const FbTk::Color &border_color) {
155 XSetWindowBorder(s_display, m_window, border_color.pixel());
156}
157
158void FbWindow::setBorderWidth(unsigned int size) {
159 XSetWindowBorderWidth(s_display, m_window, size);
160 m_border_width = size;
161}
162
163void FbWindow::setName(const char *name) {
164 XStoreName(s_display, m_window, name);
165}
166
167void FbWindow::setEventMask(long mask) {
168 XSelectInput(s_display, m_window, mask);
169}
170
171void FbWindow::clear() {
172 XClearWindow(s_display, m_window);
173}
174
175void 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
181void 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
235void 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
247FbWindow &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
262FbWindow &FbWindow::operator = (Window win) {
263 setNew(win);
264 return *this;
265}
266
267void 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
296void FbWindow::show() {
297 XMapWindow(s_display, m_window);
298}
299
300void FbWindow::showSubwindows() {
301 XMapSubwindows(s_display, m_window);
302}
303
304void FbWindow::hide() {
305 XUnmapWindow(s_display, m_window);
306}
307
308void FbWindow::move(int x, int y) {
309 XMoveWindow(s_display, m_window, x, y);
310 m_x = x;
311 m_y = y;
312}
313
314void FbWindow::resize(unsigned int width, unsigned int height) {
315 XResizeWindow(s_display, m_window, width, height);
316 m_width = width;
317 m_height = height;
318}
319
320void FbWindow::moveResize(int x, int y, unsigned int width, unsigned int height) {
321 XMoveResizeWindow(s_display, m_window, x, y, width, height);
322 m_x = x;
323 m_y = y;
324 m_width = width;
325 m_height = height;
326}
327
328void FbWindow::lower() {
329 XLowerWindow(s_display, window());
330}
331
332void FbWindow::raise() {
333 XRaiseWindow(s_display, window());
334}
335
336void FbWindow::setInputFocus(int revert_to, int time) {
337 XSetInputFocus(s_display, window(), revert_to, time);
338}
339
340void FbWindow::setCursor(Cursor cur) {
341 XDefineCursor(s_display, window(), cur);
342}
343
344void FbWindow::unsetCursor() {
345 XUndefineCursor(s_display, window());
346}
347
348void FbWindow::reparent(const FbWindow &parent, int x, int y) {
349 XReparentWindow(s_display, window(), parent.window(), x, y);
350 m_parent = &parent;
351 updateGeometry();
352}
353
354bool FbWindow::property(Atom property,
355 long long_offset, long long_length,
356 bool do_delete,
357 Atom req_type,
358 Atom *actual_type_return,
359 int *actual_format_return,
360 unsigned long *nitems_return,
361 unsigned long *bytes_after_return,
362 unsigned char **prop_return) const {
363 if (XGetWindowProperty(s_display, window(),
364 property, long_offset, long_length, do_delete,
365 req_type, actual_type_return,
366 actual_format_return, nitems_return,
367 bytes_after_return, prop_return) == Success)
368 return true;
369
370 return false;
371}
372
373void FbWindow::changeProperty(Atom property, Atom type,
374 int format,
375 int mode,
376 unsigned char *data,
377 int nelements) {
378
379 XChangeProperty(s_display, m_window, property, type,
380 format, mode,
381 data, nelements);
382}
383
384int FbWindow::screenNumber() const {
385 return m_screen_num;
386}
387
388long FbWindow::eventMask() const {
389 XWindowAttributes attrib;
390 if (XGetWindowAttributes(s_display, window(),
391 &attrib) == Success) {
392 return attrib.your_event_mask;
393 }
394 return 0;
395}
396
397void FbWindow::setBufferPixmap(Pixmap pm) {
398 m_buffer_pm = pm;
399}
400
401void FbWindow::updateGeometry() {
402 if (m_window == 0)
403 return;
404
405 Window root;
406 unsigned int border_width, depth;
407 XGetGeometry(s_display, m_window, &root, &m_x, &m_y,
408 (unsigned int *)&m_width, (unsigned int *)&m_height,
409 &border_width, &depth);
410 m_depth = depth;
411}
412
413void FbWindow::create(Window parent, int x, int y,
414 unsigned int width, unsigned int height,
415 long eventmask, bool override_redirect,
416 int depth, int class_type) {
417
418
419 if (s_display == 0)
420 s_display = FbTk::App::instance()->display();
421
422 m_border_width = 0;
423
424 long valmask = CWEventMask;
425 XSetWindowAttributes values;
426 values.event_mask = eventmask;
427
428 if (override_redirect) {
429 valmask |= CWOverrideRedirect;
430 values.override_redirect = True;
431 }
432
433 m_window = XCreateWindow(s_display, parent, x, y, width, height,
434 0, // border width
435 depth, // depth
436 class_type, // class
437 CopyFromParent, // visual
438 valmask, // create mask
439 &values); // create atrribs
440
441 assert(m_window);
442
443 updateGeometry();
444 FbWindow::setBackgroundColor(Color("gray", screenNumber()));
445}
446
447bool operator == (Window win, const FbWindow &fbwin) {
448 return win == fbwin.window();
449}
450
451};