- Properly fixed the color change in the Appearance panel preview box
[wmaker-crm.git] / src / window.c
blob58c3715b83d9fa0ff9bcee14d7425045d8503d7e
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"
56 #include "xinerama.h"
59 #ifdef MWM_HINTS
60 # include "motif.h"
61 #endif
62 #ifdef KWM_HINTS
63 # include "kwm.h"
64 #endif
65 #ifdef GNOME_STUFF
66 # include "gnome.h"
67 #endif
68 #ifdef OLWM_HINTS
69 # include "openlook.h"
70 #endif
72 /****** Global Variables ******/
74 extern WShortKey wKeyBindings[WKBD_LAST];
76 #ifdef SHAPE
77 extern Bool wShapeSupported;
78 #endif
80 /* contexts */
81 extern XContext wWinContext;
83 /* cursors */
84 extern Cursor wCursor[WCUR_LAST];
86 /* protocol atoms */
87 extern Atom _XA_WM_DELETE_WINDOW;
88 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
90 extern Atom _XA_WINDOWMAKER_STATE;
92 extern WPreferences wPreferences;
94 #define MOD_MASK wPreferences.modifier_mask
96 extern Time LastTimestamp;
98 /* superfluous... */
99 extern void DoWindowBirth(WWindow*);
103 /***** Local Stuff *****/
106 static WWindowState *windowState=NULL;
110 /* local functions */
111 static FocusMode getFocusMode(WWindow *wwin);
113 static int getSavedState(Window window, WSavedState **state);
115 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
117 /* event handlers */
120 /* frame window (during window grabs) */
121 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
123 /* close button */
124 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
125 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
127 /* iconify button */
128 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
130 #ifdef XKB_BUTTON_HINT
131 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
132 #endif
134 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
135 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
137 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
140 /****** Notification Observers ******/
142 static void
143 appearanceObserver(void *self, WMNotification *notif)
145 WWindow *wwin = (WWindow*)self;
146 int flags = (int)WMGetNotificationClientData(notif);
148 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
149 return;
151 if (flags & WFontSettings) {
152 wWindowConfigureBorders(wwin);
153 if(wwin->flags.shaded) {
154 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
155 wwin->frame->top_width - 1);
157 wwin->client.y = wwin->frame_y - wwin->client.height
158 + wwin->frame->top_width;
159 wWindowSynthConfigureNotify(wwin);
162 if (flags & WTextureSettings) {
163 wwin->frame->flags.need_texture_remake = 1;
165 if (flags & (WTextureSettings | WColorSettings)) {
166 if (wwin->frame->titlebar)
167 XClearWindow(dpy, wwin->frame->titlebar->window);
169 wFrameWindowPaint(wwin->frame);
173 /************************************/
175 WWindow*
176 wWindowFor(Window window)
178 WObjDescriptor *desc;
180 if (window==None)
181 return NULL;
183 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
184 return NULL;
186 if (desc->parent_type==WCLASS_WINDOW)
187 return desc->parent;
188 else if (desc->parent_type==WCLASS_FRAME) {
189 WFrameWindow *frame = (WFrameWindow*)desc->parent;
190 if (frame->flags.is_client_window_frame)
191 return frame->child;
194 return NULL;
198 WWindow*
199 wWindowCreate()
201 WWindow *wwin;
203 wwin = wmalloc(sizeof(WWindow));
204 wretain(wwin);
206 memset(wwin, 0, sizeof(WWindow));
208 wwin->client_descriptor.handle_mousedown = frameMouseDown;
209 wwin->client_descriptor.parent = wwin;
210 wwin->client_descriptor.self = wwin;
211 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
213 return wwin;
217 void
218 wWindowDestroy(WWindow *wwin)
220 int i;
222 if (wwin->screen_ptr->cmap_window == wwin) {
223 wwin->screen_ptr->cmap_window = NULL;
226 WMRemoveNotificationObserver(wwin);
228 wwin->flags.destroyed = 1;
230 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
231 if (!wwin->screen_ptr->shortcutWindows[i])
232 continue;
234 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
236 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
237 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
238 wwin->screen_ptr->shortcutWindows[i] = NULL;
242 if (wwin->fake_group && wwin->fake_group->retainCount>0) {
243 wwin->fake_group->retainCount--;
244 if (wwin->fake_group->retainCount==0 && wwin->fake_group->leader!=None) {
245 XDestroyWindow(dpy, wwin->fake_group->leader);
246 wwin->fake_group->leader = None;
247 wwin->fake_group->origLeader = None;
248 XFlush(dpy);
252 if (wwin->normal_hints)
253 XFree(wwin->normal_hints);
255 if (wwin->wm_hints)
256 XFree(wwin->wm_hints);
258 if (wwin->wm_instance)
259 XFree(wwin->wm_instance);
261 if (wwin->wm_class)
262 XFree(wwin->wm_class);
264 if (wwin->wm_gnustep_attr)
265 wfree(wwin->wm_gnustep_attr);
267 if (wwin->cmap_windows)
268 XFree(wwin->cmap_windows);
270 XDeleteContext(dpy, wwin->client_win, wWinContext);
272 if (wwin->frame)
273 wFrameWindowDestroy(wwin->frame);
275 if (wwin->icon) {
276 RemoveFromStackList(wwin->icon->core);
277 wIconDestroy(wwin->icon);
278 if (wPreferences.auto_arrange_icons)
279 wArrangeIcons(wwin->screen_ptr, True);
281 wrelease(wwin);
287 static void
288 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
290 if (gs_hints->flags & GSWindowStyleAttr) {
291 if (gs_hints->window_style == WMBorderlessWindowMask) {
292 wwin->client_flags.no_border = 1;
293 wwin->client_flags.no_titlebar = 1;
294 wwin->client_flags.no_closable = 1;
295 wwin->client_flags.no_miniaturizable = 1;
296 wwin->client_flags.no_resizable = 1;
297 wwin->client_flags.no_close_button = 1;
298 wwin->client_flags.no_miniaturize_button = 1;
299 wwin->client_flags.no_resizebar = 1;
300 } else {
301 wwin->client_flags.no_close_button =
302 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
304 wwin->client_flags.no_closable =
305 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
307 wwin->client_flags.no_miniaturize_button =
308 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
310 wwin->client_flags.no_miniaturizable =
311 wwin->client_flags.no_miniaturize_button;
313 wwin->client_flags.no_resizebar =
314 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
316 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
318 /* these attributes supposedly imply in the existence
319 * of a titlebar */
320 if (gs_hints->window_style & (WMResizableWindowMask|
321 WMClosableWindowMask|
322 WMMiniaturizableWindowMask)) {
323 wwin->client_flags.no_titlebar = 0;
324 } else {
325 wwin->client_flags.no_titlebar =
326 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
330 } else {
331 /* setup the defaults */
332 wwin->client_flags.no_border = 0;
333 wwin->client_flags.no_titlebar = 0;
334 wwin->client_flags.no_closable = 0;
335 wwin->client_flags.no_miniaturizable = 0;
336 wwin->client_flags.no_resizable = 0;
337 wwin->client_flags.no_close_button = 0;
338 wwin->client_flags.no_miniaturize_button = 0;
339 wwin->client_flags.no_resizebar = 0;
341 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
342 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 GNOME_STUFF
435 check = wGNOMECheckClientHints(wwin, &tmp_level, &tmp_workspace);
436 #endif /* GNOME_STUFF */
438 #ifdef KWM_HINTS
439 if (!check)
440 wKWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
441 #endif /* KWM_HINTS */
443 #ifdef OLWM_HINTS
444 wOLWMCheckClientHints(wwin);
445 #endif /* OLWM_HINTS */
447 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
448 * have INT_MIN that means that no window level was requested. -Dan
450 if (tmp_level == INT_MIN) {
451 if (WFLAGP(wwin, floating))
452 *level = WMFloatingLevel;
453 else if (WFLAGP(wwin, sunken))
454 *level = WMSunkenLevel;
455 else
456 *level = WMNormalLevel;
457 } else {
458 *level = tmp_level;
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 /* dont use WSETUFLAG, since this was not an attribute change
499 * made by the user */
500 wwin->user_flags.no_focusable = 1;
507 Bool
508 wWindowCanReceiveFocus(WWindow *wwin)
510 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
511 return False;
512 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
513 return False;
514 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
515 return False;
517 return True;
521 Bool
522 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
524 int w1, h1, w2, h2;
526 w1 = wwin->frame->core->width;
527 h1 = wwin->frame->core->height;
528 w2 = obscured->frame->core->width;
529 h2 = obscured->frame->core->height;
531 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
532 && wwin->frame->workspace != obscured->frame->workspace)
533 return False;
535 if (wwin->frame_x + w1 < obscured->frame_x
536 || wwin->frame_y + h1 < obscured->frame_y
537 || wwin->frame_x > obscured->frame_x + w2
538 || wwin->frame_y > obscured->frame_y + h2) {
539 return False;
542 return True;
546 static Window
547 createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
549 XClassHint *classHint;
550 XWMHints *hints;
551 Window leader;
552 int argc;
553 char **argv;
555 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
556 /* set class hint */
557 classHint = XAllocClassHint();
558 classHint->res_name = instance;
559 classHint->res_class = class;
560 XSetClassHint(dpy, leader, classHint);
561 XFree(classHint);
563 /* inherit these from the original leader if available */
564 hints = XGetWMHints(dpy, win);
565 if (!hints) {
566 hints = XAllocWMHints();
567 hints->flags = 0;
569 /* set window group leader to self */
570 hints->window_group = leader;
571 hints->flags |= WindowGroupHint;
572 XSetWMHints(dpy, leader, hints);
573 XFree(hints);
575 if (XGetCommand(dpy, win, &argv, &argc)!=0 && argc > 0) {
576 XSetCommand(dpy, leader, argv, argc);
577 XFreeStringList(argv);
580 return leader;
584 static int
585 matchIdentifier(void *item, void *cdata)
587 return (strcmp(((WFakeGroupLeader*)item)->identifier, (char*)cdata)==0);
592 *----------------------------------------------------------------
593 * wManageWindow--
594 * reparents the window and allocates a descriptor for it.
595 * Window manager hints and other hints are fetched to configure
596 * the window decoration attributes and others. User preferences
597 * for the window are used if available, to configure window
598 * decorations and some behaviour.
599 * If in startup, windows that are override redirect,
600 * unmapped and never were managed and are Withdrawn are not
601 * managed.
603 * Returns:
604 * the new window descriptor
606 * Side effects:
607 * The window is reparented and appropriate notification
608 * is done to the client. Input mask for the window is setup.
609 * The window descriptor is also associated with various window
610 * contexts and inserted in the head of the window list.
611 * Event handler contexts are associated for some objects
612 * (buttons, titlebar and resizebar)
614 *----------------------------------------------------------------
616 WWindow*
617 wManageWindow(WScreen *scr, Window window)
619 WWindow *wwin;
620 int x, y;
621 unsigned width, height;
622 XWindowAttributes wattribs;
623 XSetWindowAttributes attribs;
624 WWindowState *win_state;
625 WWindow *transientOwner = NULL;
626 int window_level;
627 int wm_state;
628 int foo;
629 int workspace = -1;
630 char *title;
631 Bool withdraw = False;
632 Bool raise = False;
634 /* mutex. */
635 /* XGrabServer(dpy); */
636 XSync(dpy, False);
637 /* make sure the window is still there */
638 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
639 XUngrabServer(dpy);
640 return NULL;
643 /* if it's an override-redirect, ignore it */
644 if (wattribs.override_redirect) {
645 XUngrabServer(dpy);
646 return NULL;
649 wm_state = PropGetWindowState(window);
651 /* if it's startup and the window is unmapped, don't manage it */
652 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
653 XUngrabServer(dpy);
654 return NULL;
657 if (!wFetchName(dpy, window, &title)) {
658 title = NULL;
661 #ifdef KWM_HINTS
662 if (title && !wKWMManageableClient(scr, window, title)) {
663 XFree(title);
664 XUngrabServer(dpy);
665 return NULL;
667 #endif /* KWM_HINTS */
670 wwin = wWindowCreate();
672 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
674 #ifdef DEBUG
675 printf("managing window %x\n", (unsigned)window);
676 #endif
678 #ifdef SHAPE
679 if (wShapeSupported) {
680 int junk;
681 unsigned int ujunk;
682 int b_shaped;
684 XShapeSelectInput(dpy, window, ShapeNotifyMask);
685 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
686 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
687 wwin->flags.shaped = b_shaped;
689 #endif
692 *--------------------------------------------------
694 * Get hints and other information in properties
696 *--------------------------------------------------
698 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
700 /* setup descriptor */
701 wwin->client_win = window;
702 wwin->screen_ptr = scr;
704 wwin->old_border_width = wattribs.border_width;
706 wwin->event_mask = CLIENT_EVENTS;
707 attribs.event_mask = CLIENT_EVENTS;
708 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
709 attribs.save_under = False;
710 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
711 |CWSaveUnder, &attribs);
712 XSetWindowBorderWidth(dpy, window, 0);
714 /* get hints from GNUstep app */
715 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
716 wwin->flags.is_gnustep = 1;
718 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
719 wwin->wm_gnustep_attr = NULL;
722 wwin->client_leader = PropGetClientLeader(window);
723 if (wwin->client_leader!=None)
724 wwin->main_window = wwin->client_leader;
726 wwin->wm_hints = XGetWMHints(dpy, window);
728 if (wwin->wm_hints) {
729 if (wwin->wm_hints->flags & StateHint) {
731 if (wwin->wm_hints->initial_state == IconicState) {
733 wwin->flags.miniaturized = 1;
735 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
737 withdraw = True;
741 if (wwin->wm_hints->flags & WindowGroupHint) {
742 wwin->group_id = wwin->wm_hints->window_group;
743 /* window_group has priority over CLIENT_LEADER */
744 wwin->main_window = wwin->group_id;
745 } else {
746 wwin->group_id = None;
749 if (wwin->wm_hints->flags & UrgencyHint)
750 wwin->flags.urgent = 1;
751 } else {
752 wwin->group_id = None;
755 PropGetProtocols(window, &wwin->protocols);
757 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
758 wwin->transient_for = None;
759 } else {
760 if (wwin->transient_for==None || wwin->transient_for==window) {
761 wwin->transient_for = scr->root_win;
762 } else {
763 transientOwner = wWindowFor(wwin->transient_for);
764 if (transientOwner && transientOwner->main_window!=None) {
765 wwin->main_window = transientOwner->main_window;
766 } /*else {
767 wwin->main_window = None;
772 /* guess the focus mode */
773 wwin->focus_mode = getFocusMode(wwin);
775 /* get geometry stuff */
776 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
778 /* printf( "wManageWindow: %d %d %d %d\n", x, y, width, height);*/
780 /* get colormap windows */
781 GetColormapWindows(wwin);
784 *--------------------------------------------------
786 * Setup the decoration/window attributes and
787 * geometry
789 *--------------------------------------------------
792 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
794 #ifdef OLWM_HINTS
795 if (wwin->client_flags.olwm_transient && wwin->transient_for==None
796 && wwin->group_id != None && wwin->group_id != window) {
798 transientOwner = wWindowFor(wwin->group_id);
800 if (transientOwner) {
801 wwin->transient_for = wwin->group_id;
803 /* transients can't be iconified or maximized */
804 if (wwin->transient_for) {
805 WSETUFLAG(wwin, no_miniaturizable, 1);
806 WSETUFLAG(wwin, no_miniaturize_button, 1);
810 #endif /* OLWM_HINTS */
812 /* Make broken apps behave as a nice app. */
813 if (WFLAGP(wwin, emulate_appicon)) {
814 wwin->main_window = wwin->client_win;
817 wwin->orig_main_window = wwin->main_window;
819 if (wwin->flags.is_gnustep) {
820 WSETUFLAG(wwin, shared_appicon, 0);
823 if (wwin->main_window) {
824 extern Atom _XA_WINDOWMAKER_MENU;
825 XTextProperty text_prop;
827 if (XGetTextProperty(dpy, wwin->main_window, &text_prop,
828 _XA_WINDOWMAKER_MENU)) {
829 WSETUFLAG(wwin, shared_appicon, 0);
833 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
834 char *buffer, *instance, *class;
835 WFakeGroupLeader *fPtr;
836 int index;
838 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
841 PropGetWMClass(wwin->main_window, &class, &instance);
842 buffer = StrConcatDot(instance, class);
844 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void*)buffer);
845 if (index != WANotFound) {
846 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
847 if (fPtr->retainCount == 0) {
848 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
849 instance, class);
851 fPtr->retainCount++;
852 #undef method2
853 if (fPtr->origLeader==None) {
854 #ifdef method2
855 if (ADEQUATE(wwin->group_id)) {
856 fPtr->retainCount++;
857 fPtr->origLeader = wwin->group_id;
858 } else if (ADEQUATE(wwin->client_leader)) {
859 fPtr->retainCount++;
860 fPtr->origLeader = wwin->client_leader;
861 } else if (ADEQUATE(wwin->main_window)) {
862 fPtr->retainCount++;
863 fPtr->origLeader = wwin->main_window;
865 #else
866 if (ADEQUATE(wwin->main_window)) {
867 fPtr->retainCount++;
868 fPtr->origLeader = wwin->main_window;
870 #endif
872 wwin->fake_group = fPtr;
873 /*wwin->group_id = fPtr->leader;*/
874 wwin->main_window = fPtr->leader;
875 wfree(buffer);
876 } else {
877 fPtr = (WFakeGroupLeader*)wmalloc(sizeof(WFakeGroupLeader));
879 fPtr->identifier = buffer;
880 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
881 instance, class);
882 fPtr->origLeader = None;
883 fPtr->retainCount = 1;
885 WMAddToArray(scr->fakeGroupLeaders, fPtr);
887 #ifdef method2
888 if (ADEQUATE(wwin->group_id)) {
889 fPtr->retainCount++;
890 fPtr->origLeader = wwin->group_id;
891 } else if (ADEQUATE(wwin->client_leader)) {
892 fPtr->retainCount++;
893 fPtr->origLeader = wwin->client_leader;
894 } else if (ADEQUATE(wwin->main_window)) {
895 fPtr->retainCount++;
896 fPtr->origLeader = wwin->main_window;
898 #else
899 if (ADEQUATE(wwin->main_window)) {
900 fPtr->retainCount++;
901 fPtr->origLeader = wwin->main_window;
903 #endif
904 wwin->fake_group = fPtr;
905 /*wwin->group_id = fPtr->leader;*/
906 wwin->main_window = fPtr->leader;
908 if (instance)
909 XFree(instance);
910 if (class)
911 XFree(class);
913 #undef method2
914 #undef ADEQUATE
918 *------------------------------------------------------------
920 * Setup the initial state of the window
922 *------------------------------------------------------------
925 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
926 wwin->flags.miniaturized = 1;
929 if (WFLAGP(wwin, start_maximized) && !WFLAGP(wwin, no_resizable)) {
930 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
934 Bool bla;
936 bla = False;
937 #ifdef GNOME_STUFF
938 bla = wGNOMECheckInitialClientState(wwin);
939 #endif
940 #ifdef KWM_HINTS
941 if (!bla)
942 wKWMCheckClientInitialState(wwin);
943 #endif
946 /* apply previous state if it exists and we're in startup */
947 if (scr->flags.startup && wm_state >= 0) {
949 if (wm_state == IconicState) {
951 wwin->flags.miniaturized = 1;
953 } else if (wm_state == WithdrawnState) {
955 withdraw = True;
959 /* if there is a saved state (from file), restore it */
960 win_state = NULL;
961 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
962 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
963 } else {
964 win_state = (WWindowState*)wWindowGetSavedState(window);
966 if (win_state && !withdraw) {
968 if (win_state->state->hidden>0)
969 wwin->flags.hidden = win_state->state->hidden;
971 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
972 wwin->flags.shaded = win_state->state->shaded;
974 if (win_state->state->miniaturized>0 &&
975 !WFLAGP(wwin, no_miniaturizable)) {
976 wwin->flags.miniaturized = win_state->state->miniaturized;
979 if (!IS_OMNIPRESENT(wwin)) {
980 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
981 wwin->wm_class);
982 if (w < 0 || w >= scr->workspace_count) {
983 workspace = win_state->state->workspace;
984 if (workspace >= scr->workspace_count)
985 workspace = scr->current_workspace;
986 } else {
987 workspace = w;
989 } else {
990 workspace = scr->current_workspace;
994 /* if we're restarting, restore saved state (from hints).
995 * This will overwrite previous */
997 WSavedState *wstate;
999 if (getSavedState(window, &wstate)) {
1000 wwin->flags.shaded = wstate->shaded;
1001 wwin->flags.hidden = wstate->hidden;
1002 wwin->flags.miniaturized = wstate->miniaturized;
1003 wwin->flags.maximized = wstate->maximized;
1004 if (wwin->flags.maximized) {
1005 wwin->old_geometry.x = wstate->x;
1006 wwin->old_geometry.y = wstate->y;
1007 wwin->old_geometry.width = wstate->w;
1008 wwin->old_geometry.height = wstate->h;
1011 workspace = wstate->workspace;
1012 } else {
1013 wstate = NULL;
1016 /* restore window shortcut */
1017 if (wstate != NULL || win_state != NULL) {
1018 unsigned mask = 0;
1020 if (win_state != NULL)
1021 mask = win_state->state->window_shortcuts;
1023 if (wstate != NULL && mask == 0)
1024 mask = wstate->window_shortcuts;
1026 if (mask > 0) {
1027 int i;
1029 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1030 if (mask & (1<<i)) {
1031 if (!scr->shortcutWindows[i])
1032 scr->shortcutWindows[i] = WMCreateArray(4);
1034 WMAddToArray(scr->shortcutWindows[i], wwin);
1039 if (wstate != NULL) {
1040 wfree(wstate);
1044 /* don't let transients start miniaturized if their owners are not */
1045 if (transientOwner && !transientOwner->flags.miniaturized
1046 && wwin->flags.miniaturized && !withdraw) {
1047 wwin->flags.miniaturized = 0;
1048 if (wwin->wm_hints)
1049 wwin->wm_hints->initial_state = NormalState;
1052 /* set workspace on which the window starts */
1053 if (workspace >= 0) {
1054 if (workspace > scr->workspace_count-1) {
1055 workspace = workspace % scr->workspace_count;
1057 } else {
1058 int w;
1060 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1062 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1064 workspace = w;
1066 } else {
1067 if (wPreferences.open_transients_with_parent && transientOwner) {
1069 workspace = transientOwner->frame->workspace;
1071 } else {
1073 workspace = scr->current_workspace;
1078 /* setup window geometry */
1079 if (win_state && win_state->state->w > 0) {
1080 width = win_state->state->w;
1081 height = win_state->state->h;
1083 wWindowConstrainSize(wwin, &width, &height);
1085 /* do not ask for window placement if the window is
1086 * transient, during startup, if the initial workspace is another one
1087 * or if the window wants to start iconic.
1088 * If geometry was saved, restore it. */
1090 Bool dontBring = False;
1092 if (win_state && win_state->state->w > 0) {
1093 x = win_state->state->x;
1094 y = win_state->state->y;
1095 } else if ((wwin->transient_for==None
1096 || wPreferences.window_placement!=WPM_MANUAL)
1097 && !scr->flags.startup
1098 && workspace == scr->current_workspace
1099 && !wwin->flags.miniaturized
1100 && !wwin->flags.maximized
1101 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
1103 if (transientOwner && transientOwner->flags.mapped) {
1104 int offs = WMAX(20, 2*transientOwner->frame->top_width);
1105 WMRect rect;
1106 int head;
1108 x = transientOwner->frame_x +
1109 abs((transientOwner->frame->core->width - width)/2) + offs;
1110 y = transientOwner->frame_y +
1111 abs((transientOwner->frame->core->height - height)/3) + offs;
1114 * limit transient windows to be inside their parent's head
1116 rect.pos.x = transientOwner->frame_x;
1117 rect.pos.y = transientOwner->frame_y;
1118 rect.size.width = transientOwner->frame->core->width;
1119 rect.size.height = transientOwner->frame->core->height;
1121 head = wGetHeadForRect(scr, rect);
1122 rect = wGetRectForHead(scr, head);
1124 if (x < rect.pos.x)
1125 x = rect.pos.x;
1126 else if (x + width > rect.pos.x + rect.size.width)
1127 x = rect.pos.x + rect.size.width - width;
1129 if (y < rect.pos.y)
1130 y = rect.pos.y;
1131 else if (y + height > rect.pos.y + rect.size.height)
1132 y = rect.pos.y + rect.size.height - height;
1134 } else {
1135 PlaceWindow(wwin, &x, &y, width, height);
1137 if (wPreferences.window_placement == WPM_MANUAL)
1138 dontBring = True;
1140 else if (scr->xine_count &&
1141 wwin->normal_hints->flags & PPosition) {
1142 int head, flags;
1143 WMRect rect;
1144 int reposition = 0;
1147 * Make spash screens come out in the center of a head
1148 * trouble is that most splashies never get here
1149 * they are managed trough atoms but god knows where.
1150 * Dan, do you know ?
1152 * Most of them are not managed, they are set
1153 * OverrideRedirect, which means we can't do anything about
1154 * them. -alfredo
1156 #if 0
1157 printf( "xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width)/2);
1158 printf( "xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height)/2);
1160 if ( (unsigned)(x + (width - scr->scr_width)/2 + 10) < 20 &&
1161 (unsigned)(y + (height - scr->scr_height)/2 + 10) < 20) {
1163 reposition = 1;
1165 } else
1166 #endif
1169 * xinerama checks for: across head and dead space
1171 rect.pos.x = x;
1172 rect.pos.y = y;
1173 rect.size.width = width;
1174 rect.size.height = height;
1176 head = wGetRectPlacementInfo(scr, rect, &flags);
1178 if (flags & XFLAG_DEAD)
1179 reposition = 1;
1181 if (flags & XFLAG_MULTIPLE)
1182 reposition = 2;
1185 switch (reposition) {
1186 case 1:
1187 head = wGetHeadForPointerLocation(scr);
1188 rect = wGetRectForHead(scr, head);
1190 x = rect.pos.x + (x * rect.size.width)/scr->scr_width;
1191 y = rect.pos.y + (y * rect.size.height)/scr->scr_height;
1192 break;
1194 case 2:
1195 rect = wGetRectForHead(scr, head);
1197 if (x < rect.pos.x)
1198 x = rect.pos.x;
1199 else if (x + width > rect.pos.x + rect.size.width)
1200 x = rect.pos.x + rect.size.width - width;
1202 if (y < rect.pos.y)
1203 y = rect.pos.y;
1204 else if (y + height > rect.pos.y + rect.size.height)
1205 y = rect.pos.y + rect.size.height - height;
1207 break;
1209 default:
1210 break;
1214 if (WFLAGP(wwin, dont_move_off) && dontBring)
1215 wScreenBringInside(scr, &x, &y, width, height);
1218 if (wwin->flags.urgent) {
1219 if (!IS_OMNIPRESENT(wwin))
1220 wwin->flags.omnipresent ^= 1;
1224 *--------------------------------------------------
1226 * Create frame, borders and do reparenting
1228 *--------------------------------------------------
1230 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1231 #ifdef XKB_BUTTON_HINT
1232 if (wPreferences.modelock)
1233 foo |= WFF_LANGUAGE_BUTTON;
1234 #endif
1235 if (!WFLAGP(wwin, no_titlebar))
1236 foo |= WFF_TITLEBAR;
1237 if (!WFLAGP(wwin, no_resizebar))
1238 foo |= WFF_RESIZEBAR;
1239 if (!WFLAGP(wwin, no_border))
1240 foo |= WFF_BORDER;
1242 wwin->frame = wFrameWindowCreate(scr, window_level,
1243 x, y, width, height,
1244 &wPreferences.window_title_clearance, foo,
1245 scr->window_title_texture,
1246 scr->resizebar_texture,
1247 scr->window_title_color,
1248 &scr->title_font);
1250 wwin->frame->flags.is_client_window_frame = 1;
1251 wwin->frame->flags.justification = wPreferences.title_justification;
1253 /* setup button images */
1254 wWindowUpdateButtonImages(wwin);
1256 /* hide unused buttons */
1257 foo = 0;
1258 if (WFLAGP(wwin, no_close_button))
1259 foo |= WFF_RIGHT_BUTTON;
1260 if (WFLAGP(wwin, no_miniaturize_button))
1261 foo |= WFF_LEFT_BUTTON;
1262 #ifdef XKB_BUTTON_HINT
1263 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1264 foo |= WFF_LANGUAGE_BUTTON;
1265 #endif
1266 if (foo!=0)
1267 wFrameWindowHideButton(wwin->frame, foo);
1269 wwin->frame->child = wwin;
1271 #ifdef OLWM_HINTS
1272 /* emulate olwm push pin. Make the button look as pushed-in for
1273 * the pinned-out state. When the button is clicked, it will
1274 * revert to the normal position, which means the pin is pinned-in.
1276 if (wwin->flags.olwm_push_pin_out)
1277 wFrameWindowUpdatePushButton(wwin->frame, True);
1278 #endif /* OLWM_HINTS */
1281 wwin->frame->workspace = workspace;
1283 wwin->frame->on_click_left = windowIconifyClick;
1284 #ifdef XKB_BUTTON_HINT
1285 if (wPreferences.modelock)
1286 wwin->frame->on_click_language = windowLanguageClick;
1287 #endif
1289 wwin->frame->on_click_right = windowCloseClick;
1290 wwin->frame->on_dblclick_right = windowCloseDblClick;
1292 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1293 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1295 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1298 XSelectInput(dpy, wwin->client_win,
1299 wwin->event_mask & ~StructureNotifyMask);
1301 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1302 0, wwin->frame->top_width);
1304 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1308 int gx, gy;
1310 wClientGetGravityOffsets(wwin, &gx, &gy);
1312 /* if gravity is to the south, account for the border sizes */
1313 if (gy > 0)
1314 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1318 * wWindowConfigure() will init the client window's size
1319 * (wwin->client.{width,height}) and all other geometry
1320 * related variables (frame_x,frame_y)
1322 wWindowConfigure(wwin, x, y, width, height);
1324 /* to make sure the window receives it's new position after reparenting */
1325 wWindowSynthConfigureNotify(wwin);
1328 *--------------------------------------------------
1330 * Setup descriptors and save window to internal
1331 * lists
1333 *--------------------------------------------------
1336 if (wwin->main_window!=None) {
1337 WApplication *app;
1338 WWindow *leader;
1340 /* Leader windows do not necessary set themselves as leaders.
1341 * If this is the case, point the leader of this window to
1342 * itself */
1343 leader = wWindowFor(wwin->main_window);
1344 if (leader && leader->main_window==None) {
1345 leader->main_window = leader->client_win;
1347 app = wApplicationCreate(scr, wwin->main_window);
1348 if (app) {
1349 app->last_workspace = workspace;
1351 app->main_window_desc->fake_group = wwin->fake_group;
1354 * Do application specific stuff, like setting application
1355 * wide attributes.
1358 if (wwin->flags.hidden) {
1359 /* if the window was set to hidden because it was hidden
1360 * in a previous incarnation and that state was restored */
1361 app->flags.hidden = 1;
1362 } else if (app->flags.hidden) {
1363 if (WFLAGP(app->main_window_desc, start_hidden)) {
1364 wwin->flags.hidden = 1;
1365 } else {
1366 wUnhideApplication(app, False, False);
1367 raise = True;
1373 /* setup the frame descriptor */
1374 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1375 wwin->frame->core->descriptor.parent = wwin;
1376 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1378 /* don't let windows go away if we die */
1379 XAddToSaveSet(dpy, window);
1381 XLowerWindow(dpy, window);
1383 /* if window is in this workspace and should be mapped, then map it */
1384 if (!wwin->flags.miniaturized
1385 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1386 && !wwin->flags.hidden && !withdraw) {
1388 /* The following "if" is to avoid crashing of clients that expect
1389 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1390 * after the return from this function.
1392 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1393 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1394 } else {
1395 wClientSetState(wwin, NormalState, None);
1398 #if 0
1399 /* if not auto focus, then map the window under the currently
1400 * focused window */
1401 #define _WIDTH(w) (w)->frame->core->width
1402 #define _HEIGHT(w) (w)->frame->core->height
1403 if (!wPreferences.auto_focus && scr->focused_window
1404 && !scr->flags.startup && !transientOwner
1405 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1406 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1407 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1408 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1409 || wwin->flags.maximized)) {
1410 MoveInStackListUnder(scr->focused_window->frame->core,
1411 wwin->frame->core);
1413 #undef _WIDTH
1414 #undef _HEIGHT
1416 #endif
1418 if (wPreferences.superfluous && !wPreferences.no_animations
1419 && !scr->flags.startup &&
1420 (wwin->transient_for==None || wwin->transient_for==scr->root_win)
1422 * The brain damaged idiotic non-click to focus modes will
1423 * have trouble with this because:
1425 * 1. window is created and mapped by the client
1426 * 2. window is mapped by wmaker in small size
1427 * 3. window is animated to grow to normal size
1428 * 4. this function returns to normal event loop
1429 * 5. eventually, the EnterNotify event that would trigger
1430 * the window focusing (if the mouse is over that window)
1431 * will be processed by wmaker.
1432 * But since this event will be rather delayed
1433 * (step 3 has a large delay) the time when the event ocurred
1434 * and when it is processed, the client that owns that window
1435 * will reject the XSetInputFocus() for it.
1437 && (wPreferences.focus_mode==WKF_CLICK
1438 || wPreferences.auto_focus)) {
1439 DoWindowBirth(wwin);
1442 wWindowMap(wwin);
1445 /* setup stacking descriptor */
1446 if (transientOwner) {
1447 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1448 } else {
1449 wwin->frame->core->stacking->child_of = NULL;
1453 if (!scr->focused_window) {
1454 /* first window on the list */
1455 wwin->next = NULL;
1456 wwin->prev = NULL;
1457 scr->focused_window = wwin;
1458 } else {
1459 WWindow *tmp;
1461 /* add window at beginning of focus window list */
1462 tmp = scr->focused_window;
1463 while (tmp->prev)
1464 tmp = tmp->prev;
1465 tmp->prev = wwin;
1466 wwin->next = tmp;
1467 wwin->prev = NULL;
1470 /* raise is set to true if we un-hid the app when this window was born.
1471 * we raise, else old windows of this app will be above this new one. */
1472 if (raise) {
1473 wRaiseFrame(wwin->frame->core);
1476 /* Update name must come after WApplication stuff is done */
1477 wWindowUpdateName(wwin, title);
1478 if (title)
1479 XFree(title);
1481 XUngrabServer(dpy);
1484 *--------------------------------------------------
1486 * Final preparations before window is ready to go
1488 *--------------------------------------------------
1491 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1494 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1495 && !wwin->flags.hidden) {
1496 if (((transientOwner && transientOwner->flags.focused)
1497 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1498 wSetFocusTo(scr, wwin);
1500 wWindowResetMouseGrabs(wwin);
1502 if (!WFLAGP(wwin, no_bind_keys)) {
1503 wWindowSetKeyGrabs(wwin);
1507 WMPostNotificationName(WMNManaged, wwin, NULL);
1510 wColormapInstallForWindow(scr, scr->cmap_window);
1513 #ifdef OLWM_HINTS
1514 if (wwin->client_flags.olwm_warp_to_pin && wwin->frame->titlebar != NULL
1515 && !WFLAGP(wwin, no_close_button) && !withdraw) {
1517 XWarpPointer(dpy, None, None, 0, 0, 0, 0,
1518 wwin->frame_x + width - wwin->frame->titlebar->height * 2,
1519 wwin->frame_y);
1521 #endif
1524 *------------------------------------------------------------
1525 * Setup Notification Observers
1526 *------------------------------------------------------------
1528 WMAddNotificationObserver(appearanceObserver, wwin,
1529 WNWindowAppearanceSettingsChanged, wwin);
1533 *--------------------------------------------------
1535 * Cleanup temporary stuff
1537 *--------------------------------------------------
1540 if (win_state)
1541 wWindowDeleteSavedState(win_state);
1543 /* If the window must be withdrawed, then do it now.
1544 * Must do some optimization, 'though */
1545 if (withdraw) {
1546 wwin->flags.mapped = 0;
1547 wClientSetState(wwin, WithdrawnState, None);
1548 wUnmanageWindow(wwin, True, False);
1549 wwin = NULL;
1552 return wwin;
1559 WWindow*
1560 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1561 char *title, int x, int y, int width, int height)
1563 WWindow *wwin;
1564 int foo;
1566 wwin = wWindowCreate();
1568 WMAddNotificationObserver(appearanceObserver, wwin,
1569 WNWindowAppearanceSettingsChanged, wwin);
1571 wwin->flags.internal_window = 1;
1573 WSETUFLAG(wwin, omnipresent, 1);
1574 WSETUFLAG(wwin, no_shadeable, 1);
1575 WSETUFLAG(wwin, no_resizable, 1);
1576 WSETUFLAG(wwin, no_miniaturizable, 1);
1578 wwin->focus_mode = WFM_PASSIVE;
1580 wwin->client_win = window;
1581 wwin->screen_ptr = scr;
1583 wwin->transient_for = owner;
1585 wwin->client.x = x;
1586 wwin->client.y = y;
1587 wwin->client.width = width;
1588 wwin->client.height = height;
1590 wwin->frame_x = wwin->client.x;
1591 wwin->frame_y = wwin->client.y;
1594 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1595 foo |= WFF_TITLEBAR;
1596 #ifdef XKB_BUTTON_HINT
1597 foo |= WFF_LANGUAGE_BUTTON;
1598 #endif
1600 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1601 wwin->frame_x, wwin->frame_y,
1602 width, height,
1603 &wPreferences.window_title_clearance, foo,
1604 scr->window_title_texture,
1605 scr->resizebar_texture,
1606 scr->window_title_color,
1607 &scr->title_font);
1609 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1611 wwin->frame->flags.is_client_window_frame = 1;
1612 wwin->frame->flags.justification = wPreferences.title_justification;
1614 wFrameWindowChangeTitle(wwin->frame, title);
1616 /* setup button images */
1617 wWindowUpdateButtonImages(wwin);
1619 /* hide buttons */
1620 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1622 wwin->frame->child = wwin;
1624 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1626 #ifdef XKB_BUTTON_HINT
1627 if (wPreferences.modelock)
1628 wwin->frame->on_click_language = windowLanguageClick;
1629 #endif
1631 wwin->frame->on_click_right = windowCloseClick;
1633 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1634 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1636 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1638 wwin->client.y += wwin->frame->top_width;
1639 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1640 0, wwin->frame->top_width);
1642 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1643 wwin->client.width, wwin->client.height);
1645 /* setup the frame descriptor */
1646 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1647 wwin->frame->core->descriptor.parent = wwin;
1648 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1651 XLowerWindow(dpy, window);
1652 XMapSubwindows(dpy, wwin->frame->core->window);
1654 /* setup stacking descriptor */
1655 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
1656 WWindow *tmp;
1657 tmp = wWindowFor(wwin->transient_for);
1658 if (tmp)
1659 wwin->frame->core->stacking->child_of = tmp->frame->core;
1660 } else {
1661 wwin->frame->core->stacking->child_of = NULL;
1665 if (!scr->focused_window) {
1666 /* first window on the list */
1667 wwin->next = NULL;
1668 wwin->prev = NULL;
1669 scr->focused_window = wwin;
1670 } else {
1671 WWindow *tmp;
1673 /* add window at beginning of focus window list */
1674 tmp = scr->focused_window;
1675 while (tmp->prev)
1676 tmp = tmp->prev;
1677 tmp->prev = wwin;
1678 wwin->next = tmp;
1679 wwin->prev = NULL;
1682 if (wwin->flags.is_gnustep == 0)
1683 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1685 /* if (wPreferences.auto_focus)*/
1686 wSetFocusTo(scr, wwin);
1688 wWindowResetMouseGrabs(wwin);
1690 wWindowSetKeyGrabs(wwin);
1692 return wwin;
1697 *----------------------------------------------------------------------
1698 * wUnmanageWindow--
1699 * Removes the frame window from a window and destroys all data
1700 * related to it. The window will be reparented back to the root window
1701 * if restore is True.
1703 * Side effects:
1704 * Everything related to the window is destroyed and the window
1705 * is removed from the window lists. Focus is set to the previous on the
1706 * window list.
1707 *----------------------------------------------------------------------
1709 void
1710 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1712 WCoreWindow *frame = wwin->frame->core;
1713 WWindow *owner = NULL;
1714 WWindow *newFocusedWindow = NULL;
1715 int wasFocused;
1716 WScreen *scr = wwin->screen_ptr;
1719 /* First close attribute editor window if open */
1720 if (wwin->flags.inspector_open) {
1721 wCloseInspectorForWindow(wwin);
1724 /* Close window menu if it's open for this window */
1725 if (wwin->flags.menu_open_for_me) {
1726 CloseWindowMenu(scr);
1729 if (!destroyed) {
1730 if (!wwin->flags.internal_window)
1731 XRemoveFromSaveSet(dpy, wwin->client_win);
1733 XSelectInput(dpy, wwin->client_win, NoEventMask);
1735 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1736 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1739 XUnmapWindow(dpy, frame->window);
1741 XUnmapWindow(dpy, wwin->client_win);
1743 /* deselect window */
1744 wSelectWindow(wwin, False);
1746 /* remove all pending events on window */
1747 /* I think this only matters for autoraise */
1748 if (wPreferences.raise_delay)
1749 WMDeleteTimerWithClientData(wwin->frame->core);
1751 XFlush(dpy);
1753 /* reparent the window back to the root */
1754 if (restore)
1755 wClientRestore(wwin);
1757 if (wwin->transient_for!=scr->root_win) {
1758 owner = wWindowFor(wwin->transient_for);
1759 if (owner) {
1760 if (!owner->flags.semi_focused) {
1761 owner = NULL;
1762 } else {
1763 owner->flags.semi_focused = 0;
1768 wasFocused = wwin->flags.focused;
1770 /* remove from window focus list */
1771 if (!wwin->prev && !wwin->next) {
1772 /* was the only window */
1773 scr->focused_window = NULL;
1774 newFocusedWindow = NULL;
1775 } else {
1776 WWindow *tmp;
1778 if (wwin->prev)
1779 wwin->prev->next = wwin->next;
1780 if (wwin->next)
1781 wwin->next->prev = wwin->prev;
1782 else {
1783 scr->focused_window = wwin->prev;
1784 scr->focused_window->next = NULL;
1787 if (wPreferences.focus_mode==WKF_CLICK) {
1789 /* if in click to focus mode and the window
1790 * was a transient, focus the owner window
1792 tmp = NULL;
1793 if (wPreferences.focus_mode==WKF_CLICK) {
1794 tmp = wWindowFor(wwin->transient_for);
1795 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1796 tmp = NULL;
1799 /* otherwise, focus the next one in the focus list */
1800 if (!tmp) {
1801 tmp = scr->focused_window;
1802 while (tmp) { /* look for one in the window list first */
1803 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1804 && (tmp->flags.mapped || tmp->flags.shaded))
1805 break;
1806 tmp = tmp->prev;
1808 if (!tmp) { /* if unsuccessful, choose any focusable window */
1809 tmp = scr->focused_window;
1810 while (tmp) {
1811 if (!WFLAGP(tmp, no_focusable)
1812 && (tmp->flags.mapped || tmp->flags.shaded))
1813 break;
1814 tmp = tmp->prev;
1819 newFocusedWindow = tmp;
1821 } else if (wPreferences.focus_mode==WKF_SLOPPY) {
1822 unsigned int mask;
1823 int foo;
1824 Window bar, win;
1826 /* This is to let the root window get the keyboard input
1827 * if Sloppy focus mode and no other window get focus.
1828 * This way keybindings will not freeze.
1830 tmp = NULL;
1831 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1832 &foo, &foo, &foo, &foo, &mask))
1833 tmp = wWindowFor(win);
1834 if (tmp == wwin)
1835 tmp = NULL;
1836 newFocusedWindow = tmp;
1837 } else {
1838 newFocusedWindow = NULL;
1842 if (!wwin->flags.internal_window) {
1843 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1846 #ifdef DEBUG
1847 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1848 (unsigned)frame->window);
1849 #endif
1851 if (wasFocused) {
1852 if (newFocusedWindow != owner && owner) {
1853 if (wwin->flags.is_gnustep == 0)
1854 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1856 wSetFocusTo(scr, newFocusedWindow);
1858 wWindowDestroy(wwin);
1859 XFlush(dpy);
1863 void
1864 wWindowMap(WWindow *wwin)
1866 XMapWindow(dpy, wwin->frame->core->window);
1867 if (!wwin->flags.shaded) {
1868 /* window will be remapped when getting MapNotify */
1869 XSelectInput(dpy, wwin->client_win,
1870 wwin->event_mask & ~StructureNotifyMask);
1871 XMapWindow(dpy, wwin->client_win);
1872 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1874 wwin->flags.mapped = 1;
1879 void
1880 wWindowUnmap(WWindow *wwin)
1882 wwin->flags.mapped = 0;
1884 /* prevent window withdrawal when getting UnmapNotify */
1885 XSelectInput(dpy, wwin->client_win,
1886 wwin->event_mask & ~StructureNotifyMask);
1887 XUnmapWindow(dpy, wwin->client_win);
1888 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1890 XUnmapWindow(dpy, wwin->frame->core->window);
1895 void
1896 wWindowFocus(WWindow *wwin, WWindow *owin)
1898 WWindow *nowner;
1899 WWindow *oowner;
1901 #ifdef KEEP_XKB_LOCK_STATUS
1902 if (wPreferences.modelock) {
1903 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1905 #endif /* KEEP_XKB_LOCK_STATUS */
1907 wwin->flags.semi_focused = 0;
1909 if (wwin->flags.is_gnustep == 0)
1910 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1912 wwin->flags.focused = 1;
1914 wWindowResetMouseGrabs(wwin);
1916 WMPostNotificationName(WMNChangedFocus, wwin, (void*)True);
1918 if (owin == wwin || !owin)
1919 return;
1921 nowner = wWindowFor(wwin->transient_for);
1923 /* new window is a transient for the old window */
1924 if (nowner == owin) {
1925 owin->flags.semi_focused = 1;
1926 wWindowUnfocus(nowner);
1927 return;
1930 oowner = wWindowFor(owin->transient_for);
1932 /* new window is owner of old window */
1933 if (wwin == oowner) {
1934 wWindowUnfocus(owin);
1935 return;
1938 if (!nowner) {
1939 wWindowUnfocus(owin);
1940 return;
1943 /* new window has same owner of old window */
1944 if (oowner == nowner) {
1945 /* prevent unfocusing of owner */
1946 oowner->flags.semi_focused = 0;
1947 wWindowUnfocus(owin);
1948 oowner->flags.semi_focused = 1;
1950 return;
1953 /* nowner != NULL && oowner != nowner */
1954 nowner->flags.semi_focused = 1;
1955 wWindowUnfocus(nowner);
1956 wWindowUnfocus(owin);
1960 void
1961 wWindowUnfocus(WWindow *wwin)
1963 CloseWindowMenu(wwin->screen_ptr);
1965 if (wwin->flags.is_gnustep == 0)
1966 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1967 ? WS_PFOCUSED : WS_UNFOCUSED);
1969 if (wwin->transient_for!=None
1970 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1971 WWindow *owner;
1972 owner = wWindowFor(wwin->transient_for);
1973 if (owner && owner->flags.semi_focused) {
1974 owner->flags.semi_focused = 0;
1975 if (owner->flags.mapped || owner->flags.shaded) {
1976 wWindowUnfocus(owner);
1977 wFrameWindowPaint(owner->frame);
1981 wwin->flags.focused = 0;
1983 wWindowResetMouseGrabs(wwin);
1985 WMPostNotificationName(WMNChangedFocus, wwin, (void*)False);
1989 void
1990 wWindowUpdateName(WWindow *wwin, char *newTitle)
1992 char *title;
1994 if (!wwin->frame)
1995 return;
1997 wwin->flags.wm_name_changed = 1;
1999 if (!newTitle) {
2000 /* the hint was removed */
2001 title = DEF_WINDOW_TITLE;
2002 } else {
2003 title = newTitle;
2006 if (wFrameWindowChangeTitle(wwin->frame, title)) {
2007 WMPostNotificationName(WMNChangedName, wwin, NULL);
2014 *----------------------------------------------------------------------
2016 * wWindowConstrainSize--
2017 * Constrains size for the client window, taking the maximal size,
2018 * window resize increments and other size hints into account.
2020 * Returns:
2021 * The closest size to what was given that the client window can
2022 * have.
2024 *----------------------------------------------------------------------
2026 void
2027 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
2029 int width = *nwidth;
2030 int height = *nheight;
2031 int winc = 1;
2032 int hinc = 1;
2033 int minW = 1, minH = 1;
2034 int maxW = wwin->screen_ptr->scr_width*2;
2035 int maxH = wwin->screen_ptr->scr_height*2;
2036 int minAX = -1, minAY = -1;
2037 int maxAX = -1, maxAY = -1;
2038 int baseW = 0;
2039 int baseH = 0;
2041 if (wwin->normal_hints) {
2042 winc = wwin->normal_hints->width_inc;
2043 hinc = wwin->normal_hints->height_inc;
2044 minW = wwin->normal_hints->min_width;
2045 minH = wwin->normal_hints->min_height;
2046 maxW = wwin->normal_hints->max_width;
2047 maxH = wwin->normal_hints->max_height;
2048 if (wwin->normal_hints->flags & PAspect) {
2049 minAX = wwin->normal_hints->min_aspect.x;
2050 minAY = wwin->normal_hints->min_aspect.y;
2051 maxAX = wwin->normal_hints->max_aspect.x;
2052 maxAY = wwin->normal_hints->max_aspect.y;
2055 baseW = wwin->normal_hints->base_width;
2056 baseH = wwin->normal_hints->base_height;
2059 if (width < minW)
2060 width = minW;
2061 if (height < minH)
2062 height = minH;
2064 if (width > maxW)
2065 width = maxW;
2066 if (height > maxH)
2067 height = maxH;
2069 /* aspect ratio code borrowed from olwm */
2070 if (minAX > 0) {
2071 /* adjust max aspect ratio */
2072 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
2073 if (maxAX > maxAY) {
2074 height = (width * maxAY) / maxAX;
2075 if (height > maxH) {
2076 height = maxH;
2077 width = (height * maxAX) / maxAY;
2079 } else {
2080 width = (height * maxAX) / maxAY;
2081 if (width > maxW) {
2082 width = maxW;
2083 height = (width * maxAY) / maxAX;
2088 /* adjust min aspect ratio */
2089 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
2090 if (minAX > minAY) {
2091 height = (width * minAY) / minAX;
2092 if (height < minH) {
2093 height = minH;
2094 width = (height * minAX) / minAY;
2096 } else {
2097 width = (height * minAX) / minAY;
2098 if (width < minW) {
2099 width = minW;
2100 height = (width * minAY) / minAX;
2106 if (baseW != 0) {
2107 width = (((width - baseW) / winc) * winc) + baseW;
2108 } else {
2109 width = (((width - minW) / winc) * winc) + minW;
2112 if (baseH != 0) {
2113 height = (((height - baseH) / hinc) * hinc) + baseH;
2114 } else {
2115 height = (((height - minH) / hinc) * hinc) + minH;
2118 /* broken stupid apps may cause preposterous values for these.. */
2119 if (width > 0)
2120 *nwidth = width;
2121 if (height > 0)
2122 *nheight = height;
2126 void
2127 wWindowCropSize(WWindow *wwin, int maxW, int maxH,
2128 int *width, int *height)
2130 int baseW = 0, baseH = 0;
2131 int winc = 1, hinc = 1;
2133 if (wwin->normal_hints) {
2134 baseW = wwin->normal_hints->base_width;
2135 baseH = wwin->normal_hints->base_height;
2137 winc = wwin->normal_hints->width_inc;
2138 hinc = wwin->normal_hints->height_inc;
2141 if (*width > maxW)
2142 *width = maxW - (maxW - baseW) % winc;
2144 if (*height > maxH)
2145 *height = maxH - (maxH - baseH) % hinc;
2149 void
2150 wWindowChangeWorkspace(WWindow *wwin, int workspace)
2152 WScreen *scr = wwin->screen_ptr;
2153 WApplication *wapp;
2154 int unmap = 0;
2156 if (workspace >= scr->workspace_count || workspace < 0
2157 || workspace == wwin->frame->workspace)
2158 return;
2160 if (workspace != scr->current_workspace) {
2161 /* Sent to other workspace. Unmap window */
2162 if ((wwin->flags.mapped
2163 || wwin->flags.shaded
2164 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2165 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2167 wapp = wApplicationOf(wwin->main_window);
2168 if (wapp) {
2169 wapp->last_workspace = workspace;
2171 if (wwin->flags.miniaturized) {
2172 if (wwin->icon) {
2173 XUnmapWindow(dpy, wwin->icon->core->window);
2174 wwin->icon->mapped = 0;
2176 } else {
2177 unmap = 1;
2178 wSetFocusTo(scr, NULL);
2181 } else {
2182 /* brought to current workspace. Map window */
2183 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2184 if (wwin->icon) {
2185 XMapWindow(dpy, wwin->icon->core->window);
2186 wwin->icon->mapped = 1;
2188 } else if (!wwin->flags.mapped &&
2189 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2190 wWindowMap(wwin);
2193 if (!IS_OMNIPRESENT(wwin)) {
2194 int oldWorkspace = wwin->frame->workspace;
2196 wwin->frame->workspace = workspace;
2198 WMPostNotificationName(WMNChangedWorkspace, wwin, (void*)oldWorkspace);
2201 if (unmap) {
2202 wWindowUnmap(wwin);
2207 void
2208 wWindowSynthConfigureNotify(WWindow *wwin)
2210 XEvent sevent;
2212 sevent.type = ConfigureNotify;
2213 sevent.xconfigure.display = dpy;
2214 sevent.xconfigure.event = wwin->client_win;
2215 sevent.xconfigure.window = wwin->client_win;
2217 sevent.xconfigure.x = wwin->client.x;
2218 sevent.xconfigure.y = wwin->client.y;
2219 sevent.xconfigure.width = wwin->client.width;
2220 sevent.xconfigure.height = wwin->client.height;
2222 sevent.xconfigure.border_width = wwin->old_border_width;
2223 if (WFLAGP(wwin, no_titlebar))
2224 sevent.xconfigure.above = None;
2225 else
2226 sevent.xconfigure.above = wwin->frame->titlebar->window;
2228 sevent.xconfigure.override_redirect = False;
2229 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2230 #ifdef KWM_HINTS
2231 wKWMSendEventMessage(wwin, WKWMChangedClient);
2232 #endif
2233 XFlush(dpy);
2238 *----------------------------------------------------------------------
2239 * wWindowConfigure--
2240 * Configures the frame, decorations and client window to the
2241 * specified geometry. The geometry is not checked for validity,
2242 * wWindowConstrainSize() must be used for that.
2243 * The size parameters are for the client window, but the position is
2244 * for the frame.
2245 * The client window receives a ConfigureNotify event, according
2246 * to what ICCCM says.
2248 * Returns:
2249 * None
2251 * Side effects:
2252 * Window size and position are changed and client window receives
2253 * a ConfigureNotify event.
2254 *----------------------------------------------------------------------
2256 void
2257 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
2258 WWindow *wwin;
2259 int req_x, req_y; /* new position of the frame */
2260 int req_width, req_height; /* new size of the client */
2262 int synth_notify = False;
2263 int resize;
2265 resize = (req_width!=wwin->client.width
2266 || req_height!=wwin->client.height);
2268 * if the window is being moved but not resized then
2269 * send a synthetic ConfigureNotify
2271 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
2272 synth_notify = True;
2275 if (WFLAGP(wwin, dont_move_off))
2276 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2277 req_width, req_height);
2278 if (resize) {
2279 if (req_width < MIN_WINDOW_SIZE)
2280 req_width = MIN_WINDOW_SIZE;
2281 if (req_height < MIN_WINDOW_SIZE)
2282 req_height = MIN_WINDOW_SIZE;
2284 /* If growing, resize inner part before frame,
2285 * if shrinking, resize frame before.
2286 * This will prevent the frame (that can have a different color)
2287 * to be exposed, causing flicker */
2288 if (req_height > wwin->frame->core->height
2289 || req_width > wwin->frame->core->width)
2290 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2292 if (wwin->flags.shaded) {
2293 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2294 req_width, wwin->frame->core->height);
2295 wwin->old_geometry.height = req_height;
2296 } else {
2297 int h;
2299 h = req_height + wwin->frame->top_width
2300 + wwin->frame->bottom_width;
2302 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
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 wwin->client.x = req_x;
2310 wwin->client.y = req_y + wwin->frame->top_width;
2311 wwin->client.width = req_width;
2312 wwin->client.height = req_height;
2313 } else {
2314 wwin->client.x = req_x;
2315 wwin->client.y = req_y + wwin->frame->top_width;
2317 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2319 wwin->frame_x = req_x;
2320 wwin->frame_y = req_y;
2321 if (!WFLAGP(wwin, no_border)) {
2322 wwin->client.x += FRAME_BORDER_WIDTH;
2323 wwin->client.y += FRAME_BORDER_WIDTH;
2326 #ifdef SHAPE
2327 if (wShapeSupported && wwin->flags.shaped && resize) {
2328 wWindowSetShape(wwin);
2330 #endif
2332 if (synth_notify)
2333 wWindowSynthConfigureNotify(wwin);
2334 XFlush(dpy);
2338 void
2339 wWindowMove(wwin, req_x, req_y)
2340 WWindow *wwin;
2341 int req_x, req_y; /* new position of the frame */
2343 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2344 int synth_notify = False;
2346 /* Send a synthetic ConfigureNotify event for every window movement. */
2347 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2348 synth_notify = True;
2350 #else
2351 /* A single synthetic ConfigureNotify event is sent at the end of
2352 * a completed (opaque) movement in moveres.c */
2353 #endif
2355 if (WFLAGP(wwin, dont_move_off))
2356 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2357 wwin->frame->core->width, wwin->frame->core->height);
2359 wwin->client.x = req_x;
2360 wwin->client.y = req_y + wwin->frame->top_width;
2361 if (!WFLAGP(wwin, no_border)) {
2362 wwin->client.x += FRAME_BORDER_WIDTH;
2363 wwin->client.y += FRAME_BORDER_WIDTH;
2366 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2368 wwin->frame_x = req_x;
2369 wwin->frame_y = req_y;
2371 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2372 if (synth_notify)
2373 wWindowSynthConfigureNotify(wwin);
2374 #endif
2378 void
2379 wWindowUpdateButtonImages(WWindow *wwin)
2381 WScreen *scr = wwin->screen_ptr;
2382 Pixmap pixmap, mask;
2383 WFrameWindow *fwin = wwin->frame;
2385 if (WFLAGP(wwin, no_titlebar))
2386 return;
2388 /* miniaturize button */
2390 if (!WFLAGP(wwin, no_miniaturize_button)) {
2391 if (wwin->wm_gnustep_attr
2392 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2393 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2395 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2396 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2397 } else {
2398 mask = None;
2401 if (fwin->lbutton_image
2402 && (fwin->lbutton_image->image != pixmap
2403 || fwin->lbutton_image->mask != mask)) {
2404 wPixmapDestroy(fwin->lbutton_image);
2405 fwin->lbutton_image = NULL;
2408 if (!fwin->lbutton_image) {
2409 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2410 fwin->lbutton_image->client_owned = 1;
2411 fwin->lbutton_image->client_owned_mask = 1;
2413 } else {
2414 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2415 wPixmapDestroy(fwin->lbutton_image);
2417 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2421 #ifdef XKB_BUTTON_HINT
2422 if (!WFLAGP(wwin, no_language_button)) {
2423 if (fwin->languagebutton_image &&
2424 !fwin->languagebutton_image->shared) {
2425 wPixmapDestroy(fwin->languagebutton_image);
2427 fwin->languagebutton_image =
2428 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2430 #endif
2432 /* close button */
2434 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2435 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2437 if (!WFLAGP(wwin, no_close_button)) {
2438 if (wwin->wm_gnustep_attr
2439 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2440 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2442 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2443 mask = wwin->wm_gnustep_attr->close_mask;
2444 else
2445 mask = None;
2447 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2448 || fwin->rbutton_image->mask != mask)) {
2449 wPixmapDestroy(fwin->rbutton_image);
2450 fwin->rbutton_image = NULL;
2453 if (!fwin->rbutton_image) {
2454 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2455 fwin->rbutton_image->client_owned = 1;
2456 fwin->rbutton_image->client_owned_mask = 1;
2459 } else if (WFLAGP(wwin, kill_close)) {
2461 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2462 wPixmapDestroy(fwin->rbutton_image);
2464 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2466 } else if (MGFLAGP(wwin, broken_close)) {
2468 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2469 wPixmapDestroy(fwin->rbutton_image);
2471 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2473 } else {
2475 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2476 wPixmapDestroy(fwin->rbutton_image);
2478 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2482 /* force buttons to be redrawn */
2483 fwin->flags.need_texture_change = 1;
2484 wFrameWindowPaint(fwin);
2489 *---------------------------------------------------------------------------
2490 * wWindowConfigureBorders--
2491 * Update window border configuration according to attribute flags.
2493 *---------------------------------------------------------------------------
2495 void
2496 wWindowConfigureBorders(WWindow *wwin)
2498 if (wwin->frame) {
2499 int flags;
2500 int newy, oldh;
2502 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2503 #ifdef XKB_BUTTON_HINT
2504 flags |= WFF_LANGUAGE_BUTTON;
2505 #endif
2506 if (!WFLAGP(wwin, no_titlebar))
2507 flags |= WFF_TITLEBAR;
2508 if (!WFLAGP(wwin, no_resizebar) && !WFLAGP(wwin, no_resizable))
2509 flags |= WFF_RESIZEBAR;
2510 if (!WFLAGP(wwin, no_border))
2511 flags |= WFF_BORDER;
2512 if (wwin->flags.shaded)
2513 flags |= WFF_IS_SHADED;
2515 oldh = wwin->frame->top_width;
2516 wFrameWindowUpdateBorders(wwin->frame, flags);
2517 if (oldh != wwin->frame->top_width) {
2518 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2520 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2521 wWindowConfigure(wwin, wwin->frame_x, newy,
2522 wwin->client.width, wwin->client.height);
2525 flags = 0;
2526 if (!WFLAGP(wwin, no_miniaturize_button)
2527 && wwin->frame->flags.hide_left_button)
2528 flags |= WFF_LEFT_BUTTON;
2530 #ifdef XKB_BUTTON_HINT
2531 if (!WFLAGP(wwin, no_language_button)
2532 && wwin->frame->flags.hide_language_button)
2533 flags |= WFF_LANGUAGE_BUTTON;
2534 #endif
2536 if (!WFLAGP(wwin, no_close_button)
2537 && wwin->frame->flags.hide_right_button)
2538 flags |= WFF_RIGHT_BUTTON;
2540 if (flags!=0) {
2541 wWindowUpdateButtonImages(wwin);
2542 wFrameWindowShowButton(wwin->frame, flags);
2545 flags = 0;
2546 if (WFLAGP(wwin, no_miniaturize_button)
2547 && !wwin->frame->flags.hide_left_button)
2548 flags |= WFF_LEFT_BUTTON;
2550 #ifdef XKB_BUTTON_HINT
2551 if (WFLAGP(wwin, no_language_button)
2552 && !wwin->frame->flags.hide_language_button)
2553 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 wFrameWindowHideButton(wwin->frame, flags);
2563 #ifdef SHAPE
2564 if (wShapeSupported && wwin->flags.shaped) {
2565 wWindowSetShape(wwin);
2567 #endif
2572 void
2573 wWindowSaveState(WWindow *wwin)
2575 CARD32 data[10];
2576 int i;
2578 memset(data, 0, sizeof(CARD32)*10);
2579 data[0] = wwin->frame->workspace;
2580 data[1] = wwin->flags.miniaturized;
2581 data[2] = wwin->flags.shaded;
2582 data[3] = wwin->flags.hidden;
2583 data[4] = wwin->flags.maximized;
2584 if (wwin->flags.maximized == 0) {
2585 data[5] = wwin->frame_x;
2586 data[6] = wwin->frame_y;
2587 data[7] = wwin->frame->core->width;
2588 data[8] = wwin->frame->core->height;
2589 } else {
2590 data[5] = wwin->old_geometry.x;
2591 data[6] = wwin->old_geometry.y;
2592 data[7] = wwin->old_geometry.width;
2593 data[8] = wwin->old_geometry.height;
2596 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2597 if (wwin->screen_ptr->shortcutWindows[i] &&
2598 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2599 data[9] |= 1<<i;
2601 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2602 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2603 (unsigned char *)data, 10);
2607 static int
2608 getSavedState(Window window, WSavedState **state)
2610 Atom type_ret;
2611 int fmt_ret;
2612 unsigned long nitems_ret;
2613 unsigned long bytes_after_ret;
2614 CARD32 *data;
2616 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2617 True, _XA_WINDOWMAKER_STATE,
2618 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2619 (unsigned char **)&data)!=Success || !data)
2620 return 0;
2622 *state = wmalloc(sizeof(WSavedState));
2624 (*state)->workspace = data[0];
2625 (*state)->miniaturized = data[1];
2626 (*state)->shaded = data[2];
2627 (*state)->hidden = data[3];
2628 (*state)->maximized = data[4];
2629 (*state)->x = data[5];
2630 (*state)->y = data[6];
2631 (*state)->w = data[7];
2632 (*state)->h = data[8];
2633 (*state)->window_shortcuts = data[9];
2635 XFree(data);
2637 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2638 return 1;
2639 else
2640 return 0;
2644 #ifdef SHAPE
2645 void
2646 wWindowClearShape(WWindow *wwin)
2648 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2649 0, wwin->frame->top_width, None, ShapeSet);
2650 XFlush(dpy);
2653 void
2654 wWindowSetShape(WWindow *wwin)
2656 XRectangle rect[2];
2657 int count;
2658 #ifdef OPTIMIZE_SHAPE
2659 XRectangle *rects;
2660 XRectangle *urec;
2661 int ordering;
2663 /* only shape is the client's */
2664 if (WFLAGP(wwin, no_titlebar) && WFLAGP(wwin, no_resizebar)) {
2665 goto alt_code;
2668 /* Get array of rectangles describing the shape mask */
2669 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2670 &count, &ordering);
2671 if (!rects) {
2672 goto alt_code;
2675 urec = malloc(sizeof(XRectangle)*(count+2));
2676 if (!urec) {
2677 XFree(rects);
2678 goto alt_code;
2681 /* insert our decoration rectangles in the rect list */
2682 memcpy(urec, rects, sizeof(XRectangle)*count);
2683 XFree(rects);
2685 if (!WFLAGP(wwin, no_titlebar)) {
2686 urec[count].x = -1;
2687 urec[count].y = -1 - wwin->frame->top_width;
2688 urec[count].width = wwin->frame->core->width + 2;
2689 urec[count].height = wwin->frame->top_width + 1;
2690 count++;
2692 if (!WFLAGP(wwin, no_resizebar)) {
2693 urec[count].x = -1;
2694 urec[count].y = wwin->frame->core->height
2695 - wwin->frame->bottom_width - wwin->frame->top_width;
2696 urec[count].width = wwin->frame->core->width + 2;
2697 urec[count].height = wwin->frame->bottom_width + 1;
2698 count++;
2701 /* shape our frame window */
2702 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2703 0, wwin->frame->top_width, urec, count,
2704 ShapeSet, Unsorted);
2705 XFlush(dpy);
2706 wfree(urec);
2707 return;
2709 alt_code:
2710 #endif /* OPTIMIZE_SHAPE */
2711 count = 0;
2712 if (!WFLAGP(wwin, no_titlebar)) {
2713 rect[count].x = -1;
2714 rect[count].y = -1;
2715 rect[count].width = wwin->frame->core->width + 2;
2716 rect[count].height = wwin->frame->top_width + 1;
2717 count++;
2719 if (!WFLAGP(wwin, no_resizebar)) {
2720 rect[count].x = -1;
2721 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2722 rect[count].width = wwin->frame->core->width + 2;
2723 rect[count].height = wwin->frame->bottom_width + 1;
2724 count++;
2726 if (count > 0) {
2727 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2728 0, 0, rect, count, ShapeSet, Unsorted);
2730 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2731 0, wwin->frame->top_width, wwin->client_win,
2732 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2733 XFlush(dpy);
2735 #endif /* SHAPE */
2737 /* ====================================================================== */
2739 static FocusMode
2740 getFocusMode(WWindow *wwin)
2742 FocusMode mode;
2744 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2745 if (wwin->wm_hints->input == True) {
2746 if (wwin->protocols.TAKE_FOCUS)
2747 mode = WFM_LOCALLY_ACTIVE;
2748 else
2749 mode = WFM_PASSIVE;
2750 } else {
2751 if (wwin->protocols.TAKE_FOCUS)
2752 mode = WFM_GLOBALLY_ACTIVE;
2753 else
2754 mode = WFM_NO_INPUT;
2756 } else {
2757 mode = WFM_PASSIVE;
2759 return mode;
2763 void
2764 wWindowSetKeyGrabs(WWindow *wwin)
2766 int i;
2767 WShortKey *key;
2769 for (i=0; i<WKBD_LAST; i++) {
2770 key = &wKeyBindings[i];
2772 if (key->keycode==0)
2773 continue;
2774 if (key->modifier!=AnyModifier) {
2775 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2776 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2777 #ifdef NUMLOCK_HACK
2778 /* Also grab all modifier combinations possible that include,
2779 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2780 * work even if the NumLock/ScrollLock key is on.
2782 wHackedGrabKey(key->keycode, key->modifier,
2783 wwin->frame->core->window, True, GrabModeAsync,
2784 GrabModeAsync);
2785 #endif
2787 XGrabKey(dpy, key->keycode, key->modifier,
2788 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2791 #ifndef LITE
2792 wRootMenuBindShortcuts(wwin->frame->core->window);
2793 #endif
2798 void
2799 wWindowResetMouseGrabs(WWindow *wwin)
2801 /* Mouse grabs can't be done on the client window because of
2802 * ICCCM and because clients that try to do the same will crash.
2804 * But there is a problem wich makes tbar buttons of unfocused
2805 * windows not usable as the click goes to the frame window instead
2806 * of the button itself. Must figure a way to fix that.
2809 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2811 if (!WFLAGP(wwin, no_bind_mouse)) {
2812 /* grabs for Meta+drag */
2813 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2814 True, ButtonPressMask, GrabModeSync,
2815 GrabModeAsync, None, None);
2818 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2819 && !wwin->flags.is_gnustep) {
2820 /* the passive grabs to focus the window */
2821 /* if (wPreferences.focus_mode == WKF_CLICK) */
2822 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2823 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2824 None, None);
2826 XFlush(dpy);
2830 void
2831 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2833 if (attr->flags & GSExtraFlagsAttr) {
2834 if (MGFLAGP(wwin, broken_close) !=
2835 (attr->extra_flags & GSDocumentEditedFlag)) {
2836 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2837 wWindowUpdateButtonImages(wwin);
2843 WMagicNumber
2844 wWindowAddSavedState(char *instance, char *class, char *command,
2845 pid_t pid, WSavedState *state)
2847 WWindowState *wstate;
2849 wstate = malloc(sizeof(WWindowState));
2850 if (!wstate)
2851 return 0;
2853 memset(wstate, 0, sizeof(WWindowState));
2854 wstate->pid = pid;
2855 if (instance)
2856 wstate->instance = wstrdup(instance);
2857 if (class)
2858 wstate->class = wstrdup(class);
2859 if (command)
2860 wstate->command = wstrdup(command);
2861 wstate->state = state;
2863 wstate->next = windowState;
2864 windowState = wstate;
2866 #ifdef DEBUG
2867 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2868 class, command);
2869 #endif
2871 return wstate;
2875 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2878 WMagicNumber
2879 wWindowGetSavedState(Window win)
2881 char *instance, *class, *command=NULL;
2882 WWindowState *wstate = windowState;
2883 char **argv;
2884 int argc;
2886 if (!wstate)
2887 return NULL;
2889 if (XGetCommand(dpy, win, &argv, &argc)) {
2890 if (argc > 0)
2891 command = wtokenjoin(argv, argc);
2892 XFreeStringList(argv);
2894 if (!command)
2895 return NULL;
2897 if (PropGetWMClass(win, &class, &instance)) {
2898 while (wstate) {
2899 if (SAME(instance, wstate->instance) &&
2900 SAME(class, wstate->class) &&
2901 SAME(command, wstate->command)) {
2902 break;
2904 wstate = wstate->next;
2906 } else {
2907 wstate = NULL;
2910 #ifdef DEBUG
2911 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2912 class, command);
2913 #endif
2915 if (command) wfree(command);
2916 if (instance) XFree(instance);
2917 if (class) XFree(class);
2919 return wstate;
2923 void
2924 wWindowDeleteSavedState(WMagicNumber id)
2926 WWindowState *tmp, *wstate=(WWindowState*)id;
2928 if (!wstate || !windowState)
2929 return;
2931 tmp = windowState;
2932 if (tmp==wstate) {
2933 windowState = wstate->next;
2934 #ifdef DEBUG
2935 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2936 wstate, wstate->instance, wstate->class, wstate->command);
2937 #endif
2938 if (wstate->instance) wfree(wstate->instance);
2939 if (wstate->class) wfree(wstate->class);
2940 if (wstate->command) wfree(wstate->command);
2941 wfree(wstate->state);
2942 wfree(wstate);
2943 } else {
2944 while (tmp->next) {
2945 if (tmp->next==wstate) {
2946 tmp->next=wstate->next;
2947 #ifdef DEBUG
2948 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2949 wstate, wstate->instance, wstate->class, wstate->command);
2950 #endif
2951 if (wstate->instance) wfree(wstate->instance);
2952 if (wstate->class) wfree(wstate->class);
2953 if (wstate->command) wfree(wstate->command);
2954 wfree(wstate->state);
2955 wfree(wstate);
2956 break;
2958 tmp = tmp->next;
2964 void
2965 wWindowDeleteSavedStatesForPID(pid_t pid)
2967 WWindowState *tmp, *wstate;
2969 if (!windowState)
2970 return;
2972 tmp = windowState;
2973 if (tmp->pid == pid) {
2974 wstate = windowState;
2975 windowState = tmp->next;
2976 #ifdef DEBUG
2977 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2978 wstate, wstate->instance, wstate->class, wstate->command);
2979 #endif
2980 if (wstate->instance) wfree(wstate->instance);
2981 if (wstate->class) wfree(wstate->class);
2982 if (wstate->command) wfree(wstate->command);
2983 wfree(wstate->state);
2984 wfree(wstate);
2985 } else {
2986 while (tmp->next) {
2987 if (tmp->next->pid==pid) {
2988 wstate = tmp->next;
2989 tmp->next = wstate->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 break;
3001 tmp = tmp->next;
3007 void
3008 wWindowSetOmnipresent(WWindow *wwin, Bool flag)
3010 wwin->flags.omnipresent = flag;
3012 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
3016 /* ====================================================================== */
3018 static void
3019 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3021 WWindow *wwin = data;
3023 #ifndef NUMLOCK_HACK
3024 if ((event->xbutton.state & ValidModMask)
3025 != (event->xbutton.state & ~LockMask)) {
3026 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3027 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3029 #endif
3031 event->xbutton.state &= ValidModMask;
3033 CloseWindowMenu(wwin->screen_ptr);
3035 if (wPreferences.focus_mode==WKF_CLICK
3036 && !(event->xbutton.state&ControlMask)
3037 && !WFLAGP(wwin, no_focusable)) {
3038 wSetFocusTo(wwin->screen_ptr, wwin);
3041 if (event->xbutton.button == Button1)
3042 wRaiseFrame(wwin->frame->core);
3044 if (event->xbutton.window != wwin->frame->resizebar->window) {
3045 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
3046 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3047 GrabModeAsync, GrabModeAsync, None,
3048 None, CurrentTime)!=GrabSuccess) {
3049 #ifdef DEBUG0
3050 wwarning("pointer grab failed for window move");
3051 #endif
3052 return;
3056 if (event->xbutton.state & MOD_MASK) {
3057 /* move the window */
3058 wMouseMoveWindow(wwin, event);
3059 XUngrabPointer(dpy, CurrentTime);
3060 } else {
3061 wMouseResizeWindow(wwin, event);
3062 XUngrabPointer(dpy, CurrentTime);
3068 static void
3069 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
3071 WWindow *wwin = data;
3073 event->xbutton.state &= ValidModMask;
3075 if (event->xbutton.button==Button1) {
3076 if (event->xbutton.state == 0) {
3077 if (!WFLAGP(wwin, no_shadeable)) {
3078 /* shade window */
3079 if (wwin->flags.shaded)
3080 wUnshadeWindow(wwin);
3081 else
3082 wShadeWindow(wwin);
3084 } else {
3085 int dir = 0;
3087 if (event->xbutton.state & ControlMask)
3088 dir |= MAX_VERTICAL;
3090 if (event->xbutton.state & ShiftMask) {
3091 dir |= MAX_HORIZONTAL;
3092 if (!(event->xbutton.state & ControlMask))
3093 wSelectWindow(wwin, !wwin->flags.selected);
3096 /* maximize window */
3097 if (dir!=0 && !WFLAGP(wwin, no_resizable)) {
3098 int ndir = dir ^ wwin->flags.maximized;
3099 if (wwin->flags.maximized != 0)
3100 wUnmaximizeWindow(wwin);
3101 if (ndir != 0)
3102 wMaximizeWindow(wwin, ndir);
3105 } else if (event->xbutton.button==Button3) {
3106 if (event->xbutton.state & MOD_MASK) {
3107 wHideOtherApplications(wwin);
3109 } else if (event->xbutton.button==Button2) {
3110 wSelectWindow(wwin, !wwin->flags.selected);
3111 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
3112 wShadeWindow(wwin);
3113 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
3114 wUnshadeWindow(wwin);
3119 static void
3120 frameMouseDown(WObjDescriptor *desc, XEvent *event)
3122 WWindow *wwin = desc->parent;
3124 event->xbutton.state &= ValidModMask;
3126 CloseWindowMenu(wwin->screen_ptr);
3128 if (/*wPreferences.focus_mode==WKF_CLICK
3129 &&*/ !(event->xbutton.state&ControlMask)
3130 && !WFLAGP(wwin, no_focusable)) {
3131 wSetFocusTo(wwin->screen_ptr, wwin);
3133 if (event->xbutton.button == Button1) {
3134 wRaiseFrame(wwin->frame->core);
3137 if (event->xbutton.state & MOD_MASK) {
3138 /* move the window */
3139 if (XGrabPointer(dpy, wwin->client_win, False,
3140 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3141 GrabModeAsync, GrabModeAsync, None,
3142 None, CurrentTime)!=GrabSuccess) {
3143 #ifdef DEBUG0
3144 wwarning("pointer grab failed for window move");
3145 #endif
3146 return;
3148 if (event->xbutton.button == Button3 && !WFLAGP(wwin, no_resizable))
3149 wMouseResizeWindow(wwin, event);
3150 else
3151 wMouseMoveWindow(wwin, event);
3152 XUngrabPointer(dpy, CurrentTime);
3157 static void
3158 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3160 WWindow *wwin = (WWindow*)data;
3162 #ifndef NUMLOCK_HACK
3163 if ((event->xbutton.state & ValidModMask)
3164 != (event->xbutton.state & ~LockMask)) {
3165 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3166 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3168 #endif
3169 event->xbutton.state &= ValidModMask;
3171 CloseWindowMenu(wwin->screen_ptr);
3173 if (wPreferences.focus_mode==WKF_CLICK
3174 && !(event->xbutton.state&ControlMask)
3175 && !WFLAGP(wwin, no_focusable)) {
3176 wSetFocusTo(wwin->screen_ptr, wwin);
3179 if (event->xbutton.button == Button1
3180 || event->xbutton.button == Button2) {
3182 if (event->xbutton.button == Button1) {
3183 if (event->xbutton.state & MOD_MASK) {
3184 wLowerFrame(wwin->frame->core);
3185 } else {
3186 wRaiseFrame(wwin->frame->core);
3189 if ((event->xbutton.state & ShiftMask)
3190 && !(event->xbutton.state & ControlMask)) {
3191 wSelectWindow(wwin, !wwin->flags.selected);
3192 return;
3194 if (event->xbutton.window != wwin->frame->titlebar->window
3195 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3196 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3197 GrabModeAsync, GrabModeAsync, None,
3198 None, CurrentTime)!=GrabSuccess) {
3199 #ifdef DEBUG0
3200 wwarning("pointer grab failed for window move");
3201 #endif
3202 return;
3205 /* move the window */
3206 wMouseMoveWindow(wwin, event);
3208 XUngrabPointer(dpy, CurrentTime);
3209 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
3210 && !wwin->flags.internal_window
3211 && !WCHECK_STATE(WSTATE_MODAL)) {
3212 WObjDescriptor *desc;
3214 if (event->xbutton.window != wwin->frame->titlebar->window
3215 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3216 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3217 GrabModeAsync, GrabModeAsync, None,
3218 None, CurrentTime)!=GrabSuccess) {
3219 #ifdef DEBUG0
3220 wwarning("pointer grab failed for window move");
3221 #endif
3222 return;
3225 OpenWindowMenu(wwin, event->xbutton.x_root,
3226 wwin->frame_y+wwin->frame->top_width, False);
3228 /* allow drag select */
3229 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3230 event->xany.send_event = True;
3231 (*desc->handle_mousedown)(desc, event);
3233 XUngrabPointer(dpy, CurrentTime);
3239 static void
3240 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3242 WWindow *wwin = data;
3244 event->xbutton.state &= ValidModMask;
3246 CloseWindowMenu(wwin->screen_ptr);
3248 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3249 return;
3251 /* if control-click, kill the client */
3252 if (event->xbutton.state & ControlMask) {
3253 wClientKill(wwin);
3254 } else {
3255 #ifdef OLWM_HINTS
3256 if (wwin->flags.olwm_push_pin_out) {
3258 wwin->flags.olwm_push_pin_out = 0;
3260 wOLWMChangePushpinState(wwin, True);
3262 wFrameWindowUpdatePushButton(wwin->frame, False);
3264 return;
3266 #endif
3267 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
3268 /* send delete message */
3269 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3275 static void
3276 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3278 WWindow *wwin = data;
3280 CloseWindowMenu(wwin->screen_ptr);
3282 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3283 return;
3285 /* send delete message */
3286 if (wwin->protocols.DELETE_WINDOW) {
3287 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3288 } else {
3289 wClientKill(wwin);
3294 #ifdef XKB_BUTTON_HINT
3295 static void
3296 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3298 WWindow *wwin = data;
3299 WFrameWindow *fwin = wwin->frame;
3300 WScreen *scr = fwin->screen_ptr;
3301 XkbStateRec staterec;
3302 int tl;
3304 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3305 return;
3306 tl = wwin->frame->languagemode;
3307 wwin->frame->languagemode = wwin->frame->last_languagemode;
3308 wwin->frame->last_languagemode = tl;
3309 wSetFocusTo(scr, wwin);
3310 wwin->frame->languagebutton_image =
3311 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3312 wwin->frame->languagemode];
3313 wFrameWindowUpdateLanguageButton(wwin->frame);
3314 if (event->xbutton.button == Button3)
3315 return;
3316 wRaiseFrame(fwin->core);
3318 #endif
3321 static void
3322 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3324 WWindow *wwin = data;
3326 event->xbutton.state &= ValidModMask;
3328 CloseWindowMenu(wwin->screen_ptr);
3330 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3331 return;
3333 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3334 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3335 LastTimestamp);
3336 } else {
3337 WApplication *wapp;
3338 if ((event->xbutton.state & ControlMask) ||
3339 (event->xbutton.button == Button3)) {
3341 wapp = wApplicationOf(wwin->main_window);
3342 if (wapp && !WFLAGP(wwin, no_appicon))
3343 wHideApplication(wapp);
3344 } else if (event->xbutton.state==0) {
3345 wIconifyWindow(wwin);