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);
65 _event_dispatcher
->clearHandler(_window
);
67 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
70 _parent
->removeChild(this);
72 XDestroyWindow(**display
, _window
);
75 void Widget::create(bool override_redirect
)
77 const ScreenInfo
*scr_info
= display
->screenInfo(_screen
);
78 Window p_window
= _parent
? _parent
->window() : scr_info
->rootWindow();
80 _rect
.setRect(0, 0, 1, 1); // just some initial values
82 XSetWindowAttributes attrib_create
;
83 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
85 attrib_create
.background_pixmap
= None
;
86 attrib_create
.colormap
= scr_info
->colormap();
87 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
88 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
90 if (override_redirect
) {
91 create_mask
|= CWOverrideRedirect
;
92 attrib_create
.override_redirect
= true;
96 create_mask
|= CWCursor
;
97 attrib_create
.cursor
= _cursor
;
100 _window
= XCreateWindow(**display
, p_window
, _rect
.x(),
101 _rect
.y(), _rect
.width(), _rect
.height(), 0,
102 scr_info
->depth(), InputOutput
,
103 scr_info
->visual(), create_mask
, &attrib_create
);
107 void Widget::setWidth(int w
)
111 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
114 void Widget::setHeight(int h
)
117 _fixed_height
= true;
118 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
121 void Widget::move(const Point
&to
)
123 move(to
.x(), to
.y());
126 void Widget::move(int x
, int y
)
129 XMoveWindow(**display
, _window
, x
, y
);
133 void Widget::resize(const Point
&to
)
135 resize(to
.x(), to
.y());
138 void Widget::resize(int w
, int h
)
140 assert(w
> 0 && h
> 0);
141 _fixed_width
= _fixed_height
= true;
142 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
145 void Widget::setGeometry(const Rect
&new_geom
)
147 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
150 void Widget::setGeometry(const Point
&topleft
, int width
, int height
)
152 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
155 void Widget::setGeometry(int x
, int y
, int width
, int height
)
157 _rect
= Rect(x
, y
, width
, height
);
160 // make all parents dirty too
167 // don't use an XMoveResizeWindow here, because it doesn't seem to move
168 // windows with StaticGravity? This works, that didn't.
169 XResizeWindow(**display
, _window
, width
, height
);
170 XMoveWindow(**display
, _window
, x
, y
);
174 void Widget::show(bool recursive
)
179 // make sure the internal state isn't mangled
184 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
185 for (; it
!= end
; ++it
)
186 (*it
)->show(recursive
);
189 XMapWindow(**display
, _window
);
193 void Widget::hide(bool recursive
)
199 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
200 for (; it
!= end
; ++it
)
204 XUnmapWindow(**display
, _window
);
208 void Widget::focus(void)
212 Widget::WidgetList::iterator it
= _children
.begin(),
213 end
= _children
.end();
214 for (; it
!= end
; ++it
)
218 void Widget::unfocus(void)
222 Widget::WidgetList::iterator it
= _children
.begin(),
223 end
= _children
.end();
224 for (; it
!= end
; ++it
)
228 bool Widget::grabMouse(void)
230 Status ret
= XGrabPointer(**display
, _window
, True
,
231 (ButtonPressMask
| ButtonReleaseMask
|
232 ButtonMotionMask
| EnterWindowMask
|
233 LeaveWindowMask
| PointerMotionMask
),
234 GrabModeSync
, GrabModeAsync
, None
, None
,
236 _grabbed_mouse
= (ret
== GrabSuccess
);
237 return _grabbed_mouse
;
240 void Widget::ungrabMouse(void)
242 if (! _grabbed_mouse
)
245 XUngrabPointer(**display
, CurrentTime
);
246 _grabbed_mouse
= false;
249 bool Widget::grabKeyboard(void)
251 Status ret
= XGrabKeyboard(**display
, _window
, True
,
252 GrabModeSync
, GrabModeAsync
, CurrentTime
);
253 _grabbed_keyboard
= (ret
== GrabSuccess
);
254 return _grabbed_keyboard
;
258 void Widget::ungrabKeyboard(void)
260 if (! _grabbed_keyboard
)
263 XUngrabKeyboard(**display
, CurrentTime
);
264 _grabbed_keyboard
= false;
267 void Widget::render(void)
269 if (!_texture
) return;
271 Surface
*s
= _surface
; // save the current surface
273 _surface
= new Surface(_screen
, _rect
.size());
274 display
->renderControl(_screen
)->drawBackground(*_surface
, *_texture
);
276 renderForeground(); // for inherited types to render onto the _surface
278 XSetWindowBackgroundPixmap(**display
, _window
, _surface
->pixmap());
281 delete s
; // delete the old surface *after* its pixmap isn't in use anymore
284 void Widget::adjust(void)
286 if (_direction
== Horizontal
)
292 void Widget::adjustHorz(void)
294 if (_children
.size() == 0)
298 WidgetList::iterator it
, end
= _children
.end();
301 int width
= _bevel_width
;
302 WidgetList stretchable
;
304 for (it
= _children
.begin(); it
!= end
; ++it
) {
306 if (tmp
->isStretchableVert())
307 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
308 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
309 if (tmp
->isStretchableHorz())
310 stretchable
.push_back(tmp
);
312 width
+= tmp
->_rect
.width() + _bevel_width
;
314 if (tmp
->_rect
.height() > tallest
)
315 tallest
= tmp
->_rect
.height();
318 if (stretchable
.size() > 0) {
319 WidgetList::iterator str_it
= stretchable
.begin(),
320 str_end
= stretchable
.end();
322 int str_width
= _rect
.width() - width
/ stretchable
.size();
324 for (; str_it
!= str_end
; ++str_it
)
325 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
329 Widget
*prev_widget
= 0;
331 for (it
= _children
.begin(); it
!= end
; ++it
) {
336 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
339 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
345 internalResize(width
, tallest
+ _bevel_width
* 2);
348 void Widget::adjustVert(void)
350 if (_children
.size() == 0)
354 WidgetList::iterator it
, end
= _children
.end();
357 int height
= _bevel_width
;
358 WidgetList stretchable
;
360 for (it
= _children
.begin(); it
!= end
; ++it
) {
362 if (tmp
->isStretchableHorz())
363 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
364 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
365 if (tmp
->isStretchableVert())
366 stretchable
.push_back(tmp
);
368 height
+= tmp
->_rect
.height() + _bevel_width
;
370 if (tmp
->_rect
.width() > widest
)
371 widest
= tmp
->_rect
.width();
374 if (stretchable
.size() > 0) {
375 WidgetList::iterator str_it
= stretchable
.begin(),
376 str_end
= stretchable
.end();
378 int str_height
= _rect
.height() - height
/ stretchable
.size();
380 for (; str_it
!= str_end
; ++str_it
)
381 (*str_it
)->setHeight(str_height
> _bevel_width
?
382 str_height
- _bevel_width
: _bevel_width
);
385 Widget
*prev_widget
= 0;
387 for (it
= _children
.begin(); it
!= end
; ++it
) {
392 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
395 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
402 internalResize(widest
+ _bevel_width
* 2, height
);
405 void Widget::update()
410 XClearWindow(**display
, _window
);
413 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
414 for (; it
!= end
; ++it
)
420 void Widget::internalResize(int w
, int h
)
422 assert(w
> 0 && h
> 0);
424 bool fw
= _fixed_width
, fh
= _fixed_height
;
429 resize(w
, _rect
.height());
431 resize(_rect
.width(), h
);
437 void Widget::addChild(Widget
*child
, bool front
)
441 _children
.push_front(child
);
443 _children
.push_back(child
);
446 void Widget::removeChild(Widget
*child
)
449 WidgetList::iterator it
, end
= _children
.end();
450 for (it
= _children
.begin(); it
!= end
; ++it
) {
455 if (it
!= _children
.end())
459 void Widget::setStyle(RenderStyle
*style
)
465 WidgetList::iterator it
, end
= _children
.end();
466 for (it
= _children
.begin(); it
!= end
; ++it
)
467 (*it
)->setStyle(style
);
471 void Widget::setEventDispatcher(EventDispatcher
*disp
)
473 if (_event_dispatcher
)
474 _event_dispatcher
->clearHandler(_window
);
475 _event_dispatcher
= disp
;
476 _event_dispatcher
->registerHandler(_window
, this);
479 void Widget::exposeHandler(const XExposeEvent
&e
)
481 EventHandler::exposeHandler(e
);
482 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
485 void Widget::configureHandler(const XConfigureEvent
&e
)
487 EventHandler::configureHandler(e
);
489 if (_ignore_config
) {
493 int height
= e
.height
;
496 while (XCheckTypedWindowEvent(**display
, _window
, ConfigureNotify
, &ev
)) {
497 width
= ev
.xconfigure
.width
;
498 height
= ev
.xconfigure
.height
;
501 if (!(width
== _rect
.width() && height
== _rect
.height())) {
503 _rect
.setSize(width
, height
);