]> icculus.org git repositories - mikachu/openbox.git/blob - src/Window.cc
stop moving a window if it is maximized
[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   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 (flags.moving)
1426     endMove();
1427
1428   if (windowmenu) windowmenu->hide();
1429
1430   setState(IconicState);
1431
1432   XSelectInput(display, client.window, NoEventMask);
1433   XUnmapWindow(display, client.window);
1434   XSelectInput(display, client.window,
1435                PropertyChangeMask | StructureNotifyMask | FocusChangeMask);
1436
1437   XUnmapWindow(display, frame.window);
1438   flags.visible = False;
1439   flags.iconic = True;
1440
1441   screen->getWorkspace(workspace_number)->removeWindow(this);
1442
1443   if (flags.transient && client.transient_for &&
1444       !client.transient_for->flags.iconic) {
1445     client.transient_for->iconify();
1446   }
1447   screen->addIcon(this);
1448
1449   if (client.transient && !client.transient->flags.iconic) {
1450     client.transient->iconify();
1451   }
1452 }
1453
1454
1455 void OpenboxWindow::deiconify(bool reassoc, bool raise, bool initial) {
1456   if (flags.iconic || reassoc)
1457     screen->reassociateWindow(this, -1, False);
1458   else if (workspace_number != screen->getCurrentWorkspace()->getWorkspaceID())
1459     return;
1460
1461   setState(NormalState);
1462
1463   XSelectInput(display, client.window, NoEventMask);
1464   XMapWindow(display, client.window);
1465   XSelectInput(display, client.window,
1466                PropertyChangeMask | StructureNotifyMask | FocusChangeMask);
1467
1468   XMapSubwindows(display, frame.window);
1469   XMapWindow(display, frame.window);
1470
1471   // if we're using the click to place placement type, then immediately
1472   // after the window is mapped, we need to start interactively moving it
1473   if (initial && place_window &&
1474       screen->placementPolicy() == BScreen::ClickMousePlacement) {
1475     int x, y, rx, ry;
1476     Window c, r;
1477     unsigned int m;
1478     XQueryPointer(openbox.getXDisplay(), screen->getRootWindow(),
1479                   &r, &c, &rx, &ry, &x, &y, &m);
1480     startMove(rx, ry);
1481   }
1482   
1483   if (flags.iconic && screen->focusNew()) setInputFocus();
1484
1485   flags.visible = True;
1486   flags.iconic = False;
1487
1488   if (reassoc && client.transient) client.transient->deiconify(True, False);
1489
1490   if (raise)
1491     screen->getWorkspace(workspace_number)->raiseWindow(this);
1492 }
1493
1494
1495 void OpenboxWindow::close(void) {
1496   XEvent ce;
1497   ce.xclient.type = ClientMessage;
1498   ce.xclient.message_type = openbox.getWMProtocolsAtom();
1499   ce.xclient.display = display;
1500   ce.xclient.window = client.window;
1501   ce.xclient.format = 32;
1502   ce.xclient.data.l[0] = openbox.getWMDeleteAtom();
1503   ce.xclient.data.l[1] = CurrentTime;
1504   ce.xclient.data.l[2] = 0l;
1505   ce.xclient.data.l[3] = 0l;
1506   ce.xclient.data.l[4] = 0l;
1507   XSendEvent(display, client.window, False, NoEventMask, &ce);
1508 }
1509
1510
1511 void OpenboxWindow::withdraw(void) {
1512   if (flags.moving)
1513     endMove();
1514
1515   flags.visible = False;
1516   flags.iconic = False;
1517
1518   XUnmapWindow(display, frame.window);
1519
1520   XSelectInput(display, client.window, NoEventMask);
1521   XUnmapWindow(display, client.window);
1522   XSelectInput(display, client.window,
1523                PropertyChangeMask | StructureNotifyMask | FocusChangeMask);
1524
1525   if (windowmenu) windowmenu->hide();
1526 }
1527
1528
1529 void OpenboxWindow::maximize(unsigned int button) {
1530   if (flags.moving)
1531     endMove();
1532   
1533   // handle case where menu is open then the max button is used instead
1534   if (windowmenu && windowmenu->isVisible()) windowmenu->hide();
1535
1536   if (flags.maximized) {
1537     flags.maximized = 0;
1538
1539     openbox_attrib.flags &= ! (AttribMaxHoriz | AttribMaxVert);
1540     openbox_attrib.attrib &= ! (AttribMaxHoriz | AttribMaxVert);
1541
1542     // when a resize is begun, maximize(0) is called to clear any maximization
1543     // flags currently set.  Otherwise it still thinks it is maximized.
1544     // so we do not need to call configure() because resizing will handle it
1545     if (!flags.resizing)
1546       configure(openbox_attrib.premax_x, openbox_attrib.premax_y,
1547                 openbox_attrib.premax_w, openbox_attrib.premax_h);
1548
1549     openbox_attrib.premax_x = openbox_attrib.premax_y = 0;
1550     openbox_attrib.premax_w = openbox_attrib.premax_h = 0;
1551
1552     redrawAllButtons();
1553     setState(current_state);
1554     return;
1555   }
1556
1557   openbox_attrib.premax_x = frame.x;
1558   openbox_attrib.premax_y = frame.y;
1559   openbox_attrib.premax_w = frame.width;
1560   openbox_attrib.premax_h = frame.height;
1561
1562   Rect space = screen->availableArea();
1563   unsigned int dw = space.w(),
1564                dh = space.h();
1565   dw -= frame.border_w * 2;
1566   dw -= frame.mwm_border_w * 2;
1567   dw -= client.base_width;
1568
1569   dh -= frame.border_w * 2;
1570   dh -= frame.mwm_border_w * 2;
1571   dh -= ((frame.handle_h + frame.border_w) * decorations.handle);
1572   dh -= client.base_height;
1573   dh -= frame.y_border;
1574
1575   if (dw < client.min_width) dw = client.min_width;
1576   if (dh < client.min_height) dh = client.min_height;
1577   if (dw > client.max_width) dw = client.max_width;
1578   if (dh > client.max_height) dh = client.max_height;
1579
1580   dw -= (dw % client.width_inc);
1581   dw += client.base_width;
1582   dw += frame.mwm_border_w * 2;
1583
1584   dh -= (dh % client.height_inc);
1585   dh += client.base_height;
1586   dh += frame.y_border;
1587   dh += ((frame.handle_h + frame.border_w) * decorations.handle);
1588   dh += frame.mwm_border_w * 2;
1589
1590   int dx = space.x() + ((space.w() - dw) / 2) - frame.border_w,
1591       dy = space.y() + ((space.h() - dh) / 2) - frame.border_w;
1592
1593   switch(button) {
1594   case 1:
1595     openbox_attrib.flags |= AttribMaxHoriz | AttribMaxVert;
1596     openbox_attrib.attrib |= AttribMaxHoriz | AttribMaxVert;
1597     break;
1598
1599   case 2:
1600     openbox_attrib.flags |= AttribMaxVert;
1601     openbox_attrib.attrib |= AttribMaxVert;
1602
1603     dw = frame.width;
1604     dx = frame.x;
1605     break;
1606
1607   case 3:
1608     openbox_attrib.flags |= AttribMaxHoriz;
1609     openbox_attrib.attrib |= AttribMaxHoriz;
1610
1611     dh = frame.height;
1612     dy = frame.y;
1613     break;
1614   }
1615
1616   if (flags.shaded) {
1617     openbox_attrib.flags ^= AttribShaded;
1618     openbox_attrib.attrib ^= AttribShaded;
1619     flags.shaded = False;
1620   }
1621
1622   flags.maximized = button;
1623
1624   configure(dx, dy, dw, dh);
1625   screen->getWorkspace(workspace_number)->raiseWindow(this);
1626   redrawAllButtons();
1627   setState(current_state);
1628 }
1629
1630
1631 void OpenboxWindow::setWorkspace(int n) {
1632   workspace_number = n;
1633
1634   openbox_attrib.flags |= AttribWorkspace;
1635   openbox_attrib.workspace = workspace_number;
1636 }
1637
1638
1639 void OpenboxWindow::shade(void) {
1640   if (!decorations.titlebar)
1641     return;
1642
1643   if (flags.shaded) {
1644     XResizeWindow(display, frame.window, frame.width, frame.height);
1645     flags.shaded = False;
1646     openbox_attrib.flags ^= AttribShaded;
1647     openbox_attrib.attrib ^= AttribShaded;
1648
1649     setState(NormalState);
1650   } else {
1651     XResizeWindow(display, frame.window, frame.width, frame.title_h);
1652     flags.shaded = True;
1653     openbox_attrib.flags |= AttribShaded;
1654     openbox_attrib.attrib |= AttribShaded;
1655
1656     setState(IconicState);
1657   }
1658 }
1659
1660
1661 void OpenboxWindow::stick(void) {
1662   if (flags.stuck) {
1663     openbox_attrib.flags ^= AttribOmnipresent;
1664     openbox_attrib.attrib ^= AttribOmnipresent;
1665
1666     flags.stuck = False;
1667
1668     if (! flags.iconic)
1669       screen->reassociateWindow(this, -1, True);
1670
1671     setState(current_state);
1672   } else {
1673     flags.stuck = True;
1674
1675     openbox_attrib.flags |= AttribOmnipresent;
1676     openbox_attrib.attrib |= AttribOmnipresent;
1677
1678     setState(current_state);
1679   }
1680 }
1681
1682
1683 void OpenboxWindow::setFocusFlag(Bool focus) {
1684   flags.focused = focus;
1685
1686   if (decorations.titlebar) {
1687     if (flags.focused) {
1688       if (frame.ftitle)
1689         XSetWindowBackgroundPixmap(display, frame.title, frame.ftitle);
1690       else
1691         XSetWindowBackground(display, frame.title, frame.ftitle_pixel);
1692     } else {
1693       if (frame.utitle)
1694         XSetWindowBackgroundPixmap(display, frame.title, frame.utitle);
1695       else
1696         XSetWindowBackground(display, frame.title, frame.utitle_pixel);
1697     }
1698     XClearWindow(display, frame.title);
1699
1700     redrawLabel();
1701     redrawAllButtons();
1702   }
1703
1704   if (decorations.handle) {
1705     if (flags.focused) {
1706       if (frame.fhandle)
1707         XSetWindowBackgroundPixmap(display, frame.handle, frame.fhandle);
1708       else
1709         XSetWindowBackground(display, frame.handle, frame.fhandle_pixel);
1710
1711       if (frame.fgrip) {
1712         XSetWindowBackgroundPixmap(display, frame.right_grip, frame.fgrip);
1713         XSetWindowBackgroundPixmap(display, frame.left_grip, frame.fgrip);
1714       } else {
1715         XSetWindowBackground(display, frame.right_grip, frame.fgrip_pixel);
1716         XSetWindowBackground(display, frame.left_grip, frame.fgrip_pixel);
1717       }
1718     } else {
1719       if (frame.uhandle)
1720         XSetWindowBackgroundPixmap(display, frame.handle, frame.uhandle);
1721       else
1722         XSetWindowBackground(display, frame.handle, frame.uhandle_pixel);
1723
1724       if (frame.ugrip) {
1725         XSetWindowBackgroundPixmap(display, frame.right_grip, frame.ugrip);
1726         XSetWindowBackgroundPixmap(display, frame.left_grip, frame.ugrip);
1727       } else {
1728         XSetWindowBackground(display, frame.right_grip, frame.ugrip_pixel);
1729         XSetWindowBackground(display, frame.left_grip, frame.ugrip_pixel);
1730       }
1731     }
1732     XClearWindow(display, frame.handle);
1733     XClearWindow(display, frame.right_grip);
1734     XClearWindow(display, frame.left_grip);
1735   }
1736
1737   if (decorations.border) {
1738     if (flags.focused)
1739       XSetWindowBorder(display, frame.plate, frame.fborder_pixel);
1740     else
1741       XSetWindowBorder(display, frame.plate, frame.uborder_pixel);
1742   }
1743
1744   if (screen->sloppyFocus() && screen->autoRaise() && timer->isTiming())
1745     timer->stop();
1746
1747 }
1748
1749
1750 void OpenboxWindow::installColormap(Bool install) {
1751   openbox.grab();
1752   if (! validateClient()) return;
1753
1754   int i = 0, ncmap = 0;
1755   Colormap *cmaps = XListInstalledColormaps(display, client.window, &ncmap);
1756   XWindowAttributes wattrib;
1757   if (cmaps) {
1758     if (XGetWindowAttributes(display, client.window, &wattrib)) {
1759       if (install) {
1760         // install the window's colormap
1761         for (i = 0; i < ncmap; i++) {
1762           if (*(cmaps + i) == wattrib.colormap)
1763             // this window is using an installed color map... do not install
1764             install = False;
1765         }
1766         // otherwise, install the window's colormap
1767         if (install)
1768           XInstallColormap(display, wattrib.colormap);
1769       } else {
1770         // uninstall the window's colormap
1771         for (i = 0; i < ncmap; i++) {
1772           if (*(cmaps + i) == wattrib.colormap)
1773             // we found the colormap to uninstall
1774             XUninstallColormap(display, wattrib.colormap);
1775         }
1776       }
1777     }
1778
1779     XFree(cmaps);
1780   }
1781
1782   openbox.ungrab();
1783 }
1784
1785
1786 void OpenboxWindow::setState(unsigned long new_state) {
1787   current_state = new_state;
1788
1789   unsigned long state[2];
1790   state[0] = (unsigned long) current_state;
1791   state[1] = (unsigned long) None;
1792   XChangeProperty(display, client.window, openbox.getWMStateAtom(),
1793                   openbox.getWMStateAtom(), 32, PropModeReplace,
1794                   (unsigned char *) state, 2);
1795
1796   XChangeProperty(display, client.window,
1797                   openbox.getOpenboxAttributesAtom(),
1798                   openbox.getOpenboxAttributesAtom(), 32, PropModeReplace,
1799                   (unsigned char *) &openbox_attrib,
1800                   PropOpenboxAttributesElements);
1801 }
1802
1803
1804 Bool OpenboxWindow::getState(void) {
1805   current_state = 0;
1806
1807   Atom atom_return;
1808   Bool ret = False;
1809   int foo;
1810   unsigned long *state, ulfoo, nitems;
1811
1812   if ((XGetWindowProperty(display, client.window, openbox.getWMStateAtom(),
1813                           0l, 2l, False, openbox.getWMStateAtom(),
1814                           &atom_return, &foo, &nitems, &ulfoo,
1815                           (unsigned char **) &state) != Success) ||
1816       (! state)) {
1817     openbox.ungrab();
1818     return False;
1819   }
1820
1821   if (nitems >= 1) {
1822     current_state = (unsigned long) state[0];
1823
1824     ret = True;
1825   }
1826
1827   XFree((void *) state);
1828
1829   return ret;
1830 }
1831
1832
1833 void OpenboxWindow::setGravityOffsets(void) {
1834   // x coordinates for each gravity type
1835   const int x_west = client.x;
1836   const int x_east = client.x + client.width - frame.width;
1837   const int x_center = client.x + client.width - frame.width/2;
1838   // y coordinates for each gravity type
1839   const int y_north = client.y;
1840   const int y_south = client.y + client.height - frame.height;
1841   const int y_center = client.y + client.height - frame.height/2;
1842
1843   switch (client.win_gravity) {
1844   case NorthWestGravity:
1845   default:
1846     frame.x = x_west;
1847     frame.y = y_north;
1848     break;
1849   case NorthGravity:
1850     frame.x = x_center;
1851     frame.y = y_north;
1852     break;
1853   case NorthEastGravity:
1854     frame.x = x_east;
1855     frame.y = y_north;
1856     break;
1857   case SouthWestGravity:
1858     frame.x = x_west;
1859     frame.y = y_south;
1860     break;
1861   case SouthGravity:
1862     frame.x = x_center;
1863     frame.y = y_south;
1864     break;
1865   case SouthEastGravity:
1866     frame.x = x_east;
1867     frame.y = y_south;
1868     break;
1869   case WestGravity:
1870     frame.x = x_west;
1871     frame.y = y_center;
1872     break;
1873   case EastGravity:
1874     frame.x = x_east;
1875     frame.y = y_center;
1876     break;
1877   case CenterGravity:
1878     frame.x = x_center;
1879     frame.y = y_center;
1880     break;
1881   case ForgetGravity:
1882   case StaticGravity:
1883     frame.x = client.x - frame.mwm_border_w + frame.border_w;
1884     frame.y = client.y - frame.y_border - frame.mwm_border_w - frame.border_w;
1885     break;
1886   }
1887 }
1888
1889
1890 void OpenboxWindow::restoreAttributes(void) {
1891   if (! getState()) current_state = NormalState;
1892
1893   Atom atom_return;
1894   int foo;
1895   unsigned long ulfoo, nitems;
1896
1897   OpenboxAttributes *net;
1898   int ret = XGetWindowProperty(display, client.window,
1899                                openbox.getOpenboxAttributesAtom(), 0l,
1900                                PropOpenboxAttributesElements, False,
1901                                openbox.getOpenboxAttributesAtom(),
1902                                &atom_return, &foo, &nitems, &ulfoo,
1903                                (unsigned char **) &net);
1904   if (ret != Success || !net || nitems != PropOpenboxAttributesElements)
1905     return;
1906
1907   openbox_attrib.flags = net->flags;
1908   openbox_attrib.attrib = net->attrib;
1909   openbox_attrib.decoration = net->decoration;
1910   openbox_attrib.workspace = net->workspace;
1911   openbox_attrib.stack = net->stack;
1912   openbox_attrib.premax_x = net->premax_x;
1913   openbox_attrib.premax_y = net->premax_y;
1914   openbox_attrib.premax_w = net->premax_w;
1915   openbox_attrib.premax_h = net->premax_h;
1916
1917   XFree((void *) net);
1918
1919   if (openbox_attrib.flags & AttribShaded &&
1920       openbox_attrib.attrib & AttribShaded) {
1921     int save_state =
1922       ((current_state == IconicState) ? NormalState : current_state);
1923
1924     flags.shaded = False;
1925     shade();
1926
1927     current_state = save_state;
1928   }
1929
1930   if (((int) openbox_attrib.workspace != screen->getCurrentWorkspaceID()) &&
1931       ((int) openbox_attrib.workspace < screen->getWorkspaceCount())) {
1932     screen->reassociateWindow(this, openbox_attrib.workspace, True);
1933
1934     if (current_state == NormalState) current_state = WithdrawnState;
1935   } else if (current_state == WithdrawnState) {
1936     current_state = NormalState;
1937   }
1938
1939   if (openbox_attrib.flags & AttribOmnipresent &&
1940       openbox_attrib.attrib & AttribOmnipresent) {
1941     flags.stuck = False;
1942     stick();
1943
1944     current_state = NormalState;
1945   }
1946
1947   if ((openbox_attrib.flags & AttribMaxHoriz) ||
1948       (openbox_attrib.flags & AttribMaxVert)) {
1949     int x = openbox_attrib.premax_x, y = openbox_attrib.premax_y;
1950     unsigned int w = openbox_attrib.premax_w, h = openbox_attrib.premax_h;
1951     flags.maximized = 0;
1952
1953     unsigned int m = False;
1954     if ((openbox_attrib.flags & AttribMaxHoriz) &&
1955         (openbox_attrib.flags & AttribMaxVert))
1956       m = (openbox_attrib.attrib & (AttribMaxHoriz | AttribMaxVert)) ? 1 : 0;
1957     else if (openbox_attrib.flags & AttribMaxVert)
1958       m = (openbox_attrib.attrib & AttribMaxVert) ? 2 : 0;
1959     else if (openbox_attrib.flags & AttribMaxHoriz)
1960       m = (openbox_attrib.attrib & AttribMaxHoriz) ? 3 : 0;
1961
1962     if (m) maximize(m);
1963
1964     openbox_attrib.premax_x = x;
1965     openbox_attrib.premax_y = y;
1966     openbox_attrib.premax_w = w;
1967     openbox_attrib.premax_h = h;
1968   }
1969
1970   setState(current_state);
1971 }
1972
1973
1974 /*
1975  * The reverse of the setGravityOffsets function. Uses the frame window's
1976  * position to find the window's reference point.
1977  */
1978 void OpenboxWindow::restoreGravity(void) {
1979   // x coordinates for each gravity type
1980   const int x_west = frame.x;
1981   const int x_east = frame.x + frame.width - client.width;
1982   const int x_center = frame.x + (frame.width/2) - client.width;
1983   // y coordinates for each gravity type
1984   const int y_north = frame.y;
1985   const int y_south = frame.y + frame.height - client.height;
1986   const int y_center = frame.y + (frame.height/2) - client.height;
1987
1988   switch(client.win_gravity) {
1989   default:
1990   case NorthWestGravity:
1991     client.x = x_west;
1992     client.y = y_north;
1993     break;
1994   case NorthGravity:
1995     client.x = x_center;
1996     client.y = y_north;
1997     break;
1998   case NorthEastGravity:
1999     client.x = x_east;
2000     client.y = y_north;
2001     break;
2002   case SouthWestGravity:
2003     client.x = x_west;
2004     client.y = y_south;
2005     break;
2006   case SouthGravity:
2007     client.x = x_center;
2008     client.y = y_south;
2009     break;
2010   case SouthEastGravity:
2011     client.x = x_east;
2012     client.y = y_south;
2013     break;
2014   case WestGravity:
2015     client.x = x_west;
2016     client.y = y_center;
2017     break;
2018   case EastGravity:
2019     client.x = x_east;
2020     client.y = y_center;
2021     break;
2022   case CenterGravity:
2023     client.x = x_center;
2024     client.y = y_center;
2025     break;
2026   case ForgetGravity:
2027   case StaticGravity:
2028     client.x = frame.x + frame.mwm_border_w + frame.border_w;
2029     client.y = frame.y + frame.y_border + frame.mwm_border_w +
2030       frame.border_w;
2031     break;
2032   }
2033 }
2034
2035
2036 void OpenboxWindow::redrawLabel(void) {
2037   int dx = frame.bevel_w * 2, dlen = client.title_len;
2038   unsigned int l = client.title_text_w;
2039
2040   if (flags.focused) {
2041     if (frame.flabel)
2042       XSetWindowBackgroundPixmap(display, frame.label, frame.flabel);
2043     else
2044       XSetWindowBackground(display, frame.label, frame.flabel_pixel);
2045   } else {
2046     if (frame.ulabel)
2047       XSetWindowBackgroundPixmap(display, frame.label, frame.ulabel);
2048     else
2049       XSetWindowBackground(display, frame.label, frame.ulabel_pixel);
2050   }
2051   XClearWindow(display, frame.label);
2052
2053   if (client.title_text_w > frame.label_w) {
2054     for (; dlen >= 0; dlen--) {
2055       if (i18n->multibyte()) {
2056         XRectangle ink, logical;
2057         XmbTextExtents(screen->getWindowStyle()->fontset, client.title, dlen,
2058                        &ink, &logical);
2059         l = logical.width;
2060       } else {
2061         l = XTextWidth(screen->getWindowStyle()->font, client.title, dlen);
2062       }
2063       l += (frame.bevel_w * 4);
2064
2065       if (l < frame.label_w)
2066         break;
2067     }
2068   }
2069
2070   switch (screen->getWindowStyle()->justify) {
2071   case BScreen::RightJustify:
2072     dx += frame.label_w - l;
2073     break;
2074
2075   case BScreen::CenterJustify:
2076     dx += (frame.label_w - l) / 2;
2077     break;
2078   }
2079
2080   WindowStyle *style = screen->getWindowStyle();
2081   GC text_gc = (flags.focused) ? style->l_text_focus_gc :
2082     style->l_text_unfocus_gc;
2083   if (i18n->multibyte())
2084     XmbDrawString(display, frame.label, style->fontset, text_gc, dx,
2085                   (1 - style->fontset_extents->max_ink_extent.y),
2086                   client.title, dlen);
2087   else
2088     XDrawString(display, frame.label, text_gc, dx,
2089                 (style->font->ascent + 1), client.title, dlen);
2090 }
2091
2092
2093 void OpenboxWindow::redrawAllButtons(void) {
2094   if (frame.iconify_button) redrawIconifyButton(False);
2095   if (frame.maximize_button) redrawMaximizeButton(flags.maximized);
2096   if (frame.close_button) redrawCloseButton(False);
2097 }
2098
2099
2100 void OpenboxWindow::redrawIconifyButton(Bool pressed) {
2101   if (! pressed) {
2102     if (flags.focused) {
2103       if (frame.fbutton)
2104         XSetWindowBackgroundPixmap(display, frame.iconify_button,
2105                                    frame.fbutton);
2106       else
2107         XSetWindowBackground(display, frame.iconify_button,
2108                              frame.fbutton_pixel);
2109     } else {
2110       if (frame.ubutton)
2111         XSetWindowBackgroundPixmap(display, frame.iconify_button,
2112                                    frame.ubutton);
2113       else
2114         XSetWindowBackground(display, frame.iconify_button,
2115                              frame.ubutton_pixel);
2116     }
2117   } else {
2118     if (frame.pbutton)
2119       XSetWindowBackgroundPixmap(display, frame.iconify_button, frame.pbutton);
2120     else
2121       XSetWindowBackground(display, frame.iconify_button, frame.pbutton_pixel);
2122   }
2123   XClearWindow(display, frame.iconify_button);
2124
2125   XDrawRectangle(display, frame.iconify_button,
2126                  ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2127                   screen->getWindowStyle()->b_pic_unfocus_gc),
2128                  2, (frame.button_h - 5), (frame.button_w - 5), 2);
2129 }
2130
2131
2132 void OpenboxWindow::redrawMaximizeButton(Bool pressed) {
2133   if (! pressed) {
2134     if (flags.focused) {
2135       if (frame.fbutton)
2136         XSetWindowBackgroundPixmap(display, frame.maximize_button,
2137                                    frame.fbutton);
2138       else
2139         XSetWindowBackground(display, frame.maximize_button,
2140                              frame.fbutton_pixel);
2141     } else {
2142       if (frame.ubutton)
2143         XSetWindowBackgroundPixmap(display, frame.maximize_button,
2144                                    frame.ubutton);
2145       else
2146         XSetWindowBackground(display, frame.maximize_button,
2147                              frame.ubutton_pixel);
2148     }
2149   } else {
2150     if (frame.pbutton)
2151       XSetWindowBackgroundPixmap(display, frame.maximize_button,
2152                                  frame.pbutton);
2153     else
2154       XSetWindowBackground(display, frame.maximize_button,
2155                            frame.pbutton_pixel);
2156   }
2157   XClearWindow(display, frame.maximize_button);
2158
2159   XDrawRectangle(display, frame.maximize_button,
2160                  ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2161                   screen->getWindowStyle()->b_pic_unfocus_gc),
2162                  2, 2, (frame.button_w - 5), (frame.button_h - 5));
2163   XDrawLine(display, frame.maximize_button,
2164             ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2165              screen->getWindowStyle()->b_pic_unfocus_gc),
2166             2, 3, (frame.button_w - 3), 3);
2167 }
2168
2169
2170 void OpenboxWindow::redrawCloseButton(Bool pressed) {
2171   if (! pressed) {
2172     if (flags.focused) {
2173       if (frame.fbutton)
2174         XSetWindowBackgroundPixmap(display, frame.close_button,
2175                                    frame.fbutton);
2176       else
2177         XSetWindowBackground(display, frame.close_button,
2178                              frame.fbutton_pixel);
2179     } else {
2180       if (frame.ubutton)
2181         XSetWindowBackgroundPixmap(display, frame.close_button,
2182                                    frame.ubutton);
2183       else
2184         XSetWindowBackground(display, frame.close_button,
2185                              frame.ubutton_pixel);
2186     }
2187   } else {
2188     if (frame.pbutton)
2189       XSetWindowBackgroundPixmap(display, frame.close_button, frame.pbutton);
2190     else
2191       XSetWindowBackground(display, frame.close_button, frame.pbutton_pixel);
2192   }
2193   XClearWindow(display, frame.close_button);
2194
2195   XDrawLine(display, frame.close_button,
2196             ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2197              screen->getWindowStyle()->b_pic_unfocus_gc), 2, 2,
2198             (frame.button_w - 3), (frame.button_h - 3));
2199   XDrawLine(display, frame.close_button,
2200             ((flags.focused) ? screen->getWindowStyle()->b_pic_focus_gc :
2201              screen->getWindowStyle()->b_pic_unfocus_gc), 2,
2202             (frame.button_h - 3),
2203             (frame.button_w - 3), 2);
2204 }
2205
2206
2207 void OpenboxWindow::mapRequestEvent(XMapRequestEvent *re) {
2208   cout << "MAP REQUEST " << client.window << " " << client.title << endl;
2209   if (re->window == client.window) {
2210 #ifdef    DEBUG
2211     fprintf(stderr, i18n->getMessage(WindowSet, WindowMapRequest,
2212                              "OpenboxWindow::mapRequestEvent() for 0x%lx\n"),
2213             client.window);
2214 #endif // DEBUG
2215
2216     openbox.grab();
2217     if (! validateClient()) return;
2218
2219     Bool get_state_ret = getState();
2220     if (! (get_state_ret && openbox.isStartup())) {
2221       if ((client.wm_hint_flags & StateHint) &&
2222           (! (current_state == NormalState || current_state == IconicState)))
2223         current_state = client.initial_state;
2224       else
2225         current_state = NormalState;
2226     } else if (flags.iconic) {
2227       current_state = NormalState;
2228     }
2229
2230     switch (current_state) {
2231     case IconicState:
2232       iconify();
2233       break;
2234
2235     case WithdrawnState:
2236       withdraw();
2237       break;
2238
2239     case NormalState:
2240     case InactiveState:
2241     case ZoomState:
2242     default:
2243       deiconify(False, True, True);     // specify that we're initializing the
2244                                         // window
2245       break;
2246     }
2247
2248     openbox.ungrab();
2249   }
2250 }
2251
2252
2253 void OpenboxWindow::mapNotifyEvent(XMapEvent *ne) {
2254   if ((ne->window == client.window) && (! ne->override_redirect)
2255       && (flags.visible)) {
2256     openbox.grab();
2257     if (! validateClient()) return;
2258
2259     if (decorations.titlebar) positionButtons();
2260
2261     setState(NormalState);
2262
2263     redrawAllButtons();
2264
2265     if (flags.transient || screen->focusNew())
2266       setInputFocus();
2267     else
2268       setFocusFlag(False);
2269
2270     flags.visible = True;
2271     flags.iconic = False;
2272
2273     openbox.ungrab();
2274   }
2275 }
2276
2277
2278 void OpenboxWindow::unmapNotifyEvent(XUnmapEvent *ue) {
2279   if (ue->window == client.window) {
2280 #ifdef    DEBUG
2281     fprintf(stderr, i18n->getMessage(WindowSet, WindowUnmapNotify,
2282                              "OpenboxWindow::unmapNotifyEvent() for 0x%lx\n"),
2283             client.window);
2284 #endif // DEBUG
2285
2286     openbox.grab();
2287     if (! validateClient()) return;
2288
2289     if (flags.moving)
2290       endMove();
2291     
2292     XChangeSaveSet(display, client.window, SetModeDelete);
2293     XSelectInput(display, client.window, NoEventMask);
2294
2295     XDeleteProperty(display, client.window, openbox.getWMStateAtom());
2296     XDeleteProperty(display, client.window,
2297                     openbox.getOpenboxAttributesAtom());
2298
2299     XUnmapWindow(display, frame.window);
2300     XUnmapWindow(display, client.window);
2301
2302     XEvent dummy;
2303     if (! XCheckTypedWindowEvent(display, client.window, ReparentNotify,
2304                                  &dummy)) {
2305 #ifdef    DEBUG
2306       fprintf(stderr, i18n->getMessage(WindowSet, WindowUnmapNotifyReparent,
2307                        "OpenboxWindow::unmapNotifyEvent(): reparent 0x%lx to "
2308                        "root.\n"), client.window);
2309 #endif // DEBUG
2310
2311       restoreGravity();
2312       XReparentWindow(display, client.window, screen->getRootWindow(),
2313                       client.x, client.y);
2314     }
2315
2316     XFlush(display);
2317
2318     openbox.ungrab();
2319
2320     delete this;
2321   }
2322 }
2323
2324
2325 void OpenboxWindow::destroyNotifyEvent(XDestroyWindowEvent *de) {
2326   if (de->window == client.window) {
2327     if (flags.moving)
2328       endMove();
2329     XUnmapWindow(display, frame.window);
2330
2331     delete this;
2332   }
2333 }
2334
2335
2336 void OpenboxWindow::propertyNotifyEvent(Atom atom) {
2337   openbox.grab();
2338   if (! validateClient()) return;
2339
2340   switch(atom) {
2341   case XA_WM_CLASS:
2342   case XA_WM_CLIENT_MACHINE:
2343   case XA_WM_COMMAND:
2344     break;
2345
2346   case XA_WM_TRANSIENT_FOR:
2347     // determine if this is a transient window
2348     Window win;
2349     if (XGetTransientForHint(display, client.window, &win)) {
2350       if (win && (win != client.window)) {
2351         if ((client.transient_for = openbox.searchWindow(win))) {
2352           client.transient_for->client.transient = this;
2353           flags.stuck = client.transient_for->flags.stuck;
2354           flags.transient = True;
2355         } else if (win == client.window_group) {
2356           //jr This doesn't look quite right...
2357           if ((client.transient_for = openbox.searchGroup(win, this))) {
2358             client.transient_for->client.transient = this;
2359             flags.stuck = client.transient_for->flags.stuck;
2360             flags.transient = True;
2361           }
2362         }
2363       }
2364
2365       if (win == screen->getRootWindow()) flags.modal = True;
2366     }
2367
2368     // adjust the window decorations based on transience
2369     if (flags.transient)
2370       decorations.maximize = decorations.handle = functions.maximize = False;
2371
2372     reconfigure();
2373
2374     break;
2375
2376   case XA_WM_HINTS:
2377     getWMHints();
2378     break;
2379
2380   case XA_WM_ICON_NAME:
2381     getWMIconName();
2382     if (flags.iconic) screen->iconUpdate();
2383     break;
2384
2385   case XA_WM_NAME:
2386     getWMName();
2387
2388     if (decorations.titlebar)
2389       redrawLabel();
2390
2391     if (! flags.iconic)
2392       screen->getWorkspace(workspace_number)->update();
2393
2394     break;
2395
2396   case XA_WM_NORMAL_HINTS: {
2397     getWMNormalHints();
2398
2399     if ((client.normal_hint_flags & PMinSize) &&
2400         (client.normal_hint_flags & PMaxSize)) {
2401       if (client.max_width <= client.min_width &&
2402           client.max_height <= client.min_height)
2403         decorations.maximize = decorations.handle =
2404             functions.resize = functions.maximize = False;
2405       else
2406         decorations.maximize = decorations.handle =
2407             functions.resize = functions.maximize = True;
2408     }
2409
2410     int x = frame.x, y = frame.y;
2411     unsigned int w = frame.width, h = frame.height;
2412
2413     upsize();
2414
2415     if ((x != frame.x) || (y != frame.y) ||
2416         (w != frame.width) || (h != frame.height))
2417       reconfigure();
2418
2419     break;
2420   }
2421
2422   default:
2423     if (atom == openbox.getWMProtocolsAtom()) {
2424       getWMProtocols();
2425
2426       if (decorations.close && (! frame.close_button)) {
2427         createCloseButton();
2428         if (decorations.titlebar) positionButtons(True);
2429         if (windowmenu) windowmenu->reconfigure();
2430       }
2431     }
2432
2433     break;
2434   }
2435
2436   openbox.ungrab();
2437 }
2438
2439
2440 void OpenboxWindow::exposeEvent(XExposeEvent *ee) {
2441   if (frame.label == ee->window && decorations.titlebar)
2442     redrawLabel();
2443   else if (frame.close_button == ee->window)
2444     redrawCloseButton(False);
2445   else if (frame.maximize_button == ee->window)
2446     redrawMaximizeButton(flags.maximized);
2447   else if (frame.iconify_button == ee->window)
2448     redrawIconifyButton(False);
2449 }
2450
2451
2452 void OpenboxWindow::configureRequestEvent(XConfigureRequestEvent *cr) {
2453   if (cr->window == client.window) {
2454     openbox.grab();
2455     if (! validateClient()) return;
2456
2457     int cx = frame.x, cy = frame.y;
2458     unsigned int cw = frame.width, ch = frame.height;
2459
2460     if (cr->value_mask & CWBorderWidth)
2461       client.old_bw = cr->border_width;
2462
2463     if (cr->value_mask & CWX)
2464       cx = cr->x - frame.mwm_border_w - frame.border_w;
2465
2466     if (cr->value_mask & CWY)
2467       cy = cr->y - frame.y_border - frame.mwm_border_w -
2468         frame.border_w;
2469
2470     if (cr->value_mask & CWWidth)
2471       cw = cr->width + (frame.mwm_border_w * 2);
2472
2473     if (cr->value_mask & CWHeight)
2474       ch = cr->height + frame.y_border + (frame.mwm_border_w * 2) +
2475         (frame.border_w * decorations.handle) + frame.handle_h;
2476
2477     if (frame.x != cx || frame.y != cy ||
2478         frame.width != cw || frame.height != ch)
2479       configure(cx, cy, cw, ch);
2480
2481     if (cr->value_mask & CWStackMode) {
2482       switch (cr->detail) {
2483       case Above:
2484       case TopIf:
2485       default:
2486         if (flags.iconic) deiconify();
2487         screen->getWorkspace(workspace_number)->raiseWindow(this);
2488         break;
2489
2490       case Below:
2491       case BottomIf:
2492         if (flags.iconic) deiconify();
2493         screen->getWorkspace(workspace_number)->lowerWindow(this);
2494         break;
2495       }
2496     }
2497
2498     openbox.ungrab();
2499   }
2500 }
2501
2502
2503 void OpenboxWindow::buttonPressEvent(XButtonEvent *be) {
2504   openbox.grab();
2505   if (! validateClient())
2506     return;
2507
2508   int stack_change = 1; // < 0 means to lower the window
2509                         // > 0 means to raise the window
2510                         // 0 means to leave it where it is
2511   
2512   // alt + left/right click begins interactively moving/resizing the window
2513   // when the mouse is moved
2514   if (be->state == Mod1Mask && (be->button == 1 || be->button == 3)) {
2515     if (be->button == 3) {
2516       if (screen->getWindowZones() == 4 &&
2517           be->y < (signed) frame.height / 2) {
2518         resize_zone = ZoneTop;
2519       } else {
2520         resize_zone = ZoneBottom;
2521       }
2522       if (screen->getWindowZones() >= 2 &&
2523           be->x < (signed) frame.width / 2) {
2524         resize_zone |= ZoneLeft;
2525       } else {
2526         resize_zone |= ZoneRight;
2527       }
2528     }
2529   // control + left click on the titlebar shades the window
2530   } else if (be->state == ControlMask && be->button == 1) {
2531     if (be->window == frame.title ||
2532         be->window == frame.label)
2533       shade();
2534   // left click
2535   } else if (be->state == 0 && be->button == 1) {
2536     if (windowmenu && windowmenu->isVisible())
2537         windowmenu->hide();
2538
2539     if (be->window == frame.maximize_button) {
2540       redrawMaximizeButton(True);
2541     } else if (be->window == frame.iconify_button) {
2542       redrawIconifyButton(True);
2543     } else if (be->window == frame.close_button) {
2544       redrawCloseButton(True);
2545     } else if (be->window == frame.plate) {
2546       XAllowEvents(display, ReplayPointer, be->time);
2547     } else if (be->window == frame.title ||
2548                be->window == frame.label) {
2549       // shade the window when the titlebar is double clicked
2550       if ( (be->time - lastButtonPressTime) <=
2551             openbox.getDoubleClickInterval()) {
2552         lastButtonPressTime = 0;
2553         shade();
2554       } else {
2555         lastButtonPressTime = be->time;
2556       }
2557       // clicking and dragging on the titlebar moves the window, so on a click
2558       // we need to save the coords of the click in case the user drags
2559       frame.grab_x = be->x_root - frame.x - frame.border_w;
2560       frame.grab_y = be->y_root - frame.y - frame.border_w;
2561     } else if (be->window == frame.handle ||
2562                be->window == frame.left_grip ||
2563                be->window == frame.right_grip ||
2564                be->window == frame.window) {
2565       // clicking and dragging on the window's frame moves the window, so on a
2566       // click we need to save the coords of the click in case the user drags
2567       frame.grab_x = be->x_root - frame.x - frame.border_w;
2568       frame.grab_y = be->y_root - frame.y - frame.border_w;
2569       if (be->window == frame.left_grip)
2570         resize_zone = ZoneBottom | ZoneLeft;
2571       else if (be->window == frame.right_grip)
2572         resize_zone = ZoneBottom | ZoneRight;
2573     }
2574   // middle click
2575   } else if (be->state == 0 && be->button == 2) {
2576     if (be->window == frame.maximize_button) {
2577       redrawMaximizeButton(True);
2578     // a middle click anywhere on the window's frame except for on the buttons
2579     // will lower the window
2580     } else if (! (be->window == frame.iconify_button ||
2581                   be->window == frame.close_button) ) {
2582       stack_change = -1;
2583     }
2584   // right click
2585   } else if (be->state == 0 && be->button == 3) {
2586     if (be->window == frame.maximize_button) {
2587       redrawMaximizeButton(True);
2588     // a right click on the window's frame will show or hide the window's
2589     // windowmenu
2590     } else if (be->window == frame.title ||
2591                be->window == frame.label ||
2592                be->window == frame.handle ||
2593                be->window == frame.window) {
2594       int mx, my;
2595       if (windowmenu) {
2596         if (windowmenu->isVisible()) {
2597           windowmenu->hide();
2598         } else {
2599           // get the coords for the menu
2600           mx = be->x_root - windowmenu->getWidth() / 2;
2601           if (be->window == frame.title || be->window == frame.label) {
2602             my = frame.y + frame.title_h;
2603           } else if (be->window == frame.handle) {
2604             my = frame.y + frame.y_handle - windowmenu->getHeight();
2605           } else { // (be->window == frame.window)
2606             if (be->y <= (signed) frame.bevel_w) {
2607               my = frame.y + frame.y_border;
2608             } else {
2609               my = be->y_root - (windowmenu->getHeight() / 2);
2610             }
2611           }
2612
2613           if (mx > (signed) (frame.x + frame.width -
2614               windowmenu->getWidth())) {
2615             mx = frame.x + frame.width - windowmenu->getWidth();
2616           } else if (mx < frame.x) {
2617             mx = frame.x;
2618           }
2619
2620           if (my > (signed) (frame.y + frame.y_handle -
2621                 windowmenu->getHeight())) {
2622             my = frame.y + frame.y_handle - windowmenu->getHeight();
2623           } else if (my < (signed) (frame.y +
2624               ((decorations.titlebar) ? frame.title_h : frame.y_border))) {
2625             my = frame.y +
2626               ((decorations.titlebar) ? frame.title_h : frame.y_border);
2627           }
2628
2629           windowmenu->move(mx, my);
2630           windowmenu->show();
2631           XRaiseWindow(display, windowmenu->getWindowID());
2632           XRaiseWindow(display, windowmenu->getSendToMenu()->getWindowID());
2633           stack_change = 0;  // dont raise the window overtop of the menu
2634         }
2635       }
2636     }
2637   // mouse wheel up
2638   } else if (be->state == 0 && be->button == 4) {
2639     if ((be->window == frame.label ||
2640         be->window == frame.title) &&
2641         !flags.shaded)
2642       shade();
2643   // mouse wheel down
2644   } else if (be->state == 0 && be->button == 5) {
2645     if ((be->window == frame.label ||
2646         be->window == frame.title) &&
2647         flags.shaded)
2648       shade();
2649   }
2650
2651   if (! (flags.focused || screen->sloppyFocus()) ) {
2652     setInputFocus();  // any click focus' the window in 'click to focus'
2653   }
2654   if (stack_change < 0) {
2655     screen->getWorkspace(workspace_number)->lowerWindow(this);
2656   } else if (stack_change > 0) {
2657     screen->getWorkspace(workspace_number)->raiseWindow(this);
2658   }
2659  
2660   openbox.ungrab();
2661 }
2662
2663
2664 void OpenboxWindow::buttonReleaseEvent(XButtonEvent *re) {
2665   openbox.grab();
2666   if (! validateClient())
2667     return;
2668
2669   // alt + middle button released
2670   if (re->state == (Mod1Mask & Button2Mask) && re->button == 2) {
2671     if (re->window == frame.window) {
2672       XUngrabPointer(display, CurrentTime); // why? i dont know
2673     }
2674   // left button released
2675   } else if (re->button == 1) {
2676     if (re->window == frame.maximize_button) {
2677       if (re->state == Button1Mask && // only the left button was depressed
2678           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2679           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2680         maximize(re->button);
2681       } else {
2682         redrawMaximizeButton(False);
2683       }
2684     } else if (re->window == frame.iconify_button) {
2685       if (re->state == Button1Mask && // only the left button was depressed
2686           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2687           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2688         iconify();
2689       } else {
2690         redrawIconifyButton(False);
2691       }
2692     } else if (re->window == frame.close_button) {
2693       if (re->state == Button1Mask && // only the left button was depressed
2694           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2695           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2696           close();
2697       }
2698       //we should always redraw the close button. some applications
2699       //eg. acroread don't honour the close.
2700       redrawCloseButton(False);
2701     }
2702   // middle button released
2703   } else if (re->button == 2) {
2704     if (re->window == frame.maximize_button) {
2705       if (re->state == Button2Mask && // only the middle button was depressed
2706           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2707           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2708         maximize(re->button);
2709       } else {
2710         redrawMaximizeButton(False);
2711       }
2712     }
2713   // right button released
2714   } else if (re->button == 3) {
2715     if (re->window == frame.maximize_button) {
2716       if (re->state == Button3Mask && // only the right button was depressed
2717           (re->x >= 0) && ((unsigned) re->x <= frame.button_w) &&
2718           (re->y >= 0) && ((unsigned) re->y <= frame.button_h)) {
2719         maximize(re->button);
2720       } else {
2721         redrawMaximizeButton(False);
2722       }
2723     }
2724   }
2725   
2726   // when the window is being interactively moved, a button release stops the
2727   // move where it is
2728   if (flags.moving) {
2729     endMove();
2730   // when the window is being interactively resized, a button release stops the
2731   // resizing
2732   } else if (flags.resizing) {
2733     flags.resizing = False;
2734     XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2735                    frame.resize_x, frame.resize_y,
2736                    frame.resize_w - 1, frame.resize_h - 1);
2737     screen->hideGeometry();
2738     if (resize_zone & ZoneLeft) {
2739       left_fixsize();
2740     } else {  // when resizing with "Alt+Button3", the resize is the same as if
2741               // done with the right grip (the right side of the window is what
2742               // moves)
2743       right_fixsize();
2744     }
2745     // unset maximized state when resized after fully maximized
2746     if (flags.maximized == 1) {
2747         maximize(0);
2748     }
2749     configure(frame.resize_x, frame.resize_y,
2750               frame.resize_w - (frame.border_w * 2),
2751               frame.resize_h - (frame.border_w * 2));
2752     openbox.ungrab();
2753     XUngrabPointer(display, CurrentTime);
2754     resize_zone = 0;
2755   }
2756
2757   openbox.ungrab();
2758 }
2759
2760
2761 void OpenboxWindow::startMove(int x, int y) {
2762   ASSERT(!flags.moving);
2763
2764   // make sure only one window is moving at a time
2765   OpenboxWindow *w = openbox.getMaskedWindow();
2766   if (w != (OpenboxWindow *) 0 && w->flags.moving)
2767     w->endMove();
2768   
2769   XGrabPointer(display, frame.window, False, PointerMotionMask |
2770                ButtonReleaseMask, GrabModeAsync, GrabModeAsync,
2771                None, openbox.getMoveCursor(), CurrentTime);
2772
2773   if (windowmenu && windowmenu->isVisible())
2774     windowmenu->hide();
2775
2776   flags.moving = True;
2777
2778   openbox.maskWindowEvents(client.window, this);
2779
2780   if (! screen->opaqueMove()) {
2781     openbox.grab();
2782
2783     frame.move_x = frame.x;
2784     frame.move_y = frame.y;
2785     frame.resize_w = frame.width + (frame.border_w * 2);
2786     frame.resize_h = ((flags.shaded) ? frame.title_h : frame.height) +
2787       (frame.border_w * 2);
2788
2789     screen->showPosition(frame.x, frame.y);
2790
2791     XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2792                    frame.move_x, frame.move_y,
2793                    frame.resize_w - 1, frame.resize_h - 1);
2794   }
2795   frame.grab_x = x - frame.x - frame.border_w;
2796   frame.grab_y = y - frame.y - frame.border_w;
2797   cout << "START MOVE " << client.window << " " << client.title << endl;
2798 }
2799
2800
2801 void OpenboxWindow::doMove(int x, int y) {
2802   ASSERT(flags.moving);
2803
2804   int dx = x - frame.grab_x, dy = y - frame.grab_y;
2805
2806   dx -= frame.border_w;
2807   dy -= frame.border_w;
2808
2809   int snap_distance = screen->edgeSnapThreshold();
2810   // width/height of the snapping window
2811   unsigned int snap_w = frame.width + (frame.border_w * 2);
2812   unsigned int snap_h = area().h() + (frame.border_w * 2);
2813   if (snap_distance) {
2814     int drx = screen->size().w() - (dx + snap_w);
2815
2816     if (dx < drx && (dx > 0 && dx < snap_distance) ||
2817         (dx < 0 && dx > -snap_distance) )
2818       dx = 0;
2819     else if ( (drx > 0 && drx < snap_distance) ||
2820              (drx < 0 && drx > -snap_distance) )
2821       dx = screen->size().w() - snap_w;
2822
2823     int dtty, dbby, dty, dby;
2824     switch (screen->getToolbar()->placement()) {
2825     case Toolbar::TopLeft:
2826     case Toolbar::TopCenter:
2827     case Toolbar::TopRight:
2828       dtty = screen->getToolbar()->getExposedHeight() +
2829         frame.border_w;
2830       dbby = screen->size().h();
2831       break;
2832
2833     default:
2834       dtty = 0;
2835       dbby = screen->getToolbar()->area().y();
2836       break;
2837     }
2838
2839     dty = dy - dtty;
2840     dby = dbby - (dy + snap_h);
2841
2842     if ( (dy > 0 && dty < snap_distance) ||
2843         (dy < 0 && dty > -snap_distance) )
2844       dy = dtty;
2845     else if ( (dby > 0 && dby < snap_distance) ||
2846              (dby < 0 && dby > -snap_distance) )
2847       dy = dbby - snap_h;
2848   }
2849
2850   if (screen->opaqueMove()) {
2851     configure(dx, dy, frame.width, frame.height);
2852   } else {
2853     XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2854                    frame.move_x, frame.move_y, frame.resize_w - 1,
2855                    frame.resize_h - 1);
2856
2857     frame.move_x = dx;
2858     frame.move_y = dy;
2859
2860     XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2861                    frame.move_x, frame.move_y, frame.resize_w - 1,
2862                    frame.resize_h - 1);
2863   }
2864
2865   screen->showPosition(dx, dy);
2866 }
2867
2868
2869 void OpenboxWindow::endMove() {
2870   ASSERT(flags.moving);
2871
2872   flags.moving = False;
2873
2874   openbox.maskWindowEvents(0, (OpenboxWindow *) 0);
2875   if (!screen->opaqueMove()) {
2876     XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2877                    frame.move_x, frame.move_y, frame.resize_w - 1,
2878                    frame.resize_h - 1);
2879
2880     configure(frame.move_x, frame.move_y, frame.width, frame.height);
2881     openbox.ungrab();
2882   } else {
2883     configure(frame.x, frame.y, frame.width, frame.height);
2884   }
2885   screen->hideGeometry();
2886   XUngrabPointer(display, CurrentTime);
2887   // if there are any left over motions from the move, drop them now cuz they
2888   // cause problems
2889   XEvent e;
2890   while (XCheckTypedWindowEvent(display, frame.window, MotionNotify, &e));
2891   cout << "END MOVE " << client.window << " " << client.title << endl;
2892 }
2893
2894
2895 void OpenboxWindow::motionNotifyEvent(XMotionEvent *me) {
2896   if (flags.moving)
2897       doMove(me->x_root, me->y_root);
2898   else if (!flags.resizing && (me->state & Button1Mask) && functions.move &&
2899       (frame.title == me->window || frame.label == me->window ||
2900        frame.handle == me->window || frame.window == me->window))
2901     startMove(me->x_root, me->y_root);
2902   else if (functions.resize &&
2903              (((me->state & Button1Mask) && (me->window == frame.right_grip ||
2904                                              me->window == frame.left_grip)) ||
2905               (me->state == (Mod1Mask | Button3Mask) &&
2906                                              me->window == frame.window))) {
2907     Bool left = resize_zone & ZoneLeft;
2908
2909     if (! flags.resizing) {
2910       cout << "START RESIZE " << client.window << " " << client.title << endl;
2911       Cursor cursor;
2912       if (resize_zone & ZoneTop)
2913         cursor = (resize_zone & ZoneLeft) ?
2914           openbox.getUpperLeftAngleCursor() :
2915           openbox.getUpperRightAngleCursor();
2916       else
2917         cursor = (resize_zone & ZoneLeft) ?
2918           openbox.getLowerLeftAngleCursor() :
2919           openbox.getLowerRightAngleCursor();
2920       XGrabPointer(display, me->window, False, ButtonMotionMask |
2921                    ButtonReleaseMask, GrabModeAsync, GrabModeAsync, None,
2922                    cursor, CurrentTime);
2923
2924       flags.resizing = True;
2925
2926       openbox.grab();
2927
2928       int gx, gy;
2929       if (resize_zone & ZoneRight)
2930         frame.grab_x = me->x - screen->getBorderWidth();
2931       else
2932         frame.grab_x = me->x + screen->getBorderWidth();
2933       if (resize_zone & ZoneTop)
2934         frame.grab_y = me->y + screen->getBorderWidth() * 2;
2935       else
2936         frame.grab_y = me->y - screen->getBorderWidth() * 2;
2937       frame.resize_x = frame.x;
2938       frame.resize_y = frame.y;
2939       frame.resize_w = frame.width + (frame.border_w * 2);
2940       frame.resize_h = frame.height + (frame.border_w * 2);
2941
2942       if (left)
2943         left_fixsize(&gx, &gy);
2944       else
2945         right_fixsize(&gx, &gy);
2946
2947       screen->showGeometry(gx, gy);
2948
2949       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2950                      frame.resize_x, frame.resize_y,
2951                      frame.resize_w - 1, frame.resize_h - 1);
2952     } else {
2953       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2954                      frame.resize_x, frame.resize_y,
2955                      frame.resize_w - 1, frame.resize_h - 1);
2956
2957       int gx, gy;
2958
2959       if (resize_zone & ZoneTop)
2960         frame.resize_h = frame.height - (me->y - frame.grab_y);
2961       else
2962         frame.resize_h = frame.height + (me->y - frame.grab_y);
2963       if (frame.resize_h < 1) frame.resize_h = 1;
2964
2965       if (left) {
2966         frame.resize_x = me->x_root - frame.grab_x;
2967         if (frame.resize_x > (signed) (frame.x + frame.width))
2968           frame.resize_x = frame.resize_x + frame.width - 1;
2969
2970         left_fixsize(&gx, &gy);
2971       } else {
2972         frame.resize_w = frame.width + (me->x - frame.grab_x);
2973         if (frame.resize_w < 1) frame.resize_w = 1;
2974
2975         right_fixsize(&gx, &gy);
2976       }
2977
2978       XDrawRectangle(display, screen->getRootWindow(), screen->getOpGC(),
2979                      frame.resize_x, frame.resize_y,
2980                      frame.resize_w - 1, frame.resize_h - 1);
2981
2982       screen->showGeometry(gx, gy);
2983     }
2984   } else
2985     cout << "MOTION " << client.window << " " << client.title << endl;
2986 }
2987
2988
2989 #ifdef    SHAPE
2990 void OpenboxWindow::shapeEvent(XShapeEvent *) {
2991   if (openbox.hasShapeExtensions()) {
2992     if (flags.shaped) {
2993       openbox.grab();
2994       if (! validateClient()) return;
2995       XShapeCombineShape(display, frame.window, ShapeBounding,
2996                          frame.mwm_border_w, frame.y_border +
2997                          frame.mwm_border_w, client.window,
2998                          ShapeBounding, ShapeSet);
2999
3000       int num = 1;
3001       XRectangle xrect[2];
3002       xrect[0].x = xrect[0].y = 0;
3003       xrect[0].width = frame.width;
3004       xrect[0].height = frame.y_border;
3005
3006       if (decorations.handle) {
3007         xrect[1].x = 0;
3008         xrect[1].y = frame.y_handle;
3009         xrect[1].width = frame.width;
3010         xrect[1].height = frame.handle_h + frame.border_w;
3011         num++;
3012       }
3013
3014       XShapeCombineRectangles(display, frame.window, ShapeBounding, 0, 0,
3015                               xrect, num, ShapeUnion, Unsorted);
3016       openbox.ungrab();
3017     }
3018   }
3019 }
3020 #endif // SHAPE
3021
3022
3023 Bool OpenboxWindow::validateClient(void) {
3024   XSync(display, False);
3025
3026   XEvent e;
3027   if (XCheckTypedWindowEvent(display, client.window, DestroyNotify, &e) ||
3028       XCheckTypedWindowEvent(display, client.window, UnmapNotify, &e)) {
3029     XPutBackEvent(display, &e);
3030     openbox.ungrab();
3031
3032     return False;
3033   }
3034
3035   return True;
3036 }
3037
3038
3039 void OpenboxWindow::restore(void) {
3040   XChangeSaveSet(display, client.window, SetModeDelete);
3041   XSelectInput(display, client.window, NoEventMask);
3042
3043   restoreGravity();
3044
3045   XUnmapWindow(display, frame.window);
3046   XUnmapWindow(display, client.window);
3047
3048   XSetWindowBorderWidth(display, client.window, client.old_bw);
3049   XReparentWindow(display, client.window, screen->getRootWindow(),
3050                   client.x, client.y);
3051   XMapWindow(display, client.window);
3052
3053   XFlush(display);
3054 }
3055
3056
3057 void OpenboxWindow::timeout(void) {
3058   screen->getWorkspace(workspace_number)->raiseWindow(this);
3059 }
3060
3061
3062 void OpenboxWindow::changeOpenboxHints(OpenboxHints *net) {
3063   if ((net->flags & AttribShaded) &&
3064       ((openbox_attrib.attrib & AttribShaded) !=
3065        (net->attrib & AttribShaded)))
3066     shade();
3067
3068   if (flags.visible && // watch out for requests when we can not be seen
3069       (net->flags & (AttribMaxVert | AttribMaxHoriz)) &&
3070       ((openbox_attrib.attrib & (AttribMaxVert | AttribMaxHoriz)) !=
3071        (net->attrib & (AttribMaxVert | AttribMaxHoriz)))) {
3072     if (flags.maximized) {
3073       maximize(0);
3074     } else {
3075       int button = 0;
3076
3077       if ((net->flags & AttribMaxHoriz) && (net->flags & AttribMaxVert))
3078         button = ((net->attrib & (AttribMaxHoriz | AttribMaxVert)) ?  1 : 0);
3079       else if (net->flags & AttribMaxVert)
3080         button = ((net->attrib & AttribMaxVert) ? 2 : 0);
3081       else if (net->flags & AttribMaxHoriz)
3082         button = ((net->attrib & AttribMaxHoriz) ? 3 : 0);
3083
3084       maximize(button);
3085     }
3086   }
3087
3088   if ((net->flags & AttribOmnipresent) &&
3089       ((openbox_attrib.attrib & AttribOmnipresent) !=
3090        (net->attrib & AttribOmnipresent)))
3091     stick();
3092
3093   if ((net->flags & AttribWorkspace) &&
3094       (workspace_number != (signed) net->workspace)) {
3095     screen->reassociateWindow(this, net->workspace, True);
3096
3097     if (screen->getCurrentWorkspaceID() != (signed) net->workspace) withdraw();
3098     else deiconify();
3099   }
3100
3101   if (net->flags & AttribDecoration) {
3102     switch (net->decoration) {
3103     case DecorNone:
3104       decorations.titlebar = decorations.border = decorations.handle =
3105        decorations.iconify = decorations.maximize = decorations.menu = False;
3106
3107       break;
3108
3109     default:
3110     case DecorNormal:
3111       decorations.titlebar = decorations.iconify = decorations.menu =
3112         decorations.border = True;
3113       decorations.handle = (functions.resize && !flags.transient);
3114       decorations.maximize = functions.maximize;
3115
3116       break;
3117
3118     case DecorTiny:
3119       decorations.titlebar = decorations.iconify = decorations.menu = True;
3120       decorations.border = decorations.border = decorations.handle = False;
3121       decorations.maximize = functions.maximize;
3122
3123       break;
3124
3125     case DecorTool:
3126       decorations.titlebar = decorations.menu = True;
3127       decorations.iconify = decorations.border = False;
3128       decorations.handle = (functions.resize && !flags.transient);
3129       decorations.maximize = functions.maximize;
3130
3131       break;
3132     }
3133     if (frame.window) {
3134       XMapSubwindows(display, frame.window);
3135       XMapWindow(display, frame.window);
3136     }
3137
3138     reconfigure();
3139     setState(current_state);
3140   }
3141 }
3142
3143
3144 /*
3145  * Set the sizes of all components of the window frame
3146  * (the window decorations).
3147  * These values are based upon the current style settings and the client
3148  * window's dimentions.
3149  */
3150 void OpenboxWindow::upsize(void) {
3151   frame.bevel_w = screen->getBevelWidth();
3152
3153   if (decorations.border) {
3154     frame.border_w = screen->getBorderWidth();
3155     if (!flags.transient)
3156       frame.mwm_border_w = screen->getFrameWidth();
3157     else
3158       frame.mwm_border_w = 0;
3159   } else {
3160     frame.mwm_border_w = frame.border_w = 0;
3161   }
3162
3163   if (decorations.titlebar) {
3164     // the height of the titlebar is based upon the height of the font being
3165     // used to display the window's title
3166     WindowStyle *style = screen->getWindowStyle();
3167     if (i18n->multibyte())
3168       frame.title_h = (style->fontset_extents->max_ink_extent.height +
3169                        (frame.bevel_w * 2) + 2);
3170     else
3171       frame.title_h = (style->font->ascent + style->font->descent +
3172                        (frame.bevel_w * 2) + 2);
3173
3174     frame.label_h = frame.title_h - (frame.bevel_w * 2);
3175     frame.button_w = frame.button_h = (frame.label_h - 2);
3176     frame.y_border = frame.title_h + frame.border_w;
3177   } else {
3178     frame.title_h = 0;
3179     frame.label_h = 0;
3180     frame.button_w = frame.button_h = 0;
3181     frame.y_border = 0;
3182   }
3183
3184   frame.border_h = client.height + frame.mwm_border_w * 2;
3185
3186   if (decorations.handle) {
3187     frame.y_handle = frame.y_border + frame.border_h + frame.border_w;
3188     frame.grip_w = frame.button_w * 2;
3189     frame.grip_h = frame.handle_h = screen->getHandleWidth();
3190   } else {
3191     frame.y_handle = frame.y_border + frame.border_h;
3192     frame.handle_h = 0;
3193     frame.grip_w = frame.grip_h = 0;
3194   }
3195   
3196   frame.width = client.width + (frame.mwm_border_w * 2);
3197   frame.height = frame.y_handle + frame.handle_h;
3198 }
3199
3200
3201 /*
3202  * Set the size and position of the client window.
3203  * These values are based upon the current style settings and the frame
3204  * window's dimensions.
3205  */
3206 void OpenboxWindow::downsize(void) {
3207   frame.y_handle = frame.height - frame.handle_h;
3208   frame.border_h = frame.y_handle - frame.y_border -
3209     (decorations.handle ? frame.border_w : 0);
3210
3211   client.x = frame.x + frame.mwm_border_w + frame.border_w;
3212   client.y = frame.y + frame.y_border + frame.mwm_border_w + frame.border_w;
3213
3214   client.width = frame.width - (frame.mwm_border_w * 2);
3215   client.height = frame.height - frame.y_border - (frame.mwm_border_w * 2)
3216     - frame.handle_h - (decorations.handle ? frame.border_w : 0);
3217
3218   frame.y_handle = frame.border_h + frame.y_border + frame.border_w;
3219 }
3220
3221
3222 void OpenboxWindow::right_fixsize(int *gx, int *gy) {
3223   // calculate the size of the client window and conform it to the
3224   // size specified by the size hints of the client window...
3225   int dx = frame.resize_w - client.base_width - (frame.mwm_border_w * 2) -
3226     (frame.border_w * 2) + (client.width_inc / 2);
3227   int dy = frame.resize_h - frame.y_border - client.base_height -
3228     frame.handle_h - (frame.border_w * 3) - (frame.mwm_border_w * 2)
3229     + (client.height_inc / 2);
3230
3231   if (dx < (signed) client.min_width) dx = client.min_width;
3232   if (dy < (signed) client.min_height) dy = client.min_height;
3233   if ((unsigned) dx > client.max_width) dx = client.max_width;
3234   if ((unsigned) dy > client.max_height) dy = client.max_height;
3235
3236   dx /= client.width_inc;
3237   dy /= client.height_inc;
3238
3239   if (gx) *gx = dx;
3240   if (gy) *gy = dy;
3241
3242   dx = (dx * client.width_inc) + client.base_width;
3243   dy = (dy * client.height_inc) + client.base_height;
3244
3245   frame.resize_w = dx + (frame.mwm_border_w * 2) + (frame.border_w * 2);
3246   frame.resize_h = dy + frame.y_border + frame.handle_h +
3247                    (frame.mwm_border_w * 2) +  (frame.border_w * 3);
3248   if (resize_zone & ZoneTop)
3249     frame.resize_y = frame.y + frame.height - frame.resize_h +
3250       screen->getBorderWidth() * 2;
3251 }
3252
3253
3254 void OpenboxWindow::left_fixsize(int *gx, int *gy) {
3255   // calculate the size of the client window and conform it to the
3256   // size specified by the size hints of the client window...
3257   int dx = frame.x + frame.width - frame.resize_x - client.base_width -
3258     (frame.mwm_border_w * 2) + (client.width_inc / 2);
3259   int dy = frame.resize_h - frame.y_border - client.base_height -
3260     frame.handle_h - (frame.border_w * 3) - (frame.mwm_border_w * 2)
3261     + (client.height_inc / 2);
3262
3263   if (dx < (signed) client.min_width) dx = client.min_width;
3264   if (dy < (signed) client.min_height) dy = client.min_height;
3265   if ((unsigned) dx > client.max_width) dx = client.max_width;
3266   if ((unsigned) dy > client.max_height) dy = client.max_height;
3267
3268   dx /= client.width_inc;
3269   dy /= client.height_inc;
3270
3271   if (gx) *gx = dx;
3272   if (gy) *gy = dy;
3273
3274   dx = (dx * client.width_inc) + client.base_width;
3275   dy = (dy * client.height_inc) + client.base_height;
3276
3277   frame.resize_w = dx + (frame.mwm_border_w * 2) + (frame.border_w * 2);
3278   frame.resize_x = frame.x + frame.width - frame.resize_w +
3279                    (frame.border_w * 2);
3280   frame.resize_h = dy + frame.y_border + frame.handle_h +
3281                    (frame.mwm_border_w * 2) + (frame.border_w * 3);
3282   if (resize_zone & ZoneTop)
3283     frame.resize_y = frame.y + frame.height - frame.resize_h +
3284       screen->getBorderWidth() * 2;
3285   
3286 }