1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
10 #include "screeninfo.hh"
17 Widget::Widget(Widget *parent, Direction direction)
18 : Surface(parent->screen()),
20 _dirty(false), _focused(false),
21 _parent(parent), _style(parent->style()), _direction(direction),
22 _cursor(parent->cursor()), _bevel_width(parent->bevelWidth()),
24 _visible(false), _grabbed_mouse(false),
25 _grabbed_keyboard(false), _stretchable_vert(false),
26 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
27 _bcolor(0), _bwidth(0), _pos(0,0), _screen(parent->screen()),
28 _fixed_width(false), _fixed_height(false),
29 _event_dispatcher(parent->eventDispatcher())
32 parent->addChild(this);
34 _event_dispatcher->registerHandler(_window, this);
35 setStyle(_style); // let the widget initialize stuff
38 Widget::Widget(EventDispatcher *event_dispatcher, Style *style,
39 Direction direction, Cursor cursor, int bevel_width,
40 bool override_redirect)
41 : Surface(style->getScreen()),
43 _dirty(false),_focused(false),
44 _parent(0), _style(style), _direction(direction), _cursor(cursor),
45 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
46 _grabbed_mouse(false), _grabbed_keyboard(false),
47 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
48 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0), _pos(0,0),
49 _screen(style->getScreen()), _fixed_width(false), _fixed_height(false),
50 _event_dispatcher(event_dispatcher)
52 assert(event_dispatcher);
54 create(override_redirect);
55 _event_dispatcher->registerHandler(_window, this);
56 setStyle(_style); // let the widget initialize stuff
64 _event_dispatcher->clearHandler(_window);
66 std::for_each(_children.begin(), _children.end(), PointerAssassin());
69 _parent->removeChild(this);
71 XDestroyWindow(**display, _window);
74 void Widget::create(bool override_redirect)
76 const ScreenInfo *scr_info = display->screenInfo(_screen);
77 Window p_window = _parent ? _parent->window() : scr_info->rootWindow();
79 XSetWindowAttributes attrib_create;
80 unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
82 attrib_create.background_pixmap = None;
83 attrib_create.colormap = scr_info->colormap();
84 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
85 ButtonMotionMask | ExposureMask | StructureNotifyMask;
87 if (override_redirect) {
88 create_mask |= CWOverrideRedirect;
89 attrib_create.override_redirect = true;
93 create_mask |= CWCursor;
94 attrib_create.cursor = _cursor;
97 _window = XCreateWindow(**display, p_window, _pos.x(),
98 _pos.y(), width(), height(), 0,
99 scr_info->depth(), InputOutput,
100 scr_info->visual(), create_mask, &attrib_create);
104 void Widget::setWidth(int w)
108 setGeometry(_pos.x(), _pos.y(), w, height());
111 void Widget::setHeight(int h)
114 _fixed_height = true;
115 setGeometry(_pos.x(), _pos.y(), _pos.x(), h);
118 void Widget::move(const Point &to)
120 move(to.x(), to.y());
123 void Widget::move(int x, int y)
126 XMoveWindow(**display, _window, x, y);
130 void Widget::resize(const Point &to)
132 resize(to.x(), to.y());
135 void Widget::resize(int w, int h)
137 assert(w > 0 && h > 0);
138 _fixed_width = _fixed_height = true;
139 setGeometry(_pos.x(), _pos.y(), w, h);
142 void Widget::setGeometry(const Rect &new_geom)
144 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
147 void Widget::setGeometry(const Point &topleft, int width, int height)
149 setGeometry(topleft.x(), topleft.y(), width, height);
152 void Widget::setGeometry(int x, int y, int width, int height)
155 setSize(width, height);
158 // don't use an XMoveResizeWindow here, because it doesn't seem to move
159 // windows with StaticGravity? This works, that didn't.
160 XResizeWindow(**display, _window, width, height);
161 XMoveWindow(**display, _window, x, y);
165 void Widget::show(bool recursive)
170 // make sure the internal state isn't mangled
175 WidgetList::iterator it = _children.begin(), end = _children.end();
176 for (; it != end; ++it)
180 XMapWindow(**display, _window);
184 void Widget::hide(bool recursive)
190 WidgetList::iterator it = _children.begin(), end = _children.end();
191 for (; it != end; ++it)
195 XUnmapWindow(**display, _window);
199 void Widget::focus(void)
203 Widget::WidgetList::iterator it = _children.begin(),
204 end = _children.end();
205 for (; it != end; ++it)
209 void Widget::unfocus(void)
213 Widget::WidgetList::iterator it = _children.begin(),
214 end = _children.end();
215 for (; it != end; ++it)
219 bool Widget::grabMouse(void)
221 Status ret = XGrabPointer(**display, _window, True,
222 (ButtonPressMask | ButtonReleaseMask |
223 ButtonMotionMask | EnterWindowMask |
224 LeaveWindowMask | PointerMotionMask),
225 GrabModeSync, GrabModeAsync, None, None,
227 _grabbed_mouse = (ret == GrabSuccess);
228 return _grabbed_mouse;
231 void Widget::ungrabMouse(void)
233 if (! _grabbed_mouse)
236 XUngrabPointer(**display, CurrentTime);
237 _grabbed_mouse = false;
240 bool Widget::grabKeyboard(void)
242 Status ret = XGrabKeyboard(**display, _window, True,
243 GrabModeSync, GrabModeAsync, CurrentTime);
244 _grabbed_keyboard = (ret == GrabSuccess);
245 return _grabbed_keyboard;
249 void Widget::ungrabKeyboard(void)
251 if (! _grabbed_keyboard)
254 XUngrabKeyboard(**display, CurrentTime);
255 _grabbed_keyboard = false;
258 void Widget::render(void)
260 if (!_texture) return;
262 _bg_pixmap = _texture->render(width(), height(), _bg_pixmap);
265 XSetWindowBackgroundPixmap(**display, _window, _bg_pixmap);
268 unsigned int pix = _texture->color().pixel();
269 if (pix != _bg_pixel) {
271 XSetWindowBackground(**display, _window, pix);
276 void Widget::adjust(void)
278 if (_direction == Horizontal)
284 void Widget::adjustHorz(void)
286 if (_children.size() == 0)
290 WidgetList::iterator it, end = _children.end();
293 int width = _bevel_width;
294 WidgetList stretchable;
296 for (it = _children.begin(); it != end; ++it) {
298 if (tmp->isStretchableVert())
299 tmp->setHeight(height() > _bevel_width * 2 ?
300 height() - _bevel_width * 2 : _bevel_width);
301 if (tmp->isStretchableHorz())
302 stretchable.push_back(tmp);
304 width += tmp->width() + _bevel_width;
306 if (tmp->height() > tallest)
307 tallest = tmp->height();
310 if (stretchable.size() > 0) {
311 WidgetList::iterator str_it = stretchable.begin(),
312 str_end = stretchable.end();
314 int str_width = Surface::width() - width / stretchable.size();
316 for (; str_it != str_end; ++str_it)
317 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
321 Widget *prev_widget = 0;
323 for (it = _children.begin(); it != end; ++it) {
328 x = prev_widget->_pos.x() + prev_widget->width() + _bevel_width;
330 x = _pos.x() + _bevel_width;
331 y = (tallest - tmp->height()) / 2 + _bevel_width;
338 internalResize(width, tallest + _bevel_width * 2);
341 void Widget::adjustVert(void)
343 if (_children.size() == 0)
347 WidgetList::iterator it, end = _children.end();
350 int height = _bevel_width;
351 WidgetList stretchable;
353 for (it = _children.begin(); it != end; ++it) {
355 if (tmp->isStretchableHorz())
356 tmp->setWidth(width() > _bevel_width * 2 ?
357 width() - _bevel_width * 2 : _bevel_width);
358 if (tmp->isStretchableVert())
359 stretchable.push_back(tmp);
361 height += tmp->height() + _bevel_width;
363 if (tmp->width() > widest)
364 widest = tmp->width();
367 if (stretchable.size() > 0) {
368 WidgetList::iterator str_it = stretchable.begin(),
369 str_end = stretchable.end();
371 int str_height = Surface::height() - height / stretchable.size();
373 for (; str_it != str_end; ++str_it)
374 (*str_it)->setHeight(str_height > _bevel_width ?
375 str_height - _bevel_width : _bevel_width);
378 Widget *prev_widget = 0;
380 for (it = _children.begin(); it != end; ++it) {
385 y = prev_widget->_pos.y() + prev_widget->height() + _bevel_width;
387 y = _pos.y() + _bevel_width;
388 x = (widest - tmp->width()) / 2 + _bevel_width;
395 internalResize(widest + _bevel_width * 2, height);
398 void Widget::update(void)
403 XClearWindow(**display, _window);
406 WidgetList::iterator it = _children.begin(), end = _children.end();
407 for (; it != end; ++it)
413 void Widget::internalResize(int w, int h)
415 assert(w > 0 && h > 0);
417 if (! _fixed_width && ! _fixed_height)
419 else if (! _fixed_width)
421 else if (! _fixed_height)
425 void Widget::addChild(Widget *child, bool front)
429 _children.push_front(child);
431 _children.push_back(child);
434 void Widget::removeChild(Widget *child)
437 WidgetList::iterator it, end = _children.end();
438 for (it = _children.begin(); it != end; ++it) {
443 if (it != _children.end())
447 void Widget::setStyle(Style *style)
453 WidgetList::iterator it, end = _children.end();
454 for (it = _children.begin(); it != end; ++it)
455 (*it)->setStyle(style);
459 void Widget::setEventDispatcher(EventDispatcher *disp)
461 if (_event_dispatcher)
462 _event_dispatcher->clearHandler(_window);
463 _event_dispatcher = disp;
464 _event_dispatcher->registerHandler(_window, this);
467 void Widget::exposeHandler(const XExposeEvent &e)
469 EventHandler::exposeHandler(e);
474 void Widget::configureHandler(const XConfigureEvent &e)
476 EventHandler::configureHandler(e);
477 if (_ignore_config) {
480 if (!(e.width == width() && e.height == height())) {
482 _pos.setPoint(e.x, e.y);
483 setSize(e.width, e.height);