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