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