1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
7 #include "rendertexture.hh"
8 #include "rendercolor.hh"
9 #include "eventdispatcher.hh"
10 #include "screeninfo.hh"
18 Widget::Widget(int screen, EventDispatcher *ed, Direction direction, int bevel,
25 _event_mask(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
26 ExposureMask | StructureNotifyMask),
27 _alignment(RenderStyle::CenterJustify),
28 _direction(direction),
29 _max_size(INT_MAX, INT_MAX),
38 createWindow(overrideredir);
39 _dispatcher->registerHandler(_window, this);
42 Widget::Widget(Widget *parent, Direction direction, int bevel)
44 _screen(parent->_screen),
48 _event_mask(ButtonPressMask | ButtonReleaseMask | ButtonMotionMask |
49 ExposureMask | StructureNotifyMask),
50 _alignment(RenderStyle::CenterJustify),
51 _direction(direction),
52 _max_size(INT_MAX, INT_MAX),
58 _dispatcher(parent->_dispatcher),
63 parent->addChild(this);
64 if (parent->visible()) parent->layout();
65 _dispatcher->registerHandler(_window, this);
66 styleChanged(*RenderStyle::style(_screen));
71 assert(_children.empty()); // this would be bad. theyd have a hanging _parent
73 if (_surface) delete _surface;
74 if (_parent) _parent->removeChild(this);
76 _dispatcher->clearHandler(_window);
77 XDestroyWindow(**display, _window);
80 void Widget::show(bool children)
83 std::list<Widget*>::iterator it , end = _children.end();
84 for (it = _children.begin(); it != end; ++it) {
90 if (_parent) _parent->calcDefaultSizes();
94 XMapWindow(**display, _window);
103 XUnmapWindow(**display, _window);
105 _parent->calcDefaultSizes();
111 void Widget::setEventMask(long e)
113 XSelectInput(**display, _window, e);
117 void Widget::update()
119 if (!_visible) return;
122 _parent->calcDefaultSizes();
123 _parent->layout(); // relay-out us and our siblings
130 void Widget::moveresize(const Rect &r)
133 w = std::max(std::min(r.width(), maxSize().width()), minSize().width());
134 h = std::max(std::min(r.height(), maxSize().height()), minSize().height());
136 if (r.x() == area().x() && r.y() == area().y() &&
137 w == area().width() && h == area().height()) {
138 return; // no change, don't cause a big layout chain to occur!
141 internal_moveresize(r.x(), r.y(), w, h);
146 void Widget::internal_moveresize(int x, int y, int w, int h)
150 assert(_borderwidth >= 0);
152 if (!(x == _area.x() && y == _area.y())) {
153 if (!(w == _area.width() && h == _area.height()))
154 XMoveResizeWindow(**display, _window, x, y,
155 w - _borderwidth * 2,
156 h - _borderwidth * 2);
158 XMoveWindow(**display, _window, x, y);
160 XResizeWindow(**display, _window, w - _borderwidth*2, h - _borderwidth*2);
163 _area = Rect(x, y, w, h);
166 void Widget::setAlignment(RenderStyle::Justify a)
172 void Widget::createWindow(bool overrideredir)
174 const ScreenInfo *info = display->screenInfo(_screen);
175 XSetWindowAttributes attrib;
176 unsigned long mask = CWEventMask | CWBorderPixel;
178 attrib.event_mask = _event_mask;
179 attrib.border_pixel = (_bordercolor ?
180 _bordercolor->pixel():
181 BlackPixel(**display, _screen));
184 mask |= CWOverrideRedirect;
185 attrib.override_redirect = true;
188 _window = XCreateWindow(**display, (_parent ?
190 RootWindow(**display, _screen)),
191 _area.x(), _area.y(),
192 _area.width(), _area.height(),
199 assert(_window != None);
203 void Widget::calcDefaultSizes()
205 std::list<Widget*>::const_iterator it, end = _children.end();
206 int min_biggest = 0, max_biggest = 0;
207 int min_sum = _bevel + _borderwidth * 2;
208 int max_sum = _bevel + _borderwidth * 2;
209 bool fullmax = false;
211 for (it = _children.begin(); it != end; ++it) {
212 const otk::Size &min = (*it)->minSize();
213 const otk::Size &max = (*it)->maxSize();
214 if (_direction == Horizontal) {
215 if (min.height() > min_biggest) min_biggest = min.height();
216 if (max.height() > max_biggest) max_biggest = max.height();
217 min_sum += _bevel + min.width();
218 if (max.width() == INT_MAX)
221 max_sum += _bevel + max.width();
223 if (min.width() > min_biggest) min_biggest = min.width();
224 if (max.width() > max_biggest) max_biggest = max.width();
225 min_sum += _bevel + min.height();
226 if (max.height() == INT_MAX)
229 max_sum += _bevel + max.height();
232 if (_direction == Horizontal) {
233 _min_size = otk::Size(min_sum, min_biggest + (_bevel + _borderwidth) * 2);
234 _max_size = otk::Size((fullmax ? INT_MAX :
235 max_sum + (_bevel + _borderwidth) * 2),
238 _min_size = otk::Size(min_biggest, min_sum + (_bevel + _borderwidth) * 2);
239 _max_size = otk::Size(max_biggest, (fullmax ? INT_MAX : max_sum +
240 (_bevel + _borderwidth) * 2));
245 void Widget::setBorderWidth(int w)
248 if (!parent()) return; // top-level windows cannot have borders
249 if (w == borderWidth()) return; // no change
252 XSetWindowBorderWidth(**display, _window, _borderwidth);
258 void Widget::setMinSize(const Size &s)
264 void Widget::setMaxSize(const Size &s)
270 void Widget::setBorderColor(const RenderColor *c)
273 XSetWindowBorder(**otk::display, _window,
274 c ? c->pixel() : BlackPixel(**otk::display, _screen));
277 void Widget::setBevel(int b)
284 void Widget::layout()
286 if (_children.empty() || !_visible) return;
287 if (_direction == Horizontal)
293 void Widget::layoutHorz()
295 std::list<Widget*>::iterator it, end;
297 // work with just the visible children
298 std::list<Widget*> visible = _children;
299 for (it = visible.begin(), end = visible.end(); it != end;) {
300 std::list<Widget*>::iterator next = it; ++next;
301 if (!(*it)->visible())
306 if (visible.empty()) return;
308 int x, y, w, h; // working area
310 w = _area.width() - _borderwidth * 2 - _bevel * 2;
311 h = _area.height() - _borderwidth * 2 - _bevel * 2;
312 if (w < 0 || h < 0) return; // not worth laying anything out!
314 int free = w - (visible.size() - 1) * _bevel;
315 if (free < 0) free = 0;
318 std::list<Widget*> adjustable = visible;
320 // find the 'free' space, and how many children will be using it
321 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
322 std::list<Widget*>::iterator next = it; ++next;
323 free -= (*it)->minSize().width();
324 if (free < 0) free = 0;
325 if ((*it)->maxSize().width() - (*it)->minSize().width() <= 0)
326 adjustable.erase(it);
329 // some widgets may have max widths that restrict them, find the 'true'
330 // amount of free space after these widgets are not included
331 if (!adjustable.empty()) {
333 each = free / adjustable.size();
334 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
335 std::list<Widget*>::iterator next = it; ++next;
336 int m = (*it)->maxSize().width() - (*it)->minSize().width();
337 if (m > 0 && m < each) {
339 if (free < 0) free = 0;
340 adjustable.erase(it);
341 break; // if one is found to be fixed, then the free space needs to
342 // change, and the rest need to be reexamined
346 } while (it != end && !adjustable.empty());
349 // place/size the widgets
350 if (!adjustable.empty())
351 each = free / adjustable.size();
354 for (it = visible.begin(), end = visible.end(); it != end; ++it) {
356 // is the widget adjustable?
357 std::list<Widget*>::const_iterator
358 found = std::find(adjustable.begin(), adjustable.end(), *it);
359 if (found != adjustable.end()) {
361 w = (*it)->minSize().width() + each;
364 w = (*it)->minSize().width();
366 // align it vertically
368 int hh = std::max(std::min(h, (*it)->_max_size.height()),
369 (*it)->_min_size.height());
372 case RenderStyle::RightBottomJustify:
375 case RenderStyle::CenterJustify:
378 case RenderStyle::LeftTopJustify:
382 (*it)->internal_moveresize(x, yy, w, hh);
389 void Widget::layoutVert()
391 std::list<Widget*>::iterator it, end;
393 // work with just the visible children
394 std::list<Widget*> visible = _children;
395 for (it = visible.begin(), end = visible.end(); it != end;) {
396 std::list<Widget*>::iterator next = it; ++next;
397 if (!(*it)->visible())
402 if (visible.empty()) return;
404 int x, y, w, h; // working area
406 w = _area.width() - _borderwidth * 2 - _bevel * 2;
407 h = _area.height() - _borderwidth * 2 - _bevel * 2;
408 if (w < 0 || h < 0) return; // not worth laying anything out!
410 int free = h - (visible.size() - 1) * _bevel;
411 if (free < 0) free = 0;
414 std::list<Widget*> adjustable = visible;
416 // find the 'free' space, and how many children will be using it
417 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
418 std::list<Widget*>::iterator next = it; ++next;
419 free -= (*it)->minSize().height();
420 if (free < 0) free = 0;
421 if ((*it)->maxSize().height() - (*it)->minSize().height() <= 0)
422 adjustable.erase(it);
425 // some widgets may have max heights that restrict them, find the 'true'
426 // amount of free space after these widgets are not included
427 if (!adjustable.empty()) {
429 each = free / adjustable.size();
430 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
431 std::list<Widget*>::iterator next = it; ++next;
432 int m = (*it)->maxSize().height() - (*it)->minSize().height();
433 if (m > 0 && m < each) {
435 if (free < 0) free = 0;
436 adjustable.erase(it);
437 break; // if one is found to be fixed, then the free space needs to
438 // change, and the rest need to be reexamined
442 } while (it != end && !adjustable.empty());
445 // place/size the widgets
446 if (!adjustable.empty())
447 each = free / adjustable.size();
450 for (it = visible.begin(), end = visible.end(); it != end; ++it) {
452 // is the widget adjustable?
453 std::list<Widget*>::const_iterator
454 found = std::find(adjustable.begin(), adjustable.end(), *it);
455 if (found != adjustable.end()) {
457 h = (*it)->minSize().height() + each;
460 h = (*it)->minSize().height();
462 // align it horizontally
464 int ww = std::max(std::min(w, (*it)->_max_size.width()),
465 (*it)->_min_size.width());
468 case RenderStyle::RightBottomJustify:
471 case RenderStyle::CenterJustify:
474 case RenderStyle::LeftTopJustify:
478 (*it)->internal_moveresize(xx, y, ww, h);
485 void Widget::render()
489 // set a solid color as the default background
490 XSetWindowBackground(**display, _window,
491 RenderStyle::style(_screen)->
492 titlebarUnfocusBackground()->color().pixel());
495 if (_borderwidth * 2 > _area.width() ||
496 _borderwidth * 2 > _area.height())
497 return; // no surface to draw on
499 Surface *s = new Surface(_screen, Size(_area.width() - _borderwidth * 2,
500 _area.height() - _borderwidth * 2));
501 display->renderControl(_screen)->drawBackground(*s, *_texture);
503 renderForeground(*s); // for inherited types to render onto the _surface
505 XSetWindowBackgroundPixmap(**display, _window, s->pixmap());
506 XClearWindow(**display, _window);
508 // delete the old surface *after* its pixmap isn't in use anymore
509 if (_surface) delete _surface;
511 s->freePixelData(); // done rendering with this surface
517 void Widget::renderChildren()
519 std::list<Widget*>::iterator it, end = _children.end();
520 for (it = _children.begin(); it != end; ++it)
524 void Widget::styleChanged(const RenderStyle &)
529 void Widget::exposeHandler(const XExposeEvent &e)
531 EventHandler::exposeHandler(e);
532 XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
535 void Widget::configureHandler(const XConfigureEvent &e)
537 if (_ignore_config) {
540 // only interested in these for top level windows
544 ev.xconfigure.width = e.width;
545 ev.xconfigure.height = e.height;
546 while (XCheckTypedWindowEvent(**display, window(), ConfigureNotify, &ev));
548 if (!(ev.xconfigure.width == area().width() &&
549 ev.xconfigure.height == area().height())) {
550 _area = Rect(_area.position(), Size(e.width, e.height));