Ignore shared_appicon if an appicon already exists
[wmaker-crm.git] / src / window.c
blob564e57b1303c6d6ceedce7764181dbc6792f09b3
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <math.h>
39 /* For getting mouse wheel mappings from WINGs */
40 #include <WINGs/WINGsP.h>
42 #include "WindowMaker.h"
43 #include "GNUstep.h"
44 #include "wcore.h"
45 #include "framewin.h"
46 #include "texture.h"
47 #include "window.h"
48 #include "winspector.h"
49 #include "icon.h"
50 #include "properties.h"
51 #include "actions.h"
52 #include "client.h"
53 #include "funcs.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
60 #ifdef MWM_HINTS
61 # include "motif.h"
62 #endif
63 #include "wmspec.h"
65 #define MOD_MASK wPreferences.modifier_mask
67 /****** Global Variables ******/
68 extern WShortKey wKeyBindings[WKBD_LAST];
70 #ifdef SHAPE
71 extern Bool wShapeSupported;
72 #endif
74 /* contexts */
75 extern XContext wWinContext;
77 /* protocol atoms */
78 extern Atom _XA_WM_DELETE_WINDOW;
79 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
80 extern Atom _XA_WINDOWMAKER_STATE;
81 extern WPreferences wPreferences;
82 extern Time LastTimestamp;
84 /* superfluous... */
85 extern void DoWindowBirth(WWindow *wwin);
87 /***** Local Stuff *****/
88 static WWindowState *windowState = NULL;
89 static FocusMode getFocusMode(WWindow *wwin);
90 static int getSavedState(Window window, WSavedState **state);
91 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
93 /* frame window (during window grabs) */
94 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
96 /* close button */
97 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
98 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
100 /* iconify button */
101 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
103 #ifdef XKB_BUTTON_HINT
104 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
105 #endif
107 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
108 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
109 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
111 /****** Notification Observers ******/
113 static void appearanceObserver(void *self, WMNotification * notif)
115 WWindow *wwin = (WWindow *) self;
116 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
118 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
119 return;
121 if (flags & WFontSettings) {
122 wWindowConfigureBorders(wwin);
123 if (wwin->flags.shaded) {
124 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
126 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
127 wWindowSynthConfigureNotify(wwin);
130 if (flags & WTextureSettings) {
131 wwin->frame->flags.need_texture_remake = 1;
133 if (flags & (WTextureSettings | WColorSettings)) {
134 if (wwin->frame->titlebar)
135 XClearWindow(dpy, wwin->frame->titlebar->window);
137 wFrameWindowPaint(wwin->frame);
142 WWindow *wWindowFor(Window window)
144 WObjDescriptor *desc;
146 if (window == None)
147 return NULL;
149 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
150 return NULL;
152 if (desc->parent_type == WCLASS_WINDOW)
153 return desc->parent;
154 else if (desc->parent_type == WCLASS_FRAME) {
155 WFrameWindow *frame = (WFrameWindow *) desc->parent;
156 if (frame->flags.is_client_window_frame)
157 return frame->child;
160 return NULL;
163 WWindow *wWindowCreate(void)
165 WWindow *wwin;
167 wwin = wmalloc(sizeof(WWindow));
168 wretain(wwin);
170 memset(wwin, 0, sizeof(WWindow));
172 wwin->client_descriptor.handle_mousedown = frameMouseDown;
173 wwin->client_descriptor.parent = wwin;
174 wwin->client_descriptor.self = wwin;
175 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
177 return wwin;
180 void wWindowDestroy(WWindow *wwin)
182 int i;
184 if (wwin->screen_ptr->cmap_window == wwin)
185 wwin->screen_ptr->cmap_window = NULL;
187 WMRemoveNotificationObserver(wwin);
189 wwin->flags.destroyed = 1;
191 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
192 if (!wwin->screen_ptr->shortcutWindows[i])
193 continue;
195 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
197 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
198 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
199 wwin->screen_ptr->shortcutWindows[i] = NULL;
203 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
204 wwin->fake_group->retainCount--;
205 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
206 XDestroyWindow(dpy, wwin->fake_group->leader);
207 wwin->fake_group->leader = None;
208 wwin->fake_group->origLeader = None;
209 XFlush(dpy);
213 if (wwin->normal_hints)
214 XFree(wwin->normal_hints);
216 if (wwin->wm_hints)
217 XFree(wwin->wm_hints);
219 if (wwin->wm_instance)
220 XFree(wwin->wm_instance);
222 if (wwin->wm_class)
223 XFree(wwin->wm_class);
225 if (wwin->wm_gnustep_attr)
226 wfree(wwin->wm_gnustep_attr);
228 if (wwin->cmap_windows)
229 XFree(wwin->cmap_windows);
231 XDeleteContext(dpy, wwin->client_win, wWinContext);
233 if (wwin->frame)
234 wFrameWindowDestroy(wwin->frame);
236 if (wwin->icon) {
237 RemoveFromStackList(wwin->icon->core);
238 wIconDestroy(wwin->icon);
239 if (wPreferences.auto_arrange_icons)
240 wArrangeIcons(wwin->screen_ptr, True);
242 if (wwin->net_icon_image)
243 RReleaseImage(wwin->net_icon_image);
245 wrelease(wwin);
248 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
250 if (gs_hints->flags & GSWindowStyleAttr) {
251 if (gs_hints->window_style == WMBorderlessWindowMask) {
252 wwin->client_flags.no_border = 1;
253 wwin->client_flags.no_titlebar = 1;
254 wwin->client_flags.no_closable = 1;
255 wwin->client_flags.no_miniaturizable = 1;
256 wwin->client_flags.no_resizable = 1;
257 wwin->client_flags.no_close_button = 1;
258 wwin->client_flags.no_miniaturize_button = 1;
259 wwin->client_flags.no_resizebar = 1;
260 } else {
261 wwin->client_flags.no_close_button =
262 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
264 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
266 wwin->client_flags.no_miniaturize_button =
267 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
269 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
271 wwin->client_flags.no_resizebar =
272 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
274 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
276 /* these attributes supposedly imply in the existence
277 * of a titlebar */
278 if (gs_hints->window_style & (WMResizableWindowMask |
279 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
280 wwin->client_flags.no_titlebar = 0;
281 } else {
282 wwin->client_flags.no_titlebar =
283 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
287 } else {
288 /* setup the defaults */
289 wwin->client_flags.no_border = 0;
290 wwin->client_flags.no_titlebar = 0;
291 wwin->client_flags.no_closable = 0;
292 wwin->client_flags.no_miniaturizable = 0;
293 wwin->client_flags.no_resizable = 0;
294 wwin->client_flags.no_close_button = 0;
295 wwin->client_flags.no_miniaturize_button = 0;
296 wwin->client_flags.no_resizebar = 0;
298 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
299 wwin->client_flags.no_appicon = 1;
302 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
304 WScreen *scr = wwin->screen_ptr;
306 /* sets global default stuff */
307 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
309 * Decoration setting is done in this precedence (lower to higher)
310 * - use global default in the resource database
311 * - guess some settings
312 * - use GNUstep/external window attributes
313 * - set hints specified for the app in the resource DB
316 WSETUFLAG(wwin, broken_close, 0);
318 if (wwin->protocols.DELETE_WINDOW)
319 WSETUFLAG(wwin, kill_close, 0);
320 else
321 WSETUFLAG(wwin, kill_close, 1);
323 /* transients can't be iconified or maximized */
324 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
325 WSETUFLAG(wwin, no_miniaturizable, 1);
326 WSETUFLAG(wwin, no_miniaturize_button, 1);
329 /* if the window can't be resized, remove the resizebar */
330 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
331 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
332 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
333 WSETUFLAG(wwin, no_resizable, 1);
334 WSETUFLAG(wwin, no_resizebar, 1);
337 /* set GNUstep window attributes */
338 if (wwin->wm_gnustep_attr) {
339 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
341 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
343 *level = wwin->wm_gnustep_attr->window_level;
345 * INT_MIN is the only illegal window level.
347 if (*level == INT_MIN)
348 *level = INT_MIN + 1;
349 } else {
350 /* setup defaults */
351 *level = WMNormalLevel;
353 } else {
354 int tmp_workspace = -1;
355 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
356 Bool check;
358 check = False;
360 #ifdef MWM_HINTS
361 wMWMCheckClientHints(wwin);
362 #endif /* MWM_HINTS */
364 if (!check)
365 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
367 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
368 * have INT_MIN that means that no window level was requested. -Dan
370 if (tmp_level == INT_MIN) {
371 if (WFLAGP(wwin, floating))
372 *level = WMFloatingLevel;
373 else if (WFLAGP(wwin, sunken))
374 *level = WMSunkenLevel;
375 else
376 *level = WMNormalLevel;
377 } else {
378 *level = tmp_level;
381 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
382 WWindow *transientOwner = wWindowFor(wwin->transient_for);
383 if (transientOwner) {
384 int ownerLevel = transientOwner->frame->core->stacking->window_level;
385 if (ownerLevel > *level)
386 *level = ownerLevel;
390 if (tmp_workspace >= 0)
391 *workspace = tmp_workspace % scr->workspace_count;
395 * Set attributes specified only for that window/class.
396 * This might do duplicate work with the 1st wDefaultFillAttributes().
398 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
399 &wwin->user_flags, &wwin->defined_user_flags, False);
401 * Sanity checks for attributes that depend on other attributes
403 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
404 wwin->user_flags.emulate_appicon = 0;
406 if (wwin->main_window != None) {
407 WApplication *wapp = wApplicationOf(wwin->main_window);
408 if (wapp && !wapp->flags.emulated)
409 wwin->user_flags.emulate_appicon = 0;
412 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
413 wwin->user_flags.emulate_appicon = 0;
415 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
416 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
417 wwin->user_flags.sunken = 0;
419 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
421 /* windows that have takefocus=False shouldn't take focus at all */
422 if (wwin->focus_mode == WFM_NO_INPUT)
423 wwin->client_flags.no_focusable = 1;
426 Bool wWindowCanReceiveFocus(WWindow *wwin)
428 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
429 return False;
430 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
431 return False;
432 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
433 return False;
435 return True;
438 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
440 int w1, h1, w2, h2;
442 w1 = wwin->frame->core->width;
443 h1 = wwin->frame->core->height;
444 w2 = obscured->frame->core->width;
445 h2 = obscured->frame->core->height;
447 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
448 && wwin->frame->workspace != obscured->frame->workspace)
449 return False;
451 if (wwin->frame_x + w1 < obscured->frame_x
452 || wwin->frame_y + h1 < obscured->frame_y
453 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
454 return False;
456 return True;
459 static void fixLeaderProperties(WWindow *wwin)
461 XClassHint *classHint;
462 XWMHints *hints, *clientHints;
463 Window leaders[2], window;
464 char **argv, *command;
465 int argc, i, pid;
466 Bool haveCommand;
468 classHint = XAllocClassHint();
469 clientHints = XGetWMHints(dpy, wwin->client_win);
470 pid = wNETWMGetPidForWindow(wwin->client_win);
471 if (pid > 0)
472 haveCommand = GetCommandForPid(pid, &argv, &argc);
473 else
474 haveCommand = False;
476 leaders[0] = wwin->client_leader;
477 leaders[1] = wwin->group_id;
479 if (haveCommand) {
480 command = GetCommandForWindow(wwin->client_win);
481 if (command) {
482 /* command already set. nothing to do. */
483 wfree(command);
484 } else {
485 XSetCommand(dpy, wwin->client_win, argv, argc);
489 for (i = 0; i < 2; i++) {
490 window = leaders[i];
491 if (window) {
492 if (XGetClassHint(dpy, window, classHint) == 0) {
493 classHint->res_name = wwin->wm_instance;
494 classHint->res_class = wwin->wm_class;
495 XSetClassHint(dpy, window, classHint);
497 hints = XGetWMHints(dpy, window);
498 if (hints) {
499 XFree(hints);
500 } else if (clientHints) {
501 /* set window group leader to self */
502 clientHints->window_group = window;
503 clientHints->flags |= WindowGroupHint;
504 XSetWMHints(dpy, window, clientHints);
507 if (haveCommand) {
508 command = GetCommandForWindow(window);
509 if (command) {
510 /* command already set. nothing to do. */
511 wfree(command);
512 } else {
513 XSetCommand(dpy, window, argv, argc);
519 XFree(classHint);
520 if (clientHints)
521 XFree(clientHints);
522 if (haveCommand)
523 wfree(argv);
526 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
528 XClassHint *classHint;
529 XWMHints *hints;
530 Window leader;
531 int argc;
532 char **argv;
534 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
535 /* set class hint */
536 classHint = XAllocClassHint();
537 classHint->res_name = instance;
538 classHint->res_class = class;
539 XSetClassHint(dpy, leader, classHint);
540 XFree(classHint);
542 /* inherit these from the original leader if available */
543 hints = XGetWMHints(dpy, win);
544 if (!hints) {
545 hints = XAllocWMHints();
546 hints->flags = 0;
548 /* set window group leader to self */
549 hints->window_group = leader;
550 hints->flags |= WindowGroupHint;
551 XSetWMHints(dpy, leader, hints);
552 XFree(hints);
554 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
555 XSetCommand(dpy, leader, argv, argc);
556 XFreeStringList(argv);
559 return leader;
562 static int matchIdentifier(const void *item, const void *cdata)
564 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
568 *----------------------------------------------------------------
569 * wManageWindow--
570 * reparents the window and allocates a descriptor for it.
571 * Window manager hints and other hints are fetched to configure
572 * the window decoration attributes and others. User preferences
573 * for the window are used if available, to configure window
574 * decorations and some behaviour.
575 * If in startup, windows that are override redirect,
576 * unmapped and never were managed and are Withdrawn are not
577 * managed.
579 * Returns:
580 * the new window descriptor
582 * Side effects:
583 * The window is reparented and appropriate notification
584 * is done to the client. Input mask for the window is setup.
585 * The window descriptor is also associated with various window
586 * contexts and inserted in the head of the window list.
587 * Event handler contexts are associated for some objects
588 * (buttons, titlebar and resizebar)
590 *----------------------------------------------------------------
592 WWindow *wManageWindow(WScreen *scr, Window window)
594 WWindow *wwin;
595 int x, y;
596 unsigned width, height;
597 XWindowAttributes wattribs;
598 XSetWindowAttributes attribs;
599 WWindowState *win_state;
600 WWindow *transientOwner = NULL;
601 int window_level;
602 int wm_state;
603 int foo;
604 int workspace = -1;
605 char *title;
606 Bool withdraw = False;
607 Bool raise = False;
609 /* mutex. */
610 XGrabServer(dpy);
611 XSync(dpy, False);
612 /* make sure the window is still there */
613 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
614 XUngrabServer(dpy);
615 return NULL;
618 /* if it's an override-redirect, ignore it */
619 if (wattribs.override_redirect) {
620 XUngrabServer(dpy);
621 return NULL;
624 wm_state = PropGetWindowState(window);
626 /* if it's startup and the window is unmapped, don't manage it */
627 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
628 XUngrabServer(dpy);
629 return NULL;
632 wwin = wWindowCreate();
634 title = wNETWMGetWindowName(window);
635 if (title)
636 wwin->flags.net_has_title = 1;
637 if (!title && !wFetchName(dpy, window, &title))
638 title = NULL;
640 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
642 #ifdef SHAPE
643 if (wShapeSupported) {
644 int junk;
645 unsigned int ujunk;
646 int b_shaped;
648 XShapeSelectInput(dpy, window, ShapeNotifyMask);
649 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
650 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
651 wwin->flags.shaped = b_shaped;
653 #endif
656 * Get hints and other information in properties
658 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
660 /* setup descriptor */
661 wwin->client_win = window;
662 wwin->screen_ptr = scr;
663 wwin->old_border_width = wattribs.border_width;
664 wwin->event_mask = CLIENT_EVENTS;
665 attribs.event_mask = CLIENT_EVENTS;
666 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
667 attribs.save_under = False;
669 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
670 XSetWindowBorderWidth(dpy, window, 0);
672 /* get hints from GNUstep app */
673 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "GNUstep") == 0) {
674 wwin->flags.is_gnustep = 1;
676 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
677 wwin->wm_gnustep_attr = NULL;
680 if (wwin->wm_class != NULL && strcmp(wwin->wm_class, "DockApp") == 0) {
681 wwin->flags.is_dockapp = 1;
682 withdraw = True;
685 wwin->client_leader = PropGetClientLeader(window);
686 if (wwin->client_leader != None)
687 wwin->main_window = wwin->client_leader;
689 wwin->wm_hints = XGetWMHints(dpy, window);
691 if (wwin->wm_hints) {
692 if (wwin->wm_hints->flags & StateHint) {
694 if (wwin->wm_hints->initial_state == IconicState) {
696 wwin->flags.miniaturized = 1;
698 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
700 wwin->flags.is_dockapp = 1;
701 withdraw = True;
705 if (wwin->wm_hints->flags & WindowGroupHint) {
706 wwin->group_id = wwin->wm_hints->window_group;
707 /* window_group has priority over CLIENT_LEADER */
708 wwin->main_window = wwin->group_id;
709 } else {
710 wwin->group_id = None;
713 if (wwin->wm_hints->flags & UrgencyHint) {
714 wwin->flags.urgent = 1;
715 wAppBounceWhileUrgent(wApplicationOf(wwin->main_window));
717 } else {
718 wwin->group_id = None;
721 PropGetProtocols(window, &wwin->protocols);
723 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
724 wwin->transient_for = None;
725 } else {
726 if (wwin->transient_for == None || wwin->transient_for == window) {
727 wwin->transient_for = scr->root_win;
728 } else {
729 transientOwner = wWindowFor(wwin->transient_for);
730 if (transientOwner && transientOwner->main_window != None)
731 wwin->main_window = transientOwner->main_window;
735 /* guess the focus mode */
736 wwin->focus_mode = getFocusMode(wwin);
738 /* get geometry stuff */
739 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
741 /* get colormap windows */
742 GetColormapWindows(wwin);
745 * Setup the decoration/window attributes and
746 * geometry
748 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
750 /* Make broken apps behave as a nice app. */
751 if (WFLAGP(wwin, emulate_appicon))
752 wwin->main_window = wwin->client_win;
754 fixLeaderProperties(wwin);
756 wwin->orig_main_window = wwin->main_window;
758 if (wwin->flags.is_gnustep)
759 WSETUFLAG(wwin, shared_appicon, 0);
761 if (wwin->main_window) {
762 extern Atom _XA_WINDOWMAKER_MENU;
763 XTextProperty text_prop;
765 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
766 WSETUFLAG(wwin, shared_appicon, 0);
770 if (wwin->flags.is_dockapp)
771 WSETUFLAG(wwin, shared_appicon, 0);
773 if (wwin->main_window) {
774 WApplication *app = wApplicationOf(wwin->main_window);
775 if (app && app->app_icon)
776 WSETUFLAG(wwin, shared_appicon, 0);
779 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
780 char *buffer, *instance, *class;
781 WFakeGroupLeader *fPtr;
782 int index;
784 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
786 /* // only enter here if PropGetWMClass() succeds */
787 PropGetWMClass(wwin->main_window, &class, &instance);
788 buffer = StrConcatDot(instance, class);
790 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
791 if (index != WANotFound) {
792 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
793 if (fPtr->retainCount == 0) {
794 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
795 instance, class);
797 fPtr->retainCount++;
798 if (fPtr->origLeader == None) {
799 if (ADEQUATE(wwin->main_window)) {
800 fPtr->retainCount++;
801 fPtr->origLeader = wwin->main_window;
804 wwin->fake_group = fPtr;
805 /*wwin->group_id = fPtr->leader; */
806 wwin->main_window = fPtr->leader;
807 wfree(buffer);
808 } else {
809 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
811 fPtr->identifier = buffer;
812 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
813 fPtr->origLeader = None;
814 fPtr->retainCount = 1;
816 WMAddToArray(scr->fakeGroupLeaders, fPtr);
818 if (ADEQUATE(wwin->main_window)) {
819 fPtr->retainCount++;
820 fPtr->origLeader = wwin->main_window;
822 wwin->fake_group = fPtr;
823 /*wwin->group_id = fPtr->leader; */
824 wwin->main_window = fPtr->leader;
826 if (instance)
827 XFree(instance);
828 if (class)
829 XFree(class);
830 #undef ADEQUATE
834 * Setup the initial state of the window
836 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
837 wwin->flags.miniaturized = 1;
839 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
840 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
842 wNETWMCheckInitialClientState(wwin);
844 /* apply previous state if it exists and we're in startup */
845 if (scr->flags.startup && wm_state >= 0) {
847 if (wm_state == IconicState) {
849 wwin->flags.miniaturized = 1;
851 } else if (wm_state == WithdrawnState) {
853 withdraw = True;
857 /* if there is a saved state (from file), restore it */
858 win_state = NULL;
859 if (wwin->main_window != None)
860 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
861 else
862 win_state = (WWindowState *) wWindowGetSavedState(window);
864 if (win_state && !withdraw) {
865 if (win_state->state->hidden > 0)
866 wwin->flags.hidden = win_state->state->hidden;
868 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
869 wwin->flags.shaded = win_state->state->shaded;
871 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
872 wwin->flags.miniaturized = win_state->state->miniaturized;
875 if (!IS_OMNIPRESENT(wwin)) {
876 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
877 wwin->wm_class);
878 if (w < 0 || w >= scr->workspace_count) {
879 workspace = win_state->state->workspace;
880 if (workspace >= scr->workspace_count)
881 workspace = scr->current_workspace;
882 } else {
883 workspace = w;
885 } else {
886 workspace = scr->current_workspace;
890 /* if we're restarting, restore saved state (from hints).
891 * This will overwrite previous */
893 WSavedState *wstate;
895 if (getSavedState(window, &wstate)) {
896 wwin->flags.shaded = wstate->shaded;
897 wwin->flags.hidden = wstate->hidden;
898 wwin->flags.miniaturized = wstate->miniaturized;
899 wwin->flags.maximized = wstate->maximized;
900 if (wwin->flags.maximized) {
901 wwin->old_geometry.x = wstate->x;
902 wwin->old_geometry.y = wstate->y;
903 wwin->old_geometry.width = wstate->w;
904 wwin->old_geometry.height = wstate->h;
907 workspace = wstate->workspace;
908 } else {
909 wstate = NULL;
912 /* restore window shortcut */
913 if (wstate != NULL || win_state != NULL) {
914 unsigned mask = 0;
916 if (win_state != NULL)
917 mask = win_state->state->window_shortcuts;
919 if (wstate != NULL && mask == 0)
920 mask = wstate->window_shortcuts;
922 if (mask > 0) {
923 int i;
925 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
926 if (mask & (1 << i)) {
927 if (!scr->shortcutWindows[i])
928 scr->shortcutWindows[i] = WMCreateArray(4);
930 WMAddToArray(scr->shortcutWindows[i], wwin);
935 if (wstate != NULL)
936 wfree(wstate);
939 /* don't let transients start miniaturized if their owners are not */
940 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
941 wwin->flags.miniaturized = 0;
942 if (wwin->wm_hints)
943 wwin->wm_hints->initial_state = NormalState;
946 /* set workspace on which the window starts */
947 if (workspace >= 0) {
948 if (workspace > scr->workspace_count - 1) {
949 workspace = workspace % scr->workspace_count;
951 } else {
952 int w;
954 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
956 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
958 workspace = w;
960 } else {
961 if (wPreferences.open_transients_with_parent && transientOwner) {
963 workspace = transientOwner->frame->workspace;
965 } else {
967 workspace = scr->current_workspace;
972 /* setup window geometry */
973 if (win_state && win_state->state->w > 0) {
974 width = win_state->state->w;
975 height = win_state->state->h;
977 wWindowConstrainSize(wwin, &width, &height);
979 /* do not ask for window placement if the window is
980 * transient, during startup, if the initial workspace is another one
981 * or if the window wants to start iconic.
982 * If geometry was saved, restore it. */
984 Bool dontBring = False;
986 if (win_state && win_state->state->w > 0) {
987 x = win_state->state->x;
988 y = win_state->state->y;
989 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
990 && !scr->flags.startup
991 && workspace == scr->current_workspace
992 && !wwin->flags.miniaturized
993 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
995 if (transientOwner && transientOwner->flags.mapped) {
996 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
997 WMRect rect;
998 int head;
1000 x = transientOwner->frame_x +
1001 abs((transientOwner->frame->core->width - width) / 2) + offs;
1002 y = transientOwner->frame_y +
1003 abs((transientOwner->frame->core->height - height) / 3) + offs;
1006 * limit transient windows to be inside their parent's head
1008 rect.pos.x = transientOwner->frame_x;
1009 rect.pos.y = transientOwner->frame_y;
1010 rect.size.width = transientOwner->frame->core->width;
1011 rect.size.height = transientOwner->frame->core->height;
1013 head = wGetHeadForRect(scr, rect);
1014 rect = wGetRectForHead(scr, head);
1016 if (x < rect.pos.x)
1017 x = rect.pos.x;
1018 else if (x + width > rect.pos.x + rect.size.width)
1019 x = rect.pos.x + rect.size.width - width;
1021 if (y < rect.pos.y)
1022 y = rect.pos.y;
1023 else if (y + height > rect.pos.y + rect.size.height)
1024 y = rect.pos.y + rect.size.height - height;
1026 } else {
1027 PlaceWindow(wwin, &x, &y, width, height);
1029 if (wPreferences.window_placement == WPM_MANUAL) {
1030 dontBring = True;
1032 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1033 int head, flags;
1034 WMRect rect;
1035 int reposition = 0;
1038 * Make spash screens come out in the center of a head
1039 * trouble is that most splashies never get here
1040 * they are managed trough atoms but god knows where.
1041 * Dan, do you know ? -peter
1043 * Most of them are not managed, they have set
1044 * OverrideRedirect, which means we can't do anything about
1045 * them. -alfredo
1049 * xinerama checks for: across head and dead space
1051 rect.pos.x = x;
1052 rect.pos.y = y;
1053 rect.size.width = width;
1054 rect.size.height = height;
1056 head = wGetRectPlacementInfo(scr, rect, &flags);
1058 if (flags & XFLAG_DEAD)
1059 reposition = 1;
1061 if (flags & XFLAG_MULTIPLE)
1062 reposition = 2;
1065 switch (reposition) {
1066 case 1:
1067 head = wGetHeadForPointerLocation(scr);
1068 rect = wGetRectForHead(scr, head);
1070 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1071 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1072 break;
1074 case 2:
1075 rect = wGetRectForHead(scr, head);
1077 if (x < rect.pos.x)
1078 x = rect.pos.x;
1079 else if (x + width > rect.pos.x + rect.size.width)
1080 x = rect.pos.x + rect.size.width - width;
1082 if (y < rect.pos.y)
1083 y = rect.pos.y;
1084 else if (y + height > rect.pos.y + rect.size.height)
1085 y = rect.pos.y + rect.size.height - height;
1087 break;
1089 default:
1090 break;
1094 if (WFLAGP(wwin, dont_move_off) && dontBring)
1095 wScreenBringInside(scr, &x, &y, width, height);
1098 wNETWMPositionSplash(wwin, &x, &y, width, height);
1100 if (wwin->flags.urgent) {
1101 if (!IS_OMNIPRESENT(wwin))
1102 wwin->flags.omnipresent ^= 1;
1106 * Create frame, borders and do reparenting
1108 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1109 #ifdef XKB_BUTTON_HINT
1110 if (wPreferences.modelock)
1111 foo |= WFF_LANGUAGE_BUTTON;
1112 #endif
1113 if (HAS_TITLEBAR(wwin))
1114 foo |= WFF_TITLEBAR;
1115 if (HAS_RESIZEBAR(wwin))
1116 foo |= WFF_RESIZEBAR;
1117 if (HAS_BORDER(wwin))
1118 foo |= WFF_BORDER;
1120 wwin->frame = wFrameWindowCreate(scr, window_level,
1121 x, y, width, height,
1122 &wPreferences.window_title_clearance, foo,
1123 scr->window_title_texture,
1124 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1126 wwin->frame->flags.is_client_window_frame = 1;
1127 wwin->frame->flags.justification = wPreferences.title_justification;
1129 /* setup button images */
1130 wWindowUpdateButtonImages(wwin);
1132 /* hide unused buttons */
1133 foo = 0;
1134 if (WFLAGP(wwin, no_close_button))
1135 foo |= WFF_RIGHT_BUTTON;
1136 if (WFLAGP(wwin, no_miniaturize_button))
1137 foo |= WFF_LEFT_BUTTON;
1138 #ifdef XKB_BUTTON_HINT
1139 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1140 foo |= WFF_LANGUAGE_BUTTON;
1141 #endif
1142 if (foo != 0)
1143 wFrameWindowHideButton(wwin->frame, foo);
1145 wwin->frame->child = wwin;
1146 wwin->frame->workspace = workspace;
1147 wwin->frame->on_click_left = windowIconifyClick;
1149 #ifdef XKB_BUTTON_HINT
1150 if (wPreferences.modelock)
1151 wwin->frame->on_click_language = windowLanguageClick;
1152 #endif
1154 wwin->frame->on_click_right = windowCloseClick;
1155 wwin->frame->on_dblclick_right = windowCloseDblClick;
1156 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1157 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1158 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1160 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1161 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1162 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1165 int gx, gy;
1167 wClientGetGravityOffsets(wwin, &gx, &gy);
1169 /* if gravity is to the south, account for the border sizes */
1170 if (gy > 0)
1171 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1175 * wWindowConfigure() will init the client window's size
1176 * (wwin->client.{width,height}) and all other geometry
1177 * related variables (frame_x,frame_y)
1179 wWindowConfigure(wwin, x, y, width, height);
1181 /* to make sure the window receives it's new position after reparenting */
1182 wWindowSynthConfigureNotify(wwin);
1185 * Setup descriptors and save window to internal
1186 * lists
1188 if (wwin->main_window != None) {
1189 WApplication *app;
1190 WWindow *leader;
1192 /* Leader windows do not necessary set themselves as leaders.
1193 * If this is the case, point the leader of this window to
1194 * itself */
1195 leader = wWindowFor(wwin->main_window);
1196 if (leader && leader->main_window == None) {
1197 leader->main_window = leader->client_win;
1199 app = wApplicationCreate(wwin);
1200 if (app) {
1201 app->last_workspace = workspace;
1204 * Do application specific stuff, like setting application
1205 * wide attributes.
1208 if (wwin->flags.hidden) {
1209 /* if the window was set to hidden because it was hidden
1210 * in a previous incarnation and that state was restored */
1211 app->flags.hidden = 1;
1212 } else if (app->flags.hidden) {
1213 if (WFLAGP(app->main_window_desc, start_hidden)) {
1214 wwin->flags.hidden = 1;
1215 } else {
1216 wUnhideApplication(app, False, False);
1217 raise = True;
1220 wAppBounce(app);
1224 /* setup the frame descriptor */
1225 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1226 wwin->frame->core->descriptor.parent = wwin;
1227 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1229 /* don't let windows go away if we die */
1230 XAddToSaveSet(dpy, window);
1232 XLowerWindow(dpy, window);
1234 /* if window is in this workspace and should be mapped, then map it */
1235 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1236 && !wwin->flags.hidden && !withdraw) {
1238 /* The following "if" is to avoid crashing of clients that expect
1239 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1240 * after the return from this function.
1242 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1243 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1244 } else {
1245 wClientSetState(wwin, NormalState, None);
1248 #if 0
1249 /* if not auto focus, then map the window under the currently
1250 * focused window */
1251 #define _WIDTH(w) (w)->frame->core->width
1252 #define _HEIGHT(w) (w)->frame->core->height
1253 if (!wPreferences.auto_focus && scr->focused_window
1254 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1255 && (_WIDTH(wwin) >
1256 (_WIDTH(scr->focused_window) * 5) / 3
1257 || _HEIGHT(wwin) >
1258 (_HEIGHT(scr->focused_window) * 5) / 3)
1259 && WINDOW_LEVEL(scr->focused_window) ==
1260 WINDOW_LEVEL(wwin))
1261 || wwin->flags.maximized)) {
1262 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1264 #undef _WIDTH
1265 #undef _HEIGHT
1267 #endif
1269 if (wPreferences.superfluous && !wPreferences.no_animations
1270 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1272 * The brain damaged idiotic non-click to focus modes will
1273 * have trouble with this because:
1275 * 1. window is created and mapped by the client
1276 * 2. window is mapped by wmaker in small size
1277 * 3. window is animated to grow to normal size
1278 * 4. this function returns to normal event loop
1279 * 5. eventually, the EnterNotify event that would trigger
1280 * the window focusing (if the mouse is over that window)
1281 * will be processed by wmaker.
1282 * But since this event will be rather delayed
1283 * (step 3 has a large delay) the time when the event ocurred
1284 * and when it is processed, the client that owns that window
1285 * will reject the XSetInputFocus() for it.
1287 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1288 DoWindowBirth(wwin);
1291 wWindowMap(wwin);
1294 /* setup stacking descriptor */
1295 if (transientOwner) {
1296 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1297 } else {
1298 wwin->frame->core->stacking->child_of = NULL;
1301 if (!scr->focused_window) {
1302 /* first window on the list */
1303 wwin->next = NULL;
1304 wwin->prev = NULL;
1305 scr->focused_window = wwin;
1306 } else {
1307 WWindow *tmp;
1309 /* add window at beginning of focus window list */
1310 tmp = scr->focused_window;
1311 while (tmp->prev)
1312 tmp = tmp->prev;
1313 tmp->prev = wwin;
1314 wwin->next = tmp;
1315 wwin->prev = NULL;
1318 /* raise is set to true if we un-hid the app when this window was born.
1319 * we raise, else old windows of this app will be above this new one. */
1320 if (raise) {
1321 wRaiseFrame(wwin->frame->core);
1324 /* Update name must come after WApplication stuff is done */
1325 wWindowUpdateName(wwin, title);
1326 if (title)
1327 XFree(title);
1329 XUngrabServer(dpy);
1332 * Final preparations before window is ready to go
1334 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1336 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1337 if (((transientOwner && transientOwner->flags.focused)
1338 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1340 /* only auto_focus if on same screen as mouse
1341 * (and same head for xinerama mode)
1342 * TODO: make it an option */
1344 /*TODO add checking the head of the window, is it available? */
1345 short same_screen = 0, same_head = 1;
1347 int foo;
1348 unsigned int bar;
1349 Window dummy;
1351 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1352 &foo, &foo, &foo, &foo, &bar) != False) {
1353 same_screen = 1;
1356 if (same_screen == 1 && same_head == 1) {
1357 wSetFocusTo(scr, wwin);
1361 wWindowResetMouseGrabs(wwin);
1363 if (!WFLAGP(wwin, no_bind_keys))
1364 wWindowSetKeyGrabs(wwin);
1366 WMPostNotificationName(WMNManaged, wwin, NULL);
1367 wColormapInstallForWindow(scr, scr->cmap_window);
1369 /* Setup Notification Observers */
1370 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1372 /* Cleanup temporary stuff */
1373 if (win_state)
1374 wWindowDeleteSavedState(win_state);
1376 /* If the window must be withdrawed, then do it now.
1377 * Must do some optimization, 'though */
1378 if (withdraw) {
1379 wwin->flags.mapped = 0;
1380 wClientSetState(wwin, WithdrawnState, None);
1381 wUnmanageWindow(wwin, True, False);
1382 wwin = NULL;
1385 return wwin;
1388 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1389 char *title, int x, int y, int width, int height)
1391 WWindow *wwin;
1392 int foo;
1394 wwin = wWindowCreate();
1396 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1398 wwin->flags.internal_window = 1;
1400 WSETUFLAG(wwin, omnipresent, 1);
1401 WSETUFLAG(wwin, no_shadeable, 1);
1402 WSETUFLAG(wwin, no_resizable, 1);
1403 WSETUFLAG(wwin, no_miniaturizable, 1);
1405 wwin->focus_mode = WFM_PASSIVE;
1406 wwin->client_win = window;
1407 wwin->screen_ptr = scr;
1408 wwin->transient_for = owner;
1409 wwin->client.x = x;
1410 wwin->client.y = y;
1411 wwin->client.width = width;
1412 wwin->client.height = height;
1413 wwin->frame_x = wwin->client.x;
1414 wwin->frame_y = wwin->client.y;
1416 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1417 foo |= WFF_TITLEBAR;
1418 #ifdef XKB_BUTTON_HINT
1419 foo |= WFF_LANGUAGE_BUTTON;
1420 #endif
1422 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1423 wwin->frame_x, wwin->frame_y,
1424 width, height,
1425 &wPreferences.window_title_clearance, foo,
1426 scr->window_title_texture,
1427 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1429 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1431 wwin->frame->flags.is_client_window_frame = 1;
1432 wwin->frame->flags.justification = wPreferences.title_justification;
1434 wFrameWindowChangeTitle(wwin->frame, title);
1436 /* setup button images */
1437 wWindowUpdateButtonImages(wwin);
1439 /* hide buttons */
1440 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1442 wwin->frame->child = wwin;
1443 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1445 #ifdef XKB_BUTTON_HINT
1446 if (wPreferences.modelock)
1447 wwin->frame->on_click_language = windowLanguageClick;
1448 #endif
1450 wwin->frame->on_click_right = windowCloseClick;
1451 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1452 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1453 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1454 wwin->client.y += wwin->frame->top_width;
1456 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1457 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1459 /* setup the frame descriptor */
1460 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1461 wwin->frame->core->descriptor.parent = wwin;
1462 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1464 XLowerWindow(dpy, window);
1465 XMapSubwindows(dpy, wwin->frame->core->window);
1467 /* setup stacking descriptor */
1468 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1469 WWindow *tmp;
1470 tmp = wWindowFor(wwin->transient_for);
1471 if (tmp)
1472 wwin->frame->core->stacking->child_of = tmp->frame->core;
1473 } else {
1474 wwin->frame->core->stacking->child_of = NULL;
1477 if (!scr->focused_window) {
1478 /* first window on the list */
1479 wwin->next = NULL;
1480 wwin->prev = NULL;
1481 scr->focused_window = wwin;
1482 } else {
1483 WWindow *tmp;
1485 /* add window at beginning of focus window list */
1486 tmp = scr->focused_window;
1487 while (tmp->prev)
1488 tmp = tmp->prev;
1489 tmp->prev = wwin;
1490 wwin->next = tmp;
1491 wwin->prev = NULL;
1494 if (wwin->flags.is_gnustep == 0)
1495 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1497 /* if (wPreferences.auto_focus) */
1498 wSetFocusTo(scr, wwin);
1499 wWindowResetMouseGrabs(wwin);
1500 wWindowSetKeyGrabs(wwin);
1502 return wwin;
1506 *----------------------------------------------------------------------
1507 * wUnmanageWindow--
1508 * Removes the frame window from a window and destroys all data
1509 * related to it. The window will be reparented back to the root window
1510 * if restore is True.
1512 * Side effects:
1513 * Everything related to the window is destroyed and the window
1514 * is removed from the window lists. Focus is set to the previous on the
1515 * window list.
1516 *----------------------------------------------------------------------
1518 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1520 WCoreWindow *frame = wwin->frame->core;
1521 WWindow *owner = NULL;
1522 WWindow *newFocusedWindow = NULL;
1523 int wasFocused;
1524 WScreen *scr = wwin->screen_ptr;
1526 /* First close attribute editor window if open */
1527 if (wwin->flags.inspector_open) {
1528 wCloseInspectorForWindow(wwin);
1531 /* Close window menu if it's open for this window */
1532 if (wwin->flags.menu_open_for_me) {
1533 CloseWindowMenu(scr);
1536 if (!destroyed) {
1537 if (!wwin->flags.internal_window)
1538 XRemoveFromSaveSet(dpy, wwin->client_win);
1540 XSelectInput(dpy, wwin->client_win, NoEventMask);
1542 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1543 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1546 XUnmapWindow(dpy, frame->window);
1548 XUnmapWindow(dpy, wwin->client_win);
1550 /* deselect window */
1551 wSelectWindow(wwin, False);
1553 /* remove all pending events on window */
1554 /* I think this only matters for autoraise */
1555 if (wPreferences.raise_delay)
1556 WMDeleteTimerWithClientData(wwin->frame->core);
1558 XFlush(dpy);
1560 /* reparent the window back to the root */
1561 if (restore)
1562 wClientRestore(wwin);
1564 if (wwin->transient_for != scr->root_win) {
1565 owner = wWindowFor(wwin->transient_for);
1566 if (owner) {
1567 if (!owner->flags.semi_focused) {
1568 owner = NULL;
1569 } else {
1570 owner->flags.semi_focused = 0;
1575 wasFocused = wwin->flags.focused;
1577 /* remove from window focus list */
1578 if (!wwin->prev && !wwin->next) {
1579 /* was the only window */
1580 scr->focused_window = NULL;
1581 newFocusedWindow = NULL;
1582 } else {
1583 WWindow *tmp;
1585 if (wwin->prev)
1586 wwin->prev->next = wwin->next;
1587 if (wwin->next)
1588 wwin->next->prev = wwin->prev;
1589 else {
1590 scr->focused_window = wwin->prev;
1591 scr->focused_window->next = NULL;
1594 if (wPreferences.focus_mode == WKF_CLICK) {
1596 /* if in click to focus mode and the window
1597 * was a transient, focus the owner window
1599 tmp = NULL;
1600 if (wPreferences.focus_mode == WKF_CLICK) {
1601 tmp = wWindowFor(wwin->transient_for);
1602 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1603 tmp = NULL;
1606 /* otherwise, focus the next one in the focus list */
1607 if (!tmp) {
1608 tmp = scr->focused_window;
1609 while (tmp) { /* look for one in the window list first */
1610 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1611 && (tmp->flags.mapped || tmp->flags.shaded))
1612 break;
1613 tmp = tmp->prev;
1615 if (!tmp) { /* if unsuccessful, choose any focusable window */
1616 tmp = scr->focused_window;
1617 while (tmp) {
1618 if (!WFLAGP(tmp, no_focusable)
1619 && (tmp->flags.mapped || tmp->flags.shaded))
1620 break;
1621 tmp = tmp->prev;
1626 newFocusedWindow = tmp;
1628 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1629 unsigned int mask;
1630 int foo;
1631 Window bar, win;
1633 /* This is to let the root window get the keyboard input
1634 * if Sloppy focus mode and no other window get focus.
1635 * This way keybindings will not freeze.
1637 tmp = NULL;
1638 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1639 tmp = wWindowFor(win);
1640 if (tmp == wwin)
1641 tmp = NULL;
1642 newFocusedWindow = tmp;
1643 } else {
1644 newFocusedWindow = NULL;
1648 if (!wwin->flags.internal_window)
1649 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1650 if (wasFocused) {
1651 if (newFocusedWindow != owner && owner) {
1652 if (wwin->flags.is_gnustep == 0)
1653 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1655 wSetFocusTo(scr, newFocusedWindow);
1657 wWindowDestroy(wwin);
1658 XFlush(dpy);
1661 void wWindowMap(WWindow *wwin)
1663 XMapWindow(dpy, wwin->frame->core->window);
1664 if (!wwin->flags.shaded) {
1665 /* window will be remapped when getting MapNotify */
1666 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1667 XMapWindow(dpy, wwin->client_win);
1668 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1670 wwin->flags.mapped = 1;
1674 void wWindowUnmap(WWindow *wwin)
1676 wwin->flags.mapped = 0;
1678 /* prevent window withdrawal when getting UnmapNotify */
1679 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1680 XUnmapWindow(dpy, wwin->client_win);
1681 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1683 XUnmapWindow(dpy, wwin->frame->core->window);
1686 void wWindowFocus(WWindow *wwin, WWindow *owin)
1688 WWindow *nowner;
1689 WWindow *oowner;
1691 #ifdef KEEP_XKB_LOCK_STATUS
1692 if (wPreferences.modelock) {
1693 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1695 #endif /* KEEP_XKB_LOCK_STATUS */
1697 wwin->flags.semi_focused = 0;
1699 if (wwin->flags.is_gnustep == 0)
1700 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1702 wwin->flags.focused = 1;
1704 wWindowResetMouseGrabs(wwin);
1706 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1708 if (owin == wwin || !owin)
1709 return;
1711 nowner = wWindowFor(wwin->transient_for);
1713 /* new window is a transient for the old window */
1714 if (nowner == owin) {
1715 owin->flags.semi_focused = 1;
1716 wWindowUnfocus(nowner);
1717 return;
1720 oowner = wWindowFor(owin->transient_for);
1722 /* new window is owner of old window */
1723 if (wwin == oowner) {
1724 wWindowUnfocus(owin);
1725 return;
1728 if (!nowner) {
1729 wWindowUnfocus(owin);
1730 return;
1733 /* new window has same owner of old window */
1734 if (oowner == nowner) {
1735 /* prevent unfocusing of owner */
1736 oowner->flags.semi_focused = 0;
1737 wWindowUnfocus(owin);
1738 oowner->flags.semi_focused = 1;
1740 return;
1743 /* nowner != NULL && oowner != nowner */
1744 nowner->flags.semi_focused = 1;
1745 wWindowUnfocus(nowner);
1746 wWindowUnfocus(owin);
1749 void wWindowUnfocus(WWindow *wwin)
1751 CloseWindowMenu(wwin->screen_ptr);
1753 if (wwin->flags.is_gnustep == 0)
1754 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1756 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1757 WWindow *owner;
1758 owner = wWindowFor(wwin->transient_for);
1759 if (owner && owner->flags.semi_focused) {
1760 owner->flags.semi_focused = 0;
1761 if (owner->flags.mapped || owner->flags.shaded) {
1762 wWindowUnfocus(owner);
1763 wFrameWindowPaint(owner->frame);
1767 wwin->flags.focused = 0;
1768 wWindowResetMouseGrabs(wwin);
1769 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1772 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1774 char *title;
1776 if (!wwin->frame)
1777 return;
1779 wwin->flags.wm_name_changed = 1;
1781 if (!newTitle) {
1782 /* the hint was removed */
1783 title = DEF_WINDOW_TITLE;
1784 } else {
1785 title = newTitle;
1788 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1789 WMPostNotificationName(WMNChangedName, wwin, NULL);
1794 *----------------------------------------------------------------------
1796 * wWindowConstrainSize--
1797 * Constrains size for the client window, taking the maximal size,
1798 * window resize increments and other size hints into account.
1800 * Returns:
1801 * The closest size to what was given that the client window can
1802 * have.
1804 *----------------------------------------------------------------------
1806 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1808 int width = (int)*nwidth;
1809 int height = (int)*nheight;
1810 int winc = 1;
1811 int hinc = 1;
1812 int minW = 1, minH = 1;
1813 int maxW = wwin->screen_ptr->scr_width * 2;
1814 int maxH = wwin->screen_ptr->scr_height * 2;
1815 int minAX = -1, minAY = -1;
1816 int maxAX = -1, maxAY = -1;
1817 int baseW = 0;
1818 int baseH = 0;
1820 if (wwin->normal_hints) {
1821 winc = wwin->normal_hints->width_inc;
1822 hinc = wwin->normal_hints->height_inc;
1823 minW = wwin->normal_hints->min_width;
1824 minH = wwin->normal_hints->min_height;
1825 maxW = wwin->normal_hints->max_width;
1826 maxH = wwin->normal_hints->max_height;
1827 if (wwin->normal_hints->flags & PAspect) {
1828 minAX = wwin->normal_hints->min_aspect.x;
1829 minAY = wwin->normal_hints->min_aspect.y;
1830 maxAX = wwin->normal_hints->max_aspect.x;
1831 maxAY = wwin->normal_hints->max_aspect.y;
1834 baseW = wwin->normal_hints->base_width;
1835 baseH = wwin->normal_hints->base_height;
1838 if (width < minW)
1839 width = minW;
1840 if (height < minH)
1841 height = minH;
1843 if (width > maxW)
1844 width = maxW;
1845 if (height > maxH)
1846 height = maxH;
1848 /* aspect ratio code borrowed from olwm */
1849 if (minAX > 0) {
1850 /* adjust max aspect ratio */
1851 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1852 if (maxAX > maxAY) {
1853 height = (width * maxAY) / maxAX;
1854 if (height > maxH) {
1855 height = maxH;
1856 width = (height * maxAX) / maxAY;
1858 } else {
1859 width = (height * maxAX) / maxAY;
1860 if (width > maxW) {
1861 width = maxW;
1862 height = (width * maxAY) / maxAX;
1867 /* adjust min aspect ratio */
1868 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1869 if (minAX > minAY) {
1870 height = (width * minAY) / minAX;
1871 if (height < minH) {
1872 height = minH;
1873 width = (height * minAX) / minAY;
1875 } else {
1876 width = (height * minAX) / minAY;
1877 if (width < minW) {
1878 width = minW;
1879 height = (width * minAY) / minAX;
1885 if (baseW != 0)
1886 width = (((width - baseW) / winc) * winc) + baseW;
1887 else
1888 width = (((width - minW) / winc) * winc) + minW;
1890 if (baseH != 0)
1891 height = (((height - baseH) / hinc) * hinc) + baseH;
1892 else
1893 height = (((height - minH) / hinc) * hinc) + minH;
1895 /* broken stupid apps may cause preposterous values for these.. */
1896 if (width > 0)
1897 *nwidth = width;
1898 if (height > 0)
1899 *nheight = height;
1902 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1903 unsigned int *width, unsigned int *height)
1905 int baseW = 0, baseH = 0;
1906 int winc = 1, hinc = 1;
1908 if (wwin->normal_hints) {
1909 baseW = wwin->normal_hints->base_width;
1910 baseH = wwin->normal_hints->base_height;
1912 winc = wwin->normal_hints->width_inc;
1913 hinc = wwin->normal_hints->height_inc;
1916 if (*width > maxW)
1917 *width = maxW - (maxW - baseW) % winc;
1919 if (*height > maxH)
1920 *height = maxH - (maxH - baseH) % hinc;
1923 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1925 WScreen *scr = wwin->screen_ptr;
1926 WApplication *wapp;
1927 int unmap = 0;
1929 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1930 return;
1932 if (workspace != scr->current_workspace) {
1933 /* Sent to other workspace. Unmap window */
1934 if ((wwin->flags.mapped
1935 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1936 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1938 wapp = wApplicationOf(wwin->main_window);
1939 if (wapp) {
1940 wapp->last_workspace = workspace;
1942 if (wwin->flags.miniaturized) {
1943 if (wwin->icon) {
1944 XUnmapWindow(dpy, wwin->icon->core->window);
1945 wwin->icon->mapped = 0;
1947 } else {
1948 unmap = 1;
1949 wSetFocusTo(scr, NULL);
1952 } else {
1953 /* brought to current workspace. Map window */
1954 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1955 if (wwin->icon) {
1956 XMapWindow(dpy, wwin->icon->core->window);
1957 wwin->icon->mapped = 1;
1959 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1960 wWindowMap(wwin);
1963 if (!IS_OMNIPRESENT(wwin)) {
1964 int oldWorkspace = wwin->frame->workspace;
1966 wwin->frame->workspace = workspace;
1968 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
1971 if (unmap)
1972 wWindowUnmap(wwin);
1975 void wWindowSynthConfigureNotify(WWindow *wwin)
1977 XEvent sevent;
1979 sevent.type = ConfigureNotify;
1980 sevent.xconfigure.display = dpy;
1981 sevent.xconfigure.event = wwin->client_win;
1982 sevent.xconfigure.window = wwin->client_win;
1984 sevent.xconfigure.x = wwin->client.x;
1985 sevent.xconfigure.y = wwin->client.y;
1986 sevent.xconfigure.width = wwin->client.width;
1987 sevent.xconfigure.height = wwin->client.height;
1989 sevent.xconfigure.border_width = wwin->old_border_width;
1990 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
1991 sevent.xconfigure.above = wwin->frame->titlebar->window;
1992 else
1993 sevent.xconfigure.above = None;
1995 sevent.xconfigure.override_redirect = False;
1996 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1997 XFlush(dpy);
2001 *----------------------------------------------------------------------
2002 * wWindowConfigure()
2004 * req_x, req_y: new requested positions for the frame
2005 * req_width, req_height: new requested sizes for the client
2007 * Configures the frame, decorations and client window to the specified
2008 * geometry, whose validity is not checked -- wWindowConstrainSize()
2009 * must be used for that.
2010 * The size parameters are for the client window, but the position is
2011 * for the frame.
2012 * The client window receives a ConfigureNotify event, according
2013 * to what ICCCM says.
2015 * Returns:
2016 * None
2018 * Side effects:
2019 * Window size and position are changed and client window receives
2020 * a ConfigureNotify event.
2021 *----------------------------------------------------------------------
2023 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2025 int synth_notify = False;
2026 int resize;
2028 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2030 * if the window is being moved but not resized then
2031 * send a synthetic ConfigureNotify
2033 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2034 synth_notify = True;
2037 if (WFLAGP(wwin, dont_move_off))
2038 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2039 if (resize) {
2040 if (req_width < MIN_WINDOW_SIZE)
2041 req_width = MIN_WINDOW_SIZE;
2042 if (req_height < MIN_WINDOW_SIZE)
2043 req_height = MIN_WINDOW_SIZE;
2045 /* If growing, resize inner part before frame,
2046 * if shrinking, resize frame before.
2047 * This will prevent the frame (that can have a different color)
2048 * to be exposed, causing flicker */
2049 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2050 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2052 if (wwin->flags.shaded) {
2053 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2054 wwin->old_geometry.height = req_height;
2055 } else {
2056 int h;
2058 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2060 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2063 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2064 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2066 wwin->client.x = req_x;
2067 wwin->client.y = req_y + wwin->frame->top_width;
2068 wwin->client.width = req_width;
2069 wwin->client.height = req_height;
2070 } else {
2071 wwin->client.x = req_x;
2072 wwin->client.y = req_y + wwin->frame->top_width;
2074 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2076 wwin->frame_x = req_x;
2077 wwin->frame_y = req_y;
2078 if (HAS_BORDER(wwin)) {
2079 wwin->client.x += FRAME_BORDER_WIDTH;
2080 wwin->client.y += FRAME_BORDER_WIDTH;
2082 #ifdef SHAPE
2083 if (wShapeSupported && wwin->flags.shaped && resize) {
2084 wWindowSetShape(wwin);
2086 #endif
2088 if (synth_notify)
2089 wWindowSynthConfigureNotify(wwin);
2090 XFlush(dpy);
2093 /* req_x, req_y: new position of the frame */
2094 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2096 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2097 int synth_notify = False;
2099 /* Send a synthetic ConfigureNotify event for every window movement. */
2100 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2101 synth_notify = True;
2103 #else
2104 /* A single synthetic ConfigureNotify event is sent at the end of
2105 * a completed (opaque) movement in moveres.c */
2106 #endif
2108 if (WFLAGP(wwin, dont_move_off))
2109 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2110 wwin->frame->core->width, wwin->frame->core->height);
2112 wwin->client.x = req_x;
2113 wwin->client.y = req_y + wwin->frame->top_width;
2114 if (HAS_BORDER(wwin)) {
2115 wwin->client.x += FRAME_BORDER_WIDTH;
2116 wwin->client.y += FRAME_BORDER_WIDTH;
2119 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2121 wwin->frame_x = req_x;
2122 wwin->frame_y = req_y;
2124 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2125 if (synth_notify)
2126 wWindowSynthConfigureNotify(wwin);
2127 #endif
2130 void wWindowUpdateButtonImages(WWindow *wwin)
2132 WScreen *scr = wwin->screen_ptr;
2133 Pixmap pixmap, mask;
2134 WFrameWindow *fwin = wwin->frame;
2136 if (!HAS_TITLEBAR(wwin))
2137 return;
2139 /* miniaturize button */
2140 if (!WFLAGP(wwin, no_miniaturize_button)) {
2141 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2142 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2144 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2145 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2146 } else {
2147 mask = None;
2150 if (fwin->lbutton_image
2151 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2152 wPixmapDestroy(fwin->lbutton_image);
2153 fwin->lbutton_image = NULL;
2156 if (!fwin->lbutton_image) {
2157 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2158 fwin->lbutton_image->client_owned = 1;
2159 fwin->lbutton_image->client_owned_mask = 1;
2161 } else {
2162 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2163 wPixmapDestroy(fwin->lbutton_image);
2165 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2168 #ifdef XKB_BUTTON_HINT
2169 if (!WFLAGP(wwin, no_language_button)) {
2170 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2171 wPixmapDestroy(fwin->languagebutton_image);
2173 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2175 #endif
2177 /* close button */
2179 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2180 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2182 if (!WFLAGP(wwin, no_close_button)) {
2183 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2184 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2186 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2187 mask = wwin->wm_gnustep_attr->close_mask;
2188 else
2189 mask = None;
2191 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2192 || fwin->rbutton_image->mask != mask)) {
2193 wPixmapDestroy(fwin->rbutton_image);
2194 fwin->rbutton_image = NULL;
2197 if (!fwin->rbutton_image) {
2198 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2199 fwin->rbutton_image->client_owned = 1;
2200 fwin->rbutton_image->client_owned_mask = 1;
2203 } else if (WFLAGP(wwin, kill_close)) {
2205 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2206 wPixmapDestroy(fwin->rbutton_image);
2208 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2210 } 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 {
2219 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2220 wPixmapDestroy(fwin->rbutton_image);
2222 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2226 /* force buttons to be redrawn */
2227 fwin->flags.need_texture_change = 1;
2228 wFrameWindowPaint(fwin);
2232 *---------------------------------------------------------------------------
2233 * wWindowConfigureBorders--
2234 * Update window border configuration according to attribute flags.
2236 *---------------------------------------------------------------------------
2238 void wWindowConfigureBorders(WWindow *wwin)
2240 if (wwin->frame) {
2241 int flags;
2242 int newy, oldh;
2244 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2246 #ifdef XKB_BUTTON_HINT
2247 if (wPreferences.modelock)
2248 flags |= WFF_LANGUAGE_BUTTON;
2249 #endif
2251 if (HAS_TITLEBAR(wwin))
2252 flags |= WFF_TITLEBAR;
2253 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2254 flags |= WFF_RESIZEBAR;
2255 if (HAS_BORDER(wwin))
2256 flags |= WFF_BORDER;
2257 if (wwin->flags.shaded)
2258 flags |= WFF_IS_SHADED;
2260 oldh = wwin->frame->top_width;
2261 wFrameWindowUpdateBorders(wwin->frame, flags);
2262 if (oldh != wwin->frame->top_width) {
2263 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2265 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2266 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2269 flags = 0;
2270 if (!WFLAGP(wwin, no_miniaturize_button)
2271 && wwin->frame->flags.hide_left_button)
2272 flags |= WFF_LEFT_BUTTON;
2274 #ifdef XKB_BUTTON_HINT
2275 if (!WFLAGP(wwin, no_language_button)
2276 && wwin->frame->flags.hide_language_button) {
2277 flags |= WFF_LANGUAGE_BUTTON;
2279 #endif
2281 if (!WFLAGP(wwin, no_close_button)
2282 && wwin->frame->flags.hide_right_button)
2283 flags |= WFF_RIGHT_BUTTON;
2285 if (flags != 0) {
2286 wWindowUpdateButtonImages(wwin);
2287 wFrameWindowShowButton(wwin->frame, flags);
2290 flags = 0;
2291 if (WFLAGP(wwin, no_miniaturize_button)
2292 && !wwin->frame->flags.hide_left_button)
2293 flags |= WFF_LEFT_BUTTON;
2295 #ifdef XKB_BUTTON_HINT
2296 if (WFLAGP(wwin, no_language_button)
2297 && !wwin->frame->flags.hide_language_button)
2298 flags |= WFF_LANGUAGE_BUTTON;
2299 #endif
2301 if (WFLAGP(wwin, no_close_button)
2302 && !wwin->frame->flags.hide_right_button)
2303 flags |= WFF_RIGHT_BUTTON;
2305 if (flags != 0)
2306 wFrameWindowHideButton(wwin->frame, flags);
2308 #ifdef SHAPE
2309 if (wShapeSupported && wwin->flags.shaped) {
2310 wWindowSetShape(wwin);
2312 #endif
2316 void wWindowSaveState(WWindow * wwin)
2318 long data[10];
2319 int i;
2321 memset(data, 0, sizeof(long) * 10);
2322 data[0] = wwin->frame->workspace;
2323 data[1] = wwin->flags.miniaturized;
2324 data[2] = wwin->flags.shaded;
2325 data[3] = wwin->flags.hidden;
2326 data[4] = wwin->flags.maximized;
2327 if (wwin->flags.maximized == 0) {
2328 data[5] = wwin->frame_x;
2329 data[6] = wwin->frame_y;
2330 data[7] = wwin->frame->core->width;
2331 data[8] = wwin->frame->core->height;
2332 } else {
2333 data[5] = wwin->old_geometry.x;
2334 data[6] = wwin->old_geometry.y;
2335 data[7] = wwin->old_geometry.width;
2336 data[8] = wwin->old_geometry.height;
2339 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2340 if (wwin->screen_ptr->shortcutWindows[i] &&
2341 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2342 data[9] |= 1 << i;
2344 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2345 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2348 static int getSavedState(Window window, WSavedState ** state)
2350 Atom type_ret;
2351 int fmt_ret;
2352 unsigned long nitems_ret;
2353 unsigned long bytes_after_ret;
2354 long *data;
2356 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2357 True, _XA_WINDOWMAKER_STATE,
2358 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2359 (unsigned char **)&data) != Success || !data || nitems_ret < 10)
2360 return 0;
2362 *state = wmalloc(sizeof(WSavedState));
2364 (*state)->workspace = data[0];
2365 (*state)->miniaturized = data[1];
2366 (*state)->shaded = data[2];
2367 (*state)->hidden = data[3];
2368 (*state)->maximized = data[4];
2369 (*state)->x = data[5];
2370 (*state)->y = data[6];
2371 (*state)->w = data[7];
2372 (*state)->h = data[8];
2373 (*state)->window_shortcuts = data[9];
2375 XFree(data);
2377 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2378 return 1;
2379 else
2380 return 0;
2383 #ifdef SHAPE
2384 void wWindowClearShape(WWindow * wwin)
2386 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2387 0, wwin->frame->top_width, None, ShapeSet);
2388 XFlush(dpy);
2391 void wWindowSetShape(WWindow * wwin)
2393 XRectangle rect[2];
2394 int count;
2395 #ifdef OPTIMIZE_SHAPE
2396 XRectangle *rects;
2397 XRectangle *urec;
2398 int ordering;
2400 /* only shape is the client's */
2401 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2402 goto alt_code;
2405 /* Get array of rectangles describing the shape mask */
2406 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2407 if (!rects) {
2408 goto alt_code;
2411 urec = malloc(sizeof(XRectangle) * (count + 2));
2412 if (!urec) {
2413 XFree(rects);
2414 goto alt_code;
2417 /* insert our decoration rectangles in the rect list */
2418 memcpy(urec, rects, sizeof(XRectangle) * count);
2419 XFree(rects);
2421 if (HAS_TITLEBAR(wwin)) {
2422 urec[count].x = -1;
2423 urec[count].y = -1 - wwin->frame->top_width;
2424 urec[count].width = wwin->frame->core->width + 2;
2425 urec[count].height = wwin->frame->top_width + 1;
2426 count++;
2428 if (HAS_RESIZEBAR(wwin)) {
2429 urec[count].x = -1;
2430 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2431 urec[count].width = wwin->frame->core->width + 2;
2432 urec[count].height = wwin->frame->bottom_width + 1;
2433 count++;
2436 /* shape our frame window */
2437 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2438 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2439 XFlush(dpy);
2440 wfree(urec);
2441 return;
2443 alt_code:
2444 #endif /* OPTIMIZE_SHAPE */
2445 count = 0;
2446 if (HAS_TITLEBAR(wwin)) {
2447 rect[count].x = -1;
2448 rect[count].y = -1;
2449 rect[count].width = wwin->frame->core->width + 2;
2450 rect[count].height = wwin->frame->top_width + 1;
2451 count++;
2453 if (HAS_RESIZEBAR(wwin)) {
2454 rect[count].x = -1;
2455 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2456 rect[count].width = wwin->frame->core->width + 2;
2457 rect[count].height = wwin->frame->bottom_width + 1;
2458 count++;
2460 if (count > 0) {
2461 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2462 0, 0, rect, count, ShapeSet, Unsorted);
2464 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2465 0, wwin->frame->top_width, wwin->client_win,
2466 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2467 XFlush(dpy);
2469 #endif /* SHAPE */
2471 /* ====================================================================== */
2473 static FocusMode getFocusMode(WWindow * wwin)
2475 FocusMode mode;
2477 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2478 if (wwin->wm_hints->input == True) {
2479 if (wwin->protocols.TAKE_FOCUS)
2480 mode = WFM_LOCALLY_ACTIVE;
2481 else
2482 mode = WFM_PASSIVE;
2483 } else {
2484 if (wwin->protocols.TAKE_FOCUS)
2485 mode = WFM_GLOBALLY_ACTIVE;
2486 else
2487 mode = WFM_NO_INPUT;
2489 } else {
2490 mode = WFM_PASSIVE;
2492 return mode;
2495 void wWindowSetKeyGrabs(WWindow * wwin)
2497 int i;
2498 WShortKey *key;
2500 for (i = 0; i < WKBD_LAST; i++) {
2501 key = &wKeyBindings[i];
2503 if (key->keycode == 0)
2504 continue;
2505 if (key->modifier != AnyModifier) {
2506 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2507 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2508 #ifdef NUMLOCK_HACK
2509 /* Also grab all modifier combinations possible that include,
2510 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2511 * work even if the NumLock/ScrollLock key is on.
2513 wHackedGrabKey(key->keycode, key->modifier,
2514 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2515 #endif
2517 XGrabKey(dpy, key->keycode, key->modifier,
2518 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2521 wRootMenuBindShortcuts(wwin->frame->core->window);
2524 void wWindowResetMouseGrabs(WWindow * wwin)
2526 /* Mouse grabs can't be done on the client window because of
2527 * ICCCM and because clients that try to do the same will crash.
2529 * But there is a problem wich makes tbar buttons of unfocused
2530 * windows not usable as the click goes to the frame window instead
2531 * of the button itself. Must figure a way to fix that.
2534 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2536 if (!WFLAGP(wwin, no_bind_mouse)) {
2537 /* grabs for Meta+drag */
2538 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2539 True, ButtonPressMask | ButtonReleaseMask,
2540 GrabModeSync, GrabModeAsync, None, None);
2542 /* for CTRL+Wheel to Scroll Horiz, we have to grab CTRL as well
2543 * but we only grab it for Button4 and Button 5 since a lot of apps
2544 * use CTRL+Button1-3 for app related functionality
2546 if (wPreferences.resize_increment > 0) {
2547 wHackedGrabButton(Button4, ControlMask, wwin->client_win,
2548 True, ButtonPressMask | ButtonReleaseMask,
2549 GrabModeSync, GrabModeAsync, None, None);
2550 wHackedGrabButton(Button5, ControlMask, wwin->client_win,
2551 True, ButtonPressMask | ButtonReleaseMask,
2552 GrabModeSync, GrabModeAsync, None, None);
2554 wHackedGrabButton(Button4, MOD_MASK | ControlMask, wwin->client_win,
2555 True, ButtonPressMask | ButtonReleaseMask,
2556 GrabModeSync, GrabModeAsync, None, None);
2557 wHackedGrabButton(Button5, MOD_MASK | ControlMask, wwin->client_win,
2558 True, ButtonPressMask | ButtonReleaseMask,
2559 GrabModeSync, GrabModeAsync, None, None);
2563 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2564 && !wwin->flags.is_gnustep) {
2565 /* the passive grabs to focus the window */
2566 /* if (wPreferences.focus_mode == WKF_CLICK) */
2567 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2568 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2570 XFlush(dpy);
2573 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2575 if (attr->flags & GSExtraFlagsAttr) {
2576 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2577 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2578 wWindowUpdateButtonImages(wwin);
2583 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2585 WWindowState *wstate;
2587 wstate = malloc(sizeof(WWindowState));
2588 if (!wstate)
2589 return NULL;
2591 memset(wstate, 0, sizeof(WWindowState));
2592 wstate->pid = pid;
2593 if (instance)
2594 wstate->instance = wstrdup(instance);
2595 if (class)
2596 wstate->class = wstrdup(class);
2597 if (command)
2598 wstate->command = wstrdup(command);
2599 wstate->state = state;
2601 wstate->next = windowState;
2602 windowState = wstate;
2604 return wstate;
2607 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2609 WMagicNumber wWindowGetSavedState(Window win)
2611 char *instance, *class, *command = NULL;
2612 WWindowState *wstate = windowState;
2614 if (!wstate)
2615 return NULL;
2617 command = GetCommandForWindow(win);
2618 if (!command)
2619 return NULL;
2621 if (PropGetWMClass(win, &class, &instance)) {
2622 while (wstate) {
2623 if (SAME(instance, wstate->instance) &&
2624 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2625 break;
2627 wstate = wstate->next;
2629 } else {
2630 wstate = NULL;
2633 if (command)
2634 wfree(command);
2635 if (instance)
2636 XFree(instance);
2637 if (class)
2638 XFree(class);
2640 return wstate;
2643 void wWindowDeleteSavedState(WMagicNumber id)
2645 WWindowState *tmp, *wstate = (WWindowState *) id;
2647 if (!wstate || !windowState)
2648 return;
2650 tmp = windowState;
2651 if (tmp == wstate) {
2652 windowState = wstate->next;
2653 if (wstate->instance)
2654 wfree(wstate->instance);
2655 if (wstate->class)
2656 wfree(wstate->class);
2657 if (wstate->command)
2658 wfree(wstate->command);
2659 wfree(wstate->state);
2660 wfree(wstate);
2661 } else {
2662 while (tmp->next) {
2663 if (tmp->next == wstate) {
2664 tmp->next = wstate->next;
2665 if (wstate->instance)
2666 wfree(wstate->instance);
2667 if (wstate->class)
2668 wfree(wstate->class);
2669 if (wstate->command)
2670 wfree(wstate->command);
2671 wfree(wstate->state);
2672 wfree(wstate);
2673 break;
2675 tmp = tmp->next;
2680 void wWindowDeleteSavedStatesForPID(pid_t pid)
2682 WWindowState *tmp, *wstate;
2684 if (!windowState)
2685 return;
2687 tmp = windowState;
2688 if (tmp->pid == pid) {
2689 wstate = windowState;
2690 windowState = tmp->next;
2691 if (wstate->instance)
2692 wfree(wstate->instance);
2693 if (wstate->class)
2694 wfree(wstate->class);
2695 if (wstate->command)
2696 wfree(wstate->command);
2697 wfree(wstate->state);
2698 wfree(wstate);
2699 } else {
2700 while (tmp->next) {
2701 if (tmp->next->pid == pid) {
2702 wstate = tmp->next;
2703 tmp->next = wstate->next;
2704 if (wstate->instance)
2705 wfree(wstate->instance);
2706 if (wstate->class)
2707 wfree(wstate->class);
2708 if (wstate->command)
2709 wfree(wstate->command);
2710 wfree(wstate->state);
2711 wfree(wstate);
2712 break;
2714 tmp = tmp->next;
2719 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2721 if (wwin->flags.omnipresent == flag)
2722 return;
2724 wwin->flags.omnipresent = flag;
2725 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2728 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2730 WWindow *wwin = data;
2732 #ifndef NUMLOCK_HACK
2733 if ((event->xbutton.state & ValidModMask)
2734 != (event->xbutton.state & ~LockMask)) {
2735 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2736 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2738 #endif
2740 event->xbutton.state &= ValidModMask;
2742 CloseWindowMenu(wwin->screen_ptr);
2744 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2745 && !WFLAGP(wwin, no_focusable)) {
2746 wSetFocusTo(wwin->screen_ptr, wwin);
2749 if (event->xbutton.button == Button1)
2750 wRaiseFrame(wwin->frame->core);
2752 if (event->xbutton.window != wwin->frame->resizebar->window) {
2753 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2754 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2755 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2756 return;
2760 if (event->xbutton.state & MOD_MASK) {
2761 /* move the window */
2762 wMouseMoveWindow(wwin, event);
2763 XUngrabPointer(dpy, CurrentTime);
2764 } else {
2765 wMouseResizeWindow(wwin, event);
2766 XUngrabPointer(dpy, CurrentTime);
2770 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2772 WWindow *wwin = data;
2774 event->xbutton.state &= ValidModMask;
2776 if (event->xbutton.button == Button1) {
2777 if (event->xbutton.state == 0) {
2778 if (!WFLAGP(wwin, no_shadeable)) {
2779 /* shade window */
2780 if (wwin->flags.shaded)
2781 wUnshadeWindow(wwin);
2782 else
2783 wShadeWindow(wwin);
2785 } else {
2786 int dir = 0;
2788 if (event->xbutton.state & ControlMask)
2789 dir |= MAX_VERTICAL;
2791 if (event->xbutton.state & ShiftMask) {
2792 dir |= MAX_HORIZONTAL;
2793 if (!(event->xbutton.state & ControlMask))
2794 wSelectWindow(wwin, !wwin->flags.selected);
2797 /* maximize window */
2798 if (dir != 0 && IS_RESIZABLE(wwin)) {
2799 int ndir = dir ^ wwin->flags.maximized;
2801 if (ndir != 0)
2802 wMaximizeWindow(wwin, ndir);
2803 else
2804 wUnmaximizeWindow(wwin);
2807 } else if (event->xbutton.button == Button3) {
2808 if (event->xbutton.state & MOD_MASK) {
2809 wHideOtherApplications(wwin);
2811 } else if (event->xbutton.button == Button2) {
2812 wSelectWindow(wwin, !wwin->flags.selected);
2813 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2814 wShadeWindow(wwin);
2815 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2816 wUnshadeWindow(wwin);
2820 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2822 WWindow *wwin = desc->parent;
2823 unsigned int new_width, w_scale;
2824 unsigned int new_height, h_scale;
2825 unsigned int resize_width_increment = 0;
2826 unsigned int resize_height_increment = 0;
2828 if (wwin->normal_hints) {
2829 w_scale = (wPreferences.resize_increment + wwin->normal_hints->width_inc - 1) / wwin->normal_hints->width_inc;
2830 h_scale = (wPreferences.resize_increment + wwin->normal_hints->height_inc - 1) / wwin->normal_hints->height_inc;
2831 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2832 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2834 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2835 resize_width_increment = wPreferences.resize_increment;
2836 resize_height_increment = wPreferences.resize_increment;
2839 event->xbutton.state &= ValidModMask;
2841 CloseWindowMenu(wwin->screen_ptr);
2843 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2844 wSetFocusTo(wwin->screen_ptr, wwin);
2846 if (event->xbutton.button == Button1)
2847 wRaiseFrame(wwin->frame->core);
2849 if (event->xbutton.state & ControlMask) {
2850 if (event->xbutton.button == Button4) {
2851 new_width = wwin->client.width - resize_width_increment;
2852 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2853 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2855 if (event->xbutton.button == Button5) {
2856 new_width = wwin->client.width + resize_width_increment;
2857 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2858 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2862 if (event->xbutton.state & MOD_MASK) {
2863 /* move the window */
2864 if (XGrabPointer(dpy, wwin->client_win, False,
2865 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2866 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2867 return;
2869 if (event->xbutton.button == Button3) {
2870 wMouseResizeWindow(wwin, event);
2871 } else if (event->xbutton.button == Button4) {
2872 new_height = wwin->client.height - resize_height_increment;
2873 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2874 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2875 } else if (event->xbutton.button == Button5) {
2876 new_height = wwin->client.height + resize_height_increment;
2877 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2878 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2879 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2880 wMouseMoveWindow(wwin, event);
2882 XUngrabPointer(dpy, CurrentTime);
2886 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2888 WWindow *wwin = (WWindow *) data;
2890 #ifndef NUMLOCK_HACK
2891 if ((event->xbutton.state & ValidModMask)
2892 != (event->xbutton.state & ~LockMask)) {
2893 wwarning(_("The NumLock, ScrollLock or similar key seems to be turned on. "
2894 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2896 #endif
2897 event->xbutton.state &= ValidModMask;
2899 CloseWindowMenu(wwin->screen_ptr);
2901 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2902 && !WFLAGP(wwin, no_focusable)) {
2903 wSetFocusTo(wwin->screen_ptr, wwin);
2906 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2908 if (event->xbutton.button == Button1) {
2909 if (event->xbutton.state & MOD_MASK) {
2910 wLowerFrame(wwin->frame->core);
2911 } else {
2912 wRaiseFrame(wwin->frame->core);
2915 if ((event->xbutton.state & ShiftMask)
2916 && !(event->xbutton.state & ControlMask)) {
2917 wSelectWindow(wwin, !wwin->flags.selected);
2918 return;
2920 if (event->xbutton.window != wwin->frame->titlebar->window
2921 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2922 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2923 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2924 return;
2927 /* move the window */
2928 wMouseMoveWindow(wwin, event);
2930 XUngrabPointer(dpy, CurrentTime);
2931 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2932 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2933 WObjDescriptor *desc;
2935 if (event->xbutton.window != wwin->frame->titlebar->window
2936 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2937 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2938 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2939 return;
2942 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2944 /* allow drag select */
2945 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2946 event->xany.send_event = True;
2947 (*desc->handle_mousedown) (desc, event);
2949 XUngrabPointer(dpy, CurrentTime);
2953 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2955 WWindow *wwin = data;
2957 event->xbutton.state &= ValidModMask;
2959 CloseWindowMenu(wwin->screen_ptr);
2961 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2962 return;
2964 /* if control-click, kill the client */
2965 if (event->xbutton.state & ControlMask) {
2966 wClientKill(wwin);
2967 } else {
2968 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
2969 /* send delete message */
2970 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2975 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2977 WWindow *wwin = data;
2979 CloseWindowMenu(wwin->screen_ptr);
2981 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2982 return;
2984 /* send delete message */
2985 if (wwin->protocols.DELETE_WINDOW) {
2986 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2987 } else {
2988 wClientKill(wwin);
2992 #ifdef XKB_BUTTON_HINT
2993 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
2995 WWindow *wwin = data;
2996 WFrameWindow *fwin = wwin->frame;
2997 WScreen *scr = fwin->screen_ptr;
2998 int tl;
3000 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3001 return;
3002 tl = wwin->frame->languagemode;
3003 wwin->frame->languagemode = wwin->frame->last_languagemode;
3004 wwin->frame->last_languagemode = tl;
3005 wSetFocusTo(scr, wwin);
3006 wwin->frame->languagebutton_image =
3007 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3008 wFrameWindowUpdateLanguageButton(wwin->frame);
3009 if (event->xbutton.button == Button3)
3010 return;
3011 wRaiseFrame(fwin->core);
3013 #endif
3015 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3017 WWindow *wwin = data;
3019 event->xbutton.state &= ValidModMask;
3021 CloseWindowMenu(wwin->screen_ptr);
3023 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3024 return;
3026 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3027 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3028 } else {
3029 WApplication *wapp;
3030 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3032 wapp = wApplicationOf(wwin->main_window);
3033 if (wapp && !WFLAGP(wwin, no_appicon))
3034 wHideApplication(wapp);
3035 } else if (event->xbutton.state == 0) {
3036 wIconifyWindow(wwin);