1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
7 #include "basewidget.hh"
10 #include "screeninfo.hh"
16 OtkBaseWidget::OtkBaseWidget(OtkBaseWidget *parent)
19 _parent(parent), _style(parent->getStyle()),
20 _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
21 _ignore_config(0), _visible(false), _focused(false), _grabbed_mouse(false),
22 _grabbed_keyboard(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
23 _screen(parent->getScreen())
26 parent->addChild(this);
30 OtkBaseWidget::OtkBaseWidget(Style *style, Cursor cursor, int bevel_width)
32 _dirty(false), _parent(0), _style(style), _cursor(cursor),
33 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
34 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
35 _texture(0), _bg_pixmap(0), _bg_pixel(0), _screen(style->getScreen())
41 OtkBaseWidget::~OtkBaseWidget()
46 std::for_each(_children.begin(), _children.end(), PointerAssassin());
49 _parent->removeChild(this);
51 XDestroyWindow(otk::OBDisplay::display, _window);
54 void OtkBaseWidget::create(void)
56 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
57 Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
59 _rect.setRect(0, 0, 1, 1); // just some initial values
61 XSetWindowAttributes attrib_create;
62 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
64 attrib_create.background_pixmap = None;
65 attrib_create.colormap = scr_info->getColormap();
66 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
67 ButtonMotionMask | ExposureMask | StructureNotifyMask;
70 create_mask |= CWCursor;
71 attrib_create.cursor = _cursor;
74 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
75 _rect.y(), _rect.width(), _rect.height(), 0,
76 scr_info->getDepth(), InputOutput,
77 scr_info->getVisual(), create_mask, &attrib_create);
81 void OtkBaseWidget::setWidth(int w)
84 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
87 void OtkBaseWidget::setHeight(int h)
90 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
93 void OtkBaseWidget::move(const Point &to)
98 void OtkBaseWidget::move(int x, int y)
101 XMoveWindow(otk::OBDisplay::display, _window, x, y);
105 void OtkBaseWidget::resize(const Point &to)
107 resize(to.x(), to.y());
110 void OtkBaseWidget::resize(int w, int h)
112 assert(w > 0 && h > 0);
113 setGeometry(_rect.x(), _rect.y(), w, h);
116 void OtkBaseWidget::setGeometry(const Rect &new_geom)
118 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
121 void OtkBaseWidget::setGeometry(const Point &topleft, int width, int height)
123 setGeometry(topleft.x(), topleft.y(), width, height);
126 void OtkBaseWidget::setGeometry(int x, int y, int width, int height)
128 _rect = Rect(x, y, width, height);
131 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
135 void OtkBaseWidget::show(bool recursive)
140 // make sure the internal state isn't mangled
145 OtkBaseWidgetList::iterator it = _children.begin(), end = _children.end();
146 for (; it != end; ++it)
150 XMapWindow(otk::OBDisplay::display, _window);
154 void OtkBaseWidget::hide(bool recursive)
160 OtkBaseWidgetList::iterator it = _children.begin(), end = _children.end();
161 for (; it != end; ++it)
165 XUnmapWindow(otk::OBDisplay::display, _window);
169 void OtkBaseWidget::focus(void)
174 XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
178 bool OtkBaseWidget::grabMouse(void)
180 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
181 (ButtonPressMask | ButtonReleaseMask |
182 ButtonMotionMask | EnterWindowMask |
183 LeaveWindowMask | PointerMotionMask),
184 GrabModeSync, GrabModeAsync, None, None,
186 _grabbed_mouse = (ret == GrabSuccess);
187 return _grabbed_mouse;
190 void OtkBaseWidget::ungrabMouse(void)
192 if (! _grabbed_mouse)
195 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
196 _grabbed_mouse = false;
199 bool OtkBaseWidget::grabKeyboard(void)
201 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
202 GrabModeSync, GrabModeAsync, CurrentTime);
203 _grabbed_keyboard = (ret == GrabSuccess);
204 return _grabbed_keyboard;
208 void OtkBaseWidget::ungrabKeyboard(void)
210 if (! _grabbed_keyboard)
213 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
214 _grabbed_keyboard = false;
217 void OtkBaseWidget::render(void)
219 if (!_texture) return;
221 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
224 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
226 unsigned int pix = _texture->color().pixel();
227 if (pix != _bg_pixel) {
229 XSetWindowBackground(otk::OBDisplay::display, _window, pix);
234 void OtkBaseWidget::update(void)
238 XClearWindow(OBDisplay::display, _window);
241 OtkBaseWidgetList::iterator it = _children.begin(), end = _children.end();
242 for (; it != end; ++it)
248 void OtkBaseWidget::addChild(OtkBaseWidget *child, bool front)
252 _children.push_front(child);
254 _children.push_back(child);
257 void OtkBaseWidget::removeChild(OtkBaseWidget *child)
260 OtkBaseWidgetList::iterator it, end = _children.end();
261 for (it = _children.begin(); it != end; ++it) {
266 if (it != _children.end())
270 void OtkBaseWidget::setStyle(Style *style)
275 OtkBaseWidgetList::iterator it, end = _children.end();
276 for (it = _children.begin(); it != end; ++it)
277 (*it)->setStyle(style);
280 void OtkBaseWidget::exposeHandler(const XExposeEvent &e)
282 OtkEventHandler::exposeHandler(e);
287 void OtkBaseWidget::configureHandler(const XConfigureEvent &e)
289 OtkEventHandler::configureHandler(e);
290 if (_ignore_config) {
293 if (!(e.width == _rect.width() && e.height == _rect.height())) {
295 _rect.setSize(e.width, e.height);