]> Dogcows Code - chaz/openbox/blob - otk/widget.cc
fix indenting
[chaz/openbox] / 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), _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 Widget::Widget(EventDispatcher *event_dispatcher, Style *style,
37 Direction direction, Cursor cursor, int bevel_width,
38 bool override_redirect)
39 : EventHandler(),
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(override_redirect);
52 _event_dispatcher->registerHandler(_window, this);
53 setStyle(_style); // let the widget initialize stuff
54 }
55
56 Widget::~Widget()
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(**display, _window);
69 }
70
71 void Widget::create(bool override_redirect)
72 {
73 const ScreenInfo *scr_info = display->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
81 attrib_create.background_pixmap = None;
82 attrib_create.colormap = scr_info->colormap();
83 attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
84 ButtonMotionMask | ExposureMask | StructureNotifyMask;
85
86 if (override_redirect) {
87 create_mask |= CWOverrideRedirect;
88 attrib_create.override_redirect = true;
89 }
90
91 if (_cursor) {
92 create_mask |= CWCursor;
93 attrib_create.cursor = _cursor;
94 }
95
96 _window = XCreateWindow(**display, p_window, _rect.x(),
97 _rect.y(), _rect.width(), _rect.height(), 0,
98 scr_info->depth(), InputOutput,
99 scr_info->visual(), create_mask, &attrib_create);
100 _ignore_config++;
101 }
102
103 void Widget::setWidth(int w)
104 {
105 assert(w > 0);
106 _fixed_width = true;
107 setGeometry(_rect.x(), _rect.y(), w, _rect.height());
108 }
109
110 void Widget::setHeight(int h)
111 {
112 assert(h > 0);
113 _fixed_height = true;
114 setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
115 }
116
117 void Widget::move(const Point &to)
118 {
119 move(to.x(), to.y());
120 }
121
122 void Widget::move(int x, int y)
123 {
124 _rect.setPos(x, y);
125 XMoveWindow(**display, _window, x, y);
126 _ignore_config++;
127 }
128
129 void Widget::resize(const Point &to)
130 {
131 resize(to.x(), to.y());
132 }
133
134 void Widget::resize(int w, int h)
135 {
136 assert(w > 0 && h > 0);
137 _fixed_width = _fixed_height = true;
138 setGeometry(_rect.x(), _rect.y(), w, h);
139 }
140
141 void Widget::setGeometry(const Rect &new_geom)
142 {
143 setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
144 }
145
146 void Widget::setGeometry(const Point &topleft, int width, int height)
147 {
148 setGeometry(topleft.x(), topleft.y(), width, height);
149 }
150
151 void Widget::setGeometry(int x, int y, int width, int height)
152 {
153 _rect = Rect(x, y, width, height);
154 _dirty = true;
155
156 XMoveResizeWindow(**display, _window, x, y, width, height);
157 _ignore_config++;
158 }
159
160 void Widget::show(bool recursive)
161 {
162 if (_visible)
163 return;
164
165 // make sure the internal state isn't mangled
166 if (_dirty)
167 update();
168
169 if (recursive) {
170 WidgetList::iterator it = _children.begin(), end = _children.end();
171 for (; it != end; ++it)
172 (*it)->show();
173 }
174
175 XMapWindow(**display, _window);
176 _visible = true;
177 }
178
179 void Widget::hide(bool recursive)
180 {
181 if (! _visible)
182 return;
183
184 if (recursive) {
185 WidgetList::iterator it = _children.begin(), end = _children.end();
186 for (; it != end; ++it)
187 (*it)->hide();
188 }
189
190 XUnmapWindow(**display, _window);
191 _visible = false;
192 }
193
194 void Widget::focus(void)
195 {
196 _focused = true;
197
198 Widget::WidgetList::iterator it = _children.begin(),
199 end = _children.end();
200 for (; it != end; ++it)
201 (*it)->focus();
202 }
203
204 void Widget::unfocus(void)
205 {
206 _focused = false;
207
208 Widget::WidgetList::iterator it = _children.begin(),
209 end = _children.end();
210 for (; it != end; ++it)
211 (*it)->unfocus();
212 }
213
214 bool Widget::grabMouse(void)
215 {
216 Status ret = XGrabPointer(**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 Widget::ungrabMouse(void)
227 {
228 if (! _grabbed_mouse)
229 return;
230
231 XUngrabPointer(**display, CurrentTime);
232 _grabbed_mouse = false;
233 }
234
235 bool Widget::grabKeyboard(void)
236 {
237 Status ret = XGrabKeyboard(**display, _window, True,
238 GrabModeSync, GrabModeAsync, CurrentTime);
239 _grabbed_keyboard = (ret == GrabSuccess);
240 return _grabbed_keyboard;
241
242 }
243
244 void Widget::ungrabKeyboard(void)
245 {
246 if (! _grabbed_keyboard)
247 return;
248
249 XUngrabKeyboard(**display, CurrentTime);
250 _grabbed_keyboard = false;
251 }
252
253 void Widget::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(**display, _window, _bg_pixmap);
261 _bg_pixel = None;
262 } else {
263 unsigned int pix = _texture->color().pixel();
264 if (pix != _bg_pixel) {
265 _bg_pixel = pix;
266 XSetWindowBackground(**display, _window, pix);
267 }
268 }
269 }
270
271 void Widget::adjust(void)
272 {
273 if (_direction == Horizontal)
274 adjustHorz();
275 else
276 adjustVert();
277 }
278
279 void Widget::adjustHorz(void)
280 {
281 if (_children.size() == 0)
282 return;
283
284 Widget *tmp;
285 WidgetList::iterator it, end = _children.end();
286
287 int tallest = 0;
288 int width = _bevel_width;
289 WidgetList stretchable;
290
291 for (it = _children.begin(); it != end; ++it) {
292 tmp = *it;
293 if (tmp->isStretchableVert())
294 tmp->setHeight(_rect.height() > _bevel_width * 2 ?
295 _rect.height() - _bevel_width * 2 : _bevel_width);
296 if (tmp->isStretchableHorz())
297 stretchable.push_back(tmp);
298 else
299 width += tmp->_rect.width() + _bevel_width;
300
301 if (tmp->_rect.height() > tallest)
302 tallest = tmp->_rect.height();
303 }
304
305 if (stretchable.size() > 0) {
306 WidgetList::iterator str_it = stretchable.begin(),
307 str_end = stretchable.end();
308
309 int str_width = _rect.width() - width / stretchable.size();
310
311 for (; str_it != str_end; ++str_it)
312 (*str_it)->setWidth(str_width > _bevel_width ? str_width - _bevel_width
313 : _bevel_width);
314 }
315
316 Widget *prev_widget = 0;
317
318 for (it = _children.begin(); it != end; ++it) {
319 tmp = *it;
320 int x, y;
321
322 if (prev_widget)
323 x = prev_widget->_rect.x() + prev_widget->_rect.width() + _bevel_width;
324 else
325 x = _rect.x() + _bevel_width;
326 y = (tallest - tmp->_rect.height()) / 2 + _bevel_width;
327
328 tmp->move(x, y);
329
330 prev_widget = tmp;
331 }
332
333 internalResize(width, tallest + _bevel_width * 2);
334 }
335
336 void Widget::adjustVert(void)
337 {
338 if (_children.size() == 0)
339 return;
340
341 Widget *tmp;
342 WidgetList::iterator it, end = _children.end();
343
344 int widest = 0;
345 int height = _bevel_width;
346 WidgetList stretchable;
347
348 for (it = _children.begin(); it != end; ++it) {
349 tmp = *it;
350 if (tmp->isStretchableHorz())
351 tmp->setWidth(_rect.width() > _bevel_width * 2 ?
352 _rect.width() - _bevel_width * 2 : _bevel_width);
353 if (tmp->isStretchableVert())
354 stretchable.push_back(tmp);
355 else
356 height += tmp->_rect.height() + _bevel_width;
357
358 if (tmp->_rect.width() > widest)
359 widest = tmp->_rect.width();
360 }
361
362 if (stretchable.size() > 0) {
363 WidgetList::iterator str_it = stretchable.begin(),
364 str_end = stretchable.end();
365
366 int str_height = _rect.height() - height / stretchable.size();
367
368 for (; str_it != str_end; ++str_it)
369 (*str_it)->setHeight(str_height > _bevel_width ?
370 str_height - _bevel_width : _bevel_width);
371 }
372
373 Widget *prev_widget = 0;
374
375 for (it = _children.begin(); it != end; ++it) {
376 tmp = *it;
377 int x, y;
378
379 if (prev_widget)
380 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
381 else
382 y = _rect.y() + _bevel_width;
383 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
384
385 tmp->move(x, y);
386
387 prev_widget = tmp;
388 }
389
390 internalResize(widest + _bevel_width * 2, height);
391 }
392
393 void Widget::update(void)
394 {
395 if (_dirty) {
396 adjust();
397 render();
398 XClearWindow(**display, _window);
399 }
400
401 WidgetList::iterator it = _children.begin(), end = _children.end();
402 for (; it != end; ++it)
403 (*it)->update();
404
405 _dirty = false;
406 }
407
408 void Widget::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 Widget::addChild(Widget *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 Widget::removeChild(Widget *child)
430 {
431 assert(child);
432 WidgetList::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 Widget::setStyle(Style *style)
443 {
444 assert(style);
445 _style = style;
446 _dirty = true;
447
448 WidgetList::iterator it, end = _children.end();
449 for (it = _children.begin(); it != end; ++it)
450 (*it)->setStyle(style);
451 }
452
453
454 void Widget::setEventDispatcher(EventDispatcher *disp)
455 {
456 if (_event_dispatcher)
457 _event_dispatcher->clearHandler(_window);
458 _event_dispatcher = disp;
459 _event_dispatcher->registerHandler(_window, this);
460 }
461
462 void Widget::exposeHandler(const XExposeEvent &e)
463 {
464 EventHandler::exposeHandler(e);
465 _dirty = true;
466 update();
467 }
468
469 void Widget::configureHandler(const XConfigureEvent &e)
470 {
471 EventHandler::configureHandler(e);
472 if (_ignore_config) {
473 _ignore_config--;
474 } else {
475 if (!(e.width == _rect.width() && e.height == _rect.height())) {
476 _dirty = true;
477 _rect.setSize(e.width, e.height);
478 }
479 update();
480 }
481 }
482
483 }
This page took 0.052622 seconds and 4 git commands to generate.