Fixes relate to modelock.
[wmaker-crm.git] / src / window.c
blob8f9436c944edd7257ced947c2d8892c16d5bf3fa
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"
54 #include "list.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 wwin->screen_ptr->shortcutSelectedWindows[i] = list_remove_elem(wwin->screen_ptr->shortcutSelectedWindows[i], wwin);
216 if (wwin->screen_ptr->shortcutWindow[i] == wwin) {
217 if (wwin->screen_ptr->shortcutSelectedWindows[i]) {
218 LinkedList *list = wwin->screen_ptr->shortcutSelectedWindows[i];
219 wwin->screen_ptr->shortcutWindow[i] =
220 list->head;
222 else wwin->screen_ptr->shortcutWindow[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 scr->shortcutWindow[i] = wwin;
810 free(wstate);
814 /* don't let transients start miniaturized if their owners are not */
815 if (transientOwner && !transientOwner->flags.miniaturized
816 && wwin->flags.miniaturized && !withdraw) {
817 wwin->flags.miniaturized = 0;
818 if (wwin->wm_hints)
819 wwin->wm_hints->initial_state = NormalState;
822 /* set workspace on which the window starts */
823 if (workspace >= 0) {
824 if (workspace > scr->workspace_count-1) {
825 workspace = workspace % scr->workspace_count;
827 } else {
828 int w;
830 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
832 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
834 workspace = w;
836 } else {
837 if (wPreferences.open_transients_with_parent && transientOwner) {
839 workspace = transientOwner->frame->workspace;
841 } else {
843 workspace = scr->current_workspace;
848 /* setup window geometry */
849 if (win_state && win_state->state->use_geometry) {
850 width = win_state->state->w;
851 height = win_state->state->h;
853 wWindowConstrainSize(wwin, &width, &height);
855 /* do not ask for window placement if the window is
856 * transient, during startup, if the initial workspace is another one
857 * or if the window wants to start iconic.
858 * If geometry was saved, restore it. */
860 Bool dontBring = False;
862 if (win_state && win_state->state->use_geometry) {
863 x = win_state->state->x;
864 y = win_state->state->y;
865 } else if ((wwin->transient_for==None
866 || wPreferences.window_placement!=WPM_MANUAL)
867 && !scr->flags.startup
868 && workspace == scr->current_workspace
869 && !wwin->flags.miniaturized
870 && !wwin->flags.maximized
871 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
872 PlaceWindow(wwin, &x, &y, width, height);
873 if (wPreferences.window_placement == WPM_MANUAL)
874 dontBring = True;
877 if (WFLAGP(wwin, dont_move_off) && dontBring)
878 wScreenBringInside(scr, &x, &y, width, height);
881 if (wwin->flags.urgent) {
882 if (!IS_OMNIPRESENT(wwin))
883 wwin->flags.omnipresent ^= 1;
887 *--------------------------------------------------
889 * Create frame, borders and do reparenting
891 *--------------------------------------------------
893 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
894 #ifdef XKB_BUTTON_HINT
895 if (wPreferences.modelock)
896 foo |= WFF_XKB_BUTTON;
897 #endif
898 if (!WFLAGP(wwin, no_titlebar))
899 foo |= WFF_TITLEBAR;
900 if (!WFLAGP(wwin, no_resizebar))
901 foo |= WFF_RESIZEBAR;
903 wwin->frame = wFrameWindowCreate(scr, window_level,
904 x, y, width, height, foo,
905 scr->window_title_texture,
906 scr->resizebar_texture,
907 scr->window_title_pixel,
908 &scr->window_title_gc,
909 &scr->title_font);
911 wwin->frame->flags.is_client_window_frame = 1;
912 wwin->frame->flags.justification = wPreferences.title_justification;
914 /* setup button images */
915 wWindowUpdateButtonImages(wwin);
917 /* hide unused buttons */
918 foo = 0;
919 if (WFLAGP(wwin, no_close_button))
920 foo |= WFF_RIGHT_BUTTON;
921 if (WFLAGP(wwin, no_miniaturize_button))
922 foo |= WFF_LEFT_BUTTON;
923 if (foo!=0)
924 wFrameWindowHideButton(wwin->frame, foo);
926 wwin->frame->child = wwin;
928 #ifdef OLWM_HINTS
929 /* emulate olwm push pin. Make the button look as pushed-in for
930 * the pinned-out state. When the button is clicked, it will
931 * revert to the normal position, which means the pin is pinned-in.
933 if (wwin->flags.olwm_push_pin_out)
934 wFrameWindowUpdatePushButton(wwin->frame, True);
935 #endif /* OLWM_HINTS */
937 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
938 if (title)
939 XFree(title);
941 wwin->frame->workspace = workspace;
943 wwin->frame->on_click_left = windowIconifyClick;
944 #ifdef XKB_BUTTON_HINT
945 if (wPreferences.modelock)
946 wwin->frame->on_click_language = windowLanguageClick;
947 #endif
949 wwin->frame->on_click_right = windowCloseClick;
950 wwin->frame->on_dblclick_right = windowCloseDblClick;
952 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
953 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
955 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
958 XSelectInput(dpy, wwin->client_win,
959 wwin->event_mask & ~StructureNotifyMask);
961 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
962 0, wwin->frame->top_width);
964 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
968 int gx, gy;
970 wClientGetGravityOffsets(wwin, &gx, &gy);
972 /* if gravity is to the south, account for the border sizes */
973 if (gy > 0)
974 y -= wwin->frame->top_width + wwin->frame->bottom_width;
978 * wWindowConfigure() will init the client window's size
979 * (wwin->client.{width,height}) and all other geometry
980 * related variables (frame_x,frame_y)
982 wWindowConfigure(wwin, x, y, width, height);
984 /* to make sure the window receives it's new position after reparenting */
985 wWindowSynthConfigureNotify(wwin);
988 *--------------------------------------------------
990 * Setup descriptors and save window to internal
991 * lists
993 *--------------------------------------------------
996 if (wwin->main_window!=None) {
997 WApplication *app;
998 WWindow *leader;
1000 /* Leader windows do not necessary set themselves as leaders.
1001 * If this is the case, point the leader of this window to
1002 * itself */
1003 leader = wWindowFor(wwin->main_window);
1004 if (leader && leader->main_window==None) {
1005 leader->main_window = leader->client_win;
1007 app = wApplicationCreate(scr, wwin->main_window);
1008 if (app) {
1009 app->last_workspace = workspace;
1012 * Do application specific stuff, like setting application
1013 * wide attributes.
1016 if (wwin->flags.hidden) {
1017 /* if the window was set to hidden because it was hidden
1018 * in a previous incarnation and that state was restored */
1019 app->flags.hidden = 1;
1022 if (app->flags.hidden) {
1023 wwin->flags.hidden = 1;
1028 /* setup the frame descriptor */
1029 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1030 wwin->frame->core->descriptor.parent = wwin;
1031 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1033 /* don't let windows go away if we die */
1034 XAddToSaveSet(dpy, window);
1036 XLowerWindow(dpy, window);
1038 /* if window is in this workspace and should be mapped, then map it */
1039 if (!wwin->flags.miniaturized
1040 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1041 && !wwin->flags.hidden && !withdraw) {
1043 /* The following "if" is to avoid crashing of clients that expect
1044 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1045 * after the return from this function.
1047 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1048 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1049 } else {
1050 wClientSetState(wwin, NormalState, None);
1053 #if 0
1054 /* if not auto focus, then map the window under the currently
1055 * focused window */
1056 #define _WIDTH(w) (w)->frame->core->width
1057 #define _HEIGHT(w) (w)->frame->core->height
1058 if (!wPreferences.auto_focus && scr->focused_window
1059 && !scr->flags.startup && !transientOwner
1060 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1061 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1062 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1063 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1064 || wwin->flags.maximized)) {
1065 MoveInStackListUnder(scr->focused_window->frame->core,
1066 wwin->frame->core);
1068 #undef _WIDTH
1069 #undef _HEIGHT
1071 #endif
1073 if (wPreferences.superfluous && !wPreferences.no_animations
1074 && !scr->flags.startup && wwin->transient_for==None
1076 * The brain damaged idiotic non-click to focus modes will
1077 * have trouble with this because:
1079 * 1. window is created and mapped by the client
1080 * 2. window is mapped by wmaker in small size
1081 * 3. window is animated to grow to normal size
1082 * 4. this function returns to normal event loop
1083 * 5. eventually, the EnterNotify event that would trigger
1084 * the window focusing (if the mouse is over that window)
1085 * will be processed by wmaker.
1086 * But since this event will be rather delayed
1087 * (step 3 has a large delay) the time when the event ocurred
1088 * and when it is processed, the client that owns that window
1089 * will reject the XSetInputFocus() for it.
1091 && (wPreferences.focus_mode==WKF_CLICK
1092 || wPreferences.auto_focus)) {
1093 DoWindowBirth(wwin);
1096 wWindowMap(wwin);
1099 /* setup stacking descriptor */
1100 if (transientOwner) {
1101 /* && wPreferences.on_top_transients */
1102 if (transientOwner) {
1103 wwin->frame->core->stacking->child_of =
1104 transientOwner->frame->core;
1106 } else {
1107 wwin->frame->core->stacking->child_of = NULL;
1111 if (!scr->focused_window) {
1112 /* first window on the list */
1113 wwin->next = NULL;
1114 wwin->prev = NULL;
1115 scr->focused_window = wwin;
1116 } else {
1117 WWindow *tmp;
1119 /* add window at beginning of focus window list */
1120 tmp = scr->focused_window;
1121 while (tmp->prev)
1122 tmp = tmp->prev;
1123 tmp->prev = wwin;
1124 wwin->next = tmp;
1125 wwin->prev = NULL;
1128 #ifdef GNOME_STUFF
1129 wGNOMEUpdateClientStateHint(wwin, True);
1130 #endif
1131 #ifdef KWM_HINTS
1132 wKWMUpdateClientWorkspace(wwin);
1133 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1134 #endif
1136 XUngrabServer(dpy);
1139 *--------------------------------------------------
1141 * Final preparations before window is ready to go
1143 *--------------------------------------------------
1146 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1149 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1150 && !wwin->flags.hidden) {
1151 if (((transientOwner && transientOwner->flags.focused)
1152 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1153 wSetFocusTo(scr, wwin);
1155 wWindowResetMouseGrabs(wwin);
1157 if (!WFLAGP(wwin, no_bind_keys)) {
1158 wWindowSetKeyGrabs(wwin);
1160 #ifdef GNOME_STUFF
1161 wGNOMEUpdateClientListHint(scr);
1162 #endif
1163 #ifdef KWM_HINTS
1164 wwin->flags.kwm_managed = 1;
1166 wKWMSendEventMessage(wwin, WKWMAddWindow);
1167 #endif
1169 wColormapInstallForWindow(scr, scr->cmap_window);
1171 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1173 #ifdef OLWM_HINTS
1174 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1175 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1177 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1178 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1179 wwin->frame_y);
1181 #endif
1184 *------------------------------------------------------------
1185 * Setup Notification Observers
1186 *------------------------------------------------------------
1188 WMAddNotificationObserver(appearanceObserver, wwin,
1189 WNWindowAppearanceSettingsChanged, wwin);
1193 *--------------------------------------------------
1195 * Cleanup temporary stuff
1197 *--------------------------------------------------
1200 if (win_state)
1201 wWindowDeleteSavedState(win_state);
1203 /* If the window must be withdrawed, then do it now.
1204 * Must do some optimization, 'though */
1205 if (withdraw) {
1206 wwin->flags.mapped = 0;
1207 wClientSetState(wwin, WithdrawnState, None);
1208 wUnmanageWindow(wwin, True, False);
1209 wwin = NULL;
1212 return wwin;
1219 WWindow*
1220 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1221 char *title, int x, int y, int width, int height)
1223 WWindow *wwin;
1224 int foo;
1226 wwin = wWindowCreate();
1228 WMAddNotificationObserver(appearanceObserver, wwin,
1229 WNWindowAppearanceSettingsChanged, wwin);
1231 wwin->flags.internal_window = 1;
1233 WSETUFLAG(wwin, omnipresent, 1);
1234 WSETUFLAG(wwin, no_shadeable, 1);
1235 WSETUFLAG(wwin, no_resizable, 1);
1236 WSETUFLAG(wwin, no_miniaturizable, 1);
1238 wwin->focus_mode = WFM_PASSIVE;
1240 wwin->client_win = window;
1241 wwin->screen_ptr = scr;
1243 wwin->transient_for = owner;
1245 wwin->client.x = x;
1246 wwin->client.y = y;
1247 wwin->client.width = width;
1248 wwin->client.height = height;
1250 wwin->frame_x = wwin->client.x;
1251 wwin->frame_y = wwin->client.y;
1254 foo = WFF_RIGHT_BUTTON;
1255 foo |= WFF_TITLEBAR;
1256 #ifdef XKB_BUTTON_HINT
1257 foo |= WFF_XKB_BUTTON;
1258 #endif
1260 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1261 wwin->frame_x, wwin->frame_y,
1262 width, height, foo,
1263 scr->window_title_texture,
1264 scr->resizebar_texture,
1265 scr->window_title_pixel,
1266 &scr->window_title_gc,
1267 &scr->title_font);
1269 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1271 wwin->frame->flags.is_client_window_frame = 1;
1272 wwin->frame->flags.justification = wPreferences.title_justification;
1274 wFrameWindowChangeTitle(wwin->frame, title);
1276 /* setup button images */
1277 wWindowUpdateButtonImages(wwin);
1279 /* hide buttons */
1280 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1282 wwin->frame->child = wwin;
1284 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1286 #ifdef XKB_BUTTON_HINT
1287 if (wPreferences.modelock)
1288 wwin->frame->on_click_language = windowLanguageClick;
1289 #endif
1291 wwin->frame->on_click_right = windowCloseClick;
1293 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1294 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1296 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1298 wwin->client.y += wwin->frame->top_width;
1299 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1300 0, wwin->frame->top_width);
1302 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1303 wwin->client.width, wwin->client.height);
1305 /* setup the frame descriptor */
1306 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1307 wwin->frame->core->descriptor.parent = wwin;
1308 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1311 XLowerWindow(dpy, window);
1312 XMapSubwindows(dpy, wwin->frame->core->window);
1314 /* setup stacking descriptor */
1315 if (
1316 #ifdef removed
1317 wPreferences.on_top_transients &&
1318 #endif
1319 wwin->transient_for!=None
1320 && wwin->transient_for!=scr->root_win) {
1321 WWindow *tmp;
1322 tmp = wWindowFor(wwin->transient_for);
1323 if (tmp)
1324 wwin->frame->core->stacking->child_of = tmp->frame->core;
1325 } else {
1326 wwin->frame->core->stacking->child_of = NULL;
1330 if (!scr->focused_window) {
1331 /* first window on the list */
1332 wwin->next = NULL;
1333 wwin->prev = NULL;
1334 scr->focused_window = wwin;
1335 } else {
1336 WWindow *tmp;
1338 /* add window at beginning of focus window list */
1339 tmp = scr->focused_window;
1340 while (tmp->prev)
1341 tmp = tmp->prev;
1342 tmp->prev = wwin;
1343 wwin->next = tmp;
1344 wwin->prev = NULL;
1347 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1349 /* if (wPreferences.auto_focus)*/
1350 wSetFocusTo(scr, wwin);
1352 wWindowResetMouseGrabs(wwin);
1354 wWindowSetKeyGrabs(wwin);
1356 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1358 return wwin;
1363 *----------------------------------------------------------------------
1364 * wUnmanageWindow--
1365 * Removes the frame window from a window and destroys all data
1366 * related to it. The window will be reparented back to the root window
1367 * if restore is True.
1369 * Side effects:
1370 * Everything related to the window is destroyed and the window
1371 * is removed from the window lists. Focus is set to the previous on the
1372 * window list.
1373 *----------------------------------------------------------------------
1375 void
1376 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1378 WCoreWindow *frame = wwin->frame->core;
1379 WWindow *owner = NULL;
1380 WWindow *newFocusedWindow = NULL;
1381 int wasFocused;
1382 WScreen *scr = wwin->screen_ptr;
1385 #ifdef KWM_HINTS
1386 wwin->frame->workspace = -1;
1388 wKWMUpdateClientWorkspace(wwin);
1389 #endif
1391 /* First close attribute editor window if open */
1392 if (wwin->flags.inspector_open) {
1393 WWindow *pwin = wwin->inspector->frame; /* the inspector window */
1394 (*pwin->frame->on_click_right)(NULL, pwin, NULL);
1397 /* Close window menu if it's open for this window */
1398 if (wwin->flags.menu_open_for_me) {
1399 CloseWindowMenu(scr);
1402 if (!destroyed) {
1403 if (!wwin->flags.internal_window)
1404 XRemoveFromSaveSet(dpy, wwin->client_win);
1406 XSelectInput(dpy, wwin->client_win, NoEventMask);
1408 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1409 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1412 XUnmapWindow(dpy, frame->window);
1414 XUnmapWindow(dpy, wwin->client_win);
1416 /* deselect window */
1417 wSelectWindow(wwin, False);
1419 /* remove all pending events on window */
1420 /* I think this only matters for autoraise */
1421 if (wPreferences.raise_delay)
1422 WMDeleteTimerWithClientData(wwin->frame->core);
1424 XFlush(dpy);
1426 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1428 /* reparent the window back to the root */
1429 if (restore)
1430 wClientRestore(wwin);
1432 if (wwin->transient_for!=scr->root_win) {
1433 owner = wWindowFor(wwin->transient_for);
1434 if (owner) {
1435 if (!owner->flags.semi_focused) {
1436 owner = NULL;
1437 } else {
1438 owner->flags.semi_focused = 0;
1443 wasFocused = wwin->flags.focused;
1445 /* remove from window focus list */
1446 if (!wwin->prev && !wwin->next) {
1447 /* was the only window */
1448 scr->focused_window = NULL;
1449 newFocusedWindow = NULL;
1450 } else {
1451 WWindow *tmp;
1453 if (wwin->prev)
1454 wwin->prev->next = wwin->next;
1455 if (wwin->next)
1456 wwin->next->prev = wwin->prev;
1457 else {
1458 scr->focused_window = wwin->prev;
1459 scr->focused_window->next = NULL;
1462 /* if in click to focus mode and the window
1463 * was a transient, focus the owner window
1465 tmp = NULL;
1466 if (wPreferences.focus_mode==WKF_CLICK) {
1467 tmp = wWindowFor(wwin->transient_for);
1468 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1469 tmp = NULL;
1472 /* otherwise, focus the next one in the focus list */
1473 if (!tmp) {
1474 tmp = scr->focused_window;
1475 while (tmp) {
1476 if (!WFLAGP(tmp, no_focusable)
1477 && (tmp->flags.mapped || tmp->flags.shaded))
1478 break;
1479 tmp = tmp->prev;
1482 if (wPreferences.focus_mode==WKF_CLICK) {
1483 newFocusedWindow = tmp;
1484 } else if (wPreferences.focus_mode==WKF_SLOPPY
1485 || wPreferences.focus_mode==WKF_POINTER) {
1486 unsigned int mask;
1487 int foo;
1488 Window bar, win;
1490 /* This is to let the root window get the keyboard input
1491 * if Sloppy focus mode and no other window get focus.
1492 * This way keybindings will not freeze.
1494 tmp = NULL;
1495 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1496 &foo, &foo, &foo, &foo, &mask))
1497 tmp = wWindowFor(win);
1498 if (tmp == wwin)
1499 tmp = NULL;
1500 newFocusedWindow = tmp;
1501 } else {
1502 newFocusedWindow = NULL;
1506 if (!wwin->flags.internal_window) {
1507 #ifdef GNOME_STUFF
1508 wGNOMERemoveClient(wwin);
1509 #endif
1510 #ifdef KWM_HINTS
1511 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1512 #endif
1515 #ifdef DEBUG
1516 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1517 (unsigned)frame->window);
1518 #endif
1520 if (wasFocused) {
1521 if (newFocusedWindow != owner && owner) {
1522 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1524 wSetFocusTo(scr, newFocusedWindow);
1526 wWindowDestroy(wwin);
1527 XFlush(dpy);
1531 void
1532 wWindowMap(WWindow *wwin)
1534 XMapWindow(dpy, wwin->frame->core->window);
1535 if (!wwin->flags.shaded) {
1536 /* window will be remapped when getting MapNotify */
1537 XSelectInput(dpy, wwin->client_win,
1538 wwin->event_mask & ~StructureNotifyMask);
1539 XMapWindow(dpy, wwin->client_win);
1540 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1542 wwin->flags.mapped = 1;
1547 void
1548 wWindowUnmap(WWindow *wwin)
1550 wwin->flags.mapped = 0;
1552 /* prevent window withdrawal when getting UnmapNotify */
1553 XSelectInput(dpy, wwin->client_win,
1554 wwin->event_mask & ~StructureNotifyMask);
1555 XUnmapWindow(dpy, wwin->client_win);
1556 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1558 XUnmapWindow(dpy, wwin->frame->core->window);
1563 void
1564 wWindowFocus(WWindow *wwin, WWindow *owin)
1566 WWindow *nowner;
1567 WWindow *oowner;
1569 #ifdef KEEP_XKB_LOCK_STATUS
1570 if (wPreferences.modelock) {
1571 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1573 #endif /* KEEP_XKB_LOCK_STATUS */
1575 wwin->flags.semi_focused = 0;
1577 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1579 wwin->flags.focused = 1;
1581 wWindowResetMouseGrabs(wwin);
1583 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1585 if (owin == wwin || !owin)
1586 return;
1588 nowner = wWindowFor(wwin->transient_for);
1590 /* new window is a transient for the old window */
1591 if (nowner == owin) {
1592 owin->flags.semi_focused = 1;
1593 wWindowUnfocus(nowner);
1594 return;
1597 oowner = wWindowFor(owin->transient_for);
1599 /* new window is owner of old window */
1600 if (wwin == oowner) {
1601 wWindowUnfocus(owin);
1602 return;
1605 if (!nowner) {
1606 wWindowUnfocus(owin);
1607 return;
1610 /* new window has same owner of old window */
1611 if (oowner == nowner) {
1612 /* prevent unfocusing of owner */
1613 oowner->flags.semi_focused = 0;
1614 wWindowUnfocus(owin);
1615 oowner->flags.semi_focused = 1;
1617 return;
1620 /* nowner != NULL && oowner != nowner */
1621 nowner->flags.semi_focused = 1;
1622 wWindowUnfocus(nowner);
1623 wWindowUnfocus(owin);
1627 void
1628 wWindowUnfocus(WWindow *wwin)
1630 CloseWindowMenu(wwin->screen_ptr);
1632 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1633 ? WS_PFOCUSED : WS_UNFOCUSED);
1635 if (wwin->transient_for!=None
1636 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1637 WWindow *owner;
1638 owner = wWindowFor(wwin->transient_for);
1639 if (owner && owner->flags.semi_focused) {
1640 owner->flags.semi_focused = 0;
1641 if (owner->flags.mapped || owner->flags.shaded) {
1642 wWindowUnfocus(owner);
1643 wFrameWindowPaint(owner->frame);
1647 wwin->flags.focused = 0;
1649 wWindowResetMouseGrabs(wwin);
1651 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1658 *----------------------------------------------------------------------
1660 * wWindowConstrainSize--
1661 * Constrains size for the client window, taking the maximal size,
1662 * window resize increments and other size hints into account.
1664 * Returns:
1665 * The closest size to what was given that the client window can
1666 * have.
1668 *----------------------------------------------------------------------
1670 void
1671 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1673 XSizeHints *sizeh = wwin->normal_hints;
1674 int width = *nwidth;
1675 int height = *nheight;
1676 int winc = sizeh->width_inc;
1677 int hinc = sizeh->height_inc;
1679 if (width < sizeh->min_width)
1680 width = sizeh->min_width;
1681 if (height < sizeh->min_height)
1682 height = sizeh->min_height;
1684 if (width > sizeh->max_width)
1685 width = sizeh->max_width;
1686 if (height > sizeh->max_height)
1687 height = sizeh->max_height;
1689 /* aspect ratio code borrowed from olwm */
1690 if (sizeh->flags & PAspect) {
1691 /* adjust max aspect ratio */
1692 if (!(sizeh->max_aspect.x==1 && sizeh->max_aspect.y==1)
1693 && width * sizeh->max_aspect.y > height * sizeh->max_aspect.x) {
1694 if (sizeh->max_aspect.x > sizeh->max_aspect.y) {
1695 height = (width * sizeh->max_aspect.y) / sizeh->max_aspect.x;
1696 if (height > sizeh->max_height) {
1697 height = sizeh->max_height;
1698 width = (height*sizeh->max_aspect.x) / sizeh->max_aspect.y;
1700 } else {
1701 width = (height * sizeh->max_aspect.x) / sizeh->max_aspect.y;
1702 if (width > sizeh->max_width) {
1703 width = sizeh->max_width;
1704 height = (width*sizeh->max_aspect.y) / sizeh->max_aspect.x;
1709 /* adjust min aspect ratio */
1710 if (!(sizeh->min_aspect.x==1 && sizeh->min_aspect.y==1)
1711 && width * sizeh->min_aspect.y < height * sizeh->min_aspect.x) {
1712 if (sizeh->min_aspect.x > sizeh->min_aspect.y) {
1713 height = (width * sizeh->min_aspect.y) / sizeh->min_aspect.x;
1714 if (height < sizeh->min_height) {
1715 height = sizeh->min_height;
1716 width = (height*sizeh->min_aspect.x) / sizeh->min_aspect.y;
1718 } else {
1719 width = (height * sizeh->min_aspect.x) / sizeh->min_aspect.y;
1720 if (width < sizeh->min_width) {
1721 width = sizeh->min_width;
1722 height = (width*sizeh->min_aspect.y) / sizeh->min_aspect.x;
1728 if (sizeh->base_width != 0) {
1729 width = (((width - sizeh->base_width) / winc) * winc)
1730 + sizeh->base_width;
1731 } else {
1732 width = (((width - sizeh->min_width) / winc) * winc)
1733 + sizeh->min_width;
1736 if (sizeh->base_width != 0) {
1737 height = (((height - sizeh->base_height) / hinc) * hinc)
1738 + sizeh->base_height;
1739 } else {
1740 height = (((height - sizeh->min_height) / hinc) * hinc)
1741 + sizeh->min_height;
1744 *nwidth = width;
1745 *nheight = height;
1749 void
1750 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1752 WScreen *scr = wwin->screen_ptr;
1753 WApplication *wapp;
1754 int unmap = 0;
1756 if (workspace >= scr->workspace_count || workspace < 0
1757 || workspace == wwin->frame->workspace)
1758 return;
1760 if (workspace != scr->current_workspace) {
1761 /* Sent to other workspace. Unmap window */
1762 if ((wwin->flags.mapped
1763 || wwin->flags.shaded
1764 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1765 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1767 wapp = wApplicationOf(wwin->main_window);
1768 if (wapp) {
1769 wapp->last_workspace = workspace;
1771 if (wwin->flags.miniaturized) {
1772 XUnmapWindow(dpy, wwin->icon->core->window);
1773 wwin->icon->mapped = 0;
1774 } else {
1775 unmap = 1;
1776 wSetFocusTo(scr, NULL);
1779 } else {
1780 /* brought to current workspace. Map window */
1781 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1782 XMapWindow(dpy, wwin->icon->core->window);
1783 wwin->icon->mapped = 1;
1784 } else if (!wwin->flags.mapped &&
1785 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1786 wWindowMap(wwin);
1789 if (!IS_OMNIPRESENT(wwin)) {
1790 wwin->frame->workspace = workspace;
1791 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1793 #ifdef GNOME_STUFF
1794 wGNOMEUpdateClientStateHint(wwin, True);
1795 #endif
1796 #ifdef KWM_HINTS
1797 wKWMUpdateClientWorkspace(wwin);
1798 wKWMSendEventMessage(wwin, WKWMChangedClient);
1799 #endif
1800 if (unmap) {
1801 wWindowUnmap(wwin);
1806 void
1807 wWindowSynthConfigureNotify(WWindow *wwin)
1809 XEvent sevent;
1811 sevent.type = ConfigureNotify;
1812 sevent.xconfigure.display = dpy;
1813 sevent.xconfigure.event = wwin->client_win;
1814 sevent.xconfigure.window = wwin->client_win;
1816 sevent.xconfigure.x = wwin->client.x;
1817 sevent.xconfigure.y = wwin->client.y;
1818 sevent.xconfigure.width = wwin->client.width;
1819 sevent.xconfigure.height = wwin->client.height;
1821 sevent.xconfigure.border_width = wwin->old_border_width;
1822 if (WFLAGP(wwin, no_titlebar))
1823 sevent.xconfigure.above = None;
1824 else
1825 sevent.xconfigure.above = wwin->frame->titlebar->window;
1827 sevent.xconfigure.override_redirect = False;
1828 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1829 #ifdef KWM_HINTS
1830 wKWMSendEventMessage(wwin, WKWMChangedClient);
1831 #endif
1832 XFlush(dpy);
1837 *----------------------------------------------------------------------
1838 * wWindowConfigure--
1839 * Configures the frame, decorations and client window to the
1840 * specified geometry. The geometry is not checked for validity,
1841 * wWindowConstrainSize() must be used for that.
1842 * The size parameters are for the client window, but the position is
1843 * for the frame.
1844 * The client window receives a ConfigureNotify event, according
1845 * to what ICCCM says.
1847 * Returns:
1848 * None
1850 * Side effects:
1851 * Window size and position are changed and client window receives
1852 * a ConfigureNotify event.
1853 *----------------------------------------------------------------------
1855 void
1856 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1857 WWindow *wwin;
1858 int req_x, req_y; /* new position of the frame */
1859 int req_width, req_height; /* new size of the client */
1861 int synth_notify = False;
1862 int resize;
1864 resize = (req_width!=wwin->client.width
1865 || req_height!=wwin->client.height);
1867 * if the window is being moved but not resized then
1868 * send a synthetic ConfigureNotify
1870 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1871 synth_notify = True;
1874 if (WFLAGP(wwin, dont_move_off))
1875 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1876 req_width, req_height);
1877 if (resize) {
1878 if (req_width < MIN_WINDOW_SIZE)
1879 req_width = MIN_WINDOW_SIZE;
1880 if (req_height < MIN_WINDOW_SIZE)
1881 req_height = MIN_WINDOW_SIZE;
1883 /* If growing, resize inner part before frame,
1884 * if shrinking, resize frame before.
1885 * This will prevent the frame (that can have a different color)
1886 * to be exposed, causing flicker */
1887 if (req_height > wwin->frame->core->height
1888 || req_width > wwin->frame->core->width)
1889 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1891 if (wwin->flags.shaded) {
1892 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1893 req_width, wwin->frame->core->height);
1894 wwin->old_geometry.height = req_height;
1895 } else {
1896 int h;
1898 h = req_height + wwin->frame->top_width
1899 + wwin->frame->bottom_width;
1901 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1904 if (!(req_height > wwin->frame->core->height
1905 || req_width > wwin->frame->core->width))
1906 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1908 wwin->client.x = req_x;
1909 wwin->client.y = req_y + wwin->frame->top_width;
1910 wwin->client.width = req_width;
1911 wwin->client.height = req_height;
1912 } else {
1913 wwin->client.x = req_x;
1914 wwin->client.y = req_y + wwin->frame->top_width;
1916 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1918 wwin->frame_x = req_x;
1919 wwin->frame_y = req_y;
1921 #ifdef SHAPE
1922 if (wShapeSupported && wwin->flags.shaped && resize) {
1923 wWindowSetShape(wwin);
1925 #endif
1927 if (synth_notify)
1928 wWindowSynthConfigureNotify(wwin);
1929 XFlush(dpy);
1933 void
1934 wWindowMove(wwin, req_x, req_y)
1935 WWindow *wwin;
1936 int req_x, req_y; /* new position of the frame */
1938 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1939 int synth_notify = False;
1941 /* Send a synthetic ConfigureNotify event for every window movement. */
1942 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1943 synth_notify = True;
1945 #else
1946 /* A single synthetic ConfigureNotify event is sent at the end of
1947 * a completed (opaque) movement in moveres.c */
1948 #endif
1950 if (WFLAGP(wwin, dont_move_off))
1951 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1952 wwin->frame->core->width, wwin->frame->core->height);
1954 wwin->client.x = req_x;
1955 wwin->client.y = req_y + wwin->frame->top_width;
1957 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1959 wwin->frame_x = req_x;
1960 wwin->frame_y = req_y;
1962 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1963 if (synth_notify)
1964 wWindowSynthConfigureNotify(wwin);
1965 #endif
1969 void
1970 wWindowUpdateButtonImages(WWindow *wwin)
1972 WScreen *scr = wwin->screen_ptr;
1973 Pixmap pixmap, mask;
1974 WFrameWindow *fwin = wwin->frame;
1976 if (WFLAGP(wwin, no_titlebar))
1977 return;
1979 /* miniaturize button */
1981 if (!WFLAGP(wwin, no_miniaturize_button)) {
1982 if (wwin->wm_gnustep_attr
1983 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
1984 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
1986 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
1987 mask = wwin->wm_gnustep_attr->miniaturize_mask;
1988 } else {
1989 mask = None;
1992 if (fwin->lbutton_image
1993 && (fwin->lbutton_image->image != pixmap
1994 || fwin->lbutton_image->mask != mask)) {
1995 wPixmapDestroy(fwin->lbutton_image);
1996 fwin->lbutton_image = NULL;
1999 if (!fwin->lbutton_image) {
2000 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2001 fwin->lbutton_image->client_owned = 1;
2002 fwin->lbutton_image->client_owned_mask = 1;
2004 } else {
2005 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2006 wPixmapDestroy(fwin->lbutton_image);
2008 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2012 #ifdef XKB_BUTTON_HINT
2013 if (!WFLAGP(wwin, no_language_button)) {
2014 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2015 wPixmapDestroy(fwin->languagebutton_image);
2017 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2019 #endif
2021 /* close button */
2023 if (!WFLAGP(wwin, no_close_button)) {
2024 if (wwin->wm_gnustep_attr
2025 && wwin->wm_gnustep_attr->flags&GSClosePixmapAttr) {
2026 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2028 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr) {
2029 mask = wwin->wm_gnustep_attr->close_mask;
2030 } else {
2031 mask = None;
2034 if (fwin->rbutton_image
2035 && (fwin->rbutton_image->image != pixmap
2036 || fwin->rbutton_image->mask != mask)) {
2037 wPixmapDestroy(fwin->rbutton_image);
2038 fwin->rbutton_image = NULL;
2041 if (!fwin->rbutton_image) {
2042 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2043 fwin->rbutton_image->client_owned = 1;
2044 fwin->rbutton_image->client_owned_mask = 1;
2046 } else if (WFLAGP(wwin, kill_close)) {
2047 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2048 wPixmapDestroy(fwin->rbutton_image);
2050 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2051 } else if (WFLAGP(wwin, broken_close)) {
2052 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2053 wPixmapDestroy(fwin->rbutton_image);
2055 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2056 } else {
2057 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2058 wPixmapDestroy(fwin->rbutton_image);
2060 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2064 /* force buttons to be redrawn */
2065 fwin->flags.need_texture_change = 1;
2066 wFrameWindowPaint(fwin);
2071 *---------------------------------------------------------------------------
2072 * wWindowConfigureBorders--
2073 * Update window border configuration according to attribute flags.
2075 *---------------------------------------------------------------------------
2077 void
2078 wWindowConfigureBorders(WWindow *wwin)
2080 if (wwin->frame) {
2081 int flags;
2082 int newy, oldh;
2084 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2085 if (!WFLAGP(wwin, no_titlebar))
2086 flags |= WFF_TITLEBAR;
2087 if (!WFLAGP(wwin, no_resizebar))
2088 flags |= WFF_RESIZEBAR;
2089 if (wwin->flags.shaded)
2090 flags |= WFF_IS_SHADED;
2092 oldh = wwin->frame->top_width;
2093 wFrameWindowUpdateBorders(wwin->frame, flags);
2094 if (oldh != wwin->frame->top_width) {
2095 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2097 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2098 wWindowConfigure(wwin, wwin->frame_x, newy,
2099 wwin->client.width, wwin->client.height);
2102 flags = 0;
2103 if (!WFLAGP(wwin, no_miniaturize_button)
2104 && wwin->frame->flags.hide_left_button)
2105 flags |= WFF_LEFT_BUTTON;
2107 if (!WFLAGP(wwin, no_close_button)
2108 && wwin->frame->flags.hide_right_button)
2109 flags |= WFF_RIGHT_BUTTON;
2111 if (flags!=0) {
2112 wWindowUpdateButtonImages(wwin);
2113 wFrameWindowShowButton(wwin->frame, flags);
2116 flags = 0;
2117 if (WFLAGP(wwin, no_miniaturize_button)
2118 && !wwin->frame->flags.hide_left_button)
2119 flags |= WFF_LEFT_BUTTON;
2121 if (WFLAGP(wwin, no_close_button)
2122 && !wwin->frame->flags.hide_right_button)
2123 flags |= WFF_RIGHT_BUTTON;
2125 if (flags!=0)
2126 wFrameWindowHideButton(wwin->frame, flags);
2128 #ifdef SHAPE
2129 if (wShapeSupported && wwin->flags.shaped) {
2130 wWindowSetShape(wwin);
2132 #endif
2137 void
2138 wWindowSaveState(WWindow *wwin)
2140 CARD32 data[10];
2141 int i;
2143 memset(data, 0, sizeof(CARD32)*10);
2144 data[0] = wwin->frame->workspace;
2145 data[1] = wwin->flags.miniaturized;
2146 data[2] = wwin->flags.shaded;
2147 data[3] = wwin->flags.hidden;
2149 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2150 if (wwin->screen_ptr->shortcutWindow[i] == wwin)
2151 data[9] |= 1<<i;
2153 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2154 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2155 (unsigned char *)data, 10);
2159 static int
2160 getSavedState(Window window, WSavedState **state)
2162 Atom type_ret;
2163 int fmt_ret;
2164 unsigned long nitems_ret;
2165 unsigned long bytes_after_ret;
2166 CARD32 *data;
2168 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2169 True, _XA_WINDOWMAKER_STATE,
2170 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2171 (unsigned char **)&data)!=Success || !data)
2172 return 0;
2174 *state = malloc(sizeof(WSavedState));
2176 if (*state) {
2177 (*state)->workspace = data[0];
2178 (*state)->miniaturized = data[1];
2179 (*state)->shaded = data[2];
2180 (*state)->hidden = data[3];
2181 (*state)->use_geometry = data[4];
2182 (*state)->x = data[5];
2183 (*state)->y = data[6];
2184 (*state)->w = data[7];
2185 (*state)->h = data[8];
2186 (*state)->window_shortcuts = data[9];
2188 XFree(data);
2190 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2191 return 1;
2192 else
2193 return 0;
2197 #ifdef SHAPE
2198 void
2199 wWindowClearShape(WWindow *wwin)
2201 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2202 0, wwin->frame->top_width, None, ShapeSet);
2203 XFlush(dpy);
2206 void
2207 wWindowSetShape(WWindow *wwin)
2209 XRectangle rect[2];
2210 int count;
2211 #ifdef OPTIMIZE_SHAPE
2212 XRectangle *rects;
2213 XRectangle *urec;
2214 int ordering;
2216 /* only shape is the client's */
2217 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2218 goto alt_code;
2221 /* Get array of rectangles describing the shape mask */
2222 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2223 &count, &ordering);
2224 if (!rects) {
2225 goto alt_code;
2228 urec = malloc(sizeof(XRectangle)*(count+2));
2229 if (!urec) {
2230 XFree(rects);
2231 goto alt_code;
2234 /* insert our decoration rectangles in the rect list */
2235 memcpy(urec, rects, sizeof(XRectangle)*count);
2236 XFree(rects);
2238 if (!WFLAGP(wwin, no_titlebar)) {
2239 urec[count].x = -1;
2240 urec[count].y = -1 - wwin->frame->top_width;
2241 urec[count].width = wwin->frame->core->width + 2;
2242 urec[count].height = wwin->frame->top_width + 1;
2243 count++;
2245 if (!WFLAGP(wwin, no_resizebar)) {
2246 urec[count].x = -1;
2247 urec[count].y = wwin->frame->core->height
2248 - wwin->frame->bottom_width - wwin->frame->top_width;
2249 urec[count].width = wwin->frame->core->width + 2;
2250 urec[count].height = wwin->frame->bottom_width + 1;
2251 count++;
2254 /* shape our frame window */
2255 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2256 0, wwin->frame->top_width, urec, count,
2257 ShapeSet, Unsorted);
2258 XFlush(dpy);
2259 free(urec);
2260 return;
2262 alt_code:
2263 #endif /* OPTIMIZE_SHAPE */
2264 count = 0;
2265 if (!WFLAGP(wwin, no_titlebar)) {
2266 rect[count].x = -1;
2267 rect[count].y = -1;
2268 rect[count].width = wwin->frame->core->width + 2;
2269 rect[count].height = wwin->frame->top_width + 1;
2270 count++;
2272 if (!WFLAGP(wwin, no_resizebar)) {
2273 rect[count].x = -1;
2274 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2275 rect[count].width = wwin->frame->core->width + 2;
2276 rect[count].height = wwin->frame->bottom_width + 1;
2277 count++;
2279 if (count > 0) {
2280 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2281 0, 0, rect, count, ShapeSet, Unsorted);
2283 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2284 0, wwin->frame->top_width, wwin->client_win,
2285 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2286 XFlush(dpy);
2288 #endif /* SHAPE */
2290 /* ====================================================================== */
2292 static FocusMode
2293 getFocusMode(WWindow *wwin)
2295 FocusMode mode;
2297 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2298 if (wwin->wm_hints->input == True) {
2299 if (wwin->protocols.TAKE_FOCUS)
2300 mode = WFM_LOCALLY_ACTIVE;
2301 else
2302 mode = WFM_PASSIVE;
2303 } else {
2304 if (wwin->protocols.TAKE_FOCUS)
2305 mode = WFM_GLOBALLY_ACTIVE;
2306 else
2307 mode = WFM_NO_INPUT;
2309 } else {
2310 mode = WFM_PASSIVE;
2312 return mode;
2316 void
2317 wWindowSetKeyGrabs(WWindow *wwin)
2319 int i;
2320 WShortKey *key;
2322 for (i=0; i<WKBD_LAST; i++) {
2323 key = &wKeyBindings[i];
2325 if (key->keycode==0)
2326 continue;
2327 if (key->modifier!=AnyModifier) {
2328 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2329 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2330 #ifdef NUMLOCK_HACK
2331 /* Also grab all modifier combinations possible that include,
2332 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2333 * work even if the NumLock/ScrollLock key is on.
2335 wHackedGrabKey(key->keycode, key->modifier,
2336 wwin->frame->core->window, True, GrabModeAsync,
2337 GrabModeAsync);
2338 #endif
2340 XGrabKey(dpy, key->keycode, key->modifier,
2341 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2344 #ifndef LITE
2345 wRootMenuBindShortcuts(wwin->frame->core->window);
2346 #endif
2351 void
2352 wWindowResetMouseGrabs(WWindow *wwin)
2354 /* Mouse grabs can't be done on the client window because of
2355 * ICCCM and because clients that try to do the same will crash.
2357 * But there is a problem wich makes tbar buttons of unfocused
2358 * windows not usable as the click goes to the frame window instead
2359 * of the button itself. Must figure a way to fix that.
2362 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2364 if (!WFLAGP(wwin, no_bind_mouse)) {
2365 /* grabs for Meta+drag */
2366 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2367 True, ButtonPressMask, GrabModeSync,
2368 GrabModeAsync, None, None);
2371 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2372 /* the passive grabs to focus the window */
2373 if (wPreferences.focus_mode == WKF_CLICK)
2374 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2375 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2376 None, None);
2378 XFlush(dpy);
2382 void
2383 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2386 if (attr->flags & GSExtraFlagsAttr) {
2387 if (WFLAGP(wwin, broken_close) !=
2388 (attr->extra_flags & GSDocumentEditedFlag)) {
2390 wwin->client_flags.broken_close = !WFLAGP(wwin, broken_close);
2392 wWindowUpdateButtonImages(wwin);
2399 WMagicNumber
2400 wWindowAddSavedState(char *instance, char *class, char *command,
2401 pid_t pid, WSavedState *state)
2403 WWindowState *wstate;
2405 wstate = malloc(sizeof(WWindowState));
2406 if (!wstate)
2407 return 0;
2409 memset(wstate, 0, sizeof(WWindowState));
2410 wstate->pid = pid;
2411 if (instance)
2412 wstate->instance = wstrdup(instance);
2413 if (class)
2414 wstate->class = wstrdup(class);
2415 if (command)
2416 wstate->command = wstrdup(command);
2417 wstate->state = state;
2419 wstate->next = windowState;
2420 windowState = wstate;
2422 #ifdef DEBUG
2423 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2424 class, command);
2425 #endif
2427 return wstate;
2431 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2434 WMagicNumber
2435 wWindowGetSavedState(Window win)
2437 char *instance, *class, *command=NULL;
2438 WWindowState *wstate = windowState;
2439 char **argv;
2440 int argc;
2442 if (!wstate)
2443 return NULL;
2445 if (XGetCommand(dpy, win, &argv, &argc)) {
2446 if (argc > 0)
2447 command = FlattenStringList(argv, argc);
2448 XFreeStringList(argv);
2450 if (!command)
2451 return NULL;
2453 if (PropGetWMClass(win, &class, &instance)) {
2454 while (wstate) {
2455 if (SAME(instance, wstate->instance) &&
2456 SAME(class, wstate->class) &&
2457 SAME(command, wstate->command)) {
2458 break;
2460 wstate = wstate->next;
2462 } else {
2463 wstate = NULL;
2466 #ifdef DEBUG
2467 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2468 class, command);
2469 #endif
2471 if (command) free(command);
2472 if (instance) XFree(instance);
2473 if (class) XFree(class);
2475 return wstate;
2479 void
2480 wWindowDeleteSavedState(WMagicNumber id)
2482 WWindowState *tmp, *wstate=(WWindowState*)id;
2484 if (!wstate || !windowState)
2485 return;
2487 tmp = windowState;
2488 if (tmp==wstate) {
2489 windowState = wstate->next;
2490 #ifdef DEBUG
2491 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2492 wstate, wstate->instance, wstate->class, wstate->command);
2493 #endif
2494 if (wstate->instance) free(wstate->instance);
2495 if (wstate->class) free(wstate->class);
2496 if (wstate->command) free(wstate->command);
2497 free(wstate->state);
2498 free(wstate);
2499 } else {
2500 while (tmp->next) {
2501 if (tmp->next==wstate) {
2502 tmp->next=wstate->next;
2503 #ifdef DEBUG
2504 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2505 wstate, wstate->instance, wstate->class, wstate->command);
2506 #endif
2507 if (wstate->instance) free(wstate->instance);
2508 if (wstate->class) free(wstate->class);
2509 if (wstate->command) free(wstate->command);
2510 free(wstate->state);
2511 free(wstate);
2512 break;
2514 tmp = tmp->next;
2520 void
2521 wWindowDeleteSavedStatesForPID(pid_t pid)
2523 WWindowState *tmp, *wstate;
2525 if (!windowState)
2526 return;
2528 tmp = windowState;
2529 if (tmp->pid == pid) {
2530 wstate = windowState;
2531 windowState = tmp->next;
2532 #ifdef DEBUG
2533 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2534 wstate, wstate->instance, wstate->class, wstate->command);
2535 #endif
2536 if (wstate->instance) free(wstate->instance);
2537 if (wstate->class) free(wstate->class);
2538 if (wstate->command) free(wstate->command);
2539 free(wstate->state);
2540 free(wstate);
2541 } else {
2542 while (tmp->next) {
2543 if (tmp->next->pid==pid) {
2544 wstate = tmp->next;
2545 tmp->next = wstate->next;
2546 #ifdef DEBUG
2547 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2548 wstate, wstate->instance, wstate->class, wstate->command);
2549 #endif
2550 if (wstate->instance) free(wstate->instance);
2551 if (wstate->class) free(wstate->class);
2552 if (wstate->command) free(wstate->command);
2553 free(wstate->state);
2554 free(wstate);
2555 break;
2557 tmp = tmp->next;
2563 /* ====================================================================== */
2565 static void
2566 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2568 WWindow *wwin = data;
2570 #ifndef NUMLOCK_HACK
2571 if ((event->xbutton.state & ValidModMask)
2572 != (event->xbutton.state & ~LockMask)) {
2573 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2574 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2576 #endif
2578 event->xbutton.state &= ValidModMask;
2580 CloseWindowMenu(wwin->screen_ptr);
2582 if (wPreferences.focus_mode==WKF_CLICK
2583 && !(event->xbutton.state&ControlMask)
2584 && !WFLAGP(wwin, no_focusable)) {
2585 wSetFocusTo(wwin->screen_ptr, wwin);
2588 if (event->xbutton.button == Button1)
2589 wRaiseFrame(wwin->frame->core);
2591 if (event->xbutton.window != wwin->frame->resizebar->window) {
2592 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2593 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2594 GrabModeAsync, GrabModeAsync, None,
2595 None, CurrentTime)!=GrabSuccess) {
2596 #ifdef DEBUG0
2597 wwarning("pointer grab failed for window move");
2598 #endif
2599 return;
2603 if (event->xbutton.state & MOD_MASK) {
2604 /* move the window */
2605 wMouseMoveWindow(wwin, event);
2606 XUngrabPointer(dpy, CurrentTime);
2607 } else {
2608 wMouseResizeWindow(wwin, event);
2609 XUngrabPointer(dpy, CurrentTime);
2615 static void
2616 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2618 WWindow *wwin = data;
2620 event->xbutton.state &= ValidModMask;
2622 if (event->xbutton.button==Button1) {
2623 if (event->xbutton.state == 0) {
2624 if (!WFLAGP(wwin, no_shadeable)) {
2625 /* shade window */
2626 if (wwin->flags.shaded)
2627 wUnshadeWindow(wwin);
2628 else
2629 wShadeWindow(wwin);
2631 } else {
2632 int dir = 0;
2634 if (event->xbutton.state & ControlMask)
2635 dir |= MAX_VERTICAL;
2637 if (event->xbutton.state & ShiftMask) {
2638 dir |= MAX_HORIZONTAL;
2639 if (!(event->xbutton.state & ControlMask))
2640 wSelectWindow(wwin, !wwin->flags.selected);
2643 /* maximize window */
2644 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2645 if (wwin->flags.maximized)
2646 wUnmaximizeWindow(wwin);
2647 else
2648 wMaximizeWindow(wwin, dir);
2651 } else if (event->xbutton.button==Button3) {
2652 if (event->xbutton.state & MOD_MASK) {
2653 wHideOtherApplications(wwin);
2655 } else if (event->xbutton.button==Button2) {
2656 wSelectWindow(wwin, !wwin->flags.selected);
2661 static void
2662 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2664 WWindow *wwin = desc->parent;
2666 event->xbutton.state &= ValidModMask;
2668 CloseWindowMenu(wwin->screen_ptr);
2670 if (wPreferences.focus_mode==WKF_CLICK
2671 && !(event->xbutton.state&ControlMask)
2672 && !WFLAGP(wwin, no_focusable)) {
2673 wSetFocusTo(wwin->screen_ptr, wwin);
2675 if (event->xbutton.button == Button1) {
2676 wRaiseFrame(wwin->frame->core);
2679 if (event->xbutton.state & MOD_MASK) {
2680 /* move the window */
2681 if (XGrabPointer(dpy, wwin->client_win, False,
2682 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2683 GrabModeAsync, GrabModeAsync, None,
2684 None, CurrentTime)!=GrabSuccess) {
2685 #ifdef DEBUG0
2686 wwarning("pointer grab failed for window move");
2687 #endif
2688 return;
2690 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2691 wMouseResizeWindow(wwin, event);
2692 else
2693 wMouseMoveWindow(wwin, event);
2694 XUngrabPointer(dpy, CurrentTime);
2699 static void
2700 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2702 WWindow *wwin = (WWindow*)data;
2704 #ifndef NUMLOCK_HACK
2705 if ((event->xbutton.state & ValidModMask)
2706 != (event->xbutton.state & ~LockMask)) {
2707 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2708 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2710 #endif
2711 event->xbutton.state &= ValidModMask;
2713 CloseWindowMenu(wwin->screen_ptr);
2715 if (wPreferences.focus_mode==WKF_CLICK
2716 && !(event->xbutton.state&ControlMask)
2717 && !WFLAGP(wwin, no_focusable)) {
2718 wSetFocusTo(wwin->screen_ptr, wwin);
2721 if (event->xbutton.button == Button1
2722 || event->xbutton.button == Button2) {
2724 if (event->xbutton.button == Button1) {
2725 if (event->xbutton.state & MOD_MASK) {
2726 wLowerFrame(wwin->frame->core);
2727 } else {
2728 wRaiseFrame(wwin->frame->core);
2731 if ((event->xbutton.state & ShiftMask)
2732 && !(event->xbutton.state & ControlMask)) {
2733 wSelectWindow(wwin, !wwin->flags.selected);
2734 return;
2736 if (event->xbutton.window != wwin->frame->titlebar->window
2737 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2738 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2739 GrabModeAsync, GrabModeAsync, None,
2740 None, CurrentTime)!=GrabSuccess) {
2741 #ifdef DEBUG0
2742 wwarning("pointer grab failed for window move");
2743 #endif
2744 return;
2747 /* move the window */
2748 wMouseMoveWindow(wwin, event);
2750 XUngrabPointer(dpy, CurrentTime);
2751 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2752 && !wwin->flags.internal_window
2753 && !WCHECK_STATE(WSTATE_MODAL)) {
2754 WObjDescriptor *desc;
2756 if (event->xbutton.window != wwin->frame->titlebar->window
2757 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2758 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2759 GrabModeAsync, GrabModeAsync, None,
2760 None, CurrentTime)!=GrabSuccess) {
2761 #ifdef DEBUG0
2762 wwarning("pointer grab failed for window move");
2763 #endif
2764 return;
2767 OpenWindowMenu(wwin, event->xbutton.x_root,
2768 wwin->frame_y+wwin->frame->top_width, False);
2770 /* allow drag select */
2771 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2772 event->xany.send_event = True;
2773 (*desc->handle_mousedown)(desc, event);
2775 XUngrabPointer(dpy, CurrentTime);
2781 static void
2782 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2784 WWindow *wwin = data;
2786 event->xbutton.state &= ValidModMask;
2788 CloseWindowMenu(wwin->screen_ptr);
2790 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2791 return;
2793 /* if control-click, kill the client */
2794 if (event->xbutton.state & ControlMask) {
2795 wClientKill(wwin);
2796 } else {
2797 #ifdef OLWM_HINTS
2798 if (wwin->flags.olwm_push_pin_out) {
2800 wwin->flags.olwm_push_pin_out = 0;
2802 wOLWMChangePushpinState(wwin, True);
2804 wFrameWindowUpdatePushButton(wwin->frame, False);
2806 return;
2808 #endif
2809 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2810 /* send delete message */
2811 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2817 static void
2818 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2820 WWindow *wwin = data;
2822 CloseWindowMenu(wwin->screen_ptr);
2824 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2825 return;
2827 /* send delete message */
2828 if (wwin->protocols.DELETE_WINDOW) {
2829 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2830 } else {
2831 wClientKill(wwin);
2836 #ifdef XKB_BUTTON_HINT
2837 static void
2838 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2840 WWindow *wwin = data;
2841 WFrameWindow *fwin = wwin->frame;
2842 WScreen *scr = fwin->screen_ptr;
2843 XkbStateRec staterec;
2844 int tl;
2846 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2847 return;
2848 tl = wwin->frame->languagemode;
2849 wwin->frame->languagemode = wwin->frame->last_languagemode;
2850 wwin->frame->last_languagemode = tl;
2851 wFrameWindowPaint(fwin);
2852 wSetFocusTo(scr, wwin);
2853 if (event->xbutton.button == Button3)
2854 return;
2855 wRaiseFrame(fwin->core);
2857 #endif
2860 static void
2861 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2863 WWindow *wwin = data;
2865 event->xbutton.state &= ValidModMask;
2867 CloseWindowMenu(wwin->screen_ptr);
2869 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2870 return;
2872 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2873 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2874 LastTimestamp);
2875 } else {
2876 WApplication *wapp;
2877 if ((event->xbutton.state & ControlMask) ||
2878 (event->xbutton.button == Button3)) {
2880 wapp = wApplicationOf(wwin->main_window);
2881 if (wapp && !WFLAGP(wwin, no_appicon))
2882 wHideApplication(wapp);
2883 } else if (event->xbutton.state==0) {
2884 wIconifyWindow(wwin);