fixed restart crash bug
[wmaker-crm.git] / src / window.c
blob2d7b696a177ac1a6470e1dfef82a9902b9263446
1 /* window.c - client window managing stuffs
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
38 #include "WindowMaker.h"
39 #include "GNUstep.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "texture.h"
43 #include "window.h"
44 #include "winspector.h"
45 #include "icon.h"
46 #include "properties.h"
47 #include "actions.h"
48 #include "client.h"
49 #include "funcs.h"
50 #include "keybind.h"
51 #include "stacking.h"
52 #include "defaults.h"
53 #include "workspace.h"
56 #ifdef MWM_HINTS
57 # include "motif.h"
58 #endif
59 #ifdef KWM_HINTS
60 # include "kwm.h"
61 #endif
62 #ifdef GNOME_STUFF
63 # include "gnome.h"
64 #endif
65 #ifdef OLWM_HINTS
66 # include "openlook.h"
67 #endif
69 /****** Global Variables ******/
71 extern WShortKey wKeyBindings[WKBD_LAST];
73 #ifdef SHAPE
74 extern Bool wShapeSupported;
75 #endif
77 /* contexts */
78 extern XContext wWinContext;
80 /* cursors */
81 extern Cursor wCursor[WCUR_LAST];
83 /* protocol atoms */
84 extern Atom _XA_WM_DELETE_WINDOW;
85 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
87 extern Atom _XA_WINDOWMAKER_STATE;
89 extern WPreferences wPreferences;
91 #define MOD_MASK wPreferences.modifier_mask
93 extern Time LastTimestamp;
95 /* superfluous... */
96 extern void DoWindowBirth(WWindow*);
99 /***** Local Stuff *****/
102 static WWindowState *windowState=NULL;
106 /* local functions */
107 static FocusMode getFocusMode(WWindow *wwin);
109 static int getSavedState(Window window, WSavedState **state);
111 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
113 /* event handlers */
116 /* frame window (during window grabs) */
117 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
119 /* close button */
120 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
121 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
123 /* iconify button */
124 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
126 #ifdef XKB_BUTTON_HINT
127 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
128 #endif
130 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
131 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
133 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
136 /****** Notification Observers ******/
138 static void
139 appearanceObserver(void *self, WMNotification *notif)
141 WWindow *wwin = (WWindow*)self;
142 int flags = (int)WMGetNotificationClientData(notif);
144 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
145 return;
147 if (flags & WFontSettings) {
148 wWindowConfigureBorders(wwin);
149 if(wwin->flags.shaded) {
150 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
151 wwin->frame->top_width - 1);
153 wwin->client.y = wwin->frame_y - wwin->client.height
154 + wwin->frame->top_width;
155 wWindowSynthConfigureNotify(wwin);
158 if (flags & WTextureSettings) {
159 wwin->frame->flags.need_texture_remake = 1;
161 if (flags & (WTextureSettings | WColorSettings)) {
162 if (wwin->frame->titlebar)
163 XClearWindow(dpy, wwin->frame->titlebar->window);
165 wFrameWindowPaint(wwin->frame);
169 /************************************/
171 WWindow*
172 wWindowFor(Window window)
174 WObjDescriptor *desc;
176 if (window==None)
177 return NULL;
179 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
180 return NULL;
182 if (desc->parent_type==WCLASS_WINDOW)
183 return desc->parent;
184 else if (desc->parent_type==WCLASS_FRAME) {
185 WFrameWindow *frame = (WFrameWindow*)desc->parent;
186 if (frame->flags.is_client_window_frame)
187 return frame->child;
190 return NULL;
194 WWindow*
195 wWindowCreate()
197 WWindow *wwin;
199 wwin = wmalloc(sizeof(WWindow));
200 wretain(wwin);
202 memset(wwin, 0, sizeof(WWindow));
204 wwin->client_descriptor.handle_mousedown = frameMouseDown;
205 wwin->client_descriptor.parent = wwin;
206 wwin->client_descriptor.self = wwin;
207 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
209 return wwin;
213 void
214 wWindowDestroy(WWindow *wwin)
216 int i;
218 if (wwin->screen_ptr->cmap_window == wwin) {
219 wwin->screen_ptr->cmap_window = NULL;
222 WMRemoveNotificationObserver(wwin);
224 wwin->flags.destroyed = 1;
226 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
227 if (!wwin->screen_ptr->shortcutWindows[i])
228 continue;
230 WMRemoveFromBag(wwin->screen_ptr->shortcutWindows[i], wwin);
232 if (!WMGetBagItemCount(wwin->screen_ptr->shortcutWindows[i])) {
233 WMFreeBag(wwin->screen_ptr->shortcutWindows[i]);
234 wwin->screen_ptr->shortcutWindows[i] = NULL;
238 if (wwin->normal_hints)
239 XFree(wwin->normal_hints);
241 if (wwin->wm_hints)
242 XFree(wwin->wm_hints);
244 if (wwin->wm_instance)
245 XFree(wwin->wm_instance);
247 if (wwin->wm_class)
248 XFree(wwin->wm_class);
250 if (wwin->wm_gnustep_attr)
251 free(wwin->wm_gnustep_attr);
253 if (wwin->cmap_windows)
254 XFree(wwin->cmap_windows);
256 XDeleteContext(dpy, wwin->client_win, wWinContext);
258 if (wwin->frame)
259 wFrameWindowDestroy(wwin->frame);
261 if (wwin->icon) {
262 RemoveFromStackList(wwin->icon->core);
263 wIconDestroy(wwin->icon);
264 if (wPreferences.auto_arrange_icons)
265 wArrangeIcons(wwin->screen_ptr, True);
267 wrelease(wwin);
273 static void
274 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
276 if (gs_hints->flags & GSWindowStyleAttr) {
277 if (gs_hints->window_style == WMBorderlessWindowMask) {
278 wwin->client_flags.no_border = 1;
279 wwin->client_flags.no_titlebar = 1;
280 wwin->client_flags.no_closable = 1;
281 wwin->client_flags.no_miniaturizable = 1;
282 wwin->client_flags.no_resizable = 1;
283 wwin->client_flags.no_close_button = 1;
284 wwin->client_flags.no_miniaturize_button = 1;
285 wwin->client_flags.no_resizebar = 1;
286 } else {
287 wwin->client_flags.no_close_button =
288 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
290 wwin->client_flags.no_closable =
291 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
293 wwin->client_flags.no_miniaturize_button =
294 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
296 wwin->client_flags.no_miniaturizable =
297 wwin->client_flags.no_miniaturize_button;
299 wwin->client_flags.no_resizebar =
300 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
302 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
304 /* these attributes supposedly imply in the existence
305 * of a titlebar */
306 if (gs_hints->window_style & (WMResizableWindowMask|
307 WMClosableWindowMask|
308 WMMiniaturizableWindowMask)) {
309 wwin->client_flags.no_titlebar = 0;
310 } else {
311 wwin->client_flags.no_titlebar =
312 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
316 } else {
317 /* setup the defaults */
318 wwin->client_flags.no_border = 0;
319 wwin->client_flags.no_titlebar = 0;
320 wwin->client_flags.no_closable = 0;
321 wwin->client_flags.no_miniaturizable = 0;
322 wwin->client_flags.no_resizable = 0;
323 wwin->client_flags.no_close_button = 0;
324 wwin->client_flags.no_miniaturize_button = 0;
325 wwin->client_flags.no_resizebar = 0;
327 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
328 wwin->client_flags.no_appicon = 1;
333 void
334 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
335 WWindowAttributes *mask)
337 if (wflags->no_appicon && mask->no_appicon)
338 wflags->emulate_appicon = 0;
340 if (wwin->main_window!=None) {
341 WApplication *wapp = wApplicationOf(wwin->main_window);
342 if (wapp && !wapp->flags.emulated)
343 wflags->emulate_appicon = 0;
346 if (wwin->transient_for!=None
347 && wwin->transient_for!=wwin->screen_ptr->root_win)
348 wflags->emulate_appicon = 0;
350 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
351 wflags->sunken = 0;
356 void
357 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
359 WScreen *scr = wwin->screen_ptr;
361 /* sets global default stuff */
362 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
363 &wwin->client_flags, NULL, True);
365 * Decoration setting is done in this precedence (lower to higher)
366 * - use global default in the resource database
367 * - guess some settings
368 * - use GNUstep/external window attributes
369 * - set hints specified for the app in the resource DB
372 WSETUFLAG(wwin, broken_close, 0);
374 if (wwin->protocols.DELETE_WINDOW)
375 WSETUFLAG(wwin, kill_close, 0);
376 else
377 WSETUFLAG(wwin, kill_close, 1);
379 /* transients can't be iconified or maximized */
380 if (wwin->transient_for) {
381 WSETUFLAG(wwin, no_miniaturizable, 1);
382 WSETUFLAG(wwin, no_miniaturize_button, 1);
385 /* if the window can't be resized, remove the resizebar */
386 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
387 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
388 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
389 WSETUFLAG(wwin, no_resizable, 1);
390 WSETUFLAG(wwin, no_resizebar, 1);
393 /* set GNUstep window attributes */
394 if (wwin->wm_gnustep_attr) {
395 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
397 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
399 switch (wwin->wm_gnustep_attr->window_level) {
400 case WMNormalWindowLevel:
401 *level = WMNormalLevel;
402 break;
403 case WMFloatingWindowLevel:
404 *level = WMFloatingLevel;
405 break;
406 case WMDockWindowLevel:
407 *level = WMDockLevel;
408 break;
409 case WMSubmenuWindowLevel:
410 *level = WMSubmenuLevel;
411 break;
412 case WMMainMenuWindowLevel:
413 *level = WMMainMenuLevel;
414 break;
415 default:
416 *level = WMNormalLevel;
417 break;
419 } else {
420 /* setup defaults */
421 *level = WMNormalLevel;
423 } else {
424 int tmp_workspace = -1;
425 int tmp_level = -1;
426 Bool check;
428 check = False;
430 #ifdef MWM_HINTS
431 wMWMCheckClientHints(wwin);
432 #endif /* MWM_HINTS */
434 #ifdef GNOME_STUFF
435 check = wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
436 #endif /* GNOME_STUFF */
438 #ifdef KWM_HINTS
439 if (!check)
440 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
441 #endif /* KWM_HINTS */
443 #ifdef OLWM_HINTS
444 wOLWMCheckClientHints(wwin);
445 #endif /* OLWM_HINTS */
447 if (tmp_level < 0) {
448 if (WFLAGP(wwin, floating))
449 *level = WMFloatingLevel;
450 else if (WFLAGP(wwin, sunken))
451 *level = WMSunkenLevel;
452 else
453 *level = WMNormalLevel;
454 } else {
455 *level = tmp_level;
458 if (tmp_workspace >= 0) {
459 *workspace = tmp_workspace % scr->workspace_count;
464 * Set attributes specified only for that window/class.
465 * This might do duplicate work with the 1st wDefaultFillAttributes().
467 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
468 &wwin->user_flags, &wwin->defined_user_flags,
469 False);
471 * Sanity checks for attributes that depend on other attributes
473 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
474 wwin->user_flags.emulate_appicon = 0;
476 if (wwin->main_window!=None) {
477 WApplication *wapp = wApplicationOf(wwin->main_window);
478 if (wapp && !wapp->flags.emulated)
479 wwin->user_flags.emulate_appicon = 0;
482 if (wwin->transient_for!=None
483 && wwin->transient_for!=wwin->screen_ptr->root_win)
484 wwin->user_flags.emulate_appicon = 0;
486 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
487 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
488 wwin->user_flags.sunken = 0;
490 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
496 Bool
497 wWindowCanReceiveFocus(WWindow *wwin)
499 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
500 return False;
501 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
502 return False;
503 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
504 return False;
506 return True;
510 Bool
511 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
513 int w1, h1, w2, h2;
515 w1 = wwin->frame->core->width;
516 h1 = wwin->frame->core->height;
517 w2 = obscured->frame->core->width;
518 h2 = obscured->frame->core->height;
520 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
521 && wwin->frame->workspace != obscured->frame->workspace)
522 return False;
524 if (wwin->frame_x + w1 < obscured->frame_x
525 || wwin->frame_y + h1 < obscured->frame_y
526 || wwin->frame_x > obscured->frame_x + w2
527 || wwin->frame_y > obscured->frame_y + h2) {
528 return False;
531 return True;
536 *----------------------------------------------------------------
537 * wManageWindow--
538 * reparents the window and allocates a descriptor for it.
539 * Window manager hints and other hints are fetched to configure
540 * the window decoration attributes and others. User preferences
541 * for the window are used if available, to configure window
542 * decorations and some behaviour.
543 * If in startup, windows that are override redirect,
544 * unmapped and never were managed and are Withdrawn are not
545 * managed.
547 * Returns:
548 * the new window descriptor
550 * Side effects:
551 * The window is reparented and appropriate notification
552 * is done to the client. Input mask for the window is setup.
553 * The window descriptor is also associated with various window
554 * contexts and inserted in the head of the window list.
555 * Event handler contexts are associated for some objects
556 * (buttons, titlebar and resizebar)
558 *----------------------------------------------------------------
560 WWindow*
561 wManageWindow(WScreen *scr, Window window)
563 WWindow *wwin;
564 int x, y;
565 unsigned width, height;
566 XWindowAttributes wattribs;
567 XSetWindowAttributes attribs;
568 WWindowState *win_state;
569 WWindow *transientOwner = NULL;
570 int window_level;
571 int wm_state;
572 int foo;
573 int workspace = -1;
574 char *title;
575 Bool withdraw = False;
577 /* mutex. */
578 /* XGrabServer(dpy); */
579 XSync(dpy, False);
580 /* make sure the window is still there */
581 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
582 XUngrabServer(dpy);
583 return NULL;
586 /* if it's an override-redirect, ignore it */
587 if (wattribs.override_redirect) {
588 XUngrabServer(dpy);
589 return NULL;
592 wm_state = PropGetWindowState(window);
594 /* if it's startup and the window is unmapped, don't manage it */
595 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
596 XUngrabServer(dpy);
597 return NULL;
600 if (!wFetchName(dpy, window, &title)) {
601 title = NULL;
604 #ifdef KWM_HINTS
605 if (title && !wKWMManageableClient(scr, window, title)) {
606 XFree(title);
607 XUngrabServer(dpy);
608 return NULL;
610 #endif /* KWM_HINTS */
613 wwin = wWindowCreate();
615 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
617 #ifdef DEBUG
618 printf("managing window %x\n", (unsigned)window);
619 #endif
621 #ifdef SHAPE
622 if (wShapeSupported) {
623 int junk;
624 unsigned int ujunk;
625 int b_shaped;
627 XShapeSelectInput(dpy, window, ShapeNotifyMask);
628 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
629 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
630 wwin->flags.shaped = b_shaped;
632 #endif
635 *--------------------------------------------------
637 * Get hints and other information in properties
639 *--------------------------------------------------
641 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
643 /* setup descriptor */
644 wwin->client_win = window;
645 wwin->screen_ptr = scr;
647 wwin->old_border_width = wattribs.border_width;
649 wwin->event_mask = CLIENT_EVENTS;
650 attribs.event_mask = CLIENT_EVENTS;
651 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
652 attribs.save_under = False;
653 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
654 |CWSaveUnder, &attribs);
655 XSetWindowBorderWidth(dpy, window, 0);
657 /* get hints from GNUstep app */
658 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
659 wwin->flags.is_gnustep = 1;
661 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
662 wwin->wm_gnustep_attr = NULL;
665 wwin->client_leader = PropGetClientLeader(window);
666 if (wwin->client_leader!=None)
667 wwin->main_window = wwin->client_leader;
669 wwin->wm_hints = XGetWMHints(dpy, window);
671 if (wwin->wm_hints) {
672 if (wwin->wm_hints->flags & StateHint) {
674 if (wwin->wm_hints->initial_state == IconicState) {
676 wwin->flags.miniaturized = 1;
678 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
680 withdraw = True;
684 if (wwin->wm_hints->flags & WindowGroupHint) {
685 wwin->group_id = wwin->wm_hints->window_group;
686 /* window_group has priority over CLIENT_LEADER */
687 wwin->main_window = wwin->group_id;
688 } else {
689 wwin->group_id = None;
692 if (wwin->wm_hints->flags & UrgencyHint)
693 wwin->flags.urgent = 1;
694 } else {
695 wwin->group_id = None;
698 PropGetProtocols(window, &wwin->protocols);
700 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
701 wwin->transient_for = None;
702 } else {
703 if (wwin->transient_for==None || wwin->transient_for==window) {
704 wwin->transient_for = scr->root_win;
705 } else {
706 transientOwner = wWindowFor(wwin->transient_for);
707 if (transientOwner && transientOwner->main_window!=None) {
708 wwin->main_window = transientOwner->main_window;
709 } /*else {
710 wwin->main_window = None;
715 /* guess the focus mode */
716 wwin->focus_mode = getFocusMode(wwin);
718 /* get geometry stuff */
719 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
721 /* get colormap windows */
722 GetColormapWindows(wwin);
725 *--------------------------------------------------
727 * Setup the decoration/window attributes and
728 * geometry
730 *--------------------------------------------------
733 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
735 #ifdef OLWM_HINTS
736 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
737 && wwin->group_id != None && wwin->group_id != window) {
739 transientOwner = wWindowFor(wwin->group_id);
741 if (transientOwner) {
742 wwin->transient_for = wwin->group_id;
744 /* transients can't be iconified or maximized */
745 if (wwin->transient_for) {
746 WSETUFLAG(wwin, no_miniaturizable, 1);
747 WSETUFLAG(wwin, no_miniaturize_button, 1);
751 #endif /* OLWM_HINTS */
754 * Make broken apps behave as a nice app.
756 if (WFLAGP(wwin, emulate_appicon)) {
757 wwin->main_window = wwin->client_win;
761 *------------------------------------------------------------
763 * Setup the initial state of the window
765 *------------------------------------------------------------
768 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
769 wwin->flags.miniaturized = 1;
772 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
773 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
777 Bool bla;
779 bla = False;
780 #ifdef GNOME_STUFF
781 bla = wGNOMECheckInitialClientState(wwin);
782 #endif
783 #ifdef KWM_HINTS
784 if (!bla)
785 wKWMCheckClientInitialState(wwin);
786 #endif
789 /* apply previous state if it exists and we're in startup */
790 if (scr->flags.startup && wm_state >= 0) {
792 if (wm_state == IconicState) {
794 wwin->flags.miniaturized = 1;
796 } else if (wm_state == WithdrawnState) {
798 withdraw = True;
802 /* if there is a saved state (from file), restore it */
803 win_state = NULL;
804 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
805 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
806 } else {
807 win_state = (WWindowState*)wWindowGetSavedState(window);
809 if (win_state && !withdraw) {
811 if (win_state->state->hidden>0)
812 wwin->flags.hidden = win_state->state->hidden;
814 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
815 wwin->flags.shaded = win_state->state->shaded;
817 if (win_state->state->miniaturized>0 &&
818 !WFLAGP(wwin, no_miniaturizable)) {
819 wwin->flags.miniaturized = win_state->state->miniaturized;
821 if (!IS_OMNIPRESENT(wwin)) {
822 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
823 wwin->wm_class);
824 if (w < 0 || w >= scr->workspace_count) {
825 workspace = win_state->state->workspace;
826 if (workspace >= scr->workspace_count)
827 workspace = scr->current_workspace;
828 } else {
829 workspace = w;
831 } else {
832 workspace = scr->current_workspace;
836 /* if we're restarting, restore saved state (from hints).
837 * This will overwrite previous */
839 WSavedState *wstate;
841 if (getSavedState(window, &wstate)) {
842 wwin->flags.shaded = wstate->shaded;
843 wwin->flags.hidden = wstate->hidden;
844 wwin->flags.miniaturized = wstate->miniaturized;
845 workspace = wstate->workspace;
847 if (scr->flags.startup && wstate->window_shortcuts > 0) {
848 int i;
850 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
851 if (wstate->window_shortcuts & (1<<i)) {
852 if (!scr->shortcutWindows[i])
853 scr->shortcutWindows[i] = WMCreateBag(4);
855 WMPutInBag(scr->shortcutWindows[i], wwin);
859 free(wstate);
863 /* don't let transients start miniaturized if their owners are not */
864 if (transientOwner && !transientOwner->flags.miniaturized
865 && wwin->flags.miniaturized && !withdraw) {
866 wwin->flags.miniaturized = 0;
867 if (wwin->wm_hints)
868 wwin->wm_hints->initial_state = NormalState;
871 /* set workspace on which the window starts */
872 if (workspace >= 0) {
873 if (workspace > scr->workspace_count-1) {
874 workspace = workspace % scr->workspace_count;
876 } else {
877 int w;
879 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
881 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
883 workspace = w;
885 } else {
886 if (wPreferences.open_transients_with_parent && transientOwner) {
888 workspace = transientOwner->frame->workspace;
890 } else {
892 workspace = scr->current_workspace;
897 /* setup window geometry */
898 if (win_state && win_state->state->use_geometry) {
899 width = win_state->state->w;
900 height = win_state->state->h;
902 wWindowConstrainSize(wwin, &width, &height);
904 /* do not ask for window placement if the window is
905 * transient, during startup, if the initial workspace is another one
906 * or if the window wants to start iconic.
907 * If geometry was saved, restore it. */
909 Bool dontBring = False;
911 if (win_state && win_state->state->use_geometry) {
912 x = win_state->state->x;
913 y = win_state->state->y;
914 } else if ((wwin->transient_for==None
915 || wPreferences.window_placement!=WPM_MANUAL)
916 && !scr->flags.startup
917 && workspace == scr->current_workspace
918 && !wwin->flags.miniaturized
919 && !wwin->flags.maximized
920 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
922 if (transientOwner && transientOwner->flags.mapped) {
923 int offs = WMAX(20, 2*transientOwner->frame->top_width);
925 x = transientOwner->frame_x +
926 abs((transientOwner->frame->core->width - width)/2) + offs;
927 y = transientOwner->frame_y +
928 abs((transientOwner->frame->core->height - height)/3) + offs;
930 if (x < 0)
931 x = 0;
932 else if (x + width > scr->scr_width)
933 x = scr->scr_width - width;
935 if (y < 0)
936 y = 0;
937 else if (y + height > scr->scr_height)
938 y = scr->scr_height - height;
939 } else {
940 PlaceWindow(wwin, &x, &y, width, height);
942 if (wPreferences.window_placement == WPM_MANUAL)
943 dontBring = True;
946 if (WFLAGP(wwin, dont_move_off) && dontBring)
947 wScreenBringInside(scr, &x, &y, width, height);
950 if (wwin->flags.urgent) {
951 if (!IS_OMNIPRESENT(wwin))
952 wwin->flags.omnipresent ^= 1;
956 *--------------------------------------------------
958 * Create frame, borders and do reparenting
960 *--------------------------------------------------
962 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
963 #ifdef XKB_BUTTON_HINT
964 if (wPreferences.modelock)
965 foo |= WFF_LANGUAGE_BUTTON;
966 #endif
967 if (!WFLAGP(wwin, no_titlebar))
968 foo |= WFF_TITLEBAR;
969 if (!WFLAGP(wwin, no_resizebar))
970 foo |= WFF_RESIZEBAR;
971 if (!WFLAGP(wwin, no_border))
972 foo |= WFF_BORDER;
974 wwin->frame = wFrameWindowCreate(scr, window_level,
975 x, y, width, height,
976 &wPreferences.window_title_clearance, foo,
977 scr->window_title_texture,
978 scr->resizebar_texture,
979 scr->window_title_pixel,
980 #ifdef DRAWSTRING_PLUGIN
981 W_STRING_FTITLE,
982 #endif
983 &scr->window_title_gc,
984 &scr->title_font);
986 wwin->frame->flags.is_client_window_frame = 1;
987 wwin->frame->flags.justification = wPreferences.title_justification;
989 /* setup button images */
990 wWindowUpdateButtonImages(wwin);
992 /* hide unused buttons */
993 foo = 0;
994 if (WFLAGP(wwin, no_close_button))
995 foo |= WFF_RIGHT_BUTTON;
996 if (WFLAGP(wwin, no_miniaturize_button))
997 foo |= WFF_LEFT_BUTTON;
998 #ifdef XKB_BUTTON_HINT
999 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1000 foo |= WFF_LANGUAGE_BUTTON;
1001 #endif
1002 if (foo!=0)
1003 wFrameWindowHideButton(wwin->frame, foo);
1005 wwin->frame->child = wwin;
1007 #ifdef OLWM_HINTS
1008 /* emulate olwm push pin. Make the button look as pushed-in for
1009 * the pinned-out state. When the button is clicked, it will
1010 * revert to the normal position, which means the pin is pinned-in.
1012 if (wwin->flags.olwm_push_pin_out)
1013 wFrameWindowUpdatePushButton(wwin->frame, True);
1014 #endif /* OLWM_HINTS */
1016 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
1017 if (title)
1018 XFree(title);
1020 wwin->frame->workspace = workspace;
1022 wwin->frame->on_click_left = windowIconifyClick;
1023 #ifdef XKB_BUTTON_HINT
1024 if (wPreferences.modelock)
1025 wwin->frame->on_click_language = windowLanguageClick;
1026 #endif
1028 wwin->frame->on_click_right = windowCloseClick;
1029 wwin->frame->on_dblclick_right = windowCloseDblClick;
1031 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1032 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1034 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1037 XSelectInput(dpy, wwin->client_win,
1038 wwin->event_mask & ~StructureNotifyMask);
1040 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1041 0, wwin->frame->top_width);
1043 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1047 int gx, gy;
1049 wClientGetGravityOffsets(wwin, &gx, &gy);
1051 /* if gravity is to the south, account for the border sizes */
1052 if (gy > 0)
1053 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1057 * wWindowConfigure() will init the client window's size
1058 * (wwin->client.{width,height}) and all other geometry
1059 * related variables (frame_x,frame_y)
1061 wWindowConfigure(wwin, x, y, width, height);
1063 /* to make sure the window receives it's new position after reparenting */
1064 wWindowSynthConfigureNotify(wwin);
1067 *--------------------------------------------------
1069 * Setup descriptors and save window to internal
1070 * lists
1072 *--------------------------------------------------
1075 if (wwin->main_window!=None) {
1076 WApplication *app;
1077 WWindow *leader;
1079 /* Leader windows do not necessary set themselves as leaders.
1080 * If this is the case, point the leader of this window to
1081 * itself */
1082 leader = wWindowFor(wwin->main_window);
1083 if (leader && leader->main_window==None) {
1084 leader->main_window = leader->client_win;
1086 app = wApplicationCreate(scr, wwin->main_window);
1087 if (app) {
1088 app->last_workspace = workspace;
1091 * Do application specific stuff, like setting application
1092 * wide attributes.
1095 if (wwin->flags.hidden) {
1096 /* if the window was set to hidden because it was hidden
1097 * in a previous incarnation and that state was restored */
1098 app->flags.hidden = 1;
1101 if (app->flags.hidden) {
1102 wwin->flags.hidden = 1;
1107 /* setup the frame descriptor */
1108 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1109 wwin->frame->core->descriptor.parent = wwin;
1110 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1112 /* don't let windows go away if we die */
1113 XAddToSaveSet(dpy, window);
1115 XLowerWindow(dpy, window);
1117 /* if window is in this workspace and should be mapped, then map it */
1118 if (!wwin->flags.miniaturized
1119 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1120 && !wwin->flags.hidden && !withdraw) {
1122 /* The following "if" is to avoid crashing of clients that expect
1123 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1124 * after the return from this function.
1126 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1127 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1128 } else {
1129 wClientSetState(wwin, NormalState, None);
1132 #if 0
1133 /* if not auto focus, then map the window under the currently
1134 * focused window */
1135 #define _WIDTH(w) (w)->frame->core->width
1136 #define _HEIGHT(w) (w)->frame->core->height
1137 if (!wPreferences.auto_focus && scr->focused_window
1138 && !scr->flags.startup && !transientOwner
1139 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1140 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1141 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1142 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1143 || wwin->flags.maximized)) {
1144 MoveInStackListUnder(scr->focused_window->frame->core,
1145 wwin->frame->core);
1147 #undef _WIDTH
1148 #undef _HEIGHT
1150 #endif
1152 if (wPreferences.superfluous && !wPreferences.no_animations
1153 && !scr->flags.startup && wwin->transient_for==None
1155 * The brain damaged idiotic non-click to focus modes will
1156 * have trouble with this because:
1158 * 1. window is created and mapped by the client
1159 * 2. window is mapped by wmaker in small size
1160 * 3. window is animated to grow to normal size
1161 * 4. this function returns to normal event loop
1162 * 5. eventually, the EnterNotify event that would trigger
1163 * the window focusing (if the mouse is over that window)
1164 * will be processed by wmaker.
1165 * But since this event will be rather delayed
1166 * (step 3 has a large delay) the time when the event ocurred
1167 * and when it is processed, the client that owns that window
1168 * will reject the XSetInputFocus() for it.
1170 && (wPreferences.focus_mode==WKF_CLICK
1171 || wPreferences.auto_focus)) {
1172 DoWindowBirth(wwin);
1175 wWindowMap(wwin);
1178 /* setup stacking descriptor */
1179 if (transientOwner) {
1180 /* && wPreferences.on_top_transients */
1181 if (transientOwner) {
1182 wwin->frame->core->stacking->child_of =
1183 transientOwner->frame->core;
1185 } else {
1186 wwin->frame->core->stacking->child_of = NULL;
1190 if (!scr->focused_window) {
1191 /* first window on the list */
1192 wwin->next = NULL;
1193 wwin->prev = NULL;
1194 scr->focused_window = wwin;
1195 } else {
1196 WWindow *tmp;
1198 /* add window at beginning of focus window list */
1199 tmp = scr->focused_window;
1200 while (tmp->prev)
1201 tmp = tmp->prev;
1202 tmp->prev = wwin;
1203 wwin->next = tmp;
1204 wwin->prev = NULL;
1207 #ifdef GNOME_STUFF
1208 wGNOMEUpdateClientStateHint(wwin, True);
1209 #endif
1210 #ifdef KWM_HINTS
1211 wKWMUpdateClientWorkspace(wwin);
1212 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1213 #endif
1215 XUngrabServer(dpy);
1218 *--------------------------------------------------
1220 * Final preparations before window is ready to go
1222 *--------------------------------------------------
1225 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1228 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1229 && !wwin->flags.hidden) {
1230 if (((transientOwner && transientOwner->flags.focused)
1231 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1232 wSetFocusTo(scr, wwin);
1234 wWindowResetMouseGrabs(wwin);
1236 if (!WFLAGP(wwin, no_bind_keys)) {
1237 wWindowSetKeyGrabs(wwin);
1239 #ifdef GNOME_STUFF
1240 wGNOMEUpdateClientListHint(scr);
1241 #endif
1242 #ifdef KWM_HINTS
1243 wwin->flags.kwm_managed = 1;
1245 wKWMSendEventMessage(wwin, WKWMAddWindow);
1246 #endif
1248 wColormapInstallForWindow(scr, scr->cmap_window);
1250 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1252 #ifdef OLWM_HINTS
1253 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1254 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1256 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1257 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1258 wwin->frame_y);
1260 #endif
1263 *------------------------------------------------------------
1264 * Setup Notification Observers
1265 *------------------------------------------------------------
1267 WMAddNotificationObserver(appearanceObserver, wwin,
1268 WNWindowAppearanceSettingsChanged, wwin);
1272 *--------------------------------------------------
1274 * Cleanup temporary stuff
1276 *--------------------------------------------------
1279 if (win_state)
1280 wWindowDeleteSavedState(win_state);
1282 /* If the window must be withdrawed, then do it now.
1283 * Must do some optimization, 'though */
1284 if (withdraw) {
1285 wwin->flags.mapped = 0;
1286 wClientSetState(wwin, WithdrawnState, None);
1287 wUnmanageWindow(wwin, True, False);
1288 wwin = NULL;
1291 return wwin;
1298 WWindow*
1299 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1300 char *title, int x, int y, int width, int height)
1302 WWindow *wwin;
1303 int foo;
1305 wwin = wWindowCreate();
1307 WMAddNotificationObserver(appearanceObserver, wwin,
1308 WNWindowAppearanceSettingsChanged, wwin);
1310 wwin->flags.internal_window = 1;
1312 WSETUFLAG(wwin, omnipresent, 1);
1313 WSETUFLAG(wwin, no_shadeable, 1);
1314 WSETUFLAG(wwin, no_resizable, 1);
1315 WSETUFLAG(wwin, no_miniaturizable, 1);
1317 wwin->focus_mode = WFM_PASSIVE;
1319 wwin->client_win = window;
1320 wwin->screen_ptr = scr;
1322 wwin->transient_for = owner;
1324 wwin->client.x = x;
1325 wwin->client.y = y;
1326 wwin->client.width = width;
1327 wwin->client.height = height;
1329 wwin->frame_x = wwin->client.x;
1330 wwin->frame_y = wwin->client.y;
1333 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1334 foo |= WFF_TITLEBAR;
1335 #ifdef XKB_BUTTON_HINT
1336 foo |= WFF_LANGUAGE_BUTTON;
1337 #endif
1339 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1340 wwin->frame_x, wwin->frame_y,
1341 width, height,
1342 &wPreferences.window_title_clearance, foo,
1343 scr->window_title_texture,
1344 scr->resizebar_texture,
1345 scr->window_title_pixel,
1346 #ifdef DRAWSTRING_PLUGIN
1347 W_STRING_FTITLE,
1348 #endif
1349 &scr->window_title_gc,
1350 &scr->title_font);
1352 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1354 wwin->frame->flags.is_client_window_frame = 1;
1355 wwin->frame->flags.justification = wPreferences.title_justification;
1357 wFrameWindowChangeTitle(wwin->frame, title);
1359 /* setup button images */
1360 wWindowUpdateButtonImages(wwin);
1362 /* hide buttons */
1363 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1365 wwin->frame->child = wwin;
1367 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1369 #ifdef XKB_BUTTON_HINT
1370 if (wPreferences.modelock)
1371 wwin->frame->on_click_language = windowLanguageClick;
1372 #endif
1374 wwin->frame->on_click_right = windowCloseClick;
1376 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1377 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1379 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1381 wwin->client.y += wwin->frame->top_width;
1382 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1383 0, wwin->frame->top_width);
1385 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1386 wwin->client.width, wwin->client.height);
1388 /* setup the frame descriptor */
1389 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1390 wwin->frame->core->descriptor.parent = wwin;
1391 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1394 XLowerWindow(dpy, window);
1395 XMapSubwindows(dpy, wwin->frame->core->window);
1397 /* setup stacking descriptor */
1398 if (
1399 #ifdef removed
1400 wPreferences.on_top_transients &&
1401 #endif
1402 wwin->transient_for!=None
1403 && wwin->transient_for!=scr->root_win) {
1404 WWindow *tmp;
1405 tmp = wWindowFor(wwin->transient_for);
1406 if (tmp)
1407 wwin->frame->core->stacking->child_of = tmp->frame->core;
1408 } else {
1409 wwin->frame->core->stacking->child_of = NULL;
1413 if (!scr->focused_window) {
1414 /* first window on the list */
1415 wwin->next = NULL;
1416 wwin->prev = NULL;
1417 scr->focused_window = wwin;
1418 } else {
1419 WWindow *tmp;
1421 /* add window at beginning of focus window list */
1422 tmp = scr->focused_window;
1423 while (tmp->prev)
1424 tmp = tmp->prev;
1425 tmp->prev = wwin;
1426 wwin->next = tmp;
1427 wwin->prev = NULL;
1430 if (wwin->flags.is_gnustep == 0)
1431 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1433 /* if (wPreferences.auto_focus)*/
1434 wSetFocusTo(scr, wwin);
1436 wWindowResetMouseGrabs(wwin);
1438 wWindowSetKeyGrabs(wwin);
1440 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1442 return wwin;
1447 *----------------------------------------------------------------------
1448 * wUnmanageWindow--
1449 * Removes the frame window from a window and destroys all data
1450 * related to it. The window will be reparented back to the root window
1451 * if restore is True.
1453 * Side effects:
1454 * Everything related to the window is destroyed and the window
1455 * is removed from the window lists. Focus is set to the previous on the
1456 * window list.
1457 *----------------------------------------------------------------------
1459 void
1460 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1462 WCoreWindow *frame = wwin->frame->core;
1463 WWindow *owner = NULL;
1464 WWindow *newFocusedWindow = NULL;
1465 int wasFocused;
1466 WScreen *scr = wwin->screen_ptr;
1469 #ifdef KWM_HINTS
1470 wwin->frame->workspace = -1;
1472 wKWMUpdateClientWorkspace(wwin);
1473 #endif
1475 /* First close attribute editor window if open */
1476 if (wwin->flags.inspector_open) {
1477 wCloseInspectorForWindow(wwin);
1480 /* Close window menu if it's open for this window */
1481 if (wwin->flags.menu_open_for_me) {
1482 CloseWindowMenu(scr);
1485 if (!destroyed) {
1486 if (!wwin->flags.internal_window)
1487 XRemoveFromSaveSet(dpy, wwin->client_win);
1489 XSelectInput(dpy, wwin->client_win, NoEventMask);
1491 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1492 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1495 XUnmapWindow(dpy, frame->window);
1497 XUnmapWindow(dpy, wwin->client_win);
1499 /* deselect window */
1500 wSelectWindow(wwin, False);
1502 /* remove all pending events on window */
1503 /* I think this only matters for autoraise */
1504 if (wPreferences.raise_delay)
1505 WMDeleteTimerWithClientData(wwin->frame->core);
1507 XFlush(dpy);
1509 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1511 /* reparent the window back to the root */
1512 if (restore)
1513 wClientRestore(wwin);
1515 if (wwin->transient_for!=scr->root_win) {
1516 owner = wWindowFor(wwin->transient_for);
1517 if (owner) {
1518 if (!owner->flags.semi_focused) {
1519 owner = NULL;
1520 } else {
1521 owner->flags.semi_focused = 0;
1526 wasFocused = wwin->flags.focused;
1528 /* remove from window focus list */
1529 if (!wwin->prev && !wwin->next) {
1530 /* was the only window */
1531 scr->focused_window = NULL;
1532 newFocusedWindow = NULL;
1533 } else {
1534 WWindow *tmp;
1536 if (wwin->prev)
1537 wwin->prev->next = wwin->next;
1538 if (wwin->next)
1539 wwin->next->prev = wwin->prev;
1540 else {
1541 scr->focused_window = wwin->prev;
1542 scr->focused_window->next = NULL;
1545 if (wPreferences.focus_mode==WKF_CLICK) {
1547 /* if in click to focus mode and the window
1548 * was a transient, focus the owner window
1550 tmp = NULL;
1551 if (wPreferences.focus_mode==WKF_CLICK) {
1552 tmp = wWindowFor(wwin->transient_for);
1553 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1554 tmp = NULL;
1557 /* otherwise, focus the next one in the focus list */
1558 if (!tmp) {
1559 tmp = scr->focused_window;
1560 while (tmp) { /* look for one in the window list first */
1561 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1562 && (tmp->flags.mapped || tmp->flags.shaded))
1563 break;
1564 tmp = tmp->prev;
1566 if (!tmp) { /* if unsuccessful, choose any focusable window */
1567 tmp = scr->focused_window;
1568 while (tmp) {
1569 if (!WFLAGP(tmp, no_focusable)
1570 && (tmp->flags.mapped || tmp->flags.shaded))
1571 break;
1572 tmp = tmp->prev;
1577 newFocusedWindow = tmp;
1579 } else if (wPreferences.focus_mode==WKF_SLOPPY
1580 || wPreferences.focus_mode==WKF_POINTER) {
1581 unsigned int mask;
1582 int foo;
1583 Window bar, win;
1585 /* This is to let the root window get the keyboard input
1586 * if Sloppy focus mode and no other window get focus.
1587 * This way keybindings will not freeze.
1589 tmp = NULL;
1590 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1591 &foo, &foo, &foo, &foo, &mask))
1592 tmp = wWindowFor(win);
1593 if (tmp == wwin)
1594 tmp = NULL;
1595 newFocusedWindow = tmp;
1596 } else {
1597 newFocusedWindow = NULL;
1601 if (!wwin->flags.internal_window) {
1602 #ifdef GNOME_STUFF
1603 wGNOMERemoveClient(wwin);
1604 #endif
1605 #ifdef KWM_HINTS
1606 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1607 #endif
1610 #ifdef DEBUG
1611 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1612 (unsigned)frame->window);
1613 #endif
1615 if (wasFocused) {
1616 if (newFocusedWindow != owner && owner) {
1617 if (wwin->flags.is_gnustep == 0)
1618 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1620 wSetFocusTo(scr, newFocusedWindow);
1622 wWindowDestroy(wwin);
1623 XFlush(dpy);
1627 void
1628 wWindowMap(WWindow *wwin)
1630 XMapWindow(dpy, wwin->frame->core->window);
1631 if (!wwin->flags.shaded) {
1632 /* window will be remapped when getting MapNotify */
1633 XSelectInput(dpy, wwin->client_win,
1634 wwin->event_mask & ~StructureNotifyMask);
1635 XMapWindow(dpy, wwin->client_win);
1636 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1638 wwin->flags.mapped = 1;
1643 void
1644 wWindowUnmap(WWindow *wwin)
1646 wwin->flags.mapped = 0;
1648 /* prevent window withdrawal when getting UnmapNotify */
1649 XSelectInput(dpy, wwin->client_win,
1650 wwin->event_mask & ~StructureNotifyMask);
1651 XUnmapWindow(dpy, wwin->client_win);
1652 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1654 XUnmapWindow(dpy, wwin->frame->core->window);
1659 void
1660 wWindowFocus(WWindow *wwin, WWindow *owin)
1662 WWindow *nowner;
1663 WWindow *oowner;
1665 #ifdef KEEP_XKB_LOCK_STATUS
1666 if (wPreferences.modelock) {
1667 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1669 #endif /* KEEP_XKB_LOCK_STATUS */
1671 wwin->flags.semi_focused = 0;
1673 if (wwin->flags.is_gnustep == 0)
1674 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1676 wwin->flags.focused = 1;
1678 wWindowResetMouseGrabs(wwin);
1680 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1682 if (owin == wwin || !owin)
1683 return;
1685 nowner = wWindowFor(wwin->transient_for);
1687 /* new window is a transient for the old window */
1688 if (nowner == owin) {
1689 owin->flags.semi_focused = 1;
1690 wWindowUnfocus(nowner);
1691 return;
1694 oowner = wWindowFor(owin->transient_for);
1696 /* new window is owner of old window */
1697 if (wwin == oowner) {
1698 wWindowUnfocus(owin);
1699 return;
1702 if (!nowner) {
1703 wWindowUnfocus(owin);
1704 return;
1707 /* new window has same owner of old window */
1708 if (oowner == nowner) {
1709 /* prevent unfocusing of owner */
1710 oowner->flags.semi_focused = 0;
1711 wWindowUnfocus(owin);
1712 oowner->flags.semi_focused = 1;
1714 return;
1717 /* nowner != NULL && oowner != nowner */
1718 nowner->flags.semi_focused = 1;
1719 wWindowUnfocus(nowner);
1720 wWindowUnfocus(owin);
1724 void
1725 wWindowUnfocus(WWindow *wwin)
1727 CloseWindowMenu(wwin->screen_ptr);
1729 if (wwin->flags.is_gnustep == 0)
1730 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1731 ? WS_PFOCUSED : WS_UNFOCUSED);
1733 if (wwin->transient_for!=None
1734 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1735 WWindow *owner;
1736 owner = wWindowFor(wwin->transient_for);
1737 if (owner && owner->flags.semi_focused) {
1738 owner->flags.semi_focused = 0;
1739 if (owner->flags.mapped || owner->flags.shaded) {
1740 wWindowUnfocus(owner);
1741 wFrameWindowPaint(owner->frame);
1745 wwin->flags.focused = 0;
1747 wWindowResetMouseGrabs(wwin);
1749 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1756 *----------------------------------------------------------------------
1758 * wWindowConstrainSize--
1759 * Constrains size for the client window, taking the maximal size,
1760 * window resize increments and other size hints into account.
1762 * Returns:
1763 * The closest size to what was given that the client window can
1764 * have.
1766 *----------------------------------------------------------------------
1768 void
1769 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1771 int width = *nwidth;
1772 int height = *nheight;
1773 int winc = 1;
1774 int hinc = 1;
1775 int minW = 1, minH = 1;
1776 int maxW = wwin->screen_ptr->scr_width*2;
1777 int maxH = wwin->screen_ptr->scr_height*2;
1778 int minAX = -1, minAY = -1;
1779 int maxAX = -1, maxAY = -1;
1780 int baseW = 0;
1781 int baseH = 0;
1783 if (wwin->normal_hints) {
1784 winc = wwin->normal_hints->width_inc;
1785 hinc = wwin->normal_hints->height_inc;
1786 minW = wwin->normal_hints->min_width;
1787 minH = wwin->normal_hints->min_height;
1788 maxW = wwin->normal_hints->max_width;
1789 maxH = wwin->normal_hints->max_height;
1790 if (wwin->normal_hints->flags & PAspect) {
1791 minAX = wwin->normal_hints->min_aspect.x;
1792 minAY = wwin->normal_hints->min_aspect.y;
1793 maxAX = wwin->normal_hints->max_aspect.x;
1794 maxAY = wwin->normal_hints->max_aspect.y;
1798 if (width < minW)
1799 width = minW;
1800 if (height < minH)
1801 height = minH;
1803 if (width > maxW)
1804 width = maxW;
1805 if (height > maxH)
1806 height = maxH;
1808 /* aspect ratio code borrowed from olwm */
1809 if (minAX > 0) {
1810 /* adjust max aspect ratio */
1811 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1812 if (maxAX > maxAY) {
1813 height = (width * maxAY) / maxAX;
1814 if (height > maxH) {
1815 height = maxH;
1816 width = (height * maxAX) / maxAY;
1818 } else {
1819 width = (height * maxAX) / maxAY;
1820 if (width > maxW) {
1821 width = maxW;
1822 height = (width * maxAY) / maxAX;
1827 /* adjust min aspect ratio */
1828 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1829 if (minAX > minAY) {
1830 height = (width * minAY) / minAX;
1831 if (height < minH) {
1832 height = minH;
1833 width = (height * minAX) / minAY;
1835 } else {
1836 width = (height * minAX) / minAY;
1837 if (width < minW) {
1838 width = minW;
1839 height = (width * minAY) / minAX;
1845 if (baseW != 0) {
1846 width = (((width - baseW) / winc) * winc) + baseW;
1847 } else {
1848 width = (((width - minW) / winc) * winc) + minW;
1851 if (baseW != 0) {
1852 height = (((height - baseH) / hinc) * hinc) + baseH;
1853 } else {
1854 height = (((height - minH) / hinc) * hinc) + minH;
1857 /* broken stupid apps may cause preposterous values for these.. */
1858 if (width > 0)
1859 *nwidth = width;
1860 if (height > 0)
1861 *nheight = height;
1865 void
1866 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1868 WScreen *scr = wwin->screen_ptr;
1869 WApplication *wapp;
1870 int unmap = 0;
1872 if (workspace >= scr->workspace_count || workspace < 0
1873 || workspace == wwin->frame->workspace)
1874 return;
1876 if (workspace != scr->current_workspace) {
1877 /* Sent to other workspace. Unmap window */
1878 if ((wwin->flags.mapped
1879 || wwin->flags.shaded
1880 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1881 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1883 wapp = wApplicationOf(wwin->main_window);
1884 if (wapp) {
1885 wapp->last_workspace = workspace;
1887 if (wwin->flags.miniaturized) {
1888 if (wwin->icon) {
1889 XUnmapWindow(dpy, wwin->icon->core->window);
1890 wwin->icon->mapped = 0;
1892 } else {
1893 unmap = 1;
1894 wSetFocusTo(scr, NULL);
1897 } else {
1898 /* brought to current workspace. Map window */
1899 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1900 if (wwin->icon) {
1901 XMapWindow(dpy, wwin->icon->core->window);
1902 wwin->icon->mapped = 1;
1904 } else if (!wwin->flags.mapped &&
1905 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1906 wWindowMap(wwin);
1909 if (!IS_OMNIPRESENT(wwin)) {
1910 wwin->frame->workspace = workspace;
1911 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1913 #ifdef GNOME_STUFF
1914 wGNOMEUpdateClientStateHint(wwin, True);
1915 #endif
1916 #ifdef KWM_HINTS
1917 wKWMUpdateClientWorkspace(wwin);
1918 wKWMSendEventMessage(wwin, WKWMChangedClient);
1919 #endif
1920 if (unmap) {
1921 wWindowUnmap(wwin);
1926 void
1927 wWindowSynthConfigureNotify(WWindow *wwin)
1929 XEvent sevent;
1931 sevent.type = ConfigureNotify;
1932 sevent.xconfigure.display = dpy;
1933 sevent.xconfigure.event = wwin->client_win;
1934 sevent.xconfigure.window = wwin->client_win;
1936 sevent.xconfigure.x = wwin->client.x;
1937 sevent.xconfigure.y = wwin->client.y;
1938 sevent.xconfigure.width = wwin->client.width;
1939 sevent.xconfigure.height = wwin->client.height;
1941 sevent.xconfigure.border_width = wwin->old_border_width;
1942 if (WFLAGP(wwin, no_titlebar))
1943 sevent.xconfigure.above = None;
1944 else
1945 sevent.xconfigure.above = wwin->frame->titlebar->window;
1947 sevent.xconfigure.override_redirect = False;
1948 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1949 #ifdef KWM_HINTS
1950 wKWMSendEventMessage(wwin, WKWMChangedClient);
1951 #endif
1952 XFlush(dpy);
1957 *----------------------------------------------------------------------
1958 * wWindowConfigure--
1959 * Configures the frame, decorations and client window to the
1960 * specified geometry. The geometry is not checked for validity,
1961 * wWindowConstrainSize() must be used for that.
1962 * The size parameters are for the client window, but the position is
1963 * for the frame.
1964 * The client window receives a ConfigureNotify event, according
1965 * to what ICCCM says.
1967 * Returns:
1968 * None
1970 * Side effects:
1971 * Window size and position are changed and client window receives
1972 * a ConfigureNotify event.
1973 *----------------------------------------------------------------------
1975 void
1976 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1977 WWindow *wwin;
1978 int req_x, req_y; /* new position of the frame */
1979 int req_width, req_height; /* new size of the client */
1981 int synth_notify = False;
1982 int resize;
1984 resize = (req_width!=wwin->client.width
1985 || req_height!=wwin->client.height);
1987 * if the window is being moved but not resized then
1988 * send a synthetic ConfigureNotify
1990 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1991 synth_notify = True;
1994 if (WFLAGP(wwin, dont_move_off))
1995 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1996 req_width, req_height);
1997 if (resize) {
1998 if (req_width < MIN_WINDOW_SIZE)
1999 req_width = MIN_WINDOW_SIZE;
2000 if (req_height < MIN_WINDOW_SIZE)
2001 req_height = MIN_WINDOW_SIZE;
2003 /* If growing, resize inner part before frame,
2004 * if shrinking, resize frame before.
2005 * This will prevent the frame (that can have a different color)
2006 * to be exposed, causing flicker */
2007 if (req_height > wwin->frame->core->height
2008 || req_width > wwin->frame->core->width)
2009 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2011 if (wwin->flags.shaded) {
2012 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2013 req_width, wwin->frame->core->height);
2014 wwin->old_geometry.height = req_height;
2015 } else {
2016 int h;
2018 h = req_height + wwin->frame->top_width
2019 + wwin->frame->bottom_width;
2021 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2024 if (!(req_height > wwin->frame->core->height
2025 || req_width > wwin->frame->core->width))
2026 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2028 wwin->client.x = req_x;
2029 wwin->client.y = req_y + wwin->frame->top_width;
2030 wwin->client.width = req_width;
2031 wwin->client.height = req_height;
2032 } else {
2033 wwin->client.x = req_x;
2034 wwin->client.y = req_y + wwin->frame->top_width;
2036 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2038 wwin->frame_x = req_x;
2039 wwin->frame_y = req_y;
2040 if (!WFLAGP(wwin, no_border)) {
2041 wwin->client.x += FRAME_BORDER_WIDTH;
2042 wwin->client.y += FRAME_BORDER_WIDTH;
2045 #ifdef SHAPE
2046 if (wShapeSupported && wwin->flags.shaped && resize) {
2047 wWindowSetShape(wwin);
2049 #endif
2051 if (synth_notify)
2052 wWindowSynthConfigureNotify(wwin);
2053 XFlush(dpy);
2057 void
2058 wWindowMove(wwin, req_x, req_y)
2059 WWindow *wwin;
2060 int req_x, req_y; /* new position of the frame */
2062 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2063 int synth_notify = False;
2065 /* Send a synthetic ConfigureNotify event for every window movement. */
2066 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2067 synth_notify = True;
2069 #else
2070 /* A single synthetic ConfigureNotify event is sent at the end of
2071 * a completed (opaque) movement in moveres.c */
2072 #endif
2074 if (WFLAGP(wwin, dont_move_off))
2075 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2076 wwin->frame->core->width, wwin->frame->core->height);
2078 wwin->client.x = req_x;
2079 wwin->client.y = req_y + wwin->frame->top_width;
2080 if (!WFLAGP(wwin, no_border)) {
2081 wwin->client.x += FRAME_BORDER_WIDTH;
2082 wwin->client.y += FRAME_BORDER_WIDTH;
2085 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2087 wwin->frame_x = req_x;
2088 wwin->frame_y = req_y;
2090 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2091 if (synth_notify)
2092 wWindowSynthConfigureNotify(wwin);
2093 #endif
2097 void
2098 wWindowUpdateButtonImages(WWindow *wwin)
2100 WScreen *scr = wwin->screen_ptr;
2101 Pixmap pixmap, mask;
2102 WFrameWindow *fwin = wwin->frame;
2104 if (WFLAGP(wwin, no_titlebar))
2105 return;
2107 /* miniaturize button */
2109 if (!WFLAGP(wwin, no_miniaturize_button)) {
2110 if (wwin->wm_gnustep_attr
2111 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2112 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2114 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2115 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2116 } else {
2117 mask = None;
2120 if (fwin->lbutton_image
2121 && (fwin->lbutton_image->image != pixmap
2122 || fwin->lbutton_image->mask != mask)) {
2123 wPixmapDestroy(fwin->lbutton_image);
2124 fwin->lbutton_image = NULL;
2127 if (!fwin->lbutton_image) {
2128 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2129 fwin->lbutton_image->client_owned = 1;
2130 fwin->lbutton_image->client_owned_mask = 1;
2132 } else {
2133 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2134 wPixmapDestroy(fwin->lbutton_image);
2136 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2140 #ifdef XKB_BUTTON_HINT
2141 if (!WFLAGP(wwin, no_language_button)) {
2142 if (fwin->languagebutton_image &&
2143 !fwin->languagebutton_image->shared) {
2144 wPixmapDestroy(fwin->languagebutton_image);
2146 fwin->languagebutton_image =
2147 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2149 #endif
2151 /* close button */
2153 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2154 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2156 if (!WFLAGP(wwin, no_close_button)) {
2157 if (wwin->wm_gnustep_attr
2158 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2159 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2161 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2162 mask = wwin->wm_gnustep_attr->close_mask;
2163 else
2164 mask = None;
2166 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2167 || fwin->rbutton_image->mask != mask)) {
2168 wPixmapDestroy(fwin->rbutton_image);
2169 fwin->rbutton_image = NULL;
2172 if (!fwin->rbutton_image) {
2173 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2174 fwin->rbutton_image->client_owned = 1;
2175 fwin->rbutton_image->client_owned_mask = 1;
2178 } else if (WFLAGP(wwin, kill_close)) {
2180 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2181 wPixmapDestroy(fwin->rbutton_image);
2183 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2185 } else if (MGFLAGP(wwin, broken_close)) {
2187 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2188 wPixmapDestroy(fwin->rbutton_image);
2190 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2192 } else {
2194 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2195 wPixmapDestroy(fwin->rbutton_image);
2197 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2201 /* force buttons to be redrawn */
2202 fwin->flags.need_texture_change = 1;
2203 wFrameWindowPaint(fwin);
2208 *---------------------------------------------------------------------------
2209 * wWindowConfigureBorders--
2210 * Update window border configuration according to attribute flags.
2212 *---------------------------------------------------------------------------
2214 void
2215 wWindowConfigureBorders(WWindow *wwin)
2217 if (wwin->frame) {
2218 int flags;
2219 int newy, oldh;
2221 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2222 #ifdef XKB_BUTTON_HINT
2223 flags |= WFF_LANGUAGE_BUTTON;
2224 #endif
2225 if (!WFLAGP(wwin, no_titlebar))
2226 flags |= WFF_TITLEBAR;
2227 if (!WFLAGP(wwin, no_resizebar))
2228 flags |= WFF_RESIZEBAR;
2229 if (!WFLAGP(wwin, no_border))
2230 flags |= WFF_BORDER;
2231 if (wwin->flags.shaded)
2232 flags |= WFF_IS_SHADED;
2234 oldh = wwin->frame->top_width;
2235 wFrameWindowUpdateBorders(wwin->frame, flags);
2236 if (oldh != wwin->frame->top_width) {
2237 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2239 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2240 wWindowConfigure(wwin, wwin->frame_x, newy,
2241 wwin->client.width, wwin->client.height);
2244 flags = 0;
2245 if (!WFLAGP(wwin, no_miniaturize_button)
2246 && wwin->frame->flags.hide_left_button)
2247 flags |= WFF_LEFT_BUTTON;
2249 #ifdef XKB_BUTTON_HINT
2250 if (!WFLAGP(wwin, no_language_button)
2251 && wwin->frame->flags.hide_language_button)
2252 flags |= WFF_LANGUAGE_BUTTON;
2253 #endif
2255 if (!WFLAGP(wwin, no_close_button)
2256 && wwin->frame->flags.hide_right_button)
2257 flags |= WFF_RIGHT_BUTTON;
2259 if (flags!=0) {
2260 wWindowUpdateButtonImages(wwin);
2261 wFrameWindowShowButton(wwin->frame, flags);
2264 flags = 0;
2265 if (WFLAGP(wwin, no_miniaturize_button)
2266 && !wwin->frame->flags.hide_left_button)
2267 flags |= WFF_LEFT_BUTTON;
2269 #ifdef XKB_BUTTON_HINT
2270 if (WFLAGP(wwin, no_language_button)
2271 && !wwin->frame->flags.hide_language_button)
2272 flags |= WFF_LANGUAGE_BUTTON;
2273 #endif
2275 if (WFLAGP(wwin, no_close_button)
2276 && !wwin->frame->flags.hide_right_button)
2277 flags |= WFF_RIGHT_BUTTON;
2279 if (flags!=0)
2280 wFrameWindowHideButton(wwin->frame, flags);
2282 #ifdef SHAPE
2283 if (wShapeSupported && wwin->flags.shaped) {
2284 wWindowSetShape(wwin);
2286 #endif
2291 void
2292 wWindowSaveState(WWindow *wwin)
2294 CARD32 data[10];
2295 int i;
2297 memset(data, 0, sizeof(CARD32)*10);
2298 data[0] = wwin->frame->workspace;
2299 data[1] = wwin->flags.miniaturized;
2300 data[2] = wwin->flags.shaded;
2301 data[3] = wwin->flags.hidden;
2303 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2304 if (wwin->screen_ptr->shortcutWindows[i] &&
2305 WMCountInBag(wwin->screen_ptr->shortcutWindows[i], wwin))
2306 data[9] |= 1<<i;
2308 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2309 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2310 (unsigned char *)data, 10);
2314 static int
2315 getSavedState(Window window, WSavedState **state)
2317 Atom type_ret;
2318 int fmt_ret;
2319 unsigned long nitems_ret;
2320 unsigned long bytes_after_ret;
2321 CARD32 *data;
2323 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2324 True, _XA_WINDOWMAKER_STATE,
2325 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2326 (unsigned char **)&data)!=Success || !data)
2327 return 0;
2329 *state = malloc(sizeof(WSavedState));
2331 if (*state) {
2332 (*state)->workspace = data[0];
2333 (*state)->miniaturized = data[1];
2334 (*state)->shaded = data[2];
2335 (*state)->hidden = data[3];
2336 (*state)->use_geometry = data[4];
2337 (*state)->x = data[5];
2338 (*state)->y = data[6];
2339 (*state)->w = data[7];
2340 (*state)->h = data[8];
2341 (*state)->window_shortcuts = data[9];
2343 XFree(data);
2345 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2346 return 1;
2347 else
2348 return 0;
2352 #ifdef SHAPE
2353 void
2354 wWindowClearShape(WWindow *wwin)
2356 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2357 0, wwin->frame->top_width, None, ShapeSet);
2358 XFlush(dpy);
2361 void
2362 wWindowSetShape(WWindow *wwin)
2364 XRectangle rect[2];
2365 int count;
2366 #ifdef OPTIMIZE_SHAPE
2367 XRectangle *rects;
2368 XRectangle *urec;
2369 int ordering;
2371 /* only shape is the client's */
2372 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2373 goto alt_code;
2376 /* Get array of rectangles describing the shape mask */
2377 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2378 &count, &ordering);
2379 if (!rects) {
2380 goto alt_code;
2383 urec = malloc(sizeof(XRectangle)*(count+2));
2384 if (!urec) {
2385 XFree(rects);
2386 goto alt_code;
2389 /* insert our decoration rectangles in the rect list */
2390 memcpy(urec, rects, sizeof(XRectangle)*count);
2391 XFree(rects);
2393 if (!WFLAGP(wwin, no_titlebar)) {
2394 urec[count].x = -1;
2395 urec[count].y = -1 - wwin->frame->top_width;
2396 urec[count].width = wwin->frame->core->width + 2;
2397 urec[count].height = wwin->frame->top_width + 1;
2398 count++;
2400 if (!WFLAGP(wwin, no_resizebar)) {
2401 urec[count].x = -1;
2402 urec[count].y = wwin->frame->core->height
2403 - wwin->frame->bottom_width - wwin->frame->top_width;
2404 urec[count].width = wwin->frame->core->width + 2;
2405 urec[count].height = wwin->frame->bottom_width + 1;
2406 count++;
2409 /* shape our frame window */
2410 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2411 0, wwin->frame->top_width, urec, count,
2412 ShapeSet, Unsorted);
2413 XFlush(dpy);
2414 free(urec);
2415 return;
2417 alt_code:
2418 #endif /* OPTIMIZE_SHAPE */
2419 count = 0;
2420 if (!WFLAGP(wwin, no_titlebar)) {
2421 rect[count].x = -1;
2422 rect[count].y = -1;
2423 rect[count].width = wwin->frame->core->width + 2;
2424 rect[count].height = wwin->frame->top_width + 1;
2425 count++;
2427 if (!WFLAGP(wwin, no_resizebar)) {
2428 rect[count].x = -1;
2429 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2430 rect[count].width = wwin->frame->core->width + 2;
2431 rect[count].height = wwin->frame->bottom_width + 1;
2432 count++;
2434 if (count > 0) {
2435 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2436 0, 0, rect, count, ShapeSet, Unsorted);
2438 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2439 0, wwin->frame->top_width, wwin->client_win,
2440 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2441 XFlush(dpy);
2443 #endif /* SHAPE */
2445 /* ====================================================================== */
2447 static FocusMode
2448 getFocusMode(WWindow *wwin)
2450 FocusMode mode;
2452 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2453 if (wwin->wm_hints->input == True) {
2454 if (wwin->protocols.TAKE_FOCUS)
2455 mode = WFM_LOCALLY_ACTIVE;
2456 else
2457 mode = WFM_PASSIVE;
2458 } else {
2459 if (wwin->protocols.TAKE_FOCUS)
2460 mode = WFM_GLOBALLY_ACTIVE;
2461 else
2462 mode = WFM_NO_INPUT;
2464 } else {
2465 mode = WFM_PASSIVE;
2467 return mode;
2471 void
2472 wWindowSetKeyGrabs(WWindow *wwin)
2474 int i;
2475 WShortKey *key;
2477 for (i=0; i<WKBD_LAST; i++) {
2478 key = &wKeyBindings[i];
2480 if (key->keycode==0)
2481 continue;
2482 if (key->modifier!=AnyModifier) {
2483 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2484 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2485 #ifdef NUMLOCK_HACK
2486 /* Also grab all modifier combinations possible that include,
2487 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2488 * work even if the NumLock/ScrollLock key is on.
2490 wHackedGrabKey(key->keycode, key->modifier,
2491 wwin->frame->core->window, True, GrabModeAsync,
2492 GrabModeAsync);
2493 #endif
2495 XGrabKey(dpy, key->keycode, key->modifier,
2496 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2499 #ifndef LITE
2500 wRootMenuBindShortcuts(wwin->frame->core->window);
2501 #endif
2506 void
2507 wWindowResetMouseGrabs(WWindow *wwin)
2509 /* Mouse grabs can't be done on the client window because of
2510 * ICCCM and because clients that try to do the same will crash.
2512 * But there is a problem wich makes tbar buttons of unfocused
2513 * windows not usable as the click goes to the frame window instead
2514 * of the button itself. Must figure a way to fix that.
2517 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2519 if (!WFLAGP(wwin, no_bind_mouse)) {
2520 /* grabs for Meta+drag */
2521 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2522 True, ButtonPressMask, GrabModeSync,
2523 GrabModeAsync, None, None);
2526 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2527 && !wwin->flags.is_gnustep) {
2528 /* the passive grabs to focus the window */
2529 if (wPreferences.focus_mode == WKF_CLICK)
2530 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2531 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2532 None, None);
2534 XFlush(dpy);
2538 void
2539 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2541 if (attr->flags & GSExtraFlagsAttr) {
2542 if (MGFLAGP(wwin, broken_close) !=
2543 (attr->extra_flags & GSDocumentEditedFlag)) {
2544 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2545 wWindowUpdateButtonImages(wwin);
2551 WMagicNumber
2552 wWindowAddSavedState(char *instance, char *class, char *command,
2553 pid_t pid, WSavedState *state)
2555 WWindowState *wstate;
2557 wstate = malloc(sizeof(WWindowState));
2558 if (!wstate)
2559 return 0;
2561 memset(wstate, 0, sizeof(WWindowState));
2562 wstate->pid = pid;
2563 if (instance)
2564 wstate->instance = wstrdup(instance);
2565 if (class)
2566 wstate->class = wstrdup(class);
2567 if (command)
2568 wstate->command = wstrdup(command);
2569 wstate->state = state;
2571 wstate->next = windowState;
2572 windowState = wstate;
2574 #ifdef DEBUG
2575 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2576 class, command);
2577 #endif
2579 return wstate;
2583 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2586 WMagicNumber
2587 wWindowGetSavedState(Window win)
2589 char *instance, *class, *command=NULL;
2590 WWindowState *wstate = windowState;
2591 char **argv;
2592 int argc;
2594 if (!wstate)
2595 return NULL;
2597 if (XGetCommand(dpy, win, &argv, &argc)) {
2598 if (argc > 0)
2599 command = FlattenStringList(argv, argc);
2600 XFreeStringList(argv);
2602 if (!command)
2603 return NULL;
2605 if (PropGetWMClass(win, &class, &instance)) {
2606 while (wstate) {
2607 if (SAME(instance, wstate->instance) &&
2608 SAME(class, wstate->class) &&
2609 SAME(command, wstate->command)) {
2610 break;
2612 wstate = wstate->next;
2614 } else {
2615 wstate = NULL;
2618 #ifdef DEBUG
2619 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2620 class, command);
2621 #endif
2623 if (command) free(command);
2624 if (instance) XFree(instance);
2625 if (class) XFree(class);
2627 return wstate;
2631 void
2632 wWindowDeleteSavedState(WMagicNumber id)
2634 WWindowState *tmp, *wstate=(WWindowState*)id;
2636 if (!wstate || !windowState)
2637 return;
2639 tmp = windowState;
2640 if (tmp==wstate) {
2641 windowState = wstate->next;
2642 #ifdef DEBUG
2643 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2644 wstate, wstate->instance, wstate->class, wstate->command);
2645 #endif
2646 if (wstate->instance) free(wstate->instance);
2647 if (wstate->class) free(wstate->class);
2648 if (wstate->command) free(wstate->command);
2649 free(wstate->state);
2650 free(wstate);
2651 } else {
2652 while (tmp->next) {
2653 if (tmp->next==wstate) {
2654 tmp->next=wstate->next;
2655 #ifdef DEBUG
2656 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2657 wstate, wstate->instance, wstate->class, wstate->command);
2658 #endif
2659 if (wstate->instance) free(wstate->instance);
2660 if (wstate->class) free(wstate->class);
2661 if (wstate->command) free(wstate->command);
2662 free(wstate->state);
2663 free(wstate);
2664 break;
2666 tmp = tmp->next;
2672 void
2673 wWindowDeleteSavedStatesForPID(pid_t pid)
2675 WWindowState *tmp, *wstate;
2677 if (!windowState)
2678 return;
2680 tmp = windowState;
2681 if (tmp->pid == pid) {
2682 wstate = windowState;
2683 windowState = tmp->next;
2684 #ifdef DEBUG
2685 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2686 wstate, wstate->instance, wstate->class, wstate->command);
2687 #endif
2688 if (wstate->instance) free(wstate->instance);
2689 if (wstate->class) free(wstate->class);
2690 if (wstate->command) free(wstate->command);
2691 free(wstate->state);
2692 free(wstate);
2693 } else {
2694 while (tmp->next) {
2695 if (tmp->next->pid==pid) {
2696 wstate = tmp->next;
2697 tmp->next = wstate->next;
2698 #ifdef DEBUG
2699 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2700 wstate, wstate->instance, wstate->class, wstate->command);
2701 #endif
2702 if (wstate->instance) free(wstate->instance);
2703 if (wstate->class) free(wstate->class);
2704 if (wstate->command) free(wstate->command);
2705 free(wstate->state);
2706 free(wstate);
2707 break;
2709 tmp = tmp->next;
2715 /* ====================================================================== */
2717 static void
2718 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2720 WWindow *wwin = data;
2722 #ifndef NUMLOCK_HACK
2723 if ((event->xbutton.state & ValidModMask)
2724 != (event->xbutton.state & ~LockMask)) {
2725 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2726 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2728 #endif
2730 event->xbutton.state &= ValidModMask;
2732 CloseWindowMenu(wwin->screen_ptr);
2734 if (wPreferences.focus_mode==WKF_CLICK
2735 && !(event->xbutton.state&ControlMask)
2736 && !WFLAGP(wwin, no_focusable)) {
2737 wSetFocusTo(wwin->screen_ptr, wwin);
2740 if (event->xbutton.button == Button1)
2741 wRaiseFrame(wwin->frame->core);
2743 if (event->xbutton.window != wwin->frame->resizebar->window) {
2744 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2745 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2746 GrabModeAsync, GrabModeAsync, None,
2747 None, CurrentTime)!=GrabSuccess) {
2748 #ifdef DEBUG0
2749 wwarning("pointer grab failed for window move");
2750 #endif
2751 return;
2755 if (event->xbutton.state & MOD_MASK) {
2756 /* move the window */
2757 wMouseMoveWindow(wwin, event);
2758 XUngrabPointer(dpy, CurrentTime);
2759 } else {
2760 wMouseResizeWindow(wwin, event);
2761 XUngrabPointer(dpy, CurrentTime);
2767 static void
2768 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2770 WWindow *wwin = data;
2772 event->xbutton.state &= ValidModMask;
2774 if (event->xbutton.button==Button1) {
2775 if (event->xbutton.state == 0) {
2776 if (!WFLAGP(wwin, no_shadeable)) {
2777 /* shade window */
2778 if (wwin->flags.shaded)
2779 wUnshadeWindow(wwin);
2780 else
2781 wShadeWindow(wwin);
2783 } else {
2784 int dir = 0;
2786 if (event->xbutton.state & ControlMask)
2787 dir |= MAX_VERTICAL;
2789 if (event->xbutton.state & ShiftMask) {
2790 dir |= MAX_HORIZONTAL;
2791 if (!(event->xbutton.state & ControlMask))
2792 wSelectWindow(wwin, !wwin->flags.selected);
2795 /* maximize window */
2796 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2797 if (wwin->flags.maximized)
2798 wUnmaximizeWindow(wwin);
2799 else
2800 wMaximizeWindow(wwin, dir);
2803 } else if (event->xbutton.button==Button3) {
2804 if (event->xbutton.state & MOD_MASK) {
2805 wHideOtherApplications(wwin);
2807 } else if (event->xbutton.button==Button2) {
2808 wSelectWindow(wwin, !wwin->flags.selected);
2813 static void
2814 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2816 WWindow *wwin = desc->parent;
2818 event->xbutton.state &= ValidModMask;
2820 CloseWindowMenu(wwin->screen_ptr);
2822 if (wPreferences.focus_mode==WKF_CLICK
2823 && !(event->xbutton.state&ControlMask)
2824 && !WFLAGP(wwin, no_focusable)) {
2825 wSetFocusTo(wwin->screen_ptr, wwin);
2827 if (event->xbutton.button == Button1) {
2828 wRaiseFrame(wwin->frame->core);
2831 if (event->xbutton.state & MOD_MASK) {
2832 /* move the window */
2833 if (XGrabPointer(dpy, wwin->client_win, False,
2834 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2835 GrabModeAsync, GrabModeAsync, None,
2836 None, CurrentTime)!=GrabSuccess) {
2837 #ifdef DEBUG0
2838 wwarning("pointer grab failed for window move");
2839 #endif
2840 return;
2842 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2843 wMouseResizeWindow(wwin, event);
2844 else
2845 wMouseMoveWindow(wwin, event);
2846 XUngrabPointer(dpy, CurrentTime);
2851 static void
2852 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2854 WWindow *wwin = (WWindow*)data;
2856 #ifndef NUMLOCK_HACK
2857 if ((event->xbutton.state & ValidModMask)
2858 != (event->xbutton.state & ~LockMask)) {
2859 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2860 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2862 #endif
2863 event->xbutton.state &= ValidModMask;
2865 CloseWindowMenu(wwin->screen_ptr);
2867 if (wPreferences.focus_mode==WKF_CLICK
2868 && !(event->xbutton.state&ControlMask)
2869 && !WFLAGP(wwin, no_focusable)) {
2870 wSetFocusTo(wwin->screen_ptr, wwin);
2873 if (event->xbutton.button == Button1
2874 || event->xbutton.button == Button2) {
2876 if (event->xbutton.button == Button1) {
2877 if (event->xbutton.state & MOD_MASK) {
2878 wLowerFrame(wwin->frame->core);
2879 } else {
2880 wRaiseFrame(wwin->frame->core);
2883 if ((event->xbutton.state & ShiftMask)
2884 && !(event->xbutton.state & ControlMask)) {
2885 wSelectWindow(wwin, !wwin->flags.selected);
2886 return;
2888 if (event->xbutton.window != wwin->frame->titlebar->window
2889 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2890 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2891 GrabModeAsync, GrabModeAsync, None,
2892 None, CurrentTime)!=GrabSuccess) {
2893 #ifdef DEBUG0
2894 wwarning("pointer grab failed for window move");
2895 #endif
2896 return;
2899 /* move the window */
2900 wMouseMoveWindow(wwin, event);
2902 XUngrabPointer(dpy, CurrentTime);
2903 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2904 && !wwin->flags.internal_window
2905 && !WCHECK_STATE(WSTATE_MODAL)) {
2906 WObjDescriptor *desc;
2908 if (event->xbutton.window != wwin->frame->titlebar->window
2909 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2910 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2911 GrabModeAsync, GrabModeAsync, None,
2912 None, CurrentTime)!=GrabSuccess) {
2913 #ifdef DEBUG0
2914 wwarning("pointer grab failed for window move");
2915 #endif
2916 return;
2919 OpenWindowMenu(wwin, event->xbutton.x_root,
2920 wwin->frame_y+wwin->frame->top_width, False);
2922 /* allow drag select */
2923 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2924 event->xany.send_event = True;
2925 (*desc->handle_mousedown)(desc, event);
2927 XUngrabPointer(dpy, CurrentTime);
2933 static void
2934 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2936 WWindow *wwin = data;
2938 event->xbutton.state &= ValidModMask;
2940 CloseWindowMenu(wwin->screen_ptr);
2942 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2943 return;
2945 /* if control-click, kill the client */
2946 if (event->xbutton.state & ControlMask) {
2947 wClientKill(wwin);
2948 } else {
2949 #ifdef OLWM_HINTS
2950 if (wwin->flags.olwm_push_pin_out) {
2952 wwin->flags.olwm_push_pin_out = 0;
2954 wOLWMChangePushpinState(wwin, True);
2956 wFrameWindowUpdatePushButton(wwin->frame, False);
2958 return;
2960 #endif
2961 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2962 /* send delete message */
2963 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2969 static void
2970 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2972 WWindow *wwin = data;
2974 CloseWindowMenu(wwin->screen_ptr);
2976 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2977 return;
2979 /* send delete message */
2980 if (wwin->protocols.DELETE_WINDOW) {
2981 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2982 } else {
2983 wClientKill(wwin);
2988 #ifdef XKB_BUTTON_HINT
2989 static void
2990 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2992 WWindow *wwin = data;
2993 WFrameWindow *fwin = wwin->frame;
2994 WScreen *scr = fwin->screen_ptr;
2995 XkbStateRec staterec;
2996 int tl;
2998 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2999 return;
3000 tl = wwin->frame->languagemode;
3001 wwin->frame->languagemode = wwin->frame->last_languagemode;
3002 wwin->frame->last_languagemode = tl;
3003 wSetFocusTo(scr, wwin);
3004 wwin->frame->languagebutton_image =
3005 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3006 wwin->frame->languagemode];
3007 wFrameWindowUpdateLanguageButton(wwin->frame);
3008 if (event->xbutton.button == Button3)
3009 return;
3010 wRaiseFrame(fwin->core);
3012 #endif
3015 static void
3016 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3018 WWindow *wwin = data;
3020 event->xbutton.state &= ValidModMask;
3022 CloseWindowMenu(wwin->screen_ptr);
3024 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3025 return;
3027 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3028 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3029 LastTimestamp);
3030 } else {
3031 WApplication *wapp;
3032 if ((event->xbutton.state & ControlMask) ||
3033 (event->xbutton.button == Button3)) {
3035 wapp = wApplicationOf(wwin->main_window);
3036 if (wapp && !WFLAGP(wwin, no_appicon))
3037 wHideApplication(wapp);
3038 } else if (event->xbutton.state==0) {
3039 wIconifyWindow(wwin);