Fix Maximize/Unmaximize in window menu
[wmaker-crm.git] / src / window.c
blob1cb384a836d33cf6c426ec6966356ea7305fac67
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
29 #ifdef KEEP_XKB_LOCK_STATUS
30 #include <X11/XKBlib.h>
31 #endif /* KEEP_XKB_LOCK_STATUS */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <math.h>
38 /* For getting mouse wheel mappings from WINGs */
39 #include <WINGs/WINGsP.h>
41 #include "WindowMaker.h"
42 #include "GNUstep.h"
43 #include "wcore.h"
44 #include "framewin.h"
45 #include "texture.h"
46 #include "window.h"
47 #include "winspector.h"
48 #include "icon.h"
49 #include "properties.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "colormap.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
59 #include "appmenu.h"
60 #include "appicon.h"
61 #include "superfluous.h"
62 #include "rootmenu.h"
64 #ifdef MWM_HINTS
65 # include "motif.h"
66 #endif
67 #include "wmspec.h"
69 #define MOD_MASK wPreferences.modifier_mask
71 /****** Global Variables ******/
72 extern WShortKey wKeyBindings[WKBD_LAST];
74 #ifdef SHAPE
75 extern Bool wShapeSupported;
76 #endif
78 /* contexts */
79 extern XContext wWinContext;
81 /* protocol atoms */
82 extern Atom _XA_WM_DELETE_WINDOW;
83 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
84 extern Atom _XA_WINDOWMAKER_STATE;
85 extern WPreferences wPreferences;
86 extern Time LastTimestamp;
88 /***** Local Stuff *****/
89 static WWindowState *windowState = NULL;
90 static FocusMode getFocusMode(WWindow *wwin);
91 static int getSavedState(Window window, WSavedState **state);
92 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
94 /* frame window (during window grabs) */
95 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
97 /* close button */
98 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
99 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
101 /* iconify button */
102 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
104 #ifdef XKB_BUTTON_HINT
105 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
106 #endif
108 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
109 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
110 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
112 static void release_wwindowstate(WWindowState *wstate);
114 /****** Notification Observers ******/
116 static void appearanceObserver(void *self, WMNotification * notif)
118 WWindow *wwin = (WWindow *) self;
119 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
121 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
122 return;
124 if (flags & WFontSettings) {
125 wWindowConfigureBorders(wwin);
126 if (wwin->flags.shaded) {
127 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
128 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
129 wWindowSynthConfigureNotify(wwin);
132 if (flags & WTextureSettings)
133 wwin->frame->flags.need_texture_remake = 1;
135 if (flags & (WTextureSettings | WColorSettings)) {
136 if (wwin->frame->titlebar)
137 XClearWindow(dpy, wwin->frame->titlebar->window);
139 wFrameWindowPaint(wwin->frame);
144 /* Return the WWindow associated with a given (Xlib) Window. */
145 WWindow *wWindowFor(Window window)
147 WObjDescriptor *desc;
149 if (window == None)
150 return NULL;
152 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
153 return NULL;
155 if (desc->parent_type == WCLASS_WINDOW)
156 return desc->parent;
157 else if (desc->parent_type == WCLASS_FRAME) {
158 WFrameWindow *frame = (WFrameWindow *) desc->parent;
159 if (frame->flags.is_client_window_frame)
160 return frame->child;
163 return NULL;
166 WWindow *wWindowCreate(void)
168 WWindow *wwin;
170 wwin = wmalloc(sizeof(WWindow));
171 wretain(wwin);
173 wwin->client_descriptor.handle_mousedown = frameMouseDown;
174 wwin->client_descriptor.parent = wwin;
175 wwin->client_descriptor.self = wwin;
176 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
178 return wwin;
181 void wWindowDestroy(WWindow *wwin)
183 int i;
185 if (wwin->screen_ptr->cmap_window == wwin)
186 wwin->screen_ptr->cmap_window = NULL;
188 WMRemoveNotificationObserver(wwin);
190 wwin->flags.destroyed = 1;
192 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
193 if (!wwin->screen_ptr->shortcutWindows[i])
194 continue;
196 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
198 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
199 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
200 wwin->screen_ptr->shortcutWindows[i] = NULL;
204 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
205 wwin->fake_group->retainCount--;
206 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
207 XDestroyWindow(dpy, wwin->fake_group->leader);
208 wwin->fake_group->leader = None;
209 wwin->fake_group->origLeader = None;
210 XFlush(dpy);
214 if (wwin->normal_hints)
215 XFree(wwin->normal_hints);
217 if (wwin->wm_hints)
218 XFree(wwin->wm_hints);
220 if (wwin->wm_instance)
221 XFree(wwin->wm_instance);
223 if (wwin->wm_class)
224 XFree(wwin->wm_class);
226 if (wwin->wm_gnustep_attr)
227 wfree(wwin->wm_gnustep_attr);
229 if (wwin->cmap_windows)
230 XFree(wwin->cmap_windows);
232 XDeleteContext(dpy, wwin->client_win, wWinContext);
234 if (wwin->frame)
235 wFrameWindowDestroy(wwin->frame);
237 if (wwin->icon) {
238 RemoveFromStackList(wwin->icon->core);
239 wIconDestroy(wwin->icon);
240 if (wPreferences.auto_arrange_icons)
241 wArrangeIcons(wwin->screen_ptr, True);
243 if (wwin->net_icon_image)
244 RReleaseImage(wwin->net_icon_image);
246 wrelease(wwin);
249 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
251 if (gs_hints->flags & GSWindowStyleAttr) {
252 if (gs_hints->window_style == WMBorderlessWindowMask) {
253 wwin->client_flags.no_border = 1;
254 wwin->client_flags.no_titlebar = 1;
255 wwin->client_flags.no_closable = 1;
256 wwin->client_flags.no_miniaturizable = 1;
257 wwin->client_flags.no_resizable = 1;
258 wwin->client_flags.no_close_button = 1;
259 wwin->client_flags.no_miniaturize_button = 1;
260 wwin->client_flags.no_resizebar = 1;
261 } else {
262 wwin->client_flags.no_close_button =
263 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
265 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
267 wwin->client_flags.no_miniaturize_button =
268 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
270 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
272 wwin->client_flags.no_resizebar =
273 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
275 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
277 /* these attributes supposedly imply in the existence
278 * of a titlebar */
279 if (gs_hints->window_style & (WMResizableWindowMask |
280 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
281 wwin->client_flags.no_titlebar = 0;
282 } else {
283 wwin->client_flags.no_titlebar =
284 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
288 } else {
289 /* setup the defaults */
290 wwin->client_flags.no_border = 0;
291 wwin->client_flags.no_titlebar = 0;
292 wwin->client_flags.no_closable = 0;
293 wwin->client_flags.no_miniaturizable = 0;
294 wwin->client_flags.no_resizable = 0;
295 wwin->client_flags.no_close_button = 0;
296 wwin->client_flags.no_miniaturize_button = 0;
297 wwin->client_flags.no_resizebar = 0;
299 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
300 wwin->client_flags.no_appicon = 1;
303 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
305 WScreen *scr = wwin->screen_ptr;
307 /* sets global default stuff */
308 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
310 * Decoration setting is done in this precedence (lower to higher)
311 * - use global default in the resource database
312 * - guess some settings
313 * - use GNUstep/external window attributes
314 * - set hints specified for the app in the resource DB
317 WSETUFLAG(wwin, broken_close, 0);
319 if (wwin->protocols.DELETE_WINDOW)
320 WSETUFLAG(wwin, kill_close, 0);
321 else
322 WSETUFLAG(wwin, kill_close, 1);
324 /* transients can't be iconified or maximized */
325 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
326 WSETUFLAG(wwin, no_miniaturizable, 1);
327 WSETUFLAG(wwin, no_miniaturize_button, 1);
330 /* if the window can't be resized, remove the resizebar */
331 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
332 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
333 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
334 WSETUFLAG(wwin, no_resizable, 1);
335 WSETUFLAG(wwin, no_resizebar, 1);
338 /* set GNUstep window attributes */
339 if (wwin->wm_gnustep_attr) {
340 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
342 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
344 *level = wwin->wm_gnustep_attr->window_level;
346 * INT_MIN is the only illegal window level.
348 if (*level == INT_MIN)
349 *level = INT_MIN + 1;
350 } else {
351 /* setup defaults */
352 *level = WMNormalLevel;
354 } else {
355 int tmp_workspace = -1;
356 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
358 #ifdef MWM_HINTS
359 wMWMCheckClientHints(wwin);
360 #endif /* MWM_HINTS */
362 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
364 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
365 * have INT_MIN that means that no window level was requested. -Dan
367 if (tmp_level == INT_MIN) {
368 if (WFLAGP(wwin, floating))
369 *level = WMFloatingLevel;
370 else if (WFLAGP(wwin, sunken))
371 *level = WMSunkenLevel;
372 else
373 *level = WMNormalLevel;
374 } else {
375 *level = tmp_level;
378 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
379 WWindow *transientOwner = wWindowFor(wwin->transient_for);
380 if (transientOwner) {
381 int ownerLevel = transientOwner->frame->core->stacking->window_level;
382 if (ownerLevel > *level)
383 *level = ownerLevel;
387 if (tmp_workspace >= 0)
388 *workspace = tmp_workspace % scr->workspace_count;
392 * Set attributes specified only for that window/class.
393 * This might do duplicate work with the 1st wDefaultFillAttributes().
395 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
396 &wwin->defined_user_flags, False);
398 * Sanity checks for attributes that depend on other attributes
400 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
401 wwin->user_flags.emulate_appicon = 0;
403 if (wwin->main_window != None) {
404 WApplication *wapp = wApplicationOf(wwin->main_window);
405 if (wapp && !wapp->flags.emulated)
406 wwin->user_flags.emulate_appicon = 0;
409 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
410 wwin->user_flags.emulate_appicon = 0;
412 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
413 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
414 wwin->user_flags.sunken = 0;
416 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
418 /* windows that have takefocus=False shouldn't take focus at all */
419 if (wwin->focus_mode == WFM_NO_INPUT)
420 wwin->client_flags.no_focusable = 1;
423 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
425 int w1, h1, w2, h2;
427 w1 = wwin->frame->core->width;
428 h1 = wwin->frame->core->height;
429 w2 = obscured->frame->core->width;
430 h2 = obscured->frame->core->height;
432 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
433 && wwin->frame->workspace != obscured->frame->workspace)
434 return False;
436 if (wwin->frame_x + w1 < obscured->frame_x
437 || wwin->frame_y + h1 < obscured->frame_y
438 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
439 return False;
441 return True;
444 static void fixLeaderProperties(WWindow *wwin)
446 XClassHint *classHint;
447 XWMHints *hints, *clientHints;
448 XWindowAttributes attr;
449 Window leaders[2], window;
450 char **argv, *command;
451 int argc, i, pid;
452 Bool haveCommand;
454 classHint = XAllocClassHint();
455 clientHints = XGetWMHints(dpy, wwin->client_win);
456 pid = wNETWMGetPidForWindow(wwin->client_win);
457 if (pid > 0)
458 haveCommand = GetCommandForPid(pid, &argv, &argc);
459 else
460 haveCommand = False;
462 leaders[0] = wwin->client_leader;
463 leaders[1] = wwin->group_id;
465 if (haveCommand) {
466 command = GetCommandForWindow(wwin->client_win);
467 if (command)
468 wfree(command); /* command already set. nothing to do. */
469 else
470 XSetCommand(dpy, wwin->client_win, argv, argc);
473 for (i = 0; i < 2; i++) {
474 window = leaders[i];
475 if (window) {
476 if (XGetClassHint(dpy, window, classHint) == 0) {
477 classHint->res_name = wwin->wm_instance;
478 classHint->res_class = wwin->wm_class;
479 XSetClassHint(dpy, window, classHint);
481 hints = XGetWMHints(dpy, window);
482 if (hints) {
483 XFree(hints);
484 } else if (clientHints) {
485 /* set window group leader to self */
486 clientHints->window_group = window;
487 clientHints->flags |= WindowGroupHint;
488 XSetWMHints(dpy, window, clientHints);
491 if (haveCommand) {
492 command = GetCommandForWindow(window);
493 if (command)
494 wfree(command); /* command already set. nothing to do. */
495 else
496 XSetCommand(dpy, window, argv, argc);
499 /* Make sure we get notification when this window is destroyed */
500 if (XGetWindowAttributes(dpy, window, &attr))
501 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
505 XFree(classHint);
506 if (clientHints)
507 XFree(clientHints);
508 if (haveCommand)
509 wfree(argv);
512 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
514 XClassHint *classHint;
515 XWMHints *hints;
516 Window leader;
517 int argc;
518 char **argv;
520 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
521 /* set class hint */
522 classHint = XAllocClassHint();
523 classHint->res_name = instance;
524 classHint->res_class = class;
525 XSetClassHint(dpy, leader, classHint);
526 XFree(classHint);
528 /* inherit these from the original leader if available */
529 hints = XGetWMHints(dpy, win);
530 if (!hints) {
531 hints = XAllocWMHints();
532 hints->flags = 0;
534 /* set window group leader to self */
535 hints->window_group = leader;
536 hints->flags |= WindowGroupHint;
537 XSetWMHints(dpy, leader, hints);
538 XFree(hints);
540 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
541 XSetCommand(dpy, leader, argv, argc);
542 XFreeStringList(argv);
545 return leader;
548 static int matchIdentifier(const void *item, const void *cdata)
550 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
554 *----------------------------------------------------------------
555 * wManageWindow--
556 * reparents the window and allocates a descriptor for it.
557 * Window manager hints and other hints are fetched to configure
558 * the window decoration attributes and others. User preferences
559 * for the window are used if available, to configure window
560 * decorations and some behaviour.
561 * If in startup, windows that are override redirect,
562 * unmapped and never were managed and are Withdrawn are not
563 * managed.
565 * Returns:
566 * the new window descriptor
568 * Side effects:
569 * The window is reparented and appropriate notification
570 * is done to the client. Input mask for the window is setup.
571 * The window descriptor is also associated with various window
572 * contexts and inserted in the head of the window list.
573 * Event handler contexts are associated for some objects
574 * (buttons, titlebar and resizebar)
576 *----------------------------------------------------------------
578 WWindow *wManageWindow(WScreen *scr, Window window)
580 WWindow *wwin;
581 int x, y;
582 unsigned width, height;
583 XWindowAttributes wattribs;
584 XSetWindowAttributes attribs;
585 WWindowState *win_state;
586 WWindow *transientOwner = NULL;
587 int window_level;
588 int wm_state;
589 int foo;
590 int workspace = -1;
591 char *title;
592 Bool withdraw = False;
593 Bool raise = False;
594 Bool frame_adjustment = True;
596 /* mutex. */
597 XGrabServer(dpy);
598 XSync(dpy, False);
600 /* make sure the window is still there */
601 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
602 XUngrabServer(dpy);
603 return NULL;
606 /* if it's an override-redirect, ignore it */
607 if (wattribs.override_redirect) {
608 XUngrabServer(dpy);
609 return NULL;
612 wm_state = PropGetWindowState(window);
614 /* if it's startup and the window is unmapped, don't manage it */
615 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
616 XUngrabServer(dpy);
617 return NULL;
620 wwin = wWindowCreate();
622 title = wNETWMGetWindowName(window);
623 if (title)
624 wwin->flags.net_has_title = 1;
625 else if (!wFetchName(dpy, window, &title))
626 title = NULL;
628 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
630 #ifdef SHAPE
631 if (wShapeSupported) {
632 int junk;
633 unsigned int ujunk;
634 int b_shaped;
636 XShapeSelectInput(dpy, window, ShapeNotifyMask);
637 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
638 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
639 wwin->flags.shaped = b_shaped;
641 #endif
643 /* Get hints and other information in properties */
644 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
646 /* setup descriptor */
647 wwin->client_win = window;
648 wwin->screen_ptr = scr;
649 wwin->old_border_width = wattribs.border_width;
650 wwin->event_mask = CLIENT_EVENTS;
651 attribs.event_mask = CLIENT_EVENTS;
652 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
653 attribs.save_under = False;
655 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
656 XSetWindowBorderWidth(dpy, window, 0);
658 /* get hints from GNUstep app */
659 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
660 wwin->flags.is_gnustep = 1;
662 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
663 wwin->wm_gnustep_attr = NULL;
665 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
666 wwin->flags.is_dockapp = 1;
667 withdraw = True;
670 wwin->client_leader = PropGetClientLeader(window);
671 if (wwin->client_leader != None)
672 wwin->main_window = wwin->client_leader;
674 wwin->wm_hints = XGetWMHints(dpy, window);
676 if (wwin->wm_hints) {
677 if (wwin->wm_hints->flags & StateHint) {
678 if (wwin->wm_hints->initial_state == IconicState) {
679 wwin->flags.miniaturized = 1;
680 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
681 wwin->flags.is_dockapp = 1;
682 withdraw = True;
686 if (wwin->wm_hints->flags & WindowGroupHint) {
687 wwin->group_id = wwin->wm_hints->window_group;
688 /* window_group has priority over CLIENT_LEADER */
689 wwin->main_window = wwin->group_id;
690 } else {
691 wwin->group_id = None;
694 if (wwin->wm_hints->flags & UrgencyHint) {
695 wwin->flags.urgent = 1;
696 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
698 } else {
699 wwin->group_id = None;
702 PropGetProtocols(window, &wwin->protocols);
704 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
705 wwin->transient_for = None;
706 } else {
707 if (wwin->transient_for == None || wwin->transient_for == window) {
708 wwin->transient_for = scr->root_win;
709 } else {
710 transientOwner = wWindowFor(wwin->transient_for);
711 if (transientOwner && transientOwner->main_window != None)
712 wwin->main_window = transientOwner->main_window;
716 /* guess the focus mode */
717 wwin->focus_mode = getFocusMode(wwin);
719 /* get geometry stuff */
720 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
722 /* get colormap windows */
723 GetColormapWindows(wwin);
726 * Setup the decoration/window attributes and
727 * geometry
729 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
731 /* Make broken apps behave as a nice app. */
732 if (WFLAGP(wwin, emulate_appicon))
733 wwin->main_window = wwin->client_win;
735 fixLeaderProperties(wwin);
737 wwin->orig_main_window = wwin->main_window;
739 if (wwin->flags.is_gnustep)
740 WSETUFLAG(wwin, shared_appicon, 0);
742 if (wwin->main_window) {
743 extern Atom _XA_WINDOWMAKER_MENU;
744 XTextProperty text_prop;
746 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
747 WSETUFLAG(wwin, shared_appicon, 0);
750 if (wwin->flags.is_dockapp)
751 WSETUFLAG(wwin, shared_appicon, 0);
753 if (wwin->main_window) {
754 WApplication *app = wApplicationOf(wwin->main_window);
755 if (app && app->app_icon)
756 WSETUFLAG(wwin, shared_appicon, 0);
759 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
760 char *buffer, *instance, *class;
761 WFakeGroupLeader *fPtr;
762 int index;
764 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
766 /* // only enter here if PropGetWMClass() succeds */
767 PropGetWMClass(wwin->main_window, &class, &instance);
768 buffer = StrConcatDot(instance, class);
770 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
771 if (index != WANotFound) {
772 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
773 if (fPtr->retainCount == 0)
774 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
775 instance, class);
777 fPtr->retainCount++;
778 if (fPtr->origLeader == None) {
779 if (ADEQUATE(wwin->main_window)) {
780 fPtr->retainCount++;
781 fPtr->origLeader = wwin->main_window;
784 wwin->fake_group = fPtr;
785 wwin->main_window = fPtr->leader;
786 wfree(buffer);
787 } else {
788 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
790 fPtr->identifier = buffer;
791 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
792 fPtr->origLeader = None;
793 fPtr->retainCount = 1;
795 WMAddToArray(scr->fakeGroupLeaders, fPtr);
797 if (ADEQUATE(wwin->main_window)) {
798 fPtr->retainCount++;
799 fPtr->origLeader = wwin->main_window;
801 wwin->fake_group = fPtr;
802 wwin->main_window = fPtr->leader;
805 if (instance)
806 free(instance);
808 if (class)
809 free(class);
810 #undef ADEQUATE
814 * Setup the initial state of the window
816 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
817 wwin->flags.miniaturized = 1;
819 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
820 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
822 wNETWMCheckInitialClientState(wwin);
824 /* apply previous state if it exists and we're in startup */
825 if (scr->flags.startup && wm_state >= 0) {
826 if (wm_state == IconicState)
827 wwin->flags.miniaturized = 1;
828 else if (wm_state == WithdrawnState)
829 withdraw = True;
832 /* if there is a saved state (from file), restore it */
833 win_state = NULL;
834 if (wwin->main_window != None)
835 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
836 else
837 win_state = (WWindowState *) wWindowGetSavedState(window);
839 if (win_state && !withdraw) {
840 if (win_state->state->hidden > 0)
841 wwin->flags.hidden = win_state->state->hidden;
843 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
844 wwin->flags.shaded = win_state->state->shaded;
846 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
847 wwin->flags.miniaturized = win_state->state->miniaturized;
849 if (!IS_OMNIPRESENT(wwin)) {
850 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
851 wwin->wm_class);
852 if (w < 0 || w >= scr->workspace_count) {
853 workspace = win_state->state->workspace;
854 if (workspace >= scr->workspace_count)
855 workspace = scr->current_workspace;
856 } else {
857 workspace = w;
859 } else {
860 workspace = scr->current_workspace;
864 /* if we're restarting, restore saved state (from hints).
865 * This will overwrite previous */
867 WSavedState *wstate;
869 if (getSavedState(window, &wstate)) {
870 wwin->flags.shaded = wstate->shaded;
871 wwin->flags.hidden = wstate->hidden;
872 wwin->flags.miniaturized = wstate->miniaturized;
873 wwin->flags.maximized = wstate->maximized;
874 if (wwin->flags.maximized) {
875 wwin->old_geometry.x = wstate->x;
876 wwin->old_geometry.y = wstate->y;
877 wwin->old_geometry.width = wstate->w;
878 wwin->old_geometry.height = wstate->h;
881 workspace = wstate->workspace;
882 } else {
883 wstate = NULL;
886 /* restore window shortcut */
887 if (wstate != NULL || win_state != NULL) {
888 unsigned mask = 0;
890 if (win_state != NULL)
891 mask = win_state->state->window_shortcuts;
893 if (wstate != NULL && mask == 0)
894 mask = wstate->window_shortcuts;
896 if (mask > 0) {
897 int i;
899 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
900 if (mask & (1 << i)) {
901 if (!scr->shortcutWindows[i])
902 scr->shortcutWindows[i] = WMCreateArray(4);
904 WMAddToArray(scr->shortcutWindows[i], wwin);
910 if (wstate != NULL)
911 wfree(wstate);
914 /* don't let transients start miniaturized if their owners are not */
915 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
916 wwin->flags.miniaturized = 0;
917 if (wwin->wm_hints)
918 wwin->wm_hints->initial_state = NormalState;
921 /* set workspace on which the window starts */
922 if (workspace >= 0) {
923 if (workspace > scr->workspace_count - 1)
924 workspace = workspace % scr->workspace_count;
925 } else {
926 int w;
928 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
930 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
931 workspace = w;
932 } else {
933 if (wPreferences.open_transients_with_parent && transientOwner)
934 workspace = transientOwner->frame->workspace;
935 else
936 workspace = scr->current_workspace;
940 /* setup window geometry */
941 if (win_state && win_state->state->w > 0) {
942 width = win_state->state->w;
943 height = win_state->state->h;
946 wWindowConstrainSize(wwin, &width, &height);
948 /* do not ask for window placement if the window is
949 * transient, during startup, if the initial workspace is another one
950 * or if the window wants to start iconic.
951 * If geometry was saved, restore it. */
953 Bool dontBring = False;
955 if (win_state && win_state->state->w > 0) {
956 x = win_state->state->x;
957 y = win_state->state->y;
958 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
959 && !scr->flags.startup
960 && workspace == scr->current_workspace
961 && !wwin->flags.miniaturized
962 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
964 if (transientOwner && transientOwner->flags.mapped) {
965 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
966 WMRect rect;
967 int head;
969 x = transientOwner->frame_x +
970 abs((transientOwner->frame->core->width - width) / 2) + offs;
971 y = transientOwner->frame_y +
972 abs((transientOwner->frame->core->height - height) / 3) + offs;
974 /* limit transient windows to be inside their parent's head */
975 rect.pos.x = transientOwner->frame_x;
976 rect.pos.y = transientOwner->frame_y;
977 rect.size.width = transientOwner->frame->core->width;
978 rect.size.height = transientOwner->frame->core->height;
980 head = wGetHeadForRect(scr, rect);
981 rect = wGetRectForHead(scr, head);
983 if (x < rect.pos.x)
984 x = rect.pos.x;
985 else if (x + width > rect.pos.x + rect.size.width)
986 x = rect.pos.x + rect.size.width - width;
988 if (y < rect.pos.y)
989 y = rect.pos.y;
990 else if (y + height > rect.pos.y + rect.size.height)
991 y = rect.pos.y + rect.size.height - height;
993 } else {
994 PlaceWindow(wwin, &x, &y, width, height);
995 frame_adjustment = False;
998 if (wPreferences.window_placement == WPM_MANUAL)
999 dontBring = True;
1001 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1002 int head, flags;
1003 WMRect rect;
1004 int reposition = 0;
1006 /* Make spash screens come out in the center of a head
1007 * trouble is that most splashies never get here
1008 * they are managed trough atoms but god knows where.
1009 * Dan, do you know ? -peter
1011 * Most of them are not managed, they have set
1012 * OverrideRedirect, which means we can't do anything about
1013 * them. -alfredo */
1015 /* xinerama checks for: across head and dead space */
1016 rect.pos.x = x;
1017 rect.pos.y = y;
1018 rect.size.width = width;
1019 rect.size.height = height;
1021 head = wGetRectPlacementInfo(scr, rect, &flags);
1023 if (flags & XFLAG_DEAD)
1024 reposition = 1;
1026 if (flags & XFLAG_MULTIPLE)
1027 reposition = 2;
1030 switch (reposition) {
1031 case 1:
1032 head = wGetHeadForPointerLocation(scr);
1033 rect = wGetRectForHead(scr, head);
1035 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1036 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1037 break;
1039 case 2:
1040 rect = wGetRectForHead(scr, head);
1042 if (x < rect.pos.x)
1043 x = rect.pos.x;
1044 else if (x + width > rect.pos.x + rect.size.width)
1045 x = rect.pos.x + rect.size.width - width;
1047 if (y < rect.pos.y)
1048 y = rect.pos.y;
1049 else if (y + height > rect.pos.y + rect.size.height)
1050 y = rect.pos.y + rect.size.height - height;
1052 break;
1054 default:
1055 break;
1059 if (WFLAGP(wwin, dont_move_off) && dontBring)
1060 wScreenBringInside(scr, &x, &y, width, height);
1063 wNETWMPositionSplash(wwin, &x, &y, width, height);
1065 if (wwin->flags.urgent) {
1066 if (!IS_OMNIPRESENT(wwin))
1067 wwin->flags.omnipresent ^= 1;
1070 /* Create frame, borders and do reparenting */
1071 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1072 #ifdef XKB_BUTTON_HINT
1073 if (wPreferences.modelock)
1074 foo |= WFF_LANGUAGE_BUTTON;
1075 #endif
1076 if (HAS_TITLEBAR(wwin))
1077 foo |= WFF_TITLEBAR;
1079 if (HAS_RESIZEBAR(wwin))
1080 foo |= WFF_RESIZEBAR;
1082 if (HAS_BORDER(wwin))
1083 foo |= WFF_BORDER;
1085 wwin->frame = wFrameWindowCreate(scr, window_level,
1086 x, y, width, height,
1087 &wPreferences.window_title_clearance,
1088 &wPreferences.window_title_min_height,
1089 &wPreferences.window_title_max_height,
1090 foo,
1091 scr->window_title_texture,
1092 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1093 wattribs.depth, wattribs.visual, wattribs.colormap);
1095 wwin->frame->flags.is_client_window_frame = 1;
1096 wwin->frame->flags.justification = wPreferences.title_justification;
1098 /* setup button images */
1099 wWindowUpdateButtonImages(wwin);
1101 /* hide unused buttons */
1102 foo = 0;
1103 if (WFLAGP(wwin, no_close_button))
1104 foo |= WFF_RIGHT_BUTTON;
1106 if (WFLAGP(wwin, no_miniaturize_button))
1107 foo |= WFF_LEFT_BUTTON;
1109 #ifdef XKB_BUTTON_HINT
1110 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1111 foo |= WFF_LANGUAGE_BUTTON;
1112 #endif
1113 if (foo != 0)
1114 wFrameWindowHideButton(wwin->frame, foo);
1116 wwin->frame->child = wwin;
1117 wwin->frame->workspace = workspace;
1118 wwin->frame->on_click_left = windowIconifyClick;
1120 #ifdef XKB_BUTTON_HINT
1121 if (wPreferences.modelock)
1122 wwin->frame->on_click_language = windowLanguageClick;
1123 #endif
1125 wwin->frame->on_click_right = windowCloseClick;
1126 wwin->frame->on_dblclick_right = windowCloseDblClick;
1127 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1128 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1129 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1131 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1132 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1133 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1136 int gx, gy;
1138 wClientGetGravityOffsets(wwin, &gx, &gy);
1140 /* if gravity is to the south, account for the border sizes */
1141 if (gy > 0)
1142 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1145 if (frame_adjustment) {
1146 WMRect rect;
1147 WArea usableArea;
1148 int head;
1150 rect.pos.x = x;
1151 rect.pos.y = y;
1152 rect.size.width = 1;
1153 rect.size.height = 1;
1155 head = wGetHeadForRect(scr, rect);
1156 usableArea = wGetUsableAreaForHead(scr, head, NULL, True);
1158 /* wWindowConfigure() will account for the frame
1159 * when placing so the window would be shifted without
1160 * the adjustment below */
1161 if (y >= usableArea.y1 + wwin->frame->top_width)
1162 y -= wwin->frame->top_width;
1164 /* wWindowConfigure() will account for the window border
1165 * when placing so the window would be shifted without
1166 * the adjustment below */
1167 if (HAS_BORDER(wwin)) {
1168 if (x >= usableArea.x1 + FRAME_BORDER_WIDTH)
1169 x -= FRAME_BORDER_WIDTH;
1170 if (y >= usableArea.y1 + FRAME_BORDER_WIDTH)
1171 y -= FRAME_BORDER_WIDTH;
1175 /* If this is a newly-mapped window which had a border,
1176 * the absolute co-ordinates reported to us are actually
1177 * the co-ordinates of the border. We, however, track
1178 * the absolute co-ordinates of the client window, offset
1179 * by the title bar and frame border. As a result
1180 * we need to offset placement of the client by the border
1181 * size so its position matches what we expect. */
1182 x += wwin->old_border_width;
1183 y += wwin->old_border_width;
1185 /* wWindowConfigure() will init the client window's size
1186 * (wwin->client.{width,height}) and all other geometry
1187 * related variables (frame_x,frame_y) */
1188 wWindowConfigure(wwin, x, y, width, height);
1190 /* to make sure the window receives it's new position after reparenting */
1191 wWindowSynthConfigureNotify(wwin);
1193 /* Setup descriptors and save window to internal 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;
1209 /* Do application specific stuff, like setting application
1210 * wide attributes. */
1212 if (wwin->flags.hidden) {
1213 /* if the window was set to hidden because it was hidden
1214 * in a previous incarnation and that state was restored */
1215 app->flags.hidden = 1;
1216 } else if (app->flags.hidden) {
1217 if (WFLAGP(app->main_window_desc, start_hidden)) {
1218 wwin->flags.hidden = 1;
1219 } else {
1220 wUnhideApplication(app, False, False);
1221 raise = True;
1224 wAppBounce(app);
1228 /* setup the frame descriptor */
1229 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1230 wwin->frame->core->descriptor.parent = wwin;
1231 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1233 /* don't let windows go away if we die */
1234 XAddToSaveSet(dpy, window);
1236 XLowerWindow(dpy, window);
1238 /* if window is in this workspace and should be mapped, then map it */
1239 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1240 && !wwin->flags.hidden && !withdraw) {
1242 /* The following "if" is to avoid crashing of clients that expect
1243 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1244 * after the return from this function. */
1245 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1246 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1247 else
1248 wClientSetState(wwin, NormalState, None);
1250 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1251 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1253 * The brain damaged idiotic non-click to focus modes will
1254 * have trouble with this because:
1256 * 1. window is created and mapped by the client
1257 * 2. window is mapped by wmaker in small size
1258 * 3. window is animated to grow to normal size
1259 * 4. this function returns to normal event loop
1260 * 5. eventually, the EnterNotify event that would trigger
1261 * the window focusing (if the mouse is over that window)
1262 * will be processed by wmaker.
1263 * But since this event will be rather delayed
1264 * (step 3 has a large delay) the time when the event ocurred
1265 * and when it is processed, the client that owns that window
1266 * will reject the XSetInputFocus() for it.
1268 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1269 DoWindowBirth(wwin);
1271 wWindowMap(wwin);
1274 /* setup stacking descriptor */
1275 if (transientOwner)
1276 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1277 else
1278 wwin->frame->core->stacking->child_of = NULL;
1280 if (!scr->focused_window) {
1281 /* first window on the list */
1282 wwin->next = NULL;
1283 wwin->prev = NULL;
1284 scr->focused_window = wwin;
1285 } else {
1286 WWindow *tmp;
1288 /* add window at beginning of focus window list */
1289 tmp = scr->focused_window;
1290 while (tmp->prev)
1291 tmp = tmp->prev;
1293 tmp->prev = wwin;
1294 wwin->next = tmp;
1295 wwin->prev = NULL;
1298 /* raise is set to true if we un-hid the app when this window was born.
1299 * we raise, else old windows of this app will be above this new one. */
1300 if (raise)
1301 wRaiseFrame(wwin->frame->core);
1303 /* Update name must come after WApplication stuff is done */
1304 wWindowUpdateName(wwin, title);
1305 if (title)
1306 XFree(title);
1308 XUngrabServer(dpy);
1310 /* Final preparations before window is ready to go */
1311 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1313 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1314 if (((transientOwner && transientOwner->flags.focused)
1315 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1317 /* only auto_focus if on same screen as mouse
1318 * (and same head for xinerama mode)
1319 * TODO: make it an option */
1321 /*TODO add checking the head of the window, is it available? */
1322 short same_screen = 0, same_head = 1;
1324 int foo;
1325 unsigned int bar;
1326 Window dummy;
1328 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1329 &foo, &foo, &foo, &foo, &bar) != False)
1330 same_screen = 1;
1332 if (same_screen == 1 && same_head == 1)
1333 wSetFocusTo(scr, wwin);
1336 wWindowResetMouseGrabs(wwin);
1338 if (!WFLAGP(wwin, no_bind_keys))
1339 wWindowSetKeyGrabs(wwin);
1341 WMPostNotificationName(WMNManaged, wwin, NULL);
1342 wColormapInstallForWindow(scr, scr->cmap_window);
1344 /* Setup Notification Observers */
1345 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1347 /* Cleanup temporary stuff */
1348 if (win_state)
1349 wWindowDeleteSavedState(win_state);
1351 /* If the window must be withdrawed, then do it now.
1352 * Must do some optimization, 'though */
1353 if (withdraw) {
1354 wwin->flags.mapped = 0;
1355 wClientSetState(wwin, WithdrawnState, None);
1356 wUnmanageWindow(wwin, True, False);
1357 wwin = NULL;
1360 return wwin;
1363 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1364 char *title, int x, int y, int width, int height)
1366 WWindow *wwin;
1367 int foo;
1369 wwin = wWindowCreate();
1371 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1373 wwin->flags.internal_window = 1;
1375 WSETUFLAG(wwin, omnipresent, 1);
1376 WSETUFLAG(wwin, no_shadeable, 1);
1377 WSETUFLAG(wwin, no_resizable, 1);
1378 WSETUFLAG(wwin, no_miniaturizable, 1);
1380 wwin->focus_mode = WFM_PASSIVE;
1381 wwin->client_win = window;
1382 wwin->screen_ptr = scr;
1383 wwin->transient_for = owner;
1384 wwin->client.x = x;
1385 wwin->client.y = y;
1386 wwin->client.width = width;
1387 wwin->client.height = height;
1388 wwin->frame_x = wwin->client.x;
1389 wwin->frame_y = wwin->client.y;
1391 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1392 foo |= WFF_TITLEBAR;
1393 #ifdef XKB_BUTTON_HINT
1394 foo |= WFF_LANGUAGE_BUTTON;
1395 #endif
1397 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1398 wwin->frame_x, wwin->frame_y,
1399 width, height,
1400 &wPreferences.window_title_clearance,
1401 &wPreferences.window_title_min_height,
1402 &wPreferences.window_title_max_height,
1403 foo,
1404 scr->window_title_texture,
1405 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1406 scr->w_depth, scr->w_visual, scr->w_colormap);
1408 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1410 wwin->frame->flags.is_client_window_frame = 1;
1411 wwin->frame->flags.justification = wPreferences.title_justification;
1413 wFrameWindowChangeTitle(wwin->frame, title);
1415 /* setup button images */
1416 wWindowUpdateButtonImages(wwin);
1418 /* hide buttons */
1419 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1421 wwin->frame->child = wwin;
1422 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1424 #ifdef XKB_BUTTON_HINT
1425 if (wPreferences.modelock)
1426 wwin->frame->on_click_language = windowLanguageClick;
1427 #endif
1429 wwin->frame->on_click_right = windowCloseClick;
1430 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1431 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1432 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1433 wwin->client.y += wwin->frame->top_width;
1435 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1436 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1438 /* setup the frame descriptor */
1439 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1440 wwin->frame->core->descriptor.parent = wwin;
1441 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1443 XLowerWindow(dpy, window);
1444 XMapSubwindows(dpy, wwin->frame->core->window);
1446 /* setup stacking descriptor */
1447 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1448 WWindow *tmp;
1449 tmp = wWindowFor(wwin->transient_for);
1450 if (tmp)
1451 wwin->frame->core->stacking->child_of = tmp->frame->core;
1452 } else {
1453 wwin->frame->core->stacking->child_of = NULL;
1456 if (!scr->focused_window) {
1457 /* first window on the list */
1458 wwin->next = NULL;
1459 wwin->prev = NULL;
1460 scr->focused_window = wwin;
1461 } else {
1462 WWindow *tmp;
1464 /* add window at beginning of focus window list */
1465 tmp = scr->focused_window;
1466 while (tmp->prev)
1467 tmp = tmp->prev;
1468 tmp->prev = wwin;
1469 wwin->next = tmp;
1470 wwin->prev = NULL;
1473 if (wwin->flags.is_gnustep == 0)
1474 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1476 /* if (wPreferences.auto_focus) */
1477 wSetFocusTo(scr, wwin);
1478 wWindowResetMouseGrabs(wwin);
1479 wWindowSetKeyGrabs(wwin);
1481 return wwin;
1485 *----------------------------------------------------------------------
1486 * wUnmanageWindow--
1487 * Removes the frame window from a window and destroys all data
1488 * related to it. The window will be reparented back to the root window
1489 * if restore is True.
1491 * Side effects:
1492 * Everything related to the window is destroyed and the window
1493 * is removed from the window lists. Focus is set to the previous on the
1494 * window list.
1495 *----------------------------------------------------------------------
1497 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1499 WCoreWindow *frame = wwin->frame->core;
1500 WWindow *owner = NULL;
1501 WWindow *newFocusedWindow = NULL;
1502 int wasFocused;
1503 WScreen *scr = wwin->screen_ptr;
1505 /* First close attribute editor window if open */
1506 if (wwin->flags.inspector_open)
1507 wCloseInspectorForWindow(wwin);
1509 /* Close window menu if it's open for this window */
1510 if (wwin->flags.menu_open_for_me)
1511 CloseWindowMenu(scr);
1513 if (!destroyed) {
1514 if (!wwin->flags.internal_window)
1515 XRemoveFromSaveSet(dpy, wwin->client_win);
1517 /* If this is a leader window, we still need to listen for
1518 * DestroyNotify and PropertyNotify. */
1519 if (wApplicationOf(wwin->client_win))
1520 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1521 else
1522 XSelectInput(dpy, wwin->client_win, NoEventMask);
1524 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1525 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1528 XUnmapWindow(dpy, frame->window);
1530 XUnmapWindow(dpy, wwin->client_win);
1532 /* deselect window */
1533 wSelectWindow(wwin, False);
1535 /* remove all pending events on window */
1536 /* I think this only matters for autoraise */
1537 if (wPreferences.raise_delay)
1538 WMDeleteTimerWithClientData(wwin->frame->core);
1540 XFlush(dpy);
1542 /* reparent the window back to the root */
1543 if (restore)
1544 wClientRestore(wwin);
1546 if (wwin->transient_for != scr->root_win) {
1547 owner = wWindowFor(wwin->transient_for);
1548 if (owner) {
1549 if (!owner->flags.semi_focused)
1550 owner = NULL;
1551 else
1552 owner->flags.semi_focused = 0;
1556 wasFocused = wwin->flags.focused;
1558 /* remove from window focus list */
1559 if (!wwin->prev && !wwin->next) {
1560 /* was the only window */
1561 scr->focused_window = NULL;
1562 newFocusedWindow = NULL;
1563 } else {
1564 WWindow *tmp;
1566 if (wwin->prev)
1567 wwin->prev->next = wwin->next;
1569 if (wwin->next)
1570 wwin->next->prev = wwin->prev;
1571 else {
1572 scr->focused_window = wwin->prev;
1573 scr->focused_window->next = NULL;
1576 if (wPreferences.focus_mode == WKF_CLICK) {
1578 /* if in click to focus mode and the window
1579 * was a transient, focus the owner window
1581 tmp = NULL;
1582 if (wPreferences.focus_mode == WKF_CLICK) {
1583 tmp = wWindowFor(wwin->transient_for);
1584 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1585 tmp = NULL;
1588 /* otherwise, focus the next one in the focus list */
1589 if (!tmp) {
1590 tmp = scr->focused_window;
1591 while (tmp) { /* look for one in the window list first */
1592 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1593 && (tmp->flags.mapped || tmp->flags.shaded))
1594 break;
1595 tmp = tmp->prev;
1597 if (!tmp) { /* if unsuccessful, choose any focusable window */
1598 tmp = scr->focused_window;
1599 while (tmp) {
1600 if (!WFLAGP(tmp, no_focusable)
1601 && (tmp->flags.mapped || tmp->flags.shaded))
1602 break;
1603 tmp = tmp->prev;
1608 newFocusedWindow = tmp;
1610 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1611 unsigned int mask;
1612 int foo;
1613 Window bar, win;
1615 /* This is to let the root window get the keyboard input
1616 * if Sloppy focus mode and no other window get focus.
1617 * This way keybindings will not freeze.
1619 tmp = NULL;
1620 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1621 tmp = wWindowFor(win);
1622 if (tmp == wwin)
1623 tmp = NULL;
1624 newFocusedWindow = tmp;
1625 } else {
1626 newFocusedWindow = NULL;
1630 if (!wwin->flags.internal_window)
1631 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1632 if (wasFocused) {
1633 if (newFocusedWindow != owner && owner) {
1634 if (wwin->flags.is_gnustep == 0)
1635 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1637 wSetFocusTo(scr, newFocusedWindow);
1640 /* Close menu and unhighlight */
1641 WApplication *oapp = wApplicationOf(wwin->main_window);
1642 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1643 if (oapp && oapp != napp) {
1644 wAppMenuUnmap(oapp->menu);
1645 if (wPreferences.highlight_active_app)
1646 wApplicationDeactivate(oapp);
1649 wNETCleanupFrameExtents(wwin);
1651 wWindowDestroy(wwin);
1652 XFlush(dpy);
1655 void wWindowMap(WWindow *wwin)
1657 XMapWindow(dpy, wwin->frame->core->window);
1658 if (!wwin->flags.shaded) {
1659 /* window will be remapped when getting MapNotify */
1660 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1661 XMapWindow(dpy, wwin->client_win);
1662 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1664 wwin->flags.mapped = 1;
1668 void wWindowUnmap(WWindow *wwin)
1670 wwin->flags.mapped = 0;
1672 /* prevent window withdrawal when getting UnmapNotify */
1673 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1674 XUnmapWindow(dpy, wwin->client_win);
1675 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1677 XUnmapWindow(dpy, wwin->frame->core->window);
1680 void wWindowFocus(WWindow *wwin, WWindow *owin)
1682 WWindow *nowner;
1683 WWindow *oowner;
1685 #ifdef KEEP_XKB_LOCK_STATUS
1686 if (wPreferences.modelock)
1687 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1688 #endif /* KEEP_XKB_LOCK_STATUS */
1690 wwin->flags.semi_focused = 0;
1692 if (wwin->flags.is_gnustep == 0)
1693 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1695 wwin->flags.focused = 1;
1697 wWindowResetMouseGrabs(wwin);
1699 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1701 if (owin == wwin || !owin)
1702 return;
1704 nowner = wWindowFor(wwin->transient_for);
1706 /* new window is a transient for the old window */
1707 if (nowner == owin) {
1708 owin->flags.semi_focused = 1;
1709 wWindowUnfocus(nowner);
1710 return;
1713 oowner = wWindowFor(owin->transient_for);
1715 /* new window is owner of old window */
1716 if (wwin == oowner) {
1717 wWindowUnfocus(owin);
1718 return;
1721 if (!nowner) {
1722 wWindowUnfocus(owin);
1723 return;
1726 /* new window has same owner of old window */
1727 if (oowner == nowner) {
1728 /* prevent unfocusing of owner */
1729 oowner->flags.semi_focused = 0;
1730 wWindowUnfocus(owin);
1731 oowner->flags.semi_focused = 1;
1733 return;
1736 /* nowner != NULL && oowner != nowner */
1737 nowner->flags.semi_focused = 1;
1738 wWindowUnfocus(nowner);
1739 wWindowUnfocus(owin);
1742 void wWindowUnfocus(WWindow *wwin)
1744 CloseWindowMenu(wwin->screen_ptr);
1746 if (wwin->flags.is_gnustep == 0)
1747 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1749 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1750 WWindow *owner;
1751 owner = wWindowFor(wwin->transient_for);
1752 if (owner && owner->flags.semi_focused) {
1753 owner->flags.semi_focused = 0;
1754 if (owner->flags.mapped || owner->flags.shaded) {
1755 wWindowUnfocus(owner);
1756 wFrameWindowPaint(owner->frame);
1760 wwin->flags.focused = 0;
1761 wWindowResetMouseGrabs(wwin);
1762 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1765 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1767 char *title;
1769 if (!wwin->frame)
1770 return;
1772 if (!newTitle)
1773 title = DEF_WINDOW_TITLE; /* the hint was removed */
1774 else
1775 title = newTitle;
1777 if (wFrameWindowChangeTitle(wwin->frame, title))
1778 WMPostNotificationName(WMNChangedName, wwin, NULL);
1782 *----------------------------------------------------------------------
1784 * wWindowConstrainSize--
1785 * Constrains size for the client window, taking the maximal size,
1786 * window resize increments and other size hints into account.
1788 * Returns:
1789 * The closest size to what was given that the client window can
1790 * have.
1792 *----------------------------------------------------------------------
1794 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1796 int width = (int)*nwidth;
1797 int height = (int)*nheight;
1798 int winc = 1;
1799 int hinc = 1;
1800 int minW = 1, minH = 1;
1801 int maxW = wwin->screen_ptr->scr_width * 2;
1802 int maxH = wwin->screen_ptr->scr_height * 2;
1803 int minAX = -1, minAY = -1;
1804 int maxAX = -1, maxAY = -1;
1805 int baseW = 0;
1806 int baseH = 0;
1808 if (wwin->normal_hints) {
1809 winc = wwin->normal_hints->width_inc;
1810 hinc = wwin->normal_hints->height_inc;
1811 minW = wwin->normal_hints->min_width;
1812 minH = wwin->normal_hints->min_height;
1813 maxW = wwin->normal_hints->max_width;
1814 maxH = wwin->normal_hints->max_height;
1815 if (wwin->normal_hints->flags & PAspect) {
1816 minAX = wwin->normal_hints->min_aspect.x;
1817 minAY = wwin->normal_hints->min_aspect.y;
1818 maxAX = wwin->normal_hints->max_aspect.x;
1819 maxAY = wwin->normal_hints->max_aspect.y;
1822 baseW = wwin->normal_hints->base_width;
1823 baseH = wwin->normal_hints->base_height;
1826 if (width < minW)
1827 width = minW;
1828 if (height < minH)
1829 height = minH;
1831 if (width > maxW)
1832 width = maxW;
1833 if (height > maxH)
1834 height = maxH;
1836 /* aspect ratio code borrowed from olwm */
1837 if (minAX > 0) {
1838 /* adjust max aspect ratio */
1839 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1840 if (maxAX > maxAY) {
1841 height = (width * maxAY) / maxAX;
1842 if (height > maxH) {
1843 height = maxH;
1844 width = (height * maxAX) / maxAY;
1846 } else {
1847 width = (height * maxAX) / maxAY;
1848 if (width > maxW) {
1849 width = maxW;
1850 height = (width * maxAY) / maxAX;
1855 /* adjust min aspect ratio */
1856 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1857 if (minAX > minAY) {
1858 height = (width * minAY) / minAX;
1859 if (height < minH) {
1860 height = minH;
1861 width = (height * minAX) / minAY;
1863 } else {
1864 width = (height * minAX) / minAY;
1865 if (width < minW) {
1866 width = minW;
1867 height = (width * minAY) / minAX;
1873 if (baseW != 0)
1874 width = (((width - baseW) / winc) * winc) + baseW;
1875 else
1876 width = (((width - minW) / winc) * winc) + minW;
1878 if (baseH != 0)
1879 height = (((height - baseH) / hinc) * hinc) + baseH;
1880 else
1881 height = (((height - minH) / hinc) * hinc) + minH;
1883 /* broken stupid apps may cause preposterous values for these.. */
1884 if (width > 0)
1885 *nwidth = width;
1886 if (height > 0)
1887 *nheight = height;
1890 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1891 unsigned int *width, unsigned int *height)
1893 int baseW = 0, baseH = 0;
1894 int winc = 1, hinc = 1;
1896 if (wwin->normal_hints) {
1897 baseW = wwin->normal_hints->base_width;
1898 baseH = wwin->normal_hints->base_height;
1900 winc = wwin->normal_hints->width_inc;
1901 hinc = wwin->normal_hints->height_inc;
1904 if (*width > maxW)
1905 *width = maxW - (maxW - baseW) % winc;
1907 if (*height > maxH)
1908 *height = maxH - (maxH - baseH) % hinc;
1911 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1913 WScreen *scr = wwin->screen_ptr;
1914 WApplication *wapp;
1915 int unmap = 0;
1917 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1918 return;
1920 if (workspace != scr->current_workspace) {
1921 /* Sent to other workspace. Unmap window */
1922 if ((wwin->flags.mapped
1923 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1924 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1926 wapp = wApplicationOf(wwin->main_window);
1927 if (wapp)
1928 wapp->last_workspace = workspace;
1930 if (wwin->flags.miniaturized) {
1931 if (wwin->icon) {
1932 XUnmapWindow(dpy, wwin->icon->core->window);
1933 wwin->icon->mapped = 0;
1935 } else {
1936 unmap = 1;
1937 wSetFocusTo(scr, NULL);
1940 } else {
1941 /* brought to current workspace. Map window */
1942 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1943 if (wwin->icon) {
1944 XMapWindow(dpy, wwin->icon->core->window);
1945 wwin->icon->mapped = 1;
1947 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1948 wWindowMap(wwin);
1951 if (!IS_OMNIPRESENT(wwin)) {
1952 int oldWorkspace = wwin->frame->workspace;
1953 wwin->frame->workspace = workspace;
1954 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1957 if (unmap)
1958 wWindowUnmap(wwin);
1961 void wWindowSynthConfigureNotify(WWindow *wwin)
1963 XEvent sevent;
1965 sevent.type = ConfigureNotify;
1966 sevent.xconfigure.display = dpy;
1967 sevent.xconfigure.event = wwin->client_win;
1968 sevent.xconfigure.window = wwin->client_win;
1970 sevent.xconfigure.x = wwin->client.x;
1971 sevent.xconfigure.y = wwin->client.y;
1972 sevent.xconfigure.width = wwin->client.width;
1973 sevent.xconfigure.height = wwin->client.height;
1975 sevent.xconfigure.border_width = wwin->old_border_width;
1976 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1977 sevent.xconfigure.above = wwin->frame->titlebar->window;
1978 else
1979 sevent.xconfigure.above = None;
1981 sevent.xconfigure.override_redirect = False;
1982 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1983 XFlush(dpy);
1987 *----------------------------------------------------------------------
1988 * wWindowConfigure()
1990 * req_x, req_y: new requested positions for the frame
1991 * req_width, req_height: new requested sizes for the client
1993 * Configures the frame, decorations and client window to the specified
1994 * geometry, whose validity is not checked -- wWindowConstrainSize()
1995 * must be used for that.
1996 * The size parameters are for the client window, but the position is
1997 * for the frame.
1998 * The client window receives a ConfigureNotify event, according
1999 * to what ICCCM says.
2001 * Returns:
2002 * None
2004 * Side effects:
2005 * Window size and position are changed and client window receives
2006 * a ConfigureNotify event.
2007 *----------------------------------------------------------------------
2009 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2011 int synth_notify = False;
2012 int resize;
2014 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2016 * if the window is being moved but not resized then
2017 * send a synthetic ConfigureNotify
2019 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2020 synth_notify = True;
2022 if (WFLAGP(wwin, dont_move_off))
2023 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2025 if (resize) {
2026 if (req_width < MIN_WINDOW_SIZE)
2027 req_width = MIN_WINDOW_SIZE;
2028 if (req_height < MIN_WINDOW_SIZE)
2029 req_height = MIN_WINDOW_SIZE;
2031 /* If growing, resize inner part before frame,
2032 * if shrinking, resize frame before.
2033 * This will prevent the frame (that can have a different color)
2034 * to be exposed, causing flicker */
2035 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2036 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2038 if (wwin->flags.shaded) {
2039 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2040 wwin->old_geometry.height = req_height;
2041 } else {
2042 int h;
2044 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2046 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2049 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2050 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2052 wwin->client.x = req_x;
2053 wwin->client.y = req_y + wwin->frame->top_width;
2054 wwin->client.width = req_width;
2055 wwin->client.height = req_height;
2056 } else {
2057 wwin->client.x = req_x;
2058 wwin->client.y = req_y + wwin->frame->top_width;
2060 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2062 wwin->frame_x = req_x;
2063 wwin->frame_y = req_y;
2064 if (HAS_BORDER(wwin)) {
2065 wwin->client.x += FRAME_BORDER_WIDTH;
2066 wwin->client.y += FRAME_BORDER_WIDTH;
2068 #ifdef SHAPE
2069 if (wShapeSupported && wwin->flags.shaped && resize)
2070 wWindowSetShape(wwin);
2071 #endif
2073 if (synth_notify)
2074 wWindowSynthConfigureNotify(wwin);
2076 wNETFrameExtents(wwin);
2078 XFlush(dpy);
2081 /* req_x, req_y: new position of the frame */
2082 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2084 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2085 int synth_notify = False;
2087 /* Send a synthetic ConfigureNotify event for every window movement. */
2088 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2089 synth_notify = True;
2090 #else
2091 /* A single synthetic ConfigureNotify event is sent at the end of
2092 * a completed (opaque) movement in moveres.c */
2093 #endif
2095 if (WFLAGP(wwin, dont_move_off))
2096 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2097 wwin->frame->core->width, wwin->frame->core->height);
2099 wwin->client.x = req_x;
2100 wwin->client.y = req_y + wwin->frame->top_width;
2101 if (HAS_BORDER(wwin)) {
2102 wwin->client.x += FRAME_BORDER_WIDTH;
2103 wwin->client.y += FRAME_BORDER_WIDTH;
2106 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2108 wwin->frame_x = req_x;
2109 wwin->frame_y = req_y;
2111 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2112 if (synth_notify)
2113 wWindowSynthConfigureNotify(wwin);
2114 #endif
2117 void wWindowUpdateButtonImages(WWindow *wwin)
2119 WScreen *scr = wwin->screen_ptr;
2120 Pixmap pixmap, mask;
2121 WFrameWindow *fwin = wwin->frame;
2123 if (!HAS_TITLEBAR(wwin))
2124 return;
2126 /* miniaturize button */
2127 if (!WFLAGP(wwin, no_miniaturize_button)) {
2128 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2129 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2131 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2132 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2133 else
2134 mask = None;
2136 if (fwin->lbutton_image
2137 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2138 wPixmapDestroy(fwin->lbutton_image);
2139 fwin->lbutton_image = NULL;
2142 if (!fwin->lbutton_image) {
2143 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2144 fwin->lbutton_image->client_owned = 1;
2145 fwin->lbutton_image->client_owned_mask = 1;
2147 } else {
2148 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2149 wPixmapDestroy(fwin->lbutton_image);
2151 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2154 #ifdef XKB_BUTTON_HINT
2155 if (!WFLAGP(wwin, no_language_button)) {
2156 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2157 wPixmapDestroy(fwin->languagebutton_image);
2159 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2161 #endif
2163 /* close button */
2165 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2166 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2168 if (!WFLAGP(wwin, no_close_button)) {
2169 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2170 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2172 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2173 mask = wwin->wm_gnustep_attr->close_mask;
2174 else
2175 mask = None;
2177 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2178 || fwin->rbutton_image->mask != mask)) {
2179 wPixmapDestroy(fwin->rbutton_image);
2180 fwin->rbutton_image = NULL;
2183 if (!fwin->rbutton_image) {
2184 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2185 fwin->rbutton_image->client_owned = 1;
2186 fwin->rbutton_image->client_owned_mask = 1;
2189 } else if (WFLAGP(wwin, kill_close)) {
2190 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2191 wPixmapDestroy(fwin->rbutton_image);
2193 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2195 } else if (MGFLAGP(wwin, broken_close)) {
2196 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2197 wPixmapDestroy(fwin->rbutton_image);
2199 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2201 } else {
2202 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2203 wPixmapDestroy(fwin->rbutton_image);
2205 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2209 /* force buttons to be redrawn */
2210 fwin->flags.need_texture_change = 1;
2211 wFrameWindowPaint(fwin);
2215 *---------------------------------------------------------------------------
2216 * wWindowConfigureBorders--
2217 * Update window border configuration according to attribute flags.
2219 *---------------------------------------------------------------------------
2221 void wWindowConfigureBorders(WWindow *wwin)
2223 if (wwin->frame) {
2224 int flags;
2225 int newx, newy, oldh;
2226 int border_width;
2227 XWindowAttributes attr;
2229 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2231 #ifdef XKB_BUTTON_HINT
2232 if (wPreferences.modelock)
2233 flags |= WFF_LANGUAGE_BUTTON;
2234 #endif
2236 if (HAS_TITLEBAR(wwin))
2237 flags |= WFF_TITLEBAR;
2238 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2239 flags |= WFF_RESIZEBAR;
2240 if (HAS_BORDER(wwin))
2241 flags |= WFF_BORDER;
2242 if (wwin->flags.shaded)
2243 flags |= WFF_IS_SHADED;
2245 if (!XGetWindowAttributes(dpy, wwin->frame->core->window, &attr))
2246 attr.border_width = 0;
2247 border_width = (flags & WFF_BORDER) ? FRAME_BORDER_WIDTH : 0;
2248 oldh = wwin->frame->top_width + attr.border_width;
2249 wFrameWindowUpdateBorders(wwin->frame, flags);
2250 if (oldh != wwin->frame->top_width + border_width) {
2251 newx = wwin->frame_x + attr.border_width - border_width;
2252 newy = wwin->frame_y + oldh - border_width - wwin->frame->top_width;
2254 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2255 wWindowConfigure(wwin, newx, newy, wwin->client.width, wwin->client.height);
2258 flags = 0;
2259 if (!WFLAGP(wwin, no_miniaturize_button)
2260 && wwin->frame->flags.hide_left_button)
2261 flags |= WFF_LEFT_BUTTON;
2263 #ifdef XKB_BUTTON_HINT
2264 if (!WFLAGP(wwin, no_language_button)
2265 && wwin->frame->flags.hide_language_button)
2266 flags |= WFF_LANGUAGE_BUTTON;
2267 #endif
2269 if (!WFLAGP(wwin, no_close_button)
2270 && wwin->frame->flags.hide_right_button)
2271 flags |= WFF_RIGHT_BUTTON;
2273 if (flags != 0) {
2274 wWindowUpdateButtonImages(wwin);
2275 wFrameWindowShowButton(wwin->frame, flags);
2278 flags = 0;
2279 if (WFLAGP(wwin, no_miniaturize_button)
2280 && !wwin->frame->flags.hide_left_button)
2281 flags |= WFF_LEFT_BUTTON;
2283 #ifdef XKB_BUTTON_HINT
2284 if (WFLAGP(wwin, no_language_button)
2285 && !wwin->frame->flags.hide_language_button)
2286 flags |= WFF_LANGUAGE_BUTTON;
2287 #endif
2289 if (WFLAGP(wwin, no_close_button)
2290 && !wwin->frame->flags.hide_right_button)
2291 flags |= WFF_RIGHT_BUTTON;
2293 if (flags != 0)
2294 wFrameWindowHideButton(wwin->frame, flags);
2296 #ifdef SHAPE
2297 if (wShapeSupported && wwin->flags.shaped)
2298 wWindowSetShape(wwin);
2299 #endif
2303 void wWindowSaveState(WWindow * wwin)
2305 long data[10];
2306 int i;
2308 memset(data, 0, sizeof(long) * 10);
2309 data[0] = wwin->frame->workspace;
2310 data[1] = wwin->flags.miniaturized;
2311 data[2] = wwin->flags.shaded;
2312 data[3] = wwin->flags.hidden;
2313 data[4] = wwin->flags.maximized;
2314 if (wwin->flags.maximized == 0) {
2315 data[5] = wwin->frame_x;
2316 data[6] = wwin->frame_y;
2317 data[7] = wwin->frame->core->width;
2318 data[8] = wwin->frame->core->height;
2319 } else {
2320 data[5] = wwin->old_geometry.x;
2321 data[6] = wwin->old_geometry.y;
2322 data[7] = wwin->old_geometry.width;
2323 data[8] = wwin->old_geometry.height;
2326 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2327 if (wwin->screen_ptr->shortcutWindows[i] &&
2328 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2329 data[9] |= 1 << i;
2331 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2332 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2335 static int getSavedState(Window window, WSavedState ** state)
2337 Atom type_ret;
2338 int fmt_ret;
2339 unsigned long nitems_ret;
2340 unsigned long bytes_after_ret;
2341 long *data;
2343 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2344 True, _XA_WINDOWMAKER_STATE,
2345 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2346 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2347 return 0;
2349 *state = wmalloc(sizeof(WSavedState));
2351 (*state)->workspace = data[0];
2352 (*state)->miniaturized = data[1];
2353 (*state)->shaded = data[2];
2354 (*state)->hidden = data[3];
2355 (*state)->maximized = data[4];
2356 (*state)->x = data[5];
2357 (*state)->y = data[6];
2358 (*state)->w = data[7];
2359 (*state)->h = data[8];
2360 (*state)->window_shortcuts = data[9];
2362 XFree(data);
2364 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2365 return 1;
2366 else
2367 return 0;
2370 #ifdef SHAPE
2371 void wWindowClearShape(WWindow * wwin)
2373 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2374 0, wwin->frame->top_width, None, ShapeSet);
2375 XFlush(dpy);
2378 void wWindowSetShape(WWindow * wwin)
2380 XRectangle rect[2];
2381 int count;
2382 #ifdef OPTIMIZE_SHAPE
2383 XRectangle *rects;
2384 XRectangle *urec;
2385 int ordering;
2387 /* only shape is the client's */
2388 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2389 goto alt_code;
2391 /* Get array of rectangles describing the shape mask */
2392 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2393 if (!rects)
2394 goto alt_code;
2396 urec = malloc(sizeof(XRectangle) * (count + 2));
2397 if (!urec) {
2398 XFree(rects);
2399 goto alt_code;
2402 /* insert our decoration rectangles in the rect list */
2403 memcpy(urec, rects, sizeof(XRectangle) * count);
2404 XFree(rects);
2406 if (HAS_TITLEBAR(wwin)) {
2407 urec[count].x = -1;
2408 urec[count].y = -1 - wwin->frame->top_width;
2409 urec[count].width = wwin->frame->core->width + 2;
2410 urec[count].height = wwin->frame->top_width + 1;
2411 count++;
2413 if (HAS_RESIZEBAR(wwin)) {
2414 urec[count].x = -1;
2415 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2416 urec[count].width = wwin->frame->core->width + 2;
2417 urec[count].height = wwin->frame->bottom_width + 1;
2418 count++;
2421 /* shape our frame window */
2422 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2423 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2424 XFlush(dpy);
2425 wfree(urec);
2426 return;
2428 alt_code:
2429 #endif /* OPTIMIZE_SHAPE */
2430 count = 0;
2431 if (HAS_TITLEBAR(wwin)) {
2432 rect[count].x = -1;
2433 rect[count].y = -1;
2434 rect[count].width = wwin->frame->core->width + 2;
2435 rect[count].height = wwin->frame->top_width + 1;
2436 count++;
2438 if (HAS_RESIZEBAR(wwin)) {
2439 rect[count].x = -1;
2440 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2441 rect[count].width = wwin->frame->core->width + 2;
2442 rect[count].height = wwin->frame->bottom_width + 1;
2443 count++;
2445 if (count > 0) {
2446 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2447 0, 0, rect, count, ShapeSet, Unsorted);
2449 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2450 0, wwin->frame->top_width, wwin->client_win,
2451 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2452 XFlush(dpy);
2454 #endif /* SHAPE */
2456 /* ====================================================================== */
2458 static FocusMode getFocusMode(WWindow * wwin)
2460 FocusMode mode;
2462 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2463 if (wwin->wm_hints->input == True) {
2464 if (wwin->protocols.TAKE_FOCUS)
2465 mode = WFM_LOCALLY_ACTIVE;
2466 else
2467 mode = WFM_PASSIVE;
2468 } else {
2469 if (wwin->protocols.TAKE_FOCUS)
2470 mode = WFM_GLOBALLY_ACTIVE;
2471 else
2472 mode = WFM_NO_INPUT;
2474 } else {
2475 mode = WFM_PASSIVE;
2477 return mode;
2480 void wWindowSetKeyGrabs(WWindow * wwin)
2482 int i;
2483 WShortKey *key;
2485 for (i = 0; i < WKBD_LAST; i++) {
2486 key = &wKeyBindings[i];
2488 if (key->keycode == 0)
2489 continue;
2490 if (key->modifier != AnyModifier) {
2491 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2492 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2493 #ifdef NUMLOCK_HACK
2494 /* Also grab all modifier combinations possible that include,
2495 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2496 * work even if the NumLock/ScrollLock key is on.
2498 wHackedGrabKey(key->keycode, key->modifier,
2499 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2500 #endif
2502 XGrabKey(dpy, key->keycode, key->modifier,
2503 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2506 wRootMenuBindShortcuts(wwin->frame->core->window);
2509 void wWindowResetMouseGrabs(WWindow * wwin)
2511 /* Mouse grabs can't be done on the client window because of
2512 * ICCCM and because clients that try to do the same will crash.
2514 * But there is a problem wich makes tbar buttons of unfocused
2515 * windows not usable as the click goes to the frame window instead
2516 * of the button itself. Must figure a way to fix that.
2519 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2521 if (!WFLAGP(wwin, no_bind_mouse)) {
2522 /* grabs for Meta+drag */
2523 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2524 True, ButtonPressMask | ButtonReleaseMask,
2525 GrabModeSync, GrabModeAsync, None, None);
2527 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2528 * but we only grab it for Button4 and Button 5 since a lot of apps
2529 * use CTRL+Button1-3 for app related functionality
2531 if (wPreferences.resize_increment > 0) {
2532 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2533 True, ButtonPressMask | ButtonReleaseMask,
2534 GrabModeSync, GrabModeAsync, None, None);
2535 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2536 True, ButtonPressMask | ButtonReleaseMask,
2537 GrabModeSync, GrabModeAsync, None, None);
2539 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2540 True, ButtonPressMask | ButtonReleaseMask,
2541 GrabModeSync, GrabModeAsync, None, None);
2542 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2543 True, ButtonPressMask | ButtonReleaseMask,
2544 GrabModeSync, GrabModeAsync, None, None);
2548 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2549 && !wwin->flags.is_gnustep) {
2550 /* the passive grabs to focus the window */
2551 /* if (wPreferences.focus_mode == WKF_CLICK) */
2552 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2553 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2555 XFlush(dpy);
2558 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2560 if (attr->flags & GSExtraFlagsAttr) {
2561 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2562 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2563 wWindowUpdateButtonImages(wwin);
2568 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2570 WWindowState *wstate;
2572 wstate = malloc(sizeof(WWindowState));
2573 if (!wstate)
2574 return NULL;
2576 memset(wstate, 0, sizeof(WWindowState));
2577 wstate->pid = pid;
2578 if (instance)
2579 wstate->instance = wstrdup(instance);
2580 if (class)
2581 wstate->class = wstrdup(class);
2582 if (command)
2583 wstate->command = wstrdup(command);
2584 wstate->state = state;
2586 wstate->next = windowState;
2587 windowState = wstate;
2589 return wstate;
2592 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2594 WMagicNumber wWindowGetSavedState(Window win)
2596 char *instance, *class, *command = NULL;
2597 WWindowState *wstate = windowState;
2599 if (!wstate)
2600 return NULL;
2602 command = GetCommandForWindow(win);
2603 if (!command)
2604 return NULL;
2606 if (PropGetWMClass(win, &class, &instance)) {
2607 while (wstate) {
2608 if (SAME(instance, wstate->instance) &&
2609 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2610 break;
2612 wstate = wstate->next;
2614 } else {
2615 wstate = NULL;
2618 if (command)
2619 wfree(command);
2620 if (instance)
2621 free(instance);
2622 if (class)
2623 free(class);
2625 return wstate;
2628 void wWindowDeleteSavedState(WMagicNumber id)
2630 WWindowState *tmp, *wstate = (WWindowState *) id;
2632 if (!wstate || !windowState)
2633 return;
2635 tmp = windowState;
2636 if (tmp == wstate) {
2637 windowState = wstate->next;
2638 release_wwindowstate(wstate);
2639 } else {
2640 while (tmp->next) {
2641 if (tmp->next == wstate) {
2642 tmp->next = wstate->next;
2643 release_wwindowstate(wstate);
2644 break;
2646 tmp = tmp->next;
2651 void wWindowDeleteSavedStatesForPID(pid_t pid)
2653 WWindowState *tmp, *wstate;
2655 if (!windowState)
2656 return;
2658 tmp = windowState;
2659 if (tmp->pid == pid) {
2660 wstate = windowState;
2661 windowState = tmp->next;
2663 release_wwindowstate(wstate);
2664 } else {
2665 while (tmp->next) {
2666 if (tmp->next->pid == pid) {
2667 wstate = tmp->next;
2668 tmp->next = wstate->next;
2669 release_wwindowstate(wstate);
2670 break;
2672 tmp = tmp->next;
2677 static void release_wwindowstate(WWindowState *wstate)
2679 if (wstate->instance)
2680 wfree(wstate->instance);
2682 if (wstate->class)
2683 wfree(wstate->class);
2685 if (wstate->command)
2686 wfree(wstate->command);
2688 wfree(wstate->state);
2689 wfree(wstate);
2692 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2694 if (wwin->flags.omnipresent == flag)
2695 return;
2697 wwin->flags.omnipresent = flag;
2698 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2701 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2703 WWindow *wwin = data;
2705 #ifndef NUMLOCK_HACK
2706 if ((event->xbutton.state & ValidModMask)
2707 != (event->xbutton.state & ~LockMask)) {
2708 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2709 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2711 #endif
2713 event->xbutton.state &= ValidModMask;
2715 CloseWindowMenu(wwin->screen_ptr);
2717 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2718 && !WFLAGP(wwin, no_focusable)) {
2719 wSetFocusTo(wwin->screen_ptr, wwin);
2722 if (event->xbutton.button == Button1)
2723 wRaiseFrame(wwin->frame->core);
2725 if (event->xbutton.window != wwin->frame->resizebar->window) {
2726 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2727 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2728 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2729 return;
2733 if (event->xbutton.state & MOD_MASK) {
2734 /* move the window */
2735 wMouseMoveWindow(wwin, event);
2736 XUngrabPointer(dpy, CurrentTime);
2737 } else {
2738 wMouseResizeWindow(wwin, event);
2739 XUngrabPointer(dpy, CurrentTime);
2743 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2745 WWindow *wwin = data;
2747 event->xbutton.state &= ValidModMask;
2749 if (event->xbutton.button == Button1) {
2750 if (event->xbutton.state == 0) {
2751 if (!WFLAGP(wwin, no_shadeable)) {
2752 /* shade window */
2753 if (wwin->flags.shaded)
2754 wUnshadeWindow(wwin);
2755 else
2756 wShadeWindow(wwin);
2758 } else {
2759 int dir = 0;
2761 if (event->xbutton.state & ControlMask)
2762 dir |= MAX_VERTICAL;
2764 if (event->xbutton.state & ShiftMask) {
2765 dir |= MAX_HORIZONTAL;
2766 if (!(event->xbutton.state & ControlMask))
2767 wSelectWindow(wwin, !wwin->flags.selected);
2770 /* maximize window */
2771 if (dir != 0 && IS_RESIZABLE(wwin)) {
2772 int ndir = dir ^ wwin->flags.maximized;
2774 if (ndir != 0)
2775 wMaximizeWindow(wwin, ndir);
2776 else
2777 wUnmaximizeWindow(wwin);
2780 } else if (event->xbutton.button == Button3) {
2781 if (event->xbutton.state & MOD_MASK)
2782 wHideOtherApplications(wwin);
2783 } else if (event->xbutton.button == Button2) {
2784 wSelectWindow(wwin, !wwin->flags.selected);
2785 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2786 wShadeWindow(wwin);
2787 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2788 wUnshadeWindow(wwin);
2792 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2794 WWindow *wwin = desc->parent;
2795 unsigned int new_width, w_scale;
2796 unsigned int new_height, h_scale;
2797 unsigned int resize_width_increment = 0;
2798 unsigned int resize_height_increment = 0;
2800 if (wwin->normal_hints) {
2801 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2802 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2803 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2804 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2806 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2807 resize_width_increment = wPreferences.resize_increment;
2808 resize_height_increment = wPreferences.resize_increment;
2811 event->xbutton.state &= ValidModMask;
2813 CloseWindowMenu(wwin->screen_ptr);
2815 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2816 wSetFocusTo(wwin->screen_ptr, wwin);
2818 if (event->xbutton.button == Button1)
2819 wRaiseFrame(wwin->frame->core);
2821 if (event->xbutton.state & ControlMask) {
2822 if (event->xbutton.button == Button4) {
2823 new_width = wwin->client.width - resize_width_increment;
2824 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2825 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2827 if (event->xbutton.button == Button5) {
2828 new_width = wwin->client.width + resize_width_increment;
2829 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2830 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2834 if (event->xbutton.state & MOD_MASK) {
2835 /* move the window */
2836 if (XGrabPointer(dpy, wwin->client_win, False,
2837 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2838 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2839 return;
2841 if (event->xbutton.button == Button3) {
2842 wMouseResizeWindow(wwin, event);
2843 } else if (event->xbutton.button == Button4) {
2844 new_height = wwin->client.height - resize_height_increment;
2845 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2846 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2847 } else if (event->xbutton.button == Button5) {
2848 new_height = wwin->client.height + resize_height_increment;
2849 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2850 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2851 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2852 wMouseMoveWindow(wwin, event);
2854 XUngrabPointer(dpy, CurrentTime);
2858 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2860 WWindow *wwin = (WWindow *) data;
2862 #ifndef NUMLOCK_HACK
2863 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2864 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2865 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2866 #endif
2867 event->xbutton.state &= ValidModMask;
2869 CloseWindowMenu(wwin->screen_ptr);
2871 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2872 && !WFLAGP(wwin, no_focusable))
2873 wSetFocusTo(wwin->screen_ptr, wwin);
2875 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2877 if (event->xbutton.button == Button1) {
2878 if (event->xbutton.state & MOD_MASK)
2879 wLowerFrame(wwin->frame->core);
2880 else
2881 wRaiseFrame(wwin->frame->core);
2883 if ((event->xbutton.state & ShiftMask)
2884 && !(event->xbutton.state & ControlMask)) {
2885 wSelectWindow(wwin, !wwin->flags.selected);
2886 return;
2888 if (event->xbutton.window != wwin->frame->titlebar->window
2889 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2890 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2891 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2892 return;
2895 /* move the window */
2896 wMouseMoveWindow(wwin, event);
2898 XUngrabPointer(dpy, CurrentTime);
2899 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2900 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2901 WObjDescriptor *desc;
2903 if (event->xbutton.window != wwin->frame->titlebar->window
2904 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2905 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2906 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2907 return;
2910 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2912 /* allow drag select */
2913 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2914 event->xany.send_event = True;
2915 (*desc->handle_mousedown) (desc, event);
2917 XUngrabPointer(dpy, CurrentTime);
2921 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2923 WWindow *wwin = data;
2925 event->xbutton.state &= ValidModMask;
2927 CloseWindowMenu(wwin->screen_ptr);
2929 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2930 return;
2932 /* if control-click, kill the client */
2933 if (event->xbutton.state & ControlMask) {
2934 wClientKill(wwin);
2935 } else {
2936 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2937 /* send delete message */
2938 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2943 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2945 WWindow *wwin = data;
2947 CloseWindowMenu(wwin->screen_ptr);
2949 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2950 return;
2952 /* send delete message */
2953 if (wwin->protocols.DELETE_WINDOW)
2954 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2955 else
2956 wClientKill(wwin);
2959 #ifdef XKB_BUTTON_HINT
2960 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2962 WWindow *wwin = data;
2963 WFrameWindow *fwin = wwin->frame;
2964 WScreen *scr = fwin->screen_ptr;
2965 int tl;
2967 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2968 return;
2969 tl = wwin->frame->languagemode;
2970 wwin->frame->languagemode = wwin->frame->last_languagemode;
2971 wwin->frame->last_languagemode = tl;
2972 wSetFocusTo(scr, wwin);
2973 wwin->frame->languagebutton_image =
2974 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2975 wFrameWindowUpdateLanguageButton(wwin->frame);
2976 if (event->xbutton.button == Button3)
2977 return;
2978 wRaiseFrame(fwin->core);
2980 #endif
2982 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2984 WWindow *wwin = data;
2986 event->xbutton.state &= ValidModMask;
2988 CloseWindowMenu(wwin->screen_ptr);
2990 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2991 return;
2993 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2994 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2995 } else {
2996 WApplication *wapp;
2997 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2999 wapp = wApplicationOf(wwin->main_window);
3000 if (wapp && !WFLAGP(wwin, no_appicon))
3001 wHideApplication(wapp);
3002 } else if (event->xbutton.state == 0) {
3003 wIconifyWindow(wwin);