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