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 _label.setWidth(width - sep * 2 -
200 (_button_iconify.width() + sep) * (layout.size() - 1));
203 for (int i = 0, len = layout.size(); i < len; ++i) {
206 _button_iconify.move(x, _button_iconify.getRect().y());
207 x += _button_iconify.width();
210 _label.move(x, _label.getRect().y());
214 _button_max.move(x, _button_max.getRect().y());
215 x += _button_max.width();
218 _button_stick.move(x, _button_stick.getRect().y());
219 x += _button_stick.width();
222 _button_close.move(x, _button_close.getRect().y());
223 x += _button_close.width();
226 assert(false); // the layout string is invalid!
232 if (_decorations & OBClient::Decor_Handle) {
233 _handle.setGeometry(-bwidth,
234 _innersize.top + _client->area().height() + cbwidth,
235 width, _style->getHandleWidth());
236 _grip_left.setGeometry(-bwidth,
238 // XXX: get a Point class in otk and use that for
239 // the 'buttons size' since theyre all the same
240 _button_iconify.width() * 2,
242 _grip_right.setGeometry(((_handle.getRect().right() + 1) -
243 _button_iconify.width() * 2),
245 // XXX: get a Point class in otk and use that for
246 // the 'buttons size' since theyre all the same
247 _button_iconify.width() * 2,
249 _innersize.bottom += _handle.height() + bwidth;
253 // position/size all the windows
255 resize(_innersize.left + _innersize.right + _client->area().width(),
256 _innersize.top + _innersize.bottom + _client->area().height());
258 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
259 _client->area().width(), _client->area().height());
261 // map/unmap all the windows
262 if (_decorations & OBClient::Decor_Titlebar) {
264 if (_decorations & OBClient::Decor_Iconify)
265 _button_iconify.show();
267 _button_iconify.hide();
268 if (_decorations & OBClient::Decor_Maximize)
272 if (_decorations & OBClient::Decor_Sticky)
273 _button_stick.show();
275 _button_stick.hide();
276 if (_decorations & OBClient::Decor_Close)
277 _button_close.show();
279 _button_close.hide();
282 _titlebar.hide(true);
285 if (_decorations & OBClient::Decor_Handle)
290 // XXX: more is gunna have to happen here
292 _size.left = _innersize.left + bwidth;
293 _size.right = _innersize.right + bwidth;
294 _size.top = _innersize.top + bwidth;
295 _size.bottom = _innersize.bottom + bwidth;
303 void OBFrame::adjustShape()
306 int bwidth = (_decorations & OBClient::Decor_Border) ?
307 _style->getBorderWidth() : 0;
309 if (!_client->shaped()) {
310 // clear the shape on the frame window
311 XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
316 // make the frame's shape match the clients
317 XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
320 _client->window(), ShapeBounding, ShapeSet);
325 if (_decorations & OBClient::Decor_Titlebar) {
326 xrect[0].x = _titlebar.getRect().x();
327 xrect[0].y = _titlebar.getRect().y();
328 xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
329 xrect[0].height = _titlebar.height() + bwidth * 2;
333 if (_decorations & OBClient::Decor_Handle) {
334 xrect[1].x = _handle.getRect().x();
335 xrect[1].y = _handle.getRect().y();
336 xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
337 xrect[1].height = _handle.height() + bwidth * 2;
341 XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
342 ShapeBounding, 0, 0, xrect, num,
343 ShapeUnion, Unsorted);
349 void OBFrame::grabClient()
352 // select the event mask on the frame
353 //XSelectInput(otk::OBDisplay::display, _window, SubstructureRedirectMask);
355 // reparent the client to the frame
356 XReparentWindow(otk::OBDisplay::display, _client->window(),
357 _plate.getWindow(), 0, 0);
358 _client->ignore_unmaps++;
360 // raise the client above the frame
361 //XRaiseWindow(otk::OBDisplay::display, _client->window());
362 // map the client so it maps when the frame does
363 XMapWindow(otk::OBDisplay::display, _client->window());
370 void OBFrame::releaseClient(bool remap)
372 // check if the app has already reparented its window to the root window
374 if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
375 ReparentNotify, &ev)) {
376 remap = true; // XXX: why do we remap the window if they already
377 // reparented to root?
379 // according to the ICCCM - if the client doesn't reparent to
380 // root, then we have to do it for them
381 XReparentWindow(otk::OBDisplay::display, _client->window(),
382 _screen->getRootWindow(),
383 _client->area().x(), _client->area().y());
386 // if we want to remap the window, do so now
388 XMapWindow(otk::OBDisplay::display, _client->window());
392 void OBFrame::applyGravity()
395 // apply horizontal window gravity
396 switch (_client->gravity()) {
398 case NorthWestGravity:
399 case SouthWestGravity:
401 x = _client->area().x();
407 x = _client->area().x() - (_size.left + _size.right) / 2;
410 case NorthEastGravity:
411 case SouthEastGravity:
413 x = _client->area().x() - _size.left - _size.right + 2;
418 x = _client->area().x() - _size.left;
422 // apply vertical window gravity
423 switch (_client->gravity()) {
425 case NorthWestGravity:
426 case NorthEastGravity:
428 y = _client->area().y();
434 y = _client->area().y() - (_size.top + _size.bottom) / 2;
437 case SouthWestGravity:
438 case SouthEastGravity:
440 y = _client->area().y() - _size.top - _size.bottom + 2;
445 y = _client->area().y() - _size.top;