]> icculus.org git repositories - dana/openbox.git/blob - src/frame.cc
can tell where events are coming from!
[dana/openbox.git] / src / frame.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef HAVE_CONFIG_H
4 # include "../config.h"
5 #endif
6
7 extern "C" {
8 #ifdef    SHAPE
9 #include <X11/extensions/shape.h>
10 #endif // SHAPE
11 }
12
13 #include "openbox.hh"
14 #include "frame.hh"
15 #include "client.hh"
16 #include "otk/display.hh"
17
18 #include <string>
19
20 namespace ob {
21
22 const long OBFrame::event_mask;
23
24 OBFrame::OBFrame(OBClient *client, otk::Style *style)
25   : otk::OtkWidget(Openbox::instance, style),
26     OBWidget(Type_Frame),
27     _client(client),
28     _screen(otk::OBDisplay::screenInfo(client->screen())),
29     _plate(this, OBWidget::Type_Plate),
30     _titlebar(this, OBWidget::Type_Titlebar),
31     _button_close(&_titlebar, OBWidget::Type_CloseButton),
32     _button_iconify(&_titlebar, OBWidget::Type_IconifyButton),
33     _button_max(&_titlebar, OBWidget::Type_MaximizeButton),
34     _button_stick(&_titlebar, OBWidget::Type_StickyButton),
35     _label(&_titlebar, OBWidget::Type_Label),
36     _handle(this, OBWidget::Type_Handle),
37     _grip_left(&_handle, OBWidget::Type_LeftGrip),
38     _grip_right(&_handle, OBWidget::Type_RightGrip),
39     _decorations(client->decorations())
40 {
41   assert(client);
42   assert(style);
43
44   XSelectInput(otk::OBDisplay::display, getWindow(), OBFrame::event_mask);
45   
46   unmanaged();
47   _titlebar.unmanaged();
48   _button_close.unmanaged();
49   _button_iconify.unmanaged();
50   _button_max.unmanaged();
51   _button_stick.unmanaged();
52   _label.unmanaged();
53   _handle.unmanaged();
54   _grip_left.unmanaged();
55   _grip_right.unmanaged();
56   _plate.unmanaged();
57
58   _grip_left.setCursor(Openbox::instance->cursors().ll_angle);
59   _grip_right.setCursor(Openbox::instance->cursors().lr_angle);
60   
61   _button_close.setText("X");
62   _button_iconify.setText("I");
63   _button_max.setText("M");
64   _button_stick.setText("S");
65   _label.setText(_client->title());
66
67   _style = 0;
68   setStyle(style);
69
70   //XXX: uncomment me unfocus(); // stuff starts out focused in otk
71   
72   _plate.show(); // the other stuff is shown based on decor settings
73   
74   grabClient();
75 }
76
77
78 OBFrame::~OBFrame()
79 {
80   releaseClient(false);
81 }
82
83
84 void OBFrame::setStyle(otk::Style *style)
85 {
86   assert(style);
87
88   otk::OtkWidget::setStyle(style);
89
90   // if a style was previously set, then 'replace' is true, cause we're
91   // replacing a style
92   bool replace = (_style);
93
94   if (replace) {
95     // XXX: do shit here whatever
96   }
97   
98   _style = style;
99
100   // XXX: change when focus changes!
101   XSetWindowBorder(otk::OBDisplay::display, getWindow(),
102                    _style->getBorderColor()->pixel());
103
104   // if !replace, then adjust() will get called after the client is grabbed!
105   if (replace) {
106     // size/position everything
107     adjustSize();
108     adjustPosition();
109   }
110 }
111
112
113 void OBFrame::adjustSize()
114 {
115   // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
116   _decorations = _client->decorations();
117   _decorations = 0xffffffff;
118   
119   int width;   // the width of the client and its border
120   int bwidth;  // width to make borders
121   int cbwidth; // width of the inner client border
122   
123   if (_decorations & OBClient::Decor_Border) {
124     bwidth = _style->getBorderWidth();
125     cbwidth = _style->getFrameWidth();
126   } else
127     bwidth = cbwidth = 0;
128   _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
129     cbwidth;
130   width = _client->area().width() + cbwidth * 2;
131
132   _plate.setBorderWidth(cbwidth);
133
134   setBorderWidth(bwidth);
135   _titlebar.setBorderWidth(bwidth);
136   _grip_left.setBorderWidth(bwidth);
137   _grip_right.setBorderWidth(bwidth);
138   _handle.setBorderWidth(bwidth);
139   
140   if (_decorations & OBClient::Decor_Titlebar) {
141     // set the titlebar size
142     _titlebar.setGeometry(-bwidth,
143                           -bwidth,
144                           width,
145                           (_style->getFont().height() +
146                            _style->getBevelWidth() * 2));
147     _innersize.top += _titlebar.height() + bwidth;
148
149     // set the label size
150     _label.setGeometry(0, _style->getBevelWidth(),
151                        width, _style->getFont().height());
152     // set the buttons sizes
153     if (_decorations & OBClient::Decor_Iconify)
154       _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
155                                   _label.height() - 2,
156                                   _label.height() - 2);
157     if (_decorations & OBClient::Decor_Maximize)
158       _button_max.setGeometry(0, _style->getBevelWidth() + 1,
159                               _label.height() - 2,
160                               _label.height() - 2);
161     if (_decorations & OBClient::Decor_Sticky)
162       _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
163                                 _label.height() - 2,
164                                 _label.height() - 2);
165     if (_decorations & OBClient::Decor_Close)
166       _button_close.setGeometry(0, _style->getBevelWidth() + 1,
167                                 _label.height() - 2,
168                                 _label.height() - 2);
169
170     // separation between titlebar elements
171     const int sep = _style->getBevelWidth() + 1;
172
173     std::string layout = "SLIMC"; // XXX: get this from somewhere
174     // XXX: it is REQUIRED that by this point, the string only has one of each
175     // possible letter, all of the letters are valid, and L exists somewhere in
176     // the string!
177
178     // the size of the label. this ASSUMES the layout has only buttons other
179     // that the ONE LABEL!!
180     // adds an extra sep so that there's a space on either side of the
181     // titlebar.. note: x = sep, below.
182     int lwidth = width - sep * 2 -
183       (_button_iconify.width() + sep) * (layout.size() - 1);
184     // quick sanity check for really small windows. if this is needed, its
185     // obviously not going to be displayed right...
186     // XXX: maybe we should make this look better somehow? constraints?
187     if (lwidth <= 0) lwidth = 1;
188     _label.setWidth(lwidth);
189
190     int x = sep;
191     for (int i = 0, len = layout.size(); i < len; ++i) {
192       switch (layout[i]) {
193       case 'I':
194         _button_iconify.move(x, _button_iconify.getRect().y());
195         x += _button_iconify.width();
196         break;
197       case 'L':
198         _label.move(x, _label.getRect().y());
199         x += _label.width();
200         break;
201       case 'M':
202         _button_max.move(x, _button_max.getRect().y());
203         x += _button_max.width();
204         break;
205       case 'S':
206         _button_stick.move(x, _button_stick.getRect().y());
207         x += _button_stick.width();
208         break;
209       case 'C':
210         _button_close.move(x, _button_close.getRect().y());
211         x += _button_close.width();
212         break;
213       default:
214         assert(false); // the layout string is invalid!
215       }
216       x += sep;
217     }
218   }
219
220   if (_decorations & OBClient::Decor_Handle) {
221     _handle.setGeometry(-bwidth,
222                         _innersize.top + _client->area().height() + cbwidth,
223                         width, _style->getHandleWidth());
224     _grip_left.setGeometry(-bwidth,
225                            -bwidth,
226                            // XXX: get a Point class in otk and use that for
227                            // the 'buttons size' since theyre all the same
228                            _button_iconify.width() * 2,
229                            _handle.height());
230     _grip_right.setGeometry(((_handle.getRect().right() + 1) -
231                              _button_iconify.width() * 2),
232                             -bwidth,
233                             // XXX: get a Point class in otk and use that for
234                             // the 'buttons size' since theyre all the same
235                             _button_iconify.width() * 2,
236                             _handle.height());
237     _innersize.bottom += _handle.height() + bwidth;
238   }
239   
240
241   // position/size all the windows
242
243   resize(_innersize.left + _innersize.right + _client->area().width(),
244          _innersize.top + _innersize.bottom + _client->area().height());
245
246   _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
247                      _client->area().width(), _client->area().height());
248
249   // map/unmap all the windows
250   if (_decorations & OBClient::Decor_Titlebar) {
251     _label.show();
252     if (_decorations & OBClient::Decor_Iconify)
253       _button_iconify.show();
254     else
255       _button_iconify.hide();
256     if (_decorations & OBClient::Decor_Maximize)
257       _button_max.show();
258     else
259       _button_max.hide();
260     if (_decorations & OBClient::Decor_Sticky)
261       _button_stick.show();
262     else
263       _button_stick.hide();
264     if (_decorations & OBClient::Decor_Close)
265       _button_close.show();
266     else
267       _button_close.hide();
268     _titlebar.show();
269   } else {
270     _titlebar.hide(true);
271   }
272
273   if (_decorations & OBClient::Decor_Handle)
274     _handle.show(true);
275   else
276     _handle.hide(true);
277   
278   // XXX: more is gunna have to happen here
279
280   _size.left   = _innersize.left + bwidth;
281   _size.right  = _innersize.right + bwidth;
282   _size.top    = _innersize.top + bwidth;
283   _size.bottom = _innersize.bottom + bwidth;
284
285   adjustShape();
286
287   update();
288 }
289
290
291 void OBFrame::adjustPosition()
292 {
293   int x, y;
294   clientGravity(x, y);
295   move(x, y);
296 }
297
298
299 void OBFrame::adjustShape()
300 {
301 #ifdef SHAPE
302   int bwidth = (_decorations & OBClient::Decor_Border) ?
303     _style->getBorderWidth() : 0;
304   
305   if (!_client->shaped()) {
306     // clear the shape on the frame window
307     XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
308                       _innersize.left,
309                       _innersize.top,
310                       None, ShapeSet);
311   } else {
312     // make the frame's shape match the clients
313     XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
314                        _innersize.left,
315                        _innersize.top,
316                        _client->window(), ShapeBounding, ShapeSet);
317
318     int num = 0;
319     XRectangle xrect[2];
320
321     if (_decorations & OBClient::Decor_Titlebar) {
322       xrect[0].x = _titlebar.getRect().x();
323       xrect[0].y = _titlebar.getRect().y();
324       xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
325       xrect[0].height = _titlebar.height() + bwidth * 2;
326       ++num;
327     }
328
329     if (_decorations & OBClient::Decor_Handle) {
330       xrect[1].x = _handle.getRect().x();
331       xrect[1].y = _handle.getRect().y();
332       xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
333       xrect[1].height = _handle.height() + bwidth * 2;
334       ++num;
335     }
336
337     XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
338                             ShapeBounding, 0, 0, xrect, num,
339                             ShapeUnion, Unsorted);
340   }
341 #endif // SHAPE
342 }
343
344
345 void OBFrame::grabClient()
346 {
347   
348   // reparent the client to the frame
349   XReparentWindow(otk::OBDisplay::display, _client->window(),
350                   _plate.getWindow(), 0, 0);
351   _client->ignore_unmaps++;
352
353   // select the event mask on the client's parent (to receive config req's)
354   XSelectInput(otk::OBDisplay::display, _plate.getWindow(),
355                SubstructureRedirectMask);
356
357   // map the client so it maps when the frame does
358   XMapWindow(otk::OBDisplay::display, _client->window());
359
360   adjustSize();
361   adjustPosition();
362 }
363
364
365 void OBFrame::releaseClient(bool remap)
366 {
367   // check if the app has already reparented its window to the root window
368   XEvent ev;
369   if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
370                              ReparentNotify, &ev)) {
371     remap = true; // XXX: why do we remap the window if they already
372                   // reparented to root?
373   } else {
374     // according to the ICCCM - if the client doesn't reparent to
375     // root, then we have to do it for them
376     XReparentWindow(otk::OBDisplay::display, _client->window(),
377                     _screen->getRootWindow(),
378                     _client->area().x(), _client->area().y());
379   }
380
381   // if we want to remap the window, do so now
382   if (remap)
383     XMapWindow(otk::OBDisplay::display, _client->window());
384 }
385
386
387 void OBFrame::clientGravity(int &x, int &y)
388 {
389   x = _client->area().x();
390   y = _client->area().y();
391
392   // horizontal
393   switch (_client->gravity()) {
394   default:
395   case NorthWestGravity:
396   case SouthWestGravity:
397   case WestGravity:
398     break;
399
400   case NorthGravity:
401   case SouthGravity:
402   case CenterGravity:
403     x -= (_size.left + _size.right) / 2;
404     break;
405
406   case NorthEastGravity:
407   case SouthEastGravity:
408   case EastGravity:
409     x -= _size.left + _size.right;
410     break;
411
412   case ForgetGravity:
413   case StaticGravity:
414     x -= _size.left;
415     break;
416   }
417
418   // vertical
419   switch (_client->gravity()) {
420   default:
421   case NorthWestGravity:
422   case NorthEastGravity:
423   case NorthGravity:
424     break;
425
426   case CenterGravity:
427   case EastGravity:
428   case WestGravity:
429     y -= (_size.top + _size.bottom) / 2;
430     break;
431
432   case SouthWestGravity:
433   case SouthEastGravity:
434   case SouthGravity:
435     y -= _size.top + _size.bottom;
436     break;
437
438   case ForgetGravity:
439   case StaticGravity:
440     y -= _size.top;
441     break;
442   }
443 }
444
445
446 void OBFrame::frameGravity(int &x, int &y)
447 {
448   x = getRect().x();
449   y = getRect().y();
450   
451   // horizontal
452   switch (_client->gravity()) {
453   default:
454   case NorthWestGravity:
455   case WestGravity:
456   case SouthWestGravity:
457     break;
458   case NorthGravity:
459   case CenterGravity:
460   case SouthGravity:
461     x += (_size.left + _size.right) / 2;
462     break;
463   case NorthEastGravity:
464   case EastGravity:
465   case SouthEastGravity:
466     x += _size.left + _size.right;
467     break;
468   case StaticGravity:
469   case ForgetGravity:
470     x += _size.left;
471     break;
472   }
473
474   // vertical
475   switch (_client->gravity()) {
476   default:
477   case NorthWestGravity:
478   case WestGravity:
479   case SouthWestGravity:
480     break;
481   case NorthGravity:
482   case CenterGravity:
483   case SouthGravity:
484     y += (_size.top + _size.bottom) / 2;
485     break;
486   case NorthEastGravity:
487   case EastGravity:
488   case SouthEastGravity:
489     y += _size.top + _size.bottom;
490     break;
491   case StaticGravity:
492   case ForgetGravity:
493     y += _size.top;
494     break;
495   }
496 }
497
498
499 }