Code not needed at wWindowSetupInitialAttributes
[wmaker-crm.git] / src / window.c
blobf0f093d7e386069ced2aca8f540d21307395e6a3
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 */
357 #ifdef MWM_HINTS
358 wMWMCheckClientHints(wwin);
359 #endif /* MWM_HINTS */
361 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
363 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
364 * have INT_MIN that means that no window level was requested. -Dan
366 if (tmp_level == INT_MIN) {
367 if (WFLAGP(wwin, floating))
368 *level = WMFloatingLevel;
369 else if (WFLAGP(wwin, sunken))
370 *level = WMSunkenLevel;
371 else
372 *level = WMNormalLevel;
373 } else {
374 *level = tmp_level;
377 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
378 WWindow *transientOwner = wWindowFor(wwin->transient_for);
379 if (transientOwner) {
380 int ownerLevel = transientOwner->frame->core->stacking->window_level;
381 if (ownerLevel > *level)
382 *level = ownerLevel;
386 if (tmp_workspace >= 0)
387 *workspace = tmp_workspace % scr->workspace_count;
391 * Set attributes specified only for that window/class.
392 * This might do duplicate work with the 1st wDefaultFillAttributes().
394 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
395 &wwin->defined_user_flags, False);
397 * Sanity checks for attributes that depend on other attributes
399 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
400 wwin->user_flags.emulate_appicon = 0;
402 if (wwin->main_window != None) {
403 WApplication *wapp = wApplicationOf(wwin->main_window);
404 if (wapp && !wapp->flags.emulated)
405 wwin->user_flags.emulate_appicon = 0;
408 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
409 wwin->user_flags.emulate_appicon = 0;
411 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
412 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
413 wwin->user_flags.sunken = 0;
415 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
417 /* windows that have takefocus=False shouldn't take focus at all */
418 if (wwin->focus_mode == WFM_NO_INPUT)
419 wwin->client_flags.no_focusable = 1;
422 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
424 int w1, h1, w2, h2;
426 w1 = wwin->frame->core->width;
427 h1 = wwin->frame->core->height;
428 w2 = obscured->frame->core->width;
429 h2 = obscured->frame->core->height;
431 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
432 && wwin->frame->workspace != obscured->frame->workspace)
433 return False;
435 if (wwin->frame_x + w1 < obscured->frame_x
436 || wwin->frame_y + h1 < obscured->frame_y
437 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
438 return False;
440 return True;
443 static void fixLeaderProperties(WWindow *wwin)
445 XClassHint *classHint;
446 XWMHints *hints, *clientHints;
447 XWindowAttributes attr;
448 Window leaders[2], window;
449 char **argv, *command;
450 int argc, i, pid;
451 Bool haveCommand;
453 classHint = XAllocClassHint();
454 clientHints = XGetWMHints(dpy, wwin->client_win);
455 pid = wNETWMGetPidForWindow(wwin->client_win);
456 if (pid > 0)
457 haveCommand = GetCommandForPid(pid, &argv, &argc);
458 else
459 haveCommand = False;
461 leaders[0] = wwin->client_leader;
462 leaders[1] = wwin->group_id;
464 if (haveCommand) {
465 command = GetCommandForWindow(wwin->client_win);
466 if (command)
467 wfree(command); /* command already set. nothing to do. */
468 else
469 XSetCommand(dpy, wwin->client_win, argv, argc);
472 for (i = 0; i < 2; i++) {
473 window = leaders[i];
474 if (window) {
475 if (XGetClassHint(dpy, window, classHint) == 0) {
476 classHint->res_name = wwin->wm_instance;
477 classHint->res_class = wwin->wm_class;
478 XSetClassHint(dpy, window, classHint);
480 hints = XGetWMHints(dpy, window);
481 if (hints) {
482 XFree(hints);
483 } else if (clientHints) {
484 /* set window group leader to self */
485 clientHints->window_group = window;
486 clientHints->flags |= WindowGroupHint;
487 XSetWMHints(dpy, window, clientHints);
490 if (haveCommand) {
491 command = GetCommandForWindow(window);
492 if (command)
493 wfree(command); /* command already set. nothing to do. */
494 else
495 XSetCommand(dpy, window, argv, argc);
498 /* Make sure we get notification when this window is destroyed */
499 if (XGetWindowAttributes(dpy, window, &attr))
500 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
504 XFree(classHint);
505 if (clientHints)
506 XFree(clientHints);
507 if (haveCommand)
508 wfree(argv);
511 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
513 XClassHint *classHint;
514 XWMHints *hints;
515 Window leader;
516 int argc;
517 char **argv;
519 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
520 /* set class hint */
521 classHint = XAllocClassHint();
522 classHint->res_name = instance;
523 classHint->res_class = class;
524 XSetClassHint(dpy, leader, classHint);
525 XFree(classHint);
527 /* inherit these from the original leader if available */
528 hints = XGetWMHints(dpy, win);
529 if (!hints) {
530 hints = XAllocWMHints();
531 hints->flags = 0;
533 /* set window group leader to self */
534 hints->window_group = leader;
535 hints->flags |= WindowGroupHint;
536 XSetWMHints(dpy, leader, hints);
537 XFree(hints);
539 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
540 XSetCommand(dpy, leader, argv, argc);
541 XFreeStringList(argv);
544 return leader;
547 static int matchIdentifier(const void *item, const void *cdata)
549 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
553 *----------------------------------------------------------------
554 * wManageWindow--
555 * reparents the window and allocates a descriptor for it.
556 * Window manager hints and other hints are fetched to configure
557 * the window decoration attributes and others. User preferences
558 * for the window are used if available, to configure window
559 * decorations and some behaviour.
560 * If in startup, windows that are override redirect,
561 * unmapped and never were managed and are Withdrawn are not
562 * managed.
564 * Returns:
565 * the new window descriptor
567 * Side effects:
568 * The window is reparented and appropriate notification
569 * is done to the client. Input mask for the window is setup.
570 * The window descriptor is also associated with various window
571 * contexts and inserted in the head of the window list.
572 * Event handler contexts are associated for some objects
573 * (buttons, titlebar and resizebar)
575 *----------------------------------------------------------------
577 WWindow *wManageWindow(WScreen *scr, Window window)
579 WWindow *wwin;
580 int x, y;
581 unsigned width, height;
582 XWindowAttributes wattribs;
583 XSetWindowAttributes attribs;
584 WWindowState *win_state;
585 WWindow *transientOwner = NULL;
586 int window_level;
587 int wm_state;
588 int foo;
589 int workspace = -1;
590 char *title;
591 Bool withdraw = False;
592 Bool raise = False;
594 /* mutex. */
595 XGrabServer(dpy);
596 XSync(dpy, False);
597 /* make sure the window is still there */
598 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
599 XUngrabServer(dpy);
600 return NULL;
603 /* if it's an override-redirect, ignore it */
604 if (wattribs.override_redirect) {
605 XUngrabServer(dpy);
606 return NULL;
609 wm_state = PropGetWindowState(window);
611 /* if it's startup and the window is unmapped, don't manage it */
612 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
613 XUngrabServer(dpy);
614 return NULL;
617 wwin = wWindowCreate();
619 title = wNETWMGetWindowName(window);
620 if (title)
621 wwin->flags.net_has_title = 1;
622 if (!title && !wFetchName(dpy, window, &title))
623 title = NULL;
625 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
627 #ifdef SHAPE
628 if (wShapeSupported) {
629 int junk;
630 unsigned int ujunk;
631 int b_shaped;
633 XShapeSelectInput(dpy, window, ShapeNotifyMask);
634 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
635 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
636 wwin->flags.shaped = b_shaped;
638 #endif
641 * Get hints and other information in properties
643 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
645 /* setup descriptor */
646 wwin->client_win = window;
647 wwin->screen_ptr = scr;
648 wwin->old_border_width = wattribs.border_width;
649 wwin->event_mask = CLIENT_EVENTS;
650 attribs.event_mask = CLIENT_EVENTS;
651 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
652 attribs.save_under = False;
654 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
655 XSetWindowBorderWidth(dpy, window, 0);
657 /* get hints from GNUstep app */
658 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
659 wwin->flags.is_gnustep = 1;
661 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
662 wwin->wm_gnustep_attr = NULL;
664 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
665 wwin->flags.is_dockapp = 1;
666 withdraw = True;
669 wwin->client_leader = PropGetClientLeader(window);
670 if (wwin->client_leader != None)
671 wwin->main_window = wwin->client_leader;
673 wwin->wm_hints = XGetWMHints(dpy, window);
675 if (wwin->wm_hints) {
676 if (wwin->wm_hints->flags & StateHint) {
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->group_id = fPtr->leader; */
786 wwin->main_window = fPtr->leader;
787 wfree(buffer);
788 } else {
789 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
791 fPtr->identifier = buffer;
792 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
793 fPtr->origLeader = None;
794 fPtr->retainCount = 1;
796 WMAddToArray(scr->fakeGroupLeaders, fPtr);
798 if (ADEQUATE(wwin->main_window)) {
799 fPtr->retainCount++;
800 fPtr->origLeader = wwin->main_window;
802 wwin->fake_group = fPtr;
803 /*wwin->group_id = fPtr->leader; */
804 wwin->main_window = fPtr->leader;
806 if (instance)
807 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;
833 /* if there is a saved state (from file), restore it */
834 win_state = NULL;
835 if (wwin->main_window != None)
836 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
837 else
838 win_state = (WWindowState *) wWindowGetSavedState(window);
840 if (win_state && !withdraw) {
841 if (win_state->state->hidden > 0)
842 wwin->flags.hidden = win_state->state->hidden;
844 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
845 wwin->flags.shaded = win_state->state->shaded;
847 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
848 wwin->flags.miniaturized = win_state->state->miniaturized;
850 if (!IS_OMNIPRESENT(wwin)) {
851 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
852 wwin->wm_class);
853 if (w < 0 || w >= scr->workspace_count) {
854 workspace = win_state->state->workspace;
855 if (workspace >= scr->workspace_count)
856 workspace = scr->current_workspace;
857 } else {
858 workspace = w;
860 } else {
861 workspace = scr->current_workspace;
865 /* if we're restarting, restore saved state (from hints).
866 * This will overwrite previous */
868 WSavedState *wstate;
870 if (getSavedState(window, &wstate)) {
871 wwin->flags.shaded = wstate->shaded;
872 wwin->flags.hidden = wstate->hidden;
873 wwin->flags.miniaturized = wstate->miniaturized;
874 wwin->flags.maximized = wstate->maximized;
875 if (wwin->flags.maximized) {
876 wwin->old_geometry.x = wstate->x;
877 wwin->old_geometry.y = wstate->y;
878 wwin->old_geometry.width = wstate->w;
879 wwin->old_geometry.height = wstate->h;
882 workspace = wstate->workspace;
883 } else {
884 wstate = NULL;
887 /* restore window shortcut */
888 if (wstate != NULL || win_state != NULL) {
889 unsigned mask = 0;
891 if (win_state != NULL)
892 mask = win_state->state->window_shortcuts;
894 if (wstate != NULL && mask == 0)
895 mask = wstate->window_shortcuts;
897 if (mask > 0) {
898 int i;
900 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
901 if (mask & (1 << i)) {
902 if (!scr->shortcutWindows[i])
903 scr->shortcutWindows[i] = WMCreateArray(4);
905 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;
945 wWindowConstrainSize(wwin, &width, &height);
947 /* do not ask for window placement if the window is
948 * transient, during startup, if the initial workspace is another one
949 * or if the window wants to start iconic.
950 * If geometry was saved, restore it. */
952 Bool dontBring = False;
954 if (win_state && win_state->state->w > 0) {
955 x = win_state->state->x;
956 y = win_state->state->y;
957 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
958 && !scr->flags.startup
959 && workspace == scr->current_workspace
960 && !wwin->flags.miniaturized
961 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
963 if (transientOwner && transientOwner->flags.mapped) {
964 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
965 WMRect rect;
966 int head;
968 x = transientOwner->frame_x +
969 abs((transientOwner->frame->core->width - width) / 2) + offs;
970 y = transientOwner->frame_y +
971 abs((transientOwner->frame->core->height - height) / 3) + offs;
974 * limit transient windows to be inside their parent's head
976 rect.pos.x = transientOwner->frame_x;
977 rect.pos.y = transientOwner->frame_y;
978 rect.size.width = transientOwner->frame->core->width;
979 rect.size.height = transientOwner->frame->core->height;
981 head = wGetHeadForRect(scr, rect);
982 rect = wGetRectForHead(scr, head);
984 if (x < rect.pos.x)
985 x = rect.pos.x;
986 else if (x + width > rect.pos.x + rect.size.width)
987 x = rect.pos.x + rect.size.width - width;
989 if (y < rect.pos.y)
990 y = rect.pos.y;
991 else if (y + height > rect.pos.y + rect.size.height)
992 y = rect.pos.y + rect.size.height - height;
994 } else {
995 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;
1006 * Make spash screens come out in the center of a head
1007 * trouble is that most splashies never get here
1008 * they are managed trough atoms but god knows where.
1009 * Dan, do you know ? -peter
1011 * Most of them are not managed, they have set
1012 * OverrideRedirect, which means we can't do anything about
1013 * them. -alfredo
1017 * xinerama checks for: across head and dead space
1019 rect.pos.x = x;
1020 rect.pos.y = y;
1021 rect.size.width = width;
1022 rect.size.height = height;
1024 head = wGetRectPlacementInfo(scr, rect, &flags);
1026 if (flags & XFLAG_DEAD)
1027 reposition = 1;
1029 if (flags & XFLAG_MULTIPLE)
1030 reposition = 2;
1033 switch (reposition) {
1034 case 1:
1035 head = wGetHeadForPointerLocation(scr);
1036 rect = wGetRectForHead(scr, head);
1038 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1039 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1040 break;
1042 case 2:
1043 rect = wGetRectForHead(scr, head);
1045 if (x < rect.pos.x)
1046 x = rect.pos.x;
1047 else if (x + width > rect.pos.x + rect.size.width)
1048 x = rect.pos.x + rect.size.width - width;
1050 if (y < rect.pos.y)
1051 y = rect.pos.y;
1052 else if (y + height > rect.pos.y + rect.size.height)
1053 y = rect.pos.y + rect.size.height - height;
1055 break;
1057 default:
1058 break;
1062 if (WFLAGP(wwin, dont_move_off) && dontBring)
1063 wScreenBringInside(scr, &x, &y, width, height);
1066 wNETWMPositionSplash(wwin, &x, &y, width, height);
1068 if (wwin->flags.urgent) {
1069 if (!IS_OMNIPRESENT(wwin))
1070 wwin->flags.omnipresent ^= 1;
1074 * Create frame, borders and do reparenting
1076 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1077 #ifdef XKB_BUTTON_HINT
1078 if (wPreferences.modelock)
1079 foo |= WFF_LANGUAGE_BUTTON;
1080 #endif
1081 if (HAS_TITLEBAR(wwin))
1082 foo |= WFF_TITLEBAR;
1083 if (HAS_RESIZEBAR(wwin))
1084 foo |= WFF_RESIZEBAR;
1085 if (HAS_BORDER(wwin))
1086 foo |= WFF_BORDER;
1088 wwin->frame = wFrameWindowCreate(scr, window_level,
1089 x, y, width, height,
1090 &wPreferences.window_title_clearance,
1091 &wPreferences.window_title_min_height,
1092 &wPreferences.window_title_max_height,
1093 foo,
1094 scr->window_title_texture,
1095 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1096 wattribs.depth, wattribs.visual, wattribs.colormap);
1098 wwin->frame->flags.is_client_window_frame = 1;
1099 wwin->frame->flags.justification = wPreferences.title_justification;
1101 /* setup button images */
1102 wWindowUpdateButtonImages(wwin);
1104 /* hide unused buttons */
1105 foo = 0;
1106 if (WFLAGP(wwin, no_close_button))
1107 foo |= WFF_RIGHT_BUTTON;
1108 if (WFLAGP(wwin, no_miniaturize_button))
1109 foo |= WFF_LEFT_BUTTON;
1110 #ifdef XKB_BUTTON_HINT
1111 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1112 foo |= WFF_LANGUAGE_BUTTON;
1113 #endif
1114 if (foo != 0)
1115 wFrameWindowHideButton(wwin->frame, foo);
1117 wwin->frame->child = wwin;
1118 wwin->frame->workspace = workspace;
1119 wwin->frame->on_click_left = windowIconifyClick;
1121 #ifdef XKB_BUTTON_HINT
1122 if (wPreferences.modelock)
1123 wwin->frame->on_click_language = windowLanguageClick;
1124 #endif
1126 wwin->frame->on_click_right = windowCloseClick;
1127 wwin->frame->on_dblclick_right = windowCloseDblClick;
1128 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1129 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1130 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1132 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1133 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1134 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1137 int gx, gy;
1139 wClientGetGravityOffsets(wwin, &gx, &gy);
1141 /* if gravity is to the south, account for the border sizes */
1142 if (gy > 0)
1143 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1147 * wWindowConfigure() will init the client window's size
1148 * (wwin->client.{width,height}) and all other geometry
1149 * related variables (frame_x,frame_y)
1151 wWindowConfigure(wwin, x, y, width, height);
1153 /* to make sure the window receives it's new position after reparenting */
1154 wWindowSynthConfigureNotify(wwin);
1157 * Setup descriptors and save window to internal
1158 * lists
1160 if (wwin->main_window != None) {
1161 WApplication *app;
1162 WWindow *leader;
1164 /* Leader windows do not necessary set themselves as leaders.
1165 * If this is the case, point the leader of this window to
1166 * itself */
1167 leader = wWindowFor(wwin->main_window);
1168 if (leader && leader->main_window == None)
1169 leader->main_window = leader->client_win;
1171 app = wApplicationCreate(wwin);
1172 if (app) {
1173 app->last_workspace = workspace;
1176 * Do application specific stuff, like setting application
1177 * wide attributes.
1180 if (wwin->flags.hidden) {
1181 /* if the window was set to hidden because it was hidden
1182 * in a previous incarnation and that state was restored */
1183 app->flags.hidden = 1;
1184 } else if (app->flags.hidden) {
1185 if (WFLAGP(app->main_window_desc, start_hidden)) {
1186 wwin->flags.hidden = 1;
1187 } else {
1188 wUnhideApplication(app, False, False);
1189 raise = True;
1192 wAppBounce(app);
1196 /* setup the frame descriptor */
1197 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1198 wwin->frame->core->descriptor.parent = wwin;
1199 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1201 /* don't let windows go away if we die */
1202 XAddToSaveSet(dpy, window);
1204 XLowerWindow(dpy, window);
1206 /* if window is in this workspace and should be mapped, then map it */
1207 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1208 && !wwin->flags.hidden && !withdraw) {
1210 /* The following "if" is to avoid crashing of clients that expect
1211 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1212 * after the return from this function.
1214 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1215 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1216 else
1217 wClientSetState(wwin, NormalState, None);
1219 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1220 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1222 * The brain damaged idiotic non-click to focus modes will
1223 * have trouble with this because:
1225 * 1. window is created and mapped by the client
1226 * 2. window is mapped by wmaker in small size
1227 * 3. window is animated to grow to normal size
1228 * 4. this function returns to normal event loop
1229 * 5. eventually, the EnterNotify event that would trigger
1230 * the window focusing (if the mouse is over that window)
1231 * will be processed by wmaker.
1232 * But since this event will be rather delayed
1233 * (step 3 has a large delay) the time when the event ocurred
1234 * and when it is processed, the client that owns that window
1235 * will reject the XSetInputFocus() for it.
1237 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1238 DoWindowBirth(wwin);
1240 wWindowMap(wwin);
1243 /* setup stacking descriptor */
1244 if (transientOwner)
1245 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1246 else
1247 wwin->frame->core->stacking->child_of = NULL;
1249 if (!scr->focused_window) {
1250 /* first window on the list */
1251 wwin->next = NULL;
1252 wwin->prev = NULL;
1253 scr->focused_window = wwin;
1254 } else {
1255 WWindow *tmp;
1257 /* add window at beginning of focus window list */
1258 tmp = scr->focused_window;
1259 while (tmp->prev)
1260 tmp = tmp->prev;
1261 tmp->prev = wwin;
1262 wwin->next = tmp;
1263 wwin->prev = NULL;
1266 /* raise is set to true if we un-hid the app when this window was born.
1267 * we raise, else old windows of this app will be above this new one. */
1268 if (raise)
1269 wRaiseFrame(wwin->frame->core);
1271 /* Update name must come after WApplication stuff is done */
1272 wWindowUpdateName(wwin, title);
1273 if (title)
1274 XFree(title);
1276 XUngrabServer(dpy);
1279 * Final preparations before window is ready to go
1281 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1283 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1284 if (((transientOwner && transientOwner->flags.focused)
1285 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1287 /* only auto_focus if on same screen as mouse
1288 * (and same head for xinerama mode)
1289 * TODO: make it an option */
1291 /*TODO add checking the head of the window, is it available? */
1292 short same_screen = 0, same_head = 1;
1294 int foo;
1295 unsigned int bar;
1296 Window dummy;
1298 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1299 &foo, &foo, &foo, &foo, &bar) != False)
1300 same_screen = 1;
1302 if (same_screen == 1 && same_head == 1)
1303 wSetFocusTo(scr, wwin);
1306 wWindowResetMouseGrabs(wwin);
1308 if (!WFLAGP(wwin, no_bind_keys))
1309 wWindowSetKeyGrabs(wwin);
1311 WMPostNotificationName(WMNManaged, wwin, NULL);
1312 wColormapInstallForWindow(scr, scr->cmap_window);
1314 /* Setup Notification Observers */
1315 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1317 /* Cleanup temporary stuff */
1318 if (win_state)
1319 wWindowDeleteSavedState(win_state);
1321 /* If the window must be withdrawed, then do it now.
1322 * Must do some optimization, 'though */
1323 if (withdraw) {
1324 wwin->flags.mapped = 0;
1325 wClientSetState(wwin, WithdrawnState, None);
1326 wUnmanageWindow(wwin, True, False);
1327 wwin = NULL;
1330 return wwin;
1333 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1334 char *title, int x, int y, int width, int height)
1336 WWindow *wwin;
1337 int foo;
1339 wwin = wWindowCreate();
1341 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1343 wwin->flags.internal_window = 1;
1345 WSETUFLAG(wwin, omnipresent, 1);
1346 WSETUFLAG(wwin, no_shadeable, 1);
1347 WSETUFLAG(wwin, no_resizable, 1);
1348 WSETUFLAG(wwin, no_miniaturizable, 1);
1350 wwin->focus_mode = WFM_PASSIVE;
1351 wwin->client_win = window;
1352 wwin->screen_ptr = scr;
1353 wwin->transient_for = owner;
1354 wwin->client.x = x;
1355 wwin->client.y = y;
1356 wwin->client.width = width;
1357 wwin->client.height = height;
1358 wwin->frame_x = wwin->client.x;
1359 wwin->frame_y = wwin->client.y;
1361 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1362 foo |= WFF_TITLEBAR;
1363 #ifdef XKB_BUTTON_HINT
1364 foo |= WFF_LANGUAGE_BUTTON;
1365 #endif
1367 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1368 wwin->frame_x, wwin->frame_y,
1369 width, height,
1370 &wPreferences.window_title_clearance,
1371 &wPreferences.window_title_min_height,
1372 &wPreferences.window_title_max_height,
1373 foo,
1374 scr->window_title_texture,
1375 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1376 scr->w_depth, scr->w_visual, scr->w_colormap);
1378 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1380 wwin->frame->flags.is_client_window_frame = 1;
1381 wwin->frame->flags.justification = wPreferences.title_justification;
1383 wFrameWindowChangeTitle(wwin->frame, title);
1385 /* setup button images */
1386 wWindowUpdateButtonImages(wwin);
1388 /* hide buttons */
1389 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1391 wwin->frame->child = wwin;
1392 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1394 #ifdef XKB_BUTTON_HINT
1395 if (wPreferences.modelock)
1396 wwin->frame->on_click_language = windowLanguageClick;
1397 #endif
1399 wwin->frame->on_click_right = windowCloseClick;
1400 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1401 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1402 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1403 wwin->client.y += wwin->frame->top_width;
1405 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1406 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1408 /* setup the frame descriptor */
1409 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1410 wwin->frame->core->descriptor.parent = wwin;
1411 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1413 XLowerWindow(dpy, window);
1414 XMapSubwindows(dpy, wwin->frame->core->window);
1416 /* setup stacking descriptor */
1417 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1418 WWindow *tmp;
1419 tmp = wWindowFor(wwin->transient_for);
1420 if (tmp)
1421 wwin->frame->core->stacking->child_of = tmp->frame->core;
1422 } else {
1423 wwin->frame->core->stacking->child_of = NULL;
1426 if (!scr->focused_window) {
1427 /* first window on the list */
1428 wwin->next = NULL;
1429 wwin->prev = NULL;
1430 scr->focused_window = wwin;
1431 } else {
1432 WWindow *tmp;
1434 /* add window at beginning of focus window list */
1435 tmp = scr->focused_window;
1436 while (tmp->prev)
1437 tmp = tmp->prev;
1438 tmp->prev = wwin;
1439 wwin->next = tmp;
1440 wwin->prev = NULL;
1443 if (wwin->flags.is_gnustep == 0)
1444 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1446 /* if (wPreferences.auto_focus) */
1447 wSetFocusTo(scr, wwin);
1448 wWindowResetMouseGrabs(wwin);
1449 wWindowSetKeyGrabs(wwin);
1451 return wwin;
1455 *----------------------------------------------------------------------
1456 * wUnmanageWindow--
1457 * Removes the frame window from a window and destroys all data
1458 * related to it. The window will be reparented back to the root window
1459 * if restore is True.
1461 * Side effects:
1462 * Everything related to the window is destroyed and the window
1463 * is removed from the window lists. Focus is set to the previous on the
1464 * window list.
1465 *----------------------------------------------------------------------
1467 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1469 WCoreWindow *frame = wwin->frame->core;
1470 WWindow *owner = NULL;
1471 WWindow *newFocusedWindow = NULL;
1472 int wasFocused;
1473 WScreen *scr = wwin->screen_ptr;
1475 /* First close attribute editor window if open */
1476 if (wwin->flags.inspector_open)
1477 wCloseInspectorForWindow(wwin);
1479 /* Close window menu if it's open for this window */
1480 if (wwin->flags.menu_open_for_me)
1481 CloseWindowMenu(scr);
1483 if (!destroyed) {
1484 if (!wwin->flags.internal_window)
1485 XRemoveFromSaveSet(dpy, wwin->client_win);
1487 /* If this is a leader window, we still need to listen for
1488 * DestroyNotify and PropertyNotify. */
1489 if (wApplicationOf(wwin->client_win))
1490 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1491 else
1492 XSelectInput(dpy, wwin->client_win, NoEventMask);
1494 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1495 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1498 XUnmapWindow(dpy, frame->window);
1500 XUnmapWindow(dpy, wwin->client_win);
1502 /* deselect window */
1503 wSelectWindow(wwin, False);
1505 /* remove all pending events on window */
1506 /* I think this only matters for autoraise */
1507 if (wPreferences.raise_delay)
1508 WMDeleteTimerWithClientData(wwin->frame->core);
1510 XFlush(dpy);
1512 /* reparent the window back to the root */
1513 if (restore)
1514 wClientRestore(wwin);
1516 if (wwin->transient_for != scr->root_win) {
1517 owner = wWindowFor(wwin->transient_for);
1518 if (owner) {
1519 if (!owner->flags.semi_focused)
1520 owner = NULL;
1521 else
1522 owner->flags.semi_focused = 0;
1526 wasFocused = wwin->flags.focused;
1528 /* remove from window focus list */
1529 if (!wwin->prev && !wwin->next) {
1530 /* was the only window */
1531 scr->focused_window = NULL;
1532 newFocusedWindow = NULL;
1533 } else {
1534 WWindow *tmp;
1536 if (wwin->prev)
1537 wwin->prev->next = wwin->next;
1539 if (wwin->next)
1540 wwin->next->prev = wwin->prev;
1541 else {
1542 scr->focused_window = wwin->prev;
1543 scr->focused_window->next = NULL;
1546 if (wPreferences.focus_mode == WKF_CLICK) {
1548 /* if in click to focus mode and the window
1549 * was a transient, focus the owner window
1551 tmp = NULL;
1552 if (wPreferences.focus_mode == WKF_CLICK) {
1553 tmp = wWindowFor(wwin->transient_for);
1554 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1555 tmp = NULL;
1558 /* otherwise, focus the next one in the focus list */
1559 if (!tmp) {
1560 tmp = scr->focused_window;
1561 while (tmp) { /* look for one in the window list first */
1562 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1563 && (tmp->flags.mapped || tmp->flags.shaded))
1564 break;
1565 tmp = tmp->prev;
1567 if (!tmp) { /* if unsuccessful, choose any focusable window */
1568 tmp = scr->focused_window;
1569 while (tmp) {
1570 if (!WFLAGP(tmp, no_focusable)
1571 && (tmp->flags.mapped || tmp->flags.shaded))
1572 break;
1573 tmp = tmp->prev;
1578 newFocusedWindow = tmp;
1580 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1581 unsigned int mask;
1582 int foo;
1583 Window bar, win;
1585 /* This is to let the root window get the keyboard input
1586 * if Sloppy focus mode and no other window get focus.
1587 * This way keybindings will not freeze.
1589 tmp = NULL;
1590 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1591 tmp = wWindowFor(win);
1592 if (tmp == wwin)
1593 tmp = NULL;
1594 newFocusedWindow = tmp;
1595 } else {
1596 newFocusedWindow = NULL;
1600 if (!wwin->flags.internal_window)
1601 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1602 if (wasFocused) {
1603 if (newFocusedWindow != owner && owner) {
1604 if (wwin->flags.is_gnustep == 0)
1605 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1607 wSetFocusTo(scr, newFocusedWindow);
1610 /* Close menu and unhighlight */
1611 WApplication *oapp = wApplicationOf(wwin->main_window);
1612 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1613 if (oapp && oapp != napp) {
1614 wAppMenuUnmap(oapp->menu);
1615 wApplicationDeactivate(oapp);
1618 wNETCleanupFrameExtents(wwin);
1620 wWindowDestroy(wwin);
1621 XFlush(dpy);
1624 void wWindowMap(WWindow *wwin)
1626 XMapWindow(dpy, wwin->frame->core->window);
1627 if (!wwin->flags.shaded) {
1628 /* window will be remapped when getting MapNotify */
1629 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1630 XMapWindow(dpy, wwin->client_win);
1631 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1633 wwin->flags.mapped = 1;
1637 void wWindowUnmap(WWindow *wwin)
1639 wwin->flags.mapped = 0;
1641 /* prevent window withdrawal when getting UnmapNotify */
1642 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1643 XUnmapWindow(dpy, wwin->client_win);
1644 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1646 XUnmapWindow(dpy, wwin->frame->core->window);
1649 void wWindowFocus(WWindow *wwin, WWindow *owin)
1651 WWindow *nowner;
1652 WWindow *oowner;
1654 #ifdef KEEP_XKB_LOCK_STATUS
1655 if (wPreferences.modelock)
1656 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1657 #endif /* KEEP_XKB_LOCK_STATUS */
1659 wwin->flags.semi_focused = 0;
1661 if (wwin->flags.is_gnustep == 0)
1662 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1664 wwin->flags.focused = 1;
1666 wWindowResetMouseGrabs(wwin);
1668 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1670 if (owin == wwin || !owin)
1671 return;
1673 nowner = wWindowFor(wwin->transient_for);
1675 /* new window is a transient for the old window */
1676 if (nowner == owin) {
1677 owin->flags.semi_focused = 1;
1678 wWindowUnfocus(nowner);
1679 return;
1682 oowner = wWindowFor(owin->transient_for);
1684 /* new window is owner of old window */
1685 if (wwin == oowner) {
1686 wWindowUnfocus(owin);
1687 return;
1690 if (!nowner) {
1691 wWindowUnfocus(owin);
1692 return;
1695 /* new window has same owner of old window */
1696 if (oowner == nowner) {
1697 /* prevent unfocusing of owner */
1698 oowner->flags.semi_focused = 0;
1699 wWindowUnfocus(owin);
1700 oowner->flags.semi_focused = 1;
1702 return;
1705 /* nowner != NULL && oowner != nowner */
1706 nowner->flags.semi_focused = 1;
1707 wWindowUnfocus(nowner);
1708 wWindowUnfocus(owin);
1711 void wWindowUnfocus(WWindow *wwin)
1713 CloseWindowMenu(wwin->screen_ptr);
1715 if (wwin->flags.is_gnustep == 0)
1716 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1718 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1719 WWindow *owner;
1720 owner = wWindowFor(wwin->transient_for);
1721 if (owner && owner->flags.semi_focused) {
1722 owner->flags.semi_focused = 0;
1723 if (owner->flags.mapped || owner->flags.shaded) {
1724 wWindowUnfocus(owner);
1725 wFrameWindowPaint(owner->frame);
1729 wwin->flags.focused = 0;
1730 wWindowResetMouseGrabs(wwin);
1731 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1734 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1736 char *title;
1738 if (!wwin->frame)
1739 return;
1741 if (!newTitle)
1742 title = DEF_WINDOW_TITLE; /* the hint was removed */
1743 else
1744 title = newTitle;
1746 if (wFrameWindowChangeTitle(wwin->frame, title))
1747 WMPostNotificationName(WMNChangedName, wwin, NULL);
1751 *----------------------------------------------------------------------
1753 * wWindowConstrainSize--
1754 * Constrains size for the client window, taking the maximal size,
1755 * window resize increments and other size hints into account.
1757 * Returns:
1758 * The closest size to what was given that the client window can
1759 * have.
1761 *----------------------------------------------------------------------
1763 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1765 int width = (int)*nwidth;
1766 int height = (int)*nheight;
1767 int winc = 1;
1768 int hinc = 1;
1769 int minW = 1, minH = 1;
1770 int maxW = wwin->screen_ptr->scr_width * 2;
1771 int maxH = wwin->screen_ptr->scr_height * 2;
1772 int minAX = -1, minAY = -1;
1773 int maxAX = -1, maxAY = -1;
1774 int baseW = 0;
1775 int baseH = 0;
1777 if (wwin->normal_hints) {
1778 winc = wwin->normal_hints->width_inc;
1779 hinc = wwin->normal_hints->height_inc;
1780 minW = wwin->normal_hints->min_width;
1781 minH = wwin->normal_hints->min_height;
1782 maxW = wwin->normal_hints->max_width;
1783 maxH = wwin->normal_hints->max_height;
1784 if (wwin->normal_hints->flags & PAspect) {
1785 minAX = wwin->normal_hints->min_aspect.x;
1786 minAY = wwin->normal_hints->min_aspect.y;
1787 maxAX = wwin->normal_hints->max_aspect.x;
1788 maxAY = wwin->normal_hints->max_aspect.y;
1791 baseW = wwin->normal_hints->base_width;
1792 baseH = wwin->normal_hints->base_height;
1795 if (width < minW)
1796 width = minW;
1797 if (height < minH)
1798 height = minH;
1800 if (width > maxW)
1801 width = maxW;
1802 if (height > maxH)
1803 height = maxH;
1805 /* aspect ratio code borrowed from olwm */
1806 if (minAX > 0) {
1807 /* adjust max aspect ratio */
1808 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1809 if (maxAX > maxAY) {
1810 height = (width * maxAY) / maxAX;
1811 if (height > maxH) {
1812 height = maxH;
1813 width = (height * maxAX) / maxAY;
1815 } else {
1816 width = (height * maxAX) / maxAY;
1817 if (width > maxW) {
1818 width = maxW;
1819 height = (width * maxAY) / maxAX;
1824 /* adjust min aspect ratio */
1825 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1826 if (minAX > minAY) {
1827 height = (width * minAY) / minAX;
1828 if (height < minH) {
1829 height = minH;
1830 width = (height * minAX) / minAY;
1832 } else {
1833 width = (height * minAX) / minAY;
1834 if (width < minW) {
1835 width = minW;
1836 height = (width * minAY) / minAX;
1842 if (baseW != 0)
1843 width = (((width - baseW) / winc) * winc) + baseW;
1844 else
1845 width = (((width - minW) / winc) * winc) + minW;
1847 if (baseH != 0)
1848 height = (((height - baseH) / hinc) * hinc) + baseH;
1849 else
1850 height = (((height - minH) / hinc) * hinc) + minH;
1852 /* broken stupid apps may cause preposterous values for these.. */
1853 if (width > 0)
1854 *nwidth = width;
1855 if (height > 0)
1856 *nheight = height;
1859 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1860 unsigned int *width, unsigned int *height)
1862 int baseW = 0, baseH = 0;
1863 int winc = 1, hinc = 1;
1865 if (wwin->normal_hints) {
1866 baseW = wwin->normal_hints->base_width;
1867 baseH = wwin->normal_hints->base_height;
1869 winc = wwin->normal_hints->width_inc;
1870 hinc = wwin->normal_hints->height_inc;
1873 if (*width > maxW)
1874 *width = maxW - (maxW - baseW) % winc;
1876 if (*height > maxH)
1877 *height = maxH - (maxH - baseH) % hinc;
1880 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1882 WScreen *scr = wwin->screen_ptr;
1883 WApplication *wapp;
1884 int unmap = 0;
1886 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1887 return;
1889 if (workspace != scr->current_workspace) {
1890 /* Sent to other workspace. Unmap window */
1891 if ((wwin->flags.mapped
1892 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1893 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1895 wapp = wApplicationOf(wwin->main_window);
1896 if (wapp)
1897 wapp->last_workspace = workspace;
1899 if (wwin->flags.miniaturized) {
1900 if (wwin->icon) {
1901 XUnmapWindow(dpy, wwin->icon->core->window);
1902 wwin->icon->mapped = 0;
1904 } else {
1905 unmap = 1;
1906 wSetFocusTo(scr, NULL);
1909 } else {
1910 /* brought to current workspace. Map window */
1911 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1912 if (wwin->icon) {
1913 XMapWindow(dpy, wwin->icon->core->window);
1914 wwin->icon->mapped = 1;
1916 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1917 wWindowMap(wwin);
1920 if (!IS_OMNIPRESENT(wwin)) {
1921 int oldWorkspace = wwin->frame->workspace;
1922 wwin->frame->workspace = workspace;
1923 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1926 if (unmap)
1927 wWindowUnmap(wwin);
1930 void wWindowSynthConfigureNotify(WWindow *wwin)
1932 XEvent sevent;
1934 sevent.type = ConfigureNotify;
1935 sevent.xconfigure.display = dpy;
1936 sevent.xconfigure.event = wwin->client_win;
1937 sevent.xconfigure.window = wwin->client_win;
1939 sevent.xconfigure.x = wwin->client.x;
1940 sevent.xconfigure.y = wwin->client.y;
1941 sevent.xconfigure.width = wwin->client.width;
1942 sevent.xconfigure.height = wwin->client.height;
1944 sevent.xconfigure.border_width = wwin->old_border_width;
1945 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1946 sevent.xconfigure.above = wwin->frame->titlebar->window;
1947 else
1948 sevent.xconfigure.above = None;
1950 sevent.xconfigure.override_redirect = False;
1951 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1952 XFlush(dpy);
1956 *----------------------------------------------------------------------
1957 * wWindowConfigure()
1959 * req_x, req_y: new requested positions for the frame
1960 * req_width, req_height: new requested sizes for the client
1962 * Configures the frame, decorations and client window to the specified
1963 * geometry, whose validity is not checked -- wWindowConstrainSize()
1964 * must be used for that.
1965 * The size parameters are for the client window, but the position is
1966 * for the frame.
1967 * The client window receives a ConfigureNotify event, according
1968 * to what ICCCM says.
1970 * Returns:
1971 * None
1973 * Side effects:
1974 * Window size and position are changed and client window receives
1975 * a ConfigureNotify event.
1976 *----------------------------------------------------------------------
1978 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
1980 int synth_notify = False;
1981 int resize;
1983 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
1985 * if the window is being moved but not resized then
1986 * send a synthetic ConfigureNotify
1988 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
1989 synth_notify = True;
1991 if (WFLAGP(wwin, dont_move_off))
1992 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
1994 if (resize) {
1995 if (req_width < MIN_WINDOW_SIZE)
1996 req_width = MIN_WINDOW_SIZE;
1997 if (req_height < MIN_WINDOW_SIZE)
1998 req_height = MIN_WINDOW_SIZE;
2000 /* If growing, resize inner part before frame,
2001 * if shrinking, resize frame before.
2002 * This will prevent the frame (that can have a different color)
2003 * to be exposed, causing flicker */
2004 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2005 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2007 if (wwin->flags.shaded) {
2008 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2009 wwin->old_geometry.height = req_height;
2010 } else {
2011 int h;
2013 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2015 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2018 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2019 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2021 wwin->client.x = req_x;
2022 wwin->client.y = req_y + wwin->frame->top_width;
2023 wwin->client.width = req_width;
2024 wwin->client.height = req_height;
2025 } else {
2026 wwin->client.x = req_x;
2027 wwin->client.y = req_y + wwin->frame->top_width;
2029 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2031 wwin->frame_x = req_x;
2032 wwin->frame_y = req_y;
2033 if (HAS_BORDER(wwin)) {
2034 wwin->client.x += FRAME_BORDER_WIDTH;
2035 wwin->client.y += FRAME_BORDER_WIDTH;
2037 #ifdef SHAPE
2038 if (wShapeSupported && wwin->flags.shaped && resize)
2039 wWindowSetShape(wwin);
2040 #endif
2042 if (synth_notify)
2043 wWindowSynthConfigureNotify(wwin);
2045 wNETFrameExtents(wwin);
2047 XFlush(dpy);
2050 /* req_x, req_y: new position of the frame */
2051 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2053 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2054 int synth_notify = False;
2056 /* Send a synthetic ConfigureNotify event for every window movement. */
2057 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2058 synth_notify = True;
2059 #else
2060 /* A single synthetic ConfigureNotify event is sent at the end of
2061 * a completed (opaque) movement in moveres.c */
2062 #endif
2064 if (WFLAGP(wwin, dont_move_off))
2065 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2066 wwin->frame->core->width, wwin->frame->core->height);
2068 wwin->client.x = req_x;
2069 wwin->client.y = req_y + wwin->frame->top_width;
2070 if (HAS_BORDER(wwin)) {
2071 wwin->client.x += FRAME_BORDER_WIDTH;
2072 wwin->client.y += FRAME_BORDER_WIDTH;
2075 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2077 wwin->frame_x = req_x;
2078 wwin->frame_y = req_y;
2080 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2081 if (synth_notify)
2082 wWindowSynthConfigureNotify(wwin);
2083 #endif
2086 void wWindowUpdateButtonImages(WWindow *wwin)
2088 WScreen *scr = wwin->screen_ptr;
2089 Pixmap pixmap, mask;
2090 WFrameWindow *fwin = wwin->frame;
2092 if (!HAS_TITLEBAR(wwin))
2093 return;
2095 /* miniaturize button */
2096 if (!WFLAGP(wwin, no_miniaturize_button)) {
2097 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2098 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2100 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2101 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2102 else
2103 mask = None;
2105 if (fwin->lbutton_image
2106 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2107 wPixmapDestroy(fwin->lbutton_image);
2108 fwin->lbutton_image = NULL;
2111 if (!fwin->lbutton_image) {
2112 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2113 fwin->lbutton_image->client_owned = 1;
2114 fwin->lbutton_image->client_owned_mask = 1;
2116 } else {
2117 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2118 wPixmapDestroy(fwin->lbutton_image);
2120 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2123 #ifdef XKB_BUTTON_HINT
2124 if (!WFLAGP(wwin, no_language_button)) {
2125 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2126 wPixmapDestroy(fwin->languagebutton_image);
2128 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2130 #endif
2132 /* close button */
2134 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2135 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2137 if (!WFLAGP(wwin, no_close_button)) {
2138 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2139 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2141 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2142 mask = wwin->wm_gnustep_attr->close_mask;
2143 else
2144 mask = None;
2146 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2147 || fwin->rbutton_image->mask != mask)) {
2148 wPixmapDestroy(fwin->rbutton_image);
2149 fwin->rbutton_image = NULL;
2152 if (!fwin->rbutton_image) {
2153 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2154 fwin->rbutton_image->client_owned = 1;
2155 fwin->rbutton_image->client_owned_mask = 1;
2158 } else if (WFLAGP(wwin, kill_close)) {
2159 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2160 wPixmapDestroy(fwin->rbutton_image);
2162 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2164 } else if (MGFLAGP(wwin, broken_close)) {
2165 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2166 wPixmapDestroy(fwin->rbutton_image);
2168 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2170 } else {
2171 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2172 wPixmapDestroy(fwin->rbutton_image);
2174 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2178 /* force buttons to be redrawn */
2179 fwin->flags.need_texture_change = 1;
2180 wFrameWindowPaint(fwin);
2184 *---------------------------------------------------------------------------
2185 * wWindowConfigureBorders--
2186 * Update window border configuration according to attribute flags.
2188 *---------------------------------------------------------------------------
2190 void wWindowConfigureBorders(WWindow *wwin)
2192 if (wwin->frame) {
2193 int flags;
2194 int newy, oldh;
2196 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2198 #ifdef XKB_BUTTON_HINT
2199 if (wPreferences.modelock)
2200 flags |= WFF_LANGUAGE_BUTTON;
2201 #endif
2203 if (HAS_TITLEBAR(wwin))
2204 flags |= WFF_TITLEBAR;
2205 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2206 flags |= WFF_RESIZEBAR;
2207 if (HAS_BORDER(wwin))
2208 flags |= WFF_BORDER;
2209 if (wwin->flags.shaded)
2210 flags |= WFF_IS_SHADED;
2212 oldh = wwin->frame->top_width;
2213 wFrameWindowUpdateBorders(wwin->frame, flags);
2214 if (oldh != wwin->frame->top_width) {
2215 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2217 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2218 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2221 flags = 0;
2222 if (!WFLAGP(wwin, no_miniaturize_button)
2223 && wwin->frame->flags.hide_left_button)
2224 flags |= WFF_LEFT_BUTTON;
2226 #ifdef XKB_BUTTON_HINT
2227 if (!WFLAGP(wwin, no_language_button)
2228 && wwin->frame->flags.hide_language_button)
2229 flags |= WFF_LANGUAGE_BUTTON;
2230 #endif
2232 if (!WFLAGP(wwin, no_close_button)
2233 && wwin->frame->flags.hide_right_button)
2234 flags |= WFF_RIGHT_BUTTON;
2236 if (flags != 0) {
2237 wWindowUpdateButtonImages(wwin);
2238 wFrameWindowShowButton(wwin->frame, flags);
2241 flags = 0;
2242 if (WFLAGP(wwin, no_miniaturize_button)
2243 && !wwin->frame->flags.hide_left_button)
2244 flags |= WFF_LEFT_BUTTON;
2246 #ifdef XKB_BUTTON_HINT
2247 if (WFLAGP(wwin, no_language_button)
2248 && !wwin->frame->flags.hide_language_button)
2249 flags |= WFF_LANGUAGE_BUTTON;
2250 #endif
2252 if (WFLAGP(wwin, no_close_button)
2253 && !wwin->frame->flags.hide_right_button)
2254 flags |= WFF_RIGHT_BUTTON;
2256 if (flags != 0)
2257 wFrameWindowHideButton(wwin->frame, flags);
2259 #ifdef SHAPE
2260 if (wShapeSupported && wwin->flags.shaped)
2261 wWindowSetShape(wwin);
2262 #endif
2266 void wWindowSaveState(WWindow * wwin)
2268 long data[10];
2269 int i;
2271 memset(data, 0, sizeof(long) * 10);
2272 data[0] = wwin->frame->workspace;
2273 data[1] = wwin->flags.miniaturized;
2274 data[2] = wwin->flags.shaded;
2275 data[3] = wwin->flags.hidden;
2276 data[4] = wwin->flags.maximized;
2277 if (wwin->flags.maximized == 0) {
2278 data[5] = wwin->frame_x;
2279 data[6] = wwin->frame_y;
2280 data[7] = wwin->frame->core->width;
2281 data[8] = wwin->frame->core->height;
2282 } else {
2283 data[5] = wwin->old_geometry.x;
2284 data[6] = wwin->old_geometry.y;
2285 data[7] = wwin->old_geometry.width;
2286 data[8] = wwin->old_geometry.height;
2289 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2290 if (wwin->screen_ptr->shortcutWindows[i] &&
2291 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2292 data[9] |= 1 << i;
2294 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2295 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2298 static int getSavedState(Window window, WSavedState ** state)
2300 Atom type_ret;
2301 int fmt_ret;
2302 unsigned long nitems_ret;
2303 unsigned long bytes_after_ret;
2304 long *data;
2306 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2307 True, _XA_WINDOWMAKER_STATE,
2308 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2309 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2310 return 0;
2312 *state = wmalloc(sizeof(WSavedState));
2314 (*state)->workspace = data[0];
2315 (*state)->miniaturized = data[1];
2316 (*state)->shaded = data[2];
2317 (*state)->hidden = data[3];
2318 (*state)->maximized = data[4];
2319 (*state)->x = data[5];
2320 (*state)->y = data[6];
2321 (*state)->w = data[7];
2322 (*state)->h = data[8];
2323 (*state)->window_shortcuts = data[9];
2325 XFree(data);
2327 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2328 return 1;
2329 else
2330 return 0;
2333 #ifdef SHAPE
2334 void wWindowClearShape(WWindow * wwin)
2336 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2337 0, wwin->frame->top_width, None, ShapeSet);
2338 XFlush(dpy);
2341 void wWindowSetShape(WWindow * wwin)
2343 XRectangle rect[2];
2344 int count;
2345 #ifdef OPTIMIZE_SHAPE
2346 XRectangle *rects;
2347 XRectangle *urec;
2348 int ordering;
2350 /* only shape is the client's */
2351 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2352 goto alt_code;
2354 /* Get array of rectangles describing the shape mask */
2355 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2356 if (!rects)
2357 goto alt_code;
2359 urec = malloc(sizeof(XRectangle) * (count + 2));
2360 if (!urec) {
2361 XFree(rects);
2362 goto alt_code;
2365 /* insert our decoration rectangles in the rect list */
2366 memcpy(urec, rects, sizeof(XRectangle) * count);
2367 XFree(rects);
2369 if (HAS_TITLEBAR(wwin)) {
2370 urec[count].x = -1;
2371 urec[count].y = -1 - wwin->frame->top_width;
2372 urec[count].width = wwin->frame->core->width + 2;
2373 urec[count].height = wwin->frame->top_width + 1;
2374 count++;
2376 if (HAS_RESIZEBAR(wwin)) {
2377 urec[count].x = -1;
2378 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2379 urec[count].width = wwin->frame->core->width + 2;
2380 urec[count].height = wwin->frame->bottom_width + 1;
2381 count++;
2384 /* shape our frame window */
2385 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2386 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2387 XFlush(dpy);
2388 wfree(urec);
2389 return;
2391 alt_code:
2392 #endif /* OPTIMIZE_SHAPE */
2393 count = 0;
2394 if (HAS_TITLEBAR(wwin)) {
2395 rect[count].x = -1;
2396 rect[count].y = -1;
2397 rect[count].width = wwin->frame->core->width + 2;
2398 rect[count].height = wwin->frame->top_width + 1;
2399 count++;
2401 if (HAS_RESIZEBAR(wwin)) {
2402 rect[count].x = -1;
2403 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2404 rect[count].width = wwin->frame->core->width + 2;
2405 rect[count].height = wwin->frame->bottom_width + 1;
2406 count++;
2408 if (count > 0) {
2409 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2410 0, 0, rect, count, ShapeSet, Unsorted);
2412 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2413 0, wwin->frame->top_width, wwin->client_win,
2414 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2415 XFlush(dpy);
2417 #endif /* SHAPE */
2419 /* ====================================================================== */
2421 static FocusMode getFocusMode(WWindow * wwin)
2423 FocusMode mode;
2425 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2426 if (wwin->wm_hints->input == True) {
2427 if (wwin->protocols.TAKE_FOCUS)
2428 mode = WFM_LOCALLY_ACTIVE;
2429 else
2430 mode = WFM_PASSIVE;
2431 } else {
2432 if (wwin->protocols.TAKE_FOCUS)
2433 mode = WFM_GLOBALLY_ACTIVE;
2434 else
2435 mode = WFM_NO_INPUT;
2437 } else {
2438 mode = WFM_PASSIVE;
2440 return mode;
2443 void wWindowSetKeyGrabs(WWindow * wwin)
2445 int i;
2446 WShortKey *key;
2448 for (i = 0; i < WKBD_LAST; i++) {
2449 key = &wKeyBindings[i];
2451 if (key->keycode == 0)
2452 continue;
2453 if (key->modifier != AnyModifier) {
2454 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2455 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2456 #ifdef NUMLOCK_HACK
2457 /* Also grab all modifier combinations possible that include,
2458 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2459 * work even if the NumLock/ScrollLock key is on.
2461 wHackedGrabKey(key->keycode, key->modifier,
2462 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2463 #endif
2465 XGrabKey(dpy, key->keycode, key->modifier,
2466 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2469 wRootMenuBindShortcuts(wwin->frame->core->window);
2472 void wWindowResetMouseGrabs(WWindow * wwin)
2474 /* Mouse grabs can't be done on the client window because of
2475 * ICCCM and because clients that try to do the same will crash.
2477 * But there is a problem wich makes tbar buttons of unfocused
2478 * windows not usable as the click goes to the frame window instead
2479 * of the button itself. Must figure a way to fix that.
2482 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2484 if (!WFLAGP(wwin, no_bind_mouse)) {
2485 /* grabs for Meta+drag */
2486 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2487 True, ButtonPressMask | ButtonReleaseMask,
2488 GrabModeSync, GrabModeAsync, None, None);
2490 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2491 * but we only grab it for Button4 and Button 5 since a lot of apps
2492 * use CTRL+Button1-3 for app related functionality
2494 if (wPreferences.resize_increment > 0) {
2495 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2496 True, ButtonPressMask | ButtonReleaseMask,
2497 GrabModeSync, GrabModeAsync, None, None);
2498 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2499 True, ButtonPressMask | ButtonReleaseMask,
2500 GrabModeSync, GrabModeAsync, None, None);
2502 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2503 True, ButtonPressMask | ButtonReleaseMask,
2504 GrabModeSync, GrabModeAsync, None, None);
2505 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2506 True, ButtonPressMask | ButtonReleaseMask,
2507 GrabModeSync, GrabModeAsync, None, None);
2511 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2512 && !wwin->flags.is_gnustep) {
2513 /* the passive grabs to focus the window */
2514 /* if (wPreferences.focus_mode == WKF_CLICK) */
2515 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2516 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2518 XFlush(dpy);
2521 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2523 if (attr->flags & GSExtraFlagsAttr) {
2524 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2525 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2526 wWindowUpdateButtonImages(wwin);
2531 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2533 WWindowState *wstate;
2535 wstate = malloc(sizeof(WWindowState));
2536 if (!wstate)
2537 return NULL;
2539 memset(wstate, 0, sizeof(WWindowState));
2540 wstate->pid = pid;
2541 if (instance)
2542 wstate->instance = wstrdup(instance);
2543 if (class)
2544 wstate->class = wstrdup(class);
2545 if (command)
2546 wstate->command = wstrdup(command);
2547 wstate->state = state;
2549 wstate->next = windowState;
2550 windowState = wstate;
2552 return wstate;
2555 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2557 WMagicNumber wWindowGetSavedState(Window win)
2559 char *instance, *class, *command = NULL;
2560 WWindowState *wstate = windowState;
2562 if (!wstate)
2563 return NULL;
2565 command = GetCommandForWindow(win);
2566 if (!command)
2567 return NULL;
2569 if (PropGetWMClass(win, &class, &instance)) {
2570 while (wstate) {
2571 if (SAME(instance, wstate->instance) &&
2572 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2573 break;
2575 wstate = wstate->next;
2577 } else {
2578 wstate = NULL;
2581 if (command)
2582 wfree(command);
2583 if (instance)
2584 free(instance);
2585 if (class)
2586 free(class);
2588 return wstate;
2591 void wWindowDeleteSavedState(WMagicNumber id)
2593 WWindowState *tmp, *wstate = (WWindowState *) id;
2595 if (!wstate || !windowState)
2596 return;
2598 tmp = windowState;
2599 if (tmp == wstate) {
2600 windowState = wstate->next;
2601 remove_wwindowstate(wstate);
2602 } else {
2603 while (tmp->next) {
2604 if (tmp->next == wstate) {
2605 tmp->next = wstate->next;
2606 remove_wwindowstate(wstate);
2607 break;
2609 tmp = tmp->next;
2614 void wWindowDeleteSavedStatesForPID(pid_t pid)
2616 WWindowState *tmp, *wstate;
2618 if (!windowState)
2619 return;
2621 tmp = windowState;
2622 if (tmp->pid == pid) {
2623 wstate = windowState;
2624 windowState = tmp->next;
2626 remove_wwindowstate(wstate);
2627 } else {
2628 while (tmp->next) {
2629 if (tmp->next->pid == pid) {
2630 wstate = tmp->next;
2631 tmp->next = wstate->next;
2632 remove_wwindowstate(wstate);
2633 break;
2635 tmp = tmp->next;
2640 static void remove_wwindowstate(WWindowState *wstate)
2642 if (wstate->instance)
2643 wfree(wstate->instance);
2645 if (wstate->class)
2646 wfree(wstate->class);
2648 if (wstate->command)
2649 wfree(wstate->command);
2651 wfree(wstate->state);
2652 wfree(wstate);
2655 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2657 if (wwin->flags.omnipresent == flag)
2658 return;
2660 wwin->flags.omnipresent = flag;
2661 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2664 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2666 WWindow *wwin = data;
2668 #ifndef NUMLOCK_HACK
2669 if ((event->xbutton.state & ValidModMask)
2670 != (event->xbutton.state & ~LockMask)) {
2671 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2672 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2674 #endif
2676 event->xbutton.state &= ValidModMask;
2678 CloseWindowMenu(wwin->screen_ptr);
2680 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2681 && !WFLAGP(wwin, no_focusable)) {
2682 wSetFocusTo(wwin->screen_ptr, wwin);
2685 if (event->xbutton.button == Button1)
2686 wRaiseFrame(wwin->frame->core);
2688 if (event->xbutton.window != wwin->frame->resizebar->window) {
2689 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2690 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2691 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2692 return;
2696 if (event->xbutton.state & MOD_MASK) {
2697 /* move the window */
2698 wMouseMoveWindow(wwin, event);
2699 XUngrabPointer(dpy, CurrentTime);
2700 } else {
2701 wMouseResizeWindow(wwin, event);
2702 XUngrabPointer(dpy, CurrentTime);
2706 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2708 WWindow *wwin = data;
2710 event->xbutton.state &= ValidModMask;
2712 if (event->xbutton.button == Button1) {
2713 if (event->xbutton.state == 0) {
2714 if (!WFLAGP(wwin, no_shadeable)) {
2715 /* shade window */
2716 if (wwin->flags.shaded)
2717 wUnshadeWindow(wwin);
2718 else
2719 wShadeWindow(wwin);
2721 } else {
2722 int dir = 0;
2724 if (event->xbutton.state & ControlMask)
2725 dir |= MAX_VERTICAL;
2727 if (event->xbutton.state & ShiftMask) {
2728 dir |= MAX_HORIZONTAL;
2729 if (!(event->xbutton.state & ControlMask))
2730 wSelectWindow(wwin, !wwin->flags.selected);
2733 /* maximize window */
2734 if (dir != 0 && IS_RESIZABLE(wwin)) {
2735 int ndir = dir ^ wwin->flags.maximized;
2737 if (ndir != 0)
2738 wMaximizeWindow(wwin, ndir);
2739 else
2740 wUnmaximizeWindow(wwin);
2743 } else if (event->xbutton.button == Button3) {
2744 if (event->xbutton.state & MOD_MASK)
2745 wHideOtherApplications(wwin);
2746 } else if (event->xbutton.button == Button2) {
2747 wSelectWindow(wwin, !wwin->flags.selected);
2748 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2749 wShadeWindow(wwin);
2750 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2751 wUnshadeWindow(wwin);
2755 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2757 WWindow *wwin = desc->parent;
2758 unsigned int new_width, w_scale;
2759 unsigned int new_height, h_scale;
2760 unsigned int resize_width_increment = 0;
2761 unsigned int resize_height_increment = 0;
2763 if (wwin->normal_hints) {
2764 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2765 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2766 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2767 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2769 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2770 resize_width_increment = wPreferences.resize_increment;
2771 resize_height_increment = wPreferences.resize_increment;
2774 event->xbutton.state &= ValidModMask;
2776 CloseWindowMenu(wwin->screen_ptr);
2778 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2779 wSetFocusTo(wwin->screen_ptr, wwin);
2781 if (event->xbutton.button == Button1)
2782 wRaiseFrame(wwin->frame->core);
2784 if (event->xbutton.state & ControlMask) {
2785 if (event->xbutton.button == Button4) {
2786 new_width = wwin->client.width - resize_width_increment;
2787 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2788 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2790 if (event->xbutton.button == Button5) {
2791 new_width = wwin->client.width + resize_width_increment;
2792 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2793 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2797 if (event->xbutton.state & MOD_MASK) {
2798 /* move the window */
2799 if (XGrabPointer(dpy, wwin->client_win, False,
2800 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2801 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2802 return;
2804 if (event->xbutton.button == Button3) {
2805 wMouseResizeWindow(wwin, event);
2806 } else if (event->xbutton.button == Button4) {
2807 new_height = wwin->client.height - resize_height_increment;
2808 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2809 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2810 } else if (event->xbutton.button == Button5) {
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 == Button1 || event->xbutton.button == Button2) {
2815 wMouseMoveWindow(wwin, event);
2817 XUngrabPointer(dpy, CurrentTime);
2821 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2823 WWindow *wwin = (WWindow *) data;
2825 #ifndef NUMLOCK_HACK
2826 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2827 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2828 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2829 #endif
2830 event->xbutton.state &= ValidModMask;
2832 CloseWindowMenu(wwin->screen_ptr);
2834 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2835 && !WFLAGP(wwin, no_focusable))
2836 wSetFocusTo(wwin->screen_ptr, wwin);
2838 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2840 if (event->xbutton.button == Button1) {
2841 if (event->xbutton.state & MOD_MASK)
2842 wLowerFrame(wwin->frame->core);
2843 else
2844 wRaiseFrame(wwin->frame->core);
2846 if ((event->xbutton.state & ShiftMask)
2847 && !(event->xbutton.state & ControlMask)) {
2848 wSelectWindow(wwin, !wwin->flags.selected);
2849 return;
2851 if (event->xbutton.window != wwin->frame->titlebar->window
2852 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2853 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2854 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2855 return;
2858 /* move the window */
2859 wMouseMoveWindow(wwin, event);
2861 XUngrabPointer(dpy, CurrentTime);
2862 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2863 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2864 WObjDescriptor *desc;
2866 if (event->xbutton.window != wwin->frame->titlebar->window
2867 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2868 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2869 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2870 return;
2873 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2875 /* allow drag select */
2876 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2877 event->xany.send_event = True;
2878 (*desc->handle_mousedown) (desc, event);
2880 XUngrabPointer(dpy, CurrentTime);
2884 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2886 WWindow *wwin = data;
2888 event->xbutton.state &= ValidModMask;
2890 CloseWindowMenu(wwin->screen_ptr);
2892 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2893 return;
2895 /* if control-click, kill the client */
2896 if (event->xbutton.state & ControlMask) {
2897 wClientKill(wwin);
2898 } else {
2899 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2900 /* send delete message */
2901 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2906 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2908 WWindow *wwin = data;
2910 CloseWindowMenu(wwin->screen_ptr);
2912 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2913 return;
2915 /* send delete message */
2916 if (wwin->protocols.DELETE_WINDOW)
2917 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2918 else
2919 wClientKill(wwin);
2922 #ifdef XKB_BUTTON_HINT
2923 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2925 WWindow *wwin = data;
2926 WFrameWindow *fwin = wwin->frame;
2927 WScreen *scr = fwin->screen_ptr;
2928 int tl;
2930 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2931 return;
2932 tl = wwin->frame->languagemode;
2933 wwin->frame->languagemode = wwin->frame->last_languagemode;
2934 wwin->frame->last_languagemode = tl;
2935 wSetFocusTo(scr, wwin);
2936 wwin->frame->languagebutton_image =
2937 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2938 wFrameWindowUpdateLanguageButton(wwin->frame);
2939 if (event->xbutton.button == Button3)
2940 return;
2941 wRaiseFrame(fwin->core);
2943 #endif
2945 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2947 WWindow *wwin = data;
2949 event->xbutton.state &= ValidModMask;
2951 CloseWindowMenu(wwin->screen_ptr);
2953 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2954 return;
2956 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2957 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2958 } else {
2959 WApplication *wapp;
2960 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2962 wapp = wApplicationOf(wwin->main_window);
2963 if (wapp && !WFLAGP(wwin, no_appicon))
2964 wHideApplication(wapp);
2965 } else if (event->xbutton.state == 0) {
2966 wIconifyWindow(wwin);