Fixed issue with GNUstep applications losing focus when all their windows
[wmaker-crm.git] / src / window.c
blob1a82549abf21a470a270d050aecc54e00c9179db
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
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>
37 /* For getting mouse wheel mappings from WINGs */
38 #include <WINGs/WINGsP.h>
40 #include "WindowMaker.h"
41 #include "GNUstep.h"
42 #include "wcore.h"
43 #include "framewin.h"
44 #include "texture.h"
45 #include "window.h"
46 #include "winspector.h"
47 #include "icon.h"
48 #include "properties.h"
49 #include "actions.h"
50 #include "client.h"
51 #include "funcs.h"
52 #include "keybind.h"
53 #include "stacking.h"
54 #include "defaults.h"
55 #include "workspace.h"
56 #include "xinerama.h"
59 #ifdef MWM_HINTS
60 # include "motif.h"
61 #endif
62 #ifdef NETWM_HINTS
63 # include "wmspec.h"
64 #endif
66 /****** Global Variables ******/
68 extern WShortKey wKeyBindings[WKBD_LAST];
70 #ifdef SHAPE
71 extern Bool wShapeSupported;
72 #endif
74 /* contexts */
75 extern XContext wWinContext;
77 /* cursors */
78 extern Cursor wCursor[WCUR_LAST];
80 /* protocol atoms */
81 extern Atom _XA_WM_DELETE_WINDOW;
82 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
84 extern Atom _XA_WINDOWMAKER_STATE;
86 extern WPreferences wPreferences;
88 #define MOD_MASK wPreferences.modifier_mask
90 extern Time LastTimestamp;
92 /* superfluous... */
93 extern void DoWindowBirth(WWindow*);
97 /***** Local Stuff *****/
100 static WWindowState *windowState=NULL;
104 /* local functions */
105 static FocusMode getFocusMode(WWindow *wwin);
107 static int getSavedState(Window window, WSavedState **state);
109 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
111 /* event handlers */
114 /* frame window (during window grabs) */
115 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
117 /* close button */
118 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
119 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
121 /* iconify button */
122 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
124 #ifdef XKB_BUTTON_HINT
125 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
126 #endif
128 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
129 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
131 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
134 /****** Notification Observers ******/
136 static void
137 appearanceObserver(void *self, WMNotification *notif)
139 WWindow *wwin = (WWindow*)self;
140 int flags = (int)WMGetNotificationClientData(notif);
142 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
143 return;
145 if (flags & WFontSettings) {
146 wWindowConfigureBorders(wwin);
147 if(wwin->flags.shaded) {
148 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
149 wwin->frame->top_width - 1);
151 wwin->client.y = wwin->frame_y - wwin->client.height
152 + wwin->frame->top_width;
153 wWindowSynthConfigureNotify(wwin);
156 if (flags & WTextureSettings) {
157 wwin->frame->flags.need_texture_remake = 1;
159 if (flags & (WTextureSettings | WColorSettings)) {
160 if (wwin->frame->titlebar)
161 XClearWindow(dpy, wwin->frame->titlebar->window);
163 wFrameWindowPaint(wwin->frame);
167 /************************************/
169 WWindow*
170 wWindowFor(Window window)
172 WObjDescriptor *desc;
174 if (window==None)
175 return NULL;
177 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
178 return NULL;
180 if (desc->parent_type==WCLASS_WINDOW)
181 return desc->parent;
182 else if (desc->parent_type==WCLASS_FRAME) {
183 WFrameWindow *frame = (WFrameWindow*)desc->parent;
184 if (frame->flags.is_client_window_frame)
185 return frame->child;
188 return NULL;
192 WWindow*
193 wWindowCreate()
195 WWindow *wwin;
197 wwin = wmalloc(sizeof(WWindow));
198 wretain(wwin);
200 memset(wwin, 0, sizeof(WWindow));
202 wwin->client_descriptor.handle_mousedown = frameMouseDown;
203 wwin->client_descriptor.parent = wwin;
204 wwin->client_descriptor.self = wwin;
205 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
207 return wwin;
211 void
212 wWindowDestroy(WWindow *wwin)
214 int i;
216 if (wwin->screen_ptr->cmap_window == wwin) {
217 wwin->screen_ptr->cmap_window = NULL;
220 WMRemoveNotificationObserver(wwin);
222 wwin->flags.destroyed = 1;
224 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
225 if (!wwin->screen_ptr->shortcutWindows[i])
226 continue;
228 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
230 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
231 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
232 wwin->screen_ptr->shortcutWindows[i] = NULL;
236 if (wwin->fake_group && wwin->fake_group->retainCount>0) {
237 wwin->fake_group->retainCount--;
238 if (wwin->fake_group->retainCount==0 && wwin->fake_group->leader!=None) {
239 XDestroyWindow(dpy, wwin->fake_group->leader);
240 wwin->fake_group->leader = None;
241 wwin->fake_group->origLeader = None;
242 XFlush(dpy);
246 if (wwin->normal_hints)
247 XFree(wwin->normal_hints);
249 if (wwin->wm_hints)
250 XFree(wwin->wm_hints);
252 if (wwin->wm_instance)
253 XFree(wwin->wm_instance);
255 if (wwin->wm_class)
256 XFree(wwin->wm_class);
258 if (wwin->wm_gnustep_attr)
259 wfree(wwin->wm_gnustep_attr);
261 if (wwin->cmap_windows)
262 XFree(wwin->cmap_windows);
264 XDeleteContext(dpy, wwin->client_win, wWinContext);
266 if (wwin->frame)
267 wFrameWindowDestroy(wwin->frame);
269 if (wwin->icon) {
270 RemoveFromStackList(wwin->icon->core);
271 wIconDestroy(wwin->icon);
272 if (wPreferences.auto_arrange_icons)
273 wArrangeIcons(wwin->screen_ptr, True);
276 #ifdef NETWM_HINTS
277 if (wwin->net_icon_image)
278 RReleaseImage(wwin->net_icon_image);
279 #endif
281 wrelease(wwin);
285 static void
286 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
288 if (gs_hints->flags & GSWindowStyleAttr) {
289 if (gs_hints->window_style == WMBorderlessWindowMask) {
290 wwin->client_flags.no_border = 1;
291 wwin->client_flags.no_titlebar = 1;
292 wwin->client_flags.no_closable = 1;
293 wwin->client_flags.no_miniaturizable = 1;
294 wwin->client_flags.no_resizable = 1;
295 wwin->client_flags.no_close_button = 1;
296 wwin->client_flags.no_miniaturize_button = 1;
297 wwin->client_flags.no_resizebar = 1;
298 } else {
299 wwin->client_flags.no_close_button =
300 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
302 wwin->client_flags.no_closable =
303 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
305 wwin->client_flags.no_miniaturize_button =
306 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
308 wwin->client_flags.no_miniaturizable =
309 wwin->client_flags.no_miniaturize_button;
311 wwin->client_flags.no_resizebar =
312 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
314 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
316 /* these attributes supposedly imply in the existence
317 * of a titlebar */
318 if (gs_hints->window_style & (WMResizableWindowMask|
319 WMClosableWindowMask|
320 WMMiniaturizableWindowMask)) {
321 wwin->client_flags.no_titlebar = 0;
322 } else {
323 wwin->client_flags.no_titlebar =
324 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
328 } else {
329 /* setup the defaults */
330 wwin->client_flags.no_border = 0;
331 wwin->client_flags.no_titlebar = 0;
332 wwin->client_flags.no_closable = 0;
333 wwin->client_flags.no_miniaturizable = 0;
334 wwin->client_flags.no_resizable = 0;
335 wwin->client_flags.no_close_button = 0;
336 wwin->client_flags.no_miniaturize_button = 0;
337 wwin->client_flags.no_resizebar = 0;
339 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
340 wwin->client_flags.no_appicon = 1;
346 void
347 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
348 WWindowAttributes *mask)
350 if (wflags->no_appicon && mask->no_appicon)
351 wflags->emulate_appicon = 0;
353 if (wwin->main_window!=None) {
354 WApplication *wapp = wApplicationOf(wwin->main_window);
355 if (wapp && !wapp->flags.emulated)
356 wflags->emulate_appicon = 0;
359 if (wwin->transient_for!=None
360 && wwin->transient_for!=wwin->screen_ptr->root_win)
361 wflags->emulate_appicon = 0;
363 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
364 wflags->sunken = 0;
369 void
370 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
372 WScreen *scr = wwin->screen_ptr;
374 /* sets global default stuff */
375 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
376 &wwin->client_flags, NULL, True);
378 * Decoration setting is done in this precedence (lower to higher)
379 * - use global default in the resource database
380 * - guess some settings
381 * - use GNUstep/external window attributes
382 * - set hints specified for the app in the resource DB
385 WSETUFLAG(wwin, broken_close, 0);
387 if (wwin->protocols.DELETE_WINDOW)
388 WSETUFLAG(wwin, kill_close, 0);
389 else
390 WSETUFLAG(wwin, kill_close, 1);
392 /* transients can't be iconified or maximized */
393 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
394 WSETUFLAG(wwin, no_miniaturizable, 1);
395 WSETUFLAG(wwin, no_miniaturize_button, 1);
398 /* if the window can't be resized, remove the resizebar */
399 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
400 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
401 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
402 WSETUFLAG(wwin, no_resizable, 1);
403 WSETUFLAG(wwin, no_resizebar, 1);
406 /* set GNUstep window attributes */
407 if (wwin->wm_gnustep_attr) {
408 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
410 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
412 *level = wwin->wm_gnustep_attr->window_level;
414 * INT_MIN is the only illegal window level.
416 if (*level == INT_MIN)
417 *level = INT_MIN + 1;
418 } else {
419 /* setup defaults */
420 *level = WMNormalLevel;
422 } else {
423 int tmp_workspace = -1;
424 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
425 Bool check;
427 check = False;
429 #ifdef MWM_HINTS
430 wMWMCheckClientHints(wwin);
431 #endif /* MWM_HINTS */
433 #ifdef NETWM_HINTS
434 if (!check)
435 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
436 #endif
438 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
439 * have INT_MIN that means that no window level was requested. -Dan
441 if (tmp_level == INT_MIN) {
442 if (WFLAGP(wwin, floating))
443 *level = WMFloatingLevel;
444 else if (WFLAGP(wwin, sunken))
445 *level = WMSunkenLevel;
446 else
447 *level = WMNormalLevel;
448 } else {
449 *level = tmp_level;
452 if (wwin->transient_for!=None && wwin->transient_for != scr->root_win) {
453 WWindow * transientOwner = wWindowFor(wwin->transient_for);
454 if (transientOwner) {
455 int ownerLevel = transientOwner->frame->core->stacking->window_level;
456 if (ownerLevel > *level) *level = ownerLevel;
460 if (tmp_workspace >= 0) {
461 *workspace = tmp_workspace % scr->workspace_count;
466 * Set attributes specified only for that window/class.
467 * This might do duplicate work with the 1st wDefaultFillAttributes().
469 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
470 &wwin->user_flags, &wwin->defined_user_flags,
471 False);
473 * Sanity checks for attributes that depend on other attributes
475 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
476 wwin->user_flags.emulate_appicon = 0;
478 if (wwin->main_window!=None) {
479 WApplication *wapp = wApplicationOf(wwin->main_window);
480 if (wapp && !wapp->flags.emulated)
481 wwin->user_flags.emulate_appicon = 0;
484 if (wwin->transient_for!=None
485 && wwin->transient_for!=wwin->screen_ptr->root_win)
486 wwin->user_flags.emulate_appicon = 0;
488 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
489 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
490 wwin->user_flags.sunken = 0;
492 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
495 /* windows that have takefocus=False shouldn't take focus at all */
496 if (wwin->focus_mode == WFM_NO_INPUT) {
497 wwin->client_flags.no_focusable = 1;
504 Bool
505 wWindowCanReceiveFocus(WWindow *wwin)
507 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
508 return False;
509 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
510 return False;
511 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
512 return False;
514 return True;
518 Bool
519 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
521 int w1, h1, w2, h2;
523 w1 = wwin->frame->core->width;
524 h1 = wwin->frame->core->height;
525 w2 = obscured->frame->core->width;
526 h2 = obscured->frame->core->height;
528 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
529 && wwin->frame->workspace != obscured->frame->workspace)
530 return False;
532 if (wwin->frame_x + w1 < obscured->frame_x
533 || wwin->frame_y + h1 < obscured->frame_y
534 || wwin->frame_x > obscured->frame_x + w2
535 || wwin->frame_y > obscured->frame_y + h2) {
536 return False;
539 return True;
543 static void
544 fixLeaderProperties(WWindow *wwin)
546 XClassHint *classHint;
547 XWMHints *hints, *clientHints;
548 Window leaders[2], window;
549 char **argv, *command;
550 int argc, i, pid;
551 Bool haveCommand;
553 classHint = XAllocClassHint();
554 clientHints = XGetWMHints(dpy, wwin->client_win);
555 pid = wNETWMGetPidForWindow(wwin->client_win);
556 if (pid > 0) {
557 haveCommand = GetCommandForPid(pid, &argv, &argc);
558 } else {
559 haveCommand = False;
562 leaders[0] = wwin->client_leader;
563 leaders[1] = wwin->group_id;
565 if (haveCommand) {
566 command = GetCommandForWindow(wwin->client_win);
567 if (command) {
568 /* command already set. nothing to do. */
569 wfree(command);
570 } else {
571 XSetCommand(dpy, wwin->client_win, argv, argc);
575 for (i=0; i<2; i++) {
576 window = leaders[i];
577 if (window) {
578 if (XGetClassHint(dpy, window, classHint) == 0) {
579 classHint->res_name = wwin->wm_instance;
580 classHint->res_class = wwin->wm_class;
581 XSetClassHint(dpy, window, classHint);
583 hints = XGetWMHints(dpy, window);
584 if (hints) {
585 XFree(hints);
586 } else if (clientHints) {
587 /* set window group leader to self */
588 clientHints->window_group = window;
589 clientHints->flags |= WindowGroupHint;
590 XSetWMHints(dpy, window, clientHints);
593 if (haveCommand) {
594 command = GetCommandForWindow(window);
595 if (command) {
596 /* command already set. nothing to do. */
597 wfree(command);
598 } else {
599 XSetCommand(dpy, window, argv, argc);
605 XFree(classHint);
606 if (clientHints) {
607 XFree(clientHints);
609 if (haveCommand) {
610 wfree(argv);
615 static Window
616 createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
618 XClassHint *classHint;
619 XWMHints *hints;
620 Window leader;
621 int argc;
622 char **argv;
624 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
625 /* set class hint */
626 classHint = XAllocClassHint();
627 classHint->res_name = instance;
628 classHint->res_class = class;
629 XSetClassHint(dpy, leader, classHint);
630 XFree(classHint);
632 /* inherit these from the original leader if available */
633 hints = XGetWMHints(dpy, win);
634 if (!hints) {
635 hints = XAllocWMHints();
636 hints->flags = 0;
638 /* set window group leader to self */
639 hints->window_group = leader;
640 hints->flags |= WindowGroupHint;
641 XSetWMHints(dpy, leader, hints);
642 XFree(hints);
644 if (XGetCommand(dpy, win, &argv, &argc)!=0 && argc > 0) {
645 XSetCommand(dpy, leader, argv, argc);
646 XFreeStringList(argv);
649 return leader;
653 static int
654 matchIdentifier(void *item, void *cdata)
656 return (strcmp(((WFakeGroupLeader*)item)->identifier, (char*)cdata)==0);
661 *----------------------------------------------------------------
662 * wManageWindow--
663 * reparents the window and allocates a descriptor for it.
664 * Window manager hints and other hints are fetched to configure
665 * the window decoration attributes and others. User preferences
666 * for the window are used if available, to configure window
667 * decorations and some behaviour.
668 * If in startup, windows that are override redirect,
669 * unmapped and never were managed and are Withdrawn are not
670 * managed.
672 * Returns:
673 * the new window descriptor
675 * Side effects:
676 * The window is reparented and appropriate notification
677 * is done to the client. Input mask for the window is setup.
678 * The window descriptor is also associated with various window
679 * contexts and inserted in the head of the window list.
680 * Event handler contexts are associated for some objects
681 * (buttons, titlebar and resizebar)
683 *----------------------------------------------------------------
685 WWindow*
686 wManageWindow(WScreen *scr, Window window)
688 WWindow *wwin;
689 int x, y;
690 unsigned width, height;
691 XWindowAttributes wattribs;
692 XSetWindowAttributes attribs;
693 WWindowState *win_state;
694 WWindow *transientOwner = NULL;
695 int window_level;
696 int wm_state;
697 int foo;
698 int workspace = -1;
699 char *title;
700 Bool withdraw = False;
701 Bool raise = False;
703 /* mutex. */
704 /* XGrabServer(dpy); */
705 XSync(dpy, False);
706 /* make sure the window is still there */
707 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
708 XUngrabServer(dpy);
709 return NULL;
712 /* if it's an override-redirect, ignore it */
713 if (wattribs.override_redirect) {
714 XUngrabServer(dpy);
715 return NULL;
718 wm_state = PropGetWindowState(window);
720 /* if it's startup and the window is unmapped, don't manage it */
721 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
722 XUngrabServer(dpy);
723 return NULL;
726 wwin = wWindowCreate();
728 title= wNETWMGetWindowName(window);
729 if (title)
730 wwin->flags.net_has_title= 1;
731 if (!title && !wFetchName(dpy, window, &title))
732 title = NULL;
734 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
736 #ifdef DEBUG
737 printf("managing window %x\n", (unsigned)window);
738 #endif
740 #ifdef SHAPE
741 if (wShapeSupported) {
742 int junk;
743 unsigned int ujunk;
744 int b_shaped;
746 XShapeSelectInput(dpy, window, ShapeNotifyMask);
747 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
748 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
749 wwin->flags.shaped = b_shaped;
751 #endif
754 *--------------------------------------------------
756 * Get hints and other information in properties
758 *--------------------------------------------------
760 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
762 /* setup descriptor */
763 wwin->client_win = window;
764 wwin->screen_ptr = scr;
766 wwin->old_border_width = wattribs.border_width;
768 wwin->event_mask = CLIENT_EVENTS;
769 attribs.event_mask = CLIENT_EVENTS;
770 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
771 attribs.save_under = False;
772 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
773 |CWSaveUnder, &attribs);
774 XSetWindowBorderWidth(dpy, window, 0);
776 /* get hints from GNUstep app */
777 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
778 wwin->flags.is_gnustep = 1;
780 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
781 wwin->wm_gnustep_attr = NULL;
784 wwin->client_leader = PropGetClientLeader(window);
785 if (wwin->client_leader!=None)
786 wwin->main_window = wwin->client_leader;
788 wwin->wm_hints = XGetWMHints(dpy, window);
790 if (wwin->wm_hints) {
791 if (wwin->wm_hints->flags & StateHint) {
793 if (wwin->wm_hints->initial_state == IconicState) {
795 wwin->flags.miniaturized = 1;
797 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
799 withdraw = True;
803 if (wwin->wm_hints->flags & WindowGroupHint) {
804 wwin->group_id = wwin->wm_hints->window_group;
805 /* window_group has priority over CLIENT_LEADER */
806 wwin->main_window = wwin->group_id;
807 } else {
808 wwin->group_id = None;
811 if (wwin->wm_hints->flags & UrgencyHint)
812 wwin->flags.urgent = 1;
813 } else {
814 wwin->group_id = None;
817 PropGetProtocols(window, &wwin->protocols);
819 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
820 wwin->transient_for = None;
821 } else {
822 if (wwin->transient_for==None || wwin->transient_for==window) {
823 wwin->transient_for = scr->root_win;
824 } else {
825 transientOwner = wWindowFor(wwin->transient_for);
826 if (transientOwner && transientOwner->main_window!=None) {
827 wwin->main_window = transientOwner->main_window;
828 } /*else {
829 wwin->main_window = None;
834 /* guess the focus mode */
835 wwin->focus_mode = getFocusMode(wwin);
837 /* get geometry stuff */
838 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
840 /* printf("wManageWindow: %d %d %d %d\n", x, y, width, height);*/
842 /* get colormap windows */
843 GetColormapWindows(wwin);
846 *--------------------------------------------------
848 * Setup the decoration/window attributes and
849 * geometry
851 *--------------------------------------------------
854 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
856 /* Make broken apps behave as a nice app. */
857 if (WFLAGP(wwin, emulate_appicon)) {
858 wwin->main_window = wwin->client_win;
861 fixLeaderProperties(wwin);
863 wwin->orig_main_window = wwin->main_window;
865 if (wwin->flags.is_gnustep) {
866 WSETUFLAG(wwin, shared_appicon, 0);
869 if (wwin->main_window) {
870 extern Atom _XA_WINDOWMAKER_MENU;
871 XTextProperty text_prop;
873 if (XGetTextProperty(dpy, wwin->main_window, &text_prop,
874 _XA_WINDOWMAKER_MENU)) {
875 WSETUFLAG(wwin, shared_appicon, 0);
879 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
880 char *buffer, *instance, *class;
881 WFakeGroupLeader *fPtr;
882 int index;
884 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
886 /* // only enter here if PropGetWMClass() succeds */
887 PropGetWMClass(wwin->main_window, &class, &instance);
888 buffer = StrConcatDot(instance, class);
890 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void*)buffer);
891 if (index != WANotFound) {
892 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
893 if (fPtr->retainCount == 0) {
894 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
895 instance, class);
897 fPtr->retainCount++;
898 #undef method2
899 if (fPtr->origLeader==None) {
900 #ifdef method2
901 if (ADEQUATE(wwin->group_id)) {
902 fPtr->retainCount++;
903 fPtr->origLeader = wwin->group_id;
904 } else if (ADEQUATE(wwin->client_leader)) {
905 fPtr->retainCount++;
906 fPtr->origLeader = wwin->client_leader;
907 } else if (ADEQUATE(wwin->main_window)) {
908 fPtr->retainCount++;
909 fPtr->origLeader = wwin->main_window;
911 #else
912 if (ADEQUATE(wwin->main_window)) {
913 fPtr->retainCount++;
914 fPtr->origLeader = wwin->main_window;
916 #endif
918 wwin->fake_group = fPtr;
919 /*wwin->group_id = fPtr->leader;*/
920 wwin->main_window = fPtr->leader;
921 wfree(buffer);
922 } else {
923 fPtr = (WFakeGroupLeader*)wmalloc(sizeof(WFakeGroupLeader));
925 fPtr->identifier = buffer;
926 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
927 instance, class);
928 fPtr->origLeader = None;
929 fPtr->retainCount = 1;
931 WMAddToArray(scr->fakeGroupLeaders, fPtr);
933 #ifdef method2
934 if (ADEQUATE(wwin->group_id)) {
935 fPtr->retainCount++;
936 fPtr->origLeader = wwin->group_id;
937 } else if (ADEQUATE(wwin->client_leader)) {
938 fPtr->retainCount++;
939 fPtr->origLeader = wwin->client_leader;
940 } else if (ADEQUATE(wwin->main_window)) {
941 fPtr->retainCount++;
942 fPtr->origLeader = wwin->main_window;
944 #else
945 if (ADEQUATE(wwin->main_window)) {
946 fPtr->retainCount++;
947 fPtr->origLeader = wwin->main_window;
949 #endif
950 wwin->fake_group = fPtr;
951 /*wwin->group_id = fPtr->leader;*/
952 wwin->main_window = fPtr->leader;
954 if (instance)
955 XFree(instance);
956 if (class)
957 XFree(class);
959 #undef method2
960 #undef ADEQUATE
964 *------------------------------------------------------------
966 * Setup the initial state of the window
968 *------------------------------------------------------------
971 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
972 wwin->flags.miniaturized = 1;
975 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin)) {
976 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
979 #ifdef NETWM_HINTS
980 wNETWMCheckInitialClientState(wwin);
981 #endif
983 /* apply previous state if it exists and we're in startup */
984 if (scr->flags.startup && wm_state >= 0) {
986 if (wm_state == IconicState) {
988 wwin->flags.miniaturized = 1;
990 } else if (wm_state == WithdrawnState) {
992 withdraw = True;
996 /* if there is a saved state (from file), restore it */
997 win_state = NULL;
998 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
999 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
1000 } else {
1001 win_state = (WWindowState*)wWindowGetSavedState(window);
1003 if (win_state && !withdraw) {
1005 if (win_state->state->hidden>0)
1006 wwin->flags.hidden = win_state->state->hidden;
1008 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
1009 wwin->flags.shaded = win_state->state->shaded;
1011 if (win_state->state->miniaturized>0 &&
1012 !WFLAGP(wwin, no_miniaturizable)) {
1013 wwin->flags.miniaturized = win_state->state->miniaturized;
1016 if (!IS_OMNIPRESENT(wwin)) {
1017 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
1018 wwin->wm_class);
1019 if (w < 0 || w >= scr->workspace_count) {
1020 workspace = win_state->state->workspace;
1021 if (workspace >= scr->workspace_count)
1022 workspace = scr->current_workspace;
1023 } else {
1024 workspace = w;
1026 } else {
1027 workspace = scr->current_workspace;
1031 /* if we're restarting, restore saved state (from hints).
1032 * This will overwrite previous */
1034 WSavedState *wstate;
1036 if (getSavedState(window, &wstate)) {
1037 wwin->flags.shaded = wstate->shaded;
1038 wwin->flags.hidden = wstate->hidden;
1039 wwin->flags.miniaturized = wstate->miniaturized;
1040 wwin->flags.maximized = wstate->maximized;
1041 if (wwin->flags.maximized) {
1042 wwin->old_geometry.x = wstate->x;
1043 wwin->old_geometry.y = wstate->y;
1044 wwin->old_geometry.width = wstate->w;
1045 wwin->old_geometry.height = wstate->h;
1048 workspace = wstate->workspace;
1049 } else {
1050 wstate = NULL;
1053 /* restore window shortcut */
1054 if (wstate != NULL || win_state != NULL) {
1055 unsigned mask = 0;
1057 if (win_state != NULL)
1058 mask = win_state->state->window_shortcuts;
1060 if (wstate != NULL && mask == 0)
1061 mask = wstate->window_shortcuts;
1063 if (mask > 0) {
1064 int i;
1066 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1067 if (mask & (1<<i)) {
1068 if (!scr->shortcutWindows[i])
1069 scr->shortcutWindows[i] = WMCreateArray(4);
1071 WMAddToArray(scr->shortcutWindows[i], wwin);
1076 if (wstate != NULL) {
1077 wfree(wstate);
1081 /* don't let transients start miniaturized if their owners are not */
1082 if (transientOwner && !transientOwner->flags.miniaturized
1083 && wwin->flags.miniaturized && !withdraw) {
1084 wwin->flags.miniaturized = 0;
1085 if (wwin->wm_hints)
1086 wwin->wm_hints->initial_state = NormalState;
1089 /* set workspace on which the window starts */
1090 if (workspace >= 0) {
1091 if (workspace > scr->workspace_count-1) {
1092 workspace = workspace % scr->workspace_count;
1094 } else {
1095 int w;
1097 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1099 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1101 workspace = w;
1103 } else {
1104 if (wPreferences.open_transients_with_parent && transientOwner) {
1106 workspace = transientOwner->frame->workspace;
1108 } else {
1110 workspace = scr->current_workspace;
1115 /* setup window geometry */
1116 if (win_state && win_state->state->w > 0) {
1117 width = win_state->state->w;
1118 height = win_state->state->h;
1120 wWindowConstrainSize(wwin, &width, &height);
1122 /* do not ask for window placement if the window is
1123 * transient, during startup, if the initial workspace is another one
1124 * or if the window wants to start iconic.
1125 * If geometry was saved, restore it. */
1127 Bool dontBring = False;
1129 if (win_state && win_state->state->w > 0) {
1130 x = win_state->state->x;
1131 y = win_state->state->y;
1132 } else if ((wwin->transient_for==None
1133 || wPreferences.window_placement!=WPM_MANUAL)
1134 && !scr->flags.startup
1135 && workspace == scr->current_workspace
1136 && !wwin->flags.miniaturized
1137 && !wwin->flags.maximized
1138 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
1140 if (transientOwner && transientOwner->flags.mapped) {
1141 int offs = WMAX(20, 2*transientOwner->frame->top_width);
1142 WMRect rect;
1143 int head;
1145 x = transientOwner->frame_x +
1146 abs((transientOwner->frame->core->width - width)/2) + offs;
1147 y = transientOwner->frame_y +
1148 abs((transientOwner->frame->core->height - height)/3) + offs;
1151 * limit transient windows to be inside their parent's head
1153 rect.pos.x = transientOwner->frame_x;
1154 rect.pos.y = transientOwner->frame_y;
1155 rect.size.width = transientOwner->frame->core->width;
1156 rect.size.height = transientOwner->frame->core->height;
1158 head = wGetHeadForRect(scr, rect);
1159 rect = wGetRectForHead(scr, head);
1161 if (x < rect.pos.x)
1162 x = rect.pos.x;
1163 else if (x + width > rect.pos.x + rect.size.width)
1164 x = rect.pos.x + rect.size.width - width;
1166 if (y < rect.pos.y)
1167 y = rect.pos.y;
1168 else if (y + height > rect.pos.y + rect.size.height)
1169 y = rect.pos.y + rect.size.height - height;
1171 } else {
1172 PlaceWindow(wwin, &x, &y, width, height);
1174 if (wPreferences.window_placement == WPM_MANUAL) {
1175 dontBring = True;
1177 } else if (scr->xine_info.count &&
1178 (wwin->normal_hints->flags & PPosition)) {
1179 int head, flags;
1180 WMRect rect;
1181 int reposition = 0;
1184 * Make spash screens come out in the center of a head
1185 * trouble is that most splashies never get here
1186 * they are managed trough atoms but god knows where.
1187 * Dan, do you know ? -peter
1189 * Most of them are not managed, they have set
1190 * OverrideRedirect, which means we can't do anything about
1191 * them. -alfredo
1193 #if 0
1194 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width)/2);
1195 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height)/2);
1197 if ((unsigned)(x + (width - scr->scr_width)/2 + 10) < 20 &&
1198 (unsigned)(y + (height - scr->scr_height)/2 + 10) < 20) {
1200 reposition = 1;
1202 } else
1203 #endif
1206 * xinerama checks for: across head and dead space
1208 rect.pos.x = x;
1209 rect.pos.y = y;
1210 rect.size.width = width;
1211 rect.size.height = height;
1213 head = wGetRectPlacementInfo(scr, rect, &flags);
1215 if (flags & XFLAG_DEAD)
1216 reposition = 1;
1218 if (flags & XFLAG_MULTIPLE)
1219 reposition = 2;
1222 switch (reposition) {
1223 case 1:
1224 head = wGetHeadForPointerLocation(scr);
1225 rect = wGetRectForHead(scr, head);
1227 x = rect.pos.x + (x * rect.size.width)/scr->scr_width;
1228 y = rect.pos.y + (y * rect.size.height)/scr->scr_height;
1229 break;
1231 case 2:
1232 rect = wGetRectForHead(scr, head);
1234 if (x < rect.pos.x)
1235 x = rect.pos.x;
1236 else if (x + width > rect.pos.x + rect.size.width)
1237 x = rect.pos.x + rect.size.width - width;
1239 if (y < rect.pos.y)
1240 y = rect.pos.y;
1241 else if (y + height > rect.pos.y + rect.size.height)
1242 y = rect.pos.y + rect.size.height - height;
1244 break;
1246 default:
1247 break;
1251 if (WFLAGP(wwin, dont_move_off) && dontBring)
1252 wScreenBringInside(scr, &x, &y, width, height);
1255 #ifdef NETWM_HINTS
1256 wNETWMPositionSplash(wwin, &x, &y, width, height);
1257 #endif
1259 if (wwin->flags.urgent) {
1260 if (!IS_OMNIPRESENT(wwin))
1261 wwin->flags.omnipresent ^= 1;
1265 *--------------------------------------------------
1267 * Create frame, borders and do reparenting
1269 *--------------------------------------------------
1271 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1272 #ifdef XKB_BUTTON_HINT
1273 if (wPreferences.modelock)
1274 foo |= WFF_LANGUAGE_BUTTON;
1275 #endif
1276 if (HAS_TITLEBAR(wwin))
1277 foo |= WFF_TITLEBAR;
1278 if (HAS_RESIZEBAR(wwin))
1279 foo |= WFF_RESIZEBAR;
1280 if (HAS_BORDER(wwin))
1281 foo |= WFF_BORDER;
1283 wwin->frame = wFrameWindowCreate(scr, window_level,
1284 x, y, width, height,
1285 &wPreferences.window_title_clearance, foo,
1286 scr->window_title_texture,
1287 scr->resizebar_texture,
1288 scr->window_title_color,
1289 &scr->title_font);
1291 wwin->frame->flags.is_client_window_frame = 1;
1292 wwin->frame->flags.justification = wPreferences.title_justification;
1294 /* setup button images */
1295 wWindowUpdateButtonImages(wwin);
1297 /* hide unused buttons */
1298 foo = 0;
1299 if (WFLAGP(wwin, no_close_button))
1300 foo |= WFF_RIGHT_BUTTON;
1301 if (WFLAGP(wwin, no_miniaturize_button))
1302 foo |= WFF_LEFT_BUTTON;
1303 #ifdef XKB_BUTTON_HINT
1304 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1305 foo |= WFF_LANGUAGE_BUTTON;
1306 #endif
1307 if (foo!=0)
1308 wFrameWindowHideButton(wwin->frame, foo);
1310 wwin->frame->child = wwin;
1312 wwin->frame->workspace = workspace;
1314 wwin->frame->on_click_left = windowIconifyClick;
1315 #ifdef XKB_BUTTON_HINT
1316 if (wPreferences.modelock)
1317 wwin->frame->on_click_language = windowLanguageClick;
1318 #endif
1320 wwin->frame->on_click_right = windowCloseClick;
1321 wwin->frame->on_dblclick_right = windowCloseDblClick;
1323 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1324 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1326 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1329 XSelectInput(dpy, wwin->client_win,
1330 wwin->event_mask & ~StructureNotifyMask);
1332 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1333 0, wwin->frame->top_width);
1335 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1339 int gx, gy;
1341 wClientGetGravityOffsets(wwin, &gx, &gy);
1343 /* if gravity is to the south, account for the border sizes */
1344 if (gy > 0)
1345 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1349 * wWindowConfigure() will init the client window's size
1350 * (wwin->client.{width,height}) and all other geometry
1351 * related variables (frame_x,frame_y)
1353 wWindowConfigure(wwin, x, y, width, height);
1355 /* to make sure the window receives it's new position after reparenting */
1356 wWindowSynthConfigureNotify(wwin);
1359 *--------------------------------------------------
1361 * Setup descriptors and save window to internal
1362 * lists
1364 *--------------------------------------------------
1367 if (wwin->main_window!=None) {
1368 WApplication *app;
1369 WWindow *leader;
1371 /* Leader windows do not necessary set themselves as leaders.
1372 * If this is the case, point the leader of this window to
1373 * itself */
1374 leader = wWindowFor(wwin->main_window);
1375 if (leader && leader->main_window==None) {
1376 leader->main_window = leader->client_win;
1378 app = wApplicationCreate(wwin);
1379 if (app) {
1380 app->last_workspace = workspace;
1383 * Do application specific stuff, like setting application
1384 * wide attributes.
1387 if (wwin->flags.hidden) {
1388 /* if the window was set to hidden because it was hidden
1389 * in a previous incarnation and that state was restored */
1390 app->flags.hidden = 1;
1391 } else if (app->flags.hidden) {
1392 if (WFLAGP(app->main_window_desc, start_hidden)) {
1393 wwin->flags.hidden = 1;
1394 } else {
1395 wUnhideApplication(app, False, False);
1396 raise = True;
1402 /* setup the frame descriptor */
1403 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1404 wwin->frame->core->descriptor.parent = wwin;
1405 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1407 /* don't let windows go away if we die */
1408 XAddToSaveSet(dpy, window);
1410 XLowerWindow(dpy, window);
1412 /* if window is in this workspace and should be mapped, then map it */
1413 if (!wwin->flags.miniaturized
1414 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1415 && !wwin->flags.hidden && !withdraw) {
1417 /* The following "if" is to avoid crashing of clients that expect
1418 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1419 * after the return from this function.
1421 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1422 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1423 } else {
1424 wClientSetState(wwin, NormalState, None);
1427 #if 0
1428 /* if not auto focus, then map the window under the currently
1429 * focused window */
1430 #define _WIDTH(w) (w)->frame->core->width
1431 #define _HEIGHT(w) (w)->frame->core->height
1432 if (!wPreferences.auto_focus && scr->focused_window
1433 && !scr->flags.startup && !transientOwner
1434 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1435 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1436 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1437 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1438 || wwin->flags.maximized)) {
1439 MoveInStackListUnder(scr->focused_window->frame->core,
1440 wwin->frame->core);
1442 #undef _WIDTH
1443 #undef _HEIGHT
1445 #endif
1447 if (wPreferences.superfluous && !wPreferences.no_animations
1448 && !scr->flags.startup &&
1449 (wwin->transient_for==None || wwin->transient_for==scr->root_win)
1451 * The brain damaged idiotic non-click to focus modes will
1452 * have trouble with this because:
1454 * 1. window is created and mapped by the client
1455 * 2. window is mapped by wmaker in small size
1456 * 3. window is animated to grow to normal size
1457 * 4. this function returns to normal event loop
1458 * 5. eventually, the EnterNotify event that would trigger
1459 * the window focusing (if the mouse is over that window)
1460 * will be processed by wmaker.
1461 * But since this event will be rather delayed
1462 * (step 3 has a large delay) the time when the event ocurred
1463 * and when it is processed, the client that owns that window
1464 * will reject the XSetInputFocus() for it.
1466 && (wPreferences.focus_mode==WKF_CLICK
1467 || wPreferences.auto_focus)) {
1468 DoWindowBirth(wwin);
1471 wWindowMap(wwin);
1474 /* setup stacking descriptor */
1475 if (transientOwner) {
1476 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1477 } else {
1478 wwin->frame->core->stacking->child_of = NULL;
1482 if (!scr->focused_window) {
1483 /* first window on the list */
1484 wwin->next = NULL;
1485 wwin->prev = NULL;
1486 scr->focused_window = wwin;
1487 } else {
1488 WWindow *tmp;
1490 /* add window at beginning of focus window list */
1491 tmp = scr->focused_window;
1492 while (tmp->prev)
1493 tmp = tmp->prev;
1494 tmp->prev = wwin;
1495 wwin->next = tmp;
1496 wwin->prev = NULL;
1499 /* raise is set to true if we un-hid the app when this window was born.
1500 * we raise, else old windows of this app will be above this new one. */
1501 if (raise) {
1502 wRaiseFrame(wwin->frame->core);
1505 /* Update name must come after WApplication stuff is done */
1506 wWindowUpdateName(wwin, title);
1507 if (title)
1508 XFree(title);
1510 XUngrabServer(dpy);
1513 *--------------------------------------------------
1515 * Final preparations before window is ready to go
1517 *--------------------------------------------------
1520 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1523 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1524 && !wwin->flags.hidden) {
1525 if (((transientOwner && transientOwner->flags.focused)
1526 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1527 wSetFocusTo(scr, wwin);
1529 wWindowResetMouseGrabs(wwin);
1531 if (!WFLAGP(wwin, no_bind_keys)) {
1532 wWindowSetKeyGrabs(wwin);
1536 WMPostNotificationName(WMNManaged, wwin, NULL);
1539 wColormapInstallForWindow(scr, scr->cmap_window);
1543 *------------------------------------------------------------
1544 * Setup Notification Observers
1545 *------------------------------------------------------------
1547 WMAddNotificationObserver(appearanceObserver, wwin,
1548 WNWindowAppearanceSettingsChanged, wwin);
1552 *--------------------------------------------------
1554 * Cleanup temporary stuff
1556 *--------------------------------------------------
1559 if (win_state)
1560 wWindowDeleteSavedState(win_state);
1562 /* If the window must be withdrawed, then do it now.
1563 * Must do some optimization, 'though */
1564 if (withdraw) {
1565 wwin->flags.mapped = 0;
1566 wClientSetState(wwin, WithdrawnState, None);
1567 wUnmanageWindow(wwin, True, False);
1568 wwin = NULL;
1571 return wwin;
1578 WWindow*
1579 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1580 char *title, int x, int y, int width, int height)
1582 WWindow *wwin;
1583 int foo;
1585 wwin = wWindowCreate();
1587 WMAddNotificationObserver(appearanceObserver, wwin,
1588 WNWindowAppearanceSettingsChanged, wwin);
1590 wwin->flags.internal_window = 1;
1592 WSETUFLAG(wwin, omnipresent, 1);
1593 WSETUFLAG(wwin, no_shadeable, 1);
1594 WSETUFLAG(wwin, no_resizable, 1);
1595 WSETUFLAG(wwin, no_miniaturizable, 1);
1597 wwin->focus_mode = WFM_PASSIVE;
1599 wwin->client_win = window;
1600 wwin->screen_ptr = scr;
1602 wwin->transient_for = owner;
1604 wwin->client.x = x;
1605 wwin->client.y = y;
1606 wwin->client.width = width;
1607 wwin->client.height = height;
1609 wwin->frame_x = wwin->client.x;
1610 wwin->frame_y = wwin->client.y;
1613 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1614 foo |= WFF_TITLEBAR;
1615 #ifdef XKB_BUTTON_HINT
1616 foo |= WFF_LANGUAGE_BUTTON;
1617 #endif
1619 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1620 wwin->frame_x, wwin->frame_y,
1621 width, height,
1622 &wPreferences.window_title_clearance, foo,
1623 scr->window_title_texture,
1624 scr->resizebar_texture,
1625 scr->window_title_color,
1626 &scr->title_font);
1628 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1630 wwin->frame->flags.is_client_window_frame = 1;
1631 wwin->frame->flags.justification = wPreferences.title_justification;
1633 wFrameWindowChangeTitle(wwin->frame, title);
1635 /* setup button images */
1636 wWindowUpdateButtonImages(wwin);
1638 /* hide buttons */
1639 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1641 wwin->frame->child = wwin;
1643 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1645 #ifdef XKB_BUTTON_HINT
1646 if (wPreferences.modelock)
1647 wwin->frame->on_click_language = windowLanguageClick;
1648 #endif
1650 wwin->frame->on_click_right = windowCloseClick;
1652 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1653 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1655 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1657 wwin->client.y += wwin->frame->top_width;
1658 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1659 0, wwin->frame->top_width);
1661 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1662 wwin->client.width, wwin->client.height);
1664 /* setup the frame descriptor */
1665 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1666 wwin->frame->core->descriptor.parent = wwin;
1667 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1670 XLowerWindow(dpy, window);
1671 XMapSubwindows(dpy, wwin->frame->core->window);
1673 /* setup stacking descriptor */
1674 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
1675 WWindow *tmp;
1676 tmp = wWindowFor(wwin->transient_for);
1677 if (tmp)
1678 wwin->frame->core->stacking->child_of = tmp->frame->core;
1679 } else {
1680 wwin->frame->core->stacking->child_of = NULL;
1684 if (!scr->focused_window) {
1685 /* first window on the list */
1686 wwin->next = NULL;
1687 wwin->prev = NULL;
1688 scr->focused_window = wwin;
1689 } else {
1690 WWindow *tmp;
1692 /* add window at beginning of focus window list */
1693 tmp = scr->focused_window;
1694 while (tmp->prev)
1695 tmp = tmp->prev;
1696 tmp->prev = wwin;
1697 wwin->next = tmp;
1698 wwin->prev = NULL;
1701 if (wwin->flags.is_gnustep == 0)
1702 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1704 /* if (wPreferences.auto_focus)*/
1705 wSetFocusTo(scr, wwin);
1707 wWindowResetMouseGrabs(wwin);
1709 wWindowSetKeyGrabs(wwin);
1711 return wwin;
1716 *----------------------------------------------------------------------
1717 * wUnmanageWindow--
1718 * Removes the frame window from a window and destroys all data
1719 * related to it. The window will be reparented back to the root window
1720 * if restore is True.
1722 * Side effects:
1723 * Everything related to the window is destroyed and the window
1724 * is removed from the window lists. Focus is set to the previous on the
1725 * window list.
1726 *----------------------------------------------------------------------
1728 void
1729 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1731 WCoreWindow *frame = wwin->frame->core;
1732 WWindow *owner = NULL;
1733 WWindow *newFocusedWindow = NULL;
1734 int wasFocused;
1735 WScreen *scr = wwin->screen_ptr;
1738 /* First close attribute editor window if open */
1739 if (wwin->flags.inspector_open) {
1740 wCloseInspectorForWindow(wwin);
1743 /* Close window menu if it's open for this window */
1744 if (wwin->flags.menu_open_for_me) {
1745 CloseWindowMenu(scr);
1748 if (!destroyed) {
1749 if (!wwin->flags.internal_window)
1750 XRemoveFromSaveSet(dpy, wwin->client_win);
1752 XSelectInput(dpy, wwin->client_win, NoEventMask);
1754 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1755 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1758 XUnmapWindow(dpy, frame->window);
1760 XUnmapWindow(dpy, wwin->client_win);
1762 /* deselect window */
1763 wSelectWindow(wwin, False);
1765 /* remove all pending events on window */
1766 /* I think this only matters for autoraise */
1767 if (wPreferences.raise_delay)
1768 WMDeleteTimerWithClientData(wwin->frame->core);
1770 XFlush(dpy);
1772 /* reparent the window back to the root */
1773 if (restore)
1774 wClientRestore(wwin);
1776 if (wwin->transient_for!=scr->root_win) {
1777 owner = wWindowFor(wwin->transient_for);
1778 if (owner) {
1779 if (!owner->flags.semi_focused) {
1780 owner = NULL;
1781 } else {
1782 owner->flags.semi_focused = 0;
1787 wasFocused = wwin->flags.focused;
1789 /* remove from window focus list */
1790 if (!wwin->prev && !wwin->next) {
1791 /* was the only window */
1792 scr->focused_window = NULL;
1793 newFocusedWindow = NULL;
1794 } else {
1795 WWindow *tmp;
1797 if (wwin->prev)
1798 wwin->prev->next = wwin->next;
1799 if (wwin->next)
1800 wwin->next->prev = wwin->prev;
1801 else {
1802 scr->focused_window = wwin->prev;
1803 scr->focused_window->next = NULL;
1806 if (wPreferences.focus_mode==WKF_CLICK) {
1808 /* if in click to focus mode and the window
1809 * was a transient, focus the owner window
1811 tmp = NULL;
1812 if (wPreferences.focus_mode==WKF_CLICK) {
1813 tmp = wWindowFor(wwin->transient_for);
1814 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1815 tmp = NULL;
1818 /* otherwise, focus the next one in the focus list */
1819 if (!tmp) {
1820 tmp = scr->focused_window;
1821 while (tmp) { /* look for one in the window list first */
1822 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1823 && (tmp->flags.mapped || tmp->flags.shaded))
1824 break;
1825 tmp = tmp->prev;
1827 if (!tmp) { /* if unsuccessful, choose any focusable window */
1828 tmp = scr->focused_window;
1829 while (tmp) {
1830 if (!WFLAGP(tmp, no_focusable)
1831 && (tmp->flags.mapped || tmp->flags.shaded))
1832 break;
1833 tmp = tmp->prev;
1838 newFocusedWindow = tmp;
1840 } else if (wPreferences.focus_mode==WKF_SLOPPY) {
1841 unsigned int mask;
1842 int foo;
1843 Window bar, win;
1845 /* This is to let the root window get the keyboard input
1846 * if Sloppy focus mode and no other window get focus.
1847 * This way keybindings will not freeze.
1849 tmp = NULL;
1850 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1851 &foo, &foo, &foo, &foo, &mask))
1852 tmp = wWindowFor(win);
1853 if (tmp == wwin)
1854 tmp = NULL;
1855 newFocusedWindow = tmp;
1856 } else {
1857 newFocusedWindow = NULL;
1861 if (!wwin->flags.internal_window) {
1862 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1865 #ifdef DEBUG
1866 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1867 (unsigned)frame->window);
1868 #endif
1870 if (wasFocused) {
1871 if (newFocusedWindow != owner && owner) {
1872 if (wwin->flags.is_gnustep == 0)
1873 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1875 wSetFocusTo(scr, newFocusedWindow);
1877 wWindowDestroy(wwin);
1878 XFlush(dpy);
1882 void
1883 wWindowMap(WWindow *wwin)
1885 XMapWindow(dpy, wwin->frame->core->window);
1886 if (!wwin->flags.shaded) {
1887 /* window will be remapped when getting MapNotify */
1888 XSelectInput(dpy, wwin->client_win,
1889 wwin->event_mask & ~StructureNotifyMask);
1890 XMapWindow(dpy, wwin->client_win);
1891 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1893 wwin->flags.mapped = 1;
1898 void
1899 wWindowUnmap(WWindow *wwin)
1901 wwin->flags.mapped = 0;
1903 /* prevent window withdrawal when getting UnmapNotify */
1904 XSelectInput(dpy, wwin->client_win,
1905 wwin->event_mask & ~StructureNotifyMask);
1906 XUnmapWindow(dpy, wwin->client_win);
1907 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1909 XUnmapWindow(dpy, wwin->frame->core->window);
1914 void
1915 wWindowFocus(WWindow *wwin, WWindow *owin)
1917 WWindow *nowner;
1918 WWindow *oowner;
1920 #ifdef KEEP_XKB_LOCK_STATUS
1921 if (wPreferences.modelock) {
1922 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1924 #endif /* KEEP_XKB_LOCK_STATUS */
1926 wwin->flags.semi_focused = 0;
1928 if (wwin->flags.is_gnustep == 0)
1929 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1931 wwin->flags.focused = 1;
1933 wWindowResetMouseGrabs(wwin);
1935 WMPostNotificationName(WMNChangedFocus, wwin, (void*)True);
1937 if (owin == wwin || !owin)
1938 return;
1940 nowner = wWindowFor(wwin->transient_for);
1942 /* new window is a transient for the old window */
1943 if (nowner == owin) {
1944 owin->flags.semi_focused = 1;
1945 wWindowUnfocus(nowner);
1946 return;
1949 oowner = wWindowFor(owin->transient_for);
1951 /* new window is owner of old window */
1952 if (wwin == oowner) {
1953 wWindowUnfocus(owin);
1954 return;
1957 if (!nowner) {
1958 wWindowUnfocus(owin);
1959 return;
1962 /* new window has same owner of old window */
1963 if (oowner == nowner) {
1964 /* prevent unfocusing of owner */
1965 oowner->flags.semi_focused = 0;
1966 wWindowUnfocus(owin);
1967 oowner->flags.semi_focused = 1;
1969 return;
1972 /* nowner != NULL && oowner != nowner */
1973 nowner->flags.semi_focused = 1;
1974 wWindowUnfocus(nowner);
1975 wWindowUnfocus(owin);
1979 void
1980 wWindowUnfocus(WWindow *wwin)
1982 CloseWindowMenu(wwin->screen_ptr);
1984 if (wwin->flags.is_gnustep == 0)
1985 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1986 ? WS_PFOCUSED : WS_UNFOCUSED);
1988 if (wwin->transient_for!=None
1989 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1990 WWindow *owner;
1991 owner = wWindowFor(wwin->transient_for);
1992 if (owner && owner->flags.semi_focused) {
1993 owner->flags.semi_focused = 0;
1994 if (owner->flags.mapped || owner->flags.shaded) {
1995 wWindowUnfocus(owner);
1996 wFrameWindowPaint(owner->frame);
2000 wwin->flags.focused = 0;
2002 wWindowResetMouseGrabs(wwin);
2004 WMPostNotificationName(WMNChangedFocus, wwin, (void*)False);
2008 void
2009 wWindowUpdateName(WWindow *wwin, char *newTitle)
2011 char *title;
2013 if (!wwin->frame)
2014 return;
2016 wwin->flags.wm_name_changed = 1;
2018 if (!newTitle) {
2019 /* the hint was removed */
2020 title = DEF_WINDOW_TITLE;
2021 } else {
2022 title = newTitle;
2025 if (wFrameWindowChangeTitle(wwin->frame, title)) {
2026 WMPostNotificationName(WMNChangedName, wwin, NULL);
2033 *----------------------------------------------------------------------
2035 * wWindowConstrainSize--
2036 * Constrains size for the client window, taking the maximal size,
2037 * window resize increments and other size hints into account.
2039 * Returns:
2040 * The closest size to what was given that the client window can
2041 * have.
2043 *----------------------------------------------------------------------
2045 void
2046 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
2048 int width = *nwidth;
2049 int height = *nheight;
2050 int winc = 1;
2051 int hinc = 1;
2052 int minW = 1, minH = 1;
2053 int maxW = wwin->screen_ptr->scr_width*2;
2054 int maxH = wwin->screen_ptr->scr_height*2;
2055 int minAX = -1, minAY = -1;
2056 int maxAX = -1, maxAY = -1;
2057 int baseW = 0;
2058 int baseH = 0;
2060 if (wwin->normal_hints) {
2061 winc = wwin->normal_hints->width_inc;
2062 hinc = wwin->normal_hints->height_inc;
2063 minW = wwin->normal_hints->min_width;
2064 minH = wwin->normal_hints->min_height;
2065 maxW = wwin->normal_hints->max_width;
2066 maxH = wwin->normal_hints->max_height;
2067 if (wwin->normal_hints->flags & PAspect) {
2068 minAX = wwin->normal_hints->min_aspect.x;
2069 minAY = wwin->normal_hints->min_aspect.y;
2070 maxAX = wwin->normal_hints->max_aspect.x;
2071 maxAY = wwin->normal_hints->max_aspect.y;
2074 baseW = wwin->normal_hints->base_width;
2075 baseH = wwin->normal_hints->base_height;
2078 if (width < minW)
2079 width = minW;
2080 if (height < minH)
2081 height = minH;
2083 if (width > maxW)
2084 width = maxW;
2085 if (height > maxH)
2086 height = maxH;
2088 /* aspect ratio code borrowed from olwm */
2089 if (minAX > 0) {
2090 /* adjust max aspect ratio */
2091 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
2092 if (maxAX > maxAY) {
2093 height = (width * maxAY) / maxAX;
2094 if (height > maxH) {
2095 height = maxH;
2096 width = (height * maxAX) / maxAY;
2098 } else {
2099 width = (height * maxAX) / maxAY;
2100 if (width > maxW) {
2101 width = maxW;
2102 height = (width * maxAY) / maxAX;
2107 /* adjust min aspect ratio */
2108 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
2109 if (minAX > minAY) {
2110 height = (width * minAY) / minAX;
2111 if (height < minH) {
2112 height = minH;
2113 width = (height * minAX) / minAY;
2115 } else {
2116 width = (height * minAX) / minAY;
2117 if (width < minW) {
2118 width = minW;
2119 height = (width * minAY) / minAX;
2125 if (baseW != 0) {
2126 width = (((width - baseW) / winc) * winc) + baseW;
2127 } else {
2128 width = (((width - minW) / winc) * winc) + minW;
2131 if (baseH != 0) {
2132 height = (((height - baseH) / hinc) * hinc) + baseH;
2133 } else {
2134 height = (((height - minH) / hinc) * hinc) + minH;
2137 /* broken stupid apps may cause preposterous values for these.. */
2138 if (width > 0)
2139 *nwidth = width;
2140 if (height > 0)
2141 *nheight = height;
2145 void
2146 wWindowCropSize(WWindow *wwin, int maxW, int maxH,
2147 int *width, int *height)
2149 int baseW = 0, baseH = 0;
2150 int winc = 1, hinc = 1;
2152 if (wwin->normal_hints) {
2153 baseW = wwin->normal_hints->base_width;
2154 baseH = wwin->normal_hints->base_height;
2156 winc = wwin->normal_hints->width_inc;
2157 hinc = wwin->normal_hints->height_inc;
2160 if (*width > maxW)
2161 *width = maxW - (maxW - baseW) % winc;
2163 if (*height > maxH)
2164 *height = maxH - (maxH - baseH) % hinc;
2168 void
2169 wWindowChangeWorkspace(WWindow *wwin, int workspace)
2171 WScreen *scr = wwin->screen_ptr;
2172 WApplication *wapp;
2173 int unmap = 0;
2175 if (workspace >= scr->workspace_count || workspace < 0
2176 || workspace == wwin->frame->workspace)
2177 return;
2179 if (workspace != scr->current_workspace) {
2180 /* Sent to other workspace. Unmap window */
2181 if ((wwin->flags.mapped
2182 || wwin->flags.shaded
2183 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2184 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2186 wapp = wApplicationOf(wwin->main_window);
2187 if (wapp) {
2188 wapp->last_workspace = workspace;
2190 if (wwin->flags.miniaturized) {
2191 if (wwin->icon) {
2192 XUnmapWindow(dpy, wwin->icon->core->window);
2193 wwin->icon->mapped = 0;
2195 } else {
2196 unmap = 1;
2197 wSetFocusTo(scr, NULL);
2200 } else {
2201 /* brought to current workspace. Map window */
2202 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2203 if (wwin->icon) {
2204 XMapWindow(dpy, wwin->icon->core->window);
2205 wwin->icon->mapped = 1;
2207 } else if (!wwin->flags.mapped &&
2208 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2209 wWindowMap(wwin);
2212 if (!IS_OMNIPRESENT(wwin)) {
2213 int oldWorkspace = wwin->frame->workspace;
2215 wwin->frame->workspace = workspace;
2217 WMPostNotificationName(WMNChangedWorkspace, wwin, (void*)oldWorkspace);
2220 if (unmap) {
2221 wWindowUnmap(wwin);
2226 void
2227 wWindowSynthConfigureNotify(WWindow *wwin)
2229 XEvent sevent;
2231 sevent.type = ConfigureNotify;
2232 sevent.xconfigure.display = dpy;
2233 sevent.xconfigure.event = wwin->client_win;
2234 sevent.xconfigure.window = wwin->client_win;
2236 sevent.xconfigure.x = wwin->client.x;
2237 sevent.xconfigure.y = wwin->client.y;
2238 sevent.xconfigure.width = wwin->client.width;
2239 sevent.xconfigure.height = wwin->client.height;
2241 sevent.xconfigure.border_width = wwin->old_border_width;
2242 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2243 sevent.xconfigure.above = wwin->frame->titlebar->window;
2244 else
2245 sevent.xconfigure.above = None;
2247 sevent.xconfigure.override_redirect = False;
2248 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2249 XFlush(dpy);
2254 *----------------------------------------------------------------------
2255 * wWindowConfigure--
2256 * Configures the frame, decorations and client window to the
2257 * specified geometry. The geometry is not checked for validity,
2258 * wWindowConstrainSize() must be used for that.
2259 * The size parameters are for the client window, but the position is
2260 * for the frame.
2261 * The client window receives a ConfigureNotify event, according
2262 * to what ICCCM says.
2264 * Returns:
2265 * None
2267 * Side effects:
2268 * Window size and position are changed and client window receives
2269 * a ConfigureNotify event.
2270 *----------------------------------------------------------------------
2272 void
2273 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
2274 WWindow *wwin;
2275 int req_x, req_y; /* new position of the frame */
2276 int req_width, req_height; /* new size of the client */
2278 int synth_notify = False;
2279 int resize;
2281 resize = (req_width!=wwin->client.width
2282 || req_height!=wwin->client.height);
2284 * if the window is being moved but not resized then
2285 * send a synthetic ConfigureNotify
2287 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
2288 synth_notify = True;
2291 if (WFLAGP(wwin, dont_move_off))
2292 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2293 req_width, req_height);
2294 if (resize) {
2295 if (req_width < MIN_WINDOW_SIZE)
2296 req_width = MIN_WINDOW_SIZE;
2297 if (req_height < MIN_WINDOW_SIZE)
2298 req_height = MIN_WINDOW_SIZE;
2300 /* If growing, resize inner part before frame,
2301 * if shrinking, resize frame before.
2302 * This will prevent the frame (that can have a different color)
2303 * to be exposed, causing flicker */
2304 if (req_height > wwin->frame->core->height
2305 || req_width > wwin->frame->core->width)
2306 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2308 if (wwin->flags.shaded) {
2309 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2310 req_width, wwin->frame->core->height);
2311 wwin->old_geometry.height = req_height;
2312 } else {
2313 int h;
2315 h = req_height + wwin->frame->top_width
2316 + wwin->frame->bottom_width;
2318 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2321 if (!(req_height > wwin->frame->core->height
2322 || req_width > wwin->frame->core->width))
2323 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2325 wwin->client.x = req_x;
2326 wwin->client.y = req_y + wwin->frame->top_width;
2327 wwin->client.width = req_width;
2328 wwin->client.height = req_height;
2329 } else {
2330 wwin->client.x = req_x;
2331 wwin->client.y = req_y + wwin->frame->top_width;
2333 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2335 wwin->frame_x = req_x;
2336 wwin->frame_y = req_y;
2337 if (HAS_BORDER(wwin)) {
2338 wwin->client.x += FRAME_BORDER_WIDTH;
2339 wwin->client.y += FRAME_BORDER_WIDTH;
2342 #ifdef SHAPE
2343 if (wShapeSupported && wwin->flags.shaped && resize) {
2344 wWindowSetShape(wwin);
2346 #endif
2348 if (synth_notify)
2349 wWindowSynthConfigureNotify(wwin);
2350 XFlush(dpy);
2354 void
2355 wWindowMove(wwin, req_x, req_y)
2356 WWindow *wwin;
2357 int req_x, req_y; /* new position of the frame */
2359 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2360 int synth_notify = False;
2362 /* Send a synthetic ConfigureNotify event for every window movement. */
2363 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2364 synth_notify = True;
2366 #else
2367 /* A single synthetic ConfigureNotify event is sent at the end of
2368 * a completed (opaque) movement in moveres.c */
2369 #endif
2371 if (WFLAGP(wwin, dont_move_off))
2372 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2373 wwin->frame->core->width, wwin->frame->core->height);
2375 wwin->client.x = req_x;
2376 wwin->client.y = req_y + wwin->frame->top_width;
2377 if (HAS_BORDER(wwin)) {
2378 wwin->client.x += FRAME_BORDER_WIDTH;
2379 wwin->client.y += FRAME_BORDER_WIDTH;
2382 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2384 wwin->frame_x = req_x;
2385 wwin->frame_y = req_y;
2387 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2388 if (synth_notify)
2389 wWindowSynthConfigureNotify(wwin);
2390 #endif
2394 void
2395 wWindowUpdateButtonImages(WWindow *wwin)
2397 WScreen *scr = wwin->screen_ptr;
2398 Pixmap pixmap, mask;
2399 WFrameWindow *fwin = wwin->frame;
2401 if (!HAS_TITLEBAR(wwin))
2402 return;
2404 /* miniaturize button */
2406 if (!WFLAGP(wwin, no_miniaturize_button)) {
2407 if (wwin->wm_gnustep_attr
2408 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2409 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2411 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2412 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2413 } else {
2414 mask = None;
2417 if (fwin->lbutton_image
2418 && (fwin->lbutton_image->image != pixmap
2419 || fwin->lbutton_image->mask != mask)) {
2420 wPixmapDestroy(fwin->lbutton_image);
2421 fwin->lbutton_image = NULL;
2424 if (!fwin->lbutton_image) {
2425 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2426 fwin->lbutton_image->client_owned = 1;
2427 fwin->lbutton_image->client_owned_mask = 1;
2429 } else {
2430 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2431 wPixmapDestroy(fwin->lbutton_image);
2433 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2437 #ifdef XKB_BUTTON_HINT
2438 if (!WFLAGP(wwin, no_language_button)) {
2439 if (fwin->languagebutton_image &&
2440 !fwin->languagebutton_image->shared) {
2441 wPixmapDestroy(fwin->languagebutton_image);
2443 fwin->languagebutton_image =
2444 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2446 #endif
2448 /* close button */
2450 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2451 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2453 if (!WFLAGP(wwin, no_close_button)) {
2454 if (wwin->wm_gnustep_attr
2455 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2456 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2458 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2459 mask = wwin->wm_gnustep_attr->close_mask;
2460 else
2461 mask = None;
2463 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2464 || fwin->rbutton_image->mask != mask)) {
2465 wPixmapDestroy(fwin->rbutton_image);
2466 fwin->rbutton_image = NULL;
2469 if (!fwin->rbutton_image) {
2470 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2471 fwin->rbutton_image->client_owned = 1;
2472 fwin->rbutton_image->client_owned_mask = 1;
2475 } else if (WFLAGP(wwin, kill_close)) {
2477 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2478 wPixmapDestroy(fwin->rbutton_image);
2480 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2482 } else if (MGFLAGP(wwin, broken_close)) {
2484 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2485 wPixmapDestroy(fwin->rbutton_image);
2487 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2489 } else {
2491 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2492 wPixmapDestroy(fwin->rbutton_image);
2494 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2498 /* force buttons to be redrawn */
2499 fwin->flags.need_texture_change = 1;
2500 wFrameWindowPaint(fwin);
2505 *---------------------------------------------------------------------------
2506 * wWindowConfigureBorders--
2507 * Update window border configuration according to attribute flags.
2509 *---------------------------------------------------------------------------
2511 void
2512 wWindowConfigureBorders(WWindow *wwin)
2514 if (wwin->frame) {
2515 int flags;
2516 int newy, oldh;
2518 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2520 #ifdef XKB_BUTTON_HINT
2521 flags |= WFF_LANGUAGE_BUTTON;
2522 #endif
2524 if (HAS_TITLEBAR(wwin))
2525 flags |= WFF_TITLEBAR;
2526 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2527 flags |= WFF_RESIZEBAR;
2528 if (HAS_BORDER(wwin))
2529 flags |= WFF_BORDER;
2530 if (wwin->flags.shaded)
2531 flags |= WFF_IS_SHADED;
2533 oldh = wwin->frame->top_width;
2534 wFrameWindowUpdateBorders(wwin->frame, flags);
2535 if (oldh != wwin->frame->top_width) {
2536 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2538 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2539 wWindowConfigure(wwin, wwin->frame_x, newy,
2540 wwin->client.width, wwin->client.height);
2543 flags = 0;
2544 if (!WFLAGP(wwin, no_miniaturize_button)
2545 && wwin->frame->flags.hide_left_button)
2546 flags |= WFF_LEFT_BUTTON;
2548 #ifdef XKB_BUTTON_HINT
2549 if (!WFLAGP(wwin, no_language_button)
2550 && wwin->frame->flags.hide_language_button) {
2551 flags |= WFF_LANGUAGE_BUTTON;
2553 #endif
2555 if (!WFLAGP(wwin, no_close_button)
2556 && wwin->frame->flags.hide_right_button)
2557 flags |= WFF_RIGHT_BUTTON;
2559 if (flags!=0) {
2560 wWindowUpdateButtonImages(wwin);
2561 wFrameWindowShowButton(wwin->frame, flags);
2564 flags = 0;
2565 if (WFLAGP(wwin, no_miniaturize_button)
2566 && !wwin->frame->flags.hide_left_button)
2567 flags |= WFF_LEFT_BUTTON;
2569 #ifdef XKB_BUTTON_HINT
2570 if (WFLAGP(wwin, no_language_button)
2571 && !wwin->frame->flags.hide_language_button)
2572 flags |= WFF_LANGUAGE_BUTTON;
2573 #endif
2575 if (WFLAGP(wwin, no_close_button)
2576 && !wwin->frame->flags.hide_right_button)
2577 flags |= WFF_RIGHT_BUTTON;
2579 if (flags!=0)
2580 wFrameWindowHideButton(wwin->frame, flags);
2582 #ifdef SHAPE
2583 if (wShapeSupported && wwin->flags.shaped) {
2584 wWindowSetShape(wwin);
2586 #endif
2591 void
2592 wWindowSaveState(WWindow *wwin)
2594 CARD32 data[10];
2595 int i;
2597 memset(data, 0, sizeof(CARD32)*10);
2598 data[0] = wwin->frame->workspace;
2599 data[1] = wwin->flags.miniaturized;
2600 data[2] = wwin->flags.shaded;
2601 data[3] = wwin->flags.hidden;
2602 data[4] = wwin->flags.maximized;
2603 if (wwin->flags.maximized == 0) {
2604 data[5] = wwin->frame_x;
2605 data[6] = wwin->frame_y;
2606 data[7] = wwin->frame->core->width;
2607 data[8] = wwin->frame->core->height;
2608 } else {
2609 data[5] = wwin->old_geometry.x;
2610 data[6] = wwin->old_geometry.y;
2611 data[7] = wwin->old_geometry.width;
2612 data[8] = wwin->old_geometry.height;
2615 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2616 if (wwin->screen_ptr->shortcutWindows[i] &&
2617 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2618 data[9] |= 1<<i;
2620 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2621 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2622 (unsigned char *)data, 10);
2626 static int
2627 getSavedState(Window window, WSavedState **state)
2629 Atom type_ret;
2630 int fmt_ret;
2631 unsigned long nitems_ret;
2632 unsigned long bytes_after_ret;
2633 CARD32 *data;
2635 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2636 True, _XA_WINDOWMAKER_STATE,
2637 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2638 (unsigned char **)&data)!=Success || !data)
2639 return 0;
2641 *state = wmalloc(sizeof(WSavedState));
2643 (*state)->workspace = data[0];
2644 (*state)->miniaturized = data[1];
2645 (*state)->shaded = data[2];
2646 (*state)->hidden = data[3];
2647 (*state)->maximized = data[4];
2648 (*state)->x = data[5];
2649 (*state)->y = data[6];
2650 (*state)->w = data[7];
2651 (*state)->h = data[8];
2652 (*state)->window_shortcuts = data[9];
2654 XFree(data);
2656 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2657 return 1;
2658 else
2659 return 0;
2663 #ifdef SHAPE
2664 void
2665 wWindowClearShape(WWindow *wwin)
2667 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2668 0, wwin->frame->top_width, None, ShapeSet);
2669 XFlush(dpy);
2672 void
2673 wWindowSetShape(WWindow *wwin)
2675 XRectangle rect[2];
2676 int count;
2677 #ifdef OPTIMIZE_SHAPE
2678 XRectangle *rects;
2679 XRectangle *urec;
2680 int ordering;
2682 /* only shape is the client's */
2683 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2684 goto alt_code;
2687 /* Get array of rectangles describing the shape mask */
2688 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2689 &count, &ordering);
2690 if (!rects) {
2691 goto alt_code;
2694 urec = malloc(sizeof(XRectangle)*(count+2));
2695 if (!urec) {
2696 XFree(rects);
2697 goto alt_code;
2700 /* insert our decoration rectangles in the rect list */
2701 memcpy(urec, rects, sizeof(XRectangle)*count);
2702 XFree(rects);
2704 if (HAS_TITLEBAR(wwin)) {
2705 urec[count].x = -1;
2706 urec[count].y = -1 - wwin->frame->top_width;
2707 urec[count].width = wwin->frame->core->width + 2;
2708 urec[count].height = wwin->frame->top_width + 1;
2709 count++;
2711 if (HAS_RESIZEBAR(wwin)) {
2712 urec[count].x = -1;
2713 urec[count].y = wwin->frame->core->height
2714 - wwin->frame->bottom_width - wwin->frame->top_width;
2715 urec[count].width = wwin->frame->core->width + 2;
2716 urec[count].height = wwin->frame->bottom_width + 1;
2717 count++;
2720 /* shape our frame window */
2721 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2722 0, wwin->frame->top_width, urec, count,
2723 ShapeSet, Unsorted);
2724 XFlush(dpy);
2725 wfree(urec);
2726 return;
2728 alt_code:
2729 #endif /* OPTIMIZE_SHAPE */
2730 count = 0;
2731 if (HAS_TITLEBAR(wwin)) {
2732 rect[count].x = -1;
2733 rect[count].y = -1;
2734 rect[count].width = wwin->frame->core->width + 2;
2735 rect[count].height = wwin->frame->top_width + 1;
2736 count++;
2738 if (HAS_RESIZEBAR(wwin)) {
2739 rect[count].x = -1;
2740 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2741 rect[count].width = wwin->frame->core->width + 2;
2742 rect[count].height = wwin->frame->bottom_width + 1;
2743 count++;
2745 if (count > 0) {
2746 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2747 0, 0, rect, count, ShapeSet, Unsorted);
2749 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2750 0, wwin->frame->top_width, wwin->client_win,
2751 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2752 XFlush(dpy);
2754 #endif /* SHAPE */
2756 /* ====================================================================== */
2758 static FocusMode
2759 getFocusMode(WWindow *wwin)
2761 FocusMode mode;
2763 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2764 if (wwin->wm_hints->input == True) {
2765 if (wwin->protocols.TAKE_FOCUS)
2766 mode = WFM_LOCALLY_ACTIVE;
2767 else
2768 mode = WFM_PASSIVE;
2769 } else {
2770 if (wwin->protocols.TAKE_FOCUS)
2771 mode = WFM_GLOBALLY_ACTIVE;
2772 else
2773 mode = WFM_NO_INPUT;
2775 } else {
2776 mode = WFM_PASSIVE;
2778 return mode;
2782 void
2783 wWindowSetKeyGrabs(WWindow *wwin)
2785 int i;
2786 WShortKey *key;
2788 for (i=0; i<WKBD_LAST; i++) {
2789 key = &wKeyBindings[i];
2791 if (key->keycode==0)
2792 continue;
2793 if (key->modifier!=AnyModifier) {
2794 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2795 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2796 #ifdef NUMLOCK_HACK
2797 /* Also grab all modifier combinations possible that include,
2798 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2799 * work even if the NumLock/ScrollLock key is on.
2801 wHackedGrabKey(key->keycode, key->modifier,
2802 wwin->frame->core->window, True, GrabModeAsync,
2803 GrabModeAsync);
2804 #endif
2806 XGrabKey(dpy, key->keycode, key->modifier,
2807 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2810 #ifndef LITE
2811 wRootMenuBindShortcuts(wwin->frame->core->window);
2812 #endif
2817 void
2818 wWindowResetMouseGrabs(WWindow *wwin)
2820 /* Mouse grabs can't be done on the client window because of
2821 * ICCCM and because clients that try to do the same will crash.
2823 * But there is a problem wich makes tbar buttons of unfocused
2824 * windows not usable as the click goes to the frame window instead
2825 * of the button itself. Must figure a way to fix that.
2828 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2830 if (!WFLAGP(wwin, no_bind_mouse)) {
2831 /* grabs for Meta+drag */
2832 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2833 True, ButtonPressMask|ButtonReleaseMask,
2834 GrabModeSync, GrabModeAsync, None, None);
2837 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2838 && !wwin->flags.is_gnustep) {
2839 /* the passive grabs to focus the window */
2840 /* if (wPreferences.focus_mode == WKF_CLICK) */
2841 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2842 True, ButtonPressMask|ButtonReleaseMask,
2843 GrabModeSync, GrabModeAsync, None, None);
2845 XFlush(dpy);
2849 void
2850 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2852 if (attr->flags & GSExtraFlagsAttr) {
2853 if (MGFLAGP(wwin, broken_close) !=
2854 (attr->extra_flags & GSDocumentEditedFlag)) {
2855 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2856 wWindowUpdateButtonImages(wwin);
2862 WMagicNumber
2863 wWindowAddSavedState(char *instance, char *class, char *command,
2864 pid_t pid, WSavedState *state)
2866 WWindowState *wstate;
2868 wstate = malloc(sizeof(WWindowState));
2869 if (!wstate)
2870 return 0;
2872 memset(wstate, 0, sizeof(WWindowState));
2873 wstate->pid = pid;
2874 if (instance)
2875 wstate->instance = wstrdup(instance);
2876 if (class)
2877 wstate->class = wstrdup(class);
2878 if (command)
2879 wstate->command = wstrdup(command);
2880 wstate->state = state;
2882 wstate->next = windowState;
2883 windowState = wstate;
2885 #ifdef DEBUG
2886 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2887 class, command);
2888 #endif
2890 return wstate;
2894 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2897 WMagicNumber
2898 wWindowGetSavedState(Window win)
2900 char *instance, *class, *command=NULL;
2901 WWindowState *wstate = windowState;
2903 if (!wstate)
2904 return NULL;
2906 command = GetCommandForWindow(win);
2907 if (!command)
2908 return NULL;
2910 if (PropGetWMClass(win, &class, &instance)) {
2911 while (wstate) {
2912 if (SAME(instance, wstate->instance) &&
2913 SAME(class, wstate->class) &&
2914 SAME(command, wstate->command)) {
2915 break;
2917 wstate = wstate->next;
2919 } else {
2920 wstate = NULL;
2923 #ifdef DEBUG
2924 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2925 class, command);
2926 #endif
2928 if (command) wfree(command);
2929 if (instance) XFree(instance);
2930 if (class) XFree(class);
2932 return wstate;
2936 void
2937 wWindowDeleteSavedState(WMagicNumber id)
2939 WWindowState *tmp, *wstate=(WWindowState*)id;
2941 if (!wstate || !windowState)
2942 return;
2944 tmp = windowState;
2945 if (tmp==wstate) {
2946 windowState = wstate->next;
2947 #ifdef DEBUG
2948 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2949 wstate, wstate->instance, wstate->class, wstate->command);
2950 #endif
2951 if (wstate->instance) wfree(wstate->instance);
2952 if (wstate->class) wfree(wstate->class);
2953 if (wstate->command) wfree(wstate->command);
2954 wfree(wstate->state);
2955 wfree(wstate);
2956 } else {
2957 while (tmp->next) {
2958 if (tmp->next==wstate) {
2959 tmp->next=wstate->next;
2960 #ifdef DEBUG
2961 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2962 wstate, wstate->instance, wstate->class, wstate->command);
2963 #endif
2964 if (wstate->instance) wfree(wstate->instance);
2965 if (wstate->class) wfree(wstate->class);
2966 if (wstate->command) wfree(wstate->command);
2967 wfree(wstate->state);
2968 wfree(wstate);
2969 break;
2971 tmp = tmp->next;
2977 void
2978 wWindowDeleteSavedStatesForPID(pid_t pid)
2980 WWindowState *tmp, *wstate;
2982 if (!windowState)
2983 return;
2985 tmp = windowState;
2986 if (tmp->pid == pid) {
2987 wstate = windowState;
2988 windowState = tmp->next;
2989 #ifdef DEBUG
2990 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2991 wstate, wstate->instance, wstate->class, wstate->command);
2992 #endif
2993 if (wstate->instance) wfree(wstate->instance);
2994 if (wstate->class) wfree(wstate->class);
2995 if (wstate->command) wfree(wstate->command);
2996 wfree(wstate->state);
2997 wfree(wstate);
2998 } else {
2999 while (tmp->next) {
3000 if (tmp->next->pid==pid) {
3001 wstate = tmp->next;
3002 tmp->next = wstate->next;
3003 #ifdef DEBUG
3004 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
3005 wstate, wstate->instance, wstate->class, wstate->command);
3006 #endif
3007 if (wstate->instance) wfree(wstate->instance);
3008 if (wstate->class) wfree(wstate->class);
3009 if (wstate->command) wfree(wstate->command);
3010 wfree(wstate->state);
3011 wfree(wstate);
3012 break;
3014 tmp = tmp->next;
3020 void
3021 wWindowSetOmnipresent(WWindow *wwin, Bool flag)
3023 if (wwin->flags.omnipresent == flag)
3024 return;
3026 wwin->flags.omnipresent = flag;
3027 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
3031 /* ====================================================================== */
3033 static void
3034 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3036 WWindow *wwin = data;
3038 #ifndef NUMLOCK_HACK
3039 if ((event->xbutton.state & ValidModMask)
3040 != (event->xbutton.state & ~LockMask)) {
3041 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3042 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3044 #endif
3046 event->xbutton.state &= ValidModMask;
3048 CloseWindowMenu(wwin->screen_ptr);
3050 if (wPreferences.focus_mode==WKF_CLICK
3051 && !(event->xbutton.state&ControlMask)
3052 && !WFLAGP(wwin, no_focusable)) {
3053 wSetFocusTo(wwin->screen_ptr, wwin);
3056 if (event->xbutton.button == Button1)
3057 wRaiseFrame(wwin->frame->core);
3059 if (event->xbutton.window != wwin->frame->resizebar->window) {
3060 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
3061 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3062 GrabModeAsync, GrabModeAsync, None,
3063 None, CurrentTime)!=GrabSuccess) {
3064 #ifdef DEBUG0
3065 wwarning("pointer grab failed for window move");
3066 #endif
3067 return;
3071 if (event->xbutton.state & MOD_MASK) {
3072 /* move the window */
3073 wMouseMoveWindow(wwin, event);
3074 XUngrabPointer(dpy, CurrentTime);
3075 } else {
3076 wMouseResizeWindow(wwin, event);
3077 XUngrabPointer(dpy, CurrentTime);
3083 static void
3084 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
3086 WWindow *wwin = data;
3088 event->xbutton.state &= ValidModMask;
3090 if (event->xbutton.button==Button1) {
3091 if (event->xbutton.state == 0) {
3092 if (!WFLAGP(wwin, no_shadeable)) {
3093 /* shade window */
3094 if (wwin->flags.shaded)
3095 wUnshadeWindow(wwin);
3096 else
3097 wShadeWindow(wwin);
3099 } else {
3100 int dir = 0;
3102 if (event->xbutton.state & ControlMask)
3103 dir |= MAX_VERTICAL;
3105 if (event->xbutton.state & ShiftMask) {
3106 dir |= MAX_HORIZONTAL;
3107 if (!(event->xbutton.state & ControlMask))
3108 wSelectWindow(wwin, !wwin->flags.selected);
3111 /* maximize window */
3112 if (dir!=0 && IS_RESIZABLE(wwin)) {
3113 int ndir = dir ^ wwin->flags.maximized;
3115 if (ndir != 0) {
3116 wMaximizeWindow(wwin, ndir);
3117 } else {
3118 wUnmaximizeWindow(wwin);
3122 } else if (event->xbutton.button==Button3) {
3123 if (event->xbutton.state & MOD_MASK) {
3124 wHideOtherApplications(wwin);
3126 } else if (event->xbutton.button==Button2) {
3127 wSelectWindow(wwin, !wwin->flags.selected);
3128 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
3129 wShadeWindow(wwin);
3130 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
3131 wUnshadeWindow(wwin);
3136 static void
3137 frameMouseDown(WObjDescriptor *desc, XEvent *event)
3139 WWindow *wwin = desc->parent;
3141 event->xbutton.state &= ValidModMask;
3143 CloseWindowMenu(wwin->screen_ptr);
3145 if (/*wPreferences.focus_mode==WKF_CLICK
3146 &&*/ !(event->xbutton.state&ControlMask)
3147 && !WFLAGP(wwin, no_focusable)) {
3148 wSetFocusTo(wwin->screen_ptr, wwin);
3150 if (event->xbutton.button == Button1) {
3151 wRaiseFrame(wwin->frame->core);
3154 if (event->xbutton.state & MOD_MASK) {
3155 /* move the window */
3156 if (XGrabPointer(dpy, wwin->client_win, False,
3157 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3158 GrabModeAsync, GrabModeAsync, None,
3159 None, CurrentTime)!=GrabSuccess) {
3160 #ifdef DEBUG0
3161 wwarning("pointer grab failed for window move");
3162 #endif
3163 return;
3165 if (event->xbutton.button == Button3)
3166 wMouseResizeWindow(wwin, event);
3167 else if (event->xbutton.button==Button1 || event->xbutton.button==Button2)
3168 wMouseMoveWindow(wwin, event);
3169 XUngrabPointer(dpy, CurrentTime);
3174 static void
3175 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3177 WWindow *wwin = (WWindow*)data;
3179 #ifndef NUMLOCK_HACK
3180 if ((event->xbutton.state & ValidModMask)
3181 != (event->xbutton.state & ~LockMask)) {
3182 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3183 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3185 #endif
3186 event->xbutton.state &= ValidModMask;
3188 CloseWindowMenu(wwin->screen_ptr);
3190 if (wPreferences.focus_mode==WKF_CLICK
3191 && !(event->xbutton.state&ControlMask)
3192 && !WFLAGP(wwin, no_focusable)) {
3193 wSetFocusTo(wwin->screen_ptr, wwin);
3196 if (event->xbutton.button == Button1
3197 || event->xbutton.button == Button2) {
3199 if (event->xbutton.button == Button1) {
3200 if (event->xbutton.state & MOD_MASK) {
3201 wLowerFrame(wwin->frame->core);
3202 } else {
3203 wRaiseFrame(wwin->frame->core);
3206 if ((event->xbutton.state & ShiftMask)
3207 && !(event->xbutton.state & ControlMask)) {
3208 wSelectWindow(wwin, !wwin->flags.selected);
3209 return;
3211 if (event->xbutton.window != wwin->frame->titlebar->window
3212 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3213 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3214 GrabModeAsync, GrabModeAsync, None,
3215 None, CurrentTime)!=GrabSuccess) {
3216 #ifdef DEBUG0
3217 wwarning("pointer grab failed for window move");
3218 #endif
3219 return;
3222 /* move the window */
3223 wMouseMoveWindow(wwin, event);
3225 XUngrabPointer(dpy, CurrentTime);
3226 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
3227 && !wwin->flags.internal_window
3228 && !WCHECK_STATE(WSTATE_MODAL)) {
3229 WObjDescriptor *desc;
3231 if (event->xbutton.window != wwin->frame->titlebar->window
3232 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3233 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3234 GrabModeAsync, GrabModeAsync, None,
3235 None, CurrentTime)!=GrabSuccess) {
3236 #ifdef DEBUG0
3237 wwarning("pointer grab failed for window move");
3238 #endif
3239 return;
3242 OpenWindowMenu(wwin, event->xbutton.x_root,
3243 wwin->frame_y+wwin->frame->top_width, False);
3245 /* allow drag select */
3246 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3247 event->xany.send_event = True;
3248 (*desc->handle_mousedown)(desc, event);
3250 XUngrabPointer(dpy, CurrentTime);
3256 static void
3257 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3259 WWindow *wwin = data;
3261 event->xbutton.state &= ValidModMask;
3263 CloseWindowMenu(wwin->screen_ptr);
3265 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3266 return;
3268 /* if control-click, kill the client */
3269 if (event->xbutton.state & ControlMask) {
3270 wClientKill(wwin);
3271 } else {
3272 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
3273 /* send delete message */
3274 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3280 static void
3281 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3283 WWindow *wwin = data;
3285 CloseWindowMenu(wwin->screen_ptr);
3287 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3288 return;
3290 /* send delete message */
3291 if (wwin->protocols.DELETE_WINDOW) {
3292 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3293 } else {
3294 wClientKill(wwin);
3299 #ifdef XKB_BUTTON_HINT
3300 static void
3301 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3303 WWindow *wwin = data;
3304 WFrameWindow *fwin = wwin->frame;
3305 WScreen *scr = fwin->screen_ptr;
3306 XkbStateRec staterec;
3307 int tl;
3309 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3310 return;
3311 tl = wwin->frame->languagemode;
3312 wwin->frame->languagemode = wwin->frame->last_languagemode;
3313 wwin->frame->last_languagemode = tl;
3314 wSetFocusTo(scr, wwin);
3315 wwin->frame->languagebutton_image =
3316 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3317 wwin->frame->languagemode];
3318 wFrameWindowUpdateLanguageButton(wwin->frame);
3319 if (event->xbutton.button == Button3)
3320 return;
3321 wRaiseFrame(fwin->core);
3323 #endif
3326 static void
3327 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3329 WWindow *wwin = data;
3331 event->xbutton.state &= ValidModMask;
3333 CloseWindowMenu(wwin->screen_ptr);
3335 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3336 return;
3338 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3339 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3340 LastTimestamp);
3341 } else {
3342 WApplication *wapp;
3343 if ((event->xbutton.state & ControlMask) ||
3344 (event->xbutton.button == Button3)) {
3346 wapp = wApplicationOf(wwin->main_window);
3347 if (wapp && !WFLAGP(wwin, no_appicon))
3348 wHideApplication(wapp);
3349 } else if (event->xbutton.state==0) {
3350 wIconifyWindow(wwin);