]> icculus.org git repositories - mikachu/openbox.git/blob - src/frame.cc
add an OBBackgroundWidget and use it for setting colors so far.
[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     _client(client),
27     _screen(otk::OBDisplay::screenInfo(client->screen())),
28     _plate(this, OBWidget::Type_Plate),
29     _titlebar(this, OBWidget::Type_Titlebar),
30     _button_close(&_titlebar),
31     _button_iconify(&_titlebar),
32     _button_max(&_titlebar),
33     _button_stick(&_titlebar),
34     _label(&_titlebar),
35     _handle(this, OBWidget::Type_Handle),
36     _grip_left(&_handle),
37     _grip_right(&_handle),
38     _decorations(client->decorations())
39 {
40   assert(client);
41   assert(style);
42
43   XSelectInput(otk::OBDisplay::display, getWindow(), OBFrame::event_mask);
44   
45   unmanaged();
46   _titlebar.unmanaged();
47   _button_close.unmanaged();
48   _button_iconify.unmanaged();
49   _button_max.unmanaged();
50   _button_stick.unmanaged();
51   _label.unmanaged();
52   _handle.unmanaged();
53   _grip_left.unmanaged();
54   _grip_right.unmanaged();
55   _plate.unmanaged();
56
57   _grip_left.setCursor(Openbox::instance->cursors().ll_angle);
58   _grip_right.setCursor(Openbox::instance->cursors().lr_angle);
59   
60   _button_close.setText("X");
61   _button_iconify.setText("I");
62   _button_max.setText("M");
63   _button_stick.setText("S");
64   _label.setText(_client->title());
65
66   _style = 0;
67   setStyle(style);
68
69   //XXX: uncomment me unfocus(); // stuff starts out focused in otk
70   
71   _plate.show(); // the other stuff is shown based on decor settings
72   
73   grabClient();
74 }
75
76
77 OBFrame::~OBFrame()
78 {
79   releaseClient(false);
80 }
81
82
83 void OBFrame::setStyle(otk::Style *style)
84 {
85   assert(style);
86
87   otk::OtkWidget::setStyle(style);
88   // set the grips' textures
89   _grip_left.setTexture(style->getGripFocus());
90   _grip_left.setUnfocusTexture(style->getGripUnfocus());
91   _grip_left.setPressedFocusTexture(style->getGripFocus());
92   _grip_left.setPressedUnfocusTexture(style->getGripUnfocus());
93   _grip_right.setTexture(style->getGripFocus());
94   _grip_right.setUnfocusTexture(style->getGripUnfocus());
95   _grip_right.setPressedFocusTexture(style->getGripFocus());
96   _grip_right.setPressedUnfocusTexture(style->getGripUnfocus());
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   }
105   
106   _style = style;
107
108   // XXX: change when focus changes!
109   XSetWindowBorder(otk::OBDisplay::display, getWindow(),
110                    _style->getBorderColor()->pixel());
111   XSetWindowBorder(otk::OBDisplay::display, _grip_left.getWindow(),
112                    _style->getBorderColor()->pixel());
113   XSetWindowBorder(otk::OBDisplay::display, _grip_right.getWindow(),
114                    _style->getBorderColor()->pixel());
115
116   // if !replace, then adjust() will get called after the client is grabbed!
117   if (replace) {
118     // size/position everything
119     adjustSize();
120     adjustPosition();
121   }
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   
135   if (_decorations & OBClient::Decor_Border) {
136     bwidth = _style->getBorderWidth();
137     cbwidth = _style->getFrameWidth();
138   } else
139     bwidth = cbwidth = 0;
140   _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
141     cbwidth;
142   width = _client->area().width() + cbwidth * 2;
143
144   _plate.setBorderWidth(cbwidth);
145
146   setBorderWidth(bwidth);
147   _titlebar.setBorderWidth(bwidth);
148   _grip_left.setBorderWidth(bwidth);
149   _grip_right.setBorderWidth(bwidth);
150   _handle.setBorderWidth(bwidth);
151   
152   if (_decorations & OBClient::Decor_Titlebar) {
153     // set the titlebar size
154     _titlebar.setGeometry(-bwidth,
155                           -bwidth,
156                           width,
157                           (_style->getFont().height() +
158                            _style->getBevelWidth() * 2));
159     _innersize.top += _titlebar.height() + bwidth;
160
161     // set the label size
162     _label.setGeometry(0, _style->getBevelWidth(),
163                        width, _style->getFont().height());
164     // set the buttons sizes
165     if (_decorations & OBClient::Decor_Iconify)
166       _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
167                                   _label.height() - 2,
168                                   _label.height() - 2);
169     if (_decorations & OBClient::Decor_Maximize)
170       _button_max.setGeometry(0, _style->getBevelWidth() + 1,
171                               _label.height() - 2,
172                               _label.height() - 2);
173     if (_decorations & OBClient::Decor_Sticky)
174       _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
175                                 _label.height() - 2,
176                                 _label.height() - 2);
177     if (_decorations & OBClient::Decor_Close)
178       _button_close.setGeometry(0, _style->getBevelWidth() + 1,
179                                 _label.height() - 2,
180                                 _label.height() - 2);
181
182     // separation between titlebar elements
183     const int sep = _style->getBevelWidth() + 1;
184
185     std::string layout = "SLIMC"; // XXX: get this from somewhere
186     // XXX: it is REQUIRED that by this point, the string only has one of each
187     // possible letter, all of the letters are valid, and L exists somewhere in
188     // the string!
189
190     // the size of the label. this ASSUMES the layout has only buttons other
191     // that the ONE LABEL!!
192     // adds an extra sep so that there's a space on either side of the
193     // titlebar.. note: x = sep, below.
194     int lwidth = width - sep * 2 -
195       (_button_iconify.width() + sep) * (layout.size() - 1);
196     // quick sanity check for really small windows. if this is needed, its
197     // obviously not going to be displayed right...
198     // XXX: maybe we should make this look better somehow? constraints?
199     if (lwidth <= 0) lwidth = 1;
200     _label.setWidth(lwidth);
201
202     int x = sep;
203     for (int i = 0, len = layout.size(); i < len; ++i) {
204       switch (layout[i]) {
205       case 'I':
206         _button_iconify.move(x, _button_iconify.getRect().y());
207         x += _button_iconify.width();
208         break;
209       case 'L':
210         _label.move(x, _label.getRect().y());
211         x += _label.width();
212         break;
213       case 'M':
214         _button_max.move(x, _button_max.getRect().y());
215         x += _button_max.width();
216         break;
217       case 'S':
218         _button_stick.move(x, _button_stick.getRect().y());
219         x += _button_stick.width();
220         break;
221       case 'C':
222         _button_close.move(x, _button_close.getRect().y());
223         x += _button_close.width();
224         break;
225       default:
226         assert(false); // the layout string is invalid!
227       }
228       x += sep;
229     }
230   }
231
232   if (_decorations & OBClient::Decor_Handle) {
233     _handle.setGeometry(-bwidth,
234                         _innersize.top + _client->area().height() + cbwidth,
235                         width, _style->getHandleWidth());
236     _grip_left.setGeometry(-bwidth,
237                            -bwidth,
238                            // XXX: get a Point class in otk and use that for
239                            // the 'buttons size' since theyre all the same
240                            _button_iconify.width() * 2,
241                            _handle.height());
242     _grip_right.setGeometry(((_handle.getRect().right() + 1) -
243                              _button_iconify.width() * 2),
244                             -bwidth,
245                             // XXX: get a Point class in otk and use that for
246                             // the 'buttons size' since theyre all the same
247                             _button_iconify.width() * 2,
248                             _handle.height());
249     _innersize.bottom += _handle.height() + bwidth;
250   }
251   
252
253   // position/size all the windows
254
255   resize(_innersize.left + _innersize.right + _client->area().width(),
256          _innersize.top + _innersize.bottom + _client->area().height());
257
258   _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
259                      _client->area().width(), _client->area().height());
260
261   // map/unmap all the windows
262   if (_decorations & OBClient::Decor_Titlebar) {
263     _label.show();
264     if (_decorations & OBClient::Decor_Iconify)
265       _button_iconify.show();
266     else
267       _button_iconify.hide();
268     if (_decorations & OBClient::Decor_Maximize)
269       _button_max.show();
270     else
271       _button_max.hide();
272     if (_decorations & OBClient::Decor_Sticky)
273       _button_stick.show();
274     else
275       _button_stick.hide();
276     if (_decorations & OBClient::Decor_Close)
277       _button_close.show();
278     else
279       _button_close.hide();
280     _titlebar.show();
281   } else {
282     _titlebar.hide(true);
283   }
284
285   if (_decorations & OBClient::Decor_Handle)
286     _handle.show(true);
287   else
288     _handle.hide(true);
289   
290   // XXX: more is gunna have to happen here
291
292   _size.left   = _innersize.left + bwidth;
293   _size.right  = _innersize.right + bwidth;
294   _size.top    = _innersize.top + bwidth;
295   _size.bottom = _innersize.bottom + bwidth;
296
297   adjustShape();
298
299   update();
300 }
301
302
303 void OBFrame::adjustPosition()
304 {
305   int x, y;
306   clientGravity(x, y);
307   move(x, y);
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 (to receive config req's)
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   adjustSize();
373   adjustPosition();
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::clientGravity(int &x, int &y)
400 {
401   x = _client->area().x();
402   y = _client->area().y();
403
404   // horizontal
405   switch (_client->gravity()) {
406   default:
407   case NorthWestGravity:
408   case SouthWestGravity:
409   case WestGravity:
410     break;
411
412   case NorthGravity:
413   case SouthGravity:
414   case CenterGravity:
415     x -= (_size.left + _size.right) / 2;
416     break;
417
418   case NorthEastGravity:
419   case SouthEastGravity:
420   case EastGravity:
421     x -= _size.left + _size.right;
422     break;
423
424   case ForgetGravity:
425   case StaticGravity:
426     x -= _size.left;
427     break;
428   }
429
430   // vertical
431   switch (_client->gravity()) {
432   default:
433   case NorthWestGravity:
434   case NorthEastGravity:
435   case NorthGravity:
436     break;
437
438   case CenterGravity:
439   case EastGravity:
440   case WestGravity:
441     y -= (_size.top + _size.bottom) / 2;
442     break;
443
444   case SouthWestGravity:
445   case SouthEastGravity:
446   case SouthGravity:
447     y -= _size.top + _size.bottom;
448     break;
449
450   case ForgetGravity:
451   case StaticGravity:
452     y -= _size.top;
453     break;
454   }
455 }
456
457
458 void OBFrame::frameGravity(int &x, int &y)
459 {
460   x = getRect().x();
461   y = getRect().y();
462   
463   // horizontal
464   switch (_client->gravity()) {
465   default:
466   case NorthWestGravity:
467   case WestGravity:
468   case SouthWestGravity:
469     break;
470   case NorthGravity:
471   case CenterGravity:
472   case SouthGravity:
473     x += (_size.left + _size.right) / 2;
474     break;
475   case NorthEastGravity:
476   case EastGravity:
477   case SouthEastGravity:
478     x += _size.left + _size.right;
479     break;
480   case StaticGravity:
481   case ForgetGravity:
482     x += _size.left;
483     break;
484   }
485
486   // vertical
487   switch (_client->gravity()) {
488   default:
489   case NorthWestGravity:
490   case WestGravity:
491   case SouthWestGravity:
492     break;
493   case NorthGravity:
494   case CenterGravity:
495   case SouthGravity:
496     y += (_size.top + _size.bottom) / 2;
497     break;
498   case NorthEastGravity:
499   case EastGravity:
500   case SouthEastGravity:
501     y += _size.top + _size.bottom;
502     break;
503   case StaticGravity:
504   case ForgetGravity:
505     y += _size.top;
506     break;
507   }
508 }
509
510
511 }