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