]> icculus.org git repositories - dana/openbox.git/blob - src/frame.cc
enter/leave and focus events are working beautifully
[dana/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 }
120
121
122 void OBFrame::adjustSize()
123 {
124   // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
125   _decorations = _client->decorations();
126   _decorations = 0xffffffff;
127   
128   int width;   // the width of the client and its border
129   int bwidth;  // width to make borders
130   int cbwidth; // width of the inner client border
131   
132   if (_decorations & OBClient::Decor_Border) {
133     bwidth = _style->getBorderWidth();
134     cbwidth = _style->getFrameWidth();
135   } else
136     bwidth = cbwidth = 0;
137   _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
138     cbwidth;
139   width = _client->area().width() + cbwidth * 2;
140
141   _plate.setBorderWidth(cbwidth);
142
143   setBorderWidth(bwidth);
144   _titlebar.setBorderWidth(bwidth);
145   _grip_left.setBorderWidth(bwidth);
146   _grip_right.setBorderWidth(bwidth);
147   _handle.setBorderWidth(bwidth);
148   
149   if (_decorations & OBClient::Decor_Titlebar) {
150     // set the titlebar size
151     _titlebar.setGeometry(-bwidth,
152                           -bwidth,
153                           width,
154                           (_style->getFont()->height() +
155                            _style->getBevelWidth() * 2));
156     _innersize.top += _titlebar.height() + bwidth;
157
158     // set the label size
159     _label.setGeometry(0, _style->getBevelWidth(),
160                        width, _style->getFont()->height());
161     // set the buttons sizes
162     if (_decorations & OBClient::Decor_Iconify)
163       _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
164                                   _label.height() - 2,
165                                   _label.height() - 2);
166     if (_decorations & OBClient::Decor_Maximize)
167       _button_max.setGeometry(0, _style->getBevelWidth() + 1,
168                               _label.height() - 2,
169                               _label.height() - 2);
170     if (_decorations & OBClient::Decor_Sticky)
171       _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
172                                 _label.height() - 2,
173                                 _label.height() - 2);
174     if (_decorations & OBClient::Decor_Close)
175       _button_close.setGeometry(0, _style->getBevelWidth() + 1,
176                                 _label.height() - 2,
177                                 _label.height() - 2);
178
179     // separation between titlebar elements
180     const int sep = _style->getBevelWidth() + 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   // XXX: more is gunna have to happen here
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 }