WPrefs: Code formatting in TexturePanel.c; minimizes checkpatch.pl warnings.
[wmaker-crm.git] / src / window.c
blob801d7b6cb83e57758dc20ae8e5c54adb3c805bfe
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 USE_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 static void discard_hints_from_gtk(WWindow *wwin)
293 Atom type;
294 int format;
295 unsigned long nb_item, nb_remain;
296 unsigned char *result;
297 int status;
299 status = XGetWindowProperty(dpy, wwin->client_win, w_global.atom.desktop.gtk_object_path, 0, 16, False,
300 AnyPropertyType, &type, &format, &nb_item, &nb_remain, &result);
301 if (status != Success)
302 return;
304 /* If we're here, that means the Property exists. We don't care what is inside, it means it is a GTK-based application */
306 if (result != NULL)
307 XFree(result);
309 /* GTK is asking to remove these decorations: */
310 wwin->client_flags.no_titlebar = 0;
311 wwin->client_flags.no_close_button = 0;
312 wwin->client_flags.no_miniaturize_button = 0;
313 wwin->client_flags.no_resizebar = 0;
316 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
318 WScreen *scr = wwin->screen_ptr;
320 /* sets global default stuff */
321 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags, NULL, True);
322 wwin->defined_user_flags = wwin->user_flags;
325 * Decoration setting is done in this precedence (lower to higher)
326 * - use global default in the resource database
327 * - guess some settings
328 * - use GNUstep/external window attributes
329 * - set hints specified for the app in the resource DB
332 wwin->client_flags.broken_close = 0;
334 if (wwin->protocols.DELETE_WINDOW)
335 wwin->client_flags.kill_close = 0;
336 else
337 wwin->client_flags.kill_close = 1;
339 /* transients can't be iconified or maximized */
340 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
341 wwin->client_flags.no_miniaturizable = 1;
342 wwin->client_flags.no_miniaturize_button = 1;
345 /* if the window can't be resized, remove the resizebar */
346 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
347 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
348 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
349 wwin->client_flags.no_resizable = 1;
350 wwin->client_flags.no_resizebar = 1;
353 /* set GNUstep window attributes */
354 if (wwin->wm_gnustep_attr) {
355 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
357 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
359 *level = wwin->wm_gnustep_attr->window_level;
361 * INT_MIN is the only illegal window level.
363 if (*level == INT_MIN)
364 *level = INT_MIN + 1;
365 } else {
366 /* setup defaults */
367 *level = WMNormalLevel;
369 } else {
370 int tmp_workspace = -1;
371 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
373 #ifdef USE_MWM_HINTS
374 wMWMCheckClientHints(wwin);
375 #endif /* USE_MWM_HINTS */
377 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
379 if (wPreferences.ignore_gtk_decoration_hints)
380 discard_hints_from_gtk(wwin);
382 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
383 * have INT_MIN that means that no window level was requested. -Dan
385 if (tmp_level == INT_MIN) {
386 if (WFLAGP(wwin, floating))
387 *level = WMFloatingLevel;
388 else if (WFLAGP(wwin, sunken))
389 *level = WMSunkenLevel;
390 else
391 *level = WMNormalLevel;
392 } else {
393 *level = tmp_level;
396 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
397 WWindow *transientOwner = wWindowFor(wwin->transient_for);
398 if (transientOwner) {
399 int ownerLevel = transientOwner->frame->core->stacking->window_level;
400 if (ownerLevel > *level)
401 *level = ownerLevel;
405 if (tmp_workspace >= 0)
406 *workspace = tmp_workspace % scr->workspace_count;
410 * Set attributes specified only for that window/class.
411 * This might do duplicate work with the 1st wDefaultFillAttributes().
413 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
414 &wwin->defined_user_flags, False);
416 * Sanity checks for attributes that depend on other attributes
418 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
419 wwin->user_flags.emulate_appicon = 0;
421 if (wwin->main_window != None) {
422 WApplication *wapp = wApplicationOf(wwin->main_window);
423 if (wapp && !wapp->flags.emulated)
424 wwin->user_flags.emulate_appicon = 0;
427 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
428 wwin->user_flags.emulate_appicon = 0;
430 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
431 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
432 wwin->user_flags.sunken = 0;
434 /* A window that does not have a title cannot be Shaded and we don't let user override this */
435 wwin->client_flags.no_shadeable = WFLAGP(wwin, no_titlebar);
436 wwin->defined_user_flags.no_shadeable = 0;
438 /* windows that have takefocus=False shouldn't take focus at all */
439 if (wwin->focus_mode == WFM_NO_INPUT)
440 wwin->client_flags.no_focusable = 1;
443 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
445 int w1, h1, w2, h2;
447 w1 = wwin->frame->core->width;
448 h1 = wwin->frame->core->height;
449 w2 = obscured->frame->core->width;
450 h2 = obscured->frame->core->height;
452 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
453 && wwin->frame->workspace != obscured->frame->workspace)
454 return False;
456 if (wwin->frame_x + w1 < obscured->frame_x
457 || wwin->frame_y + h1 < obscured->frame_y
458 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
459 return False;
461 return True;
464 static void fixLeaderProperties(WWindow *wwin)
466 XClassHint *classHint;
467 XWMHints *hints, *clientHints;
468 XWindowAttributes attr;
469 Window leaders[2], window;
470 char **argv, *command;
471 int argc, i, pid;
472 Bool haveCommand;
474 classHint = XAllocClassHint();
475 clientHints = XGetWMHints(dpy, wwin->client_win);
476 pid = wNETWMGetPidForWindow(wwin->client_win);
477 if (pid > 0)
478 haveCommand = GetCommandForPid(pid, &argv, &argc);
479 else
480 haveCommand = False;
482 leaders[0] = wwin->client_leader;
483 leaders[1] = wwin->group_id;
485 if (haveCommand) {
486 command = GetCommandForWindow(wwin->client_win);
487 if (command)
488 wfree(command); /* command already set. nothing to do. */
489 else
490 XSetCommand(dpy, wwin->client_win, argv, argc);
493 for (i = 0; i < 2; i++) {
494 window = leaders[i];
495 if (window) {
496 if (XGetClassHint(dpy, window, classHint) == 0) {
497 classHint->res_name = wwin->wm_instance;
498 classHint->res_class = wwin->wm_class;
499 XSetClassHint(dpy, window, classHint);
501 hints = XGetWMHints(dpy, window);
502 if (hints) {
503 XFree(hints);
504 } else if (clientHints) {
505 /* set window group leader to self */
506 clientHints->window_group = window;
507 clientHints->flags |= WindowGroupHint;
508 XSetWMHints(dpy, window, clientHints);
511 if (haveCommand) {
512 command = GetCommandForWindow(window);
513 if (command)
514 wfree(command); /* command already set. nothing to do. */
515 else
516 XSetCommand(dpy, window, argv, argc);
519 /* Make sure we get notification when this window is destroyed */
520 if (XGetWindowAttributes(dpy, window, &attr))
521 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
525 XFree(classHint);
526 if (clientHints)
527 XFree(clientHints);
528 if (haveCommand)
529 wfree(argv);
532 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
534 XClassHint *classHint;
535 XWMHints *hints;
536 Window leader;
537 int argc;
538 char **argv;
540 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
541 /* set class hint */
542 classHint = XAllocClassHint();
543 classHint->res_name = instance;
544 classHint->res_class = class;
545 XSetClassHint(dpy, leader, classHint);
546 XFree(classHint);
548 /* inherit these from the original leader if available */
549 hints = XGetWMHints(dpy, win);
550 if (!hints) {
551 hints = XAllocWMHints();
552 hints->flags = 0;
554 /* set window group leader to self */
555 hints->window_group = leader;
556 hints->flags |= WindowGroupHint;
557 XSetWMHints(dpy, leader, hints);
558 XFree(hints);
560 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
561 XSetCommand(dpy, leader, argv, argc);
562 XFreeStringList(argv);
565 return leader;
568 static int matchIdentifier(const void *item, const void *cdata)
570 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
574 *----------------------------------------------------------------
575 * wManageWindow--
576 * reparents the window and allocates a descriptor for it.
577 * Window manager hints and other hints are fetched to configure
578 * the window decoration attributes and others. User preferences
579 * for the window are used if available, to configure window
580 * decorations and some behaviour.
581 * If in startup, windows that are override redirect,
582 * unmapped and never were managed and are Withdrawn are not
583 * managed.
585 * Returns:
586 * the new window descriptor
588 * Side effects:
589 * The window is reparented and appropriate notification
590 * is done to the client. Input mask for the window is setup.
591 * The window descriptor is also associated with various window
592 * contexts and inserted in the head of the window list.
593 * Event handler contexts are associated for some objects
594 * (buttons, titlebar and resizebar)
596 *----------------------------------------------------------------
598 WWindow *wManageWindow(WScreen *scr, Window window)
600 WWindow *wwin;
601 int x, y;
602 unsigned width, height;
603 XWindowAttributes wattribs;
604 XSetWindowAttributes attribs;
605 WWindowState *win_state;
606 WWindow *transientOwner = NULL;
607 int window_level;
608 int wm_state;
609 int foo;
610 int workspace = -1;
611 char *title;
612 Bool withdraw = False;
613 Bool raise = False;
615 /* mutex. */
616 XGrabServer(dpy);
617 XSync(dpy, False);
619 /* make sure the window is still there */
620 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
621 XUngrabServer(dpy);
622 return NULL;
625 /* if it's an override-redirect, ignore it */
626 if (wattribs.override_redirect) {
627 XUngrabServer(dpy);
628 return NULL;
631 wm_state = PropGetWindowState(window);
633 /* if it's startup and the window is unmapped, don't manage it */
634 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
635 XUngrabServer(dpy);
636 return NULL;
639 wwin = wWindowCreate();
641 title = wNETWMGetWindowName(window);
642 if (title)
643 wwin->flags.net_has_title = 1;
644 else if (!wFetchName(dpy, window, &title))
645 title = NULL;
647 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
649 #ifdef USE_XSHAPE
650 if (w_global.xext.shape.supported) {
651 int junk;
652 unsigned int ujunk;
653 int b_shaped;
655 XShapeSelectInput(dpy, window, ShapeNotifyMask);
656 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
657 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
658 wwin->flags.shaped = b_shaped;
660 #endif
662 /* Get hints and other information in properties */
663 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
665 /* setup descriptor */
666 wwin->client_win = window;
667 wwin->screen_ptr = scr;
668 wwin->old_border_width = wattribs.border_width;
669 wwin->event_mask = CLIENT_EVENTS;
670 attribs.event_mask = CLIENT_EVENTS;
671 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
672 attribs.save_under = False;
674 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
675 XSetWindowBorderWidth(dpy, window, 0);
677 /* get hints from GNUstep app */
678 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
679 wwin->flags.is_gnustep = 1;
681 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
682 wwin->wm_gnustep_attr = NULL;
684 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
685 wwin->flags.is_dockapp = 1;
686 withdraw = True;
689 wwin->client_leader = PropGetClientLeader(window);
690 if (wwin->client_leader != None)
691 wwin->main_window = wwin->client_leader;
693 wwin->wm_hints = XGetWMHints(dpy, window);
695 if (wwin->wm_hints) {
696 if (wwin->wm_hints->flags & StateHint) {
697 if (wwin->wm_hints->initial_state == IconicState) {
698 wwin->flags.miniaturized = 1;
699 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
700 wwin->flags.is_dockapp = 1;
701 withdraw = True;
705 if (wwin->wm_hints->flags & WindowGroupHint) {
706 wwin->group_id = wwin->wm_hints->window_group;
707 /* window_group has priority over CLIENT_LEADER */
708 wwin->main_window = wwin->group_id;
709 } else {
710 wwin->group_id = None;
713 if (wwin->wm_hints->flags & UrgencyHint) {
714 wwin->flags.urgent = 1;
715 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
717 } else {
718 wwin->group_id = None;
721 PropGetProtocols(window, &wwin->protocols);
723 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
724 wwin->transient_for = None;
725 } else {
726 if (wwin->transient_for == None || wwin->transient_for == window) {
727 wwin->transient_for = scr->root_win;
728 } else {
729 transientOwner = wWindowFor(wwin->transient_for);
730 if (transientOwner && transientOwner->main_window != None)
731 wwin->main_window = transientOwner->main_window;
735 /* guess the focus mode */
736 wwin->focus_mode = getFocusMode(wwin);
738 /* get geometry stuff */
739 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
741 /* get colormap windows */
742 GetColormapWindows(wwin);
745 * Setup the decoration/window attributes and
746 * geometry
748 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
750 /* Make broken apps behave as a nice app. */
751 if (WFLAGP(wwin, emulate_appicon))
752 wwin->main_window = wwin->client_win;
754 fixLeaderProperties(wwin);
756 wwin->orig_main_window = wwin->main_window;
758 if (wwin->flags.is_gnustep)
759 wwin->client_flags.shared_appicon = 0;
761 if (wwin->main_window) {
762 XTextProperty text_prop;
764 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, w_global.atom.wmaker.menu))
765 wwin->client_flags.shared_appicon = 0;
768 if (wwin->flags.is_dockapp)
769 wwin->client_flags.shared_appicon = 0;
771 if (wwin->main_window) {
772 WApplication *app = wApplicationOf(wwin->main_window);
773 if (app && app->app_icon)
774 wwin->client_flags.shared_appicon = 0;
777 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
778 char *buffer, *instance, *class;
779 WFakeGroupLeader *fPtr;
780 int index;
782 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
784 /* // only enter here if PropGetWMClass() succeeds */
785 PropGetWMClass(wwin->main_window, &class, &instance);
786 buffer = StrConcatDot(instance, class);
788 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
789 if (index != WANotFound) {
790 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
791 if (fPtr->retainCount == 0)
792 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
793 instance, class);
795 fPtr->retainCount++;
796 if (fPtr->origLeader == None) {
797 if (ADEQUATE(wwin->main_window)) {
798 fPtr->retainCount++;
799 fPtr->origLeader = wwin->main_window;
802 wwin->fake_group = fPtr;
803 wwin->main_window = fPtr->leader;
804 wfree(buffer);
805 } else {
806 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
808 fPtr->identifier = buffer;
809 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
810 fPtr->origLeader = None;
811 fPtr->retainCount = 1;
813 WMAddToArray(scr->fakeGroupLeaders, fPtr);
815 if (ADEQUATE(wwin->main_window)) {
816 fPtr->retainCount++;
817 fPtr->origLeader = wwin->main_window;
819 wwin->fake_group = fPtr;
820 wwin->main_window = fPtr->leader;
823 if (instance)
824 free(instance);
826 if (class)
827 free(class);
828 #undef ADEQUATE
832 * Setup the initial state of the window
834 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
835 wwin->flags.miniaturized = 1;
837 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
838 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
840 wNETWMCheckInitialClientState(wwin);
842 /* apply previous state if it exists and we're in startup */
843 if (scr->flags.startup && wm_state >= 0) {
844 if (wm_state == IconicState)
845 wwin->flags.miniaturized = 1;
846 else if (wm_state == WithdrawnState)
847 withdraw = True;
850 /* if there is a saved state (from file), restore it */
851 win_state = NULL;
852 if (wwin->main_window != None)
853 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
854 else
855 win_state = (WWindowState *) wWindowGetSavedState(window);
857 if (win_state && !withdraw) {
858 if (win_state->state->hidden > 0)
859 wwin->flags.hidden = win_state->state->hidden;
861 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
862 wwin->flags.shaded = win_state->state->shaded;
864 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
865 wwin->flags.miniaturized = win_state->state->miniaturized;
867 if (!IS_OMNIPRESENT(wwin)) {
868 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
869 wwin->wm_class);
870 if (w < 0 || w >= scr->workspace_count) {
871 workspace = win_state->state->workspace;
872 if (workspace >= scr->workspace_count)
873 workspace = scr->current_workspace;
874 } else {
875 workspace = w;
877 } else {
878 workspace = scr->current_workspace;
882 /* if we're restarting, restore saved state (from hints).
883 * This will overwrite previous */
885 WSavedState *wstate;
887 if (getSavedState(window, &wstate)) {
888 wwin->flags.shaded = wstate->shaded;
889 wwin->flags.hidden = wstate->hidden;
890 wwin->flags.miniaturized = wstate->miniaturized;
891 wwin->flags.maximized = wstate->maximized;
892 if (wwin->flags.maximized) {
893 wwin->old_geometry.x = wstate->x;
894 wwin->old_geometry.y = wstate->y;
895 wwin->old_geometry.width = wstate->w;
896 wwin->old_geometry.height = wstate->h;
899 workspace = wstate->workspace;
900 } else {
901 wstate = NULL;
904 /* restore window shortcut */
905 if (wstate != NULL || win_state != NULL) {
906 unsigned mask = 0;
908 if (win_state != NULL)
909 mask = win_state->state->window_shortcuts;
911 if (wstate != NULL && mask == 0)
912 mask = wstate->window_shortcuts;
914 if (mask > 0) {
915 int i;
917 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
918 if (mask & (1 << i)) {
919 if (!scr->shortcutWindows[i])
920 scr->shortcutWindows[i] = WMCreateArray(4);
922 WMAddToArray(scr->shortcutWindows[i], wwin);
928 if (wstate != NULL)
929 wfree(wstate);
932 /* don't let transients start miniaturized if their owners are not */
933 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
934 wwin->flags.miniaturized = 0;
935 if (wwin->wm_hints)
936 wwin->wm_hints->initial_state = NormalState;
939 /* set workspace on which the window starts */
940 if (workspace >= 0) {
941 if (workspace > scr->workspace_count - 1)
942 workspace = workspace % scr->workspace_count;
943 } else {
944 int w;
946 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
948 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
949 workspace = w;
950 } else {
951 if (wPreferences.open_transients_with_parent && transientOwner)
952 workspace = transientOwner->frame->workspace;
953 else
954 workspace = scr->current_workspace;
958 /* setup window geometry */
959 if (win_state && win_state->state->w > 0) {
960 width = win_state->state->w;
961 height = win_state->state->h;
964 wWindowConstrainSize(wwin, &width, &height);
966 /* do not ask for window placement if the window is
967 * transient, during startup, or if the window wants
968 * to start iconic. If geometry was saved, restore it. */
970 Bool dontBring = False;
972 if (win_state && win_state->state->w > 0) {
973 x = win_state->state->x;
974 y = win_state->state->y;
975 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
976 && !scr->flags.startup
977 && !wwin->flags.miniaturized
978 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
980 if (transientOwner && transientOwner->flags.mapped) {
981 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
982 WMRect rect;
983 int head;
985 x = transientOwner->frame_x +
986 abs((transientOwner->frame->core->width - width) / 2) + offs;
987 y = transientOwner->frame_y +
988 abs((transientOwner->frame->core->height - height) / 3) + offs;
990 /* limit transient windows to be inside their parent's head */
991 rect.pos.x = transientOwner->frame_x;
992 rect.pos.y = transientOwner->frame_y;
993 rect.size.width = transientOwner->frame->core->width;
994 rect.size.height = transientOwner->frame->core->height;
996 head = wGetHeadForRect(scr, rect);
997 rect = wGetRectForHead(scr, head);
999 if (x < rect.pos.x)
1000 x = rect.pos.x;
1001 else if (x + width > rect.pos.x + rect.size.width)
1002 x = rect.pos.x + rect.size.width - width;
1004 if (y < rect.pos.y)
1005 y = rect.pos.y;
1006 else if (y + height > rect.pos.y + rect.size.height)
1007 y = rect.pos.y + rect.size.height - height;
1009 } else {
1010 PlaceWindow(wwin, &x, &y, width, height);
1013 if (wPreferences.window_placement == WPM_MANUAL)
1014 dontBring = True;
1016 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1017 int head, flags;
1018 WMRect rect;
1019 int reposition = 0;
1021 /* Make spash screens come out in the center of a head
1022 * trouble is that most splashies never get here
1023 * they are managed trough atoms but god knows where.
1024 * Dan, do you know ? -peter
1026 * Most of them are not managed, they have set
1027 * OverrideRedirect, which means we can't do anything about
1028 * them. -alfredo */
1030 /* xinerama checks for: across head and dead space */
1031 rect.pos.x = x;
1032 rect.pos.y = y;
1033 rect.size.width = width;
1034 rect.size.height = height;
1036 head = wGetRectPlacementInfo(scr, rect, &flags);
1038 if (flags & XFLAG_DEAD)
1039 reposition = 1;
1041 if (flags & XFLAG_MULTIPLE)
1042 reposition = 2;
1045 switch (reposition) {
1046 case 1:
1047 head = wGetHeadForPointerLocation(scr);
1048 rect = wGetRectForHead(scr, head);
1050 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1051 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1052 break;
1054 case 2:
1055 rect = wGetRectForHead(scr, head);
1057 if (x < rect.pos.x)
1058 x = rect.pos.x;
1059 else if (x + width > rect.pos.x + rect.size.width)
1060 x = rect.pos.x + rect.size.width - width;
1062 if (y < rect.pos.y)
1063 y = rect.pos.y;
1064 else if (y + height > rect.pos.y + rect.size.height)
1065 y = rect.pos.y + rect.size.height - height;
1067 break;
1069 default:
1070 break;
1074 if (WFLAGP(wwin, dont_move_off) && dontBring)
1075 wScreenBringInside(scr, &x, &y, width, height);
1078 wNETWMPositionSplash(wwin, &x, &y, width, height);
1080 if (wwin->flags.urgent) {
1081 if (!IS_OMNIPRESENT(wwin))
1082 wwin->flags.omnipresent ^= 1;
1085 /* Create frame, borders and do reparenting */
1086 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1087 #ifdef XKB_BUTTON_HINT
1088 if (wPreferences.modelock)
1089 foo |= WFF_LANGUAGE_BUTTON;
1090 #endif
1091 if (HAS_TITLEBAR(wwin))
1092 foo |= WFF_TITLEBAR;
1094 if (HAS_RESIZEBAR(wwin))
1095 foo |= WFF_RESIZEBAR;
1097 if (HAS_BORDER(wwin))
1098 foo |= WFF_BORDER;
1100 wwin->frame = wFrameWindowCreate(scr, window_level,
1101 x, y, width, height,
1102 &wPreferences.window_title_clearance,
1103 &wPreferences.window_title_min_height,
1104 &wPreferences.window_title_max_height,
1105 foo,
1106 scr->window_title_texture,
1107 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1108 wattribs.depth, wattribs.visual, wattribs.colormap);
1110 wwin->frame->flags.is_client_window_frame = 1;
1111 wwin->frame->flags.justification = wPreferences.title_justification;
1113 wNETWMCheckInitialFrameState(wwin);
1115 /* setup button images */
1116 wWindowUpdateButtonImages(wwin);
1118 /* hide unused buttons */
1119 foo = 0;
1120 if (WFLAGP(wwin, no_close_button))
1121 foo |= WFF_RIGHT_BUTTON;
1123 if (WFLAGP(wwin, no_miniaturize_button))
1124 foo |= WFF_LEFT_BUTTON;
1126 #ifdef XKB_BUTTON_HINT
1127 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1128 foo |= WFF_LANGUAGE_BUTTON;
1129 #endif
1130 if (foo != 0)
1131 wFrameWindowHideButton(wwin->frame, foo);
1133 wwin->frame->child = wwin;
1134 wwin->frame->workspace = workspace;
1135 wwin->frame->on_click_left = windowIconifyClick;
1137 #ifdef XKB_BUTTON_HINT
1138 if (wPreferences.modelock)
1139 wwin->frame->on_click_language = windowLanguageClick;
1140 #endif
1142 wwin->frame->on_click_right = windowCloseClick;
1143 wwin->frame->on_dblclick_right = windowCloseDblClick;
1144 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1145 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1146 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1148 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1149 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1150 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1153 int gx, gy;
1155 wClientGetGravityOffsets(wwin, &gx, &gy);
1157 /* if gravity is to the south, account for the border sizes */
1158 if (gy > 0)
1159 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1163 * wWindowConfigure() will init the client window's size
1164 * (wwin->client.{width,height}) and all other geometry
1165 * related variables (frame_x,frame_y) */
1166 wWindowConfigure(wwin, x, y, width, height);
1168 /* to make sure the window receives it's new position after reparenting */
1169 wWindowSynthConfigureNotify(wwin);
1171 /* Setup descriptors and save window to internal lists */
1172 if (wwin->main_window != None) {
1173 WApplication *app;
1174 WWindow *leader;
1176 /* Leader windows do not necessary set themselves as leaders.
1177 * If this is the case, point the leader of this window to
1178 * itself */
1179 leader = wWindowFor(wwin->main_window);
1180 if (leader && leader->main_window == None)
1181 leader->main_window = leader->client_win;
1183 app = wApplicationCreate(wwin);
1184 if (app) {
1185 app->last_workspace = workspace;
1187 /* Do application specific stuff, like setting application
1188 * wide attributes. */
1190 if (wwin->flags.hidden) {
1191 /* if the window was set to hidden because it was hidden
1192 * in a previous incarnation and that state was restored */
1193 app->flags.hidden = 1;
1194 } else if (app->flags.hidden) {
1195 if (WFLAGP(app->main_window_desc, start_hidden)) {
1196 wwin->flags.hidden = 1;
1197 } else {
1198 wUnhideApplication(app, False, False);
1199 raise = True;
1202 wAppBounce(app);
1206 /* setup the frame descriptor */
1207 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1208 wwin->frame->core->descriptor.parent = wwin;
1209 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1211 /* don't let windows go away if we die */
1212 XAddToSaveSet(dpy, window);
1214 XLowerWindow(dpy, window);
1216 /* if window is in this workspace and should be mapped, then map it */
1217 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1218 && !wwin->flags.hidden && !withdraw) {
1220 /* The following "if" is to avoid crashing of clients that expect
1221 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1222 * after the return from this function. */
1223 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1224 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1225 else
1226 wClientSetState(wwin, NormalState, None);
1228 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1229 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1231 * The brain damaged idiotic non-click to focus modes will
1232 * have trouble with this because:
1234 * 1. window is created and mapped by the client
1235 * 2. window is mapped by wmaker in small size
1236 * 3. window is animated to grow to normal size
1237 * 4. this function returns to normal event loop
1238 * 5. eventually, the EnterNotify event that would trigger
1239 * the window focusing (if the mouse is over that window)
1240 * will be processed by wmaker.
1241 * But since this event will be rather delayed
1242 * (step 3 has a large delay) the time when the event occurred
1243 * and when it is processed, the client that owns that window
1244 * will reject the XSetInputFocus() for it.
1246 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1247 DoWindowBirth(wwin);
1249 wWindowMap(wwin);
1252 /* setup stacking descriptor */
1253 if (transientOwner)
1254 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1255 else
1256 wwin->frame->core->stacking->child_of = NULL;
1258 if (!scr->focused_window) {
1259 /* first window on the list */
1260 wwin->next = NULL;
1261 wwin->prev = NULL;
1262 scr->focused_window = wwin;
1263 } else {
1264 WWindow *tmp;
1266 /* add window at beginning of focus window list */
1267 tmp = scr->focused_window;
1268 while (tmp->prev)
1269 tmp = tmp->prev;
1271 tmp->prev = wwin;
1272 wwin->next = tmp;
1273 wwin->prev = NULL;
1276 /* raise is set to true if we un-hid the app when this window was born.
1277 * we raise, else old windows of this app will be above this new one. */
1278 if (raise)
1279 wRaiseFrame(wwin->frame->core);
1281 /* Update name must come after WApplication stuff is done */
1282 wWindowUpdateName(wwin, title);
1283 if (title)
1284 XFree(title);
1286 XUngrabServer(dpy);
1288 /* Final preparations before window is ready to go */
1289 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1291 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1292 if (((transientOwner && transientOwner->flags.focused)
1293 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1295 /* only auto_focus if on same screen as mouse
1296 * (and same head for xinerama mode)
1297 * TODO: make it an option */
1299 /*TODO add checking the head of the window, is it available? */
1300 short same_screen = 0, same_head = 1;
1302 int foo;
1303 unsigned int bar;
1304 Window dummy;
1306 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1307 &foo, &foo, &foo, &foo, &bar) != False)
1308 same_screen = 1;
1310 if (same_screen == 1 && same_head == 1)
1311 wSetFocusTo(scr, wwin);
1314 wWindowResetMouseGrabs(wwin);
1316 if (!WFLAGP(wwin, no_bind_keys))
1317 wWindowSetKeyGrabs(wwin);
1319 WMPostNotificationName(WMNManaged, wwin, NULL);
1320 wColormapInstallForWindow(scr, scr->cmap_window);
1322 /* Setup Notification Observers */
1323 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1325 /* Cleanup temporary stuff */
1326 if (win_state)
1327 wWindowDeleteSavedState(win_state);
1329 /* If the window must be withdrawed, then do it now.
1330 * Must do some optimization, 'though */
1331 if (withdraw) {
1332 wwin->flags.mapped = 0;
1333 wClientSetState(wwin, WithdrawnState, None);
1334 wUnmanageWindow(wwin, True, False);
1335 wwin = NULL;
1338 return wwin;
1341 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1342 const char *title, int x, int y, int width, int height)
1344 WWindow *wwin;
1345 int foo;
1347 wwin = wWindowCreate();
1349 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1351 wwin->flags.internal_window = 1;
1353 wwin->client_flags.omnipresent = 1;
1354 wwin->client_flags.no_shadeable = 1;
1355 wwin->client_flags.no_resizable = 1;
1356 wwin->client_flags.no_miniaturizable = 1;
1358 wwin->focus_mode = WFM_PASSIVE;
1359 wwin->client_win = window;
1360 wwin->screen_ptr = scr;
1361 wwin->transient_for = owner;
1362 wwin->client.x = x;
1363 wwin->client.y = y;
1364 wwin->client.width = width;
1365 wwin->client.height = height;
1366 wwin->frame_x = wwin->client.x;
1367 wwin->frame_y = wwin->client.y;
1369 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1370 foo |= WFF_TITLEBAR;
1371 #ifdef XKB_BUTTON_HINT
1372 foo |= WFF_LANGUAGE_BUTTON;
1373 #endif
1375 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1376 wwin->frame_x, wwin->frame_y,
1377 width, height,
1378 &wPreferences.window_title_clearance,
1379 &wPreferences.window_title_min_height,
1380 &wPreferences.window_title_max_height,
1381 foo,
1382 scr->window_title_texture,
1383 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1384 scr->w_depth, scr->w_visual, scr->w_colormap);
1386 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
1388 wwin->frame->flags.is_client_window_frame = 1;
1389 wwin->frame->flags.justification = wPreferences.title_justification;
1391 wFrameWindowChangeTitle(wwin->frame, title);
1393 /* setup button images */
1394 wWindowUpdateButtonImages(wwin);
1396 /* hide buttons */
1397 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1399 wwin->frame->child = wwin;
1400 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1402 #ifdef XKB_BUTTON_HINT
1403 if (wPreferences.modelock)
1404 wwin->frame->on_click_language = windowLanguageClick;
1405 #endif
1407 wwin->frame->on_click_right = windowCloseClick;
1408 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1409 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1410 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1411 wwin->client.y += wwin->frame->top_width;
1413 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1414 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1416 /* setup the frame descriptor */
1417 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1418 wwin->frame->core->descriptor.parent = wwin;
1419 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1421 XLowerWindow(dpy, window);
1422 XMapSubwindows(dpy, wwin->frame->core->window);
1424 /* setup stacking descriptor */
1425 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1426 WWindow *tmp;
1427 tmp = wWindowFor(wwin->transient_for);
1428 if (tmp)
1429 wwin->frame->core->stacking->child_of = tmp->frame->core;
1430 } else {
1431 wwin->frame->core->stacking->child_of = NULL;
1434 if (!scr->focused_window) {
1435 /* first window on the list */
1436 wwin->next = NULL;
1437 wwin->prev = NULL;
1438 scr->focused_window = wwin;
1439 } else {
1440 WWindow *tmp;
1442 /* add window at beginning of focus window list */
1443 tmp = scr->focused_window;
1444 while (tmp->prev)
1445 tmp = tmp->prev;
1446 tmp->prev = wwin;
1447 wwin->next = tmp;
1448 wwin->prev = NULL;
1451 if (wwin->flags.is_gnustep == 0)
1452 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1454 /* if (wPreferences.auto_focus) */
1455 wSetFocusTo(scr, wwin);
1456 wWindowResetMouseGrabs(wwin);
1457 wWindowSetKeyGrabs(wwin);
1459 return wwin;
1463 *----------------------------------------------------------------------
1464 * wUnmanageWindow--
1465 * Removes the frame window from a window and destroys all data
1466 * related to it. The window will be reparented back to the root window
1467 * if restore is True.
1469 * Side effects:
1470 * Everything related to the window is destroyed and the window
1471 * is removed from the window lists. Focus is set to the previous on the
1472 * window list.
1473 *----------------------------------------------------------------------
1475 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1477 WCoreWindow *frame = wwin->frame->core;
1478 WWindow *owner = NULL;
1479 WWindow *newFocusedWindow = NULL;
1480 int wasFocused;
1481 WScreen *scr = wwin->screen_ptr;
1483 /* First close attribute editor window if open */
1484 if (wwin->flags.inspector_open)
1485 wCloseInspectorForWindow(wwin);
1487 /* Close window menu if it's open for this window */
1488 if (wwin->flags.menu_open_for_me)
1489 CloseWindowMenu(scr);
1491 /* Don't restore focus to this window after a window exits
1492 * fullscreen mode */
1493 if (scr->bfs_focused_window == wwin)
1494 scr->bfs_focused_window = NULL;
1496 if (!destroyed) {
1497 if (!wwin->flags.internal_window)
1498 XRemoveFromSaveSet(dpy, wwin->client_win);
1500 /* If this is a leader window, we still need to listen for
1501 * DestroyNotify and PropertyNotify. */
1502 if (wApplicationOf(wwin->client_win))
1503 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1504 else
1505 XSelectInput(dpy, wwin->client_win, NoEventMask);
1507 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1508 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1511 XUnmapWindow(dpy, frame->window);
1513 XUnmapWindow(dpy, wwin->client_win);
1515 /* deselect window */
1516 wSelectWindow(wwin, False);
1518 /* remove all pending events on window */
1519 /* I think this only matters for autoraise */
1520 if (wPreferences.raise_delay)
1521 WMDeleteTimerWithClientData(wwin->frame->core);
1523 XFlush(dpy);
1525 /* reparent the window back to the root */
1526 if (restore)
1527 wClientRestore(wwin);
1529 if (wwin->transient_for != scr->root_win) {
1530 owner = wWindowFor(wwin->transient_for);
1531 if (owner) {
1532 if (!owner->flags.semi_focused)
1533 owner = NULL;
1534 else
1535 owner->flags.semi_focused = 0;
1539 wasFocused = wwin->flags.focused;
1541 /* remove from window focus list */
1542 if (!wwin->prev && !wwin->next) {
1543 /* was the only window */
1544 scr->focused_window = NULL;
1545 newFocusedWindow = NULL;
1546 } else {
1547 WWindow *tmp;
1549 if (wwin->prev)
1550 wwin->prev->next = wwin->next;
1552 if (wwin->next)
1553 wwin->next->prev = wwin->prev;
1554 else {
1555 scr->focused_window = wwin->prev;
1556 scr->focused_window->next = NULL;
1559 if (wPreferences.focus_mode == WKF_CLICK) {
1561 /* if in click to focus mode and the window
1562 * was a transient, focus the owner window
1564 tmp = NULL;
1565 if (wPreferences.focus_mode == WKF_CLICK) {
1566 tmp = wWindowFor(wwin->transient_for);
1567 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1568 tmp = NULL;
1571 /* otherwise, focus the next one in the focus list */
1572 if (!tmp) {
1573 tmp = scr->focused_window;
1574 while (tmp) { /* look for one in the window list first */
1575 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1576 && (tmp->flags.mapped || tmp->flags.shaded))
1577 break;
1578 tmp = tmp->prev;
1580 if (!tmp) { /* if unsuccessful, choose any focusable window */
1581 tmp = scr->focused_window;
1582 while (tmp) {
1583 if (!WFLAGP(tmp, no_focusable)
1584 && (tmp->flags.mapped || tmp->flags.shaded))
1585 break;
1586 tmp = tmp->prev;
1591 newFocusedWindow = tmp;
1593 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1594 unsigned int mask;
1595 int foo;
1596 Window bar, win;
1598 /* This is to let the root window get the keyboard input
1599 * if Sloppy focus mode and no other window get focus.
1600 * This way keybindings will not freeze.
1602 tmp = NULL;
1603 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1604 tmp = wWindowFor(win);
1605 if (tmp == wwin)
1606 tmp = NULL;
1607 newFocusedWindow = tmp;
1608 } else {
1609 newFocusedWindow = NULL;
1613 if (!wwin->flags.internal_window)
1614 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1615 if (wasFocused) {
1616 if (newFocusedWindow != owner && owner) {
1617 if (wwin->flags.is_gnustep == 0)
1618 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1620 wSetFocusTo(scr, newFocusedWindow);
1623 /* Close menu and unhighlight */
1624 WApplication *oapp = wApplicationOf(wwin->main_window);
1625 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1626 if (oapp && oapp != napp) {
1627 wAppMenuUnmap(oapp->menu);
1628 if (wPreferences.highlight_active_app)
1629 wApplicationDeactivate(oapp);
1632 wNETCleanupFrameExtents(wwin);
1634 wWindowDestroy(wwin);
1635 XFlush(dpy);
1638 void wWindowMap(WWindow *wwin)
1640 XMapWindow(dpy, wwin->frame->core->window);
1641 if (!wwin->flags.shaded) {
1642 /* window will be remapped when getting MapNotify */
1643 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1644 XMapWindow(dpy, wwin->client_win);
1645 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1647 wwin->flags.mapped = 1;
1651 void wWindowUnmap(WWindow *wwin)
1653 wwin->flags.mapped = 0;
1655 /* prevent window withdrawal when getting UnmapNotify */
1656 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1657 XUnmapWindow(dpy, wwin->client_win);
1658 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1660 XUnmapWindow(dpy, wwin->frame->core->window);
1663 void wWindowSingleFocus(WWindow *wwin)
1665 WScreen *scr;
1666 int x, y, move = 0;
1668 if (!wwin)
1669 return;
1671 scr = wwin->screen_ptr;
1672 wMakeWindowVisible(wwin);
1674 x = wwin->frame_x;
1675 y = wwin->frame_y;
1677 /* bring window back to visible area */
1678 move = wScreenBringInside(scr, &x, &y, wwin->frame->core->width, wwin->frame->core->height);
1680 if (move) {
1681 wWindowConfigure(wwin, x, y, wwin->client.width, wwin->client.height);
1685 void wWindowFocusPrev(WWindow *wwin, Bool inSameWorkspace)
1687 WWindow *tmp;
1689 if (!wwin || !wwin->prev)
1690 return;
1692 tmp = wwin;
1693 while (tmp->prev)
1694 tmp = tmp->prev;
1696 if (inSameWorkspace)
1697 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1698 tmp = tmp->next;
1700 wWindowSingleFocus(tmp);
1703 void wWindowFocusNext(WWindow *wwin, Bool inSameWorkspace)
1705 WWindow *tmp;
1707 if (!wwin || !wwin->prev)
1708 return;
1710 tmp = wwin->prev;
1711 if (inSameWorkspace)
1712 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1713 tmp = tmp->prev;
1715 wWindowSingleFocus(tmp);
1718 void wWindowFocus(WWindow *wwin, WWindow *owin)
1720 WWindow *nowner;
1721 WWindow *oowner;
1723 #ifdef KEEP_XKB_LOCK_STATUS
1724 if (wPreferences.modelock)
1725 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1726 #endif /* KEEP_XKB_LOCK_STATUS */
1728 wwin->flags.semi_focused = 0;
1730 if (wwin->flags.is_gnustep == 0)
1731 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1733 wwin->flags.focused = 1;
1735 wWindowResetMouseGrabs(wwin);
1737 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1739 if (owin == wwin || !owin)
1740 return;
1742 nowner = wWindowFor(wwin->transient_for);
1744 /* new window is a transient for the old window */
1745 if (nowner == owin) {
1746 owin->flags.semi_focused = 1;
1747 wWindowUnfocus(nowner);
1748 return;
1751 oowner = wWindowFor(owin->transient_for);
1753 /* new window is owner of old window */
1754 if (wwin == oowner) {
1755 wWindowUnfocus(owin);
1756 return;
1759 if (!nowner) {
1760 wWindowUnfocus(owin);
1761 return;
1764 /* new window has same owner of old window */
1765 if (oowner == nowner) {
1766 /* prevent unfocusing of owner */
1767 oowner->flags.semi_focused = 0;
1768 wWindowUnfocus(owin);
1769 oowner->flags.semi_focused = 1;
1771 return;
1774 /* nowner != NULL && oowner != nowner */
1775 nowner->flags.semi_focused = 1;
1776 wWindowUnfocus(nowner);
1777 wWindowUnfocus(owin);
1780 void wWindowUnfocus(WWindow *wwin)
1782 CloseWindowMenu(wwin->screen_ptr);
1784 if (wwin->flags.is_gnustep == 0)
1785 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1787 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1788 WWindow *owner;
1789 owner = wWindowFor(wwin->transient_for);
1790 if (owner && owner->flags.semi_focused) {
1791 owner->flags.semi_focused = 0;
1792 if (owner->flags.mapped || owner->flags.shaded) {
1793 wWindowUnfocus(owner);
1794 wFrameWindowPaint(owner->frame);
1798 wwin->flags.focused = 0;
1799 wWindowResetMouseGrabs(wwin);
1800 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1803 void wWindowUpdateName(WWindow *wwin, const char *newTitle)
1805 const char *title;
1807 if (!wwin->frame)
1808 return;
1810 if (!newTitle)
1811 title = DEF_WINDOW_TITLE; /* the hint was removed */
1812 else
1813 title = newTitle;
1815 if (wFrameWindowChangeTitle(wwin->frame, title))
1816 WMPostNotificationName(WMNChangedName, wwin, NULL);
1820 *----------------------------------------------------------------------
1822 * wWindowConstrainSize--
1823 * Constrains size for the client window, taking the maximal size,
1824 * window resize increments and other size hints into account.
1826 * Returns:
1827 * The closest size to what was given that the client window can
1828 * have.
1830 *----------------------------------------------------------------------
1832 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1834 int width = (int)*nwidth;
1835 int height = (int)*nheight;
1836 int winc = 1;
1837 int hinc = 1;
1838 int minW = 1, minH = 1;
1839 int maxW = wwin->screen_ptr->scr_width * 2;
1840 int maxH = wwin->screen_ptr->scr_height * 2;
1841 int minAX = -1, minAY = -1;
1842 int maxAX = -1, maxAY = -1;
1843 int baseW = 0;
1844 int baseH = 0;
1846 if (wwin->normal_hints) {
1847 winc = wwin->normal_hints->width_inc;
1848 hinc = wwin->normal_hints->height_inc;
1849 minW = wwin->normal_hints->min_width;
1850 minH = wwin->normal_hints->min_height;
1851 maxW = wwin->normal_hints->max_width;
1852 maxH = wwin->normal_hints->max_height;
1853 if (wwin->normal_hints->flags & PAspect) {
1854 minAX = wwin->normal_hints->min_aspect.x;
1855 minAY = wwin->normal_hints->min_aspect.y;
1856 maxAX = wwin->normal_hints->max_aspect.x;
1857 maxAY = wwin->normal_hints->max_aspect.y;
1860 baseW = wwin->normal_hints->base_width;
1861 baseH = wwin->normal_hints->base_height;
1864 if (width < minW)
1865 width = minW;
1866 if (height < minH)
1867 height = minH;
1869 if (width > maxW)
1870 width = maxW;
1871 if (height > maxH)
1872 height = maxH;
1874 /* aspect ratio code borrowed from olwm */
1875 if (minAX > 0) {
1876 /* adjust max aspect ratio */
1877 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1878 if (maxAX > maxAY) {
1879 height = (width * maxAY) / maxAX;
1880 if (height > maxH) {
1881 height = maxH;
1882 width = (height * maxAX) / maxAY;
1884 } else {
1885 width = (height * maxAX) / maxAY;
1886 if (width > maxW) {
1887 width = maxW;
1888 height = (width * maxAY) / maxAX;
1893 /* adjust min aspect ratio */
1894 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1895 if (minAX > minAY) {
1896 height = (width * minAY) / minAX;
1897 if (height < minH) {
1898 height = minH;
1899 width = (height * minAX) / minAY;
1901 } else {
1902 width = (height * minAX) / minAY;
1903 if (width < minW) {
1904 width = minW;
1905 height = (width * minAY) / minAX;
1911 if (baseW != 0)
1912 width = (((width - baseW) / winc) * winc) + baseW;
1913 else
1914 width = (((width - minW) / winc) * winc) + minW;
1916 if (baseH != 0)
1917 height = (((height - baseH) / hinc) * hinc) + baseH;
1918 else
1919 height = (((height - minH) / hinc) * hinc) + minH;
1921 /* broken stupid apps may cause preposterous values for these.. */
1922 if (width > 0)
1923 *nwidth = width;
1924 if (height > 0)
1925 *nheight = height;
1928 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1929 unsigned int *width, unsigned int *height)
1931 int baseW = 0, baseH = 0;
1932 int winc = 1, hinc = 1;
1934 if (wwin->normal_hints) {
1935 baseW = wwin->normal_hints->base_width;
1936 baseH = wwin->normal_hints->base_height;
1938 winc = wwin->normal_hints->width_inc;
1939 hinc = wwin->normal_hints->height_inc;
1942 if (*width > maxW)
1943 *width = maxW - (maxW - baseW) % winc;
1945 if (*height > maxH)
1946 *height = maxH - (maxH - baseH) % hinc;
1949 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1951 WScreen *scr = wwin->screen_ptr;
1952 WApplication *wapp;
1953 int unmap = 0;
1955 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1956 return;
1958 if (workspace != scr->current_workspace) {
1959 /* Sent to other workspace. Unmap window */
1960 if ((wwin->flags.mapped
1961 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1962 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1964 wapp = wApplicationOf(wwin->main_window);
1965 if (wapp)
1966 wapp->last_workspace = workspace;
1968 if (wwin->flags.miniaturized) {
1969 if (wwin->icon) {
1970 XUnmapWindow(dpy, wwin->icon->core->window);
1971 wwin->icon->mapped = 0;
1973 } else {
1974 unmap = 1;
1975 wSetFocusTo(scr, NULL);
1978 } else {
1979 /* brought to current workspace. Map window */
1980 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1981 if (wwin->icon) {
1982 XMapWindow(dpy, wwin->icon->core->window);
1983 wwin->icon->mapped = 1;
1985 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1986 wWindowMap(wwin);
1989 if (!IS_OMNIPRESENT(wwin)) {
1990 int oldWorkspace = wwin->frame->workspace;
1991 wwin->frame->workspace = workspace;
1992 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1995 if (unmap)
1996 wWindowUnmap(wwin);
1999 void wWindowChangeWorkspaceRelative(WWindow *wwin, int amount)
2001 WScreen *scr = wwin->screen_ptr;
2002 int w = scr->current_workspace + amount;
2004 if (amount < 0) {
2005 if (w >= 0) {
2006 wWindowChangeWorkspace(wwin, w);
2007 } else if (wPreferences.ws_cycle) {
2008 wWindowChangeWorkspace(wwin, scr->workspace_count + w);
2010 } else if (amount > 0) {
2011 if (w < scr->workspace_count) {
2012 wWindowChangeWorkspace(wwin, w);
2013 } else if (wPreferences.ws_advance) {
2014 int workspace = WMIN(w, MAX_WORKSPACES - 1);
2015 wWorkspaceMake(scr, workspace);
2016 wWindowChangeWorkspace(wwin, workspace);
2017 } else if (wPreferences.ws_cycle) {
2018 wWindowChangeWorkspace(wwin, w % scr->workspace_count);
2023 void wWindowSynthConfigureNotify(WWindow *wwin)
2025 XEvent sevent;
2027 sevent.type = ConfigureNotify;
2028 sevent.xconfigure.display = dpy;
2029 sevent.xconfigure.event = wwin->client_win;
2030 sevent.xconfigure.window = wwin->client_win;
2032 sevent.xconfigure.x = wwin->client.x;
2033 sevent.xconfigure.y = wwin->client.y;
2034 sevent.xconfigure.width = wwin->client.width;
2035 sevent.xconfigure.height = wwin->client.height;
2037 sevent.xconfigure.border_width = wwin->old_border_width;
2038 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2039 sevent.xconfigure.above = wwin->frame->titlebar->window;
2040 else
2041 sevent.xconfigure.above = None;
2043 sevent.xconfigure.override_redirect = False;
2044 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2045 XFlush(dpy);
2049 *----------------------------------------------------------------------
2050 * wWindowConfigure()
2052 * req_x, req_y: new requested positions for the frame
2053 * req_width, req_height: new requested sizes for the client
2055 * Configures the frame, decorations and client window to the specified
2056 * geometry, whose validity is not checked -- wWindowConstrainSize()
2057 * must be used for that.
2058 * The size parameters are for the client window, but the position is
2059 * for the frame.
2060 * The client window receives a ConfigureNotify event, according
2061 * to what ICCCM says.
2063 * Returns:
2064 * None
2066 * Side effects:
2067 * Window size and position are changed and client window receives
2068 * a ConfigureNotify event.
2069 *----------------------------------------------------------------------
2071 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2073 int synth_notify = False;
2074 int resize;
2076 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2078 * if the window is being moved but not resized then
2079 * send a synthetic ConfigureNotify
2081 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2082 synth_notify = True;
2084 if (WFLAGP(wwin, dont_move_off))
2085 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2087 if (resize) {
2088 if (req_width < MIN_WINDOW_SIZE)
2089 req_width = MIN_WINDOW_SIZE;
2090 if (req_height < MIN_WINDOW_SIZE)
2091 req_height = MIN_WINDOW_SIZE;
2093 /* If growing, resize inner part before frame,
2094 * if shrinking, resize frame before.
2095 * This will prevent the frame (that can have a different color)
2096 * to be exposed, causing flicker */
2097 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2098 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2100 if (wwin->flags.shaded) {
2101 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2102 wwin->old_geometry.height = req_height;
2103 } else {
2104 int h;
2106 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2108 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2111 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2112 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2114 wwin->client.x = req_x;
2115 wwin->client.y = req_y + wwin->frame->top_width;
2116 wwin->client.width = req_width;
2117 wwin->client.height = req_height;
2118 } else {
2119 wwin->client.x = req_x;
2120 wwin->client.y = req_y + wwin->frame->top_width;
2122 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2124 wwin->frame_x = req_x;
2125 wwin->frame_y = req_y;
2126 if (HAS_BORDER(wwin)) {
2127 wwin->client.x += wwin->screen_ptr->frame_border_width;
2128 wwin->client.y += wwin->screen_ptr->frame_border_width;
2130 #ifdef USE_XSHAPE
2131 if (w_global.xext.shape.supported && wwin->flags.shaped && resize)
2132 wWindowSetShape(wwin);
2133 #endif
2135 if (synth_notify)
2136 wWindowSynthConfigureNotify(wwin);
2138 wNETFrameExtents(wwin);
2140 XFlush(dpy);
2143 /* req_x, req_y: new position of the frame */
2144 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2146 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2147 int synth_notify = False;
2149 /* Send a synthetic ConfigureNotify event for every window movement. */
2150 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2151 synth_notify = True;
2152 #else
2153 /* A single synthetic ConfigureNotify event is sent at the end of
2154 * a completed (opaque) movement in moveres.c */
2155 #endif
2157 if (WFLAGP(wwin, dont_move_off))
2158 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2159 wwin->frame->core->width, wwin->frame->core->height);
2161 wwin->client.x = req_x;
2162 wwin->client.y = req_y + wwin->frame->top_width;
2163 if (HAS_BORDER(wwin)) {
2164 wwin->client.x += wwin->screen_ptr->frame_border_width;
2165 wwin->client.y += wwin->screen_ptr->frame_border_width;
2168 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2170 wwin->frame_x = req_x;
2171 wwin->frame_y = req_y;
2173 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2174 if (synth_notify)
2175 wWindowSynthConfigureNotify(wwin);
2176 #endif
2179 void wWindowUpdateButtonImages(WWindow *wwin)
2181 WScreen *scr = wwin->screen_ptr;
2182 Pixmap pixmap, mask;
2183 WFrameWindow *fwin = wwin->frame;
2185 if (!HAS_TITLEBAR(wwin))
2186 return;
2188 /* miniaturize button */
2189 if (!WFLAGP(wwin, no_miniaturize_button)) {
2190 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2191 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2193 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2194 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2195 else
2196 mask = None;
2198 if (fwin->lbutton_image
2199 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2200 wPixmapDestroy(fwin->lbutton_image);
2201 fwin->lbutton_image = NULL;
2204 if (!fwin->lbutton_image) {
2205 fwin->lbutton_image = wPixmapCreate(pixmap, mask);
2206 fwin->lbutton_image->client_owned = 1;
2207 fwin->lbutton_image->client_owned_mask = 1;
2209 } else {
2210 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2211 wPixmapDestroy(fwin->lbutton_image);
2213 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2216 #ifdef XKB_BUTTON_HINT
2217 if (!WFLAGP(wwin, no_language_button)) {
2218 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2219 wPixmapDestroy(fwin->languagebutton_image);
2221 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2223 #endif
2225 /* close button */
2227 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2228 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2230 if (!WFLAGP(wwin, no_close_button)) {
2231 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2232 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2234 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2235 mask = wwin->wm_gnustep_attr->close_mask;
2236 else
2237 mask = None;
2239 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2240 || fwin->rbutton_image->mask != mask)) {
2241 wPixmapDestroy(fwin->rbutton_image);
2242 fwin->rbutton_image = NULL;
2245 if (!fwin->rbutton_image) {
2246 fwin->rbutton_image = wPixmapCreate(pixmap, mask);
2247 fwin->rbutton_image->client_owned = 1;
2248 fwin->rbutton_image->client_owned_mask = 1;
2251 } else if (WFLAGP(wwin, kill_close)) {
2252 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2253 wPixmapDestroy(fwin->rbutton_image);
2255 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2257 } else if (MGFLAGP(wwin, broken_close)) {
2258 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2259 wPixmapDestroy(fwin->rbutton_image);
2261 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2263 } else {
2264 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2265 wPixmapDestroy(fwin->rbutton_image);
2267 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2271 /* force buttons to be redrawn */
2272 fwin->flags.need_texture_change = 1;
2273 wFrameWindowPaint(fwin);
2277 *---------------------------------------------------------------------------
2278 * wWindowConfigureBorders--
2279 * Update window border configuration according to attribute flags.
2281 *---------------------------------------------------------------------------
2283 void wWindowConfigureBorders(WWindow *wwin)
2285 if (wwin->frame) {
2286 int flags;
2287 int newy, oldh;
2289 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2291 #ifdef XKB_BUTTON_HINT
2292 if (wPreferences.modelock)
2293 flags |= WFF_LANGUAGE_BUTTON;
2294 #endif
2296 if (HAS_TITLEBAR(wwin))
2297 flags |= WFF_TITLEBAR;
2298 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2299 flags |= WFF_RESIZEBAR;
2300 if (HAS_BORDER(wwin))
2301 flags |= WFF_BORDER;
2302 if (wwin->flags.shaded)
2303 flags |= WFF_IS_SHADED;
2304 if (wwin->flags.selected)
2305 flags |= WFF_SELECTED;
2307 oldh = wwin->frame->top_width;
2308 wFrameWindowUpdateBorders(wwin->frame, flags);
2309 if (oldh != wwin->frame->top_width) {
2310 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2312 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2313 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2316 flags = 0;
2317 if (!WFLAGP(wwin, no_miniaturize_button)
2318 && wwin->frame->flags.hide_left_button)
2319 flags |= WFF_LEFT_BUTTON;
2321 #ifdef XKB_BUTTON_HINT
2322 if (!WFLAGP(wwin, no_language_button)
2323 && wwin->frame->flags.hide_language_button)
2324 flags |= WFF_LANGUAGE_BUTTON;
2325 #endif
2327 if (!WFLAGP(wwin, no_close_button)
2328 && wwin->frame->flags.hide_right_button)
2329 flags |= WFF_RIGHT_BUTTON;
2331 if (flags != 0) {
2332 wWindowUpdateButtonImages(wwin);
2333 wFrameWindowShowButton(wwin->frame, flags);
2336 flags = 0;
2337 if (WFLAGP(wwin, no_miniaturize_button)
2338 && !wwin->frame->flags.hide_left_button)
2339 flags |= WFF_LEFT_BUTTON;
2341 #ifdef XKB_BUTTON_HINT
2342 if (WFLAGP(wwin, no_language_button)
2343 && !wwin->frame->flags.hide_language_button)
2344 flags |= WFF_LANGUAGE_BUTTON;
2345 #endif
2347 if (WFLAGP(wwin, no_close_button)
2348 && !wwin->frame->flags.hide_right_button)
2349 flags |= WFF_RIGHT_BUTTON;
2351 if (flags != 0)
2352 wFrameWindowHideButton(wwin->frame, flags);
2354 #ifdef USE_XSHAPE
2355 if (w_global.xext.shape.supported && wwin->flags.shaped)
2356 wWindowSetShape(wwin);
2357 #endif
2361 void wWindowSaveState(WWindow *wwin)
2363 long data[10];
2364 int i;
2366 memset(data, 0, sizeof(long) * 10);
2367 data[0] = wwin->frame->workspace;
2368 data[1] = wwin->flags.miniaturized;
2369 data[2] = wwin->flags.shaded;
2370 data[3] = wwin->flags.hidden;
2371 data[4] = wwin->flags.maximized;
2372 if (wwin->flags.maximized == 0) {
2373 data[5] = wwin->frame_x;
2374 data[6] = wwin->frame_y;
2375 data[7] = wwin->frame->core->width;
2376 data[8] = wwin->frame->core->height;
2377 } else {
2378 data[5] = wwin->old_geometry.x;
2379 data[6] = wwin->old_geometry.y;
2380 data[7] = wwin->old_geometry.width;
2381 data[8] = wwin->old_geometry.height;
2384 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2385 if (wwin->screen_ptr->shortcutWindows[i] &&
2386 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2387 data[9] |= 1 << i;
2390 XChangeProperty(dpy, wwin->client_win, w_global.atom.wmaker.state,
2391 w_global.atom.wmaker.state, 32, PropModeReplace, (unsigned char *)data, 10);
2394 static int getSavedState(Window window, WSavedState ** state)
2396 Atom type_ret;
2397 int fmt_ret;
2398 unsigned long nitems_ret;
2399 unsigned long bytes_after_ret;
2400 long *data;
2402 if (XGetWindowProperty(dpy, window, w_global.atom.wmaker.state, 0, 10,
2403 True, w_global.atom.wmaker.state,
2404 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2405 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2406 return 0;
2408 if (type_ret != w_global.atom.wmaker.state) {
2409 XFree(data);
2410 return 0;
2413 *state = wmalloc(sizeof(WSavedState));
2415 (*state)->workspace = data[0];
2416 (*state)->miniaturized = data[1];
2417 (*state)->shaded = data[2];
2418 (*state)->hidden = data[3];
2419 (*state)->maximized = data[4];
2420 (*state)->x = data[5];
2421 (*state)->y = data[6];
2422 (*state)->w = data[7];
2423 (*state)->h = data[8];
2424 (*state)->window_shortcuts = data[9];
2426 XFree(data);
2428 return 1;
2431 #ifdef USE_XSHAPE
2432 void wWindowClearShape(WWindow * wwin)
2434 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2435 0, wwin->frame->top_width, None, ShapeSet);
2436 XFlush(dpy);
2439 void wWindowSetShape(WWindow * wwin)
2441 XRectangle rect[2];
2442 int count;
2443 #ifdef OPTIMIZE_SHAPE
2444 XRectangle *rects;
2445 XRectangle *urec;
2446 int ordering;
2448 /* only shape is the client's */
2449 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2450 goto alt_code;
2452 /* Get array of rectangles describing the shape mask */
2453 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2454 if (!rects)
2455 goto alt_code;
2457 urec = malloc(sizeof(XRectangle) * (count + 2));
2458 if (!urec) {
2459 XFree(rects);
2460 goto alt_code;
2463 /* insert our decoration rectangles in the rect list */
2464 memcpy(urec, rects, sizeof(XRectangle) * count);
2465 XFree(rects);
2467 if (HAS_TITLEBAR(wwin)) {
2468 urec[count].x = -1;
2469 urec[count].y = -1 - wwin->frame->top_width;
2470 urec[count].width = wwin->frame->core->width + 2;
2471 urec[count].height = wwin->frame->top_width + 1;
2472 count++;
2474 if (HAS_RESIZEBAR(wwin)) {
2475 urec[count].x = -1;
2476 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2477 urec[count].width = wwin->frame->core->width + 2;
2478 urec[count].height = wwin->frame->bottom_width + 1;
2479 count++;
2482 /* shape our frame window */
2483 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2484 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2485 XFlush(dpy);
2486 wfree(urec);
2487 return;
2489 alt_code:
2490 #endif /* OPTIMIZE_SHAPE */
2491 count = 0;
2492 if (HAS_TITLEBAR(wwin)) {
2493 rect[count].x = -1;
2494 rect[count].y = -1;
2495 rect[count].width = wwin->frame->core->width + 2;
2496 rect[count].height = wwin->frame->top_width + 1;
2497 count++;
2499 if (HAS_RESIZEBAR(wwin)) {
2500 rect[count].x = -1;
2501 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2502 rect[count].width = wwin->frame->core->width + 2;
2503 rect[count].height = wwin->frame->bottom_width + 1;
2504 count++;
2506 if (count > 0) {
2507 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2508 0, 0, rect, count, ShapeSet, Unsorted);
2510 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2511 0, wwin->frame->top_width, wwin->client_win,
2512 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2513 XFlush(dpy);
2515 #endif /* USE_XSHAPE */
2517 /* ====================================================================== */
2519 static FocusMode getFocusMode(WWindow * wwin)
2521 FocusMode mode;
2523 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2524 if (wwin->wm_hints->input == True) {
2525 if (wwin->protocols.TAKE_FOCUS)
2526 mode = WFM_LOCALLY_ACTIVE;
2527 else
2528 mode = WFM_PASSIVE;
2529 } else {
2530 if (wwin->protocols.TAKE_FOCUS)
2531 mode = WFM_GLOBALLY_ACTIVE;
2532 else
2533 mode = WFM_NO_INPUT;
2535 } else {
2536 mode = WFM_PASSIVE;
2538 return mode;
2541 void wWindowSetKeyGrabs(WWindow * wwin)
2543 int i;
2544 WShortKey *key;
2546 for (i = 0; i < WKBD_LAST; i++) {
2547 key = &wKeyBindings[i];
2549 if (key->keycode == 0)
2550 continue;
2551 if (key->modifier != AnyModifier) {
2552 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2553 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2554 #ifdef NUMLOCK_HACK
2555 /* Also grab all modifier combinations possible that include,
2556 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2557 * work even if the NumLock/ScrollLock key is on.
2559 wHackedGrabKey(key->keycode, key->modifier,
2560 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2561 #endif
2563 XGrabKey(dpy, key->keycode, key->modifier,
2564 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2567 wRootMenuBindShortcuts(wwin->frame->core->window);
2570 void wWindowResetMouseGrabs(WWindow * wwin)
2572 /* Mouse grabs can't be done on the client window because of
2573 * ICCCM and because clients that try to do the same will crash.
2575 * But there is a problem which makes tbar buttons of unfocused
2576 * windows not usable as the click goes to the frame window instead
2577 * of the button itself. Must figure a way to fix that.
2580 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2582 if (!WFLAGP(wwin, no_bind_mouse)) {
2583 /* grabs for Meta+drag */
2584 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2585 True, ButtonPressMask | ButtonReleaseMask,
2586 GrabModeSync, GrabModeAsync, None, None);
2588 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2589 * but we only grab it for Button4 and Button 5 since a lot of apps
2590 * use CTRL+Button1-3 for app related functionality
2592 if (wPreferences.resize_increment > 0) {
2593 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2594 True, ButtonPressMask | ButtonReleaseMask,
2595 GrabModeSync, GrabModeAsync, None, None);
2596 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2597 True, ButtonPressMask | ButtonReleaseMask,
2598 GrabModeSync, GrabModeAsync, None, None);
2600 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2601 True, ButtonPressMask | ButtonReleaseMask,
2602 GrabModeSync, GrabModeAsync, None, None);
2603 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2604 True, ButtonPressMask | ButtonReleaseMask,
2605 GrabModeSync, GrabModeAsync, None, None);
2609 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2610 && !wwin->flags.is_gnustep) {
2611 /* the passive grabs to focus the window */
2612 /* if (wPreferences.focus_mode == WKF_CLICK) */
2613 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2614 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2616 XFlush(dpy);
2619 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2621 if (attr->flags & GSExtraFlagsAttr) {
2622 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2623 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2624 wWindowUpdateButtonImages(wwin);
2629 WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
2630 const char *command, pid_t pid, WSavedState * state)
2632 WWindowState *wstate;
2634 wstate = malloc(sizeof(WWindowState));
2635 if (!wstate)
2636 return NULL;
2638 memset(wstate, 0, sizeof(WWindowState));
2639 wstate->pid = pid;
2640 if (instance)
2641 wstate->instance = wstrdup(instance);
2642 if (class)
2643 wstate->class = wstrdup(class);
2644 if (command)
2645 wstate->command = wstrdup(command);
2646 wstate->state = state;
2648 wstate->next = windowState;
2649 windowState = wstate;
2651 return wstate;
2654 static inline int is_same(const char *x, const char *y)
2656 if ((x == NULL) && (y == NULL))
2657 return 1;
2659 if ((x == NULL) || (y == NULL))
2660 return 0;
2662 if (strcmp(x, y) == 0)
2663 return 1;
2664 else
2665 return 0;
2668 WMagicNumber wWindowGetSavedState(Window win)
2670 char *instance, *class, *command = NULL;
2671 WWindowState *wstate = windowState;
2673 if (!wstate)
2674 return NULL;
2676 command = GetCommandForWindow(win);
2677 if (!command)
2678 return NULL;
2680 if (PropGetWMClass(win, &class, &instance)) {
2681 while (wstate) {
2682 if (is_same(instance, wstate->instance) &&
2683 is_same(class, wstate->class) &&
2684 is_same(command, wstate->command)) {
2685 break;
2687 wstate = wstate->next;
2689 } else {
2690 wstate = NULL;
2693 if (command)
2694 wfree(command);
2695 if (instance)
2696 free(instance);
2697 if (class)
2698 free(class);
2700 return wstate;
2703 void wWindowDeleteSavedState(WMagicNumber id)
2705 WWindowState *tmp, *wstate = (WWindowState *) id;
2707 if (!wstate || !windowState)
2708 return;
2710 tmp = windowState;
2711 if (tmp == wstate) {
2712 windowState = wstate->next;
2713 release_wwindowstate(wstate);
2714 } else {
2715 while (tmp->next) {
2716 if (tmp->next == wstate) {
2717 tmp->next = wstate->next;
2718 release_wwindowstate(wstate);
2719 break;
2721 tmp = tmp->next;
2726 void wWindowDeleteSavedStatesForPID(pid_t pid)
2728 WWindowState *tmp, *wstate;
2730 if (!windowState)
2731 return;
2733 tmp = windowState;
2734 if (tmp->pid == pid) {
2735 wstate = windowState;
2736 windowState = tmp->next;
2738 release_wwindowstate(wstate);
2739 } else {
2740 while (tmp->next) {
2741 if (tmp->next->pid == pid) {
2742 wstate = tmp->next;
2743 tmp->next = wstate->next;
2744 release_wwindowstate(wstate);
2745 break;
2747 tmp = tmp->next;
2752 static void release_wwindowstate(WWindowState *wstate)
2754 if (wstate->instance)
2755 wfree(wstate->instance);
2757 if (wstate->class)
2758 wfree(wstate->class);
2760 if (wstate->command)
2761 wfree(wstate->command);
2763 wfree(wstate->state);
2764 wfree(wstate);
2767 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2769 if (wwin->flags.omnipresent == flag)
2770 return;
2772 wwin->flags.omnipresent = flag;
2773 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2776 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2778 WWindow *wwin = data;
2780 /* Parameter not used, but tell the compiler that it is ok */
2781 (void) sender;
2783 #ifndef NUMLOCK_HACK
2784 if ((event->xbutton.state & ValidModMask)
2785 != (event->xbutton.state & ~LockMask)) {
2786 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2787 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2789 #endif
2791 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2793 CloseWindowMenu(wwin->screen_ptr);
2795 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2796 && !WFLAGP(wwin, no_focusable)) {
2797 wSetFocusTo(wwin->screen_ptr, wwin);
2800 if (event->xbutton.button == Button1)
2801 wRaiseFrame(wwin->frame->core);
2803 if (event->xbutton.window != wwin->frame->resizebar->window) {
2804 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2805 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2806 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2807 return;
2811 if (event->xbutton.state & MOD_MASK) {
2812 /* move the window */
2813 wMouseMoveWindow(wwin, event);
2814 XUngrabPointer(dpy, CurrentTime);
2815 } else {
2816 wMouseResizeWindow(wwin, event);
2817 XUngrabPointer(dpy, CurrentTime);
2821 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2823 WWindow *wwin = data;
2825 /* Parameter not used, but tell the compiler that it is ok */
2826 (void) sender;
2828 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2830 if (event->xbutton.button == Button1) {
2831 if (event->xbutton.state == 0) {
2832 if (!WFLAGP(wwin, no_shadeable)) {
2833 /* shade window */
2834 if (wwin->flags.shaded)
2835 wUnshadeWindow(wwin);
2836 else
2837 wShadeWindow(wwin);
2839 } else {
2840 int dir = 0;
2842 if (event->xbutton.state & ControlMask)
2843 dir |= MAX_VERTICAL;
2845 if (event->xbutton.state & ShiftMask) {
2846 dir |= MAX_HORIZONTAL;
2847 if (!(event->xbutton.state & ControlMask))
2848 wSelectWindow(wwin, !wwin->flags.selected);
2851 /* maximize window */
2852 if (dir != 0 && IS_RESIZABLE(wwin)) {
2853 int ndir = dir ^ wwin->flags.maximized;
2855 if (ndir != 0)
2856 wMaximizeWindow(wwin, ndir);
2857 else
2858 wUnmaximizeWindow(wwin);
2861 } else if (event->xbutton.button == Button3) {
2862 if (event->xbutton.state & MOD_MASK)
2863 wHideOtherApplications(wwin);
2864 } else if (event->xbutton.button == Button2) {
2865 wSelectWindow(wwin, !wwin->flags.selected);
2866 } else if (event->xbutton.button == W_getconf_mouseWheelUp()) {
2867 wShadeWindow(wwin);
2868 } else if (event->xbutton.button == W_getconf_mouseWheelDown()) {
2869 wUnshadeWindow(wwin);
2873 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2875 WWindow *wwin = desc->parent;
2876 unsigned int new_width, w_scale;
2877 unsigned int new_height, h_scale;
2878 unsigned int resize_width_increment = 0;
2879 unsigned int resize_height_increment = 0;
2881 if (wwin->normal_hints) {
2882 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2883 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2884 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2885 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2887 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2888 resize_width_increment = wPreferences.resize_increment;
2889 resize_height_increment = wPreferences.resize_increment;
2892 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2894 CloseWindowMenu(wwin->screen_ptr);
2896 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2897 wSetFocusTo(wwin->screen_ptr, wwin);
2899 if (event->xbutton.button == Button1)
2900 wRaiseFrame(wwin->frame->core);
2902 if (event->xbutton.state & ControlMask) {
2903 if (event->xbutton.button == Button4) {
2904 new_width = wwin->client.width - resize_width_increment;
2905 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2906 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2908 if (event->xbutton.button == Button5) {
2909 new_width = wwin->client.width + resize_width_increment;
2910 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2911 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2915 if (event->xbutton.state & MOD_MASK) {
2916 /* move the window */
2917 if (XGrabPointer(dpy, wwin->client_win, False,
2918 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2919 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2920 return;
2922 if (event->xbutton.button == Button3) {
2923 wMouseResizeWindow(wwin, event);
2924 } else if (event->xbutton.button == Button4) {
2925 new_height = wwin->client.height - resize_height_increment;
2926 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2927 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2928 } else if (event->xbutton.button == Button5) {
2929 new_height = wwin->client.height + resize_height_increment;
2930 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2931 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2932 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2933 wMouseMoveWindow(wwin, event);
2935 XUngrabPointer(dpy, CurrentTime);
2939 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2941 WWindow *wwin = (WWindow *) data;
2943 /* Parameter not used, but tell the compiler that it is ok */
2944 (void) sender;
2946 #ifndef NUMLOCK_HACK
2947 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2948 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2949 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2950 #endif
2951 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2953 CloseWindowMenu(wwin->screen_ptr);
2955 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2956 && !WFLAGP(wwin, no_focusable))
2957 wSetFocusTo(wwin->screen_ptr, wwin);
2959 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2961 if (event->xbutton.button == Button1) {
2962 if (event->xbutton.state & MOD_MASK)
2963 wLowerFrame(wwin->frame->core);
2964 else
2965 wRaiseFrame(wwin->frame->core);
2967 if ((event->xbutton.state & ShiftMask)
2968 && !(event->xbutton.state & ControlMask)) {
2969 wSelectWindow(wwin, !wwin->flags.selected);
2970 return;
2972 if (event->xbutton.window != wwin->frame->titlebar->window
2973 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2974 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2975 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2976 return;
2979 /* move the window */
2980 wMouseMoveWindow(wwin, event);
2982 XUngrabPointer(dpy, CurrentTime);
2983 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2984 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2985 WObjDescriptor *desc;
2987 if (event->xbutton.window != wwin->frame->titlebar->window
2988 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2989 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2990 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2991 return;
2994 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2996 /* allow drag select */
2997 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2998 event->xany.send_event = True;
2999 (*desc->handle_mousedown) (desc, event);
3001 XUngrabPointer(dpy, CurrentTime);
3005 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3007 WWindow *wwin = data;
3009 /* Parameter not used, but tell the compiler that it is ok */
3010 (void) sender;
3012 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3014 CloseWindowMenu(wwin->screen_ptr);
3016 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3017 return;
3019 /* if control-click, kill the client */
3020 if (event->xbutton.state & ControlMask) {
3021 wClientKill(wwin);
3022 } else {
3023 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3024 /* send delete message */
3025 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3026 w_global.timestamp.last_event);
3031 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3033 WWindow *wwin = data;
3035 /* Parameter not used, but tell the compiler that it is ok */
3036 (void) sender;
3038 CloseWindowMenu(wwin->screen_ptr);
3040 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3041 return;
3043 /* send delete message */
3044 if (wwin->protocols.DELETE_WINDOW)
3045 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3046 w_global.timestamp.last_event);
3047 else
3048 wClientKill(wwin);
3051 #ifdef XKB_BUTTON_HINT
3052 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3054 WWindow *wwin = data;
3055 WFrameWindow *fwin = wwin->frame;
3056 WScreen *scr = fwin->screen_ptr;
3057 int tl;
3059 /* Parameter not used, but tell the compiler that it is ok */
3060 (void) sender;
3062 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3063 return;
3064 tl = wwin->frame->languagemode;
3065 wwin->frame->languagemode = wwin->frame->last_languagemode;
3066 wwin->frame->last_languagemode = tl;
3067 wSetFocusTo(scr, wwin);
3068 wwin->frame->languagebutton_image =
3069 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3070 wFrameWindowUpdateLanguageButton(wwin->frame);
3071 if (event->xbutton.button == Button3)
3072 return;
3073 wRaiseFrame(fwin->core);
3075 #endif
3077 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3079 WWindow *wwin = data;
3081 /* Parameter not used, but tell the compiler that it is ok */
3082 (void) sender;
3084 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3086 CloseWindowMenu(wwin->screen_ptr);
3088 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3089 return;
3091 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3092 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
3093 w_global.timestamp.last_event);
3094 } else {
3095 WApplication *wapp;
3096 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3098 wapp = wApplicationOf(wwin->main_window);
3099 if (wapp && !WFLAGP(wwin, no_appicon))
3100 wHideApplication(wapp);
3101 } else if (event->xbutton.state == 0) {
3102 wIconifyWindow(wwin);