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)
39 _dirty(false),_focused(false),
40 _parent(0), _style(style), _direction(direction), _cursor(cursor),
41 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
42 _grabbed_mouse(false), _grabbed_keyboard(false),
43 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
44 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0),
45 _screen(style->getScreen()), _fixed_width(false), _fixed_height(false),
46 _event_dispatcher(event_dispatcher)
48 assert(event_dispatcher);
51 _event_dispatcher->registerHandler(_window, this);
52 setStyle(_style); // let the widget initialize stuff
55 OtkWidget::~OtkWidget()
60 _event_dispatcher->clearHandler(_window);
62 std::for_each(_children.begin(), _children.end(), PointerAssassin());
65 _parent->removeChild(this);
67 XDestroyWindow(otk::OBDisplay::display, _window);
70 void OtkWidget::create(void)
72 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
73 Window p_window = _parent ? _parent->window() : scr_info->rootWindow();
75 _rect.setRect(0, 0, 1, 1); // just some initial values
77 XSetWindowAttributes attrib_create;
78 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
80 attrib_create.background_pixmap = None;
81 attrib_create.colormap = scr_info->colormap();
82 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
83 ButtonMotionMask | ExposureMask | StructureNotifyMask;
86 create_mask |= CWCursor;
87 attrib_create.cursor = _cursor;
90 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
91 _rect.y(), _rect.width(), _rect.height(), 0,
92 scr_info->depth(), InputOutput,
93 scr_info->visual(), create_mask, &attrib_create);
97 void OtkWidget::setWidth(int w)
101 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
104 void OtkWidget::setHeight(int h)
107 _fixed_height = true;
108 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
111 void OtkWidget::move(const Point &to)
113 move(to.x(), to.y());
116 void OtkWidget::move(int x, int y)
119 XMoveWindow(otk::OBDisplay::display, _window, x, y);
123 void OtkWidget::resize(const Point &to)
125 resize(to.x(), to.y());
128 void OtkWidget::resize(int w, int h)
130 assert(w > 0 && h > 0);
131 _fixed_width = _fixed_height = true;
132 setGeometry(_rect.x(), _rect.y(), w, h);
135 void OtkWidget::setGeometry(const Rect &new_geom)
137 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
140 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
142 setGeometry(topleft.x(), topleft.y(), width, height);
145 void OtkWidget::setGeometry(int x, int y, int width, int height)
147 _rect = Rect(x, y, width, height);
150 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
154 void OtkWidget::show(bool recursive)
159 // make sure the internal state isn't mangled
164 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
165 for (; it != end; ++it)
169 XMapWindow(otk::OBDisplay::display, _window);
173 void OtkWidget::hide(bool recursive)
179 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
180 for (; it != end; ++it)
184 XUnmapWindow(otk::OBDisplay::display, _window);
188 void OtkWidget::focus(void)
192 OtkWidget::OtkWidgetList::iterator it = _children.begin(),
193 end = _children.end();
194 for (; it != end; ++it)
198 void OtkWidget::unfocus(void)
202 OtkWidget::OtkWidgetList::iterator it = _children.begin(),
203 end = _children.end();
204 for (; it != end; ++it)
208 bool OtkWidget::grabMouse(void)
210 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
211 (ButtonPressMask | ButtonReleaseMask |
212 ButtonMotionMask | EnterWindowMask |
213 LeaveWindowMask | PointerMotionMask),
214 GrabModeSync, GrabModeAsync, None, None,
216 _grabbed_mouse = (ret == GrabSuccess);
217 return _grabbed_mouse;
220 void OtkWidget::ungrabMouse(void)
222 if (! _grabbed_mouse)
225 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
226 _grabbed_mouse = false;
229 bool OtkWidget::grabKeyboard(void)
231 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
232 GrabModeSync, GrabModeAsync, CurrentTime);
233 _grabbed_keyboard = (ret == GrabSuccess);
234 return _grabbed_keyboard;
238 void OtkWidget::ungrabKeyboard(void)
240 if (! _grabbed_keyboard)
243 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
244 _grabbed_keyboard = false;
247 void OtkWidget::render(void)
249 if (!_texture) return;
251 printf("rendering %lx\n", _texture);
253 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
256 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
258 unsigned int pix = _texture->color().pixel();
259 if (pix != _bg_pixel) {
261 XSetWindowBackground(otk::OBDisplay::display, _window, pix);
266 void OtkWidget::adjust(void)
268 if (_direction == Horizontal)
274 void OtkWidget::adjustHorz(void)
276 if (_children.size() == 0)
280 OtkWidgetList::iterator it, end = _children.end();
283 int width = _bevel_width;
284 OtkWidgetList stretchable;
286 for (it = _children.begin(); it != end; ++it) {
288 if (tmp->isStretchableVert())
289 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
290 _rect.height() - _bevel_width * 2 : _bevel_width);
291 if (tmp->isStretchableHorz())
292 stretchable.push_back(tmp);
294 width += tmp->_rect.width() + _bevel_width;
296 if (tmp->_rect.height() > tallest)
297 tallest = tmp->_rect.height();
300 if (stretchable.size() > 0) {
301 OtkWidgetList::iterator str_it = stretchable.begin(),
302 str_end = stretchable.end();
304 int str_width = _rect.width() - width / stretchable.size();
306 for (; str_it != str_end; ++str_it)
307 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
311 OtkWidget *prev_widget = 0;
313 for (it = _children.begin(); it != end; ++it) {
318 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
320 x = _rect.x() + _bevel_width;
321 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
328 internalResize(width, tallest + _bevel_width * 2);
331 void OtkWidget::adjustVert(void)
333 if (_children.size() == 0)
337 OtkWidgetList::iterator it, end = _children.end();
340 int height = _bevel_width;
341 OtkWidgetList stretchable;
343 for (it = _children.begin(); it != end; ++it) {
345 if (tmp->isStretchableHorz())
346 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
347 _rect.width() - _bevel_width * 2 : _bevel_width);
348 if (tmp->isStretchableVert())
349 stretchable.push_back(tmp);
351 height += tmp->_rect.height() + _bevel_width;
353 if (tmp->_rect.width() > widest)
354 widest = tmp->_rect.width();
357 if (stretchable.size() > 0) {
358 OtkWidgetList::iterator str_it = stretchable.begin(),
359 str_end = stretchable.end();
361 int str_height = _rect.height() - height / stretchable.size();
363 for (; str_it != str_end; ++str_it)
364 (*str_it)->setHeight(str_height > _bevel_width ?
365 str_height - _bevel_width : _bevel_width);
368 OtkWidget *prev_widget = 0;
370 for (it = _children.begin(); it != end; ++it) {
375 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
377 y = _rect.y() + _bevel_width;
378 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
385 internalResize(widest + _bevel_width * 2, height);
388 void OtkWidget::update(void)
391 printf("widget dirty, redrawing\n");
394 XClearWindow(OBDisplay::display, _window);
397 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
398 for (; it != end; ++it)
404 void OtkWidget::internalResize(int w, int h)
406 assert(w > 0 && h > 0);
408 if (! _fixed_width && ! _fixed_height)
410 else if (! _fixed_width)
411 resize(w, _rect.height());
412 else if (! _fixed_height)
413 resize(_rect.width(), h);
416 void OtkWidget::addChild(OtkWidget *child, bool front)
420 _children.push_front(child);
422 _children.push_back(child);
425 void OtkWidget::removeChild(OtkWidget *child)
428 OtkWidgetList::iterator it, end = _children.end();
429 for (it = _children.begin(); it != end; ++it) {
434 if (it != _children.end())
438 void OtkWidget::setStyle(Style *style)
444 OtkWidgetList::iterator it, end = _children.end();
445 for (it = _children.begin(); it != end; ++it)
446 (*it)->setStyle(style);
450 void OtkWidget::setEventDispatcher(OtkEventDispatcher *disp)
452 if (_event_dispatcher)
453 _event_dispatcher->clearHandler(_window);
454 _event_dispatcher = disp;
455 _event_dispatcher->registerHandler(_window, this);
458 void OtkWidget::exposeHandler(const XExposeEvent &e)
460 OtkEventHandler::exposeHandler(e);
465 void OtkWidget::configureHandler(const XConfigureEvent &e)
467 OtkEventHandler::configureHandler(e);
468 if (_ignore_config) {
471 if (!(e.width == _rect.width() && e.height == _rect.height())) {
473 _rect.setSize(e.width, e.height);