]> Dogcows Code - chaz/openbox/blob - otk/widget.cc
HOOGE improvements. now widgets calculate their size according to the sizes of their...
[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 for (it = _children.begin(); it != end; ++it) {
300 tmp = *it;
301 if (tmp->_rect.width() > widest)
302 widest = tmp->_rect.width();
303 height += tmp->_rect.height() + _bevel_width;
304 }
305
306 OtkWidget *prev_widget = 0;
307
308 for (it = _children.begin(); it != end; ++it) {
309 tmp = *it;
310 int x, y;
311
312 if (prev_widget)
313 y = prev_widget->_rect.y() + prev_widget->_rect.height() + _bevel_width;
314 else
315 y = _rect.y() + _bevel_width;
316 x = (widest - tmp->_rect.width()) / 2 + _bevel_width;
317
318 tmp->move(x, y);
319
320 prev_widget = tmp;
321 }
322
323 internalResize(widest + _bevel_width * 2, height);
324 }
325
326 void OtkWidget::update(void)
327 {
328 if (_dirty) {
329 adjust();
330 render();
331 XClearWindow(OBDisplay::display, _window);
332 }
333 _dirty = false;
334 }
335
336 void OtkWidget::internalResize(int w, int h)
337 {
338 assert(w > 0 && h > 0);
339
340 if (! _fixed_width && ! _fixed_height)
341 resize(w, h);
342 else if (! _fixed_width)
343 resize(w, _rect.height());
344 else if (! _fixed_height)
345 resize(_rect.width(), h);
346 }
347
348 void OtkWidget::addChild(OtkWidget *child, bool front)
349 {
350 assert(child);
351 if (front)
352 _children.push_front(child);
353 else
354 _children.push_back(child);
355 }
356
357 void OtkWidget::removeChild(OtkWidget *child)
358 {
359 assert(child);
360 OtkWidgetList::iterator it, end = _children.end();
361 for (it = _children.begin(); it != end; ++it) {
362 if ((*it) == child)
363 break;
364 }
365
366 if (it != _children.end())
367 _children.erase(it);
368 }
369
370 }
This page took 0.059212 seconds and 4 git commands to generate.