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