- Fixed the PPosition/PSize code (I hope) --Dan
[wmaker-crm.git] / src / window.c
blobd0b08539677da62098fb2225368f58d91f95466c
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);
150 if (flags & WTextureSettings) {
151 wwin->frame->flags.need_texture_remake = 1;
153 if (flags & (WTextureSettings | WColorSettings)) {
154 if (wwin->frame->titlebar)
155 XClearWindow(dpy, wwin->frame->titlebar->window);
157 wFrameWindowPaint(wwin->frame);
161 /************************************/
163 WWindow*
164 wWindowFor(Window window)
166 WObjDescriptor *desc;
168 if (window==None)
169 return NULL;
171 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
172 return NULL;
174 if (desc->parent_type==WCLASS_WINDOW)
175 return desc->parent;
176 else if (desc->parent_type==WCLASS_FRAME) {
177 WFrameWindow *frame = (WFrameWindow*)desc->parent;
178 if (frame->flags.is_client_window_frame)
179 return frame->child;
182 return NULL;
186 WWindow*
187 wWindowCreate()
189 WWindow *wwin;
191 wwin = wmalloc(sizeof(WWindow));
192 wretain(wwin);
194 memset(wwin, 0, sizeof(WWindow));
196 wwin->client_descriptor.handle_mousedown = frameMouseDown;
197 wwin->client_descriptor.parent = wwin;
198 wwin->client_descriptor.self = wwin;
199 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
201 return wwin;
205 void
206 wWindowDestroy(WWindow *wwin)
208 int i;
210 WMRemoveNotificationObserver(wwin);
212 wwin->flags.destroyed = 1;
214 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
215 if (wwin->screen_ptr->shortcutSelectedWindows[i]) {
216 WMRemoveFromBag(wwin->screen_ptr->shortcutSelectedWindows[i], wwin);
218 if (!WMGetBagItemCount(wwin->screen_ptr->shortcutSelectedWindows[i])) {
219 WMFreeBag(wwin->screen_ptr->shortcutSelectedWindows[i]);
220 wwin->screen_ptr->shortcutSelectedWindows[i] = NULL;
225 if (wwin->normal_hints)
226 free(wwin->normal_hints);
228 if (wwin->wm_hints)
229 XFree(wwin->wm_hints);
231 if (wwin->wm_instance)
232 XFree(wwin->wm_instance);
234 if (wwin->wm_class)
235 XFree(wwin->wm_class);
237 if (wwin->wm_gnustep_attr)
238 free(wwin->wm_gnustep_attr);
240 if (wwin->cmap_windows)
241 XFree(wwin->cmap_windows);
243 XDeleteContext(dpy, wwin->client_win, wWinContext);
245 if (wwin->frame)
246 wFrameWindowDestroy(wwin->frame);
248 if (wwin->icon) {
249 RemoveFromStackList(wwin->icon->core);
250 wIconDestroy(wwin->icon);
251 if (wPreferences.auto_arrange_icons)
252 wArrangeIcons(wwin->screen_ptr, True);
254 wrelease(wwin);
260 static void
261 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
263 if (gs_hints->flags & GSWindowStyleAttr) {
264 wwin->client_flags.no_titlebar =
265 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
267 wwin->client_flags.no_close_button =
268 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
270 wwin->client_flags.no_closable =
271 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
273 wwin->client_flags.no_miniaturize_button =
274 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
276 wwin->client_flags.no_miniaturizable =
277 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
279 wwin->client_flags.no_resizebar =
280 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
282 wwin->client_flags.no_resizable =
283 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
284 } else {
285 /* setup the defaults */
286 wwin->client_flags.no_titlebar = 0;
287 wwin->client_flags.no_closable = 0;
288 wwin->client_flags.no_miniaturizable = 0;
289 wwin->client_flags.no_resizable = 0;
290 wwin->client_flags.no_close_button = 0;
291 wwin->client_flags.no_miniaturize_button = 0;
292 wwin->client_flags.no_resizebar = 0;
294 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
295 wwin->client_flags.no_appicon = 1;
300 void
301 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
302 WWindowAttributes *mask)
304 if (wflags->no_appicon && mask->no_appicon)
305 wflags->emulate_appicon = 0;
307 if (wwin->main_window!=None) {
308 WApplication *wapp = wApplicationOf(wwin->main_window);
309 if (wapp && !wapp->flags.emulated)
310 wflags->emulate_appicon = 0;
313 if (wwin->transient_for!=None
314 && wwin->transient_for!=wwin->screen_ptr->root_win)
315 wflags->emulate_appicon = 0;
317 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
318 wflags->sunken = 0;
323 void
324 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
326 WScreen *scr = wwin->screen_ptr;
328 /* sets global default stuff */
329 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
330 &wwin->client_flags, NULL, True);
332 * Decoration setting is done in this precedence (lower to higher)
333 * - use global default in the resource database
334 * - guess some settings
335 * - use GNUstep/external window attributes
336 * - set hints specified for the app in the resource DB
339 WSETUFLAG(wwin, broken_close, 0);
341 if (wwin->protocols.DELETE_WINDOW)
342 WSETUFLAG(wwin, kill_close, 0);
343 else
344 WSETUFLAG(wwin, kill_close, 1);
346 /* transients can't be iconified or maximized */
347 if (wwin->transient_for) {
348 WSETUFLAG(wwin, no_miniaturizable, 1);
349 WSETUFLAG(wwin, no_miniaturize_button, 1);
352 /* if the window can't be resized, remove the resizebar */
353 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
354 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
355 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
356 WSETUFLAG(wwin, no_resizable, 1);
357 WSETUFLAG(wwin, no_resizebar, 1);
360 /* set GNUstep window attributes */
361 if (wwin->wm_gnustep_attr) {
362 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
364 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
366 switch (wwin->wm_gnustep_attr->window_level) {
367 case WMNormalWindowLevel:
368 *level = WMNormalLevel;
369 break;
370 case WMFloatingWindowLevel:
371 *level = WMFloatingLevel;
372 break;
373 case WMDockWindowLevel:
374 *level = WMDockLevel;
375 break;
376 case WMSubmenuWindowLevel:
377 *level = WMSubmenuLevel;
378 break;
379 case WMMainMenuWindowLevel:
380 *level = WMMainMenuLevel;
381 break;
382 default:
383 *level = WMNormalLevel;
384 break;
386 } else {
387 /* setup defaults */
388 *level = WMNormalLevel;
390 } else {
391 int tmp_workspace = -1;
392 int tmp_level = -1;
394 #ifdef MWM_HINTS
395 wMWMCheckClientHints(wwin);
396 #endif /* MWM_HINTS */
398 #ifdef KWM_HINTS
399 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
400 #endif /* KWM_HINTS */
402 #ifdef GNOME_STUFF
403 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
404 #endif /* GNOME_STUFF */
406 #ifdef OLWM_HINTS
407 wOLWMCheckClientHints(wwin);
408 #endif /* OLWM_HINTS */
410 if (tmp_level < 0) {
411 if (WFLAGP(wwin, floating))
412 *level = WMFloatingLevel;
413 else if (WFLAGP(wwin, sunken))
414 *level = WMSunkenLevel;
415 else
416 *level = WMNormalLevel;
417 } else {
418 *level = tmp_level;
421 if (tmp_workspace >= 0) {
422 *workspace = tmp_workspace % scr->workspace_count;
427 * Set attributes specified only for that window/class.
428 * This might do duplicate work with the 1st wDefaultFillAttributes().
430 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
431 &wwin->user_flags, &wwin->defined_user_flags,
432 False);
434 * Sanity checks for attributes that depend on other attributes
436 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
437 wwin->user_flags.emulate_appicon = 0;
439 if (wwin->main_window!=None) {
440 WApplication *wapp = wApplicationOf(wwin->main_window);
441 if (wapp && !wapp->flags.emulated)
442 wwin->user_flags.emulate_appicon = 0;
445 if (wwin->transient_for!=None
446 && wwin->transient_for!=wwin->screen_ptr->root_win)
447 wwin->user_flags.emulate_appicon = 0;
449 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
450 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
451 wwin->user_flags.sunken = 0;
453 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
459 Bool
460 wWindowCanReceiveFocus(WWindow *wwin)
462 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
463 return False;
464 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
465 return False;
466 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
467 return False;
469 return True;
473 Bool
474 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
476 int w1, h1, w2, h2;
478 w1 = wwin->frame->core->width;
479 h1 = wwin->frame->core->height;
480 w2 = obscured->frame->core->width;
481 h2 = obscured->frame->core->height;
483 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
484 && wwin->frame->workspace != obscured->frame->workspace)
485 return False;
487 if (wwin->frame_x + w1 < obscured->frame_x
488 || wwin->frame_y + h1 < obscured->frame_y
489 || wwin->frame_x > obscured->frame_x + w2
490 || wwin->frame_y > obscured->frame_y + h2) {
491 return False;
494 return True;
499 *----------------------------------------------------------------
500 * wManageWindow--
501 * reparents the window and allocates a descriptor for it.
502 * Window manager hints and other hints are fetched to configure
503 * the window decoration attributes and others. User preferences
504 * for the window are used if available, to configure window
505 * decorations and some behaviour.
506 * If in startup, windows that are override redirect,
507 * unmapped and never were managed and are Withdrawn are not
508 * managed.
510 * Returns:
511 * the new window descriptor
513 * Side effects:
514 * The window is reparented and appropriate notification
515 * is done to the client. Input mask for the window is setup.
516 * The window descriptor is also associated with various window
517 * contexts and inserted in the head of the window list.
518 * Event handler contexts are associated for some objects
519 * (buttons, titlebar and resizebar)
521 *----------------------------------------------------------------
523 WWindow*
524 wManageWindow(WScreen *scr, Window window)
526 WWindow *wwin;
527 int x, y;
528 unsigned width, height;
529 XWindowAttributes wattribs;
530 XSetWindowAttributes attribs;
531 WWindowState *win_state;
532 WWindow *transientOwner = NULL;
533 int window_level;
534 int wm_state;
535 int foo;
536 int workspace = -1;
537 char *title;
538 Bool withdraw = False;
540 /* mutex. */
541 /* XGrabServer(dpy); */
542 XSync(dpy, False);
543 /* make sure the window is still there */
544 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
545 XUngrabServer(dpy);
546 return NULL;
549 /* if it's an override-redirect, ignore it */
550 if (wattribs.override_redirect) {
551 XUngrabServer(dpy);
552 return NULL;
555 wm_state = PropGetWindowState(window);
557 /* if it's startup and the window is unmapped, don't manage it */
558 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
559 XUngrabServer(dpy);
560 return NULL;
563 if (!wFetchName(dpy, window, &title)) {
564 title = NULL;
567 #ifdef KWM_HINTS
568 if (title && !wKWMManageableClient(scr, window, title)) {
569 XFree(title);
570 XUngrabServer(dpy);
571 return NULL;
573 #endif /* KWM_HINTS */
576 wwin = wWindowCreate();
578 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
580 #ifdef DEBUG
581 printf("managing window %x\n", (unsigned)window);
582 #endif
584 #ifdef SHAPE
585 if (wShapeSupported) {
586 int junk;
587 unsigned int ujunk;
588 int b_shaped;
590 XShapeSelectInput(dpy, window, ShapeNotifyMask);
591 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
592 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
593 wwin->flags.shaped = b_shaped;
595 #endif
598 *--------------------------------------------------
600 * Get hints and other information in properties
602 *--------------------------------------------------
604 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
606 /* setup descriptor */
607 wwin->client_win = window;
608 wwin->screen_ptr = scr;
610 wwin->old_border_width = wattribs.border_width;
612 wwin->event_mask = CLIENT_EVENTS;
613 attribs.event_mask = CLIENT_EVENTS;
614 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
615 attribs.save_under = False;
616 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
617 |CWSaveUnder, &attribs);
618 XSetWindowBorderWidth(dpy, window, 0);
620 /* get hints from GNUstep app */
621 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
622 wwin->wm_gnustep_attr = NULL;
625 wwin->client_leader = PropGetClientLeader(window);
626 if (wwin->client_leader!=None)
627 wwin->main_window = wwin->client_leader;
629 wwin->wm_hints = XGetWMHints(dpy, window);
631 if (wwin->wm_hints) {
632 if (wwin->wm_hints->flags & StateHint) {
634 if (wwin->wm_hints->initial_state == IconicState) {
636 wwin->flags.miniaturized = 1;
638 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
640 withdraw = True;
644 if (wwin->wm_hints->flags & WindowGroupHint) {
645 wwin->group_id = wwin->wm_hints->window_group;
646 /* window_group has priority over CLIENT_LEADER */
647 wwin->main_window = wwin->group_id;
648 } else {
649 wwin->group_id = None;
652 if (wwin->wm_hints->flags & UrgencyHint)
653 wwin->flags.urgent = 1;
654 } else {
655 wwin->group_id = None;
658 PropGetProtocols(window, &wwin->protocols);
660 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
661 wwin->transient_for = None;
662 } else {
663 if (wwin->transient_for==None || wwin->transient_for==window) {
664 wwin->transient_for = scr->root_win;
665 } else {
666 transientOwner = wWindowFor(wwin->transient_for);
667 if (transientOwner && transientOwner->main_window!=None) {
668 wwin->main_window = transientOwner->main_window;
669 } /*else {
670 wwin->main_window = None;
675 /* guess the focus mode */
676 wwin->focus_mode = getFocusMode(wwin);
678 /* get geometry stuff */
679 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
681 /* get colormap windows */
682 GetColormapWindows(wwin);
685 *--------------------------------------------------
687 * Setup the decoration/window attributes and
688 * geometry
690 *--------------------------------------------------
693 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
695 #ifdef OLWM_HINTS
696 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
697 && wwin->group_id != None && wwin->group_id != window) {
699 transientOwner = wWindowFor(wwin->group_id);
701 if (transientOwner) {
702 wwin->transient_for = wwin->group_id;
704 /* transients can't be iconified or maximized */
705 if (wwin->transient_for) {
706 WSETUFLAG(wwin, no_miniaturizable, 1);
707 WSETUFLAG(wwin, no_miniaturize_button, 1);
711 #endif /* OLWM_HINTS */
714 * Make broken apps behave as a nice app.
716 if (WFLAGP(wwin, emulate_appicon)) {
717 wwin->main_window = wwin->client_win;
721 *------------------------------------------------------------
723 * Setup the initial state of the window
725 *------------------------------------------------------------
728 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
729 wwin->flags.miniaturized = 1;
732 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
733 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
736 #ifdef GNOME_STUFF
737 wGNOMECheckInitialClientState(wwin);
738 #endif
739 #ifdef KWM_HINTS
740 wKWMCheckClientInitialState(wwin);
741 #endif
743 /* apply previous state if it exists and we're in startup */
744 if (scr->flags.startup && wm_state >= 0) {
746 if (wm_state == IconicState) {
748 wwin->flags.miniaturized = 1;
750 } else if (wm_state == WithdrawnState) {
752 withdraw = True;
756 /* if there is a saved state (from file), restore it */
757 win_state = NULL;
758 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
759 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
760 } else {
761 win_state = (WWindowState*)wWindowGetSavedState(window);
763 if (win_state && !withdraw) {
765 if (win_state->state->hidden>0)
766 wwin->flags.hidden = win_state->state->hidden;
768 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
769 wwin->flags.shaded = win_state->state->shaded;
771 if (win_state->state->miniaturized>0 &&
772 !WFLAGP(wwin, no_miniaturizable)) {
773 wwin->flags.miniaturized = win_state->state->miniaturized;
775 if (!IS_OMNIPRESENT(wwin)) {
776 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
777 wwin->wm_class);
778 if (w < 0 || w >= scr->workspace_count) {
779 workspace = win_state->state->workspace;
780 if (workspace >= scr->workspace_count)
781 workspace = scr->current_workspace;
782 } else {
783 workspace = w;
785 } else {
786 workspace = scr->current_workspace;
790 /* if we're restarting, restore saved state (from hints).
791 * This will overwrite previous */
793 WSavedState *wstate;
795 if (getSavedState(window, &wstate)) {
796 wwin->flags.shaded = wstate->shaded;
797 wwin->flags.hidden = wstate->hidden;
798 wwin->flags.miniaturized = wstate->miniaturized;
799 workspace = wstate->workspace;
801 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
802 int i;
804 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
805 if (wstate->window_shortcuts & (1<<i))
806 scr->shortcutWindow[i] = wwin;
809 free(wstate);
813 /* don't let transients start miniaturized if their owners are not */
814 if (transientOwner && !transientOwner->flags.miniaturized
815 && wwin->flags.miniaturized && !withdraw) {
816 wwin->flags.miniaturized = 0;
817 if (wwin->wm_hints)
818 wwin->wm_hints->initial_state = NormalState;
821 /* set workspace on which the window starts */
822 if (workspace >= 0) {
823 if (workspace > scr->workspace_count-1) {
824 workspace = workspace % scr->workspace_count;
826 } else {
827 int w;
829 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
831 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
833 workspace = w;
835 } else {
836 if (wPreferences.open_transients_with_parent && transientOwner) {
838 workspace = transientOwner->frame->workspace;
840 } else {
842 workspace = scr->current_workspace;
847 /* setup window geometry */
848 if (win_state && win_state->state->use_geometry) {
849 width = win_state->state->w;
850 height = win_state->state->h;
852 wWindowConstrainSize(wwin, &width, &height);
854 /* do not ask for window placement if the window is
855 * transient, during startup, if the initial workspace is another one
856 * or if the window wants to start iconic.
857 * If geometry was saved, restore it. */
859 Bool dontBring = False;
861 if (win_state && win_state->state->use_geometry) {
862 x = win_state->state->x;
863 y = win_state->state->y;
864 } else if ((wwin->transient_for==None
865 || wPreferences.window_placement!=WPM_MANUAL)
866 && !scr->flags.startup
867 && workspace == scr->current_workspace
868 && !wwin->flags.miniaturized
869 && !wwin->flags.maximized
870 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
871 PlaceWindow(wwin, &x, &y, width, height);
872 if (wPreferences.window_placement == WPM_MANUAL)
873 dontBring = True;
876 if (WFLAGP(wwin, dont_move_off) && dontBring)
877 wScreenBringInside(scr, &x, &y, width, height);
880 if (wwin->flags.urgent) {
881 if (!IS_OMNIPRESENT(wwin))
882 wwin->flags.omnipresent ^= 1;
886 *--------------------------------------------------
888 * Create frame, borders and do reparenting
890 *--------------------------------------------------
892 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
893 #ifdef XKB_BUTTON_HINT
894 if (wPreferences.modelock)
895 foo |= WFF_LANGUAGE_BUTTON;
896 #endif
897 if (!WFLAGP(wwin, no_titlebar))
898 foo |= WFF_TITLEBAR;
899 if (!WFLAGP(wwin, no_resizebar))
900 foo |= WFF_RESIZEBAR;
902 wwin->frame = wFrameWindowCreate(scr, window_level,
903 x, y, width, height, foo,
904 scr->window_title_texture,
905 scr->resizebar_texture,
906 scr->window_title_pixel,
907 #ifdef DRAWSTRING_PLUGIN
908 W_STRING_FTITLE,
909 #endif
910 &scr->window_title_gc,
911 &scr->title_font);
913 wwin->frame->flags.is_client_window_frame = 1;
914 wwin->frame->flags.justification = wPreferences.title_justification;
916 /* setup button images */
917 wWindowUpdateButtonImages(wwin);
919 /* hide unused buttons */
920 foo = 0;
921 if (WFLAGP(wwin, no_close_button))
922 foo |= WFF_RIGHT_BUTTON;
923 if (WFLAGP(wwin, no_miniaturize_button))
924 foo |= WFF_LEFT_BUTTON;
925 #ifdef XKB_BUTTON_HINT
926 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
927 foo |= WFF_LANGUAGE_BUTTON;
928 #endif
929 if (foo!=0)
930 wFrameWindowHideButton(wwin->frame, foo);
932 wwin->frame->child = wwin;
934 #ifdef OLWM_HINTS
935 /* emulate olwm push pin. Make the button look as pushed-in for
936 * the pinned-out state. When the button is clicked, it will
937 * revert to the normal position, which means the pin is pinned-in.
939 if (wwin->flags.olwm_push_pin_out)
940 wFrameWindowUpdatePushButton(wwin->frame, True);
941 #endif /* OLWM_HINTS */
943 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
944 if (title)
945 XFree(title);
947 wwin->frame->workspace = workspace;
949 wwin->frame->on_click_left = windowIconifyClick;
950 #ifdef XKB_BUTTON_HINT
951 if (wPreferences.modelock)
952 wwin->frame->on_click_language = windowLanguageClick;
953 #endif
955 wwin->frame->on_click_right = windowCloseClick;
956 wwin->frame->on_dblclick_right = windowCloseDblClick;
958 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
959 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
961 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
964 XSelectInput(dpy, wwin->client_win,
965 wwin->event_mask & ~StructureNotifyMask);
967 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
968 0, wwin->frame->top_width);
970 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
974 int gx, gy;
976 wClientGetGravityOffsets(wwin, &gx, &gy);
978 /* if gravity is to the south, account for the border sizes */
979 if (gy > 0)
980 y -= wwin->frame->top_width + wwin->frame->bottom_width;
984 * wWindowConfigure() will init the client window's size
985 * (wwin->client.{width,height}) and all other geometry
986 * related variables (frame_x,frame_y)
988 wWindowConfigure(wwin, x, y, width, height);
990 /* to make sure the window receives it's new position after reparenting */
991 wWindowSynthConfigureNotify(wwin);
994 *--------------------------------------------------
996 * Setup descriptors and save window to internal
997 * lists
999 *--------------------------------------------------
1002 if (wwin->main_window!=None) {
1003 WApplication *app;
1004 WWindow *leader;
1006 /* Leader windows do not necessary set themselves as leaders.
1007 * If this is the case, point the leader of this window to
1008 * itself */
1009 leader = wWindowFor(wwin->main_window);
1010 if (leader && leader->main_window==None) {
1011 leader->main_window = leader->client_win;
1013 app = wApplicationCreate(scr, wwin->main_window);
1014 if (app) {
1015 app->last_workspace = workspace;
1018 * Do application specific stuff, like setting application
1019 * wide attributes.
1022 if (wwin->flags.hidden) {
1023 /* if the window was set to hidden because it was hidden
1024 * in a previous incarnation and that state was restored */
1025 app->flags.hidden = 1;
1028 if (app->flags.hidden) {
1029 wwin->flags.hidden = 1;
1034 /* setup the frame descriptor */
1035 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1036 wwin->frame->core->descriptor.parent = wwin;
1037 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1039 /* don't let windows go away if we die */
1040 XAddToSaveSet(dpy, window);
1042 XLowerWindow(dpy, window);
1044 /* if window is in this workspace and should be mapped, then map it */
1045 if (!wwin->flags.miniaturized
1046 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1047 && !wwin->flags.hidden && !withdraw) {
1049 /* The following "if" is to avoid crashing of clients that expect
1050 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1051 * after the return from this function.
1053 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1054 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1055 } else {
1056 wClientSetState(wwin, NormalState, None);
1059 #if 0
1060 /* if not auto focus, then map the window under the currently
1061 * focused window */
1062 #define _WIDTH(w) (w)->frame->core->width
1063 #define _HEIGHT(w) (w)->frame->core->height
1064 if (!wPreferences.auto_focus && scr->focused_window
1065 && !scr->flags.startup && !transientOwner
1066 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1067 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1068 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1069 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1070 || wwin->flags.maximized)) {
1071 MoveInStackListUnder(scr->focused_window->frame->core,
1072 wwin->frame->core);
1074 #undef _WIDTH
1075 #undef _HEIGHT
1077 #endif
1079 if (wPreferences.superfluous && !wPreferences.no_animations
1080 && !scr->flags.startup && wwin->transient_for==None
1082 * The brain damaged idiotic non-click to focus modes will
1083 * have trouble with this because:
1085 * 1. window is created and mapped by the client
1086 * 2. window is mapped by wmaker in small size
1087 * 3. window is animated to grow to normal size
1088 * 4. this function returns to normal event loop
1089 * 5. eventually, the EnterNotify event that would trigger
1090 * the window focusing (if the mouse is over that window)
1091 * will be processed by wmaker.
1092 * But since this event will be rather delayed
1093 * (step 3 has a large delay) the time when the event ocurred
1094 * and when it is processed, the client that owns that window
1095 * will reject the XSetInputFocus() for it.
1097 && (wPreferences.focus_mode==WKF_CLICK
1098 || wPreferences.auto_focus)) {
1099 DoWindowBirth(wwin);
1102 wWindowMap(wwin);
1105 /* setup stacking descriptor */
1106 if (transientOwner) {
1107 /* && wPreferences.on_top_transients */
1108 if (transientOwner) {
1109 wwin->frame->core->stacking->child_of =
1110 transientOwner->frame->core;
1112 } else {
1113 wwin->frame->core->stacking->child_of = NULL;
1117 if (!scr->focused_window) {
1118 /* first window on the list */
1119 wwin->next = NULL;
1120 wwin->prev = NULL;
1121 scr->focused_window = wwin;
1122 } else {
1123 WWindow *tmp;
1125 /* add window at beginning of focus window list */
1126 tmp = scr->focused_window;
1127 while (tmp->prev)
1128 tmp = tmp->prev;
1129 tmp->prev = wwin;
1130 wwin->next = tmp;
1131 wwin->prev = NULL;
1134 #ifdef GNOME_STUFF
1135 wGNOMEUpdateClientStateHint(wwin, True);
1136 #endif
1137 #ifdef KWM_HINTS
1138 wKWMUpdateClientWorkspace(wwin);
1139 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1140 #endif
1142 XUngrabServer(dpy);
1145 *--------------------------------------------------
1147 * Final preparations before window is ready to go
1149 *--------------------------------------------------
1152 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1155 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1156 && !wwin->flags.hidden) {
1157 if (((transientOwner && transientOwner->flags.focused)
1158 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1159 wSetFocusTo(scr, wwin);
1161 wWindowResetMouseGrabs(wwin);
1163 if (!WFLAGP(wwin, no_bind_keys)) {
1164 wWindowSetKeyGrabs(wwin);
1166 #ifdef GNOME_STUFF
1167 wGNOMEUpdateClientListHint(scr);
1168 #endif
1169 #ifdef KWM_HINTS
1170 wwin->flags.kwm_managed = 1;
1172 wKWMSendEventMessage(wwin, WKWMAddWindow);
1173 #endif
1175 wColormapInstallForWindow(scr, scr->cmap_window);
1177 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1179 #ifdef OLWM_HINTS
1180 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1181 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1183 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1184 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1185 wwin->frame_y);
1187 #endif
1190 *------------------------------------------------------------
1191 * Setup Notification Observers
1192 *------------------------------------------------------------
1194 WMAddNotificationObserver(appearanceObserver, wwin,
1195 WNWindowAppearanceSettingsChanged, wwin);
1199 *--------------------------------------------------
1201 * Cleanup temporary stuff
1203 *--------------------------------------------------
1206 if (win_state)
1207 wWindowDeleteSavedState(win_state);
1209 /* If the window must be withdrawed, then do it now.
1210 * Must do some optimization, 'though */
1211 if (withdraw) {
1212 wwin->flags.mapped = 0;
1213 wClientSetState(wwin, WithdrawnState, None);
1214 wUnmanageWindow(wwin, True, False);
1215 wwin = NULL;
1218 return wwin;
1225 WWindow*
1226 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1227 char *title, int x, int y, int width, int height)
1229 WWindow *wwin;
1230 int foo;
1232 wwin = wWindowCreate();
1234 WMAddNotificationObserver(appearanceObserver, wwin,
1235 WNWindowAppearanceSettingsChanged, wwin);
1237 wwin->flags.internal_window = 1;
1239 WSETUFLAG(wwin, omnipresent, 1);
1240 WSETUFLAG(wwin, no_shadeable, 1);
1241 WSETUFLAG(wwin, no_resizable, 1);
1242 WSETUFLAG(wwin, no_miniaturizable, 1);
1244 wwin->focus_mode = WFM_PASSIVE;
1246 wwin->client_win = window;
1247 wwin->screen_ptr = scr;
1249 wwin->transient_for = owner;
1251 wwin->client.x = x;
1252 wwin->client.y = y;
1253 wwin->client.width = width;
1254 wwin->client.height = height;
1256 wwin->frame_x = wwin->client.x;
1257 wwin->frame_y = wwin->client.y;
1260 foo = WFF_RIGHT_BUTTON;
1261 foo |= WFF_TITLEBAR;
1262 #ifdef XKB_BUTTON_HINT
1263 foo |= WFF_LANGUAGE_BUTTON;
1264 #endif
1266 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1267 wwin->frame_x, wwin->frame_y,
1268 width, height, foo,
1269 scr->window_title_texture,
1270 scr->resizebar_texture,
1271 scr->window_title_pixel,
1272 #ifdef DRAWSTRING_PLUGIN
1273 W_STRING_FTITLE,
1274 #endif
1275 &scr->window_title_gc,
1276 &scr->title_font);
1278 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1280 wwin->frame->flags.is_client_window_frame = 1;
1281 wwin->frame->flags.justification = wPreferences.title_justification;
1283 wFrameWindowChangeTitle(wwin->frame, title);
1285 /* setup button images */
1286 wWindowUpdateButtonImages(wwin);
1288 /* hide buttons */
1289 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1291 wwin->frame->child = wwin;
1293 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1295 #ifdef XKB_BUTTON_HINT
1296 if (wPreferences.modelock)
1297 wwin->frame->on_click_language = windowLanguageClick;
1298 #endif
1300 wwin->frame->on_click_right = windowCloseClick;
1302 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1303 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1305 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1307 wwin->client.y += wwin->frame->top_width;
1308 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1309 0, wwin->frame->top_width);
1311 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1312 wwin->client.width, wwin->client.height);
1314 /* setup the frame descriptor */
1315 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1316 wwin->frame->core->descriptor.parent = wwin;
1317 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1320 XLowerWindow(dpy, window);
1321 XMapSubwindows(dpy, wwin->frame->core->window);
1323 /* setup stacking descriptor */
1324 if (
1325 #ifdef removed
1326 wPreferences.on_top_transients &&
1327 #endif
1328 wwin->transient_for!=None
1329 && wwin->transient_for!=scr->root_win) {
1330 WWindow *tmp;
1331 tmp = wWindowFor(wwin->transient_for);
1332 if (tmp)
1333 wwin->frame->core->stacking->child_of = tmp->frame->core;
1334 } else {
1335 wwin->frame->core->stacking->child_of = NULL;
1339 if (!scr->focused_window) {
1340 /* first window on the list */
1341 wwin->next = NULL;
1342 wwin->prev = NULL;
1343 scr->focused_window = wwin;
1344 } else {
1345 WWindow *tmp;
1347 /* add window at beginning of focus window list */
1348 tmp = scr->focused_window;
1349 while (tmp->prev)
1350 tmp = tmp->prev;
1351 tmp->prev = wwin;
1352 wwin->next = tmp;
1353 wwin->prev = NULL;
1356 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1358 /* if (wPreferences.auto_focus)*/
1359 wSetFocusTo(scr, wwin);
1361 wWindowResetMouseGrabs(wwin);
1363 wWindowSetKeyGrabs(wwin);
1365 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1367 return wwin;
1372 *----------------------------------------------------------------------
1373 * wUnmanageWindow--
1374 * Removes the frame window from a window and destroys all data
1375 * related to it. The window will be reparented back to the root window
1376 * if restore is True.
1378 * Side effects:
1379 * Everything related to the window is destroyed and the window
1380 * is removed from the window lists. Focus is set to the previous on the
1381 * window list.
1382 *----------------------------------------------------------------------
1384 void
1385 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1387 WCoreWindow *frame = wwin->frame->core;
1388 WWindow *owner = NULL;
1389 WWindow *newFocusedWindow = NULL;
1390 int wasFocused;
1391 WScreen *scr = wwin->screen_ptr;
1394 #ifdef KWM_HINTS
1395 wwin->frame->workspace = -1;
1397 wKWMUpdateClientWorkspace(wwin);
1398 #endif
1400 /* First close attribute editor window if open */
1401 if (wwin->flags.inspector_open) {
1402 WWindow *pwin = wwin->inspector->frame; /* the inspector window */
1403 (*pwin->frame->on_click_right)(NULL, pwin, NULL);
1406 /* Close window menu if it's open for this window */
1407 if (wwin->flags.menu_open_for_me) {
1408 CloseWindowMenu(scr);
1411 if (!destroyed) {
1412 if (!wwin->flags.internal_window)
1413 XRemoveFromSaveSet(dpy, wwin->client_win);
1415 XSelectInput(dpy, wwin->client_win, NoEventMask);
1417 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1418 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1421 XUnmapWindow(dpy, frame->window);
1423 XUnmapWindow(dpy, wwin->client_win);
1425 /* deselect window */
1426 wSelectWindow(wwin, False);
1428 /* remove all pending events on window */
1429 /* I think this only matters for autoraise */
1430 if (wPreferences.raise_delay)
1431 WMDeleteTimerWithClientData(wwin->frame->core);
1433 XFlush(dpy);
1435 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1437 /* reparent the window back to the root */
1438 if (restore)
1439 wClientRestore(wwin);
1441 if (wwin->transient_for!=scr->root_win) {
1442 owner = wWindowFor(wwin->transient_for);
1443 if (owner) {
1444 if (!owner->flags.semi_focused) {
1445 owner = NULL;
1446 } else {
1447 owner->flags.semi_focused = 0;
1452 wasFocused = wwin->flags.focused;
1454 /* remove from window focus list */
1455 if (!wwin->prev && !wwin->next) {
1456 /* was the only window */
1457 scr->focused_window = NULL;
1458 newFocusedWindow = NULL;
1459 } else {
1460 WWindow *tmp;
1462 if (wwin->prev)
1463 wwin->prev->next = wwin->next;
1464 if (wwin->next)
1465 wwin->next->prev = wwin->prev;
1466 else {
1467 scr->focused_window = wwin->prev;
1468 scr->focused_window->next = NULL;
1471 if (wPreferences.focus_mode==WKF_CLICK) {
1473 /* if in click to focus mode and the window
1474 * was a transient, focus the owner window
1476 tmp = NULL;
1477 if (wPreferences.focus_mode==WKF_CLICK) {
1478 tmp = wWindowFor(wwin->transient_for);
1479 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1480 tmp = NULL;
1483 /* otherwise, focus the next one in the focus list */
1484 if (!tmp) {
1485 tmp = scr->focused_window;
1486 while (tmp) { /* look for one in the window list first */
1487 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1488 && (tmp->flags.mapped || tmp->flags.shaded))
1489 break;
1490 tmp = tmp->prev;
1492 if (!tmp) { /* if unsuccessful, choose any focusable window */
1493 tmp = scr->focused_window;
1494 while (tmp) {
1495 if (!WFLAGP(tmp, no_focusable)
1496 && (tmp->flags.mapped || tmp->flags.shaded))
1497 break;
1498 tmp = tmp->prev;
1503 newFocusedWindow = tmp;
1505 } else if (wPreferences.focus_mode==WKF_SLOPPY
1506 || wPreferences.focus_mode==WKF_POINTER) {
1507 unsigned int mask;
1508 int foo;
1509 Window bar, win;
1511 /* This is to let the root window get the keyboard input
1512 * if Sloppy focus mode and no other window get focus.
1513 * This way keybindings will not freeze.
1515 tmp = NULL;
1516 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1517 &foo, &foo, &foo, &foo, &mask))
1518 tmp = wWindowFor(win);
1519 if (tmp == wwin)
1520 tmp = NULL;
1521 newFocusedWindow = tmp;
1522 } else {
1523 newFocusedWindow = NULL;
1527 if (!wwin->flags.internal_window) {
1528 #ifdef GNOME_STUFF
1529 wGNOMERemoveClient(wwin);
1530 #endif
1531 #ifdef KWM_HINTS
1532 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1533 #endif
1536 #ifdef DEBUG
1537 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1538 (unsigned)frame->window);
1539 #endif
1541 if (wasFocused) {
1542 if (newFocusedWindow != owner && owner) {
1543 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1545 wSetFocusTo(scr, newFocusedWindow);
1547 wWindowDestroy(wwin);
1548 XFlush(dpy);
1552 void
1553 wWindowMap(WWindow *wwin)
1555 XMapWindow(dpy, wwin->frame->core->window);
1556 if (!wwin->flags.shaded) {
1557 /* window will be remapped when getting MapNotify */
1558 XSelectInput(dpy, wwin->client_win,
1559 wwin->event_mask & ~StructureNotifyMask);
1560 XMapWindow(dpy, wwin->client_win);
1561 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1563 wwin->flags.mapped = 1;
1568 void
1569 wWindowUnmap(WWindow *wwin)
1571 wwin->flags.mapped = 0;
1573 /* prevent window withdrawal when getting UnmapNotify */
1574 XSelectInput(dpy, wwin->client_win,
1575 wwin->event_mask & ~StructureNotifyMask);
1576 XUnmapWindow(dpy, wwin->client_win);
1577 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1579 XUnmapWindow(dpy, wwin->frame->core->window);
1584 void
1585 wWindowFocus(WWindow *wwin, WWindow *owin)
1587 WWindow *nowner;
1588 WWindow *oowner;
1590 #ifdef KEEP_XKB_LOCK_STATUS
1591 if (wPreferences.modelock) {
1592 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1594 #endif /* KEEP_XKB_LOCK_STATUS */
1596 wwin->flags.semi_focused = 0;
1598 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1600 wwin->flags.focused = 1;
1602 wWindowResetMouseGrabs(wwin);
1604 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1606 if (owin == wwin || !owin)
1607 return;
1609 nowner = wWindowFor(wwin->transient_for);
1611 /* new window is a transient for the old window */
1612 if (nowner == owin) {
1613 owin->flags.semi_focused = 1;
1614 wWindowUnfocus(nowner);
1615 return;
1618 oowner = wWindowFor(owin->transient_for);
1620 /* new window is owner of old window */
1621 if (wwin == oowner) {
1622 wWindowUnfocus(owin);
1623 return;
1626 if (!nowner) {
1627 wWindowUnfocus(owin);
1628 return;
1631 /* new window has same owner of old window */
1632 if (oowner == nowner) {
1633 /* prevent unfocusing of owner */
1634 oowner->flags.semi_focused = 0;
1635 wWindowUnfocus(owin);
1636 oowner->flags.semi_focused = 1;
1638 return;
1641 /* nowner != NULL && oowner != nowner */
1642 nowner->flags.semi_focused = 1;
1643 wWindowUnfocus(nowner);
1644 wWindowUnfocus(owin);
1648 void
1649 wWindowUnfocus(WWindow *wwin)
1651 CloseWindowMenu(wwin->screen_ptr);
1653 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1654 ? WS_PFOCUSED : WS_UNFOCUSED);
1656 if (wwin->transient_for!=None
1657 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1658 WWindow *owner;
1659 owner = wWindowFor(wwin->transient_for);
1660 if (owner && owner->flags.semi_focused) {
1661 owner->flags.semi_focused = 0;
1662 if (owner->flags.mapped || owner->flags.shaded) {
1663 wWindowUnfocus(owner);
1664 wFrameWindowPaint(owner->frame);
1668 wwin->flags.focused = 0;
1670 wWindowResetMouseGrabs(wwin);
1672 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1679 *----------------------------------------------------------------------
1681 * wWindowConstrainSize--
1682 * Constrains size for the client window, taking the maximal size,
1683 * window resize increments and other size hints into account.
1685 * Returns:
1686 * The closest size to what was given that the client window can
1687 * have.
1689 *----------------------------------------------------------------------
1691 void
1692 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1694 int width = *nwidth;
1695 int height = *nheight;
1696 int winc = 1;
1697 int hinc = 1;
1698 int minW = 1, minH = 1;
1699 int maxW = wwin->screen_ptr->scr_width*2;
1700 int maxH = wwin->screen_ptr->scr_height*2;
1701 int minAX = -1, minAY = -1;
1702 int maxAX = -1, maxAY = -1;
1703 int baseW = 0;
1704 int baseH = 0;
1706 if (wwin->normal_hints) {
1707 winc = wwin->normal_hints->width_inc;
1708 hinc = wwin->normal_hints->height_inc;
1709 minW = wwin->normal_hints->min_width;
1710 minH = wwin->normal_hints->min_height;
1711 maxW = wwin->normal_hints->max_width;
1712 maxH = wwin->normal_hints->max_height;
1713 if (wwin->normal_hints->flags & PAspect) {
1714 minAX = wwin->normal_hints->min_aspect.x;
1715 minAY = wwin->normal_hints->min_aspect.y;
1716 maxAX = wwin->normal_hints->max_aspect.x;
1717 maxAY = wwin->normal_hints->max_aspect.y;
1721 if (width < minW)
1722 width = minW;
1723 if (height < minH)
1724 height = minH;
1726 if (width > maxW)
1727 width = maxW;
1728 if (height > maxH)
1729 height = maxH;
1731 /* aspect ratio code borrowed from olwm */
1732 if (minAX > 0) {
1733 /* adjust max aspect ratio */
1734 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1735 if (maxAX > maxAY) {
1736 height = (width * maxAY) / maxAX;
1737 if (height > maxH) {
1738 height = maxH;
1739 width = (height * maxAX) / maxAY;
1741 } else {
1742 width = (height * maxAX) / maxAY;
1743 if (width > maxW) {
1744 width = maxW;
1745 height = (width * maxAY) / maxAX;
1750 /* adjust min aspect ratio */
1751 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1752 if (minAX > minAY) {
1753 height = (width * minAY) / minAX;
1754 if (height < minH) {
1755 height = minH;
1756 width = (height * minAX) / minAY;
1758 } else {
1759 width = (height * minAX) / minAY;
1760 if (width < minW) {
1761 width = minW;
1762 height = (width * minAY) / minAX;
1768 if (baseW != 0) {
1769 width = (((width - baseW) / winc) * winc) + baseW;
1770 } else {
1771 width = (((width - minW) / winc) * winc) + minW;
1774 if (baseW != 0) {
1775 height = (((height - baseH) / hinc) * hinc) + baseH;
1776 } else {
1777 height = (((height - minH) / hinc) * hinc) + minH;
1780 *nwidth = width;
1781 *nheight = height;
1785 void
1786 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1788 WScreen *scr = wwin->screen_ptr;
1789 WApplication *wapp;
1790 int unmap = 0;
1792 if (workspace >= scr->workspace_count || workspace < 0
1793 || workspace == wwin->frame->workspace)
1794 return;
1796 if (workspace != scr->current_workspace) {
1797 /* Sent to other workspace. Unmap window */
1798 if ((wwin->flags.mapped
1799 || wwin->flags.shaded
1800 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1801 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1803 wapp = wApplicationOf(wwin->main_window);
1804 if (wapp) {
1805 wapp->last_workspace = workspace;
1807 if (wwin->flags.miniaturized) {
1808 if (wwin->icon) {
1809 XUnmapWindow(dpy, wwin->icon->core->window);
1810 wwin->icon->mapped = 0;
1812 } else {
1813 unmap = 1;
1814 wSetFocusTo(scr, NULL);
1817 } else {
1818 /* brought to current workspace. Map window */
1819 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1820 if (wwin->icon) {
1821 XMapWindow(dpy, wwin->icon->core->window);
1822 wwin->icon->mapped = 1;
1824 } else if (!wwin->flags.mapped &&
1825 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1826 wWindowMap(wwin);
1829 if (!IS_OMNIPRESENT(wwin)) {
1830 wwin->frame->workspace = workspace;
1831 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1833 #ifdef GNOME_STUFF
1834 wGNOMEUpdateClientStateHint(wwin, True);
1835 #endif
1836 #ifdef KWM_HINTS
1837 wKWMUpdateClientWorkspace(wwin);
1838 wKWMSendEventMessage(wwin, WKWMChangedClient);
1839 #endif
1840 if (unmap) {
1841 wWindowUnmap(wwin);
1846 void
1847 wWindowSynthConfigureNotify(WWindow *wwin)
1849 XEvent sevent;
1851 sevent.type = ConfigureNotify;
1852 sevent.xconfigure.display = dpy;
1853 sevent.xconfigure.event = wwin->client_win;
1854 sevent.xconfigure.window = wwin->client_win;
1856 sevent.xconfigure.x = wwin->client.x;
1857 sevent.xconfigure.y = wwin->client.y;
1858 sevent.xconfigure.width = wwin->client.width;
1859 sevent.xconfigure.height = wwin->client.height;
1861 sevent.xconfigure.border_width = wwin->old_border_width;
1862 if (WFLAGP(wwin, no_titlebar))
1863 sevent.xconfigure.above = None;
1864 else
1865 sevent.xconfigure.above = wwin->frame->titlebar->window;
1867 sevent.xconfigure.override_redirect = False;
1868 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1869 #ifdef KWM_HINTS
1870 wKWMSendEventMessage(wwin, WKWMChangedClient);
1871 #endif
1872 XFlush(dpy);
1877 *----------------------------------------------------------------------
1878 * wWindowConfigure--
1879 * Configures the frame, decorations and client window to the
1880 * specified geometry. The geometry is not checked for validity,
1881 * wWindowConstrainSize() must be used for that.
1882 * The size parameters are for the client window, but the position is
1883 * for the frame.
1884 * The client window receives a ConfigureNotify event, according
1885 * to what ICCCM says.
1887 * Returns:
1888 * None
1890 * Side effects:
1891 * Window size and position are changed and client window receives
1892 * a ConfigureNotify event.
1893 *----------------------------------------------------------------------
1895 void
1896 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1897 WWindow *wwin;
1898 int req_x, req_y; /* new position of the frame */
1899 int req_width, req_height; /* new size of the client */
1901 int synth_notify = False;
1902 int resize;
1904 resize = (req_width!=wwin->client.width
1905 || req_height!=wwin->client.height);
1907 * if the window is being moved but not resized then
1908 * send a synthetic ConfigureNotify
1910 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1911 synth_notify = True;
1914 if (WFLAGP(wwin, dont_move_off))
1915 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1916 req_width, req_height);
1917 if (resize) {
1918 if (req_width < MIN_WINDOW_SIZE)
1919 req_width = MIN_WINDOW_SIZE;
1920 if (req_height < MIN_WINDOW_SIZE)
1921 req_height = MIN_WINDOW_SIZE;
1923 /* If growing, resize inner part before frame,
1924 * if shrinking, resize frame before.
1925 * This will prevent the frame (that can have a different color)
1926 * to be exposed, causing flicker */
1927 if (req_height > wwin->frame->core->height
1928 || req_width > wwin->frame->core->width)
1929 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1931 if (wwin->flags.shaded) {
1932 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1933 req_width, wwin->frame->core->height);
1934 wwin->old_geometry.height = req_height;
1935 } else {
1936 int h;
1938 h = req_height + wwin->frame->top_width
1939 + wwin->frame->bottom_width;
1941 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1944 if (!(req_height > wwin->frame->core->height
1945 || req_width > wwin->frame->core->width))
1946 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1948 wwin->client.x = req_x;
1949 wwin->client.y = req_y + wwin->frame->top_width;
1950 wwin->client.width = req_width;
1951 wwin->client.height = req_height;
1952 } else {
1953 wwin->client.x = req_x;
1954 wwin->client.y = req_y + wwin->frame->top_width;
1956 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1958 wwin->frame_x = req_x;
1959 wwin->frame_y = req_y;
1961 #ifdef SHAPE
1962 if (wShapeSupported && wwin->flags.shaped && resize) {
1963 wWindowSetShape(wwin);
1965 #endif
1967 if (synth_notify)
1968 wWindowSynthConfigureNotify(wwin);
1969 XFlush(dpy);
1973 void
1974 wWindowMove(wwin, req_x, req_y)
1975 WWindow *wwin;
1976 int req_x, req_y; /* new position of the frame */
1978 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1979 int synth_notify = False;
1981 /* Send a synthetic ConfigureNotify event for every window movement. */
1982 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1983 synth_notify = True;
1985 #else
1986 /* A single synthetic ConfigureNotify event is sent at the end of
1987 * a completed (opaque) movement in moveres.c */
1988 #endif
1990 if (WFLAGP(wwin, dont_move_off))
1991 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1992 wwin->frame->core->width, wwin->frame->core->height);
1994 wwin->client.x = req_x;
1995 wwin->client.y = req_y + wwin->frame->top_width;
1997 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1999 wwin->frame_x = req_x;
2000 wwin->frame_y = req_y;
2002 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2003 if (synth_notify)
2004 wWindowSynthConfigureNotify(wwin);
2005 #endif
2008 void
2009 wWindowUpdateButtonImages(WWindow *wwin)
2011 WScreen *scr = wwin->screen_ptr;
2012 Pixmap pixmap, mask;
2013 WFrameWindow *fwin = wwin->frame;
2015 if (WFLAGP(wwin, no_titlebar))
2016 return;
2018 /* miniaturize button */
2020 if (!WFLAGP(wwin, no_miniaturize_button)) {
2021 if (wwin->wm_gnustep_attr
2022 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2023 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2025 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2026 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2027 } else {
2028 mask = None;
2031 if (fwin->lbutton_image
2032 && (fwin->lbutton_image->image != pixmap
2033 || fwin->lbutton_image->mask != mask)) {
2034 wPixmapDestroy(fwin->lbutton_image);
2035 fwin->lbutton_image = NULL;
2038 if (!fwin->lbutton_image) {
2039 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2040 fwin->lbutton_image->client_owned = 1;
2041 fwin->lbutton_image->client_owned_mask = 1;
2043 } else {
2044 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2045 wPixmapDestroy(fwin->lbutton_image);
2047 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2051 #ifdef XKB_BUTTON_HINT
2052 if (!WFLAGP(wwin, no_language_button)) {
2053 if (fwin->languagebutton_image &&
2054 !fwin->languagebutton_image->shared) {
2055 wPixmapDestroy(fwin->languagebutton_image);
2057 fwin->languagebutton_image =
2058 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2060 #endif
2062 /* close button */
2064 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2065 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2067 if (!WFLAGP(wwin, no_close_button)) {
2068 if (wwin->wm_gnustep_attr
2069 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2070 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2072 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2073 mask = wwin->wm_gnustep_attr->close_mask;
2074 else
2075 mask = None;
2077 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2078 || fwin->rbutton_image->mask != mask)) {
2079 wPixmapDestroy(fwin->rbutton_image);
2080 fwin->rbutton_image = NULL;
2083 if (!fwin->rbutton_image) {
2084 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2085 fwin->rbutton_image->client_owned = 1;
2086 fwin->rbutton_image->client_owned_mask = 1;
2089 } else if (WFLAGP(wwin, kill_close)) {
2091 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2092 wPixmapDestroy(fwin->rbutton_image);
2094 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2096 } else if (MGFLAGP(wwin, broken_close)) {
2098 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2099 wPixmapDestroy(fwin->rbutton_image);
2101 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2103 } else {
2105 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2106 wPixmapDestroy(fwin->rbutton_image);
2108 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2112 /* force buttons to be redrawn */
2113 fwin->flags.need_texture_change = 1;
2114 wFrameWindowPaint(fwin);
2119 *---------------------------------------------------------------------------
2120 * wWindowConfigureBorders--
2121 * Update window border configuration according to attribute flags.
2123 *---------------------------------------------------------------------------
2125 void
2126 wWindowConfigureBorders(WWindow *wwin)
2128 if (wwin->frame) {
2129 int flags;
2130 int newy, oldh;
2132 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2133 #ifdef XKB_BUTTON_HINT
2134 flags |= WFF_LANGUAGE_BUTTON;
2135 #endif
2136 if (!WFLAGP(wwin, no_titlebar))
2137 flags |= WFF_TITLEBAR;
2138 if (!WFLAGP(wwin, no_resizebar))
2139 flags |= WFF_RESIZEBAR;
2140 if (wwin->flags.shaded)
2141 flags |= WFF_IS_SHADED;
2143 oldh = wwin->frame->top_width;
2144 wFrameWindowUpdateBorders(wwin->frame, flags);
2145 if (oldh != wwin->frame->top_width) {
2146 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2148 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2149 wWindowConfigure(wwin, wwin->frame_x, newy,
2150 wwin->client.width, wwin->client.height);
2153 flags = 0;
2154 if (!WFLAGP(wwin, no_miniaturize_button)
2155 && wwin->frame->flags.hide_left_button)
2156 flags |= WFF_LEFT_BUTTON;
2158 #ifdef XKB_BUTTON_HINT
2159 if (!WFLAGP(wwin, no_language_button)
2160 && wwin->frame->flags.hide_language_button)
2161 flags |= WFF_LANGUAGE_BUTTON;
2162 #endif
2164 if (!WFLAGP(wwin, no_close_button)
2165 && wwin->frame->flags.hide_right_button)
2166 flags |= WFF_RIGHT_BUTTON;
2168 if (flags!=0) {
2169 wWindowUpdateButtonImages(wwin);
2170 wFrameWindowShowButton(wwin->frame, flags);
2173 flags = 0;
2174 if (WFLAGP(wwin, no_miniaturize_button)
2175 && !wwin->frame->flags.hide_left_button)
2176 flags |= WFF_LEFT_BUTTON;
2178 #ifdef XKB_BUTTON_HINT
2179 if (WFLAGP(wwin, no_language_button)
2180 && !wwin->frame->flags.hide_language_button)
2181 flags |= WFF_LANGUAGE_BUTTON;
2182 #endif
2184 if (WFLAGP(wwin, no_close_button)
2185 && !wwin->frame->flags.hide_right_button)
2186 flags |= WFF_RIGHT_BUTTON;
2188 if (flags!=0)
2189 wFrameWindowHideButton(wwin->frame, flags);
2191 #ifdef SHAPE
2192 if (wShapeSupported && wwin->flags.shaped) {
2193 wWindowSetShape(wwin);
2195 #endif
2200 void
2201 wWindowSaveState(WWindow *wwin)
2203 CARD32 data[10];
2204 int i;
2206 memset(data, 0, sizeof(CARD32)*10);
2207 data[0] = wwin->frame->workspace;
2208 data[1] = wwin->flags.miniaturized;
2209 data[2] = wwin->flags.shaded;
2210 data[3] = wwin->flags.hidden;
2212 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2213 if (wwin->screen_ptr->shortcutWindow[i] == wwin)
2214 data[9] |= 1<<i;
2216 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2217 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2218 (unsigned char *)data, 10);
2222 static int
2223 getSavedState(Window window, WSavedState **state)
2225 Atom type_ret;
2226 int fmt_ret;
2227 unsigned long nitems_ret;
2228 unsigned long bytes_after_ret;
2229 CARD32 *data;
2231 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2232 True, _XA_WINDOWMAKER_STATE,
2233 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2234 (unsigned char **)&data)!=Success || !data)
2235 return 0;
2237 *state = malloc(sizeof(WSavedState));
2239 if (*state) {
2240 (*state)->workspace = data[0];
2241 (*state)->miniaturized = data[1];
2242 (*state)->shaded = data[2];
2243 (*state)->hidden = data[3];
2244 (*state)->use_geometry = data[4];
2245 (*state)->x = data[5];
2246 (*state)->y = data[6];
2247 (*state)->w = data[7];
2248 (*state)->h = data[8];
2249 (*state)->window_shortcuts = data[9];
2251 XFree(data);
2253 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2254 return 1;
2255 else
2256 return 0;
2260 #ifdef SHAPE
2261 void
2262 wWindowClearShape(WWindow *wwin)
2264 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2265 0, wwin->frame->top_width, None, ShapeSet);
2266 XFlush(dpy);
2269 void
2270 wWindowSetShape(WWindow *wwin)
2272 XRectangle rect[2];
2273 int count;
2274 #ifdef OPTIMIZE_SHAPE
2275 XRectangle *rects;
2276 XRectangle *urec;
2277 int ordering;
2279 /* only shape is the client's */
2280 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2281 goto alt_code;
2284 /* Get array of rectangles describing the shape mask */
2285 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2286 &count, &ordering);
2287 if (!rects) {
2288 goto alt_code;
2291 urec = malloc(sizeof(XRectangle)*(count+2));
2292 if (!urec) {
2293 XFree(rects);
2294 goto alt_code;
2297 /* insert our decoration rectangles in the rect list */
2298 memcpy(urec, rects, sizeof(XRectangle)*count);
2299 XFree(rects);
2301 if (!WFLAGP(wwin, no_titlebar)) {
2302 urec[count].x = -1;
2303 urec[count].y = -1 - wwin->frame->top_width;
2304 urec[count].width = wwin->frame->core->width + 2;
2305 urec[count].height = wwin->frame->top_width + 1;
2306 count++;
2308 if (!WFLAGP(wwin, no_resizebar)) {
2309 urec[count].x = -1;
2310 urec[count].y = wwin->frame->core->height
2311 - wwin->frame->bottom_width - wwin->frame->top_width;
2312 urec[count].width = wwin->frame->core->width + 2;
2313 urec[count].height = wwin->frame->bottom_width + 1;
2314 count++;
2317 /* shape our frame window */
2318 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2319 0, wwin->frame->top_width, urec, count,
2320 ShapeSet, Unsorted);
2321 XFlush(dpy);
2322 free(urec);
2323 return;
2325 alt_code:
2326 #endif /* OPTIMIZE_SHAPE */
2327 count = 0;
2328 if (!WFLAGP(wwin, no_titlebar)) {
2329 rect[count].x = -1;
2330 rect[count].y = -1;
2331 rect[count].width = wwin->frame->core->width + 2;
2332 rect[count].height = wwin->frame->top_width + 1;
2333 count++;
2335 if (!WFLAGP(wwin, no_resizebar)) {
2336 rect[count].x = -1;
2337 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2338 rect[count].width = wwin->frame->core->width + 2;
2339 rect[count].height = wwin->frame->bottom_width + 1;
2340 count++;
2342 if (count > 0) {
2343 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2344 0, 0, rect, count, ShapeSet, Unsorted);
2346 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2347 0, wwin->frame->top_width, wwin->client_win,
2348 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2349 XFlush(dpy);
2351 #endif /* SHAPE */
2353 /* ====================================================================== */
2355 static FocusMode
2356 getFocusMode(WWindow *wwin)
2358 FocusMode mode;
2360 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2361 if (wwin->wm_hints->input == True) {
2362 if (wwin->protocols.TAKE_FOCUS)
2363 mode = WFM_LOCALLY_ACTIVE;
2364 else
2365 mode = WFM_PASSIVE;
2366 } else {
2367 if (wwin->protocols.TAKE_FOCUS)
2368 mode = WFM_GLOBALLY_ACTIVE;
2369 else
2370 mode = WFM_NO_INPUT;
2372 } else {
2373 mode = WFM_PASSIVE;
2375 return mode;
2379 void
2380 wWindowSetKeyGrabs(WWindow *wwin)
2382 int i;
2383 WShortKey *key;
2385 for (i=0; i<WKBD_LAST; i++) {
2386 key = &wKeyBindings[i];
2388 if (key->keycode==0)
2389 continue;
2390 if (key->modifier!=AnyModifier) {
2391 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2392 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2393 #ifdef NUMLOCK_HACK
2394 /* Also grab all modifier combinations possible that include,
2395 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2396 * work even if the NumLock/ScrollLock key is on.
2398 wHackedGrabKey(key->keycode, key->modifier,
2399 wwin->frame->core->window, True, GrabModeAsync,
2400 GrabModeAsync);
2401 #endif
2403 XGrabKey(dpy, key->keycode, key->modifier,
2404 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2407 #ifndef LITE
2408 wRootMenuBindShortcuts(wwin->frame->core->window);
2409 #endif
2414 void
2415 wWindowResetMouseGrabs(WWindow *wwin)
2417 /* Mouse grabs can't be done on the client window because of
2418 * ICCCM and because clients that try to do the same will crash.
2420 * But there is a problem wich makes tbar buttons of unfocused
2421 * windows not usable as the click goes to the frame window instead
2422 * of the button itself. Must figure a way to fix that.
2425 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2427 if (!WFLAGP(wwin, no_bind_mouse)) {
2428 /* grabs for Meta+drag */
2429 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2430 True, ButtonPressMask, GrabModeSync,
2431 GrabModeAsync, None, None);
2434 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2435 /* the passive grabs to focus the window */
2436 if (wPreferences.focus_mode == WKF_CLICK)
2437 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2438 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2439 None, None);
2441 XFlush(dpy);
2445 void
2446 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2448 if (attr->flags & GSExtraFlagsAttr) {
2449 if (MGFLAGP(wwin, broken_close) !=
2450 (attr->extra_flags & GSDocumentEditedFlag)) {
2451 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2452 wWindowUpdateButtonImages(wwin);
2458 WMagicNumber
2459 wWindowAddSavedState(char *instance, char *class, char *command,
2460 pid_t pid, WSavedState *state)
2462 WWindowState *wstate;
2464 wstate = malloc(sizeof(WWindowState));
2465 if (!wstate)
2466 return 0;
2468 memset(wstate, 0, sizeof(WWindowState));
2469 wstate->pid = pid;
2470 if (instance)
2471 wstate->instance = wstrdup(instance);
2472 if (class)
2473 wstate->class = wstrdup(class);
2474 if (command)
2475 wstate->command = wstrdup(command);
2476 wstate->state = state;
2478 wstate->next = windowState;
2479 windowState = wstate;
2481 #ifdef DEBUG
2482 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2483 class, command);
2484 #endif
2486 return wstate;
2490 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2493 WMagicNumber
2494 wWindowGetSavedState(Window win)
2496 char *instance, *class, *command=NULL;
2497 WWindowState *wstate = windowState;
2498 char **argv;
2499 int argc;
2501 if (!wstate)
2502 return NULL;
2504 if (XGetCommand(dpy, win, &argv, &argc)) {
2505 if (argc > 0)
2506 command = FlattenStringList(argv, argc);
2507 XFreeStringList(argv);
2509 if (!command)
2510 return NULL;
2512 if (PropGetWMClass(win, &class, &instance)) {
2513 while (wstate) {
2514 if (SAME(instance, wstate->instance) &&
2515 SAME(class, wstate->class) &&
2516 SAME(command, wstate->command)) {
2517 break;
2519 wstate = wstate->next;
2521 } else {
2522 wstate = NULL;
2525 #ifdef DEBUG
2526 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2527 class, command);
2528 #endif
2530 if (command) free(command);
2531 if (instance) XFree(instance);
2532 if (class) XFree(class);
2534 return wstate;
2538 void
2539 wWindowDeleteSavedState(WMagicNumber id)
2541 WWindowState *tmp, *wstate=(WWindowState*)id;
2543 if (!wstate || !windowState)
2544 return;
2546 tmp = windowState;
2547 if (tmp==wstate) {
2548 windowState = wstate->next;
2549 #ifdef DEBUG
2550 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2551 wstate, wstate->instance, wstate->class, wstate->command);
2552 #endif
2553 if (wstate->instance) free(wstate->instance);
2554 if (wstate->class) free(wstate->class);
2555 if (wstate->command) free(wstate->command);
2556 free(wstate->state);
2557 free(wstate);
2558 } else {
2559 while (tmp->next) {
2560 if (tmp->next==wstate) {
2561 tmp->next=wstate->next;
2562 #ifdef DEBUG
2563 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2564 wstate, wstate->instance, wstate->class, wstate->command);
2565 #endif
2566 if (wstate->instance) free(wstate->instance);
2567 if (wstate->class) free(wstate->class);
2568 if (wstate->command) free(wstate->command);
2569 free(wstate->state);
2570 free(wstate);
2571 break;
2573 tmp = tmp->next;
2579 void
2580 wWindowDeleteSavedStatesForPID(pid_t pid)
2582 WWindowState *tmp, *wstate;
2584 if (!windowState)
2585 return;
2587 tmp = windowState;
2588 if (tmp->pid == pid) {
2589 wstate = windowState;
2590 windowState = tmp->next;
2591 #ifdef DEBUG
2592 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2593 wstate, wstate->instance, wstate->class, wstate->command);
2594 #endif
2595 if (wstate->instance) free(wstate->instance);
2596 if (wstate->class) free(wstate->class);
2597 if (wstate->command) free(wstate->command);
2598 free(wstate->state);
2599 free(wstate);
2600 } else {
2601 while (tmp->next) {
2602 if (tmp->next->pid==pid) {
2603 wstate = tmp->next;
2604 tmp->next = wstate->next;
2605 #ifdef DEBUG
2606 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2607 wstate, wstate->instance, wstate->class, wstate->command);
2608 #endif
2609 if (wstate->instance) free(wstate->instance);
2610 if (wstate->class) free(wstate->class);
2611 if (wstate->command) free(wstate->command);
2612 free(wstate->state);
2613 free(wstate);
2614 break;
2616 tmp = tmp->next;
2622 /* ====================================================================== */
2624 static void
2625 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2627 WWindow *wwin = data;
2629 #ifndef NUMLOCK_HACK
2630 if ((event->xbutton.state & ValidModMask)
2631 != (event->xbutton.state & ~LockMask)) {
2632 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2633 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2635 #endif
2637 event->xbutton.state &= ValidModMask;
2639 CloseWindowMenu(wwin->screen_ptr);
2641 if (wPreferences.focus_mode==WKF_CLICK
2642 && !(event->xbutton.state&ControlMask)
2643 && !WFLAGP(wwin, no_focusable)) {
2644 wSetFocusTo(wwin->screen_ptr, wwin);
2647 if (event->xbutton.button == Button1)
2648 wRaiseFrame(wwin->frame->core);
2650 if (event->xbutton.window != wwin->frame->resizebar->window) {
2651 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2652 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2653 GrabModeAsync, GrabModeAsync, None,
2654 None, CurrentTime)!=GrabSuccess) {
2655 #ifdef DEBUG0
2656 wwarning("pointer grab failed for window move");
2657 #endif
2658 return;
2662 if (event->xbutton.state & MOD_MASK) {
2663 /* move the window */
2664 wMouseMoveWindow(wwin, event);
2665 XUngrabPointer(dpy, CurrentTime);
2666 } else {
2667 wMouseResizeWindow(wwin, event);
2668 XUngrabPointer(dpy, CurrentTime);
2674 static void
2675 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2677 WWindow *wwin = data;
2679 event->xbutton.state &= ValidModMask;
2681 if (event->xbutton.button==Button1) {
2682 if (event->xbutton.state == 0) {
2683 if (!WFLAGP(wwin, no_shadeable)) {
2684 /* shade window */
2685 if (wwin->flags.shaded)
2686 wUnshadeWindow(wwin);
2687 else
2688 wShadeWindow(wwin);
2690 } else {
2691 int dir = 0;
2693 if (event->xbutton.state & ControlMask)
2694 dir |= MAX_VERTICAL;
2696 if (event->xbutton.state & ShiftMask) {
2697 dir |= MAX_HORIZONTAL;
2698 if (!(event->xbutton.state & ControlMask))
2699 wSelectWindow(wwin, !wwin->flags.selected);
2702 /* maximize window */
2703 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2704 if (wwin->flags.maximized)
2705 wUnmaximizeWindow(wwin);
2706 else
2707 wMaximizeWindow(wwin, dir);
2710 } else if (event->xbutton.button==Button3) {
2711 if (event->xbutton.state & MOD_MASK) {
2712 wHideOtherApplications(wwin);
2714 } else if (event->xbutton.button==Button2) {
2715 wSelectWindow(wwin, !wwin->flags.selected);
2720 static void
2721 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2723 WWindow *wwin = desc->parent;
2725 event->xbutton.state &= ValidModMask;
2727 CloseWindowMenu(wwin->screen_ptr);
2729 if (wPreferences.focus_mode==WKF_CLICK
2730 && !(event->xbutton.state&ControlMask)
2731 && !WFLAGP(wwin, no_focusable)) {
2732 wSetFocusTo(wwin->screen_ptr, wwin);
2734 if (event->xbutton.button == Button1) {
2735 wRaiseFrame(wwin->frame->core);
2738 if (event->xbutton.state & MOD_MASK) {
2739 /* move the window */
2740 if (XGrabPointer(dpy, wwin->client_win, False,
2741 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2742 GrabModeAsync, GrabModeAsync, None,
2743 None, CurrentTime)!=GrabSuccess) {
2744 #ifdef DEBUG0
2745 wwarning("pointer grab failed for window move");
2746 #endif
2747 return;
2749 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2750 wMouseResizeWindow(wwin, event);
2751 else
2752 wMouseMoveWindow(wwin, event);
2753 XUngrabPointer(dpy, CurrentTime);
2758 static void
2759 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2761 WWindow *wwin = (WWindow*)data;
2763 #ifndef NUMLOCK_HACK
2764 if ((event->xbutton.state & ValidModMask)
2765 != (event->xbutton.state & ~LockMask)) {
2766 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2767 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2769 #endif
2770 event->xbutton.state &= ValidModMask;
2772 CloseWindowMenu(wwin->screen_ptr);
2774 if (wPreferences.focus_mode==WKF_CLICK
2775 && !(event->xbutton.state&ControlMask)
2776 && !WFLAGP(wwin, no_focusable)) {
2777 wSetFocusTo(wwin->screen_ptr, wwin);
2780 if (event->xbutton.button == Button1
2781 || event->xbutton.button == Button2) {
2783 if (event->xbutton.button == Button1) {
2784 if (event->xbutton.state & MOD_MASK) {
2785 wLowerFrame(wwin->frame->core);
2786 } else {
2787 wRaiseFrame(wwin->frame->core);
2790 if ((event->xbutton.state & ShiftMask)
2791 && !(event->xbutton.state & ControlMask)) {
2792 wSelectWindow(wwin, !wwin->flags.selected);
2793 return;
2795 if (event->xbutton.window != wwin->frame->titlebar->window
2796 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2797 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2798 GrabModeAsync, GrabModeAsync, None,
2799 None, CurrentTime)!=GrabSuccess) {
2800 #ifdef DEBUG0
2801 wwarning("pointer grab failed for window move");
2802 #endif
2803 return;
2806 /* move the window */
2807 wMouseMoveWindow(wwin, event);
2809 XUngrabPointer(dpy, CurrentTime);
2810 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2811 && !wwin->flags.internal_window
2812 && !WCHECK_STATE(WSTATE_MODAL)) {
2813 WObjDescriptor *desc;
2815 if (event->xbutton.window != wwin->frame->titlebar->window
2816 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2817 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2818 GrabModeAsync, GrabModeAsync, None,
2819 None, CurrentTime)!=GrabSuccess) {
2820 #ifdef DEBUG0
2821 wwarning("pointer grab failed for window move");
2822 #endif
2823 return;
2826 OpenWindowMenu(wwin, event->xbutton.x_root,
2827 wwin->frame_y+wwin->frame->top_width, False);
2829 /* allow drag select */
2830 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2831 event->xany.send_event = True;
2832 (*desc->handle_mousedown)(desc, event);
2834 XUngrabPointer(dpy, CurrentTime);
2840 static void
2841 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2843 WWindow *wwin = data;
2845 event->xbutton.state &= ValidModMask;
2847 CloseWindowMenu(wwin->screen_ptr);
2849 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2850 return;
2852 /* if control-click, kill the client */
2853 if (event->xbutton.state & ControlMask) {
2854 wClientKill(wwin);
2855 } else {
2856 #ifdef OLWM_HINTS
2857 if (wwin->flags.olwm_push_pin_out) {
2859 wwin->flags.olwm_push_pin_out = 0;
2861 wOLWMChangePushpinState(wwin, True);
2863 wFrameWindowUpdatePushButton(wwin->frame, False);
2865 return;
2867 #endif
2868 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2869 /* send delete message */
2870 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2876 static void
2877 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2879 WWindow *wwin = data;
2881 CloseWindowMenu(wwin->screen_ptr);
2883 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2884 return;
2886 /* send delete message */
2887 if (wwin->protocols.DELETE_WINDOW) {
2888 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2889 } else {
2890 wClientKill(wwin);
2895 #ifdef XKB_BUTTON_HINT
2896 static void
2897 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2899 WWindow *wwin = data;
2900 WFrameWindow *fwin = wwin->frame;
2901 WScreen *scr = fwin->screen_ptr;
2902 XkbStateRec staterec;
2903 int tl;
2905 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2906 return;
2907 tl = wwin->frame->languagemode;
2908 wwin->frame->languagemode = wwin->frame->last_languagemode;
2909 wwin->frame->last_languagemode = tl;
2910 wSetFocusTo(scr, wwin);
2911 wwin->frame->languagebutton_image =
2912 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
2913 wwin->frame->languagemode];
2914 wFrameWindowUpdateLanguageButton(wwin->frame);
2915 if (event->xbutton.button == Button3)
2916 return;
2917 wRaiseFrame(fwin->core);
2919 #endif
2922 static void
2923 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2925 WWindow *wwin = data;
2927 event->xbutton.state &= ValidModMask;
2929 CloseWindowMenu(wwin->screen_ptr);
2931 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2932 return;
2934 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2935 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2936 LastTimestamp);
2937 } else {
2938 WApplication *wapp;
2939 if ((event->xbutton.state & ControlMask) ||
2940 (event->xbutton.button == Button3)) {
2942 wapp = wApplicationOf(wwin->main_window);
2943 if (wapp && !WFLAGP(wwin, no_appicon))
2944 wHideApplication(wapp);
2945 } else if (event->xbutton.state==0) {
2946 wIconifyWindow(wwin);