]> icculus.org git repositories - dana/openbox.git/blob - src/frame.cc
titlebar layouts work
[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 "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   _decorations = 0xffffffff;
130
131   // true/false for whether to show each element of the titlebar
132   bool tit_i = false, tit_m = false, tit_s = false, tit_c = false;
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   const int bevel = _style->getBevelWidth();
137   
138   if (_decorations & OBClient::Decor_Border) {
139     bwidth = _style->getBorderWidth();
140     cbwidth = _style->getFrameWidth();
141   } else
142     bwidth = cbwidth = 0;
143   _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
144     cbwidth;
145   width = _client->area().width() + cbwidth * 2;
146
147   _plate.setBorderWidth(cbwidth);
148
149   setBorderWidth(bwidth);
150   _titlebar.setBorderWidth(bwidth);
151   _grip_left.setBorderWidth(bwidth);
152   _grip_right.setBorderWidth(bwidth);
153   _handle.setBorderWidth(bwidth);
154   
155   if (_decorations & OBClient::Decor_Titlebar) {
156     // set the titlebar size
157     _titlebar.setGeometry(-bwidth,
158                           -bwidth,
159                           width,
160                           _style->getFont()->height() + bevel * 2);
161     _innersize.top += _titlebar.height() + bwidth;
162
163     // set the label size
164     _label.setGeometry(0, bevel, width, _style->getFont()->height());
165     // set the buttons sizes
166     if (_decorations & OBClient::Decor_Iconify)
167       _button_iconify.setGeometry(0, bevel + 1,
168                                   _label.height() - 2,
169                                   _label.height() - 2);
170     if (_decorations & OBClient::Decor_Maximize)
171       _button_max.setGeometry(0, bevel + 1,
172                               _label.height() - 2,
173                               _label.height() - 2);
174     if (_decorations & OBClient::Decor_Sticky)
175       _button_stick.setGeometry(0, bevel + 1,
176                                 _label.height() - 2,
177                                 _label.height() - 2);
178     if (_decorations & OBClient::Decor_Close)
179       _button_close.setGeometry(0, bevel + 1,
180                                 _label.height() - 2,
181                                 _label.height() - 2);
182
183     // separation between titlebar elements
184     const int sep = bevel + 1;
185
186     std::string layout;
187     if (!python_get_string("titlebar_layout", &layout))
188       layout = "ILMC";
189
190     // this code ensures that the string only has one of each possible
191     // letter, all of the letters are valid, and L exists somewhere in the
192     // string!
193     bool tit_l = false;
194   
195     for (std::string::size_type i = 0; i < layout.size(); ++i) {
196       switch(layout[i]) {
197       case 'i':
198       case 'I':
199         if (!tit_i && (_decorations & OBClient::Decor_Iconify)) {
200           tit_i = true;
201           continue;
202         }
203       case 'l':
204       case 'L':
205         if (!tit_l) {
206           tit_l = true;
207           continue;
208         }
209       case 'm':
210       case 'M':
211         if (!tit_m && (_decorations & OBClient::Decor_Maximize)) {
212           tit_m = true;
213           continue;
214         }
215       case 's':
216       case 'S':
217         if (!tit_s && (_decorations & OBClient::Decor_Sticky)) {
218           tit_s = true;
219           continue;
220         }
221       case 'c':
222       case 'C':
223         if (!tit_c && (_decorations & OBClient::Decor_Close)) {
224           tit_c = true;
225           continue;
226         }
227       }
228       // if we get here then we don't want the letter, kill it
229       layout.erase(i--, 1);
230     }
231     if (!tit_l)
232       layout += 'L';
233     
234     // the size of the label. this ASSUMES the layout has only buttons other
235     // that the ONE LABEL!!
236     // adds an extra sep so that there's a space on either side of the
237     // titlebar.. note: x = sep, below.
238     int lwidth = width - sep * 2 -
239       (_button_iconify.width() + sep) * (layout.size() - 1);
240     // quick sanity check for really small windows. if this is needed, its
241     // obviously not going to be displayed right...
242     // XXX: maybe we should make this look better somehow? constraints?
243     if (lwidth <= 0) lwidth = 1;
244     _label.setWidth(lwidth);
245
246     int x = sep;
247     for (int i = 0, len = layout.size(); i < len; ++i) {
248       switch (layout[i]) {
249       case 'I':
250         _button_iconify.move(x, _button_iconify.rect().y());
251         x += _button_iconify.width();
252         break;
253       case 'L':
254         _label.move(x, _label.rect().y());
255         x += _label.width();
256         break;
257       case 'M':
258         _button_max.move(x, _button_max.rect().y());
259         x += _button_max.width();
260         break;
261       case 'S':
262         _button_stick.move(x, _button_stick.rect().y());
263         x += _button_stick.width();
264         break;
265       case 'C':
266         _button_close.move(x, _button_close.rect().y());
267         x += _button_close.width();
268         break;
269       default:
270         assert(false); // the layout string is invalid!
271       }
272       x += sep;
273     }
274   }
275
276   if (_decorations & OBClient::Decor_Handle) {
277     _handle.setGeometry(-bwidth,
278                         _innersize.top + _client->area().height() + cbwidth,
279                         width, _style->getHandleWidth());
280     _grip_left.setGeometry(-bwidth,
281                            -bwidth,
282                            // XXX: get a Point class in otk and use that for
283                            // the 'buttons size' since theyre all the same
284                            _button_iconify.width() * 2,
285                            _handle.height());
286     _grip_right.setGeometry(((_handle.rect().right() + 1) -
287                              _button_iconify.width() * 2),
288                             -bwidth,
289                             // XXX: get a Point class in otk and use that for
290                             // the 'buttons size' since theyre all the same
291                             _button_iconify.width() * 2,
292                             _handle.height());
293     _innersize.bottom += _handle.height() + bwidth;
294   }
295   
296
297   // position/size all the windows
298
299   resize(_innersize.left + _innersize.right + _client->area().width(),
300          _innersize.top + _innersize.bottom + _client->area().height());
301
302   _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
303                      _client->area().width(), _client->area().height());
304
305   // map/unmap all the windows
306   if (_decorations & OBClient::Decor_Titlebar) {
307     _label.show();
308     if (tit_i)
309       _button_iconify.show();
310     else
311       _button_iconify.hide();
312     if (tit_m)
313       _button_max.show();
314     else
315       _button_max.hide();
316     if (tit_s)
317       _button_stick.show();
318     else
319       _button_stick.hide();
320     if (tit_c)
321       _button_close.show();
322     else
323       _button_close.hide();
324     _titlebar.show();
325   } else {
326     _titlebar.hide(true);
327   }
328
329   if (_decorations & OBClient::Decor_Handle)
330     _handle.show(true);
331   else
332     _handle.hide(true);
333   
334   _size.left   = _innersize.left + bwidth;
335   _size.right  = _innersize.right + bwidth;
336   _size.top    = _innersize.top + bwidth;
337   _size.bottom = _innersize.bottom + bwidth;
338
339   adjustShape();
340
341   update();
342 }
343
344
345 void OBFrame::adjustPosition()
346 {
347   int x, y;
348   clientGravity(x, y);
349   move(x, y);
350 }
351
352
353 void OBFrame::adjustShape()
354 {
355 #ifdef SHAPE
356   int bwidth = (_decorations & OBClient::Decor_Border) ?
357     _style->getBorderWidth() : 0;
358   
359   if (!_client->shaped()) {
360     // clear the shape on the frame window
361     XShapeCombineMask(otk::OBDisplay::display, window(), ShapeBounding,
362                       _innersize.left,
363                       _innersize.top,
364                       None, ShapeSet);
365   } else {
366     // make the frame's shape match the clients
367     XShapeCombineShape(otk::OBDisplay::display, window(), ShapeBounding,
368                        _innersize.left,
369                        _innersize.top,
370                        _client->window(), ShapeBounding, ShapeSet);
371
372     int num = 0;
373     XRectangle xrect[2];
374
375     if (_decorations & OBClient::Decor_Titlebar) {
376       xrect[0].x = _titlebar.rect().x();
377       xrect[0].y = _titlebar.rect().y();
378       xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
379       xrect[0].height = _titlebar.height() + bwidth * 2;
380       ++num;
381     }
382
383     if (_decorations & OBClient::Decor_Handle) {
384       xrect[1].x = _handle.rect().x();
385       xrect[1].y = _handle.rect().y();
386       xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
387       xrect[1].height = _handle.height() + bwidth * 2;
388       ++num;
389     }
390
391     XShapeCombineRectangles(otk::OBDisplay::display, window(),
392                             ShapeBounding, 0, 0, xrect, num,
393                             ShapeUnion, Unsorted);
394   }
395 #endif // SHAPE
396 }
397
398
399 void OBFrame::grabClient()
400 {
401   
402   // reparent the client to the frame
403   XReparentWindow(otk::OBDisplay::display, _client->window(),
404                   _plate.window(), 0, 0);
405   _client->ignore_unmaps++;
406
407   // select the event mask on the client's parent (to receive config req's)
408   XSelectInput(otk::OBDisplay::display, _plate.window(),
409                SubstructureRedirectMask);
410
411   // map the client so it maps when the frame does
412   XMapWindow(otk::OBDisplay::display, _client->window());
413
414   adjustSize();
415   adjustPosition();
416 }
417
418
419 void OBFrame::releaseClient(bool remap)
420 {
421   // check if the app has already reparented its window to the root window
422   XEvent ev;
423   if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
424                              ReparentNotify, &ev)) {
425     remap = true; // XXX: why do we remap the window if they already
426                   // reparented to root?
427   } else {
428     // according to the ICCCM - if the client doesn't reparent to
429     // root, then we have to do it for them
430     XReparentWindow(otk::OBDisplay::display, _client->window(),
431                     _screen->rootWindow(),
432                     _client->area().x(), _client->area().y());
433   }
434
435   // if we want to remap the window, do so now
436   if (remap)
437     XMapWindow(otk::OBDisplay::display, _client->window());
438 }
439
440
441 void OBFrame::clientGravity(int &x, int &y)
442 {
443   x = _client->area().x();
444   y = _client->area().y();
445
446   // horizontal
447   switch (_client->gravity()) {
448   default:
449   case NorthWestGravity:
450   case SouthWestGravity:
451   case WestGravity:
452     break;
453
454   case NorthGravity:
455   case SouthGravity:
456   case CenterGravity:
457     x -= (_size.left + _size.right) / 2;
458     break;
459
460   case NorthEastGravity:
461   case SouthEastGravity:
462   case EastGravity:
463     x -= _size.left + _size.right;
464     break;
465
466   case ForgetGravity:
467   case StaticGravity:
468     x -= _size.left;
469     break;
470   }
471
472   // vertical
473   switch (_client->gravity()) {
474   default:
475   case NorthWestGravity:
476   case NorthEastGravity:
477   case NorthGravity:
478     break;
479
480   case CenterGravity:
481   case EastGravity:
482   case WestGravity:
483     y -= (_size.top + _size.bottom) / 2;
484     break;
485
486   case SouthWestGravity:
487   case SouthEastGravity:
488   case SouthGravity:
489     y -= _size.top + _size.bottom;
490     break;
491
492   case ForgetGravity:
493   case StaticGravity:
494     y -= _size.top;
495     break;
496   }
497 }
498
499
500 void OBFrame::frameGravity(int &x, int &y)
501 {
502   x = rect().x();
503   y = rect().y();
504   
505   // horizontal
506   switch (_client->gravity()) {
507   default:
508   case NorthWestGravity:
509   case WestGravity:
510   case SouthWestGravity:
511     break;
512   case NorthGravity:
513   case CenterGravity:
514   case SouthGravity:
515     x += (_size.left + _size.right) / 2;
516     break;
517   case NorthEastGravity:
518   case EastGravity:
519   case SouthEastGravity:
520     x += _size.left + _size.right;
521     break;
522   case StaticGravity:
523   case ForgetGravity:
524     x += _size.left;
525     break;
526   }
527
528   // vertical
529   switch (_client->gravity()) {
530   default:
531   case NorthWestGravity:
532   case WestGravity:
533   case SouthWestGravity:
534     break;
535   case NorthGravity:
536   case CenterGravity:
537   case SouthGravity:
538     y += (_size.top + _size.bottom) / 2;
539     break;
540   case NorthEastGravity:
541   case EastGravity:
542   case SouthEastGravity:
543     y += _size.top + _size.bottom;
544     break;
545   case StaticGravity:
546   case ForgetGravity:
547     y += _size.top;
548     break;
549   }
550 }
551
552
553 }