ResizebarBack option
[wmaker-crm.git] / src / window.c
bloba6410da7bc7355e1dc71ba1d72b1c73c548b1d34
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"
55 #ifdef MWM_HINTS
56 # include "motif.h"
57 #endif
58 #ifdef KWM_HINTS
59 # include "kwm.h"
60 #endif
61 #ifdef GNOME_STUFF
62 # include "gnome.h"
63 #endif
64 #ifdef OLWM_HINTS
65 # include "openlook.h"
66 #endif
68 /****** Global Variables ******/
70 extern WShortKey wKeyBindings[WKBD_LAST];
72 #ifdef SHAPE
73 extern Bool wShapeSupported;
74 #endif
76 /* contexts */
77 extern XContext wWinContext;
79 /* cursors */
80 extern Cursor wCursor[WCUR_LAST];
82 /* protocol atoms */
83 extern Atom _XA_WM_DELETE_WINDOW;
84 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
86 extern Atom _XA_WINDOWMAKER_STATE;
88 extern WPreferences wPreferences;
90 #define MOD_MASK wPreferences.modifier_mask
92 extern Time LastTimestamp;
94 /* superfluous... */
95 extern void DoWindowBirth(WWindow*);
98 /***** Local Stuff *****/
101 static WWindowState *windowState=NULL;
105 /* local functions */
106 static FocusMode getFocusMode(WWindow *wwin);
108 static int getSavedState(Window window, WSavedState **state);
110 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
112 /* event handlers */
115 /* frame window (during window grabs) */
116 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
118 /* close button */
119 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
120 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
122 /* iconify button */
123 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
126 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
127 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
129 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
132 /****** Notification Observers ******/
134 static void
135 appearanceObserver(void *self, WMNotification *notif)
137 WWindow *wwin = (WWindow*)self;
138 int flags = (int)WMGetNotificationClientData(notif);
140 if (!wwin->frame || !wwin->frame->titlebar)
141 return;
143 if (flags & WFontSettings) {
144 wWindowConfigureBorders(wwin);
146 if (flags & WTextureSettings) {
147 wwin->frame->flags.need_texture_remake = 1;
149 if (flags & (WTextureSettings | WColorSettings)) {
150 wFrameWindowPaint(wwin->frame);
154 /************************************/
156 WWindow*
157 wWindowFor(Window window)
159 WObjDescriptor *desc;
161 if (window==None)
162 return NULL;
164 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
165 return NULL;
167 if (desc->parent_type==WCLASS_WINDOW)
168 return desc->parent;
169 else if (desc->parent_type==WCLASS_FRAME) {
170 WFrameWindow *frame = (WFrameWindow*)desc->parent;
171 if (frame->flags.is_client_window_frame)
172 return frame->child;
175 return NULL;
179 WWindow*
180 wWindowCreate()
182 WWindow *wwin;
184 wwin = wmalloc(sizeof(WWindow));
185 wretain(wwin);
187 memset(wwin, 0, sizeof(WWindow));
189 wwin->client_descriptor.handle_mousedown = frameMouseDown;
190 wwin->client_descriptor.parent = wwin;
191 wwin->client_descriptor.self = wwin;
192 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
194 return wwin;
198 void
199 wWindowDestroy(WWindow *wwin)
201 int i;
203 WMRemoveNotificationObserver(wwin);
205 wwin->flags.destroyed = 1;
207 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
208 if (wwin->screen_ptr->shortcutWindow[i] == wwin) {
209 wwin->screen_ptr->shortcutWindow[i] = NULL;
213 if (wwin->normal_hints)
214 free(wwin->normal_hints);
216 if (wwin->wm_hints)
217 XFree(wwin->wm_hints);
219 if (wwin->wm_instance)
220 XFree(wwin->wm_instance);
222 if (wwin->wm_class)
223 XFree(wwin->wm_class);
225 if (wwin->wm_gnustep_attr)
226 free(wwin->wm_gnustep_attr);
228 if (wwin->cmap_windows)
229 XFree(wwin->cmap_windows);
231 XDeleteContext(dpy, wwin->client_win, wWinContext);
233 if (wwin->frame)
234 wFrameWindowDestroy(wwin->frame);
236 if (wwin->icon) {
237 RemoveFromStackList(wwin->icon->core);
238 wIconDestroy(wwin->icon);
239 if (wPreferences.auto_arrange_icons)
240 wArrangeIcons(wwin->screen_ptr, True);
242 wrelease(wwin);
248 static void
249 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
251 if (gs_hints->flags & GSWindowStyleAttr) {
252 wwin->client_flags.no_titlebar =
253 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
255 wwin->client_flags.no_close_button =
256 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
258 wwin->client_flags.no_closable =
259 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
261 wwin->client_flags.no_miniaturize_button =
262 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
264 wwin->client_flags.no_miniaturizable =
265 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
267 wwin->client_flags.no_resizebar =
268 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
270 wwin->client_flags.no_resizable =
271 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
272 } else {
273 /* setup the defaults */
274 wwin->client_flags.no_titlebar = 0;
275 wwin->client_flags.no_closable = 0;
276 wwin->client_flags.no_miniaturizable = 0;
277 wwin->client_flags.no_resizable = 0;
278 wwin->client_flags.no_close_button = 0;
279 wwin->client_flags.no_miniaturize_button = 0;
280 wwin->client_flags.no_resizebar = 0;
282 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
283 wwin->client_flags.no_appicon = 1;
288 void
289 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
290 WWindowAttributes *mask)
292 if (wflags->no_appicon && mask->no_appicon)
293 wflags->emulate_appicon = 0;
295 if (wwin->main_window!=None) {
296 WApplication *wapp = wApplicationOf(wwin->main_window);
297 if (wapp && !wapp->flags.emulated)
298 wflags->emulate_appicon = 0;
301 if (wwin->transient_for!=None
302 && wwin->transient_for!=wwin->screen_ptr->root_win)
303 wflags->emulate_appicon = 0;
305 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
306 wflags->sunken = 0;
311 void
312 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
314 WScreen *scr = wwin->screen_ptr;
316 /* sets global default stuff */
317 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
318 &wwin->client_flags, NULL, True);
320 * Decoration setting is done in this precedence (lower to higher)
321 * - use global default in the resource database
322 * - guess some settings
323 * - use GNUstep/external window attributes
324 * - set hints specified for the app in the resource DB
327 WSETUFLAG(wwin, broken_close, 0);
329 if (wwin->protocols.DELETE_WINDOW)
330 WSETUFLAG(wwin, kill_close, 0);
331 else
332 WSETUFLAG(wwin, kill_close, 1);
334 /* transients can't be iconified or maximized */
335 if (wwin->transient_for) {
336 WSETUFLAG(wwin, no_miniaturizable, 1);
337 WSETUFLAG(wwin, no_miniaturize_button, 1);
340 /* if the window can't be resized, remove the resizebar */
341 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
342 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
343 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
344 WSETUFLAG(wwin, no_resizable, 1);
345 WSETUFLAG(wwin, no_resizebar, 1);
348 /* set GNUstep window attributes */
349 if (wwin->wm_gnustep_attr) {
350 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
352 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
354 switch (wwin->wm_gnustep_attr->window_level) {
355 case WMNormalWindowLevel:
356 *level = WMNormalLevel;
357 break;
358 case WMFloatingWindowLevel:
359 *level = WMFloatingLevel;
360 break;
361 case WMDockWindowLevel:
362 *level = WMDockLevel;
363 break;
364 case WMSubmenuWindowLevel:
365 *level = WMSubmenuLevel;
366 break;
367 case WMMainMenuWindowLevel:
368 *level = WMMainMenuLevel;
369 break;
370 default:
371 *level = WMNormalLevel;
372 break;
374 } else {
375 /* setup defaults */
376 *level = WMNormalLevel;
378 } else {
379 int tmp_workspace = -1;
380 int tmp_level = -1;
382 #ifdef MWM_HINTS
383 wMWMCheckClientHints(wwin);
384 #endif /* MWM_HINTS */
386 #ifdef KWM_HINTS
387 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
388 #endif /* KWM_HINTS */
390 #ifdef GNOME_STUFF
391 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
392 #endif /* GNOME_STUFF */
394 #ifdef OLWM_HINTS
395 wOLWMCheckClientHints(wwin);
396 #endif /* OLWM_HINTS */
398 if (tmp_level < 0) {
399 if (WFLAGP(wwin, floating))
400 *level = WMFloatingLevel;
401 else if (WFLAGP(wwin, sunken))
402 *level = WMSunkenLevel;
403 else
404 *level = WMNormalLevel;
405 } else {
406 *level = tmp_level;
409 if (tmp_workspace >= 0) {
410 *workspace = tmp_workspace % scr->workspace_count;
415 * Set attributes specified only for that window/class.
416 * This might do duplicate work with the 1st wDefaultFillAttributes().
418 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
419 &wwin->user_flags, &wwin->defined_user_flags,
420 False);
422 * Sanity checks for attributes that depend on other attributes
424 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
425 wwin->user_flags.emulate_appicon = 0;
427 if (wwin->main_window!=None) {
428 WApplication *wapp = wApplicationOf(wwin->main_window);
429 if (wapp && !wapp->flags.emulated)
430 wwin->user_flags.emulate_appicon = 0;
433 if (wwin->transient_for!=None
434 && wwin->transient_for!=wwin->screen_ptr->root_win)
435 wwin->user_flags.emulate_appicon = 0;
437 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
438 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
439 wwin->user_flags.sunken = 0;
441 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
447 Bool
448 wWindowCanReceiveFocus(WWindow *wwin)
450 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
451 return False;
452 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
453 return False;
454 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
455 return False;
457 return True;
461 Bool
462 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
464 int w1, h1, w2, h2;
466 w1 = wwin->frame->core->width;
467 h1 = wwin->frame->core->height;
468 w2 = obscured->frame->core->width;
469 h2 = obscured->frame->core->height;
471 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
472 && wwin->frame->workspace != obscured->frame->workspace)
473 return False;
475 if (wwin->frame_x + w1 < obscured->frame_x
476 || wwin->frame_y + h1 < obscured->frame_y
477 || wwin->frame_x > obscured->frame_x + w2
478 || wwin->frame_y > obscured->frame_y + h2) {
479 return False;
482 return True;
487 *----------------------------------------------------------------
488 * wManageWindow--
489 * reparents the window and allocates a descriptor for it.
490 * Window manager hints and other hints are fetched to configure
491 * the window decoration attributes and others. User preferences
492 * for the window are used if available, to configure window
493 * decorations and some behaviour.
494 * If in startup, windows that are override redirect,
495 * unmapped and never were managed and are Withdrawn are not
496 * managed.
498 * Returns:
499 * the new window descriptor
501 * Side effects:
502 * The window is reparented and appropriate notification
503 * is done to the client. Input mask for the window is setup.
504 * The window descriptor is also associated with various window
505 * contexts and inserted in the head of the window list.
506 * Event handler contexts are associated for some objects
507 * (buttons, titlebar and resizebar)
509 *----------------------------------------------------------------
511 WWindow*
512 wManageWindow(WScreen *scr, Window window)
514 WWindow *wwin;
515 int x, y;
516 unsigned width, height;
517 XWindowAttributes wattribs;
518 XSetWindowAttributes attribs;
519 WWindowState *win_state;
520 WWindow *transientOwner = NULL;
521 int window_level;
522 int wm_state;
523 int foo;
524 int workspace = -1;
525 char *title;
526 Bool withdraw = False;
528 /* mutex. */
529 /* XGrabServer(dpy); */
530 XSync(dpy, False);
531 /* make sure the window is still there */
532 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
533 XUngrabServer(dpy);
534 return NULL;
537 /* if it's an override-redirect, ignore it */
538 if (wattribs.override_redirect) {
539 XUngrabServer(dpy);
540 return NULL;
543 wm_state = PropGetWindowState(window);
545 /* if it's startup and the window is unmapped, don't manage it */
546 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
547 XUngrabServer(dpy);
548 return NULL;
551 if (!wFetchName(dpy, window, &title)) {
552 title = NULL;
555 #ifdef KWM_HINTS
556 if (title && !wKWMManageableClient(scr, window, title)) {
557 XFree(title);
558 XUngrabServer(dpy);
559 return NULL;
561 #endif /* KWM_HINTS */
564 wwin = wWindowCreate();
566 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
568 #ifdef DEBUG
569 printf("managing window %x\n", (unsigned)window);
570 #endif
572 #ifdef SHAPE
573 if (wShapeSupported) {
574 int junk;
575 unsigned int ujunk;
576 int b_shaped;
578 XShapeSelectInput(dpy, window, ShapeNotifyMask);
579 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
580 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
581 wwin->flags.shaped = b_shaped;
583 #endif
586 *--------------------------------------------------
588 * Get hints and other information in properties
590 *--------------------------------------------------
592 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
594 /* setup descriptor */
595 wwin->client_win = window;
596 wwin->screen_ptr = scr;
598 wwin->old_border_width = wattribs.border_width;
600 wwin->event_mask = CLIENT_EVENTS;
601 attribs.event_mask = CLIENT_EVENTS;
602 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
603 attribs.save_under = False;
604 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
605 |CWSaveUnder, &attribs);
606 XSetWindowBorderWidth(dpy, window, 0);
608 /* get hints from GNUstep app */
609 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
610 wwin->wm_gnustep_attr = NULL;
613 wwin->client_leader = PropGetClientLeader(window);
614 if (wwin->client_leader!=None)
615 wwin->main_window = wwin->client_leader;
617 wwin->wm_hints = XGetWMHints(dpy, window);
619 if (wwin->wm_hints) {
620 if (wwin->wm_hints->flags & StateHint) {
622 if (wwin->wm_hints->initial_state == IconicState) {
624 wwin->flags.miniaturized = 1;
626 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
628 withdraw = True;
632 if (wwin->wm_hints->flags & WindowGroupHint) {
633 wwin->group_id = wwin->wm_hints->window_group;
634 /* window_group has priority over CLIENT_LEADER */
635 wwin->main_window = wwin->group_id;
636 } else {
637 wwin->group_id = None;
640 if (wwin->wm_hints->flags & UrgencyHint)
641 wwin->flags.urgent = 1;
642 } else {
643 wwin->group_id = None;
646 PropGetProtocols(window, &wwin->protocols);
648 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
649 wwin->transient_for = None;
650 } else {
651 if (wwin->transient_for==None || wwin->transient_for==window) {
652 wwin->transient_for = scr->root_win;
653 } else {
654 transientOwner = wWindowFor(wwin->transient_for);
655 if (transientOwner && transientOwner->main_window!=None) {
656 wwin->main_window = transientOwner->main_window;
657 } /*else {
658 wwin->main_window = None;
663 /* guess the focus mode */
664 wwin->focus_mode = getFocusMode(wwin);
666 /* get geometry stuff */
667 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
669 /* get colormap windows */
670 GetColormapWindows(wwin);
673 *--------------------------------------------------
675 * Setup the decoration/window attributes and
676 * geometry
678 *--------------------------------------------------
681 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
683 #ifdef OLWM_HINTS
684 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
685 && wwin->group_id != None && wwin->group_id != window) {
687 transientOwner = wWindowFor(wwin->group_id);
689 if (transientOwner) {
690 wwin->transient_for = wwin->group_id;
692 /* transients can't be iconified or maximized */
693 if (wwin->transient_for) {
694 WSETUFLAG(wwin, no_miniaturizable, 1);
695 WSETUFLAG(wwin, no_miniaturize_button, 1);
699 #endif /* OLWM_HINTS */
702 * Make broken apps behave as a nice app.
704 if (WFLAGP(wwin, emulate_appicon)) {
705 wwin->main_window = wwin->client_win;
709 *------------------------------------------------------------
711 * Setup the initial state of the window
713 *------------------------------------------------------------
716 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
717 wwin->flags.miniaturized = 1;
720 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
721 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
724 #ifdef GNOME_STUFF
725 wGNOMECheckInitialClientState(wwin);
726 #endif
727 #ifdef KWM_HINTS
728 wKWMCheckClientInitialState(wwin);
729 #endif
731 /* apply previous state if it exists and we're in startup */
732 if (scr->flags.startup && wm_state >= 0) {
734 if (wm_state == IconicState) {
736 wwin->flags.miniaturized = 1;
738 } else if (wm_state == WithdrawnState) {
740 withdraw = True;
744 /* if there is a saved state (from file), restore it */
745 win_state = NULL;
746 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
747 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
748 } else {
749 win_state = (WWindowState*)wWindowGetSavedState(window);
751 if (win_state && !withdraw) {
753 if (win_state->state->hidden>0)
754 wwin->flags.hidden = win_state->state->hidden;
756 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
757 wwin->flags.shaded = win_state->state->shaded;
759 if (win_state->state->miniaturized>0 &&
760 !WFLAGP(wwin, no_miniaturizable)) {
761 wwin->flags.miniaturized = win_state->state->miniaturized;
763 if (!IS_OMNIPRESENT(wwin)) {
764 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
765 wwin->wm_class);
766 if (w < 0 || w >= scr->workspace_count) {
767 workspace = win_state->state->workspace;
768 if (workspace >= scr->workspace_count)
769 workspace = scr->current_workspace;
770 } else {
771 workspace = w;
773 } else {
774 workspace = scr->current_workspace;
778 /* if we're restarting, restore saved state (from hints).
779 * This will overwrite previous */
781 WSavedState *wstate;
783 if (getSavedState(window, &wstate)) {
784 wwin->flags.shaded = wstate->shaded;
785 wwin->flags.hidden = wstate->hidden;
786 wwin->flags.miniaturized = wstate->miniaturized;
787 workspace = wstate->workspace;
789 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
790 int i;
792 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
793 if (wstate->window_shortcuts & (1<<i))
794 scr->shortcutWindow[i] = wwin;
797 free(wstate);
801 /* don't let transients start miniaturized if their owners are not */
802 if (transientOwner && !transientOwner->flags.miniaturized
803 && wwin->flags.miniaturized && !withdraw) {
804 wwin->flags.miniaturized = 0;
805 if (wwin->wm_hints)
806 wwin->wm_hints->initial_state = NormalState;
809 /* set workspace on which the window starts */
810 if (workspace >= 0) {
811 if (workspace > scr->workspace_count-1) {
812 workspace = workspace % scr->workspace_count;
814 } else {
815 int w;
817 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
819 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
821 workspace = w;
823 } else {
824 if (wPreferences.open_transients_with_parent && transientOwner) {
826 workspace = transientOwner->frame->workspace;
828 } else {
830 workspace = scr->current_workspace;
835 /* setup window geometry */
836 if (win_state && win_state->state->use_geometry) {
837 width = win_state->state->w;
838 height = win_state->state->h;
840 wWindowConstrainSize(wwin, &width, &height);
842 /* do not ask for window placement if the window is
843 * transient, during startup, if the initial workspace is another one
844 * or if the window wants to start iconic.
845 * If geometry was saved, restore it. */
847 Bool dontBring = False;
849 if (win_state && win_state->state->use_geometry) {
850 x = win_state->state->x;
851 y = win_state->state->y;
852 } else if ((wwin->transient_for==None
853 || wPreferences.window_placement!=WPM_MANUAL)
854 && !scr->flags.startup
855 && workspace == scr->current_workspace
856 && !wwin->flags.miniaturized
857 && !wwin->flags.maximized
858 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
859 PlaceWindow(wwin, &x, &y, width, height);
860 if (wPreferences.window_placement == WPM_MANUAL)
861 dontBring = True;
864 if (WFLAGP(wwin, dont_move_off) && dontBring)
865 wScreenBringInside(scr, &x, &y, width, height);
868 if (wwin->flags.urgent) {
869 if (!IS_OMNIPRESENT(wwin))
870 wwin->flags.omnipresent ^= 1;
874 *--------------------------------------------------
876 * Create frame, borders and do reparenting
878 *--------------------------------------------------
880 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
881 if (!WFLAGP(wwin, no_titlebar))
882 foo |= WFF_TITLEBAR;
883 if (!WFLAGP(wwin, no_resizebar))
884 foo |= WFF_RESIZEBAR;
886 wwin->frame = wFrameWindowCreate(scr, window_level,
887 x, y, width, height, foo,
888 scr->window_title_texture,
889 scr->resizebar_texture,
890 scr->window_title_pixel,
891 &scr->window_title_gc,
892 &scr->title_font);
894 wwin->frame->flags.is_client_window_frame = 1;
895 wwin->frame->flags.justification = wPreferences.title_justification;
897 /* setup button images */
898 wWindowUpdateButtonImages(wwin);
900 /* hide unused buttons */
901 foo = 0;
902 if (WFLAGP(wwin, no_close_button))
903 foo |= WFF_RIGHT_BUTTON;
904 if (WFLAGP(wwin, no_miniaturize_button))
905 foo |= WFF_LEFT_BUTTON;
906 if (foo!=0)
907 wFrameWindowHideButton(wwin->frame, foo);
909 wwin->frame->child = wwin;
911 #ifdef OLWM_HINTS
912 /* emulate olwm push pin. Make the button look as pushed-in for
913 * the pinned-out state. When the button is clicked, it will
914 * revert to the normal position, which means the pin is pinned-in.
916 if (wwin->flags.olwm_push_pin_out)
917 wFrameWindowUpdatePushButton(wwin->frame, True);
918 #endif /* OLWM_HINTS */
920 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
921 if (title)
922 XFree(title);
924 wwin->frame->workspace = workspace;
926 wwin->frame->on_click_left = windowIconifyClick;
928 wwin->frame->on_click_right = windowCloseClick;
929 wwin->frame->on_dblclick_right = windowCloseDblClick;
931 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
932 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
934 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
937 XSelectInput(dpy, wwin->client_win,
938 wwin->event_mask & ~StructureNotifyMask);
940 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
941 0, wwin->frame->top_width);
943 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
947 int gx, gy;
949 wClientGetGravityOffsets(wwin, &gx, &gy);
951 /* if gravity is to the south, account for the border sizes */
952 if (gy > 0)
953 y -= wwin->frame->top_width + wwin->frame->bottom_width;
957 * wWindowConfigure() will init the client window's size
958 * (wwin->client.{width,height}) and all other geometry
959 * related variables (frame_x,frame_y)
961 wWindowConfigure(wwin, x, y, width, height);
963 /* to make sure the window receives it's new position after reparenting */
964 wWindowSynthConfigureNotify(wwin);
967 *--------------------------------------------------
969 * Setup descriptors and save window to internal
970 * lists
972 *--------------------------------------------------
975 if (wwin->main_window!=None) {
976 WApplication *app;
977 WWindow *leader;
979 /* Leader windows do not necessary set themselves as leaders.
980 * If this is the case, point the leader of this window to
981 * itself */
982 leader = wWindowFor(wwin->main_window);
983 if (leader && leader->main_window==None) {
984 leader->main_window = leader->client_win;
986 app = wApplicationCreate(scr, wwin->main_window);
987 if (app) {
988 app->last_workspace = workspace;
991 * Do application specific stuff, like setting application
992 * wide attributes.
995 if (wwin->flags.hidden) {
996 /* if the window was set to hidden because it was hidden
997 * in a previous incarnation and that state was restored */
998 app->flags.hidden = 1;
1001 if (app->flags.hidden) {
1002 wwin->flags.hidden = 1;
1007 /* setup the frame descriptor */
1008 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1009 wwin->frame->core->descriptor.parent = wwin;
1010 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1012 /* don't let windows go away if we die */
1013 XAddToSaveSet(dpy, window);
1015 XLowerWindow(dpy, window);
1017 /* if window is in this workspace and should be mapped, then map it */
1018 if (!wwin->flags.miniaturized
1019 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1020 && !wwin->flags.hidden && !withdraw) {
1022 /* The following "if" is to avoid crashing of clients that expect
1023 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1024 * after the return from this function.
1026 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1027 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1028 } else {
1029 wClientSetState(wwin, NormalState, None);
1032 #if 0
1033 /* if not auto focus, then map the window under the currently
1034 * focused window */
1035 #define _WIDTH(w) (w)->frame->core->width
1036 #define _HEIGHT(w) (w)->frame->core->height
1037 if (!wPreferences.auto_focus && scr->focused_window
1038 && !scr->flags.startup && !transientOwner
1039 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1040 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1041 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1042 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1043 || wwin->flags.maximized)) {
1044 MoveInStackListUnder(scr->focused_window->frame->core,
1045 wwin->frame->core);
1047 #undef _WIDTH
1048 #undef _HEIGHT
1050 #endif
1052 if (wPreferences.superfluous && !wPreferences.no_animations
1053 && !scr->flags.startup && wwin->transient_for==None
1055 * The brain damaged idiotic non-click to focus modes will
1056 * have trouble with this because:
1058 * 1. window is created and mapped by the client
1059 * 2. window is mapped by wmaker in small size
1060 * 3. window is animated to grow to normal size
1061 * 4. this function returns to normal event loop
1062 * 5. eventually, the EnterNotify event that would trigger
1063 * the window focusing (if the mouse is over that window)
1064 * will be processed by wmaker.
1065 * But since this event will be rather delayed
1066 * (step 3 has a large delay) the time when the event ocurred
1067 * and when it is processed, the client that owns that window
1068 * will reject the XSetInputFocus() for it.
1070 && (wPreferences.focus_mode==WKF_CLICK
1071 || wPreferences.auto_focus)) {
1072 DoWindowBirth(wwin);
1075 wWindowMap(wwin);
1078 /* setup stacking descriptor */
1079 if (transientOwner) {
1080 /* && wPreferences.on_top_transients */
1081 if (transientOwner) {
1082 wwin->frame->core->stacking->child_of =
1083 transientOwner->frame->core;
1085 } else {
1086 wwin->frame->core->stacking->child_of = NULL;
1090 if (!scr->focused_window) {
1091 /* first window on the list */
1092 wwin->next = NULL;
1093 wwin->prev = NULL;
1094 scr->focused_window = wwin;
1095 } else {
1096 WWindow *tmp;
1098 /* add window at beginning of focus window list */
1099 tmp = scr->focused_window;
1100 while (tmp->prev)
1101 tmp = tmp->prev;
1102 tmp->prev = wwin;
1103 wwin->next = tmp;
1104 wwin->prev = NULL;
1107 #ifdef GNOME_STUFF
1108 wGNOMEUpdateClientStateHint(wwin, True);
1109 #endif
1110 #ifdef KWM_HINTS
1111 wKWMUpdateClientWorkspace(wwin);
1112 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1113 #endif
1115 XUngrabServer(dpy);
1118 *--------------------------------------------------
1120 * Final preparations before window is ready to go
1122 *--------------------------------------------------
1125 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1128 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1129 && !wwin->flags.hidden) {
1130 if ((transientOwner && transientOwner->flags.focused)
1131 || wPreferences.auto_focus)
1132 wSetFocusTo(scr, wwin);
1134 wWindowResetMouseGrabs(wwin);
1136 if (!WFLAGP(wwin, no_bind_keys)) {
1137 wWindowSetKeyGrabs(wwin);
1139 #ifdef GNOME_STUFF
1140 wGNOMEUpdateClientListHint(scr);
1141 #endif
1142 #ifdef KWM_HINTS
1143 wwin->flags.kwm_managed = 1;
1145 wKWMSendEventMessage(wwin, WKWMAddWindow);
1146 #endif
1148 wColormapInstallForWindow(scr, scr->cmap_window);
1150 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1152 #ifdef OLWM_HINTS
1153 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1154 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1156 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1157 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1158 wwin->frame_y);
1160 #endif
1163 *------------------------------------------------------------
1164 * Setup Notification Observers
1165 *------------------------------------------------------------
1167 WMAddNotificationObserver(appearanceObserver, wwin,
1168 WNWindowAppearanceSettingsChanged, wwin);
1172 *--------------------------------------------------
1174 * Cleanup temporary stuff
1176 *--------------------------------------------------
1179 if (win_state)
1180 wWindowDeleteSavedState(win_state);
1182 /* If the window must be withdrawed, then do it now.
1183 * Must do some optimization, 'though */
1184 if (withdraw) {
1185 wwin->flags.mapped = 0;
1186 wClientSetState(wwin, WithdrawnState, None);
1187 wUnmanageWindow(wwin, True, False);
1188 wwin = NULL;
1191 return wwin;
1198 WWindow*
1199 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1200 char *title, int x, int y, int width, int height)
1202 WWindow *wwin;
1203 int foo;
1205 wwin = wWindowCreate();
1207 WMAddNotificationObserver(appearanceObserver, wwin,
1208 WNWindowAppearanceSettingsChanged, wwin);
1210 wwin->flags.internal_window = 1;
1212 WSETUFLAG(wwin, omnipresent, 1);
1213 WSETUFLAG(wwin, no_shadeable, 1);
1214 WSETUFLAG(wwin, no_resizable, 1);
1215 WSETUFLAG(wwin, no_miniaturizable, 1);
1217 wwin->focus_mode = WFM_PASSIVE;
1219 wwin->client_win = window;
1220 wwin->screen_ptr = scr;
1222 wwin->transient_for = owner;
1224 wwin->client.x = x;
1225 wwin->client.y = y;
1226 wwin->client.width = width;
1227 wwin->client.height = height;
1229 wwin->frame_x = wwin->client.x;
1230 wwin->frame_y = wwin->client.y;
1233 foo = WFF_RIGHT_BUTTON;
1234 foo |= WFF_TITLEBAR;
1236 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1237 wwin->frame_x, wwin->frame_y,
1238 width, height, foo,
1239 scr->window_title_texture,
1240 scr->resizebar_texture,
1241 scr->window_title_pixel,
1242 &scr->window_title_gc,
1243 &scr->title_font);
1245 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1247 wwin->frame->flags.is_client_window_frame = 1;
1248 wwin->frame->flags.justification = wPreferences.title_justification;
1250 wFrameWindowChangeTitle(wwin->frame, title);
1252 /* setup button images */
1253 wWindowUpdateButtonImages(wwin);
1255 /* hide buttons */
1256 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1258 wwin->frame->child = wwin;
1260 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1262 wwin->frame->on_click_right = windowCloseClick;
1264 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1265 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1267 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1269 wwin->client.y += wwin->frame->top_width;
1270 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1271 0, wwin->frame->top_width);
1273 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1274 wwin->client.width, wwin->client.height);
1276 /* setup the frame descriptor */
1277 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1278 wwin->frame->core->descriptor.parent = wwin;
1279 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1282 XLowerWindow(dpy, window);
1283 XMapSubwindows(dpy, wwin->frame->core->window);
1285 /* setup stacking descriptor */
1286 if (
1287 #ifdef removed
1288 wPreferences.on_top_transients &&
1289 #endif
1290 wwin->transient_for!=None
1291 && wwin->transient_for!=scr->root_win) {
1292 WWindow *tmp;
1293 tmp = wWindowFor(wwin->transient_for);
1294 if (tmp)
1295 wwin->frame->core->stacking->child_of = tmp->frame->core;
1296 } else {
1297 wwin->frame->core->stacking->child_of = NULL;
1301 if (!scr->focused_window) {
1302 /* first window on the list */
1303 wwin->next = NULL;
1304 wwin->prev = NULL;
1305 scr->focused_window = wwin;
1306 } else {
1307 WWindow *tmp;
1309 /* add window at beginning of focus window list */
1310 tmp = scr->focused_window;
1311 while (tmp->prev)
1312 tmp = tmp->prev;
1313 tmp->prev = wwin;
1314 wwin->next = tmp;
1315 wwin->prev = NULL;
1318 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1320 /* if (wPreferences.auto_focus)*/
1321 wSetFocusTo(scr, wwin);
1323 wWindowResetMouseGrabs(wwin);
1325 wWindowSetKeyGrabs(wwin);
1327 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1329 return wwin;
1334 *----------------------------------------------------------------------
1335 * wUnmanageWindow--
1336 * Removes the frame window from a window and destroys all data
1337 * related to it. The window will be reparented back to the root window
1338 * if restore is True.
1340 * Side effects:
1341 * Everything related to the window is destroyed and the window
1342 * is removed from the window lists. Focus is set to the previous on the
1343 * window list.
1344 *----------------------------------------------------------------------
1346 void
1347 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1349 WCoreWindow *frame = wwin->frame->core;
1350 WWindow *owner = NULL;
1351 WWindow *newFocusedWindow = NULL;
1352 int wasFocused;
1353 WScreen *scr = wwin->screen_ptr;
1356 #ifdef KWM_HINTS
1357 wwin->frame->workspace = -1;
1359 wKWMUpdateClientWorkspace(wwin);
1360 #endif
1362 /* First close attribute editor window if open */
1363 if (wwin->flags.inspector_open) {
1364 WWindow *pwin = wwin->inspector->frame; /* the inspector window */
1365 (*pwin->frame->on_click_right)(NULL, pwin, NULL);
1368 /* Close window menu if it's open for this window */
1369 if (wwin->flags.menu_open_for_me) {
1370 CloseWindowMenu(scr);
1373 if (!destroyed) {
1374 if (!wwin->flags.internal_window)
1375 XRemoveFromSaveSet(dpy, wwin->client_win);
1377 XSelectInput(dpy, wwin->client_win, NoEventMask);
1379 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1380 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1383 XUnmapWindow(dpy, frame->window);
1385 XUnmapWindow(dpy, wwin->client_win);
1387 /* deselect window */
1388 wSelectWindow(wwin, False);
1390 /* remove all pending events on window */
1391 /* I think this only matters for autoraise */
1392 if (wPreferences.raise_delay)
1393 WMDeleteTimerWithClientData(wwin->frame->core);
1395 XFlush(dpy);
1397 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1399 /* reparent the window back to the root */
1400 if (restore)
1401 wClientRestore(wwin);
1403 if (wwin->transient_for!=scr->root_win) {
1404 owner = wWindowFor(wwin->transient_for);
1405 if (owner) {
1406 if (!owner->flags.semi_focused) {
1407 owner = NULL;
1408 } else {
1409 owner->flags.semi_focused = 0;
1414 wasFocused = wwin->flags.focused;
1416 /* remove from window focus list */
1417 if (!wwin->prev && !wwin->next) {
1418 /* was the only window */
1419 scr->focused_window = NULL;
1420 newFocusedWindow = NULL;
1421 } else {
1422 WWindow *tmp;
1424 if (wwin->prev)
1425 wwin->prev->next = wwin->next;
1426 if (wwin->next)
1427 wwin->next->prev = wwin->prev;
1428 else {
1429 scr->focused_window = wwin->prev;
1430 scr->focused_window->next = NULL;
1433 /* if in click to focus mode and the window
1434 * was a transient, focus the owner window
1436 tmp = NULL;
1437 if (wPreferences.focus_mode==WKF_CLICK) {
1438 tmp = wWindowFor(wwin->transient_for);
1439 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1440 tmp = NULL;
1443 /* otherwise, focus the next one in the focus list */
1444 if (!tmp) {
1445 tmp = scr->focused_window;
1446 while (tmp) {
1447 if (!WFLAGP(tmp, no_focusable)
1448 && (tmp->flags.mapped || tmp->flags.shaded))
1449 break;
1450 tmp = tmp->prev;
1453 if (wPreferences.focus_mode==WKF_CLICK) {
1454 newFocusedWindow = tmp;
1455 } else if (wPreferences.focus_mode==WKF_SLOPPY
1456 || wPreferences.focus_mode==WKF_POINTER) {
1457 unsigned int mask;
1458 int foo;
1459 Window bar, win;
1461 /* This is to let the root window get the keyboard input
1462 * if Sloppy focus mode and no other window get focus.
1463 * This way keybindings will not freeze.
1465 tmp = NULL;
1466 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1467 &foo, &foo, &foo, &foo, &mask))
1468 tmp = wWindowFor(win);
1469 if (tmp == wwin)
1470 tmp = NULL;
1471 newFocusedWindow = tmp;
1472 } else {
1473 newFocusedWindow = NULL;
1477 if (!wwin->flags.internal_window) {
1478 #ifdef GNOME_STUFF
1479 wGNOMERemoveClient(wwin);
1480 #endif
1481 #ifdef KWM_HINTS
1482 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1483 #endif
1486 #ifdef DEBUG
1487 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1488 (unsigned)frame->window);
1489 #endif
1491 if (wasFocused) {
1492 if (newFocusedWindow != owner && owner) {
1493 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1495 wSetFocusTo(scr, newFocusedWindow);
1497 wWindowDestroy(wwin);
1498 XFlush(dpy);
1502 void
1503 wWindowMap(WWindow *wwin)
1505 XMapWindow(dpy, wwin->frame->core->window);
1506 if (!wwin->flags.shaded) {
1507 /* window will be remapped when getting MapNotify */
1508 XSelectInput(dpy, wwin->client_win,
1509 wwin->event_mask & ~StructureNotifyMask);
1510 XMapWindow(dpy, wwin->client_win);
1511 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1513 wwin->flags.mapped = 1;
1518 void
1519 wWindowUnmap(WWindow *wwin)
1521 wwin->flags.mapped = 0;
1523 /* prevent window withdrawal when getting UnmapNotify */
1524 XSelectInput(dpy, wwin->client_win,
1525 wwin->event_mask & ~StructureNotifyMask);
1526 XUnmapWindow(dpy, wwin->client_win);
1527 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1529 XUnmapWindow(dpy, wwin->frame->core->window);
1534 void
1535 wWindowFocus(WWindow *wwin, WWindow *owin)
1537 WWindow *nowner;
1538 WWindow *oowner;
1540 #ifdef KEEP_XKB_LOCK_STATUS
1541 if (wPreferences.modelock) {
1542 if (!wwin->flags.focused) {
1543 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1546 #endif /* KEEP_XKB_LOCK_STATUS */
1548 wwin->flags.semi_focused = 0;
1550 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1552 wWindowResetMouseGrabs(wwin);
1554 wwin->flags.focused = 1;
1556 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1558 if (owin == wwin || !owin)
1559 return;
1561 nowner = wWindowFor(wwin->transient_for);
1563 /* new window is a transient for the old window */
1564 if (nowner == owin) {
1565 owin->flags.semi_focused = 1;
1566 wWindowUnfocus(nowner);
1567 return;
1570 /* new window is owner of old window */
1571 if (wwin == oowner) {
1572 wWindowUnfocus(owin);
1573 return;
1576 if (!nowner) {
1577 wWindowUnfocus(owin);
1578 return;
1581 /* new window has same owner of old window */
1582 oowner = wWindowFor(owin->transient_for);
1583 if (oowner == nowner) {
1584 /* prevent unfocusing of owner */
1585 oowner->flags.semi_focused = 0;
1586 wWindowUnfocus(owin);
1587 oowner->flags.semi_focused = 1;
1589 return;
1592 /* nowner != NULL && oowner != nowner */
1593 nowner->flags.semi_focused = 1;
1594 wWindowUnfocus(nowner);
1595 wWindowUnfocus(owin);
1599 void
1600 wWindowUnfocus(WWindow *wwin)
1602 CloseWindowMenu(wwin->screen_ptr);
1604 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1605 ? WS_PFOCUSED : WS_UNFOCUSED);
1607 if (wwin->transient_for!=None
1608 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1609 WWindow *owner;
1610 owner = wWindowFor(wwin->transient_for);
1611 if (owner && owner->flags.semi_focused) {
1612 owner->flags.semi_focused = 0;
1613 if (owner->flags.mapped || owner->flags.shaded) {
1614 wWindowUnfocus(owner);
1615 wFrameWindowPaint(owner->frame);
1619 wwin->flags.focused = 0;
1620 wWindowResetMouseGrabs(wwin);
1622 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1629 *----------------------------------------------------------------------
1631 * wWindowConstrainSize--
1632 * Constrains size for the client window, taking the maximal size,
1633 * window resize increments and other size hints into account.
1635 * Returns:
1636 * The closest size to what was given that the client window can
1637 * have.
1639 *----------------------------------------------------------------------
1641 void
1642 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1644 XSizeHints *sizeh = wwin->normal_hints;
1645 int width = *nwidth;
1646 int height = *nheight;
1647 int winc = sizeh->width_inc;
1648 int hinc = sizeh->height_inc;
1650 if (width < sizeh->min_width)
1651 width = sizeh->min_width;
1652 if (height < sizeh->min_height)
1653 height = sizeh->min_height;
1655 if (width > sizeh->max_width)
1656 width = sizeh->max_width;
1657 if (height > sizeh->max_height)
1658 height = sizeh->max_height;
1660 /* aspect ratio code borrowed from olwm */
1661 if (sizeh->flags & PAspect) {
1662 /* adjust max aspect ratio */
1663 if (!(sizeh->max_aspect.x==1 && sizeh->max_aspect.y==1)
1664 && width * sizeh->max_aspect.y > height * sizeh->max_aspect.x) {
1665 if (sizeh->max_aspect.x > sizeh->max_aspect.y) {
1666 height = (width * sizeh->max_aspect.y) / sizeh->max_aspect.x;
1667 if (height > sizeh->max_height) {
1668 height = sizeh->max_height;
1669 width = (height*sizeh->max_aspect.x) / sizeh->max_aspect.y;
1671 } else {
1672 width = (height * sizeh->max_aspect.x) / sizeh->max_aspect.y;
1673 if (width > sizeh->max_width) {
1674 width = sizeh->max_width;
1675 height = (width*sizeh->max_aspect.y) / sizeh->max_aspect.x;
1680 /* adjust min aspect ratio */
1681 if (!(sizeh->min_aspect.x==1 && sizeh->min_aspect.y==1)
1682 && width * sizeh->min_aspect.y < height * sizeh->min_aspect.x) {
1683 if (sizeh->min_aspect.x > sizeh->min_aspect.y) {
1684 height = (width * sizeh->min_aspect.y) / sizeh->min_aspect.x;
1685 if (height < sizeh->min_height) {
1686 height = sizeh->min_height;
1687 width = (height*sizeh->min_aspect.x) / sizeh->min_aspect.y;
1689 } else {
1690 width = (height * sizeh->min_aspect.x) / sizeh->min_aspect.y;
1691 if (width < sizeh->min_width) {
1692 width = sizeh->min_width;
1693 height = (width*sizeh->min_aspect.y) / sizeh->min_aspect.x;
1699 if (sizeh->base_width != 0) {
1700 width = (((width - sizeh->base_width) / winc) * winc)
1701 + sizeh->base_width;
1702 } else {
1703 width = (((width - sizeh->min_width) / winc) * winc)
1704 + sizeh->min_width;
1707 if (sizeh->base_width != 0) {
1708 height = (((height - sizeh->base_height) / hinc) * hinc)
1709 + sizeh->base_height;
1710 } else {
1711 height = (((height - sizeh->min_height) / hinc) * hinc)
1712 + sizeh->min_height;
1715 *nwidth = width;
1716 *nheight = height;
1720 void
1721 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1723 WScreen *scr = wwin->screen_ptr;
1724 WApplication *wapp;
1725 int unmap = 0;
1727 if (workspace >= scr->workspace_count || workspace < 0
1728 || workspace == wwin->frame->workspace)
1729 return;
1731 if (workspace != scr->current_workspace) {
1732 /* Sent to other workspace. Unmap window */
1733 if ((wwin->flags.mapped
1734 || wwin->flags.shaded
1735 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1736 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1738 wapp = wApplicationOf(wwin->main_window);
1739 if (wapp) {
1740 wapp->last_workspace = workspace;
1742 if (wwin->flags.miniaturized) {
1743 XUnmapWindow(dpy, wwin->icon->core->window);
1744 wwin->icon->mapped = 0;
1745 } else {
1746 unmap = 1;
1747 wSetFocusTo(scr, NULL);
1750 } else {
1751 /* brought to current workspace. Map window */
1752 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1753 XMapWindow(dpy, wwin->icon->core->window);
1754 wwin->icon->mapped = 1;
1755 } else if (!wwin->flags.mapped &&
1756 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1757 wWindowMap(wwin);
1760 if (!IS_OMNIPRESENT(wwin)) {
1761 wwin->frame->workspace = workspace;
1762 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1764 #ifdef GNOME_STUFF
1765 wGNOMEUpdateClientStateHint(wwin, True);
1766 #endif
1767 #ifdef KWM_HINTS
1768 wKWMUpdateClientWorkspace(wwin);
1769 wKWMSendEventMessage(wwin, WKWMChangedClient);
1770 #endif
1771 if (unmap) {
1772 wWindowUnmap(wwin);
1777 void
1778 wWindowSynthConfigureNotify(WWindow *wwin)
1780 XEvent sevent;
1782 sevent.type = ConfigureNotify;
1783 sevent.xconfigure.display = dpy;
1784 sevent.xconfigure.event = wwin->client_win;
1785 sevent.xconfigure.window = wwin->client_win;
1787 sevent.xconfigure.x = wwin->client.x;
1788 sevent.xconfigure.y = wwin->client.y;
1789 sevent.xconfigure.width = wwin->client.width;
1790 sevent.xconfigure.height = wwin->client.height;
1792 sevent.xconfigure.border_width = wwin->old_border_width;
1793 if (WFLAGP(wwin, no_titlebar))
1794 sevent.xconfigure.above = None;
1795 else
1796 sevent.xconfigure.above = wwin->frame->titlebar->window;
1798 sevent.xconfigure.override_redirect = False;
1799 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1800 #ifdef KWM_HINTS
1801 wKWMSendEventMessage(wwin, WKWMChangedClient);
1802 #endif
1803 XFlush(dpy);
1808 *----------------------------------------------------------------------
1809 * wWindowConfigure--
1810 * Configures the frame, decorations and client window to the
1811 * specified geometry. The geometry is not checked for validity,
1812 * wWindowConstrainSize() must be used for that.
1813 * The size parameters are for the client window, but the position is
1814 * for the frame.
1815 * The client window receives a ConfigureNotify event, according
1816 * to what ICCCM says.
1818 * Returns:
1819 * None
1821 * Side effects:
1822 * Window size and position are changed and client window receives
1823 * a ConfigureNotify event.
1824 *----------------------------------------------------------------------
1826 void
1827 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1828 WWindow *wwin;
1829 int req_x, req_y; /* new position of the frame */
1830 int req_width, req_height; /* new size of the client */
1832 int synth_notify = False;
1833 int resize;
1835 resize = (req_width!=wwin->client.width
1836 || req_height!=wwin->client.height);
1838 * if the window is being moved but not resized then
1839 * send a synthetic ConfigureNotify
1841 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1842 synth_notify = True;
1845 if (WFLAGP(wwin, dont_move_off))
1846 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1847 req_width, req_height);
1848 if (resize) {
1849 if (req_width < MIN_WINDOW_SIZE)
1850 req_width = MIN_WINDOW_SIZE;
1851 if (req_height < MIN_WINDOW_SIZE)
1852 req_height = MIN_WINDOW_SIZE;
1854 /* If growing, resize inner part before frame,
1855 * if shrinking, resize frame before.
1856 * This will prevent the frame (that can have a different color)
1857 * to be exposed, causing flicker */
1858 if (req_height > wwin->frame->core->height
1859 || req_width > wwin->frame->core->width)
1860 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1862 if (wwin->flags.shaded) {
1863 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1864 req_width, wwin->frame->core->height);
1865 wwin->old_geometry.height = req_height;
1866 } else {
1867 int h;
1869 h = req_height + wwin->frame->top_width
1870 + wwin->frame->bottom_width;
1872 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1875 if (!(req_height > wwin->frame->core->height
1876 || req_width > wwin->frame->core->width))
1877 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1879 wwin->client.x = req_x;
1880 wwin->client.y = req_y + wwin->frame->top_width;
1881 wwin->client.width = req_width;
1882 wwin->client.height = req_height;
1883 } else {
1884 wwin->client.x = req_x;
1885 wwin->client.y = req_y + wwin->frame->top_width;
1887 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1889 wwin->frame_x = req_x;
1890 wwin->frame_y = req_y;
1892 #ifdef SHAPE
1893 if (wShapeSupported && wwin->flags.shaped && resize) {
1894 wWindowSetShape(wwin);
1896 #endif
1898 if (synth_notify)
1899 wWindowSynthConfigureNotify(wwin);
1900 XFlush(dpy);
1904 void
1905 wWindowMove(wwin, req_x, req_y)
1906 WWindow *wwin;
1907 int req_x, req_y; /* new position of the frame */
1909 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1910 int synth_notify = False;
1912 /* Send a synthetic ConfigureNotify event for every window movement. */
1913 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1914 synth_notify = True;
1916 #else
1917 /* A single synthetic ConfigureNotify event is sent at the end of
1918 * a completed (opaque) movement in moveres.c */
1919 #endif
1921 if (WFLAGP(wwin, dont_move_off))
1922 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1923 wwin->frame->core->width, wwin->frame->core->height);
1925 wwin->client.x = req_x;
1926 wwin->client.y = req_y + wwin->frame->top_width;
1928 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1930 wwin->frame_x = req_x;
1931 wwin->frame_y = req_y;
1933 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1934 if (synth_notify)
1935 wWindowSynthConfigureNotify(wwin);
1936 #endif
1940 void
1941 wWindowUpdateButtonImages(WWindow *wwin)
1943 WScreen *scr = wwin->screen_ptr;
1944 Pixmap pixmap, mask;
1945 WFrameWindow *fwin = wwin->frame;
1947 if (WFLAGP(wwin, no_titlebar))
1948 return;
1950 /* miniaturize button */
1952 if (!WFLAGP(wwin, no_miniaturize_button)) {
1953 if (wwin->wm_gnustep_attr
1954 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
1955 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
1957 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
1958 mask = wwin->wm_gnustep_attr->miniaturize_mask;
1959 } else {
1960 mask = None;
1963 if (fwin->lbutton_image
1964 && (fwin->lbutton_image->image != pixmap
1965 || fwin->lbutton_image->mask != mask)) {
1966 wPixmapDestroy(fwin->lbutton_image);
1967 fwin->lbutton_image = NULL;
1970 if (!fwin->lbutton_image) {
1971 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
1972 fwin->lbutton_image->client_owned = 1;
1973 fwin->lbutton_image->client_owned_mask = 1;
1975 } else {
1976 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
1977 wPixmapDestroy(fwin->lbutton_image);
1979 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
1983 /* close button */
1985 if (!WFLAGP(wwin, no_close_button)) {
1986 if (wwin->wm_gnustep_attr
1987 && wwin->wm_gnustep_attr->flags&GSClosePixmapAttr) {
1988 pixmap = wwin->wm_gnustep_attr->close_pixmap;
1990 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr) {
1991 mask = wwin->wm_gnustep_attr->close_mask;
1992 } else {
1993 mask = None;
1996 if (fwin->rbutton_image
1997 && (fwin->rbutton_image->image != pixmap
1998 || fwin->rbutton_image->mask != mask)) {
1999 wPixmapDestroy(fwin->rbutton_image);
2000 fwin->rbutton_image = NULL;
2003 if (!fwin->rbutton_image) {
2004 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2005 fwin->rbutton_image->client_owned = 1;
2006 fwin->rbutton_image->client_owned_mask = 1;
2008 } else if (WFLAGP(wwin, kill_close)) {
2009 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2010 wPixmapDestroy(fwin->rbutton_image);
2012 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2013 } else if (WFLAGP(wwin, broken_close)) {
2014 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2015 wPixmapDestroy(fwin->rbutton_image);
2017 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2018 } else {
2019 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
2020 wPixmapDestroy(fwin->rbutton_image);
2022 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2026 /* force buttons to be redrawn */
2027 fwin->flags.need_texture_change = 1;
2028 wFrameWindowPaint(fwin);
2033 *---------------------------------------------------------------------------
2034 * wWindowConfigureBorders--
2035 * Update window border configuration according to attribute flags.
2037 *---------------------------------------------------------------------------
2039 void
2040 wWindowConfigureBorders(WWindow *wwin)
2042 if (wwin->frame) {
2043 int flags;
2044 int newy, oldh;
2046 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2047 if (!WFLAGP(wwin, no_titlebar))
2048 flags |= WFF_TITLEBAR;
2049 if (!WFLAGP(wwin, no_resizebar))
2050 flags |= WFF_RESIZEBAR;
2051 if (wwin->flags.shaded)
2052 flags |= WFF_IS_SHADED;
2054 oldh = wwin->frame->top_width;
2055 wFrameWindowUpdateBorders(wwin->frame, flags);
2056 if (oldh != wwin->frame->top_width) {
2057 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2059 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2060 wWindowConfigure(wwin, wwin->frame_x, newy,
2061 wwin->client.width, wwin->client.height);
2064 flags = 0;
2065 if (!WFLAGP(wwin, no_miniaturize_button)
2066 && wwin->frame->flags.hide_left_button)
2067 flags |= WFF_LEFT_BUTTON;
2069 if (!WFLAGP(wwin, no_close_button)
2070 && wwin->frame->flags.hide_right_button)
2071 flags |= WFF_RIGHT_BUTTON;
2073 if (flags!=0) {
2074 wWindowUpdateButtonImages(wwin);
2075 wFrameWindowShowButton(wwin->frame, flags);
2078 flags = 0;
2079 if (WFLAGP(wwin, no_miniaturize_button)
2080 && !wwin->frame->flags.hide_left_button)
2081 flags |= WFF_LEFT_BUTTON;
2083 if (WFLAGP(wwin, no_close_button)
2084 && !wwin->frame->flags.hide_right_button)
2085 flags |= WFF_RIGHT_BUTTON;
2087 if (flags!=0)
2088 wFrameWindowHideButton(wwin->frame, flags);
2090 #ifdef SHAPE
2091 if (wShapeSupported && wwin->flags.shaped) {
2092 wWindowSetShape(wwin);
2094 #endif
2099 void
2100 wWindowSaveState(WWindow *wwin)
2102 CARD32 data[10];
2103 int i;
2105 memset(data, 0, sizeof(CARD32)*10);
2106 data[0] = wwin->frame->workspace;
2107 data[1] = wwin->flags.miniaturized;
2108 data[2] = wwin->flags.shaded;
2109 data[3] = wwin->flags.hidden;
2111 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2112 if (wwin->screen_ptr->shortcutWindow[i] == wwin)
2113 data[9] |= 1<<i;
2115 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2116 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2117 (unsigned char *)data, 10);
2121 static int
2122 getSavedState(Window window, WSavedState **state)
2124 Atom type_ret;
2125 int fmt_ret;
2126 unsigned long nitems_ret;
2127 unsigned long bytes_after_ret;
2128 CARD32 *data;
2130 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2131 True, _XA_WINDOWMAKER_STATE,
2132 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2133 (unsigned char **)&data)!=Success || !data)
2134 return 0;
2136 *state = malloc(sizeof(WSavedState));
2138 if (*state) {
2139 (*state)->workspace = data[0];
2140 (*state)->miniaturized = data[1];
2141 (*state)->shaded = data[2];
2142 (*state)->hidden = data[3];
2143 (*state)->use_geometry = data[4];
2144 (*state)->x = data[5];
2145 (*state)->y = data[6];
2146 (*state)->w = data[7];
2147 (*state)->h = data[8];
2148 (*state)->window_shortcuts = data[9];
2150 XFree(data);
2152 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2153 return 1;
2154 else
2155 return 0;
2159 #ifdef SHAPE
2160 void
2161 wWindowClearShape(WWindow *wwin)
2163 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2164 0, wwin->frame->top_width, None, ShapeSet);
2165 XFlush(dpy);
2168 void
2169 wWindowSetShape(WWindow *wwin)
2171 XRectangle rect[2];
2172 int count;
2173 #ifdef OPTIMIZE_SHAPE
2174 XRectangle *rects;
2175 XRectangle *urec;
2176 int ordering;
2178 /* only shape is the client's */
2179 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2180 goto alt_code;
2183 /* Get array of rectangles describing the shape mask */
2184 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2185 &count, &ordering);
2186 if (!rects) {
2187 goto alt_code;
2190 urec = malloc(sizeof(XRectangle)*(count+2));
2191 if (!urec) {
2192 XFree(rects);
2193 goto alt_code;
2196 /* insert our decoration rectangles in the rect list */
2197 memcpy(urec, rects, sizeof(XRectangle)*count);
2198 XFree(rects);
2200 if (!WFLAGP(wwin, no_titlebar)) {
2201 urec[count].x = -1;
2202 urec[count].y = -1 - wwin->frame->top_width;
2203 urec[count].width = wwin->frame->core->width + 2;
2204 urec[count].height = wwin->frame->top_width + 1;
2205 count++;
2207 if (!WFLAGP(wwin, no_resizebar)) {
2208 urec[count].x = -1;
2209 urec[count].y = wwin->frame->core->height
2210 - wwin->frame->bottom_width - wwin->frame->top_width;
2211 urec[count].width = wwin->frame->core->width + 2;
2212 urec[count].height = wwin->frame->bottom_width + 1;
2213 count++;
2216 /* shape our frame window */
2217 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2218 0, wwin->frame->top_width, urec, count,
2219 ShapeSet, Unsorted);
2220 XFlush(dpy);
2221 free(urec);
2222 return;
2224 alt_code:
2225 #endif /* OPTIMIZE_SHAPE */
2226 count = 0;
2227 if (!WFLAGP(wwin, no_titlebar)) {
2228 rect[count].x = -1;
2229 rect[count].y = -1;
2230 rect[count].width = wwin->frame->core->width + 2;
2231 rect[count].height = wwin->frame->top_width + 1;
2232 count++;
2234 if (!WFLAGP(wwin, no_resizebar)) {
2235 rect[count].x = -1;
2236 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2237 rect[count].width = wwin->frame->core->width + 2;
2238 rect[count].height = wwin->frame->bottom_width + 1;
2239 count++;
2241 if (count > 0) {
2242 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2243 0, 0, rect, count, ShapeSet, Unsorted);
2245 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2246 0, wwin->frame->top_width, wwin->client_win,
2247 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2248 XFlush(dpy);
2250 #endif /* SHAPE */
2252 /* ====================================================================== */
2254 static FocusMode
2255 getFocusMode(WWindow *wwin)
2257 FocusMode mode;
2259 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2260 if (wwin->wm_hints->input == True) {
2261 if (wwin->protocols.TAKE_FOCUS)
2262 mode = WFM_LOCALLY_ACTIVE;
2263 else
2264 mode = WFM_PASSIVE;
2265 } else {
2266 if (wwin->protocols.TAKE_FOCUS)
2267 mode = WFM_GLOBALLY_ACTIVE;
2268 else
2269 mode = WFM_NO_INPUT;
2271 } else {
2272 mode = WFM_PASSIVE;
2274 return mode;
2278 void
2279 wWindowSetKeyGrabs(WWindow *wwin)
2281 int i;
2282 WShortKey *key;
2284 for (i=0; i<WKBD_LAST; i++) {
2285 key = &wKeyBindings[i];
2287 if (key->keycode==0)
2288 continue;
2289 if (key->modifier!=AnyModifier) {
2290 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2291 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2292 #ifdef NUMLOCK_HACK
2293 /* Also grab all modifier combinations possible that include,
2294 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2295 * work even if the NumLock/ScrollLock key is on.
2297 wHackedGrabKey(key->keycode, key->modifier,
2298 wwin->frame->core->window, True, GrabModeAsync,
2299 GrabModeAsync);
2300 #endif
2302 XGrabKey(dpy, key->keycode, key->modifier,
2303 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2306 #ifndef LITE
2307 wRootMenuBindShortcuts(wwin->frame->core->window);
2308 #endif
2313 void
2314 wWindowResetMouseGrabs(WWindow *wwin)
2316 /* Mouse grabs can't be done on the client window because of
2317 * ICCCM and because clients that try to do the same will crash.
2319 * But there is a problem wich makes tbar buttons of unfocused
2320 * windows not usable as the click goes to the frame window instead
2321 * of the button itself. Must figure a way to fix that.
2324 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2326 if (!WFLAGP(wwin, no_bind_mouse)) {
2327 /* grabs for Meta+drag */
2328 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2329 True, ButtonPressMask, GrabModeSync,
2330 GrabModeAsync, None, None);
2333 if (!wwin->flags.focused) {
2334 /* the passive grabs to focus the window */
2335 if (wPreferences.focus_mode == WKF_CLICK)
2336 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2337 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2338 None, None);
2340 XFlush(dpy);
2344 void
2345 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2348 if (attr->flags & GSExtraFlagsAttr) {
2349 if (WFLAGP(wwin, broken_close) !=
2350 (attr->extra_flags & GSDocumentEditedFlag)) {
2352 wwin->client_flags.broken_close = !WFLAGP(wwin, broken_close);
2354 wWindowUpdateButtonImages(wwin);
2361 WMagicNumber
2362 wWindowAddSavedState(char *instance, char *class, char *command,
2363 pid_t pid, WSavedState *state)
2365 WWindowState *wstate;
2367 wstate = malloc(sizeof(WWindowState));
2368 if (!wstate)
2369 return 0;
2371 memset(wstate, 0, sizeof(WWindowState));
2372 wstate->pid = pid;
2373 if (instance)
2374 wstate->instance = wstrdup(instance);
2375 if (class)
2376 wstate->class = wstrdup(class);
2377 if (command)
2378 wstate->command = wstrdup(command);
2379 wstate->state = state;
2381 wstate->next = windowState;
2382 windowState = wstate;
2384 #ifdef DEBUG
2385 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2386 class, command);
2387 #endif
2389 return wstate;
2393 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2396 WMagicNumber
2397 wWindowGetSavedState(Window win)
2399 char *instance, *class, *command=NULL;
2400 WWindowState *wstate = windowState;
2401 char **argv;
2402 int argc;
2404 if (!wstate)
2405 return NULL;
2407 if (XGetCommand(dpy, win, &argv, &argc)) {
2408 if (argc > 0)
2409 command = FlattenStringList(argv, argc);
2410 XFreeStringList(argv);
2412 if (!command)
2413 return NULL;
2415 if (PropGetWMClass(win, &class, &instance)) {
2416 while (wstate) {
2417 if (SAME(instance, wstate->instance) &&
2418 SAME(class, wstate->class) &&
2419 SAME(command, wstate->command)) {
2420 break;
2422 wstate = wstate->next;
2424 } else {
2425 wstate = NULL;
2428 #ifdef DEBUG
2429 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2430 class, command);
2431 #endif
2433 if (command) free(command);
2434 if (instance) XFree(instance);
2435 if (class) XFree(class);
2437 return wstate;
2441 void
2442 wWindowDeleteSavedState(WMagicNumber id)
2444 WWindowState *tmp, *wstate=(WWindowState*)id;
2446 if (!wstate || !windowState)
2447 return;
2449 tmp = windowState;
2450 if (tmp==wstate) {
2451 windowState = wstate->next;
2452 #ifdef DEBUG
2453 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2454 wstate, wstate->instance, wstate->class, wstate->command);
2455 #endif
2456 if (wstate->instance) free(wstate->instance);
2457 if (wstate->class) free(wstate->class);
2458 if (wstate->command) free(wstate->command);
2459 free(wstate->state);
2460 free(wstate);
2461 } else {
2462 while (tmp->next) {
2463 if (tmp->next==wstate) {
2464 tmp->next=wstate->next;
2465 #ifdef DEBUG
2466 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2467 wstate, wstate->instance, wstate->class, wstate->command);
2468 #endif
2469 if (wstate->instance) free(wstate->instance);
2470 if (wstate->class) free(wstate->class);
2471 if (wstate->command) free(wstate->command);
2472 free(wstate->state);
2473 free(wstate);
2474 break;
2476 tmp = tmp->next;
2482 void
2483 wWindowDeleteSavedStatesForPID(pid_t pid)
2485 WWindowState *tmp, *wstate;
2487 if (!windowState)
2488 return;
2490 tmp = windowState;
2491 if (tmp->pid == pid) {
2492 wstate = windowState;
2493 windowState = tmp->next;
2494 #ifdef DEBUG
2495 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2496 wstate, wstate->instance, wstate->class, wstate->command);
2497 #endif
2498 if (wstate->instance) free(wstate->instance);
2499 if (wstate->class) free(wstate->class);
2500 if (wstate->command) free(wstate->command);
2501 free(wstate->state);
2502 free(wstate);
2503 } else {
2504 while (tmp->next) {
2505 if (tmp->next->pid==pid) {
2506 wstate = tmp->next;
2507 tmp->next = wstate->next;
2508 #ifdef DEBUG
2509 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2510 wstate, wstate->instance, wstate->class, wstate->command);
2511 #endif
2512 if (wstate->instance) free(wstate->instance);
2513 if (wstate->class) free(wstate->class);
2514 if (wstate->command) free(wstate->command);
2515 free(wstate->state);
2516 free(wstate);
2517 break;
2519 tmp = tmp->next;
2525 /* ====================================================================== */
2527 static void
2528 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2530 WWindow *wwin = data;
2532 #ifndef NUMLOCK_HACK
2533 if ((event->xbutton.state & ValidModMask)
2534 != (event->xbutton.state & ~LockMask)) {
2535 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2536 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2538 #endif
2540 event->xbutton.state &= ValidModMask;
2542 CloseWindowMenu(wwin->screen_ptr);
2544 if (wPreferences.focus_mode==WKF_CLICK
2545 && !(event->xbutton.state&ControlMask)
2546 && !WFLAGP(wwin, no_focusable)) {
2547 wSetFocusTo(wwin->screen_ptr, wwin);
2550 if (event->xbutton.button == Button1)
2551 wRaiseFrame(wwin->frame->core);
2553 if (event->xbutton.window != wwin->frame->resizebar->window) {
2554 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2555 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2556 GrabModeAsync, GrabModeAsync, None,
2557 None, CurrentTime)!=GrabSuccess) {
2558 #ifdef DEBUG0
2559 wwarning("pointer grab failed for window move");
2560 #endif
2561 return;
2565 if (event->xbutton.state & MOD_MASK) {
2566 /* move the window */
2567 wMouseMoveWindow(wwin, event);
2568 XUngrabPointer(dpy, CurrentTime);
2569 } else {
2570 wMouseResizeWindow(wwin, event);
2571 XUngrabPointer(dpy, CurrentTime);
2577 static void
2578 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2580 WWindow *wwin = data;
2582 event->xbutton.state &= ValidModMask;
2584 if (event->xbutton.button==Button1) {
2585 if (event->xbutton.state == 0) {
2586 if (!WFLAGP(wwin, no_shadeable)) {
2587 /* shade window */
2588 if (wwin->flags.shaded)
2589 wUnshadeWindow(wwin);
2590 else
2591 wShadeWindow(wwin);
2593 } else {
2594 int dir = 0;
2596 if (event->xbutton.state & ControlMask)
2597 dir |= MAX_VERTICAL;
2599 if (event->xbutton.state & ShiftMask) {
2600 dir |= MAX_HORIZONTAL;
2601 if (!(event->xbutton.state & ControlMask))
2602 wSelectWindow(wwin, !wwin->flags.selected);
2605 /* maximize window */
2606 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2607 if (wwin->flags.maximized)
2608 wUnmaximizeWindow(wwin);
2609 else
2610 wMaximizeWindow(wwin, dir);
2613 } else if (event->xbutton.button==Button3) {
2614 if (event->xbutton.state & MOD_MASK) {
2615 wHideOtherApplications(wwin);
2617 } else if (event->xbutton.button==Button2) {
2618 wSelectWindow(wwin, !wwin->flags.selected);
2623 static void
2624 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2626 WWindow *wwin = desc->parent;
2628 event->xbutton.state &= ValidModMask;
2630 CloseWindowMenu(wwin->screen_ptr);
2632 if (wPreferences.focus_mode==WKF_CLICK
2633 && !(event->xbutton.state&ControlMask)
2634 && !WFLAGP(wwin, no_focusable)) {
2635 wSetFocusTo(wwin->screen_ptr, wwin);
2637 if (event->xbutton.button == Button1) {
2638 wRaiseFrame(wwin->frame->core);
2641 if (event->xbutton.state & MOD_MASK) {
2642 /* move the window */
2643 if (XGrabPointer(dpy, wwin->client_win, False,
2644 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2645 GrabModeAsync, GrabModeAsync, None,
2646 None, CurrentTime)!=GrabSuccess) {
2647 #ifdef DEBUG0
2648 wwarning("pointer grab failed for window move");
2649 #endif
2650 return;
2652 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2653 wMouseResizeWindow(wwin, event);
2654 else
2655 wMouseMoveWindow(wwin, event);
2656 XUngrabPointer(dpy, CurrentTime);
2661 static void
2662 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2664 WWindow *wwin = (WWindow*)data;
2666 #ifndef NUMLOCK_HACK
2667 if ((event->xbutton.state & ValidModMask)
2668 != (event->xbutton.state & ~LockMask)) {
2669 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2670 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2672 #endif
2673 event->xbutton.state &= ValidModMask;
2675 CloseWindowMenu(wwin->screen_ptr);
2677 if (wPreferences.focus_mode==WKF_CLICK
2678 && !(event->xbutton.state&ControlMask)
2679 && !WFLAGP(wwin, no_focusable)) {
2680 wSetFocusTo(wwin->screen_ptr, wwin);
2683 if (event->xbutton.button == Button1
2684 || event->xbutton.button == Button2) {
2686 if (event->xbutton.button == Button1) {
2687 if (event->xbutton.state & MOD_MASK) {
2688 wLowerFrame(wwin->frame->core);
2689 } else {
2690 wRaiseFrame(wwin->frame->core);
2693 if ((event->xbutton.state & ShiftMask)
2694 && !(event->xbutton.state & ControlMask)) {
2695 wSelectWindow(wwin, !wwin->flags.selected);
2696 return;
2698 if (event->xbutton.window != wwin->frame->titlebar->window
2699 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2700 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2701 GrabModeAsync, GrabModeAsync, None,
2702 None, CurrentTime)!=GrabSuccess) {
2703 #ifdef DEBUG0
2704 wwarning("pointer grab failed for window move");
2705 #endif
2706 return;
2709 /* move the window */
2710 wMouseMoveWindow(wwin, event);
2712 XUngrabPointer(dpy, CurrentTime);
2713 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2714 && !wwin->flags.internal_window
2715 && !WCHECK_STATE(WSTATE_MODAL)) {
2716 WObjDescriptor *desc;
2718 if (event->xbutton.window != wwin->frame->titlebar->window
2719 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2720 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2721 GrabModeAsync, GrabModeAsync, None,
2722 None, CurrentTime)!=GrabSuccess) {
2723 #ifdef DEBUG0
2724 wwarning("pointer grab failed for window move");
2725 #endif
2726 return;
2729 OpenWindowMenu(wwin, event->xbutton.x_root,
2730 wwin->frame_y+wwin->frame->top_width, False);
2732 /* allow drag select */
2733 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2734 event->xany.send_event = True;
2735 (*desc->handle_mousedown)(desc, event);
2737 XUngrabPointer(dpy, CurrentTime);
2743 static void
2744 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2746 WWindow *wwin = data;
2748 event->xbutton.state &= ValidModMask;
2750 CloseWindowMenu(wwin->screen_ptr);
2752 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2753 return;
2755 /* if control-click, kill the client */
2756 if (event->xbutton.state & ControlMask) {
2757 wClientKill(wwin);
2758 } else {
2759 #ifdef OLWM_HINTS
2760 if (wwin->flags.olwm_push_pin_out) {
2762 wwin->flags.olwm_push_pin_out = 0;
2764 wOLWMChangePushpinState(wwin, True);
2766 wFrameWindowUpdatePushButton(wwin->frame, False);
2768 return;
2770 #endif
2771 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2772 /* send delete message */
2773 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2779 static void
2780 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2782 WWindow *wwin = data;
2784 CloseWindowMenu(wwin->screen_ptr);
2786 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2787 return;
2789 /* send delete message */
2790 if (wwin->protocols.DELETE_WINDOW) {
2791 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2792 } else {
2793 wClientKill(wwin);
2798 static void
2799 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2801 WWindow *wwin = data;
2803 event->xbutton.state &= ValidModMask;
2805 CloseWindowMenu(wwin->screen_ptr);
2807 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2808 return;
2810 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2811 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2812 LastTimestamp);
2813 } else {
2814 WApplication *wapp;
2815 if ((event->xbutton.state & ControlMask) ||
2816 (event->xbutton.button == Button3)) {
2818 wapp = wApplicationOf(wwin->main_window);
2819 if (wapp && !WFLAGP(wwin, no_appicon))
2820 wHideApplication(wapp);
2821 } else if (event->xbutton.state==0) {
2822 wIconifyWindow(wwin);