4 #include "screeninfo.hh"
11 OtkWidget::OtkWidget(OtkWidget
*parent
, Direction direction
)
14 _parent(parent
), _style(parent
->getStyle()), _direction(direction
),
15 _cursor(parent
->getCursor()), _bevel_width(parent
->getBevelWidth()),
17 _visible(false), _focused(false), _grabbed_mouse(false),
18 _grabbed_keyboard(false), _stretchable_vert(false),
19 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
20 _screen(parent
->getScreen()), _fixed_width(false), _fixed_height(false),
21 _event_dispatcher(parent
->getEventDispatcher())
23 parent
->addChild(this);
25 _event_dispatcher
->registerHandler(_window
, this);
28 OtkWidget::OtkWidget(OtkApplication
*app
, Direction direction
,
29 Cursor cursor
, int bevel_width
)
32 _parent(0), _style(app
->getStyle()), _direction(direction
), _cursor(cursor
),
33 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
34 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
35 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
36 _bg_pixmap(0), _bg_pixel(0), _screen(app
->getStyle()->getScreen()),
37 _fixed_width(false), _fixed_height(false),
38 _event_dispatcher(app
)
42 _event_dispatcher
->registerHandler(_window
, this);
45 OtkWidget::OtkWidget(Style
*style
, Direction direction
,
46 Cursor cursor
, int bevel_width
)
49 _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
50 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
51 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
52 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
53 _bg_pixmap(0), _bg_pixel(0), _screen(style
->getScreen()),
54 _fixed_width(false), _fixed_height(false)
60 OtkWidget::~OtkWidget()
65 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
68 _parent
->removeChild(this);
70 XDestroyWindow(otk::OBDisplay::display
, _window
);
73 void OtkWidget::create(void)
75 const ScreenInfo
*scr_info
= otk::OBDisplay::screenInfo(_screen
);
76 Window p_window
= _parent
? _parent
->getWindow() : scr_info
->getRootWindow();
78 _rect
.setRect(0, 0, 1, 1); // just some initial values
80 XSetWindowAttributes attrib_create
;
81 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
83 attrib_create
.background_pixmap
= None
;
84 attrib_create
.colormap
= scr_info
->getColormap();
85 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
86 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
89 create_mask
|= CWCursor
;
90 attrib_create
.cursor
= _cursor
;
93 _window
= XCreateWindow(otk::OBDisplay::display
, p_window
, _rect
.x(),
94 _rect
.y(), _rect
.width(), _rect
.height(), 0,
95 scr_info
->getDepth(), InputOutput
,
96 scr_info
->getVisual(), create_mask
, &attrib_create
);
100 void OtkWidget::setWidth(int w
)
104 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
107 void OtkWidget::setHeight(int h
)
110 _fixed_height
= true;
111 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
114 void OtkWidget::move(const Point
&to
)
116 move(to
.x(), to
.y());
119 void OtkWidget::move(int x
, int y
)
122 XMoveWindow(otk::OBDisplay::display
, _window
, x
, y
);
126 void OtkWidget::resize(const Point
&to
)
128 resize(to
.x(), to
.y());
131 void OtkWidget::resize(int w
, int h
)
133 assert(w
> 0 && h
> 0);
134 _fixed_width
= _fixed_height
= true;
135 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
138 void OtkWidget::setGeometry(const Rect
&new_geom
)
140 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
143 void OtkWidget::setGeometry(const Point
&topleft
, int width
, int height
)
145 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
148 void OtkWidget::setGeometry(int x
, int y
, int width
, int height
)
150 _rect
= Rect(x
, y
, width
, height
);
153 XMoveResizeWindow(otk::OBDisplay::display
, _window
, x
, y
, width
, height
);
157 void OtkWidget::show(void)
162 // make sure the internal state isn't mangled
166 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
167 for (; it
!= end
; ++it
)
170 XMapWindow(otk::OBDisplay::display
, _window
);
174 void OtkWidget::hide(void)
179 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
180 for (; it
!= end
; ++it
)
183 XUnmapWindow(otk::OBDisplay::display
, _window
);
187 void OtkWidget::focus(void)
192 XSetInputFocus(otk::OBDisplay::display
, _window
, RevertToPointerRoot
,
196 bool OtkWidget::grabMouse(void)
198 Status ret
= XGrabPointer(otk::OBDisplay::display
, _window
, True
,
199 (ButtonPressMask
| ButtonReleaseMask
|
200 ButtonMotionMask
| EnterWindowMask
|
201 LeaveWindowMask
| PointerMotionMask
),
202 GrabModeSync
, GrabModeAsync
, None
, None
,
204 _grabbed_mouse
= (ret
== GrabSuccess
);
205 return _grabbed_mouse
;
208 void OtkWidget::ungrabMouse(void)
210 if (! _grabbed_mouse
)
213 XUngrabPointer(otk::OBDisplay::display
, CurrentTime
);
214 _grabbed_mouse
= false;
217 bool OtkWidget::grabKeyboard(void)
219 Status ret
= XGrabKeyboard(otk::OBDisplay::display
, _window
, True
,
220 GrabModeSync
, GrabModeAsync
, CurrentTime
);
221 _grabbed_keyboard
= (ret
== GrabSuccess
);
222 return _grabbed_keyboard
;
226 void OtkWidget::ungrabKeyboard(void)
228 if (! _grabbed_keyboard
)
231 XUngrabKeyboard(otk::OBDisplay::display
, CurrentTime
);
232 _grabbed_keyboard
= false;
235 void OtkWidget::render(void)
237 _bg_pixmap
= _texture
->render(_rect
.width(), _rect
.height(), _bg_pixmap
);
240 XSetWindowBackgroundPixmap(otk::OBDisplay::display
, _window
, _bg_pixmap
);
242 unsigned int pix
= _texture
->color().pixel();
243 if (pix
!= _bg_pixel
) {
245 XSetWindowBackground(otk::OBDisplay::display
, _window
, pix
);
250 void OtkWidget::adjust(void)
252 if (_direction
== Horizontal
)
258 void OtkWidget::adjustHorz(void)
260 if (_children
.size() == 0)
264 OtkWidgetList::iterator it
, end
= _children
.end();
267 int width
= _bevel_width
;
268 OtkWidgetList stretchable
;
270 for (it
= _children
.begin(); it
!= end
; ++it
) {
272 if (tmp
->isStretchableVert())
273 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
274 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
275 if (tmp
->isStretchableHorz())
276 stretchable
.push_back(tmp
);
278 width
+= tmp
->_rect
.width() + _bevel_width
;
280 if (tmp
->_rect
.height() > tallest
)
281 tallest
= tmp
->_rect
.height();
284 if (stretchable
.size() > 0) {
285 OtkWidgetList::iterator str_it
= stretchable
.begin(),
286 str_end
= stretchable
.end();
288 int str_width
= _rect
.width() - width
/ stretchable
.size();
290 for (; str_it
!= str_end
; ++str_it
)
291 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
295 OtkWidget
*prev_widget
= 0;
297 for (it
= _children
.begin(); it
!= end
; ++it
) {
302 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
304 x
= _rect
.x() + _bevel_width
;
305 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
312 internalResize(width
, tallest
+ _bevel_width
* 2);
315 void OtkWidget::adjustVert(void)
317 if (_children
.size() == 0)
321 OtkWidgetList::iterator it
, end
= _children
.end();
324 int height
= _bevel_width
;
325 OtkWidgetList stretchable
;
327 for (it
= _children
.begin(); it
!= end
; ++it
) {
329 if (tmp
->isStretchableHorz())
330 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
331 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
332 if (tmp
->isStretchableVert())
333 stretchable
.push_back(tmp
);
335 height
+= tmp
->_rect
.height() + _bevel_width
;
337 if (tmp
->_rect
.width() > widest
)
338 widest
= tmp
->_rect
.width();
341 if (stretchable
.size() > 0) {
342 OtkWidgetList::iterator str_it
= stretchable
.begin(),
343 str_end
= stretchable
.end();
345 int str_height
= _rect
.height() - height
/ stretchable
.size();
347 for (; str_it
!= str_end
; ++str_it
)
348 (*str_it
)->setHeight(str_height
> _bevel_width
?
349 str_height
- _bevel_width
: _bevel_width
);
352 OtkWidget
*prev_widget
= 0;
354 for (it
= _children
.begin(); it
!= end
; ++it
) {
359 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
361 y
= _rect
.y() + _bevel_width
;
362 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
369 internalResize(widest
+ _bevel_width
* 2, height
);
372 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 int OtkWidget::exposeHandler(const XExposeEvent
&e
)
431 OtkEventHandler::exposeHandler(e
);
437 int OtkWidget::configureHandler(const XConfigureEvent
&e
)
439 OtkEventHandler::configureHandler(e
);
440 if (_ignore_config
) {
443 if (!(e
.width
== _rect
.width() && e
.height
== _rect
.height())) {
445 _rect
.setSize(e
.width
, e
.height
);