New helper function remove_wwindowstate()
[wmaker-crm.git] / src / window.c
blob3d12c50b89fc83f2b860a06edf2632454798f5c3
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 remove_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 WWindow *wWindowFor(Window window)
146 WObjDescriptor *desc;
148 if (window == None)
149 return NULL;
151 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
152 return NULL;
154 if (desc->parent_type == WCLASS_WINDOW)
155 return desc->parent;
156 else if (desc->parent_type == WCLASS_FRAME) {
157 WFrameWindow *frame = (WFrameWindow *) desc->parent;
158 if (frame->flags.is_client_window_frame)
159 return frame->child;
162 return NULL;
165 WWindow *wWindowCreate(void)
167 WWindow *wwin;
169 wwin = wmalloc(sizeof(WWindow));
170 wretain(wwin);
172 wwin->client_descriptor.handle_mousedown = frameMouseDown;
173 wwin->client_descriptor.parent = wwin;
174 wwin->client_descriptor.self = wwin;
175 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
177 return wwin;
180 void wWindowDestroy(WWindow *wwin)
182 int i;
184 if (wwin->screen_ptr->cmap_window == wwin)
185 wwin->screen_ptr->cmap_window = NULL;
187 WMRemoveNotificationObserver(wwin);
189 wwin->flags.destroyed = 1;
191 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
192 if (!wwin->screen_ptr->shortcutWindows[i])
193 continue;
195 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
197 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
198 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
199 wwin->screen_ptr->shortcutWindows[i] = NULL;
203 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
204 wwin->fake_group->retainCount--;
205 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
206 XDestroyWindow(dpy, wwin->fake_group->leader);
207 wwin->fake_group->leader = None;
208 wwin->fake_group->origLeader = None;
209 XFlush(dpy);
213 if (wwin->normal_hints)
214 XFree(wwin->normal_hints);
216 if (wwin->wm_hints)
217 XFree(wwin->wm_hints);
219 if (wwin->wm_instance)
220 XFree(wwin->wm_instance);
222 if (wwin->wm_class)
223 XFree(wwin->wm_class);
225 if (wwin->wm_gnustep_attr)
226 wfree(wwin->wm_gnustep_attr);
228 if (wwin->cmap_windows)
229 XFree(wwin->cmap_windows);
231 XDeleteContext(dpy, wwin->client_win, wWinContext);
233 if (wwin->frame)
234 wFrameWindowDestroy(wwin->frame);
236 if (wwin->icon) {
237 RemoveFromStackList(wwin->icon->core);
238 wIconDestroy(wwin->icon);
239 if (wPreferences.auto_arrange_icons)
240 wArrangeIcons(wwin->screen_ptr, True);
242 if (wwin->net_icon_image)
243 RReleaseImage(wwin->net_icon_image);
245 wrelease(wwin);
248 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
250 if (gs_hints->flags & GSWindowStyleAttr) {
251 if (gs_hints->window_style == WMBorderlessWindowMask) {
252 wwin->client_flags.no_border = 1;
253 wwin->client_flags.no_titlebar = 1;
254 wwin->client_flags.no_closable = 1;
255 wwin->client_flags.no_miniaturizable = 1;
256 wwin->client_flags.no_resizable = 1;
257 wwin->client_flags.no_close_button = 1;
258 wwin->client_flags.no_miniaturize_button = 1;
259 wwin->client_flags.no_resizebar = 1;
260 } else {
261 wwin->client_flags.no_close_button =
262 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
264 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
266 wwin->client_flags.no_miniaturize_button =
267 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
269 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
271 wwin->client_flags.no_resizebar =
272 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
274 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
276 /* these attributes supposedly imply in the existence
277 * of a titlebar */
278 if (gs_hints->window_style & (WMResizableWindowMask |
279 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
280 wwin->client_flags.no_titlebar = 0;
281 } else {
282 wwin->client_flags.no_titlebar =
283 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
287 } else {
288 /* setup the defaults */
289 wwin->client_flags.no_border = 0;
290 wwin->client_flags.no_titlebar = 0;
291 wwin->client_flags.no_closable = 0;
292 wwin->client_flags.no_miniaturizable = 0;
293 wwin->client_flags.no_resizable = 0;
294 wwin->client_flags.no_close_button = 0;
295 wwin->client_flags.no_miniaturize_button = 0;
296 wwin->client_flags.no_resizebar = 0;
298 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
299 wwin->client_flags.no_appicon = 1;
302 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
304 WScreen *scr = wwin->screen_ptr;
306 /* sets global default stuff */
307 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
309 * Decoration setting is done in this precedence (lower to higher)
310 * - use global default in the resource database
311 * - guess some settings
312 * - use GNUstep/external window attributes
313 * - set hints specified for the app in the resource DB
316 WSETUFLAG(wwin, broken_close, 0);
318 if (wwin->protocols.DELETE_WINDOW)
319 WSETUFLAG(wwin, kill_close, 0);
320 else
321 WSETUFLAG(wwin, kill_close, 1);
323 /* transients can't be iconified or maximized */
324 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
325 WSETUFLAG(wwin, no_miniaturizable, 1);
326 WSETUFLAG(wwin, no_miniaturize_button, 1);
329 /* if the window can't be resized, remove the resizebar */
330 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
331 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
332 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
333 WSETUFLAG(wwin, no_resizable, 1);
334 WSETUFLAG(wwin, no_resizebar, 1);
337 /* set GNUstep window attributes */
338 if (wwin->wm_gnustep_attr) {
339 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
341 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
343 *level = wwin->wm_gnustep_attr->window_level;
345 * INT_MIN is the only illegal window level.
347 if (*level == INT_MIN)
348 *level = INT_MIN + 1;
349 } else {
350 /* setup defaults */
351 *level = WMNormalLevel;
353 } else {
354 int tmp_workspace = -1;
355 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
356 Bool check;
358 check = False;
360 #ifdef MWM_HINTS
361 wMWMCheckClientHints(wwin);
362 #endif /* MWM_HINTS */
364 if (!check)
365 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
367 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
368 * have INT_MIN that means that no window level was requested. -Dan
370 if (tmp_level == INT_MIN) {
371 if (WFLAGP(wwin, floating))
372 *level = WMFloatingLevel;
373 else if (WFLAGP(wwin, sunken))
374 *level = WMSunkenLevel;
375 else
376 *level = WMNormalLevel;
377 } else {
378 *level = tmp_level;
381 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
382 WWindow *transientOwner = wWindowFor(wwin->transient_for);
383 if (transientOwner) {
384 int ownerLevel = transientOwner->frame->core->stacking->window_level;
385 if (ownerLevel > *level)
386 *level = ownerLevel;
390 if (tmp_workspace >= 0)
391 *workspace = tmp_workspace % scr->workspace_count;
395 * Set attributes specified only for that window/class.
396 * This might do duplicate work with the 1st wDefaultFillAttributes().
398 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
399 &wwin->defined_user_flags, False);
401 * Sanity checks for attributes that depend on other attributes
403 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
404 wwin->user_flags.emulate_appicon = 0;
406 if (wwin->main_window != None) {
407 WApplication *wapp = wApplicationOf(wwin->main_window);
408 if (wapp && !wapp->flags.emulated)
409 wwin->user_flags.emulate_appicon = 0;
412 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
413 wwin->user_flags.emulate_appicon = 0;
415 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
416 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
417 wwin->user_flags.sunken = 0;
419 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
421 /* windows that have takefocus=False shouldn't take focus at all */
422 if (wwin->focus_mode == WFM_NO_INPUT)
423 wwin->client_flags.no_focusable = 1;
426 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
428 int w1, h1, w2, h2;
430 w1 = wwin->frame->core->width;
431 h1 = wwin->frame->core->height;
432 w2 = obscured->frame->core->width;
433 h2 = obscured->frame->core->height;
435 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
436 && wwin->frame->workspace != obscured->frame->workspace)
437 return False;
439 if (wwin->frame_x + w1 < obscured->frame_x
440 || wwin->frame_y + h1 < obscured->frame_y
441 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
442 return False;
444 return True;
447 static void fixLeaderProperties(WWindow *wwin)
449 XClassHint *classHint;
450 XWMHints *hints, *clientHints;
451 XWindowAttributes attr;
452 Window leaders[2], window;
453 char **argv, *command;
454 int argc, i, pid;
455 Bool haveCommand;
457 classHint = XAllocClassHint();
458 clientHints = XGetWMHints(dpy, wwin->client_win);
459 pid = wNETWMGetPidForWindow(wwin->client_win);
460 if (pid > 0)
461 haveCommand = GetCommandForPid(pid, &argv, &argc);
462 else
463 haveCommand = False;
465 leaders[0] = wwin->client_leader;
466 leaders[1] = wwin->group_id;
468 if (haveCommand) {
469 command = GetCommandForWindow(wwin->client_win);
470 if (command)
471 wfree(command); /* command already set. nothing to do. */
472 else
473 XSetCommand(dpy, wwin->client_win, argv, argc);
476 for (i = 0; i < 2; i++) {
477 window = leaders[i];
478 if (window) {
479 if (XGetClassHint(dpy, window, classHint) == 0) {
480 classHint->res_name = wwin->wm_instance;
481 classHint->res_class = wwin->wm_class;
482 XSetClassHint(dpy, window, classHint);
484 hints = XGetWMHints(dpy, window);
485 if (hints) {
486 XFree(hints);
487 } else if (clientHints) {
488 /* set window group leader to self */
489 clientHints->window_group = window;
490 clientHints->flags |= WindowGroupHint;
491 XSetWMHints(dpy, window, clientHints);
494 if (haveCommand) {
495 command = GetCommandForWindow(window);
496 if (command)
497 wfree(command); /* command already set. nothing to do. */
498 else
499 XSetCommand(dpy, window, argv, argc);
502 /* Make sure we get notification when this window is destroyed */
503 if (XGetWindowAttributes(dpy, window, &attr))
504 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
508 XFree(classHint);
509 if (clientHints)
510 XFree(clientHints);
511 if (haveCommand)
512 wfree(argv);
515 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
517 XClassHint *classHint;
518 XWMHints *hints;
519 Window leader;
520 int argc;
521 char **argv;
523 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
524 /* set class hint */
525 classHint = XAllocClassHint();
526 classHint->res_name = instance;
527 classHint->res_class = class;
528 XSetClassHint(dpy, leader, classHint);
529 XFree(classHint);
531 /* inherit these from the original leader if available */
532 hints = XGetWMHints(dpy, win);
533 if (!hints) {
534 hints = XAllocWMHints();
535 hints->flags = 0;
537 /* set window group leader to self */
538 hints->window_group = leader;
539 hints->flags |= WindowGroupHint;
540 XSetWMHints(dpy, leader, hints);
541 XFree(hints);
543 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
544 XSetCommand(dpy, leader, argv, argc);
545 XFreeStringList(argv);
548 return leader;
551 static int matchIdentifier(const void *item, const void *cdata)
553 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
557 *----------------------------------------------------------------
558 * wManageWindow--
559 * reparents the window and allocates a descriptor for it.
560 * Window manager hints and other hints are fetched to configure
561 * the window decoration attributes and others. User preferences
562 * for the window are used if available, to configure window
563 * decorations and some behaviour.
564 * If in startup, windows that are override redirect,
565 * unmapped and never were managed and are Withdrawn are not
566 * managed.
568 * Returns:
569 * the new window descriptor
571 * Side effects:
572 * The window is reparented and appropriate notification
573 * is done to the client. Input mask for the window is setup.
574 * The window descriptor is also associated with various window
575 * contexts and inserted in the head of the window list.
576 * Event handler contexts are associated for some objects
577 * (buttons, titlebar and resizebar)
579 *----------------------------------------------------------------
581 WWindow *wManageWindow(WScreen *scr, Window window)
583 WWindow *wwin;
584 int x, y;
585 unsigned width, height;
586 XWindowAttributes wattribs;
587 XSetWindowAttributes attribs;
588 WWindowState *win_state;
589 WWindow *transientOwner = NULL;
590 int window_level;
591 int wm_state;
592 int foo;
593 int workspace = -1;
594 char *title;
595 Bool withdraw = False;
596 Bool raise = False;
598 /* mutex. */
599 XGrabServer(dpy);
600 XSync(dpy, False);
601 /* make sure the window is still there */
602 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
603 XUngrabServer(dpy);
604 return NULL;
607 /* if it's an override-redirect, ignore it */
608 if (wattribs.override_redirect) {
609 XUngrabServer(dpy);
610 return NULL;
613 wm_state = PropGetWindowState(window);
615 /* if it's startup and the window is unmapped, don't manage it */
616 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
617 XUngrabServer(dpy);
618 return NULL;
621 wwin = wWindowCreate();
623 title = wNETWMGetWindowName(window);
624 if (title)
625 wwin->flags.net_has_title = 1;
626 if (!title && !wFetchName(dpy, window, &title))
627 title = NULL;
629 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
631 #ifdef SHAPE
632 if (wShapeSupported) {
633 int junk;
634 unsigned int ujunk;
635 int b_shaped;
637 XShapeSelectInput(dpy, window, ShapeNotifyMask);
638 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
639 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
640 wwin->flags.shaped = b_shaped;
642 #endif
645 * Get hints and other information in properties
647 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
649 /* setup descriptor */
650 wwin->client_win = window;
651 wwin->screen_ptr = scr;
652 wwin->old_border_width = wattribs.border_width;
653 wwin->event_mask = CLIENT_EVENTS;
654 attribs.event_mask = CLIENT_EVENTS;
655 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
656 attribs.save_under = False;
658 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
659 XSetWindowBorderWidth(dpy, window, 0);
661 /* get hints from GNUstep app */
662 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
663 wwin->flags.is_gnustep = 1;
665 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
666 wwin->wm_gnustep_attr = NULL;
668 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
669 wwin->flags.is_dockapp = 1;
670 withdraw = True;
673 wwin->client_leader = PropGetClientLeader(window);
674 if (wwin->client_leader != None)
675 wwin->main_window = wwin->client_leader;
677 wwin->wm_hints = XGetWMHints(dpy, window);
679 if (wwin->wm_hints) {
680 if (wwin->wm_hints->flags & StateHint) {
682 if (wwin->wm_hints->initial_state == IconicState) {
683 wwin->flags.miniaturized = 1;
684 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
685 wwin->flags.is_dockapp = 1;
686 withdraw = True;
690 if (wwin->wm_hints->flags & WindowGroupHint) {
691 wwin->group_id = wwin->wm_hints->window_group;
692 /* window_group has priority over CLIENT_LEADER */
693 wwin->main_window = wwin->group_id;
694 } else {
695 wwin->group_id = None;
698 if (wwin->wm_hints->flags & UrgencyHint) {
699 wwin->flags.urgent = 1;
700 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
702 } else {
703 wwin->group_id = None;
706 PropGetProtocols(window, &wwin->protocols);
708 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
709 wwin->transient_for = None;
710 } else {
711 if (wwin->transient_for == None || wwin->transient_for == window) {
712 wwin->transient_for = scr->root_win;
713 } else {
714 transientOwner = wWindowFor(wwin->transient_for);
715 if (transientOwner && transientOwner->main_window != None)
716 wwin->main_window = transientOwner->main_window;
720 /* guess the focus mode */
721 wwin->focus_mode = getFocusMode(wwin);
723 /* get geometry stuff */
724 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
726 /* get colormap windows */
727 GetColormapWindows(wwin);
730 * Setup the decoration/window attributes and
731 * geometry
733 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
735 /* Make broken apps behave as a nice app. */
736 if (WFLAGP(wwin, emulate_appicon))
737 wwin->main_window = wwin->client_win;
739 fixLeaderProperties(wwin);
741 wwin->orig_main_window = wwin->main_window;
743 if (wwin->flags.is_gnustep)
744 WSETUFLAG(wwin, shared_appicon, 0);
746 if (wwin->main_window) {
747 extern Atom _XA_WINDOWMAKER_MENU;
748 XTextProperty text_prop;
750 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
751 WSETUFLAG(wwin, shared_appicon, 0);
754 if (wwin->flags.is_dockapp)
755 WSETUFLAG(wwin, shared_appicon, 0);
757 if (wwin->main_window) {
758 WApplication *app = wApplicationOf(wwin->main_window);
759 if (app && app->app_icon)
760 WSETUFLAG(wwin, shared_appicon, 0);
763 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
764 char *buffer, *instance, *class;
765 WFakeGroupLeader *fPtr;
766 int index;
768 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
770 /* // only enter here if PropGetWMClass() succeds */
771 PropGetWMClass(wwin->main_window, &class, &instance);
772 buffer = StrConcatDot(instance, class);
774 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
775 if (index != WANotFound) {
776 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
777 if (fPtr->retainCount == 0)
778 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
779 instance, class);
781 fPtr->retainCount++;
782 if (fPtr->origLeader == None) {
783 if (ADEQUATE(wwin->main_window)) {
784 fPtr->retainCount++;
785 fPtr->origLeader = wwin->main_window;
788 wwin->fake_group = fPtr;
789 /*wwin->group_id = fPtr->leader; */
790 wwin->main_window = fPtr->leader;
791 wfree(buffer);
792 } else {
793 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
795 fPtr->identifier = buffer;
796 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
797 fPtr->origLeader = None;
798 fPtr->retainCount = 1;
800 WMAddToArray(scr->fakeGroupLeaders, fPtr);
802 if (ADEQUATE(wwin->main_window)) {
803 fPtr->retainCount++;
804 fPtr->origLeader = wwin->main_window;
806 wwin->fake_group = fPtr;
807 /*wwin->group_id = fPtr->leader; */
808 wwin->main_window = fPtr->leader;
810 if (instance)
811 free(instance);
812 if (class)
813 free(class);
814 #undef ADEQUATE
818 * Setup the initial state of the window
820 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
821 wwin->flags.miniaturized = 1;
823 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
824 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
826 wNETWMCheckInitialClientState(wwin);
828 /* apply previous state if it exists and we're in startup */
829 if (scr->flags.startup && wm_state >= 0) {
830 if (wm_state == IconicState) {
831 wwin->flags.miniaturized = 1;
832 } else if (wm_state == WithdrawnState) {
833 withdraw = True;
837 /* if there is a saved state (from file), restore it */
838 win_state = NULL;
839 if (wwin->main_window != None)
840 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
841 else
842 win_state = (WWindowState *) wWindowGetSavedState(window);
844 if (win_state && !withdraw) {
845 if (win_state->state->hidden > 0)
846 wwin->flags.hidden = win_state->state->hidden;
848 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
849 wwin->flags.shaded = win_state->state->shaded;
851 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
852 wwin->flags.miniaturized = win_state->state->miniaturized;
854 if (!IS_OMNIPRESENT(wwin)) {
855 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
856 wwin->wm_class);
857 if (w < 0 || w >= scr->workspace_count) {
858 workspace = win_state->state->workspace;
859 if (workspace >= scr->workspace_count)
860 workspace = scr->current_workspace;
861 } else {
862 workspace = w;
864 } else {
865 workspace = scr->current_workspace;
869 /* if we're restarting, restore saved state (from hints).
870 * This will overwrite previous */
872 WSavedState *wstate;
874 if (getSavedState(window, &wstate)) {
875 wwin->flags.shaded = wstate->shaded;
876 wwin->flags.hidden = wstate->hidden;
877 wwin->flags.miniaturized = wstate->miniaturized;
878 wwin->flags.maximized = wstate->maximized;
879 if (wwin->flags.maximized) {
880 wwin->old_geometry.x = wstate->x;
881 wwin->old_geometry.y = wstate->y;
882 wwin->old_geometry.width = wstate->w;
883 wwin->old_geometry.height = wstate->h;
886 workspace = wstate->workspace;
887 } else {
888 wstate = NULL;
891 /* restore window shortcut */
892 if (wstate != NULL || win_state != NULL) {
893 unsigned mask = 0;
895 if (win_state != NULL)
896 mask = win_state->state->window_shortcuts;
898 if (wstate != NULL && mask == 0)
899 mask = wstate->window_shortcuts;
901 if (mask > 0) {
902 int i;
904 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
905 if (mask & (1 << i)) {
906 if (!scr->shortcutWindows[i])
907 scr->shortcutWindows[i] = WMCreateArray(4);
909 WMAddToArray(scr->shortcutWindows[i], wwin);
914 if (wstate != NULL)
915 wfree(wstate);
918 /* don't let transients start miniaturized if their owners are not */
919 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
920 wwin->flags.miniaturized = 0;
921 if (wwin->wm_hints)
922 wwin->wm_hints->initial_state = NormalState;
925 /* set workspace on which the window starts */
926 if (workspace >= 0) {
927 if (workspace > scr->workspace_count - 1)
928 workspace = workspace % scr->workspace_count;
929 } else {
930 int w;
932 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
934 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
935 workspace = w;
936 } else {
937 if (wPreferences.open_transients_with_parent && transientOwner)
938 workspace = transientOwner->frame->workspace;
939 else
940 workspace = scr->current_workspace;
944 /* setup window geometry */
945 if (win_state && win_state->state->w > 0) {
946 width = win_state->state->w;
947 height = win_state->state->h;
949 wWindowConstrainSize(wwin, &width, &height);
951 /* do not ask for window placement if the window is
952 * transient, during startup, if the initial workspace is another one
953 * or if the window wants to start iconic.
954 * If geometry was saved, restore it. */
956 Bool dontBring = False;
958 if (win_state && win_state->state->w > 0) {
959 x = win_state->state->x;
960 y = win_state->state->y;
961 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
962 && !scr->flags.startup
963 && workspace == scr->current_workspace
964 && !wwin->flags.miniaturized
965 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
967 if (transientOwner && transientOwner->flags.mapped) {
968 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
969 WMRect rect;
970 int head;
972 x = transientOwner->frame_x +
973 abs((transientOwner->frame->core->width - width) / 2) + offs;
974 y = transientOwner->frame_y +
975 abs((transientOwner->frame->core->height - height) / 3) + offs;
978 * limit transient windows to be inside their parent's head
980 rect.pos.x = transientOwner->frame_x;
981 rect.pos.y = transientOwner->frame_y;
982 rect.size.width = transientOwner->frame->core->width;
983 rect.size.height = transientOwner->frame->core->height;
985 head = wGetHeadForRect(scr, rect);
986 rect = wGetRectForHead(scr, head);
988 if (x < rect.pos.x)
989 x = rect.pos.x;
990 else if (x + width > rect.pos.x + rect.size.width)
991 x = rect.pos.x + rect.size.width - width;
993 if (y < rect.pos.y)
994 y = rect.pos.y;
995 else if (y + height > rect.pos.y + rect.size.height)
996 y = rect.pos.y + rect.size.height - height;
998 } else {
999 PlaceWindow(wwin, &x, &y, width, height);
1001 if (wPreferences.window_placement == WPM_MANUAL) {
1002 dontBring = True;
1004 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1005 int head, flags;
1006 WMRect rect;
1007 int reposition = 0;
1010 * Make spash screens come out in the center of a head
1011 * trouble is that most splashies never get here
1012 * they are managed trough atoms but god knows where.
1013 * Dan, do you know ? -peter
1015 * Most of them are not managed, they have set
1016 * OverrideRedirect, which means we can't do anything about
1017 * them. -alfredo
1021 * xinerama checks for: across head and dead space
1023 rect.pos.x = x;
1024 rect.pos.y = y;
1025 rect.size.width = width;
1026 rect.size.height = height;
1028 head = wGetRectPlacementInfo(scr, rect, &flags);
1030 if (flags & XFLAG_DEAD)
1031 reposition = 1;
1033 if (flags & XFLAG_MULTIPLE)
1034 reposition = 2;
1037 switch (reposition) {
1038 case 1:
1039 head = wGetHeadForPointerLocation(scr);
1040 rect = wGetRectForHead(scr, head);
1042 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1043 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1044 break;
1046 case 2:
1047 rect = wGetRectForHead(scr, head);
1049 if (x < rect.pos.x)
1050 x = rect.pos.x;
1051 else if (x + width > rect.pos.x + rect.size.width)
1052 x = rect.pos.x + rect.size.width - width;
1054 if (y < rect.pos.y)
1055 y = rect.pos.y;
1056 else if (y + height > rect.pos.y + rect.size.height)
1057 y = rect.pos.y + rect.size.height - height;
1059 break;
1061 default:
1062 break;
1066 if (WFLAGP(wwin, dont_move_off) && dontBring)
1067 wScreenBringInside(scr, &x, &y, width, height);
1070 wNETWMPositionSplash(wwin, &x, &y, width, height);
1072 if (wwin->flags.urgent) {
1073 if (!IS_OMNIPRESENT(wwin))
1074 wwin->flags.omnipresent ^= 1;
1078 * Create frame, borders and do reparenting
1080 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1081 #ifdef XKB_BUTTON_HINT
1082 if (wPreferences.modelock)
1083 foo |= WFF_LANGUAGE_BUTTON;
1084 #endif
1085 if (HAS_TITLEBAR(wwin))
1086 foo |= WFF_TITLEBAR;
1087 if (HAS_RESIZEBAR(wwin))
1088 foo |= WFF_RESIZEBAR;
1089 if (HAS_BORDER(wwin))
1090 foo |= WFF_BORDER;
1092 wwin->frame = wFrameWindowCreate(scr, window_level,
1093 x, y, width, height,
1094 &wPreferences.window_title_clearance,
1095 &wPreferences.window_title_min_height,
1096 &wPreferences.window_title_max_height,
1097 foo,
1098 scr->window_title_texture,
1099 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1100 wattribs.depth, wattribs.visual, wattribs.colormap);
1102 wwin->frame->flags.is_client_window_frame = 1;
1103 wwin->frame->flags.justification = wPreferences.title_justification;
1105 /* setup button images */
1106 wWindowUpdateButtonImages(wwin);
1108 /* hide unused buttons */
1109 foo = 0;
1110 if (WFLAGP(wwin, no_close_button))
1111 foo |= WFF_RIGHT_BUTTON;
1112 if (WFLAGP(wwin, no_miniaturize_button))
1113 foo |= WFF_LEFT_BUTTON;
1114 #ifdef XKB_BUTTON_HINT
1115 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1116 foo |= WFF_LANGUAGE_BUTTON;
1117 #endif
1118 if (foo != 0)
1119 wFrameWindowHideButton(wwin->frame, foo);
1121 wwin->frame->child = wwin;
1122 wwin->frame->workspace = workspace;
1123 wwin->frame->on_click_left = windowIconifyClick;
1125 #ifdef XKB_BUTTON_HINT
1126 if (wPreferences.modelock)
1127 wwin->frame->on_click_language = windowLanguageClick;
1128 #endif
1130 wwin->frame->on_click_right = windowCloseClick;
1131 wwin->frame->on_dblclick_right = windowCloseDblClick;
1132 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1133 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1134 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1136 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1137 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1138 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1141 int gx, gy;
1143 wClientGetGravityOffsets(wwin, &gx, &gy);
1145 /* if gravity is to the south, account for the border sizes */
1146 if (gy > 0)
1147 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1151 * wWindowConfigure() will init the client window's size
1152 * (wwin->client.{width,height}) and all other geometry
1153 * related variables (frame_x,frame_y)
1155 wWindowConfigure(wwin, x, y, width, height);
1157 /* to make sure the window receives it's new position after reparenting */
1158 wWindowSynthConfigureNotify(wwin);
1161 * Setup descriptors and save window to internal
1162 * lists
1164 if (wwin->main_window != None) {
1165 WApplication *app;
1166 WWindow *leader;
1168 /* Leader windows do not necessary set themselves as leaders.
1169 * If this is the case, point the leader of this window to
1170 * itself */
1171 leader = wWindowFor(wwin->main_window);
1172 if (leader && leader->main_window == None)
1173 leader->main_window = leader->client_win;
1175 app = wApplicationCreate(wwin);
1176 if (app) {
1177 app->last_workspace = workspace;
1180 * Do application specific stuff, like setting application
1181 * wide attributes.
1184 if (wwin->flags.hidden) {
1185 /* if the window was set to hidden because it was hidden
1186 * in a previous incarnation and that state was restored */
1187 app->flags.hidden = 1;
1188 } else if (app->flags.hidden) {
1189 if (WFLAGP(app->main_window_desc, start_hidden)) {
1190 wwin->flags.hidden = 1;
1191 } else {
1192 wUnhideApplication(app, False, False);
1193 raise = True;
1196 wAppBounce(app);
1200 /* setup the frame descriptor */
1201 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1202 wwin->frame->core->descriptor.parent = wwin;
1203 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1205 /* don't let windows go away if we die */
1206 XAddToSaveSet(dpy, window);
1208 XLowerWindow(dpy, window);
1210 /* if window is in this workspace and should be mapped, then map it */
1211 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1212 && !wwin->flags.hidden && !withdraw) {
1214 /* The following "if" is to avoid crashing of clients that expect
1215 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1216 * after the return from this function.
1218 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1219 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1220 else
1221 wClientSetState(wwin, NormalState, None);
1223 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1224 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1226 * The brain damaged idiotic non-click to focus modes will
1227 * have trouble with this because:
1229 * 1. window is created and mapped by the client
1230 * 2. window is mapped by wmaker in small size
1231 * 3. window is animated to grow to normal size
1232 * 4. this function returns to normal event loop
1233 * 5. eventually, the EnterNotify event that would trigger
1234 * the window focusing (if the mouse is over that window)
1235 * will be processed by wmaker.
1236 * But since this event will be rather delayed
1237 * (step 3 has a large delay) the time when the event ocurred
1238 * and when it is processed, the client that owns that window
1239 * will reject the XSetInputFocus() for it.
1241 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1242 DoWindowBirth(wwin);
1244 wWindowMap(wwin);
1247 /* setup stacking descriptor */
1248 if (transientOwner)
1249 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1250 else
1251 wwin->frame->core->stacking->child_of = NULL;
1253 if (!scr->focused_window) {
1254 /* first window on the list */
1255 wwin->next = NULL;
1256 wwin->prev = NULL;
1257 scr->focused_window = wwin;
1258 } else {
1259 WWindow *tmp;
1261 /* add window at beginning of focus window list */
1262 tmp = scr->focused_window;
1263 while (tmp->prev)
1264 tmp = tmp->prev;
1265 tmp->prev = wwin;
1266 wwin->next = tmp;
1267 wwin->prev = NULL;
1270 /* raise is set to true if we un-hid the app when this window was born.
1271 * we raise, else old windows of this app will be above this new one. */
1272 if (raise)
1273 wRaiseFrame(wwin->frame->core);
1275 /* Update name must come after WApplication stuff is done */
1276 wWindowUpdateName(wwin, title);
1277 if (title)
1278 XFree(title);
1280 XUngrabServer(dpy);
1283 * Final preparations before window is ready to go
1285 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1287 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1288 if (((transientOwner && transientOwner->flags.focused)
1289 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1291 /* only auto_focus if on same screen as mouse
1292 * (and same head for xinerama mode)
1293 * TODO: make it an option */
1295 /*TODO add checking the head of the window, is it available? */
1296 short same_screen = 0, same_head = 1;
1298 int foo;
1299 unsigned int bar;
1300 Window dummy;
1302 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1303 &foo, &foo, &foo, &foo, &bar) != False)
1304 same_screen = 1;
1306 if (same_screen == 1 && same_head == 1)
1307 wSetFocusTo(scr, wwin);
1310 wWindowResetMouseGrabs(wwin);
1312 if (!WFLAGP(wwin, no_bind_keys))
1313 wWindowSetKeyGrabs(wwin);
1315 WMPostNotificationName(WMNManaged, wwin, NULL);
1316 wColormapInstallForWindow(scr, scr->cmap_window);
1318 /* Setup Notification Observers */
1319 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1321 /* Cleanup temporary stuff */
1322 if (win_state)
1323 wWindowDeleteSavedState(win_state);
1325 /* If the window must be withdrawed, then do it now.
1326 * Must do some optimization, 'though */
1327 if (withdraw) {
1328 wwin->flags.mapped = 0;
1329 wClientSetState(wwin, WithdrawnState, None);
1330 wUnmanageWindow(wwin, True, False);
1331 wwin = NULL;
1334 return wwin;
1337 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1338 char *title, int x, int y, int width, int height)
1340 WWindow *wwin;
1341 int foo;
1343 wwin = wWindowCreate();
1345 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1347 wwin->flags.internal_window = 1;
1349 WSETUFLAG(wwin, omnipresent, 1);
1350 WSETUFLAG(wwin, no_shadeable, 1);
1351 WSETUFLAG(wwin, no_resizable, 1);
1352 WSETUFLAG(wwin, no_miniaturizable, 1);
1354 wwin->focus_mode = WFM_PASSIVE;
1355 wwin->client_win = window;
1356 wwin->screen_ptr = scr;
1357 wwin->transient_for = owner;
1358 wwin->client.x = x;
1359 wwin->client.y = y;
1360 wwin->client.width = width;
1361 wwin->client.height = height;
1362 wwin->frame_x = wwin->client.x;
1363 wwin->frame_y = wwin->client.y;
1365 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1366 foo |= WFF_TITLEBAR;
1367 #ifdef XKB_BUTTON_HINT
1368 foo |= WFF_LANGUAGE_BUTTON;
1369 #endif
1371 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1372 wwin->frame_x, wwin->frame_y,
1373 width, height,
1374 &wPreferences.window_title_clearance,
1375 &wPreferences.window_title_min_height,
1376 &wPreferences.window_title_max_height,
1377 foo,
1378 scr->window_title_texture,
1379 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1380 scr->w_depth, scr->w_visual, scr->w_colormap);
1382 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1384 wwin->frame->flags.is_client_window_frame = 1;
1385 wwin->frame->flags.justification = wPreferences.title_justification;
1387 wFrameWindowChangeTitle(wwin->frame, title);
1389 /* setup button images */
1390 wWindowUpdateButtonImages(wwin);
1392 /* hide buttons */
1393 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1395 wwin->frame->child = wwin;
1396 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1398 #ifdef XKB_BUTTON_HINT
1399 if (wPreferences.modelock)
1400 wwin->frame->on_click_language = windowLanguageClick;
1401 #endif
1403 wwin->frame->on_click_right = windowCloseClick;
1404 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1405 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1406 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1407 wwin->client.y += wwin->frame->top_width;
1409 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1410 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1412 /* setup the frame descriptor */
1413 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1414 wwin->frame->core->descriptor.parent = wwin;
1415 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1417 XLowerWindow(dpy, window);
1418 XMapSubwindows(dpy, wwin->frame->core->window);
1420 /* setup stacking descriptor */
1421 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1422 WWindow *tmp;
1423 tmp = wWindowFor(wwin->transient_for);
1424 if (tmp)
1425 wwin->frame->core->stacking->child_of = tmp->frame->core;
1426 } else {
1427 wwin->frame->core->stacking->child_of = NULL;
1430 if (!scr->focused_window) {
1431 /* first window on the list */
1432 wwin->next = NULL;
1433 wwin->prev = NULL;
1434 scr->focused_window = wwin;
1435 } else {
1436 WWindow *tmp;
1438 /* add window at beginning of focus window list */
1439 tmp = scr->focused_window;
1440 while (tmp->prev)
1441 tmp = tmp->prev;
1442 tmp->prev = wwin;
1443 wwin->next = tmp;
1444 wwin->prev = NULL;
1447 if (wwin->flags.is_gnustep == 0)
1448 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1450 /* if (wPreferences.auto_focus) */
1451 wSetFocusTo(scr, wwin);
1452 wWindowResetMouseGrabs(wwin);
1453 wWindowSetKeyGrabs(wwin);
1455 return wwin;
1459 *----------------------------------------------------------------------
1460 * wUnmanageWindow--
1461 * Removes the frame window from a window and destroys all data
1462 * related to it. The window will be reparented back to the root window
1463 * if restore is True.
1465 * Side effects:
1466 * Everything related to the window is destroyed and the window
1467 * is removed from the window lists. Focus is set to the previous on the
1468 * window list.
1469 *----------------------------------------------------------------------
1471 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1473 WCoreWindow *frame = wwin->frame->core;
1474 WWindow *owner = NULL;
1475 WWindow *newFocusedWindow = NULL;
1476 int wasFocused;
1477 WScreen *scr = wwin->screen_ptr;
1479 /* First close attribute editor window if open */
1480 if (wwin->flags.inspector_open)
1481 wCloseInspectorForWindow(wwin);
1483 /* Close window menu if it's open for this window */
1484 if (wwin->flags.menu_open_for_me)
1485 CloseWindowMenu(scr);
1487 if (!destroyed) {
1488 if (!wwin->flags.internal_window)
1489 XRemoveFromSaveSet(dpy, wwin->client_win);
1491 /* If this is a leader window, we still need to listen for
1492 * DestroyNotify and PropertyNotify. */
1493 if (wApplicationOf(wwin->client_win))
1494 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1495 else
1496 XSelectInput(dpy, wwin->client_win, NoEventMask);
1498 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1499 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1502 XUnmapWindow(dpy, frame->window);
1504 XUnmapWindow(dpy, wwin->client_win);
1506 /* deselect window */
1507 wSelectWindow(wwin, False);
1509 /* remove all pending events on window */
1510 /* I think this only matters for autoraise */
1511 if (wPreferences.raise_delay)
1512 WMDeleteTimerWithClientData(wwin->frame->core);
1514 XFlush(dpy);
1516 /* reparent the window back to the root */
1517 if (restore)
1518 wClientRestore(wwin);
1520 if (wwin->transient_for != scr->root_win) {
1521 owner = wWindowFor(wwin->transient_for);
1522 if (owner) {
1523 if (!owner->flags.semi_focused)
1524 owner = NULL;
1525 else
1526 owner->flags.semi_focused = 0;
1530 wasFocused = wwin->flags.focused;
1532 /* remove from window focus list */
1533 if (!wwin->prev && !wwin->next) {
1534 /* was the only window */
1535 scr->focused_window = NULL;
1536 newFocusedWindow = NULL;
1537 } else {
1538 WWindow *tmp;
1540 if (wwin->prev)
1541 wwin->prev->next = wwin->next;
1543 if (wwin->next)
1544 wwin->next->prev = wwin->prev;
1545 else {
1546 scr->focused_window = wwin->prev;
1547 scr->focused_window->next = NULL;
1550 if (wPreferences.focus_mode == WKF_CLICK) {
1552 /* if in click to focus mode and the window
1553 * was a transient, focus the owner window
1555 tmp = NULL;
1556 if (wPreferences.focus_mode == WKF_CLICK) {
1557 tmp = wWindowFor(wwin->transient_for);
1558 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1559 tmp = NULL;
1562 /* otherwise, focus the next one in the focus list */
1563 if (!tmp) {
1564 tmp = scr->focused_window;
1565 while (tmp) { /* look for one in the window list first */
1566 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1567 && (tmp->flags.mapped || tmp->flags.shaded))
1568 break;
1569 tmp = tmp->prev;
1571 if (!tmp) { /* if unsuccessful, choose any focusable window */
1572 tmp = scr->focused_window;
1573 while (tmp) {
1574 if (!WFLAGP(tmp, no_focusable)
1575 && (tmp->flags.mapped || tmp->flags.shaded))
1576 break;
1577 tmp = tmp->prev;
1582 newFocusedWindow = tmp;
1584 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1585 unsigned int mask;
1586 int foo;
1587 Window bar, win;
1589 /* This is to let the root window get the keyboard input
1590 * if Sloppy focus mode and no other window get focus.
1591 * This way keybindings will not freeze.
1593 tmp = NULL;
1594 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1595 tmp = wWindowFor(win);
1596 if (tmp == wwin)
1597 tmp = NULL;
1598 newFocusedWindow = tmp;
1599 } else {
1600 newFocusedWindow = NULL;
1604 if (!wwin->flags.internal_window)
1605 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1606 if (wasFocused) {
1607 if (newFocusedWindow != owner && owner) {
1608 if (wwin->flags.is_gnustep == 0)
1609 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1611 wSetFocusTo(scr, newFocusedWindow);
1614 /* Close menu and unhighlight */
1615 WApplication *oapp = wApplicationOf(wwin->main_window);
1616 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1617 if (oapp && oapp != napp) {
1618 wAppMenuUnmap(oapp->menu);
1619 wApplicationDeactivate(oapp);
1622 wNETCleanupFrameExtents(wwin);
1624 wWindowDestroy(wwin);
1625 XFlush(dpy);
1628 void wWindowMap(WWindow *wwin)
1630 XMapWindow(dpy, wwin->frame->core->window);
1631 if (!wwin->flags.shaded) {
1632 /* window will be remapped when getting MapNotify */
1633 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1634 XMapWindow(dpy, wwin->client_win);
1635 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1637 wwin->flags.mapped = 1;
1641 void wWindowUnmap(WWindow *wwin)
1643 wwin->flags.mapped = 0;
1645 /* prevent window withdrawal when getting UnmapNotify */
1646 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1647 XUnmapWindow(dpy, wwin->client_win);
1648 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1650 XUnmapWindow(dpy, wwin->frame->core->window);
1653 void wWindowFocus(WWindow *wwin, WWindow *owin)
1655 WWindow *nowner;
1656 WWindow *oowner;
1658 #ifdef KEEP_XKB_LOCK_STATUS
1659 if (wPreferences.modelock)
1660 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1661 #endif /* KEEP_XKB_LOCK_STATUS */
1663 wwin->flags.semi_focused = 0;
1665 if (wwin->flags.is_gnustep == 0)
1666 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1668 wwin->flags.focused = 1;
1670 wWindowResetMouseGrabs(wwin);
1672 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1674 if (owin == wwin || !owin)
1675 return;
1677 nowner = wWindowFor(wwin->transient_for);
1679 /* new window is a transient for the old window */
1680 if (nowner == owin) {
1681 owin->flags.semi_focused = 1;
1682 wWindowUnfocus(nowner);
1683 return;
1686 oowner = wWindowFor(owin->transient_for);
1688 /* new window is owner of old window */
1689 if (wwin == oowner) {
1690 wWindowUnfocus(owin);
1691 return;
1694 if (!nowner) {
1695 wWindowUnfocus(owin);
1696 return;
1699 /* new window has same owner of old window */
1700 if (oowner == nowner) {
1701 /* prevent unfocusing of owner */
1702 oowner->flags.semi_focused = 0;
1703 wWindowUnfocus(owin);
1704 oowner->flags.semi_focused = 1;
1706 return;
1709 /* nowner != NULL && oowner != nowner */
1710 nowner->flags.semi_focused = 1;
1711 wWindowUnfocus(nowner);
1712 wWindowUnfocus(owin);
1715 void wWindowUnfocus(WWindow *wwin)
1717 CloseWindowMenu(wwin->screen_ptr);
1719 if (wwin->flags.is_gnustep == 0)
1720 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1722 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1723 WWindow *owner;
1724 owner = wWindowFor(wwin->transient_for);
1725 if (owner && owner->flags.semi_focused) {
1726 owner->flags.semi_focused = 0;
1727 if (owner->flags.mapped || owner->flags.shaded) {
1728 wWindowUnfocus(owner);
1729 wFrameWindowPaint(owner->frame);
1733 wwin->flags.focused = 0;
1734 wWindowResetMouseGrabs(wwin);
1735 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1738 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1740 char *title;
1742 if (!wwin->frame)
1743 return;
1745 if (!newTitle)
1746 title = DEF_WINDOW_TITLE; /* the hint was removed */
1747 else
1748 title = newTitle;
1750 if (wFrameWindowChangeTitle(wwin->frame, title))
1751 WMPostNotificationName(WMNChangedName, wwin, NULL);
1755 *----------------------------------------------------------------------
1757 * wWindowConstrainSize--
1758 * Constrains size for the client window, taking the maximal size,
1759 * window resize increments and other size hints into account.
1761 * Returns:
1762 * The closest size to what was given that the client window can
1763 * have.
1765 *----------------------------------------------------------------------
1767 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1769 int width = (int)*nwidth;
1770 int height = (int)*nheight;
1771 int winc = 1;
1772 int hinc = 1;
1773 int minW = 1, minH = 1;
1774 int maxW = wwin->screen_ptr->scr_width * 2;
1775 int maxH = wwin->screen_ptr->scr_height * 2;
1776 int minAX = -1, minAY = -1;
1777 int maxAX = -1, maxAY = -1;
1778 int baseW = 0;
1779 int baseH = 0;
1781 if (wwin->normal_hints) {
1782 winc = wwin->normal_hints->width_inc;
1783 hinc = wwin->normal_hints->height_inc;
1784 minW = wwin->normal_hints->min_width;
1785 minH = wwin->normal_hints->min_height;
1786 maxW = wwin->normal_hints->max_width;
1787 maxH = wwin->normal_hints->max_height;
1788 if (wwin->normal_hints->flags & PAspect) {
1789 minAX = wwin->normal_hints->min_aspect.x;
1790 minAY = wwin->normal_hints->min_aspect.y;
1791 maxAX = wwin->normal_hints->max_aspect.x;
1792 maxAY = wwin->normal_hints->max_aspect.y;
1795 baseW = wwin->normal_hints->base_width;
1796 baseH = wwin->normal_hints->base_height;
1799 if (width < minW)
1800 width = minW;
1801 if (height < minH)
1802 height = minH;
1804 if (width > maxW)
1805 width = maxW;
1806 if (height > maxH)
1807 height = maxH;
1809 /* aspect ratio code borrowed from olwm */
1810 if (minAX > 0) {
1811 /* adjust max aspect ratio */
1812 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1813 if (maxAX > maxAY) {
1814 height = (width * maxAY) / maxAX;
1815 if (height > maxH) {
1816 height = maxH;
1817 width = (height * maxAX) / maxAY;
1819 } else {
1820 width = (height * maxAX) / maxAY;
1821 if (width > maxW) {
1822 width = maxW;
1823 height = (width * maxAY) / maxAX;
1828 /* adjust min aspect ratio */
1829 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1830 if (minAX > minAY) {
1831 height = (width * minAY) / minAX;
1832 if (height < minH) {
1833 height = minH;
1834 width = (height * minAX) / minAY;
1836 } else {
1837 width = (height * minAX) / minAY;
1838 if (width < minW) {
1839 width = minW;
1840 height = (width * minAY) / minAX;
1846 if (baseW != 0)
1847 width = (((width - baseW) / winc) * winc) + baseW;
1848 else
1849 width = (((width - minW) / winc) * winc) + minW;
1851 if (baseH != 0)
1852 height = (((height - baseH) / hinc) * hinc) + baseH;
1853 else
1854 height = (((height - minH) / hinc) * hinc) + minH;
1856 /* broken stupid apps may cause preposterous values for these.. */
1857 if (width > 0)
1858 *nwidth = width;
1859 if (height > 0)
1860 *nheight = height;
1863 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1864 unsigned int *width, unsigned int *height)
1866 int baseW = 0, baseH = 0;
1867 int winc = 1, hinc = 1;
1869 if (wwin->normal_hints) {
1870 baseW = wwin->normal_hints->base_width;
1871 baseH = wwin->normal_hints->base_height;
1873 winc = wwin->normal_hints->width_inc;
1874 hinc = wwin->normal_hints->height_inc;
1877 if (*width > maxW)
1878 *width = maxW - (maxW - baseW) % winc;
1880 if (*height > maxH)
1881 *height = maxH - (maxH - baseH) % hinc;
1884 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1886 WScreen *scr = wwin->screen_ptr;
1887 WApplication *wapp;
1888 int unmap = 0;
1890 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1891 return;
1893 if (workspace != scr->current_workspace) {
1894 /* Sent to other workspace. Unmap window */
1895 if ((wwin->flags.mapped
1896 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1897 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1899 wapp = wApplicationOf(wwin->main_window);
1900 if (wapp)
1901 wapp->last_workspace = workspace;
1903 if (wwin->flags.miniaturized) {
1904 if (wwin->icon) {
1905 XUnmapWindow(dpy, wwin->icon->core->window);
1906 wwin->icon->mapped = 0;
1908 } else {
1909 unmap = 1;
1910 wSetFocusTo(scr, NULL);
1913 } else {
1914 /* brought to current workspace. Map window */
1915 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1916 if (wwin->icon) {
1917 XMapWindow(dpy, wwin->icon->core->window);
1918 wwin->icon->mapped = 1;
1920 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1921 wWindowMap(wwin);
1924 if (!IS_OMNIPRESENT(wwin)) {
1925 int oldWorkspace = wwin->frame->workspace;
1926 wwin->frame->workspace = workspace;
1927 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1930 if (unmap)
1931 wWindowUnmap(wwin);
1934 void wWindowSynthConfigureNotify(WWindow *wwin)
1936 XEvent sevent;
1938 sevent.type = ConfigureNotify;
1939 sevent.xconfigure.display = dpy;
1940 sevent.xconfigure.event = wwin->client_win;
1941 sevent.xconfigure.window = wwin->client_win;
1943 sevent.xconfigure.x = wwin->client.x;
1944 sevent.xconfigure.y = wwin->client.y;
1945 sevent.xconfigure.width = wwin->client.width;
1946 sevent.xconfigure.height = wwin->client.height;
1948 sevent.xconfigure.border_width = wwin->old_border_width;
1949 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1950 sevent.xconfigure.above = wwin->frame->titlebar->window;
1951 else
1952 sevent.xconfigure.above = None;
1954 sevent.xconfigure.override_redirect = False;
1955 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1956 XFlush(dpy);
1960 *----------------------------------------------------------------------
1961 * wWindowConfigure()
1963 * req_x, req_y: new requested positions for the frame
1964 * req_width, req_height: new requested sizes for the client
1966 * Configures the frame, decorations and client window to the specified
1967 * geometry, whose validity is not checked -- wWindowConstrainSize()
1968 * must be used for that.
1969 * The size parameters are for the client window, but the position is
1970 * for the frame.
1971 * The client window receives a ConfigureNotify event, according
1972 * to what ICCCM says.
1974 * Returns:
1975 * None
1977 * Side effects:
1978 * Window size and position are changed and client window receives
1979 * a ConfigureNotify event.
1980 *----------------------------------------------------------------------
1982 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
1984 int synth_notify = False;
1985 int resize;
1987 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
1989 * if the window is being moved but not resized then
1990 * send a synthetic ConfigureNotify
1992 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
1993 synth_notify = True;
1995 if (WFLAGP(wwin, dont_move_off))
1996 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
1998 if (resize) {
1999 if (req_width < MIN_WINDOW_SIZE)
2000 req_width = MIN_WINDOW_SIZE;
2001 if (req_height < MIN_WINDOW_SIZE)
2002 req_height = MIN_WINDOW_SIZE;
2004 /* If growing, resize inner part before frame,
2005 * if shrinking, resize frame before.
2006 * This will prevent the frame (that can have a different color)
2007 * to be exposed, causing flicker */
2008 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2009 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2011 if (wwin->flags.shaded) {
2012 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2013 wwin->old_geometry.height = req_height;
2014 } else {
2015 int h;
2017 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2019 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2022 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2023 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2025 wwin->client.x = req_x;
2026 wwin->client.y = req_y + wwin->frame->top_width;
2027 wwin->client.width = req_width;
2028 wwin->client.height = req_height;
2029 } else {
2030 wwin->client.x = req_x;
2031 wwin->client.y = req_y + wwin->frame->top_width;
2033 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2035 wwin->frame_x = req_x;
2036 wwin->frame_y = req_y;
2037 if (HAS_BORDER(wwin)) {
2038 wwin->client.x += FRAME_BORDER_WIDTH;
2039 wwin->client.y += FRAME_BORDER_WIDTH;
2041 #ifdef SHAPE
2042 if (wShapeSupported && wwin->flags.shaped && resize)
2043 wWindowSetShape(wwin);
2044 #endif
2046 if (synth_notify)
2047 wWindowSynthConfigureNotify(wwin);
2049 wNETFrameExtents(wwin);
2051 XFlush(dpy);
2054 /* req_x, req_y: new position of the frame */
2055 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2057 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2058 int synth_notify = False;
2060 /* Send a synthetic ConfigureNotify event for every window movement. */
2061 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2062 synth_notify = True;
2063 #else
2064 /* A single synthetic ConfigureNotify event is sent at the end of
2065 * a completed (opaque) movement in moveres.c */
2066 #endif
2068 if (WFLAGP(wwin, dont_move_off))
2069 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2070 wwin->frame->core->width, wwin->frame->core->height);
2072 wwin->client.x = req_x;
2073 wwin->client.y = req_y + wwin->frame->top_width;
2074 if (HAS_BORDER(wwin)) {
2075 wwin->client.x += FRAME_BORDER_WIDTH;
2076 wwin->client.y += FRAME_BORDER_WIDTH;
2079 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2081 wwin->frame_x = req_x;
2082 wwin->frame_y = req_y;
2084 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2085 if (synth_notify)
2086 wWindowSynthConfigureNotify(wwin);
2087 #endif
2090 void wWindowUpdateButtonImages(WWindow *wwin)
2092 WScreen *scr = wwin->screen_ptr;
2093 Pixmap pixmap, mask;
2094 WFrameWindow *fwin = wwin->frame;
2096 if (!HAS_TITLEBAR(wwin))
2097 return;
2099 /* miniaturize button */
2100 if (!WFLAGP(wwin, no_miniaturize_button)) {
2101 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2102 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2104 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2105 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2106 else
2107 mask = None;
2109 if (fwin->lbutton_image
2110 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2111 wPixmapDestroy(fwin->lbutton_image);
2112 fwin->lbutton_image = NULL;
2115 if (!fwin->lbutton_image) {
2116 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2117 fwin->lbutton_image->client_owned = 1;
2118 fwin->lbutton_image->client_owned_mask = 1;
2120 } else {
2121 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2122 wPixmapDestroy(fwin->lbutton_image);
2124 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2127 #ifdef XKB_BUTTON_HINT
2128 if (!WFLAGP(wwin, no_language_button)) {
2129 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2130 wPixmapDestroy(fwin->languagebutton_image);
2132 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2134 #endif
2136 /* close button */
2138 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2139 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2141 if (!WFLAGP(wwin, no_close_button)) {
2142 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2143 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2145 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2146 mask = wwin->wm_gnustep_attr->close_mask;
2147 else
2148 mask = None;
2150 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2151 || fwin->rbutton_image->mask != mask)) {
2152 wPixmapDestroy(fwin->rbutton_image);
2153 fwin->rbutton_image = NULL;
2156 if (!fwin->rbutton_image) {
2157 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2158 fwin->rbutton_image->client_owned = 1;
2159 fwin->rbutton_image->client_owned_mask = 1;
2162 } else if (WFLAGP(wwin, kill_close)) {
2163 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2164 wPixmapDestroy(fwin->rbutton_image);
2166 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2168 } else if (MGFLAGP(wwin, broken_close)) {
2169 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2170 wPixmapDestroy(fwin->rbutton_image);
2172 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2174 } else {
2175 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2176 wPixmapDestroy(fwin->rbutton_image);
2178 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2182 /* force buttons to be redrawn */
2183 fwin->flags.need_texture_change = 1;
2184 wFrameWindowPaint(fwin);
2188 *---------------------------------------------------------------------------
2189 * wWindowConfigureBorders--
2190 * Update window border configuration according to attribute flags.
2192 *---------------------------------------------------------------------------
2194 void wWindowConfigureBorders(WWindow *wwin)
2196 if (wwin->frame) {
2197 int flags;
2198 int newy, oldh;
2200 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2202 #ifdef XKB_BUTTON_HINT
2203 if (wPreferences.modelock)
2204 flags |= WFF_LANGUAGE_BUTTON;
2205 #endif
2207 if (HAS_TITLEBAR(wwin))
2208 flags |= WFF_TITLEBAR;
2209 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2210 flags |= WFF_RESIZEBAR;
2211 if (HAS_BORDER(wwin))
2212 flags |= WFF_BORDER;
2213 if (wwin->flags.shaded)
2214 flags |= WFF_IS_SHADED;
2216 oldh = wwin->frame->top_width;
2217 wFrameWindowUpdateBorders(wwin->frame, flags);
2218 if (oldh != wwin->frame->top_width) {
2219 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2221 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2222 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2225 flags = 0;
2226 if (!WFLAGP(wwin, no_miniaturize_button)
2227 && wwin->frame->flags.hide_left_button)
2228 flags |= WFF_LEFT_BUTTON;
2230 #ifdef XKB_BUTTON_HINT
2231 if (!WFLAGP(wwin, no_language_button)
2232 && wwin->frame->flags.hide_language_button)
2233 flags |= WFF_LANGUAGE_BUTTON;
2234 #endif
2236 if (!WFLAGP(wwin, no_close_button)
2237 && wwin->frame->flags.hide_right_button)
2238 flags |= WFF_RIGHT_BUTTON;
2240 if (flags != 0) {
2241 wWindowUpdateButtonImages(wwin);
2242 wFrameWindowShowButton(wwin->frame, flags);
2245 flags = 0;
2246 if (WFLAGP(wwin, no_miniaturize_button)
2247 && !wwin->frame->flags.hide_left_button)
2248 flags |= WFF_LEFT_BUTTON;
2250 #ifdef XKB_BUTTON_HINT
2251 if (WFLAGP(wwin, no_language_button)
2252 && !wwin->frame->flags.hide_language_button)
2253 flags |= WFF_LANGUAGE_BUTTON;
2254 #endif
2256 if (WFLAGP(wwin, no_close_button)
2257 && !wwin->frame->flags.hide_right_button)
2258 flags |= WFF_RIGHT_BUTTON;
2260 if (flags != 0)
2261 wFrameWindowHideButton(wwin->frame, flags);
2263 #ifdef SHAPE
2264 if (wShapeSupported && wwin->flags.shaped)
2265 wWindowSetShape(wwin);
2266 #endif
2270 void wWindowSaveState(WWindow * wwin)
2272 long data[10];
2273 int i;
2275 memset(data, 0, sizeof(long) * 10);
2276 data[0] = wwin->frame->workspace;
2277 data[1] = wwin->flags.miniaturized;
2278 data[2] = wwin->flags.shaded;
2279 data[3] = wwin->flags.hidden;
2280 data[4] = wwin->flags.maximized;
2281 if (wwin->flags.maximized == 0) {
2282 data[5] = wwin->frame_x;
2283 data[6] = wwin->frame_y;
2284 data[7] = wwin->frame->core->width;
2285 data[8] = wwin->frame->core->height;
2286 } else {
2287 data[5] = wwin->old_geometry.x;
2288 data[6] = wwin->old_geometry.y;
2289 data[7] = wwin->old_geometry.width;
2290 data[8] = wwin->old_geometry.height;
2293 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2294 if (wwin->screen_ptr->shortcutWindows[i] &&
2295 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2296 data[9] |= 1 << i;
2298 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2299 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2302 static int getSavedState(Window window, WSavedState ** state)
2304 Atom type_ret;
2305 int fmt_ret;
2306 unsigned long nitems_ret;
2307 unsigned long bytes_after_ret;
2308 long *data;
2310 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2311 True, _XA_WINDOWMAKER_STATE,
2312 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2313 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2314 return 0;
2316 *state = wmalloc(sizeof(WSavedState));
2318 (*state)->workspace = data[0];
2319 (*state)->miniaturized = data[1];
2320 (*state)->shaded = data[2];
2321 (*state)->hidden = data[3];
2322 (*state)->maximized = data[4];
2323 (*state)->x = data[5];
2324 (*state)->y = data[6];
2325 (*state)->w = data[7];
2326 (*state)->h = data[8];
2327 (*state)->window_shortcuts = data[9];
2329 XFree(data);
2331 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2332 return 1;
2333 else
2334 return 0;
2337 #ifdef SHAPE
2338 void wWindowClearShape(WWindow * wwin)
2340 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2341 0, wwin->frame->top_width, None, ShapeSet);
2342 XFlush(dpy);
2345 void wWindowSetShape(WWindow * wwin)
2347 XRectangle rect[2];
2348 int count;
2349 #ifdef OPTIMIZE_SHAPE
2350 XRectangle *rects;
2351 XRectangle *urec;
2352 int ordering;
2354 /* only shape is the client's */
2355 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2356 goto alt_code;
2358 /* Get array of rectangles describing the shape mask */
2359 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2360 if (!rects)
2361 goto alt_code;
2363 urec = malloc(sizeof(XRectangle) * (count + 2));
2364 if (!urec) {
2365 XFree(rects);
2366 goto alt_code;
2369 /* insert our decoration rectangles in the rect list */
2370 memcpy(urec, rects, sizeof(XRectangle) * count);
2371 XFree(rects);
2373 if (HAS_TITLEBAR(wwin)) {
2374 urec[count].x = -1;
2375 urec[count].y = -1 - wwin->frame->top_width;
2376 urec[count].width = wwin->frame->core->width + 2;
2377 urec[count].height = wwin->frame->top_width + 1;
2378 count++;
2380 if (HAS_RESIZEBAR(wwin)) {
2381 urec[count].x = -1;
2382 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2383 urec[count].width = wwin->frame->core->width + 2;
2384 urec[count].height = wwin->frame->bottom_width + 1;
2385 count++;
2388 /* shape our frame window */
2389 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2390 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2391 XFlush(dpy);
2392 wfree(urec);
2393 return;
2395 alt_code:
2396 #endif /* OPTIMIZE_SHAPE */
2397 count = 0;
2398 if (HAS_TITLEBAR(wwin)) {
2399 rect[count].x = -1;
2400 rect[count].y = -1;
2401 rect[count].width = wwin->frame->core->width + 2;
2402 rect[count].height = wwin->frame->top_width + 1;
2403 count++;
2405 if (HAS_RESIZEBAR(wwin)) {
2406 rect[count].x = -1;
2407 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2408 rect[count].width = wwin->frame->core->width + 2;
2409 rect[count].height = wwin->frame->bottom_width + 1;
2410 count++;
2412 if (count > 0) {
2413 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2414 0, 0, rect, count, ShapeSet, Unsorted);
2416 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2417 0, wwin->frame->top_width, wwin->client_win,
2418 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2419 XFlush(dpy);
2421 #endif /* SHAPE */
2423 /* ====================================================================== */
2425 static FocusMode getFocusMode(WWindow * wwin)
2427 FocusMode mode;
2429 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2430 if (wwin->wm_hints->input == True) {
2431 if (wwin->protocols.TAKE_FOCUS)
2432 mode = WFM_LOCALLY_ACTIVE;
2433 else
2434 mode = WFM_PASSIVE;
2435 } else {
2436 if (wwin->protocols.TAKE_FOCUS)
2437 mode = WFM_GLOBALLY_ACTIVE;
2438 else
2439 mode = WFM_NO_INPUT;
2441 } else {
2442 mode = WFM_PASSIVE;
2444 return mode;
2447 void wWindowSetKeyGrabs(WWindow * wwin)
2449 int i;
2450 WShortKey *key;
2452 for (i = 0; i < WKBD_LAST; i++) {
2453 key = &wKeyBindings[i];
2455 if (key->keycode == 0)
2456 continue;
2457 if (key->modifier != AnyModifier) {
2458 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2459 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2460 #ifdef NUMLOCK_HACK
2461 /* Also grab all modifier combinations possible that include,
2462 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2463 * work even if the NumLock/ScrollLock key is on.
2465 wHackedGrabKey(key->keycode, key->modifier,
2466 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2467 #endif
2469 XGrabKey(dpy, key->keycode, key->modifier,
2470 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2473 wRootMenuBindShortcuts(wwin->frame->core->window);
2476 void wWindowResetMouseGrabs(WWindow * wwin)
2478 /* Mouse grabs can't be done on the client window because of
2479 * ICCCM and because clients that try to do the same will crash.
2481 * But there is a problem wich makes tbar buttons of unfocused
2482 * windows not usable as the click goes to the frame window instead
2483 * of the button itself. Must figure a way to fix that.
2486 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2488 if (!WFLAGP(wwin, no_bind_mouse)) {
2489 /* grabs for Meta+drag */
2490 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2491 True, ButtonPressMask | ButtonReleaseMask,
2492 GrabModeSync, GrabModeAsync, None, None);
2494 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2495 * but we only grab it for Button4 and Button 5 since a lot of apps
2496 * use CTRL+Button1-3 for app related functionality
2498 if (wPreferences.resize_increment > 0) {
2499 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2500 True, ButtonPressMask | ButtonReleaseMask,
2501 GrabModeSync, GrabModeAsync, None, None);
2502 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2503 True, ButtonPressMask | ButtonReleaseMask,
2504 GrabModeSync, GrabModeAsync, None, None);
2506 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2507 True, ButtonPressMask | ButtonReleaseMask,
2508 GrabModeSync, GrabModeAsync, None, None);
2509 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2510 True, ButtonPressMask | ButtonReleaseMask,
2511 GrabModeSync, GrabModeAsync, None, None);
2515 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2516 && !wwin->flags.is_gnustep) {
2517 /* the passive grabs to focus the window */
2518 /* if (wPreferences.focus_mode == WKF_CLICK) */
2519 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2520 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2522 XFlush(dpy);
2525 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2527 if (attr->flags & GSExtraFlagsAttr) {
2528 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2529 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2530 wWindowUpdateButtonImages(wwin);
2535 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2537 WWindowState *wstate;
2539 wstate = malloc(sizeof(WWindowState));
2540 if (!wstate)
2541 return NULL;
2543 memset(wstate, 0, sizeof(WWindowState));
2544 wstate->pid = pid;
2545 if (instance)
2546 wstate->instance = wstrdup(instance);
2547 if (class)
2548 wstate->class = wstrdup(class);
2549 if (command)
2550 wstate->command = wstrdup(command);
2551 wstate->state = state;
2553 wstate->next = windowState;
2554 windowState = wstate;
2556 return wstate;
2559 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2561 WMagicNumber wWindowGetSavedState(Window win)
2563 char *instance, *class, *command = NULL;
2564 WWindowState *wstate = windowState;
2566 if (!wstate)
2567 return NULL;
2569 command = GetCommandForWindow(win);
2570 if (!command)
2571 return NULL;
2573 if (PropGetWMClass(win, &class, &instance)) {
2574 while (wstate) {
2575 if (SAME(instance, wstate->instance) &&
2576 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2577 break;
2579 wstate = wstate->next;
2581 } else {
2582 wstate = NULL;
2585 if (command)
2586 wfree(command);
2587 if (instance)
2588 free(instance);
2589 if (class)
2590 free(class);
2592 return wstate;
2595 void wWindowDeleteSavedState(WMagicNumber id)
2597 WWindowState *tmp, *wstate = (WWindowState *) id;
2599 if (!wstate || !windowState)
2600 return;
2602 tmp = windowState;
2603 if (tmp == wstate) {
2604 windowState = wstate->next;
2605 remove_wwindowstate(wstate);
2606 } else {
2607 while (tmp->next) {
2608 if (tmp->next == wstate) {
2609 tmp->next = wstate->next;
2610 remove_wwindowstate(wstate);
2611 break;
2613 tmp = tmp->next;
2618 void wWindowDeleteSavedStatesForPID(pid_t pid)
2620 WWindowState *tmp, *wstate;
2622 if (!windowState)
2623 return;
2625 tmp = windowState;
2626 if (tmp->pid == pid) {
2627 wstate = windowState;
2628 windowState = tmp->next;
2630 remove_wwindowstate(wstate);
2631 } else {
2632 while (tmp->next) {
2633 if (tmp->next->pid == pid) {
2634 wstate = tmp->next;
2635 tmp->next = wstate->next;
2636 remove_wwindowstate(wstate);
2637 break;
2639 tmp = tmp->next;
2644 static void remove_wwindowstate(WWindowState *wstate)
2646 if (wstate->instance)
2647 wfree(wstate->instance);
2649 if (wstate->class)
2650 wfree(wstate->class);
2652 if (wstate->command)
2653 wfree(wstate->command);
2655 wfree(wstate->state);
2656 wfree(wstate);
2659 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2661 if (wwin->flags.omnipresent == flag)
2662 return;
2664 wwin->flags.omnipresent = flag;
2665 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2668 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2670 WWindow *wwin = data;
2672 #ifndef NUMLOCK_HACK
2673 if ((event->xbutton.state & ValidModMask)
2674 != (event->xbutton.state & ~LockMask)) {
2675 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2676 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2678 #endif
2680 event->xbutton.state &= ValidModMask;
2682 CloseWindowMenu(wwin->screen_ptr);
2684 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2685 && !WFLAGP(wwin, no_focusable)) {
2686 wSetFocusTo(wwin->screen_ptr, wwin);
2689 if (event->xbutton.button == Button1)
2690 wRaiseFrame(wwin->frame->core);
2692 if (event->xbutton.window != wwin->frame->resizebar->window) {
2693 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2694 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2695 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2696 return;
2700 if (event->xbutton.state & MOD_MASK) {
2701 /* move the window */
2702 wMouseMoveWindow(wwin, event);
2703 XUngrabPointer(dpy, CurrentTime);
2704 } else {
2705 wMouseResizeWindow(wwin, event);
2706 XUngrabPointer(dpy, CurrentTime);
2710 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2712 WWindow *wwin = data;
2714 event->xbutton.state &= ValidModMask;
2716 if (event->xbutton.button == Button1) {
2717 if (event->xbutton.state == 0) {
2718 if (!WFLAGP(wwin, no_shadeable)) {
2719 /* shade window */
2720 if (wwin->flags.shaded)
2721 wUnshadeWindow(wwin);
2722 else
2723 wShadeWindow(wwin);
2725 } else {
2726 int dir = 0;
2728 if (event->xbutton.state & ControlMask)
2729 dir |= MAX_VERTICAL;
2731 if (event->xbutton.state & ShiftMask) {
2732 dir |= MAX_HORIZONTAL;
2733 if (!(event->xbutton.state & ControlMask))
2734 wSelectWindow(wwin, !wwin->flags.selected);
2737 /* maximize window */
2738 if (dir != 0 && IS_RESIZABLE(wwin)) {
2739 int ndir = dir ^ wwin->flags.maximized;
2741 if (ndir != 0)
2742 wMaximizeWindow(wwin, ndir);
2743 else
2744 wUnmaximizeWindow(wwin);
2747 } else if (event->xbutton.button == Button3) {
2748 if (event->xbutton.state & MOD_MASK)
2749 wHideOtherApplications(wwin);
2750 } else if (event->xbutton.button == Button2) {
2751 wSelectWindow(wwin, !wwin->flags.selected);
2752 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2753 wShadeWindow(wwin);
2754 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2755 wUnshadeWindow(wwin);
2759 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2761 WWindow *wwin = desc->parent;
2762 unsigned int new_width, w_scale;
2763 unsigned int new_height, h_scale;
2764 unsigned int resize_width_increment = 0;
2765 unsigned int resize_height_increment = 0;
2767 if (wwin->normal_hints) {
2768 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2769 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2770 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2771 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2773 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2774 resize_width_increment = wPreferences.resize_increment;
2775 resize_height_increment = wPreferences.resize_increment;
2778 event->xbutton.state &= ValidModMask;
2780 CloseWindowMenu(wwin->screen_ptr);
2782 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2783 wSetFocusTo(wwin->screen_ptr, wwin);
2785 if (event->xbutton.button == Button1)
2786 wRaiseFrame(wwin->frame->core);
2788 if (event->xbutton.state & ControlMask) {
2789 if (event->xbutton.button == Button4) {
2790 new_width = wwin->client.width - resize_width_increment;
2791 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2792 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2794 if (event->xbutton.button == Button5) {
2795 new_width = wwin->client.width + resize_width_increment;
2796 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2797 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2801 if (event->xbutton.state & MOD_MASK) {
2802 /* move the window */
2803 if (XGrabPointer(dpy, wwin->client_win, False,
2804 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2805 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2806 return;
2808 if (event->xbutton.button == Button3) {
2809 wMouseResizeWindow(wwin, event);
2810 } else if (event->xbutton.button == Button4) {
2811 new_height = wwin->client.height - resize_height_increment;
2812 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2813 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2814 } else if (event->xbutton.button == Button5) {
2815 new_height = wwin->client.height + resize_height_increment;
2816 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2817 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2818 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2819 wMouseMoveWindow(wwin, event);
2821 XUngrabPointer(dpy, CurrentTime);
2825 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2827 WWindow *wwin = (WWindow *) data;
2829 #ifndef NUMLOCK_HACK
2830 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2831 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2832 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2833 #endif
2834 event->xbutton.state &= ValidModMask;
2836 CloseWindowMenu(wwin->screen_ptr);
2838 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2839 && !WFLAGP(wwin, no_focusable))
2840 wSetFocusTo(wwin->screen_ptr, wwin);
2842 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2844 if (event->xbutton.button == Button1) {
2845 if (event->xbutton.state & MOD_MASK)
2846 wLowerFrame(wwin->frame->core);
2847 else
2848 wRaiseFrame(wwin->frame->core);
2850 if ((event->xbutton.state & ShiftMask)
2851 && !(event->xbutton.state & ControlMask)) {
2852 wSelectWindow(wwin, !wwin->flags.selected);
2853 return;
2855 if (event->xbutton.window != wwin->frame->titlebar->window
2856 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2857 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2858 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2859 return;
2862 /* move the window */
2863 wMouseMoveWindow(wwin, event);
2865 XUngrabPointer(dpy, CurrentTime);
2866 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2867 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2868 WObjDescriptor *desc;
2870 if (event->xbutton.window != wwin->frame->titlebar->window
2871 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2872 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2873 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2874 return;
2877 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2879 /* allow drag select */
2880 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2881 event->xany.send_event = True;
2882 (*desc->handle_mousedown) (desc, event);
2884 XUngrabPointer(dpy, CurrentTime);
2888 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2890 WWindow *wwin = data;
2892 event->xbutton.state &= ValidModMask;
2894 CloseWindowMenu(wwin->screen_ptr);
2896 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2897 return;
2899 /* if control-click, kill the client */
2900 if (event->xbutton.state & ControlMask) {
2901 wClientKill(wwin);
2902 } else {
2903 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2904 /* send delete message */
2905 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2910 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2912 WWindow *wwin = data;
2914 CloseWindowMenu(wwin->screen_ptr);
2916 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2917 return;
2919 /* send delete message */
2920 if (wwin->protocols.DELETE_WINDOW)
2921 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2922 else
2923 wClientKill(wwin);
2926 #ifdef XKB_BUTTON_HINT
2927 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2929 WWindow *wwin = data;
2930 WFrameWindow *fwin = wwin->frame;
2931 WScreen *scr = fwin->screen_ptr;
2932 int tl;
2934 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2935 return;
2936 tl = wwin->frame->languagemode;
2937 wwin->frame->languagemode = wwin->frame->last_languagemode;
2938 wwin->frame->last_languagemode = tl;
2939 wSetFocusTo(scr, wwin);
2940 wwin->frame->languagebutton_image =
2941 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2942 wFrameWindowUpdateLanguageButton(wwin->frame);
2943 if (event->xbutton.button == Button3)
2944 return;
2945 wRaiseFrame(fwin->core);
2947 #endif
2949 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2951 WWindow *wwin = data;
2953 event->xbutton.state &= ValidModMask;
2955 CloseWindowMenu(wwin->screen_ptr);
2957 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2958 return;
2960 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2961 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2962 } else {
2963 WApplication *wapp;
2964 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2966 wapp = wApplicationOf(wwin->main_window);
2967 if (wapp && !WFLAGP(wwin, no_appicon))
2968 wHideApplication(wapp);
2969 } else if (event->xbutton.state == 0) {
2970 wIconifyWindow(wwin);