Respect size hints when resizing with wheel.
[wmaker-crm.git] / src / window.c
blob5f1e53cf43ece0b3da2a7c5cbbbe85a021d534b6
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>
38 /* For getting mouse wheel mappings from WINGs */
39 #include <WINGs/WINGsP.h>
41 #include "WindowMaker.h"
42 #include "GNUstep.h"
43 #include "wcore.h"
44 #include "framewin.h"
45 #include "texture.h"
46 #include "window.h"
47 #include "winspector.h"
48 #include "icon.h"
49 #include "properties.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "keybind.h"
54 #include "stacking.h"
55 #include "defaults.h"
56 #include "workspace.h"
57 #include "xinerama.h"
59 #ifdef MWM_HINTS
60 # include "motif.h"
61 #endif
62 #include "wmspec.h"
64 #define MOD_MASK wPreferences.modifier_mask
66 /****** Global Variables ******/
67 extern WShortKey wKeyBindings[WKBD_LAST];
69 #ifdef SHAPE
70 extern Bool wShapeSupported;
71 #endif
73 /* contexts */
74 extern XContext wWinContext;
76 /* cursors */
77 extern Cursor wCursor[WCUR_LAST];
79 /* protocol atoms */
80 extern Atom _XA_WM_DELETE_WINDOW;
81 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
82 extern Atom _XA_WINDOWMAKER_STATE;
84 extern WPreferences wPreferences;
85 extern Time LastTimestamp;
87 /* superfluous... */
88 extern void DoWindowBirth(WWindow *wwin);
90 /***** Local Stuff *****/
91 static WWindowState *windowState = NULL;
93 static FocusMode getFocusMode(WWindow *wwin);
94 static int getSavedState(Window window, WSavedState **state);
95 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
97 /* frame window (during window grabs) */
98 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
100 /* close button */
101 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
102 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
104 /* iconify button */
105 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
107 #ifdef XKB_BUTTON_HINT
108 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
109 #endif
111 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
112 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
113 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
115 /****** Notification Observers ******/
117 static void appearanceObserver(void *self, WMNotification * notif)
119 WWindow *wwin = (WWindow *) self;
120 uintptr_t flags = (uintptr_t)WMGetNotificationClientData(notif);
122 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
123 return;
125 if (flags & WFontSettings) {
126 wWindowConfigureBorders(wwin);
127 if (wwin->flags.shaded) {
128 wFrameWindowResize(wwin->frame, wwin->frame->core->width, wwin->frame->top_width - 1);
130 wwin->client.y = wwin->frame_y - wwin->client.height + wwin->frame->top_width;
131 wWindowSynthConfigureNotify(wwin);
134 if (flags & WTextureSettings) {
135 wwin->frame->flags.need_texture_remake = 1;
137 if (flags & (WTextureSettings | WColorSettings)) {
138 if (wwin->frame->titlebar)
139 XClearWindow(dpy, wwin->frame->titlebar->window);
141 wFrameWindowPaint(wwin->frame);
146 WWindow *wWindowFor(Window window)
148 WObjDescriptor *desc;
150 if (window == None)
151 return NULL;
153 if (XFindContext(dpy, window, wWinContext, (XPointer *) & desc) == XCNOENT)
154 return NULL;
156 if (desc->parent_type == WCLASS_WINDOW)
157 return desc->parent;
158 else if (desc->parent_type == WCLASS_FRAME) {
159 WFrameWindow *frame = (WFrameWindow *) desc->parent;
160 if (frame->flags.is_client_window_frame)
161 return frame->child;
164 return NULL;
167 WWindow *wWindowCreate()
169 WWindow *wwin;
171 wwin = wmalloc(sizeof(WWindow));
172 wretain(wwin);
174 memset(wwin, 0, sizeof(WWindow));
176 wwin->client_descriptor.handle_mousedown = frameMouseDown;
177 wwin->client_descriptor.parent = wwin;
178 wwin->client_descriptor.self = wwin;
179 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
181 return wwin;
184 void wWindowDestroy(WWindow *wwin)
186 int i;
188 if (wwin->screen_ptr->cmap_window == wwin)
189 wwin->screen_ptr->cmap_window = NULL;
191 WMRemoveNotificationObserver(wwin);
193 wwin->flags.destroyed = 1;
195 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
196 if (!wwin->screen_ptr->shortcutWindows[i])
197 continue;
199 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
201 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
202 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
203 wwin->screen_ptr->shortcutWindows[i] = NULL;
207 if (wwin->fake_group && wwin->fake_group->retainCount > 0) {
208 wwin->fake_group->retainCount--;
209 if (wwin->fake_group->retainCount == 0 && wwin->fake_group->leader != None) {
210 XDestroyWindow(dpy, wwin->fake_group->leader);
211 wwin->fake_group->leader = None;
212 wwin->fake_group->origLeader = None;
213 XFlush(dpy);
217 if (wwin->normal_hints)
218 XFree(wwin->normal_hints);
220 if (wwin->wm_hints)
221 XFree(wwin->wm_hints);
223 if (wwin->wm_instance)
224 XFree(wwin->wm_instance);
226 if (wwin->wm_class)
227 XFree(wwin->wm_class);
229 if (wwin->wm_gnustep_attr)
230 wfree(wwin->wm_gnustep_attr);
232 if (wwin->cmap_windows)
233 XFree(wwin->cmap_windows);
235 XDeleteContext(dpy, wwin->client_win, wWinContext);
237 if (wwin->frame)
238 wFrameWindowDestroy(wwin->frame);
240 if (wwin->icon) {
241 RemoveFromStackList(wwin->icon->core);
242 wIconDestroy(wwin->icon);
243 if (wPreferences.auto_arrange_icons)
244 wArrangeIcons(wwin->screen_ptr, True);
246 if (wwin->net_icon_image)
247 RReleaseImage(wwin->net_icon_image);
249 wrelease(wwin);
252 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
254 if (gs_hints->flags & GSWindowStyleAttr) {
255 if (gs_hints->window_style == WMBorderlessWindowMask) {
256 wwin->client_flags.no_border = 1;
257 wwin->client_flags.no_titlebar = 1;
258 wwin->client_flags.no_closable = 1;
259 wwin->client_flags.no_miniaturizable = 1;
260 wwin->client_flags.no_resizable = 1;
261 wwin->client_flags.no_close_button = 1;
262 wwin->client_flags.no_miniaturize_button = 1;
263 wwin->client_flags.no_resizebar = 1;
264 } else {
265 wwin->client_flags.no_close_button =
266 ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
268 wwin->client_flags.no_closable = ((gs_hints->window_style & WMClosableWindowMask) ? 0 : 1);
270 wwin->client_flags.no_miniaturize_button =
271 ((gs_hints->window_style & WMMiniaturizableWindowMask) ? 0 : 1);
273 wwin->client_flags.no_miniaturizable = wwin->client_flags.no_miniaturize_button;
275 wwin->client_flags.no_resizebar =
276 ((gs_hints->window_style & WMResizableWindowMask) ? 0 : 1);
278 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
280 /* these attributes supposedly imply in the existence
281 * of a titlebar */
282 if (gs_hints->window_style & (WMResizableWindowMask |
283 WMClosableWindowMask | WMMiniaturizableWindowMask)) {
284 wwin->client_flags.no_titlebar = 0;
285 } else {
286 wwin->client_flags.no_titlebar =
287 ((gs_hints->window_style & WMTitledWindowMask) ? 0 : 1);
291 } else {
292 /* setup the defaults */
293 wwin->client_flags.no_border = 0;
294 wwin->client_flags.no_titlebar = 0;
295 wwin->client_flags.no_closable = 0;
296 wwin->client_flags.no_miniaturizable = 0;
297 wwin->client_flags.no_resizable = 0;
298 wwin->client_flags.no_close_button = 0;
299 wwin->client_flags.no_miniaturize_button = 0;
300 wwin->client_flags.no_resizebar = 0;
302 if (gs_hints->extra_flags & GSNoApplicationIconFlag)
303 wwin->client_flags.no_appicon = 1;
306 void wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags, WWindowAttributes *mask)
308 if (wflags->no_appicon && mask->no_appicon)
309 wflags->emulate_appicon = 0;
311 if (wwin->main_window != None) {
312 WApplication *wapp = wApplicationOf(wwin->main_window);
313 if (wapp && !wapp->flags.emulated)
314 wflags->emulate_appicon = 0;
317 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
318 wflags->emulate_appicon = 0;
320 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
321 wflags->sunken = 0;
324 void wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
326 WScreen *scr = wwin->screen_ptr;
328 /* sets global default stuff */
329 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class, &wwin->client_flags, NULL, True);
331 * Decoration setting is done in this precedence (lower to higher)
332 * - use global default in the resource database
333 * - guess some settings
334 * - use GNUstep/external window attributes
335 * - set hints specified for the app in the resource DB
338 WSETUFLAG(wwin, broken_close, 0);
340 if (wwin->protocols.DELETE_WINDOW)
341 WSETUFLAG(wwin, kill_close, 0);
342 else
343 WSETUFLAG(wwin, kill_close, 1);
345 /* transients can't be iconified or maximized */
346 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
347 WSETUFLAG(wwin, no_miniaturizable, 1);
348 WSETUFLAG(wwin, no_miniaturize_button, 1);
351 /* if the window can't be resized, remove the resizebar */
352 if (wwin->normal_hints->flags & (PMinSize | PMaxSize)
353 && (wwin->normal_hints->min_width == wwin->normal_hints->max_width)
354 && (wwin->normal_hints->min_height == wwin->normal_hints->max_height)) {
355 WSETUFLAG(wwin, no_resizable, 1);
356 WSETUFLAG(wwin, no_resizebar, 1);
359 /* set GNUstep window attributes */
360 if (wwin->wm_gnustep_attr) {
361 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
363 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
365 *level = wwin->wm_gnustep_attr->window_level;
367 * INT_MIN is the only illegal window level.
369 if (*level == INT_MIN)
370 *level = INT_MIN + 1;
371 } else {
372 /* setup defaults */
373 *level = WMNormalLevel;
375 } else {
376 int tmp_workspace = -1;
377 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
378 Bool check;
380 check = False;
382 #ifdef MWM_HINTS
383 wMWMCheckClientHints(wwin);
384 #endif /* MWM_HINTS */
386 if (!check)
387 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
389 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
390 * have INT_MIN that means that no window level was requested. -Dan
392 if (tmp_level == INT_MIN) {
393 if (WFLAGP(wwin, floating))
394 *level = WMFloatingLevel;
395 else if (WFLAGP(wwin, sunken))
396 *level = WMSunkenLevel;
397 else
398 *level = WMNormalLevel;
399 } else {
400 *level = tmp_level;
403 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
404 WWindow *transientOwner = wWindowFor(wwin->transient_for);
405 if (transientOwner) {
406 int ownerLevel = transientOwner->frame->core->stacking->window_level;
407 if (ownerLevel > *level)
408 *level = ownerLevel;
412 if (tmp_workspace >= 0)
413 *workspace = tmp_workspace % scr->workspace_count;
417 * Set attributes specified only for that window/class.
418 * This might do duplicate work with the 1st wDefaultFillAttributes().
420 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
421 &wwin->user_flags, &wwin->defined_user_flags, False);
423 * Sanity checks for attributes that depend on other attributes
425 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
426 wwin->user_flags.emulate_appicon = 0;
428 if (wwin->main_window != None) {
429 WApplication *wapp = wApplicationOf(wwin->main_window);
430 if (wapp && !wapp->flags.emulated)
431 wwin->user_flags.emulate_appicon = 0;
434 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win)
435 wwin->user_flags.emulate_appicon = 0;
437 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
438 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
439 wwin->user_flags.sunken = 0;
441 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
443 /* windows that have takefocus=False shouldn't take focus at all */
444 if (wwin->focus_mode == WFM_NO_INPUT)
445 wwin->client_flags.no_focusable = 1;
448 Bool wWindowCanReceiveFocus(WWindow *wwin)
450 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
451 return False;
452 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
453 return False;
454 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
455 return False;
457 return True;
460 Bool wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
462 int w1, h1, w2, h2;
464 w1 = wwin->frame->core->width;
465 h1 = wwin->frame->core->height;
466 w2 = obscured->frame->core->width;
467 h2 = obscured->frame->core->height;
469 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
470 && wwin->frame->workspace != obscured->frame->workspace)
471 return False;
473 if (wwin->frame_x + w1 < obscured->frame_x
474 || wwin->frame_y + h1 < obscured->frame_y
475 || wwin->frame_x > obscured->frame_x + w2 || wwin->frame_y > obscured->frame_y + h2)
476 return False;
478 return True;
481 static void fixLeaderProperties(WWindow *wwin)
483 XClassHint *classHint;
484 XWMHints *hints, *clientHints;
485 Window leaders[2], window;
486 char **argv, *command;
487 int argc, i, pid;
488 Bool haveCommand;
490 classHint = XAllocClassHint();
491 clientHints = XGetWMHints(dpy, wwin->client_win);
492 pid = wNETWMGetPidForWindow(wwin->client_win);
493 if (pid > 0)
494 haveCommand = GetCommandForPid(pid, &argv, &argc);
495 else
496 haveCommand = False;
498 leaders[0] = wwin->client_leader;
499 leaders[1] = wwin->group_id;
501 if (haveCommand) {
502 command = GetCommandForWindow(wwin->client_win);
503 if (command) {
504 /* command already set. nothing to do. */
505 wfree(command);
506 } else {
507 XSetCommand(dpy, wwin->client_win, argv, argc);
511 for (i = 0; i < 2; i++) {
512 window = leaders[i];
513 if (window) {
514 if (XGetClassHint(dpy, window, classHint) == 0) {
515 classHint->res_name = wwin->wm_instance;
516 classHint->res_class = wwin->wm_class;
517 XSetClassHint(dpy, window, classHint);
519 hints = XGetWMHints(dpy, window);
520 if (hints) {
521 XFree(hints);
522 } else if (clientHints) {
523 /* set window group leader to self */
524 clientHints->window_group = window;
525 clientHints->flags |= WindowGroupHint;
526 XSetWMHints(dpy, window, clientHints);
529 if (haveCommand) {
530 command = GetCommandForWindow(window);
531 if (command) {
532 /* command already set. nothing to do. */
533 wfree(command);
534 } else {
535 XSetCommand(dpy, window, argv, argc);
541 XFree(classHint);
542 if (clientHints)
543 XFree(clientHints);
544 if (haveCommand)
545 wfree(argv);
548 static Window createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
550 XClassHint *classHint;
551 XWMHints *hints;
552 Window leader;
553 int argc;
554 char **argv;
556 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
557 /* set class hint */
558 classHint = XAllocClassHint();
559 classHint->res_name = instance;
560 classHint->res_class = class;
561 XSetClassHint(dpy, leader, classHint);
562 XFree(classHint);
564 /* inherit these from the original leader if available */
565 hints = XGetWMHints(dpy, win);
566 if (!hints) {
567 hints = XAllocWMHints();
568 hints->flags = 0;
570 /* set window group leader to self */
571 hints->window_group = leader;
572 hints->flags |= WindowGroupHint;
573 XSetWMHints(dpy, leader, hints);
574 XFree(hints);
576 if (XGetCommand(dpy, win, &argv, &argc) != 0 && argc > 0) {
577 XSetCommand(dpy, leader, argv, argc);
578 XFreeStringList(argv);
581 return leader;
584 static int matchIdentifier(void *item, void *cdata)
586 return (strcmp(((WFakeGroupLeader *) item)->identifier, (char *)cdata) == 0);
590 *----------------------------------------------------------------
591 * wManageWindow--
592 * reparents the window and allocates a descriptor for it.
593 * Window manager hints and other hints are fetched to configure
594 * the window decoration attributes and others. User preferences
595 * for the window are used if available, to configure window
596 * decorations and some behaviour.
597 * If in startup, windows that are override redirect,
598 * unmapped and never were managed and are Withdrawn are not
599 * managed.
601 * Returns:
602 * the new window descriptor
604 * Side effects:
605 * The window is reparented and appropriate notification
606 * is done to the client. Input mask for the window is setup.
607 * The window descriptor is also associated with various window
608 * contexts and inserted in the head of the window list.
609 * Event handler contexts are associated for some objects
610 * (buttons, titlebar and resizebar)
612 *----------------------------------------------------------------
614 WWindow *wManageWindow(WScreen *scr, Window window)
616 WWindow *wwin;
617 int x, y;
618 unsigned width, height;
619 XWindowAttributes wattribs;
620 XSetWindowAttributes attribs;
621 WWindowState *win_state;
622 WWindow *transientOwner = NULL;
623 int window_level;
624 int wm_state;
625 int foo;
626 int workspace = -1;
627 char *title;
628 Bool withdraw = False;
629 Bool raise = False;
631 /* mutex. */
632 /* XGrabServer(dpy); */
633 XSync(dpy, False);
634 /* make sure the window is still there */
635 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
636 XUngrabServer(dpy);
637 return NULL;
640 /* if it's an override-redirect, ignore it */
641 if (wattribs.override_redirect) {
642 XUngrabServer(dpy);
643 return NULL;
646 wm_state = PropGetWindowState(window);
648 /* if it's startup and the window is unmapped, don't manage it */
649 if (scr->flags.startup && wm_state < 0 && wattribs.map_state == IsUnmapped) {
650 XUngrabServer(dpy);
651 return NULL;
654 wwin = wWindowCreate();
656 title = wNETWMGetWindowName(window);
657 if (title)
658 wwin->flags.net_has_title = 1;
659 if (!title && !wFetchName(dpy, window, &title))
660 title = NULL;
662 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
664 #ifdef DEBUG
665 printf("managing window %x\n", (unsigned)window);
666 #endif
668 #ifdef SHAPE
669 if (wShapeSupported) {
670 int junk;
671 unsigned int ujunk;
672 int b_shaped;
674 XShapeSelectInput(dpy, window, ShapeNotifyMask);
675 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
676 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
677 wwin->flags.shaped = b_shaped;
679 #endif
682 * Get hints and other information in properties
684 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
686 /* setup descriptor */
687 wwin->client_win = window;
688 wwin->screen_ptr = scr;
689 wwin->old_border_width = wattribs.border_width;
690 wwin->event_mask = CLIENT_EVENTS;
691 attribs.event_mask = CLIENT_EVENTS;
692 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
693 attribs.save_under = False;
695 XChangeWindowAttributes(dpy, window, CWEventMask | CWDontPropagate | CWSaveUnder, &attribs);
696 XSetWindowBorderWidth(dpy, window, 0);
698 /* get hints from GNUstep app */
699 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
700 wwin->flags.is_gnustep = 1;
702 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
703 wwin->wm_gnustep_attr = NULL;
706 wwin->client_leader = PropGetClientLeader(window);
707 if (wwin->client_leader != None)
708 wwin->main_window = wwin->client_leader;
710 wwin->wm_hints = XGetWMHints(dpy, window);
712 if (wwin->wm_hints) {
713 if (wwin->wm_hints->flags & StateHint) {
715 if (wwin->wm_hints->initial_state == IconicState) {
717 wwin->flags.miniaturized = 1;
719 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
721 withdraw = True;
725 if (wwin->wm_hints->flags & WindowGroupHint) {
726 wwin->group_id = wwin->wm_hints->window_group;
727 /* window_group has priority over CLIENT_LEADER */
728 wwin->main_window = wwin->group_id;
729 } else {
730 wwin->group_id = None;
733 if (wwin->wm_hints->flags & UrgencyHint)
734 wwin->flags.urgent = 1;
735 } else {
736 wwin->group_id = None;
739 PropGetProtocols(window, &wwin->protocols);
741 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
742 wwin->transient_for = None;
743 } else {
744 if (wwin->transient_for == None || wwin->transient_for == window) {
745 wwin->transient_for = scr->root_win;
746 } else {
747 transientOwner = wWindowFor(wwin->transient_for);
748 if (transientOwner && transientOwner->main_window != None)
749 wwin->main_window = transientOwner->main_window;
753 /* guess the focus mode */
754 wwin->focus_mode = getFocusMode(wwin);
756 /* get geometry stuff */
757 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
759 /* get colormap windows */
760 GetColormapWindows(wwin);
763 * Setup the decoration/window attributes and
764 * geometry
766 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
768 /* Make broken apps behave as a nice app. */
769 if (WFLAGP(wwin, emulate_appicon))
770 wwin->main_window = wwin->client_win;
772 fixLeaderProperties(wwin);
774 wwin->orig_main_window = wwin->main_window;
776 if (wwin->flags.is_gnustep)
777 WSETUFLAG(wwin, shared_appicon, 0);
779 if (wwin->main_window) {
780 extern Atom _XA_WINDOWMAKER_MENU;
781 XTextProperty text_prop;
783 if (XGetTextProperty(dpy, wwin->main_window, &text_prop, _XA_WINDOWMAKER_MENU)) {
784 WSETUFLAG(wwin, shared_appicon, 0);
788 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
789 char *buffer, *instance, *class;
790 WFakeGroupLeader *fPtr;
791 int index;
793 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
795 /* // only enter here if PropGetWMClass() succeds */
796 PropGetWMClass(wwin->main_window, &class, &instance);
797 buffer = StrConcatDot(instance, class);
799 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void *)buffer);
800 if (index != WANotFound) {
801 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
802 if (fPtr->retainCount == 0) {
803 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
804 instance, class);
806 fPtr->retainCount++;
807 #undef method2
808 if (fPtr->origLeader == None) {
809 #ifdef method2
810 if (ADEQUATE(wwin->group_id)) {
811 fPtr->retainCount++;
812 fPtr->origLeader = wwin->group_id;
813 } else if (ADEQUATE(wwin->client_leader)) {
814 fPtr->retainCount++;
815 fPtr->origLeader = wwin->client_leader;
816 } else if (ADEQUATE(wwin->main_window)) {
817 fPtr->retainCount++;
818 fPtr->origLeader = wwin->main_window;
820 #else
821 if (ADEQUATE(wwin->main_window)) {
822 fPtr->retainCount++;
823 fPtr->origLeader = wwin->main_window;
825 #endif
827 wwin->fake_group = fPtr;
828 /*wwin->group_id = fPtr->leader; */
829 wwin->main_window = fPtr->leader;
830 wfree(buffer);
831 } else {
832 fPtr = (WFakeGroupLeader *) wmalloc(sizeof(WFakeGroupLeader));
834 fPtr->identifier = buffer;
835 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window, instance, class);
836 fPtr->origLeader = None;
837 fPtr->retainCount = 1;
839 WMAddToArray(scr->fakeGroupLeaders, fPtr);
841 #ifdef method2
842 if (ADEQUATE(wwin->group_id)) {
843 fPtr->retainCount++;
844 fPtr->origLeader = wwin->group_id;
845 } else if (ADEQUATE(wwin->client_leader)) {
846 fPtr->retainCount++;
847 fPtr->origLeader = wwin->client_leader;
848 } else if (ADEQUATE(wwin->main_window)) {
849 fPtr->retainCount++;
850 fPtr->origLeader = wwin->main_window;
852 #else
853 if (ADEQUATE(wwin->main_window)) {
854 fPtr->retainCount++;
855 fPtr->origLeader = wwin->main_window;
857 #endif
858 wwin->fake_group = fPtr;
859 /*wwin->group_id = fPtr->leader; */
860 wwin->main_window = fPtr->leader;
862 if (instance)
863 XFree(instance);
864 if (class)
865 XFree(class);
867 #undef method2
868 #undef ADEQUATE
872 * Setup the initial state of the window
874 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable))
875 wwin->flags.miniaturized = 1;
877 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin))
878 wwin->flags.maximized = MAX_VERTICAL | MAX_HORIZONTAL;
880 wNETWMCheckInitialClientState(wwin);
882 /* apply previous state if it exists and we're in startup */
883 if (scr->flags.startup && wm_state >= 0) {
885 if (wm_state == IconicState) {
887 wwin->flags.miniaturized = 1;
889 } else if (wm_state == WithdrawnState) {
891 withdraw = True;
895 /* if there is a saved state (from file), restore it */
896 win_state = NULL;
897 if (wwin->main_window != None)
898 win_state = (WWindowState *) wWindowGetSavedState(wwin->main_window);
899 else
900 win_state = (WWindowState *) wWindowGetSavedState(window);
902 if (win_state && !withdraw) {
903 if (win_state->state->hidden > 0)
904 wwin->flags.hidden = win_state->state->hidden;
906 if (win_state->state->shaded > 0 && !WFLAGP(wwin, no_shadeable))
907 wwin->flags.shaded = win_state->state->shaded;
909 if (win_state->state->miniaturized > 0 && !WFLAGP(wwin, no_miniaturizable)) {
910 wwin->flags.miniaturized = win_state->state->miniaturized;
913 if (!IS_OMNIPRESENT(wwin)) {
914 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
915 wwin->wm_class);
916 if (w < 0 || w >= scr->workspace_count) {
917 workspace = win_state->state->workspace;
918 if (workspace >= scr->workspace_count)
919 workspace = scr->current_workspace;
920 } else {
921 workspace = w;
923 } else {
924 workspace = scr->current_workspace;
928 /* if we're restarting, restore saved state (from hints).
929 * This will overwrite previous */
931 WSavedState *wstate;
933 if (getSavedState(window, &wstate)) {
934 wwin->flags.shaded = wstate->shaded;
935 wwin->flags.hidden = wstate->hidden;
936 wwin->flags.miniaturized = wstate->miniaturized;
937 wwin->flags.maximized = wstate->maximized;
938 if (wwin->flags.maximized) {
939 wwin->old_geometry.x = wstate->x;
940 wwin->old_geometry.y = wstate->y;
941 wwin->old_geometry.width = wstate->w;
942 wwin->old_geometry.height = wstate->h;
945 workspace = wstate->workspace;
946 } else {
947 wstate = NULL;
950 /* restore window shortcut */
951 if (wstate != NULL || win_state != NULL) {
952 unsigned mask = 0;
954 if (win_state != NULL)
955 mask = win_state->state->window_shortcuts;
957 if (wstate != NULL && mask == 0)
958 mask = wstate->window_shortcuts;
960 if (mask > 0) {
961 int i;
963 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
964 if (mask & (1 << i)) {
965 if (!scr->shortcutWindows[i])
966 scr->shortcutWindows[i] = WMCreateArray(4);
968 WMAddToArray(scr->shortcutWindows[i], wwin);
973 if (wstate != NULL)
974 wfree(wstate);
977 /* don't let transients start miniaturized if their owners are not */
978 if (transientOwner && !transientOwner->flags.miniaturized && wwin->flags.miniaturized && !withdraw) {
979 wwin->flags.miniaturized = 0;
980 if (wwin->wm_hints)
981 wwin->wm_hints->initial_state = NormalState;
984 /* set workspace on which the window starts */
985 if (workspace >= 0) {
986 if (workspace > scr->workspace_count - 1) {
987 workspace = workspace % scr->workspace_count;
989 } else {
990 int w;
992 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
994 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
996 workspace = w;
998 } else {
999 if (wPreferences.open_transients_with_parent && transientOwner) {
1001 workspace = transientOwner->frame->workspace;
1003 } else {
1005 workspace = scr->current_workspace;
1010 /* setup window geometry */
1011 if (win_state && win_state->state->w > 0) {
1012 width = win_state->state->w;
1013 height = win_state->state->h;
1015 wWindowConstrainSize(wwin, &width, &height);
1017 /* do not ask for window placement if the window is
1018 * transient, during startup, if the initial workspace is another one
1019 * or if the window wants to start iconic.
1020 * If geometry was saved, restore it. */
1022 Bool dontBring = False;
1024 if (win_state && win_state->state->w > 0) {
1025 x = win_state->state->x;
1026 y = win_state->state->y;
1027 } else if ((wwin->transient_for == None || wPreferences.window_placement != WPM_MANUAL)
1028 && !scr->flags.startup
1029 && workspace == scr->current_workspace
1030 && !wwin->flags.miniaturized
1031 && !wwin->flags.maximized && !(wwin->normal_hints->flags & (USPosition | PPosition))) {
1033 if (transientOwner && transientOwner->flags.mapped) {
1034 int offs = WMAX(20, 2 * transientOwner->frame->top_width);
1035 WMRect rect;
1036 int head;
1038 x = transientOwner->frame_x +
1039 abs((transientOwner->frame->core->width - width) / 2) + offs;
1040 y = transientOwner->frame_y +
1041 abs((transientOwner->frame->core->height - height) / 3) + offs;
1044 * limit transient windows to be inside their parent's head
1046 rect.pos.x = transientOwner->frame_x;
1047 rect.pos.y = transientOwner->frame_y;
1048 rect.size.width = transientOwner->frame->core->width;
1049 rect.size.height = transientOwner->frame->core->height;
1051 head = wGetHeadForRect(scr, rect);
1052 rect = wGetRectForHead(scr, head);
1054 if (x < rect.pos.x)
1055 x = rect.pos.x;
1056 else if (x + width > rect.pos.x + rect.size.width)
1057 x = rect.pos.x + rect.size.width - width;
1059 if (y < rect.pos.y)
1060 y = rect.pos.y;
1061 else if (y + height > rect.pos.y + rect.size.height)
1062 y = rect.pos.y + rect.size.height - height;
1064 } else {
1065 PlaceWindow(wwin, &x, &y, width, height);
1067 if (wPreferences.window_placement == WPM_MANUAL) {
1068 dontBring = True;
1070 } else if (scr->xine_info.count && (wwin->normal_hints->flags & PPosition)) {
1071 int head, flags;
1072 WMRect rect;
1073 int reposition = 0;
1076 * Make spash screens come out in the center of a head
1077 * trouble is that most splashies never get here
1078 * they are managed trough atoms but god knows where.
1079 * Dan, do you know ? -peter
1081 * Most of them are not managed, they have set
1082 * OverrideRedirect, which means we can't do anything about
1083 * them. -alfredo
1085 #if 0
1086 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width) / 2);
1087 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height) / 2);
1089 if ((unsigned)(x + (width - scr->scr_width) / 2 + 10) < 20 &&
1090 (unsigned)(y + (height - scr->scr_height) / 2 + 10) < 20) {
1092 reposition = 1;
1094 } else
1095 #endif
1098 * xinerama checks for: across head and dead space
1100 rect.pos.x = x;
1101 rect.pos.y = y;
1102 rect.size.width = width;
1103 rect.size.height = height;
1105 head = wGetRectPlacementInfo(scr, rect, &flags);
1107 if (flags & XFLAG_DEAD)
1108 reposition = 1;
1110 if (flags & XFLAG_MULTIPLE)
1111 reposition = 2;
1114 switch (reposition) {
1115 case 1:
1116 head = wGetHeadForPointerLocation(scr);
1117 rect = wGetRectForHead(scr, head);
1119 x = rect.pos.x + (x * rect.size.width) / scr->scr_width;
1120 y = rect.pos.y + (y * rect.size.height) / scr->scr_height;
1121 break;
1123 case 2:
1124 rect = wGetRectForHead(scr, head);
1126 if (x < rect.pos.x)
1127 x = rect.pos.x;
1128 else if (x + width > rect.pos.x + rect.size.width)
1129 x = rect.pos.x + rect.size.width - width;
1131 if (y < rect.pos.y)
1132 y = rect.pos.y;
1133 else if (y + height > rect.pos.y + rect.size.height)
1134 y = rect.pos.y + rect.size.height - height;
1136 break;
1138 default:
1139 break;
1143 if (WFLAGP(wwin, dont_move_off) && dontBring)
1144 wScreenBringInside(scr, &x, &y, width, height);
1147 wNETWMPositionSplash(wwin, &x, &y, width, height);
1149 if (wwin->flags.urgent) {
1150 if (!IS_OMNIPRESENT(wwin))
1151 wwin->flags.omnipresent ^= 1;
1155 * Create frame, borders and do reparenting
1157 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1158 #ifdef XKB_BUTTON_HINT
1159 if (wPreferences.modelock)
1160 foo |= WFF_LANGUAGE_BUTTON;
1161 #endif
1162 if (HAS_TITLEBAR(wwin))
1163 foo |= WFF_TITLEBAR;
1164 if (HAS_RESIZEBAR(wwin))
1165 foo |= WFF_RESIZEBAR;
1166 if (HAS_BORDER(wwin))
1167 foo |= WFF_BORDER;
1169 wwin->frame = wFrameWindowCreate(scr, window_level,
1170 x, y, width, height,
1171 &wPreferences.window_title_clearance, foo,
1172 scr->window_title_texture,
1173 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1175 wwin->frame->flags.is_client_window_frame = 1;
1176 wwin->frame->flags.justification = wPreferences.title_justification;
1178 /* setup button images */
1179 wWindowUpdateButtonImages(wwin);
1181 /* hide unused buttons */
1182 foo = 0;
1183 if (WFLAGP(wwin, no_close_button))
1184 foo |= WFF_RIGHT_BUTTON;
1185 if (WFLAGP(wwin, no_miniaturize_button))
1186 foo |= WFF_LEFT_BUTTON;
1187 #ifdef XKB_BUTTON_HINT
1188 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1189 foo |= WFF_LANGUAGE_BUTTON;
1190 #endif
1191 if (foo != 0)
1192 wFrameWindowHideButton(wwin->frame, foo);
1194 wwin->frame->child = wwin;
1195 wwin->frame->workspace = workspace;
1196 wwin->frame->on_click_left = windowIconifyClick;
1198 #ifdef XKB_BUTTON_HINT
1199 if (wPreferences.modelock)
1200 wwin->frame->on_click_language = windowLanguageClick;
1201 #endif
1203 wwin->frame->on_click_right = windowCloseClick;
1204 wwin->frame->on_dblclick_right = windowCloseDblClick;
1205 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1206 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1207 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1209 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1210 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1211 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1214 int gx, gy;
1216 wClientGetGravityOffsets(wwin, &gx, &gy);
1218 /* if gravity is to the south, account for the border sizes */
1219 if (gy > 0)
1220 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1224 * wWindowConfigure() will init the client window's size
1225 * (wwin->client.{width,height}) and all other geometry
1226 * related variables (frame_x,frame_y)
1228 wWindowConfigure(wwin, x, y, width, height);
1230 /* to make sure the window receives it's new position after reparenting */
1231 wWindowSynthConfigureNotify(wwin);
1234 * Setup descriptors and save window to internal
1235 * lists
1237 if (wwin->main_window != None) {
1238 WApplication *app;
1239 WWindow *leader;
1241 /* Leader windows do not necessary set themselves as leaders.
1242 * If this is the case, point the leader of this window to
1243 * itself */
1244 leader = wWindowFor(wwin->main_window);
1245 if (leader && leader->main_window == None) {
1246 leader->main_window = leader->client_win;
1248 app = wApplicationCreate(wwin);
1249 if (app) {
1250 app->last_workspace = workspace;
1253 * Do application specific stuff, like setting application
1254 * wide attributes.
1257 if (wwin->flags.hidden) {
1258 /* if the window was set to hidden because it was hidden
1259 * in a previous incarnation and that state was restored */
1260 app->flags.hidden = 1;
1261 } else if (app->flags.hidden) {
1262 if (WFLAGP(app->main_window_desc, start_hidden)) {
1263 wwin->flags.hidden = 1;
1264 } else {
1265 wUnhideApplication(app, False, False);
1266 raise = True;
1272 /* setup the frame descriptor */
1273 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1274 wwin->frame->core->descriptor.parent = wwin;
1275 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1277 /* don't let windows go away if we die */
1278 XAddToSaveSet(dpy, window);
1280 XLowerWindow(dpy, window);
1282 /* if window is in this workspace and should be mapped, then map it */
1283 if (!wwin->flags.miniaturized && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1284 && !wwin->flags.hidden && !withdraw) {
1286 /* The following "if" is to avoid crashing of clients that expect
1287 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1288 * after the return from this function.
1290 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1291 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1292 } else {
1293 wClientSetState(wwin, NormalState, None);
1296 #if 0
1297 /* if not auto focus, then map the window under the currently
1298 * focused window */
1299 #define _WIDTH(w) (w)->frame->core->width
1300 #define _HEIGHT(w) (w)->frame->core->height
1301 if (!wPreferences.auto_focus && scr->focused_window
1302 && !scr->flags.startup && !transientOwner && ((wWindowObscuresWindow(wwin, scr->focused_window)
1303 && (_WIDTH(wwin) >
1304 (_WIDTH(scr->focused_window) * 5) / 3
1305 || _HEIGHT(wwin) >
1306 (_HEIGHT(scr->focused_window) * 5) / 3)
1307 && WINDOW_LEVEL(scr->focused_window) ==
1308 WINDOW_LEVEL(wwin))
1309 || wwin->flags.maximized)) {
1310 MoveInStackListUnder(scr->focused_window->frame->core, wwin->frame->core);
1312 #undef _WIDTH
1313 #undef _HEIGHT
1315 #endif
1317 if (wPreferences.superfluous && !wPreferences.no_animations
1318 && !scr->flags.startup && (wwin->transient_for == None || wwin->transient_for == scr->root_win)
1320 * The brain damaged idiotic non-click to focus modes will
1321 * have trouble with this because:
1323 * 1. window is created and mapped by the client
1324 * 2. window is mapped by wmaker in small size
1325 * 3. window is animated to grow to normal size
1326 * 4. this function returns to normal event loop
1327 * 5. eventually, the EnterNotify event that would trigger
1328 * the window focusing (if the mouse is over that window)
1329 * will be processed by wmaker.
1330 * But since this event will be rather delayed
1331 * (step 3 has a large delay) the time when the event ocurred
1332 * and when it is processed, the client that owns that window
1333 * will reject the XSetInputFocus() for it.
1335 && (wPreferences.focus_mode == WKF_CLICK || wPreferences.auto_focus)) {
1336 DoWindowBirth(wwin);
1339 wWindowMap(wwin);
1342 /* setup stacking descriptor */
1343 if (transientOwner) {
1344 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1345 } else {
1346 wwin->frame->core->stacking->child_of = NULL;
1349 if (!scr->focused_window) {
1350 /* first window on the list */
1351 wwin->next = NULL;
1352 wwin->prev = NULL;
1353 scr->focused_window = wwin;
1354 } else {
1355 WWindow *tmp;
1357 /* add window at beginning of focus window list */
1358 tmp = scr->focused_window;
1359 while (tmp->prev)
1360 tmp = tmp->prev;
1361 tmp->prev = wwin;
1362 wwin->next = tmp;
1363 wwin->prev = NULL;
1366 /* raise is set to true if we un-hid the app when this window was born.
1367 * we raise, else old windows of this app will be above this new one. */
1368 if (raise) {
1369 wRaiseFrame(wwin->frame->core);
1372 /* Update name must come after WApplication stuff is done */
1373 wWindowUpdateName(wwin, title);
1374 if (title)
1375 XFree(title);
1377 XUngrabServer(dpy);
1380 * Final preparations before window is ready to go
1382 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1384 if (!wwin->flags.miniaturized && workspace == scr->current_workspace && !wwin->flags.hidden) {
1385 if (((transientOwner && transientOwner->flags.focused)
1386 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1387 wSetFocusTo(scr, wwin);
1389 wWindowResetMouseGrabs(wwin);
1391 if (!WFLAGP(wwin, no_bind_keys))
1392 wWindowSetKeyGrabs(wwin);
1394 WMPostNotificationName(WMNManaged, wwin, NULL);
1395 wColormapInstallForWindow(scr, scr->cmap_window);
1397 /* Setup Notification Observers */
1398 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1400 /* Cleanup temporary stuff */
1401 if (win_state)
1402 wWindowDeleteSavedState(win_state);
1404 /* If the window must be withdrawed, then do it now.
1405 * Must do some optimization, 'though */
1406 if (withdraw) {
1407 wwin->flags.mapped = 0;
1408 wClientSetState(wwin, WithdrawnState, None);
1409 wUnmanageWindow(wwin, True, False);
1410 wwin = NULL;
1413 return wwin;
1416 WWindow *wManageInternalWindow(WScreen *scr, Window window, Window owner,
1417 char *title, int x, int y, int width, int height)
1419 WWindow *wwin;
1420 int foo;
1422 wwin = wWindowCreate();
1424 WMAddNotificationObserver(appearanceObserver, wwin, WNWindowAppearanceSettingsChanged, wwin);
1426 wwin->flags.internal_window = 1;
1428 WSETUFLAG(wwin, omnipresent, 1);
1429 WSETUFLAG(wwin, no_shadeable, 1);
1430 WSETUFLAG(wwin, no_resizable, 1);
1431 WSETUFLAG(wwin, no_miniaturizable, 1);
1433 wwin->focus_mode = WFM_PASSIVE;
1434 wwin->client_win = window;
1435 wwin->screen_ptr = scr;
1436 wwin->transient_for = owner;
1437 wwin->client.x = x;
1438 wwin->client.y = y;
1439 wwin->client.width = width;
1440 wwin->client.height = height;
1441 wwin->frame_x = wwin->client.x;
1442 wwin->frame_y = wwin->client.y;
1444 foo = WFF_RIGHT_BUTTON | WFF_BORDER;
1445 foo |= WFF_TITLEBAR;
1446 #ifdef XKB_BUTTON_HINT
1447 foo |= WFF_LANGUAGE_BUTTON;
1448 #endif
1450 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1451 wwin->frame_x, wwin->frame_y,
1452 width, height,
1453 &wPreferences.window_title_clearance, foo,
1454 scr->window_title_texture,
1455 scr->resizebar_texture, scr->window_title_color, &scr->title_font);
1457 XSaveContext(dpy, window, wWinContext, (XPointer) & wwin->client_descriptor);
1459 wwin->frame->flags.is_client_window_frame = 1;
1460 wwin->frame->flags.justification = wPreferences.title_justification;
1462 wFrameWindowChangeTitle(wwin->frame, title);
1464 /* setup button images */
1465 wWindowUpdateButtonImages(wwin);
1467 /* hide buttons */
1468 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1470 wwin->frame->child = wwin;
1471 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1473 #ifdef XKB_BUTTON_HINT
1474 if (wPreferences.modelock)
1475 wwin->frame->on_click_language = windowLanguageClick;
1476 #endif
1478 wwin->frame->on_click_right = windowCloseClick;
1479 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1480 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1481 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1482 wwin->client.y += wwin->frame->top_width;
1484 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window, 0, wwin->frame->top_width);
1485 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, wwin->client.width, wwin->client.height);
1487 /* setup the frame descriptor */
1488 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1489 wwin->frame->core->descriptor.parent = wwin;
1490 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1492 XLowerWindow(dpy, window);
1493 XMapSubwindows(dpy, wwin->frame->core->window);
1495 /* setup stacking descriptor */
1496 if (wwin->transient_for != None && wwin->transient_for != scr->root_win) {
1497 WWindow *tmp;
1498 tmp = wWindowFor(wwin->transient_for);
1499 if (tmp)
1500 wwin->frame->core->stacking->child_of = tmp->frame->core;
1501 } else {
1502 wwin->frame->core->stacking->child_of = NULL;
1505 if (!scr->focused_window) {
1506 /* first window on the list */
1507 wwin->next = NULL;
1508 wwin->prev = NULL;
1509 scr->focused_window = wwin;
1510 } else {
1511 WWindow *tmp;
1513 /* add window at beginning of focus window list */
1514 tmp = scr->focused_window;
1515 while (tmp->prev)
1516 tmp = tmp->prev;
1517 tmp->prev = wwin;
1518 wwin->next = tmp;
1519 wwin->prev = NULL;
1522 if (wwin->flags.is_gnustep == 0)
1523 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1525 /* if (wPreferences.auto_focus) */
1526 wSetFocusTo(scr, wwin);
1527 wWindowResetMouseGrabs(wwin);
1528 wWindowSetKeyGrabs(wwin);
1530 return wwin;
1534 *----------------------------------------------------------------------
1535 * wUnmanageWindow--
1536 * Removes the frame window from a window and destroys all data
1537 * related to it. The window will be reparented back to the root window
1538 * if restore is True.
1540 * Side effects:
1541 * Everything related to the window is destroyed and the window
1542 * is removed from the window lists. Focus is set to the previous on the
1543 * window list.
1544 *----------------------------------------------------------------------
1546 void wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1548 WCoreWindow *frame = wwin->frame->core;
1549 WWindow *owner = NULL;
1550 WWindow *newFocusedWindow = NULL;
1551 int wasFocused;
1552 WScreen *scr = wwin->screen_ptr;
1554 /* First close attribute editor window if open */
1555 if (wwin->flags.inspector_open) {
1556 wCloseInspectorForWindow(wwin);
1559 /* Close window menu if it's open for this window */
1560 if (wwin->flags.menu_open_for_me) {
1561 CloseWindowMenu(scr);
1564 if (!destroyed) {
1565 if (!wwin->flags.internal_window)
1566 XRemoveFromSaveSet(dpy, wwin->client_win);
1568 XSelectInput(dpy, wwin->client_win, NoEventMask);
1570 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1571 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1574 XUnmapWindow(dpy, frame->window);
1576 XUnmapWindow(dpy, wwin->client_win);
1578 /* deselect window */
1579 wSelectWindow(wwin, False);
1581 /* remove all pending events on window */
1582 /* I think this only matters for autoraise */
1583 if (wPreferences.raise_delay)
1584 WMDeleteTimerWithClientData(wwin->frame->core);
1586 XFlush(dpy);
1588 /* reparent the window back to the root */
1589 if (restore)
1590 wClientRestore(wwin);
1592 if (wwin->transient_for != scr->root_win) {
1593 owner = wWindowFor(wwin->transient_for);
1594 if (owner) {
1595 if (!owner->flags.semi_focused) {
1596 owner = NULL;
1597 } else {
1598 owner->flags.semi_focused = 0;
1603 wasFocused = wwin->flags.focused;
1605 /* remove from window focus list */
1606 if (!wwin->prev && !wwin->next) {
1607 /* was the only window */
1608 scr->focused_window = NULL;
1609 newFocusedWindow = NULL;
1610 } else {
1611 WWindow *tmp;
1613 if (wwin->prev)
1614 wwin->prev->next = wwin->next;
1615 if (wwin->next)
1616 wwin->next->prev = wwin->prev;
1617 else {
1618 scr->focused_window = wwin->prev;
1619 scr->focused_window->next = NULL;
1622 if (wPreferences.focus_mode == WKF_CLICK) {
1624 /* if in click to focus mode and the window
1625 * was a transient, focus the owner window
1627 tmp = NULL;
1628 if (wPreferences.focus_mode == WKF_CLICK) {
1629 tmp = wWindowFor(wwin->transient_for);
1630 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1631 tmp = NULL;
1634 /* otherwise, focus the next one in the focus list */
1635 if (!tmp) {
1636 tmp = scr->focused_window;
1637 while (tmp) { /* look for one in the window list first */
1638 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1639 && (tmp->flags.mapped || tmp->flags.shaded))
1640 break;
1641 tmp = tmp->prev;
1643 if (!tmp) { /* if unsuccessful, choose any focusable window */
1644 tmp = scr->focused_window;
1645 while (tmp) {
1646 if (!WFLAGP(tmp, no_focusable)
1647 && (tmp->flags.mapped || tmp->flags.shaded))
1648 break;
1649 tmp = tmp->prev;
1654 newFocusedWindow = tmp;
1656 } else if (wPreferences.focus_mode == WKF_SLOPPY) {
1657 unsigned int mask;
1658 int foo;
1659 Window bar, win;
1661 /* This is to let the root window get the keyboard input
1662 * if Sloppy focus mode and no other window get focus.
1663 * This way keybindings will not freeze.
1665 tmp = NULL;
1666 if (XQueryPointer(dpy, scr->root_win, &bar, &win, &foo, &foo, &foo, &foo, &mask))
1667 tmp = wWindowFor(win);
1668 if (tmp == wwin)
1669 tmp = NULL;
1670 newFocusedWindow = tmp;
1671 } else {
1672 newFocusedWindow = NULL;
1676 if (!wwin->flags.internal_window)
1677 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1678 #ifdef DEBUG
1679 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win, (unsigned)frame->window);
1680 #endif
1682 if (wasFocused) {
1683 if (newFocusedWindow != owner && owner) {
1684 if (wwin->flags.is_gnustep == 0)
1685 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1687 wSetFocusTo(scr, newFocusedWindow);
1689 wWindowDestroy(wwin);
1690 XFlush(dpy);
1693 void wWindowMap(WWindow *wwin)
1695 XMapWindow(dpy, wwin->frame->core->window);
1696 if (!wwin->flags.shaded) {
1697 /* window will be remapped when getting MapNotify */
1698 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1699 XMapWindow(dpy, wwin->client_win);
1700 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1702 wwin->flags.mapped = 1;
1706 void wWindowUnmap(WWindow *wwin)
1708 wwin->flags.mapped = 0;
1710 /* prevent window withdrawal when getting UnmapNotify */
1711 XSelectInput(dpy, wwin->client_win, wwin->event_mask & ~StructureNotifyMask);
1712 XUnmapWindow(dpy, wwin->client_win);
1713 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1715 XUnmapWindow(dpy, wwin->frame->core->window);
1718 void wWindowFocus(WWindow *wwin, WWindow *owin)
1720 WWindow *nowner;
1721 WWindow *oowner;
1723 #ifdef KEEP_XKB_LOCK_STATUS
1724 if (wPreferences.modelock) {
1725 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1727 #endif /* KEEP_XKB_LOCK_STATUS */
1729 wwin->flags.semi_focused = 0;
1731 if (wwin->flags.is_gnustep == 0)
1732 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1734 wwin->flags.focused = 1;
1736 wWindowResetMouseGrabs(wwin);
1738 WMPostNotificationName(WMNChangedFocus, wwin, (void *)True);
1740 if (owin == wwin || !owin)
1741 return;
1743 nowner = wWindowFor(wwin->transient_for);
1745 /* new window is a transient for the old window */
1746 if (nowner == owin) {
1747 owin->flags.semi_focused = 1;
1748 wWindowUnfocus(nowner);
1749 return;
1752 oowner = wWindowFor(owin->transient_for);
1754 /* new window is owner of old window */
1755 if (wwin == oowner) {
1756 wWindowUnfocus(owin);
1757 return;
1760 if (!nowner) {
1761 wWindowUnfocus(owin);
1762 return;
1765 /* new window has same owner of old window */
1766 if (oowner == nowner) {
1767 /* prevent unfocusing of owner */
1768 oowner->flags.semi_focused = 0;
1769 wWindowUnfocus(owin);
1770 oowner->flags.semi_focused = 1;
1772 return;
1775 /* nowner != NULL && oowner != nowner */
1776 nowner->flags.semi_focused = 1;
1777 wWindowUnfocus(nowner);
1778 wWindowUnfocus(owin);
1781 void wWindowUnfocus(WWindow *wwin)
1783 CloseWindowMenu(wwin->screen_ptr);
1785 if (wwin->flags.is_gnustep == 0)
1786 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused ? WS_PFOCUSED : WS_UNFOCUSED);
1788 if (wwin->transient_for != None && wwin->transient_for != wwin->screen_ptr->root_win) {
1789 WWindow *owner;
1790 owner = wWindowFor(wwin->transient_for);
1791 if (owner && owner->flags.semi_focused) {
1792 owner->flags.semi_focused = 0;
1793 if (owner->flags.mapped || owner->flags.shaded) {
1794 wWindowUnfocus(owner);
1795 wFrameWindowPaint(owner->frame);
1799 wwin->flags.focused = 0;
1800 wWindowResetMouseGrabs(wwin);
1801 WMPostNotificationName(WMNChangedFocus, wwin, (void *)False);
1804 void wWindowUpdateName(WWindow *wwin, char *newTitle)
1806 char *title;
1808 if (!wwin->frame)
1809 return;
1811 wwin->flags.wm_name_changed = 1;
1813 if (!newTitle) {
1814 /* the hint was removed */
1815 title = DEF_WINDOW_TITLE;
1816 } else {
1817 title = newTitle;
1820 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1821 WMPostNotificationName(WMNChangedName, wwin, NULL);
1826 *----------------------------------------------------------------------
1828 * wWindowConstrainSize--
1829 * Constrains size for the client window, taking the maximal size,
1830 * window resize increments and other size hints into account.
1832 * Returns:
1833 * The closest size to what was given that the client window can
1834 * have.
1836 *----------------------------------------------------------------------
1838 void wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
1840 int width = (int)*nwidth;
1841 int height = (int)*nheight;
1842 int winc = 1;
1843 int hinc = 1;
1844 int minW = 1, minH = 1;
1845 int maxW = wwin->screen_ptr->scr_width * 2;
1846 int maxH = wwin->screen_ptr->scr_height * 2;
1847 int minAX = -1, minAY = -1;
1848 int maxAX = -1, maxAY = -1;
1849 int baseW = 0;
1850 int baseH = 0;
1852 if (wwin->normal_hints) {
1853 winc = wwin->normal_hints->width_inc;
1854 hinc = wwin->normal_hints->height_inc;
1855 minW = wwin->normal_hints->min_width;
1856 minH = wwin->normal_hints->min_height;
1857 maxW = wwin->normal_hints->max_width;
1858 maxH = wwin->normal_hints->max_height;
1859 if (wwin->normal_hints->flags & PAspect) {
1860 minAX = wwin->normal_hints->min_aspect.x;
1861 minAY = wwin->normal_hints->min_aspect.y;
1862 maxAX = wwin->normal_hints->max_aspect.x;
1863 maxAY = wwin->normal_hints->max_aspect.y;
1866 baseW = wwin->normal_hints->base_width;
1867 baseH = wwin->normal_hints->base_height;
1870 if (width < minW)
1871 width = minW;
1872 if (height < minH)
1873 height = minH;
1875 if (width > maxW)
1876 width = maxW;
1877 if (height > maxH)
1878 height = maxH;
1880 /* aspect ratio code borrowed from olwm */
1881 if (minAX > 0) {
1882 /* adjust max aspect ratio */
1883 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1884 if (maxAX > maxAY) {
1885 height = (width * maxAY) / maxAX;
1886 if (height > maxH) {
1887 height = maxH;
1888 width = (height * maxAX) / maxAY;
1890 } else {
1891 width = (height * maxAX) / maxAY;
1892 if (width > maxW) {
1893 width = maxW;
1894 height = (width * maxAY) / maxAX;
1899 /* adjust min aspect ratio */
1900 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1901 if (minAX > minAY) {
1902 height = (width * minAY) / minAX;
1903 if (height < minH) {
1904 height = minH;
1905 width = (height * minAX) / minAY;
1907 } else {
1908 width = (height * minAX) / minAY;
1909 if (width < minW) {
1910 width = minW;
1911 height = (width * minAY) / minAX;
1917 if (baseW != 0)
1918 width = (((width - baseW) / winc) * winc) + baseW;
1919 else
1920 width = (((width - minW) / winc) * winc) + minW;
1922 if (baseH != 0)
1923 height = (((height - baseH) / hinc) * hinc) + baseH;
1924 else
1925 height = (((height - minH) / hinc) * hinc) + minH;
1927 /* broken stupid apps may cause preposterous values for these.. */
1928 if (width > 0)
1929 *nwidth = width;
1930 if (height > 0)
1931 *nheight = height;
1934 void wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
1935 unsigned int *width, unsigned int *height)
1937 int baseW = 0, baseH = 0;
1938 int winc = 1, hinc = 1;
1940 if (wwin->normal_hints) {
1941 baseW = wwin->normal_hints->base_width;
1942 baseH = wwin->normal_hints->base_height;
1944 winc = wwin->normal_hints->width_inc;
1945 hinc = wwin->normal_hints->height_inc;
1948 if (*width > maxW)
1949 *width = maxW - (maxW - baseW) % winc;
1951 if (*height > maxH)
1952 *height = maxH - (maxH - baseH) % hinc;
1955 void wWindowChangeWorkspace(WWindow *wwin, int workspace)
1957 WScreen *scr = wwin->screen_ptr;
1958 WApplication *wapp;
1959 int unmap = 0;
1961 if (workspace >= scr->workspace_count || workspace < 0 || workspace == wwin->frame->workspace)
1962 return;
1964 if (workspace != scr->current_workspace) {
1965 /* Sent to other workspace. Unmap window */
1966 if ((wwin->flags.mapped
1967 || wwin->flags.shaded || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
1968 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
1970 wapp = wApplicationOf(wwin->main_window);
1971 if (wapp) {
1972 wapp->last_workspace = workspace;
1974 if (wwin->flags.miniaturized) {
1975 if (wwin->icon) {
1976 XUnmapWindow(dpy, wwin->icon->core->window);
1977 wwin->icon->mapped = 0;
1979 } else {
1980 unmap = 1;
1981 wSetFocusTo(scr, NULL);
1984 } else {
1985 /* brought to current workspace. Map window */
1986 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
1987 if (wwin->icon) {
1988 XMapWindow(dpy, wwin->icon->core->window);
1989 wwin->icon->mapped = 1;
1991 } else if (!wwin->flags.mapped && !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1992 wWindowMap(wwin);
1995 if (!IS_OMNIPRESENT(wwin)) {
1996 int oldWorkspace = wwin->frame->workspace;
1998 wwin->frame->workspace = workspace;
2000 WMPostNotificationName(WMNChangedWorkspace, wwin, (void *)(uintptr_t) oldWorkspace);
2003 if (unmap)
2004 wWindowUnmap(wwin);
2007 void wWindowSynthConfigureNotify(WWindow *wwin)
2009 XEvent sevent;
2011 sevent.type = ConfigureNotify;
2012 sevent.xconfigure.display = dpy;
2013 sevent.xconfigure.event = wwin->client_win;
2014 sevent.xconfigure.window = wwin->client_win;
2016 sevent.xconfigure.x = wwin->client.x;
2017 sevent.xconfigure.y = wwin->client.y;
2018 sevent.xconfigure.width = wwin->client.width;
2019 sevent.xconfigure.height = wwin->client.height;
2021 sevent.xconfigure.border_width = wwin->old_border_width;
2022 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2023 sevent.xconfigure.above = wwin->frame->titlebar->window;
2024 else
2025 sevent.xconfigure.above = None;
2027 sevent.xconfigure.override_redirect = False;
2028 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2029 XFlush(dpy);
2033 *----------------------------------------------------------------------
2034 * wWindowConfigure()
2036 * req_x, req_y: new requested positions for the frame
2037 * req_width, req_height: new requested sizes for the client
2039 * Configures the frame, decorations and client window to the specified
2040 * geometry, whose validity is not checked -- wWindowConstrainSize()
2041 * must be used for that.
2042 * The size parameters are for the client window, but the position is
2043 * for the frame.
2044 * The client window receives a ConfigureNotify event, according
2045 * to what ICCCM says.
2047 * Returns:
2048 * None
2050 * Side effects:
2051 * Window size and position are changed and client window receives
2052 * a ConfigureNotify event.
2053 *----------------------------------------------------------------------
2055 void wWindowConfigure(WWindow *wwin, int req_x, int req_y, int req_width, int req_height)
2057 int synth_notify = False;
2058 int resize;
2060 resize = (req_width != wwin->client.width || req_height != wwin->client.height);
2062 * if the window is being moved but not resized then
2063 * send a synthetic ConfigureNotify
2065 if ((req_x != wwin->frame_x || req_y != wwin->frame_y) && !resize) {
2066 synth_notify = True;
2069 if (WFLAGP(wwin, dont_move_off))
2070 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y, req_width, req_height);
2071 if (resize) {
2072 if (req_width < MIN_WINDOW_SIZE)
2073 req_width = MIN_WINDOW_SIZE;
2074 if (req_height < MIN_WINDOW_SIZE)
2075 req_height = MIN_WINDOW_SIZE;
2077 /* If growing, resize inner part before frame,
2078 * if shrinking, resize frame before.
2079 * This will prevent the frame (that can have a different color)
2080 * to be exposed, causing flicker */
2081 if (req_height > wwin->frame->core->height || req_width > wwin->frame->core->width)
2082 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2084 if (wwin->flags.shaded) {
2085 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, wwin->frame->core->height);
2086 wwin->old_geometry.height = req_height;
2087 } else {
2088 int h;
2090 h = req_height + wwin->frame->top_width + wwin->frame->bottom_width;
2092 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2095 if (!(req_height > wwin->frame->core->height || req_width > wwin->frame->core->width))
2096 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2098 wwin->client.x = req_x;
2099 wwin->client.y = req_y + wwin->frame->top_width;
2100 wwin->client.width = req_width;
2101 wwin->client.height = req_height;
2102 } else {
2103 wwin->client.x = req_x;
2104 wwin->client.y = req_y + wwin->frame->top_width;
2106 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2108 wwin->frame_x = req_x;
2109 wwin->frame_y = req_y;
2110 if (HAS_BORDER(wwin)) {
2111 wwin->client.x += FRAME_BORDER_WIDTH;
2112 wwin->client.y += FRAME_BORDER_WIDTH;
2114 #ifdef SHAPE
2115 if (wShapeSupported && wwin->flags.shaped && resize) {
2116 wWindowSetShape(wwin);
2118 #endif
2120 if (synth_notify)
2121 wWindowSynthConfigureNotify(wwin);
2122 XFlush(dpy);
2125 /* req_x, req_y: new position of the frame */
2126 void wWindowMove(WWindow *wwin, int req_x, int req_y)
2128 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2129 int synth_notify = False;
2131 /* Send a synthetic ConfigureNotify event for every window movement. */
2132 if ((req_x != wwin->frame_x || req_y != wwin->frame_y)) {
2133 synth_notify = True;
2135 #else
2136 /* A single synthetic ConfigureNotify event is sent at the end of
2137 * a completed (opaque) movement in moveres.c */
2138 #endif
2140 if (WFLAGP(wwin, dont_move_off))
2141 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2142 wwin->frame->core->width, wwin->frame->core->height);
2144 wwin->client.x = req_x;
2145 wwin->client.y = req_y + wwin->frame->top_width;
2146 if (HAS_BORDER(wwin)) {
2147 wwin->client.x += FRAME_BORDER_WIDTH;
2148 wwin->client.y += FRAME_BORDER_WIDTH;
2151 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2153 wwin->frame_x = req_x;
2154 wwin->frame_y = req_y;
2156 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2157 if (synth_notify)
2158 wWindowSynthConfigureNotify(wwin);
2159 #endif
2162 void wWindowUpdateButtonImages(WWindow *wwin)
2164 WScreen *scr = wwin->screen_ptr;
2165 Pixmap pixmap, mask;
2166 WFrameWindow *fwin = wwin->frame;
2168 if (!HAS_TITLEBAR(wwin))
2169 return;
2171 /* miniaturize button */
2172 if (!WFLAGP(wwin, no_miniaturize_button)) {
2173 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2174 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2176 if (wwin->wm_gnustep_attr->flags & GSMiniaturizeMaskAttr) {
2177 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2178 } else {
2179 mask = None;
2182 if (fwin->lbutton_image
2183 && (fwin->lbutton_image->image != pixmap || fwin->lbutton_image->mask != mask)) {
2184 wPixmapDestroy(fwin->lbutton_image);
2185 fwin->lbutton_image = NULL;
2188 if (!fwin->lbutton_image) {
2189 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2190 fwin->lbutton_image->client_owned = 1;
2191 fwin->lbutton_image->client_owned_mask = 1;
2193 } else {
2194 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2195 wPixmapDestroy(fwin->lbutton_image);
2197 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2200 #ifdef XKB_BUTTON_HINT
2201 if (!WFLAGP(wwin, no_language_button)) {
2202 if (fwin->languagebutton_image && !fwin->languagebutton_image->shared) {
2203 wPixmapDestroy(fwin->languagebutton_image);
2205 fwin->languagebutton_image = scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2207 #endif
2209 /* close button */
2211 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2212 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2214 if (!WFLAGP(wwin, no_close_button)) {
2215 if (wwin->wm_gnustep_attr && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2216 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2218 if (wwin->wm_gnustep_attr->flags & GSCloseMaskAttr)
2219 mask = wwin->wm_gnustep_attr->close_mask;
2220 else
2221 mask = None;
2223 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2224 || fwin->rbutton_image->mask != mask)) {
2225 wPixmapDestroy(fwin->rbutton_image);
2226 fwin->rbutton_image = NULL;
2229 if (!fwin->rbutton_image) {
2230 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2231 fwin->rbutton_image->client_owned = 1;
2232 fwin->rbutton_image->client_owned_mask = 1;
2235 } else if (WFLAGP(wwin, kill_close)) {
2237 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2238 wPixmapDestroy(fwin->rbutton_image);
2240 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2242 } else if (MGFLAGP(wwin, broken_close)) {
2244 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2245 wPixmapDestroy(fwin->rbutton_image);
2247 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2249 } else {
2251 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2252 wPixmapDestroy(fwin->rbutton_image);
2254 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2258 /* force buttons to be redrawn */
2259 fwin->flags.need_texture_change = 1;
2260 wFrameWindowPaint(fwin);
2264 *---------------------------------------------------------------------------
2265 * wWindowConfigureBorders--
2266 * Update window border configuration according to attribute flags.
2268 *---------------------------------------------------------------------------
2270 void wWindowConfigureBorders(WWindow *wwin)
2272 if (wwin->frame) {
2273 int flags;
2274 int newy, oldh;
2276 flags = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
2278 #ifdef XKB_BUTTON_HINT
2279 flags |= WFF_LANGUAGE_BUTTON;
2280 #endif
2282 if (HAS_TITLEBAR(wwin))
2283 flags |= WFF_TITLEBAR;
2284 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2285 flags |= WFF_RESIZEBAR;
2286 if (HAS_BORDER(wwin))
2287 flags |= WFF_BORDER;
2288 if (wwin->flags.shaded)
2289 flags |= WFF_IS_SHADED;
2291 oldh = wwin->frame->top_width;
2292 wFrameWindowUpdateBorders(wwin->frame, flags);
2293 if (oldh != wwin->frame->top_width) {
2294 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2296 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2297 wWindowConfigure(wwin, wwin->frame_x, newy, wwin->client.width, wwin->client.height);
2300 flags = 0;
2301 if (!WFLAGP(wwin, no_miniaturize_button)
2302 && wwin->frame->flags.hide_left_button)
2303 flags |= WFF_LEFT_BUTTON;
2305 #ifdef XKB_BUTTON_HINT
2306 if (!WFLAGP(wwin, no_language_button)
2307 && wwin->frame->flags.hide_language_button) {
2308 flags |= WFF_LANGUAGE_BUTTON;
2310 #endif
2312 if (!WFLAGP(wwin, no_close_button)
2313 && wwin->frame->flags.hide_right_button)
2314 flags |= WFF_RIGHT_BUTTON;
2316 if (flags != 0) {
2317 wWindowUpdateButtonImages(wwin);
2318 wFrameWindowShowButton(wwin->frame, flags);
2321 flags = 0;
2322 if (WFLAGP(wwin, no_miniaturize_button)
2323 && !wwin->frame->flags.hide_left_button)
2324 flags |= WFF_LEFT_BUTTON;
2326 #ifdef XKB_BUTTON_HINT
2327 if (WFLAGP(wwin, no_language_button)
2328 && !wwin->frame->flags.hide_language_button)
2329 flags |= WFF_LANGUAGE_BUTTON;
2330 #endif
2332 if (WFLAGP(wwin, no_close_button)
2333 && !wwin->frame->flags.hide_right_button)
2334 flags |= WFF_RIGHT_BUTTON;
2336 if (flags != 0)
2337 wFrameWindowHideButton(wwin->frame, flags);
2339 #ifdef SHAPE
2340 if (wShapeSupported && wwin->flags.shaped) {
2341 wWindowSetShape(wwin);
2343 #endif
2347 void wWindowSaveState(WWindow * wwin)
2349 CARD32 data[10];
2350 int i;
2352 memset(data, 0, sizeof(CARD32) * 10);
2353 data[0] = wwin->frame->workspace;
2354 data[1] = wwin->flags.miniaturized;
2355 data[2] = wwin->flags.shaded;
2356 data[3] = wwin->flags.hidden;
2357 data[4] = wwin->flags.maximized;
2358 if (wwin->flags.maximized == 0) {
2359 data[5] = wwin->frame_x;
2360 data[6] = wwin->frame_y;
2361 data[7] = wwin->frame->core->width;
2362 data[8] = wwin->frame->core->height;
2363 } else {
2364 data[5] = wwin->old_geometry.x;
2365 data[6] = wwin->old_geometry.y;
2366 data[7] = wwin->old_geometry.width;
2367 data[8] = wwin->old_geometry.height;
2370 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2371 if (wwin->screen_ptr->shortcutWindows[i] &&
2372 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2373 data[9] |= 1 << i;
2375 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2376 _XA_WINDOWMAKER_STATE, 32, PropModeReplace, (unsigned char *)data, 10);
2379 static int getSavedState(Window window, WSavedState ** state)
2381 Atom type_ret;
2382 int fmt_ret;
2383 unsigned long nitems_ret;
2384 unsigned long bytes_after_ret;
2385 CARD32 *data;
2387 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2388 True, _XA_WINDOWMAKER_STATE,
2389 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2390 (unsigned char **)&data) != Success || !data)
2391 return 0;
2393 *state = wmalloc(sizeof(WSavedState));
2395 (*state)->workspace = data[0];
2396 (*state)->miniaturized = data[1];
2397 (*state)->shaded = data[2];
2398 (*state)->hidden = data[3];
2399 (*state)->maximized = data[4];
2400 (*state)->x = data[5];
2401 (*state)->y = data[6];
2402 (*state)->w = data[7];
2403 (*state)->h = data[8];
2404 (*state)->window_shortcuts = data[9];
2406 XFree(data);
2408 if (*state && type_ret == _XA_WINDOWMAKER_STATE)
2409 return 1;
2410 else
2411 return 0;
2414 #ifdef SHAPE
2415 void wWindowClearShape(WWindow * wwin)
2417 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2418 0, wwin->frame->top_width, None, ShapeSet);
2419 XFlush(dpy);
2422 void wWindowSetShape(WWindow * wwin)
2424 XRectangle rect[2];
2425 int count;
2426 #ifdef OPTIMIZE_SHAPE
2427 XRectangle *rects;
2428 XRectangle *urec;
2429 int ordering;
2431 /* only shape is the client's */
2432 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2433 goto alt_code;
2436 /* Get array of rectangles describing the shape mask */
2437 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding, &count, &ordering);
2438 if (!rects) {
2439 goto alt_code;
2442 urec = malloc(sizeof(XRectangle) * (count + 2));
2443 if (!urec) {
2444 XFree(rects);
2445 goto alt_code;
2448 /* insert our decoration rectangles in the rect list */
2449 memcpy(urec, rects, sizeof(XRectangle) * count);
2450 XFree(rects);
2452 if (HAS_TITLEBAR(wwin)) {
2453 urec[count].x = -1;
2454 urec[count].y = -1 - wwin->frame->top_width;
2455 urec[count].width = wwin->frame->core->width + 2;
2456 urec[count].height = wwin->frame->top_width + 1;
2457 count++;
2459 if (HAS_RESIZEBAR(wwin)) {
2460 urec[count].x = -1;
2461 urec[count].y = wwin->frame->core->height - wwin->frame->bottom_width - wwin->frame->top_width;
2462 urec[count].width = wwin->frame->core->width + 2;
2463 urec[count].height = wwin->frame->bottom_width + 1;
2464 count++;
2467 /* shape our frame window */
2468 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2469 0, wwin->frame->top_width, urec, count, ShapeSet, Unsorted);
2470 XFlush(dpy);
2471 wfree(urec);
2472 return;
2474 alt_code:
2475 #endif /* OPTIMIZE_SHAPE */
2476 count = 0;
2477 if (HAS_TITLEBAR(wwin)) {
2478 rect[count].x = -1;
2479 rect[count].y = -1;
2480 rect[count].width = wwin->frame->core->width + 2;
2481 rect[count].height = wwin->frame->top_width + 1;
2482 count++;
2484 if (HAS_RESIZEBAR(wwin)) {
2485 rect[count].x = -1;
2486 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2487 rect[count].width = wwin->frame->core->width + 2;
2488 rect[count].height = wwin->frame->bottom_width + 1;
2489 count++;
2491 if (count > 0) {
2492 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2493 0, 0, rect, count, ShapeSet, Unsorted);
2495 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2496 0, wwin->frame->top_width, wwin->client_win,
2497 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2498 XFlush(dpy);
2500 #endif /* SHAPE */
2502 /* ====================================================================== */
2504 static FocusMode getFocusMode(WWindow * wwin)
2506 FocusMode mode;
2508 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2509 if (wwin->wm_hints->input == True) {
2510 if (wwin->protocols.TAKE_FOCUS)
2511 mode = WFM_LOCALLY_ACTIVE;
2512 else
2513 mode = WFM_PASSIVE;
2514 } else {
2515 if (wwin->protocols.TAKE_FOCUS)
2516 mode = WFM_GLOBALLY_ACTIVE;
2517 else
2518 mode = WFM_NO_INPUT;
2520 } else {
2521 mode = WFM_PASSIVE;
2523 return mode;
2526 void wWindowSetKeyGrabs(WWindow * wwin)
2528 int i;
2529 WShortKey *key;
2531 for (i = 0; i < WKBD_LAST; i++) {
2532 key = &wKeyBindings[i];
2534 if (key->keycode == 0)
2535 continue;
2536 if (key->modifier != AnyModifier) {
2537 XGrabKey(dpy, key->keycode, key->modifier | LockMask,
2538 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2539 #ifdef NUMLOCK_HACK
2540 /* Also grab all modifier combinations possible that include,
2541 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2542 * work even if the NumLock/ScrollLock key is on.
2544 wHackedGrabKey(key->keycode, key->modifier,
2545 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2546 #endif
2548 XGrabKey(dpy, key->keycode, key->modifier,
2549 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2552 wRootMenuBindShortcuts(wwin->frame->core->window);
2555 void wWindowResetMouseGrabs(WWindow * wwin)
2557 /* Mouse grabs can't be done on the client window because of
2558 * ICCCM and because clients that try to do the same will crash.
2560 * But there is a problem wich makes tbar buttons of unfocused
2561 * windows not usable as the click goes to the frame window instead
2562 * of the button itself. Must figure a way to fix that.
2565 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2567 if (!WFLAGP(wwin, no_bind_mouse)) {
2568 /* grabs for Meta+drag */
2569 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2570 True, ButtonPressMask | ButtonReleaseMask,
2571 GrabModeSync, GrabModeAsync, None, None);
2574 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2575 && !wwin->flags.is_gnustep) {
2576 /* the passive grabs to focus the window */
2577 /* if (wPreferences.focus_mode == WKF_CLICK) */
2578 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2579 True, ButtonPressMask | ButtonReleaseMask, GrabModeSync, GrabModeAsync, None, None);
2581 XFlush(dpy);
2584 void wWindowUpdateGNUstepAttr(WWindow * wwin, GNUstepWMAttributes * attr)
2586 if (attr->flags & GSExtraFlagsAttr) {
2587 if (MGFLAGP(wwin, broken_close) != (attr->extra_flags & GSDocumentEditedFlag)) {
2588 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2589 wWindowUpdateButtonImages(wwin);
2594 WMagicNumber wWindowAddSavedState(char *instance, char *class, char *command, pid_t pid, WSavedState * state)
2596 WWindowState *wstate;
2598 wstate = malloc(sizeof(WWindowState));
2599 if (!wstate)
2600 return 0;
2602 memset(wstate, 0, sizeof(WWindowState));
2603 wstate->pid = pid;
2604 if (instance)
2605 wstate->instance = wstrdup(instance);
2606 if (class)
2607 wstate->class = wstrdup(class);
2608 if (command)
2609 wstate->command = wstrdup(command);
2610 wstate->state = state;
2612 wstate->next = windowState;
2613 windowState = wstate;
2615 #ifdef DEBUG
2616 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
2617 #endif
2619 return wstate;
2622 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2624 WMagicNumber wWindowGetSavedState(Window win)
2626 char *instance, *class, *command = NULL;
2627 WWindowState *wstate = windowState;
2629 if (!wstate)
2630 return NULL;
2632 command = GetCommandForWindow(win);
2633 if (!command)
2634 return NULL;
2636 if (PropGetWMClass(win, &class, &instance)) {
2637 while (wstate) {
2638 if (SAME(instance, wstate->instance) &&
2639 SAME(class, wstate->class) && SAME(command, wstate->command)) {
2640 break;
2642 wstate = wstate->next;
2644 } else {
2645 wstate = NULL;
2648 #ifdef DEBUG
2649 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance, class, command);
2650 #endif
2652 if (command)
2653 wfree(command);
2654 if (instance)
2655 XFree(instance);
2656 if (class)
2657 XFree(class);
2659 return wstate;
2662 void wWindowDeleteSavedState(WMagicNumber id)
2664 WWindowState *tmp, *wstate = (WWindowState *) id;
2666 if (!wstate || !windowState)
2667 return;
2669 tmp = windowState;
2670 if (tmp == wstate) {
2671 windowState = wstate->next;
2672 #ifdef DEBUG
2673 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2674 wstate, wstate->instance, wstate->class, wstate->command);
2675 #endif
2676 if (wstate->instance)
2677 wfree(wstate->instance);
2678 if (wstate->class)
2679 wfree(wstate->class);
2680 if (wstate->command)
2681 wfree(wstate->command);
2682 wfree(wstate->state);
2683 wfree(wstate);
2684 } else {
2685 while (tmp->next) {
2686 if (tmp->next == wstate) {
2687 tmp->next = wstate->next;
2688 #ifdef DEBUG
2689 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2690 wstate, wstate->instance, wstate->class, wstate->command);
2691 #endif
2692 if (wstate->instance)
2693 wfree(wstate->instance);
2694 if (wstate->class)
2695 wfree(wstate->class);
2696 if (wstate->command)
2697 wfree(wstate->command);
2698 wfree(wstate->state);
2699 wfree(wstate);
2700 break;
2702 tmp = tmp->next;
2707 void wWindowDeleteSavedStatesForPID(pid_t pid)
2709 WWindowState *tmp, *wstate;
2711 if (!windowState)
2712 return;
2714 tmp = windowState;
2715 if (tmp->pid == pid) {
2716 wstate = windowState;
2717 windowState = tmp->next;
2718 #ifdef DEBUG
2719 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2720 wstate, wstate->instance, wstate->class, wstate->command);
2721 #endif
2722 if (wstate->instance)
2723 wfree(wstate->instance);
2724 if (wstate->class)
2725 wfree(wstate->class);
2726 if (wstate->command)
2727 wfree(wstate->command);
2728 wfree(wstate->state);
2729 wfree(wstate);
2730 } else {
2731 while (tmp->next) {
2732 if (tmp->next->pid == pid) {
2733 wstate = tmp->next;
2734 tmp->next = wstate->next;
2735 #ifdef DEBUG
2736 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2737 wstate, wstate->instance, wstate->class, wstate->command);
2738 #endif
2739 if (wstate->instance)
2740 wfree(wstate->instance);
2741 if (wstate->class)
2742 wfree(wstate->class);
2743 if (wstate->command)
2744 wfree(wstate->command);
2745 wfree(wstate->state);
2746 wfree(wstate);
2747 break;
2749 tmp = tmp->next;
2754 void wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2756 if (wwin->flags.omnipresent == flag)
2757 return;
2759 wwin->flags.omnipresent = flag;
2760 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2763 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2765 WWindow *wwin = data;
2767 #ifndef NUMLOCK_HACK
2768 if ((event->xbutton.state & ValidModMask)
2769 != (event->xbutton.state & ~LockMask)) {
2770 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2771 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2773 #endif
2775 event->xbutton.state &= ValidModMask;
2777 CloseWindowMenu(wwin->screen_ptr);
2779 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2780 && !WFLAGP(wwin, no_focusable)) {
2781 wSetFocusTo(wwin->screen_ptr, wwin);
2784 if (event->xbutton.button == Button1)
2785 wRaiseFrame(wwin->frame->core);
2787 if (event->xbutton.window != wwin->frame->resizebar->window) {
2788 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2789 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2790 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2791 #ifdef DEBUG0
2792 wwarning("pointer grab failed for window move");
2793 #endif
2794 return;
2798 if (event->xbutton.state & MOD_MASK) {
2799 /* move the window */
2800 wMouseMoveWindow(wwin, event);
2801 XUngrabPointer(dpy, CurrentTime);
2802 } else {
2803 wMouseResizeWindow(wwin, event);
2804 XUngrabPointer(dpy, CurrentTime);
2808 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2810 WWindow *wwin = data;
2812 event->xbutton.state &= ValidModMask;
2814 if (event->xbutton.button == Button1) {
2815 if (event->xbutton.state == 0) {
2816 if (!WFLAGP(wwin, no_shadeable)) {
2817 /* shade window */
2818 if (wwin->flags.shaded)
2819 wUnshadeWindow(wwin);
2820 else
2821 wShadeWindow(wwin);
2823 } else {
2824 int dir = 0;
2826 if (event->xbutton.state & ControlMask)
2827 dir |= MAX_VERTICAL;
2829 if (event->xbutton.state & ShiftMask) {
2830 dir |= MAX_HORIZONTAL;
2831 if (!(event->xbutton.state & ControlMask))
2832 wSelectWindow(wwin, !wwin->flags.selected);
2835 /* maximize window */
2836 if (dir != 0 && IS_RESIZABLE(wwin)) {
2837 int ndir = dir ^ wwin->flags.maximized;
2839 if (ndir != 0)
2840 wMaximizeWindow(wwin, ndir);
2841 else
2842 wUnmaximizeWindow(wwin);
2845 } else if (event->xbutton.button == Button3) {
2846 if (event->xbutton.state & MOD_MASK) {
2847 wHideOtherApplications(wwin);
2849 } else if (event->xbutton.button == Button2) {
2850 wSelectWindow(wwin, !wwin->flags.selected);
2851 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
2852 wShadeWindow(wwin);
2853 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
2854 wUnshadeWindow(wwin);
2858 static void frameMouseDown(WObjDescriptor *desc, XEvent *event)
2860 WWindow *wwin = desc->parent;
2861 unsigned int new_width;
2862 unsigned int new_height;
2863 unsigned int resize_width_increment = 0;
2864 unsigned int resize_height_increment = 0;
2866 if (wwin->normal_hints) {
2867 resize_width_increment = wwin->normal_hints->width_inc;
2868 resize_height_increment = wwin->normal_hints->height_inc;
2870 if (resize_width_increment <= 1 && resize_height_increment <= 1) {
2871 resize_width_increment = wPreferences.resize_increment;
2872 resize_height_increment = wPreferences.resize_increment;
2875 event->xbutton.state &= ValidModMask;
2877 CloseWindowMenu(wwin->screen_ptr);
2879 if (!(event->xbutton.state & ControlMask) && !WFLAGP(wwin, no_focusable))
2880 wSetFocusTo(wwin->screen_ptr, wwin);
2882 if (event->xbutton.button == Button1)
2883 wRaiseFrame(wwin->frame->core);
2885 if (event->xbutton.state & MOD_MASK) {
2886 /* move the window */
2887 if (XGrabPointer(dpy, wwin->client_win, False,
2888 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2889 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2890 #ifdef DEBUG0
2891 wwarning("pointer grab failed for window move");
2892 #endif
2893 return;
2895 if (event->xbutton.button == Button3) {
2896 wMouseResizeWindow(wwin, event);
2897 } else if (event->xbutton.button == Button4) {
2898 new_width = wwin->client.width - resize_width_increment;
2899 new_height = wwin->client.height - resize_height_increment;
2900 wWindowConstrainSize(wwin, &new_width,&new_height);
2901 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, new_height);
2902 } else if (event->xbutton.button == Button5) {
2903 new_width = wwin->client.width + resize_width_increment;
2904 new_height = wwin->client.height + resize_height_increment;
2905 wWindowConstrainSize(wwin, &new_width,&new_height);
2906 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y, new_width, new_height);
2907 } else if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2908 wMouseMoveWindow(wwin, event);
2910 XUngrabPointer(dpy, CurrentTime);
2914 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2916 WWindow *wwin = (WWindow *) data;
2918 #ifndef NUMLOCK_HACK
2919 if ((event->xbutton.state & ValidModMask)
2920 != (event->xbutton.state & ~LockMask)) {
2921 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"
2922 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2924 #endif
2925 event->xbutton.state &= ValidModMask;
2927 CloseWindowMenu(wwin->screen_ptr);
2929 if (wPreferences.focus_mode == WKF_CLICK && !(event->xbutton.state & ControlMask)
2930 && !WFLAGP(wwin, no_focusable)) {
2931 wSetFocusTo(wwin->screen_ptr, wwin);
2934 if (event->xbutton.button == Button1 || event->xbutton.button == Button2) {
2936 if (event->xbutton.button == Button1) {
2937 if (event->xbutton.state & MOD_MASK) {
2938 wLowerFrame(wwin->frame->core);
2939 } else {
2940 wRaiseFrame(wwin->frame->core);
2943 if ((event->xbutton.state & ShiftMask)
2944 && !(event->xbutton.state & ControlMask)) {
2945 wSelectWindow(wwin, !wwin->flags.selected);
2946 return;
2948 if (event->xbutton.window != wwin->frame->titlebar->window
2949 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2950 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2951 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2952 #ifdef DEBUG0
2953 wwarning("pointer grab failed for window move");
2954 #endif
2955 return;
2958 /* move the window */
2959 wMouseMoveWindow(wwin, event);
2961 XUngrabPointer(dpy, CurrentTime);
2962 } else if (event->xbutton.button == Button3 && event->xbutton.state == 0
2963 && !wwin->flags.internal_window && !WCHECK_STATE(WSTATE_MODAL)) {
2964 WObjDescriptor *desc;
2966 if (event->xbutton.window != wwin->frame->titlebar->window
2967 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2968 ButtonMotionMask | ButtonReleaseMask | ButtonPressMask,
2969 GrabModeAsync, GrabModeAsync, None, None, CurrentTime) != GrabSuccess) {
2970 #ifdef DEBUG0
2971 wwarning("pointer grab failed for window move");
2972 #endif
2973 return;
2976 OpenWindowMenu(wwin, event->xbutton.x_root, wwin->frame_y + wwin->frame->top_width, False);
2978 /* allow drag select */
2979 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2980 event->xany.send_event = True;
2981 (*desc->handle_mousedown) (desc, event);
2983 XUngrabPointer(dpy, CurrentTime);
2987 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2989 WWindow *wwin = data;
2991 event->xbutton.state &= ValidModMask;
2993 CloseWindowMenu(wwin->screen_ptr);
2995 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
2996 return;
2998 /* if control-click, kill the client */
2999 if (event->xbutton.state & ControlMask) {
3000 wClientKill(wwin);
3001 } else {
3002 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state == 0) {
3003 /* send delete message */
3004 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3009 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3011 WWindow *wwin = data;
3013 CloseWindowMenu(wwin->screen_ptr);
3015 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3016 return;
3018 /* send delete message */
3019 if (wwin->protocols.DELETE_WINDOW) {
3020 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3021 } else {
3022 wClientKill(wwin);
3026 #ifdef XKB_BUTTON_HINT
3027 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3029 WWindow *wwin = data;
3030 WFrameWindow *fwin = wwin->frame;
3031 WScreen *scr = fwin->screen_ptr;
3032 XkbStateRec staterec;
3033 int tl;
3035 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3036 return;
3037 tl = wwin->frame->languagemode;
3038 wwin->frame->languagemode = wwin->frame->last_languagemode;
3039 wwin->frame->last_languagemode = tl;
3040 wSetFocusTo(scr, wwin);
3041 wwin->frame->languagebutton_image =
3042 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 + wwin->frame->languagemode];
3043 wFrameWindowUpdateLanguageButton(wwin->frame);
3044 if (event->xbutton.button == Button3)
3045 return;
3046 wRaiseFrame(fwin->core);
3048 #endif
3050 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3052 WWindow *wwin = data;
3054 event->xbutton.state &= ValidModMask;
3056 CloseWindowMenu(wwin->screen_ptr);
3058 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3059 return;
3061 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state == 0) {
3062 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW, LastTimestamp);
3063 } else {
3064 WApplication *wapp;
3065 if ((event->xbutton.state & ControlMask) || (event->xbutton.button == Button3)) {
3067 wapp = wApplicationOf(wwin->main_window);
3068 if (wapp && !WFLAGP(wwin, no_appicon))
3069 wHideApplication(wapp);
3070 } else if (event->xbutton.state == 0) {
3071 wIconifyWindow(wwin);