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