Added option to 'configure' to control debug information for compilation
[wmaker-crm.git] / src / window.c
blob9970c1a7eedf71cbdf6df7a2311b7ccce2d7390e
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef SHAPE
27 #include <X11/extensions/shape.h>
28 #endif
29 #ifdef KEEP_XKB_LOCK_STATUS
30 #include <X11/XKBlib.h>
31 #endif /* KEEP_XKB_LOCK_STATUS */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <stdint.h>
36 #include <math.h>
38 /* For getting mouse wheel mappings from WINGs */
39 #include <WINGs/WINGsP.h>
41 #include "WindowMaker.h"
42 #include "GNUstep.h"
43 #include "wcore.h"
44 #include "framewin.h"
45 #include "texture.h"
46 #include "window.h"
47 #include "winspector.h"
48 #include "icon.h"
49 #include "properties.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "colormap.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
59 #include "appmenu.h"
60 #include "appicon.h"
61 #include "superfluous.h"
62 #include "rootmenu.h"
64 #ifdef MWM_HINTS
65 # include "motif.h"
66 #endif
67 #include "wmspec.h"
69 #define MOD_MASK wPreferences.modifier_mask
71 /****** Global Variables ******/
72 extern WShortKey wKeyBindings[WKBD_LAST];
74 #ifdef SHAPE
75 extern Bool wShapeSupported;
76 #endif
78 /* contexts */
79 extern XContext wWinContext;
81 /* protocol atoms */
82 extern Atom _XA_WM_DELETE_WINDOW;
83 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
84 extern Atom _XA_WINDOWMAKER_STATE;
85 extern WPreferences wPreferences;
86 extern Time LastTimestamp;
88 /***** Local Stuff *****/
89 static WWindowState *windowState = NULL;
90 static FocusMode getFocusMode(WWindow *wwin);
91 static int getSavedState(Window window, WSavedState **state);
92 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
94 /* frame window (during window grabs) */
95 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
97 /* close button */
98 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
99 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
101 /* iconify button */
102 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
104 #ifdef XKB_BUTTON_HINT
105 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
106 #endif
108 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
109 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
110 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
112 static void release_wwindowstate(WWindowState *wstate);
114 /****** Notification Observers ******/
116 static void appearanceObserver(void *self, WMNotification * notif)
118 WWindow *wwin = (WWindow *) self;
119 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
121 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
122 return;
124 if (flags & WFontSettings) {
125 wWindowConfigureBorders(wwin);
126 if (wwin->flags.shaded) {
127 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
128 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
129 wWindowSynthConfigureNotify(wwin);
132 if (flags & WTextureSettings)
133 wwin->frame->flags.need_texture_remake = 1;
135 if (flags & (WTextureSettings | WColorSettings)) {
136 if (wwin->frame->titlebar)
137 XClearWindow(dpy, wwin->frame->titlebar->window);
139 wFrameWindowPaint(wwin->frame);
144 /* Return the WWindow associated with a given (Xlib) Window. */
145 WWindow *wWindowFor(Window window)
147 WObjDescriptor *desc;
149 if (window == None)
150 return NULL;
152 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
153 return NULL;
155 if (desc->parent_type == WCLASS_WINDOW)
156 return desc->parent;
157 else if (desc->parent_type == WCLASS_FRAME) {
158 WFrameWindow *frame = (WFrameWindow *) desc->parent;
159 if (frame->flags.is_client_window_frame)
160 return frame->child;
163 return NULL;
166 WWindow *wWindowCreate(void)
168 WWindow *wwin;
170 wwin = wmalloc(sizeof(WWindow));
171 wretain(wwin);
173 wwin->client_descriptor.handle_mousedown = frameMouseDown;
174 wwin->client_descriptor.parent = wwin;
175 wwin->client_descriptor.self = wwin;
176 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
178 return wwin;
181 void wWindowDestroy(WWindow *wwin)
183 int i;
185 if (wwin->screen_ptr->cmap_window == wwin)
186 wwin->screen_ptr->cmap_window = NULL;
188 WMRemoveNotificationObserver(wwin);
190 wwin->flags.destroyed = 1;
192 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
193 if (!wwin->screen_ptr->shortcutWindows[i])
194 continue;
196 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
198 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
199 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
200 wwin->screen_ptr->shortcutWindows[i] = NULL;
204 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
205 wwin->fake_group->retainCount--;
206 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
207 XDestroyWindow(dpy, wwin->fake_group->leader);
208 wwin->fake_group->leader = None;
209 wwin->fake_group->origLeader = None;
210 XFlush(dpy);
214 if (wwin->normal_hints)
215 XFree(wwin->normal_hints);
217 if (wwin->wm_hints)
218 XFree(wwin->wm_hints);
220 if (wwin->wm_instance)
221 XFree(wwin->wm_instance);
223 if (wwin->wm_class)
224 XFree(wwin->wm_class);
226 if (wwin->wm_gnustep_attr)
227 wfree(wwin->wm_gnustep_attr);
229 if (wwin->cmap_windows)
230 XFree(wwin->cmap_windows);
232 XDeleteContext(dpy, wwin->client_win, wWinContext);
234 if (wwin->frame)
235 wFrameWindowDestroy(wwin->frame);
237 if (wwin->icon) {
238 RemoveFromStackList(wwin->icon->core);
239 wIconDestroy(wwin->icon);
240 if (wPreferences.auto_arrange_icons)
241 wArrangeIcons(wwin->screen_ptr, True);
243 if (wwin->net_icon_image)
244 RReleaseImage(wwin->net_icon_image);
246 wrelease(wwin);
249 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
251 if (gs_hints->flags & GSWindowStyleAttr) {
252 if (gs_hints->window_style == WMBorderlessWindowMask) {
253 wwin->client_flags.no_border = 1;
254 wwin->client_flags.no_titlebar = 1;
255 wwin->client_flags.no_closable = 1;
256 wwin->client_flags.no_miniaturizable = 1;
257 wwin->client_flags.no_resizable = 1;
258 wwin->client_flags.no_close_button = 1;
259 wwin->client_flags.no_miniaturize_button = 1;
260 wwin->client_flags.no_resizebar = 1;
261 } else {
262 wwin->client_flags.no_close_button =
263 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
265 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
267 wwin->client_flags.no_miniaturize_button =
268 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
270 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
272 wwin->client_flags.no_resizebar =
273 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
275 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
277 /* these attributes supposedly imply in the existence
278 * of a titlebar */
279 if (gs_hints->window_style & (WMResizableWindowMask |
280 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
281 wwin->client_flags.no_titlebar = 0;
282 } else {
283 wwin->client_flags.no_titlebar =
284 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
288 } else {
289 /* setup the defaults */
290 wwin->client_flags.no_border = 0;
291 wwin->client_flags.no_titlebar = 0;
292 wwin->client_flags.no_closable = 0;
293 wwin->client_flags.no_miniaturizable = 0;
294 wwin->client_flags.no_resizable = 0;
295 wwin->client_flags.no_close_button = 0;
296 wwin->client_flags.no_miniaturize_button = 0;
297 wwin->client_flags.no_resizebar = 0;
299 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
300 wwin->client_flags.no_appicon = 1;
303 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
305 WScreen *scr = wwin->screen_ptr;
307 /* sets global default stuff */
308 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
310 * Decoration setting is done in this precedence (lower to higher)
311 * - use global default in the resource database
312 * - guess some settings
313 * - use GNUstep/external window attributes
314 * - set hints specified for the app in the resource DB
317 WSETUFLAG(wwin, broken_close, 0);
319 if (wwin->protocols.DELETE_WINDOW)
320 WSETUFLAG(wwin, kill_close, 0);
321 else
322 WSETUFLAG(wwin, kill_close, 1);
324 /* transients can't be iconified or maximized */
325 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
326 WSETUFLAG(wwin, no_miniaturizable, 1);
327 WSETUFLAG(wwin, no_miniaturize_button, 1);
330 /* if the window can't be resized, remove the resizebar */
331 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
332 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
333 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
334 WSETUFLAG(wwin, no_resizable, 1);
335 WSETUFLAG(wwin, no_resizebar, 1);
338 /* set GNUstep window attributes */
339 if (wwin->wm_gnustep_attr) {
340 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
342 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
344 *level = wwin->wm_gnustep_attr->window_level;
346 * INT_MIN is the only illegal window level.
348 if (*level == INT_MIN)
349 *level = INT_MIN + 1;
350 } else {
351 /* setup defaults */
352 *level = WMNormalLevel;
354 } else {
355 int tmp_workspace = -1;
356 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
358 #ifdef MWM_HINTS
359 wMWMCheckClientHints(wwin);
360 #endif /* MWM_HINTS */
362 wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
364 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
365 * have INT_MIN that means that no window level was requested. -Dan
367 if (tmp_level == INT_MIN) {
368 if (WFLAGP(wwin, floating))
369 *level = WMFloatingLevel;
370 else if (WFLAGP(wwin, sunken))
371 *level = WMSunkenLevel;
372 else
373 *level = WMNormalLevel;
374 } else {
375 *level = tmp_level;
378 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
379 WWindow *transientOwner = wWindowFor(wwin->transient_for);
380 if (transientOwner) {
381 int ownerLevel = transientOwner->frame->core->stacking->window_level;
382 if (ownerLevel > *level)
383 *level = ownerLevel;
387 if (tmp_workspace >= 0)
388 *workspace = tmp_workspace % scr->workspace_count;
392 * Set attributes specified only for that window/class.
393 * This might do duplicate work with the 1st wDefaultFillAttributes().
395 wDefaultFillAttributes(wwin->wm_instance, wwin->wm_class, &wwin->user_flags,
396 &wwin->defined_user_flags, False);
398 * Sanity checks for attributes that depend on other attributes
400 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
401 wwin->user_flags.emulate_appicon = 0;
403 if (wwin->main_window != None) {
404 WApplication *wapp = wApplicationOf(wwin->main_window);
405 if (wapp && !wapp->flags.emulated)
406 wwin->user_flags.emulate_appicon = 0;
409 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
410 wwin->user_flags.emulate_appicon = 0;
412 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
413 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
414 wwin->user_flags.sunken = 0;
416 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
418 /* windows that have takefocus=False shouldn't take focus at all */
419 if (wwin->focus_mode == WFM_NO_INPUT)
420 wwin->client_flags.no_focusable = 1;
423 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
425 int w1, h1, w2, h2;
427 w1 = wwin->frame->core->width;
428 h1 = wwin->frame->core->height;
429 w2 = obscured->frame->core->width;
430 h2 = obscured->frame->core->height;
432 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
433 && wwin->frame->workspace != obscured->frame->workspace)
434 return False;
436 if (wwin->frame_x + w1 < obscured->frame_x
437 || wwin->frame_y + h1 < obscured->frame_y
438 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
439 return False;
441 return True;
444 static void fixLeaderProperties(WWindow *wwin)
446 XClassHint *classHint;
447 XWMHints *hints, *clientHints;
448 XWindowAttributes attr;
449 Window leaders[2], window;
450 char **argv, *command;
451 int argc, i, pid;
452 Bool haveCommand;
454 classHint = XAllocClassHint();
455 clientHints = XGetWMHints(dpy, wwin->client_win);
456 pid = wNETWMGetPidForWindow(wwin->client_win);
457 if (pid > 0)
458 haveCommand = GetCommandForPid(pid, &argv, &argc);
459 else
460 haveCommand = False;
462 leaders[0] = wwin->client_leader;
463 leaders[1] = wwin->group_id;
465 if (haveCommand) {
466 command = GetCommandForWindow(wwin->client_win);
467 if (command)
468 wfree(command); /* command already set. nothing to do. */
469 else
470 XSetCommand(dpy, wwin->client_win, argv, argc);
473 for (i = 0; i < 2; i++) {
474 window = leaders[i];
475 if (window) {
476 if (XGetClassHint(dpy, window, classHint) == 0) {
477 classHint->res_name = wwin->wm_instance;
478 classHint->res_class = wwin->wm_class;
479 XSetClassHint(dpy, window, classHint);
481 hints = XGetWMHints(dpy, window);
482 if (hints) {
483 XFree(hints);
484 } else if (clientHints) {
485 /* set window group leader to self */
486 clientHints->window_group = window;
487 clientHints->flags |= WindowGroupHint;
488 XSetWMHints(dpy, window, clientHints);
491 if (haveCommand) {
492 command = GetCommandForWindow(window);
493 if (command)
494 wfree(command); /* command already set. nothing to do. */
495 else
496 XSetCommand(dpy, window, argv, argc);
499 /* Make sure we get notification when this window is destroyed */
500 if (XGetWindowAttributes(dpy, window, &attr))
501 XSelectInput(dpy, window, attr.your_event_mask | StructureNotifyMask | PropertyChangeMask);
505 XFree(classHint);
506 if (clientHints)
507 XFree(clientHints);
508 if (haveCommand)
509 wfree(argv);
512 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
514 XClassHint *classHint;
515 XWMHints *hints;
516 Window leader;
517 int argc;
518 char **argv;
520 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
521 /* set class hint */
522 classHint = XAllocClassHint();
523 classHint->res_name = instance;
524 classHint->res_class = class;
525 XSetClassHint(dpy, leader, classHint);
526 XFree(classHint);
528 /* inherit these from the original leader if available */
529 hints = XGetWMHints(dpy, win);
530 if (!hints) {
531 hints = XAllocWMHints();
532 hints->flags = 0;
534 /* set window group leader to self */
535 hints->window_group = leader;
536 hints->flags |= WindowGroupHint;
537 XSetWMHints(dpy, leader, hints);
538 XFree(hints);
540 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
541 XSetCommand(dpy, leader, argv, argc);
542 XFreeStringList(argv);
545 return leader;
548 static int matchIdentifier(const void *item, const void *cdata)
550 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
554 *----------------------------------------------------------------
555 * wManageWindow--
556 * reparents the window and allocates a descriptor for it.
557 * Window manager hints and other hints are fetched to configure
558 * the window decoration attributes and others. User preferences
559 * for the window are used if available, to configure window
560 * decorations and some behaviour.
561 * If in startup, windows that are override redirect,
562 * unmapped and never were managed and are Withdrawn are not
563 * managed.
565 * Returns:
566 * the new window descriptor
568 * Side effects:
569 * The window is reparented and appropriate notification
570 * is done to the client. Input mask for the window is setup.
571 * The window descriptor is also associated with various window
572 * contexts and inserted in the head of the window list.
573 * Event handler contexts are associated for some objects
574 * (buttons, titlebar and resizebar)
576 *----------------------------------------------------------------
578 WWindow *wManageWindow(WScreen *scr, Window window)
580 WWindow *wwin;
581 int x, y;
582 unsigned width, height;
583 XWindowAttributes wattribs;
584 XSetWindowAttributes attribs;
585 WWindowState *win_state;
586 WWindow *transientOwner = NULL;
587 int window_level;
588 int wm_state;
589 int foo;
590 int workspace = -1;
591 char *title;
592 Bool withdraw = False;
593 Bool raise = False;
595 /* mutex. */
596 XGrabServer(dpy);
597 XSync(dpy, False);
598 /* make sure the window is still there */
599 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
600 XUngrabServer(dpy);
601 return NULL;
604 /* if it's an override-redirect, ignore it */
605 if (wattribs.override_redirect) {
606 XUngrabServer(dpy);
607 return NULL;
610 wm_state = PropGetWindowState(window);
612 /* if it's startup and the window is unmapped, don't manage it */
613 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
614 XUngrabServer(dpy);
615 return NULL;
618 wwin = wWindowCreate();
620 title = wNETWMGetWindowName(window);
621 if (title)
622 wwin->flags.net_has_title = 1;
623 if (!title && !wFetchName(dpy, window, &title))
624 title = NULL;
626 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
628 #ifdef SHAPE
629 if (wShapeSupported) {
630 int junk;
631 unsigned int ujunk;
632 int b_shaped;
634 XShapeSelectInput(dpy, window, ShapeNotifyMask);
635 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
636 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
637 wwin->flags.shaped = b_shaped;
639 #endif
642 * Get hints and other information in properties
644 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
646 /* setup descriptor */
647 wwin->client_win = window;
648 wwin->screen_ptr = scr;
649 wwin->old_border_width = wattribs.border_width;
650 wwin->event_mask = CLIENT_EVENTS;
651 attribs.event_mask = CLIENT_EVENTS;
652 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
653 attribs.save_under = False;
655 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
656 XSetWindowBorderWidth(dpy, window, 0);
658 /* get hints from GNUstep app */
659 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
660 wwin->flags.is_gnustep = 1;
662 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
663 wwin->wm_gnustep_attr = NULL;
665 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
666 wwin->flags.is_dockapp = 1;
667 withdraw = True;
670 wwin->client_leader = PropGetClientLeader(window);
671 if (wwin->client_leader != None)
672 wwin->main_window = wwin->client_leader;
674 wwin->wm_hints = XGetWMHints(dpy, window);
676 if (wwin->wm_hints) {
677 if (wwin->wm_hints->flags & StateHint) {
679 if (wwin->wm_hints->initial_state == IconicState) {
680 wwin->flags.miniaturized = 1;
681 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
682 wwin->flags.is_dockapp = 1;
683 withdraw = True;
687 if (wwin->wm_hints->flags & WindowGroupHint) {
688 wwin->group_id = wwin->wm_hints->window_group;
689 /* window_group has priority over CLIENT_LEADER */
690 wwin->main_window = wwin->group_id;
691 } else {
692 wwin->group_id = None;
695 if (wwin->wm_hints->flags & UrgencyHint) {
696 wwin->flags.urgent = 1;
697 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
699 } else {
700 wwin->group_id = None;
703 PropGetProtocols(window, &wwin->protocols);
705 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
706 wwin->transient_for = None;
707 } else {
708 if (wwin->transient_for == None || wwin->transient_for == window) {
709 wwin->transient_for = scr->root_win;
710 } else {
711 transientOwner = wWindowFor(wwin->transient_for);
712 if (transientOwner && transientOwner->main_window != None)
713 wwin->main_window = transientOwner->main_window;
717 /* guess the focus mode */
718 wwin->focus_mode = getFocusMode(wwin);
720 /* get geometry stuff */
721 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
723 /* get colormap windows */
724 GetColormapWindows(wwin);
727 * Setup the decoration/window attributes and
728 * geometry
730 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
732 /* Make broken apps behave as a nice app. */
733 if (WFLAGP(wwin, emulate_appicon))
734 wwin->main_window = wwin->client_win;
736 fixLeaderProperties(wwin);
738 wwin->orig_main_window = wwin->main_window;
740 if (wwin->flags.is_gnustep)
741 WSETUFLAG(wwin, shared_appicon, 0);
743 if (wwin->main_window) {
744 extern Atom _XA_WINDOWMAKER_MENU;
745 XTextProperty text_prop;
747 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
748 WSETUFLAG(wwin, shared_appicon, 0);
751 if (wwin->flags.is_dockapp)
752 WSETUFLAG(wwin, shared_appicon, 0);
754 if (wwin->main_window) {
755 WApplication *app = wApplicationOf(wwin->main_window);
756 if (app && app->app_icon)
757 WSETUFLAG(wwin, shared_appicon, 0);
760 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
761 char *buffer, *instance, *class;
762 WFakeGroupLeader *fPtr;
763 int index;
765 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
767 /* // only enter here if PropGetWMClass() succeds */
768 PropGetWMClass(wwin->main_window, &class, &instance);
769 buffer = StrConcatDot(instance, class);
771 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
772 if (index != WANotFound) {
773 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
774 if (fPtr->retainCount == 0)
775 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
776 instance, class);
778 fPtr->retainCount++;
779 if (fPtr->origLeader == None) {
780 if (ADEQUATE(wwin->main_window)) {
781 fPtr->retainCount++;
782 fPtr->origLeader = wwin->main_window;
785 wwin->fake_group = fPtr;
786 /*wwin->group_id = fPtr->leader; */
787 wwin->main_window = fPtr->leader;
788 wfree(buffer);
789 } else {
790 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
792 fPtr->identifier = buffer;
793 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
794 fPtr->origLeader = None;
795 fPtr->retainCount = 1;
797 WMAddToArray(scr->fakeGroupLeaders, fPtr);
799 if (ADEQUATE(wwin->main_window)) {
800 fPtr->retainCount++;
801 fPtr->origLeader = wwin->main_window;
803 wwin->fake_group = fPtr;
804 /*wwin->group_id = fPtr->leader; */
805 wwin->main_window = fPtr->leader;
807 if (instance)
808 free(instance);
809 if (class)
810 free(class);
811 #undef ADEQUATE
815 * Setup the initial state of the window
817 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
818 wwin->flags.miniaturized = 1;
820 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
821 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
823 wNETWMCheckInitialClientState(wwin);
825 /* apply previous state if it exists and we're in startup */
826 if (scr->flags.startup && wm_state >= 0) {
827 if (wm_state == IconicState) {
828 wwin->flags.miniaturized = 1;
829 } else if (wm_state == WithdrawnState) {
830 withdraw = True;
834 /* if there is a saved state (from file), restore it */
835 win_state = NULL;
836 if (wwin->main_window != None)
837 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
838 else
839 win_state = (WWindowState *) wWindowGetSavedState(window);
841 if (win_state && !withdraw) {
842 if (win_state->state->hidden > 0)
843 wwin->flags.hidden = win_state->state->hidden;
845 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
846 wwin->flags.shaded = win_state->state->shaded;
848 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
849 wwin->flags.miniaturized = win_state->state->miniaturized;
851 if (!IS_OMNIPRESENT(wwin)) {
852 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
853 wwin->wm_class);
854 if (w < 0 || w >= scr->workspace_count) {
855 workspace = win_state->state->workspace;
856 if (workspace >= scr->workspace_count)
857 workspace = scr->current_workspace;
858 } else {
859 workspace = w;
861 } else {
862 workspace = scr->current_workspace;
866 /* if we're restarting, restore saved state (from hints).
867 * This will overwrite previous */
869 WSavedState *wstate;
871 if (getSavedState(window, &wstate)) {
872 wwin->flags.shaded = wstate->shaded;
873 wwin->flags.hidden = wstate->hidden;
874 wwin->flags.miniaturized = wstate->miniaturized;
875 wwin->flags.maximized = wstate->maximized;
876 if (wwin->flags.maximized) {
877 wwin->old_geometry.x = wstate->x;
878 wwin->old_geometry.y = wstate->y;
879 wwin->old_geometry.width = wstate->w;
880 wwin->old_geometry.height = wstate->h;
883 workspace = wstate->workspace;
884 } else {
885 wstate = NULL;
888 /* restore window shortcut */
889 if (wstate != NULL || win_state != NULL) {
890 unsigned mask = 0;
892 if (win_state != NULL)
893 mask = win_state->state->window_shortcuts;
895 if (wstate != NULL && mask == 0)
896 mask = wstate->window_shortcuts;
898 if (mask > 0) {
899 int i;
901 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
902 if (mask & (1 << i)) {
903 if (!scr->shortcutWindows[i])
904 scr->shortcutWindows[i] = WMCreateArray(4);
906 WMAddToArray(scr->shortcutWindows[i], wwin);
911 if (wstate != NULL)
912 wfree(wstate);
915 /* don't let transients start miniaturized if their owners are not */
916 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
917 wwin->flags.miniaturized = 0;
918 if (wwin->wm_hints)
919 wwin->wm_hints->initial_state = NormalState;
922 /* set workspace on which the window starts */
923 if (workspace >= 0) {
924 if (workspace > scr->workspace_count - 1)
925 workspace = workspace % scr->workspace_count;
926 } else {
927 int w;
929 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
931 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
932 workspace = w;
933 } else {
934 if (wPreferences.open_transients_with_parent && transientOwner)
935 workspace = transientOwner->frame->workspace;
936 else
937 workspace = scr->current_workspace;
941 /* setup window geometry */
942 if (win_state && win_state->state->w > 0) {
943 width = win_state->state->w;
944 height = win_state->state->h;
946 wWindowConstrainSize(wwin, &width, &height);
948 /* do not ask for window placement if the window is
949 * transient, during startup, if the initial workspace is another one
950 * or if the window wants to start iconic.
951 * If geometry was saved, restore it. */
953 Bool dontBring = False;
955 if (win_state && win_state->state->w > 0) {
956 x = win_state->state->x;
957 y = win_state->state->y;
958 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
959 && !scr->flags.startup
960 && workspace == scr->current_workspace
961 && !wwin->flags.miniaturized
962 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
964 if (transientOwner && transientOwner->flags.mapped) {
965 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
966 WMRect rect;
967 int head;
969 x = transientOwner->frame_x +
970 abs((transientOwner->frame->core->width - width) / 2) + offs;
971 y = transientOwner->frame_y +
972 abs((transientOwner->frame->core->height - height) / 3) + offs;
975 * limit transient windows to be inside their parent's head
977 rect.pos.x = transientOwner->frame_x;
978 rect.pos.y = transientOwner->frame_y;
979 rect.size.width = transientOwner->frame->core->width;
980 rect.size.height = transientOwner->frame->core->height;
982 head = wGetHeadForRect(scr, rect);
983 rect = wGetRectForHead(scr, head);
985 if (x < rect.pos.x)
986 x = rect.pos.x;
987 else if (x + width > rect.pos.x + rect.size.width)
988 x = rect.pos.x + rect.size.width - width;
990 if (y < rect.pos.y)
991 y = rect.pos.y;
992 else if (y + height > rect.pos.y + rect.size.height)
993 y = rect.pos.y + rect.size.height - height;
995 } else {
996 PlaceWindow(wwin, &x, &y, width, height);
998 if (wPreferences.window_placement == WPM_MANUAL) {
999 dontBring = True;
1001 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1002 int head, flags;
1003 WMRect rect;
1004 int reposition = 0;
1007 * Make spash screens come out in the center of a head
1008 * trouble is that most splashies never get here
1009 * they are managed trough atoms but god knows where.
1010 * Dan, do you know ? -peter
1012 * Most of them are not managed, they have set
1013 * OverrideRedirect, which means we can't do anything about
1014 * them. -alfredo
1018 * xinerama checks for: across head and dead space
1020 rect.pos.x = x;
1021 rect.pos.y = y;
1022 rect.size.width = width;
1023 rect.size.height = height;
1025 head = wGetRectPlacementInfo(scr, rect, &flags);
1027 if (flags & XFLAG_DEAD)
1028 reposition = 1;
1030 if (flags & XFLAG_MULTIPLE)
1031 reposition = 2;
1034 switch (reposition) {
1035 case 1:
1036 head = wGetHeadForPointerLocation(scr);
1037 rect = wGetRectForHead(scr, head);
1039 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1040 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1041 break;
1043 case 2:
1044 rect = wGetRectForHead(scr, head);
1046 if (x < rect.pos.x)
1047 x = rect.pos.x;
1048 else if (x + width > rect.pos.x + rect.size.width)
1049 x = rect.pos.x + rect.size.width - width;
1051 if (y < rect.pos.y)
1052 y = rect.pos.y;
1053 else if (y + height > rect.pos.y + rect.size.height)
1054 y = rect.pos.y + rect.size.height - height;
1056 break;
1058 default:
1059 break;
1063 if (WFLAGP(wwin, dont_move_off) && dontBring)
1064 wScreenBringInside(scr, &x, &y, width, height);
1067 wNETWMPositionSplash(wwin, &x, &y, width, height);
1069 if (wwin->flags.urgent) {
1070 if (!IS_OMNIPRESENT(wwin))
1071 wwin->flags.omnipresent ^= 1;
1075 * Create frame, borders and do reparenting
1077 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1078 #ifdef XKB_BUTTON_HINT
1079 if (wPreferences.modelock)
1080 foo |= WFF_LANGUAGE_BUTTON;
1081 #endif
1082 if (HAS_TITLEBAR(wwin))
1083 foo |= WFF_TITLEBAR;
1084 if (HAS_RESIZEBAR(wwin))
1085 foo |= WFF_RESIZEBAR;
1086 if (HAS_BORDER(wwin))
1087 foo |= WFF_BORDER;
1089 wwin->frame = wFrameWindowCreate(scr, window_level,
1090 x, y, width, height,
1091 &wPreferences.window_title_clearance,
1092 &wPreferences.window_title_min_height,
1093 &wPreferences.window_title_max_height,
1094 foo,
1095 scr->window_title_texture,
1096 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1097 wattribs.depth, wattribs.visual, wattribs.colormap);
1099 wwin->frame->flags.is_client_window_frame = 1;
1100 wwin->frame->flags.justification = wPreferences.title_justification;
1102 /* setup button images */
1103 wWindowUpdateButtonImages(wwin);
1105 /* hide unused buttons */
1106 foo = 0;
1107 if (WFLAGP(wwin, no_close_button))
1108 foo |= WFF_RIGHT_BUTTON;
1109 if (WFLAGP(wwin, no_miniaturize_button))
1110 foo |= WFF_LEFT_BUTTON;
1111 #ifdef XKB_BUTTON_HINT
1112 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1113 foo |= WFF_LANGUAGE_BUTTON;
1114 #endif
1115 if (foo != 0)
1116 wFrameWindowHideButton(wwin->frame, foo);
1118 wwin->frame->child = wwin;
1119 wwin->frame->workspace = workspace;
1120 wwin->frame->on_click_left = windowIconifyClick;
1122 #ifdef XKB_BUTTON_HINT
1123 if (wPreferences.modelock)
1124 wwin->frame->on_click_language = windowLanguageClick;
1125 #endif
1127 wwin->frame->on_click_right = windowCloseClick;
1128 wwin->frame->on_dblclick_right = windowCloseDblClick;
1129 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1130 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1131 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1133 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1134 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1135 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1138 int gx, gy;
1140 wClientGetGravityOffsets(wwin, &gx, &gy);
1142 /* if gravity is to the south, account for the border sizes */
1143 if (gy > 0)
1144 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1147 /* wWindowConfigure() will account for the window border
1148 * when placing so the window would be shifted without
1149 * the adjustment below
1151 if (HAS_BORDER(wwin)) {
1152 WMRect rect;
1153 WArea usableArea;
1154 int head;
1156 rect.pos.x = x;
1157 rect.pos.y = y;
1158 rect.size.width = 1;
1159 rect.size.height = 1;
1161 head = wGetHeadForRect(scr, rect);
1162 usableArea = wGetUsableAreaForHead(scr, head, NULL, True);
1164 if (x >= usableArea.x1 + 2 * FRAME_BORDER_WIDTH)
1165 x -= 2 * FRAME_BORDER_WIDTH;
1166 if (y >= usableArea.y1 + 2 * FRAME_BORDER_WIDTH)
1167 y -= 2 * FRAME_BORDER_WIDTH;
1171 * wWindowConfigure() will init the client window's size
1172 * (wwin->client.{width,height}) and all other geometry
1173 * related variables (frame_x,frame_y)
1175 wWindowConfigure(wwin, x, y, width, height);
1177 /* to make sure the window receives it's new position after reparenting */
1178 wWindowSynthConfigureNotify(wwin);
1181 * Setup descriptors and save window to internal
1182 * lists
1184 if (wwin->main_window != None) {
1185 WApplication *app;
1186 WWindow *leader;
1188 /* Leader windows do not necessary set themselves as leaders.
1189 * If this is the case, point the leader of this window to
1190 * itself */
1191 leader = wWindowFor(wwin->main_window);
1192 if (leader && leader->main_window == None)
1193 leader->main_window = leader->client_win;
1195 app = wApplicationCreate(wwin);
1196 if (app) {
1197 app->last_workspace = workspace;
1200 * Do application specific stuff, like setting application
1201 * wide attributes.
1204 if (wwin->flags.hidden) {
1205 /* if the window was set to hidden because it was hidden
1206 * in a previous incarnation and that state was restored */
1207 app->flags.hidden = 1;
1208 } else if (app->flags.hidden) {
1209 if (WFLAGP(app->main_window_desc, start_hidden)) {
1210 wwin->flags.hidden = 1;
1211 } else {
1212 wUnhideApplication(app, False, False);
1213 raise = True;
1216 wAppBounce(app);
1220 /* setup the frame descriptor */
1221 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1222 wwin->frame->core->descriptor.parent = wwin;
1223 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1225 /* don't let windows go away if we die */
1226 XAddToSaveSet(dpy, window);
1228 XLowerWindow(dpy, window);
1230 /* if window is in this workspace and should be mapped, then map it */
1231 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1232 && !wwin->flags.hidden && !withdraw) {
1234 /* The following "if" is to avoid crashing of clients that expect
1235 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1236 * after the return from this function.
1238 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1239 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1240 else
1241 wClientSetState(wwin, NormalState, None);
1243 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1244 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1246 * The brain damaged idiotic non-click to focus modes will
1247 * have trouble with this because:
1249 * 1. window is created and mapped by the client
1250 * 2. window is mapped by wmaker in small size
1251 * 3. window is animated to grow to normal size
1252 * 4. this function returns to normal event loop
1253 * 5. eventually, the EnterNotify event that would trigger
1254 * the window focusing (if the mouse is over that window)
1255 * will be processed by wmaker.
1256 * But since this event will be rather delayed
1257 * (step 3 has a large delay) the time when the event ocurred
1258 * and when it is processed, the client that owns that window
1259 * will reject the XSetInputFocus() for it.
1261 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1262 DoWindowBirth(wwin);
1264 wWindowMap(wwin);
1267 /* setup stacking descriptor */
1268 if (transientOwner)
1269 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1270 else
1271 wwin->frame->core->stacking->child_of = NULL;
1273 if (!scr->focused_window) {
1274 /* first window on the list */
1275 wwin->next = NULL;
1276 wwin->prev = NULL;
1277 scr->focused_window = wwin;
1278 } else {
1279 WWindow *tmp;
1281 /* add window at beginning of focus window list */
1282 tmp = scr->focused_window;
1283 while (tmp->prev)
1284 tmp = tmp->prev;
1285 tmp->prev = wwin;
1286 wwin->next = tmp;
1287 wwin->prev = NULL;
1290 /* raise is set to true if we un-hid the app when this window was born.
1291 * we raise, else old windows of this app will be above this new one. */
1292 if (raise)
1293 wRaiseFrame(wwin->frame->core);
1295 /* Update name must come after WApplication stuff is done */
1296 wWindowUpdateName(wwin, title);
1297 if (title)
1298 XFree(title);
1300 XUngrabServer(dpy);
1303 * Final preparations before window is ready to go
1305 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1307 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1308 if (((transientOwner && transientOwner->flags.focused)
1309 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1311 /* only auto_focus if on same screen as mouse
1312 * (and same head for xinerama mode)
1313 * TODO: make it an option */
1315 /*TODO add checking the head of the window, is it available? */
1316 short same_screen = 0, same_head = 1;
1318 int foo;
1319 unsigned int bar;
1320 Window dummy;
1322 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1323 &foo, &foo, &foo, &foo, &bar) != False)
1324 same_screen = 1;
1326 if (same_screen == 1 && same_head == 1)
1327 wSetFocusTo(scr, wwin);
1330 wWindowResetMouseGrabs(wwin);
1332 if (!WFLAGP(wwin, no_bind_keys))
1333 wWindowSetKeyGrabs(wwin);
1335 WMPostNotificationName(WMNManaged, wwin, NULL);
1336 wColormapInstallForWindow(scr, scr->cmap_window);
1338 /* Setup Notification Observers */
1339 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1341 /* Cleanup temporary stuff */
1342 if (win_state)
1343 wWindowDeleteSavedState(win_state);
1345 /* If the window must be withdrawed, then do it now.
1346 * Must do some optimization, 'though */
1347 if (withdraw) {
1348 wwin->flags.mapped = 0;
1349 wClientSetState(wwin, WithdrawnState, None);
1350 wUnmanageWindow(wwin, True, False);
1351 wwin = NULL;
1354 return wwin;
1357 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1358 char *title, int x, int y, int width, int height)
1360 WWindow *wwin;
1361 int foo;
1363 wwin = wWindowCreate();
1365 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1367 wwin->flags.internal_window = 1;
1369 WSETUFLAG(wwin, omnipresent, 1);
1370 WSETUFLAG(wwin, no_shadeable, 1);
1371 WSETUFLAG(wwin, no_resizable, 1);
1372 WSETUFLAG(wwin, no_miniaturizable, 1);
1374 wwin->focus_mode = WFM_PASSIVE;
1375 wwin->client_win = window;
1376 wwin->screen_ptr = scr;
1377 wwin->transient_for = owner;
1378 wwin->client.x = x;
1379 wwin->client.y = y;
1380 wwin->client.width = width;
1381 wwin->client.height = height;
1382 wwin->frame_x = wwin->client.x;
1383 wwin->frame_y = wwin->client.y;
1385 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1386 foo |= WFF_TITLEBAR;
1387 #ifdef XKB_BUTTON_HINT
1388 foo |= WFF_LANGUAGE_BUTTON;
1389 #endif
1391 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1392 wwin->frame_x, wwin->frame_y,
1393 width, height,
1394 &wPreferences.window_title_clearance,
1395 &wPreferences.window_title_min_height,
1396 &wPreferences.window_title_max_height,
1397 foo,
1398 scr->window_title_texture,
1399 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1400 scr->w_depth, scr->w_visual, scr->w_colormap);
1402 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1404 wwin->frame->flags.is_client_window_frame = 1;
1405 wwin->frame->flags.justification = wPreferences.title_justification;
1407 wFrameWindowChangeTitle(wwin->frame, title);
1409 /* setup button images */
1410 wWindowUpdateButtonImages(wwin);
1412 /* hide buttons */
1413 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1415 wwin->frame->child = wwin;
1416 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1418 #ifdef XKB_BUTTON_HINT
1419 if (wPreferences.modelock)
1420 wwin->frame->on_click_language = windowLanguageClick;
1421 #endif
1423 wwin->frame->on_click_right = windowCloseClick;
1424 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1425 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1426 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1427 wwin->client.y += wwin->frame->top_width;
1429 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1430 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1432 /* setup the frame descriptor */
1433 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1434 wwin->frame->core->descriptor.parent = wwin;
1435 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1437 XLowerWindow(dpy, window);
1438 XMapSubwindows(dpy, wwin->frame->core->window);
1440 /* setup stacking descriptor */
1441 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1442 WWindow *tmp;
1443 tmp = wWindowFor(wwin->transient_for);
1444 if (tmp)
1445 wwin->frame->core->stacking->child_of = tmp->frame->core;
1446 } else {
1447 wwin->frame->core->stacking->child_of = NULL;
1450 if (!scr->focused_window) {
1451 /* first window on the list */
1452 wwin->next = NULL;
1453 wwin->prev = NULL;
1454 scr->focused_window = wwin;
1455 } else {
1456 WWindow *tmp;
1458 /* add window at beginning of focus window list */
1459 tmp = scr->focused_window;
1460 while (tmp->prev)
1461 tmp = tmp->prev;
1462 tmp->prev = wwin;
1463 wwin->next = tmp;
1464 wwin->prev = NULL;
1467 if (wwin->flags.is_gnustep == 0)
1468 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1470 /* if (wPreferences.auto_focus) */
1471 wSetFocusTo(scr, wwin);
1472 wWindowResetMouseGrabs(wwin);
1473 wWindowSetKeyGrabs(wwin);
1475 return wwin;
1479 *----------------------------------------------------------------------
1480 * wUnmanageWindow--
1481 * Removes the frame window from a window and destroys all data
1482 * related to it. The window will be reparented back to the root window
1483 * if restore is True.
1485 * Side effects:
1486 * Everything related to the window is destroyed and the window
1487 * is removed from the window lists. Focus is set to the previous on the
1488 * window list.
1489 *----------------------------------------------------------------------
1491 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1493 WCoreWindow *frame = wwin->frame->core;
1494 WWindow *owner = NULL;
1495 WWindow *newFocusedWindow = NULL;
1496 int wasFocused;
1497 WScreen *scr = wwin->screen_ptr;
1499 /* First close attribute editor window if open */
1500 if (wwin->flags.inspector_open)
1501 wCloseInspectorForWindow(wwin);
1503 /* Close window menu if it's open for this window */
1504 if (wwin->flags.menu_open_for_me)
1505 CloseWindowMenu(scr);
1507 if (!destroyed) {
1508 if (!wwin->flags.internal_window)
1509 XRemoveFromSaveSet(dpy, wwin->client_win);
1511 /* If this is a leader window, we still need to listen for
1512 * DestroyNotify and PropertyNotify. */
1513 if (wApplicationOf(wwin->client_win))
1514 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1515 else
1516 XSelectInput(dpy, wwin->client_win, NoEventMask);
1518 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1519 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1522 XUnmapWindow(dpy, frame->window);
1524 XUnmapWindow(dpy, wwin->client_win);
1526 /* deselect window */
1527 wSelectWindow(wwin, False);
1529 /* remove all pending events on window */
1530 /* I think this only matters for autoraise */
1531 if (wPreferences.raise_delay)
1532 WMDeleteTimerWithClientData(wwin->frame->core);
1534 XFlush(dpy);
1536 /* reparent the window back to the root */
1537 if (restore)
1538 wClientRestore(wwin);
1540 if (wwin->transient_for != scr->root_win) {
1541 owner = wWindowFor(wwin->transient_for);
1542 if (owner) {
1543 if (!owner->flags.semi_focused)
1544 owner = NULL;
1545 else
1546 owner->flags.semi_focused = 0;
1550 wasFocused = wwin->flags.focused;
1552 /* remove from window focus list */
1553 if (!wwin->prev && !wwin->next) {
1554 /* was the only window */
1555 scr->focused_window = NULL;
1556 newFocusedWindow = NULL;
1557 } else {
1558 WWindow *tmp;
1560 if (wwin->prev)
1561 wwin->prev->next = wwin->next;
1563 if (wwin->next)
1564 wwin->next->prev = wwin->prev;
1565 else {
1566 scr->focused_window = wwin->prev;
1567 scr->focused_window->next = NULL;
1570 if (wPreferences.focus_mode == WKF_CLICK) {
1572 /* if in click to focus mode and the window
1573 * was a transient, focus the owner window
1575 tmp = NULL;
1576 if (wPreferences.focus_mode == WKF_CLICK) {
1577 tmp = wWindowFor(wwin->transient_for);
1578 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1579 tmp = NULL;
1582 /* otherwise, focus the next one in the focus list */
1583 if (!tmp) {
1584 tmp = scr->focused_window;
1585 while (tmp) { /* look for one in the window list first */
1586 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1587 && (tmp->flags.mapped || tmp->flags.shaded))
1588 break;
1589 tmp = tmp->prev;
1591 if (!tmp) { /* if unsuccessful, choose any focusable window */
1592 tmp = scr->focused_window;
1593 while (tmp) {
1594 if (!WFLAGP(tmp, no_focusable)
1595 && (tmp->flags.mapped || tmp->flags.shaded))
1596 break;
1597 tmp = tmp->prev;
1602 newFocusedWindow = tmp;
1604 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1605 unsigned int mask;
1606 int foo;
1607 Window bar, win;
1609 /* This is to let the root window get the keyboard input
1610 * if Sloppy focus mode and no other window get focus.
1611 * This way keybindings will not freeze.
1613 tmp = NULL;
1614 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1615 tmp = wWindowFor(win);
1616 if (tmp == wwin)
1617 tmp = NULL;
1618 newFocusedWindow = tmp;
1619 } else {
1620 newFocusedWindow = NULL;
1624 if (!wwin->flags.internal_window)
1625 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1626 if (wasFocused) {
1627 if (newFocusedWindow != owner && owner) {
1628 if (wwin->flags.is_gnustep == 0)
1629 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1631 wSetFocusTo(scr, newFocusedWindow);
1634 /* Close menu and unhighlight */
1635 WApplication *oapp = wApplicationOf(wwin->main_window);
1636 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1637 if (oapp && oapp != napp) {
1638 wAppMenuUnmap(oapp->menu);
1639 wApplicationDeactivate(oapp);
1642 wNETCleanupFrameExtents(wwin);
1644 wWindowDestroy(wwin);
1645 XFlush(dpy);
1648 void wWindowMap(WWindow *wwin)
1650 XMapWindow(dpy, wwin->frame->core->window);
1651 if (!wwin->flags.shaded) {
1652 /* window will be remapped when getting MapNotify */
1653 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1654 XMapWindow(dpy, wwin->client_win);
1655 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1657 wwin->flags.mapped = 1;
1661 void wWindowUnmap(WWindow *wwin)
1663 wwin->flags.mapped = 0;
1665 /* prevent window withdrawal when getting UnmapNotify */
1666 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1667 XUnmapWindow(dpy, wwin->client_win);
1668 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1670 XUnmapWindow(dpy, wwin->frame->core->window);
1673 void wWindowFocus(WWindow *wwin, WWindow *owin)
1675 WWindow *nowner;
1676 WWindow *oowner;
1678 #ifdef KEEP_XKB_LOCK_STATUS
1679 if (wPreferences.modelock)
1680 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1681 #endif /* KEEP_XKB_LOCK_STATUS */
1683 wwin->flags.semi_focused = 0;
1685 if (wwin->flags.is_gnustep == 0)
1686 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1688 wwin->flags.focused = 1;
1690 wWindowResetMouseGrabs(wwin);
1692 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1694 if (owin == wwin || !owin)
1695 return;
1697 nowner = wWindowFor(wwin->transient_for);
1699 /* new window is a transient for the old window */
1700 if (nowner == owin) {
1701 owin->flags.semi_focused = 1;
1702 wWindowUnfocus(nowner);
1703 return;
1706 oowner = wWindowFor(owin->transient_for);
1708 /* new window is owner of old window */
1709 if (wwin == oowner) {
1710 wWindowUnfocus(owin);
1711 return;
1714 if (!nowner) {
1715 wWindowUnfocus(owin);
1716 return;
1719 /* new window has same owner of old window */
1720 if (oowner == nowner) {
1721 /* prevent unfocusing of owner */
1722 oowner->flags.semi_focused = 0;
1723 wWindowUnfocus(owin);
1724 oowner->flags.semi_focused = 1;
1726 return;
1729 /* nowner != NULL && oowner != nowner */
1730 nowner->flags.semi_focused = 1;
1731 wWindowUnfocus(nowner);
1732 wWindowUnfocus(owin);
1735 void wWindowUnfocus(WWindow *wwin)
1737 CloseWindowMenu(wwin->screen_ptr);
1739 if (wwin->flags.is_gnustep == 0)
1740 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1742 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1743 WWindow *owner;
1744 owner = wWindowFor(wwin->transient_for);
1745 if (owner && owner->flags.semi_focused) {
1746 owner->flags.semi_focused = 0;
1747 if (owner->flags.mapped || owner->flags.shaded) {
1748 wWindowUnfocus(owner);
1749 wFrameWindowPaint(owner->frame);
1753 wwin->flags.focused = 0;
1754 wWindowResetMouseGrabs(wwin);
1755 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1758 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1760 char *title;
1762 if (!wwin->frame)
1763 return;
1765 if (!newTitle)
1766 title = DEF_WINDOW_TITLE; /* the hint was removed */
1767 else
1768 title = newTitle;
1770 if (wFrameWindowChangeTitle(wwin->frame, title))
1771 WMPostNotificationName(WMNChangedName, wwin, NULL);
1775 *----------------------------------------------------------------------
1777 * wWindowConstrainSize--
1778 * Constrains size for the client window, taking the maximal size,
1779 * window resize increments and other size hints into account.
1781 * Returns:
1782 * The closest size to what was given that the client window can
1783 * have.
1785 *----------------------------------------------------------------------
1787 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1789 int width = (int)*nwidth;
1790 int height = (int)*nheight;
1791 int winc = 1;
1792 int hinc = 1;
1793 int minW = 1, minH = 1;
1794 int maxW = wwin->screen_ptr->scr_width * 2;
1795 int maxH = wwin->screen_ptr->scr_height * 2;
1796 int minAX = -1, minAY = -1;
1797 int maxAX = -1, maxAY = -1;
1798 int baseW = 0;
1799 int baseH = 0;
1801 if (wwin->normal_hints) {
1802 winc = wwin->normal_hints->width_inc;
1803 hinc = wwin->normal_hints->height_inc;
1804 minW = wwin->normal_hints->min_width;
1805 minH = wwin->normal_hints->min_height;
1806 maxW = wwin->normal_hints->max_width;
1807 maxH = wwin->normal_hints->max_height;
1808 if (wwin->normal_hints->flags & PAspect) {
1809 minAX = wwin->normal_hints->min_aspect.x;
1810 minAY = wwin->normal_hints->min_aspect.y;
1811 maxAX = wwin->normal_hints->max_aspect.x;
1812 maxAY = wwin->normal_hints->max_aspect.y;
1815 baseW = wwin->normal_hints->base_width;
1816 baseH = wwin->normal_hints->base_height;
1819 if (width < minW)
1820 width = minW;
1821 if (height < minH)
1822 height = minH;
1824 if (width > maxW)
1825 width = maxW;
1826 if (height > maxH)
1827 height = maxH;
1829 /* aspect ratio code borrowed from olwm */
1830 if (minAX > 0) {
1831 /* adjust max aspect ratio */
1832 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1833 if (maxAX > maxAY) {
1834 height = (width * maxAY) / maxAX;
1835 if (height > maxH) {
1836 height = maxH;
1837 width = (height * maxAX) / maxAY;
1839 } else {
1840 width = (height * maxAX) / maxAY;
1841 if (width > maxW) {
1842 width = maxW;
1843 height = (width * maxAY) / maxAX;
1848 /* adjust min aspect ratio */
1849 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1850 if (minAX > minAY) {
1851 height = (width * minAY) / minAX;
1852 if (height < minH) {
1853 height = minH;
1854 width = (height * minAX) / minAY;
1856 } else {
1857 width = (height * minAX) / minAY;
1858 if (width < minW) {
1859 width = minW;
1860 height = (width * minAY) / minAX;
1866 if (baseW != 0)
1867 width = (((width - baseW) / winc) * winc) + baseW;
1868 else
1869 width = (((width - minW) / winc) * winc) + minW;
1871 if (baseH != 0)
1872 height = (((height - baseH) / hinc) * hinc) + baseH;
1873 else
1874 height = (((height - minH) / hinc) * hinc) + minH;
1876 /* broken stupid apps may cause preposterous values for these.. */
1877 if (width > 0)
1878 *nwidth = width;
1879 if (height > 0)
1880 *nheight = height;
1883 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1884 unsigned int *width, unsigned int *height)
1886 int baseW = 0, baseH = 0;
1887 int winc = 1, hinc = 1;
1889 if (wwin->normal_hints) {
1890 baseW = wwin->normal_hints->base_width;
1891 baseH = wwin->normal_hints->base_height;
1893 winc = wwin->normal_hints->width_inc;
1894 hinc = wwin->normal_hints->height_inc;
1897 if (*width > maxW)
1898 *width = maxW - (maxW - baseW) % winc;
1900 if (*height > maxH)
1901 *height = maxH - (maxH - baseH) % hinc;
1904 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1906 WScreen *scr = wwin->screen_ptr;
1907 WApplication *wapp;
1908 int unmap = 0;
1910 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1911 return;
1913 if (workspace != scr->current_workspace) {
1914 /* Sent to other workspace. Unmap window */
1915 if ((wwin->flags.mapped
1916 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1917 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1919 wapp = wApplicationOf(wwin->main_window);
1920 if (wapp)
1921 wapp->last_workspace = workspace;
1923 if (wwin->flags.miniaturized) {
1924 if (wwin->icon) {
1925 XUnmapWindow(dpy, wwin->icon->core->window);
1926 wwin->icon->mapped = 0;
1928 } else {
1929 unmap = 1;
1930 wSetFocusTo(scr, NULL);
1933 } else {
1934 /* brought to current workspace. Map window */
1935 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1936 if (wwin->icon) {
1937 XMapWindow(dpy, wwin->icon->core->window);
1938 wwin->icon->mapped = 1;
1940 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1941 wWindowMap(wwin);
1944 if (!IS_OMNIPRESENT(wwin)) {
1945 int oldWorkspace = wwin->frame->workspace;
1946 wwin->frame->workspace = workspace;
1947 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1950 if (unmap)
1951 wWindowUnmap(wwin);
1954 void wWindowSynthConfigureNotify(WWindow *wwin)
1956 XEvent sevent;
1958 sevent.type = ConfigureNotify;
1959 sevent.xconfigure.display = dpy;
1960 sevent.xconfigure.event = wwin->client_win;
1961 sevent.xconfigure.window = wwin->client_win;
1963 sevent.xconfigure.x = wwin->client.x;
1964 sevent.xconfigure.y = wwin->client.y;
1965 sevent.xconfigure.width = wwin->client.width;
1966 sevent.xconfigure.height = wwin->client.height;
1968 sevent.xconfigure.border_width = wwin->old_border_width;
1969 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1970 sevent.xconfigure.above = wwin->frame->titlebar->window;
1971 else
1972 sevent.xconfigure.above = None;
1974 sevent.xconfigure.override_redirect = False;
1975 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1976 XFlush(dpy);
1980 *----------------------------------------------------------------------
1981 * wWindowConfigure()
1983 * req_x, req_y: new requested positions for the frame
1984 * req_width, req_height: new requested sizes for the client
1986 * Configures the frame, decorations and client window to the specified
1987 * geometry, whose validity is not checked -- wWindowConstrainSize()
1988 * must be used for that.
1989 * The size parameters are for the client window, but the position is
1990 * for the frame.
1991 * The client window receives a ConfigureNotify event, according
1992 * to what ICCCM says.
1994 * Returns:
1995 * None
1997 * Side effects:
1998 * Window size and position are changed and client window receives
1999 * a ConfigureNotify event.
2000 *----------------------------------------------------------------------
2002 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2004 int synth_notify = False;
2005 int resize;
2007 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2009 * if the window is being moved but not resized then
2010 * send a synthetic ConfigureNotify
2012 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2013 synth_notify = True;
2015 if (WFLAGP(wwin, dont_move_off))
2016 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2018 if (resize) {
2019 if (req_width < MIN_WINDOW_SIZE)
2020 req_width = MIN_WINDOW_SIZE;
2021 if (req_height < MIN_WINDOW_SIZE)
2022 req_height = MIN_WINDOW_SIZE;
2024 /* If growing, resize inner part before frame,
2025 * if shrinking, resize frame before.
2026 * This will prevent the frame (that can have a different color)
2027 * to be exposed, causing flicker */
2028 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2029 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2031 if (wwin->flags.shaded) {
2032 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2033 wwin->old_geometry.height = req_height;
2034 } else {
2035 int h;
2037 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2039 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2042 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2043 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2045 wwin->client.x = req_x;
2046 wwin->client.y = req_y + wwin->frame->top_width;
2047 wwin->client.width = req_width;
2048 wwin->client.height = req_height;
2049 } else {
2050 wwin->client.x = req_x;
2051 wwin->client.y = req_y + wwin->frame->top_width;
2053 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2055 wwin->frame_x = req_x;
2056 wwin->frame_y = req_y;
2057 if (HAS_BORDER(wwin)) {
2058 wwin->client.x += FRAME_BORDER_WIDTH;
2059 wwin->client.y += FRAME_BORDER_WIDTH;
2061 #ifdef SHAPE
2062 if (wShapeSupported && wwin->flags.shaped && resize)
2063 wWindowSetShape(wwin);
2064 #endif
2066 if (synth_notify)
2067 wWindowSynthConfigureNotify(wwin);
2069 wNETFrameExtents(wwin);
2071 XFlush(dpy);
2074 /* req_x, req_y: new position of the frame */
2075 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2077 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2078 int synth_notify = False;
2080 /* Send a synthetic ConfigureNotify event for every window movement. */
2081 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2082 synth_notify = True;
2083 #else
2084 /* A single synthetic ConfigureNotify event is sent at the end of
2085 * a completed (opaque) movement in moveres.c */
2086 #endif
2088 if (WFLAGP(wwin, dont_move_off))
2089 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2090 wwin->frame->core->width, wwin->frame->core->height);
2092 wwin->client.x = req_x;
2093 wwin->client.y = req_y + wwin->frame->top_width;
2094 if (HAS_BORDER(wwin)) {
2095 wwin->client.x += FRAME_BORDER_WIDTH;
2096 wwin->client.y += FRAME_BORDER_WIDTH;
2099 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2101 wwin->frame_x = req_x;
2102 wwin->frame_y = req_y;
2104 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2105 if (synth_notify)
2106 wWindowSynthConfigureNotify(wwin);
2107 #endif
2110 void wWindowUpdateButtonImages(WWindow *wwin)
2112 WScreen *scr = wwin->screen_ptr;
2113 Pixmap pixmap, mask;
2114 WFrameWindow *fwin = wwin->frame;
2116 if (!HAS_TITLEBAR(wwin))
2117 return;
2119 /* miniaturize button */
2120 if (!WFLAGP(wwin, no_miniaturize_button)) {
2121 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2122 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2124 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2125 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2126 else
2127 mask = None;
2129 if (fwin->lbutton_image
2130 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2131 wPixmapDestroy(fwin->lbutton_image);
2132 fwin->lbutton_image = NULL;
2135 if (!fwin->lbutton_image) {
2136 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2137 fwin->lbutton_image->client_owned = 1;
2138 fwin->lbutton_image->client_owned_mask = 1;
2140 } else {
2141 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2142 wPixmapDestroy(fwin->lbutton_image);
2144 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2147 #ifdef XKB_BUTTON_HINT
2148 if (!WFLAGP(wwin, no_language_button)) {
2149 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2150 wPixmapDestroy(fwin->languagebutton_image);
2152 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2154 #endif
2156 /* close button */
2158 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2159 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2161 if (!WFLAGP(wwin, no_close_button)) {
2162 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2163 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2165 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2166 mask = wwin->wm_gnustep_attr->close_mask;
2167 else
2168 mask = None;
2170 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2171 || fwin->rbutton_image->mask != mask)) {
2172 wPixmapDestroy(fwin->rbutton_image);
2173 fwin->rbutton_image = NULL;
2176 if (!fwin->rbutton_image) {
2177 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2178 fwin->rbutton_image->client_owned = 1;
2179 fwin->rbutton_image->client_owned_mask = 1;
2182 } else if (WFLAGP(wwin, kill_close)) {
2183 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2184 wPixmapDestroy(fwin->rbutton_image);
2186 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2188 } else if (MGFLAGP(wwin, broken_close)) {
2189 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2190 wPixmapDestroy(fwin->rbutton_image);
2192 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2194 } else {
2195 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2196 wPixmapDestroy(fwin->rbutton_image);
2198 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2202 /* force buttons to be redrawn */
2203 fwin->flags.need_texture_change = 1;
2204 wFrameWindowPaint(fwin);
2208 *---------------------------------------------------------------------------
2209 * wWindowConfigureBorders--
2210 * Update window border configuration according to attribute flags.
2212 *---------------------------------------------------------------------------
2214 void wWindowConfigureBorders(WWindow *wwin)
2216 if (wwin->frame) {
2217 int flags;
2218 int newy, oldh;
2220 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2222 #ifdef XKB_BUTTON_HINT
2223 if (wPreferences.modelock)
2224 flags |= WFF_LANGUAGE_BUTTON;
2225 #endif
2227 if (HAS_TITLEBAR(wwin))
2228 flags |= WFF_TITLEBAR;
2229 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2230 flags |= WFF_RESIZEBAR;
2231 if (HAS_BORDER(wwin))
2232 flags |= WFF_BORDER;
2233 if (wwin->flags.shaded)
2234 flags |= WFF_IS_SHADED;
2236 oldh = wwin->frame->top_width;
2237 wFrameWindowUpdateBorders(wwin->frame, flags);
2238 if (oldh != wwin->frame->top_width) {
2239 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2241 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2242 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2245 flags = 0;
2246 if (!WFLAGP(wwin, no_miniaturize_button)
2247 && wwin->frame->flags.hide_left_button)
2248 flags |= WFF_LEFT_BUTTON;
2250 #ifdef XKB_BUTTON_HINT
2251 if (!WFLAGP(wwin, no_language_button)
2252 && wwin->frame->flags.hide_language_button)
2253 flags |= WFF_LANGUAGE_BUTTON;
2254 #endif
2256 if (!WFLAGP(wwin, no_close_button)
2257 && wwin->frame->flags.hide_right_button)
2258 flags |= WFF_RIGHT_BUTTON;
2260 if (flags != 0) {
2261 wWindowUpdateButtonImages(wwin);
2262 wFrameWindowShowButton(wwin->frame, flags);
2265 flags = 0;
2266 if (WFLAGP(wwin, no_miniaturize_button)
2267 && !wwin->frame->flags.hide_left_button)
2268 flags |= WFF_LEFT_BUTTON;
2270 #ifdef XKB_BUTTON_HINT
2271 if (WFLAGP(wwin, no_language_button)
2272 && !wwin->frame->flags.hide_language_button)
2273 flags |= WFF_LANGUAGE_BUTTON;
2274 #endif
2276 if (WFLAGP(wwin, no_close_button)
2277 && !wwin->frame->flags.hide_right_button)
2278 flags |= WFF_RIGHT_BUTTON;
2280 if (flags != 0)
2281 wFrameWindowHideButton(wwin->frame, flags);
2283 #ifdef SHAPE
2284 if (wShapeSupported && wwin->flags.shaped)
2285 wWindowSetShape(wwin);
2286 #endif
2290 void wWindowSaveState(WWindow * wwin)
2292 long data[10];
2293 int i;
2295 memset(data, 0, sizeof(long) * 10);
2296 data[0] = wwin->frame->workspace;
2297 data[1] = wwin->flags.miniaturized;
2298 data[2] = wwin->flags.shaded;
2299 data[3] = wwin->flags.hidden;
2300 data[4] = wwin->flags.maximized;
2301 if (wwin->flags.maximized == 0) {
2302 data[5] = wwin->frame_x;
2303 data[6] = wwin->frame_y;
2304 data[7] = wwin->frame->core->width;
2305 data[8] = wwin->frame->core->height;
2306 } else {
2307 data[5] = wwin->old_geometry.x;
2308 data[6] = wwin->old_geometry.y;
2309 data[7] = wwin->old_geometry.width;
2310 data[8] = wwin->old_geometry.height;
2313 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2314 if (wwin->screen_ptr->shortcutWindows[i] &&
2315 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2316 data[9] |= 1 << i;
2318 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2319 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2322 static int getSavedState(Window window, WSavedState ** state)
2324 Atom type_ret;
2325 int fmt_ret;
2326 unsigned long nitems_ret;
2327 unsigned long bytes_after_ret;
2328 long *data;
2330 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2331 True, _XA_WINDOWMAKER_STATE,
2332 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2333 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2334 return 0;
2336 *state = wmalloc(sizeof(WSavedState));
2338 (*state)->workspace = data[0];
2339 (*state)->miniaturized = data[1];
2340 (*state)->shaded = data[2];
2341 (*state)->hidden = data[3];
2342 (*state)->maximized = data[4];
2343 (*state)->x = data[5];
2344 (*state)->y = data[6];
2345 (*state)->w = data[7];
2346 (*state)->h = data[8];
2347 (*state)->window_shortcuts = data[9];
2349 XFree(data);
2351 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2352 return 1;
2353 else
2354 return 0;
2357 #ifdef SHAPE
2358 void wWindowClearShape(WWindow * wwin)
2360 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2361 0, wwin->frame->top_width, None, ShapeSet);
2362 XFlush(dpy);
2365 void wWindowSetShape(WWindow * wwin)
2367 XRectangle rect[2];
2368 int count;
2369 #ifdef OPTIMIZE_SHAPE
2370 XRectangle *rects;
2371 XRectangle *urec;
2372 int ordering;
2374 /* only shape is the client's */
2375 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2376 goto alt_code;
2378 /* Get array of rectangles describing the shape mask */
2379 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2380 if (!rects)
2381 goto alt_code;
2383 urec = malloc(sizeof(XRectangle) * (count + 2));
2384 if (!urec) {
2385 XFree(rects);
2386 goto alt_code;
2389 /* insert our decoration rectangles in the rect list */
2390 memcpy(urec, rects, sizeof(XRectangle) * count);
2391 XFree(rects);
2393 if (HAS_TITLEBAR(wwin)) {
2394 urec[count].x = -1;
2395 urec[count].y = -1 - wwin->frame->top_width;
2396 urec[count].width = wwin->frame->core->width + 2;
2397 urec[count].height = wwin->frame->top_width + 1;
2398 count++;
2400 if (HAS_RESIZEBAR(wwin)) {
2401 urec[count].x = -1;
2402 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2403 urec[count].width = wwin->frame->core->width + 2;
2404 urec[count].height = wwin->frame->bottom_width + 1;
2405 count++;
2408 /* shape our frame window */
2409 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2410 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2411 XFlush(dpy);
2412 wfree(urec);
2413 return;
2415 alt_code:
2416 #endif /* OPTIMIZE_SHAPE */
2417 count = 0;
2418 if (HAS_TITLEBAR(wwin)) {
2419 rect[count].x = -1;
2420 rect[count].y = -1;
2421 rect[count].width = wwin->frame->core->width + 2;
2422 rect[count].height = wwin->frame->top_width + 1;
2423 count++;
2425 if (HAS_RESIZEBAR(wwin)) {
2426 rect[count].x = -1;
2427 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2428 rect[count].width = wwin->frame->core->width + 2;
2429 rect[count].height = wwin->frame->bottom_width + 1;
2430 count++;
2432 if (count > 0) {
2433 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2434 0, 0, rect, count, ShapeSet, Unsorted);
2436 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2437 0, wwin->frame->top_width, wwin->client_win,
2438 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2439 XFlush(dpy);
2441 #endif /* SHAPE */
2443 /* ====================================================================== */
2445 static FocusMode getFocusMode(WWindow * wwin)
2447 FocusMode mode;
2449 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2450 if (wwin->wm_hints->input == True) {
2451 if (wwin->protocols.TAKE_FOCUS)
2452 mode = WFM_LOCALLY_ACTIVE;
2453 else
2454 mode = WFM_PASSIVE;
2455 } else {
2456 if (wwin->protocols.TAKE_FOCUS)
2457 mode = WFM_GLOBALLY_ACTIVE;
2458 else
2459 mode = WFM_NO_INPUT;
2461 } else {
2462 mode = WFM_PASSIVE;
2464 return mode;
2467 void wWindowSetKeyGrabs(WWindow * wwin)
2469 int i;
2470 WShortKey *key;
2472 for (i = 0; i < WKBD_LAST; i++) {
2473 key = &wKeyBindings[i];
2475 if (key->keycode == 0)
2476 continue;
2477 if (key->modifier != AnyModifier) {
2478 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2479 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2480 #ifdef NUMLOCK_HACK
2481 /* Also grab all modifier combinations possible that include,
2482 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2483 * work even if the NumLock/ScrollLock key is on.
2485 wHackedGrabKey(key->keycode, key->modifier,
2486 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2487 #endif
2489 XGrabKey(dpy, key->keycode, key->modifier,
2490 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2493 wRootMenuBindShortcuts(wwin->frame->core->window);
2496 void wWindowResetMouseGrabs(WWindow * wwin)
2498 /* Mouse grabs can't be done on the client window because of
2499 * ICCCM and because clients that try to do the same will crash.
2501 * But there is a problem wich makes tbar buttons of unfocused
2502 * windows not usable as the click goes to the frame window instead
2503 * of the button itself. Must figure a way to fix that.
2506 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2508 if (!WFLAGP(wwin, no_bind_mouse)) {
2509 /* grabs for Meta+drag */
2510 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2511 True, ButtonPressMask | ButtonReleaseMask,
2512 GrabModeSync, GrabModeAsync, None, None);
2514 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2515 * but we only grab it for Button4 and Button 5 since a lot of apps
2516 * use CTRL+Button1-3 for app related functionality
2518 if (wPreferences.resize_increment > 0) {
2519 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2520 True, ButtonPressMask | ButtonReleaseMask,
2521 GrabModeSync, GrabModeAsync, None, None);
2522 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2523 True, ButtonPressMask | ButtonReleaseMask,
2524 GrabModeSync, GrabModeAsync, None, None);
2526 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2527 True, ButtonPressMask | ButtonReleaseMask,
2528 GrabModeSync, GrabModeAsync, None, None);
2529 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2530 True, ButtonPressMask | ButtonReleaseMask,
2531 GrabModeSync, GrabModeAsync, None, None);
2535 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2536 && !wwin->flags.is_gnustep) {
2537 /* the passive grabs to focus the window */
2538 /* if (wPreferences.focus_mode == WKF_CLICK) */
2539 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2540 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2542 XFlush(dpy);
2545 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2547 if (attr->flags & GSExtraFlagsAttr) {
2548 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2549 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2550 wWindowUpdateButtonImages(wwin);
2555 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2557 WWindowState *wstate;
2559 wstate = malloc(sizeof(WWindowState));
2560 if (!wstate)
2561 return NULL;
2563 memset(wstate, 0, sizeof(WWindowState));
2564 wstate->pid = pid;
2565 if (instance)
2566 wstate->instance = wstrdup(instance);
2567 if (class)
2568 wstate->class = wstrdup(class);
2569 if (command)
2570 wstate->command = wstrdup(command);
2571 wstate->state = state;
2573 wstate->next = windowState;
2574 windowState = wstate;
2576 return wstate;
2579 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2581 WMagicNumber wWindowGetSavedState(Window win)
2583 char *instance, *class, *command = NULL;
2584 WWindowState *wstate = windowState;
2586 if (!wstate)
2587 return NULL;
2589 command = GetCommandForWindow(win);
2590 if (!command)
2591 return NULL;
2593 if (PropGetWMClass(win, &class, &instance)) {
2594 while (wstate) {
2595 if (SAME(instance, wstate->instance) &&
2596 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2597 break;
2599 wstate = wstate->next;
2601 } else {
2602 wstate = NULL;
2605 if (command)
2606 wfree(command);
2607 if (instance)
2608 free(instance);
2609 if (class)
2610 free(class);
2612 return wstate;
2615 void wWindowDeleteSavedState(WMagicNumber id)
2617 WWindowState *tmp, *wstate = (WWindowState *) id;
2619 if (!wstate || !windowState)
2620 return;
2622 tmp = windowState;
2623 if (tmp == wstate) {
2624 windowState = wstate->next;
2625 release_wwindowstate(wstate);
2626 } else {
2627 while (tmp->next) {
2628 if (tmp->next == wstate) {
2629 tmp->next = wstate->next;
2630 release_wwindowstate(wstate);
2631 break;
2633 tmp = tmp->next;
2638 void wWindowDeleteSavedStatesForPID(pid_t pid)
2640 WWindowState *tmp, *wstate;
2642 if (!windowState)
2643 return;
2645 tmp = windowState;
2646 if (tmp->pid == pid) {
2647 wstate = windowState;
2648 windowState = tmp->next;
2650 release_wwindowstate(wstate);
2651 } else {
2652 while (tmp->next) {
2653 if (tmp->next->pid == pid) {
2654 wstate = tmp->next;
2655 tmp->next = wstate->next;
2656 release_wwindowstate(wstate);
2657 break;
2659 tmp = tmp->next;
2664 static void release_wwindowstate(WWindowState *wstate)
2666 if (wstate->instance)
2667 wfree(wstate->instance);
2669 if (wstate->class)
2670 wfree(wstate->class);
2672 if (wstate->command)
2673 wfree(wstate->command);
2675 wfree(wstate->state);
2676 wfree(wstate);
2679 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2681 if (wwin->flags.omnipresent == flag)
2682 return;
2684 wwin->flags.omnipresent = flag;
2685 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2688 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2690 WWindow *wwin = data;
2692 #ifndef NUMLOCK_HACK
2693 if ((event->xbutton.state & ValidModMask)
2694 != (event->xbutton.state & ~LockMask)) {
2695 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2696 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2698 #endif
2700 event->xbutton.state &= ValidModMask;
2702 CloseWindowMenu(wwin->screen_ptr);
2704 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2705 && !WFLAGP(wwin, no_focusable)) {
2706 wSetFocusTo(wwin->screen_ptr, wwin);
2709 if (event->xbutton.button == Button1)
2710 wRaiseFrame(wwin->frame->core);
2712 if (event->xbutton.window != wwin->frame->resizebar->window) {
2713 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2714 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2715 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2716 return;
2720 if (event->xbutton.state & MOD_MASK) {
2721 /* move the window */
2722 wMouseMoveWindow(wwin, event);
2723 XUngrabPointer(dpy, CurrentTime);
2724 } else {
2725 wMouseResizeWindow(wwin, event);
2726 XUngrabPointer(dpy, CurrentTime);
2730 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2732 WWindow *wwin = data;
2734 event->xbutton.state &= ValidModMask;
2736 if (event->xbutton.button == Button1) {
2737 if (event->xbutton.state == 0) {
2738 if (!WFLAGP(wwin, no_shadeable)) {
2739 /* shade window */
2740 if (wwin->flags.shaded)
2741 wUnshadeWindow(wwin);
2742 else
2743 wShadeWindow(wwin);
2745 } else {
2746 int dir = 0;
2748 if (event->xbutton.state & ControlMask)
2749 dir |= MAX_VERTICAL;
2751 if (event->xbutton.state & ShiftMask) {
2752 dir |= MAX_HORIZONTAL;
2753 if (!(event->xbutton.state & ControlMask))
2754 wSelectWindow(wwin, !wwin->flags.selected);
2757 /* maximize window */
2758 if (dir != 0 && IS_RESIZABLE(wwin)) {
2759 int ndir = dir ^ wwin->flags.maximized;
2761 if (ndir != 0)
2762 wMaximizeWindow(wwin, ndir);
2763 else
2764 wUnmaximizeWindow(wwin);
2767 } else if (event->xbutton.button == Button3) {
2768 if (event->xbutton.state & MOD_MASK)
2769 wHideOtherApplications(wwin);
2770 } else if (event->xbutton.button == Button2) {
2771 wSelectWindow(wwin, !wwin->flags.selected);
2772 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2773 wShadeWindow(wwin);
2774 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2775 wUnshadeWindow(wwin);
2779 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2781 WWindow *wwin = desc->parent;
2782 unsigned int new_width, w_scale;
2783 unsigned int new_height, h_scale;
2784 unsigned int resize_width_increment = 0;
2785 unsigned int resize_height_increment = 0;
2787 if (wwin->normal_hints) {
2788 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2789 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2790 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2791 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2793 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2794 resize_width_increment = wPreferences.resize_increment;
2795 resize_height_increment = wPreferences.resize_increment;
2798 event->xbutton.state &= ValidModMask;
2800 CloseWindowMenu(wwin->screen_ptr);
2802 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2803 wSetFocusTo(wwin->screen_ptr, wwin);
2805 if (event->xbutton.button == Button1)
2806 wRaiseFrame(wwin->frame->core);
2808 if (event->xbutton.state & ControlMask) {
2809 if (event->xbutton.button == Button4) {
2810 new_width = wwin->client.width - resize_width_increment;
2811 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2812 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2814 if (event->xbutton.button == Button5) {
2815 new_width = wwin->client.width + resize_width_increment;
2816 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2817 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2821 if (event->xbutton.state & MOD_MASK) {
2822 /* move the window */
2823 if (XGrabPointer(dpy, wwin->client_win, False,
2824 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2825 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2826 return;
2828 if (event->xbutton.button == Button3) {
2829 wMouseResizeWindow(wwin, event);
2830 } else if (event->xbutton.button == Button4) {
2831 new_height = wwin->client.height - resize_height_increment;
2832 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2833 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2834 } else if (event->xbutton.button == Button5) {
2835 new_height = wwin->client.height + resize_height_increment;
2836 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2837 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2838 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2839 wMouseMoveWindow(wwin, event);
2841 XUngrabPointer(dpy, CurrentTime);
2845 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2847 WWindow *wwin = (WWindow *) data;
2849 #ifndef NUMLOCK_HACK
2850 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2851 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2852 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2853 #endif
2854 event->xbutton.state &= ValidModMask;
2856 CloseWindowMenu(wwin->screen_ptr);
2858 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2859 && !WFLAGP(wwin, no_focusable))
2860 wSetFocusTo(wwin->screen_ptr, wwin);
2862 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2864 if (event->xbutton.button == Button1) {
2865 if (event->xbutton.state & MOD_MASK)
2866 wLowerFrame(wwin->frame->core);
2867 else
2868 wRaiseFrame(wwin->frame->core);
2870 if ((event->xbutton.state & ShiftMask)
2871 && !(event->xbutton.state & ControlMask)) {
2872 wSelectWindow(wwin, !wwin->flags.selected);
2873 return;
2875 if (event->xbutton.window != wwin->frame->titlebar->window
2876 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2877 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2878 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2879 return;
2882 /* move the window */
2883 wMouseMoveWindow(wwin, event);
2885 XUngrabPointer(dpy, CurrentTime);
2886 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2887 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2888 WObjDescriptor *desc;
2890 if (event->xbutton.window != wwin->frame->titlebar->window
2891 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2892 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2893 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2894 return;
2897 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2899 /* allow drag select */
2900 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2901 event->xany.send_event = True;
2902 (*desc->handle_mousedown) (desc, event);
2904 XUngrabPointer(dpy, CurrentTime);
2908 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2910 WWindow *wwin = data;
2912 event->xbutton.state &= ValidModMask;
2914 CloseWindowMenu(wwin->screen_ptr);
2916 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2917 return;
2919 /* if control-click, kill the client */
2920 if (event->xbutton.state & ControlMask) {
2921 wClientKill(wwin);
2922 } else {
2923 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2924 /* send delete message */
2925 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2930 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2932 WWindow *wwin = data;
2934 CloseWindowMenu(wwin->screen_ptr);
2936 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2937 return;
2939 /* send delete message */
2940 if (wwin->protocols.DELETE_WINDOW)
2941 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2942 else
2943 wClientKill(wwin);
2946 #ifdef XKB_BUTTON_HINT
2947 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2949 WWindow *wwin = data;
2950 WFrameWindow *fwin = wwin->frame;
2951 WScreen *scr = fwin->screen_ptr;
2952 int tl;
2954 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2955 return;
2956 tl = wwin->frame->languagemode;
2957 wwin->frame->languagemode = wwin->frame->last_languagemode;
2958 wwin->frame->last_languagemode = tl;
2959 wSetFocusTo(scr, wwin);
2960 wwin->frame->languagebutton_image =
2961 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2962 wFrameWindowUpdateLanguageButton(wwin->frame);
2963 if (event->xbutton.button == Button3)
2964 return;
2965 wRaiseFrame(fwin->core);
2967 #endif
2969 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2971 WWindow *wwin = data;
2973 event->xbutton.state &= ValidModMask;
2975 CloseWindowMenu(wwin->screen_ptr);
2977 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2978 return;
2980 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
2981 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
2982 } else {
2983 WApplication *wapp;
2984 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
2986 wapp = wApplicationOf(wwin->main_window);
2987 if (wapp && !WFLAGP(wwin, no_appicon))
2988 wHideApplication(wapp);
2989 } else if (event->xbutton.state == 0) {
2990 wIconifyWindow(wwin);