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