]> Dogcows Code - chaz/openbox/blob - otk/widget.cc
c3e17cbe5a181cdcefd8bd97b7c6cceb771348be
[chaz/openbox] / otk / widget.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "widget.hh"
8 #include "display.hh"
9 #include "assassin.hh"
10 #include "screeninfo.hh"
11 #include "focuslabel.hh"
12 #include <algorithm>
13 #include <iostream>
14
15 namespace otk {
16
17 Widget::Widget(Widget *parent, Direction direction)
18 : EventHandler(),
19 _dirty(false), _focused(false),
20 _parent(parent), _style(parent->style()), _direction(direction),
21 _cursor(parent->cursor()), _bevel_width(parent->bevelWidth()),
22 _ignore_config(0),
23 _visible(false), _grabbed_mouse(false),
24 _grabbed_keyboard(false), _stretchable_vert(false),
25 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
26 _bcolor(0), _bwidth(0), _rect(0, 0, 1, 1), _screen(parent->screen()),
27 _fixed_width(false), _fixed_height(false),
28 _surface(0),
29 _event_dispatcher(parent->eventDispatcher())
30 {
31 assert(parent);
32 parent->addChild(this);
33 create();
34 _event_dispatcher->registerHandler(_window, this);
35 }
36
37 Widget::Widget(EventDispatcher *event_dispatcher, RenderStyle *style,
38 Direction direction, Cursor cursor, int bevel_width,
39 bool override_redirect)
40 : EventHandler(),
41 _dirty(false),_focused(false),
42 _parent(0), _style(style), _direction(direction), _cursor(cursor),
43 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
44 _grabbed_mouse(false), _grabbed_keyboard(false),
45 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
46 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0), _rect(0, 0, 1, 1),
47 _screen(style->screen()), _fixed_width(false), _fixed_height(false),
48 _surface(0),
49 _event_dispatcher(event_dispatcher)
50 {
51 assert(event_dispatcher);
52 assert(style);
53 create(override_redirect);
54 _event_dispatcher->registerHandler(_window, this);
55 }
56
57 Widget::~Widget()
58 {
59 if (_visible)
60 hide();
61
62 if (_surface)
63 delete _surface;
64
65 _event_dispatcher->clearHandler(_window);
66
67 std::for_each(_children.begin(), _children.end(), PointerAssassin());
68
69 if (_parent)
70 _parent->removeChild(this);
71
72 XDestroyWindow(**display, _window);
73 }
74
75 void Widget::create(bool override_redirect)
76 {
77 const ScreenInfo *scr_info = display->screenInfo(_screen);
78 Window p_window = _parent ? _parent->window() : scr_info->rootWindow();
79
80 _rect.setRect(0, 0, 1, 1); // just some initial values
81
82 XSetWindowAttributes attrib_create;
83 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
84
85 attrib_create.background_pixmap = None;
86 attrib_create.colormap = scr_info->colormap();
87 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
88 ButtonMotionMask | ExposureMask | StructureNotifyMask;
89
90 if (override_redirect) {
91 create_mask |= CWOverrideRedirect;
92 attrib_create.override_redirect = true;
93 }
94
95 if (_cursor) {
96 create_mask |= CWCursor;
97 attrib_create.cursor = _cursor;
98 }
99
100 _window = XCreateWindow(**display, p_window, _rect.x(),
101 _rect.y(), _rect.width(), _rect.height(), 0,
102 scr_info->depth(), InputOutput,
103 scr_info->visual(), create_mask, &attrib_create);
104 _ignore_config++;
105 }
106
107 void Widget::setWidth(int w)
108 {
109 assert(w > 0);
110 _fixed_width = true;
111 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
112 }
113
114 void Widget::setHeight(int h)
115 {
116 assert(h > 0);
117 _fixed_height = true;
118 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
119 }
120
121 void Widget::move(const Point &to)
122 {
123 move(to.x(), to.y());
124 }
125
126 void Widget::move(int x, int y)
127 {
128 _rect.setPos(x, y);
129 XMoveWindow(**display, _window, x, y);
130 _ignore_config++;
131 }
132
133 void Widget::resize(const Point &to)
134 {
135 resize(to.x(), to.y());
136 }
137
138 void Widget::resize(int w, int h)
139 {
140 assert(w > 0 && h > 0);
141 _fixed_width = _fixed_height = true;
142 setGeometry(_rect.x(), _rect.y(), w, h);
143 }
144
145 void Widget::setGeometry(const Rect &new_geom)
146 {
147 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
148 }
149
150 void Widget::setGeometry(const Point &topleft, int width, int height)
151 {
152 setGeometry(topleft.x(), topleft.y(), width, height);
153 }
154
155 void Widget::setGeometry(int x, int y, int width, int height)
156 {
157 _rect = Rect(x, y, width, height);
158 _dirty = true;
159
160 // make all parents dirty too
161 Widget *p = _parent;
162 while (p) {
163 p->_dirty = true;
164 p = p->_parent;
165 }
166
167 // don't use an XMoveResizeWindow here, because it doesn't seem to move
168 // windows with StaticGravity? This works, that didn't.
169 XResizeWindow(**display, _window, width, height);
170 XMoveWindow(**display, _window, x, y);
171 _ignore_config+=2;
172 }
173
174 void Widget::show(bool recursive)
175 {
176 if (_visible)
177 return;
178
179 // make sure the internal state isn't mangled
180 if (_dirty)
181 update();
182
183 if (recursive) {
184 WidgetList::iterator it = _children.begin(), end = _children.end();
185 for (; it != end; ++it)
186 (*it)->show(recursive);
187 }
188
189 XMapWindow(**display, _window);
190 _visible = true;
191 }
192
193 void Widget::hide(bool recursive)
194 {
195 if (! _visible)
196 return;
197
198 if (recursive) {
199 WidgetList::iterator it = _children.begin(), end = _children.end();
200 for (; it != end; ++it)
201 (*it)->hide();
202 }
203
204 XUnmapWindow(**display, _window);
205 _visible = false;
206 }
207
208 void Widget::focus(void)
209 {
210 _focused = true;
211
212 Widget::WidgetList::iterator it = _children.begin(),
213 end = _children.end();
214 for (; it != end; ++it)
215 (*it)->focus();
216 }
217
218 void Widget::unfocus(void)
219 {
220 _focused = false;
221
222 Widget::WidgetList::iterator it = _children.begin(),
223 end = _children.end();
224 for (; it != end; ++it)
225 (*it)->unfocus();
226 }
227
228 bool Widget::grabMouse(void)
229 {
230 Status ret = XGrabPointer(**display, _window, True,
231 (ButtonPressMask | ButtonReleaseMask |
232 ButtonMotionMask | EnterWindowMask |
233 LeaveWindowMask | PointerMotionMask),
234 GrabModeSync, GrabModeAsync, None, None,
235 CurrentTime);
236 _grabbed_mouse = (ret == GrabSuccess);
237 return _grabbed_mouse;
238 }
239
240 void Widget::ungrabMouse(void)
241 {
242 if (! _grabbed_mouse)
243 return;
244
245 XUngrabPointer(**display, CurrentTime);
246 _grabbed_mouse = false;
247 }
248
249 bool Widget::grabKeyboard(void)
250 {
251 Status ret = XGrabKeyboard(**display, _window, True,
252 GrabModeSync, GrabModeAsync, CurrentTime);
253 _grabbed_keyboard = (ret == GrabSuccess);
254 return _grabbed_keyboard;
255
256 }
257
258 void Widget::ungrabKeyboard(void)
259 {
260 if (! _grabbed_keyboard)
261 return;
262
263 XUngrabKeyboard(**display, CurrentTime);
264 _grabbed_keyboard = false;
265 }
266
267 void Widget::render(void)
268 {
269 if (!_texture) return;
270
271 Surface *s = _surface; // save the current surface
272
273 _surface = new Surface(_screen, _rect.size());
274 display->renderControl(_screen)->drawBackground(*_surface, *_texture);
275
276 renderForeground(); // for inherited types to render onto the _surface
277
278 XSetWindowBackgroundPixmap(**display, _window, _surface->pixmap());
279
280 delete s; // delete the old surface *after* its pixmap isn't in use anymore
281 }
282
283 void Widget::adjust(void)
284 {
285 if (_direction == Horizontal)
286 adjustHorz();
287 else
288 adjustVert();
289 }
290
291 void Widget::adjustHorz(void)
292 {
293 if (_children.size() == 0)
294 return;
295
296 Widget *tmp;
297 WidgetList::iterator it, end = _children.end();
298
299 int tallest = 0;
300 int width = _bevel_width;
301 WidgetList stretchable;
302
303 for (it = _children.begin(); it != end; ++it) {
304 tmp = *it;
305 if (tmp->isStretchableVert())
306 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
307 _rect.height() - _bevel_width * 2 : _bevel_width);
308 if (tmp->isStretchableHorz())
309 stretchable.push_back(tmp);
310 else
311 width += tmp->_rect.width() + _bevel_width;
312
313 if (tmp->_rect.height() > tallest)
314 tallest = tmp->_rect.height();
315 }
316
317 if (stretchable.size() > 0) {
318 WidgetList::iterator str_it = stretchable.begin(),
319 str_end = stretchable.end();
320
321 int str_width = _rect.width() - width / stretchable.size();
322
323 for (; str_it != str_end; ++str_it)
324 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
325 : _bevel_width);
326 }
327
328 Widget *prev_widget = 0;
329
330 for (it = _children.begin(); it != end; ++it) {
331 tmp = *it;
332 int x, y;
333
334 if (prev_widget)
335 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
336 else
337 x = _bevel_width;
338 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
339
340 tmp->move(x, y);
341
342 prev_widget = tmp;
343 }
344 internalResize(width, tallest + _bevel_width * 2);
345 }
346
347 void Widget::adjustVert(void)
348 {
349 if (_children.size() == 0)
350 return;
351
352 Widget *tmp;
353 WidgetList::iterator it, end = _children.end();
354
355 int widest = 0;
356 int height = _bevel_width;
357 WidgetList stretchable;
358
359 for (it = _children.begin(); it != end; ++it) {
360 tmp = *it;
361 if (tmp->isStretchableHorz())
362 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
363 _rect.width() - _bevel_width * 2 : _bevel_width);
364 if (tmp->isStretchableVert())
365 stretchable.push_back(tmp);
366 else
367 height += tmp->_rect.height() + _bevel_width;
368
369 if (tmp->_rect.width() > widest)
370 widest = tmp->_rect.width();
371 }
372
373 if (stretchable.size() > 0) {
374 WidgetList::iterator str_it = stretchable.begin(),
375 str_end = stretchable.end();
376
377 int str_height = _rect.height() - height / stretchable.size();
378
379 for (; str_it != str_end; ++str_it)
380 (*str_it)->setHeight(str_height > _bevel_width ?
381 str_height - _bevel_width : _bevel_width);
382 }
383
384 Widget *prev_widget = 0;
385
386 for (it = _children.begin(); it != end; ++it) {
387 tmp = *it;
388 int x, y;
389
390 if (prev_widget)
391 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
392 else
393 y = _bevel_width;
394 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
395
396 tmp->move(x, y);
397
398 prev_widget = tmp;
399 }
400
401 internalResize(widest + _bevel_width * 2, height);
402 }
403
404 void Widget::update()
405 {
406 if (_dirty) {
407 adjust();
408 render();
409 XClearWindow(**display, _window);
410 }
411
412 WidgetList::iterator it = _children.begin(), end = _children.end();
413 for (; it != end; ++it)
414 (*it)->update();
415
416 _dirty = false;
417 }
418
419 void Widget::internalResize(int w, int h)
420 {
421 assert(w > 0 && h > 0);
422
423 bool fw = _fixed_width, fh = _fixed_height;
424
425 if (! fw && ! fh)
426 resize(w, h);
427 else if (! fw)
428 resize(w, _rect.height());
429 else if (! fh)
430 resize(_rect.width(), h);
431
432 _fixed_width = fw;
433 _fixed_height = fh;
434 }
435
436 void Widget::addChild(Widget *child, bool front)
437 {
438 assert(child);
439 if (front)
440 _children.push_front(child);
441 else
442 _children.push_back(child);
443 }
444
445 void Widget::removeChild(Widget *child)
446 {
447 assert(child);
448 WidgetList::iterator it, end = _children.end();
449 for (it = _children.begin(); it != end; ++it) {
450 if ((*it) == child)
451 break;
452 }
453
454 if (it != _children.end())
455 _children.erase(it);
456 }
457
458 void Widget::setStyle(RenderStyle *style)
459 {
460 assert(style);
461 _style = style;
462 _dirty = true;
463
464 WidgetList::iterator it, end = _children.end();
465 for (it = _children.begin(); it != end; ++it)
466 (*it)->setStyle(style);
467 }
468
469
470 void Widget::setEventDispatcher(EventDispatcher *disp)
471 {
472 if (_event_dispatcher)
473 _event_dispatcher->clearHandler(_window);
474 _event_dispatcher = disp;
475 _event_dispatcher->registerHandler(_window, this);
476 }
477
478 void Widget::exposeHandler(const XExposeEvent &e)
479 {
480 EventHandler::exposeHandler(e);
481 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
482 }
483
484 void Widget::configureHandler(const XConfigureEvent &e)
485 {
486 EventHandler::configureHandler(e);
487
488 if (_ignore_config) {
489 _ignore_config--;
490 } else {
491 int width = e.width;
492 int height = e.height;
493
494 XEvent ev;
495 while (XCheckTypedWindowEvent(**display, _window, ConfigureNotify, &ev)) {
496 width = ev.xconfigure.width;
497 height = ev.xconfigure.height;
498 }
499
500 if (!(width == _rect.width() && height == _rect.height())) {
501 _dirty = true;
502 _rect.setSize(width, height);
503 }
504 update();
505 }
506 }
507
508 }
This page took 0.06021 seconds and 4 git commands to generate.