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