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
111 // XXX: change when focus changes!
112 XSetWindowBorder(otk::OBDisplay::display, _plate.getWindow(),
113 _style->getFrameFocus()->color().pixel());
115 XSetWindowBorder(otk::OBDisplay::display, getWindow(),
116 _style->getBorderColor()->pixel());
117 XSetWindowBorder(otk::OBDisplay::display, _titlebar.getWindow(),
118 _style->getBorderColor()->pixel());
119 XSetWindowBorder(otk::OBDisplay::display, _grip_left.getWindow(),
120 _style->getBorderColor()->pixel());
121 XSetWindowBorder(otk::OBDisplay::display, _grip_right.getWindow(),
122 _style->getBorderColor()->pixel());
123 XSetWindowBorder(otk::OBDisplay::display, _handle.getWindow(),
124 _style->getBorderColor()->pixel());
126 // if !replace, then adjust() will get called after the client is grabbed!
128 // size/position everything
135 void OBFrame::adjustSize()
137 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
138 _decorations = _client->decorations();
139 _decorations = 0xffffffff;
141 int width; // the width of the client and its border
142 int bwidth; // width to make borders
143 int cbwidth; // width of the inner client border
145 if (_decorations & OBClient::Decor_Border) {
146 bwidth = _style->getBorderWidth();
147 cbwidth = _style->getFrameWidth();
149 bwidth = cbwidth = 0;
150 _innersize.left = _innersize.top = _innersize.bottom = _innersize.right =
152 width = _client->area().width() + cbwidth * 2;
154 XSetWindowBorderWidth(otk::OBDisplay::display, _plate.getWindow(), cbwidth);
156 XSetWindowBorderWidth(otk::OBDisplay::display, getWindow(), bwidth);
157 XSetWindowBorderWidth(otk::OBDisplay::display, _titlebar.getWindow(),
159 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_left.getWindow(),
161 XSetWindowBorderWidth(otk::OBDisplay::display, _grip_right.getWindow(),
163 XSetWindowBorderWidth(otk::OBDisplay::display, _handle.getWindow(), bwidth);
165 if (_decorations & OBClient::Decor_Titlebar) {
166 // set the titlebar size
167 _titlebar.setGeometry(-bwidth,
170 (_style->getFont().height() +
171 _style->getBevelWidth() * 2));
172 _innersize.top += _titlebar.height() + bwidth;
174 // set the label size
175 _label.setGeometry(0, _style->getBevelWidth(),
176 width, _style->getFont().height());
177 // set the buttons sizes
178 if (_decorations & OBClient::Decor_Iconify)
179 _button_iconify.setGeometry(0, _style->getBevelWidth() + 1,
181 _label.height() - 2);
182 if (_decorations & OBClient::Decor_Maximize)
183 _button_max.setGeometry(0, _style->getBevelWidth() + 1,
185 _label.height() - 2);
186 if (_decorations & OBClient::Decor_Sticky)
187 _button_stick.setGeometry(0, _style->getBevelWidth() + 1,
189 _label.height() - 2);
190 if (_decorations & OBClient::Decor_Close)
191 _button_close.setGeometry(0, _style->getBevelWidth() + 1,
193 _label.height() - 2);
195 // separation between titlebar elements
196 const int sep = _style->getBevelWidth() + 1;
198 std::string layout = "SLIMC"; // XXX: get this from somewhere
199 // XXX: it is REQUIRED that by this point, the string only has one of each
200 // possible letter, all of the letters are valid, and L exists somewhere in
203 // the size of the label. this ASSUMES the layout has only buttons other
204 // that the ONE LABEL!!
205 // adds an extra sep so that there's a space on either side of the
206 // titlebar.. note: x = sep, below.
207 int lwidth = width - sep * 2 -
208 (_button_iconify.width() + sep) * (layout.size() - 1);
209 // quick sanity check for really small windows. if this is needed, its
210 // obviously not going to be displayed right...
211 // XXX: maybe we should make this look better somehow? constraints?
212 if (lwidth <= 0) lwidth = 1;
213 _label.setWidth(lwidth);
216 for (int i = 0, len = layout.size(); i < len; ++i) {
219 _button_iconify.move(x, _button_iconify.getRect().y());
220 x += _button_iconify.width();
223 _label.move(x, _label.getRect().y());
227 _button_max.move(x, _button_max.getRect().y());
228 x += _button_max.width();
231 _button_stick.move(x, _button_stick.getRect().y());
232 x += _button_stick.width();
235 _button_close.move(x, _button_close.getRect().y());
236 x += _button_close.width();
239 assert(false); // the layout string is invalid!
245 if (_decorations & OBClient::Decor_Handle) {
246 _handle.setGeometry(-bwidth,
247 _innersize.top + _client->area().height() + cbwidth,
248 width, _style->getHandleWidth());
249 _grip_left.setGeometry(-bwidth,
251 // XXX: get a Point class in otk and use that for
252 // the 'buttons size' since theyre all the same
253 _button_iconify.width() * 2,
255 _grip_right.setGeometry(((_handle.getRect().right() + 1) -
256 _button_iconify.width() * 2),
258 // XXX: get a Point class in otk and use that for
259 // the 'buttons size' since theyre all the same
260 _button_iconify.width() * 2,
262 _innersize.bottom += _handle.height() + bwidth;
266 // position/size all the windows
268 resize(_innersize.left + _innersize.right + _client->area().width(),
269 _innersize.top + _innersize.bottom + _client->area().height());
271 _plate.setGeometry(_innersize.left - cbwidth, _innersize.top - cbwidth,
272 _client->area().width(), _client->area().height());
274 // map/unmap all the windows
275 if (_decorations & OBClient::Decor_Titlebar) {
277 if (_decorations & OBClient::Decor_Iconify)
278 _button_iconify.show();
280 _button_iconify.hide();
281 if (_decorations & OBClient::Decor_Maximize)
285 if (_decorations & OBClient::Decor_Sticky)
286 _button_stick.show();
288 _button_stick.hide();
289 if (_decorations & OBClient::Decor_Close)
290 _button_close.show();
292 _button_close.hide();
295 _titlebar.hide(true);
298 if (_decorations & OBClient::Decor_Handle)
303 // XXX: more is gunna have to happen here
305 _size.left = _innersize.left + bwidth;
306 _size.right = _innersize.right + bwidth;
307 _size.top = _innersize.top + bwidth;
308 _size.bottom = _innersize.bottom + bwidth;
316 void OBFrame::adjustPosition()
324 void OBFrame::adjustShape()
327 int bwidth = (_decorations & OBClient::Decor_Border) ?
328 _style->getBorderWidth() : 0;
330 if (!_client->shaped()) {
331 // clear the shape on the frame window
332 XShapeCombineMask(otk::OBDisplay::display, getWindow(), ShapeBounding,
337 // make the frame's shape match the clients
338 XShapeCombineShape(otk::OBDisplay::display, getWindow(), ShapeBounding,
341 _client->window(), ShapeBounding, ShapeSet);
346 if (_decorations & OBClient::Decor_Titlebar) {
347 xrect[0].x = _titlebar.getRect().x();
348 xrect[0].y = _titlebar.getRect().y();
349 xrect[0].width = _titlebar.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
350 xrect[0].height = _titlebar.height() + bwidth * 2;
354 if (_decorations & OBClient::Decor_Handle) {
355 xrect[1].x = _handle.getRect().x();
356 xrect[1].y = _handle.getRect().y();
357 xrect[1].width = _handle.width() + bwidth * 2; // XXX: this is useless once the widget handles borders!
358 xrect[1].height = _handle.height() + bwidth * 2;
362 XShapeCombineRectangles(otk::OBDisplay::display, getWindow(),
363 ShapeBounding, 0, 0, xrect, num,
364 ShapeUnion, Unsorted);
370 void OBFrame::grabClient()
373 // reparent the client to the frame
374 XReparentWindow(otk::OBDisplay::display, _client->window(),
375 _plate.getWindow(), 0, 0);
376 _client->ignore_unmaps++;
378 // select the event mask on the client's parent (to receive config req's)
379 XSelectInput(otk::OBDisplay::display, _plate.getWindow(),
380 SubstructureRedirectMask);
382 // map the client so it maps when the frame does
383 XMapWindow(otk::OBDisplay::display, _client->window());
390 void OBFrame::releaseClient(bool remap)
392 // check if the app has already reparented its window to the root window
394 if (XCheckTypedWindowEvent(otk::OBDisplay::display, _client->window(),
395 ReparentNotify, &ev)) {
396 remap = true; // XXX: why do we remap the window if they already
397 // reparented to root?
399 // according to the ICCCM - if the client doesn't reparent to
400 // root, then we have to do it for them
401 XReparentWindow(otk::OBDisplay::display, _client->window(),
402 _screen->getRootWindow(),
403 _client->area().x(), _client->area().y());
406 // if we want to remap the window, do so now
408 XMapWindow(otk::OBDisplay::display, _client->window());
412 void OBFrame::clientGravity(int &x, int &y)
414 x = _client->area().x();
415 y = _client->area().y();
418 switch (_client->gravity()) {
420 case NorthWestGravity:
421 case SouthWestGravity:
428 x -= (_size.left + _size.right) / 2;
431 case NorthEastGravity:
432 case SouthEastGravity:
434 x -= _size.left + _size.right;
444 switch (_client->gravity()) {
446 case NorthWestGravity:
447 case NorthEastGravity:
454 y -= (_size.top + _size.bottom) / 2;
457 case SouthWestGravity:
458 case SouthEastGravity:
460 y -= _size.top + _size.bottom;
471 void OBFrame::frameGravity(int &x, int &y)
477 switch (_client->gravity()) {
479 case NorthWestGravity:
481 case SouthWestGravity:
486 x += (_size.left + _size.right) / 2;
488 case NorthEastGravity:
490 case SouthEastGravity:
491 x += _size.left + _size.right;
500 switch (_client->gravity()) {
502 case NorthWestGravity:
504 case SouthWestGravity:
509 y += (_size.top + _size.bottom) / 2;
511 case NorthEastGravity:
513 case SouthEastGravity:
514 y += _size.top + _size.bottom;