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(void)
155 // make sure the internal state isn't mangled
159 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
160 for (; it
!= end
; ++it
)
163 XMapWindow(otk::OBDisplay::display
, _window
);
167 void OtkWidget::hide(void)
172 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
173 for (; it
!= end
; ++it
)
176 XUnmapWindow(otk::OBDisplay::display
, _window
);
180 void OtkWidget::focus(void)
185 XSetInputFocus(otk::OBDisplay::display
, _window
, RevertToPointerRoot
,
189 bool OtkWidget::grabMouse(void)
191 Status ret
= XGrabPointer(otk::OBDisplay::display
, _window
, True
,
192 (ButtonPressMask
| ButtonReleaseMask
|
193 ButtonMotionMask
| EnterWindowMask
|
194 LeaveWindowMask
| PointerMotionMask
),
195 GrabModeSync
, GrabModeAsync
, None
, None
,
197 _grabbed_mouse
= (ret
== GrabSuccess
);
198 return _grabbed_mouse
;
201 void OtkWidget::ungrabMouse(void)
203 if (! _grabbed_mouse
)
206 XUngrabPointer(otk::OBDisplay::display
, CurrentTime
);
207 _grabbed_mouse
= false;
210 bool OtkWidget::grabKeyboard(void)
212 Status ret
= XGrabKeyboard(otk::OBDisplay::display
, _window
, True
,
213 GrabModeSync
, GrabModeAsync
, CurrentTime
);
214 _grabbed_keyboard
= (ret
== GrabSuccess
);
215 return _grabbed_keyboard
;
219 void OtkWidget::ungrabKeyboard(void)
221 if (! _grabbed_keyboard
)
224 XUngrabKeyboard(otk::OBDisplay::display
, CurrentTime
);
225 _grabbed_keyboard
= false;
228 void OtkWidget::render(void)
230 _bg_pixmap
= _texture
->render(_rect
.width(), _rect
.height(), _bg_pixmap
);
233 XSetWindowBackgroundPixmap(otk::OBDisplay::display
, _window
, _bg_pixmap
);
235 unsigned int pix
= _texture
->color().pixel();
236 if (pix
!= _bg_pixel
) {
238 XSetWindowBackground(otk::OBDisplay::display
, _window
, pix
);
243 void OtkWidget::adjust(void)
245 if (_direction
== Horizontal
)
251 void OtkWidget::adjustHorz(void)
253 if (_children
.size() == 0)
257 OtkWidgetList::iterator it
, end
= _children
.end();
260 int width
= _bevel_width
;
261 OtkWidgetList stretchable
;
263 for (it
= _children
.begin(); it
!= end
; ++it
) {
265 if (tmp
->isStretchableVert())
266 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
267 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
268 if (tmp
->isStretchableHorz())
269 stretchable
.push_back(tmp
);
271 width
+= tmp
->_rect
.width() + _bevel_width
;
273 if (tmp
->_rect
.height() > tallest
)
274 tallest
= tmp
->_rect
.height();
277 if (stretchable
.size() > 0) {
278 OtkWidgetList::iterator str_it
= stretchable
.begin(),
279 str_end
= stretchable
.end();
281 int str_width
= _rect
.width() - width
/ stretchable
.size();
283 for (; str_it
!= str_end
; ++str_it
)
284 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
288 OtkWidget
*prev_widget
= 0;
290 for (it
= _children
.begin(); it
!= end
; ++it
) {
295 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
297 x
= _rect
.x() + _bevel_width
;
298 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
305 internalResize(width
, tallest
+ _bevel_width
* 2);
308 void OtkWidget::adjustVert(void)
310 if (_children
.size() == 0)
314 OtkWidgetList::iterator it
, end
= _children
.end();
317 int height
= _bevel_width
;
318 OtkWidgetList stretchable
;
320 for (it
= _children
.begin(); it
!= end
; ++it
) {
322 if (tmp
->isStretchableHorz())
323 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
324 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
325 if (tmp
->isStretchableVert())
326 stretchable
.push_back(tmp
);
328 height
+= tmp
->_rect
.height() + _bevel_width
;
330 if (tmp
->_rect
.width() > widest
)
331 widest
= tmp
->_rect
.width();
334 if (stretchable
.size() > 0) {
335 OtkWidgetList::iterator str_it
= stretchable
.begin(),
336 str_end
= stretchable
.end();
338 int str_height
= _rect
.height() - height
/ stretchable
.size();
340 for (; str_it
!= str_end
; ++str_it
)
341 (*str_it
)->setHeight(str_height
> _bevel_width
?
342 str_height
- _bevel_width
: _bevel_width
);
345 OtkWidget
*prev_widget
= 0;
347 for (it
= _children
.begin(); it
!= end
; ++it
) {
352 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
354 y
= _rect
.y() + _bevel_width
;
355 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
362 internalResize(widest
+ _bevel_width
* 2, height
);
365 void OtkWidget::update(void)
370 XClearWindow(OBDisplay::display
, _window
);
373 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
374 for (; it
!= end
; ++it
)
380 void OtkWidget::internalResize(int w
, int h
)
382 assert(w
> 0 && h
> 0);
384 if (! _fixed_width
&& ! _fixed_height
)
386 else if (! _fixed_width
)
387 resize(w
, _rect
.height());
388 else if (! _fixed_height
)
389 resize(_rect
.width(), h
);
392 void OtkWidget::addChild(OtkWidget
*child
, bool front
)
396 _children
.push_front(child
);
398 _children
.push_back(child
);
401 void OtkWidget::removeChild(OtkWidget
*child
)
404 OtkWidgetList::iterator it
, end
= _children
.end();
405 for (it
= _children
.begin(); it
!= end
; ++it
) {
410 if (it
!= _children
.end())
414 void OtkWidget::setEventDispatcher(OtkEventDispatcher
*disp
)
416 if (_event_dispatcher
)
417 _event_dispatcher
->clearHandler(_window
);
418 _event_dispatcher
= disp
;
419 _event_dispatcher
->registerHandler(_window
, this);
422 void OtkWidget::exposeHandler(const XExposeEvent
&e
)
424 OtkEventHandler::exposeHandler(e
);
429 void OtkWidget::configureHandler(const XConfigureEvent
&e
)
431 OtkEventHandler::configureHandler(e
);
432 if (_ignore_config
) {
435 if (!(e
.width
== _rect
.width() && e
.height
== _rect
.height())) {
437 _rect
.setSize(e
.width
, e
.height
);