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), _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);
35 setStyle(_style
); // let the widget initialize stuff
38 Widget::Widget(EventDispatcher
*event_dispatcher
, RenderStyle
*style
,
39 Direction direction
, Cursor cursor
, int bevel_width
,
40 bool override_redirect
)
42 _dirty(false),_focused(false),
43 _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
44 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
45 _grabbed_mouse(false), _grabbed_keyboard(false),
46 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
47 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0), _rect(0, 0, 1, 1),
48 _screen(style
->screen()), _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 _rect
.setRect(0, 0, 1, 1); // just some initial values
81 XSetWindowAttributes attrib_create
;
82 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
84 attrib_create
.background_pixmap
= None
;
85 attrib_create
.colormap
= scr_info
->colormap();
86 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
87 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
89 if (override_redirect
) {
90 create_mask
|= CWOverrideRedirect
;
91 attrib_create
.override_redirect
= true;
95 create_mask
|= CWCursor
;
96 attrib_create
.cursor
= _cursor
;
99 _window
= XCreateWindow(**display
, p_window
, _rect
.x(),
100 _rect
.y(), _rect
.width(), _rect
.height(), 0,
101 scr_info
->depth(), InputOutput
,
102 scr_info
->visual(), create_mask
, &attrib_create
);
106 void Widget::setWidth(int w
)
110 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
113 void Widget::setHeight(int h
)
116 _fixed_height
= true;
117 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
120 void Widget::move(const Point
&to
)
122 move(to
.x(), to
.y());
125 void Widget::move(int x
, int y
)
128 XMoveWindow(**display
, _window
, x
, y
);
132 void Widget::resize(const Point
&to
)
134 resize(to
.x(), to
.y());
137 void Widget::resize(int w
, int h
)
139 assert(w
> 0 && h
> 0);
140 _fixed_width
= _fixed_height
= true;
141 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
144 void Widget::setGeometry(const Rect
&new_geom
)
146 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
149 void Widget::setGeometry(const Point
&topleft
, int width
, int height
)
151 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
154 void Widget::setGeometry(int x
, int y
, int width
, int height
)
156 _rect
= Rect(x
, y
, width
, height
);
159 // don't use an XMoveResizeWindow here, because it doesn't seem to move
160 // windows with StaticGravity? This works, that didn't.
161 XResizeWindow(**display
, _window
, width
, height
);
162 XMoveWindow(**display
, _window
, x
, y
);
166 void Widget::show(bool recursive
)
171 // make sure the internal state isn't mangled
176 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
177 for (; it
!= end
; ++it
)
181 XMapWindow(**display
, _window
);
185 void Widget::hide(bool recursive
)
191 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
192 for (; it
!= end
; ++it
)
196 XUnmapWindow(**display
, _window
);
200 void Widget::focus(void)
204 Widget::WidgetList::iterator it
= _children
.begin(),
205 end
= _children
.end();
206 for (; it
!= end
; ++it
)
210 void Widget::unfocus(void)
214 Widget::WidgetList::iterator it
= _children
.begin(),
215 end
= _children
.end();
216 for (; it
!= end
; ++it
)
220 bool Widget::grabMouse(void)
222 Status ret
= XGrabPointer(**display
, _window
, True
,
223 (ButtonPressMask
| ButtonReleaseMask
|
224 ButtonMotionMask
| EnterWindowMask
|
225 LeaveWindowMask
| PointerMotionMask
),
226 GrabModeSync
, GrabModeAsync
, None
, None
,
228 _grabbed_mouse
= (ret
== GrabSuccess
);
229 return _grabbed_mouse
;
232 void Widget::ungrabMouse(void)
234 if (! _grabbed_mouse
)
237 XUngrabPointer(**display
, CurrentTime
);
238 _grabbed_mouse
= false;
241 bool Widget::grabKeyboard(void)
243 Status ret
= XGrabKeyboard(**display
, _window
, True
,
244 GrabModeSync
, GrabModeAsync
, CurrentTime
);
245 _grabbed_keyboard
= (ret
== GrabSuccess
);
246 return _grabbed_keyboard
;
250 void Widget::ungrabKeyboard(void)
252 if (! _grabbed_keyboard
)
255 XUngrabKeyboard(**display
, CurrentTime
);
256 _grabbed_keyboard
= false;
259 void Widget::render(void)
261 if (!_texture
) return;
264 Surface
*s
= _surface
; // save the current surface
266 _surface
= new Surface(_screen
, _rect
.size());
267 display
->renderControl(_screen
)->drawBackground(*_surface
, *_texture
);
271 XSetWindowBackgroundPixmap(**display
, _window
, _surface
->pixmap());
273 delete s
; // delete the old surface *after* its pixmap isn't in use anymore
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(_rect
.height() > _bevel_width
* 2 ?
300 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
301 if (tmp
->isStretchableHorz())
302 stretchable
.push_back(tmp
);
304 width
+= tmp
->_rect
.width() + _bevel_width
;
306 if (tmp
->_rect
.height() > tallest
)
307 tallest
= tmp
->_rect
.height();
310 if (stretchable
.size() > 0) {
311 WidgetList::iterator str_it
= stretchable
.begin(),
312 str_end
= stretchable
.end();
314 int str_width
= _rect
.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
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
330 x
= _rect
.x() + _bevel_width
;
331 y
= (tallest
- tmp
->_rect
.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(_rect
.width() > _bevel_width
* 2 ?
357 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
358 if (tmp
->isStretchableVert())
359 stretchable
.push_back(tmp
);
361 height
+= tmp
->_rect
.height() + _bevel_width
;
363 if (tmp
->_rect
.width() > widest
)
364 widest
= tmp
->_rect
.width();
367 if (stretchable
.size() > 0) {
368 WidgetList::iterator str_it
= stretchable
.begin(),
369 str_end
= stretchable
.end();
371 int str_height
= _rect
.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
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
387 y
= _rect
.y() + _bevel_width
;
388 x
= (widest
- tmp
->_rect
.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
)
420 resize(w
, _rect
.height());
421 else if (! _fixed_height
)
422 resize(_rect
.width(), h
);
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(RenderStyle
*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
);
470 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
473 void Widget::configureHandler(const XConfigureEvent
&e
)
475 EventHandler::configureHandler(e
);
477 if (_ignore_config
) {
481 int height
= e
.height
;
484 while (XCheckTypedWindowEvent(**display
, _window
, ConfigureNotify
, &ev
)) {
485 width
= ev
.xconfigure
.width
;
486 height
= ev
.xconfigure
.height
;
489 if (!(width
== _rect
.width() && height
== _rect
.height())) {
491 _rect
.setSize(width
, height
);