1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
9 #include <X11/extensions/shape.h>
16 #include "otk/display.hh"
19 #include <iostream> // TEMP
23 OBFrame::OBFrame(OBClient *client, otk::Style *style)
24 : otk::OtkWidget(Openbox::instance, style),
26 _screen(otk::OBDisplay::screenInfo(client->screen())),
29 _button_close(&_titlebar),
30 _button_iconify(&_titlebar),
31 _button_max(&_titlebar),
32 _button_stick(&_titlebar),
36 _grip_right(&_handle),
37 _decorations(client->decorations())
43 _titlebar.unmanaged();
44 _button_close.unmanaged();
45 _button_iconify.unmanaged();
46 _button_max.unmanaged();
47 _button_stick.unmanaged();
50 _grip_left.unmanaged();
51 _grip_right.unmanaged();
54 _grip_left.setCursor(Openbox::instance->cursors().ll_angle);
55 _grip_right.setCursor(Openbox::instance->cursors().lr_angle);
59 _button_close.setText("X");
60 _button_iconify.setText("I");
61 _button_max.setText("M");
62 _button_stick.setText("S");
63 _label.setText(_client->title());
78 void OBFrame::setStyle(otk::Style *style)
82 otk::OtkWidget::setStyle(style);
83 // set the grips' textures
84 _grip_left.setTexture(style->getGripFocus());
85 _grip_left.setUnfocusTexture(style->getGripUnfocus());
86 _grip_left.setPressedFocusTexture(style->getGripFocus());
87 _grip_left.setPressedUnfocusTexture(style->getGripUnfocus());
88 _grip_right.setTexture(style->getGripFocus());
89 _grip_right.setUnfocusTexture(style->getGripUnfocus());
90 _grip_right.setPressedFocusTexture(style->getGripFocus());
91 _grip_right.setPressedUnfocusTexture(style->getGripUnfocus());
93 _titlebar.setTexture(style->getTitleFocus());
94 _titlebar.setUnfocusTexture(style->getTitleUnfocus());
95 _handle.setTexture(style->getHandleFocus());
96 _handle.setUnfocusTexture(style->getHandleUnfocus());
98 // if a style was previously set, then 'replace' is true, cause we're
100 bool replace = (_style);
103 // XXX: do shit here whatever
104 // XXX: save the position based on gravity
109 // XXX: change when focus changes!
110 XSetWindowBorder(otk::OBDisplay::display, _plate.getWindow(),
111 _style->getFrameFocus()->color().pixel());
113 XSetWindowBorder(otk::OBDisplay::display, getWindow(),
114 _style->getBorderColor()->pixel());
115 XSetWindowBorder(otk::OBDisplay::display, _titlebar.getWindow(),
116 _style->getBorderColor()->pixel());
117 XSetWindowBorder(otk::OBDisplay::display, _grip_left.getWindow(),
118 _style->getBorderColor()->pixel());
119 XSetWindowBorder(otk::OBDisplay::display, _grip_right.getWindow(),
120 _style->getBorderColor()->pixel());
121 XSetWindowBorder(otk::OBDisplay::display, _handle.getWindow(),
122 _style->getBorderColor()->pixel());
124 // if !replace, then adjust() will get called after the client is grabbed!
126 adjust(); // size/position everything
130 void OBFrame::adjust()
132 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
133 _decorations = _client->decorations();
134 _decorations = 0xffffffff;
136 int width; // the width of the client and its border
137 int bwidth; // width to make borders
138 int cbwidth; // width of the inner client border
140 if (_decorations & OBClient::Decor_Border) {
141 bwidth = _style->getBorderWidth();
142 cbwidth = _style->getFrameWidth();
144 bwidth = cbwidth = 0;
145 _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
147 width = _client->area().width() + cbwidth * 2;
149 XSetWindowBorderWidth(otk::OBDisplay::display, _plate.getWindow(), cbwidth);
151 XSetWindowBorderWidth(otk::OBDisplay::display, getWindow(), bwidth);
152 XSetWindowBorderWidth(otk::OBDisplay::display, _titlebar.getWindow(),
154 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_left.getWindow(),
156 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_right.getWindow(),
158 XSetWindowBorderWidth(otk::OBDisplay::display, _handle.getWindow(), bwidth);
160 if (_decorations & OBClient::Decor_Titlebar) {
161 // set the titlebar size
162 _titlebar.setGeometry(-bwidth,
165 (_style->getFont().height() +
166 _style->getBevelWidth() * 2));
167 _innersize.top += _titlebar.height() + bwidth;
169 // set the label size
170 _label.setGeometry(0, _style->getBevelWidth(),
171 width, _style->getFont().height());
172 // set the buttons sizes
173 if (_decorations & OBClient::Decor_Iconify)
174 _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
176 _label.height() - 2);
177 if (_decorations & OBClient::Decor_Maximize)
178 _button_max.setGeometry(0, _style->getBevelWidth() + 1,
180 _label.height() - 2);
181 if (_decorations & OBClient::Decor_Sticky)
182 _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
184 _label.height() - 2);
185 if (_decorations & OBClient::Decor_Close)
186 _button_close.setGeometry(0, _style->getBevelWidth() + 1,
188 _label.height() - 2);
190 // separation between titlebar elements
191 const int sep = _style->getBevelWidth() + 1;
193 std::string layout = "SLIMC"; // XXX: get this from somewhere
194 // XXX: it is REQUIRED that by this point, the string only has one of each
195 // possible letter, all of the letters are valid, and L exists somewhere in
198 // the size of the label. this ASSUMES the layout has only buttons other
199 // that the ONE LABEL!!
200 // adds an extra sep so that there's a space on either side of the
201 // titlebar.. note: x = sep, below.
202 int lwidth = width - sep * 2 -
203 (_button_iconify.width() + sep) * (layout.size() - 1);
204 // quick sanity check for really small windows. if this is needed, its
205 // obviously not going to be displayed right...
206 // XXX: maybe we should make this look better somehow? constraints?
207 if (lwidth <= 0) lwidth = 1;
208 _label.setWidth(lwidth);
211 for (int i = 0, len = layout.size(); i < len; ++i) {
214 _button_iconify.move(x, _button_iconify.getRect().y());
215 x += _button_iconify.width();
218 _label.move(x, _label.getRect().y());
222 _button_max.move(x, _button_max.getRect().y());
223 x += _button_max.width();
226 _button_stick.move(x, _button_stick.getRect().y());
227 x += _button_stick.width();
230 _button_close.move(x, _button_close.getRect().y());
231 x += _button_close.width();
234 assert(false); // the layout string is invalid!
240 if (_decorations & OBClient::Decor_Handle) {
241 _handle.setGeometry(-bwidth,
242 _innersize.top + _client->area().height() + cbwidth,
243 width, _style->getHandleWidth());
244 _grip_left.setGeometry(-bwidth,
246 // XXX: get a Point class in otk and use that for
247 // the 'buttons size' since theyre all the same
248 _button_iconify.width() * 2,
250 _grip_right.setGeometry(((_handle.getRect().right() + 1) -
251 _button_iconify.width() * 2),
253 // XXX: get a Point class in otk and use that for
254 // the 'buttons size' since theyre all the same
255 _button_iconify.width() * 2,
257 _innersize.bottom += _handle.height() + bwidth;
261 // position/size all the windows
263 resize(_innersize.left + _innersize.right + _client->area().width(),
264 _innersize.top + _innersize.bottom + _client->area().height());
266 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
267 _client->area().width(), _client->area().height());
269 // map/unmap all the windows
270 if (_decorations & OBClient::Decor_Titlebar) {
272 if (_decorations & OBClient::Decor_Iconify)
273 _button_iconify.show();
275 _button_iconify.hide();
276 if (_decorations & OBClient::Decor_Maximize)
280 if (_decorations & OBClient::Decor_Sticky)
281 _button_stick.show();
283 _button_stick.hide();
284 if (_decorations & OBClient::Decor_Close)
285 _button_close.show();
287 _button_close.hide();
290 _titlebar.hide(true);
293 if (_decorations & OBClient::Decor_Handle)
298 // XXX: more is gunna have to happen here
300 _size.left = _innersize.left + bwidth;
301 _size.right = _innersize.right + bwidth;
302 _size.top = _innersize.top + bwidth;
303 _size.bottom = _innersize.bottom + bwidth;
311 void OBFrame::adjustShape()
314 int bwidth = (_decorations & OBClient::Decor_Border) ?
315 _style->getBorderWidth() : 0;
317 if (!_client->shaped()) {
318 // clear the shape on the frame window
319 XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
324 // make the frame's shape match the clients
325 XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
328 _client->window(), ShapeBounding, ShapeSet);
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;
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;
349 XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
350 ShapeBounding, 0, 0, xrect, num,
351 ShapeUnion, Unsorted);
357 void OBFrame::grabClient()
360 // reparent the client to the frame
361 XReparentWindow(otk::OBDisplay::display, _client->window(),
362 _plate.getWindow(), 0, 0);
363 _client->ignore_unmaps++;
365 // select the event mask on the client's parent
366 //XSelectInput(otk::OBDisplay::display, _plate.getWindow(),
367 // SubstructureRedirectMask);
369 // map the client so it maps when the frame does
370 XMapWindow(otk::OBDisplay::display, _client->window());
377 void OBFrame::releaseClient(bool remap)
379 // check if the app has already reparented its window to the root window
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?
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());
393 // if we want to remap the window, do so now
395 XMapWindow(otk::OBDisplay::display, _client->window());
399 void OBFrame::applyGravity()
402 // apply horizontal window gravity
403 switch (_client->gravity()) {
405 case NorthWestGravity:
406 case SouthWestGravity:
408 x = _client->area().x();
414 x = _client->area().x() - (_size.left + _size.right) / 2;
417 case NorthEastGravity:
418 case SouthEastGravity:
420 x = _client->area().x() - _size.left - _size.right + 2;
425 x = _client->area().x() - _size.left;
429 // apply vertical window gravity
430 switch (_client->gravity()) {
432 case NorthWestGravity:
433 case NorthEastGravity:
435 y = _client->area().y();
441 y = _client->area().y() - (_size.top + _size.bottom) / 2;
444 case SouthWestGravity:
445 case SouthEastGravity:
447 y = _client->area().y() - _size.top - _size.bottom + 2;
452 y = _client->area().y() - _size.top;
459 void OBFrame::reverseGravity()
461 move(_client->area().x() - _size.left, _client->area().y() - _size.top);