change Clearance to ExtendSpace. Also add ExtendSpace option for menu
[wmaker-crm.git] / src / window.c
blobe5d6c435a3543e6508fa7b00681ad755da2a476d
1 /* window.c - client window managing stuffs
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
38 #include "WindowMaker.h"
39 #include "GNUstep.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "texture.h"
43 #include "window.h"
44 #include "winspector.h"
45 #include "icon.h"
46 #include "properties.h"
47 #include "actions.h"
48 #include "client.h"
49 #include "funcs.h"
50 #include "keybind.h"
51 #include "stacking.h"
52 #include "defaults.h"
53 #include "workspace.h"
56 #ifdef MWM_HINTS
57 # include "motif.h"
58 #endif
59 #ifdef KWM_HINTS
60 # include "kwm.h"
61 #endif
62 #ifdef GNOME_STUFF
63 # include "gnome.h"
64 #endif
65 #ifdef OLWM_HINTS
66 # include "openlook.h"
67 #endif
69 /****** Global Variables ******/
71 extern WShortKey wKeyBindings[WKBD_LAST];
73 #ifdef SHAPE
74 extern Bool wShapeSupported;
75 #endif
77 /* contexts */
78 extern XContext wWinContext;
80 /* cursors */
81 extern Cursor wCursor[WCUR_LAST];
83 /* protocol atoms */
84 extern Atom _XA_WM_DELETE_WINDOW;
85 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
87 extern Atom _XA_WINDOWMAKER_STATE;
89 extern WPreferences wPreferences;
91 #define MOD_MASK wPreferences.modifier_mask
93 extern Time LastTimestamp;
95 /* superfluous... */
96 extern void DoWindowBirth(WWindow*);
99 /***** Local Stuff *****/
102 static WWindowState *windowState=NULL;
106 /* local functions */
107 static FocusMode getFocusMode(WWindow *wwin);
109 static int getSavedState(Window window, WSavedState **state);
111 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
113 /* event handlers */
116 /* frame window (during window grabs) */
117 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
119 /* close button */
120 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
121 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
123 /* iconify button */
124 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
126 #ifdef XKB_BUTTON_HINT
127 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
128 #endif
130 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
131 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
133 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
136 /****** Notification Observers ******/
138 static void
139 appearanceObserver(void *self, WMNotification *notif)
141 WWindow *wwin = (WWindow*)self;
142 int flags = (int)WMGetNotificationClientData(notif);
144 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
145 return;
147 if (flags & WFontSettings) {
148 wWindowConfigureBorders(wwin);
149 if(wwin->flags.shaded) {
150 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
151 wwin->frame->top_width - 1);
153 wwin->client.y = wwin->frame_y - wwin->client.height
154 + wwin->frame->top_width;
155 wWindowSynthConfigureNotify(wwin);
158 if (flags & WTextureSettings) {
159 wwin->frame->flags.need_texture_remake = 1;
161 if (flags & (WTextureSettings | WColorSettings)) {
162 if (wwin->frame->titlebar)
163 XClearWindow(dpy, wwin->frame->titlebar->window);
165 wFrameWindowPaint(wwin->frame);
169 /************************************/
171 WWindow*
172 wWindowFor(Window window)
174 WObjDescriptor *desc;
176 if (window==None)
177 return NULL;
179 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
180 return NULL;
182 if (desc->parent_type==WCLASS_WINDOW)
183 return desc->parent;
184 else if (desc->parent_type==WCLASS_FRAME) {
185 WFrameWindow *frame = (WFrameWindow*)desc->parent;
186 if (frame->flags.is_client_window_frame)
187 return frame->child;
190 return NULL;
194 WWindow*
195 wWindowCreate()
197 WWindow *wwin;
199 wwin = wmalloc(sizeof(WWindow));
200 wretain(wwin);
202 memset(wwin, 0, sizeof(WWindow));
204 wwin->client_descriptor.handle_mousedown = frameMouseDown;
205 wwin->client_descriptor.parent = wwin;
206 wwin->client_descriptor.self = wwin;
207 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
209 return wwin;
213 void
214 wWindowDestroy(WWindow *wwin)
216 int i;
218 WMRemoveNotificationObserver(wwin);
220 wwin->flags.destroyed = 1;
222 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
223 if (!wwin->screen_ptr->shortcutWindows[i])
224 continue;
226 WMRemoveFromBag(wwin->screen_ptr->shortcutWindows[i], wwin);
228 if (!WMGetBagItemCount(wwin->screen_ptr->shortcutWindows[i])) {
229 WMFreeBag(wwin->screen_ptr->shortcutWindows[i]);
230 wwin->screen_ptr->shortcutWindows[i] = NULL;
234 if (wwin->normal_hints)
235 free(wwin->normal_hints);
237 if (wwin->wm_hints)
238 XFree(wwin->wm_hints);
240 if (wwin->wm_instance)
241 XFree(wwin->wm_instance);
243 if (wwin->wm_class)
244 XFree(wwin->wm_class);
246 if (wwin->wm_gnustep_attr)
247 free(wwin->wm_gnustep_attr);
249 if (wwin->cmap_windows)
250 XFree(wwin->cmap_windows);
252 XDeleteContext(dpy, wwin->client_win, wWinContext);
254 if (wwin->frame)
255 wFrameWindowDestroy(wwin->frame);
257 if (wwin->icon) {
258 RemoveFromStackList(wwin->icon->core);
259 wIconDestroy(wwin->icon);
260 if (wPreferences.auto_arrange_icons)
261 wArrangeIcons(wwin->screen_ptr, True);
263 wrelease(wwin);
269 static void
270 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
272 if (gs_hints->flags & GSWindowStyleAttr) {
273 wwin->client_flags.no_titlebar =
274 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
276 wwin->client_flags.no_close_button =
277 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
279 wwin->client_flags.no_closable =
280 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
282 wwin->client_flags.no_miniaturize_button =
283 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
285 wwin->client_flags.no_miniaturizable =
286 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
288 wwin->client_flags.no_resizebar =
289 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
291 wwin->client_flags.no_resizable =
292 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
293 } else {
294 /* setup the defaults */
295 wwin->client_flags.no_titlebar = 0;
296 wwin->client_flags.no_closable = 0;
297 wwin->client_flags.no_miniaturizable = 0;
298 wwin->client_flags.no_resizable = 0;
299 wwin->client_flags.no_close_button = 0;
300 wwin->client_flags.no_miniaturize_button = 0;
301 wwin->client_flags.no_resizebar = 0;
303 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
304 wwin->client_flags.no_appicon = 1;
309 void
310 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
311 WWindowAttributes *mask)
313 if (wflags->no_appicon && mask->no_appicon)
314 wflags->emulate_appicon = 0;
316 if (wwin->main_window!=None) {
317 WApplication *wapp = wApplicationOf(wwin->main_window);
318 if (wapp && !wapp->flags.emulated)
319 wflags->emulate_appicon = 0;
322 if (wwin->transient_for!=None
323 && wwin->transient_for!=wwin->screen_ptr->root_win)
324 wflags->emulate_appicon = 0;
326 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
327 wflags->sunken = 0;
332 void
333 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
335 WScreen *scr = wwin->screen_ptr;
337 /* sets global default stuff */
338 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
339 &wwin->client_flags, NULL, True);
341 * Decoration setting is done in this precedence (lower to higher)
342 * - use global default in the resource database
343 * - guess some settings
344 * - use GNUstep/external window attributes
345 * - set hints specified for the app in the resource DB
348 WSETUFLAG(wwin, broken_close, 0);
350 if (wwin->protocols.DELETE_WINDOW)
351 WSETUFLAG(wwin, kill_close, 0);
352 else
353 WSETUFLAG(wwin, kill_close, 1);
355 /* transients can't be iconified or maximized */
356 if (wwin->transient_for) {
357 WSETUFLAG(wwin, no_miniaturizable, 1);
358 WSETUFLAG(wwin, no_miniaturize_button, 1);
361 /* if the window can't be resized, remove the resizebar */
362 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
363 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
364 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
365 WSETUFLAG(wwin, no_resizable, 1);
366 WSETUFLAG(wwin, no_resizebar, 1);
369 /* set GNUstep window attributes */
370 if (wwin->wm_gnustep_attr) {
371 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
373 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
375 switch (wwin->wm_gnustep_attr->window_level) {
376 case WMNormalWindowLevel:
377 *level = WMNormalLevel;
378 break;
379 case WMFloatingWindowLevel:
380 *level = WMFloatingLevel;
381 break;
382 case WMDockWindowLevel:
383 *level = WMDockLevel;
384 break;
385 case WMSubmenuWindowLevel:
386 *level = WMSubmenuLevel;
387 break;
388 case WMMainMenuWindowLevel:
389 *level = WMMainMenuLevel;
390 break;
391 default:
392 *level = WMNormalLevel;
393 break;
395 } else {
396 /* setup defaults */
397 *level = WMNormalLevel;
399 } else {
400 int tmp_workspace = -1;
401 int tmp_level = -1;
403 #ifdef MWM_HINTS
404 wMWMCheckClientHints(wwin);
405 #endif /* MWM_HINTS */
407 #ifdef KWM_HINTS
408 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
409 #endif /* KWM_HINTS */
411 #ifdef GNOME_STUFF
412 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
413 #endif /* GNOME_STUFF */
415 #ifdef OLWM_HINTS
416 wOLWMCheckClientHints(wwin);
417 #endif /* OLWM_HINTS */
419 if (tmp_level < 0) {
420 if (WFLAGP(wwin, floating))
421 *level = WMFloatingLevel;
422 else if (WFLAGP(wwin, sunken))
423 *level = WMSunkenLevel;
424 else
425 *level = WMNormalLevel;
426 } else {
427 *level = tmp_level;
430 if (tmp_workspace >= 0) {
431 *workspace = tmp_workspace % scr->workspace_count;
436 * Set attributes specified only for that window/class.
437 * This might do duplicate work with the 1st wDefaultFillAttributes().
439 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
440 &wwin->user_flags, &wwin->defined_user_flags,
441 False);
443 * Sanity checks for attributes that depend on other attributes
445 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
446 wwin->user_flags.emulate_appicon = 0;
448 if (wwin->main_window!=None) {
449 WApplication *wapp = wApplicationOf(wwin->main_window);
450 if (wapp && !wapp->flags.emulated)
451 wwin->user_flags.emulate_appicon = 0;
454 if (wwin->transient_for!=None
455 && wwin->transient_for!=wwin->screen_ptr->root_win)
456 wwin->user_flags.emulate_appicon = 0;
458 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
459 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
460 wwin->user_flags.sunken = 0;
462 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
468 Bool
469 wWindowCanReceiveFocus(WWindow *wwin)
471 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
472 return False;
473 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
474 return False;
475 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
476 return False;
478 return True;
482 Bool
483 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
485 int w1, h1, w2, h2;
487 w1 = wwin->frame->core->width;
488 h1 = wwin->frame->core->height;
489 w2 = obscured->frame->core->width;
490 h2 = obscured->frame->core->height;
492 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
493 && wwin->frame->workspace != obscured->frame->workspace)
494 return False;
496 if (wwin->frame_x + w1 < obscured->frame_x
497 || wwin->frame_y + h1 < obscured->frame_y
498 || wwin->frame_x > obscured->frame_x + w2
499 || wwin->frame_y > obscured->frame_y + h2) {
500 return False;
503 return True;
508 *----------------------------------------------------------------
509 * wManageWindow--
510 * reparents the window and allocates a descriptor for it.
511 * Window manager hints and other hints are fetched to configure
512 * the window decoration attributes and others. User preferences
513 * for the window are used if available, to configure window
514 * decorations and some behaviour.
515 * If in startup, windows that are override redirect,
516 * unmapped and never were managed and are Withdrawn are not
517 * managed.
519 * Returns:
520 * the new window descriptor
522 * Side effects:
523 * The window is reparented and appropriate notification
524 * is done to the client. Input mask for the window is setup.
525 * The window descriptor is also associated with various window
526 * contexts and inserted in the head of the window list.
527 * Event handler contexts are associated for some objects
528 * (buttons, titlebar and resizebar)
530 *----------------------------------------------------------------
532 WWindow*
533 wManageWindow(WScreen *scr, Window window)
535 WWindow *wwin;
536 int x, y;
537 unsigned width, height;
538 XWindowAttributes wattribs;
539 XSetWindowAttributes attribs;
540 WWindowState *win_state;
541 WWindow *transientOwner = NULL;
542 int window_level;
543 int wm_state;
544 int foo;
545 int workspace = -1;
546 char *title;
547 Bool withdraw = False;
549 /* mutex. */
550 /* XGrabServer(dpy); */
551 XSync(dpy, False);
552 /* make sure the window is still there */
553 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
554 XUngrabServer(dpy);
555 return NULL;
558 /* if it's an override-redirect, ignore it */
559 if (wattribs.override_redirect) {
560 XUngrabServer(dpy);
561 return NULL;
564 wm_state = PropGetWindowState(window);
566 /* if it's startup and the window is unmapped, don't manage it */
567 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
568 XUngrabServer(dpy);
569 return NULL;
572 if (!wFetchName(dpy, window, &title)) {
573 title = NULL;
576 #ifdef KWM_HINTS
577 if (title && !wKWMManageableClient(scr, window, title)) {
578 XFree(title);
579 XUngrabServer(dpy);
580 return NULL;
582 #endif /* KWM_HINTS */
585 wwin = wWindowCreate();
587 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
589 #ifdef DEBUG
590 printf("managing window %x\n", (unsigned)window);
591 #endif
593 #ifdef SHAPE
594 if (wShapeSupported) {
595 int junk;
596 unsigned int ujunk;
597 int b_shaped;
599 XShapeSelectInput(dpy, window, ShapeNotifyMask);
600 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
601 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
602 wwin->flags.shaped = b_shaped;
604 #endif
607 *--------------------------------------------------
609 * Get hints and other information in properties
611 *--------------------------------------------------
613 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
615 /* setup descriptor */
616 wwin->client_win = window;
617 wwin->screen_ptr = scr;
619 wwin->old_border_width = wattribs.border_width;
621 wwin->event_mask = CLIENT_EVENTS;
622 attribs.event_mask = CLIENT_EVENTS;
623 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
624 attribs.save_under = False;
625 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
626 |CWSaveUnder, &attribs);
627 XSetWindowBorderWidth(dpy, window, 0);
629 /* get hints from GNUstep app */
630 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
631 wwin->wm_gnustep_attr = NULL;
634 wwin->client_leader = PropGetClientLeader(window);
635 if (wwin->client_leader!=None)
636 wwin->main_window = wwin->client_leader;
638 wwin->wm_hints = XGetWMHints(dpy, window);
640 if (wwin->wm_hints) {
641 if (wwin->wm_hints->flags & StateHint) {
643 if (wwin->wm_hints->initial_state == IconicState) {
645 wwin->flags.miniaturized = 1;
647 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
649 withdraw = True;
653 if (wwin->wm_hints->flags & WindowGroupHint) {
654 wwin->group_id = wwin->wm_hints->window_group;
655 /* window_group has priority over CLIENT_LEADER */
656 wwin->main_window = wwin->group_id;
657 } else {
658 wwin->group_id = None;
661 if (wwin->wm_hints->flags & UrgencyHint)
662 wwin->flags.urgent = 1;
663 } else {
664 wwin->group_id = None;
667 PropGetProtocols(window, &wwin->protocols);
669 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
670 wwin->transient_for = None;
671 } else {
672 if (wwin->transient_for==None || wwin->transient_for==window) {
673 wwin->transient_for = scr->root_win;
674 } else {
675 transientOwner = wWindowFor(wwin->transient_for);
676 if (transientOwner && transientOwner->main_window!=None) {
677 wwin->main_window = transientOwner->main_window;
678 } /*else {
679 wwin->main_window = None;
684 /* guess the focus mode */
685 wwin->focus_mode = getFocusMode(wwin);
687 /* get geometry stuff */
688 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
690 /* get colormap windows */
691 GetColormapWindows(wwin);
694 *--------------------------------------------------
696 * Setup the decoration/window attributes and
697 * geometry
699 *--------------------------------------------------
702 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
704 #ifdef OLWM_HINTS
705 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
706 && wwin->group_id != None && wwin->group_id != window) {
708 transientOwner = wWindowFor(wwin->group_id);
710 if (transientOwner) {
711 wwin->transient_for = wwin->group_id;
713 /* transients can't be iconified or maximized */
714 if (wwin->transient_for) {
715 WSETUFLAG(wwin, no_miniaturizable, 1);
716 WSETUFLAG(wwin, no_miniaturize_button, 1);
720 #endif /* OLWM_HINTS */
723 * Make broken apps behave as a nice app.
725 if (WFLAGP(wwin, emulate_appicon)) {
726 wwin->main_window = wwin->client_win;
730 *------------------------------------------------------------
732 * Setup the initial state of the window
734 *------------------------------------------------------------
737 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
738 wwin->flags.miniaturized = 1;
741 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
742 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
745 #ifdef GNOME_STUFF
746 wGNOMECheckInitialClientState(wwin);
747 #endif
748 #ifdef KWM_HINTS
749 wKWMCheckClientInitialState(wwin);
750 #endif
752 /* apply previous state if it exists and we're in startup */
753 if (scr->flags.startup && wm_state >= 0) {
755 if (wm_state == IconicState) {
757 wwin->flags.miniaturized = 1;
759 } else if (wm_state == WithdrawnState) {
761 withdraw = True;
765 /* if there is a saved state (from file), restore it */
766 win_state = NULL;
767 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
768 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
769 } else {
770 win_state = (WWindowState*)wWindowGetSavedState(window);
772 if (win_state && !withdraw) {
774 if (win_state->state->hidden>0)
775 wwin->flags.hidden = win_state->state->hidden;
777 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
778 wwin->flags.shaded = win_state->state->shaded;
780 if (win_state->state->miniaturized>0 &&
781 !WFLAGP(wwin, no_miniaturizable)) {
782 wwin->flags.miniaturized = win_state->state->miniaturized;
784 if (!IS_OMNIPRESENT(wwin)) {
785 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
786 wwin->wm_class);
787 if (w < 0 || w >= scr->workspace_count) {
788 workspace = win_state->state->workspace;
789 if (workspace >= scr->workspace_count)
790 workspace = scr->current_workspace;
791 } else {
792 workspace = w;
794 } else {
795 workspace = scr->current_workspace;
799 /* if we're restarting, restore saved state (from hints).
800 * This will overwrite previous */
802 WSavedState *wstate;
804 if (getSavedState(window, &wstate)) {
805 wwin->flags.shaded = wstate->shaded;
806 wwin->flags.hidden = wstate->hidden;
807 wwin->flags.miniaturized = wstate->miniaturized;
808 workspace = wstate->workspace;
810 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
811 int i;
813 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
814 if (wstate->window_shortcuts & (1<<i)) {
815 if (!scr->shortcutWindows[i])
816 scr->shortcutWindows[i] = WMCreateBag(4);
818 WMPutInBag(scr->shortcutWindows[i], wwin);
822 free(wstate);
826 /* don't let transients start miniaturized if their owners are not */
827 if (transientOwner && !transientOwner->flags.miniaturized
828 && wwin->flags.miniaturized && !withdraw) {
829 wwin->flags.miniaturized = 0;
830 if (wwin->wm_hints)
831 wwin->wm_hints->initial_state = NormalState;
834 /* set workspace on which the window starts */
835 if (workspace >= 0) {
836 if (workspace > scr->workspace_count-1) {
837 workspace = workspace % scr->workspace_count;
839 } else {
840 int w;
842 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
844 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
846 workspace = w;
848 } else {
849 if (wPreferences.open_transients_with_parent && transientOwner) {
851 workspace = transientOwner->frame->workspace;
853 } else {
855 workspace = scr->current_workspace;
860 /* setup window geometry */
861 if (win_state && win_state->state->use_geometry) {
862 width = win_state->state->w;
863 height = win_state->state->h;
865 wWindowConstrainSize(wwin, &width, &height);
867 /* do not ask for window placement if the window is
868 * transient, during startup, if the initial workspace is another one
869 * or if the window wants to start iconic.
870 * If geometry was saved, restore it. */
872 Bool dontBring = False;
874 if (win_state && win_state->state->use_geometry) {
875 x = win_state->state->x;
876 y = win_state->state->y;
877 } else if ((wwin->transient_for==None
878 || wPreferences.window_placement!=WPM_MANUAL)
879 && !scr->flags.startup
880 && workspace == scr->current_workspace
881 && !wwin->flags.miniaturized
882 && !wwin->flags.maximized
883 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
884 PlaceWindow(wwin, &x, &y, width, height);
885 if (wPreferences.window_placement == WPM_MANUAL)
886 dontBring = True;
889 if (WFLAGP(wwin, dont_move_off) && dontBring)
890 wScreenBringInside(scr, &x, &y, width, height);
893 if (wwin->flags.urgent) {
894 if (!IS_OMNIPRESENT(wwin))
895 wwin->flags.omnipresent ^= 1;
899 *--------------------------------------------------
901 * Create frame, borders and do reparenting
903 *--------------------------------------------------
905 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
906 #ifdef XKB_BUTTON_HINT
907 if (wPreferences.modelock)
908 foo |= WFF_LANGUAGE_BUTTON;
909 #endif
910 if (!WFLAGP(wwin, no_titlebar))
911 foo |= WFF_TITLEBAR;
912 if (!WFLAGP(wwin, no_resizebar))
913 foo |= WFF_RESIZEBAR;
915 wwin->frame = wFrameWindowCreate(scr, window_level,
916 x, y, width, height,
917 &wPreferences.window_title_clearance, foo,
918 scr->window_title_texture,
919 scr->resizebar_texture,
920 scr->window_title_pixel,
921 #ifdef DRAWSTRING_PLUGIN
922 W_STRING_FTITLE,
923 #endif
924 &scr->window_title_gc,
925 &scr->title_font);
927 wwin->frame->flags.is_client_window_frame = 1;
928 wwin->frame->flags.justification = wPreferences.title_justification;
930 /* setup button images */
931 wWindowUpdateButtonImages(wwin);
933 /* hide unused buttons */
934 foo = 0;
935 if (WFLAGP(wwin, no_close_button))
936 foo |= WFF_RIGHT_BUTTON;
937 if (WFLAGP(wwin, no_miniaturize_button))
938 foo |= WFF_LEFT_BUTTON;
939 #ifdef XKB_BUTTON_HINT
940 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
941 foo |= WFF_LANGUAGE_BUTTON;
942 #endif
943 if (foo!=0)
944 wFrameWindowHideButton(wwin->frame, foo);
946 wwin->frame->child = wwin;
948 #ifdef OLWM_HINTS
949 /* emulate olwm push pin. Make the button look as pushed-in for
950 * the pinned-out state. When the button is clicked, it will
951 * revert to the normal position, which means the pin is pinned-in.
953 if (wwin->flags.olwm_push_pin_out)
954 wFrameWindowUpdatePushButton(wwin->frame, True);
955 #endif /* OLWM_HINTS */
957 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
958 if (title)
959 XFree(title);
961 wwin->frame->workspace = workspace;
963 wwin->frame->on_click_left = windowIconifyClick;
964 #ifdef XKB_BUTTON_HINT
965 if (wPreferences.modelock)
966 wwin->frame->on_click_language = windowLanguageClick;
967 #endif
969 wwin->frame->on_click_right = windowCloseClick;
970 wwin->frame->on_dblclick_right = windowCloseDblClick;
972 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
973 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
975 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
978 XSelectInput(dpy, wwin->client_win,
979 wwin->event_mask & ~StructureNotifyMask);
981 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
982 0, wwin->frame->top_width);
984 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
988 int gx, gy;
990 wClientGetGravityOffsets(wwin, &gx, &gy);
992 /* if gravity is to the south, account for the border sizes */
993 if (gy > 0)
994 y -= wwin->frame->top_width + wwin->frame->bottom_width;
998 * wWindowConfigure() will init the client window's size
999 * (wwin->client.{width,height}) and all other geometry
1000 * related variables (frame_x,frame_y)
1002 wWindowConfigure(wwin, x, y, width, height);
1004 /* to make sure the window receives it's new position after reparenting */
1005 wWindowSynthConfigureNotify(wwin);
1008 *--------------------------------------------------
1010 * Setup descriptors and save window to internal
1011 * lists
1013 *--------------------------------------------------
1016 if (wwin->main_window!=None) {
1017 WApplication *app;
1018 WWindow *leader;
1020 /* Leader windows do not necessary set themselves as leaders.
1021 * If this is the case, point the leader of this window to
1022 * itself */
1023 leader = wWindowFor(wwin->main_window);
1024 if (leader && leader->main_window==None) {
1025 leader->main_window = leader->client_win;
1027 app = wApplicationCreate(scr, wwin->main_window);
1028 if (app) {
1029 app->last_workspace = workspace;
1032 * Do application specific stuff, like setting application
1033 * wide attributes.
1036 if (wwin->flags.hidden) {
1037 /* if the window was set to hidden because it was hidden
1038 * in a previous incarnation and that state was restored */
1039 app->flags.hidden = 1;
1042 if (app->flags.hidden) {
1043 wwin->flags.hidden = 1;
1048 /* setup the frame descriptor */
1049 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1050 wwin->frame->core->descriptor.parent = wwin;
1051 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1053 /* don't let windows go away if we die */
1054 XAddToSaveSet(dpy, window);
1056 XLowerWindow(dpy, window);
1058 /* if window is in this workspace and should be mapped, then map it */
1059 if (!wwin->flags.miniaturized
1060 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1061 && !wwin->flags.hidden && !withdraw) {
1063 /* The following "if" is to avoid crashing of clients that expect
1064 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1065 * after the return from this function.
1067 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1068 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1069 } else {
1070 wClientSetState(wwin, NormalState, None);
1073 #if 0
1074 /* if not auto focus, then map the window under the currently
1075 * focused window */
1076 #define _WIDTH(w) (w)->frame->core->width
1077 #define _HEIGHT(w) (w)->frame->core->height
1078 if (!wPreferences.auto_focus && scr->focused_window
1079 && !scr->flags.startup && !transientOwner
1080 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1081 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1082 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1083 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1084 || wwin->flags.maximized)) {
1085 MoveInStackListUnder(scr->focused_window->frame->core,
1086 wwin->frame->core);
1088 #undef _WIDTH
1089 #undef _HEIGHT
1091 #endif
1093 if (wPreferences.superfluous && !wPreferences.no_animations
1094 && !scr->flags.startup && wwin->transient_for==None
1096 * The brain damaged idiotic non-click to focus modes will
1097 * have trouble with this because:
1099 * 1. window is created and mapped by the client
1100 * 2. window is mapped by wmaker in small size
1101 * 3. window is animated to grow to normal size
1102 * 4. this function returns to normal event loop
1103 * 5. eventually, the EnterNotify event that would trigger
1104 * the window focusing (if the mouse is over that window)
1105 * will be processed by wmaker.
1106 * But since this event will be rather delayed
1107 * (step 3 has a large delay) the time when the event ocurred
1108 * and when it is processed, the client that owns that window
1109 * will reject the XSetInputFocus() for it.
1111 && (wPreferences.focus_mode==WKF_CLICK
1112 || wPreferences.auto_focus)) {
1113 DoWindowBirth(wwin);
1116 wWindowMap(wwin);
1119 /* setup stacking descriptor */
1120 if (transientOwner) {
1121 /* && wPreferences.on_top_transients */
1122 if (transientOwner) {
1123 wwin->frame->core->stacking->child_of =
1124 transientOwner->frame->core;
1126 } else {
1127 wwin->frame->core->stacking->child_of = NULL;
1131 if (!scr->focused_window) {
1132 /* first window on the list */
1133 wwin->next = NULL;
1134 wwin->prev = NULL;
1135 scr->focused_window = wwin;
1136 } else {
1137 WWindow *tmp;
1139 /* add window at beginning of focus window list */
1140 tmp = scr->focused_window;
1141 while (tmp->prev)
1142 tmp = tmp->prev;
1143 tmp->prev = wwin;
1144 wwin->next = tmp;
1145 wwin->prev = NULL;
1148 #ifdef GNOME_STUFF
1149 wGNOMEUpdateClientStateHint(wwin, True);
1150 #endif
1151 #ifdef KWM_HINTS
1152 wKWMUpdateClientWorkspace(wwin);
1153 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1154 #endif
1156 XUngrabServer(dpy);
1159 *--------------------------------------------------
1161 * Final preparations before window is ready to go
1163 *--------------------------------------------------
1166 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1169 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1170 && !wwin->flags.hidden) {
1171 if (((transientOwner && transientOwner->flags.focused)
1172 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1173 wSetFocusTo(scr, wwin);
1175 wWindowResetMouseGrabs(wwin);
1177 if (!WFLAGP(wwin, no_bind_keys)) {
1178 wWindowSetKeyGrabs(wwin);
1180 #ifdef GNOME_STUFF
1181 wGNOMEUpdateClientListHint(scr);
1182 #endif
1183 #ifdef KWM_HINTS
1184 wwin->flags.kwm_managed = 1;
1186 wKWMSendEventMessage(wwin, WKWMAddWindow);
1187 #endif
1189 wColormapInstallForWindow(scr, scr->cmap_window);
1191 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1193 #ifdef OLWM_HINTS
1194 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1195 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1197 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1198 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1199 wwin->frame_y);
1201 #endif
1204 *------------------------------------------------------------
1205 * Setup Notification Observers
1206 *------------------------------------------------------------
1208 WMAddNotificationObserver(appearanceObserver, wwin,
1209 WNWindowAppearanceSettingsChanged, wwin);
1213 *--------------------------------------------------
1215 * Cleanup temporary stuff
1217 *--------------------------------------------------
1220 if (win_state)
1221 wWindowDeleteSavedState(win_state);
1223 /* If the window must be withdrawed, then do it now.
1224 * Must do some optimization, 'though */
1225 if (withdraw) {
1226 wwin->flags.mapped = 0;
1227 wClientSetState(wwin, WithdrawnState, None);
1228 wUnmanageWindow(wwin, True, False);
1229 wwin = NULL;
1232 return wwin;
1239 WWindow*
1240 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1241 char *title, int x, int y, int width, int height)
1243 WWindow *wwin;
1244 int foo;
1246 wwin = wWindowCreate();
1248 WMAddNotificationObserver(appearanceObserver, wwin,
1249 WNWindowAppearanceSettingsChanged, wwin);
1251 wwin->flags.internal_window = 1;
1253 WSETUFLAG(wwin, omnipresent, 1);
1254 WSETUFLAG(wwin, no_shadeable, 1);
1255 WSETUFLAG(wwin, no_resizable, 1);
1256 WSETUFLAG(wwin, no_miniaturizable, 1);
1258 wwin->focus_mode = WFM_PASSIVE;
1260 wwin->client_win = window;
1261 wwin->screen_ptr = scr;
1263 wwin->transient_for = owner;
1265 wwin->client.x = x;
1266 wwin->client.y = y;
1267 wwin->client.width = width;
1268 wwin->client.height = height;
1270 wwin->frame_x = wwin->client.x;
1271 wwin->frame_y = wwin->client.y;
1274 foo = WFF_RIGHT_BUTTON;
1275 foo |= WFF_TITLEBAR;
1276 #ifdef XKB_BUTTON_HINT
1277 foo |= WFF_LANGUAGE_BUTTON;
1278 #endif
1280 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1281 wwin->frame_x, wwin->frame_y,
1282 width, height,
1283 &wPreferences.window_title_clearance, foo,
1284 scr->window_title_texture,
1285 scr->resizebar_texture,
1286 scr->window_title_pixel,
1287 #ifdef DRAWSTRING_PLUGIN
1288 W_STRING_FTITLE,
1289 #endif
1290 &scr->window_title_gc,
1291 &scr->title_font);
1293 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1295 wwin->frame->flags.is_client_window_frame = 1;
1296 wwin->frame->flags.justification = wPreferences.title_justification;
1298 wFrameWindowChangeTitle(wwin->frame, title);
1300 /* setup button images */
1301 wWindowUpdateButtonImages(wwin);
1303 /* hide buttons */
1304 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1306 wwin->frame->child = wwin;
1308 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1310 #ifdef XKB_BUTTON_HINT
1311 if (wPreferences.modelock)
1312 wwin->frame->on_click_language = windowLanguageClick;
1313 #endif
1315 wwin->frame->on_click_right = windowCloseClick;
1317 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1318 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1320 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1322 wwin->client.y += wwin->frame->top_width;
1323 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1324 0, wwin->frame->top_width);
1326 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1327 wwin->client.width, wwin->client.height);
1329 /* setup the frame descriptor */
1330 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1331 wwin->frame->core->descriptor.parent = wwin;
1332 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1335 XLowerWindow(dpy, window);
1336 XMapSubwindows(dpy, wwin->frame->core->window);
1338 /* setup stacking descriptor */
1339 if (
1340 #ifdef removed
1341 wPreferences.on_top_transients &&
1342 #endif
1343 wwin->transient_for!=None
1344 && wwin->transient_for!=scr->root_win) {
1345 WWindow *tmp;
1346 tmp = wWindowFor(wwin->transient_for);
1347 if (tmp)
1348 wwin->frame->core->stacking->child_of = tmp->frame->core;
1349 } else {
1350 wwin->frame->core->stacking->child_of = NULL;
1354 if (!scr->focused_window) {
1355 /* first window on the list */
1356 wwin->next = NULL;
1357 wwin->prev = NULL;
1358 scr->focused_window = wwin;
1359 } else {
1360 WWindow *tmp;
1362 /* add window at beginning of focus window list */
1363 tmp = scr->focused_window;
1364 while (tmp->prev)
1365 tmp = tmp->prev;
1366 tmp->prev = wwin;
1367 wwin->next = tmp;
1368 wwin->prev = NULL;
1371 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1373 /* if (wPreferences.auto_focus)*/
1374 wSetFocusTo(scr, wwin);
1376 wWindowResetMouseGrabs(wwin);
1378 wWindowSetKeyGrabs(wwin);
1380 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1382 return wwin;
1387 *----------------------------------------------------------------------
1388 * wUnmanageWindow--
1389 * Removes the frame window from a window and destroys all data
1390 * related to it. The window will be reparented back to the root window
1391 * if restore is True.
1393 * Side effects:
1394 * Everything related to the window is destroyed and the window
1395 * is removed from the window lists. Focus is set to the previous on the
1396 * window list.
1397 *----------------------------------------------------------------------
1399 void
1400 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1402 WCoreWindow *frame = wwin->frame->core;
1403 WWindow *owner = NULL;
1404 WWindow *newFocusedWindow = NULL;
1405 int wasFocused;
1406 WScreen *scr = wwin->screen_ptr;
1409 #ifdef KWM_HINTS
1410 wwin->frame->workspace = -1;
1412 wKWMUpdateClientWorkspace(wwin);
1413 #endif
1415 /* First close attribute editor window if open */
1416 if (wwin->flags.inspector_open) {
1417 wCloseInspectorForWindow(wwin);
1420 /* Close window menu if it's open for this window */
1421 if (wwin->flags.menu_open_for_me) {
1422 CloseWindowMenu(scr);
1425 if (!destroyed) {
1426 if (!wwin->flags.internal_window)
1427 XRemoveFromSaveSet(dpy, wwin->client_win);
1429 XSelectInput(dpy, wwin->client_win, NoEventMask);
1431 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1432 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1435 XUnmapWindow(dpy, frame->window);
1437 XUnmapWindow(dpy, wwin->client_win);
1439 /* deselect window */
1440 wSelectWindow(wwin, False);
1442 /* remove all pending events on window */
1443 /* I think this only matters for autoraise */
1444 if (wPreferences.raise_delay)
1445 WMDeleteTimerWithClientData(wwin->frame->core);
1447 XFlush(dpy);
1449 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1451 /* reparent the window back to the root */
1452 if (restore)
1453 wClientRestore(wwin);
1455 if (wwin->transient_for!=scr->root_win) {
1456 owner = wWindowFor(wwin->transient_for);
1457 if (owner) {
1458 if (!owner->flags.semi_focused) {
1459 owner = NULL;
1460 } else {
1461 owner->flags.semi_focused = 0;
1466 wasFocused = wwin->flags.focused;
1468 /* remove from window focus list */
1469 if (!wwin->prev && !wwin->next) {
1470 /* was the only window */
1471 scr->focused_window = NULL;
1472 newFocusedWindow = NULL;
1473 } else {
1474 WWindow *tmp;
1476 if (wwin->prev)
1477 wwin->prev->next = wwin->next;
1478 if (wwin->next)
1479 wwin->next->prev = wwin->prev;
1480 else {
1481 scr->focused_window = wwin->prev;
1482 scr->focused_window->next = NULL;
1485 if (wPreferences.focus_mode==WKF_CLICK) {
1487 /* if in click to focus mode and the window
1488 * was a transient, focus the owner window
1490 tmp = NULL;
1491 if (wPreferences.focus_mode==WKF_CLICK) {
1492 tmp = wWindowFor(wwin->transient_for);
1493 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1494 tmp = NULL;
1497 /* otherwise, focus the next one in the focus list */
1498 if (!tmp) {
1499 tmp = scr->focused_window;
1500 while (tmp) { /* look for one in the window list first */
1501 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1502 && (tmp->flags.mapped || tmp->flags.shaded))
1503 break;
1504 tmp = tmp->prev;
1506 if (!tmp) { /* if unsuccessful, choose any focusable window */
1507 tmp = scr->focused_window;
1508 while (tmp) {
1509 if (!WFLAGP(tmp, no_focusable)
1510 && (tmp->flags.mapped || tmp->flags.shaded))
1511 break;
1512 tmp = tmp->prev;
1517 newFocusedWindow = tmp;
1519 } else if (wPreferences.focus_mode==WKF_SLOPPY
1520 || wPreferences.focus_mode==WKF_POINTER) {
1521 unsigned int mask;
1522 int foo;
1523 Window bar, win;
1525 /* This is to let the root window get the keyboard input
1526 * if Sloppy focus mode and no other window get focus.
1527 * This way keybindings will not freeze.
1529 tmp = NULL;
1530 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1531 &foo, &foo, &foo, &foo, &mask))
1532 tmp = wWindowFor(win);
1533 if (tmp == wwin)
1534 tmp = NULL;
1535 newFocusedWindow = tmp;
1536 } else {
1537 newFocusedWindow = NULL;
1541 if (!wwin->flags.internal_window) {
1542 #ifdef GNOME_STUFF
1543 wGNOMERemoveClient(wwin);
1544 #endif
1545 #ifdef KWM_HINTS
1546 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1547 #endif
1550 #ifdef DEBUG
1551 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1552 (unsigned)frame->window);
1553 #endif
1555 if (wasFocused) {
1556 if (newFocusedWindow != owner && owner) {
1557 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1559 wSetFocusTo(scr, newFocusedWindow);
1561 wWindowDestroy(wwin);
1562 XFlush(dpy);
1566 void
1567 wWindowMap(WWindow *wwin)
1569 XMapWindow(dpy, wwin->frame->core->window);
1570 if (!wwin->flags.shaded) {
1571 /* window will be remapped when getting MapNotify */
1572 XSelectInput(dpy, wwin->client_win,
1573 wwin->event_mask & ~StructureNotifyMask);
1574 XMapWindow(dpy, wwin->client_win);
1575 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1577 wwin->flags.mapped = 1;
1582 void
1583 wWindowUnmap(WWindow *wwin)
1585 wwin->flags.mapped = 0;
1587 /* prevent window withdrawal when getting UnmapNotify */
1588 XSelectInput(dpy, wwin->client_win,
1589 wwin->event_mask & ~StructureNotifyMask);
1590 XUnmapWindow(dpy, wwin->client_win);
1591 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1593 XUnmapWindow(dpy, wwin->frame->core->window);
1598 void
1599 wWindowFocus(WWindow *wwin, WWindow *owin)
1601 WWindow *nowner;
1602 WWindow *oowner;
1604 #ifdef KEEP_XKB_LOCK_STATUS
1605 if (wPreferences.modelock) {
1606 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1608 #endif /* KEEP_XKB_LOCK_STATUS */
1610 wwin->flags.semi_focused = 0;
1612 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1614 wwin->flags.focused = 1;
1616 wWindowResetMouseGrabs(wwin);
1618 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1620 if (owin == wwin || !owin)
1621 return;
1623 nowner = wWindowFor(wwin->transient_for);
1625 /* new window is a transient for the old window */
1626 if (nowner == owin) {
1627 owin->flags.semi_focused = 1;
1628 wWindowUnfocus(nowner);
1629 return;
1632 oowner = wWindowFor(owin->transient_for);
1634 /* new window is owner of old window */
1635 if (wwin == oowner) {
1636 wWindowUnfocus(owin);
1637 return;
1640 if (!nowner) {
1641 wWindowUnfocus(owin);
1642 return;
1645 /* new window has same owner of old window */
1646 if (oowner == nowner) {
1647 /* prevent unfocusing of owner */
1648 oowner->flags.semi_focused = 0;
1649 wWindowUnfocus(owin);
1650 oowner->flags.semi_focused = 1;
1652 return;
1655 /* nowner != NULL && oowner != nowner */
1656 nowner->flags.semi_focused = 1;
1657 wWindowUnfocus(nowner);
1658 wWindowUnfocus(owin);
1662 void
1663 wWindowUnfocus(WWindow *wwin)
1665 CloseWindowMenu(wwin->screen_ptr);
1667 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1668 ? WS_PFOCUSED : WS_UNFOCUSED);
1670 if (wwin->transient_for!=None
1671 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1672 WWindow *owner;
1673 owner = wWindowFor(wwin->transient_for);
1674 if (owner && owner->flags.semi_focused) {
1675 owner->flags.semi_focused = 0;
1676 if (owner->flags.mapped || owner->flags.shaded) {
1677 wWindowUnfocus(owner);
1678 wFrameWindowPaint(owner->frame);
1682 wwin->flags.focused = 0;
1684 wWindowResetMouseGrabs(wwin);
1686 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1693 *----------------------------------------------------------------------
1695 * wWindowConstrainSize--
1696 * Constrains size for the client window, taking the maximal size,
1697 * window resize increments and other size hints into account.
1699 * Returns:
1700 * The closest size to what was given that the client window can
1701 * have.
1703 *----------------------------------------------------------------------
1705 void
1706 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1708 int width = *nwidth;
1709 int height = *nheight;
1710 int winc = 1;
1711 int hinc = 1;
1712 int minW = 1, minH = 1;
1713 int maxW = wwin->screen_ptr->scr_width*2;
1714 int maxH = wwin->screen_ptr->scr_height*2;
1715 int minAX = -1, minAY = -1;
1716 int maxAX = -1, maxAY = -1;
1717 int baseW = 0;
1718 int baseH = 0;
1720 if (wwin->normal_hints) {
1721 winc = wwin->normal_hints->width_inc;
1722 hinc = wwin->normal_hints->height_inc;
1723 minW = wwin->normal_hints->min_width;
1724 minH = wwin->normal_hints->min_height;
1725 maxW = wwin->normal_hints->max_width;
1726 maxH = wwin->normal_hints->max_height;
1727 if (wwin->normal_hints->flags & PAspect) {
1728 minAX = wwin->normal_hints->min_aspect.x;
1729 minAY = wwin->normal_hints->min_aspect.y;
1730 maxAX = wwin->normal_hints->max_aspect.x;
1731 maxAY = wwin->normal_hints->max_aspect.y;
1735 if (width < minW)
1736 width = minW;
1737 if (height < minH)
1738 height = minH;
1740 if (width > maxW)
1741 width = maxW;
1742 if (height > maxH)
1743 height = maxH;
1745 /* aspect ratio code borrowed from olwm */
1746 if (minAX > 0) {
1747 /* adjust max aspect ratio */
1748 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1749 if (maxAX > maxAY) {
1750 height = (width * maxAY) / maxAX;
1751 if (height > maxH) {
1752 height = maxH;
1753 width = (height * maxAX) / maxAY;
1755 } else {
1756 width = (height * maxAX) / maxAY;
1757 if (width > maxW) {
1758 width = maxW;
1759 height = (width * maxAY) / maxAX;
1764 /* adjust min aspect ratio */
1765 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1766 if (minAX > minAY) {
1767 height = (width * minAY) / minAX;
1768 if (height < minH) {
1769 height = minH;
1770 width = (height * minAX) / minAY;
1772 } else {
1773 width = (height * minAX) / minAY;
1774 if (width < minW) {
1775 width = minW;
1776 height = (width * minAY) / minAX;
1782 if (baseW != 0) {
1783 width = (((width - baseW) / winc) * winc) + baseW;
1784 } else {
1785 width = (((width - minW) / winc) * winc) + minW;
1788 if (baseW != 0) {
1789 height = (((height - baseH) / hinc) * hinc) + baseH;
1790 } else {
1791 height = (((height - minH) / hinc) * hinc) + minH;
1794 *nwidth = width;
1795 *nheight = height;
1799 void
1800 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1802 WScreen *scr = wwin->screen_ptr;
1803 WApplication *wapp;
1804 int unmap = 0;
1806 if (workspace >= scr->workspace_count || workspace < 0
1807 || workspace == wwin->frame->workspace)
1808 return;
1810 if (workspace != scr->current_workspace) {
1811 /* Sent to other workspace. Unmap window */
1812 if ((wwin->flags.mapped
1813 || wwin->flags.shaded
1814 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1815 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1817 wapp = wApplicationOf(wwin->main_window);
1818 if (wapp) {
1819 wapp->last_workspace = workspace;
1821 if (wwin->flags.miniaturized) {
1822 if (wwin->icon) {
1823 XUnmapWindow(dpy, wwin->icon->core->window);
1824 wwin->icon->mapped = 0;
1826 } else {
1827 unmap = 1;
1828 wSetFocusTo(scr, NULL);
1831 } else {
1832 /* brought to current workspace. Map window */
1833 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1834 if (wwin->icon) {
1835 XMapWindow(dpy, wwin->icon->core->window);
1836 wwin->icon->mapped = 1;
1838 } else if (!wwin->flags.mapped &&
1839 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1840 wWindowMap(wwin);
1843 if (!IS_OMNIPRESENT(wwin)) {
1844 wwin->frame->workspace = workspace;
1845 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1847 #ifdef GNOME_STUFF
1848 wGNOMEUpdateClientStateHint(wwin, True);
1849 #endif
1850 #ifdef KWM_HINTS
1851 wKWMUpdateClientWorkspace(wwin);
1852 wKWMSendEventMessage(wwin, WKWMChangedClient);
1853 #endif
1854 if (unmap) {
1855 wWindowUnmap(wwin);
1860 void
1861 wWindowSynthConfigureNotify(WWindow *wwin)
1863 XEvent sevent;
1865 sevent.type = ConfigureNotify;
1866 sevent.xconfigure.display = dpy;
1867 sevent.xconfigure.event = wwin->client_win;
1868 sevent.xconfigure.window = wwin->client_win;
1870 sevent.xconfigure.x = wwin->client.x;
1871 sevent.xconfigure.y = wwin->client.y;
1872 sevent.xconfigure.width = wwin->client.width;
1873 sevent.xconfigure.height = wwin->client.height;
1875 sevent.xconfigure.border_width = wwin->old_border_width;
1876 if (WFLAGP(wwin, no_titlebar))
1877 sevent.xconfigure.above = None;
1878 else
1879 sevent.xconfigure.above = wwin->frame->titlebar->window;
1881 sevent.xconfigure.override_redirect = False;
1882 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1883 #ifdef KWM_HINTS
1884 wKWMSendEventMessage(wwin, WKWMChangedClient);
1885 #endif
1886 XFlush(dpy);
1891 *----------------------------------------------------------------------
1892 * wWindowConfigure--
1893 * Configures the frame, decorations and client window to the
1894 * specified geometry. The geometry is not checked for validity,
1895 * wWindowConstrainSize() must be used for that.
1896 * The size parameters are for the client window, but the position is
1897 * for the frame.
1898 * The client window receives a ConfigureNotify event, according
1899 * to what ICCCM says.
1901 * Returns:
1902 * None
1904 * Side effects:
1905 * Window size and position are changed and client window receives
1906 * a ConfigureNotify event.
1907 *----------------------------------------------------------------------
1909 void
1910 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1911 WWindow *wwin;
1912 int req_x, req_y; /* new position of the frame */
1913 int req_width, req_height; /* new size of the client */
1915 int synth_notify = False;
1916 int resize;
1918 resize = (req_width!=wwin->client.width
1919 || req_height!=wwin->client.height);
1921 * if the window is being moved but not resized then
1922 * send a synthetic ConfigureNotify
1924 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1925 synth_notify = True;
1928 if (WFLAGP(wwin, dont_move_off))
1929 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1930 req_width, req_height);
1931 if (resize) {
1932 if (req_width < MIN_WINDOW_SIZE)
1933 req_width = MIN_WINDOW_SIZE;
1934 if (req_height < MIN_WINDOW_SIZE)
1935 req_height = MIN_WINDOW_SIZE;
1937 /* If growing, resize inner part before frame,
1938 * if shrinking, resize frame before.
1939 * This will prevent the frame (that can have a different color)
1940 * to be exposed, causing flicker */
1941 if (req_height > wwin->frame->core->height
1942 || req_width > wwin->frame->core->width)
1943 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1945 if (wwin->flags.shaded) {
1946 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1947 req_width, wwin->frame->core->height);
1948 wwin->old_geometry.height = req_height;
1949 } else {
1950 int h;
1952 h = req_height + wwin->frame->top_width
1953 + wwin->frame->bottom_width;
1955 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1958 if (!(req_height > wwin->frame->core->height
1959 || req_width > wwin->frame->core->width))
1960 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1962 wwin->client.x = req_x;
1963 wwin->client.y = req_y + wwin->frame->top_width;
1964 wwin->client.width = req_width;
1965 wwin->client.height = req_height;
1966 } else {
1967 wwin->client.x = req_x;
1968 wwin->client.y = req_y + wwin->frame->top_width;
1970 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1972 wwin->frame_x = req_x;
1973 wwin->frame_y = req_y;
1975 #ifdef SHAPE
1976 if (wShapeSupported && wwin->flags.shaped && resize) {
1977 wWindowSetShape(wwin);
1979 #endif
1981 if (synth_notify)
1982 wWindowSynthConfigureNotify(wwin);
1983 XFlush(dpy);
1987 void
1988 wWindowMove(wwin, req_x, req_y)
1989 WWindow *wwin;
1990 int req_x, req_y; /* new position of the frame */
1992 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1993 int synth_notify = False;
1995 /* Send a synthetic ConfigureNotify event for every window movement. */
1996 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1997 synth_notify = True;
1999 #else
2000 /* A single synthetic ConfigureNotify event is sent at the end of
2001 * a completed (opaque) movement in moveres.c */
2002 #endif
2004 if (WFLAGP(wwin, dont_move_off))
2005 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2006 wwin->frame->core->width, wwin->frame->core->height);
2008 wwin->client.x = req_x;
2009 wwin->client.y = req_y + wwin->frame->top_width;
2011 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2013 wwin->frame_x = req_x;
2014 wwin->frame_y = req_y;
2016 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2017 if (synth_notify)
2018 wWindowSynthConfigureNotify(wwin);
2019 #endif
2023 void
2024 wWindowUpdateButtonImages(WWindow *wwin)
2026 WScreen *scr = wwin->screen_ptr;
2027 Pixmap pixmap, mask;
2028 WFrameWindow *fwin = wwin->frame;
2030 if (WFLAGP(wwin, no_titlebar))
2031 return;
2033 /* miniaturize button */
2035 if (!WFLAGP(wwin, no_miniaturize_button)) {
2036 if (wwin->wm_gnustep_attr
2037 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2038 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2040 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2041 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2042 } else {
2043 mask = None;
2046 if (fwin->lbutton_image
2047 && (fwin->lbutton_image->image != pixmap
2048 || fwin->lbutton_image->mask != mask)) {
2049 wPixmapDestroy(fwin->lbutton_image);
2050 fwin->lbutton_image = NULL;
2053 if (!fwin->lbutton_image) {
2054 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2055 fwin->lbutton_image->client_owned = 1;
2056 fwin->lbutton_image->client_owned_mask = 1;
2058 } else {
2059 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2060 wPixmapDestroy(fwin->lbutton_image);
2062 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2066 #ifdef XKB_BUTTON_HINT
2067 if (!WFLAGP(wwin, no_language_button)) {
2068 if (fwin->languagebutton_image &&
2069 !fwin->languagebutton_image->shared) {
2070 wPixmapDestroy(fwin->languagebutton_image);
2072 fwin->languagebutton_image =
2073 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2075 #endif
2077 /* close button */
2079 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2080 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2082 if (!WFLAGP(wwin, no_close_button)) {
2083 if (wwin->wm_gnustep_attr
2084 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2085 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2087 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2088 mask = wwin->wm_gnustep_attr->close_mask;
2089 else
2090 mask = None;
2092 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2093 || fwin->rbutton_image->mask != mask)) {
2094 wPixmapDestroy(fwin->rbutton_image);
2095 fwin->rbutton_image = NULL;
2098 if (!fwin->rbutton_image) {
2099 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2100 fwin->rbutton_image->client_owned = 1;
2101 fwin->rbutton_image->client_owned_mask = 1;
2104 } else if (WFLAGP(wwin, kill_close)) {
2106 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2107 wPixmapDestroy(fwin->rbutton_image);
2109 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2111 } else if (MGFLAGP(wwin, broken_close)) {
2113 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2114 wPixmapDestroy(fwin->rbutton_image);
2116 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2118 } else {
2120 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2121 wPixmapDestroy(fwin->rbutton_image);
2123 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2127 /* force buttons to be redrawn */
2128 fwin->flags.need_texture_change = 1;
2129 wFrameWindowPaint(fwin);
2134 *---------------------------------------------------------------------------
2135 * wWindowConfigureBorders--
2136 * Update window border configuration according to attribute flags.
2138 *---------------------------------------------------------------------------
2140 void
2141 wWindowConfigureBorders(WWindow *wwin)
2143 if (wwin->frame) {
2144 int flags;
2145 int newy, oldh;
2147 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2148 #ifdef XKB_BUTTON_HINT
2149 flags |= WFF_LANGUAGE_BUTTON;
2150 #endif
2151 if (!WFLAGP(wwin, no_titlebar))
2152 flags |= WFF_TITLEBAR;
2153 if (!WFLAGP(wwin, no_resizebar))
2154 flags |= WFF_RESIZEBAR;
2155 if (wwin->flags.shaded)
2156 flags |= WFF_IS_SHADED;
2158 oldh = wwin->frame->top_width;
2159 wFrameWindowUpdateBorders(wwin->frame, flags);
2160 if (oldh != wwin->frame->top_width) {
2161 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2163 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2164 wWindowConfigure(wwin, wwin->frame_x, newy,
2165 wwin->client.width, wwin->client.height);
2168 flags = 0;
2169 if (!WFLAGP(wwin, no_miniaturize_button)
2170 && wwin->frame->flags.hide_left_button)
2171 flags |= WFF_LEFT_BUTTON;
2173 #ifdef XKB_BUTTON_HINT
2174 if (!WFLAGP(wwin, no_language_button)
2175 && wwin->frame->flags.hide_language_button)
2176 flags |= WFF_LANGUAGE_BUTTON;
2177 #endif
2179 if (!WFLAGP(wwin, no_close_button)
2180 && wwin->frame->flags.hide_right_button)
2181 flags |= WFF_RIGHT_BUTTON;
2183 if (flags!=0) {
2184 wWindowUpdateButtonImages(wwin);
2185 wFrameWindowShowButton(wwin->frame, flags);
2188 flags = 0;
2189 if (WFLAGP(wwin, no_miniaturize_button)
2190 && !wwin->frame->flags.hide_left_button)
2191 flags |= WFF_LEFT_BUTTON;
2193 #ifdef XKB_BUTTON_HINT
2194 if (WFLAGP(wwin, no_language_button)
2195 && !wwin->frame->flags.hide_language_button)
2196 flags |= WFF_LANGUAGE_BUTTON;
2197 #endif
2199 if (WFLAGP(wwin, no_close_button)
2200 && !wwin->frame->flags.hide_right_button)
2201 flags |= WFF_RIGHT_BUTTON;
2203 if (flags!=0)
2204 wFrameWindowHideButton(wwin->frame, flags);
2206 #ifdef SHAPE
2207 if (wShapeSupported && wwin->flags.shaped) {
2208 wWindowSetShape(wwin);
2210 #endif
2215 void
2216 wWindowSaveState(WWindow *wwin)
2218 CARD32 data[10];
2219 int i;
2221 memset(data, 0, sizeof(CARD32)*10);
2222 data[0] = wwin->frame->workspace;
2223 data[1] = wwin->flags.miniaturized;
2224 data[2] = wwin->flags.shaded;
2225 data[3] = wwin->flags.hidden;
2227 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2228 if (wwin->screen_ptr->shortcutWindows[i] &&
2229 WMCountInBag(wwin->screen_ptr->shortcutWindows[i], wwin))
2230 data[9] |= 1<<i;
2232 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2233 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2234 (unsigned char *)data, 10);
2238 static int
2239 getSavedState(Window window, WSavedState **state)
2241 Atom type_ret;
2242 int fmt_ret;
2243 unsigned long nitems_ret;
2244 unsigned long bytes_after_ret;
2245 CARD32 *data;
2247 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2248 True, _XA_WINDOWMAKER_STATE,
2249 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2250 (unsigned char **)&data)!=Success || !data)
2251 return 0;
2253 *state = malloc(sizeof(WSavedState));
2255 if (*state) {
2256 (*state)->workspace = data[0];
2257 (*state)->miniaturized = data[1];
2258 (*state)->shaded = data[2];
2259 (*state)->hidden = data[3];
2260 (*state)->use_geometry = data[4];
2261 (*state)->x = data[5];
2262 (*state)->y = data[6];
2263 (*state)->w = data[7];
2264 (*state)->h = data[8];
2265 (*state)->window_shortcuts = data[9];
2267 XFree(data);
2269 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2270 return 1;
2271 else
2272 return 0;
2276 #ifdef SHAPE
2277 void
2278 wWindowClearShape(WWindow *wwin)
2280 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2281 0, wwin->frame->top_width, None, ShapeSet);
2282 XFlush(dpy);
2285 void
2286 wWindowSetShape(WWindow *wwin)
2288 XRectangle rect[2];
2289 int count;
2290 #ifdef OPTIMIZE_SHAPE
2291 XRectangle *rects;
2292 XRectangle *urec;
2293 int ordering;
2295 /* only shape is the client's */
2296 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2297 goto alt_code;
2300 /* Get array of rectangles describing the shape mask */
2301 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2302 &count, &ordering);
2303 if (!rects) {
2304 goto alt_code;
2307 urec = malloc(sizeof(XRectangle)*(count+2));
2308 if (!urec) {
2309 XFree(rects);
2310 goto alt_code;
2313 /* insert our decoration rectangles in the rect list */
2314 memcpy(urec, rects, sizeof(XRectangle)*count);
2315 XFree(rects);
2317 if (!WFLAGP(wwin, no_titlebar)) {
2318 urec[count].x = -1;
2319 urec[count].y = -1 - wwin->frame->top_width;
2320 urec[count].width = wwin->frame->core->width + 2;
2321 urec[count].height = wwin->frame->top_width + 1;
2322 count++;
2324 if (!WFLAGP(wwin, no_resizebar)) {
2325 urec[count].x = -1;
2326 urec[count].y = wwin->frame->core->height
2327 - wwin->frame->bottom_width - wwin->frame->top_width;
2328 urec[count].width = wwin->frame->core->width + 2;
2329 urec[count].height = wwin->frame->bottom_width + 1;
2330 count++;
2333 /* shape our frame window */
2334 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2335 0, wwin->frame->top_width, urec, count,
2336 ShapeSet, Unsorted);
2337 XFlush(dpy);
2338 free(urec);
2339 return;
2341 alt_code:
2342 #endif /* OPTIMIZE_SHAPE */
2343 count = 0;
2344 if (!WFLAGP(wwin, no_titlebar)) {
2345 rect[count].x = -1;
2346 rect[count].y = -1;
2347 rect[count].width = wwin->frame->core->width + 2;
2348 rect[count].height = wwin->frame->top_width + 1;
2349 count++;
2351 if (!WFLAGP(wwin, no_resizebar)) {
2352 rect[count].x = -1;
2353 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2354 rect[count].width = wwin->frame->core->width + 2;
2355 rect[count].height = wwin->frame->bottom_width + 1;
2356 count++;
2358 if (count > 0) {
2359 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2360 0, 0, rect, count, ShapeSet, Unsorted);
2362 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2363 0, wwin->frame->top_width, wwin->client_win,
2364 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2365 XFlush(dpy);
2367 #endif /* SHAPE */
2369 /* ====================================================================== */
2371 static FocusMode
2372 getFocusMode(WWindow *wwin)
2374 FocusMode mode;
2376 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2377 if (wwin->wm_hints->input == True) {
2378 if (wwin->protocols.TAKE_FOCUS)
2379 mode = WFM_LOCALLY_ACTIVE;
2380 else
2381 mode = WFM_PASSIVE;
2382 } else {
2383 if (wwin->protocols.TAKE_FOCUS)
2384 mode = WFM_GLOBALLY_ACTIVE;
2385 else
2386 mode = WFM_NO_INPUT;
2388 } else {
2389 mode = WFM_PASSIVE;
2391 return mode;
2395 void
2396 wWindowSetKeyGrabs(WWindow *wwin)
2398 int i;
2399 WShortKey *key;
2401 for (i=0; i<WKBD_LAST; i++) {
2402 key = &wKeyBindings[i];
2404 if (key->keycode==0)
2405 continue;
2406 if (key->modifier!=AnyModifier) {
2407 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2408 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2409 #ifdef NUMLOCK_HACK
2410 /* Also grab all modifier combinations possible that include,
2411 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2412 * work even if the NumLock/ScrollLock key is on.
2414 wHackedGrabKey(key->keycode, key->modifier,
2415 wwin->frame->core->window, True, GrabModeAsync,
2416 GrabModeAsync);
2417 #endif
2419 XGrabKey(dpy, key->keycode, key->modifier,
2420 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2423 #ifndef LITE
2424 wRootMenuBindShortcuts(wwin->frame->core->window);
2425 #endif
2430 void
2431 wWindowResetMouseGrabs(WWindow *wwin)
2433 /* Mouse grabs can't be done on the client window because of
2434 * ICCCM and because clients that try to do the same will crash.
2436 * But there is a problem wich makes tbar buttons of unfocused
2437 * windows not usable as the click goes to the frame window instead
2438 * of the button itself. Must figure a way to fix that.
2441 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2443 if (!WFLAGP(wwin, no_bind_mouse)) {
2444 /* grabs for Meta+drag */
2445 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2446 True, ButtonPressMask, GrabModeSync,
2447 GrabModeAsync, None, None);
2450 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2451 /* the passive grabs to focus the window */
2452 if (wPreferences.focus_mode == WKF_CLICK)
2453 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2454 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2455 None, None);
2457 XFlush(dpy);
2461 void
2462 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2464 if (attr->flags & GSExtraFlagsAttr) {
2465 if (MGFLAGP(wwin, broken_close) !=
2466 (attr->extra_flags & GSDocumentEditedFlag)) {
2467 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2468 wWindowUpdateButtonImages(wwin);
2474 WMagicNumber
2475 wWindowAddSavedState(char *instance, char *class, char *command,
2476 pid_t pid, WSavedState *state)
2478 WWindowState *wstate;
2480 wstate = malloc(sizeof(WWindowState));
2481 if (!wstate)
2482 return 0;
2484 memset(wstate, 0, sizeof(WWindowState));
2485 wstate->pid = pid;
2486 if (instance)
2487 wstate->instance = wstrdup(instance);
2488 if (class)
2489 wstate->class = wstrdup(class);
2490 if (command)
2491 wstate->command = wstrdup(command);
2492 wstate->state = state;
2494 wstate->next = windowState;
2495 windowState = wstate;
2497 #ifdef DEBUG
2498 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2499 class, command);
2500 #endif
2502 return wstate;
2506 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2509 WMagicNumber
2510 wWindowGetSavedState(Window win)
2512 char *instance, *class, *command=NULL;
2513 WWindowState *wstate = windowState;
2514 char **argv;
2515 int argc;
2517 if (!wstate)
2518 return NULL;
2520 if (XGetCommand(dpy, win, &argv, &argc)) {
2521 if (argc > 0)
2522 command = FlattenStringList(argv, argc);
2523 XFreeStringList(argv);
2525 if (!command)
2526 return NULL;
2528 if (PropGetWMClass(win, &class, &instance)) {
2529 while (wstate) {
2530 if (SAME(instance, wstate->instance) &&
2531 SAME(class, wstate->class) &&
2532 SAME(command, wstate->command)) {
2533 break;
2535 wstate = wstate->next;
2537 } else {
2538 wstate = NULL;
2541 #ifdef DEBUG
2542 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2543 class, command);
2544 #endif
2546 if (command) free(command);
2547 if (instance) XFree(instance);
2548 if (class) XFree(class);
2550 return wstate;
2554 void
2555 wWindowDeleteSavedState(WMagicNumber id)
2557 WWindowState *tmp, *wstate=(WWindowState*)id;
2559 if (!wstate || !windowState)
2560 return;
2562 tmp = windowState;
2563 if (tmp==wstate) {
2564 windowState = wstate->next;
2565 #ifdef DEBUG
2566 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2567 wstate, wstate->instance, wstate->class, wstate->command);
2568 #endif
2569 if (wstate->instance) free(wstate->instance);
2570 if (wstate->class) free(wstate->class);
2571 if (wstate->command) free(wstate->command);
2572 free(wstate->state);
2573 free(wstate);
2574 } else {
2575 while (tmp->next) {
2576 if (tmp->next==wstate) {
2577 tmp->next=wstate->next;
2578 #ifdef DEBUG
2579 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2580 wstate, wstate->instance, wstate->class, wstate->command);
2581 #endif
2582 if (wstate->instance) free(wstate->instance);
2583 if (wstate->class) free(wstate->class);
2584 if (wstate->command) free(wstate->command);
2585 free(wstate->state);
2586 free(wstate);
2587 break;
2589 tmp = tmp->next;
2595 void
2596 wWindowDeleteSavedStatesForPID(pid_t pid)
2598 WWindowState *tmp, *wstate;
2600 if (!windowState)
2601 return;
2603 tmp = windowState;
2604 if (tmp->pid == pid) {
2605 wstate = windowState;
2606 windowState = tmp->next;
2607 #ifdef DEBUG
2608 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2609 wstate, wstate->instance, wstate->class, wstate->command);
2610 #endif
2611 if (wstate->instance) free(wstate->instance);
2612 if (wstate->class) free(wstate->class);
2613 if (wstate->command) free(wstate->command);
2614 free(wstate->state);
2615 free(wstate);
2616 } else {
2617 while (tmp->next) {
2618 if (tmp->next->pid==pid) {
2619 wstate = tmp->next;
2620 tmp->next = wstate->next;
2621 #ifdef DEBUG
2622 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2623 wstate, wstate->instance, wstate->class, wstate->command);
2624 #endif
2625 if (wstate->instance) free(wstate->instance);
2626 if (wstate->class) free(wstate->class);
2627 if (wstate->command) free(wstate->command);
2628 free(wstate->state);
2629 free(wstate);
2630 break;
2632 tmp = tmp->next;
2638 /* ====================================================================== */
2640 static void
2641 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2643 WWindow *wwin = data;
2645 #ifndef NUMLOCK_HACK
2646 if ((event->xbutton.state & ValidModMask)
2647 != (event->xbutton.state & ~LockMask)) {
2648 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2649 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2651 #endif
2653 event->xbutton.state &= ValidModMask;
2655 CloseWindowMenu(wwin->screen_ptr);
2657 if (wPreferences.focus_mode==WKF_CLICK
2658 && !(event->xbutton.state&ControlMask)
2659 && !WFLAGP(wwin, no_focusable)) {
2660 wSetFocusTo(wwin->screen_ptr, wwin);
2663 if (event->xbutton.button == Button1)
2664 wRaiseFrame(wwin->frame->core);
2666 if (event->xbutton.window != wwin->frame->resizebar->window) {
2667 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2668 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2669 GrabModeAsync, GrabModeAsync, None,
2670 None, CurrentTime)!=GrabSuccess) {
2671 #ifdef DEBUG0
2672 wwarning("pointer grab failed for window move");
2673 #endif
2674 return;
2678 if (event->xbutton.state & MOD_MASK) {
2679 /* move the window */
2680 wMouseMoveWindow(wwin, event);
2681 XUngrabPointer(dpy, CurrentTime);
2682 } else {
2683 wMouseResizeWindow(wwin, event);
2684 XUngrabPointer(dpy, CurrentTime);
2690 static void
2691 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2693 WWindow *wwin = data;
2695 event->xbutton.state &= ValidModMask;
2697 if (event->xbutton.button==Button1) {
2698 if (event->xbutton.state == 0) {
2699 if (!WFLAGP(wwin, no_shadeable)) {
2700 /* shade window */
2701 if (wwin->flags.shaded)
2702 wUnshadeWindow(wwin);
2703 else
2704 wShadeWindow(wwin);
2706 } else {
2707 int dir = 0;
2709 if (event->xbutton.state & ControlMask)
2710 dir |= MAX_VERTICAL;
2712 if (event->xbutton.state & ShiftMask) {
2713 dir |= MAX_HORIZONTAL;
2714 if (!(event->xbutton.state & ControlMask))
2715 wSelectWindow(wwin, !wwin->flags.selected);
2718 /* maximize window */
2719 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2720 if (wwin->flags.maximized)
2721 wUnmaximizeWindow(wwin);
2722 else
2723 wMaximizeWindow(wwin, dir);
2726 } else if (event->xbutton.button==Button3) {
2727 if (event->xbutton.state & MOD_MASK) {
2728 wHideOtherApplications(wwin);
2730 } else if (event->xbutton.button==Button2) {
2731 wSelectWindow(wwin, !wwin->flags.selected);
2736 static void
2737 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2739 WWindow *wwin = desc->parent;
2741 event->xbutton.state &= ValidModMask;
2743 CloseWindowMenu(wwin->screen_ptr);
2745 if (wPreferences.focus_mode==WKF_CLICK
2746 && !(event->xbutton.state&ControlMask)
2747 && !WFLAGP(wwin, no_focusable)) {
2748 wSetFocusTo(wwin->screen_ptr, wwin);
2750 if (event->xbutton.button == Button1) {
2751 wRaiseFrame(wwin->frame->core);
2754 if (event->xbutton.state & MOD_MASK) {
2755 /* move the window */
2756 if (XGrabPointer(dpy, wwin->client_win, False,
2757 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2758 GrabModeAsync, GrabModeAsync, None,
2759 None, CurrentTime)!=GrabSuccess) {
2760 #ifdef DEBUG0
2761 wwarning("pointer grab failed for window move");
2762 #endif
2763 return;
2765 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2766 wMouseResizeWindow(wwin, event);
2767 else
2768 wMouseMoveWindow(wwin, event);
2769 XUngrabPointer(dpy, CurrentTime);
2774 static void
2775 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2777 WWindow *wwin = (WWindow*)data;
2779 #ifndef NUMLOCK_HACK
2780 if ((event->xbutton.state & ValidModMask)
2781 != (event->xbutton.state & ~LockMask)) {
2782 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2783 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2785 #endif
2786 event->xbutton.state &= ValidModMask;
2788 CloseWindowMenu(wwin->screen_ptr);
2790 if (wPreferences.focus_mode==WKF_CLICK
2791 && !(event->xbutton.state&ControlMask)
2792 && !WFLAGP(wwin, no_focusable)) {
2793 wSetFocusTo(wwin->screen_ptr, wwin);
2796 if (event->xbutton.button == Button1
2797 || event->xbutton.button == Button2) {
2799 if (event->xbutton.button == Button1) {
2800 if (event->xbutton.state & MOD_MASK) {
2801 wLowerFrame(wwin->frame->core);
2802 } else {
2803 wRaiseFrame(wwin->frame->core);
2806 if ((event->xbutton.state & ShiftMask)
2807 && !(event->xbutton.state & ControlMask)) {
2808 wSelectWindow(wwin, !wwin->flags.selected);
2809 return;
2811 if (event->xbutton.window != wwin->frame->titlebar->window
2812 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2813 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2814 GrabModeAsync, GrabModeAsync, None,
2815 None, CurrentTime)!=GrabSuccess) {
2816 #ifdef DEBUG0
2817 wwarning("pointer grab failed for window move");
2818 #endif
2819 return;
2822 /* move the window */
2823 wMouseMoveWindow(wwin, event);
2825 XUngrabPointer(dpy, CurrentTime);
2826 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2827 && !wwin->flags.internal_window
2828 && !WCHECK_STATE(WSTATE_MODAL)) {
2829 WObjDescriptor *desc;
2831 if (event->xbutton.window != wwin->frame->titlebar->window
2832 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2833 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2834 GrabModeAsync, GrabModeAsync, None,
2835 None, CurrentTime)!=GrabSuccess) {
2836 #ifdef DEBUG0
2837 wwarning("pointer grab failed for window move");
2838 #endif
2839 return;
2842 OpenWindowMenu(wwin, event->xbutton.x_root,
2843 wwin->frame_y+wwin->frame->top_width, False);
2845 /* allow drag select */
2846 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2847 event->xany.send_event = True;
2848 (*desc->handle_mousedown)(desc, event);
2850 XUngrabPointer(dpy, CurrentTime);
2856 static void
2857 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2859 WWindow *wwin = data;
2861 event->xbutton.state &= ValidModMask;
2863 CloseWindowMenu(wwin->screen_ptr);
2865 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2866 return;
2868 /* if control-click, kill the client */
2869 if (event->xbutton.state & ControlMask) {
2870 wClientKill(wwin);
2871 } else {
2872 #ifdef OLWM_HINTS
2873 if (wwin->flags.olwm_push_pin_out) {
2875 wwin->flags.olwm_push_pin_out = 0;
2877 wOLWMChangePushpinState(wwin, True);
2879 wFrameWindowUpdatePushButton(wwin->frame, False);
2881 return;
2883 #endif
2884 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2885 /* send delete message */
2886 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2892 static void
2893 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2895 WWindow *wwin = data;
2897 CloseWindowMenu(wwin->screen_ptr);
2899 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2900 return;
2902 /* send delete message */
2903 if (wwin->protocols.DELETE_WINDOW) {
2904 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2905 } else {
2906 wClientKill(wwin);
2911 #ifdef XKB_BUTTON_HINT
2912 static void
2913 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2915 WWindow *wwin = data;
2916 WFrameWindow *fwin = wwin->frame;
2917 WScreen *scr = fwin->screen_ptr;
2918 XkbStateRec staterec;
2919 int tl;
2921 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2922 return;
2923 tl = wwin->frame->languagemode;
2924 wwin->frame->languagemode = wwin->frame->last_languagemode;
2925 wwin->frame->last_languagemode = tl;
2926 wSetFocusTo(scr, wwin);
2927 wwin->frame->languagebutton_image =
2928 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
2929 wwin->frame->languagemode];
2930 wFrameWindowUpdateLanguageButton(wwin->frame);
2931 if (event->xbutton.button == Button3)
2932 return;
2933 wRaiseFrame(fwin->core);
2935 #endif
2938 static void
2939 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2941 WWindow *wwin = data;
2943 event->xbutton.state &= ValidModMask;
2945 CloseWindowMenu(wwin->screen_ptr);
2947 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2948 return;
2950 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2951 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2952 LastTimestamp);
2953 } else {
2954 WApplication *wapp;
2955 if ((event->xbutton.state & ControlMask) ||
2956 (event->xbutton.button == Button3)) {
2958 wapp = wApplicationOf(wwin->main_window);
2959 if (wapp && !WFLAGP(wwin, no_appicon))
2960 wHideApplication(wapp);
2961 } else if (event->xbutton.state==0) {
2962 wIconifyWindow(wwin);