WRaster: add functions to save image on disk
[wmaker-crm.git] / src / window.c
blobf9cffe942b239a81672a3bf52a9f9c6f9706b423
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 #ifdef USE_XRES
33 #include <X11/extensions/XRes.h>
34 #endif
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <stdint.h>
39 #include <math.h>
41 /* For getting mouse wheel mappings from WINGs */
42 #include <WINGs/WINGs.h>
44 #include "WindowMaker.h"
45 #include "GNUstep.h"
46 #include "wcore.h"
47 #include "framewin.h"
48 #include "texture.h"
49 #include "window.h"
50 #include "winspector.h"
51 #include "icon.h"
52 #include "properties.h"
53 #include "actions.h"
54 #include "client.h"
55 #include "colormap.h"
56 #include "keybind.h"
57 #include "stacking.h"
58 #include "defaults.h"
59 #include "workspace.h"
60 #include "xinerama.h"
61 #include "appmenu.h"
62 #include "appicon.h"
63 #include "superfluous.h"
64 #include "rootmenu.h"
65 #include "placement.h"
66 #include "misc.h"
67 #include "startup.h"
68 #include "winmenu.h"
69 #include "osdep.h"
71 #ifdef USE_MWM_HINTS
72 # include "motif.h"
73 #endif
74 #include "wmspec.h"
76 #define MOD_MASK wPreferences.modifier_mask
79 /***** Local Stuff *****/
80 static WWindowState *windowState = NULL;
81 static FocusMode getFocusMode(WWindow *wwin);
82 static int getSavedState(Window window, WSavedState **state);
83 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
85 /* frame window (during window grabs) */
86 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
88 /* close button */
89 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
90 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
92 /* iconify button */
93 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
95 #ifdef XKB_BUTTON_HINT
96 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
97 #endif
99 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
100 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
101 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
103 static void release_wwindowstate(WWindowState *wstate);
105 /****** Notification Observers ******/
107 static void appearanceObserver(void *self, WMNotification * notif)
109 WWindow *wwin = (WWindow *) self;
110 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
112 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
113 return;
115 if (flags & WFontSettings) {
116 wWindowConfigureBorders(wwin);
117 if (wwin->flags.shaded) {
118 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
119 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
120 wWindowSynthConfigureNotify(wwin);
123 if (flags & WTextureSettings)
124 wwin->frame->flags.need_texture_remake = 1;
126 if (flags & (WTextureSettings | WColorSettings)) {
127 if (wwin->frame->titlebar)
128 XClearWindow(dpy, wwin->frame->titlebar->window);
130 wFrameWindowPaint(wwin->frame);
135 /* Return the WWindow associated with a given (Xlib) Window. */
136 WWindow *wWindowFor(Window window)
138 WObjDescriptor *desc;
140 if (window == None)
141 return NULL;
143 if (XFindContext(dpy, window, w_global.context.client_win, (XPointer *) & desc) == XCNOENT)
144 return NULL;
146 if (desc->parent_type == WCLASS_WINDOW)
147 return desc->parent;
148 else if (desc->parent_type == WCLASS_FRAME) {
149 WFrameWindow *frame = (WFrameWindow *) desc->parent;
150 if (frame->flags.is_client_window_frame)
151 return frame->child;
154 return NULL;
157 WWindow *wWindowCreate(void)
159 WWindow *wwin;
161 wwin = wmalloc(sizeof(WWindow));
162 wretain(wwin);
164 wwin->client_descriptor.handle_mousedown = frameMouseDown;
165 wwin->client_descriptor.parent = wwin;
166 wwin->client_descriptor.self = wwin;
167 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
169 return wwin;
172 void wWindowDestroy(WWindow *wwin)
174 int i;
176 if (wwin->screen_ptr->cmap_window == wwin)
177 wwin->screen_ptr->cmap_window = NULL;
179 WMRemoveNotificationObserver(wwin);
181 wwin->flags.destroyed = 1;
183 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
184 if (!wwin->screen_ptr->shortcutWindows[i])
185 continue;
187 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
189 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
190 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
191 wwin->screen_ptr->shortcutWindows[i] = NULL;
195 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
196 wwin->fake_group->retainCount--;
197 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
198 XDestroyWindow(dpy, wwin->fake_group->leader);
199 wwin->fake_group->leader = None;
200 wwin->fake_group->origLeader = None;
201 XFlush(dpy);
205 if (wwin->normal_hints)
206 XFree(wwin->normal_hints);
208 if (wwin->wm_hints)
209 XFree(wwin->wm_hints);
211 if (wwin->wm_instance)
212 XFree(wwin->wm_instance);
214 if (wwin->wm_class)
215 XFree(wwin->wm_class);
217 if (wwin->wm_gnustep_attr)
218 wfree(wwin->wm_gnustep_attr);
220 if (wwin->cmap_windows)
221 XFree(wwin->cmap_windows);
223 XDeleteContext(dpy, wwin->client_win, w_global.context.client_win);
225 if (wwin->frame)
226 wFrameWindowDestroy(wwin->frame);
228 if (wwin->icon) {
229 RemoveFromStackList(wwin->icon->core);
230 wIconDestroy(wwin->icon);
231 if (wPreferences.auto_arrange_icons)
232 wArrangeIcons(wwin->screen_ptr, True);
234 if (wwin->net_icon_image)
235 RReleaseImage(wwin->net_icon_image);
237 wrelease(wwin);
240 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
242 if (gs_hints->flags & GSWindowStyleAttr) {
243 if (gs_hints->window_style == WMBorderlessWindowMask) {
244 wwin->client_flags.no_border = 1;
245 wwin->client_flags.no_titlebar = 1;
246 wwin->client_flags.no_closable = 1;
247 wwin->client_flags.no_miniaturizable = 1;
248 wwin->client_flags.no_resizable = 1;
249 wwin->client_flags.no_close_button = 1;
250 wwin->client_flags.no_miniaturize_button = 1;
251 wwin->client_flags.no_resizebar = 1;
252 } else {
253 wwin->client_flags.no_close_button =
254 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
256 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
258 wwin->client_flags.no_miniaturize_button =
259 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
261 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
263 wwin->client_flags.no_resizebar =
264 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
266 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
268 /* these attributes supposedly imply in the existence
269 * of a titlebar */
270 if (gs_hints->window_style & (WMResizableWindowMask |
271 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
272 wwin->client_flags.no_titlebar = 0;
273 } else {
274 wwin->client_flags.no_titlebar =
275 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
279 } else {
280 /* setup the defaults */
281 wwin->client_flags.no_border = 0;
282 wwin->client_flags.no_titlebar = 0;
283 wwin->client_flags.no_closable = 0;
284 wwin->client_flags.no_miniaturizable = 0;
285 wwin->client_flags.no_resizable = 0;
286 wwin->client_flags.no_close_button = 0;
287 wwin->client_flags.no_miniaturize_button = 0;
288 wwin->client_flags.no_resizebar = 0;
290 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
291 wwin->client_flags.no_appicon = 1;
294 static void discard_hints_from_gtk(WWindow *wwin)
296 Atom type;
297 int format;
298 unsigned long nb_item, nb_remain;
299 unsigned char *result;
300 int status;
302 status = XGetWindowProperty(dpy, wwin->client_win, w_global.atom.desktop.gtk_object_path, 0, 16, False,
303 AnyPropertyType, &type, &format, &nb_item, &nb_remain, &result);
304 if (status != Success)
305 return;
307 /* If we're here, that means the Property exists. We don't care what is inside, it means it is a GTK-based application */
309 if (result != NULL)
310 XFree(result);
312 /* GTK is asking to remove these decorations: */
313 wwin->client_flags.no_titlebar = 0;
314 wwin->client_flags.no_close_button = 0;
315 wwin->client_flags.no_miniaturize_button = 0;
316 wwin->client_flags.no_resizebar = 0;
319 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
321 WScreen *scr = wwin->screen_ptr;
323 /* sets global default stuff */
324 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags, NULL, True);
325 wwin->defined_user_flags = wwin->user_flags;
328 * Decoration setting is done in this precedence (lower to higher)
329 * - use global default in the resource database
330 * - guess some settings
331 * - use GNUstep/external window attributes
332 * - set hints specified for the app in the resource DB
335 wwin->client_flags.broken_close = 0;
337 if (wwin->protocols.DELETE_WINDOW)
338 wwin->client_flags.kill_close = 0;
339 else
340 wwin->client_flags.kill_close = 1;
342 /* transients can't be iconified or maximized */
343 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
344 wwin->client_flags.no_miniaturizable = 1;
345 wwin->client_flags.no_miniaturize_button = 1;
348 /* if the window can't be resized, remove the resizebar */
349 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
350 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
351 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
352 wwin->client_flags.no_resizable = 1;
353 wwin->client_flags.no_resizebar = 1;
356 /* set GNUstep window attributes */
357 if (wwin->wm_gnustep_attr) {
358 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
360 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
362 *level = wwin->wm_gnustep_attr->window_level;
364 * INT_MIN is the only illegal window level.
366 if (*level == INT_MIN)
367 *level = INT_MIN + 1;
368 } else {
369 /* setup defaults */
370 *level = WMNormalLevel;
372 } else {
373 int tmp_workspace = -1;
374 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
376 #ifdef USE_MWM_HINTS
377 wMWMCheckClientHints(wwin);
378 #endif /* USE_MWM_HINTS */
380 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
382 if (wPreferences.ignore_gtk_decoration_hints)
383 discard_hints_from_gtk(wwin);
385 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
386 * have INT_MIN that means that no window level was requested. -Dan
388 if (tmp_level == INT_MIN) {
389 if (WFLAGP(wwin, floating))
390 *level = WMFloatingLevel;
391 else if (WFLAGP(wwin, sunken))
392 *level = WMSunkenLevel;
393 else
394 *level = WMNormalLevel;
395 } else {
396 *level = tmp_level;
399 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
400 WWindow *transientOwner = wWindowFor(wwin->transient_for);
401 if (transientOwner) {
402 int ownerLevel = transientOwner->frame->core->stacking->window_level;
403 if (ownerLevel > *level)
404 *level = ownerLevel;
408 if (tmp_workspace >= 0)
409 *workspace = tmp_workspace % scr->workspace_count;
413 * Set attributes specified only for that window/class.
414 * This might do duplicate work with the 1st wDefaultFillAttributes().
416 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
417 &wwin->defined_user_flags, False);
419 /* Restore decoration if the user has enabled the
420 * IgnoreDecorationChanges option */
421 if (wwin->user_flags.ignore_decoration_changes) {
422 WSETUFLAG(wwin, no_titlebar, 0);
423 WSETUFLAG(wwin, no_resizable, 0);
424 WSETUFLAG(wwin, no_miniaturizable, 0);
425 WSETUFLAG(wwin, no_resizebar, 0);
426 WSETUFLAG(wwin, no_close_button, 0);
427 WSETUFLAG(wwin, no_miniaturize_button, 0);
428 WSETUFLAG(wwin, no_border, 0);
429 WSETUFLAG(wwin, no_movable, 0);
433 * Sanity checks for attributes that depend on other attributes
435 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
436 wwin->user_flags.emulate_appicon = 0;
438 if (wwin->main_window != None) {
439 WApplication *wapp = wApplicationOf(wwin->main_window);
440 if (wapp && !wapp->flags.emulated)
441 wwin->user_flags.emulate_appicon = 0;
444 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
445 wwin->user_flags.emulate_appicon = 0;
447 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
448 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
449 wwin->user_flags.sunken = 0;
451 /* A window that does not have a title cannot be Shaded and we don't let user override this */
452 wwin->client_flags.no_shadeable = WFLAGP(wwin, no_titlebar);
453 wwin->defined_user_flags.no_shadeable = 0;
455 /* windows that have takefocus=False shouldn't take focus at all */
456 if (wwin->focus_mode == WFM_NO_INPUT)
457 wwin->client_flags.no_focusable = 1;
460 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
462 int w1, h1, w2, h2;
464 w1 = wwin->frame->core->width;
465 h1 = wwin->frame->core->height;
466 w2 = obscured->frame->core->width;
467 h2 = obscured->frame->core->height;
469 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
470 && wwin->frame->workspace != obscured->frame->workspace)
471 return False;
473 if (wwin->frame_x + w1 < obscured->frame_x
474 || wwin->frame_y + h1 < obscured->frame_y
475 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
476 return False;
478 return True;
481 /* Get the corresponding Process Identification Number of the active window */
482 static pid_t getWindowPid(Window win)
484 pid_t pid = -1;
486 pid = wNETWMGetPidForWindow(win);
487 #ifdef USE_XRES
488 if (pid > 0)
489 return pid;
490 else {
491 XResClientIdSpec spec;
492 int status;
493 long i, num_ids = 0;
494 XResClientIdValue *client_ids = NULL;
496 spec.client = win;
497 spec.mask = XRES_CLIENT_ID_PID_MASK;
499 status = XResQueryClientIds(dpy, 1, &spec, &num_ids, &client_ids);
501 if (status != Success)
502 return -1;
504 for (i = 0; i < num_ids; i++) {
505 if (client_ids[i].spec.mask == XRES_CLIENT_ID_PID_MASK) {
506 pid = XResGetClientPid(&client_ids[i]);
507 break;
510 XResClientIdsDestroy(num_ids, client_ids);
512 #endif
513 return pid;
516 static void fixLeaderProperties(WWindow *wwin)
518 XClassHint *classHint;
519 XWMHints *hints, *clientHints;
520 XWindowAttributes attr;
521 Window leaders[2], window;
522 char **argv, *command;
523 int argc, i, pid;
524 Bool haveCommand;
526 classHint = XAllocClassHint();
527 clientHints = XGetWMHints(dpy, wwin->client_win);
528 pid = getWindowPid(wwin->client_win);
529 if (pid > 0)
530 haveCommand = GetCommandForPid(pid, &argv, &argc);
531 else
532 haveCommand = False;
534 leaders[0] = wwin->client_leader;
535 leaders[1] = wwin->group_id;
537 if (haveCommand) {
538 command = GetCommandForWindow(wwin->client_win);
539 if (command)
540 wfree(command); /* command already set. nothing to do. */
541 else
542 XSetCommand(dpy, wwin->client_win, argv, argc);
545 for (i = 0; i < 2; i++) {
546 window = leaders[i];
547 if (window) {
548 if (XGetClassHint(dpy, window, classHint) == 0) {
549 classHint->res_name = wwin->wm_instance;
550 classHint->res_class = wwin->wm_class;
551 XSetClassHint(dpy, window, classHint);
553 hints = XGetWMHints(dpy, window);
554 if (hints) {
555 XFree(hints);
556 } else if (clientHints) {
557 /* set window group leader to self */
558 clientHints->window_group = window;
559 clientHints->flags |= WindowGroupHint;
560 XSetWMHints(dpy, window, clientHints);
563 if (haveCommand) {
564 command = GetCommandForWindow(window);
565 if (command)
566 wfree(command); /* command already set. nothing to do. */
567 else
568 XSetCommand(dpy, window, argv, argc);
571 /* Make sure we get notification when this window is destroyed */
572 if (XGetWindowAttributes(dpy, window, &attr))
573 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
577 XFree(classHint);
578 if (clientHints)
579 XFree(clientHints);
580 if (haveCommand)
581 wfree(argv);
584 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
586 XClassHint *classHint;
587 XWMHints *hints;
588 Window leader;
589 int argc;
590 char **argv;
592 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
593 /* set class hint */
594 classHint = XAllocClassHint();
595 classHint->res_name = instance;
596 classHint->res_class = class;
597 XSetClassHint(dpy, leader, classHint);
598 XFree(classHint);
600 /* inherit these from the original leader if available */
601 hints = XGetWMHints(dpy, win);
602 if (!hints) {
603 hints = XAllocWMHints();
604 hints->flags = 0;
606 /* set window group leader to self */
607 hints->window_group = leader;
608 hints->flags |= WindowGroupHint;
609 XSetWMHints(dpy, leader, hints);
610 XFree(hints);
612 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
613 XSetCommand(dpy, leader, argv, argc);
614 XFreeStringList(argv);
617 return leader;
620 static int matchIdentifier(const void *item, const void *cdata)
622 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
626 *----------------------------------------------------------------
627 * wManageWindow--
628 * reparents the window and allocates a descriptor for it.
629 * Window manager hints and other hints are fetched to configure
630 * the window decoration attributes and others. User preferences
631 * for the window are used if available, to configure window
632 * decorations and some behaviour.
633 * If in startup, windows that are override redirect,
634 * unmapped and never were managed and are Withdrawn are not
635 * managed.
637 * Returns:
638 * the new window descriptor
640 * Side effects:
641 * The window is reparented and appropriate notification
642 * is done to the client. Input mask for the window is setup.
643 * The window descriptor is also associated with various window
644 * contexts and inserted in the head of the window list.
645 * Event handler contexts are associated for some objects
646 * (buttons, titlebar and resizebar)
648 *----------------------------------------------------------------
650 WWindow *wManageWindow(WScreen *scr, Window window)
652 WWindow *wwin;
653 int x, y;
654 unsigned width, height;
655 XWindowAttributes wattribs;
656 XSetWindowAttributes attribs;
657 WWindowState *win_state;
658 WWindow *transientOwner = NULL;
659 int window_level;
660 int wm_state;
661 int foo;
662 int workspace = -1;
663 char *title;
664 Bool withdraw = False;
665 Bool raise = False;
667 /* mutex. */
668 XGrabServer(dpy);
669 XSync(dpy, False);
671 /* make sure the window is still there */
672 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
673 XUngrabServer(dpy);
674 return NULL;
677 /* if it's an override-redirect, ignore it */
678 if (wattribs.override_redirect) {
679 XUngrabServer(dpy);
680 return NULL;
683 wm_state = PropGetWindowState(window);
685 /* if it's startup and the window is unmapped, don't manage it */
686 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
687 XUngrabServer(dpy);
688 return NULL;
691 wwin = wWindowCreate();
693 title = wNETWMGetWindowName(window);
694 if (title)
695 wwin->flags.net_has_title = 1;
696 else if (!wFetchName(dpy, window, &title))
697 title = NULL;
699 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
701 #ifdef USE_XSHAPE
702 if (w_global.xext.shape.supported) {
703 int junk;
704 unsigned int ujunk;
705 int b_shaped;
707 XShapeSelectInput(dpy, window, ShapeNotifyMask);
708 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
709 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
710 wwin->flags.shaped = b_shaped;
712 #endif
714 /* Get hints and other information in properties */
715 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
717 /* setup descriptor */
718 wwin->client_win = window;
719 wwin->screen_ptr = scr;
720 wwin->old_border_width = wattribs.border_width;
721 wwin->event_mask = CLIENT_EVENTS;
722 attribs.event_mask = CLIENT_EVENTS;
723 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
724 attribs.save_under = False;
726 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
727 XSetWindowBorderWidth(dpy, window, 0);
729 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") != 0)
730 wwin->flags.fullscreen_monitors[0] = -1;
732 /* get hints from GNUstep app */
733 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
734 wwin->flags.is_gnustep = 1;
736 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
737 wwin->wm_gnustep_attr = NULL;
739 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
740 wwin->flags.is_dockapp = 1;
741 withdraw = True;
744 wwin->client_leader = PropGetClientLeader(window);
745 if (wwin->client_leader != None)
746 wwin->main_window = wwin->client_leader;
748 wwin->wm_hints = XGetWMHints(dpy, window);
750 if (wwin->wm_hints) {
751 if (wwin->wm_hints->flags & StateHint) {
752 if (wwin->wm_hints->initial_state == IconicState) {
753 wwin->flags.miniaturized = 1;
754 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
755 wwin->flags.is_dockapp = 1;
756 withdraw = True;
760 if (wwin->wm_hints->flags & WindowGroupHint) {
761 wwin->group_id = wwin->wm_hints->window_group;
762 /* window_group has priority over CLIENT_LEADER */
763 wwin->main_window = wwin->group_id;
764 } else {
765 wwin->group_id = None;
768 if (wwin->wm_hints->flags & UrgencyHint) {
769 wwin->flags.urgent = 1;
770 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
772 } else {
773 wwin->group_id = None;
776 PropGetProtocols(window, &wwin->protocols);
778 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
779 wwin->transient_for = None;
780 } else {
781 if (wwin->transient_for == None || wwin->transient_for == window) {
782 wwin->transient_for = scr->root_win;
783 } else {
784 transientOwner = wWindowFor(wwin->transient_for);
785 if (transientOwner && transientOwner->main_window != None)
786 wwin->main_window = transientOwner->main_window;
790 /* guess the focus mode */
791 wwin->focus_mode = getFocusMode(wwin);
793 /* get geometry stuff */
794 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
796 /* get colormap windows */
797 GetColormapWindows(wwin);
800 * Setup the decoration/window attributes and
801 * geometry
803 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
805 /* Make broken apps behave as a nice app. */
806 if (WFLAGP(wwin, emulate_appicon))
807 wwin->main_window = wwin->client_win;
809 fixLeaderProperties(wwin);
811 wwin->orig_main_window = wwin->main_window;
813 if (wwin->flags.is_gnustep)
814 wwin->client_flags.shared_appicon = 0;
816 if (wwin->main_window) {
817 XTextProperty text_prop;
819 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, w_global.atom.wmaker.menu))
820 wwin->client_flags.shared_appicon = 0;
823 if (wwin->flags.is_dockapp)
824 wwin->client_flags.shared_appicon = 0;
826 if (wwin->main_window) {
827 WApplication *app = wApplicationOf(wwin->main_window);
828 if (app && app->app_icon)
829 wwin->client_flags.shared_appicon = 0;
832 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
833 char *buffer, *instance, *class;
834 WFakeGroupLeader *fPtr;
835 int index;
837 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
839 /* // only enter here if PropGetWMClass() succeeds */
840 PropGetWMClass(wwin->main_window, &class, &instance);
841 buffer = StrConcatDot(instance, class);
843 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
844 if (index != WANotFound) {
845 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
846 if (fPtr->retainCount == 0)
847 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
848 instance, class);
850 fPtr->retainCount++;
851 if (fPtr->origLeader == None) {
852 if (ADEQUATE(wwin->main_window)) {
853 fPtr->retainCount++;
854 fPtr->origLeader = wwin->main_window;
857 wwin->fake_group = fPtr;
858 wwin->main_window = fPtr->leader;
859 wfree(buffer);
860 } else {
861 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
863 fPtr->identifier = buffer;
864 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
865 fPtr->origLeader = None;
866 fPtr->retainCount = 1;
868 WMAddToArray(scr->fakeGroupLeaders, fPtr);
870 if (ADEQUATE(wwin->main_window)) {
871 fPtr->retainCount++;
872 fPtr->origLeader = wwin->main_window;
874 wwin->fake_group = fPtr;
875 wwin->main_window = fPtr->leader;
878 if (instance)
879 free(instance);
881 if (class)
882 free(class);
883 #undef ADEQUATE
887 * Setup the initial state of the window
889 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
890 wwin->flags.miniaturized = 1;
892 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
893 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
895 wNETWMCheckInitialClientState(wwin);
897 /* apply previous state if it exists and we're in startup */
898 if (scr->flags.startup && wm_state >= 0) {
899 if (wm_state == IconicState)
900 wwin->flags.miniaturized = 1;
901 else if (wm_state == WithdrawnState)
902 withdraw = True;
905 /* if there is a saved state (from file), restore it */
906 win_state = NULL;
907 if (wwin->main_window != None)
908 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
909 else
910 win_state = (WWindowState *) wWindowGetSavedState(window);
912 if (win_state && !withdraw) {
913 if (win_state->state->hidden > 0)
914 wwin->flags.hidden = win_state->state->hidden;
916 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
917 wwin->flags.shaded = win_state->state->shaded;
919 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
920 wwin->flags.miniaturized = win_state->state->miniaturized;
922 if (win_state->state->maximized > 0)
923 wwin->flags.maximized = win_state->state->maximized;
925 if (!IS_OMNIPRESENT(wwin)) {
926 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
927 wwin->wm_class);
928 if (w < 0 || w >= scr->workspace_count) {
929 workspace = win_state->state->workspace;
930 if (workspace >= scr->workspace_count)
931 workspace = scr->current_workspace;
932 } else {
933 workspace = w;
935 } else {
936 workspace = scr->current_workspace;
940 /* if we're restarting, restore saved state (from hints).
941 * This will overwrite previous */
943 WSavedState *wstate;
945 if (getSavedState(window, &wstate)) {
946 wwin->flags.shaded = wstate->shaded;
947 wwin->flags.hidden = wstate->hidden;
948 wwin->flags.miniaturized = wstate->miniaturized;
949 wwin->flags.maximized = wstate->maximized;
950 if (wwin->flags.maximized) {
951 wwin->old_geometry.x = wstate->x;
952 wwin->old_geometry.y = wstate->y;
953 wwin->old_geometry.width = wstate->w;
954 wwin->old_geometry.height = wstate->h;
957 workspace = wstate->workspace;
958 } else {
959 wstate = NULL;
962 /* restore window shortcut */
963 if (wstate != NULL || win_state != NULL) {
964 unsigned mask = 0;
966 if (win_state != NULL)
967 mask = win_state->state->window_shortcuts;
969 if (wstate != NULL && mask == 0)
970 mask = wstate->window_shortcuts;
972 if (mask > 0) {
973 int i;
975 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
976 if (mask & (1 << i)) {
977 if (!scr->shortcutWindows[i])
978 scr->shortcutWindows[i] = WMCreateArray(4);
980 WMAddToArray(scr->shortcutWindows[i], wwin);
986 if (wstate != NULL)
987 wfree(wstate);
990 /* don't let transients start miniaturized if their owners are not */
991 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
992 wwin->flags.miniaturized = 0;
993 if (wwin->wm_hints)
994 wwin->wm_hints->initial_state = NormalState;
997 /* set workspace on which the window starts */
998 if (workspace >= 0) {
999 if (workspace > scr->workspace_count - 1)
1000 workspace = workspace % scr->workspace_count;
1001 } else {
1002 int w;
1004 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1006 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1007 workspace = w;
1008 } else {
1009 if (wPreferences.open_transients_with_parent && transientOwner)
1010 workspace = transientOwner->frame->workspace;
1011 else
1012 workspace = scr->current_workspace;
1016 /* setup window geometry */
1017 if (win_state && win_state->state->w > 0) {
1018 width = win_state->state->w;
1019 height = win_state->state->h;
1022 wWindowConstrainSize(wwin, &width, &height);
1024 /* do not ask for window placement if the window is
1025 * transient, during startup, or if the window wants
1026 * to start iconic. If geometry was saved, restore it. */
1028 Bool dontBring = False;
1030 if (win_state && win_state->state->w > 0) {
1031 x = win_state->state->x;
1032 y = win_state->state->y;
1033 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1034 && !scr->flags.startup
1035 && !wwin->flags.miniaturized
1036 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1038 if (transientOwner && transientOwner->flags.mapped) {
1039 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1040 WMRect rect;
1041 int head;
1043 x = transientOwner->frame_x +
1044 abs(((int)transientOwner->frame->core->width - (int)width) / 2) + offs;
1045 y = transientOwner->frame_y +
1046 abs(((int)transientOwner->frame->core->height - (int)height) / 3) + offs;
1048 /* limit transient windows to be inside their parent's head */
1049 rect.pos.x = transientOwner->frame_x;
1050 rect.pos.y = transientOwner->frame_y;
1051 rect.size.width = transientOwner->frame->core->width;
1052 rect.size.height = transientOwner->frame->core->height;
1054 head = wGetHeadForRect(scr, rect);
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 } else {
1068 PlaceWindow(wwin, &x, &y, width, height);
1071 if (wPreferences.window_placement == WPM_MANUAL)
1072 dontBring = True;
1074 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1075 int head, flags;
1076 WMRect rect;
1077 int reposition = 0;
1079 /* Make spash screens come out in the center of a head
1080 * trouble is that most splashies never get here
1081 * they are managed trough atoms but god knows where.
1082 * Dan, do you know ? -peter
1084 * Most of them are not managed, they have set
1085 * OverrideRedirect, which means we can't do anything about
1086 * them. -alfredo */
1088 /* xinerama checks for: across head and dead space */
1089 rect.pos.x = x;
1090 rect.pos.y = y;
1091 rect.size.width = width;
1092 rect.size.height = height;
1094 head = wGetRectPlacementInfo(scr, rect, &flags);
1096 if (flags & XFLAG_DEAD)
1097 reposition = 1;
1099 if (flags & XFLAG_MULTIPLE)
1100 reposition = 2;
1103 switch (reposition) {
1104 case 1:
1105 head = wGetHeadForPointerLocation(scr);
1106 rect = wGetRectForHead(scr, head);
1108 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1109 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1110 break;
1112 case 2:
1113 rect = wGetRectForHead(scr, head);
1115 if (x < rect.pos.x)
1116 x = rect.pos.x;
1117 else if (x + width > rect.pos.x + rect.size.width)
1118 x = rect.pos.x + rect.size.width - width;
1120 if (y < rect.pos.y)
1121 y = rect.pos.y;
1122 else if (y + height > rect.pos.y + rect.size.height)
1123 y = rect.pos.y + rect.size.height - height;
1125 break;
1127 default:
1128 break;
1132 if (WFLAGP(wwin, dont_move_off) && dontBring)
1133 wScreenBringInside(scr, &x, &y, width, height);
1136 wNETWMPositionSplash(wwin, &x, &y, width, height);
1138 if (wwin->flags.urgent) {
1139 if (!IS_OMNIPRESENT(wwin))
1140 wwin->flags.omnipresent ^= 1;
1143 /* Create frame, borders and do reparenting */
1144 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1145 #ifdef XKB_BUTTON_HINT
1146 if (wPreferences.modelock)
1147 foo |= WFF_LANGUAGE_BUTTON;
1148 #endif
1149 if (HAS_TITLEBAR(wwin))
1150 foo |= WFF_TITLEBAR;
1152 if (HAS_RESIZEBAR(wwin))
1153 foo |= WFF_RESIZEBAR;
1155 if (HAS_BORDER(wwin))
1156 foo |= WFF_BORDER;
1158 wwin->frame = wFrameWindowCreate(scr, window_level,
1159 x, y, width, height,
1160 &wPreferences.window_title_clearance,
1161 &wPreferences.window_title_min_height,
1162 &wPreferences.window_title_max_height,
1163 foo,
1164 scr->window_title_texture,
1165 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1166 wattribs.depth, wattribs.visual, wattribs.colormap);
1168 wwin->frame->flags.is_client_window_frame = 1;
1169 wwin->frame->flags.justification = wPreferences.title_justification;
1171 wNETWMCheckInitialFrameState(wwin);
1173 /* setup button images */
1174 wWindowUpdateButtonImages(wwin);
1176 /* hide unused buttons */
1177 foo = 0;
1178 if (WFLAGP(wwin, no_close_button))
1179 foo |= WFF_RIGHT_BUTTON;
1181 if (WFLAGP(wwin, no_miniaturize_button))
1182 foo |= WFF_LEFT_BUTTON;
1184 #ifdef XKB_BUTTON_HINT
1185 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1186 foo |= WFF_LANGUAGE_BUTTON;
1187 #endif
1188 if (foo != 0)
1189 wFrameWindowHideButton(wwin->frame, foo);
1191 wwin->frame->child = wwin;
1192 wwin->frame->workspace = workspace;
1193 wwin->frame->on_click_left = windowIconifyClick;
1195 #ifdef XKB_BUTTON_HINT
1196 if (wPreferences.modelock)
1197 wwin->frame->on_click_language = windowLanguageClick;
1198 #endif
1200 wwin->frame->on_click_right = windowCloseClick;
1201 wwin->frame->on_dblclick_right = windowCloseDblClick;
1202 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1203 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1204 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1206 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1207 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1208 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1211 int gx, gy;
1213 wClientGetGravityOffsets(wwin, &gx, &gy);
1215 /* if gravity is to the south, account for the border sizes */
1216 if (gy > 0)
1217 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1221 * wWindowConfigure() will init the client window's size
1222 * (wwin->client.{width,height}) and all other geometry
1223 * related variables (frame_x,frame_y) */
1224 wWindowConfigure(wwin, x, y, width, height);
1226 /* to make sure the window receives it's new position after reparenting */
1227 wWindowSynthConfigureNotify(wwin);
1229 /* Setup descriptors and save window to internal lists */
1230 if (wwin->main_window != None) {
1231 WApplication *app;
1232 WWindow *leader;
1234 /* Leader windows do not necessary set themselves as leaders.
1235 * If this is the case, point the leader of this window to
1236 * itself */
1237 leader = wWindowFor(wwin->main_window);
1238 if (leader && leader->main_window == None)
1239 leader->main_window = leader->client_win;
1241 app = wApplicationCreate(wwin);
1242 if (app) {
1243 app->last_workspace = workspace;
1245 /* Do application specific stuff, like setting application
1246 * wide attributes. */
1248 if (wwin->flags.hidden) {
1249 /* if the window was set to hidden because it was hidden
1250 * in a previous incarnation and that state was restored */
1251 app->flags.hidden = 1;
1252 } else if (app->flags.hidden) {
1253 if (WFLAGP(app->main_window_desc, start_hidden)) {
1254 wwin->flags.hidden = 1;
1255 } else {
1256 wUnhideApplication(app, False, False);
1257 raise = True;
1260 wAppBounce(app);
1264 /* setup the frame descriptor */
1265 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1266 wwin->frame->core->descriptor.parent = wwin;
1267 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1269 /* don't let windows go away if we die */
1270 XAddToSaveSet(dpy, window);
1272 XLowerWindow(dpy, window);
1274 /* if window is in this workspace and should be mapped, then map it */
1275 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1276 && !wwin->flags.hidden && !withdraw) {
1278 /* The following "if" is to avoid crashing of clients that expect
1279 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1280 * after the return from this function. */
1281 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1282 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1283 else
1284 wClientSetState(wwin, NormalState, None);
1286 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1287 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1289 * The brain damaged idiotic non-click to focus modes will
1290 * have trouble with this because:
1292 * 1. window is created and mapped by the client
1293 * 2. window is mapped by wmaker in small size
1294 * 3. window is animated to grow to normal size
1295 * 4. this function returns to normal event loop
1296 * 5. eventually, the EnterNotify event that would trigger
1297 * the window focusing (if the mouse is over that window)
1298 * will be processed by wmaker.
1299 * But since this event will be rather delayed
1300 * (step 3 has a large delay) the time when the event occurred
1301 * and when it is processed, the client that owns that window
1302 * will reject the XSetInputFocus() for it.
1304 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1305 DoWindowBirth(wwin);
1307 wWindowMap(wwin);
1310 /* setup stacking descriptor */
1311 if (transientOwner)
1312 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1313 else
1314 wwin->frame->core->stacking->child_of = NULL;
1316 if (!scr->focused_window) {
1317 /* first window on the list */
1318 wwin->next = NULL;
1319 wwin->prev = NULL;
1320 scr->focused_window = wwin;
1321 } else {
1322 WWindow *tmp;
1324 /* add window at beginning of focus window list */
1325 tmp = scr->focused_window;
1326 while (tmp->prev)
1327 tmp = tmp->prev;
1329 tmp->prev = wwin;
1330 wwin->next = tmp;
1331 wwin->prev = NULL;
1334 /* raise is set to true if we un-hid the app when this window was born.
1335 * we raise, else old windows of this app will be above this new one. */
1336 if (raise)
1337 wRaiseFrame(wwin->frame->core);
1339 /* Update name must come after WApplication stuff is done */
1340 wWindowUpdateName(wwin, title);
1341 if (title)
1342 XFree(title);
1344 XUngrabServer(dpy);
1346 /* Final preparations before window is ready to go */
1347 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1349 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1350 if (((transientOwner && transientOwner->flags.focused)
1351 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1353 /* only auto_focus if on same screen as mouse
1354 * (and same head for xinerama mode)
1355 * TODO: make it an option */
1357 /*TODO add checking the head of the window, is it available? */
1358 short same_screen = 0, same_head = 1;
1360 int foo;
1361 unsigned int bar;
1362 Window dummy;
1364 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1365 &foo, &foo, &foo, &foo, &bar) != False)
1366 same_screen = 1;
1368 if (same_screen == 1 && same_head == 1)
1369 wSetFocusTo(scr, wwin);
1372 wWindowResetMouseGrabs(wwin);
1374 if (!WFLAGP(wwin, no_bind_keys))
1375 wWindowSetKeyGrabs(wwin);
1377 WMPostNotificationName(WMNManaged, wwin, NULL);
1378 wColormapInstallForWindow(scr, scr->cmap_window);
1380 /* Setup Notification Observers */
1381 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1383 /* Cleanup temporary stuff */
1384 if (win_state)
1385 wWindowDeleteSavedState(win_state);
1387 /* If the window must be withdrawed, then do it now.
1388 * Must do some optimization, 'though */
1389 if (withdraw) {
1390 wwin->flags.mapped = 0;
1391 wClientSetState(wwin, WithdrawnState, None);
1392 wUnmanageWindow(wwin, True, False);
1393 wwin = NULL;
1396 return wwin;
1399 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1400 const char *title, int x, int y, int width, int height)
1402 WWindow *wwin;
1403 int foo;
1405 wwin = wWindowCreate();
1407 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1409 wwin->flags.internal_window = 1;
1411 wwin->client_flags.omnipresent = 1;
1412 wwin->client_flags.no_shadeable = 1;
1413 wwin->client_flags.no_resizable = 1;
1414 wwin->client_flags.no_miniaturizable = 1;
1416 wwin->focus_mode = WFM_PASSIVE;
1417 wwin->client_win = window;
1418 wwin->screen_ptr = scr;
1419 wwin->transient_for = owner;
1420 wwin->client.x = x;
1421 wwin->client.y = y;
1422 wwin->client.width = width;
1423 wwin->client.height = height;
1424 wwin->frame_x = wwin->client.x;
1425 wwin->frame_y = wwin->client.y;
1427 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1428 foo |= WFF_TITLEBAR;
1429 #ifdef XKB_BUTTON_HINT
1430 foo |= WFF_LANGUAGE_BUTTON;
1431 #endif
1433 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1434 wwin->frame_x, wwin->frame_y,
1435 width, height,
1436 &wPreferences.window_title_clearance,
1437 &wPreferences.window_title_min_height,
1438 &wPreferences.window_title_max_height,
1439 foo,
1440 scr->window_title_texture,
1441 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1442 scr->w_depth, scr->w_visual, scr->w_colormap);
1444 XSaveContext(dpy, window, w_global.context.client_win, (XPointer) & wwin->client_descriptor);
1446 wwin->frame->flags.is_client_window_frame = 1;
1447 wwin->frame->flags.justification = wPreferences.title_justification;
1449 wFrameWindowChangeTitle(wwin->frame, title);
1451 /* setup button images */
1452 wWindowUpdateButtonImages(wwin);
1454 /* hide buttons */
1455 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1457 wwin->frame->child = wwin;
1458 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1460 #ifdef XKB_BUTTON_HINT
1461 if (wPreferences.modelock)
1462 wwin->frame->on_click_language = windowLanguageClick;
1463 #endif
1465 wwin->frame->on_click_right = windowCloseClick;
1466 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1467 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1468 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1469 wwin->client.y += wwin->frame->top_width;
1471 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1472 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1474 /* setup the frame descriptor */
1475 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1476 wwin->frame->core->descriptor.parent = wwin;
1477 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1479 XLowerWindow(dpy, window);
1480 XMapSubwindows(dpy, wwin->frame->core->window);
1482 /* setup stacking descriptor */
1483 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1484 WWindow *tmp;
1485 tmp = wWindowFor(wwin->transient_for);
1486 if (tmp)
1487 wwin->frame->core->stacking->child_of = tmp->frame->core;
1488 } else {
1489 wwin->frame->core->stacking->child_of = NULL;
1492 if (!scr->focused_window) {
1493 /* first window on the list */
1494 wwin->next = NULL;
1495 wwin->prev = NULL;
1496 scr->focused_window = wwin;
1497 } else {
1498 WWindow *tmp;
1500 /* add window at beginning of focus window list */
1501 tmp = scr->focused_window;
1502 while (tmp->prev)
1503 tmp = tmp->prev;
1504 tmp->prev = wwin;
1505 wwin->next = tmp;
1506 wwin->prev = NULL;
1509 if (wwin->flags.is_gnustep == 0)
1510 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1512 /* if (wPreferences.auto_focus) */
1513 wSetFocusTo(scr, wwin);
1514 wWindowResetMouseGrabs(wwin);
1515 wWindowSetKeyGrabs(wwin);
1517 return wwin;
1521 *----------------------------------------------------------------------
1522 * wUnmanageWindow--
1523 * Removes the frame window from a window and destroys all data
1524 * related to it. The window will be reparented back to the root window
1525 * if restore is True.
1527 * Side effects:
1528 * Everything related to the window is destroyed and the window
1529 * is removed from the window lists. Focus is set to the previous on the
1530 * window list.
1531 *----------------------------------------------------------------------
1533 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1535 WCoreWindow *frame = wwin->frame->core;
1536 WWindow *owner = NULL;
1537 WWindow *newFocusedWindow = NULL;
1538 int wasFocused;
1539 WScreen *scr = wwin->screen_ptr;
1541 /* First close attribute editor window if open */
1542 if (wwin->flags.inspector_open)
1543 wCloseInspectorForWindow(wwin);
1545 /* Close window menu if it's open for this window */
1546 if (wwin->flags.menu_open_for_me)
1547 CloseWindowMenu(scr);
1549 /* Don't restore focus to this window after a window exits
1550 * fullscreen mode */
1551 if (scr->bfs_focused_window == wwin)
1552 scr->bfs_focused_window = NULL;
1554 if (!destroyed) {
1555 if (!wwin->flags.internal_window)
1556 XRemoveFromSaveSet(dpy, wwin->client_win);
1558 /* If this is a leader window, we still need to listen for
1559 * DestroyNotify and PropertyNotify. */
1560 if (wApplicationOf(wwin->client_win))
1561 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1562 else
1563 XSelectInput(dpy, wwin->client_win, NoEventMask);
1565 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1566 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1569 XUnmapWindow(dpy, frame->window);
1571 XUnmapWindow(dpy, wwin->client_win);
1573 /* deselect window */
1574 wSelectWindow(wwin, False);
1576 /* remove all pending events on window */
1577 /* I think this only matters for autoraise */
1578 if (wPreferences.raise_delay)
1579 WMDeleteTimerWithClientData(wwin->frame->core);
1581 XFlush(dpy);
1583 /* reparent the window back to the root */
1584 if (restore)
1585 wClientRestore(wwin);
1587 if (wwin->transient_for != scr->root_win) {
1588 owner = wWindowFor(wwin->transient_for);
1589 if (owner) {
1590 if (!owner->flags.semi_focused)
1591 owner = NULL;
1592 else
1593 owner->flags.semi_focused = 0;
1597 wasFocused = wwin->flags.focused;
1599 /* remove from window focus list */
1600 if (!wwin->prev && !wwin->next) {
1601 /* was the only window */
1602 scr->focused_window = NULL;
1603 newFocusedWindow = NULL;
1604 } else {
1605 WWindow *tmp;
1607 if (wwin->prev)
1608 wwin->prev->next = wwin->next;
1610 if (wwin->next)
1611 wwin->next->prev = wwin->prev;
1612 else {
1613 scr->focused_window = wwin->prev;
1614 scr->focused_window->next = NULL;
1617 if (wPreferences.focus_mode == WKF_CLICK) {
1619 /* if in click to focus mode and the window
1620 * was a transient, focus the owner window
1622 tmp = NULL;
1623 if (wPreferences.focus_mode == WKF_CLICK) {
1624 tmp = wWindowFor(wwin->transient_for);
1625 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1626 tmp = NULL;
1629 /* otherwise, focus the next one in the focus list */
1630 if (!tmp) {
1631 tmp = scr->focused_window;
1632 while (tmp) { /* look for one in the window list first */
1633 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1634 && (tmp->flags.mapped || tmp->flags.shaded))
1635 break;
1636 tmp = tmp->prev;
1638 if (!tmp) { /* if unsuccessful, choose any focusable window */
1639 tmp = scr->focused_window;
1640 while (tmp) {
1641 if (!WFLAGP(tmp, no_focusable)
1642 && (tmp->flags.mapped || tmp->flags.shaded))
1643 break;
1644 tmp = tmp->prev;
1649 newFocusedWindow = tmp;
1651 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1652 unsigned int mask;
1653 int foo;
1654 Window bar, win;
1656 /* This is to let the root window get the keyboard input
1657 * if Sloppy focus mode and no other window get focus.
1658 * This way keybindings will not freeze.
1660 tmp = NULL;
1661 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1662 tmp = wWindowFor(win);
1663 if (tmp == wwin)
1664 tmp = NULL;
1665 newFocusedWindow = tmp;
1666 } else {
1667 newFocusedWindow = NULL;
1671 if (!wwin->flags.internal_window)
1672 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1673 if (wasFocused) {
1674 if (newFocusedWindow != owner && owner) {
1675 if (wwin->flags.is_gnustep == 0)
1676 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1678 wSetFocusTo(scr, newFocusedWindow);
1681 /* Close menu and unhighlight */
1682 WApplication *oapp = wApplicationOf(wwin->main_window);
1683 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1684 if (oapp && oapp != napp) {
1685 wAppMenuUnmap(oapp->menu);
1686 if (wPreferences.highlight_active_app)
1687 wApplicationDeactivate(oapp);
1690 wNETCleanupFrameExtents(wwin);
1692 wWindowDestroy(wwin);
1693 XFlush(dpy);
1696 void wWindowMap(WWindow *wwin)
1698 XMapWindow(dpy, wwin->frame->core->window);
1699 if (!wwin->flags.shaded) {
1700 /* window will be remapped when getting MapNotify */
1701 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1702 XMapWindow(dpy, wwin->client_win);
1703 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1705 wwin->flags.mapped = 1;
1709 void wWindowUnmap(WWindow *wwin)
1711 wwin->flags.mapped = 0;
1713 /* prevent window withdrawal when getting UnmapNotify */
1714 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1715 XUnmapWindow(dpy, wwin->client_win);
1716 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1718 XUnmapWindow(dpy, wwin->frame->core->window);
1721 void wWindowSingleFocus(WWindow *wwin)
1723 WScreen *scr;
1724 int x, y, move = 0;
1726 if (!wwin)
1727 return;
1729 scr = wwin->screen_ptr;
1730 wMakeWindowVisible(wwin);
1732 x = wwin->frame_x;
1733 y = wwin->frame_y;
1735 /* bring window back to visible area */
1736 move = wScreenBringInside(scr, &x, &y, wwin->frame->core->width, wwin->frame->core->height);
1738 if (move)
1739 wWindowConfigure(wwin, x, y, wwin->client.width, wwin->client.height);
1742 void wWindowFocusPrev(WWindow *wwin, Bool inSameWorkspace)
1744 WWindow *tmp;
1746 if (!wwin || !wwin->prev)
1747 return;
1749 tmp = wwin;
1750 while (tmp->prev)
1751 tmp = tmp->prev;
1753 if (inSameWorkspace)
1754 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1755 tmp = tmp->next;
1757 wWindowSingleFocus(tmp);
1760 void wWindowFocusNext(WWindow *wwin, Bool inSameWorkspace)
1762 WWindow *tmp;
1764 if (!wwin || !wwin->prev)
1765 return;
1767 tmp = wwin->prev;
1768 if (inSameWorkspace)
1769 while (tmp && (tmp->frame->workspace != wwin->frame->workspace))
1770 tmp = tmp->prev;
1772 wWindowSingleFocus(tmp);
1775 void wWindowFocus(WWindow *wwin, WWindow *owin)
1777 WWindow *nowner;
1778 WWindow *oowner;
1780 #ifdef KEEP_XKB_LOCK_STATUS
1781 if (wPreferences.modelock)
1782 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1783 #endif /* KEEP_XKB_LOCK_STATUS */
1785 wwin->flags.semi_focused = 0;
1787 if (wwin->flags.is_gnustep == 0)
1788 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1790 wwin->flags.focused = 1;
1792 wWindowResetMouseGrabs(wwin);
1794 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1796 if (owin == wwin || !owin)
1797 return;
1799 nowner = wWindowFor(wwin->transient_for);
1801 /* new window is a transient for the old window */
1802 if (nowner == owin) {
1803 owin->flags.semi_focused = 1;
1804 wWindowUnfocus(nowner);
1805 return;
1808 oowner = wWindowFor(owin->transient_for);
1810 /* new window is owner of old window */
1811 if (wwin == oowner) {
1812 wWindowUnfocus(owin);
1813 return;
1816 if (!nowner) {
1817 wWindowUnfocus(owin);
1818 return;
1821 /* new window has same owner of old window */
1822 if (oowner == nowner) {
1823 /* prevent unfocusing of owner */
1824 oowner->flags.semi_focused = 0;
1825 wWindowUnfocus(owin);
1826 oowner->flags.semi_focused = 1;
1828 return;
1831 /* nowner != NULL && oowner != nowner */
1832 nowner->flags.semi_focused = 1;
1833 wWindowUnfocus(nowner);
1834 wWindowUnfocus(owin);
1837 void wWindowUnfocus(WWindow *wwin)
1839 CloseWindowMenu(wwin->screen_ptr);
1841 if (wwin->flags.is_gnustep == 0)
1842 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1844 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1845 WWindow *owner;
1846 owner = wWindowFor(wwin->transient_for);
1847 if (owner && owner->flags.semi_focused) {
1848 owner->flags.semi_focused = 0;
1849 if (owner->flags.mapped || owner->flags.shaded) {
1850 wWindowUnfocus(owner);
1851 wFrameWindowPaint(owner->frame);
1855 wwin->flags.focused = 0;
1856 wWindowResetMouseGrabs(wwin);
1857 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1860 void wWindowUpdateName(WWindow *wwin, const char *newTitle)
1862 const char *title;
1864 if (!wwin->frame)
1865 return;
1867 if (!newTitle)
1868 title = DEF_WINDOW_TITLE; /* the hint was removed */
1869 else
1870 title = newTitle;
1872 if (wFrameWindowChangeTitle(wwin->frame, title))
1873 WMPostNotificationName(WMNChangedName, wwin, NULL);
1877 *----------------------------------------------------------------------
1879 * wWindowConstrainSize--
1880 * Constrains size for the client window, taking the maximal size,
1881 * window resize increments and other size hints into account.
1883 * Returns:
1884 * The closest size to what was given that the client window can
1885 * have.
1887 *----------------------------------------------------------------------
1889 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1891 int width = (int)*nwidth;
1892 int height = (int)*nheight;
1893 int winc = 1;
1894 int hinc = 1;
1895 int minW = 1, minH = 1;
1896 int maxW = wwin->screen_ptr->scr_width * 2;
1897 int maxH = wwin->screen_ptr->scr_height * 2;
1898 int minAX = -1, minAY = -1;
1899 int maxAX = -1, maxAY = -1;
1900 int baseW = 0;
1901 int baseH = 0;
1904 * X11 proto defines width and height as a CARD16
1905 * if window size is guaranteed to fail, failsafe to a reasonable size
1907 if (width > USHRT_MAX && height > USHRT_MAX) {
1908 width = 640;
1909 height = 480;
1910 return;
1913 if (wwin->normal_hints) {
1914 if (!wwin->flags.maximized) {
1915 winc = wwin->normal_hints->width_inc;
1916 hinc = wwin->normal_hints->height_inc;
1918 minW = wwin->normal_hints->min_width;
1919 minH = wwin->normal_hints->min_height;
1920 maxW = wwin->normal_hints->max_width;
1921 maxH = wwin->normal_hints->max_height;
1922 if (wwin->normal_hints->flags & PAspect) {
1923 minAX = wwin->normal_hints->min_aspect.x;
1924 minAY = wwin->normal_hints->min_aspect.y;
1925 maxAX = wwin->normal_hints->max_aspect.x;
1926 maxAY = wwin->normal_hints->max_aspect.y;
1929 baseW = wwin->normal_hints->base_width;
1930 baseH = wwin->normal_hints->base_height;
1933 /* trust the mins provided by the client but not the maxs */
1934 if (width < minW)
1935 width = minW;
1936 if (height < minH)
1937 height = minH;
1939 /* if only one dimension is over the top, set a default 4/3 ratio */
1940 if (width > maxW && height < maxH) {
1941 width = height * 4 / 3;
1942 } else {
1943 if(height > maxH && width < maxW)
1944 height = width * 3 / 4;
1947 /* aspect ratio code borrowed from olwm */
1948 if (minAX > 0) {
1949 /* adjust max aspect ratio */
1950 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1951 if (maxAX > maxAY) {
1952 height = (width * maxAY) / maxAX;
1953 if (height > maxH) {
1954 height = maxH;
1955 width = (height * maxAX) / maxAY;
1957 } else {
1958 width = (height * maxAX) / maxAY;
1959 if (width > maxW) {
1960 width = maxW;
1961 height = (width * maxAY) / maxAX;
1966 /* adjust min aspect ratio */
1967 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1968 if (minAX > minAY) {
1969 height = (width * minAY) / minAX;
1970 if (height < minH) {
1971 height = minH;
1972 width = (height * minAX) / minAY;
1974 } else {
1975 width = (height * minAX) / minAY;
1976 if (width < minW) {
1977 width = minW;
1978 height = (width * minAY) / minAX;
1984 if (!wwin->flags.maximized) {
1985 if (baseW != 0)
1986 width = (((width - baseW) / winc) * winc) + baseW;
1987 else
1988 width = (((width - minW) / winc) * winc) + minW;
1990 if (baseH != 0)
1991 height = (((height - baseH) / hinc) * hinc) + baseH;
1992 else
1993 height = (((height - minH) / hinc) * hinc) + minH;
1996 /* broken stupid apps may cause preposterous values for these.. */
1997 if (width > 0)
1998 *nwidth = width;
1999 if (height > 0)
2000 *nheight = height;
2003 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
2004 unsigned int *width, unsigned int *height)
2006 int baseW = 0, baseH = 0;
2007 int winc = 1, hinc = 1;
2009 if (wwin->normal_hints) {
2010 baseW = wwin->normal_hints->base_width;
2011 baseH = wwin->normal_hints->base_height;
2013 winc = wwin->normal_hints->width_inc;
2014 hinc = wwin->normal_hints->height_inc;
2017 if (*width > maxW)
2018 *width = maxW - (maxW - baseW) % winc;
2020 if (*height > maxH)
2021 *height = maxH - (maxH - baseH) % hinc;
2024 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
2026 WScreen *scr = wwin->screen_ptr;
2027 WApplication *wapp;
2028 int unmap = 0;
2030 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
2031 return;
2033 if (workspace != scr->current_workspace) {
2034 /* Sent to other workspace. Unmap window */
2035 if ((wwin->flags.mapped
2036 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2037 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2039 wapp = wApplicationOf(wwin->main_window);
2040 if (wapp)
2041 wapp->last_workspace = workspace;
2043 if (wwin->flags.miniaturized) {
2044 if (wwin->icon) {
2045 XUnmapWindow(dpy, wwin->icon->core->window);
2046 wwin->icon->mapped = 0;
2048 } else {
2049 unmap = 1;
2050 wSetFocusTo(scr, NULL);
2053 } else {
2054 /* brought to current workspace. Map window */
2055 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2056 if (wwin->icon) {
2057 XMapWindow(dpy, wwin->icon->core->window);
2058 wwin->icon->mapped = 1;
2060 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2061 wWindowMap(wwin);
2064 if (!IS_OMNIPRESENT(wwin)) {
2065 int oldWorkspace = wwin->frame->workspace;
2066 wwin->frame->workspace = workspace;
2067 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
2070 if (unmap)
2071 wWindowUnmap(wwin);
2074 void wWindowChangeWorkspaceRelative(WWindow *wwin, int amount)
2076 WScreen *scr = wwin->screen_ptr;
2077 int w = scr->current_workspace + amount;
2079 if (amount < 0) {
2080 if (w >= 0) {
2081 wWindowChangeWorkspace(wwin, w);
2082 } else if (wPreferences.ws_cycle) {
2083 wWindowChangeWorkspace(wwin, scr->workspace_count + w);
2085 } else if (amount > 0) {
2086 if (w < scr->workspace_count) {
2087 wWindowChangeWorkspace(wwin, w);
2088 } else if (wPreferences.ws_advance) {
2089 int workspace = WMIN(w, MAX_WORKSPACES - 1);
2090 wWorkspaceMake(scr, workspace);
2091 wWindowChangeWorkspace(wwin, workspace);
2092 } else if (wPreferences.ws_cycle) {
2093 wWindowChangeWorkspace(wwin, w % scr->workspace_count);
2098 void wWindowSynthConfigureNotify(WWindow *wwin)
2100 XEvent sevent;
2102 sevent.type = ConfigureNotify;
2103 sevent.xconfigure.display = dpy;
2104 sevent.xconfigure.event = wwin->client_win;
2105 sevent.xconfigure.window = wwin->client_win;
2107 sevent.xconfigure.x = wwin->client.x;
2108 sevent.xconfigure.y = wwin->client.y;
2109 sevent.xconfigure.width = wwin->client.width;
2110 sevent.xconfigure.height = wwin->client.height;
2112 sevent.xconfigure.border_width = wwin->old_border_width;
2113 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2114 sevent.xconfigure.above = wwin->frame->titlebar->window;
2115 else
2116 sevent.xconfigure.above = None;
2118 sevent.xconfigure.override_redirect = False;
2119 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2120 XFlush(dpy);
2124 *----------------------------------------------------------------------
2125 * wWindowConfigure()
2127 * req_x, req_y: new requested positions for the frame
2128 * req_width, req_height: new requested sizes for the client
2130 * Configures the frame, decorations and client window to the specified
2131 * geometry, whose validity is not checked -- wWindowConstrainSize()
2132 * must be used for that.
2133 * The size parameters are for the client window, but the position is
2134 * for the frame.
2135 * The client window receives a ConfigureNotify event, according
2136 * to what ICCCM says.
2138 * Returns:
2139 * None
2141 * Side effects:
2142 * Window size and position are changed and client window receives
2143 * a ConfigureNotify event.
2144 *----------------------------------------------------------------------
2146 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2148 int synth_notify = False;
2149 int resize;
2151 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2153 * if the window is being moved but not resized then
2154 * send a synthetic ConfigureNotify
2156 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2157 synth_notify = True;
2159 if (WFLAGP(wwin, dont_move_off))
2160 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2162 if (resize) {
2163 if (req_width < MIN_WINDOW_SIZE)
2164 req_width = MIN_WINDOW_SIZE;
2165 if (req_height < MIN_WINDOW_SIZE)
2166 req_height = MIN_WINDOW_SIZE;
2168 /* If growing, resize inner part before frame,
2169 * if shrinking, resize frame before.
2170 * This will prevent the frame (that can have a different color)
2171 * to be exposed, causing flicker */
2172 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2173 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2175 if (wwin->flags.shaded) {
2176 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2177 wwin->old_geometry.height = req_height;
2178 } else {
2179 int h;
2181 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2183 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2186 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2187 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2189 wwin->client.x = req_x;
2190 wwin->client.y = req_y + wwin->frame->top_width;
2191 wwin->client.width = req_width;
2192 wwin->client.height = req_height;
2193 } else {
2194 wwin->client.x = req_x;
2195 wwin->client.y = req_y + wwin->frame->top_width;
2197 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2199 wwin->frame_x = req_x;
2200 wwin->frame_y = req_y;
2201 if (HAS_BORDER(wwin)) {
2202 wwin->client.x += wwin->screen_ptr->frame_border_width;
2203 wwin->client.y += wwin->screen_ptr->frame_border_width;
2205 #ifdef USE_XSHAPE
2206 if (w_global.xext.shape.supported && wwin->flags.shaped && resize)
2207 wWindowSetShape(wwin);
2208 #endif
2210 if (synth_notify)
2211 wWindowSynthConfigureNotify(wwin);
2213 wNETFrameExtents(wwin);
2215 XFlush(dpy);
2218 /* req_x, req_y: new position of the frame */
2219 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2221 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2222 int synth_notify = False;
2224 /* Send a synthetic ConfigureNotify event for every window movement. */
2225 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2226 synth_notify = True;
2227 #else
2228 /* A single synthetic ConfigureNotify event is sent at the end of
2229 * a completed (opaque) movement in moveres.c */
2230 #endif
2232 if (WFLAGP(wwin, dont_move_off))
2233 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2234 wwin->frame->core->width, wwin->frame->core->height);
2236 wwin->client.x = req_x;
2237 wwin->client.y = req_y + wwin->frame->top_width;
2238 if (HAS_BORDER(wwin)) {
2239 wwin->client.x += wwin->screen_ptr->frame_border_width;
2240 wwin->client.y += wwin->screen_ptr->frame_border_width;
2243 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2245 wwin->frame_x = req_x;
2246 wwin->frame_y = req_y;
2248 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2249 if (synth_notify)
2250 wWindowSynthConfigureNotify(wwin);
2251 #endif
2254 void wWindowUpdateButtonImages(WWindow *wwin)
2256 WScreen *scr = wwin->screen_ptr;
2257 Pixmap pixmap, mask;
2258 WFrameWindow *fwin = wwin->frame;
2260 if (!HAS_TITLEBAR(wwin))
2261 return;
2263 /* miniaturize button */
2264 if (!WFLAGP(wwin, no_miniaturize_button)) {
2265 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2266 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2268 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2269 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2270 else
2271 mask = None;
2273 if (fwin->lbutton_image
2274 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2275 wPixmapDestroy(fwin->lbutton_image);
2276 fwin->lbutton_image = NULL;
2279 if (!fwin->lbutton_image) {
2280 fwin->lbutton_image = wPixmapCreate(pixmap, mask);
2281 fwin->lbutton_image->client_owned = 1;
2282 fwin->lbutton_image->client_owned_mask = 1;
2284 } else {
2285 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2286 wPixmapDestroy(fwin->lbutton_image);
2288 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2291 #ifdef XKB_BUTTON_HINT
2292 if (!WFLAGP(wwin, no_language_button)) {
2293 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2294 wPixmapDestroy(fwin->languagebutton_image);
2296 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2298 #endif
2300 /* close button */
2302 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2303 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2305 if (!WFLAGP(wwin, no_close_button)) {
2306 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2307 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2309 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2310 mask = wwin->wm_gnustep_attr->close_mask;
2311 else
2312 mask = None;
2314 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2315 || fwin->rbutton_image->mask != mask)) {
2316 wPixmapDestroy(fwin->rbutton_image);
2317 fwin->rbutton_image = NULL;
2320 if (!fwin->rbutton_image) {
2321 fwin->rbutton_image = wPixmapCreate(pixmap, mask);
2322 fwin->rbutton_image->client_owned = 1;
2323 fwin->rbutton_image->client_owned_mask = 1;
2326 } else if (WFLAGP(wwin, kill_close)) {
2327 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2328 wPixmapDestroy(fwin->rbutton_image);
2330 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2332 } else if (MGFLAGP(wwin, broken_close)) {
2333 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2334 wPixmapDestroy(fwin->rbutton_image);
2336 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2338 } else {
2339 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2340 wPixmapDestroy(fwin->rbutton_image);
2342 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2346 /* force buttons to be redrawn */
2347 fwin->flags.need_texture_change = 1;
2348 wFrameWindowPaint(fwin);
2352 *---------------------------------------------------------------------------
2353 * wWindowConfigureBorders--
2354 * Update window border configuration according to attribute flags.
2356 *---------------------------------------------------------------------------
2358 void wWindowConfigureBorders(WWindow *wwin)
2360 if (wwin->frame) {
2361 int flags;
2362 int newy, oldh;
2364 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2366 #ifdef XKB_BUTTON_HINT
2367 if (wPreferences.modelock)
2368 flags |= WFF_LANGUAGE_BUTTON;
2369 #endif
2371 if (HAS_TITLEBAR(wwin))
2372 flags |= WFF_TITLEBAR;
2373 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2374 flags |= WFF_RESIZEBAR;
2375 if (HAS_BORDER(wwin))
2376 flags |= WFF_BORDER;
2377 if (wwin->flags.shaded)
2378 flags |= WFF_IS_SHADED;
2379 if (wwin->flags.selected)
2380 flags |= WFF_SELECTED;
2382 oldh = wwin->frame->top_width;
2383 wFrameWindowUpdateBorders(wwin->frame, flags);
2384 if (oldh != wwin->frame->top_width) {
2385 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2387 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2388 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2391 flags = 0;
2392 if (!WFLAGP(wwin, no_miniaturize_button)
2393 && wwin->frame->flags.hide_left_button)
2394 flags |= WFF_LEFT_BUTTON;
2396 #ifdef XKB_BUTTON_HINT
2397 if (!WFLAGP(wwin, no_language_button)
2398 && wwin->frame->flags.hide_language_button)
2399 flags |= WFF_LANGUAGE_BUTTON;
2400 #endif
2402 if (!WFLAGP(wwin, no_close_button)
2403 && wwin->frame->flags.hide_right_button)
2404 flags |= WFF_RIGHT_BUTTON;
2406 if (flags != 0) {
2407 wWindowUpdateButtonImages(wwin);
2408 wFrameWindowShowButton(wwin->frame, flags);
2411 flags = 0;
2412 if (WFLAGP(wwin, no_miniaturize_button)
2413 && !wwin->frame->flags.hide_left_button)
2414 flags |= WFF_LEFT_BUTTON;
2416 #ifdef XKB_BUTTON_HINT
2417 if (WFLAGP(wwin, no_language_button)
2418 && !wwin->frame->flags.hide_language_button)
2419 flags |= WFF_LANGUAGE_BUTTON;
2420 #endif
2422 if (WFLAGP(wwin, no_close_button)
2423 && !wwin->frame->flags.hide_right_button)
2424 flags |= WFF_RIGHT_BUTTON;
2426 if (flags != 0)
2427 wFrameWindowHideButton(wwin->frame, flags);
2429 #ifdef USE_XSHAPE
2430 if (w_global.xext.shape.supported && wwin->flags.shaped)
2431 wWindowSetShape(wwin);
2432 #endif
2436 void wWindowSaveState(WWindow *wwin)
2438 long data[10];
2439 int i;
2441 memset(data, 0, sizeof(long) * 10);
2442 data[0] = wwin->frame->workspace;
2443 data[1] = wwin->flags.miniaturized;
2444 data[2] = wwin->flags.shaded;
2445 data[3] = wwin->flags.hidden;
2446 data[4] = wwin->flags.maximized;
2447 if (wwin->flags.maximized == 0) {
2448 data[5] = wwin->frame_x;
2449 data[6] = wwin->frame_y;
2450 data[7] = wwin->frame->core->width;
2451 data[8] = wwin->frame->core->height;
2452 } else {
2453 data[5] = wwin->old_geometry.x;
2454 data[6] = wwin->old_geometry.y;
2455 data[7] = wwin->old_geometry.width;
2456 data[8] = wwin->old_geometry.height;
2459 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2460 if (wwin->screen_ptr->shortcutWindows[i] &&
2461 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2462 data[9] |= 1 << i;
2465 XChangeProperty(dpy, wwin->client_win, w_global.atom.wmaker.state,
2466 w_global.atom.wmaker.state, 32, PropModeReplace, (unsigned char *)data, 10);
2469 static int getSavedState(Window window, WSavedState ** state)
2471 Atom type_ret;
2472 int fmt_ret;
2473 unsigned long nitems_ret;
2474 unsigned long bytes_after_ret;
2475 long *data;
2477 if (XGetWindowProperty(dpy, window, w_global.atom.wmaker.state, 0, 10,
2478 True, w_global.atom.wmaker.state,
2479 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2480 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2481 return 0;
2483 if (type_ret != w_global.atom.wmaker.state) {
2484 XFree(data);
2485 return 0;
2488 *state = wmalloc(sizeof(WSavedState));
2490 (*state)->workspace = data[0];
2491 (*state)->miniaturized = data[1];
2492 (*state)->shaded = data[2];
2493 (*state)->hidden = data[3];
2494 (*state)->maximized = data[4];
2495 (*state)->x = data[5];
2496 (*state)->y = data[6];
2497 (*state)->w = data[7];
2498 (*state)->h = data[8];
2499 (*state)->window_shortcuts = data[9];
2501 XFree(data);
2503 return 1;
2506 #ifdef USE_XSHAPE
2507 void wWindowClearShape(WWindow * wwin)
2509 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2510 0, wwin->frame->top_width, None, ShapeSet);
2511 XFlush(dpy);
2514 void wWindowSetShape(WWindow * wwin)
2516 XRectangle rect[2];
2517 int count;
2518 #ifdef OPTIMIZE_SHAPE
2519 XRectangle *rects;
2520 XRectangle *urec;
2521 int ordering;
2523 /* only shape is the client's */
2524 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2525 goto alt_code;
2527 /* Get array of rectangles describing the shape mask */
2528 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2529 if (!rects)
2530 goto alt_code;
2532 urec = malloc(sizeof(XRectangle) * (count + 2));
2533 if (!urec) {
2534 XFree(rects);
2535 goto alt_code;
2538 /* insert our decoration rectangles in the rect list */
2539 memcpy(urec, rects, sizeof(XRectangle) * count);
2540 XFree(rects);
2542 if (HAS_TITLEBAR(wwin)) {
2543 urec[count].x = -1;
2544 urec[count].y = -1 - wwin->frame->top_width;
2545 urec[count].width = wwin->frame->core->width + 2;
2546 urec[count].height = wwin->frame->top_width + 1;
2547 count++;
2549 if (HAS_RESIZEBAR(wwin)) {
2550 urec[count].x = -1;
2551 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2552 urec[count].width = wwin->frame->core->width + 2;
2553 urec[count].height = wwin->frame->bottom_width + 1;
2554 count++;
2557 /* shape our frame window */
2558 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2559 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2560 XFlush(dpy);
2561 wfree(urec);
2562 return;
2564 alt_code:
2565 #endif /* OPTIMIZE_SHAPE */
2566 count = 0;
2567 if (HAS_TITLEBAR(wwin)) {
2568 rect[count].x = -1;
2569 rect[count].y = -1;
2570 rect[count].width = wwin->frame->core->width + 2;
2571 rect[count].height = wwin->frame->top_width + 1;
2572 count++;
2574 if (HAS_RESIZEBAR(wwin)) {
2575 rect[count].x = -1;
2576 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2577 rect[count].width = wwin->frame->core->width + 2;
2578 rect[count].height = wwin->frame->bottom_width + 1;
2579 count++;
2581 if (count > 0) {
2582 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2583 0, 0, rect, count, ShapeSet, Unsorted);
2585 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2586 0, wwin->frame->top_width, wwin->client_win,
2587 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2588 XFlush(dpy);
2590 #endif /* USE_XSHAPE */
2592 /* ====================================================================== */
2594 static FocusMode getFocusMode(WWindow * wwin)
2596 FocusMode mode;
2598 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2599 if (wwin->wm_hints->input == True) {
2600 if (wwin->protocols.TAKE_FOCUS)
2601 mode = WFM_LOCALLY_ACTIVE;
2602 else
2603 mode = WFM_PASSIVE;
2604 } else {
2605 if (wwin->protocols.TAKE_FOCUS)
2606 mode = WFM_GLOBALLY_ACTIVE;
2607 else
2608 mode = WFM_NO_INPUT;
2610 } else {
2611 mode = WFM_PASSIVE;
2613 return mode;
2616 void wWindowSetKeyGrabs(WWindow * wwin)
2618 int i;
2619 WShortKey *key;
2621 for (i = 0; i < WKBD_LAST; i++) {
2622 key = &wKeyBindings[i];
2624 if (key->keycode == 0)
2625 continue;
2626 if (key->modifier != AnyModifier) {
2627 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2628 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2629 #ifdef NUMLOCK_HACK
2630 /* Also grab all modifier combinations possible that include,
2631 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2632 * work even if the NumLock/ScrollLock key is on.
2634 wHackedGrabKey(key->keycode, key->modifier,
2635 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2636 #endif
2638 XGrabKey(dpy, key->keycode, key->modifier,
2639 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2642 wRootMenuBindShortcuts(wwin->frame->core->window);
2645 void wWindowResetMouseGrabs(WWindow * wwin)
2647 /* Mouse grabs can't be done on the client window because of
2648 * ICCCM and because clients that try to do the same will crash.
2650 * But there is a problem which makes tbar buttons of unfocused
2651 * windows not usable as the click goes to the frame window instead
2652 * of the button itself. Must figure a way to fix that.
2655 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2657 if (!WFLAGP(wwin, no_bind_mouse)) {
2658 /* grabs for Meta+drag */
2659 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2660 True, ButtonPressMask | ButtonReleaseMask,
2661 GrabModeSync, GrabModeAsync, None, None);
2663 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2664 * but we only grab it for Button4 and Button 5 since a lot of apps
2665 * use CTRL+Button1-3 for app related functionality
2667 if (wPreferences.resize_increment > 0) {
2668 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2669 True, ButtonPressMask | ButtonReleaseMask,
2670 GrabModeSync, GrabModeAsync, None, None);
2671 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2672 True, ButtonPressMask | ButtonReleaseMask,
2673 GrabModeSync, GrabModeAsync, None, None);
2675 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2676 True, ButtonPressMask | ButtonReleaseMask,
2677 GrabModeSync, GrabModeAsync, None, None);
2678 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2679 True, ButtonPressMask | ButtonReleaseMask,
2680 GrabModeSync, GrabModeAsync, None, None);
2684 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2685 && !wwin->flags.is_gnustep) {
2686 /* the passive grabs to focus the window */
2687 /* if (wPreferences.focus_mode == WKF_CLICK) */
2688 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2689 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2691 XFlush(dpy);
2694 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2696 if (attr->flags & GSExtraFlagsAttr) {
2697 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2698 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2699 wWindowUpdateButtonImages(wwin);
2704 WMagicNumber wWindowAddSavedState(const char *instance, const char *class,
2705 const char *command, pid_t pid, WSavedState *state)
2707 WWindowState *wstate;
2709 wstate = malloc(sizeof(WWindowState));
2710 if (!wstate)
2711 return NULL;
2713 memset(wstate, 0, sizeof(WWindowState));
2714 wstate->pid = pid;
2715 if (instance)
2716 wstate->instance = wstrdup(instance);
2717 if (class)
2718 wstate->class = wstrdup(class);
2719 if (command)
2720 wstate->command = wstrdup(command);
2721 wstate->state = state;
2723 wstate->next = windowState;
2724 windowState = wstate;
2726 return wstate;
2729 static inline int is_same(const char *x, const char *y)
2731 if ((x == NULL) && (y == NULL))
2732 return 1;
2734 if ((x == NULL) || (y == NULL))
2735 return 0;
2737 if (strcmp(x, y) == 0)
2738 return 1;
2739 else
2740 return 0;
2743 WMagicNumber wWindowGetSavedState(Window win)
2745 char *instance, *class, *command = NULL;
2746 WWindowState *wstate = windowState;
2748 if (!wstate)
2749 return NULL;
2751 command = GetCommandForWindow(win);
2752 if (!command)
2753 return NULL;
2755 if (PropGetWMClass(win, &class, &instance)) {
2756 while (wstate) {
2757 if (is_same(instance, wstate->instance) &&
2758 is_same(class, wstate->class) &&
2759 is_same(command, wstate->command)) {
2760 break;
2762 wstate = wstate->next;
2764 } else {
2765 wstate = NULL;
2768 if (command)
2769 wfree(command);
2770 if (instance)
2771 free(instance);
2772 if (class)
2773 free(class);
2775 return wstate;
2778 void wWindowDeleteSavedState(WMagicNumber id)
2780 WWindowState *tmp, *wstate = (WWindowState *) id;
2782 if (!wstate || !windowState)
2783 return;
2785 tmp = windowState;
2786 if (tmp == wstate) {
2787 windowState = wstate->next;
2788 release_wwindowstate(wstate);
2789 } else {
2790 while (tmp->next) {
2791 if (tmp->next == wstate) {
2792 tmp->next = wstate->next;
2793 release_wwindowstate(wstate);
2794 break;
2796 tmp = tmp->next;
2801 void wWindowDeleteSavedStatesForPID(pid_t pid)
2803 WWindowState *tmp, *wstate;
2805 if (!windowState)
2806 return;
2808 tmp = windowState;
2809 if (tmp->pid == pid) {
2810 wstate = windowState;
2811 windowState = tmp->next;
2813 release_wwindowstate(wstate);
2814 } else {
2815 while (tmp->next) {
2816 if (tmp->next->pid == pid) {
2817 wstate = tmp->next;
2818 tmp->next = wstate->next;
2819 release_wwindowstate(wstate);
2820 break;
2822 tmp = tmp->next;
2827 static void release_wwindowstate(WWindowState *wstate)
2829 if (wstate->instance)
2830 wfree(wstate->instance);
2832 if (wstate->class)
2833 wfree(wstate->class);
2835 if (wstate->command)
2836 wfree(wstate->command);
2838 wfree(wstate->state);
2839 wfree(wstate);
2842 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2844 if (wwin->flags.omnipresent == flag)
2845 return;
2847 wwin->flags.omnipresent = flag;
2848 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2851 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2853 WWindow *wwin = data;
2855 /* Parameter not used, but tell the compiler that it is ok */
2856 (void) sender;
2858 #ifndef NUMLOCK_HACK
2859 if ((event->xbutton.state & ValidModMask)
2860 != (event->xbutton.state & ~LockMask)) {
2861 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2862 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2864 #endif
2866 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2868 CloseWindowMenu(wwin->screen_ptr);
2870 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2871 && !WFLAGP(wwin, no_focusable)) {
2872 wSetFocusTo(wwin->screen_ptr, wwin);
2875 if (event->xbutton.button == Button1)
2876 wRaiseFrame(wwin->frame->core);
2878 if (event->xbutton.window != wwin->frame->resizebar->window) {
2879 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2880 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2881 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2882 return;
2886 if (event->xbutton.state & MOD_MASK) {
2887 /* move the window */
2888 wMouseMoveWindow(wwin, event);
2889 XUngrabPointer(dpy, CurrentTime);
2890 } else {
2891 wMouseResizeWindow(wwin, event);
2892 XUngrabPointer(dpy, CurrentTime);
2896 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2898 WWindow *wwin = data;
2900 /* Parameter not used, but tell the compiler that it is ok */
2901 (void) sender;
2903 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2905 if (event->xbutton.button == Button1 ) {
2906 if (event->xbutton.state == 0 ) {
2907 if (!WFLAGP(wwin, no_shadeable) & !wPreferences.double_click_fullscreen) {
2908 /* shade window */
2909 if (wwin->flags.shaded)
2910 wUnshadeWindow(wwin);
2911 else
2912 wShadeWindow(wwin);
2916 if (wPreferences.double_click_fullscreen){
2917 int dir = 0;
2918 if (event->xbutton.state == 0) {
2919 /* maximize window full screen*/
2920 dir |= (MAX_VERTICAL|MAX_HORIZONTAL);
2921 int ndir = dir ^ wwin->flags.maximized;
2922 wMaximizeWindow(wwin, ndir, wGetHeadForWindow(wwin));
2927 } else {
2928 int dir = 0;
2930 if (event->xbutton.state & ControlMask)
2931 dir |= MAX_VERTICAL;
2933 if (event->xbutton.state & ShiftMask) {
2934 dir |= MAX_HORIZONTAL;
2935 if (!(event->xbutton.state & ControlMask))
2936 wSelectWindow(wwin, !wwin->flags.selected);
2939 /* maximize window */
2940 if (dir != 0 && IS_RESIZABLE(wwin)) {
2941 int ndir = dir ^ wwin->flags.maximized;
2943 if (ndir != 0)
2944 wMaximizeWindow(wwin, ndir, wGetHeadForWindow(wwin));
2945 else
2946 wUnmaximizeWindow(wwin);
2949 } else if (event->xbutton.button == Button3) {
2950 if (event->xbutton.state & MOD_MASK)
2951 wHideOtherApplications(wwin);
2952 } else if (event->xbutton.button == Button2) {
2953 wSelectWindow(wwin, !wwin->flags.selected);
2954 } else if (event->xbutton.button == W_getconf_mouseWheelUp()) {
2955 wShadeWindow(wwin);
2956 } else if (event->xbutton.button == W_getconf_mouseWheelDown()) {
2957 wUnshadeWindow(wwin);
2961 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2963 WWindow *wwin = desc->parent;
2964 unsigned int new_width, w_scale;
2965 unsigned int new_height, h_scale;
2966 unsigned int resize_width_increment = 0;
2967 unsigned int resize_height_increment = 0;
2969 if (wwin->normal_hints) {
2970 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2971 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2972 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2973 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2975 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2976 resize_width_increment = wPreferences.resize_increment;
2977 resize_height_increment = wPreferences.resize_increment;
2980 event->xbutton.state &= w_global.shortcut.modifiers_mask;
2982 CloseWindowMenu(wwin->screen_ptr);
2984 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2985 wSetFocusTo(wwin->screen_ptr, wwin);
2987 if (event->xbutton.button == Button1)
2988 wRaiseFrame(wwin->frame->core);
2990 if (event->xbutton.state & ControlMask) {
2991 if (event->xbutton.button == Button4) {
2992 new_width = wwin->client.width - resize_width_increment;
2993 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2994 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2996 if (event->xbutton.button == Button5) {
2997 new_width = wwin->client.width + resize_width_increment;
2998 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2999 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
3003 if (event->xbutton.state & MOD_MASK) {
3004 /* move the window */
3005 if (XGrabPointer(dpy, wwin->client_win, False,
3006 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3007 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3008 return;
3010 if (event->xbutton.button == Button3) {
3011 wMouseResizeWindow(wwin, event);
3012 } else if (event->xbutton.button == Button4) {
3013 new_height = wwin->client.height - resize_height_increment;
3014 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
3015 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
3016 } else if (event->xbutton.button == Button5) {
3017 new_height = wwin->client.height + resize_height_increment;
3018 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
3019 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
3020 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
3021 wMouseMoveWindow(wwin, event);
3023 XUngrabPointer(dpy, CurrentTime);
3027 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3029 WWindow *wwin = (WWindow *) data;
3031 /* Parameter not used, but tell the compiler that it is ok */
3032 (void) sender;
3034 #ifndef NUMLOCK_HACK
3035 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
3036 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
3037 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3038 #endif
3039 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3041 CloseWindowMenu(wwin->screen_ptr);
3043 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
3044 && !WFLAGP(wwin, no_focusable))
3045 wSetFocusTo(wwin->screen_ptr, wwin);
3047 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
3049 if (event->xbutton.button == Button1) {
3050 if (event->xbutton.state & MOD_MASK)
3051 wLowerFrame(wwin->frame->core);
3052 else
3053 wRaiseFrame(wwin->frame->core);
3055 if ((event->xbutton.state & ShiftMask)
3056 && !(event->xbutton.state & ControlMask)) {
3057 wSelectWindow(wwin, !wwin->flags.selected);
3058 return;
3060 if (event->xbutton.window != wwin->frame->titlebar->window
3061 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3062 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3063 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3064 return;
3067 /* move the window */
3068 wMouseMoveWindow(wwin, event);
3070 XUngrabPointer(dpy, CurrentTime);
3071 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
3072 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
3073 WObjDescriptor *desc;
3075 if (event->xbutton.window != wwin->frame->titlebar->window
3076 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3077 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3078 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3079 return;
3082 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
3084 /* allow drag select */
3085 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3086 event->xany.send_event = True;
3087 (*desc->handle_mousedown) (desc, event);
3089 XUngrabPointer(dpy, CurrentTime);
3093 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3095 WWindow *wwin = data;
3097 /* Parameter not used, but tell the compiler that it is ok */
3098 (void) sender;
3100 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3102 CloseWindowMenu(wwin->screen_ptr);
3104 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3105 return;
3107 /* if control-click, kill the client */
3108 if (event->xbutton.state & ControlMask) {
3109 wClientKill(wwin);
3110 } else {
3111 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3112 /* send delete message */
3113 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3114 w_global.timestamp.last_event);
3119 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3121 WWindow *wwin = data;
3123 /* Parameter not used, but tell the compiler that it is ok */
3124 (void) sender;
3126 CloseWindowMenu(wwin->screen_ptr);
3128 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3129 return;
3131 /* send delete message */
3132 if (wwin->protocols.DELETE_WINDOW)
3133 wClientSendProtocol(wwin, w_global.atom.wm.delete_window,
3134 w_global.timestamp.last_event);
3135 else
3136 wClientKill(wwin);
3139 #ifdef XKB_BUTTON_HINT
3140 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3142 WWindow *wwin = data;
3143 WFrameWindow *fwin = wwin->frame;
3144 WScreen *scr = fwin->screen_ptr;
3145 int tl;
3147 /* Parameter not used, but tell the compiler that it is ok */
3148 (void) sender;
3150 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3151 return;
3152 tl = wwin->frame->languagemode;
3153 wwin->frame->languagemode = wwin->frame->last_languagemode;
3154 wwin->frame->last_languagemode = tl;
3155 wSetFocusTo(scr, wwin);
3156 wwin->frame->languagebutton_image =
3157 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3158 wFrameWindowUpdateLanguageButton(wwin->frame);
3159 if (event->xbutton.button == Button3)
3160 return;
3161 wRaiseFrame(fwin->core);
3163 #endif
3165 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3167 WWindow *wwin = data;
3169 /* Parameter not used, but tell the compiler that it is ok */
3170 (void) sender;
3172 event->xbutton.state &= w_global.shortcut.modifiers_mask;
3174 CloseWindowMenu(wwin->screen_ptr);
3176 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3177 return;
3179 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3180 wClientSendProtocol(wwin, w_global.atom.gnustep.wm_miniaturize_window,
3181 w_global.timestamp.last_event);
3182 } else {
3183 WApplication *wapp;
3184 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3186 wapp = wApplicationOf(wwin->main_window);
3187 if (wapp && !WFLAGP(wwin, no_appicon))
3188 wHideApplication(wapp);
3189 } else if (event->xbutton.state == 0) {
3190 wIconifyWindow(wwin);