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