Made highlighting the AppIcon of the active app configurable at run time
[wmaker-crm.git] / src / window.c
blob7f12c6573a28b917ef21f2c0174c3ca2796b7f86
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;
594 Bool frame_adjustment = True;
596 /* mutex. */
597 XGrabServer(dpy);
598 XSync(dpy, False);
599 /* make sure the window is still there */
600 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
601 XUngrabServer(dpy);
602 return NULL;
605 /* if it's an override-redirect, ignore it */
606 if (wattribs.override_redirect) {
607 XUngrabServer(dpy);
608 return NULL;
611 wm_state = PropGetWindowState(window);
613 /* if it's startup and the window is unmapped, don't manage it */
614 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
615 XUngrabServer(dpy);
616 return NULL;
619 wwin = wWindowCreate();
621 title = wNETWMGetWindowName(window);
622 if (title)
623 wwin->flags.net_has_title = 1;
624 if (!title && !wFetchName(dpy, window, &title))
625 title = NULL;
627 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
629 #ifdef SHAPE
630 if (wShapeSupported) {
631 int junk;
632 unsigned int ujunk;
633 int b_shaped;
635 XShapeSelectInput(dpy, window, ShapeNotifyMask);
636 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
637 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
638 wwin->flags.shaped = b_shaped;
640 #endif
643 * Get hints and other information in properties
645 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
647 /* setup descriptor */
648 wwin->client_win = window;
649 wwin->screen_ptr = scr;
650 wwin->old_border_width = wattribs.border_width;
651 wwin->event_mask = CLIENT_EVENTS;
652 attribs.event_mask = CLIENT_EVENTS;
653 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
654 attribs.save_under = False;
656 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
657 XSetWindowBorderWidth(dpy, window, 0);
659 /* get hints from GNUstep app */
660 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0)
661 wwin->flags.is_gnustep = 1;
663 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr))
664 wwin->wm_gnustep_attr = NULL;
666 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
667 wwin->flags.is_dockapp = 1;
668 withdraw = True;
671 wwin->client_leader = PropGetClientLeader(window);
672 if (wwin->client_leader != None)
673 wwin->main_window = wwin->client_leader;
675 wwin->wm_hints = XGetWMHints(dpy, window);
677 if (wwin->wm_hints) {
678 if (wwin->wm_hints->flags & StateHint) {
680 if (wwin->wm_hints->initial_state == IconicState) {
681 wwin->flags.miniaturized = 1;
682 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
683 wwin->flags.is_dockapp = 1;
684 withdraw = True;
688 if (wwin->wm_hints->flags & WindowGroupHint) {
689 wwin->group_id = wwin->wm_hints->window_group;
690 /* window_group has priority over CLIENT_LEADER */
691 wwin->main_window = wwin->group_id;
692 } else {
693 wwin->group_id = None;
696 if (wwin->wm_hints->flags & UrgencyHint) {
697 wwin->flags.urgent = 1;
698 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
700 } else {
701 wwin->group_id = None;
704 PropGetProtocols(window, &wwin->protocols);
706 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
707 wwin->transient_for = None;
708 } else {
709 if (wwin->transient_for == None || wwin->transient_for == window) {
710 wwin->transient_for = scr->root_win;
711 } else {
712 transientOwner = wWindowFor(wwin->transient_for);
713 if (transientOwner && transientOwner->main_window != None)
714 wwin->main_window = transientOwner->main_window;
718 /* guess the focus mode */
719 wwin->focus_mode = getFocusMode(wwin);
721 /* get geometry stuff */
722 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
724 /* get colormap windows */
725 GetColormapWindows(wwin);
728 * Setup the decoration/window attributes and
729 * geometry
731 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
733 /* Make broken apps behave as a nice app. */
734 if (WFLAGP(wwin, emulate_appicon))
735 wwin->main_window = wwin->client_win;
737 fixLeaderProperties(wwin);
739 wwin->orig_main_window = wwin->main_window;
741 if (wwin->flags.is_gnustep)
742 WSETUFLAG(wwin, shared_appicon, 0);
744 if (wwin->main_window) {
745 extern Atom _XA_WINDOWMAKER_MENU;
746 XTextProperty text_prop;
748 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU))
749 WSETUFLAG(wwin, shared_appicon, 0);
752 if (wwin->flags.is_dockapp)
753 WSETUFLAG(wwin, shared_appicon, 0);
755 if (wwin->main_window) {
756 WApplication *app = wApplicationOf(wwin->main_window);
757 if (app && app->app_icon)
758 WSETUFLAG(wwin, shared_appicon, 0);
761 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
762 char *buffer, *instance, *class;
763 WFakeGroupLeader *fPtr;
764 int index;
766 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
768 /* // only enter here if PropGetWMClass() succeds */
769 PropGetWMClass(wwin->main_window, &class, &instance);
770 buffer = StrConcatDot(instance, class);
772 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
773 if (index != WANotFound) {
774 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
775 if (fPtr->retainCount == 0)
776 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
777 instance, class);
779 fPtr->retainCount++;
780 if (fPtr->origLeader == None) {
781 if (ADEQUATE(wwin->main_window)) {
782 fPtr->retainCount++;
783 fPtr->origLeader = wwin->main_window;
786 wwin->fake_group = fPtr;
787 /*wwin->group_id = fPtr->leader; */
788 wwin->main_window = fPtr->leader;
789 wfree(buffer);
790 } else {
791 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
793 fPtr->identifier = buffer;
794 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
795 fPtr->origLeader = None;
796 fPtr->retainCount = 1;
798 WMAddToArray(scr->fakeGroupLeaders, fPtr);
800 if (ADEQUATE(wwin->main_window)) {
801 fPtr->retainCount++;
802 fPtr->origLeader = wwin->main_window;
804 wwin->fake_group = fPtr;
805 /*wwin->group_id = fPtr->leader; */
806 wwin->main_window = fPtr->leader;
808 if (instance)
809 free(instance);
810 if (class)
811 free(class);
812 #undef ADEQUATE
816 * Setup the initial state of the window
818 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
819 wwin->flags.miniaturized = 1;
821 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
822 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
824 wNETWMCheckInitialClientState(wwin);
826 /* apply previous state if it exists and we're in startup */
827 if (scr->flags.startup && wm_state >= 0) {
828 if (wm_state == IconicState) {
829 wwin->flags.miniaturized = 1;
830 } else if (wm_state == WithdrawnState) {
831 withdraw = True;
835 /* if there is a saved state (from file), restore it */
836 win_state = NULL;
837 if (wwin->main_window != None)
838 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
839 else
840 win_state = (WWindowState *) wWindowGetSavedState(window);
842 if (win_state && !withdraw) {
843 if (win_state->state->hidden > 0)
844 wwin->flags.hidden = win_state->state->hidden;
846 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
847 wwin->flags.shaded = win_state->state->shaded;
849 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable))
850 wwin->flags.miniaturized = win_state->state->miniaturized;
852 if (!IS_OMNIPRESENT(wwin)) {
853 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
854 wwin->wm_class);
855 if (w < 0 || w >= scr->workspace_count) {
856 workspace = win_state->state->workspace;
857 if (workspace >= scr->workspace_count)
858 workspace = scr->current_workspace;
859 } else {
860 workspace = w;
862 } else {
863 workspace = scr->current_workspace;
867 /* if we're restarting, restore saved state (from hints).
868 * This will overwrite previous */
870 WSavedState *wstate;
872 if (getSavedState(window, &wstate)) {
873 wwin->flags.shaded = wstate->shaded;
874 wwin->flags.hidden = wstate->hidden;
875 wwin->flags.miniaturized = wstate->miniaturized;
876 wwin->flags.maximized = wstate->maximized;
877 if (wwin->flags.maximized) {
878 wwin->old_geometry.x = wstate->x;
879 wwin->old_geometry.y = wstate->y;
880 wwin->old_geometry.width = wstate->w;
881 wwin->old_geometry.height = wstate->h;
884 workspace = wstate->workspace;
885 } else {
886 wstate = NULL;
889 /* restore window shortcut */
890 if (wstate != NULL || win_state != NULL) {
891 unsigned mask = 0;
893 if (win_state != NULL)
894 mask = win_state->state->window_shortcuts;
896 if (wstate != NULL && mask == 0)
897 mask = wstate->window_shortcuts;
899 if (mask > 0) {
900 int i;
902 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
903 if (mask & (1 << i)) {
904 if (!scr->shortcutWindows[i])
905 scr->shortcutWindows[i] = WMCreateArray(4);
907 WMAddToArray(scr->shortcutWindows[i], wwin);
912 if (wstate != NULL)
913 wfree(wstate);
916 /* don't let transients start miniaturized if their owners are not */
917 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
918 wwin->flags.miniaturized = 0;
919 if (wwin->wm_hints)
920 wwin->wm_hints->initial_state = NormalState;
923 /* set workspace on which the window starts */
924 if (workspace >= 0) {
925 if (workspace > scr->workspace_count - 1)
926 workspace = workspace % scr->workspace_count;
927 } else {
928 int w;
930 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
932 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
933 workspace = w;
934 } else {
935 if (wPreferences.open_transients_with_parent && transientOwner)
936 workspace = transientOwner->frame->workspace;
937 else
938 workspace = scr->current_workspace;
942 /* setup window geometry */
943 if (win_state && win_state->state->w > 0) {
944 width = win_state->state->w;
945 height = win_state->state->h;
947 wWindowConstrainSize(wwin, &width, &height);
949 /* do not ask for window placement if the window is
950 * transient, during startup, if the initial workspace is another one
951 * or if the window wants to start iconic.
952 * If geometry was saved, restore it. */
954 Bool dontBring = False;
956 if (win_state && win_state->state->w > 0) {
957 x = win_state->state->x;
958 y = win_state->state->y;
959 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
960 && !scr->flags.startup
961 && workspace == scr->current_workspace
962 && !wwin->flags.miniaturized
963 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
965 if (transientOwner && transientOwner->flags.mapped) {
966 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
967 WMRect rect;
968 int head;
970 x = transientOwner->frame_x +
971 abs((transientOwner->frame->core->width - width) / 2) + offs;
972 y = transientOwner->frame_y +
973 abs((transientOwner->frame->core->height - height) / 3) + offs;
976 * limit transient windows to be inside their parent's head
978 rect.pos.x = transientOwner->frame_x;
979 rect.pos.y = transientOwner->frame_y;
980 rect.size.width = transientOwner->frame->core->width;
981 rect.size.height = transientOwner->frame->core->height;
983 head = wGetHeadForRect(scr, rect);
984 rect = wGetRectForHead(scr, head);
986 if (x < rect.pos.x)
987 x = rect.pos.x;
988 else if (x + width > rect.pos.x + rect.size.width)
989 x = rect.pos.x + rect.size.width - width;
991 if (y < rect.pos.y)
992 y = rect.pos.y;
993 else if (y + height > rect.pos.y + rect.size.height)
994 y = rect.pos.y + rect.size.height - height;
996 } else {
997 PlaceWindow(wwin, &x, &y, width, height);
998 frame_adjustment = False;
1000 if (wPreferences.window_placement == WPM_MANUAL) {
1001 dontBring = True;
1003 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1004 int head, flags;
1005 WMRect rect;
1006 int reposition = 0;
1009 * Make spash screens come out in the center of a head
1010 * trouble is that most splashies never get here
1011 * they are managed trough atoms but god knows where.
1012 * Dan, do you know ? -peter
1014 * Most of them are not managed, they have set
1015 * OverrideRedirect, which means we can't do anything about
1016 * them. -alfredo
1020 * xinerama checks for: across head and dead space
1022 rect.pos.x = x;
1023 rect.pos.y = y;
1024 rect.size.width = width;
1025 rect.size.height = height;
1027 head = wGetRectPlacementInfo(scr, rect, &flags);
1029 if (flags & XFLAG_DEAD)
1030 reposition = 1;
1032 if (flags & XFLAG_MULTIPLE)
1033 reposition = 2;
1036 switch (reposition) {
1037 case 1:
1038 head = wGetHeadForPointerLocation(scr);
1039 rect = wGetRectForHead(scr, head);
1041 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1042 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1043 break;
1045 case 2:
1046 rect = wGetRectForHead(scr, head);
1048 if (x < rect.pos.x)
1049 x = rect.pos.x;
1050 else if (x + width > rect.pos.x + rect.size.width)
1051 x = rect.pos.x + rect.size.width - width;
1053 if (y < rect.pos.y)
1054 y = rect.pos.y;
1055 else if (y + height > rect.pos.y + rect.size.height)
1056 y = rect.pos.y + rect.size.height - height;
1058 break;
1060 default:
1061 break;
1065 if (WFLAGP(wwin, dont_move_off) && dontBring)
1066 wScreenBringInside(scr, &x, &y, width, height);
1069 wNETWMPositionSplash(wwin, &x, &y, width, height);
1071 if (wwin->flags.urgent) {
1072 if (!IS_OMNIPRESENT(wwin))
1073 wwin->flags.omnipresent ^= 1;
1077 * Create frame, borders and do reparenting
1079 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1080 #ifdef XKB_BUTTON_HINT
1081 if (wPreferences.modelock)
1082 foo |= WFF_LANGUAGE_BUTTON;
1083 #endif
1084 if (HAS_TITLEBAR(wwin))
1085 foo |= WFF_TITLEBAR;
1086 if (HAS_RESIZEBAR(wwin))
1087 foo |= WFF_RESIZEBAR;
1088 if (HAS_BORDER(wwin))
1089 foo |= WFF_BORDER;
1091 wwin->frame = wFrameWindowCreate(scr, window_level,
1092 x, y, width, height,
1093 &wPreferences.window_title_clearance,
1094 &wPreferences.window_title_min_height,
1095 &wPreferences.window_title_max_height,
1096 foo,
1097 scr->window_title_texture,
1098 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1099 wattribs.depth, wattribs.visual, wattribs.colormap);
1101 wwin->frame->flags.is_client_window_frame = 1;
1102 wwin->frame->flags.justification = wPreferences.title_justification;
1104 /* setup button images */
1105 wWindowUpdateButtonImages(wwin);
1107 /* hide unused buttons */
1108 foo = 0;
1109 if (WFLAGP(wwin, no_close_button))
1110 foo |= WFF_RIGHT_BUTTON;
1111 if (WFLAGP(wwin, no_miniaturize_button))
1112 foo |= WFF_LEFT_BUTTON;
1113 #ifdef XKB_BUTTON_HINT
1114 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1115 foo |= WFF_LANGUAGE_BUTTON;
1116 #endif
1117 if (foo != 0)
1118 wFrameWindowHideButton(wwin->frame, foo);
1120 wwin->frame->child = wwin;
1121 wwin->frame->workspace = workspace;
1122 wwin->frame->on_click_left = windowIconifyClick;
1124 #ifdef XKB_BUTTON_HINT
1125 if (wPreferences.modelock)
1126 wwin->frame->on_click_language = windowLanguageClick;
1127 #endif
1129 wwin->frame->on_click_right = windowCloseClick;
1130 wwin->frame->on_dblclick_right = windowCloseDblClick;
1131 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1132 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1133 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1135 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1136 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1137 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1140 int gx, gy;
1142 wClientGetGravityOffsets(wwin, &gx, &gy);
1144 /* if gravity is to the south, account for the border sizes */
1145 if (gy > 0)
1146 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1149 if (frame_adjustment) {
1150 WMRect rect;
1151 WArea usableArea;
1152 int head;
1154 rect.pos.x = x;
1155 rect.pos.y = y;
1156 rect.size.width = 1;
1157 rect.size.height = 1;
1159 head = wGetHeadForRect(scr, rect);
1160 usableArea = wGetUsableAreaForHead(scr, head, NULL, True);
1162 /* wWindowConfigure() will account for the frame
1163 * when placing so the window would be shifted without
1164 * the adjustment below
1166 if (y >= usableArea.y1 + wwin->frame->top_width)
1167 y -= wwin->frame->top_width;
1169 /* wWindowConfigure() will account for the window border
1170 * when placing so the window would be shifted without
1171 * the adjustment below
1173 if (HAS_BORDER(wwin)) {
1174 if (x >= usableArea.x1 + FRAME_BORDER_WIDTH)
1175 x -= FRAME_BORDER_WIDTH;
1176 if (y >= usableArea.y1 + FRAME_BORDER_WIDTH)
1177 y -= FRAME_BORDER_WIDTH;
1181 /* If this is a newly-mapped window which had a border,
1182 * the absolute co-ordinates reported to us are actually
1183 * the co-ordinates of the border. We, however, track
1184 * the absolute co-ordinates of the client window, offset
1185 * by the title bar and frame border. As a result
1186 * we need to offset placement of the client by the border
1187 * size so its position matches what we expect.
1189 x += wwin->old_border_width;
1190 y += wwin->old_border_width;
1193 * wWindowConfigure() will init the client window's size
1194 * (wwin->client.{width,height}) and all other geometry
1195 * related variables (frame_x,frame_y)
1197 wWindowConfigure(wwin, x, y, width, height);
1199 /* to make sure the window receives it's new position after reparenting */
1200 wWindowSynthConfigureNotify(wwin);
1203 * Setup descriptors and save window to internal
1204 * lists
1206 if (wwin->main_window != None) {
1207 WApplication *app;
1208 WWindow *leader;
1210 /* Leader windows do not necessary set themselves as leaders.
1211 * If this is the case, point the leader of this window to
1212 * itself */
1213 leader = wWindowFor(wwin->main_window);
1214 if (leader && leader->main_window == None)
1215 leader->main_window = leader->client_win;
1217 app = wApplicationCreate(wwin);
1218 if (app) {
1219 app->last_workspace = workspace;
1222 * Do application specific stuff, like setting application
1223 * wide attributes.
1226 if (wwin->flags.hidden) {
1227 /* if the window was set to hidden because it was hidden
1228 * in a previous incarnation and that state was restored */
1229 app->flags.hidden = 1;
1230 } else if (app->flags.hidden) {
1231 if (WFLAGP(app->main_window_desc, start_hidden)) {
1232 wwin->flags.hidden = 1;
1233 } else {
1234 wUnhideApplication(app, False, False);
1235 raise = True;
1238 wAppBounce(app);
1242 /* setup the frame descriptor */
1243 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1244 wwin->frame->core->descriptor.parent = wwin;
1245 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1247 /* don't let windows go away if we die */
1248 XAddToSaveSet(dpy, window);
1250 XLowerWindow(dpy, window);
1252 /* if window is in this workspace and should be mapped, then map it */
1253 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1254 && !wwin->flags.hidden && !withdraw) {
1256 /* The following "if" is to avoid crashing of clients that expect
1257 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1258 * after the return from this function.
1260 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint))
1261 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1262 else
1263 wClientSetState(wwin, NormalState, None);
1265 if (wPreferences.superfluous && !wPreferences.no_animations && !scr->flags.startup &&
1266 (wwin->transient_for == None || wwin->transient_for == scr->root_win) &&
1268 * The brain damaged idiotic non-click to focus modes will
1269 * have trouble with this because:
1271 * 1. window is created and mapped by the client
1272 * 2. window is mapped by wmaker in small size
1273 * 3. window is animated to grow to normal size
1274 * 4. this function returns to normal event loop
1275 * 5. eventually, the EnterNotify event that would trigger
1276 * the window focusing (if the mouse is over that window)
1277 * will be processed by wmaker.
1278 * But since this event will be rather delayed
1279 * (step 3 has a large delay) the time when the event ocurred
1280 * and when it is processed, the client that owns that window
1281 * will reject the XSetInputFocus() for it.
1283 (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus))
1284 DoWindowBirth(wwin);
1286 wWindowMap(wwin);
1289 /* setup stacking descriptor */
1290 if (transientOwner)
1291 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1292 else
1293 wwin->frame->core->stacking->child_of = NULL;
1295 if (!scr->focused_window) {
1296 /* first window on the list */
1297 wwin->next = NULL;
1298 wwin->prev = NULL;
1299 scr->focused_window = wwin;
1300 } else {
1301 WWindow *tmp;
1303 /* add window at beginning of focus window list */
1304 tmp = scr->focused_window;
1305 while (tmp->prev)
1306 tmp = tmp->prev;
1307 tmp->prev = wwin;
1308 wwin->next = tmp;
1309 wwin->prev = NULL;
1312 /* raise is set to true if we un-hid the app when this window was born.
1313 * we raise, else old windows of this app will be above this new one. */
1314 if (raise)
1315 wRaiseFrame(wwin->frame->core);
1317 /* Update name must come after WApplication stuff is done */
1318 wWindowUpdateName(wwin, title);
1319 if (title)
1320 XFree(title);
1322 XUngrabServer(dpy);
1325 * Final preparations before window is ready to go
1327 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1329 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1330 if (((transientOwner && transientOwner->flags.focused)
1331 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1333 /* only auto_focus if on same screen as mouse
1334 * (and same head for xinerama mode)
1335 * TODO: make it an option */
1337 /*TODO add checking the head of the window, is it available? */
1338 short same_screen = 0, same_head = 1;
1340 int foo;
1341 unsigned int bar;
1342 Window dummy;
1344 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1345 &foo, &foo, &foo, &foo, &bar) != False)
1346 same_screen = 1;
1348 if (same_screen == 1 && same_head == 1)
1349 wSetFocusTo(scr, wwin);
1352 wWindowResetMouseGrabs(wwin);
1354 if (!WFLAGP(wwin, no_bind_keys))
1355 wWindowSetKeyGrabs(wwin);
1357 WMPostNotificationName(WMNManaged, wwin, NULL);
1358 wColormapInstallForWindow(scr, scr->cmap_window);
1360 /* Setup Notification Observers */
1361 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1363 /* Cleanup temporary stuff */
1364 if (win_state)
1365 wWindowDeleteSavedState(win_state);
1367 /* If the window must be withdrawed, then do it now.
1368 * Must do some optimization, 'though */
1369 if (withdraw) {
1370 wwin->flags.mapped = 0;
1371 wClientSetState(wwin, WithdrawnState, None);
1372 wUnmanageWindow(wwin, True, False);
1373 wwin = NULL;
1376 return wwin;
1379 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1380 char *title, int x, int y, int width, int height)
1382 WWindow *wwin;
1383 int foo;
1385 wwin = wWindowCreate();
1387 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1389 wwin->flags.internal_window = 1;
1391 WSETUFLAG(wwin, omnipresent, 1);
1392 WSETUFLAG(wwin, no_shadeable, 1);
1393 WSETUFLAG(wwin, no_resizable, 1);
1394 WSETUFLAG(wwin, no_miniaturizable, 1);
1396 wwin->focus_mode = WFM_PASSIVE;
1397 wwin->client_win = window;
1398 wwin->screen_ptr = scr;
1399 wwin->transient_for = owner;
1400 wwin->client.x = x;
1401 wwin->client.y = y;
1402 wwin->client.width = width;
1403 wwin->client.height = height;
1404 wwin->frame_x = wwin->client.x;
1405 wwin->frame_y = wwin->client.y;
1407 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1408 foo |= WFF_TITLEBAR;
1409 #ifdef XKB_BUTTON_HINT
1410 foo |= WFF_LANGUAGE_BUTTON;
1411 #endif
1413 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1414 wwin->frame_x, wwin->frame_y,
1415 width, height,
1416 &wPreferences.window_title_clearance,
1417 &wPreferences.window_title_min_height,
1418 &wPreferences.window_title_max_height,
1419 foo,
1420 scr->window_title_texture,
1421 scr->resizebar_texture, scr->window_title_color, &scr->title_font,
1422 scr->w_depth, scr->w_visual, scr->w_colormap);
1424 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1426 wwin->frame->flags.is_client_window_frame = 1;
1427 wwin->frame->flags.justification = wPreferences.title_justification;
1429 wFrameWindowChangeTitle(wwin->frame, title);
1431 /* setup button images */
1432 wWindowUpdateButtonImages(wwin);
1434 /* hide buttons */
1435 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1437 wwin->frame->child = wwin;
1438 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1440 #ifdef XKB_BUTTON_HINT
1441 if (wPreferences.modelock)
1442 wwin->frame->on_click_language = windowLanguageClick;
1443 #endif
1445 wwin->frame->on_click_right = windowCloseClick;
1446 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1447 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1448 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1449 wwin->client.y += wwin->frame->top_width;
1451 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1452 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1454 /* setup the frame descriptor */
1455 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1456 wwin->frame->core->descriptor.parent = wwin;
1457 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1459 XLowerWindow(dpy, window);
1460 XMapSubwindows(dpy, wwin->frame->core->window);
1462 /* setup stacking descriptor */
1463 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1464 WWindow *tmp;
1465 tmp = wWindowFor(wwin->transient_for);
1466 if (tmp)
1467 wwin->frame->core->stacking->child_of = tmp->frame->core;
1468 } else {
1469 wwin->frame->core->stacking->child_of = NULL;
1472 if (!scr->focused_window) {
1473 /* first window on the list */
1474 wwin->next = NULL;
1475 wwin->prev = NULL;
1476 scr->focused_window = wwin;
1477 } else {
1478 WWindow *tmp;
1480 /* add window at beginning of focus window list */
1481 tmp = scr->focused_window;
1482 while (tmp->prev)
1483 tmp = tmp->prev;
1484 tmp->prev = wwin;
1485 wwin->next = tmp;
1486 wwin->prev = NULL;
1489 if (wwin->flags.is_gnustep == 0)
1490 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1492 /* if (wPreferences.auto_focus) */
1493 wSetFocusTo(scr, wwin);
1494 wWindowResetMouseGrabs(wwin);
1495 wWindowSetKeyGrabs(wwin);
1497 return wwin;
1501 *----------------------------------------------------------------------
1502 * wUnmanageWindow--
1503 * Removes the frame window from a window and destroys all data
1504 * related to it. The window will be reparented back to the root window
1505 * if restore is True.
1507 * Side effects:
1508 * Everything related to the window is destroyed and the window
1509 * is removed from the window lists. Focus is set to the previous on the
1510 * window list.
1511 *----------------------------------------------------------------------
1513 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1515 WCoreWindow *frame = wwin->frame->core;
1516 WWindow *owner = NULL;
1517 WWindow *newFocusedWindow = NULL;
1518 int wasFocused;
1519 WScreen *scr = wwin->screen_ptr;
1521 /* First close attribute editor window if open */
1522 if (wwin->flags.inspector_open)
1523 wCloseInspectorForWindow(wwin);
1525 /* Close window menu if it's open for this window */
1526 if (wwin->flags.menu_open_for_me)
1527 CloseWindowMenu(scr);
1529 if (!destroyed) {
1530 if (!wwin->flags.internal_window)
1531 XRemoveFromSaveSet(dpy, wwin->client_win);
1533 /* If this is a leader window, we still need to listen for
1534 * DestroyNotify and PropertyNotify. */
1535 if (wApplicationOf(wwin->client_win))
1536 XSelectInput(dpy, wwin->client_win, StructureNotifyMask | PropertyChangeMask);
1537 else
1538 XSelectInput(dpy, wwin->client_win, NoEventMask);
1540 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1541 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1544 XUnmapWindow(dpy, frame->window);
1546 XUnmapWindow(dpy, wwin->client_win);
1548 /* deselect window */
1549 wSelectWindow(wwin, False);
1551 /* remove all pending events on window */
1552 /* I think this only matters for autoraise */
1553 if (wPreferences.raise_delay)
1554 WMDeleteTimerWithClientData(wwin->frame->core);
1556 XFlush(dpy);
1558 /* reparent the window back to the root */
1559 if (restore)
1560 wClientRestore(wwin);
1562 if (wwin->transient_for != scr->root_win) {
1563 owner = wWindowFor(wwin->transient_for);
1564 if (owner) {
1565 if (!owner->flags.semi_focused)
1566 owner = NULL;
1567 else
1568 owner->flags.semi_focused = 0;
1572 wasFocused = wwin->flags.focused;
1574 /* remove from window focus list */
1575 if (!wwin->prev && !wwin->next) {
1576 /* was the only window */
1577 scr->focused_window = NULL;
1578 newFocusedWindow = NULL;
1579 } else {
1580 WWindow *tmp;
1582 if (wwin->prev)
1583 wwin->prev->next = wwin->next;
1585 if (wwin->next)
1586 wwin->next->prev = wwin->prev;
1587 else {
1588 scr->focused_window = wwin->prev;
1589 scr->focused_window->next = NULL;
1592 if (wPreferences.focus_mode == WKF_CLICK) {
1594 /* if in click to focus mode and the window
1595 * was a transient, focus the owner window
1597 tmp = NULL;
1598 if (wPreferences.focus_mode == WKF_CLICK) {
1599 tmp = wWindowFor(wwin->transient_for);
1600 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1601 tmp = NULL;
1604 /* otherwise, focus the next one in the focus list */
1605 if (!tmp) {
1606 tmp = scr->focused_window;
1607 while (tmp) { /* look for one in the window list first */
1608 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1609 && (tmp->flags.mapped || tmp->flags.shaded))
1610 break;
1611 tmp = tmp->prev;
1613 if (!tmp) { /* if unsuccessful, choose any focusable window */
1614 tmp = scr->focused_window;
1615 while (tmp) {
1616 if (!WFLAGP(tmp, no_focusable)
1617 && (tmp->flags.mapped || tmp->flags.shaded))
1618 break;
1619 tmp = tmp->prev;
1624 newFocusedWindow = tmp;
1626 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1627 unsigned int mask;
1628 int foo;
1629 Window bar, win;
1631 /* This is to let the root window get the keyboard input
1632 * if Sloppy focus mode and no other window get focus.
1633 * This way keybindings will not freeze.
1635 tmp = NULL;
1636 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1637 tmp = wWindowFor(win);
1638 if (tmp == wwin)
1639 tmp = NULL;
1640 newFocusedWindow = tmp;
1641 } else {
1642 newFocusedWindow = NULL;
1646 if (!wwin->flags.internal_window)
1647 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1648 if (wasFocused) {
1649 if (newFocusedWindow != owner && owner) {
1650 if (wwin->flags.is_gnustep == 0)
1651 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1653 wSetFocusTo(scr, newFocusedWindow);
1656 /* Close menu and unhighlight */
1657 WApplication *oapp = wApplicationOf(wwin->main_window);
1658 WApplication *napp = scr->focused_window ? wApplicationOf(scr->focused_window->main_window) : NULL;
1659 if (oapp && oapp != napp) {
1660 wAppMenuUnmap(oapp->menu);
1661 if (wPreferences.highlight_active_app)
1662 wApplicationDeactivate(oapp);
1665 wNETCleanupFrameExtents(wwin);
1667 wWindowDestroy(wwin);
1668 XFlush(dpy);
1671 void wWindowMap(WWindow *wwin)
1673 XMapWindow(dpy, wwin->frame->core->window);
1674 if (!wwin->flags.shaded) {
1675 /* window will be remapped when getting MapNotify */
1676 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1677 XMapWindow(dpy, wwin->client_win);
1678 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1680 wwin->flags.mapped = 1;
1684 void wWindowUnmap(WWindow *wwin)
1686 wwin->flags.mapped = 0;
1688 /* prevent window withdrawal when getting UnmapNotify */
1689 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1690 XUnmapWindow(dpy, wwin->client_win);
1691 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1693 XUnmapWindow(dpy, wwin->frame->core->window);
1696 void wWindowFocus(WWindow *wwin, WWindow *owin)
1698 WWindow *nowner;
1699 WWindow *oowner;
1701 #ifdef KEEP_XKB_LOCK_STATUS
1702 if (wPreferences.modelock)
1703 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1704 #endif /* KEEP_XKB_LOCK_STATUS */
1706 wwin->flags.semi_focused = 0;
1708 if (wwin->flags.is_gnustep == 0)
1709 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1711 wwin->flags.focused = 1;
1713 wWindowResetMouseGrabs(wwin);
1715 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1717 if (owin == wwin || !owin)
1718 return;
1720 nowner = wWindowFor(wwin->transient_for);
1722 /* new window is a transient for the old window */
1723 if (nowner == owin) {
1724 owin->flags.semi_focused = 1;
1725 wWindowUnfocus(nowner);
1726 return;
1729 oowner = wWindowFor(owin->transient_for);
1731 /* new window is owner of old window */
1732 if (wwin == oowner) {
1733 wWindowUnfocus(owin);
1734 return;
1737 if (!nowner) {
1738 wWindowUnfocus(owin);
1739 return;
1742 /* new window has same owner of old window */
1743 if (oowner == nowner) {
1744 /* prevent unfocusing of owner */
1745 oowner->flags.semi_focused = 0;
1746 wWindowUnfocus(owin);
1747 oowner->flags.semi_focused = 1;
1749 return;
1752 /* nowner != NULL && oowner != nowner */
1753 nowner->flags.semi_focused = 1;
1754 wWindowUnfocus(nowner);
1755 wWindowUnfocus(owin);
1758 void wWindowUnfocus(WWindow *wwin)
1760 CloseWindowMenu(wwin->screen_ptr);
1762 if (wwin->flags.is_gnustep == 0)
1763 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1765 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1766 WWindow *owner;
1767 owner = wWindowFor(wwin->transient_for);
1768 if (owner && owner->flags.semi_focused) {
1769 owner->flags.semi_focused = 0;
1770 if (owner->flags.mapped || owner->flags.shaded) {
1771 wWindowUnfocus(owner);
1772 wFrameWindowPaint(owner->frame);
1776 wwin->flags.focused = 0;
1777 wWindowResetMouseGrabs(wwin);
1778 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1781 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1783 char *title;
1785 if (!wwin->frame)
1786 return;
1788 if (!newTitle)
1789 title = DEF_WINDOW_TITLE; /* the hint was removed */
1790 else
1791 title = newTitle;
1793 if (wFrameWindowChangeTitle(wwin->frame, title))
1794 WMPostNotificationName(WMNChangedName, wwin, NULL);
1798 *----------------------------------------------------------------------
1800 * wWindowConstrainSize--
1801 * Constrains size for the client window, taking the maximal size,
1802 * window resize increments and other size hints into account.
1804 * Returns:
1805 * The closest size to what was given that the client window can
1806 * have.
1808 *----------------------------------------------------------------------
1810 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1812 int width = (int)*nwidth;
1813 int height = (int)*nheight;
1814 int winc = 1;
1815 int hinc = 1;
1816 int minW = 1, minH = 1;
1817 int maxW = wwin->screen_ptr->scr_width * 2;
1818 int maxH = wwin->screen_ptr->scr_height * 2;
1819 int minAX = -1, minAY = -1;
1820 int maxAX = -1, maxAY = -1;
1821 int baseW = 0;
1822 int baseH = 0;
1824 if (wwin->normal_hints) {
1825 winc = wwin->normal_hints->width_inc;
1826 hinc = wwin->normal_hints->height_inc;
1827 minW = wwin->normal_hints->min_width;
1828 minH = wwin->normal_hints->min_height;
1829 maxW = wwin->normal_hints->max_width;
1830 maxH = wwin->normal_hints->max_height;
1831 if (wwin->normal_hints->flags & PAspect) {
1832 minAX = wwin->normal_hints->min_aspect.x;
1833 minAY = wwin->normal_hints->min_aspect.y;
1834 maxAX = wwin->normal_hints->max_aspect.x;
1835 maxAY = wwin->normal_hints->max_aspect.y;
1838 baseW = wwin->normal_hints->base_width;
1839 baseH = wwin->normal_hints->base_height;
1842 if (width < minW)
1843 width = minW;
1844 if (height < minH)
1845 height = minH;
1847 if (width > maxW)
1848 width = maxW;
1849 if (height > maxH)
1850 height = maxH;
1852 /* aspect ratio code borrowed from olwm */
1853 if (minAX > 0) {
1854 /* adjust max aspect ratio */
1855 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1856 if (maxAX > maxAY) {
1857 height = (width * maxAY) / maxAX;
1858 if (height > maxH) {
1859 height = maxH;
1860 width = (height * maxAX) / maxAY;
1862 } else {
1863 width = (height * maxAX) / maxAY;
1864 if (width > maxW) {
1865 width = maxW;
1866 height = (width * maxAY) / maxAX;
1871 /* adjust min aspect ratio */
1872 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1873 if (minAX > minAY) {
1874 height = (width * minAY) / minAX;
1875 if (height < minH) {
1876 height = minH;
1877 width = (height * minAX) / minAY;
1879 } else {
1880 width = (height * minAX) / minAY;
1881 if (width < minW) {
1882 width = minW;
1883 height = (width * minAY) / minAX;
1889 if (baseW != 0)
1890 width = (((width - baseW) / winc) * winc) + baseW;
1891 else
1892 width = (((width - minW) / winc) * winc) + minW;
1894 if (baseH != 0)
1895 height = (((height - baseH) / hinc) * hinc) + baseH;
1896 else
1897 height = (((height - minH) / hinc) * hinc) + minH;
1899 /* broken stupid apps may cause preposterous values for these.. */
1900 if (width > 0)
1901 *nwidth = width;
1902 if (height > 0)
1903 *nheight = height;
1906 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1907 unsigned int *width, unsigned int *height)
1909 int baseW = 0, baseH = 0;
1910 int winc = 1, hinc = 1;
1912 if (wwin->normal_hints) {
1913 baseW = wwin->normal_hints->base_width;
1914 baseH = wwin->normal_hints->base_height;
1916 winc = wwin->normal_hints->width_inc;
1917 hinc = wwin->normal_hints->height_inc;
1920 if (*width > maxW)
1921 *width = maxW - (maxW - baseW) % winc;
1923 if (*height > maxH)
1924 *height = maxH - (maxH - baseH) % hinc;
1927 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1929 WScreen *scr = wwin->screen_ptr;
1930 WApplication *wapp;
1931 int unmap = 0;
1933 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1934 return;
1936 if (workspace != scr->current_workspace) {
1937 /* Sent to other workspace. Unmap window */
1938 if ((wwin->flags.mapped
1939 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1940 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1942 wapp = wApplicationOf(wwin->main_window);
1943 if (wapp)
1944 wapp->last_workspace = workspace;
1946 if (wwin->flags.miniaturized) {
1947 if (wwin->icon) {
1948 XUnmapWindow(dpy, wwin->icon->core->window);
1949 wwin->icon->mapped = 0;
1951 } else {
1952 unmap = 1;
1953 wSetFocusTo(scr, NULL);
1956 } else {
1957 /* brought to current workspace. Map window */
1958 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1959 if (wwin->icon) {
1960 XMapWindow(dpy, wwin->icon->core->window);
1961 wwin->icon->mapped = 1;
1963 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1964 wWindowMap(wwin);
1967 if (!IS_OMNIPRESENT(wwin)) {
1968 int oldWorkspace = wwin->frame->workspace;
1969 wwin->frame->workspace = workspace;
1970 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1973 if (unmap)
1974 wWindowUnmap(wwin);
1977 void wWindowSynthConfigureNotify(WWindow *wwin)
1979 XEvent sevent;
1981 sevent.type = ConfigureNotify;
1982 sevent.xconfigure.display = dpy;
1983 sevent.xconfigure.event = wwin->client_win;
1984 sevent.xconfigure.window = wwin->client_win;
1986 sevent.xconfigure.x = wwin->client.x;
1987 sevent.xconfigure.y = wwin->client.y;
1988 sevent.xconfigure.width = wwin->client.width;
1989 sevent.xconfigure.height = wwin->client.height;
1991 sevent.xconfigure.border_width = wwin->old_border_width;
1992 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1993 sevent.xconfigure.above = wwin->frame->titlebar->window;
1994 else
1995 sevent.xconfigure.above = None;
1997 sevent.xconfigure.override_redirect = False;
1998 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1999 XFlush(dpy);
2003 *----------------------------------------------------------------------
2004 * wWindowConfigure()
2006 * req_x, req_y: new requested positions for the frame
2007 * req_width, req_height: new requested sizes for the client
2009 * Configures the frame, decorations and client window to the specified
2010 * geometry, whose validity is not checked -- wWindowConstrainSize()
2011 * must be used for that.
2012 * The size parameters are for the client window, but the position is
2013 * for the frame.
2014 * The client window receives a ConfigureNotify event, according
2015 * to what ICCCM says.
2017 * Returns:
2018 * None
2020 * Side effects:
2021 * Window size and position are changed and client window receives
2022 * a ConfigureNotify event.
2023 *----------------------------------------------------------------------
2025 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2027 int synth_notify = False;
2028 int resize;
2030 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2032 * if the window is being moved but not resized then
2033 * send a synthetic ConfigureNotify
2035 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize)
2036 synth_notify = True;
2038 if (WFLAGP(wwin, dont_move_off))
2039 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2041 if (resize) {
2042 if (req_width < MIN_WINDOW_SIZE)
2043 req_width = MIN_WINDOW_SIZE;
2044 if (req_height < MIN_WINDOW_SIZE)
2045 req_height = MIN_WINDOW_SIZE;
2047 /* If growing, resize inner part before frame,
2048 * if shrinking, resize frame before.
2049 * This will prevent the frame (that can have a different color)
2050 * to be exposed, causing flicker */
2051 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2052 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2054 if (wwin->flags.shaded) {
2055 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2056 wwin->old_geometry.height = req_height;
2057 } else {
2058 int h;
2060 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2062 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2065 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2066 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2068 wwin->client.x = req_x;
2069 wwin->client.y = req_y + wwin->frame->top_width;
2070 wwin->client.width = req_width;
2071 wwin->client.height = req_height;
2072 } else {
2073 wwin->client.x = req_x;
2074 wwin->client.y = req_y + wwin->frame->top_width;
2076 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2078 wwin->frame_x = req_x;
2079 wwin->frame_y = req_y;
2080 if (HAS_BORDER(wwin)) {
2081 wwin->client.x += FRAME_BORDER_WIDTH;
2082 wwin->client.y += FRAME_BORDER_WIDTH;
2084 #ifdef SHAPE
2085 if (wShapeSupported && wwin->flags.shaped && resize)
2086 wWindowSetShape(wwin);
2087 #endif
2089 if (synth_notify)
2090 wWindowSynthConfigureNotify(wwin);
2092 wNETFrameExtents(wwin);
2094 XFlush(dpy);
2097 /* req_x, req_y: new position of the frame */
2098 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2100 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2101 int synth_notify = False;
2103 /* Send a synthetic ConfigureNotify event for every window movement. */
2104 if ((req_x != wwin->frame_x || req_y != wwin->frame_y))
2105 synth_notify = True;
2106 #else
2107 /* A single synthetic ConfigureNotify event is sent at the end of
2108 * a completed (opaque) movement in moveres.c */
2109 #endif
2111 if (WFLAGP(wwin, dont_move_off))
2112 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2113 wwin->frame->core->width, wwin->frame->core->height);
2115 wwin->client.x = req_x;
2116 wwin->client.y = req_y + wwin->frame->top_width;
2117 if (HAS_BORDER(wwin)) {
2118 wwin->client.x += FRAME_BORDER_WIDTH;
2119 wwin->client.y += FRAME_BORDER_WIDTH;
2122 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2124 wwin->frame_x = req_x;
2125 wwin->frame_y = req_y;
2127 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2128 if (synth_notify)
2129 wWindowSynthConfigureNotify(wwin);
2130 #endif
2133 void wWindowUpdateButtonImages(WWindow *wwin)
2135 WScreen *scr = wwin->screen_ptr;
2136 Pixmap pixmap, mask;
2137 WFrameWindow *fwin = wwin->frame;
2139 if (!HAS_TITLEBAR(wwin))
2140 return;
2142 /* miniaturize button */
2143 if (!WFLAGP(wwin, no_miniaturize_button)) {
2144 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2145 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2147 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr)
2148 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2149 else
2150 mask = None;
2152 if (fwin->lbutton_image
2153 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2154 wPixmapDestroy(fwin->lbutton_image);
2155 fwin->lbutton_image = NULL;
2158 if (!fwin->lbutton_image) {
2159 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2160 fwin->lbutton_image->client_owned = 1;
2161 fwin->lbutton_image->client_owned_mask = 1;
2163 } else {
2164 if (fwin->lbutton_image && !fwin->lbutton_image->shared)
2165 wPixmapDestroy(fwin->lbutton_image);
2167 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2170 #ifdef XKB_BUTTON_HINT
2171 if (!WFLAGP(wwin, no_language_button)) {
2172 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared)
2173 wPixmapDestroy(fwin->languagebutton_image);
2175 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2177 #endif
2179 /* close button */
2181 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2182 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2184 if (!WFLAGP(wwin, no_close_button)) {
2185 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2186 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2188 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2189 mask = wwin->wm_gnustep_attr->close_mask;
2190 else
2191 mask = None;
2193 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2194 || fwin->rbutton_image->mask != mask)) {
2195 wPixmapDestroy(fwin->rbutton_image);
2196 fwin->rbutton_image = NULL;
2199 if (!fwin->rbutton_image) {
2200 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2201 fwin->rbutton_image->client_owned = 1;
2202 fwin->rbutton_image->client_owned_mask = 1;
2205 } else if (WFLAGP(wwin, kill_close)) {
2206 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2207 wPixmapDestroy(fwin->rbutton_image);
2209 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2211 } else if (MGFLAGP(wwin, broken_close)) {
2212 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2213 wPixmapDestroy(fwin->rbutton_image);
2215 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2217 } else {
2218 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2219 wPixmapDestroy(fwin->rbutton_image);
2221 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2225 /* force buttons to be redrawn */
2226 fwin->flags.need_texture_change = 1;
2227 wFrameWindowPaint(fwin);
2231 *---------------------------------------------------------------------------
2232 * wWindowConfigureBorders--
2233 * Update window border configuration according to attribute flags.
2235 *---------------------------------------------------------------------------
2237 void wWindowConfigureBorders(WWindow *wwin)
2239 if (wwin->frame) {
2240 int flags;
2241 int newx, newy, oldh;
2242 int border_width;
2243 XWindowAttributes attr;
2245 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2247 #ifdef XKB_BUTTON_HINT
2248 if (wPreferences.modelock)
2249 flags |= WFF_LANGUAGE_BUTTON;
2250 #endif
2252 if (HAS_TITLEBAR(wwin))
2253 flags |= WFF_TITLEBAR;
2254 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2255 flags |= WFF_RESIZEBAR;
2256 if (HAS_BORDER(wwin))
2257 flags |= WFF_BORDER;
2258 if (wwin->flags.shaded)
2259 flags |= WFF_IS_SHADED;
2261 if (!XGetWindowAttributes(dpy, wwin->frame->core->window, &attr))
2262 attr.border_width = 0;
2263 border_width = (flags & WFF_BORDER) ? FRAME_BORDER_WIDTH : 0;
2264 oldh = wwin->frame->top_width + attr.border_width;
2265 wFrameWindowUpdateBorders(wwin->frame, flags);
2266 if (oldh != wwin->frame->top_width + border_width) {
2267 newx = wwin->frame_x + attr.border_width - border_width;
2268 newy = wwin->frame_y + oldh - border_width - wwin->frame->top_width;
2270 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2271 wWindowConfigure(wwin, newx, newy, wwin->client.width, wwin->client.height);
2274 flags = 0;
2275 if (!WFLAGP(wwin, no_miniaturize_button)
2276 && wwin->frame->flags.hide_left_button)
2277 flags |= WFF_LEFT_BUTTON;
2279 #ifdef XKB_BUTTON_HINT
2280 if (!WFLAGP(wwin, no_language_button)
2281 && wwin->frame->flags.hide_language_button)
2282 flags |= WFF_LANGUAGE_BUTTON;
2283 #endif
2285 if (!WFLAGP(wwin, no_close_button)
2286 && wwin->frame->flags.hide_right_button)
2287 flags |= WFF_RIGHT_BUTTON;
2289 if (flags != 0) {
2290 wWindowUpdateButtonImages(wwin);
2291 wFrameWindowShowButton(wwin->frame, flags);
2294 flags = 0;
2295 if (WFLAGP(wwin, no_miniaturize_button)
2296 && !wwin->frame->flags.hide_left_button)
2297 flags |= WFF_LEFT_BUTTON;
2299 #ifdef XKB_BUTTON_HINT
2300 if (WFLAGP(wwin, no_language_button)
2301 && !wwin->frame->flags.hide_language_button)
2302 flags |= WFF_LANGUAGE_BUTTON;
2303 #endif
2305 if (WFLAGP(wwin, no_close_button)
2306 && !wwin->frame->flags.hide_right_button)
2307 flags |= WFF_RIGHT_BUTTON;
2309 if (flags != 0)
2310 wFrameWindowHideButton(wwin->frame, flags);
2312 #ifdef SHAPE
2313 if (wShapeSupported && wwin->flags.shaped)
2314 wWindowSetShape(wwin);
2315 #endif
2319 void wWindowSaveState(WWindow * wwin)
2321 long data[10];
2322 int i;
2324 memset(data, 0, sizeof(long) * 10);
2325 data[0] = wwin->frame->workspace;
2326 data[1] = wwin->flags.miniaturized;
2327 data[2] = wwin->flags.shaded;
2328 data[3] = wwin->flags.hidden;
2329 data[4] = wwin->flags.maximized;
2330 if (wwin->flags.maximized == 0) {
2331 data[5] = wwin->frame_x;
2332 data[6] = wwin->frame_y;
2333 data[7] = wwin->frame->core->width;
2334 data[8] = wwin->frame->core->height;
2335 } else {
2336 data[5] = wwin->old_geometry.x;
2337 data[6] = wwin->old_geometry.y;
2338 data[7] = wwin->old_geometry.width;
2339 data[8] = wwin->old_geometry.height;
2342 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2343 if (wwin->screen_ptr->shortcutWindows[i] &&
2344 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2345 data[9] |= 1 << i;
2347 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2348 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2351 static int getSavedState(Window window, WSavedState ** state)
2353 Atom type_ret;
2354 int fmt_ret;
2355 unsigned long nitems_ret;
2356 unsigned long bytes_after_ret;
2357 long *data;
2359 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2360 True, _XA_WINDOWMAKER_STATE,
2361 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2362 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2363 return 0;
2365 *state = wmalloc(sizeof(WSavedState));
2367 (*state)->workspace = data[0];
2368 (*state)->miniaturized = data[1];
2369 (*state)->shaded = data[2];
2370 (*state)->hidden = data[3];
2371 (*state)->maximized = data[4];
2372 (*state)->x = data[5];
2373 (*state)->y = data[6];
2374 (*state)->w = data[7];
2375 (*state)->h = data[8];
2376 (*state)->window_shortcuts = data[9];
2378 XFree(data);
2380 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2381 return 1;
2382 else
2383 return 0;
2386 #ifdef SHAPE
2387 void wWindowClearShape(WWindow * wwin)
2389 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2390 0, wwin->frame->top_width, None, ShapeSet);
2391 XFlush(dpy);
2394 void wWindowSetShape(WWindow * wwin)
2396 XRectangle rect[2];
2397 int count;
2398 #ifdef OPTIMIZE_SHAPE
2399 XRectangle *rects;
2400 XRectangle *urec;
2401 int ordering;
2403 /* only shape is the client's */
2404 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin))
2405 goto alt_code;
2407 /* Get array of rectangles describing the shape mask */
2408 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2409 if (!rects)
2410 goto alt_code;
2412 urec = malloc(sizeof(XRectangle) * (count + 2));
2413 if (!urec) {
2414 XFree(rects);
2415 goto alt_code;
2418 /* insert our decoration rectangles in the rect list */
2419 memcpy(urec, rects, sizeof(XRectangle) * count);
2420 XFree(rects);
2422 if (HAS_TITLEBAR(wwin)) {
2423 urec[count].x = -1;
2424 urec[count].y = -1 - wwin->frame->top_width;
2425 urec[count].width = wwin->frame->core->width + 2;
2426 urec[count].height = wwin->frame->top_width + 1;
2427 count++;
2429 if (HAS_RESIZEBAR(wwin)) {
2430 urec[count].x = -1;
2431 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2432 urec[count].width = wwin->frame->core->width + 2;
2433 urec[count].height = wwin->frame->bottom_width + 1;
2434 count++;
2437 /* shape our frame window */
2438 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2439 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2440 XFlush(dpy);
2441 wfree(urec);
2442 return;
2444 alt_code:
2445 #endif /* OPTIMIZE_SHAPE */
2446 count = 0;
2447 if (HAS_TITLEBAR(wwin)) {
2448 rect[count].x = -1;
2449 rect[count].y = -1;
2450 rect[count].width = wwin->frame->core->width + 2;
2451 rect[count].height = wwin->frame->top_width + 1;
2452 count++;
2454 if (HAS_RESIZEBAR(wwin)) {
2455 rect[count].x = -1;
2456 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2457 rect[count].width = wwin->frame->core->width + 2;
2458 rect[count].height = wwin->frame->bottom_width + 1;
2459 count++;
2461 if (count > 0) {
2462 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2463 0, 0, rect, count, ShapeSet, Unsorted);
2465 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2466 0, wwin->frame->top_width, wwin->client_win,
2467 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2468 XFlush(dpy);
2470 #endif /* SHAPE */
2472 /* ====================================================================== */
2474 static FocusMode getFocusMode(WWindow * wwin)
2476 FocusMode mode;
2478 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2479 if (wwin->wm_hints->input == True) {
2480 if (wwin->protocols.TAKE_FOCUS)
2481 mode = WFM_LOCALLY_ACTIVE;
2482 else
2483 mode = WFM_PASSIVE;
2484 } else {
2485 if (wwin->protocols.TAKE_FOCUS)
2486 mode = WFM_GLOBALLY_ACTIVE;
2487 else
2488 mode = WFM_NO_INPUT;
2490 } else {
2491 mode = WFM_PASSIVE;
2493 return mode;
2496 void wWindowSetKeyGrabs(WWindow * wwin)
2498 int i;
2499 WShortKey *key;
2501 for (i = 0; i < WKBD_LAST; i++) {
2502 key = &wKeyBindings[i];
2504 if (key->keycode == 0)
2505 continue;
2506 if (key->modifier != AnyModifier) {
2507 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2508 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2509 #ifdef NUMLOCK_HACK
2510 /* Also grab all modifier combinations possible that include,
2511 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2512 * work even if the NumLock/ScrollLock key is on.
2514 wHackedGrabKey(key->keycode, key->modifier,
2515 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2516 #endif
2518 XGrabKey(dpy, key->keycode, key->modifier,
2519 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2522 wRootMenuBindShortcuts(wwin->frame->core->window);
2525 void wWindowResetMouseGrabs(WWindow * wwin)
2527 /* Mouse grabs can't be done on the client window because of
2528 * ICCCM and because clients that try to do the same will crash.
2530 * But there is a problem wich makes tbar buttons of unfocused
2531 * windows not usable as the click goes to the frame window instead
2532 * of the button itself. Must figure a way to fix that.
2535 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2537 if (!WFLAGP(wwin, no_bind_mouse)) {
2538 /* grabs for Meta+drag */
2539 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2540 True, ButtonPressMask | ButtonReleaseMask,
2541 GrabModeSync, GrabModeAsync, None, None);
2543 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2544 * but we only grab it for Button4 and Button 5 since a lot of apps
2545 * use CTRL+Button1-3 for app related functionality
2547 if (wPreferences.resize_increment > 0) {
2548 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2549 True, ButtonPressMask | ButtonReleaseMask,
2550 GrabModeSync, GrabModeAsync, None, None);
2551 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2552 True, ButtonPressMask | ButtonReleaseMask,
2553 GrabModeSync, GrabModeAsync, None, None);
2555 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2556 True, ButtonPressMask | ButtonReleaseMask,
2557 GrabModeSync, GrabModeAsync, None, None);
2558 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2559 True, ButtonPressMask | ButtonReleaseMask,
2560 GrabModeSync, GrabModeAsync, None, None);
2564 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2565 && !wwin->flags.is_gnustep) {
2566 /* the passive grabs to focus the window */
2567 /* if (wPreferences.focus_mode == WKF_CLICK) */
2568 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2569 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2571 XFlush(dpy);
2574 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2576 if (attr->flags & GSExtraFlagsAttr) {
2577 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2578 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2579 wWindowUpdateButtonImages(wwin);
2584 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2586 WWindowState *wstate;
2588 wstate = malloc(sizeof(WWindowState));
2589 if (!wstate)
2590 return NULL;
2592 memset(wstate, 0, sizeof(WWindowState));
2593 wstate->pid = pid;
2594 if (instance)
2595 wstate->instance = wstrdup(instance);
2596 if (class)
2597 wstate->class = wstrdup(class);
2598 if (command)
2599 wstate->command = wstrdup(command);
2600 wstate->state = state;
2602 wstate->next = windowState;
2603 windowState = wstate;
2605 return wstate;
2608 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2610 WMagicNumber wWindowGetSavedState(Window win)
2612 char *instance, *class, *command = NULL;
2613 WWindowState *wstate = windowState;
2615 if (!wstate)
2616 return NULL;
2618 command = GetCommandForWindow(win);
2619 if (!command)
2620 return NULL;
2622 if (PropGetWMClass(win, &class, &instance)) {
2623 while (wstate) {
2624 if (SAME(instance, wstate->instance) &&
2625 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2626 break;
2628 wstate = wstate->next;
2630 } else {
2631 wstate = NULL;
2634 if (command)
2635 wfree(command);
2636 if (instance)
2637 free(instance);
2638 if (class)
2639 free(class);
2641 return wstate;
2644 void wWindowDeleteSavedState(WMagicNumber id)
2646 WWindowState *tmp, *wstate = (WWindowState *) id;
2648 if (!wstate || !windowState)
2649 return;
2651 tmp = windowState;
2652 if (tmp == wstate) {
2653 windowState = wstate->next;
2654 release_wwindowstate(wstate);
2655 } else {
2656 while (tmp->next) {
2657 if (tmp->next == wstate) {
2658 tmp->next = wstate->next;
2659 release_wwindowstate(wstate);
2660 break;
2662 tmp = tmp->next;
2667 void wWindowDeleteSavedStatesForPID(pid_t pid)
2669 WWindowState *tmp, *wstate;
2671 if (!windowState)
2672 return;
2674 tmp = windowState;
2675 if (tmp->pid == pid) {
2676 wstate = windowState;
2677 windowState = tmp->next;
2679 release_wwindowstate(wstate);
2680 } else {
2681 while (tmp->next) {
2682 if (tmp->next->pid == pid) {
2683 wstate = tmp->next;
2684 tmp->next = wstate->next;
2685 release_wwindowstate(wstate);
2686 break;
2688 tmp = tmp->next;
2693 static void release_wwindowstate(WWindowState *wstate)
2695 if (wstate->instance)
2696 wfree(wstate->instance);
2698 if (wstate->class)
2699 wfree(wstate->class);
2701 if (wstate->command)
2702 wfree(wstate->command);
2704 wfree(wstate->state);
2705 wfree(wstate);
2708 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2710 if (wwin->flags.omnipresent == flag)
2711 return;
2713 wwin->flags.omnipresent = flag;
2714 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2717 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2719 WWindow *wwin = data;
2721 #ifndef NUMLOCK_HACK
2722 if ((event->xbutton.state & ValidModMask)
2723 != (event->xbutton.state & ~LockMask)) {
2724 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2725 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2727 #endif
2729 event->xbutton.state &= ValidModMask;
2731 CloseWindowMenu(wwin->screen_ptr);
2733 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2734 && !WFLAGP(wwin, no_focusable)) {
2735 wSetFocusTo(wwin->screen_ptr, wwin);
2738 if (event->xbutton.button == Button1)
2739 wRaiseFrame(wwin->frame->core);
2741 if (event->xbutton.window != wwin->frame->resizebar->window) {
2742 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2743 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2744 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2745 return;
2749 if (event->xbutton.state & MOD_MASK) {
2750 /* move the window */
2751 wMouseMoveWindow(wwin, event);
2752 XUngrabPointer(dpy, CurrentTime);
2753 } else {
2754 wMouseResizeWindow(wwin, event);
2755 XUngrabPointer(dpy, CurrentTime);
2759 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2761 WWindow *wwin = data;
2763 event->xbutton.state &= ValidModMask;
2765 if (event->xbutton.button == Button1) {
2766 if (event->xbutton.state == 0) {
2767 if (!WFLAGP(wwin, no_shadeable)) {
2768 /* shade window */
2769 if (wwin->flags.shaded)
2770 wUnshadeWindow(wwin);
2771 else
2772 wShadeWindow(wwin);
2774 } else {
2775 int dir = 0;
2777 if (event->xbutton.state & ControlMask)
2778 dir |= MAX_VERTICAL;
2780 if (event->xbutton.state & ShiftMask) {
2781 dir |= MAX_HORIZONTAL;
2782 if (!(event->xbutton.state & ControlMask))
2783 wSelectWindow(wwin, !wwin->flags.selected);
2786 /* maximize window */
2787 if (dir != 0 && IS_RESIZABLE(wwin)) {
2788 int ndir = dir ^ wwin->flags.maximized;
2790 if (ndir != 0)
2791 wMaximizeWindow(wwin, ndir);
2792 else
2793 wUnmaximizeWindow(wwin);
2796 } else if (event->xbutton.button == Button3) {
2797 if (event->xbutton.state & MOD_MASK)
2798 wHideOtherApplications(wwin);
2799 } else if (event->xbutton.button == Button2) {
2800 wSelectWindow(wwin, !wwin->flags.selected);
2801 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2802 wShadeWindow(wwin);
2803 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2804 wUnshadeWindow(wwin);
2808 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2810 WWindow *wwin = desc->parent;
2811 unsigned int new_width, w_scale;
2812 unsigned int new_height, h_scale;
2813 unsigned int resize_width_increment = 0;
2814 unsigned int resize_height_increment = 0;
2816 if (wwin->normal_hints) {
2817 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2818 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2819 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2820 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2822 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2823 resize_width_increment = wPreferences.resize_increment;
2824 resize_height_increment = wPreferences.resize_increment;
2827 event->xbutton.state &= ValidModMask;
2829 CloseWindowMenu(wwin->screen_ptr);
2831 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2832 wSetFocusTo(wwin->screen_ptr, wwin);
2834 if (event->xbutton.button == Button1)
2835 wRaiseFrame(wwin->frame->core);
2837 if (event->xbutton.state & ControlMask) {
2838 if (event->xbutton.button == Button4) {
2839 new_width = wwin->client.width - resize_width_increment;
2840 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2841 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2843 if (event->xbutton.button == Button5) {
2844 new_width = wwin->client.width + resize_width_increment;
2845 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2846 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2850 if (event->xbutton.state & MOD_MASK) {
2851 /* move the window */
2852 if (XGrabPointer(dpy, wwin->client_win, False,
2853 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2854 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2855 return;
2857 if (event->xbutton.button == Button3) {
2858 wMouseResizeWindow(wwin, event);
2859 } else if (event->xbutton.button == Button4) {
2860 new_height = wwin->client.height - resize_height_increment;
2861 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2862 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2863 } else if (event->xbutton.button == Button5) {
2864 new_height = wwin->client.height + resize_height_increment;
2865 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2866 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2867 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2868 wMouseMoveWindow(wwin, event);
2870 XUngrabPointer(dpy, CurrentTime);
2874 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2876 WWindow *wwin = (WWindow *) data;
2878 #ifndef NUMLOCK_HACK
2879 if ((event->xbutton.state & ValidModMask) != (event->xbutton.state & ~LockMask))
2880 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2881 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2882 #endif
2883 event->xbutton.state &= ValidModMask;
2885 CloseWindowMenu(wwin->screen_ptr);
2887 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2888 && !WFLAGP(wwin, no_focusable))
2889 wSetFocusTo(wwin->screen_ptr, wwin);
2891 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2893 if (event->xbutton.button == Button1) {
2894 if (event->xbutton.state & MOD_MASK)
2895 wLowerFrame(wwin->frame->core);
2896 else
2897 wRaiseFrame(wwin->frame->core);
2899 if ((event->xbutton.state & ShiftMask)
2900 && !(event->xbutton.state & ControlMask)) {
2901 wSelectWindow(wwin, !wwin->flags.selected);
2902 return;
2904 if (event->xbutton.window != wwin->frame->titlebar->window
2905 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2906 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2907 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2908 return;
2911 /* move the window */
2912 wMouseMoveWindow(wwin, event);
2914 XUngrabPointer(dpy, CurrentTime);
2915 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2916 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2917 WObjDescriptor *desc;
2919 if (event->xbutton.window != wwin->frame->titlebar->window
2920 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2921 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2922 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2923 return;
2926 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2928 /* allow drag select */
2929 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2930 event->xany.send_event = True;
2931 (*desc->handle_mousedown) (desc, event);
2933 XUngrabPointer(dpy, CurrentTime);
2937 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2939 WWindow *wwin = data;
2941 event->xbutton.state &= ValidModMask;
2943 CloseWindowMenu(wwin->screen_ptr);
2945 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2946 return;
2948 /* if control-click, kill the client */
2949 if (event->xbutton.state & ControlMask) {
2950 wClientKill(wwin);
2951 } else {
2952 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2953 /* send delete message */
2954 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2959 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2961 WWindow *wwin = data;
2963 CloseWindowMenu(wwin->screen_ptr);
2965 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2966 return;
2968 /* send delete message */
2969 if (wwin->protocols.DELETE_WINDOW)
2970 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2971 else
2972 wClientKill(wwin);
2975 #ifdef XKB_BUTTON_HINT
2976 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2978 WWindow *wwin = data;
2979 WFrameWindow *fwin = wwin->frame;
2980 WScreen *scr = fwin->screen_ptr;
2981 int tl;
2983 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
2984 return;
2985 tl = wwin->frame->languagemode;
2986 wwin->frame->languagemode = wwin->frame->last_languagemode;
2987 wwin->frame->last_languagemode = tl;
2988 wSetFocusTo(scr, wwin);
2989 wwin->frame->languagebutton_image =
2990 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
2991 wFrameWindowUpdateLanguageButton(wwin->frame);
2992 if (event->xbutton.button == Button3)
2993 return;
2994 wRaiseFrame(fwin->core);
2996 #endif
2998 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3000 WWindow *wwin = data;
3002 event->xbutton.state &= ValidModMask;
3004 CloseWindowMenu(wwin->screen_ptr);
3006 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3007 return;
3009 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3010 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3011 } else {
3012 WApplication *wapp;
3013 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3015 wapp = wApplicationOf(wwin->main_window);
3016 if (wapp && !WFLAGP(wwin, no_appicon))
3017 wHideApplication(wapp);
3018 } else if (event->xbutton.state == 0) {
3019 wIconifyWindow(wwin);