]> Dogcows Code - chaz/openbox/blob - otk/widget.cc
use parentrelative when theres no texture set
[chaz/openbox] / otk / widget.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "config.h"
4 #include "widget.hh"
5 #include "display.hh"
6 #include "surface.hh"
7 #include "rendertexture.hh"
8 #include "rendercolor.hh"
9 #include "eventdispatcher.hh"
10 #include "screeninfo.hh"
11
12 #include <climits>
13 #include <cassert>
14 #include <algorithm>
15
16 namespace otk {
17
18 Widget::Widget(int screen, EventDispatcher *ed, Direction direction, int bevel,
19 bool overrideredir)
20 : _texture(0),
21 _screen(screen),
22 _parent(0),
23 _window(0),
24 _surface(0),
25 _event_mask(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
26 ExposureMask | StructureNotifyMask),
27 _alignment(RenderStyle::CenterJustify),
28 _direction(direction),
29 _max_size(INT_MAX, INT_MAX),
30 _visible(false),
31 _bordercolor(0),
32 _borderwidth(0),
33 _bevel(bevel),
34 _dirty(true),
35 _dispatcher(ed),
36 _ignore_config(0)
37 {
38 createWindow(overrideredir);
39 _dispatcher->registerHandler(_window, this);
40 }
41
42 Widget::Widget(Widget *parent, Direction direction, int bevel)
43 : _texture(0),
44 _screen(parent->_screen),
45 _parent(parent),
46 _window(0),
47 _surface(0),
48 _event_mask(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
49 ExposureMask | StructureNotifyMask),
50 _alignment(RenderStyle::CenterJustify),
51 _direction(direction),
52 _max_size(INT_MAX, INT_MAX),
53 _visible(false),
54 _bordercolor(0),
55 _borderwidth(0),
56 _bevel(bevel),
57 _dirty(true),
58 _dispatcher(parent->_dispatcher),
59 _ignore_config(0)
60 {
61 assert(parent);
62 createWindow(false);
63 parent->addChild(this);
64 if (parent->visible()) parent->layout();
65 _dispatcher->registerHandler(_window, this);
66 }
67
68 Widget::~Widget()
69 {
70 assert(_children.empty()); // this would be bad. theyd have a hanging _parent
71
72 if (_surface) delete _surface;
73 if (_parent) _parent->removeChild(this);
74
75 _dispatcher->clearHandler(_window);
76 XDestroyWindow(**display, _window);
77 }
78
79 void Widget::show(bool children)
80 {
81 if (children) {
82 std::list<Widget*>::iterator it , end = _children.end();
83 for (it = _children.begin(); it != end; ++it) {
84 (*it)->show(true);
85 }
86 }
87 if (!_visible) {
88 _visible = true;
89 XMapWindow(**display, _window);
90 update();
91 }
92 }
93
94 void Widget::hide()
95 {
96 if (_visible) {
97 _visible = false;
98 XUnmapWindow(**display, _window);
99 if (_parent) _parent->layout();
100 }
101 }
102
103 void Widget::setEventMask(long e)
104 {
105 XSelectInput(**display, _window, e);
106 _event_mask = e;
107 }
108
109 void Widget::update()
110 {
111 if (!_visible) return;
112 _dirty = true;
113 if (parent())
114 parent()->layout(); // relay-out us and our siblings
115 else {
116 render();
117 layout();
118 }
119 }
120
121 void Widget::moveresize(const Rect &r)
122 {
123 int w, h;
124 w = std::min(std::max(r.width(), minSize().width()), maxSize().width());
125 h = std::min(std::max(r.height(), minSize().height()), maxSize().height());
126
127 if (r.x() == area().x() && r.y() == area().y() &&
128 w == area().width() && h == area().height()) {
129 return; // no change, don't cause a big layout chain to occur!
130 }
131
132 internal_moveresize(r.x(), r.y(), w, h);
133
134 update();
135 }
136
137 void Widget::internal_moveresize(int x, int y, int w, int h)
138 {
139 assert(w > 0);
140 assert(h > 0);
141 assert(_borderwidth >= 0);
142 _dirty = true;
143 XMoveResizeWindow(**display, _window, x, y,
144 w - _borderwidth * 2,
145 h - _borderwidth * 2);
146 _ignore_config++;
147
148 _area = Rect(x, y, w, h);
149 }
150
151 void Widget::setAlignment(RenderStyle::Justify a)
152 {
153 _alignment = a;
154 layout();
155 }
156
157 void Widget::createWindow(bool overrideredir)
158 {
159 const ScreenInfo *info = display->screenInfo(_screen);
160 XSetWindowAttributes attrib;
161 unsigned long mask = CWEventMask | CWBorderPixel;
162
163 attrib.event_mask = _event_mask;
164 attrib.border_pixel = (_bordercolor ?
165 _bordercolor->pixel():
166 BlackPixel(**display, _screen));
167
168 if (overrideredir) {
169 mask |= CWOverrideRedirect;
170 attrib.override_redirect = true;
171 }
172
173 _window = XCreateWindow(**display, (_parent ?
174 _parent->_window :
175 RootWindow(**display, _screen)),
176 _area.x(), _area.y(),
177 _area.width(), _area.height(),
178 _borderwidth,
179 info->depth(),
180 InputOutput,
181 info->visual(),
182 mask,
183 &attrib);
184 assert(_window != None);
185 ++_ignore_config;
186 }
187
188 void Widget::setBorderWidth(int w)
189 {
190 assert(w >= 0);
191 if (!parent()) return; // top-level windows cannot have borders
192 if (w == borderWidth()) return; // no change
193
194 _borderwidth = w;
195 XSetWindowBorderWidth(**display, _window, _borderwidth);
196
197 calcDefaultSizes();
198 update();
199 }
200
201 void Widget::setMinSize(const Size &s)
202 {
203 _min_size = s;
204 update();
205 }
206
207 void Widget::setMaxSize(const Size &s)
208 {
209 _max_size = s;
210 update();
211 }
212
213 void Widget::setBorderColor(const RenderColor *c)
214 {
215 _bordercolor = c;
216 XSetWindowBorder(**otk::display, _window,
217 c ? c->pixel() : BlackPixel(**otk::display, _screen));
218 }
219
220 void Widget::setBevel(int b)
221 {
222 _bevel = b;
223 calcDefaultSizes();
224 layout();
225 }
226
227 void Widget::layout()
228 {
229 if (_children.empty() || !_visible) return;
230 if (_direction == Horizontal)
231 layoutHorz();
232 else
233 layoutVert();
234 }
235
236 void Widget::layoutHorz()
237 {
238 std::list<Widget*>::iterator it, end;
239
240 // work with just the visible children
241 std::list<Widget*> visible = _children;
242 for (it = visible.begin(), end = visible.end(); it != end;) {
243 std::list<Widget*>::iterator next = it; ++next;
244 if (!(*it)->visible())
245 visible.erase(it);
246 it = next;
247 }
248
249 if (visible.empty()) return;
250
251 int x, y, w, h; // working area
252 x = y = _bevel;
253 w = _area.width() - _borderwidth * 2 - _bevel * 2;
254 h = _area.height() - _borderwidth * 2 - _bevel * 2;
255 if (w < 0 || h < 0) return; // not worth laying anything out!
256
257 int free = w - (visible.size() - 1) * _bevel;
258 if (free < 0) free = 0;
259 int each;
260
261 std::list<Widget*> adjustable = visible;
262
263 // find the 'free' space, and how many children will be using it
264 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
265 std::list<Widget*>::iterator next = it; ++next;
266 free -= (*it)->minSize().width();
267 if (free < 0) free = 0;
268 if ((*it)->maxSize().width() - (*it)->minSize().width() <= 0)
269 adjustable.erase(it);
270 it = next;
271 }
272 // some widgets may have max widths that restrict them, find the 'true'
273 // amount of free space after these widgets are not included
274 if (!adjustable.empty()) {
275 do {
276 each = free / adjustable.size();
277 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
278 std::list<Widget*>::iterator next = it; ++next;
279 int m = (*it)->maxSize().width() - (*it)->minSize().width();
280 if (m > 0 && m < each) {
281 free -= m;
282 if (free < 0) free = 0;
283 adjustable.erase(it);
284 break; // if one is found to be fixed, then the free space needs to
285 // change, and the rest need to be reexamined
286 }
287 it = next;
288 }
289 } while (it != end && !adjustable.empty());
290 }
291
292 // place/size the widgets
293 if (!adjustable.empty())
294 each = free / adjustable.size();
295 else
296 each = 0;
297 for (it = visible.begin(), end = visible.end(); it != end; ++it) {
298 int w;
299 // is the widget adjustable?
300 std::list<Widget*>::const_iterator
301 found = std::find(adjustable.begin(), adjustable.end(), *it);
302 if (found != adjustable.end()) {
303 // adjustable
304 w = (*it)->minSize().width() + each;
305 } else {
306 // fixed
307 w = (*it)->minSize().width();
308 }
309 // align it vertically
310 int yy = y;
311 int hh = std::max(std::min(h, (*it)->_max_size.height()),
312 (*it)->_min_size.height());
313 if (hh < h) {
314 switch(_alignment) {
315 case RenderStyle::RightBottomJustify:
316 yy += h - hh;
317 break;
318 case RenderStyle::CenterJustify:
319 yy += (h - hh) / 2;
320 break;
321 case RenderStyle::LeftTopJustify:
322 break;
323 }
324 }
325 (*it)->internal_moveresize(x, yy, w, hh);
326 (*it)->render();
327 (*it)->layout();
328 x += w + _bevel;
329 }
330 }
331
332 void Widget::layoutVert()
333 {
334 std::list<Widget*>::iterator it, end;
335
336 // work with just the visible children
337 std::list<Widget*> visible = _children;
338 for (it = visible.begin(), end = visible.end(); it != end;) {
339 std::list<Widget*>::iterator next = it; ++next;
340 if (!(*it)->visible())
341 visible.erase(it);
342 it = next;
343 }
344
345 if (visible.empty()) return;
346
347 int x, y, w, h; // working area
348 x = y = _bevel;
349 w = _area.width() - _borderwidth * 2 - _bevel * 2;
350 h = _area.height() - _borderwidth * 2 - _bevel * 2;
351 if (w < 0 || h < 0) return; // not worth laying anything out!
352
353 int free = h - (visible.size() - 1) * _bevel;
354 if (free < 0) free = 0;
355 int each;
356
357 std::list<Widget*> adjustable = visible;
358
359 // find the 'free' space, and how many children will be using it
360 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
361 std::list<Widget*>::iterator next = it; ++next;
362 free -= (*it)->minSize().height();
363 if (free < 0) free = 0;
364 if ((*it)->maxSize().height() - (*it)->minSize().height() <= 0)
365 adjustable.erase(it);
366 it = next;
367 }
368 // some widgets may have max heights that restrict them, find the 'true'
369 // amount of free space after these widgets are not included
370 if (!adjustable.empty()) {
371 do {
372 each = free / adjustable.size();
373 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
374 std::list<Widget*>::iterator next = it; ++next;
375 int m = (*it)->maxSize().height() - (*it)->minSize().height();
376 if (m > 0 && m < each) {
377 free -= m;
378 if (free < 0) free = 0;
379 adjustable.erase(it);
380 break; // if one is found to be fixed, then the free space needs to
381 // change, and the rest need to be reexamined
382 }
383 it = next;
384 }
385 } while (it != end && !adjustable.empty());
386 }
387
388 // place/size the widgets
389 if (!adjustable.empty())
390 each = free / adjustable.size();
391 else
392 each = 0;
393 for (it = visible.begin(), end = visible.end(); it != end; ++it) {
394 int h;
395 // is the widget adjustable?
396 std::list<Widget*>::const_iterator
397 found = std::find(adjustable.begin(), adjustable.end(), *it);
398 if (found != adjustable.end()) {
399 // adjustable
400 h = (*it)->minSize().height() + each;
401 } else {
402 // fixed
403 h = (*it)->minSize().height();
404 }
405 // align it horizontally
406 int xx = x;
407 int ww = std::max(std::min(w, (*it)->_max_size.width()),
408 (*it)->_min_size.width());
409 if (ww < w) {
410 switch(_alignment) {
411 case RenderStyle::RightBottomJustify:
412 xx += w - ww;
413 break;
414 case RenderStyle::CenterJustify:
415 xx += (w - ww) / 2;
416 break;
417 case RenderStyle::LeftTopJustify:
418 break;
419 }
420 }
421 (*it)->internal_moveresize(xx, y, ww, h);
422 (*it)->render();
423 (*it)->layout();
424 y += h + _bevel;
425 }
426 }
427
428 void Widget::render()
429 {
430 if (!_dirty) return;
431 if (!_texture) {
432 XSetWindowBackgroundPixmap(**display, _window, ParentRelative);
433 return;
434 }
435 if (_borderwidth * 2 > _area.width() ||
436 _borderwidth * 2 > _area.height())
437 return; // no surface to draw on
438
439 Surface *s = new Surface(_screen, Size(_area.width() - _borderwidth * 2,
440 _area.height() - _borderwidth * 2));
441 display->renderControl(_screen)->drawBackground(*s, *_texture);
442
443 renderForeground(*s); // for inherited types to render onto the _surface
444
445 XSetWindowBackgroundPixmap(**display, _window, s->pixmap());
446 XClearWindow(**display, _window);
447
448 // delete the old surface *after* its pixmap isn't in use anymore
449 if (_surface) delete _surface;
450
451 _surface = s;
452
453 _dirty = false;
454 }
455
456 void Widget::renderChildren()
457 {
458 std::list<Widget*>::iterator it, end = _children.end();
459 for (it = _children.begin(); it != end; ++it)
460 (*it)->render();
461 }
462
463 void Widget::exposeHandler(const XExposeEvent &e)
464 {
465 EventHandler::exposeHandler(e);
466 XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
467 }
468
469 void Widget::configureHandler(const XConfigureEvent &e)
470 {
471 if (_ignore_config) {
472 _ignore_config--;
473 } else {
474 // only interested in these for top level windows
475 if (_parent) return;
476
477 XEvent ev;
478 ev.xconfigure.width = e.width;
479 ev.xconfigure.height = e.height;
480 while (XCheckTypedWindowEvent(**display, window(), ConfigureNotify, &ev));
481
482 if (!(ev.xconfigure.width == area().width() &&
483 ev.xconfigure.height == area().height())) {
484 _area = Rect(_area.position(), Size(e.width, e.height));
485 update();
486 }
487 }
488 }
489
490 }
This page took 0.058451 seconds and 4 git commands to generate.