1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
10 #include "screeninfo.hh"
17 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
19 _dirty(false), _focused(false),
20 _parent(parent), _style(parent->style()), _direction(direction),
21 _cursor(parent->cursor()), _bevel_width(parent->bevelWidth()),
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), _screen(parent->screen()), _fixed_width(false),
27 _fixed_height(false), _event_dispatcher(parent->eventDispatcher())
30 parent->addChild(this);
32 _event_dispatcher->registerHandler(_window, this);
33 setStyle(_style); // let the widget initialize stuff
36 OtkWidget::OtkWidget(OtkEventDispatcher *event_dispatcher, Style *style,
37 Direction direction, Cursor cursor, int bevel_width,
38 unsigned long create_mask)
40 _dirty(false),_focused(false),
41 _parent(0), _style(style), _direction(direction), _cursor(cursor),
42 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
43 _grabbed_mouse(false), _grabbed_keyboard(false),
44 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
45 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0),
46 _screen(style->getScreen()), _fixed_width(false), _fixed_height(false),
47 _event_dispatcher(event_dispatcher)
49 assert(event_dispatcher);
52 _event_dispatcher->registerHandler(_window, this);
53 setStyle(_style); // let the widget initialize stuff
56 OtkWidget::~OtkWidget()
61 _event_dispatcher->clearHandler(_window);
63 std::for_each(_children.begin(), _children.end(), PointerAssassin());
66 _parent->removeChild(this);
68 XDestroyWindow(otk::OBDisplay::display, _window);
71 void OtkWidget::create(unsigned long mask)
73 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
74 Window p_window = _parent ? _parent->window() : scr_info->rootWindow();
76 _rect.setRect(0, 0, 1, 1); // just some initial values
78 XSetWindowAttributes attrib_create;
79 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask |
82 attrib_create.background_pixmap = None;
83 attrib_create.colormap = scr_info->colormap();
84 attrib_create.override_redirect = True; // not used by default
85 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
86 ButtonMotionMask | ExposureMask | StructureNotifyMask;
90 create_mask |= CWCursor;
91 attrib_create.cursor = _cursor;
94 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
95 _rect.y(), _rect.width(), _rect.height(), 0,
96 scr_info->depth(), InputOutput,
97 scr_info->visual(), create_mask, &attrib_create);
101 void OtkWidget::setWidth(int w)
105 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
108 void OtkWidget::setHeight(int h)
111 _fixed_height = true;
112 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
115 void OtkWidget::move(const Point &to)
117 move(to.x(), to.y());
120 void OtkWidget::move(int x, int y)
123 XMoveWindow(otk::OBDisplay::display, _window, x, y);
127 void OtkWidget::resize(const Point &to)
129 resize(to.x(), to.y());
132 void OtkWidget::resize(int w, int h)
134 assert(w > 0 && h > 0);
135 _fixed_width = _fixed_height = true;
136 setGeometry(_rect.x(), _rect.y(), w, h);
139 void OtkWidget::setGeometry(const Rect &new_geom)
141 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
144 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
146 setGeometry(topleft.x(), topleft.y(), width, height);
149 void OtkWidget::setGeometry(int x, int y, int width, int height)
151 _rect = Rect(x, y, width, height);
154 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
158 void OtkWidget::show(bool recursive)
163 // make sure the internal state isn't mangled
168 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
169 for (; it != end; ++it)
173 XMapWindow(otk::OBDisplay::display, _window);
177 void OtkWidget::hide(bool recursive)
183 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
184 for (; it != end; ++it)
188 XUnmapWindow(otk::OBDisplay::display, _window);
192 void OtkWidget::focus(void)
196 OtkWidget::OtkWidgetList::iterator it = _children.begin(),
197 end = _children.end();
198 for (; it != end; ++it)
202 void OtkWidget::unfocus(void)
206 OtkWidget::OtkWidgetList::iterator it = _children.begin(),
207 end = _children.end();
208 for (; it != end; ++it)
212 bool OtkWidget::grabMouse(void)
214 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
215 (ButtonPressMask | ButtonReleaseMask |
216 ButtonMotionMask | EnterWindowMask |
217 LeaveWindowMask | PointerMotionMask),
218 GrabModeSync, GrabModeAsync, None, None,
220 _grabbed_mouse = (ret == GrabSuccess);
221 return _grabbed_mouse;
224 void OtkWidget::ungrabMouse(void)
226 if (! _grabbed_mouse)
229 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
230 _grabbed_mouse = false;
233 bool OtkWidget::grabKeyboard(void)
235 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
236 GrabModeSync, GrabModeAsync, CurrentTime);
237 _grabbed_keyboard = (ret == GrabSuccess);
238 return _grabbed_keyboard;
242 void OtkWidget::ungrabKeyboard(void)
244 if (! _grabbed_keyboard)
247 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
248 _grabbed_keyboard = false;
251 void OtkWidget::render(void)
253 if (!_texture) return;
255 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
258 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
261 unsigned int pix = _texture->color().pixel();
262 if (pix != _bg_pixel) {
264 XSetWindowBackground(otk::OBDisplay::display, _window, pix);
269 void OtkWidget::adjust(void)
271 if (_direction == Horizontal)
277 void OtkWidget::adjustHorz(void)
279 if (_children.size() == 0)
283 OtkWidgetList::iterator it, end = _children.end();
286 int width = _bevel_width;
287 OtkWidgetList stretchable;
289 for (it = _children.begin(); it != end; ++it) {
291 if (tmp->isStretchableVert())
292 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
293 _rect.height() - _bevel_width * 2 : _bevel_width);
294 if (tmp->isStretchableHorz())
295 stretchable.push_back(tmp);
297 width += tmp->_rect.width() + _bevel_width;
299 if (tmp->_rect.height() > tallest)
300 tallest = tmp->_rect.height();
303 if (stretchable.size() > 0) {
304 OtkWidgetList::iterator str_it = stretchable.begin(),
305 str_end = stretchable.end();
307 int str_width = _rect.width() - width / stretchable.size();
309 for (; str_it != str_end; ++str_it)
310 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
314 OtkWidget *prev_widget = 0;
316 for (it = _children.begin(); it != end; ++it) {
321 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
323 x = _rect.x() + _bevel_width;
324 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
331 internalResize(width, tallest + _bevel_width * 2);
334 void OtkWidget::adjustVert(void)
336 if (_children.size() == 0)
340 OtkWidgetList::iterator it, end = _children.end();
343 int height = _bevel_width;
344 OtkWidgetList stretchable;
346 for (it = _children.begin(); it != end; ++it) {
348 if (tmp->isStretchableHorz())
349 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
350 _rect.width() - _bevel_width * 2 : _bevel_width);
351 if (tmp->isStretchableVert())
352 stretchable.push_back(tmp);
354 height += tmp->_rect.height() + _bevel_width;
356 if (tmp->_rect.width() > widest)
357 widest = tmp->_rect.width();
360 if (stretchable.size() > 0) {
361 OtkWidgetList::iterator str_it = stretchable.begin(),
362 str_end = stretchable.end();
364 int str_height = _rect.height() - height / stretchable.size();
366 for (; str_it != str_end; ++str_it)
367 (*str_it)->setHeight(str_height > _bevel_width ?
368 str_height - _bevel_width : _bevel_width);
371 OtkWidget *prev_widget = 0;
373 for (it = _children.begin(); it != end; ++it) {
378 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
380 y = _rect.y() + _bevel_width;
381 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
388 internalResize(widest + _bevel_width * 2, height);
391 void OtkWidget::update(void)
396 XClearWindow(OBDisplay::display, _window);
399 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
400 for (; it != end; ++it)
406 void OtkWidget::internalResize(int w, int h)
408 assert(w > 0 && h > 0);
410 if (! _fixed_width && ! _fixed_height)
412 else if (! _fixed_width)
413 resize(w, _rect.height());
414 else if (! _fixed_height)
415 resize(_rect.width(), h);
418 void OtkWidget::addChild(OtkWidget *child, bool front)
422 _children.push_front(child);
424 _children.push_back(child);
427 void OtkWidget::removeChild(OtkWidget *child)
430 OtkWidgetList::iterator it, end = _children.end();
431 for (it = _children.begin(); it != end; ++it) {
436 if (it != _children.end())
440 void OtkWidget::setStyle(Style *style)
446 OtkWidgetList::iterator it, end = _children.end();
447 for (it = _children.begin(); it != end; ++it)
448 (*it)->setStyle(style);
452 void OtkWidget::setEventDispatcher(OtkEventDispatcher *disp)
454 if (_event_dispatcher)
455 _event_dispatcher->clearHandler(_window);
456 _event_dispatcher = disp;
457 _event_dispatcher->registerHandler(_window, this);
460 void OtkWidget::exposeHandler(const XExposeEvent &e)
462 OtkEventHandler::exposeHandler(e);
467 void OtkWidget::configureHandler(const XConfigureEvent &e)
469 OtkEventHandler::configureHandler(e);
470 if (_ignore_config) {
473 if (!(e.width == _rect.width() && e.height == _rect.height())) {
475 _rect.setSize(e.width, e.height);