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