4051035d87bd79e73c70deda92dac007f187985d
[wmaker-crm.git] / src / window.c
blob4051035d87bd79e73c70deda92dac007f187985d
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;
595 /* mutex. */
596 XGrabServer(dpy);
597 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 else if (!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
642 /* Get hints and other information in properties */
643 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
645 /* setup descriptor */
646 wwin->client_win = window;
647 wwin->screen_ptr = scr;
648 wwin->old_border_width = wattribs.border_width;
649 wwin->event_mask = CLIENT_EVENTS;
650 attribs.event_mask = CLIENT_EVENTS;
651 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
652 attribs.save_under = False;
654 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
655 XSetWindowBorderWidth(dpy, window, 0);
657 /* get hints from GNUstep app */
658 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
659 wwin->flags.is_gnustep = 1;
661 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
662 wwin->wm_gnustep_attr = NULL;
664 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
665 wwin->flags.is_dockapp = 1;
666 withdraw = True;
669 wwin->client_leader = PropGetClientLeader(window);
670 if (wwin->client_leader != None)
671 wwin->main_window = wwin->client_leader;
673 wwin->wm_hints = XGetWMHints(dpy, window);
675 if (wwin->wm_hints) {
676 if (wwin->wm_hints->flags & StateHint) {
677 if (wwin->wm_hints->initial_state == IconicState) {
678 wwin->flags.miniaturized = 1;
679 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
680 wwin->flags.is_dockapp = 1;
681 withdraw = True;
685 if (wwin->wm_hints->flags & WindowGroupHint) {
686 wwin->group_id = wwin->wm_hints->window_group;
687 /* window_group has priority over CLIENT_LEADER */
688 wwin->main_window = wwin->group_id;
689 } else {
690 wwin->group_id = None;
693 if (wwin->wm_hints->flags & UrgencyHint) {
694 wwin->flags.urgent = 1;
695 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
697 } else {
698 wwin->group_id = None;
701 PropGetProtocols(window, &wwin->protocols);
703 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
704 wwin->transient_for = None;
705 } else {
706 if (wwin->transient_for == None || wwin->transient_for == window) {
707 wwin->transient_for = scr->root_win;
708 } else {
709 transientOwner = wWindowFor(wwin->transient_for);
710 if (transientOwner && transientOwner->main_window != None)
711 wwin->main_window = transientOwner->main_window;
715 /* guess the focus mode */
716 wwin->focus_mode = getFocusMode(wwin);
718 /* get geometry stuff */
719 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
721 /* get colormap windows */
722 GetColormapWindows(wwin);
725 * Setup the decoration/window attributes and
726 * geometry
728 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
730 /* Make broken apps behave as a nice app. */
731 if (WFLAGP(wwin, emulate_appicon))
732 wwin->main_window = wwin->client_win;
734 fixLeaderProperties(wwin);
736 wwin->orig_main_window = wwin->main_window;
738 if (wwin->flags.is_gnustep)
739 WSETUFLAG(wwin, shared_appicon, 0);
741 if (wwin->main_window) {
742 extern Atom _XA_WINDOWMAKER_MENU;
743 XTextProperty text_prop;
745 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
746 WSETUFLAG(wwin, shared_appicon, 0);
749 if (wwin->flags.is_dockapp)
750 WSETUFLAG(wwin, shared_appicon, 0);
752 if (wwin->main_window) {
753 WApplication *app = wApplicationOf(wwin->main_window);
754 if (app && app->app_icon)
755 WSETUFLAG(wwin, shared_appicon, 0);
758 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
759 char *buffer, *instance, *class;
760 WFakeGroupLeader *fPtr;
761 int index;
763 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
765 /* // only enter here if PropGetWMClass() succeds */
766 PropGetWMClass(wwin->main_window, &class, &instance);
767 buffer = StrConcatDot(instance, class);
769 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
770 if (index != WANotFound) {
771 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
772 if (fPtr->retainCount == 0)
773 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
774 instance, class);
776 fPtr->retainCount++;
777 if (fPtr->origLeader == None) {
778 if (ADEQUATE(wwin->main_window)) {
779 fPtr->retainCount++;
780 fPtr->origLeader = wwin->main_window;
783 wwin->fake_group = fPtr;
784 wwin->main_window = fPtr->leader;
785 wfree(buffer);
786 } else {
787 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
789 fPtr->identifier = buffer;
790 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
791 fPtr->origLeader = None;
792 fPtr->retainCount = 1;
794 WMAddToArray(scr->fakeGroupLeaders, fPtr);
796 if (ADEQUATE(wwin->main_window)) {
797 fPtr->retainCount++;
798 fPtr->origLeader = wwin->main_window;
800 wwin->fake_group = fPtr;
801 wwin->main_window = fPtr->leader;
804 if (instance)
805 free(instance);
807 if (class)
808 free(class);
809 #undef ADEQUATE
813 * Setup the initial state of the window
815 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
816 wwin->flags.miniaturized = 1;
818 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
819 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
821 wNETWMCheckInitialClientState(wwin);
823 /* apply previous state if it exists and we're in startup */
824 if (scr->flags.startup && wm_state >= 0) {
825 if (wm_state == IconicState)
826 wwin->flags.miniaturized = 1;
827 else if (wm_state == WithdrawnState)
828 withdraw = True;
831 /* if there is a saved state (from file), restore it */
832 win_state = NULL;
833 if (wwin->main_window != None)
834 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
835 else
836 win_state = (WWindowState *) wWindowGetSavedState(window);
838 if (win_state && !withdraw) {
839 if (win_state->state->hidden > 0)
840 wwin->flags.hidden = win_state->state->hidden;
842 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
843 wwin->flags.shaded = win_state->state->shaded;
845 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
846 wwin->flags.miniaturized = win_state->state->miniaturized;
848 if (!IS_OMNIPRESENT(wwin)) {
849 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
850 wwin->wm_class);
851 if (w < 0 || w >= scr->workspace_count) {
852 workspace = win_state->state->workspace;
853 if (workspace >= scr->workspace_count)
854 workspace = scr->current_workspace;
855 } else {
856 workspace = w;
858 } else {
859 workspace = scr->current_workspace;
863 /* if we're restarting, restore saved state (from hints).
864 * This will overwrite previous */
866 WSavedState *wstate;
868 if (getSavedState(window, &wstate)) {
869 wwin->flags.shaded = wstate->shaded;
870 wwin->flags.hidden = wstate->hidden;
871 wwin->flags.miniaturized = wstate->miniaturized;
872 wwin->flags.maximized = wstate->maximized;
873 if (wwin->flags.maximized) {
874 wwin->old_geometry.x = wstate->x;
875 wwin->old_geometry.y = wstate->y;
876 wwin->old_geometry.width = wstate->w;
877 wwin->old_geometry.height = wstate->h;
880 workspace = wstate->workspace;
881 } else {
882 wstate = NULL;
885 /* restore window shortcut */
886 if (wstate != NULL || win_state != NULL) {
887 unsigned mask = 0;
889 if (win_state != NULL)
890 mask = win_state->state->window_shortcuts;
892 if (wstate != NULL && mask == 0)
893 mask = wstate->window_shortcuts;
895 if (mask > 0) {
896 int i;
898 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
899 if (mask & (1 << i)) {
900 if (!scr->shortcutWindows[i])
901 scr->shortcutWindows[i] = WMCreateArray(4);
903 WMAddToArray(scr->shortcutWindows[i], wwin);
909 if (wstate != NULL)
910 wfree(wstate);
913 /* don't let transients start miniaturized if their owners are not */
914 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
915 wwin->flags.miniaturized = 0;
916 if (wwin->wm_hints)
917 wwin->wm_hints->initial_state = NormalState;
920 /* set workspace on which the window starts */
921 if (workspace >= 0) {
922 if (workspace > scr->workspace_count - 1)
923 workspace = workspace % scr->workspace_count;
924 } else {
925 int w;
927 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
929 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
930 workspace = w;
931 } else {
932 if (wPreferences.open_transients_with_parent && transientOwner)
933 workspace = transientOwner->frame->workspace;
934 else
935 workspace = scr->current_workspace;
939 /* setup window geometry */
940 if (win_state && win_state->state->w > 0) {
941 width = win_state->state->w;
942 height = win_state->state->h;
945 wWindowConstrainSize(wwin, &width, &height);
947 /* do not ask for window placement if the window is
948 * transient, during startup, if the initial workspace is another one
949 * or if the window wants to start iconic.
950 * If geometry was saved, restore it. */
952 Bool dontBring = False;
954 if (win_state && win_state->state->w > 0) {
955 x = win_state->state->x;
956 y = win_state->state->y;
957 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
958 && !scr->flags.startup
959 && workspace == scr->current_workspace
960 && !wwin->flags.miniaturized
961 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
963 if (transientOwner && transientOwner->flags.mapped) {
964 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
965 WMRect rect;
966 int head;
968 x = transientOwner->frame_x +
969 abs((transientOwner->frame->core->width - width) / 2) + offs;
970 y = transientOwner->frame_y +
971 abs((transientOwner->frame->core->height - height) / 3) + offs;
973 /* limit transient windows to be inside their parent's head */
974 rect.pos.x = transientOwner->frame_x;
975 rect.pos.y = transientOwner->frame_y;
976 rect.size.width = transientOwner->frame->core->width;
977 rect.size.height = transientOwner->frame->core->height;
979 head = wGetHeadForRect(scr, rect);
980 rect = wGetRectForHead(scr, head);
982 if (x < rect.pos.x)
983 x = rect.pos.x;
984 else if (x + width > rect.pos.x + rect.size.width)
985 x = rect.pos.x + rect.size.width - width;
987 if (y < rect.pos.y)
988 y = rect.pos.y;
989 else if (y + height > rect.pos.y + rect.size.height)
990 y = rect.pos.y + rect.size.height - height;
992 } else {
993 PlaceWindow(wwin, &x, &y, width, height);
996 if (wPreferences.window_placement == WPM_MANUAL)
997 dontBring = True;
999 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1000 int head, flags;
1001 WMRect rect;
1002 int reposition = 0;
1004 /* Make spash screens come out in the center of a head
1005 * trouble is that most splashies never get here
1006 * they are managed trough atoms but god knows where.
1007 * Dan, do you know ? -peter
1009 * Most of them are not managed, they have set
1010 * OverrideRedirect, which means we can't do anything about
1011 * them. -alfredo */
1013 /* xinerama checks for: across head and dead space */
1014 rect.pos.x = x;
1015 rect.pos.y = y;
1016 rect.size.width = width;
1017 rect.size.height = height;
1019 head = wGetRectPlacementInfo(scr, rect, &flags);
1021 if (flags & XFLAG_DEAD)
1022 reposition = 1;
1024 if (flags & XFLAG_MULTIPLE)
1025 reposition = 2;
1028 switch (reposition) {
1029 case 1:
1030 head = wGetHeadForPointerLocation(scr);
1031 rect = wGetRectForHead(scr, head);
1033 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1034 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1035 break;
1037 case 2:
1038 rect = wGetRectForHead(scr, head);
1040 if (x < rect.pos.x)
1041 x = rect.pos.x;
1042 else if (x + width > rect.pos.x + rect.size.width)
1043 x = rect.pos.x + rect.size.width - width;
1045 if (y < rect.pos.y)
1046 y = rect.pos.y;
1047 else if (y + height > rect.pos.y + rect.size.height)
1048 y = rect.pos.y + rect.size.height - height;
1050 break;
1052 default:
1053 break;
1057 if (WFLAGP(wwin, dont_move_off) && dontBring)
1058 wScreenBringInside(scr, &x, &y, width, height);
1061 wNETWMPositionSplash(wwin, &x, &y, width, height);
1063 if (wwin->flags.urgent) {
1064 if (!IS_OMNIPRESENT(wwin))
1065 wwin->flags.omnipresent ^= 1;
1068 /* Create frame, borders and do reparenting */
1069 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1070 #ifdef XKB_BUTTON_HINT
1071 if (wPreferences.modelock)
1072 foo |= WFF_LANGUAGE_BUTTON;
1073 #endif
1074 if (HAS_TITLEBAR(wwin))
1075 foo |= WFF_TITLEBAR;
1077 if (HAS_RESIZEBAR(wwin))
1078 foo |= WFF_RESIZEBAR;
1080 if (HAS_BORDER(wwin))
1081 foo |= WFF_BORDER;
1083 wwin->frame = wFrameWindowCreate(scr, window_level,
1084 x, y, width, height,
1085 &wPreferences.window_title_clearance,
1086 &wPreferences.window_title_min_height,
1087 &wPreferences.window_title_max_height,
1088 foo,
1089 scr->window_title_texture,
1090 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1091 wattribs.depth, wattribs.visual, wattribs.colormap);
1093 wwin->frame->flags.is_client_window_frame = 1;
1094 wwin->frame->flags.justification = wPreferences.title_justification;
1096 /* setup button images */
1097 wWindowUpdateButtonImages(wwin);
1099 /* hide unused buttons */
1100 foo = 0;
1101 if (WFLAGP(wwin, no_close_button))
1102 foo |= WFF_RIGHT_BUTTON;
1104 if (WFLAGP(wwin, no_miniaturize_button))
1105 foo |= WFF_LEFT_BUTTON;
1107 #ifdef XKB_BUTTON_HINT
1108 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1109 foo |= WFF_LANGUAGE_BUTTON;
1110 #endif
1111 if (foo != 0)
1112 wFrameWindowHideButton(wwin->frame, foo);
1114 wwin->frame->child = wwin;
1115 wwin->frame->workspace = workspace;
1116 wwin->frame->on_click_left = windowIconifyClick;
1118 #ifdef XKB_BUTTON_HINT
1119 if (wPreferences.modelock)
1120 wwin->frame->on_click_language = windowLanguageClick;
1121 #endif
1123 wwin->frame->on_click_right = windowCloseClick;
1124 wwin->frame->on_dblclick_right = windowCloseDblClick;
1125 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1126 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1127 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1129 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1130 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1131 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1134 int gx, gy;
1136 wClientGetGravityOffsets(wwin, &gx, &gy);
1138 /* if gravity is to the south, account for the border sizes */
1139 if (gy > 0)
1140 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1143 /* wWindowConfigure() will account for the window border
1144 * when placing so the window would be shifted without
1145 * the adjustment below
1147 if (HAS_BORDER(wwin)) {
1148 WMRect rect;
1149 WArea usableArea;
1150 int head;
1152 rect.pos.x = x;
1153 rect.pos.y = y;
1154 rect.size.width = 1;
1155 rect.size.height = 1;
1157 head = wGetHeadForRect(scr, rect);
1158 usableArea = wGetUsableAreaForHead(scr, head, NULL, True);
1160 if (x >= usableArea.x1 + 2 * FRAME_BORDER_WIDTH)
1161 x -= 2 * FRAME_BORDER_WIDTH;
1162 if (y >= usableArea.y1 + 2 * FRAME_BORDER_WIDTH)
1163 y -= 2 * FRAME_BORDER_WIDTH;
1167 * wWindowConfigure() will init the client window's size
1168 * (wwin->client.{width,height}) and all other geometry
1169 * related variables (frame_x,frame_y) */
1170 wWindowConfigure(wwin, x, y, width, height);
1172 /* to make sure the window receives it's new position after reparenting */
1173 wWindowSynthConfigureNotify(wwin);
1175 /* Setup descriptors and save window to internal lists */
1176 if (wwin->main_window != None) {
1177 WApplication *app;
1178 WWindow *leader;
1180 /* Leader windows do not necessary set themselves as leaders.
1181 * If this is the case, point the leader of this window to
1182 * itself */
1183 leader = wWindowFor(wwin->main_window);
1184 if (leader && leader->main_window == None)
1185 leader->main_window = leader->client_win;
1187 app = wApplicationCreate(wwin);
1188 if (app) {
1189 app->last_workspace = workspace;
1191 /* Do application specific stuff, like setting application
1192 * wide attributes. */
1194 if (wwin->flags.hidden) {
1195 /* if the window was set to hidden because it was hidden
1196 * in a previous incarnation and that state was restored */
1197 app->flags.hidden = 1;
1198 } else if (app->flags.hidden) {
1199 if (WFLAGP(app->main_window_desc, start_hidden)) {
1200 wwin->flags.hidden = 1;
1201 } else {
1202 wUnhideApplication(app, False, False);
1203 raise = True;
1206 wAppBounce(app);
1210 /* setup the frame descriptor */
1211 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1212 wwin->frame->core->descriptor.parent = wwin;
1213 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1215 /* don't let windows go away if we die */
1216 XAddToSaveSet(dpy, window);
1218 XLowerWindow(dpy, window);
1220 /* if window is in this workspace and should be mapped, then map it */
1221 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1222 && !wwin->flags.hidden && !withdraw) {
1224 /* The following "if" is to avoid crashing of clients that expect
1225 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1226 * after the return from this function. */
1227 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1228 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1229 else
1230 wClientSetState(wwin, NormalState, None);
1232 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1233 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1235 * The brain damaged idiotic non-click to focus modes will
1236 * have trouble with this because:
1238 * 1. window is created and mapped by the client
1239 * 2. window is mapped by wmaker in small size
1240 * 3. window is animated to grow to normal size
1241 * 4. this function returns to normal event loop
1242 * 5. eventually, the EnterNotify event that would trigger
1243 * the window focusing (if the mouse is over that window)
1244 * will be processed by wmaker.
1245 * But since this event will be rather delayed
1246 * (step 3 has a large delay) the time when the event ocurred
1247 * and when it is processed, the client that owns that window
1248 * will reject the XSetInputFocus() for it.
1250 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1251 DoWindowBirth(wwin);
1253 wWindowMap(wwin);
1256 /* setup stacking descriptor */
1257 if (transientOwner)
1258 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1259 else
1260 wwin->frame->core->stacking->child_of = NULL;
1262 if (!scr->focused_window) {
1263 /* first window on the list */
1264 wwin->next = NULL;
1265 wwin->prev = NULL;
1266 scr->focused_window = wwin;
1267 } else {
1268 WWindow *tmp;
1270 /* add window at beginning of focus window list */
1271 tmp = scr->focused_window;
1272 while (tmp->prev)
1273 tmp = tmp->prev;
1275 tmp->prev = wwin;
1276 wwin->next = tmp;
1277 wwin->prev = NULL;
1280 /* raise is set to true if we un-hid the app when this window was born.
1281 * we raise, else old windows of this app will be above this new one. */
1282 if (raise)
1283 wRaiseFrame(wwin->frame->core);
1285 /* Update name must come after WApplication stuff is done */
1286 wWindowUpdateName(wwin, title);
1287 if (title)
1288 XFree(title);
1290 XUngrabServer(dpy);
1292 /* Final preparations before window is ready to go */
1293 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1295 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1296 if (((transientOwner && transientOwner->flags.focused)
1297 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1299 /* only auto_focus if on same screen as mouse
1300 * (and same head for xinerama mode)
1301 * TODO: make it an option */
1303 /*TODO add checking the head of the window, is it available? */
1304 short same_screen = 0, same_head = 1;
1306 int foo;
1307 unsigned int bar;
1308 Window dummy;
1310 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1311 &foo, &foo, &foo, &foo, &bar) != False)
1312 same_screen = 1;
1314 if (same_screen == 1 && same_head == 1)
1315 wSetFocusTo(scr, wwin);
1318 wWindowResetMouseGrabs(wwin);
1320 if (!WFLAGP(wwin, no_bind_keys))
1321 wWindowSetKeyGrabs(wwin);
1323 WMPostNotificationName(WMNManaged, wwin, NULL);
1324 wColormapInstallForWindow(scr, scr->cmap_window);
1326 /* Setup Notification Observers */
1327 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1329 /* Cleanup temporary stuff */
1330 if (win_state)
1331 wWindowDeleteSavedState(win_state);
1333 /* If the window must be withdrawed, then do it now.
1334 * Must do some optimization, 'though */
1335 if (withdraw) {
1336 wwin->flags.mapped = 0;
1337 wClientSetState(wwin, WithdrawnState, None);
1338 wUnmanageWindow(wwin, True, False);
1339 wwin = NULL;
1342 return wwin;
1345 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1346 char *title, int x, int y, int width, int height)
1348 WWindow *wwin;
1349 int foo;
1351 wwin = wWindowCreate();
1353 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1355 wwin->flags.internal_window = 1;
1357 WSETUFLAG(wwin, omnipresent, 1);
1358 WSETUFLAG(wwin, no_shadeable, 1);
1359 WSETUFLAG(wwin, no_resizable, 1);
1360 WSETUFLAG(wwin, no_miniaturizable, 1);
1362 wwin->focus_mode = WFM_PASSIVE;
1363 wwin->client_win = window;
1364 wwin->screen_ptr = scr;
1365 wwin->transient_for = owner;
1366 wwin->client.x = x;
1367 wwin->client.y = y;
1368 wwin->client.width = width;
1369 wwin->client.height = height;
1370 wwin->frame_x = wwin->client.x;
1371 wwin->frame_y = wwin->client.y;
1373 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1374 foo |= WFF_TITLEBAR;
1375 #ifdef XKB_BUTTON_HINT
1376 foo |= WFF_LANGUAGE_BUTTON;
1377 #endif
1379 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1380 wwin->frame_x, wwin->frame_y,
1381 width, height,
1382 &wPreferences.window_title_clearance,
1383 &wPreferences.window_title_min_height,
1384 &wPreferences.window_title_max_height,
1385 foo,
1386 scr->window_title_texture,
1387 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1388 scr->w_depth, scr->w_visual, scr->w_colormap);
1390 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1392 wwin->frame->flags.is_client_window_frame = 1;
1393 wwin->frame->flags.justification = wPreferences.title_justification;
1395 wFrameWindowChangeTitle(wwin->frame, title);
1397 /* setup button images */
1398 wWindowUpdateButtonImages(wwin);
1400 /* hide buttons */
1401 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1403 wwin->frame->child = wwin;
1404 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1406 #ifdef XKB_BUTTON_HINT
1407 if (wPreferences.modelock)
1408 wwin->frame->on_click_language = windowLanguageClick;
1409 #endif
1411 wwin->frame->on_click_right = windowCloseClick;
1412 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1413 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1414 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1415 wwin->client.y += wwin->frame->top_width;
1417 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1418 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1420 /* setup the frame descriptor */
1421 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1422 wwin->frame->core->descriptor.parent = wwin;
1423 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1425 XLowerWindow(dpy, window);
1426 XMapSubwindows(dpy, wwin->frame->core->window);
1428 /* setup stacking descriptor */
1429 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1430 WWindow *tmp;
1431 tmp = wWindowFor(wwin->transient_for);
1432 if (tmp)
1433 wwin->frame->core->stacking->child_of = tmp->frame->core;
1434 } else {
1435 wwin->frame->core->stacking->child_of = NULL;
1438 if (!scr->focused_window) {
1439 /* first window on the list */
1440 wwin->next = NULL;
1441 wwin->prev = NULL;
1442 scr->focused_window = wwin;
1443 } else {
1444 WWindow *tmp;
1446 /* add window at beginning of focus window list */
1447 tmp = scr->focused_window;
1448 while (tmp->prev)
1449 tmp = tmp->prev;
1450 tmp->prev = wwin;
1451 wwin->next = tmp;
1452 wwin->prev = NULL;
1455 if (wwin->flags.is_gnustep == 0)
1456 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1458 /* if (wPreferences.auto_focus) */
1459 wSetFocusTo(scr, wwin);
1460 wWindowResetMouseGrabs(wwin);
1461 wWindowSetKeyGrabs(wwin);
1463 return wwin;
1467 *----------------------------------------------------------------------
1468 * wUnmanageWindow--
1469 * Removes the frame window from a window and destroys all data
1470 * related to it. The window will be reparented back to the root window
1471 * if restore is True.
1473 * Side effects:
1474 * Everything related to the window is destroyed and the window
1475 * is removed from the window lists. Focus is set to the previous on the
1476 * window list.
1477 *----------------------------------------------------------------------
1479 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1481 WCoreWindow *frame = wwin->frame->core;
1482 WWindow *owner = NULL;
1483 WWindow *newFocusedWindow = NULL;
1484 int wasFocused;
1485 WScreen *scr = wwin->screen_ptr;
1487 /* First close attribute editor window if open */
1488 if (wwin->flags.inspector_open)
1489 wCloseInspectorForWindow(wwin);
1491 /* Close window menu if it's open for this window */
1492 if (wwin->flags.menu_open_for_me)
1493 CloseWindowMenu(scr);
1495 if (!destroyed) {
1496 if (!wwin->flags.internal_window)
1497 XRemoveFromSaveSet(dpy, wwin->client_win);
1499 /* If this is a leader window, we still need to listen for
1500 * DestroyNotify and PropertyNotify. */
1501 if (wApplicationOf(wwin->client_win))
1502 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1503 else
1504 XSelectInput(dpy, wwin->client_win, NoEventMask);
1506 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1507 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1510 XUnmapWindow(dpy, frame->window);
1512 XUnmapWindow(dpy, wwin->client_win);
1514 /* deselect window */
1515 wSelectWindow(wwin, False);
1517 /* remove all pending events on window */
1518 /* I think this only matters for autoraise */
1519 if (wPreferences.raise_delay)
1520 WMDeleteTimerWithClientData(wwin->frame->core);
1522 XFlush(dpy);
1524 /* reparent the window back to the root */
1525 if (restore)
1526 wClientRestore(wwin);
1528 if (wwin->transient_for != scr->root_win) {
1529 owner = wWindowFor(wwin->transient_for);
1530 if (owner) {
1531 if (!owner->flags.semi_focused)
1532 owner = NULL;
1533 else
1534 owner->flags.semi_focused = 0;
1538 wasFocused = wwin->flags.focused;
1540 /* remove from window focus list */
1541 if (!wwin->prev && !wwin->next) {
1542 /* was the only window */
1543 scr->focused_window = NULL;
1544 newFocusedWindow = NULL;
1545 } else {
1546 WWindow *tmp;
1548 if (wwin->prev)
1549 wwin->prev->next = wwin->next;
1551 if (wwin->next)
1552 wwin->next->prev = wwin->prev;
1553 else {
1554 scr->focused_window = wwin->prev;
1555 scr->focused_window->next = NULL;
1558 if (wPreferences.focus_mode == WKF_CLICK) {
1560 /* if in click to focus mode and the window
1561 * was a transient, focus the owner window
1563 tmp = NULL;
1564 if (wPreferences.focus_mode == WKF_CLICK) {
1565 tmp = wWindowFor(wwin->transient_for);
1566 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1567 tmp = NULL;
1570 /* otherwise, focus the next one in the focus list */
1571 if (!tmp) {
1572 tmp = scr->focused_window;
1573 while (tmp) { /* look for one in the window list first */
1574 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1575 && (tmp->flags.mapped || tmp->flags.shaded))
1576 break;
1577 tmp = tmp->prev;
1579 if (!tmp) { /* if unsuccessful, choose any focusable window */
1580 tmp = scr->focused_window;
1581 while (tmp) {
1582 if (!WFLAGP(tmp, no_focusable)
1583 && (tmp->flags.mapped || tmp->flags.shaded))
1584 break;
1585 tmp = tmp->prev;
1590 newFocusedWindow = tmp;
1592 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1593 unsigned int mask;
1594 int foo;
1595 Window bar, win;
1597 /* This is to let the root window get the keyboard input
1598 * if Sloppy focus mode and no other window get focus.
1599 * This way keybindings will not freeze.
1601 tmp = NULL;
1602 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1603 tmp = wWindowFor(win);
1604 if (tmp == wwin)
1605 tmp = NULL;
1606 newFocusedWindow = tmp;
1607 } else {
1608 newFocusedWindow = NULL;
1612 if (!wwin->flags.internal_window)
1613 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1614 if (wasFocused) {
1615 if (newFocusedWindow != owner && owner) {
1616 if (wwin->flags.is_gnustep == 0)
1617 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1619 wSetFocusTo(scr, newFocusedWindow);
1622 /* Close menu and unhighlight */
1623 WApplication *oapp = wApplicationOf(wwin->main_window);
1624 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1625 if (oapp && oapp != napp) {
1626 wAppMenuUnmap(oapp->menu);
1627 if (wPreferences.highlight_active_app)
1628 wApplicationDeactivate(oapp);
1631 wNETCleanupFrameExtents(wwin);
1633 wWindowDestroy(wwin);
1634 XFlush(dpy);
1637 void wWindowMap(WWindow *wwin)
1639 XMapWindow(dpy, wwin->frame->core->window);
1640 if (!wwin->flags.shaded) {
1641 /* window will be remapped when getting MapNotify */
1642 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1643 XMapWindow(dpy, wwin->client_win);
1644 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1646 wwin->flags.mapped = 1;
1650 void wWindowUnmap(WWindow *wwin)
1652 wwin->flags.mapped = 0;
1654 /* prevent window withdrawal when getting UnmapNotify */
1655 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1656 XUnmapWindow(dpy, wwin->client_win);
1657 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1659 XUnmapWindow(dpy, wwin->frame->core->window);
1662 void wWindowFocus(WWindow *wwin, WWindow *owin)
1664 WWindow *nowner;
1665 WWindow *oowner;
1667 #ifdef KEEP_XKB_LOCK_STATUS
1668 if (wPreferences.modelock)
1669 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1670 #endif /* KEEP_XKB_LOCK_STATUS */
1672 wwin->flags.semi_focused = 0;
1674 if (wwin->flags.is_gnustep == 0)
1675 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1677 wwin->flags.focused = 1;
1679 wWindowResetMouseGrabs(wwin);
1681 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1683 if (owin == wwin || !owin)
1684 return;
1686 nowner = wWindowFor(wwin->transient_for);
1688 /* new window is a transient for the old window */
1689 if (nowner == owin) {
1690 owin->flags.semi_focused = 1;
1691 wWindowUnfocus(nowner);
1692 return;
1695 oowner = wWindowFor(owin->transient_for);
1697 /* new window is owner of old window */
1698 if (wwin == oowner) {
1699 wWindowUnfocus(owin);
1700 return;
1703 if (!nowner) {
1704 wWindowUnfocus(owin);
1705 return;
1708 /* new window has same owner of old window */
1709 if (oowner == nowner) {
1710 /* prevent unfocusing of owner */
1711 oowner->flags.semi_focused = 0;
1712 wWindowUnfocus(owin);
1713 oowner->flags.semi_focused = 1;
1715 return;
1718 /* nowner != NULL && oowner != nowner */
1719 nowner->flags.semi_focused = 1;
1720 wWindowUnfocus(nowner);
1721 wWindowUnfocus(owin);
1724 void wWindowUnfocus(WWindow *wwin)
1726 CloseWindowMenu(wwin->screen_ptr);
1728 if (wwin->flags.is_gnustep == 0)
1729 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1731 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1732 WWindow *owner;
1733 owner = wWindowFor(wwin->transient_for);
1734 if (owner && owner->flags.semi_focused) {
1735 owner->flags.semi_focused = 0;
1736 if (owner->flags.mapped || owner->flags.shaded) {
1737 wWindowUnfocus(owner);
1738 wFrameWindowPaint(owner->frame);
1742 wwin->flags.focused = 0;
1743 wWindowResetMouseGrabs(wwin);
1744 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1747 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1749 char *title;
1751 if (!wwin->frame)
1752 return;
1754 if (!newTitle)
1755 title = DEF_WINDOW_TITLE; /* the hint was removed */
1756 else
1757 title = newTitle;
1759 if (wFrameWindowChangeTitle(wwin->frame, title))
1760 WMPostNotificationName(WMNChangedName, wwin, NULL);
1764 *----------------------------------------------------------------------
1766 * wWindowConstrainSize--
1767 * Constrains size for the client window, taking the maximal size,
1768 * window resize increments and other size hints into account.
1770 * Returns:
1771 * The closest size to what was given that the client window can
1772 * have.
1774 *----------------------------------------------------------------------
1776 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1778 int width = (int)*nwidth;
1779 int height = (int)*nheight;
1780 int winc = 1;
1781 int hinc = 1;
1782 int minW = 1, minH = 1;
1783 int maxW = wwin->screen_ptr->scr_width * 2;
1784 int maxH = wwin->screen_ptr->scr_height * 2;
1785 int minAX = -1, minAY = -1;
1786 int maxAX = -1, maxAY = -1;
1787 int baseW = 0;
1788 int baseH = 0;
1790 if (wwin->normal_hints) {
1791 winc = wwin->normal_hints->width_inc;
1792 hinc = wwin->normal_hints->height_inc;
1793 minW = wwin->normal_hints->min_width;
1794 minH = wwin->normal_hints->min_height;
1795 maxW = wwin->normal_hints->max_width;
1796 maxH = wwin->normal_hints->max_height;
1797 if (wwin->normal_hints->flags & PAspect) {
1798 minAX = wwin->normal_hints->min_aspect.x;
1799 minAY = wwin->normal_hints->min_aspect.y;
1800 maxAX = wwin->normal_hints->max_aspect.x;
1801 maxAY = wwin->normal_hints->max_aspect.y;
1804 baseW = wwin->normal_hints->base_width;
1805 baseH = wwin->normal_hints->base_height;
1808 if (width < minW)
1809 width = minW;
1810 if (height < minH)
1811 height = minH;
1813 if (width > maxW)
1814 width = maxW;
1815 if (height > maxH)
1816 height = maxH;
1818 /* aspect ratio code borrowed from olwm */
1819 if (minAX > 0) {
1820 /* adjust max aspect ratio */
1821 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1822 if (maxAX > maxAY) {
1823 height = (width * maxAY) / maxAX;
1824 if (height > maxH) {
1825 height = maxH;
1826 width = (height * maxAX) / maxAY;
1828 } else {
1829 width = (height * maxAX) / maxAY;
1830 if (width > maxW) {
1831 width = maxW;
1832 height = (width * maxAY) / maxAX;
1837 /* adjust min aspect ratio */
1838 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1839 if (minAX > minAY) {
1840 height = (width * minAY) / minAX;
1841 if (height < minH) {
1842 height = minH;
1843 width = (height * minAX) / minAY;
1845 } else {
1846 width = (height * minAX) / minAY;
1847 if (width < minW) {
1848 width = minW;
1849 height = (width * minAY) / minAX;
1855 if (baseW != 0)
1856 width = (((width - baseW) / winc) * winc) + baseW;
1857 else
1858 width = (((width - minW) / winc) * winc) + minW;
1860 if (baseH != 0)
1861 height = (((height - baseH) / hinc) * hinc) + baseH;
1862 else
1863 height = (((height - minH) / hinc) * hinc) + minH;
1865 /* broken stupid apps may cause preposterous values for these.. */
1866 if (width > 0)
1867 *nwidth = width;
1868 if (height > 0)
1869 *nheight = height;
1872 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1873 unsigned int *width, unsigned int *height)
1875 int baseW = 0, baseH = 0;
1876 int winc = 1, hinc = 1;
1878 if (wwin->normal_hints) {
1879 baseW = wwin->normal_hints->base_width;
1880 baseH = wwin->normal_hints->base_height;
1882 winc = wwin->normal_hints->width_inc;
1883 hinc = wwin->normal_hints->height_inc;
1886 if (*width > maxW)
1887 *width = maxW - (maxW - baseW) % winc;
1889 if (*height > maxH)
1890 *height = maxH - (maxH - baseH) % hinc;
1893 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1895 WScreen *scr = wwin->screen_ptr;
1896 WApplication *wapp;
1897 int unmap = 0;
1899 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1900 return;
1902 if (workspace != scr->current_workspace) {
1903 /* Sent to other workspace. Unmap window */
1904 if ((wwin->flags.mapped
1905 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1906 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1908 wapp = wApplicationOf(wwin->main_window);
1909 if (wapp)
1910 wapp->last_workspace = workspace;
1912 if (wwin->flags.miniaturized) {
1913 if (wwin->icon) {
1914 XUnmapWindow(dpy, wwin->icon->core->window);
1915 wwin->icon->mapped = 0;
1917 } else {
1918 unmap = 1;
1919 wSetFocusTo(scr, NULL);
1922 } else {
1923 /* brought to current workspace. Map window */
1924 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1925 if (wwin->icon) {
1926 XMapWindow(dpy, wwin->icon->core->window);
1927 wwin->icon->mapped = 1;
1929 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1930 wWindowMap(wwin);
1933 if (!IS_OMNIPRESENT(wwin)) {
1934 int oldWorkspace = wwin->frame->workspace;
1935 wwin->frame->workspace = workspace;
1936 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1939 if (unmap)
1940 wWindowUnmap(wwin);
1943 void wWindowSynthConfigureNotify(WWindow *wwin)
1945 XEvent sevent;
1947 sevent.type = ConfigureNotify;
1948 sevent.xconfigure.display = dpy;
1949 sevent.xconfigure.event = wwin->client_win;
1950 sevent.xconfigure.window = wwin->client_win;
1952 sevent.xconfigure.x = wwin->client.x;
1953 sevent.xconfigure.y = wwin->client.y;
1954 sevent.xconfigure.width = wwin->client.width;
1955 sevent.xconfigure.height = wwin->client.height;
1957 sevent.xconfigure.border_width = wwin->old_border_width;
1958 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1959 sevent.xconfigure.above = wwin->frame->titlebar->window;
1960 else
1961 sevent.xconfigure.above = None;
1963 sevent.xconfigure.override_redirect = False;
1964 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1965 XFlush(dpy);
1969 *----------------------------------------------------------------------
1970 * wWindowConfigure()
1972 * req_x, req_y: new requested positions for the frame
1973 * req_width, req_height: new requested sizes for the client
1975 * Configures the frame, decorations and client window to the specified
1976 * geometry, whose validity is not checked -- wWindowConstrainSize()
1977 * must be used for that.
1978 * The size parameters are for the client window, but the position is
1979 * for the frame.
1980 * The client window receives a ConfigureNotify event, according
1981 * to what ICCCM says.
1983 * Returns:
1984 * None
1986 * Side effects:
1987 * Window size and position are changed and client window receives
1988 * a ConfigureNotify event.
1989 *----------------------------------------------------------------------
1991 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
1993 int synth_notify = False;
1994 int resize;
1996 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
1998 * if the window is being moved but not resized then
1999 * send a synthetic ConfigureNotify
2001 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2002 synth_notify = True;
2004 if (WFLAGP(wwin, dont_move_off))
2005 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2007 if (resize) {
2008 if (req_width < MIN_WINDOW_SIZE)
2009 req_width = MIN_WINDOW_SIZE;
2010 if (req_height < MIN_WINDOW_SIZE)
2011 req_height = MIN_WINDOW_SIZE;
2013 /* If growing, resize inner part before frame,
2014 * if shrinking, resize frame before.
2015 * This will prevent the frame (that can have a different color)
2016 * to be exposed, causing flicker */
2017 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2018 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2020 if (wwin->flags.shaded) {
2021 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2022 wwin->old_geometry.height = req_height;
2023 } else {
2024 int h;
2026 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2028 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2031 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2032 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2034 wwin->client.x = req_x;
2035 wwin->client.y = req_y + wwin->frame->top_width;
2036 wwin->client.width = req_width;
2037 wwin->client.height = req_height;
2038 } else {
2039 wwin->client.x = req_x;
2040 wwin->client.y = req_y + wwin->frame->top_width;
2042 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2044 wwin->frame_x = req_x;
2045 wwin->frame_y = req_y;
2046 if (HAS_BORDER(wwin)) {
2047 wwin->client.x += FRAME_BORDER_WIDTH;
2048 wwin->client.y += FRAME_BORDER_WIDTH;
2050 #ifdef SHAPE
2051 if (wShapeSupported && wwin->flags.shaped && resize)
2052 wWindowSetShape(wwin);
2053 #endif
2055 if (synth_notify)
2056 wWindowSynthConfigureNotify(wwin);
2058 wNETFrameExtents(wwin);
2060 XFlush(dpy);
2063 /* req_x, req_y: new position of the frame */
2064 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2066 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2067 int synth_notify = False;
2069 /* Send a synthetic ConfigureNotify event for every window movement. */
2070 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2071 synth_notify = True;
2072 #else
2073 /* A single synthetic ConfigureNotify event is sent at the end of
2074 * a completed (opaque) movement in moveres.c */
2075 #endif
2077 if (WFLAGP(wwin, dont_move_off))
2078 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2079 wwin->frame->core->width, wwin->frame->core->height);
2081 wwin->client.x = req_x;
2082 wwin->client.y = req_y + wwin->frame->top_width;
2083 if (HAS_BORDER(wwin)) {
2084 wwin->client.x += FRAME_BORDER_WIDTH;
2085 wwin->client.y += FRAME_BORDER_WIDTH;
2088 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2090 wwin->frame_x = req_x;
2091 wwin->frame_y = req_y;
2093 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2094 if (synth_notify)
2095 wWindowSynthConfigureNotify(wwin);
2096 #endif
2099 void wWindowUpdateButtonImages(WWindow *wwin)
2101 WScreen *scr = wwin->screen_ptr;
2102 Pixmap pixmap, mask;
2103 WFrameWindow *fwin = wwin->frame;
2105 if (!HAS_TITLEBAR(wwin))
2106 return;
2108 /* miniaturize button */
2109 if (!WFLAGP(wwin, no_miniaturize_button)) {
2110 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2111 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2113 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2114 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2115 else
2116 mask = None;
2118 if (fwin->lbutton_image
2119 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2120 wPixmapDestroy(fwin->lbutton_image);
2121 fwin->lbutton_image = NULL;
2124 if (!fwin->lbutton_image) {
2125 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2126 fwin->lbutton_image->client_owned = 1;
2127 fwin->lbutton_image->client_owned_mask = 1;
2129 } else {
2130 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2131 wPixmapDestroy(fwin->lbutton_image);
2133 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2136 #ifdef XKB_BUTTON_HINT
2137 if (!WFLAGP(wwin, no_language_button)) {
2138 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2139 wPixmapDestroy(fwin->languagebutton_image);
2141 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2143 #endif
2145 /* close button */
2147 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2148 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2150 if (!WFLAGP(wwin, no_close_button)) {
2151 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2152 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2154 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2155 mask = wwin->wm_gnustep_attr->close_mask;
2156 else
2157 mask = None;
2159 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2160 || fwin->rbutton_image->mask != mask)) {
2161 wPixmapDestroy(fwin->rbutton_image);
2162 fwin->rbutton_image = NULL;
2165 if (!fwin->rbutton_image) {
2166 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2167 fwin->rbutton_image->client_owned = 1;
2168 fwin->rbutton_image->client_owned_mask = 1;
2171 } else if (WFLAGP(wwin, kill_close)) {
2172 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2173 wPixmapDestroy(fwin->rbutton_image);
2175 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2177 } else if (MGFLAGP(wwin, broken_close)) {
2178 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2179 wPixmapDestroy(fwin->rbutton_image);
2181 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2183 } else {
2184 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2185 wPixmapDestroy(fwin->rbutton_image);
2187 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2191 /* force buttons to be redrawn */
2192 fwin->flags.need_texture_change = 1;
2193 wFrameWindowPaint(fwin);
2197 *---------------------------------------------------------------------------
2198 * wWindowConfigureBorders--
2199 * Update window border configuration according to attribute flags.
2201 *---------------------------------------------------------------------------
2203 void wWindowConfigureBorders(WWindow *wwin)
2205 if (wwin->frame) {
2206 int flags;
2207 int newy, oldh;
2209 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2211 #ifdef XKB_BUTTON_HINT
2212 if (wPreferences.modelock)
2213 flags |= WFF_LANGUAGE_BUTTON;
2214 #endif
2216 if (HAS_TITLEBAR(wwin))
2217 flags |= WFF_TITLEBAR;
2218 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2219 flags |= WFF_RESIZEBAR;
2220 if (HAS_BORDER(wwin))
2221 flags |= WFF_BORDER;
2222 if (wwin->flags.shaded)
2223 flags |= WFF_IS_SHADED;
2225 oldh = wwin->frame->top_width;
2226 wFrameWindowUpdateBorders(wwin->frame, flags);
2227 if (oldh != wwin->frame->top_width) {
2228 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2230 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2231 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2234 flags = 0;
2235 if (!WFLAGP(wwin, no_miniaturize_button)
2236 && wwin->frame->flags.hide_left_button)
2237 flags |= WFF_LEFT_BUTTON;
2239 #ifdef XKB_BUTTON_HINT
2240 if (!WFLAGP(wwin, no_language_button)
2241 && wwin->frame->flags.hide_language_button)
2242 flags |= WFF_LANGUAGE_BUTTON;
2243 #endif
2245 if (!WFLAGP(wwin, no_close_button)
2246 && wwin->frame->flags.hide_right_button)
2247 flags |= WFF_RIGHT_BUTTON;
2249 if (flags != 0) {
2250 wWindowUpdateButtonImages(wwin);
2251 wFrameWindowShowButton(wwin->frame, flags);
2254 flags = 0;
2255 if (WFLAGP(wwin, no_miniaturize_button)
2256 && !wwin->frame->flags.hide_left_button)
2257 flags |= WFF_LEFT_BUTTON;
2259 #ifdef XKB_BUTTON_HINT
2260 if (WFLAGP(wwin, no_language_button)
2261 && !wwin->frame->flags.hide_language_button)
2262 flags |= WFF_LANGUAGE_BUTTON;
2263 #endif
2265 if (WFLAGP(wwin, no_close_button)
2266 && !wwin->frame->flags.hide_right_button)
2267 flags |= WFF_RIGHT_BUTTON;
2269 if (flags != 0)
2270 wFrameWindowHideButton(wwin->frame, flags);
2272 #ifdef SHAPE
2273 if (wShapeSupported && wwin->flags.shaped)
2274 wWindowSetShape(wwin);
2275 #endif
2279 void wWindowSaveState(WWindow * wwin)
2281 long data[10];
2282 int i;
2284 memset(data, 0, sizeof(long) * 10);
2285 data[0] = wwin->frame->workspace;
2286 data[1] = wwin->flags.miniaturized;
2287 data[2] = wwin->flags.shaded;
2288 data[3] = wwin->flags.hidden;
2289 data[4] = wwin->flags.maximized;
2290 if (wwin->flags.maximized == 0) {
2291 data[5] = wwin->frame_x;
2292 data[6] = wwin->frame_y;
2293 data[7] = wwin->frame->core->width;
2294 data[8] = wwin->frame->core->height;
2295 } else {
2296 data[5] = wwin->old_geometry.x;
2297 data[6] = wwin->old_geometry.y;
2298 data[7] = wwin->old_geometry.width;
2299 data[8] = wwin->old_geometry.height;
2302 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2303 if (wwin->screen_ptr->shortcutWindows[i] &&
2304 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2305 data[9] |= 1 << i;
2307 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2308 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2311 static int getSavedState(Window window, WSavedState ** state)
2313 Atom type_ret;
2314 int fmt_ret;
2315 unsigned long nitems_ret;
2316 unsigned long bytes_after_ret;
2317 long *data;
2319 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2320 True, _XA_WINDOWMAKER_STATE,
2321 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2322 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2323 return 0;
2325 *state = wmalloc(sizeof(WSavedState));
2327 (*state)->workspace = data[0];
2328 (*state)->miniaturized = data[1];
2329 (*state)->shaded = data[2];
2330 (*state)->hidden = data[3];
2331 (*state)->maximized = data[4];
2332 (*state)->x = data[5];
2333 (*state)->y = data[6];
2334 (*state)->w = data[7];
2335 (*state)->h = data[8];
2336 (*state)->window_shortcuts = data[9];
2338 XFree(data);
2340 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2341 return 1;
2342 else
2343 return 0;
2346 #ifdef SHAPE
2347 void wWindowClearShape(WWindow * wwin)
2349 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2350 0, wwin->frame->top_width, None, ShapeSet);
2351 XFlush(dpy);
2354 void wWindowSetShape(WWindow * wwin)
2356 XRectangle rect[2];
2357 int count;
2358 #ifdef OPTIMIZE_SHAPE
2359 XRectangle *rects;
2360 XRectangle *urec;
2361 int ordering;
2363 /* only shape is the client's */
2364 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2365 goto alt_code;
2367 /* Get array of rectangles describing the shape mask */
2368 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2369 if (!rects)
2370 goto alt_code;
2372 urec = malloc(sizeof(XRectangle) * (count + 2));
2373 if (!urec) {
2374 XFree(rects);
2375 goto alt_code;
2378 /* insert our decoration rectangles in the rect list */
2379 memcpy(urec, rects, sizeof(XRectangle) * count);
2380 XFree(rects);
2382 if (HAS_TITLEBAR(wwin)) {
2383 urec[count].x = -1;
2384 urec[count].y = -1 - wwin->frame->top_width;
2385 urec[count].width = wwin->frame->core->width + 2;
2386 urec[count].height = wwin->frame->top_width + 1;
2387 count++;
2389 if (HAS_RESIZEBAR(wwin)) {
2390 urec[count].x = -1;
2391 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2392 urec[count].width = wwin->frame->core->width + 2;
2393 urec[count].height = wwin->frame->bottom_width + 1;
2394 count++;
2397 /* shape our frame window */
2398 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2399 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2400 XFlush(dpy);
2401 wfree(urec);
2402 return;
2404 alt_code:
2405 #endif /* OPTIMIZE_SHAPE */
2406 count = 0;
2407 if (HAS_TITLEBAR(wwin)) {
2408 rect[count].x = -1;
2409 rect[count].y = -1;
2410 rect[count].width = wwin->frame->core->width + 2;
2411 rect[count].height = wwin->frame->top_width + 1;
2412 count++;
2414 if (HAS_RESIZEBAR(wwin)) {
2415 rect[count].x = -1;
2416 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2417 rect[count].width = wwin->frame->core->width + 2;
2418 rect[count].height = wwin->frame->bottom_width + 1;
2419 count++;
2421 if (count > 0) {
2422 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2423 0, 0, rect, count, ShapeSet, Unsorted);
2425 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2426 0, wwin->frame->top_width, wwin->client_win,
2427 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2428 XFlush(dpy);
2430 #endif /* SHAPE */
2432 /* ====================================================================== */
2434 static FocusMode getFocusMode(WWindow * wwin)
2436 FocusMode mode;
2438 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2439 if (wwin->wm_hints->input == True) {
2440 if (wwin->protocols.TAKE_FOCUS)
2441 mode = WFM_LOCALLY_ACTIVE;
2442 else
2443 mode = WFM_PASSIVE;
2444 } else {
2445 if (wwin->protocols.TAKE_FOCUS)
2446 mode = WFM_GLOBALLY_ACTIVE;
2447 else
2448 mode = WFM_NO_INPUT;
2450 } else {
2451 mode = WFM_PASSIVE;
2453 return mode;
2456 void wWindowSetKeyGrabs(WWindow * wwin)
2458 int i;
2459 WShortKey *key;
2461 for (i = 0; i < WKBD_LAST; i++) {
2462 key = &wKeyBindings[i];
2464 if (key->keycode == 0)
2465 continue;
2466 if (key->modifier != AnyModifier) {
2467 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2468 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2469 #ifdef NUMLOCK_HACK
2470 /* Also grab all modifier combinations possible that include,
2471 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2472 * work even if the NumLock/ScrollLock key is on.
2474 wHackedGrabKey(key->keycode, key->modifier,
2475 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2476 #endif
2478 XGrabKey(dpy, key->keycode, key->modifier,
2479 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2482 wRootMenuBindShortcuts(wwin->frame->core->window);
2485 void wWindowResetMouseGrabs(WWindow * wwin)
2487 /* Mouse grabs can't be done on the client window because of
2488 * ICCCM and because clients that try to do the same will crash.
2490 * But there is a problem wich makes tbar buttons of unfocused
2491 * windows not usable as the click goes to the frame window instead
2492 * of the button itself. Must figure a way to fix that.
2495 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2497 if (!WFLAGP(wwin, no_bind_mouse)) {
2498 /* grabs for Meta+drag */
2499 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2500 True, ButtonPressMask | ButtonReleaseMask,
2501 GrabModeSync, GrabModeAsync, None, None);
2503 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2504 * but we only grab it for Button4 and Button 5 since a lot of apps
2505 * use CTRL+Button1-3 for app related functionality
2507 if (wPreferences.resize_increment > 0) {
2508 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2509 True, ButtonPressMask | ButtonReleaseMask,
2510 GrabModeSync, GrabModeAsync, None, None);
2511 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2512 True, ButtonPressMask | ButtonReleaseMask,
2513 GrabModeSync, GrabModeAsync, None, None);
2515 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2516 True, ButtonPressMask | ButtonReleaseMask,
2517 GrabModeSync, GrabModeAsync, None, None);
2518 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2519 True, ButtonPressMask | ButtonReleaseMask,
2520 GrabModeSync, GrabModeAsync, None, None);
2524 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2525 && !wwin->flags.is_gnustep) {
2526 /* the passive grabs to focus the window */
2527 /* if (wPreferences.focus_mode == WKF_CLICK) */
2528 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2529 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2531 XFlush(dpy);
2534 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2536 if (attr->flags & GSExtraFlagsAttr) {
2537 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2538 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2539 wWindowUpdateButtonImages(wwin);
2544 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2546 WWindowState *wstate;
2548 wstate = malloc(sizeof(WWindowState));
2549 if (!wstate)
2550 return NULL;
2552 memset(wstate, 0, sizeof(WWindowState));
2553 wstate->pid = pid;
2554 if (instance)
2555 wstate->instance = wstrdup(instance);
2556 if (class)
2557 wstate->class = wstrdup(class);
2558 if (command)
2559 wstate->command = wstrdup(command);
2560 wstate->state = state;
2562 wstate->next = windowState;
2563 windowState = wstate;
2565 return wstate;
2568 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2570 WMagicNumber wWindowGetSavedState(Window win)
2572 char *instance, *class, *command = NULL;
2573 WWindowState *wstate = windowState;
2575 if (!wstate)
2576 return NULL;
2578 command = GetCommandForWindow(win);
2579 if (!command)
2580 return NULL;
2582 if (PropGetWMClass(win, &class, &instance)) {
2583 while (wstate) {
2584 if (SAME(instance, wstate->instance) &&
2585 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2586 break;
2588 wstate = wstate->next;
2590 } else {
2591 wstate = NULL;
2594 if (command)
2595 wfree(command);
2596 if (instance)
2597 free(instance);
2598 if (class)
2599 free(class);
2601 return wstate;
2604 void wWindowDeleteSavedState(WMagicNumber id)
2606 WWindowState *tmp, *wstate = (WWindowState *) id;
2608 if (!wstate || !windowState)
2609 return;
2611 tmp = windowState;
2612 if (tmp == wstate) {
2613 windowState = wstate->next;
2614 release_wwindowstate(wstate);
2615 } else {
2616 while (tmp->next) {
2617 if (tmp->next == wstate) {
2618 tmp->next = wstate->next;
2619 release_wwindowstate(wstate);
2620 break;
2622 tmp = tmp->next;
2627 void wWindowDeleteSavedStatesForPID(pid_t pid)
2629 WWindowState *tmp, *wstate;
2631 if (!windowState)
2632 return;
2634 tmp = windowState;
2635 if (tmp->pid == pid) {
2636 wstate = windowState;
2637 windowState = tmp->next;
2639 release_wwindowstate(wstate);
2640 } else {
2641 while (tmp->next) {
2642 if (tmp->next->pid == pid) {
2643 wstate = tmp->next;
2644 tmp->next = wstate->next;
2645 release_wwindowstate(wstate);
2646 break;
2648 tmp = tmp->next;
2653 static void release_wwindowstate(WWindowState *wstate)
2655 if (wstate->instance)
2656 wfree(wstate->instance);
2658 if (wstate->class)
2659 wfree(wstate->class);
2661 if (wstate->command)
2662 wfree(wstate->command);
2664 wfree(wstate->state);
2665 wfree(wstate);
2668 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2670 if (wwin->flags.omnipresent == flag)
2671 return;
2673 wwin->flags.omnipresent = flag;
2674 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2677 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2679 WWindow *wwin = data;
2681 #ifndef NUMLOCK_HACK
2682 if ((event->xbutton.state & ValidModMask)
2683 != (event->xbutton.state & ~LockMask)) {
2684 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2685 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2687 #endif
2689 event->xbutton.state &= ValidModMask;
2691 CloseWindowMenu(wwin->screen_ptr);
2693 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2694 && !WFLAGP(wwin, no_focusable)) {
2695 wSetFocusTo(wwin->screen_ptr, wwin);
2698 if (event->xbutton.button == Button1)
2699 wRaiseFrame(wwin->frame->core);
2701 if (event->xbutton.window != wwin->frame->resizebar->window) {
2702 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2703 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2704 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2705 return;
2709 if (event->xbutton.state & MOD_MASK) {
2710 /* move the window */
2711 wMouseMoveWindow(wwin, event);
2712 XUngrabPointer(dpy, CurrentTime);
2713 } else {
2714 wMouseResizeWindow(wwin, event);
2715 XUngrabPointer(dpy, CurrentTime);
2719 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2721 WWindow *wwin = data;
2723 event->xbutton.state &= ValidModMask;
2725 if (event->xbutton.button == Button1) {
2726 if (event->xbutton.state == 0) {
2727 if (!WFLAGP(wwin, no_shadeable)) {
2728 /* shade window */
2729 if (wwin->flags.shaded)
2730 wUnshadeWindow(wwin);
2731 else
2732 wShadeWindow(wwin);
2734 } else {
2735 int dir = 0;
2737 if (event->xbutton.state & ControlMask)
2738 dir |= MAX_VERTICAL;
2740 if (event->xbutton.state & ShiftMask) {
2741 dir |= MAX_HORIZONTAL;
2742 if (!(event->xbutton.state & ControlMask))
2743 wSelectWindow(wwin, !wwin->flags.selected);
2746 /* maximize window */
2747 if (dir != 0 && IS_RESIZABLE(wwin)) {
2748 int ndir = dir ^ wwin->flags.maximized;
2750 if (ndir != 0)
2751 wMaximizeWindow(wwin, ndir);
2752 else
2753 wUnmaximizeWindow(wwin);
2756 } else if (event->xbutton.button == Button3) {
2757 if (event->xbutton.state & MOD_MASK)
2758 wHideOtherApplications(wwin);
2759 } else if (event->xbutton.button == Button2) {
2760 wSelectWindow(wwin, !wwin->flags.selected);
2761 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2762 wShadeWindow(wwin);
2763 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2764 wUnshadeWindow(wwin);
2768 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2770 WWindow *wwin = desc->parent;
2771 unsigned int new_width, w_scale;
2772 unsigned int new_height, h_scale;
2773 unsigned int resize_width_increment = 0;
2774 unsigned int resize_height_increment = 0;
2776 if (wwin->normal_hints) {
2777 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2778 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2779 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2780 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2782 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2783 resize_width_increment = wPreferences.resize_increment;
2784 resize_height_increment = wPreferences.resize_increment;
2787 event->xbutton.state &= ValidModMask;
2789 CloseWindowMenu(wwin->screen_ptr);
2791 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2792 wSetFocusTo(wwin->screen_ptr, wwin);
2794 if (event->xbutton.button == Button1)
2795 wRaiseFrame(wwin->frame->core);
2797 if (event->xbutton.state & ControlMask) {
2798 if (event->xbutton.button == Button4) {
2799 new_width = wwin->client.width - resize_width_increment;
2800 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2801 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2803 if (event->xbutton.button == Button5) {
2804 new_width = wwin->client.width + resize_width_increment;
2805 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2806 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2810 if (event->xbutton.state & MOD_MASK) {
2811 /* move the window */
2812 if (XGrabPointer(dpy, wwin->client_win, False,
2813 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2814 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2815 return;
2817 if (event->xbutton.button == Button3) {
2818 wMouseResizeWindow(wwin, event);
2819 } else if (event->xbutton.button == Button4) {
2820 new_height = wwin->client.height - resize_height_increment;
2821 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2822 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2823 } else if (event->xbutton.button == Button5) {
2824 new_height = wwin->client.height + resize_height_increment;
2825 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2826 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2827 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2828 wMouseMoveWindow(wwin, event);
2830 XUngrabPointer(dpy, CurrentTime);
2834 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2836 WWindow *wwin = (WWindow *) data;
2838 #ifndef NUMLOCK_HACK
2839 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2840 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2841 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2842 #endif
2843 event->xbutton.state &= ValidModMask;
2845 CloseWindowMenu(wwin->screen_ptr);
2847 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2848 && !WFLAGP(wwin, no_focusable))
2849 wSetFocusTo(wwin->screen_ptr, wwin);
2851 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2853 if (event->xbutton.button == Button1) {
2854 if (event->xbutton.state & MOD_MASK)
2855 wLowerFrame(wwin->frame->core);
2856 else
2857 wRaiseFrame(wwin->frame->core);
2859 if ((event->xbutton.state & ShiftMask)
2860 && !(event->xbutton.state & ControlMask)) {
2861 wSelectWindow(wwin, !wwin->flags.selected);
2862 return;
2864 if (event->xbutton.window != wwin->frame->titlebar->window
2865 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2866 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2867 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2868 return;
2871 /* move the window */
2872 wMouseMoveWindow(wwin, event);
2874 XUngrabPointer(dpy, CurrentTime);
2875 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2876 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2877 WObjDescriptor *desc;
2879 if (event->xbutton.window != wwin->frame->titlebar->window
2880 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2881 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2882 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2883 return;
2886 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2888 /* allow drag select */
2889 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2890 event->xany.send_event = True;
2891 (*desc->handle_mousedown) (desc, event);
2893 XUngrabPointer(dpy, CurrentTime);
2897 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2899 WWindow *wwin = data;
2901 event->xbutton.state &= ValidModMask;
2903 CloseWindowMenu(wwin->screen_ptr);
2905 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2906 return;
2908 /* if control-click, kill the client */
2909 if (event->xbutton.state & ControlMask) {
2910 wClientKill(wwin);
2911 } else {
2912 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2913 /* send delete message */
2914 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2919 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2921 WWindow *wwin = data;
2923 CloseWindowMenu(wwin->screen_ptr);
2925 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2926 return;
2928 /* send delete message */
2929 if (wwin->protocols.DELETE_WINDOW)
2930 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2931 else
2932 wClientKill(wwin);
2935 #ifdef XKB_BUTTON_HINT
2936 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2938 WWindow *wwin = data;
2939 WFrameWindow *fwin = wwin->frame;
2940 WScreen *scr = fwin->screen_ptr;
2941 int tl;
2943 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2944 return;
2945 tl = wwin->frame->languagemode;
2946 wwin->frame->languagemode = wwin->frame->last_languagemode;
2947 wwin->frame->last_languagemode = tl;
2948 wSetFocusTo(scr, wwin);
2949 wwin->frame->languagebutton_image =
2950 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2951 wFrameWindowUpdateLanguageButton(wwin->frame);
2952 if (event->xbutton.button == Button3)
2953 return;
2954 wRaiseFrame(fwin->core);
2956 #endif
2958 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2960 WWindow *wwin = data;
2962 event->xbutton.state &= ValidModMask;
2964 CloseWindowMenu(wwin->screen_ptr);
2966 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2967 return;
2969 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2970 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2971 } else {
2972 WApplication *wapp;
2973 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2975 wapp = wApplicationOf(wwin->main_window);
2976 if (wapp && !WFLAGP(wwin, no_appicon))
2977 wHideApplication(wapp);
2978 } else if (event->xbutton.state == 0) {
2979 wIconifyWindow(wwin);