changed geometry display stuff
[wmaker-crm.git] / src / window.c
blobff43629e607d87c2cd4c85b2572d880b80f423f0
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 (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
655 wwin->wm_gnustep_attr = NULL;
658 wwin->client_leader = PropGetClientLeader(window);
659 if (wwin->client_leader!=None)
660 wwin->main_window = wwin->client_leader;
662 wwin->wm_hints = XGetWMHints(dpy, window);
664 if (wwin->wm_hints) {
665 if (wwin->wm_hints->flags & StateHint) {
667 if (wwin->wm_hints->initial_state == IconicState) {
669 wwin->flags.miniaturized = 1;
671 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
673 withdraw = True;
677 if (wwin->wm_hints->flags & WindowGroupHint) {
678 wwin->group_id = wwin->wm_hints->window_group;
679 /* window_group has priority over CLIENT_LEADER */
680 wwin->main_window = wwin->group_id;
681 } else {
682 wwin->group_id = None;
685 if (wwin->wm_hints->flags & UrgencyHint)
686 wwin->flags.urgent = 1;
687 } else {
688 wwin->group_id = None;
691 PropGetProtocols(window, &wwin->protocols);
693 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
694 wwin->transient_for = None;
695 } else {
696 if (wwin->transient_for==None || wwin->transient_for==window) {
697 wwin->transient_for = scr->root_win;
698 } else {
699 transientOwner = wWindowFor(wwin->transient_for);
700 if (transientOwner && transientOwner->main_window!=None) {
701 wwin->main_window = transientOwner->main_window;
702 } /*else {
703 wwin->main_window = None;
708 /* guess the focus mode */
709 wwin->focus_mode = getFocusMode(wwin);
711 /* get geometry stuff */
712 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
714 /* get colormap windows */
715 GetColormapWindows(wwin);
718 *--------------------------------------------------
720 * Setup the decoration/window attributes and
721 * geometry
723 *--------------------------------------------------
726 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
728 #ifdef OLWM_HINTS
729 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
730 && wwin->group_id != None && wwin->group_id != window) {
732 transientOwner = wWindowFor(wwin->group_id);
734 if (transientOwner) {
735 wwin->transient_for = wwin->group_id;
737 /* transients can't be iconified or maximized */
738 if (wwin->transient_for) {
739 WSETUFLAG(wwin, no_miniaturizable, 1);
740 WSETUFLAG(wwin, no_miniaturize_button, 1);
744 #endif /* OLWM_HINTS */
747 * Make broken apps behave as a nice app.
749 if (WFLAGP(wwin, emulate_appicon)) {
750 wwin->main_window = wwin->client_win;
754 *------------------------------------------------------------
756 * Setup the initial state of the window
758 *------------------------------------------------------------
761 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
762 wwin->flags.miniaturized = 1;
765 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
766 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
769 #ifdef GNOME_STUFF
770 wGNOMECheckInitialClientState(wwin);
771 #endif
772 #ifdef KWM_HINTS
773 wKWMCheckClientInitialState(wwin);
774 #endif
776 /* apply previous state if it exists and we're in startup */
777 if (scr->flags.startup && wm_state >= 0) {
779 if (wm_state == IconicState) {
781 wwin->flags.miniaturized = 1;
783 } else if (wm_state == WithdrawnState) {
785 withdraw = True;
789 /* if there is a saved state (from file), restore it */
790 win_state = NULL;
791 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
792 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
793 } else {
794 win_state = (WWindowState*)wWindowGetSavedState(window);
796 if (win_state && !withdraw) {
798 if (win_state->state->hidden>0)
799 wwin->flags.hidden = win_state->state->hidden;
801 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
802 wwin->flags.shaded = win_state->state->shaded;
804 if (win_state->state->miniaturized>0 &&
805 !WFLAGP(wwin, no_miniaturizable)) {
806 wwin->flags.miniaturized = win_state->state->miniaturized;
808 if (!IS_OMNIPRESENT(wwin)) {
809 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
810 wwin->wm_class);
811 if (w < 0 || w >= scr->workspace_count) {
812 workspace = win_state->state->workspace;
813 if (workspace >= scr->workspace_count)
814 workspace = scr->current_workspace;
815 } else {
816 workspace = w;
818 } else {
819 workspace = scr->current_workspace;
823 /* if we're restarting, restore saved state (from hints).
824 * This will overwrite previous */
826 WSavedState *wstate;
828 if (getSavedState(window, &wstate)) {
829 wwin->flags.shaded = wstate->shaded;
830 wwin->flags.hidden = wstate->hidden;
831 wwin->flags.miniaturized = wstate->miniaturized;
832 workspace = wstate->workspace;
834 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
835 int i;
837 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
838 if (wstate->window_shortcuts & (1<<i)) {
839 if (!scr->shortcutWindows[i])
840 scr->shortcutWindows[i] = WMCreateBag(4);
842 WMPutInBag(scr->shortcutWindows[i], wwin);
846 free(wstate);
850 /* don't let transients start miniaturized if their owners are not */
851 if (transientOwner && !transientOwner->flags.miniaturized
852 && wwin->flags.miniaturized && !withdraw) {
853 wwin->flags.miniaturized = 0;
854 if (wwin->wm_hints)
855 wwin->wm_hints->initial_state = NormalState;
858 /* set workspace on which the window starts */
859 if (workspace >= 0) {
860 if (workspace > scr->workspace_count-1) {
861 workspace = workspace % scr->workspace_count;
863 } else {
864 int w;
866 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
868 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
870 workspace = w;
872 } else {
873 if (wPreferences.open_transients_with_parent && transientOwner) {
875 workspace = transientOwner->frame->workspace;
877 } else {
879 workspace = scr->current_workspace;
884 /* setup window geometry */
885 if (win_state && win_state->state->use_geometry) {
886 width = win_state->state->w;
887 height = win_state->state->h;
889 wWindowConstrainSize(wwin, &width, &height);
891 /* do not ask for window placement if the window is
892 * transient, during startup, if the initial workspace is another one
893 * or if the window wants to start iconic.
894 * If geometry was saved, restore it. */
896 Bool dontBring = False;
898 if (win_state && win_state->state->use_geometry) {
899 x = win_state->state->x;
900 y = win_state->state->y;
901 } else if ((wwin->transient_for==None
902 || wPreferences.window_placement!=WPM_MANUAL)
903 && !scr->flags.startup
904 && workspace == scr->current_workspace
905 && !wwin->flags.miniaturized
906 && !wwin->flags.maximized
907 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
908 PlaceWindow(wwin, &x, &y, width, height);
909 if (wPreferences.window_placement == WPM_MANUAL)
910 dontBring = True;
913 if (WFLAGP(wwin, dont_move_off) && dontBring)
914 wScreenBringInside(scr, &x, &y, width, height);
917 if (wwin->flags.urgent) {
918 if (!IS_OMNIPRESENT(wwin))
919 wwin->flags.omnipresent ^= 1;
923 *--------------------------------------------------
925 * Create frame, borders and do reparenting
927 *--------------------------------------------------
929 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
930 #ifdef XKB_BUTTON_HINT
931 if (wPreferences.modelock)
932 foo |= WFF_LANGUAGE_BUTTON;
933 #endif
934 if (!WFLAGP(wwin, no_titlebar))
935 foo |= WFF_TITLEBAR;
936 if (!WFLAGP(wwin, no_resizebar))
937 foo |= WFF_RESIZEBAR;
938 if (!WFLAGP(wwin, no_border))
939 foo |= WFF_BORDER;
941 wwin->frame = wFrameWindowCreate(scr, window_level,
942 x, y, width, height,
943 &wPreferences.window_title_clearance, foo,
944 scr->window_title_texture,
945 scr->resizebar_texture,
946 scr->window_title_pixel,
947 #ifdef DRAWSTRING_PLUGIN
948 W_STRING_FTITLE,
949 #endif
950 &scr->window_title_gc,
951 &scr->title_font);
953 wwin->frame->flags.is_client_window_frame = 1;
954 wwin->frame->flags.justification = wPreferences.title_justification;
956 /* setup button images */
957 wWindowUpdateButtonImages(wwin);
959 /* hide unused buttons */
960 foo = 0;
961 if (WFLAGP(wwin, no_close_button))
962 foo |= WFF_RIGHT_BUTTON;
963 if (WFLAGP(wwin, no_miniaturize_button))
964 foo |= WFF_LEFT_BUTTON;
965 #ifdef XKB_BUTTON_HINT
966 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
967 foo |= WFF_LANGUAGE_BUTTON;
968 #endif
969 if (foo!=0)
970 wFrameWindowHideButton(wwin->frame, foo);
972 wwin->frame->child = wwin;
974 #ifdef OLWM_HINTS
975 /* emulate olwm push pin. Make the button look as pushed-in for
976 * the pinned-out state. When the button is clicked, it will
977 * revert to the normal position, which means the pin is pinned-in.
979 if (wwin->flags.olwm_push_pin_out)
980 wFrameWindowUpdatePushButton(wwin->frame, True);
981 #endif /* OLWM_HINTS */
983 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
984 if (title)
985 XFree(title);
987 wwin->frame->workspace = workspace;
989 wwin->frame->on_click_left = windowIconifyClick;
990 #ifdef XKB_BUTTON_HINT
991 if (wPreferences.modelock)
992 wwin->frame->on_click_language = windowLanguageClick;
993 #endif
995 wwin->frame->on_click_right = windowCloseClick;
996 wwin->frame->on_dblclick_right = windowCloseDblClick;
998 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
999 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1001 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1004 XSelectInput(dpy, wwin->client_win,
1005 wwin->event_mask & ~StructureNotifyMask);
1007 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1008 0, wwin->frame->top_width);
1010 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1014 int gx, gy;
1016 wClientGetGravityOffsets(wwin, &gx, &gy);
1018 /* if gravity is to the south, account for the border sizes */
1019 if (gy > 0)
1020 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1024 * wWindowConfigure() will init the client window's size
1025 * (wwin->client.{width,height}) and all other geometry
1026 * related variables (frame_x,frame_y)
1028 wWindowConfigure(wwin, x, y, width, height);
1030 /* to make sure the window receives it's new position after reparenting */
1031 wWindowSynthConfigureNotify(wwin);
1034 *--------------------------------------------------
1036 * Setup descriptors and save window to internal
1037 * lists
1039 *--------------------------------------------------
1042 if (wwin->main_window!=None) {
1043 WApplication *app;
1044 WWindow *leader;
1046 /* Leader windows do not necessary set themselves as leaders.
1047 * If this is the case, point the leader of this window to
1048 * itself */
1049 leader = wWindowFor(wwin->main_window);
1050 if (leader && leader->main_window==None) {
1051 leader->main_window = leader->client_win;
1053 app = wApplicationCreate(scr, wwin->main_window);
1054 if (app) {
1055 app->last_workspace = workspace;
1058 * Do application specific stuff, like setting application
1059 * wide attributes.
1062 if (wwin->flags.hidden) {
1063 /* if the window was set to hidden because it was hidden
1064 * in a previous incarnation and that state was restored */
1065 app->flags.hidden = 1;
1068 if (app->flags.hidden) {
1069 wwin->flags.hidden = 1;
1074 /* setup the frame descriptor */
1075 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1076 wwin->frame->core->descriptor.parent = wwin;
1077 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1079 /* don't let windows go away if we die */
1080 XAddToSaveSet(dpy, window);
1082 XLowerWindow(dpy, window);
1084 /* if window is in this workspace and should be mapped, then map it */
1085 if (!wwin->flags.miniaturized
1086 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1087 && !wwin->flags.hidden && !withdraw) {
1089 /* The following "if" is to avoid crashing of clients that expect
1090 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1091 * after the return from this function.
1093 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1094 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1095 } else {
1096 wClientSetState(wwin, NormalState, None);
1099 #if 0
1100 /* if not auto focus, then map the window under the currently
1101 * focused window */
1102 #define _WIDTH(w) (w)->frame->core->width
1103 #define _HEIGHT(w) (w)->frame->core->height
1104 if (!wPreferences.auto_focus && scr->focused_window
1105 && !scr->flags.startup && !transientOwner
1106 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1107 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1108 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1109 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1110 || wwin->flags.maximized)) {
1111 MoveInStackListUnder(scr->focused_window->frame->core,
1112 wwin->frame->core);
1114 #undef _WIDTH
1115 #undef _HEIGHT
1117 #endif
1119 if (wPreferences.superfluous && !wPreferences.no_animations
1120 && !scr->flags.startup && wwin->transient_for==None
1122 * The brain damaged idiotic non-click to focus modes will
1123 * have trouble with this because:
1125 * 1. window is created and mapped by the client
1126 * 2. window is mapped by wmaker in small size
1127 * 3. window is animated to grow to normal size
1128 * 4. this function returns to normal event loop
1129 * 5. eventually, the EnterNotify event that would trigger
1130 * the window focusing (if the mouse is over that window)
1131 * will be processed by wmaker.
1132 * But since this event will be rather delayed
1133 * (step 3 has a large delay) the time when the event ocurred
1134 * and when it is processed, the client that owns that window
1135 * will reject the XSetInputFocus() for it.
1137 && (wPreferences.focus_mode==WKF_CLICK
1138 || wPreferences.auto_focus)) {
1139 DoWindowBirth(wwin);
1142 wWindowMap(wwin);
1145 /* setup stacking descriptor */
1146 if (transientOwner) {
1147 /* && wPreferences.on_top_transients */
1148 if (transientOwner) {
1149 wwin->frame->core->stacking->child_of =
1150 transientOwner->frame->core;
1152 } else {
1153 wwin->frame->core->stacking->child_of = NULL;
1157 if (!scr->focused_window) {
1158 /* first window on the list */
1159 wwin->next = NULL;
1160 wwin->prev = NULL;
1161 scr->focused_window = wwin;
1162 } else {
1163 WWindow *tmp;
1165 /* add window at beginning of focus window list */
1166 tmp = scr->focused_window;
1167 while (tmp->prev)
1168 tmp = tmp->prev;
1169 tmp->prev = wwin;
1170 wwin->next = tmp;
1171 wwin->prev = NULL;
1174 #ifdef GNOME_STUFF
1175 wGNOMEUpdateClientStateHint(wwin, True);
1176 #endif
1177 #ifdef KWM_HINTS
1178 wKWMUpdateClientWorkspace(wwin);
1179 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1180 #endif
1182 XUngrabServer(dpy);
1185 *--------------------------------------------------
1187 * Final preparations before window is ready to go
1189 *--------------------------------------------------
1192 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1195 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1196 && !wwin->flags.hidden) {
1197 if (((transientOwner && transientOwner->flags.focused)
1198 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1199 wSetFocusTo(scr, wwin);
1201 wWindowResetMouseGrabs(wwin);
1203 if (!WFLAGP(wwin, no_bind_keys)) {
1204 wWindowSetKeyGrabs(wwin);
1206 #ifdef GNOME_STUFF
1207 wGNOMEUpdateClientListHint(scr);
1208 #endif
1209 #ifdef KWM_HINTS
1210 wwin->flags.kwm_managed = 1;
1212 wKWMSendEventMessage(wwin, WKWMAddWindow);
1213 #endif
1215 wColormapInstallForWindow(scr, scr->cmap_window);
1217 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1219 #ifdef OLWM_HINTS
1220 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1221 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1223 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1224 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1225 wwin->frame_y);
1227 #endif
1230 *------------------------------------------------------------
1231 * Setup Notification Observers
1232 *------------------------------------------------------------
1234 WMAddNotificationObserver(appearanceObserver, wwin,
1235 WNWindowAppearanceSettingsChanged, wwin);
1239 *--------------------------------------------------
1241 * Cleanup temporary stuff
1243 *--------------------------------------------------
1246 if (win_state)
1247 wWindowDeleteSavedState(win_state);
1249 /* If the window must be withdrawed, then do it now.
1250 * Must do some optimization, 'though */
1251 if (withdraw) {
1252 wwin->flags.mapped = 0;
1253 wClientSetState(wwin, WithdrawnState, None);
1254 wUnmanageWindow(wwin, True, False);
1255 wwin = NULL;
1258 return wwin;
1265 WWindow*
1266 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1267 char *title, int x, int y, int width, int height)
1269 WWindow *wwin;
1270 int foo;
1272 wwin = wWindowCreate();
1274 WMAddNotificationObserver(appearanceObserver, wwin,
1275 WNWindowAppearanceSettingsChanged, wwin);
1277 wwin->flags.internal_window = 1;
1279 WSETUFLAG(wwin, omnipresent, 1);
1280 WSETUFLAG(wwin, no_shadeable, 1);
1281 WSETUFLAG(wwin, no_resizable, 1);
1282 WSETUFLAG(wwin, no_miniaturizable, 1);
1284 wwin->focus_mode = WFM_PASSIVE;
1286 wwin->client_win = window;
1287 wwin->screen_ptr = scr;
1289 wwin->transient_for = owner;
1291 wwin->client.x = x;
1292 wwin->client.y = y;
1293 wwin->client.width = width;
1294 wwin->client.height = height;
1296 wwin->frame_x = wwin->client.x;
1297 wwin->frame_y = wwin->client.y;
1300 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1301 foo |= WFF_TITLEBAR;
1302 #ifdef XKB_BUTTON_HINT
1303 foo |= WFF_LANGUAGE_BUTTON;
1304 #endif
1306 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1307 wwin->frame_x, wwin->frame_y,
1308 width, height,
1309 &wPreferences.window_title_clearance, foo,
1310 scr->window_title_texture,
1311 scr->resizebar_texture,
1312 scr->window_title_pixel,
1313 #ifdef DRAWSTRING_PLUGIN
1314 W_STRING_FTITLE,
1315 #endif
1316 &scr->window_title_gc,
1317 &scr->title_font);
1319 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1321 wwin->frame->flags.is_client_window_frame = 1;
1322 wwin->frame->flags.justification = wPreferences.title_justification;
1324 wFrameWindowChangeTitle(wwin->frame, title);
1326 /* setup button images */
1327 wWindowUpdateButtonImages(wwin);
1329 /* hide buttons */
1330 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1332 wwin->frame->child = wwin;
1334 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1336 #ifdef XKB_BUTTON_HINT
1337 if (wPreferences.modelock)
1338 wwin->frame->on_click_language = windowLanguageClick;
1339 #endif
1341 wwin->frame->on_click_right = windowCloseClick;
1343 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1344 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1346 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1348 wwin->client.y += wwin->frame->top_width;
1349 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1350 0, wwin->frame->top_width);
1352 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1353 wwin->client.width, wwin->client.height);
1355 /* setup the frame descriptor */
1356 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1357 wwin->frame->core->descriptor.parent = wwin;
1358 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1361 XLowerWindow(dpy, window);
1362 XMapSubwindows(dpy, wwin->frame->core->window);
1364 /* setup stacking descriptor */
1365 if (
1366 #ifdef removed
1367 wPreferences.on_top_transients &&
1368 #endif
1369 wwin->transient_for!=None
1370 && wwin->transient_for!=scr->root_win) {
1371 WWindow *tmp;
1372 tmp = wWindowFor(wwin->transient_for);
1373 if (tmp)
1374 wwin->frame->core->stacking->child_of = tmp->frame->core;
1375 } else {
1376 wwin->frame->core->stacking->child_of = NULL;
1380 if (!scr->focused_window) {
1381 /* first window on the list */
1382 wwin->next = NULL;
1383 wwin->prev = NULL;
1384 scr->focused_window = wwin;
1385 } else {
1386 WWindow *tmp;
1388 /* add window at beginning of focus window list */
1389 tmp = scr->focused_window;
1390 while (tmp->prev)
1391 tmp = tmp->prev;
1392 tmp->prev = wwin;
1393 wwin->next = tmp;
1394 wwin->prev = NULL;
1397 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1399 /* if (wPreferences.auto_focus)*/
1400 wSetFocusTo(scr, wwin);
1402 wWindowResetMouseGrabs(wwin);
1404 wWindowSetKeyGrabs(wwin);
1406 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1408 return wwin;
1413 *----------------------------------------------------------------------
1414 * wUnmanageWindow--
1415 * Removes the frame window from a window and destroys all data
1416 * related to it. The window will be reparented back to the root window
1417 * if restore is True.
1419 * Side effects:
1420 * Everything related to the window is destroyed and the window
1421 * is removed from the window lists. Focus is set to the previous on the
1422 * window list.
1423 *----------------------------------------------------------------------
1425 void
1426 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1428 WCoreWindow *frame = wwin->frame->core;
1429 WWindow *owner = NULL;
1430 WWindow *newFocusedWindow = NULL;
1431 int wasFocused;
1432 WScreen *scr = wwin->screen_ptr;
1435 #ifdef KWM_HINTS
1436 wwin->frame->workspace = -1;
1438 wKWMUpdateClientWorkspace(wwin);
1439 #endif
1441 /* First close attribute editor window if open */
1442 if (wwin->flags.inspector_open) {
1443 wCloseInspectorForWindow(wwin);
1446 /* Close window menu if it's open for this window */
1447 if (wwin->flags.menu_open_for_me) {
1448 CloseWindowMenu(scr);
1451 if (!destroyed) {
1452 if (!wwin->flags.internal_window)
1453 XRemoveFromSaveSet(dpy, wwin->client_win);
1455 XSelectInput(dpy, wwin->client_win, NoEventMask);
1457 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1458 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1461 XUnmapWindow(dpy, frame->window);
1463 XUnmapWindow(dpy, wwin->client_win);
1465 /* deselect window */
1466 wSelectWindow(wwin, False);
1468 /* remove all pending events on window */
1469 /* I think this only matters for autoraise */
1470 if (wPreferences.raise_delay)
1471 WMDeleteTimerWithClientData(wwin->frame->core);
1473 XFlush(dpy);
1475 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1477 /* reparent the window back to the root */
1478 if (restore)
1479 wClientRestore(wwin);
1481 if (wwin->transient_for!=scr->root_win) {
1482 owner = wWindowFor(wwin->transient_for);
1483 if (owner) {
1484 if (!owner->flags.semi_focused) {
1485 owner = NULL;
1486 } else {
1487 owner->flags.semi_focused = 0;
1492 wasFocused = wwin->flags.focused;
1494 /* remove from window focus list */
1495 if (!wwin->prev && !wwin->next) {
1496 /* was the only window */
1497 scr->focused_window = NULL;
1498 newFocusedWindow = NULL;
1499 } else {
1500 WWindow *tmp;
1502 if (wwin->prev)
1503 wwin->prev->next = wwin->next;
1504 if (wwin->next)
1505 wwin->next->prev = wwin->prev;
1506 else {
1507 scr->focused_window = wwin->prev;
1508 scr->focused_window->next = NULL;
1511 if (wPreferences.focus_mode==WKF_CLICK) {
1513 /* if in click to focus mode and the window
1514 * was a transient, focus the owner window
1516 tmp = NULL;
1517 if (wPreferences.focus_mode==WKF_CLICK) {
1518 tmp = wWindowFor(wwin->transient_for);
1519 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1520 tmp = NULL;
1523 /* otherwise, focus the next one in the focus list */
1524 if (!tmp) {
1525 tmp = scr->focused_window;
1526 while (tmp) { /* look for one in the window list first */
1527 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1528 && (tmp->flags.mapped || tmp->flags.shaded))
1529 break;
1530 tmp = tmp->prev;
1532 if (!tmp) { /* if unsuccessful, choose any focusable window */
1533 tmp = scr->focused_window;
1534 while (tmp) {
1535 if (!WFLAGP(tmp, no_focusable)
1536 && (tmp->flags.mapped || tmp->flags.shaded))
1537 break;
1538 tmp = tmp->prev;
1543 newFocusedWindow = tmp;
1545 } else if (wPreferences.focus_mode==WKF_SLOPPY
1546 || wPreferences.focus_mode==WKF_POINTER) {
1547 unsigned int mask;
1548 int foo;
1549 Window bar, win;
1551 /* This is to let the root window get the keyboard input
1552 * if Sloppy focus mode and no other window get focus.
1553 * This way keybindings will not freeze.
1555 tmp = NULL;
1556 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1557 &foo, &foo, &foo, &foo, &mask))
1558 tmp = wWindowFor(win);
1559 if (tmp == wwin)
1560 tmp = NULL;
1561 newFocusedWindow = tmp;
1562 } else {
1563 newFocusedWindow = NULL;
1567 if (!wwin->flags.internal_window) {
1568 #ifdef GNOME_STUFF
1569 wGNOMERemoveClient(wwin);
1570 #endif
1571 #ifdef KWM_HINTS
1572 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1573 #endif
1576 #ifdef DEBUG
1577 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1578 (unsigned)frame->window);
1579 #endif
1581 if (wasFocused) {
1582 if (newFocusedWindow != owner && owner) {
1583 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1585 wSetFocusTo(scr, newFocusedWindow);
1587 wWindowDestroy(wwin);
1588 XFlush(dpy);
1592 void
1593 wWindowMap(WWindow *wwin)
1595 XMapWindow(dpy, wwin->frame->core->window);
1596 if (!wwin->flags.shaded) {
1597 /* window will be remapped when getting MapNotify */
1598 XSelectInput(dpy, wwin->client_win,
1599 wwin->event_mask & ~StructureNotifyMask);
1600 XMapWindow(dpy, wwin->client_win);
1601 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1603 wwin->flags.mapped = 1;
1608 void
1609 wWindowUnmap(WWindow *wwin)
1611 wwin->flags.mapped = 0;
1613 /* prevent window withdrawal when getting UnmapNotify */
1614 XSelectInput(dpy, wwin->client_win,
1615 wwin->event_mask & ~StructureNotifyMask);
1616 XUnmapWindow(dpy, wwin->client_win);
1617 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1619 XUnmapWindow(dpy, wwin->frame->core->window);
1624 void
1625 wWindowFocus(WWindow *wwin, WWindow *owin)
1627 WWindow *nowner;
1628 WWindow *oowner;
1630 #ifdef KEEP_XKB_LOCK_STATUS
1631 if (wPreferences.modelock) {
1632 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1634 #endif /* KEEP_XKB_LOCK_STATUS */
1636 wwin->flags.semi_focused = 0;
1638 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1640 wwin->flags.focused = 1;
1642 wWindowResetMouseGrabs(wwin);
1644 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1646 if (owin == wwin || !owin)
1647 return;
1649 nowner = wWindowFor(wwin->transient_for);
1651 /* new window is a transient for the old window */
1652 if (nowner == owin) {
1653 owin->flags.semi_focused = 1;
1654 wWindowUnfocus(nowner);
1655 return;
1658 oowner = wWindowFor(owin->transient_for);
1660 /* new window is owner of old window */
1661 if (wwin == oowner) {
1662 wWindowUnfocus(owin);
1663 return;
1666 if (!nowner) {
1667 wWindowUnfocus(owin);
1668 return;
1671 /* new window has same owner of old window */
1672 if (oowner == nowner) {
1673 /* prevent unfocusing of owner */
1674 oowner->flags.semi_focused = 0;
1675 wWindowUnfocus(owin);
1676 oowner->flags.semi_focused = 1;
1678 return;
1681 /* nowner != NULL && oowner != nowner */
1682 nowner->flags.semi_focused = 1;
1683 wWindowUnfocus(nowner);
1684 wWindowUnfocus(owin);
1688 void
1689 wWindowUnfocus(WWindow *wwin)
1691 CloseWindowMenu(wwin->screen_ptr);
1693 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1694 ? WS_PFOCUSED : WS_UNFOCUSED);
1696 if (wwin->transient_for!=None
1697 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1698 WWindow *owner;
1699 owner = wWindowFor(wwin->transient_for);
1700 if (owner && owner->flags.semi_focused) {
1701 owner->flags.semi_focused = 0;
1702 if (owner->flags.mapped || owner->flags.shaded) {
1703 wWindowUnfocus(owner);
1704 wFrameWindowPaint(owner->frame);
1708 wwin->flags.focused = 0;
1710 wWindowResetMouseGrabs(wwin);
1712 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1719 *----------------------------------------------------------------------
1721 * wWindowConstrainSize--
1722 * Constrains size for the client window, taking the maximal size,
1723 * window resize increments and other size hints into account.
1725 * Returns:
1726 * The closest size to what was given that the client window can
1727 * have.
1729 *----------------------------------------------------------------------
1731 void
1732 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1734 int width = *nwidth;
1735 int height = *nheight;
1736 int winc = 1;
1737 int hinc = 1;
1738 int minW = 1, minH = 1;
1739 int maxW = wwin->screen_ptr->scr_width*2;
1740 int maxH = wwin->screen_ptr->scr_height*2;
1741 int minAX = -1, minAY = -1;
1742 int maxAX = -1, maxAY = -1;
1743 int baseW = 0;
1744 int baseH = 0;
1746 if (wwin->normal_hints) {
1747 winc = wwin->normal_hints->width_inc;
1748 hinc = wwin->normal_hints->height_inc;
1749 minW = wwin->normal_hints->min_width;
1750 minH = wwin->normal_hints->min_height;
1751 maxW = wwin->normal_hints->max_width;
1752 maxH = wwin->normal_hints->max_height;
1753 if (wwin->normal_hints->flags & PAspect) {
1754 minAX = wwin->normal_hints->min_aspect.x;
1755 minAY = wwin->normal_hints->min_aspect.y;
1756 maxAX = wwin->normal_hints->max_aspect.x;
1757 maxAY = wwin->normal_hints->max_aspect.y;
1761 if (width < minW)
1762 width = minW;
1763 if (height < minH)
1764 height = minH;
1766 if (width > maxW)
1767 width = maxW;
1768 if (height > maxH)
1769 height = maxH;
1771 /* aspect ratio code borrowed from olwm */
1772 if (minAX > 0) {
1773 /* adjust max aspect ratio */
1774 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1775 if (maxAX > maxAY) {
1776 height = (width * maxAY) / maxAX;
1777 if (height > maxH) {
1778 height = maxH;
1779 width = (height * maxAX) / maxAY;
1781 } else {
1782 width = (height * maxAX) / maxAY;
1783 if (width > maxW) {
1784 width = maxW;
1785 height = (width * maxAY) / maxAX;
1790 /* adjust min aspect ratio */
1791 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1792 if (minAX > minAY) {
1793 height = (width * minAY) / minAX;
1794 if (height < minH) {
1795 height = minH;
1796 width = (height * minAX) / minAY;
1798 } else {
1799 width = (height * minAX) / minAY;
1800 if (width < minW) {
1801 width = minW;
1802 height = (width * minAY) / minAX;
1808 if (baseW != 0) {
1809 width = (((width - baseW) / winc) * winc) + baseW;
1810 } else {
1811 width = (((width - minW) / winc) * winc) + minW;
1814 if (baseW != 0) {
1815 height = (((height - baseH) / hinc) * hinc) + baseH;
1816 } else {
1817 height = (((height - minH) / hinc) * hinc) + minH;
1820 *nwidth = width;
1821 *nheight = height;
1825 void
1826 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1828 WScreen *scr = wwin->screen_ptr;
1829 WApplication *wapp;
1830 int unmap = 0;
1832 if (workspace >= scr->workspace_count || workspace < 0
1833 || workspace == wwin->frame->workspace)
1834 return;
1836 if (workspace != scr->current_workspace) {
1837 /* Sent to other workspace. Unmap window */
1838 if ((wwin->flags.mapped
1839 || wwin->flags.shaded
1840 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1841 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1843 wapp = wApplicationOf(wwin->main_window);
1844 if (wapp) {
1845 wapp->last_workspace = workspace;
1847 if (wwin->flags.miniaturized) {
1848 if (wwin->icon) {
1849 XUnmapWindow(dpy, wwin->icon->core->window);
1850 wwin->icon->mapped = 0;
1852 } else {
1853 unmap = 1;
1854 wSetFocusTo(scr, NULL);
1857 } else {
1858 /* brought to current workspace. Map window */
1859 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1860 if (wwin->icon) {
1861 XMapWindow(dpy, wwin->icon->core->window);
1862 wwin->icon->mapped = 1;
1864 } else if (!wwin->flags.mapped &&
1865 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1866 wWindowMap(wwin);
1869 if (!IS_OMNIPRESENT(wwin)) {
1870 wwin->frame->workspace = workspace;
1871 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1873 #ifdef GNOME_STUFF
1874 wGNOMEUpdateClientStateHint(wwin, True);
1875 #endif
1876 #ifdef KWM_HINTS
1877 wKWMUpdateClientWorkspace(wwin);
1878 wKWMSendEventMessage(wwin, WKWMChangedClient);
1879 #endif
1880 if (unmap) {
1881 wWindowUnmap(wwin);
1886 void
1887 wWindowSynthConfigureNotify(WWindow *wwin)
1889 XEvent sevent;
1891 sevent.type = ConfigureNotify;
1892 sevent.xconfigure.display = dpy;
1893 sevent.xconfigure.event = wwin->client_win;
1894 sevent.xconfigure.window = wwin->client_win;
1896 sevent.xconfigure.x = wwin->client.x;
1897 sevent.xconfigure.y = wwin->client.y;
1898 sevent.xconfigure.width = wwin->client.width;
1899 sevent.xconfigure.height = wwin->client.height;
1901 sevent.xconfigure.border_width = wwin->old_border_width;
1902 if (WFLAGP(wwin, no_titlebar))
1903 sevent.xconfigure.above = None;
1904 else
1905 sevent.xconfigure.above = wwin->frame->titlebar->window;
1907 sevent.xconfigure.override_redirect = False;
1908 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1909 #ifdef KWM_HINTS
1910 wKWMSendEventMessage(wwin, WKWMChangedClient);
1911 #endif
1912 XFlush(dpy);
1917 *----------------------------------------------------------------------
1918 * wWindowConfigure--
1919 * Configures the frame, decorations and client window to the
1920 * specified geometry. The geometry is not checked for validity,
1921 * wWindowConstrainSize() must be used for that.
1922 * The size parameters are for the client window, but the position is
1923 * for the frame.
1924 * The client window receives a ConfigureNotify event, according
1925 * to what ICCCM says.
1927 * Returns:
1928 * None
1930 * Side effects:
1931 * Window size and position are changed and client window receives
1932 * a ConfigureNotify event.
1933 *----------------------------------------------------------------------
1935 void
1936 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1937 WWindow *wwin;
1938 int req_x, req_y; /* new position of the frame */
1939 int req_width, req_height; /* new size of the client */
1941 int synth_notify = False;
1942 int resize;
1944 resize = (req_width!=wwin->client.width
1945 || req_height!=wwin->client.height);
1947 * if the window is being moved but not resized then
1948 * send a synthetic ConfigureNotify
1950 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1951 synth_notify = True;
1954 if (WFLAGP(wwin, dont_move_off))
1955 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1956 req_width, req_height);
1957 if (resize) {
1958 if (req_width < MIN_WINDOW_SIZE)
1959 req_width = MIN_WINDOW_SIZE;
1960 if (req_height < MIN_WINDOW_SIZE)
1961 req_height = MIN_WINDOW_SIZE;
1963 /* If growing, resize inner part before frame,
1964 * if shrinking, resize frame before.
1965 * This will prevent the frame (that can have a different color)
1966 * to be exposed, causing flicker */
1967 if (req_height > wwin->frame->core->height
1968 || req_width > wwin->frame->core->width)
1969 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1971 if (wwin->flags.shaded) {
1972 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1973 req_width, wwin->frame->core->height);
1974 wwin->old_geometry.height = req_height;
1975 } else {
1976 int h;
1978 h = req_height + wwin->frame->top_width
1979 + wwin->frame->bottom_width;
1981 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1984 if (!(req_height > wwin->frame->core->height
1985 || req_width > wwin->frame->core->width))
1986 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1988 wwin->client.x = req_x;
1989 wwin->client.y = req_y + wwin->frame->top_width;
1990 wwin->client.width = req_width;
1991 wwin->client.height = req_height;
1992 } else {
1993 wwin->client.x = req_x;
1994 wwin->client.y = req_y + wwin->frame->top_width;
1996 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1998 wwin->frame_x = req_x;
1999 wwin->frame_y = req_y;
2001 #ifdef SHAPE
2002 if (wShapeSupported && wwin->flags.shaped && resize) {
2003 wWindowSetShape(wwin);
2005 #endif
2007 if (synth_notify)
2008 wWindowSynthConfigureNotify(wwin);
2009 XFlush(dpy);
2013 void
2014 wWindowMove(wwin, req_x, req_y)
2015 WWindow *wwin;
2016 int req_x, req_y; /* new position of the frame */
2018 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2019 int synth_notify = False;
2021 /* Send a synthetic ConfigureNotify event for every window movement. */
2022 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2023 synth_notify = True;
2025 #else
2026 /* A single synthetic ConfigureNotify event is sent at the end of
2027 * a completed (opaque) movement in moveres.c */
2028 #endif
2030 if (WFLAGP(wwin, dont_move_off))
2031 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2032 wwin->frame->core->width, wwin->frame->core->height);
2034 wwin->client.x = req_x;
2035 wwin->client.y = req_y + wwin->frame->top_width;
2037 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2039 wwin->frame_x = req_x;
2040 wwin->frame_y = req_y;
2042 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2043 if (synth_notify)
2044 wWindowSynthConfigureNotify(wwin);
2045 #endif
2049 void
2050 wWindowUpdateButtonImages(WWindow *wwin)
2052 WScreen *scr = wwin->screen_ptr;
2053 Pixmap pixmap, mask;
2054 WFrameWindow *fwin = wwin->frame;
2056 if (WFLAGP(wwin, no_titlebar))
2057 return;
2059 /* miniaturize button */
2061 if (!WFLAGP(wwin, no_miniaturize_button)) {
2062 if (wwin->wm_gnustep_attr
2063 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2064 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2066 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2067 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2068 } else {
2069 mask = None;
2072 if (fwin->lbutton_image
2073 && (fwin->lbutton_image->image != pixmap
2074 || fwin->lbutton_image->mask != mask)) {
2075 wPixmapDestroy(fwin->lbutton_image);
2076 fwin->lbutton_image = NULL;
2079 if (!fwin->lbutton_image) {
2080 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2081 fwin->lbutton_image->client_owned = 1;
2082 fwin->lbutton_image->client_owned_mask = 1;
2084 } else {
2085 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2086 wPixmapDestroy(fwin->lbutton_image);
2088 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2092 #ifdef XKB_BUTTON_HINT
2093 if (!WFLAGP(wwin, no_language_button)) {
2094 if (fwin->languagebutton_image &&
2095 !fwin->languagebutton_image->shared) {
2096 wPixmapDestroy(fwin->languagebutton_image);
2098 fwin->languagebutton_image =
2099 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2101 #endif
2103 /* close button */
2105 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2106 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2108 if (!WFLAGP(wwin, no_close_button)) {
2109 if (wwin->wm_gnustep_attr
2110 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2111 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2113 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2114 mask = wwin->wm_gnustep_attr->close_mask;
2115 else
2116 mask = None;
2118 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2119 || fwin->rbutton_image->mask != mask)) {
2120 wPixmapDestroy(fwin->rbutton_image);
2121 fwin->rbutton_image = NULL;
2124 if (!fwin->rbutton_image) {
2125 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2126 fwin->rbutton_image->client_owned = 1;
2127 fwin->rbutton_image->client_owned_mask = 1;
2130 } else if (WFLAGP(wwin, kill_close)) {
2132 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2133 wPixmapDestroy(fwin->rbutton_image);
2135 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2137 } else if (MGFLAGP(wwin, broken_close)) {
2139 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2140 wPixmapDestroy(fwin->rbutton_image);
2142 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2144 } else {
2146 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2147 wPixmapDestroy(fwin->rbutton_image);
2149 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2153 /* force buttons to be redrawn */
2154 fwin->flags.need_texture_change = 1;
2155 wFrameWindowPaint(fwin);
2160 *---------------------------------------------------------------------------
2161 * wWindowConfigureBorders--
2162 * Update window border configuration according to attribute flags.
2164 *---------------------------------------------------------------------------
2166 void
2167 wWindowConfigureBorders(WWindow *wwin)
2169 if (wwin->frame) {
2170 int flags;
2171 int newy, oldh;
2173 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2174 #ifdef XKB_BUTTON_HINT
2175 flags |= WFF_LANGUAGE_BUTTON;
2176 #endif
2177 if (!WFLAGP(wwin, no_titlebar))
2178 flags |= WFF_TITLEBAR;
2179 if (!WFLAGP(wwin, no_resizebar))
2180 flags |= WFF_RESIZEBAR;
2181 if (!WFLAGP(wwin, no_border))
2182 flags |= WFF_BORDER;
2183 if (wwin->flags.shaded)
2184 flags |= WFF_IS_SHADED;
2186 oldh = wwin->frame->top_width;
2187 wFrameWindowUpdateBorders(wwin->frame, flags);
2188 if (oldh != wwin->frame->top_width) {
2189 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2191 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2192 wWindowConfigure(wwin, wwin->frame_x, newy,
2193 wwin->client.width, wwin->client.height);
2196 flags = 0;
2197 if (!WFLAGP(wwin, no_miniaturize_button)
2198 && wwin->frame->flags.hide_left_button)
2199 flags |= WFF_LEFT_BUTTON;
2201 #ifdef XKB_BUTTON_HINT
2202 if (!WFLAGP(wwin, no_language_button)
2203 && wwin->frame->flags.hide_language_button)
2204 flags |= WFF_LANGUAGE_BUTTON;
2205 #endif
2207 if (!WFLAGP(wwin, no_close_button)
2208 && wwin->frame->flags.hide_right_button)
2209 flags |= WFF_RIGHT_BUTTON;
2211 if (flags!=0) {
2212 wWindowUpdateButtonImages(wwin);
2213 wFrameWindowShowButton(wwin->frame, flags);
2216 flags = 0;
2217 if (WFLAGP(wwin, no_miniaturize_button)
2218 && !wwin->frame->flags.hide_left_button)
2219 flags |= WFF_LEFT_BUTTON;
2221 #ifdef XKB_BUTTON_HINT
2222 if (WFLAGP(wwin, no_language_button)
2223 && !wwin->frame->flags.hide_language_button)
2224 flags |= WFF_LANGUAGE_BUTTON;
2225 #endif
2227 if (WFLAGP(wwin, no_close_button)
2228 && !wwin->frame->flags.hide_right_button)
2229 flags |= WFF_RIGHT_BUTTON;
2231 if (flags!=0)
2232 wFrameWindowHideButton(wwin->frame, flags);
2234 #ifdef SHAPE
2235 if (wShapeSupported && wwin->flags.shaped) {
2236 wWindowSetShape(wwin);
2238 #endif
2243 void
2244 wWindowSaveState(WWindow *wwin)
2246 CARD32 data[10];
2247 int i;
2249 memset(data, 0, sizeof(CARD32)*10);
2250 data[0] = wwin->frame->workspace;
2251 data[1] = wwin->flags.miniaturized;
2252 data[2] = wwin->flags.shaded;
2253 data[3] = wwin->flags.hidden;
2255 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2256 if (wwin->screen_ptr->shortcutWindows[i] &&
2257 WMCountInBag(wwin->screen_ptr->shortcutWindows[i], wwin))
2258 data[9] |= 1<<i;
2260 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2261 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2262 (unsigned char *)data, 10);
2266 static int
2267 getSavedState(Window window, WSavedState **state)
2269 Atom type_ret;
2270 int fmt_ret;
2271 unsigned long nitems_ret;
2272 unsigned long bytes_after_ret;
2273 CARD32 *data;
2275 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2276 True, _XA_WINDOWMAKER_STATE,
2277 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2278 (unsigned char **)&data)!=Success || !data)
2279 return 0;
2281 *state = malloc(sizeof(WSavedState));
2283 if (*state) {
2284 (*state)->workspace = data[0];
2285 (*state)->miniaturized = data[1];
2286 (*state)->shaded = data[2];
2287 (*state)->hidden = data[3];
2288 (*state)->use_geometry = data[4];
2289 (*state)->x = data[5];
2290 (*state)->y = data[6];
2291 (*state)->w = data[7];
2292 (*state)->h = data[8];
2293 (*state)->window_shortcuts = data[9];
2295 XFree(data);
2297 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2298 return 1;
2299 else
2300 return 0;
2304 #ifdef SHAPE
2305 void
2306 wWindowClearShape(WWindow *wwin)
2308 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2309 0, wwin->frame->top_width, None, ShapeSet);
2310 XFlush(dpy);
2313 void
2314 wWindowSetShape(WWindow *wwin)
2316 XRectangle rect[2];
2317 int count;
2318 #ifdef OPTIMIZE_SHAPE
2319 XRectangle *rects;
2320 XRectangle *urec;
2321 int ordering;
2323 /* only shape is the client's */
2324 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2325 goto alt_code;
2328 /* Get array of rectangles describing the shape mask */
2329 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2330 &count, &ordering);
2331 if (!rects) {
2332 goto alt_code;
2335 urec = malloc(sizeof(XRectangle)*(count+2));
2336 if (!urec) {
2337 XFree(rects);
2338 goto alt_code;
2341 /* insert our decoration rectangles in the rect list */
2342 memcpy(urec, rects, sizeof(XRectangle)*count);
2343 XFree(rects);
2345 if (!WFLAGP(wwin, no_titlebar)) {
2346 urec[count].x = -1;
2347 urec[count].y = -1 - wwin->frame->top_width;
2348 urec[count].width = wwin->frame->core->width + 2;
2349 urec[count].height = wwin->frame->top_width + 1;
2350 count++;
2352 if (!WFLAGP(wwin, no_resizebar)) {
2353 urec[count].x = -1;
2354 urec[count].y = wwin->frame->core->height
2355 - wwin->frame->bottom_width - wwin->frame->top_width;
2356 urec[count].width = wwin->frame->core->width + 2;
2357 urec[count].height = wwin->frame->bottom_width + 1;
2358 count++;
2361 /* shape our frame window */
2362 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2363 0, wwin->frame->top_width, urec, count,
2364 ShapeSet, Unsorted);
2365 XFlush(dpy);
2366 free(urec);
2367 return;
2369 alt_code:
2370 #endif /* OPTIMIZE_SHAPE */
2371 count = 0;
2372 if (!WFLAGP(wwin, no_titlebar)) {
2373 rect[count].x = -1;
2374 rect[count].y = -1;
2375 rect[count].width = wwin->frame->core->width + 2;
2376 rect[count].height = wwin->frame->top_width + 1;
2377 count++;
2379 if (!WFLAGP(wwin, no_resizebar)) {
2380 rect[count].x = -1;
2381 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2382 rect[count].width = wwin->frame->core->width + 2;
2383 rect[count].height = wwin->frame->bottom_width + 1;
2384 count++;
2386 if (count > 0) {
2387 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2388 0, 0, rect, count, ShapeSet, Unsorted);
2390 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2391 0, wwin->frame->top_width, wwin->client_win,
2392 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2393 XFlush(dpy);
2395 #endif /* SHAPE */
2397 /* ====================================================================== */
2399 static FocusMode
2400 getFocusMode(WWindow *wwin)
2402 FocusMode mode;
2404 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2405 if (wwin->wm_hints->input == True) {
2406 if (wwin->protocols.TAKE_FOCUS)
2407 mode = WFM_LOCALLY_ACTIVE;
2408 else
2409 mode = WFM_PASSIVE;
2410 } else {
2411 if (wwin->protocols.TAKE_FOCUS)
2412 mode = WFM_GLOBALLY_ACTIVE;
2413 else
2414 mode = WFM_NO_INPUT;
2416 } else {
2417 mode = WFM_PASSIVE;
2419 return mode;
2423 void
2424 wWindowSetKeyGrabs(WWindow *wwin)
2426 int i;
2427 WShortKey *key;
2429 for (i=0; i<WKBD_LAST; i++) {
2430 key = &wKeyBindings[i];
2432 if (key->keycode==0)
2433 continue;
2434 if (key->modifier!=AnyModifier) {
2435 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2436 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2437 #ifdef NUMLOCK_HACK
2438 /* Also grab all modifier combinations possible that include,
2439 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2440 * work even if the NumLock/ScrollLock key is on.
2442 wHackedGrabKey(key->keycode, key->modifier,
2443 wwin->frame->core->window, True, GrabModeAsync,
2444 GrabModeAsync);
2445 #endif
2447 XGrabKey(dpy, key->keycode, key->modifier,
2448 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2451 #ifndef LITE
2452 wRootMenuBindShortcuts(wwin->frame->core->window);
2453 #endif
2458 void
2459 wWindowResetMouseGrabs(WWindow *wwin)
2461 /* Mouse grabs can't be done on the client window because of
2462 * ICCCM and because clients that try to do the same will crash.
2464 * But there is a problem wich makes tbar buttons of unfocused
2465 * windows not usable as the click goes to the frame window instead
2466 * of the button itself. Must figure a way to fix that.
2469 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2471 if (!WFLAGP(wwin, no_bind_mouse)) {
2472 /* grabs for Meta+drag */
2473 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2474 True, ButtonPressMask, GrabModeSync,
2475 GrabModeAsync, None, None);
2478 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2479 /* the passive grabs to focus the window */
2480 if (wPreferences.focus_mode == WKF_CLICK)
2481 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2482 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2483 None, None);
2485 XFlush(dpy);
2489 void
2490 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2492 if (attr->flags & GSExtraFlagsAttr) {
2493 if (MGFLAGP(wwin, broken_close) !=
2494 (attr->extra_flags & GSDocumentEditedFlag)) {
2495 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2496 wWindowUpdateButtonImages(wwin);
2502 WMagicNumber
2503 wWindowAddSavedState(char *instance, char *class, char *command,
2504 pid_t pid, WSavedState *state)
2506 WWindowState *wstate;
2508 wstate = malloc(sizeof(WWindowState));
2509 if (!wstate)
2510 return 0;
2512 memset(wstate, 0, sizeof(WWindowState));
2513 wstate->pid = pid;
2514 if (instance)
2515 wstate->instance = wstrdup(instance);
2516 if (class)
2517 wstate->class = wstrdup(class);
2518 if (command)
2519 wstate->command = wstrdup(command);
2520 wstate->state = state;
2522 wstate->next = windowState;
2523 windowState = wstate;
2525 #ifdef DEBUG
2526 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2527 class, command);
2528 #endif
2530 return wstate;
2534 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2537 WMagicNumber
2538 wWindowGetSavedState(Window win)
2540 char *instance, *class, *command=NULL;
2541 WWindowState *wstate = windowState;
2542 char **argv;
2543 int argc;
2545 if (!wstate)
2546 return NULL;
2548 if (XGetCommand(dpy, win, &argv, &argc)) {
2549 if (argc > 0)
2550 command = FlattenStringList(argv, argc);
2551 XFreeStringList(argv);
2553 if (!command)
2554 return NULL;
2556 if (PropGetWMClass(win, &class, &instance)) {
2557 while (wstate) {
2558 if (SAME(instance, wstate->instance) &&
2559 SAME(class, wstate->class) &&
2560 SAME(command, wstate->command)) {
2561 break;
2563 wstate = wstate->next;
2565 } else {
2566 wstate = NULL;
2569 #ifdef DEBUG
2570 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2571 class, command);
2572 #endif
2574 if (command) free(command);
2575 if (instance) XFree(instance);
2576 if (class) XFree(class);
2578 return wstate;
2582 void
2583 wWindowDeleteSavedState(WMagicNumber id)
2585 WWindowState *tmp, *wstate=(WWindowState*)id;
2587 if (!wstate || !windowState)
2588 return;
2590 tmp = windowState;
2591 if (tmp==wstate) {
2592 windowState = wstate->next;
2593 #ifdef DEBUG
2594 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2595 wstate, wstate->instance, wstate->class, wstate->command);
2596 #endif
2597 if (wstate->instance) free(wstate->instance);
2598 if (wstate->class) free(wstate->class);
2599 if (wstate->command) free(wstate->command);
2600 free(wstate->state);
2601 free(wstate);
2602 } else {
2603 while (tmp->next) {
2604 if (tmp->next==wstate) {
2605 tmp->next=wstate->next;
2606 #ifdef DEBUG
2607 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2608 wstate, wstate->instance, wstate->class, wstate->command);
2609 #endif
2610 if (wstate->instance) free(wstate->instance);
2611 if (wstate->class) free(wstate->class);
2612 if (wstate->command) free(wstate->command);
2613 free(wstate->state);
2614 free(wstate);
2615 break;
2617 tmp = tmp->next;
2623 void
2624 wWindowDeleteSavedStatesForPID(pid_t pid)
2626 WWindowState *tmp, *wstate;
2628 if (!windowState)
2629 return;
2631 tmp = windowState;
2632 if (tmp->pid == pid) {
2633 wstate = windowState;
2634 windowState = tmp->next;
2635 #ifdef DEBUG
2636 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2637 wstate, wstate->instance, wstate->class, wstate->command);
2638 #endif
2639 if (wstate->instance) free(wstate->instance);
2640 if (wstate->class) free(wstate->class);
2641 if (wstate->command) free(wstate->command);
2642 free(wstate->state);
2643 free(wstate);
2644 } else {
2645 while (tmp->next) {
2646 if (tmp->next->pid==pid) {
2647 wstate = tmp->next;
2648 tmp->next = wstate->next;
2649 #ifdef DEBUG
2650 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2651 wstate, wstate->instance, wstate->class, wstate->command);
2652 #endif
2653 if (wstate->instance) free(wstate->instance);
2654 if (wstate->class) free(wstate->class);
2655 if (wstate->command) free(wstate->command);
2656 free(wstate->state);
2657 free(wstate);
2658 break;
2660 tmp = tmp->next;
2666 /* ====================================================================== */
2668 static void
2669 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2671 WWindow *wwin = data;
2673 #ifndef NUMLOCK_HACK
2674 if ((event->xbutton.state & ValidModMask)
2675 != (event->xbutton.state & ~LockMask)) {
2676 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2677 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2679 #endif
2681 event->xbutton.state &= ValidModMask;
2683 CloseWindowMenu(wwin->screen_ptr);
2685 if (wPreferences.focus_mode==WKF_CLICK
2686 && !(event->xbutton.state&ControlMask)
2687 && !WFLAGP(wwin, no_focusable)) {
2688 wSetFocusTo(wwin->screen_ptr, wwin);
2691 if (event->xbutton.button == Button1)
2692 wRaiseFrame(wwin->frame->core);
2694 if (event->xbutton.window != wwin->frame->resizebar->window) {
2695 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2696 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2697 GrabModeAsync, GrabModeAsync, None,
2698 None, CurrentTime)!=GrabSuccess) {
2699 #ifdef DEBUG0
2700 wwarning("pointer grab failed for window move");
2701 #endif
2702 return;
2706 if (event->xbutton.state & MOD_MASK) {
2707 /* move the window */
2708 wMouseMoveWindow(wwin, event);
2709 XUngrabPointer(dpy, CurrentTime);
2710 } else {
2711 wMouseResizeWindow(wwin, event);
2712 XUngrabPointer(dpy, CurrentTime);
2718 static void
2719 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2721 WWindow *wwin = data;
2723 event->xbutton.state &= ValidModMask;
2725 if (event->xbutton.button==Button1) {
2726 if (event->xbutton.state == 0) {
2727 if (!WFLAGP(wwin, no_shadeable)) {
2728 /* shade window */
2729 if (wwin->flags.shaded)
2730 wUnshadeWindow(wwin);
2731 else
2732 wShadeWindow(wwin);
2734 } else {
2735 int dir = 0;
2737 if (event->xbutton.state & ControlMask)
2738 dir |= MAX_VERTICAL;
2740 if (event->xbutton.state & ShiftMask) {
2741 dir |= MAX_HORIZONTAL;
2742 if (!(event->xbutton.state & ControlMask))
2743 wSelectWindow(wwin, !wwin->flags.selected);
2746 /* maximize window */
2747 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2748 if (wwin->flags.maximized)
2749 wUnmaximizeWindow(wwin);
2750 else
2751 wMaximizeWindow(wwin, dir);
2754 } else if (event->xbutton.button==Button3) {
2755 if (event->xbutton.state & MOD_MASK) {
2756 wHideOtherApplications(wwin);
2758 } else if (event->xbutton.button==Button2) {
2759 wSelectWindow(wwin, !wwin->flags.selected);
2764 static void
2765 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2767 WWindow *wwin = desc->parent;
2769 event->xbutton.state &= ValidModMask;
2771 CloseWindowMenu(wwin->screen_ptr);
2773 if (wPreferences.focus_mode==WKF_CLICK
2774 && !(event->xbutton.state&ControlMask)
2775 && !WFLAGP(wwin, no_focusable)) {
2776 wSetFocusTo(wwin->screen_ptr, wwin);
2778 if (event->xbutton.button == Button1) {
2779 wRaiseFrame(wwin->frame->core);
2782 if (event->xbutton.state & MOD_MASK) {
2783 /* move the window */
2784 if (XGrabPointer(dpy, wwin->client_win, False,
2785 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2786 GrabModeAsync, GrabModeAsync, None,
2787 None, CurrentTime)!=GrabSuccess) {
2788 #ifdef DEBUG0
2789 wwarning("pointer grab failed for window move");
2790 #endif
2791 return;
2793 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2794 wMouseResizeWindow(wwin, event);
2795 else
2796 wMouseMoveWindow(wwin, event);
2797 XUngrabPointer(dpy, CurrentTime);
2802 static void
2803 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2805 WWindow *wwin = (WWindow*)data;
2807 #ifndef NUMLOCK_HACK
2808 if ((event->xbutton.state & ValidModMask)
2809 != (event->xbutton.state & ~LockMask)) {
2810 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2811 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2813 #endif
2814 event->xbutton.state &= ValidModMask;
2816 CloseWindowMenu(wwin->screen_ptr);
2818 if (wPreferences.focus_mode==WKF_CLICK
2819 && !(event->xbutton.state&ControlMask)
2820 && !WFLAGP(wwin, no_focusable)) {
2821 wSetFocusTo(wwin->screen_ptr, wwin);
2824 if (event->xbutton.button == Button1
2825 || event->xbutton.button == Button2) {
2827 if (event->xbutton.button == Button1) {
2828 if (event->xbutton.state & MOD_MASK) {
2829 wLowerFrame(wwin->frame->core);
2830 } else {
2831 wRaiseFrame(wwin->frame->core);
2834 if ((event->xbutton.state & ShiftMask)
2835 && !(event->xbutton.state & ControlMask)) {
2836 wSelectWindow(wwin, !wwin->flags.selected);
2837 return;
2839 if (event->xbutton.window != wwin->frame->titlebar->window
2840 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2841 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2842 GrabModeAsync, GrabModeAsync, None,
2843 None, CurrentTime)!=GrabSuccess) {
2844 #ifdef DEBUG0
2845 wwarning("pointer grab failed for window move");
2846 #endif
2847 return;
2850 /* move the window */
2851 wMouseMoveWindow(wwin, event);
2853 XUngrabPointer(dpy, CurrentTime);
2854 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2855 && !wwin->flags.internal_window
2856 && !WCHECK_STATE(WSTATE_MODAL)) {
2857 WObjDescriptor *desc;
2859 if (event->xbutton.window != wwin->frame->titlebar->window
2860 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2861 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2862 GrabModeAsync, GrabModeAsync, None,
2863 None, CurrentTime)!=GrabSuccess) {
2864 #ifdef DEBUG0
2865 wwarning("pointer grab failed for window move");
2866 #endif
2867 return;
2870 OpenWindowMenu(wwin, event->xbutton.x_root,
2871 wwin->frame_y+wwin->frame->top_width, False);
2873 /* allow drag select */
2874 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2875 event->xany.send_event = True;
2876 (*desc->handle_mousedown)(desc, event);
2878 XUngrabPointer(dpy, CurrentTime);
2884 static void
2885 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2887 WWindow *wwin = data;
2889 event->xbutton.state &= ValidModMask;
2891 CloseWindowMenu(wwin->screen_ptr);
2893 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2894 return;
2896 /* if control-click, kill the client */
2897 if (event->xbutton.state & ControlMask) {
2898 wClientKill(wwin);
2899 } else {
2900 #ifdef OLWM_HINTS
2901 if (wwin->flags.olwm_push_pin_out) {
2903 wwin->flags.olwm_push_pin_out = 0;
2905 wOLWMChangePushpinState(wwin, True);
2907 wFrameWindowUpdatePushButton(wwin->frame, False);
2909 return;
2911 #endif
2912 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2913 /* send delete message */
2914 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2920 static void
2921 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2923 WWindow *wwin = data;
2925 CloseWindowMenu(wwin->screen_ptr);
2927 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2928 return;
2930 /* send delete message */
2931 if (wwin->protocols.DELETE_WINDOW) {
2932 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2933 } else {
2934 wClientKill(wwin);
2939 #ifdef XKB_BUTTON_HINT
2940 static void
2941 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2943 WWindow *wwin = data;
2944 WFrameWindow *fwin = wwin->frame;
2945 WScreen *scr = fwin->screen_ptr;
2946 XkbStateRec staterec;
2947 int tl;
2949 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2950 return;
2951 tl = wwin->frame->languagemode;
2952 wwin->frame->languagemode = wwin->frame->last_languagemode;
2953 wwin->frame->last_languagemode = tl;
2954 wSetFocusTo(scr, wwin);
2955 wwin->frame->languagebutton_image =
2956 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
2957 wwin->frame->languagemode];
2958 wFrameWindowUpdateLanguageButton(wwin->frame);
2959 if (event->xbutton.button == Button3)
2960 return;
2961 wRaiseFrame(fwin->core);
2963 #endif
2966 static void
2967 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2969 WWindow *wwin = data;
2971 event->xbutton.state &= ValidModMask;
2973 CloseWindowMenu(wwin->screen_ptr);
2975 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2976 return;
2978 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2979 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2980 LastTimestamp);
2981 } else {
2982 WApplication *wapp;
2983 if ((event->xbutton.state & ControlMask) ||
2984 (event->xbutton.button == Button3)) {
2986 wapp = wApplicationOf(wwin->main_window);
2987 if (wapp && !WFLAGP(wwin, no_appicon))
2988 wHideApplication(wapp);
2989 } else if (event->xbutton.state==0) {
2990 wIconifyWindow(wwin);