- Removed support for legacy systems: OpenLook, KDE-2.x, Gnome-1.x
[wmaker-crm.git] / src / window.c
blob96bb71878e5a0f50f22d7b1aa216b2110da4b843
1 /* window.c - client window managing stuffs
3 * Window Maker window manager
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 #include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
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 NETWM_HINTS
63 # include "wmspec.h"
64 #endif
66 /****** Global Variables ******/
68 extern WShortKey wKeyBindings[WKBD_LAST];
70 #ifdef SHAPE
71 extern Bool wShapeSupported;
72 #endif
74 /* contexts */
75 extern XContext wWinContext;
77 /* cursors */
78 extern Cursor wCursor[WCUR_LAST];
80 /* protocol atoms */
81 extern Atom _XA_WM_DELETE_WINDOW;
82 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
84 extern Atom _XA_WINDOWMAKER_STATE;
86 extern WPreferences wPreferences;
88 #define MOD_MASK wPreferences.modifier_mask
90 extern Time LastTimestamp;
92 /* superfluous... */
93 extern void DoWindowBirth(WWindow*);
97 /***** Local Stuff *****/
100 static WWindowState *windowState=NULL;
104 /* local functions */
105 static FocusMode getFocusMode(WWindow *wwin);
107 static int getSavedState(Window window, WSavedState **state);
109 static void setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints);
111 /* event handlers */
114 /* frame window (during window grabs) */
115 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
117 /* close button */
118 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
119 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
121 /* iconify button */
122 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
124 #ifdef XKB_BUTTON_HINT
125 static void windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event);
126 #endif
128 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
129 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
131 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
134 /****** Notification Observers ******/
136 static void
137 appearanceObserver(void *self, WMNotification *notif)
139 WWindow *wwin = (WWindow*)self;
140 int flags = (int)WMGetNotificationClientData(notif);
142 if (!wwin->frame || (!wwin->frame->titlebar && !wwin->frame->resizebar))
143 return;
145 if (flags & WFontSettings) {
146 wWindowConfigureBorders(wwin);
147 if(wwin->flags.shaded) {
148 wFrameWindowResize(wwin->frame, wwin->frame->core->width,
149 wwin->frame->top_width - 1);
151 wwin->client.y = wwin->frame_y - wwin->client.height
152 + wwin->frame->top_width;
153 wWindowSynthConfigureNotify(wwin);
156 if (flags & WTextureSettings) {
157 wwin->frame->flags.need_texture_remake = 1;
159 if (flags & (WTextureSettings | WColorSettings)) {
160 if (wwin->frame->titlebar)
161 XClearWindow(dpy, wwin->frame->titlebar->window);
163 wFrameWindowPaint(wwin->frame);
167 /************************************/
169 WWindow*
170 wWindowFor(Window window)
172 WObjDescriptor *desc;
174 if (window==None)
175 return NULL;
177 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
178 return NULL;
180 if (desc->parent_type==WCLASS_WINDOW)
181 return desc->parent;
182 else if (desc->parent_type==WCLASS_FRAME) {
183 WFrameWindow *frame = (WFrameWindow*)desc->parent;
184 if (frame->flags.is_client_window_frame)
185 return frame->child;
188 return NULL;
192 WWindow*
193 wWindowCreate()
195 WWindow *wwin;
197 wwin = wmalloc(sizeof(WWindow));
198 wretain(wwin);
200 memset(wwin, 0, sizeof(WWindow));
202 wwin->client_descriptor.handle_mousedown = frameMouseDown;
203 wwin->client_descriptor.parent = wwin;
204 wwin->client_descriptor.self = wwin;
205 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
207 return wwin;
211 void
212 wWindowDestroy(WWindow *wwin)
214 int i;
216 if (wwin->screen_ptr->cmap_window == wwin) {
217 wwin->screen_ptr->cmap_window = NULL;
220 WMRemoveNotificationObserver(wwin);
222 wwin->flags.destroyed = 1;
224 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
225 if (!wwin->screen_ptr->shortcutWindows[i])
226 continue;
228 WMRemoveFromArray(wwin->screen_ptr->shortcutWindows[i], wwin);
230 if (!WMGetArrayItemCount(wwin->screen_ptr->shortcutWindows[i])) {
231 WMFreeArray(wwin->screen_ptr->shortcutWindows[i]);
232 wwin->screen_ptr->shortcutWindows[i] = NULL;
236 if (wwin->fake_group && wwin->fake_group->retainCount>0) {
237 wwin->fake_group->retainCount--;
238 if (wwin->fake_group->retainCount==0 && wwin->fake_group->leader!=None) {
239 XDestroyWindow(dpy, wwin->fake_group->leader);
240 wwin->fake_group->leader = None;
241 wwin->fake_group->origLeader = None;
242 XFlush(dpy);
246 if (wwin->normal_hints)
247 XFree(wwin->normal_hints);
249 if (wwin->wm_hints)
250 XFree(wwin->wm_hints);
252 if (wwin->wm_instance)
253 XFree(wwin->wm_instance);
255 if (wwin->wm_class)
256 XFree(wwin->wm_class);
258 if (wwin->wm_gnustep_attr)
259 wfree(wwin->wm_gnustep_attr);
261 if (wwin->cmap_windows)
262 XFree(wwin->cmap_windows);
264 XDeleteContext(dpy, wwin->client_win, wWinContext);
266 if (wwin->frame)
267 wFrameWindowDestroy(wwin->frame);
269 if (wwin->icon) {
270 RemoveFromStackList(wwin->icon->core);
271 wIconDestroy(wwin->icon);
272 if (wPreferences.auto_arrange_icons)
273 wArrangeIcons(wwin->screen_ptr, True);
276 #ifdef NETWM_HINTS
277 if (wwin->net_icon_image)
278 RReleaseImage(wwin->net_icon_image);
279 #endif
281 wrelease(wwin);
285 static void
286 setupGNUstepHints(WWindow *wwin, GNUstepWMAttributes *gs_hints)
288 if (gs_hints->flags & GSWindowStyleAttr) {
289 if (gs_hints->window_style == WMBorderlessWindowMask) {
290 wwin->client_flags.no_border = 1;
291 wwin->client_flags.no_titlebar = 1;
292 wwin->client_flags.no_closable = 1;
293 wwin->client_flags.no_miniaturizable = 1;
294 wwin->client_flags.no_resizable = 1;
295 wwin->client_flags.no_close_button = 1;
296 wwin->client_flags.no_miniaturize_button = 1;
297 wwin->client_flags.no_resizebar = 1;
298 } else {
299 wwin->client_flags.no_close_button =
300 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
302 wwin->client_flags.no_closable =
303 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
305 wwin->client_flags.no_miniaturize_button =
306 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
308 wwin->client_flags.no_miniaturizable =
309 wwin->client_flags.no_miniaturize_button;
311 wwin->client_flags.no_resizebar =
312 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
314 wwin->client_flags.no_resizable = wwin->client_flags.no_resizebar;
316 /* these attributes supposedly imply in the existence
317 * of a titlebar */
318 if (gs_hints->window_style & (WMResizableWindowMask|
319 WMClosableWindowMask|
320 WMMiniaturizableWindowMask)) {
321 wwin->client_flags.no_titlebar = 0;
322 } else {
323 wwin->client_flags.no_titlebar =
324 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
328 } else {
329 /* setup the defaults */
330 wwin->client_flags.no_border = 0;
331 wwin->client_flags.no_titlebar = 0;
332 wwin->client_flags.no_closable = 0;
333 wwin->client_flags.no_miniaturizable = 0;
334 wwin->client_flags.no_resizable = 0;
335 wwin->client_flags.no_close_button = 0;
336 wwin->client_flags.no_miniaturize_button = 0;
337 wwin->client_flags.no_resizebar = 0;
339 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
340 wwin->client_flags.no_appicon = 1;
345 void
346 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags,
347 WWindowAttributes *mask)
349 if (wflags->no_appicon && mask->no_appicon)
350 wflags->emulate_appicon = 0;
352 if (wwin->main_window!=None) {
353 WApplication *wapp = wApplicationOf(wwin->main_window);
354 if (wapp && !wapp->flags.emulated)
355 wflags->emulate_appicon = 0;
358 if (wwin->transient_for!=None
359 && wwin->transient_for!=wwin->screen_ptr->root_win)
360 wflags->emulate_appicon = 0;
362 if (wflags->sunken && mask->sunken && wflags->floating && mask->floating)
363 wflags->sunken = 0;
368 void
369 wWindowSetupInitialAttributes(WWindow *wwin, int *level, int *workspace)
371 WScreen *scr = wwin->screen_ptr;
373 /* sets global default stuff */
374 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
375 &wwin->client_flags, NULL, True);
377 * Decoration setting is done in this precedence (lower to higher)
378 * - use global default in the resource database
379 * - guess some settings
380 * - use GNUstep/external window attributes
381 * - set hints specified for the app in the resource DB
384 WSETUFLAG(wwin, broken_close, 0);
386 if (wwin->protocols.DELETE_WINDOW)
387 WSETUFLAG(wwin, kill_close, 0);
388 else
389 WSETUFLAG(wwin, kill_close, 1);
391 /* transients can't be iconified or maximized */
392 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
393 WSETUFLAG(wwin, no_miniaturizable, 1);
394 WSETUFLAG(wwin, no_miniaturize_button, 1);
397 /* if the window can't be resized, remove the resizebar */
398 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
399 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
400 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
401 WSETUFLAG(wwin, no_resizable, 1);
402 WSETUFLAG(wwin, no_resizebar, 1);
405 /* set GNUstep window attributes */
406 if (wwin->wm_gnustep_attr) {
407 setupGNUstepHints(wwin, wwin->wm_gnustep_attr);
409 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
411 *level = wwin->wm_gnustep_attr->window_level;
413 * INT_MIN is the only illegal window level.
415 if (*level == INT_MIN)
416 *level = INT_MIN + 1;
417 } else {
418 /* setup defaults */
419 *level = WMNormalLevel;
421 } else {
422 int tmp_workspace = -1;
423 int tmp_level = INT_MIN; /* INT_MIN is never used by the window levels */
424 Bool check;
426 check = False;
428 #ifdef MWM_HINTS
429 wMWMCheckClientHints(wwin);
430 #endif /* MWM_HINTS */
432 #ifdef NETWM_HINTS
433 if (!check)
434 check = wNETWMCheckClientHints(wwin, &tmp_level, &tmp_workspace);
435 #endif
437 /* window levels are between INT_MIN+1 and INT_MAX, so if we still
438 * have INT_MIN that means that no window level was requested. -Dan
440 if (tmp_level == INT_MIN) {
441 if (WFLAGP(wwin, floating))
442 *level = WMFloatingLevel;
443 else if (WFLAGP(wwin, sunken))
444 *level = WMSunkenLevel;
445 else
446 *level = WMNormalLevel;
447 } else {
448 *level = tmp_level;
451 if (wwin->transient_for!=None && wwin->transient_for != scr->root_win) {
452 WWindow * transientOwner = wWindowFor(wwin->transient_for);
453 if (transientOwner) {
454 int ownerLevel = transientOwner->frame->core->stacking->window_level;
455 if (ownerLevel > *level) *level = ownerLevel;
459 if (tmp_workspace >= 0) {
460 *workspace = tmp_workspace % scr->workspace_count;
465 * Set attributes specified only for that window/class.
466 * This might do duplicate work with the 1st wDefaultFillAttributes().
468 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
469 &wwin->user_flags, &wwin->defined_user_flags,
470 False);
472 * Sanity checks for attributes that depend on other attributes
474 if (wwin->user_flags.no_appicon && wwin->defined_user_flags.no_appicon)
475 wwin->user_flags.emulate_appicon = 0;
477 if (wwin->main_window!=None) {
478 WApplication *wapp = wApplicationOf(wwin->main_window);
479 if (wapp && !wapp->flags.emulated)
480 wwin->user_flags.emulate_appicon = 0;
483 if (wwin->transient_for!=None
484 && wwin->transient_for!=wwin->screen_ptr->root_win)
485 wwin->user_flags.emulate_appicon = 0;
487 if (wwin->user_flags.sunken && wwin->defined_user_flags.sunken
488 && wwin->user_flags.floating && wwin->defined_user_flags.floating)
489 wwin->user_flags.sunken = 0;
491 WSETUFLAG(wwin, no_shadeable, WFLAGP(wwin, no_titlebar));
494 /* windows that have takefocus=False shouldn't take focus at all */
495 if (wwin->focus_mode == WFM_NO_INPUT) {
496 wwin->client_flags.no_focusable = 1;
503 Bool
504 wWindowCanReceiveFocus(WWindow *wwin)
506 if (!wwin->flags.mapped && (!wwin->flags.shaded || wwin->flags.hidden))
507 return False;
508 if (WFLAGP(wwin, no_focusable) || wwin->flags.miniaturized)
509 return False;
510 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
511 return False;
513 return True;
517 Bool
518 wWindowObscuresWindow(WWindow *wwin, WWindow *obscured)
520 int w1, h1, w2, h2;
522 w1 = wwin->frame->core->width;
523 h1 = wwin->frame->core->height;
524 w2 = obscured->frame->core->width;
525 h2 = obscured->frame->core->height;
527 if (!IS_OMNIPRESENT(wwin) && !IS_OMNIPRESENT(obscured)
528 && wwin->frame->workspace != obscured->frame->workspace)
529 return False;
531 if (wwin->frame_x + w1 < obscured->frame_x
532 || wwin->frame_y + h1 < obscured->frame_y
533 || wwin->frame_x > obscured->frame_x + w2
534 || wwin->frame_y > obscured->frame_y + h2) {
535 return False;
538 return True;
542 static void
543 fixLeaderProperties(WWindow *wwin)
545 XClassHint *classHint;
546 XWMHints *hints, *clientHints;
547 Window leaders[2], window;
548 char **argv, *command;
549 int argc, i, pid;
550 Bool haveCommand;
552 classHint = XAllocClassHint();
553 clientHints = XGetWMHints(dpy, wwin->client_win);
554 pid = wNETWMGetPidForWindow(wwin->client_win);
555 if (pid > 0) {
556 haveCommand = GetCommandForPid(pid, &argv, &argc);
557 } else {
558 haveCommand = False;
561 leaders[0] = wwin->client_leader;
562 leaders[1] = wwin->group_id;
564 if (haveCommand) {
565 command = GetCommandForWindow(wwin->client_win);
566 if (command) {
567 /* command already set. nothing to do. */
568 wfree(command);
569 } else {
570 XSetCommand(dpy, wwin->client_win, argv, argc);
574 for (i=0; i<2; i++) {
575 window = leaders[i];
576 if (window) {
577 if (XGetClassHint(dpy, window, classHint) == 0) {
578 classHint->res_name = wwin->wm_instance;
579 classHint->res_class = wwin->wm_class;
580 XSetClassHint(dpy, window, classHint);
582 hints = XGetWMHints(dpy, window);
583 if (hints) {
584 XFree(hints);
585 } else if (clientHints) {
586 /* set window group leader to self */
587 clientHints->window_group = window;
588 clientHints->flags |= WindowGroupHint;
589 XSetWMHints(dpy, window, clientHints);
592 if (haveCommand) {
593 command = GetCommandForWindow(window);
594 if (command) {
595 /* command already set. nothing to do. */
596 wfree(command);
597 } else {
598 XSetCommand(dpy, window, argv, argc);
604 XFree(classHint);
605 if (clientHints) {
606 XFree(clientHints);
608 if (haveCommand) {
609 wfree(argv);
614 static Window
615 createFakeWindowGroupLeader(WScreen *scr, Window win, char *instance, char *class)
617 XClassHint *classHint;
618 XWMHints *hints;
619 Window leader;
620 int argc;
621 char **argv;
623 leader = XCreateSimpleWindow(dpy, scr->root_win, 10, 10, 10, 10, 0, 0, 0);
624 /* set class hint */
625 classHint = XAllocClassHint();
626 classHint->res_name = instance;
627 classHint->res_class = class;
628 XSetClassHint(dpy, leader, classHint);
629 XFree(classHint);
631 /* inherit these from the original leader if available */
632 hints = XGetWMHints(dpy, win);
633 if (!hints) {
634 hints = XAllocWMHints();
635 hints->flags = 0;
637 /* set window group leader to self */
638 hints->window_group = leader;
639 hints->flags |= WindowGroupHint;
640 XSetWMHints(dpy, leader, hints);
641 XFree(hints);
643 if (XGetCommand(dpy, win, &argv, &argc)!=0 && argc > 0) {
644 XSetCommand(dpy, leader, argv, argc);
645 XFreeStringList(argv);
648 return leader;
652 static int
653 matchIdentifier(void *item, void *cdata)
655 return (strcmp(((WFakeGroupLeader*)item)->identifier, (char*)cdata)==0);
660 *----------------------------------------------------------------
661 * wManageWindow--
662 * reparents the window and allocates a descriptor for it.
663 * Window manager hints and other hints are fetched to configure
664 * the window decoration attributes and others. User preferences
665 * for the window are used if available, to configure window
666 * decorations and some behaviour.
667 * If in startup, windows that are override redirect,
668 * unmapped and never were managed and are Withdrawn are not
669 * managed.
671 * Returns:
672 * the new window descriptor
674 * Side effects:
675 * The window is reparented and appropriate notification
676 * is done to the client. Input mask for the window is setup.
677 * The window descriptor is also associated with various window
678 * contexts and inserted in the head of the window list.
679 * Event handler contexts are associated for some objects
680 * (buttons, titlebar and resizebar)
682 *----------------------------------------------------------------
684 WWindow*
685 wManageWindow(WScreen *scr, Window window)
687 WWindow *wwin;
688 int x, y;
689 unsigned width, height;
690 XWindowAttributes wattribs;
691 XSetWindowAttributes attribs;
692 WWindowState *win_state;
693 WWindow *transientOwner = NULL;
694 int window_level;
695 int wm_state;
696 int foo;
697 int workspace = -1;
698 char *title;
699 Bool withdraw = False;
700 Bool raise = False;
702 /* mutex. */
703 /* XGrabServer(dpy); */
704 XSync(dpy, False);
705 /* make sure the window is still there */
706 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
707 XUngrabServer(dpy);
708 return NULL;
711 /* if it's an override-redirect, ignore it */
712 if (wattribs.override_redirect) {
713 XUngrabServer(dpy);
714 return NULL;
717 wm_state = PropGetWindowState(window);
719 /* if it's startup and the window is unmapped, don't manage it */
720 if (scr->flags.startup && wm_state < 0 && wattribs.map_state==IsUnmapped) {
721 XUngrabServer(dpy);
722 return NULL;
725 if (!wFetchName(dpy, window, &title)) {
726 title = NULL;
729 wwin = wWindowCreate();
731 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
733 #ifdef DEBUG
734 printf("managing window %x\n", (unsigned)window);
735 #endif
737 #ifdef SHAPE
738 if (wShapeSupported) {
739 int junk;
740 unsigned int ujunk;
741 int b_shaped;
743 XShapeSelectInput(dpy, window, ShapeNotifyMask);
744 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
745 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
746 wwin->flags.shaped = b_shaped;
748 #endif
751 *--------------------------------------------------
753 * Get hints and other information in properties
755 *--------------------------------------------------
757 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
759 /* setup descriptor */
760 wwin->client_win = window;
761 wwin->screen_ptr = scr;
763 wwin->old_border_width = wattribs.border_width;
765 wwin->event_mask = CLIENT_EVENTS;
766 attribs.event_mask = CLIENT_EVENTS;
767 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
768 attribs.save_under = False;
769 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
770 |CWSaveUnder, &attribs);
771 XSetWindowBorderWidth(dpy, window, 0);
773 /* get hints from GNUstep app */
774 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
775 wwin->flags.is_gnustep = 1;
777 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
778 wwin->wm_gnustep_attr = NULL;
781 wwin->client_leader = PropGetClientLeader(window);
782 if (wwin->client_leader!=None)
783 wwin->main_window = wwin->client_leader;
785 wwin->wm_hints = XGetWMHints(dpy, window);
787 if (wwin->wm_hints) {
788 if (wwin->wm_hints->flags & StateHint) {
790 if (wwin->wm_hints->initial_state == IconicState) {
792 wwin->flags.miniaturized = 1;
794 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
796 withdraw = True;
800 if (wwin->wm_hints->flags & WindowGroupHint) {
801 wwin->group_id = wwin->wm_hints->window_group;
802 /* window_group has priority over CLIENT_LEADER */
803 wwin->main_window = wwin->group_id;
804 } else {
805 wwin->group_id = None;
808 if (wwin->wm_hints->flags & UrgencyHint)
809 wwin->flags.urgent = 1;
810 } else {
811 wwin->group_id = None;
814 PropGetProtocols(window, &wwin->protocols);
816 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
817 wwin->transient_for = None;
818 } else {
819 if (wwin->transient_for==None || wwin->transient_for==window) {
820 wwin->transient_for = scr->root_win;
821 } else {
822 transientOwner = wWindowFor(wwin->transient_for);
823 if (transientOwner && transientOwner->main_window!=None) {
824 wwin->main_window = transientOwner->main_window;
825 } /*else {
826 wwin->main_window = None;
831 /* guess the focus mode */
832 wwin->focus_mode = getFocusMode(wwin);
834 /* get geometry stuff */
835 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
837 /* printf("wManageWindow: %d %d %d %d\n", x, y, width, height);*/
839 /* get colormap windows */
840 GetColormapWindows(wwin);
843 *--------------------------------------------------
845 * Setup the decoration/window attributes and
846 * geometry
848 *--------------------------------------------------
851 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
853 /* Make broken apps behave as a nice app. */
854 if (WFLAGP(wwin, emulate_appicon)) {
855 wwin->main_window = wwin->client_win;
858 fixLeaderProperties(wwin);
860 wwin->orig_main_window = wwin->main_window;
862 if (wwin->flags.is_gnustep) {
863 WSETUFLAG(wwin, shared_appicon, 0);
866 if (wwin->main_window) {
867 extern Atom _XA_WINDOWMAKER_MENU;
868 XTextProperty text_prop;
870 if (XGetTextProperty(dpy, wwin->main_window, &text_prop,
871 _XA_WINDOWMAKER_MENU)) {
872 WSETUFLAG(wwin, shared_appicon, 0);
876 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
877 char *buffer, *instance, *class;
878 WFakeGroupLeader *fPtr;
879 int index;
881 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
883 // only enter here if PropGetWMClass() succeds
884 PropGetWMClass(wwin->main_window, &class, &instance);
885 buffer = StrConcatDot(instance, class);
887 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void*)buffer);
888 if (index != WANotFound) {
889 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
890 if (fPtr->retainCount == 0) {
891 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
892 instance, class);
894 fPtr->retainCount++;
895 #undef method2
896 if (fPtr->origLeader==None) {
897 #ifdef method2
898 if (ADEQUATE(wwin->group_id)) {
899 fPtr->retainCount++;
900 fPtr->origLeader = wwin->group_id;
901 } else if (ADEQUATE(wwin->client_leader)) {
902 fPtr->retainCount++;
903 fPtr->origLeader = wwin->client_leader;
904 } else if (ADEQUATE(wwin->main_window)) {
905 fPtr->retainCount++;
906 fPtr->origLeader = wwin->main_window;
908 #else
909 if (ADEQUATE(wwin->main_window)) {
910 fPtr->retainCount++;
911 fPtr->origLeader = wwin->main_window;
913 #endif
915 wwin->fake_group = fPtr;
916 /*wwin->group_id = fPtr->leader;*/
917 wwin->main_window = fPtr->leader;
918 wfree(buffer);
919 } else {
920 fPtr = (WFakeGroupLeader*)wmalloc(sizeof(WFakeGroupLeader));
922 fPtr->identifier = buffer;
923 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
924 instance, class);
925 fPtr->origLeader = None;
926 fPtr->retainCount = 1;
928 WMAddToArray(scr->fakeGroupLeaders, fPtr);
930 #ifdef method2
931 if (ADEQUATE(wwin->group_id)) {
932 fPtr->retainCount++;
933 fPtr->origLeader = wwin->group_id;
934 } else if (ADEQUATE(wwin->client_leader)) {
935 fPtr->retainCount++;
936 fPtr->origLeader = wwin->client_leader;
937 } else if (ADEQUATE(wwin->main_window)) {
938 fPtr->retainCount++;
939 fPtr->origLeader = wwin->main_window;
941 #else
942 if (ADEQUATE(wwin->main_window)) {
943 fPtr->retainCount++;
944 fPtr->origLeader = wwin->main_window;
946 #endif
947 wwin->fake_group = fPtr;
948 /*wwin->group_id = fPtr->leader;*/
949 wwin->main_window = fPtr->leader;
951 if (instance)
952 XFree(instance);
953 if (class)
954 XFree(class);
956 #undef method2
957 #undef ADEQUATE
961 *------------------------------------------------------------
963 * Setup the initial state of the window
965 *------------------------------------------------------------
968 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
969 wwin->flags.miniaturized = 1;
972 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin)) {
973 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
976 #ifdef NETWM_HINTS
977 wNETWMCheckInitialClientState(wwin);
978 #endif
980 /* apply previous state if it exists and we're in startup */
981 if (scr->flags.startup && wm_state >= 0) {
983 if (wm_state == IconicState) {
985 wwin->flags.miniaturized = 1;
987 } else if (wm_state == WithdrawnState) {
989 withdraw = True;
993 /* if there is a saved state (from file), restore it */
994 win_state = NULL;
995 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
996 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
997 } else {
998 win_state = (WWindowState*)wWindowGetSavedState(window);
1000 if (win_state && !withdraw) {
1002 if (win_state->state->hidden>0)
1003 wwin->flags.hidden = win_state->state->hidden;
1005 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
1006 wwin->flags.shaded = win_state->state->shaded;
1008 if (win_state->state->miniaturized>0 &&
1009 !WFLAGP(wwin, no_miniaturizable)) {
1010 wwin->flags.miniaturized = win_state->state->miniaturized;
1013 if (!IS_OMNIPRESENT(wwin)) {
1014 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
1015 wwin->wm_class);
1016 if (w < 0 || w >= scr->workspace_count) {
1017 workspace = win_state->state->workspace;
1018 if (workspace >= scr->workspace_count)
1019 workspace = scr->current_workspace;
1020 } else {
1021 workspace = w;
1023 } else {
1024 workspace = scr->current_workspace;
1028 /* if we're restarting, restore saved state (from hints).
1029 * This will overwrite previous */
1031 WSavedState *wstate;
1033 if (getSavedState(window, &wstate)) {
1034 wwin->flags.shaded = wstate->shaded;
1035 wwin->flags.hidden = wstate->hidden;
1036 wwin->flags.miniaturized = wstate->miniaturized;
1037 wwin->flags.maximized = wstate->maximized;
1038 if (wwin->flags.maximized) {
1039 wwin->old_geometry.x = wstate->x;
1040 wwin->old_geometry.y = wstate->y;
1041 wwin->old_geometry.width = wstate->w;
1042 wwin->old_geometry.height = wstate->h;
1045 workspace = wstate->workspace;
1046 } else {
1047 wstate = NULL;
1050 /* restore window shortcut */
1051 if (wstate != NULL || win_state != NULL) {
1052 unsigned mask = 0;
1054 if (win_state != NULL)
1055 mask = win_state->state->window_shortcuts;
1057 if (wstate != NULL && mask == 0)
1058 mask = wstate->window_shortcuts;
1060 if (mask > 0) {
1061 int i;
1063 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1064 if (mask & (1<<i)) {
1065 if (!scr->shortcutWindows[i])
1066 scr->shortcutWindows[i] = WMCreateArray(4);
1068 WMAddToArray(scr->shortcutWindows[i], wwin);
1073 if (wstate != NULL) {
1074 wfree(wstate);
1078 /* don't let transients start miniaturized if their owners are not */
1079 if (transientOwner && !transientOwner->flags.miniaturized
1080 && wwin->flags.miniaturized && !withdraw) {
1081 wwin->flags.miniaturized = 0;
1082 if (wwin->wm_hints)
1083 wwin->wm_hints->initial_state = NormalState;
1086 /* set workspace on which the window starts */
1087 if (workspace >= 0) {
1088 if (workspace > scr->workspace_count-1) {
1089 workspace = workspace % scr->workspace_count;
1091 } else {
1092 int w;
1094 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1096 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1098 workspace = w;
1100 } else {
1101 if (wPreferences.open_transients_with_parent && transientOwner) {
1103 workspace = transientOwner->frame->workspace;
1105 } else {
1107 workspace = scr->current_workspace;
1112 /* setup window geometry */
1113 if (win_state && win_state->state->w > 0) {
1114 width = win_state->state->w;
1115 height = win_state->state->h;
1117 wWindowConstrainSize(wwin, &width, &height);
1119 /* do not ask for window placement if the window is
1120 * transient, during startup, if the initial workspace is another one
1121 * or if the window wants to start iconic.
1122 * If geometry was saved, restore it. */
1124 Bool dontBring = False;
1126 if (win_state && win_state->state->w > 0) {
1127 x = win_state->state->x;
1128 y = win_state->state->y;
1129 } else if ((wwin->transient_for==None
1130 || wPreferences.window_placement!=WPM_MANUAL)
1131 && !scr->flags.startup
1132 && workspace == scr->current_workspace
1133 && !wwin->flags.miniaturized
1134 && !wwin->flags.maximized
1135 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
1137 if (transientOwner && transientOwner->flags.mapped) {
1138 int offs = WMAX(20, 2*transientOwner->frame->top_width);
1139 WMRect rect;
1140 int head;
1142 x = transientOwner->frame_x +
1143 abs((transientOwner->frame->core->width - width)/2) + offs;
1144 y = transientOwner->frame_y +
1145 abs((transientOwner->frame->core->height - height)/3) + offs;
1148 * limit transient windows to be inside their parent's head
1150 rect.pos.x = transientOwner->frame_x;
1151 rect.pos.y = transientOwner->frame_y;
1152 rect.size.width = transientOwner->frame->core->width;
1153 rect.size.height = transientOwner->frame->core->height;
1155 head = wGetHeadForRect(scr, rect);
1156 rect = wGetRectForHead(scr, head);
1158 if (x < rect.pos.x)
1159 x = rect.pos.x;
1160 else if (x + width > rect.pos.x + rect.size.width)
1161 x = rect.pos.x + rect.size.width - width;
1163 if (y < rect.pos.y)
1164 y = rect.pos.y;
1165 else if (y + height > rect.pos.y + rect.size.height)
1166 y = rect.pos.y + rect.size.height - height;
1168 } else {
1169 PlaceWindow(wwin, &x, &y, width, height);
1171 if (wPreferences.window_placement == WPM_MANUAL) {
1172 dontBring = True;
1174 } else if (scr->xine_info.count &&
1175 (wwin->normal_hints->flags & PPosition)) {
1176 int head, flags;
1177 WMRect rect;
1178 int reposition = 0;
1181 * Make spash screens come out in the center of a head
1182 * trouble is that most splashies never get here
1183 * they are managed trough atoms but god knows where.
1184 * Dan, do you know ? -peter
1186 * Most of them are not managed, they have set
1187 * OverrideRedirect, which means we can't do anything about
1188 * them. -alfredo
1190 #if 0
1191 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width)/2);
1192 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height)/2);
1194 if ((unsigned)(x + (width - scr->scr_width)/2 + 10) < 20 &&
1195 (unsigned)(y + (height - scr->scr_height)/2 + 10) < 20) {
1197 reposition = 1;
1199 } else
1200 #endif
1203 * xinerama checks for: across head and dead space
1205 rect.pos.x = x;
1206 rect.pos.y = y;
1207 rect.size.width = width;
1208 rect.size.height = height;
1210 head = wGetRectPlacementInfo(scr, rect, &flags);
1212 if (flags & XFLAG_DEAD)
1213 reposition = 1;
1215 if (flags & XFLAG_MULTIPLE)
1216 reposition = 2;
1219 switch (reposition) {
1220 case 1:
1221 head = wGetHeadForPointerLocation(scr);
1222 rect = wGetRectForHead(scr, head);
1224 x = rect.pos.x + (x * rect.size.width)/scr->scr_width;
1225 y = rect.pos.y + (y * rect.size.height)/scr->scr_height;
1226 break;
1228 case 2:
1229 rect = wGetRectForHead(scr, head);
1231 if (x < rect.pos.x)
1232 x = rect.pos.x;
1233 else if (x + width > rect.pos.x + rect.size.width)
1234 x = rect.pos.x + rect.size.width - width;
1236 if (y < rect.pos.y)
1237 y = rect.pos.y;
1238 else if (y + height > rect.pos.y + rect.size.height)
1239 y = rect.pos.y + rect.size.height - height;
1241 break;
1243 default:
1244 break;
1248 if (WFLAGP(wwin, dont_move_off) && dontBring)
1249 wScreenBringInside(scr, &x, &y, width, height);
1252 #ifdef NETWM_HINTS
1253 wNETWMPositionSplash(wwin, &x, &y, width, height);
1254 #endif
1256 if (wwin->flags.urgent) {
1257 if (!IS_OMNIPRESENT(wwin))
1258 wwin->flags.omnipresent ^= 1;
1262 *--------------------------------------------------
1264 * Create frame, borders and do reparenting
1266 *--------------------------------------------------
1268 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1269 #ifdef XKB_BUTTON_HINT
1270 if (wPreferences.modelock)
1271 foo |= WFF_LANGUAGE_BUTTON;
1272 #endif
1273 if (HAS_TITLEBAR(wwin))
1274 foo |= WFF_TITLEBAR;
1275 if (HAS_RESIZEBAR(wwin))
1276 foo |= WFF_RESIZEBAR;
1277 if (HAS_BORDER(wwin))
1278 foo |= WFF_BORDER;
1280 wwin->frame = wFrameWindowCreate(scr, window_level,
1281 x, y, width, height,
1282 &wPreferences.window_title_clearance, foo,
1283 scr->window_title_texture,
1284 scr->resizebar_texture,
1285 scr->window_title_color,
1286 &scr->title_font);
1288 wwin->frame->flags.is_client_window_frame = 1;
1289 wwin->frame->flags.justification = wPreferences.title_justification;
1291 /* setup button images */
1292 wWindowUpdateButtonImages(wwin);
1294 /* hide unused buttons */
1295 foo = 0;
1296 if (WFLAGP(wwin, no_close_button))
1297 foo |= WFF_RIGHT_BUTTON;
1298 if (WFLAGP(wwin, no_miniaturize_button))
1299 foo |= WFF_LEFT_BUTTON;
1300 #ifdef XKB_BUTTON_HINT
1301 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1302 foo |= WFF_LANGUAGE_BUTTON;
1303 #endif
1304 if (foo!=0)
1305 wFrameWindowHideButton(wwin->frame, foo);
1307 wwin->frame->child = wwin;
1309 wwin->frame->workspace = workspace;
1311 wwin->frame->on_click_left = windowIconifyClick;
1312 #ifdef XKB_BUTTON_HINT
1313 if (wPreferences.modelock)
1314 wwin->frame->on_click_language = windowLanguageClick;
1315 #endif
1317 wwin->frame->on_click_right = windowCloseClick;
1318 wwin->frame->on_dblclick_right = windowCloseDblClick;
1320 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1321 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1323 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1326 XSelectInput(dpy, wwin->client_win,
1327 wwin->event_mask & ~StructureNotifyMask);
1329 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1330 0, wwin->frame->top_width);
1332 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1336 int gx, gy;
1338 wClientGetGravityOffsets(wwin, &gx, &gy);
1340 /* if gravity is to the south, account for the border sizes */
1341 if (gy > 0)
1342 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1346 * wWindowConfigure() will init the client window's size
1347 * (wwin->client.{width,height}) and all other geometry
1348 * related variables (frame_x,frame_y)
1350 wWindowConfigure(wwin, x, y, width, height);
1352 /* to make sure the window receives it's new position after reparenting */
1353 wWindowSynthConfigureNotify(wwin);
1356 *--------------------------------------------------
1358 * Setup descriptors and save window to internal
1359 * lists
1361 *--------------------------------------------------
1364 if (wwin->main_window!=None) {
1365 WApplication *app;
1366 WWindow *leader;
1368 /* Leader windows do not necessary set themselves as leaders.
1369 * If this is the case, point the leader of this window to
1370 * itself */
1371 leader = wWindowFor(wwin->main_window);
1372 if (leader && leader->main_window==None) {
1373 leader->main_window = leader->client_win;
1375 app = wApplicationCreate(wwin);
1376 if (app) {
1377 app->last_workspace = workspace;
1380 * Do application specific stuff, like setting application
1381 * wide attributes.
1384 if (wwin->flags.hidden) {
1385 /* if the window was set to hidden because it was hidden
1386 * in a previous incarnation and that state was restored */
1387 app->flags.hidden = 1;
1388 } else if (app->flags.hidden) {
1389 if (WFLAGP(app->main_window_desc, start_hidden)) {
1390 wwin->flags.hidden = 1;
1391 } else {
1392 wUnhideApplication(app, False, False);
1393 raise = True;
1399 /* setup the frame descriptor */
1400 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1401 wwin->frame->core->descriptor.parent = wwin;
1402 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1404 /* don't let windows go away if we die */
1405 XAddToSaveSet(dpy, window);
1407 XLowerWindow(dpy, window);
1409 /* if window is in this workspace and should be mapped, then map it */
1410 if (!wwin->flags.miniaturized
1411 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1412 && !wwin->flags.hidden && !withdraw) {
1414 /* The following "if" is to avoid crashing of clients that expect
1415 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1416 * after the return from this function.
1418 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1419 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1420 } else {
1421 wClientSetState(wwin, NormalState, None);
1424 #if 0
1425 /* if not auto focus, then map the window under the currently
1426 * focused window */
1427 #define _WIDTH(w) (w)->frame->core->width
1428 #define _HEIGHT(w) (w)->frame->core->height
1429 if (!wPreferences.auto_focus && scr->focused_window
1430 && !scr->flags.startup && !transientOwner
1431 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1432 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1433 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1434 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1435 || wwin->flags.maximized)) {
1436 MoveInStackListUnder(scr->focused_window->frame->core,
1437 wwin->frame->core);
1439 #undef _WIDTH
1440 #undef _HEIGHT
1442 #endif
1444 if (wPreferences.superfluous && !wPreferences.no_animations
1445 && !scr->flags.startup &&
1446 (wwin->transient_for==None || wwin->transient_for==scr->root_win)
1448 * The brain damaged idiotic non-click to focus modes will
1449 * have trouble with this because:
1451 * 1. window is created and mapped by the client
1452 * 2. window is mapped by wmaker in small size
1453 * 3. window is animated to grow to normal size
1454 * 4. this function returns to normal event loop
1455 * 5. eventually, the EnterNotify event that would trigger
1456 * the window focusing (if the mouse is over that window)
1457 * will be processed by wmaker.
1458 * But since this event will be rather delayed
1459 * (step 3 has a large delay) the time when the event ocurred
1460 * and when it is processed, the client that owns that window
1461 * will reject the XSetInputFocus() for it.
1463 && (wPreferences.focus_mode==WKF_CLICK
1464 || wPreferences.auto_focus)) {
1465 DoWindowBirth(wwin);
1468 wWindowMap(wwin);
1471 /* setup stacking descriptor */
1472 if (transientOwner) {
1473 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1474 } else {
1475 wwin->frame->core->stacking->child_of = NULL;
1479 if (!scr->focused_window) {
1480 /* first window on the list */
1481 wwin->next = NULL;
1482 wwin->prev = NULL;
1483 scr->focused_window = wwin;
1484 } else {
1485 WWindow *tmp;
1487 /* add window at beginning of focus window list */
1488 tmp = scr->focused_window;
1489 while (tmp->prev)
1490 tmp = tmp->prev;
1491 tmp->prev = wwin;
1492 wwin->next = tmp;
1493 wwin->prev = NULL;
1496 /* raise is set to true if we un-hid the app when this window was born.
1497 * we raise, else old windows of this app will be above this new one. */
1498 if (raise) {
1499 wRaiseFrame(wwin->frame->core);
1502 /* Update name must come after WApplication stuff is done */
1503 wWindowUpdateName(wwin, title);
1504 if (title)
1505 XFree(title);
1507 XUngrabServer(dpy);
1510 *--------------------------------------------------
1512 * Final preparations before window is ready to go
1514 *--------------------------------------------------
1517 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1520 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1521 && !wwin->flags.hidden) {
1522 if (((transientOwner && transientOwner->flags.focused)
1523 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1524 wSetFocusTo(scr, wwin);
1526 wWindowResetMouseGrabs(wwin);
1528 if (!WFLAGP(wwin, no_bind_keys)) {
1529 wWindowSetKeyGrabs(wwin);
1533 WMPostNotificationName(WMNManaged, wwin, NULL);
1536 wColormapInstallForWindow(scr, scr->cmap_window);
1540 *------------------------------------------------------------
1541 * Setup Notification Observers
1542 *------------------------------------------------------------
1544 WMAddNotificationObserver(appearanceObserver, wwin,
1545 WNWindowAppearanceSettingsChanged, wwin);
1549 *--------------------------------------------------
1551 * Cleanup temporary stuff
1553 *--------------------------------------------------
1556 if (win_state)
1557 wWindowDeleteSavedState(win_state);
1559 /* If the window must be withdrawed, then do it now.
1560 * Must do some optimization, 'though */
1561 if (withdraw) {
1562 wwin->flags.mapped = 0;
1563 wClientSetState(wwin, WithdrawnState, None);
1564 wUnmanageWindow(wwin, True, False);
1565 wwin = NULL;
1568 return wwin;
1575 WWindow*
1576 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1577 char *title, int x, int y, int width, int height)
1579 WWindow *wwin;
1580 int foo;
1582 wwin = wWindowCreate();
1584 WMAddNotificationObserver(appearanceObserver, wwin,
1585 WNWindowAppearanceSettingsChanged, wwin);
1587 wwin->flags.internal_window = 1;
1589 WSETUFLAG(wwin, omnipresent, 1);
1590 WSETUFLAG(wwin, no_shadeable, 1);
1591 WSETUFLAG(wwin, no_resizable, 1);
1592 WSETUFLAG(wwin, no_miniaturizable, 1);
1594 wwin->focus_mode = WFM_PASSIVE;
1596 wwin->client_win = window;
1597 wwin->screen_ptr = scr;
1599 wwin->transient_for = owner;
1601 wwin->client.x = x;
1602 wwin->client.y = y;
1603 wwin->client.width = width;
1604 wwin->client.height = height;
1606 wwin->frame_x = wwin->client.x;
1607 wwin->frame_y = wwin->client.y;
1610 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1611 foo |= WFF_TITLEBAR;
1612 #ifdef XKB_BUTTON_HINT
1613 foo |= WFF_LANGUAGE_BUTTON;
1614 #endif
1616 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1617 wwin->frame_x, wwin->frame_y,
1618 width, height,
1619 &wPreferences.window_title_clearance, foo,
1620 scr->window_title_texture,
1621 scr->resizebar_texture,
1622 scr->window_title_color,
1623 &scr->title_font);
1625 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1627 wwin->frame->flags.is_client_window_frame = 1;
1628 wwin->frame->flags.justification = wPreferences.title_justification;
1630 wFrameWindowChangeTitle(wwin->frame, title);
1632 /* setup button images */
1633 wWindowUpdateButtonImages(wwin);
1635 /* hide buttons */
1636 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1638 wwin->frame->child = wwin;
1640 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1642 #ifdef XKB_BUTTON_HINT
1643 if (wPreferences.modelock)
1644 wwin->frame->on_click_language = windowLanguageClick;
1645 #endif
1647 wwin->frame->on_click_right = windowCloseClick;
1649 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1650 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1652 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1654 wwin->client.y += wwin->frame->top_width;
1655 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1656 0, wwin->frame->top_width);
1658 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1659 wwin->client.width, wwin->client.height);
1661 /* setup the frame descriptor */
1662 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1663 wwin->frame->core->descriptor.parent = wwin;
1664 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1667 XLowerWindow(dpy, window);
1668 XMapSubwindows(dpy, wwin->frame->core->window);
1670 /* setup stacking descriptor */
1671 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
1672 WWindow *tmp;
1673 tmp = wWindowFor(wwin->transient_for);
1674 if (tmp)
1675 wwin->frame->core->stacking->child_of = tmp->frame->core;
1676 } else {
1677 wwin->frame->core->stacking->child_of = NULL;
1681 if (!scr->focused_window) {
1682 /* first window on the list */
1683 wwin->next = NULL;
1684 wwin->prev = NULL;
1685 scr->focused_window = wwin;
1686 } else {
1687 WWindow *tmp;
1689 /* add window at beginning of focus window list */
1690 tmp = scr->focused_window;
1691 while (tmp->prev)
1692 tmp = tmp->prev;
1693 tmp->prev = wwin;
1694 wwin->next = tmp;
1695 wwin->prev = NULL;
1698 if (wwin->flags.is_gnustep == 0)
1699 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1701 /* if (wPreferences.auto_focus)*/
1702 wSetFocusTo(scr, wwin);
1704 wWindowResetMouseGrabs(wwin);
1706 wWindowSetKeyGrabs(wwin);
1708 return wwin;
1713 *----------------------------------------------------------------------
1714 * wUnmanageWindow--
1715 * Removes the frame window from a window and destroys all data
1716 * related to it. The window will be reparented back to the root window
1717 * if restore is True.
1719 * Side effects:
1720 * Everything related to the window is destroyed and the window
1721 * is removed from the window lists. Focus is set to the previous on the
1722 * window list.
1723 *----------------------------------------------------------------------
1725 void
1726 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1728 WCoreWindow *frame = wwin->frame->core;
1729 WWindow *owner = NULL;
1730 WWindow *newFocusedWindow = NULL;
1731 int wasFocused;
1732 WScreen *scr = wwin->screen_ptr;
1735 /* First close attribute editor window if open */
1736 if (wwin->flags.inspector_open) {
1737 wCloseInspectorForWindow(wwin);
1740 /* Close window menu if it's open for this window */
1741 if (wwin->flags.menu_open_for_me) {
1742 CloseWindowMenu(scr);
1745 if (!destroyed) {
1746 if (!wwin->flags.internal_window)
1747 XRemoveFromSaveSet(dpy, wwin->client_win);
1749 XSelectInput(dpy, wwin->client_win, NoEventMask);
1751 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1752 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1755 XUnmapWindow(dpy, frame->window);
1757 XUnmapWindow(dpy, wwin->client_win);
1759 /* deselect window */
1760 wSelectWindow(wwin, False);
1762 /* remove all pending events on window */
1763 /* I think this only matters for autoraise */
1764 if (wPreferences.raise_delay)
1765 WMDeleteTimerWithClientData(wwin->frame->core);
1767 XFlush(dpy);
1769 /* reparent the window back to the root */
1770 if (restore)
1771 wClientRestore(wwin);
1773 if (wwin->transient_for!=scr->root_win) {
1774 owner = wWindowFor(wwin->transient_for);
1775 if (owner) {
1776 if (!owner->flags.semi_focused) {
1777 owner = NULL;
1778 } else {
1779 owner->flags.semi_focused = 0;
1784 wasFocused = wwin->flags.focused;
1786 /* remove from window focus list */
1787 if (!wwin->prev && !wwin->next) {
1788 /* was the only window */
1789 scr->focused_window = NULL;
1790 newFocusedWindow = NULL;
1791 } else {
1792 WWindow *tmp;
1794 if (wwin->prev)
1795 wwin->prev->next = wwin->next;
1796 if (wwin->next)
1797 wwin->next->prev = wwin->prev;
1798 else {
1799 scr->focused_window = wwin->prev;
1800 scr->focused_window->next = NULL;
1803 if (wPreferences.focus_mode==WKF_CLICK) {
1805 /* if in click to focus mode and the window
1806 * was a transient, focus the owner window
1808 tmp = NULL;
1809 if (wPreferences.focus_mode==WKF_CLICK) {
1810 tmp = wWindowFor(wwin->transient_for);
1811 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1812 tmp = NULL;
1815 /* otherwise, focus the next one in the focus list */
1816 if (!tmp) {
1817 tmp = scr->focused_window;
1818 while (tmp) { /* look for one in the window list first */
1819 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1820 && (tmp->flags.mapped || tmp->flags.shaded))
1821 break;
1822 tmp = tmp->prev;
1824 if (!tmp) { /* if unsuccessful, choose any focusable window */
1825 tmp = scr->focused_window;
1826 while (tmp) {
1827 if (!WFLAGP(tmp, no_focusable)
1828 && (tmp->flags.mapped || tmp->flags.shaded))
1829 break;
1830 tmp = tmp->prev;
1835 newFocusedWindow = tmp;
1837 } else if (wPreferences.focus_mode==WKF_SLOPPY) {
1838 unsigned int mask;
1839 int foo;
1840 Window bar, win;
1842 /* This is to let the root window get the keyboard input
1843 * if Sloppy focus mode and no other window get focus.
1844 * This way keybindings will not freeze.
1846 tmp = NULL;
1847 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1848 &foo, &foo, &foo, &foo, &mask))
1849 tmp = wWindowFor(win);
1850 if (tmp == wwin)
1851 tmp = NULL;
1852 newFocusedWindow = tmp;
1853 } else {
1854 newFocusedWindow = NULL;
1858 if (!wwin->flags.internal_window) {
1859 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1862 #ifdef DEBUG
1863 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1864 (unsigned)frame->window);
1865 #endif
1867 if (wasFocused) {
1868 if (newFocusedWindow != owner && owner) {
1869 if (wwin->flags.is_gnustep == 0)
1870 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1872 wSetFocusTo(scr, newFocusedWindow);
1874 wWindowDestroy(wwin);
1875 XFlush(dpy);
1879 void
1880 wWindowMap(WWindow *wwin)
1882 XMapWindow(dpy, wwin->frame->core->window);
1883 if (!wwin->flags.shaded) {
1884 /* window will be remapped when getting MapNotify */
1885 XSelectInput(dpy, wwin->client_win,
1886 wwin->event_mask & ~StructureNotifyMask);
1887 XMapWindow(dpy, wwin->client_win);
1888 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1890 wwin->flags.mapped = 1;
1895 void
1896 wWindowUnmap(WWindow *wwin)
1898 wwin->flags.mapped = 0;
1900 /* prevent window withdrawal when getting UnmapNotify */
1901 XSelectInput(dpy, wwin->client_win,
1902 wwin->event_mask & ~StructureNotifyMask);
1903 XUnmapWindow(dpy, wwin->client_win);
1904 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1906 XUnmapWindow(dpy, wwin->frame->core->window);
1911 void
1912 wWindowFocus(WWindow *wwin, WWindow *owin)
1914 WWindow *nowner;
1915 WWindow *oowner;
1917 #ifdef KEEP_XKB_LOCK_STATUS
1918 if (wPreferences.modelock) {
1919 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1921 #endif /* KEEP_XKB_LOCK_STATUS */
1923 wwin->flags.semi_focused = 0;
1925 if (wwin->flags.is_gnustep == 0)
1926 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1928 wwin->flags.focused = 1;
1930 wWindowResetMouseGrabs(wwin);
1932 WMPostNotificationName(WMNChangedFocus, wwin, (void*)True);
1934 if (owin == wwin || !owin)
1935 return;
1937 nowner = wWindowFor(wwin->transient_for);
1939 /* new window is a transient for the old window */
1940 if (nowner == owin) {
1941 owin->flags.semi_focused = 1;
1942 wWindowUnfocus(nowner);
1943 return;
1946 oowner = wWindowFor(owin->transient_for);
1948 /* new window is owner of old window */
1949 if (wwin == oowner) {
1950 wWindowUnfocus(owin);
1951 return;
1954 if (!nowner) {
1955 wWindowUnfocus(owin);
1956 return;
1959 /* new window has same owner of old window */
1960 if (oowner == nowner) {
1961 /* prevent unfocusing of owner */
1962 oowner->flags.semi_focused = 0;
1963 wWindowUnfocus(owin);
1964 oowner->flags.semi_focused = 1;
1966 return;
1969 /* nowner != NULL && oowner != nowner */
1970 nowner->flags.semi_focused = 1;
1971 wWindowUnfocus(nowner);
1972 wWindowUnfocus(owin);
1976 void
1977 wWindowUnfocus(WWindow *wwin)
1979 CloseWindowMenu(wwin->screen_ptr);
1981 if (wwin->flags.is_gnustep == 0)
1982 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1983 ? WS_PFOCUSED : WS_UNFOCUSED);
1985 if (wwin->transient_for!=None
1986 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1987 WWindow *owner;
1988 owner = wWindowFor(wwin->transient_for);
1989 if (owner && owner->flags.semi_focused) {
1990 owner->flags.semi_focused = 0;
1991 if (owner->flags.mapped || owner->flags.shaded) {
1992 wWindowUnfocus(owner);
1993 wFrameWindowPaint(owner->frame);
1997 wwin->flags.focused = 0;
1999 wWindowResetMouseGrabs(wwin);
2001 WMPostNotificationName(WMNChangedFocus, wwin, (void*)False);
2005 void
2006 wWindowUpdateName(WWindow *wwin, char *newTitle)
2008 char *title;
2010 if (!wwin->frame)
2011 return;
2013 wwin->flags.wm_name_changed = 1;
2015 if (!newTitle) {
2016 /* the hint was removed */
2017 title = DEF_WINDOW_TITLE;
2018 } else {
2019 title = newTitle;
2022 if (wFrameWindowChangeTitle(wwin->frame, title)) {
2023 WMPostNotificationName(WMNChangedName, wwin, NULL);
2030 *----------------------------------------------------------------------
2032 * wWindowConstrainSize--
2033 * Constrains size for the client window, taking the maximal size,
2034 * window resize increments and other size hints into account.
2036 * Returns:
2037 * The closest size to what was given that the client window can
2038 * have.
2040 *----------------------------------------------------------------------
2042 void
2043 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
2045 int width = *nwidth;
2046 int height = *nheight;
2047 int winc = 1;
2048 int hinc = 1;
2049 int minW = 1, minH = 1;
2050 int maxW = wwin->screen_ptr->scr_width*2;
2051 int maxH = wwin->screen_ptr->scr_height*2;
2052 int minAX = -1, minAY = -1;
2053 int maxAX = -1, maxAY = -1;
2054 int baseW = 0;
2055 int baseH = 0;
2057 if (wwin->normal_hints) {
2058 winc = wwin->normal_hints->width_inc;
2059 hinc = wwin->normal_hints->height_inc;
2060 minW = wwin->normal_hints->min_width;
2061 minH = wwin->normal_hints->min_height;
2062 maxW = wwin->normal_hints->max_width;
2063 maxH = wwin->normal_hints->max_height;
2064 if (wwin->normal_hints->flags & PAspect) {
2065 minAX = wwin->normal_hints->min_aspect.x;
2066 minAY = wwin->normal_hints->min_aspect.y;
2067 maxAX = wwin->normal_hints->max_aspect.x;
2068 maxAY = wwin->normal_hints->max_aspect.y;
2071 baseW = wwin->normal_hints->base_width;
2072 baseH = wwin->normal_hints->base_height;
2075 if (width < minW)
2076 width = minW;
2077 if (height < minH)
2078 height = minH;
2080 if (width > maxW)
2081 width = maxW;
2082 if (height > maxH)
2083 height = maxH;
2085 /* aspect ratio code borrowed from olwm */
2086 if (minAX > 0) {
2087 /* adjust max aspect ratio */
2088 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
2089 if (maxAX > maxAY) {
2090 height = (width * maxAY) / maxAX;
2091 if (height > maxH) {
2092 height = maxH;
2093 width = (height * maxAX) / maxAY;
2095 } else {
2096 width = (height * maxAX) / maxAY;
2097 if (width > maxW) {
2098 width = maxW;
2099 height = (width * maxAY) / maxAX;
2104 /* adjust min aspect ratio */
2105 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
2106 if (minAX > minAY) {
2107 height = (width * minAY) / minAX;
2108 if (height < minH) {
2109 height = minH;
2110 width = (height * minAX) / minAY;
2112 } else {
2113 width = (height * minAX) / minAY;
2114 if (width < minW) {
2115 width = minW;
2116 height = (width * minAY) / minAX;
2122 if (baseW != 0) {
2123 width = (((width - baseW) / winc) * winc) + baseW;
2124 } else {
2125 width = (((width - minW) / winc) * winc) + minW;
2128 if (baseH != 0) {
2129 height = (((height - baseH) / hinc) * hinc) + baseH;
2130 } else {
2131 height = (((height - minH) / hinc) * hinc) + minH;
2134 /* broken stupid apps may cause preposterous values for these.. */
2135 if (width > 0)
2136 *nwidth = width;
2137 if (height > 0)
2138 *nheight = height;
2142 void
2143 wWindowCropSize(WWindow *wwin, int maxW, int maxH,
2144 int *width, int *height)
2146 int baseW = 0, baseH = 0;
2147 int winc = 1, hinc = 1;
2149 if (wwin->normal_hints) {
2150 baseW = wwin->normal_hints->base_width;
2151 baseH = wwin->normal_hints->base_height;
2153 winc = wwin->normal_hints->width_inc;
2154 hinc = wwin->normal_hints->height_inc;
2157 if (*width > maxW)
2158 *width = maxW - (maxW - baseW) % winc;
2160 if (*height > maxH)
2161 *height = maxH - (maxH - baseH) % hinc;
2165 void
2166 wWindowChangeWorkspace(WWindow *wwin, int workspace)
2168 WScreen *scr = wwin->screen_ptr;
2169 WApplication *wapp;
2170 int unmap = 0;
2172 if (workspace >= scr->workspace_count || workspace < 0
2173 || workspace == wwin->frame->workspace)
2174 return;
2176 if (workspace != scr->current_workspace) {
2177 /* Sent to other workspace. Unmap window */
2178 if ((wwin->flags.mapped
2179 || wwin->flags.shaded
2180 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2181 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2183 wapp = wApplicationOf(wwin->main_window);
2184 if (wapp) {
2185 wapp->last_workspace = workspace;
2187 if (wwin->flags.miniaturized) {
2188 if (wwin->icon) {
2189 XUnmapWindow(dpy, wwin->icon->core->window);
2190 wwin->icon->mapped = 0;
2192 } else {
2193 unmap = 1;
2194 wSetFocusTo(scr, NULL);
2197 } else {
2198 /* brought to current workspace. Map window */
2199 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2200 if (wwin->icon) {
2201 XMapWindow(dpy, wwin->icon->core->window);
2202 wwin->icon->mapped = 1;
2204 } else if (!wwin->flags.mapped &&
2205 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2206 wWindowMap(wwin);
2209 if (!IS_OMNIPRESENT(wwin)) {
2210 int oldWorkspace = wwin->frame->workspace;
2212 wwin->frame->workspace = workspace;
2214 WMPostNotificationName(WMNChangedWorkspace, wwin, (void*)oldWorkspace);
2217 if (unmap) {
2218 wWindowUnmap(wwin);
2223 void
2224 wWindowSynthConfigureNotify(WWindow *wwin)
2226 XEvent sevent;
2228 sevent.type = ConfigureNotify;
2229 sevent.xconfigure.display = dpy;
2230 sevent.xconfigure.event = wwin->client_win;
2231 sevent.xconfigure.window = wwin->client_win;
2233 sevent.xconfigure.x = wwin->client.x;
2234 sevent.xconfigure.y = wwin->client.y;
2235 sevent.xconfigure.width = wwin->client.width;
2236 sevent.xconfigure.height = wwin->client.height;
2238 sevent.xconfigure.border_width = wwin->old_border_width;
2239 if (!HAS_TITLEBAR(wwin))
2240 sevent.xconfigure.above = None;
2241 else
2242 sevent.xconfigure.above = wwin->frame->titlebar->window;
2244 sevent.xconfigure.override_redirect = False;
2245 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2246 XFlush(dpy);
2251 *----------------------------------------------------------------------
2252 * wWindowConfigure--
2253 * Configures the frame, decorations and client window to the
2254 * specified geometry. The geometry is not checked for validity,
2255 * wWindowConstrainSize() must be used for that.
2256 * The size parameters are for the client window, but the position is
2257 * for the frame.
2258 * The client window receives a ConfigureNotify event, according
2259 * to what ICCCM says.
2261 * Returns:
2262 * None
2264 * Side effects:
2265 * Window size and position are changed and client window receives
2266 * a ConfigureNotify event.
2267 *----------------------------------------------------------------------
2269 void
2270 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
2271 WWindow *wwin;
2272 int req_x, req_y; /* new position of the frame */
2273 int req_width, req_height; /* new size of the client */
2275 int synth_notify = False;
2276 int resize;
2278 resize = (req_width!=wwin->client.width
2279 || req_height!=wwin->client.height);
2281 * if the window is being moved but not resized then
2282 * send a synthetic ConfigureNotify
2284 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
2285 synth_notify = True;
2288 if (WFLAGP(wwin, dont_move_off))
2289 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2290 req_width, req_height);
2291 if (resize) {
2292 if (req_width < MIN_WINDOW_SIZE)
2293 req_width = MIN_WINDOW_SIZE;
2294 if (req_height < MIN_WINDOW_SIZE)
2295 req_height = MIN_WINDOW_SIZE;
2297 /* If growing, resize inner part before frame,
2298 * if shrinking, resize frame before.
2299 * This will prevent the frame (that can have a different color)
2300 * to be exposed, causing flicker */
2301 if (req_height > wwin->frame->core->height
2302 || req_width > wwin->frame->core->width)
2303 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2305 if (wwin->flags.shaded) {
2306 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2307 req_width, wwin->frame->core->height);
2308 wwin->old_geometry.height = req_height;
2309 } else {
2310 int h;
2312 h = req_height + wwin->frame->top_width
2313 + wwin->frame->bottom_width;
2315 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2318 if (!(req_height > wwin->frame->core->height
2319 || req_width > wwin->frame->core->width))
2320 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2322 wwin->client.x = req_x;
2323 wwin->client.y = req_y + wwin->frame->top_width;
2324 wwin->client.width = req_width;
2325 wwin->client.height = req_height;
2326 } else {
2327 wwin->client.x = req_x;
2328 wwin->client.y = req_y + wwin->frame->top_width;
2330 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2332 wwin->frame_x = req_x;
2333 wwin->frame_y = req_y;
2334 if (HAS_BORDER(wwin)) {
2335 wwin->client.x += FRAME_BORDER_WIDTH;
2336 wwin->client.y += FRAME_BORDER_WIDTH;
2339 #ifdef SHAPE
2340 if (wShapeSupported && wwin->flags.shaped && resize) {
2341 wWindowSetShape(wwin);
2343 #endif
2345 if (synth_notify)
2346 wWindowSynthConfigureNotify(wwin);
2347 XFlush(dpy);
2351 void
2352 wWindowMove(wwin, req_x, req_y)
2353 WWindow *wwin;
2354 int req_x, req_y; /* new position of the frame */
2356 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2357 int synth_notify = False;
2359 /* Send a synthetic ConfigureNotify event for every window movement. */
2360 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2361 synth_notify = True;
2363 #else
2364 /* A single synthetic ConfigureNotify event is sent at the end of
2365 * a completed (opaque) movement in moveres.c */
2366 #endif
2368 if (WFLAGP(wwin, dont_move_off))
2369 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2370 wwin->frame->core->width, wwin->frame->core->height);
2372 wwin->client.x = req_x;
2373 wwin->client.y = req_y + wwin->frame->top_width;
2374 if (HAS_BORDER(wwin)) {
2375 wwin->client.x += FRAME_BORDER_WIDTH;
2376 wwin->client.y += FRAME_BORDER_WIDTH;
2379 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2381 wwin->frame_x = req_x;
2382 wwin->frame_y = req_y;
2384 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2385 if (synth_notify)
2386 wWindowSynthConfigureNotify(wwin);
2387 #endif
2391 void
2392 wWindowUpdateButtonImages(WWindow *wwin)
2394 WScreen *scr = wwin->screen_ptr;
2395 Pixmap pixmap, mask;
2396 WFrameWindow *fwin = wwin->frame;
2398 if (!HAS_TITLEBAR(wwin))
2399 return;
2401 /* miniaturize button */
2403 if (!WFLAGP(wwin, no_miniaturize_button)) {
2404 if (wwin->wm_gnustep_attr
2405 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2406 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2408 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2409 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2410 } else {
2411 mask = None;
2414 if (fwin->lbutton_image
2415 && (fwin->lbutton_image->image != pixmap
2416 || fwin->lbutton_image->mask != mask)) {
2417 wPixmapDestroy(fwin->lbutton_image);
2418 fwin->lbutton_image = NULL;
2421 if (!fwin->lbutton_image) {
2422 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2423 fwin->lbutton_image->client_owned = 1;
2424 fwin->lbutton_image->client_owned_mask = 1;
2426 } else {
2427 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2428 wPixmapDestroy(fwin->lbutton_image);
2430 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2434 #ifdef XKB_BUTTON_HINT
2435 if (!WFLAGP(wwin, no_language_button)) {
2436 if (fwin->languagebutton_image &&
2437 !fwin->languagebutton_image->shared) {
2438 wPixmapDestroy(fwin->languagebutton_image);
2440 fwin->languagebutton_image =
2441 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2443 #endif
2445 /* close button */
2447 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2448 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2450 if (!WFLAGP(wwin, no_close_button)) {
2451 if (wwin->wm_gnustep_attr
2452 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2453 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2455 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2456 mask = wwin->wm_gnustep_attr->close_mask;
2457 else
2458 mask = None;
2460 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2461 || fwin->rbutton_image->mask != mask)) {
2462 wPixmapDestroy(fwin->rbutton_image);
2463 fwin->rbutton_image = NULL;
2466 if (!fwin->rbutton_image) {
2467 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2468 fwin->rbutton_image->client_owned = 1;
2469 fwin->rbutton_image->client_owned_mask = 1;
2472 } else if (WFLAGP(wwin, kill_close)) {
2474 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2475 wPixmapDestroy(fwin->rbutton_image);
2477 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2479 } else if (MGFLAGP(wwin, broken_close)) {
2481 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2482 wPixmapDestroy(fwin->rbutton_image);
2484 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2486 } else {
2488 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2489 wPixmapDestroy(fwin->rbutton_image);
2491 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2495 /* force buttons to be redrawn */
2496 fwin->flags.need_texture_change = 1;
2497 wFrameWindowPaint(fwin);
2502 *---------------------------------------------------------------------------
2503 * wWindowConfigureBorders--
2504 * Update window border configuration according to attribute flags.
2506 *---------------------------------------------------------------------------
2508 void
2509 wWindowConfigureBorders(WWindow *wwin)
2511 if (wwin->frame) {
2512 int flags;
2513 int newy, oldh;
2515 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2517 #ifdef XKB_BUTTON_HINT
2518 flags |= WFF_LANGUAGE_BUTTON;
2519 #endif
2521 if (HAS_TITLEBAR(wwin))
2522 flags |= WFF_TITLEBAR;
2523 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2524 flags |= WFF_RESIZEBAR;
2525 if (HAS_BORDER(wwin))
2526 flags |= WFF_BORDER;
2527 if (wwin->flags.shaded)
2528 flags |= WFF_IS_SHADED;
2530 oldh = wwin->frame->top_width;
2531 wFrameWindowUpdateBorders(wwin->frame, flags);
2532 if (oldh != wwin->frame->top_width) {
2533 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2535 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2536 wWindowConfigure(wwin, wwin->frame_x, newy,
2537 wwin->client.width, wwin->client.height);
2540 flags = 0;
2541 if (!WFLAGP(wwin, no_miniaturize_button)
2542 && wwin->frame->flags.hide_left_button)
2543 flags |= WFF_LEFT_BUTTON;
2545 #ifdef XKB_BUTTON_HINT
2546 if (!WFLAGP(wwin, no_language_button)
2547 && wwin->frame->flags.hide_language_button) {
2548 flags |= WFF_LANGUAGE_BUTTON;
2550 #endif
2552 if (!WFLAGP(wwin, no_close_button)
2553 && wwin->frame->flags.hide_right_button)
2554 flags |= WFF_RIGHT_BUTTON;
2556 if (flags!=0) {
2557 wWindowUpdateButtonImages(wwin);
2558 wFrameWindowShowButton(wwin->frame, flags);
2561 flags = 0;
2562 if (WFLAGP(wwin, no_miniaturize_button)
2563 && !wwin->frame->flags.hide_left_button)
2564 flags |= WFF_LEFT_BUTTON;
2566 #ifdef XKB_BUTTON_HINT
2567 if (WFLAGP(wwin, no_language_button)
2568 && !wwin->frame->flags.hide_language_button)
2569 flags |= WFF_LANGUAGE_BUTTON;
2570 #endif
2572 if (WFLAGP(wwin, no_close_button)
2573 && !wwin->frame->flags.hide_right_button)
2574 flags |= WFF_RIGHT_BUTTON;
2576 if (flags!=0)
2577 wFrameWindowHideButton(wwin->frame, flags);
2579 #ifdef SHAPE
2580 if (wShapeSupported && wwin->flags.shaped) {
2581 wWindowSetShape(wwin);
2583 #endif
2588 void
2589 wWindowSaveState(WWindow *wwin)
2591 CARD32 data[10];
2592 int i;
2594 memset(data, 0, sizeof(CARD32)*10);
2595 data[0] = wwin->frame->workspace;
2596 data[1] = wwin->flags.miniaturized;
2597 data[2] = wwin->flags.shaded;
2598 data[3] = wwin->flags.hidden;
2599 data[4] = wwin->flags.maximized;
2600 if (wwin->flags.maximized == 0) {
2601 data[5] = wwin->frame_x;
2602 data[6] = wwin->frame_y;
2603 data[7] = wwin->frame->core->width;
2604 data[8] = wwin->frame->core->height;
2605 } else {
2606 data[5] = wwin->old_geometry.x;
2607 data[6] = wwin->old_geometry.y;
2608 data[7] = wwin->old_geometry.width;
2609 data[8] = wwin->old_geometry.height;
2612 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2613 if (wwin->screen_ptr->shortcutWindows[i] &&
2614 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2615 data[9] |= 1<<i;
2617 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2618 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2619 (unsigned char *)data, 10);
2623 static int
2624 getSavedState(Window window, WSavedState **state)
2626 Atom type_ret;
2627 int fmt_ret;
2628 unsigned long nitems_ret;
2629 unsigned long bytes_after_ret;
2630 CARD32 *data;
2632 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2633 True, _XA_WINDOWMAKER_STATE,
2634 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2635 (unsigned char **)&data)!=Success || !data)
2636 return 0;
2638 *state = wmalloc(sizeof(WSavedState));
2640 (*state)->workspace = data[0];
2641 (*state)->miniaturized = data[1];
2642 (*state)->shaded = data[2];
2643 (*state)->hidden = data[3];
2644 (*state)->maximized = data[4];
2645 (*state)->x = data[5];
2646 (*state)->y = data[6];
2647 (*state)->w = data[7];
2648 (*state)->h = data[8];
2649 (*state)->window_shortcuts = data[9];
2651 XFree(data);
2653 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2654 return 1;
2655 else
2656 return 0;
2660 #ifdef SHAPE
2661 void
2662 wWindowClearShape(WWindow *wwin)
2664 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2665 0, wwin->frame->top_width, None, ShapeSet);
2666 XFlush(dpy);
2669 void
2670 wWindowSetShape(WWindow *wwin)
2672 XRectangle rect[2];
2673 int count;
2674 #ifdef OPTIMIZE_SHAPE
2675 XRectangle *rects;
2676 XRectangle *urec;
2677 int ordering;
2679 /* only shape is the client's */
2680 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2681 goto alt_code;
2684 /* Get array of rectangles describing the shape mask */
2685 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2686 &count, &ordering);
2687 if (!rects) {
2688 goto alt_code;
2691 urec = malloc(sizeof(XRectangle)*(count+2));
2692 if (!urec) {
2693 XFree(rects);
2694 goto alt_code;
2697 /* insert our decoration rectangles in the rect list */
2698 memcpy(urec, rects, sizeof(XRectangle)*count);
2699 XFree(rects);
2701 if (HAS_TITLEBAR(wwin)) {
2702 urec[count].x = -1;
2703 urec[count].y = -1 - wwin->frame->top_width;
2704 urec[count].width = wwin->frame->core->width + 2;
2705 urec[count].height = wwin->frame->top_width + 1;
2706 count++;
2708 if (HAS_RESIZEBAR(wwin)) {
2709 urec[count].x = -1;
2710 urec[count].y = wwin->frame->core->height
2711 - wwin->frame->bottom_width - wwin->frame->top_width;
2712 urec[count].width = wwin->frame->core->width + 2;
2713 urec[count].height = wwin->frame->bottom_width + 1;
2714 count++;
2717 /* shape our frame window */
2718 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2719 0, wwin->frame->top_width, urec, count,
2720 ShapeSet, Unsorted);
2721 XFlush(dpy);
2722 wfree(urec);
2723 return;
2725 alt_code:
2726 #endif /* OPTIMIZE_SHAPE */
2727 count = 0;
2728 if (HAS_TITLEBAR(wwin)) {
2729 rect[count].x = -1;
2730 rect[count].y = -1;
2731 rect[count].width = wwin->frame->core->width + 2;
2732 rect[count].height = wwin->frame->top_width + 1;
2733 count++;
2735 if (HAS_RESIZEBAR(wwin)) {
2736 rect[count].x = -1;
2737 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2738 rect[count].width = wwin->frame->core->width + 2;
2739 rect[count].height = wwin->frame->bottom_width + 1;
2740 count++;
2742 if (count > 0) {
2743 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2744 0, 0, rect, count, ShapeSet, Unsorted);
2746 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2747 0, wwin->frame->top_width, wwin->client_win,
2748 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2749 XFlush(dpy);
2751 #endif /* SHAPE */
2753 /* ====================================================================== */
2755 static FocusMode
2756 getFocusMode(WWindow *wwin)
2758 FocusMode mode;
2760 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2761 if (wwin->wm_hints->input == True) {
2762 if (wwin->protocols.TAKE_FOCUS)
2763 mode = WFM_LOCALLY_ACTIVE;
2764 else
2765 mode = WFM_PASSIVE;
2766 } else {
2767 if (wwin->protocols.TAKE_FOCUS)
2768 mode = WFM_GLOBALLY_ACTIVE;
2769 else
2770 mode = WFM_NO_INPUT;
2772 } else {
2773 mode = WFM_PASSIVE;
2775 return mode;
2779 void
2780 wWindowSetKeyGrabs(WWindow *wwin)
2782 int i;
2783 WShortKey *key;
2785 for (i=0; i<WKBD_LAST; i++) {
2786 key = &wKeyBindings[i];
2788 if (key->keycode==0)
2789 continue;
2790 if (key->modifier!=AnyModifier) {
2791 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2792 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2793 #ifdef NUMLOCK_HACK
2794 /* Also grab all modifier combinations possible that include,
2795 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2796 * work even if the NumLock/ScrollLock key is on.
2798 wHackedGrabKey(key->keycode, key->modifier,
2799 wwin->frame->core->window, True, GrabModeAsync,
2800 GrabModeAsync);
2801 #endif
2803 XGrabKey(dpy, key->keycode, key->modifier,
2804 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2807 #ifndef LITE
2808 wRootMenuBindShortcuts(wwin->frame->core->window);
2809 #endif
2814 void
2815 wWindowResetMouseGrabs(WWindow *wwin)
2817 /* Mouse grabs can't be done on the client window because of
2818 * ICCCM and because clients that try to do the same will crash.
2820 * But there is a problem wich makes tbar buttons of unfocused
2821 * windows not usable as the click goes to the frame window instead
2822 * of the button itself. Must figure a way to fix that.
2825 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2827 if (!WFLAGP(wwin, no_bind_mouse)) {
2828 /* grabs for Meta+drag */
2829 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2830 True, ButtonPressMask|ButtonReleaseMask,
2831 GrabModeSync, GrabModeAsync, None, None);
2834 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2835 && !wwin->flags.is_gnustep) {
2836 /* the passive grabs to focus the window */
2837 /* if (wPreferences.focus_mode == WKF_CLICK) */
2838 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2839 True, ButtonPressMask|ButtonReleaseMask,
2840 GrabModeSync, GrabModeAsync, None, None);
2842 XFlush(dpy);
2846 void
2847 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2849 if (attr->flags & GSExtraFlagsAttr) {
2850 if (MGFLAGP(wwin, broken_close) !=
2851 (attr->extra_flags & GSDocumentEditedFlag)) {
2852 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2853 wWindowUpdateButtonImages(wwin);
2859 WMagicNumber
2860 wWindowAddSavedState(char *instance, char *class, char *command,
2861 pid_t pid, WSavedState *state)
2863 WWindowState *wstate;
2865 wstate = malloc(sizeof(WWindowState));
2866 if (!wstate)
2867 return 0;
2869 memset(wstate, 0, sizeof(WWindowState));
2870 wstate->pid = pid;
2871 if (instance)
2872 wstate->instance = wstrdup(instance);
2873 if (class)
2874 wstate->class = wstrdup(class);
2875 if (command)
2876 wstate->command = wstrdup(command);
2877 wstate->state = state;
2879 wstate->next = windowState;
2880 windowState = wstate;
2882 #ifdef DEBUG
2883 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2884 class, command);
2885 #endif
2887 return wstate;
2891 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2894 WMagicNumber
2895 wWindowGetSavedState(Window win)
2897 char *instance, *class, *command=NULL;
2898 WWindowState *wstate = windowState;
2900 if (!wstate)
2901 return NULL;
2903 command = GetCommandForWindow(win);
2904 if (!command)
2905 return NULL;
2907 if (PropGetWMClass(win, &class, &instance)) {
2908 while (wstate) {
2909 if (SAME(instance, wstate->instance) &&
2910 SAME(class, wstate->class) &&
2911 SAME(command, wstate->command)) {
2912 break;
2914 wstate = wstate->next;
2916 } else {
2917 wstate = NULL;
2920 #ifdef DEBUG
2921 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2922 class, command);
2923 #endif
2925 if (command) wfree(command);
2926 if (instance) XFree(instance);
2927 if (class) XFree(class);
2929 return wstate;
2933 void
2934 wWindowDeleteSavedState(WMagicNumber id)
2936 WWindowState *tmp, *wstate=(WWindowState*)id;
2938 if (!wstate || !windowState)
2939 return;
2941 tmp = windowState;
2942 if (tmp==wstate) {
2943 windowState = wstate->next;
2944 #ifdef DEBUG
2945 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2946 wstate, wstate->instance, wstate->class, wstate->command);
2947 #endif
2948 if (wstate->instance) wfree(wstate->instance);
2949 if (wstate->class) wfree(wstate->class);
2950 if (wstate->command) wfree(wstate->command);
2951 wfree(wstate->state);
2952 wfree(wstate);
2953 } else {
2954 while (tmp->next) {
2955 if (tmp->next==wstate) {
2956 tmp->next=wstate->next;
2957 #ifdef DEBUG
2958 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2959 wstate, wstate->instance, wstate->class, wstate->command);
2960 #endif
2961 if (wstate->instance) wfree(wstate->instance);
2962 if (wstate->class) wfree(wstate->class);
2963 if (wstate->command) wfree(wstate->command);
2964 wfree(wstate->state);
2965 wfree(wstate);
2966 break;
2968 tmp = tmp->next;
2974 void
2975 wWindowDeleteSavedStatesForPID(pid_t pid)
2977 WWindowState *tmp, *wstate;
2979 if (!windowState)
2980 return;
2982 tmp = windowState;
2983 if (tmp->pid == pid) {
2984 wstate = windowState;
2985 windowState = tmp->next;
2986 #ifdef DEBUG
2987 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2988 wstate, wstate->instance, wstate->class, wstate->command);
2989 #endif
2990 if (wstate->instance) wfree(wstate->instance);
2991 if (wstate->class) wfree(wstate->class);
2992 if (wstate->command) wfree(wstate->command);
2993 wfree(wstate->state);
2994 wfree(wstate);
2995 } else {
2996 while (tmp->next) {
2997 if (tmp->next->pid==pid) {
2998 wstate = tmp->next;
2999 tmp->next = wstate->next;
3000 #ifdef DEBUG
3001 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
3002 wstate, wstate->instance, wstate->class, wstate->command);
3003 #endif
3004 if (wstate->instance) wfree(wstate->instance);
3005 if (wstate->class) wfree(wstate->class);
3006 if (wstate->command) wfree(wstate->command);
3007 wfree(wstate->state);
3008 wfree(wstate);
3009 break;
3011 tmp = tmp->next;
3017 void
3018 wWindowSetOmnipresent(WWindow *wwin, Bool flag)
3020 if (wwin->flags.omnipresent == flag)
3021 return;
3023 wwin->flags.omnipresent = flag;
3024 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
3028 /* ====================================================================== */
3030 static void
3031 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3033 WWindow *wwin = data;
3035 #ifndef NUMLOCK_HACK
3036 if ((event->xbutton.state & ValidModMask)
3037 != (event->xbutton.state & ~LockMask)) {
3038 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3039 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3041 #endif
3043 event->xbutton.state &= ValidModMask;
3045 CloseWindowMenu(wwin->screen_ptr);
3047 if (wPreferences.focus_mode==WKF_CLICK
3048 && !(event->xbutton.state&ControlMask)
3049 && !WFLAGP(wwin, no_focusable)) {
3050 wSetFocusTo(wwin->screen_ptr, wwin);
3053 if (event->xbutton.button == Button1)
3054 wRaiseFrame(wwin->frame->core);
3056 if (event->xbutton.window != wwin->frame->resizebar->window) {
3057 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
3058 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3059 GrabModeAsync, GrabModeAsync, None,
3060 None, CurrentTime)!=GrabSuccess) {
3061 #ifdef DEBUG0
3062 wwarning("pointer grab failed for window move");
3063 #endif
3064 return;
3068 if (event->xbutton.state & MOD_MASK) {
3069 /* move the window */
3070 wMouseMoveWindow(wwin, event);
3071 XUngrabPointer(dpy, CurrentTime);
3072 } else {
3073 wMouseResizeWindow(wwin, event);
3074 XUngrabPointer(dpy, CurrentTime);
3080 static void
3081 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
3083 WWindow *wwin = data;
3085 event->xbutton.state &= ValidModMask;
3087 if (event->xbutton.button==Button1) {
3088 if (event->xbutton.state == 0) {
3089 if (!WFLAGP(wwin, no_shadeable)) {
3090 /* shade window */
3091 if (wwin->flags.shaded)
3092 wUnshadeWindow(wwin);
3093 else
3094 wShadeWindow(wwin);
3096 } else {
3097 int dir = 0;
3099 if (event->xbutton.state & ControlMask)
3100 dir |= MAX_VERTICAL;
3102 if (event->xbutton.state & ShiftMask) {
3103 dir |= MAX_HORIZONTAL;
3104 if (!(event->xbutton.state & ControlMask))
3105 wSelectWindow(wwin, !wwin->flags.selected);
3108 /* maximize window */
3109 if (dir!=0 && IS_RESIZABLE(wwin)) {
3110 int ndir = dir ^ wwin->flags.maximized;
3112 if (ndir != 0) {
3113 wMaximizeWindow(wwin, ndir);
3114 } else {
3115 wUnmaximizeWindow(wwin);
3119 } else if (event->xbutton.button==Button3) {
3120 if (event->xbutton.state & MOD_MASK) {
3121 wHideOtherApplications(wwin);
3123 } else if (event->xbutton.button==Button2) {
3124 wSelectWindow(wwin, !wwin->flags.selected);
3125 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
3126 wShadeWindow(wwin);
3127 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
3128 wUnshadeWindow(wwin);
3133 static void
3134 frameMouseDown(WObjDescriptor *desc, XEvent *event)
3136 WWindow *wwin = desc->parent;
3138 event->xbutton.state &= ValidModMask;
3140 CloseWindowMenu(wwin->screen_ptr);
3142 if (/*wPreferences.focus_mode==WKF_CLICK
3143 &&*/ !(event->xbutton.state&ControlMask)
3144 && !WFLAGP(wwin, no_focusable)) {
3145 wSetFocusTo(wwin->screen_ptr, wwin);
3147 if (event->xbutton.button == Button1) {
3148 wRaiseFrame(wwin->frame->core);
3151 if (event->xbutton.state & MOD_MASK) {
3152 /* move the window */
3153 if (XGrabPointer(dpy, wwin->client_win, False,
3154 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3155 GrabModeAsync, GrabModeAsync, None,
3156 None, CurrentTime)!=GrabSuccess) {
3157 #ifdef DEBUG0
3158 wwarning("pointer grab failed for window move");
3159 #endif
3160 return;
3162 if (event->xbutton.button == Button3)
3163 wMouseResizeWindow(wwin, event);
3164 else if (event->xbutton.button==Button1 || event->xbutton.button==Button2)
3165 wMouseMoveWindow(wwin, event);
3166 XUngrabPointer(dpy, CurrentTime);
3171 static void
3172 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3174 WWindow *wwin = (WWindow*)data;
3176 #ifndef NUMLOCK_HACK
3177 if ((event->xbutton.state & ValidModMask)
3178 != (event->xbutton.state & ~LockMask)) {
3179 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3180 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3182 #endif
3183 event->xbutton.state &= ValidModMask;
3185 CloseWindowMenu(wwin->screen_ptr);
3187 if (wPreferences.focus_mode==WKF_CLICK
3188 && !(event->xbutton.state&ControlMask)
3189 && !WFLAGP(wwin, no_focusable)) {
3190 wSetFocusTo(wwin->screen_ptr, wwin);
3193 if (event->xbutton.button == Button1
3194 || event->xbutton.button == Button2) {
3196 if (event->xbutton.button == Button1) {
3197 if (event->xbutton.state & MOD_MASK) {
3198 wLowerFrame(wwin->frame->core);
3199 } else {
3200 wRaiseFrame(wwin->frame->core);
3203 if ((event->xbutton.state & ShiftMask)
3204 && !(event->xbutton.state & ControlMask)) {
3205 wSelectWindow(wwin, !wwin->flags.selected);
3206 return;
3208 if (event->xbutton.window != wwin->frame->titlebar->window
3209 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3210 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3211 GrabModeAsync, GrabModeAsync, None,
3212 None, CurrentTime)!=GrabSuccess) {
3213 #ifdef DEBUG0
3214 wwarning("pointer grab failed for window move");
3215 #endif
3216 return;
3219 /* move the window */
3220 wMouseMoveWindow(wwin, event);
3222 XUngrabPointer(dpy, CurrentTime);
3223 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
3224 && !wwin->flags.internal_window
3225 && !WCHECK_STATE(WSTATE_MODAL)) {
3226 WObjDescriptor *desc;
3228 if (event->xbutton.window != wwin->frame->titlebar->window
3229 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3230 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3231 GrabModeAsync, GrabModeAsync, None,
3232 None, CurrentTime)!=GrabSuccess) {
3233 #ifdef DEBUG0
3234 wwarning("pointer grab failed for window move");
3235 #endif
3236 return;
3239 OpenWindowMenu(wwin, event->xbutton.x_root,
3240 wwin->frame_y+wwin->frame->top_width, False);
3242 /* allow drag select */
3243 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3244 event->xany.send_event = True;
3245 (*desc->handle_mousedown)(desc, event);
3247 XUngrabPointer(dpy, CurrentTime);
3253 static void
3254 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3256 WWindow *wwin = data;
3258 event->xbutton.state &= ValidModMask;
3260 CloseWindowMenu(wwin->screen_ptr);
3262 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3263 return;
3265 /* if control-click, kill the client */
3266 if (event->xbutton.state & ControlMask) {
3267 wClientKill(wwin);
3268 } else {
3269 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
3270 /* send delete message */
3271 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3277 static void
3278 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3280 WWindow *wwin = data;
3282 CloseWindowMenu(wwin->screen_ptr);
3284 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3285 return;
3287 /* send delete message */
3288 if (wwin->protocols.DELETE_WINDOW) {
3289 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3290 } else {
3291 wClientKill(wwin);
3296 #ifdef XKB_BUTTON_HINT
3297 static void
3298 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3300 WWindow *wwin = data;
3301 WFrameWindow *fwin = wwin->frame;
3302 WScreen *scr = fwin->screen_ptr;
3303 XkbStateRec staterec;
3304 int tl;
3306 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3307 return;
3308 tl = wwin->frame->languagemode;
3309 wwin->frame->languagemode = wwin->frame->last_languagemode;
3310 wwin->frame->last_languagemode = tl;
3311 wSetFocusTo(scr, wwin);
3312 wwin->frame->languagebutton_image =
3313 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3314 wwin->frame->languagemode];
3315 wFrameWindowUpdateLanguageButton(wwin->frame);
3316 if (event->xbutton.button == Button3)
3317 return;
3318 wRaiseFrame(fwin->core);
3320 #endif
3323 static void
3324 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3326 WWindow *wwin = data;
3328 event->xbutton.state &= ValidModMask;
3330 CloseWindowMenu(wwin->screen_ptr);
3332 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3333 return;
3335 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3336 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3337 LastTimestamp);
3338 } else {
3339 WApplication *wapp;
3340 if ((event->xbutton.state & ControlMask) ||
3341 (event->xbutton.button == Button3)) {
3343 wapp = wApplicationOf(wwin->main_window);
3344 if (wapp && !WFLAGP(wwin, no_appicon))
3345 wHideApplication(wapp);
3346 } else if (event->xbutton.state==0) {
3347 wIconifyWindow(wwin);