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
)
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), _screen(parent
->screen()), _fixed_width(false),
27 _fixed_height(false), _event_dispatcher(parent
->eventDispatcher())
30 parent
->addChild(this);
32 _event_dispatcher
->registerHandler(_window
, this);
33 setStyle(_style
); // let the widget initialize stuff
36 Widget::Widget(EventDispatcher
*event_dispatcher
, Style
*style
,
37 Direction direction
, Cursor cursor
, int bevel_width
,
38 bool override_redirect
)
40 _dirty(false),_focused(false),
41 _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
42 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
43 _grabbed_mouse(false), _grabbed_keyboard(false),
44 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
45 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0),
46 _screen(style
->getScreen()), _fixed_width(false), _fixed_height(false),
47 _event_dispatcher(event_dispatcher
)
49 assert(event_dispatcher
);
51 create(override_redirect
);
52 _event_dispatcher
->registerHandler(_window
, this);
53 setStyle(_style
); // let the widget initialize stuff
61 _event_dispatcher
->clearHandler(_window
);
63 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
66 _parent
->removeChild(this);
68 XDestroyWindow(**display
, _window
);
71 void Widget::create(bool override_redirect
)
73 const ScreenInfo
*scr_info
= display
->screenInfo(_screen
);
74 Window p_window
= _parent
? _parent
->window() : scr_info
->rootWindow();
76 _rect
.setRect(0, 0, 1, 1); // just some initial values
78 XSetWindowAttributes attrib_create
;
79 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
81 attrib_create
.background_pixmap
= None
;
82 attrib_create
.colormap
= scr_info
->colormap();
83 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
84 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
86 if (override_redirect
) {
87 create_mask
|= CWOverrideRedirect
;
88 attrib_create
.override_redirect
= true;
92 create_mask
|= CWCursor
;
93 attrib_create
.cursor
= _cursor
;
96 _window
= XCreateWindow(**display
, p_window
, _rect
.x(),
97 _rect
.y(), _rect
.width(), _rect
.height(), 0,
98 scr_info
->depth(), InputOutput
,
99 scr_info
->visual(), create_mask
, &attrib_create
);
103 void Widget::setWidth(int w
)
107 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
110 void Widget::setHeight(int h
)
113 _fixed_height
= true;
114 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
117 void Widget::move(const Point
&to
)
119 move(to
.x(), to
.y());
122 void Widget::move(int x
, int y
)
125 XMoveWindow(**display
, _window
, x
, y
);
129 void Widget::resize(const Point
&to
)
131 resize(to
.x(), to
.y());
134 void Widget::resize(int w
, int h
)
136 assert(w
> 0 && h
> 0);
137 _fixed_width
= _fixed_height
= true;
138 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
141 void Widget::setGeometry(const Rect
&new_geom
)
143 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
146 void Widget::setGeometry(const Point
&topleft
, int width
, int height
)
148 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
151 void Widget::setGeometry(int x
, int y
, int width
, int height
)
153 _rect
= Rect(x
, y
, width
, height
);
156 XResizeWindow(**display
, _window
, width
, height
);
157 XMoveWindow(**display
, _window
, x
, y
);
161 void Widget::show(bool recursive
)
166 // make sure the internal state isn't mangled
171 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
172 for (; it
!= end
; ++it
)
176 XMapWindow(**display
, _window
);
180 void Widget::hide(bool recursive
)
186 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
187 for (; it
!= end
; ++it
)
191 XUnmapWindow(**display
, _window
);
195 void Widget::focus(void)
199 Widget::WidgetList::iterator it
= _children
.begin(),
200 end
= _children
.end();
201 for (; it
!= end
; ++it
)
205 void Widget::unfocus(void)
209 Widget::WidgetList::iterator it
= _children
.begin(),
210 end
= _children
.end();
211 for (; it
!= end
; ++it
)
215 bool Widget::grabMouse(void)
217 Status ret
= XGrabPointer(**display
, _window
, True
,
218 (ButtonPressMask
| ButtonReleaseMask
|
219 ButtonMotionMask
| EnterWindowMask
|
220 LeaveWindowMask
| PointerMotionMask
),
221 GrabModeSync
, GrabModeAsync
, None
, None
,
223 _grabbed_mouse
= (ret
== GrabSuccess
);
224 return _grabbed_mouse
;
227 void Widget::ungrabMouse(void)
229 if (! _grabbed_mouse
)
232 XUngrabPointer(**display
, CurrentTime
);
233 _grabbed_mouse
= false;
236 bool Widget::grabKeyboard(void)
238 Status ret
= XGrabKeyboard(**display
, _window
, True
,
239 GrabModeSync
, GrabModeAsync
, CurrentTime
);
240 _grabbed_keyboard
= (ret
== GrabSuccess
);
241 return _grabbed_keyboard
;
245 void Widget::ungrabKeyboard(void)
247 if (! _grabbed_keyboard
)
250 XUngrabKeyboard(**display
, CurrentTime
);
251 _grabbed_keyboard
= false;
254 void Widget::render(void)
256 if (!_texture
) return;
258 _bg_pixmap
= _texture
->render(_rect
.width(), _rect
.height(), _bg_pixmap
);
261 XSetWindowBackgroundPixmap(**display
, _window
, _bg_pixmap
);
264 unsigned int pix
= _texture
->color().pixel();
265 if (pix
!= _bg_pixel
) {
267 XSetWindowBackground(**display
, _window
, pix
);
272 void Widget::adjust(void)
274 if (_direction
== Horizontal
)
280 void Widget::adjustHorz(void)
282 if (_children
.size() == 0)
286 WidgetList::iterator it
, end
= _children
.end();
289 int width
= _bevel_width
;
290 WidgetList stretchable
;
292 for (it
= _children
.begin(); it
!= end
; ++it
) {
294 if (tmp
->isStretchableVert())
295 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
296 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
297 if (tmp
->isStretchableHorz())
298 stretchable
.push_back(tmp
);
300 width
+= tmp
->_rect
.width() + _bevel_width
;
302 if (tmp
->_rect
.height() > tallest
)
303 tallest
= tmp
->_rect
.height();
306 if (stretchable
.size() > 0) {
307 WidgetList::iterator str_it
= stretchable
.begin(),
308 str_end
= stretchable
.end();
310 int str_width
= _rect
.width() - width
/ stretchable
.size();
312 for (; str_it
!= str_end
; ++str_it
)
313 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
317 Widget
*prev_widget
= 0;
319 for (it
= _children
.begin(); it
!= end
; ++it
) {
324 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
326 x
= _rect
.x() + _bevel_width
;
327 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
334 internalResize(width
, tallest
+ _bevel_width
* 2);
337 void Widget::adjustVert(void)
339 if (_children
.size() == 0)
343 WidgetList::iterator it
, end
= _children
.end();
346 int height
= _bevel_width
;
347 WidgetList stretchable
;
349 for (it
= _children
.begin(); it
!= end
; ++it
) {
351 if (tmp
->isStretchableHorz())
352 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
353 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
354 if (tmp
->isStretchableVert())
355 stretchable
.push_back(tmp
);
357 height
+= tmp
->_rect
.height() + _bevel_width
;
359 if (tmp
->_rect
.width() > widest
)
360 widest
= tmp
->_rect
.width();
363 if (stretchable
.size() > 0) {
364 WidgetList::iterator str_it
= stretchable
.begin(),
365 str_end
= stretchable
.end();
367 int str_height
= _rect
.height() - height
/ stretchable
.size();
369 for (; str_it
!= str_end
; ++str_it
)
370 (*str_it
)->setHeight(str_height
> _bevel_width
?
371 str_height
- _bevel_width
: _bevel_width
);
374 Widget
*prev_widget
= 0;
376 for (it
= _children
.begin(); it
!= end
; ++it
) {
381 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
383 y
= _rect
.y() + _bevel_width
;
384 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
391 internalResize(widest
+ _bevel_width
* 2, height
);
394 void Widget::update(void)
399 XClearWindow(**display
, _window
);
402 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
403 for (; it
!= end
; ++it
)
409 void Widget::internalResize(int w
, int h
)
411 assert(w
> 0 && h
> 0);
413 if (! _fixed_width
&& ! _fixed_height
)
415 else if (! _fixed_width
)
416 resize(w
, _rect
.height());
417 else if (! _fixed_height
)
418 resize(_rect
.width(), h
);
421 void Widget::addChild(Widget
*child
, bool front
)
425 _children
.push_front(child
);
427 _children
.push_back(child
);
430 void Widget::removeChild(Widget
*child
)
433 WidgetList::iterator it
, end
= _children
.end();
434 for (it
= _children
.begin(); it
!= end
; ++it
) {
439 if (it
!= _children
.end())
443 void Widget::setStyle(Style
*style
)
449 WidgetList::iterator it
, end
= _children
.end();
450 for (it
= _children
.begin(); it
!= end
; ++it
)
451 (*it
)->setStyle(style
);
455 void Widget::setEventDispatcher(EventDispatcher
*disp
)
457 if (_event_dispatcher
)
458 _event_dispatcher
->clearHandler(_window
);
459 _event_dispatcher
= disp
;
460 _event_dispatcher
->registerHandler(_window
, this);
463 void Widget::exposeHandler(const XExposeEvent
&e
)
465 EventHandler::exposeHandler(e
);
470 void Widget::configureHandler(const XConfigureEvent
&e
)
472 EventHandler::configureHandler(e
);
473 if (_ignore_config
) {
476 if (!(e
.width
== _rect
.width() && e
.height
== _rect
.height())) {
478 _rect
.setSize(e
.width
, e
.height
);