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