CTRL+Wheel Horizontal Resize + extras
[wmaker-crm.git] / src / window.c
blob9bb14c07021e9db9487596680039d9376627ab87
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <stdint.h>
37 #include <math.h>
39 /* For getting mouse wheel mappings from WINGs */
40 #include <WINGs/WINGsP.h>
42 #include "WindowMaker.h"
43 #include "GNUstep.h"
44 #include "wcore.h"
45 #include "framewin.h"
46 #include "texture.h"
47 #include "window.h"
48 #include "winspector.h"
49 #include "icon.h"
50 #include "properties.h"
51 #include "actions.h"
52 #include "client.h"
53 #include "funcs.h"
54 #include "keybind.h"
55 #include "stacking.h"
56 #include "defaults.h"
57 #include "workspace.h"
58 #include "xinerama.h"
60 #ifdef MWM_HINTS
61 # include "motif.h"
62 #endif
63 #include "wmspec.h"
65 #define MOD_MASK wPreferences.modifier_mask
67 /****** Global Variables ******/
68 extern WShortKey wKeyBindings[WKBD_LAST];
70 #ifdef SHAPE
71 extern Bool wShapeSupported;
72 #endif
74 /* contexts */
75 extern XContext wWinContext;
77 /* cursors */
78 extern Cursor wCursor[WCUR_LAST];
80 /* protocol atoms */
81 extern Atom _XA_WM_DELETE_WINDOW;
82 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
83 extern Atom _XA_WINDOWMAKER_STATE;
85 extern WPreferences wPreferences;
86 extern Time LastTimestamp;
88 /* superfluous... */
89 extern void DoWindowBirth(WWindow *wwin);
91 /***** Local Stuff *****/
92 static WWindowState *windowState = NULL;
94 static FocusMode getFocusMode(WWindow *wwin);
95 static int getSavedState(Window window, WSavedState **state);
96 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
98 /* frame window (during window grabs) */
99 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
101 /* close button */
102 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
103 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
105 /* iconify button */
106 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
108 #ifdef XKB_BUTTON_HINT
109 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
110 #endif
112 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
113 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
114 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
116 /****** Notification Observers ******/
118 static void appearanceObserver(void *self, WMNotification * notif)
120 WWindow *wwin = (WWindow *) self;
121 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
123 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
124 return;
126 if (flags & WFontSettings) {
127 wWindowConfigureBorders(wwin);
128 if (wwin->flags.shaded) {
129 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
131 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
132 wWindowSynthConfigureNotify(wwin);
135 if (flags & WTextureSettings) {
136 wwin->frame->flags.need_texture_remake = 1;
138 if (flags & (WTextureSettings | WColorSettings)) {
139 if (wwin->frame->titlebar)
140 XClearWindow(dpy, wwin->frame->titlebar->window);
142 wFrameWindowPaint(wwin->frame);
147 WWindow *wWindowFor(Window window)
149 WObjDescriptor *desc;
151 if (window == None)
152 return NULL;
154 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
155 return NULL;
157 if (desc->parent_type == WCLASS_WINDOW)
158 return desc->parent;
159 else if (desc->parent_type == WCLASS_FRAME) {
160 WFrameWindow *frame = (WFrameWindow *) desc->parent;
161 if (frame->flags.is_client_window_frame)
162 return frame->child;
165 return NULL;
168 WWindow *wWindowCreate()
170 WWindow *wwin;
172 wwin = wmalloc(sizeof(WWindow));
173 wretain(wwin);
175 memset(wwin, 0, sizeof(WWindow));
177 wwin->client_descriptor.handle_mousedown = frameMouseDown;
178 wwin->client_descriptor.parent = wwin;
179 wwin->client_descriptor.self = wwin;
180 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
182 return wwin;
185 void wWindowDestroy(WWindow *wwin)
187 int i;
189 if (wwin->screen_ptr->cmap_window == wwin)
190 wwin->screen_ptr->cmap_window = NULL;
192 WMRemoveNotificationObserver(wwin);
194 wwin->flags.destroyed = 1;
196 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
197 if (!wwin->screen_ptr->shortcutWindows[i])
198 continue;
200 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
202 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
203 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
204 wwin->screen_ptr->shortcutWindows[i] = NULL;
208 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
209 wwin->fake_group->retainCount--;
210 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
211 XDestroyWindow(dpy, wwin->fake_group->leader);
212 wwin->fake_group->leader = None;
213 wwin->fake_group->origLeader = None;
214 XFlush(dpy);
218 if (wwin->normal_hints)
219 XFree(wwin->normal_hints);
221 if (wwin->wm_hints)
222 XFree(wwin->wm_hints);
224 if (wwin->wm_instance)
225 XFree(wwin->wm_instance);
227 if (wwin->wm_class)
228 XFree(wwin->wm_class);
230 if (wwin->wm_gnustep_attr)
231 wfree(wwin->wm_gnustep_attr);
233 if (wwin->cmap_windows)
234 XFree(wwin->cmap_windows);
236 XDeleteContext(dpy, wwin->client_win, wWinContext);
238 if (wwin->frame)
239 wFrameWindowDestroy(wwin->frame);
241 if (wwin->icon) {
242 RemoveFromStackList(wwin->icon->core);
243 wIconDestroy(wwin->icon);
244 if (wPreferences.auto_arrange_icons)
245 wArrangeIcons(wwin->screen_ptr, True);
247 if (wwin->net_icon_image)
248 RReleaseImage(wwin->net_icon_image);
250 wrelease(wwin);
253 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
255 if (gs_hints->flags & GSWindowStyleAttr) {
256 if (gs_hints->window_style == WMBorderlessWindowMask) {
257 wwin->client_flags.no_border = 1;
258 wwin->client_flags.no_titlebar = 1;
259 wwin->client_flags.no_closable = 1;
260 wwin->client_flags.no_miniaturizable = 1;
261 wwin->client_flags.no_resizable = 1;
262 wwin->client_flags.no_close_button = 1;
263 wwin->client_flags.no_miniaturize_button = 1;
264 wwin->client_flags.no_resizebar = 1;
265 } else {
266 wwin->client_flags.no_close_button =
267 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
269 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
271 wwin->client_flags.no_miniaturize_button =
272 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
274 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
276 wwin->client_flags.no_resizebar =
277 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
279 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
281 /* these attributes supposedly imply in the existence
282 * of a titlebar */
283 if (gs_hints->window_style & (WMResizableWindowMask |
284 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
285 wwin->client_flags.no_titlebar = 0;
286 } else {
287 wwin->client_flags.no_titlebar =
288 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
292 } else {
293 /* setup the defaults */
294 wwin->client_flags.no_border = 0;
295 wwin->client_flags.no_titlebar = 0;
296 wwin->client_flags.no_closable = 0;
297 wwin->client_flags.no_miniaturizable = 0;
298 wwin->client_flags.no_resizable = 0;
299 wwin->client_flags.no_close_button = 0;
300 wwin->client_flags.no_miniaturize_button = 0;
301 wwin->client_flags.no_resizebar = 0;
303 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
304 wwin->client_flags.no_appicon = 1;
307 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
309 WScreen *scr = wwin->screen_ptr;
311 /* sets global default stuff */
312 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
314 * Decoration setting is done in this precedence (lower to higher)
315 * - use global default in the resource database
316 * - guess some settings
317 * - use GNUstep/external window attributes
318 * - set hints specified for the app in the resource DB
321 WSETUFLAG(wwin, broken_close, 0);
323 if (wwin->protocols.DELETE_WINDOW)
324 WSETUFLAG(wwin, kill_close, 0);
325 else
326 WSETUFLAG(wwin, kill_close, 1);
328 /* transients can't be iconified or maximized */
329 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
330 WSETUFLAG(wwin, no_miniaturizable, 1);
331 WSETUFLAG(wwin, no_miniaturize_button, 1);
334 /* if the window can't be resized, remove the resizebar */
335 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
336 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
337 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
338 WSETUFLAG(wwin, no_resizable, 1);
339 WSETUFLAG(wwin, no_resizebar, 1);
342 /* set GNUstep window attributes */
343 if (wwin->wm_gnustep_attr) {
344 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
346 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
348 *level = wwin->wm_gnustep_attr->window_level;
350 * INT_MIN is the only illegal window level.
352 if (*level == INT_MIN)
353 *level = INT_MIN + 1;
354 } else {
355 /* setup defaults */
356 *level = WMNormalLevel;
358 } else {
359 int tmp_workspace = -1;
360 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
361 Bool check;
363 check = False;
365 #ifdef MWM_HINTS
366 wMWMCheckClientHints(wwin);
367 #endif /* MWM_HINTS */
369 if (!check)
370 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
372 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
373 * have INT_MIN that means that no window level was requested. -Dan
375 if (tmp_level == INT_MIN) {
376 if (WFLAGP(wwin, floating))
377 *level = WMFloatingLevel;
378 else if (WFLAGP(wwin, sunken))
379 *level = WMSunkenLevel;
380 else
381 *level = WMNormalLevel;
382 } else {
383 *level = tmp_level;
386 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
387 WWindow *transientOwner = wWindowFor(wwin->transient_for);
388 if (transientOwner) {
389 int ownerLevel = transientOwner->frame->core->stacking->window_level;
390 if (ownerLevel > *level)
391 *level = ownerLevel;
395 if (tmp_workspace >= 0)
396 *workspace = tmp_workspace % scr->workspace_count;
400 * Set attributes specified only for that window/class.
401 * This might do duplicate work with the 1st wDefaultFillAttributes().
403 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
404 &wwin->user_flags, &wwin->defined_user_flags, False);
406 * Sanity checks for attributes that depend on other attributes
408 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
409 wwin->user_flags.emulate_appicon = 0;
411 if (wwin->main_window != None) {
412 WApplication *wapp = wApplicationOf(wwin->main_window);
413 if (wapp && !wapp->flags.emulated)
414 wwin->user_flags.emulate_appicon = 0;
417 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
418 wwin->user_flags.emulate_appicon = 0;
420 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
421 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
422 wwin->user_flags.sunken = 0;
424 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
426 /* windows that have takefocus=False shouldn't take focus at all */
427 if (wwin->focus_mode == WFM_NO_INPUT)
428 wwin->client_flags.no_focusable = 1;
431 Bool wWindowCanReceiveFocus(WWindow *wwin)
433 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
434 return False;
435 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
436 return False;
437 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
438 return False;
440 return True;
443 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
445 int w1, h1, w2, h2;
447 w1 = wwin->frame->core->width;
448 h1 = wwin->frame->core->height;
449 w2 = obscured->frame->core->width;
450 h2 = obscured->frame->core->height;
452 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
453 && wwin->frame->workspace != obscured->frame->workspace)
454 return False;
456 if (wwin->frame_x + w1 < obscured->frame_x
457 || wwin->frame_y + h1 < obscured->frame_y
458 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
459 return False;
461 return True;
464 static void fixLeaderProperties(WWindow *wwin)
466 XClassHint *classHint;
467 XWMHints *hints, *clientHints;
468 Window leaders[2], window;
469 char **argv, *command;
470 int argc, i, pid;
471 Bool haveCommand;
473 classHint = XAllocClassHint();
474 clientHints = XGetWMHints(dpy, wwin->client_win);
475 pid = wNETWMGetPidForWindow(wwin->client_win);
476 if (pid > 0)
477 haveCommand = GetCommandForPid(pid, &argv, &argc);
478 else
479 haveCommand = False;
481 leaders[0] = wwin->client_leader;
482 leaders[1] = wwin->group_id;
484 if (haveCommand) {
485 command = GetCommandForWindow(wwin->client_win);
486 if (command) {
487 /* command already set. nothing to do. */
488 wfree(command);
489 } else {
490 XSetCommand(dpy, wwin->client_win, argv, argc);
494 for (i = 0; i < 2; i++) {
495 window = leaders[i];
496 if (window) {
497 if (XGetClassHint(dpy, window, classHint) == 0) {
498 classHint->res_name = wwin->wm_instance;
499 classHint->res_class = wwin->wm_class;
500 XSetClassHint(dpy, window, classHint);
502 hints = XGetWMHints(dpy, window);
503 if (hints) {
504 XFree(hints);
505 } else if (clientHints) {
506 /* set window group leader to self */
507 clientHints->window_group = window;
508 clientHints->flags |= WindowGroupHint;
509 XSetWMHints(dpy, window, clientHints);
512 if (haveCommand) {
513 command = GetCommandForWindow(window);
514 if (command) {
515 /* command already set. nothing to do. */
516 wfree(command);
517 } else {
518 XSetCommand(dpy, window, argv, argc);
524 XFree(classHint);
525 if (clientHints)
526 XFree(clientHints);
527 if (haveCommand)
528 wfree(argv);
531 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
533 XClassHint *classHint;
534 XWMHints *hints;
535 Window leader;
536 int argc;
537 char **argv;
539 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
540 /* set class hint */
541 classHint = XAllocClassHint();
542 classHint->res_name = instance;
543 classHint->res_class = class;
544 XSetClassHint(dpy, leader, classHint);
545 XFree(classHint);
547 /* inherit these from the original leader if available */
548 hints = XGetWMHints(dpy, win);
549 if (!hints) {
550 hints = XAllocWMHints();
551 hints->flags = 0;
553 /* set window group leader to self */
554 hints->window_group = leader;
555 hints->flags |= WindowGroupHint;
556 XSetWMHints(dpy, leader, hints);
557 XFree(hints);
559 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
560 XSetCommand(dpy, leader, argv, argc);
561 XFreeStringList(argv);
564 return leader;
567 static int matchIdentifier(void *item, void *cdata)
569 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
573 *----------------------------------------------------------------
574 * wManageWindow--
575 * reparents the window and allocates a descriptor for it.
576 * Window manager hints and other hints are fetched to configure
577 * the window decoration attributes and others. User preferences
578 * for the window are used if available, to configure window
579 * decorations and some behaviour.
580 * If in startup, windows that are override redirect,
581 * unmapped and never were managed and are Withdrawn are not
582 * managed.
584 * Returns:
585 * the new window descriptor
587 * Side effects:
588 * The window is reparented and appropriate notification
589 * is done to the client. Input mask for the window is setup.
590 * The window descriptor is also associated with various window
591 * contexts and inserted in the head of the window list.
592 * Event handler contexts are associated for some objects
593 * (buttons, titlebar and resizebar)
595 *----------------------------------------------------------------
597 WWindow *wManageWindow(WScreen *scr, Window window)
599 WWindow *wwin;
600 int x, y;
601 unsigned width, height;
602 XWindowAttributes wattribs;
603 XSetWindowAttributes attribs;
604 WWindowState *win_state;
605 WWindow *transientOwner = NULL;
606 int window_level;
607 int wm_state;
608 int foo;
609 int workspace = -1;
610 char *title;
611 Bool withdraw = False;
612 Bool raise = False;
614 /* mutex. */
615 XGrabServer(dpy);
616 XSync(dpy, False);
617 /* make sure the window is still there */
618 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
619 XUngrabServer(dpy);
620 return NULL;
623 /* if it's an override-redirect, ignore it */
624 if (wattribs.override_redirect) {
625 XUngrabServer(dpy);
626 return NULL;
629 wm_state = PropGetWindowState(window);
631 /* if it's startup and the window is unmapped, don't manage it */
632 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
633 XUngrabServer(dpy);
634 return NULL;
637 wwin = wWindowCreate();
639 title = wNETWMGetWindowName(window);
640 if (title)
641 wwin->flags.net_has_title = 1;
642 if (!title && !wFetchName(dpy, window, &title))
643 title = NULL;
645 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
647 #ifdef DEBUG
648 printf("managing window %x\n", (unsigned)window);
649 #endif
651 #ifdef SHAPE
652 if (wShapeSupported) {
653 int junk;
654 unsigned int ujunk;
655 int b_shaped;
657 XShapeSelectInput(dpy, window, ShapeNotifyMask);
658 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
659 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
660 wwin->flags.shaped = b_shaped;
662 #endif
665 * Get hints and other information in properties
667 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
669 /* setup descriptor */
670 wwin->client_win = window;
671 wwin->screen_ptr = scr;
672 wwin->old_border_width = wattribs.border_width;
673 wwin->event_mask = CLIENT_EVENTS;
674 attribs.event_mask = CLIENT_EVENTS;
675 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
676 attribs.save_under = False;
678 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
679 XSetWindowBorderWidth(dpy, window, 0);
681 /* get hints from GNUstep app */
682 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
683 wwin->flags.is_gnustep = 1;
685 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
686 wwin->wm_gnustep_attr = NULL;
689 wwin->client_leader = PropGetClientLeader(window);
690 if (wwin->client_leader != None)
691 wwin->main_window = wwin->client_leader;
693 wwin->wm_hints = XGetWMHints(dpy, window);
695 if (wwin->wm_hints) {
696 if (wwin->wm_hints->flags & StateHint) {
698 if (wwin->wm_hints->initial_state == IconicState) {
700 wwin->flags.miniaturized = 1;
702 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
704 withdraw = True;
708 if (wwin->wm_hints->flags & WindowGroupHint) {
709 wwin->group_id = wwin->wm_hints->window_group;
710 /* window_group has priority over CLIENT_LEADER */
711 wwin->main_window = wwin->group_id;
712 } else {
713 wwin->group_id = None;
716 if (wwin->wm_hints->flags & UrgencyHint)
717 wwin->flags.urgent = 1;
718 } else {
719 wwin->group_id = None;
722 PropGetProtocols(window, &wwin->protocols);
724 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
725 wwin->transient_for = None;
726 } else {
727 if (wwin->transient_for == None || wwin->transient_for == window) {
728 wwin->transient_for = scr->root_win;
729 } else {
730 transientOwner = wWindowFor(wwin->transient_for);
731 if (transientOwner && transientOwner->main_window != None)
732 wwin->main_window = transientOwner->main_window;
736 /* guess the focus mode */
737 wwin->focus_mode = getFocusMode(wwin);
739 /* get geometry stuff */
740 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
742 /* get colormap windows */
743 GetColormapWindows(wwin);
746 * Setup the decoration/window attributes and
747 * geometry
749 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
751 /* Make broken apps behave as a nice app. */
752 if (WFLAGP(wwin, emulate_appicon))
753 wwin->main_window = wwin->client_win;
755 fixLeaderProperties(wwin);
757 wwin->orig_main_window = wwin->main_window;
759 if (wwin->flags.is_gnustep)
760 WSETUFLAG(wwin, shared_appicon, 0);
762 if (wwin->main_window) {
763 extern Atom _XA_WINDOWMAKER_MENU;
764 XTextProperty text_prop;
766 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
767 WSETUFLAG(wwin, shared_appicon, 0);
771 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
772 char *buffer, *instance, *class;
773 WFakeGroupLeader *fPtr;
774 int index;
776 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
778 /* // only enter here if PropGetWMClass() succeds */
779 PropGetWMClass(wwin->main_window, &class, &instance);
780 buffer = StrConcatDot(instance, class);
782 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
783 if (index != WANotFound) {
784 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
785 if (fPtr->retainCount == 0) {
786 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
787 instance, class);
789 fPtr->retainCount++;
790 #undef method2
791 if (fPtr->origLeader == None) {
792 #ifdef method2
793 if (ADEQUATE(wwin->group_id)) {
794 fPtr->retainCount++;
795 fPtr->origLeader = wwin->group_id;
796 } else if (ADEQUATE(wwin->client_leader)) {
797 fPtr->retainCount++;
798 fPtr->origLeader = wwin->client_leader;
799 } else if (ADEQUATE(wwin->main_window)) {
800 fPtr->retainCount++;
801 fPtr->origLeader = wwin->main_window;
803 #else
804 if (ADEQUATE(wwin->main_window)) {
805 fPtr->retainCount++;
806 fPtr->origLeader = wwin->main_window;
808 #endif
810 wwin->fake_group = fPtr;
811 /*wwin->group_id = fPtr->leader; */
812 wwin->main_window = fPtr->leader;
813 wfree(buffer);
814 } else {
815 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
817 fPtr->identifier = buffer;
818 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
819 fPtr->origLeader = None;
820 fPtr->retainCount = 1;
822 WMAddToArray(scr->fakeGroupLeaders, fPtr);
824 #ifdef method2
825 if (ADEQUATE(wwin->group_id)) {
826 fPtr->retainCount++;
827 fPtr->origLeader = wwin->group_id;
828 } else if (ADEQUATE(wwin->client_leader)) {
829 fPtr->retainCount++;
830 fPtr->origLeader = wwin->client_leader;
831 } else if (ADEQUATE(wwin->main_window)) {
832 fPtr->retainCount++;
833 fPtr->origLeader = wwin->main_window;
835 #else
836 if (ADEQUATE(wwin->main_window)) {
837 fPtr->retainCount++;
838 fPtr->origLeader = wwin->main_window;
840 #endif
841 wwin->fake_group = fPtr;
842 /*wwin->group_id = fPtr->leader; */
843 wwin->main_window = fPtr->leader;
845 if (instance)
846 XFree(instance);
847 if (class)
848 XFree(class);
850 #undef method2
851 #undef ADEQUATE
855 * Setup the initial state of the window
857 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
858 wwin->flags.miniaturized = 1;
860 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
861 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
863 wNETWMCheckInitialClientState(wwin);
865 /* apply previous state if it exists and we're in startup */
866 if (scr->flags.startup && wm_state >= 0) {
868 if (wm_state == IconicState) {
870 wwin->flags.miniaturized = 1;
872 } else if (wm_state == WithdrawnState) {
874 withdraw = True;
878 /* if there is a saved state (from file), restore it */
879 win_state = NULL;
880 if (wwin->main_window != None)
881 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
882 else
883 win_state = (WWindowState *) wWindowGetSavedState(window);
885 if (win_state && !withdraw) {
886 if (win_state->state->hidden > 0)
887 wwin->flags.hidden = win_state->state->hidden;
889 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
890 wwin->flags.shaded = win_state->state->shaded;
892 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
893 wwin->flags.miniaturized = win_state->state->miniaturized;
896 if (!IS_OMNIPRESENT(wwin)) {
897 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
898 wwin->wm_class);
899 if (w < 0 || w >= scr->workspace_count) {
900 workspace = win_state->state->workspace;
901 if (workspace >= scr->workspace_count)
902 workspace = scr->current_workspace;
903 } else {
904 workspace = w;
906 } else {
907 workspace = scr->current_workspace;
911 /* if we're restarting, restore saved state (from hints).
912 * This will overwrite previous */
914 WSavedState *wstate;
916 if (getSavedState(window, &wstate)) {
917 wwin->flags.shaded = wstate->shaded;
918 wwin->flags.hidden = wstate->hidden;
919 wwin->flags.miniaturized = wstate->miniaturized;
920 wwin->flags.maximized = wstate->maximized;
921 if (wwin->flags.maximized) {
922 wwin->old_geometry.x = wstate->x;
923 wwin->old_geometry.y = wstate->y;
924 wwin->old_geometry.width = wstate->w;
925 wwin->old_geometry.height = wstate->h;
928 workspace = wstate->workspace;
929 } else {
930 wstate = NULL;
933 /* restore window shortcut */
934 if (wstate != NULL || win_state != NULL) {
935 unsigned mask = 0;
937 if (win_state != NULL)
938 mask = win_state->state->window_shortcuts;
940 if (wstate != NULL && mask == 0)
941 mask = wstate->window_shortcuts;
943 if (mask > 0) {
944 int i;
946 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
947 if (mask & (1 << i)) {
948 if (!scr->shortcutWindows[i])
949 scr->shortcutWindows[i] = WMCreateArray(4);
951 WMAddToArray(scr->shortcutWindows[i], wwin);
956 if (wstate != NULL)
957 wfree(wstate);
960 /* don't let transients start miniaturized if their owners are not */
961 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
962 wwin->flags.miniaturized = 0;
963 if (wwin->wm_hints)
964 wwin->wm_hints->initial_state = NormalState;
967 /* set workspace on which the window starts */
968 if (workspace >= 0) {
969 if (workspace > scr->workspace_count - 1) {
970 workspace = workspace % scr->workspace_count;
972 } else {
973 int w;
975 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
977 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
979 workspace = w;
981 } else {
982 if (wPreferences.open_transients_with_parent && transientOwner) {
984 workspace = transientOwner->frame->workspace;
986 } else {
988 workspace = scr->current_workspace;
993 /* setup window geometry */
994 if (win_state && win_state->state->w > 0) {
995 width = win_state->state->w;
996 height = win_state->state->h;
998 wWindowConstrainSize(wwin, &width, &height);
1000 /* do not ask for window placement if the window is
1001 * transient, during startup, if the initial workspace is another one
1002 * or if the window wants to start iconic.
1003 * If geometry was saved, restore it. */
1005 Bool dontBring = False;
1007 if (win_state && win_state->state->w > 0) {
1008 x = win_state->state->x;
1009 y = win_state->state->y;
1010 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1011 && !scr->flags.startup
1012 && workspace == scr->current_workspace
1013 && !wwin->flags.miniaturized
1014 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1016 if (transientOwner && transientOwner->flags.mapped) {
1017 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1018 WMRect rect;
1019 int head;
1021 x = transientOwner->frame_x +
1022 abs((transientOwner->frame->core->width - width) / 2) + offs;
1023 y = transientOwner->frame_y +
1024 abs((transientOwner->frame->core->height - height) / 3) + offs;
1027 * limit transient windows to be inside their parent's head
1029 rect.pos.x = transientOwner->frame_x;
1030 rect.pos.y = transientOwner->frame_y;
1031 rect.size.width = transientOwner->frame->core->width;
1032 rect.size.height = transientOwner->frame->core->height;
1034 head = wGetHeadForRect(scr, rect);
1035 rect = wGetRectForHead(scr, head);
1037 if (x < rect.pos.x)
1038 x = rect.pos.x;
1039 else if (x + width > rect.pos.x + rect.size.width)
1040 x = rect.pos.x + rect.size.width - width;
1042 if (y < rect.pos.y)
1043 y = rect.pos.y;
1044 else if (y + height > rect.pos.y + rect.size.height)
1045 y = rect.pos.y + rect.size.height - height;
1047 } else {
1048 PlaceWindow(wwin, &x, &y, width, height);
1050 if (wPreferences.window_placement == WPM_MANUAL) {
1051 dontBring = True;
1053 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1054 int head, flags;
1055 WMRect rect;
1056 int reposition = 0;
1059 * Make spash screens come out in the center of a head
1060 * trouble is that most splashies never get here
1061 * they are managed trough atoms but god knows where.
1062 * Dan, do you know ? -peter
1064 * Most of them are not managed, they have set
1065 * OverrideRedirect, which means we can't do anything about
1066 * them. -alfredo
1068 #if 0
1069 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width) / 2);
1070 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height) / 2);
1072 if ((unsigned)(x + (width - scr->scr_width) / 2 + 10) < 20 &&
1073 (unsigned)(y + (height - scr->scr_height) / 2 + 10) < 20) {
1075 reposition = 1;
1077 } else
1078 #endif
1081 * xinerama checks for: across head and dead space
1083 rect.pos.x = x;
1084 rect.pos.y = y;
1085 rect.size.width = width;
1086 rect.size.height = height;
1088 head = wGetRectPlacementInfo(scr, rect, &flags);
1090 if (flags & XFLAG_DEAD)
1091 reposition = 1;
1093 if (flags & XFLAG_MULTIPLE)
1094 reposition = 2;
1097 switch (reposition) {
1098 case 1:
1099 head = wGetHeadForPointerLocation(scr);
1100 rect = wGetRectForHead(scr, head);
1102 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1103 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1104 break;
1106 case 2:
1107 rect = wGetRectForHead(scr, head);
1109 if (x < rect.pos.x)
1110 x = rect.pos.x;
1111 else if (x + width > rect.pos.x + rect.size.width)
1112 x = rect.pos.x + rect.size.width - width;
1114 if (y < rect.pos.y)
1115 y = rect.pos.y;
1116 else if (y + height > rect.pos.y + rect.size.height)
1117 y = rect.pos.y + rect.size.height - height;
1119 break;
1121 default:
1122 break;
1126 if (WFLAGP(wwin, dont_move_off) && dontBring)
1127 wScreenBringInside(scr, &x, &y, width, height);
1130 wNETWMPositionSplash(wwin, &x, &y, width, height);
1132 if (wwin->flags.urgent) {
1133 if (!IS_OMNIPRESENT(wwin))
1134 wwin->flags.omnipresent ^= 1;
1138 * Create frame, borders and do reparenting
1140 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1141 #ifdef XKB_BUTTON_HINT
1142 if (wPreferences.modelock)
1143 foo |= WFF_LANGUAGE_BUTTON;
1144 #endif
1145 if (HAS_TITLEBAR(wwin))
1146 foo |= WFF_TITLEBAR;
1147 if (HAS_RESIZEBAR(wwin))
1148 foo |= WFF_RESIZEBAR;
1149 if (HAS_BORDER(wwin))
1150 foo |= WFF_BORDER;
1152 wwin->frame = wFrameWindowCreate(scr, window_level,
1153 x, y, width, height,
1154 &wPreferences.window_title_clearance, foo,
1155 scr->window_title_texture,
1156 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1158 wwin->frame->flags.is_client_window_frame = 1;
1159 wwin->frame->flags.justification = wPreferences.title_justification;
1161 /* setup button images */
1162 wWindowUpdateButtonImages(wwin);
1164 /* hide unused buttons */
1165 foo = 0;
1166 if (WFLAGP(wwin, no_close_button))
1167 foo |= WFF_RIGHT_BUTTON;
1168 if (WFLAGP(wwin, no_miniaturize_button))
1169 foo |= WFF_LEFT_BUTTON;
1170 #ifdef XKB_BUTTON_HINT
1171 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1172 foo |= WFF_LANGUAGE_BUTTON;
1173 #endif
1174 if (foo != 0)
1175 wFrameWindowHideButton(wwin->frame, foo);
1177 wwin->frame->child = wwin;
1178 wwin->frame->workspace = workspace;
1179 wwin->frame->on_click_left = windowIconifyClick;
1181 #ifdef XKB_BUTTON_HINT
1182 if (wPreferences.modelock)
1183 wwin->frame->on_click_language = windowLanguageClick;
1184 #endif
1186 wwin->frame->on_click_right = windowCloseClick;
1187 wwin->frame->on_dblclick_right = windowCloseDblClick;
1188 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1189 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1190 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1192 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1193 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1194 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1197 int gx, gy;
1199 wClientGetGravityOffsets(wwin, &gx, &gy);
1201 /* if gravity is to the south, account for the border sizes */
1202 if (gy > 0)
1203 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1207 * wWindowConfigure() will init the client window's size
1208 * (wwin->client.{width,height}) and all other geometry
1209 * related variables (frame_x,frame_y)
1211 wWindowConfigure(wwin, x, y, width, height);
1213 /* to make sure the window receives it's new position after reparenting */
1214 wWindowSynthConfigureNotify(wwin);
1217 * Setup descriptors and save window to internal
1218 * lists
1220 if (wwin->main_window != None) {
1221 WApplication *app;
1222 WWindow *leader;
1224 /* Leader windows do not necessary set themselves as leaders.
1225 * If this is the case, point the leader of this window to
1226 * itself */
1227 leader = wWindowFor(wwin->main_window);
1228 if (leader && leader->main_window == None) {
1229 leader->main_window = leader->client_win;
1231 app = wApplicationCreate(wwin);
1232 if (app) {
1233 app->last_workspace = workspace;
1236 * Do application specific stuff, like setting application
1237 * wide attributes.
1240 if (wwin->flags.hidden) {
1241 /* if the window was set to hidden because it was hidden
1242 * in a previous incarnation and that state was restored */
1243 app->flags.hidden = 1;
1244 } else if (app->flags.hidden) {
1245 if (WFLAGP(app->main_window_desc, start_hidden)) {
1246 wwin->flags.hidden = 1;
1247 } else {
1248 wUnhideApplication(app, False, False);
1249 raise = True;
1255 /* setup the frame descriptor */
1256 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1257 wwin->frame->core->descriptor.parent = wwin;
1258 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1260 /* don't let windows go away if we die */
1261 XAddToSaveSet(dpy, window);
1263 XLowerWindow(dpy, window);
1265 /* if window is in this workspace and should be mapped, then map it */
1266 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1267 && !wwin->flags.hidden && !withdraw) {
1269 /* The following "if" is to avoid crashing of clients that expect
1270 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1271 * after the return from this function.
1273 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1274 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1275 } else {
1276 wClientSetState(wwin, NormalState, None);
1279 #if 0
1280 /* if not auto focus, then map the window under the currently
1281 * focused window */
1282 #define _WIDTH(w) (w)->frame->core->width
1283 #define _HEIGHT(w) (w)->frame->core->height
1284 if (!wPreferences.auto_focus && scr->focused_window
1285 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1286 && (_WIDTH(wwin) >
1287 (_WIDTH(scr->focused_window) * 5) / 3
1288 || _HEIGHT(wwin) >
1289 (_HEIGHT(scr->focused_window) * 5) / 3)
1290 && WINDOW_LEVEL(scr->focused_window) ==
1291 WINDOW_LEVEL(wwin))
1292 || wwin->flags.maximized)) {
1293 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1295 #undef _WIDTH
1296 #undef _HEIGHT
1298 #endif
1300 if (wPreferences.superfluous && !wPreferences.no_animations
1301 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1303 * The brain damaged idiotic non-click to focus modes will
1304 * have trouble with this because:
1306 * 1. window is created and mapped by the client
1307 * 2. window is mapped by wmaker in small size
1308 * 3. window is animated to grow to normal size
1309 * 4. this function returns to normal event loop
1310 * 5. eventually, the EnterNotify event that would trigger
1311 * the window focusing (if the mouse is over that window)
1312 * will be processed by wmaker.
1313 * But since this event will be rather delayed
1314 * (step 3 has a large delay) the time when the event ocurred
1315 * and when it is processed, the client that owns that window
1316 * will reject the XSetInputFocus() for it.
1318 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1319 DoWindowBirth(wwin);
1322 wWindowMap(wwin);
1325 /* setup stacking descriptor */
1326 if (transientOwner) {
1327 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1328 } else {
1329 wwin->frame->core->stacking->child_of = NULL;
1332 if (!scr->focused_window) {
1333 /* first window on the list */
1334 wwin->next = NULL;
1335 wwin->prev = NULL;
1336 scr->focused_window = wwin;
1337 } else {
1338 WWindow *tmp;
1340 /* add window at beginning of focus window list */
1341 tmp = scr->focused_window;
1342 while (tmp->prev)
1343 tmp = tmp->prev;
1344 tmp->prev = wwin;
1345 wwin->next = tmp;
1346 wwin->prev = NULL;
1349 /* raise is set to true if we un-hid the app when this window was born.
1350 * we raise, else old windows of this app will be above this new one. */
1351 if (raise) {
1352 wRaiseFrame(wwin->frame->core);
1355 /* Update name must come after WApplication stuff is done */
1356 wWindowUpdateName(wwin, title);
1357 if (title)
1358 XFree(title);
1360 XUngrabServer(dpy);
1363 * Final preparations before window is ready to go
1365 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1367 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1368 if (((transientOwner && transientOwner->flags.focused)
1369 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable)) {
1371 /* only auto_focus if on same screen as mouse
1372 * (and same head for xinerama mode)
1373 * TODO: make it an option */
1375 /*TODO add checking the head of the window, is it available? */
1376 short same_screen = 0, same_head = 1;
1378 int foo;
1379 unsigned int bar;
1380 Window dummy;
1382 if (XQueryPointer(dpy, scr->root_win, &dummy, &dummy,
1383 &foo, &foo, &foo, &foo, &bar) != False) {
1384 same_screen = 1;
1387 if (same_screen == 1 && same_head == 1) {
1388 wSetFocusTo(scr, wwin);
1392 wWindowResetMouseGrabs(wwin);
1394 if (!WFLAGP(wwin, no_bind_keys))
1395 wWindowSetKeyGrabs(wwin);
1397 WMPostNotificationName(WMNManaged, wwin, NULL);
1398 wColormapInstallForWindow(scr, scr->cmap_window);
1400 /* Setup Notification Observers */
1401 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1403 /* Cleanup temporary stuff */
1404 if (win_state)
1405 wWindowDeleteSavedState(win_state);
1407 /* If the window must be withdrawed, then do it now.
1408 * Must do some optimization, 'though */
1409 if (withdraw) {
1410 wwin->flags.mapped = 0;
1411 wClientSetState(wwin, WithdrawnState, None);
1412 wUnmanageWindow(wwin, True, False);
1413 wwin = NULL;
1416 return wwin;
1419 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1420 char *title, int x, int y, int width, int height)
1422 WWindow *wwin;
1423 int foo;
1425 wwin = wWindowCreate();
1427 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1429 wwin->flags.internal_window = 1;
1431 WSETUFLAG(wwin, omnipresent, 1);
1432 WSETUFLAG(wwin, no_shadeable, 1);
1433 WSETUFLAG(wwin, no_resizable, 1);
1434 WSETUFLAG(wwin, no_miniaturizable, 1);
1436 wwin->focus_mode = WFM_PASSIVE;
1437 wwin->client_win = window;
1438 wwin->screen_ptr = scr;
1439 wwin->transient_for = owner;
1440 wwin->client.x = x;
1441 wwin->client.y = y;
1442 wwin->client.width = width;
1443 wwin->client.height = height;
1444 wwin->frame_x = wwin->client.x;
1445 wwin->frame_y = wwin->client.y;
1447 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1448 foo |= WFF_TITLEBAR;
1449 #ifdef XKB_BUTTON_HINT
1450 foo |= WFF_LANGUAGE_BUTTON;
1451 #endif
1453 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1454 wwin->frame_x, wwin->frame_y,
1455 width, height,
1456 &wPreferences.window_title_clearance, foo,
1457 scr->window_title_texture,
1458 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1460 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1462 wwin->frame->flags.is_client_window_frame = 1;
1463 wwin->frame->flags.justification = wPreferences.title_justification;
1465 wFrameWindowChangeTitle(wwin->frame, title);
1467 /* setup button images */
1468 wWindowUpdateButtonImages(wwin);
1470 /* hide buttons */
1471 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1473 wwin->frame->child = wwin;
1474 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1476 #ifdef XKB_BUTTON_HINT
1477 if (wPreferences.modelock)
1478 wwin->frame->on_click_language = windowLanguageClick;
1479 #endif
1481 wwin->frame->on_click_right = windowCloseClick;
1482 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1483 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1484 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1485 wwin->client.y += wwin->frame->top_width;
1487 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1488 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1490 /* setup the frame descriptor */
1491 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1492 wwin->frame->core->descriptor.parent = wwin;
1493 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1495 XLowerWindow(dpy, window);
1496 XMapSubwindows(dpy, wwin->frame->core->window);
1498 /* setup stacking descriptor */
1499 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1500 WWindow *tmp;
1501 tmp = wWindowFor(wwin->transient_for);
1502 if (tmp)
1503 wwin->frame->core->stacking->child_of = tmp->frame->core;
1504 } else {
1505 wwin->frame->core->stacking->child_of = NULL;
1508 if (!scr->focused_window) {
1509 /* first window on the list */
1510 wwin->next = NULL;
1511 wwin->prev = NULL;
1512 scr->focused_window = wwin;
1513 } else {
1514 WWindow *tmp;
1516 /* add window at beginning of focus window list */
1517 tmp = scr->focused_window;
1518 while (tmp->prev)
1519 tmp = tmp->prev;
1520 tmp->prev = wwin;
1521 wwin->next = tmp;
1522 wwin->prev = NULL;
1525 if (wwin->flags.is_gnustep == 0)
1526 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1528 /* if (wPreferences.auto_focus) */
1529 wSetFocusTo(scr, wwin);
1530 wWindowResetMouseGrabs(wwin);
1531 wWindowSetKeyGrabs(wwin);
1533 return wwin;
1537 *----------------------------------------------------------------------
1538 * wUnmanageWindow--
1539 * Removes the frame window from a window and destroys all data
1540 * related to it. The window will be reparented back to the root window
1541 * if restore is True.
1543 * Side effects:
1544 * Everything related to the window is destroyed and the window
1545 * is removed from the window lists. Focus is set to the previous on the
1546 * window list.
1547 *----------------------------------------------------------------------
1549 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1551 WCoreWindow *frame = wwin->frame->core;
1552 WWindow *owner = NULL;
1553 WWindow *newFocusedWindow = NULL;
1554 int wasFocused;
1555 WScreen *scr = wwin->screen_ptr;
1557 /* First close attribute editor window if open */
1558 if (wwin->flags.inspector_open) {
1559 wCloseInspectorForWindow(wwin);
1562 /* Close window menu if it's open for this window */
1563 if (wwin->flags.menu_open_for_me) {
1564 CloseWindowMenu(scr);
1567 if (!destroyed) {
1568 if (!wwin->flags.internal_window)
1569 XRemoveFromSaveSet(dpy, wwin->client_win);
1571 XSelectInput(dpy, wwin->client_win, NoEventMask);
1573 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1574 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1577 XUnmapWindow(dpy, frame->window);
1579 XUnmapWindow(dpy, wwin->client_win);
1581 /* deselect window */
1582 wSelectWindow(wwin, False);
1584 /* remove all pending events on window */
1585 /* I think this only matters for autoraise */
1586 if (wPreferences.raise_delay)
1587 WMDeleteTimerWithClientData(wwin->frame->core);
1589 XFlush(dpy);
1591 /* reparent the window back to the root */
1592 if (restore)
1593 wClientRestore(wwin);
1595 if (wwin->transient_for != scr->root_win) {
1596 owner = wWindowFor(wwin->transient_for);
1597 if (owner) {
1598 if (!owner->flags.semi_focused) {
1599 owner = NULL;
1600 } else {
1601 owner->flags.semi_focused = 0;
1606 wasFocused = wwin->flags.focused;
1608 /* remove from window focus list */
1609 if (!wwin->prev && !wwin->next) {
1610 /* was the only window */
1611 scr->focused_window = NULL;
1612 newFocusedWindow = NULL;
1613 } else {
1614 WWindow *tmp;
1616 if (wwin->prev)
1617 wwin->prev->next = wwin->next;
1618 if (wwin->next)
1619 wwin->next->prev = wwin->prev;
1620 else {
1621 scr->focused_window = wwin->prev;
1622 scr->focused_window->next = NULL;
1625 if (wPreferences.focus_mode == WKF_CLICK) {
1627 /* if in click to focus mode and the window
1628 * was a transient, focus the owner window
1630 tmp = NULL;
1631 if (wPreferences.focus_mode == WKF_CLICK) {
1632 tmp = wWindowFor(wwin->transient_for);
1633 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1634 tmp = NULL;
1637 /* otherwise, focus the next one in the focus list */
1638 if (!tmp) {
1639 tmp = scr->focused_window;
1640 while (tmp) { /* look for one in the window list first */
1641 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1642 && (tmp->flags.mapped || tmp->flags.shaded))
1643 break;
1644 tmp = tmp->prev;
1646 if (!tmp) { /* if unsuccessful, choose any focusable window */
1647 tmp = scr->focused_window;
1648 while (tmp) {
1649 if (!WFLAGP(tmp, no_focusable)
1650 && (tmp->flags.mapped || tmp->flags.shaded))
1651 break;
1652 tmp = tmp->prev;
1657 newFocusedWindow = tmp;
1659 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1660 unsigned int mask;
1661 int foo;
1662 Window bar, win;
1664 /* This is to let the root window get the keyboard input
1665 * if Sloppy focus mode and no other window get focus.
1666 * This way keybindings will not freeze.
1668 tmp = NULL;
1669 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1670 tmp = wWindowFor(win);
1671 if (tmp == wwin)
1672 tmp = NULL;
1673 newFocusedWindow = tmp;
1674 } else {
1675 newFocusedWindow = NULL;
1679 if (!wwin->flags.internal_window)
1680 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1681 #ifdef DEBUG
1682 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win, (unsigned)frame->window);
1683 #endif
1685 if (wasFocused) {
1686 if (newFocusedWindow != owner && owner) {
1687 if (wwin->flags.is_gnustep == 0)
1688 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1690 wSetFocusTo(scr, newFocusedWindow);
1692 wWindowDestroy(wwin);
1693 XFlush(dpy);
1696 void wWindowMap(WWindow *wwin)
1698 XMapWindow(dpy, wwin->frame->core->window);
1699 if (!wwin->flags.shaded) {
1700 /* window will be remapped when getting MapNotify */
1701 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1702 XMapWindow(dpy, wwin->client_win);
1703 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1705 wwin->flags.mapped = 1;
1709 void wWindowUnmap(WWindow *wwin)
1711 wwin->flags.mapped = 0;
1713 /* prevent window withdrawal when getting UnmapNotify */
1714 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1715 XUnmapWindow(dpy, wwin->client_win);
1716 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1718 XUnmapWindow(dpy, wwin->frame->core->window);
1721 void wWindowFocus(WWindow *wwin, WWindow *owin)
1723 WWindow *nowner;
1724 WWindow *oowner;
1726 #ifdef KEEP_XKB_LOCK_STATUS
1727 if (wPreferences.modelock) {
1728 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1730 #endif /* KEEP_XKB_LOCK_STATUS */
1732 wwin->flags.semi_focused = 0;
1734 if (wwin->flags.is_gnustep == 0)
1735 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1737 wwin->flags.focused = 1;
1739 wWindowResetMouseGrabs(wwin);
1741 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1743 if (owin == wwin || !owin)
1744 return;
1746 nowner = wWindowFor(wwin->transient_for);
1748 /* new window is a transient for the old window */
1749 if (nowner == owin) {
1750 owin->flags.semi_focused = 1;
1751 wWindowUnfocus(nowner);
1752 return;
1755 oowner = wWindowFor(owin->transient_for);
1757 /* new window is owner of old window */
1758 if (wwin == oowner) {
1759 wWindowUnfocus(owin);
1760 return;
1763 if (!nowner) {
1764 wWindowUnfocus(owin);
1765 return;
1768 /* new window has same owner of old window */
1769 if (oowner == nowner) {
1770 /* prevent unfocusing of owner */
1771 oowner->flags.semi_focused = 0;
1772 wWindowUnfocus(owin);
1773 oowner->flags.semi_focused = 1;
1775 return;
1778 /* nowner != NULL && oowner != nowner */
1779 nowner->flags.semi_focused = 1;
1780 wWindowUnfocus(nowner);
1781 wWindowUnfocus(owin);
1784 void wWindowUnfocus(WWindow *wwin)
1786 CloseWindowMenu(wwin->screen_ptr);
1788 if (wwin->flags.is_gnustep == 0)
1789 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1791 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1792 WWindow *owner;
1793 owner = wWindowFor(wwin->transient_for);
1794 if (owner && owner->flags.semi_focused) {
1795 owner->flags.semi_focused = 0;
1796 if (owner->flags.mapped || owner->flags.shaded) {
1797 wWindowUnfocus(owner);
1798 wFrameWindowPaint(owner->frame);
1802 wwin->flags.focused = 0;
1803 wWindowResetMouseGrabs(wwin);
1804 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1807 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1809 char *title;
1811 if (!wwin->frame)
1812 return;
1814 wwin->flags.wm_name_changed = 1;
1816 if (!newTitle) {
1817 /* the hint was removed */
1818 title = DEF_WINDOW_TITLE;
1819 } else {
1820 title = newTitle;
1823 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1824 WMPostNotificationName(WMNChangedName, wwin, NULL);
1829 *----------------------------------------------------------------------
1831 * wWindowConstrainSize--
1832 * Constrains size for the client window, taking the maximal size,
1833 * window resize increments and other size hints into account.
1835 * Returns:
1836 * The closest size to what was given that the client window can
1837 * have.
1839 *----------------------------------------------------------------------
1841 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1843 int width = (int)*nwidth;
1844 int height = (int)*nheight;
1845 int winc = 1;
1846 int hinc = 1;
1847 int minW = 1, minH = 1;
1848 int maxW = wwin->screen_ptr->scr_width * 2;
1849 int maxH = wwin->screen_ptr->scr_height * 2;
1850 int minAX = -1, minAY = -1;
1851 int maxAX = -1, maxAY = -1;
1852 int baseW = 0;
1853 int baseH = 0;
1855 if (wwin->normal_hints) {
1856 winc = wwin->normal_hints->width_inc;
1857 hinc = wwin->normal_hints->height_inc;
1858 minW = wwin->normal_hints->min_width;
1859 minH = wwin->normal_hints->min_height;
1860 maxW = wwin->normal_hints->max_width;
1861 maxH = wwin->normal_hints->max_height;
1862 if (wwin->normal_hints->flags & PAspect) {
1863 minAX = wwin->normal_hints->min_aspect.x;
1864 minAY = wwin->normal_hints->min_aspect.y;
1865 maxAX = wwin->normal_hints->max_aspect.x;
1866 maxAY = wwin->normal_hints->max_aspect.y;
1869 baseW = wwin->normal_hints->base_width;
1870 baseH = wwin->normal_hints->base_height;
1873 if (width < minW)
1874 width = minW;
1875 if (height < minH)
1876 height = minH;
1878 if (width > maxW)
1879 width = maxW;
1880 if (height > maxH)
1881 height = maxH;
1883 /* aspect ratio code borrowed from olwm */
1884 if (minAX > 0) {
1885 /* adjust max aspect ratio */
1886 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1887 if (maxAX > maxAY) {
1888 height = (width * maxAY) / maxAX;
1889 if (height > maxH) {
1890 height = maxH;
1891 width = (height * maxAX) / maxAY;
1893 } else {
1894 width = (height * maxAX) / maxAY;
1895 if (width > maxW) {
1896 width = maxW;
1897 height = (width * maxAY) / maxAX;
1902 /* adjust min aspect ratio */
1903 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1904 if (minAX > minAY) {
1905 height = (width * minAY) / minAX;
1906 if (height < minH) {
1907 height = minH;
1908 width = (height * minAX) / minAY;
1910 } else {
1911 width = (height * minAX) / minAY;
1912 if (width < minW) {
1913 width = minW;
1914 height = (width * minAY) / minAX;
1920 if (baseW != 0)
1921 width = (((width - baseW) / winc) * winc) + baseW;
1922 else
1923 width = (((width - minW) / winc) * winc) + minW;
1925 if (baseH != 0)
1926 height = (((height - baseH) / hinc) * hinc) + baseH;
1927 else
1928 height = (((height - minH) / hinc) * hinc) + minH;
1930 /* broken stupid apps may cause preposterous values for these.. */
1931 if (width > 0)
1932 *nwidth = width;
1933 if (height > 0)
1934 *nheight = height;
1937 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1938 unsigned int *width, unsigned int *height)
1940 int baseW = 0, baseH = 0;
1941 int winc = 1, hinc = 1;
1943 if (wwin->normal_hints) {
1944 baseW = wwin->normal_hints->base_width;
1945 baseH = wwin->normal_hints->base_height;
1947 winc = wwin->normal_hints->width_inc;
1948 hinc = wwin->normal_hints->height_inc;
1951 if (*width > maxW)
1952 *width = maxW - (maxW - baseW) % winc;
1954 if (*height > maxH)
1955 *height = maxH - (maxH - baseH) % hinc;
1958 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1960 WScreen *scr = wwin->screen_ptr;
1961 WApplication *wapp;
1962 int unmap = 0;
1964 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1965 return;
1967 if (workspace != scr->current_workspace) {
1968 /* Sent to other workspace. Unmap window */
1969 if ((wwin->flags.mapped
1970 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1971 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1973 wapp = wApplicationOf(wwin->main_window);
1974 if (wapp) {
1975 wapp->last_workspace = workspace;
1977 if (wwin->flags.miniaturized) {
1978 if (wwin->icon) {
1979 XUnmapWindow(dpy, wwin->icon->core->window);
1980 wwin->icon->mapped = 0;
1982 } else {
1983 unmap = 1;
1984 wSetFocusTo(scr, NULL);
1987 } else {
1988 /* brought to current workspace. Map window */
1989 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1990 if (wwin->icon) {
1991 XMapWindow(dpy, wwin->icon->core->window);
1992 wwin->icon->mapped = 1;
1994 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1995 wWindowMap(wwin);
1998 if (!IS_OMNIPRESENT(wwin)) {
1999 int oldWorkspace = wwin->frame->workspace;
2001 wwin->frame->workspace = workspace;
2003 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
2006 if (unmap)
2007 wWindowUnmap(wwin);
2010 void wWindowSynthConfigureNotify(WWindow *wwin)
2012 XEvent sevent;
2014 sevent.type = ConfigureNotify;
2015 sevent.xconfigure.display = dpy;
2016 sevent.xconfigure.event = wwin->client_win;
2017 sevent.xconfigure.window = wwin->client_win;
2019 sevent.xconfigure.x = wwin->client.x;
2020 sevent.xconfigure.y = wwin->client.y;
2021 sevent.xconfigure.width = wwin->client.width;
2022 sevent.xconfigure.height = wwin->client.height;
2024 sevent.xconfigure.border_width = wwin->old_border_width;
2025 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2026 sevent.xconfigure.above = wwin->frame->titlebar->window;
2027 else
2028 sevent.xconfigure.above = None;
2030 sevent.xconfigure.override_redirect = False;
2031 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2032 XFlush(dpy);
2036 *----------------------------------------------------------------------
2037 * wWindowConfigure()
2039 * req_x, req_y: new requested positions for the frame
2040 * req_width, req_height: new requested sizes for the client
2042 * Configures the frame, decorations and client window to the specified
2043 * geometry, whose validity is not checked -- wWindowConstrainSize()
2044 * must be used for that.
2045 * The size parameters are for the client window, but the position is
2046 * for the frame.
2047 * The client window receives a ConfigureNotify event, according
2048 * to what ICCCM says.
2050 * Returns:
2051 * None
2053 * Side effects:
2054 * Window size and position are changed and client window receives
2055 * a ConfigureNotify event.
2056 *----------------------------------------------------------------------
2058 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2060 int synth_notify = False;
2061 int resize;
2063 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2065 * if the window is being moved but not resized then
2066 * send a synthetic ConfigureNotify
2068 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2069 synth_notify = True;
2072 if (WFLAGP(wwin, dont_move_off))
2073 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2074 if (resize) {
2075 if (req_width < MIN_WINDOW_SIZE)
2076 req_width = MIN_WINDOW_SIZE;
2077 if (req_height < MIN_WINDOW_SIZE)
2078 req_height = MIN_WINDOW_SIZE;
2080 /* If growing, resize inner part before frame,
2081 * if shrinking, resize frame before.
2082 * This will prevent the frame (that can have a different color)
2083 * to be exposed, causing flicker */
2084 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2085 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2087 if (wwin->flags.shaded) {
2088 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2089 wwin->old_geometry.height = req_height;
2090 } else {
2091 int h;
2093 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2095 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2098 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2099 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2101 wwin->client.x = req_x;
2102 wwin->client.y = req_y + wwin->frame->top_width;
2103 wwin->client.width = req_width;
2104 wwin->client.height = req_height;
2105 } else {
2106 wwin->client.x = req_x;
2107 wwin->client.y = req_y + wwin->frame->top_width;
2109 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2111 wwin->frame_x = req_x;
2112 wwin->frame_y = req_y;
2113 if (HAS_BORDER(wwin)) {
2114 wwin->client.x += FRAME_BORDER_WIDTH;
2115 wwin->client.y += FRAME_BORDER_WIDTH;
2117 #ifdef SHAPE
2118 if (wShapeSupported && wwin->flags.shaped && resize) {
2119 wWindowSetShape(wwin);
2121 #endif
2123 if (synth_notify)
2124 wWindowSynthConfigureNotify(wwin);
2125 XFlush(dpy);
2128 /* req_x, req_y: new position of the frame */
2129 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2131 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2132 int synth_notify = False;
2134 /* Send a synthetic ConfigureNotify event for every window movement. */
2135 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2136 synth_notify = True;
2138 #else
2139 /* A single synthetic ConfigureNotify event is sent at the end of
2140 * a completed (opaque) movement in moveres.c */
2141 #endif
2143 if (WFLAGP(wwin, dont_move_off))
2144 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2145 wwin->frame->core->width, wwin->frame->core->height);
2147 wwin->client.x = req_x;
2148 wwin->client.y = req_y + wwin->frame->top_width;
2149 if (HAS_BORDER(wwin)) {
2150 wwin->client.x += FRAME_BORDER_WIDTH;
2151 wwin->client.y += FRAME_BORDER_WIDTH;
2154 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2156 wwin->frame_x = req_x;
2157 wwin->frame_y = req_y;
2159 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2160 if (synth_notify)
2161 wWindowSynthConfigureNotify(wwin);
2162 #endif
2165 void wWindowUpdateButtonImages(WWindow *wwin)
2167 WScreen *scr = wwin->screen_ptr;
2168 Pixmap pixmap, mask;
2169 WFrameWindow *fwin = wwin->frame;
2171 if (!HAS_TITLEBAR(wwin))
2172 return;
2174 /* miniaturize button */
2175 if (!WFLAGP(wwin, no_miniaturize_button)) {
2176 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2177 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2179 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2180 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2181 } else {
2182 mask = None;
2185 if (fwin->lbutton_image
2186 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2187 wPixmapDestroy(fwin->lbutton_image);
2188 fwin->lbutton_image = NULL;
2191 if (!fwin->lbutton_image) {
2192 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2193 fwin->lbutton_image->client_owned = 1;
2194 fwin->lbutton_image->client_owned_mask = 1;
2196 } else {
2197 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2198 wPixmapDestroy(fwin->lbutton_image);
2200 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2203 #ifdef XKB_BUTTON_HINT
2204 if (!WFLAGP(wwin, no_language_button)) {
2205 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2206 wPixmapDestroy(fwin->languagebutton_image);
2208 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2210 #endif
2212 /* close button */
2214 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2215 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2217 if (!WFLAGP(wwin, no_close_button)) {
2218 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2219 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2221 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2222 mask = wwin->wm_gnustep_attr->close_mask;
2223 else
2224 mask = None;
2226 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2227 || fwin->rbutton_image->mask != mask)) {
2228 wPixmapDestroy(fwin->rbutton_image);
2229 fwin->rbutton_image = NULL;
2232 if (!fwin->rbutton_image) {
2233 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2234 fwin->rbutton_image->client_owned = 1;
2235 fwin->rbutton_image->client_owned_mask = 1;
2238 } else if (WFLAGP(wwin, kill_close)) {
2240 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2241 wPixmapDestroy(fwin->rbutton_image);
2243 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2245 } else if (MGFLAGP(wwin, broken_close)) {
2247 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2248 wPixmapDestroy(fwin->rbutton_image);
2250 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2252 } else {
2254 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2255 wPixmapDestroy(fwin->rbutton_image);
2257 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2261 /* force buttons to be redrawn */
2262 fwin->flags.need_texture_change = 1;
2263 wFrameWindowPaint(fwin);
2267 *---------------------------------------------------------------------------
2268 * wWindowConfigureBorders--
2269 * Update window border configuration according to attribute flags.
2271 *---------------------------------------------------------------------------
2273 void wWindowConfigureBorders(WWindow *wwin)
2275 if (wwin->frame) {
2276 int flags;
2277 int newy, oldh;
2279 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2281 #ifdef XKB_BUTTON_HINT
2282 if (wPreferences.modelock)
2283 flags |= WFF_LANGUAGE_BUTTON;
2284 #endif
2286 if (HAS_TITLEBAR(wwin))
2287 flags |= WFF_TITLEBAR;
2288 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2289 flags |= WFF_RESIZEBAR;
2290 if (HAS_BORDER(wwin))
2291 flags |= WFF_BORDER;
2292 if (wwin->flags.shaded)
2293 flags |= WFF_IS_SHADED;
2295 oldh = wwin->frame->top_width;
2296 wFrameWindowUpdateBorders(wwin->frame, flags);
2297 if (oldh != wwin->frame->top_width) {
2298 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2300 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2301 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2304 flags = 0;
2305 if (!WFLAGP(wwin, no_miniaturize_button)
2306 && wwin->frame->flags.hide_left_button)
2307 flags |= WFF_LEFT_BUTTON;
2309 #ifdef XKB_BUTTON_HINT
2310 if (!WFLAGP(wwin, no_language_button)
2311 && wwin->frame->flags.hide_language_button) {
2312 flags |= WFF_LANGUAGE_BUTTON;
2314 #endif
2316 if (!WFLAGP(wwin, no_close_button)
2317 && wwin->frame->flags.hide_right_button)
2318 flags |= WFF_RIGHT_BUTTON;
2320 if (flags != 0) {
2321 wWindowUpdateButtonImages(wwin);
2322 wFrameWindowShowButton(wwin->frame, flags);
2325 flags = 0;
2326 if (WFLAGP(wwin, no_miniaturize_button)
2327 && !wwin->frame->flags.hide_left_button)
2328 flags |= WFF_LEFT_BUTTON;
2330 #ifdef XKB_BUTTON_HINT
2331 if (WFLAGP(wwin, no_language_button)
2332 && !wwin->frame->flags.hide_language_button)
2333 flags |= WFF_LANGUAGE_BUTTON;
2334 #endif
2336 if (WFLAGP(wwin, no_close_button)
2337 && !wwin->frame->flags.hide_right_button)
2338 flags |= WFF_RIGHT_BUTTON;
2340 if (flags != 0)
2341 wFrameWindowHideButton(wwin->frame, flags);
2343 #ifdef SHAPE
2344 if (wShapeSupported && wwin->flags.shaped) {
2345 wWindowSetShape(wwin);
2347 #endif
2351 void wWindowSaveState(WWindow * wwin)
2353 CARD32 data[10];
2354 int i;
2356 memset(data, 0, sizeof(CARD32) * 10);
2357 data[0] = wwin->frame->workspace;
2358 data[1] = wwin->flags.miniaturized;
2359 data[2] = wwin->flags.shaded;
2360 data[3] = wwin->flags.hidden;
2361 data[4] = wwin->flags.maximized;
2362 if (wwin->flags.maximized == 0) {
2363 data[5] = wwin->frame_x;
2364 data[6] = wwin->frame_y;
2365 data[7] = wwin->frame->core->width;
2366 data[8] = wwin->frame->core->height;
2367 } else {
2368 data[5] = wwin->old_geometry.x;
2369 data[6] = wwin->old_geometry.y;
2370 data[7] = wwin->old_geometry.width;
2371 data[8] = wwin->old_geometry.height;
2374 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2375 if (wwin->screen_ptr->shortcutWindows[i] &&
2376 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2377 data[9] |= 1 << i;
2379 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2380 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2383 static int getSavedState(Window window, WSavedState ** state)
2385 Atom type_ret;
2386 int fmt_ret;
2387 unsigned long nitems_ret;
2388 unsigned long bytes_after_ret;
2389 CARD32 *data;
2391 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2392 True, _XA_WINDOWMAKER_STATE,
2393 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2394 (unsigned char **)&data) != Success || !data)
2395 return 0;
2397 *state = wmalloc(sizeof(WSavedState));
2399 (*state)->workspace = data[0];
2400 (*state)->miniaturized = data[1];
2401 (*state)->shaded = data[2];
2402 (*state)->hidden = data[3];
2403 (*state)->maximized = data[4];
2404 (*state)->x = data[5];
2405 (*state)->y = data[6];
2406 (*state)->w = data[7];
2407 (*state)->h = data[8];
2408 (*state)->window_shortcuts = data[9];
2410 XFree(data);
2412 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2413 return 1;
2414 else
2415 return 0;
2418 #ifdef SHAPE
2419 void wWindowClearShape(WWindow * wwin)
2421 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2422 0, wwin->frame->top_width, None, ShapeSet);
2423 XFlush(dpy);
2426 void wWindowSetShape(WWindow * wwin)
2428 XRectangle rect[2];
2429 int count;
2430 #ifdef OPTIMIZE_SHAPE
2431 XRectangle *rects;
2432 XRectangle *urec;
2433 int ordering;
2435 /* only shape is the client's */
2436 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2437 goto alt_code;
2440 /* Get array of rectangles describing the shape mask */
2441 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2442 if (!rects) {
2443 goto alt_code;
2446 urec = malloc(sizeof(XRectangle) * (count + 2));
2447 if (!urec) {
2448 XFree(rects);
2449 goto alt_code;
2452 /* insert our decoration rectangles in the rect list */
2453 memcpy(urec, rects, sizeof(XRectangle) * count);
2454 XFree(rects);
2456 if (HAS_TITLEBAR(wwin)) {
2457 urec[count].x = -1;
2458 urec[count].y = -1 - wwin->frame->top_width;
2459 urec[count].width = wwin->frame->core->width + 2;
2460 urec[count].height = wwin->frame->top_width + 1;
2461 count++;
2463 if (HAS_RESIZEBAR(wwin)) {
2464 urec[count].x = -1;
2465 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2466 urec[count].width = wwin->frame->core->width + 2;
2467 urec[count].height = wwin->frame->bottom_width + 1;
2468 count++;
2471 /* shape our frame window */
2472 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2473 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2474 XFlush(dpy);
2475 wfree(urec);
2476 return;
2478 alt_code:
2479 #endif /* OPTIMIZE_SHAPE */
2480 count = 0;
2481 if (HAS_TITLEBAR(wwin)) {
2482 rect[count].x = -1;
2483 rect[count].y = -1;
2484 rect[count].width = wwin->frame->core->width + 2;
2485 rect[count].height = wwin->frame->top_width + 1;
2486 count++;
2488 if (HAS_RESIZEBAR(wwin)) {
2489 rect[count].x = -1;
2490 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2491 rect[count].width = wwin->frame->core->width + 2;
2492 rect[count].height = wwin->frame->bottom_width + 1;
2493 count++;
2495 if (count > 0) {
2496 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2497 0, 0, rect, count, ShapeSet, Unsorted);
2499 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2500 0, wwin->frame->top_width, wwin->client_win,
2501 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2502 XFlush(dpy);
2504 #endif /* SHAPE */
2506 /* ====================================================================== */
2508 static FocusMode getFocusMode(WWindow * wwin)
2510 FocusMode mode;
2512 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2513 if (wwin->wm_hints->input == True) {
2514 if (wwin->protocols.TAKE_FOCUS)
2515 mode = WFM_LOCALLY_ACTIVE;
2516 else
2517 mode = WFM_PASSIVE;
2518 } else {
2519 if (wwin->protocols.TAKE_FOCUS)
2520 mode = WFM_GLOBALLY_ACTIVE;
2521 else
2522 mode = WFM_NO_INPUT;
2524 } else {
2525 mode = WFM_PASSIVE;
2527 return mode;
2530 void wWindowSetKeyGrabs(WWindow * wwin)
2532 int i;
2533 WShortKey *key;
2535 for (i = 0; i < WKBD_LAST; i++) {
2536 key = &wKeyBindings[i];
2538 if (key->keycode == 0)
2539 continue;
2540 if (key->modifier != AnyModifier) {
2541 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2542 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2543 #ifdef NUMLOCK_HACK
2544 /* Also grab all modifier combinations possible that include,
2545 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2546 * work even if the NumLock/ScrollLock key is on.
2548 wHackedGrabKey(key->keycode, key->modifier,
2549 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2550 #endif
2552 XGrabKey(dpy, key->keycode, key->modifier,
2553 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2556 wRootMenuBindShortcuts(wwin->frame->core->window);
2559 void wWindowResetMouseGrabs(WWindow * wwin)
2561 /* Mouse grabs can't be done on the client window because of
2562 * ICCCM and because clients that try to do the same will crash.
2564 * But there is a problem wich makes tbar buttons of unfocused
2565 * windows not usable as the click goes to the frame window instead
2566 * of the button itself. Must figure a way to fix that.
2569 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2571 if (!WFLAGP(wwin, no_bind_mouse)) {
2572 /* grabs for Meta+drag */
2573 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2574 True, ButtonPressMask | ButtonReleaseMask,
2575 GrabModeSync, GrabModeAsync, None, None);
2576 // for CTRL + Wheel to Scroll Horiz, we have to grab CTRL as well
2577 wHackedGrabButton(AnyButton, ControlMask, wwin->client_win,
2578 True, ButtonPressMask | ButtonReleaseMask,
2579 GrabModeSync, GrabModeAsync, None, None);
2580 wHackedGrabButton(AnyButton, MOD_MASK | ControlMask, wwin->client_win,
2581 True, ButtonPressMask | ButtonReleaseMask,
2582 GrabModeSync, GrabModeAsync, None, None);
2585 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2586 && !wwin->flags.is_gnustep) {
2587 /* the passive grabs to focus the window */
2588 /* if (wPreferences.focus_mode == WKF_CLICK) */
2589 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2590 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2592 XFlush(dpy);
2595 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2597 if (attr->flags & GSExtraFlagsAttr) {
2598 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2599 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2600 wWindowUpdateButtonImages(wwin);
2605 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2607 WWindowState *wstate;
2609 wstate = malloc(sizeof(WWindowState));
2610 if (!wstate)
2611 return 0;
2613 memset(wstate, 0, sizeof(WWindowState));
2614 wstate->pid = pid;
2615 if (instance)
2616 wstate->instance = wstrdup(instance);
2617 if (class)
2618 wstate->class = wstrdup(class);
2619 if (command)
2620 wstate->command = wstrdup(command);
2621 wstate->state = state;
2623 wstate->next = windowState;
2624 windowState = wstate;
2626 #ifdef DEBUG
2627 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
2628 #endif
2630 return wstate;
2633 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2635 WMagicNumber wWindowGetSavedState(Window win)
2637 char *instance, *class, *command = NULL;
2638 WWindowState *wstate = windowState;
2640 if (!wstate)
2641 return NULL;
2643 command = GetCommandForWindow(win);
2644 if (!command)
2645 return NULL;
2647 if (PropGetWMClass(win, &class, &instance)) {
2648 while (wstate) {
2649 if (SAME(instance, wstate->instance) &&
2650 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2651 break;
2653 wstate = wstate->next;
2655 } else {
2656 wstate = NULL;
2659 #ifdef DEBUG
2660 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
2661 #endif
2663 if (command)
2664 wfree(command);
2665 if (instance)
2666 XFree(instance);
2667 if (class)
2668 XFree(class);
2670 return wstate;
2673 void wWindowDeleteSavedState(WMagicNumber id)
2675 WWindowState *tmp, *wstate = (WWindowState *) id;
2677 if (!wstate || !windowState)
2678 return;
2680 tmp = windowState;
2681 if (tmp == wstate) {
2682 windowState = wstate->next;
2683 #ifdef DEBUG
2684 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2685 wstate, wstate->instance, wstate->class, wstate->command);
2686 #endif
2687 if (wstate->instance)
2688 wfree(wstate->instance);
2689 if (wstate->class)
2690 wfree(wstate->class);
2691 if (wstate->command)
2692 wfree(wstate->command);
2693 wfree(wstate->state);
2694 wfree(wstate);
2695 } else {
2696 while (tmp->next) {
2697 if (tmp->next == wstate) {
2698 tmp->next = wstate->next;
2699 #ifdef DEBUG
2700 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2701 wstate, wstate->instance, wstate->class, wstate->command);
2702 #endif
2703 if (wstate->instance)
2704 wfree(wstate->instance);
2705 if (wstate->class)
2706 wfree(wstate->class);
2707 if (wstate->command)
2708 wfree(wstate->command);
2709 wfree(wstate->state);
2710 wfree(wstate);
2711 break;
2713 tmp = tmp->next;
2718 void wWindowDeleteSavedStatesForPID(pid_t pid)
2720 WWindowState *tmp, *wstate;
2722 if (!windowState)
2723 return;
2725 tmp = windowState;
2726 if (tmp->pid == pid) {
2727 wstate = windowState;
2728 windowState = tmp->next;
2729 #ifdef DEBUG
2730 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2731 wstate, wstate->instance, wstate->class, wstate->command);
2732 #endif
2733 if (wstate->instance)
2734 wfree(wstate->instance);
2735 if (wstate->class)
2736 wfree(wstate->class);
2737 if (wstate->command)
2738 wfree(wstate->command);
2739 wfree(wstate->state);
2740 wfree(wstate);
2741 } else {
2742 while (tmp->next) {
2743 if (tmp->next->pid == pid) {
2744 wstate = tmp->next;
2745 tmp->next = wstate->next;
2746 #ifdef DEBUG
2747 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2748 wstate, wstate->instance, wstate->class, wstate->command);
2749 #endif
2750 if (wstate->instance)
2751 wfree(wstate->instance);
2752 if (wstate->class)
2753 wfree(wstate->class);
2754 if (wstate->command)
2755 wfree(wstate->command);
2756 wfree(wstate->state);
2757 wfree(wstate);
2758 break;
2760 tmp = tmp->next;
2765 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2767 if (wwin->flags.omnipresent == flag)
2768 return;
2770 wwin->flags.omnipresent = flag;
2771 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2774 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2776 WWindow *wwin = data;
2778 #ifndef NUMLOCK_HACK
2779 if ((event->xbutton.state & ValidModMask)
2780 != (event->xbutton.state & ~LockMask)) {
2781 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2782 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2784 #endif
2786 event->xbutton.state &= ValidModMask;
2788 CloseWindowMenu(wwin->screen_ptr);
2790 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2791 && !WFLAGP(wwin, no_focusable)) {
2792 wSetFocusTo(wwin->screen_ptr, wwin);
2795 if (event->xbutton.button == Button1)
2796 wRaiseFrame(wwin->frame->core);
2798 if (event->xbutton.window != wwin->frame->resizebar->window) {
2799 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2800 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2801 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2802 #ifdef DEBUG0
2803 wwarning("pointer grab failed for window move");
2804 #endif
2805 return;
2809 if (event->xbutton.state & MOD_MASK) {
2810 /* move the window */
2811 wMouseMoveWindow(wwin, event);
2812 XUngrabPointer(dpy, CurrentTime);
2813 } else {
2814 wMouseResizeWindow(wwin, event);
2815 XUngrabPointer(dpy, CurrentTime);
2819 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2821 WWindow *wwin = data;
2823 event->xbutton.state &= ValidModMask;
2825 if (event->xbutton.button == Button1) {
2826 if (event->xbutton.state == 0) {
2827 if (!WFLAGP(wwin, no_shadeable)) {
2828 /* shade window */
2829 if (wwin->flags.shaded)
2830 wUnshadeWindow(wwin);
2831 else
2832 wShadeWindow(wwin);
2834 } else {
2835 int dir = 0;
2837 if (event->xbutton.state & ControlMask)
2838 dir |= MAX_VERTICAL;
2840 if (event->xbutton.state & ShiftMask) {
2841 dir |= MAX_HORIZONTAL;
2842 if (!(event->xbutton.state & ControlMask))
2843 wSelectWindow(wwin, !wwin->flags.selected);
2846 /* maximize window */
2847 if (dir != 0 && IS_RESIZABLE(wwin)) {
2848 int ndir = dir ^ wwin->flags.maximized;
2850 if (ndir != 0)
2851 wMaximizeWindow(wwin, ndir);
2852 else
2853 wUnmaximizeWindow(wwin);
2856 } else if (event->xbutton.button == Button3) {
2857 if (event->xbutton.state & MOD_MASK) {
2858 wHideOtherApplications(wwin);
2860 } else if (event->xbutton.button == Button2) {
2861 wSelectWindow(wwin, !wwin->flags.selected);
2862 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2863 wShadeWindow(wwin);
2864 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2865 wUnshadeWindow(wwin);
2869 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2871 WWindow *wwin = desc->parent;
2872 unsigned int new_width, w_scale;
2873 unsigned int new_height, h_scale;
2874 unsigned int resize_width_increment = 0;
2875 unsigned int resize_height_increment = 0;
2877 if (wwin->normal_hints) {
2878 w_scale = ceil(wPreferences.resize_increment / wwin->normal_hints->width_inc);
2879 h_scale = ceil(wPreferences.resize_increment / wwin->normal_hints->height_inc);
2880 resize_width_increment = wwin->normal_hints->width_inc * w_scale;
2881 resize_height_increment = wwin->normal_hints->height_inc * h_scale;
2883 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2884 resize_width_increment = wPreferences.resize_increment;
2885 resize_height_increment = wPreferences.resize_increment;
2888 event->xbutton.state &= ValidModMask;
2890 CloseWindowMenu(wwin->screen_ptr);
2892 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2893 wSetFocusTo(wwin->screen_ptr, wwin);
2895 if (event->xbutton.button == Button1)
2896 wRaiseFrame(wwin->frame->core);
2898 if (event->xbutton.state & ControlMask) {
2899 if (event->xbutton.button == Button1) {
2900 if (wwin->screen_ptr->current_workspace > 0) {
2901 wWindowChangeWorkspace(wwin,wwin->screen_ptr->current_workspace - 1);
2902 wWorkspaceRelativeChange(wwin->screen_ptr,-1);
2905 if (event->xbutton.button == Button3) {
2906 if (wwin->screen_ptr->current_workspace < (wwin->screen_ptr->workspace_count - 1) ) {
2907 wWindowChangeWorkspace(wwin,wwin->screen_ptr->current_workspace + 1);
2908 wWorkspaceRelativeChange(wwin->screen_ptr,1);
2911 if (event->xbutton.button == Button4) {
2912 new_width = wwin->client.width - resize_width_increment;
2913 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2914 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2916 if (event->xbutton.button == Button5) {
2917 new_width = wwin->client.width + resize_width_increment;
2918 wWindowConstrainSize(wwin, &new_width, &wwin->client.height);
2919 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, wwin->client.height);
2923 if (event->xbutton.state & MOD_MASK) {
2924 /* move the window */
2925 if (XGrabPointer(dpy, wwin->client_win, False,
2926 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2927 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2928 #ifdef DEBUG0
2929 wwarning("pointer grab failed for window move");
2930 #endif
2931 return;
2933 if (event->xbutton.button == Button3) {
2934 wMouseResizeWindow(wwin, event);
2935 } else if (event->xbutton.button == Button4) {
2936 new_height = wwin->client.height - resize_height_increment;
2937 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2938 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2939 } else if (event->xbutton.button == Button5) {
2940 new_height = wwin->client.height + resize_height_increment;
2941 wWindowConstrainSize(wwin, &wwin->client.width, &new_height);
2942 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, new_height);
2943 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2944 wMouseMoveWindow(wwin, event);
2946 XUngrabPointer(dpy, CurrentTime);
2950 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2952 WWindow *wwin = (WWindow *) data;
2954 #ifndef NUMLOCK_HACK
2955 if ((event->xbutton.state & ValidModMask)
2956 != (event->xbutton.state & ~LockMask)) {
2957 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2958 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2960 #endif
2961 event->xbutton.state &= ValidModMask;
2963 CloseWindowMenu(wwin->screen_ptr);
2965 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2966 && !WFLAGP(wwin, no_focusable)) {
2967 wSetFocusTo(wwin->screen_ptr, wwin);
2970 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2972 if (event->xbutton.button == Button1) {
2973 if (event->xbutton.state & MOD_MASK) {
2974 wLowerFrame(wwin->frame->core);
2975 } else {
2976 wRaiseFrame(wwin->frame->core);
2979 if ((event->xbutton.state & ShiftMask)
2980 && !(event->xbutton.state & ControlMask)) {
2981 wSelectWindow(wwin, !wwin->flags.selected);
2982 return;
2984 if (event->xbutton.window != wwin->frame->titlebar->window
2985 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2986 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2987 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2988 #ifdef DEBUG0
2989 wwarning("pointer grab failed for window move");
2990 #endif
2991 return;
2994 /* move the window */
2995 wMouseMoveWindow(wwin, event);
2997 XUngrabPointer(dpy, CurrentTime);
2998 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2999 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
3000 WObjDescriptor *desc;
3002 if (event->xbutton.window != wwin->frame->titlebar->window
3003 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3004 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
3005 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
3006 #ifdef DEBUG0
3007 wwarning("pointer grab failed for window move");
3008 #endif
3009 return;
3012 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
3014 /* allow drag select */
3015 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3016 event->xany.send_event = True;
3017 (*desc->handle_mousedown) (desc, event);
3019 XUngrabPointer(dpy, CurrentTime);
3023 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3025 WWindow *wwin = data;
3027 event->xbutton.state &= ValidModMask;
3029 CloseWindowMenu(wwin->screen_ptr);
3031 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3032 return;
3034 /* if control-click, kill the client */
3035 if (event->xbutton.state & ControlMask) {
3036 wClientKill(wwin);
3037 } else {
3038 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3039 /* send delete message */
3040 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3045 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3047 WWindow *wwin = data;
3049 CloseWindowMenu(wwin->screen_ptr);
3051 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3052 return;
3054 /* send delete message */
3055 if (wwin->protocols.DELETE_WINDOW) {
3056 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3057 } else {
3058 wClientKill(wwin);
3062 #ifdef XKB_BUTTON_HINT
3063 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3065 WWindow *wwin = data;
3066 WFrameWindow *fwin = wwin->frame;
3067 WScreen *scr = fwin->screen_ptr;
3068 XkbStateRec staterec;
3069 int tl;
3071 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3072 return;
3073 tl = wwin->frame->languagemode;
3074 wwin->frame->languagemode = wwin->frame->last_languagemode;
3075 wwin->frame->last_languagemode = tl;
3076 wSetFocusTo(scr, wwin);
3077 wwin->frame->languagebutton_image =
3078 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3079 wFrameWindowUpdateLanguageButton(wwin->frame);
3080 if (event->xbutton.button == Button3)
3081 return;
3082 wRaiseFrame(fwin->core);
3084 #endif
3086 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3088 WWindow *wwin = data;
3090 event->xbutton.state &= ValidModMask;
3092 CloseWindowMenu(wwin->screen_ptr);
3094 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3095 return;
3097 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3098 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3099 } else {
3100 WApplication *wapp;
3101 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3103 wapp = wApplicationOf(wwin->main_window);
3104 if (wapp && !WFLAGP(wwin, no_appicon))
3105 wHideApplication(wapp);
3106 } else if (event->xbutton.state == 0) {
3107 wIconifyWindow(wwin);