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