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 // make all parents dirty too
164 // don't use an XMoveResizeWindow here, because it doesn't seem to move
165 // windows with StaticGravity? This works, that didn't.
166 XResizeWindow(**display
, _window
, width
, height
);
167 XMoveWindow(**display
, _window
, x
, y
);
171 void Widget::show(bool recursive
)
176 // make sure the internal state isn't mangled
181 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
182 for (; it
!= end
; ++it
)
183 (*it
)->show(recursive
);
186 XMapWindow(**display
, _window
);
190 void Widget::hide(bool recursive
)
196 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
197 for (; it
!= end
; ++it
)
201 XUnmapWindow(**display
, _window
);
205 void Widget::focus(void)
209 Widget::WidgetList::iterator it
= _children
.begin(),
210 end
= _children
.end();
211 for (; it
!= end
; ++it
)
215 void Widget::unfocus(void)
219 Widget::WidgetList::iterator it
= _children
.begin(),
220 end
= _children
.end();
221 for (; it
!= end
; ++it
)
225 bool Widget::grabMouse(void)
227 Status ret
= XGrabPointer(**display
, _window
, True
,
228 (ButtonPressMask
| ButtonReleaseMask
|
229 ButtonMotionMask
| EnterWindowMask
|
230 LeaveWindowMask
| PointerMotionMask
),
231 GrabModeSync
, GrabModeAsync
, None
, None
,
233 _grabbed_mouse
= (ret
== GrabSuccess
);
234 return _grabbed_mouse
;
237 void Widget::ungrabMouse(void)
239 if (! _grabbed_mouse
)
242 XUngrabPointer(**display
, CurrentTime
);
243 _grabbed_mouse
= false;
246 bool Widget::grabKeyboard(void)
248 Status ret
= XGrabKeyboard(**display
, _window
, True
,
249 GrabModeSync
, GrabModeAsync
, CurrentTime
);
250 _grabbed_keyboard
= (ret
== GrabSuccess
);
251 return _grabbed_keyboard
;
255 void Widget::ungrabKeyboard(void)
257 if (! _grabbed_keyboard
)
260 XUngrabKeyboard(**display
, CurrentTime
);
261 _grabbed_keyboard
= false;
264 void Widget::render(void)
266 if (!_texture
) return;
268 Surface
*s
= _surface
; // save the current surface
270 _surface
= new Surface(_screen
, _rect
.size());
271 display
->renderControl(_screen
)->drawBackground(*_surface
, *_texture
);
273 renderForeground(); // for inherited types to render onto the _surface
275 XSetWindowBackgroundPixmap(**display
, _window
, _surface
->pixmap());
277 delete s
; // delete the old surface *after* its pixmap isn't in use anymore
280 void Widget::adjust(void)
282 if (_direction
== Horizontal
)
288 void Widget::adjustHorz(void)
290 if (_children
.size() == 0)
294 WidgetList::iterator it
, end
= _children
.end();
297 int width
= _bevel_width
;
298 WidgetList stretchable
;
300 for (it
= _children
.begin(); it
!= end
; ++it
) {
302 if (tmp
->isStretchableVert())
303 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
304 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
305 if (tmp
->isStretchableHorz())
306 stretchable
.push_back(tmp
);
308 width
+= tmp
->_rect
.width() + _bevel_width
;
310 if (tmp
->_rect
.height() > tallest
)
311 tallest
= tmp
->_rect
.height();
314 if (stretchable
.size() > 0) {
315 WidgetList::iterator str_it
= stretchable
.begin(),
316 str_end
= stretchable
.end();
318 int str_width
= _rect
.width() - width
/ stretchable
.size();
320 for (; str_it
!= str_end
; ++str_it
)
321 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
325 Widget
*prev_widget
= 0;
327 for (it
= _children
.begin(); it
!= end
; ++it
) {
332 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
335 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
341 internalResize(width
, tallest
+ _bevel_width
* 2);
344 void Widget::adjustVert(void)
346 if (_children
.size() == 0)
350 WidgetList::iterator it
, end
= _children
.end();
353 int height
= _bevel_width
;
354 WidgetList stretchable
;
356 for (it
= _children
.begin(); it
!= end
; ++it
) {
358 if (tmp
->isStretchableHorz())
359 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
360 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
361 if (tmp
->isStretchableVert())
362 stretchable
.push_back(tmp
);
364 height
+= tmp
->_rect
.height() + _bevel_width
;
366 if (tmp
->_rect
.width() > widest
)
367 widest
= tmp
->_rect
.width();
370 if (stretchable
.size() > 0) {
371 WidgetList::iterator str_it
= stretchable
.begin(),
372 str_end
= stretchable
.end();
374 int str_height
= _rect
.height() - height
/ stretchable
.size();
376 for (; str_it
!= str_end
; ++str_it
)
377 (*str_it
)->setHeight(str_height
> _bevel_width
?
378 str_height
- _bevel_width
: _bevel_width
);
381 Widget
*prev_widget
= 0;
383 for (it
= _children
.begin(); it
!= end
; ++it
) {
388 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
391 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
398 internalResize(widest
+ _bevel_width
* 2, height
);
401 void Widget::update()
406 XClearWindow(**display
, _window
);
409 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
410 for (; it
!= end
; ++it
)
416 void Widget::internalResize(int w
, int h
)
418 assert(w
> 0 && h
> 0);
420 bool fw
= _fixed_width
, fh
= _fixed_height
;
425 resize(w
, _rect
.height());
427 resize(_rect
.width(), h
);
433 void Widget::addChild(Widget
*child
, bool front
)
437 _children
.push_front(child
);
439 _children
.push_back(child
);
442 void Widget::removeChild(Widget
*child
)
445 WidgetList::iterator it
, end
= _children
.end();
446 for (it
= _children
.begin(); it
!= end
; ++it
) {
451 if (it
!= _children
.end())
455 void Widget::setStyle(RenderStyle
*style
)
461 WidgetList::iterator it
, end
= _children
.end();
462 for (it
= _children
.begin(); it
!= end
; ++it
)
463 (*it
)->setStyle(style
);
467 void Widget::setEventDispatcher(EventDispatcher
*disp
)
469 if (_event_dispatcher
)
470 _event_dispatcher
->clearHandler(_window
);
471 _event_dispatcher
= disp
;
472 _event_dispatcher
->registerHandler(_window
, this);
475 void Widget::exposeHandler(const XExposeEvent
&e
)
477 EventHandler::exposeHandler(e
);
478 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
481 void Widget::configureHandler(const XConfigureEvent
&e
)
483 EventHandler::configureHandler(e
);
485 if (_ignore_config
) {
489 int height
= e
.height
;
492 while (XCheckTypedWindowEvent(**display
, _window
, ConfigureNotify
, &ev
)) {
493 width
= ev
.xconfigure
.width
;
494 height
= ev
.xconfigure
.height
;
497 if (!(width
== _rect
.width() && height
== _rect
.height())) {
499 _rect
.setSize(width
, height
);