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();
56 _button_close.setText("X");
57 _button_iconify.setText("I");
58 _button_max.setText("M");
59 _button_stick.setText("S");
60 _label.setText(_client->title());
75 void OBFrame::setStyle(otk::Style *style)
79 otk::OtkWidget::setStyle(style);
80 // set the grips' textures
81 _grip_left.setTexture(style->getGripFocus());
82 _grip_left.setUnfocusTexture(style->getGripUnfocus());
83 _grip_left.setPressedFocusTexture(style->getGripFocus());
84 _grip_left.setPressedUnfocusTexture(style->getGripUnfocus());
85 _grip_right.setTexture(style->getGripFocus());
86 _grip_right.setUnfocusTexture(style->getGripUnfocus());
87 _grip_right.setPressedFocusTexture(style->getGripFocus());
88 _grip_right.setPressedUnfocusTexture(style->getGripUnfocus());
90 _titlebar.setTexture(style->getTitleFocus());
91 _titlebar.setUnfocusTexture(style->getTitleUnfocus());
92 _handle.setTexture(style->getHandleFocus());
93 _handle.setUnfocusTexture(style->getHandleUnfocus());
95 // if a style was previously set, then 'replace' is true, cause we're
97 bool replace = (_style);
100 // XXX: do shit here whatever
101 // XXX: save the position based on gravity
106 // XXX: change when focus changes!
107 XSetWindowBorder(otk::OBDisplay::display, _plate.getWindow(),
108 _style->getFrameFocus()->color().pixel());
110 XSetWindowBorder(otk::OBDisplay::display, getWindow(),
111 _style->getBorderColor()->pixel());
112 XSetWindowBorder(otk::OBDisplay::display, _titlebar.getWindow(),
113 _style->getBorderColor()->pixel());
114 XSetWindowBorder(otk::OBDisplay::display, _grip_left.getWindow(),
115 _style->getBorderColor()->pixel());
116 XSetWindowBorder(otk::OBDisplay::display, _grip_right.getWindow(),
117 _style->getBorderColor()->pixel());
118 XSetWindowBorder(otk::OBDisplay::display, _handle.getWindow(),
119 _style->getBorderColor()->pixel());
121 // if !replace, then adjust() will get called after the client is grabbed!
123 adjust(); // size/position everything
127 void OBFrame::adjust()
129 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
130 _decorations = _client->decorations();
131 _decorations = 0xffffffff;
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
137 if (_decorations & OBClient::Decor_Border) {
138 bwidth = _style->getBorderWidth();
139 cbwidth = _style->getFrameWidth();
141 bwidth = cbwidth = 0;
142 _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
144 width = _client->area().width() + cbwidth * 2;
146 XSetWindowBorderWidth(otk::OBDisplay::display, _plate.getWindow(), cbwidth);
148 XSetWindowBorderWidth(otk::OBDisplay::display, getWindow(), bwidth);
149 XSetWindowBorderWidth(otk::OBDisplay::display, _titlebar.getWindow(),
151 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_left.getWindow(),
153 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_right.getWindow(),
155 XSetWindowBorderWidth(otk::OBDisplay::display, _handle.getWindow(), bwidth);
157 if (_decorations & OBClient::Decor_Titlebar) {
158 // set the titlebar size
159 _titlebar.setGeometry(-bwidth,
162 (_style->getFont().height() +
163 _style->getBevelWidth() * 2));
164 _innersize.top += _titlebar.height() + bwidth;
166 // set the label size
167 _label.setGeometry(0, _style->getBevelWidth(),
168 width, _style->getFont().height());
169 // set the buttons sizes
170 if (_decorations & OBClient::Decor_Iconify)
171 _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
173 _label.height() - 2);
174 if (_decorations & OBClient::Decor_Maximize)
175 _button_max.setGeometry(0, _style->getBevelWidth() + 1,
177 _label.height() - 2);
178 if (_decorations & OBClient::Decor_Sticky)
179 _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
181 _label.height() - 2);
182 if (_decorations & OBClient::Decor_Close)
183 _button_close.setGeometry(0, _style->getBevelWidth() + 1,
185 _label.height() - 2);
187 // separation between titlebar elements
188 const int sep = _style->getBevelWidth() + 1;
190 std::string layout = "SLIMC"; // XXX: get this from somewhere
191 // XXX: it is REQUIRED that by this point, the string only has one of each
192 // possible letter, all of the letters are valid, and L exists somewhere in
195 // the size of the label. this ASSUMES the layout has only buttons other
196 // that the ONE LABEL!!
197 // adds an extra sep so that there's a space on either side of the
198 // titlebar.. note: x = sep, below.
199 int lwidth = width - sep * 2 -
200 (_button_iconify.width() + sep) * (layout.size() - 1);
201 // quick sanity check for really small windows. if this is needed, its
202 // obviously not going to be displayed right...
203 // XXX: maybe we should make this look better somehow? constraints?
204 if (lwidth <= 0) lwidth = 1;
205 _label.setWidth(lwidth);
208 for (int i = 0, len = layout.size(); i < len; ++i) {
211 _button_iconify.move(x, _button_iconify.getRect().y());
212 x += _button_iconify.width();
215 _label.move(x, _label.getRect().y());
219 _button_max.move(x, _button_max.getRect().y());
220 x += _button_max.width();
223 _button_stick.move(x, _button_stick.getRect().y());
224 x += _button_stick.width();
227 _button_close.move(x, _button_close.getRect().y());
228 x += _button_close.width();
231 assert(false); // the layout string is invalid!
237 if (_decorations & OBClient::Decor_Handle) {
238 _handle.setGeometry(-bwidth,
239 _innersize.top + _client->area().height() + cbwidth,
240 width, _style->getHandleWidth());
241 _grip_left.setGeometry(-bwidth,
243 // XXX: get a Point class in otk and use that for
244 // the 'buttons size' since theyre all the same
245 _button_iconify.width() * 2,
247 _grip_right.setGeometry(((_handle.getRect().right() + 1) -
248 _button_iconify.width() * 2),
250 // XXX: get a Point class in otk and use that for
251 // the 'buttons size' since theyre all the same
252 _button_iconify.width() * 2,
254 _innersize.bottom += _handle.height() + bwidth;
258 // position/size all the windows
260 resize(_innersize.left + _innersize.right + _client->area().width(),
261 _innersize.top + _innersize.bottom + _client->area().height());
263 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
264 _client->area().width(), _client->area().height());
266 // map/unmap all the windows
267 if (_decorations & OBClient::Decor_Titlebar) {
269 if (_decorations & OBClient::Decor_Iconify)
270 _button_iconify.show();
272 _button_iconify.hide();
273 if (_decorations & OBClient::Decor_Maximize)
277 if (_decorations & OBClient::Decor_Sticky)
278 _button_stick.show();
280 _button_stick.hide();
281 if (_decorations & OBClient::Decor_Close)
282 _button_close.show();
284 _button_close.hide();
287 _titlebar.hide(true);
290 if (_decorations & OBClient::Decor_Handle)
295 // XXX: more is gunna have to happen here
297 _size.left = _innersize.left + bwidth;
298 _size.right = _innersize.right + bwidth;
299 _size.top = _innersize.top + bwidth;
300 _size.bottom = _innersize.bottom + bwidth;
308 void OBFrame::adjustShape()
311 int bwidth = (_decorations & OBClient::Decor_Border) ?
312 _style->getBorderWidth() : 0;
314 if (!_client->shaped()) {
315 // clear the shape on the frame window
316 XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
321 // make the frame's shape match the clients
322 XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
325 _client->window(), ShapeBounding, ShapeSet);
330 if (_decorations & OBClient::Decor_Titlebar) {
331 xrect[0].x = _titlebar.getRect().x();
332 xrect[0].y = _titlebar.getRect().y();
333 xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
334 xrect[0].height = _titlebar.height() + bwidth * 2;
338 if (_decorations & OBClient::Decor_Handle) {
339 xrect[1].x = _handle.getRect().x();
340 xrect[1].y = _handle.getRect().y();
341 xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
342 xrect[1].height = _handle.height() + bwidth * 2;
346 XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
347 ShapeBounding, 0, 0, xrect, num,
348 ShapeUnion, Unsorted);
354 void OBFrame::grabClient()
357 // select the event mask on the frame
358 //XSelectInput(otk::OBDisplay::display, _window, SubstructureRedirectMask);
360 // reparent the client to the frame
361 XReparentWindow(otk::OBDisplay::display, _client->window(),
362 _plate.getWindow(), 0, 0);
363 _client->ignore_unmaps++;
365 // raise the client above the frame
366 //XRaiseWindow(otk::OBDisplay::display, _client->window());
367 // map the client so it maps when the frame does
368 XMapWindow(otk::OBDisplay::display, _client->window());
375 void OBFrame::releaseClient(bool remap)
377 // check if the app has already reparented its window to the root window
379 if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
380 ReparentNotify, &ev)) {
381 remap = true; // XXX: why do we remap the window if they already
382 // reparented to root?
384 // according to the ICCCM - if the client doesn't reparent to
385 // root, then we have to do it for them
386 XReparentWindow(otk::OBDisplay::display, _client->window(),
387 _screen->getRootWindow(),
388 _client->area().x(), _client->area().y());
391 // if we want to remap the window, do so now
393 XMapWindow(otk::OBDisplay::display, _client->window());
397 void OBFrame::applyGravity()
400 // apply horizontal window gravity
401 switch (_client->gravity()) {
403 case NorthWestGravity:
404 case SouthWestGravity:
406 x = _client->area().x();
412 x = _client->area().x() - (_size.left + _size.right) / 2;
415 case NorthEastGravity:
416 case SouthEastGravity:
418 x = _client->area().x() - _size.left - _size.right + 2;
423 x = _client->area().x() - _size.left;
427 // apply vertical window gravity
428 switch (_client->gravity()) {
430 case NorthWestGravity:
431 case NorthEastGravity:
433 y = _client->area().y();
439 y = _client->area().y() - (_size.top + _size.bottom) / 2;
442 case SouthWestGravity:
443 case SouthEastGravity:
445 y = _client->area().y() - _size.top - _size.bottom + 2;
450 y = _client->area().y() - _size.top;
457 void OBFrame::reverseGravity()
459 move(_client->area().x() - _size.left, _client->area().y() - _size.top);