Remove DEBUG statements, #if 0 etc
[wmaker-crm.git] / src / window.c
blob16f3aca6445accecd4fa1c891996159f481db020
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
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <math.h>
39 /* For getting mouse wheel mappings from WINGs */
40 #include <WINGs/WINGsP.h>
42 #include "WindowMaker.h"
43 #include "GNUstep.h"
44 #include "wcore.h"
45 #include "framewin.h"
46 #include "texture.h"
47 #include "window.h"
48 #include "winspector.h"
49 #include "icon.h"
50 #include "properties.h"
51 #include "actions.h"
52 #include "client.h"
53 #include "funcs.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
60 #ifdef MWM_HINTS
61 # include "motif.h"
62 #endif
63 #include "wmspec.h"
65 #define MOD_MASK wPreferences.modifier_mask
67 /****** Global Variables ******/
68 extern WShortKey wKeyBindings[WKBD_LAST];
70 #ifdef SHAPE
71 extern Bool wShapeSupported;
72 #endif
74 /* contexts */
75 extern XContext wWinContext;
77 /* cursors */
78 extern Cursor wCursor[WCUR_LAST];
80 /* protocol atoms */
81 extern Atom _XA_WM_DELETE_WINDOW;
82 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
83 extern Atom _XA_WINDOWMAKER_STATE;
85 extern WPreferences wPreferences;
86 extern Time LastTimestamp;
88 /* superfluous... */
89 extern void DoWindowBirth(WWindow *wwin);
91 /***** Local Stuff *****/
92 static WWindowState *windowState = NULL;
94 static FocusMode getFocusMode(WWindow *wwin);
95 static int getSavedState(Window window, WSavedState **state);
96 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
98 /* frame window (during window grabs) */
99 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
101 /* close button */
102 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
103 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
105 /* iconify button */
106 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
108 #ifdef XKB_BUTTON_HINT
109 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
110 #endif
112 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
113 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
114 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
116 /****** Notification Observers ******/
118 static void appearanceObserver(void *self, WMNotification * notif)
120 WWindow *wwin = (WWindow *) self;
121 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
123 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
124 return;
126 if (flags & WFontSettings) {
127 wWindowConfigureBorders(wwin);
128 if (wwin->flags.shaded) {
129 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
131 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
132 wWindowSynthConfigureNotify(wwin);
135 if (flags & WTextureSettings) {
136 wwin->frame->flags.need_texture_remake = 1;
138 if (flags & (WTextureSettings | WColorSettings)) {
139 if (wwin->frame->titlebar)
140 XClearWindow(dpy, wwin->frame->titlebar->window);
142 wFrameWindowPaint(wwin->frame);
147 WWindow *wWindowFor(Window window)
149 WObjDescriptor *desc;
151 if (window == None)
152 return NULL;
154 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
155 return NULL;
157 if (desc->parent_type == WCLASS_WINDOW)
158 return desc->parent;
159 else if (desc->parent_type == WCLASS_FRAME) {
160 WFrameWindow *frame = (WFrameWindow *) desc->parent;
161 if (frame->flags.is_client_window_frame)
162 return frame->child;
165 return NULL;
168 WWindow *wWindowCreate()
170 WWindow *wwin;
172 wwin = wmalloc(sizeof(WWindow));
173 wretain(wwin);
175 memset(wwin, 0, sizeof(WWindow));
177 wwin->client_descriptor.handle_mousedown = frameMouseDown;
178 wwin->client_descriptor.parent = wwin;
179 wwin->client_descriptor.self = wwin;
180 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
182 return wwin;
185 void wWindowDestroy(WWindow *wwin)
187 int i;
189 if (wwin->screen_ptr->cmap_window == wwin)
190 wwin->screen_ptr->cmap_window = NULL;
192 WMRemoveNotificationObserver(wwin);
194 wwin->flags.destroyed = 1;
196 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
197 if (!wwin->screen_ptr->shortcutWindows[i])
198 continue;
200 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
202 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
203 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
204 wwin->screen_ptr->shortcutWindows[i] = NULL;
208 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
209 wwin->fake_group->retainCount--;
210 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
211 XDestroyWindow(dpy, wwin->fake_group->leader);
212 wwin->fake_group->leader = None;
213 wwin->fake_group->origLeader = None;
214 XFlush(dpy);
218 if (wwin->normal_hints)
219 XFree(wwin->normal_hints);
221 if (wwin->wm_hints)
222 XFree(wwin->wm_hints);
224 if (wwin->wm_instance)
225 XFree(wwin->wm_instance);
227 if (wwin->wm_class)
228 XFree(wwin->wm_class);
230 if (wwin->wm_gnustep_attr)
231 wfree(wwin->wm_gnustep_attr);
233 if (wwin->cmap_windows)
234 XFree(wwin->cmap_windows);
236 XDeleteContext(dpy, wwin->client_win, wWinContext);
238 if (wwin->frame)
239 wFrameWindowDestroy(wwin->frame);
241 if (wwin->icon) {
242 RemoveFromStackList(wwin->icon->core);
243 wIconDestroy(wwin->icon);
244 if (wPreferences.auto_arrange_icons)
245 wArrangeIcons(wwin->screen_ptr, True);
247 if (wwin->net_icon_image)
248 RReleaseImage(wwin->net_icon_image);
250 wrelease(wwin);
253 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
255 if (gs_hints->flags & GSWindowStyleAttr) {
256 if (gs_hints->window_style == WMBorderlessWindowMask) {
257 wwin->client_flags.no_border = 1;
258 wwin->client_flags.no_titlebar = 1;
259 wwin->client_flags.no_closable = 1;
260 wwin->client_flags.no_miniaturizable = 1;
261 wwin->client_flags.no_resizable = 1;
262 wwin->client_flags.no_close_button = 1;
263 wwin->client_flags.no_miniaturize_button = 1;
264 wwin->client_flags.no_resizebar = 1;
265 } else {
266 wwin->client_flags.no_close_button =
267 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
269 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
271 wwin->client_flags.no_miniaturize_button =
272 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
274 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
276 wwin->client_flags.no_resizebar =
277 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
279 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
281 /* these attributes supposedly imply in the existence
282 * of a titlebar */
283 if (gs_hints->window_style & (WMResizableWindowMask |
284 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
285 wwin->client_flags.no_titlebar = 0;
286 } else {
287 wwin->client_flags.no_titlebar =
288 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
292 } else {
293 /* setup the defaults */
294 wwin->client_flags.no_border = 0;
295 wwin->client_flags.no_titlebar = 0;
296 wwin->client_flags.no_closable = 0;
297 wwin->client_flags.no_miniaturizable = 0;
298 wwin->client_flags.no_resizable = 0;
299 wwin->client_flags.no_close_button = 0;
300 wwin->client_flags.no_miniaturize_button = 0;
301 wwin->client_flags.no_resizebar = 0;
303 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
304 wwin->client_flags.no_appicon = 1;
307 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
309 WScreen *scr = wwin->screen_ptr;
311 /* sets global default stuff */
312 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
314 * Decoration setting is done in this precedence (lower to higher)
315 * - use global default in the resource database
316 * - guess some settings
317 * - use GNUstep/external window attributes
318 * - set hints specified for the app in the resource DB
321 WSETUFLAG(wwin, broken_close, 0);
323 if (wwin->protocols.DELETE_WINDOW)
324 WSETUFLAG(wwin, kill_close, 0);
325 else
326 WSETUFLAG(wwin, kill_close, 1);
328 /* transients can't be iconified or maximized */
329 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
330 WSETUFLAG(wwin, no_miniaturizable, 1);
331 WSETUFLAG(wwin, no_miniaturize_button, 1);
334 /* if the window can't be resized, remove the resizebar */
335 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
336 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
337 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
338 WSETUFLAG(wwin, no_resizable, 1);
339 WSETUFLAG(wwin, no_resizebar, 1);
342 /* set GNUstep window attributes */
343 if (wwin->wm_gnustep_attr) {
344 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
346 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
348 *level = wwin->wm_gnustep_attr->window_level;
350 * INT_MIN is the only illegal window level.
352 if (*level == INT_MIN)
353 *level = INT_MIN + 1;
354 } else {
355 /* setup defaults */
356 *level = WMNormalLevel;
358 } else {
359 int tmp_workspace = -1;
360 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
361 Bool check;
363 check = False;
365 #ifdef MWM_HINTS
366 wMWMCheckClientHints(wwin);
367 #endif /* MWM_HINTS */
369 if (!check)
370 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
372 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
373 * have INT_MIN that means that no window level was requested. -Dan
375 if (tmp_level == INT_MIN) {
376 if (WFLAGP(wwin, floating))
377 *level = WMFloatingLevel;
378 else if (WFLAGP(wwin, sunken))
379 *level = WMSunkenLevel;
380 else
381 *level = WMNormalLevel;
382 } else {
383 *level = tmp_level;
386 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
387 WWindow *transientOwner = wWindowFor(wwin->transient_for);
388 if (transientOwner) {
389 int ownerLevel = transientOwner->frame->core->stacking->window_level;
390 if (ownerLevel > *level)
391 *level = ownerLevel;
395 if (tmp_workspace >= 0)
396 *workspace = tmp_workspace % scr->workspace_count;
400 * Set attributes specified only for that window/class.
401 * This might do duplicate work with the 1st wDefaultFillAttributes().
403 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
404 &wwin->user_flags, &wwin->defined_user_flags, False);
406 * Sanity checks for attributes that depend on other attributes
408 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
409 wwin->user_flags.emulate_appicon = 0;
411 if (wwin->main_window != None) {
412 WApplication *wapp = wApplicationOf(wwin->main_window);
413 if (wapp && !wapp->flags.emulated)
414 wwin->user_flags.emulate_appicon = 0;
417 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
418 wwin->user_flags.emulate_appicon = 0;
420 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
421 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
422 wwin->user_flags.sunken = 0;
424 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
426 /* windows that have takefocus=False shouldn't take focus at all */
427 if (wwin->focus_mode == WFM_NO_INPUT)
428 wwin->client_flags.no_focusable = 1;
431 Bool wWindowCanReceiveFocus(WWindow *wwin)
433 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
434 return False;
435 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
436 return False;
437 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
438 return False;
440 return True;
443 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
445 int w1, h1, w2, h2;
447 w1 = wwin->frame->core->width;
448 h1 = wwin->frame->core->height;
449 w2 = obscured->frame->core->width;
450 h2 = obscured->frame->core->height;
452 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
453 && wwin->frame->workspace != obscured->frame->workspace)
454 return False;
456 if (wwin->frame_x + w1 < obscured->frame_x
457 || wwin->frame_y + h1 < obscured->frame_y
458 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
459 return False;
461 return True;
464 static void fixLeaderProperties(WWindow *wwin)
466 XClassHint *classHint;
467 XWMHints *hints, *clientHints;
468 Window leaders[2], window;
469 char **argv, *command;
470 int argc, i, pid;
471 Bool haveCommand;
473 classHint = XAllocClassHint();
474 clientHints = XGetWMHints(dpy, wwin->client_win);
475 pid = wNETWMGetPidForWindow(wwin->client_win);
476 if (pid > 0)
477 haveCommand = GetCommandForPid(pid, &argv, &argc);
478 else
479 haveCommand = False;
481 leaders[0] = wwin->client_leader;
482 leaders[1] = wwin->group_id;
484 if (haveCommand) {
485 command = GetCommandForWindow(wwin->client_win);
486 if (command) {
487 /* command already set. nothing to do. */
488 wfree(command);
489 } else {
490 XSetCommand(dpy, wwin->client_win, argv, argc);
494 for (i = 0; i < 2; i++) {
495 window = leaders[i];
496 if (window) {
497 if (XGetClassHint(dpy, window, classHint) == 0) {
498 classHint->res_name = wwin->wm_instance;
499 classHint->res_class = wwin->wm_class;
500 XSetClassHint(dpy, window, classHint);
502 hints = XGetWMHints(dpy, window);
503 if (hints) {
504 XFree(hints);
505 } else if (clientHints) {
506 /* set window group leader to self */
507 clientHints->window_group = window;
508 clientHints->flags |= WindowGroupHint;
509 XSetWMHints(dpy, window, clientHints);
512 if (haveCommand) {
513 command = GetCommandForWindow(window);
514 if (command) {
515 /* command already set. nothing to do. */
516 wfree(command);
517 } else {
518 XSetCommand(dpy, window, argv, argc);
524 XFree(classHint);
525 if (clientHints)
526 XFree(clientHints);
527 if (haveCommand)
528 wfree(argv);
531 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
533 XClassHint *classHint;
534 XWMHints *hints;
535 Window leader;
536 int argc;
537 char **argv;
539 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
540 /* set class hint */
541 classHint = XAllocClassHint();
542 classHint->res_name = instance;
543 classHint->res_class = class;
544 XSetClassHint(dpy, leader, classHint);
545 XFree(classHint);
547 /* inherit these from the original leader if available */
548 hints = XGetWMHints(dpy, win);
549 if (!hints) {
550 hints = XAllocWMHints();
551 hints->flags = 0;
553 /* set window group leader to self */
554 hints->window_group = leader;
555 hints->flags |= WindowGroupHint;
556 XSetWMHints(dpy, leader, hints);
557 XFree(hints);
559 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
560 XSetCommand(dpy, leader, argv, argc);
561 XFreeStringList(argv);
564 return leader;
567 static int matchIdentifier(const void *item, const void *cdata)
569 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
573 *----------------------------------------------------------------
574 * wManageWindow--
575 * reparents the window and allocates a descriptor for it.
576 * Window manager hints and other hints are fetched to configure
577 * the window decoration attributes and others. User preferences
578 * for the window are used if available, to configure window
579 * decorations and some behaviour.
580 * If in startup, windows that are override redirect,
581 * unmapped and never were managed and are Withdrawn are not
582 * managed.
584 * Returns:
585 * the new window descriptor
587 * Side effects:
588 * The window is reparented and appropriate notification
589 * is done to the client. Input mask for the window is setup.
590 * The window descriptor is also associated with various window
591 * contexts and inserted in the head of the window list.
592 * Event handler contexts are associated for some objects
593 * (buttons, titlebar and resizebar)
595 *----------------------------------------------------------------
597 WWindow *wManageWindow(WScreen *scr, Window window)
599 WWindow *wwin;
600 int x, y;
601 unsigned width, height;
602 XWindowAttributes wattribs;
603 XSetWindowAttributes attribs;
604 WWindowState *win_state;
605 WWindow *transientOwner = NULL;
606 int window_level;
607 int wm_state;
608 int foo;
609 int workspace = -1;
610 char *title;
611 Bool withdraw = False;
612 Bool raise = False;
614 /* mutex. */
615 XGrabServer(dpy);
616 XSync(dpy, False);
617 /* make sure the window is still there */
618 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
619 XUngrabServer(dpy);
620 return NULL;
623 /* if it's an override-redirect, ignore it */
624 if (wattribs.override_redirect) {
625 XUngrabServer(dpy);
626 return NULL;
629 wm_state = PropGetWindowState(window);
631 /* if it's startup and the window is unmapped, don't manage it */
632 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
633 XUngrabServer(dpy);
634 return NULL;
637 wwin = wWindowCreate();
639 title = wNETWMGetWindowName(window);
640 if (title)
641 wwin->flags.net_has_title = 1;
642 if (!title && !wFetchName(dpy, window, &title))
643 title = NULL;
645 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
647 #ifdef SHAPE
648 if (wShapeSupported) {
649 int junk;
650 unsigned int ujunk;
651 int b_shaped;
653 XShapeSelectInput(dpy, window, ShapeNotifyMask);
654 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
655 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
656 wwin->flags.shaped = b_shaped;
658 #endif
661 * Get hints and other information in properties
663 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
665 /* setup descriptor */
666 wwin->client_win = window;
667 wwin->screen_ptr = scr;
668 wwin->old_border_width = wattribs.border_width;
669 wwin->event_mask = CLIENT_EVENTS;
670 attribs.event_mask = CLIENT_EVENTS;
671 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
672 attribs.save_under = False;
674 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
675 XSetWindowBorderWidth(dpy, window, 0);
677 /* get hints from GNUstep app */
678 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
679 wwin->flags.is_gnustep = 1;
681 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
682 wwin->wm_gnustep_attr = NULL;
685 wwin->client_leader = PropGetClientLeader(window);
686 if (wwin->client_leader != None)
687 wwin->main_window = wwin->client_leader;
689 wwin->wm_hints = XGetWMHints(dpy, window);
691 if (wwin->wm_hints) {
692 if (wwin->wm_hints->flags & StateHint) {
694 if (wwin->wm_hints->initial_state == IconicState) {
696 wwin->flags.miniaturized = 1;
698 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
700 withdraw = True;
704 if (wwin->wm_hints->flags & WindowGroupHint) {
705 wwin->group_id = wwin->wm_hints->window_group;
706 /* window_group has priority over CLIENT_LEADER */
707 wwin->main_window = wwin->group_id;
708 } else {
709 wwin->group_id = None;
712 if (wwin->wm_hints->flags & UrgencyHint)
713 wwin->flags.urgent = 1;
714 } else {
715 wwin->group_id = None;
718 PropGetProtocols(window, &wwin->protocols);
720 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
721 wwin->transient_for = None;
722 } else {
723 if (wwin->transient_for == None || wwin->transient_for == window) {
724 wwin->transient_for = scr->root_win;
725 } else {
726 transientOwner = wWindowFor(wwin->transient_for);
727 if (transientOwner && transientOwner->main_window != None)
728 wwin->main_window = transientOwner->main_window;
732 /* guess the focus mode */
733 wwin->focus_mode = getFocusMode(wwin);
735 /* get geometry stuff */
736 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
738 /* get colormap windows */
739 GetColormapWindows(wwin);
742 * Setup the decoration/window attributes and
743 * geometry
745 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
747 /* Make broken apps behave as a nice app. */
748 if (WFLAGP(wwin, emulate_appicon))
749 wwin->main_window = wwin->client_win;
751 fixLeaderProperties(wwin);
753 wwin->orig_main_window = wwin->main_window;
755 if (wwin->flags.is_gnustep)
756 WSETUFLAG(wwin, shared_appicon, 0);
758 if (wwin->main_window) {
759 extern Atom _XA_WINDOWMAKER_MENU;
760 XTextProperty text_prop;
762 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
763 WSETUFLAG(wwin, shared_appicon, 0);
767 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
768 char *buffer, *instance, *class;
769 WFakeGroupLeader *fPtr;
770 int index;
772 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
774 /* // only enter here if PropGetWMClass() succeds */
775 PropGetWMClass(wwin->main_window, &class, &instance);
776 buffer = StrConcatDot(instance, class);
778 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
779 if (index != WANotFound) {
780 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
781 if (fPtr->retainCount == 0) {
782 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
783 instance, class);
785 fPtr->retainCount++;
786 #undef method2
787 if (fPtr->origLeader == None) {
788 #ifdef method2
789 if (ADEQUATE(wwin->group_id)) {
790 fPtr->retainCount++;
791 fPtr->origLeader = wwin->group_id;
792 } else if (ADEQUATE(wwin->client_leader)) {
793 fPtr->retainCount++;
794 fPtr->origLeader = wwin->client_leader;
795 } else if (ADEQUATE(wwin->main_window)) {
796 fPtr->retainCount++;
797 fPtr->origLeader = wwin->main_window;
799 #else
800 if (ADEQUATE(wwin->main_window)) {
801 fPtr->retainCount++;
802 fPtr->origLeader = wwin->main_window;
804 #endif
806 wwin->fake_group = fPtr;
807 /*wwin->group_id = fPtr->leader; */
808 wwin->main_window = fPtr->leader;
809 wfree(buffer);
810 } else {
811 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
813 fPtr->identifier = buffer;
814 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
815 fPtr->origLeader = None;
816 fPtr->retainCount = 1;
818 WMAddToArray(scr->fakeGroupLeaders, fPtr);
820 #ifdef method2
821 if (ADEQUATE(wwin->group_id)) {
822 fPtr->retainCount++;
823 fPtr->origLeader = wwin->group_id;
824 } else if (ADEQUATE(wwin->client_leader)) {
825 fPtr->retainCount++;
826 fPtr->origLeader = wwin->client_leader;
827 } else if (ADEQUATE(wwin->main_window)) {
828 fPtr->retainCount++;
829 fPtr->origLeader = wwin->main_window;
831 #else
832 if (ADEQUATE(wwin->main_window)) {
833 fPtr->retainCount++;
834 fPtr->origLeader = wwin->main_window;
836 #endif
837 wwin->fake_group = fPtr;
838 /*wwin->group_id = fPtr->leader; */
839 wwin->main_window = fPtr->leader;
841 if (instance)
842 XFree(instance);
843 if (class)
844 XFree(class);
846 #undef method2
847 #undef ADEQUATE
851 * Setup the initial state of the window
853 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
854 wwin->flags.miniaturized = 1;
856 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
857 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
859 wNETWMCheckInitialClientState(wwin);
861 /* apply previous state if it exists and we're in startup */
862 if (scr->flags.startup && wm_state >= 0) {
864 if (wm_state == IconicState) {
866 wwin->flags.miniaturized = 1;
868 } else if (wm_state == WithdrawnState) {
870 withdraw = True;
874 /* if there is a saved state (from file), restore it */
875 win_state = NULL;
876 if (wwin->main_window != None)
877 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
878 else
879 win_state = (WWindowState *) wWindowGetSavedState(window);
881 if (win_state && !withdraw) {
882 if (win_state->state->hidden > 0)
883 wwin->flags.hidden = win_state->state->hidden;
885 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
886 wwin->flags.shaded = win_state->state->shaded;
888 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
889 wwin->flags.miniaturized = win_state->state->miniaturized;
892 if (!IS_OMNIPRESENT(wwin)) {
893 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
894 wwin->wm_class);
895 if (w < 0 || w >= scr->workspace_count) {
896 workspace = win_state->state->workspace;
897 if (workspace >= scr->workspace_count)
898 workspace = scr->current_workspace;
899 } else {
900 workspace = w;
902 } else {
903 workspace = scr->current_workspace;
907 /* if we're restarting, restore saved state (from hints).
908 * This will overwrite previous */
910 WSavedState *wstate;
912 if (getSavedState(window, &wstate)) {
913 wwin->flags.shaded = wstate->shaded;
914 wwin->flags.hidden = wstate->hidden;
915 wwin->flags.miniaturized = wstate->miniaturized;
916 wwin->flags.maximized = wstate->maximized;
917 if (wwin->flags.maximized) {
918 wwin->old_geometry.x = wstate->x;
919 wwin->old_geometry.y = wstate->y;
920 wwin->old_geometry.width = wstate->w;
921 wwin->old_geometry.height = wstate->h;
924 workspace = wstate->workspace;
925 } else {
926 wstate = NULL;
929 /* restore window shortcut */
930 if (wstate != NULL || win_state != NULL) {
931 unsigned mask = 0;
933 if (win_state != NULL)
934 mask = win_state->state->window_shortcuts;
936 if (wstate != NULL && mask == 0)
937 mask = wstate->window_shortcuts;
939 if (mask > 0) {
940 int i;
942 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
943 if (mask & (1 << i)) {
944 if (!scr->shortcutWindows[i])
945 scr->shortcutWindows[i] = WMCreateArray(4);
947 WMAddToArray(scr->shortcutWindows[i], wwin);
952 if (wstate != NULL)
953 wfree(wstate);
956 /* don't let transients start miniaturized if their owners are not */
957 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
958 wwin->flags.miniaturized = 0;
959 if (wwin->wm_hints)
960 wwin->wm_hints->initial_state = NormalState;
963 /* set workspace on which the window starts */
964 if (workspace >= 0) {
965 if (workspace > scr->workspace_count - 1) {
966 workspace = workspace % scr->workspace_count;
968 } else {
969 int w;
971 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
973 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
975 workspace = w;
977 } else {
978 if (wPreferences.open_transients_with_parent && transientOwner) {
980 workspace = transientOwner->frame->workspace;
982 } else {
984 workspace = scr->current_workspace;
989 /* setup window geometry */
990 if (win_state && win_state->state->w > 0) {
991 width = win_state->state->w;
992 height = win_state->state->h;
994 wWindowConstrainSize(wwin, &width, &height);
996 /* do not ask for window placement if the window is
997 * transient, during startup, if the initial workspace is another one
998 * or if the window wants to start iconic.
999 * If geometry was saved, restore it. */
1001 Bool dontBring = False;
1003 if (win_state && win_state->state->w > 0) {
1004 x = win_state->state->x;
1005 y = win_state->state->y;
1006 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1007 && !scr->flags.startup
1008 && workspace == scr->current_workspace
1009 && !wwin->flags.miniaturized
1010 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1012 if (transientOwner && transientOwner->flags.mapped) {
1013 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1014 WMRect rect;
1015 int head;
1017 x = transientOwner->frame_x +
1018 abs((transientOwner->frame->core->width - width) / 2) + offs;
1019 y = transientOwner->frame_y +
1020 abs((transientOwner->frame->core->height - height) / 3) + offs;
1023 * limit transient windows to be inside their parent's head
1025 rect.pos.x = transientOwner->frame_x;
1026 rect.pos.y = transientOwner->frame_y;
1027 rect.size.width = transientOwner->frame->core->width;
1028 rect.size.height = transientOwner->frame->core->height;
1030 head = wGetHeadForRect(scr, rect);
1031 rect = wGetRectForHead(scr, head);
1033 if (x < rect.pos.x)
1034 x = rect.pos.x;
1035 else if (x + width > rect.pos.x + rect.size.width)
1036 x = rect.pos.x + rect.size.width - width;
1038 if (y < rect.pos.y)
1039 y = rect.pos.y;
1040 else if (y + height > rect.pos.y + rect.size.height)
1041 y = rect.pos.y + rect.size.height - height;
1043 } else {
1044 PlaceWindow(wwin, &x, &y, width, height);
1046 if (wPreferences.window_placement == WPM_MANUAL) {
1047 dontBring = True;
1049 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1050 int head, flags;
1051 WMRect rect;
1052 int reposition = 0;
1055 * Make spash screens come out in the center of a head
1056 * trouble is that most splashies never get here
1057 * they are managed trough atoms but god knows where.
1058 * Dan, do you know ? -peter
1060 * Most of them are not managed, they have set
1061 * OverrideRedirect, which means we can't do anything about
1062 * them. -alfredo
1064 #if 0
1065 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width) / 2);
1066 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height) / 2);
1068 if ((unsigned)(x + (width - scr->scr_width) / 2 + 10) < 20 &&
1069 (unsigned)(y + (height - scr->scr_height) / 2 + 10) < 20) {
1071 reposition = 1;
1073 } else
1074 #endif
1077 * xinerama checks for: across head and dead space
1079 rect.pos.x = x;
1080 rect.pos.y = y;
1081 rect.size.width = width;
1082 rect.size.height = height;
1084 head = wGetRectPlacementInfo(scr, rect, &flags);
1086 if (flags & XFLAG_DEAD)
1087 reposition = 1;
1089 if (flags & XFLAG_MULTIPLE)
1090 reposition = 2;
1093 switch (reposition) {
1094 case 1:
1095 head = wGetHeadForPointerLocation(scr);
1096 rect = wGetRectForHead(scr, head);
1098 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1099 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1100 break;
1102 case 2:
1103 rect = wGetRectForHead(scr, head);
1105 if (x < rect.pos.x)
1106 x = rect.pos.x;
1107 else if (x + width > rect.pos.x + rect.size.width)
1108 x = rect.pos.x + rect.size.width - width;
1110 if (y < rect.pos.y)
1111 y = rect.pos.y;
1112 else if (y + height > rect.pos.y + rect.size.height)
1113 y = rect.pos.y + rect.size.height - height;
1115 break;
1117 default:
1118 break;
1122 if (WFLAGP(wwin, dont_move_off) && dontBring)
1123 wScreenBringInside(scr, &x, &y, width, height);
1126 wNETWMPositionSplash(wwin, &x, &y, width, height);
1128 if (wwin->flags.urgent) {
1129 if (!IS_OMNIPRESENT(wwin))
1130 wwin->flags.omnipresent ^= 1;
1134 * Create frame, borders and do reparenting
1136 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1137 #ifdef XKB_BUTTON_HINT
1138 if (wPreferences.modelock)
1139 foo |= WFF_LANGUAGE_BUTTON;
1140 #endif
1141 if (HAS_TITLEBAR(wwin))
1142 foo |= WFF_TITLEBAR;
1143 if (HAS_RESIZEBAR(wwin))
1144 foo |= WFF_RESIZEBAR;
1145 if (HAS_BORDER(wwin))
1146 foo |= WFF_BORDER;
1148 wwin->frame = wFrameWindowCreate(scr, window_level,
1149 x, y, width, height,
1150 &wPreferences.window_title_clearance, foo,
1151 scr->window_title_texture,
1152 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1154 wwin->frame->flags.is_client_window_frame = 1;
1155 wwin->frame->flags.justification = wPreferences.title_justification;
1157 /* setup button images */
1158 wWindowUpdateButtonImages(wwin);
1160 /* hide unused buttons */
1161 foo = 0;
1162 if (WFLAGP(wwin, no_close_button))
1163 foo |= WFF_RIGHT_BUTTON;
1164 if (WFLAGP(wwin, no_miniaturize_button))
1165 foo |= WFF_LEFT_BUTTON;
1166 #ifdef XKB_BUTTON_HINT
1167 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1168 foo |= WFF_LANGUAGE_BUTTON;
1169 #endif
1170 if (foo != 0)
1171 wFrameWindowHideButton(wwin->frame, foo);
1173 wwin->frame->child = wwin;
1174 wwin->frame->workspace = workspace;
1175 wwin->frame->on_click_left = windowIconifyClick;
1177 #ifdef XKB_BUTTON_HINT
1178 if (wPreferences.modelock)
1179 wwin->frame->on_click_language = windowLanguageClick;
1180 #endif
1182 wwin->frame->on_click_right = windowCloseClick;
1183 wwin->frame->on_dblclick_right = windowCloseDblClick;
1184 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1185 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1186 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1188 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1189 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1190 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1193 int gx, gy;
1195 wClientGetGravityOffsets(wwin, &gx, &gy);
1197 /* if gravity is to the south, account for the border sizes */
1198 if (gy > 0)
1199 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1203 * wWindowConfigure() will init the client window's size
1204 * (wwin->client.{width,height}) and all other geometry
1205 * related variables (frame_x,frame_y)
1207 wWindowConfigure(wwin, x, y, width, height);
1209 /* to make sure the window receives it's new position after reparenting */
1210 wWindowSynthConfigureNotify(wwin);
1213 * Setup descriptors and save window to internal
1214 * lists
1216 if (wwin->main_window != None) {
1217 WApplication *app;
1218 WWindow *leader;
1220 /* Leader windows do not necessary set themselves as leaders.
1221 * If this is the case, point the leader of this window to
1222 * itself */
1223 leader = wWindowFor(wwin->main_window);
1224 if (leader && leader->main_window == None) {
1225 leader->main_window = leader->client_win;
1227 app = wApplicationCreate(wwin);
1228 if (app) {
1229 app->last_workspace = workspace;
1232 * Do application specific stuff, like setting application
1233 * wide attributes.
1236 if (wwin->flags.hidden) {
1237 /* if the window was set to hidden because it was hidden
1238 * in a previous incarnation and that state was restored */
1239 app->flags.hidden = 1;
1240 } else if (app->flags.hidden) {
1241 if (WFLAGP(app->main_window_desc, start_hidden)) {
1242 wwin->flags.hidden = 1;
1243 } else {
1244 wUnhideApplication(app, False, False);
1245 raise = True;
1251 /* setup the frame descriptor */
1252 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1253 wwin->frame->core->descriptor.parent = wwin;
1254 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1256 /* don't let windows go away if we die */
1257 XAddToSaveSet(dpy, window);
1259 XLowerWindow(dpy, window);
1261 /* if window is in this workspace and should be mapped, then map it */
1262 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1263 && !wwin->flags.hidden && !withdraw) {
1265 /* The following "if" is to avoid crashing of clients that expect
1266 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1267 * after the return from this function.
1269 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1270 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1271 } else {
1272 wClientSetState(wwin, NormalState, None);
1275 #if 0
1276 /* if not auto focus, then map the window under the currently
1277 * focused window */
1278 #define _WIDTH(w) (w)->frame->core->width
1279 #define _HEIGHT(w) (w)->frame->core->height
1280 if (!wPreferences.auto_focus && scr->focused_window
1281 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1282 && (_WIDTH(wwin) >
1283 (_WIDTH(scr->focused_window) * 5) / 3
1284 || _HEIGHT(wwin) >
1285 (_HEIGHT(scr->focused_window) * 5) / 3)
1286 && WINDOW_LEVEL(scr->focused_window) ==
1287 WINDOW_LEVEL(wwin))
1288 || wwin->flags.maximized)) {
1289 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1291 #undef _WIDTH
1292 #undef _HEIGHT
1294 #endif
1296 if (wPreferences.superfluous && !wPreferences.no_animations
1297 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1299 * The brain damaged idiotic non-click to focus modes will
1300 * have trouble with this because:
1302 * 1. window is created and mapped by the client
1303 * 2. window is mapped by wmaker in small size
1304 * 3. window is animated to grow to normal size
1305 * 4. this function returns to normal event loop
1306 * 5. eventually, the EnterNotify event that would trigger
1307 * the window focusing (if the mouse is over that window)
1308 * will be processed by wmaker.
1309 * But since this event will be rather delayed
1310 * (step 3 has a large delay) the time when the event ocurred
1311 * and when it is processed, the client that owns that window
1312 * will reject the XSetInputFocus() for it.
1314 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1315 DoWindowBirth(wwin);
1318 wWindowMap(wwin);
1321 /* setup stacking descriptor */
1322 if (transientOwner) {
1323 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1324 } else {
1325 wwin->frame->core->stacking->child_of = NULL;
1328 if (!scr->focused_window) {
1329 /* first window on the list */
1330 wwin->next = NULL;
1331 wwin->prev = NULL;
1332 scr->focused_window = wwin;
1333 } else {
1334 WWindow *tmp;
1336 /* add window at beginning of focus window list */
1337 tmp = scr->focused_window;
1338 while (tmp->prev)
1339 tmp = tmp->prev;
1340 tmp->prev = wwin;
1341 wwin->next = tmp;
1342 wwin->prev = NULL;
1345 /* raise is set to true if we un-hid the app when this window was born.
1346 * we raise, else old windows of this app will be above this new one. */
1347 if (raise) {
1348 wRaiseFrame(wwin->frame->core);
1351 /* Update name must come after WApplication stuff is done */
1352 wWindowUpdateName(wwin, title);
1353 if (title)
1354 XFree(title);
1356 XUngrabServer(dpy);
1359 * Final preparations before window is ready to go
1361 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1363 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1364 if (((transientOwner && transientOwner->flags.focused)
1365 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1367 /* only auto_focus if on same screen as mouse
1368 * (and same head for xinerama mode)
1369 * TODO: make it an option */
1371 /*TODO add checking the head of the window, is it available? */
1372 short same_screen = 0, same_head = 1;
1374 int foo;
1375 unsigned int bar;
1376 Window dummy;
1378 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1379 &foo, &foo, &foo, &foo, &bar) != False) {
1380 same_screen = 1;
1383 if (same_screen == 1 && same_head == 1) {
1384 wSetFocusTo(scr, wwin);
1388 wWindowResetMouseGrabs(wwin);
1390 if (!WFLAGP(wwin, no_bind_keys))
1391 wWindowSetKeyGrabs(wwin);
1393 WMPostNotificationName(WMNManaged, wwin, NULL);
1394 wColormapInstallForWindow(scr, scr->cmap_window);
1396 /* Setup Notification Observers */
1397 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1399 /* Cleanup temporary stuff */
1400 if (win_state)
1401 wWindowDeleteSavedState(win_state);
1403 /* If the window must be withdrawed, then do it now.
1404 * Must do some optimization, 'though */
1405 if (withdraw) {
1406 wwin->flags.mapped = 0;
1407 wClientSetState(wwin, WithdrawnState, None);
1408 wUnmanageWindow(wwin, True, False);
1409 wwin = NULL;
1412 return wwin;
1415 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1416 char *title, int x, int y, int width, int height)
1418 WWindow *wwin;
1419 int foo;
1421 wwin = wWindowCreate();
1423 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1425 wwin->flags.internal_window = 1;
1427 WSETUFLAG(wwin, omnipresent, 1);
1428 WSETUFLAG(wwin, no_shadeable, 1);
1429 WSETUFLAG(wwin, no_resizable, 1);
1430 WSETUFLAG(wwin, no_miniaturizable, 1);
1432 wwin->focus_mode = WFM_PASSIVE;
1433 wwin->client_win = window;
1434 wwin->screen_ptr = scr;
1435 wwin->transient_for = owner;
1436 wwin->client.x = x;
1437 wwin->client.y = y;
1438 wwin->client.width = width;
1439 wwin->client.height = height;
1440 wwin->frame_x = wwin->client.x;
1441 wwin->frame_y = wwin->client.y;
1443 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1444 foo |= WFF_TITLEBAR;
1445 #ifdef XKB_BUTTON_HINT
1446 foo |= WFF_LANGUAGE_BUTTON;
1447 #endif
1449 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1450 wwin->frame_x, wwin->frame_y,
1451 width, height,
1452 &wPreferences.window_title_clearance, foo,
1453 scr->window_title_texture,
1454 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1456 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1458 wwin->frame->flags.is_client_window_frame = 1;
1459 wwin->frame->flags.justification = wPreferences.title_justification;
1461 wFrameWindowChangeTitle(wwin->frame, title);
1463 /* setup button images */
1464 wWindowUpdateButtonImages(wwin);
1466 /* hide buttons */
1467 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1469 wwin->frame->child = wwin;
1470 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1472 #ifdef XKB_BUTTON_HINT
1473 if (wPreferences.modelock)
1474 wwin->frame->on_click_language = windowLanguageClick;
1475 #endif
1477 wwin->frame->on_click_right = windowCloseClick;
1478 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1479 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1480 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1481 wwin->client.y += wwin->frame->top_width;
1483 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1484 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1486 /* setup the frame descriptor */
1487 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1488 wwin->frame->core->descriptor.parent = wwin;
1489 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1491 XLowerWindow(dpy, window);
1492 XMapSubwindows(dpy, wwin->frame->core->window);
1494 /* setup stacking descriptor */
1495 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1496 WWindow *tmp;
1497 tmp = wWindowFor(wwin->transient_for);
1498 if (tmp)
1499 wwin->frame->core->stacking->child_of = tmp->frame->core;
1500 } else {
1501 wwin->frame->core->stacking->child_of = NULL;
1504 if (!scr->focused_window) {
1505 /* first window on the list */
1506 wwin->next = NULL;
1507 wwin->prev = NULL;
1508 scr->focused_window = wwin;
1509 } else {
1510 WWindow *tmp;
1512 /* add window at beginning of focus window list */
1513 tmp = scr->focused_window;
1514 while (tmp->prev)
1515 tmp = tmp->prev;
1516 tmp->prev = wwin;
1517 wwin->next = tmp;
1518 wwin->prev = NULL;
1521 if (wwin->flags.is_gnustep == 0)
1522 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1524 /* if (wPreferences.auto_focus) */
1525 wSetFocusTo(scr, wwin);
1526 wWindowResetMouseGrabs(wwin);
1527 wWindowSetKeyGrabs(wwin);
1529 return wwin;
1533 *----------------------------------------------------------------------
1534 * wUnmanageWindow--
1535 * Removes the frame window from a window and destroys all data
1536 * related to it. The window will be reparented back to the root window
1537 * if restore is True.
1539 * Side effects:
1540 * Everything related to the window is destroyed and the window
1541 * is removed from the window lists. Focus is set to the previous on the
1542 * window list.
1543 *----------------------------------------------------------------------
1545 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1547 WCoreWindow *frame = wwin->frame->core;
1548 WWindow *owner = NULL;
1549 WWindow *newFocusedWindow = NULL;
1550 int wasFocused;
1551 WScreen *scr = wwin->screen_ptr;
1553 /* First close attribute editor window if open */
1554 if (wwin->flags.inspector_open) {
1555 wCloseInspectorForWindow(wwin);
1558 /* Close window menu if it's open for this window */
1559 if (wwin->flags.menu_open_for_me) {
1560 CloseWindowMenu(scr);
1563 if (!destroyed) {
1564 if (!wwin->flags.internal_window)
1565 XRemoveFromSaveSet(dpy, wwin->client_win);
1567 XSelectInput(dpy, wwin->client_win, NoEventMask);
1569 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1570 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1573 XUnmapWindow(dpy, frame->window);
1575 XUnmapWindow(dpy, wwin->client_win);
1577 /* deselect window */
1578 wSelectWindow(wwin, False);
1580 /* remove all pending events on window */
1581 /* I think this only matters for autoraise */
1582 if (wPreferences.raise_delay)
1583 WMDeleteTimerWithClientData(wwin->frame->core);
1585 XFlush(dpy);
1587 /* reparent the window back to the root */
1588 if (restore)
1589 wClientRestore(wwin);
1591 if (wwin->transient_for != scr->root_win) {
1592 owner = wWindowFor(wwin->transient_for);
1593 if (owner) {
1594 if (!owner->flags.semi_focused) {
1595 owner = NULL;
1596 } else {
1597 owner->flags.semi_focused = 0;
1602 wasFocused = wwin->flags.focused;
1604 /* remove from window focus list */
1605 if (!wwin->prev && !wwin->next) {
1606 /* was the only window */
1607 scr->focused_window = NULL;
1608 newFocusedWindow = NULL;
1609 } else {
1610 WWindow *tmp;
1612 if (wwin->prev)
1613 wwin->prev->next = wwin->next;
1614 if (wwin->next)
1615 wwin->next->prev = wwin->prev;
1616 else {
1617 scr->focused_window = wwin->prev;
1618 scr->focused_window->next = NULL;
1621 if (wPreferences.focus_mode == WKF_CLICK) {
1623 /* if in click to focus mode and the window
1624 * was a transient, focus the owner window
1626 tmp = NULL;
1627 if (wPreferences.focus_mode == WKF_CLICK) {
1628 tmp = wWindowFor(wwin->transient_for);
1629 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1630 tmp = NULL;
1633 /* otherwise, focus the next one in the focus list */
1634 if (!tmp) {
1635 tmp = scr->focused_window;
1636 while (tmp) { /* look for one in the window list first */
1637 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1638 && (tmp->flags.mapped || tmp->flags.shaded))
1639 break;
1640 tmp = tmp->prev;
1642 if (!tmp) { /* if unsuccessful, choose any focusable window */
1643 tmp = scr->focused_window;
1644 while (tmp) {
1645 if (!WFLAGP(tmp, no_focusable)
1646 && (tmp->flags.mapped || tmp->flags.shaded))
1647 break;
1648 tmp = tmp->prev;
1653 newFocusedWindow = tmp;
1655 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1656 unsigned int mask;
1657 int foo;
1658 Window bar, win;
1660 /* This is to let the root window get the keyboard input
1661 * if Sloppy focus mode and no other window get focus.
1662 * This way keybindings will not freeze.
1664 tmp = NULL;
1665 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1666 tmp = wWindowFor(win);
1667 if (tmp == wwin)
1668 tmp = NULL;
1669 newFocusedWindow = tmp;
1670 } else {
1671 newFocusedWindow = NULL;
1675 if (!wwin->flags.internal_window)
1676 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1677 if (wasFocused) {
1678 if (newFocusedWindow != owner && owner) {
1679 if (wwin->flags.is_gnustep == 0)
1680 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1682 wSetFocusTo(scr, newFocusedWindow);
1684 wWindowDestroy(wwin);
1685 XFlush(dpy);
1688 void wWindowMap(WWindow *wwin)
1690 XMapWindow(dpy, wwin->frame->core->window);
1691 if (!wwin->flags.shaded) {
1692 /* window will be remapped when getting MapNotify */
1693 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1694 XMapWindow(dpy, wwin->client_win);
1695 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1697 wwin->flags.mapped = 1;
1701 void wWindowUnmap(WWindow *wwin)
1703 wwin->flags.mapped = 0;
1705 /* prevent window withdrawal when getting UnmapNotify */
1706 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1707 XUnmapWindow(dpy, wwin->client_win);
1708 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1710 XUnmapWindow(dpy, wwin->frame->core->window);
1713 void wWindowFocus(WWindow *wwin, WWindow *owin)
1715 WWindow *nowner;
1716 WWindow *oowner;
1718 #ifdef KEEP_XKB_LOCK_STATUS
1719 if (wPreferences.modelock) {
1720 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1722 #endif /* KEEP_XKB_LOCK_STATUS */
1724 wwin->flags.semi_focused = 0;
1726 if (wwin->flags.is_gnustep == 0)
1727 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1729 wwin->flags.focused = 1;
1731 wWindowResetMouseGrabs(wwin);
1733 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1735 if (owin == wwin || !owin)
1736 return;
1738 nowner = wWindowFor(wwin->transient_for);
1740 /* new window is a transient for the old window */
1741 if (nowner == owin) {
1742 owin->flags.semi_focused = 1;
1743 wWindowUnfocus(nowner);
1744 return;
1747 oowner = wWindowFor(owin->transient_for);
1749 /* new window is owner of old window */
1750 if (wwin == oowner) {
1751 wWindowUnfocus(owin);
1752 return;
1755 if (!nowner) {
1756 wWindowUnfocus(owin);
1757 return;
1760 /* new window has same owner of old window */
1761 if (oowner == nowner) {
1762 /* prevent unfocusing of owner */
1763 oowner->flags.semi_focused = 0;
1764 wWindowUnfocus(owin);
1765 oowner->flags.semi_focused = 1;
1767 return;
1770 /* nowner != NULL && oowner != nowner */
1771 nowner->flags.semi_focused = 1;
1772 wWindowUnfocus(nowner);
1773 wWindowUnfocus(owin);
1776 void wWindowUnfocus(WWindow *wwin)
1778 CloseWindowMenu(wwin->screen_ptr);
1780 if (wwin->flags.is_gnustep == 0)
1781 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1783 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1784 WWindow *owner;
1785 owner = wWindowFor(wwin->transient_for);
1786 if (owner && owner->flags.semi_focused) {
1787 owner->flags.semi_focused = 0;
1788 if (owner->flags.mapped || owner->flags.shaded) {
1789 wWindowUnfocus(owner);
1790 wFrameWindowPaint(owner->frame);
1794 wwin->flags.focused = 0;
1795 wWindowResetMouseGrabs(wwin);
1796 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1799 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1801 char *title;
1803 if (!wwin->frame)
1804 return;
1806 wwin->flags.wm_name_changed = 1;
1808 if (!newTitle) {
1809 /* the hint was removed */
1810 title = DEF_WINDOW_TITLE;
1811 } else {
1812 title = newTitle;
1815 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1816 WMPostNotificationName(WMNChangedName, wwin, NULL);
1821 *----------------------------------------------------------------------
1823 * wWindowConstrainSize--
1824 * Constrains size for the client window, taking the maximal size,
1825 * window resize increments and other size hints into account.
1827 * Returns:
1828 * The closest size to what was given that the client window can
1829 * have.
1831 *----------------------------------------------------------------------
1833 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1835 int width = (int)*nwidth;
1836 int height = (int)*nheight;
1837 int winc = 1;
1838 int hinc = 1;
1839 int minW = 1, minH = 1;
1840 int maxW = wwin->screen_ptr->scr_width * 2;
1841 int maxH = wwin->screen_ptr->scr_height * 2;
1842 int minAX = -1, minAY = -1;
1843 int maxAX = -1, maxAY = -1;
1844 int baseW = 0;
1845 int baseH = 0;
1847 if (wwin->normal_hints) {
1848 winc = wwin->normal_hints->width_inc;
1849 hinc = wwin->normal_hints->height_inc;
1850 minW = wwin->normal_hints->min_width;
1851 minH = wwin->normal_hints->min_height;
1852 maxW = wwin->normal_hints->max_width;
1853 maxH = wwin->normal_hints->max_height;
1854 if (wwin->normal_hints->flags & PAspect) {
1855 minAX = wwin->normal_hints->min_aspect.x;
1856 minAY = wwin->normal_hints->min_aspect.y;
1857 maxAX = wwin->normal_hints->max_aspect.x;
1858 maxAY = wwin->normal_hints->max_aspect.y;
1861 baseW = wwin->normal_hints->base_width;
1862 baseH = wwin->normal_hints->base_height;
1865 if (width < minW)
1866 width = minW;
1867 if (height < minH)
1868 height = minH;
1870 if (width > maxW)
1871 width = maxW;
1872 if (height > maxH)
1873 height = maxH;
1875 /* aspect ratio code borrowed from olwm */
1876 if (minAX > 0) {
1877 /* adjust max aspect ratio */
1878 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1879 if (maxAX > maxAY) {
1880 height = (width * maxAY) / maxAX;
1881 if (height > maxH) {
1882 height = maxH;
1883 width = (height * maxAX) / maxAY;
1885 } else {
1886 width = (height * maxAX) / maxAY;
1887 if (width > maxW) {
1888 width = maxW;
1889 height = (width * maxAY) / maxAX;
1894 /* adjust min aspect ratio */
1895 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1896 if (minAX > minAY) {
1897 height = (width * minAY) / minAX;
1898 if (height < minH) {
1899 height = minH;
1900 width = (height * minAX) / minAY;
1902 } else {
1903 width = (height * minAX) / minAY;
1904 if (width < minW) {
1905 width = minW;
1906 height = (width * minAY) / minAX;
1912 if (baseW != 0)
1913 width = (((width - baseW) / winc) * winc) + baseW;
1914 else
1915 width = (((width - minW) / winc) * winc) + minW;
1917 if (baseH != 0)
1918 height = (((height - baseH) / hinc) * hinc) + baseH;
1919 else
1920 height = (((height - minH) / hinc) * hinc) + minH;
1922 /* broken stupid apps may cause preposterous values for these.. */
1923 if (width > 0)
1924 *nwidth = width;
1925 if (height > 0)
1926 *nheight = height;
1929 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1930 unsigned int *width, unsigned int *height)
1932 int baseW = 0, baseH = 0;
1933 int winc = 1, hinc = 1;
1935 if (wwin->normal_hints) {
1936 baseW = wwin->normal_hints->base_width;
1937 baseH = wwin->normal_hints->base_height;
1939 winc = wwin->normal_hints->width_inc;
1940 hinc = wwin->normal_hints->height_inc;
1943 if (*width > maxW)
1944 *width = maxW - (maxW - baseW) % winc;
1946 if (*height > maxH)
1947 *height = maxH - (maxH - baseH) % hinc;
1950 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1952 WScreen *scr = wwin->screen_ptr;
1953 WApplication *wapp;
1954 int unmap = 0;
1956 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1957 return;
1959 if (workspace != scr->current_workspace) {
1960 /* Sent to other workspace. Unmap window */
1961 if ((wwin->flags.mapped
1962 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1963 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1965 wapp = wApplicationOf(wwin->main_window);
1966 if (wapp) {
1967 wapp->last_workspace = workspace;
1969 if (wwin->flags.miniaturized) {
1970 if (wwin->icon) {
1971 XUnmapWindow(dpy, wwin->icon->core->window);
1972 wwin->icon->mapped = 0;
1974 } else {
1975 unmap = 1;
1976 wSetFocusTo(scr, NULL);
1979 } else {
1980 /* brought to current workspace. Map window */
1981 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1982 if (wwin->icon) {
1983 XMapWindow(dpy, wwin->icon->core->window);
1984 wwin->icon->mapped = 1;
1986 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1987 wWindowMap(wwin);
1990 if (!IS_OMNIPRESENT(wwin)) {
1991 int oldWorkspace = wwin->frame->workspace;
1993 wwin->frame->workspace = workspace;
1995 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1998 if (unmap)
1999 wWindowUnmap(wwin);
2002 void wWindowSynthConfigureNotify(WWindow *wwin)
2004 XEvent sevent;
2006 sevent.type = ConfigureNotify;
2007 sevent.xconfigure.display = dpy;
2008 sevent.xconfigure.event = wwin->client_win;
2009 sevent.xconfigure.window = wwin->client_win;
2011 sevent.xconfigure.x = wwin->client.x;
2012 sevent.xconfigure.y = wwin->client.y;
2013 sevent.xconfigure.width = wwin->client.width;
2014 sevent.xconfigure.height = wwin->client.height;
2016 sevent.xconfigure.border_width = wwin->old_border_width;
2017 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2018 sevent.xconfigure.above = wwin->frame->titlebar->window;
2019 else
2020 sevent.xconfigure.above = None;
2022 sevent.xconfigure.override_redirect = False;
2023 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2024 XFlush(dpy);
2028 *----------------------------------------------------------------------
2029 * wWindowConfigure()
2031 * req_x, req_y: new requested positions for the frame
2032 * req_width, req_height: new requested sizes for the client
2034 * Configures the frame, decorations and client window to the specified
2035 * geometry, whose validity is not checked -- wWindowConstrainSize()
2036 * must be used for that.
2037 * The size parameters are for the client window, but the position is
2038 * for the frame.
2039 * The client window receives a ConfigureNotify event, according
2040 * to what ICCCM says.
2042 * Returns:
2043 * None
2045 * Side effects:
2046 * Window size and position are changed and client window receives
2047 * a ConfigureNotify event.
2048 *----------------------------------------------------------------------
2050 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2052 int synth_notify = False;
2053 int resize;
2055 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2057 * if the window is being moved but not resized then
2058 * send a synthetic ConfigureNotify
2060 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2061 synth_notify = True;
2064 if (WFLAGP(wwin, dont_move_off))
2065 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2066 if (resize) {
2067 if (req_width < MIN_WINDOW_SIZE)
2068 req_width = MIN_WINDOW_SIZE;
2069 if (req_height < MIN_WINDOW_SIZE)
2070 req_height = MIN_WINDOW_SIZE;
2072 /* If growing, resize inner part before frame,
2073 * if shrinking, resize frame before.
2074 * This will prevent the frame (that can have a different color)
2075 * to be exposed, causing flicker */
2076 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2077 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2079 if (wwin->flags.shaded) {
2080 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2081 wwin->old_geometry.height = req_height;
2082 } else {
2083 int h;
2085 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2087 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2090 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2091 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2093 wwin->client.x = req_x;
2094 wwin->client.y = req_y + wwin->frame->top_width;
2095 wwin->client.width = req_width;
2096 wwin->client.height = req_height;
2097 } else {
2098 wwin->client.x = req_x;
2099 wwin->client.y = req_y + wwin->frame->top_width;
2101 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2103 wwin->frame_x = req_x;
2104 wwin->frame_y = req_y;
2105 if (HAS_BORDER(wwin)) {
2106 wwin->client.x += FRAME_BORDER_WIDTH;
2107 wwin->client.y += FRAME_BORDER_WIDTH;
2109 #ifdef SHAPE
2110 if (wShapeSupported && wwin->flags.shaped && resize) {
2111 wWindowSetShape(wwin);
2113 #endif
2115 if (synth_notify)
2116 wWindowSynthConfigureNotify(wwin);
2117 XFlush(dpy);
2120 /* req_x, req_y: new position of the frame */
2121 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2123 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2124 int synth_notify = False;
2126 /* Send a synthetic ConfigureNotify event for every window movement. */
2127 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2128 synth_notify = True;
2130 #else
2131 /* A single synthetic ConfigureNotify event is sent at the end of
2132 * a completed (opaque) movement in moveres.c */
2133 #endif
2135 if (WFLAGP(wwin, dont_move_off))
2136 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2137 wwin->frame->core->width, wwin->frame->core->height);
2139 wwin->client.x = req_x;
2140 wwin->client.y = req_y + wwin->frame->top_width;
2141 if (HAS_BORDER(wwin)) {
2142 wwin->client.x += FRAME_BORDER_WIDTH;
2143 wwin->client.y += FRAME_BORDER_WIDTH;
2146 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2148 wwin->frame_x = req_x;
2149 wwin->frame_y = req_y;
2151 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2152 if (synth_notify)
2153 wWindowSynthConfigureNotify(wwin);
2154 #endif
2157 void wWindowUpdateButtonImages(WWindow *wwin)
2159 WScreen *scr = wwin->screen_ptr;
2160 Pixmap pixmap, mask;
2161 WFrameWindow *fwin = wwin->frame;
2163 if (!HAS_TITLEBAR(wwin))
2164 return;
2166 /* miniaturize button */
2167 if (!WFLAGP(wwin, no_miniaturize_button)) {
2168 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2169 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2171 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2172 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2173 } else {
2174 mask = None;
2177 if (fwin->lbutton_image
2178 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2179 wPixmapDestroy(fwin->lbutton_image);
2180 fwin->lbutton_image = NULL;
2183 if (!fwin->lbutton_image) {
2184 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2185 fwin->lbutton_image->client_owned = 1;
2186 fwin->lbutton_image->client_owned_mask = 1;
2188 } else {
2189 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2190 wPixmapDestroy(fwin->lbutton_image);
2192 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2195 #ifdef XKB_BUTTON_HINT
2196 if (!WFLAGP(wwin, no_language_button)) {
2197 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2198 wPixmapDestroy(fwin->languagebutton_image);
2200 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2202 #endif
2204 /* close button */
2206 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2207 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2209 if (!WFLAGP(wwin, no_close_button)) {
2210 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2211 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2213 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2214 mask = wwin->wm_gnustep_attr->close_mask;
2215 else
2216 mask = None;
2218 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2219 || fwin->rbutton_image->mask != mask)) {
2220 wPixmapDestroy(fwin->rbutton_image);
2221 fwin->rbutton_image = NULL;
2224 if (!fwin->rbutton_image) {
2225 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2226 fwin->rbutton_image->client_owned = 1;
2227 fwin->rbutton_image->client_owned_mask = 1;
2230 } else if (WFLAGP(wwin, kill_close)) {
2232 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2233 wPixmapDestroy(fwin->rbutton_image);
2235 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2237 } else if (MGFLAGP(wwin, broken_close)) {
2239 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2240 wPixmapDestroy(fwin->rbutton_image);
2242 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2244 } else {
2246 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2247 wPixmapDestroy(fwin->rbutton_image);
2249 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2253 /* force buttons to be redrawn */
2254 fwin->flags.need_texture_change = 1;
2255 wFrameWindowPaint(fwin);
2259 *---------------------------------------------------------------------------
2260 * wWindowConfigureBorders--
2261 * Update window border configuration according to attribute flags.
2263 *---------------------------------------------------------------------------
2265 void wWindowConfigureBorders(WWindow *wwin)
2267 if (wwin->frame) {
2268 int flags;
2269 int newy, oldh;
2271 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2273 #ifdef XKB_BUTTON_HINT
2274 if (wPreferences.modelock)
2275 flags |= WFF_LANGUAGE_BUTTON;
2276 #endif
2278 if (HAS_TITLEBAR(wwin))
2279 flags |= WFF_TITLEBAR;
2280 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2281 flags |= WFF_RESIZEBAR;
2282 if (HAS_BORDER(wwin))
2283 flags |= WFF_BORDER;
2284 if (wwin->flags.shaded)
2285 flags |= WFF_IS_SHADED;
2287 oldh = wwin->frame->top_width;
2288 wFrameWindowUpdateBorders(wwin->frame, flags);
2289 if (oldh != wwin->frame->top_width) {
2290 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2292 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2293 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2296 flags = 0;
2297 if (!WFLAGP(wwin, no_miniaturize_button)
2298 && wwin->frame->flags.hide_left_button)
2299 flags |= WFF_LEFT_BUTTON;
2301 #ifdef XKB_BUTTON_HINT
2302 if (!WFLAGP(wwin, no_language_button)
2303 && wwin->frame->flags.hide_language_button) {
2304 flags |= WFF_LANGUAGE_BUTTON;
2306 #endif
2308 if (!WFLAGP(wwin, no_close_button)
2309 && wwin->frame->flags.hide_right_button)
2310 flags |= WFF_RIGHT_BUTTON;
2312 if (flags != 0) {
2313 wWindowUpdateButtonImages(wwin);
2314 wFrameWindowShowButton(wwin->frame, flags);
2317 flags = 0;
2318 if (WFLAGP(wwin, no_miniaturize_button)
2319 && !wwin->frame->flags.hide_left_button)
2320 flags |= WFF_LEFT_BUTTON;
2322 #ifdef XKB_BUTTON_HINT
2323 if (WFLAGP(wwin, no_language_button)
2324 && !wwin->frame->flags.hide_language_button)
2325 flags |= WFF_LANGUAGE_BUTTON;
2326 #endif
2328 if (WFLAGP(wwin, no_close_button)
2329 && !wwin->frame->flags.hide_right_button)
2330 flags |= WFF_RIGHT_BUTTON;
2332 if (flags != 0)
2333 wFrameWindowHideButton(wwin->frame, flags);
2335 #ifdef SHAPE
2336 if (wShapeSupported && wwin->flags.shaped) {
2337 wWindowSetShape(wwin);
2339 #endif
2343 void wWindowSaveState(WWindow * wwin)
2345 CARD32 data[10];
2346 int i;
2348 memset(data, 0, sizeof(CARD32) * 10);
2349 data[0] = wwin->frame->workspace;
2350 data[1] = wwin->flags.miniaturized;
2351 data[2] = wwin->flags.shaded;
2352 data[3] = wwin->flags.hidden;
2353 data[4] = wwin->flags.maximized;
2354 if (wwin->flags.maximized == 0) {
2355 data[5] = wwin->frame_x;
2356 data[6] = wwin->frame_y;
2357 data[7] = wwin->frame->core->width;
2358 data[8] = wwin->frame->core->height;
2359 } else {
2360 data[5] = wwin->old_geometry.x;
2361 data[6] = wwin->old_geometry.y;
2362 data[7] = wwin->old_geometry.width;
2363 data[8] = wwin->old_geometry.height;
2366 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2367 if (wwin->screen_ptr->shortcutWindows[i] &&
2368 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2369 data[9] |= 1 << i;
2371 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2372 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2375 static int getSavedState(Window window, WSavedState ** state)
2377 Atom type_ret;
2378 int fmt_ret;
2379 unsigned long nitems_ret;
2380 unsigned long bytes_after_ret;
2381 CARD32 *data;
2383 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2384 True, _XA_WINDOWMAKER_STATE,
2385 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2386 (unsigned char **)&data) != Success || !data)
2387 return 0;
2389 *state = wmalloc(sizeof(WSavedState));
2391 (*state)->workspace = data[0];
2392 (*state)->miniaturized = data[1];
2393 (*state)->shaded = data[2];
2394 (*state)->hidden = data[3];
2395 (*state)->maximized = data[4];
2396 (*state)->x = data[5];
2397 (*state)->y = data[6];
2398 (*state)->w = data[7];
2399 (*state)->h = data[8];
2400 (*state)->window_shortcuts = data[9];
2402 XFree(data);
2404 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2405 return 1;
2406 else
2407 return 0;
2410 #ifdef SHAPE
2411 void wWindowClearShape(WWindow * wwin)
2413 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2414 0, wwin->frame->top_width, None, ShapeSet);
2415 XFlush(dpy);
2418 void wWindowSetShape(WWindow * wwin)
2420 XRectangle rect[2];
2421 int count;
2422 #ifdef OPTIMIZE_SHAPE
2423 XRectangle *rects;
2424 XRectangle *urec;
2425 int ordering;
2427 /* only shape is the client's */
2428 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2429 goto alt_code;
2432 /* Get array of rectangles describing the shape mask */
2433 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2434 if (!rects) {
2435 goto alt_code;
2438 urec = malloc(sizeof(XRectangle) * (count + 2));
2439 if (!urec) {
2440 XFree(rects);
2441 goto alt_code;
2444 /* insert our decoration rectangles in the rect list */
2445 memcpy(urec, rects, sizeof(XRectangle) * count);
2446 XFree(rects);
2448 if (HAS_TITLEBAR(wwin)) {
2449 urec[count].x = -1;
2450 urec[count].y = -1 - wwin->frame->top_width;
2451 urec[count].width = wwin->frame->core->width + 2;
2452 urec[count].height = wwin->frame->top_width + 1;
2453 count++;
2455 if (HAS_RESIZEBAR(wwin)) {
2456 urec[count].x = -1;
2457 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2458 urec[count].width = wwin->frame->core->width + 2;
2459 urec[count].height = wwin->frame->bottom_width + 1;
2460 count++;
2463 /* shape our frame window */
2464 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2465 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2466 XFlush(dpy);
2467 wfree(urec);
2468 return;
2470 alt_code:
2471 #endif /* OPTIMIZE_SHAPE */
2472 count = 0;
2473 if (HAS_TITLEBAR(wwin)) {
2474 rect[count].x = -1;
2475 rect[count].y = -1;
2476 rect[count].width = wwin->frame->core->width + 2;
2477 rect[count].height = wwin->frame->top_width + 1;
2478 count++;
2480 if (HAS_RESIZEBAR(wwin)) {
2481 rect[count].x = -1;
2482 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2483 rect[count].width = wwin->frame->core->width + 2;
2484 rect[count].height = wwin->frame->bottom_width + 1;
2485 count++;
2487 if (count > 0) {
2488 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2489 0, 0, rect, count, ShapeSet, Unsorted);
2491 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2492 0, wwin->frame->top_width, wwin->client_win,
2493 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2494 XFlush(dpy);
2496 #endif /* SHAPE */
2498 /* ====================================================================== */
2500 static FocusMode getFocusMode(WWindow * wwin)
2502 FocusMode mode;
2504 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2505 if (wwin->wm_hints->input == True) {
2506 if (wwin->protocols.TAKE_FOCUS)
2507 mode = WFM_LOCALLY_ACTIVE;
2508 else
2509 mode = WFM_PASSIVE;
2510 } else {
2511 if (wwin->protocols.TAKE_FOCUS)
2512 mode = WFM_GLOBALLY_ACTIVE;
2513 else
2514 mode = WFM_NO_INPUT;
2516 } else {
2517 mode = WFM_PASSIVE;
2519 return mode;
2522 void wWindowSetKeyGrabs(WWindow * wwin)
2524 int i;
2525 WShortKey *key;
2527 for (i = 0; i < WKBD_LAST; i++) {
2528 key = &wKeyBindings[i];
2530 if (key->keycode == 0)
2531 continue;
2532 if (key->modifier != AnyModifier) {
2533 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2534 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2535 #ifdef NUMLOCK_HACK
2536 /* Also grab all modifier combinations possible that include,
2537 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2538 * work even if the NumLock/ScrollLock key is on.
2540 wHackedGrabKey(key->keycode, key->modifier,
2541 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2542 #endif
2544 XGrabKey(dpy, key->keycode, key->modifier,
2545 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2548 wRootMenuBindShortcuts(wwin->frame->core->window);
2551 void wWindowResetMouseGrabs(WWindow * wwin)
2553 /* Mouse grabs can't be done on the client window because of
2554 * ICCCM and because clients that try to do the same will crash.
2556 * But there is a problem wich makes tbar buttons of unfocused
2557 * windows not usable as the click goes to the frame window instead
2558 * of the button itself. Must figure a way to fix that.
2561 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2563 if (!WFLAGP(wwin, no_bind_mouse)) {
2564 /* grabs for Meta+drag */
2565 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2566 True, ButtonPressMask | ButtonReleaseMask,
2567 GrabModeSync, GrabModeAsync, None, None);
2569 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2570 * but we only grab it for Button4 and Button 5 since a lot of apps
2571 * use CTRL+Button1-3 for app related functionality
2573 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2574 True, ButtonPressMask | ButtonReleaseMask,
2575 GrabModeSync, GrabModeAsync, None, None);
2576 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2577 True, ButtonPressMask | ButtonReleaseMask,
2578 GrabModeSync, GrabModeAsync, None, None);
2580 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2581 True, ButtonPressMask | ButtonReleaseMask,
2582 GrabModeSync, GrabModeAsync, None, None);
2583 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2584 True, ButtonPressMask | ButtonReleaseMask,
2585 GrabModeSync, GrabModeAsync, None, None);
2588 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2589 && !wwin->flags.is_gnustep) {
2590 /* the passive grabs to focus the window */
2591 /* if (wPreferences.focus_mode == WKF_CLICK) */
2592 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2593 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2595 XFlush(dpy);
2598 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2600 if (attr->flags & GSExtraFlagsAttr) {
2601 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2602 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2603 wWindowUpdateButtonImages(wwin);
2608 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2610 WWindowState *wstate;
2612 wstate = malloc(sizeof(WWindowState));
2613 if (!wstate)
2614 return 0;
2616 memset(wstate, 0, sizeof(WWindowState));
2617 wstate->pid = pid;
2618 if (instance)
2619 wstate->instance = wstrdup(instance);
2620 if (class)
2621 wstate->class = wstrdup(class);
2622 if (command)
2623 wstate->command = wstrdup(command);
2624 wstate->state = state;
2626 wstate->next = windowState;
2627 windowState = wstate;
2629 return wstate;
2632 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2634 WMagicNumber wWindowGetSavedState(Window win)
2636 char *instance, *class, *command = NULL;
2637 WWindowState *wstate = windowState;
2639 if (!wstate)
2640 return NULL;
2642 command = GetCommandForWindow(win);
2643 if (!command)
2644 return NULL;
2646 if (PropGetWMClass(win, &class, &instance)) {
2647 while (wstate) {
2648 if (SAME(instance, wstate->instance) &&
2649 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2650 break;
2652 wstate = wstate->next;
2654 } else {
2655 wstate = NULL;
2658 if (command)
2659 wfree(command);
2660 if (instance)
2661 XFree(instance);
2662 if (class)
2663 XFree(class);
2665 return wstate;
2668 void wWindowDeleteSavedState(WMagicNumber id)
2670 WWindowState *tmp, *wstate = (WWindowState *) id;
2672 if (!wstate || !windowState)
2673 return;
2675 tmp = windowState;
2676 if (tmp == wstate) {
2677 windowState = wstate->next;
2678 if (wstate->instance)
2679 wfree(wstate->instance);
2680 if (wstate->class)
2681 wfree(wstate->class);
2682 if (wstate->command)
2683 wfree(wstate->command);
2684 wfree(wstate->state);
2685 wfree(wstate);
2686 } else {
2687 while (tmp->next) {
2688 if (tmp->next == wstate) {
2689 tmp->next = wstate->next;
2690 if (wstate->instance)
2691 wfree(wstate->instance);
2692 if (wstate->class)
2693 wfree(wstate->class);
2694 if (wstate->command)
2695 wfree(wstate->command);
2696 wfree(wstate->state);
2697 wfree(wstate);
2698 break;
2700 tmp = tmp->next;
2705 void wWindowDeleteSavedStatesForPID(pid_t pid)
2707 WWindowState *tmp, *wstate;
2709 if (!windowState)
2710 return;
2712 tmp = windowState;
2713 if (tmp->pid == pid) {
2714 wstate = windowState;
2715 windowState = tmp->next;
2716 if (wstate->instance)
2717 wfree(wstate->instance);
2718 if (wstate->class)
2719 wfree(wstate->class);
2720 if (wstate->command)
2721 wfree(wstate->command);
2722 wfree(wstate->state);
2723 wfree(wstate);
2724 } else {
2725 while (tmp->next) {
2726 if (tmp->next->pid == pid) {
2727 wstate = tmp->next;
2728 tmp->next = wstate->next;
2729 if (wstate->instance)
2730 wfree(wstate->instance);
2731 if (wstate->class)
2732 wfree(wstate->class);
2733 if (wstate->command)
2734 wfree(wstate->command);
2735 wfree(wstate->state);
2736 wfree(wstate);
2737 break;
2739 tmp = tmp->next;
2744 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2746 if (wwin->flags.omnipresent == flag)
2747 return;
2749 wwin->flags.omnipresent = flag;
2750 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2753 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2755 WWindow *wwin = data;
2757 #ifndef NUMLOCK_HACK
2758 if ((event->xbutton.state & ValidModMask)
2759 != (event->xbutton.state & ~LockMask)) {
2760 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2761 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2763 #endif
2765 event->xbutton.state &= ValidModMask;
2767 CloseWindowMenu(wwin->screen_ptr);
2769 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2770 && !WFLAGP(wwin, no_focusable)) {
2771 wSetFocusTo(wwin->screen_ptr, wwin);
2774 if (event->xbutton.button == Button1)
2775 wRaiseFrame(wwin->frame->core);
2777 if (event->xbutton.window != wwin->frame->resizebar->window) {
2778 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2779 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2780 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2781 return;
2785 if (event->xbutton.state & MOD_MASK) {
2786 /* move the window */
2787 wMouseMoveWindow(wwin, event);
2788 XUngrabPointer(dpy, CurrentTime);
2789 } else {
2790 wMouseResizeWindow(wwin, event);
2791 XUngrabPointer(dpy, CurrentTime);
2795 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2797 WWindow *wwin = data;
2799 event->xbutton.state &= ValidModMask;
2801 if (event->xbutton.button == Button1) {
2802 if (event->xbutton.state == 0) {
2803 if (!WFLAGP(wwin, no_shadeable)) {
2804 /* shade window */
2805 if (wwin->flags.shaded)
2806 wUnshadeWindow(wwin);
2807 else
2808 wShadeWindow(wwin);
2810 } else {
2811 int dir = 0;
2813 if (event->xbutton.state & ControlMask)
2814 dir |= MAX_VERTICAL;
2816 if (event->xbutton.state & ShiftMask) {
2817 dir |= MAX_HORIZONTAL;
2818 if (!(event->xbutton.state & ControlMask))
2819 wSelectWindow(wwin, !wwin->flags.selected);
2822 /* maximize window */
2823 if (dir != 0 && IS_RESIZABLE(wwin)) {
2824 int ndir = dir ^ wwin->flags.maximized;
2826 if (ndir != 0)
2827 wMaximizeWindow(wwin, ndir);
2828 else
2829 wUnmaximizeWindow(wwin);
2832 } else if (event->xbutton.button == Button3) {
2833 if (event->xbutton.state & MOD_MASK) {
2834 wHideOtherApplications(wwin);
2836 } else if (event->xbutton.button == Button2) {
2837 wSelectWindow(wwin, !wwin->flags.selected);
2838 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2839 wShadeWindow(wwin);
2840 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2841 wUnshadeWindow(wwin);
2845 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2847 WWindow *wwin = desc->parent;
2848 unsigned int new_width, w_scale;
2849 unsigned int new_height, h_scale;
2850 unsigned int resize_width_increment = 0;
2851 unsigned int resize_height_increment = 0;
2853 if (wwin->normal_hints) {
2854 w_scale = ceil(wPreferences.resize_increment / wwin->normal_hints->width_inc);
2855 h_scale = ceil(wPreferences.resize_increment / wwin->normal_hints->height_inc);
2856 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2857 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2859 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2860 resize_width_increment = wPreferences.resize_increment;
2861 resize_height_increment = wPreferences.resize_increment;
2864 event->xbutton.state &= ValidModMask;
2866 CloseWindowMenu(wwin->screen_ptr);
2868 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2869 wSetFocusTo(wwin->screen_ptr, wwin);
2871 if (event->xbutton.button == Button1)
2872 wRaiseFrame(wwin->frame->core);
2874 if (event->xbutton.state & ControlMask) {
2875 if (event->xbutton.button == Button4) {
2876 new_width = wwin->client.width - resize_width_increment;
2877 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2878 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2880 if (event->xbutton.button == Button5) {
2881 new_width = wwin->client.width + resize_width_increment;
2882 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2883 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2887 if (event->xbutton.state & MOD_MASK) {
2888 /* move the window */
2889 if (XGrabPointer(dpy, wwin->client_win, False,
2890 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2891 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2892 return;
2894 if (event->xbutton.button == Button3) {
2895 wMouseResizeWindow(wwin, event);
2896 } else if (event->xbutton.button == Button4) {
2897 new_height = wwin->client.height - resize_height_increment;
2898 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2899 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2900 } else if (event->xbutton.button == Button5) {
2901 new_height = wwin->client.height + resize_height_increment;
2902 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2903 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2904 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2905 wMouseMoveWindow(wwin, event);
2907 XUngrabPointer(dpy, CurrentTime);
2911 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2913 WWindow *wwin = (WWindow *) data;
2915 #ifndef NUMLOCK_HACK
2916 if ((event->xbutton.state & ValidModMask)
2917 != (event->xbutton.state & ~LockMask)) {
2918 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2919 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2921 #endif
2922 event->xbutton.state &= ValidModMask;
2924 CloseWindowMenu(wwin->screen_ptr);
2926 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2927 && !WFLAGP(wwin, no_focusable)) {
2928 wSetFocusTo(wwin->screen_ptr, wwin);
2931 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2933 if (event->xbutton.button == Button1) {
2934 if (event->xbutton.state & MOD_MASK) {
2935 wLowerFrame(wwin->frame->core);
2936 } else {
2937 wRaiseFrame(wwin->frame->core);
2940 if ((event->xbutton.state & ShiftMask)
2941 && !(event->xbutton.state & ControlMask)) {
2942 wSelectWindow(wwin, !wwin->flags.selected);
2943 return;
2945 if (event->xbutton.window != wwin->frame->titlebar->window
2946 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2947 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2948 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2949 return;
2952 /* move the window */
2953 wMouseMoveWindow(wwin, event);
2955 XUngrabPointer(dpy, CurrentTime);
2956 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2957 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2958 WObjDescriptor *desc;
2960 if (event->xbutton.window != wwin->frame->titlebar->window
2961 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2962 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2963 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2964 return;
2967 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2969 /* allow drag select */
2970 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2971 event->xany.send_event = True;
2972 (*desc->handle_mousedown) (desc, event);
2974 XUngrabPointer(dpy, CurrentTime);
2978 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2980 WWindow *wwin = data;
2982 event->xbutton.state &= ValidModMask;
2984 CloseWindowMenu(wwin->screen_ptr);
2986 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2987 return;
2989 /* if control-click, kill the client */
2990 if (event->xbutton.state & ControlMask) {
2991 wClientKill(wwin);
2992 } else {
2993 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2994 /* send delete message */
2995 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3000 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3002 WWindow *wwin = data;
3004 CloseWindowMenu(wwin->screen_ptr);
3006 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3007 return;
3009 /* send delete message */
3010 if (wwin->protocols.DELETE_WINDOW) {
3011 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3012 } else {
3013 wClientKill(wwin);
3017 #ifdef XKB_BUTTON_HINT
3018 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3020 WWindow *wwin = data;
3021 WFrameWindow *fwin = wwin->frame;
3022 WScreen *scr = fwin->screen_ptr;
3023 XkbStateRec staterec;
3024 int tl;
3026 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3027 return;
3028 tl = wwin->frame->languagemode;
3029 wwin->frame->languagemode = wwin->frame->last_languagemode;
3030 wwin->frame->last_languagemode = tl;
3031 wSetFocusTo(scr, wwin);
3032 wwin->frame->languagebutton_image =
3033 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3034 wFrameWindowUpdateLanguageButton(wwin->frame);
3035 if (event->xbutton.button == Button3)
3036 return;
3037 wRaiseFrame(fwin->core);
3039 #endif
3041 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3043 WWindow *wwin = data;
3045 event->xbutton.state &= ValidModMask;
3047 CloseWindowMenu(wwin->screen_ptr);
3049 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3050 return;
3052 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3053 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3054 } else {
3055 WApplication *wapp;
3056 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3058 wapp = wApplicationOf(wwin->main_window);
3059 if (wapp && !WFLAGP(wwin, no_appicon))
3060 wHideApplication(wapp);
3061 } else if (event->xbutton.state == 0) {
3062 wIconifyWindow(wwin);