]> icculus.org git repositories - mikachu/openbox.git/blob - src/Window.cc
added BScreen::availableArea() which returns a rect specifying where windows can...
[mikachu/openbox.git] / src / Window.cc
1 // Window.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #define   _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 #  include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #include <X11/Xatom.h>
34 #include <X11/keysym.h>
35
36 #ifdef    HAVE_STRING_H
37 #  include <string.h>
38 #endif // HAVE_STRING_H
39
40 #ifdef    DEBUG
41 #  ifdef    HAVE_STDIO_H
42 #    include <stdio.h>
43 #  endif // HAVE_STDIO_H
44 #endif // DEBUG
45
46 #include "i18n.h"
47 #include "openbox.h"
48 #include "Iconmenu.h"
49 #include "Screen.h"
50 #include "Toolbar.h"
51 #include "Window.h"
52 #include "Windowmenu.h"
53 #include "Workspace.h"
54 #ifdef    SLIT
55 #  include "Slit.h"
56 #endif // SLIT
57 #include "Util.h"
58
59 /*
60  * Initializes the class with default values/the window's set initial values.
61  */
62 OpenboxWindow::OpenboxWindow(Openbox &o, Window w, BScreen *s) : openbox(o) {
63 #ifdef    DEBUG
64   fprintf(stderr, i18n->getMessage(WindowSet, WindowCreating,
65                      "OpenboxWindow::OpenboxWindow(): creating 0x%lx\n"),
66           w);
67 #endif // DEBUG
68
69   client.window = w;
70   display = openbox.getXDisplay();
71
72   openbox.grab();
73   if (! validateClient()) return;
74
75   // fetch client size and placement
76   XWindowAttributes wattrib;
77   if ((! XGetWindowAttributes(display, client.window, &wattrib)) ||
78       (! wattrib.screen) || wattrib.override_redirect) {
79 #ifdef    DEBUG
80     fprintf(stderr,
81             i18n->getMessage(WindowSet, WindowXGetWindowAttributesFail,
82                "OpenboxWindow::OpenboxWindow(): XGetWindowAttributes "
83                "failed\n"));
84 #endif // DEBUG
85
86     openbox.ungrab();
87     return;
88   }
89
90   if (s) {
91     screen = s;
92   } else {
93     screen = openbox.searchScreen(RootWindowOfScreen(wattrib.screen));
94     if (! screen) {
95 #ifdef    DEBUG
96       fprintf(stderr, i18n->getMessage(WindowSet, WindowCannotFindScreen,
97                       "OpenboxWindow::OpenboxWindow(): can't find screen\n"
98                       "\tfor root window 0x%lx\n"),
99                       RootWindowOfScreen(wattrib.screen));
100 #endif // DEBUG
101
102       openbox.ungrab();
103       return;
104     }
105   }
106
107   flags.moving = flags.resizing = flags.shaded = flags.visible =
108     flags.iconic = flags.transient = flags.focused =
109     flags.stuck = flags.modal =  flags.send_focus_message =
110     flags.shaped = flags.managed = False;
111   flags.maximized = 0;
112
113   openbox_attrib.workspace = workspace_number = window_number = -1;
114
115   openbox_attrib.flags = openbox_attrib.attrib = openbox_attrib.stack
116     = openbox_attrib.decoration = 0l;
117   openbox_attrib.premax_x = openbox_attrib.premax_y = 0;
118   openbox_attrib.premax_w = openbox_attrib.premax_h = 0;
119
120   frame.window = frame.plate = frame.title = frame.handle = None;
121   frame.close_button = frame.iconify_button = frame.maximize_button = None;
122   frame.right_grip = frame.left_grip = None;
123
124   frame.utitle = frame.ftitle = frame.uhandle = frame.fhandle = None;
125   frame.ulabel = frame.flabel = frame.ubutton = frame.fbutton = None;
126   frame.pbutton = frame.ugrip = frame.fgrip = None;
127
128   decorations.titlebar = decorations.border = decorations.handle = True;
129   decorations.iconify = decorations.maximize = decorations.menu = True;
130   functions.resize = functions.move = functions.iconify =
131     functions.maximize = True;
132   functions.close = decorations.close = False;
133
134   client.wm_hint_flags = client.normal_hint_flags = 0;
135   client.transient_for = client.transient = 0;
136   client.title = 0;
137   client.title_len = 0;
138   client.icon_title = 0;
139   client.mwm_hint = (MwmHints *) 0;
140   client.openbox_hint = (OpenboxHints *) 0;
141
142   // get the initial size and location of client window (relative to the
143   // _root window_). This position is the reference point used with the
144   // window's gravity to find the window's initial position.
145   client.x = wattrib.x;
146   client.y = wattrib.y;
147   client.width = wattrib.width;
148   client.height = wattrib.height;
149   client.old_bw = wattrib.border_width;
150
151   windowmenu = 0;
152   lastButtonPressTime = 0;
153   image_ctrl = screen->getImageControl();
154
155   timer = new BTimer(openbox, *this);
156   timer->setTimeout(openbox.getAutoRaiseDelay());
157   timer->fireOnce(True);
158
159   getOpenboxHints();
160   if (! client.openbox_hint)
161     getMWMHints();
162
163   // get size, aspect, minimum/maximum size and other hints set by the
164   // client
165   getWMProtocols();
166   getWMHints();
167   getWMNormalHints();
168
169 #ifdef    SLIT
170   if (client.initial_state == WithdrawnState) {
171     screen->getSlit()->addClient(client.window);
172     openbox.ungrab();
173     delete this;
174     return;
175   }
176 #endif // SLIT
177
178   flags.managed = True;
179   openbox.saveWindowSearch(client.window, this);
180
181   // determine if this is a transient window
182   Window win;
183   if (XGetTransientForHint(display, client.window, &win)) {
184     if (win && (win != client.window)) {
185       OpenboxWindow *tr;
186       if ((tr = openbox.searchWindow(win))) {
187         while (tr->client.transient) tr = tr->client.transient;
188         client.transient_for = tr;
189         tr->client.transient = this;
190         flags.stuck = client.transient_for->flags.stuck;
191         flags.transient = True;
192       } else if (win == client.window_group) {
193         if ((tr = openbox.searchGroup(win, this))) {
194           while (tr->client.transient) tr = tr->client.transient;
195           client.transient_for = tr;
196           tr->client.transient = this;
197           flags.stuck = client.transient_for->flags.stuck;
198           flags.transient = True;
199         }
200       }
201     }
202
203     if (win == screen->getRootWindow()) flags.modal = True;
204   }
205
206   // adjust the window decorations based on transience and window sizes
207   if (flags.transient)
208     decorations.maximize = decorations.handle = functions.maximize = False;
209
210   if ((client.normal_hint_flags & PMinSize) &&
211       (client.normal_hint_flags & PMaxSize) &&
212        client.max_width <= client.min_width &&
213       client.max_height <= client.min_height) {
214     decorations.maximize = decorations.handle =
215       functions.resize = functions.maximize = False;
216   }
217   upsize();
218
219   Bool place_window = True;
220   if (openbox.isStartup() || flags.transient ||
221       client.normal_hint_flags & (PPosition|USPosition)) {
222     setGravityOffsets();
223
224     if ((openbox.isStartup()) ||
225         (frame.x >= 0 &&
226          (signed) (frame.y + frame.y_border) >= 0 &&
227          frame.x <= (signed) screen->size().w() &&
228          frame.y <= (signed) screen->size().h()))
229       place_window = False;
230   }
231
232   frame.window = createToplevelWindow(frame.x, frame.y, frame.width,
233                                       frame.height,
234                                       frame.border_w);
235   openbox.saveWindowSearch(frame.window, this);
236
237   frame.plate = createChildWindow(frame.window);
238   openbox.saveWindowSearch(frame.plate, this);
239
240   if (decorations.titlebar) {
241     frame.title = createChildWindow(frame.window);
242     frame.label = createChildWindow(frame.title);
243     openbox.saveWindowSearch(frame.title, this);
244     openbox.saveWindowSearch(frame.label, this);
245   }
246
247   if (decorations.handle) {
248     frame.handle = createChildWindow(frame.window);
249     openbox.saveWindowSearch(frame.handle, this);
250
251     frame.left_grip =
252       createChildWindow(frame.handle, openbox.getLowerLeftAngleCursor());
253     openbox.saveWindowSearch(frame.left_grip, this);
254
255     frame.right_grip =
256       createChildWindow(frame.handle, openbox.getLowerRightAngleCursor());
257     openbox.saveWindowSearch(frame.right_grip, this);
258   }
259
260   associateClientWindow();
261
262   if (! screen->sloppyFocus())
263     openbox.grabButton(Button1, 0, frame.plate, True, ButtonPressMask,
264         GrabModeSync, GrabModeSync, None, None);
265
266   openbox.grabButton(Button1, Mod1Mask, frame.window, True,
267       ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
268       GrabModeAsync, None, openbox.getMoveCursor());
269   openbox.grabButton(Button2, Mod1Mask, frame.window, True,
270       ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None, None);
271   openbox.grabButton(Button3, Mod1Mask, frame.window, True,
272       ButtonReleaseMask | ButtonMotionMask, GrabModeAsync,
273       GrabModeAsync, None, None);
274
275   positionWindows();
276   XRaiseWindow(display, frame.plate);
277   XMapSubwindows(display, frame.plate);
278   if (decorations.titlebar) XMapSubwindows(display, frame.title);
279   XMapSubwindows(display, frame.window);
280
281   if (decorations.menu)
282     windowmenu = new Windowmenu(*this);
283
284   decorate();
285
286   if (workspace_number < 0 || workspace_number >= screen->getWorkspaceCount())
287     screen->getCurrentWorkspace()->addWindow(this, place_window);
288   else
289     screen->getWorkspace(workspace_number)->addWindow(this, place_window);
290
291   configure(frame.x, frame.y, frame.width, frame.height);
292
293   if (flags.shaded) {
294     flags.shaded = False;
295     shade();
296   }
297
298   if (flags.maximized && functions.maximize) {
299     unsigned int button = flags.maximized;
300     flags.maximized = 0;
301     maximize(button);
302   }
303
304   setFocusFlag(False);
305
306   openbox.ungrab();
307 }
308
309
310 OpenboxWindow::~OpenboxWindow(void) {
311   if (flags.moving || flags.resizing) {
312     screen->hideGeometry();
313     XUngrabPointer(display, CurrentTime);
314   }
315
316   if (workspace_number != -1 && window_number != -1)
317     screen->getWorkspace(workspace_number)->removeWindow(this);
318   else if (flags.iconic)
319     screen->removeIcon(this);
320
321   if (timer) {
322     if (timer->isTiming()) timer->stop();
323     delete timer;
324   }
325
326   if (windowmenu) delete windowmenu;
327
328   if (client.title)
329     delete [] client.title;
330
331   if (client.icon_title)
332     delete [] client.icon_title;
333
334   if (client.mwm_hint)
335     XFree(client.mwm_hint);
336
337   if (client.openbox_hint)
338     XFree(client.openbox_hint);
339
340   if (client.window_group)
341     openbox.removeGroupSearch(client.window_group);
342
343   if (flags.transient && client.transient_for)
344     client.transient_for->client.transient = client.transient;
345   if (client.transient)
346     client.transient->client.transient_for = client.transient_for;
347
348   if (frame.close_button) {
349     openbox.removeWindowSearch(frame.close_button);
350     XDestroyWindow(display, frame.close_button);
351   }
352
353   if (frame.iconify_button) {
354     openbox.removeWindowSearch(frame.iconify_button);
355     XDestroyWindow(display, frame.iconify_button);
356   }
357
358   if (frame.maximize_button) {
359     openbox.removeWindowSearch(frame.maximize_button);
360     XDestroyWindow(display, frame.maximize_button);
361   }
362
363   if (frame.title) {
364     if (frame.ftitle)
365       image_ctrl->removeImage(frame.ftitle);
366
367     if (frame.utitle)
368       image_ctrl->removeImage(frame.utitle);
369
370     if (frame.flabel)
371       image_ctrl->removeImage(frame.flabel);
372
373     if( frame.ulabel)
374       image_ctrl->removeImage(frame.ulabel);
375
376     openbox.removeWindowSearch(frame.label);
377     openbox.removeWindowSearch(frame.title);
378     XDestroyWindow(display, frame.label);
379     XDestroyWindow(display, frame.title);
380   }
381
382   if (frame.handle) {
383     if (frame.fhandle)
384       image_ctrl->removeImage(frame.fhandle);
385
386     if (frame.uhandle)
387       image_ctrl->removeImage(frame.uhandle);
388
389     if (frame.fgrip)
390       image_ctrl->removeImage(frame.fgrip);
391
392     if (frame.ugrip)
393       image_ctrl->removeImage(frame.ugrip);
394
395     openbox.removeWindowSearch(frame.handle);
396     openbox.removeWindowSearch(frame.right_grip);
397     openbox.removeWindowSearch(frame.left_grip);
398     XDestroyWindow(display, frame.right_grip);
399     XDestroyWindow(display, frame.left_grip);
400     XDestroyWindow(display, frame.handle);
401   }
402
403   if (frame.fbutton)
404     image_ctrl->removeImage(frame.fbutton);
405
406   if (frame.ubutton)
407     image_ctrl->removeImage(frame.ubutton);
408
409   if (frame.pbutton)
410     image_ctrl->removeImage(frame.pbutton);
411
412   if (frame.plate) {
413     openbox.removeWindowSearch(frame.plate);
414     XDestroyWindow(display, frame.plate);
415   }
416
417   if (frame.window) {
418     openbox.removeWindowSearch(frame.window);
419     XDestroyWindow(display, frame.window);
420   }
421
422   if (flags.managed) {
423     openbox.removeWindowSearch(client.window);
424     screen->removeNetizen(client.window);
425   }
426 }
427
428
429 /*
430  * Creates a new top level window, with a given location, size, and border
431  * width.
432  * Returns: the newly created window
433  */
434 Window OpenboxWindow::createToplevelWindow(int x, int y, unsigned int width,
435                                             unsigned int height,
436                                             unsigned int borderwidth)
437 {
438   XSetWindowAttributes attrib_create;
439   unsigned long create_mask = CWBackPixmap | CWBorderPixel | CWColormap |
440                               CWOverrideRedirect | CWEventMask;
441
442   attrib_create.background_pixmap = None;
443   attrib_create.colormap = screen->getColormap();
444   attrib_create.override_redirect = True;
445   attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
446                              ButtonMotionMask | EnterWindowMask;
447
448   return XCreateWindow(display, screen->getRootWindow(), x, y, width, height,
449                         borderwidth, screen->getDepth(), InputOutput,
450                         screen->getVisual(), create_mask,
451                         &attrib_create);
452 }
453
454
455 /*
456  * Creates a child window, and optionally associates a given cursor with
457  * the new window.
458  */
459 Window OpenboxWindow::createChildWindow(Window parent, Cursor cursor) {
460   XSetWindowAttributes attrib_create;
461   unsigned long create_mask = CWBackPixmap | CWBorderPixel |
462                               CWEventMask;
463
464   attrib_create.background_pixmap = None;
465   attrib_create.event_mask = ButtonPressMask | ButtonReleaseMask |
466                              ButtonMotionMask | ExposureMask |
467                              EnterWindowMask | LeaveWindowMask;
468
469   if (cursor) {
470     create_mask |= CWCursor;
471     attrib_create.cursor = cursor;
472   }
473
474   return XCreateWindow(display, parent, 0, 0, 1, 1, 0, screen->getDepth(),
475                        InputOutput, screen->getVisual(), create_mask,
476                        &attrib_create);
477 }
478
479
480 void OpenboxWindow::associateClientWindow(void) {
481   XSetWindowBorderWidth(display, client.window, 0);
482   getWMName();
483   getWMIconName();
484
485   XChangeSaveSet(display, client.window, SetModeInsert);
486   XSetWindowAttributes attrib_set;
487
488   XSelectInput(display, frame.plate, NoEventMask);
489   XReparentWindow(display, client.window, frame.plate, 0, 0);
490   XSelectInput(display, frame.plate, SubstructureRedirectMask);
491
492   XFlush(display);
493
494   attrib_set.event_mask = PropertyChangeMask | StructureNotifyMask |
495                           FocusChangeMask;
496   attrib_set.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask |
497                                      ButtonMotionMask;
498
499   XChangeWindowAttributes(display, client.window, CWEventMask|CWDontPropagate,
500                           &attrib_set);
501
502 #ifdef    SHAPE
503   if (openbox.hasShapeExtensions()) {
504     XShapeSelectInput(display, client.window, ShapeNotifyMask);
505
506     int foo;
507     unsigned int ufoo;
508
509     XShapeQueryExtents(display, client.window, &flags.shaped, &foo, &foo,
510                        &ufoo, &ufoo, &foo, &foo, &foo, &ufoo, &ufoo);
511
512     if (flags.shaped) {
513       XShapeCombineShape(display, frame.window, ShapeBounding,
514                          frame.mwm_border_w, frame.y_border +
515                          frame.mwm_border_w, client.window,
516                          ShapeBounding, ShapeSet);
517
518       int num = 1;
519       XRectangle xrect[2];
520       xrect[0].x = xrect[0].y = 0;
521       xrect[0].width = frame.width;
522       xrect[0].height = frame.y_border;
523
524       if (decorations.handle) {
525         xrect[1].x = 0;
526         xrect[1].y = frame.y_handle;
527         xrect[1].width = frame.width;
528         xrect[1].height = frame.handle_h + frame.border_w;
529         num++;
530       }
531
532       XShapeCombineRectangles(display, frame.window, ShapeBounding, 0, 0,
533                               xrect, num, ShapeUnion, Unsorted);
534     }
535   }
536 #endif // SHAPE
537
538   if (decorations.iconify) createIconifyButton();
539   if (decorations.maximize) createMaximizeButton();
540   if (decorations.close) createCloseButton();
541
542   if (frame.ubutton) {
543     if (frame.close_button)
544       XSetWindowBackgroundPixmap(display, frame.close_button, frame.ubutton);
545     if (frame.maximize_button)
546       XSetWindowBackgroundPixmap(display, frame.maximize_button,
547                                  frame.ubutton);
548     if (frame.iconify_button)
549       XSetWindowBackgroundPixmap(display, frame.iconify_button, frame.ubutton);
550   } else {
551     if (frame.close_button)
552       XSetWindowBackground(display, frame.close_button, frame.ubutton_pixel);
553     if (frame.maximize_button)
554       XSetWindowBackground(display, frame.maximize_button,
555                            frame.ubutton_pixel);
556     if (frame.iconify_button)
557       XSetWindowBackground(display, frame.iconify_button, frame.ubutton_pixel);
558   }
559 }
560
561
562 void OpenboxWindow::decorate(void) {
563   Pixmap tmp = frame.fbutton;
564   BTexture *texture = &(screen->getWindowStyle()->b_focus);
565   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
566     frame.fbutton = None;
567     frame.fbutton_pixel = texture->getColor()->getPixel();
568   } else {
569     frame.fbutton =
570       image_ctrl->renderImage(frame.button_w, frame.button_h, texture);
571   }
572   if (tmp) image_ctrl->removeImage(tmp);
573
574   tmp = frame.ubutton;
575   texture = &(screen->getWindowStyle()->b_unfocus);
576   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
577     frame.ubutton = None;
578     frame.ubutton_pixel = texture->getColor()->getPixel();
579   } else {
580     frame.ubutton =
581       image_ctrl->renderImage(frame.button_w, frame.button_h, texture);
582   }
583   if (tmp) image_ctrl->removeImage(tmp);
584
585   tmp = frame.pbutton;
586   texture = &(screen->getWindowStyle()->b_pressed);
587   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
588     frame.pbutton = None;
589     frame.pbutton_pixel = texture->getColor()->getPixel();
590   } else {
591     frame.pbutton =
592       image_ctrl->renderImage(frame.button_w, frame.button_h, texture);
593   }
594   if (tmp) image_ctrl->removeImage(tmp);
595
596   if (decorations.titlebar) {
597     tmp = frame.ftitle;
598     texture = &(screen->getWindowStyle()->t_focus);
599     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
600       frame.ftitle = None;
601       frame.ftitle_pixel = texture->getColor()->getPixel();
602     } else {
603       frame.ftitle =
604         image_ctrl->renderImage(frame.width, frame.title_h, texture);
605     }
606     if (tmp) image_ctrl->removeImage(tmp);
607
608     tmp = frame.utitle;
609     texture = &(screen->getWindowStyle()->t_unfocus);
610     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
611       frame.utitle = None;
612       frame.utitle_pixel = texture->getColor()->getPixel();
613     } else {
614       frame.utitle =
615         image_ctrl->renderImage(frame.width, frame.title_h, texture);
616     }
617     if (tmp) image_ctrl->removeImage(tmp);
618
619     XSetWindowBorder(display, frame.title,
620                      screen->getBorderColor()->getPixel());
621
622     decorateLabel();
623   }
624
625   if (decorations.border) {
626     frame.fborder_pixel = screen->getWindowStyle()->f_focus.getPixel();
627     frame.uborder_pixel = screen->getWindowStyle()->f_unfocus.getPixel();
628     openbox_attrib.flags |= AttribDecoration;
629     openbox_attrib.decoration = DecorNormal;
630   } else {
631     openbox_attrib.flags |= AttribDecoration;
632     openbox_attrib.decoration = DecorNone;
633   }
634
635   if (decorations.handle) {
636     tmp = frame.fhandle;
637     texture = &(screen->getWindowStyle()->h_focus);
638     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
639       frame.fhandle = None;
640       frame.fhandle_pixel = texture->getColor()->getPixel();
641     } else {
642       frame.fhandle =
643         image_ctrl->renderImage(frame.width, frame.handle_h, texture);
644     }
645     if (tmp) image_ctrl->removeImage(tmp);
646
647     tmp = frame.uhandle;
648     texture = &(screen->getWindowStyle()->h_unfocus);
649     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
650       frame.uhandle = None;
651       frame.uhandle_pixel = texture->getColor()->getPixel();
652     } else {
653       frame.uhandle =
654         image_ctrl->renderImage(frame.width, frame.handle_h, texture);
655     }
656     if (tmp) image_ctrl->removeImage(tmp);
657
658     tmp = frame.fgrip;
659     texture = &(screen->getWindowStyle()->g_focus);
660     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
661       frame.fgrip = None;
662       frame.fgrip_pixel = texture->getColor()->getPixel();
663     } else {
664       frame.fgrip =
665         image_ctrl->renderImage(frame.grip_w, frame.grip_h, texture);
666     }
667     if (tmp) image_ctrl->removeImage(tmp);
668
669     tmp = frame.ugrip;
670     texture = &(screen->getWindowStyle()->g_unfocus);
671     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
672       frame.ugrip = None;
673       frame.ugrip_pixel = texture->getColor()->getPixel();
674     } else {
675       frame.ugrip =
676         image_ctrl->renderImage(frame.grip_w, frame.grip_h, texture);
677     }
678     if (tmp) image_ctrl->removeImage(tmp);
679
680     XSetWindowBorder(display, frame.handle,
681                      screen->getBorderColor()->getPixel());
682     XSetWindowBorder(display, frame.left_grip,
683                      screen->getBorderColor()->getPixel());
684     XSetWindowBorder(display, frame.right_grip,
685                      screen->getBorderColor()->getPixel());
686   }
687
688   XSetWindowBorder(display, frame.window,
689                    screen->getBorderColor()->getPixel());
690 }
691
692
693 void OpenboxWindow::decorateLabel(void) {
694   Pixmap tmp = frame.flabel;
695   BTexture *texture = &(screen->getWindowStyle()->l_focus);
696   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
697     frame.flabel = None;
698     frame.flabel_pixel = texture->getColor()->getPixel();
699   } else {
700     frame.flabel =
701       image_ctrl->renderImage(frame.label_w, frame.label_h, texture);
702   }
703   if (tmp) image_ctrl->removeImage(tmp);
704
705   tmp = frame.ulabel;
706   texture = &(screen->getWindowStyle()->l_unfocus);
707   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
708     frame.ulabel = None;
709     frame.ulabel_pixel = texture->getColor()->getPixel();
710   } else {
711     frame.ulabel =
712       image_ctrl->renderImage(frame.label_w, frame.label_h, texture);
713   }
714   if (tmp) image_ctrl->removeImage(tmp);
715 }
716
717
718 void OpenboxWindow::createCloseButton(void) {
719   if (decorations.close && frame.title != None) {
720     frame.close_button = createChildWindow(frame.title);
721     openbox.saveWindowSearch(frame.close_button, this);
722   }
723 }
724
725
726 void OpenboxWindow::createIconifyButton(void) {
727   if (decorations.iconify && frame.title != None) {
728     frame.iconify_button = createChildWindow(frame.title);
729     openbox.saveWindowSearch(frame.iconify_button, this);
730   }
731 }
732
733
734 void OpenboxWindow::createMaximizeButton(void) {
735   if (decorations.maximize && frame.title != None) {
736     frame.maximize_button = createChildWindow(frame.title);
737     openbox.saveWindowSearch(frame.maximize_button, this);
738   }
739 }
740
741
742 void OpenboxWindow::positionButtons(Bool redecorate_label) {
743   const char *format = openbox.getTitleBarLayout();
744   const unsigned int bw = frame.bevel_w + 1;
745   const unsigned int by = frame.bevel_w + 1;
746   unsigned int bx = frame.bevel_w + 1;
747   unsigned int bcount = strlen(format) - 1;
748
749   if (!decorations.close)
750     bcount--;
751   if (!decorations.maximize)
752     bcount--;
753   if (!decorations.iconify)
754     bcount--;
755   frame.label_w = frame.width - bx * 2 - (frame.button_w + bw) * bcount;
756   
757   bool hasclose, hasiconify, hasmaximize;
758   hasclose = hasiconify = hasmaximize = false;
759  
760   for (int i = 0; format[i] != '\0' && i < 4; i++) {
761     switch(format[i]) {
762     case 'C':
763       if (decorations.close && frame.close_button != None) {
764         XMoveResizeWindow(display, frame.close_button, bx, by,
765                           frame.button_w, frame.button_h);
766         XMapWindow(display, frame.close_button);
767         XClearWindow(display, frame.close_button);
768         bx += frame.button_w + bw;
769         hasclose = true;
770       } else if (frame.close_button)
771         XUnmapWindow(display, frame.close_button);
772       break;
773     case 'I':
774       if (decorations.iconify && frame.iconify_button != None) {
775         XMoveResizeWindow(display, frame.iconify_button, bx, by,
776                           frame.button_w, frame.button_h);
777         XMapWindow(display, frame.iconify_button);
778         XClearWindow(display, frame.iconify_button);
779         bx += frame.button_w + bw;
780         hasiconify = true;
781       } else if (frame.close_button)
782         XUnmapWindow(display, frame.close_button);
783       break;
784     case 'M':
785       if (decorations.maximize && frame.maximize_button != None) {
786         XMoveResizeWindow(display, frame.maximize_button, bx, by,
787                           frame.button_w, frame.button_h);
788         XMapWindow(display, frame.maximize_button);
789         XClearWindow(display, frame.maximize_button);
790         bx += frame.button_w + bw;
791         hasmaximize = true;
792       } else if (frame.close_button)
793         XUnmapWindow(display, frame.close_button);
794       break;
795     case 'L':
796       XMoveResizeWindow(display, frame.label, bx, by - 1,
797                         frame.label_w, frame.label_h);
798       bx += frame.label_w + bw;
799       break;
800     }
801   }
802
803   if (!hasclose) {
804       openbox.removeWindowSearch(frame.close_button);
805       XDestroyWindow(display, frame.close_button);     
806   }
807   if (!hasiconify) {
808       openbox.removeWindowSearch(frame.iconify_button);
809       XDestroyWindow(display, frame.iconify_button);
810   }
811   if (!hasmaximize) {
812       openbox.removeWindowSearch(frame.maximize_button);
813       XDestroyWindow(display, frame.maximize_button);                 
814   }
815   if (redecorate_label)
816     decorateLabel();
817   redrawLabel();
818   redrawAllButtons();
819 }
820
821
822 void OpenboxWindow::reconfigure(void) {
823   upsize();
824
825   client.x = frame.x + frame.mwm_border_w + frame.border_w;
826   client.y = frame.y + frame.y_border + frame.mwm_border_w +
827              frame.border_w;
828
829   if (client.title) {
830     if (i18n->multibyte()) {
831       XRectangle ink, logical;
832       XmbTextExtents(screen->getWindowStyle()->fontset,
833                      client.title, client.title_len, &ink, &logical);
834       client.title_text_w = logical.width;
835     } else {
836       client.title_text_w = XTextWidth(screen->getWindowStyle()->font,
837                                        client.title, client.title_len);
838     }
839     client.title_text_w += (frame.bevel_w * 4);
840   }
841
842   positionWindows();
843   decorate();
844
845   XClearWindow(display, frame.window);
846   setFocusFlag(flags.focused);
847
848   configure(frame.x, frame.y, frame.width, frame.height);
849
850   if (! screen->sloppyFocus())
851     openbox.grabButton(Button1, 0, frame.plate, True, ButtonPressMask,
852         GrabModeSync, GrabModeSync, None, None);
853   else
854     openbox.ungrabButton(Button1, 0, frame.plate);
855
856   if (windowmenu) {
857     windowmenu->move(windowmenu->getX(), frame.y + frame.title_h);
858     windowmenu->reconfigure();
859   }
860 }
861
862
863 void OpenboxWindow::positionWindows(void) {
864   XResizeWindow(display, frame.window, frame.width,
865                 ((flags.shaded) ? frame.title_h : frame.height));
866   XSetWindowBorderWidth(display, frame.window, frame.border_w);
867   XSetWindowBorderWidth(display, frame.plate, frame.mwm_border_w);
868   XMoveResizeWindow(display, frame.plate, 0, frame.y_border,
869                     client.width, client.height);
870   XMoveResizeWindow(display, client.window, 0, 0, client.width, client.height);
871
872   if (decorations.titlebar) {
873     XSetWindowBorderWidth(display, frame.title, frame.border_w);
874     XMoveResizeWindow(display, frame.title, -frame.border_w,
875                       -frame.border_w, frame.width, frame.title_h);
876
877     positionButtons();
878   } else if (frame.title) {
879     XUnmapWindow(display, frame.title);
880   }
881   if (decorations.handle) {
882     XSetWindowBorderWidth(display, frame.handle, frame.border_w);
883     XSetWindowBorderWidth(display, frame.left_grip, frame.border_w);
884     XSetWindowBorderWidth(display, frame.right_grip, frame.border_w);
885
886     XMoveResizeWindow(display, frame.handle, -frame.border_w,
887                       frame.y_handle - frame.border_w,
888                       frame.width, frame.handle_h);
889     XMoveResizeWindow(display, frame.left_grip, -frame.border_w,
890                       -frame.border_w, frame.grip_w, frame.grip_h);
891     XMoveResizeWindow(display, frame.right_grip,
892                       frame.width - frame.grip_w - frame.border_w,
893                       -frame.border_w, frame.grip_w, frame.grip_h);
894     XMapSubwindows(display, frame.handle);
895   } else if (frame.handle) {
896     XUnmapWindow(display, frame.handle);
897   }
898 }
899
900
901 void OpenboxWindow::getWMName(void) {
902   if (client.title) {
903     delete [] client.title;
904     client.title = (char *) 0;
905   }
906
907   XTextProperty text_prop;
908   char **list;
909   int num;
910
911   if (XGetWMName(display, client.window, &text_prop)) {
912     if (text_prop.value && text_prop.nitems > 0) {
913       if (text_prop.encoding != XA_STRING) {
914         text_prop.nitems = strlen((char *) text_prop.value);
915
916         if ((XmbTextPropertyToTextList(display, &text_prop,
917                                        &list, &num) == Success) &&
918             (num > 0) && *list) {
919           client.title = bstrdup(*list);
920           XFreeStringList(list);
921         } else {
922           client.title = bstrdup((char *) text_prop.value);
923         }
924       } else {
925         client.title = bstrdup((char *) text_prop.value);
926       }
927       XFree((char *) text_prop.value);
928     } else {
929       client.title = bstrdup(i18n->getMessage(WindowSet, WindowUnnamed,
930                                               "Unnamed"));
931     }
932   } else {
933     client.title = bstrdup(i18n->getMessage(WindowSet, WindowUnnamed,
934                                             "Unnamed"));
935   }
936   client.title_len = strlen(client.title);
937
938   if (i18n->multibyte()) {
939     XRectangle ink, logical;
940     XmbTextExtents(screen->getWindowStyle()->fontset,
941                    client.title, client.title_len, &ink, &logical);
942     client.title_text_w = logical.width;
943   } else {
944     client.title_len = strlen(client.title);
945     client.title_text_w = XTextWidth(screen->getWindowStyle()->font,
946                                      client.title, client.title_len);
947   }
948
949   client.title_text_w += (frame.bevel_w * 4);
950 }
951
952
953 void OpenboxWindow::getWMIconName(void) {
954   if (client.icon_title) {
955     delete [] client.icon_title;
956     client.icon_title = (char *) 0;
957   }
958
959   XTextProperty text_prop;
960   char **list;
961   int num;
962
963   if (XGetWMIconName(display, client.window, &text_prop)) {
964     if (text_prop.value && text_prop.nitems > 0) {
965       if (text_prop.encoding != XA_STRING) {
966         text_prop.nitems = strlen((char *) text_prop.value);
967
968         if ((XmbTextPropertyToTextList(display, &text_prop,
969                                        &list, &num) == Success) &&
970             (num > 0) && *list) {
971           client.icon_title = bstrdup(*list);
972           XFreeStringList(list);
973         } else {
974           client.icon_title = bstrdup((char *) text_prop.value);
975         }
976       } else {
977         client.icon_title = bstrdup((char *) text_prop.value);
978       }
979       XFree((char *) text_prop.value);
980     } else {
981       client.icon_title = bstrdup(client.title);
982     }
983   } else {
984     client.icon_title = bstrdup(client.title);
985   }
986 }
987
988
989 /*
990  * Retrieve which WM Protocols are supported by the client window.
991  * If the WM_DELETE_WINDOW protocol is supported, add the close button to the
992  * window's decorations and allow the close behavior.
993  * If the WM_TAKE_FOCUS protocol is supported, save a value that indicates
994  * this.
995  */
996 void OpenboxWindow::getWMProtocols(void) {
997   Atom *proto;
998   int num_return = 0;
999
1000   if (XGetWMProtocols(display, client.window, &proto, &num_return)) {
1001     for (int i = 0; i < num_return; ++i) {
1002       if (proto[i] == openbox.getWMDeleteAtom())
1003         functions.close = decorations.close = True;
1004       else if (proto[i] == openbox.getWMTakeFocusAtom())
1005         flags.send_focus_message = True;
1006       else if (proto[i] == openbox.getOpenboxStructureMessagesAtom())
1007         screen->addNetizen(new Netizen(*screen, client.window));
1008     }
1009
1010     XFree(proto);
1011   }
1012 }
1013
1014
1015 /*
1016  * Gets the value of the WM_HINTS property.
1017  * If the property is not set, then use a set of default values.
1018  */
1019 void OpenboxWindow::getWMHints(void) {
1020   XWMHints *wmhint = XGetWMHints(display, client.window);
1021   if (! wmhint) {
1022     flags.visible = True;
1023     flags.iconic = False;
1024     focus_mode = F_Passive;
1025     client.window_group = None;
1026     client.initial_state = NormalState;
1027     return;
1028   }
1029   client.wm_hint_flags = wmhint->flags;
1030   if (wmhint->flags & InputHint) {
1031     if (wmhint->input == True) {
1032       if (flags.send_focus_message)
1033         focus_mode = F_LocallyActive;
1034       else
1035         focus_mode = F_Passive;
1036     } else {
1037       if (flags.send_focus_message)
1038         focus_mode = F_GloballyActive;
1039       else
1040         focus_mode = F_NoInput;
1041     }
1042   } else {
1043     focus_mode = F_Passive;
1044   }
1045
1046   if (wmhint->flags & StateHint)
1047     client.initial_state = wmhint->initial_state;
1048   else
1049     client.initial_state = NormalState;
1050
1051   if (wmhint->flags & WindowGroupHint) {
1052     if (! client.window_group) {
1053       client.window_group = wmhint->window_group;
1054       openbox.saveGroupSearch(client.window_group, this);
1055     }
1056   } else {
1057     client.window_group = None;
1058   }
1059   XFree(wmhint);
1060 }
1061
1062
1063 /*
1064  * Gets the value of the WM_NORMAL_HINTS property.
1065  * If the property is not set, then use a set of default values.
1066  */
1067 void OpenboxWindow::getWMNormalHints(void) {
1068   long icccm_mask;
1069   XSizeHints sizehint;
1070
1071   client.min_width = client.min_height =
1072     client.base_width = client.base_height =
1073     client.width_inc = client.height_inc = 1;
1074   client.max_width = screen->size().w();
1075   client.max_height = screen->size().h();
1076   client.min_aspect_x = client.min_aspect_y =
1077     client.max_aspect_x = client.max_aspect_y = 1;
1078   client.win_gravity = NorthWestGravity;
1079
1080   if (! XGetWMNormalHints(display, client.window, &sizehint, &icccm_mask))
1081     return;
1082
1083   client.normal_hint_flags = sizehint.flags;
1084
1085   if (sizehint.flags & PMinSize) {
1086     client.min_width = sizehint.min_width;
1087     client.min_height = sizehint.min_height;
1088   }
1089
1090   if (sizehint.flags & PMaxSize) {
1091     client.max_width = sizehint.max_width;
1092     client.max_height = sizehint.max_height;
1093   }
1094
1095   if (sizehint.flags & PResizeInc) {
1096     client.width_inc = sizehint.width_inc;
1097     client.height_inc = sizehint.height_inc;
1098   }
1099
1100   if (sizehint.flags & PAspect) {
1101     client.min_aspect_x = sizehint.min_aspect.x;
1102     client.min_aspect_y = sizehint.min_aspect.y;
1103     client.max_aspect_x = sizehint.max_aspect.x;
1104     client.max_aspect_y = sizehint.max_aspect.y;
1105   }
1106
1107   if (sizehint.flags & PBaseSize) {
1108     client.base_width = sizehint.base_width;
1109     client.base_height = sizehint.base_height;
1110   }
1111
1112   if (sizehint.flags & PWinGravity)
1113     client.win_gravity = sizehint.win_gravity;
1114 }
1115
1116
1117 /*
1118  * Gets the MWM hints for the class' contained window.
1119  * This is used while initializing the window to its first state, and not
1120  * thereafter.
1121  * Returns: true if the MWM hints are successfully retreived and applied; false
1122  * if they are not.
1123  */
1124 void OpenboxWindow::getMWMHints(void) {
1125   int format;
1126   Atom atom_return;
1127   unsigned long num, len;
1128
1129   int ret = XGetWindowProperty(display, client.window,
1130                                openbox.getMotifWMHintsAtom(), 0,
1131                                PropMwmHintsElements, False,
1132                                openbox.getMotifWMHintsAtom(), &atom_return,
1133                                &format, &num, &len,
1134                                (unsigned char **) &client.mwm_hint);
1135
1136   if (ret != Success || !client.mwm_hint || num != PropMwmHintsElements)
1137     return;
1138
1139   if (client.mwm_hint->flags & MwmHintsDecorations) {
1140     if (client.mwm_hint->decorations & MwmDecorAll) {
1141       decorations.titlebar = decorations.handle = decorations.border =
1142         decorations.iconify = decorations.maximize =
1143         decorations.close = decorations.menu = True;
1144     } else {
1145       decorations.titlebar = decorations.handle = decorations.border =
1146         decorations.iconify = decorations.maximize =
1147         decorations.close = decorations.menu = False;
1148
1149       if (client.mwm_hint->decorations & MwmDecorBorder)
1150         decorations.border = True;
1151       if (client.mwm_hint->decorations & MwmDecorHandle)
1152         decorations.handle = True;
1153       if (client.mwm_hint->decorations & MwmDecorTitle)
1154         decorations.titlebar = True;
1155       if (client.mwm_hint->decorations & MwmDecorMenu)
1156         decorations.menu = True;
1157       if (client.mwm_hint->decorations & MwmDecorIconify)
1158         decorations.iconify = True;
1159       if (client.mwm_hint->decorations & MwmDecorMaximize)
1160         decorations.maximize = True;
1161     }
1162   }
1163
1164   if (client.mwm_hint->flags & MwmHintsFunctions) {
1165     if (client.mwm_hint->functions & MwmFuncAll) {
1166       functions.resize = functions.move = functions.iconify =
1167         functions.maximize = functions.close = True;
1168     } else {
1169       functions.resize = functions.move = functions.iconify =
1170         functions.maximize = functions.close = False;
1171
1172       if (client.mwm_hint->functions & MwmFuncResize)
1173         functions.resize = True;
1174       if (client.mwm_hint->functions & MwmFuncMove)
1175         functions.move = True;
1176       if (client.mwm_hint->functions & MwmFuncIconify)
1177         functions.iconify = True;
1178       if (client.mwm_hint->functions & MwmFuncMaximize)
1179         functions.maximize = True;
1180       if (client.mwm_hint->functions & MwmFuncClose)
1181         functions.close = True;
1182     }
1183   }
1184 }
1185
1186
1187 /*
1188  * Gets the openbox hints from the class' contained window.
1189  * This is used while initializing the window to its first state, and not
1190  * thereafter.
1191  * Returns: true if the hints are successfully retreived and applied; false if
1192  * they are not.
1193  */
1194 void OpenboxWindow::getOpenboxHints(void) {
1195   int format;
1196   Atom atom_return;
1197   unsigned long num, len;
1198
1199   int ret = XGetWindowProperty(display, client.window,
1200                                openbox.getOpenboxHintsAtom(), 0,
1201                                PropOpenboxHintsElements, False,
1202                                openbox.getOpenboxHintsAtom(), &atom_return,
1203                                &format, &num, &len,
1204                                (unsigned char **) &client.openbox_hint);
1205   if (ret != Success || !client.openbox_hint ||
1206       num != PropOpenboxHintsElements)
1207     return;
1208
1209   if (client.openbox_hint->flags & AttribShaded)
1210     flags.shaded = (client.openbox_hint->attrib & AttribShaded);
1211
1212   if ((client.openbox_hint->flags & AttribMaxHoriz) &&
1213       (client.openbox_hint->flags & AttribMaxVert))
1214     flags.maximized = (client.openbox_hint->attrib &
1215                        (AttribMaxHoriz | AttribMaxVert)) ? 1 : 0;
1216   else if (client.openbox_hint->flags & AttribMaxVert)
1217     flags.maximized = (client.openbox_hint->attrib & AttribMaxVert) ? 2 : 0;
1218   else if (client.openbox_hint->flags & AttribMaxHoriz)
1219     flags.maximized = (client.openbox_hint->attrib & AttribMaxHoriz) ? 3 : 0;
1220
1221   if (client.openbox_hint->flags & AttribOmnipresent)
1222     flags.stuck = (client.openbox_hint->attrib & AttribOmnipresent);
1223
1224   if (client.openbox_hint->flags & AttribWorkspace)
1225     workspace_number = client.openbox_hint->workspace;
1226
1227   // if (client.openbox_hint->flags & AttribStack)
1228   //   don't yet have always on top/bottom for openbox yet... working
1229   //   on that
1230
1231   if (client.openbox_hint->flags & AttribDecoration) {
1232     switch (client.openbox_hint->decoration) {
1233     case DecorNone:
1234       decorations.titlebar = decorations.border = decorations.handle =
1235         decorations.iconify = decorations.maximize =
1236         decorations.menu = False;
1237       functions.resize = functions.move = functions.iconify =
1238         functions.maximize = False;
1239
1240       break;
1241
1242     case DecorTiny:
1243       decorations.titlebar = decorations.iconify = decorations.menu =
1244         functions.move = functions.iconify = True;
1245       decorations.border = decorations.handle = decorations.maximize =
1246         functions.resize = functions.maximize = False;
1247
1248       break;
1249
1250     case DecorTool:
1251       decorations.titlebar = decorations.menu = functions.move = True;
1252       decorations.iconify = decorations.border = decorations.handle =
1253         decorations.maximize = functions.resize = functions.maximize =
1254         functions.iconify = False;
1255
1256       break;
1257
1258     case DecorNormal:
1259     default:
1260       decorations.titlebar = decorations.border = decorations.handle =
1261         decorations.iconify = decorations.maximize =
1262         decorations.menu = True;
1263       functions.resize = functions.move = functions.iconify =
1264         functions.maximize = True;
1265
1266       break;
1267     }
1268
1269     reconfigure();
1270   }
1271 }
1272
1273
1274 void OpenboxWindow::configure(int dx, int dy,
1275                                unsigned int dw, unsigned int dh) {
1276   Bool send_event = (frame.x != dx || frame.y != dy);
1277
1278   if ((dw != frame.width) || (dh != frame.height)) {
1279     if ((((signed) frame.width) + dx) < 0) dx = 0;
1280     if ((((signed) frame.height) + dy) < 0) dy = 0;
1281
1282     frame.x = dx;
1283     frame.y = dy;
1284     frame.width = dw;
1285     frame.height = dh;
1286
1287     downsize();
1288
1289 #ifdef    SHAPE
1290     if (openbox.hasShapeExtensions() && flags.shaped) {
1291       XShapeCombineShape(display, frame.window, ShapeBounding,
1292                          frame.mwm_border_w, frame.y_border +
1293                          frame.mwm_border_w, client.window,
1294                          ShapeBounding, ShapeSet);
1295
1296       int num = 1;
1297       XRectangle xrect[2];
1298       xrect[0].x = xrect[0].y = 0;
1299       xrect[0].width = frame.width;
1300       xrect[0].height = frame.y_border;
1301
1302       if (decorations.handle) {
1303         xrect[1].x = 0;
1304         xrect[1].y = frame.y_handle;
1305         xrect[1].width = frame.width;
1306         xrect[1].height = frame.handle_h + frame.border_w;
1307         num++;
1308       }
1309
1310       XShapeCombineRectangles(display, frame.window, ShapeBounding, 0, 0,
1311                               xrect, num, ShapeUnion, Unsorted);
1312     }
1313 #endif // SHAPE
1314
1315     XMoveWindow(display, frame.window, frame.x, frame.y);
1316
1317     positionWindows();
1318     decorate();
1319     setFocusFlag(flags.focused);
1320     redrawAllButtons();
1321   } else {
1322     frame.x = dx;
1323     frame.y = dy;
1324
1325     XMoveWindow(display, frame.window, frame.x, frame.y);
1326
1327     if (! flags.moving) send_event = True;
1328   }
1329
1330   if (send_event && ! flags.moving) {
1331     client.x = dx + frame.mwm_border_w + frame.border_w;
1332     client.y = dy + frame.y_border + frame.mwm_border_w +
1333                frame.border_w;
1334
1335     XEvent event;
1336     event.type = ConfigureNotify;
1337
1338     event.xconfigure.display = display;
1339     event.xconfigure.event = client.window;
1340     event.xconfigure.window = client.window;
1341     event.xconfigure.x = client.x;
1342     event.xconfigure.y = client.y;
1343     event.xconfigure.width = client.width;
1344     event.xconfigure.height = client.height;
1345     event.xconfigure.border_width = client.old_bw;
1346     event.xconfigure.above = frame.window;
1347     event.xconfigure.override_redirect = False;
1348
1349     XSendEvent(display, client.window, True, NoEventMask, &event);
1350
1351     screen->updateNetizenConfigNotify(&event);
1352   }
1353 }
1354
1355
1356 Bool OpenboxWindow::setInputFocus(void) {
1357   if (((signed) (frame.x + frame.width)) < 0) {
1358     if (((signed) (frame.y + frame.y_border)) < 0)
1359       configure(frame.border_w, frame.border_w, frame.width, frame.height);
1360     else if (frame.y > (signed) screen->size().h())
1361       configure(frame.border_w, screen->size().h() - frame.height,
1362                 frame.width, frame.height);
1363     else
1364       configure(frame.border_w, frame.y + frame.border_w,
1365                 frame.width, frame.height);
1366   } else if (frame.x > (signed) screen->size().w()) {
1367     if (((signed) (frame.y + frame.y_border)) < 0)
1368       configure(screen->size().w() - frame.width, frame.border_w,
1369                 frame.width, frame.height);
1370     else if (frame.y > (signed) screen->size().h())
1371       configure(screen->size().w() - frame.width,
1372                 screen->size().h() - frame.height, frame.width, frame.height);
1373     else
1374       configure(screen->size().w() - frame.width,
1375                 frame.y + frame.border_w, frame.width, frame.height);
1376   }
1377
1378   openbox.grab();
1379   if (! validateClient()) return False;
1380
1381   Bool ret = False;
1382
1383   if (client.transient && flags.modal) {
1384     ret = client.transient->setInputFocus();
1385   } else if (! flags.focused) {
1386     if (focus_mode == F_LocallyActive || focus_mode == F_Passive)
1387       XSetInputFocus(display, client.window,
1388                      RevertToPointerRoot, CurrentTime);
1389     else
1390       XSetInputFocus(display, screen->getRootWindow(),
1391                      RevertToNone, CurrentTime);
1392
1393     openbox.setFocusedWindow(this);
1394
1395     if (flags.send_focus_message) {
1396       XEvent ce;
1397       ce.xclient.type = ClientMessage;
1398       ce.xclient.message_type = openbox.getWMProtocolsAtom();
1399       ce.xclient.display = display;
1400       ce.xclient.window = client.window;
1401       ce.xclient.format = 32;
1402       ce.xclient.data.l[0] = openbox.getWMTakeFocusAtom();
1403       ce.xclient.data.l[1] = openbox.getLastTime();
1404       ce.xclient.data.l[2] = 0l;
1405       ce.xclient.data.l[3] = 0l;
1406       ce.xclient.data.l[4] = 0l;
1407       XSendEvent(display, client.window, False, NoEventMask, &ce);
1408     }
1409
1410     if (screen->sloppyFocus() && screen->autoRaise())
1411       timer->start();
1412
1413     ret = True;
1414   }
1415
1416   openbox.ungrab();
1417
1418   return ret;
1419 }
1420
1421
1422 void OpenboxWindow::iconify(void) {
1423   if (flags.iconic) return;
1424
1425   if (windowmenu) windowmenu->hide();
1426
1427   setState(IconicState);
1428
1429   XSelectInput(display, client.window, NoEventMask);
1430   XUnmapWindow(display, client.window);
1431   XSelectInput(display, client.window,
1432                PropertyChangeMask | StructureNotifyMask | FocusChangeMask);
1433
1434   XUnmapWindow(display, frame.window);
1435   flags.visible = False;
1436   flags.iconic = True;
1437
1438   screen->getWorkspace(workspace_number)->removeWindow(this);
1439
1440   if (flags.transient && client.transient_for &&
1441       !client.transient_for->flags.iconic) {
1442     client.transient_for->iconify();
1443   }
1444   screen->addIcon(this);
1445
1446   if (client.transient && !client.transient->flags.iconic) {
1447     client.transient->iconify();
1448   }
1449 }
1450
1451
1452 void OpenboxWindow::deiconify(Bool reassoc, Bool raise) {
1453   if (flags.iconic || reassoc)
1454     screen->reassociateWindow(this, -1, False);
1455   else if (workspace_number != screen->getCurrentWorkspace()->getWorkspaceID())
1456     return;
1457
1458   setState(NormalState);
1459
1460   XSelectInput(display, client.window, NoEventMask);
1461   XMapWindow(display, client.window);
1462   XSelectInput(display, client.window,
1463                PropertyChangeMask | StructureNotifyMask | FocusChangeMask);
1464
1465   XMapSubwindows(display, frame.window);
1466   XMapWindow(display, frame.window);
1467
1468   if (flags.iconic && screen->focusNew()) setInputFocus();
1469
1470   flags.visible = True;
1471   flags.iconic = False;
1472
1473   if (reassoc && client.transient) client.transient->deiconify(True, False);
1474
1475   if (raise)
1476     screen->getWorkspace(workspace_number)->raiseWindow(this);
1477 }
1478
1479
1480 void OpenboxWindow::close(void) {
1481   XEvent ce;
1482   ce.xclient.type = ClientMessage;
1483   ce.xclient.message_type = openbox.getWMProtocolsAtom();
1484   ce.xclient.display = display;
1485   ce.xclient.window = client.window;
1486   ce.xclient.format = 32;
1487   ce.xclient.data.l[0] = openbox.getWMDeleteAtom();
1488   ce.xclient.data.l[1] = CurrentTime;
1489   ce.xclient.data.l[2] = 0l;
1490   ce.xclient.data.l[3] = 0l;
1491   ce.xclient.data.l[4] = 0l;
1492   XSendEvent(display, client.window, False, NoEventMask, &ce);
1493 }
1494
1495
1496 void OpenboxWindow::withdraw(void) {
1497   flags.visible = False;
1498   flags.iconic = False;
1499
1500   XUnmapWindow(display, frame.window);
1501
1502   XSelectInput(display, client.window, NoEventMask);
1503   XUnmapWindow(display, client.window);
1504   XSelectInput(display, client.window,
1505                PropertyChangeMask | StructureNotifyMask | FocusChangeMask);
1506
1507   if (windowmenu) windowmenu->hide();
1508 }
1509
1510
1511 void OpenboxWindow::maximize(unsigned int button) {
1512   // handle case where menu is open then the max button is used instead
1513   if (windowmenu && windowmenu->isVisible()) windowmenu->hide();
1514
1515   if (flags.maximized) {
1516     flags.maximized = 0;
1517
1518     openbox_attrib.flags &= ! (AttribMaxHoriz | AttribMaxVert);
1519     openbox_attrib.attrib &= ! (AttribMaxHoriz | AttribMaxVert);
1520
1521     // when a resize is begun, maximize(0) is called to clear any maximization
1522     // flags currently set.  Otherwise it still thinks it is maximized.
1523     // so we do not need to call configure() because resizing will handle it
1524     if (!flags.resizing)
1525       configure(openbox_attrib.premax_x, openbox_attrib.premax_y,
1526                 openbox_attrib.premax_w, openbox_attrib.premax_h);
1527
1528     openbox_attrib.premax_x = openbox_attrib.premax_y = 0;
1529     openbox_attrib.premax_w = openbox_attrib.premax_h = 0;
1530
1531     redrawAllButtons();
1532     setState(current_state);
1533     return;
1534   }
1535
1536   openbox_attrib.premax_x = frame.x;
1537   openbox_attrib.premax_y = frame.y;
1538   openbox_attrib.premax_w = frame.width;
1539   openbox_attrib.premax_h = frame.height;
1540
1541   Rect space = screen->availableArea();
1542   unsigned int dw = space.w(),
1543                dh = space.h();
1544   dw -= frame.border_w * 2;
1545   dw -= frame.mwm_border_w * 2;
1546   dw -= client.base_width;
1547
1548   dh -= frame.border_w * 2;
1549   dh -= frame.mwm_border_w * 2;
1550   dh -= ((frame.handle_h + frame.border_w) * decorations.handle);
1551   dh -= client.base_height;
1552   dh -= frame.y_border;
1553
1554   if (dw < client.min_width) dw = client.min_width;
1555   if (dh < client.min_height) dh = client.min_height;
1556   if (dw > client.max_width) dw = client.max_width;
1557   if (dh > client.max_height) dh = client.max_height;
1558
1559   dw -= (dw % client.width_inc);
1560   dw += client.base_width;
1561   dw += frame.mwm_border_w * 2;
1562
1563   dh -= (dh % client.height_inc);
1564   dh += client.base_height;
1565   dh += frame.y_border;
1566   dh += ((frame.handle_h + frame.border_w) * decorations.handle);
1567   dh += frame.mwm_border_w * 2;
1568
1569   int dx = space.x() + ((space.w() - dw) / 2) - frame.border_w,
1570       dy = space.y() + ((space.h() - dh) / 2) - frame.border_w;
1571
1572   switch(button) {
1573   case 1:
1574     openbox_attrib.flags |= AttribMaxHoriz | AttribMaxVert;
1575     openbox_attrib.attrib |= AttribMaxHoriz | AttribMaxVert;
1576     break;
1577
1578   case 2:
1579     openbox_attrib.flags |= AttribMaxVert;
1580     openbox_attrib.attrib |= AttribMaxVert;
1581
1582     dw = frame.width;
1583     dx = frame.x;
1584     break;
1585
1586   case 3:
1587     openbox_attrib.flags |= AttribMaxHoriz;
1588     openbox_attrib.attrib |= AttribMaxHoriz;
1589
1590     dh = frame.height;
1591     dy = frame.y;
1592     break;
1593   }
1594
1595   if (flags.shaded) {
1596     openbox_attrib.flags ^= AttribShaded;
1597     openbox_attrib.attrib ^= AttribShaded;
1598     flags.shaded = False;
1599   }
1600
1601   flags.maximized = button;
1602
1603   configure(dx, dy, dw, dh);
1604   screen->getWorkspace(workspace_number)->raiseWindow(this);
1605   redrawAllButtons();
1606   setState(current_state);
1607 }
1608
1609
1610 void OpenboxWindow::setWorkspace(int n) {
1611   workspace_number = n;
1612
1613   openbox_attrib.flags |= AttribWorkspace;
1614   openbox_attrib.workspace = workspace_number;
1615 }
1616
1617
1618 void OpenboxWindow::shade(void) {
1619   if (!decorations.titlebar)
1620     return;
1621
1622   if (flags.shaded) {
1623     XResizeWindow(display, frame.window, frame.width, frame.height);
1624     flags.shaded = False;
1625     openbox_attrib.flags ^= AttribShaded;
1626     openbox_attrib.attrib ^= AttribShaded;
1627
1628     setState(NormalState);
1629   } else {
1630     XResizeWindow(display, frame.window, frame.width, frame.title_h);
1631     flags.shaded = True;
1632     openbox_attrib.flags |= AttribShaded;
1633     openbox_attrib.attrib |= AttribShaded;
1634
1635     setState(IconicState);
1636   }
1637 }
1638
1639
1640 void OpenboxWindow::stick(void) {
1641   if (flags.stuck) {
1642     openbox_attrib.flags ^= AttribOmnipresent;
1643     openbox_attrib.attrib ^= AttribOmnipresent;
1644
1645     flags.stuck = False;
1646
1647     if (! flags.iconic)
1648       screen->reassociateWindow(this, -1, True);
1649
1650     setState(current_state);
1651   } else {
1652     flags.stuck = True;
1653
1654     openbox_attrib.flags |= AttribOmnipresent;
1655     openbox_attrib.attrib |= AttribOmnipresent;
1656
1657     setState(current_state);
1658   }
1659 }
1660
1661
1662 void OpenboxWindow::setFocusFlag(Bool focus) {
1663   flags.focused = focus;
1664
1665   if (decorations.titlebar) {
1666     if (flags.focused) {
1667       if (frame.ftitle)
1668         XSetWindowBackgroundPixmap(display, frame.title, frame.ftitle);
1669       else
1670         XSetWindowBackground(display, frame.title, frame.ftitle_pixel);
1671     } else {
1672       if (frame.utitle)
1673         XSetWindowBackgroundPixmap(display, frame.title, frame.utitle);
1674       else
1675         XSetWindowBackground(display, frame.title, frame.utitle_pixel);
1676     }
1677     XClearWindow(display, frame.title);
1678
1679     redrawLabel();
1680     redrawAllButtons();
1681   }
1682
1683   if (decorations.handle) {
1684     if (flags.focused) {
1685       if (frame.fhandle)
1686         XSetWindowBackgroundPixmap(display, frame.handle, frame.fhandle);
1687       else
1688         XSetWindowBackground(display, frame.handle, frame.fhandle_pixel);
1689
1690       if (frame.fgrip) {
1691         XSetWindowBackgroundPixmap(display, frame.right_grip, frame.fgrip);
1692         XSetWindowBackgroundPixmap(display, frame.left_grip, frame.fgrip);
1693       } else {
1694         XSetWindowBackground(display, frame.right_grip, frame.fgrip_pixel);
1695         XSetWindowBackground(display, frame.left_grip, frame.fgrip_pixel);
1696       }
1697     } else {
1698       if (frame.uhandle)
1699         XSetWindowBackgroundPixmap(display, frame.handle, frame.uhandle);
1700       else
1701         XSetWindowBackground(display, frame.handle, frame.uhandle_pixel);
1702
1703       if (frame.ugrip) {
1704         XSetWindowBackgroundPixmap(display, frame.right_grip, frame.ugrip);
1705         XSetWindowBackgroundPixmap(display, frame.left_grip, frame.ugrip);
1706       } else {
1707         XSetWindowBackground(display, frame.right_grip, frame.ugrip_pixel);
1708         XSetWindowBackground(display, frame.left_grip, frame.ugrip_pixel);
1709       }
1710     }
1711     XClearWindow(display, frame.handle);
1712     XClearWindow(display, frame.right_grip);
1713     XClearWindow(display, frame.left_grip);
1714   }
1715
1716   if (decorations.border) {
1717     if (flags.focused)
1718       XSetWindowBorder(display, frame.plate, frame.fborder_pixel);
1719     else
1720       XSetWindowBorder(display, frame.plate, frame.uborder_pixel);
1721   }
1722
1723   if (screen->sloppyFocus() && screen->autoRaise() && timer->isTiming())
1724     timer->stop();
1725 }
1726
1727
1728 void OpenboxWindow::installColormap(Bool install) {
1729   openbox.grab();
1730   if (! validateClient()) return;
1731
1732   int i = 0, ncmap = 0;
1733   Colormap *cmaps = XListInstalledColormaps(display, client.window, &ncmap);
1734   XWindowAttributes wattrib;
1735   if (cmaps) {
1736     if (XGetWindowAttributes(display, client.window, &wattrib)) {
1737       if (install) {
1738         // install the window's colormap
1739         for (i = 0; i < ncmap; i++) {
1740           if (*(cmaps + i) == wattrib.colormap)
1741             // this window is using an installed color map... do not install
1742             install = False;
1743         }
1744         // otherwise, install the window's colormap
1745         if (install)
1746           XInstallColormap(display, wattrib.colormap);
1747       } else {
1748         // uninstall the window's colormap
1749         for (i = 0; i < ncmap; i++) {
1750           if (*(cmaps + i) == wattrib.colormap)
1751             // we found the colormap to uninstall
1752             XUninstallColormap(display, wattrib.colormap);
1753         }
1754       }
1755     }
1756
1757     XFree(cmaps);
1758   }
1759
1760   openbox.ungrab();
1761 }
1762
1763
1764 void OpenboxWindow::setState(unsigned long new_state) {
1765   current_state = new_state;
1766
1767   unsigned long state[2];
1768   state[0] = (unsigned long) current_state;
1769   state[1] = (unsigned long) None;
1770   XChangeProperty(display, client.window, openbox.getWMStateAtom(),
1771                   openbox.getWMStateAtom(), 32, PropModeReplace,
1772                   (unsigned char *) state, 2);
1773
1774   XChangeProperty(display, client.window,
1775                   openbox.getOpenboxAttributesAtom(),
1776                   openbox.getOpenboxAttributesAtom(), 32, PropModeReplace,
1777                   (unsigned char *) &openbox_attrib,
1778                   PropOpenboxAttributesElements);
1779 }
1780
1781
1782 Bool OpenboxWindow::getState(void) {
1783   current_state = 0;
1784
1785   Atom atom_return;
1786   Bool ret = False;
1787   int foo;
1788   unsigned long *state, ulfoo, nitems;
1789
1790   if ((XGetWindowProperty(display, client.window, openbox.getWMStateAtom(),
1791                           0l, 2l, False, openbox.getWMStateAtom(),
1792                           &atom_return, &foo, &nitems, &ulfoo,
1793                           (unsigned char **) &state) != Success) ||
1794       (! state)) {
1795     openbox.ungrab();
1796     return False;
1797   }
1798
1799   if (nitems >= 1) {
1800     current_state = (unsigned long) state[0];
1801
1802     ret = True;
1803   }
1804
1805   XFree((void *) state);
1806
1807   return ret;
1808 }
1809
1810
1811 void OpenboxWindow::setGravityOffsets(void) {
1812   // x coordinates for each gravity type
1813   const int x_west = client.x;
1814   const int x_east = client.x + client.width - frame.width;
1815   const int x_center = client.x + client.width - frame.width/2;
1816   // y coordinates for each gravity type
1817   const int y_north = client.y;
1818   const int y_south = client.y + client.height - frame.height;
1819   const int y_center = client.y + client.height - frame.height/2;
1820
1821   switch (client.win_gravity) {
1822   case NorthWestGravity:
1823   default:
1824     frame.x = x_west;
1825     frame.y = y_north;
1826     break;
1827   case NorthGravity:
1828     frame.x = x_center;
1829     frame.y = y_north;
1830     break;
1831   case NorthEastGravity:
1832     frame.x = x_east;
1833     frame.y = y_north;
1834     break;
1835   case SouthWestGravity:
1836     frame.x = x_west;
1837     frame.y = y_south;
1838     break;
1839   case SouthGravity:
1840     frame.x = x_center;
1841     frame.y = y_south;
1842     break;
1843   case SouthEastGravity:
1844     frame.x = x_east;
1845     frame.y = y_south;
1846     break;
1847   case WestGravity:
1848     frame.x = x_west;
1849     frame.y = y_center;
1850     break;
1851   case EastGravity:
1852     frame.x = x_east;
1853     frame.y = y_center;
1854     break;
1855   case CenterGravity:
1856     frame.x = x_center;
1857     frame.y = y_center;
1858     break;
1859   case ForgetGravity:
1860   case StaticGravity:
1861     frame.x = client.x - frame.mwm_border_w + frame.border_w;
1862     frame.y = client.y - frame.y_border - frame.mwm_border_w - frame.border_w;
1863     break;
1864   }
1865 }
1866
1867
1868 void OpenboxWindow::restoreAttributes(void) {
1869   if (! getState()) current_state = NormalState;
1870
1871   Atom atom_return;
1872   int foo;
1873   unsigned long ulfoo, nitems;
1874
1875   OpenboxAttributes *net;
1876   int ret = XGetWindowProperty(display, client.window,
1877                                openbox.getOpenboxAttributesAtom(), 0l,
1878                                PropOpenboxAttributesElements, False,
1879                                openbox.getOpenboxAttributesAtom(),
1880                                &atom_return, &foo, &nitems, &ulfoo,
1881                                (unsigned char **) &net);
1882   if (ret != Success || !net || nitems != PropOpenboxAttributesElements)
1883     return;
1884
1885   openbox_attrib.flags = net->flags;
1886   openbox_attrib.attrib = net->attrib;
1887   openbox_attrib.decoration = net->decoration;
1888   openbox_attrib.workspace = net->workspace;
1889   openbox_attrib.stack = net->stack;
1890   openbox_attrib.premax_x = net->premax_x;
1891   openbox_attrib.premax_y = net->premax_y;
1892   openbox_attrib.premax_w = net->premax_w;
1893   openbox_attrib.premax_h = net->premax_h;
1894
1895   XFree((void *) net);
1896
1897   if (openbox_attrib.flags & AttribShaded &&
1898       openbox_attrib.attrib & AttribShaded) {
1899     int save_state =
1900       ((current_state == IconicState) ? NormalState : current_state);
1901
1902     flags.shaded = False;
1903     shade();
1904
1905     current_state = save_state;
1906   }
1907
1908   if (((int) openbox_attrib.workspace != screen->getCurrentWorkspaceID()) &&
1909       ((int) openbox_attrib.workspace < screen->getWorkspaceCount())) {
1910     screen->reassociateWindow(this, openbox_attrib.workspace, True);
1911
1912     if (current_state == NormalState) current_state = WithdrawnState;
1913   } else if (current_state == WithdrawnState) {
1914     current_state = NormalState;
1915   }
1916
1917   if (openbox_attrib.flags & AttribOmnipresent &&
1918       openbox_attrib.attrib & AttribOmnipresent) {
1919     flags.stuck = False;
1920     stick();
1921
1922     current_state = NormalState;
1923   }
1924
1925   if ((openbox_attrib.flags & AttribMaxHoriz) ||
1926       (openbox_attrib.flags & AttribMaxVert)) {
1927     int x = openbox_attrib.premax_x, y = openbox_attrib.premax_y;
1928     unsigned int w = openbox_attrib.premax_w, h = openbox_attrib.premax_h;
1929     flags.maximized = 0;
1930
1931     unsigned int m = False;
1932     if ((openbox_attrib.flags & AttribMaxHoriz) &&
1933         (openbox_attrib.flags & AttribMaxVert))
1934       m = (openbox_attrib.attrib & (AttribMaxHoriz | AttribMaxVert)) ? 1 : 0;
1935     else if (openbox_attrib.flags & AttribMaxVert)
1936       m = (openbox_attrib.attrib & AttribMaxVert) ? 2 : 0;
1937     else if (openbox_attrib.flags & AttribMaxHoriz)
1938       m = (openbox_attrib.attrib & AttribMaxHoriz) ? 3 : 0;
1939
1940     if (m) maximize(m);
1941
1942     openbox_attrib.premax_x = x;
1943     openbox_attrib.premax_y = y;
1944     openbox_attrib.premax_w = w;
1945     openbox_attrib.premax_h = h;
1946   }
1947
1948   setState(current_state);
1949 }
1950
1951
1952 /*
1953  * The reverse of the setGravityOffsets function. Uses the frame window's
1954  * position to find the window's reference point.
1955  */
1956 void OpenboxWindow::restoreGravity(void) {
1957   // x coordinates for each gravity type
1958   const int x_west = frame.x;
1959   const int x_east = frame.x + frame.width - client.width;
1960   const int x_center = frame.x + (frame.width/2) - client.width;
1961   // y coordinates for each gravity type
1962   const int y_north = frame.y;
1963   const int y_south = frame.y + frame.height - client.height;
1964   const int y_center = frame.y + (frame.height/2) - client.height;
1965
1966   switch(client.win_gravity) {
1967   default:
1968   case NorthWestGravity:
1969     client.x = x_west;
1970     client.y = y_north;
1971     break;
1972   case NorthGravity:
1973     client.x = x_center;
1974     client.y = y_north;
1975     break;
1976   case NorthEastGravity:
1977     client.x = x_east;
1978     client.y = y_north;
1979     break;
1980   case SouthWestGravity:
1981     client.x = x_west;
1982     client.y = y_south;
1983     break;
1984   case SouthGravity:
1985     client.x = x_center;
1986     client.y = y_south;
1987     break;
1988   case SouthEastGravity:
1989     client.x = x_east;
1990     client.y = y_south;
1991     break;
1992   case WestGravity:
1993     client.x = x_west;
1994     client.y = y_center;
1995     break;
1996   case EastGravity:
1997     client.x = x_east;
1998     client.y = y_center;
1999     break;
2000   case CenterGravity:
2001     client.x = x_center;
2002     client.y = y_center;
2003     break;
2004   case ForgetGravity:
2005   case StaticGravity:
2006     client.x = frame.x + frame.mwm_border_w + frame.border_w;
2007     client.y = frame.y + frame.y_border + frame.mwm_border_w +
2008       frame.border_w;
2009     break;
2010   }
2011 }
2012
2013
2014 void OpenboxWindow::redrawLabel(void) {
2015   int dx = frame.bevel_w * 2, dlen = client.title_len;
2016   unsigned int l = client.title_text_w;
2017
2018   if (flags.focused) {
2019     if (frame.flabel)
2020       XSetWindowBackgroundPixmap(display, frame.label, frame.flabel);
2021     else
2022       XSetWindowBackground(display, frame.label, frame.flabel_pixel);
2023   } else {
2024     if (frame.ulabel)
2025       XSetWindowBackgroundPixmap(display, frame.label, frame.ulabel);
2026     else
2027       XSetWindowBackground(display, frame.label, frame.ulabel_pixel);
2028   }
2029   XClearWindow(display, frame.label);
2030
2031   if (client.title_text_w > frame.label_w) {
2032     for (; dlen >= 0; dlen--) {
2033       if (i18n->multibyte()) {
2034         XRectangle ink, logical;
2035         XmbTextExtents(screen->getWindowStyle()->fontset, client.title, dlen,
2036                        &ink, &logical);
2037         l = logical.width;
2038       } else {
2039         l = XTextWidth(screen->getWindowStyle()->font, client.title, dlen);
2040       }
2041       l += (frame.bevel_w * 4);
2042
2043       if (l < frame.label_w)
2044         break;
2045     }
2046   }
2047
2048   switch (screen->getWindowStyle()->justify) {
2049   case BScreen::RightJustify:
2050     dx += frame.label_w - l;
2051     break;
2052
2053   case BScreen::CenterJustify:
2054     dx += (frame.label_w - l) / 2;
2055     break;
2056   }
2057
2058   WindowStyle *style = screen->getWindowStyle();
2059   GC text_gc = (flags.focused) ? style->l_text_focus_gc :
2060     style->l_text_unfocus_gc;
2061   if (i18n->multibyte())
2062     XmbDrawString(display, frame.label, style->fontset, text_gc, dx,
2063                   (1 - style->fontset_extents->max_ink_extent.y),
2064                   client.title, dlen);
2065   else
2066     XDrawString(display, frame.label, text_gc, dx,
2067                 (style->font->ascent + 1), client.title, dlen);
2068 }
2069
2070
2071 void OpenboxWindow::redrawAllButtons(void) {
2072   if (frame.iconify_button) redrawIconifyButton(False);
2073   if (frame.maximize_button) redrawMaximizeButton(flags.maximized);
2074   if (frame.close_button) redrawCloseButton(False);
2075 }
2076
2077
2078 void OpenboxWindow::redrawIconifyButton(Bool pressed) {
2079   if (! pressed) {
2080     if (flags.focused) {
2081       if (frame.fbutton)
2082         XSetWindowBackgroundPixmap(display, frame.iconify_button,
2083                                    frame.fbutton);
2084       else
2085         XSetWindowBackground(display, frame.iconify_button,
2086                              frame.fbutton_pixel);
2087     } else {
2088       if (frame.ubutton)
2089         XSetWindowBackgroundPixmap(display, frame.iconify_button,
2090                                    frame.ubutton);
2091       else
2092         XSetWindowBackground(display, frame.iconify_button,
2093                              frame.ubutton_pixel);
2094     }
2095   } else {
2096     if (frame.pbutton)
2097       XSetWindowBackgroundPixmap(display, frame.iconify_button, frame.pbutton);
2098     else
2099       XSetWindowBackground(display, frame.iconify_button, frame.pbutton_pixel);
2100   }
2101   XClearWindow(display, frame.iconify_button);
2102
2103   XDrawRectangle(display, frame.iconify_button,
2104                  ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2105                   screen->getWindowStyle()->b_pic_unfocus_gc),
2106                  2, (frame.button_h - 5), (frame.button_w - 5), 2);
2107 }
2108
2109
2110 void OpenboxWindow::redrawMaximizeButton(Bool pressed) {
2111   if (! pressed) {
2112     if (flags.focused) {
2113       if (frame.fbutton)
2114         XSetWindowBackgroundPixmap(display, frame.maximize_button,
2115                                    frame.fbutton);
2116       else
2117         XSetWindowBackground(display, frame.maximize_button,
2118                              frame.fbutton_pixel);
2119     } else {
2120       if (frame.ubutton)
2121         XSetWindowBackgroundPixmap(display, frame.maximize_button,
2122                                    frame.ubutton);
2123       else
2124         XSetWindowBackground(display, frame.maximize_button,
2125                              frame.ubutton_pixel);
2126     }
2127   } else {
2128     if (frame.pbutton)
2129       XSetWindowBackgroundPixmap(display, frame.maximize_button,
2130                                  frame.pbutton);
2131     else
2132       XSetWindowBackground(display, frame.maximize_button,
2133                            frame.pbutton_pixel);
2134   }
2135   XClearWindow(display, frame.maximize_button);
2136
2137   XDrawRectangle(display, frame.maximize_button,
2138                  ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2139                   screen->getWindowStyle()->b_pic_unfocus_gc),
2140                  2, 2, (frame.button_w - 5), (frame.button_h - 5));
2141   XDrawLine(display, frame.maximize_button,
2142             ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2143              screen->getWindowStyle()->b_pic_unfocus_gc),
2144             2, 3, (frame.button_w - 3), 3);
2145 }
2146
2147
2148 void OpenboxWindow::redrawCloseButton(Bool pressed) {
2149   if (! pressed) {
2150     if (flags.focused) {
2151       if (frame.fbutton)
2152         XSetWindowBackgroundPixmap(display, frame.close_button,
2153                                    frame.fbutton);
2154       else
2155         XSetWindowBackground(display, frame.close_button,
2156                              frame.fbutton_pixel);
2157     } else {
2158       if (frame.ubutton)
2159         XSetWindowBackgroundPixmap(display, frame.close_button,
2160                                    frame.ubutton);
2161       else
2162         XSetWindowBackground(display, frame.close_button,
2163                              frame.ubutton_pixel);
2164     }
2165   } else {
2166     if (frame.pbutton)
2167       XSetWindowBackgroundPixmap(display, frame.close_button, frame.pbutton);
2168     else
2169       XSetWindowBackground(display, frame.close_button, frame.pbutton_pixel);
2170   }
2171   XClearWindow(display, frame.close_button);
2172
2173   XDrawLine(display, frame.close_button,
2174             ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2175              screen->getWindowStyle()->b_pic_unfocus_gc), 2, 2,
2176             (frame.button_w - 3), (frame.button_h - 3));
2177   XDrawLine(display, frame.close_button,
2178             ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2179              screen->getWindowStyle()->b_pic_unfocus_gc), 2,
2180             (frame.button_h - 3),
2181             (frame.button_w - 3), 2);
2182 }
2183
2184
2185 void OpenboxWindow::mapRequestEvent(XMapRequestEvent *re) {
2186   if (re->window == client.window) {
2187 #ifdef    DEBUG
2188     fprintf(stderr, i18n->getMessage(WindowSet, WindowMapRequest,
2189                              "OpenboxWindow::mapRequestEvent() for 0x%lx\n"),
2190             client.window);
2191 #endif // DEBUG
2192
2193     openbox.grab();
2194     if (! validateClient()) return;
2195
2196     Bool get_state_ret = getState();
2197     if (! (get_state_ret && openbox.isStartup())) {
2198       if ((client.wm_hint_flags & StateHint) &&
2199           (! (current_state == NormalState || current_state == IconicState)))
2200         current_state = client.initial_state;
2201       else
2202         current_state = NormalState;
2203     } else if (flags.iconic) {
2204       current_state = NormalState;
2205     }
2206
2207     switch (current_state) {
2208     case IconicState:
2209       iconify();
2210       break;
2211
2212     case WithdrawnState:
2213       withdraw();
2214       break;
2215
2216     case NormalState:
2217     case InactiveState:
2218     case ZoomState:
2219     default:
2220       deiconify(False);
2221       break;
2222     }
2223
2224     openbox.ungrab();
2225   }
2226 }
2227
2228
2229 void OpenboxWindow::mapNotifyEvent(XMapEvent *ne) {
2230   if ((ne->window == client.window) && (! ne->override_redirect)
2231       && (flags.visible)) {
2232     openbox.grab();
2233     if (! validateClient()) return;
2234
2235     if (decorations.titlebar) positionButtons();
2236
2237     setState(NormalState);
2238
2239     redrawAllButtons();
2240
2241     if (flags.transient || screen->focusNew())
2242       setInputFocus();
2243     else
2244       setFocusFlag(False);
2245
2246     flags.visible = True;
2247     flags.iconic = False;
2248
2249     openbox.ungrab();
2250   }
2251 }
2252
2253
2254 void OpenboxWindow::unmapNotifyEvent(XUnmapEvent *ue) {
2255   if (ue->window == client.window) {
2256 #ifdef    DEBUG
2257     fprintf(stderr, i18n->getMessage(WindowSet, WindowUnmapNotify,
2258                              "OpenboxWindow::unmapNotifyEvent() for 0x%lx\n"),
2259             client.window);
2260 #endif // DEBUG
2261
2262     openbox.grab();
2263     if (! validateClient()) return;
2264
2265     XChangeSaveSet(display, client.window, SetModeDelete);
2266     XSelectInput(display, client.window, NoEventMask);
2267
2268     XDeleteProperty(display, client.window, openbox.getWMStateAtom());
2269     XDeleteProperty(display, client.window,
2270                     openbox.getOpenboxAttributesAtom());
2271
2272     XUnmapWindow(display, frame.window);
2273     XUnmapWindow(display, client.window);
2274
2275     XEvent dummy;
2276     if (! XCheckTypedWindowEvent(display, client.window, ReparentNotify,
2277                                  &dummy)) {
2278 #ifdef    DEBUG
2279       fprintf(stderr, i18n->getMessage(WindowSet, WindowUnmapNotifyReparent,
2280                        "OpenboxWindow::unmapNotifyEvent(): reparent 0x%lx to "
2281                        "root.\n"), client.window);
2282 #endif // DEBUG
2283
2284       restoreGravity();
2285       XReparentWindow(display, client.window, screen->getRootWindow(),
2286                       client.x, client.y);
2287     }
2288
2289     XFlush(display);
2290
2291     openbox.ungrab();
2292
2293     delete this;
2294   }
2295 }
2296
2297
2298 void OpenboxWindow::destroyNotifyEvent(XDestroyWindowEvent *de) {
2299   if (de->window == client.window) {
2300     XUnmapWindow(display, frame.window);
2301
2302     delete this;
2303   }
2304 }
2305
2306
2307 void OpenboxWindow::propertyNotifyEvent(Atom atom) {
2308   openbox.grab();
2309   if (! validateClient()) return;
2310
2311   switch(atom) {
2312   case XA_WM_CLASS:
2313   case XA_WM_CLIENT_MACHINE:
2314   case XA_WM_COMMAND:
2315     break;
2316
2317   case XA_WM_TRANSIENT_FOR:
2318     // determine if this is a transient window
2319     Window win;
2320     if (XGetTransientForHint(display, client.window, &win)) {
2321       if (win && (win != client.window)) {
2322         if ((client.transient_for = openbox.searchWindow(win))) {
2323           client.transient_for->client.transient = this;
2324           flags.stuck = client.transient_for->flags.stuck;
2325           flags.transient = True;
2326         } else if (win == client.window_group) {
2327           //jr This doesn't look quite right...
2328           if ((client.transient_for = openbox.searchGroup(win, this))) {
2329             client.transient_for->client.transient = this;
2330             flags.stuck = client.transient_for->flags.stuck;
2331             flags.transient = True;
2332           }
2333         }
2334       }
2335
2336       if (win == screen->getRootWindow()) flags.modal = True;
2337     }
2338
2339     // adjust the window decorations based on transience
2340     if (flags.transient)
2341       decorations.maximize = decorations.handle = functions.maximize = False;
2342
2343     reconfigure();
2344
2345     break;
2346
2347   case XA_WM_HINTS:
2348     getWMHints();
2349     break;
2350
2351   case XA_WM_ICON_NAME:
2352     getWMIconName();
2353     if (flags.iconic) screen->iconUpdate();
2354     break;
2355
2356   case XA_WM_NAME:
2357     getWMName();
2358
2359     if (decorations.titlebar)
2360       redrawLabel();
2361
2362     if (! flags.iconic)
2363       screen->getWorkspace(workspace_number)->update();
2364
2365     break;
2366
2367   case XA_WM_NORMAL_HINTS: {
2368     getWMNormalHints();
2369
2370     if ((client.normal_hint_flags & PMinSize) &&
2371         (client.normal_hint_flags & PMaxSize)) {
2372       if (client.max_width <= client.min_width &&
2373           client.max_height <= client.min_height)
2374         decorations.maximize = decorations.handle =
2375             functions.resize = functions.maximize = False;
2376       else
2377         decorations.maximize = decorations.handle =
2378             functions.resize = functions.maximize = True;
2379     }
2380
2381     int x = frame.x, y = frame.y;
2382     unsigned int w = frame.width, h = frame.height;
2383
2384     upsize();
2385
2386     if ((x != frame.x) || (y != frame.y) ||
2387         (w != frame.width) || (h != frame.height))
2388       reconfigure();
2389
2390     break;
2391   }
2392
2393   default:
2394     if (atom == openbox.getWMProtocolsAtom()) {
2395       getWMProtocols();
2396
2397       if (decorations.close && (! frame.close_button)) {
2398         createCloseButton();
2399         if (decorations.titlebar) positionButtons(True);
2400         if (windowmenu) windowmenu->reconfigure();
2401       }
2402     }
2403
2404     break;
2405   }
2406
2407   openbox.ungrab();
2408 }
2409
2410
2411 void OpenboxWindow::exposeEvent(XExposeEvent *ee) {
2412   if (frame.label == ee->window && decorations.titlebar)
2413     redrawLabel();
2414   else if (frame.close_button == ee->window)
2415     redrawCloseButton(False);
2416   else if (frame.maximize_button == ee->window)
2417     redrawMaximizeButton(flags.maximized);
2418   else if (frame.iconify_button == ee->window)
2419     redrawIconifyButton(False);
2420 }
2421
2422
2423 void OpenboxWindow::configureRequestEvent(XConfigureRequestEvent *cr) {
2424   if (cr->window == client.window) {
2425     openbox.grab();
2426     if (! validateClient()) return;
2427
2428     int cx = frame.x, cy = frame.y;
2429     unsigned int cw = frame.width, ch = frame.height;
2430
2431     if (cr->value_mask & CWBorderWidth)
2432       client.old_bw = cr->border_width;
2433
2434     if (cr->value_mask & CWX)
2435       cx = cr->x - frame.mwm_border_w - frame.border_w;
2436
2437     if (cr->value_mask & CWY)
2438       cy = cr->y - frame.y_border - frame.mwm_border_w -
2439         frame.border_w;
2440
2441     if (cr->value_mask & CWWidth)
2442       cw = cr->width + (frame.mwm_border_w * 2);
2443
2444     if (cr->value_mask & CWHeight)
2445       ch = cr->height + frame.y_border + (frame.mwm_border_w * 2) +
2446         (frame.border_w * decorations.handle) + frame.handle_h;
2447
2448     if (frame.x != cx || frame.y != cy ||
2449         frame.width != cw || frame.height != ch)
2450       configure(cx, cy, cw, ch);
2451
2452     if (cr->value_mask & CWStackMode) {
2453       switch (cr->detail) {
2454       case Above:
2455       case TopIf:
2456       default:
2457         if (flags.iconic) deiconify();
2458         screen->getWorkspace(workspace_number)->raiseWindow(this);
2459         break;
2460
2461       case Below:
2462       case BottomIf:
2463         if (flags.iconic) deiconify();
2464         screen->getWorkspace(workspace_number)->lowerWindow(this);
2465         break;
2466       }
2467     }
2468
2469     openbox.ungrab();
2470   }
2471 }
2472
2473
2474 void OpenboxWindow::buttonPressEvent(XButtonEvent *be) {
2475   openbox.grab();
2476   if (! validateClient())
2477     return;
2478
2479   int stack_change = 1; // < 0 means to lower the window
2480                         // > 0 means to raise the window
2481                         // 0 means to leave it where it is
2482   
2483   // alt + left/right click begins interactively moving/resizing the window
2484   // when the mouse is moved
2485   if (be->state == Mod1Mask && (be->button == 1 || be->button == 3)) {
2486     frame.grab_x = be->x_root - frame.x - frame.border_w;
2487     frame.grab_y = be->y_root - frame.y - frame.border_w;
2488     if (be->button == 3) {
2489       if (screen->getWindowZones() == 4 &&
2490           be->y < (signed) frame.height / 2) {
2491         resize_zone = ZoneTop;
2492       } else {
2493         resize_zone = ZoneBottom;
2494       }
2495       if (screen->getWindowZones() >= 2 &&
2496           be->x < (signed) frame.width / 2) {
2497         resize_zone |= ZoneLeft;
2498       } else {
2499         resize_zone |= ZoneRight;
2500       }
2501     }
2502   // control + left click on the titlebar shades the window
2503   } else if (be->state == ControlMask && be->button == 1) {
2504     if (be->window == frame.title ||
2505         be->window == frame.label)
2506       shade();
2507   // left click
2508   } else if (be->state == 0 && be->button == 1) {
2509     if (windowmenu && windowmenu->isVisible())
2510         windowmenu->hide();
2511
2512     if (be->window == frame.maximize_button) {
2513       redrawMaximizeButton(True);
2514     } else if (be->window == frame.iconify_button) {
2515       redrawIconifyButton(True);
2516     } else if (be->window == frame.close_button) {
2517       redrawCloseButton(True);
2518     } else if (be->window == frame.plate) {
2519       XAllowEvents(display, ReplayPointer, be->time);
2520     } else if (be->window == frame.title ||
2521                be->window == frame.label) {
2522       // shade the window when the titlebar is double clicked
2523       if ( (be->time - lastButtonPressTime) <=
2524             openbox.getDoubleClickInterval()) {
2525         lastButtonPressTime = 0;
2526         shade();
2527       } else {
2528         lastButtonPressTime = be->time;
2529       }
2530       // clicking and dragging on the titlebar moves the window, so on a click
2531       // we need to save the coords of the click in case the user drags
2532       frame.grab_x = be->x_root - frame.x - frame.border_w;
2533       frame.grab_y = be->y_root - frame.y - frame.border_w;
2534     } else if (be->window == frame.handle ||
2535                be->window == frame.left_grip ||
2536                be->window == frame.right_grip ||
2537                be->window == frame.window) {
2538       // clicking and dragging on the window's frame moves the window, so on a
2539       // click we need to save the coords of the click in case the user drags
2540       frame.grab_x = be->x_root - frame.x - frame.border_w;
2541       frame.grab_y = be->y_root - frame.y - frame.border_w;
2542       if (be->window == frame.left_grip)
2543         resize_zone = ZoneBottom | ZoneLeft;
2544       else if (be->window == frame.right_grip)
2545         resize_zone = ZoneBottom | ZoneRight;
2546     }
2547   // middle click
2548   } else if (be->state == 0 && be->button == 2) {
2549     if (be->window == frame.maximize_button) {
2550       redrawMaximizeButton(True);
2551     // a middle click anywhere on the window's frame except for on the buttons
2552     // will lower the window
2553     } else if (! (be->window == frame.iconify_button ||
2554                   be->window == frame.close_button) ) {
2555       stack_change = -1;
2556     }
2557   // right click
2558   } else if (be->state == 0 && be->button == 3) {
2559     if (be->window == frame.maximize_button) {
2560       redrawMaximizeButton(True);
2561     // a right click on the window's frame will show or hide the window's
2562     // windowmenu
2563     } else if (be->window == frame.title ||
2564                be->window == frame.label ||
2565                be->window == frame.handle ||
2566                be->window == frame.window) {
2567       int mx, my;
2568       if (windowmenu) {
2569         if (windowmenu->isVisible()) {
2570           windowmenu->hide();
2571         } else {
2572           // get the coords for the menu
2573           mx = be->x_root - windowmenu->getWidth() / 2;
2574           if (be->window == frame.title || be->window == frame.label) {
2575             my = frame.y + frame.title_h;
2576           } else if (be->window = frame.handle) {
2577             my = frame.y + frame.y_handle - windowmenu->getHeight();
2578           } else { // (be->window == frame.window)
2579             if (be->y <= (signed) frame.bevel_w) {
2580               my = frame.y + frame.y_border;
2581             } else {
2582               my = be->y_root - (windowmenu->getHeight() / 2);
2583             }
2584           }
2585
2586           if (mx > (signed) (frame.x + frame.width -
2587               windowmenu->getWidth())) {
2588             mx = frame.x + frame.width - windowmenu->getWidth();
2589           } else if (mx < frame.x) {
2590             mx = frame.x;
2591           }
2592
2593           if (my > (signed) (frame.y + frame.y_handle -
2594                 windowmenu->getHeight())) {
2595             my = frame.y + frame.y_handle - windowmenu->getHeight();
2596           } else if (my < (signed) (frame.y +
2597               ((decorations.titlebar) ? frame.title_h : frame.y_border))) {
2598             my = frame.y +
2599               ((decorations.titlebar) ? frame.title_h : frame.y_border);
2600           }
2601
2602           windowmenu->move(mx, my);
2603           windowmenu->show();
2604           XRaiseWindow(display, windowmenu->getWindowID());
2605           XRaiseWindow(display, windowmenu->getSendToMenu()->getWindowID());
2606           stack_change = 0;  // dont raise the window overtop of the menu
2607         }
2608       }
2609     }
2610   // mouse wheel up
2611   } else if (be->state == 0 && be->button == 4) {
2612     if ((be->window == frame.label ||
2613         be->window == frame.title) &&
2614         !flags.shaded)
2615       shade();
2616   // mouse wheel down
2617   } else if (be->state == 0 && be->button == 5) {
2618     if ((be->window == frame.label ||
2619         be->window == frame.title) &&
2620         flags.shaded)
2621       shade();
2622   }
2623
2624   if (! (flags.focused || screen->sloppyFocus()) ) {
2625     setInputFocus();  // any click focus' the window in 'click to focus'
2626   }
2627   if (stack_change < 0) {
2628     screen->getWorkspace(workspace_number)->lowerWindow(this);
2629   } else if (stack_change > 0) {
2630     screen->getWorkspace(workspace_number)->raiseWindow(this);
2631   }
2632  
2633   openbox.ungrab();
2634 }
2635
2636
2637 void OpenboxWindow::buttonReleaseEvent(XButtonEvent *re) {
2638   openbox.grab();
2639   if (! validateClient())
2640     return;
2641
2642   // alt + middle button released
2643   if (re->state == (Mod1Mask & Button2Mask) && re->button == 2) {
2644     if (re->window == frame.window) {
2645       XUngrabPointer(display, CurrentTime); // why? i dont know
2646     }
2647   // left button released
2648   } else if (re->button == 1) {
2649     if (re->window == frame.maximize_button) {
2650       if (re->state == Button1Mask && // only the left button was depressed
2651           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2652           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2653         maximize(re->button);
2654       } else {
2655         redrawMaximizeButton(False);
2656       }
2657     } else if (re->window == frame.iconify_button) {
2658       if (re->state == Button1Mask && // only the left button was depressed
2659           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2660           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2661         iconify();
2662       } else {
2663         redrawIconifyButton(False);
2664       }
2665     } else if (re->window == frame.close_button) {
2666       if (re->state == Button1Mask && // only the left button was depressed
2667           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2668           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2669           close();
2670       }
2671       //we should always redraw the close button. some applications
2672       //eg. acroread don't honour the close.
2673       redrawCloseButton(False);
2674     }
2675   // middle button released
2676   } else if (re->button == 2) {
2677     if (re->window == frame.maximize_button) {
2678       if (re->state == Button2Mask && // only the middle button was depressed
2679           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2680           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2681         maximize(re->button);
2682       } else {
2683         redrawMaximizeButton(False);
2684       }
2685     }
2686   // right button released
2687   } else if (re->button == 3) {
2688     if (re->window == frame.maximize_button) {
2689       if (re->state == Button3Mask && // only the right button was depressed
2690           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2691           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2692         maximize(re->button);
2693       } else {
2694         redrawMaximizeButton(False);
2695       }
2696     }
2697   }
2698   
2699   // when the window is being interactively moved, a button release stops the
2700   // move where it is
2701   if (flags.moving) {
2702     flags.moving = False;
2703
2704     openbox.maskWindowEvents(0, (OpenboxWindow *) 0);
2705     if (!screen->opaqueMove()) {
2706       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2707                      frame.move_x, frame.move_y, frame.resize_w - 1,
2708                      frame.resize_h - 1);
2709
2710       configure(frame.move_x, frame.move_y, frame.width, frame.height);
2711       openbox.ungrab();
2712     } else {
2713       configure(frame.x, frame.y, frame.width, frame.height);
2714     }
2715     screen->hideGeometry();
2716     XUngrabPointer(display, CurrentTime);
2717   // when the window is being interactively resized, a button release stops the
2718   // resizing
2719   } else if (flags.resizing) {
2720     flags.resizing = False;
2721     XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2722                    frame.resize_x, frame.resize_y,
2723                    frame.resize_w - 1, frame.resize_h - 1);
2724     screen->hideGeometry();
2725     if (resize_zone & ZoneLeft) {
2726       left_fixsize();
2727     } else {  // when resizing with "Alt+Button3", the resize is the same as if
2728               // done with the right grip (the right side of the window is what
2729               // moves)
2730       right_fixsize();
2731     }
2732     // unset maximized state when resized after fully maximized
2733     if (flags.maximized == 1) {
2734         maximize(0);
2735     }
2736     configure(frame.resize_x, frame.resize_y,
2737               frame.resize_w - (frame.border_w * 2),
2738               frame.resize_h - (frame.border_w * 2));
2739     openbox.ungrab();
2740     XUngrabPointer(display, CurrentTime);
2741     resize_zone = 0;
2742   }
2743
2744   openbox.ungrab();
2745 }
2746
2747
2748 void OpenboxWindow::motionNotifyEvent(XMotionEvent *me) {
2749   if (!flags.resizing && (me->state & Button1Mask) && functions.move &&
2750       (frame.title == me->window || frame.label == me->window ||
2751        frame.handle == me->window || frame.window == me->window)) {
2752     if (! flags.moving) {
2753       XGrabPointer(display, me->window, False, Button1MotionMask |
2754                    ButtonReleaseMask, GrabModeAsync, GrabModeAsync,
2755                    None, openbox.getMoveCursor(), CurrentTime);
2756
2757       if (windowmenu && windowmenu->isVisible())
2758         windowmenu->hide();
2759
2760       flags.moving = True;
2761
2762       openbox.maskWindowEvents(client.window, this);
2763
2764       if (! screen->opaqueMove()) {
2765         openbox.grab();
2766
2767         frame.move_x = frame.x;
2768         frame.move_y = frame.y;
2769         frame.resize_w = frame.width + (frame.border_w * 2);
2770         frame.resize_h = ((flags.shaded) ? frame.title_h : frame.height) +
2771           (frame.border_w * 2);
2772
2773         screen->showPosition(frame.x, frame.y);
2774
2775         XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2776                        frame.move_x, frame.move_y,
2777                        frame.resize_w - 1, frame.resize_h - 1);
2778       }
2779     } else {
2780       int dx = me->x_root - frame.grab_x, dy = me->y_root - frame.grab_y;
2781
2782       dx -= frame.border_w;
2783       dy -= frame.border_w;
2784
2785       int snap_distance = screen->edgeSnapThreshold();
2786       // width/height of the snapping window
2787       unsigned int snap_w = frame.width + (frame.border_w * 2);
2788       unsigned int snap_h = area().h() + (frame.border_w * 2);
2789       if (snap_distance) {
2790         int drx = screen->size().w() - (dx + snap_w);
2791
2792         if (dx < drx && (dx > 0 && dx < snap_distance) ||
2793                         (dx < 0 && dx > -snap_distance) )
2794           dx = 0;
2795         else if ( (drx > 0 && drx < snap_distance) ||
2796                   (drx < 0 && drx > -snap_distance) )
2797           dx = screen->size().w() - snap_w;
2798
2799         int dtty, dbby, dty, dby;
2800         switch (screen->getToolbar()->placement()) {
2801         case Toolbar::TopLeft:
2802         case Toolbar::TopCenter:
2803         case Toolbar::TopRight:
2804           dtty = screen->getToolbar()->getExposedHeight() +
2805                  frame.border_w;
2806           dbby = screen->size().h();
2807           break;
2808
2809         default:
2810           dtty = 0;
2811           dbby = screen->getToolbar()->area().y();
2812           break;
2813         }
2814
2815         dty = dy - dtty;
2816         dby = dbby - (dy + snap_h);
2817
2818         if ( (dy > 0 && dty < snap_distance) ||
2819             (dy < 0 && dty > -snap_distance) )
2820           dy = dtty;
2821         else if ( (dby > 0 && dby < snap_distance) ||
2822                  (dby < 0 && dby > -snap_distance) )
2823           dy = dbby - snap_h;
2824       }
2825
2826       if (screen->opaqueMove()) {
2827         configure(dx, dy, frame.width, frame.height);
2828       } else {
2829         XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2830                        frame.move_x, frame.move_y, frame.resize_w - 1,
2831                        frame.resize_h - 1);
2832
2833         frame.move_x = dx;
2834         frame.move_y = dy;
2835
2836         XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2837                        frame.move_x, frame.move_y, frame.resize_w - 1,
2838                        frame.resize_h - 1);
2839       }
2840
2841       screen->showPosition(dx, dy);
2842     }
2843   } else if (functions.resize &&
2844              (((me->state & Button1Mask) && (me->window == frame.right_grip ||
2845                                              me->window == frame.left_grip)) ||
2846               (me->state & (Mod1Mask | Button3Mask) &&
2847                                              me->window == frame.window))) {
2848     Bool left = resize_zone & ZoneLeft;
2849
2850     if (! flags.resizing) {
2851       Cursor cursor;
2852       if (resize_zone & ZoneTop)
2853         cursor = (resize_zone & ZoneLeft) ?
2854           openbox.getUpperLeftAngleCursor() :
2855           openbox.getUpperRightAngleCursor();
2856       else
2857         cursor = (resize_zone & ZoneLeft) ?
2858           openbox.getLowerLeftAngleCursor() :
2859           openbox.getLowerRightAngleCursor();
2860       XGrabPointer(display, me->window, False, ButtonMotionMask |
2861                    ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None,
2862                    cursor, CurrentTime);
2863
2864       flags.resizing = True;
2865
2866       openbox.grab();
2867
2868       int gx, gy;
2869       if (resize_zone & ZoneRight)
2870         frame.grab_x = me->x - screen->getBorderWidth();
2871       else
2872         frame.grab_x = me->x + screen->getBorderWidth();
2873       if (resize_zone & ZoneTop)
2874         frame.grab_y = me->y + screen->getBorderWidth() * 2;
2875       else
2876         frame.grab_y = me->y - screen->getBorderWidth() * 2;
2877       frame.resize_x = frame.x;
2878       frame.resize_y = frame.y;
2879       frame.resize_w = frame.width + (frame.border_w * 2);
2880       frame.resize_h = frame.height + (frame.border_w * 2);
2881
2882       if (left)
2883         left_fixsize(&gx, &gy);
2884       else
2885         right_fixsize(&gx, &gy);
2886
2887       screen->showGeometry(gx, gy);
2888
2889       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2890                      frame.resize_x, frame.resize_y,
2891                      frame.resize_w - 1, frame.resize_h - 1);
2892     } else {
2893       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2894                      frame.resize_x, frame.resize_y,
2895                      frame.resize_w - 1, frame.resize_h - 1);
2896
2897       int gx, gy;
2898
2899       if (resize_zone & ZoneTop)
2900         frame.resize_h = frame.height - (me->y - frame.grab_y);
2901       else
2902         frame.resize_h = frame.height + (me->y - frame.grab_y);
2903       if (frame.resize_h < 1) frame.resize_h = 1;
2904
2905       if (left) {
2906         frame.resize_x = me->x_root - frame.grab_x;
2907         if (frame.resize_x > (signed) (frame.x + frame.width))
2908           frame.resize_x = frame.resize_x + frame.width - 1;
2909
2910         left_fixsize(&gx, &gy);
2911       } else {
2912         frame.resize_w = frame.width + (me->x - frame.grab_x);
2913         if (frame.resize_w < 1) frame.resize_w = 1;
2914
2915         right_fixsize(&gx, &gy);
2916       }
2917
2918       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2919                      frame.resize_x, frame.resize_y,
2920                      frame.resize_w - 1, frame.resize_h - 1);
2921
2922       screen->showGeometry(gx, gy);
2923     }
2924   }
2925 }
2926
2927
2928 #ifdef    SHAPE
2929 void OpenboxWindow::shapeEvent(XShapeEvent *) {
2930   if (openbox.hasShapeExtensions()) {
2931     if (flags.shaped) {
2932       openbox.grab();
2933       if (! validateClient()) return;
2934       XShapeCombineShape(display, frame.window, ShapeBounding,
2935                          frame.mwm_border_w, frame.y_border +
2936                          frame.mwm_border_w, client.window,
2937                          ShapeBounding, ShapeSet);
2938
2939       int num = 1;
2940       XRectangle xrect[2];
2941       xrect[0].x = xrect[0].y = 0;
2942       xrect[0].width = frame.width;
2943       xrect[0].height = frame.y_border;
2944
2945       if (decorations.handle) {
2946         xrect[1].x = 0;
2947         xrect[1].y = frame.y_handle;
2948         xrect[1].width = frame.width;
2949         xrect[1].height = frame.handle_h + frame.border_w;
2950         num++;
2951       }
2952
2953       XShapeCombineRectangles(display, frame.window, ShapeBounding, 0, 0,
2954                               xrect, num, ShapeUnion, Unsorted);
2955       openbox.ungrab();
2956     }
2957   }
2958 }
2959 #endif // SHAPE
2960
2961
2962 Bool OpenboxWindow::validateClient(void) {
2963   XSync(display, False);
2964
2965   XEvent e;
2966   if (XCheckTypedWindowEvent(display, client.window, DestroyNotify, &e) ||
2967       XCheckTypedWindowEvent(display, client.window, UnmapNotify, &e)) {
2968     XPutBackEvent(display, &e);
2969     openbox.ungrab();
2970
2971     return False;
2972   }
2973
2974   return True;
2975 }
2976
2977
2978 void OpenboxWindow::restore(void) {
2979   XChangeSaveSet(display, client.window, SetModeDelete);
2980   XSelectInput(display, client.window, NoEventMask);
2981
2982   restoreGravity();
2983
2984   XUnmapWindow(display, frame.window);
2985   XUnmapWindow(display, client.window);
2986
2987   XSetWindowBorderWidth(display, client.window, client.old_bw);
2988   XReparentWindow(display, client.window, screen->getRootWindow(),
2989                   client.x, client.y);
2990   XMapWindow(display, client.window);
2991
2992   XFlush(display);
2993 }
2994
2995
2996 void OpenboxWindow::timeout(void) {
2997   screen->getWorkspace(workspace_number)->raiseWindow(this);
2998 }
2999
3000
3001 void OpenboxWindow::changeOpenboxHints(OpenboxHints *net) {
3002   if ((net->flags & AttribShaded) &&
3003       ((openbox_attrib.attrib & AttribShaded) !=
3004        (net->attrib & AttribShaded)))
3005     shade();
3006
3007   if (flags.visible && // watch out for requests when we can not be seen
3008       (net->flags & (AttribMaxVert | AttribMaxHoriz)) &&
3009       ((openbox_attrib.attrib & (AttribMaxVert | AttribMaxHoriz)) !=
3010        (net->attrib & (AttribMaxVert | AttribMaxHoriz)))) {
3011     if (flags.maximized) {
3012       maximize(0);
3013     } else {
3014       int button = 0;
3015
3016       if ((net->flags & AttribMaxHoriz) && (net->flags & AttribMaxVert))
3017         button = ((net->attrib & (AttribMaxHoriz | AttribMaxVert)) ?  1 : 0);
3018       else if (net->flags & AttribMaxVert)
3019         button = ((net->attrib & AttribMaxVert) ? 2 : 0);
3020       else if (net->flags & AttribMaxHoriz)
3021         button = ((net->attrib & AttribMaxHoriz) ? 3 : 0);
3022
3023       maximize(button);
3024     }
3025   }
3026
3027   if ((net->flags & AttribOmnipresent) &&
3028       ((openbox_attrib.attrib & AttribOmnipresent) !=
3029        (net->attrib & AttribOmnipresent)))
3030     stick();
3031
3032   if ((net->flags & AttribWorkspace) &&
3033       (workspace_number != (signed) net->workspace)) {
3034     screen->reassociateWindow(this, net->workspace, True);
3035
3036     if (screen->getCurrentWorkspaceID() != (signed) net->workspace) withdraw();
3037     else deiconify();
3038   }
3039
3040   if (net->flags & AttribDecoration) {
3041     switch (net->decoration) {
3042     case DecorNone:
3043       decorations.titlebar = decorations.border = decorations.handle =
3044        decorations.iconify = decorations.maximize = decorations.menu = False;
3045
3046       break;
3047
3048     default:
3049     case DecorNormal:
3050       decorations.titlebar = decorations.border = decorations.handle =
3051        decorations.iconify = decorations.maximize = decorations.menu = True;
3052
3053       break;
3054
3055     case DecorTiny:
3056       decorations.titlebar = decorations.iconify = decorations.menu = True;
3057       decorations.border = decorations.handle = decorations.maximize = False;
3058  
3059       break;
3060
3061     case DecorTool:
3062       decorations.titlebar = decorations.menu = functions.move = True;
3063       decorations.iconify = decorations.border = decorations.handle =
3064         decorations.maximize = False;
3065
3066       break;
3067     }
3068     if (frame.window) {
3069       XMapSubwindows(display, frame.window);
3070       XMapWindow(display, frame.window);
3071     }
3072
3073     reconfigure();
3074     setState(current_state);
3075   }
3076 }
3077
3078
3079 /*
3080  * Set the sizes of all components of the window frame
3081  * (the window decorations).
3082  * These values are based upon the current style settings and the client
3083  * window's dimentions.
3084  */
3085 void OpenboxWindow::upsize(void) {
3086   frame.bevel_w = screen->getBevelWidth();
3087
3088   if (decorations.border) {
3089     frame.border_w = screen->getBorderWidth();
3090     if (!flags.transient)
3091       frame.mwm_border_w = screen->getFrameWidth();
3092     else
3093       frame.mwm_border_w = 0;
3094   } else {
3095     frame.mwm_border_w = frame.border_w = 0;
3096   }
3097
3098   if (decorations.titlebar) {
3099     // the height of the titlebar is based upon the height of the font being
3100     // used to display the window's title
3101     WindowStyle *style = screen->getWindowStyle();
3102     if (i18n->multibyte())
3103       frame.title_h = (style->fontset_extents->max_ink_extent.height +
3104                        (frame.bevel_w * 2) + 2);
3105     else
3106       frame.title_h = (style->font->ascent + style->font->descent +
3107                        (frame.bevel_w * 2) + 2);
3108
3109     frame.label_h = frame.title_h - (frame.bevel_w * 2);
3110     frame.button_w = frame.button_h = (frame.label_h - 2);
3111     frame.y_border = frame.title_h + frame.border_w;
3112   } else {
3113     frame.title_h = 0;
3114     frame.label_h = 0;
3115     frame.button_w = frame.button_h = 0;
3116     frame.y_border = 0;
3117   }
3118
3119   frame.border_h = client.height + frame.mwm_border_w * 2;
3120
3121   if (decorations.handle) {
3122     frame.y_handle = frame.y_border + frame.border_h + frame.border_w;
3123     frame.grip_w = frame.button_w * 2;
3124     frame.grip_h = frame.handle_h = screen->getHandleWidth();
3125   } else {
3126     frame.y_handle = frame.y_border + frame.border_h;
3127     frame.handle_h = 0;
3128     frame.grip_w = frame.grip_h = 0;
3129   }
3130   
3131   frame.width = client.width + (frame.mwm_border_w * 2);
3132   frame.height = frame.y_handle + frame.handle_h;
3133 }
3134
3135
3136 /*
3137  * Set the size and position of the client window.
3138  * These values are based upon the current style settings and the frame
3139  * window's dimensions.
3140  */
3141 void OpenboxWindow::downsize(void) {
3142   frame.y_handle = frame.height - frame.handle_h;
3143   frame.border_h = frame.y_handle - frame.y_border -
3144     (decorations.handle ? frame.border_w : 0);
3145
3146   client.x = frame.x + frame.mwm_border_w + frame.border_w;
3147   client.y = frame.y + frame.y_border + frame.mwm_border_w + frame.border_w;
3148
3149   client.width = frame.width - (frame.mwm_border_w * 2);
3150   client.height = frame.height - frame.y_border - (frame.mwm_border_w * 2)
3151     - frame.handle_h - (decorations.handle ? frame.border_w : 0);
3152
3153   frame.y_handle = frame.border_h + frame.y_border + frame.border_w;
3154 }
3155
3156
3157 void OpenboxWindow::right_fixsize(int *gx, int *gy) {
3158   // calculate the size of the client window and conform it to the
3159   // size specified by the size hints of the client window...
3160   int dx = frame.resize_w - client.base_width - (frame.mwm_border_w * 2) -
3161     (frame.border_w * 2) + (client.width_inc / 2);
3162   int dy = frame.resize_h - frame.y_border - client.base_height -
3163     frame.handle_h - (frame.border_w * 3) - (frame.mwm_border_w * 2)
3164     + (client.height_inc / 2);
3165
3166   if (dx < (signed) client.min_width) dx = client.min_width;
3167   if (dy < (signed) client.min_height) dy = client.min_height;
3168   if ((unsigned) dx > client.max_width) dx = client.max_width;
3169   if ((unsigned) dy > client.max_height) dy = client.max_height;
3170
3171   dx /= client.width_inc;
3172   dy /= client.height_inc;
3173
3174   if (gx) *gx = dx;
3175   if (gy) *gy = dy;
3176
3177   dx = (dx * client.width_inc) + client.base_width;
3178   dy = (dy * client.height_inc) + client.base_height;
3179
3180   frame.resize_w = dx + (frame.mwm_border_w * 2) + (frame.border_w * 2);
3181   frame.resize_h = dy + frame.y_border + frame.handle_h +
3182                    (frame.mwm_border_w * 2) +  (frame.border_w * 3);
3183   if (resize_zone & ZoneTop)
3184     frame.resize_y = frame.y + frame.height - frame.resize_h +
3185       screen->getBorderWidth() * 2;
3186 }
3187
3188
3189 void OpenboxWindow::left_fixsize(int *gx, int *gy) {
3190   // calculate the size of the client window and conform it to the
3191   // size specified by the size hints of the client window...
3192   int dx = frame.x + frame.width - frame.resize_x - client.base_width -
3193     (frame.mwm_border_w * 2) + (client.width_inc / 2);
3194   int dy = frame.resize_h - frame.y_border - client.base_height -
3195     frame.handle_h - (frame.border_w * 3) - (frame.mwm_border_w * 2)
3196     + (client.height_inc / 2);
3197
3198   if (dx < (signed) client.min_width) dx = client.min_width;
3199   if (dy < (signed) client.min_height) dy = client.min_height;
3200   if ((unsigned) dx > client.max_width) dx = client.max_width;
3201   if ((unsigned) dy > client.max_height) dy = client.max_height;
3202
3203   dx /= client.width_inc;
3204   dy /= client.height_inc;
3205
3206   if (gx) *gx = dx;
3207   if (gy) *gy = dy;
3208
3209   dx = (dx * client.width_inc) + client.base_width;
3210   dy = (dy * client.height_inc) + client.base_height;
3211
3212   frame.resize_w = dx + (frame.mwm_border_w * 2) + (frame.border_w * 2);
3213   frame.resize_x = frame.x + frame.width - frame.resize_w +
3214                    (frame.border_w * 2);
3215   frame.resize_h = dy + frame.y_border + frame.handle_h +
3216                    (frame.mwm_border_w * 2) + (frame.border_w * 3);
3217   if (resize_zone & ZoneTop)
3218     frame.resize_y = frame.y + frame.height - frame.resize_h +
3219       screen->getBorderWidth() * 2;
3220   
3221 }