fixed crash on restart
[wmaker-crm.git] / src / window.c
blobf9c4f0aacc74622fc3a90a9faff476e22631042d
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->shortcutWindows[i])
216 continue;
218 WMRemoveFromBag(wwin->screen_ptr->shortcutWindows[i], wwin);
220 if (!WMGetBagItemCount(wwin->screen_ptr->shortcutWindows[i])) {
221 WMFreeBag(wwin->screen_ptr->shortcutWindows[i]);
222 wwin->screen_ptr->shortcutWindows[i] = NULL;
226 if (wwin->normal_hints)
227 free(wwin->normal_hints);
229 if (wwin->wm_hints)
230 XFree(wwin->wm_hints);
232 if (wwin->wm_instance)
233 XFree(wwin->wm_instance);
235 if (wwin->wm_class)
236 XFree(wwin->wm_class);
238 if (wwin->wm_gnustep_attr)
239 free(wwin->wm_gnustep_attr);
241 if (wwin->cmap_windows)
242 XFree(wwin->cmap_windows);
244 XDeleteContext(dpy, wwin->client_win, wWinContext);
246 if (wwin->frame)
247 wFrameWindowDestroy(wwin->frame);
249 if (wwin->icon) {
250 RemoveFromStackList(wwin->icon->core);
251 wIconDestroy(wwin->icon);
252 if (wPreferences.auto_arrange_icons)
253 wArrangeIcons(wwin->screen_ptr, True);
255 wrelease(wwin);
261 static void
262 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
264 if (gs_hints->flags & GSWindowStyleAttr) {
265 wwin->client_flags.no_titlebar =
266 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
268 wwin->client_flags.no_close_button =
269 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
271 wwin->client_flags.no_closable =
272 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
274 wwin->client_flags.no_miniaturize_button =
275 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
277 wwin->client_flags.no_miniaturizable =
278 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
280 wwin->client_flags.no_resizebar =
281 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
283 wwin->client_flags.no_resizable =
284 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
285 } else {
286 /* setup the defaults */
287 wwin->client_flags.no_titlebar = 0;
288 wwin->client_flags.no_closable = 0;
289 wwin->client_flags.no_miniaturizable = 0;
290 wwin->client_flags.no_resizable = 0;
291 wwin->client_flags.no_close_button = 0;
292 wwin->client_flags.no_miniaturize_button = 0;
293 wwin->client_flags.no_resizebar = 0;
295 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
296 wwin->client_flags.no_appicon = 1;
301 void
302 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
303 WWindowAttributes *mask)
305 if (wflags->no_appicon && mask->no_appicon)
306 wflags->emulate_appicon = 0;
308 if (wwin->main_window!=None) {
309 WApplication *wapp = wApplicationOf(wwin->main_window);
310 if (wapp && !wapp->flags.emulated)
311 wflags->emulate_appicon = 0;
314 if (wwin->transient_for!=None
315 && wwin->transient_for!=wwin->screen_ptr->root_win)
316 wflags->emulate_appicon = 0;
318 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
319 wflags->sunken = 0;
324 void
325 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
327 WScreen *scr = wwin->screen_ptr;
329 /* sets global default stuff */
330 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
331 &wwin->client_flags, NULL, True);
333 * Decoration setting is done in this precedence (lower to higher)
334 * - use global default in the resource database
335 * - guess some settings
336 * - use GNUstep/external window attributes
337 * - set hints specified for the app in the resource DB
340 WSETUFLAG(wwin, broken_close, 0);
342 if (wwin->protocols.DELETE_WINDOW)
343 WSETUFLAG(wwin, kill_close, 0);
344 else
345 WSETUFLAG(wwin, kill_close, 1);
347 /* transients can't be iconified or maximized */
348 if (wwin->transient_for) {
349 WSETUFLAG(wwin, no_miniaturizable, 1);
350 WSETUFLAG(wwin, no_miniaturize_button, 1);
353 /* if the window can't be resized, remove the resizebar */
354 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
355 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
356 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
357 WSETUFLAG(wwin, no_resizable, 1);
358 WSETUFLAG(wwin, no_resizebar, 1);
361 /* set GNUstep window attributes */
362 if (wwin->wm_gnustep_attr) {
363 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
365 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
367 switch (wwin->wm_gnustep_attr->window_level) {
368 case WMNormalWindowLevel:
369 *level = WMNormalLevel;
370 break;
371 case WMFloatingWindowLevel:
372 *level = WMFloatingLevel;
373 break;
374 case WMDockWindowLevel:
375 *level = WMDockLevel;
376 break;
377 case WMSubmenuWindowLevel:
378 *level = WMSubmenuLevel;
379 break;
380 case WMMainMenuWindowLevel:
381 *level = WMMainMenuLevel;
382 break;
383 default:
384 *level = WMNormalLevel;
385 break;
387 } else {
388 /* setup defaults */
389 *level = WMNormalLevel;
391 } else {
392 int tmp_workspace = -1;
393 int tmp_level = -1;
395 #ifdef MWM_HINTS
396 wMWMCheckClientHints(wwin);
397 #endif /* MWM_HINTS */
399 #ifdef KWM_HINTS
400 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
401 #endif /* KWM_HINTS */
403 #ifdef GNOME_STUFF
404 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
405 #endif /* GNOME_STUFF */
407 #ifdef OLWM_HINTS
408 wOLWMCheckClientHints(wwin);
409 #endif /* OLWM_HINTS */
411 if (tmp_level < 0) {
412 if (WFLAGP(wwin, floating))
413 *level = WMFloatingLevel;
414 else if (WFLAGP(wwin, sunken))
415 *level = WMSunkenLevel;
416 else
417 *level = WMNormalLevel;
418 } else {
419 *level = tmp_level;
422 if (tmp_workspace >= 0) {
423 *workspace = tmp_workspace % scr->workspace_count;
428 * Set attributes specified only for that window/class.
429 * This might do duplicate work with the 1st wDefaultFillAttributes().
431 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
432 &wwin->user_flags, &wwin->defined_user_flags,
433 False);
435 * Sanity checks for attributes that depend on other attributes
437 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
438 wwin->user_flags.emulate_appicon = 0;
440 if (wwin->main_window!=None) {
441 WApplication *wapp = wApplicationOf(wwin->main_window);
442 if (wapp && !wapp->flags.emulated)
443 wwin->user_flags.emulate_appicon = 0;
446 if (wwin->transient_for!=None
447 && wwin->transient_for!=wwin->screen_ptr->root_win)
448 wwin->user_flags.emulate_appicon = 0;
450 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
451 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
452 wwin->user_flags.sunken = 0;
454 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
460 Bool
461 wWindowCanReceiveFocus(WWindow *wwin)
463 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
464 return False;
465 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
466 return False;
467 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
468 return False;
470 return True;
474 Bool
475 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
477 int w1, h1, w2, h2;
479 w1 = wwin->frame->core->width;
480 h1 = wwin->frame->core->height;
481 w2 = obscured->frame->core->width;
482 h2 = obscured->frame->core->height;
484 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
485 && wwin->frame->workspace != obscured->frame->workspace)
486 return False;
488 if (wwin->frame_x + w1 < obscured->frame_x
489 || wwin->frame_y + h1 < obscured->frame_y
490 || wwin->frame_x > obscured->frame_x + w2
491 || wwin->frame_y > obscured->frame_y + h2) {
492 return False;
495 return True;
500 *----------------------------------------------------------------
501 * wManageWindow--
502 * reparents the window and allocates a descriptor for it.
503 * Window manager hints and other hints are fetched to configure
504 * the window decoration attributes and others. User preferences
505 * for the window are used if available, to configure window
506 * decorations and some behaviour.
507 * If in startup, windows that are override redirect,
508 * unmapped and never were managed and are Withdrawn are not
509 * managed.
511 * Returns:
512 * the new window descriptor
514 * Side effects:
515 * The window is reparented and appropriate notification
516 * is done to the client. Input mask for the window is setup.
517 * The window descriptor is also associated with various window
518 * contexts and inserted in the head of the window list.
519 * Event handler contexts are associated for some objects
520 * (buttons, titlebar and resizebar)
522 *----------------------------------------------------------------
524 WWindow*
525 wManageWindow(WScreen *scr, Window window)
527 WWindow *wwin;
528 int x, y;
529 unsigned width, height;
530 XWindowAttributes wattribs;
531 XSetWindowAttributes attribs;
532 WWindowState *win_state;
533 WWindow *transientOwner = NULL;
534 int window_level;
535 int wm_state;
536 int foo;
537 int workspace = -1;
538 char *title;
539 Bool withdraw = False;
541 /* mutex. */
542 /* XGrabServer(dpy); */
543 XSync(dpy, False);
544 /* make sure the window is still there */
545 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
546 XUngrabServer(dpy);
547 return NULL;
550 /* if it's an override-redirect, ignore it */
551 if (wattribs.override_redirect) {
552 XUngrabServer(dpy);
553 return NULL;
556 wm_state = PropGetWindowState(window);
558 /* if it's startup and the window is unmapped, don't manage it */
559 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
560 XUngrabServer(dpy);
561 return NULL;
564 if (!wFetchName(dpy, window, &title)) {
565 title = NULL;
568 #ifdef KWM_HINTS
569 if (title && !wKWMManageableClient(scr, window, title)) {
570 XFree(title);
571 XUngrabServer(dpy);
572 return NULL;
574 #endif /* KWM_HINTS */
577 wwin = wWindowCreate();
579 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
581 #ifdef DEBUG
582 printf("managing window %x\n", (unsigned)window);
583 #endif
585 #ifdef SHAPE
586 if (wShapeSupported) {
587 int junk;
588 unsigned int ujunk;
589 int b_shaped;
591 XShapeSelectInput(dpy, window, ShapeNotifyMask);
592 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
593 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
594 wwin->flags.shaped = b_shaped;
596 #endif
599 *--------------------------------------------------
601 * Get hints and other information in properties
603 *--------------------------------------------------
605 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
607 /* setup descriptor */
608 wwin->client_win = window;
609 wwin->screen_ptr = scr;
611 wwin->old_border_width = wattribs.border_width;
613 wwin->event_mask = CLIENT_EVENTS;
614 attribs.event_mask = CLIENT_EVENTS;
615 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
616 attribs.save_under = False;
617 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
618 |CWSaveUnder, &attribs);
619 XSetWindowBorderWidth(dpy, window, 0);
621 /* get hints from GNUstep app */
622 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
623 wwin->wm_gnustep_attr = NULL;
626 wwin->client_leader = PropGetClientLeader(window);
627 if (wwin->client_leader!=None)
628 wwin->main_window = wwin->client_leader;
630 wwin->wm_hints = XGetWMHints(dpy, window);
632 if (wwin->wm_hints) {
633 if (wwin->wm_hints->flags & StateHint) {
635 if (wwin->wm_hints->initial_state == IconicState) {
637 wwin->flags.miniaturized = 1;
639 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
641 withdraw = True;
645 if (wwin->wm_hints->flags & WindowGroupHint) {
646 wwin->group_id = wwin->wm_hints->window_group;
647 /* window_group has priority over CLIENT_LEADER */
648 wwin->main_window = wwin->group_id;
649 } else {
650 wwin->group_id = None;
653 if (wwin->wm_hints->flags & UrgencyHint)
654 wwin->flags.urgent = 1;
655 } else {
656 wwin->group_id = None;
659 PropGetProtocols(window, &wwin->protocols);
661 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
662 wwin->transient_for = None;
663 } else {
664 if (wwin->transient_for==None || wwin->transient_for==window) {
665 wwin->transient_for = scr->root_win;
666 } else {
667 transientOwner = wWindowFor(wwin->transient_for);
668 if (transientOwner && transientOwner->main_window!=None) {
669 wwin->main_window = transientOwner->main_window;
670 } /*else {
671 wwin->main_window = None;
676 /* guess the focus mode */
677 wwin->focus_mode = getFocusMode(wwin);
679 /* get geometry stuff */
680 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
682 /* get colormap windows */
683 GetColormapWindows(wwin);
686 *--------------------------------------------------
688 * Setup the decoration/window attributes and
689 * geometry
691 *--------------------------------------------------
694 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
696 #ifdef OLWM_HINTS
697 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
698 && wwin->group_id != None && wwin->group_id != window) {
700 transientOwner = wWindowFor(wwin->group_id);
702 if (transientOwner) {
703 wwin->transient_for = wwin->group_id;
705 /* transients can't be iconified or maximized */
706 if (wwin->transient_for) {
707 WSETUFLAG(wwin, no_miniaturizable, 1);
708 WSETUFLAG(wwin, no_miniaturize_button, 1);
712 #endif /* OLWM_HINTS */
715 * Make broken apps behave as a nice app.
717 if (WFLAGP(wwin, emulate_appicon)) {
718 wwin->main_window = wwin->client_win;
722 *------------------------------------------------------------
724 * Setup the initial state of the window
726 *------------------------------------------------------------
729 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
730 wwin->flags.miniaturized = 1;
733 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
734 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
737 #ifdef GNOME_STUFF
738 wGNOMECheckInitialClientState(wwin);
739 #endif
740 #ifdef KWM_HINTS
741 wKWMCheckClientInitialState(wwin);
742 #endif
744 /* apply previous state if it exists and we're in startup */
745 if (scr->flags.startup && wm_state >= 0) {
747 if (wm_state == IconicState) {
749 wwin->flags.miniaturized = 1;
751 } else if (wm_state == WithdrawnState) {
753 withdraw = True;
757 /* if there is a saved state (from file), restore it */
758 win_state = NULL;
759 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
760 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
761 } else {
762 win_state = (WWindowState*)wWindowGetSavedState(window);
764 if (win_state && !withdraw) {
766 if (win_state->state->hidden>0)
767 wwin->flags.hidden = win_state->state->hidden;
769 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
770 wwin->flags.shaded = win_state->state->shaded;
772 if (win_state->state->miniaturized>0 &&
773 !WFLAGP(wwin, no_miniaturizable)) {
774 wwin->flags.miniaturized = win_state->state->miniaturized;
776 if (!IS_OMNIPRESENT(wwin)) {
777 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
778 wwin->wm_class);
779 if (w < 0 || w >= scr->workspace_count) {
780 workspace = win_state->state->workspace;
781 if (workspace >= scr->workspace_count)
782 workspace = scr->current_workspace;
783 } else {
784 workspace = w;
786 } else {
787 workspace = scr->current_workspace;
791 /* if we're restarting, restore saved state (from hints).
792 * This will overwrite previous */
794 WSavedState *wstate;
796 if (getSavedState(window, &wstate)) {
797 wwin->flags.shaded = wstate->shaded;
798 wwin->flags.hidden = wstate->hidden;
799 wwin->flags.miniaturized = wstate->miniaturized;
800 workspace = wstate->workspace;
802 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
803 int i;
805 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
806 if (wstate->window_shortcuts & (1<<i)) {
807 if (!scr->shortcutWindows[i])
808 scr->shortcutWindows[i] = WMCreateBag(4);
810 WMPutInBag(scr->shortcutWindows[i], wwin);
814 free(wstate);
818 /* don't let transients start miniaturized if their owners are not */
819 if (transientOwner && !transientOwner->flags.miniaturized
820 && wwin->flags.miniaturized && !withdraw) {
821 wwin->flags.miniaturized = 0;
822 if (wwin->wm_hints)
823 wwin->wm_hints->initial_state = NormalState;
826 /* set workspace on which the window starts */
827 if (workspace >= 0) {
828 if (workspace > scr->workspace_count-1) {
829 workspace = workspace % scr->workspace_count;
831 } else {
832 int w;
834 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
836 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
838 workspace = w;
840 } else {
841 if (wPreferences.open_transients_with_parent && transientOwner) {
843 workspace = transientOwner->frame->workspace;
845 } else {
847 workspace = scr->current_workspace;
852 /* setup window geometry */
853 if (win_state && win_state->state->use_geometry) {
854 width = win_state->state->w;
855 height = win_state->state->h;
857 wWindowConstrainSize(wwin, &width, &height);
859 /* do not ask for window placement if the window is
860 * transient, during startup, if the initial workspace is another one
861 * or if the window wants to start iconic.
862 * If geometry was saved, restore it. */
864 Bool dontBring = False;
866 if (win_state && win_state->state->use_geometry) {
867 x = win_state->state->x;
868 y = win_state->state->y;
869 } else if ((wwin->transient_for==None
870 || wPreferences.window_placement!=WPM_MANUAL)
871 && !scr->flags.startup
872 && workspace == scr->current_workspace
873 && !wwin->flags.miniaturized
874 && !wwin->flags.maximized
875 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
876 PlaceWindow(wwin, &x, &y, width, height);
877 if (wPreferences.window_placement == WPM_MANUAL)
878 dontBring = True;
881 if (WFLAGP(wwin, dont_move_off) && dontBring)
882 wScreenBringInside(scr, &x, &y, width, height);
885 if (wwin->flags.urgent) {
886 if (!IS_OMNIPRESENT(wwin))
887 wwin->flags.omnipresent ^= 1;
891 *--------------------------------------------------
893 * Create frame, borders and do reparenting
895 *--------------------------------------------------
897 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
898 #ifdef XKB_BUTTON_HINT
899 if (wPreferences.modelock)
900 foo |= WFF_LANGUAGE_BUTTON;
901 #endif
902 if (!WFLAGP(wwin, no_titlebar))
903 foo |= WFF_TITLEBAR;
904 if (!WFLAGP(wwin, no_resizebar))
905 foo |= WFF_RESIZEBAR;
907 wwin->frame = wFrameWindowCreate(scr, window_level,
908 x, y, width, height, foo,
909 scr->window_title_texture,
910 scr->resizebar_texture,
911 scr->window_title_pixel,
912 #ifdef DRAWSTRING_PLUGIN
913 W_STRING_FTITLE,
914 #endif
915 &scr->window_title_gc,
916 &scr->title_font);
918 wwin->frame->flags.is_client_window_frame = 1;
919 wwin->frame->flags.justification = wPreferences.title_justification;
921 /* setup button images */
922 wWindowUpdateButtonImages(wwin);
924 /* hide unused buttons */
925 foo = 0;
926 if (WFLAGP(wwin, no_close_button))
927 foo |= WFF_RIGHT_BUTTON;
928 if (WFLAGP(wwin, no_miniaturize_button))
929 foo |= WFF_LEFT_BUTTON;
930 #ifdef XKB_BUTTON_HINT
931 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
932 foo |= WFF_LANGUAGE_BUTTON;
933 #endif
934 if (foo!=0)
935 wFrameWindowHideButton(wwin->frame, foo);
937 wwin->frame->child = wwin;
939 #ifdef OLWM_HINTS
940 /* emulate olwm push pin. Make the button look as pushed-in for
941 * the pinned-out state. When the button is clicked, it will
942 * revert to the normal position, which means the pin is pinned-in.
944 if (wwin->flags.olwm_push_pin_out)
945 wFrameWindowUpdatePushButton(wwin->frame, True);
946 #endif /* OLWM_HINTS */
948 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
949 if (title)
950 XFree(title);
952 wwin->frame->workspace = workspace;
954 wwin->frame->on_click_left = windowIconifyClick;
955 #ifdef XKB_BUTTON_HINT
956 if (wPreferences.modelock)
957 wwin->frame->on_click_language = windowLanguageClick;
958 #endif
960 wwin->frame->on_click_right = windowCloseClick;
961 wwin->frame->on_dblclick_right = windowCloseDblClick;
963 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
964 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
966 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
969 XSelectInput(dpy, wwin->client_win,
970 wwin->event_mask & ~StructureNotifyMask);
972 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
973 0, wwin->frame->top_width);
975 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
979 int gx, gy;
981 wClientGetGravityOffsets(wwin, &gx, &gy);
983 /* if gravity is to the south, account for the border sizes */
984 if (gy > 0)
985 y -= wwin->frame->top_width + wwin->frame->bottom_width;
989 * wWindowConfigure() will init the client window's size
990 * (wwin->client.{width,height}) and all other geometry
991 * related variables (frame_x,frame_y)
993 wWindowConfigure(wwin, x, y, width, height);
995 /* to make sure the window receives it's new position after reparenting */
996 wWindowSynthConfigureNotify(wwin);
999 *--------------------------------------------------
1001 * Setup descriptors and save window to internal
1002 * lists
1004 *--------------------------------------------------
1007 if (wwin->main_window!=None) {
1008 WApplication *app;
1009 WWindow *leader;
1011 /* Leader windows do not necessary set themselves as leaders.
1012 * If this is the case, point the leader of this window to
1013 * itself */
1014 leader = wWindowFor(wwin->main_window);
1015 if (leader && leader->main_window==None) {
1016 leader->main_window = leader->client_win;
1018 app = wApplicationCreate(scr, wwin->main_window);
1019 if (app) {
1020 app->last_workspace = workspace;
1023 * Do application specific stuff, like setting application
1024 * wide attributes.
1027 if (wwin->flags.hidden) {
1028 /* if the window was set to hidden because it was hidden
1029 * in a previous incarnation and that state was restored */
1030 app->flags.hidden = 1;
1033 if (app->flags.hidden) {
1034 wwin->flags.hidden = 1;
1039 /* setup the frame descriptor */
1040 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1041 wwin->frame->core->descriptor.parent = wwin;
1042 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1044 /* don't let windows go away if we die */
1045 XAddToSaveSet(dpy, window);
1047 XLowerWindow(dpy, window);
1049 /* if window is in this workspace and should be mapped, then map it */
1050 if (!wwin->flags.miniaturized
1051 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1052 && !wwin->flags.hidden && !withdraw) {
1054 /* The following "if" is to avoid crashing of clients that expect
1055 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1056 * after the return from this function.
1058 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1059 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1060 } else {
1061 wClientSetState(wwin, NormalState, None);
1064 #if 0
1065 /* if not auto focus, then map the window under the currently
1066 * focused window */
1067 #define _WIDTH(w) (w)->frame->core->width
1068 #define _HEIGHT(w) (w)->frame->core->height
1069 if (!wPreferences.auto_focus && scr->focused_window
1070 && !scr->flags.startup && !transientOwner
1071 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1072 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1073 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1074 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1075 || wwin->flags.maximized)) {
1076 MoveInStackListUnder(scr->focused_window->frame->core,
1077 wwin->frame->core);
1079 #undef _WIDTH
1080 #undef _HEIGHT
1082 #endif
1084 if (wPreferences.superfluous && !wPreferences.no_animations
1085 && !scr->flags.startup && wwin->transient_for==None
1087 * The brain damaged idiotic non-click to focus modes will
1088 * have trouble with this because:
1090 * 1. window is created and mapped by the client
1091 * 2. window is mapped by wmaker in small size
1092 * 3. window is animated to grow to normal size
1093 * 4. this function returns to normal event loop
1094 * 5. eventually, the EnterNotify event that would trigger
1095 * the window focusing (if the mouse is over that window)
1096 * will be processed by wmaker.
1097 * But since this event will be rather delayed
1098 * (step 3 has a large delay) the time when the event ocurred
1099 * and when it is processed, the client that owns that window
1100 * will reject the XSetInputFocus() for it.
1102 && (wPreferences.focus_mode==WKF_CLICK
1103 || wPreferences.auto_focus)) {
1104 DoWindowBirth(wwin);
1107 wWindowMap(wwin);
1110 /* setup stacking descriptor */
1111 if (transientOwner) {
1112 /* && wPreferences.on_top_transients */
1113 if (transientOwner) {
1114 wwin->frame->core->stacking->child_of =
1115 transientOwner->frame->core;
1117 } else {
1118 wwin->frame->core->stacking->child_of = NULL;
1122 if (!scr->focused_window) {
1123 /* first window on the list */
1124 wwin->next = NULL;
1125 wwin->prev = NULL;
1126 scr->focused_window = wwin;
1127 } else {
1128 WWindow *tmp;
1130 /* add window at beginning of focus window list */
1131 tmp = scr->focused_window;
1132 while (tmp->prev)
1133 tmp = tmp->prev;
1134 tmp->prev = wwin;
1135 wwin->next = tmp;
1136 wwin->prev = NULL;
1139 #ifdef GNOME_STUFF
1140 wGNOMEUpdateClientStateHint(wwin, True);
1141 #endif
1142 #ifdef KWM_HINTS
1143 wKWMUpdateClientWorkspace(wwin);
1144 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1145 #endif
1147 XUngrabServer(dpy);
1150 *--------------------------------------------------
1152 * Final preparations before window is ready to go
1154 *--------------------------------------------------
1157 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1160 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1161 && !wwin->flags.hidden) {
1162 if (((transientOwner && transientOwner->flags.focused)
1163 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1164 wSetFocusTo(scr, wwin);
1166 wWindowResetMouseGrabs(wwin);
1168 if (!WFLAGP(wwin, no_bind_keys)) {
1169 wWindowSetKeyGrabs(wwin);
1171 #ifdef GNOME_STUFF
1172 wGNOMEUpdateClientListHint(scr);
1173 #endif
1174 #ifdef KWM_HINTS
1175 wwin->flags.kwm_managed = 1;
1177 wKWMSendEventMessage(wwin, WKWMAddWindow);
1178 #endif
1180 wColormapInstallForWindow(scr, scr->cmap_window);
1182 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1184 #ifdef OLWM_HINTS
1185 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1186 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1188 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1189 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1190 wwin->frame_y);
1192 #endif
1195 *------------------------------------------------------------
1196 * Setup Notification Observers
1197 *------------------------------------------------------------
1199 WMAddNotificationObserver(appearanceObserver, wwin,
1200 WNWindowAppearanceSettingsChanged, wwin);
1204 *--------------------------------------------------
1206 * Cleanup temporary stuff
1208 *--------------------------------------------------
1211 if (win_state)
1212 wWindowDeleteSavedState(win_state);
1214 /* If the window must be withdrawed, then do it now.
1215 * Must do some optimization, 'though */
1216 if (withdraw) {
1217 wwin->flags.mapped = 0;
1218 wClientSetState(wwin, WithdrawnState, None);
1219 wUnmanageWindow(wwin, True, False);
1220 wwin = NULL;
1223 return wwin;
1230 WWindow*
1231 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1232 char *title, int x, int y, int width, int height)
1234 WWindow *wwin;
1235 int foo;
1237 wwin = wWindowCreate();
1239 WMAddNotificationObserver(appearanceObserver, wwin,
1240 WNWindowAppearanceSettingsChanged, wwin);
1242 wwin->flags.internal_window = 1;
1244 WSETUFLAG(wwin, omnipresent, 1);
1245 WSETUFLAG(wwin, no_shadeable, 1);
1246 WSETUFLAG(wwin, no_resizable, 1);
1247 WSETUFLAG(wwin, no_miniaturizable, 1);
1249 wwin->focus_mode = WFM_PASSIVE;
1251 wwin->client_win = window;
1252 wwin->screen_ptr = scr;
1254 wwin->transient_for = owner;
1256 wwin->client.x = x;
1257 wwin->client.y = y;
1258 wwin->client.width = width;
1259 wwin->client.height = height;
1261 wwin->frame_x = wwin->client.x;
1262 wwin->frame_y = wwin->client.y;
1265 foo = WFF_RIGHT_BUTTON;
1266 foo |= WFF_TITLEBAR;
1267 #ifdef XKB_BUTTON_HINT
1268 foo |= WFF_LANGUAGE_BUTTON;
1269 #endif
1271 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1272 wwin->frame_x, wwin->frame_y,
1273 width, height, foo,
1274 scr->window_title_texture,
1275 scr->resizebar_texture,
1276 scr->window_title_pixel,
1277 #ifdef DRAWSTRING_PLUGIN
1278 W_STRING_FTITLE,
1279 #endif
1280 &scr->window_title_gc,
1281 &scr->title_font);
1283 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1285 wwin->frame->flags.is_client_window_frame = 1;
1286 wwin->frame->flags.justification = wPreferences.title_justification;
1288 wFrameWindowChangeTitle(wwin->frame, title);
1290 /* setup button images */
1291 wWindowUpdateButtonImages(wwin);
1293 /* hide buttons */
1294 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1296 wwin->frame->child = wwin;
1298 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1300 #ifdef XKB_BUTTON_HINT
1301 if (wPreferences.modelock)
1302 wwin->frame->on_click_language = windowLanguageClick;
1303 #endif
1305 wwin->frame->on_click_right = windowCloseClick;
1307 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1308 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1310 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1312 wwin->client.y += wwin->frame->top_width;
1313 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1314 0, wwin->frame->top_width);
1316 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1317 wwin->client.width, wwin->client.height);
1319 /* setup the frame descriptor */
1320 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1321 wwin->frame->core->descriptor.parent = wwin;
1322 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1325 XLowerWindow(dpy, window);
1326 XMapSubwindows(dpy, wwin->frame->core->window);
1328 /* setup stacking descriptor */
1329 if (
1330 #ifdef removed
1331 wPreferences.on_top_transients &&
1332 #endif
1333 wwin->transient_for!=None
1334 && wwin->transient_for!=scr->root_win) {
1335 WWindow *tmp;
1336 tmp = wWindowFor(wwin->transient_for);
1337 if (tmp)
1338 wwin->frame->core->stacking->child_of = tmp->frame->core;
1339 } else {
1340 wwin->frame->core->stacking->child_of = NULL;
1344 if (!scr->focused_window) {
1345 /* first window on the list */
1346 wwin->next = NULL;
1347 wwin->prev = NULL;
1348 scr->focused_window = wwin;
1349 } else {
1350 WWindow *tmp;
1352 /* add window at beginning of focus window list */
1353 tmp = scr->focused_window;
1354 while (tmp->prev)
1355 tmp = tmp->prev;
1356 tmp->prev = wwin;
1357 wwin->next = tmp;
1358 wwin->prev = NULL;
1361 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1363 /* if (wPreferences.auto_focus)*/
1364 wSetFocusTo(scr, wwin);
1366 wWindowResetMouseGrabs(wwin);
1368 wWindowSetKeyGrabs(wwin);
1370 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1372 return wwin;
1377 *----------------------------------------------------------------------
1378 * wUnmanageWindow--
1379 * Removes the frame window from a window and destroys all data
1380 * related to it. The window will be reparented back to the root window
1381 * if restore is True.
1383 * Side effects:
1384 * Everything related to the window is destroyed and the window
1385 * is removed from the window lists. Focus is set to the previous on the
1386 * window list.
1387 *----------------------------------------------------------------------
1389 void
1390 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1392 WCoreWindow *frame = wwin->frame->core;
1393 WWindow *owner = NULL;
1394 WWindow *newFocusedWindow = NULL;
1395 int wasFocused;
1396 WScreen *scr = wwin->screen_ptr;
1399 #ifdef KWM_HINTS
1400 wwin->frame->workspace = -1;
1402 wKWMUpdateClientWorkspace(wwin);
1403 #endif
1405 /* First close attribute editor window if open */
1406 if (wwin->flags.inspector_open) {
1407 WWindow *pwin = wwin->inspector->frame; /* the inspector window */
1408 (*pwin->frame->on_click_right)(NULL, pwin, NULL);
1411 /* Close window menu if it's open for this window */
1412 if (wwin->flags.menu_open_for_me) {
1413 CloseWindowMenu(scr);
1416 if (!destroyed) {
1417 if (!wwin->flags.internal_window)
1418 XRemoveFromSaveSet(dpy, wwin->client_win);
1420 XSelectInput(dpy, wwin->client_win, NoEventMask);
1422 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1423 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1426 XUnmapWindow(dpy, frame->window);
1428 XUnmapWindow(dpy, wwin->client_win);
1430 /* deselect window */
1431 wSelectWindow(wwin, False);
1433 /* remove all pending events on window */
1434 /* I think this only matters for autoraise */
1435 if (wPreferences.raise_delay)
1436 WMDeleteTimerWithClientData(wwin->frame->core);
1438 XFlush(dpy);
1440 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1442 /* reparent the window back to the root */
1443 if (restore)
1444 wClientRestore(wwin);
1446 if (wwin->transient_for!=scr->root_win) {
1447 owner = wWindowFor(wwin->transient_for);
1448 if (owner) {
1449 if (!owner->flags.semi_focused) {
1450 owner = NULL;
1451 } else {
1452 owner->flags.semi_focused = 0;
1457 wasFocused = wwin->flags.focused;
1459 /* remove from window focus list */
1460 if (!wwin->prev && !wwin->next) {
1461 /* was the only window */
1462 scr->focused_window = NULL;
1463 newFocusedWindow = NULL;
1464 } else {
1465 WWindow *tmp;
1467 if (wwin->prev)
1468 wwin->prev->next = wwin->next;
1469 if (wwin->next)
1470 wwin->next->prev = wwin->prev;
1471 else {
1472 scr->focused_window = wwin->prev;
1473 scr->focused_window->next = NULL;
1476 if (wPreferences.focus_mode==WKF_CLICK) {
1478 /* if in click to focus mode and the window
1479 * was a transient, focus the owner window
1481 tmp = NULL;
1482 if (wPreferences.focus_mode==WKF_CLICK) {
1483 tmp = wWindowFor(wwin->transient_for);
1484 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1485 tmp = NULL;
1488 /* otherwise, focus the next one in the focus list */
1489 if (!tmp) {
1490 tmp = scr->focused_window;
1491 while (tmp) { /* look for one in the window list first */
1492 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1493 && (tmp->flags.mapped || tmp->flags.shaded))
1494 break;
1495 tmp = tmp->prev;
1497 if (!tmp) { /* if unsuccessful, choose any focusable window */
1498 tmp = scr->focused_window;
1499 while (tmp) {
1500 if (!WFLAGP(tmp, no_focusable)
1501 && (tmp->flags.mapped || tmp->flags.shaded))
1502 break;
1503 tmp = tmp->prev;
1508 newFocusedWindow = tmp;
1510 } else if (wPreferences.focus_mode==WKF_SLOPPY
1511 || wPreferences.focus_mode==WKF_POINTER) {
1512 unsigned int mask;
1513 int foo;
1514 Window bar, win;
1516 /* This is to let the root window get the keyboard input
1517 * if Sloppy focus mode and no other window get focus.
1518 * This way keybindings will not freeze.
1520 tmp = NULL;
1521 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1522 &foo, &foo, &foo, &foo, &mask))
1523 tmp = wWindowFor(win);
1524 if (tmp == wwin)
1525 tmp = NULL;
1526 newFocusedWindow = tmp;
1527 } else {
1528 newFocusedWindow = NULL;
1532 if (!wwin->flags.internal_window) {
1533 #ifdef GNOME_STUFF
1534 wGNOMERemoveClient(wwin);
1535 #endif
1536 #ifdef KWM_HINTS
1537 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1538 #endif
1541 #ifdef DEBUG
1542 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1543 (unsigned)frame->window);
1544 #endif
1546 if (wasFocused) {
1547 if (newFocusedWindow != owner && owner) {
1548 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1550 wSetFocusTo(scr, newFocusedWindow);
1552 wWindowDestroy(wwin);
1553 XFlush(dpy);
1557 void
1558 wWindowMap(WWindow *wwin)
1560 XMapWindow(dpy, wwin->frame->core->window);
1561 if (!wwin->flags.shaded) {
1562 /* window will be remapped when getting MapNotify */
1563 XSelectInput(dpy, wwin->client_win,
1564 wwin->event_mask & ~StructureNotifyMask);
1565 XMapWindow(dpy, wwin->client_win);
1566 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1568 wwin->flags.mapped = 1;
1573 void
1574 wWindowUnmap(WWindow *wwin)
1576 wwin->flags.mapped = 0;
1578 /* prevent window withdrawal when getting UnmapNotify */
1579 XSelectInput(dpy, wwin->client_win,
1580 wwin->event_mask & ~StructureNotifyMask);
1581 XUnmapWindow(dpy, wwin->client_win);
1582 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1584 XUnmapWindow(dpy, wwin->frame->core->window);
1589 void
1590 wWindowFocus(WWindow *wwin, WWindow *owin)
1592 WWindow *nowner;
1593 WWindow *oowner;
1595 #ifdef KEEP_XKB_LOCK_STATUS
1596 if (wPreferences.modelock) {
1597 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1599 #endif /* KEEP_XKB_LOCK_STATUS */
1601 wwin->flags.semi_focused = 0;
1603 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1605 wwin->flags.focused = 1;
1607 wWindowResetMouseGrabs(wwin);
1609 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1611 if (owin == wwin || !owin)
1612 return;
1614 nowner = wWindowFor(wwin->transient_for);
1616 /* new window is a transient for the old window */
1617 if (nowner == owin) {
1618 owin->flags.semi_focused = 1;
1619 wWindowUnfocus(nowner);
1620 return;
1623 oowner = wWindowFor(owin->transient_for);
1625 /* new window is owner of old window */
1626 if (wwin == oowner) {
1627 wWindowUnfocus(owin);
1628 return;
1631 if (!nowner) {
1632 wWindowUnfocus(owin);
1633 return;
1636 /* new window has same owner of old window */
1637 if (oowner == nowner) {
1638 /* prevent unfocusing of owner */
1639 oowner->flags.semi_focused = 0;
1640 wWindowUnfocus(owin);
1641 oowner->flags.semi_focused = 1;
1643 return;
1646 /* nowner != NULL && oowner != nowner */
1647 nowner->flags.semi_focused = 1;
1648 wWindowUnfocus(nowner);
1649 wWindowUnfocus(owin);
1653 void
1654 wWindowUnfocus(WWindow *wwin)
1656 CloseWindowMenu(wwin->screen_ptr);
1658 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1659 ? WS_PFOCUSED : WS_UNFOCUSED);
1661 if (wwin->transient_for!=None
1662 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1663 WWindow *owner;
1664 owner = wWindowFor(wwin->transient_for);
1665 if (owner && owner->flags.semi_focused) {
1666 owner->flags.semi_focused = 0;
1667 if (owner->flags.mapped || owner->flags.shaded) {
1668 wWindowUnfocus(owner);
1669 wFrameWindowPaint(owner->frame);
1673 wwin->flags.focused = 0;
1675 wWindowResetMouseGrabs(wwin);
1677 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1684 *----------------------------------------------------------------------
1686 * wWindowConstrainSize--
1687 * Constrains size for the client window, taking the maximal size,
1688 * window resize increments and other size hints into account.
1690 * Returns:
1691 * The closest size to what was given that the client window can
1692 * have.
1694 *----------------------------------------------------------------------
1696 void
1697 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1699 int width = *nwidth;
1700 int height = *nheight;
1701 int winc = 1;
1702 int hinc = 1;
1703 int minW = 1, minH = 1;
1704 int maxW = wwin->screen_ptr->scr_width*2;
1705 int maxH = wwin->screen_ptr->scr_height*2;
1706 int minAX = -1, minAY = -1;
1707 int maxAX = -1, maxAY = -1;
1708 int baseW = 0;
1709 int baseH = 0;
1711 if (wwin->normal_hints) {
1712 winc = wwin->normal_hints->width_inc;
1713 hinc = wwin->normal_hints->height_inc;
1714 minW = wwin->normal_hints->min_width;
1715 minH = wwin->normal_hints->min_height;
1716 maxW = wwin->normal_hints->max_width;
1717 maxH = wwin->normal_hints->max_height;
1718 if (wwin->normal_hints->flags & PAspect) {
1719 minAX = wwin->normal_hints->min_aspect.x;
1720 minAY = wwin->normal_hints->min_aspect.y;
1721 maxAX = wwin->normal_hints->max_aspect.x;
1722 maxAY = wwin->normal_hints->max_aspect.y;
1726 if (width < minW)
1727 width = minW;
1728 if (height < minH)
1729 height = minH;
1731 if (width > maxW)
1732 width = maxW;
1733 if (height > maxH)
1734 height = maxH;
1736 /* aspect ratio code borrowed from olwm */
1737 if (minAX > 0) {
1738 /* adjust max aspect ratio */
1739 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1740 if (maxAX > maxAY) {
1741 height = (width * maxAY) / maxAX;
1742 if (height > maxH) {
1743 height = maxH;
1744 width = (height * maxAX) / maxAY;
1746 } else {
1747 width = (height * maxAX) / maxAY;
1748 if (width > maxW) {
1749 width = maxW;
1750 height = (width * maxAY) / maxAX;
1755 /* adjust min aspect ratio */
1756 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1757 if (minAX > minAY) {
1758 height = (width * minAY) / minAX;
1759 if (height < minH) {
1760 height = minH;
1761 width = (height * minAX) / minAY;
1763 } else {
1764 width = (height * minAX) / minAY;
1765 if (width < minW) {
1766 width = minW;
1767 height = (width * minAY) / minAX;
1773 if (baseW != 0) {
1774 width = (((width - baseW) / winc) * winc) + baseW;
1775 } else {
1776 width = (((width - minW) / winc) * winc) + minW;
1779 if (baseW != 0) {
1780 height = (((height - baseH) / hinc) * hinc) + baseH;
1781 } else {
1782 height = (((height - minH) / hinc) * hinc) + minH;
1785 *nwidth = width;
1786 *nheight = height;
1790 void
1791 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1793 WScreen *scr = wwin->screen_ptr;
1794 WApplication *wapp;
1795 int unmap = 0;
1797 if (workspace >= scr->workspace_count || workspace < 0
1798 || workspace == wwin->frame->workspace)
1799 return;
1801 if (workspace != scr->current_workspace) {
1802 /* Sent to other workspace. Unmap window */
1803 if ((wwin->flags.mapped
1804 || wwin->flags.shaded
1805 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1806 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1808 wapp = wApplicationOf(wwin->main_window);
1809 if (wapp) {
1810 wapp->last_workspace = workspace;
1812 if (wwin->flags.miniaturized) {
1813 if (wwin->icon) {
1814 XUnmapWindow(dpy, wwin->icon->core->window);
1815 wwin->icon->mapped = 0;
1817 } else {
1818 unmap = 1;
1819 wSetFocusTo(scr, NULL);
1822 } else {
1823 /* brought to current workspace. Map window */
1824 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1825 if (wwin->icon) {
1826 XMapWindow(dpy, wwin->icon->core->window);
1827 wwin->icon->mapped = 1;
1829 } else if (!wwin->flags.mapped &&
1830 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1831 wWindowMap(wwin);
1834 if (!IS_OMNIPRESENT(wwin)) {
1835 wwin->frame->workspace = workspace;
1836 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1838 #ifdef GNOME_STUFF
1839 wGNOMEUpdateClientStateHint(wwin, True);
1840 #endif
1841 #ifdef KWM_HINTS
1842 wKWMUpdateClientWorkspace(wwin);
1843 wKWMSendEventMessage(wwin, WKWMChangedClient);
1844 #endif
1845 if (unmap) {
1846 wWindowUnmap(wwin);
1851 void
1852 wWindowSynthConfigureNotify(WWindow *wwin)
1854 XEvent sevent;
1856 sevent.type = ConfigureNotify;
1857 sevent.xconfigure.display = dpy;
1858 sevent.xconfigure.event = wwin->client_win;
1859 sevent.xconfigure.window = wwin->client_win;
1861 sevent.xconfigure.x = wwin->client.x;
1862 sevent.xconfigure.y = wwin->client.y;
1863 sevent.xconfigure.width = wwin->client.width;
1864 sevent.xconfigure.height = wwin->client.height;
1866 sevent.xconfigure.border_width = wwin->old_border_width;
1867 if (WFLAGP(wwin, no_titlebar))
1868 sevent.xconfigure.above = None;
1869 else
1870 sevent.xconfigure.above = wwin->frame->titlebar->window;
1872 sevent.xconfigure.override_redirect = False;
1873 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1874 #ifdef KWM_HINTS
1875 wKWMSendEventMessage(wwin, WKWMChangedClient);
1876 #endif
1877 XFlush(dpy);
1882 *----------------------------------------------------------------------
1883 * wWindowConfigure--
1884 * Configures the frame, decorations and client window to the
1885 * specified geometry. The geometry is not checked for validity,
1886 * wWindowConstrainSize() must be used for that.
1887 * The size parameters are for the client window, but the position is
1888 * for the frame.
1889 * The client window receives a ConfigureNotify event, according
1890 * to what ICCCM says.
1892 * Returns:
1893 * None
1895 * Side effects:
1896 * Window size and position are changed and client window receives
1897 * a ConfigureNotify event.
1898 *----------------------------------------------------------------------
1900 void
1901 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1902 WWindow *wwin;
1903 int req_x, req_y; /* new position of the frame */
1904 int req_width, req_height; /* new size of the client */
1906 int synth_notify = False;
1907 int resize;
1909 resize = (req_width!=wwin->client.width
1910 || req_height!=wwin->client.height);
1912 * if the window is being moved but not resized then
1913 * send a synthetic ConfigureNotify
1915 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1916 synth_notify = True;
1919 if (WFLAGP(wwin, dont_move_off))
1920 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1921 req_width, req_height);
1922 if (resize) {
1923 if (req_width < MIN_WINDOW_SIZE)
1924 req_width = MIN_WINDOW_SIZE;
1925 if (req_height < MIN_WINDOW_SIZE)
1926 req_height = MIN_WINDOW_SIZE;
1928 /* If growing, resize inner part before frame,
1929 * if shrinking, resize frame before.
1930 * This will prevent the frame (that can have a different color)
1931 * to be exposed, causing flicker */
1932 if (req_height > wwin->frame->core->height
1933 || req_width > wwin->frame->core->width)
1934 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1936 if (wwin->flags.shaded) {
1937 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1938 req_width, wwin->frame->core->height);
1939 wwin->old_geometry.height = req_height;
1940 } else {
1941 int h;
1943 h = req_height + wwin->frame->top_width
1944 + wwin->frame->bottom_width;
1946 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1949 if (!(req_height > wwin->frame->core->height
1950 || req_width > wwin->frame->core->width))
1951 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1953 wwin->client.x = req_x;
1954 wwin->client.y = req_y + wwin->frame->top_width;
1955 wwin->client.width = req_width;
1956 wwin->client.height = req_height;
1957 } else {
1958 wwin->client.x = req_x;
1959 wwin->client.y = req_y + wwin->frame->top_width;
1961 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1963 wwin->frame_x = req_x;
1964 wwin->frame_y = req_y;
1966 #ifdef SHAPE
1967 if (wShapeSupported && wwin->flags.shaped && resize) {
1968 wWindowSetShape(wwin);
1970 #endif
1972 if (synth_notify)
1973 wWindowSynthConfigureNotify(wwin);
1974 XFlush(dpy);
1978 void
1979 wWindowMove(wwin, req_x, req_y)
1980 WWindow *wwin;
1981 int req_x, req_y; /* new position of the frame */
1983 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1984 int synth_notify = False;
1986 /* Send a synthetic ConfigureNotify event for every window movement. */
1987 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1988 synth_notify = True;
1990 #else
1991 /* A single synthetic ConfigureNotify event is sent at the end of
1992 * a completed (opaque) movement in moveres.c */
1993 #endif
1995 if (WFLAGP(wwin, dont_move_off))
1996 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1997 wwin->frame->core->width, wwin->frame->core->height);
1999 wwin->client.x = req_x;
2000 wwin->client.y = req_y + wwin->frame->top_width;
2002 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2004 wwin->frame_x = req_x;
2005 wwin->frame_y = req_y;
2007 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2008 if (synth_notify)
2009 wWindowSynthConfigureNotify(wwin);
2010 #endif
2013 void
2014 wWindowUpdateButtonImages(WWindow *wwin)
2016 WScreen *scr = wwin->screen_ptr;
2017 Pixmap pixmap, mask;
2018 WFrameWindow *fwin = wwin->frame;
2020 if (WFLAGP(wwin, no_titlebar))
2021 return;
2023 /* miniaturize button */
2025 if (!WFLAGP(wwin, no_miniaturize_button)) {
2026 if (wwin->wm_gnustep_attr
2027 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2028 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2030 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2031 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2032 } else {
2033 mask = None;
2036 if (fwin->lbutton_image
2037 && (fwin->lbutton_image->image != pixmap
2038 || fwin->lbutton_image->mask != mask)) {
2039 wPixmapDestroy(fwin->lbutton_image);
2040 fwin->lbutton_image = NULL;
2043 if (!fwin->lbutton_image) {
2044 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2045 fwin->lbutton_image->client_owned = 1;
2046 fwin->lbutton_image->client_owned_mask = 1;
2048 } else {
2049 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2050 wPixmapDestroy(fwin->lbutton_image);
2052 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2056 #ifdef XKB_BUTTON_HINT
2057 if (!WFLAGP(wwin, no_language_button)) {
2058 if (fwin->languagebutton_image &&
2059 !fwin->languagebutton_image->shared) {
2060 wPixmapDestroy(fwin->languagebutton_image);
2062 fwin->languagebutton_image =
2063 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2065 #endif
2067 /* close button */
2069 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2070 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2072 if (!WFLAGP(wwin, no_close_button)) {
2073 if (wwin->wm_gnustep_attr
2074 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2075 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2077 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2078 mask = wwin->wm_gnustep_attr->close_mask;
2079 else
2080 mask = None;
2082 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2083 || fwin->rbutton_image->mask != mask)) {
2084 wPixmapDestroy(fwin->rbutton_image);
2085 fwin->rbutton_image = NULL;
2088 if (!fwin->rbutton_image) {
2089 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2090 fwin->rbutton_image->client_owned = 1;
2091 fwin->rbutton_image->client_owned_mask = 1;
2094 } else if (WFLAGP(wwin, kill_close)) {
2096 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2097 wPixmapDestroy(fwin->rbutton_image);
2099 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2101 } else if (MGFLAGP(wwin, broken_close)) {
2103 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2104 wPixmapDestroy(fwin->rbutton_image);
2106 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2108 } else {
2110 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2111 wPixmapDestroy(fwin->rbutton_image);
2113 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2117 /* force buttons to be redrawn */
2118 fwin->flags.need_texture_change = 1;
2119 wFrameWindowPaint(fwin);
2124 *---------------------------------------------------------------------------
2125 * wWindowConfigureBorders--
2126 * Update window border configuration according to attribute flags.
2128 *---------------------------------------------------------------------------
2130 void
2131 wWindowConfigureBorders(WWindow *wwin)
2133 if (wwin->frame) {
2134 int flags;
2135 int newy, oldh;
2137 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2138 #ifdef XKB_BUTTON_HINT
2139 flags |= WFF_LANGUAGE_BUTTON;
2140 #endif
2141 if (!WFLAGP(wwin, no_titlebar))
2142 flags |= WFF_TITLEBAR;
2143 if (!WFLAGP(wwin, no_resizebar))
2144 flags |= WFF_RESIZEBAR;
2145 if (wwin->flags.shaded)
2146 flags |= WFF_IS_SHADED;
2148 oldh = wwin->frame->top_width;
2149 wFrameWindowUpdateBorders(wwin->frame, flags);
2150 if (oldh != wwin->frame->top_width) {
2151 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2153 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2154 wWindowConfigure(wwin, wwin->frame_x, newy,
2155 wwin->client.width, wwin->client.height);
2158 flags = 0;
2159 if (!WFLAGP(wwin, no_miniaturize_button)
2160 && wwin->frame->flags.hide_left_button)
2161 flags |= WFF_LEFT_BUTTON;
2163 #ifdef XKB_BUTTON_HINT
2164 if (!WFLAGP(wwin, no_language_button)
2165 && wwin->frame->flags.hide_language_button)
2166 flags |= WFF_LANGUAGE_BUTTON;
2167 #endif
2169 if (!WFLAGP(wwin, no_close_button)
2170 && wwin->frame->flags.hide_right_button)
2171 flags |= WFF_RIGHT_BUTTON;
2173 if (flags!=0) {
2174 wWindowUpdateButtonImages(wwin);
2175 wFrameWindowShowButton(wwin->frame, flags);
2178 flags = 0;
2179 if (WFLAGP(wwin, no_miniaturize_button)
2180 && !wwin->frame->flags.hide_left_button)
2181 flags |= WFF_LEFT_BUTTON;
2183 #ifdef XKB_BUTTON_HINT
2184 if (WFLAGP(wwin, no_language_button)
2185 && !wwin->frame->flags.hide_language_button)
2186 flags |= WFF_LANGUAGE_BUTTON;
2187 #endif
2189 if (WFLAGP(wwin, no_close_button)
2190 && !wwin->frame->flags.hide_right_button)
2191 flags |= WFF_RIGHT_BUTTON;
2193 if (flags!=0)
2194 wFrameWindowHideButton(wwin->frame, flags);
2196 #ifdef SHAPE
2197 if (wShapeSupported && wwin->flags.shaped) {
2198 wWindowSetShape(wwin);
2200 #endif
2205 void
2206 wWindowSaveState(WWindow *wwin)
2208 CARD32 data[10];
2209 int i;
2211 memset(data, 0, sizeof(CARD32)*10);
2212 data[0] = wwin->frame->workspace;
2213 data[1] = wwin->flags.miniaturized;
2214 data[2] = wwin->flags.shaded;
2215 data[3] = wwin->flags.hidden;
2217 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2218 if (wwin->screen_ptr->shortcutWindows[i] &&
2219 WMCountInBag(wwin->screen_ptr->shortcutWindows[i], wwin))
2220 data[9] |= 1<<i;
2222 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2223 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2224 (unsigned char *)data, 10);
2228 static int
2229 getSavedState(Window window, WSavedState **state)
2231 Atom type_ret;
2232 int fmt_ret;
2233 unsigned long nitems_ret;
2234 unsigned long bytes_after_ret;
2235 CARD32 *data;
2237 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2238 True, _XA_WINDOWMAKER_STATE,
2239 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2240 (unsigned char **)&data)!=Success || !data)
2241 return 0;
2243 *state = malloc(sizeof(WSavedState));
2245 if (*state) {
2246 (*state)->workspace = data[0];
2247 (*state)->miniaturized = data[1];
2248 (*state)->shaded = data[2];
2249 (*state)->hidden = data[3];
2250 (*state)->use_geometry = data[4];
2251 (*state)->x = data[5];
2252 (*state)->y = data[6];
2253 (*state)->w = data[7];
2254 (*state)->h = data[8];
2255 (*state)->window_shortcuts = data[9];
2257 XFree(data);
2259 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2260 return 1;
2261 else
2262 return 0;
2266 #ifdef SHAPE
2267 void
2268 wWindowClearShape(WWindow *wwin)
2270 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2271 0, wwin->frame->top_width, None, ShapeSet);
2272 XFlush(dpy);
2275 void
2276 wWindowSetShape(WWindow *wwin)
2278 XRectangle rect[2];
2279 int count;
2280 #ifdef OPTIMIZE_SHAPE
2281 XRectangle *rects;
2282 XRectangle *urec;
2283 int ordering;
2285 /* only shape is the client's */
2286 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2287 goto alt_code;
2290 /* Get array of rectangles describing the shape mask */
2291 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2292 &count, &ordering);
2293 if (!rects) {
2294 goto alt_code;
2297 urec = malloc(sizeof(XRectangle)*(count+2));
2298 if (!urec) {
2299 XFree(rects);
2300 goto alt_code;
2303 /* insert our decoration rectangles in the rect list */
2304 memcpy(urec, rects, sizeof(XRectangle)*count);
2305 XFree(rects);
2307 if (!WFLAGP(wwin, no_titlebar)) {
2308 urec[count].x = -1;
2309 urec[count].y = -1 - wwin->frame->top_width;
2310 urec[count].width = wwin->frame->core->width + 2;
2311 urec[count].height = wwin->frame->top_width + 1;
2312 count++;
2314 if (!WFLAGP(wwin, no_resizebar)) {
2315 urec[count].x = -1;
2316 urec[count].y = wwin->frame->core->height
2317 - wwin->frame->bottom_width - wwin->frame->top_width;
2318 urec[count].width = wwin->frame->core->width + 2;
2319 urec[count].height = wwin->frame->bottom_width + 1;
2320 count++;
2323 /* shape our frame window */
2324 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2325 0, wwin->frame->top_width, urec, count,
2326 ShapeSet, Unsorted);
2327 XFlush(dpy);
2328 free(urec);
2329 return;
2331 alt_code:
2332 #endif /* OPTIMIZE_SHAPE */
2333 count = 0;
2334 if (!WFLAGP(wwin, no_titlebar)) {
2335 rect[count].x = -1;
2336 rect[count].y = -1;
2337 rect[count].width = wwin->frame->core->width + 2;
2338 rect[count].height = wwin->frame->top_width + 1;
2339 count++;
2341 if (!WFLAGP(wwin, no_resizebar)) {
2342 rect[count].x = -1;
2343 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2344 rect[count].width = wwin->frame->core->width + 2;
2345 rect[count].height = wwin->frame->bottom_width + 1;
2346 count++;
2348 if (count > 0) {
2349 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2350 0, 0, rect, count, ShapeSet, Unsorted);
2352 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2353 0, wwin->frame->top_width, wwin->client_win,
2354 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2355 XFlush(dpy);
2357 #endif /* SHAPE */
2359 /* ====================================================================== */
2361 static FocusMode
2362 getFocusMode(WWindow *wwin)
2364 FocusMode mode;
2366 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2367 if (wwin->wm_hints->input == True) {
2368 if (wwin->protocols.TAKE_FOCUS)
2369 mode = WFM_LOCALLY_ACTIVE;
2370 else
2371 mode = WFM_PASSIVE;
2372 } else {
2373 if (wwin->protocols.TAKE_FOCUS)
2374 mode = WFM_GLOBALLY_ACTIVE;
2375 else
2376 mode = WFM_NO_INPUT;
2378 } else {
2379 mode = WFM_PASSIVE;
2381 return mode;
2385 void
2386 wWindowSetKeyGrabs(WWindow *wwin)
2388 int i;
2389 WShortKey *key;
2391 for (i=0; i<WKBD_LAST; i++) {
2392 key = &wKeyBindings[i];
2394 if (key->keycode==0)
2395 continue;
2396 if (key->modifier!=AnyModifier) {
2397 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2398 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2399 #ifdef NUMLOCK_HACK
2400 /* Also grab all modifier combinations possible that include,
2401 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2402 * work even if the NumLock/ScrollLock key is on.
2404 wHackedGrabKey(key->keycode, key->modifier,
2405 wwin->frame->core->window, True, GrabModeAsync,
2406 GrabModeAsync);
2407 #endif
2409 XGrabKey(dpy, key->keycode, key->modifier,
2410 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2413 #ifndef LITE
2414 wRootMenuBindShortcuts(wwin->frame->core->window);
2415 #endif
2420 void
2421 wWindowResetMouseGrabs(WWindow *wwin)
2423 /* Mouse grabs can't be done on the client window because of
2424 * ICCCM and because clients that try to do the same will crash.
2426 * But there is a problem wich makes tbar buttons of unfocused
2427 * windows not usable as the click goes to the frame window instead
2428 * of the button itself. Must figure a way to fix that.
2431 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2433 if (!WFLAGP(wwin, no_bind_mouse)) {
2434 /* grabs for Meta+drag */
2435 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2436 True, ButtonPressMask, GrabModeSync,
2437 GrabModeAsync, None, None);
2440 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2441 /* the passive grabs to focus the window */
2442 if (wPreferences.focus_mode == WKF_CLICK)
2443 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2444 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2445 None, None);
2447 XFlush(dpy);
2451 void
2452 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2454 if (attr->flags & GSExtraFlagsAttr) {
2455 if (MGFLAGP(wwin, broken_close) !=
2456 (attr->extra_flags & GSDocumentEditedFlag)) {
2457 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2458 wWindowUpdateButtonImages(wwin);
2464 WMagicNumber
2465 wWindowAddSavedState(char *instance, char *class, char *command,
2466 pid_t pid, WSavedState *state)
2468 WWindowState *wstate;
2470 wstate = malloc(sizeof(WWindowState));
2471 if (!wstate)
2472 return 0;
2474 memset(wstate, 0, sizeof(WWindowState));
2475 wstate->pid = pid;
2476 if (instance)
2477 wstate->instance = wstrdup(instance);
2478 if (class)
2479 wstate->class = wstrdup(class);
2480 if (command)
2481 wstate->command = wstrdup(command);
2482 wstate->state = state;
2484 wstate->next = windowState;
2485 windowState = wstate;
2487 #ifdef DEBUG
2488 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2489 class, command);
2490 #endif
2492 return wstate;
2496 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2499 WMagicNumber
2500 wWindowGetSavedState(Window win)
2502 char *instance, *class, *command=NULL;
2503 WWindowState *wstate = windowState;
2504 char **argv;
2505 int argc;
2507 if (!wstate)
2508 return NULL;
2510 if (XGetCommand(dpy, win, &argv, &argc)) {
2511 if (argc > 0)
2512 command = FlattenStringList(argv, argc);
2513 XFreeStringList(argv);
2515 if (!command)
2516 return NULL;
2518 if (PropGetWMClass(win, &class, &instance)) {
2519 while (wstate) {
2520 if (SAME(instance, wstate->instance) &&
2521 SAME(class, wstate->class) &&
2522 SAME(command, wstate->command)) {
2523 break;
2525 wstate = wstate->next;
2527 } else {
2528 wstate = NULL;
2531 #ifdef DEBUG
2532 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2533 class, command);
2534 #endif
2536 if (command) free(command);
2537 if (instance) XFree(instance);
2538 if (class) XFree(class);
2540 return wstate;
2544 void
2545 wWindowDeleteSavedState(WMagicNumber id)
2547 WWindowState *tmp, *wstate=(WWindowState*)id;
2549 if (!wstate || !windowState)
2550 return;
2552 tmp = windowState;
2553 if (tmp==wstate) {
2554 windowState = wstate->next;
2555 #ifdef DEBUG
2556 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2557 wstate, wstate->instance, wstate->class, wstate->command);
2558 #endif
2559 if (wstate->instance) free(wstate->instance);
2560 if (wstate->class) free(wstate->class);
2561 if (wstate->command) free(wstate->command);
2562 free(wstate->state);
2563 free(wstate);
2564 } else {
2565 while (tmp->next) {
2566 if (tmp->next==wstate) {
2567 tmp->next=wstate->next;
2568 #ifdef DEBUG
2569 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2570 wstate, wstate->instance, wstate->class, wstate->command);
2571 #endif
2572 if (wstate->instance) free(wstate->instance);
2573 if (wstate->class) free(wstate->class);
2574 if (wstate->command) free(wstate->command);
2575 free(wstate->state);
2576 free(wstate);
2577 break;
2579 tmp = tmp->next;
2585 void
2586 wWindowDeleteSavedStatesForPID(pid_t pid)
2588 WWindowState *tmp, *wstate;
2590 if (!windowState)
2591 return;
2593 tmp = windowState;
2594 if (tmp->pid == pid) {
2595 wstate = windowState;
2596 windowState = tmp->next;
2597 #ifdef DEBUG
2598 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2599 wstate, wstate->instance, wstate->class, wstate->command);
2600 #endif
2601 if (wstate->instance) free(wstate->instance);
2602 if (wstate->class) free(wstate->class);
2603 if (wstate->command) free(wstate->command);
2604 free(wstate->state);
2605 free(wstate);
2606 } else {
2607 while (tmp->next) {
2608 if (tmp->next->pid==pid) {
2609 wstate = tmp->next;
2610 tmp->next = wstate->next;
2611 #ifdef DEBUG
2612 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2613 wstate, wstate->instance, wstate->class, wstate->command);
2614 #endif
2615 if (wstate->instance) free(wstate->instance);
2616 if (wstate->class) free(wstate->class);
2617 if (wstate->command) free(wstate->command);
2618 free(wstate->state);
2619 free(wstate);
2620 break;
2622 tmp = tmp->next;
2628 /* ====================================================================== */
2630 static void
2631 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2633 WWindow *wwin = data;
2635 #ifndef NUMLOCK_HACK
2636 if ((event->xbutton.state & ValidModMask)
2637 != (event->xbutton.state & ~LockMask)) {
2638 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2639 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2641 #endif
2643 event->xbutton.state &= ValidModMask;
2645 CloseWindowMenu(wwin->screen_ptr);
2647 if (wPreferences.focus_mode==WKF_CLICK
2648 && !(event->xbutton.state&ControlMask)
2649 && !WFLAGP(wwin, no_focusable)) {
2650 wSetFocusTo(wwin->screen_ptr, wwin);
2653 if (event->xbutton.button == Button1)
2654 wRaiseFrame(wwin->frame->core);
2656 if (event->xbutton.window != wwin->frame->resizebar->window) {
2657 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2658 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2659 GrabModeAsync, GrabModeAsync, None,
2660 None, CurrentTime)!=GrabSuccess) {
2661 #ifdef DEBUG0
2662 wwarning("pointer grab failed for window move");
2663 #endif
2664 return;
2668 if (event->xbutton.state & MOD_MASK) {
2669 /* move the window */
2670 wMouseMoveWindow(wwin, event);
2671 XUngrabPointer(dpy, CurrentTime);
2672 } else {
2673 wMouseResizeWindow(wwin, event);
2674 XUngrabPointer(dpy, CurrentTime);
2680 static void
2681 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2683 WWindow *wwin = data;
2685 event->xbutton.state &= ValidModMask;
2687 if (event->xbutton.button==Button1) {
2688 if (event->xbutton.state == 0) {
2689 if (!WFLAGP(wwin, no_shadeable)) {
2690 /* shade window */
2691 if (wwin->flags.shaded)
2692 wUnshadeWindow(wwin);
2693 else
2694 wShadeWindow(wwin);
2696 } else {
2697 int dir = 0;
2699 if (event->xbutton.state & ControlMask)
2700 dir |= MAX_VERTICAL;
2702 if (event->xbutton.state & ShiftMask) {
2703 dir |= MAX_HORIZONTAL;
2704 if (!(event->xbutton.state & ControlMask))
2705 wSelectWindow(wwin, !wwin->flags.selected);
2708 /* maximize window */
2709 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2710 if (wwin->flags.maximized)
2711 wUnmaximizeWindow(wwin);
2712 else
2713 wMaximizeWindow(wwin, dir);
2716 } else if (event->xbutton.button==Button3) {
2717 if (event->xbutton.state & MOD_MASK) {
2718 wHideOtherApplications(wwin);
2720 } else if (event->xbutton.button==Button2) {
2721 wSelectWindow(wwin, !wwin->flags.selected);
2726 static void
2727 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2729 WWindow *wwin = desc->parent;
2731 event->xbutton.state &= ValidModMask;
2733 CloseWindowMenu(wwin->screen_ptr);
2735 if (wPreferences.focus_mode==WKF_CLICK
2736 && !(event->xbutton.state&ControlMask)
2737 && !WFLAGP(wwin, no_focusable)) {
2738 wSetFocusTo(wwin->screen_ptr, wwin);
2740 if (event->xbutton.button == Button1) {
2741 wRaiseFrame(wwin->frame->core);
2744 if (event->xbutton.state & MOD_MASK) {
2745 /* move the window */
2746 if (XGrabPointer(dpy, wwin->client_win, False,
2747 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2748 GrabModeAsync, GrabModeAsync, None,
2749 None, CurrentTime)!=GrabSuccess) {
2750 #ifdef DEBUG0
2751 wwarning("pointer grab failed for window move");
2752 #endif
2753 return;
2755 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2756 wMouseResizeWindow(wwin, event);
2757 else
2758 wMouseMoveWindow(wwin, event);
2759 XUngrabPointer(dpy, CurrentTime);
2764 static void
2765 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2767 WWindow *wwin = (WWindow*)data;
2769 #ifndef NUMLOCK_HACK
2770 if ((event->xbutton.state & ValidModMask)
2771 != (event->xbutton.state & ~LockMask)) {
2772 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2773 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2775 #endif
2776 event->xbutton.state &= ValidModMask;
2778 CloseWindowMenu(wwin->screen_ptr);
2780 if (wPreferences.focus_mode==WKF_CLICK
2781 && !(event->xbutton.state&ControlMask)
2782 && !WFLAGP(wwin, no_focusable)) {
2783 wSetFocusTo(wwin->screen_ptr, wwin);
2786 if (event->xbutton.button == Button1
2787 || event->xbutton.button == Button2) {
2789 if (event->xbutton.button == Button1) {
2790 if (event->xbutton.state & MOD_MASK) {
2791 wLowerFrame(wwin->frame->core);
2792 } else {
2793 wRaiseFrame(wwin->frame->core);
2796 if ((event->xbutton.state & ShiftMask)
2797 && !(event->xbutton.state & ControlMask)) {
2798 wSelectWindow(wwin, !wwin->flags.selected);
2799 return;
2801 if (event->xbutton.window != wwin->frame->titlebar->window
2802 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2803 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2804 GrabModeAsync, GrabModeAsync, None,
2805 None, CurrentTime)!=GrabSuccess) {
2806 #ifdef DEBUG0
2807 wwarning("pointer grab failed for window move");
2808 #endif
2809 return;
2812 /* move the window */
2813 wMouseMoveWindow(wwin, event);
2815 XUngrabPointer(dpy, CurrentTime);
2816 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2817 && !wwin->flags.internal_window
2818 && !WCHECK_STATE(WSTATE_MODAL)) {
2819 WObjDescriptor *desc;
2821 if (event->xbutton.window != wwin->frame->titlebar->window
2822 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2823 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2824 GrabModeAsync, GrabModeAsync, None,
2825 None, CurrentTime)!=GrabSuccess) {
2826 #ifdef DEBUG0
2827 wwarning("pointer grab failed for window move");
2828 #endif
2829 return;
2832 OpenWindowMenu(wwin, event->xbutton.x_root,
2833 wwin->frame_y+wwin->frame->top_width, False);
2835 /* allow drag select */
2836 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2837 event->xany.send_event = True;
2838 (*desc->handle_mousedown)(desc, event);
2840 XUngrabPointer(dpy, CurrentTime);
2846 static void
2847 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2849 WWindow *wwin = data;
2851 event->xbutton.state &= ValidModMask;
2853 CloseWindowMenu(wwin->screen_ptr);
2855 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2856 return;
2858 /* if control-click, kill the client */
2859 if (event->xbutton.state & ControlMask) {
2860 wClientKill(wwin);
2861 } else {
2862 #ifdef OLWM_HINTS
2863 if (wwin->flags.olwm_push_pin_out) {
2865 wwin->flags.olwm_push_pin_out = 0;
2867 wOLWMChangePushpinState(wwin, True);
2869 wFrameWindowUpdatePushButton(wwin->frame, False);
2871 return;
2873 #endif
2874 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2875 /* send delete message */
2876 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2882 static void
2883 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2885 WWindow *wwin = data;
2887 CloseWindowMenu(wwin->screen_ptr);
2889 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2890 return;
2892 /* send delete message */
2893 if (wwin->protocols.DELETE_WINDOW) {
2894 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2895 } else {
2896 wClientKill(wwin);
2901 #ifdef XKB_BUTTON_HINT
2902 static void
2903 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2905 WWindow *wwin = data;
2906 WFrameWindow *fwin = wwin->frame;
2907 WScreen *scr = fwin->screen_ptr;
2908 XkbStateRec staterec;
2909 int tl;
2911 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2912 return;
2913 tl = wwin->frame->languagemode;
2914 wwin->frame->languagemode = wwin->frame->last_languagemode;
2915 wwin->frame->last_languagemode = tl;
2916 wSetFocusTo(scr, wwin);
2917 wwin->frame->languagebutton_image =
2918 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
2919 wwin->frame->languagemode];
2920 wFrameWindowUpdateLanguageButton(wwin->frame);
2921 if (event->xbutton.button == Button3)
2922 return;
2923 wRaiseFrame(fwin->core);
2925 #endif
2928 static void
2929 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2931 WWindow *wwin = data;
2933 event->xbutton.state &= ValidModMask;
2935 CloseWindowMenu(wwin->screen_ptr);
2937 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2938 return;
2940 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2941 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2942 LastTimestamp);
2943 } else {
2944 WApplication *wapp;
2945 if ((event->xbutton.state & ControlMask) ||
2946 (event->xbutton.button == Button3)) {
2948 wapp = wApplicationOf(wwin->main_window);
2949 if (wapp && !WFLAGP(wwin, no_appicon))
2950 wHideApplication(wapp);
2951 } else if (event->xbutton.state==0) {
2952 wIconifyWindow(wwin);