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),
28 _event_mask(ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
|
29 ExposureMask
| StructureNotifyMask
),
31 _event_dispatcher(parent
->eventDispatcher())
34 parent
->addChild(this);
36 _event_dispatcher
->registerHandler(_window
, this);
39 Widget::Widget(EventDispatcher
*event_dispatcher
, RenderStyle
*style
,
40 Direction direction
, Cursor cursor
, int bevel_width
,
41 bool override_redirect
)
43 _dirty(false),_focused(false),
44 _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
45 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
46 _grabbed_mouse(false), _grabbed_keyboard(false),
47 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
48 _bg_pixmap(0), _bg_pixel(0), _bcolor(0), _bwidth(0), _rect(0, 0, 1, 1),
49 _screen(style
->screen()), _fixed_width(false), _fixed_height(false),
50 _event_mask(ButtonPressMask
| ButtonReleaseMask
| ButtonMotionMask
|
51 ExposureMask
| StructureNotifyMask
),
53 _event_dispatcher(event_dispatcher
)
55 assert(event_dispatcher
);
57 create(override_redirect
);
58 _event_dispatcher
->registerHandler(_window
, this);
69 _event_dispatcher
->clearHandler(_window
);
71 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
74 _parent
->removeChild(this);
76 XDestroyWindow(**display
, _window
);
79 void Widget::create(bool override_redirect
)
81 const ScreenInfo
*scr_info
= display
->screenInfo(_screen
);
82 Window p_window
= _parent
? _parent
->window() : scr_info
->rootWindow();
84 _rect
.setRect(0, 0, 1, 1); // just some initial values
86 XSetWindowAttributes attrib_create
;
87 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
89 attrib_create
.background_pixmap
= None
;
90 attrib_create
.colormap
= scr_info
->colormap();
91 attrib_create
.event_mask
= _event_mask
;
93 if (override_redirect
) {
94 create_mask
|= CWOverrideRedirect
;
95 attrib_create
.override_redirect
= true;
99 create_mask
|= CWCursor
;
100 attrib_create
.cursor
= _cursor
;
103 _window
= XCreateWindow(**display
, p_window
, _rect
.x(),
104 _rect
.y(), _rect
.width(), _rect
.height(), 0,
105 scr_info
->depth(), InputOutput
,
106 scr_info
->visual(), create_mask
, &attrib_create
);
110 void Widget::setEventMask(long e
)
112 XSelectInput(**display
, _window
, e
);
116 void Widget::setWidth(int w
)
120 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
123 void Widget::setHeight(int h
)
126 _fixed_height
= true;
127 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
130 void Widget::move(const Point
&to
)
132 move(to
.x(), to
.y());
135 void Widget::move(int x
, int y
)
138 XMoveWindow(**display
, _window
, x
, y
);
142 void Widget::resize(const Point
&to
)
144 resize(to
.x(), to
.y());
147 void Widget::resize(int w
, int h
)
149 assert(w
> 0 && h
> 0);
150 _fixed_width
= _fixed_height
= true;
151 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
154 void Widget::setGeometry(const Rect
&new_geom
)
156 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
159 void Widget::setGeometry(const Point
&topleft
, int width
, int height
)
161 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
164 void Widget::setGeometry(int x
, int y
, int width
, int height
)
166 _rect
= Rect(x
, y
, width
, height
);
169 // make all parents dirty too
176 // don't use an XMoveResizeWindow here, because it doesn't seem to move
177 // windows with StaticGravity? This works, that didn't.
178 XResizeWindow(**display
, _window
, width
, height
);
179 XMoveWindow(**display
, _window
, x
, y
);
183 void Widget::show(bool recursive
)
188 // make sure the internal state isn't mangled
193 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
194 for (; it
!= end
; ++it
)
195 (*it
)->show(recursive
);
198 XMapWindow(**display
, _window
);
202 void Widget::hide(bool recursive
)
208 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
209 for (; it
!= end
; ++it
)
213 XUnmapWindow(**display
, _window
);
217 void Widget::focus(void)
221 Widget::WidgetList::iterator it
= _children
.begin(),
222 end
= _children
.end();
223 for (; it
!= end
; ++it
)
227 void Widget::unfocus(void)
231 Widget::WidgetList::iterator it
= _children
.begin(),
232 end
= _children
.end();
233 for (; it
!= end
; ++it
)
237 bool Widget::grabMouse(void)
239 Status ret
= XGrabPointer(**display
, _window
, True
,
240 (ButtonPressMask
| ButtonReleaseMask
|
241 ButtonMotionMask
| EnterWindowMask
|
242 LeaveWindowMask
| PointerMotionMask
),
243 GrabModeSync
, GrabModeAsync
, None
, None
,
245 _grabbed_mouse
= (ret
== GrabSuccess
);
246 return _grabbed_mouse
;
249 void Widget::ungrabMouse(void)
251 if (! _grabbed_mouse
)
254 XUngrabPointer(**display
, CurrentTime
);
255 _grabbed_mouse
= false;
258 bool Widget::grabKeyboard(void)
260 Status ret
= XGrabKeyboard(**display
, _window
, True
,
261 GrabModeSync
, GrabModeAsync
, CurrentTime
);
262 _grabbed_keyboard
= (ret
== GrabSuccess
);
263 return _grabbed_keyboard
;
267 void Widget::ungrabKeyboard(void)
269 if (! _grabbed_keyboard
)
272 XUngrabKeyboard(**display
, CurrentTime
);
273 _grabbed_keyboard
= false;
276 void Widget::render(void)
279 XSetWindowBackgroundPixmap(**display
, _window
, ParentRelative
);
283 Surface
*s
= _surface
; // save the current surface
285 _surface
= new Surface(_screen
, _rect
.size());
286 display
->renderControl(_screen
)->drawBackground(*_surface
, *_texture
);
288 renderForeground(); // for inherited types to render onto the _surface
290 XSetWindowBackgroundPixmap(**display
, _window
, _surface
->pixmap());
293 delete s
; // delete the old surface *after* its pixmap isn't in use anymore
296 void Widget::adjust(void)
298 if (_direction
== Horizontal
)
304 void Widget::adjustHorz(void)
306 if (_children
.size() == 0)
310 WidgetList::iterator it
, end
= _children
.end();
313 int width
= _bevel_width
;
314 WidgetList stretchable
;
316 for (it
= _children
.begin(); it
!= end
; ++it
) {
318 if (tmp
->isStretchableVert())
319 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
320 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
321 if (tmp
->isStretchableHorz())
322 stretchable
.push_back(tmp
);
324 width
+= tmp
->_rect
.width() + _bevel_width
;
326 if (tmp
->_rect
.height() > tallest
)
327 tallest
= tmp
->_rect
.height();
330 if (stretchable
.size() > 0) {
331 WidgetList::iterator str_it
= stretchable
.begin(),
332 str_end
= stretchable
.end();
334 int str_width
= _rect
.width() - width
/ stretchable
.size();
336 for (; str_it
!= str_end
; ++str_it
)
337 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
341 Widget
*prev_widget
= 0;
343 for (it
= _children
.begin(); it
!= end
; ++it
) {
348 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
351 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
357 internalResize(width
, tallest
+ _bevel_width
* 2);
360 void Widget::adjustVert(void)
362 if (_children
.size() == 0)
366 WidgetList::iterator it
, end
= _children
.end();
369 int height
= _bevel_width
;
370 WidgetList stretchable
;
372 for (it
= _children
.begin(); it
!= end
; ++it
) {
374 if (tmp
->isStretchableHorz())
375 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
376 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
377 if (tmp
->isStretchableVert())
378 stretchable
.push_back(tmp
);
380 height
+= tmp
->_rect
.height() + _bevel_width
;
382 if (tmp
->_rect
.width() > widest
)
383 widest
= tmp
->_rect
.width();
386 if (stretchable
.size() > 0) {
387 WidgetList::iterator str_it
= stretchable
.begin(),
388 str_end
= stretchable
.end();
390 int str_height
= _rect
.height() - height
/ stretchable
.size();
392 for (; str_it
!= str_end
; ++str_it
)
393 (*str_it
)->setHeight(str_height
> _bevel_width
?
394 str_height
- _bevel_width
: _bevel_width
);
396 if (stretchable
.size() > 0)
397 height
= _rect
.height();
399 Widget
*prev_widget
= 0;
401 for (it
= _children
.begin(); it
!= end
; ++it
) {
406 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
409 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
416 internalResize(widest
+ _bevel_width
* 2, height
);
419 void Widget::update()
421 WidgetList::iterator it
= _children
.begin(), end
= _children
.end();
422 for (; it
!= end
; ++it
)
428 XClearWindow(**display
, _window
);
434 void Widget::internalResize(int w
, int h
)
436 assert(w
> 0 && h
> 0);
438 bool fw
= _fixed_width
, fh
= _fixed_height
;
443 resize(w
, _rect
.height());
445 resize(_rect
.width(), h
);
451 void Widget::addChild(Widget
*child
, bool front
)
455 _children
.push_front(child
);
457 _children
.push_back(child
);
460 void Widget::removeChild(Widget
*child
)
463 WidgetList::iterator it
, end
= _children
.end();
464 for (it
= _children
.begin(); it
!= end
; ++it
) {
469 if (it
!= _children
.end())
473 void Widget::setStyle(RenderStyle
*style
)
479 WidgetList::iterator it
, end
= _children
.end();
480 for (it
= _children
.begin(); it
!= end
; ++it
)
481 (*it
)->setStyle(style
);
485 void Widget::setEventDispatcher(EventDispatcher
*disp
)
487 if (_event_dispatcher
)
488 _event_dispatcher
->clearHandler(_window
);
489 _event_dispatcher
= disp
;
490 _event_dispatcher
->registerHandler(_window
, this);
493 void Widget::exposeHandler(const XExposeEvent
&e
)
495 EventHandler::exposeHandler(e
);
496 // XClearArea(**display, _window, e.x, e.y, e.width, e.height, false);
499 void Widget::configureHandler(const XConfigureEvent
&e
)
501 EventHandler::configureHandler(e
);
503 if (_ignore_config
) {
507 int height
= e
.height
;
510 while (XCheckTypedWindowEvent(**display
, _window
, ConfigureNotify
, &ev
)) {
511 width
= ev
.xconfigure
.width
;
512 height
= ev
.xconfigure
.height
;
515 if (!(width
== _rect
.width() && height
== _rect
.height())) {
517 _rect
.setSize(width
, height
);