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, Horizontal, 0, 1,
31 _screen(otk::OBDisplay::screenInfo(client->screen())),
32 _plate(this, OBWidget::Type_Plate),
33 _titlebar(this, OBWidget::Type_Titlebar),
34 _button_close(&_titlebar, OBWidget::Type_CloseButton),
35 _button_iconify(&_titlebar, OBWidget::Type_IconifyButton),
36 _button_max(&_titlebar, OBWidget::Type_MaximizeButton),
37 _button_stick(&_titlebar, OBWidget::Type_StickyButton),
38 _label(&_titlebar, OBWidget::Type_Label),
39 _handle(this, OBWidget::Type_Handle),
40 _grip_left(&_handle, OBWidget::Type_LeftGrip),
41 _grip_right(&_handle, OBWidget::Type_RightGrip),
42 _decorations(client->decorations())
47 XSelectInput(otk::OBDisplay::display, _window, OBFrame::event_mask);
49 _grip_left.setCursor(Openbox::instance->cursors().ll_angle);
50 _grip_right.setCursor(Openbox::instance->cursors().lr_angle);
52 _label.setText(_client->title());
57 otk::OtkWidget::unfocus(); // stuff starts out appearing focused in otk
59 _plate.show(); // the other stuff is shown based on decor settings
71 void OBFrame::setTitle(const std::string &text)
78 void OBFrame::setStyle(otk::Style *style)
82 otk::OtkWidget::setStyle(style);
84 // if a style was previously set, then 'replace' is true, cause we're
86 bool replace = (_style);
89 // XXX: do shit here whatever
94 setBorderColor(_style->getBorderColor());
96 // if !replace, then adjust() will get called after the client is grabbed!
98 // size/position everything
105 void OBFrame::focus()
107 otk::OtkWidget::focus();
113 void OBFrame::unfocus()
115 otk::OtkWidget::unfocus();
120 void OBFrame::adjust()
122 // the party all happens in adjustSize
126 void OBFrame::adjustSize()
128 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
129 _decorations = _client->decorations();
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 if (_client->shaded())
300 resize(_innersize.left + _innersize.right + _client->area().width(),
303 resize(_innersize.left + _innersize.right + _client->area().width(),
304 _innersize.top + _innersize.bottom + _client->area().height());
306 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
307 _client->area().width(), _client->area().height());
309 // map/unmap all the windows
310 if (_decorations & OBClient::Decor_Titlebar) {
313 _button_iconify.show();
315 _button_iconify.hide();
321 _button_stick.show();
323 _button_stick.hide();
325 _button_close.show();
327 _button_close.hide();
330 _titlebar.hide(true);
333 if (_decorations & OBClient::Decor_Handle)
338 _size.left = _innersize.left + bwidth;
339 _size.right = _innersize.right + bwidth;
340 _size.top = _innersize.top + bwidth;
341 _size.bottom = _innersize.bottom + bwidth;
349 void OBFrame::adjustPosition()
357 void OBFrame::adjustShape()
360 int bwidth = (_decorations & OBClient::Decor_Border) ?
361 _style->getBorderWidth() : 0;
363 if (!_client->shaped()) {
364 // clear the shape on the frame window
365 XShapeCombineMask(otk::OBDisplay::display, window(), ShapeBounding,
370 // make the frame's shape match the clients
371 XShapeCombineShape(otk::OBDisplay::display, window(), ShapeBounding,
374 _client->window(), ShapeBounding, ShapeSet);
379 if (_decorations & OBClient::Decor_Titlebar) {
380 xrect[0].x = _titlebar.rect().x();
381 xrect[0].y = _titlebar.rect().y();
382 xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
383 xrect[0].height = _titlebar.height() + bwidth * 2;
387 if (_decorations & OBClient::Decor_Handle) {
388 xrect[1].x = _handle.rect().x();
389 xrect[1].y = _handle.rect().y();
390 xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
391 xrect[1].height = _handle.height() + bwidth * 2;
395 XShapeCombineRectangles(otk::OBDisplay::display, window(),
396 ShapeBounding, 0, 0, xrect, num,
397 ShapeUnion, Unsorted);
403 void OBFrame::grabClient()
406 // reparent the client to the frame
407 XReparentWindow(otk::OBDisplay::display, _client->window(),
408 _plate.window(), 0, 0);
409 _client->ignore_unmaps++;
411 // select the event mask on the client's parent (to receive config req's)
412 XSelectInput(otk::OBDisplay::display, _plate.window(),
413 SubstructureRedirectMask);
415 // map the client so it maps when the frame does
416 XMapWindow(otk::OBDisplay::display, _client->window());
423 void OBFrame::releaseClient(bool remap)
425 // check if the app has already reparented its window to the root window
427 if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
428 ReparentNotify, &ev)) {
429 remap = true; // XXX: why do we remap the window if they already
430 // reparented to root?
432 // according to the ICCCM - if the client doesn't reparent to
433 // root, then we have to do it for them
434 XReparentWindow(otk::OBDisplay::display, _client->window(),
435 _screen->rootWindow(),
436 _client->area().x(), _client->area().y());
439 // if we want to remap the window, do so now
441 XMapWindow(otk::OBDisplay::display, _client->window());
445 void OBFrame::clientGravity(int &x, int &y)
447 x = _client->area().x();
448 y = _client->area().y();
451 switch (_client->gravity()) {
453 case NorthWestGravity:
454 case SouthWestGravity:
461 x -= (_size.left + _size.right) / 2;
464 case NorthEastGravity:
465 case SouthEastGravity:
467 x -= _size.left + _size.right;
477 switch (_client->gravity()) {
479 case NorthWestGravity:
480 case NorthEastGravity:
487 y -= (_size.top + _size.bottom) / 2;
490 case SouthWestGravity:
491 case SouthEastGravity:
493 y -= _size.top + _size.bottom;
504 void OBFrame::frameGravity(int &x, int &y)
510 switch (_client->gravity()) {
512 case NorthWestGravity:
514 case SouthWestGravity:
519 x += (_size.left + _size.right) / 2;
521 case NorthEastGravity:
523 case SouthEastGravity:
524 x += _size.left + _size.right;
533 switch (_client->gravity()) {
535 case NorthWestGravity:
537 case SouthWestGravity:
542 y += (_size.top + _size.bottom) / 2;
544 case NorthEastGravity:
546 case SouthEastGravity:
547 y += _size.top + _size.bottom;