Fixed regression when placing windows.
[wmaker-crm.git] / src / window.c
blob7f0a1feef81aa83bbcd9e1021d513e3ad3593497
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
29 #ifdef KEEP_XKB_LOCK_STATUS
30 #include <X11/XKBlib.h>
31 #endif /* KEEP_XKB_LOCK_STATUS */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <math.h>
38 /* For getting mouse wheel mappings from WINGs */
39 #include <WINGs/WINGsP.h>
41 #include "WindowMaker.h"
42 #include "GNUstep.h"
43 #include "wcore.h"
44 #include "framewin.h"
45 #include "texture.h"
46 #include "window.h"
47 #include "winspector.h"
48 #include "icon.h"
49 #include "properties.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "colormap.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
59 #include "appmenu.h"
60 #include "appicon.h"
61 #include "superfluous.h"
62 #include "rootmenu.h"
64 #ifdef MWM_HINTS
65 # include "motif.h"
66 #endif
67 #include "wmspec.h"
69 #define MOD_MASK wPreferences.modifier_mask
71 /****** Global Variables ******/
72 extern WShortKey wKeyBindings[WKBD_LAST];
74 #ifdef SHAPE
75 extern Bool wShapeSupported;
76 #endif
78 /* contexts */
79 extern XContext wWinContext;
81 /* protocol atoms */
82 extern Atom _XA_WM_DELETE_WINDOW;
83 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
84 extern Atom _XA_WINDOWMAKER_STATE;
85 extern WPreferences wPreferences;
86 extern Time LastTimestamp;
88 /***** Local Stuff *****/
89 static WWindowState *windowState = NULL;
90 static FocusMode getFocusMode(WWindow *wwin);
91 static int getSavedState(Window window, WSavedState **state);
92 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
94 /* frame window (during window grabs) */
95 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
97 /* close button */
98 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
99 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
101 /* iconify button */
102 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
104 #ifdef XKB_BUTTON_HINT
105 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
106 #endif
108 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
109 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
110 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
112 static void release_wwindowstate(WWindowState *wstate);
114 /****** Notification Observers ******/
116 static void appearanceObserver(void *self, WMNotification * notif)
118 WWindow *wwin = (WWindow *) self;
119 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
121 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
122 return;
124 if (flags & WFontSettings) {
125 wWindowConfigureBorders(wwin);
126 if (wwin->flags.shaded) {
127 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
128 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
129 wWindowSynthConfigureNotify(wwin);
132 if (flags & WTextureSettings)
133 wwin->frame->flags.need_texture_remake = 1;
135 if (flags & (WTextureSettings | WColorSettings)) {
136 if (wwin->frame->titlebar)
137 XClearWindow(dpy, wwin->frame->titlebar->window);
139 wFrameWindowPaint(wwin->frame);
144 /* Return the WWindow associated with a given (Xlib) Window. */
145 WWindow *wWindowFor(Window window)
147 WObjDescriptor *desc;
149 if (window == None)
150 return NULL;
152 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
153 return NULL;
155 if (desc->parent_type == WCLASS_WINDOW)
156 return desc->parent;
157 else if (desc->parent_type == WCLASS_FRAME) {
158 WFrameWindow *frame = (WFrameWindow *) desc->parent;
159 if (frame->flags.is_client_window_frame)
160 return frame->child;
163 return NULL;
166 WWindow *wWindowCreate(void)
168 WWindow *wwin;
170 wwin = wmalloc(sizeof(WWindow));
171 wretain(wwin);
173 wwin->client_descriptor.handle_mousedown = frameMouseDown;
174 wwin->client_descriptor.parent = wwin;
175 wwin->client_descriptor.self = wwin;
176 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
178 return wwin;
181 void wWindowDestroy(WWindow *wwin)
183 int i;
185 if (wwin->screen_ptr->cmap_window == wwin)
186 wwin->screen_ptr->cmap_window = NULL;
188 WMRemoveNotificationObserver(wwin);
190 wwin->flags.destroyed = 1;
192 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
193 if (!wwin->screen_ptr->shortcutWindows[i])
194 continue;
196 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
198 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
199 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
200 wwin->screen_ptr->shortcutWindows[i] = NULL;
204 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
205 wwin->fake_group->retainCount--;
206 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
207 XDestroyWindow(dpy, wwin->fake_group->leader);
208 wwin->fake_group->leader = None;
209 wwin->fake_group->origLeader = None;
210 XFlush(dpy);
214 if (wwin->normal_hints)
215 XFree(wwin->normal_hints);
217 if (wwin->wm_hints)
218 XFree(wwin->wm_hints);
220 if (wwin->wm_instance)
221 XFree(wwin->wm_instance);
223 if (wwin->wm_class)
224 XFree(wwin->wm_class);
226 if (wwin->wm_gnustep_attr)
227 wfree(wwin->wm_gnustep_attr);
229 if (wwin->cmap_windows)
230 XFree(wwin->cmap_windows);
232 XDeleteContext(dpy, wwin->client_win, wWinContext);
234 if (wwin->frame)
235 wFrameWindowDestroy(wwin->frame);
237 if (wwin->icon) {
238 RemoveFromStackList(wwin->icon->core);
239 wIconDestroy(wwin->icon);
240 if (wPreferences.auto_arrange_icons)
241 wArrangeIcons(wwin->screen_ptr, True);
243 if (wwin->net_icon_image)
244 RReleaseImage(wwin->net_icon_image);
246 wrelease(wwin);
249 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
251 if (gs_hints->flags & GSWindowStyleAttr) {
252 if (gs_hints->window_style == WMBorderlessWindowMask) {
253 wwin->client_flags.no_border = 1;
254 wwin->client_flags.no_titlebar = 1;
255 wwin->client_flags.no_closable = 1;
256 wwin->client_flags.no_miniaturizable = 1;
257 wwin->client_flags.no_resizable = 1;
258 wwin->client_flags.no_close_button = 1;
259 wwin->client_flags.no_miniaturize_button = 1;
260 wwin->client_flags.no_resizebar = 1;
261 } else {
262 wwin->client_flags.no_close_button =
263 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
265 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
267 wwin->client_flags.no_miniaturize_button =
268 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
270 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
272 wwin->client_flags.no_resizebar =
273 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
275 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
277 /* these attributes supposedly imply in the existence
278 * of a titlebar */
279 if (gs_hints->window_style & (WMResizableWindowMask |
280 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
281 wwin->client_flags.no_titlebar = 0;
282 } else {
283 wwin->client_flags.no_titlebar =
284 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
288 } else {
289 /* setup the defaults */
290 wwin->client_flags.no_border = 0;
291 wwin->client_flags.no_titlebar = 0;
292 wwin->client_flags.no_closable = 0;
293 wwin->client_flags.no_miniaturizable = 0;
294 wwin->client_flags.no_resizable = 0;
295 wwin->client_flags.no_close_button = 0;
296 wwin->client_flags.no_miniaturize_button = 0;
297 wwin->client_flags.no_resizebar = 0;
299 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
300 wwin->client_flags.no_appicon = 1;
303 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
305 WScreen *scr = wwin->screen_ptr;
307 /* sets global default stuff */
308 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
310 * Decoration setting is done in this precedence (lower to higher)
311 * - use global default in the resource database
312 * - guess some settings
313 * - use GNUstep/external window attributes
314 * - set hints specified for the app in the resource DB
317 WSETUFLAG(wwin, broken_close, 0);
319 if (wwin->protocols.DELETE_WINDOW)
320 WSETUFLAG(wwin, kill_close, 0);
321 else
322 WSETUFLAG(wwin, kill_close, 1);
324 /* transients can't be iconified or maximized */
325 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
326 WSETUFLAG(wwin, no_miniaturizable, 1);
327 WSETUFLAG(wwin, no_miniaturize_button, 1);
330 /* if the window can't be resized, remove the resizebar */
331 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
332 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
333 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
334 WSETUFLAG(wwin, no_resizable, 1);
335 WSETUFLAG(wwin, no_resizebar, 1);
338 /* set GNUstep window attributes */
339 if (wwin->wm_gnustep_attr) {
340 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
342 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
344 *level = wwin->wm_gnustep_attr->window_level;
346 * INT_MIN is the only illegal window level.
348 if (*level == INT_MIN)
349 *level = INT_MIN + 1;
350 } else {
351 /* setup defaults */
352 *level = WMNormalLevel;
354 } else {
355 int tmp_workspace = -1;
356 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
358 #ifdef MWM_HINTS
359 wMWMCheckClientHints(wwin);
360 #endif /* MWM_HINTS */
362 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
364 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
365 * have INT_MIN that means that no window level was requested. -Dan
367 if (tmp_level == INT_MIN) {
368 if (WFLAGP(wwin, floating))
369 *level = WMFloatingLevel;
370 else if (WFLAGP(wwin, sunken))
371 *level = WMSunkenLevel;
372 else
373 *level = WMNormalLevel;
374 } else {
375 *level = tmp_level;
378 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
379 WWindow *transientOwner = wWindowFor(wwin->transient_for);
380 if (transientOwner) {
381 int ownerLevel = transientOwner->frame->core->stacking->window_level;
382 if (ownerLevel > *level)
383 *level = ownerLevel;
387 if (tmp_workspace >= 0)
388 *workspace = tmp_workspace % scr->workspace_count;
392 * Set attributes specified only for that window/class.
393 * This might do duplicate work with the 1st wDefaultFillAttributes().
395 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
396 &wwin->defined_user_flags, False);
398 * Sanity checks for attributes that depend on other attributes
400 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
401 wwin->user_flags.emulate_appicon = 0;
403 if (wwin->main_window != None) {
404 WApplication *wapp = wApplicationOf(wwin->main_window);
405 if (wapp && !wapp->flags.emulated)
406 wwin->user_flags.emulate_appicon = 0;
409 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
410 wwin->user_flags.emulate_appicon = 0;
412 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
413 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
414 wwin->user_flags.sunken = 0;
416 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
418 /* windows that have takefocus=False shouldn't take focus at all */
419 if (wwin->focus_mode == WFM_NO_INPUT)
420 wwin->client_flags.no_focusable = 1;
423 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
425 int w1, h1, w2, h2;
427 w1 = wwin->frame->core->width;
428 h1 = wwin->frame->core->height;
429 w2 = obscured->frame->core->width;
430 h2 = obscured->frame->core->height;
432 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
433 && wwin->frame->workspace != obscured->frame->workspace)
434 return False;
436 if (wwin->frame_x + w1 < obscured->frame_x
437 || wwin->frame_y + h1 < obscured->frame_y
438 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
439 return False;
441 return True;
444 static void fixLeaderProperties(WWindow *wwin)
446 XClassHint *classHint;
447 XWMHints *hints, *clientHints;
448 XWindowAttributes attr;
449 Window leaders[2], window;
450 char **argv, *command;
451 int argc, i, pid;
452 Bool haveCommand;
454 classHint = XAllocClassHint();
455 clientHints = XGetWMHints(dpy, wwin->client_win);
456 pid = wNETWMGetPidForWindow(wwin->client_win);
457 if (pid > 0)
458 haveCommand = GetCommandForPid(pid, &argv, &argc);
459 else
460 haveCommand = False;
462 leaders[0] = wwin->client_leader;
463 leaders[1] = wwin->group_id;
465 if (haveCommand) {
466 command = GetCommandForWindow(wwin->client_win);
467 if (command)
468 wfree(command); /* command already set. nothing to do. */
469 else
470 XSetCommand(dpy, wwin->client_win, argv, argc);
473 for (i = 0; i < 2; i++) {
474 window = leaders[i];
475 if (window) {
476 if (XGetClassHint(dpy, window, classHint) == 0) {
477 classHint->res_name = wwin->wm_instance;
478 classHint->res_class = wwin->wm_class;
479 XSetClassHint(dpy, window, classHint);
481 hints = XGetWMHints(dpy, window);
482 if (hints) {
483 XFree(hints);
484 } else if (clientHints) {
485 /* set window group leader to self */
486 clientHints->window_group = window;
487 clientHints->flags |= WindowGroupHint;
488 XSetWMHints(dpy, window, clientHints);
491 if (haveCommand) {
492 command = GetCommandForWindow(window);
493 if (command)
494 wfree(command); /* command already set. nothing to do. */
495 else
496 XSetCommand(dpy, window, argv, argc);
499 /* Make sure we get notification when this window is destroyed */
500 if (XGetWindowAttributes(dpy, window, &attr))
501 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
505 XFree(classHint);
506 if (clientHints)
507 XFree(clientHints);
508 if (haveCommand)
509 wfree(argv);
512 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
514 XClassHint *classHint;
515 XWMHints *hints;
516 Window leader;
517 int argc;
518 char **argv;
520 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
521 /* set class hint */
522 classHint = XAllocClassHint();
523 classHint->res_name = instance;
524 classHint->res_class = class;
525 XSetClassHint(dpy, leader, classHint);
526 XFree(classHint);
528 /* inherit these from the original leader if available */
529 hints = XGetWMHints(dpy, win);
530 if (!hints) {
531 hints = XAllocWMHints();
532 hints->flags = 0;
534 /* set window group leader to self */
535 hints->window_group = leader;
536 hints->flags |= WindowGroupHint;
537 XSetWMHints(dpy, leader, hints);
538 XFree(hints);
540 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
541 XSetCommand(dpy, leader, argv, argc);
542 XFreeStringList(argv);
545 return leader;
548 static int matchIdentifier(const void *item, const void *cdata)
550 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
554 *----------------------------------------------------------------
555 * wManageWindow--
556 * reparents the window and allocates a descriptor for it.
557 * Window manager hints and other hints are fetched to configure
558 * the window decoration attributes and others. User preferences
559 * for the window are used if available, to configure window
560 * decorations and some behaviour.
561 * If in startup, windows that are override redirect,
562 * unmapped and never were managed and are Withdrawn are not
563 * managed.
565 * Returns:
566 * the new window descriptor
568 * Side effects:
569 * The window is reparented and appropriate notification
570 * is done to the client. Input mask for the window is setup.
571 * The window descriptor is also associated with various window
572 * contexts and inserted in the head of the window list.
573 * Event handler contexts are associated for some objects
574 * (buttons, titlebar and resizebar)
576 *----------------------------------------------------------------
578 WWindow *wManageWindow(WScreen *scr, Window window)
580 WWindow *wwin;
581 int x, y;
582 unsigned width, height;
583 XWindowAttributes wattribs;
584 XSetWindowAttributes attribs;
585 WWindowState *win_state;
586 WWindow *transientOwner = NULL;
587 int window_level;
588 int wm_state;
589 int foo;
590 int workspace = -1;
591 char *title;
592 Bool withdraw = False;
593 Bool raise = False;
594 Bool frame_adjustment = True;
596 /* mutex. */
597 XGrabServer(dpy);
598 XSync(dpy, False);
599 /* make sure the window is still there */
600 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
601 XUngrabServer(dpy);
602 return NULL;
605 /* if it's an override-redirect, ignore it */
606 if (wattribs.override_redirect) {
607 XUngrabServer(dpy);
608 return NULL;
611 wm_state = PropGetWindowState(window);
613 /* if it's startup and the window is unmapped, don't manage it */
614 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
615 XUngrabServer(dpy);
616 return NULL;
619 wwin = wWindowCreate();
621 title = wNETWMGetWindowName(window);
622 if (title)
623 wwin->flags.net_has_title = 1;
624 if (!title && !wFetchName(dpy, window, &title))
625 title = NULL;
627 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
629 #ifdef SHAPE
630 if (wShapeSupported) {
631 int junk;
632 unsigned int ujunk;
633 int b_shaped;
635 XShapeSelectInput(dpy, window, ShapeNotifyMask);
636 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
637 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
638 wwin->flags.shaped = b_shaped;
640 #endif
643 * Get hints and other information in properties
645 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
647 /* setup descriptor */
648 wwin->client_win = window;
649 wwin->screen_ptr = scr;
650 wwin->old_border_width = wattribs.border_width;
651 wwin->event_mask = CLIENT_EVENTS;
652 attribs.event_mask = CLIENT_EVENTS;
653 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
654 attribs.save_under = False;
656 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
657 XSetWindowBorderWidth(dpy, window, 0);
659 /* get hints from GNUstep app */
660 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
661 wwin->flags.is_gnustep = 1;
663 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
664 wwin->wm_gnustep_attr = NULL;
666 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
667 wwin->flags.is_dockapp = 1;
668 withdraw = True;
671 wwin->client_leader = PropGetClientLeader(window);
672 if (wwin->client_leader != None)
673 wwin->main_window = wwin->client_leader;
675 wwin->wm_hints = XGetWMHints(dpy, window);
677 if (wwin->wm_hints) {
678 if (wwin->wm_hints->flags & StateHint) {
680 if (wwin->wm_hints->initial_state == IconicState) {
681 wwin->flags.miniaturized = 1;
682 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
683 wwin->flags.is_dockapp = 1;
684 withdraw = True;
688 if (wwin->wm_hints->flags & WindowGroupHint) {
689 wwin->group_id = wwin->wm_hints->window_group;
690 /* window_group has priority over CLIENT_LEADER */
691 wwin->main_window = wwin->group_id;
692 } else {
693 wwin->group_id = None;
696 if (wwin->wm_hints->flags & UrgencyHint) {
697 wwin->flags.urgent = 1;
698 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
700 } else {
701 wwin->group_id = None;
704 PropGetProtocols(window, &wwin->protocols);
706 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
707 wwin->transient_for = None;
708 } else {
709 if (wwin->transient_for == None || wwin->transient_for == window) {
710 wwin->transient_for = scr->root_win;
711 } else {
712 transientOwner = wWindowFor(wwin->transient_for);
713 if (transientOwner && transientOwner->main_window != None)
714 wwin->main_window = transientOwner->main_window;
718 /* guess the focus mode */
719 wwin->focus_mode = getFocusMode(wwin);
721 /* get geometry stuff */
722 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
724 /* get colormap windows */
725 GetColormapWindows(wwin);
728 * Setup the decoration/window attributes and
729 * geometry
731 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
733 /* Make broken apps behave as a nice app. */
734 if (WFLAGP(wwin, emulate_appicon))
735 wwin->main_window = wwin->client_win;
737 fixLeaderProperties(wwin);
739 wwin->orig_main_window = wwin->main_window;
741 if (wwin->flags.is_gnustep)
742 WSETUFLAG(wwin, shared_appicon, 0);
744 if (wwin->main_window) {
745 extern Atom _XA_WINDOWMAKER_MENU;
746 XTextProperty text_prop;
748 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
749 WSETUFLAG(wwin, shared_appicon, 0);
752 if (wwin->flags.is_dockapp)
753 WSETUFLAG(wwin, shared_appicon, 0);
755 if (wwin->main_window) {
756 WApplication *app = wApplicationOf(wwin->main_window);
757 if (app && app->app_icon)
758 WSETUFLAG(wwin, shared_appicon, 0);
761 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
762 char *buffer, *instance, *class;
763 WFakeGroupLeader *fPtr;
764 int index;
766 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
768 /* // only enter here if PropGetWMClass() succeds */
769 PropGetWMClass(wwin->main_window, &class, &instance);
770 buffer = StrConcatDot(instance, class);
772 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
773 if (index != WANotFound) {
774 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
775 if (fPtr->retainCount == 0)
776 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
777 instance, class);
779 fPtr->retainCount++;
780 if (fPtr->origLeader == None) {
781 if (ADEQUATE(wwin->main_window)) {
782 fPtr->retainCount++;
783 fPtr->origLeader = wwin->main_window;
786 wwin->fake_group = fPtr;
787 /*wwin->group_id = fPtr->leader; */
788 wwin->main_window = fPtr->leader;
789 wfree(buffer);
790 } else {
791 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
793 fPtr->identifier = buffer;
794 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
795 fPtr->origLeader = None;
796 fPtr->retainCount = 1;
798 WMAddToArray(scr->fakeGroupLeaders, fPtr);
800 if (ADEQUATE(wwin->main_window)) {
801 fPtr->retainCount++;
802 fPtr->origLeader = wwin->main_window;
804 wwin->fake_group = fPtr;
805 /*wwin->group_id = fPtr->leader; */
806 wwin->main_window = fPtr->leader;
808 if (instance)
809 free(instance);
810 if (class)
811 free(class);
812 #undef ADEQUATE
816 * Setup the initial state of the window
818 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
819 wwin->flags.miniaturized = 1;
821 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
822 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
824 wNETWMCheckInitialClientState(wwin);
826 /* apply previous state if it exists and we're in startup */
827 if (scr->flags.startup && wm_state >= 0) {
828 if (wm_state == IconicState) {
829 wwin->flags.miniaturized = 1;
830 } else if (wm_state == WithdrawnState) {
831 withdraw = True;
835 /* if there is a saved state (from file), restore it */
836 win_state = NULL;
837 if (wwin->main_window != None)
838 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
839 else
840 win_state = (WWindowState *) wWindowGetSavedState(window);
842 if (win_state && !withdraw) {
843 if (win_state->state->hidden > 0)
844 wwin->flags.hidden = win_state->state->hidden;
846 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
847 wwin->flags.shaded = win_state->state->shaded;
849 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
850 wwin->flags.miniaturized = win_state->state->miniaturized;
852 if (!IS_OMNIPRESENT(wwin)) {
853 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
854 wwin->wm_class);
855 if (w < 0 || w >= scr->workspace_count) {
856 workspace = win_state->state->workspace;
857 if (workspace >= scr->workspace_count)
858 workspace = scr->current_workspace;
859 } else {
860 workspace = w;
862 } else {
863 workspace = scr->current_workspace;
867 /* if we're restarting, restore saved state (from hints).
868 * This will overwrite previous */
870 WSavedState *wstate;
872 if (getSavedState(window, &wstate)) {
873 wwin->flags.shaded = wstate->shaded;
874 wwin->flags.hidden = wstate->hidden;
875 wwin->flags.miniaturized = wstate->miniaturized;
876 wwin->flags.maximized = wstate->maximized;
877 if (wwin->flags.maximized) {
878 wwin->old_geometry.x = wstate->x;
879 wwin->old_geometry.y = wstate->y;
880 wwin->old_geometry.width = wstate->w;
881 wwin->old_geometry.height = wstate->h;
884 workspace = wstate->workspace;
885 } else {
886 wstate = NULL;
889 /* restore window shortcut */
890 if (wstate != NULL || win_state != NULL) {
891 unsigned mask = 0;
893 if (win_state != NULL)
894 mask = win_state->state->window_shortcuts;
896 if (wstate != NULL && mask == 0)
897 mask = wstate->window_shortcuts;
899 if (mask > 0) {
900 int i;
902 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
903 if (mask & (1 << i)) {
904 if (!scr->shortcutWindows[i])
905 scr->shortcutWindows[i] = WMCreateArray(4);
907 WMAddToArray(scr->shortcutWindows[i], wwin);
912 if (wstate != NULL)
913 wfree(wstate);
916 /* don't let transients start miniaturized if their owners are not */
917 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
918 wwin->flags.miniaturized = 0;
919 if (wwin->wm_hints)
920 wwin->wm_hints->initial_state = NormalState;
923 /* set workspace on which the window starts */
924 if (workspace >= 0) {
925 if (workspace > scr->workspace_count - 1)
926 workspace = workspace % scr->workspace_count;
927 } else {
928 int w;
930 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
932 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
933 workspace = w;
934 } else {
935 if (wPreferences.open_transients_with_parent && transientOwner)
936 workspace = transientOwner->frame->workspace;
937 else
938 workspace = scr->current_workspace;
942 /* setup window geometry */
943 if (win_state && win_state->state->w > 0) {
944 width = win_state->state->w;
945 height = win_state->state->h;
947 wWindowConstrainSize(wwin, &width, &height);
949 /* do not ask for window placement if the window is
950 * transient, during startup, if the initial workspace is another one
951 * or if the window wants to start iconic.
952 * If geometry was saved, restore it. */
954 Bool dontBring = False;
956 if (win_state && win_state->state->w > 0) {
957 x = win_state->state->x;
958 y = win_state->state->y;
959 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
960 && !scr->flags.startup
961 && workspace == scr->current_workspace
962 && !wwin->flags.miniaturized
963 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
965 if (transientOwner && transientOwner->flags.mapped) {
966 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
967 WMRect rect;
968 int head;
970 x = transientOwner->frame_x +
971 abs((transientOwner->frame->core->width - width) / 2) + offs;
972 y = transientOwner->frame_y +
973 abs((transientOwner->frame->core->height - height) / 3) + offs;
976 * limit transient windows to be inside their parent's head
978 rect.pos.x = transientOwner->frame_x;
979 rect.pos.y = transientOwner->frame_y;
980 rect.size.width = transientOwner->frame->core->width;
981 rect.size.height = transientOwner->frame->core->height;
983 head = wGetHeadForRect(scr, rect);
984 rect = wGetRectForHead(scr, head);
986 if (x < rect.pos.x)
987 x = rect.pos.x;
988 else if (x + width > rect.pos.x + rect.size.width)
989 x = rect.pos.x + rect.size.width - width;
991 if (y < rect.pos.y)
992 y = rect.pos.y;
993 else if (y + height > rect.pos.y + rect.size.height)
994 y = rect.pos.y + rect.size.height - height;
996 } else {
997 PlaceWindow(wwin, &x, &y, width, height);
998 frame_adjustment = False;
1000 if (wPreferences.window_placement == WPM_MANUAL) {
1001 dontBring = True;
1003 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1004 int head, flags;
1005 WMRect rect;
1006 int reposition = 0;
1009 * Make spash screens come out in the center of a head
1010 * trouble is that most splashies never get here
1011 * they are managed trough atoms but god knows where.
1012 * Dan, do you know ? -peter
1014 * Most of them are not managed, they have set
1015 * OverrideRedirect, which means we can't do anything about
1016 * them. -alfredo
1020 * xinerama checks for: across head and dead space
1022 rect.pos.x = x;
1023 rect.pos.y = y;
1024 rect.size.width = width;
1025 rect.size.height = height;
1027 head = wGetRectPlacementInfo(scr, rect, &flags);
1029 if (flags & XFLAG_DEAD)
1030 reposition = 1;
1032 if (flags & XFLAG_MULTIPLE)
1033 reposition = 2;
1036 switch (reposition) {
1037 case 1:
1038 head = wGetHeadForPointerLocation(scr);
1039 rect = wGetRectForHead(scr, head);
1041 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1042 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1043 break;
1045 case 2:
1046 rect = wGetRectForHead(scr, head);
1048 if (x < rect.pos.x)
1049 x = rect.pos.x;
1050 else if (x + width > rect.pos.x + rect.size.width)
1051 x = rect.pos.x + rect.size.width - width;
1053 if (y < rect.pos.y)
1054 y = rect.pos.y;
1055 else if (y + height > rect.pos.y + rect.size.height)
1056 y = rect.pos.y + rect.size.height - height;
1058 break;
1060 default:
1061 break;
1065 if (WFLAGP(wwin, dont_move_off) && dontBring)
1066 wScreenBringInside(scr, &x, &y, width, height);
1069 wNETWMPositionSplash(wwin, &x, &y, width, height);
1071 if (wwin->flags.urgent) {
1072 if (!IS_OMNIPRESENT(wwin))
1073 wwin->flags.omnipresent ^= 1;
1077 * Create frame, borders and do reparenting
1079 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1080 #ifdef XKB_BUTTON_HINT
1081 if (wPreferences.modelock)
1082 foo |= WFF_LANGUAGE_BUTTON;
1083 #endif
1084 if (HAS_TITLEBAR(wwin))
1085 foo |= WFF_TITLEBAR;
1086 if (HAS_RESIZEBAR(wwin))
1087 foo |= WFF_RESIZEBAR;
1088 if (HAS_BORDER(wwin))
1089 foo |= WFF_BORDER;
1091 wwin->frame = wFrameWindowCreate(scr, window_level,
1092 x, y, width, height,
1093 &wPreferences.window_title_clearance,
1094 &wPreferences.window_title_min_height,
1095 &wPreferences.window_title_max_height,
1096 foo,
1097 scr->window_title_texture,
1098 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1099 wattribs.depth, wattribs.visual, wattribs.colormap);
1101 wwin->frame->flags.is_client_window_frame = 1;
1102 wwin->frame->flags.justification = wPreferences.title_justification;
1104 /* setup button images */
1105 wWindowUpdateButtonImages(wwin);
1107 /* hide unused buttons */
1108 foo = 0;
1109 if (WFLAGP(wwin, no_close_button))
1110 foo |= WFF_RIGHT_BUTTON;
1111 if (WFLAGP(wwin, no_miniaturize_button))
1112 foo |= WFF_LEFT_BUTTON;
1113 #ifdef XKB_BUTTON_HINT
1114 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1115 foo |= WFF_LANGUAGE_BUTTON;
1116 #endif
1117 if (foo != 0)
1118 wFrameWindowHideButton(wwin->frame, foo);
1120 wwin->frame->child = wwin;
1121 wwin->frame->workspace = workspace;
1122 wwin->frame->on_click_left = windowIconifyClick;
1124 #ifdef XKB_BUTTON_HINT
1125 if (wPreferences.modelock)
1126 wwin->frame->on_click_language = windowLanguageClick;
1127 #endif
1129 wwin->frame->on_click_right = windowCloseClick;
1130 wwin->frame->on_dblclick_right = windowCloseDblClick;
1131 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1132 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1133 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1135 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1136 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1137 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1140 int gx, gy;
1142 wClientGetGravityOffsets(wwin, &gx, &gy);
1144 /* if gravity is to the south, account for the border sizes */
1145 if (gy > 0)
1146 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1149 if (frame_adjustment) {
1150 WMRect rect;
1151 WArea usableArea;
1152 int head;
1154 rect.pos.x = x;
1155 rect.pos.y = y;
1156 rect.size.width = 1;
1157 rect.size.height = 1;
1159 head = wGetHeadForRect(scr, rect);
1160 usableArea = wGetUsableAreaForHead(scr, head, NULL, True);
1162 /* wWindowConfigure() will account for the frame
1163 * when placing so the window would be shifted without
1164 * the adjustment below
1166 if (y >= usableArea.y1 + wwin->frame->top_width)
1167 y -= wwin->frame->top_width;
1169 /* wWindowConfigure() will account for the window border
1170 * when placing so the window would be shifted without
1171 * the adjustment below
1173 if (HAS_BORDER(wwin)) {
1174 if (x >= usableArea.x1 + FRAME_BORDER_WIDTH)
1175 x -= FRAME_BORDER_WIDTH;
1176 if (y >= usableArea.y1 + FRAME_BORDER_WIDTH)
1177 y -= FRAME_BORDER_WIDTH;
1182 * wWindowConfigure() will init the client window's size
1183 * (wwin->client.{width,height}) and all other geometry
1184 * related variables (frame_x,frame_y)
1186 wWindowConfigure(wwin, x, y, width, height);
1188 /* to make sure the window receives it's new position after reparenting */
1189 wWindowSynthConfigureNotify(wwin);
1192 * Setup descriptors and save window to internal
1193 * lists
1195 if (wwin->main_window != None) {
1196 WApplication *app;
1197 WWindow *leader;
1199 /* Leader windows do not necessary set themselves as leaders.
1200 * If this is the case, point the leader of this window to
1201 * itself */
1202 leader = wWindowFor(wwin->main_window);
1203 if (leader && leader->main_window == None)
1204 leader->main_window = leader->client_win;
1206 app = wApplicationCreate(wwin);
1207 if (app) {
1208 app->last_workspace = workspace;
1211 * Do application specific stuff, like setting application
1212 * wide attributes.
1215 if (wwin->flags.hidden) {
1216 /* if the window was set to hidden because it was hidden
1217 * in a previous incarnation and that state was restored */
1218 app->flags.hidden = 1;
1219 } else if (app->flags.hidden) {
1220 if (WFLAGP(app->main_window_desc, start_hidden)) {
1221 wwin->flags.hidden = 1;
1222 } else {
1223 wUnhideApplication(app, False, False);
1224 raise = True;
1227 wAppBounce(app);
1231 /* setup the frame descriptor */
1232 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1233 wwin->frame->core->descriptor.parent = wwin;
1234 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1236 /* don't let windows go away if we die */
1237 XAddToSaveSet(dpy, window);
1239 XLowerWindow(dpy, window);
1241 /* if window is in this workspace and should be mapped, then map it */
1242 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1243 && !wwin->flags.hidden && !withdraw) {
1245 /* The following "if" is to avoid crashing of clients that expect
1246 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1247 * after the return from this function.
1249 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1250 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1251 else
1252 wClientSetState(wwin, NormalState, None);
1254 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1255 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1257 * The brain damaged idiotic non-click to focus modes will
1258 * have trouble with this because:
1260 * 1. window is created and mapped by the client
1261 * 2. window is mapped by wmaker in small size
1262 * 3. window is animated to grow to normal size
1263 * 4. this function returns to normal event loop
1264 * 5. eventually, the EnterNotify event that would trigger
1265 * the window focusing (if the mouse is over that window)
1266 * will be processed by wmaker.
1267 * But since this event will be rather delayed
1268 * (step 3 has a large delay) the time when the event ocurred
1269 * and when it is processed, the client that owns that window
1270 * will reject the XSetInputFocus() for it.
1272 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1273 DoWindowBirth(wwin);
1275 wWindowMap(wwin);
1278 /* setup stacking descriptor */
1279 if (transientOwner)
1280 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1281 else
1282 wwin->frame->core->stacking->child_of = NULL;
1284 if (!scr->focused_window) {
1285 /* first window on the list */
1286 wwin->next = NULL;
1287 wwin->prev = NULL;
1288 scr->focused_window = wwin;
1289 } else {
1290 WWindow *tmp;
1292 /* add window at beginning of focus window list */
1293 tmp = scr->focused_window;
1294 while (tmp->prev)
1295 tmp = tmp->prev;
1296 tmp->prev = wwin;
1297 wwin->next = tmp;
1298 wwin->prev = NULL;
1301 /* raise is set to true if we un-hid the app when this window was born.
1302 * we raise, else old windows of this app will be above this new one. */
1303 if (raise)
1304 wRaiseFrame(wwin->frame->core);
1306 /* Update name must come after WApplication stuff is done */
1307 wWindowUpdateName(wwin, title);
1308 if (title)
1309 XFree(title);
1311 XUngrabServer(dpy);
1314 * Final preparations before window is ready to go
1316 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1318 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1319 if (((transientOwner && transientOwner->flags.focused)
1320 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1322 /* only auto_focus if on same screen as mouse
1323 * (and same head for xinerama mode)
1324 * TODO: make it an option */
1326 /*TODO add checking the head of the window, is it available? */
1327 short same_screen = 0, same_head = 1;
1329 int foo;
1330 unsigned int bar;
1331 Window dummy;
1333 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1334 &foo, &foo, &foo, &foo, &bar) != False)
1335 same_screen = 1;
1337 if (same_screen == 1 && same_head == 1)
1338 wSetFocusTo(scr, wwin);
1341 wWindowResetMouseGrabs(wwin);
1343 if (!WFLAGP(wwin, no_bind_keys))
1344 wWindowSetKeyGrabs(wwin);
1346 WMPostNotificationName(WMNManaged, wwin, NULL);
1347 wColormapInstallForWindow(scr, scr->cmap_window);
1349 /* Setup Notification Observers */
1350 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1352 /* Cleanup temporary stuff */
1353 if (win_state)
1354 wWindowDeleteSavedState(win_state);
1356 /* If the window must be withdrawed, then do it now.
1357 * Must do some optimization, 'though */
1358 if (withdraw) {
1359 wwin->flags.mapped = 0;
1360 wClientSetState(wwin, WithdrawnState, None);
1361 wUnmanageWindow(wwin, True, False);
1362 wwin = NULL;
1365 return wwin;
1368 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1369 char *title, int x, int y, int width, int height)
1371 WWindow *wwin;
1372 int foo;
1374 wwin = wWindowCreate();
1376 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1378 wwin->flags.internal_window = 1;
1380 WSETUFLAG(wwin, omnipresent, 1);
1381 WSETUFLAG(wwin, no_shadeable, 1);
1382 WSETUFLAG(wwin, no_resizable, 1);
1383 WSETUFLAG(wwin, no_miniaturizable, 1);
1385 wwin->focus_mode = WFM_PASSIVE;
1386 wwin->client_win = window;
1387 wwin->screen_ptr = scr;
1388 wwin->transient_for = owner;
1389 wwin->client.x = x;
1390 wwin->client.y = y;
1391 wwin->client.width = width;
1392 wwin->client.height = height;
1393 wwin->frame_x = wwin->client.x;
1394 wwin->frame_y = wwin->client.y;
1396 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1397 foo |= WFF_TITLEBAR;
1398 #ifdef XKB_BUTTON_HINT
1399 foo |= WFF_LANGUAGE_BUTTON;
1400 #endif
1402 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1403 wwin->frame_x, wwin->frame_y,
1404 width, height,
1405 &wPreferences.window_title_clearance,
1406 &wPreferences.window_title_min_height,
1407 &wPreferences.window_title_max_height,
1408 foo,
1409 scr->window_title_texture,
1410 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1411 scr->w_depth, scr->w_visual, scr->w_colormap);
1413 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1415 wwin->frame->flags.is_client_window_frame = 1;
1416 wwin->frame->flags.justification = wPreferences.title_justification;
1418 wFrameWindowChangeTitle(wwin->frame, title);
1420 /* setup button images */
1421 wWindowUpdateButtonImages(wwin);
1423 /* hide buttons */
1424 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1426 wwin->frame->child = wwin;
1427 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1429 #ifdef XKB_BUTTON_HINT
1430 if (wPreferences.modelock)
1431 wwin->frame->on_click_language = windowLanguageClick;
1432 #endif
1434 wwin->frame->on_click_right = windowCloseClick;
1435 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1436 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1437 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1438 wwin->client.y += wwin->frame->top_width;
1440 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1441 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1443 /* setup the frame descriptor */
1444 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1445 wwin->frame->core->descriptor.parent = wwin;
1446 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1448 XLowerWindow(dpy, window);
1449 XMapSubwindows(dpy, wwin->frame->core->window);
1451 /* setup stacking descriptor */
1452 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1453 WWindow *tmp;
1454 tmp = wWindowFor(wwin->transient_for);
1455 if (tmp)
1456 wwin->frame->core->stacking->child_of = tmp->frame->core;
1457 } else {
1458 wwin->frame->core->stacking->child_of = NULL;
1461 if (!scr->focused_window) {
1462 /* first window on the list */
1463 wwin->next = NULL;
1464 wwin->prev = NULL;
1465 scr->focused_window = wwin;
1466 } else {
1467 WWindow *tmp;
1469 /* add window at beginning of focus window list */
1470 tmp = scr->focused_window;
1471 while (tmp->prev)
1472 tmp = tmp->prev;
1473 tmp->prev = wwin;
1474 wwin->next = tmp;
1475 wwin->prev = NULL;
1478 if (wwin->flags.is_gnustep == 0)
1479 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1481 /* if (wPreferences.auto_focus) */
1482 wSetFocusTo(scr, wwin);
1483 wWindowResetMouseGrabs(wwin);
1484 wWindowSetKeyGrabs(wwin);
1486 return wwin;
1490 *----------------------------------------------------------------------
1491 * wUnmanageWindow--
1492 * Removes the frame window from a window and destroys all data
1493 * related to it. The window will be reparented back to the root window
1494 * if restore is True.
1496 * Side effects:
1497 * Everything related to the window is destroyed and the window
1498 * is removed from the window lists. Focus is set to the previous on the
1499 * window list.
1500 *----------------------------------------------------------------------
1502 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1504 WCoreWindow *frame = wwin->frame->core;
1505 WWindow *owner = NULL;
1506 WWindow *newFocusedWindow = NULL;
1507 int wasFocused;
1508 WScreen *scr = wwin->screen_ptr;
1510 /* First close attribute editor window if open */
1511 if (wwin->flags.inspector_open)
1512 wCloseInspectorForWindow(wwin);
1514 /* Close window menu if it's open for this window */
1515 if (wwin->flags.menu_open_for_me)
1516 CloseWindowMenu(scr);
1518 if (!destroyed) {
1519 if (!wwin->flags.internal_window)
1520 XRemoveFromSaveSet(dpy, wwin->client_win);
1522 /* If this is a leader window, we still need to listen for
1523 * DestroyNotify and PropertyNotify. */
1524 if (wApplicationOf(wwin->client_win))
1525 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1526 else
1527 XSelectInput(dpy, wwin->client_win, NoEventMask);
1529 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1530 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1533 XUnmapWindow(dpy, frame->window);
1535 XUnmapWindow(dpy, wwin->client_win);
1537 /* deselect window */
1538 wSelectWindow(wwin, False);
1540 /* remove all pending events on window */
1541 /* I think this only matters for autoraise */
1542 if (wPreferences.raise_delay)
1543 WMDeleteTimerWithClientData(wwin->frame->core);
1545 XFlush(dpy);
1547 /* reparent the window back to the root */
1548 if (restore)
1549 wClientRestore(wwin);
1551 if (wwin->transient_for != scr->root_win) {
1552 owner = wWindowFor(wwin->transient_for);
1553 if (owner) {
1554 if (!owner->flags.semi_focused)
1555 owner = NULL;
1556 else
1557 owner->flags.semi_focused = 0;
1561 wasFocused = wwin->flags.focused;
1563 /* remove from window focus list */
1564 if (!wwin->prev && !wwin->next) {
1565 /* was the only window */
1566 scr->focused_window = NULL;
1567 newFocusedWindow = NULL;
1568 } else {
1569 WWindow *tmp;
1571 if (wwin->prev)
1572 wwin->prev->next = wwin->next;
1574 if (wwin->next)
1575 wwin->next->prev = wwin->prev;
1576 else {
1577 scr->focused_window = wwin->prev;
1578 scr->focused_window->next = NULL;
1581 if (wPreferences.focus_mode == WKF_CLICK) {
1583 /* if in click to focus mode and the window
1584 * was a transient, focus the owner window
1586 tmp = NULL;
1587 if (wPreferences.focus_mode == WKF_CLICK) {
1588 tmp = wWindowFor(wwin->transient_for);
1589 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1590 tmp = NULL;
1593 /* otherwise, focus the next one in the focus list */
1594 if (!tmp) {
1595 tmp = scr->focused_window;
1596 while (tmp) { /* look for one in the window list first */
1597 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1598 && (tmp->flags.mapped || tmp->flags.shaded))
1599 break;
1600 tmp = tmp->prev;
1602 if (!tmp) { /* if unsuccessful, choose any focusable window */
1603 tmp = scr->focused_window;
1604 while (tmp) {
1605 if (!WFLAGP(tmp, no_focusable)
1606 && (tmp->flags.mapped || tmp->flags.shaded))
1607 break;
1608 tmp = tmp->prev;
1613 newFocusedWindow = tmp;
1615 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1616 unsigned int mask;
1617 int foo;
1618 Window bar, win;
1620 /* This is to let the root window get the keyboard input
1621 * if Sloppy focus mode and no other window get focus.
1622 * This way keybindings will not freeze.
1624 tmp = NULL;
1625 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1626 tmp = wWindowFor(win);
1627 if (tmp == wwin)
1628 tmp = NULL;
1629 newFocusedWindow = tmp;
1630 } else {
1631 newFocusedWindow = NULL;
1635 if (!wwin->flags.internal_window)
1636 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1637 if (wasFocused) {
1638 if (newFocusedWindow != owner && owner) {
1639 if (wwin->flags.is_gnustep == 0)
1640 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1642 wSetFocusTo(scr, newFocusedWindow);
1645 /* Close menu and unhighlight */
1646 WApplication *oapp = wApplicationOf(wwin->main_window);
1647 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1648 if (oapp && oapp != napp) {
1649 wAppMenuUnmap(oapp->menu);
1650 wApplicationDeactivate(oapp);
1653 wNETCleanupFrameExtents(wwin);
1655 wWindowDestroy(wwin);
1656 XFlush(dpy);
1659 void wWindowMap(WWindow *wwin)
1661 XMapWindow(dpy, wwin->frame->core->window);
1662 if (!wwin->flags.shaded) {
1663 /* window will be remapped when getting MapNotify */
1664 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1665 XMapWindow(dpy, wwin->client_win);
1666 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1668 wwin->flags.mapped = 1;
1672 void wWindowUnmap(WWindow *wwin)
1674 wwin->flags.mapped = 0;
1676 /* prevent window withdrawal when getting UnmapNotify */
1677 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1678 XUnmapWindow(dpy, wwin->client_win);
1679 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1681 XUnmapWindow(dpy, wwin->frame->core->window);
1684 void wWindowFocus(WWindow *wwin, WWindow *owin)
1686 WWindow *nowner;
1687 WWindow *oowner;
1689 #ifdef KEEP_XKB_LOCK_STATUS
1690 if (wPreferences.modelock)
1691 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1692 #endif /* KEEP_XKB_LOCK_STATUS */
1694 wwin->flags.semi_focused = 0;
1696 if (wwin->flags.is_gnustep == 0)
1697 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1699 wwin->flags.focused = 1;
1701 wWindowResetMouseGrabs(wwin);
1703 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1705 if (owin == wwin || !owin)
1706 return;
1708 nowner = wWindowFor(wwin->transient_for);
1710 /* new window is a transient for the old window */
1711 if (nowner == owin) {
1712 owin->flags.semi_focused = 1;
1713 wWindowUnfocus(nowner);
1714 return;
1717 oowner = wWindowFor(owin->transient_for);
1719 /* new window is owner of old window */
1720 if (wwin == oowner) {
1721 wWindowUnfocus(owin);
1722 return;
1725 if (!nowner) {
1726 wWindowUnfocus(owin);
1727 return;
1730 /* new window has same owner of old window */
1731 if (oowner == nowner) {
1732 /* prevent unfocusing of owner */
1733 oowner->flags.semi_focused = 0;
1734 wWindowUnfocus(owin);
1735 oowner->flags.semi_focused = 1;
1737 return;
1740 /* nowner != NULL && oowner != nowner */
1741 nowner->flags.semi_focused = 1;
1742 wWindowUnfocus(nowner);
1743 wWindowUnfocus(owin);
1746 void wWindowUnfocus(WWindow *wwin)
1748 CloseWindowMenu(wwin->screen_ptr);
1750 if (wwin->flags.is_gnustep == 0)
1751 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1753 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1754 WWindow *owner;
1755 owner = wWindowFor(wwin->transient_for);
1756 if (owner && owner->flags.semi_focused) {
1757 owner->flags.semi_focused = 0;
1758 if (owner->flags.mapped || owner->flags.shaded) {
1759 wWindowUnfocus(owner);
1760 wFrameWindowPaint(owner->frame);
1764 wwin->flags.focused = 0;
1765 wWindowResetMouseGrabs(wwin);
1766 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1769 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1771 char *title;
1773 if (!wwin->frame)
1774 return;
1776 if (!newTitle)
1777 title = DEF_WINDOW_TITLE; /* the hint was removed */
1778 else
1779 title = newTitle;
1781 if (wFrameWindowChangeTitle(wwin->frame, title))
1782 WMPostNotificationName(WMNChangedName, wwin, NULL);
1786 *----------------------------------------------------------------------
1788 * wWindowConstrainSize--
1789 * Constrains size for the client window, taking the maximal size,
1790 * window resize increments and other size hints into account.
1792 * Returns:
1793 * The closest size to what was given that the client window can
1794 * have.
1796 *----------------------------------------------------------------------
1798 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1800 int width = (int)*nwidth;
1801 int height = (int)*nheight;
1802 int winc = 1;
1803 int hinc = 1;
1804 int minW = 1, minH = 1;
1805 int maxW = wwin->screen_ptr->scr_width * 2;
1806 int maxH = wwin->screen_ptr->scr_height * 2;
1807 int minAX = -1, minAY = -1;
1808 int maxAX = -1, maxAY = -1;
1809 int baseW = 0;
1810 int baseH = 0;
1812 if (wwin->normal_hints) {
1813 winc = wwin->normal_hints->width_inc;
1814 hinc = wwin->normal_hints->height_inc;
1815 minW = wwin->normal_hints->min_width;
1816 minH = wwin->normal_hints->min_height;
1817 maxW = wwin->normal_hints->max_width;
1818 maxH = wwin->normal_hints->max_height;
1819 if (wwin->normal_hints->flags & PAspect) {
1820 minAX = wwin->normal_hints->min_aspect.x;
1821 minAY = wwin->normal_hints->min_aspect.y;
1822 maxAX = wwin->normal_hints->max_aspect.x;
1823 maxAY = wwin->normal_hints->max_aspect.y;
1826 baseW = wwin->normal_hints->base_width;
1827 baseH = wwin->normal_hints->base_height;
1830 if (width < minW)
1831 width = minW;
1832 if (height < minH)
1833 height = minH;
1835 if (width > maxW)
1836 width = maxW;
1837 if (height > maxH)
1838 height = maxH;
1840 /* aspect ratio code borrowed from olwm */
1841 if (minAX > 0) {
1842 /* adjust max aspect ratio */
1843 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1844 if (maxAX > maxAY) {
1845 height = (width * maxAY) / maxAX;
1846 if (height > maxH) {
1847 height = maxH;
1848 width = (height * maxAX) / maxAY;
1850 } else {
1851 width = (height * maxAX) / maxAY;
1852 if (width > maxW) {
1853 width = maxW;
1854 height = (width * maxAY) / maxAX;
1859 /* adjust min aspect ratio */
1860 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1861 if (minAX > minAY) {
1862 height = (width * minAY) / minAX;
1863 if (height < minH) {
1864 height = minH;
1865 width = (height * minAX) / minAY;
1867 } else {
1868 width = (height * minAX) / minAY;
1869 if (width < minW) {
1870 width = minW;
1871 height = (width * minAY) / minAX;
1877 if (baseW != 0)
1878 width = (((width - baseW) / winc) * winc) + baseW;
1879 else
1880 width = (((width - minW) / winc) * winc) + minW;
1882 if (baseH != 0)
1883 height = (((height - baseH) / hinc) * hinc) + baseH;
1884 else
1885 height = (((height - minH) / hinc) * hinc) + minH;
1887 /* broken stupid apps may cause preposterous values for these.. */
1888 if (width > 0)
1889 *nwidth = width;
1890 if (height > 0)
1891 *nheight = height;
1894 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1895 unsigned int *width, unsigned int *height)
1897 int baseW = 0, baseH = 0;
1898 int winc = 1, hinc = 1;
1900 if (wwin->normal_hints) {
1901 baseW = wwin->normal_hints->base_width;
1902 baseH = wwin->normal_hints->base_height;
1904 winc = wwin->normal_hints->width_inc;
1905 hinc = wwin->normal_hints->height_inc;
1908 if (*width > maxW)
1909 *width = maxW - (maxW - baseW) % winc;
1911 if (*height > maxH)
1912 *height = maxH - (maxH - baseH) % hinc;
1915 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1917 WScreen *scr = wwin->screen_ptr;
1918 WApplication *wapp;
1919 int unmap = 0;
1921 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1922 return;
1924 if (workspace != scr->current_workspace) {
1925 /* Sent to other workspace. Unmap window */
1926 if ((wwin->flags.mapped
1927 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1928 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1930 wapp = wApplicationOf(wwin->main_window);
1931 if (wapp)
1932 wapp->last_workspace = workspace;
1934 if (wwin->flags.miniaturized) {
1935 if (wwin->icon) {
1936 XUnmapWindow(dpy, wwin->icon->core->window);
1937 wwin->icon->mapped = 0;
1939 } else {
1940 unmap = 1;
1941 wSetFocusTo(scr, NULL);
1944 } else {
1945 /* brought to current workspace. Map window */
1946 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1947 if (wwin->icon) {
1948 XMapWindow(dpy, wwin->icon->core->window);
1949 wwin->icon->mapped = 1;
1951 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1952 wWindowMap(wwin);
1955 if (!IS_OMNIPRESENT(wwin)) {
1956 int oldWorkspace = wwin->frame->workspace;
1957 wwin->frame->workspace = workspace;
1958 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1961 if (unmap)
1962 wWindowUnmap(wwin);
1965 void wWindowSynthConfigureNotify(WWindow *wwin)
1967 XEvent sevent;
1969 sevent.type = ConfigureNotify;
1970 sevent.xconfigure.display = dpy;
1971 sevent.xconfigure.event = wwin->client_win;
1972 sevent.xconfigure.window = wwin->client_win;
1974 sevent.xconfigure.x = wwin->client.x;
1975 sevent.xconfigure.y = wwin->client.y;
1976 sevent.xconfigure.width = wwin->client.width;
1977 sevent.xconfigure.height = wwin->client.height;
1979 sevent.xconfigure.border_width = wwin->old_border_width;
1980 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1981 sevent.xconfigure.above = wwin->frame->titlebar->window;
1982 else
1983 sevent.xconfigure.above = None;
1985 sevent.xconfigure.override_redirect = False;
1986 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1987 XFlush(dpy);
1991 *----------------------------------------------------------------------
1992 * wWindowConfigure()
1994 * req_x, req_y: new requested positions for the frame
1995 * req_width, req_height: new requested sizes for the client
1997 * Configures the frame, decorations and client window to the specified
1998 * geometry, whose validity is not checked -- wWindowConstrainSize()
1999 * must be used for that.
2000 * The size parameters are for the client window, but the position is
2001 * for the frame.
2002 * The client window receives a ConfigureNotify event, according
2003 * to what ICCCM says.
2005 * Returns:
2006 * None
2008 * Side effects:
2009 * Window size and position are changed and client window receives
2010 * a ConfigureNotify event.
2011 *----------------------------------------------------------------------
2013 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2015 int synth_notify = False;
2016 int resize;
2018 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2020 * if the window is being moved but not resized then
2021 * send a synthetic ConfigureNotify
2023 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2024 synth_notify = True;
2026 if (WFLAGP(wwin, dont_move_off))
2027 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2029 if (resize) {
2030 if (req_width < MIN_WINDOW_SIZE)
2031 req_width = MIN_WINDOW_SIZE;
2032 if (req_height < MIN_WINDOW_SIZE)
2033 req_height = MIN_WINDOW_SIZE;
2035 /* If growing, resize inner part before frame,
2036 * if shrinking, resize frame before.
2037 * This will prevent the frame (that can have a different color)
2038 * to be exposed, causing flicker */
2039 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2040 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2042 if (wwin->flags.shaded) {
2043 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2044 wwin->old_geometry.height = req_height;
2045 } else {
2046 int h;
2048 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2050 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2053 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2054 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2056 wwin->client.x = req_x;
2057 wwin->client.y = req_y + wwin->frame->top_width;
2058 wwin->client.width = req_width;
2059 wwin->client.height = req_height;
2060 } else {
2061 wwin->client.x = req_x;
2062 wwin->client.y = req_y + wwin->frame->top_width;
2064 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2066 wwin->frame_x = req_x;
2067 wwin->frame_y = req_y;
2068 if (HAS_BORDER(wwin)) {
2069 wwin->client.x += FRAME_BORDER_WIDTH;
2070 wwin->client.y += FRAME_BORDER_WIDTH;
2072 #ifdef SHAPE
2073 if (wShapeSupported && wwin->flags.shaped && resize)
2074 wWindowSetShape(wwin);
2075 #endif
2077 if (synth_notify)
2078 wWindowSynthConfigureNotify(wwin);
2080 wNETFrameExtents(wwin);
2082 XFlush(dpy);
2085 /* req_x, req_y: new position of the frame */
2086 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2088 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2089 int synth_notify = False;
2091 /* Send a synthetic ConfigureNotify event for every window movement. */
2092 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2093 synth_notify = True;
2094 #else
2095 /* A single synthetic ConfigureNotify event is sent at the end of
2096 * a completed (opaque) movement in moveres.c */
2097 #endif
2099 if (WFLAGP(wwin, dont_move_off))
2100 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2101 wwin->frame->core->width, wwin->frame->core->height);
2103 wwin->client.x = req_x;
2104 wwin->client.y = req_y + wwin->frame->top_width;
2105 if (HAS_BORDER(wwin)) {
2106 wwin->client.x += FRAME_BORDER_WIDTH;
2107 wwin->client.y += FRAME_BORDER_WIDTH;
2110 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2112 wwin->frame_x = req_x;
2113 wwin->frame_y = req_y;
2115 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2116 if (synth_notify)
2117 wWindowSynthConfigureNotify(wwin);
2118 #endif
2121 void wWindowUpdateButtonImages(WWindow *wwin)
2123 WScreen *scr = wwin->screen_ptr;
2124 Pixmap pixmap, mask;
2125 WFrameWindow *fwin = wwin->frame;
2127 if (!HAS_TITLEBAR(wwin))
2128 return;
2130 /* miniaturize button */
2131 if (!WFLAGP(wwin, no_miniaturize_button)) {
2132 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2133 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2135 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2136 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2137 else
2138 mask = None;
2140 if (fwin->lbutton_image
2141 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2142 wPixmapDestroy(fwin->lbutton_image);
2143 fwin->lbutton_image = NULL;
2146 if (!fwin->lbutton_image) {
2147 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2148 fwin->lbutton_image->client_owned = 1;
2149 fwin->lbutton_image->client_owned_mask = 1;
2151 } else {
2152 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2153 wPixmapDestroy(fwin->lbutton_image);
2155 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2158 #ifdef XKB_BUTTON_HINT
2159 if (!WFLAGP(wwin, no_language_button)) {
2160 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2161 wPixmapDestroy(fwin->languagebutton_image);
2163 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2165 #endif
2167 /* close button */
2169 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2170 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2172 if (!WFLAGP(wwin, no_close_button)) {
2173 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2174 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2176 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2177 mask = wwin->wm_gnustep_attr->close_mask;
2178 else
2179 mask = None;
2181 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2182 || fwin->rbutton_image->mask != mask)) {
2183 wPixmapDestroy(fwin->rbutton_image);
2184 fwin->rbutton_image = NULL;
2187 if (!fwin->rbutton_image) {
2188 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2189 fwin->rbutton_image->client_owned = 1;
2190 fwin->rbutton_image->client_owned_mask = 1;
2193 } else if (WFLAGP(wwin, kill_close)) {
2194 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2195 wPixmapDestroy(fwin->rbutton_image);
2197 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2199 } else if (MGFLAGP(wwin, broken_close)) {
2200 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2201 wPixmapDestroy(fwin->rbutton_image);
2203 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2205 } else {
2206 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2207 wPixmapDestroy(fwin->rbutton_image);
2209 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2213 /* force buttons to be redrawn */
2214 fwin->flags.need_texture_change = 1;
2215 wFrameWindowPaint(fwin);
2219 *---------------------------------------------------------------------------
2220 * wWindowConfigureBorders--
2221 * Update window border configuration according to attribute flags.
2223 *---------------------------------------------------------------------------
2225 void wWindowConfigureBorders(WWindow *wwin)
2227 if (wwin->frame) {
2228 int flags;
2229 int newy, oldh;
2231 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2233 #ifdef XKB_BUTTON_HINT
2234 if (wPreferences.modelock)
2235 flags |= WFF_LANGUAGE_BUTTON;
2236 #endif
2238 if (HAS_TITLEBAR(wwin))
2239 flags |= WFF_TITLEBAR;
2240 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2241 flags |= WFF_RESIZEBAR;
2242 if (HAS_BORDER(wwin))
2243 flags |= WFF_BORDER;
2244 if (wwin->flags.shaded)
2245 flags |= WFF_IS_SHADED;
2247 oldh = wwin->frame->top_width;
2248 wFrameWindowUpdateBorders(wwin->frame, flags);
2249 if (oldh != wwin->frame->top_width) {
2250 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2252 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2253 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2256 flags = 0;
2257 if (!WFLAGP(wwin, no_miniaturize_button)
2258 && wwin->frame->flags.hide_left_button)
2259 flags |= WFF_LEFT_BUTTON;
2261 #ifdef XKB_BUTTON_HINT
2262 if (!WFLAGP(wwin, no_language_button)
2263 && wwin->frame->flags.hide_language_button)
2264 flags |= WFF_LANGUAGE_BUTTON;
2265 #endif
2267 if (!WFLAGP(wwin, no_close_button)
2268 && wwin->frame->flags.hide_right_button)
2269 flags |= WFF_RIGHT_BUTTON;
2271 if (flags != 0) {
2272 wWindowUpdateButtonImages(wwin);
2273 wFrameWindowShowButton(wwin->frame, flags);
2276 flags = 0;
2277 if (WFLAGP(wwin, no_miniaturize_button)
2278 && !wwin->frame->flags.hide_left_button)
2279 flags |= WFF_LEFT_BUTTON;
2281 #ifdef XKB_BUTTON_HINT
2282 if (WFLAGP(wwin, no_language_button)
2283 && !wwin->frame->flags.hide_language_button)
2284 flags |= WFF_LANGUAGE_BUTTON;
2285 #endif
2287 if (WFLAGP(wwin, no_close_button)
2288 && !wwin->frame->flags.hide_right_button)
2289 flags |= WFF_RIGHT_BUTTON;
2291 if (flags != 0)
2292 wFrameWindowHideButton(wwin->frame, flags);
2294 #ifdef SHAPE
2295 if (wShapeSupported && wwin->flags.shaped)
2296 wWindowSetShape(wwin);
2297 #endif
2301 void wWindowSaveState(WWindow * wwin)
2303 long data[10];
2304 int i;
2306 memset(data, 0, sizeof(long) * 10);
2307 data[0] = wwin->frame->workspace;
2308 data[1] = wwin->flags.miniaturized;
2309 data[2] = wwin->flags.shaded;
2310 data[3] = wwin->flags.hidden;
2311 data[4] = wwin->flags.maximized;
2312 if (wwin->flags.maximized == 0) {
2313 data[5] = wwin->frame_x;
2314 data[6] = wwin->frame_y;
2315 data[7] = wwin->frame->core->width;
2316 data[8] = wwin->frame->core->height;
2317 } else {
2318 data[5] = wwin->old_geometry.x;
2319 data[6] = wwin->old_geometry.y;
2320 data[7] = wwin->old_geometry.width;
2321 data[8] = wwin->old_geometry.height;
2324 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2325 if (wwin->screen_ptr->shortcutWindows[i] &&
2326 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2327 data[9] |= 1 << i;
2329 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2330 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2333 static int getSavedState(Window window, WSavedState ** state)
2335 Atom type_ret;
2336 int fmt_ret;
2337 unsigned long nitems_ret;
2338 unsigned long bytes_after_ret;
2339 long *data;
2341 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2342 True, _XA_WINDOWMAKER_STATE,
2343 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2344 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2345 return 0;
2347 *state = wmalloc(sizeof(WSavedState));
2349 (*state)->workspace = data[0];
2350 (*state)->miniaturized = data[1];
2351 (*state)->shaded = data[2];
2352 (*state)->hidden = data[3];
2353 (*state)->maximized = data[4];
2354 (*state)->x = data[5];
2355 (*state)->y = data[6];
2356 (*state)->w = data[7];
2357 (*state)->h = data[8];
2358 (*state)->window_shortcuts = data[9];
2360 XFree(data);
2362 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2363 return 1;
2364 else
2365 return 0;
2368 #ifdef SHAPE
2369 void wWindowClearShape(WWindow * wwin)
2371 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2372 0, wwin->frame->top_width, None, ShapeSet);
2373 XFlush(dpy);
2376 void wWindowSetShape(WWindow * wwin)
2378 XRectangle rect[2];
2379 int count;
2380 #ifdef OPTIMIZE_SHAPE
2381 XRectangle *rects;
2382 XRectangle *urec;
2383 int ordering;
2385 /* only shape is the client's */
2386 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2387 goto alt_code;
2389 /* Get array of rectangles describing the shape mask */
2390 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2391 if (!rects)
2392 goto alt_code;
2394 urec = malloc(sizeof(XRectangle) * (count + 2));
2395 if (!urec) {
2396 XFree(rects);
2397 goto alt_code;
2400 /* insert our decoration rectangles in the rect list */
2401 memcpy(urec, rects, sizeof(XRectangle) * count);
2402 XFree(rects);
2404 if (HAS_TITLEBAR(wwin)) {
2405 urec[count].x = -1;
2406 urec[count].y = -1 - wwin->frame->top_width;
2407 urec[count].width = wwin->frame->core->width + 2;
2408 urec[count].height = wwin->frame->top_width + 1;
2409 count++;
2411 if (HAS_RESIZEBAR(wwin)) {
2412 urec[count].x = -1;
2413 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2414 urec[count].width = wwin->frame->core->width + 2;
2415 urec[count].height = wwin->frame->bottom_width + 1;
2416 count++;
2419 /* shape our frame window */
2420 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2421 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2422 XFlush(dpy);
2423 wfree(urec);
2424 return;
2426 alt_code:
2427 #endif /* OPTIMIZE_SHAPE */
2428 count = 0;
2429 if (HAS_TITLEBAR(wwin)) {
2430 rect[count].x = -1;
2431 rect[count].y = -1;
2432 rect[count].width = wwin->frame->core->width + 2;
2433 rect[count].height = wwin->frame->top_width + 1;
2434 count++;
2436 if (HAS_RESIZEBAR(wwin)) {
2437 rect[count].x = -1;
2438 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2439 rect[count].width = wwin->frame->core->width + 2;
2440 rect[count].height = wwin->frame->bottom_width + 1;
2441 count++;
2443 if (count > 0) {
2444 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2445 0, 0, rect, count, ShapeSet, Unsorted);
2447 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2448 0, wwin->frame->top_width, wwin->client_win,
2449 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2450 XFlush(dpy);
2452 #endif /* SHAPE */
2454 /* ====================================================================== */
2456 static FocusMode getFocusMode(WWindow * wwin)
2458 FocusMode mode;
2460 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2461 if (wwin->wm_hints->input == True) {
2462 if (wwin->protocols.TAKE_FOCUS)
2463 mode = WFM_LOCALLY_ACTIVE;
2464 else
2465 mode = WFM_PASSIVE;
2466 } else {
2467 if (wwin->protocols.TAKE_FOCUS)
2468 mode = WFM_GLOBALLY_ACTIVE;
2469 else
2470 mode = WFM_NO_INPUT;
2472 } else {
2473 mode = WFM_PASSIVE;
2475 return mode;
2478 void wWindowSetKeyGrabs(WWindow * wwin)
2480 int i;
2481 WShortKey *key;
2483 for (i = 0; i < WKBD_LAST; i++) {
2484 key = &wKeyBindings[i];
2486 if (key->keycode == 0)
2487 continue;
2488 if (key->modifier != AnyModifier) {
2489 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2490 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2491 #ifdef NUMLOCK_HACK
2492 /* Also grab all modifier combinations possible that include,
2493 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2494 * work even if the NumLock/ScrollLock key is on.
2496 wHackedGrabKey(key->keycode, key->modifier,
2497 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2498 #endif
2500 XGrabKey(dpy, key->keycode, key->modifier,
2501 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2504 wRootMenuBindShortcuts(wwin->frame->core->window);
2507 void wWindowResetMouseGrabs(WWindow * wwin)
2509 /* Mouse grabs can't be done on the client window because of
2510 * ICCCM and because clients that try to do the same will crash.
2512 * But there is a problem wich makes tbar buttons of unfocused
2513 * windows not usable as the click goes to the frame window instead
2514 * of the button itself. Must figure a way to fix that.
2517 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2519 if (!WFLAGP(wwin, no_bind_mouse)) {
2520 /* grabs for Meta+drag */
2521 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2522 True, ButtonPressMask | ButtonReleaseMask,
2523 GrabModeSync, GrabModeAsync, None, None);
2525 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2526 * but we only grab it for Button4 and Button 5 since a lot of apps
2527 * use CTRL+Button1-3 for app related functionality
2529 if (wPreferences.resize_increment > 0) {
2530 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2531 True, ButtonPressMask | ButtonReleaseMask,
2532 GrabModeSync, GrabModeAsync, None, None);
2533 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2534 True, ButtonPressMask | ButtonReleaseMask,
2535 GrabModeSync, GrabModeAsync, None, None);
2537 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2538 True, ButtonPressMask | ButtonReleaseMask,
2539 GrabModeSync, GrabModeAsync, None, None);
2540 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2541 True, ButtonPressMask | ButtonReleaseMask,
2542 GrabModeSync, GrabModeAsync, None, None);
2546 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2547 && !wwin->flags.is_gnustep) {
2548 /* the passive grabs to focus the window */
2549 /* if (wPreferences.focus_mode == WKF_CLICK) */
2550 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2551 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2553 XFlush(dpy);
2556 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2558 if (attr->flags & GSExtraFlagsAttr) {
2559 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2560 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2561 wWindowUpdateButtonImages(wwin);
2566 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2568 WWindowState *wstate;
2570 wstate = malloc(sizeof(WWindowState));
2571 if (!wstate)
2572 return NULL;
2574 memset(wstate, 0, sizeof(WWindowState));
2575 wstate->pid = pid;
2576 if (instance)
2577 wstate->instance = wstrdup(instance);
2578 if (class)
2579 wstate->class = wstrdup(class);
2580 if (command)
2581 wstate->command = wstrdup(command);
2582 wstate->state = state;
2584 wstate->next = windowState;
2585 windowState = wstate;
2587 return wstate;
2590 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2592 WMagicNumber wWindowGetSavedState(Window win)
2594 char *instance, *class, *command = NULL;
2595 WWindowState *wstate = windowState;
2597 if (!wstate)
2598 return NULL;
2600 command = GetCommandForWindow(win);
2601 if (!command)
2602 return NULL;
2604 if (PropGetWMClass(win, &class, &instance)) {
2605 while (wstate) {
2606 if (SAME(instance, wstate->instance) &&
2607 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2608 break;
2610 wstate = wstate->next;
2612 } else {
2613 wstate = NULL;
2616 if (command)
2617 wfree(command);
2618 if (instance)
2619 free(instance);
2620 if (class)
2621 free(class);
2623 return wstate;
2626 void wWindowDeleteSavedState(WMagicNumber id)
2628 WWindowState *tmp, *wstate = (WWindowState *) id;
2630 if (!wstate || !windowState)
2631 return;
2633 tmp = windowState;
2634 if (tmp == wstate) {
2635 windowState = wstate->next;
2636 release_wwindowstate(wstate);
2637 } else {
2638 while (tmp->next) {
2639 if (tmp->next == wstate) {
2640 tmp->next = wstate->next;
2641 release_wwindowstate(wstate);
2642 break;
2644 tmp = tmp->next;
2649 void wWindowDeleteSavedStatesForPID(pid_t pid)
2651 WWindowState *tmp, *wstate;
2653 if (!windowState)
2654 return;
2656 tmp = windowState;
2657 if (tmp->pid == pid) {
2658 wstate = windowState;
2659 windowState = tmp->next;
2661 release_wwindowstate(wstate);
2662 } else {
2663 while (tmp->next) {
2664 if (tmp->next->pid == pid) {
2665 wstate = tmp->next;
2666 tmp->next = wstate->next;
2667 release_wwindowstate(wstate);
2668 break;
2670 tmp = tmp->next;
2675 static void release_wwindowstate(WWindowState *wstate)
2677 if (wstate->instance)
2678 wfree(wstate->instance);
2680 if (wstate->class)
2681 wfree(wstate->class);
2683 if (wstate->command)
2684 wfree(wstate->command);
2686 wfree(wstate->state);
2687 wfree(wstate);
2690 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2692 if (wwin->flags.omnipresent == flag)
2693 return;
2695 wwin->flags.omnipresent = flag;
2696 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2699 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2701 WWindow *wwin = data;
2703 #ifndef NUMLOCK_HACK
2704 if ((event->xbutton.state & ValidModMask)
2705 != (event->xbutton.state & ~LockMask)) {
2706 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2707 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2709 #endif
2711 event->xbutton.state &= ValidModMask;
2713 CloseWindowMenu(wwin->screen_ptr);
2715 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2716 && !WFLAGP(wwin, no_focusable)) {
2717 wSetFocusTo(wwin->screen_ptr, wwin);
2720 if (event->xbutton.button == Button1)
2721 wRaiseFrame(wwin->frame->core);
2723 if (event->xbutton.window != wwin->frame->resizebar->window) {
2724 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2725 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2726 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2727 return;
2731 if (event->xbutton.state & MOD_MASK) {
2732 /* move the window */
2733 wMouseMoveWindow(wwin, event);
2734 XUngrabPointer(dpy, CurrentTime);
2735 } else {
2736 wMouseResizeWindow(wwin, event);
2737 XUngrabPointer(dpy, CurrentTime);
2741 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2743 WWindow *wwin = data;
2745 event->xbutton.state &= ValidModMask;
2747 if (event->xbutton.button == Button1) {
2748 if (event->xbutton.state == 0) {
2749 if (!WFLAGP(wwin, no_shadeable)) {
2750 /* shade window */
2751 if (wwin->flags.shaded)
2752 wUnshadeWindow(wwin);
2753 else
2754 wShadeWindow(wwin);
2756 } else {
2757 int dir = 0;
2759 if (event->xbutton.state & ControlMask)
2760 dir |= MAX_VERTICAL;
2762 if (event->xbutton.state & ShiftMask) {
2763 dir |= MAX_HORIZONTAL;
2764 if (!(event->xbutton.state & ControlMask))
2765 wSelectWindow(wwin, !wwin->flags.selected);
2768 /* maximize window */
2769 if (dir != 0 && IS_RESIZABLE(wwin)) {
2770 int ndir = dir ^ wwin->flags.maximized;
2772 if (ndir != 0)
2773 wMaximizeWindow(wwin, ndir);
2774 else
2775 wUnmaximizeWindow(wwin);
2778 } else if (event->xbutton.button == Button3) {
2779 if (event->xbutton.state & MOD_MASK)
2780 wHideOtherApplications(wwin);
2781 } else if (event->xbutton.button == Button2) {
2782 wSelectWindow(wwin, !wwin->flags.selected);
2783 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2784 wShadeWindow(wwin);
2785 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2786 wUnshadeWindow(wwin);
2790 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2792 WWindow *wwin = desc->parent;
2793 unsigned int new_width, w_scale;
2794 unsigned int new_height, h_scale;
2795 unsigned int resize_width_increment = 0;
2796 unsigned int resize_height_increment = 0;
2798 if (wwin->normal_hints) {
2799 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2800 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2801 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2802 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2804 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2805 resize_width_increment = wPreferences.resize_increment;
2806 resize_height_increment = wPreferences.resize_increment;
2809 event->xbutton.state &= ValidModMask;
2811 CloseWindowMenu(wwin->screen_ptr);
2813 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2814 wSetFocusTo(wwin->screen_ptr, wwin);
2816 if (event->xbutton.button == Button1)
2817 wRaiseFrame(wwin->frame->core);
2819 if (event->xbutton.state & ControlMask) {
2820 if (event->xbutton.button == Button4) {
2821 new_width = wwin->client.width - resize_width_increment;
2822 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2823 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2825 if (event->xbutton.button == Button5) {
2826 new_width = wwin->client.width + resize_width_increment;
2827 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2828 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2832 if (event->xbutton.state & MOD_MASK) {
2833 /* move the window */
2834 if (XGrabPointer(dpy, wwin->client_win, False,
2835 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2836 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2837 return;
2839 if (event->xbutton.button == Button3) {
2840 wMouseResizeWindow(wwin, event);
2841 } else if (event->xbutton.button == Button4) {
2842 new_height = wwin->client.height - resize_height_increment;
2843 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2844 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2845 } else if (event->xbutton.button == Button5) {
2846 new_height = wwin->client.height + resize_height_increment;
2847 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2848 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2849 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2850 wMouseMoveWindow(wwin, event);
2852 XUngrabPointer(dpy, CurrentTime);
2856 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2858 WWindow *wwin = (WWindow *) data;
2860 #ifndef NUMLOCK_HACK
2861 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2862 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2863 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2864 #endif
2865 event->xbutton.state &= ValidModMask;
2867 CloseWindowMenu(wwin->screen_ptr);
2869 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2870 && !WFLAGP(wwin, no_focusable))
2871 wSetFocusTo(wwin->screen_ptr, wwin);
2873 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2875 if (event->xbutton.button == Button1) {
2876 if (event->xbutton.state & MOD_MASK)
2877 wLowerFrame(wwin->frame->core);
2878 else
2879 wRaiseFrame(wwin->frame->core);
2881 if ((event->xbutton.state & ShiftMask)
2882 && !(event->xbutton.state & ControlMask)) {
2883 wSelectWindow(wwin, !wwin->flags.selected);
2884 return;
2886 if (event->xbutton.window != wwin->frame->titlebar->window
2887 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2888 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2889 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2890 return;
2893 /* move the window */
2894 wMouseMoveWindow(wwin, event);
2896 XUngrabPointer(dpy, CurrentTime);
2897 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2898 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2899 WObjDescriptor *desc;
2901 if (event->xbutton.window != wwin->frame->titlebar->window
2902 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2903 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2904 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2905 return;
2908 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2910 /* allow drag select */
2911 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2912 event->xany.send_event = True;
2913 (*desc->handle_mousedown) (desc, event);
2915 XUngrabPointer(dpy, CurrentTime);
2919 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2921 WWindow *wwin = data;
2923 event->xbutton.state &= ValidModMask;
2925 CloseWindowMenu(wwin->screen_ptr);
2927 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2928 return;
2930 /* if control-click, kill the client */
2931 if (event->xbutton.state & ControlMask) {
2932 wClientKill(wwin);
2933 } else {
2934 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2935 /* send delete message */
2936 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2941 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2943 WWindow *wwin = data;
2945 CloseWindowMenu(wwin->screen_ptr);
2947 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2948 return;
2950 /* send delete message */
2951 if (wwin->protocols.DELETE_WINDOW)
2952 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2953 else
2954 wClientKill(wwin);
2957 #ifdef XKB_BUTTON_HINT
2958 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2960 WWindow *wwin = data;
2961 WFrameWindow *fwin = wwin->frame;
2962 WScreen *scr = fwin->screen_ptr;
2963 int tl;
2965 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2966 return;
2967 tl = wwin->frame->languagemode;
2968 wwin->frame->languagemode = wwin->frame->last_languagemode;
2969 wwin->frame->last_languagemode = tl;
2970 wSetFocusTo(scr, wwin);
2971 wwin->frame->languagebutton_image =
2972 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2973 wFrameWindowUpdateLanguageButton(wwin->frame);
2974 if (event->xbutton.button == Button3)
2975 return;
2976 wRaiseFrame(fwin->core);
2978 #endif
2980 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2982 WWindow *wwin = data;
2984 event->xbutton.state &= ValidModMask;
2986 CloseWindowMenu(wwin->screen_ptr);
2988 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2989 return;
2991 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2992 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2993 } else {
2994 WApplication *wapp;
2995 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2997 wapp = wApplicationOf(wwin->main_window);
2998 if (wapp && !WFLAGP(wwin, no_appicon))
2999 wHideApplication(wapp);
3000 } else if (event->xbutton.state == 0) {
3001 wIconifyWindow(wwin);