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"
22 const long OBFrame::event_mask;
24 OBFrame::OBFrame(OBClient *client, otk::Style *style)
25 : otk::OtkWidget(Openbox::instance, style),
27 _screen(otk::OBDisplay::screenInfo(client->screen())),
30 _button_close(&_titlebar),
31 _button_iconify(&_titlebar),
32 _button_max(&_titlebar),
33 _button_stick(&_titlebar),
37 _grip_right(&_handle),
38 _decorations(client->decorations())
43 XSelectInput(otk::OBDisplay::display, getWindow(), OBFrame::event_mask);
46 _titlebar.unmanaged();
47 _button_close.unmanaged();
48 _button_iconify.unmanaged();
49 _button_max.unmanaged();
50 _button_stick.unmanaged();
53 _grip_left.unmanaged();
54 _grip_right.unmanaged();
57 _grip_left.setCursor(Openbox::instance->cursors().ll_angle);
58 _grip_right.setCursor(Openbox::instance->cursors().lr_angle);
62 _button_close.setText("X");
63 _button_iconify.setText("I");
64 _button_max.setText("M");
65 _button_stick.setText("S");
66 _label.setText(_client->title());
81 void OBFrame::setStyle(otk::Style *style)
85 otk::OtkWidget::setStyle(style);
86 // set the grips' textures
87 _grip_left.setTexture(style->getGripFocus());
88 _grip_left.setUnfocusTexture(style->getGripUnfocus());
89 _grip_left.setPressedFocusTexture(style->getGripFocus());
90 _grip_left.setPressedUnfocusTexture(style->getGripUnfocus());
91 _grip_right.setTexture(style->getGripFocus());
92 _grip_right.setUnfocusTexture(style->getGripUnfocus());
93 _grip_right.setPressedFocusTexture(style->getGripFocus());
94 _grip_right.setPressedUnfocusTexture(style->getGripUnfocus());
96 _titlebar.setTexture(style->getTitleFocus());
97 _titlebar.setUnfocusTexture(style->getTitleUnfocus());
98 _handle.setTexture(style->getHandleFocus());
99 _handle.setUnfocusTexture(style->getHandleUnfocus());
101 // if a style was previously set, then 'replace' is true, cause we're
103 bool replace = (_style);
106 // XXX: do shit here whatever
107 // XXX: save the position based on gravity
112 // XXX: change when focus changes!
113 XSetWindowBorder(otk::OBDisplay::display, _plate.getWindow(),
114 _style->getFrameFocus()->color().pixel());
116 XSetWindowBorder(otk::OBDisplay::display, getWindow(),
117 _style->getBorderColor()->pixel());
118 XSetWindowBorder(otk::OBDisplay::display, _titlebar.getWindow(),
119 _style->getBorderColor()->pixel());
120 XSetWindowBorder(otk::OBDisplay::display, _grip_left.getWindow(),
121 _style->getBorderColor()->pixel());
122 XSetWindowBorder(otk::OBDisplay::display, _grip_right.getWindow(),
123 _style->getBorderColor()->pixel());
124 XSetWindowBorder(otk::OBDisplay::display, _handle.getWindow(),
125 _style->getBorderColor()->pixel());
127 // if !replace, then adjust() will get called after the client is grabbed!
129 adjust(); // size/position everything
133 void OBFrame::adjust()
135 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
136 _decorations = _client->decorations();
137 _decorations = 0xffffffff;
139 int width; // the width of the client and its border
140 int bwidth; // width to make borders
141 int cbwidth; // width of the inner client border
143 if (_decorations & OBClient::Decor_Border) {
144 bwidth = _style->getBorderWidth();
145 cbwidth = _style->getFrameWidth();
147 bwidth = cbwidth = 0;
148 _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
150 width = _client->area().width() + cbwidth * 2;
152 XSetWindowBorderWidth(otk::OBDisplay::display, _plate.getWindow(), cbwidth);
154 XSetWindowBorderWidth(otk::OBDisplay::display, getWindow(), bwidth);
155 XSetWindowBorderWidth(otk::OBDisplay::display, _titlebar.getWindow(),
157 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_left.getWindow(),
159 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_right.getWindow(),
161 XSetWindowBorderWidth(otk::OBDisplay::display, _handle.getWindow(), bwidth);
163 if (_decorations & OBClient::Decor_Titlebar) {
164 // set the titlebar size
165 _titlebar.setGeometry(-bwidth,
168 (_style->getFont().height() +
169 _style->getBevelWidth() * 2));
170 _innersize.top += _titlebar.height() + bwidth;
172 // set the label size
173 _label.setGeometry(0, _style->getBevelWidth(),
174 width, _style->getFont().height());
175 // set the buttons sizes
176 if (_decorations & OBClient::Decor_Iconify)
177 _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
179 _label.height() - 2);
180 if (_decorations & OBClient::Decor_Maximize)
181 _button_max.setGeometry(0, _style->getBevelWidth() + 1,
183 _label.height() - 2);
184 if (_decorations & OBClient::Decor_Sticky)
185 _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
187 _label.height() - 2);
188 if (_decorations & OBClient::Decor_Close)
189 _button_close.setGeometry(0, _style->getBevelWidth() + 1,
191 _label.height() - 2);
193 // separation between titlebar elements
194 const int sep = _style->getBevelWidth() + 1;
196 std::string layout = "SLIMC"; // XXX: get this from somewhere
197 // XXX: it is REQUIRED that by this point, the string only has one of each
198 // possible letter, all of the letters are valid, and L exists somewhere in
201 // the size of the label. this ASSUMES the layout has only buttons other
202 // that the ONE LABEL!!
203 // adds an extra sep so that there's a space on either side of the
204 // titlebar.. note: x = sep, below.
205 int lwidth = width - sep * 2 -
206 (_button_iconify.width() + sep) * (layout.size() - 1);
207 // quick sanity check for really small windows. if this is needed, its
208 // obviously not going to be displayed right...
209 // XXX: maybe we should make this look better somehow? constraints?
210 if (lwidth <= 0) lwidth = 1;
211 _label.setWidth(lwidth);
214 for (int i = 0, len = layout.size(); i < len; ++i) {
217 _button_iconify.move(x, _button_iconify.getRect().y());
218 x += _button_iconify.width();
221 _label.move(x, _label.getRect().y());
225 _button_max.move(x, _button_max.getRect().y());
226 x += _button_max.width();
229 _button_stick.move(x, _button_stick.getRect().y());
230 x += _button_stick.width();
233 _button_close.move(x, _button_close.getRect().y());
234 x += _button_close.width();
237 assert(false); // the layout string is invalid!
243 if (_decorations & OBClient::Decor_Handle) {
244 _handle.setGeometry(-bwidth,
245 _innersize.top + _client->area().height() + cbwidth,
246 width, _style->getHandleWidth());
247 _grip_left.setGeometry(-bwidth,
249 // XXX: get a Point class in otk and use that for
250 // the 'buttons size' since theyre all the same
251 _button_iconify.width() * 2,
253 _grip_right.setGeometry(((_handle.getRect().right() + 1) -
254 _button_iconify.width() * 2),
256 // XXX: get a Point class in otk and use that for
257 // the 'buttons size' since theyre all the same
258 _button_iconify.width() * 2,
260 _innersize.bottom += _handle.height() + bwidth;
264 // position/size all the windows
266 resize(_innersize.left + _innersize.right + _client->area().width(),
267 _innersize.top + _innersize.bottom + _client->area().height());
269 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
270 _client->area().width(), _client->area().height());
272 // map/unmap all the windows
273 if (_decorations & OBClient::Decor_Titlebar) {
275 if (_decorations & OBClient::Decor_Iconify)
276 _button_iconify.show();
278 _button_iconify.hide();
279 if (_decorations & OBClient::Decor_Maximize)
283 if (_decorations & OBClient::Decor_Sticky)
284 _button_stick.show();
286 _button_stick.hide();
287 if (_decorations & OBClient::Decor_Close)
288 _button_close.show();
290 _button_close.hide();
293 _titlebar.hide(true);
296 if (_decorations & OBClient::Decor_Handle)
301 // XXX: more is gunna have to happen here
303 _size.left = _innersize.left + bwidth;
304 _size.right = _innersize.right + bwidth;
305 _size.top = _innersize.top + bwidth;
306 _size.bottom = _innersize.bottom + bwidth;
314 void OBFrame::adjustShape()
317 int bwidth = (_decorations & OBClient::Decor_Border) ?
318 _style->getBorderWidth() : 0;
320 if (!_client->shaped()) {
321 // clear the shape on the frame window
322 XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
327 // make the frame's shape match the clients
328 XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
331 _client->window(), ShapeBounding, ShapeSet);
336 if (_decorations & OBClient::Decor_Titlebar) {
337 xrect[0].x = _titlebar.getRect().x();
338 xrect[0].y = _titlebar.getRect().y();
339 xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
340 xrect[0].height = _titlebar.height() + bwidth * 2;
344 if (_decorations & OBClient::Decor_Handle) {
345 xrect[1].x = _handle.getRect().x();
346 xrect[1].y = _handle.getRect().y();
347 xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
348 xrect[1].height = _handle.height() + bwidth * 2;
352 XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
353 ShapeBounding, 0, 0, xrect, num,
354 ShapeUnion, Unsorted);
360 void OBFrame::grabClient()
363 // reparent the client to the frame
364 XReparentWindow(otk::OBDisplay::display, _client->window(),
365 _plate.getWindow(), 0, 0);
366 _client->ignore_unmaps++;
368 // select the event mask on the client's parent
369 //XSelectInput(otk::OBDisplay::display, _plate.getWindow(),
370 // SubstructureRedirectMask);
372 // map the client so it maps when the frame does
373 XMapWindow(otk::OBDisplay::display, _client->window());
380 void OBFrame::releaseClient(bool remap)
382 // check if the app has already reparented its window to the root window
384 if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
385 ReparentNotify, &ev)) {
386 remap = true; // XXX: why do we remap the window if they already
387 // reparented to root?
389 // according to the ICCCM - if the client doesn't reparent to
390 // root, then we have to do it for them
391 XReparentWindow(otk::OBDisplay::display, _client->window(),
392 _screen->getRootWindow(),
393 _client->area().x(), _client->area().y());
396 // if we want to remap the window, do so now
398 XMapWindow(otk::OBDisplay::display, _client->window());
402 void OBFrame::applyGravity()
405 // apply horizontal window gravity
406 switch (_client->gravity()) {
408 case NorthWestGravity:
409 case SouthWestGravity:
411 x = _client->area().x();
417 x = _client->area().x() - (_size.left + _size.right) / 2;
420 case NorthEastGravity:
421 case SouthEastGravity:
423 x = _client->area().x() - _size.left - _size.right + 2;
428 x = _client->area().x() - _size.left;
432 // apply vertical window gravity
433 switch (_client->gravity()) {
435 case NorthWestGravity:
436 case NorthEastGravity:
438 y = _client->area().y();
444 y = _client->area().y() - (_size.top + _size.bottom) / 2;
447 case SouthWestGravity:
448 case SouthEastGravity:
450 y = _client->area().y() - _size.top - _size.bottom + 2;
455 y = _client->area().y() - _size.top;
462 void OBFrame::reverseGravity()
464 move(_client->area().x() - _size.left, _client->area().y() - _size.top);