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