Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / window.c
blob9ac5e78a797dfa8f30e567f9089e2080721467a8
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/WINGs.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 "colormap.h"
53 #include "keybind.h"
54 #include "stacking.h"
55 #include "defaults.h"
56 #include "workspace.h"
57 #include "xinerama.h"
58 #include "appmenu.h"
59 #include "appicon.h"
60 #include "superfluous.h"
61 #include "rootmenu.h"
62 #include "placement.h"
63 #include "misc.h"
65 #ifdef MWM_HINTS
66 # include "motif.h"
67 #endif
68 #include "wmspec.h"
70 #define MOD_MASK wPreferences.modifier_mask
72 /****** Global Variables ******/
73 extern WShortKey wKeyBindings[WKBD_LAST];
75 #ifdef SHAPE
76 extern Bool wShapeSupported;
77 #endif
79 /* contexts */
80 extern XContext wWinContext;
82 /* protocol atoms */
83 extern Atom _XA_WM_DELETE_WINDOW;
84 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
85 extern Atom _XA_WINDOWMAKER_STATE;
86 extern WPreferences wPreferences;
87 extern Time LastTimestamp;
89 /***** Local Stuff *****/
90 static WWindowState *windowState = NULL;
91 static FocusMode getFocusMode(WWindow *wwin);
92 static int getSavedState(Window window, WSavedState **state);
93 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
95 /* frame window (during window grabs) */
96 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
98 /* close button */
99 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
100 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
102 /* iconify button */
103 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
105 #ifdef XKB_BUTTON_HINT
106 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
107 #endif
109 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
110 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
111 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
113 static void release_wwindowstate(WWindowState *wstate);
115 /****** Notification Observers ******/
117 static void appearanceObserver(void *self, WMNotification * notif)
119 WWindow *wwin = (WWindow *) self;
120 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
122 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
123 return;
125 if (flags & WFontSettings) {
126 wWindowConfigureBorders(wwin);
127 if (wwin->flags.shaded) {
128 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
129 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
130 wWindowSynthConfigureNotify(wwin);
133 if (flags & WTextureSettings)
134 wwin->frame->flags.need_texture_remake = 1;
136 if (flags & (WTextureSettings | WColorSettings)) {
137 if (wwin->frame->titlebar)
138 XClearWindow(dpy, wwin->frame->titlebar->window);
140 wFrameWindowPaint(wwin->frame);
145 /* Return the WWindow associated with a given (Xlib) Window. */
146 WWindow *wWindowFor(Window window)
148 WObjDescriptor *desc;
150 if (window == None)
151 return NULL;
153 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
154 return NULL;
156 if (desc->parent_type == WCLASS_WINDOW)
157 return desc->parent;
158 else if (desc->parent_type == WCLASS_FRAME) {
159 WFrameWindow *frame = (WFrameWindow *) desc->parent;
160 if (frame->flags.is_client_window_frame)
161 return frame->child;
164 return NULL;
167 WWindow *wWindowCreate(void)
169 WWindow *wwin;
171 wwin = wmalloc(sizeof(WWindow));
172 wretain(wwin);
174 wwin->client_descriptor.handle_mousedown = frameMouseDown;
175 wwin->client_descriptor.parent = wwin;
176 wwin->client_descriptor.self = wwin;
177 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
179 return wwin;
182 void wWindowDestroy(WWindow *wwin)
184 int i;
186 if (wwin->screen_ptr->cmap_window == wwin)
187 wwin->screen_ptr->cmap_window = NULL;
189 WMRemoveNotificationObserver(wwin);
191 wwin->flags.destroyed = 1;
193 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
194 if (!wwin->screen_ptr->shortcutWindows[i])
195 continue;
197 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
199 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
200 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
201 wwin->screen_ptr->shortcutWindows[i] = NULL;
205 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
206 wwin->fake_group->retainCount--;
207 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
208 XDestroyWindow(dpy, wwin->fake_group->leader);
209 wwin->fake_group->leader = None;
210 wwin->fake_group->origLeader = None;
211 XFlush(dpy);
215 if (wwin->normal_hints)
216 XFree(wwin->normal_hints);
218 if (wwin->wm_hints)
219 XFree(wwin->wm_hints);
221 if (wwin->wm_instance)
222 XFree(wwin->wm_instance);
224 if (wwin->wm_class)
225 XFree(wwin->wm_class);
227 if (wwin->wm_gnustep_attr)
228 wfree(wwin->wm_gnustep_attr);
230 if (wwin->cmap_windows)
231 XFree(wwin->cmap_windows);
233 XDeleteContext(dpy, wwin->client_win, wWinContext);
235 if (wwin->frame)
236 wFrameWindowDestroy(wwin->frame);
238 if (wwin->icon) {
239 RemoveFromStackList(wwin->icon->core);
240 wIconDestroy(wwin->icon);
241 if (wPreferences.auto_arrange_icons)
242 wArrangeIcons(wwin->screen_ptr, True);
244 if (wwin->net_icon_image)
245 RReleaseImage(wwin->net_icon_image);
247 wrelease(wwin);
250 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
252 if (gs_hints->flags & GSWindowStyleAttr) {
253 if (gs_hints->window_style == WMBorderlessWindowMask) {
254 wwin->client_flags.no_border = 1;
255 wwin->client_flags.no_titlebar = 1;
256 wwin->client_flags.no_closable = 1;
257 wwin->client_flags.no_miniaturizable = 1;
258 wwin->client_flags.no_resizable = 1;
259 wwin->client_flags.no_close_button = 1;
260 wwin->client_flags.no_miniaturize_button = 1;
261 wwin->client_flags.no_resizebar = 1;
262 } else {
263 wwin->client_flags.no_close_button =
264 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
266 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
268 wwin->client_flags.no_miniaturize_button =
269 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
271 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
273 wwin->client_flags.no_resizebar =
274 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
276 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
278 /* these attributes supposedly imply in the existence
279 * of a titlebar */
280 if (gs_hints->window_style & (WMResizableWindowMask |
281 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
282 wwin->client_flags.no_titlebar = 0;
283 } else {
284 wwin->client_flags.no_titlebar =
285 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
289 } else {
290 /* setup the defaults */
291 wwin->client_flags.no_border = 0;
292 wwin->client_flags.no_titlebar = 0;
293 wwin->client_flags.no_closable = 0;
294 wwin->client_flags.no_miniaturizable = 0;
295 wwin->client_flags.no_resizable = 0;
296 wwin->client_flags.no_close_button = 0;
297 wwin->client_flags.no_miniaturize_button = 0;
298 wwin->client_flags.no_resizebar = 0;
300 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
301 wwin->client_flags.no_appicon = 1;
304 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
306 WScreen *scr = wwin->screen_ptr;
308 /* sets global default stuff */
309 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
311 * Decoration setting is done in this precedence (lower to higher)
312 * - use global default in the resource database
313 * - guess some settings
314 * - use GNUstep/external window attributes
315 * - set hints specified for the app in the resource DB
318 WSETUFLAG(wwin, broken_close, 0);
320 if (wwin->protocols.DELETE_WINDOW)
321 WSETUFLAG(wwin, kill_close, 0);
322 else
323 WSETUFLAG(wwin, kill_close, 1);
325 /* transients can't be iconified or maximized */
326 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
327 WSETUFLAG(wwin, no_miniaturizable, 1);
328 WSETUFLAG(wwin, no_miniaturize_button, 1);
331 /* if the window can't be resized, remove the resizebar */
332 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
333 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
334 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
335 WSETUFLAG(wwin, no_resizable, 1);
336 WSETUFLAG(wwin, no_resizebar, 1);
339 /* set GNUstep window attributes */
340 if (wwin->wm_gnustep_attr) {
341 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
343 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
345 *level = wwin->wm_gnustep_attr->window_level;
347 * INT_MIN is the only illegal window level.
349 if (*level == INT_MIN)
350 *level = INT_MIN + 1;
351 } else {
352 /* setup defaults */
353 *level = WMNormalLevel;
355 } else {
356 int tmp_workspace = -1;
357 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
359 #ifdef MWM_HINTS
360 wMWMCheckClientHints(wwin);
361 #endif /* MWM_HINTS */
363 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
365 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
366 * have INT_MIN that means that no window level was requested. -Dan
368 if (tmp_level == INT_MIN) {
369 if (WFLAGP(wwin, floating))
370 *level = WMFloatingLevel;
371 else if (WFLAGP(wwin, sunken))
372 *level = WMSunkenLevel;
373 else
374 *level = WMNormalLevel;
375 } else {
376 *level = tmp_level;
379 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
380 WWindow *transientOwner = wWindowFor(wwin->transient_for);
381 if (transientOwner) {
382 int ownerLevel = transientOwner->frame->core->stacking->window_level;
383 if (ownerLevel > *level)
384 *level = ownerLevel;
388 if (tmp_workspace >= 0)
389 *workspace = tmp_workspace % scr->workspace_count;
393 * Set attributes specified only for that window/class.
394 * This might do duplicate work with the 1st wDefaultFillAttributes().
396 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
397 &wwin->defined_user_flags, False);
399 * Sanity checks for attributes that depend on other attributes
401 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
402 wwin->user_flags.emulate_appicon = 0;
404 if (wwin->main_window != None) {
405 WApplication *wapp = wApplicationOf(wwin->main_window);
406 if (wapp && !wapp->flags.emulated)
407 wwin->user_flags.emulate_appicon = 0;
410 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
411 wwin->user_flags.emulate_appicon = 0;
413 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
414 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
415 wwin->user_flags.sunken = 0;
417 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
419 /* windows that have takefocus=False shouldn't take focus at all */
420 if (wwin->focus_mode == WFM_NO_INPUT)
421 wwin->client_flags.no_focusable = 1;
424 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
426 int w1, h1, w2, h2;
428 w1 = wwin->frame->core->width;
429 h1 = wwin->frame->core->height;
430 w2 = obscured->frame->core->width;
431 h2 = obscured->frame->core->height;
433 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
434 && wwin->frame->workspace != obscured->frame->workspace)
435 return False;
437 if (wwin->frame_x + w1 < obscured->frame_x
438 || wwin->frame_y + h1 < obscured->frame_y
439 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
440 return False;
442 return True;
445 static void fixLeaderProperties(WWindow *wwin)
447 XClassHint *classHint;
448 XWMHints *hints, *clientHints;
449 XWindowAttributes attr;
450 Window leaders[2], window;
451 char **argv, *command;
452 int argc, i, pid;
453 Bool haveCommand;
455 classHint = XAllocClassHint();
456 clientHints = XGetWMHints(dpy, wwin->client_win);
457 pid = wNETWMGetPidForWindow(wwin->client_win);
458 if (pid > 0)
459 haveCommand = GetCommandForPid(pid, &argv, &argc);
460 else
461 haveCommand = False;
463 leaders[0] = wwin->client_leader;
464 leaders[1] = wwin->group_id;
466 if (haveCommand) {
467 command = GetCommandForWindow(wwin->client_win);
468 if (command)
469 wfree(command); /* command already set. nothing to do. */
470 else
471 XSetCommand(dpy, wwin->client_win, argv, argc);
474 for (i = 0; i < 2; i++) {
475 window = leaders[i];
476 if (window) {
477 if (XGetClassHint(dpy, window, classHint) == 0) {
478 classHint->res_name = wwin->wm_instance;
479 classHint->res_class = wwin->wm_class;
480 XSetClassHint(dpy, window, classHint);
482 hints = XGetWMHints(dpy, window);
483 if (hints) {
484 XFree(hints);
485 } else if (clientHints) {
486 /* set window group leader to self */
487 clientHints->window_group = window;
488 clientHints->flags |= WindowGroupHint;
489 XSetWMHints(dpy, window, clientHints);
492 if (haveCommand) {
493 command = GetCommandForWindow(window);
494 if (command)
495 wfree(command); /* command already set. nothing to do. */
496 else
497 XSetCommand(dpy, window, argv, argc);
500 /* Make sure we get notification when this window is destroyed */
501 if (XGetWindowAttributes(dpy, window, &attr))
502 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
506 XFree(classHint);
507 if (clientHints)
508 XFree(clientHints);
509 if (haveCommand)
510 wfree(argv);
513 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
515 XClassHint *classHint;
516 XWMHints *hints;
517 Window leader;
518 int argc;
519 char **argv;
521 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
522 /* set class hint */
523 classHint = XAllocClassHint();
524 classHint->res_name = instance;
525 classHint->res_class = class;
526 XSetClassHint(dpy, leader, classHint);
527 XFree(classHint);
529 /* inherit these from the original leader if available */
530 hints = XGetWMHints(dpy, win);
531 if (!hints) {
532 hints = XAllocWMHints();
533 hints->flags = 0;
535 /* set window group leader to self */
536 hints->window_group = leader;
537 hints->flags |= WindowGroupHint;
538 XSetWMHints(dpy, leader, hints);
539 XFree(hints);
541 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
542 XSetCommand(dpy, leader, argv, argc);
543 XFreeStringList(argv);
546 return leader;
549 static int matchIdentifier(const void *item, const void *cdata)
551 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
555 *----------------------------------------------------------------
556 * wManageWindow--
557 * reparents the window and allocates a descriptor for it.
558 * Window manager hints and other hints are fetched to configure
559 * the window decoration attributes and others. User preferences
560 * for the window are used if available, to configure window
561 * decorations and some behaviour.
562 * If in startup, windows that are override redirect,
563 * unmapped and never were managed and are Withdrawn are not
564 * managed.
566 * Returns:
567 * the new window descriptor
569 * Side effects:
570 * The window is reparented and appropriate notification
571 * is done to the client. Input mask for the window is setup.
572 * The window descriptor is also associated with various window
573 * contexts and inserted in the head of the window list.
574 * Event handler contexts are associated for some objects
575 * (buttons, titlebar and resizebar)
577 *----------------------------------------------------------------
579 WWindow *wManageWindow(WScreen *scr, Window window)
581 WWindow *wwin;
582 int x, y;
583 unsigned width, height;
584 XWindowAttributes wattribs;
585 XSetWindowAttributes attribs;
586 WWindowState *win_state;
587 WWindow *transientOwner = NULL;
588 int window_level;
589 int wm_state;
590 int foo;
591 int workspace = -1;
592 char *title;
593 Bool withdraw = False;
594 Bool raise = False;
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);
997 if (wPreferences.window_placement == WPM_MANUAL)
998 dontBring = True;
1000 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1001 int head, flags;
1002 WMRect rect;
1003 int reposition = 0;
1005 /* Make spash screens come out in the center of a head
1006 * trouble is that most splashies never get here
1007 * they are managed trough atoms but god knows where.
1008 * Dan, do you know ? -peter
1010 * Most of them are not managed, they have set
1011 * OverrideRedirect, which means we can't do anything about
1012 * them. -alfredo */
1014 /* xinerama checks for: across head and dead space */
1015 rect.pos.x = x;
1016 rect.pos.y = y;
1017 rect.size.width = width;
1018 rect.size.height = height;
1020 head = wGetRectPlacementInfo(scr, rect, &flags);
1022 if (flags & XFLAG_DEAD)
1023 reposition = 1;
1025 if (flags & XFLAG_MULTIPLE)
1026 reposition = 2;
1029 switch (reposition) {
1030 case 1:
1031 head = wGetHeadForPointerLocation(scr);
1032 rect = wGetRectForHead(scr, head);
1034 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1035 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1036 break;
1038 case 2:
1039 rect = wGetRectForHead(scr, head);
1041 if (x < rect.pos.x)
1042 x = rect.pos.x;
1043 else if (x + width > rect.pos.x + rect.size.width)
1044 x = rect.pos.x + rect.size.width - width;
1046 if (y < rect.pos.y)
1047 y = rect.pos.y;
1048 else if (y + height > rect.pos.y + rect.size.height)
1049 y = rect.pos.y + rect.size.height - height;
1051 break;
1053 default:
1054 break;
1058 if (WFLAGP(wwin, dont_move_off) && dontBring)
1059 wScreenBringInside(scr, &x, &y, width, height);
1062 wNETWMPositionSplash(wwin, &x, &y, width, height);
1064 if (wwin->flags.urgent) {
1065 if (!IS_OMNIPRESENT(wwin))
1066 wwin->flags.omnipresent ^= 1;
1069 /* Create frame, borders and do reparenting */
1070 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1071 #ifdef XKB_BUTTON_HINT
1072 if (wPreferences.modelock)
1073 foo |= WFF_LANGUAGE_BUTTON;
1074 #endif
1075 if (HAS_TITLEBAR(wwin))
1076 foo |= WFF_TITLEBAR;
1078 if (HAS_RESIZEBAR(wwin))
1079 foo |= WFF_RESIZEBAR;
1081 if (HAS_BORDER(wwin))
1082 foo |= WFF_BORDER;
1084 wwin->frame = wFrameWindowCreate(scr, window_level,
1085 x, y, width, height,
1086 &wPreferences.window_title_clearance,
1087 &wPreferences.window_title_min_height,
1088 &wPreferences.window_title_max_height,
1089 foo,
1090 scr->window_title_texture,
1091 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1092 wattribs.depth, wattribs.visual, wattribs.colormap);
1094 wwin->frame->flags.is_client_window_frame = 1;
1095 wwin->frame->flags.justification = wPreferences.title_justification;
1097 /* setup button images */
1098 wWindowUpdateButtonImages(wwin);
1100 /* hide unused buttons */
1101 foo = 0;
1102 if (WFLAGP(wwin, no_close_button))
1103 foo |= WFF_RIGHT_BUTTON;
1105 if (WFLAGP(wwin, no_miniaturize_button))
1106 foo |= WFF_LEFT_BUTTON;
1108 #ifdef XKB_BUTTON_HINT
1109 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1110 foo |= WFF_LANGUAGE_BUTTON;
1111 #endif
1112 if (foo != 0)
1113 wFrameWindowHideButton(wwin->frame, foo);
1115 wwin->frame->child = wwin;
1116 wwin->frame->workspace = workspace;
1117 wwin->frame->on_click_left = windowIconifyClick;
1119 #ifdef XKB_BUTTON_HINT
1120 if (wPreferences.modelock)
1121 wwin->frame->on_click_language = windowLanguageClick;
1122 #endif
1124 wwin->frame->on_click_right = windowCloseClick;
1125 wwin->frame->on_dblclick_right = windowCloseDblClick;
1126 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1127 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1128 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1130 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1131 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1132 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1135 int gx, gy;
1137 wClientGetGravityOffsets(wwin, &gx, &gy);
1139 /* if gravity is to the south, account for the border sizes */
1140 if (gy > 0)
1141 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1145 * wWindowConfigure() will init the client window's size
1146 * (wwin->client.{width,height}) and all other geometry
1147 * related variables (frame_x,frame_y) */
1148 wWindowConfigure(wwin, x, y, width, height);
1150 /* to make sure the window receives it's new position after reparenting */
1151 wWindowSynthConfigureNotify(wwin);
1153 /* Setup descriptors and save window to internal lists */
1154 if (wwin->main_window != None) {
1155 WApplication *app;
1156 WWindow *leader;
1158 /* Leader windows do not necessary set themselves as leaders.
1159 * If this is the case, point the leader of this window to
1160 * itself */
1161 leader = wWindowFor(wwin->main_window);
1162 if (leader && leader->main_window == None)
1163 leader->main_window = leader->client_win;
1165 app = wApplicationCreate(wwin);
1166 if (app) {
1167 app->last_workspace = workspace;
1169 /* Do application specific stuff, like setting application
1170 * wide attributes. */
1172 if (wwin->flags.hidden) {
1173 /* if the window was set to hidden because it was hidden
1174 * in a previous incarnation and that state was restored */
1175 app->flags.hidden = 1;
1176 } else if (app->flags.hidden) {
1177 if (WFLAGP(app->main_window_desc, start_hidden)) {
1178 wwin->flags.hidden = 1;
1179 } else {
1180 wUnhideApplication(app, False, False);
1181 raise = True;
1184 wAppBounce(app);
1188 /* setup the frame descriptor */
1189 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1190 wwin->frame->core->descriptor.parent = wwin;
1191 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1193 /* don't let windows go away if we die */
1194 XAddToSaveSet(dpy, window);
1196 XLowerWindow(dpy, window);
1198 /* if window is in this workspace and should be mapped, then map it */
1199 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1200 && !wwin->flags.hidden && !withdraw) {
1202 /* The following "if" is to avoid crashing of clients that expect
1203 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1204 * after the return from this function. */
1205 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1206 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1207 else
1208 wClientSetState(wwin, NormalState, None);
1210 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1211 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1213 * The brain damaged idiotic non-click to focus modes will
1214 * have trouble with this because:
1216 * 1. window is created and mapped by the client
1217 * 2. window is mapped by wmaker in small size
1218 * 3. window is animated to grow to normal size
1219 * 4. this function returns to normal event loop
1220 * 5. eventually, the EnterNotify event that would trigger
1221 * the window focusing (if the mouse is over that window)
1222 * will be processed by wmaker.
1223 * But since this event will be rather delayed
1224 * (step 3 has a large delay) the time when the event ocurred
1225 * and when it is processed, the client that owns that window
1226 * will reject the XSetInputFocus() for it.
1228 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1229 DoWindowBirth(wwin);
1231 wWindowMap(wwin);
1234 /* setup stacking descriptor */
1235 if (transientOwner)
1236 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1237 else
1238 wwin->frame->core->stacking->child_of = NULL;
1240 if (!scr->focused_window) {
1241 /* first window on the list */
1242 wwin->next = NULL;
1243 wwin->prev = NULL;
1244 scr->focused_window = wwin;
1245 } else {
1246 WWindow *tmp;
1248 /* add window at beginning of focus window list */
1249 tmp = scr->focused_window;
1250 while (tmp->prev)
1251 tmp = tmp->prev;
1253 tmp->prev = wwin;
1254 wwin->next = tmp;
1255 wwin->prev = NULL;
1258 /* raise is set to true if we un-hid the app when this window was born.
1259 * we raise, else old windows of this app will be above this new one. */
1260 if (raise)
1261 wRaiseFrame(wwin->frame->core);
1263 /* Update name must come after WApplication stuff is done */
1264 wWindowUpdateName(wwin, title);
1265 if (title)
1266 XFree(title);
1268 XUngrabServer(dpy);
1270 /* Final preparations before window is ready to go */
1271 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1273 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1274 if (((transientOwner && transientOwner->flags.focused)
1275 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1277 /* only auto_focus if on same screen as mouse
1278 * (and same head for xinerama mode)
1279 * TODO: make it an option */
1281 /*TODO add checking the head of the window, is it available? */
1282 short same_screen = 0, same_head = 1;
1284 int foo;
1285 unsigned int bar;
1286 Window dummy;
1288 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1289 &foo, &foo, &foo, &foo, &bar) != False)
1290 same_screen = 1;
1292 if (same_screen == 1 && same_head == 1)
1293 wSetFocusTo(scr, wwin);
1296 wWindowResetMouseGrabs(wwin);
1298 if (!WFLAGP(wwin, no_bind_keys))
1299 wWindowSetKeyGrabs(wwin);
1301 WMPostNotificationName(WMNManaged, wwin, NULL);
1302 wColormapInstallForWindow(scr, scr->cmap_window);
1304 /* Setup Notification Observers */
1305 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1307 /* Cleanup temporary stuff */
1308 if (win_state)
1309 wWindowDeleteSavedState(win_state);
1311 /* If the window must be withdrawed, then do it now.
1312 * Must do some optimization, 'though */
1313 if (withdraw) {
1314 wwin->flags.mapped = 0;
1315 wClientSetState(wwin, WithdrawnState, None);
1316 wUnmanageWindow(wwin, True, False);
1317 wwin = NULL;
1320 return wwin;
1323 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1324 char *title, int x, int y, int width, int height)
1326 WWindow *wwin;
1327 int foo;
1329 wwin = wWindowCreate();
1331 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1333 wwin->flags.internal_window = 1;
1335 WSETUFLAG(wwin, omnipresent, 1);
1336 WSETUFLAG(wwin, no_shadeable, 1);
1337 WSETUFLAG(wwin, no_resizable, 1);
1338 WSETUFLAG(wwin, no_miniaturizable, 1);
1340 wwin->focus_mode = WFM_PASSIVE;
1341 wwin->client_win = window;
1342 wwin->screen_ptr = scr;
1343 wwin->transient_for = owner;
1344 wwin->client.x = x;
1345 wwin->client.y = y;
1346 wwin->client.width = width;
1347 wwin->client.height = height;
1348 wwin->frame_x = wwin->client.x;
1349 wwin->frame_y = wwin->client.y;
1351 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1352 foo |= WFF_TITLEBAR;
1353 #ifdef XKB_BUTTON_HINT
1354 foo |= WFF_LANGUAGE_BUTTON;
1355 #endif
1357 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1358 wwin->frame_x, wwin->frame_y,
1359 width, height,
1360 &wPreferences.window_title_clearance,
1361 &wPreferences.window_title_min_height,
1362 &wPreferences.window_title_max_height,
1363 foo,
1364 scr->window_title_texture,
1365 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1366 scr->w_depth, scr->w_visual, scr->w_colormap);
1368 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1370 wwin->frame->flags.is_client_window_frame = 1;
1371 wwin->frame->flags.justification = wPreferences.title_justification;
1373 wFrameWindowChangeTitle(wwin->frame, title);
1375 /* setup button images */
1376 wWindowUpdateButtonImages(wwin);
1378 /* hide buttons */
1379 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1381 wwin->frame->child = wwin;
1382 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1384 #ifdef XKB_BUTTON_HINT
1385 if (wPreferences.modelock)
1386 wwin->frame->on_click_language = windowLanguageClick;
1387 #endif
1389 wwin->frame->on_click_right = windowCloseClick;
1390 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1391 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1392 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1393 wwin->client.y += wwin->frame->top_width;
1395 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1396 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1398 /* setup the frame descriptor */
1399 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1400 wwin->frame->core->descriptor.parent = wwin;
1401 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1403 XLowerWindow(dpy, window);
1404 XMapSubwindows(dpy, wwin->frame->core->window);
1406 /* setup stacking descriptor */
1407 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1408 WWindow *tmp;
1409 tmp = wWindowFor(wwin->transient_for);
1410 if (tmp)
1411 wwin->frame->core->stacking->child_of = tmp->frame->core;
1412 } else {
1413 wwin->frame->core->stacking->child_of = NULL;
1416 if (!scr->focused_window) {
1417 /* first window on the list */
1418 wwin->next = NULL;
1419 wwin->prev = NULL;
1420 scr->focused_window = wwin;
1421 } else {
1422 WWindow *tmp;
1424 /* add window at beginning of focus window list */
1425 tmp = scr->focused_window;
1426 while (tmp->prev)
1427 tmp = tmp->prev;
1428 tmp->prev = wwin;
1429 wwin->next = tmp;
1430 wwin->prev = NULL;
1433 if (wwin->flags.is_gnustep == 0)
1434 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1436 /* if (wPreferences.auto_focus) */
1437 wSetFocusTo(scr, wwin);
1438 wWindowResetMouseGrabs(wwin);
1439 wWindowSetKeyGrabs(wwin);
1441 return wwin;
1445 *----------------------------------------------------------------------
1446 * wUnmanageWindow--
1447 * Removes the frame window from a window and destroys all data
1448 * related to it. The window will be reparented back to the root window
1449 * if restore is True.
1451 * Side effects:
1452 * Everything related to the window is destroyed and the window
1453 * is removed from the window lists. Focus is set to the previous on the
1454 * window list.
1455 *----------------------------------------------------------------------
1457 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1459 WCoreWindow *frame = wwin->frame->core;
1460 WWindow *owner = NULL;
1461 WWindow *newFocusedWindow = NULL;
1462 int wasFocused;
1463 WScreen *scr = wwin->screen_ptr;
1465 /* First close attribute editor window if open */
1466 if (wwin->flags.inspector_open)
1467 wCloseInspectorForWindow(wwin);
1469 /* Close window menu if it's open for this window */
1470 if (wwin->flags.menu_open_for_me)
1471 CloseWindowMenu(scr);
1473 if (!destroyed) {
1474 if (!wwin->flags.internal_window)
1475 XRemoveFromSaveSet(dpy, wwin->client_win);
1477 /* If this is a leader window, we still need to listen for
1478 * DestroyNotify and PropertyNotify. */
1479 if (wApplicationOf(wwin->client_win))
1480 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1481 else
1482 XSelectInput(dpy, wwin->client_win, NoEventMask);
1484 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1485 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1488 XUnmapWindow(dpy, frame->window);
1490 XUnmapWindow(dpy, wwin->client_win);
1492 /* deselect window */
1493 wSelectWindow(wwin, False);
1495 /* remove all pending events on window */
1496 /* I think this only matters for autoraise */
1497 if (wPreferences.raise_delay)
1498 WMDeleteTimerWithClientData(wwin->frame->core);
1500 XFlush(dpy);
1502 /* reparent the window back to the root */
1503 if (restore)
1504 wClientRestore(wwin);
1506 if (wwin->transient_for != scr->root_win) {
1507 owner = wWindowFor(wwin->transient_for);
1508 if (owner) {
1509 if (!owner->flags.semi_focused)
1510 owner = NULL;
1511 else
1512 owner->flags.semi_focused = 0;
1516 wasFocused = wwin->flags.focused;
1518 /* remove from window focus list */
1519 if (!wwin->prev && !wwin->next) {
1520 /* was the only window */
1521 scr->focused_window = NULL;
1522 newFocusedWindow = NULL;
1523 } else {
1524 WWindow *tmp;
1526 if (wwin->prev)
1527 wwin->prev->next = wwin->next;
1529 if (wwin->next)
1530 wwin->next->prev = wwin->prev;
1531 else {
1532 scr->focused_window = wwin->prev;
1533 scr->focused_window->next = NULL;
1536 if (wPreferences.focus_mode == WKF_CLICK) {
1538 /* if in click to focus mode and the window
1539 * was a transient, focus the owner window
1541 tmp = NULL;
1542 if (wPreferences.focus_mode == WKF_CLICK) {
1543 tmp = wWindowFor(wwin->transient_for);
1544 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1545 tmp = NULL;
1548 /* otherwise, focus the next one in the focus list */
1549 if (!tmp) {
1550 tmp = scr->focused_window;
1551 while (tmp) { /* look for one in the window list first */
1552 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1553 && (tmp->flags.mapped || tmp->flags.shaded))
1554 break;
1555 tmp = tmp->prev;
1557 if (!tmp) { /* if unsuccessful, choose any focusable window */
1558 tmp = scr->focused_window;
1559 while (tmp) {
1560 if (!WFLAGP(tmp, no_focusable)
1561 && (tmp->flags.mapped || tmp->flags.shaded))
1562 break;
1563 tmp = tmp->prev;
1568 newFocusedWindow = tmp;
1570 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1571 unsigned int mask;
1572 int foo;
1573 Window bar, win;
1575 /* This is to let the root window get the keyboard input
1576 * if Sloppy focus mode and no other window get focus.
1577 * This way keybindings will not freeze.
1579 tmp = NULL;
1580 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1581 tmp = wWindowFor(win);
1582 if (tmp == wwin)
1583 tmp = NULL;
1584 newFocusedWindow = tmp;
1585 } else {
1586 newFocusedWindow = NULL;
1590 if (!wwin->flags.internal_window)
1591 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1592 if (wasFocused) {
1593 if (newFocusedWindow != owner && owner) {
1594 if (wwin->flags.is_gnustep == 0)
1595 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1597 wSetFocusTo(scr, newFocusedWindow);
1600 /* Close menu and unhighlight */
1601 WApplication *oapp = wApplicationOf(wwin->main_window);
1602 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1603 if (oapp && oapp != napp) {
1604 wAppMenuUnmap(oapp->menu);
1605 if (wPreferences.highlight_active_app)
1606 wApplicationDeactivate(oapp);
1609 wNETCleanupFrameExtents(wwin);
1611 wWindowDestroy(wwin);
1612 XFlush(dpy);
1615 void wWindowMap(WWindow *wwin)
1617 XMapWindow(dpy, wwin->frame->core->window);
1618 if (!wwin->flags.shaded) {
1619 /* window will be remapped when getting MapNotify */
1620 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1621 XMapWindow(dpy, wwin->client_win);
1622 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1624 wwin->flags.mapped = 1;
1628 void wWindowUnmap(WWindow *wwin)
1630 wwin->flags.mapped = 0;
1632 /* prevent window withdrawal when getting UnmapNotify */
1633 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1634 XUnmapWindow(dpy, wwin->client_win);
1635 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1637 XUnmapWindow(dpy, wwin->frame->core->window);
1640 void wWindowFocus(WWindow *wwin, WWindow *owin)
1642 WWindow *nowner;
1643 WWindow *oowner;
1645 #ifdef KEEP_XKB_LOCK_STATUS
1646 if (wPreferences.modelock)
1647 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1648 #endif /* KEEP_XKB_LOCK_STATUS */
1650 wwin->flags.semi_focused = 0;
1652 if (wwin->flags.is_gnustep == 0)
1653 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1655 wwin->flags.focused = 1;
1657 wWindowResetMouseGrabs(wwin);
1659 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1661 if (owin == wwin || !owin)
1662 return;
1664 nowner = wWindowFor(wwin->transient_for);
1666 /* new window is a transient for the old window */
1667 if (nowner == owin) {
1668 owin->flags.semi_focused = 1;
1669 wWindowUnfocus(nowner);
1670 return;
1673 oowner = wWindowFor(owin->transient_for);
1675 /* new window is owner of old window */
1676 if (wwin == oowner) {
1677 wWindowUnfocus(owin);
1678 return;
1681 if (!nowner) {
1682 wWindowUnfocus(owin);
1683 return;
1686 /* new window has same owner of old window */
1687 if (oowner == nowner) {
1688 /* prevent unfocusing of owner */
1689 oowner->flags.semi_focused = 0;
1690 wWindowUnfocus(owin);
1691 oowner->flags.semi_focused = 1;
1693 return;
1696 /* nowner != NULL && oowner != nowner */
1697 nowner->flags.semi_focused = 1;
1698 wWindowUnfocus(nowner);
1699 wWindowUnfocus(owin);
1702 void wWindowUnfocus(WWindow *wwin)
1704 CloseWindowMenu(wwin->screen_ptr);
1706 if (wwin->flags.is_gnustep == 0)
1707 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1709 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1710 WWindow *owner;
1711 owner = wWindowFor(wwin->transient_for);
1712 if (owner && owner->flags.semi_focused) {
1713 owner->flags.semi_focused = 0;
1714 if (owner->flags.mapped || owner->flags.shaded) {
1715 wWindowUnfocus(owner);
1716 wFrameWindowPaint(owner->frame);
1720 wwin->flags.focused = 0;
1721 wWindowResetMouseGrabs(wwin);
1722 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1725 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1727 char *title;
1729 if (!wwin->frame)
1730 return;
1732 if (!newTitle)
1733 title = DEF_WINDOW_TITLE; /* the hint was removed */
1734 else
1735 title = newTitle;
1737 if (wFrameWindowChangeTitle(wwin->frame, title))
1738 WMPostNotificationName(WMNChangedName, wwin, NULL);
1742 *----------------------------------------------------------------------
1744 * wWindowConstrainSize--
1745 * Constrains size for the client window, taking the maximal size,
1746 * window resize increments and other size hints into account.
1748 * Returns:
1749 * The closest size to what was given that the client window can
1750 * have.
1752 *----------------------------------------------------------------------
1754 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1756 int width = (int)*nwidth;
1757 int height = (int)*nheight;
1758 int winc = 1;
1759 int hinc = 1;
1760 int minW = 1, minH = 1;
1761 int maxW = wwin->screen_ptr->scr_width * 2;
1762 int maxH = wwin->screen_ptr->scr_height * 2;
1763 int minAX = -1, minAY = -1;
1764 int maxAX = -1, maxAY = -1;
1765 int baseW = 0;
1766 int baseH = 0;
1768 if (wwin->normal_hints) {
1769 winc = wwin->normal_hints->width_inc;
1770 hinc = wwin->normal_hints->height_inc;
1771 minW = wwin->normal_hints->min_width;
1772 minH = wwin->normal_hints->min_height;
1773 maxW = wwin->normal_hints->max_width;
1774 maxH = wwin->normal_hints->max_height;
1775 if (wwin->normal_hints->flags & PAspect) {
1776 minAX = wwin->normal_hints->min_aspect.x;
1777 minAY = wwin->normal_hints->min_aspect.y;
1778 maxAX = wwin->normal_hints->max_aspect.x;
1779 maxAY = wwin->normal_hints->max_aspect.y;
1782 baseW = wwin->normal_hints->base_width;
1783 baseH = wwin->normal_hints->base_height;
1786 if (width < minW)
1787 width = minW;
1788 if (height < minH)
1789 height = minH;
1791 if (width > maxW)
1792 width = maxW;
1793 if (height > maxH)
1794 height = maxH;
1796 /* aspect ratio code borrowed from olwm */
1797 if (minAX > 0) {
1798 /* adjust max aspect ratio */
1799 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1800 if (maxAX > maxAY) {
1801 height = (width * maxAY) / maxAX;
1802 if (height > maxH) {
1803 height = maxH;
1804 width = (height * maxAX) / maxAY;
1806 } else {
1807 width = (height * maxAX) / maxAY;
1808 if (width > maxW) {
1809 width = maxW;
1810 height = (width * maxAY) / maxAX;
1815 /* adjust min aspect ratio */
1816 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1817 if (minAX > minAY) {
1818 height = (width * minAY) / minAX;
1819 if (height < minH) {
1820 height = minH;
1821 width = (height * minAX) / minAY;
1823 } else {
1824 width = (height * minAX) / minAY;
1825 if (width < minW) {
1826 width = minW;
1827 height = (width * minAY) / minAX;
1833 if (baseW != 0)
1834 width = (((width - baseW) / winc) * winc) + baseW;
1835 else
1836 width = (((width - minW) / winc) * winc) + minW;
1838 if (baseH != 0)
1839 height = (((height - baseH) / hinc) * hinc) + baseH;
1840 else
1841 height = (((height - minH) / hinc) * hinc) + minH;
1843 /* broken stupid apps may cause preposterous values for these.. */
1844 if (width > 0)
1845 *nwidth = width;
1846 if (height > 0)
1847 *nheight = height;
1850 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1851 unsigned int *width, unsigned int *height)
1853 int baseW = 0, baseH = 0;
1854 int winc = 1, hinc = 1;
1856 if (wwin->normal_hints) {
1857 baseW = wwin->normal_hints->base_width;
1858 baseH = wwin->normal_hints->base_height;
1860 winc = wwin->normal_hints->width_inc;
1861 hinc = wwin->normal_hints->height_inc;
1864 if (*width > maxW)
1865 *width = maxW - (maxW - baseW) % winc;
1867 if (*height > maxH)
1868 *height = maxH - (maxH - baseH) % hinc;
1871 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1873 WScreen *scr = wwin->screen_ptr;
1874 WApplication *wapp;
1875 int unmap = 0;
1877 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1878 return;
1880 if (workspace != scr->current_workspace) {
1881 /* Sent to other workspace. Unmap window */
1882 if ((wwin->flags.mapped
1883 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1884 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1886 wapp = wApplicationOf(wwin->main_window);
1887 if (wapp)
1888 wapp->last_workspace = workspace;
1890 if (wwin->flags.miniaturized) {
1891 if (wwin->icon) {
1892 XUnmapWindow(dpy, wwin->icon->core->window);
1893 wwin->icon->mapped = 0;
1895 } else {
1896 unmap = 1;
1897 wSetFocusTo(scr, NULL);
1900 } else {
1901 /* brought to current workspace. Map window */
1902 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1903 if (wwin->icon) {
1904 XMapWindow(dpy, wwin->icon->core->window);
1905 wwin->icon->mapped = 1;
1907 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1908 wWindowMap(wwin);
1911 if (!IS_OMNIPRESENT(wwin)) {
1912 int oldWorkspace = wwin->frame->workspace;
1913 wwin->frame->workspace = workspace;
1914 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1917 if (unmap)
1918 wWindowUnmap(wwin);
1921 void wWindowChangeWorkspaceRelative(WWindow *wwin, int amount)
1923 WScreen *scr = wwin->screen_ptr;
1924 int w = scr->current_workspace + amount;
1926 if (amount < 0) {
1927 if (w >= 0) {
1928 wWindowChangeWorkspace(wwin, w);
1929 } else if (wPreferences.ws_cycle) {
1930 wWindowChangeWorkspace(wwin, scr->workspace_count + w);
1932 } else if (amount > 0) {
1933 if (w < scr->workspace_count) {
1934 wWindowChangeWorkspace(wwin, w);
1935 } else if (wPreferences.ws_advance) {
1936 int workspace = WMIN(w, MAX_WORKSPACES - 1);
1937 wWorkspaceMake(scr, workspace);
1938 wWindowChangeWorkspace(wwin, workspace);
1939 } else if (wPreferences.ws_cycle) {
1940 wWindowChangeWorkspace(wwin, w % scr->workspace_count);
1945 void wWindowSynthConfigureNotify(WWindow *wwin)
1947 XEvent sevent;
1949 sevent.type = ConfigureNotify;
1950 sevent.xconfigure.display = dpy;
1951 sevent.xconfigure.event = wwin->client_win;
1952 sevent.xconfigure.window = wwin->client_win;
1954 sevent.xconfigure.x = wwin->client.x;
1955 sevent.xconfigure.y = wwin->client.y;
1956 sevent.xconfigure.width = wwin->client.width;
1957 sevent.xconfigure.height = wwin->client.height;
1959 sevent.xconfigure.border_width = wwin->old_border_width;
1960 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1961 sevent.xconfigure.above = wwin->frame->titlebar->window;
1962 else
1963 sevent.xconfigure.above = None;
1965 sevent.xconfigure.override_redirect = False;
1966 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1967 XFlush(dpy);
1971 *----------------------------------------------------------------------
1972 * wWindowConfigure()
1974 * req_x, req_y: new requested positions for the frame
1975 * req_width, req_height: new requested sizes for the client
1977 * Configures the frame, decorations and client window to the specified
1978 * geometry, whose validity is not checked -- wWindowConstrainSize()
1979 * must be used for that.
1980 * The size parameters are for the client window, but the position is
1981 * for the frame.
1982 * The client window receives a ConfigureNotify event, according
1983 * to what ICCCM says.
1985 * Returns:
1986 * None
1988 * Side effects:
1989 * Window size and position are changed and client window receives
1990 * a ConfigureNotify event.
1991 *----------------------------------------------------------------------
1993 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
1995 int synth_notify = False;
1996 int resize;
1998 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2000 * if the window is being moved but not resized then
2001 * send a synthetic ConfigureNotify
2003 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2004 synth_notify = True;
2006 if (WFLAGP(wwin, dont_move_off))
2007 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2009 if (resize) {
2010 if (req_width < MIN_WINDOW_SIZE)
2011 req_width = MIN_WINDOW_SIZE;
2012 if (req_height < MIN_WINDOW_SIZE)
2013 req_height = MIN_WINDOW_SIZE;
2015 /* If growing, resize inner part before frame,
2016 * if shrinking, resize frame before.
2017 * This will prevent the frame (that can have a different color)
2018 * to be exposed, causing flicker */
2019 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2020 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2022 if (wwin->flags.shaded) {
2023 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2024 wwin->old_geometry.height = req_height;
2025 } else {
2026 int h;
2028 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2030 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2033 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2034 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2036 wwin->client.x = req_x;
2037 wwin->client.y = req_y + wwin->frame->top_width;
2038 wwin->client.width = req_width;
2039 wwin->client.height = req_height;
2040 } else {
2041 wwin->client.x = req_x;
2042 wwin->client.y = req_y + wwin->frame->top_width;
2044 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2046 wwin->frame_x = req_x;
2047 wwin->frame_y = req_y;
2048 if (HAS_BORDER(wwin)) {
2049 wwin->client.x += wwin->screen_ptr->frame_border_width;
2050 wwin->client.y += wwin->screen_ptr->frame_border_width;
2052 #ifdef SHAPE
2053 if (wShapeSupported && wwin->flags.shaped && resize)
2054 wWindowSetShape(wwin);
2055 #endif
2057 if (synth_notify)
2058 wWindowSynthConfigureNotify(wwin);
2060 wNETFrameExtents(wwin);
2062 XFlush(dpy);
2065 /* req_x, req_y: new position of the frame */
2066 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2068 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2069 int synth_notify = False;
2071 /* Send a synthetic ConfigureNotify event for every window movement. */
2072 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2073 synth_notify = True;
2074 #else
2075 /* A single synthetic ConfigureNotify event is sent at the end of
2076 * a completed (opaque) movement in moveres.c */
2077 #endif
2079 if (WFLAGP(wwin, dont_move_off))
2080 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2081 wwin->frame->core->width, wwin->frame->core->height);
2083 wwin->client.x = req_x;
2084 wwin->client.y = req_y + wwin->frame->top_width;
2085 if (HAS_BORDER(wwin)) {
2086 wwin->client.x += wwin->screen_ptr->frame_border_width;
2087 wwin->client.y += wwin->screen_ptr->frame_border_width;
2090 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2092 wwin->frame_x = req_x;
2093 wwin->frame_y = req_y;
2095 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2096 if (synth_notify)
2097 wWindowSynthConfigureNotify(wwin);
2098 #endif
2101 void wWindowUpdateButtonImages(WWindow *wwin)
2103 WScreen *scr = wwin->screen_ptr;
2104 Pixmap pixmap, mask;
2105 WFrameWindow *fwin = wwin->frame;
2107 if (!HAS_TITLEBAR(wwin))
2108 return;
2110 /* miniaturize button */
2111 if (!WFLAGP(wwin, no_miniaturize_button)) {
2112 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2113 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2115 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2116 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2117 else
2118 mask = None;
2120 if (fwin->lbutton_image
2121 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2122 wPixmapDestroy(fwin->lbutton_image);
2123 fwin->lbutton_image = NULL;
2126 if (!fwin->lbutton_image) {
2127 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2128 fwin->lbutton_image->client_owned = 1;
2129 fwin->lbutton_image->client_owned_mask = 1;
2131 } else {
2132 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2133 wPixmapDestroy(fwin->lbutton_image);
2135 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2138 #ifdef XKB_BUTTON_HINT
2139 if (!WFLAGP(wwin, no_language_button)) {
2140 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2141 wPixmapDestroy(fwin->languagebutton_image);
2143 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2145 #endif
2147 /* close button */
2149 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2150 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2152 if (!WFLAGP(wwin, no_close_button)) {
2153 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2154 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2156 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2157 mask = wwin->wm_gnustep_attr->close_mask;
2158 else
2159 mask = None;
2161 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2162 || fwin->rbutton_image->mask != mask)) {
2163 wPixmapDestroy(fwin->rbutton_image);
2164 fwin->rbutton_image = NULL;
2167 if (!fwin->rbutton_image) {
2168 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2169 fwin->rbutton_image->client_owned = 1;
2170 fwin->rbutton_image->client_owned_mask = 1;
2173 } else if (WFLAGP(wwin, kill_close)) {
2174 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2175 wPixmapDestroy(fwin->rbutton_image);
2177 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2179 } else if (MGFLAGP(wwin, broken_close)) {
2180 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2181 wPixmapDestroy(fwin->rbutton_image);
2183 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2185 } else {
2186 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2187 wPixmapDestroy(fwin->rbutton_image);
2189 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2193 /* force buttons to be redrawn */
2194 fwin->flags.need_texture_change = 1;
2195 wFrameWindowPaint(fwin);
2199 *---------------------------------------------------------------------------
2200 * wWindowConfigureBorders--
2201 * Update window border configuration according to attribute flags.
2203 *---------------------------------------------------------------------------
2205 void wWindowConfigureBorders(WWindow *wwin)
2207 if (wwin->frame) {
2208 int flags;
2209 int newy, oldh;
2211 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2213 #ifdef XKB_BUTTON_HINT
2214 if (wPreferences.modelock)
2215 flags |= WFF_LANGUAGE_BUTTON;
2216 #endif
2218 if (HAS_TITLEBAR(wwin))
2219 flags |= WFF_TITLEBAR;
2220 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2221 flags |= WFF_RESIZEBAR;
2222 if (HAS_BORDER(wwin))
2223 flags |= WFF_BORDER;
2224 if (wwin->flags.shaded)
2225 flags |= WFF_IS_SHADED;
2226 if (wwin->flags.selected)
2227 flags |= WFF_SELECTED;
2229 oldh = wwin->frame->top_width;
2230 wFrameWindowUpdateBorders(wwin->frame, flags);
2231 if (oldh != wwin->frame->top_width) {
2232 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2234 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2235 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2238 flags = 0;
2239 if (!WFLAGP(wwin, no_miniaturize_button)
2240 && wwin->frame->flags.hide_left_button)
2241 flags |= WFF_LEFT_BUTTON;
2243 #ifdef XKB_BUTTON_HINT
2244 if (!WFLAGP(wwin, no_language_button)
2245 && wwin->frame->flags.hide_language_button)
2246 flags |= WFF_LANGUAGE_BUTTON;
2247 #endif
2249 if (!WFLAGP(wwin, no_close_button)
2250 && wwin->frame->flags.hide_right_button)
2251 flags |= WFF_RIGHT_BUTTON;
2253 if (flags != 0) {
2254 wWindowUpdateButtonImages(wwin);
2255 wFrameWindowShowButton(wwin->frame, flags);
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 wFrameWindowHideButton(wwin->frame, flags);
2276 #ifdef SHAPE
2277 if (wShapeSupported && wwin->flags.shaped)
2278 wWindowSetShape(wwin);
2279 #endif
2283 void wWindowSaveState(WWindow * wwin)
2285 long data[10];
2286 int i;
2288 memset(data, 0, sizeof(long) * 10);
2289 data[0] = wwin->frame->workspace;
2290 data[1] = wwin->flags.miniaturized;
2291 data[2] = wwin->flags.shaded;
2292 data[3] = wwin->flags.hidden;
2293 data[4] = wwin->flags.maximized;
2294 if (wwin->flags.maximized == 0) {
2295 data[5] = wwin->frame_x;
2296 data[6] = wwin->frame_y;
2297 data[7] = wwin->frame->core->width;
2298 data[8] = wwin->frame->core->height;
2299 } else {
2300 data[5] = wwin->old_geometry.x;
2301 data[6] = wwin->old_geometry.y;
2302 data[7] = wwin->old_geometry.width;
2303 data[8] = wwin->old_geometry.height;
2306 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2307 if (wwin->screen_ptr->shortcutWindows[i] &&
2308 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2309 data[9] |= 1 << i;
2311 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2312 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2315 static int getSavedState(Window window, WSavedState ** state)
2317 Atom type_ret;
2318 int fmt_ret;
2319 unsigned long nitems_ret;
2320 unsigned long bytes_after_ret;
2321 long *data;
2323 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2324 True, _XA_WINDOWMAKER_STATE,
2325 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2326 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2327 return 0;
2329 *state = wmalloc(sizeof(WSavedState));
2331 (*state)->workspace = data[0];
2332 (*state)->miniaturized = data[1];
2333 (*state)->shaded = data[2];
2334 (*state)->hidden = data[3];
2335 (*state)->maximized = data[4];
2336 (*state)->x = data[5];
2337 (*state)->y = data[6];
2338 (*state)->w = data[7];
2339 (*state)->h = data[8];
2340 (*state)->window_shortcuts = data[9];
2342 XFree(data);
2344 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2345 return 1;
2346 else
2347 return 0;
2350 #ifdef SHAPE
2351 void wWindowClearShape(WWindow * wwin)
2353 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2354 0, wwin->frame->top_width, None, ShapeSet);
2355 XFlush(dpy);
2358 void wWindowSetShape(WWindow * wwin)
2360 XRectangle rect[2];
2361 int count;
2362 #ifdef OPTIMIZE_SHAPE
2363 XRectangle *rects;
2364 XRectangle *urec;
2365 int ordering;
2367 /* only shape is the client's */
2368 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2369 goto alt_code;
2371 /* Get array of rectangles describing the shape mask */
2372 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2373 if (!rects)
2374 goto alt_code;
2376 urec = malloc(sizeof(XRectangle) * (count + 2));
2377 if (!urec) {
2378 XFree(rects);
2379 goto alt_code;
2382 /* insert our decoration rectangles in the rect list */
2383 memcpy(urec, rects, sizeof(XRectangle) * count);
2384 XFree(rects);
2386 if (HAS_TITLEBAR(wwin)) {
2387 urec[count].x = -1;
2388 urec[count].y = -1 - wwin->frame->top_width;
2389 urec[count].width = wwin->frame->core->width + 2;
2390 urec[count].height = wwin->frame->top_width + 1;
2391 count++;
2393 if (HAS_RESIZEBAR(wwin)) {
2394 urec[count].x = -1;
2395 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2396 urec[count].width = wwin->frame->core->width + 2;
2397 urec[count].height = wwin->frame->bottom_width + 1;
2398 count++;
2401 /* shape our frame window */
2402 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2403 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2404 XFlush(dpy);
2405 wfree(urec);
2406 return;
2408 alt_code:
2409 #endif /* OPTIMIZE_SHAPE */
2410 count = 0;
2411 if (HAS_TITLEBAR(wwin)) {
2412 rect[count].x = -1;
2413 rect[count].y = -1;
2414 rect[count].width = wwin->frame->core->width + 2;
2415 rect[count].height = wwin->frame->top_width + 1;
2416 count++;
2418 if (HAS_RESIZEBAR(wwin)) {
2419 rect[count].x = -1;
2420 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2421 rect[count].width = wwin->frame->core->width + 2;
2422 rect[count].height = wwin->frame->bottom_width + 1;
2423 count++;
2425 if (count > 0) {
2426 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2427 0, 0, rect, count, ShapeSet, Unsorted);
2429 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2430 0, wwin->frame->top_width, wwin->client_win,
2431 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2432 XFlush(dpy);
2434 #endif /* SHAPE */
2436 /* ====================================================================== */
2438 static FocusMode getFocusMode(WWindow * wwin)
2440 FocusMode mode;
2442 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2443 if (wwin->wm_hints->input == True) {
2444 if (wwin->protocols.TAKE_FOCUS)
2445 mode = WFM_LOCALLY_ACTIVE;
2446 else
2447 mode = WFM_PASSIVE;
2448 } else {
2449 if (wwin->protocols.TAKE_FOCUS)
2450 mode = WFM_GLOBALLY_ACTIVE;
2451 else
2452 mode = WFM_NO_INPUT;
2454 } else {
2455 mode = WFM_PASSIVE;
2457 return mode;
2460 void wWindowSetKeyGrabs(WWindow * wwin)
2462 int i;
2463 WShortKey *key;
2465 for (i = 0; i < WKBD_LAST; i++) {
2466 key = &wKeyBindings[i];
2468 if (key->keycode == 0)
2469 continue;
2470 if (key->modifier != AnyModifier) {
2471 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2472 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2473 #ifdef NUMLOCK_HACK
2474 /* Also grab all modifier combinations possible that include,
2475 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2476 * work even if the NumLock/ScrollLock key is on.
2478 wHackedGrabKey(key->keycode, key->modifier,
2479 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2480 #endif
2482 XGrabKey(dpy, key->keycode, key->modifier,
2483 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2486 wRootMenuBindShortcuts(wwin->frame->core->window);
2489 void wWindowResetMouseGrabs(WWindow * wwin)
2491 /* Mouse grabs can't be done on the client window because of
2492 * ICCCM and because clients that try to do the same will crash.
2494 * But there is a problem wich makes tbar buttons of unfocused
2495 * windows not usable as the click goes to the frame window instead
2496 * of the button itself. Must figure a way to fix that.
2499 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2501 if (!WFLAGP(wwin, no_bind_mouse)) {
2502 /* grabs for Meta+drag */
2503 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2504 True, ButtonPressMask | ButtonReleaseMask,
2505 GrabModeSync, GrabModeAsync, None, None);
2507 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2508 * but we only grab it for Button4 and Button 5 since a lot of apps
2509 * use CTRL+Button1-3 for app related functionality
2511 if (wPreferences.resize_increment > 0) {
2512 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2513 True, ButtonPressMask | ButtonReleaseMask,
2514 GrabModeSync, GrabModeAsync, None, None);
2515 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2516 True, ButtonPressMask | ButtonReleaseMask,
2517 GrabModeSync, GrabModeAsync, None, None);
2519 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2520 True, ButtonPressMask | ButtonReleaseMask,
2521 GrabModeSync, GrabModeAsync, None, None);
2522 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2523 True, ButtonPressMask | ButtonReleaseMask,
2524 GrabModeSync, GrabModeAsync, None, None);
2528 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2529 && !wwin->flags.is_gnustep) {
2530 /* the passive grabs to focus the window */
2531 /* if (wPreferences.focus_mode == WKF_CLICK) */
2532 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2533 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2535 XFlush(dpy);
2538 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2540 if (attr->flags & GSExtraFlagsAttr) {
2541 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2542 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2543 wWindowUpdateButtonImages(wwin);
2548 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2550 WWindowState *wstate;
2552 wstate = malloc(sizeof(WWindowState));
2553 if (!wstate)
2554 return NULL;
2556 memset(wstate, 0, sizeof(WWindowState));
2557 wstate->pid = pid;
2558 if (instance)
2559 wstate->instance = wstrdup(instance);
2560 if (class)
2561 wstate->class = wstrdup(class);
2562 if (command)
2563 wstate->command = wstrdup(command);
2564 wstate->state = state;
2566 wstate->next = windowState;
2567 windowState = wstate;
2569 return wstate;
2572 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2574 WMagicNumber wWindowGetSavedState(Window win)
2576 char *instance, *class, *command = NULL;
2577 WWindowState *wstate = windowState;
2579 if (!wstate)
2580 return NULL;
2582 command = GetCommandForWindow(win);
2583 if (!command)
2584 return NULL;
2586 if (PropGetWMClass(win, &class, &instance)) {
2587 while (wstate) {
2588 if (SAME(instance, wstate->instance) &&
2589 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2590 break;
2592 wstate = wstate->next;
2594 } else {
2595 wstate = NULL;
2598 if (command)
2599 wfree(command);
2600 if (instance)
2601 free(instance);
2602 if (class)
2603 free(class);
2605 return wstate;
2608 void wWindowDeleteSavedState(WMagicNumber id)
2610 WWindowState *tmp, *wstate = (WWindowState *) id;
2612 if (!wstate || !windowState)
2613 return;
2615 tmp = windowState;
2616 if (tmp == wstate) {
2617 windowState = wstate->next;
2618 release_wwindowstate(wstate);
2619 } else {
2620 while (tmp->next) {
2621 if (tmp->next == wstate) {
2622 tmp->next = wstate->next;
2623 release_wwindowstate(wstate);
2624 break;
2626 tmp = tmp->next;
2631 void wWindowDeleteSavedStatesForPID(pid_t pid)
2633 WWindowState *tmp, *wstate;
2635 if (!windowState)
2636 return;
2638 tmp = windowState;
2639 if (tmp->pid == pid) {
2640 wstate = windowState;
2641 windowState = tmp->next;
2643 release_wwindowstate(wstate);
2644 } else {
2645 while (tmp->next) {
2646 if (tmp->next->pid == pid) {
2647 wstate = tmp->next;
2648 tmp->next = wstate->next;
2649 release_wwindowstate(wstate);
2650 break;
2652 tmp = tmp->next;
2657 static void release_wwindowstate(WWindowState *wstate)
2659 if (wstate->instance)
2660 wfree(wstate->instance);
2662 if (wstate->class)
2663 wfree(wstate->class);
2665 if (wstate->command)
2666 wfree(wstate->command);
2668 wfree(wstate->state);
2669 wfree(wstate);
2672 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2674 if (wwin->flags.omnipresent == flag)
2675 return;
2677 wwin->flags.omnipresent = flag;
2678 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2681 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2683 WWindow *wwin = data;
2685 #ifndef NUMLOCK_HACK
2686 if ((event->xbutton.state & ValidModMask)
2687 != (event->xbutton.state & ~LockMask)) {
2688 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2689 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2691 #endif
2693 event->xbutton.state &= ValidModMask;
2695 CloseWindowMenu(wwin->screen_ptr);
2697 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2698 && !WFLAGP(wwin, no_focusable)) {
2699 wSetFocusTo(wwin->screen_ptr, wwin);
2702 if (event->xbutton.button == Button1)
2703 wRaiseFrame(wwin->frame->core);
2705 if (event->xbutton.window != wwin->frame->resizebar->window) {
2706 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2707 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2708 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2709 return;
2713 if (event->xbutton.state & MOD_MASK) {
2714 /* move the window */
2715 wMouseMoveWindow(wwin, event);
2716 XUngrabPointer(dpy, CurrentTime);
2717 } else {
2718 wMouseResizeWindow(wwin, event);
2719 XUngrabPointer(dpy, CurrentTime);
2723 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2725 WWindow *wwin = data;
2727 event->xbutton.state &= ValidModMask;
2729 if (event->xbutton.button == Button1) {
2730 if (event->xbutton.state == 0) {
2731 if (!WFLAGP(wwin, no_shadeable)) {
2732 /* shade window */
2733 if (wwin->flags.shaded)
2734 wUnshadeWindow(wwin);
2735 else
2736 wShadeWindow(wwin);
2738 } else {
2739 int dir = 0;
2741 if (event->xbutton.state & ControlMask)
2742 dir |= MAX_VERTICAL;
2744 if (event->xbutton.state & ShiftMask) {
2745 dir |= MAX_HORIZONTAL;
2746 if (!(event->xbutton.state & ControlMask))
2747 wSelectWindow(wwin, !wwin->flags.selected);
2750 /* maximize window */
2751 if (dir != 0 && IS_RESIZABLE(wwin)) {
2752 int ndir = dir ^ wwin->flags.maximized;
2754 if (ndir != 0)
2755 wMaximizeWindow(wwin, ndir);
2756 else
2757 wUnmaximizeWindow(wwin);
2760 } else if (event->xbutton.button == Button3) {
2761 if (event->xbutton.state & MOD_MASK)
2762 wHideOtherApplications(wwin);
2763 } else if (event->xbutton.button == Button2) {
2764 wSelectWindow(wwin, !wwin->flags.selected);
2765 } else if (event->xbutton.button == W_getconf_mouseWheelUp()) {
2766 wShadeWindow(wwin);
2767 } else if (event->xbutton.button == W_getconf_mouseWheelDown()) {
2768 wUnshadeWindow(wwin);
2772 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2774 WWindow *wwin = desc->parent;
2775 unsigned int new_width, w_scale;
2776 unsigned int new_height, h_scale;
2777 unsigned int resize_width_increment = 0;
2778 unsigned int resize_height_increment = 0;
2780 if (wwin->normal_hints) {
2781 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2782 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2783 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2784 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2786 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2787 resize_width_increment = wPreferences.resize_increment;
2788 resize_height_increment = wPreferences.resize_increment;
2791 event->xbutton.state &= ValidModMask;
2793 CloseWindowMenu(wwin->screen_ptr);
2795 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2796 wSetFocusTo(wwin->screen_ptr, wwin);
2798 if (event->xbutton.button == Button1)
2799 wRaiseFrame(wwin->frame->core);
2801 if (event->xbutton.state & ControlMask) {
2802 if (event->xbutton.button == Button4) {
2803 new_width = wwin->client.width - resize_width_increment;
2804 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2805 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2807 if (event->xbutton.button == Button5) {
2808 new_width = wwin->client.width + resize_width_increment;
2809 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2810 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2814 if (event->xbutton.state & MOD_MASK) {
2815 /* move the window */
2816 if (XGrabPointer(dpy, wwin->client_win, False,
2817 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2818 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2819 return;
2821 if (event->xbutton.button == Button3) {
2822 wMouseResizeWindow(wwin, event);
2823 } else if (event->xbutton.button == Button4) {
2824 new_height = wwin->client.height - resize_height_increment;
2825 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2826 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2827 } else if (event->xbutton.button == Button5) {
2828 new_height = wwin->client.height + resize_height_increment;
2829 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2830 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2831 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2832 wMouseMoveWindow(wwin, event);
2834 XUngrabPointer(dpy, CurrentTime);
2838 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2840 WWindow *wwin = (WWindow *) data;
2842 #ifndef NUMLOCK_HACK
2843 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2844 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2845 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2846 #endif
2847 event->xbutton.state &= ValidModMask;
2849 CloseWindowMenu(wwin->screen_ptr);
2851 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2852 && !WFLAGP(wwin, no_focusable))
2853 wSetFocusTo(wwin->screen_ptr, wwin);
2855 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2857 if (event->xbutton.button == Button1) {
2858 if (event->xbutton.state & MOD_MASK)
2859 wLowerFrame(wwin->frame->core);
2860 else
2861 wRaiseFrame(wwin->frame->core);
2863 if ((event->xbutton.state & ShiftMask)
2864 && !(event->xbutton.state & ControlMask)) {
2865 wSelectWindow(wwin, !wwin->flags.selected);
2866 return;
2868 if (event->xbutton.window != wwin->frame->titlebar->window
2869 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2870 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2871 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2872 return;
2875 /* move the window */
2876 wMouseMoveWindow(wwin, event);
2878 XUngrabPointer(dpy, CurrentTime);
2879 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2880 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2881 WObjDescriptor *desc;
2883 if (event->xbutton.window != wwin->frame->titlebar->window
2884 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2885 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2886 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2887 return;
2890 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2892 /* allow drag select */
2893 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2894 event->xany.send_event = True;
2895 (*desc->handle_mousedown) (desc, event);
2897 XUngrabPointer(dpy, CurrentTime);
2901 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2903 WWindow *wwin = data;
2905 event->xbutton.state &= ValidModMask;
2907 CloseWindowMenu(wwin->screen_ptr);
2909 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2910 return;
2912 /* if control-click, kill the client */
2913 if (event->xbutton.state & ControlMask) {
2914 wClientKill(wwin);
2915 } else {
2916 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2917 /* send delete message */
2918 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2923 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2925 WWindow *wwin = data;
2927 CloseWindowMenu(wwin->screen_ptr);
2929 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2930 return;
2932 /* send delete message */
2933 if (wwin->protocols.DELETE_WINDOW)
2934 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2935 else
2936 wClientKill(wwin);
2939 #ifdef XKB_BUTTON_HINT
2940 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2942 WWindow *wwin = data;
2943 WFrameWindow *fwin = wwin->frame;
2944 WScreen *scr = fwin->screen_ptr;
2945 int tl;
2947 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2948 return;
2949 tl = wwin->frame->languagemode;
2950 wwin->frame->languagemode = wwin->frame->last_languagemode;
2951 wwin->frame->last_languagemode = tl;
2952 wSetFocusTo(scr, wwin);
2953 wwin->frame->languagebutton_image =
2954 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2955 wFrameWindowUpdateLanguageButton(wwin->frame);
2956 if (event->xbutton.button == Button3)
2957 return;
2958 wRaiseFrame(fwin->core);
2960 #endif
2962 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2964 WWindow *wwin = data;
2966 event->xbutton.state &= ValidModMask;
2968 CloseWindowMenu(wwin->screen_ptr);
2970 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2971 return;
2973 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2974 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2975 } else {
2976 WApplication *wapp;
2977 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2979 wapp = wApplicationOf(wwin->main_window);
2980 if (wapp && !WFLAGP(wwin, no_appicon))
2981 wHideApplication(wapp);
2982 } else if (event->xbutton.state == 0) {
2983 wIconifyWindow(wwin);