]> Dogcows Code - chaz/openbox/blob - otk/widget.cc
rm a XXX.. comment it out :)
[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 if (s)
281 delete s; // delete the old surface *after* its pixmap isn't in use anymore
282 }
283
284 void Widget::adjust(void)
285 {
286 if (_direction == Horizontal)
287 adjustHorz();
288 else
289 adjustVert();
290 }
291
292 void Widget::adjustHorz(void)
293 {
294 if (_children.size() == 0)
295 return;
296
297 Widget *tmp;
298 WidgetList::iterator it, end = _children.end();
299
300 int tallest = 0;
301 int width = _bevel_width;
302 WidgetList stretchable;
303
304 for (it = _children.begin(); it != end; ++it) {
305 tmp = *it;
306 if (tmp->isStretchableVert())
307 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
308 _rect.height() - _bevel_width * 2 : _bevel_width);
309 if (tmp->isStretchableHorz())
310 stretchable.push_back(tmp);
311 else
312 width += tmp->_rect.width() + _bevel_width;
313
314 if (tmp->_rect.height() > tallest)
315 tallest = tmp->_rect.height();
316 }
317
318 if (stretchable.size() > 0) {
319 WidgetList::iterator str_it = stretchable.begin(),
320 str_end = stretchable.end();
321
322 int str_width = _rect.width() - width / stretchable.size();
323
324 for (; str_it != str_end; ++str_it)
325 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
326 : _bevel_width);
327 }
328
329 Widget *prev_widget = 0;
330
331 for (it = _children.begin(); it != end; ++it) {
332 tmp = *it;
333 int x, y;
334
335 if (prev_widget)
336 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
337 else
338 x = _bevel_width;
339 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
340
341 tmp->move(x, y);
342
343 prev_widget = tmp;
344 }
345 internalResize(width, tallest + _bevel_width * 2);
346 }
347
348 void Widget::adjustVert(void)
349 {
350 if (_children.size() == 0)
351 return;
352
353 Widget *tmp;
354 WidgetList::iterator it, end = _children.end();
355
356 int widest = 0;
357 int height = _bevel_width;
358 WidgetList stretchable;
359
360 for (it = _children.begin(); it != end; ++it) {
361 tmp = *it;
362 if (tmp->isStretchableHorz())
363 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
364 _rect.width() - _bevel_width * 2 : _bevel_width);
365 if (tmp->isStretchableVert())
366 stretchable.push_back(tmp);
367 else
368 height += tmp->_rect.height() + _bevel_width;
369
370 if (tmp->_rect.width() > widest)
371 widest = tmp->_rect.width();
372 }
373
374 if (stretchable.size() > 0) {
375 WidgetList::iterator str_it = stretchable.begin(),
376 str_end = stretchable.end();
377
378 int str_height = _rect.height() - height / stretchable.size();
379
380 for (; str_it != str_end; ++str_it)
381 (*str_it)->setHeight(str_height > _bevel_width ?
382 str_height - _bevel_width : _bevel_width);
383 }
384
385 Widget *prev_widget = 0;
386
387 for (it = _children.begin(); it != end; ++it) {
388 tmp = *it;
389 int x, y;
390
391 if (prev_widget)
392 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
393 else
394 y = _bevel_width;
395 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
396
397 tmp->move(x, y);
398
399 prev_widget = tmp;
400 }
401
402 internalResize(widest + _bevel_width * 2, height);
403 }
404
405 void Widget::update()
406 {
407 if (_dirty) {
408 adjust();
409 render();
410 XClearWindow(**display, _window);
411 }
412
413 WidgetList::iterator it = _children.begin(), end = _children.end();
414 for (; it != end; ++it)
415 (*it)->update();
416
417 _dirty = false;
418 }
419
420 void Widget::internalResize(int w, int h)
421 {
422 assert(w > 0 && h > 0);
423
424 bool fw = _fixed_width, fh = _fixed_height;
425
426 if (! fw && ! fh)
427 resize(w, h);
428 else if (! fw)
429 resize(w, _rect.height());
430 else if (! fh)
431 resize(_rect.width(), h);
432
433 _fixed_width = fw;
434 _fixed_height = fh;
435 }
436
437 void Widget::addChild(Widget *child, bool front)
438 {
439 assert(child);
440 if (front)
441 _children.push_front(child);
442 else
443 _children.push_back(child);
444 }
445
446 void Widget::removeChild(Widget *child)
447 {
448 assert(child);
449 WidgetList::iterator it, end = _children.end();
450 for (it = _children.begin(); it != end; ++it) {
451 if ((*it) == child)
452 break;
453 }
454
455 if (it != _children.end())
456 _children.erase(it);
457 }
458
459 void Widget::setStyle(RenderStyle *style)
460 {
461 assert(style);
462 _style = style;
463 _dirty = true;
464
465 WidgetList::iterator it, end = _children.end();
466 for (it = _children.begin(); it != end; ++it)
467 (*it)->setStyle(style);
468 }
469
470
471 void Widget::setEventDispatcher(EventDispatcher *disp)
472 {
473 if (_event_dispatcher)
474 _event_dispatcher->clearHandler(_window);
475 _event_dispatcher = disp;
476 _event_dispatcher->registerHandler(_window, this);
477 }
478
479 void Widget::exposeHandler(const XExposeEvent &e)
480 {
481 EventHandler::exposeHandler(e);
482 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
483 }
484
485 void Widget::configureHandler(const XConfigureEvent &e)
486 {
487 EventHandler::configureHandler(e);
488
489 if (_ignore_config) {
490 _ignore_config--;
491 } else {
492 int width = e.width;
493 int height = e.height;
494
495 XEvent ev;
496 while (XCheckTypedWindowEvent(**display, _window, ConfigureNotify, &ev)) {
497 width = ev.xconfigure.width;
498 height = ev.xconfigure.height;
499 }
500
501 if (!(width == _rect.width() && height == _rect.height())) {
502 _dirty = true;
503 _rect.setSize(width, height);
504 }
505 update();
506 }
507 }
508
509 }
This page took 0.060935 seconds and 4 git commands to generate.