Shortcuts for moving windows between workspaces.
[wmaker-crm.git] / src / window.c
blobaffafcfbe28afad99f0df93ee5c1411bf0d999d7
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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
29 #ifdef KEEP_XKB_LOCK_STATUS
30 #include <X11/XKBlib.h>
31 #endif /* KEEP_XKB_LOCK_STATUS */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <math.h>
38 /* For getting mouse wheel mappings from WINGs */
39 #include <WINGs/WINGs.h>
41 #include "WindowMaker.h"
42 #include "GNUstep.h"
43 #include "wcore.h"
44 #include "framewin.h"
45 #include "texture.h"
46 #include "window.h"
47 #include "winspector.h"
48 #include "icon.h"
49 #include "properties.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "colormap.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
59 #include "appmenu.h"
60 #include "appicon.h"
61 #include "superfluous.h"
62 #include "rootmenu.h"
64 #ifdef MWM_HINTS
65 # include "motif.h"
66 #endif
67 #include "wmspec.h"
69 #define MOD_MASK wPreferences.modifier_mask
71 /****** Global Variables ******/
72 extern WShortKey wKeyBindings[WKBD_LAST];
74 #ifdef SHAPE
75 extern Bool wShapeSupported;
76 #endif
78 /* contexts */
79 extern XContext wWinContext;
81 /* protocol atoms */
82 extern Atom _XA_WM_DELETE_WINDOW;
83 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
84 extern Atom _XA_WINDOWMAKER_STATE;
85 extern WPreferences wPreferences;
86 extern Time LastTimestamp;
88 /***** Local Stuff *****/
89 static WWindowState *windowState = NULL;
90 static FocusMode getFocusMode(WWindow *wwin);
91 static int getSavedState(Window window, WSavedState **state);
92 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
94 /* frame window (during window grabs) */
95 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
97 /* close button */
98 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
99 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
101 /* iconify button */
102 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
104 #ifdef XKB_BUTTON_HINT
105 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
106 #endif
108 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
109 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
110 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
112 static void release_wwindowstate(WWindowState *wstate);
114 /****** Notification Observers ******/
116 static void appearanceObserver(void *self, WMNotification * notif)
118 WWindow *wwin = (WWindow *) self;
119 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
121 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
122 return;
124 if (flags & WFontSettings) {
125 wWindowConfigureBorders(wwin);
126 if (wwin->flags.shaded) {
127 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
128 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
129 wWindowSynthConfigureNotify(wwin);
132 if (flags & WTextureSettings)
133 wwin->frame->flags.need_texture_remake = 1;
135 if (flags & (WTextureSettings | WColorSettings)) {
136 if (wwin->frame->titlebar)
137 XClearWindow(dpy, wwin->frame->titlebar->window);
139 wFrameWindowPaint(wwin->frame);
144 /* Return the WWindow associated with a given (Xlib) Window. */
145 WWindow *wWindowFor(Window window)
147 WObjDescriptor *desc;
149 if (window == None)
150 return NULL;
152 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
153 return NULL;
155 if (desc->parent_type == WCLASS_WINDOW)
156 return desc->parent;
157 else if (desc->parent_type == WCLASS_FRAME) {
158 WFrameWindow *frame = (WFrameWindow *) desc->parent;
159 if (frame->flags.is_client_window_frame)
160 return frame->child;
163 return NULL;
166 WWindow *wWindowCreate(void)
168 WWindow *wwin;
170 wwin = wmalloc(sizeof(WWindow));
171 wretain(wwin);
173 wwin->client_descriptor.handle_mousedown = frameMouseDown;
174 wwin->client_descriptor.parent = wwin;
175 wwin->client_descriptor.self = wwin;
176 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
178 return wwin;
181 void wWindowDestroy(WWindow *wwin)
183 int i;
185 if (wwin->screen_ptr->cmap_window == wwin)
186 wwin->screen_ptr->cmap_window = NULL;
188 WMRemoveNotificationObserver(wwin);
190 wwin->flags.destroyed = 1;
192 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
193 if (!wwin->screen_ptr->shortcutWindows[i])
194 continue;
196 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
198 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
199 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
200 wwin->screen_ptr->shortcutWindows[i] = NULL;
204 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
205 wwin->fake_group->retainCount--;
206 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
207 XDestroyWindow(dpy, wwin->fake_group->leader);
208 wwin->fake_group->leader = None;
209 wwin->fake_group->origLeader = None;
210 XFlush(dpy);
214 if (wwin->normal_hints)
215 XFree(wwin->normal_hints);
217 if (wwin->wm_hints)
218 XFree(wwin->wm_hints);
220 if (wwin->wm_instance)
221 XFree(wwin->wm_instance);
223 if (wwin->wm_class)
224 XFree(wwin->wm_class);
226 if (wwin->wm_gnustep_attr)
227 wfree(wwin->wm_gnustep_attr);
229 if (wwin->cmap_windows)
230 XFree(wwin->cmap_windows);
232 XDeleteContext(dpy, wwin->client_win, wWinContext);
234 if (wwin->frame)
235 wFrameWindowDestroy(wwin->frame);
237 if (wwin->icon) {
238 RemoveFromStackList(wwin->icon->core);
239 wIconDestroy(wwin->icon);
240 if (wPreferences.auto_arrange_icons)
241 wArrangeIcons(wwin->screen_ptr, True);
243 if (wwin->net_icon_image)
244 RReleaseImage(wwin->net_icon_image);
246 wrelease(wwin);
249 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
251 if (gs_hints->flags & GSWindowStyleAttr) {
252 if (gs_hints->window_style == WMBorderlessWindowMask) {
253 wwin->client_flags.no_border = 1;
254 wwin->client_flags.no_titlebar = 1;
255 wwin->client_flags.no_closable = 1;
256 wwin->client_flags.no_miniaturizable = 1;
257 wwin->client_flags.no_resizable = 1;
258 wwin->client_flags.no_close_button = 1;
259 wwin->client_flags.no_miniaturize_button = 1;
260 wwin->client_flags.no_resizebar = 1;
261 } else {
262 wwin->client_flags.no_close_button =
263 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
265 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
267 wwin->client_flags.no_miniaturize_button =
268 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
270 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
272 wwin->client_flags.no_resizebar =
273 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
275 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
277 /* these attributes supposedly imply in the existence
278 * of a titlebar */
279 if (gs_hints->window_style & (WMResizableWindowMask |
280 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
281 wwin->client_flags.no_titlebar = 0;
282 } else {
283 wwin->client_flags.no_titlebar =
284 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
288 } else {
289 /* setup the defaults */
290 wwin->client_flags.no_border = 0;
291 wwin->client_flags.no_titlebar = 0;
292 wwin->client_flags.no_closable = 0;
293 wwin->client_flags.no_miniaturizable = 0;
294 wwin->client_flags.no_resizable = 0;
295 wwin->client_flags.no_close_button = 0;
296 wwin->client_flags.no_miniaturize_button = 0;
297 wwin->client_flags.no_resizebar = 0;
299 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
300 wwin->client_flags.no_appicon = 1;
303 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
305 WScreen *scr = wwin->screen_ptr;
307 /* sets global default stuff */
308 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
310 * Decoration setting is done in this precedence (lower to higher)
311 * - use global default in the resource database
312 * - guess some settings
313 * - use GNUstep/external window attributes
314 * - set hints specified for the app in the resource DB
317 WSETUFLAG(wwin, broken_close, 0);
319 if (wwin->protocols.DELETE_WINDOW)
320 WSETUFLAG(wwin, kill_close, 0);
321 else
322 WSETUFLAG(wwin, kill_close, 1);
324 /* transients can't be iconified or maximized */
325 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
326 WSETUFLAG(wwin, no_miniaturizable, 1);
327 WSETUFLAG(wwin, no_miniaturize_button, 1);
330 /* if the window can't be resized, remove the resizebar */
331 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
332 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
333 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
334 WSETUFLAG(wwin, no_resizable, 1);
335 WSETUFLAG(wwin, no_resizebar, 1);
338 /* set GNUstep window attributes */
339 if (wwin->wm_gnustep_attr) {
340 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
342 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
344 *level = wwin->wm_gnustep_attr->window_level;
346 * INT_MIN is the only illegal window level.
348 if (*level == INT_MIN)
349 *level = INT_MIN + 1;
350 } else {
351 /* setup defaults */
352 *level = WMNormalLevel;
354 } else {
355 int tmp_workspace = -1;
356 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
358 #ifdef MWM_HINTS
359 wMWMCheckClientHints(wwin);
360 #endif /* MWM_HINTS */
362 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
364 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
365 * have INT_MIN that means that no window level was requested. -Dan
367 if (tmp_level == INT_MIN) {
368 if (WFLAGP(wwin, floating))
369 *level = WMFloatingLevel;
370 else if (WFLAGP(wwin, sunken))
371 *level = WMSunkenLevel;
372 else
373 *level = WMNormalLevel;
374 } else {
375 *level = tmp_level;
378 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
379 WWindow *transientOwner = wWindowFor(wwin->transient_for);
380 if (transientOwner) {
381 int ownerLevel = transientOwner->frame->core->stacking->window_level;
382 if (ownerLevel > *level)
383 *level = ownerLevel;
387 if (tmp_workspace >= 0)
388 *workspace = tmp_workspace % scr->workspace_count;
392 * Set attributes specified only for that window/class.
393 * This might do duplicate work with the 1st wDefaultFillAttributes().
395 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
396 &wwin->defined_user_flags, False);
398 * Sanity checks for attributes that depend on other attributes
400 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
401 wwin->user_flags.emulate_appicon = 0;
403 if (wwin->main_window != None) {
404 WApplication *wapp = wApplicationOf(wwin->main_window);
405 if (wapp && !wapp->flags.emulated)
406 wwin->user_flags.emulate_appicon = 0;
409 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
410 wwin->user_flags.emulate_appicon = 0;
412 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
413 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
414 wwin->user_flags.sunken = 0;
416 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
418 /* windows that have takefocus=False shouldn't take focus at all */
419 if (wwin->focus_mode == WFM_NO_INPUT)
420 wwin->client_flags.no_focusable = 1;
423 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
425 int w1, h1, w2, h2;
427 w1 = wwin->frame->core->width;
428 h1 = wwin->frame->core->height;
429 w2 = obscured->frame->core->width;
430 h2 = obscured->frame->core->height;
432 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
433 && wwin->frame->workspace != obscured->frame->workspace)
434 return False;
436 if (wwin->frame_x + w1 < obscured->frame_x
437 || wwin->frame_y + h1 < obscured->frame_y
438 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
439 return False;
441 return True;
444 static void fixLeaderProperties(WWindow *wwin)
446 XClassHint *classHint;
447 XWMHints *hints, *clientHints;
448 XWindowAttributes attr;
449 Window leaders[2], window;
450 char **argv, *command;
451 int argc, i, pid;
452 Bool haveCommand;
454 classHint = XAllocClassHint();
455 clientHints = XGetWMHints(dpy, wwin->client_win);
456 pid = wNETWMGetPidForWindow(wwin->client_win);
457 if (pid > 0)
458 haveCommand = GetCommandForPid(pid, &argv, &argc);
459 else
460 haveCommand = False;
462 leaders[0] = wwin->client_leader;
463 leaders[1] = wwin->group_id;
465 if (haveCommand) {
466 command = GetCommandForWindow(wwin->client_win);
467 if (command)
468 wfree(command); /* command already set. nothing to do. */
469 else
470 XSetCommand(dpy, wwin->client_win, argv, argc);
473 for (i = 0; i < 2; i++) {
474 window = leaders[i];
475 if (window) {
476 if (XGetClassHint(dpy, window, classHint) == 0) {
477 classHint->res_name = wwin->wm_instance;
478 classHint->res_class = wwin->wm_class;
479 XSetClassHint(dpy, window, classHint);
481 hints = XGetWMHints(dpy, window);
482 if (hints) {
483 XFree(hints);
484 } else if (clientHints) {
485 /* set window group leader to self */
486 clientHints->window_group = window;
487 clientHints->flags |= WindowGroupHint;
488 XSetWMHints(dpy, window, clientHints);
491 if (haveCommand) {
492 command = GetCommandForWindow(window);
493 if (command)
494 wfree(command); /* command already set. nothing to do. */
495 else
496 XSetCommand(dpy, window, argv, argc);
499 /* Make sure we get notification when this window is destroyed */
500 if (XGetWindowAttributes(dpy, window, &attr))
501 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
505 XFree(classHint);
506 if (clientHints)
507 XFree(clientHints);
508 if (haveCommand)
509 wfree(argv);
512 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
514 XClassHint *classHint;
515 XWMHints *hints;
516 Window leader;
517 int argc;
518 char **argv;
520 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
521 /* set class hint */
522 classHint = XAllocClassHint();
523 classHint->res_name = instance;
524 classHint->res_class = class;
525 XSetClassHint(dpy, leader, classHint);
526 XFree(classHint);
528 /* inherit these from the original leader if available */
529 hints = XGetWMHints(dpy, win);
530 if (!hints) {
531 hints = XAllocWMHints();
532 hints->flags = 0;
534 /* set window group leader to self */
535 hints->window_group = leader;
536 hints->flags |= WindowGroupHint;
537 XSetWMHints(dpy, leader, hints);
538 XFree(hints);
540 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
541 XSetCommand(dpy, leader, argv, argc);
542 XFreeStringList(argv);
545 return leader;
548 static int matchIdentifier(const void *item, const void *cdata)
550 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
554 *----------------------------------------------------------------
555 * wManageWindow--
556 * reparents the window and allocates a descriptor for it.
557 * Window manager hints and other hints are fetched to configure
558 * the window decoration attributes and others. User preferences
559 * for the window are used if available, to configure window
560 * decorations and some behaviour.
561 * If in startup, windows that are override redirect,
562 * unmapped and never were managed and are Withdrawn are not
563 * managed.
565 * Returns:
566 * the new window descriptor
568 * Side effects:
569 * The window is reparented and appropriate notification
570 * is done to the client. Input mask for the window is setup.
571 * The window descriptor is also associated with various window
572 * contexts and inserted in the head of the window list.
573 * Event handler contexts are associated for some objects
574 * (buttons, titlebar and resizebar)
576 *----------------------------------------------------------------
578 WWindow *wManageWindow(WScreen *scr, Window window)
580 WWindow *wwin;
581 int x, y;
582 unsigned width, height;
583 XWindowAttributes wattribs;
584 XSetWindowAttributes attribs;
585 WWindowState *win_state;
586 WWindow *transientOwner = NULL;
587 int window_level;
588 int wm_state;
589 int foo;
590 int workspace = -1;
591 char *title;
592 Bool withdraw = False;
593 Bool raise = False;
595 /* mutex. */
596 XGrabServer(dpy);
597 XSync(dpy, False);
599 /* make sure the window is still there */
600 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
601 XUngrabServer(dpy);
602 return NULL;
605 /* if it's an override-redirect, ignore it */
606 if (wattribs.override_redirect) {
607 XUngrabServer(dpy);
608 return NULL;
611 wm_state = PropGetWindowState(window);
613 /* if it's startup and the window is unmapped, don't manage it */
614 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
615 XUngrabServer(dpy);
616 return NULL;
619 wwin = wWindowCreate();
621 title = wNETWMGetWindowName(window);
622 if (title)
623 wwin->flags.net_has_title = 1;
624 else if (!wFetchName(dpy, window, &title))
625 title = NULL;
627 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
629 #ifdef SHAPE
630 if (wShapeSupported) {
631 int junk;
632 unsigned int ujunk;
633 int b_shaped;
635 XShapeSelectInput(dpy, window, ShapeNotifyMask);
636 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
637 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
638 wwin->flags.shaped = b_shaped;
640 #endif
642 /* Get hints and other information in properties */
643 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
645 /* setup descriptor */
646 wwin->client_win = window;
647 wwin->screen_ptr = scr;
648 wwin->old_border_width = wattribs.border_width;
649 wwin->event_mask = CLIENT_EVENTS;
650 attribs.event_mask = CLIENT_EVENTS;
651 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
652 attribs.save_under = False;
654 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
655 XSetWindowBorderWidth(dpy, window, 0);
657 /* get hints from GNUstep app */
658 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
659 wwin->flags.is_gnustep = 1;
661 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
662 wwin->wm_gnustep_attr = NULL;
664 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
665 wwin->flags.is_dockapp = 1;
666 withdraw = True;
669 wwin->client_leader = PropGetClientLeader(window);
670 if (wwin->client_leader != None)
671 wwin->main_window = wwin->client_leader;
673 wwin->wm_hints = XGetWMHints(dpy, window);
675 if (wwin->wm_hints) {
676 if (wwin->wm_hints->flags & StateHint) {
677 if (wwin->wm_hints->initial_state == IconicState) {
678 wwin->flags.miniaturized = 1;
679 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
680 wwin->flags.is_dockapp = 1;
681 withdraw = True;
685 if (wwin->wm_hints->flags & WindowGroupHint) {
686 wwin->group_id = wwin->wm_hints->window_group;
687 /* window_group has priority over CLIENT_LEADER */
688 wwin->main_window = wwin->group_id;
689 } else {
690 wwin->group_id = None;
693 if (wwin->wm_hints->flags & UrgencyHint) {
694 wwin->flags.urgent = 1;
695 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
697 } else {
698 wwin->group_id = None;
701 PropGetProtocols(window, &wwin->protocols);
703 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
704 wwin->transient_for = None;
705 } else {
706 if (wwin->transient_for == None || wwin->transient_for == window) {
707 wwin->transient_for = scr->root_win;
708 } else {
709 transientOwner = wWindowFor(wwin->transient_for);
710 if (transientOwner && transientOwner->main_window != None)
711 wwin->main_window = transientOwner->main_window;
715 /* guess the focus mode */
716 wwin->focus_mode = getFocusMode(wwin);
718 /* get geometry stuff */
719 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
721 /* get colormap windows */
722 GetColormapWindows(wwin);
725 * Setup the decoration/window attributes and
726 * geometry
728 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
730 /* Make broken apps behave as a nice app. */
731 if (WFLAGP(wwin, emulate_appicon))
732 wwin->main_window = wwin->client_win;
734 fixLeaderProperties(wwin);
736 wwin->orig_main_window = wwin->main_window;
738 if (wwin->flags.is_gnustep)
739 WSETUFLAG(wwin, shared_appicon, 0);
741 if (wwin->main_window) {
742 extern Atom _XA_WINDOWMAKER_MENU;
743 XTextProperty text_prop;
745 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
746 WSETUFLAG(wwin, shared_appicon, 0);
749 if (wwin->flags.is_dockapp)
750 WSETUFLAG(wwin, shared_appicon, 0);
752 if (wwin->main_window) {
753 WApplication *app = wApplicationOf(wwin->main_window);
754 if (app && app->app_icon)
755 WSETUFLAG(wwin, shared_appicon, 0);
758 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
759 char *buffer, *instance, *class;
760 WFakeGroupLeader *fPtr;
761 int index;
763 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
765 /* // only enter here if PropGetWMClass() succeds */
766 PropGetWMClass(wwin->main_window, &class, &instance);
767 buffer = StrConcatDot(instance, class);
769 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
770 if (index != WANotFound) {
771 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
772 if (fPtr->retainCount == 0)
773 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
774 instance, class);
776 fPtr->retainCount++;
777 if (fPtr->origLeader == None) {
778 if (ADEQUATE(wwin->main_window)) {
779 fPtr->retainCount++;
780 fPtr->origLeader = wwin->main_window;
783 wwin->fake_group = fPtr;
784 wwin->main_window = fPtr->leader;
785 wfree(buffer);
786 } else {
787 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
789 fPtr->identifier = buffer;
790 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
791 fPtr->origLeader = None;
792 fPtr->retainCount = 1;
794 WMAddToArray(scr->fakeGroupLeaders, fPtr);
796 if (ADEQUATE(wwin->main_window)) {
797 fPtr->retainCount++;
798 fPtr->origLeader = wwin->main_window;
800 wwin->fake_group = fPtr;
801 wwin->main_window = fPtr->leader;
804 if (instance)
805 free(instance);
807 if (class)
808 free(class);
809 #undef ADEQUATE
813 * Setup the initial state of the window
815 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
816 wwin->flags.miniaturized = 1;
818 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
819 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
821 wNETWMCheckInitialClientState(wwin);
823 /* apply previous state if it exists and we're in startup */
824 if (scr->flags.startup && wm_state >= 0) {
825 if (wm_state == IconicState)
826 wwin->flags.miniaturized = 1;
827 else if (wm_state == WithdrawnState)
828 withdraw = True;
831 /* if there is a saved state (from file), restore it */
832 win_state = NULL;
833 if (wwin->main_window != None)
834 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
835 else
836 win_state = (WWindowState *) wWindowGetSavedState(window);
838 if (win_state && !withdraw) {
839 if (win_state->state->hidden > 0)
840 wwin->flags.hidden = win_state->state->hidden;
842 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
843 wwin->flags.shaded = win_state->state->shaded;
845 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
846 wwin->flags.miniaturized = win_state->state->miniaturized;
848 if (!IS_OMNIPRESENT(wwin)) {
849 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
850 wwin->wm_class);
851 if (w < 0 || w >= scr->workspace_count) {
852 workspace = win_state->state->workspace;
853 if (workspace >= scr->workspace_count)
854 workspace = scr->current_workspace;
855 } else {
856 workspace = w;
858 } else {
859 workspace = scr->current_workspace;
863 /* if we're restarting, restore saved state (from hints).
864 * This will overwrite previous */
866 WSavedState *wstate;
868 if (getSavedState(window, &wstate)) {
869 wwin->flags.shaded = wstate->shaded;
870 wwin->flags.hidden = wstate->hidden;
871 wwin->flags.miniaturized = wstate->miniaturized;
872 wwin->flags.maximized = wstate->maximized;
873 if (wwin->flags.maximized) {
874 wwin->old_geometry.x = wstate->x;
875 wwin->old_geometry.y = wstate->y;
876 wwin->old_geometry.width = wstate->w;
877 wwin->old_geometry.height = wstate->h;
880 workspace = wstate->workspace;
881 } else {
882 wstate = NULL;
885 /* restore window shortcut */
886 if (wstate != NULL || win_state != NULL) {
887 unsigned mask = 0;
889 if (win_state != NULL)
890 mask = win_state->state->window_shortcuts;
892 if (wstate != NULL && mask == 0)
893 mask = wstate->window_shortcuts;
895 if (mask > 0) {
896 int i;
898 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
899 if (mask & (1 << i)) {
900 if (!scr->shortcutWindows[i])
901 scr->shortcutWindows[i] = WMCreateArray(4);
903 WMAddToArray(scr->shortcutWindows[i], wwin);
909 if (wstate != NULL)
910 wfree(wstate);
913 /* don't let transients start miniaturized if their owners are not */
914 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
915 wwin->flags.miniaturized = 0;
916 if (wwin->wm_hints)
917 wwin->wm_hints->initial_state = NormalState;
920 /* set workspace on which the window starts */
921 if (workspace >= 0) {
922 if (workspace > scr->workspace_count - 1)
923 workspace = workspace % scr->workspace_count;
924 } else {
925 int w;
927 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
929 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
930 workspace = w;
931 } else {
932 if (wPreferences.open_transients_with_parent && transientOwner)
933 workspace = transientOwner->frame->workspace;
934 else
935 workspace = scr->current_workspace;
939 /* setup window geometry */
940 if (win_state && win_state->state->w > 0) {
941 width = win_state->state->w;
942 height = win_state->state->h;
945 wWindowConstrainSize(wwin, &width, &height);
947 /* do not ask for window placement if the window is
948 * transient, during startup, if the initial workspace is another one
949 * or if the window wants to start iconic.
950 * If geometry was saved, restore it. */
952 Bool dontBring = False;
954 if (win_state && win_state->state->w > 0) {
955 x = win_state->state->x;
956 y = win_state->state->y;
957 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
958 && !scr->flags.startup
959 && workspace == scr->current_workspace
960 && !wwin->flags.miniaturized
961 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
963 if (transientOwner && transientOwner->flags.mapped) {
964 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
965 WMRect rect;
966 int head;
968 x = transientOwner->frame_x +
969 abs((transientOwner->frame->core->width - width) / 2) + offs;
970 y = transientOwner->frame_y +
971 abs((transientOwner->frame->core->height - height) / 3) + offs;
973 /* limit transient windows to be inside their parent's head */
974 rect.pos.x = transientOwner->frame_x;
975 rect.pos.y = transientOwner->frame_y;
976 rect.size.width = transientOwner->frame->core->width;
977 rect.size.height = transientOwner->frame->core->height;
979 head = wGetHeadForRect(scr, rect);
980 rect = wGetRectForHead(scr, head);
982 if (x < rect.pos.x)
983 x = rect.pos.x;
984 else if (x + width > rect.pos.x + rect.size.width)
985 x = rect.pos.x + rect.size.width - width;
987 if (y < rect.pos.y)
988 y = rect.pos.y;
989 else if (y + height > rect.pos.y + rect.size.height)
990 y = rect.pos.y + rect.size.height - height;
992 } else {
993 PlaceWindow(wwin, &x, &y, width, height);
996 if (wPreferences.window_placement == WPM_MANUAL)
997 dontBring = True;
999 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1000 int head, flags;
1001 WMRect rect;
1002 int reposition = 0;
1004 /* Make spash screens come out in the center of a head
1005 * trouble is that most splashies never get here
1006 * they are managed trough atoms but god knows where.
1007 * Dan, do you know ? -peter
1009 * Most of them are not managed, they have set
1010 * OverrideRedirect, which means we can't do anything about
1011 * them. -alfredo */
1013 /* xinerama checks for: across head and dead space */
1014 rect.pos.x = x;
1015 rect.pos.y = y;
1016 rect.size.width = width;
1017 rect.size.height = height;
1019 head = wGetRectPlacementInfo(scr, rect, &flags);
1021 if (flags & XFLAG_DEAD)
1022 reposition = 1;
1024 if (flags & XFLAG_MULTIPLE)
1025 reposition = 2;
1028 switch (reposition) {
1029 case 1:
1030 head = wGetHeadForPointerLocation(scr);
1031 rect = wGetRectForHead(scr, head);
1033 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1034 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1035 break;
1037 case 2:
1038 rect = wGetRectForHead(scr, head);
1040 if (x < rect.pos.x)
1041 x = rect.pos.x;
1042 else if (x + width > rect.pos.x + rect.size.width)
1043 x = rect.pos.x + rect.size.width - width;
1045 if (y < rect.pos.y)
1046 y = rect.pos.y;
1047 else if (y + height > rect.pos.y + rect.size.height)
1048 y = rect.pos.y + rect.size.height - height;
1050 break;
1052 default:
1053 break;
1057 if (WFLAGP(wwin, dont_move_off) && dontBring)
1058 wScreenBringInside(scr, &x, &y, width, height);
1061 wNETWMPositionSplash(wwin, &x, &y, width, height);
1063 if (wwin->flags.urgent) {
1064 if (!IS_OMNIPRESENT(wwin))
1065 wwin->flags.omnipresent ^= 1;
1068 /* Create frame, borders and do reparenting */
1069 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1070 #ifdef XKB_BUTTON_HINT
1071 if (wPreferences.modelock)
1072 foo |= WFF_LANGUAGE_BUTTON;
1073 #endif
1074 if (HAS_TITLEBAR(wwin))
1075 foo |= WFF_TITLEBAR;
1077 if (HAS_RESIZEBAR(wwin))
1078 foo |= WFF_RESIZEBAR;
1080 if (HAS_BORDER(wwin))
1081 foo |= WFF_BORDER;
1083 wwin->frame = wFrameWindowCreate(scr, window_level,
1084 x, y, width, height,
1085 &wPreferences.window_title_clearance,
1086 &wPreferences.window_title_min_height,
1087 &wPreferences.window_title_max_height,
1088 foo,
1089 scr->window_title_texture,
1090 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1091 wattribs.depth, wattribs.visual, wattribs.colormap);
1093 wwin->frame->flags.is_client_window_frame = 1;
1094 wwin->frame->flags.justification = wPreferences.title_justification;
1096 /* setup button images */
1097 wWindowUpdateButtonImages(wwin);
1099 /* hide unused buttons */
1100 foo = 0;
1101 if (WFLAGP(wwin, no_close_button))
1102 foo |= WFF_RIGHT_BUTTON;
1104 if (WFLAGP(wwin, no_miniaturize_button))
1105 foo |= WFF_LEFT_BUTTON;
1107 #ifdef XKB_BUTTON_HINT
1108 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1109 foo |= WFF_LANGUAGE_BUTTON;
1110 #endif
1111 if (foo != 0)
1112 wFrameWindowHideButton(wwin->frame, foo);
1114 wwin->frame->child = wwin;
1115 wwin->frame->workspace = workspace;
1116 wwin->frame->on_click_left = windowIconifyClick;
1118 #ifdef XKB_BUTTON_HINT
1119 if (wPreferences.modelock)
1120 wwin->frame->on_click_language = windowLanguageClick;
1121 #endif
1123 wwin->frame->on_click_right = windowCloseClick;
1124 wwin->frame->on_dblclick_right = windowCloseDblClick;
1125 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1126 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1127 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1129 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1130 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1131 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1134 int gx, gy;
1136 wClientGetGravityOffsets(wwin, &gx, &gy);
1138 /* if gravity is to the south, account for the border sizes */
1139 if (gy > 0)
1140 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1144 * wWindowConfigure() will init the client window's size
1145 * (wwin->client.{width,height}) and all other geometry
1146 * related variables (frame_x,frame_y) */
1147 wWindowConfigure(wwin, x, y, width, height);
1149 /* to make sure the window receives it's new position after reparenting */
1150 wWindowSynthConfigureNotify(wwin);
1152 /* Setup descriptors and save window to internal lists */
1153 if (wwin->main_window != None) {
1154 WApplication *app;
1155 WWindow *leader;
1157 /* Leader windows do not necessary set themselves as leaders.
1158 * If this is the case, point the leader of this window to
1159 * itself */
1160 leader = wWindowFor(wwin->main_window);
1161 if (leader && leader->main_window == None)
1162 leader->main_window = leader->client_win;
1164 app = wApplicationCreate(wwin);
1165 if (app) {
1166 app->last_workspace = workspace;
1168 /* Do application specific stuff, like setting application
1169 * wide attributes. */
1171 if (wwin->flags.hidden) {
1172 /* if the window was set to hidden because it was hidden
1173 * in a previous incarnation and that state was restored */
1174 app->flags.hidden = 1;
1175 } else if (app->flags.hidden) {
1176 if (WFLAGP(app->main_window_desc, start_hidden)) {
1177 wwin->flags.hidden = 1;
1178 } else {
1179 wUnhideApplication(app, False, False);
1180 raise = True;
1183 wAppBounce(app);
1187 /* setup the frame descriptor */
1188 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1189 wwin->frame->core->descriptor.parent = wwin;
1190 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1192 /* don't let windows go away if we die */
1193 XAddToSaveSet(dpy, window);
1195 XLowerWindow(dpy, window);
1197 /* if window is in this workspace and should be mapped, then map it */
1198 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1199 && !wwin->flags.hidden && !withdraw) {
1201 /* The following "if" is to avoid crashing of clients that expect
1202 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1203 * after the return from this function. */
1204 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1205 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1206 else
1207 wClientSetState(wwin, NormalState, None);
1209 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1210 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1212 * The brain damaged idiotic non-click to focus modes will
1213 * have trouble with this because:
1215 * 1. window is created and mapped by the client
1216 * 2. window is mapped by wmaker in small size
1217 * 3. window is animated to grow to normal size
1218 * 4. this function returns to normal event loop
1219 * 5. eventually, the EnterNotify event that would trigger
1220 * the window focusing (if the mouse is over that window)
1221 * will be processed by wmaker.
1222 * But since this event will be rather delayed
1223 * (step 3 has a large delay) the time when the event ocurred
1224 * and when it is processed, the client that owns that window
1225 * will reject the XSetInputFocus() for it.
1227 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1228 DoWindowBirth(wwin);
1230 wWindowMap(wwin);
1233 /* setup stacking descriptor */
1234 if (transientOwner)
1235 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1236 else
1237 wwin->frame->core->stacking->child_of = NULL;
1239 if (!scr->focused_window) {
1240 /* first window on the list */
1241 wwin->next = NULL;
1242 wwin->prev = NULL;
1243 scr->focused_window = wwin;
1244 } else {
1245 WWindow *tmp;
1247 /* add window at beginning of focus window list */
1248 tmp = scr->focused_window;
1249 while (tmp->prev)
1250 tmp = tmp->prev;
1252 tmp->prev = wwin;
1253 wwin->next = tmp;
1254 wwin->prev = NULL;
1257 /* raise is set to true if we un-hid the app when this window was born.
1258 * we raise, else old windows of this app will be above this new one. */
1259 if (raise)
1260 wRaiseFrame(wwin->frame->core);
1262 /* Update name must come after WApplication stuff is done */
1263 wWindowUpdateName(wwin, title);
1264 if (title)
1265 XFree(title);
1267 XUngrabServer(dpy);
1269 /* Final preparations before window is ready to go */
1270 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1272 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1273 if (((transientOwner && transientOwner->flags.focused)
1274 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1276 /* only auto_focus if on same screen as mouse
1277 * (and same head for xinerama mode)
1278 * TODO: make it an option */
1280 /*TODO add checking the head of the window, is it available? */
1281 short same_screen = 0, same_head = 1;
1283 int foo;
1284 unsigned int bar;
1285 Window dummy;
1287 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1288 &foo, &foo, &foo, &foo, &bar) != False)
1289 same_screen = 1;
1291 if (same_screen == 1 && same_head == 1)
1292 wSetFocusTo(scr, wwin);
1295 wWindowResetMouseGrabs(wwin);
1297 if (!WFLAGP(wwin, no_bind_keys))
1298 wWindowSetKeyGrabs(wwin);
1300 WMPostNotificationName(WMNManaged, wwin, NULL);
1301 wColormapInstallForWindow(scr, scr->cmap_window);
1303 /* Setup Notification Observers */
1304 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1306 /* Cleanup temporary stuff */
1307 if (win_state)
1308 wWindowDeleteSavedState(win_state);
1310 /* If the window must be withdrawed, then do it now.
1311 * Must do some optimization, 'though */
1312 if (withdraw) {
1313 wwin->flags.mapped = 0;
1314 wClientSetState(wwin, WithdrawnState, None);
1315 wUnmanageWindow(wwin, True, False);
1316 wwin = NULL;
1319 return wwin;
1322 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1323 char *title, int x, int y, int width, int height)
1325 WWindow *wwin;
1326 int foo;
1328 wwin = wWindowCreate();
1330 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1332 wwin->flags.internal_window = 1;
1334 WSETUFLAG(wwin, omnipresent, 1);
1335 WSETUFLAG(wwin, no_shadeable, 1);
1336 WSETUFLAG(wwin, no_resizable, 1);
1337 WSETUFLAG(wwin, no_miniaturizable, 1);
1339 wwin->focus_mode = WFM_PASSIVE;
1340 wwin->client_win = window;
1341 wwin->screen_ptr = scr;
1342 wwin->transient_for = owner;
1343 wwin->client.x = x;
1344 wwin->client.y = y;
1345 wwin->client.width = width;
1346 wwin->client.height = height;
1347 wwin->frame_x = wwin->client.x;
1348 wwin->frame_y = wwin->client.y;
1350 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1351 foo |= WFF_TITLEBAR;
1352 #ifdef XKB_BUTTON_HINT
1353 foo |= WFF_LANGUAGE_BUTTON;
1354 #endif
1356 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1357 wwin->frame_x, wwin->frame_y,
1358 width, height,
1359 &wPreferences.window_title_clearance,
1360 &wPreferences.window_title_min_height,
1361 &wPreferences.window_title_max_height,
1362 foo,
1363 scr->window_title_texture,
1364 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1365 scr->w_depth, scr->w_visual, scr->w_colormap);
1367 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1369 wwin->frame->flags.is_client_window_frame = 1;
1370 wwin->frame->flags.justification = wPreferences.title_justification;
1372 wFrameWindowChangeTitle(wwin->frame, title);
1374 /* setup button images */
1375 wWindowUpdateButtonImages(wwin);
1377 /* hide buttons */
1378 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1380 wwin->frame->child = wwin;
1381 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1383 #ifdef XKB_BUTTON_HINT
1384 if (wPreferences.modelock)
1385 wwin->frame->on_click_language = windowLanguageClick;
1386 #endif
1388 wwin->frame->on_click_right = windowCloseClick;
1389 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1390 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1391 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1392 wwin->client.y += wwin->frame->top_width;
1394 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1395 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1397 /* setup the frame descriptor */
1398 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1399 wwin->frame->core->descriptor.parent = wwin;
1400 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1402 XLowerWindow(dpy, window);
1403 XMapSubwindows(dpy, wwin->frame->core->window);
1405 /* setup stacking descriptor */
1406 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1407 WWindow *tmp;
1408 tmp = wWindowFor(wwin->transient_for);
1409 if (tmp)
1410 wwin->frame->core->stacking->child_of = tmp->frame->core;
1411 } else {
1412 wwin->frame->core->stacking->child_of = NULL;
1415 if (!scr->focused_window) {
1416 /* first window on the list */
1417 wwin->next = NULL;
1418 wwin->prev = NULL;
1419 scr->focused_window = wwin;
1420 } else {
1421 WWindow *tmp;
1423 /* add window at beginning of focus window list */
1424 tmp = scr->focused_window;
1425 while (tmp->prev)
1426 tmp = tmp->prev;
1427 tmp->prev = wwin;
1428 wwin->next = tmp;
1429 wwin->prev = NULL;
1432 if (wwin->flags.is_gnustep == 0)
1433 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1435 /* if (wPreferences.auto_focus) */
1436 wSetFocusTo(scr, wwin);
1437 wWindowResetMouseGrabs(wwin);
1438 wWindowSetKeyGrabs(wwin);
1440 return wwin;
1444 *----------------------------------------------------------------------
1445 * wUnmanageWindow--
1446 * Removes the frame window from a window and destroys all data
1447 * related to it. The window will be reparented back to the root window
1448 * if restore is True.
1450 * Side effects:
1451 * Everything related to the window is destroyed and the window
1452 * is removed from the window lists. Focus is set to the previous on the
1453 * window list.
1454 *----------------------------------------------------------------------
1456 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1458 WCoreWindow *frame = wwin->frame->core;
1459 WWindow *owner = NULL;
1460 WWindow *newFocusedWindow = NULL;
1461 int wasFocused;
1462 WScreen *scr = wwin->screen_ptr;
1464 /* First close attribute editor window if open */
1465 if (wwin->flags.inspector_open)
1466 wCloseInspectorForWindow(wwin);
1468 /* Close window menu if it's open for this window */
1469 if (wwin->flags.menu_open_for_me)
1470 CloseWindowMenu(scr);
1472 if (!destroyed) {
1473 if (!wwin->flags.internal_window)
1474 XRemoveFromSaveSet(dpy, wwin->client_win);
1476 /* If this is a leader window, we still need to listen for
1477 * DestroyNotify and PropertyNotify. */
1478 if (wApplicationOf(wwin->client_win))
1479 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1480 else
1481 XSelectInput(dpy, wwin->client_win, NoEventMask);
1483 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1484 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1487 XUnmapWindow(dpy, frame->window);
1489 XUnmapWindow(dpy, wwin->client_win);
1491 /* deselect window */
1492 wSelectWindow(wwin, False);
1494 /* remove all pending events on window */
1495 /* I think this only matters for autoraise */
1496 if (wPreferences.raise_delay)
1497 WMDeleteTimerWithClientData(wwin->frame->core);
1499 XFlush(dpy);
1501 /* reparent the window back to the root */
1502 if (restore)
1503 wClientRestore(wwin);
1505 if (wwin->transient_for != scr->root_win) {
1506 owner = wWindowFor(wwin->transient_for);
1507 if (owner) {
1508 if (!owner->flags.semi_focused)
1509 owner = NULL;
1510 else
1511 owner->flags.semi_focused = 0;
1515 wasFocused = wwin->flags.focused;
1517 /* remove from window focus list */
1518 if (!wwin->prev && !wwin->next) {
1519 /* was the only window */
1520 scr->focused_window = NULL;
1521 newFocusedWindow = NULL;
1522 } else {
1523 WWindow *tmp;
1525 if (wwin->prev)
1526 wwin->prev->next = wwin->next;
1528 if (wwin->next)
1529 wwin->next->prev = wwin->prev;
1530 else {
1531 scr->focused_window = wwin->prev;
1532 scr->focused_window->next = NULL;
1535 if (wPreferences.focus_mode == WKF_CLICK) {
1537 /* if in click to focus mode and the window
1538 * was a transient, focus the owner window
1540 tmp = NULL;
1541 if (wPreferences.focus_mode == WKF_CLICK) {
1542 tmp = wWindowFor(wwin->transient_for);
1543 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1544 tmp = NULL;
1547 /* otherwise, focus the next one in the focus list */
1548 if (!tmp) {
1549 tmp = scr->focused_window;
1550 while (tmp) { /* look for one in the window list first */
1551 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1552 && (tmp->flags.mapped || tmp->flags.shaded))
1553 break;
1554 tmp = tmp->prev;
1556 if (!tmp) { /* if unsuccessful, choose any focusable window */
1557 tmp = scr->focused_window;
1558 while (tmp) {
1559 if (!WFLAGP(tmp, no_focusable)
1560 && (tmp->flags.mapped || tmp->flags.shaded))
1561 break;
1562 tmp = tmp->prev;
1567 newFocusedWindow = tmp;
1569 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1570 unsigned int mask;
1571 int foo;
1572 Window bar, win;
1574 /* This is to let the root window get the keyboard input
1575 * if Sloppy focus mode and no other window get focus.
1576 * This way keybindings will not freeze.
1578 tmp = NULL;
1579 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1580 tmp = wWindowFor(win);
1581 if (tmp == wwin)
1582 tmp = NULL;
1583 newFocusedWindow = tmp;
1584 } else {
1585 newFocusedWindow = NULL;
1589 if (!wwin->flags.internal_window)
1590 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1591 if (wasFocused) {
1592 if (newFocusedWindow != owner && owner) {
1593 if (wwin->flags.is_gnustep == 0)
1594 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1596 wSetFocusTo(scr, newFocusedWindow);
1599 /* Close menu and unhighlight */
1600 WApplication *oapp = wApplicationOf(wwin->main_window);
1601 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1602 if (oapp && oapp != napp) {
1603 wAppMenuUnmap(oapp->menu);
1604 if (wPreferences.highlight_active_app)
1605 wApplicationDeactivate(oapp);
1608 wNETCleanupFrameExtents(wwin);
1610 wWindowDestroy(wwin);
1611 XFlush(dpy);
1614 void wWindowMap(WWindow *wwin)
1616 XMapWindow(dpy, wwin->frame->core->window);
1617 if (!wwin->flags.shaded) {
1618 /* window will be remapped when getting MapNotify */
1619 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1620 XMapWindow(dpy, wwin->client_win);
1621 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1623 wwin->flags.mapped = 1;
1627 void wWindowUnmap(WWindow *wwin)
1629 wwin->flags.mapped = 0;
1631 /* prevent window withdrawal when getting UnmapNotify */
1632 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1633 XUnmapWindow(dpy, wwin->client_win);
1634 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1636 XUnmapWindow(dpy, wwin->frame->core->window);
1639 void wWindowFocus(WWindow *wwin, WWindow *owin)
1641 WWindow *nowner;
1642 WWindow *oowner;
1644 #ifdef KEEP_XKB_LOCK_STATUS
1645 if (wPreferences.modelock)
1646 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1647 #endif /* KEEP_XKB_LOCK_STATUS */
1649 wwin->flags.semi_focused = 0;
1651 if (wwin->flags.is_gnustep == 0)
1652 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1654 wwin->flags.focused = 1;
1656 wWindowResetMouseGrabs(wwin);
1658 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1660 if (owin == wwin || !owin)
1661 return;
1663 nowner = wWindowFor(wwin->transient_for);
1665 /* new window is a transient for the old window */
1666 if (nowner == owin) {
1667 owin->flags.semi_focused = 1;
1668 wWindowUnfocus(nowner);
1669 return;
1672 oowner = wWindowFor(owin->transient_for);
1674 /* new window is owner of old window */
1675 if (wwin == oowner) {
1676 wWindowUnfocus(owin);
1677 return;
1680 if (!nowner) {
1681 wWindowUnfocus(owin);
1682 return;
1685 /* new window has same owner of old window */
1686 if (oowner == nowner) {
1687 /* prevent unfocusing of owner */
1688 oowner->flags.semi_focused = 0;
1689 wWindowUnfocus(owin);
1690 oowner->flags.semi_focused = 1;
1692 return;
1695 /* nowner != NULL && oowner != nowner */
1696 nowner->flags.semi_focused = 1;
1697 wWindowUnfocus(nowner);
1698 wWindowUnfocus(owin);
1701 void wWindowUnfocus(WWindow *wwin)
1703 CloseWindowMenu(wwin->screen_ptr);
1705 if (wwin->flags.is_gnustep == 0)
1706 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1708 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1709 WWindow *owner;
1710 owner = wWindowFor(wwin->transient_for);
1711 if (owner && owner->flags.semi_focused) {
1712 owner->flags.semi_focused = 0;
1713 if (owner->flags.mapped || owner->flags.shaded) {
1714 wWindowUnfocus(owner);
1715 wFrameWindowPaint(owner->frame);
1719 wwin->flags.focused = 0;
1720 wWindowResetMouseGrabs(wwin);
1721 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1724 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1726 char *title;
1728 if (!wwin->frame)
1729 return;
1731 if (!newTitle)
1732 title = DEF_WINDOW_TITLE; /* the hint was removed */
1733 else
1734 title = newTitle;
1736 if (wFrameWindowChangeTitle(wwin->frame, title))
1737 WMPostNotificationName(WMNChangedName, wwin, NULL);
1741 *----------------------------------------------------------------------
1743 * wWindowConstrainSize--
1744 * Constrains size for the client window, taking the maximal size,
1745 * window resize increments and other size hints into account.
1747 * Returns:
1748 * The closest size to what was given that the client window can
1749 * have.
1751 *----------------------------------------------------------------------
1753 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1755 int width = (int)*nwidth;
1756 int height = (int)*nheight;
1757 int winc = 1;
1758 int hinc = 1;
1759 int minW = 1, minH = 1;
1760 int maxW = wwin->screen_ptr->scr_width * 2;
1761 int maxH = wwin->screen_ptr->scr_height * 2;
1762 int minAX = -1, minAY = -1;
1763 int maxAX = -1, maxAY = -1;
1764 int baseW = 0;
1765 int baseH = 0;
1767 if (wwin->normal_hints) {
1768 winc = wwin->normal_hints->width_inc;
1769 hinc = wwin->normal_hints->height_inc;
1770 minW = wwin->normal_hints->min_width;
1771 minH = wwin->normal_hints->min_height;
1772 maxW = wwin->normal_hints->max_width;
1773 maxH = wwin->normal_hints->max_height;
1774 if (wwin->normal_hints->flags & PAspect) {
1775 minAX = wwin->normal_hints->min_aspect.x;
1776 minAY = wwin->normal_hints->min_aspect.y;
1777 maxAX = wwin->normal_hints->max_aspect.x;
1778 maxAY = wwin->normal_hints->max_aspect.y;
1781 baseW = wwin->normal_hints->base_width;
1782 baseH = wwin->normal_hints->base_height;
1785 if (width < minW)
1786 width = minW;
1787 if (height < minH)
1788 height = minH;
1790 if (width > maxW)
1791 width = maxW;
1792 if (height > maxH)
1793 height = maxH;
1795 /* aspect ratio code borrowed from olwm */
1796 if (minAX > 0) {
1797 /* adjust max aspect ratio */
1798 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1799 if (maxAX > maxAY) {
1800 height = (width * maxAY) / maxAX;
1801 if (height > maxH) {
1802 height = maxH;
1803 width = (height * maxAX) / maxAY;
1805 } else {
1806 width = (height * maxAX) / maxAY;
1807 if (width > maxW) {
1808 width = maxW;
1809 height = (width * maxAY) / maxAX;
1814 /* adjust min aspect ratio */
1815 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1816 if (minAX > minAY) {
1817 height = (width * minAY) / minAX;
1818 if (height < minH) {
1819 height = minH;
1820 width = (height * minAX) / minAY;
1822 } else {
1823 width = (height * minAX) / minAY;
1824 if (width < minW) {
1825 width = minW;
1826 height = (width * minAY) / minAX;
1832 if (baseW != 0)
1833 width = (((width - baseW) / winc) * winc) + baseW;
1834 else
1835 width = (((width - minW) / winc) * winc) + minW;
1837 if (baseH != 0)
1838 height = (((height - baseH) / hinc) * hinc) + baseH;
1839 else
1840 height = (((height - minH) / hinc) * hinc) + minH;
1842 /* broken stupid apps may cause preposterous values for these.. */
1843 if (width > 0)
1844 *nwidth = width;
1845 if (height > 0)
1846 *nheight = height;
1849 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1850 unsigned int *width, unsigned int *height)
1852 int baseW = 0, baseH = 0;
1853 int winc = 1, hinc = 1;
1855 if (wwin->normal_hints) {
1856 baseW = wwin->normal_hints->base_width;
1857 baseH = wwin->normal_hints->base_height;
1859 winc = wwin->normal_hints->width_inc;
1860 hinc = wwin->normal_hints->height_inc;
1863 if (*width > maxW)
1864 *width = maxW - (maxW - baseW) % winc;
1866 if (*height > maxH)
1867 *height = maxH - (maxH - baseH) % hinc;
1870 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1872 WScreen *scr = wwin->screen_ptr;
1873 WApplication *wapp;
1874 int unmap = 0;
1876 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1877 return;
1879 if (workspace != scr->current_workspace) {
1880 /* Sent to other workspace. Unmap window */
1881 if ((wwin->flags.mapped
1882 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1883 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1885 wapp = wApplicationOf(wwin->main_window);
1886 if (wapp)
1887 wapp->last_workspace = workspace;
1889 if (wwin->flags.miniaturized) {
1890 if (wwin->icon) {
1891 XUnmapWindow(dpy, wwin->icon->core->window);
1892 wwin->icon->mapped = 0;
1894 } else {
1895 unmap = 1;
1896 wSetFocusTo(scr, NULL);
1899 } else {
1900 /* brought to current workspace. Map window */
1901 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1902 if (wwin->icon) {
1903 XMapWindow(dpy, wwin->icon->core->window);
1904 wwin->icon->mapped = 1;
1906 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1907 wWindowMap(wwin);
1910 if (!IS_OMNIPRESENT(wwin)) {
1911 int oldWorkspace = wwin->frame->workspace;
1912 wwin->frame->workspace = workspace;
1913 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1916 if (unmap)
1917 wWindowUnmap(wwin);
1920 void wWindowChangeWorkspaceRelative(WWindow *wwin, int amount)
1922 WScreen *scr = wwin->screen_ptr;
1923 int w = scr->current_workspace + amount;
1925 if (amount < 0) {
1926 if (w >= 0) {
1927 wWindowChangeWorkspace(wwin, w);
1928 } else if (wPreferences.ws_cycle) {
1929 wWindowChangeWorkspace(wwin, scr->workspace_count + w);
1931 } else if (amount > 0) {
1932 if (w < scr->workspace_count) {
1933 wWindowChangeWorkspace(wwin, w);
1934 } else if (wPreferences.ws_advance) {
1935 int workspace = WMIN(w, MAX_WORKSPACES - 1);
1936 wWorkspaceMake(scr, workspace);
1937 wWindowChangeWorkspace(wwin, workspace);
1938 } else if (wPreferences.ws_cycle) {
1939 wWindowChangeWorkspace(wwin, w % scr->workspace_count);
1944 void wWindowSynthConfigureNotify(WWindow *wwin)
1946 XEvent sevent;
1948 sevent.type = ConfigureNotify;
1949 sevent.xconfigure.display = dpy;
1950 sevent.xconfigure.event = wwin->client_win;
1951 sevent.xconfigure.window = wwin->client_win;
1953 sevent.xconfigure.x = wwin->client.x;
1954 sevent.xconfigure.y = wwin->client.y;
1955 sevent.xconfigure.width = wwin->client.width;
1956 sevent.xconfigure.height = wwin->client.height;
1958 sevent.xconfigure.border_width = wwin->old_border_width;
1959 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1960 sevent.xconfigure.above = wwin->frame->titlebar->window;
1961 else
1962 sevent.xconfigure.above = None;
1964 sevent.xconfigure.override_redirect = False;
1965 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1966 XFlush(dpy);
1970 *----------------------------------------------------------------------
1971 * wWindowConfigure()
1973 * req_x, req_y: new requested positions for the frame
1974 * req_width, req_height: new requested sizes for the client
1976 * Configures the frame, decorations and client window to the specified
1977 * geometry, whose validity is not checked -- wWindowConstrainSize()
1978 * must be used for that.
1979 * The size parameters are for the client window, but the position is
1980 * for the frame.
1981 * The client window receives a ConfigureNotify event, according
1982 * to what ICCCM says.
1984 * Returns:
1985 * None
1987 * Side effects:
1988 * Window size and position are changed and client window receives
1989 * a ConfigureNotify event.
1990 *----------------------------------------------------------------------
1992 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
1994 int synth_notify = False;
1995 int resize;
1997 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
1999 * if the window is being moved but not resized then
2000 * send a synthetic ConfigureNotify
2002 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2003 synth_notify = True;
2005 if (WFLAGP(wwin, dont_move_off))
2006 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2008 if (resize) {
2009 if (req_width < MIN_WINDOW_SIZE)
2010 req_width = MIN_WINDOW_SIZE;
2011 if (req_height < MIN_WINDOW_SIZE)
2012 req_height = MIN_WINDOW_SIZE;
2014 /* If growing, resize inner part before frame,
2015 * if shrinking, resize frame before.
2016 * This will prevent the frame (that can have a different color)
2017 * to be exposed, causing flicker */
2018 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2019 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2021 if (wwin->flags.shaded) {
2022 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2023 wwin->old_geometry.height = req_height;
2024 } else {
2025 int h;
2027 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2029 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2032 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2033 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2035 wwin->client.x = req_x;
2036 wwin->client.y = req_y + wwin->frame->top_width;
2037 wwin->client.width = req_width;
2038 wwin->client.height = req_height;
2039 } else {
2040 wwin->client.x = req_x;
2041 wwin->client.y = req_y + wwin->frame->top_width;
2043 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2045 wwin->frame_x = req_x;
2046 wwin->frame_y = req_y;
2047 if (HAS_BORDER(wwin)) {
2048 wwin->client.x += wwin->screen_ptr->frame_border_width;
2049 wwin->client.y += wwin->screen_ptr->frame_border_width;
2051 #ifdef SHAPE
2052 if (wShapeSupported && wwin->flags.shaped && resize)
2053 wWindowSetShape(wwin);
2054 #endif
2056 if (synth_notify)
2057 wWindowSynthConfigureNotify(wwin);
2059 wNETFrameExtents(wwin);
2061 XFlush(dpy);
2064 /* req_x, req_y: new position of the frame */
2065 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2067 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2068 int synth_notify = False;
2070 /* Send a synthetic ConfigureNotify event for every window movement. */
2071 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2072 synth_notify = True;
2073 #else
2074 /* A single synthetic ConfigureNotify event is sent at the end of
2075 * a completed (opaque) movement in moveres.c */
2076 #endif
2078 if (WFLAGP(wwin, dont_move_off))
2079 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2080 wwin->frame->core->width, wwin->frame->core->height);
2082 wwin->client.x = req_x;
2083 wwin->client.y = req_y + wwin->frame->top_width;
2084 if (HAS_BORDER(wwin)) {
2085 wwin->client.x += wwin->screen_ptr->frame_border_width;
2086 wwin->client.y += wwin->screen_ptr->frame_border_width;
2089 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2091 wwin->frame_x = req_x;
2092 wwin->frame_y = req_y;
2094 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2095 if (synth_notify)
2096 wWindowSynthConfigureNotify(wwin);
2097 #endif
2100 void wWindowUpdateButtonImages(WWindow *wwin)
2102 WScreen *scr = wwin->screen_ptr;
2103 Pixmap pixmap, mask;
2104 WFrameWindow *fwin = wwin->frame;
2106 if (!HAS_TITLEBAR(wwin))
2107 return;
2109 /* miniaturize button */
2110 if (!WFLAGP(wwin, no_miniaturize_button)) {
2111 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2112 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2114 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2115 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2116 else
2117 mask = None;
2119 if (fwin->lbutton_image
2120 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2121 wPixmapDestroy(fwin->lbutton_image);
2122 fwin->lbutton_image = NULL;
2125 if (!fwin->lbutton_image) {
2126 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2127 fwin->lbutton_image->client_owned = 1;
2128 fwin->lbutton_image->client_owned_mask = 1;
2130 } else {
2131 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2132 wPixmapDestroy(fwin->lbutton_image);
2134 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2137 #ifdef XKB_BUTTON_HINT
2138 if (!WFLAGP(wwin, no_language_button)) {
2139 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2140 wPixmapDestroy(fwin->languagebutton_image);
2142 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2144 #endif
2146 /* close button */
2148 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2149 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2151 if (!WFLAGP(wwin, no_close_button)) {
2152 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2153 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2155 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2156 mask = wwin->wm_gnustep_attr->close_mask;
2157 else
2158 mask = None;
2160 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2161 || fwin->rbutton_image->mask != mask)) {
2162 wPixmapDestroy(fwin->rbutton_image);
2163 fwin->rbutton_image = NULL;
2166 if (!fwin->rbutton_image) {
2167 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2168 fwin->rbutton_image->client_owned = 1;
2169 fwin->rbutton_image->client_owned_mask = 1;
2172 } else if (WFLAGP(wwin, kill_close)) {
2173 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2174 wPixmapDestroy(fwin->rbutton_image);
2176 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2178 } else if (MGFLAGP(wwin, broken_close)) {
2179 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2180 wPixmapDestroy(fwin->rbutton_image);
2182 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2184 } else {
2185 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2186 wPixmapDestroy(fwin->rbutton_image);
2188 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2192 /* force buttons to be redrawn */
2193 fwin->flags.need_texture_change = 1;
2194 wFrameWindowPaint(fwin);
2198 *---------------------------------------------------------------------------
2199 * wWindowConfigureBorders--
2200 * Update window border configuration according to attribute flags.
2202 *---------------------------------------------------------------------------
2204 void wWindowConfigureBorders(WWindow *wwin)
2206 if (wwin->frame) {
2207 int flags;
2208 int newy, oldh;
2210 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2212 #ifdef XKB_BUTTON_HINT
2213 if (wPreferences.modelock)
2214 flags |= WFF_LANGUAGE_BUTTON;
2215 #endif
2217 if (HAS_TITLEBAR(wwin))
2218 flags |= WFF_TITLEBAR;
2219 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2220 flags |= WFF_RESIZEBAR;
2221 if (HAS_BORDER(wwin))
2222 flags |= WFF_BORDER;
2223 if (wwin->flags.shaded)
2224 flags |= WFF_IS_SHADED;
2225 if (wwin->flags.selected)
2226 flags |= WFF_SELECTED;
2228 oldh = wwin->frame->top_width;
2229 wFrameWindowUpdateBorders(wwin->frame, flags);
2230 if (oldh != wwin->frame->top_width) {
2231 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2233 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2234 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2237 flags = 0;
2238 if (!WFLAGP(wwin, no_miniaturize_button)
2239 && wwin->frame->flags.hide_left_button)
2240 flags |= WFF_LEFT_BUTTON;
2242 #ifdef XKB_BUTTON_HINT
2243 if (!WFLAGP(wwin, no_language_button)
2244 && wwin->frame->flags.hide_language_button)
2245 flags |= WFF_LANGUAGE_BUTTON;
2246 #endif
2248 if (!WFLAGP(wwin, no_close_button)
2249 && wwin->frame->flags.hide_right_button)
2250 flags |= WFF_RIGHT_BUTTON;
2252 if (flags != 0) {
2253 wWindowUpdateButtonImages(wwin);
2254 wFrameWindowShowButton(wwin->frame, flags);
2257 flags = 0;
2258 if (WFLAGP(wwin, no_miniaturize_button)
2259 && !wwin->frame->flags.hide_left_button)
2260 flags |= WFF_LEFT_BUTTON;
2262 #ifdef XKB_BUTTON_HINT
2263 if (WFLAGP(wwin, no_language_button)
2264 && !wwin->frame->flags.hide_language_button)
2265 flags |= WFF_LANGUAGE_BUTTON;
2266 #endif
2268 if (WFLAGP(wwin, no_close_button)
2269 && !wwin->frame->flags.hide_right_button)
2270 flags |= WFF_RIGHT_BUTTON;
2272 if (flags != 0)
2273 wFrameWindowHideButton(wwin->frame, flags);
2275 #ifdef SHAPE
2276 if (wShapeSupported && wwin->flags.shaped)
2277 wWindowSetShape(wwin);
2278 #endif
2282 void wWindowSaveState(WWindow * wwin)
2284 long data[10];
2285 int i;
2287 memset(data, 0, sizeof(long) * 10);
2288 data[0] = wwin->frame->workspace;
2289 data[1] = wwin->flags.miniaturized;
2290 data[2] = wwin->flags.shaded;
2291 data[3] = wwin->flags.hidden;
2292 data[4] = wwin->flags.maximized;
2293 if (wwin->flags.maximized == 0) {
2294 data[5] = wwin->frame_x;
2295 data[6] = wwin->frame_y;
2296 data[7] = wwin->frame->core->width;
2297 data[8] = wwin->frame->core->height;
2298 } else {
2299 data[5] = wwin->old_geometry.x;
2300 data[6] = wwin->old_geometry.y;
2301 data[7] = wwin->old_geometry.width;
2302 data[8] = wwin->old_geometry.height;
2305 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2306 if (wwin->screen_ptr->shortcutWindows[i] &&
2307 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2308 data[9] |= 1 << i;
2310 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2311 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2314 static int getSavedState(Window window, WSavedState ** state)
2316 Atom type_ret;
2317 int fmt_ret;
2318 unsigned long nitems_ret;
2319 unsigned long bytes_after_ret;
2320 long *data;
2322 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2323 True, _XA_WINDOWMAKER_STATE,
2324 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2325 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2326 return 0;
2328 *state = wmalloc(sizeof(WSavedState));
2330 (*state)->workspace = data[0];
2331 (*state)->miniaturized = data[1];
2332 (*state)->shaded = data[2];
2333 (*state)->hidden = data[3];
2334 (*state)->maximized = data[4];
2335 (*state)->x = data[5];
2336 (*state)->y = data[6];
2337 (*state)->w = data[7];
2338 (*state)->h = data[8];
2339 (*state)->window_shortcuts = data[9];
2341 XFree(data);
2343 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2344 return 1;
2345 else
2346 return 0;
2349 #ifdef SHAPE
2350 void wWindowClearShape(WWindow * wwin)
2352 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2353 0, wwin->frame->top_width, None, ShapeSet);
2354 XFlush(dpy);
2357 void wWindowSetShape(WWindow * wwin)
2359 XRectangle rect[2];
2360 int count;
2361 #ifdef OPTIMIZE_SHAPE
2362 XRectangle *rects;
2363 XRectangle *urec;
2364 int ordering;
2366 /* only shape is the client's */
2367 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2368 goto alt_code;
2370 /* Get array of rectangles describing the shape mask */
2371 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2372 if (!rects)
2373 goto alt_code;
2375 urec = malloc(sizeof(XRectangle) * (count + 2));
2376 if (!urec) {
2377 XFree(rects);
2378 goto alt_code;
2381 /* insert our decoration rectangles in the rect list */
2382 memcpy(urec, rects, sizeof(XRectangle) * count);
2383 XFree(rects);
2385 if (HAS_TITLEBAR(wwin)) {
2386 urec[count].x = -1;
2387 urec[count].y = -1 - wwin->frame->top_width;
2388 urec[count].width = wwin->frame->core->width + 2;
2389 urec[count].height = wwin->frame->top_width + 1;
2390 count++;
2392 if (HAS_RESIZEBAR(wwin)) {
2393 urec[count].x = -1;
2394 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2395 urec[count].width = wwin->frame->core->width + 2;
2396 urec[count].height = wwin->frame->bottom_width + 1;
2397 count++;
2400 /* shape our frame window */
2401 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2402 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2403 XFlush(dpy);
2404 wfree(urec);
2405 return;
2407 alt_code:
2408 #endif /* OPTIMIZE_SHAPE */
2409 count = 0;
2410 if (HAS_TITLEBAR(wwin)) {
2411 rect[count].x = -1;
2412 rect[count].y = -1;
2413 rect[count].width = wwin->frame->core->width + 2;
2414 rect[count].height = wwin->frame->top_width + 1;
2415 count++;
2417 if (HAS_RESIZEBAR(wwin)) {
2418 rect[count].x = -1;
2419 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2420 rect[count].width = wwin->frame->core->width + 2;
2421 rect[count].height = wwin->frame->bottom_width + 1;
2422 count++;
2424 if (count > 0) {
2425 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2426 0, 0, rect, count, ShapeSet, Unsorted);
2428 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2429 0, wwin->frame->top_width, wwin->client_win,
2430 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2431 XFlush(dpy);
2433 #endif /* SHAPE */
2435 /* ====================================================================== */
2437 static FocusMode getFocusMode(WWindow * wwin)
2439 FocusMode mode;
2441 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2442 if (wwin->wm_hints->input == True) {
2443 if (wwin->protocols.TAKE_FOCUS)
2444 mode = WFM_LOCALLY_ACTIVE;
2445 else
2446 mode = WFM_PASSIVE;
2447 } else {
2448 if (wwin->protocols.TAKE_FOCUS)
2449 mode = WFM_GLOBALLY_ACTIVE;
2450 else
2451 mode = WFM_NO_INPUT;
2453 } else {
2454 mode = WFM_PASSIVE;
2456 return mode;
2459 void wWindowSetKeyGrabs(WWindow * wwin)
2461 int i;
2462 WShortKey *key;
2464 for (i = 0; i < WKBD_LAST; i++) {
2465 key = &wKeyBindings[i];
2467 if (key->keycode == 0)
2468 continue;
2469 if (key->modifier != AnyModifier) {
2470 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2471 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2472 #ifdef NUMLOCK_HACK
2473 /* Also grab all modifier combinations possible that include,
2474 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2475 * work even if the NumLock/ScrollLock key is on.
2477 wHackedGrabKey(key->keycode, key->modifier,
2478 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2479 #endif
2481 XGrabKey(dpy, key->keycode, key->modifier,
2482 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2485 wRootMenuBindShortcuts(wwin->frame->core->window);
2488 void wWindowResetMouseGrabs(WWindow * wwin)
2490 /* Mouse grabs can't be done on the client window because of
2491 * ICCCM and because clients that try to do the same will crash.
2493 * But there is a problem wich makes tbar buttons of unfocused
2494 * windows not usable as the click goes to the frame window instead
2495 * of the button itself. Must figure a way to fix that.
2498 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2500 if (!WFLAGP(wwin, no_bind_mouse)) {
2501 /* grabs for Meta+drag */
2502 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2503 True, ButtonPressMask | ButtonReleaseMask,
2504 GrabModeSync, GrabModeAsync, None, None);
2506 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2507 * but we only grab it for Button4 and Button 5 since a lot of apps
2508 * use CTRL+Button1-3 for app related functionality
2510 if (wPreferences.resize_increment > 0) {
2511 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2512 True, ButtonPressMask | ButtonReleaseMask,
2513 GrabModeSync, GrabModeAsync, None, None);
2514 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2515 True, ButtonPressMask | ButtonReleaseMask,
2516 GrabModeSync, GrabModeAsync, None, None);
2518 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2519 True, ButtonPressMask | ButtonReleaseMask,
2520 GrabModeSync, GrabModeAsync, None, None);
2521 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2522 True, ButtonPressMask | ButtonReleaseMask,
2523 GrabModeSync, GrabModeAsync, None, None);
2527 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2528 && !wwin->flags.is_gnustep) {
2529 /* the passive grabs to focus the window */
2530 /* if (wPreferences.focus_mode == WKF_CLICK) */
2531 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2532 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2534 XFlush(dpy);
2537 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2539 if (attr->flags & GSExtraFlagsAttr) {
2540 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2541 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2542 wWindowUpdateButtonImages(wwin);
2547 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2549 WWindowState *wstate;
2551 wstate = malloc(sizeof(WWindowState));
2552 if (!wstate)
2553 return NULL;
2555 memset(wstate, 0, sizeof(WWindowState));
2556 wstate->pid = pid;
2557 if (instance)
2558 wstate->instance = wstrdup(instance);
2559 if (class)
2560 wstate->class = wstrdup(class);
2561 if (command)
2562 wstate->command = wstrdup(command);
2563 wstate->state = state;
2565 wstate->next = windowState;
2566 windowState = wstate;
2568 return wstate;
2571 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2573 WMagicNumber wWindowGetSavedState(Window win)
2575 char *instance, *class, *command = NULL;
2576 WWindowState *wstate = windowState;
2578 if (!wstate)
2579 return NULL;
2581 command = GetCommandForWindow(win);
2582 if (!command)
2583 return NULL;
2585 if (PropGetWMClass(win, &class, &instance)) {
2586 while (wstate) {
2587 if (SAME(instance, wstate->instance) &&
2588 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2589 break;
2591 wstate = wstate->next;
2593 } else {
2594 wstate = NULL;
2597 if (command)
2598 wfree(command);
2599 if (instance)
2600 free(instance);
2601 if (class)
2602 free(class);
2604 return wstate;
2607 void wWindowDeleteSavedState(WMagicNumber id)
2609 WWindowState *tmp, *wstate = (WWindowState *) id;
2611 if (!wstate || !windowState)
2612 return;
2614 tmp = windowState;
2615 if (tmp == wstate) {
2616 windowState = wstate->next;
2617 release_wwindowstate(wstate);
2618 } else {
2619 while (tmp->next) {
2620 if (tmp->next == wstate) {
2621 tmp->next = wstate->next;
2622 release_wwindowstate(wstate);
2623 break;
2625 tmp = tmp->next;
2630 void wWindowDeleteSavedStatesForPID(pid_t pid)
2632 WWindowState *tmp, *wstate;
2634 if (!windowState)
2635 return;
2637 tmp = windowState;
2638 if (tmp->pid == pid) {
2639 wstate = windowState;
2640 windowState = tmp->next;
2642 release_wwindowstate(wstate);
2643 } else {
2644 while (tmp->next) {
2645 if (tmp->next->pid == pid) {
2646 wstate = tmp->next;
2647 tmp->next = wstate->next;
2648 release_wwindowstate(wstate);
2649 break;
2651 tmp = tmp->next;
2656 static void release_wwindowstate(WWindowState *wstate)
2658 if (wstate->instance)
2659 wfree(wstate->instance);
2661 if (wstate->class)
2662 wfree(wstate->class);
2664 if (wstate->command)
2665 wfree(wstate->command);
2667 wfree(wstate->state);
2668 wfree(wstate);
2671 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2673 if (wwin->flags.omnipresent == flag)
2674 return;
2676 wwin->flags.omnipresent = flag;
2677 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2680 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2682 WWindow *wwin = data;
2684 #ifndef NUMLOCK_HACK
2685 if ((event->xbutton.state & ValidModMask)
2686 != (event->xbutton.state & ~LockMask)) {
2687 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2688 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2690 #endif
2692 event->xbutton.state &= ValidModMask;
2694 CloseWindowMenu(wwin->screen_ptr);
2696 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2697 && !WFLAGP(wwin, no_focusable)) {
2698 wSetFocusTo(wwin->screen_ptr, wwin);
2701 if (event->xbutton.button == Button1)
2702 wRaiseFrame(wwin->frame->core);
2704 if (event->xbutton.window != wwin->frame->resizebar->window) {
2705 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2706 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2707 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2708 return;
2712 if (event->xbutton.state & MOD_MASK) {
2713 /* move the window */
2714 wMouseMoveWindow(wwin, event);
2715 XUngrabPointer(dpy, CurrentTime);
2716 } else {
2717 wMouseResizeWindow(wwin, event);
2718 XUngrabPointer(dpy, CurrentTime);
2722 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2724 WWindow *wwin = data;
2726 event->xbutton.state &= ValidModMask;
2728 if (event->xbutton.button == Button1) {
2729 if (event->xbutton.state == 0) {
2730 if (!WFLAGP(wwin, no_shadeable)) {
2731 /* shade window */
2732 if (wwin->flags.shaded)
2733 wUnshadeWindow(wwin);
2734 else
2735 wShadeWindow(wwin);
2737 } else {
2738 int dir = 0;
2740 if (event->xbutton.state & ControlMask)
2741 dir |= MAX_VERTICAL;
2743 if (event->xbutton.state & ShiftMask) {
2744 dir |= MAX_HORIZONTAL;
2745 if (!(event->xbutton.state & ControlMask))
2746 wSelectWindow(wwin, !wwin->flags.selected);
2749 /* maximize window */
2750 if (dir != 0 && IS_RESIZABLE(wwin)) {
2751 int ndir = dir ^ wwin->flags.maximized;
2753 if (ndir != 0)
2754 wMaximizeWindow(wwin, ndir);
2755 else
2756 wUnmaximizeWindow(wwin);
2759 } else if (event->xbutton.button == Button3) {
2760 if (event->xbutton.state & MOD_MASK)
2761 wHideOtherApplications(wwin);
2762 } else if (event->xbutton.button == Button2) {
2763 wSelectWindow(wwin, !wwin->flags.selected);
2764 } else if (event->xbutton.button == W_getconf_mouseWheelUp()) {
2765 wShadeWindow(wwin);
2766 } else if (event->xbutton.button == W_getconf_mouseWheelDown()) {
2767 wUnshadeWindow(wwin);
2771 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2773 WWindow *wwin = desc->parent;
2774 unsigned int new_width, w_scale;
2775 unsigned int new_height, h_scale;
2776 unsigned int resize_width_increment = 0;
2777 unsigned int resize_height_increment = 0;
2779 if (wwin->normal_hints) {
2780 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2781 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2782 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2783 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2785 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2786 resize_width_increment = wPreferences.resize_increment;
2787 resize_height_increment = wPreferences.resize_increment;
2790 event->xbutton.state &= ValidModMask;
2792 CloseWindowMenu(wwin->screen_ptr);
2794 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2795 wSetFocusTo(wwin->screen_ptr, wwin);
2797 if (event->xbutton.button == Button1)
2798 wRaiseFrame(wwin->frame->core);
2800 if (event->xbutton.state & ControlMask) {
2801 if (event->xbutton.button == Button4) {
2802 new_width = wwin->client.width - resize_width_increment;
2803 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2804 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2806 if (event->xbutton.button == Button5) {
2807 new_width = wwin->client.width + resize_width_increment;
2808 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2809 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2813 if (event->xbutton.state & MOD_MASK) {
2814 /* move the window */
2815 if (XGrabPointer(dpy, wwin->client_win, False,
2816 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2817 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2818 return;
2820 if (event->xbutton.button == Button3) {
2821 wMouseResizeWindow(wwin, event);
2822 } else if (event->xbutton.button == Button4) {
2823 new_height = wwin->client.height - resize_height_increment;
2824 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2825 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2826 } else if (event->xbutton.button == Button5) {
2827 new_height = wwin->client.height + resize_height_increment;
2828 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2829 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2830 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2831 wMouseMoveWindow(wwin, event);
2833 XUngrabPointer(dpy, CurrentTime);
2837 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2839 WWindow *wwin = (WWindow *) data;
2841 #ifndef NUMLOCK_HACK
2842 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2843 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2844 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2845 #endif
2846 event->xbutton.state &= ValidModMask;
2848 CloseWindowMenu(wwin->screen_ptr);
2850 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2851 && !WFLAGP(wwin, no_focusable))
2852 wSetFocusTo(wwin->screen_ptr, wwin);
2854 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2856 if (event->xbutton.button == Button1) {
2857 if (event->xbutton.state & MOD_MASK)
2858 wLowerFrame(wwin->frame->core);
2859 else
2860 wRaiseFrame(wwin->frame->core);
2862 if ((event->xbutton.state & ShiftMask)
2863 && !(event->xbutton.state & ControlMask)) {
2864 wSelectWindow(wwin, !wwin->flags.selected);
2865 return;
2867 if (event->xbutton.window != wwin->frame->titlebar->window
2868 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2869 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2870 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2871 return;
2874 /* move the window */
2875 wMouseMoveWindow(wwin, event);
2877 XUngrabPointer(dpy, CurrentTime);
2878 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2879 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2880 WObjDescriptor *desc;
2882 if (event->xbutton.window != wwin->frame->titlebar->window
2883 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2884 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2885 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2886 return;
2889 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2891 /* allow drag select */
2892 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2893 event->xany.send_event = True;
2894 (*desc->handle_mousedown) (desc, event);
2896 XUngrabPointer(dpy, CurrentTime);
2900 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2902 WWindow *wwin = data;
2904 event->xbutton.state &= ValidModMask;
2906 CloseWindowMenu(wwin->screen_ptr);
2908 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2909 return;
2911 /* if control-click, kill the client */
2912 if (event->xbutton.state & ControlMask) {
2913 wClientKill(wwin);
2914 } else {
2915 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2916 /* send delete message */
2917 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2922 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2924 WWindow *wwin = data;
2926 CloseWindowMenu(wwin->screen_ptr);
2928 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2929 return;
2931 /* send delete message */
2932 if (wwin->protocols.DELETE_WINDOW)
2933 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2934 else
2935 wClientKill(wwin);
2938 #ifdef XKB_BUTTON_HINT
2939 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2941 WWindow *wwin = data;
2942 WFrameWindow *fwin = wwin->frame;
2943 WScreen *scr = fwin->screen_ptr;
2944 int tl;
2946 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2947 return;
2948 tl = wwin->frame->languagemode;
2949 wwin->frame->languagemode = wwin->frame->last_languagemode;
2950 wwin->frame->last_languagemode = tl;
2951 wSetFocusTo(scr, wwin);
2952 wwin->frame->languagebutton_image =
2953 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2954 wFrameWindowUpdateLanguageButton(wwin->frame);
2955 if (event->xbutton.button == Button3)
2956 return;
2957 wRaiseFrame(fwin->core);
2959 #endif
2961 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2963 WWindow *wwin = data;
2965 event->xbutton.state &= ValidModMask;
2967 CloseWindowMenu(wwin->screen_ptr);
2969 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2970 return;
2972 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2973 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2974 } else {
2975 WApplication *wapp;
2976 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2978 wapp = wApplicationOf(wwin->main_window);
2979 if (wapp && !WFLAGP(wwin, no_appicon))
2980 wHideApplication(wapp);
2981 } else if (event->xbutton.state == 0) {
2982 wIconifyWindow(wwin);