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