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