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