4 #include "screeninfo.hh"
10 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
11 : _parent(parent), _style(parent->getStyle()), _direction(direction),
12 _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
13 _visible(false), _focused(false), _grabbed_mouse(false),
14 _grabbed_keyboard(false), _stretchable_vert(false),
15 _stretchable_horz(false), _texture(0), _bg_pixmap(0),
16 _screen(parent->getScreen())
18 parent->addChild(this);
22 OtkWidget::OtkWidget(Style *style, Direction direction,
23 Cursor cursor, int bevel_width)
24 : _parent(0), _style(style), _direction(direction), _cursor(cursor),
25 _bevel_width(bevel_width), _visible(false),
26 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
27 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
28 _bg_pixmap(0), _screen(style->getScreen())
34 OtkWidget::~OtkWidget()
39 std::for_each(_children.begin(), _children.end(), PointerAssassin());
42 _parent->removeChild(this);
44 XDestroyWindow(otk::OBDisplay::display, _window);
47 void OtkWidget::create(void)
49 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
50 Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
52 _rect.setRect(0, 0, 1, 1); // just some initial values
54 XSetWindowAttributes attrib_create;
55 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
57 attrib_create.background_pixmap = None;
58 attrib_create.colormap = scr_info->getColormap();
59 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
60 ButtonMotionMask | ExposureMask | StructureNotifyMask;
63 create_mask |= CWCursor;
64 attrib_create.cursor = _cursor;
67 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
68 _rect.y(), _rect.width(), _rect.height(), 0,
69 scr_info->getDepth(), InputOutput,
70 scr_info->getVisual(), create_mask, &attrib_create);
73 void OtkWidget::move(const Point &to)
78 void OtkWidget::move(int x, int y)
81 XMoveWindow(otk::OBDisplay::display, _window, x, y);
84 void OtkWidget::resize(const Point &to)
86 resize(to.x(), to.y());
89 void OtkWidget::resize(int x, int y)
91 assert(x >= _rect.x() && y >= _rect.y());
93 setGeometry(_rect.x(), _rect.y(), x - _rect.x(), y - _rect.y());
96 void OtkWidget::setGeometry(const Rect &new_geom)
98 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
101 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
103 setGeometry(topleft.x(), topleft.y(), width, height);
106 void OtkWidget::setGeometry(int x, int y, int width, int height)
108 _rect = Rect(x, y, width, height);
110 fprintf(stderr, "Resizing to x: %d, y: %d, width: %d, height: %d\n",
111 x, y, width, height);
113 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
117 void OtkWidget::show(void)
122 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
123 for (; it != end; ++it) {
124 fprintf(stderr, "showing child\n");
128 fprintf(stderr, "x: %d, y: %d, width: %d, height: %d\n",
129 _rect.x(), _rect.y(), _rect.width(), _rect.height());
131 XMapWindow(otk::OBDisplay::display, _window);
135 void OtkWidget::hide(void)
140 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
141 for (; it != end; ++it)
144 XUnmapWindow(otk::OBDisplay::display, _window);
148 void OtkWidget::focus(void)
153 XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
157 bool OtkWidget::grabMouse(void)
159 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
160 (ButtonPressMask | ButtonReleaseMask |
161 ButtonMotionMask | EnterWindowMask |
162 LeaveWindowMask | PointerMotionMask),
163 GrabModeSync, GrabModeAsync, None, None,
165 _grabbed_mouse = (ret == GrabSuccess);
166 return _grabbed_mouse;
169 void OtkWidget::ungrabMouse(void)
171 if (! _grabbed_mouse)
174 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
175 _grabbed_mouse = false;
178 bool OtkWidget::grabKeyboard(void)
180 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
181 GrabModeSync, GrabModeAsync, CurrentTime);
182 _grabbed_keyboard = (ret == GrabSuccess);
183 return _grabbed_keyboard;
187 void OtkWidget::ungrabKeyboard(void)
189 if (! _grabbed_keyboard)
192 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
193 _grabbed_keyboard = false;
196 void OtkWidget::setTexture(BTexture *texture)
198 if (!texture && !_texture)
201 Pixmap old = _bg_pixmap;
206 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
208 if (_bg_pixmap != old)
209 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
211 //XSetWindowBackground(otk::OBDisplay::display, win, pix);
214 void OtkWidget::addChild(OtkWidget *child, bool front)
218 _children.push_front(child);
220 _children.push_back(child);
223 void OtkWidget::removeChild(OtkWidget *child)
225 OtkWidgetList::iterator it, end = _children.end();
226 for (; it != end; ++it) {
231 if (it != _children.end())