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