]> icculus.org git repositories - dana/openbox.git/blob - otk/widget.cc
change the style-load-error msg
[dana/openbox.git] / otk / widget.cc
1 #include "widget.hh"
2 #include "display.hh"
3 #include "assassin.hh"
4 #include "screeninfo.hh"
5
6 #include <algorithm>
7 #include <iostream>
8
9 namespace otk {
10
11 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
12   : OtkEventHandler(),
13     _dirty(false),
14     _parent(parent), _style(parent->getStyle()), _direction(direction),
15     _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
16     _ignore_config(0),
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()), _application(0)
22 {
23   assert(parent);
24   parent->addChild(this);
25   create();
26   _event_dispatcher->registerHandler(_window, this);
27 }
28
29 OtkWidget::OtkWidget(OtkEventDispatcher *event_dispatcher, Style *style,
30                      Direction direction, Cursor cursor, int bevel_width)
31   : OtkEventHandler(),
32     _dirty(false),
33     _parent(0), _style(style), _direction(direction), _cursor(cursor),
34     _bevel_width(bevel_width), _ignore_config(0), _visible(false),
35     _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
36     _stretchable_vert(false), _stretchable_horz(false), _texture(0),
37     _bg_pixmap(0), _bg_pixel(0), _screen(style->getScreen()),
38     _fixed_width(false), _fixed_height(false),
39     _event_dispatcher(event_dispatcher), _application(0)
40 {
41   assert(event_dispatcher);
42   assert(style);
43   create();
44   _event_dispatcher->registerHandler(_window, this);
45 }
46
47 OtkWidget::~OtkWidget()
48 {
49   if (_visible)
50     hide();
51
52   std::for_each(_children.begin(), _children.end(), PointerAssassin());
53
54   if (_parent)
55     _parent->removeChild(this);
56
57   XDestroyWindow(otk::OBDisplay::display, _window);
58 }
59
60 void OtkWidget::create(void)
61 {
62   const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
63   Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
64
65   _rect.setRect(0, 0, 1, 1); // just some initial values
66
67   XSetWindowAttributes attrib_create;
68   unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
69
70   attrib_create.background_pixmap = None;
71   attrib_create.colormap = scr_info->getColormap();
72   attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
73     ButtonMotionMask | ExposureMask | StructureNotifyMask;
74
75   if (_cursor) {
76     create_mask |= CWCursor;
77     attrib_create.cursor = _cursor;
78   }
79
80   _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
81                           _rect.y(), _rect.width(), _rect.height(), 0,
82                           scr_info->getDepth(), InputOutput,
83                           scr_info->getVisual(), create_mask, &attrib_create);
84   _ignore_config++;
85 }
86
87 void OtkWidget::setWidth(int w)
88 {
89   assert(w > 0);
90   _fixed_width = true;  
91   setGeometry(_rect.x(), _rect.y(), w, _rect.height());
92 }
93
94 void OtkWidget::setHeight(int h)
95 {
96   assert(h > 0);
97   _fixed_height = true;
98   setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
99 }
100
101 void OtkWidget::move(const Point &to)
102 {
103   move(to.x(), to.y());
104 }
105
106 void OtkWidget::move(int x, int y)
107 {
108   _rect.setPos(x, y);
109   XMoveWindow(otk::OBDisplay::display, _window, x, y);
110   _ignore_config++;
111 }
112
113 void OtkWidget::resize(const Point &to)
114 {
115   resize(to.x(), to.y());
116 }
117
118 void OtkWidget::resize(int w, int h)
119 {
120   assert(w > 0 && h > 0);
121   _fixed_width = _fixed_height = true;
122   setGeometry(_rect.x(), _rect.y(), w, h);
123 }
124
125 void OtkWidget::setGeometry(const Rect &new_geom)
126 {
127   setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
128 }
129  
130 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
131 {
132   setGeometry(topleft.x(), topleft.y(), width, height);
133 }
134
135 void OtkWidget::setGeometry(int x, int y, int width, int height)
136 {
137   _rect = Rect(x, y, width, height);
138   _dirty = true;
139
140   XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
141   _ignore_config++;
142 }
143
144 void OtkWidget::show(void)
145 {
146   if (_visible)
147     return;
148
149   // make sure the internal state isn't mangled
150   if (_dirty)
151     update();
152
153   OtkWidgetList::iterator it = _children.begin(), end = _children.end();
154   for (; it != end; ++it)
155     (*it)->show();
156
157   XMapWindow(otk::OBDisplay::display, _window);
158   _visible = true;
159 }
160
161 void OtkWidget::hide(void)
162 {
163   if (! _visible)
164     return;
165
166   OtkWidgetList::iterator it = _children.begin(), end = _children.end();
167   for (; it != end; ++it)
168     (*it)->hide();
169
170   XUnmapWindow(otk::OBDisplay::display, _window);
171   _visible = false;
172 }
173
174 void OtkWidget::focus(void)
175 {
176   if (! _visible)
177     return;
178
179   XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
180                  CurrentTime);
181 }
182
183 bool OtkWidget::grabMouse(void)
184 {
185   Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
186                             (ButtonPressMask | ButtonReleaseMask |
187                              ButtonMotionMask | EnterWindowMask |
188                              LeaveWindowMask | PointerMotionMask),
189                             GrabModeSync, GrabModeAsync, None, None,
190                             CurrentTime);
191   _grabbed_mouse = (ret == GrabSuccess);
192   return _grabbed_mouse;
193 }
194
195 void OtkWidget::ungrabMouse(void)
196 {
197   if (! _grabbed_mouse)
198     return;
199
200   XUngrabPointer(otk::OBDisplay::display, CurrentTime);
201   _grabbed_mouse = false;
202 }
203
204 bool OtkWidget::grabKeyboard(void)
205 {
206   Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
207                              GrabModeSync, GrabModeAsync, CurrentTime);
208   _grabbed_keyboard = (ret == GrabSuccess);
209   return _grabbed_keyboard;
210
211 }
212
213 void OtkWidget::ungrabKeyboard(void)
214 {
215   if (! _grabbed_keyboard)
216     return;
217
218   XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
219   _grabbed_keyboard = false;
220 }
221
222 void OtkWidget::render(void)
223 {
224   _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
225
226   if (_bg_pixmap)
227     XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
228   else {
229     unsigned int pix = _texture->color().pixel();
230     if (pix != _bg_pixel) {
231       _bg_pixel = pix;
232       XSetWindowBackground(otk::OBDisplay::display, _window, pix);
233     }
234   }
235 }
236
237 void OtkWidget::adjust(void)
238 {
239   if (_direction == Horizontal)
240     adjustHorz();
241   else
242     adjustVert();
243 }
244
245 void OtkWidget::adjustHorz(void)
246 {
247   if (_children.size() == 0)
248     return;
249
250   OtkWidget *tmp;
251   OtkWidgetList::iterator it, end = _children.end();
252
253   int tallest = 0;
254   int width = _bevel_width;
255   OtkWidgetList stretchable;
256
257   for (it = _children.begin(); it != end; ++it) {
258     tmp = *it;
259     if (tmp->isStretchableVert())
260       tmp->setHeight(_rect.height() > _bevel_width * 2 ?
261                      _rect.height() - _bevel_width * 2 : _bevel_width);
262     if (tmp->isStretchableHorz())
263       stretchable.push_back(tmp);
264     else
265       width += tmp->_rect.width() + _bevel_width;
266
267     if (tmp->_rect.height() > tallest)
268       tallest = tmp->_rect.height();
269   }
270
271   if (stretchable.size() > 0) {
272     OtkWidgetList::iterator str_it = stretchable.begin(),
273       str_end = stretchable.end();
274
275     int str_width = _rect.width() - width / stretchable.size();
276
277     for (; str_it != str_end; ++str_it)
278       (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
279                           : _bevel_width);
280   }
281
282   OtkWidget *prev_widget = 0;
283
284   for (it = _children.begin(); it != end; ++it) {
285     tmp = *it;
286     int x, y;
287
288     if (prev_widget)
289       x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
290     else
291       x = _rect.x() + _bevel_width;
292     y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
293
294     tmp->move(x, y);
295
296     prev_widget = tmp;
297   }
298
299   internalResize(width, tallest + _bevel_width * 2);
300 }
301
302 void OtkWidget::adjustVert(void)
303 {
304   if (_children.size() == 0)
305     return;
306
307   OtkWidget *tmp;
308   OtkWidgetList::iterator it, end = _children.end();
309
310   int widest = 0;
311   int height = _bevel_width;
312   OtkWidgetList stretchable;
313
314   for (it = _children.begin(); it != end; ++it) {
315     tmp = *it;
316     if (tmp->isStretchableHorz())
317       tmp->setWidth(_rect.width() > _bevel_width * 2 ?
318                     _rect.width() - _bevel_width * 2 : _bevel_width);
319     if (tmp->isStretchableVert())
320       stretchable.push_back(tmp);
321     else
322       height += tmp->_rect.height() + _bevel_width;
323
324     if (tmp->_rect.width() > widest)
325       widest = tmp->_rect.width();
326   }
327
328   if (stretchable.size() > 0) {
329     OtkWidgetList::iterator str_it = stretchable.begin(),
330       str_end = stretchable.end();
331
332     int str_height = _rect.height() - height / stretchable.size();
333
334     for (; str_it != str_end; ++str_it)
335       (*str_it)->setHeight(str_height > _bevel_width ?
336                            str_height - _bevel_width : _bevel_width);
337   }
338
339   OtkWidget *prev_widget = 0;
340
341   for (it = _children.begin(); it != end; ++it) {
342     tmp = *it;
343     int x, y;
344
345     if (prev_widget)
346       y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
347     else
348       y = _rect.y() + _bevel_width;
349     x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
350
351     tmp->move(x, y);
352
353     prev_widget = tmp;
354   }
355
356   internalResize(widest + _bevel_width * 2, height);
357 }
358
359 void OtkWidget::update(void)
360 {
361   if (_dirty) {
362     adjust();
363     render();
364     XClearWindow(OBDisplay::display, _window);
365   }
366
367   OtkWidgetList::iterator it = _children.begin(), end = _children.end();
368   for (; it != end; ++it)
369     (*it)->update();
370
371   _dirty = false;
372 }
373
374 void OtkWidget::internalResize(int w, int h)
375 {
376   assert(w > 0 && h > 0);
377
378   if (! _fixed_width && ! _fixed_height)
379     resize(w, h);
380   else if (! _fixed_width)
381     resize(w, _rect.height());
382   else if (! _fixed_height)
383     resize(_rect.width(), h);
384 }
385
386 void OtkWidget::addChild(OtkWidget *child, bool front)
387 {
388   assert(child);
389   if (front)
390     _children.push_front(child);
391   else
392     _children.push_back(child);
393 }
394
395 void OtkWidget::removeChild(OtkWidget *child)
396 {
397   assert(child);
398   OtkWidgetList::iterator it, end = _children.end();
399   for (it = _children.begin(); it != end; ++it) {
400     if ((*it) == child)
401       break;
402   }
403
404   if (it != _children.end())
405     _children.erase(it);
406 }
407
408 void OtkWidget::setEventDispatcher(OtkEventDispatcher *disp)
409 {
410   if (_event_dispatcher)
411     _event_dispatcher->clearHandler(_window);
412   _event_dispatcher = disp;
413   _event_dispatcher->registerHandler(_window, this);
414 }
415
416 void OtkWidget::exposeHandler(const XExposeEvent &e)
417 {
418   OtkEventHandler::exposeHandler(e);
419   _dirty = true;
420   update();
421 }
422
423 void OtkWidget::configureHandler(const XConfigureEvent &e)
424 {
425   OtkEventHandler::configureHandler(e);
426   if (_ignore_config) {
427     _ignore_config--;
428   } else {
429     if (!(e.width == _rect.width() && e.height == _rect.height())) {
430       _dirty = true;
431       _rect.setSize(e.width, e.height);
432     }
433     update();
434   }
435 }
436
437 }