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