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