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(UINT_MAX, UINT_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(UINT_MAX, UINT_MAX),
58 _dispatcher(parent->_dispatcher),
63 parent->addChild(this);
65 _dispatcher->registerHandler(_window, this);
70 assert(_children.empty()); // this would be bad. theyd have a hanging _parent
72 if (_surface) delete _surface;
73 if (_parent) _parent->removeChild(this);
75 _dispatcher->clearHandler(_window);
76 XDestroyWindow(**display, _window);
79 void Widget::show(bool children)
82 std::list<Widget*>::iterator it , end = _children.end();
83 for (it = _children.begin(); it != end; ++it)
88 XMapWindow(**display, _window);
97 XUnmapWindow(**display, _window);
98 if (_parent) _parent->layout();
102 void Widget::setEventMask(long e)
104 XSelectInput(**display, _window, e);
108 void Widget::update()
112 parent()->layout(); // relay-out us and our siblings
119 void Widget::moveresize(const Rect &r)
122 w = std::min(std::max(r.width(), minSize().width()), maxSize().width());
123 h = std::min(std::max(r.height(), minSize().height()), maxSize().height());
125 if (r.x() == area().x() && r.y() == area().y() &&
126 w == area().width() && h == area().height()) {
127 return; // no change, don't cause a big layout chain to occur!
130 internal_moveresize(r.x(), r.y(), w, h);
135 void Widget::internal_moveresize(int x, int y, unsigned w, unsigned int h)
139 assert(_borderwidth >= 0);
141 XMoveResizeWindow(**display, _window, x, y,
142 w - _borderwidth * 2,
143 h - _borderwidth * 2);
146 _area = Rect(x, y, w, h);
149 void Widget::setAlignment(RenderStyle::Justify a)
155 void Widget::createWindow(bool overrideredir)
157 const ScreenInfo *info = display->screenInfo(_screen);
158 XSetWindowAttributes attrib;
159 unsigned long mask = CWEventMask | CWBorderPixel;
161 attrib.event_mask = _event_mask;
162 attrib.border_pixel = (_bordercolor ?
163 _bordercolor->pixel():
164 BlackPixel(**display, _screen));
167 mask |= CWOverrideRedirect;
168 attrib.override_redirect = true;
171 _window = XCreateWindow(**display, (_parent ?
173 RootWindow(**display, _screen)),
174 _area.x(), _area.y(),
175 _area.width(), _area.height(),
182 assert(_window != None);
186 void Widget::setBorderWidth(int w)
189 if (!parent()) return; // top-level windows cannot have borders
190 if (w == borderWidth()) return; // no change
193 XSetWindowBorderWidth(**display, _window, _borderwidth);
199 void Widget::setMinSize(const Size &s)
205 void Widget::setMaxSize(const Size &s)
211 void Widget::setBorderColor(const RenderColor *c)
214 XSetWindowBorder(**otk::display, _window,
215 c ? c->pixel() : BlackPixel(**otk::display, _screen));
218 void Widget::setBevel(int b)
225 void Widget::layout()
227 if (_direction == Horizontal)
233 void Widget::layoutHorz()
235 std::list<Widget*>::iterator it, end;
237 // work with just the visible children
238 std::list<Widget*> visible = _children;
239 for (it = visible.begin(), end = visible.end(); it != end;) {
240 std::list<Widget*>::iterator next = it; ++next;
241 if (!(*it)->visible())
246 if (visible.empty()) return;
248 if ((unsigned)(_borderwidth * 2 + _bevel * 2) > _area.width() ||
249 (unsigned)(_borderwidth * 2 + _bevel * 2) > _area.height())
250 return; // not worth laying anything out!
252 int x, y; unsigned int w, h; // working area
254 w = _area.width() - _borderwidth * 2 - _bevel * 2;
255 h = _area.height() - _borderwidth * 2 - _bevel * 2;
257 int free = w - (visible.size() - 1) * _bevel;
258 if (free < 0) free = 0;
261 std::list<Widget*> adjustable = visible;
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);
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()) {
276 each = free / adjustable.size();
277 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
278 std::list<Widget*>::iterator next = it; ++next;
279 unsigned int m = (*it)->maxSize().width() - (*it)->minSize().width();
280 if (m > 0 && m < each) {
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
289 } while (it != end && !adjustable.empty());
292 // place/size the widgets
293 if (!adjustable.empty())
294 each = free / adjustable.size();
297 for (it = visible.begin(), end = visible.end(); it != end; ++it) {
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()) {
304 w = (*it)->minSize().width() + each;
307 w = (*it)->minSize().width();
309 // align it vertically
311 unsigned int hh = std::max(std::min(h, (*it)->_max_size.height()),
312 (*it)->_min_size.height());
315 case RenderStyle::RightBottomJustify:
318 case RenderStyle::CenterJustify:
321 case RenderStyle::LeftTopJustify:
325 (*it)->internal_moveresize(x, yy, w, hh);
332 void Widget::layoutVert()
334 std::list<Widget*>::iterator it, end;
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())
345 if (visible.empty()) return;
347 if ((unsigned)(_borderwidth * 2 + _bevel * 2) > _area.width() ||
348 (unsigned)(_borderwidth * 2 + _bevel * 2) > _area.height())
349 return; // not worth laying anything out!
351 int x, y; unsigned int w, h; // working area
353 w = _area.width() - _borderwidth * 2 - _bevel * 2;
354 h = _area.height() - _borderwidth * 2 - _bevel * 2;
356 int free = h - (visible.size() - 1) * _bevel;
357 if (free < 0) free = 0;
360 std::list<Widget*> adjustable = visible;
362 // find the 'free' space, and how many children will be using it
363 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
364 std::list<Widget*>::iterator next = it; ++next;
365 free -= (*it)->minSize().height();
366 if (free < 0) free = 0;
367 if ((*it)->maxSize().height() - (*it)->minSize().height() <= 0)
368 adjustable.erase(it);
371 // some widgets may have max heights that restrict them, find the 'true'
372 // amount of free space after these widgets are not included
373 if (!adjustable.empty()) {
375 each = free / adjustable.size();
376 for (it = adjustable.begin(), end = adjustable.end(); it != end;) {
377 std::list<Widget*>::iterator next = it; ++next;
378 unsigned int m = (*it)->maxSize().height() - (*it)->minSize().height();
379 if (m > 0 && m < each) {
381 if (free < 0) free = 0;
382 adjustable.erase(it);
383 break; // if one is found to be fixed, then the free space needs to
384 // change, and the rest need to be reexamined
388 } while (it != end && !adjustable.empty());
391 // place/size the widgets
392 if (!adjustable.empty())
393 each = free / adjustable.size();
396 for (it = visible.begin(), end = visible.end(); it != end; ++it) {
398 // is the widget adjustable?
399 std::list<Widget*>::const_iterator
400 found = std::find(adjustable.begin(), adjustable.end(), *it);
401 if (found != adjustable.end()) {
403 h = (*it)->minSize().height() + each;
406 h = (*it)->minSize().height();
408 // align it horizontally
410 unsigned int ww = std::max(std::min(w, (*it)->_max_size.width()),
411 (*it)->_min_size.width());
414 case RenderStyle::RightBottomJustify:
417 case RenderStyle::CenterJustify:
420 case RenderStyle::LeftTopJustify:
424 (*it)->internal_moveresize(xx, y, ww, h);
431 void Widget::render()
433 if (!_texture || !_dirty) return;
434 if ((unsigned)_borderwidth * 2 > _area.width() ||
435 (unsigned)_borderwidth * 2 > _area.height())
436 return; // no surface to draw on
438 Surface *s = new Surface(_screen, Size(_area.width() - _borderwidth * 2,
439 _area.height() - _borderwidth * 2));
440 display->renderControl(_screen)->drawBackground(*s, *_texture);
442 renderForeground(*s); // for inherited types to render onto the _surface
444 XSetWindowBackgroundPixmap(**display, _window, s->pixmap());
445 XClearWindow(**display, _window);
447 // delete the old surface *after* its pixmap isn't in use anymore
448 if (_surface) delete _surface;
455 void Widget::renderChildren()
457 std::list<Widget*>::iterator it, end = _children.end();
458 for (it = _children.begin(); it != end; ++it)
462 void Widget::exposeHandler(const XExposeEvent &e)
464 EventHandler::exposeHandler(e);
465 XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
468 void Widget::configureHandler(const XConfigureEvent &e)
470 if (_ignore_config) {
474 ev.xconfigure.width = e.width;
475 ev.xconfigure.height = e.height;
476 while (XCheckTypedWindowEvent(**display, window(), ConfigureNotify, &ev));
478 if (!((unsigned)ev.xconfigure.width == area().width() &&
479 (unsigned)ev.xconfigure.height == area().height())) {
480 _area = Rect(_area.position(), Size(e.width, e.height));