]> icculus.org git repositories - mikachu/openbox.git/blob - src/frame.cc
you can bind buttons already about, and itll replace them
[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 "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   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 = "SLIMC"; // XXX: get this from somewhere
185     // XXX: it is REQUIRED that by this point, the string only has one of each
186     // possible letter, all of the letters are valid, and L exists somewhere in
187     // the string!
188
189     // the size of the label. this ASSUMES the layout has only buttons other
190     // that the ONE LABEL!!
191     // adds an extra sep so that there's a space on either side of the
192     // titlebar.. note: x = sep, below.
193     int lwidth = width - sep * 2 -
194       (_button_iconify.width() + sep) * (layout.size() - 1);
195     // quick sanity check for really small windows. if this is needed, its
196     // obviously not going to be displayed right...
197     // XXX: maybe we should make this look better somehow? constraints?
198     if (lwidth <= 0) lwidth = 1;
199     _label.setWidth(lwidth);
200
201     int x = sep;
202     for (int i = 0, len = layout.size(); i < len; ++i) {
203       switch (layout[i]) {
204       case 'I':
205         _button_iconify.move(x, _button_iconify.rect().y());
206         x += _button_iconify.width();
207         break;
208       case 'L':
209         _label.move(x, _label.rect().y());
210         x += _label.width();
211         break;
212       case 'M':
213         _button_max.move(x, _button_max.rect().y());
214         x += _button_max.width();
215         break;
216       case 'S':
217         _button_stick.move(x, _button_stick.rect().y());
218         x += _button_stick.width();
219         break;
220       case 'C':
221         _button_close.move(x, _button_close.rect().y());
222         x += _button_close.width();
223         break;
224       default:
225         assert(false); // the layout string is invalid!
226       }
227       x += sep;
228     }
229   }
230
231   if (_decorations & OBClient::Decor_Handle) {
232     _handle.setGeometry(-bwidth,
233                         _innersize.top + _client->area().height() + cbwidth,
234                         width, _style->getHandleWidth());
235     _grip_left.setGeometry(-bwidth,
236                            -bwidth,
237                            // XXX: get a Point class in otk and use that for
238                            // the 'buttons size' since theyre all the same
239                            _button_iconify.width() * 2,
240                            _handle.height());
241     _grip_right.setGeometry(((_handle.rect().right() + 1) -
242                              _button_iconify.width() * 2),
243                             -bwidth,
244                             // XXX: get a Point class in otk and use that for
245                             // the 'buttons size' since theyre all the same
246                             _button_iconify.width() * 2,
247                             _handle.height());
248     _innersize.bottom += _handle.height() + bwidth;
249   }
250   
251
252   // position/size all the windows
253
254   resize(_innersize.left + _innersize.right + _client->area().width(),
255          _innersize.top + _innersize.bottom + _client->area().height());
256
257   _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
258                      _client->area().width(), _client->area().height());
259
260   // map/unmap all the windows
261   if (_decorations & OBClient::Decor_Titlebar) {
262     _label.show();
263     if (_decorations & OBClient::Decor_Iconify)
264       _button_iconify.show();
265     else
266       _button_iconify.hide();
267     if (_decorations & OBClient::Decor_Maximize)
268       _button_max.show();
269     else
270       _button_max.hide();
271     if (_decorations & OBClient::Decor_Sticky)
272       _button_stick.show();
273     else
274       _button_stick.hide();
275     if (_decorations & OBClient::Decor_Close)
276       _button_close.show();
277     else
278       _button_close.hide();
279     _titlebar.show();
280   } else {
281     _titlebar.hide(true);
282   }
283
284   if (_decorations & OBClient::Decor_Handle)
285     _handle.show(true);
286   else
287     _handle.hide(true);
288   
289   _size.left   = _innersize.left + bwidth;
290   _size.right  = _innersize.right + bwidth;
291   _size.top    = _innersize.top + bwidth;
292   _size.bottom = _innersize.bottom + bwidth;
293
294   adjustShape();
295
296   update();
297 }
298
299
300 void OBFrame::adjustPosition()
301 {
302   int x, y;
303   clientGravity(x, y);
304   move(x, y);
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, window(), 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, window(), 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.rect().x();
332       xrect[0].y = _titlebar.rect().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.rect().x();
340       xrect[1].y = _handle.rect().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, window(),
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.window(), 0, 0);
360   _client->ignore_unmaps++;
361
362   // select the event mask on the client's parent (to receive config req's)
363   XSelectInput(otk::OBDisplay::display, _plate.window(),
364                SubstructureRedirectMask);
365
366   // map the client so it maps when the frame does
367   XMapWindow(otk::OBDisplay::display, _client->window());
368
369   adjustSize();
370   adjustPosition();
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->rootWindow(),
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::clientGravity(int &x, int &y)
397 {
398   x = _client->area().x();
399   y = _client->area().y();
400
401   // horizontal
402   switch (_client->gravity()) {
403   default:
404   case NorthWestGravity:
405   case SouthWestGravity:
406   case WestGravity:
407     break;
408
409   case NorthGravity:
410   case SouthGravity:
411   case CenterGravity:
412     x -= (_size.left + _size.right) / 2;
413     break;
414
415   case NorthEastGravity:
416   case SouthEastGravity:
417   case EastGravity:
418     x -= _size.left + _size.right;
419     break;
420
421   case ForgetGravity:
422   case StaticGravity:
423     x -= _size.left;
424     break;
425   }
426
427   // vertical
428   switch (_client->gravity()) {
429   default:
430   case NorthWestGravity:
431   case NorthEastGravity:
432   case NorthGravity:
433     break;
434
435   case CenterGravity:
436   case EastGravity:
437   case WestGravity:
438     y -= (_size.top + _size.bottom) / 2;
439     break;
440
441   case SouthWestGravity:
442   case SouthEastGravity:
443   case SouthGravity:
444     y -= _size.top + _size.bottom;
445     break;
446
447   case ForgetGravity:
448   case StaticGravity:
449     y -= _size.top;
450     break;
451   }
452 }
453
454
455 void OBFrame::frameGravity(int &x, int &y)
456 {
457   x = rect().x();
458   y = rect().y();
459   
460   // horizontal
461   switch (_client->gravity()) {
462   default:
463   case NorthWestGravity:
464   case WestGravity:
465   case SouthWestGravity:
466     break;
467   case NorthGravity:
468   case CenterGravity:
469   case SouthGravity:
470     x += (_size.left + _size.right) / 2;
471     break;
472   case NorthEastGravity:
473   case EastGravity:
474   case SouthEastGravity:
475     x += _size.left + _size.right;
476     break;
477   case StaticGravity:
478   case ForgetGravity:
479     x += _size.left;
480     break;
481   }
482
483   // vertical
484   switch (_client->gravity()) {
485   default:
486   case NorthWestGravity:
487   case WestGravity:
488   case SouthWestGravity:
489     break;
490   case NorthGravity:
491   case CenterGravity:
492   case SouthGravity:
493     y += (_size.top + _size.bottom) / 2;
494     break;
495   case NorthEastGravity:
496   case EastGravity:
497   case SouthEastGravity:
498     y += _size.top + _size.bottom;
499     break;
500   case StaticGravity:
501   case ForgetGravity:
502     y += _size.top;
503     break;
504   }
505 }
506
507
508 }