Fix some warnings
[wmaker-crm.git] / src / window.c
blob0a585ebce9e97f06545ad82653f98e4d6d8ac45d
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 "keybind.h"
54 #include "stacking.h"
55 #include "defaults.h"
56 #include "workspace.h"
57 #include "xinerama.h"
58 #include "appmenu.h"
59 #include "appicon.h"
61 #ifdef MWM_HINTS
62 # include "motif.h"
63 #endif
64 #include "wmspec.h"
66 #define MOD_MASK wPreferences.modifier_mask
68 /****** Global Variables ******/
69 extern WShortKey wKeyBindings[WKBD_LAST];
71 #ifdef SHAPE
72 extern Bool wShapeSupported;
73 #endif
75 /* contexts */
76 extern XContext wWinContext;
78 /* protocol atoms */
79 extern Atom _XA_WM_DELETE_WINDOW;
80 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
81 extern Atom _XA_WINDOWMAKER_STATE;
82 extern WPreferences wPreferences;
83 extern Time LastTimestamp;
85 /* superfluous... */
86 extern void DoWindowBirth(WWindow *wwin);
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 /****** Notification Observers ******/
114 static void appearanceObserver(void *self, WMNotification * notif)
116 WWindow *wwin = (WWindow *) self;
117 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
119 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
120 return;
122 if (flags & WFontSettings) {
123 wWindowConfigureBorders(wwin);
124 if (wwin->flags.shaded) {
125 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
127 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
128 wWindowSynthConfigureNotify(wwin);
131 if (flags & WTextureSettings) {
132 wwin->frame->flags.need_texture_remake = 1;
134 if (flags & (WTextureSettings | WColorSettings)) {
135 if (wwin->frame->titlebar)
136 XClearWindow(dpy, wwin->frame->titlebar->window);
138 wFrameWindowPaint(wwin->frame);
143 WWindow *wWindowFor(Window window)
145 WObjDescriptor *desc;
147 if (window == None)
148 return NULL;
150 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
151 return NULL;
153 if (desc->parent_type == WCLASS_WINDOW)
154 return desc->parent;
155 else if (desc->parent_type == WCLASS_FRAME) {
156 WFrameWindow *frame = (WFrameWindow *) desc->parent;
157 if (frame->flags.is_client_window_frame)
158 return frame->child;
161 return NULL;
164 WWindow *wWindowCreate(void)
166 WWindow *wwin;
168 wwin = wmalloc(sizeof(WWindow));
169 wretain(wwin);
171 memset(wwin, 0, sizeof(WWindow));
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(scr, 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 */
357 Bool check;
359 check = False;
361 #ifdef MWM_HINTS
362 wMWMCheckClientHints(wwin);
363 #endif /* MWM_HINTS */
365 if (!check)
366 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
368 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
369 * have INT_MIN that means that no window level was requested. -Dan
371 if (tmp_level == INT_MIN) {
372 if (WFLAGP(wwin, floating))
373 *level = WMFloatingLevel;
374 else if (WFLAGP(wwin, sunken))
375 *level = WMSunkenLevel;
376 else
377 *level = WMNormalLevel;
378 } else {
379 *level = tmp_level;
382 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
383 WWindow *transientOwner = wWindowFor(wwin->transient_for);
384 if (transientOwner) {
385 int ownerLevel = transientOwner->frame->core->stacking->window_level;
386 if (ownerLevel > *level)
387 *level = ownerLevel;
391 if (tmp_workspace >= 0)
392 *workspace = tmp_workspace % scr->workspace_count;
396 * Set attributes specified only for that window/class.
397 * This might do duplicate work with the 1st wDefaultFillAttributes().
399 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
400 &wwin->user_flags, &wwin->defined_user_flags, False);
402 * Sanity checks for attributes that depend on other attributes
404 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
405 wwin->user_flags.emulate_appicon = 0;
407 if (wwin->main_window != None) {
408 WApplication *wapp = wApplicationOf(wwin->main_window);
409 if (wapp && !wapp->flags.emulated)
410 wwin->user_flags.emulate_appicon = 0;
413 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
414 wwin->user_flags.emulate_appicon = 0;
416 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
417 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
418 wwin->user_flags.sunken = 0;
420 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
422 /* windows that have takefocus=False shouldn't take focus at all */
423 if (wwin->focus_mode == WFM_NO_INPUT)
424 wwin->client_flags.no_focusable = 1;
427 Bool wWindowCanReceiveFocus(WWindow *wwin)
429 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
430 return False;
431 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
432 return False;
433 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
434 return False;
436 return True;
439 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
441 int w1, h1, w2, h2;
443 w1 = wwin->frame->core->width;
444 h1 = wwin->frame->core->height;
445 w2 = obscured->frame->core->width;
446 h2 = obscured->frame->core->height;
448 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
449 && wwin->frame->workspace != obscured->frame->workspace)
450 return False;
452 if (wwin->frame_x + w1 < obscured->frame_x
453 || wwin->frame_y + h1 < obscured->frame_y
454 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
455 return False;
457 return True;
460 static void fixLeaderProperties(WWindow *wwin)
462 XClassHint *classHint;
463 XWMHints *hints, *clientHints;
464 XWindowAttributes attr;
465 Window leaders[2], window;
466 char **argv, *command;
467 int argc, i, pid;
468 Bool haveCommand;
470 classHint = XAllocClassHint();
471 clientHints = XGetWMHints(dpy, wwin->client_win);
472 pid = wNETWMGetPidForWindow(wwin->client_win);
473 if (pid > 0)
474 haveCommand = GetCommandForPid(pid, &argv, &argc);
475 else
476 haveCommand = False;
478 leaders[0] = wwin->client_leader;
479 leaders[1] = wwin->group_id;
481 if (haveCommand) {
482 command = GetCommandForWindow(wwin->client_win);
483 if (command) {
484 /* command already set. nothing to do. */
485 wfree(command);
486 } else {
487 XSetCommand(dpy, wwin->client_win, argv, argc);
491 for (i = 0; i < 2; i++) {
492 window = leaders[i];
493 if (window) {
494 if (XGetClassHint(dpy, window, classHint) == 0) {
495 classHint->res_name = wwin->wm_instance;
496 classHint->res_class = wwin->wm_class;
497 XSetClassHint(dpy, window, classHint);
499 hints = XGetWMHints(dpy, window);
500 if (hints) {
501 XFree(hints);
502 } else if (clientHints) {
503 /* set window group leader to self */
504 clientHints->window_group = window;
505 clientHints->flags |= WindowGroupHint;
506 XSetWMHints(dpy, window, clientHints);
509 if (haveCommand) {
510 command = GetCommandForWindow(window);
511 if (command) {
512 /* command already set. nothing to do. */
513 wfree(command);
514 } else {
515 XSetCommand(dpy, window, argv, argc);
519 /* Make sure we get notification when this window is destroyed */
520 if (XGetWindowAttributes(dpy, window, &attr))
521 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask);
525 XFree(classHint);
526 if (clientHints)
527 XFree(clientHints);
528 if (haveCommand)
529 wfree(argv);
532 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
534 XClassHint *classHint;
535 XWMHints *hints;
536 Window leader;
537 int argc;
538 char **argv;
540 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
541 /* set class hint */
542 classHint = XAllocClassHint();
543 classHint->res_name = instance;
544 classHint->res_class = class;
545 XSetClassHint(dpy, leader, classHint);
546 XFree(classHint);
548 /* inherit these from the original leader if available */
549 hints = XGetWMHints(dpy, win);
550 if (!hints) {
551 hints = XAllocWMHints();
552 hints->flags = 0;
554 /* set window group leader to self */
555 hints->window_group = leader;
556 hints->flags |= WindowGroupHint;
557 XSetWMHints(dpy, leader, hints);
558 XFree(hints);
560 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
561 XSetCommand(dpy, leader, argv, argc);
562 XFreeStringList(argv);
565 return leader;
568 static int matchIdentifier(const void *item, const void *cdata)
570 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
574 *----------------------------------------------------------------
575 * wManageWindow--
576 * reparents the window and allocates a descriptor for it.
577 * Window manager hints and other hints are fetched to configure
578 * the window decoration attributes and others. User preferences
579 * for the window are used if available, to configure window
580 * decorations and some behaviour.
581 * If in startup, windows that are override redirect,
582 * unmapped and never were managed and are Withdrawn are not
583 * managed.
585 * Returns:
586 * the new window descriptor
588 * Side effects:
589 * The window is reparented and appropriate notification
590 * is done to the client. Input mask for the window is setup.
591 * The window descriptor is also associated with various window
592 * contexts and inserted in the head of the window list.
593 * Event handler contexts are associated for some objects
594 * (buttons, titlebar and resizebar)
596 *----------------------------------------------------------------
598 WWindow *wManageWindow(WScreen *scr, Window window)
600 WWindow *wwin;
601 int x, y;
602 unsigned width, height;
603 XWindowAttributes wattribs;
604 XSetWindowAttributes attribs;
605 WWindowState *win_state;
606 WWindow *transientOwner = NULL;
607 int window_level;
608 int wm_state;
609 int foo;
610 int workspace = -1;
611 char *title;
612 Bool withdraw = False;
613 Bool raise = False;
615 /* mutex. */
616 XGrabServer(dpy);
617 XSync(dpy, False);
618 /* make sure the window is still there */
619 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
620 XUngrabServer(dpy);
621 return NULL;
624 /* if it's an override-redirect, ignore it */
625 if (wattribs.override_redirect) {
626 XUngrabServer(dpy);
627 return NULL;
630 wm_state = PropGetWindowState(window);
632 /* if it's startup and the window is unmapped, don't manage it */
633 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
634 XUngrabServer(dpy);
635 return NULL;
638 wwin = wWindowCreate();
640 title = wNETWMGetWindowName(window);
641 if (title)
642 wwin->flags.net_has_title = 1;
643 if (!title && !wFetchName(dpy, window, &title))
644 title = NULL;
646 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
648 #ifdef SHAPE
649 if (wShapeSupported) {
650 int junk;
651 unsigned int ujunk;
652 int b_shaped;
654 XShapeSelectInput(dpy, window, ShapeNotifyMask);
655 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
656 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
657 wwin->flags.shaped = b_shaped;
659 #endif
662 * Get hints and other information in properties
664 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
666 /* setup descriptor */
667 wwin->client_win = window;
668 wwin->screen_ptr = scr;
669 wwin->old_border_width = wattribs.border_width;
670 wwin->event_mask = CLIENT_EVENTS;
671 attribs.event_mask = CLIENT_EVENTS;
672 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
673 attribs.save_under = False;
675 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
676 XSetWindowBorderWidth(dpy, window, 0);
678 /* get hints from GNUstep app */
679 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0) {
680 wwin->flags.is_gnustep = 1;
682 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
683 wwin->wm_gnustep_attr = NULL;
686 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
687 wwin->flags.is_dockapp = 1;
688 withdraw = True;
691 wwin->client_leader = PropGetClientLeader(window);
692 if (wwin->client_leader != None)
693 wwin->main_window = wwin->client_leader;
695 wwin->wm_hints = XGetWMHints(dpy, window);
697 if (wwin->wm_hints) {
698 if (wwin->wm_hints->flags & StateHint) {
700 if (wwin->wm_hints->initial_state == IconicState) {
702 wwin->flags.miniaturized = 1;
704 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
706 wwin->flags.is_dockapp = 1;
707 withdraw = True;
711 if (wwin->wm_hints->flags & WindowGroupHint) {
712 wwin->group_id = wwin->wm_hints->window_group;
713 /* window_group has priority over CLIENT_LEADER */
714 wwin->main_window = wwin->group_id;
715 } else {
716 wwin->group_id = None;
719 if (wwin->wm_hints->flags & UrgencyHint) {
720 wwin->flags.urgent = 1;
721 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
723 } else {
724 wwin->group_id = None;
727 PropGetProtocols(window, &wwin->protocols);
729 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
730 wwin->transient_for = None;
731 } else {
732 if (wwin->transient_for == None || wwin->transient_for == window) {
733 wwin->transient_for = scr->root_win;
734 } else {
735 transientOwner = wWindowFor(wwin->transient_for);
736 if (transientOwner && transientOwner->main_window != None)
737 wwin->main_window = transientOwner->main_window;
741 /* guess the focus mode */
742 wwin->focus_mode = getFocusMode(wwin);
744 /* get geometry stuff */
745 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
747 /* get colormap windows */
748 GetColormapWindows(wwin);
751 * Setup the decoration/window attributes and
752 * geometry
754 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
756 /* Make broken apps behave as a nice app. */
757 if (WFLAGP(wwin, emulate_appicon))
758 wwin->main_window = wwin->client_win;
760 fixLeaderProperties(wwin);
762 wwin->orig_main_window = wwin->main_window;
764 if (wwin->flags.is_gnustep)
765 WSETUFLAG(wwin, shared_appicon, 0);
767 if (wwin->main_window) {
768 extern Atom _XA_WINDOWMAKER_MENU;
769 XTextProperty text_prop;
771 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
772 WSETUFLAG(wwin, shared_appicon, 0);
776 if (wwin->flags.is_dockapp)
777 WSETUFLAG(wwin, shared_appicon, 0);
779 if (wwin->main_window) {
780 WApplication *app = wApplicationOf(wwin->main_window);
781 if (app && app->app_icon)
782 WSETUFLAG(wwin, shared_appicon, 0);
785 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
786 char *buffer, *instance, *class;
787 WFakeGroupLeader *fPtr;
788 int index;
790 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
792 /* // only enter here if PropGetWMClass() succeds */
793 PropGetWMClass(wwin->main_window, &class, &instance);
794 buffer = StrConcatDot(instance, class);
796 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
797 if (index != WANotFound) {
798 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
799 if (fPtr->retainCount == 0) {
800 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
801 instance, class);
803 fPtr->retainCount++;
804 if (fPtr->origLeader == None) {
805 if (ADEQUATE(wwin->main_window)) {
806 fPtr->retainCount++;
807 fPtr->origLeader = wwin->main_window;
810 wwin->fake_group = fPtr;
811 /*wwin->group_id = fPtr->leader; */
812 wwin->main_window = fPtr->leader;
813 wfree(buffer);
814 } else {
815 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
817 fPtr->identifier = buffer;
818 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
819 fPtr->origLeader = None;
820 fPtr->retainCount = 1;
822 WMAddToArray(scr->fakeGroupLeaders, fPtr);
824 if (ADEQUATE(wwin->main_window)) {
825 fPtr->retainCount++;
826 fPtr->origLeader = wwin->main_window;
828 wwin->fake_group = fPtr;
829 /*wwin->group_id = fPtr->leader; */
830 wwin->main_window = fPtr->leader;
832 if (instance)
833 XFree(instance);
834 if (class)
835 XFree(class);
836 #undef ADEQUATE
840 * Setup the initial state of the window
842 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
843 wwin->flags.miniaturized = 1;
845 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
846 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
848 wNETWMCheckInitialClientState(wwin);
850 /* apply previous state if it exists and we're in startup */
851 if (scr->flags.startup && wm_state >= 0) {
853 if (wm_state == IconicState) {
855 wwin->flags.miniaturized = 1;
857 } else if (wm_state == WithdrawnState) {
859 withdraw = True;
863 /* if there is a saved state (from file), restore it */
864 win_state = NULL;
865 if (wwin->main_window != None)
866 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
867 else
868 win_state = (WWindowState *) wWindowGetSavedState(window);
870 if (win_state && !withdraw) {
871 if (win_state->state->hidden > 0)
872 wwin->flags.hidden = win_state->state->hidden;
874 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
875 wwin->flags.shaded = win_state->state->shaded;
877 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
878 wwin->flags.miniaturized = win_state->state->miniaturized;
881 if (!IS_OMNIPRESENT(wwin)) {
882 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
883 wwin->wm_class);
884 if (w < 0 || w >= scr->workspace_count) {
885 workspace = win_state->state->workspace;
886 if (workspace >= scr->workspace_count)
887 workspace = scr->current_workspace;
888 } else {
889 workspace = w;
891 } else {
892 workspace = scr->current_workspace;
896 /* if we're restarting, restore saved state (from hints).
897 * This will overwrite previous */
899 WSavedState *wstate;
901 if (getSavedState(window, &wstate)) {
902 wwin->flags.shaded = wstate->shaded;
903 wwin->flags.hidden = wstate->hidden;
904 wwin->flags.miniaturized = wstate->miniaturized;
905 wwin->flags.maximized = wstate->maximized;
906 if (wwin->flags.maximized) {
907 wwin->old_geometry.x = wstate->x;
908 wwin->old_geometry.y = wstate->y;
909 wwin->old_geometry.width = wstate->w;
910 wwin->old_geometry.height = wstate->h;
913 workspace = wstate->workspace;
914 } else {
915 wstate = NULL;
918 /* restore window shortcut */
919 if (wstate != NULL || win_state != NULL) {
920 unsigned mask = 0;
922 if (win_state != NULL)
923 mask = win_state->state->window_shortcuts;
925 if (wstate != NULL && mask == 0)
926 mask = wstate->window_shortcuts;
928 if (mask > 0) {
929 int i;
931 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
932 if (mask & (1 << i)) {
933 if (!scr->shortcutWindows[i])
934 scr->shortcutWindows[i] = WMCreateArray(4);
936 WMAddToArray(scr->shortcutWindows[i], wwin);
941 if (wstate != NULL)
942 wfree(wstate);
945 /* don't let transients start miniaturized if their owners are not */
946 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
947 wwin->flags.miniaturized = 0;
948 if (wwin->wm_hints)
949 wwin->wm_hints->initial_state = NormalState;
952 /* set workspace on which the window starts */
953 if (workspace >= 0) {
954 if (workspace > scr->workspace_count - 1) {
955 workspace = workspace % scr->workspace_count;
957 } else {
958 int w;
960 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
962 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
964 workspace = w;
966 } else {
967 if (wPreferences.open_transients_with_parent && transientOwner) {
969 workspace = transientOwner->frame->workspace;
971 } else {
973 workspace = scr->current_workspace;
978 /* setup window geometry */
979 if (win_state && win_state->state->w > 0) {
980 width = win_state->state->w;
981 height = win_state->state->h;
983 wWindowConstrainSize(wwin, &width, &height);
985 /* do not ask for window placement if the window is
986 * transient, during startup, if the initial workspace is another one
987 * or if the window wants to start iconic.
988 * If geometry was saved, restore it. */
990 Bool dontBring = False;
992 if (win_state && win_state->state->w > 0) {
993 x = win_state->state->x;
994 y = win_state->state->y;
995 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
996 && !scr->flags.startup
997 && workspace == scr->current_workspace
998 && !wwin->flags.miniaturized
999 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1001 if (transientOwner && transientOwner->flags.mapped) {
1002 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1003 WMRect rect;
1004 int head;
1006 x = transientOwner->frame_x +
1007 abs((transientOwner->frame->core->width - width) / 2) + offs;
1008 y = transientOwner->frame_y +
1009 abs((transientOwner->frame->core->height - height) / 3) + offs;
1012 * limit transient windows to be inside their parent's head
1014 rect.pos.x = transientOwner->frame_x;
1015 rect.pos.y = transientOwner->frame_y;
1016 rect.size.width = transientOwner->frame->core->width;
1017 rect.size.height = transientOwner->frame->core->height;
1019 head = wGetHeadForRect(scr, rect);
1020 rect = wGetRectForHead(scr, head);
1022 if (x < rect.pos.x)
1023 x = rect.pos.x;
1024 else if (x + width > rect.pos.x + rect.size.width)
1025 x = rect.pos.x + rect.size.width - width;
1027 if (y < rect.pos.y)
1028 y = rect.pos.y;
1029 else if (y + height > rect.pos.y + rect.size.height)
1030 y = rect.pos.y + rect.size.height - height;
1032 } else {
1033 PlaceWindow(wwin, &x, &y, width, height);
1035 if (wPreferences.window_placement == WPM_MANUAL) {
1036 dontBring = True;
1038 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1039 int head, flags;
1040 WMRect rect;
1041 int reposition = 0;
1044 * Make spash screens come out in the center of a head
1045 * trouble is that most splashies never get here
1046 * they are managed trough atoms but god knows where.
1047 * Dan, do you know ? -peter
1049 * Most of them are not managed, they have set
1050 * OverrideRedirect, which means we can't do anything about
1051 * them. -alfredo
1055 * xinerama checks for: across head and dead space
1057 rect.pos.x = x;
1058 rect.pos.y = y;
1059 rect.size.width = width;
1060 rect.size.height = height;
1062 head = wGetRectPlacementInfo(scr, rect, &flags);
1064 if (flags & XFLAG_DEAD)
1065 reposition = 1;
1067 if (flags & XFLAG_MULTIPLE)
1068 reposition = 2;
1071 switch (reposition) {
1072 case 1:
1073 head = wGetHeadForPointerLocation(scr);
1074 rect = wGetRectForHead(scr, head);
1076 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1077 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1078 break;
1080 case 2:
1081 rect = wGetRectForHead(scr, head);
1083 if (x < rect.pos.x)
1084 x = rect.pos.x;
1085 else if (x + width > rect.pos.x + rect.size.width)
1086 x = rect.pos.x + rect.size.width - width;
1088 if (y < rect.pos.y)
1089 y = rect.pos.y;
1090 else if (y + height > rect.pos.y + rect.size.height)
1091 y = rect.pos.y + rect.size.height - height;
1093 break;
1095 default:
1096 break;
1100 if (WFLAGP(wwin, dont_move_off) && dontBring)
1101 wScreenBringInside(scr, &x, &y, width, height);
1104 wNETWMPositionSplash(wwin, &x, &y, width, height);
1106 if (wwin->flags.urgent) {
1107 if (!IS_OMNIPRESENT(wwin))
1108 wwin->flags.omnipresent ^= 1;
1112 * Create frame, borders and do reparenting
1114 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1115 #ifdef XKB_BUTTON_HINT
1116 if (wPreferences.modelock)
1117 foo |= WFF_LANGUAGE_BUTTON;
1118 #endif
1119 if (HAS_TITLEBAR(wwin))
1120 foo |= WFF_TITLEBAR;
1121 if (HAS_RESIZEBAR(wwin))
1122 foo |= WFF_RESIZEBAR;
1123 if (HAS_BORDER(wwin))
1124 foo |= WFF_BORDER;
1126 wwin->frame = wFrameWindowCreate(scr, window_level,
1127 x, y, width, height,
1128 &wPreferences.window_title_clearance, foo,
1129 scr->window_title_texture,
1130 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1132 wwin->frame->flags.is_client_window_frame = 1;
1133 wwin->frame->flags.justification = wPreferences.title_justification;
1135 /* setup button images */
1136 wWindowUpdateButtonImages(wwin);
1138 /* hide unused buttons */
1139 foo = 0;
1140 if (WFLAGP(wwin, no_close_button))
1141 foo |= WFF_RIGHT_BUTTON;
1142 if (WFLAGP(wwin, no_miniaturize_button))
1143 foo |= WFF_LEFT_BUTTON;
1144 #ifdef XKB_BUTTON_HINT
1145 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1146 foo |= WFF_LANGUAGE_BUTTON;
1147 #endif
1148 if (foo != 0)
1149 wFrameWindowHideButton(wwin->frame, foo);
1151 wwin->frame->child = wwin;
1152 wwin->frame->workspace = workspace;
1153 wwin->frame->on_click_left = windowIconifyClick;
1155 #ifdef XKB_BUTTON_HINT
1156 if (wPreferences.modelock)
1157 wwin->frame->on_click_language = windowLanguageClick;
1158 #endif
1160 wwin->frame->on_click_right = windowCloseClick;
1161 wwin->frame->on_dblclick_right = windowCloseDblClick;
1162 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1163 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1164 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1166 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1167 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1168 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1171 int gx, gy;
1173 wClientGetGravityOffsets(wwin, &gx, &gy);
1175 /* if gravity is to the south, account for the border sizes */
1176 if (gy > 0)
1177 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1181 * wWindowConfigure() will init the client window's size
1182 * (wwin->client.{width,height}) and all other geometry
1183 * related variables (frame_x,frame_y)
1185 wWindowConfigure(wwin, x, y, width, height);
1187 /* to make sure the window receives it's new position after reparenting */
1188 wWindowSynthConfigureNotify(wwin);
1191 * Setup descriptors and save window to internal
1192 * lists
1194 if (wwin->main_window != None) {
1195 WApplication *app;
1196 WWindow *leader;
1198 /* Leader windows do not necessary set themselves as leaders.
1199 * If this is the case, point the leader of this window to
1200 * itself */
1201 leader = wWindowFor(wwin->main_window);
1202 if (leader && leader->main_window == None) {
1203 leader->main_window = leader->client_win;
1205 app = wApplicationCreate(wwin);
1206 if (app) {
1207 app->last_workspace = workspace;
1210 * Do application specific stuff, like setting application
1211 * wide attributes.
1214 if (wwin->flags.hidden) {
1215 /* if the window was set to hidden because it was hidden
1216 * in a previous incarnation and that state was restored */
1217 app->flags.hidden = 1;
1218 } else if (app->flags.hidden) {
1219 if (WFLAGP(app->main_window_desc, start_hidden)) {
1220 wwin->flags.hidden = 1;
1221 } else {
1222 wUnhideApplication(app, False, False);
1223 raise = True;
1226 wAppBounce(app);
1230 /* setup the frame descriptor */
1231 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1232 wwin->frame->core->descriptor.parent = wwin;
1233 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1235 /* don't let windows go away if we die */
1236 XAddToSaveSet(dpy, window);
1238 XLowerWindow(dpy, window);
1240 /* if window is in this workspace and should be mapped, then map it */
1241 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1242 && !wwin->flags.hidden && !withdraw) {
1244 /* The following "if" is to avoid crashing of clients that expect
1245 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1246 * after the return from this function.
1248 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1249 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1250 } else {
1251 wClientSetState(wwin, NormalState, None);
1254 #if 0
1255 /* if not auto focus, then map the window under the currently
1256 * focused window */
1257 #define _WIDTH(w) (w)->frame->core->width
1258 #define _HEIGHT(w) (w)->frame->core->height
1259 if (!wPreferences.auto_focus && scr->focused_window
1260 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1261 && (_WIDTH(wwin) >
1262 (_WIDTH(scr->focused_window) * 5) / 3
1263 || _HEIGHT(wwin) >
1264 (_HEIGHT(scr->focused_window) * 5) / 3)
1265 && WINDOW_LEVEL(scr->focused_window) ==
1266 WINDOW_LEVEL(wwin))
1267 || wwin->flags.maximized)) {
1268 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1270 #undef _WIDTH
1271 #undef _HEIGHT
1273 #endif
1275 if (wPreferences.superfluous && !wPreferences.no_animations
1276 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1278 * The brain damaged idiotic non-click to focus modes will
1279 * have trouble with this because:
1281 * 1. window is created and mapped by the client
1282 * 2. window is mapped by wmaker in small size
1283 * 3. window is animated to grow to normal size
1284 * 4. this function returns to normal event loop
1285 * 5. eventually, the EnterNotify event that would trigger
1286 * the window focusing (if the mouse is over that window)
1287 * will be processed by wmaker.
1288 * But since this event will be rather delayed
1289 * (step 3 has a large delay) the time when the event ocurred
1290 * and when it is processed, the client that owns that window
1291 * will reject the XSetInputFocus() for it.
1293 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1294 DoWindowBirth(wwin);
1297 wWindowMap(wwin);
1300 /* setup stacking descriptor */
1301 if (transientOwner) {
1302 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1303 } else {
1304 wwin->frame->core->stacking->child_of = NULL;
1307 if (!scr->focused_window) {
1308 /* first window on the list */
1309 wwin->next = NULL;
1310 wwin->prev = NULL;
1311 scr->focused_window = wwin;
1312 } else {
1313 WWindow *tmp;
1315 /* add window at beginning of focus window list */
1316 tmp = scr->focused_window;
1317 while (tmp->prev)
1318 tmp = tmp->prev;
1319 tmp->prev = wwin;
1320 wwin->next = tmp;
1321 wwin->prev = NULL;
1324 /* raise is set to true if we un-hid the app when this window was born.
1325 * we raise, else old windows of this app will be above this new one. */
1326 if (raise) {
1327 wRaiseFrame(wwin->frame->core);
1330 /* Update name must come after WApplication stuff is done */
1331 wWindowUpdateName(wwin, title);
1332 if (title)
1333 XFree(title);
1335 XUngrabServer(dpy);
1338 * Final preparations before window is ready to go
1340 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1342 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1343 if (((transientOwner && transientOwner->flags.focused)
1344 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1346 /* only auto_focus if on same screen as mouse
1347 * (and same head for xinerama mode)
1348 * TODO: make it an option */
1350 /*TODO add checking the head of the window, is it available? */
1351 short same_screen = 0, same_head = 1;
1353 int foo;
1354 unsigned int bar;
1355 Window dummy;
1357 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1358 &foo, &foo, &foo, &foo, &bar) != False) {
1359 same_screen = 1;
1362 if (same_screen == 1 && same_head == 1) {
1363 wSetFocusTo(scr, wwin);
1367 wWindowResetMouseGrabs(wwin);
1369 if (!WFLAGP(wwin, no_bind_keys))
1370 wWindowSetKeyGrabs(wwin);
1372 WMPostNotificationName(WMNManaged, wwin, NULL);
1373 wColormapInstallForWindow(scr, scr->cmap_window);
1375 /* Setup Notification Observers */
1376 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1378 /* Cleanup temporary stuff */
1379 if (win_state)
1380 wWindowDeleteSavedState(win_state);
1382 /* If the window must be withdrawed, then do it now.
1383 * Must do some optimization, 'though */
1384 if (withdraw) {
1385 wwin->flags.mapped = 0;
1386 wClientSetState(wwin, WithdrawnState, None);
1387 wUnmanageWindow(wwin, True, False);
1388 wwin = NULL;
1391 return wwin;
1394 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1395 char *title, int x, int y, int width, int height)
1397 WWindow *wwin;
1398 int foo;
1400 wwin = wWindowCreate();
1402 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1404 wwin->flags.internal_window = 1;
1406 WSETUFLAG(wwin, omnipresent, 1);
1407 WSETUFLAG(wwin, no_shadeable, 1);
1408 WSETUFLAG(wwin, no_resizable, 1);
1409 WSETUFLAG(wwin, no_miniaturizable, 1);
1411 wwin->focus_mode = WFM_PASSIVE;
1412 wwin->client_win = window;
1413 wwin->screen_ptr = scr;
1414 wwin->transient_for = owner;
1415 wwin->client.x = x;
1416 wwin->client.y = y;
1417 wwin->client.width = width;
1418 wwin->client.height = height;
1419 wwin->frame_x = wwin->client.x;
1420 wwin->frame_y = wwin->client.y;
1422 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1423 foo |= WFF_TITLEBAR;
1424 #ifdef XKB_BUTTON_HINT
1425 foo |= WFF_LANGUAGE_BUTTON;
1426 #endif
1428 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1429 wwin->frame_x, wwin->frame_y,
1430 width, height,
1431 &wPreferences.window_title_clearance, foo,
1432 scr->window_title_texture,
1433 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1435 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1437 wwin->frame->flags.is_client_window_frame = 1;
1438 wwin->frame->flags.justification = wPreferences.title_justification;
1440 wFrameWindowChangeTitle(wwin->frame, title);
1442 /* setup button images */
1443 wWindowUpdateButtonImages(wwin);
1445 /* hide buttons */
1446 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1448 wwin->frame->child = wwin;
1449 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1451 #ifdef XKB_BUTTON_HINT
1452 if (wPreferences.modelock)
1453 wwin->frame->on_click_language = windowLanguageClick;
1454 #endif
1456 wwin->frame->on_click_right = windowCloseClick;
1457 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1458 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1459 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1460 wwin->client.y += wwin->frame->top_width;
1462 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1463 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1465 /* setup the frame descriptor */
1466 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1467 wwin->frame->core->descriptor.parent = wwin;
1468 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1470 XLowerWindow(dpy, window);
1471 XMapSubwindows(dpy, wwin->frame->core->window);
1473 /* setup stacking descriptor */
1474 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1475 WWindow *tmp;
1476 tmp = wWindowFor(wwin->transient_for);
1477 if (tmp)
1478 wwin->frame->core->stacking->child_of = tmp->frame->core;
1479 } else {
1480 wwin->frame->core->stacking->child_of = NULL;
1483 if (!scr->focused_window) {
1484 /* first window on the list */
1485 wwin->next = NULL;
1486 wwin->prev = NULL;
1487 scr->focused_window = wwin;
1488 } else {
1489 WWindow *tmp;
1491 /* add window at beginning of focus window list */
1492 tmp = scr->focused_window;
1493 while (tmp->prev)
1494 tmp = tmp->prev;
1495 tmp->prev = wwin;
1496 wwin->next = tmp;
1497 wwin->prev = NULL;
1500 if (wwin->flags.is_gnustep == 0)
1501 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1503 /* if (wPreferences.auto_focus) */
1504 wSetFocusTo(scr, wwin);
1505 wWindowResetMouseGrabs(wwin);
1506 wWindowSetKeyGrabs(wwin);
1508 return wwin;
1512 *----------------------------------------------------------------------
1513 * wUnmanageWindow--
1514 * Removes the frame window from a window and destroys all data
1515 * related to it. The window will be reparented back to the root window
1516 * if restore is True.
1518 * Side effects:
1519 * Everything related to the window is destroyed and the window
1520 * is removed from the window lists. Focus is set to the previous on the
1521 * window list.
1522 *----------------------------------------------------------------------
1524 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1526 WCoreWindow *frame = wwin->frame->core;
1527 WWindow *owner = NULL;
1528 WWindow *newFocusedWindow = NULL;
1529 int wasFocused;
1530 WScreen *scr = wwin->screen_ptr;
1532 /* First close attribute editor window if open */
1533 if (wwin->flags.inspector_open) {
1534 wCloseInspectorForWindow(wwin);
1537 /* Close window menu if it's open for this window */
1538 if (wwin->flags.menu_open_for_me) {
1539 CloseWindowMenu(scr);
1542 if (!destroyed) {
1543 if (!wwin->flags.internal_window)
1544 XRemoveFromSaveSet(dpy, wwin->client_win);
1546 XSelectInput(dpy, wwin->client_win, NoEventMask);
1548 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1549 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1552 XUnmapWindow(dpy, frame->window);
1554 XUnmapWindow(dpy, wwin->client_win);
1556 /* deselect window */
1557 wSelectWindow(wwin, False);
1559 /* remove all pending events on window */
1560 /* I think this only matters for autoraise */
1561 if (wPreferences.raise_delay)
1562 WMDeleteTimerWithClientData(wwin->frame->core);
1564 XFlush(dpy);
1566 /* reparent the window back to the root */
1567 if (restore)
1568 wClientRestore(wwin);
1570 if (wwin->transient_for != scr->root_win) {
1571 owner = wWindowFor(wwin->transient_for);
1572 if (owner) {
1573 if (!owner->flags.semi_focused) {
1574 owner = NULL;
1575 } else {
1576 owner->flags.semi_focused = 0;
1581 wasFocused = wwin->flags.focused;
1583 /* remove from window focus list */
1584 if (!wwin->prev && !wwin->next) {
1585 /* was the only window */
1586 scr->focused_window = NULL;
1587 newFocusedWindow = NULL;
1588 } else {
1589 WWindow *tmp;
1591 if (wwin->prev)
1592 wwin->prev->next = wwin->next;
1593 if (wwin->next)
1594 wwin->next->prev = wwin->prev;
1595 else {
1596 scr->focused_window = wwin->prev;
1597 scr->focused_window->next = NULL;
1600 if (wPreferences.focus_mode == WKF_CLICK) {
1602 /* if in click to focus mode and the window
1603 * was a transient, focus the owner window
1605 tmp = NULL;
1606 if (wPreferences.focus_mode == WKF_CLICK) {
1607 tmp = wWindowFor(wwin->transient_for);
1608 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1609 tmp = NULL;
1612 /* otherwise, focus the next one in the focus list */
1613 if (!tmp) {
1614 tmp = scr->focused_window;
1615 while (tmp) { /* look for one in the window list first */
1616 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1617 && (tmp->flags.mapped || tmp->flags.shaded))
1618 break;
1619 tmp = tmp->prev;
1621 if (!tmp) { /* if unsuccessful, choose any focusable window */
1622 tmp = scr->focused_window;
1623 while (tmp) {
1624 if (!WFLAGP(tmp, no_focusable)
1625 && (tmp->flags.mapped || tmp->flags.shaded))
1626 break;
1627 tmp = tmp->prev;
1632 newFocusedWindow = tmp;
1634 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1635 unsigned int mask;
1636 int foo;
1637 Window bar, win;
1639 /* This is to let the root window get the keyboard input
1640 * if Sloppy focus mode and no other window get focus.
1641 * This way keybindings will not freeze.
1643 tmp = NULL;
1644 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1645 tmp = wWindowFor(win);
1646 if (tmp == wwin)
1647 tmp = NULL;
1648 newFocusedWindow = tmp;
1649 } else {
1650 newFocusedWindow = NULL;
1654 if (!wwin->flags.internal_window)
1655 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1656 if (wasFocused) {
1657 if (newFocusedWindow != owner && owner) {
1658 if (wwin->flags.is_gnustep == 0)
1659 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1661 wSetFocusTo(scr, newFocusedWindow);
1664 /* Close menu and unhighlight */
1665 WApplication *oapp = wApplicationOf(wwin->main_window);
1666 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1667 if (oapp && oapp != napp) {
1668 wAppMenuUnmap(oapp->menu);
1669 wApplicationDeactivate(oapp);
1672 wWindowDestroy(wwin);
1673 XFlush(dpy);
1676 void wWindowMap(WWindow *wwin)
1678 XMapWindow(dpy, wwin->frame->core->window);
1679 if (!wwin->flags.shaded) {
1680 /* window will be remapped when getting MapNotify */
1681 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1682 XMapWindow(dpy, wwin->client_win);
1683 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1685 wwin->flags.mapped = 1;
1689 void wWindowUnmap(WWindow *wwin)
1691 wwin->flags.mapped = 0;
1693 /* prevent window withdrawal when getting UnmapNotify */
1694 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1695 XUnmapWindow(dpy, wwin->client_win);
1696 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1698 XUnmapWindow(dpy, wwin->frame->core->window);
1701 void wWindowFocus(WWindow *wwin, WWindow *owin)
1703 WWindow *nowner;
1704 WWindow *oowner;
1706 #ifdef KEEP_XKB_LOCK_STATUS
1707 if (wPreferences.modelock) {
1708 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1710 #endif /* KEEP_XKB_LOCK_STATUS */
1712 wwin->flags.semi_focused = 0;
1714 if (wwin->flags.is_gnustep == 0)
1715 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1717 wwin->flags.focused = 1;
1719 wWindowResetMouseGrabs(wwin);
1721 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1723 if (owin == wwin || !owin)
1724 return;
1726 nowner = wWindowFor(wwin->transient_for);
1728 /* new window is a transient for the old window */
1729 if (nowner == owin) {
1730 owin->flags.semi_focused = 1;
1731 wWindowUnfocus(nowner);
1732 return;
1735 oowner = wWindowFor(owin->transient_for);
1737 /* new window is owner of old window */
1738 if (wwin == oowner) {
1739 wWindowUnfocus(owin);
1740 return;
1743 if (!nowner) {
1744 wWindowUnfocus(owin);
1745 return;
1748 /* new window has same owner of old window */
1749 if (oowner == nowner) {
1750 /* prevent unfocusing of owner */
1751 oowner->flags.semi_focused = 0;
1752 wWindowUnfocus(owin);
1753 oowner->flags.semi_focused = 1;
1755 return;
1758 /* nowner != NULL && oowner != nowner */
1759 nowner->flags.semi_focused = 1;
1760 wWindowUnfocus(nowner);
1761 wWindowUnfocus(owin);
1764 void wWindowUnfocus(WWindow *wwin)
1766 CloseWindowMenu(wwin->screen_ptr);
1768 if (wwin->flags.is_gnustep == 0)
1769 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1771 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1772 WWindow *owner;
1773 owner = wWindowFor(wwin->transient_for);
1774 if (owner && owner->flags.semi_focused) {
1775 owner->flags.semi_focused = 0;
1776 if (owner->flags.mapped || owner->flags.shaded) {
1777 wWindowUnfocus(owner);
1778 wFrameWindowPaint(owner->frame);
1782 wwin->flags.focused = 0;
1783 wWindowResetMouseGrabs(wwin);
1784 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1787 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1789 char *title;
1791 if (!wwin->frame)
1792 return;
1794 wwin->flags.wm_name_changed = 1;
1796 if (!newTitle) {
1797 /* the hint was removed */
1798 title = DEF_WINDOW_TITLE;
1799 } else {
1800 title = newTitle;
1803 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1804 WMPostNotificationName(WMNChangedName, wwin, NULL);
1809 *----------------------------------------------------------------------
1811 * wWindowConstrainSize--
1812 * Constrains size for the client window, taking the maximal size,
1813 * window resize increments and other size hints into account.
1815 * Returns:
1816 * The closest size to what was given that the client window can
1817 * have.
1819 *----------------------------------------------------------------------
1821 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1823 int width = (int)*nwidth;
1824 int height = (int)*nheight;
1825 int winc = 1;
1826 int hinc = 1;
1827 int minW = 1, minH = 1;
1828 int maxW = wwin->screen_ptr->scr_width * 2;
1829 int maxH = wwin->screen_ptr->scr_height * 2;
1830 int minAX = -1, minAY = -1;
1831 int maxAX = -1, maxAY = -1;
1832 int baseW = 0;
1833 int baseH = 0;
1835 if (wwin->normal_hints) {
1836 winc = wwin->normal_hints->width_inc;
1837 hinc = wwin->normal_hints->height_inc;
1838 minW = wwin->normal_hints->min_width;
1839 minH = wwin->normal_hints->min_height;
1840 maxW = wwin->normal_hints->max_width;
1841 maxH = wwin->normal_hints->max_height;
1842 if (wwin->normal_hints->flags & PAspect) {
1843 minAX = wwin->normal_hints->min_aspect.x;
1844 minAY = wwin->normal_hints->min_aspect.y;
1845 maxAX = wwin->normal_hints->max_aspect.x;
1846 maxAY = wwin->normal_hints->max_aspect.y;
1849 baseW = wwin->normal_hints->base_width;
1850 baseH = wwin->normal_hints->base_height;
1853 if (width < minW)
1854 width = minW;
1855 if (height < minH)
1856 height = minH;
1858 if (width > maxW)
1859 width = maxW;
1860 if (height > maxH)
1861 height = maxH;
1863 /* aspect ratio code borrowed from olwm */
1864 if (minAX > 0) {
1865 /* adjust max aspect ratio */
1866 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1867 if (maxAX > maxAY) {
1868 height = (width * maxAY) / maxAX;
1869 if (height > maxH) {
1870 height = maxH;
1871 width = (height * maxAX) / maxAY;
1873 } else {
1874 width = (height * maxAX) / maxAY;
1875 if (width > maxW) {
1876 width = maxW;
1877 height = (width * maxAY) / maxAX;
1882 /* adjust min aspect ratio */
1883 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1884 if (minAX > minAY) {
1885 height = (width * minAY) / minAX;
1886 if (height < minH) {
1887 height = minH;
1888 width = (height * minAX) / minAY;
1890 } else {
1891 width = (height * minAX) / minAY;
1892 if (width < minW) {
1893 width = minW;
1894 height = (width * minAY) / minAX;
1900 if (baseW != 0)
1901 width = (((width - baseW) / winc) * winc) + baseW;
1902 else
1903 width = (((width - minW) / winc) * winc) + minW;
1905 if (baseH != 0)
1906 height = (((height - baseH) / hinc) * hinc) + baseH;
1907 else
1908 height = (((height - minH) / hinc) * hinc) + minH;
1910 /* broken stupid apps may cause preposterous values for these.. */
1911 if (width > 0)
1912 *nwidth = width;
1913 if (height > 0)
1914 *nheight = height;
1917 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1918 unsigned int *width, unsigned int *height)
1920 int baseW = 0, baseH = 0;
1921 int winc = 1, hinc = 1;
1923 if (wwin->normal_hints) {
1924 baseW = wwin->normal_hints->base_width;
1925 baseH = wwin->normal_hints->base_height;
1927 winc = wwin->normal_hints->width_inc;
1928 hinc = wwin->normal_hints->height_inc;
1931 if (*width > maxW)
1932 *width = maxW - (maxW - baseW) % winc;
1934 if (*height > maxH)
1935 *height = maxH - (maxH - baseH) % hinc;
1938 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1940 WScreen *scr = wwin->screen_ptr;
1941 WApplication *wapp;
1942 int unmap = 0;
1944 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1945 return;
1947 if (workspace != scr->current_workspace) {
1948 /* Sent to other workspace. Unmap window */
1949 if ((wwin->flags.mapped
1950 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1951 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1953 wapp = wApplicationOf(wwin->main_window);
1954 if (wapp) {
1955 wapp->last_workspace = workspace;
1957 if (wwin->flags.miniaturized) {
1958 if (wwin->icon) {
1959 XUnmapWindow(dpy, wwin->icon->core->window);
1960 wwin->icon->mapped = 0;
1962 } else {
1963 unmap = 1;
1964 wSetFocusTo(scr, NULL);
1967 } else {
1968 /* brought to current workspace. Map window */
1969 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1970 if (wwin->icon) {
1971 XMapWindow(dpy, wwin->icon->core->window);
1972 wwin->icon->mapped = 1;
1974 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1975 wWindowMap(wwin);
1978 if (!IS_OMNIPRESENT(wwin)) {
1979 int oldWorkspace = wwin->frame->workspace;
1981 wwin->frame->workspace = workspace;
1983 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1986 if (unmap)
1987 wWindowUnmap(wwin);
1990 void wWindowSynthConfigureNotify(WWindow *wwin)
1992 XEvent sevent;
1994 sevent.type = ConfigureNotify;
1995 sevent.xconfigure.display = dpy;
1996 sevent.xconfigure.event = wwin->client_win;
1997 sevent.xconfigure.window = wwin->client_win;
1999 sevent.xconfigure.x = wwin->client.x;
2000 sevent.xconfigure.y = wwin->client.y;
2001 sevent.xconfigure.width = wwin->client.width;
2002 sevent.xconfigure.height = wwin->client.height;
2004 sevent.xconfigure.border_width = wwin->old_border_width;
2005 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2006 sevent.xconfigure.above = wwin->frame->titlebar->window;
2007 else
2008 sevent.xconfigure.above = None;
2010 sevent.xconfigure.override_redirect = False;
2011 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2012 XFlush(dpy);
2016 *----------------------------------------------------------------------
2017 * wWindowConfigure()
2019 * req_x, req_y: new requested positions for the frame
2020 * req_width, req_height: new requested sizes for the client
2022 * Configures the frame, decorations and client window to the specified
2023 * geometry, whose validity is not checked -- wWindowConstrainSize()
2024 * must be used for that.
2025 * The size parameters are for the client window, but the position is
2026 * for the frame.
2027 * The client window receives a ConfigureNotify event, according
2028 * to what ICCCM says.
2030 * Returns:
2031 * None
2033 * Side effects:
2034 * Window size and position are changed and client window receives
2035 * a ConfigureNotify event.
2036 *----------------------------------------------------------------------
2038 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2040 int synth_notify = False;
2041 int resize;
2043 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2045 * if the window is being moved but not resized then
2046 * send a synthetic ConfigureNotify
2048 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2049 synth_notify = True;
2052 if (WFLAGP(wwin, dont_move_off))
2053 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2054 if (resize) {
2055 if (req_width < MIN_WINDOW_SIZE)
2056 req_width = MIN_WINDOW_SIZE;
2057 if (req_height < MIN_WINDOW_SIZE)
2058 req_height = MIN_WINDOW_SIZE;
2060 /* If growing, resize inner part before frame,
2061 * if shrinking, resize frame before.
2062 * This will prevent the frame (that can have a different color)
2063 * to be exposed, causing flicker */
2064 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2065 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2067 if (wwin->flags.shaded) {
2068 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2069 wwin->old_geometry.height = req_height;
2070 } else {
2071 int h;
2073 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2075 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2078 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2079 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2081 wwin->client.x = req_x;
2082 wwin->client.y = req_y + wwin->frame->top_width;
2083 wwin->client.width = req_width;
2084 wwin->client.height = req_height;
2085 } else {
2086 wwin->client.x = req_x;
2087 wwin->client.y = req_y + wwin->frame->top_width;
2089 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2091 wwin->frame_x = req_x;
2092 wwin->frame_y = req_y;
2093 if (HAS_BORDER(wwin)) {
2094 wwin->client.x += FRAME_BORDER_WIDTH;
2095 wwin->client.y += FRAME_BORDER_WIDTH;
2097 #ifdef SHAPE
2098 if (wShapeSupported && wwin->flags.shaped && resize) {
2099 wWindowSetShape(wwin);
2101 #endif
2103 if (synth_notify)
2104 wWindowSynthConfigureNotify(wwin);
2105 XFlush(dpy);
2108 /* req_x, req_y: new position of the frame */
2109 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2111 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2112 int synth_notify = False;
2114 /* Send a synthetic ConfigureNotify event for every window movement. */
2115 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2116 synth_notify = True;
2118 #else
2119 /* A single synthetic ConfigureNotify event is sent at the end of
2120 * a completed (opaque) movement in moveres.c */
2121 #endif
2123 if (WFLAGP(wwin, dont_move_off))
2124 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2125 wwin->frame->core->width, wwin->frame->core->height);
2127 wwin->client.x = req_x;
2128 wwin->client.y = req_y + wwin->frame->top_width;
2129 if (HAS_BORDER(wwin)) {
2130 wwin->client.x += FRAME_BORDER_WIDTH;
2131 wwin->client.y += FRAME_BORDER_WIDTH;
2134 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2136 wwin->frame_x = req_x;
2137 wwin->frame_y = req_y;
2139 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2140 if (synth_notify)
2141 wWindowSynthConfigureNotify(wwin);
2142 #endif
2145 void wWindowUpdateButtonImages(WWindow *wwin)
2147 WScreen *scr = wwin->screen_ptr;
2148 Pixmap pixmap, mask;
2149 WFrameWindow *fwin = wwin->frame;
2151 if (!HAS_TITLEBAR(wwin))
2152 return;
2154 /* miniaturize button */
2155 if (!WFLAGP(wwin, no_miniaturize_button)) {
2156 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2157 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2159 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2160 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2161 } else {
2162 mask = None;
2165 if (fwin->lbutton_image
2166 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2167 wPixmapDestroy(fwin->lbutton_image);
2168 fwin->lbutton_image = NULL;
2171 if (!fwin->lbutton_image) {
2172 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2173 fwin->lbutton_image->client_owned = 1;
2174 fwin->lbutton_image->client_owned_mask = 1;
2176 } else {
2177 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2178 wPixmapDestroy(fwin->lbutton_image);
2180 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2183 #ifdef XKB_BUTTON_HINT
2184 if (!WFLAGP(wwin, no_language_button)) {
2185 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2186 wPixmapDestroy(fwin->languagebutton_image);
2188 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2190 #endif
2192 /* close button */
2194 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2195 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2197 if (!WFLAGP(wwin, no_close_button)) {
2198 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2199 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2201 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2202 mask = wwin->wm_gnustep_attr->close_mask;
2203 else
2204 mask = None;
2206 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2207 || fwin->rbutton_image->mask != mask)) {
2208 wPixmapDestroy(fwin->rbutton_image);
2209 fwin->rbutton_image = NULL;
2212 if (!fwin->rbutton_image) {
2213 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2214 fwin->rbutton_image->client_owned = 1;
2215 fwin->rbutton_image->client_owned_mask = 1;
2218 } else if (WFLAGP(wwin, kill_close)) {
2220 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2221 wPixmapDestroy(fwin->rbutton_image);
2223 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2225 } else if (MGFLAGP(wwin, broken_close)) {
2227 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2228 wPixmapDestroy(fwin->rbutton_image);
2230 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2232 } else {
2234 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2235 wPixmapDestroy(fwin->rbutton_image);
2237 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2241 /* force buttons to be redrawn */
2242 fwin->flags.need_texture_change = 1;
2243 wFrameWindowPaint(fwin);
2247 *---------------------------------------------------------------------------
2248 * wWindowConfigureBorders--
2249 * Update window border configuration according to attribute flags.
2251 *---------------------------------------------------------------------------
2253 void wWindowConfigureBorders(WWindow *wwin)
2255 if (wwin->frame) {
2256 int flags;
2257 int newy, oldh;
2259 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2261 #ifdef XKB_BUTTON_HINT
2262 if (wPreferences.modelock)
2263 flags |= WFF_LANGUAGE_BUTTON;
2264 #endif
2266 if (HAS_TITLEBAR(wwin))
2267 flags |= WFF_TITLEBAR;
2268 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2269 flags |= WFF_RESIZEBAR;
2270 if (HAS_BORDER(wwin))
2271 flags |= WFF_BORDER;
2272 if (wwin->flags.shaded)
2273 flags |= WFF_IS_SHADED;
2275 oldh = wwin->frame->top_width;
2276 wFrameWindowUpdateBorders(wwin->frame, flags);
2277 if (oldh != wwin->frame->top_width) {
2278 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2280 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2281 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2284 flags = 0;
2285 if (!WFLAGP(wwin, no_miniaturize_button)
2286 && wwin->frame->flags.hide_left_button)
2287 flags |= WFF_LEFT_BUTTON;
2289 #ifdef XKB_BUTTON_HINT
2290 if (!WFLAGP(wwin, no_language_button)
2291 && wwin->frame->flags.hide_language_button) {
2292 flags |= WFF_LANGUAGE_BUTTON;
2294 #endif
2296 if (!WFLAGP(wwin, no_close_button)
2297 && wwin->frame->flags.hide_right_button)
2298 flags |= WFF_RIGHT_BUTTON;
2300 if (flags != 0) {
2301 wWindowUpdateButtonImages(wwin);
2302 wFrameWindowShowButton(wwin->frame, flags);
2305 flags = 0;
2306 if (WFLAGP(wwin, no_miniaturize_button)
2307 && !wwin->frame->flags.hide_left_button)
2308 flags |= WFF_LEFT_BUTTON;
2310 #ifdef XKB_BUTTON_HINT
2311 if (WFLAGP(wwin, no_language_button)
2312 && !wwin->frame->flags.hide_language_button)
2313 flags |= WFF_LANGUAGE_BUTTON;
2314 #endif
2316 if (WFLAGP(wwin, no_close_button)
2317 && !wwin->frame->flags.hide_right_button)
2318 flags |= WFF_RIGHT_BUTTON;
2320 if (flags != 0)
2321 wFrameWindowHideButton(wwin->frame, flags);
2323 #ifdef SHAPE
2324 if (wShapeSupported && wwin->flags.shaped) {
2325 wWindowSetShape(wwin);
2327 #endif
2331 void wWindowSaveState(WWindow * wwin)
2333 long data[10];
2334 int i;
2336 memset(data, 0, sizeof(long) * 10);
2337 data[0] = wwin->frame->workspace;
2338 data[1] = wwin->flags.miniaturized;
2339 data[2] = wwin->flags.shaded;
2340 data[3] = wwin->flags.hidden;
2341 data[4] = wwin->flags.maximized;
2342 if (wwin->flags.maximized == 0) {
2343 data[5] = wwin->frame_x;
2344 data[6] = wwin->frame_y;
2345 data[7] = wwin->frame->core->width;
2346 data[8] = wwin->frame->core->height;
2347 } else {
2348 data[5] = wwin->old_geometry.x;
2349 data[6] = wwin->old_geometry.y;
2350 data[7] = wwin->old_geometry.width;
2351 data[8] = wwin->old_geometry.height;
2354 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2355 if (wwin->screen_ptr->shortcutWindows[i] &&
2356 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2357 data[9] |= 1 << i;
2359 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2360 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2363 static int getSavedState(Window window, WSavedState ** state)
2365 Atom type_ret;
2366 int fmt_ret;
2367 unsigned long nitems_ret;
2368 unsigned long bytes_after_ret;
2369 long *data;
2371 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2372 True, _XA_WINDOWMAKER_STATE,
2373 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2374 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2375 return 0;
2377 *state = wmalloc(sizeof(WSavedState));
2379 (*state)->workspace = data[0];
2380 (*state)->miniaturized = data[1];
2381 (*state)->shaded = data[2];
2382 (*state)->hidden = data[3];
2383 (*state)->maximized = data[4];
2384 (*state)->x = data[5];
2385 (*state)->y = data[6];
2386 (*state)->w = data[7];
2387 (*state)->h = data[8];
2388 (*state)->window_shortcuts = data[9];
2390 XFree(data);
2392 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2393 return 1;
2394 else
2395 return 0;
2398 #ifdef SHAPE
2399 void wWindowClearShape(WWindow * wwin)
2401 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2402 0, wwin->frame->top_width, None, ShapeSet);
2403 XFlush(dpy);
2406 void wWindowSetShape(WWindow * wwin)
2408 XRectangle rect[2];
2409 int count;
2410 #ifdef OPTIMIZE_SHAPE
2411 XRectangle *rects;
2412 XRectangle *urec;
2413 int ordering;
2415 /* only shape is the client's */
2416 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2417 goto alt_code;
2420 /* Get array of rectangles describing the shape mask */
2421 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2422 if (!rects) {
2423 goto alt_code;
2426 urec = malloc(sizeof(XRectangle) * (count + 2));
2427 if (!urec) {
2428 XFree(rects);
2429 goto alt_code;
2432 /* insert our decoration rectangles in the rect list */
2433 memcpy(urec, rects, sizeof(XRectangle) * count);
2434 XFree(rects);
2436 if (HAS_TITLEBAR(wwin)) {
2437 urec[count].x = -1;
2438 urec[count].y = -1 - wwin->frame->top_width;
2439 urec[count].width = wwin->frame->core->width + 2;
2440 urec[count].height = wwin->frame->top_width + 1;
2441 count++;
2443 if (HAS_RESIZEBAR(wwin)) {
2444 urec[count].x = -1;
2445 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2446 urec[count].width = wwin->frame->core->width + 2;
2447 urec[count].height = wwin->frame->bottom_width + 1;
2448 count++;
2451 /* shape our frame window */
2452 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2453 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2454 XFlush(dpy);
2455 wfree(urec);
2456 return;
2458 alt_code:
2459 #endif /* OPTIMIZE_SHAPE */
2460 count = 0;
2461 if (HAS_TITLEBAR(wwin)) {
2462 rect[count].x = -1;
2463 rect[count].y = -1;
2464 rect[count].width = wwin->frame->core->width + 2;
2465 rect[count].height = wwin->frame->top_width + 1;
2466 count++;
2468 if (HAS_RESIZEBAR(wwin)) {
2469 rect[count].x = -1;
2470 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2471 rect[count].width = wwin->frame->core->width + 2;
2472 rect[count].height = wwin->frame->bottom_width + 1;
2473 count++;
2475 if (count > 0) {
2476 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2477 0, 0, rect, count, ShapeSet, Unsorted);
2479 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2480 0, wwin->frame->top_width, wwin->client_win,
2481 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2482 XFlush(dpy);
2484 #endif /* SHAPE */
2486 /* ====================================================================== */
2488 static FocusMode getFocusMode(WWindow * wwin)
2490 FocusMode mode;
2492 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2493 if (wwin->wm_hints->input == True) {
2494 if (wwin->protocols.TAKE_FOCUS)
2495 mode = WFM_LOCALLY_ACTIVE;
2496 else
2497 mode = WFM_PASSIVE;
2498 } else {
2499 if (wwin->protocols.TAKE_FOCUS)
2500 mode = WFM_GLOBALLY_ACTIVE;
2501 else
2502 mode = WFM_NO_INPUT;
2504 } else {
2505 mode = WFM_PASSIVE;
2507 return mode;
2510 void wWindowSetKeyGrabs(WWindow * wwin)
2512 int i;
2513 WShortKey *key;
2515 for (i = 0; i < WKBD_LAST; i++) {
2516 key = &wKeyBindings[i];
2518 if (key->keycode == 0)
2519 continue;
2520 if (key->modifier != AnyModifier) {
2521 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2522 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2523 #ifdef NUMLOCK_HACK
2524 /* Also grab all modifier combinations possible that include,
2525 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2526 * work even if the NumLock/ScrollLock key is on.
2528 wHackedGrabKey(key->keycode, key->modifier,
2529 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2530 #endif
2532 XGrabKey(dpy, key->keycode, key->modifier,
2533 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2536 wRootMenuBindShortcuts(wwin->frame->core->window);
2539 void wWindowResetMouseGrabs(WWindow * wwin)
2541 /* Mouse grabs can't be done on the client window because of
2542 * ICCCM and because clients that try to do the same will crash.
2544 * But there is a problem wich makes tbar buttons of unfocused
2545 * windows not usable as the click goes to the frame window instead
2546 * of the button itself. Must figure a way to fix that.
2549 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2551 if (!WFLAGP(wwin, no_bind_mouse)) {
2552 /* grabs for Meta+drag */
2553 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2554 True, ButtonPressMask | ButtonReleaseMask,
2555 GrabModeSync, GrabModeAsync, None, None);
2557 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2558 * but we only grab it for Button4 and Button 5 since a lot of apps
2559 * use CTRL+Button1-3 for app related functionality
2561 if (wPreferences.resize_increment > 0) {
2562 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2563 True, ButtonPressMask | ButtonReleaseMask,
2564 GrabModeSync, GrabModeAsync, None, None);
2565 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2566 True, ButtonPressMask | ButtonReleaseMask,
2567 GrabModeSync, GrabModeAsync, None, None);
2569 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2570 True, ButtonPressMask | ButtonReleaseMask,
2571 GrabModeSync, GrabModeAsync, None, None);
2572 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2573 True, ButtonPressMask | ButtonReleaseMask,
2574 GrabModeSync, GrabModeAsync, None, None);
2578 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2579 && !wwin->flags.is_gnustep) {
2580 /* the passive grabs to focus the window */
2581 /* if (wPreferences.focus_mode == WKF_CLICK) */
2582 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2583 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2585 XFlush(dpy);
2588 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2590 if (attr->flags & GSExtraFlagsAttr) {
2591 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2592 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2593 wWindowUpdateButtonImages(wwin);
2598 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2600 WWindowState *wstate;
2602 wstate = malloc(sizeof(WWindowState));
2603 if (!wstate)
2604 return NULL;
2606 memset(wstate, 0, sizeof(WWindowState));
2607 wstate->pid = pid;
2608 if (instance)
2609 wstate->instance = wstrdup(instance);
2610 if (class)
2611 wstate->class = wstrdup(class);
2612 if (command)
2613 wstate->command = wstrdup(command);
2614 wstate->state = state;
2616 wstate->next = windowState;
2617 windowState = wstate;
2619 return wstate;
2622 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2624 WMagicNumber wWindowGetSavedState(Window win)
2626 char *instance, *class, *command = NULL;
2627 WWindowState *wstate = windowState;
2629 if (!wstate)
2630 return NULL;
2632 command = GetCommandForWindow(win);
2633 if (!command)
2634 return NULL;
2636 if (PropGetWMClass(win, &class, &instance)) {
2637 while (wstate) {
2638 if (SAME(instance, wstate->instance) &&
2639 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2640 break;
2642 wstate = wstate->next;
2644 } else {
2645 wstate = NULL;
2648 if (command)
2649 wfree(command);
2650 if (instance)
2651 XFree(instance);
2652 if (class)
2653 XFree(class);
2655 return wstate;
2658 void wWindowDeleteSavedState(WMagicNumber id)
2660 WWindowState *tmp, *wstate = (WWindowState *) id;
2662 if (!wstate || !windowState)
2663 return;
2665 tmp = windowState;
2666 if (tmp == wstate) {
2667 windowState = wstate->next;
2668 if (wstate->instance)
2669 wfree(wstate->instance);
2670 if (wstate->class)
2671 wfree(wstate->class);
2672 if (wstate->command)
2673 wfree(wstate->command);
2674 wfree(wstate->state);
2675 wfree(wstate);
2676 } else {
2677 while (tmp->next) {
2678 if (tmp->next == wstate) {
2679 tmp->next = wstate->next;
2680 if (wstate->instance)
2681 wfree(wstate->instance);
2682 if (wstate->class)
2683 wfree(wstate->class);
2684 if (wstate->command)
2685 wfree(wstate->command);
2686 wfree(wstate->state);
2687 wfree(wstate);
2688 break;
2690 tmp = tmp->next;
2695 void wWindowDeleteSavedStatesForPID(pid_t pid)
2697 WWindowState *tmp, *wstate;
2699 if (!windowState)
2700 return;
2702 tmp = windowState;
2703 if (tmp->pid == pid) {
2704 wstate = windowState;
2705 windowState = tmp->next;
2706 if (wstate->instance)
2707 wfree(wstate->instance);
2708 if (wstate->class)
2709 wfree(wstate->class);
2710 if (wstate->command)
2711 wfree(wstate->command);
2712 wfree(wstate->state);
2713 wfree(wstate);
2714 } else {
2715 while (tmp->next) {
2716 if (tmp->next->pid == pid) {
2717 wstate = tmp->next;
2718 tmp->next = wstate->next;
2719 if (wstate->instance)
2720 wfree(wstate->instance);
2721 if (wstate->class)
2722 wfree(wstate->class);
2723 if (wstate->command)
2724 wfree(wstate->command);
2725 wfree(wstate->state);
2726 wfree(wstate);
2727 break;
2729 tmp = tmp->next;
2734 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2736 if (wwin->flags.omnipresent == flag)
2737 return;
2739 wwin->flags.omnipresent = flag;
2740 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2743 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2745 WWindow *wwin = data;
2747 #ifndef NUMLOCK_HACK
2748 if ((event->xbutton.state & ValidModMask)
2749 != (event->xbutton.state & ~LockMask)) {
2750 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2751 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2753 #endif
2755 event->xbutton.state &= ValidModMask;
2757 CloseWindowMenu(wwin->screen_ptr);
2759 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2760 && !WFLAGP(wwin, no_focusable)) {
2761 wSetFocusTo(wwin->screen_ptr, wwin);
2764 if (event->xbutton.button == Button1)
2765 wRaiseFrame(wwin->frame->core);
2767 if (event->xbutton.window != wwin->frame->resizebar->window) {
2768 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2769 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2770 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2771 return;
2775 if (event->xbutton.state & MOD_MASK) {
2776 /* move the window */
2777 wMouseMoveWindow(wwin, event);
2778 XUngrabPointer(dpy, CurrentTime);
2779 } else {
2780 wMouseResizeWindow(wwin, event);
2781 XUngrabPointer(dpy, CurrentTime);
2785 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2787 WWindow *wwin = data;
2789 event->xbutton.state &= ValidModMask;
2791 if (event->xbutton.button == Button1) {
2792 if (event->xbutton.state == 0) {
2793 if (!WFLAGP(wwin, no_shadeable)) {
2794 /* shade window */
2795 if (wwin->flags.shaded)
2796 wUnshadeWindow(wwin);
2797 else
2798 wShadeWindow(wwin);
2800 } else {
2801 int dir = 0;
2803 if (event->xbutton.state & ControlMask)
2804 dir |= MAX_VERTICAL;
2806 if (event->xbutton.state & ShiftMask) {
2807 dir |= MAX_HORIZONTAL;
2808 if (!(event->xbutton.state & ControlMask))
2809 wSelectWindow(wwin, !wwin->flags.selected);
2812 /* maximize window */
2813 if (dir != 0 && IS_RESIZABLE(wwin)) {
2814 int ndir = dir ^ wwin->flags.maximized;
2816 if (ndir != 0)
2817 wMaximizeWindow(wwin, ndir);
2818 else
2819 wUnmaximizeWindow(wwin);
2822 } else if (event->xbutton.button == Button3) {
2823 if (event->xbutton.state & MOD_MASK) {
2824 wHideOtherApplications(wwin);
2826 } else if (event->xbutton.button == Button2) {
2827 wSelectWindow(wwin, !wwin->flags.selected);
2828 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2829 wShadeWindow(wwin);
2830 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2831 wUnshadeWindow(wwin);
2835 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2837 WWindow *wwin = desc->parent;
2838 unsigned int new_width, w_scale;
2839 unsigned int new_height, h_scale;
2840 unsigned int resize_width_increment = 0;
2841 unsigned int resize_height_increment = 0;
2843 if (wwin->normal_hints) {
2844 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2845 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2846 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2847 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2849 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2850 resize_width_increment = wPreferences.resize_increment;
2851 resize_height_increment = wPreferences.resize_increment;
2854 event->xbutton.state &= ValidModMask;
2856 CloseWindowMenu(wwin->screen_ptr);
2858 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2859 wSetFocusTo(wwin->screen_ptr, wwin);
2861 if (event->xbutton.button == Button1)
2862 wRaiseFrame(wwin->frame->core);
2864 if (event->xbutton.state & ControlMask) {
2865 if (event->xbutton.button == Button4) {
2866 new_width = wwin->client.width - resize_width_increment;
2867 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2868 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2870 if (event->xbutton.button == Button5) {
2871 new_width = wwin->client.width + resize_width_increment;
2872 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2873 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2877 if (event->xbutton.state & MOD_MASK) {
2878 /* move the window */
2879 if (XGrabPointer(dpy, wwin->client_win, False,
2880 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2881 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2882 return;
2884 if (event->xbutton.button == Button3) {
2885 wMouseResizeWindow(wwin, event);
2886 } else if (event->xbutton.button == Button4) {
2887 new_height = wwin->client.height - resize_height_increment;
2888 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2889 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2890 } else if (event->xbutton.button == Button5) {
2891 new_height = wwin->client.height + resize_height_increment;
2892 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2893 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2894 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2895 wMouseMoveWindow(wwin, event);
2897 XUngrabPointer(dpy, CurrentTime);
2901 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2903 WWindow *wwin = (WWindow *) data;
2905 #ifndef NUMLOCK_HACK
2906 if ((event->xbutton.state & ValidModMask)
2907 != (event->xbutton.state & ~LockMask)) {
2908 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2909 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2911 #endif
2912 event->xbutton.state &= ValidModMask;
2914 CloseWindowMenu(wwin->screen_ptr);
2916 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2917 && !WFLAGP(wwin, no_focusable)) {
2918 wSetFocusTo(wwin->screen_ptr, wwin);
2921 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2923 if (event->xbutton.button == Button1) {
2924 if (event->xbutton.state & MOD_MASK) {
2925 wLowerFrame(wwin->frame->core);
2926 } else {
2927 wRaiseFrame(wwin->frame->core);
2930 if ((event->xbutton.state & ShiftMask)
2931 && !(event->xbutton.state & ControlMask)) {
2932 wSelectWindow(wwin, !wwin->flags.selected);
2933 return;
2935 if (event->xbutton.window != wwin->frame->titlebar->window
2936 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2937 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2938 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2939 return;
2942 /* move the window */
2943 wMouseMoveWindow(wwin, event);
2945 XUngrabPointer(dpy, CurrentTime);
2946 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2947 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2948 WObjDescriptor *desc;
2950 if (event->xbutton.window != wwin->frame->titlebar->window
2951 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2952 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2953 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2954 return;
2957 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2959 /* allow drag select */
2960 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2961 event->xany.send_event = True;
2962 (*desc->handle_mousedown) (desc, event);
2964 XUngrabPointer(dpy, CurrentTime);
2968 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2970 WWindow *wwin = data;
2972 event->xbutton.state &= ValidModMask;
2974 CloseWindowMenu(wwin->screen_ptr);
2976 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2977 return;
2979 /* if control-click, kill the client */
2980 if (event->xbutton.state & ControlMask) {
2981 wClientKill(wwin);
2982 } else {
2983 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2984 /* send delete message */
2985 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2990 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2992 WWindow *wwin = data;
2994 CloseWindowMenu(wwin->screen_ptr);
2996 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2997 return;
2999 /* send delete message */
3000 if (wwin->protocols.DELETE_WINDOW) {
3001 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3002 } else {
3003 wClientKill(wwin);
3007 #ifdef XKB_BUTTON_HINT
3008 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3010 WWindow *wwin = data;
3011 WFrameWindow *fwin = wwin->frame;
3012 WScreen *scr = fwin->screen_ptr;
3013 int tl;
3015 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3016 return;
3017 tl = wwin->frame->languagemode;
3018 wwin->frame->languagemode = wwin->frame->last_languagemode;
3019 wwin->frame->last_languagemode = tl;
3020 wSetFocusTo(scr, wwin);
3021 wwin->frame->languagebutton_image =
3022 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3023 wFrameWindowUpdateLanguageButton(wwin->frame);
3024 if (event->xbutton.button == Button3)
3025 return;
3026 wRaiseFrame(fwin->core);
3028 #endif
3030 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3032 WWindow *wwin = data;
3034 event->xbutton.state &= ValidModMask;
3036 CloseWindowMenu(wwin->screen_ptr);
3038 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3039 return;
3041 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3042 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3043 } else {
3044 WApplication *wapp;
3045 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3047 wapp = wApplicationOf(wwin->main_window);
3048 if (wapp && !WFLAGP(wwin, no_appicon))
3049 wHideApplication(wapp);
3050 } else if (event->xbutton.state == 0) {
3051 wIconifyWindow(wwin);