]> icculus.org git repositories - mikachu/openbox.git/blob - otk/widget.cc
initial commit of focus widget
[mikachu/openbox.git] / otk / widget.cc
1 #include "widget.hh"
2 #include "display.hh"
3 #include "assassin.hh"
4 #include "screeninfo.hh"
5
6 #include <algorithm>
7
8 namespace otk {
9
10 OtkWidget::OtkWidget(OtkWidget *parent, Direction direction)
11   : _parent(parent), _style(parent->getStyle()), _direction(direction),
12     _cursor(parent->getCursor()), _bevel_width(parent->getBevelWidth()),
13     _visible(false), _focused(false), _grabbed_mouse(false),
14     _grabbed_keyboard(false), _stretchable_vert(false),
15     _stretchable_horz(false), _texture(0), _bg_pixmap(0),
16     _screen(parent->getScreen())
17 {
18   parent->addChild(this);
19   create();
20 }
21
22 OtkWidget::OtkWidget(Style *style, Direction direction,
23                      Cursor cursor, int bevel_width)
24   : _parent(0), _style(style), _direction(direction), _cursor(cursor),
25     _bevel_width(bevel_width), _visible(false),
26     _focused(false), _grabbed_mouse(false), _grabbed_keyboard(false),
27     _stretchable_vert(false), _stretchable_horz(false), _texture(0),
28     _bg_pixmap(0), _screen(style->getScreen())
29 {
30   assert(style);
31   create();
32 }
33
34 OtkWidget::~OtkWidget()
35 {
36   if (_visible)
37     hide();
38
39   std::for_each(_children.begin(), _children.end(), PointerAssassin());
40
41   if (_parent)
42     _parent->removeChild(this);
43
44   XDestroyWindow(otk::OBDisplay::display, _window);
45 }
46
47 void OtkWidget::create(void)
48 {
49   const ScreenInfo *scr_info = otk::OBDisplay::screenInfo(_screen);
50   Window p_window = _parent ? _parent->getWindow() : scr_info->getRootWindow();
51
52   _rect.setRect(0, 0, 1, 1); // just some initial values
53
54   XSetWindowAttributes attrib_create;
55   unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWEventMask;
56
57   attrib_create.background_pixmap = None;
58   attrib_create.colormap = scr_info->getColormap();
59   attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
60     ButtonMotionMask | ExposureMask | StructureNotifyMask;
61
62   if (_cursor) {
63     create_mask |= CWCursor;
64     attrib_create.cursor = _cursor;
65   }
66
67   _window = XCreateWindow(otk::OBDisplay::display, p_window, _rect.x(),
68                           _rect.y(), _rect.width(), _rect.height(), 0,
69                           scr_info->getDepth(), InputOutput,
70                           scr_info->getVisual(), create_mask, &attrib_create);
71 }
72
73 void OtkWidget::move(const Point &to)
74 {
75   move(to.x(), to.y());
76 }
77
78 void OtkWidget::move(int x, int y)
79 {
80   _rect.setPos(x, y);
81   XMoveWindow(otk::OBDisplay::display, _window, x, y);
82 }
83
84 void OtkWidget::resize(const Point &to)
85 {
86   resize(to.x(), to.y());
87 }
88
89 void OtkWidget::resize(int x, int y)
90 {
91   assert(x >= _rect.x() && y >= _rect.y());
92
93   setGeometry(_rect.x(), _rect.y(), x - _rect.x(), y - _rect.y());
94 }
95
96 void OtkWidget::setGeometry(const Rect &new_geom)
97 {
98   setGeometry(new_geom.x(), new_geom.y(), new_geom.width(), new_geom.height());
99 }
100  
101 void OtkWidget::setGeometry(const Point &topleft, int width, int height)
102 {
103   setGeometry(topleft.x(), topleft.y(), width, height);
104 }
105
106 void OtkWidget::setGeometry(int x, int y, int width, int height)
107 {
108   _rect = Rect(x, y, width, height);
109
110   fprintf(stderr, "Resizing to x: %d, y: %d, width: %d, height: %d\n",
111           x, y, width, height);
112
113   XMoveResizeWindow(otk::OBDisplay::display, _window, x, y, width, height);
114   setTexture();
115 }
116
117 void OtkWidget::show(void)
118 {
119   if (_visible)
120     return;
121
122   OtkWidgetList::iterator it = _children.begin(), end = _children.end();
123   for (; it != end; ++it) {
124     fprintf(stderr, "showing child\n");
125     (*it)->show();
126   }
127
128   fprintf(stderr, "x: %d, y: %d, width: %d, height: %d\n",
129           _rect.x(), _rect.y(), _rect.width(), _rect.height());
130
131   XMapWindow(otk::OBDisplay::display, _window);
132   _visible = true;
133 }
134
135 void OtkWidget::hide(void)
136 {
137   if (! _visible)
138     return;
139
140   OtkWidgetList::iterator it = _children.begin(), end = _children.end();
141   for (; it != end; ++it)
142     (*it)->hide();
143
144   XUnmapWindow(otk::OBDisplay::display, _window);
145   _visible = false;
146 }
147
148 void OtkWidget::focus(void)
149 {
150   if (! _visible)
151     return;
152
153   XSetInputFocus(otk::OBDisplay::display, _window, RevertToPointerRoot,
154                  CurrentTime);
155 }
156
157 bool OtkWidget::grabMouse(void)
158 {
159   Status ret = XGrabPointer(otk::OBDisplay::display, _window, True,
160                             (ButtonPressMask | ButtonReleaseMask |
161                              ButtonMotionMask | EnterWindowMask |
162                              LeaveWindowMask | PointerMotionMask),
163                             GrabModeSync, GrabModeAsync, None, None,
164                             CurrentTime);
165   _grabbed_mouse = (ret == GrabSuccess);
166   return _grabbed_mouse;
167 }
168
169 void OtkWidget::ungrabMouse(void)
170 {
171   if (! _grabbed_mouse)
172     return;
173
174   XUngrabPointer(otk::OBDisplay::display, CurrentTime);
175   _grabbed_mouse = false;
176 }
177
178 bool OtkWidget::grabKeyboard(void)
179 {
180   Status ret = XGrabKeyboard(otk::OBDisplay::display, _window, True,
181                              GrabModeSync, GrabModeAsync, CurrentTime);
182   _grabbed_keyboard = (ret == GrabSuccess);
183   return _grabbed_keyboard;
184
185 }
186
187 void OtkWidget::ungrabKeyboard(void)
188 {
189   if (! _grabbed_keyboard)
190     return;
191
192   XUngrabKeyboard(otk::OBDisplay::display, CurrentTime);
193   _grabbed_keyboard = false;
194 }
195
196 void OtkWidget::setTexture(BTexture *texture)
197 {
198   if (!texture && !_texture)
199     return;
200
201   Pixmap old = _bg_pixmap;
202
203   if (texture)
204     _texture = texture;
205
206   _bg_pixmap = _texture->render(_rect.width(), _rect.height(), _bg_pixmap);
207
208   if (_bg_pixmap != old)
209     XSetWindowBackgroundPixmap(otk::OBDisplay::display, _window, _bg_pixmap);
210   
211   //XSetWindowBackground(otk::OBDisplay::display, win, pix);
212 }
213
214 void OtkWidget::addChild(OtkWidget *child, bool front)
215 {
216   assert(child);
217   if (front)
218     _children.push_front(child);
219   else
220     _children.push_back(child);
221 }
222
223 void OtkWidget::removeChild(OtkWidget *child)
224 {
225   OtkWidgetList::iterator it, end = _children.end();
226   for (; it != end; ++it) {
227     if ((*it) == child)
228       break;
229   }
230
231   if (it != _children.end())
232     _children.erase(it);
233 }
234
235 }