1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
10 #include "screeninfo.hh"
11 #include "focuslabel.hh"
17 Widget::Widget(Widget *parent, Direction direction)
19 _dirty(false), _focused(false),
20 _parent(parent), _style(parent->style()), _direction(direction),
21 _cursor(parent->cursor()), _bevel_width(parent->bevelWidth()),
23 _visible(false), _grabbed_mouse(false),
24 _grabbed_keyboard(false), _stretchable_vert(false),
25 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
26 _bcolor(0), _bwidth(0), _rect(0, 0, 1, 1), _screen(parent->screen()),
27 _fixed_width(false), _fixed_height(false),
29 _event_dispatcher(parent->eventDispatcher())
32 parent->addChild(this);
34 _event_dispatcher->registerHandler(_window, this);
37 Widget::Widget(EventDispatcher *event_dispatcher, RenderStyle *style,
38 Direction direction, Cursor cursor, int bevel_width,
39 bool override_redirect)
41 _dirty(false),_focused(false),
42 _parent(0), _style(style), _direction(direction), _cursor(cursor),
43 _bevel_width(bevel_width), _ignore_config(0), _visible(false),
44 _grabbed_mouse(false), _grabbed_keyboard(false),
45 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
46 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0), _rect(0, 0, 1, 1),
47 _screen(style->screen()), _fixed_width(false), _fixed_height(false),
49 _event_dispatcher(event_dispatcher)
51 assert(event_dispatcher);
53 create(override_redirect);
54 _event_dispatcher->registerHandler(_window, this);
62 _event_dispatcher->clearHandler(_window);
64 std::for_each(_children.begin(), _children.end(), PointerAssassin());
67 _parent->removeChild(this);
69 XDestroyWindow(**display, _window);
72 void Widget::create(bool override_redirect)
74 const ScreenInfo *scr_info = display->screenInfo(_screen);
75 Window p_window = _parent ? _parent->window() : scr_info->rootWindow();
77 _rect.setRect(0, 0, 1, 1); // just some initial values
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, _rect.x(),
98 _rect.y(), _rect.width(), _rect.height(), 0,
99 scr_info->depth(), InputOutput,
100 scr_info->visual(), create_mask, &attrib_create);
104 void Widget::setWidth(int w)
108 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
111 void Widget::setHeight(int h)
114 _fixed_height = true;
115 setGeometry(_rect.x(), _rect.y(), _rect.width(), 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(_rect.x(), _rect.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)
154 _rect = Rect(x, y, width, height);
157 // don't use an XMoveResizeWindow here, because it doesn't seem to move
158 // windows with StaticGravity? This works, that didn't.
159 XResizeWindow(**display, _window, width, height);
160 XMoveWindow(**display, _window, x, y);
164 void Widget::show(bool recursive)
169 // make sure the internal state isn't mangled
174 WidgetList::iterator it = _children.begin(), end = _children.end();
175 for (; it != end; ++it)
176 (*it)->show(recursive);
179 XMapWindow(**display, _window);
183 void Widget::hide(bool recursive)
189 WidgetList::iterator it = _children.begin(), end = _children.end();
190 for (; it != end; ++it)
194 XUnmapWindow(**display, _window);
198 void Widget::focus(void)
202 Widget::WidgetList::iterator it = _children.begin(),
203 end = _children.end();
204 for (; it != end; ++it)
208 void Widget::unfocus(void)
212 Widget::WidgetList::iterator it = _children.begin(),
213 end = _children.end();
214 for (; it != end; ++it)
218 bool Widget::grabMouse(void)
220 Status ret = XGrabPointer(**display, _window, True,
221 (ButtonPressMask | ButtonReleaseMask |
222 ButtonMotionMask | EnterWindowMask |
223 LeaveWindowMask | PointerMotionMask),
224 GrabModeSync, GrabModeAsync, None, None,
226 _grabbed_mouse = (ret == GrabSuccess);
227 return _grabbed_mouse;
230 void Widget::ungrabMouse(void)
232 if (! _grabbed_mouse)
235 XUngrabPointer(**display, CurrentTime);
236 _grabbed_mouse = false;
239 bool Widget::grabKeyboard(void)
241 Status ret = XGrabKeyboard(**display, _window, True,
242 GrabModeSync, GrabModeAsync, CurrentTime);
243 _grabbed_keyboard = (ret == GrabSuccess);
244 return _grabbed_keyboard;
248 void Widget::ungrabKeyboard(void)
250 if (! _grabbed_keyboard)
253 XUngrabKeyboard(**display, CurrentTime);
254 _grabbed_keyboard = false;
257 void Widget::render(void)
259 if (!_texture) return;
261 Surface *s = _surface; // save the current surface
263 _surface = new Surface(_screen, _rect.size());
264 display->renderControl(_screen)->drawBackground(*_surface, *_texture);
266 renderForeground(); // for inherited types to render onto the _surface
268 XSetWindowBackgroundPixmap(**display, _window, _surface->pixmap());
270 delete s; // delete the old surface *after* its pixmap isn't in use anymore
273 void Widget::adjust(void)
275 if (_direction == Horizontal)
281 void Widget::adjustHorz(void)
283 if (_children.size() == 0)
287 WidgetList::iterator it, end = _children.end();
290 int width = _bevel_width;
291 WidgetList stretchable;
293 for (it = _children.begin(); it != end; ++it) {
295 if (tmp->isStretchableVert())
296 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
297 _rect.height() - _bevel_width * 2 : _bevel_width);
298 if (tmp->isStretchableHorz())
299 stretchable.push_back(tmp);
301 width += tmp->_rect.width() + _bevel_width;
303 if (tmp->_rect.height() > tallest)
304 tallest = tmp->_rect.height();
307 if (stretchable.size() > 0) {
308 WidgetList::iterator str_it = stretchable.begin(),
309 str_end = stretchable.end();
311 int str_width = _rect.width() - width / stretchable.size();
313 for (; str_it != str_end; ++str_it)
314 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
318 Widget *prev_widget = 0;
320 for (it = _children.begin(); it != end; ++it) {
325 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
328 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
335 internalResize(width, tallest + _bevel_width * 2);
338 void Widget::adjustVert(void)
340 if (_children.size() == 0)
344 WidgetList::iterator it, end = _children.end();
347 int height = _bevel_width;
348 WidgetList stretchable;
350 for (it = _children.begin(); it != end; ++it) {
352 if (tmp->isStretchableHorz())
353 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
354 _rect.width() - _bevel_width * 2 : _bevel_width);
355 if (tmp->isStretchableVert())
356 stretchable.push_back(tmp);
358 height += tmp->_rect.height() + _bevel_width;
360 if (tmp->_rect.width() > widest)
361 widest = tmp->_rect.width();
364 if (stretchable.size() > 0) {
365 WidgetList::iterator str_it = stretchable.begin(),
366 str_end = stretchable.end();
368 int str_height = _rect.height() - height / stretchable.size();
370 for (; str_it != str_end; ++str_it)
371 (*str_it)->setHeight(str_height > _bevel_width ?
372 str_height - _bevel_width : _bevel_width);
375 Widget *prev_widget = 0;
377 for (it = _children.begin(); it != end; ++it) {
382 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
385 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
392 internalResize(widest + _bevel_width * 2, height);
395 void Widget::update(void)
400 XClearWindow(**display, _window);
403 WidgetList::iterator it = _children.begin(), end = _children.end();
404 for (; it != end; ++it)
410 void Widget::internalResize(int w, int h)
412 assert(w > 0 && h > 0);
414 if (! _fixed_width && ! _fixed_height)
416 else if (! _fixed_width)
417 resize(w, _rect.height());
418 else if (! _fixed_height)
419 resize(_rect.width(), h);
422 void Widget::addChild(Widget *child, bool front)
426 _children.push_front(child);
428 _children.push_back(child);
431 void Widget::removeChild(Widget *child)
434 WidgetList::iterator it, end = _children.end();
435 for (it = _children.begin(); it != end; ++it) {
440 if (it != _children.end())
444 void Widget::setStyle(RenderStyle *style)
450 WidgetList::iterator it, end = _children.end();
451 for (it = _children.begin(); it != end; ++it)
452 (*it)->setStyle(style);
456 void Widget::setEventDispatcher(EventDispatcher *disp)
458 if (_event_dispatcher)
459 _event_dispatcher->clearHandler(_window);
460 _event_dispatcher = disp;
461 _event_dispatcher->registerHandler(_window, this);
464 void Widget::exposeHandler(const XExposeEvent &e)
466 EventHandler::exposeHandler(e);
467 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
470 void Widget::configureHandler(const XConfigureEvent &e)
472 EventHandler::configureHandler(e);
474 if (_ignore_config) {
478 int height = e.height;
481 while (XCheckTypedWindowEvent(**display, _window, ConfigureNotify, &ev)) {
482 width = ev.xconfigure.width;
483 height = ev.xconfigure.height;
486 if (!(width == _rect.width() && height == _rect.height())) {
488 _rect.setSize(width, height);