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