wmaker: Fix compiler warnings about pointer <--> integer conversion
[wmaker-crm.git] / src / window.c
blob6cefe6c4c3d1eb10ded3ae5e324ddd06dec802a4
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"
60 #ifdef MWM_HINTS
61 # include "motif.h"
62 #endif
63 #ifdef NETWM_HINTS
64 # include "wmspec.h"
65 #endif
67 /****** Global Variables ******/
69 extern WShortKey wKeyBindings[WKBD_LAST];
71 #ifdef SHAPE
72 extern Bool wShapeSupported;
73 #endif
75 /* contexts */
76 extern XContext wWinContext;
78 /* cursors */
79 extern Cursor wCursor[WCUR_LAST];
81 /* protocol atoms */
82 extern Atom _XA_WM_DELETE_WINDOW;
83 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
85 extern Atom _XA_WINDOWMAKER_STATE;
87 extern WPreferences wPreferences;
89 #define MOD_MASK wPreferences.modifier_mask
91 extern Time LastTimestamp;
93 /* superfluous... */
94 extern void DoWindowBirth(WWindow*);
98 /***** Local Stuff *****/
101 static WWindowState *windowState=NULL;
105 /* local functions */
106 static FocusMode getFocusMode(WWindow *wwin);
108 static int getSavedState(Window window, WSavedState **state);
110 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
112 /* event handlers */
115 /* frame window (during window grabs) */
116 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
118 /* close button */
119 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
120 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
122 /* iconify button */
123 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
125 #ifdef XKB_BUTTON_HINT
126 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
127 #endif
129 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
130 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
132 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
135 /****** Notification Observers ******/
137 static void
138 appearanceObserver(void *self, WMNotification *notif)
140 WWindow *wwin = (WWindow*)self;
141 int flags = (int)(uintptr_t)WMGetNotificationClientData(notif);
143 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
144 return;
146 if (flags & WFontSettings) {
147 wWindowConfigureBorders(wwin);
148 if(wwin->flags.shaded) {
149 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
150 wwin->frame->top_width - 1);
152 wwin->client.y = wwin->frame_y - wwin->client.height
153 + wwin->frame->top_width;
154 wWindowSynthConfigureNotify(wwin);
157 if (flags & WTextureSettings) {
158 wwin->frame->flags.need_texture_remake = 1;
160 if (flags & (WTextureSettings | WColorSettings)) {
161 if (wwin->frame->titlebar)
162 XClearWindow(dpy, wwin->frame->titlebar->window);
164 wFrameWindowPaint(wwin->frame);
168 /************************************/
170 WWindow*
171 wWindowFor(Window window)
173 WObjDescriptor *desc;
175 if (window==None)
176 return NULL;
178 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
179 return NULL;
181 if (desc->parent_type==WCLASS_WINDOW)
182 return desc->parent;
183 else if (desc->parent_type==WCLASS_FRAME) {
184 WFrameWindow *frame = (WFrameWindow*)desc->parent;
185 if (frame->flags.is_client_window_frame)
186 return frame->child;
189 return NULL;
193 WWindow*
194 wWindowCreate()
196 WWindow *wwin;
198 wwin = wmalloc(sizeof(WWindow));
199 wretain(wwin);
201 memset(wwin, 0, sizeof(WWindow));
203 wwin->client_descriptor.handle_mousedown = frameMouseDown;
204 wwin->client_descriptor.parent = wwin;
205 wwin->client_descriptor.self = wwin;
206 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
208 return wwin;
212 void
213 wWindowDestroy(WWindow *wwin)
215 int i;
217 if (wwin->screen_ptr->cmap_window == wwin) {
218 wwin->screen_ptr->cmap_window = NULL;
221 WMRemoveNotificationObserver(wwin);
223 wwin->flags.destroyed = 1;
225 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
226 if (!wwin->screen_ptr->shortcutWindows[i])
227 continue;
229 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
231 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
232 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
233 wwin->screen_ptr->shortcutWindows[i] = NULL;
237 if (wwin->fake_group && wwin->fake_group->retainCount>0) {
238 wwin->fake_group->retainCount--;
239 if (wwin->fake_group->retainCount==0 && wwin->fake_group->leader!=None) {
240 XDestroyWindow(dpy, wwin->fake_group->leader);
241 wwin->fake_group->leader = None;
242 wwin->fake_group->origLeader = None;
243 XFlush(dpy);
247 if (wwin->normal_hints)
248 XFree(wwin->normal_hints);
250 if (wwin->wm_hints)
251 XFree(wwin->wm_hints);
253 if (wwin->wm_instance)
254 XFree(wwin->wm_instance);
256 if (wwin->wm_class)
257 XFree(wwin->wm_class);
259 if (wwin->wm_gnustep_attr)
260 wfree(wwin->wm_gnustep_attr);
262 if (wwin->cmap_windows)
263 XFree(wwin->cmap_windows);
265 XDeleteContext(dpy, wwin->client_win, wWinContext);
267 if (wwin->frame)
268 wFrameWindowDestroy(wwin->frame);
270 if (wwin->icon) {
271 RemoveFromStackList(wwin->icon->core);
272 wIconDestroy(wwin->icon);
273 if (wPreferences.auto_arrange_icons)
274 wArrangeIcons(wwin->screen_ptr, True);
277 #ifdef NETWM_HINTS
278 if (wwin->net_icon_image)
279 RReleaseImage(wwin->net_icon_image);
280 #endif
282 wrelease(wwin);
286 static void
287 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
289 if (gs_hints->flags & GSWindowStyleAttr) {
290 if (gs_hints->window_style == WMBorderlessWindowMask) {
291 wwin->client_flags.no_border = 1;
292 wwin->client_flags.no_titlebar = 1;
293 wwin->client_flags.no_closable = 1;
294 wwin->client_flags.no_miniaturizable = 1;
295 wwin->client_flags.no_resizable = 1;
296 wwin->client_flags.no_close_button = 1;
297 wwin->client_flags.no_miniaturize_button = 1;
298 wwin->client_flags.no_resizebar = 1;
299 } else {
300 wwin->client_flags.no_close_button =
301 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
303 wwin->client_flags.no_closable =
304 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
306 wwin->client_flags.no_miniaturize_button =
307 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
309 wwin->client_flags.no_miniaturizable =
310 wwin->client_flags.no_miniaturize_button;
312 wwin->client_flags.no_resizebar =
313 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
315 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
317 /* these attributes supposedly imply in the existence
318 * of a titlebar */
319 if (gs_hints->window_style & (WMResizableWindowMask|
320 WMClosableWindowMask|
321 WMMiniaturizableWindowMask)) {
322 wwin->client_flags.no_titlebar = 0;
323 } else {
324 wwin->client_flags.no_titlebar =
325 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
329 } else {
330 /* setup the defaults */
331 wwin->client_flags.no_border = 0;
332 wwin->client_flags.no_titlebar = 0;
333 wwin->client_flags.no_closable = 0;
334 wwin->client_flags.no_miniaturizable = 0;
335 wwin->client_flags.no_resizable = 0;
336 wwin->client_flags.no_close_button = 0;
337 wwin->client_flags.no_miniaturize_button = 0;
338 wwin->client_flags.no_resizebar = 0;
340 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
341 wwin->client_flags.no_appicon = 1;
347 void
348 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
349 WWindowAttributes *mask)
351 if (wflags->no_appicon && mask->no_appicon)
352 wflags->emulate_appicon = 0;
354 if (wwin->main_window!=None) {
355 WApplication *wapp = wApplicationOf(wwin->main_window);
356 if (wapp && !wapp->flags.emulated)
357 wflags->emulate_appicon = 0;
360 if (wwin->transient_for!=None
361 && wwin->transient_for!=wwin->screen_ptr->root_win)
362 wflags->emulate_appicon = 0;
364 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
365 wflags->sunken = 0;
370 void
371 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
373 WScreen *scr = wwin->screen_ptr;
375 /* sets global default stuff */
376 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
377 &wwin->client_flags, NULL, True);
379 * Decoration setting is done in this precedence (lower to higher)
380 * - use global default in the resource database
381 * - guess some settings
382 * - use GNUstep/external window attributes
383 * - set hints specified for the app in the resource DB
386 WSETUFLAG(wwin, broken_close, 0);
388 if (wwin->protocols.DELETE_WINDOW)
389 WSETUFLAG(wwin, kill_close, 0);
390 else
391 WSETUFLAG(wwin, kill_close, 1);
393 /* transients can't be iconified or maximized */
394 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
395 WSETUFLAG(wwin, no_miniaturizable, 1);
396 WSETUFLAG(wwin, no_miniaturize_button, 1);
399 /* if the window can't be resized, remove the resizebar */
400 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
401 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
402 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
403 WSETUFLAG(wwin, no_resizable, 1);
404 WSETUFLAG(wwin, no_resizebar, 1);
407 /* set GNUstep window attributes */
408 if (wwin->wm_gnustep_attr) {
409 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
411 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
413 *level = wwin->wm_gnustep_attr->window_level;
415 * INT_MIN is the only illegal window level.
417 if (*level == INT_MIN)
418 *level = INT_MIN + 1;
419 } else {
420 /* setup defaults */
421 *level = WMNormalLevel;
423 } else {
424 int tmp_workspace = -1;
425 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
426 Bool check;
428 check = False;
430 #ifdef MWM_HINTS
431 wMWMCheckClientHints(wwin);
432 #endif /* MWM_HINTS */
434 #ifdef NETWM_HINTS
435 if (!check)
436 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
437 #endif
439 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
440 * have INT_MIN that means that no window level was requested. -Dan
442 if (tmp_level == INT_MIN) {
443 if (WFLAGP(wwin, floating))
444 *level = WMFloatingLevel;
445 else if (WFLAGP(wwin, sunken))
446 *level = WMSunkenLevel;
447 else
448 *level = WMNormalLevel;
449 } else {
450 *level = tmp_level;
453 if (wwin->transient_for!=None && wwin->transient_for != scr->root_win) {
454 WWindow * transientOwner = wWindowFor(wwin->transient_for);
455 if (transientOwner) {
456 int ownerLevel = transientOwner->frame->core->stacking->window_level;
457 if (ownerLevel > *level) *level = ownerLevel;
461 if (tmp_workspace >= 0) {
462 *workspace = tmp_workspace % scr->workspace_count;
467 * Set attributes specified only for that window/class.
468 * This might do duplicate work with the 1st wDefaultFillAttributes().
470 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
471 &wwin->user_flags, &wwin->defined_user_flags,
472 False);
474 * Sanity checks for attributes that depend on other attributes
476 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
477 wwin->user_flags.emulate_appicon = 0;
479 if (wwin->main_window!=None) {
480 WApplication *wapp = wApplicationOf(wwin->main_window);
481 if (wapp && !wapp->flags.emulated)
482 wwin->user_flags.emulate_appicon = 0;
485 if (wwin->transient_for!=None
486 && wwin->transient_for!=wwin->screen_ptr->root_win)
487 wwin->user_flags.emulate_appicon = 0;
489 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
490 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
491 wwin->user_flags.sunken = 0;
493 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
496 /* windows that have takefocus=False shouldn't take focus at all */
497 if (wwin->focus_mode == WFM_NO_INPUT) {
498 wwin->client_flags.no_focusable = 1;
505 Bool
506 wWindowCanReceiveFocus(WWindow *wwin)
508 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
509 return False;
510 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
511 return False;
512 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
513 return False;
515 return True;
519 Bool
520 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
522 int w1, h1, w2, h2;
524 w1 = wwin->frame->core->width;
525 h1 = wwin->frame->core->height;
526 w2 = obscured->frame->core->width;
527 h2 = obscured->frame->core->height;
529 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
530 && wwin->frame->workspace != obscured->frame->workspace)
531 return False;
533 if (wwin->frame_x + w1 < obscured->frame_x
534 || wwin->frame_y + h1 < obscured->frame_y
535 || wwin->frame_x > obscured->frame_x + w2
536 || wwin->frame_y > obscured->frame_y + h2) {
537 return False;
540 return True;
544 static void
545 fixLeaderProperties(WWindow *wwin)
547 XClassHint *classHint;
548 XWMHints *hints, *clientHints;
549 Window leaders[2], window;
550 char **argv, *command;
551 int argc, i, pid;
552 Bool haveCommand;
554 classHint = XAllocClassHint();
555 clientHints = XGetWMHints(dpy, wwin->client_win);
556 pid = wNETWMGetPidForWindow(wwin->client_win);
557 if (pid > 0) {
558 haveCommand = GetCommandForPid(pid, &argv, &argc);
559 } else {
560 haveCommand = False;
563 leaders[0] = wwin->client_leader;
564 leaders[1] = wwin->group_id;
566 if (haveCommand) {
567 command = GetCommandForWindow(wwin->client_win);
568 if (command) {
569 /* command already set. nothing to do. */
570 wfree(command);
571 } else {
572 XSetCommand(dpy, wwin->client_win, argv, argc);
576 for (i=0; i<2; i++) {
577 window = leaders[i];
578 if (window) {
579 if (XGetClassHint(dpy, window, classHint) == 0) {
580 classHint->res_name = wwin->wm_instance;
581 classHint->res_class = wwin->wm_class;
582 XSetClassHint(dpy, window, classHint);
584 hints = XGetWMHints(dpy, window);
585 if (hints) {
586 XFree(hints);
587 } else if (clientHints) {
588 /* set window group leader to self */
589 clientHints->window_group = window;
590 clientHints->flags |= WindowGroupHint;
591 XSetWMHints(dpy, window, clientHints);
594 if (haveCommand) {
595 command = GetCommandForWindow(window);
596 if (command) {
597 /* command already set. nothing to do. */
598 wfree(command);
599 } else {
600 XSetCommand(dpy, window, argv, argc);
606 XFree(classHint);
607 if (clientHints) {
608 XFree(clientHints);
610 if (haveCommand) {
611 wfree(argv);
616 static Window
617 createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
619 XClassHint *classHint;
620 XWMHints *hints;
621 Window leader;
622 int argc;
623 char **argv;
625 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
626 /* set class hint */
627 classHint = XAllocClassHint();
628 classHint->res_name = instance;
629 classHint->res_class = class;
630 XSetClassHint(dpy, leader, classHint);
631 XFree(classHint);
633 /* inherit these from the original leader if available */
634 hints = XGetWMHints(dpy, win);
635 if (!hints) {
636 hints = XAllocWMHints();
637 hints->flags = 0;
639 /* set window group leader to self */
640 hints->window_group = leader;
641 hints->flags |= WindowGroupHint;
642 XSetWMHints(dpy, leader, hints);
643 XFree(hints);
645 if (XGetCommand(dpy, win, &argv, &argc)!=0 && argc > 0) {
646 XSetCommand(dpy, leader, argv, argc);
647 XFreeStringList(argv);
650 return leader;
654 static int
655 matchIdentifier(void *item, void *cdata)
657 return (strcmp(((WFakeGroupLeader*)item)->identifier, (char*)cdata)==0);
662 *----------------------------------------------------------------
663 * wManageWindow--
664 * reparents the window and allocates a descriptor for it.
665 * Window manager hints and other hints are fetched to configure
666 * the window decoration attributes and others. User preferences
667 * for the window are used if available, to configure window
668 * decorations and some behaviour.
669 * If in startup, windows that are override redirect,
670 * unmapped and never were managed and are Withdrawn are not
671 * managed.
673 * Returns:
674 * the new window descriptor
676 * Side effects:
677 * The window is reparented and appropriate notification
678 * is done to the client. Input mask for the window is setup.
679 * The window descriptor is also associated with various window
680 * contexts and inserted in the head of the window list.
681 * Event handler contexts are associated for some objects
682 * (buttons, titlebar and resizebar)
684 *----------------------------------------------------------------
686 WWindow*
687 wManageWindow(WScreen *scr, Window window)
689 WWindow *wwin;
690 int x, y;
691 unsigned width, height;
692 XWindowAttributes wattribs;
693 XSetWindowAttributes attribs;
694 WWindowState *win_state;
695 WWindow *transientOwner = NULL;
696 int window_level;
697 int wm_state;
698 int foo;
699 int workspace = -1;
700 char *title;
701 Bool withdraw = False;
702 Bool raise = False;
704 /* mutex. */
705 /* XGrabServer(dpy); */
706 XSync(dpy, False);
707 /* make sure the window is still there */
708 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
709 XUngrabServer(dpy);
710 return NULL;
713 /* if it's an override-redirect, ignore it */
714 if (wattribs.override_redirect) {
715 XUngrabServer(dpy);
716 return NULL;
719 wm_state = PropGetWindowState(window);
721 /* if it's startup and the window is unmapped, don't manage it */
722 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
723 XUngrabServer(dpy);
724 return NULL;
727 wwin = wWindowCreate();
729 title= wNETWMGetWindowName(window);
730 if (title)
731 wwin->flags.net_has_title= 1;
732 if (!title && !wFetchName(dpy, window, &title))
733 title = NULL;
735 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
737 #ifdef DEBUG
738 printf("managing window %x\n", (unsigned)window);
739 #endif
741 #ifdef SHAPE
742 if (wShapeSupported) {
743 int junk;
744 unsigned int ujunk;
745 int b_shaped;
747 XShapeSelectInput(dpy, window, ShapeNotifyMask);
748 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
749 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
750 wwin->flags.shaped = b_shaped;
752 #endif
755 *--------------------------------------------------
757 * Get hints and other information in properties
759 *--------------------------------------------------
761 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
763 /* setup descriptor */
764 wwin->client_win = window;
765 wwin->screen_ptr = scr;
767 wwin->old_border_width = wattribs.border_width;
769 wwin->event_mask = CLIENT_EVENTS;
770 attribs.event_mask = CLIENT_EVENTS;
771 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
772 attribs.save_under = False;
773 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
774 |CWSaveUnder, &attribs);
775 XSetWindowBorderWidth(dpy, window, 0);
777 /* get hints from GNUstep app */
778 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
779 wwin->flags.is_gnustep = 1;
781 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
782 wwin->wm_gnustep_attr = NULL;
785 wwin->client_leader = PropGetClientLeader(window);
786 if (wwin->client_leader!=None)
787 wwin->main_window = wwin->client_leader;
789 wwin->wm_hints = XGetWMHints(dpy, window);
791 if (wwin->wm_hints) {
792 if (wwin->wm_hints->flags & StateHint) {
794 if (wwin->wm_hints->initial_state == IconicState) {
796 wwin->flags.miniaturized = 1;
798 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
800 withdraw = True;
804 if (wwin->wm_hints->flags & WindowGroupHint) {
805 wwin->group_id = wwin->wm_hints->window_group;
806 /* window_group has priority over CLIENT_LEADER */
807 wwin->main_window = wwin->group_id;
808 } else {
809 wwin->group_id = None;
812 if (wwin->wm_hints->flags & UrgencyHint)
813 wwin->flags.urgent = 1;
814 } else {
815 wwin->group_id = None;
818 PropGetProtocols(window, &wwin->protocols);
820 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
821 wwin->transient_for = None;
822 } else {
823 if (wwin->transient_for==None || wwin->transient_for==window) {
824 wwin->transient_for = scr->root_win;
825 } else {
826 transientOwner = wWindowFor(wwin->transient_for);
827 if (transientOwner && transientOwner->main_window!=None) {
828 wwin->main_window = transientOwner->main_window;
829 } /*else {
830 wwin->main_window = None;
835 /* guess the focus mode */
836 wwin->focus_mode = getFocusMode(wwin);
838 /* get geometry stuff */
839 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
841 /* printf("wManageWindow: %d %d %d %d\n", x, y, width, height);*/
843 /* get colormap windows */
844 GetColormapWindows(wwin);
847 *--------------------------------------------------
849 * Setup the decoration/window attributes and
850 * geometry
852 *--------------------------------------------------
855 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
857 /* Make broken apps behave as a nice app. */
858 if (WFLAGP(wwin, emulate_appicon)) {
859 wwin->main_window = wwin->client_win;
862 fixLeaderProperties(wwin);
864 wwin->orig_main_window = wwin->main_window;
866 if (wwin->flags.is_gnustep) {
867 WSETUFLAG(wwin, shared_appicon, 0);
870 if (wwin->main_window) {
871 extern Atom _XA_WINDOWMAKER_MENU;
872 XTextProperty text_prop;
874 if (XGetTextProperty(dpy, wwin->main_window, &text_prop,
875 _XA_WINDOWMAKER_MENU)) {
876 WSETUFLAG(wwin, shared_appicon, 0);
880 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
881 char *buffer, *instance, *class;
882 WFakeGroupLeader *fPtr;
883 int index;
885 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
887 /* // only enter here if PropGetWMClass() succeds */
888 PropGetWMClass(wwin->main_window, &class, &instance);
889 buffer = StrConcatDot(instance, class);
891 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void*)buffer);
892 if (index != WANotFound) {
893 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
894 if (fPtr->retainCount == 0) {
895 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
896 instance, class);
898 fPtr->retainCount++;
899 #undef method2
900 if (fPtr->origLeader==None) {
901 #ifdef method2
902 if (ADEQUATE(wwin->group_id)) {
903 fPtr->retainCount++;
904 fPtr->origLeader = wwin->group_id;
905 } else if (ADEQUATE(wwin->client_leader)) {
906 fPtr->retainCount++;
907 fPtr->origLeader = wwin->client_leader;
908 } else if (ADEQUATE(wwin->main_window)) {
909 fPtr->retainCount++;
910 fPtr->origLeader = wwin->main_window;
912 #else
913 if (ADEQUATE(wwin->main_window)) {
914 fPtr->retainCount++;
915 fPtr->origLeader = wwin->main_window;
917 #endif
919 wwin->fake_group = fPtr;
920 /*wwin->group_id = fPtr->leader;*/
921 wwin->main_window = fPtr->leader;
922 wfree(buffer);
923 } else {
924 fPtr = (WFakeGroupLeader*)wmalloc(sizeof(WFakeGroupLeader));
926 fPtr->identifier = buffer;
927 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
928 instance, class);
929 fPtr->origLeader = None;
930 fPtr->retainCount = 1;
932 WMAddToArray(scr->fakeGroupLeaders, fPtr);
934 #ifdef method2
935 if (ADEQUATE(wwin->group_id)) {
936 fPtr->retainCount++;
937 fPtr->origLeader = wwin->group_id;
938 } else if (ADEQUATE(wwin->client_leader)) {
939 fPtr->retainCount++;
940 fPtr->origLeader = wwin->client_leader;
941 } else if (ADEQUATE(wwin->main_window)) {
942 fPtr->retainCount++;
943 fPtr->origLeader = wwin->main_window;
945 #else
946 if (ADEQUATE(wwin->main_window)) {
947 fPtr->retainCount++;
948 fPtr->origLeader = wwin->main_window;
950 #endif
951 wwin->fake_group = fPtr;
952 /*wwin->group_id = fPtr->leader;*/
953 wwin->main_window = fPtr->leader;
955 if (instance)
956 XFree(instance);
957 if (class)
958 XFree(class);
960 #undef method2
961 #undef ADEQUATE
965 *------------------------------------------------------------
967 * Setup the initial state of the window
969 *------------------------------------------------------------
972 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
973 wwin->flags.miniaturized = 1;
976 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin)) {
977 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
980 #ifdef NETWM_HINTS
981 wNETWMCheckInitialClientState(wwin);
982 #endif
984 /* apply previous state if it exists and we're in startup */
985 if (scr->flags.startup && wm_state >= 0) {
987 if (wm_state == IconicState) {
989 wwin->flags.miniaturized = 1;
991 } else if (wm_state == WithdrawnState) {
993 withdraw = True;
997 /* if there is a saved state (from file), restore it */
998 win_state = NULL;
999 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
1000 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
1001 } else {
1002 win_state = (WWindowState*)wWindowGetSavedState(window);
1004 if (win_state && !withdraw) {
1006 if (win_state->state->hidden>0)
1007 wwin->flags.hidden = win_state->state->hidden;
1009 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
1010 wwin->flags.shaded = win_state->state->shaded;
1012 if (win_state->state->miniaturized>0 &&
1013 !WFLAGP(wwin, no_miniaturizable)) {
1014 wwin->flags.miniaturized = win_state->state->miniaturized;
1017 if (!IS_OMNIPRESENT(wwin)) {
1018 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
1019 wwin->wm_class);
1020 if (w < 0 || w >= scr->workspace_count) {
1021 workspace = win_state->state->workspace;
1022 if (workspace >= scr->workspace_count)
1023 workspace = scr->current_workspace;
1024 } else {
1025 workspace = w;
1027 } else {
1028 workspace = scr->current_workspace;
1032 /* if we're restarting, restore saved state (from hints).
1033 * This will overwrite previous */
1035 WSavedState *wstate;
1037 if (getSavedState(window, &wstate)) {
1038 wwin->flags.shaded = wstate->shaded;
1039 wwin->flags.hidden = wstate->hidden;
1040 wwin->flags.miniaturized = wstate->miniaturized;
1041 wwin->flags.maximized = wstate->maximized;
1042 if (wwin->flags.maximized) {
1043 wwin->old_geometry.x = wstate->x;
1044 wwin->old_geometry.y = wstate->y;
1045 wwin->old_geometry.width = wstate->w;
1046 wwin->old_geometry.height = wstate->h;
1049 workspace = wstate->workspace;
1050 } else {
1051 wstate = NULL;
1054 /* restore window shortcut */
1055 if (wstate != NULL || win_state != NULL) {
1056 unsigned mask = 0;
1058 if (win_state != NULL)
1059 mask = win_state->state->window_shortcuts;
1061 if (wstate != NULL && mask == 0)
1062 mask = wstate->window_shortcuts;
1064 if (mask > 0) {
1065 int i;
1067 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1068 if (mask & (1<<i)) {
1069 if (!scr->shortcutWindows[i])
1070 scr->shortcutWindows[i] = WMCreateArray(4);
1072 WMAddToArray(scr->shortcutWindows[i], wwin);
1077 if (wstate != NULL) {
1078 wfree(wstate);
1082 /* don't let transients start miniaturized if their owners are not */
1083 if (transientOwner && !transientOwner->flags.miniaturized
1084 && wwin->flags.miniaturized && !withdraw) {
1085 wwin->flags.miniaturized = 0;
1086 if (wwin->wm_hints)
1087 wwin->wm_hints->initial_state = NormalState;
1090 /* set workspace on which the window starts */
1091 if (workspace >= 0) {
1092 if (workspace > scr->workspace_count-1) {
1093 workspace = workspace % scr->workspace_count;
1095 } else {
1096 int w;
1098 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1100 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1102 workspace = w;
1104 } else {
1105 if (wPreferences.open_transients_with_parent && transientOwner) {
1107 workspace = transientOwner->frame->workspace;
1109 } else {
1111 workspace = scr->current_workspace;
1116 /* setup window geometry */
1117 if (win_state && win_state->state->w > 0) {
1118 width = win_state->state->w;
1119 height = win_state->state->h;
1121 wWindowConstrainSize(wwin, &width, &height);
1123 /* do not ask for window placement if the window is
1124 * transient, during startup, if the initial workspace is another one
1125 * or if the window wants to start iconic.
1126 * If geometry was saved, restore it. */
1128 Bool dontBring = False;
1130 if (win_state && win_state->state->w > 0) {
1131 x = win_state->state->x;
1132 y = win_state->state->y;
1133 } else if ((wwin->transient_for==None
1134 || wPreferences.window_placement!=WPM_MANUAL)
1135 && !scr->flags.startup
1136 && workspace == scr->current_workspace
1137 && !wwin->flags.miniaturized
1138 && !wwin->flags.maximized
1139 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
1141 if (transientOwner && transientOwner->flags.mapped) {
1142 int offs = WMAX(20, 2*transientOwner->frame->top_width);
1143 WMRect rect;
1144 int head;
1146 x = transientOwner->frame_x +
1147 abs((transientOwner->frame->core->width - width)/2) + offs;
1148 y = transientOwner->frame_y +
1149 abs((transientOwner->frame->core->height - height)/3) + offs;
1152 * limit transient windows to be inside their parent's head
1154 rect.pos.x = transientOwner->frame_x;
1155 rect.pos.y = transientOwner->frame_y;
1156 rect.size.width = transientOwner->frame->core->width;
1157 rect.size.height = transientOwner->frame->core->height;
1159 head = wGetHeadForRect(scr, rect);
1160 rect = wGetRectForHead(scr, head);
1162 if (x < rect.pos.x)
1163 x = rect.pos.x;
1164 else if (x + width > rect.pos.x + rect.size.width)
1165 x = rect.pos.x + rect.size.width - width;
1167 if (y < rect.pos.y)
1168 y = rect.pos.y;
1169 else if (y + height > rect.pos.y + rect.size.height)
1170 y = rect.pos.y + rect.size.height - height;
1172 } else {
1173 PlaceWindow(wwin, &x, &y, width, height);
1175 if (wPreferences.window_placement == WPM_MANUAL) {
1176 dontBring = True;
1178 } else if (scr->xine_info.count &&
1179 (wwin->normal_hints->flags & PPosition)) {
1180 int head, flags;
1181 WMRect rect;
1182 int reposition = 0;
1185 * Make spash screens come out in the center of a head
1186 * trouble is that most splashies never get here
1187 * they are managed trough atoms but god knows where.
1188 * Dan, do you know ? -peter
1190 * Most of them are not managed, they have set
1191 * OverrideRedirect, which means we can't do anything about
1192 * them. -alfredo
1194 #if 0
1195 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width)/2);
1196 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height)/2);
1198 if ((unsigned)(x + (width - scr->scr_width)/2 + 10) < 20 &&
1199 (unsigned)(y + (height - scr->scr_height)/2 + 10) < 20) {
1201 reposition = 1;
1203 } else
1204 #endif
1207 * xinerama checks for: across head and dead space
1209 rect.pos.x = x;
1210 rect.pos.y = y;
1211 rect.size.width = width;
1212 rect.size.height = height;
1214 head = wGetRectPlacementInfo(scr, rect, &flags);
1216 if (flags & XFLAG_DEAD)
1217 reposition = 1;
1219 if (flags & XFLAG_MULTIPLE)
1220 reposition = 2;
1223 switch (reposition) {
1224 case 1:
1225 head = wGetHeadForPointerLocation(scr);
1226 rect = wGetRectForHead(scr, head);
1228 x = rect.pos.x + (x * rect.size.width)/scr->scr_width;
1229 y = rect.pos.y + (y * rect.size.height)/scr->scr_height;
1230 break;
1232 case 2:
1233 rect = wGetRectForHead(scr, head);
1235 if (x < rect.pos.x)
1236 x = rect.pos.x;
1237 else if (x + width > rect.pos.x + rect.size.width)
1238 x = rect.pos.x + rect.size.width - width;
1240 if (y < rect.pos.y)
1241 y = rect.pos.y;
1242 else if (y + height > rect.pos.y + rect.size.height)
1243 y = rect.pos.y + rect.size.height - height;
1245 break;
1247 default:
1248 break;
1252 if (WFLAGP(wwin, dont_move_off) && dontBring)
1253 wScreenBringInside(scr, &x, &y, width, height);
1256 #ifdef NETWM_HINTS
1257 wNETWMPositionSplash(wwin, &x, &y, width, height);
1258 #endif
1260 if (wwin->flags.urgent) {
1261 if (!IS_OMNIPRESENT(wwin))
1262 wwin->flags.omnipresent ^= 1;
1266 *--------------------------------------------------
1268 * Create frame, borders and do reparenting
1270 *--------------------------------------------------
1272 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1273 #ifdef XKB_BUTTON_HINT
1274 if (wPreferences.modelock)
1275 foo |= WFF_LANGUAGE_BUTTON;
1276 #endif
1277 if (HAS_TITLEBAR(wwin))
1278 foo |= WFF_TITLEBAR;
1279 if (HAS_RESIZEBAR(wwin))
1280 foo |= WFF_RESIZEBAR;
1281 if (HAS_BORDER(wwin))
1282 foo |= WFF_BORDER;
1284 wwin->frame = wFrameWindowCreate(scr, window_level,
1285 x, y, width, height,
1286 &wPreferences.window_title_clearance, foo,
1287 scr->window_title_texture,
1288 scr->resizebar_texture,
1289 scr->window_title_color,
1290 &scr->title_font);
1292 wwin->frame->flags.is_client_window_frame = 1;
1293 wwin->frame->flags.justification = wPreferences.title_justification;
1295 /* setup button images */
1296 wWindowUpdateButtonImages(wwin);
1298 /* hide unused buttons */
1299 foo = 0;
1300 if (WFLAGP(wwin, no_close_button))
1301 foo |= WFF_RIGHT_BUTTON;
1302 if (WFLAGP(wwin, no_miniaturize_button))
1303 foo |= WFF_LEFT_BUTTON;
1304 #ifdef XKB_BUTTON_HINT
1305 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1306 foo |= WFF_LANGUAGE_BUTTON;
1307 #endif
1308 if (foo!=0)
1309 wFrameWindowHideButton(wwin->frame, foo);
1311 wwin->frame->child = wwin;
1313 wwin->frame->workspace = workspace;
1315 wwin->frame->on_click_left = windowIconifyClick;
1316 #ifdef XKB_BUTTON_HINT
1317 if (wPreferences.modelock)
1318 wwin->frame->on_click_language = windowLanguageClick;
1319 #endif
1321 wwin->frame->on_click_right = windowCloseClick;
1322 wwin->frame->on_dblclick_right = windowCloseDblClick;
1324 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1325 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1327 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1330 XSelectInput(dpy, wwin->client_win,
1331 wwin->event_mask & ~StructureNotifyMask);
1333 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1334 0, wwin->frame->top_width);
1336 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1340 int gx, gy;
1342 wClientGetGravityOffsets(wwin, &gx, &gy);
1344 /* if gravity is to the south, account for the border sizes */
1345 if (gy > 0)
1346 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1350 * wWindowConfigure() will init the client window's size
1351 * (wwin->client.{width,height}) and all other geometry
1352 * related variables (frame_x,frame_y)
1354 wWindowConfigure(wwin, x, y, width, height);
1356 /* to make sure the window receives it's new position after reparenting */
1357 wWindowSynthConfigureNotify(wwin);
1360 *--------------------------------------------------
1362 * Setup descriptors and save window to internal
1363 * lists
1365 *--------------------------------------------------
1368 if (wwin->main_window!=None) {
1369 WApplication *app;
1370 WWindow *leader;
1372 /* Leader windows do not necessary set themselves as leaders.
1373 * If this is the case, point the leader of this window to
1374 * itself */
1375 leader = wWindowFor(wwin->main_window);
1376 if (leader && leader->main_window==None) {
1377 leader->main_window = leader->client_win;
1379 app = wApplicationCreate(wwin);
1380 if (app) {
1381 app->last_workspace = workspace;
1384 * Do application specific stuff, like setting application
1385 * wide attributes.
1388 if (wwin->flags.hidden) {
1389 /* if the window was set to hidden because it was hidden
1390 * in a previous incarnation and that state was restored */
1391 app->flags.hidden = 1;
1392 } else if (app->flags.hidden) {
1393 if (WFLAGP(app->main_window_desc, start_hidden)) {
1394 wwin->flags.hidden = 1;
1395 } else {
1396 wUnhideApplication(app, False, False);
1397 raise = True;
1403 /* setup the frame descriptor */
1404 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1405 wwin->frame->core->descriptor.parent = wwin;
1406 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1408 /* don't let windows go away if we die */
1409 XAddToSaveSet(dpy, window);
1411 XLowerWindow(dpy, window);
1413 /* if window is in this workspace and should be mapped, then map it */
1414 if (!wwin->flags.miniaturized
1415 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1416 && !wwin->flags.hidden && !withdraw) {
1418 /* The following "if" is to avoid crashing of clients that expect
1419 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1420 * after the return from this function.
1422 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1423 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1424 } else {
1425 wClientSetState(wwin, NormalState, None);
1428 #if 0
1429 /* if not auto focus, then map the window under the currently
1430 * focused window */
1431 #define _WIDTH(w) (w)->frame->core->width
1432 #define _HEIGHT(w) (w)->frame->core->height
1433 if (!wPreferences.auto_focus && scr->focused_window
1434 && !scr->flags.startup && !transientOwner
1435 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1436 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1437 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1438 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1439 || wwin->flags.maximized)) {
1440 MoveInStackListUnder(scr->focused_window->frame->core,
1441 wwin->frame->core);
1443 #undef _WIDTH
1444 #undef _HEIGHT
1446 #endif
1448 if (wPreferences.superfluous && !wPreferences.no_animations
1449 && !scr->flags.startup &&
1450 (wwin->transient_for==None || wwin->transient_for==scr->root_win)
1452 * The brain damaged idiotic non-click to focus modes will
1453 * have trouble with this because:
1455 * 1. window is created and mapped by the client
1456 * 2. window is mapped by wmaker in small size
1457 * 3. window is animated to grow to normal size
1458 * 4. this function returns to normal event loop
1459 * 5. eventually, the EnterNotify event that would trigger
1460 * the window focusing (if the mouse is over that window)
1461 * will be processed by wmaker.
1462 * But since this event will be rather delayed
1463 * (step 3 has a large delay) the time when the event ocurred
1464 * and when it is processed, the client that owns that window
1465 * will reject the XSetInputFocus() for it.
1467 && (wPreferences.focus_mode==WKF_CLICK
1468 || wPreferences.auto_focus)) {
1469 DoWindowBirth(wwin);
1472 wWindowMap(wwin);
1475 /* setup stacking descriptor */
1476 if (transientOwner) {
1477 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1478 } else {
1479 wwin->frame->core->stacking->child_of = NULL;
1483 if (!scr->focused_window) {
1484 /* first window on the list */
1485 wwin->next = NULL;
1486 wwin->prev = NULL;
1487 scr->focused_window = wwin;
1488 } else {
1489 WWindow *tmp;
1491 /* add window at beginning of focus window list */
1492 tmp = scr->focused_window;
1493 while (tmp->prev)
1494 tmp = tmp->prev;
1495 tmp->prev = wwin;
1496 wwin->next = tmp;
1497 wwin->prev = NULL;
1500 /* raise is set to true if we un-hid the app when this window was born.
1501 * we raise, else old windows of this app will be above this new one. */
1502 if (raise) {
1503 wRaiseFrame(wwin->frame->core);
1506 /* Update name must come after WApplication stuff is done */
1507 wWindowUpdateName(wwin, title);
1508 if (title)
1509 XFree(title);
1511 XUngrabServer(dpy);
1514 *--------------------------------------------------
1516 * Final preparations before window is ready to go
1518 *--------------------------------------------------
1521 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1524 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1525 && !wwin->flags.hidden) {
1526 if (((transientOwner && transientOwner->flags.focused)
1527 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1528 wSetFocusTo(scr, wwin);
1530 wWindowResetMouseGrabs(wwin);
1532 if (!WFLAGP(wwin, no_bind_keys)) {
1533 wWindowSetKeyGrabs(wwin);
1537 WMPostNotificationName(WMNManaged, wwin, NULL);
1540 wColormapInstallForWindow(scr, scr->cmap_window);
1544 *------------------------------------------------------------
1545 * Setup Notification Observers
1546 *------------------------------------------------------------
1548 WMAddNotificationObserver(appearanceObserver, wwin,
1549 WNWindowAppearanceSettingsChanged, wwin);
1553 *--------------------------------------------------
1555 * Cleanup temporary stuff
1557 *--------------------------------------------------
1560 if (win_state)
1561 wWindowDeleteSavedState(win_state);
1563 /* If the window must be withdrawed, then do it now.
1564 * Must do some optimization, 'though */
1565 if (withdraw) {
1566 wwin->flags.mapped = 0;
1567 wClientSetState(wwin, WithdrawnState, None);
1568 wUnmanageWindow(wwin, True, False);
1569 wwin = NULL;
1572 return wwin;
1579 WWindow*
1580 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1581 char *title, int x, int y, int width, int height)
1583 WWindow *wwin;
1584 int foo;
1586 wwin = wWindowCreate();
1588 WMAddNotificationObserver(appearanceObserver, wwin,
1589 WNWindowAppearanceSettingsChanged, wwin);
1591 wwin->flags.internal_window = 1;
1593 WSETUFLAG(wwin, omnipresent, 1);
1594 WSETUFLAG(wwin, no_shadeable, 1);
1595 WSETUFLAG(wwin, no_resizable, 1);
1596 WSETUFLAG(wwin, no_miniaturizable, 1);
1598 wwin->focus_mode = WFM_PASSIVE;
1600 wwin->client_win = window;
1601 wwin->screen_ptr = scr;
1603 wwin->transient_for = owner;
1605 wwin->client.x = x;
1606 wwin->client.y = y;
1607 wwin->client.width = width;
1608 wwin->client.height = height;
1610 wwin->frame_x = wwin->client.x;
1611 wwin->frame_y = wwin->client.y;
1614 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1615 foo |= WFF_TITLEBAR;
1616 #ifdef XKB_BUTTON_HINT
1617 foo |= WFF_LANGUAGE_BUTTON;
1618 #endif
1620 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1621 wwin->frame_x, wwin->frame_y,
1622 width, height,
1623 &wPreferences.window_title_clearance, foo,
1624 scr->window_title_texture,
1625 scr->resizebar_texture,
1626 scr->window_title_color,
1627 &scr->title_font);
1629 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1631 wwin->frame->flags.is_client_window_frame = 1;
1632 wwin->frame->flags.justification = wPreferences.title_justification;
1634 wFrameWindowChangeTitle(wwin->frame, title);
1636 /* setup button images */
1637 wWindowUpdateButtonImages(wwin);
1639 /* hide buttons */
1640 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1642 wwin->frame->child = wwin;
1644 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1646 #ifdef XKB_BUTTON_HINT
1647 if (wPreferences.modelock)
1648 wwin->frame->on_click_language = windowLanguageClick;
1649 #endif
1651 wwin->frame->on_click_right = windowCloseClick;
1653 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1654 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1656 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1658 wwin->client.y += wwin->frame->top_width;
1659 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1660 0, wwin->frame->top_width);
1662 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1663 wwin->client.width, wwin->client.height);
1665 /* setup the frame descriptor */
1666 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1667 wwin->frame->core->descriptor.parent = wwin;
1668 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1671 XLowerWindow(dpy, window);
1672 XMapSubwindows(dpy, wwin->frame->core->window);
1674 /* setup stacking descriptor */
1675 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
1676 WWindow *tmp;
1677 tmp = wWindowFor(wwin->transient_for);
1678 if (tmp)
1679 wwin->frame->core->stacking->child_of = tmp->frame->core;
1680 } else {
1681 wwin->frame->core->stacking->child_of = NULL;
1685 if (!scr->focused_window) {
1686 /* first window on the list */
1687 wwin->next = NULL;
1688 wwin->prev = NULL;
1689 scr->focused_window = wwin;
1690 } else {
1691 WWindow *tmp;
1693 /* add window at beginning of focus window list */
1694 tmp = scr->focused_window;
1695 while (tmp->prev)
1696 tmp = tmp->prev;
1697 tmp->prev = wwin;
1698 wwin->next = tmp;
1699 wwin->prev = NULL;
1702 if (wwin->flags.is_gnustep == 0)
1703 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1705 /* if (wPreferences.auto_focus)*/
1706 wSetFocusTo(scr, wwin);
1708 wWindowResetMouseGrabs(wwin);
1710 wWindowSetKeyGrabs(wwin);
1712 return wwin;
1717 *----------------------------------------------------------------------
1718 * wUnmanageWindow--
1719 * Removes the frame window from a window and destroys all data
1720 * related to it. The window will be reparented back to the root window
1721 * if restore is True.
1723 * Side effects:
1724 * Everything related to the window is destroyed and the window
1725 * is removed from the window lists. Focus is set to the previous on the
1726 * window list.
1727 *----------------------------------------------------------------------
1729 void
1730 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1732 WCoreWindow *frame = wwin->frame->core;
1733 WWindow *owner = NULL;
1734 WWindow *newFocusedWindow = NULL;
1735 int wasFocused;
1736 WScreen *scr = wwin->screen_ptr;
1739 /* First close attribute editor window if open */
1740 if (wwin->flags.inspector_open) {
1741 wCloseInspectorForWindow(wwin);
1744 /* Close window menu if it's open for this window */
1745 if (wwin->flags.menu_open_for_me) {
1746 CloseWindowMenu(scr);
1749 if (!destroyed) {
1750 if (!wwin->flags.internal_window)
1751 XRemoveFromSaveSet(dpy, wwin->client_win);
1753 XSelectInput(dpy, wwin->client_win, NoEventMask);
1755 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1756 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1759 XUnmapWindow(dpy, frame->window);
1761 XUnmapWindow(dpy, wwin->client_win);
1763 /* deselect window */
1764 wSelectWindow(wwin, False);
1766 /* remove all pending events on window */
1767 /* I think this only matters for autoraise */
1768 if (wPreferences.raise_delay)
1769 WMDeleteTimerWithClientData(wwin->frame->core);
1771 XFlush(dpy);
1773 /* reparent the window back to the root */
1774 if (restore)
1775 wClientRestore(wwin);
1777 if (wwin->transient_for!=scr->root_win) {
1778 owner = wWindowFor(wwin->transient_for);
1779 if (owner) {
1780 if (!owner->flags.semi_focused) {
1781 owner = NULL;
1782 } else {
1783 owner->flags.semi_focused = 0;
1788 wasFocused = wwin->flags.focused;
1790 /* remove from window focus list */
1791 if (!wwin->prev && !wwin->next) {
1792 /* was the only window */
1793 scr->focused_window = NULL;
1794 newFocusedWindow = NULL;
1795 } else {
1796 WWindow *tmp;
1798 if (wwin->prev)
1799 wwin->prev->next = wwin->next;
1800 if (wwin->next)
1801 wwin->next->prev = wwin->prev;
1802 else {
1803 scr->focused_window = wwin->prev;
1804 scr->focused_window->next = NULL;
1807 if (wPreferences.focus_mode==WKF_CLICK) {
1809 /* if in click to focus mode and the window
1810 * was a transient, focus the owner window
1812 tmp = NULL;
1813 if (wPreferences.focus_mode==WKF_CLICK) {
1814 tmp = wWindowFor(wwin->transient_for);
1815 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1816 tmp = NULL;
1819 /* otherwise, focus the next one in the focus list */
1820 if (!tmp) {
1821 tmp = scr->focused_window;
1822 while (tmp) { /* look for one in the window list first */
1823 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1824 && (tmp->flags.mapped || tmp->flags.shaded))
1825 break;
1826 tmp = tmp->prev;
1828 if (!tmp) { /* if unsuccessful, choose any focusable window */
1829 tmp = scr->focused_window;
1830 while (tmp) {
1831 if (!WFLAGP(tmp, no_focusable)
1832 && (tmp->flags.mapped || tmp->flags.shaded))
1833 break;
1834 tmp = tmp->prev;
1839 newFocusedWindow = tmp;
1841 } else if (wPreferences.focus_mode==WKF_SLOPPY) {
1842 unsigned int mask;
1843 int foo;
1844 Window bar, win;
1846 /* This is to let the root window get the keyboard input
1847 * if Sloppy focus mode and no other window get focus.
1848 * This way keybindings will not freeze.
1850 tmp = NULL;
1851 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1852 &foo, &foo, &foo, &foo, &mask))
1853 tmp = wWindowFor(win);
1854 if (tmp == wwin)
1855 tmp = NULL;
1856 newFocusedWindow = tmp;
1857 } else {
1858 newFocusedWindow = NULL;
1862 if (!wwin->flags.internal_window) {
1863 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1866 #ifdef DEBUG
1867 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1868 (unsigned)frame->window);
1869 #endif
1871 if (wasFocused) {
1872 if (newFocusedWindow != owner && owner) {
1873 if (wwin->flags.is_gnustep == 0)
1874 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1876 wSetFocusTo(scr, newFocusedWindow);
1878 wWindowDestroy(wwin);
1879 XFlush(dpy);
1883 void
1884 wWindowMap(WWindow *wwin)
1886 XMapWindow(dpy, wwin->frame->core->window);
1887 if (!wwin->flags.shaded) {
1888 /* window will be remapped when getting MapNotify */
1889 XSelectInput(dpy, wwin->client_win,
1890 wwin->event_mask & ~StructureNotifyMask);
1891 XMapWindow(dpy, wwin->client_win);
1892 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1894 wwin->flags.mapped = 1;
1899 void
1900 wWindowUnmap(WWindow *wwin)
1902 wwin->flags.mapped = 0;
1904 /* prevent window withdrawal when getting UnmapNotify */
1905 XSelectInput(dpy, wwin->client_win,
1906 wwin->event_mask & ~StructureNotifyMask);
1907 XUnmapWindow(dpy, wwin->client_win);
1908 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1910 XUnmapWindow(dpy, wwin->frame->core->window);
1915 void
1916 wWindowFocus(WWindow *wwin, WWindow *owin)
1918 WWindow *nowner;
1919 WWindow *oowner;
1921 #ifdef KEEP_XKB_LOCK_STATUS
1922 if (wPreferences.modelock) {
1923 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1925 #endif /* KEEP_XKB_LOCK_STATUS */
1927 wwin->flags.semi_focused = 0;
1929 if (wwin->flags.is_gnustep == 0)
1930 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1932 wwin->flags.focused = 1;
1934 wWindowResetMouseGrabs(wwin);
1936 WMPostNotificationName(WMNChangedFocus, wwin, (void*)True);
1938 if (owin == wwin || !owin)
1939 return;
1941 nowner = wWindowFor(wwin->transient_for);
1943 /* new window is a transient for the old window */
1944 if (nowner == owin) {
1945 owin->flags.semi_focused = 1;
1946 wWindowUnfocus(nowner);
1947 return;
1950 oowner = wWindowFor(owin->transient_for);
1952 /* new window is owner of old window */
1953 if (wwin == oowner) {
1954 wWindowUnfocus(owin);
1955 return;
1958 if (!nowner) {
1959 wWindowUnfocus(owin);
1960 return;
1963 /* new window has same owner of old window */
1964 if (oowner == nowner) {
1965 /* prevent unfocusing of owner */
1966 oowner->flags.semi_focused = 0;
1967 wWindowUnfocus(owin);
1968 oowner->flags.semi_focused = 1;
1970 return;
1973 /* nowner != NULL && oowner != nowner */
1974 nowner->flags.semi_focused = 1;
1975 wWindowUnfocus(nowner);
1976 wWindowUnfocus(owin);
1980 void
1981 wWindowUnfocus(WWindow *wwin)
1983 CloseWindowMenu(wwin->screen_ptr);
1985 if (wwin->flags.is_gnustep == 0)
1986 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1987 ? WS_PFOCUSED : WS_UNFOCUSED);
1989 if (wwin->transient_for!=None
1990 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1991 WWindow *owner;
1992 owner = wWindowFor(wwin->transient_for);
1993 if (owner && owner->flags.semi_focused) {
1994 owner->flags.semi_focused = 0;
1995 if (owner->flags.mapped || owner->flags.shaded) {
1996 wWindowUnfocus(owner);
1997 wFrameWindowPaint(owner->frame);
2001 wwin->flags.focused = 0;
2003 wWindowResetMouseGrabs(wwin);
2005 WMPostNotificationName(WMNChangedFocus, wwin, (void*)False);
2009 void
2010 wWindowUpdateName(WWindow *wwin, char *newTitle)
2012 char *title;
2014 if (!wwin->frame)
2015 return;
2017 wwin->flags.wm_name_changed = 1;
2019 if (!newTitle) {
2020 /* the hint was removed */
2021 title = DEF_WINDOW_TITLE;
2022 } else {
2023 title = newTitle;
2026 if (wFrameWindowChangeTitle(wwin->frame, title)) {
2027 WMPostNotificationName(WMNChangedName, wwin, NULL);
2034 *----------------------------------------------------------------------
2036 * wWindowConstrainSize--
2037 * Constrains size for the client window, taking the maximal size,
2038 * window resize increments and other size hints into account.
2040 * Returns:
2041 * The closest size to what was given that the client window can
2042 * have.
2044 *----------------------------------------------------------------------
2046 void
2047 wWindowConstrainSize(WWindow *wwin, unsigned int *nwidth, unsigned int *nheight)
2049 int width = (int)*nwidth;
2050 int height = (int)*nheight;
2051 int winc = 1;
2052 int hinc = 1;
2053 int minW = 1, minH = 1;
2054 int maxW = wwin->screen_ptr->scr_width*2;
2055 int maxH = wwin->screen_ptr->scr_height*2;
2056 int minAX = -1, minAY = -1;
2057 int maxAX = -1, maxAY = -1;
2058 int baseW = 0;
2059 int baseH = 0;
2061 if (wwin->normal_hints) {
2062 winc = wwin->normal_hints->width_inc;
2063 hinc = wwin->normal_hints->height_inc;
2064 minW = wwin->normal_hints->min_width;
2065 minH = wwin->normal_hints->min_height;
2066 maxW = wwin->normal_hints->max_width;
2067 maxH = wwin->normal_hints->max_height;
2068 if (wwin->normal_hints->flags & PAspect) {
2069 minAX = wwin->normal_hints->min_aspect.x;
2070 minAY = wwin->normal_hints->min_aspect.y;
2071 maxAX = wwin->normal_hints->max_aspect.x;
2072 maxAY = wwin->normal_hints->max_aspect.y;
2075 baseW = wwin->normal_hints->base_width;
2076 baseH = wwin->normal_hints->base_height;
2079 if (width < minW)
2080 width = minW;
2081 if (height < minH)
2082 height = minH;
2084 if (width > maxW)
2085 width = maxW;
2086 if (height > maxH)
2087 height = maxH;
2089 /* aspect ratio code borrowed from olwm */
2090 if (minAX > 0) {
2091 /* adjust max aspect ratio */
2092 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
2093 if (maxAX > maxAY) {
2094 height = (width * maxAY) / maxAX;
2095 if (height > maxH) {
2096 height = maxH;
2097 width = (height * maxAX) / maxAY;
2099 } else {
2100 width = (height * maxAX) / maxAY;
2101 if (width > maxW) {
2102 width = maxW;
2103 height = (width * maxAY) / maxAX;
2108 /* adjust min aspect ratio */
2109 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
2110 if (minAX > minAY) {
2111 height = (width * minAY) / minAX;
2112 if (height < minH) {
2113 height = minH;
2114 width = (height * minAX) / minAY;
2116 } else {
2117 width = (height * minAX) / minAY;
2118 if (width < minW) {
2119 width = minW;
2120 height = (width * minAY) / minAX;
2126 if (baseW != 0) {
2127 width = (((width - baseW) / winc) * winc) + baseW;
2128 } else {
2129 width = (((width - minW) / winc) * winc) + minW;
2132 if (baseH != 0) {
2133 height = (((height - baseH) / hinc) * hinc) + baseH;
2134 } else {
2135 height = (((height - minH) / hinc) * hinc) + minH;
2138 /* broken stupid apps may cause preposterous values for these.. */
2139 if (width > 0)
2140 *nwidth = width;
2141 if (height > 0)
2142 *nheight = height;
2146 void
2147 wWindowCropSize(WWindow *wwin, unsigned int maxW, unsigned int maxH,
2148 unsigned int *width, unsigned int *height)
2150 int baseW = 0, baseH = 0;
2151 int winc = 1, hinc = 1;
2153 if (wwin->normal_hints) {
2154 baseW = wwin->normal_hints->base_width;
2155 baseH = wwin->normal_hints->base_height;
2157 winc = wwin->normal_hints->width_inc;
2158 hinc = wwin->normal_hints->height_inc;
2161 if (*width > maxW)
2162 *width = maxW - (maxW - baseW) % winc;
2164 if (*height > maxH)
2165 *height = maxH - (maxH - baseH) % hinc;
2169 void
2170 wWindowChangeWorkspace(WWindow *wwin, int workspace)
2172 WScreen *scr = wwin->screen_ptr;
2173 WApplication *wapp;
2174 int unmap = 0;
2176 if (workspace >= scr->workspace_count || workspace < 0
2177 || workspace == wwin->frame->workspace)
2178 return;
2180 if (workspace != scr->current_workspace) {
2181 /* Sent to other workspace. Unmap window */
2182 if ((wwin->flags.mapped
2183 || wwin->flags.shaded
2184 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2185 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2187 wapp = wApplicationOf(wwin->main_window);
2188 if (wapp) {
2189 wapp->last_workspace = workspace;
2191 if (wwin->flags.miniaturized) {
2192 if (wwin->icon) {
2193 XUnmapWindow(dpy, wwin->icon->core->window);
2194 wwin->icon->mapped = 0;
2196 } else {
2197 unmap = 1;
2198 wSetFocusTo(scr, NULL);
2201 } else {
2202 /* brought to current workspace. Map window */
2203 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2204 if (wwin->icon) {
2205 XMapWindow(dpy, wwin->icon->core->window);
2206 wwin->icon->mapped = 1;
2208 } else if (!wwin->flags.mapped &&
2209 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2210 wWindowMap(wwin);
2213 if (!IS_OMNIPRESENT(wwin)) {
2214 int oldWorkspace = wwin->frame->workspace;
2216 wwin->frame->workspace = workspace;
2218 WMPostNotificationName(WMNChangedWorkspace, wwin, (void*)(uintptr_t)oldWorkspace);
2221 if (unmap) {
2222 wWindowUnmap(wwin);
2227 void
2228 wWindowSynthConfigureNotify(WWindow *wwin)
2230 XEvent sevent;
2232 sevent.type = ConfigureNotify;
2233 sevent.xconfigure.display = dpy;
2234 sevent.xconfigure.event = wwin->client_win;
2235 sevent.xconfigure.window = wwin->client_win;
2237 sevent.xconfigure.x = wwin->client.x;
2238 sevent.xconfigure.y = wwin->client.y;
2239 sevent.xconfigure.width = wwin->client.width;
2240 sevent.xconfigure.height = wwin->client.height;
2242 sevent.xconfigure.border_width = wwin->old_border_width;
2243 if (HAS_TITLEBAR(wwin) && wwin->frame->titlebar)
2244 sevent.xconfigure.above = wwin->frame->titlebar->window;
2245 else
2246 sevent.xconfigure.above = None;
2248 sevent.xconfigure.override_redirect = False;
2249 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2250 XFlush(dpy);
2255 *----------------------------------------------------------------------
2256 * wWindowConfigure--
2257 * Configures the frame, decorations and client window to the
2258 * specified geometry. The geometry is not checked for validity,
2259 * wWindowConstrainSize() must be used for that.
2260 * The size parameters are for the client window, but the position is
2261 * for the frame.
2262 * The client window receives a ConfigureNotify event, according
2263 * to what ICCCM says.
2265 * Returns:
2266 * None
2268 * Side effects:
2269 * Window size and position are changed and client window receives
2270 * a ConfigureNotify event.
2271 *----------------------------------------------------------------------
2273 void
2274 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
2275 WWindow *wwin;
2276 int req_x, req_y; /* new position of the frame */
2277 int req_width, req_height; /* new size of the client */
2279 int synth_notify = False;
2280 int resize;
2282 resize = (req_width!=wwin->client.width
2283 || req_height!=wwin->client.height);
2285 * if the window is being moved but not resized then
2286 * send a synthetic ConfigureNotify
2288 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
2289 synth_notify = True;
2292 if (WFLAGP(wwin, dont_move_off))
2293 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2294 req_width, req_height);
2295 if (resize) {
2296 if (req_width < MIN_WINDOW_SIZE)
2297 req_width = MIN_WINDOW_SIZE;
2298 if (req_height < MIN_WINDOW_SIZE)
2299 req_height = MIN_WINDOW_SIZE;
2301 /* If growing, resize inner part before frame,
2302 * if shrinking, resize frame before.
2303 * This will prevent the frame (that can have a different color)
2304 * to be exposed, causing flicker */
2305 if (req_height > wwin->frame->core->height
2306 || req_width > wwin->frame->core->width)
2307 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2309 if (wwin->flags.shaded) {
2310 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2311 req_width, wwin->frame->core->height);
2312 wwin->old_geometry.height = req_height;
2313 } else {
2314 int h;
2316 h = req_height + wwin->frame->top_width
2317 + wwin->frame->bottom_width;
2319 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2322 if (!(req_height > wwin->frame->core->height
2323 || req_width > wwin->frame->core->width))
2324 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2326 wwin->client.x = req_x;
2327 wwin->client.y = req_y + wwin->frame->top_width;
2328 wwin->client.width = req_width;
2329 wwin->client.height = req_height;
2330 } else {
2331 wwin->client.x = req_x;
2332 wwin->client.y = req_y + wwin->frame->top_width;
2334 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2336 wwin->frame_x = req_x;
2337 wwin->frame_y = req_y;
2338 if (HAS_BORDER(wwin)) {
2339 wwin->client.x += FRAME_BORDER_WIDTH;
2340 wwin->client.y += FRAME_BORDER_WIDTH;
2343 #ifdef SHAPE
2344 if (wShapeSupported && wwin->flags.shaped && resize) {
2345 wWindowSetShape(wwin);
2347 #endif
2349 if (synth_notify)
2350 wWindowSynthConfigureNotify(wwin);
2351 XFlush(dpy);
2355 void
2356 wWindowMove(wwin, req_x, req_y)
2357 WWindow *wwin;
2358 int req_x, req_y; /* new position of the frame */
2360 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2361 int synth_notify = False;
2363 /* Send a synthetic ConfigureNotify event for every window movement. */
2364 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2365 synth_notify = True;
2367 #else
2368 /* A single synthetic ConfigureNotify event is sent at the end of
2369 * a completed (opaque) movement in moveres.c */
2370 #endif
2372 if (WFLAGP(wwin, dont_move_off))
2373 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2374 wwin->frame->core->width, wwin->frame->core->height);
2376 wwin->client.x = req_x;
2377 wwin->client.y = req_y + wwin->frame->top_width;
2378 if (HAS_BORDER(wwin)) {
2379 wwin->client.x += FRAME_BORDER_WIDTH;
2380 wwin->client.y += FRAME_BORDER_WIDTH;
2383 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2385 wwin->frame_x = req_x;
2386 wwin->frame_y = req_y;
2388 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2389 if (synth_notify)
2390 wWindowSynthConfigureNotify(wwin);
2391 #endif
2395 void
2396 wWindowUpdateButtonImages(WWindow *wwin)
2398 WScreen *scr = wwin->screen_ptr;
2399 Pixmap pixmap, mask;
2400 WFrameWindow *fwin = wwin->frame;
2402 if (!HAS_TITLEBAR(wwin))
2403 return;
2405 /* miniaturize button */
2407 if (!WFLAGP(wwin, no_miniaturize_button)) {
2408 if (wwin->wm_gnustep_attr
2409 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2410 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2412 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2413 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2414 } else {
2415 mask = None;
2418 if (fwin->lbutton_image
2419 && (fwin->lbutton_image->image != pixmap
2420 || fwin->lbutton_image->mask != mask)) {
2421 wPixmapDestroy(fwin->lbutton_image);
2422 fwin->lbutton_image = NULL;
2425 if (!fwin->lbutton_image) {
2426 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2427 fwin->lbutton_image->client_owned = 1;
2428 fwin->lbutton_image->client_owned_mask = 1;
2430 } else {
2431 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2432 wPixmapDestroy(fwin->lbutton_image);
2434 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2438 #ifdef XKB_BUTTON_HINT
2439 if (!WFLAGP(wwin, no_language_button)) {
2440 if (fwin->languagebutton_image &&
2441 !fwin->languagebutton_image->shared) {
2442 wPixmapDestroy(fwin->languagebutton_image);
2444 fwin->languagebutton_image =
2445 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2447 #endif
2449 /* close button */
2451 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2452 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2454 if (!WFLAGP(wwin, no_close_button)) {
2455 if (wwin->wm_gnustep_attr
2456 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2457 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2459 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2460 mask = wwin->wm_gnustep_attr->close_mask;
2461 else
2462 mask = None;
2464 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2465 || fwin->rbutton_image->mask != mask)) {
2466 wPixmapDestroy(fwin->rbutton_image);
2467 fwin->rbutton_image = NULL;
2470 if (!fwin->rbutton_image) {
2471 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2472 fwin->rbutton_image->client_owned = 1;
2473 fwin->rbutton_image->client_owned_mask = 1;
2476 } else if (WFLAGP(wwin, kill_close)) {
2478 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2479 wPixmapDestroy(fwin->rbutton_image);
2481 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2483 } else if (MGFLAGP(wwin, broken_close)) {
2485 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2486 wPixmapDestroy(fwin->rbutton_image);
2488 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2490 } else {
2492 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2493 wPixmapDestroy(fwin->rbutton_image);
2495 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2499 /* force buttons to be redrawn */
2500 fwin->flags.need_texture_change = 1;
2501 wFrameWindowPaint(fwin);
2506 *---------------------------------------------------------------------------
2507 * wWindowConfigureBorders--
2508 * Update window border configuration according to attribute flags.
2510 *---------------------------------------------------------------------------
2512 void
2513 wWindowConfigureBorders(WWindow *wwin)
2515 if (wwin->frame) {
2516 int flags;
2517 int newy, oldh;
2519 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2521 #ifdef XKB_BUTTON_HINT
2522 flags |= WFF_LANGUAGE_BUTTON;
2523 #endif
2525 if (HAS_TITLEBAR(wwin))
2526 flags |= WFF_TITLEBAR;
2527 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2528 flags |= WFF_RESIZEBAR;
2529 if (HAS_BORDER(wwin))
2530 flags |= WFF_BORDER;
2531 if (wwin->flags.shaded)
2532 flags |= WFF_IS_SHADED;
2534 oldh = wwin->frame->top_width;
2535 wFrameWindowUpdateBorders(wwin->frame, flags);
2536 if (oldh != wwin->frame->top_width) {
2537 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2539 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2540 wWindowConfigure(wwin, wwin->frame_x, newy,
2541 wwin->client.width, wwin->client.height);
2544 flags = 0;
2545 if (!WFLAGP(wwin, no_miniaturize_button)
2546 && wwin->frame->flags.hide_left_button)
2547 flags |= WFF_LEFT_BUTTON;
2549 #ifdef XKB_BUTTON_HINT
2550 if (!WFLAGP(wwin, no_language_button)
2551 && wwin->frame->flags.hide_language_button) {
2552 flags |= WFF_LANGUAGE_BUTTON;
2554 #endif
2556 if (!WFLAGP(wwin, no_close_button)
2557 && wwin->frame->flags.hide_right_button)
2558 flags |= WFF_RIGHT_BUTTON;
2560 if (flags!=0) {
2561 wWindowUpdateButtonImages(wwin);
2562 wFrameWindowShowButton(wwin->frame, flags);
2565 flags = 0;
2566 if (WFLAGP(wwin, no_miniaturize_button)
2567 && !wwin->frame->flags.hide_left_button)
2568 flags |= WFF_LEFT_BUTTON;
2570 #ifdef XKB_BUTTON_HINT
2571 if (WFLAGP(wwin, no_language_button)
2572 && !wwin->frame->flags.hide_language_button)
2573 flags |= WFF_LANGUAGE_BUTTON;
2574 #endif
2576 if (WFLAGP(wwin, no_close_button)
2577 && !wwin->frame->flags.hide_right_button)
2578 flags |= WFF_RIGHT_BUTTON;
2580 if (flags!=0)
2581 wFrameWindowHideButton(wwin->frame, flags);
2583 #ifdef SHAPE
2584 if (wShapeSupported && wwin->flags.shaped) {
2585 wWindowSetShape(wwin);
2587 #endif
2592 void
2593 wWindowSaveState(WWindow *wwin)
2595 CARD32 data[10];
2596 int i;
2598 memset(data, 0, sizeof(CARD32)*10);
2599 data[0] = wwin->frame->workspace;
2600 data[1] = wwin->flags.miniaturized;
2601 data[2] = wwin->flags.shaded;
2602 data[3] = wwin->flags.hidden;
2603 data[4] = wwin->flags.maximized;
2604 if (wwin->flags.maximized == 0) {
2605 data[5] = wwin->frame_x;
2606 data[6] = wwin->frame_y;
2607 data[7] = wwin->frame->core->width;
2608 data[8] = wwin->frame->core->height;
2609 } else {
2610 data[5] = wwin->old_geometry.x;
2611 data[6] = wwin->old_geometry.y;
2612 data[7] = wwin->old_geometry.width;
2613 data[8] = wwin->old_geometry.height;
2616 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2617 if (wwin->screen_ptr->shortcutWindows[i] &&
2618 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2619 data[9] |= 1<<i;
2621 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2622 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2623 (unsigned char *)data, 10);
2627 static int
2628 getSavedState(Window window, WSavedState **state)
2630 Atom type_ret;
2631 int fmt_ret;
2632 unsigned long nitems_ret;
2633 unsigned long bytes_after_ret;
2634 CARD32 *data;
2636 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2637 True, _XA_WINDOWMAKER_STATE,
2638 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2639 (unsigned char **)&data)!=Success || !data)
2640 return 0;
2642 *state = wmalloc(sizeof(WSavedState));
2644 (*state)->workspace = data[0];
2645 (*state)->miniaturized = data[1];
2646 (*state)->shaded = data[2];
2647 (*state)->hidden = data[3];
2648 (*state)->maximized = data[4];
2649 (*state)->x = data[5];
2650 (*state)->y = data[6];
2651 (*state)->w = data[7];
2652 (*state)->h = data[8];
2653 (*state)->window_shortcuts = data[9];
2655 XFree(data);
2657 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2658 return 1;
2659 else
2660 return 0;
2664 #ifdef SHAPE
2665 void
2666 wWindowClearShape(WWindow *wwin)
2668 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2669 0, wwin->frame->top_width, None, ShapeSet);
2670 XFlush(dpy);
2673 void
2674 wWindowSetShape(WWindow *wwin)
2676 XRectangle rect[2];
2677 int count;
2678 #ifdef OPTIMIZE_SHAPE
2679 XRectangle *rects;
2680 XRectangle *urec;
2681 int ordering;
2683 /* only shape is the client's */
2684 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2685 goto alt_code;
2688 /* Get array of rectangles describing the shape mask */
2689 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2690 &count, &ordering);
2691 if (!rects) {
2692 goto alt_code;
2695 urec = malloc(sizeof(XRectangle)*(count+2));
2696 if (!urec) {
2697 XFree(rects);
2698 goto alt_code;
2701 /* insert our decoration rectangles in the rect list */
2702 memcpy(urec, rects, sizeof(XRectangle)*count);
2703 XFree(rects);
2705 if (HAS_TITLEBAR(wwin)) {
2706 urec[count].x = -1;
2707 urec[count].y = -1 - wwin->frame->top_width;
2708 urec[count].width = wwin->frame->core->width + 2;
2709 urec[count].height = wwin->frame->top_width + 1;
2710 count++;
2712 if (HAS_RESIZEBAR(wwin)) {
2713 urec[count].x = -1;
2714 urec[count].y = wwin->frame->core->height
2715 - wwin->frame->bottom_width - wwin->frame->top_width;
2716 urec[count].width = wwin->frame->core->width + 2;
2717 urec[count].height = wwin->frame->bottom_width + 1;
2718 count++;
2721 /* shape our frame window */
2722 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2723 0, wwin->frame->top_width, urec, count,
2724 ShapeSet, Unsorted);
2725 XFlush(dpy);
2726 wfree(urec);
2727 return;
2729 alt_code:
2730 #endif /* OPTIMIZE_SHAPE */
2731 count = 0;
2732 if (HAS_TITLEBAR(wwin)) {
2733 rect[count].x = -1;
2734 rect[count].y = -1;
2735 rect[count].width = wwin->frame->core->width + 2;
2736 rect[count].height = wwin->frame->top_width + 1;
2737 count++;
2739 if (HAS_RESIZEBAR(wwin)) {
2740 rect[count].x = -1;
2741 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2742 rect[count].width = wwin->frame->core->width + 2;
2743 rect[count].height = wwin->frame->bottom_width + 1;
2744 count++;
2746 if (count > 0) {
2747 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2748 0, 0, rect, count, ShapeSet, Unsorted);
2750 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2751 0, wwin->frame->top_width, wwin->client_win,
2752 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2753 XFlush(dpy);
2755 #endif /* SHAPE */
2757 /* ====================================================================== */
2759 static FocusMode
2760 getFocusMode(WWindow *wwin)
2762 FocusMode mode;
2764 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2765 if (wwin->wm_hints->input == True) {
2766 if (wwin->protocols.TAKE_FOCUS)
2767 mode = WFM_LOCALLY_ACTIVE;
2768 else
2769 mode = WFM_PASSIVE;
2770 } else {
2771 if (wwin->protocols.TAKE_FOCUS)
2772 mode = WFM_GLOBALLY_ACTIVE;
2773 else
2774 mode = WFM_NO_INPUT;
2776 } else {
2777 mode = WFM_PASSIVE;
2779 return mode;
2783 void
2784 wWindowSetKeyGrabs(WWindow *wwin)
2786 int i;
2787 WShortKey *key;
2789 for (i=0; i<WKBD_LAST; i++) {
2790 key = &wKeyBindings[i];
2792 if (key->keycode==0)
2793 continue;
2794 if (key->modifier!=AnyModifier) {
2795 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2796 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2797 #ifdef NUMLOCK_HACK
2798 /* Also grab all modifier combinations possible that include,
2799 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2800 * work even if the NumLock/ScrollLock key is on.
2802 wHackedGrabKey(key->keycode, key->modifier,
2803 wwin->frame->core->window, True, GrabModeAsync,
2804 GrabModeAsync);
2805 #endif
2807 XGrabKey(dpy, key->keycode, key->modifier,
2808 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2811 #ifndef LITE
2812 wRootMenuBindShortcuts(wwin->frame->core->window);
2813 #endif
2818 void
2819 wWindowResetMouseGrabs(WWindow *wwin)
2821 /* Mouse grabs can't be done on the client window because of
2822 * ICCCM and because clients that try to do the same will crash.
2824 * But there is a problem wich makes tbar buttons of unfocused
2825 * windows not usable as the click goes to the frame window instead
2826 * of the button itself. Must figure a way to fix that.
2829 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2831 if (!WFLAGP(wwin, no_bind_mouse)) {
2832 /* grabs for Meta+drag */
2833 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2834 True, ButtonPressMask|ButtonReleaseMask,
2835 GrabModeSync, GrabModeAsync, None, None);
2838 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2839 && !wwin->flags.is_gnustep) {
2840 /* the passive grabs to focus the window */
2841 /* if (wPreferences.focus_mode == WKF_CLICK) */
2842 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2843 True, ButtonPressMask|ButtonReleaseMask,
2844 GrabModeSync, GrabModeAsync, None, None);
2846 XFlush(dpy);
2850 void
2851 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2853 if (attr->flags & GSExtraFlagsAttr) {
2854 if (MGFLAGP(wwin, broken_close) !=
2855 (attr->extra_flags & GSDocumentEditedFlag)) {
2856 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2857 wWindowUpdateButtonImages(wwin);
2863 WMagicNumber
2864 wWindowAddSavedState(char *instance, char *class, char *command,
2865 pid_t pid, WSavedState *state)
2867 WWindowState *wstate;
2869 wstate = malloc(sizeof(WWindowState));
2870 if (!wstate)
2871 return 0;
2873 memset(wstate, 0, sizeof(WWindowState));
2874 wstate->pid = pid;
2875 if (instance)
2876 wstate->instance = wstrdup(instance);
2877 if (class)
2878 wstate->class = wstrdup(class);
2879 if (command)
2880 wstate->command = wstrdup(command);
2881 wstate->state = state;
2883 wstate->next = windowState;
2884 windowState = wstate;
2886 #ifdef DEBUG
2887 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2888 class, command);
2889 #endif
2891 return wstate;
2895 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2898 WMagicNumber
2899 wWindowGetSavedState(Window win)
2901 char *instance, *class, *command=NULL;
2902 WWindowState *wstate = windowState;
2904 if (!wstate)
2905 return NULL;
2907 command = GetCommandForWindow(win);
2908 if (!command)
2909 return NULL;
2911 if (PropGetWMClass(win, &class, &instance)) {
2912 while (wstate) {
2913 if (SAME(instance, wstate->instance) &&
2914 SAME(class, wstate->class) &&
2915 SAME(command, wstate->command)) {
2916 break;
2918 wstate = wstate->next;
2920 } else {
2921 wstate = NULL;
2924 #ifdef DEBUG
2925 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2926 class, command);
2927 #endif
2929 if (command) wfree(command);
2930 if (instance) XFree(instance);
2931 if (class) XFree(class);
2933 return wstate;
2937 void
2938 wWindowDeleteSavedState(WMagicNumber id)
2940 WWindowState *tmp, *wstate=(WWindowState*)id;
2942 if (!wstate || !windowState)
2943 return;
2945 tmp = windowState;
2946 if (tmp==wstate) {
2947 windowState = wstate->next;
2948 #ifdef DEBUG
2949 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2950 wstate, wstate->instance, wstate->class, wstate->command);
2951 #endif
2952 if (wstate->instance) wfree(wstate->instance);
2953 if (wstate->class) wfree(wstate->class);
2954 if (wstate->command) wfree(wstate->command);
2955 wfree(wstate->state);
2956 wfree(wstate);
2957 } else {
2958 while (tmp->next) {
2959 if (tmp->next==wstate) {
2960 tmp->next=wstate->next;
2961 #ifdef DEBUG
2962 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2963 wstate, wstate->instance, wstate->class, wstate->command);
2964 #endif
2965 if (wstate->instance) wfree(wstate->instance);
2966 if (wstate->class) wfree(wstate->class);
2967 if (wstate->command) wfree(wstate->command);
2968 wfree(wstate->state);
2969 wfree(wstate);
2970 break;
2972 tmp = tmp->next;
2978 void
2979 wWindowDeleteSavedStatesForPID(pid_t pid)
2981 WWindowState *tmp, *wstate;
2983 if (!windowState)
2984 return;
2986 tmp = windowState;
2987 if (tmp->pid == pid) {
2988 wstate = windowState;
2989 windowState = tmp->next;
2990 #ifdef DEBUG
2991 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2992 wstate, wstate->instance, wstate->class, wstate->command);
2993 #endif
2994 if (wstate->instance) wfree(wstate->instance);
2995 if (wstate->class) wfree(wstate->class);
2996 if (wstate->command) wfree(wstate->command);
2997 wfree(wstate->state);
2998 wfree(wstate);
2999 } else {
3000 while (tmp->next) {
3001 if (tmp->next->pid==pid) {
3002 wstate = tmp->next;
3003 tmp->next = wstate->next;
3004 #ifdef DEBUG
3005 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
3006 wstate, wstate->instance, wstate->class, wstate->command);
3007 #endif
3008 if (wstate->instance) wfree(wstate->instance);
3009 if (wstate->class) wfree(wstate->class);
3010 if (wstate->command) wfree(wstate->command);
3011 wfree(wstate->state);
3012 wfree(wstate);
3013 break;
3015 tmp = tmp->next;
3021 void
3022 wWindowSetOmnipresent(WWindow *wwin, Bool flag)
3024 if (wwin->flags.omnipresent == flag)
3025 return;
3027 wwin->flags.omnipresent = flag;
3028 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
3032 /* ====================================================================== */
3034 static void
3035 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3037 WWindow *wwin = data;
3039 #ifndef NUMLOCK_HACK
3040 if ((event->xbutton.state & ValidModMask)
3041 != (event->xbutton.state & ~LockMask)) {
3042 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3043 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3045 #endif
3047 event->xbutton.state &= ValidModMask;
3049 CloseWindowMenu(wwin->screen_ptr);
3051 if (wPreferences.focus_mode==WKF_CLICK
3052 && !(event->xbutton.state&ControlMask)
3053 && !WFLAGP(wwin, no_focusable)) {
3054 wSetFocusTo(wwin->screen_ptr, wwin);
3057 if (event->xbutton.button == Button1)
3058 wRaiseFrame(wwin->frame->core);
3060 if (event->xbutton.window != wwin->frame->resizebar->window) {
3061 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
3062 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3063 GrabModeAsync, GrabModeAsync, None,
3064 None, CurrentTime)!=GrabSuccess) {
3065 #ifdef DEBUG0
3066 wwarning("pointer grab failed for window move");
3067 #endif
3068 return;
3072 if (event->xbutton.state & MOD_MASK) {
3073 /* move the window */
3074 wMouseMoveWindow(wwin, event);
3075 XUngrabPointer(dpy, CurrentTime);
3076 } else {
3077 wMouseResizeWindow(wwin, event);
3078 XUngrabPointer(dpy, CurrentTime);
3084 static void
3085 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
3087 WWindow *wwin = data;
3089 event->xbutton.state &= ValidModMask;
3091 if (event->xbutton.button==Button1) {
3092 if (event->xbutton.state == 0) {
3093 if (!WFLAGP(wwin, no_shadeable)) {
3094 /* shade window */
3095 if (wwin->flags.shaded)
3096 wUnshadeWindow(wwin);
3097 else
3098 wShadeWindow(wwin);
3100 } else {
3101 int dir = 0;
3103 if (event->xbutton.state & ControlMask)
3104 dir |= MAX_VERTICAL;
3106 if (event->xbutton.state & ShiftMask) {
3107 dir |= MAX_HORIZONTAL;
3108 if (!(event->xbutton.state & ControlMask))
3109 wSelectWindow(wwin, !wwin->flags.selected);
3112 /* maximize window */
3113 if (dir!=0 && IS_RESIZABLE(wwin)) {
3114 int ndir = dir ^ wwin->flags.maximized;
3116 if (ndir != 0) {
3117 wMaximizeWindow(wwin, ndir);
3118 } else {
3119 wUnmaximizeWindow(wwin);
3123 } else if (event->xbutton.button==Button3) {
3124 if (event->xbutton.state & MOD_MASK) {
3125 wHideOtherApplications(wwin);
3127 } else if (event->xbutton.button==Button2) {
3128 wSelectWindow(wwin, !wwin->flags.selected);
3129 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
3130 wShadeWindow(wwin);
3131 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
3132 wUnshadeWindow(wwin);
3137 static void
3138 frameMouseDown(WObjDescriptor *desc, XEvent *event)
3140 WWindow *wwin = desc->parent;
3142 event->xbutton.state &= ValidModMask;
3144 CloseWindowMenu(wwin->screen_ptr);
3146 if (/*wPreferences.focus_mode==WKF_CLICK
3147 &&*/ !(event->xbutton.state&ControlMask)
3148 && !WFLAGP(wwin, no_focusable)) {
3149 wSetFocusTo(wwin->screen_ptr, wwin);
3151 if (event->xbutton.button == Button1) {
3152 wRaiseFrame(wwin->frame->core);
3155 if (event->xbutton.state & MOD_MASK) {
3156 /* move the window */
3157 if (XGrabPointer(dpy, wwin->client_win, False,
3158 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3159 GrabModeAsync, GrabModeAsync, None,
3160 None, CurrentTime)!=GrabSuccess) {
3161 #ifdef DEBUG0
3162 wwarning("pointer grab failed for window move");
3163 #endif
3164 return;
3166 if (event->xbutton.button == Button3)
3167 wMouseResizeWindow(wwin, event);
3168 else if (event->xbutton.button==Button1 || event->xbutton.button==Button2)
3169 wMouseMoveWindow(wwin, event);
3170 XUngrabPointer(dpy, CurrentTime);
3175 static void
3176 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3178 WWindow *wwin = (WWindow*)data;
3180 #ifndef NUMLOCK_HACK
3181 if ((event->xbutton.state & ValidModMask)
3182 != (event->xbutton.state & ~LockMask)) {
3183 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3184 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3186 #endif
3187 event->xbutton.state &= ValidModMask;
3189 CloseWindowMenu(wwin->screen_ptr);
3191 if (wPreferences.focus_mode==WKF_CLICK
3192 && !(event->xbutton.state&ControlMask)
3193 && !WFLAGP(wwin, no_focusable)) {
3194 wSetFocusTo(wwin->screen_ptr, wwin);
3197 if (event->xbutton.button == Button1
3198 || event->xbutton.button == Button2) {
3200 if (event->xbutton.button == Button1) {
3201 if (event->xbutton.state & MOD_MASK) {
3202 wLowerFrame(wwin->frame->core);
3203 } else {
3204 wRaiseFrame(wwin->frame->core);
3207 if ((event->xbutton.state & ShiftMask)
3208 && !(event->xbutton.state & ControlMask)) {
3209 wSelectWindow(wwin, !wwin->flags.selected);
3210 return;
3212 if (event->xbutton.window != wwin->frame->titlebar->window
3213 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3214 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3215 GrabModeAsync, GrabModeAsync, None,
3216 None, CurrentTime)!=GrabSuccess) {
3217 #ifdef DEBUG0
3218 wwarning("pointer grab failed for window move");
3219 #endif
3220 return;
3223 /* move the window */
3224 wMouseMoveWindow(wwin, event);
3226 XUngrabPointer(dpy, CurrentTime);
3227 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
3228 && !wwin->flags.internal_window
3229 && !WCHECK_STATE(WSTATE_MODAL)) {
3230 WObjDescriptor *desc;
3232 if (event->xbutton.window != wwin->frame->titlebar->window
3233 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3234 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3235 GrabModeAsync, GrabModeAsync, None,
3236 None, CurrentTime)!=GrabSuccess) {
3237 #ifdef DEBUG0
3238 wwarning("pointer grab failed for window move");
3239 #endif
3240 return;
3243 OpenWindowMenu(wwin, event->xbutton.x_root,
3244 wwin->frame_y+wwin->frame->top_width, False);
3246 /* allow drag select */
3247 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3248 event->xany.send_event = True;
3249 (*desc->handle_mousedown)(desc, event);
3251 XUngrabPointer(dpy, CurrentTime);
3257 static void
3258 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3260 WWindow *wwin = data;
3262 event->xbutton.state &= ValidModMask;
3264 CloseWindowMenu(wwin->screen_ptr);
3266 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3267 return;
3269 /* if control-click, kill the client */
3270 if (event->xbutton.state & ControlMask) {
3271 wClientKill(wwin);
3272 } else {
3273 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
3274 /* send delete message */
3275 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3281 static void
3282 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3284 WWindow *wwin = data;
3286 CloseWindowMenu(wwin->screen_ptr);
3288 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3289 return;
3291 /* send delete message */
3292 if (wwin->protocols.DELETE_WINDOW) {
3293 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3294 } else {
3295 wClientKill(wwin);
3300 #ifdef XKB_BUTTON_HINT
3301 static void
3302 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3304 WWindow *wwin = data;
3305 WFrameWindow *fwin = wwin->frame;
3306 WScreen *scr = fwin->screen_ptr;
3307 XkbStateRec staterec;
3308 int tl;
3310 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3311 return;
3312 tl = wwin->frame->languagemode;
3313 wwin->frame->languagemode = wwin->frame->last_languagemode;
3314 wwin->frame->last_languagemode = tl;
3315 wSetFocusTo(scr, wwin);
3316 wwin->frame->languagebutton_image =
3317 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3318 wwin->frame->languagemode];
3319 wFrameWindowUpdateLanguageButton(wwin->frame);
3320 if (event->xbutton.button == Button3)
3321 return;
3322 wRaiseFrame(fwin->core);
3324 #endif
3327 static void
3328 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3330 WWindow *wwin = data;
3332 event->xbutton.state &= ValidModMask;
3334 CloseWindowMenu(wwin->screen_ptr);
3336 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3337 return;
3339 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3340 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3341 LastTimestamp);
3342 } else {
3343 WApplication *wapp;
3344 if ((event->xbutton.state & ControlMask) ||
3345 (event->xbutton.button == Button3)) {
3347 wapp = wApplicationOf(wwin->main_window);
3348 if (wapp && !WFLAGP(wwin, no_appicon))
3349 wHideApplication(wapp);
3350 } else if (event->xbutton.state==0) {
3351 wIconifyWindow(wwin);