]> icculus.org git repositories - mikachu/openbox.git/blob - otk/basewidget.cc
move event handling into basewidget again <FLINCH>
[mikachu/openbox.git] / otk / basewidget.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 "basewidget.hh"
8 #include "display.hh"
9 #include "assassin.hh"
10 #include "screeninfo.hh"
11
12 #include <algorithm>
13
14 namespace otk {
15
16 OtkBaseWidget::OtkBaseWidget(OtkBaseWidget *parent)
17   : OtkEventHandler(),
18     _dirty(false),
19     _parent(parent), _style(parent->getStyle()),
20     _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
21     _ignore_config(0), _visible(false), _focused(false), _grabbed_mouse(false),
22     _grabbed_keyboard(false), _texture(0), _bg_pixmap(0), _bg_pixel(0),
23     _screen(parent->getScreen())
24 {
25   assert(parent);
26   parent->addChild(this);
27   create();
28 }
29
30 OtkBaseWidget::OtkBaseWidget(Style *style, Cursor cursor, int bevel_width)
31   : OtkEventHandler(),
32     _dirty(false), _parent(0), _style(style), _cursor(cursor),
33     _bevel_width(bevel_width), _ignore_config(0), _visible(false),
34     _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
35     _texture(0), _bg_pixmap(0), _bg_pixel(0), _screen(style->getScreen())
36 {
37   assert(style);
38   create();
39 }
40
41 OtkBaseWidget::~OtkBaseWidget()
42 {
43   if (_visible)
44     hide();
45
46   std::for_each(_children.begin(), _children.end(), PointerAssassin());
47
48   if (_parent)
49     _parent->removeChild(this);
50
51   XDestroyWindow(otk::OBDisplay::display, _window);
52 }
53
54 void OtkBaseWidget::create(void)
55 {
56   const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
57   Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
58
59   _rect.setRect(0, 0, 1, 1); // just some initial values
60
61   XSetWindowAttributes attrib_create;
62   unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
63
64   attrib_create.background_pixmap = None;
65   attrib_create.colormap = scr_info->getColormap();
66   attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
67     ButtonMotionMask | ExposureMask | StructureNotifyMask;
68
69   if (_cursor) {
70     create_mask |= CWCursor;
71     attrib_create.cursor = _cursor;
72   }
73
74   _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
75                           _rect.y(), _rect.width(), _rect.height(), 0,
76                           scr_info->getDepth(), InputOutput,
77                           scr_info->getVisual(), create_mask, &attrib_create);
78   _ignore_config++;
79 }
80
81 void OtkBaseWidget::setWidth(int w)
82 {
83   assert(w > 0);
84   setGeometry(_rect.x(), _rect.y(), w, _rect.height());
85 }
86
87 void OtkBaseWidget::setHeight(int h)
88 {
89   assert(h > 0);
90   setGeometry(_rect.x(), _rect.y(), _rect.width(), h);
91 }
92
93 void OtkBaseWidget::move(const Point &to)
94 {
95   move(to.x(), to.y());
96 }
97
98 void OtkBaseWidget::move(int x, int y)
99 {
100   _rect.setPos(x, y);
101   XMoveWindow(otk::OBDisplay::display, _window, x, y);
102   _ignore_config++;
103 }
104
105 void OtkBaseWidget::resize(const Point &to)
106 {
107   resize(to.x(), to.y());
108 }
109
110 void OtkBaseWidget::resize(int w, int h)
111 {
112   assert(w > 0 && h > 0);
113   setGeometry(_rect.x(), _rect.y(), w, h);
114 }
115
116 void OtkBaseWidget::setGeometry(const Rect &new_geom)
117 {
118   setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
119 }
120  
121 void OtkBaseWidget::setGeometry(const Point &topleft, int width, int height)
122 {
123   setGeometry(topleft.x(), topleft.y(), width, height);
124 }
125
126 void OtkBaseWidget::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 OtkBaseWidget::show(bool recursive)
136 {
137   if (_visible)
138     return;
139
140   // make sure the internal state isn't mangled
141   if (_dirty)
142     update();
143
144   if (recursive) {
145     OtkBaseWidgetList::iterator it = _children.begin(), end = _children.end();
146     for (; it != end; ++it)
147       (*it)->show();
148   }
149
150   XMapWindow(otk::OBDisplay::display, _window);
151   _visible = true;
152 }
153
154 void OtkBaseWidget::hide(bool recursive)
155 {
156   if (! _visible)
157     return;
158
159   if (recursive) {
160     OtkBaseWidgetList::iterator it = _children.begin(), end = _children.end();
161     for (; it != end; ++it)
162       (*it)->hide();
163   }
164   
165   XUnmapWindow(otk::OBDisplay::display, _window);
166   _visible = false;
167 }
168
169 void OtkBaseWidget::focus(void)
170 {
171   if (! _visible)
172     return;
173
174   XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
175                  CurrentTime);
176 }
177
178 bool OtkBaseWidget::grabMouse(void)
179 {
180   Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
181                             (ButtonPressMask | ButtonReleaseMask |
182                              ButtonMotionMask | EnterWindowMask |
183                              LeaveWindowMask | PointerMotionMask),
184                             GrabModeSync, GrabModeAsync, None, None,
185                             CurrentTime);
186   _grabbed_mouse = (ret == GrabSuccess);
187   return _grabbed_mouse;
188 }
189
190 void OtkBaseWidget::ungrabMouse(void)
191 {
192   if (! _grabbed_mouse)
193     return;
194
195   XUngrabPointer(otk::OBDisplay::display, CurrentTime);
196   _grabbed_mouse = false;
197 }
198
199 bool OtkBaseWidget::grabKeyboard(void)
200 {
201   Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
202                              GrabModeSync, GrabModeAsync, CurrentTime);
203   _grabbed_keyboard = (ret == GrabSuccess);
204   return _grabbed_keyboard;
205
206 }
207
208 void OtkBaseWidget::ungrabKeyboard(void)
209 {
210   if (! _grabbed_keyboard)
211     return;
212
213   XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
214   _grabbed_keyboard = false;
215 }
216
217 void OtkBaseWidget::render(void)
218 {
219   if (!_texture) return;
220   
221   _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
222
223   if (_bg_pixmap)
224     XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
225   else {
226     unsigned int pix = _texture->color().pixel();
227     if (pix != _bg_pixel) {
228       _bg_pixel = pix;
229       XSetWindowBackground(otk::OBDisplay::display, _window, pix);
230     }
231   }
232 }
233
234 void OtkBaseWidget::update(void)
235 {
236   if (_dirty) {
237     render();
238     XClearWindow(OBDisplay::display, _window);
239   }
240
241   OtkBaseWidgetList::iterator it = _children.begin(), end = _children.end();
242   for (; it != end; ++it)
243     (*it)->update();
244
245   _dirty = false;
246 }
247
248 void OtkBaseWidget::addChild(OtkBaseWidget *child, bool front)
249 {
250   assert(child);
251   if (front)
252     _children.push_front(child);
253   else
254     _children.push_back(child);
255 }
256
257 void OtkBaseWidget::removeChild(OtkBaseWidget *child)
258 {
259   assert(child);
260   OtkBaseWidgetList::iterator it, end = _children.end();
261   for (it = _children.begin(); it != end; ++it) {
262     if ((*it) == child)
263       break;
264   }
265
266   if (it != _children.end())
267     _children.erase(it);
268 }
269
270 void OtkBaseWidget::setStyle(Style *style)
271 {
272   assert(style);
273   _style = style;
274   _dirty = true;
275   OtkBaseWidgetList::iterator it, end = _children.end();
276   for (it = _children.begin(); it != end; ++it)
277     (*it)->setStyle(style);
278 }
279
280 void OtkBaseWidget::exposeHandler(const XExposeEvent &e)
281 {
282   OtkEventHandler::exposeHandler(e);
283   _dirty = true;
284   update();
285 }
286
287 void OtkBaseWidget::configureHandler(const XConfigureEvent &e)
288 {
289   OtkEventHandler::configureHandler(e);
290   if (_ignore_config) {
291     _ignore_config--;
292   } else {
293     if (!(e.width == _rect.width() && e.height == _rect.height())) {
294       _dirty = true;
295       _rect.setSize(e.width, e.height);
296     }
297     update();
298   }
299 }
300
301 }