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