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