Support for GNUstep control of window titlebars added.
[wmaker-crm.git] / src / window.c
blob5a9b9c96ca7e4b9f577b919aa3c7699087879e4d
1 /* window.c - client window managing stuffs
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
38 #include "WindowMaker.h"
39 #include "GNUstep.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "texture.h"
43 #include "window.h"
44 #include "winspector.h"
45 #include "icon.h"
46 #include "properties.h"
47 #include "actions.h"
48 #include "client.h"
49 #include "funcs.h"
50 #include "keybind.h"
51 #include "stacking.h"
52 #include "defaults.h"
53 #include "workspace.h"
56 #ifdef MWM_HINTS
57 # include "motif.h"
58 #endif
59 #ifdef KWM_HINTS
60 # include "kwm.h"
61 #endif
62 #ifdef GNOME_STUFF
63 # include "gnome.h"
64 #endif
65 #ifdef OLWM_HINTS
66 # include "openlook.h"
67 #endif
69 /****** Global Variables ******/
71 extern WShortKey wKeyBindings[WKBD_LAST];
73 #ifdef SHAPE
74 extern Bool wShapeSupported;
75 #endif
77 /* contexts */
78 extern XContext wWinContext;
80 /* cursors */
81 extern Cursor wCursor[WCUR_LAST];
83 /* protocol atoms */
84 extern Atom _XA_WM_DELETE_WINDOW;
85 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
87 extern Atom _XA_WINDOWMAKER_STATE;
89 extern WPreferences wPreferences;
91 #define MOD_MASK wPreferences.modifier_mask
93 extern Time LastTimestamp;
95 /* superfluous... */
96 extern void DoWindowBirth(WWindow*);
99 /***** Local Stuff *****/
102 static WWindowState *windowState=NULL;
106 /* local functions */
107 static FocusMode getFocusMode(WWindow *wwin);
109 static int getSavedState(Window window, WSavedState **state);
111 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
113 /* event handlers */
116 /* frame window (during window grabs) */
117 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
119 /* close button */
120 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
121 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
123 /* iconify button */
124 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
126 #ifdef XKB_BUTTON_HINT
127 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
128 #endif
130 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
131 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
133 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
136 /****** Notification Observers ******/
138 static void
139 appearanceObserver(void *self, WMNotification *notif)
141 WWindow *wwin = (WWindow*)self;
142 int flags = (int)WMGetNotificationClientData(notif);
144 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
145 return;
147 if (flags & WFontSettings) {
148 wWindowConfigureBorders(wwin);
149 if(wwin->flags.shaded) {
150 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
151 wwin->frame->top_width - 1);
153 wwin->client.y = wwin->frame_y - wwin->client.height
154 + wwin->frame->top_width;
155 wWindowSynthConfigureNotify(wwin);
158 if (flags & WTextureSettings) {
159 wwin->frame->flags.need_texture_remake = 1;
161 if (flags & (WTextureSettings | WColorSettings)) {
162 if (wwin->frame->titlebar)
163 XClearWindow(dpy, wwin->frame->titlebar->window);
165 wFrameWindowPaint(wwin->frame);
169 /************************************/
171 WWindow*
172 wWindowFor(Window window)
174 WObjDescriptor *desc;
176 if (window==None)
177 return NULL;
179 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
180 return NULL;
182 if (desc->parent_type==WCLASS_WINDOW)
183 return desc->parent;
184 else if (desc->parent_type==WCLASS_FRAME) {
185 WFrameWindow *frame = (WFrameWindow*)desc->parent;
186 if (frame->flags.is_client_window_frame)
187 return frame->child;
190 return NULL;
194 WWindow*
195 wWindowCreate()
197 WWindow *wwin;
199 wwin = wmalloc(sizeof(WWindow));
200 wretain(wwin);
202 memset(wwin, 0, sizeof(WWindow));
204 wwin->client_descriptor.handle_mousedown = frameMouseDown;
205 wwin->client_descriptor.parent = wwin;
206 wwin->client_descriptor.self = wwin;
207 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
209 return wwin;
213 void
214 wWindowDestroy(WWindow *wwin)
216 int i;
218 if (wwin->screen_ptr->cmap_window == wwin) {
219 wwin->screen_ptr->cmap_window = NULL;
222 WMRemoveNotificationObserver(wwin);
224 wwin->flags.destroyed = 1;
226 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
227 if (!wwin->screen_ptr->shortcutWindows[i])
228 continue;
230 WMRemoveFromBag(wwin->screen_ptr->shortcutWindows[i], wwin);
232 if (!WMGetBagItemCount(wwin->screen_ptr->shortcutWindows[i])) {
233 WMFreeBag(wwin->screen_ptr->shortcutWindows[i]);
234 wwin->screen_ptr->shortcutWindows[i] = NULL;
238 if (wwin->normal_hints)
239 XFree(wwin->normal_hints);
241 if (wwin->wm_hints)
242 XFree(wwin->wm_hints);
244 if (wwin->wm_instance)
245 XFree(wwin->wm_instance);
247 if (wwin->wm_class)
248 XFree(wwin->wm_class);
250 if (wwin->wm_gnustep_attr)
251 free(wwin->wm_gnustep_attr);
253 if (wwin->cmap_windows)
254 XFree(wwin->cmap_windows);
256 XDeleteContext(dpy, wwin->client_win, wWinContext);
258 if (wwin->frame)
259 wFrameWindowDestroy(wwin->frame);
261 if (wwin->icon) {
262 RemoveFromStackList(wwin->icon->core);
263 wIconDestroy(wwin->icon);
264 if (wPreferences.auto_arrange_icons)
265 wArrangeIcons(wwin->screen_ptr, True);
267 wrelease(wwin);
273 static void
274 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
276 if (gs_hints->flags & GSWindowStyleAttr) {
277 if (gs_hints->window_style == WMBorderlessWindowMask) {
278 wwin->client_flags.no_border = 1;
279 wwin->client_flags.no_titlebar = 1;
280 wwin->client_flags.no_closable = 1;
281 wwin->client_flags.no_miniaturizable = 1;
282 wwin->client_flags.no_resizable = 1;
283 wwin->client_flags.no_close_button = 1;
284 wwin->client_flags.no_miniaturize_button = 1;
285 wwin->client_flags.no_resizebar = 1;
286 } else {
287 wwin->client_flags.no_close_button =
288 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
290 wwin->client_flags.no_closable =
291 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
293 wwin->client_flags.no_miniaturize_button =
294 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
296 wwin->client_flags.no_miniaturizable =
297 wwin->client_flags.no_miniaturize_button;
299 wwin->client_flags.no_resizebar =
300 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
302 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
304 /* these attributes supposedly imply in the existence
305 * of a titlebar */
306 if (gs_hints->window_style & (WMResizableWindowMask|
307 WMClosableWindowMask|
308 WMMiniaturizableWindowMask)) {
309 wwin->client_flags.no_titlebar = 0;
310 } else {
311 wwin->client_flags.no_titlebar =
312 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
316 } else {
317 /* setup the defaults */
318 wwin->client_flags.no_border = 0;
319 wwin->client_flags.no_titlebar = 0;
320 wwin->client_flags.no_closable = 0;
321 wwin->client_flags.no_miniaturizable = 0;
322 wwin->client_flags.no_resizable = 0;
323 wwin->client_flags.no_close_button = 0;
324 wwin->client_flags.no_miniaturize_button = 0;
325 wwin->client_flags.no_resizebar = 0;
327 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
328 wwin->client_flags.no_appicon = 1;
333 void
334 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
335 WWindowAttributes *mask)
337 if (wflags->no_appicon && mask->no_appicon)
338 wflags->emulate_appicon = 0;
340 if (wwin->main_window!=None) {
341 WApplication *wapp = wApplicationOf(wwin->main_window);
342 if (wapp && !wapp->flags.emulated)
343 wflags->emulate_appicon = 0;
346 if (wwin->transient_for!=None
347 && wwin->transient_for!=wwin->screen_ptr->root_win)
348 wflags->emulate_appicon = 0;
350 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
351 wflags->sunken = 0;
356 void
357 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
359 WScreen *scr = wwin->screen_ptr;
361 /* sets global default stuff */
362 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
363 &wwin->client_flags, NULL, True);
365 * Decoration setting is done in this precedence (lower to higher)
366 * - use global default in the resource database
367 * - guess some settings
368 * - use GNUstep/external window attributes
369 * - set hints specified for the app in the resource DB
372 WSETUFLAG(wwin, broken_close, 0);
374 if (wwin->protocols.DELETE_WINDOW)
375 WSETUFLAG(wwin, kill_close, 0);
376 else
377 WSETUFLAG(wwin, kill_close, 1);
379 /* transients can't be iconified or maximized */
380 if (wwin->transient_for) {
381 WSETUFLAG(wwin, no_miniaturizable, 1);
382 WSETUFLAG(wwin, no_miniaturize_button, 1);
385 /* if the window can't be resized, remove the resizebar */
386 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
387 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
388 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
389 WSETUFLAG(wwin, no_resizable, 1);
390 WSETUFLAG(wwin, no_resizebar, 1);
393 /* set GNUstep window attributes */
394 if (wwin->wm_gnustep_attr) {
395 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
397 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
399 switch (wwin->wm_gnustep_attr->window_level) {
400 case WMNormalWindowLevel:
401 *level = WMNormalLevel;
402 break;
403 case WMFloatingWindowLevel:
404 *level = WMFloatingLevel;
405 break;
406 case WMDockWindowLevel:
407 *level = WMDockLevel;
408 break;
409 case WMSubmenuWindowLevel:
410 *level = WMSubmenuLevel;
411 break;
412 case WMMainMenuWindowLevel:
413 *level = WMMainMenuLevel;
414 break;
415 default:
416 *level = WMNormalLevel;
417 break;
419 } else {
420 /* setup defaults */
421 *level = WMNormalLevel;
423 } else {
424 int tmp_workspace = -1;
425 int tmp_level = -1;
427 #ifdef MWM_HINTS
428 wMWMCheckClientHints(wwin);
429 #endif /* MWM_HINTS */
431 #ifdef KWM_HINTS
432 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
433 #endif /* KWM_HINTS */
435 #ifdef GNOME_STUFF
436 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
437 #endif /* GNOME_STUFF */
439 #ifdef OLWM_HINTS
440 wOLWMCheckClientHints(wwin);
441 #endif /* OLWM_HINTS */
443 if (tmp_level < 0) {
444 if (WFLAGP(wwin, floating))
445 *level = WMFloatingLevel;
446 else if (WFLAGP(wwin, sunken))
447 *level = WMSunkenLevel;
448 else
449 *level = WMNormalLevel;
450 } else {
451 *level = tmp_level;
454 if (tmp_workspace >= 0) {
455 *workspace = tmp_workspace % scr->workspace_count;
460 * Set attributes specified only for that window/class.
461 * This might do duplicate work with the 1st wDefaultFillAttributes().
463 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
464 &wwin->user_flags, &wwin->defined_user_flags,
465 False);
467 * Sanity checks for attributes that depend on other attributes
469 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
470 wwin->user_flags.emulate_appicon = 0;
472 if (wwin->main_window!=None) {
473 WApplication *wapp = wApplicationOf(wwin->main_window);
474 if (wapp && !wapp->flags.emulated)
475 wwin->user_flags.emulate_appicon = 0;
478 if (wwin->transient_for!=None
479 && wwin->transient_for!=wwin->screen_ptr->root_win)
480 wwin->user_flags.emulate_appicon = 0;
482 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
483 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
484 wwin->user_flags.sunken = 0;
486 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
492 Bool
493 wWindowCanReceiveFocus(WWindow *wwin)
495 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
496 return False;
497 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
498 return False;
499 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
500 return False;
502 return True;
506 Bool
507 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
509 int w1, h1, w2, h2;
511 w1 = wwin->frame->core->width;
512 h1 = wwin->frame->core->height;
513 w2 = obscured->frame->core->width;
514 h2 = obscured->frame->core->height;
516 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
517 && wwin->frame->workspace != obscured->frame->workspace)
518 return False;
520 if (wwin->frame_x + w1 < obscured->frame_x
521 || wwin->frame_y + h1 < obscured->frame_y
522 || wwin->frame_x > obscured->frame_x + w2
523 || wwin->frame_y > obscured->frame_y + h2) {
524 return False;
527 return True;
532 *----------------------------------------------------------------
533 * wManageWindow--
534 * reparents the window and allocates a descriptor for it.
535 * Window manager hints and other hints are fetched to configure
536 * the window decoration attributes and others. User preferences
537 * for the window are used if available, to configure window
538 * decorations and some behaviour.
539 * If in startup, windows that are override redirect,
540 * unmapped and never were managed and are Withdrawn are not
541 * managed.
543 * Returns:
544 * the new window descriptor
546 * Side effects:
547 * The window is reparented and appropriate notification
548 * is done to the client. Input mask for the window is setup.
549 * The window descriptor is also associated with various window
550 * contexts and inserted in the head of the window list.
551 * Event handler contexts are associated for some objects
552 * (buttons, titlebar and resizebar)
554 *----------------------------------------------------------------
556 WWindow*
557 wManageWindow(WScreen *scr, Window window)
559 WWindow *wwin;
560 int x, y;
561 unsigned width, height;
562 XWindowAttributes wattribs;
563 XSetWindowAttributes attribs;
564 WWindowState *win_state;
565 WWindow *transientOwner = NULL;
566 int window_level;
567 int wm_state;
568 int foo;
569 int workspace = -1;
570 char *title;
571 Bool withdraw = False;
573 /* mutex. */
574 /* XGrabServer(dpy); */
575 XSync(dpy, False);
576 /* make sure the window is still there */
577 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
578 XUngrabServer(dpy);
579 return NULL;
582 /* if it's an override-redirect, ignore it */
583 if (wattribs.override_redirect) {
584 XUngrabServer(dpy);
585 return NULL;
588 wm_state = PropGetWindowState(window);
590 /* if it's startup and the window is unmapped, don't manage it */
591 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
592 XUngrabServer(dpy);
593 return NULL;
596 if (!wFetchName(dpy, window, &title)) {
597 title = NULL;
600 #ifdef KWM_HINTS
601 if (title && !wKWMManageableClient(scr, window, title)) {
602 XFree(title);
603 XUngrabServer(dpy);
604 return NULL;
606 #endif /* KWM_HINTS */
609 wwin = wWindowCreate();
611 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
613 #ifdef DEBUG
614 printf("managing window %x\n", (unsigned)window);
615 #endif
617 #ifdef SHAPE
618 if (wShapeSupported) {
619 int junk;
620 unsigned int ujunk;
621 int b_shaped;
623 XShapeSelectInput(dpy, window, ShapeNotifyMask);
624 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
625 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
626 wwin->flags.shaped = b_shaped;
628 #endif
631 *--------------------------------------------------
633 * Get hints and other information in properties
635 *--------------------------------------------------
637 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
639 /* setup descriptor */
640 wwin->client_win = window;
641 wwin->screen_ptr = scr;
643 wwin->old_border_width = wattribs.border_width;
645 wwin->event_mask = CLIENT_EVENTS;
646 attribs.event_mask = CLIENT_EVENTS;
647 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
648 attribs.save_under = False;
649 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
650 |CWSaveUnder, &attribs);
651 XSetWindowBorderWidth(dpy, window, 0);
653 /* get hints from GNUstep app */
654 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
655 wwin->flags.is_gnustep = 1;
657 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
658 wwin->wm_gnustep_attr = NULL;
661 wwin->client_leader = PropGetClientLeader(window);
662 if (wwin->client_leader!=None)
663 wwin->main_window = wwin->client_leader;
665 wwin->wm_hints = XGetWMHints(dpy, window);
667 if (wwin->wm_hints) {
668 if (wwin->wm_hints->flags & StateHint) {
670 if (wwin->wm_hints->initial_state == IconicState) {
672 wwin->flags.miniaturized = 1;
674 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
676 withdraw = True;
680 if (wwin->wm_hints->flags & WindowGroupHint) {
681 wwin->group_id = wwin->wm_hints->window_group;
682 /* window_group has priority over CLIENT_LEADER */
683 wwin->main_window = wwin->group_id;
684 } else {
685 wwin->group_id = None;
688 if (wwin->wm_hints->flags & UrgencyHint)
689 wwin->flags.urgent = 1;
690 } else {
691 wwin->group_id = None;
694 PropGetProtocols(window, &wwin->protocols);
696 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
697 wwin->transient_for = None;
698 } else {
699 if (wwin->transient_for==None || wwin->transient_for==window) {
700 wwin->transient_for = scr->root_win;
701 } else {
702 transientOwner = wWindowFor(wwin->transient_for);
703 if (transientOwner && transientOwner->main_window!=None) {
704 wwin->main_window = transientOwner->main_window;
705 } /*else {
706 wwin->main_window = None;
711 /* guess the focus mode */
712 wwin->focus_mode = getFocusMode(wwin);
714 /* get geometry stuff */
715 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
717 /* get colormap windows */
718 GetColormapWindows(wwin);
721 *--------------------------------------------------
723 * Setup the decoration/window attributes and
724 * geometry
726 *--------------------------------------------------
729 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
731 #ifdef OLWM_HINTS
732 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
733 && wwin->group_id != None && wwin->group_id != window) {
735 transientOwner = wWindowFor(wwin->group_id);
737 if (transientOwner) {
738 wwin->transient_for = wwin->group_id;
740 /* transients can't be iconified or maximized */
741 if (wwin->transient_for) {
742 WSETUFLAG(wwin, no_miniaturizable, 1);
743 WSETUFLAG(wwin, no_miniaturize_button, 1);
747 #endif /* OLWM_HINTS */
750 * Make broken apps behave as a nice app.
752 if (WFLAGP(wwin, emulate_appicon)) {
753 wwin->main_window = wwin->client_win;
757 *------------------------------------------------------------
759 * Setup the initial state of the window
761 *------------------------------------------------------------
764 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
765 wwin->flags.miniaturized = 1;
768 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
769 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
772 #ifdef GNOME_STUFF
773 wGNOMECheckInitialClientState(wwin);
774 #endif
775 #ifdef KWM_HINTS
776 wKWMCheckClientInitialState(wwin);
777 #endif
779 /* apply previous state if it exists and we're in startup */
780 if (scr->flags.startup && wm_state >= 0) {
782 if (wm_state == IconicState) {
784 wwin->flags.miniaturized = 1;
786 } else if (wm_state == WithdrawnState) {
788 withdraw = True;
792 /* if there is a saved state (from file), restore it */
793 win_state = NULL;
794 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
795 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
796 } else {
797 win_state = (WWindowState*)wWindowGetSavedState(window);
799 if (win_state && !withdraw) {
801 if (win_state->state->hidden>0)
802 wwin->flags.hidden = win_state->state->hidden;
804 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
805 wwin->flags.shaded = win_state->state->shaded;
807 if (win_state->state->miniaturized>0 &&
808 !WFLAGP(wwin, no_miniaturizable)) {
809 wwin->flags.miniaturized = win_state->state->miniaturized;
811 if (!IS_OMNIPRESENT(wwin)) {
812 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
813 wwin->wm_class);
814 if (w < 0 || w >= scr->workspace_count) {
815 workspace = win_state->state->workspace;
816 if (workspace >= scr->workspace_count)
817 workspace = scr->current_workspace;
818 } else {
819 workspace = w;
821 } else {
822 workspace = scr->current_workspace;
826 /* if we're restarting, restore saved state (from hints).
827 * This will overwrite previous */
829 WSavedState *wstate;
831 if (getSavedState(window, &wstate)) {
832 wwin->flags.shaded = wstate->shaded;
833 wwin->flags.hidden = wstate->hidden;
834 wwin->flags.miniaturized = wstate->miniaturized;
835 workspace = wstate->workspace;
837 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
838 int i;
840 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
841 if (wstate->window_shortcuts & (1<<i)) {
842 if (!scr->shortcutWindows[i])
843 scr->shortcutWindows[i] = WMCreateBag(4);
845 WMPutInBag(scr->shortcutWindows[i], wwin);
849 free(wstate);
853 /* don't let transients start miniaturized if their owners are not */
854 if (transientOwner && !transientOwner->flags.miniaturized
855 && wwin->flags.miniaturized && !withdraw) {
856 wwin->flags.miniaturized = 0;
857 if (wwin->wm_hints)
858 wwin->wm_hints->initial_state = NormalState;
861 /* set workspace on which the window starts */
862 if (workspace >= 0) {
863 if (workspace > scr->workspace_count-1) {
864 workspace = workspace % scr->workspace_count;
866 } else {
867 int w;
869 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
871 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
873 workspace = w;
875 } else {
876 if (wPreferences.open_transients_with_parent && transientOwner) {
878 workspace = transientOwner->frame->workspace;
880 } else {
882 workspace = scr->current_workspace;
887 /* setup window geometry */
888 if (win_state && win_state->state->use_geometry) {
889 width = win_state->state->w;
890 height = win_state->state->h;
892 wWindowConstrainSize(wwin, &width, &height);
894 /* do not ask for window placement if the window is
895 * transient, during startup, if the initial workspace is another one
896 * or if the window wants to start iconic.
897 * If geometry was saved, restore it. */
899 Bool dontBring = False;
901 if (win_state && win_state->state->use_geometry) {
902 x = win_state->state->x;
903 y = win_state->state->y;
904 } else if ((wwin->transient_for==None
905 || wPreferences.window_placement!=WPM_MANUAL)
906 && !scr->flags.startup
907 && workspace == scr->current_workspace
908 && !wwin->flags.miniaturized
909 && !wwin->flags.maximized
910 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
911 PlaceWindow(wwin, &x, &y, width, height);
912 if (wPreferences.window_placement == WPM_MANUAL)
913 dontBring = True;
916 if (WFLAGP(wwin, dont_move_off) && dontBring)
917 wScreenBringInside(scr, &x, &y, width, height);
920 if (wwin->flags.urgent) {
921 if (!IS_OMNIPRESENT(wwin))
922 wwin->flags.omnipresent ^= 1;
926 *--------------------------------------------------
928 * Create frame, borders and do reparenting
930 *--------------------------------------------------
932 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
933 #ifdef XKB_BUTTON_HINT
934 if (wPreferences.modelock)
935 foo |= WFF_LANGUAGE_BUTTON;
936 #endif
937 if (!WFLAGP(wwin, no_titlebar))
938 foo |= WFF_TITLEBAR;
939 if (!WFLAGP(wwin, no_resizebar))
940 foo |= WFF_RESIZEBAR;
941 if (!WFLAGP(wwin, no_border))
942 foo |= WFF_BORDER;
944 wwin->frame = wFrameWindowCreate(scr, window_level,
945 x, y, width, height,
946 &wPreferences.window_title_clearance, foo,
947 scr->window_title_texture,
948 scr->resizebar_texture,
949 scr->window_title_pixel,
950 #ifdef DRAWSTRING_PLUGIN
951 W_STRING_FTITLE,
952 #endif
953 &scr->window_title_gc,
954 &scr->title_font);
956 wwin->frame->flags.is_client_window_frame = 1;
957 wwin->frame->flags.justification = wPreferences.title_justification;
959 /* setup button images */
960 wWindowUpdateButtonImages(wwin);
962 /* hide unused buttons */
963 foo = 0;
964 if (WFLAGP(wwin, no_close_button))
965 foo |= WFF_RIGHT_BUTTON;
966 if (WFLAGP(wwin, no_miniaturize_button))
967 foo |= WFF_LEFT_BUTTON;
968 #ifdef XKB_BUTTON_HINT
969 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
970 foo |= WFF_LANGUAGE_BUTTON;
971 #endif
972 if (foo!=0)
973 wFrameWindowHideButton(wwin->frame, foo);
975 wwin->frame->child = wwin;
977 #ifdef OLWM_HINTS
978 /* emulate olwm push pin. Make the button look as pushed-in for
979 * the pinned-out state. When the button is clicked, it will
980 * revert to the normal position, which means the pin is pinned-in.
982 if (wwin->flags.olwm_push_pin_out)
983 wFrameWindowUpdatePushButton(wwin->frame, True);
984 #endif /* OLWM_HINTS */
986 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
987 if (title)
988 XFree(title);
990 wwin->frame->workspace = workspace;
992 wwin->frame->on_click_left = windowIconifyClick;
993 #ifdef XKB_BUTTON_HINT
994 if (wPreferences.modelock)
995 wwin->frame->on_click_language = windowLanguageClick;
996 #endif
998 wwin->frame->on_click_right = windowCloseClick;
999 wwin->frame->on_dblclick_right = windowCloseDblClick;
1001 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1002 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1004 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1007 XSelectInput(dpy, wwin->client_win,
1008 wwin->event_mask & ~StructureNotifyMask);
1010 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1011 0, wwin->frame->top_width);
1013 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1017 int gx, gy;
1019 wClientGetGravityOffsets(wwin, &gx, &gy);
1021 /* if gravity is to the south, account for the border sizes */
1022 if (gy > 0)
1023 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1027 * wWindowConfigure() will init the client window's size
1028 * (wwin->client.{width,height}) and all other geometry
1029 * related variables (frame_x,frame_y)
1031 wWindowConfigure(wwin, x, y, width, height);
1033 /* to make sure the window receives it's new position after reparenting */
1034 wWindowSynthConfigureNotify(wwin);
1037 *--------------------------------------------------
1039 * Setup descriptors and save window to internal
1040 * lists
1042 *--------------------------------------------------
1045 if (wwin->main_window!=None) {
1046 WApplication *app;
1047 WWindow *leader;
1049 /* Leader windows do not necessary set themselves as leaders.
1050 * If this is the case, point the leader of this window to
1051 * itself */
1052 leader = wWindowFor(wwin->main_window);
1053 if (leader && leader->main_window==None) {
1054 leader->main_window = leader->client_win;
1056 app = wApplicationCreate(scr, wwin->main_window);
1057 if (app) {
1058 app->last_workspace = workspace;
1061 * Do application specific stuff, like setting application
1062 * wide attributes.
1065 if (wwin->flags.hidden) {
1066 /* if the window was set to hidden because it was hidden
1067 * in a previous incarnation and that state was restored */
1068 app->flags.hidden = 1;
1071 if (app->flags.hidden) {
1072 wwin->flags.hidden = 1;
1077 /* setup the frame descriptor */
1078 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1079 wwin->frame->core->descriptor.parent = wwin;
1080 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1082 /* don't let windows go away if we die */
1083 XAddToSaveSet(dpy, window);
1085 XLowerWindow(dpy, window);
1087 /* if window is in this workspace and should be mapped, then map it */
1088 if (!wwin->flags.miniaturized
1089 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1090 && !wwin->flags.hidden && !withdraw) {
1092 /* The following "if" is to avoid crashing of clients that expect
1093 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1094 * after the return from this function.
1096 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1097 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1098 } else {
1099 wClientSetState(wwin, NormalState, None);
1102 #if 0
1103 /* if not auto focus, then map the window under the currently
1104 * focused window */
1105 #define _WIDTH(w) (w)->frame->core->width
1106 #define _HEIGHT(w) (w)->frame->core->height
1107 if (!wPreferences.auto_focus && scr->focused_window
1108 && !scr->flags.startup && !transientOwner
1109 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1110 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1111 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1112 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1113 || wwin->flags.maximized)) {
1114 MoveInStackListUnder(scr->focused_window->frame->core,
1115 wwin->frame->core);
1117 #undef _WIDTH
1118 #undef _HEIGHT
1120 #endif
1122 if (wPreferences.superfluous && !wPreferences.no_animations
1123 && !scr->flags.startup && wwin->transient_for==None
1125 * The brain damaged idiotic non-click to focus modes will
1126 * have trouble with this because:
1128 * 1. window is created and mapped by the client
1129 * 2. window is mapped by wmaker in small size
1130 * 3. window is animated to grow to normal size
1131 * 4. this function returns to normal event loop
1132 * 5. eventually, the EnterNotify event that would trigger
1133 * the window focusing (if the mouse is over that window)
1134 * will be processed by wmaker.
1135 * But since this event will be rather delayed
1136 * (step 3 has a large delay) the time when the event ocurred
1137 * and when it is processed, the client that owns that window
1138 * will reject the XSetInputFocus() for it.
1140 && (wPreferences.focus_mode==WKF_CLICK
1141 || wPreferences.auto_focus)) {
1142 DoWindowBirth(wwin);
1145 wWindowMap(wwin);
1148 /* setup stacking descriptor */
1149 if (transientOwner) {
1150 /* && wPreferences.on_top_transients */
1151 if (transientOwner) {
1152 wwin->frame->core->stacking->child_of =
1153 transientOwner->frame->core;
1155 } else {
1156 wwin->frame->core->stacking->child_of = NULL;
1160 if (!scr->focused_window) {
1161 /* first window on the list */
1162 wwin->next = NULL;
1163 wwin->prev = NULL;
1164 scr->focused_window = wwin;
1165 } else {
1166 WWindow *tmp;
1168 /* add window at beginning of focus window list */
1169 tmp = scr->focused_window;
1170 while (tmp->prev)
1171 tmp = tmp->prev;
1172 tmp->prev = wwin;
1173 wwin->next = tmp;
1174 wwin->prev = NULL;
1177 #ifdef GNOME_STUFF
1178 wGNOMEUpdateClientStateHint(wwin, True);
1179 #endif
1180 #ifdef KWM_HINTS
1181 wKWMUpdateClientWorkspace(wwin);
1182 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1183 #endif
1185 XUngrabServer(dpy);
1188 *--------------------------------------------------
1190 * Final preparations before window is ready to go
1192 *--------------------------------------------------
1195 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1198 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1199 && !wwin->flags.hidden) {
1200 if (((transientOwner && transientOwner->flags.focused)
1201 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1202 wSetFocusTo(scr, wwin);
1204 wWindowResetMouseGrabs(wwin);
1206 if (!WFLAGP(wwin, no_bind_keys)) {
1207 wWindowSetKeyGrabs(wwin);
1209 #ifdef GNOME_STUFF
1210 wGNOMEUpdateClientListHint(scr);
1211 #endif
1212 #ifdef KWM_HINTS
1213 wwin->flags.kwm_managed = 1;
1215 wKWMSendEventMessage(wwin, WKWMAddWindow);
1216 #endif
1218 wColormapInstallForWindow(scr, scr->cmap_window);
1220 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1222 #ifdef OLWM_HINTS
1223 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1224 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1226 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1227 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1228 wwin->frame_y);
1230 #endif
1233 *------------------------------------------------------------
1234 * Setup Notification Observers
1235 *------------------------------------------------------------
1237 WMAddNotificationObserver(appearanceObserver, wwin,
1238 WNWindowAppearanceSettingsChanged, wwin);
1242 *--------------------------------------------------
1244 * Cleanup temporary stuff
1246 *--------------------------------------------------
1249 if (win_state)
1250 wWindowDeleteSavedState(win_state);
1252 /* If the window must be withdrawed, then do it now.
1253 * Must do some optimization, 'though */
1254 if (withdraw) {
1255 wwin->flags.mapped = 0;
1256 wClientSetState(wwin, WithdrawnState, None);
1257 wUnmanageWindow(wwin, True, False);
1258 wwin = NULL;
1261 return wwin;
1268 WWindow*
1269 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1270 char *title, int x, int y, int width, int height)
1272 WWindow *wwin;
1273 int foo;
1275 wwin = wWindowCreate();
1277 WMAddNotificationObserver(appearanceObserver, wwin,
1278 WNWindowAppearanceSettingsChanged, wwin);
1280 wwin->flags.internal_window = 1;
1282 WSETUFLAG(wwin, omnipresent, 1);
1283 WSETUFLAG(wwin, no_shadeable, 1);
1284 WSETUFLAG(wwin, no_resizable, 1);
1285 WSETUFLAG(wwin, no_miniaturizable, 1);
1287 wwin->focus_mode = WFM_PASSIVE;
1289 wwin->client_win = window;
1290 wwin->screen_ptr = scr;
1292 wwin->transient_for = owner;
1294 wwin->client.x = x;
1295 wwin->client.y = y;
1296 wwin->client.width = width;
1297 wwin->client.height = height;
1299 wwin->frame_x = wwin->client.x;
1300 wwin->frame_y = wwin->client.y;
1303 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1304 foo |= WFF_TITLEBAR;
1305 #ifdef XKB_BUTTON_HINT
1306 foo |= WFF_LANGUAGE_BUTTON;
1307 #endif
1309 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1310 wwin->frame_x, wwin->frame_y,
1311 width, height,
1312 &wPreferences.window_title_clearance, foo,
1313 scr->window_title_texture,
1314 scr->resizebar_texture,
1315 scr->window_title_pixel,
1316 #ifdef DRAWSTRING_PLUGIN
1317 W_STRING_FTITLE,
1318 #endif
1319 &scr->window_title_gc,
1320 &scr->title_font);
1322 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1324 wwin->frame->flags.is_client_window_frame = 1;
1325 wwin->frame->flags.justification = wPreferences.title_justification;
1327 wFrameWindowChangeTitle(wwin->frame, title);
1329 /* setup button images */
1330 wWindowUpdateButtonImages(wwin);
1332 /* hide buttons */
1333 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1335 wwin->frame->child = wwin;
1337 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1339 #ifdef XKB_BUTTON_HINT
1340 if (wPreferences.modelock)
1341 wwin->frame->on_click_language = windowLanguageClick;
1342 #endif
1344 wwin->frame->on_click_right = windowCloseClick;
1346 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1347 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1349 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1351 wwin->client.y += wwin->frame->top_width;
1352 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1353 0, wwin->frame->top_width);
1355 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1356 wwin->client.width, wwin->client.height);
1358 /* setup the frame descriptor */
1359 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1360 wwin->frame->core->descriptor.parent = wwin;
1361 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1364 XLowerWindow(dpy, window);
1365 XMapSubwindows(dpy, wwin->frame->core->window);
1367 /* setup stacking descriptor */
1368 if (
1369 #ifdef removed
1370 wPreferences.on_top_transients &&
1371 #endif
1372 wwin->transient_for!=None
1373 && wwin->transient_for!=scr->root_win) {
1374 WWindow *tmp;
1375 tmp = wWindowFor(wwin->transient_for);
1376 if (tmp)
1377 wwin->frame->core->stacking->child_of = tmp->frame->core;
1378 } else {
1379 wwin->frame->core->stacking->child_of = NULL;
1383 if (!scr->focused_window) {
1384 /* first window on the list */
1385 wwin->next = NULL;
1386 wwin->prev = NULL;
1387 scr->focused_window = wwin;
1388 } else {
1389 WWindow *tmp;
1391 /* add window at beginning of focus window list */
1392 tmp = scr->focused_window;
1393 while (tmp->prev)
1394 tmp = tmp->prev;
1395 tmp->prev = wwin;
1396 wwin->next = tmp;
1397 wwin->prev = NULL;
1400 if (wwin->flags.is_gnustep == 0)
1401 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1403 /* if (wPreferences.auto_focus)*/
1404 wSetFocusTo(scr, wwin);
1406 wWindowResetMouseGrabs(wwin);
1408 wWindowSetKeyGrabs(wwin);
1410 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1412 return wwin;
1417 *----------------------------------------------------------------------
1418 * wUnmanageWindow--
1419 * Removes the frame window from a window and destroys all data
1420 * related to it. The window will be reparented back to the root window
1421 * if restore is True.
1423 * Side effects:
1424 * Everything related to the window is destroyed and the window
1425 * is removed from the window lists. Focus is set to the previous on the
1426 * window list.
1427 *----------------------------------------------------------------------
1429 void
1430 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1432 WCoreWindow *frame = wwin->frame->core;
1433 WWindow *owner = NULL;
1434 WWindow *newFocusedWindow = NULL;
1435 int wasFocused;
1436 WScreen *scr = wwin->screen_ptr;
1439 #ifdef KWM_HINTS
1440 wwin->frame->workspace = -1;
1442 wKWMUpdateClientWorkspace(wwin);
1443 #endif
1445 /* First close attribute editor window if open */
1446 if (wwin->flags.inspector_open) {
1447 wCloseInspectorForWindow(wwin);
1450 /* Close window menu if it's open for this window */
1451 if (wwin->flags.menu_open_for_me) {
1452 CloseWindowMenu(scr);
1455 if (!destroyed) {
1456 if (!wwin->flags.internal_window)
1457 XRemoveFromSaveSet(dpy, wwin->client_win);
1459 XSelectInput(dpy, wwin->client_win, NoEventMask);
1461 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1462 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1465 XUnmapWindow(dpy, frame->window);
1467 XUnmapWindow(dpy, wwin->client_win);
1469 /* deselect window */
1470 wSelectWindow(wwin, False);
1472 /* remove all pending events on window */
1473 /* I think this only matters for autoraise */
1474 if (wPreferences.raise_delay)
1475 WMDeleteTimerWithClientData(wwin->frame->core);
1477 XFlush(dpy);
1479 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1481 /* reparent the window back to the root */
1482 if (restore)
1483 wClientRestore(wwin);
1485 if (wwin->transient_for!=scr->root_win) {
1486 owner = wWindowFor(wwin->transient_for);
1487 if (owner) {
1488 if (!owner->flags.semi_focused) {
1489 owner = NULL;
1490 } else {
1491 owner->flags.semi_focused = 0;
1496 wasFocused = wwin->flags.focused;
1498 /* remove from window focus list */
1499 if (!wwin->prev && !wwin->next) {
1500 /* was the only window */
1501 scr->focused_window = NULL;
1502 newFocusedWindow = NULL;
1503 } else {
1504 WWindow *tmp;
1506 if (wwin->prev)
1507 wwin->prev->next = wwin->next;
1508 if (wwin->next)
1509 wwin->next->prev = wwin->prev;
1510 else {
1511 scr->focused_window = wwin->prev;
1512 scr->focused_window->next = NULL;
1515 if (wPreferences.focus_mode==WKF_CLICK) {
1517 /* if in click to focus mode and the window
1518 * was a transient, focus the owner window
1520 tmp = NULL;
1521 if (wPreferences.focus_mode==WKF_CLICK) {
1522 tmp = wWindowFor(wwin->transient_for);
1523 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1524 tmp = NULL;
1527 /* otherwise, focus the next one in the focus list */
1528 if (!tmp) {
1529 tmp = scr->focused_window;
1530 while (tmp) { /* look for one in the window list first */
1531 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1532 && (tmp->flags.mapped || tmp->flags.shaded))
1533 break;
1534 tmp = tmp->prev;
1536 if (!tmp) { /* if unsuccessful, choose any focusable window */
1537 tmp = scr->focused_window;
1538 while (tmp) {
1539 if (!WFLAGP(tmp, no_focusable)
1540 && (tmp->flags.mapped || tmp->flags.shaded))
1541 break;
1542 tmp = tmp->prev;
1547 newFocusedWindow = tmp;
1549 } else if (wPreferences.focus_mode==WKF_SLOPPY
1550 || wPreferences.focus_mode==WKF_POINTER) {
1551 unsigned int mask;
1552 int foo;
1553 Window bar, win;
1555 /* This is to let the root window get the keyboard input
1556 * if Sloppy focus mode and no other window get focus.
1557 * This way keybindings will not freeze.
1559 tmp = NULL;
1560 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1561 &foo, &foo, &foo, &foo, &mask))
1562 tmp = wWindowFor(win);
1563 if (tmp == wwin)
1564 tmp = NULL;
1565 newFocusedWindow = tmp;
1566 } else {
1567 newFocusedWindow = NULL;
1571 if (!wwin->flags.internal_window) {
1572 #ifdef GNOME_STUFF
1573 wGNOMERemoveClient(wwin);
1574 #endif
1575 #ifdef KWM_HINTS
1576 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1577 #endif
1580 #ifdef DEBUG
1581 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1582 (unsigned)frame->window);
1583 #endif
1585 if (wasFocused) {
1586 if (newFocusedWindow != owner && owner) {
1587 if (wwin->flags.is_gnustep == 0)
1588 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1590 wSetFocusTo(scr, newFocusedWindow);
1592 wWindowDestroy(wwin);
1593 XFlush(dpy);
1597 void
1598 wWindowMap(WWindow *wwin)
1600 XMapWindow(dpy, wwin->frame->core->window);
1601 if (!wwin->flags.shaded) {
1602 /* window will be remapped when getting MapNotify */
1603 XSelectInput(dpy, wwin->client_win,
1604 wwin->event_mask & ~StructureNotifyMask);
1605 XMapWindow(dpy, wwin->client_win);
1606 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1608 wwin->flags.mapped = 1;
1613 void
1614 wWindowUnmap(WWindow *wwin)
1616 wwin->flags.mapped = 0;
1618 /* prevent window withdrawal when getting UnmapNotify */
1619 XSelectInput(dpy, wwin->client_win,
1620 wwin->event_mask & ~StructureNotifyMask);
1621 XUnmapWindow(dpy, wwin->client_win);
1622 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1624 XUnmapWindow(dpy, wwin->frame->core->window);
1629 void
1630 wWindowFocus(WWindow *wwin, WWindow *owin)
1632 WWindow *nowner;
1633 WWindow *oowner;
1635 #ifdef KEEP_XKB_LOCK_STATUS
1636 if (wPreferences.modelock) {
1637 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1639 #endif /* KEEP_XKB_LOCK_STATUS */
1641 wwin->flags.semi_focused = 0;
1643 if (wwin->flags.is_gnustep == 0)
1644 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1646 wwin->flags.focused = 1;
1648 wWindowResetMouseGrabs(wwin);
1650 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1652 if (owin == wwin || !owin)
1653 return;
1655 nowner = wWindowFor(wwin->transient_for);
1657 /* new window is a transient for the old window */
1658 if (nowner == owin) {
1659 owin->flags.semi_focused = 1;
1660 wWindowUnfocus(nowner);
1661 return;
1664 oowner = wWindowFor(owin->transient_for);
1666 /* new window is owner of old window */
1667 if (wwin == oowner) {
1668 wWindowUnfocus(owin);
1669 return;
1672 if (!nowner) {
1673 wWindowUnfocus(owin);
1674 return;
1677 /* new window has same owner of old window */
1678 if (oowner == nowner) {
1679 /* prevent unfocusing of owner */
1680 oowner->flags.semi_focused = 0;
1681 wWindowUnfocus(owin);
1682 oowner->flags.semi_focused = 1;
1684 return;
1687 /* nowner != NULL && oowner != nowner */
1688 nowner->flags.semi_focused = 1;
1689 wWindowUnfocus(nowner);
1690 wWindowUnfocus(owin);
1694 void
1695 wWindowUnfocus(WWindow *wwin)
1697 CloseWindowMenu(wwin->screen_ptr);
1699 if (wwin->flags.is_gnustep == 0)
1700 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1701 ? WS_PFOCUSED : WS_UNFOCUSED);
1703 if (wwin->transient_for!=None
1704 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1705 WWindow *owner;
1706 owner = wWindowFor(wwin->transient_for);
1707 if (owner && owner->flags.semi_focused) {
1708 owner->flags.semi_focused = 0;
1709 if (owner->flags.mapped || owner->flags.shaded) {
1710 wWindowUnfocus(owner);
1711 wFrameWindowPaint(owner->frame);
1715 wwin->flags.focused = 0;
1717 wWindowResetMouseGrabs(wwin);
1719 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1726 *----------------------------------------------------------------------
1728 * wWindowConstrainSize--
1729 * Constrains size for the client window, taking the maximal size,
1730 * window resize increments and other size hints into account.
1732 * Returns:
1733 * The closest size to what was given that the client window can
1734 * have.
1736 *----------------------------------------------------------------------
1738 void
1739 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1741 int width = *nwidth;
1742 int height = *nheight;
1743 int winc = 1;
1744 int hinc = 1;
1745 int minW = 1, minH = 1;
1746 int maxW = wwin->screen_ptr->scr_width*2;
1747 int maxH = wwin->screen_ptr->scr_height*2;
1748 int minAX = -1, minAY = -1;
1749 int maxAX = -1, maxAY = -1;
1750 int baseW = 0;
1751 int baseH = 0;
1753 if (wwin->normal_hints) {
1754 winc = wwin->normal_hints->width_inc;
1755 hinc = wwin->normal_hints->height_inc;
1756 minW = wwin->normal_hints->min_width;
1757 minH = wwin->normal_hints->min_height;
1758 maxW = wwin->normal_hints->max_width;
1759 maxH = wwin->normal_hints->max_height;
1760 if (wwin->normal_hints->flags & PAspect) {
1761 minAX = wwin->normal_hints->min_aspect.x;
1762 minAY = wwin->normal_hints->min_aspect.y;
1763 maxAX = wwin->normal_hints->max_aspect.x;
1764 maxAY = wwin->normal_hints->max_aspect.y;
1768 if (width < minW)
1769 width = minW;
1770 if (height < minH)
1771 height = minH;
1773 if (width > maxW)
1774 width = maxW;
1775 if (height > maxH)
1776 height = maxH;
1778 /* aspect ratio code borrowed from olwm */
1779 if (minAX > 0) {
1780 /* adjust max aspect ratio */
1781 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1782 if (maxAX > maxAY) {
1783 height = (width * maxAY) / maxAX;
1784 if (height > maxH) {
1785 height = maxH;
1786 width = (height * maxAX) / maxAY;
1788 } else {
1789 width = (height * maxAX) / maxAY;
1790 if (width > maxW) {
1791 width = maxW;
1792 height = (width * maxAY) / maxAX;
1797 /* adjust min aspect ratio */
1798 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1799 if (minAX > minAY) {
1800 height = (width * minAY) / minAX;
1801 if (height < minH) {
1802 height = minH;
1803 width = (height * minAX) / minAY;
1805 } else {
1806 width = (height * minAX) / minAY;
1807 if (width < minW) {
1808 width = minW;
1809 height = (width * minAY) / minAX;
1815 if (baseW != 0) {
1816 width = (((width - baseW) / winc) * winc) + baseW;
1817 } else {
1818 width = (((width - minW) / winc) * winc) + minW;
1821 if (baseW != 0) {
1822 height = (((height - baseH) / hinc) * hinc) + baseH;
1823 } else {
1824 height = (((height - minH) / hinc) * hinc) + minH;
1827 *nwidth = width;
1828 *nheight = height;
1832 void
1833 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1835 WScreen *scr = wwin->screen_ptr;
1836 WApplication *wapp;
1837 int unmap = 0;
1839 if (workspace >= scr->workspace_count || workspace < 0
1840 || workspace == wwin->frame->workspace)
1841 return;
1843 if (workspace != scr->current_workspace) {
1844 /* Sent to other workspace. Unmap window */
1845 if ((wwin->flags.mapped
1846 || wwin->flags.shaded
1847 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1848 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1850 wapp = wApplicationOf(wwin->main_window);
1851 if (wapp) {
1852 wapp->last_workspace = workspace;
1854 if (wwin->flags.miniaturized) {
1855 if (wwin->icon) {
1856 XUnmapWindow(dpy, wwin->icon->core->window);
1857 wwin->icon->mapped = 0;
1859 } else {
1860 unmap = 1;
1861 wSetFocusTo(scr, NULL);
1864 } else {
1865 /* brought to current workspace. Map window */
1866 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1867 if (wwin->icon) {
1868 XMapWindow(dpy, wwin->icon->core->window);
1869 wwin->icon->mapped = 1;
1871 } else if (!wwin->flags.mapped &&
1872 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1873 wWindowMap(wwin);
1876 if (!IS_OMNIPRESENT(wwin)) {
1877 wwin->frame->workspace = workspace;
1878 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1880 #ifdef GNOME_STUFF
1881 wGNOMEUpdateClientStateHint(wwin, True);
1882 #endif
1883 #ifdef KWM_HINTS
1884 wKWMUpdateClientWorkspace(wwin);
1885 wKWMSendEventMessage(wwin, WKWMChangedClient);
1886 #endif
1887 if (unmap) {
1888 wWindowUnmap(wwin);
1893 void
1894 wWindowSynthConfigureNotify(WWindow *wwin)
1896 XEvent sevent;
1898 sevent.type = ConfigureNotify;
1899 sevent.xconfigure.display = dpy;
1900 sevent.xconfigure.event = wwin->client_win;
1901 sevent.xconfigure.window = wwin->client_win;
1903 sevent.xconfigure.x = wwin->client.x;
1904 sevent.xconfigure.y = wwin->client.y;
1905 sevent.xconfigure.width = wwin->client.width;
1906 sevent.xconfigure.height = wwin->client.height;
1908 sevent.xconfigure.border_width = wwin->old_border_width;
1909 if (WFLAGP(wwin, no_titlebar))
1910 sevent.xconfigure.above = None;
1911 else
1912 sevent.xconfigure.above = wwin->frame->titlebar->window;
1914 sevent.xconfigure.override_redirect = False;
1915 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1916 #ifdef KWM_HINTS
1917 wKWMSendEventMessage(wwin, WKWMChangedClient);
1918 #endif
1919 XFlush(dpy);
1924 *----------------------------------------------------------------------
1925 * wWindowConfigure--
1926 * Configures the frame, decorations and client window to the
1927 * specified geometry. The geometry is not checked for validity,
1928 * wWindowConstrainSize() must be used for that.
1929 * The size parameters are for the client window, but the position is
1930 * for the frame.
1931 * The client window receives a ConfigureNotify event, according
1932 * to what ICCCM says.
1934 * Returns:
1935 * None
1937 * Side effects:
1938 * Window size and position are changed and client window receives
1939 * a ConfigureNotify event.
1940 *----------------------------------------------------------------------
1942 void
1943 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1944 WWindow *wwin;
1945 int req_x, req_y; /* new position of the frame */
1946 int req_width, req_height; /* new size of the client */
1948 int synth_notify = False;
1949 int resize;
1951 resize = (req_width!=wwin->client.width
1952 || req_height!=wwin->client.height);
1954 * if the window is being moved but not resized then
1955 * send a synthetic ConfigureNotify
1957 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1958 synth_notify = True;
1961 if (WFLAGP(wwin, dont_move_off))
1962 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1963 req_width, req_height);
1964 if (resize) {
1965 if (req_width < MIN_WINDOW_SIZE)
1966 req_width = MIN_WINDOW_SIZE;
1967 if (req_height < MIN_WINDOW_SIZE)
1968 req_height = MIN_WINDOW_SIZE;
1970 /* If growing, resize inner part before frame,
1971 * if shrinking, resize frame before.
1972 * This will prevent the frame (that can have a different color)
1973 * to be exposed, causing flicker */
1974 if (req_height > wwin->frame->core->height
1975 || req_width > wwin->frame->core->width)
1976 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1978 if (wwin->flags.shaded) {
1979 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1980 req_width, wwin->frame->core->height);
1981 wwin->old_geometry.height = req_height;
1982 } else {
1983 int h;
1985 h = req_height + wwin->frame->top_width
1986 + wwin->frame->bottom_width;
1988 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1991 if (!(req_height > wwin->frame->core->height
1992 || req_width > wwin->frame->core->width))
1993 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1995 wwin->client.x = req_x;
1996 wwin->client.y = req_y + wwin->frame->top_width;
1997 wwin->client.width = req_width;
1998 wwin->client.height = req_height;
1999 } else {
2000 wwin->client.x = req_x;
2001 wwin->client.y = req_y + wwin->frame->top_width;
2003 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2005 wwin->frame_x = req_x;
2006 wwin->frame_y = req_y;
2008 #ifdef SHAPE
2009 if (wShapeSupported && wwin->flags.shaped && resize) {
2010 wWindowSetShape(wwin);
2012 #endif
2014 if (synth_notify)
2015 wWindowSynthConfigureNotify(wwin);
2016 XFlush(dpy);
2020 void
2021 wWindowMove(wwin, req_x, req_y)
2022 WWindow *wwin;
2023 int req_x, req_y; /* new position of the frame */
2025 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2026 int synth_notify = False;
2028 /* Send a synthetic ConfigureNotify event for every window movement. */
2029 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2030 synth_notify = True;
2032 #else
2033 /* A single synthetic ConfigureNotify event is sent at the end of
2034 * a completed (opaque) movement in moveres.c */
2035 #endif
2037 if (WFLAGP(wwin, dont_move_off))
2038 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2039 wwin->frame->core->width, wwin->frame->core->height);
2041 wwin->client.x = req_x;
2042 wwin->client.y = req_y + wwin->frame->top_width;
2044 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2046 wwin->frame_x = req_x;
2047 wwin->frame_y = req_y;
2049 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2050 if (synth_notify)
2051 wWindowSynthConfigureNotify(wwin);
2052 #endif
2056 void
2057 wWindowUpdateButtonImages(WWindow *wwin)
2059 WScreen *scr = wwin->screen_ptr;
2060 Pixmap pixmap, mask;
2061 WFrameWindow *fwin = wwin->frame;
2063 if (WFLAGP(wwin, no_titlebar))
2064 return;
2066 /* miniaturize button */
2068 if (!WFLAGP(wwin, no_miniaturize_button)) {
2069 if (wwin->wm_gnustep_attr
2070 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2071 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2073 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2074 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2075 } else {
2076 mask = None;
2079 if (fwin->lbutton_image
2080 && (fwin->lbutton_image->image != pixmap
2081 || fwin->lbutton_image->mask != mask)) {
2082 wPixmapDestroy(fwin->lbutton_image);
2083 fwin->lbutton_image = NULL;
2086 if (!fwin->lbutton_image) {
2087 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2088 fwin->lbutton_image->client_owned = 1;
2089 fwin->lbutton_image->client_owned_mask = 1;
2091 } else {
2092 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2093 wPixmapDestroy(fwin->lbutton_image);
2095 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2099 #ifdef XKB_BUTTON_HINT
2100 if (!WFLAGP(wwin, no_language_button)) {
2101 if (fwin->languagebutton_image &&
2102 !fwin->languagebutton_image->shared) {
2103 wPixmapDestroy(fwin->languagebutton_image);
2105 fwin->languagebutton_image =
2106 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2108 #endif
2110 /* close button */
2112 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2113 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2115 if (!WFLAGP(wwin, no_close_button)) {
2116 if (wwin->wm_gnustep_attr
2117 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2118 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2120 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2121 mask = wwin->wm_gnustep_attr->close_mask;
2122 else
2123 mask = None;
2125 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2126 || fwin->rbutton_image->mask != mask)) {
2127 wPixmapDestroy(fwin->rbutton_image);
2128 fwin->rbutton_image = NULL;
2131 if (!fwin->rbutton_image) {
2132 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2133 fwin->rbutton_image->client_owned = 1;
2134 fwin->rbutton_image->client_owned_mask = 1;
2137 } else if (WFLAGP(wwin, kill_close)) {
2139 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2140 wPixmapDestroy(fwin->rbutton_image);
2142 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2144 } else if (MGFLAGP(wwin, broken_close)) {
2146 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2147 wPixmapDestroy(fwin->rbutton_image);
2149 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2151 } else {
2153 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2154 wPixmapDestroy(fwin->rbutton_image);
2156 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2160 /* force buttons to be redrawn */
2161 fwin->flags.need_texture_change = 1;
2162 wFrameWindowPaint(fwin);
2167 *---------------------------------------------------------------------------
2168 * wWindowConfigureBorders--
2169 * Update window border configuration according to attribute flags.
2171 *---------------------------------------------------------------------------
2173 void
2174 wWindowConfigureBorders(WWindow *wwin)
2176 if (wwin->frame) {
2177 int flags;
2178 int newy, oldh;
2180 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2181 #ifdef XKB_BUTTON_HINT
2182 flags |= WFF_LANGUAGE_BUTTON;
2183 #endif
2184 if (!WFLAGP(wwin, no_titlebar))
2185 flags |= WFF_TITLEBAR;
2186 if (!WFLAGP(wwin, no_resizebar))
2187 flags |= WFF_RESIZEBAR;
2188 if (!WFLAGP(wwin, no_border))
2189 flags |= WFF_BORDER;
2190 if (wwin->flags.shaded)
2191 flags |= WFF_IS_SHADED;
2193 oldh = wwin->frame->top_width;
2194 wFrameWindowUpdateBorders(wwin->frame, flags);
2195 if (oldh != wwin->frame->top_width) {
2196 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2198 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2199 wWindowConfigure(wwin, wwin->frame_x, newy,
2200 wwin->client.width, wwin->client.height);
2203 flags = 0;
2204 if (!WFLAGP(wwin, no_miniaturize_button)
2205 && wwin->frame->flags.hide_left_button)
2206 flags |= WFF_LEFT_BUTTON;
2208 #ifdef XKB_BUTTON_HINT
2209 if (!WFLAGP(wwin, no_language_button)
2210 && wwin->frame->flags.hide_language_button)
2211 flags |= WFF_LANGUAGE_BUTTON;
2212 #endif
2214 if (!WFLAGP(wwin, no_close_button)
2215 && wwin->frame->flags.hide_right_button)
2216 flags |= WFF_RIGHT_BUTTON;
2218 if (flags!=0) {
2219 wWindowUpdateButtonImages(wwin);
2220 wFrameWindowShowButton(wwin->frame, flags);
2223 flags = 0;
2224 if (WFLAGP(wwin, no_miniaturize_button)
2225 && !wwin->frame->flags.hide_left_button)
2226 flags |= WFF_LEFT_BUTTON;
2228 #ifdef XKB_BUTTON_HINT
2229 if (WFLAGP(wwin, no_language_button)
2230 && !wwin->frame->flags.hide_language_button)
2231 flags |= WFF_LANGUAGE_BUTTON;
2232 #endif
2234 if (WFLAGP(wwin, no_close_button)
2235 && !wwin->frame->flags.hide_right_button)
2236 flags |= WFF_RIGHT_BUTTON;
2238 if (flags!=0)
2239 wFrameWindowHideButton(wwin->frame, flags);
2241 #ifdef SHAPE
2242 if (wShapeSupported && wwin->flags.shaped) {
2243 wWindowSetShape(wwin);
2245 #endif
2250 void
2251 wWindowSaveState(WWindow *wwin)
2253 CARD32 data[10];
2254 int i;
2256 memset(data, 0, sizeof(CARD32)*10);
2257 data[0] = wwin->frame->workspace;
2258 data[1] = wwin->flags.miniaturized;
2259 data[2] = wwin->flags.shaded;
2260 data[3] = wwin->flags.hidden;
2262 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2263 if (wwin->screen_ptr->shortcutWindows[i] &&
2264 WMCountInBag(wwin->screen_ptr->shortcutWindows[i], wwin))
2265 data[9] |= 1<<i;
2267 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2268 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2269 (unsigned char *)data, 10);
2273 static int
2274 getSavedState(Window window, WSavedState **state)
2276 Atom type_ret;
2277 int fmt_ret;
2278 unsigned long nitems_ret;
2279 unsigned long bytes_after_ret;
2280 CARD32 *data;
2282 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2283 True, _XA_WINDOWMAKER_STATE,
2284 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2285 (unsigned char **)&data)!=Success || !data)
2286 return 0;
2288 *state = malloc(sizeof(WSavedState));
2290 if (*state) {
2291 (*state)->workspace = data[0];
2292 (*state)->miniaturized = data[1];
2293 (*state)->shaded = data[2];
2294 (*state)->hidden = data[3];
2295 (*state)->use_geometry = data[4];
2296 (*state)->x = data[5];
2297 (*state)->y = data[6];
2298 (*state)->w = data[7];
2299 (*state)->h = data[8];
2300 (*state)->window_shortcuts = data[9];
2302 XFree(data);
2304 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2305 return 1;
2306 else
2307 return 0;
2311 #ifdef SHAPE
2312 void
2313 wWindowClearShape(WWindow *wwin)
2315 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2316 0, wwin->frame->top_width, None, ShapeSet);
2317 XFlush(dpy);
2320 void
2321 wWindowSetShape(WWindow *wwin)
2323 XRectangle rect[2];
2324 int count;
2325 #ifdef OPTIMIZE_SHAPE
2326 XRectangle *rects;
2327 XRectangle *urec;
2328 int ordering;
2330 /* only shape is the client's */
2331 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2332 goto alt_code;
2335 /* Get array of rectangles describing the shape mask */
2336 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2337 &count, &ordering);
2338 if (!rects) {
2339 goto alt_code;
2342 urec = malloc(sizeof(XRectangle)*(count+2));
2343 if (!urec) {
2344 XFree(rects);
2345 goto alt_code;
2348 /* insert our decoration rectangles in the rect list */
2349 memcpy(urec, rects, sizeof(XRectangle)*count);
2350 XFree(rects);
2352 if (!WFLAGP(wwin, no_titlebar)) {
2353 urec[count].x = -1;
2354 urec[count].y = -1 - wwin->frame->top_width;
2355 urec[count].width = wwin->frame->core->width + 2;
2356 urec[count].height = wwin->frame->top_width + 1;
2357 count++;
2359 if (!WFLAGP(wwin, no_resizebar)) {
2360 urec[count].x = -1;
2361 urec[count].y = wwin->frame->core->height
2362 - wwin->frame->bottom_width - wwin->frame->top_width;
2363 urec[count].width = wwin->frame->core->width + 2;
2364 urec[count].height = wwin->frame->bottom_width + 1;
2365 count++;
2368 /* shape our frame window */
2369 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2370 0, wwin->frame->top_width, urec, count,
2371 ShapeSet, Unsorted);
2372 XFlush(dpy);
2373 free(urec);
2374 return;
2376 alt_code:
2377 #endif /* OPTIMIZE_SHAPE */
2378 count = 0;
2379 if (!WFLAGP(wwin, no_titlebar)) {
2380 rect[count].x = -1;
2381 rect[count].y = -1;
2382 rect[count].width = wwin->frame->core->width + 2;
2383 rect[count].height = wwin->frame->top_width + 1;
2384 count++;
2386 if (!WFLAGP(wwin, no_resizebar)) {
2387 rect[count].x = -1;
2388 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2389 rect[count].width = wwin->frame->core->width + 2;
2390 rect[count].height = wwin->frame->bottom_width + 1;
2391 count++;
2393 if (count > 0) {
2394 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2395 0, 0, rect, count, ShapeSet, Unsorted);
2397 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2398 0, wwin->frame->top_width, wwin->client_win,
2399 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2400 XFlush(dpy);
2402 #endif /* SHAPE */
2404 /* ====================================================================== */
2406 static FocusMode
2407 getFocusMode(WWindow *wwin)
2409 FocusMode mode;
2411 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2412 if (wwin->wm_hints->input == True) {
2413 if (wwin->protocols.TAKE_FOCUS)
2414 mode = WFM_LOCALLY_ACTIVE;
2415 else
2416 mode = WFM_PASSIVE;
2417 } else {
2418 if (wwin->protocols.TAKE_FOCUS)
2419 mode = WFM_GLOBALLY_ACTIVE;
2420 else
2421 mode = WFM_NO_INPUT;
2423 } else {
2424 mode = WFM_PASSIVE;
2426 return mode;
2430 void
2431 wWindowSetKeyGrabs(WWindow *wwin)
2433 int i;
2434 WShortKey *key;
2436 for (i=0; i<WKBD_LAST; i++) {
2437 key = &wKeyBindings[i];
2439 if (key->keycode==0)
2440 continue;
2441 if (key->modifier!=AnyModifier) {
2442 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2443 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2444 #ifdef NUMLOCK_HACK
2445 /* Also grab all modifier combinations possible that include,
2446 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2447 * work even if the NumLock/ScrollLock key is on.
2449 wHackedGrabKey(key->keycode, key->modifier,
2450 wwin->frame->core->window, True, GrabModeAsync,
2451 GrabModeAsync);
2452 #endif
2454 XGrabKey(dpy, key->keycode, key->modifier,
2455 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2458 #ifndef LITE
2459 wRootMenuBindShortcuts(wwin->frame->core->window);
2460 #endif
2465 void
2466 wWindowResetMouseGrabs(WWindow *wwin)
2468 /* Mouse grabs can't be done on the client window because of
2469 * ICCCM and because clients that try to do the same will crash.
2471 * But there is a problem wich makes tbar buttons of unfocused
2472 * windows not usable as the click goes to the frame window instead
2473 * of the button itself. Must figure a way to fix that.
2476 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2478 if (!WFLAGP(wwin, no_bind_mouse)) {
2479 /* grabs for Meta+drag */
2480 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2481 True, ButtonPressMask, GrabModeSync,
2482 GrabModeAsync, None, None);
2485 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2486 && !wwin->flags.is_gnustep) {
2487 /* the passive grabs to focus the window */
2488 if (wPreferences.focus_mode == WKF_CLICK)
2489 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2490 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2491 None, None);
2493 XFlush(dpy);
2497 void
2498 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2500 if (attr->flags & GSExtraFlagsAttr) {
2501 if (MGFLAGP(wwin, broken_close) !=
2502 (attr->extra_flags & GSDocumentEditedFlag)) {
2503 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2504 wWindowUpdateButtonImages(wwin);
2510 WMagicNumber
2511 wWindowAddSavedState(char *instance, char *class, char *command,
2512 pid_t pid, WSavedState *state)
2514 WWindowState *wstate;
2516 wstate = malloc(sizeof(WWindowState));
2517 if (!wstate)
2518 return 0;
2520 memset(wstate, 0, sizeof(WWindowState));
2521 wstate->pid = pid;
2522 if (instance)
2523 wstate->instance = wstrdup(instance);
2524 if (class)
2525 wstate->class = wstrdup(class);
2526 if (command)
2527 wstate->command = wstrdup(command);
2528 wstate->state = state;
2530 wstate->next = windowState;
2531 windowState = wstate;
2533 #ifdef DEBUG
2534 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2535 class, command);
2536 #endif
2538 return wstate;
2542 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2545 WMagicNumber
2546 wWindowGetSavedState(Window win)
2548 char *instance, *class, *command=NULL;
2549 WWindowState *wstate = windowState;
2550 char **argv;
2551 int argc;
2553 if (!wstate)
2554 return NULL;
2556 if (XGetCommand(dpy, win, &argv, &argc)) {
2557 if (argc > 0)
2558 command = FlattenStringList(argv, argc);
2559 XFreeStringList(argv);
2561 if (!command)
2562 return NULL;
2564 if (PropGetWMClass(win, &class, &instance)) {
2565 while (wstate) {
2566 if (SAME(instance, wstate->instance) &&
2567 SAME(class, wstate->class) &&
2568 SAME(command, wstate->command)) {
2569 break;
2571 wstate = wstate->next;
2573 } else {
2574 wstate = NULL;
2577 #ifdef DEBUG
2578 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2579 class, command);
2580 #endif
2582 if (command) free(command);
2583 if (instance) XFree(instance);
2584 if (class) XFree(class);
2586 return wstate;
2590 void
2591 wWindowDeleteSavedState(WMagicNumber id)
2593 WWindowState *tmp, *wstate=(WWindowState*)id;
2595 if (!wstate || !windowState)
2596 return;
2598 tmp = windowState;
2599 if (tmp==wstate) {
2600 windowState = wstate->next;
2601 #ifdef DEBUG
2602 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2603 wstate, wstate->instance, wstate->class, wstate->command);
2604 #endif
2605 if (wstate->instance) free(wstate->instance);
2606 if (wstate->class) free(wstate->class);
2607 if (wstate->command) free(wstate->command);
2608 free(wstate->state);
2609 free(wstate);
2610 } else {
2611 while (tmp->next) {
2612 if (tmp->next==wstate) {
2613 tmp->next=wstate->next;
2614 #ifdef DEBUG
2615 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2616 wstate, wstate->instance, wstate->class, wstate->command);
2617 #endif
2618 if (wstate->instance) free(wstate->instance);
2619 if (wstate->class) free(wstate->class);
2620 if (wstate->command) free(wstate->command);
2621 free(wstate->state);
2622 free(wstate);
2623 break;
2625 tmp = tmp->next;
2631 void
2632 wWindowDeleteSavedStatesForPID(pid_t pid)
2634 WWindowState *tmp, *wstate;
2636 if (!windowState)
2637 return;
2639 tmp = windowState;
2640 if (tmp->pid == pid) {
2641 wstate = windowState;
2642 windowState = tmp->next;
2643 #ifdef DEBUG
2644 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2645 wstate, wstate->instance, wstate->class, wstate->command);
2646 #endif
2647 if (wstate->instance) free(wstate->instance);
2648 if (wstate->class) free(wstate->class);
2649 if (wstate->command) free(wstate->command);
2650 free(wstate->state);
2651 free(wstate);
2652 } else {
2653 while (tmp->next) {
2654 if (tmp->next->pid==pid) {
2655 wstate = tmp->next;
2656 tmp->next = wstate->next;
2657 #ifdef DEBUG
2658 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2659 wstate, wstate->instance, wstate->class, wstate->command);
2660 #endif
2661 if (wstate->instance) free(wstate->instance);
2662 if (wstate->class) free(wstate->class);
2663 if (wstate->command) free(wstate->command);
2664 free(wstate->state);
2665 free(wstate);
2666 break;
2668 tmp = tmp->next;
2674 /* ====================================================================== */
2676 static void
2677 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2679 WWindow *wwin = data;
2681 #ifndef NUMLOCK_HACK
2682 if ((event->xbutton.state & ValidModMask)
2683 != (event->xbutton.state & ~LockMask)) {
2684 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2685 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2687 #endif
2689 event->xbutton.state &= ValidModMask;
2691 CloseWindowMenu(wwin->screen_ptr);
2693 if (wPreferences.focus_mode==WKF_CLICK
2694 && !(event->xbutton.state&ControlMask)
2695 && !WFLAGP(wwin, no_focusable)) {
2696 wSetFocusTo(wwin->screen_ptr, wwin);
2699 if (event->xbutton.button == Button1)
2700 wRaiseFrame(wwin->frame->core);
2702 if (event->xbutton.window != wwin->frame->resizebar->window) {
2703 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2704 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2705 GrabModeAsync, GrabModeAsync, None,
2706 None, CurrentTime)!=GrabSuccess) {
2707 #ifdef DEBUG0
2708 wwarning("pointer grab failed for window move");
2709 #endif
2710 return;
2714 if (event->xbutton.state & MOD_MASK) {
2715 /* move the window */
2716 wMouseMoveWindow(wwin, event);
2717 XUngrabPointer(dpy, CurrentTime);
2718 } else {
2719 wMouseResizeWindow(wwin, event);
2720 XUngrabPointer(dpy, CurrentTime);
2726 static void
2727 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2729 WWindow *wwin = data;
2731 event->xbutton.state &= ValidModMask;
2733 if (event->xbutton.button==Button1) {
2734 if (event->xbutton.state == 0) {
2735 if (!WFLAGP(wwin, no_shadeable)) {
2736 /* shade window */
2737 if (wwin->flags.shaded)
2738 wUnshadeWindow(wwin);
2739 else
2740 wShadeWindow(wwin);
2742 } else {
2743 int dir = 0;
2745 if (event->xbutton.state & ControlMask)
2746 dir |= MAX_VERTICAL;
2748 if (event->xbutton.state & ShiftMask) {
2749 dir |= MAX_HORIZONTAL;
2750 if (!(event->xbutton.state & ControlMask))
2751 wSelectWindow(wwin, !wwin->flags.selected);
2754 /* maximize window */
2755 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2756 if (wwin->flags.maximized)
2757 wUnmaximizeWindow(wwin);
2758 else
2759 wMaximizeWindow(wwin, dir);
2762 } else if (event->xbutton.button==Button3) {
2763 if (event->xbutton.state & MOD_MASK) {
2764 wHideOtherApplications(wwin);
2766 } else if (event->xbutton.button==Button2) {
2767 wSelectWindow(wwin, !wwin->flags.selected);
2772 static void
2773 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2775 WWindow *wwin = desc->parent;
2777 event->xbutton.state &= ValidModMask;
2779 CloseWindowMenu(wwin->screen_ptr);
2781 if (wPreferences.focus_mode==WKF_CLICK
2782 && !(event->xbutton.state&ControlMask)
2783 && !WFLAGP(wwin, no_focusable)) {
2784 wSetFocusTo(wwin->screen_ptr, wwin);
2786 if (event->xbutton.button == Button1) {
2787 wRaiseFrame(wwin->frame->core);
2790 if (event->xbutton.state & MOD_MASK) {
2791 /* move the window */
2792 if (XGrabPointer(dpy, wwin->client_win, False,
2793 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2794 GrabModeAsync, GrabModeAsync, None,
2795 None, CurrentTime)!=GrabSuccess) {
2796 #ifdef DEBUG0
2797 wwarning("pointer grab failed for window move");
2798 #endif
2799 return;
2801 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2802 wMouseResizeWindow(wwin, event);
2803 else
2804 wMouseMoveWindow(wwin, event);
2805 XUngrabPointer(dpy, CurrentTime);
2810 static void
2811 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2813 WWindow *wwin = (WWindow*)data;
2815 #ifndef NUMLOCK_HACK
2816 if ((event->xbutton.state & ValidModMask)
2817 != (event->xbutton.state & ~LockMask)) {
2818 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2819 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2821 #endif
2822 event->xbutton.state &= ValidModMask;
2824 CloseWindowMenu(wwin->screen_ptr);
2826 if (wPreferences.focus_mode==WKF_CLICK
2827 && !(event->xbutton.state&ControlMask)
2828 && !WFLAGP(wwin, no_focusable)) {
2829 wSetFocusTo(wwin->screen_ptr, wwin);
2832 if (event->xbutton.button == Button1
2833 || event->xbutton.button == Button2) {
2835 if (event->xbutton.button == Button1) {
2836 if (event->xbutton.state & MOD_MASK) {
2837 wLowerFrame(wwin->frame->core);
2838 } else {
2839 wRaiseFrame(wwin->frame->core);
2842 if ((event->xbutton.state & ShiftMask)
2843 && !(event->xbutton.state & ControlMask)) {
2844 wSelectWindow(wwin, !wwin->flags.selected);
2845 return;
2847 if (event->xbutton.window != wwin->frame->titlebar->window
2848 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2849 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2850 GrabModeAsync, GrabModeAsync, None,
2851 None, CurrentTime)!=GrabSuccess) {
2852 #ifdef DEBUG0
2853 wwarning("pointer grab failed for window move");
2854 #endif
2855 return;
2858 /* move the window */
2859 wMouseMoveWindow(wwin, event);
2861 XUngrabPointer(dpy, CurrentTime);
2862 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2863 && !wwin->flags.internal_window
2864 && !WCHECK_STATE(WSTATE_MODAL)) {
2865 WObjDescriptor *desc;
2867 if (event->xbutton.window != wwin->frame->titlebar->window
2868 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2869 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2870 GrabModeAsync, GrabModeAsync, None,
2871 None, CurrentTime)!=GrabSuccess) {
2872 #ifdef DEBUG0
2873 wwarning("pointer grab failed for window move");
2874 #endif
2875 return;
2878 OpenWindowMenu(wwin, event->xbutton.x_root,
2879 wwin->frame_y+wwin->frame->top_width, False);
2881 /* allow drag select */
2882 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2883 event->xany.send_event = True;
2884 (*desc->handle_mousedown)(desc, event);
2886 XUngrabPointer(dpy, CurrentTime);
2892 static void
2893 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2895 WWindow *wwin = data;
2897 event->xbutton.state &= ValidModMask;
2899 CloseWindowMenu(wwin->screen_ptr);
2901 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2902 return;
2904 /* if control-click, kill the client */
2905 if (event->xbutton.state & ControlMask) {
2906 wClientKill(wwin);
2907 } else {
2908 #ifdef OLWM_HINTS
2909 if (wwin->flags.olwm_push_pin_out) {
2911 wwin->flags.olwm_push_pin_out = 0;
2913 wOLWMChangePushpinState(wwin, True);
2915 wFrameWindowUpdatePushButton(wwin->frame, False);
2917 return;
2919 #endif
2920 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2921 /* send delete message */
2922 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2928 static void
2929 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2931 WWindow *wwin = data;
2933 CloseWindowMenu(wwin->screen_ptr);
2935 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2936 return;
2938 /* send delete message */
2939 if (wwin->protocols.DELETE_WINDOW) {
2940 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2941 } else {
2942 wClientKill(wwin);
2947 #ifdef XKB_BUTTON_HINT
2948 static void
2949 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2951 WWindow *wwin = data;
2952 WFrameWindow *fwin = wwin->frame;
2953 WScreen *scr = fwin->screen_ptr;
2954 XkbStateRec staterec;
2955 int tl;
2957 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2958 return;
2959 tl = wwin->frame->languagemode;
2960 wwin->frame->languagemode = wwin->frame->last_languagemode;
2961 wwin->frame->last_languagemode = tl;
2962 wSetFocusTo(scr, wwin);
2963 wwin->frame->languagebutton_image =
2964 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
2965 wwin->frame->languagemode];
2966 wFrameWindowUpdateLanguageButton(wwin->frame);
2967 if (event->xbutton.button == Button3)
2968 return;
2969 wRaiseFrame(fwin->core);
2971 #endif
2974 static void
2975 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2977 WWindow *wwin = data;
2979 event->xbutton.state &= ValidModMask;
2981 CloseWindowMenu(wwin->screen_ptr);
2983 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2984 return;
2986 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2987 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2988 LastTimestamp);
2989 } else {
2990 WApplication *wapp;
2991 if ((event->xbutton.state & ControlMask) ||
2992 (event->xbutton.button == Button3)) {
2994 wapp = wApplicationOf(wwin->main_window);
2995 if (wapp && !WFLAGP(wwin, no_appicon))
2996 wHideApplication(wapp);
2997 } else if (event->xbutton.state==0) {
2998 wIconifyWindow(wwin);