1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
4 # include "../config.h"
9 #include <X11/extensions/shape.h>
15 #include "otk/display.hh"
21 OBFrame::OBFrame(const OBClient
*client
, const otk::Style
*style
)
23 _screen(otk::OBDisplay::screenInfo(client
->screen()))
28 _decorations
= client
->decorations();
30 // create the base frame parent window
31 _window
= createFrame();
34 // create all of the style element child windows
35 _titlebar
= createChild(_window
, 0);
37 _button_iconify
= createChild(_titlebar
, 0);
38 assert(_button_iconify
);
39 _button_max
= createChild(_titlebar
, 0);
41 _button_stick
= createChild(_titlebar
, 0);
42 assert(_button_stick
);
43 _button_close
= createChild(_titlebar
, 0);
44 assert(_button_close
);
45 _label
= createChild(_titlebar
, 0);
47 XMapSubwindows(otk::OBDisplay::display
, _titlebar
);
49 _handle
= createChild(_window
, 0);
51 _grip_left
= createChild(_handle
, 0);
53 _grip_right
= createChild(_handle
, 0);
55 XMapSubwindows(otk::OBDisplay::display
, _handle
);
66 XDestroyWindow(otk::OBDisplay::display
, _button_iconify
);
67 XDestroyWindow(otk::OBDisplay::display
, _button_max
);
68 XDestroyWindow(otk::OBDisplay::display
, _button_stick
);
69 XDestroyWindow(otk::OBDisplay::display
, _button_close
);
70 XDestroyWindow(otk::OBDisplay::display
, _label
);
71 XDestroyWindow(otk::OBDisplay::display
, _titlebar
);
72 XDestroyWindow(otk::OBDisplay::display
, _grip_left
);
73 XDestroyWindow(otk::OBDisplay::display
, _grip_right
);
74 XDestroyWindow(otk::OBDisplay::display
, _handle
);
78 XDestroyWindow(otk::OBDisplay::display
, _window
);
82 void OBFrame::loadStyle(const otk::Style
*style
)
86 // if a style was previously set, then 'replace' is true, cause we're
88 bool replace
= (_style
);
91 // XXX: do shit here whatever
96 XSetWindowBorderWidth(otk::OBDisplay::display
, _window
,
97 _style
->getBorderWidth());
98 XSetWindowBorder(otk::OBDisplay::display
, _window
,
99 _style
->getBorderColor().pixel());
100 XSetWindowBorderWidth(otk::OBDisplay::display
, _titlebar
,
101 _style
->getBorderWidth());
102 XSetWindowBorder(otk::OBDisplay::display
, _titlebar
,
103 _style
->getBorderColor().pixel());
104 XSetWindowBorderWidth(otk::OBDisplay::display
, _grip_left
,
105 _style
->getBorderWidth());
106 XSetWindowBorder(otk::OBDisplay::display
, _grip_left
,
107 _style
->getBorderColor().pixel());
108 XSetWindowBorderWidth(otk::OBDisplay::display
, _grip_right
,
109 _style
->getBorderWidth());
110 XSetWindowBorder(otk::OBDisplay::display
, _grip_right
,
111 _style
->getBorderColor().pixel());
112 XSetWindowBorderWidth(otk::OBDisplay::display
, _handle
,
113 _style
->getBorderWidth());
114 XSetWindowBorder(otk::OBDisplay::display
, _handle
,
115 _style
->getBorderColor().pixel());
118 XSetWindowBackground(otk::OBDisplay::display
, _window
,
119 _style
->getFrameFocus().color().pixel());
121 // XXX: XSetWindowBackground(otk::OBDisplay::display, _window,
122 // XXX: _style->getFrameUnfocus().color().pixel());
124 // if !replace, then update() will get called after the client is grabbed!
128 // XXX: make everything redraw
133 void OBFrame::update()
135 // XXX: only if not overridden or something!!! MORE LOGIC HERE!!
136 _decorations
= _client
->decorations();
138 int width
; // the width of the client window and the border around it
140 if (_decorations
& OBClient::Decor_Border
) {
141 _size
.left
= _size
.top
= _size
.bottom
= _size
.right
=
142 _style
->getFrameWidth();
143 width
= _client
->area().width() + _style
->getFrameWidth() * 2;
145 _size
.left
= _size
.top
= _size
.bottom
= _size
.right
= 0;
146 width
= _client
->area().width();
149 if (_decorations
& OBClient::Decor_Titlebar
) {
150 // set the titlebar size
151 _titlebar_area
.setRect(-_style
->getBorderWidth(),
152 -_style
->getBorderWidth(),
154 (_style
->getFont().height() +
155 _style
->getBevelWidth() * 2));
156 _size
.top
+= _titlebar_area
.height() + _style
->getBorderWidth();
158 // set the label size
159 _label_area
.setRect(0, _style
->getBevelWidth(),
160 width
, _style
->getFont().height());
161 // set the buttons sizes
162 if (_decorations
& OBClient::Decor_Iconify
)
163 _button_iconify_area
.setRect(0, _style
->getBevelWidth() + 1,
164 _label_area
.height() - 2,
165 _label_area
.height() - 2);
166 if (_decorations
& OBClient::Decor_Maximize
)
167 _button_max_area
.setRect(0, _style
->getBevelWidth() + 1,
168 _label_area
.height() - 2,
169 _label_area
.height() - 2);
170 if (_decorations
& OBClient::Decor_Sticky
)
171 _button_stick_area
.setRect(0, _style
->getBevelWidth() + 1,
172 _label_area
.height() - 2,
173 _label_area
.height() - 2);
174 if (_decorations
& OBClient::Decor_Close
)
175 _button_close_area
.setRect(0, _style
->getBevelWidth() + 1,
176 _label_area
.height() - 2,
177 _label_area
.height() - 2);
179 // separation between titlebar elements
180 const int sep
= _style
->getBevelWidth() + 1;
182 std::string layout
= "ILMC"; // XXX: get this from somewhere
183 // XXX: it is REQUIRED that by this point, the string only has one of each
184 // possible letter, all of the letters are valid, and L exists somewhere in
187 // the size of the label. this ASSUMES the layout has only buttons other
188 // that the ONE LABEL!!
189 // adds an extra sep so that there's a space on either side of the
190 // titlebar.. note: x = sep, below.
191 _label_area
.setWidth(_label_area
.width() -
192 ((_button_iconify_area
.width() + sep
) *
193 (layout
.size() - 1) + sep
));
196 for (int i
= 0, len
= layout
.size(); i
< len
; ++i
) {
200 if (!(_decorations
& OBClient::Decor_Iconify
))
202 area
= &_button_iconify_area
;
208 if (!(_decorations
& OBClient::Decor_Maximize
))
210 area
= &_button_max_area
;
213 if (!(_decorations
& OBClient::Decor_Sticky
))
215 area
= &_button_stick_area
;
218 if (!(_decorations
& OBClient::Decor_Close
))
220 area
= &_button_close_area
;
223 assert(false); // the layout string is invalid!
224 continue; // just to fuck with g++
227 x
+= sep
+ area
->width();
231 if (_decorations
& OBClient::Decor_Handle
) {
232 _handle_area
.setRect(-_style
->getBorderWidth(),
233 _size
.top
+ _client
->area().height() +
234 _style
->getFrameWidth(),
235 width
, _style
->getHandleWidth());
236 _grip_left_area
.setRect(-_style
->getBorderWidth(),
237 -_style
->getBorderWidth(),
238 // XXX: get a Point class in otk and use that for
239 // the 'buttons size' since theyre all the same
240 _button_iconify_area
.width() * 2,
241 _handle_area
.height());
242 _grip_right_area
.setRect(((_handle_area
.right() + 1) -
243 _button_iconify_area
.width() * 2),
244 -_style
->getBorderWidth(),
245 // XXX: get a Point class in otk and use that for
246 // the 'buttons size' since theyre all the same
247 _button_iconify_area
.width() * 2,
248 _handle_area
.height());
249 _size
.bottom
+= _handle_area
.height() + _style
->getBorderWidth();
253 // position/size all the windows
255 XResizeWindow(otk::OBDisplay::display
, _window
,
256 _size
.left
+ _size
.right
+ _client
->area().width(),
257 _size
.top
+ _size
.bottom
+ _client
->area().height());
259 XMoveWindow(otk::OBDisplay::display
, _client
->window(),
260 _size
.left
, _size
.top
);
262 if (_decorations
& OBClient::Decor_Titlebar
) {
263 XMoveResizeWindow(otk::OBDisplay::display
, _titlebar
,
264 _titlebar_area
.x(), _titlebar_area
.y(),
265 _titlebar_area
.width(), _titlebar_area
.height());
266 XMoveResizeWindow(otk::OBDisplay::display
, _label
,
267 _label_area
.x(), _label_area
.y(),
268 _label_area
.width(), _label_area
.height());
269 if (_decorations
& OBClient::Decor_Iconify
)
270 XMoveResizeWindow(otk::OBDisplay::display
, _button_iconify
,
271 _button_iconify_area
.x(), _button_iconify_area
.y(),
272 _button_iconify_area
.width(),
273 _button_iconify_area
.height());
274 if (_decorations
& OBClient::Decor_Maximize
)
275 XMoveResizeWindow(otk::OBDisplay::display
, _button_max
,
276 _button_max_area
.x(), _button_max_area
.y(),
277 _button_max_area
.width(),
278 _button_max_area
.height());
279 if (_decorations
& OBClient::Decor_Sticky
)
280 XMoveResizeWindow(otk::OBDisplay::display
, _button_stick
,
281 _button_stick_area
.x(), _button_stick_area
.y(),
282 _button_stick_area
.width(),
283 _button_stick_area
.height());
284 if (_decorations
& OBClient::Decor_Close
)
285 XMoveResizeWindow(otk::OBDisplay::display
, _button_close
,
286 _button_close_area
.x(), _button_close_area
.y(),
287 _button_close_area
.width(),
288 _button_close_area
.height());
291 if (_decorations
& OBClient::Decor_Handle
) {
292 XMoveResizeWindow(otk::OBDisplay::display
, _handle
,
293 _handle_area
.x(), _handle_area
.y(),
294 _handle_area
.width(), _handle_area
.height());
295 XMoveResizeWindow(otk::OBDisplay::display
, _grip_left
,
296 _grip_left_area
.x(), _grip_left_area
.y(),
297 _grip_left_area
.width(), _grip_left_area
.height());
298 XMoveResizeWindow(otk::OBDisplay::display
, _grip_right
,
299 _grip_right_area
.x(), _grip_right_area
.y(),
300 _grip_right_area
.width(), _grip_right_area
.height());
303 // map/unmap all the windows
304 if (_decorations
& OBClient::Decor_Titlebar
) {
305 XMapWindow(otk::OBDisplay::display
, _label
);
306 if (_decorations
& OBClient::Decor_Iconify
)
307 XMapWindow(otk::OBDisplay::display
, _button_iconify
);
309 XUnmapWindow(otk::OBDisplay::display
, _button_iconify
);
310 if (_decorations
& OBClient::Decor_Maximize
)
311 XMapWindow(otk::OBDisplay::display
, _button_max
);
313 XUnmapWindow(otk::OBDisplay::display
, _button_max
);
314 if (_decorations
& OBClient::Decor_Sticky
)
315 XMapWindow(otk::OBDisplay::display
, _button_stick
);
317 XUnmapWindow(otk::OBDisplay::display
, _button_stick
);
318 if (_decorations
& OBClient::Decor_Close
)
319 XMapWindow(otk::OBDisplay::display
, _button_close
);
321 XUnmapWindow(otk::OBDisplay::display
, _button_close
);
322 XMapWindow(otk::OBDisplay::display
, _titlebar
);
324 XUnmapWindow(otk::OBDisplay::display
, _titlebar
);
325 XUnmapWindow(otk::OBDisplay::display
, _label
);
326 XUnmapWindow(otk::OBDisplay::display
, _button_iconify
);
327 XUnmapWindow(otk::OBDisplay::display
, _button_max
);
328 XUnmapWindow(otk::OBDisplay::display
, _button_stick
);
329 XUnmapWindow(otk::OBDisplay::display
, _button_close
);
332 if (_decorations
& OBClient::Decor_Handle
) {
333 XMapWindow(otk::OBDisplay::display
, _grip_left
);
334 XMapWindow(otk::OBDisplay::display
, _grip_right
);
335 XMapWindow(otk::OBDisplay::display
, _handle
);
337 XUnmapWindow(otk::OBDisplay::display
, _handle
);
338 XUnmapWindow(otk::OBDisplay::display
, _grip_left
);
339 XUnmapWindow(otk::OBDisplay::display
, _grip_right
);
342 // XXX: more is gunna have to happen here
348 void OBFrame::updateShape()
351 if (!_client
->shaped()) {
352 // clear the shape on the frame window
353 XShapeCombineMask(otk::OBDisplay::display
, _window
, ShapeBounding
,
358 // make the frame's shape match the clients
359 XShapeCombineShape(otk::OBDisplay::display
, _window
, ShapeBounding
,
362 _client
->window(), ShapeBounding
, ShapeSet
);
368 if (decorations & Decor_Titlebar) {
369 xrect[0].x = xrect[0].y = -frame.border_w;
370 xrect[0].width = frame.rect.width();
371 xrect[0].height = frame.title_h + (frame.border_w * 2);
375 if (decorations & Decor_Handle) {
376 xrect[1].x = -frame.border_w;
377 xrect[1].y = frame.rect.height() - frame.margin.bottom +
378 frame.mwm_border_w - frame.border_w;
379 xrect[1].width = frame.rect.width();
380 xrect[1].height = frame.handle_h + (frame.border_w * 2);
384 XShapeCombineRectangles(otk::OBDisplay::display
, _window
,
385 ShapeBounding
, 0, 0, xrect
, num
,
386 ShapeUnion
, Unsorted
);
392 void OBFrame::grabClient()
395 XGrabServer(otk::OBDisplay::display
);
397 // select the event mask on the frame
398 XSelectInput(otk::OBDisplay::display
, _window
, SubstructureRedirectMask
);
400 // reparent the client to the frame
401 XSelectInput(otk::OBDisplay::display
, _client
->window(),
402 OBClient::event_mask
& ~StructureNotifyMask
);
403 XReparentWindow(otk::OBDisplay::display
, _client
->window(), _window
, 0, 0);
404 XSelectInput(otk::OBDisplay::display
, _client
->window(),
405 OBClient::event_mask
);
407 // raise the client above the frame
408 XRaiseWindow(otk::OBDisplay::display
, _client
->window());
409 // map the client so it maps when the frame does
410 XMapWindow(otk::OBDisplay::display
, _client
->window());
412 XUngrabServer(otk::OBDisplay::display
);
418 void OBFrame::releaseClient(bool remap
)
420 // check if the app has already reparented its window to the root window
422 if (XCheckTypedWindowEvent(otk::OBDisplay::display
, _client
->window(),
423 ReparentNotify
, &ev
)) {
424 remap
= true; // XXX: why do we remap the window if they already
425 // reparented to root?
427 // according to the ICCCM - if the client doesn't reparent to
428 // root, then we have to do it for them
429 XReparentWindow(otk::OBDisplay::display
, _client
->window(),
430 _screen
->getRootWindow(),
431 _client
->area().x(), _client
->area().y());
434 // if we want to remap the window, do so now
436 XMapWindow(otk::OBDisplay::display
, _client
->window());
440 Window
OBFrame::createChild(Window parent
, Cursor cursor
)
442 XSetWindowAttributes attrib_create
;
443 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWEventMask
;
445 attrib_create
.background_pixmap
= None
;
446 attrib_create
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
447 ButtonMotionMask
| ExposureMask
;
450 create_mask
|= CWCursor
;
451 attrib_create
.cursor
= cursor
;
454 Window w
= XCreateWindow(otk::OBDisplay::display
, parent
, 0, 0, 1, 1, 0,
455 _screen
->getDepth(), InputOutput
,
456 _screen
->getVisual(), create_mask
, &attrib_create
);
457 XRaiseWindow(otk::OBDisplay::display
, w
); // raise above the parent
462 Window
OBFrame::createFrame()
464 XSetWindowAttributes attrib_create
;
465 unsigned long create_mask
= CWBackPixmap
| CWBorderPixel
| CWColormap
|
466 CWOverrideRedirect
| CWEventMask
;
468 attrib_create
.background_pixmap
= None
;
469 attrib_create
.colormap
= _screen
->getColormap();
470 attrib_create
.override_redirect
= True
;
471 attrib_create
.event_mask
= EnterWindowMask
| LeaveWindowMask
| ButtonPress
;
473 We catch button presses because other wise they get passed down to the
474 root window, which will then cause root menus to show when you click the
478 return XCreateWindow(otk::OBDisplay::display
, _screen
->getRootWindow(),
480 _screen
->getDepth(), InputOutput
, _screen
->getVisual(),
481 create_mask
, &attrib_create
);