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