clearance for text on titlebar.
[wmaker-crm.git] / src / window.c
blob81d32fcd45ca8439d15980c467b82b3198dc5ab4
1 /* window.c - client window managing stuffs
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
38 #include "WindowMaker.h"
39 #include "GNUstep.h"
40 #include "wcore.h"
41 #include "framewin.h"
42 #include "texture.h"
43 #include "window.h"
44 #include "winspector.h"
45 #include "icon.h"
46 #include "properties.h"
47 #include "actions.h"
48 #include "client.h"
49 #include "funcs.h"
50 #include "keybind.h"
51 #include "stacking.h"
52 #include "defaults.h"
53 #include "workspace.h"
56 #ifdef MWM_HINTS
57 # include "motif.h"
58 #endif
59 #ifdef KWM_HINTS
60 # include "kwm.h"
61 #endif
62 #ifdef GNOME_STUFF
63 # include "gnome.h"
64 #endif
65 #ifdef OLWM_HINTS
66 # include "openlook.h"
67 #endif
69 /****** Global Variables ******/
71 extern WShortKey wKeyBindings[WKBD_LAST];
73 #ifdef SHAPE
74 extern Bool wShapeSupported;
75 #endif
77 /* contexts */
78 extern XContext wWinContext;
80 /* cursors */
81 extern Cursor wCursor[WCUR_LAST];
83 /* protocol atoms */
84 extern Atom _XA_WM_DELETE_WINDOW;
85 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
87 extern Atom _XA_WINDOWMAKER_STATE;
89 extern WPreferences wPreferences;
91 #define MOD_MASK wPreferences.modifier_mask
93 extern Time LastTimestamp;
95 /* superfluous... */
96 extern void DoWindowBirth(WWindow*);
99 /***** Local Stuff *****/
102 static WWindowState *windowState=NULL;
106 /* local functions */
107 static FocusMode getFocusMode(WWindow *wwin);
109 static int getSavedState(Window window, WSavedState **state);
111 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
113 /* event handlers */
116 /* frame window (during window grabs) */
117 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
119 /* close button */
120 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
121 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
123 /* iconify button */
124 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
126 #ifdef XKB_BUTTON_HINT
127 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
128 #endif
130 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
131 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
133 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
136 /****** Notification Observers ******/
138 static void
139 appearanceObserver(void *self, WMNotification *notif)
141 WWindow *wwin = (WWindow*)self;
142 int flags = (int)WMGetNotificationClientData(notif);
144 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
145 return;
147 if (flags & WFontSettings) {
148 wWindowConfigureBorders(wwin);
149 if(wwin->flags.shaded) {
150 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
151 wwin->frame->top_width - 1);
153 wwin->client.y = wwin->frame_y - wwin->client.height
154 + wwin->frame->top_width;
155 wWindowSynthConfigureNotify(wwin);
158 if (flags & WTextureSettings) {
159 wwin->frame->flags.need_texture_remake = 1;
161 if (flags & (WTextureSettings | WColorSettings)) {
162 if (wwin->frame->titlebar)
163 XClearWindow(dpy, wwin->frame->titlebar->window);
165 wFrameWindowPaint(wwin->frame);
169 /************************************/
171 WWindow*
172 wWindowFor(Window window)
174 WObjDescriptor *desc;
176 if (window==None)
177 return NULL;
179 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
180 return NULL;
182 if (desc->parent_type==WCLASS_WINDOW)
183 return desc->parent;
184 else if (desc->parent_type==WCLASS_FRAME) {
185 WFrameWindow *frame = (WFrameWindow*)desc->parent;
186 if (frame->flags.is_client_window_frame)
187 return frame->child;
190 return NULL;
194 WWindow*
195 wWindowCreate()
197 WWindow *wwin;
199 wwin = wmalloc(sizeof(WWindow));
200 wretain(wwin);
202 memset(wwin, 0, sizeof(WWindow));
204 wwin->client_descriptor.handle_mousedown = frameMouseDown;
205 wwin->client_descriptor.parent = wwin;
206 wwin->client_descriptor.self = wwin;
207 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
209 return wwin;
213 void
214 wWindowDestroy(WWindow *wwin)
216 int i;
218 WMRemoveNotificationObserver(wwin);
220 wwin->flags.destroyed = 1;
222 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
223 if (!wwin->screen_ptr->shortcutWindows[i])
224 continue;
226 WMRemoveFromBag(wwin->screen_ptr->shortcutWindows[i], wwin);
228 if (!WMGetBagItemCount(wwin->screen_ptr->shortcutWindows[i])) {
229 WMFreeBag(wwin->screen_ptr->shortcutWindows[i]);
230 wwin->screen_ptr->shortcutWindows[i] = NULL;
234 if (wwin->normal_hints)
235 free(wwin->normal_hints);
237 if (wwin->wm_hints)
238 XFree(wwin->wm_hints);
240 if (wwin->wm_instance)
241 XFree(wwin->wm_instance);
243 if (wwin->wm_class)
244 XFree(wwin->wm_class);
246 if (wwin->wm_gnustep_attr)
247 free(wwin->wm_gnustep_attr);
249 if (wwin->cmap_windows)
250 XFree(wwin->cmap_windows);
252 XDeleteContext(dpy, wwin->client_win, wWinContext);
254 if (wwin->frame)
255 wFrameWindowDestroy(wwin->frame);
257 if (wwin->icon) {
258 RemoveFromStackList(wwin->icon->core);
259 wIconDestroy(wwin->icon);
260 if (wPreferences.auto_arrange_icons)
261 wArrangeIcons(wwin->screen_ptr, True);
263 wrelease(wwin);
269 static void
270 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
272 if (gs_hints->flags & GSWindowStyleAttr) {
273 wwin->client_flags.no_titlebar =
274 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
276 wwin->client_flags.no_close_button =
277 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
279 wwin->client_flags.no_closable =
280 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
282 wwin->client_flags.no_miniaturize_button =
283 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
285 wwin->client_flags.no_miniaturizable =
286 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
288 wwin->client_flags.no_resizebar =
289 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
291 wwin->client_flags.no_resizable =
292 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
293 } else {
294 /* setup the defaults */
295 wwin->client_flags.no_titlebar = 0;
296 wwin->client_flags.no_closable = 0;
297 wwin->client_flags.no_miniaturizable = 0;
298 wwin->client_flags.no_resizable = 0;
299 wwin->client_flags.no_close_button = 0;
300 wwin->client_flags.no_miniaturize_button = 0;
301 wwin->client_flags.no_resizebar = 0;
303 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
304 wwin->client_flags.no_appicon = 1;
309 void
310 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
311 WWindowAttributes *mask)
313 if (wflags->no_appicon && mask->no_appicon)
314 wflags->emulate_appicon = 0;
316 if (wwin->main_window!=None) {
317 WApplication *wapp = wApplicationOf(wwin->main_window);
318 if (wapp && !wapp->flags.emulated)
319 wflags->emulate_appicon = 0;
322 if (wwin->transient_for!=None
323 && wwin->transient_for!=wwin->screen_ptr->root_win)
324 wflags->emulate_appicon = 0;
326 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
327 wflags->sunken = 0;
332 void
333 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
335 WScreen *scr = wwin->screen_ptr;
337 /* sets global default stuff */
338 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
339 &wwin->client_flags, NULL, True);
341 * Decoration setting is done in this precedence (lower to higher)
342 * - use global default in the resource database
343 * - guess some settings
344 * - use GNUstep/external window attributes
345 * - set hints specified for the app in the resource DB
348 WSETUFLAG(wwin, broken_close, 0);
350 if (wwin->protocols.DELETE_WINDOW)
351 WSETUFLAG(wwin, kill_close, 0);
352 else
353 WSETUFLAG(wwin, kill_close, 1);
355 /* transients can't be iconified or maximized */
356 if (wwin->transient_for) {
357 WSETUFLAG(wwin, no_miniaturizable, 1);
358 WSETUFLAG(wwin, no_miniaturize_button, 1);
361 /* if the window can't be resized, remove the resizebar */
362 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
363 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
364 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
365 WSETUFLAG(wwin, no_resizable, 1);
366 WSETUFLAG(wwin, no_resizebar, 1);
369 /* set GNUstep window attributes */
370 if (wwin->wm_gnustep_attr) {
371 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
373 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
375 switch (wwin->wm_gnustep_attr->window_level) {
376 case WMNormalWindowLevel:
377 *level = WMNormalLevel;
378 break;
379 case WMFloatingWindowLevel:
380 *level = WMFloatingLevel;
381 break;
382 case WMDockWindowLevel:
383 *level = WMDockLevel;
384 break;
385 case WMSubmenuWindowLevel:
386 *level = WMSubmenuLevel;
387 break;
388 case WMMainMenuWindowLevel:
389 *level = WMMainMenuLevel;
390 break;
391 default:
392 *level = WMNormalLevel;
393 break;
395 } else {
396 /* setup defaults */
397 *level = WMNormalLevel;
399 } else {
400 int tmp_workspace = -1;
401 int tmp_level = -1;
403 #ifdef MWM_HINTS
404 wMWMCheckClientHints(wwin);
405 #endif /* MWM_HINTS */
407 #ifdef KWM_HINTS
408 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
409 #endif /* KWM_HINTS */
411 #ifdef GNOME_STUFF
412 wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
413 #endif /* GNOME_STUFF */
415 #ifdef OLWM_HINTS
416 wOLWMCheckClientHints(wwin);
417 #endif /* OLWM_HINTS */
419 if (tmp_level < 0) {
420 if (WFLAGP(wwin, floating))
421 *level = WMFloatingLevel;
422 else if (WFLAGP(wwin, sunken))
423 *level = WMSunkenLevel;
424 else
425 *level = WMNormalLevel;
426 } else {
427 *level = tmp_level;
430 if (tmp_workspace >= 0) {
431 *workspace = tmp_workspace % scr->workspace_count;
436 * Set attributes specified only for that window/class.
437 * This might do duplicate work with the 1st wDefaultFillAttributes().
439 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
440 &wwin->user_flags, &wwin->defined_user_flags,
441 False);
443 * Sanity checks for attributes that depend on other attributes
445 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
446 wwin->user_flags.emulate_appicon = 0;
448 if (wwin->main_window!=None) {
449 WApplication *wapp = wApplicationOf(wwin->main_window);
450 if (wapp && !wapp->flags.emulated)
451 wwin->user_flags.emulate_appicon = 0;
454 if (wwin->transient_for!=None
455 && wwin->transient_for!=wwin->screen_ptr->root_win)
456 wwin->user_flags.emulate_appicon = 0;
458 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
459 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
460 wwin->user_flags.sunken = 0;
462 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
468 Bool
469 wWindowCanReceiveFocus(WWindow *wwin)
471 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
472 return False;
473 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
474 return False;
475 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
476 return False;
478 return True;
482 Bool
483 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
485 int w1, h1, w2, h2;
487 w1 = wwin->frame->core->width;
488 h1 = wwin->frame->core->height;
489 w2 = obscured->frame->core->width;
490 h2 = obscured->frame->core->height;
492 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
493 && wwin->frame->workspace != obscured->frame->workspace)
494 return False;
496 if (wwin->frame_x + w1 < obscured->frame_x
497 || wwin->frame_y + h1 < obscured->frame_y
498 || wwin->frame_x > obscured->frame_x + w2
499 || wwin->frame_y > obscured->frame_y + h2) {
500 return False;
503 return True;
508 *----------------------------------------------------------------
509 * wManageWindow--
510 * reparents the window and allocates a descriptor for it.
511 * Window manager hints and other hints are fetched to configure
512 * the window decoration attributes and others. User preferences
513 * for the window are used if available, to configure window
514 * decorations and some behaviour.
515 * If in startup, windows that are override redirect,
516 * unmapped and never were managed and are Withdrawn are not
517 * managed.
519 * Returns:
520 * the new window descriptor
522 * Side effects:
523 * The window is reparented and appropriate notification
524 * is done to the client. Input mask for the window is setup.
525 * The window descriptor is also associated with various window
526 * contexts and inserted in the head of the window list.
527 * Event handler contexts are associated for some objects
528 * (buttons, titlebar and resizebar)
530 *----------------------------------------------------------------
532 WWindow*
533 wManageWindow(WScreen *scr, Window window)
535 WWindow *wwin;
536 int x, y;
537 unsigned width, height;
538 XWindowAttributes wattribs;
539 XSetWindowAttributes attribs;
540 WWindowState *win_state;
541 WWindow *transientOwner = NULL;
542 int window_level;
543 int wm_state;
544 int foo;
545 int workspace = -1;
546 char *title;
547 Bool withdraw = False;
549 /* mutex. */
550 /* XGrabServer(dpy); */
551 XSync(dpy, False);
552 /* make sure the window is still there */
553 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
554 XUngrabServer(dpy);
555 return NULL;
558 /* if it's an override-redirect, ignore it */
559 if (wattribs.override_redirect) {
560 XUngrabServer(dpy);
561 return NULL;
564 wm_state = PropGetWindowState(window);
566 /* if it's startup and the window is unmapped, don't manage it */
567 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
568 XUngrabServer(dpy);
569 return NULL;
572 if (!wFetchName(dpy, window, &title)) {
573 title = NULL;
576 #ifdef KWM_HINTS
577 if (title && !wKWMManageableClient(scr, window, title)) {
578 XFree(title);
579 XUngrabServer(dpy);
580 return NULL;
582 #endif /* KWM_HINTS */
585 wwin = wWindowCreate();
587 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
589 #ifdef DEBUG
590 printf("managing window %x\n", (unsigned)window);
591 #endif
593 #ifdef SHAPE
594 if (wShapeSupported) {
595 int junk;
596 unsigned int ujunk;
597 int b_shaped;
599 XShapeSelectInput(dpy, window, ShapeNotifyMask);
600 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
601 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
602 wwin->flags.shaped = b_shaped;
604 #endif
607 *--------------------------------------------------
609 * Get hints and other information in properties
611 *--------------------------------------------------
613 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
615 /* setup descriptor */
616 wwin->client_win = window;
617 wwin->screen_ptr = scr;
619 wwin->old_border_width = wattribs.border_width;
621 wwin->event_mask = CLIENT_EVENTS;
622 attribs.event_mask = CLIENT_EVENTS;
623 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
624 attribs.save_under = False;
625 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
626 |CWSaveUnder, &attribs);
627 XSetWindowBorderWidth(dpy, window, 0);
629 /* get hints from GNUstep app */
630 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
631 wwin->wm_gnustep_attr = NULL;
634 wwin->client_leader = PropGetClientLeader(window);
635 if (wwin->client_leader!=None)
636 wwin->main_window = wwin->client_leader;
638 wwin->wm_hints = XGetWMHints(dpy, window);
640 if (wwin->wm_hints) {
641 if (wwin->wm_hints->flags & StateHint) {
643 if (wwin->wm_hints->initial_state == IconicState) {
645 wwin->flags.miniaturized = 1;
647 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
649 withdraw = True;
653 if (wwin->wm_hints->flags & WindowGroupHint) {
654 wwin->group_id = wwin->wm_hints->window_group;
655 /* window_group has priority over CLIENT_LEADER */
656 wwin->main_window = wwin->group_id;
657 } else {
658 wwin->group_id = None;
661 if (wwin->wm_hints->flags & UrgencyHint)
662 wwin->flags.urgent = 1;
663 } else {
664 wwin->group_id = None;
667 PropGetProtocols(window, &wwin->protocols);
669 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
670 wwin->transient_for = None;
671 } else {
672 if (wwin->transient_for==None || wwin->transient_for==window) {
673 wwin->transient_for = scr->root_win;
674 } else {
675 transientOwner = wWindowFor(wwin->transient_for);
676 if (transientOwner && transientOwner->main_window!=None) {
677 wwin->main_window = transientOwner->main_window;
678 } /*else {
679 wwin->main_window = None;
684 /* guess the focus mode */
685 wwin->focus_mode = getFocusMode(wwin);
687 /* get geometry stuff */
688 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
690 /* get colormap windows */
691 GetColormapWindows(wwin);
694 *--------------------------------------------------
696 * Setup the decoration/window attributes and
697 * geometry
699 *--------------------------------------------------
702 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
704 #ifdef OLWM_HINTS
705 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
706 && wwin->group_id != None && wwin->group_id != window) {
708 transientOwner = wWindowFor(wwin->group_id);
710 if (transientOwner) {
711 wwin->transient_for = wwin->group_id;
713 /* transients can't be iconified or maximized */
714 if (wwin->transient_for) {
715 WSETUFLAG(wwin, no_miniaturizable, 1);
716 WSETUFLAG(wwin, no_miniaturize_button, 1);
720 #endif /* OLWM_HINTS */
723 * Make broken apps behave as a nice app.
725 if (WFLAGP(wwin, emulate_appicon)) {
726 wwin->main_window = wwin->client_win;
730 *------------------------------------------------------------
732 * Setup the initial state of the window
734 *------------------------------------------------------------
737 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
738 wwin->flags.miniaturized = 1;
741 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
742 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
745 #ifdef GNOME_STUFF
746 wGNOMECheckInitialClientState(wwin);
747 #endif
748 #ifdef KWM_HINTS
749 wKWMCheckClientInitialState(wwin);
750 #endif
752 /* apply previous state if it exists and we're in startup */
753 if (scr->flags.startup && wm_state >= 0) {
755 if (wm_state == IconicState) {
757 wwin->flags.miniaturized = 1;
759 } else if (wm_state == WithdrawnState) {
761 withdraw = True;
765 /* if there is a saved state (from file), restore it */
766 win_state = NULL;
767 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
768 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
769 } else {
770 win_state = (WWindowState*)wWindowGetSavedState(window);
772 if (win_state && !withdraw) {
774 if (win_state->state->hidden>0)
775 wwin->flags.hidden = win_state->state->hidden;
777 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
778 wwin->flags.shaded = win_state->state->shaded;
780 if (win_state->state->miniaturized>0 &&
781 !WFLAGP(wwin, no_miniaturizable)) {
782 wwin->flags.miniaturized = win_state->state->miniaturized;
784 if (!IS_OMNIPRESENT(wwin)) {
785 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
786 wwin->wm_class);
787 if (w < 0 || w >= scr->workspace_count) {
788 workspace = win_state->state->workspace;
789 if (workspace >= scr->workspace_count)
790 workspace = scr->current_workspace;
791 } else {
792 workspace = w;
794 } else {
795 workspace = scr->current_workspace;
799 /* if we're restarting, restore saved state (from hints).
800 * This will overwrite previous */
802 WSavedState *wstate;
804 if (getSavedState(window, &wstate)) {
805 wwin->flags.shaded = wstate->shaded;
806 wwin->flags.hidden = wstate->hidden;
807 wwin->flags.miniaturized = wstate->miniaturized;
808 workspace = wstate->workspace;
810 if (scr->flags.startup && wstate->window_shortcuts >= 0) {
811 int i;
813 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
814 if (wstate->window_shortcuts & (1<<i)) {
815 if (!scr->shortcutWindows[i])
816 scr->shortcutWindows[i] = WMCreateBag(4);
818 WMPutInBag(scr->shortcutWindows[i], wwin);
822 free(wstate);
826 /* don't let transients start miniaturized if their owners are not */
827 if (transientOwner && !transientOwner->flags.miniaturized
828 && wwin->flags.miniaturized && !withdraw) {
829 wwin->flags.miniaturized = 0;
830 if (wwin->wm_hints)
831 wwin->wm_hints->initial_state = NormalState;
834 /* set workspace on which the window starts */
835 if (workspace >= 0) {
836 if (workspace > scr->workspace_count-1) {
837 workspace = workspace % scr->workspace_count;
839 } else {
840 int w;
842 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
844 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
846 workspace = w;
848 } else {
849 if (wPreferences.open_transients_with_parent && transientOwner) {
851 workspace = transientOwner->frame->workspace;
853 } else {
855 workspace = scr->current_workspace;
860 /* setup window geometry */
861 if (win_state && win_state->state->use_geometry) {
862 width = win_state->state->w;
863 height = win_state->state->h;
865 wWindowConstrainSize(wwin, &width, &height);
867 /* do not ask for window placement if the window is
868 * transient, during startup, if the initial workspace is another one
869 * or if the window wants to start iconic.
870 * If geometry was saved, restore it. */
872 Bool dontBring = False;
874 if (win_state && win_state->state->use_geometry) {
875 x = win_state->state->x;
876 y = win_state->state->y;
877 } else if ((wwin->transient_for==None
878 || wPreferences.window_placement!=WPM_MANUAL)
879 && !scr->flags.startup
880 && workspace == scr->current_workspace
881 && !wwin->flags.miniaturized
882 && !wwin->flags.maximized
883 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
884 PlaceWindow(wwin, &x, &y, width, height);
885 if (wPreferences.window_placement == WPM_MANUAL)
886 dontBring = True;
889 if (WFLAGP(wwin, dont_move_off) && dontBring)
890 wScreenBringInside(scr, &x, &y, width, height);
893 if (wwin->flags.urgent) {
894 if (!IS_OMNIPRESENT(wwin))
895 wwin->flags.omnipresent ^= 1;
899 *--------------------------------------------------
901 * Create frame, borders and do reparenting
903 *--------------------------------------------------
905 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
906 #ifdef XKB_BUTTON_HINT
907 if (wPreferences.modelock)
908 foo |= WFF_LANGUAGE_BUTTON;
909 #endif
910 if (!WFLAGP(wwin, no_titlebar))
911 foo |= WFF_TITLEBAR;
912 if (!WFLAGP(wwin, no_resizebar))
913 foo |= WFF_RESIZEBAR;
915 wwin->frame = wFrameWindowCreate(scr, window_level,
916 x, y, width, height, foo,
917 scr->window_title_texture,
918 scr->resizebar_texture,
919 scr->window_title_pixel,
920 #ifdef DRAWSTRING_PLUGIN
921 W_STRING_FTITLE,
922 #endif
923 &scr->window_title_gc,
924 &scr->title_font);
926 wwin->frame->flags.is_client_window_frame = 1;
927 wwin->frame->flags.justification = wPreferences.title_justification;
929 /* setup button images */
930 wWindowUpdateButtonImages(wwin);
932 /* hide unused buttons */
933 foo = 0;
934 if (WFLAGP(wwin, no_close_button))
935 foo |= WFF_RIGHT_BUTTON;
936 if (WFLAGP(wwin, no_miniaturize_button))
937 foo |= WFF_LEFT_BUTTON;
938 #ifdef XKB_BUTTON_HINT
939 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
940 foo |= WFF_LANGUAGE_BUTTON;
941 #endif
942 if (foo!=0)
943 wFrameWindowHideButton(wwin->frame, foo);
945 wwin->frame->child = wwin;
947 #ifdef OLWM_HINTS
948 /* emulate olwm push pin. Make the button look as pushed-in for
949 * the pinned-out state. When the button is clicked, it will
950 * revert to the normal position, which means the pin is pinned-in.
952 if (wwin->flags.olwm_push_pin_out)
953 wFrameWindowUpdatePushButton(wwin->frame, True);
954 #endif /* OLWM_HINTS */
956 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
957 if (title)
958 XFree(title);
960 wwin->frame->workspace = workspace;
962 wwin->frame->on_click_left = windowIconifyClick;
963 #ifdef XKB_BUTTON_HINT
964 if (wPreferences.modelock)
965 wwin->frame->on_click_language = windowLanguageClick;
966 #endif
968 wwin->frame->on_click_right = windowCloseClick;
969 wwin->frame->on_dblclick_right = windowCloseDblClick;
971 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
972 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
974 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
977 XSelectInput(dpy, wwin->client_win,
978 wwin->event_mask & ~StructureNotifyMask);
980 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
981 0, wwin->frame->top_width);
983 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
987 int gx, gy;
989 wClientGetGravityOffsets(wwin, &gx, &gy);
991 /* if gravity is to the south, account for the border sizes */
992 if (gy > 0)
993 y -= wwin->frame->top_width + wwin->frame->bottom_width;
997 * wWindowConfigure() will init the client window's size
998 * (wwin->client.{width,height}) and all other geometry
999 * related variables (frame_x,frame_y)
1001 wWindowConfigure(wwin, x, y, width, height);
1003 /* to make sure the window receives it's new position after reparenting */
1004 wWindowSynthConfigureNotify(wwin);
1007 *--------------------------------------------------
1009 * Setup descriptors and save window to internal
1010 * lists
1012 *--------------------------------------------------
1015 if (wwin->main_window!=None) {
1016 WApplication *app;
1017 WWindow *leader;
1019 /* Leader windows do not necessary set themselves as leaders.
1020 * If this is the case, point the leader of this window to
1021 * itself */
1022 leader = wWindowFor(wwin->main_window);
1023 if (leader && leader->main_window==None) {
1024 leader->main_window = leader->client_win;
1026 app = wApplicationCreate(scr, wwin->main_window);
1027 if (app) {
1028 app->last_workspace = workspace;
1031 * Do application specific stuff, like setting application
1032 * wide attributes.
1035 if (wwin->flags.hidden) {
1036 /* if the window was set to hidden because it was hidden
1037 * in a previous incarnation and that state was restored */
1038 app->flags.hidden = 1;
1041 if (app->flags.hidden) {
1042 wwin->flags.hidden = 1;
1047 /* setup the frame descriptor */
1048 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1049 wwin->frame->core->descriptor.parent = wwin;
1050 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1052 /* don't let windows go away if we die */
1053 XAddToSaveSet(dpy, window);
1055 XLowerWindow(dpy, window);
1057 /* if window is in this workspace and should be mapped, then map it */
1058 if (!wwin->flags.miniaturized
1059 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1060 && !wwin->flags.hidden && !withdraw) {
1062 /* The following "if" is to avoid crashing of clients that expect
1063 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1064 * after the return from this function.
1066 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1067 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1068 } else {
1069 wClientSetState(wwin, NormalState, None);
1072 #if 0
1073 /* if not auto focus, then map the window under the currently
1074 * focused window */
1075 #define _WIDTH(w) (w)->frame->core->width
1076 #define _HEIGHT(w) (w)->frame->core->height
1077 if (!wPreferences.auto_focus && scr->focused_window
1078 && !scr->flags.startup && !transientOwner
1079 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1080 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1081 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1082 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1083 || wwin->flags.maximized)) {
1084 MoveInStackListUnder(scr->focused_window->frame->core,
1085 wwin->frame->core);
1087 #undef _WIDTH
1088 #undef _HEIGHT
1090 #endif
1092 if (wPreferences.superfluous && !wPreferences.no_animations
1093 && !scr->flags.startup && wwin->transient_for==None
1095 * The brain damaged idiotic non-click to focus modes will
1096 * have trouble with this because:
1098 * 1. window is created and mapped by the client
1099 * 2. window is mapped by wmaker in small size
1100 * 3. window is animated to grow to normal size
1101 * 4. this function returns to normal event loop
1102 * 5. eventually, the EnterNotify event that would trigger
1103 * the window focusing (if the mouse is over that window)
1104 * will be processed by wmaker.
1105 * But since this event will be rather delayed
1106 * (step 3 has a large delay) the time when the event ocurred
1107 * and when it is processed, the client that owns that window
1108 * will reject the XSetInputFocus() for it.
1110 && (wPreferences.focus_mode==WKF_CLICK
1111 || wPreferences.auto_focus)) {
1112 DoWindowBirth(wwin);
1115 wWindowMap(wwin);
1118 /* setup stacking descriptor */
1119 if (transientOwner) {
1120 /* && wPreferences.on_top_transients */
1121 if (transientOwner) {
1122 wwin->frame->core->stacking->child_of =
1123 transientOwner->frame->core;
1125 } else {
1126 wwin->frame->core->stacking->child_of = NULL;
1130 if (!scr->focused_window) {
1131 /* first window on the list */
1132 wwin->next = NULL;
1133 wwin->prev = NULL;
1134 scr->focused_window = wwin;
1135 } else {
1136 WWindow *tmp;
1138 /* add window at beginning of focus window list */
1139 tmp = scr->focused_window;
1140 while (tmp->prev)
1141 tmp = tmp->prev;
1142 tmp->prev = wwin;
1143 wwin->next = tmp;
1144 wwin->prev = NULL;
1147 #ifdef GNOME_STUFF
1148 wGNOMEUpdateClientStateHint(wwin, True);
1149 #endif
1150 #ifdef KWM_HINTS
1151 wKWMUpdateClientWorkspace(wwin);
1152 wKWMUpdateClientStateHint(wwin, KWMAllFlags);
1153 #endif
1155 XUngrabServer(dpy);
1158 *--------------------------------------------------
1160 * Final preparations before window is ready to go
1162 *--------------------------------------------------
1165 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1168 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1169 && !wwin->flags.hidden) {
1170 if (((transientOwner && transientOwner->flags.focused)
1171 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1172 wSetFocusTo(scr, wwin);
1174 wWindowResetMouseGrabs(wwin);
1176 if (!WFLAGP(wwin, no_bind_keys)) {
1177 wWindowSetKeyGrabs(wwin);
1179 #ifdef GNOME_STUFF
1180 wGNOMEUpdateClientListHint(scr);
1181 #endif
1182 #ifdef KWM_HINTS
1183 wwin->flags.kwm_managed = 1;
1185 wKWMSendEventMessage(wwin, WKWMAddWindow);
1186 #endif
1188 wColormapInstallForWindow(scr, scr->cmap_window);
1190 UpdateSwitchMenu(scr, wwin, ACTION_ADD);
1192 #ifdef OLWM_HINTS
1193 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1194 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1196 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1197 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1198 wwin->frame_y);
1200 #endif
1203 *------------------------------------------------------------
1204 * Setup Notification Observers
1205 *------------------------------------------------------------
1207 WMAddNotificationObserver(appearanceObserver, wwin,
1208 WNWindowAppearanceSettingsChanged, wwin);
1212 *--------------------------------------------------
1214 * Cleanup temporary stuff
1216 *--------------------------------------------------
1219 if (win_state)
1220 wWindowDeleteSavedState(win_state);
1222 /* If the window must be withdrawed, then do it now.
1223 * Must do some optimization, 'though */
1224 if (withdraw) {
1225 wwin->flags.mapped = 0;
1226 wClientSetState(wwin, WithdrawnState, None);
1227 wUnmanageWindow(wwin, True, False);
1228 wwin = NULL;
1231 return wwin;
1238 WWindow*
1239 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1240 char *title, int x, int y, int width, int height)
1242 WWindow *wwin;
1243 int foo;
1245 wwin = wWindowCreate();
1247 WMAddNotificationObserver(appearanceObserver, wwin,
1248 WNWindowAppearanceSettingsChanged, wwin);
1250 wwin->flags.internal_window = 1;
1252 WSETUFLAG(wwin, omnipresent, 1);
1253 WSETUFLAG(wwin, no_shadeable, 1);
1254 WSETUFLAG(wwin, no_resizable, 1);
1255 WSETUFLAG(wwin, no_miniaturizable, 1);
1257 wwin->focus_mode = WFM_PASSIVE;
1259 wwin->client_win = window;
1260 wwin->screen_ptr = scr;
1262 wwin->transient_for = owner;
1264 wwin->client.x = x;
1265 wwin->client.y = y;
1266 wwin->client.width = width;
1267 wwin->client.height = height;
1269 wwin->frame_x = wwin->client.x;
1270 wwin->frame_y = wwin->client.y;
1273 foo = WFF_RIGHT_BUTTON;
1274 foo |= WFF_TITLEBAR;
1275 #ifdef XKB_BUTTON_HINT
1276 foo |= WFF_LANGUAGE_BUTTON;
1277 #endif
1279 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1280 wwin->frame_x, wwin->frame_y,
1281 width, height, foo,
1282 scr->window_title_texture,
1283 scr->resizebar_texture,
1284 scr->window_title_pixel,
1285 #ifdef DRAWSTRING_PLUGIN
1286 W_STRING_FTITLE,
1287 #endif
1288 &scr->window_title_gc,
1289 &scr->title_font);
1291 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1293 wwin->frame->flags.is_client_window_frame = 1;
1294 wwin->frame->flags.justification = wPreferences.title_justification;
1296 wFrameWindowChangeTitle(wwin->frame, title);
1298 /* setup button images */
1299 wWindowUpdateButtonImages(wwin);
1301 /* hide buttons */
1302 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1304 wwin->frame->child = wwin;
1306 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1308 #ifdef XKB_BUTTON_HINT
1309 if (wPreferences.modelock)
1310 wwin->frame->on_click_language = windowLanguageClick;
1311 #endif
1313 wwin->frame->on_click_right = windowCloseClick;
1315 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1316 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1318 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1320 wwin->client.y += wwin->frame->top_width;
1321 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1322 0, wwin->frame->top_width);
1324 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1325 wwin->client.width, wwin->client.height);
1327 /* setup the frame descriptor */
1328 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1329 wwin->frame->core->descriptor.parent = wwin;
1330 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1333 XLowerWindow(dpy, window);
1334 XMapSubwindows(dpy, wwin->frame->core->window);
1336 /* setup stacking descriptor */
1337 if (
1338 #ifdef removed
1339 wPreferences.on_top_transients &&
1340 #endif
1341 wwin->transient_for!=None
1342 && wwin->transient_for!=scr->root_win) {
1343 WWindow *tmp;
1344 tmp = wWindowFor(wwin->transient_for);
1345 if (tmp)
1346 wwin->frame->core->stacking->child_of = tmp->frame->core;
1347 } else {
1348 wwin->frame->core->stacking->child_of = NULL;
1352 if (!scr->focused_window) {
1353 /* first window on the list */
1354 wwin->next = NULL;
1355 wwin->prev = NULL;
1356 scr->focused_window = wwin;
1357 } else {
1358 WWindow *tmp;
1360 /* add window at beginning of focus window list */
1361 tmp = scr->focused_window;
1362 while (tmp->prev)
1363 tmp = tmp->prev;
1364 tmp->prev = wwin;
1365 wwin->next = tmp;
1366 wwin->prev = NULL;
1369 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1371 /* if (wPreferences.auto_focus)*/
1372 wSetFocusTo(scr, wwin);
1374 wWindowResetMouseGrabs(wwin);
1376 wWindowSetKeyGrabs(wwin);
1378 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1380 return wwin;
1385 *----------------------------------------------------------------------
1386 * wUnmanageWindow--
1387 * Removes the frame window from a window and destroys all data
1388 * related to it. The window will be reparented back to the root window
1389 * if restore is True.
1391 * Side effects:
1392 * Everything related to the window is destroyed and the window
1393 * is removed from the window lists. Focus is set to the previous on the
1394 * window list.
1395 *----------------------------------------------------------------------
1397 void
1398 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1400 WCoreWindow *frame = wwin->frame->core;
1401 WWindow *owner = NULL;
1402 WWindow *newFocusedWindow = NULL;
1403 int wasFocused;
1404 WScreen *scr = wwin->screen_ptr;
1407 #ifdef KWM_HINTS
1408 wwin->frame->workspace = -1;
1410 wKWMUpdateClientWorkspace(wwin);
1411 #endif
1413 /* First close attribute editor window if open */
1414 if (wwin->flags.inspector_open) {
1415 wCloseInspectorForWindow(wwin);
1418 /* Close window menu if it's open for this window */
1419 if (wwin->flags.menu_open_for_me) {
1420 CloseWindowMenu(scr);
1423 if (!destroyed) {
1424 if (!wwin->flags.internal_window)
1425 XRemoveFromSaveSet(dpy, wwin->client_win);
1427 XSelectInput(dpy, wwin->client_win, NoEventMask);
1429 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1430 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1433 XUnmapWindow(dpy, frame->window);
1435 XUnmapWindow(dpy, wwin->client_win);
1437 /* deselect window */
1438 wSelectWindow(wwin, False);
1440 /* remove all pending events on window */
1441 /* I think this only matters for autoraise */
1442 if (wPreferences.raise_delay)
1443 WMDeleteTimerWithClientData(wwin->frame->core);
1445 XFlush(dpy);
1447 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1449 /* reparent the window back to the root */
1450 if (restore)
1451 wClientRestore(wwin);
1453 if (wwin->transient_for!=scr->root_win) {
1454 owner = wWindowFor(wwin->transient_for);
1455 if (owner) {
1456 if (!owner->flags.semi_focused) {
1457 owner = NULL;
1458 } else {
1459 owner->flags.semi_focused = 0;
1464 wasFocused = wwin->flags.focused;
1466 /* remove from window focus list */
1467 if (!wwin->prev && !wwin->next) {
1468 /* was the only window */
1469 scr->focused_window = NULL;
1470 newFocusedWindow = NULL;
1471 } else {
1472 WWindow *tmp;
1474 if (wwin->prev)
1475 wwin->prev->next = wwin->next;
1476 if (wwin->next)
1477 wwin->next->prev = wwin->prev;
1478 else {
1479 scr->focused_window = wwin->prev;
1480 scr->focused_window->next = NULL;
1483 if (wPreferences.focus_mode==WKF_CLICK) {
1485 /* if in click to focus mode and the window
1486 * was a transient, focus the owner window
1488 tmp = NULL;
1489 if (wPreferences.focus_mode==WKF_CLICK) {
1490 tmp = wWindowFor(wwin->transient_for);
1491 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1492 tmp = NULL;
1495 /* otherwise, focus the next one in the focus list */
1496 if (!tmp) {
1497 tmp = scr->focused_window;
1498 while (tmp) { /* look for one in the window list first */
1499 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1500 && (tmp->flags.mapped || tmp->flags.shaded))
1501 break;
1502 tmp = tmp->prev;
1504 if (!tmp) { /* if unsuccessful, choose any focusable window */
1505 tmp = scr->focused_window;
1506 while (tmp) {
1507 if (!WFLAGP(tmp, no_focusable)
1508 && (tmp->flags.mapped || tmp->flags.shaded))
1509 break;
1510 tmp = tmp->prev;
1515 newFocusedWindow = tmp;
1517 } else if (wPreferences.focus_mode==WKF_SLOPPY
1518 || wPreferences.focus_mode==WKF_POINTER) {
1519 unsigned int mask;
1520 int foo;
1521 Window bar, win;
1523 /* This is to let the root window get the keyboard input
1524 * if Sloppy focus mode and no other window get focus.
1525 * This way keybindings will not freeze.
1527 tmp = NULL;
1528 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1529 &foo, &foo, &foo, &foo, &mask))
1530 tmp = wWindowFor(win);
1531 if (tmp == wwin)
1532 tmp = NULL;
1533 newFocusedWindow = tmp;
1534 } else {
1535 newFocusedWindow = NULL;
1539 if (!wwin->flags.internal_window) {
1540 #ifdef GNOME_STUFF
1541 wGNOMERemoveClient(wwin);
1542 #endif
1543 #ifdef KWM_HINTS
1544 wKWMSendEventMessage(wwin, WKWMRemoveWindow);
1545 #endif
1548 #ifdef DEBUG
1549 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1550 (unsigned)frame->window);
1551 #endif
1553 if (wasFocused) {
1554 if (newFocusedWindow != owner && owner) {
1555 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1557 wSetFocusTo(scr, newFocusedWindow);
1559 wWindowDestroy(wwin);
1560 XFlush(dpy);
1564 void
1565 wWindowMap(WWindow *wwin)
1567 XMapWindow(dpy, wwin->frame->core->window);
1568 if (!wwin->flags.shaded) {
1569 /* window will be remapped when getting MapNotify */
1570 XSelectInput(dpy, wwin->client_win,
1571 wwin->event_mask & ~StructureNotifyMask);
1572 XMapWindow(dpy, wwin->client_win);
1573 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1575 wwin->flags.mapped = 1;
1580 void
1581 wWindowUnmap(WWindow *wwin)
1583 wwin->flags.mapped = 0;
1585 /* prevent window withdrawal when getting UnmapNotify */
1586 XSelectInput(dpy, wwin->client_win,
1587 wwin->event_mask & ~StructureNotifyMask);
1588 XUnmapWindow(dpy, wwin->client_win);
1589 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1591 XUnmapWindow(dpy, wwin->frame->core->window);
1596 void
1597 wWindowFocus(WWindow *wwin, WWindow *owin)
1599 WWindow *nowner;
1600 WWindow *oowner;
1602 #ifdef KEEP_XKB_LOCK_STATUS
1603 if (wPreferences.modelock) {
1604 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1606 #endif /* KEEP_XKB_LOCK_STATUS */
1608 wwin->flags.semi_focused = 0;
1610 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1612 wwin->flags.focused = 1;
1614 wWindowResetMouseGrabs(wwin);
1616 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1618 if (owin == wwin || !owin)
1619 return;
1621 nowner = wWindowFor(wwin->transient_for);
1623 /* new window is a transient for the old window */
1624 if (nowner == owin) {
1625 owin->flags.semi_focused = 1;
1626 wWindowUnfocus(nowner);
1627 return;
1630 oowner = wWindowFor(owin->transient_for);
1632 /* new window is owner of old window */
1633 if (wwin == oowner) {
1634 wWindowUnfocus(owin);
1635 return;
1638 if (!nowner) {
1639 wWindowUnfocus(owin);
1640 return;
1643 /* new window has same owner of old window */
1644 if (oowner == nowner) {
1645 /* prevent unfocusing of owner */
1646 oowner->flags.semi_focused = 0;
1647 wWindowUnfocus(owin);
1648 oowner->flags.semi_focused = 1;
1650 return;
1653 /* nowner != NULL && oowner != nowner */
1654 nowner->flags.semi_focused = 1;
1655 wWindowUnfocus(nowner);
1656 wWindowUnfocus(owin);
1660 void
1661 wWindowUnfocus(WWindow *wwin)
1663 CloseWindowMenu(wwin->screen_ptr);
1665 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1666 ? WS_PFOCUSED : WS_UNFOCUSED);
1668 if (wwin->transient_for!=None
1669 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1670 WWindow *owner;
1671 owner = wWindowFor(wwin->transient_for);
1672 if (owner && owner->flags.semi_focused) {
1673 owner->flags.semi_focused = 0;
1674 if (owner->flags.mapped || owner->flags.shaded) {
1675 wWindowUnfocus(owner);
1676 wFrameWindowPaint(owner->frame);
1680 wwin->flags.focused = 0;
1682 wWindowResetMouseGrabs(wwin);
1684 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1691 *----------------------------------------------------------------------
1693 * wWindowConstrainSize--
1694 * Constrains size for the client window, taking the maximal size,
1695 * window resize increments and other size hints into account.
1697 * Returns:
1698 * The closest size to what was given that the client window can
1699 * have.
1701 *----------------------------------------------------------------------
1703 void
1704 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1706 int width = *nwidth;
1707 int height = *nheight;
1708 int winc = 1;
1709 int hinc = 1;
1710 int minW = 1, minH = 1;
1711 int maxW = wwin->screen_ptr->scr_width*2;
1712 int maxH = wwin->screen_ptr->scr_height*2;
1713 int minAX = -1, minAY = -1;
1714 int maxAX = -1, maxAY = -1;
1715 int baseW = 0;
1716 int baseH = 0;
1718 if (wwin->normal_hints) {
1719 winc = wwin->normal_hints->width_inc;
1720 hinc = wwin->normal_hints->height_inc;
1721 minW = wwin->normal_hints->min_width;
1722 minH = wwin->normal_hints->min_height;
1723 maxW = wwin->normal_hints->max_width;
1724 maxH = wwin->normal_hints->max_height;
1725 if (wwin->normal_hints->flags & PAspect) {
1726 minAX = wwin->normal_hints->min_aspect.x;
1727 minAY = wwin->normal_hints->min_aspect.y;
1728 maxAX = wwin->normal_hints->max_aspect.x;
1729 maxAY = wwin->normal_hints->max_aspect.y;
1733 if (width < minW)
1734 width = minW;
1735 if (height < minH)
1736 height = minH;
1738 if (width > maxW)
1739 width = maxW;
1740 if (height > maxH)
1741 height = maxH;
1743 /* aspect ratio code borrowed from olwm */
1744 if (minAX > 0) {
1745 /* adjust max aspect ratio */
1746 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1747 if (maxAX > maxAY) {
1748 height = (width * maxAY) / maxAX;
1749 if (height > maxH) {
1750 height = maxH;
1751 width = (height * maxAX) / maxAY;
1753 } else {
1754 width = (height * maxAX) / maxAY;
1755 if (width > maxW) {
1756 width = maxW;
1757 height = (width * maxAY) / maxAX;
1762 /* adjust min aspect ratio */
1763 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1764 if (minAX > minAY) {
1765 height = (width * minAY) / minAX;
1766 if (height < minH) {
1767 height = minH;
1768 width = (height * minAX) / minAY;
1770 } else {
1771 width = (height * minAX) / minAY;
1772 if (width < minW) {
1773 width = minW;
1774 height = (width * minAY) / minAX;
1780 if (baseW != 0) {
1781 width = (((width - baseW) / winc) * winc) + baseW;
1782 } else {
1783 width = (((width - minW) / winc) * winc) + minW;
1786 if (baseW != 0) {
1787 height = (((height - baseH) / hinc) * hinc) + baseH;
1788 } else {
1789 height = (((height - minH) / hinc) * hinc) + minH;
1792 *nwidth = width;
1793 *nheight = height;
1797 void
1798 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1800 WScreen *scr = wwin->screen_ptr;
1801 WApplication *wapp;
1802 int unmap = 0;
1804 if (workspace >= scr->workspace_count || workspace < 0
1805 || workspace == wwin->frame->workspace)
1806 return;
1808 if (workspace != scr->current_workspace) {
1809 /* Sent to other workspace. Unmap window */
1810 if ((wwin->flags.mapped
1811 || wwin->flags.shaded
1812 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1813 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1815 wapp = wApplicationOf(wwin->main_window);
1816 if (wapp) {
1817 wapp->last_workspace = workspace;
1819 if (wwin->flags.miniaturized) {
1820 if (wwin->icon) {
1821 XUnmapWindow(dpy, wwin->icon->core->window);
1822 wwin->icon->mapped = 0;
1824 } else {
1825 unmap = 1;
1826 wSetFocusTo(scr, NULL);
1829 } else {
1830 /* brought to current workspace. Map window */
1831 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1832 if (wwin->icon) {
1833 XMapWindow(dpy, wwin->icon->core->window);
1834 wwin->icon->mapped = 1;
1836 } else if (!wwin->flags.mapped &&
1837 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1838 wWindowMap(wwin);
1841 if (!IS_OMNIPRESENT(wwin)) {
1842 wwin->frame->workspace = workspace;
1843 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1845 #ifdef GNOME_STUFF
1846 wGNOMEUpdateClientStateHint(wwin, True);
1847 #endif
1848 #ifdef KWM_HINTS
1849 wKWMUpdateClientWorkspace(wwin);
1850 wKWMSendEventMessage(wwin, WKWMChangedClient);
1851 #endif
1852 if (unmap) {
1853 wWindowUnmap(wwin);
1858 void
1859 wWindowSynthConfigureNotify(WWindow *wwin)
1861 XEvent sevent;
1863 sevent.type = ConfigureNotify;
1864 sevent.xconfigure.display = dpy;
1865 sevent.xconfigure.event = wwin->client_win;
1866 sevent.xconfigure.window = wwin->client_win;
1868 sevent.xconfigure.x = wwin->client.x;
1869 sevent.xconfigure.y = wwin->client.y;
1870 sevent.xconfigure.width = wwin->client.width;
1871 sevent.xconfigure.height = wwin->client.height;
1873 sevent.xconfigure.border_width = wwin->old_border_width;
1874 if (WFLAGP(wwin, no_titlebar))
1875 sevent.xconfigure.above = None;
1876 else
1877 sevent.xconfigure.above = wwin->frame->titlebar->window;
1879 sevent.xconfigure.override_redirect = False;
1880 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1881 #ifdef KWM_HINTS
1882 wKWMSendEventMessage(wwin, WKWMChangedClient);
1883 #endif
1884 XFlush(dpy);
1889 *----------------------------------------------------------------------
1890 * wWindowConfigure--
1891 * Configures the frame, decorations and client window to the
1892 * specified geometry. The geometry is not checked for validity,
1893 * wWindowConstrainSize() must be used for that.
1894 * The size parameters are for the client window, but the position is
1895 * for the frame.
1896 * The client window receives a ConfigureNotify event, according
1897 * to what ICCCM says.
1899 * Returns:
1900 * None
1902 * Side effects:
1903 * Window size and position are changed and client window receives
1904 * a ConfigureNotify event.
1905 *----------------------------------------------------------------------
1907 void
1908 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1909 WWindow *wwin;
1910 int req_x, req_y; /* new position of the frame */
1911 int req_width, req_height; /* new size of the client */
1913 int synth_notify = False;
1914 int resize;
1916 resize = (req_width!=wwin->client.width
1917 || req_height!=wwin->client.height);
1919 * if the window is being moved but not resized then
1920 * send a synthetic ConfigureNotify
1922 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1923 synth_notify = True;
1926 if (WFLAGP(wwin, dont_move_off))
1927 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1928 req_width, req_height);
1929 if (resize) {
1930 if (req_width < MIN_WINDOW_SIZE)
1931 req_width = MIN_WINDOW_SIZE;
1932 if (req_height < MIN_WINDOW_SIZE)
1933 req_height = MIN_WINDOW_SIZE;
1935 /* If growing, resize inner part before frame,
1936 * if shrinking, resize frame before.
1937 * This will prevent the frame (that can have a different color)
1938 * to be exposed, causing flicker */
1939 if (req_height > wwin->frame->core->height
1940 || req_width > wwin->frame->core->width)
1941 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1943 if (wwin->flags.shaded) {
1944 wFrameWindowConfigure(wwin->frame, req_x, req_y,
1945 req_width, wwin->frame->core->height);
1946 wwin->old_geometry.height = req_height;
1947 } else {
1948 int h;
1950 h = req_height + wwin->frame->top_width
1951 + wwin->frame->bottom_width;
1953 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
1956 if (!(req_height > wwin->frame->core->height
1957 || req_width > wwin->frame->core->width))
1958 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1960 wwin->client.x = req_x;
1961 wwin->client.y = req_y + wwin->frame->top_width;
1962 wwin->client.width = req_width;
1963 wwin->client.height = req_height;
1964 } else {
1965 wwin->client.x = req_x;
1966 wwin->client.y = req_y + wwin->frame->top_width;
1968 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1970 wwin->frame_x = req_x;
1971 wwin->frame_y = req_y;
1973 #ifdef SHAPE
1974 if (wShapeSupported && wwin->flags.shaped && resize) {
1975 wWindowSetShape(wwin);
1977 #endif
1979 if (synth_notify)
1980 wWindowSynthConfigureNotify(wwin);
1981 XFlush(dpy);
1985 void
1986 wWindowMove(wwin, req_x, req_y)
1987 WWindow *wwin;
1988 int req_x, req_y; /* new position of the frame */
1990 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1991 int synth_notify = False;
1993 /* Send a synthetic ConfigureNotify event for every window movement. */
1994 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1995 synth_notify = True;
1997 #else
1998 /* A single synthetic ConfigureNotify event is sent at the end of
1999 * a completed (opaque) movement in moveres.c */
2000 #endif
2002 if (WFLAGP(wwin, dont_move_off))
2003 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2004 wwin->frame->core->width, wwin->frame->core->height);
2006 wwin->client.x = req_x;
2007 wwin->client.y = req_y + wwin->frame->top_width;
2009 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2011 wwin->frame_x = req_x;
2012 wwin->frame_y = req_y;
2014 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2015 if (synth_notify)
2016 wWindowSynthConfigureNotify(wwin);
2017 #endif
2021 void
2022 wWindowUpdateButtonImages(WWindow *wwin)
2024 WScreen *scr = wwin->screen_ptr;
2025 Pixmap pixmap, mask;
2026 WFrameWindow *fwin = wwin->frame;
2028 if (WFLAGP(wwin, no_titlebar))
2029 return;
2031 /* miniaturize button */
2033 if (!WFLAGP(wwin, no_miniaturize_button)) {
2034 if (wwin->wm_gnustep_attr
2035 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2036 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2038 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2039 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2040 } else {
2041 mask = None;
2044 if (fwin->lbutton_image
2045 && (fwin->lbutton_image->image != pixmap
2046 || fwin->lbutton_image->mask != mask)) {
2047 wPixmapDestroy(fwin->lbutton_image);
2048 fwin->lbutton_image = NULL;
2051 if (!fwin->lbutton_image) {
2052 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2053 fwin->lbutton_image->client_owned = 1;
2054 fwin->lbutton_image->client_owned_mask = 1;
2056 } else {
2057 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2058 wPixmapDestroy(fwin->lbutton_image);
2060 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2064 #ifdef XKB_BUTTON_HINT
2065 if (!WFLAGP(wwin, no_language_button)) {
2066 if (fwin->languagebutton_image &&
2067 !fwin->languagebutton_image->shared) {
2068 wPixmapDestroy(fwin->languagebutton_image);
2070 fwin->languagebutton_image =
2071 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2073 #endif
2075 /* close button */
2077 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2078 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2080 if (!WFLAGP(wwin, no_close_button)) {
2081 if (wwin->wm_gnustep_attr
2082 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2083 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2085 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2086 mask = wwin->wm_gnustep_attr->close_mask;
2087 else
2088 mask = None;
2090 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2091 || fwin->rbutton_image->mask != mask)) {
2092 wPixmapDestroy(fwin->rbutton_image);
2093 fwin->rbutton_image = NULL;
2096 if (!fwin->rbutton_image) {
2097 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2098 fwin->rbutton_image->client_owned = 1;
2099 fwin->rbutton_image->client_owned_mask = 1;
2102 } else if (WFLAGP(wwin, kill_close)) {
2104 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2105 wPixmapDestroy(fwin->rbutton_image);
2107 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2109 } else if (MGFLAGP(wwin, broken_close)) {
2111 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2112 wPixmapDestroy(fwin->rbutton_image);
2114 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2116 } else {
2118 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2119 wPixmapDestroy(fwin->rbutton_image);
2121 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2125 /* force buttons to be redrawn */
2126 fwin->flags.need_texture_change = 1;
2127 wFrameWindowPaint(fwin);
2132 *---------------------------------------------------------------------------
2133 * wWindowConfigureBorders--
2134 * Update window border configuration according to attribute flags.
2136 *---------------------------------------------------------------------------
2138 void
2139 wWindowConfigureBorders(WWindow *wwin)
2141 if (wwin->frame) {
2142 int flags;
2143 int newy, oldh;
2145 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2146 #ifdef XKB_BUTTON_HINT
2147 flags |= WFF_LANGUAGE_BUTTON;
2148 #endif
2149 if (!WFLAGP(wwin, no_titlebar))
2150 flags |= WFF_TITLEBAR;
2151 if (!WFLAGP(wwin, no_resizebar))
2152 flags |= WFF_RESIZEBAR;
2153 if (wwin->flags.shaded)
2154 flags |= WFF_IS_SHADED;
2156 oldh = wwin->frame->top_width;
2157 wFrameWindowUpdateBorders(wwin->frame, flags);
2158 if (oldh != wwin->frame->top_width) {
2159 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2161 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2162 wWindowConfigure(wwin, wwin->frame_x, newy,
2163 wwin->client.width, wwin->client.height);
2166 flags = 0;
2167 if (!WFLAGP(wwin, no_miniaturize_button)
2168 && wwin->frame->flags.hide_left_button)
2169 flags |= WFF_LEFT_BUTTON;
2171 #ifdef XKB_BUTTON_HINT
2172 if (!WFLAGP(wwin, no_language_button)
2173 && wwin->frame->flags.hide_language_button)
2174 flags |= WFF_LANGUAGE_BUTTON;
2175 #endif
2177 if (!WFLAGP(wwin, no_close_button)
2178 && wwin->frame->flags.hide_right_button)
2179 flags |= WFF_RIGHT_BUTTON;
2181 if (flags!=0) {
2182 wWindowUpdateButtonImages(wwin);
2183 wFrameWindowShowButton(wwin->frame, flags);
2186 flags = 0;
2187 if (WFLAGP(wwin, no_miniaturize_button)
2188 && !wwin->frame->flags.hide_left_button)
2189 flags |= WFF_LEFT_BUTTON;
2191 #ifdef XKB_BUTTON_HINT
2192 if (WFLAGP(wwin, no_language_button)
2193 && !wwin->frame->flags.hide_language_button)
2194 flags |= WFF_LANGUAGE_BUTTON;
2195 #endif
2197 if (WFLAGP(wwin, no_close_button)
2198 && !wwin->frame->flags.hide_right_button)
2199 flags |= WFF_RIGHT_BUTTON;
2201 if (flags!=0)
2202 wFrameWindowHideButton(wwin->frame, flags);
2204 #ifdef SHAPE
2205 if (wShapeSupported && wwin->flags.shaped) {
2206 wWindowSetShape(wwin);
2208 #endif
2213 void
2214 wWindowSaveState(WWindow *wwin)
2216 CARD32 data[10];
2217 int i;
2219 memset(data, 0, sizeof(CARD32)*10);
2220 data[0] = wwin->frame->workspace;
2221 data[1] = wwin->flags.miniaturized;
2222 data[2] = wwin->flags.shaded;
2223 data[3] = wwin->flags.hidden;
2225 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2226 if (wwin->screen_ptr->shortcutWindows[i] &&
2227 WMCountInBag(wwin->screen_ptr->shortcutWindows[i], wwin))
2228 data[9] |= 1<<i;
2230 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2231 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2232 (unsigned char *)data, 10);
2236 static int
2237 getSavedState(Window window, WSavedState **state)
2239 Atom type_ret;
2240 int fmt_ret;
2241 unsigned long nitems_ret;
2242 unsigned long bytes_after_ret;
2243 CARD32 *data;
2245 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2246 True, _XA_WINDOWMAKER_STATE,
2247 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2248 (unsigned char **)&data)!=Success || !data)
2249 return 0;
2251 *state = malloc(sizeof(WSavedState));
2253 if (*state) {
2254 (*state)->workspace = data[0];
2255 (*state)->miniaturized = data[1];
2256 (*state)->shaded = data[2];
2257 (*state)->hidden = data[3];
2258 (*state)->use_geometry = data[4];
2259 (*state)->x = data[5];
2260 (*state)->y = data[6];
2261 (*state)->w = data[7];
2262 (*state)->h = data[8];
2263 (*state)->window_shortcuts = data[9];
2265 XFree(data);
2267 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2268 return 1;
2269 else
2270 return 0;
2274 #ifdef SHAPE
2275 void
2276 wWindowClearShape(WWindow *wwin)
2278 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2279 0, wwin->frame->top_width, None, ShapeSet);
2280 XFlush(dpy);
2283 void
2284 wWindowSetShape(WWindow *wwin)
2286 XRectangle rect[2];
2287 int count;
2288 #ifdef OPTIMIZE_SHAPE
2289 XRectangle *rects;
2290 XRectangle *urec;
2291 int ordering;
2293 /* only shape is the client's */
2294 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2295 goto alt_code;
2298 /* Get array of rectangles describing the shape mask */
2299 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2300 &count, &ordering);
2301 if (!rects) {
2302 goto alt_code;
2305 urec = malloc(sizeof(XRectangle)*(count+2));
2306 if (!urec) {
2307 XFree(rects);
2308 goto alt_code;
2311 /* insert our decoration rectangles in the rect list */
2312 memcpy(urec, rects, sizeof(XRectangle)*count);
2313 XFree(rects);
2315 if (!WFLAGP(wwin, no_titlebar)) {
2316 urec[count].x = -1;
2317 urec[count].y = -1 - wwin->frame->top_width;
2318 urec[count].width = wwin->frame->core->width + 2;
2319 urec[count].height = wwin->frame->top_width + 1;
2320 count++;
2322 if (!WFLAGP(wwin, no_resizebar)) {
2323 urec[count].x = -1;
2324 urec[count].y = wwin->frame->core->height
2325 - wwin->frame->bottom_width - wwin->frame->top_width;
2326 urec[count].width = wwin->frame->core->width + 2;
2327 urec[count].height = wwin->frame->bottom_width + 1;
2328 count++;
2331 /* shape our frame window */
2332 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2333 0, wwin->frame->top_width, urec, count,
2334 ShapeSet, Unsorted);
2335 XFlush(dpy);
2336 free(urec);
2337 return;
2339 alt_code:
2340 #endif /* OPTIMIZE_SHAPE */
2341 count = 0;
2342 if (!WFLAGP(wwin, no_titlebar)) {
2343 rect[count].x = -1;
2344 rect[count].y = -1;
2345 rect[count].width = wwin->frame->core->width + 2;
2346 rect[count].height = wwin->frame->top_width + 1;
2347 count++;
2349 if (!WFLAGP(wwin, no_resizebar)) {
2350 rect[count].x = -1;
2351 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2352 rect[count].width = wwin->frame->core->width + 2;
2353 rect[count].height = wwin->frame->bottom_width + 1;
2354 count++;
2356 if (count > 0) {
2357 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2358 0, 0, rect, count, ShapeSet, Unsorted);
2360 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2361 0, wwin->frame->top_width, wwin->client_win,
2362 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2363 XFlush(dpy);
2365 #endif /* SHAPE */
2367 /* ====================================================================== */
2369 static FocusMode
2370 getFocusMode(WWindow *wwin)
2372 FocusMode mode;
2374 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2375 if (wwin->wm_hints->input == True) {
2376 if (wwin->protocols.TAKE_FOCUS)
2377 mode = WFM_LOCALLY_ACTIVE;
2378 else
2379 mode = WFM_PASSIVE;
2380 } else {
2381 if (wwin->protocols.TAKE_FOCUS)
2382 mode = WFM_GLOBALLY_ACTIVE;
2383 else
2384 mode = WFM_NO_INPUT;
2386 } else {
2387 mode = WFM_PASSIVE;
2389 return mode;
2393 void
2394 wWindowSetKeyGrabs(WWindow *wwin)
2396 int i;
2397 WShortKey *key;
2399 for (i=0; i<WKBD_LAST; i++) {
2400 key = &wKeyBindings[i];
2402 if (key->keycode==0)
2403 continue;
2404 if (key->modifier!=AnyModifier) {
2405 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2406 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2407 #ifdef NUMLOCK_HACK
2408 /* Also grab all modifier combinations possible that include,
2409 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2410 * work even if the NumLock/ScrollLock key is on.
2412 wHackedGrabKey(key->keycode, key->modifier,
2413 wwin->frame->core->window, True, GrabModeAsync,
2414 GrabModeAsync);
2415 #endif
2417 XGrabKey(dpy, key->keycode, key->modifier,
2418 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2421 #ifndef LITE
2422 wRootMenuBindShortcuts(wwin->frame->core->window);
2423 #endif
2428 void
2429 wWindowResetMouseGrabs(WWindow *wwin)
2431 /* Mouse grabs can't be done on the client window because of
2432 * ICCCM and because clients that try to do the same will crash.
2434 * But there is a problem wich makes tbar buttons of unfocused
2435 * windows not usable as the click goes to the frame window instead
2436 * of the button itself. Must figure a way to fix that.
2439 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2441 if (!WFLAGP(wwin, no_bind_mouse)) {
2442 /* grabs for Meta+drag */
2443 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2444 True, ButtonPressMask, GrabModeSync,
2445 GrabModeAsync, None, None);
2448 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)) {
2449 /* the passive grabs to focus the window */
2450 if (wPreferences.focus_mode == WKF_CLICK)
2451 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2452 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2453 None, None);
2455 XFlush(dpy);
2459 void
2460 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2462 if (attr->flags & GSExtraFlagsAttr) {
2463 if (MGFLAGP(wwin, broken_close) !=
2464 (attr->extra_flags & GSDocumentEditedFlag)) {
2465 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2466 wWindowUpdateButtonImages(wwin);
2472 WMagicNumber
2473 wWindowAddSavedState(char *instance, char *class, char *command,
2474 pid_t pid, WSavedState *state)
2476 WWindowState *wstate;
2478 wstate = malloc(sizeof(WWindowState));
2479 if (!wstate)
2480 return 0;
2482 memset(wstate, 0, sizeof(WWindowState));
2483 wstate->pid = pid;
2484 if (instance)
2485 wstate->instance = wstrdup(instance);
2486 if (class)
2487 wstate->class = wstrdup(class);
2488 if (command)
2489 wstate->command = wstrdup(command);
2490 wstate->state = state;
2492 wstate->next = windowState;
2493 windowState = wstate;
2495 #ifdef DEBUG
2496 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2497 class, command);
2498 #endif
2500 return wstate;
2504 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2507 WMagicNumber
2508 wWindowGetSavedState(Window win)
2510 char *instance, *class, *command=NULL;
2511 WWindowState *wstate = windowState;
2512 char **argv;
2513 int argc;
2515 if (!wstate)
2516 return NULL;
2518 if (XGetCommand(dpy, win, &argv, &argc)) {
2519 if (argc > 0)
2520 command = FlattenStringList(argv, argc);
2521 XFreeStringList(argv);
2523 if (!command)
2524 return NULL;
2526 if (PropGetWMClass(win, &class, &instance)) {
2527 while (wstate) {
2528 if (SAME(instance, wstate->instance) &&
2529 SAME(class, wstate->class) &&
2530 SAME(command, wstate->command)) {
2531 break;
2533 wstate = wstate->next;
2535 } else {
2536 wstate = NULL;
2539 #ifdef DEBUG
2540 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2541 class, command);
2542 #endif
2544 if (command) free(command);
2545 if (instance) XFree(instance);
2546 if (class) XFree(class);
2548 return wstate;
2552 void
2553 wWindowDeleteSavedState(WMagicNumber id)
2555 WWindowState *tmp, *wstate=(WWindowState*)id;
2557 if (!wstate || !windowState)
2558 return;
2560 tmp = windowState;
2561 if (tmp==wstate) {
2562 windowState = wstate->next;
2563 #ifdef DEBUG
2564 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2565 wstate, wstate->instance, wstate->class, wstate->command);
2566 #endif
2567 if (wstate->instance) free(wstate->instance);
2568 if (wstate->class) free(wstate->class);
2569 if (wstate->command) free(wstate->command);
2570 free(wstate->state);
2571 free(wstate);
2572 } else {
2573 while (tmp->next) {
2574 if (tmp->next==wstate) {
2575 tmp->next=wstate->next;
2576 #ifdef DEBUG
2577 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2578 wstate, wstate->instance, wstate->class, wstate->command);
2579 #endif
2580 if (wstate->instance) free(wstate->instance);
2581 if (wstate->class) free(wstate->class);
2582 if (wstate->command) free(wstate->command);
2583 free(wstate->state);
2584 free(wstate);
2585 break;
2587 tmp = tmp->next;
2593 void
2594 wWindowDeleteSavedStatesForPID(pid_t pid)
2596 WWindowState *tmp, *wstate;
2598 if (!windowState)
2599 return;
2601 tmp = windowState;
2602 if (tmp->pid == pid) {
2603 wstate = windowState;
2604 windowState = tmp->next;
2605 #ifdef DEBUG
2606 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2607 wstate, wstate->instance, wstate->class, wstate->command);
2608 #endif
2609 if (wstate->instance) free(wstate->instance);
2610 if (wstate->class) free(wstate->class);
2611 if (wstate->command) free(wstate->command);
2612 free(wstate->state);
2613 free(wstate);
2614 } else {
2615 while (tmp->next) {
2616 if (tmp->next->pid==pid) {
2617 wstate = tmp->next;
2618 tmp->next = wstate->next;
2619 #ifdef DEBUG
2620 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2621 wstate, wstate->instance, wstate->class, wstate->command);
2622 #endif
2623 if (wstate->instance) free(wstate->instance);
2624 if (wstate->class) free(wstate->class);
2625 if (wstate->command) free(wstate->command);
2626 free(wstate->state);
2627 free(wstate);
2628 break;
2630 tmp = tmp->next;
2636 /* ====================================================================== */
2638 static void
2639 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2641 WWindow *wwin = data;
2643 #ifndef NUMLOCK_HACK
2644 if ((event->xbutton.state & ValidModMask)
2645 != (event->xbutton.state & ~LockMask)) {
2646 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2647 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2649 #endif
2651 event->xbutton.state &= ValidModMask;
2653 CloseWindowMenu(wwin->screen_ptr);
2655 if (wPreferences.focus_mode==WKF_CLICK
2656 && !(event->xbutton.state&ControlMask)
2657 && !WFLAGP(wwin, no_focusable)) {
2658 wSetFocusTo(wwin->screen_ptr, wwin);
2661 if (event->xbutton.button == Button1)
2662 wRaiseFrame(wwin->frame->core);
2664 if (event->xbutton.window != wwin->frame->resizebar->window) {
2665 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2666 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2667 GrabModeAsync, GrabModeAsync, None,
2668 None, CurrentTime)!=GrabSuccess) {
2669 #ifdef DEBUG0
2670 wwarning("pointer grab failed for window move");
2671 #endif
2672 return;
2676 if (event->xbutton.state & MOD_MASK) {
2677 /* move the window */
2678 wMouseMoveWindow(wwin, event);
2679 XUngrabPointer(dpy, CurrentTime);
2680 } else {
2681 wMouseResizeWindow(wwin, event);
2682 XUngrabPointer(dpy, CurrentTime);
2688 static void
2689 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2691 WWindow *wwin = data;
2693 event->xbutton.state &= ValidModMask;
2695 if (event->xbutton.button==Button1) {
2696 if (event->xbutton.state == 0) {
2697 if (!WFLAGP(wwin, no_shadeable)) {
2698 /* shade window */
2699 if (wwin->flags.shaded)
2700 wUnshadeWindow(wwin);
2701 else
2702 wShadeWindow(wwin);
2704 } else {
2705 int dir = 0;
2707 if (event->xbutton.state & ControlMask)
2708 dir |= MAX_VERTICAL;
2710 if (event->xbutton.state & ShiftMask) {
2711 dir |= MAX_HORIZONTAL;
2712 if (!(event->xbutton.state & ControlMask))
2713 wSelectWindow(wwin, !wwin->flags.selected);
2716 /* maximize window */
2717 if (dir !=0 && !WFLAGP(wwin, no_resizable)) {
2718 if (wwin->flags.maximized)
2719 wUnmaximizeWindow(wwin);
2720 else
2721 wMaximizeWindow(wwin, dir);
2724 } else if (event->xbutton.button==Button3) {
2725 if (event->xbutton.state & MOD_MASK) {
2726 wHideOtherApplications(wwin);
2728 } else if (event->xbutton.button==Button2) {
2729 wSelectWindow(wwin, !wwin->flags.selected);
2734 static void
2735 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2737 WWindow *wwin = desc->parent;
2739 event->xbutton.state &= ValidModMask;
2741 CloseWindowMenu(wwin->screen_ptr);
2743 if (wPreferences.focus_mode==WKF_CLICK
2744 && !(event->xbutton.state&ControlMask)
2745 && !WFLAGP(wwin, no_focusable)) {
2746 wSetFocusTo(wwin->screen_ptr, wwin);
2748 if (event->xbutton.button == Button1) {
2749 wRaiseFrame(wwin->frame->core);
2752 if (event->xbutton.state & MOD_MASK) {
2753 /* move the window */
2754 if (XGrabPointer(dpy, wwin->client_win, False,
2755 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2756 GrabModeAsync, GrabModeAsync, None,
2757 None, CurrentTime)!=GrabSuccess) {
2758 #ifdef DEBUG0
2759 wwarning("pointer grab failed for window move");
2760 #endif
2761 return;
2763 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
2764 wMouseResizeWindow(wwin, event);
2765 else
2766 wMouseMoveWindow(wwin, event);
2767 XUngrabPointer(dpy, CurrentTime);
2772 static void
2773 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2775 WWindow *wwin = (WWindow*)data;
2777 #ifndef NUMLOCK_HACK
2778 if ((event->xbutton.state & ValidModMask)
2779 != (event->xbutton.state & ~LockMask)) {
2780 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2781 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2783 #endif
2784 event->xbutton.state &= ValidModMask;
2786 CloseWindowMenu(wwin->screen_ptr);
2788 if (wPreferences.focus_mode==WKF_CLICK
2789 && !(event->xbutton.state&ControlMask)
2790 && !WFLAGP(wwin, no_focusable)) {
2791 wSetFocusTo(wwin->screen_ptr, wwin);
2794 if (event->xbutton.button == Button1
2795 || event->xbutton.button == Button2) {
2797 if (event->xbutton.button == Button1) {
2798 if (event->xbutton.state & MOD_MASK) {
2799 wLowerFrame(wwin->frame->core);
2800 } else {
2801 wRaiseFrame(wwin->frame->core);
2804 if ((event->xbutton.state & ShiftMask)
2805 && !(event->xbutton.state & ControlMask)) {
2806 wSelectWindow(wwin, !wwin->flags.selected);
2807 return;
2809 if (event->xbutton.window != wwin->frame->titlebar->window
2810 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2811 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2812 GrabModeAsync, GrabModeAsync, None,
2813 None, CurrentTime)!=GrabSuccess) {
2814 #ifdef DEBUG0
2815 wwarning("pointer grab failed for window move");
2816 #endif
2817 return;
2820 /* move the window */
2821 wMouseMoveWindow(wwin, event);
2823 XUngrabPointer(dpy, CurrentTime);
2824 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2825 && !wwin->flags.internal_window
2826 && !WCHECK_STATE(WSTATE_MODAL)) {
2827 WObjDescriptor *desc;
2829 if (event->xbutton.window != wwin->frame->titlebar->window
2830 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2831 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2832 GrabModeAsync, GrabModeAsync, None,
2833 None, CurrentTime)!=GrabSuccess) {
2834 #ifdef DEBUG0
2835 wwarning("pointer grab failed for window move");
2836 #endif
2837 return;
2840 OpenWindowMenu(wwin, event->xbutton.x_root,
2841 wwin->frame_y+wwin->frame->top_width, False);
2843 /* allow drag select */
2844 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2845 event->xany.send_event = True;
2846 (*desc->handle_mousedown)(desc, event);
2848 XUngrabPointer(dpy, CurrentTime);
2854 static void
2855 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2857 WWindow *wwin = data;
2859 event->xbutton.state &= ValidModMask;
2861 CloseWindowMenu(wwin->screen_ptr);
2863 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2864 return;
2866 /* if control-click, kill the client */
2867 if (event->xbutton.state & ControlMask) {
2868 wClientKill(wwin);
2869 } else {
2870 #ifdef OLWM_HINTS
2871 if (wwin->flags.olwm_push_pin_out) {
2873 wwin->flags.olwm_push_pin_out = 0;
2875 wOLWMChangePushpinState(wwin, True);
2877 wFrameWindowUpdatePushButton(wwin->frame, False);
2879 return;
2881 #endif
2882 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2883 /* send delete message */
2884 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2890 static void
2891 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2893 WWindow *wwin = data;
2895 CloseWindowMenu(wwin->screen_ptr);
2897 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2898 return;
2900 /* send delete message */
2901 if (wwin->protocols.DELETE_WINDOW) {
2902 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2903 } else {
2904 wClientKill(wwin);
2909 #ifdef XKB_BUTTON_HINT
2910 static void
2911 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2913 WWindow *wwin = data;
2914 WFrameWindow *fwin = wwin->frame;
2915 WScreen *scr = fwin->screen_ptr;
2916 XkbStateRec staterec;
2917 int tl;
2919 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2920 return;
2921 tl = wwin->frame->languagemode;
2922 wwin->frame->languagemode = wwin->frame->last_languagemode;
2923 wwin->frame->last_languagemode = tl;
2924 wSetFocusTo(scr, wwin);
2925 wwin->frame->languagebutton_image =
2926 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
2927 wwin->frame->languagemode];
2928 wFrameWindowUpdateLanguageButton(wwin->frame);
2929 if (event->xbutton.button == Button3)
2930 return;
2931 wRaiseFrame(fwin->core);
2933 #endif
2936 static void
2937 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2939 WWindow *wwin = data;
2941 event->xbutton.state &= ValidModMask;
2943 CloseWindowMenu(wwin->screen_ptr);
2945 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2946 return;
2948 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2949 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2950 LastTimestamp);
2951 } else {
2952 WApplication *wapp;
2953 if ((event->xbutton.state & ControlMask) ||
2954 (event->xbutton.button == Button3)) {
2956 wapp = wApplicationOf(wwin->main_window);
2957 if (wapp && !WFLAGP(wwin, no_appicon))
2958 wHideApplication(wapp);
2959 } else if (event->xbutton.state==0) {
2960 wIconifyWindow(wwin);