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