4 #include "screeninfo.hh"
11 OtkWidget::OtkWidget(OtkWidget
*parent
, Direction direction
)
12 : _parent(parent
), _style(parent
->getStyle()), _direction(direction
),
13 _cursor(parent
->getCursor()), _bevel_width(parent
->getBevelWidth()),
14 _visible(false), _focused(false), _grabbed_mouse(false),
15 _grabbed_keyboard(false), _stretchable_vert(false),
16 _stretchable_horz(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
17 _screen(parent
->getScreen()), _fixed_width(false), _fixed_height(false),
20 parent
->addChild(this);
24 OtkWidget::OtkWidget(Style
*style
, Direction direction
,
25 Cursor cursor
, int bevel_width
)
26 : _parent(0), _style(style
), _direction(direction
), _cursor(cursor
),
27 _bevel_width(bevel_width
), _visible(false),
28 _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
29 _stretchable_vert(false), _stretchable_horz(false), _texture(0),
30 _bg_pixmap(0), _bg_pixel(0), _screen(style
->getScreen()),
31 _fixed_width(false), _fixed_height(false), _dirty(false)
37 OtkWidget::~OtkWidget()
42 std::for_each(_children
.begin(), _children
.end(), PointerAssassin());
45 _parent
->removeChild(this);
47 XDestroyWindow(otk::OBDisplay::display
, _window
);
50 void OtkWidget::create(void)
52 const ScreenInfo
*scr_info
= otk::OBDisplay::screenInfo(_screen
);
53 Window p_window
= _parent
? _parent
->getWindow() : scr_info
->getRootWindow();
55 _rect
.setRect(0, 0, 1, 1); // just some initial values
57 XSetWindowAttributes attrib_create
;
58 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
60 attrib_create
.background_pixmap
= None
;
61 attrib_create
.colormap
= scr_info
->getColormap();
62 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
63 ButtonMotionMask
| ExposureMask
| StructureNotifyMask
;
66 create_mask
|= CWCursor
;
67 attrib_create
.cursor
= _cursor
;
70 _window
= XCreateWindow(otk::OBDisplay::display
, p_window
, _rect
.x(),
71 _rect
.y(), _rect
.width(), _rect
.height(), 0,
72 scr_info
->getDepth(), InputOutput
,
73 scr_info
->getVisual(), create_mask
, &attrib_create
);
76 void OtkWidget::setWidth(int w
)
80 setGeometry(_rect
.x(), _rect
.y(), w
, _rect
.height());
83 void OtkWidget::setHeight(int h
)
87 setGeometry(_rect
.x(), _rect
.y(), _rect
.width(), h
);
90 void OtkWidget::move(const Point
&to
)
95 void OtkWidget::move(int x
, int y
)
98 XMoveWindow(otk::OBDisplay::display
, _window
, x
, y
);
101 void OtkWidget::resize(const Point
&to
)
103 resize(to
.x(), to
.y());
106 void OtkWidget::resize(int w
, int h
)
108 assert(w
> 0 && h
> 0);
109 _fixed_width
= _fixed_height
= true;
110 setGeometry(_rect
.x(), _rect
.y(), w
, h
);
113 void OtkWidget::setGeometry(const Rect
&new_geom
)
115 setGeometry(new_geom
.x(), new_geom
.y(), new_geom
.width(), new_geom
.height());
118 void OtkWidget::setGeometry(const Point
&topleft
, int width
, int height
)
120 setGeometry(topleft
.x(), topleft
.y(), width
, height
);
123 void OtkWidget::setGeometry(int x
, int y
, int width
, int height
)
125 _rect
= Rect(x
, y
, width
, height
);
128 XMoveResizeWindow(otk::OBDisplay::display
, _window
, x
, y
, width
, height
);
131 void OtkWidget::show(void)
136 // make sure the internal state isn't mangled
140 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
141 for (; it
!= end
; ++it
)
144 XMapWindow(otk::OBDisplay::display
, _window
);
148 void OtkWidget::hide(void)
153 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
154 for (; it
!= end
; ++it
)
157 XUnmapWindow(otk::OBDisplay::display
, _window
);
161 void OtkWidget::focus(void)
166 XSetInputFocus(otk::OBDisplay::display
, _window
, RevertToPointerRoot
,
170 bool OtkWidget::grabMouse(void)
172 Status ret
= XGrabPointer(otk::OBDisplay::display
, _window
, True
,
173 (ButtonPressMask
| ButtonReleaseMask
|
174 ButtonMotionMask
| EnterWindowMask
|
175 LeaveWindowMask
| PointerMotionMask
),
176 GrabModeSync
, GrabModeAsync
, None
, None
,
178 _grabbed_mouse
= (ret
== GrabSuccess
);
179 return _grabbed_mouse
;
182 void OtkWidget::ungrabMouse(void)
184 if (! _grabbed_mouse
)
187 XUngrabPointer(otk::OBDisplay::display
, CurrentTime
);
188 _grabbed_mouse
= false;
191 bool OtkWidget::grabKeyboard(void)
193 Status ret
= XGrabKeyboard(otk::OBDisplay::display
, _window
, True
,
194 GrabModeSync
, GrabModeAsync
, CurrentTime
);
195 _grabbed_keyboard
= (ret
== GrabSuccess
);
196 return _grabbed_keyboard
;
200 void OtkWidget::ungrabKeyboard(void)
202 if (! _grabbed_keyboard
)
205 XUngrabKeyboard(otk::OBDisplay::display
, CurrentTime
);
206 _grabbed_keyboard
= false;
209 void OtkWidget::render(void)
211 Pixmap old
= _bg_pixmap
;
213 _bg_pixmap
= _texture
->render(_rect
.width(), _rect
.height(), _bg_pixmap
);
215 if (_bg_pixmap
&& _bg_pixmap
!= old
)
216 XSetWindowBackgroundPixmap(otk::OBDisplay::display
, _window
, _bg_pixmap
);
218 unsigned int pix
= _texture
->color().pixel();
219 if (pix
!= _bg_pixel
) {
221 XSetWindowBackground(otk::OBDisplay::display
, _window
, pix
);
226 void OtkWidget::adjust(void)
228 if (_direction
== Horizontal
)
234 void OtkWidget::adjustHorz(void)
236 if (_children
.size() == 0)
240 OtkWidgetList::iterator it
, end
= _children
.end();
243 int width
= _bevel_width
;
244 OtkWidgetList stretchable
;
246 for (it
= _children
.begin(); it
!= end
; ++it
) {
248 if (tmp
->isStretchableHorz() && _fixed_width
)
249 stretchable
.push_back(tmp
);
251 width
+= tmp
->_rect
.width() + _bevel_width
;
253 if (tmp
->_rect
.height() > tallest
)
254 tallest
= tmp
->_rect
.height();
257 if (stretchable
.size() > 0) {
258 OtkWidgetList::iterator str_it
= stretchable
.begin(),
259 str_end
= stretchable
.end();
261 int str_width
= _rect
.width() - width
/ stretchable
.size();
263 for (; str_it
!= str_end
; ++str_it
) {
264 (*str_it
)->setWidth(str_width
- _bevel_width
);
265 //(*str_it)->update();
269 OtkWidget
*prev_widget
= 0;
271 for (it
= _children
.begin(); it
!= end
; ++it
) {
276 x
= prev_widget
->_rect
.x() + prev_widget
->_rect
.width() + _bevel_width
;
278 x
= _rect
.x() + _bevel_width
;
279 y
= (tallest
- tmp
->_rect
.height()) / 2 + _bevel_width
;
286 internalResize(width
, tallest
+ _bevel_width
* 2);
289 void OtkWidget::adjustVert(void)
291 if (_children
.size() == 0)
295 OtkWidgetList::iterator it
, end
= _children
.end();
298 int height
= _bevel_width
;
299 OtkWidgetList stretchable
;
301 for (it
= _children
.begin(); it
!= end
; ++it
) {
303 if (tmp
->isStretchableVert() && _fixed_height
)
304 stretchable
.push_back(tmp
);
306 height
+= tmp
->_rect
.height() + _bevel_width
;
308 if (tmp
->_rect
.width() > widest
)
309 widest
= tmp
->_rect
.width();
312 if (stretchable
.size() > 0) {
313 OtkWidgetList::iterator str_it
= stretchable
.begin(),
314 str_end
= stretchable
.end();
316 int str_height
= _rect
.height() - height
/ stretchable
.size();
318 for (; str_it
!= str_end
; ++str_it
) {
319 (*str_it
)->setHeight(str_height
- _bevel_width
);
320 //(*str_it)->update();
324 OtkWidget
*prev_widget
= 0;
326 for (it
= _children
.begin(); it
!= end
; ++it
) {
331 y
= prev_widget
->_rect
.y() + prev_widget
->_rect
.height() + _bevel_width
;
333 y
= _rect
.y() + _bevel_width
;
334 x
= (widest
- tmp
->_rect
.width()) / 2 + _bevel_width
;
341 internalResize(widest
+ _bevel_width
* 2, height
);
344 void OtkWidget::update(void)
346 OtkWidgetList::iterator it
= _children
.begin(), end
= _children
.end();
347 for (; it
!= end
; ++it
)
353 XClearWindow(OBDisplay::display
, _window
);
359 void OtkWidget::internalResize(int w
, int h
)
361 assert(w
> 0 && h
> 0);
363 if (! _fixed_width
&& ! _fixed_height
)
365 else if (! _fixed_width
)
366 resize(w
, _rect
.height());
367 else if (! _fixed_height
)
368 resize(_rect
.width(), h
);
371 void OtkWidget::addChild(OtkWidget
*child
, bool front
)
375 _children
.push_front(child
);
377 _children
.push_back(child
);
380 void OtkWidget::removeChild(OtkWidget
*child
)
383 OtkWidgetList::iterator it
, end
= _children
.end();
384 for (it
= _children
.begin(); it
!= end
; ++it
) {
389 if (it
!= _children
.end())