4 #include "screeninfo.hh"
8 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
9 : _parent(parent), _style(parent->getStyle()), _direction(direction),
10 _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
11 _visible(false), _focused(false), _grabbed_mouse(false),
12 _grabbed_keyboard(false), _stretchable_vert(false),
13 _stretchable_horz(false), _texture(0), _bg_pixmap(0),
14 _screen(parent->getScreen())
16 parent->addChild(this);
20 OtkWidget::OtkWidget(Style *style, Direction direction,
21 Cursor cursor, int bevel_width)
22 : _parent(0), _style(style), _direction(direction), _cursor(cursor),
23 _bevel_width(bevel_width), _visible(false),
24 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
25 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
26 _bg_pixmap(0), _screen(style->getScreen())
32 OtkWidget::~OtkWidget()
37 std::for_each(_children.begin(), _children.end(), PointerAssassin());
40 _parent->removeChild(this);
42 XDestroyWindow(otk::OBDisplay::display, _window);
45 void OtkWidget::create(void)
47 const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
48 Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
50 _rect.setRect(0, 0, 1, 1); // just some initial values
52 XSetWindowAttributes attrib_create;
53 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
55 attrib_create.background_pixmap = None;
56 attrib_create.colormap = scr_info->getColormap();
57 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
58 ButtonMotionMask | ExposureMask | StructureNotifyMask;
61 create_mask |= CWCursor;
62 attrib_create.cursor = _cursor;
65 _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
66 _rect.y(), _rect.width(), _rect.height(), 0,
67 scr_info->getDepth(), InputOutput,
68 scr_info->getVisual(), create_mask, &attrib_create);
71 void OtkWidget::move(const Point &to)
76 void OtkWidget::move(int x, int y)
79 XMoveWindow(otk::OBDisplay::display, _window, x, y);
82 void OtkWidget::resize(const Point &to)
84 resize(to.x(), to.y());
87 void OtkWidget::resize(int x, int y)
89 assert(x >= _rect.x() && y >= _rect.y());
91 setGeometry(_rect.x(), _rect.y(), x - _rect.x(), y - _rect.y());
94 void OtkWidget::setGeometry(const Rect &new_geom)
96 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
99 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
101 setGeometry(topleft.x(), topleft.y(), width, height);
104 void OtkWidget::setGeometry(int x, int y, int width, int height)
106 _rect = Rect(x, y, width, height);
108 fprintf(stderr, "Resizing to x: %d, y: %d, width: %d, height: %d\n",
109 x, y, width, height);
111 XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
115 void OtkWidget::show(void)
120 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
121 for (; it != end; ++it) {
122 fprintf(stderr, "showing child\n");
126 fprintf(stderr, "x: %d, y: %d, width: %d, height: %d\n",
127 _rect.x(), _rect.y(), _rect.width(), _rect.height());
129 XMapWindow(otk::OBDisplay::display, _window);
133 void OtkWidget::hide(void)
138 OtkWidgetList::iterator it = _children.begin(), end = _children.end();
139 for (; it != end; ++it)
142 XUnmapWindow(otk::OBDisplay::display, _window);
146 void OtkWidget::focus(void)
151 XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
155 bool OtkWidget::grabMouse(void)
157 Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
158 (ButtonPressMask | ButtonReleaseMask |
159 ButtonMotionMask | EnterWindowMask |
160 LeaveWindowMask | PointerMotionMask),
161 GrabModeSync, GrabModeAsync, None, None,
163 _grabbed_mouse = (ret == GrabSuccess);
164 return _grabbed_mouse;
167 void OtkWidget::ungrabMouse(void)
169 if (! _grabbed_mouse)
172 XUngrabPointer(otk::OBDisplay::display, CurrentTime);
173 _grabbed_mouse = false;
176 bool OtkWidget::grabKeyboard(void)
178 Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
179 GrabModeSync, GrabModeAsync, CurrentTime);
180 _grabbed_keyboard = (ret == GrabSuccess);
181 return _grabbed_keyboard;
185 void OtkWidget::ungrabKeyboard(void)
187 if (! _grabbed_keyboard)
190 XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
191 _grabbed_keyboard = false;
194 void OtkWidget::setTexture(BTexture *texture)
196 if (!texture && !_texture)
199 Pixmap old = _bg_pixmap;
204 _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
206 if (_bg_pixmap != old)
207 XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
209 //XSetWindowBackground(otk::OBDisplay::display, win, pix);
212 void OtkWidget::addChild(OtkWidget *child, bool front)
216 _children.push_front(child);
218 _children.push_back(child);
221 void OtkWidget::removeChild(OtkWidget *child)
223 OtkWidgetList::iterator it, end = _children.end();
224 for (; it != end; ++it) {
229 if (it != _children.end())