Fix the empty button in titlebar after going fullscreen bug (Debian bug #298873)
[wmaker-crm.git] / src / window.c
blob9884944514ae6014ae9f45a4c0e248f508951322
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 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags, WWindowAttributes *mask)
309 if (wflags->no_appicon && mask->no_appicon)
310 wflags->emulate_appicon = 0;
312 if (wwin->main_window != None) {
313 WApplication *wapp = wApplicationOf(wwin->main_window);
314 if (wapp && !wapp->flags.emulated)
315 wflags->emulate_appicon = 0;
318 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
319 wflags->emulate_appicon = 0;
321 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
322 wflags->sunken = 0;
325 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
327 WScreen *scr = wwin->screen_ptr;
329 /* sets global default stuff */
330 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
332 * Decoration setting is done in this precedence (lower to higher)
333 * - use global default in the resource database
334 * - guess some settings
335 * - use GNUstep/external window attributes
336 * - set hints specified for the app in the resource DB
339 WSETUFLAG(wwin, broken_close, 0);
341 if (wwin->protocols.DELETE_WINDOW)
342 WSETUFLAG(wwin, kill_close, 0);
343 else
344 WSETUFLAG(wwin, kill_close, 1);
346 /* transients can't be iconified or maximized */
347 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
348 WSETUFLAG(wwin, no_miniaturizable, 1);
349 WSETUFLAG(wwin, no_miniaturize_button, 1);
352 /* if the window can't be resized, remove the resizebar */
353 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
354 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
355 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
356 WSETUFLAG(wwin, no_resizable, 1);
357 WSETUFLAG(wwin, no_resizebar, 1);
360 /* set GNUstep window attributes */
361 if (wwin->wm_gnustep_attr) {
362 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
364 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
366 *level = wwin->wm_gnustep_attr->window_level;
368 * INT_MIN is the only illegal window level.
370 if (*level == INT_MIN)
371 *level = INT_MIN + 1;
372 } else {
373 /* setup defaults */
374 *level = WMNormalLevel;
376 } else {
377 int tmp_workspace = -1;
378 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
379 Bool check;
381 check = False;
383 #ifdef MWM_HINTS
384 wMWMCheckClientHints(wwin);
385 #endif /* MWM_HINTS */
387 if (!check)
388 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
390 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
391 * have INT_MIN that means that no window level was requested. -Dan
393 if (tmp_level == INT_MIN) {
394 if (WFLAGP(wwin, floating))
395 *level = WMFloatingLevel;
396 else if (WFLAGP(wwin, sunken))
397 *level = WMSunkenLevel;
398 else
399 *level = WMNormalLevel;
400 } else {
401 *level = tmp_level;
404 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
405 WWindow *transientOwner = wWindowFor(wwin->transient_for);
406 if (transientOwner) {
407 int ownerLevel = transientOwner->frame->core->stacking->window_level;
408 if (ownerLevel > *level)
409 *level = ownerLevel;
413 if (tmp_workspace >= 0)
414 *workspace = tmp_workspace % scr->workspace_count;
418 * Set attributes specified only for that window/class.
419 * This might do duplicate work with the 1st wDefaultFillAttributes().
421 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
422 &wwin->user_flags, &wwin->defined_user_flags, False);
424 * Sanity checks for attributes that depend on other attributes
426 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
427 wwin->user_flags.emulate_appicon = 0;
429 if (wwin->main_window != None) {
430 WApplication *wapp = wApplicationOf(wwin->main_window);
431 if (wapp && !wapp->flags.emulated)
432 wwin->user_flags.emulate_appicon = 0;
435 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
436 wwin->user_flags.emulate_appicon = 0;
438 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
439 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
440 wwin->user_flags.sunken = 0;
442 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
444 /* windows that have takefocus=False shouldn't take focus at all */
445 if (wwin->focus_mode == WFM_NO_INPUT)
446 wwin->client_flags.no_focusable = 1;
449 Bool wWindowCanReceiveFocus(WWindow *wwin)
451 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
452 return False;
453 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
454 return False;
455 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
456 return False;
458 return True;
461 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
463 int w1, h1, w2, h2;
465 w1 = wwin->frame->core->width;
466 h1 = wwin->frame->core->height;
467 w2 = obscured->frame->core->width;
468 h2 = obscured->frame->core->height;
470 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
471 && wwin->frame->workspace != obscured->frame->workspace)
472 return False;
474 if (wwin->frame_x + w1 < obscured->frame_x
475 || wwin->frame_y + h1 < obscured->frame_y
476 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
477 return False;
479 return True;
482 static void fixLeaderProperties(WWindow *wwin)
484 XClassHint *classHint;
485 XWMHints *hints, *clientHints;
486 Window leaders[2], window;
487 char **argv, *command;
488 int argc, i, pid;
489 Bool haveCommand;
491 classHint = XAllocClassHint();
492 clientHints = XGetWMHints(dpy, wwin->client_win);
493 pid = wNETWMGetPidForWindow(wwin->client_win);
494 if (pid > 0)
495 haveCommand = GetCommandForPid(pid, &argv, &argc);
496 else
497 haveCommand = False;
499 leaders[0] = wwin->client_leader;
500 leaders[1] = wwin->group_id;
502 if (haveCommand) {
503 command = GetCommandForWindow(wwin->client_win);
504 if (command) {
505 /* command already set. nothing to do. */
506 wfree(command);
507 } else {
508 XSetCommand(dpy, wwin->client_win, argv, argc);
512 for (i = 0; i < 2; i++) {
513 window = leaders[i];
514 if (window) {
515 if (XGetClassHint(dpy, window, classHint) == 0) {
516 classHint->res_name = wwin->wm_instance;
517 classHint->res_class = wwin->wm_class;
518 XSetClassHint(dpy, window, classHint);
520 hints = XGetWMHints(dpy, window);
521 if (hints) {
522 XFree(hints);
523 } else if (clientHints) {
524 /* set window group leader to self */
525 clientHints->window_group = window;
526 clientHints->flags |= WindowGroupHint;
527 XSetWMHints(dpy, window, clientHints);
530 if (haveCommand) {
531 command = GetCommandForWindow(window);
532 if (command) {
533 /* command already set. nothing to do. */
534 wfree(command);
535 } else {
536 XSetCommand(dpy, window, argv, argc);
542 XFree(classHint);
543 if (clientHints)
544 XFree(clientHints);
545 if (haveCommand)
546 wfree(argv);
549 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
551 XClassHint *classHint;
552 XWMHints *hints;
553 Window leader;
554 int argc;
555 char **argv;
557 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
558 /* set class hint */
559 classHint = XAllocClassHint();
560 classHint->res_name = instance;
561 classHint->res_class = class;
562 XSetClassHint(dpy, leader, classHint);
563 XFree(classHint);
565 /* inherit these from the original leader if available */
566 hints = XGetWMHints(dpy, win);
567 if (!hints) {
568 hints = XAllocWMHints();
569 hints->flags = 0;
571 /* set window group leader to self */
572 hints->window_group = leader;
573 hints->flags |= WindowGroupHint;
574 XSetWMHints(dpy, leader, hints);
575 XFree(hints);
577 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
578 XSetCommand(dpy, leader, argv, argc);
579 XFreeStringList(argv);
582 return leader;
585 static int matchIdentifier(void *item, void *cdata)
587 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
591 *----------------------------------------------------------------
592 * wManageWindow--
593 * reparents the window and allocates a descriptor for it.
594 * Window manager hints and other hints are fetched to configure
595 * the window decoration attributes and others. User preferences
596 * for the window are used if available, to configure window
597 * decorations and some behaviour.
598 * If in startup, windows that are override redirect,
599 * unmapped and never were managed and are Withdrawn are not
600 * managed.
602 * Returns:
603 * the new window descriptor
605 * Side effects:
606 * The window is reparented and appropriate notification
607 * is done to the client. Input mask for the window is setup.
608 * The window descriptor is also associated with various window
609 * contexts and inserted in the head of the window list.
610 * Event handler contexts are associated for some objects
611 * (buttons, titlebar and resizebar)
613 *----------------------------------------------------------------
615 WWindow *wManageWindow(WScreen *scr, Window window)
617 WWindow *wwin;
618 int x, y;
619 unsigned width, height;
620 XWindowAttributes wattribs;
621 XSetWindowAttributes attribs;
622 WWindowState *win_state;
623 WWindow *transientOwner = NULL;
624 int window_level;
625 int wm_state;
626 int foo;
627 int workspace = -1;
628 char *title;
629 Bool withdraw = False;
630 Bool raise = False;
632 /* mutex. */
633 /* XGrabServer(dpy); */
634 XSync(dpy, False);
635 /* make sure the window is still there */
636 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
637 XUngrabServer(dpy);
638 return NULL;
641 /* if it's an override-redirect, ignore it */
642 if (wattribs.override_redirect) {
643 XUngrabServer(dpy);
644 return NULL;
647 wm_state = PropGetWindowState(window);
649 /* if it's startup and the window is unmapped, don't manage it */
650 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
651 XUngrabServer(dpy);
652 return NULL;
655 wwin = wWindowCreate();
657 title = wNETWMGetWindowName(window);
658 if (title)
659 wwin->flags.net_has_title = 1;
660 if (!title && !wFetchName(dpy, window, &title))
661 title = NULL;
663 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
665 #ifdef DEBUG
666 printf("managing window %x\n", (unsigned)window);
667 #endif
669 #ifdef SHAPE
670 if (wShapeSupported) {
671 int junk;
672 unsigned int ujunk;
673 int b_shaped;
675 XShapeSelectInput(dpy, window, ShapeNotifyMask);
676 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
677 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
678 wwin->flags.shaped = b_shaped;
680 #endif
683 * Get hints and other information in properties
685 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
687 /* setup descriptor */
688 wwin->client_win = window;
689 wwin->screen_ptr = scr;
690 wwin->old_border_width = wattribs.border_width;
691 wwin->event_mask = CLIENT_EVENTS;
692 attribs.event_mask = CLIENT_EVENTS;
693 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
694 attribs.save_under = False;
696 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
697 XSetWindowBorderWidth(dpy, window, 0);
699 /* get hints from GNUstep app */
700 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
701 wwin->flags.is_gnustep = 1;
703 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
704 wwin->wm_gnustep_attr = NULL;
707 wwin->client_leader = PropGetClientLeader(window);
708 if (wwin->client_leader != None)
709 wwin->main_window = wwin->client_leader;
711 wwin->wm_hints = XGetWMHints(dpy, window);
713 if (wwin->wm_hints) {
714 if (wwin->wm_hints->flags & StateHint) {
716 if (wwin->wm_hints->initial_state == IconicState) {
718 wwin->flags.miniaturized = 1;
720 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
722 withdraw = True;
726 if (wwin->wm_hints->flags & WindowGroupHint) {
727 wwin->group_id = wwin->wm_hints->window_group;
728 /* window_group has priority over CLIENT_LEADER */
729 wwin->main_window = wwin->group_id;
730 } else {
731 wwin->group_id = None;
734 if (wwin->wm_hints->flags & UrgencyHint)
735 wwin->flags.urgent = 1;
736 } else {
737 wwin->group_id = None;
740 PropGetProtocols(window, &wwin->protocols);
742 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
743 wwin->transient_for = None;
744 } else {
745 if (wwin->transient_for == None || wwin->transient_for == window) {
746 wwin->transient_for = scr->root_win;
747 } else {
748 transientOwner = wWindowFor(wwin->transient_for);
749 if (transientOwner && transientOwner->main_window != None)
750 wwin->main_window = transientOwner->main_window;
754 /* guess the focus mode */
755 wwin->focus_mode = getFocusMode(wwin);
757 /* get geometry stuff */
758 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
760 /* get colormap windows */
761 GetColormapWindows(wwin);
764 * Setup the decoration/window attributes and
765 * geometry
767 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
769 /* Make broken apps behave as a nice app. */
770 if (WFLAGP(wwin, emulate_appicon))
771 wwin->main_window = wwin->client_win;
773 fixLeaderProperties(wwin);
775 wwin->orig_main_window = wwin->main_window;
777 if (wwin->flags.is_gnustep)
778 WSETUFLAG(wwin, shared_appicon, 0);
780 if (wwin->main_window) {
781 extern Atom _XA_WINDOWMAKER_MENU;
782 XTextProperty text_prop;
784 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
785 WSETUFLAG(wwin, shared_appicon, 0);
789 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
790 char *buffer, *instance, *class;
791 WFakeGroupLeader *fPtr;
792 int index;
794 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
796 /* // only enter here if PropGetWMClass() succeds */
797 PropGetWMClass(wwin->main_window, &class, &instance);
798 buffer = StrConcatDot(instance, class);
800 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
801 if (index != WANotFound) {
802 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
803 if (fPtr->retainCount == 0) {
804 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
805 instance, class);
807 fPtr->retainCount++;
808 #undef method2
809 if (fPtr->origLeader == None) {
810 #ifdef method2
811 if (ADEQUATE(wwin->group_id)) {
812 fPtr->retainCount++;
813 fPtr->origLeader = wwin->group_id;
814 } else if (ADEQUATE(wwin->client_leader)) {
815 fPtr->retainCount++;
816 fPtr->origLeader = wwin->client_leader;
817 } else if (ADEQUATE(wwin->main_window)) {
818 fPtr->retainCount++;
819 fPtr->origLeader = wwin->main_window;
821 #else
822 if (ADEQUATE(wwin->main_window)) {
823 fPtr->retainCount++;
824 fPtr->origLeader = wwin->main_window;
826 #endif
828 wwin->fake_group = fPtr;
829 /*wwin->group_id = fPtr->leader; */
830 wwin->main_window = fPtr->leader;
831 wfree(buffer);
832 } else {
833 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
835 fPtr->identifier = buffer;
836 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
837 fPtr->origLeader = None;
838 fPtr->retainCount = 1;
840 WMAddToArray(scr->fakeGroupLeaders, fPtr);
842 #ifdef method2
843 if (ADEQUATE(wwin->group_id)) {
844 fPtr->retainCount++;
845 fPtr->origLeader = wwin->group_id;
846 } else if (ADEQUATE(wwin->client_leader)) {
847 fPtr->retainCount++;
848 fPtr->origLeader = wwin->client_leader;
849 } else if (ADEQUATE(wwin->main_window)) {
850 fPtr->retainCount++;
851 fPtr->origLeader = wwin->main_window;
853 #else
854 if (ADEQUATE(wwin->main_window)) {
855 fPtr->retainCount++;
856 fPtr->origLeader = wwin->main_window;
858 #endif
859 wwin->fake_group = fPtr;
860 /*wwin->group_id = fPtr->leader; */
861 wwin->main_window = fPtr->leader;
863 if (instance)
864 XFree(instance);
865 if (class)
866 XFree(class);
868 #undef method2
869 #undef ADEQUATE
873 * Setup the initial state of the window
875 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
876 wwin->flags.miniaturized = 1;
878 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
879 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
881 wNETWMCheckInitialClientState(wwin);
883 /* apply previous state if it exists and we're in startup */
884 if (scr->flags.startup && wm_state >= 0) {
886 if (wm_state == IconicState) {
888 wwin->flags.miniaturized = 1;
890 } else if (wm_state == WithdrawnState) {
892 withdraw = True;
896 /* if there is a saved state (from file), restore it */
897 win_state = NULL;
898 if (wwin->main_window != None)
899 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
900 else
901 win_state = (WWindowState *) wWindowGetSavedState(window);
903 if (win_state && !withdraw) {
904 if (win_state->state->hidden > 0)
905 wwin->flags.hidden = win_state->state->hidden;
907 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
908 wwin->flags.shaded = win_state->state->shaded;
910 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
911 wwin->flags.miniaturized = win_state->state->miniaturized;
914 if (!IS_OMNIPRESENT(wwin)) {
915 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
916 wwin->wm_class);
917 if (w < 0 || w >= scr->workspace_count) {
918 workspace = win_state->state->workspace;
919 if (workspace >= scr->workspace_count)
920 workspace = scr->current_workspace;
921 } else {
922 workspace = w;
924 } else {
925 workspace = scr->current_workspace;
929 /* if we're restarting, restore saved state (from hints).
930 * This will overwrite previous */
932 WSavedState *wstate;
934 if (getSavedState(window, &wstate)) {
935 wwin->flags.shaded = wstate->shaded;
936 wwin->flags.hidden = wstate->hidden;
937 wwin->flags.miniaturized = wstate->miniaturized;
938 wwin->flags.maximized = wstate->maximized;
939 if (wwin->flags.maximized) {
940 wwin->old_geometry.x = wstate->x;
941 wwin->old_geometry.y = wstate->y;
942 wwin->old_geometry.width = wstate->w;
943 wwin->old_geometry.height = wstate->h;
946 workspace = wstate->workspace;
947 } else {
948 wstate = NULL;
951 /* restore window shortcut */
952 if (wstate != NULL || win_state != NULL) {
953 unsigned mask = 0;
955 if (win_state != NULL)
956 mask = win_state->state->window_shortcuts;
958 if (wstate != NULL && mask == 0)
959 mask = wstate->window_shortcuts;
961 if (mask > 0) {
962 int i;
964 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
965 if (mask & (1 << i)) {
966 if (!scr->shortcutWindows[i])
967 scr->shortcutWindows[i] = WMCreateArray(4);
969 WMAddToArray(scr->shortcutWindows[i], wwin);
974 if (wstate != NULL)
975 wfree(wstate);
978 /* don't let transients start miniaturized if their owners are not */
979 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
980 wwin->flags.miniaturized = 0;
981 if (wwin->wm_hints)
982 wwin->wm_hints->initial_state = NormalState;
985 /* set workspace on which the window starts */
986 if (workspace >= 0) {
987 if (workspace > scr->workspace_count - 1) {
988 workspace = workspace % scr->workspace_count;
990 } else {
991 int w;
993 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
995 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
997 workspace = w;
999 } else {
1000 if (wPreferences.open_transients_with_parent && transientOwner) {
1002 workspace = transientOwner->frame->workspace;
1004 } else {
1006 workspace = scr->current_workspace;
1011 /* setup window geometry */
1012 if (win_state && win_state->state->w > 0) {
1013 width = win_state->state->w;
1014 height = win_state->state->h;
1016 wWindowConstrainSize(wwin, &width, &height);
1018 /* do not ask for window placement if the window is
1019 * transient, during startup, if the initial workspace is another one
1020 * or if the window wants to start iconic.
1021 * If geometry was saved, restore it. */
1023 Bool dontBring = False;
1025 if (win_state && win_state->state->w > 0) {
1026 x = win_state->state->x;
1027 y = win_state->state->y;
1028 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1029 && !scr->flags.startup
1030 && workspace == scr->current_workspace
1031 && !wwin->flags.miniaturized
1032 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1034 if (transientOwner && transientOwner->flags.mapped) {
1035 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1036 WMRect rect;
1037 int head;
1039 x = transientOwner->frame_x +
1040 abs((transientOwner->frame->core->width - width) / 2) + offs;
1041 y = transientOwner->frame_y +
1042 abs((transientOwner->frame->core->height - height) / 3) + offs;
1045 * limit transient windows to be inside their parent's head
1047 rect.pos.x = transientOwner->frame_x;
1048 rect.pos.y = transientOwner->frame_y;
1049 rect.size.width = transientOwner->frame->core->width;
1050 rect.size.height = transientOwner->frame->core->height;
1052 head = wGetHeadForRect(scr, rect);
1053 rect = wGetRectForHead(scr, head);
1055 if (x < rect.pos.x)
1056 x = rect.pos.x;
1057 else if (x + width > rect.pos.x + rect.size.width)
1058 x = rect.pos.x + rect.size.width - width;
1060 if (y < rect.pos.y)
1061 y = rect.pos.y;
1062 else if (y + height > rect.pos.y + rect.size.height)
1063 y = rect.pos.y + rect.size.height - height;
1065 } else {
1066 PlaceWindow(wwin, &x, &y, width, height);
1068 if (wPreferences.window_placement == WPM_MANUAL) {
1069 dontBring = True;
1071 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1072 int head, flags;
1073 WMRect rect;
1074 int reposition = 0;
1077 * Make spash screens come out in the center of a head
1078 * trouble is that most splashies never get here
1079 * they are managed trough atoms but god knows where.
1080 * Dan, do you know ? -peter
1082 * Most of them are not managed, they have set
1083 * OverrideRedirect, which means we can't do anything about
1084 * them. -alfredo
1086 #if 0
1087 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width) / 2);
1088 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height) / 2);
1090 if ((unsigned)(x + (width - scr->scr_width) / 2 + 10) < 20 &&
1091 (unsigned)(y + (height - scr->scr_height) / 2 + 10) < 20) {
1093 reposition = 1;
1095 } else
1096 #endif
1099 * xinerama checks for: across head and dead space
1101 rect.pos.x = x;
1102 rect.pos.y = y;
1103 rect.size.width = width;
1104 rect.size.height = height;
1106 head = wGetRectPlacementInfo(scr, rect, &flags);
1108 if (flags & XFLAG_DEAD)
1109 reposition = 1;
1111 if (flags & XFLAG_MULTIPLE)
1112 reposition = 2;
1115 switch (reposition) {
1116 case 1:
1117 head = wGetHeadForPointerLocation(scr);
1118 rect = wGetRectForHead(scr, head);
1120 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1121 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1122 break;
1124 case 2:
1125 rect = wGetRectForHead(scr, head);
1127 if (x < rect.pos.x)
1128 x = rect.pos.x;
1129 else if (x + width > rect.pos.x + rect.size.width)
1130 x = rect.pos.x + rect.size.width - width;
1132 if (y < rect.pos.y)
1133 y = rect.pos.y;
1134 else if (y + height > rect.pos.y + rect.size.height)
1135 y = rect.pos.y + rect.size.height - height;
1137 break;
1139 default:
1140 break;
1144 if (WFLAGP(wwin, dont_move_off) && dontBring)
1145 wScreenBringInside(scr, &x, &y, width, height);
1148 wNETWMPositionSplash(wwin, &x, &y, width, height);
1150 if (wwin->flags.urgent) {
1151 if (!IS_OMNIPRESENT(wwin))
1152 wwin->flags.omnipresent ^= 1;
1156 * Create frame, borders and do reparenting
1158 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1159 #ifdef XKB_BUTTON_HINT
1160 if (wPreferences.modelock)
1161 foo |= WFF_LANGUAGE_BUTTON;
1162 #endif
1163 if (HAS_TITLEBAR(wwin))
1164 foo |= WFF_TITLEBAR;
1165 if (HAS_RESIZEBAR(wwin))
1166 foo |= WFF_RESIZEBAR;
1167 if (HAS_BORDER(wwin))
1168 foo |= WFF_BORDER;
1170 wwin->frame = wFrameWindowCreate(scr, window_level,
1171 x, y, width, height,
1172 &wPreferences.window_title_clearance, foo,
1173 scr->window_title_texture,
1174 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1176 wwin->frame->flags.is_client_window_frame = 1;
1177 wwin->frame->flags.justification = wPreferences.title_justification;
1179 /* setup button images */
1180 wWindowUpdateButtonImages(wwin);
1182 /* hide unused buttons */
1183 foo = 0;
1184 if (WFLAGP(wwin, no_close_button))
1185 foo |= WFF_RIGHT_BUTTON;
1186 if (WFLAGP(wwin, no_miniaturize_button))
1187 foo |= WFF_LEFT_BUTTON;
1188 #ifdef XKB_BUTTON_HINT
1189 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1190 foo |= WFF_LANGUAGE_BUTTON;
1191 #endif
1192 if (foo != 0)
1193 wFrameWindowHideButton(wwin->frame, foo);
1195 wwin->frame->child = wwin;
1196 wwin->frame->workspace = workspace;
1197 wwin->frame->on_click_left = windowIconifyClick;
1199 #ifdef XKB_BUTTON_HINT
1200 if (wPreferences.modelock)
1201 wwin->frame->on_click_language = windowLanguageClick;
1202 #endif
1204 wwin->frame->on_click_right = windowCloseClick;
1205 wwin->frame->on_dblclick_right = windowCloseDblClick;
1206 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1207 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1208 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1210 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1211 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1212 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1215 int gx, gy;
1217 wClientGetGravityOffsets(wwin, &gx, &gy);
1219 /* if gravity is to the south, account for the border sizes */
1220 if (gy > 0)
1221 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1225 * wWindowConfigure() will init the client window's size
1226 * (wwin->client.{width,height}) and all other geometry
1227 * related variables (frame_x,frame_y)
1229 wWindowConfigure(wwin, x, y, width, height);
1231 /* to make sure the window receives it's new position after reparenting */
1232 wWindowSynthConfigureNotify(wwin);
1235 * Setup descriptors and save window to internal
1236 * lists
1238 if (wwin->main_window != None) {
1239 WApplication *app;
1240 WWindow *leader;
1242 /* Leader windows do not necessary set themselves as leaders.
1243 * If this is the case, point the leader of this window to
1244 * itself */
1245 leader = wWindowFor(wwin->main_window);
1246 if (leader && leader->main_window == None) {
1247 leader->main_window = leader->client_win;
1249 app = wApplicationCreate(wwin);
1250 if (app) {
1251 app->last_workspace = workspace;
1254 * Do application specific stuff, like setting application
1255 * wide attributes.
1258 if (wwin->flags.hidden) {
1259 /* if the window was set to hidden because it was hidden
1260 * in a previous incarnation and that state was restored */
1261 app->flags.hidden = 1;
1262 } else if (app->flags.hidden) {
1263 if (WFLAGP(app->main_window_desc, start_hidden)) {
1264 wwin->flags.hidden = 1;
1265 } else {
1266 wUnhideApplication(app, False, False);
1267 raise = True;
1273 /* setup the frame descriptor */
1274 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1275 wwin->frame->core->descriptor.parent = wwin;
1276 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1278 /* don't let windows go away if we die */
1279 XAddToSaveSet(dpy, window);
1281 XLowerWindow(dpy, window);
1283 /* if window is in this workspace and should be mapped, then map it */
1284 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1285 && !wwin->flags.hidden && !withdraw) {
1287 /* The following "if" is to avoid crashing of clients that expect
1288 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1289 * after the return from this function.
1291 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1292 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1293 } else {
1294 wClientSetState(wwin, NormalState, None);
1297 #if 0
1298 /* if not auto focus, then map the window under the currently
1299 * focused window */
1300 #define _WIDTH(w) (w)->frame->core->width
1301 #define _HEIGHT(w) (w)->frame->core->height
1302 if (!wPreferences.auto_focus && scr->focused_window
1303 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1304 && (_WIDTH(wwin) >
1305 (_WIDTH(scr->focused_window) * 5) / 3
1306 || _HEIGHT(wwin) >
1307 (_HEIGHT(scr->focused_window) * 5) / 3)
1308 && WINDOW_LEVEL(scr->focused_window) ==
1309 WINDOW_LEVEL(wwin))
1310 || wwin->flags.maximized)) {
1311 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1313 #undef _WIDTH
1314 #undef _HEIGHT
1316 #endif
1318 if (wPreferences.superfluous && !wPreferences.no_animations
1319 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1321 * The brain damaged idiotic non-click to focus modes will
1322 * have trouble with this because:
1324 * 1. window is created and mapped by the client
1325 * 2. window is mapped by wmaker in small size
1326 * 3. window is animated to grow to normal size
1327 * 4. this function returns to normal event loop
1328 * 5. eventually, the EnterNotify event that would trigger
1329 * the window focusing (if the mouse is over that window)
1330 * will be processed by wmaker.
1331 * But since this event will be rather delayed
1332 * (step 3 has a large delay) the time when the event ocurred
1333 * and when it is processed, the client that owns that window
1334 * will reject the XSetInputFocus() for it.
1336 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1337 DoWindowBirth(wwin);
1340 wWindowMap(wwin);
1343 /* setup stacking descriptor */
1344 if (transientOwner) {
1345 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1346 } else {
1347 wwin->frame->core->stacking->child_of = NULL;
1350 if (!scr->focused_window) {
1351 /* first window on the list */
1352 wwin->next = NULL;
1353 wwin->prev = NULL;
1354 scr->focused_window = wwin;
1355 } else {
1356 WWindow *tmp;
1358 /* add window at beginning of focus window list */
1359 tmp = scr->focused_window;
1360 while (tmp->prev)
1361 tmp = tmp->prev;
1362 tmp->prev = wwin;
1363 wwin->next = tmp;
1364 wwin->prev = NULL;
1367 /* raise is set to true if we un-hid the app when this window was born.
1368 * we raise, else old windows of this app will be above this new one. */
1369 if (raise) {
1370 wRaiseFrame(wwin->frame->core);
1373 /* Update name must come after WApplication stuff is done */
1374 wWindowUpdateName(wwin, title);
1375 if (title)
1376 XFree(title);
1378 XUngrabServer(dpy);
1381 * Final preparations before window is ready to go
1383 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1385 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1386 if (((transientOwner && transientOwner->flags.focused)
1387 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1388 wSetFocusTo(scr, wwin);
1390 wWindowResetMouseGrabs(wwin);
1392 if (!WFLAGP(wwin, no_bind_keys))
1393 wWindowSetKeyGrabs(wwin);
1395 WMPostNotificationName(WMNManaged, wwin, NULL);
1396 wColormapInstallForWindow(scr, scr->cmap_window);
1398 /* Setup Notification Observers */
1399 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1401 /* Cleanup temporary stuff */
1402 if (win_state)
1403 wWindowDeleteSavedState(win_state);
1405 /* If the window must be withdrawed, then do it now.
1406 * Must do some optimization, 'though */
1407 if (withdraw) {
1408 wwin->flags.mapped = 0;
1409 wClientSetState(wwin, WithdrawnState, None);
1410 wUnmanageWindow(wwin, True, False);
1411 wwin = NULL;
1414 return wwin;
1417 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1418 char *title, int x, int y, int width, int height)
1420 WWindow *wwin;
1421 int foo;
1423 wwin = wWindowCreate();
1425 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1427 wwin->flags.internal_window = 1;
1429 WSETUFLAG(wwin, omnipresent, 1);
1430 WSETUFLAG(wwin, no_shadeable, 1);
1431 WSETUFLAG(wwin, no_resizable, 1);
1432 WSETUFLAG(wwin, no_miniaturizable, 1);
1434 wwin->focus_mode = WFM_PASSIVE;
1435 wwin->client_win = window;
1436 wwin->screen_ptr = scr;
1437 wwin->transient_for = owner;
1438 wwin->client.x = x;
1439 wwin->client.y = y;
1440 wwin->client.width = width;
1441 wwin->client.height = height;
1442 wwin->frame_x = wwin->client.x;
1443 wwin->frame_y = wwin->client.y;
1445 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1446 foo |= WFF_TITLEBAR;
1447 #ifdef XKB_BUTTON_HINT
1448 foo |= WFF_LANGUAGE_BUTTON;
1449 #endif
1451 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1452 wwin->frame_x, wwin->frame_y,
1453 width, height,
1454 &wPreferences.window_title_clearance, foo,
1455 scr->window_title_texture,
1456 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1458 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1460 wwin->frame->flags.is_client_window_frame = 1;
1461 wwin->frame->flags.justification = wPreferences.title_justification;
1463 wFrameWindowChangeTitle(wwin->frame, title);
1465 /* setup button images */
1466 wWindowUpdateButtonImages(wwin);
1468 /* hide buttons */
1469 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1471 wwin->frame->child = wwin;
1472 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1474 #ifdef XKB_BUTTON_HINT
1475 if (wPreferences.modelock)
1476 wwin->frame->on_click_language = windowLanguageClick;
1477 #endif
1479 wwin->frame->on_click_right = windowCloseClick;
1480 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1481 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1482 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1483 wwin->client.y += wwin->frame->top_width;
1485 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1486 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1488 /* setup the frame descriptor */
1489 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1490 wwin->frame->core->descriptor.parent = wwin;
1491 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1493 XLowerWindow(dpy, window);
1494 XMapSubwindows(dpy, wwin->frame->core->window);
1496 /* setup stacking descriptor */
1497 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1498 WWindow *tmp;
1499 tmp = wWindowFor(wwin->transient_for);
1500 if (tmp)
1501 wwin->frame->core->stacking->child_of = tmp->frame->core;
1502 } else {
1503 wwin->frame->core->stacking->child_of = NULL;
1506 if (!scr->focused_window) {
1507 /* first window on the list */
1508 wwin->next = NULL;
1509 wwin->prev = NULL;
1510 scr->focused_window = wwin;
1511 } else {
1512 WWindow *tmp;
1514 /* add window at beginning of focus window list */
1515 tmp = scr->focused_window;
1516 while (tmp->prev)
1517 tmp = tmp->prev;
1518 tmp->prev = wwin;
1519 wwin->next = tmp;
1520 wwin->prev = NULL;
1523 if (wwin->flags.is_gnustep == 0)
1524 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1526 /* if (wPreferences.auto_focus) */
1527 wSetFocusTo(scr, wwin);
1528 wWindowResetMouseGrabs(wwin);
1529 wWindowSetKeyGrabs(wwin);
1531 return wwin;
1535 *----------------------------------------------------------------------
1536 * wUnmanageWindow--
1537 * Removes the frame window from a window and destroys all data
1538 * related to it. The window will be reparented back to the root window
1539 * if restore is True.
1541 * Side effects:
1542 * Everything related to the window is destroyed and the window
1543 * is removed from the window lists. Focus is set to the previous on the
1544 * window list.
1545 *----------------------------------------------------------------------
1547 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1549 WCoreWindow *frame = wwin->frame->core;
1550 WWindow *owner = NULL;
1551 WWindow *newFocusedWindow = NULL;
1552 int wasFocused;
1553 WScreen *scr = wwin->screen_ptr;
1555 /* First close attribute editor window if open */
1556 if (wwin->flags.inspector_open) {
1557 wCloseInspectorForWindow(wwin);
1560 /* Close window menu if it's open for this window */
1561 if (wwin->flags.menu_open_for_me) {
1562 CloseWindowMenu(scr);
1565 if (!destroyed) {
1566 if (!wwin->flags.internal_window)
1567 XRemoveFromSaveSet(dpy, wwin->client_win);
1569 XSelectInput(dpy, wwin->client_win, NoEventMask);
1571 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1572 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1575 XUnmapWindow(dpy, frame->window);
1577 XUnmapWindow(dpy, wwin->client_win);
1579 /* deselect window */
1580 wSelectWindow(wwin, False);
1582 /* remove all pending events on window */
1583 /* I think this only matters for autoraise */
1584 if (wPreferences.raise_delay)
1585 WMDeleteTimerWithClientData(wwin->frame->core);
1587 XFlush(dpy);
1589 /* reparent the window back to the root */
1590 if (restore)
1591 wClientRestore(wwin);
1593 if (wwin->transient_for != scr->root_win) {
1594 owner = wWindowFor(wwin->transient_for);
1595 if (owner) {
1596 if (!owner->flags.semi_focused) {
1597 owner = NULL;
1598 } else {
1599 owner->flags.semi_focused = 0;
1604 wasFocused = wwin->flags.focused;
1606 /* remove from window focus list */
1607 if (!wwin->prev && !wwin->next) {
1608 /* was the only window */
1609 scr->focused_window = NULL;
1610 newFocusedWindow = NULL;
1611 } else {
1612 WWindow *tmp;
1614 if (wwin->prev)
1615 wwin->prev->next = wwin->next;
1616 if (wwin->next)
1617 wwin->next->prev = wwin->prev;
1618 else {
1619 scr->focused_window = wwin->prev;
1620 scr->focused_window->next = NULL;
1623 if (wPreferences.focus_mode == WKF_CLICK) {
1625 /* if in click to focus mode and the window
1626 * was a transient, focus the owner window
1628 tmp = NULL;
1629 if (wPreferences.focus_mode == WKF_CLICK) {
1630 tmp = wWindowFor(wwin->transient_for);
1631 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1632 tmp = NULL;
1635 /* otherwise, focus the next one in the focus list */
1636 if (!tmp) {
1637 tmp = scr->focused_window;
1638 while (tmp) { /* look for one in the window list first */
1639 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1640 && (tmp->flags.mapped || tmp->flags.shaded))
1641 break;
1642 tmp = tmp->prev;
1644 if (!tmp) { /* if unsuccessful, choose any focusable window */
1645 tmp = scr->focused_window;
1646 while (tmp) {
1647 if (!WFLAGP(tmp, no_focusable)
1648 && (tmp->flags.mapped || tmp->flags.shaded))
1649 break;
1650 tmp = tmp->prev;
1655 newFocusedWindow = tmp;
1657 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1658 unsigned int mask;
1659 int foo;
1660 Window bar, win;
1662 /* This is to let the root window get the keyboard input
1663 * if Sloppy focus mode and no other window get focus.
1664 * This way keybindings will not freeze.
1666 tmp = NULL;
1667 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1668 tmp = wWindowFor(win);
1669 if (tmp == wwin)
1670 tmp = NULL;
1671 newFocusedWindow = tmp;
1672 } else {
1673 newFocusedWindow = NULL;
1677 if (!wwin->flags.internal_window)
1678 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1679 #ifdef DEBUG
1680 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win, (unsigned)frame->window);
1681 #endif
1683 if (wasFocused) {
1684 if (newFocusedWindow != owner && owner) {
1685 if (wwin->flags.is_gnustep == 0)
1686 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1688 wSetFocusTo(scr, newFocusedWindow);
1690 wWindowDestroy(wwin);
1691 XFlush(dpy);
1694 void wWindowMap(WWindow *wwin)
1696 XMapWindow(dpy, wwin->frame->core->window);
1697 if (!wwin->flags.shaded) {
1698 /* window will be remapped when getting MapNotify */
1699 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1700 XMapWindow(dpy, wwin->client_win);
1701 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1703 wwin->flags.mapped = 1;
1707 void wWindowUnmap(WWindow *wwin)
1709 wwin->flags.mapped = 0;
1711 /* prevent window withdrawal when getting UnmapNotify */
1712 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1713 XUnmapWindow(dpy, wwin->client_win);
1714 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1716 XUnmapWindow(dpy, wwin->frame->core->window);
1719 void wWindowFocus(WWindow *wwin, WWindow *owin)
1721 WWindow *nowner;
1722 WWindow *oowner;
1724 #ifdef KEEP_XKB_LOCK_STATUS
1725 if (wPreferences.modelock) {
1726 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1728 #endif /* KEEP_XKB_LOCK_STATUS */
1730 wwin->flags.semi_focused = 0;
1732 if (wwin->flags.is_gnustep == 0)
1733 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1735 wwin->flags.focused = 1;
1737 wWindowResetMouseGrabs(wwin);
1739 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1741 if (owin == wwin || !owin)
1742 return;
1744 nowner = wWindowFor(wwin->transient_for);
1746 /* new window is a transient for the old window */
1747 if (nowner == owin) {
1748 owin->flags.semi_focused = 1;
1749 wWindowUnfocus(nowner);
1750 return;
1753 oowner = wWindowFor(owin->transient_for);
1755 /* new window is owner of old window */
1756 if (wwin == oowner) {
1757 wWindowUnfocus(owin);
1758 return;
1761 if (!nowner) {
1762 wWindowUnfocus(owin);
1763 return;
1766 /* new window has same owner of old window */
1767 if (oowner == nowner) {
1768 /* prevent unfocusing of owner */
1769 oowner->flags.semi_focused = 0;
1770 wWindowUnfocus(owin);
1771 oowner->flags.semi_focused = 1;
1773 return;
1776 /* nowner != NULL && oowner != nowner */
1777 nowner->flags.semi_focused = 1;
1778 wWindowUnfocus(nowner);
1779 wWindowUnfocus(owin);
1782 void wWindowUnfocus(WWindow *wwin)
1784 CloseWindowMenu(wwin->screen_ptr);
1786 if (wwin->flags.is_gnustep == 0)
1787 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1789 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1790 WWindow *owner;
1791 owner = wWindowFor(wwin->transient_for);
1792 if (owner && owner->flags.semi_focused) {
1793 owner->flags.semi_focused = 0;
1794 if (owner->flags.mapped || owner->flags.shaded) {
1795 wWindowUnfocus(owner);
1796 wFrameWindowPaint(owner->frame);
1800 wwin->flags.focused = 0;
1801 wWindowResetMouseGrabs(wwin);
1802 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1805 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1807 char *title;
1809 if (!wwin->frame)
1810 return;
1812 wwin->flags.wm_name_changed = 1;
1814 if (!newTitle) {
1815 /* the hint was removed */
1816 title = DEF_WINDOW_TITLE;
1817 } else {
1818 title = newTitle;
1821 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1822 WMPostNotificationName(WMNChangedName, wwin, NULL);
1827 *----------------------------------------------------------------------
1829 * wWindowConstrainSize--
1830 * Constrains size for the client window, taking the maximal size,
1831 * window resize increments and other size hints into account.
1833 * Returns:
1834 * The closest size to what was given that the client window can
1835 * have.
1837 *----------------------------------------------------------------------
1839 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1841 int width = (int)*nwidth;
1842 int height = (int)*nheight;
1843 int winc = 1;
1844 int hinc = 1;
1845 int minW = 1, minH = 1;
1846 int maxW = wwin->screen_ptr->scr_width * 2;
1847 int maxH = wwin->screen_ptr->scr_height * 2;
1848 int minAX = -1, minAY = -1;
1849 int maxAX = -1, maxAY = -1;
1850 int baseW = 0;
1851 int baseH = 0;
1853 if (wwin->normal_hints) {
1854 winc = wwin->normal_hints->width_inc;
1855 hinc = wwin->normal_hints->height_inc;
1856 minW = wwin->normal_hints->min_width;
1857 minH = wwin->normal_hints->min_height;
1858 maxW = wwin->normal_hints->max_width;
1859 maxH = wwin->normal_hints->max_height;
1860 if (wwin->normal_hints->flags & PAspect) {
1861 minAX = wwin->normal_hints->min_aspect.x;
1862 minAY = wwin->normal_hints->min_aspect.y;
1863 maxAX = wwin->normal_hints->max_aspect.x;
1864 maxAY = wwin->normal_hints->max_aspect.y;
1867 baseW = wwin->normal_hints->base_width;
1868 baseH = wwin->normal_hints->base_height;
1871 if (width < minW)
1872 width = minW;
1873 if (height < minH)
1874 height = minH;
1876 if (width > maxW)
1877 width = maxW;
1878 if (height > maxH)
1879 height = maxH;
1881 /* aspect ratio code borrowed from olwm */
1882 if (minAX > 0) {
1883 /* adjust max aspect ratio */
1884 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1885 if (maxAX > maxAY) {
1886 height = (width * maxAY) / maxAX;
1887 if (height > maxH) {
1888 height = maxH;
1889 width = (height * maxAX) / maxAY;
1891 } else {
1892 width = (height * maxAX) / maxAY;
1893 if (width > maxW) {
1894 width = maxW;
1895 height = (width * maxAY) / maxAX;
1900 /* adjust min aspect ratio */
1901 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1902 if (minAX > minAY) {
1903 height = (width * minAY) / minAX;
1904 if (height < minH) {
1905 height = minH;
1906 width = (height * minAX) / minAY;
1908 } else {
1909 width = (height * minAX) / minAY;
1910 if (width < minW) {
1911 width = minW;
1912 height = (width * minAY) / minAX;
1918 if (baseW != 0)
1919 width = (((width - baseW) / winc) * winc) + baseW;
1920 else
1921 width = (((width - minW) / winc) * winc) + minW;
1923 if (baseH != 0)
1924 height = (((height - baseH) / hinc) * hinc) + baseH;
1925 else
1926 height = (((height - minH) / hinc) * hinc) + minH;
1928 /* broken stupid apps may cause preposterous values for these.. */
1929 if (width > 0)
1930 *nwidth = width;
1931 if (height > 0)
1932 *nheight = height;
1935 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1936 unsigned int *width, unsigned int *height)
1938 int baseW = 0, baseH = 0;
1939 int winc = 1, hinc = 1;
1941 if (wwin->normal_hints) {
1942 baseW = wwin->normal_hints->base_width;
1943 baseH = wwin->normal_hints->base_height;
1945 winc = wwin->normal_hints->width_inc;
1946 hinc = wwin->normal_hints->height_inc;
1949 if (*width > maxW)
1950 *width = maxW - (maxW - baseW) % winc;
1952 if (*height > maxH)
1953 *height = maxH - (maxH - baseH) % hinc;
1956 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1958 WScreen *scr = wwin->screen_ptr;
1959 WApplication *wapp;
1960 int unmap = 0;
1962 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1963 return;
1965 if (workspace != scr->current_workspace) {
1966 /* Sent to other workspace. Unmap window */
1967 if ((wwin->flags.mapped
1968 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1969 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1971 wapp = wApplicationOf(wwin->main_window);
1972 if (wapp) {
1973 wapp->last_workspace = workspace;
1975 if (wwin->flags.miniaturized) {
1976 if (wwin->icon) {
1977 XUnmapWindow(dpy, wwin->icon->core->window);
1978 wwin->icon->mapped = 0;
1980 } else {
1981 unmap = 1;
1982 wSetFocusTo(scr, NULL);
1985 } else {
1986 /* brought to current workspace. Map window */
1987 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1988 if (wwin->icon) {
1989 XMapWindow(dpy, wwin->icon->core->window);
1990 wwin->icon->mapped = 1;
1992 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1993 wWindowMap(wwin);
1996 if (!IS_OMNIPRESENT(wwin)) {
1997 int oldWorkspace = wwin->frame->workspace;
1999 wwin->frame->workspace = workspace;
2001 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
2004 if (unmap)
2005 wWindowUnmap(wwin);
2008 void wWindowSynthConfigureNotify(WWindow *wwin)
2010 XEvent sevent;
2012 sevent.type = ConfigureNotify;
2013 sevent.xconfigure.display = dpy;
2014 sevent.xconfigure.event = wwin->client_win;
2015 sevent.xconfigure.window = wwin->client_win;
2017 sevent.xconfigure.x = wwin->client.x;
2018 sevent.xconfigure.y = wwin->client.y;
2019 sevent.xconfigure.width = wwin->client.width;
2020 sevent.xconfigure.height = wwin->client.height;
2022 sevent.xconfigure.border_width = wwin->old_border_width;
2023 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2024 sevent.xconfigure.above = wwin->frame->titlebar->window;
2025 else
2026 sevent.xconfigure.above = None;
2028 sevent.xconfigure.override_redirect = False;
2029 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2030 XFlush(dpy);
2034 *----------------------------------------------------------------------
2035 * wWindowConfigure()
2037 * req_x, req_y: new requested positions for the frame
2038 * req_width, req_height: new requested sizes for the client
2040 * Configures the frame, decorations and client window to the specified
2041 * geometry, whose validity is not checked -- wWindowConstrainSize()
2042 * must be used for that.
2043 * The size parameters are for the client window, but the position is
2044 * for the frame.
2045 * The client window receives a ConfigureNotify event, according
2046 * to what ICCCM says.
2048 * Returns:
2049 * None
2051 * Side effects:
2052 * Window size and position are changed and client window receives
2053 * a ConfigureNotify event.
2054 *----------------------------------------------------------------------
2056 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2058 int synth_notify = False;
2059 int resize;
2061 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2063 * if the window is being moved but not resized then
2064 * send a synthetic ConfigureNotify
2066 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2067 synth_notify = True;
2070 if (WFLAGP(wwin, dont_move_off))
2071 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2072 if (resize) {
2073 if (req_width < MIN_WINDOW_SIZE)
2074 req_width = MIN_WINDOW_SIZE;
2075 if (req_height < MIN_WINDOW_SIZE)
2076 req_height = MIN_WINDOW_SIZE;
2078 /* If growing, resize inner part before frame,
2079 * if shrinking, resize frame before.
2080 * This will prevent the frame (that can have a different color)
2081 * to be exposed, causing flicker */
2082 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2083 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2085 if (wwin->flags.shaded) {
2086 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2087 wwin->old_geometry.height = req_height;
2088 } else {
2089 int h;
2091 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2093 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2096 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2097 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2099 wwin->client.x = req_x;
2100 wwin->client.y = req_y + wwin->frame->top_width;
2101 wwin->client.width = req_width;
2102 wwin->client.height = req_height;
2103 } else {
2104 wwin->client.x = req_x;
2105 wwin->client.y = req_y + wwin->frame->top_width;
2107 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2109 wwin->frame_x = req_x;
2110 wwin->frame_y = req_y;
2111 if (HAS_BORDER(wwin)) {
2112 wwin->client.x += FRAME_BORDER_WIDTH;
2113 wwin->client.y += FRAME_BORDER_WIDTH;
2115 #ifdef SHAPE
2116 if (wShapeSupported && wwin->flags.shaped && resize) {
2117 wWindowSetShape(wwin);
2119 #endif
2121 if (synth_notify)
2122 wWindowSynthConfigureNotify(wwin);
2123 XFlush(dpy);
2126 /* req_x, req_y: new position of the frame */
2127 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2129 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2130 int synth_notify = False;
2132 /* Send a synthetic ConfigureNotify event for every window movement. */
2133 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2134 synth_notify = True;
2136 #else
2137 /* A single synthetic ConfigureNotify event is sent at the end of
2138 * a completed (opaque) movement in moveres.c */
2139 #endif
2141 if (WFLAGP(wwin, dont_move_off))
2142 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2143 wwin->frame->core->width, wwin->frame->core->height);
2145 wwin->client.x = req_x;
2146 wwin->client.y = req_y + wwin->frame->top_width;
2147 if (HAS_BORDER(wwin)) {
2148 wwin->client.x += FRAME_BORDER_WIDTH;
2149 wwin->client.y += FRAME_BORDER_WIDTH;
2152 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2154 wwin->frame_x = req_x;
2155 wwin->frame_y = req_y;
2157 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2158 if (synth_notify)
2159 wWindowSynthConfigureNotify(wwin);
2160 #endif
2163 void wWindowUpdateButtonImages(WWindow *wwin)
2165 WScreen *scr = wwin->screen_ptr;
2166 Pixmap pixmap, mask;
2167 WFrameWindow *fwin = wwin->frame;
2169 if (!HAS_TITLEBAR(wwin))
2170 return;
2172 /* miniaturize button */
2173 if (!WFLAGP(wwin, no_miniaturize_button)) {
2174 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2175 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2177 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2178 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2179 } else {
2180 mask = None;
2183 if (fwin->lbutton_image
2184 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2185 wPixmapDestroy(fwin->lbutton_image);
2186 fwin->lbutton_image = NULL;
2189 if (!fwin->lbutton_image) {
2190 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2191 fwin->lbutton_image->client_owned = 1;
2192 fwin->lbutton_image->client_owned_mask = 1;
2194 } else {
2195 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2196 wPixmapDestroy(fwin->lbutton_image);
2198 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2201 #ifdef XKB_BUTTON_HINT
2202 if (!WFLAGP(wwin, no_language_button)) {
2203 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2204 wPixmapDestroy(fwin->languagebutton_image);
2206 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2208 #endif
2210 /* close button */
2212 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2213 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2215 if (!WFLAGP(wwin, no_close_button)) {
2216 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2217 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2219 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2220 mask = wwin->wm_gnustep_attr->close_mask;
2221 else
2222 mask = None;
2224 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2225 || fwin->rbutton_image->mask != mask)) {
2226 wPixmapDestroy(fwin->rbutton_image);
2227 fwin->rbutton_image = NULL;
2230 if (!fwin->rbutton_image) {
2231 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2232 fwin->rbutton_image->client_owned = 1;
2233 fwin->rbutton_image->client_owned_mask = 1;
2236 } else if (WFLAGP(wwin, kill_close)) {
2238 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2239 wPixmapDestroy(fwin->rbutton_image);
2241 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2243 } else if (MGFLAGP(wwin, broken_close)) {
2245 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2246 wPixmapDestroy(fwin->rbutton_image);
2248 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2250 } else {
2252 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2253 wPixmapDestroy(fwin->rbutton_image);
2255 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2259 /* force buttons to be redrawn */
2260 fwin->flags.need_texture_change = 1;
2261 wFrameWindowPaint(fwin);
2265 *---------------------------------------------------------------------------
2266 * wWindowConfigureBorders--
2267 * Update window border configuration according to attribute flags.
2269 *---------------------------------------------------------------------------
2271 void wWindowConfigureBorders(WWindow *wwin)
2273 if (wwin->frame) {
2274 int flags;
2275 int newy, oldh;
2277 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2279 #ifdef XKB_BUTTON_HINT
2280 if (wPreferences.modelock)
2281 flags |= WFF_LANGUAGE_BUTTON;
2282 #endif
2284 if (HAS_TITLEBAR(wwin))
2285 flags |= WFF_TITLEBAR;
2286 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2287 flags |= WFF_RESIZEBAR;
2288 if (HAS_BORDER(wwin))
2289 flags |= WFF_BORDER;
2290 if (wwin->flags.shaded)
2291 flags |= WFF_IS_SHADED;
2293 oldh = wwin->frame->top_width;
2294 wFrameWindowUpdateBorders(wwin->frame, flags);
2295 if (oldh != wwin->frame->top_width) {
2296 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2298 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2299 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2302 flags = 0;
2303 if (!WFLAGP(wwin, no_miniaturize_button)
2304 && wwin->frame->flags.hide_left_button)
2305 flags |= WFF_LEFT_BUTTON;
2307 #ifdef XKB_BUTTON_HINT
2308 if (!WFLAGP(wwin, no_language_button)
2309 && wwin->frame->flags.hide_language_button) {
2310 flags |= WFF_LANGUAGE_BUTTON;
2312 #endif
2314 if (!WFLAGP(wwin, no_close_button)
2315 && wwin->frame->flags.hide_right_button)
2316 flags |= WFF_RIGHT_BUTTON;
2318 if (flags != 0) {
2319 wWindowUpdateButtonImages(wwin);
2320 wFrameWindowShowButton(wwin->frame, flags);
2323 flags = 0;
2324 if (WFLAGP(wwin, no_miniaturize_button)
2325 && !wwin->frame->flags.hide_left_button)
2326 flags |= WFF_LEFT_BUTTON;
2328 #ifdef XKB_BUTTON_HINT
2329 if (WFLAGP(wwin, no_language_button)
2330 && !wwin->frame->flags.hide_language_button)
2331 flags |= WFF_LANGUAGE_BUTTON;
2332 #endif
2334 if (WFLAGP(wwin, no_close_button)
2335 && !wwin->frame->flags.hide_right_button)
2336 flags |= WFF_RIGHT_BUTTON;
2338 if (flags != 0)
2339 wFrameWindowHideButton(wwin->frame, flags);
2341 #ifdef SHAPE
2342 if (wShapeSupported && wwin->flags.shaped) {
2343 wWindowSetShape(wwin);
2345 #endif
2349 void wWindowSaveState(WWindow * wwin)
2351 CARD32 data[10];
2352 int i;
2354 memset(data, 0, sizeof(CARD32) * 10);
2355 data[0] = wwin->frame->workspace;
2356 data[1] = wwin->flags.miniaturized;
2357 data[2] = wwin->flags.shaded;
2358 data[3] = wwin->flags.hidden;
2359 data[4] = wwin->flags.maximized;
2360 if (wwin->flags.maximized == 0) {
2361 data[5] = wwin->frame_x;
2362 data[6] = wwin->frame_y;
2363 data[7] = wwin->frame->core->width;
2364 data[8] = wwin->frame->core->height;
2365 } else {
2366 data[5] = wwin->old_geometry.x;
2367 data[6] = wwin->old_geometry.y;
2368 data[7] = wwin->old_geometry.width;
2369 data[8] = wwin->old_geometry.height;
2372 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2373 if (wwin->screen_ptr->shortcutWindows[i] &&
2374 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2375 data[9] |= 1 << i;
2377 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2378 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2381 static int getSavedState(Window window, WSavedState ** state)
2383 Atom type_ret;
2384 int fmt_ret;
2385 unsigned long nitems_ret;
2386 unsigned long bytes_after_ret;
2387 CARD32 *data;
2389 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2390 True, _XA_WINDOWMAKER_STATE,
2391 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2392 (unsigned char **)&data) != Success || !data)
2393 return 0;
2395 *state = wmalloc(sizeof(WSavedState));
2397 (*state)->workspace = data[0];
2398 (*state)->miniaturized = data[1];
2399 (*state)->shaded = data[2];
2400 (*state)->hidden = data[3];
2401 (*state)->maximized = data[4];
2402 (*state)->x = data[5];
2403 (*state)->y = data[6];
2404 (*state)->w = data[7];
2405 (*state)->h = data[8];
2406 (*state)->window_shortcuts = data[9];
2408 XFree(data);
2410 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2411 return 1;
2412 else
2413 return 0;
2416 #ifdef SHAPE
2417 void wWindowClearShape(WWindow * wwin)
2419 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2420 0, wwin->frame->top_width, None, ShapeSet);
2421 XFlush(dpy);
2424 void wWindowSetShape(WWindow * wwin)
2426 XRectangle rect[2];
2427 int count;
2428 #ifdef OPTIMIZE_SHAPE
2429 XRectangle *rects;
2430 XRectangle *urec;
2431 int ordering;
2433 /* only shape is the client's */
2434 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2435 goto alt_code;
2438 /* Get array of rectangles describing the shape mask */
2439 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2440 if (!rects) {
2441 goto alt_code;
2444 urec = malloc(sizeof(XRectangle) * (count + 2));
2445 if (!urec) {
2446 XFree(rects);
2447 goto alt_code;
2450 /* insert our decoration rectangles in the rect list */
2451 memcpy(urec, rects, sizeof(XRectangle) * count);
2452 XFree(rects);
2454 if (HAS_TITLEBAR(wwin)) {
2455 urec[count].x = -1;
2456 urec[count].y = -1 - wwin->frame->top_width;
2457 urec[count].width = wwin->frame->core->width + 2;
2458 urec[count].height = wwin->frame->top_width + 1;
2459 count++;
2461 if (HAS_RESIZEBAR(wwin)) {
2462 urec[count].x = -1;
2463 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2464 urec[count].width = wwin->frame->core->width + 2;
2465 urec[count].height = wwin->frame->bottom_width + 1;
2466 count++;
2469 /* shape our frame window */
2470 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2471 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2472 XFlush(dpy);
2473 wfree(urec);
2474 return;
2476 alt_code:
2477 #endif /* OPTIMIZE_SHAPE */
2478 count = 0;
2479 if (HAS_TITLEBAR(wwin)) {
2480 rect[count].x = -1;
2481 rect[count].y = -1;
2482 rect[count].width = wwin->frame->core->width + 2;
2483 rect[count].height = wwin->frame->top_width + 1;
2484 count++;
2486 if (HAS_RESIZEBAR(wwin)) {
2487 rect[count].x = -1;
2488 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2489 rect[count].width = wwin->frame->core->width + 2;
2490 rect[count].height = wwin->frame->bottom_width + 1;
2491 count++;
2493 if (count > 0) {
2494 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2495 0, 0, rect, count, ShapeSet, Unsorted);
2497 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2498 0, wwin->frame->top_width, wwin->client_win,
2499 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2500 XFlush(dpy);
2502 #endif /* SHAPE */
2504 /* ====================================================================== */
2506 static FocusMode getFocusMode(WWindow * wwin)
2508 FocusMode mode;
2510 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2511 if (wwin->wm_hints->input == True) {
2512 if (wwin->protocols.TAKE_FOCUS)
2513 mode = WFM_LOCALLY_ACTIVE;
2514 else
2515 mode = WFM_PASSIVE;
2516 } else {
2517 if (wwin->protocols.TAKE_FOCUS)
2518 mode = WFM_GLOBALLY_ACTIVE;
2519 else
2520 mode = WFM_NO_INPUT;
2522 } else {
2523 mode = WFM_PASSIVE;
2525 return mode;
2528 void wWindowSetKeyGrabs(WWindow * wwin)
2530 int i;
2531 WShortKey *key;
2533 for (i = 0; i < WKBD_LAST; i++) {
2534 key = &wKeyBindings[i];
2536 if (key->keycode == 0)
2537 continue;
2538 if (key->modifier != AnyModifier) {
2539 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2540 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2541 #ifdef NUMLOCK_HACK
2542 /* Also grab all modifier combinations possible that include,
2543 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2544 * work even if the NumLock/ScrollLock key is on.
2546 wHackedGrabKey(key->keycode, key->modifier,
2547 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2548 #endif
2550 XGrabKey(dpy, key->keycode, key->modifier,
2551 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2554 wRootMenuBindShortcuts(wwin->frame->core->window);
2557 void wWindowResetMouseGrabs(WWindow * wwin)
2559 /* Mouse grabs can't be done on the client window because of
2560 * ICCCM and because clients that try to do the same will crash.
2562 * But there is a problem wich makes tbar buttons of unfocused
2563 * windows not usable as the click goes to the frame window instead
2564 * of the button itself. Must figure a way to fix that.
2567 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2569 if (!WFLAGP(wwin, no_bind_mouse)) {
2570 /* grabs for Meta+drag */
2571 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2572 True, ButtonPressMask | ButtonReleaseMask,
2573 GrabModeSync, GrabModeAsync, None, None);
2576 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2577 && !wwin->flags.is_gnustep) {
2578 /* the passive grabs to focus the window */
2579 /* if (wPreferences.focus_mode == WKF_CLICK) */
2580 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2581 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2583 XFlush(dpy);
2586 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2588 if (attr->flags & GSExtraFlagsAttr) {
2589 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2590 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2591 wWindowUpdateButtonImages(wwin);
2596 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2598 WWindowState *wstate;
2600 wstate = malloc(sizeof(WWindowState));
2601 if (!wstate)
2602 return 0;
2604 memset(wstate, 0, sizeof(WWindowState));
2605 wstate->pid = pid;
2606 if (instance)
2607 wstate->instance = wstrdup(instance);
2608 if (class)
2609 wstate->class = wstrdup(class);
2610 if (command)
2611 wstate->command = wstrdup(command);
2612 wstate->state = state;
2614 wstate->next = windowState;
2615 windowState = wstate;
2617 #ifdef DEBUG
2618 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
2619 #endif
2621 return wstate;
2624 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2626 WMagicNumber wWindowGetSavedState(Window win)
2628 char *instance, *class, *command = NULL;
2629 WWindowState *wstate = windowState;
2631 if (!wstate)
2632 return NULL;
2634 command = GetCommandForWindow(win);
2635 if (!command)
2636 return NULL;
2638 if (PropGetWMClass(win, &class, &instance)) {
2639 while (wstate) {
2640 if (SAME(instance, wstate->instance) &&
2641 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2642 break;
2644 wstate = wstate->next;
2646 } else {
2647 wstate = NULL;
2650 #ifdef DEBUG
2651 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
2652 #endif
2654 if (command)
2655 wfree(command);
2656 if (instance)
2657 XFree(instance);
2658 if (class)
2659 XFree(class);
2661 return wstate;
2664 void wWindowDeleteSavedState(WMagicNumber id)
2666 WWindowState *tmp, *wstate = (WWindowState *) id;
2668 if (!wstate || !windowState)
2669 return;
2671 tmp = windowState;
2672 if (tmp == wstate) {
2673 windowState = wstate->next;
2674 #ifdef DEBUG
2675 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2676 wstate, wstate->instance, wstate->class, wstate->command);
2677 #endif
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 #ifdef DEBUG
2691 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2692 wstate, wstate->instance, wstate->class, wstate->command);
2693 #endif
2694 if (wstate->instance)
2695 wfree(wstate->instance);
2696 if (wstate->class)
2697 wfree(wstate->class);
2698 if (wstate->command)
2699 wfree(wstate->command);
2700 wfree(wstate->state);
2701 wfree(wstate);
2702 break;
2704 tmp = tmp->next;
2709 void wWindowDeleteSavedStatesForPID(pid_t pid)
2711 WWindowState *tmp, *wstate;
2713 if (!windowState)
2714 return;
2716 tmp = windowState;
2717 if (tmp->pid == pid) {
2718 wstate = windowState;
2719 windowState = tmp->next;
2720 #ifdef DEBUG
2721 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2722 wstate, wstate->instance, wstate->class, wstate->command);
2723 #endif
2724 if (wstate->instance)
2725 wfree(wstate->instance);
2726 if (wstate->class)
2727 wfree(wstate->class);
2728 if (wstate->command)
2729 wfree(wstate->command);
2730 wfree(wstate->state);
2731 wfree(wstate);
2732 } else {
2733 while (tmp->next) {
2734 if (tmp->next->pid == pid) {
2735 wstate = tmp->next;
2736 tmp->next = wstate->next;
2737 #ifdef DEBUG
2738 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2739 wstate, wstate->instance, wstate->class, wstate->command);
2740 #endif
2741 if (wstate->instance)
2742 wfree(wstate->instance);
2743 if (wstate->class)
2744 wfree(wstate->class);
2745 if (wstate->command)
2746 wfree(wstate->command);
2747 wfree(wstate->state);
2748 wfree(wstate);
2749 break;
2751 tmp = tmp->next;
2756 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2758 if (wwin->flags.omnipresent == flag)
2759 return;
2761 wwin->flags.omnipresent = flag;
2762 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2765 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2767 WWindow *wwin = data;
2769 #ifndef NUMLOCK_HACK
2770 if ((event->xbutton.state & ValidModMask)
2771 != (event->xbutton.state & ~LockMask)) {
2772 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2773 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2775 #endif
2777 event->xbutton.state &= ValidModMask;
2779 CloseWindowMenu(wwin->screen_ptr);
2781 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2782 && !WFLAGP(wwin, no_focusable)) {
2783 wSetFocusTo(wwin->screen_ptr, wwin);
2786 if (event->xbutton.button == Button1)
2787 wRaiseFrame(wwin->frame->core);
2789 if (event->xbutton.window != wwin->frame->resizebar->window) {
2790 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2791 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2792 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2793 #ifdef DEBUG0
2794 wwarning("pointer grab failed for window move");
2795 #endif
2796 return;
2800 if (event->xbutton.state & MOD_MASK) {
2801 /* move the window */
2802 wMouseMoveWindow(wwin, event);
2803 XUngrabPointer(dpy, CurrentTime);
2804 } else {
2805 wMouseResizeWindow(wwin, event);
2806 XUngrabPointer(dpy, CurrentTime);
2810 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2812 WWindow *wwin = data;
2814 event->xbutton.state &= ValidModMask;
2816 if (event->xbutton.button == Button1) {
2817 if (event->xbutton.state == 0) {
2818 if (!WFLAGP(wwin, no_shadeable)) {
2819 /* shade window */
2820 if (wwin->flags.shaded)
2821 wUnshadeWindow(wwin);
2822 else
2823 wShadeWindow(wwin);
2825 } else {
2826 int dir = 0;
2828 if (event->xbutton.state & ControlMask)
2829 dir |= MAX_VERTICAL;
2831 if (event->xbutton.state & ShiftMask) {
2832 dir |= MAX_HORIZONTAL;
2833 if (!(event->xbutton.state & ControlMask))
2834 wSelectWindow(wwin, !wwin->flags.selected);
2837 /* maximize window */
2838 if (dir != 0 && IS_RESIZABLE(wwin)) {
2839 int ndir = dir ^ wwin->flags.maximized;
2841 if (ndir != 0)
2842 wMaximizeWindow(wwin, ndir);
2843 else
2844 wUnmaximizeWindow(wwin);
2847 } else if (event->xbutton.button == Button3) {
2848 if (event->xbutton.state & MOD_MASK) {
2849 wHideOtherApplications(wwin);
2851 } else if (event->xbutton.button == Button2) {
2852 wSelectWindow(wwin, !wwin->flags.selected);
2853 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2854 wShadeWindow(wwin);
2855 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2856 wUnshadeWindow(wwin);
2860 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2862 WWindow *wwin = desc->parent;
2863 unsigned int new_width, w_scale;
2864 unsigned int new_height, h_scale;
2865 unsigned int resize_width_increment = 0;
2866 unsigned int resize_height_increment = 0;
2868 if (wwin->normal_hints) {
2869 w_scale = ceil(wPreferences.resize_increment / wwin->normal_hints->width_inc);
2870 h_scale = ceil(wPreferences.resize_increment / wwin->normal_hints->height_inc);
2871 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2872 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2874 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2875 resize_width_increment = wPreferences.resize_increment;
2876 resize_height_increment = wPreferences.resize_increment;
2879 event->xbutton.state &= ValidModMask;
2881 CloseWindowMenu(wwin->screen_ptr);
2883 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2884 wSetFocusTo(wwin->screen_ptr, wwin);
2886 if (event->xbutton.button == Button1)
2887 wRaiseFrame(wwin->frame->core);
2889 if (event->xbutton.state & MOD_MASK) {
2890 /* move the window */
2891 if (XGrabPointer(dpy, wwin->client_win, False,
2892 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2893 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2894 #ifdef DEBUG0
2895 wwarning("pointer grab failed for window move");
2896 #endif
2897 return;
2899 if (event->xbutton.button == Button3) {
2900 wMouseResizeWindow(wwin, event);
2901 } else if (event->xbutton.button == Button4) {
2902 new_width = wwin->client.width - resize_width_increment;
2903 new_height = wwin->client.height - resize_height_increment;
2904 wWindowConstrainSize(wwin, &new_width,&new_height);
2905 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, new_height);
2906 } else if (event->xbutton.button == Button5) {
2907 new_width = wwin->client.width + resize_width_increment;
2908 new_height = wwin->client.height + resize_height_increment;
2909 wWindowConstrainSize(wwin, &new_width,&new_height);
2910 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, new_height);
2911 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2912 wMouseMoveWindow(wwin, event);
2914 XUngrabPointer(dpy, CurrentTime);
2918 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2920 WWindow *wwin = (WWindow *) data;
2922 #ifndef NUMLOCK_HACK
2923 if ((event->xbutton.state & ValidModMask)
2924 != (event->xbutton.state & ~LockMask)) {
2925 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2926 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2928 #endif
2929 event->xbutton.state &= ValidModMask;
2931 CloseWindowMenu(wwin->screen_ptr);
2933 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2934 && !WFLAGP(wwin, no_focusable)) {
2935 wSetFocusTo(wwin->screen_ptr, wwin);
2938 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2940 if (event->xbutton.button == Button1) {
2941 if (event->xbutton.state & MOD_MASK) {
2942 wLowerFrame(wwin->frame->core);
2943 } else {
2944 wRaiseFrame(wwin->frame->core);
2947 if ((event->xbutton.state & ShiftMask)
2948 && !(event->xbutton.state & ControlMask)) {
2949 wSelectWindow(wwin, !wwin->flags.selected);
2950 return;
2952 if (event->xbutton.window != wwin->frame->titlebar->window
2953 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2954 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2955 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2956 #ifdef DEBUG0
2957 wwarning("pointer grab failed for window move");
2958 #endif
2959 return;
2962 /* move the window */
2963 wMouseMoveWindow(wwin, event);
2965 XUngrabPointer(dpy, CurrentTime);
2966 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2967 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2968 WObjDescriptor *desc;
2970 if (event->xbutton.window != wwin->frame->titlebar->window
2971 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2972 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2973 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2974 #ifdef DEBUG0
2975 wwarning("pointer grab failed for window move");
2976 #endif
2977 return;
2980 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2982 /* allow drag select */
2983 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2984 event->xany.send_event = True;
2985 (*desc->handle_mousedown) (desc, event);
2987 XUngrabPointer(dpy, CurrentTime);
2991 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2993 WWindow *wwin = data;
2995 event->xbutton.state &= ValidModMask;
2997 CloseWindowMenu(wwin->screen_ptr);
2999 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3000 return;
3002 /* if control-click, kill the client */
3003 if (event->xbutton.state & ControlMask) {
3004 wClientKill(wwin);
3005 } else {
3006 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3007 /* send delete message */
3008 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3013 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3015 WWindow *wwin = data;
3017 CloseWindowMenu(wwin->screen_ptr);
3019 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3020 return;
3022 /* send delete message */
3023 if (wwin->protocols.DELETE_WINDOW) {
3024 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3025 } else {
3026 wClientKill(wwin);
3030 #ifdef XKB_BUTTON_HINT
3031 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3033 WWindow *wwin = data;
3034 WFrameWindow *fwin = wwin->frame;
3035 WScreen *scr = fwin->screen_ptr;
3036 XkbStateRec staterec;
3037 int tl;
3039 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3040 return;
3041 tl = wwin->frame->languagemode;
3042 wwin->frame->languagemode = wwin->frame->last_languagemode;
3043 wwin->frame->last_languagemode = tl;
3044 wSetFocusTo(scr, wwin);
3045 wwin->frame->languagebutton_image =
3046 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3047 wFrameWindowUpdateLanguageButton(wwin->frame);
3048 if (event->xbutton.button == Button3)
3049 return;
3050 wRaiseFrame(fwin->core);
3052 #endif
3054 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3056 WWindow *wwin = data;
3058 event->xbutton.state &= ValidModMask;
3060 CloseWindowMenu(wwin->screen_ptr);
3062 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3063 return;
3065 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3066 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3067 } else {
3068 WApplication *wapp;
3069 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3071 wapp = wApplicationOf(wwin->main_window);
3072 if (wapp && !WFLAGP(wwin, no_appicon))
3073 wHideApplication(wapp);
3074 } else if (event->xbutton.state == 0) {
3075 wIconifyWindow(wwin);