]> icculus.org git repositories - mikachu/openbox.git/blob - src/frame.cc
apply gravity when positioning the frame
[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   // inside this function _size is the size EXCLUDING the outer border
143   // at the end of this function it becomes the size INCLUDING the outer border
144   _size.left = _size.top = _size.bottom = _size.right = cbwidth;
145   width = _client->area().width() + cbwidth * 2;
146
147   XSetWindowBorderWidth(otk::OBDisplay::display, _plate.getWindow(), cbwidth);
148
149   XSetWindowBorderWidth(otk::OBDisplay::display, getWindow(), bwidth);
150   XSetWindowBorderWidth(otk::OBDisplay::display, _titlebar.getWindow(),
151                         bwidth);
152   XSetWindowBorderWidth(otk::OBDisplay::display, _grip_left.getWindow(),
153                         bwidth);
154   XSetWindowBorderWidth(otk::OBDisplay::display, _grip_right.getWindow(),
155                         bwidth);
156   XSetWindowBorderWidth(otk::OBDisplay::display, _handle.getWindow(), bwidth);
157
158   if (_decorations & OBClient::Decor_Titlebar) {
159     // set the titlebar size
160     _titlebar.setGeometry(-bwidth,
161                           -bwidth,
162                           width,
163                           (_style->getFont().height() +
164                            _style->getBevelWidth() * 2));
165     _size.top += _titlebar.height() + bwidth;
166
167     // set the label size
168     _label.setGeometry(0, _style->getBevelWidth(),
169                        width, _style->getFont().height());
170     // set the buttons sizes
171     if (_decorations & OBClient::Decor_Iconify)
172       _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
173                                   _label.height() - 2,
174                                   _label.height() - 2);
175     if (_decorations & OBClient::Decor_Maximize)
176       _button_max.setGeometry(0, _style->getBevelWidth() + 1,
177                               _label.height() - 2,
178                               _label.height() - 2);
179     if (_decorations & OBClient::Decor_Sticky)
180       _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
181                                 _label.height() - 2,
182                                 _label.height() - 2);
183     if (_decorations & OBClient::Decor_Close)
184       _button_close.setGeometry(0, _style->getBevelWidth() + 1,
185                                 _label.height() - 2,
186                                 _label.height() - 2);
187
188     // separation between titlebar elements
189     const int sep = _style->getBevelWidth() + 1;
190
191     std::string layout = "SLIMC"; // XXX: get this from somewhere
192     // XXX: it is REQUIRED that by this point, the string only has one of each
193     // possible letter, all of the letters are valid, and L exists somewhere in
194     // the string!
195
196     // the size of the label. this ASSUMES the layout has only buttons other
197     // that the ONE LABEL!!
198     // adds an extra sep so that there's a space on either side of the
199     // titlebar.. note: x = sep, below.
200     _label.setWidth(width - sep * 2 - 
201                     (_button_iconify.width() + sep) * (layout.size() - 1));
202
203     int x = sep;
204     for (int i = 0, len = layout.size(); i < len; ++i) {
205       switch (layout[i]) {
206       case 'I':
207         _button_iconify.move(x, _button_iconify.getRect().y());
208         x += _button_iconify.width();
209         break;
210       case 'L':
211         _label.move(x, _label.getRect().y());
212         x += _label.width();
213         break;
214       case 'M':
215         _button_max.move(x, _button_max.getRect().y());
216         x += _button_max.width();
217         break;
218       case 'S':
219         _button_stick.move(x, _button_stick.getRect().y());
220         x += _button_stick.width();
221         break;
222       case 'C':
223         _button_close.move(x, _button_close.getRect().y());
224         x += _button_close.width();
225         break;
226       default:
227         assert(false); // the layout string is invalid!
228       }
229       x += sep;
230     }
231   }
232
233   if (_decorations & OBClient::Decor_Handle) {
234     _handle.setGeometry(-bwidth,
235                         _size.top + _client->area().height() + cbwidth,
236                         width, _style->getHandleWidth());
237     _grip_left.setGeometry(-bwidth,
238                            -bwidth,
239                            // XXX: get a Point class in otk and use that for
240                            // the 'buttons size' since theyre all the same
241                            _button_iconify.width() * 2,
242                            _handle.height());
243     _grip_right.setGeometry(((_handle.getRect().right() + 1) -
244                              _button_iconify.width() * 2),
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     _size.bottom += _handle.height() + bwidth;
251   }
252   
253
254   // position/size all the windows
255
256   resize(_size.left + _size.right + _client->area().width(),
257          _size.top + _size.bottom + _client->area().height());
258
259   _plate.setGeometry(_size.left - cbwidth, _size.top - cbwidth,
260                      _client->area().width(), _client->area().height());
261
262   // map/unmap all the windows
263   if (_decorations & OBClient::Decor_Titlebar) {
264     _label.show();
265     if (_decorations & OBClient::Decor_Iconify)
266       _button_iconify.show();
267     else
268       _button_iconify.hide();
269     if (_decorations & OBClient::Decor_Maximize)
270       _button_max.show();
271     else
272       _button_max.hide();
273     if (_decorations & OBClient::Decor_Sticky)
274       _button_stick.show();
275     else
276       _button_stick.hide();
277     if (_decorations & OBClient::Decor_Close)
278       _button_close.show();
279     else
280       _button_close.hide();
281     _titlebar.show();
282   } else {
283     _titlebar.hide(true);
284   }
285
286   if (_decorations & OBClient::Decor_Handle)
287     _handle.show(true);
288   else
289     _handle.hide(true);
290   
291   // inside this function _size is the size EXCLUDING the outer border
292   // at the end of this function it becomes the size INCLUDING the outer border
293   _size.left += bwidth;
294   _size.right += bwidth;
295   _size.top += bwidth;
296   _size.bottom += bwidth;
297
298   // XXX: more is gunna have to happen here
299
300   adjustShape();
301
302   update();
303 }
304
305
306 void OBFrame::adjustShape()
307 {
308 #ifdef SHAPE
309   if (!_client->shaped()) {
310     // clear the shape on the frame window
311     XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
312                       _size.left,
313                       _size.top,
314                       None, ShapeSet);
315   } else {
316     // make the frame's shape match the clients
317     XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
318                        _size.left,
319                        _size.top,
320                        _client->window(), ShapeBounding, ShapeSet);
321
322   int num = 0;
323     XRectangle xrect[2];
324
325     /*
326     if (decorations & Decor_Titlebar) {
327     xrect[0].x = xrect[0].y = -frame.border_w;
328     xrect[0].width = frame.rect.width();
329     xrect[0].height = frame.title_h + (frame.border_w * 2);
330     ++num;
331     }
332
333     if (decorations & Decor_Handle) {
334     xrect[1].x = -frame.border_w;
335     xrect[1].y = frame.rect.height() - frame.margin.bottom +
336     frame.mwm_border_w - frame.border_w;
337     xrect[1].width = frame.rect.width();
338     xrect[1].height = frame.handle_h + (frame.border_w * 2);
339     ++num;
340     }*/
341
342     XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
343                             ShapeBounding, 0, 0, xrect, num,
344                             ShapeUnion, Unsorted);
345   }
346 #endif // SHAPE
347 }
348
349
350 void OBFrame::grabClient()
351 {
352   
353   // select the event mask on the frame
354   //XSelectInput(otk::OBDisplay::display, _window, SubstructureRedirectMask);
355
356   // reparent the client to the frame
357   XReparentWindow(otk::OBDisplay::display, _client->window(),
358                   _plate.getWindow(), 0, 0);
359   _client->ignore_unmaps++;
360
361   // raise the client above the frame
362   //XRaiseWindow(otk::OBDisplay::display, _client->window());
363   // map the client so it maps when the frame does
364   XMapWindow(otk::OBDisplay::display, _client->window());
365
366   adjust();
367   applyGravity();
368 }
369
370
371 void OBFrame::releaseClient(bool remap)
372 {
373   // check if the app has already reparented its window to the root window
374   XEvent ev;
375   if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
376                              ReparentNotify, &ev)) {
377     remap = true; // XXX: why do we remap the window if they already
378                   // reparented to root?
379   } else {
380     // according to the ICCCM - if the client doesn't reparent to
381     // root, then we have to do it for them
382     XReparentWindow(otk::OBDisplay::display, _client->window(),
383                     _screen->getRootWindow(),
384                     _client->area().x(), _client->area().y());
385   }
386
387   // if we want to remap the window, do so now
388   if (remap)
389     XMapWindow(otk::OBDisplay::display, _client->window());
390 }
391
392
393 void OBFrame::applyGravity()
394 {
395   int x, y;
396   // apply horizontal window gravity
397   switch (_client->gravity()) {
398   default:
399   case NorthWestGravity:
400   case SouthWestGravity:
401   case WestGravity:
402     x = _client->area().x();
403     break;
404
405   case NorthGravity:
406   case SouthGravity:
407   case CenterGravity:
408     x = _client->area().x() - (_size.left + _size.right) / 2;
409     break;
410
411   case NorthEastGravity:
412   case SouthEastGravity:
413   case EastGravity:
414     x = _client->area().x() - _size.left - _size.right + 2;
415     break;
416
417   case ForgetGravity:
418   case StaticGravity:
419     x = _client->area().x() - _size.left;
420     break;
421   }
422
423   // apply vertical window gravity
424   switch (_client->gravity()) {
425   default:
426   case NorthWestGravity:
427   case NorthEastGravity:
428   case NorthGravity:
429     y = _client->area().y();
430     break;
431
432   case CenterGravity:
433   case EastGravity:
434   case WestGravity:
435     y = _client->area().y() - (_size.top + _size.bottom) / 2;
436     break;
437
438   case SouthWestGravity:
439   case SouthEastGravity:
440   case SouthGravity:
441     y = _client->area().y() - _size.top - _size.bottom + 2;
442     break;
443
444   case ForgetGravity:
445   case StaticGravity:
446     y = _client->area().y() - _size.top;
447     break;
448   }
449   move(x, y);
450 }
451
452
453 }