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