4 #include "screeninfo.hh"
11 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
13 _parent(parent), _style(parent->getStyle()), _direction(direction),
14 _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
16 _visible(false), _focused(false), _grabbed_mouse(false),
17 _grabbed_keyboard(false), _stretchable_vert(false),
18 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
19 _screen(parent->getScreen()), _fixed_width(false), _fixed_height(false),
22 parent->addChild(this);
26 OtkWidget::OtkWidget(OtkApplication *app, Direction direction,
27 Cursor cursor, int bevel_width)
29 _parent(0), _style(app->getStyle()), _direction(direction), _cursor(cursor),
30 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
31 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
32 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
33 _bg_pixmap(0), _bg_pixel(0), _screen(app->getStyle()->getScreen()),
34 _fixed_width(false), _fixed_height(false), _dirty(false)
38 app->registerHandler(_window, this);
41 OtkWidget::OtkWidget(Style *style, Direction direction,
42 Cursor cursor, int bevel_width)
44 _parent(0), _style(style), _direction(direction), _cursor(cursor),
45 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
46 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
47 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
48 _bg_pixmap(0), _bg_pixel(0), _screen(style->getScreen()),
49 _fixed_width(false), _fixed_height(false), _dirty(false)
55 OtkWidget::~OtkWidget()
60 std::for_each(_children.begin(), _children.end(), PointerAssassin());
63 _parent->removeChild(this);
65 XDestroyWindow(otk::OBDisplay::display, _window);
68 void OtkWidget::create(void)
70 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
71 Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
73 _rect.setRect(0, 0, 1, 1); // just some initial values
75 XSetWindowAttributes attrib_create;
76 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
78 attrib_create.background_pixmap = None;
79 attrib_create.colormap = scr_info->getColormap();
80 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
81 ButtonMotionMask | ExposureMask | StructureNotifyMask;
84 create_mask |= CWCursor;
85 attrib_create.cursor = _cursor;
88 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
89 _rect.y(), _rect.width(), _rect.height(), 0,
90 scr_info->getDepth(), InputOutput,
91 scr_info->getVisual(), create_mask, &attrib_create);
95 void OtkWidget::setWidth(int w)
99 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
102 void OtkWidget::setHeight(int h)
105 _fixed_height = true;
106 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
109 void OtkWidget::move(const Point &to)
111 move(to.x(), to.y());
114 void OtkWidget::move(int x, int y)
117 XMoveWindow(otk::OBDisplay::display, _window, x, y);
121 void OtkWidget::resize(const Point &to)
123 resize(to.x(), to.y());
126 void OtkWidget::resize(int w, int h)
128 assert(w > 0 && h > 0);
129 _fixed_width = _fixed_height = true;
130 setGeometry(_rect.x(), _rect.y(), w, h);
133 void OtkWidget::setGeometry(const Rect &new_geom)
135 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
138 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
140 setGeometry(topleft.x(), topleft.y(), width, height);
143 void OtkWidget::setGeometry(int x, int y, int width, int height)
145 _rect = Rect(x, y, width, height);
148 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
152 void OtkWidget::show(void)
157 // make sure the internal state isn't mangled
161 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
162 for (; it != end; ++it)
165 XMapWindow(otk::OBDisplay::display, _window);
169 void OtkWidget::hide(void)
174 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
175 for (; it != end; ++it)
178 XUnmapWindow(otk::OBDisplay::display, _window);
182 void OtkWidget::focus(void)
187 XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
191 bool OtkWidget::grabMouse(void)
193 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
194 (ButtonPressMask | ButtonReleaseMask |
195 ButtonMotionMask | EnterWindowMask |
196 LeaveWindowMask | PointerMotionMask),
197 GrabModeSync, GrabModeAsync, None, None,
199 _grabbed_mouse = (ret == GrabSuccess);
200 return _grabbed_mouse;
203 void OtkWidget::ungrabMouse(void)
205 if (! _grabbed_mouse)
208 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
209 _grabbed_mouse = false;
212 bool OtkWidget::grabKeyboard(void)
214 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
215 GrabModeSync, GrabModeAsync, CurrentTime);
216 _grabbed_keyboard = (ret == GrabSuccess);
217 return _grabbed_keyboard;
221 void OtkWidget::ungrabKeyboard(void)
223 if (! _grabbed_keyboard)
226 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
227 _grabbed_keyboard = false;
230 void OtkWidget::render(void)
232 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
235 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
237 unsigned int pix = _texture->color().pixel();
238 if (pix != _bg_pixel) {
240 XSetWindowBackground(otk::OBDisplay::display, _window, pix);
245 void OtkWidget::adjust(void)
247 if (_direction == Horizontal)
253 void OtkWidget::adjustHorz(void)
255 if (_children.size() == 0)
259 OtkWidgetList::iterator it, end = _children.end();
262 int width = _bevel_width;
263 OtkWidgetList stretchable;
265 for (it = _children.begin(); it != end; ++it) {
267 if (tmp->isStretchableVert())
268 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
269 _rect.height() - _bevel_width * 2 : _bevel_width);
270 if (tmp->isStretchableHorz())
271 stretchable.push_back(tmp);
273 width += tmp->_rect.width() + _bevel_width;
275 if (tmp->_rect.height() > tallest)
276 tallest = tmp->_rect.height();
279 if (stretchable.size() > 0) {
280 OtkWidgetList::iterator str_it = stretchable.begin(),
281 str_end = stretchable.end();
283 int str_width = _rect.width() - width / stretchable.size();
285 for (; str_it != str_end; ++str_it)
286 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
290 OtkWidget *prev_widget = 0;
292 for (it = _children.begin(); it != end; ++it) {
297 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
299 x = _rect.x() + _bevel_width;
300 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
307 internalResize(width, tallest + _bevel_width * 2);
310 void OtkWidget::adjustVert(void)
312 if (_children.size() == 0)
316 OtkWidgetList::iterator it, end = _children.end();
319 int height = _bevel_width;
320 OtkWidgetList stretchable;
322 for (it = _children.begin(); it != end; ++it) {
324 if (tmp->isStretchableHorz())
325 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
326 _rect.width() - _bevel_width * 2 : _bevel_width);
327 if (tmp->isStretchableVert())
328 stretchable.push_back(tmp);
330 height += tmp->_rect.height() + _bevel_width;
332 if (tmp->_rect.width() > widest)
333 widest = tmp->_rect.width();
336 if (stretchable.size() > 0) {
337 OtkWidgetList::iterator str_it = stretchable.begin(),
338 str_end = stretchable.end();
340 int str_height = _rect.height() - height / stretchable.size();
342 for (; str_it != str_end; ++str_it)
343 (*str_it)->setHeight(str_height > _bevel_width ?
344 str_height - _bevel_width : _bevel_width);
347 OtkWidget *prev_widget = 0;
349 for (it = _children.begin(); it != end; ++it) {
354 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
356 y = _rect.y() + _bevel_width;
357 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
364 internalResize(widest + _bevel_width * 2, height);
367 void OtkWidget::update(void)
372 XClearWindow(OBDisplay::display, _window);
375 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
376 for (; it != end; ++it)
382 void OtkWidget::internalResize(int w, int h)
384 assert(w > 0 && h > 0);
386 if (! _fixed_width && ! _fixed_height)
388 else if (! _fixed_width)
389 resize(w, _rect.height());
390 else if (! _fixed_height)
391 resize(_rect.width(), h);
394 void OtkWidget::addChild(OtkWidget *child, bool front)
398 _children.push_front(child);
400 _children.push_back(child);
403 void OtkWidget::removeChild(OtkWidget *child)
406 OtkWidgetList::iterator it, end = _children.end();
407 for (it = _children.begin(); it != end; ++it) {
412 if (it != _children.end())
416 int OtkWidget::exposeHandler(const XExposeEvent &e)
418 OtkEventHandler::exposeHandler(e);
419 if (e.window == _window) {
424 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
425 for (; it != end; ++it)
426 if ((*it)->exposeHandler(e))
432 int OtkWidget::configureHandler(const XConfigureEvent &e)
434 OtkEventHandler::configureHandler(e);
435 if (e.window == _window) {
436 if (_ignore_config) {
439 std::cout << "configure\n";
440 if (!(e.width == _rect.width() && e.height == _rect.height())) {
442 _rect.setSize(e.width, e.height);
448 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
449 for (; it != end; ++it)
450 if ((*it)->configureHandler(e))