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 OtkWidget::OtkWidget(OtkWidget
*parent
, Direction direction
)
20 _parent(parent
), _style(parent
->getStyle()), _direction(direction
),
21 _cursor(parent
->getCursor()), _bevel_width(parent
->getBevelWidth()),
23 _visible(false), _focused(false), _grabbed_mouse(false),
24 _grabbed_keyboard(false), _stretchable_vert(false),
25 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
26 _screen(parent
->getScreen()), _fixed_width(false), _fixed_height(false),
27 _event_dispatcher(parent
->getEventDispatcher()), _application(0)
30 parent
->addChild(this);
32 _event_dispatcher
->registerHandler(_window
, this);
35 OtkWidget::OtkWidget(OtkEventDispatcher
*event_dispatcher
, Style
*style
,
36 Direction direction
, Cursor cursor
, int bevel_width
)
39 _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
40 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
41 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
42 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
43 _bg_pixmap(0), _bg_pixel(0), _screen(style
->getScreen()),
44 _fixed_width(false), _fixed_height(false),
45 _event_dispatcher(event_dispatcher
), _application(0)
47 assert(event_dispatcher
);
50 _event_dispatcher
->registerHandler(_window
, this);
53 OtkWidget::~OtkWidget()
58 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
61 _parent
->removeChild(this);
63 XDestroyWindow(otk::OBDisplay::display
, _window
);
66 void OtkWidget::create(void)
68 const ScreenInfo
*scr_info
= otk::OBDisplay::screenInfo(_screen
);
69 Window p_window
= _parent
? _parent
->getWindow() : scr_info
->getRootWindow();
71 _rect
.setRect(0, 0, 1, 1); // just some initial values
73 XSetWindowAttributes attrib_create
;
74 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
76 attrib_create
.background_pixmap
= None
;
77 attrib_create
.colormap
= scr_info
->getColormap();
78 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
79 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
82 create_mask
|= CWCursor
;
83 attrib_create
.cursor
= _cursor
;
86 _window
= XCreateWindow(otk::OBDisplay::display
, p_window
, _rect
.x(),
87 _rect
.y(), _rect
.width(), _rect
.height(), 0,
88 scr_info
->getDepth(), InputOutput
,
89 scr_info
->getVisual(), create_mask
, &attrib_create
);
93 void OtkWidget::setWidth(int w
)
97 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
100 void OtkWidget::setHeight(int h
)
103 _fixed_height
= true;
104 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
107 void OtkWidget::move(const Point
&to
)
109 move(to
.x(), to
.y());
112 void OtkWidget::move(int x
, int y
)
115 XMoveWindow(otk::OBDisplay::display
, _window
, x
, y
);
119 void OtkWidget::resize(const Point
&to
)
121 resize(to
.x(), to
.y());
124 void OtkWidget::resize(int w
, int h
)
126 assert(w
> 0 && h
> 0);
127 _fixed_width
= _fixed_height
= true;
128 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
131 void OtkWidget::setGeometry(const Rect
&new_geom
)
133 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
136 void OtkWidget::setGeometry(const Point
&topleft
, int width
, int height
)
138 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
141 void OtkWidget::setGeometry(int x
, int y
, int width
, int height
)
143 _rect
= Rect(x
, y
, width
, height
);
146 XMoveResizeWindow(otk::OBDisplay::display
, _window
, x
, y
, width
, height
);
150 void OtkWidget::show(bool recursive
)
155 // make sure the internal state isn't mangled
160 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
161 for (; it
!= end
; ++it
)
165 XMapWindow(otk::OBDisplay::display
, _window
);
169 void OtkWidget::hide(bool recursive
)
175 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
176 for (; it
!= end
; ++it
)
180 XUnmapWindow(otk::OBDisplay::display
, _window
);
184 void OtkWidget::focus(void)
189 XSetInputFocus(otk::OBDisplay::display
, _window
, RevertToPointerRoot
,
193 bool OtkWidget::grabMouse(void)
195 Status ret
= XGrabPointer(otk::OBDisplay::display
, _window
, True
,
196 (ButtonPressMask
| ButtonReleaseMask
|
197 ButtonMotionMask
| EnterWindowMask
|
198 LeaveWindowMask
| PointerMotionMask
),
199 GrabModeSync
, GrabModeAsync
, None
, None
,
201 _grabbed_mouse
= (ret
== GrabSuccess
);
202 return _grabbed_mouse
;
205 void OtkWidget::ungrabMouse(void)
207 if (! _grabbed_mouse
)
210 XUngrabPointer(otk::OBDisplay::display
, CurrentTime
);
211 _grabbed_mouse
= false;
214 bool OtkWidget::grabKeyboard(void)
216 Status ret
= XGrabKeyboard(otk::OBDisplay::display
, _window
, True
,
217 GrabModeSync
, GrabModeAsync
, CurrentTime
);
218 _grabbed_keyboard
= (ret
== GrabSuccess
);
219 return _grabbed_keyboard
;
223 void OtkWidget::ungrabKeyboard(void)
225 if (! _grabbed_keyboard
)
228 XUngrabKeyboard(otk::OBDisplay::display
, CurrentTime
);
229 _grabbed_keyboard
= false;
232 void OtkWidget::render(void)
234 if (!_texture
) return;
236 _bg_pixmap
= _texture
->render(_rect
.width(), _rect
.height(), _bg_pixmap
);
239 XSetWindowBackgroundPixmap(otk::OBDisplay::display
, _window
, _bg_pixmap
);
241 unsigned int pix
= _texture
->color().pixel();
242 if (pix
!= _bg_pixel
) {
244 XSetWindowBackground(otk::OBDisplay::display
, _window
, pix
);
249 void OtkWidget::adjust(void)
251 if (_direction
== Horizontal
)
257 void OtkWidget::adjustHorz(void)
259 if (_children
.size() == 0)
263 OtkWidgetList::iterator it
, end
= _children
.end();
266 int width
= _bevel_width
;
267 OtkWidgetList stretchable
;
269 for (it
= _children
.begin(); it
!= end
; ++it
) {
271 if (tmp
->isStretchableVert())
272 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
273 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
274 if (tmp
->isStretchableHorz())
275 stretchable
.push_back(tmp
);
277 width
+= tmp
->_rect
.width() + _bevel_width
;
279 if (tmp
->_rect
.height() > tallest
)
280 tallest
= tmp
->_rect
.height();
283 if (stretchable
.size() > 0) {
284 OtkWidgetList::iterator str_it
= stretchable
.begin(),
285 str_end
= stretchable
.end();
287 int str_width
= _rect
.width() - width
/ stretchable
.size();
289 for (; str_it
!= str_end
; ++str_it
)
290 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
294 OtkWidget
*prev_widget
= 0;
296 for (it
= _children
.begin(); it
!= end
; ++it
) {
301 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
303 x
= _rect
.x() + _bevel_width
;
304 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
311 internalResize(width
, tallest
+ _bevel_width
* 2);
314 void OtkWidget::adjustVert(void)
316 if (_children
.size() == 0)
320 OtkWidgetList::iterator it
, end
= _children
.end();
323 int height
= _bevel_width
;
324 OtkWidgetList stretchable
;
326 for (it
= _children
.begin(); it
!= end
; ++it
) {
328 if (tmp
->isStretchableHorz())
329 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
330 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
331 if (tmp
->isStretchableVert())
332 stretchable
.push_back(tmp
);
334 height
+= tmp
->_rect
.height() + _bevel_width
;
336 if (tmp
->_rect
.width() > widest
)
337 widest
= tmp
->_rect
.width();
340 if (stretchable
.size() > 0) {
341 OtkWidgetList::iterator str_it
= stretchable
.begin(),
342 str_end
= stretchable
.end();
344 int str_height
= _rect
.height() - height
/ stretchable
.size();
346 for (; str_it
!= str_end
; ++str_it
)
347 (*str_it
)->setHeight(str_height
> _bevel_width
?
348 str_height
- _bevel_width
: _bevel_width
);
351 OtkWidget
*prev_widget
= 0;
353 for (it
= _children
.begin(); it
!= end
; ++it
) {
358 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
360 y
= _rect
.y() + _bevel_width
;
361 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
368 internalResize(widest
+ _bevel_width
* 2, height
);
371 void OtkWidget::update(void)
377 XClearWindow(OBDisplay::display
, _window
);
380 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
381 for (; it
!= end
; ++it
)
387 void OtkWidget::internalResize(int w
, int h
)
389 assert(w
> 0 && h
> 0);
391 if (! _fixed_width
&& ! _fixed_height
)
393 else if (! _fixed_width
)
394 resize(w
, _rect
.height());
395 else if (! _fixed_height
)
396 resize(_rect
.width(), h
);
399 void OtkWidget::addChild(OtkWidget
*child
, bool front
)
403 _children
.push_front(child
);
405 _children
.push_back(child
);
408 void OtkWidget::removeChild(OtkWidget
*child
)
411 OtkWidgetList::iterator it
, end
= _children
.end();
412 for (it
= _children
.begin(); it
!= end
; ++it
) {
417 if (it
!= _children
.end())
421 void OtkWidget::setEventDispatcher(OtkEventDispatcher
*disp
)
423 if (_event_dispatcher
)
424 _event_dispatcher
->clearHandler(_window
);
425 _event_dispatcher
= disp
;
426 _event_dispatcher
->registerHandler(_window
, this);
429 void OtkWidget::exposeHandler(const XExposeEvent
&e
)
431 OtkEventHandler::exposeHandler(e
);
436 void OtkWidget::configureHandler(const XConfigureEvent
&e
)
438 OtkEventHandler::configureHandler(e
);
439 if (_ignore_config
) {
442 if (!(e
.width
== _rect
.width() && e
.height
== _rect
.height())) {
444 _rect
.setSize(e
.width
, e
.height
);