Fixed problem with not saving all windows in a session when the shared
[wmaker-crm.git] / src / window.c
blob73c806d4bc1db485ac4aa1817106d734619458bd
1 /* window.c - client window managing stuffs
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997-2002 Alfredo K. Kojima
6 *
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>
37 /* For getting mouse wheel mappings from WINGs */
38 #include <WINGs/WINGsP.h>
40 #include "WindowMaker.h"
41 #include "GNUstep.h"
42 #include "wcore.h"
43 #include "framewin.h"
44 #include "texture.h"
45 #include "window.h"
46 #include "winspector.h"
47 #include "icon.h"
48 #include "properties.h"
49 #include "actions.h"
50 #include "client.h"
51 #include "funcs.h"
52 #include "keybind.h"
53 #include "stacking.h"
54 #include "defaults.h"
55 #include "workspace.h"
58 #ifdef MWM_HINTS
59 # include "motif.h"
60 #endif
61 #ifdef KWM_HINTS
62 # include "kwm.h"
63 #endif
64 #ifdef GNOME_STUFF
65 # include "gnome.h"
66 #endif
67 #ifdef OLWM_HINTS
68 # include "openlook.h"
69 #endif
71 /****** Global Variables ******/
73 extern WShortKey wKeyBindings[WKBD_LAST];
75 #ifdef SHAPE
76 extern Bool wShapeSupported;
77 #endif
79 /* contexts */
80 extern XContext wWinContext;
82 /* cursors */
83 extern Cursor wCursor[WCUR_LAST];
85 /* protocol atoms */
86 extern Atom _XA_WM_DELETE_WINDOW;
87 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
89 extern Atom _XA_WINDOWMAKER_STATE;
91 extern WPreferences wPreferences;
93 #define MOD_MASK wPreferences.modifier_mask
95 extern Time LastTimestamp;
97 /* superfluous... */
98 extern void DoWindowBirth(WWindow*);
102 /***** Local Stuff *****/
105 static WWindowState *windowState=NULL;
109 /* local functions */
110 static FocusMode getFocusMode(WWindow *wwin);
112 static int getSavedState(Window window, WSavedState **state);
114 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
116 /* event handlers */
119 /* frame window (during window grabs) */
120 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
122 /* close button */
123 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
124 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
126 /* iconify button */
127 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
129 #ifdef XKB_BUTTON_HINT
130 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
131 #endif
133 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
134 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
136 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
139 /****** Notification Observers ******/
141 static void
142 appearanceObserver(void *self, WMNotification *notif)
144 WWindow *wwin = (WWindow*)self;
145 int flags = (int)WMGetNotificationClientData(notif);
147 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
148 return;
150 if (flags & WFontSettings) {
151 wWindowConfigureBorders(wwin);
152 if(wwin->flags.shaded) {
153 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
154 wwin->frame->top_width - 1);
156 wwin->client.y = wwin->frame_y - wwin->client.height
157 + wwin->frame->top_width;
158 wWindowSynthConfigureNotify(wwin);
161 if (flags & WTextureSettings) {
162 wwin->frame->flags.need_texture_remake = 1;
164 if (flags & (WTextureSettings | WColorSettings)) {
165 if (wwin->frame->titlebar)
166 XClearWindow(dpy, wwin->frame->titlebar->window);
168 wFrameWindowPaint(wwin->frame);
172 /************************************/
174 WWindow*
175 wWindowFor(Window window)
177 WObjDescriptor *desc;
179 if (window==None)
180 return NULL;
182 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
183 return NULL;
185 if (desc->parent_type==WCLASS_WINDOW)
186 return desc->parent;
187 else if (desc->parent_type==WCLASS_FRAME) {
188 WFrameWindow *frame = (WFrameWindow*)desc->parent;
189 if (frame->flags.is_client_window_frame)
190 return frame->child;
193 return NULL;
197 WWindow*
198 wWindowCreate()
200 WWindow *wwin;
202 wwin = wmalloc(sizeof(WWindow));
203 wretain(wwin);
205 memset(wwin, 0, sizeof(WWindow));
207 wwin->client_descriptor.handle_mousedown = frameMouseDown;
208 wwin->client_descriptor.parent = wwin;
209 wwin->client_descriptor.self = wwin;
210 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
212 return wwin;
216 void
217 wWindowDestroy(WWindow *wwin)
219 int i;
221 if (wwin->screen_ptr->cmap_window == wwin) {
222 wwin->screen_ptr->cmap_window = NULL;
225 WMRemoveNotificationObserver(wwin);
227 wwin->flags.destroyed = 1;
229 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
230 if (!wwin->screen_ptr->shortcutWindows[i])
231 continue;
233 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
235 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
236 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
237 wwin->screen_ptr->shortcutWindows[i] = NULL;
241 if (wwin->fake_group && wwin->fake_group->retainCount>0) {
242 wwin->fake_group->retainCount--;
243 if (wwin->fake_group->retainCount==0 && wwin->fake_group->leader!=None) {
244 XDestroyWindow(dpy, wwin->fake_group->leader);
245 wwin->fake_group->leader = None;
246 wwin->fake_group->origLeader = None;
247 XFlush(dpy);
251 if (wwin->normal_hints)
252 XFree(wwin->normal_hints);
254 if (wwin->wm_hints)
255 XFree(wwin->wm_hints);
257 if (wwin->wm_instance)
258 XFree(wwin->wm_instance);
260 if (wwin->wm_class)
261 XFree(wwin->wm_class);
263 if (wwin->wm_gnustep_attr)
264 wfree(wwin->wm_gnustep_attr);
266 if (wwin->cmap_windows)
267 XFree(wwin->cmap_windows);
269 XDeleteContext(dpy, wwin->client_win, wWinContext);
271 if (wwin->frame)
272 wFrameWindowDestroy(wwin->frame);
274 if (wwin->icon) {
275 RemoveFromStackList(wwin->icon->core);
276 wIconDestroy(wwin->icon);
277 if (wPreferences.auto_arrange_icons)
278 wArrangeIcons(wwin->screen_ptr, True);
280 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;
346 void
347 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
348 WWindowAttributes *mask)
350 if (wflags->no_appicon && mask->no_appicon)
351 wflags->emulate_appicon = 0;
353 if (wwin->main_window!=None) {
354 WApplication *wapp = wApplicationOf(wwin->main_window);
355 if (wapp && !wapp->flags.emulated)
356 wflags->emulate_appicon = 0;
359 if (wwin->transient_for!=None
360 && wwin->transient_for!=wwin->screen_ptr->root_win)
361 wflags->emulate_appicon = 0;
363 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
364 wflags->sunken = 0;
369 void
370 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
372 WScreen *scr = wwin->screen_ptr;
374 /* sets global default stuff */
375 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
376 &wwin->client_flags, NULL, True);
378 * Decoration setting is done in this precedence (lower to higher)
379 * - use global default in the resource database
380 * - guess some settings
381 * - use GNUstep/external window attributes
382 * - set hints specified for the app in the resource DB
385 WSETUFLAG(wwin, broken_close, 0);
387 if (wwin->protocols.DELETE_WINDOW)
388 WSETUFLAG(wwin, kill_close, 0);
389 else
390 WSETUFLAG(wwin, kill_close, 1);
392 /* transients can't be iconified or maximized */
393 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
394 WSETUFLAG(wwin, no_miniaturizable, 1);
395 WSETUFLAG(wwin, no_miniaturize_button, 1);
398 /* if the window can't be resized, remove the resizebar */
399 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
400 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
401 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
402 WSETUFLAG(wwin, no_resizable, 1);
403 WSETUFLAG(wwin, no_resizebar, 1);
406 /* set GNUstep window attributes */
407 if (wwin->wm_gnustep_attr) {
408 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
410 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
412 *level = wwin->wm_gnustep_attr->window_level;
414 * INT_MIN is the only illegal window level.
416 if (*level == INT_MIN)
417 *level = INT_MIN + 1;
418 } else {
419 /* setup defaults */
420 *level = WMNormalLevel;
422 } else {
423 int tmp_workspace = -1;
424 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
425 Bool check;
427 check = False;
429 #ifdef MWM_HINTS
430 wMWMCheckClientHints(wwin);
431 #endif /* MWM_HINTS */
433 #ifdef GNOME_STUFF
434 check = wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
435 #endif /* GNOME_STUFF */
437 #ifdef KWM_HINTS
438 if (!check)
439 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
440 #endif /* KWM_HINTS */
442 #ifdef OLWM_HINTS
443 wOLWMCheckClientHints(wwin);
444 #endif /* OLWM_HINTS */
446 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
447 * have INT_MIN that means that no window level was requested. -Dan
449 if (tmp_level == INT_MIN) {
450 if (WFLAGP(wwin, floating))
451 *level = WMFloatingLevel;
452 else if (WFLAGP(wwin, sunken))
453 *level = WMSunkenLevel;
454 else
455 *level = WMNormalLevel;
456 } else {
457 *level = tmp_level;
460 if (tmp_workspace >= 0) {
461 *workspace = tmp_workspace % scr->workspace_count;
466 * Set attributes specified only for that window/class.
467 * This might do duplicate work with the 1st wDefaultFillAttributes().
469 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
470 &wwin->user_flags, &wwin->defined_user_flags,
471 False);
473 * Sanity checks for attributes that depend on other attributes
475 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
476 wwin->user_flags.emulate_appicon = 0;
477 //WSETUFLAG(wwin, 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;
483 //WSETUFLAG(wwin, emulate_appicon, 0);
486 if (wwin->transient_for!=None
487 && wwin->transient_for!=wwin->screen_ptr->root_win)
488 wwin->user_flags.emulate_appicon = 0;
489 //WSETUFLAG(wwin, emulate_appicon, 0);
491 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
492 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
493 wwin->user_flags.sunken = 0;
494 //WSETUFLAG(wwin, sunken, 0);
496 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
502 Bool
503 wWindowCanReceiveFocus(WWindow *wwin)
505 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
506 return False;
507 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
508 return False;
509 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
510 return False;
512 return True;
516 Bool
517 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
519 int w1, h1, w2, h2;
521 w1 = wwin->frame->core->width;
522 h1 = wwin->frame->core->height;
523 w2 = obscured->frame->core->width;
524 h2 = obscured->frame->core->height;
526 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
527 && wwin->frame->workspace != obscured->frame->workspace)
528 return False;
530 if (wwin->frame_x + w1 < obscured->frame_x
531 || wwin->frame_y + h1 < obscured->frame_y
532 || wwin->frame_x > obscured->frame_x + w2
533 || wwin->frame_y > obscured->frame_y + h2) {
534 return False;
537 return True;
541 static Window
542 createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
544 XClassHint *classHint;
545 XWMHints *hints;
546 Window leader;
547 int argc;
548 char **argv;
550 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
551 /* set class hint */
552 classHint = XAllocClassHint();
553 classHint->res_name = instance;
554 classHint->res_class = class;
555 XSetClassHint(dpy, leader, classHint);
556 XFree(classHint);
558 /* inherit these from the original leader if available */
559 hints = XGetWMHints(dpy, win);
560 if (!hints) {
561 hints = XAllocWMHints();
562 hints->flags = 0;
564 /* set window group leader to self */
565 hints->window_group = leader;
566 hints->flags |= WindowGroupHint;
567 XSetWMHints(dpy, leader, hints);
568 XFree(hints);
570 if (XGetCommand(dpy, win, &argv, &argc)!=0 && argc > 0) {
571 XSetCommand(dpy, leader, argv, argc);
572 XFreeStringList(argv);
575 return leader;
579 static int
580 matchIdentifier(void *item, void *cdata)
582 return (strcmp(((WFakeGroupLeader*)item)->identifier, (char*)cdata)==0);
587 *----------------------------------------------------------------
588 * wManageWindow--
589 * reparents the window and allocates a descriptor for it.
590 * Window manager hints and other hints are fetched to configure
591 * the window decoration attributes and others. User preferences
592 * for the window are used if available, to configure window
593 * decorations and some behaviour.
594 * If in startup, windows that are override redirect,
595 * unmapped and never were managed and are Withdrawn are not
596 * managed.
598 * Returns:
599 * the new window descriptor
601 * Side effects:
602 * The window is reparented and appropriate notification
603 * is done to the client. Input mask for the window is setup.
604 * The window descriptor is also associated with various window
605 * contexts and inserted in the head of the window list.
606 * Event handler contexts are associated for some objects
607 * (buttons, titlebar and resizebar)
609 *----------------------------------------------------------------
611 WWindow*
612 wManageWindow(WScreen *scr, Window window)
614 WWindow *wwin;
615 int x, y;
616 unsigned width, height;
617 XWindowAttributes wattribs;
618 XSetWindowAttributes attribs;
619 WWindowState *win_state;
620 WWindow *transientOwner = NULL;
621 int window_level;
622 int wm_state;
623 int foo;
624 int workspace = -1;
625 char *title;
626 Bool withdraw = False;
627 Bool raise = False;
629 /* mutex. */
630 /* XGrabServer(dpy); */
631 XSync(dpy, False);
632 /* make sure the window is still there */
633 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
634 XUngrabServer(dpy);
635 return NULL;
638 /* if it's an override-redirect, ignore it */
639 if (wattribs.override_redirect) {
640 XUngrabServer(dpy);
641 return NULL;
644 wm_state = PropGetWindowState(window);
646 /* if it's startup and the window is unmapped, don't manage it */
647 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
648 XUngrabServer(dpy);
649 return NULL;
652 if (!wFetchName(dpy, window, &title)) {
653 title = NULL;
656 #ifdef KWM_HINTS
657 if (title && !wKWMManageableClient(scr, window, title)) {
658 XFree(title);
659 XUngrabServer(dpy);
660 return NULL;
662 #endif /* KWM_HINTS */
665 wwin = wWindowCreate();
667 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
669 #ifdef DEBUG
670 printf("managing window %x\n", (unsigned)window);
671 #endif
673 #ifdef SHAPE
674 if (wShapeSupported) {
675 int junk;
676 unsigned int ujunk;
677 int b_shaped;
679 XShapeSelectInput(dpy, window, ShapeNotifyMask);
680 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
681 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
682 wwin->flags.shaped = b_shaped;
684 #endif
687 *--------------------------------------------------
689 * Get hints and other information in properties
691 *--------------------------------------------------
693 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
695 /* setup descriptor */
696 wwin->client_win = window;
697 wwin->screen_ptr = scr;
699 wwin->old_border_width = wattribs.border_width;
701 wwin->event_mask = CLIENT_EVENTS;
702 attribs.event_mask = CLIENT_EVENTS;
703 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
704 attribs.save_under = False;
705 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
706 |CWSaveUnder, &attribs);
707 XSetWindowBorderWidth(dpy, window, 0);
709 /* get hints from GNUstep app */
710 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
711 wwin->flags.is_gnustep = 1;
713 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
714 wwin->wm_gnustep_attr = NULL;
717 wwin->client_leader = PropGetClientLeader(window);
718 if (wwin->client_leader!=None)
719 wwin->main_window = wwin->client_leader;
721 wwin->wm_hints = XGetWMHints(dpy, window);
723 if (wwin->wm_hints) {
724 if (wwin->wm_hints->flags & StateHint) {
726 if (wwin->wm_hints->initial_state == IconicState) {
728 wwin->flags.miniaturized = 1;
730 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
732 withdraw = True;
736 if (wwin->wm_hints->flags & WindowGroupHint) {
737 wwin->group_id = wwin->wm_hints->window_group;
738 /* window_group has priority over CLIENT_LEADER */
739 wwin->main_window = wwin->group_id;
740 } else {
741 wwin->group_id = None;
744 if (wwin->wm_hints->flags & UrgencyHint)
745 wwin->flags.urgent = 1;
746 } else {
747 wwin->group_id = None;
750 PropGetProtocols(window, &wwin->protocols);
752 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
753 wwin->transient_for = None;
754 } else {
755 if (wwin->transient_for==None || wwin->transient_for==window) {
756 wwin->transient_for = scr->root_win;
757 } else {
758 transientOwner = wWindowFor(wwin->transient_for);
759 if (transientOwner && transientOwner->main_window!=None) {
760 wwin->main_window = transientOwner->main_window;
761 } /*else {
762 wwin->main_window = None;
767 /* guess the focus mode */
768 wwin->focus_mode = getFocusMode(wwin);
770 /* get geometry stuff */
771 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
773 /* get colormap windows */
774 GetColormapWindows(wwin);
777 *--------------------------------------------------
779 * Setup the decoration/window attributes and
780 * geometry
782 *--------------------------------------------------
785 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
787 #ifdef OLWM_HINTS
788 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
789 && wwin->group_id != None && wwin->group_id != window) {
791 transientOwner = wWindowFor(wwin->group_id);
793 if (transientOwner) {
794 wwin->transient_for = wwin->group_id;
796 /* transients can't be iconified or maximized */
797 if (wwin->transient_for) {
798 WSETUFLAG(wwin, no_miniaturizable, 1);
799 WSETUFLAG(wwin, no_miniaturize_button, 1);
803 #endif /* OLWM_HINTS */
805 /* Make broken apps behave as a nice app. */
806 if (WFLAGP(wwin, emulate_appicon)) {
807 wwin->main_window = wwin->client_win;
810 wwin->orig_main_window = wwin->main_window;
812 if (wwin->flags.is_gnustep) {
813 WSETUFLAG(wwin, shared_appicon, 0);
816 if (wwin->main_window) {
817 extern Atom _XA_WINDOWMAKER_MENU;
818 XTextProperty text_prop;
820 if (XGetTextProperty(dpy, wwin->main_window, &text_prop,
821 _XA_WINDOWMAKER_MENU)) {
822 WSETUFLAG(wwin, shared_appicon, 0);
826 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
827 char *buffer, *instance, *class;
828 WFakeGroupLeader *fPtr;
829 int index;
831 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
833 PropGetWMClass(wwin->main_window, &class, &instance);
834 buffer = wmalloc(strlen(instance)+strlen(class)+2);
835 sprintf(buffer, "%s.%s", instance, class);
837 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void*)buffer);
838 if (index != WANotFound) {
839 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
840 if (fPtr->retainCount == 0) {
841 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
842 instance, class);
844 fPtr->retainCount++;
845 #undef method2
846 if (fPtr->origLeader==None) {
847 #ifdef method2
848 if (ADEQUATE(wwin->group_id)) {
849 fPtr->retainCount++;
850 fPtr->origLeader = wwin->group_id;
851 } else if (ADEQUATE(wwin->client_leader)) {
852 fPtr->retainCount++;
853 fPtr->origLeader = wwin->client_leader;
854 } else if (ADEQUATE(wwin->main_window)) {
855 fPtr->retainCount++;
856 fPtr->origLeader = wwin->main_window;
858 #else
859 if (ADEQUATE(wwin->main_window)) {
860 fPtr->retainCount++;
861 fPtr->origLeader = wwin->main_window;
863 #endif
865 wwin->fake_group = fPtr;
866 /*wwin->group_id = fPtr->leader;*/
867 wwin->main_window = fPtr->leader;
868 wfree(buffer);
869 } else {
870 fPtr = (WFakeGroupLeader*)wmalloc(sizeof(WFakeGroupLeader));
872 fPtr->identifier = buffer;
873 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
874 instance, class);
875 fPtr->origLeader = None;
876 fPtr->retainCount = 1;
878 WMAddToArray(scr->fakeGroupLeaders, fPtr);
880 #ifdef method2
881 if (ADEQUATE(wwin->group_id)) {
882 fPtr->retainCount++;
883 fPtr->origLeader = wwin->group_id;
884 } else if (ADEQUATE(wwin->client_leader)) {
885 fPtr->retainCount++;
886 fPtr->origLeader = wwin->client_leader;
887 } else if (ADEQUATE(wwin->main_window)) {
888 fPtr->retainCount++;
889 fPtr->origLeader = wwin->main_window;
891 #else
892 if (ADEQUATE(wwin->main_window)) {
893 fPtr->retainCount++;
894 fPtr->origLeader = wwin->main_window;
896 #endif
897 wwin->fake_group = fPtr;
898 /*wwin->group_id = fPtr->leader;*/
899 wwin->main_window = fPtr->leader;
901 if (instance)
902 XFree(instance);
903 if (class)
904 XFree(class);
906 #undef method2
907 #undef ADEQUATE
911 *------------------------------------------------------------
913 * Setup the initial state of the window
915 *------------------------------------------------------------
918 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
919 wwin->flags.miniaturized = 1;
922 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
923 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
927 Bool bla;
929 bla = False;
930 #ifdef GNOME_STUFF
931 bla = wGNOMECheckInitialClientState(wwin);
932 #endif
933 #ifdef KWM_HINTS
934 if (!bla)
935 wKWMCheckClientInitialState(wwin);
936 #endif
939 /* apply previous state if it exists and we're in startup */
940 if (scr->flags.startup && wm_state >= 0) {
942 if (wm_state == IconicState) {
944 wwin->flags.miniaturized = 1;
946 } else if (wm_state == WithdrawnState) {
948 withdraw = True;
952 /* if there is a saved state (from file), restore it */
953 win_state = NULL;
954 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
955 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
956 } else {
957 win_state = (WWindowState*)wWindowGetSavedState(window);
959 if (win_state && !withdraw) {
961 if (win_state->state->hidden>0)
962 wwin->flags.hidden = win_state->state->hidden;
964 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
965 wwin->flags.shaded = win_state->state->shaded;
967 if (win_state->state->miniaturized>0 &&
968 !WFLAGP(wwin, no_miniaturizable)) {
969 wwin->flags.miniaturized = win_state->state->miniaturized;
972 if (!IS_OMNIPRESENT(wwin)) {
973 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
974 wwin->wm_class);
975 if (w < 0 || w >= scr->workspace_count) {
976 workspace = win_state->state->workspace;
977 if (workspace >= scr->workspace_count)
978 workspace = scr->current_workspace;
979 } else {
980 workspace = w;
982 } else {
983 workspace = scr->current_workspace;
987 /* if we're restarting, restore saved state (from hints).
988 * This will overwrite previous */
990 WSavedState *wstate;
992 if (getSavedState(window, &wstate)) {
993 wwin->flags.shaded = wstate->shaded;
994 wwin->flags.hidden = wstate->hidden;
995 wwin->flags.miniaturized = wstate->miniaturized;
996 wwin->flags.maximized = wstate->maximized;
997 if (wwin->flags.maximized) {
998 wwin->old_geometry.x = wstate->x;
999 wwin->old_geometry.y = wstate->y;
1000 wwin->old_geometry.width = wstate->w;
1001 wwin->old_geometry.height = wstate->h;
1004 workspace = wstate->workspace;
1005 } else {
1006 wstate = NULL;
1009 /* restore window shortcut */
1010 if (wstate != NULL || win_state != NULL) {
1011 unsigned mask = 0;
1013 if (win_state != NULL)
1014 mask = win_state->state->window_shortcuts;
1016 if (wstate != NULL && mask == 0)
1017 mask = wstate->window_shortcuts;
1019 if (mask > 0) {
1020 int i;
1022 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1023 if (mask & (1<<i)) {
1024 if (!scr->shortcutWindows[i])
1025 scr->shortcutWindows[i] = WMCreateArray(4);
1027 WMAddToArray(scr->shortcutWindows[i], wwin);
1032 if (wstate != NULL) {
1033 wfree(wstate);
1037 /* don't let transients start miniaturized if their owners are not */
1038 if (transientOwner && !transientOwner->flags.miniaturized
1039 && wwin->flags.miniaturized && !withdraw) {
1040 wwin->flags.miniaturized = 0;
1041 if (wwin->wm_hints)
1042 wwin->wm_hints->initial_state = NormalState;
1045 /* set workspace on which the window starts */
1046 if (workspace >= 0) {
1047 if (workspace > scr->workspace_count-1) {
1048 workspace = workspace % scr->workspace_count;
1050 } else {
1051 int w;
1053 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1055 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1057 workspace = w;
1059 } else {
1060 if (wPreferences.open_transients_with_parent && transientOwner) {
1062 workspace = transientOwner->frame->workspace;
1064 } else {
1066 workspace = scr->current_workspace;
1071 /* setup window geometry */
1072 if (win_state && win_state->state->w > 0) {
1073 width = win_state->state->w;
1074 height = win_state->state->h;
1076 wWindowConstrainSize(wwin, &width, &height);
1078 /* do not ask for window placement if the window is
1079 * transient, during startup, if the initial workspace is another one
1080 * or if the window wants to start iconic.
1081 * If geometry was saved, restore it. */
1083 Bool dontBring = False;
1085 if (win_state && win_state->state->w > 0) {
1086 x = win_state->state->x;
1087 y = win_state->state->y;
1088 } else if ((wwin->transient_for==None
1089 || wPreferences.window_placement!=WPM_MANUAL)
1090 && !scr->flags.startup
1091 && workspace == scr->current_workspace
1092 && !wwin->flags.miniaturized
1093 && !wwin->flags.maximized
1094 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
1096 if (transientOwner && transientOwner->flags.mapped) {
1097 int offs = WMAX(20, 2*transientOwner->frame->top_width);
1099 x = transientOwner->frame_x +
1100 abs((transientOwner->frame->core->width - width)/2) + offs;
1101 y = transientOwner->frame_y +
1102 abs((transientOwner->frame->core->height - height)/3) + offs;
1104 if (x < 0)
1105 x = 0;
1106 else if (x + width > scr->scr_width)
1107 x = scr->scr_width - width;
1109 if (y < 0)
1110 y = 0;
1111 else if (y + height > scr->scr_height)
1112 y = scr->scr_height - height;
1113 } else {
1114 PlaceWindow(wwin, &x, &y, width, height);
1116 if (wPreferences.window_placement == WPM_MANUAL)
1117 dontBring = True;
1120 if (WFLAGP(wwin, dont_move_off) && dontBring)
1121 wScreenBringInside(scr, &x, &y, width, height);
1124 if (wwin->flags.urgent) {
1125 if (!IS_OMNIPRESENT(wwin))
1126 wwin->flags.omnipresent ^= 1;
1130 *--------------------------------------------------
1132 * Create frame, borders and do reparenting
1134 *--------------------------------------------------
1136 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1137 #ifdef XKB_BUTTON_HINT
1138 if (wPreferences.modelock)
1139 foo |= WFF_LANGUAGE_BUTTON;
1140 #endif
1141 if (!WFLAGP(wwin, no_titlebar))
1142 foo |= WFF_TITLEBAR;
1143 if (!WFLAGP(wwin, no_resizebar))
1144 foo |= WFF_RESIZEBAR;
1145 if (!WFLAGP(wwin, no_border))
1146 foo |= WFF_BORDER;
1148 wwin->frame = wFrameWindowCreate(scr, window_level,
1149 x, y, width, height,
1150 &wPreferences.window_title_clearance, foo,
1151 scr->window_title_texture,
1152 scr->resizebar_texture,
1153 scr->window_title_pixel,
1154 &scr->window_title_gc,
1155 &scr->title_font);
1157 wwin->frame->flags.is_client_window_frame = 1;
1158 wwin->frame->flags.justification = wPreferences.title_justification;
1160 /* setup button images */
1161 wWindowUpdateButtonImages(wwin);
1163 /* hide unused buttons */
1164 foo = 0;
1165 if (WFLAGP(wwin, no_close_button))
1166 foo |= WFF_RIGHT_BUTTON;
1167 if (WFLAGP(wwin, no_miniaturize_button))
1168 foo |= WFF_LEFT_BUTTON;
1169 #ifdef XKB_BUTTON_HINT
1170 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1171 foo |= WFF_LANGUAGE_BUTTON;
1172 #endif
1173 if (foo!=0)
1174 wFrameWindowHideButton(wwin->frame, foo);
1176 wwin->frame->child = wwin;
1178 #ifdef OLWM_HINTS
1179 /* emulate olwm push pin. Make the button look as pushed-in for
1180 * the pinned-out state. When the button is clicked, it will
1181 * revert to the normal position, which means the pin is pinned-in.
1183 if (wwin->flags.olwm_push_pin_out)
1184 wFrameWindowUpdatePushButton(wwin->frame, True);
1185 #endif /* OLWM_HINTS */
1188 wwin->frame->workspace = workspace;
1190 wwin->frame->on_click_left = windowIconifyClick;
1191 #ifdef XKB_BUTTON_HINT
1192 if (wPreferences.modelock)
1193 wwin->frame->on_click_language = windowLanguageClick;
1194 #endif
1196 wwin->frame->on_click_right = windowCloseClick;
1197 wwin->frame->on_dblclick_right = windowCloseDblClick;
1199 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1200 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1202 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1205 XSelectInput(dpy, wwin->client_win,
1206 wwin->event_mask & ~StructureNotifyMask);
1208 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1209 0, wwin->frame->top_width);
1211 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1215 int gx, gy;
1217 wClientGetGravityOffsets(wwin, &gx, &gy);
1219 /* if gravity is to the south, account for the border sizes */
1220 if (gy > 0)
1221 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1225 * wWindowConfigure() will init the client window's size
1226 * (wwin->client.{width,height}) and all other geometry
1227 * related variables (frame_x,frame_y)
1229 wWindowConfigure(wwin, x, y, width, height);
1231 /* to make sure the window receives it's new position after reparenting */
1232 wWindowSynthConfigureNotify(wwin);
1235 *--------------------------------------------------
1237 * Setup descriptors and save window to internal
1238 * lists
1240 *--------------------------------------------------
1243 if (wwin->main_window!=None) {
1244 WApplication *app;
1245 WWindow *leader;
1247 /* Leader windows do not necessary set themselves as leaders.
1248 * If this is the case, point the leader of this window to
1249 * itself */
1250 leader = wWindowFor(wwin->main_window);
1251 if (leader && leader->main_window==None) {
1252 leader->main_window = leader->client_win;
1254 app = wApplicationCreate(scr, wwin->main_window);
1255 if (app) {
1256 app->last_workspace = workspace;
1258 app->main_window_desc->fake_group = wwin->fake_group;
1261 * Do application specific stuff, like setting application
1262 * wide attributes.
1265 if (wwin->flags.hidden) {
1266 /* if the window was set to hidden because it was hidden
1267 * in a previous incarnation and that state was restored */
1268 app->flags.hidden = 1;
1269 } else if (app->flags.hidden) {
1270 if (WFLAGP(app->main_window_desc, start_hidden)) {
1271 wwin->flags.hidden = 1;
1272 } else {
1273 wUnhideApplication(app, False, False);
1274 raise = True;
1280 /* setup the frame descriptor */
1281 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1282 wwin->frame->core->descriptor.parent = wwin;
1283 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1285 /* don't let windows go away if we die */
1286 XAddToSaveSet(dpy, window);
1288 XLowerWindow(dpy, window);
1290 /* if window is in this workspace and should be mapped, then map it */
1291 if (!wwin->flags.miniaturized
1292 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1293 && !wwin->flags.hidden && !withdraw) {
1295 /* The following "if" is to avoid crashing of clients that expect
1296 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1297 * after the return from this function.
1299 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1300 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1301 } else {
1302 wClientSetState(wwin, NormalState, None);
1305 #if 0
1306 /* if not auto focus, then map the window under the currently
1307 * focused window */
1308 #define _WIDTH(w) (w)->frame->core->width
1309 #define _HEIGHT(w) (w)->frame->core->height
1310 if (!wPreferences.auto_focus && scr->focused_window
1311 && !scr->flags.startup && !transientOwner
1312 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1313 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1314 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1315 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1316 || wwin->flags.maximized)) {
1317 MoveInStackListUnder(scr->focused_window->frame->core,
1318 wwin->frame->core);
1320 #undef _WIDTH
1321 #undef _HEIGHT
1323 #endif
1325 if (wPreferences.superfluous && !wPreferences.no_animations
1326 && !scr->flags.startup &&
1327 (wwin->transient_for==None || wwin->transient_for==scr->root_win)
1329 * The brain damaged idiotic non-click to focus modes will
1330 * have trouble with this because:
1332 * 1. window is created and mapped by the client
1333 * 2. window is mapped by wmaker in small size
1334 * 3. window is animated to grow to normal size
1335 * 4. this function returns to normal event loop
1336 * 5. eventually, the EnterNotify event that would trigger
1337 * the window focusing (if the mouse is over that window)
1338 * will be processed by wmaker.
1339 * But since this event will be rather delayed
1340 * (step 3 has a large delay) the time when the event ocurred
1341 * and when it is processed, the client that owns that window
1342 * will reject the XSetInputFocus() for it.
1344 && (wPreferences.focus_mode==WKF_CLICK
1345 || wPreferences.auto_focus)) {
1346 DoWindowBirth(wwin);
1349 wWindowMap(wwin);
1352 /* setup stacking descriptor */
1353 if (transientOwner) {
1354 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1355 } else {
1356 wwin->frame->core->stacking->child_of = NULL;
1360 if (!scr->focused_window) {
1361 /* first window on the list */
1362 wwin->next = NULL;
1363 wwin->prev = NULL;
1364 scr->focused_window = wwin;
1365 } else {
1366 WWindow *tmp;
1368 /* add window at beginning of focus window list */
1369 tmp = scr->focused_window;
1370 while (tmp->prev)
1371 tmp = tmp->prev;
1372 tmp->prev = wwin;
1373 wwin->next = tmp;
1374 wwin->prev = NULL;
1377 /* raise is set to true if we un-hid the app when this window was born.
1378 * we raise, else old windows of this app will be above this new one. */
1379 if (raise) {
1380 wRaiseFrame(wwin->frame->core);
1383 /* Update name must come after WApplication stuff is done */
1384 wWindowUpdateName(wwin, title);
1385 if (title)
1386 XFree(title);
1388 XUngrabServer(dpy);
1391 *--------------------------------------------------
1393 * Final preparations before window is ready to go
1395 *--------------------------------------------------
1398 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1401 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1402 && !wwin->flags.hidden) {
1403 if (((transientOwner && transientOwner->flags.focused)
1404 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1405 wSetFocusTo(scr, wwin);
1407 wWindowResetMouseGrabs(wwin);
1409 if (!WFLAGP(wwin, no_bind_keys)) {
1410 wWindowSetKeyGrabs(wwin);
1414 WMPostNotificationName(WMNManaged, wwin, NULL);
1417 wColormapInstallForWindow(scr, scr->cmap_window);
1420 #ifdef OLWM_HINTS
1421 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1422 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1424 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1425 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1426 wwin->frame_y);
1428 #endif
1431 *------------------------------------------------------------
1432 * Setup Notification Observers
1433 *------------------------------------------------------------
1435 WMAddNotificationObserver(appearanceObserver, wwin,
1436 WNWindowAppearanceSettingsChanged, wwin);
1440 *--------------------------------------------------
1442 * Cleanup temporary stuff
1444 *--------------------------------------------------
1447 if (win_state)
1448 wWindowDeleteSavedState(win_state);
1450 /* If the window must be withdrawed, then do it now.
1451 * Must do some optimization, 'though */
1452 if (withdraw) {
1453 wwin->flags.mapped = 0;
1454 wClientSetState(wwin, WithdrawnState, None);
1455 wUnmanageWindow(wwin, True, False);
1456 wwin = NULL;
1459 return wwin;
1466 WWindow*
1467 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1468 char *title, int x, int y, int width, int height)
1470 WWindow *wwin;
1471 int foo;
1473 wwin = wWindowCreate();
1475 WMAddNotificationObserver(appearanceObserver, wwin,
1476 WNWindowAppearanceSettingsChanged, wwin);
1478 wwin->flags.internal_window = 1;
1480 WSETUFLAG(wwin, omnipresent, 1);
1481 WSETUFLAG(wwin, no_shadeable, 1);
1482 WSETUFLAG(wwin, no_resizable, 1);
1483 WSETUFLAG(wwin, no_miniaturizable, 1);
1485 wwin->focus_mode = WFM_PASSIVE;
1487 wwin->client_win = window;
1488 wwin->screen_ptr = scr;
1490 wwin->transient_for = owner;
1492 wwin->client.x = x;
1493 wwin->client.y = y;
1494 wwin->client.width = width;
1495 wwin->client.height = height;
1497 wwin->frame_x = wwin->client.x;
1498 wwin->frame_y = wwin->client.y;
1501 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1502 foo |= WFF_TITLEBAR;
1503 #ifdef XKB_BUTTON_HINT
1504 foo |= WFF_LANGUAGE_BUTTON;
1505 #endif
1507 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1508 wwin->frame_x, wwin->frame_y,
1509 width, height,
1510 &wPreferences.window_title_clearance, foo,
1511 scr->window_title_texture,
1512 scr->resizebar_texture,
1513 scr->window_title_pixel,
1514 &scr->window_title_gc,
1515 &scr->title_font);
1517 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1519 wwin->frame->flags.is_client_window_frame = 1;
1520 wwin->frame->flags.justification = wPreferences.title_justification;
1522 wFrameWindowChangeTitle(wwin->frame, title);
1524 /* setup button images */
1525 wWindowUpdateButtonImages(wwin);
1527 /* hide buttons */
1528 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1530 wwin->frame->child = wwin;
1532 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1534 #ifdef XKB_BUTTON_HINT
1535 if (wPreferences.modelock)
1536 wwin->frame->on_click_language = windowLanguageClick;
1537 #endif
1539 wwin->frame->on_click_right = windowCloseClick;
1541 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1542 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1544 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1546 wwin->client.y += wwin->frame->top_width;
1547 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1548 0, wwin->frame->top_width);
1550 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1551 wwin->client.width, wwin->client.height);
1553 /* setup the frame descriptor */
1554 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1555 wwin->frame->core->descriptor.parent = wwin;
1556 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1559 XLowerWindow(dpy, window);
1560 XMapSubwindows(dpy, wwin->frame->core->window);
1562 /* setup stacking descriptor */
1563 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
1564 WWindow *tmp;
1565 tmp = wWindowFor(wwin->transient_for);
1566 if (tmp)
1567 wwin->frame->core->stacking->child_of = tmp->frame->core;
1568 } else {
1569 wwin->frame->core->stacking->child_of = NULL;
1573 if (!scr->focused_window) {
1574 /* first window on the list */
1575 wwin->next = NULL;
1576 wwin->prev = NULL;
1577 scr->focused_window = wwin;
1578 } else {
1579 WWindow *tmp;
1581 /* add window at beginning of focus window list */
1582 tmp = scr->focused_window;
1583 while (tmp->prev)
1584 tmp = tmp->prev;
1585 tmp->prev = wwin;
1586 wwin->next = tmp;
1587 wwin->prev = NULL;
1590 if (wwin->flags.is_gnustep == 0)
1591 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1593 /* if (wPreferences.auto_focus)*/
1594 wSetFocusTo(scr, wwin);
1596 wWindowResetMouseGrabs(wwin);
1598 wWindowSetKeyGrabs(wwin);
1600 return wwin;
1605 *----------------------------------------------------------------------
1606 * wUnmanageWindow--
1607 * Removes the frame window from a window and destroys all data
1608 * related to it. The window will be reparented back to the root window
1609 * if restore is True.
1611 * Side effects:
1612 * Everything related to the window is destroyed and the window
1613 * is removed from the window lists. Focus is set to the previous on the
1614 * window list.
1615 *----------------------------------------------------------------------
1617 void
1618 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1620 WCoreWindow *frame = wwin->frame->core;
1621 WWindow *owner = NULL;
1622 WWindow *newFocusedWindow = NULL;
1623 int wasFocused;
1624 WScreen *scr = wwin->screen_ptr;
1627 /* First close attribute editor window if open */
1628 if (wwin->flags.inspector_open) {
1629 wCloseInspectorForWindow(wwin);
1632 /* Close window menu if it's open for this window */
1633 if (wwin->flags.menu_open_for_me) {
1634 CloseWindowMenu(scr);
1637 if (!destroyed) {
1638 if (!wwin->flags.internal_window)
1639 XRemoveFromSaveSet(dpy, wwin->client_win);
1641 XSelectInput(dpy, wwin->client_win, NoEventMask);
1643 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1644 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1647 XUnmapWindow(dpy, frame->window);
1649 XUnmapWindow(dpy, wwin->client_win);
1651 /* deselect window */
1652 wSelectWindow(wwin, False);
1654 /* remove all pending events on window */
1655 /* I think this only matters for autoraise */
1656 if (wPreferences.raise_delay)
1657 WMDeleteTimerWithClientData(wwin->frame->core);
1659 XFlush(dpy);
1661 /* reparent the window back to the root */
1662 if (restore)
1663 wClientRestore(wwin);
1665 if (wwin->transient_for!=scr->root_win) {
1666 owner = wWindowFor(wwin->transient_for);
1667 if (owner) {
1668 if (!owner->flags.semi_focused) {
1669 owner = NULL;
1670 } else {
1671 owner->flags.semi_focused = 0;
1676 wasFocused = wwin->flags.focused;
1678 /* remove from window focus list */
1679 if (!wwin->prev && !wwin->next) {
1680 /* was the only window */
1681 scr->focused_window = NULL;
1682 newFocusedWindow = NULL;
1683 } else {
1684 WWindow *tmp;
1686 if (wwin->prev)
1687 wwin->prev->next = wwin->next;
1688 if (wwin->next)
1689 wwin->next->prev = wwin->prev;
1690 else {
1691 scr->focused_window = wwin->prev;
1692 scr->focused_window->next = NULL;
1695 if (wPreferences.focus_mode==WKF_CLICK) {
1697 /* if in click to focus mode and the window
1698 * was a transient, focus the owner window
1700 tmp = NULL;
1701 if (wPreferences.focus_mode==WKF_CLICK) {
1702 tmp = wWindowFor(wwin->transient_for);
1703 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1704 tmp = NULL;
1707 /* otherwise, focus the next one in the focus list */
1708 if (!tmp) {
1709 tmp = scr->focused_window;
1710 while (tmp) { /* look for one in the window list first */
1711 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1712 && (tmp->flags.mapped || tmp->flags.shaded))
1713 break;
1714 tmp = tmp->prev;
1716 if (!tmp) { /* if unsuccessful, choose any focusable window */
1717 tmp = scr->focused_window;
1718 while (tmp) {
1719 if (!WFLAGP(tmp, no_focusable)
1720 && (tmp->flags.mapped || tmp->flags.shaded))
1721 break;
1722 tmp = tmp->prev;
1727 newFocusedWindow = tmp;
1729 } else if (wPreferences.focus_mode==WKF_SLOPPY) {
1730 unsigned int mask;
1731 int foo;
1732 Window bar, win;
1734 /* This is to let the root window get the keyboard input
1735 * if Sloppy focus mode and no other window get focus.
1736 * This way keybindings will not freeze.
1738 tmp = NULL;
1739 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1740 &foo, &foo, &foo, &foo, &mask))
1741 tmp = wWindowFor(win);
1742 if (tmp == wwin)
1743 tmp = NULL;
1744 newFocusedWindow = tmp;
1745 } else {
1746 newFocusedWindow = NULL;
1750 if (!wwin->flags.internal_window) {
1751 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1754 #ifdef DEBUG
1755 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1756 (unsigned)frame->window);
1757 #endif
1759 if (wasFocused) {
1760 if (newFocusedWindow != owner && owner) {
1761 if (wwin->flags.is_gnustep == 0)
1762 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1764 wSetFocusTo(scr, newFocusedWindow);
1766 wWindowDestroy(wwin);
1767 XFlush(dpy);
1771 void
1772 wWindowMap(WWindow *wwin)
1774 XMapWindow(dpy, wwin->frame->core->window);
1775 if (!wwin->flags.shaded) {
1776 /* window will be remapped when getting MapNotify */
1777 XSelectInput(dpy, wwin->client_win,
1778 wwin->event_mask & ~StructureNotifyMask);
1779 XMapWindow(dpy, wwin->client_win);
1780 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1782 wwin->flags.mapped = 1;
1787 void
1788 wWindowUnmap(WWindow *wwin)
1790 wwin->flags.mapped = 0;
1792 /* prevent window withdrawal when getting UnmapNotify */
1793 XSelectInput(dpy, wwin->client_win,
1794 wwin->event_mask & ~StructureNotifyMask);
1795 XUnmapWindow(dpy, wwin->client_win);
1796 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1798 XUnmapWindow(dpy, wwin->frame->core->window);
1803 void
1804 wWindowFocus(WWindow *wwin, WWindow *owin)
1806 WWindow *nowner;
1807 WWindow *oowner;
1809 #ifdef KEEP_XKB_LOCK_STATUS
1810 if (wPreferences.modelock) {
1811 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1813 #endif /* KEEP_XKB_LOCK_STATUS */
1815 wwin->flags.semi_focused = 0;
1817 if (wwin->flags.is_gnustep == 0)
1818 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1820 wwin->flags.focused = 1;
1822 wWindowResetMouseGrabs(wwin);
1824 WMPostNotificationName(WMNChangedFocus, wwin, (void*)True);
1826 if (owin == wwin || !owin)
1827 return;
1829 nowner = wWindowFor(wwin->transient_for);
1831 /* new window is a transient for the old window */
1832 if (nowner == owin) {
1833 owin->flags.semi_focused = 1;
1834 wWindowUnfocus(nowner);
1835 return;
1838 oowner = wWindowFor(owin->transient_for);
1840 /* new window is owner of old window */
1841 if (wwin == oowner) {
1842 wWindowUnfocus(owin);
1843 return;
1846 if (!nowner) {
1847 wWindowUnfocus(owin);
1848 return;
1851 /* new window has same owner of old window */
1852 if (oowner == nowner) {
1853 /* prevent unfocusing of owner */
1854 oowner->flags.semi_focused = 0;
1855 wWindowUnfocus(owin);
1856 oowner->flags.semi_focused = 1;
1858 return;
1861 /* nowner != NULL && oowner != nowner */
1862 nowner->flags.semi_focused = 1;
1863 wWindowUnfocus(nowner);
1864 wWindowUnfocus(owin);
1868 void
1869 wWindowUnfocus(WWindow *wwin)
1871 CloseWindowMenu(wwin->screen_ptr);
1873 if (wwin->flags.is_gnustep == 0)
1874 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1875 ? WS_PFOCUSED : WS_UNFOCUSED);
1877 if (wwin->transient_for!=None
1878 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1879 WWindow *owner;
1880 owner = wWindowFor(wwin->transient_for);
1881 if (owner && owner->flags.semi_focused) {
1882 owner->flags.semi_focused = 0;
1883 if (owner->flags.mapped || owner->flags.shaded) {
1884 wWindowUnfocus(owner);
1885 wFrameWindowPaint(owner->frame);
1889 wwin->flags.focused = 0;
1891 wWindowResetMouseGrabs(wwin);
1893 WMPostNotificationName(WMNChangedFocus, wwin, (void*)False);
1897 void
1898 wWindowUpdateName(WWindow *wwin, char *newTitle)
1900 char *title;
1902 if (!wwin->frame)
1903 return;
1905 wwin->flags.wm_name_changed = 1;
1907 if (!newTitle) {
1908 /* the hint was removed */
1909 title = DEF_WINDOW_TITLE;
1910 } else {
1911 title = newTitle;
1914 if (wFrameWindowChangeTitle(wwin->frame, title)) {
1915 WMPostNotificationName(WMNChangedName, wwin, NULL);
1922 *----------------------------------------------------------------------
1924 * wWindowConstrainSize--
1925 * Constrains size for the client window, taking the maximal size,
1926 * window resize increments and other size hints into account.
1928 * Returns:
1929 * The closest size to what was given that the client window can
1930 * have.
1932 *----------------------------------------------------------------------
1934 void
1935 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1937 int width = *nwidth;
1938 int height = *nheight;
1939 int winc = 1;
1940 int hinc = 1;
1941 int minW = 1, minH = 1;
1942 int maxW = wwin->screen_ptr->scr_width*2;
1943 int maxH = wwin->screen_ptr->scr_height*2;
1944 int minAX = -1, minAY = -1;
1945 int maxAX = -1, maxAY = -1;
1946 int baseW = 0;
1947 int baseH = 0;
1949 if (wwin->normal_hints) {
1950 winc = wwin->normal_hints->width_inc;
1951 hinc = wwin->normal_hints->height_inc;
1952 minW = wwin->normal_hints->min_width;
1953 minH = wwin->normal_hints->min_height;
1954 maxW = wwin->normal_hints->max_width;
1955 maxH = wwin->normal_hints->max_height;
1956 if (wwin->normal_hints->flags & PAspect) {
1957 minAX = wwin->normal_hints->min_aspect.x;
1958 minAY = wwin->normal_hints->min_aspect.y;
1959 maxAX = wwin->normal_hints->max_aspect.x;
1960 maxAY = wwin->normal_hints->max_aspect.y;
1963 baseW = wwin->normal_hints->base_width;
1964 baseH = wwin->normal_hints->base_height;
1967 if (width < minW)
1968 width = minW;
1969 if (height < minH)
1970 height = minH;
1972 if (width > maxW)
1973 width = maxW;
1974 if (height > maxH)
1975 height = maxH;
1977 /* aspect ratio code borrowed from olwm */
1978 if (minAX > 0) {
1979 /* adjust max aspect ratio */
1980 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
1981 if (maxAX > maxAY) {
1982 height = (width * maxAY) / maxAX;
1983 if (height > maxH) {
1984 height = maxH;
1985 width = (height * maxAX) / maxAY;
1987 } else {
1988 width = (height * maxAX) / maxAY;
1989 if (width > maxW) {
1990 width = maxW;
1991 height = (width * maxAY) / maxAX;
1996 /* adjust min aspect ratio */
1997 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
1998 if (minAX > minAY) {
1999 height = (width * minAY) / minAX;
2000 if (height < minH) {
2001 height = minH;
2002 width = (height * minAX) / minAY;
2004 } else {
2005 width = (height * minAX) / minAY;
2006 if (width < minW) {
2007 width = minW;
2008 height = (width * minAY) / minAX;
2014 if (baseW != 0) {
2015 width = (((width - baseW) / winc) * winc) + baseW;
2016 } else {
2017 width = (((width - minW) / winc) * winc) + minW;
2020 if (baseH != 0) {
2021 height = (((height - baseH) / hinc) * hinc) + baseH;
2022 } else {
2023 height = (((height - minH) / hinc) * hinc) + minH;
2026 /* broken stupid apps may cause preposterous values for these.. */
2027 if (width > 0)
2028 *nwidth = width;
2029 if (height > 0)
2030 *nheight = height;
2034 void
2035 wWindowCropSize(WWindow *wwin, int maxW, int maxH,
2036 int *width, int *height)
2038 int baseW = 0, baseH = 0;
2039 int winc = 1, hinc = 1;
2041 if (wwin->normal_hints) {
2042 baseW = wwin->normal_hints->base_width;
2043 baseH = wwin->normal_hints->base_height;
2045 winc = wwin->normal_hints->width_inc;
2046 hinc = wwin->normal_hints->height_inc;
2049 if (*width > maxW)
2050 *width = maxW - (maxW - baseW) % winc;
2052 if (*height > maxH)
2053 *height = maxH - (maxH - baseH) % hinc;
2057 void
2058 wWindowChangeWorkspace(WWindow *wwin, int workspace)
2060 WScreen *scr = wwin->screen_ptr;
2061 WApplication *wapp;
2062 int unmap = 0;
2064 if (workspace >= scr->workspace_count || workspace < 0
2065 || workspace == wwin->frame->workspace)
2066 return;
2068 if (workspace != scr->current_workspace) {
2069 /* Sent to other workspace. Unmap window */
2070 if ((wwin->flags.mapped
2071 || wwin->flags.shaded
2072 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2073 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2075 wapp = wApplicationOf(wwin->main_window);
2076 if (wapp) {
2077 wapp->last_workspace = workspace;
2079 if (wwin->flags.miniaturized) {
2080 if (wwin->icon) {
2081 XUnmapWindow(dpy, wwin->icon->core->window);
2082 wwin->icon->mapped = 0;
2084 } else {
2085 unmap = 1;
2086 wSetFocusTo(scr, NULL);
2089 } else {
2090 /* brought to current workspace. Map window */
2091 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2092 if (wwin->icon) {
2093 XMapWindow(dpy, wwin->icon->core->window);
2094 wwin->icon->mapped = 1;
2096 } else if (!wwin->flags.mapped &&
2097 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2098 wWindowMap(wwin);
2101 if (!IS_OMNIPRESENT(wwin)) {
2102 int oldWorkspace = wwin->frame->workspace;
2104 wwin->frame->workspace = workspace;
2106 WMPostNotificationName(WMNChangedWorkspace, wwin, (void*)oldWorkspace);
2109 if (unmap) {
2110 wWindowUnmap(wwin);
2115 void
2116 wWindowSynthConfigureNotify(WWindow *wwin)
2118 XEvent sevent;
2120 sevent.type = ConfigureNotify;
2121 sevent.xconfigure.display = dpy;
2122 sevent.xconfigure.event = wwin->client_win;
2123 sevent.xconfigure.window = wwin->client_win;
2125 sevent.xconfigure.x = wwin->client.x;
2126 sevent.xconfigure.y = wwin->client.y;
2127 sevent.xconfigure.width = wwin->client.width;
2128 sevent.xconfigure.height = wwin->client.height;
2130 sevent.xconfigure.border_width = wwin->old_border_width;
2131 if (WFLAGP(wwin, no_titlebar))
2132 sevent.xconfigure.above = None;
2133 else
2134 sevent.xconfigure.above = wwin->frame->titlebar->window;
2136 sevent.xconfigure.override_redirect = False;
2137 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2138 #ifdef KWM_HINTS
2139 wKWMSendEventMessage(wwin, WKWMChangedClient);
2140 #endif
2141 XFlush(dpy);
2146 *----------------------------------------------------------------------
2147 * wWindowConfigure--
2148 * Configures the frame, decorations and client window to the
2149 * specified geometry. The geometry is not checked for validity,
2150 * wWindowConstrainSize() must be used for that.
2151 * The size parameters are for the client window, but the position is
2152 * for the frame.
2153 * The client window receives a ConfigureNotify event, according
2154 * to what ICCCM says.
2156 * Returns:
2157 * None
2159 * Side effects:
2160 * Window size and position are changed and client window receives
2161 * a ConfigureNotify event.
2162 *----------------------------------------------------------------------
2164 void
2165 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
2166 WWindow *wwin;
2167 int req_x, req_y; /* new position of the frame */
2168 int req_width, req_height; /* new size of the client */
2170 int synth_notify = False;
2171 int resize;
2173 resize = (req_width!=wwin->client.width
2174 || req_height!=wwin->client.height);
2176 * if the window is being moved but not resized then
2177 * send a synthetic ConfigureNotify
2179 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
2180 synth_notify = True;
2183 if (WFLAGP(wwin, dont_move_off))
2184 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2185 req_width, req_height);
2186 if (resize) {
2187 if (req_width < MIN_WINDOW_SIZE)
2188 req_width = MIN_WINDOW_SIZE;
2189 if (req_height < MIN_WINDOW_SIZE)
2190 req_height = MIN_WINDOW_SIZE;
2192 /* If growing, resize inner part before frame,
2193 * if shrinking, resize frame before.
2194 * This will prevent the frame (that can have a different color)
2195 * to be exposed, causing flicker */
2196 if (req_height > wwin->frame->core->height
2197 || req_width > wwin->frame->core->width)
2198 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2200 if (wwin->flags.shaded) {
2201 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2202 req_width, wwin->frame->core->height);
2203 wwin->old_geometry.height = req_height;
2204 } else {
2205 int h;
2207 h = req_height + wwin->frame->top_width
2208 + wwin->frame->bottom_width;
2210 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2213 if (!(req_height > wwin->frame->core->height
2214 || req_width > wwin->frame->core->width))
2215 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2217 wwin->client.x = req_x;
2218 wwin->client.y = req_y + wwin->frame->top_width;
2219 wwin->client.width = req_width;
2220 wwin->client.height = req_height;
2221 } else {
2222 wwin->client.x = req_x;
2223 wwin->client.y = req_y + wwin->frame->top_width;
2225 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2227 wwin->frame_x = req_x;
2228 wwin->frame_y = req_y;
2229 if (!WFLAGP(wwin, no_border)) {
2230 wwin->client.x += FRAME_BORDER_WIDTH;
2231 wwin->client.y += FRAME_BORDER_WIDTH;
2234 #ifdef SHAPE
2235 if (wShapeSupported && wwin->flags.shaped && resize) {
2236 wWindowSetShape(wwin);
2238 #endif
2240 if (synth_notify)
2241 wWindowSynthConfigureNotify(wwin);
2242 XFlush(dpy);
2246 void
2247 wWindowMove(wwin, req_x, req_y)
2248 WWindow *wwin;
2249 int req_x, req_y; /* new position of the frame */
2251 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2252 int synth_notify = False;
2254 /* Send a synthetic ConfigureNotify event for every window movement. */
2255 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2256 synth_notify = True;
2258 #else
2259 /* A single synthetic ConfigureNotify event is sent at the end of
2260 * a completed (opaque) movement in moveres.c */
2261 #endif
2263 if (WFLAGP(wwin, dont_move_off))
2264 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2265 wwin->frame->core->width, wwin->frame->core->height);
2267 wwin->client.x = req_x;
2268 wwin->client.y = req_y + wwin->frame->top_width;
2269 if (!WFLAGP(wwin, no_border)) {
2270 wwin->client.x += FRAME_BORDER_WIDTH;
2271 wwin->client.y += FRAME_BORDER_WIDTH;
2274 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2276 wwin->frame_x = req_x;
2277 wwin->frame_y = req_y;
2279 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2280 if (synth_notify)
2281 wWindowSynthConfigureNotify(wwin);
2282 #endif
2286 void
2287 wWindowUpdateButtonImages(WWindow *wwin)
2289 WScreen *scr = wwin->screen_ptr;
2290 Pixmap pixmap, mask;
2291 WFrameWindow *fwin = wwin->frame;
2293 if (WFLAGP(wwin, no_titlebar))
2294 return;
2296 /* miniaturize button */
2298 if (!WFLAGP(wwin, no_miniaturize_button)) {
2299 if (wwin->wm_gnustep_attr
2300 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2301 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2303 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2304 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2305 } else {
2306 mask = None;
2309 if (fwin->lbutton_image
2310 && (fwin->lbutton_image->image != pixmap
2311 || fwin->lbutton_image->mask != mask)) {
2312 wPixmapDestroy(fwin->lbutton_image);
2313 fwin->lbutton_image = NULL;
2316 if (!fwin->lbutton_image) {
2317 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2318 fwin->lbutton_image->client_owned = 1;
2319 fwin->lbutton_image->client_owned_mask = 1;
2321 } else {
2322 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2323 wPixmapDestroy(fwin->lbutton_image);
2325 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2329 #ifdef XKB_BUTTON_HINT
2330 if (!WFLAGP(wwin, no_language_button)) {
2331 if (fwin->languagebutton_image &&
2332 !fwin->languagebutton_image->shared) {
2333 wPixmapDestroy(fwin->languagebutton_image);
2335 fwin->languagebutton_image =
2336 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2338 #endif
2340 /* close button */
2342 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2343 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2345 if (!WFLAGP(wwin, no_close_button)) {
2346 if (wwin->wm_gnustep_attr
2347 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2348 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2350 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2351 mask = wwin->wm_gnustep_attr->close_mask;
2352 else
2353 mask = None;
2355 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2356 || fwin->rbutton_image->mask != mask)) {
2357 wPixmapDestroy(fwin->rbutton_image);
2358 fwin->rbutton_image = NULL;
2361 if (!fwin->rbutton_image) {
2362 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2363 fwin->rbutton_image->client_owned = 1;
2364 fwin->rbutton_image->client_owned_mask = 1;
2367 } else if (WFLAGP(wwin, kill_close)) {
2369 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2370 wPixmapDestroy(fwin->rbutton_image);
2372 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2374 } else if (MGFLAGP(wwin, broken_close)) {
2376 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2377 wPixmapDestroy(fwin->rbutton_image);
2379 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2381 } else {
2383 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2384 wPixmapDestroy(fwin->rbutton_image);
2386 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2390 /* force buttons to be redrawn */
2391 fwin->flags.need_texture_change = 1;
2392 wFrameWindowPaint(fwin);
2397 *---------------------------------------------------------------------------
2398 * wWindowConfigureBorders--
2399 * Update window border configuration according to attribute flags.
2401 *---------------------------------------------------------------------------
2403 void
2404 wWindowConfigureBorders(WWindow *wwin)
2406 if (wwin->frame) {
2407 int flags;
2408 int newy, oldh;
2410 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2411 #ifdef XKB_BUTTON_HINT
2412 flags |= WFF_LANGUAGE_BUTTON;
2413 #endif
2414 if (!WFLAGP(wwin, no_titlebar))
2415 flags |= WFF_TITLEBAR;
2416 if (!WFLAGP(wwin, no_resizebar))
2417 flags |= WFF_RESIZEBAR;
2418 if (!WFLAGP(wwin, no_border))
2419 flags |= WFF_BORDER;
2420 if (wwin->flags.shaded)
2421 flags |= WFF_IS_SHADED;
2423 oldh = wwin->frame->top_width;
2424 wFrameWindowUpdateBorders(wwin->frame, flags);
2425 if (oldh != wwin->frame->top_width) {
2426 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2428 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2429 wWindowConfigure(wwin, wwin->frame_x, newy,
2430 wwin->client.width, wwin->client.height);
2433 flags = 0;
2434 if (!WFLAGP(wwin, no_miniaturize_button)
2435 && wwin->frame->flags.hide_left_button)
2436 flags |= WFF_LEFT_BUTTON;
2438 #ifdef XKB_BUTTON_HINT
2439 if (!WFLAGP(wwin, no_language_button)
2440 && wwin->frame->flags.hide_language_button)
2441 flags |= WFF_LANGUAGE_BUTTON;
2442 #endif
2444 if (!WFLAGP(wwin, no_close_button)
2445 && wwin->frame->flags.hide_right_button)
2446 flags |= WFF_RIGHT_BUTTON;
2448 if (flags!=0) {
2449 wWindowUpdateButtonImages(wwin);
2450 wFrameWindowShowButton(wwin->frame, flags);
2453 flags = 0;
2454 if (WFLAGP(wwin, no_miniaturize_button)
2455 && !wwin->frame->flags.hide_left_button)
2456 flags |= WFF_LEFT_BUTTON;
2458 #ifdef XKB_BUTTON_HINT
2459 if (WFLAGP(wwin, no_language_button)
2460 && !wwin->frame->flags.hide_language_button)
2461 flags |= WFF_LANGUAGE_BUTTON;
2462 #endif
2464 if (WFLAGP(wwin, no_close_button)
2465 && !wwin->frame->flags.hide_right_button)
2466 flags |= WFF_RIGHT_BUTTON;
2468 if (flags!=0)
2469 wFrameWindowHideButton(wwin->frame, flags);
2471 #ifdef SHAPE
2472 if (wShapeSupported && wwin->flags.shaped) {
2473 wWindowSetShape(wwin);
2475 #endif
2480 void
2481 wWindowSaveState(WWindow *wwin)
2483 CARD32 data[10];
2484 int i;
2486 memset(data, 0, sizeof(CARD32)*10);
2487 data[0] = wwin->frame->workspace;
2488 data[1] = wwin->flags.miniaturized;
2489 data[2] = wwin->flags.shaded;
2490 data[3] = wwin->flags.hidden;
2491 data[4] = wwin->flags.maximized;
2492 if (wwin->flags.maximized == 0) {
2493 data[5] = wwin->frame_x;
2494 data[6] = wwin->frame_y;
2495 data[7] = wwin->frame->core->width;
2496 data[8] = wwin->frame->core->height;
2497 } else {
2498 data[5] = wwin->old_geometry.x;
2499 data[6] = wwin->old_geometry.y;
2500 data[7] = wwin->old_geometry.width;
2501 data[8] = wwin->old_geometry.height;
2504 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2505 if (wwin->screen_ptr->shortcutWindows[i] &&
2506 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2507 data[9] |= 1<<i;
2509 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2510 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2511 (unsigned char *)data, 10);
2515 static int
2516 getSavedState(Window window, WSavedState **state)
2518 Atom type_ret;
2519 int fmt_ret;
2520 unsigned long nitems_ret;
2521 unsigned long bytes_after_ret;
2522 CARD32 *data;
2524 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2525 True, _XA_WINDOWMAKER_STATE,
2526 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2527 (unsigned char **)&data)!=Success || !data)
2528 return 0;
2530 *state = wmalloc(sizeof(WSavedState));
2532 (*state)->workspace = data[0];
2533 (*state)->miniaturized = data[1];
2534 (*state)->shaded = data[2];
2535 (*state)->hidden = data[3];
2536 (*state)->maximized = data[4];
2537 (*state)->x = data[5];
2538 (*state)->y = data[6];
2539 (*state)->w = data[7];
2540 (*state)->h = data[8];
2541 (*state)->window_shortcuts = data[9];
2543 XFree(data);
2545 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2546 return 1;
2547 else
2548 return 0;
2552 #ifdef SHAPE
2553 void
2554 wWindowClearShape(WWindow *wwin)
2556 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2557 0, wwin->frame->top_width, None, ShapeSet);
2558 XFlush(dpy);
2561 void
2562 wWindowSetShape(WWindow *wwin)
2564 XRectangle rect[2];
2565 int count;
2566 #ifdef OPTIMIZE_SHAPE
2567 XRectangle *rects;
2568 XRectangle *urec;
2569 int ordering;
2571 /* only shape is the client's */
2572 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2573 goto alt_code;
2576 /* Get array of rectangles describing the shape mask */
2577 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2578 &count, &ordering);
2579 if (!rects) {
2580 goto alt_code;
2583 urec = malloc(sizeof(XRectangle)*(count+2));
2584 if (!urec) {
2585 XFree(rects);
2586 goto alt_code;
2589 /* insert our decoration rectangles in the rect list */
2590 memcpy(urec, rects, sizeof(XRectangle)*count);
2591 XFree(rects);
2593 if (!WFLAGP(wwin, no_titlebar)) {
2594 urec[count].x = -1;
2595 urec[count].y = -1 - wwin->frame->top_width;
2596 urec[count].width = wwin->frame->core->width + 2;
2597 urec[count].height = wwin->frame->top_width + 1;
2598 count++;
2600 if (!WFLAGP(wwin, no_resizebar)) {
2601 urec[count].x = -1;
2602 urec[count].y = wwin->frame->core->height
2603 - wwin->frame->bottom_width - wwin->frame->top_width;
2604 urec[count].width = wwin->frame->core->width + 2;
2605 urec[count].height = wwin->frame->bottom_width + 1;
2606 count++;
2609 /* shape our frame window */
2610 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2611 0, wwin->frame->top_width, urec, count,
2612 ShapeSet, Unsorted);
2613 XFlush(dpy);
2614 wfree(urec);
2615 return;
2617 alt_code:
2618 #endif /* OPTIMIZE_SHAPE */
2619 count = 0;
2620 if (!WFLAGP(wwin, no_titlebar)) {
2621 rect[count].x = -1;
2622 rect[count].y = -1;
2623 rect[count].width = wwin->frame->core->width + 2;
2624 rect[count].height = wwin->frame->top_width + 1;
2625 count++;
2627 if (!WFLAGP(wwin, no_resizebar)) {
2628 rect[count].x = -1;
2629 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2630 rect[count].width = wwin->frame->core->width + 2;
2631 rect[count].height = wwin->frame->bottom_width + 1;
2632 count++;
2634 if (count > 0) {
2635 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2636 0, 0, rect, count, ShapeSet, Unsorted);
2638 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2639 0, wwin->frame->top_width, wwin->client_win,
2640 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2641 XFlush(dpy);
2643 #endif /* SHAPE */
2645 /* ====================================================================== */
2647 static FocusMode
2648 getFocusMode(WWindow *wwin)
2650 FocusMode mode;
2652 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2653 if (wwin->wm_hints->input == True) {
2654 if (wwin->protocols.TAKE_FOCUS)
2655 mode = WFM_LOCALLY_ACTIVE;
2656 else
2657 mode = WFM_PASSIVE;
2658 } else {
2659 if (wwin->protocols.TAKE_FOCUS)
2660 mode = WFM_GLOBALLY_ACTIVE;
2661 else
2662 mode = WFM_NO_INPUT;
2664 } else {
2665 mode = WFM_PASSIVE;
2667 return mode;
2671 void
2672 wWindowSetKeyGrabs(WWindow *wwin)
2674 int i;
2675 WShortKey *key;
2677 for (i=0; i<WKBD_LAST; i++) {
2678 key = &wKeyBindings[i];
2680 if (key->keycode==0)
2681 continue;
2682 if (key->modifier!=AnyModifier) {
2683 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2684 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2685 #ifdef NUMLOCK_HACK
2686 /* Also grab all modifier combinations possible that include,
2687 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2688 * work even if the NumLock/ScrollLock key is on.
2690 wHackedGrabKey(key->keycode, key->modifier,
2691 wwin->frame->core->window, True, GrabModeAsync,
2692 GrabModeAsync);
2693 #endif
2695 XGrabKey(dpy, key->keycode, key->modifier,
2696 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2699 #ifndef LITE
2700 wRootMenuBindShortcuts(wwin->frame->core->window);
2701 #endif
2706 void
2707 wWindowResetMouseGrabs(WWindow *wwin)
2709 /* Mouse grabs can't be done on the client window because of
2710 * ICCCM and because clients that try to do the same will crash.
2712 * But there is a problem wich makes tbar buttons of unfocused
2713 * windows not usable as the click goes to the frame window instead
2714 * of the button itself. Must figure a way to fix that.
2717 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2719 if (!WFLAGP(wwin, no_bind_mouse)) {
2720 /* grabs for Meta+drag */
2721 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2722 True, ButtonPressMask, GrabModeSync,
2723 GrabModeAsync, None, None);
2726 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2727 && !wwin->flags.is_gnustep) {
2728 /* the passive grabs to focus the window */
2729 /* if (wPreferences.focus_mode == WKF_CLICK) */
2730 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2731 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2732 None, None);
2734 XFlush(dpy);
2738 void
2739 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2741 if (attr->flags & GSExtraFlagsAttr) {
2742 if (MGFLAGP(wwin, broken_close) !=
2743 (attr->extra_flags & GSDocumentEditedFlag)) {
2744 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2745 wWindowUpdateButtonImages(wwin);
2751 WMagicNumber
2752 wWindowAddSavedState(char *instance, char *class, char *command,
2753 pid_t pid, WSavedState *state)
2755 WWindowState *wstate;
2757 wstate = malloc(sizeof(WWindowState));
2758 if (!wstate)
2759 return 0;
2761 memset(wstate, 0, sizeof(WWindowState));
2762 wstate->pid = pid;
2763 if (instance)
2764 wstate->instance = wstrdup(instance);
2765 if (class)
2766 wstate->class = wstrdup(class);
2767 if (command)
2768 wstate->command = wstrdup(command);
2769 wstate->state = state;
2771 wstate->next = windowState;
2772 windowState = wstate;
2774 #ifdef DEBUG
2775 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2776 class, command);
2777 #endif
2779 return wstate;
2783 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2786 WMagicNumber
2787 wWindowGetSavedState(Window win)
2789 char *instance, *class, *command=NULL;
2790 WWindowState *wstate = windowState;
2791 char **argv;
2792 int argc;
2794 if (!wstate)
2795 return NULL;
2797 if (XGetCommand(dpy, win, &argv, &argc)) {
2798 if (argc > 0)
2799 command = wtokenjoin(argv, argc);
2800 XFreeStringList(argv);
2802 if (!command)
2803 return NULL;
2805 if (PropGetWMClass(win, &class, &instance)) {
2806 while (wstate) {
2807 if (SAME(instance, wstate->instance) &&
2808 SAME(class, wstate->class) &&
2809 SAME(command, wstate->command)) {
2810 break;
2812 wstate = wstate->next;
2814 } else {
2815 wstate = NULL;
2818 #ifdef DEBUG
2819 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2820 class, command);
2821 #endif
2823 if (command) wfree(command);
2824 if (instance) XFree(instance);
2825 if (class) XFree(class);
2827 return wstate;
2831 void
2832 wWindowDeleteSavedState(WMagicNumber id)
2834 WWindowState *tmp, *wstate=(WWindowState*)id;
2836 if (!wstate || !windowState)
2837 return;
2839 tmp = windowState;
2840 if (tmp==wstate) {
2841 windowState = wstate->next;
2842 #ifdef DEBUG
2843 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2844 wstate, wstate->instance, wstate->class, wstate->command);
2845 #endif
2846 if (wstate->instance) wfree(wstate->instance);
2847 if (wstate->class) wfree(wstate->class);
2848 if (wstate->command) wfree(wstate->command);
2849 wfree(wstate->state);
2850 wfree(wstate);
2851 } else {
2852 while (tmp->next) {
2853 if (tmp->next==wstate) {
2854 tmp->next=wstate->next;
2855 #ifdef DEBUG
2856 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2857 wstate, wstate->instance, wstate->class, wstate->command);
2858 #endif
2859 if (wstate->instance) wfree(wstate->instance);
2860 if (wstate->class) wfree(wstate->class);
2861 if (wstate->command) wfree(wstate->command);
2862 wfree(wstate->state);
2863 wfree(wstate);
2864 break;
2866 tmp = tmp->next;
2872 void
2873 wWindowDeleteSavedStatesForPID(pid_t pid)
2875 WWindowState *tmp, *wstate;
2877 if (!windowState)
2878 return;
2880 tmp = windowState;
2881 if (tmp->pid == pid) {
2882 wstate = windowState;
2883 windowState = tmp->next;
2884 #ifdef DEBUG
2885 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2886 wstate, wstate->instance, wstate->class, wstate->command);
2887 #endif
2888 if (wstate->instance) wfree(wstate->instance);
2889 if (wstate->class) wfree(wstate->class);
2890 if (wstate->command) wfree(wstate->command);
2891 wfree(wstate->state);
2892 wfree(wstate);
2893 } else {
2894 while (tmp->next) {
2895 if (tmp->next->pid==pid) {
2896 wstate = tmp->next;
2897 tmp->next = wstate->next;
2898 #ifdef DEBUG
2899 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2900 wstate, wstate->instance, wstate->class, wstate->command);
2901 #endif
2902 if (wstate->instance) wfree(wstate->instance);
2903 if (wstate->class) wfree(wstate->class);
2904 if (wstate->command) wfree(wstate->command);
2905 wfree(wstate->state);
2906 wfree(wstate);
2907 break;
2909 tmp = tmp->next;
2915 void
2916 wWindowSetOmnipresent(WWindow *wwin, Bool flag)
2918 wwin->flags.omnipresent = flag;
2920 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
2924 /* ====================================================================== */
2926 static void
2927 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2929 WWindow *wwin = data;
2931 #ifndef NUMLOCK_HACK
2932 if ((event->xbutton.state & ValidModMask)
2933 != (event->xbutton.state & ~LockMask)) {
2934 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2935 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2937 #endif
2939 event->xbutton.state &= ValidModMask;
2941 CloseWindowMenu(wwin->screen_ptr);
2943 if (wPreferences.focus_mode==WKF_CLICK
2944 && !(event->xbutton.state&ControlMask)
2945 && !WFLAGP(wwin, no_focusable)) {
2946 wSetFocusTo(wwin->screen_ptr, wwin);
2949 if (event->xbutton.button == Button1)
2950 wRaiseFrame(wwin->frame->core);
2952 if (event->xbutton.window != wwin->frame->resizebar->window) {
2953 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2954 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2955 GrabModeAsync, GrabModeAsync, None,
2956 None, CurrentTime)!=GrabSuccess) {
2957 #ifdef DEBUG0
2958 wwarning("pointer grab failed for window move");
2959 #endif
2960 return;
2964 if (event->xbutton.state & MOD_MASK) {
2965 /* move the window */
2966 wMouseMoveWindow(wwin, event);
2967 XUngrabPointer(dpy, CurrentTime);
2968 } else {
2969 wMouseResizeWindow(wwin, event);
2970 XUngrabPointer(dpy, CurrentTime);
2976 static void
2977 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2979 WWindow *wwin = data;
2981 event->xbutton.state &= ValidModMask;
2983 if (event->xbutton.button==Button1) {
2984 if (event->xbutton.state == 0) {
2985 if (!WFLAGP(wwin, no_shadeable)) {
2986 /* shade window */
2987 if (wwin->flags.shaded)
2988 wUnshadeWindow(wwin);
2989 else
2990 wShadeWindow(wwin);
2992 } else {
2993 int dir = 0;
2995 if (event->xbutton.state & ControlMask)
2996 dir |= MAX_VERTICAL;
2998 if (event->xbutton.state & ShiftMask) {
2999 dir |= MAX_HORIZONTAL;
3000 if (!(event->xbutton.state & ControlMask))
3001 wSelectWindow(wwin, !wwin->flags.selected);
3004 /* maximize window */
3005 if (dir!=0 && !WFLAGP(wwin, no_resizable)) {
3006 int ndir = dir ^ wwin->flags.maximized;
3007 if (wwin->flags.maximized != 0)
3008 wUnmaximizeWindow(wwin);
3009 if (ndir != 0)
3010 wMaximizeWindow(wwin, ndir);
3013 } else if (event->xbutton.button==Button3) {
3014 if (event->xbutton.state & MOD_MASK) {
3015 wHideOtherApplications(wwin);
3017 } else if (event->xbutton.button==Button2) {
3018 wSelectWindow(wwin, !wwin->flags.selected);
3019 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
3020 wShadeWindow(wwin);
3021 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
3022 wUnshadeWindow(wwin);
3027 static void
3028 frameMouseDown(WObjDescriptor *desc, XEvent *event)
3030 WWindow *wwin = desc->parent;
3032 event->xbutton.state &= ValidModMask;
3034 CloseWindowMenu(wwin->screen_ptr);
3036 if (/*wPreferences.focus_mode==WKF_CLICK
3037 &&*/ !(event->xbutton.state&ControlMask)
3038 && !WFLAGP(wwin, no_focusable)) {
3039 wSetFocusTo(wwin->screen_ptr, wwin);
3041 if (event->xbutton.button == Button1) {
3042 wRaiseFrame(wwin->frame->core);
3045 if (event->xbutton.state & MOD_MASK) {
3046 /* move the window */
3047 if (XGrabPointer(dpy, wwin->client_win, False,
3048 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3049 GrabModeAsync, GrabModeAsync, None,
3050 None, CurrentTime)!=GrabSuccess) {
3051 #ifdef DEBUG0
3052 wwarning("pointer grab failed for window move");
3053 #endif
3054 return;
3056 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
3057 wMouseResizeWindow(wwin, event);
3058 else
3059 wMouseMoveWindow(wwin, event);
3060 XUngrabPointer(dpy, CurrentTime);
3065 static void
3066 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3068 WWindow *wwin = (WWindow*)data;
3070 #ifndef NUMLOCK_HACK
3071 if ((event->xbutton.state & ValidModMask)
3072 != (event->xbutton.state & ~LockMask)) {
3073 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3074 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3076 #endif
3077 event->xbutton.state &= ValidModMask;
3079 CloseWindowMenu(wwin->screen_ptr);
3081 if (wPreferences.focus_mode==WKF_CLICK
3082 && !(event->xbutton.state&ControlMask)
3083 && !WFLAGP(wwin, no_focusable)) {
3084 wSetFocusTo(wwin->screen_ptr, wwin);
3087 if (event->xbutton.button == Button1
3088 || event->xbutton.button == Button2) {
3090 if (event->xbutton.button == Button1) {
3091 if (event->xbutton.state & MOD_MASK) {
3092 wLowerFrame(wwin->frame->core);
3093 } else {
3094 wRaiseFrame(wwin->frame->core);
3097 if ((event->xbutton.state & ShiftMask)
3098 && !(event->xbutton.state & ControlMask)) {
3099 wSelectWindow(wwin, !wwin->flags.selected);
3100 return;
3102 if (event->xbutton.window != wwin->frame->titlebar->window
3103 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3104 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3105 GrabModeAsync, GrabModeAsync, None,
3106 None, CurrentTime)!=GrabSuccess) {
3107 #ifdef DEBUG0
3108 wwarning("pointer grab failed for window move");
3109 #endif
3110 return;
3113 /* move the window */
3114 wMouseMoveWindow(wwin, event);
3116 XUngrabPointer(dpy, CurrentTime);
3117 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
3118 && !wwin->flags.internal_window
3119 && !WCHECK_STATE(WSTATE_MODAL)) {
3120 WObjDescriptor *desc;
3122 if (event->xbutton.window != wwin->frame->titlebar->window
3123 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3124 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3125 GrabModeAsync, GrabModeAsync, None,
3126 None, CurrentTime)!=GrabSuccess) {
3127 #ifdef DEBUG0
3128 wwarning("pointer grab failed for window move");
3129 #endif
3130 return;
3133 OpenWindowMenu(wwin, event->xbutton.x_root,
3134 wwin->frame_y+wwin->frame->top_width, False);
3136 /* allow drag select */
3137 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3138 event->xany.send_event = True;
3139 (*desc->handle_mousedown)(desc, event);
3141 XUngrabPointer(dpy, CurrentTime);
3147 static void
3148 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3150 WWindow *wwin = data;
3152 event->xbutton.state &= ValidModMask;
3154 CloseWindowMenu(wwin->screen_ptr);
3156 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3157 return;
3159 /* if control-click, kill the client */
3160 if (event->xbutton.state & ControlMask) {
3161 wClientKill(wwin);
3162 } else {
3163 #ifdef OLWM_HINTS
3164 if (wwin->flags.olwm_push_pin_out) {
3166 wwin->flags.olwm_push_pin_out = 0;
3168 wOLWMChangePushpinState(wwin, True);
3170 wFrameWindowUpdatePushButton(wwin->frame, False);
3172 return;
3174 #endif
3175 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
3176 /* send delete message */
3177 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3183 static void
3184 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3186 WWindow *wwin = data;
3188 CloseWindowMenu(wwin->screen_ptr);
3190 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3191 return;
3193 /* send delete message */
3194 if (wwin->protocols.DELETE_WINDOW) {
3195 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3196 } else {
3197 wClientKill(wwin);
3202 #ifdef XKB_BUTTON_HINT
3203 static void
3204 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3206 WWindow *wwin = data;
3207 WFrameWindow *fwin = wwin->frame;
3208 WScreen *scr = fwin->screen_ptr;
3209 XkbStateRec staterec;
3210 int tl;
3212 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3213 return;
3214 tl = wwin->frame->languagemode;
3215 wwin->frame->languagemode = wwin->frame->last_languagemode;
3216 wwin->frame->last_languagemode = tl;
3217 wSetFocusTo(scr, wwin);
3218 wwin->frame->languagebutton_image =
3219 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3220 wwin->frame->languagemode];
3221 wFrameWindowUpdateLanguageButton(wwin->frame);
3222 if (event->xbutton.button == Button3)
3223 return;
3224 wRaiseFrame(fwin->core);
3226 #endif
3229 static void
3230 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3232 WWindow *wwin = data;
3234 event->xbutton.state &= ValidModMask;
3236 CloseWindowMenu(wwin->screen_ptr);
3238 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3239 return;
3241 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3242 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3243 LastTimestamp);
3244 } else {
3245 WApplication *wapp;
3246 if ((event->xbutton.state & ControlMask) ||
3247 (event->xbutton.button == Button3)) {
3249 wapp = wApplicationOf(wwin->main_window);
3250 if (wapp && !WFLAGP(wwin, no_appicon))
3251 wHideApplication(wapp);
3252 } else if (event->xbutton.state==0) {
3253 wIconifyWindow(wwin);