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