4 #include "screeninfo.hh"
11 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
14 _parent(parent), _style(parent->getStyle()), _direction(direction),
15 _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
17 _visible(false), _focused(false), _grabbed_mouse(false),
18 _grabbed_keyboard(false), _stretchable_vert(false),
19 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
20 _screen(parent->getScreen()), _fixed_width(false), _fixed_height(false),
21 _event_dispatcher(parent->getEventDispatcher()), _application(0)
24 parent->addChild(this);
26 _event_dispatcher->registerHandler(_window, this);
29 OtkWidget::OtkWidget(OtkEventDispatcher *event_dispatcher, Style *style,
30 Direction direction, Cursor cursor, int bevel_width)
33 _parent(0), _style(style), _direction(direction), _cursor(cursor),
34 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
35 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
36 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
37 _bg_pixmap(0), _bg_pixel(0), _screen(style->getScreen()),
38 _fixed_width(false), _fixed_height(false),
39 _event_dispatcher(event_dispatcher), _application(0)
41 assert(event_dispatcher);
44 _event_dispatcher->registerHandler(_window, this);
47 OtkWidget::~OtkWidget()
52 std::for_each(_children.begin(), _children.end(), PointerAssassin());
55 _parent->removeChild(this);
57 XDestroyWindow(otk::OBDisplay::display, _window);
60 void OtkWidget::create(void)
62 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
63 Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
65 _rect.setRect(0, 0, 1, 1); // just some initial values
67 XSetWindowAttributes attrib_create;
68 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
70 attrib_create.background_pixmap = None;
71 attrib_create.colormap = scr_info->getColormap();
72 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
73 ButtonMotionMask | ExposureMask | StructureNotifyMask;
76 create_mask |= CWCursor;
77 attrib_create.cursor = _cursor;
80 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
81 _rect.y(), _rect.width(), _rect.height(), 0,
82 scr_info->getDepth(), InputOutput,
83 scr_info->getVisual(), create_mask, &attrib_create);
87 void OtkWidget::setWidth(int w)
91 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
94 void OtkWidget::setHeight(int h)
98 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
101 void OtkWidget::move(const Point &to)
103 move(to.x(), to.y());
106 void OtkWidget::move(int x, int y)
109 XMoveWindow(otk::OBDisplay::display, _window, x, y);
113 void OtkWidget::resize(const Point &to)
115 resize(to.x(), to.y());
118 void OtkWidget::resize(int w, int h)
120 assert(w > 0 && h > 0);
121 _fixed_width = _fixed_height = true;
122 setGeometry(_rect.x(), _rect.y(), w, h);
125 void OtkWidget::setGeometry(const Rect &new_geom)
127 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
130 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
132 setGeometry(topleft.x(), topleft.y(), width, height);
135 void OtkWidget::setGeometry(int x, int y, int width, int height)
137 _rect = Rect(x, y, width, height);
140 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
144 void OtkWidget::show(void)
149 // make sure the internal state isn't mangled
153 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
154 for (; it != end; ++it)
157 XMapWindow(otk::OBDisplay::display, _window);
161 void OtkWidget::hide(void)
166 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
167 for (; it != end; ++it)
170 XUnmapWindow(otk::OBDisplay::display, _window);
174 void OtkWidget::focus(void)
179 XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
183 bool OtkWidget::grabMouse(void)
185 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
186 (ButtonPressMask | ButtonReleaseMask |
187 ButtonMotionMask | EnterWindowMask |
188 LeaveWindowMask | PointerMotionMask),
189 GrabModeSync, GrabModeAsync, None, None,
191 _grabbed_mouse = (ret == GrabSuccess);
192 return _grabbed_mouse;
195 void OtkWidget::ungrabMouse(void)
197 if (! _grabbed_mouse)
200 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
201 _grabbed_mouse = false;
204 bool OtkWidget::grabKeyboard(void)
206 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
207 GrabModeSync, GrabModeAsync, CurrentTime);
208 _grabbed_keyboard = (ret == GrabSuccess);
209 return _grabbed_keyboard;
213 void OtkWidget::ungrabKeyboard(void)
215 if (! _grabbed_keyboard)
218 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
219 _grabbed_keyboard = false;
222 void OtkWidget::render(void)
224 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
227 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
229 unsigned int pix = _texture->color().pixel();
230 if (pix != _bg_pixel) {
232 XSetWindowBackground(otk::OBDisplay::display, _window, pix);
237 void OtkWidget::adjust(void)
239 if (_direction == Horizontal)
245 void OtkWidget::adjustHorz(void)
247 if (_children.size() == 0)
251 OtkWidgetList::iterator it, end = _children.end();
254 int width = _bevel_width;
255 OtkWidgetList stretchable;
257 for (it = _children.begin(); it != end; ++it) {
259 if (tmp->isStretchableVert())
260 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
261 _rect.height() - _bevel_width * 2 : _bevel_width);
262 if (tmp->isStretchableHorz())
263 stretchable.push_back(tmp);
265 width += tmp->_rect.width() + _bevel_width;
267 if (tmp->_rect.height() > tallest)
268 tallest = tmp->_rect.height();
271 if (stretchable.size() > 0) {
272 OtkWidgetList::iterator str_it = stretchable.begin(),
273 str_end = stretchable.end();
275 int str_width = _rect.width() - width / stretchable.size();
277 for (; str_it != str_end; ++str_it)
278 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
282 OtkWidget *prev_widget = 0;
284 for (it = _children.begin(); it != end; ++it) {
289 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
291 x = _rect.x() + _bevel_width;
292 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
299 internalResize(width, tallest + _bevel_width * 2);
302 void OtkWidget::adjustVert(void)
304 if (_children.size() == 0)
308 OtkWidgetList::iterator it, end = _children.end();
311 int height = _bevel_width;
312 OtkWidgetList stretchable;
314 for (it = _children.begin(); it != end; ++it) {
316 if (tmp->isStretchableHorz())
317 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
318 _rect.width() - _bevel_width * 2 : _bevel_width);
319 if (tmp->isStretchableVert())
320 stretchable.push_back(tmp);
322 height += tmp->_rect.height() + _bevel_width;
324 if (tmp->_rect.width() > widest)
325 widest = tmp->_rect.width();
328 if (stretchable.size() > 0) {
329 OtkWidgetList::iterator str_it = stretchable.begin(),
330 str_end = stretchable.end();
332 int str_height = _rect.height() - height / stretchable.size();
334 for (; str_it != str_end; ++str_it)
335 (*str_it)->setHeight(str_height > _bevel_width ?
336 str_height - _bevel_width : _bevel_width);
339 OtkWidget *prev_widget = 0;
341 for (it = _children.begin(); it != end; ++it) {
346 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
348 y = _rect.y() + _bevel_width;
349 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
356 internalResize(widest + _bevel_width * 2, height);
359 void OtkWidget::update(void)
364 XClearWindow(OBDisplay::display, _window);
367 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
368 for (; it != end; ++it)
374 void OtkWidget::internalResize(int w, int h)
376 assert(w > 0 && h > 0);
378 if (! _fixed_width && ! _fixed_height)
380 else if (! _fixed_width)
381 resize(w, _rect.height());
382 else if (! _fixed_height)
383 resize(_rect.width(), h);
386 void OtkWidget::addChild(OtkWidget *child, bool front)
390 _children.push_front(child);
392 _children.push_back(child);
395 void OtkWidget::removeChild(OtkWidget *child)
398 OtkWidgetList::iterator it, end = _children.end();
399 for (it = _children.begin(); it != end; ++it) {
404 if (it != _children.end())
408 void OtkWidget::setEventDispatcher(OtkEventDispatcher *disp)
410 if (_event_dispatcher)
411 _event_dispatcher->clearHandler(_window);
412 _event_dispatcher = disp;
413 _event_dispatcher->registerHandler(_window, this);
416 void OtkWidget::exposeHandler(const XExposeEvent &e)
418 OtkEventHandler::exposeHandler(e);
423 void OtkWidget::configureHandler(const XConfigureEvent &e)
425 OtkEventHandler::configureHandler(e);
426 if (_ignore_config) {
429 if (!(e.width == _rect.width() && e.height == _rect.height())) {
431 _rect.setSize(e.width, e.height);