4 #include "screeninfo.hh"
11 OtkWidget::OtkWidget(OtkWidget
*parent
, Direction direction
)
13 _parent(parent
), _style(parent
->getStyle()), _direction(direction
),
14 _cursor(parent
->getCursor()), _bevel_width(parent
->getBevelWidth()),
16 _visible(false), _focused(false), _grabbed_mouse(false),
17 _grabbed_keyboard(false), _stretchable_vert(false),
18 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
19 _screen(parent
->getScreen()), _fixed_width(false), _fixed_height(false),
20 _dirty(false), _event_dispatcher(parent
->getEventDispatcher())
22 parent
->addChild(this);
24 _event_dispatcher
->registerHandler(_window
, this);
27 OtkWidget::OtkWidget(OtkApplication
*app
, Direction direction
,
28 Cursor cursor
, int bevel_width
)
30 _parent(0), _style(app
->getStyle()), _direction(direction
), _cursor(cursor
),
31 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
32 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
33 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
34 _bg_pixmap(0), _bg_pixel(0), _screen(app
->getStyle()->getScreen()),
35 _fixed_width(false), _fixed_height(false), _dirty(false),
36 _event_dispatcher(app
)
40 _event_dispatcher
->registerHandler(_window
, this);
43 OtkWidget::OtkWidget(Style
*style
, Direction direction
,
44 Cursor cursor
, int bevel_width
)
46 _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
47 _bevel_width(bevel_width
), _ignore_config(0), _visible(false),
48 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
49 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
50 _bg_pixmap(0), _bg_pixel(0), _screen(style
->getScreen()),
51 _fixed_width(false), _fixed_height(false), _dirty(false)
57 OtkWidget::~OtkWidget()
62 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
65 _parent
->removeChild(this);
67 XDestroyWindow(otk::OBDisplay::display
, _window
);
70 void OtkWidget::create(void)
72 const ScreenInfo
*scr_info
= otk::OBDisplay::screenInfo(_screen
);
73 Window p_window
= _parent
? _parent
->getWindow() : scr_info
->getRootWindow();
75 _rect
.setRect(0, 0, 1, 1); // just some initial values
77 XSetWindowAttributes attrib_create
;
78 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
80 attrib_create
.background_pixmap
= None
;
81 attrib_create
.colormap
= scr_info
->getColormap();
82 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
83 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
86 create_mask
|= CWCursor
;
87 attrib_create
.cursor
= _cursor
;
90 _window
= XCreateWindow(otk::OBDisplay::display
, p_window
, _rect
.x(),
91 _rect
.y(), _rect
.width(), _rect
.height(), 0,
92 scr_info
->getDepth(), InputOutput
,
93 scr_info
->getVisual(), create_mask
, &attrib_create
);
97 void OtkWidget::setWidth(int w
)
101 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
104 void OtkWidget::setHeight(int h
)
107 _fixed_height
= true;
108 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
111 void OtkWidget::move(const Point
&to
)
113 move(to
.x(), to
.y());
116 void OtkWidget::move(int x
, int y
)
119 XMoveWindow(otk::OBDisplay::display
, _window
, x
, y
);
123 void OtkWidget::resize(const Point
&to
)
125 resize(to
.x(), to
.y());
128 void OtkWidget::resize(int w
, int h
)
130 assert(w
> 0 && h
> 0);
131 _fixed_width
= _fixed_height
= true;
132 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
135 void OtkWidget::setGeometry(const Rect
&new_geom
)
137 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
140 void OtkWidget::setGeometry(const Point
&topleft
, int width
, int height
)
142 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
145 void OtkWidget::setGeometry(int x
, int y
, int width
, int height
)
147 _rect
= Rect(x
, y
, width
, height
);
150 XMoveResizeWindow(otk::OBDisplay::display
, _window
, x
, y
, width
, height
);
154 void OtkWidget::show(void)
159 // make sure the internal state isn't mangled
163 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
164 for (; it
!= end
; ++it
)
167 XMapWindow(otk::OBDisplay::display
, _window
);
171 void OtkWidget::hide(void)
176 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
177 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 _bg_pixmap
= _texture
->render(_rect
.width(), _rect
.height(), _bg_pixmap
);
237 XSetWindowBackgroundPixmap(otk::OBDisplay::display
, _window
, _bg_pixmap
);
239 unsigned int pix
= _texture
->color().pixel();
240 if (pix
!= _bg_pixel
) {
242 XSetWindowBackground(otk::OBDisplay::display
, _window
, pix
);
247 void OtkWidget::adjust(void)
249 if (_direction
== Horizontal
)
255 void OtkWidget::adjustHorz(void)
257 if (_children
.size() == 0)
261 OtkWidgetList::iterator it
, end
= _children
.end();
264 int width
= _bevel_width
;
265 OtkWidgetList stretchable
;
267 for (it
= _children
.begin(); it
!= end
; ++it
) {
269 if (tmp
->isStretchableVert())
270 tmp
->setHeight(_rect
.height() > _bevel_width
* 2 ?
271 _rect
.height() - _bevel_width
* 2 : _bevel_width
);
272 if (tmp
->isStretchableHorz())
273 stretchable
.push_back(tmp
);
275 width
+= tmp
->_rect
.width() + _bevel_width
;
277 if (tmp
->_rect
.height() > tallest
)
278 tallest
= tmp
->_rect
.height();
281 if (stretchable
.size() > 0) {
282 OtkWidgetList::iterator str_it
= stretchable
.begin(),
283 str_end
= stretchable
.end();
285 int str_width
= _rect
.width() - width
/ stretchable
.size();
287 for (; str_it
!= str_end
; ++str_it
)
288 (*str_it
)->setWidth(str_width
> _bevel_width
? str_width
- _bevel_width
292 OtkWidget
*prev_widget
= 0;
294 for (it
= _children
.begin(); it
!= end
; ++it
) {
299 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
301 x
= _rect
.x() + _bevel_width
;
302 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
309 internalResize(width
, tallest
+ _bevel_width
* 2);
312 void OtkWidget::adjustVert(void)
314 if (_children
.size() == 0)
318 OtkWidgetList::iterator it
, end
= _children
.end();
321 int height
= _bevel_width
;
322 OtkWidgetList stretchable
;
324 for (it
= _children
.begin(); it
!= end
; ++it
) {
326 if (tmp
->isStretchableHorz())
327 tmp
->setWidth(_rect
.width() > _bevel_width
* 2 ?
328 _rect
.width() - _bevel_width
* 2 : _bevel_width
);
329 if (tmp
->isStretchableVert())
330 stretchable
.push_back(tmp
);
332 height
+= tmp
->_rect
.height() + _bevel_width
;
334 if (tmp
->_rect
.width() > widest
)
335 widest
= tmp
->_rect
.width();
338 if (stretchable
.size() > 0) {
339 OtkWidgetList::iterator str_it
= stretchable
.begin(),
340 str_end
= stretchable
.end();
342 int str_height
= _rect
.height() - height
/ stretchable
.size();
344 for (; str_it
!= str_end
; ++str_it
)
345 (*str_it
)->setHeight(str_height
> _bevel_width
?
346 str_height
- _bevel_width
: _bevel_width
);
349 OtkWidget
*prev_widget
= 0;
351 for (it
= _children
.begin(); it
!= end
; ++it
) {
356 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
358 y
= _rect
.y() + _bevel_width
;
359 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
366 internalResize(widest
+ _bevel_width
* 2, height
);
369 void OtkWidget::update(void)
374 XClearWindow(OBDisplay::display
, _window
);
377 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
378 for (; it
!= end
; ++it
)
384 void OtkWidget::internalResize(int w
, int h
)
386 assert(w
> 0 && h
> 0);
388 if (! _fixed_width
&& ! _fixed_height
)
390 else if (! _fixed_width
)
391 resize(w
, _rect
.height());
392 else if (! _fixed_height
)
393 resize(_rect
.width(), h
);
396 void OtkWidget::addChild(OtkWidget
*child
, bool front
)
400 _children
.push_front(child
);
402 _children
.push_back(child
);
405 void OtkWidget::removeChild(OtkWidget
*child
)
408 OtkWidgetList::iterator it
, end
= _children
.end();
409 for (it
= _children
.begin(); it
!= end
; ++it
) {
414 if (it
!= _children
.end())
418 void OtkWidget::setEventDispatcher(OtkEventDispatcher
*disp
)
420 if (_event_dispatcher
)
421 _event_dispatcher
->clearHandler(_window
);
422 _event_dispatcher
= disp
;
423 _event_dispatcher
->registerHandler(_window
, this);
426 int OtkWidget::exposeHandler(const XExposeEvent
&e
)
428 OtkEventHandler::exposeHandler(e
);
429 if (e
.window
== _window
) {
437 int OtkWidget::configureHandler(const XConfigureEvent
&e
)
439 OtkEventHandler::configureHandler(e
);
440 if (e
.window
== _window
) {
441 if (_ignore_config
) {
444 if (!(e
.width
== _rect
.width() && e
.height
== _rect
.height())) {
446 _rect
.setSize(e
.width
, e
.height
);