1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
9 #include <X11/extensions/shape.h>
17 #include "bindings.hh"
18 #include "otk/display.hh"
24 const long OBFrame::event_mask;
26 OBFrame::OBFrame(OBClient *client, otk::Style *style)
27 : otk::OtkWidget(Openbox::instance, style),
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())
46 XSelectInput(otk::OBDisplay::display, window(), OBFrame::event_mask);
48 _grip_left.setCursor(Openbox::instance->cursors().ll_angle);
49 _grip_right.setCursor(Openbox::instance->cursors().lr_angle);
51 _label.setText(_client->title());
56 otk::OtkWidget::unfocus(); // stuff starts out appearing focused in otk
58 _plate.show(); // the other stuff is shown based on decor settings
70 void OBFrame::setTitle(const std::string &text)
77 void OBFrame::setStyle(otk::Style *style)
81 otk::OtkWidget::setStyle(style);
83 // if a style was previously set, then 'replace' is true, cause we're
85 bool replace = (_style);
88 // XXX: do shit here whatever
93 setBorderColor(_style->getBorderColor());
95 // if !replace, then adjust() will get called after the client is grabbed!
97 // size/position everything
104 void OBFrame::focus()
106 otk::OtkWidget::focus();
112 void OBFrame::unfocus()
114 otk::OtkWidget::unfocus();
119 void OBFrame::adjust()
121 // the party all happens in adjustSize
125 void OBFrame::adjustSize()
127 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
128 _decorations = _client->decorations();
129 _decorations = 0xffffffff;
131 // true/false for whether to show each element of the titlebar
132 bool tit_i = false, tit_m = false, tit_s = false, tit_c = false;
133 int width; // the width of the client and its border
134 int bwidth; // width to make borders
135 int cbwidth; // width of the inner client border
136 const int bevel = _style->getBevelWidth();
138 if (_decorations & OBClient::Decor_Border) {
139 bwidth = _style->getBorderWidth();
140 cbwidth = _style->getFrameWidth();
142 bwidth = cbwidth = 0;
143 _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
145 width = _client->area().width() + cbwidth * 2;
147 _plate.setBorderWidth(cbwidth);
149 setBorderWidth(bwidth);
150 _titlebar.setBorderWidth(bwidth);
151 _grip_left.setBorderWidth(bwidth);
152 _grip_right.setBorderWidth(bwidth);
153 _handle.setBorderWidth(bwidth);
155 if (_decorations & OBClient::Decor_Titlebar) {
156 // set the titlebar size
157 _titlebar.setGeometry(-bwidth,
160 _style->getFont()->height() + bevel * 2);
161 _innersize.top += _titlebar.height() + bwidth;
163 // set the label size
164 _label.setGeometry(0, bevel, width, _style->getFont()->height());
165 // set the buttons sizes
166 if (_decorations & OBClient::Decor_Iconify)
167 _button_iconify.setGeometry(0, bevel + 1,
169 _label.height() - 2);
170 if (_decorations & OBClient::Decor_Maximize)
171 _button_max.setGeometry(0, bevel + 1,
173 _label.height() - 2);
174 if (_decorations & OBClient::Decor_Sticky)
175 _button_stick.setGeometry(0, bevel + 1,
177 _label.height() - 2);
178 if (_decorations & OBClient::Decor_Close)
179 _button_close.setGeometry(0, bevel + 1,
181 _label.height() - 2);
183 // separation between titlebar elements
184 const int sep = bevel + 1;
187 if (!python_get_string("titlebar_layout", &layout))
190 // this code ensures that the string only has one of each possible
191 // letter, all of the letters are valid, and L exists somewhere in the
195 for (std::string::size_type i = 0; i < layout.size(); ++i) {
199 if (!tit_i && (_decorations & OBClient::Decor_Iconify)) {
211 if (!tit_m && (_decorations & OBClient::Decor_Maximize)) {
217 if (!tit_s && (_decorations & OBClient::Decor_Sticky)) {
223 if (!tit_c && (_decorations & OBClient::Decor_Close)) {
228 // if we get here then we don't want the letter, kill it
229 layout.erase(i--, 1);
234 // the size of the label. this ASSUMES the layout has only buttons other
235 // that the ONE LABEL!!
236 // adds an extra sep so that there's a space on either side of the
237 // titlebar.. note: x = sep, below.
238 int lwidth = width - sep * 2 -
239 (_button_iconify.width() + sep) * (layout.size() - 1);
240 // quick sanity check for really small windows. if this is needed, its
241 // obviously not going to be displayed right...
242 // XXX: maybe we should make this look better somehow? constraints?
243 if (lwidth <= 0) lwidth = 1;
244 _label.setWidth(lwidth);
247 for (int i = 0, len = layout.size(); i < len; ++i) {
250 _button_iconify.move(x, _button_iconify.rect().y());
251 x += _button_iconify.width();
254 _label.move(x, _label.rect().y());
258 _button_max.move(x, _button_max.rect().y());
259 x += _button_max.width();
262 _button_stick.move(x, _button_stick.rect().y());
263 x += _button_stick.width();
266 _button_close.move(x, _button_close.rect().y());
267 x += _button_close.width();
270 assert(false); // the layout string is invalid!
276 if (_decorations & OBClient::Decor_Handle) {
277 _handle.setGeometry(-bwidth,
278 _innersize.top + _client->area().height() + cbwidth,
279 width, _style->getHandleWidth());
280 _grip_left.setGeometry(-bwidth,
282 // XXX: get a Point class in otk and use that for
283 // the 'buttons size' since theyre all the same
284 _button_iconify.width() * 2,
286 _grip_right.setGeometry(((_handle.rect().right() + 1) -
287 _button_iconify.width() * 2),
289 // XXX: get a Point class in otk and use that for
290 // the 'buttons size' since theyre all the same
291 _button_iconify.width() * 2,
293 _innersize.bottom += _handle.height() + bwidth;
297 // position/size all the windows
299 resize(_innersize.left + _innersize.right + _client->area().width(),
300 _innersize.top + _innersize.bottom + _client->area().height());
302 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
303 _client->area().width(), _client->area().height());
305 // map/unmap all the windows
306 if (_decorations & OBClient::Decor_Titlebar) {
309 _button_iconify.show();
311 _button_iconify.hide();
317 _button_stick.show();
319 _button_stick.hide();
321 _button_close.show();
323 _button_close.hide();
326 _titlebar.hide(true);
329 if (_decorations & OBClient::Decor_Handle)
334 _size.left = _innersize.left + bwidth;
335 _size.right = _innersize.right + bwidth;
336 _size.top = _innersize.top + bwidth;
337 _size.bottom = _innersize.bottom + bwidth;
345 void OBFrame::adjustPosition()
353 void OBFrame::adjustShape()
356 int bwidth = (_decorations & OBClient::Decor_Border) ?
357 _style->getBorderWidth() : 0;
359 if (!_client->shaped()) {
360 // clear the shape on the frame window
361 XShapeCombineMask(otk::OBDisplay::display, window(), ShapeBounding,
366 // make the frame's shape match the clients
367 XShapeCombineShape(otk::OBDisplay::display, window(), ShapeBounding,
370 _client->window(), ShapeBounding, ShapeSet);
375 if (_decorations & OBClient::Decor_Titlebar) {
376 xrect[0].x = _titlebar.rect().x();
377 xrect[0].y = _titlebar.rect().y();
378 xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
379 xrect[0].height = _titlebar.height() + bwidth * 2;
383 if (_decorations & OBClient::Decor_Handle) {
384 xrect[1].x = _handle.rect().x();
385 xrect[1].y = _handle.rect().y();
386 xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
387 xrect[1].height = _handle.height() + bwidth * 2;
391 XShapeCombineRectangles(otk::OBDisplay::display, window(),
392 ShapeBounding, 0, 0, xrect, num,
393 ShapeUnion, Unsorted);
399 void OBFrame::grabClient()
402 // reparent the client to the frame
403 XReparentWindow(otk::OBDisplay::display, _client->window(),
404 _plate.window(), 0, 0);
405 _client->ignore_unmaps++;
407 // select the event mask on the client's parent (to receive config req's)
408 XSelectInput(otk::OBDisplay::display, _plate.window(),
409 SubstructureRedirectMask);
411 // map the client so it maps when the frame does
412 XMapWindow(otk::OBDisplay::display, _client->window());
419 void OBFrame::releaseClient(bool remap)
421 // check if the app has already reparented its window to the root window
423 if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
424 ReparentNotify, &ev)) {
425 remap = true; // XXX: why do we remap the window if they already
426 // reparented to root?
428 // according to the ICCCM - if the client doesn't reparent to
429 // root, then we have to do it for them
430 XReparentWindow(otk::OBDisplay::display, _client->window(),
431 _screen->rootWindow(),
432 _client->area().x(), _client->area().y());
435 // if we want to remap the window, do so now
437 XMapWindow(otk::OBDisplay::display, _client->window());
441 void OBFrame::clientGravity(int &x, int &y)
443 x = _client->area().x();
444 y = _client->area().y();
447 switch (_client->gravity()) {
449 case NorthWestGravity:
450 case SouthWestGravity:
457 x -= (_size.left + _size.right) / 2;
460 case NorthEastGravity:
461 case SouthEastGravity:
463 x -= _size.left + _size.right;
473 switch (_client->gravity()) {
475 case NorthWestGravity:
476 case NorthEastGravity:
483 y -= (_size.top + _size.bottom) / 2;
486 case SouthWestGravity:
487 case SouthEastGravity:
489 y -= _size.top + _size.bottom;
500 void OBFrame::frameGravity(int &x, int &y)
506 switch (_client->gravity()) {
508 case NorthWestGravity:
510 case SouthWestGravity:
515 x += (_size.left + _size.right) / 2;
517 case NorthEastGravity:
519 case SouthEastGravity:
520 x += _size.left + _size.right;
529 switch (_client->gravity()) {
531 case NorthWestGravity:
533 case SouthWestGravity:
538 y += (_size.top + _size.bottom) / 2;
540 case NorthEastGravity:
542 case SouthEastGravity:
543 y += _size.top + _size.bottom;