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