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