wmaker: removed unused constant SCROLL_STEPS in the switchpanel code
[wmaker-crm.git] / src / window.c
blobd46eca34ce69c945e87adf00d7dc130f9a197e36
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 USE_XSHAPE
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 "colormap.h"
53 #include "keybind.h"
54 #include "stacking.h"
55 #include "defaults.h"
56 #include "workspace.h"
57 #include "xinerama.h"
58 #include "appmenu.h"
59 #include "appicon.h"
60 #include "superfluous.h"
61 #include "rootmenu.h"
62 #include "placement.h"
63 #include "misc.h"
64 #include "startup.h"
65 #include "winmenu.h"
66 #include "osdep.h"
68 #ifdef MWM_HINTS
69 # include "motif.h"
70 #endif
71 #include "wmspec.h"
73 #define MOD_MASK wPreferences.modifier_mask
76 /***** Local Stuff *****/
77 static WWindowState *windowState = NULL;
78 static FocusMode getFocusMode(WWindow *wwin);
79 static int getSavedState(Window window, WSavedState **state);
80 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
82 /* frame window (during window grabs) */
83 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
85 /* close button */
86 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
87 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
89 /* iconify button */
90 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
92 #ifdef XKB_BUTTON_HINT
93 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
94 #endif
96 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
97 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
98 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
100 static void release_wwindowstate(WWindowState *wstate);
102 /****** Notification Observers ******/
104 static void appearanceObserver(void *self, WMNotification * notif)
106 WWindow *wwin = (WWindow *) self;
107 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
109 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
110 return;
112 if (flags & WFontSettings) {
113 wWindowConfigureBorders(wwin);
114 if (wwin->flags.shaded) {
115 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
116 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
117 wWindowSynthConfigureNotify(wwin);
120 if (flags & WTextureSettings)
121 wwin->frame->flags.need_texture_remake = 1;
123 if (flags & (WTextureSettings | WColorSettings)) {
124 if (wwin->frame->titlebar)
125 XClearWindow(dpy, wwin->frame->titlebar->window);
127 wFrameWindowPaint(wwin->frame);
132 /* Return the WWindow associated with a given (Xlib) Window. */
133 WWindow *wWindowFor(Window window)
135 WObjDescriptor *desc;
137 if (window == None)
138 return NULL;
140 if (XFindContext(dpy, window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT)
141 return NULL;
143 if (desc->parent_type == WCLASS_WINDOW)
144 return desc->parent;
145 else if (desc->parent_type == WCLASS_FRAME) {
146 WFrameWindow *frame = (WFrameWindow *) desc->parent;
147 if (frame->flags.is_client_window_frame)
148 return frame->child;
151 return NULL;
154 WWindow *wWindowCreate(void)
156 WWindow *wwin;
158 wwin = wmalloc(sizeof(WWindow));
159 wretain(wwin);
161 wwin->client_descriptor.handle_mousedown = frameMouseDown;
162 wwin->client_descriptor.parent = wwin;
163 wwin->client_descriptor.self = wwin;
164 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
166 return wwin;
169 void wWindowDestroy(WWindow *wwin)
171 int i;
173 if (wwin->screen_ptr->cmap_window == wwin)
174 wwin->screen_ptr->cmap_window = NULL;
176 WMRemoveNotificationObserver(wwin);
178 wwin->flags.destroyed = 1;
180 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
181 if (!wwin->screen_ptr->shortcutWindows[i])
182 continue;
184 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
186 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
187 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
188 wwin->screen_ptr->shortcutWindows[i] = NULL;
192 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
193 wwin->fake_group->retainCount--;
194 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
195 XDestroyWindow(dpy, wwin->fake_group->leader);
196 wwin->fake_group->leader = None;
197 wwin->fake_group->origLeader = None;
198 XFlush(dpy);
202 if (wwin->normal_hints)
203 XFree(wwin->normal_hints);
205 if (wwin->wm_hints)
206 XFree(wwin->wm_hints);
208 if (wwin->wm_instance)
209 XFree(wwin->wm_instance);
211 if (wwin->wm_class)
212 XFree(wwin->wm_class);
214 if (wwin->wm_gnustep_attr)
215 wfree(wwin->wm_gnustep_attr);
217 if (wwin->cmap_windows)
218 XFree(wwin->cmap_windows);
220 XDeleteContext(dpy, wwin->client_win, w_global.context.client_win);
222 if (wwin->frame)
223 wFrameWindowDestroy(wwin->frame);
225 if (wwin->icon) {
226 RemoveFromStackList(wwin->icon->core);
227 wIconDestroy(wwin->icon);
228 if (wPreferences.auto_arrange_icons)
229 wArrangeIcons(wwin->screen_ptr, True);
231 if (wwin->net_icon_image)
232 RReleaseImage(wwin->net_icon_image);
234 wrelease(wwin);
237 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
239 if (gs_hints->flags & GSWindowStyleAttr) {
240 if (gs_hints->window_style == WMBorderlessWindowMask) {
241 wwin->client_flags.no_border = 1;
242 wwin->client_flags.no_titlebar = 1;
243 wwin->client_flags.no_closable = 1;
244 wwin->client_flags.no_miniaturizable = 1;
245 wwin->client_flags.no_resizable = 1;
246 wwin->client_flags.no_close_button = 1;
247 wwin->client_flags.no_miniaturize_button = 1;
248 wwin->client_flags.no_resizebar = 1;
249 } else {
250 wwin->client_flags.no_close_button =
251 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
253 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
255 wwin->client_flags.no_miniaturize_button =
256 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
258 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
260 wwin->client_flags.no_resizebar =
261 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
263 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
265 /* these attributes supposedly imply in the existence
266 * of a titlebar */
267 if (gs_hints->window_style & (WMResizableWindowMask |
268 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
269 wwin->client_flags.no_titlebar = 0;
270 } else {
271 wwin->client_flags.no_titlebar =
272 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
276 } else {
277 /* setup the defaults */
278 wwin->client_flags.no_border = 0;
279 wwin->client_flags.no_titlebar = 0;
280 wwin->client_flags.no_closable = 0;
281 wwin->client_flags.no_miniaturizable = 0;
282 wwin->client_flags.no_resizable = 0;
283 wwin->client_flags.no_close_button = 0;
284 wwin->client_flags.no_miniaturize_button = 0;
285 wwin->client_flags.no_resizebar = 0;
287 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
288 wwin->client_flags.no_appicon = 1;
291 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
293 WScreen *scr = wwin->screen_ptr;
295 /* sets global default stuff */
296 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
298 * Decoration setting is done in this precedence (lower to higher)
299 * - use global default in the resource database
300 * - guess some settings
301 * - use GNUstep/external window attributes
302 * - set hints specified for the app in the resource DB
305 WSETUFLAG(wwin, broken_close, 0);
307 if (wwin->protocols.DELETE_WINDOW)
308 WSETUFLAG(wwin, kill_close, 0);
309 else
310 WSETUFLAG(wwin, kill_close, 1);
312 /* transients can't be iconified or maximized */
313 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
314 WSETUFLAG(wwin, no_miniaturizable, 1);
315 WSETUFLAG(wwin, no_miniaturize_button, 1);
318 /* if the window can't be resized, remove the resizebar */
319 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
320 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
321 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
322 WSETUFLAG(wwin, no_resizable, 1);
323 WSETUFLAG(wwin, no_resizebar, 1);
326 /* set GNUstep window attributes */
327 if (wwin->wm_gnustep_attr) {
328 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
330 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
332 *level = wwin->wm_gnustep_attr->window_level;
334 * INT_MIN is the only illegal window level.
336 if (*level == INT_MIN)
337 *level = INT_MIN + 1;
338 } else {
339 /* setup defaults */
340 *level = WMNormalLevel;
342 } else {
343 int tmp_workspace = -1;
344 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
346 #ifdef MWM_HINTS
347 wMWMCheckClientHints(wwin);
348 #endif /* MWM_HINTS */
350 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
352 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
353 * have INT_MIN that means that no window level was requested. -Dan
355 if (tmp_level == INT_MIN) {
356 if (WFLAGP(wwin, floating))
357 *level = WMFloatingLevel;
358 else if (WFLAGP(wwin, sunken))
359 *level = WMSunkenLevel;
360 else
361 *level = WMNormalLevel;
362 } else {
363 *level = tmp_level;
366 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
367 WWindow *transientOwner = wWindowFor(wwin->transient_for);
368 if (transientOwner) {
369 int ownerLevel = transientOwner->frame->core->stacking->window_level;
370 if (ownerLevel > *level)
371 *level = ownerLevel;
375 if (tmp_workspace >= 0)
376 *workspace = tmp_workspace % scr->workspace_count;
380 * Set attributes specified only for that window/class.
381 * This might do duplicate work with the 1st wDefaultFillAttributes().
383 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
384 &wwin->defined_user_flags, False);
386 * Sanity checks for attributes that depend on other attributes
388 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
389 wwin->user_flags.emulate_appicon = 0;
391 if (wwin->main_window != None) {
392 WApplication *wapp = wApplicationOf(wwin->main_window);
393 if (wapp && !wapp->flags.emulated)
394 wwin->user_flags.emulate_appicon = 0;
397 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
398 wwin->user_flags.emulate_appicon = 0;
400 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
401 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
402 wwin->user_flags.sunken = 0;
404 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
406 /* windows that have takefocus=False shouldn't take focus at all */
407 if (wwin->focus_mode == WFM_NO_INPUT)
408 wwin->client_flags.no_focusable = 1;
411 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
413 int w1, h1, w2, h2;
415 w1 = wwin->frame->core->width;
416 h1 = wwin->frame->core->height;
417 w2 = obscured->frame->core->width;
418 h2 = obscured->frame->core->height;
420 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
421 && wwin->frame->workspace != obscured->frame->workspace)
422 return False;
424 if (wwin->frame_x + w1 < obscured->frame_x
425 || wwin->frame_y + h1 < obscured->frame_y
426 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
427 return False;
429 return True;
432 static void fixLeaderProperties(WWindow *wwin)
434 XClassHint *classHint;
435 XWMHints *hints, *clientHints;
436 XWindowAttributes attr;
437 Window leaders[2], window;
438 char **argv, *command;
439 int argc, i, pid;
440 Bool haveCommand;
442 classHint = XAllocClassHint();
443 clientHints = XGetWMHints(dpy, wwin->client_win);
444 pid = wNETWMGetPidForWindow(wwin->client_win);
445 if (pid > 0)
446 haveCommand = GetCommandForPid(pid, &argv, &argc);
447 else
448 haveCommand = False;
450 leaders[0] = wwin->client_leader;
451 leaders[1] = wwin->group_id;
453 if (haveCommand) {
454 command = GetCommandForWindow(wwin->client_win);
455 if (command)
456 wfree(command); /* command already set. nothing to do. */
457 else
458 XSetCommand(dpy, wwin->client_win, argv, argc);
461 for (i = 0; i < 2; i++) {
462 window = leaders[i];
463 if (window) {
464 if (XGetClassHint(dpy, window, classHint) == 0) {
465 classHint->res_name = wwin->wm_instance;
466 classHint->res_class = wwin->wm_class;
467 XSetClassHint(dpy, window, classHint);
469 hints = XGetWMHints(dpy, window);
470 if (hints) {
471 XFree(hints);
472 } else if (clientHints) {
473 /* set window group leader to self */
474 clientHints->window_group = window;
475 clientHints->flags |= WindowGroupHint;
476 XSetWMHints(dpy, window, clientHints);
479 if (haveCommand) {
480 command = GetCommandForWindow(window);
481 if (command)
482 wfree(command); /* command already set. nothing to do. */
483 else
484 XSetCommand(dpy, window, argv, argc);
487 /* Make sure we get notification when this window is destroyed */
488 if (XGetWindowAttributes(dpy, window, &attr))
489 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
493 XFree(classHint);
494 if (clientHints)
495 XFree(clientHints);
496 if (haveCommand)
497 wfree(argv);
500 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
502 XClassHint *classHint;
503 XWMHints *hints;
504 Window leader;
505 int argc;
506 char **argv;
508 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
509 /* set class hint */
510 classHint = XAllocClassHint();
511 classHint->res_name = instance;
512 classHint->res_class = class;
513 XSetClassHint(dpy, leader, classHint);
514 XFree(classHint);
516 /* inherit these from the original leader if available */
517 hints = XGetWMHints(dpy, win);
518 if (!hints) {
519 hints = XAllocWMHints();
520 hints->flags = 0;
522 /* set window group leader to self */
523 hints->window_group = leader;
524 hints->flags |= WindowGroupHint;
525 XSetWMHints(dpy, leader, hints);
526 XFree(hints);
528 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
529 XSetCommand(dpy, leader, argv, argc);
530 XFreeStringList(argv);
533 return leader;
536 static int matchIdentifier(const void *item, const void *cdata)
538 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
542 *----------------------------------------------------------------
543 * wManageWindow--
544 * reparents the window and allocates a descriptor for it.
545 * Window manager hints and other hints are fetched to configure
546 * the window decoration attributes and others. User preferences
547 * for the window are used if available, to configure window
548 * decorations and some behaviour.
549 * If in startup, windows that are override redirect,
550 * unmapped and never were managed and are Withdrawn are not
551 * managed.
553 * Returns:
554 * the new window descriptor
556 * Side effects:
557 * The window is reparented and appropriate notification
558 * is done to the client. Input mask for the window is setup.
559 * The window descriptor is also associated with various window
560 * contexts and inserted in the head of the window list.
561 * Event handler contexts are associated for some objects
562 * (buttons, titlebar and resizebar)
564 *----------------------------------------------------------------
566 WWindow *wManageWindow(WScreen *scr, Window window)
568 WWindow *wwin;
569 int x, y;
570 unsigned width, height;
571 XWindowAttributes wattribs;
572 XSetWindowAttributes attribs;
573 WWindowState *win_state;
574 WWindow *transientOwner = NULL;
575 int window_level;
576 int wm_state;
577 int foo;
578 int workspace = -1;
579 char *title;
580 Bool withdraw = False;
581 Bool raise = False;
583 /* mutex. */
584 XGrabServer(dpy);
585 XSync(dpy, False);
587 /* make sure the window is still there */
588 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
589 XUngrabServer(dpy);
590 return NULL;
593 /* if it's an override-redirect, ignore it */
594 if (wattribs.override_redirect) {
595 XUngrabServer(dpy);
596 return NULL;
599 wm_state = PropGetWindowState(window);
601 /* if it's startup and the window is unmapped, don't manage it */
602 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
603 XUngrabServer(dpy);
604 return NULL;
607 wwin = wWindowCreate();
609 title = wNETWMGetWindowName(window);
610 if (title)
611 wwin->flags.net_has_title = 1;
612 else if (!wFetchName(dpy, window, &title))
613 title = NULL;
615 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
617 #ifdef USE_XSHAPE
618 if (w_global.xext.shape.supported) {
619 int junk;
620 unsigned int ujunk;
621 int b_shaped;
623 XShapeSelectInput(dpy, window, ShapeNotifyMask);
624 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
625 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
626 wwin->flags.shaped = b_shaped;
628 #endif
630 /* Get hints and other information in properties */
631 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
633 /* setup descriptor */
634 wwin->client_win = window;
635 wwin->screen_ptr = scr;
636 wwin->old_border_width = wattribs.border_width;
637 wwin->event_mask = CLIENT_EVENTS;
638 attribs.event_mask = CLIENT_EVENTS;
639 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
640 attribs.save_under = False;
642 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
643 XSetWindowBorderWidth(dpy, window, 0);
645 /* get hints from GNUstep app */
646 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
647 wwin->flags.is_gnustep = 1;
649 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
650 wwin->wm_gnustep_attr = NULL;
652 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
653 wwin->flags.is_dockapp = 1;
654 withdraw = True;
657 wwin->client_leader = PropGetClientLeader(window);
658 if (wwin->client_leader != None)
659 wwin->main_window = wwin->client_leader;
661 wwin->wm_hints = XGetWMHints(dpy, window);
663 if (wwin->wm_hints) {
664 if (wwin->wm_hints->flags & StateHint) {
665 if (wwin->wm_hints->initial_state == IconicState) {
666 wwin->flags.miniaturized = 1;
667 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
668 wwin->flags.is_dockapp = 1;
669 withdraw = True;
673 if (wwin->wm_hints->flags & WindowGroupHint) {
674 wwin->group_id = wwin->wm_hints->window_group;
675 /* window_group has priority over CLIENT_LEADER */
676 wwin->main_window = wwin->group_id;
677 } else {
678 wwin->group_id = None;
681 if (wwin->wm_hints->flags & UrgencyHint) {
682 wwin->flags.urgent = 1;
683 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
685 } else {
686 wwin->group_id = None;
689 PropGetProtocols(window, &wwin->protocols);
691 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
692 wwin->transient_for = None;
693 } else {
694 if (wwin->transient_for == None || wwin->transient_for == window) {
695 wwin->transient_for = scr->root_win;
696 } else {
697 transientOwner = wWindowFor(wwin->transient_for);
698 if (transientOwner && transientOwner->main_window != None)
699 wwin->main_window = transientOwner->main_window;
703 /* guess the focus mode */
704 wwin->focus_mode = getFocusMode(wwin);
706 /* get geometry stuff */
707 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
709 /* get colormap windows */
710 GetColormapWindows(wwin);
713 * Setup the decoration/window attributes and
714 * geometry
716 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
718 /* Make broken apps behave as a nice app. */
719 if (WFLAGP(wwin, emulate_appicon))
720 wwin->main_window = wwin->client_win;
722 fixLeaderProperties(wwin);
724 wwin->orig_main_window = wwin->main_window;
726 if (wwin->flags.is_gnustep)
727 WSETUFLAG(wwin, shared_appicon, 0);
729 if (wwin->main_window) {
730 XTextProperty text_prop;
732 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, w_global.atom.wmaker.menu))
733 WSETUFLAG(wwin, shared_appicon, 0);
736 if (wwin->flags.is_dockapp)
737 WSETUFLAG(wwin, shared_appicon, 0);
739 if (wwin->main_window) {
740 WApplication *app = wApplicationOf(wwin->main_window);
741 if (app && app->app_icon)
742 WSETUFLAG(wwin, shared_appicon, 0);
745 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
746 char *buffer, *instance, *class;
747 WFakeGroupLeader *fPtr;
748 int index;
750 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
752 /* // only enter here if PropGetWMClass() succeds */
753 PropGetWMClass(wwin->main_window, &class, &instance);
754 buffer = StrConcatDot(instance, class);
756 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
757 if (index != WANotFound) {
758 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
759 if (fPtr->retainCount == 0)
760 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
761 instance, class);
763 fPtr->retainCount++;
764 if (fPtr->origLeader == None) {
765 if (ADEQUATE(wwin->main_window)) {
766 fPtr->retainCount++;
767 fPtr->origLeader = wwin->main_window;
770 wwin->fake_group = fPtr;
771 wwin->main_window = fPtr->leader;
772 wfree(buffer);
773 } else {
774 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
776 fPtr->identifier = buffer;
777 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
778 fPtr->origLeader = None;
779 fPtr->retainCount = 1;
781 WMAddToArray(scr->fakeGroupLeaders, fPtr);
783 if (ADEQUATE(wwin->main_window)) {
784 fPtr->retainCount++;
785 fPtr->origLeader = wwin->main_window;
787 wwin->fake_group = fPtr;
788 wwin->main_window = fPtr->leader;
791 if (instance)
792 free(instance);
794 if (class)
795 free(class);
796 #undef ADEQUATE
800 * Setup the initial state of the window
802 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
803 wwin->flags.miniaturized = 1;
805 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
806 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
808 wNETWMCheckInitialClientState(wwin);
810 /* apply previous state if it exists and we're in startup */
811 if (scr->flags.startup && wm_state >= 0) {
812 if (wm_state == IconicState)
813 wwin->flags.miniaturized = 1;
814 else if (wm_state == WithdrawnState)
815 withdraw = True;
818 /* if there is a saved state (from file), restore it */
819 win_state = NULL;
820 if (wwin->main_window != None)
821 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
822 else
823 win_state = (WWindowState *) wWindowGetSavedState(window);
825 if (win_state && !withdraw) {
826 if (win_state->state->hidden > 0)
827 wwin->flags.hidden = win_state->state->hidden;
829 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
830 wwin->flags.shaded = win_state->state->shaded;
832 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
833 wwin->flags.miniaturized = win_state->state->miniaturized;
835 if (!IS_OMNIPRESENT(wwin)) {
836 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
837 wwin->wm_class);
838 if (w < 0 || w >= scr->workspace_count) {
839 workspace = win_state->state->workspace;
840 if (workspace >= scr->workspace_count)
841 workspace = scr->current_workspace;
842 } else {
843 workspace = w;
845 } else {
846 workspace = scr->current_workspace;
850 /* if we're restarting, restore saved state (from hints).
851 * This will overwrite previous */
853 WSavedState *wstate;
855 if (getSavedState(window, &wstate)) {
856 wwin->flags.shaded = wstate->shaded;
857 wwin->flags.hidden = wstate->hidden;
858 wwin->flags.miniaturized = wstate->miniaturized;
859 wwin->flags.maximized = wstate->maximized;
860 if (wwin->flags.maximized) {
861 wwin->old_geometry.x = wstate->x;
862 wwin->old_geometry.y = wstate->y;
863 wwin->old_geometry.width = wstate->w;
864 wwin->old_geometry.height = wstate->h;
867 workspace = wstate->workspace;
868 } else {
869 wstate = NULL;
872 /* restore window shortcut */
873 if (wstate != NULL || win_state != NULL) {
874 unsigned mask = 0;
876 if (win_state != NULL)
877 mask = win_state->state->window_shortcuts;
879 if (wstate != NULL && mask == 0)
880 mask = wstate->window_shortcuts;
882 if (mask > 0) {
883 int i;
885 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
886 if (mask & (1 << i)) {
887 if (!scr->shortcutWindows[i])
888 scr->shortcutWindows[i] = WMCreateArray(4);
890 WMAddToArray(scr->shortcutWindows[i], wwin);
896 if (wstate != NULL)
897 wfree(wstate);
900 /* don't let transients start miniaturized if their owners are not */
901 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
902 wwin->flags.miniaturized = 0;
903 if (wwin->wm_hints)
904 wwin->wm_hints->initial_state = NormalState;
907 /* set workspace on which the window starts */
908 if (workspace >= 0) {
909 if (workspace > scr->workspace_count - 1)
910 workspace = workspace % scr->workspace_count;
911 } else {
912 int w;
914 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
916 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
917 workspace = w;
918 } else {
919 if (wPreferences.open_transients_with_parent && transientOwner)
920 workspace = transientOwner->frame->workspace;
921 else
922 workspace = scr->current_workspace;
926 /* setup window geometry */
927 if (win_state && win_state->state->w > 0) {
928 width = win_state->state->w;
929 height = win_state->state->h;
932 wWindowConstrainSize(wwin, &width, &height);
934 /* do not ask for window placement if the window is
935 * transient, during startup, if the initial workspace is another one
936 * or if the window wants to start iconic.
937 * If geometry was saved, restore it. */
939 Bool dontBring = False;
941 if (win_state && win_state->state->w > 0) {
942 x = win_state->state->x;
943 y = win_state->state->y;
944 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
945 && !scr->flags.startup
946 && workspace == scr->current_workspace
947 && !wwin->flags.miniaturized
948 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
950 if (transientOwner && transientOwner->flags.mapped) {
951 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
952 WMRect rect;
953 int head;
955 x = transientOwner->frame_x +
956 abs((transientOwner->frame->core->width - width) / 2) + offs;
957 y = transientOwner->frame_y +
958 abs((transientOwner->frame->core->height - height) / 3) + offs;
960 /* limit transient windows to be inside their parent's head */
961 rect.pos.x = transientOwner->frame_x;
962 rect.pos.y = transientOwner->frame_y;
963 rect.size.width = transientOwner->frame->core->width;
964 rect.size.height = transientOwner->frame->core->height;
966 head = wGetHeadForRect(scr, rect);
967 rect = wGetRectForHead(scr, head);
969 if (x < rect.pos.x)
970 x = rect.pos.x;
971 else if (x + width > rect.pos.x + rect.size.width)
972 x = rect.pos.x + rect.size.width - width;
974 if (y < rect.pos.y)
975 y = rect.pos.y;
976 else if (y + height > rect.pos.y + rect.size.height)
977 y = rect.pos.y + rect.size.height - height;
979 } else {
980 PlaceWindow(wwin, &x, &y, width, height);
983 if (wPreferences.window_placement == WPM_MANUAL)
984 dontBring = True;
986 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
987 int head, flags;
988 WMRect rect;
989 int reposition = 0;
991 /* Make spash screens come out in the center of a head
992 * trouble is that most splashies never get here
993 * they are managed trough atoms but god knows where.
994 * Dan, do you know ? -peter
996 * Most of them are not managed, they have set
997 * OverrideRedirect, which means we can't do anything about
998 * them. -alfredo */
1000 /* xinerama checks for: across head and dead space */
1001 rect.pos.x = x;
1002 rect.pos.y = y;
1003 rect.size.width = width;
1004 rect.size.height = height;
1006 head = wGetRectPlacementInfo(scr, rect, &flags);
1008 if (flags & XFLAG_DEAD)
1009 reposition = 1;
1011 if (flags & XFLAG_MULTIPLE)
1012 reposition = 2;
1015 switch (reposition) {
1016 case 1:
1017 head = wGetHeadForPointerLocation(scr);
1018 rect = wGetRectForHead(scr, head);
1020 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1021 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1022 break;
1024 case 2:
1025 rect = wGetRectForHead(scr, head);
1027 if (x < rect.pos.x)
1028 x = rect.pos.x;
1029 else if (x + width > rect.pos.x + rect.size.width)
1030 x = rect.pos.x + rect.size.width - width;
1032 if (y < rect.pos.y)
1033 y = rect.pos.y;
1034 else if (y + height > rect.pos.y + rect.size.height)
1035 y = rect.pos.y + rect.size.height - height;
1037 break;
1039 default:
1040 break;
1044 if (WFLAGP(wwin, dont_move_off) && dontBring)
1045 wScreenBringInside(scr, &x, &y, width, height);
1048 wNETWMPositionSplash(wwin, &x, &y, width, height);
1050 if (wwin->flags.urgent) {
1051 if (!IS_OMNIPRESENT(wwin))
1052 wwin->flags.omnipresent ^= 1;
1055 /* Create frame, borders and do reparenting */
1056 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1057 #ifdef XKB_BUTTON_HINT
1058 if (wPreferences.modelock)
1059 foo |= WFF_LANGUAGE_BUTTON;
1060 #endif
1061 if (HAS_TITLEBAR(wwin))
1062 foo |= WFF_TITLEBAR;
1064 if (HAS_RESIZEBAR(wwin))
1065 foo |= WFF_RESIZEBAR;
1067 if (HAS_BORDER(wwin))
1068 foo |= WFF_BORDER;
1070 wwin->frame = wFrameWindowCreate(scr, window_level,
1071 x, y, width, height,
1072 &wPreferences.window_title_clearance,
1073 &wPreferences.window_title_min_height,
1074 &wPreferences.window_title_max_height,
1075 foo,
1076 scr->window_title_texture,
1077 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1078 wattribs.depth, wattribs.visual, wattribs.colormap);
1080 wwin->frame->flags.is_client_window_frame = 1;
1081 wwin->frame->flags.justification = wPreferences.title_justification;
1083 wNETWMCheckInitialFrameState(wwin);
1085 /* setup button images */
1086 wWindowUpdateButtonImages(wwin);
1088 /* hide unused buttons */
1089 foo = 0;
1090 if (WFLAGP(wwin, no_close_button))
1091 foo |= WFF_RIGHT_BUTTON;
1093 if (WFLAGP(wwin, no_miniaturize_button))
1094 foo |= WFF_LEFT_BUTTON;
1096 #ifdef XKB_BUTTON_HINT
1097 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1098 foo |= WFF_LANGUAGE_BUTTON;
1099 #endif
1100 if (foo != 0)
1101 wFrameWindowHideButton(wwin->frame, foo);
1103 wwin->frame->child = wwin;
1104 wwin->frame->workspace = workspace;
1105 wwin->frame->on_click_left = windowIconifyClick;
1107 #ifdef XKB_BUTTON_HINT
1108 if (wPreferences.modelock)
1109 wwin->frame->on_click_language = windowLanguageClick;
1110 #endif
1112 wwin->frame->on_click_right = windowCloseClick;
1113 wwin->frame->on_dblclick_right = windowCloseDblClick;
1114 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1115 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1116 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1118 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1119 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1120 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1123 int gx, gy;
1125 wClientGetGravityOffsets(wwin, &gx, &gy);
1127 /* if gravity is to the south, account for the border sizes */
1128 if (gy > 0)
1129 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1133 * wWindowConfigure() will init the client window's size
1134 * (wwin->client.{width,height}) and all other geometry
1135 * related variables (frame_x,frame_y) */
1136 wWindowConfigure(wwin, x, y, width, height);
1138 /* to make sure the window receives it's new position after reparenting */
1139 wWindowSynthConfigureNotify(wwin);
1141 /* Setup descriptors and save window to internal lists */
1142 if (wwin->main_window != None) {
1143 WApplication *app;
1144 WWindow *leader;
1146 /* Leader windows do not necessary set themselves as leaders.
1147 * If this is the case, point the leader of this window to
1148 * itself */
1149 leader = wWindowFor(wwin->main_window);
1150 if (leader && leader->main_window == None)
1151 leader->main_window = leader->client_win;
1153 app = wApplicationCreate(wwin);
1154 if (app) {
1155 app->last_workspace = workspace;
1157 /* Do application specific stuff, like setting application
1158 * wide attributes. */
1160 if (wwin->flags.hidden) {
1161 /* if the window was set to hidden because it was hidden
1162 * in a previous incarnation and that state was restored */
1163 app->flags.hidden = 1;
1164 } else if (app->flags.hidden) {
1165 if (WFLAGP(app->main_window_desc, start_hidden)) {
1166 wwin->flags.hidden = 1;
1167 } else {
1168 wUnhideApplication(app, False, False);
1169 raise = True;
1172 wAppBounce(app);
1176 /* setup the frame descriptor */
1177 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1178 wwin->frame->core->descriptor.parent = wwin;
1179 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1181 /* don't let windows go away if we die */
1182 XAddToSaveSet(dpy, window);
1184 XLowerWindow(dpy, window);
1186 /* if window is in this workspace and should be mapped, then map it */
1187 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1188 && !wwin->flags.hidden && !withdraw) {
1190 /* The following "if" is to avoid crashing of clients that expect
1191 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1192 * after the return from this function. */
1193 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1194 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1195 else
1196 wClientSetState(wwin, NormalState, None);
1198 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1199 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1201 * The brain damaged idiotic non-click to focus modes will
1202 * have trouble with this because:
1204 * 1. window is created and mapped by the client
1205 * 2. window is mapped by wmaker in small size
1206 * 3. window is animated to grow to normal size
1207 * 4. this function returns to normal event loop
1208 * 5. eventually, the EnterNotify event that would trigger
1209 * the window focusing (if the mouse is over that window)
1210 * will be processed by wmaker.
1211 * But since this event will be rather delayed
1212 * (step 3 has a large delay) the time when the event ocurred
1213 * and when it is processed, the client that owns that window
1214 * will reject the XSetInputFocus() for it.
1216 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1217 DoWindowBirth(wwin);
1219 wWindowMap(wwin);
1222 /* setup stacking descriptor */
1223 if (transientOwner)
1224 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1225 else
1226 wwin->frame->core->stacking->child_of = NULL;
1228 if (!scr->focused_window) {
1229 /* first window on the list */
1230 wwin->next = NULL;
1231 wwin->prev = NULL;
1232 scr->focused_window = wwin;
1233 } else {
1234 WWindow *tmp;
1236 /* add window at beginning of focus window list */
1237 tmp = scr->focused_window;
1238 while (tmp->prev)
1239 tmp = tmp->prev;
1241 tmp->prev = wwin;
1242 wwin->next = tmp;
1243 wwin->prev = NULL;
1246 /* raise is set to true if we un-hid the app when this window was born.
1247 * we raise, else old windows of this app will be above this new one. */
1248 if (raise)
1249 wRaiseFrame(wwin->frame->core);
1251 /* Update name must come after WApplication stuff is done */
1252 wWindowUpdateName(wwin, title);
1253 if (title)
1254 XFree(title);
1256 XUngrabServer(dpy);
1258 /* Final preparations before window is ready to go */
1259 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1261 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1262 if (((transientOwner && transientOwner->flags.focused)
1263 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1265 /* only auto_focus if on same screen as mouse
1266 * (and same head for xinerama mode)
1267 * TODO: make it an option */
1269 /*TODO add checking the head of the window, is it available? */
1270 short same_screen = 0, same_head = 1;
1272 int foo;
1273 unsigned int bar;
1274 Window dummy;
1276 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1277 &foo, &foo, &foo, &foo, &bar) != False)
1278 same_screen = 1;
1280 if (same_screen == 1 && same_head == 1)
1281 wSetFocusTo(scr, wwin);
1284 wWindowResetMouseGrabs(wwin);
1286 if (!WFLAGP(wwin, no_bind_keys))
1287 wWindowSetKeyGrabs(wwin);
1289 WMPostNotificationName(WMNManaged, wwin, NULL);
1290 wColormapInstallForWindow(scr, scr->cmap_window);
1292 /* Setup Notification Observers */
1293 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1295 /* Cleanup temporary stuff */
1296 if (win_state)
1297 wWindowDeleteSavedState(win_state);
1299 /* If the window must be withdrawed, then do it now.
1300 * Must do some optimization, 'though */
1301 if (withdraw) {
1302 wwin->flags.mapped = 0;
1303 wClientSetState(wwin, WithdrawnState, None);
1304 wUnmanageWindow(wwin, True, False);
1305 wwin = NULL;
1308 return wwin;
1311 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1312 const char *title, int x, int y, int width, int height)
1314 WWindow *wwin;
1315 int foo;
1317 wwin = wWindowCreate();
1319 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1321 wwin->flags.internal_window = 1;
1323 WSETUFLAG(wwin, omnipresent, 1);
1324 WSETUFLAG(wwin, no_shadeable, 1);
1325 WSETUFLAG(wwin, no_resizable, 1);
1326 WSETUFLAG(wwin, no_miniaturizable, 1);
1328 wwin->focus_mode = WFM_PASSIVE;
1329 wwin->client_win = window;
1330 wwin->screen_ptr = scr;
1331 wwin->transient_for = owner;
1332 wwin->client.x = x;
1333 wwin->client.y = y;
1334 wwin->client.width = width;
1335 wwin->client.height = height;
1336 wwin->frame_x = wwin->client.x;
1337 wwin->frame_y = wwin->client.y;
1339 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1340 foo |= WFF_TITLEBAR;
1341 #ifdef XKB_BUTTON_HINT
1342 foo |= WFF_LANGUAGE_BUTTON;
1343 #endif
1345 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1346 wwin->frame_x, wwin->frame_y,
1347 width, height,
1348 &wPreferences.window_title_clearance,
1349 &wPreferences.window_title_min_height,
1350 &wPreferences.window_title_max_height,
1351 foo,
1352 scr->window_title_texture,
1353 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1354 scr->w_depth, scr->w_visual, scr->w_colormap);
1356 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
1358 wwin->frame->flags.is_client_window_frame = 1;
1359 wwin->frame->flags.justification = wPreferences.title_justification;
1361 wFrameWindowChangeTitle(wwin->frame, title);
1363 /* setup button images */
1364 wWindowUpdateButtonImages(wwin);
1366 /* hide buttons */
1367 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1369 wwin->frame->child = wwin;
1370 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1372 #ifdef XKB_BUTTON_HINT
1373 if (wPreferences.modelock)
1374 wwin->frame->on_click_language = windowLanguageClick;
1375 #endif
1377 wwin->frame->on_click_right = windowCloseClick;
1378 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1379 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1380 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1381 wwin->client.y += wwin->frame->top_width;
1383 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1384 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1386 /* setup the frame descriptor */
1387 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1388 wwin->frame->core->descriptor.parent = wwin;
1389 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1391 XLowerWindow(dpy, window);
1392 XMapSubwindows(dpy, wwin->frame->core->window);
1394 /* setup stacking descriptor */
1395 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1396 WWindow *tmp;
1397 tmp = wWindowFor(wwin->transient_for);
1398 if (tmp)
1399 wwin->frame->core->stacking->child_of = tmp->frame->core;
1400 } else {
1401 wwin->frame->core->stacking->child_of = NULL;
1404 if (!scr->focused_window) {
1405 /* first window on the list */
1406 wwin->next = NULL;
1407 wwin->prev = NULL;
1408 scr->focused_window = wwin;
1409 } else {
1410 WWindow *tmp;
1412 /* add window at beginning of focus window list */
1413 tmp = scr->focused_window;
1414 while (tmp->prev)
1415 tmp = tmp->prev;
1416 tmp->prev = wwin;
1417 wwin->next = tmp;
1418 wwin->prev = NULL;
1421 if (wwin->flags.is_gnustep == 0)
1422 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1424 /* if (wPreferences.auto_focus) */
1425 wSetFocusTo(scr, wwin);
1426 wWindowResetMouseGrabs(wwin);
1427 wWindowSetKeyGrabs(wwin);
1429 return wwin;
1433 *----------------------------------------------------------------------
1434 * wUnmanageWindow--
1435 * Removes the frame window from a window and destroys all data
1436 * related to it. The window will be reparented back to the root window
1437 * if restore is True.
1439 * Side effects:
1440 * Everything related to the window is destroyed and the window
1441 * is removed from the window lists. Focus is set to the previous on the
1442 * window list.
1443 *----------------------------------------------------------------------
1445 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1447 WCoreWindow *frame = wwin->frame->core;
1448 WWindow *owner = NULL;
1449 WWindow *newFocusedWindow = NULL;
1450 int wasFocused;
1451 WScreen *scr = wwin->screen_ptr;
1453 /* First close attribute editor window if open */
1454 if (wwin->flags.inspector_open)
1455 wCloseInspectorForWindow(wwin);
1457 /* Close window menu if it's open for this window */
1458 if (wwin->flags.menu_open_for_me)
1459 CloseWindowMenu(scr);
1461 /* Don't restore focus to this window after a window exits
1462 * fullscreen mode */
1463 if (scr->bfs_focused_window == wwin)
1464 scr->bfs_focused_window = NULL;
1466 if (!destroyed) {
1467 if (!wwin->flags.internal_window)
1468 XRemoveFromSaveSet(dpy, wwin->client_win);
1470 /* If this is a leader window, we still need to listen for
1471 * DestroyNotify and PropertyNotify. */
1472 if (wApplicationOf(wwin->client_win))
1473 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1474 else
1475 XSelectInput(dpy, wwin->client_win, NoEventMask);
1477 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1478 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1481 XUnmapWindow(dpy, frame->window);
1483 XUnmapWindow(dpy, wwin->client_win);
1485 /* deselect window */
1486 wSelectWindow(wwin, False);
1488 /* remove all pending events on window */
1489 /* I think this only matters for autoraise */
1490 if (wPreferences.raise_delay)
1491 WMDeleteTimerWithClientData(wwin->frame->core);
1493 XFlush(dpy);
1495 /* reparent the window back to the root */
1496 if (restore)
1497 wClientRestore(wwin);
1499 if (wwin->transient_for != scr->root_win) {
1500 owner = wWindowFor(wwin->transient_for);
1501 if (owner) {
1502 if (!owner->flags.semi_focused)
1503 owner = NULL;
1504 else
1505 owner->flags.semi_focused = 0;
1509 wasFocused = wwin->flags.focused;
1511 /* remove from window focus list */
1512 if (!wwin->prev && !wwin->next) {
1513 /* was the only window */
1514 scr->focused_window = NULL;
1515 newFocusedWindow = NULL;
1516 } else {
1517 WWindow *tmp;
1519 if (wwin->prev)
1520 wwin->prev->next = wwin->next;
1522 if (wwin->next)
1523 wwin->next->prev = wwin->prev;
1524 else {
1525 scr->focused_window = wwin->prev;
1526 scr->focused_window->next = NULL;
1529 if (wPreferences.focus_mode == WKF_CLICK) {
1531 /* if in click to focus mode and the window
1532 * was a transient, focus the owner window
1534 tmp = NULL;
1535 if (wPreferences.focus_mode == WKF_CLICK) {
1536 tmp = wWindowFor(wwin->transient_for);
1537 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1538 tmp = NULL;
1541 /* otherwise, focus the next one in the focus list */
1542 if (!tmp) {
1543 tmp = scr->focused_window;
1544 while (tmp) { /* look for one in the window list first */
1545 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1546 && (tmp->flags.mapped || tmp->flags.shaded))
1547 break;
1548 tmp = tmp->prev;
1550 if (!tmp) { /* if unsuccessful, choose any focusable window */
1551 tmp = scr->focused_window;
1552 while (tmp) {
1553 if (!WFLAGP(tmp, no_focusable)
1554 && (tmp->flags.mapped || tmp->flags.shaded))
1555 break;
1556 tmp = tmp->prev;
1561 newFocusedWindow = tmp;
1563 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1564 unsigned int mask;
1565 int foo;
1566 Window bar, win;
1568 /* This is to let the root window get the keyboard input
1569 * if Sloppy focus mode and no other window get focus.
1570 * This way keybindings will not freeze.
1572 tmp = NULL;
1573 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1574 tmp = wWindowFor(win);
1575 if (tmp == wwin)
1576 tmp = NULL;
1577 newFocusedWindow = tmp;
1578 } else {
1579 newFocusedWindow = NULL;
1583 if (!wwin->flags.internal_window)
1584 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1585 if (wasFocused) {
1586 if (newFocusedWindow != owner && owner) {
1587 if (wwin->flags.is_gnustep == 0)
1588 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1590 wSetFocusTo(scr, newFocusedWindow);
1593 /* Close menu and unhighlight */
1594 WApplication *oapp = wApplicationOf(wwin->main_window);
1595 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1596 if (oapp && oapp != napp) {
1597 wAppMenuUnmap(oapp->menu);
1598 if (wPreferences.highlight_active_app)
1599 wApplicationDeactivate(oapp);
1602 wNETCleanupFrameExtents(wwin);
1604 wWindowDestroy(wwin);
1605 XFlush(dpy);
1608 void wWindowMap(WWindow *wwin)
1610 XMapWindow(dpy, wwin->frame->core->window);
1611 if (!wwin->flags.shaded) {
1612 /* window will be remapped when getting MapNotify */
1613 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1614 XMapWindow(dpy, wwin->client_win);
1615 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1617 wwin->flags.mapped = 1;
1621 void wWindowUnmap(WWindow *wwin)
1623 wwin->flags.mapped = 0;
1625 /* prevent window withdrawal when getting UnmapNotify */
1626 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1627 XUnmapWindow(dpy, wwin->client_win);
1628 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1630 XUnmapWindow(dpy, wwin->frame->core->window);
1633 void wWindowSingleFocus(WWindow *wwin)
1635 WScreen *scr;
1636 int x, y, move = 0;
1638 if (!wwin)
1639 return;
1641 scr = wwin->screen_ptr;
1642 wMakeWindowVisible(wwin);
1644 x = wwin->frame_x;
1645 y = wwin->frame_y;
1647 /* bring window back to visible area */
1648 move = wScreenBringInside(scr, &x, &y, wwin->frame->core->width, wwin->frame->core->height);
1650 if (move) {
1651 wWindowConfigure(wwin, x, y, wwin->client.width, wwin->client.height);
1655 void wWindowFocusPrev(WWindow *wwin, Bool inSameWorkspace)
1657 WWindow *tmp;
1659 if (!wwin || !wwin->prev)
1660 return;
1662 tmp = wwin;
1663 while (tmp->prev)
1664 tmp = tmp->prev;
1666 if (inSameWorkspace)
1667 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1668 tmp = tmp->next;
1670 wWindowSingleFocus(tmp);
1673 void wWindowFocusNext(WWindow *wwin, Bool inSameWorkspace)
1675 WWindow *tmp;
1677 if (!wwin || !wwin->prev)
1678 return;
1680 tmp = wwin->prev;
1681 if (inSameWorkspace)
1682 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1683 tmp = tmp->prev;
1685 wWindowSingleFocus(tmp);
1688 void wWindowFocus(WWindow *wwin, WWindow *owin)
1690 WWindow *nowner;
1691 WWindow *oowner;
1693 #ifdef KEEP_XKB_LOCK_STATUS
1694 if (wPreferences.modelock)
1695 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1696 #endif /* KEEP_XKB_LOCK_STATUS */
1698 wwin->flags.semi_focused = 0;
1700 if (wwin->flags.is_gnustep == 0)
1701 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1703 wwin->flags.focused = 1;
1705 wWindowResetMouseGrabs(wwin);
1707 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1709 if (owin == wwin || !owin)
1710 return;
1712 nowner = wWindowFor(wwin->transient_for);
1714 /* new window is a transient for the old window */
1715 if (nowner == owin) {
1716 owin->flags.semi_focused = 1;
1717 wWindowUnfocus(nowner);
1718 return;
1721 oowner = wWindowFor(owin->transient_for);
1723 /* new window is owner of old window */
1724 if (wwin == oowner) {
1725 wWindowUnfocus(owin);
1726 return;
1729 if (!nowner) {
1730 wWindowUnfocus(owin);
1731 return;
1734 /* new window has same owner of old window */
1735 if (oowner == nowner) {
1736 /* prevent unfocusing of owner */
1737 oowner->flags.semi_focused = 0;
1738 wWindowUnfocus(owin);
1739 oowner->flags.semi_focused = 1;
1741 return;
1744 /* nowner != NULL && oowner != nowner */
1745 nowner->flags.semi_focused = 1;
1746 wWindowUnfocus(nowner);
1747 wWindowUnfocus(owin);
1750 void wWindowUnfocus(WWindow *wwin)
1752 CloseWindowMenu(wwin->screen_ptr);
1754 if (wwin->flags.is_gnustep == 0)
1755 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1757 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1758 WWindow *owner;
1759 owner = wWindowFor(wwin->transient_for);
1760 if (owner && owner->flags.semi_focused) {
1761 owner->flags.semi_focused = 0;
1762 if (owner->flags.mapped || owner->flags.shaded) {
1763 wWindowUnfocus(owner);
1764 wFrameWindowPaint(owner->frame);
1768 wwin->flags.focused = 0;
1769 wWindowResetMouseGrabs(wwin);
1770 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1773 void wWindowUpdateName(WWindow *wwin, const char *newTitle)
1775 const char *title;
1777 if (!wwin->frame)
1778 return;
1780 if (!newTitle)
1781 title = DEF_WINDOW_TITLE; /* the hint was removed */
1782 else
1783 title = newTitle;
1785 if (wFrameWindowChangeTitle(wwin->frame, title))
1786 WMPostNotificationName(WMNChangedName, wwin, NULL);
1790 *----------------------------------------------------------------------
1792 * wWindowConstrainSize--
1793 * Constrains size for the client window, taking the maximal size,
1794 * window resize increments and other size hints into account.
1796 * Returns:
1797 * The closest size to what was given that the client window can
1798 * have.
1800 *----------------------------------------------------------------------
1802 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1804 int width = (int)*nwidth;
1805 int height = (int)*nheight;
1806 int winc = 1;
1807 int hinc = 1;
1808 int minW = 1, minH = 1;
1809 int maxW = wwin->screen_ptr->scr_width * 2;
1810 int maxH = wwin->screen_ptr->scr_height * 2;
1811 int minAX = -1, minAY = -1;
1812 int maxAX = -1, maxAY = -1;
1813 int baseW = 0;
1814 int baseH = 0;
1816 if (wwin->normal_hints) {
1817 winc = wwin->normal_hints->width_inc;
1818 hinc = wwin->normal_hints->height_inc;
1819 minW = wwin->normal_hints->min_width;
1820 minH = wwin->normal_hints->min_height;
1821 maxW = wwin->normal_hints->max_width;
1822 maxH = wwin->normal_hints->max_height;
1823 if (wwin->normal_hints->flags & PAspect) {
1824 minAX = wwin->normal_hints->min_aspect.x;
1825 minAY = wwin->normal_hints->min_aspect.y;
1826 maxAX = wwin->normal_hints->max_aspect.x;
1827 maxAY = wwin->normal_hints->max_aspect.y;
1830 baseW = wwin->normal_hints->base_width;
1831 baseH = wwin->normal_hints->base_height;
1834 if (width < minW)
1835 width = minW;
1836 if (height < minH)
1837 height = minH;
1839 if (width > maxW)
1840 width = maxW;
1841 if (height > maxH)
1842 height = maxH;
1844 /* aspect ratio code borrowed from olwm */
1845 if (minAX > 0) {
1846 /* adjust max aspect ratio */
1847 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1848 if (maxAX > maxAY) {
1849 height = (width * maxAY) / maxAX;
1850 if (height > maxH) {
1851 height = maxH;
1852 width = (height * maxAX) / maxAY;
1854 } else {
1855 width = (height * maxAX) / maxAY;
1856 if (width > maxW) {
1857 width = maxW;
1858 height = (width * maxAY) / maxAX;
1863 /* adjust min aspect ratio */
1864 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1865 if (minAX > minAY) {
1866 height = (width * minAY) / minAX;
1867 if (height < minH) {
1868 height = minH;
1869 width = (height * minAX) / minAY;
1871 } else {
1872 width = (height * minAX) / minAY;
1873 if (width < minW) {
1874 width = minW;
1875 height = (width * minAY) / minAX;
1881 if (baseW != 0)
1882 width = (((width - baseW) / winc) * winc) + baseW;
1883 else
1884 width = (((width - minW) / winc) * winc) + minW;
1886 if (baseH != 0)
1887 height = (((height - baseH) / hinc) * hinc) + baseH;
1888 else
1889 height = (((height - minH) / hinc) * hinc) + minH;
1891 /* broken stupid apps may cause preposterous values for these.. */
1892 if (width > 0)
1893 *nwidth = width;
1894 if (height > 0)
1895 *nheight = height;
1898 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1899 unsigned int *width, unsigned int *height)
1901 int baseW = 0, baseH = 0;
1902 int winc = 1, hinc = 1;
1904 if (wwin->normal_hints) {
1905 baseW = wwin->normal_hints->base_width;
1906 baseH = wwin->normal_hints->base_height;
1908 winc = wwin->normal_hints->width_inc;
1909 hinc = wwin->normal_hints->height_inc;
1912 if (*width > maxW)
1913 *width = maxW - (maxW - baseW) % winc;
1915 if (*height > maxH)
1916 *height = maxH - (maxH - baseH) % hinc;
1919 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1921 WScreen *scr = wwin->screen_ptr;
1922 WApplication *wapp;
1923 int unmap = 0;
1925 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1926 return;
1928 if (workspace != scr->current_workspace) {
1929 /* Sent to other workspace. Unmap window */
1930 if ((wwin->flags.mapped
1931 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1932 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1934 wapp = wApplicationOf(wwin->main_window);
1935 if (wapp)
1936 wapp->last_workspace = workspace;
1938 if (wwin->flags.miniaturized) {
1939 if (wwin->icon) {
1940 XUnmapWindow(dpy, wwin->icon->core->window);
1941 wwin->icon->mapped = 0;
1943 } else {
1944 unmap = 1;
1945 wSetFocusTo(scr, NULL);
1948 } else {
1949 /* brought to current workspace. Map window */
1950 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1951 if (wwin->icon) {
1952 XMapWindow(dpy, wwin->icon->core->window);
1953 wwin->icon->mapped = 1;
1955 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1956 wWindowMap(wwin);
1959 if (!IS_OMNIPRESENT(wwin)) {
1960 int oldWorkspace = wwin->frame->workspace;
1961 wwin->frame->workspace = workspace;
1962 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1965 if (unmap)
1966 wWindowUnmap(wwin);
1969 void wWindowChangeWorkspaceRelative(WWindow *wwin, int amount)
1971 WScreen *scr = wwin->screen_ptr;
1972 int w = scr->current_workspace + amount;
1974 if (amount < 0) {
1975 if (w >= 0) {
1976 wWindowChangeWorkspace(wwin, w);
1977 } else if (wPreferences.ws_cycle) {
1978 wWindowChangeWorkspace(wwin, scr->workspace_count + w);
1980 } else if (amount > 0) {
1981 if (w < scr->workspace_count) {
1982 wWindowChangeWorkspace(wwin, w);
1983 } else if (wPreferences.ws_advance) {
1984 int workspace = WMIN(w, MAX_WORKSPACES - 1);
1985 wWorkspaceMake(scr, workspace);
1986 wWindowChangeWorkspace(wwin, workspace);
1987 } else if (wPreferences.ws_cycle) {
1988 wWindowChangeWorkspace(wwin, w % scr->workspace_count);
1993 void wWindowSynthConfigureNotify(WWindow *wwin)
1995 XEvent sevent;
1997 sevent.type = ConfigureNotify;
1998 sevent.xconfigure.display = dpy;
1999 sevent.xconfigure.event = wwin->client_win;
2000 sevent.xconfigure.window = wwin->client_win;
2002 sevent.xconfigure.x = wwin->client.x;
2003 sevent.xconfigure.y = wwin->client.y;
2004 sevent.xconfigure.width = wwin->client.width;
2005 sevent.xconfigure.height = wwin->client.height;
2007 sevent.xconfigure.border_width = wwin->old_border_width;
2008 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2009 sevent.xconfigure.above = wwin->frame->titlebar->window;
2010 else
2011 sevent.xconfigure.above = None;
2013 sevent.xconfigure.override_redirect = False;
2014 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2015 XFlush(dpy);
2019 *----------------------------------------------------------------------
2020 * wWindowConfigure()
2022 * req_x, req_y: new requested positions for the frame
2023 * req_width, req_height: new requested sizes for the client
2025 * Configures the frame, decorations and client window to the specified
2026 * geometry, whose validity is not checked -- wWindowConstrainSize()
2027 * must be used for that.
2028 * The size parameters are for the client window, but the position is
2029 * for the frame.
2030 * The client window receives a ConfigureNotify event, according
2031 * to what ICCCM says.
2033 * Returns:
2034 * None
2036 * Side effects:
2037 * Window size and position are changed and client window receives
2038 * a ConfigureNotify event.
2039 *----------------------------------------------------------------------
2041 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2043 int synth_notify = False;
2044 int resize;
2046 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2048 * if the window is being moved but not resized then
2049 * send a synthetic ConfigureNotify
2051 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2052 synth_notify = True;
2054 if (WFLAGP(wwin, dont_move_off))
2055 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2057 if (resize) {
2058 if (req_width < MIN_WINDOW_SIZE)
2059 req_width = MIN_WINDOW_SIZE;
2060 if (req_height < MIN_WINDOW_SIZE)
2061 req_height = MIN_WINDOW_SIZE;
2063 /* If growing, resize inner part before frame,
2064 * if shrinking, resize frame before.
2065 * This will prevent the frame (that can have a different color)
2066 * to be exposed, causing flicker */
2067 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2068 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2070 if (wwin->flags.shaded) {
2071 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2072 wwin->old_geometry.height = req_height;
2073 } else {
2074 int h;
2076 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2078 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2081 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2082 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2084 wwin->client.x = req_x;
2085 wwin->client.y = req_y + wwin->frame->top_width;
2086 wwin->client.width = req_width;
2087 wwin->client.height = req_height;
2088 } else {
2089 wwin->client.x = req_x;
2090 wwin->client.y = req_y + wwin->frame->top_width;
2092 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2094 wwin->frame_x = req_x;
2095 wwin->frame_y = req_y;
2096 if (HAS_BORDER(wwin)) {
2097 wwin->client.x += wwin->screen_ptr->frame_border_width;
2098 wwin->client.y += wwin->screen_ptr->frame_border_width;
2100 #ifdef USE_XSHAPE
2101 if (w_global.xext.shape.supported && wwin->flags.shaped && resize)
2102 wWindowSetShape(wwin);
2103 #endif
2105 if (synth_notify)
2106 wWindowSynthConfigureNotify(wwin);
2108 wNETFrameExtents(wwin);
2110 XFlush(dpy);
2113 /* req_x, req_y: new position of the frame */
2114 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2116 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2117 int synth_notify = False;
2119 /* Send a synthetic ConfigureNotify event for every window movement. */
2120 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2121 synth_notify = True;
2122 #else
2123 /* A single synthetic ConfigureNotify event is sent at the end of
2124 * a completed (opaque) movement in moveres.c */
2125 #endif
2127 if (WFLAGP(wwin, dont_move_off))
2128 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2129 wwin->frame->core->width, wwin->frame->core->height);
2131 wwin->client.x = req_x;
2132 wwin->client.y = req_y + wwin->frame->top_width;
2133 if (HAS_BORDER(wwin)) {
2134 wwin->client.x += wwin->screen_ptr->frame_border_width;
2135 wwin->client.y += wwin->screen_ptr->frame_border_width;
2138 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2140 wwin->frame_x = req_x;
2141 wwin->frame_y = req_y;
2143 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2144 if (synth_notify)
2145 wWindowSynthConfigureNotify(wwin);
2146 #endif
2149 void wWindowUpdateButtonImages(WWindow *wwin)
2151 WScreen *scr = wwin->screen_ptr;
2152 Pixmap pixmap, mask;
2153 WFrameWindow *fwin = wwin->frame;
2155 if (!HAS_TITLEBAR(wwin))
2156 return;
2158 /* miniaturize button */
2159 if (!WFLAGP(wwin, no_miniaturize_button)) {
2160 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2161 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2163 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2164 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2165 else
2166 mask = None;
2168 if (fwin->lbutton_image
2169 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2170 wPixmapDestroy(fwin->lbutton_image);
2171 fwin->lbutton_image = NULL;
2174 if (!fwin->lbutton_image) {
2175 fwin->lbutton_image = wPixmapCreate(pixmap, mask);
2176 fwin->lbutton_image->client_owned = 1;
2177 fwin->lbutton_image->client_owned_mask = 1;
2179 } else {
2180 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2181 wPixmapDestroy(fwin->lbutton_image);
2183 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2186 #ifdef XKB_BUTTON_HINT
2187 if (!WFLAGP(wwin, no_language_button)) {
2188 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2189 wPixmapDestroy(fwin->languagebutton_image);
2191 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2193 #endif
2195 /* close button */
2197 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2198 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2200 if (!WFLAGP(wwin, no_close_button)) {
2201 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2202 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2204 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2205 mask = wwin->wm_gnustep_attr->close_mask;
2206 else
2207 mask = None;
2209 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2210 || fwin->rbutton_image->mask != mask)) {
2211 wPixmapDestroy(fwin->rbutton_image);
2212 fwin->rbutton_image = NULL;
2215 if (!fwin->rbutton_image) {
2216 fwin->rbutton_image = wPixmapCreate(pixmap, mask);
2217 fwin->rbutton_image->client_owned = 1;
2218 fwin->rbutton_image->client_owned_mask = 1;
2221 } else if (WFLAGP(wwin, kill_close)) {
2222 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2223 wPixmapDestroy(fwin->rbutton_image);
2225 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2227 } else if (MGFLAGP(wwin, broken_close)) {
2228 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2229 wPixmapDestroy(fwin->rbutton_image);
2231 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2233 } else {
2234 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2235 wPixmapDestroy(fwin->rbutton_image);
2237 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2241 /* force buttons to be redrawn */
2242 fwin->flags.need_texture_change = 1;
2243 wFrameWindowPaint(fwin);
2247 *---------------------------------------------------------------------------
2248 * wWindowConfigureBorders--
2249 * Update window border configuration according to attribute flags.
2251 *---------------------------------------------------------------------------
2253 void wWindowConfigureBorders(WWindow *wwin)
2255 if (wwin->frame) {
2256 int flags;
2257 int newy, oldh;
2259 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2261 #ifdef XKB_BUTTON_HINT
2262 if (wPreferences.modelock)
2263 flags |= WFF_LANGUAGE_BUTTON;
2264 #endif
2266 if (HAS_TITLEBAR(wwin))
2267 flags |= WFF_TITLEBAR;
2268 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2269 flags |= WFF_RESIZEBAR;
2270 if (HAS_BORDER(wwin))
2271 flags |= WFF_BORDER;
2272 if (wwin->flags.shaded)
2273 flags |= WFF_IS_SHADED;
2274 if (wwin->flags.selected)
2275 flags |= WFF_SELECTED;
2277 oldh = wwin->frame->top_width;
2278 wFrameWindowUpdateBorders(wwin->frame, flags);
2279 if (oldh != wwin->frame->top_width) {
2280 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2282 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2283 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2286 flags = 0;
2287 if (!WFLAGP(wwin, no_miniaturize_button)
2288 && wwin->frame->flags.hide_left_button)
2289 flags |= WFF_LEFT_BUTTON;
2291 #ifdef XKB_BUTTON_HINT
2292 if (!WFLAGP(wwin, no_language_button)
2293 && wwin->frame->flags.hide_language_button)
2294 flags |= WFF_LANGUAGE_BUTTON;
2295 #endif
2297 if (!WFLAGP(wwin, no_close_button)
2298 && wwin->frame->flags.hide_right_button)
2299 flags |= WFF_RIGHT_BUTTON;
2301 if (flags != 0) {
2302 wWindowUpdateButtonImages(wwin);
2303 wFrameWindowShowButton(wwin->frame, flags);
2306 flags = 0;
2307 if (WFLAGP(wwin, no_miniaturize_button)
2308 && !wwin->frame->flags.hide_left_button)
2309 flags |= WFF_LEFT_BUTTON;
2311 #ifdef XKB_BUTTON_HINT
2312 if (WFLAGP(wwin, no_language_button)
2313 && !wwin->frame->flags.hide_language_button)
2314 flags |= WFF_LANGUAGE_BUTTON;
2315 #endif
2317 if (WFLAGP(wwin, no_close_button)
2318 && !wwin->frame->flags.hide_right_button)
2319 flags |= WFF_RIGHT_BUTTON;
2321 if (flags != 0)
2322 wFrameWindowHideButton(wwin->frame, flags);
2324 #ifdef USE_XSHAPE
2325 if (w_global.xext.shape.supported && wwin->flags.shaped)
2326 wWindowSetShape(wwin);
2327 #endif
2331 void wWindowSaveState(WWindow *wwin)
2333 long data[10];
2334 int i;
2336 memset(data, 0, sizeof(long) * 10);
2337 data[0] = wwin->frame->workspace;
2338 data[1] = wwin->flags.miniaturized;
2339 data[2] = wwin->flags.shaded;
2340 data[3] = wwin->flags.hidden;
2341 data[4] = wwin->flags.maximized;
2342 if (wwin->flags.maximized == 0) {
2343 data[5] = wwin->frame_x;
2344 data[6] = wwin->frame_y;
2345 data[7] = wwin->frame->core->width;
2346 data[8] = wwin->frame->core->height;
2347 } else {
2348 data[5] = wwin->old_geometry.x;
2349 data[6] = wwin->old_geometry.y;
2350 data[7] = wwin->old_geometry.width;
2351 data[8] = wwin->old_geometry.height;
2354 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2355 if (wwin->screen_ptr->shortcutWindows[i] &&
2356 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2357 data[9] |= 1 << i;
2360 XChangeProperty(dpy, wwin->client_win, w_global.atom.wmaker.state,
2361 w_global.atom.wmaker.state, 32, PropModeReplace, (unsigned char *)data, 10);
2364 static int getSavedState(Window window, WSavedState ** state)
2366 Atom type_ret;
2367 int fmt_ret;
2368 unsigned long nitems_ret;
2369 unsigned long bytes_after_ret;
2370 long *data;
2372 if (XGetWindowProperty(dpy, window, w_global.atom.wmaker.state, 0, 10,
2373 True, w_global.atom.wmaker.state,
2374 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2375 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2376 return 0;
2378 if (type_ret != w_global.atom.wmaker.state) {
2379 XFree(data);
2380 return 0;
2383 *state = wmalloc(sizeof(WSavedState));
2385 (*state)->workspace = data[0];
2386 (*state)->miniaturized = data[1];
2387 (*state)->shaded = data[2];
2388 (*state)->hidden = data[3];
2389 (*state)->maximized = data[4];
2390 (*state)->x = data[5];
2391 (*state)->y = data[6];
2392 (*state)->w = data[7];
2393 (*state)->h = data[8];
2394 (*state)->window_shortcuts = data[9];
2396 XFree(data);
2398 return 1;
2401 #ifdef USE_XSHAPE
2402 void wWindowClearShape(WWindow * wwin)
2404 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2405 0, wwin->frame->top_width, None, ShapeSet);
2406 XFlush(dpy);
2409 void wWindowSetShape(WWindow * wwin)
2411 XRectangle rect[2];
2412 int count;
2413 #ifdef OPTIMIZE_SHAPE
2414 XRectangle *rects;
2415 XRectangle *urec;
2416 int ordering;
2418 /* only shape is the client's */
2419 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2420 goto alt_code;
2422 /* Get array of rectangles describing the shape mask */
2423 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2424 if (!rects)
2425 goto alt_code;
2427 urec = malloc(sizeof(XRectangle) * (count + 2));
2428 if (!urec) {
2429 XFree(rects);
2430 goto alt_code;
2433 /* insert our decoration rectangles in the rect list */
2434 memcpy(urec, rects, sizeof(XRectangle) * count);
2435 XFree(rects);
2437 if (HAS_TITLEBAR(wwin)) {
2438 urec[count].x = -1;
2439 urec[count].y = -1 - wwin->frame->top_width;
2440 urec[count].width = wwin->frame->core->width + 2;
2441 urec[count].height = wwin->frame->top_width + 1;
2442 count++;
2444 if (HAS_RESIZEBAR(wwin)) {
2445 urec[count].x = -1;
2446 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2447 urec[count].width = wwin->frame->core->width + 2;
2448 urec[count].height = wwin->frame->bottom_width + 1;
2449 count++;
2452 /* shape our frame window */
2453 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2454 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2455 XFlush(dpy);
2456 wfree(urec);
2457 return;
2459 alt_code:
2460 #endif /* OPTIMIZE_SHAPE */
2461 count = 0;
2462 if (HAS_TITLEBAR(wwin)) {
2463 rect[count].x = -1;
2464 rect[count].y = -1;
2465 rect[count].width = wwin->frame->core->width + 2;
2466 rect[count].height = wwin->frame->top_width + 1;
2467 count++;
2469 if (HAS_RESIZEBAR(wwin)) {
2470 rect[count].x = -1;
2471 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2472 rect[count].width = wwin->frame->core->width + 2;
2473 rect[count].height = wwin->frame->bottom_width + 1;
2474 count++;
2476 if (count > 0) {
2477 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2478 0, 0, rect, count, ShapeSet, Unsorted);
2480 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2481 0, wwin->frame->top_width, wwin->client_win,
2482 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2483 XFlush(dpy);
2485 #endif /* USE_XSHAPE */
2487 /* ====================================================================== */
2489 static FocusMode getFocusMode(WWindow * wwin)
2491 FocusMode mode;
2493 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2494 if (wwin->wm_hints->input == True) {
2495 if (wwin->protocols.TAKE_FOCUS)
2496 mode = WFM_LOCALLY_ACTIVE;
2497 else
2498 mode = WFM_PASSIVE;
2499 } else {
2500 if (wwin->protocols.TAKE_FOCUS)
2501 mode = WFM_GLOBALLY_ACTIVE;
2502 else
2503 mode = WFM_NO_INPUT;
2505 } else {
2506 mode = WFM_PASSIVE;
2508 return mode;
2511 void wWindowSetKeyGrabs(WWindow * wwin)
2513 int i;
2514 WShortKey *key;
2516 for (i = 0; i < WKBD_LAST; i++) {
2517 key = &wKeyBindings[i];
2519 if (key->keycode == 0)
2520 continue;
2521 if (key->modifier != AnyModifier) {
2522 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2523 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2524 #ifdef NUMLOCK_HACK
2525 /* Also grab all modifier combinations possible that include,
2526 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2527 * work even if the NumLock/ScrollLock key is on.
2529 wHackedGrabKey(key->keycode, key->modifier,
2530 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2531 #endif
2533 XGrabKey(dpy, key->keycode, key->modifier,
2534 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2537 wRootMenuBindShortcuts(wwin->frame->core->window);
2540 void wWindowResetMouseGrabs(WWindow * wwin)
2542 /* Mouse grabs can't be done on the client window because of
2543 * ICCCM and because clients that try to do the same will crash.
2545 * But there is a problem wich makes tbar buttons of unfocused
2546 * windows not usable as the click goes to the frame window instead
2547 * of the button itself. Must figure a way to fix that.
2550 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2552 if (!WFLAGP(wwin, no_bind_mouse)) {
2553 /* grabs for Meta+drag */
2554 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2555 True, ButtonPressMask | ButtonReleaseMask,
2556 GrabModeSync, GrabModeAsync, None, None);
2558 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2559 * but we only grab it for Button4 and Button 5 since a lot of apps
2560 * use CTRL+Button1-3 for app related functionality
2562 if (wPreferences.resize_increment > 0) {
2563 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2564 True, ButtonPressMask | ButtonReleaseMask,
2565 GrabModeSync, GrabModeAsync, None, None);
2566 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2567 True, ButtonPressMask | ButtonReleaseMask,
2568 GrabModeSync, GrabModeAsync, None, None);
2570 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2571 True, ButtonPressMask | ButtonReleaseMask,
2572 GrabModeSync, GrabModeAsync, None, None);
2573 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2574 True, ButtonPressMask | ButtonReleaseMask,
2575 GrabModeSync, GrabModeAsync, None, None);
2579 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2580 && !wwin->flags.is_gnustep) {
2581 /* the passive grabs to focus the window */
2582 /* if (wPreferences.focus_mode == WKF_CLICK) */
2583 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2584 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2586 XFlush(dpy);
2589 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2591 if (attr->flags & GSExtraFlagsAttr) {
2592 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2593 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2594 wWindowUpdateButtonImages(wwin);
2599 WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
2600 const char *command, pid_t pid, WSavedState * state)
2602 WWindowState *wstate;
2604 wstate = malloc(sizeof(WWindowState));
2605 if (!wstate)
2606 return NULL;
2608 memset(wstate, 0, sizeof(WWindowState));
2609 wstate->pid = pid;
2610 if (instance)
2611 wstate->instance = wstrdup(instance);
2612 if (class)
2613 wstate->class = wstrdup(class);
2614 if (command)
2615 wstate->command = wstrdup(command);
2616 wstate->state = state;
2618 wstate->next = windowState;
2619 windowState = wstate;
2621 return wstate;
2624 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2626 WMagicNumber wWindowGetSavedState(Window win)
2628 char *instance, *class, *command = NULL;
2629 WWindowState *wstate = windowState;
2631 if (!wstate)
2632 return NULL;
2634 command = GetCommandForWindow(win);
2635 if (!command)
2636 return NULL;
2638 if (PropGetWMClass(win, &class, &instance)) {
2639 while (wstate) {
2640 if (SAME(instance, wstate->instance) &&
2641 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2642 break;
2644 wstate = wstate->next;
2646 } else {
2647 wstate = NULL;
2650 if (command)
2651 wfree(command);
2652 if (instance)
2653 free(instance);
2654 if (class)
2655 free(class);
2657 return wstate;
2660 void wWindowDeleteSavedState(WMagicNumber id)
2662 WWindowState *tmp, *wstate = (WWindowState *) id;
2664 if (!wstate || !windowState)
2665 return;
2667 tmp = windowState;
2668 if (tmp == wstate) {
2669 windowState = wstate->next;
2670 release_wwindowstate(wstate);
2671 } else {
2672 while (tmp->next) {
2673 if (tmp->next == wstate) {
2674 tmp->next = wstate->next;
2675 release_wwindowstate(wstate);
2676 break;
2678 tmp = tmp->next;
2683 void wWindowDeleteSavedStatesForPID(pid_t pid)
2685 WWindowState *tmp, *wstate;
2687 if (!windowState)
2688 return;
2690 tmp = windowState;
2691 if (tmp->pid == pid) {
2692 wstate = windowState;
2693 windowState = tmp->next;
2695 release_wwindowstate(wstate);
2696 } else {
2697 while (tmp->next) {
2698 if (tmp->next->pid == pid) {
2699 wstate = tmp->next;
2700 tmp->next = wstate->next;
2701 release_wwindowstate(wstate);
2702 break;
2704 tmp = tmp->next;
2709 static void release_wwindowstate(WWindowState *wstate)
2711 if (wstate->instance)
2712 wfree(wstate->instance);
2714 if (wstate->class)
2715 wfree(wstate->class);
2717 if (wstate->command)
2718 wfree(wstate->command);
2720 wfree(wstate->state);
2721 wfree(wstate);
2724 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2726 if (wwin->flags.omnipresent == flag)
2727 return;
2729 wwin->flags.omnipresent = flag;
2730 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2733 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2735 WWindow *wwin = data;
2737 /* Parameter not used, but tell the compiler that it is ok */
2738 (void) sender;
2740 #ifndef NUMLOCK_HACK
2741 if ((event->xbutton.state & ValidModMask)
2742 != (event->xbutton.state & ~LockMask)) {
2743 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2744 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2746 #endif
2748 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2750 CloseWindowMenu(wwin->screen_ptr);
2752 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2753 && !WFLAGP(wwin, no_focusable)) {
2754 wSetFocusTo(wwin->screen_ptr, wwin);
2757 if (event->xbutton.button == Button1)
2758 wRaiseFrame(wwin->frame->core);
2760 if (event->xbutton.window != wwin->frame->resizebar->window) {
2761 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2762 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2763 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2764 return;
2768 if (event->xbutton.state & MOD_MASK) {
2769 /* move the window */
2770 wMouseMoveWindow(wwin, event);
2771 XUngrabPointer(dpy, CurrentTime);
2772 } else {
2773 wMouseResizeWindow(wwin, event);
2774 XUngrabPointer(dpy, CurrentTime);
2778 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2780 WWindow *wwin = data;
2782 /* Parameter not used, but tell the compiler that it is ok */
2783 (void) sender;
2785 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2787 if (event->xbutton.button == Button1) {
2788 if (event->xbutton.state == 0) {
2789 if (!WFLAGP(wwin, no_shadeable)) {
2790 /* shade window */
2791 if (wwin->flags.shaded)
2792 wUnshadeWindow(wwin);
2793 else
2794 wShadeWindow(wwin);
2796 } else {
2797 int dir = 0;
2799 if (event->xbutton.state & ControlMask)
2800 dir |= MAX_VERTICAL;
2802 if (event->xbutton.state & ShiftMask) {
2803 dir |= MAX_HORIZONTAL;
2804 if (!(event->xbutton.state & ControlMask))
2805 wSelectWindow(wwin, !wwin->flags.selected);
2808 /* maximize window */
2809 if (dir != 0 && IS_RESIZABLE(wwin)) {
2810 int ndir = dir ^ wwin->flags.maximized;
2812 if (ndir != 0)
2813 wMaximizeWindow(wwin, ndir);
2814 else
2815 wUnmaximizeWindow(wwin);
2818 } else if (event->xbutton.button == Button3) {
2819 if (event->xbutton.state & MOD_MASK)
2820 wHideOtherApplications(wwin);
2821 } else if (event->xbutton.button == Button2) {
2822 wSelectWindow(wwin, !wwin->flags.selected);
2823 } else if (event->xbutton.button == W_getconf_mouseWheelUp()) {
2824 wShadeWindow(wwin);
2825 } else if (event->xbutton.button == W_getconf_mouseWheelDown()) {
2826 wUnshadeWindow(wwin);
2830 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2832 WWindow *wwin = desc->parent;
2833 unsigned int new_width, w_scale;
2834 unsigned int new_height, h_scale;
2835 unsigned int resize_width_increment = 0;
2836 unsigned int resize_height_increment = 0;
2838 if (wwin->normal_hints) {
2839 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2840 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2841 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2842 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2844 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2845 resize_width_increment = wPreferences.resize_increment;
2846 resize_height_increment = wPreferences.resize_increment;
2849 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2851 CloseWindowMenu(wwin->screen_ptr);
2853 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2854 wSetFocusTo(wwin->screen_ptr, wwin);
2856 if (event->xbutton.button == Button1)
2857 wRaiseFrame(wwin->frame->core);
2859 if (event->xbutton.state & ControlMask) {
2860 if (event->xbutton.button == Button4) {
2861 new_width = wwin->client.width - resize_width_increment;
2862 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2863 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2865 if (event->xbutton.button == Button5) {
2866 new_width = wwin->client.width + resize_width_increment;
2867 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2868 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2872 if (event->xbutton.state & MOD_MASK) {
2873 /* move the window */
2874 if (XGrabPointer(dpy, wwin->client_win, False,
2875 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2876 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2877 return;
2879 if (event->xbutton.button == Button3) {
2880 wMouseResizeWindow(wwin, event);
2881 } else if (event->xbutton.button == Button4) {
2882 new_height = wwin->client.height - resize_height_increment;
2883 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2884 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2885 } else if (event->xbutton.button == Button5) {
2886 new_height = wwin->client.height + resize_height_increment;
2887 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2888 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2889 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2890 wMouseMoveWindow(wwin, event);
2892 XUngrabPointer(dpy, CurrentTime);
2896 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2898 WWindow *wwin = (WWindow *) data;
2900 /* Parameter not used, but tell the compiler that it is ok */
2901 (void) sender;
2903 #ifndef NUMLOCK_HACK
2904 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2905 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2906 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2907 #endif
2908 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2910 CloseWindowMenu(wwin->screen_ptr);
2912 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2913 && !WFLAGP(wwin, no_focusable))
2914 wSetFocusTo(wwin->screen_ptr, wwin);
2916 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2918 if (event->xbutton.button == Button1) {
2919 if (event->xbutton.state & MOD_MASK)
2920 wLowerFrame(wwin->frame->core);
2921 else
2922 wRaiseFrame(wwin->frame->core);
2924 if ((event->xbutton.state & ShiftMask)
2925 && !(event->xbutton.state & ControlMask)) {
2926 wSelectWindow(wwin, !wwin->flags.selected);
2927 return;
2929 if (event->xbutton.window != wwin->frame->titlebar->window
2930 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2931 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2932 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2933 return;
2936 /* move the window */
2937 wMouseMoveWindow(wwin, event);
2939 XUngrabPointer(dpy, CurrentTime);
2940 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2941 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2942 WObjDescriptor *desc;
2944 if (event->xbutton.window != wwin->frame->titlebar->window
2945 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2946 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2947 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2948 return;
2951 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2953 /* allow drag select */
2954 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2955 event->xany.send_event = True;
2956 (*desc->handle_mousedown) (desc, event);
2958 XUngrabPointer(dpy, CurrentTime);
2962 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2964 WWindow *wwin = data;
2966 /* Parameter not used, but tell the compiler that it is ok */
2967 (void) sender;
2969 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2971 CloseWindowMenu(wwin->screen_ptr);
2973 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2974 return;
2976 /* if control-click, kill the client */
2977 if (event->xbutton.state & ControlMask) {
2978 wClientKill(wwin);
2979 } else {
2980 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2981 /* send delete message */
2982 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
2983 w_global.timestamp.last_event);
2988 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2990 WWindow *wwin = data;
2992 /* Parameter not used, but tell the compiler that it is ok */
2993 (void) sender;
2995 CloseWindowMenu(wwin->screen_ptr);
2997 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2998 return;
3000 /* send delete message */
3001 if (wwin->protocols.DELETE_WINDOW)
3002 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3003 w_global.timestamp.last_event);
3004 else
3005 wClientKill(wwin);
3008 #ifdef XKB_BUTTON_HINT
3009 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3011 WWindow *wwin = data;
3012 WFrameWindow *fwin = wwin->frame;
3013 WScreen *scr = fwin->screen_ptr;
3014 int tl;
3016 /* Parameter not used, but tell the compiler that it is ok */
3017 (void) sender;
3019 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3020 return;
3021 tl = wwin->frame->languagemode;
3022 wwin->frame->languagemode = wwin->frame->last_languagemode;
3023 wwin->frame->last_languagemode = tl;
3024 wSetFocusTo(scr, wwin);
3025 wwin->frame->languagebutton_image =
3026 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3027 wFrameWindowUpdateLanguageButton(wwin->frame);
3028 if (event->xbutton.button == Button3)
3029 return;
3030 wRaiseFrame(fwin->core);
3032 #endif
3034 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3036 WWindow *wwin = data;
3038 /* Parameter not used, but tell the compiler that it is ok */
3039 (void) sender;
3041 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3043 CloseWindowMenu(wwin->screen_ptr);
3045 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3046 return;
3048 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3049 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
3050 w_global.timestamp.last_event);
3051 } else {
3052 WApplication *wapp;
3053 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3055 wapp = wApplicationOf(wwin->main_window);
3056 if (wapp && !WFLAGP(wwin, no_appicon))
3057 wHideApplication(wapp);
3058 } else if (event->xbutton.state == 0) {
3059 wIconifyWindow(wwin);