- Added support for NET_WM_NAME/NET_WM_ICON_NAME
[wmaker-crm.git] / src / window.c
blob35cc9b69adb6c918db7f47ed36df5db542f23408
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 wwin = wWindowCreate();
727 title= wNETWMGetWindowName(window);
728 if (title)
729 wwin->flags.net_has_title= 1;
730 if (!title && !wFetchName(dpy, window, &title))
731 title = NULL;
733 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
735 #ifdef DEBUG
736 printf("managing window %x\n", (unsigned)window);
737 #endif
739 #ifdef SHAPE
740 if (wShapeSupported) {
741 int junk;
742 unsigned int ujunk;
743 int b_shaped;
745 XShapeSelectInput(dpy, window, ShapeNotifyMask);
746 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
747 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
748 wwin->flags.shaped = b_shaped;
750 #endif
753 *--------------------------------------------------
755 * Get hints and other information in properties
757 *--------------------------------------------------
759 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
761 /* setup descriptor */
762 wwin->client_win = window;
763 wwin->screen_ptr = scr;
765 wwin->old_border_width = wattribs.border_width;
767 wwin->event_mask = CLIENT_EVENTS;
768 attribs.event_mask = CLIENT_EVENTS;
769 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
770 attribs.save_under = False;
771 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
772 |CWSaveUnder, &attribs);
773 XSetWindowBorderWidth(dpy, window, 0);
775 /* get hints from GNUstep app */
776 if (wwin->wm_class != 0 && strcmp(wwin->wm_class, "GNUstep") == 0) {
777 wwin->flags.is_gnustep = 1;
779 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
780 wwin->wm_gnustep_attr = NULL;
783 wwin->client_leader = PropGetClientLeader(window);
784 if (wwin->client_leader!=None)
785 wwin->main_window = wwin->client_leader;
787 wwin->wm_hints = XGetWMHints(dpy, window);
789 if (wwin->wm_hints) {
790 if (wwin->wm_hints->flags & StateHint) {
792 if (wwin->wm_hints->initial_state == IconicState) {
794 wwin->flags.miniaturized = 1;
796 } else if (wwin->wm_hints->initial_state == WithdrawnState) {
798 withdraw = True;
802 if (wwin->wm_hints->flags & WindowGroupHint) {
803 wwin->group_id = wwin->wm_hints->window_group;
804 /* window_group has priority over CLIENT_LEADER */
805 wwin->main_window = wwin->group_id;
806 } else {
807 wwin->group_id = None;
810 if (wwin->wm_hints->flags & UrgencyHint)
811 wwin->flags.urgent = 1;
812 } else {
813 wwin->group_id = None;
816 PropGetProtocols(window, &wwin->protocols);
818 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
819 wwin->transient_for = None;
820 } else {
821 if (wwin->transient_for==None || wwin->transient_for==window) {
822 wwin->transient_for = scr->root_win;
823 } else {
824 transientOwner = wWindowFor(wwin->transient_for);
825 if (transientOwner && transientOwner->main_window!=None) {
826 wwin->main_window = transientOwner->main_window;
827 } /*else {
828 wwin->main_window = None;
833 /* guess the focus mode */
834 wwin->focus_mode = getFocusMode(wwin);
836 /* get geometry stuff */
837 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
839 /* printf("wManageWindow: %d %d %d %d\n", x, y, width, height);*/
841 /* get colormap windows */
842 GetColormapWindows(wwin);
845 *--------------------------------------------------
847 * Setup the decoration/window attributes and
848 * geometry
850 *--------------------------------------------------
853 wWindowSetupInitialAttributes(wwin, &window_level, &workspace);
855 /* Make broken apps behave as a nice app. */
856 if (WFLAGP(wwin, emulate_appicon)) {
857 wwin->main_window = wwin->client_win;
860 fixLeaderProperties(wwin);
862 wwin->orig_main_window = wwin->main_window;
864 if (wwin->flags.is_gnustep) {
865 WSETUFLAG(wwin, shared_appicon, 0);
868 if (wwin->main_window) {
869 extern Atom _XA_WINDOWMAKER_MENU;
870 XTextProperty text_prop;
872 if (XGetTextProperty(dpy, wwin->main_window, &text_prop,
873 _XA_WINDOWMAKER_MENU)) {
874 WSETUFLAG(wwin, shared_appicon, 0);
878 if (!withdraw && wwin->main_window && WFLAGP(wwin, shared_appicon)) {
879 char *buffer, *instance, *class;
880 WFakeGroupLeader *fPtr;
881 int index;
883 #define ADEQUATE(x) ((x)!=None && (x)!=wwin->client_win && (x)!=fPtr->leader)
885 // only enter here if PropGetWMClass() succeds
886 PropGetWMClass(wwin->main_window, &class, &instance);
887 buffer = StrConcatDot(instance, class);
889 index = WMFindInArray(scr->fakeGroupLeaders, matchIdentifier, (void*)buffer);
890 if (index != WANotFound) {
891 fPtr = WMGetFromArray(scr->fakeGroupLeaders, index);
892 if (fPtr->retainCount == 0) {
893 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
894 instance, class);
896 fPtr->retainCount++;
897 #undef method2
898 if (fPtr->origLeader==None) {
899 #ifdef method2
900 if (ADEQUATE(wwin->group_id)) {
901 fPtr->retainCount++;
902 fPtr->origLeader = wwin->group_id;
903 } else if (ADEQUATE(wwin->client_leader)) {
904 fPtr->retainCount++;
905 fPtr->origLeader = wwin->client_leader;
906 } else if (ADEQUATE(wwin->main_window)) {
907 fPtr->retainCount++;
908 fPtr->origLeader = wwin->main_window;
910 #else
911 if (ADEQUATE(wwin->main_window)) {
912 fPtr->retainCount++;
913 fPtr->origLeader = wwin->main_window;
915 #endif
917 wwin->fake_group = fPtr;
918 /*wwin->group_id = fPtr->leader;*/
919 wwin->main_window = fPtr->leader;
920 wfree(buffer);
921 } else {
922 fPtr = (WFakeGroupLeader*)wmalloc(sizeof(WFakeGroupLeader));
924 fPtr->identifier = buffer;
925 fPtr->leader = createFakeWindowGroupLeader(scr, wwin->main_window,
926 instance, class);
927 fPtr->origLeader = None;
928 fPtr->retainCount = 1;
930 WMAddToArray(scr->fakeGroupLeaders, fPtr);
932 #ifdef method2
933 if (ADEQUATE(wwin->group_id)) {
934 fPtr->retainCount++;
935 fPtr->origLeader = wwin->group_id;
936 } else if (ADEQUATE(wwin->client_leader)) {
937 fPtr->retainCount++;
938 fPtr->origLeader = wwin->client_leader;
939 } else if (ADEQUATE(wwin->main_window)) {
940 fPtr->retainCount++;
941 fPtr->origLeader = wwin->main_window;
943 #else
944 if (ADEQUATE(wwin->main_window)) {
945 fPtr->retainCount++;
946 fPtr->origLeader = wwin->main_window;
948 #endif
949 wwin->fake_group = fPtr;
950 /*wwin->group_id = fPtr->leader;*/
951 wwin->main_window = fPtr->leader;
953 if (instance)
954 XFree(instance);
955 if (class)
956 XFree(class);
958 #undef method2
959 #undef ADEQUATE
963 *------------------------------------------------------------
965 * Setup the initial state of the window
967 *------------------------------------------------------------
970 if (WFLAGP(wwin, start_miniaturized) && !WFLAGP(wwin, no_miniaturizable)) {
971 wwin->flags.miniaturized = 1;
974 if (WFLAGP(wwin, start_maximized) && IS_RESIZABLE(wwin)) {
975 wwin->flags.maximized = MAX_VERTICAL|MAX_HORIZONTAL;
978 #ifdef NETWM_HINTS
979 wNETWMCheckInitialClientState(wwin);
980 #endif
982 /* apply previous state if it exists and we're in startup */
983 if (scr->flags.startup && wm_state >= 0) {
985 if (wm_state == IconicState) {
987 wwin->flags.miniaturized = 1;
989 } else if (wm_state == WithdrawnState) {
991 withdraw = True;
995 /* if there is a saved state (from file), restore it */
996 win_state = NULL;
997 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
998 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
999 } else {
1000 win_state = (WWindowState*)wWindowGetSavedState(window);
1002 if (win_state && !withdraw) {
1004 if (win_state->state->hidden>0)
1005 wwin->flags.hidden = win_state->state->hidden;
1007 if (win_state->state->shaded>0 && !WFLAGP(wwin, no_shadeable))
1008 wwin->flags.shaded = win_state->state->shaded;
1010 if (win_state->state->miniaturized>0 &&
1011 !WFLAGP(wwin, no_miniaturizable)) {
1012 wwin->flags.miniaturized = win_state->state->miniaturized;
1015 if (!IS_OMNIPRESENT(wwin)) {
1016 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
1017 wwin->wm_class);
1018 if (w < 0 || w >= scr->workspace_count) {
1019 workspace = win_state->state->workspace;
1020 if (workspace >= scr->workspace_count)
1021 workspace = scr->current_workspace;
1022 } else {
1023 workspace = w;
1025 } else {
1026 workspace = scr->current_workspace;
1030 /* if we're restarting, restore saved state (from hints).
1031 * This will overwrite previous */
1033 WSavedState *wstate;
1035 if (getSavedState(window, &wstate)) {
1036 wwin->flags.shaded = wstate->shaded;
1037 wwin->flags.hidden = wstate->hidden;
1038 wwin->flags.miniaturized = wstate->miniaturized;
1039 wwin->flags.maximized = wstate->maximized;
1040 if (wwin->flags.maximized) {
1041 wwin->old_geometry.x = wstate->x;
1042 wwin->old_geometry.y = wstate->y;
1043 wwin->old_geometry.width = wstate->w;
1044 wwin->old_geometry.height = wstate->h;
1047 workspace = wstate->workspace;
1048 } else {
1049 wstate = NULL;
1052 /* restore window shortcut */
1053 if (wstate != NULL || win_state != NULL) {
1054 unsigned mask = 0;
1056 if (win_state != NULL)
1057 mask = win_state->state->window_shortcuts;
1059 if (wstate != NULL && mask == 0)
1060 mask = wstate->window_shortcuts;
1062 if (mask > 0) {
1063 int i;
1065 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
1066 if (mask & (1<<i)) {
1067 if (!scr->shortcutWindows[i])
1068 scr->shortcutWindows[i] = WMCreateArray(4);
1070 WMAddToArray(scr->shortcutWindows[i], wwin);
1075 if (wstate != NULL) {
1076 wfree(wstate);
1080 /* don't let transients start miniaturized if their owners are not */
1081 if (transientOwner && !transientOwner->flags.miniaturized
1082 && wwin->flags.miniaturized && !withdraw) {
1083 wwin->flags.miniaturized = 0;
1084 if (wwin->wm_hints)
1085 wwin->wm_hints->initial_state = NormalState;
1088 /* set workspace on which the window starts */
1089 if (workspace >= 0) {
1090 if (workspace > scr->workspace_count-1) {
1091 workspace = workspace % scr->workspace_count;
1093 } else {
1094 int w;
1096 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
1098 if (w >= 0 && w < scr->workspace_count && !(IS_OMNIPRESENT(wwin))) {
1100 workspace = w;
1102 } else {
1103 if (wPreferences.open_transients_with_parent && transientOwner) {
1105 workspace = transientOwner->frame->workspace;
1107 } else {
1109 workspace = scr->current_workspace;
1114 /* setup window geometry */
1115 if (win_state && win_state->state->w > 0) {
1116 width = win_state->state->w;
1117 height = win_state->state->h;
1119 wWindowConstrainSize(wwin, &width, &height);
1121 /* do not ask for window placement if the window is
1122 * transient, during startup, if the initial workspace is another one
1123 * or if the window wants to start iconic.
1124 * If geometry was saved, restore it. */
1126 Bool dontBring = False;
1128 if (win_state && win_state->state->w > 0) {
1129 x = win_state->state->x;
1130 y = win_state->state->y;
1131 } else if ((wwin->transient_for==None
1132 || wPreferences.window_placement!=WPM_MANUAL)
1133 && !scr->flags.startup
1134 && workspace == scr->current_workspace
1135 && !wwin->flags.miniaturized
1136 && !wwin->flags.maximized
1137 && !(wwin->normal_hints->flags & (USPosition|PPosition))) {
1139 if (transientOwner && transientOwner->flags.mapped) {
1140 int offs = WMAX(20, 2*transientOwner->frame->top_width);
1141 WMRect rect;
1142 int head;
1144 x = transientOwner->frame_x +
1145 abs((transientOwner->frame->core->width - width)/2) + offs;
1146 y = transientOwner->frame_y +
1147 abs((transientOwner->frame->core->height - height)/3) + offs;
1150 * limit transient windows to be inside their parent's head
1152 rect.pos.x = transientOwner->frame_x;
1153 rect.pos.y = transientOwner->frame_y;
1154 rect.size.width = transientOwner->frame->core->width;
1155 rect.size.height = transientOwner->frame->core->height;
1157 head = wGetHeadForRect(scr, rect);
1158 rect = wGetRectForHead(scr, head);
1160 if (x < rect.pos.x)
1161 x = rect.pos.x;
1162 else if (x + width > rect.pos.x + rect.size.width)
1163 x = rect.pos.x + rect.size.width - width;
1165 if (y < rect.pos.y)
1166 y = rect.pos.y;
1167 else if (y + height > rect.pos.y + rect.size.height)
1168 y = rect.pos.y + rect.size.height - height;
1170 } else {
1171 PlaceWindow(wwin, &x, &y, width, height);
1173 if (wPreferences.window_placement == WPM_MANUAL) {
1174 dontBring = True;
1176 } else if (scr->xine_info.count &&
1177 (wwin->normal_hints->flags & PPosition)) {
1178 int head, flags;
1179 WMRect rect;
1180 int reposition = 0;
1183 * Make spash screens come out in the center of a head
1184 * trouble is that most splashies never get here
1185 * they are managed trough atoms but god knows where.
1186 * Dan, do you know ? -peter
1188 * Most of them are not managed, they have set
1189 * OverrideRedirect, which means we can't do anything about
1190 * them. -alfredo
1192 #if 0
1193 printf("xinerama PPosition: x: %d %d\n", x, (scr->scr_width - width)/2);
1194 printf("xinerama PPosition: y: %d %d\n", y, (scr->scr_height - height)/2);
1196 if ((unsigned)(x + (width - scr->scr_width)/2 + 10) < 20 &&
1197 (unsigned)(y + (height - scr->scr_height)/2 + 10) < 20) {
1199 reposition = 1;
1201 } else
1202 #endif
1205 * xinerama checks for: across head and dead space
1207 rect.pos.x = x;
1208 rect.pos.y = y;
1209 rect.size.width = width;
1210 rect.size.height = height;
1212 head = wGetRectPlacementInfo(scr, rect, &flags);
1214 if (flags & XFLAG_DEAD)
1215 reposition = 1;
1217 if (flags & XFLAG_MULTIPLE)
1218 reposition = 2;
1221 switch (reposition) {
1222 case 1:
1223 head = wGetHeadForPointerLocation(scr);
1224 rect = wGetRectForHead(scr, head);
1226 x = rect.pos.x + (x * rect.size.width)/scr->scr_width;
1227 y = rect.pos.y + (y * rect.size.height)/scr->scr_height;
1228 break;
1230 case 2:
1231 rect = wGetRectForHead(scr, head);
1233 if (x < rect.pos.x)
1234 x = rect.pos.x;
1235 else if (x + width > rect.pos.x + rect.size.width)
1236 x = rect.pos.x + rect.size.width - width;
1238 if (y < rect.pos.y)
1239 y = rect.pos.y;
1240 else if (y + height > rect.pos.y + rect.size.height)
1241 y = rect.pos.y + rect.size.height - height;
1243 break;
1245 default:
1246 break;
1250 if (WFLAGP(wwin, dont_move_off) && dontBring)
1251 wScreenBringInside(scr, &x, &y, width, height);
1254 #ifdef NETWM_HINTS
1255 wNETWMPositionSplash(wwin, &x, &y, width, height);
1256 #endif
1258 if (wwin->flags.urgent) {
1259 if (!IS_OMNIPRESENT(wwin))
1260 wwin->flags.omnipresent ^= 1;
1264 *--------------------------------------------------
1266 * Create frame, borders and do reparenting
1268 *--------------------------------------------------
1270 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
1271 #ifdef XKB_BUTTON_HINT
1272 if (wPreferences.modelock)
1273 foo |= WFF_LANGUAGE_BUTTON;
1274 #endif
1275 if (HAS_TITLEBAR(wwin))
1276 foo |= WFF_TITLEBAR;
1277 if (HAS_RESIZEBAR(wwin))
1278 foo |= WFF_RESIZEBAR;
1279 if (HAS_BORDER(wwin))
1280 foo |= WFF_BORDER;
1282 wwin->frame = wFrameWindowCreate(scr, window_level,
1283 x, y, width, height,
1284 &wPreferences.window_title_clearance, foo,
1285 scr->window_title_texture,
1286 scr->resizebar_texture,
1287 scr->window_title_color,
1288 &scr->title_font);
1290 wwin->frame->flags.is_client_window_frame = 1;
1291 wwin->frame->flags.justification = wPreferences.title_justification;
1293 /* setup button images */
1294 wWindowUpdateButtonImages(wwin);
1296 /* hide unused buttons */
1297 foo = 0;
1298 if (WFLAGP(wwin, no_close_button))
1299 foo |= WFF_RIGHT_BUTTON;
1300 if (WFLAGP(wwin, no_miniaturize_button))
1301 foo |= WFF_LEFT_BUTTON;
1302 #ifdef XKB_BUTTON_HINT
1303 if (WFLAGP(wwin, no_language_button) || WFLAGP(wwin, no_focusable))
1304 foo |= WFF_LANGUAGE_BUTTON;
1305 #endif
1306 if (foo!=0)
1307 wFrameWindowHideButton(wwin->frame, foo);
1309 wwin->frame->child = wwin;
1311 wwin->frame->workspace = workspace;
1313 wwin->frame->on_click_left = windowIconifyClick;
1314 #ifdef XKB_BUTTON_HINT
1315 if (wPreferences.modelock)
1316 wwin->frame->on_click_language = windowLanguageClick;
1317 #endif
1319 wwin->frame->on_click_right = windowCloseClick;
1320 wwin->frame->on_dblclick_right = windowCloseDblClick;
1322 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1323 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1325 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1328 XSelectInput(dpy, wwin->client_win,
1329 wwin->event_mask & ~StructureNotifyMask);
1331 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1332 0, wwin->frame->top_width);
1334 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1338 int gx, gy;
1340 wClientGetGravityOffsets(wwin, &gx, &gy);
1342 /* if gravity is to the south, account for the border sizes */
1343 if (gy > 0)
1344 y -= wwin->frame->top_width + wwin->frame->bottom_width;
1348 * wWindowConfigure() will init the client window's size
1349 * (wwin->client.{width,height}) and all other geometry
1350 * related variables (frame_x,frame_y)
1352 wWindowConfigure(wwin, x, y, width, height);
1354 /* to make sure the window receives it's new position after reparenting */
1355 wWindowSynthConfigureNotify(wwin);
1358 *--------------------------------------------------
1360 * Setup descriptors and save window to internal
1361 * lists
1363 *--------------------------------------------------
1366 if (wwin->main_window!=None) {
1367 WApplication *app;
1368 WWindow *leader;
1370 /* Leader windows do not necessary set themselves as leaders.
1371 * If this is the case, point the leader of this window to
1372 * itself */
1373 leader = wWindowFor(wwin->main_window);
1374 if (leader && leader->main_window==None) {
1375 leader->main_window = leader->client_win;
1377 app = wApplicationCreate(wwin);
1378 if (app) {
1379 app->last_workspace = workspace;
1382 * Do application specific stuff, like setting application
1383 * wide attributes.
1386 if (wwin->flags.hidden) {
1387 /* if the window was set to hidden because it was hidden
1388 * in a previous incarnation and that state was restored */
1389 app->flags.hidden = 1;
1390 } else if (app->flags.hidden) {
1391 if (WFLAGP(app->main_window_desc, start_hidden)) {
1392 wwin->flags.hidden = 1;
1393 } else {
1394 wUnhideApplication(app, False, False);
1395 raise = True;
1401 /* setup the frame descriptor */
1402 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1403 wwin->frame->core->descriptor.parent = wwin;
1404 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1406 /* don't let windows go away if we die */
1407 XAddToSaveSet(dpy, window);
1409 XLowerWindow(dpy, window);
1411 /* if window is in this workspace and should be mapped, then map it */
1412 if (!wwin->flags.miniaturized
1413 && (workspace == scr->current_workspace || IS_OMNIPRESENT(wwin))
1414 && !wwin->flags.hidden && !withdraw) {
1416 /* The following "if" is to avoid crashing of clients that expect
1417 * WM_STATE set before they get mapped. Else WM_STATE is set later,
1418 * after the return from this function.
1420 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
1421 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
1422 } else {
1423 wClientSetState(wwin, NormalState, None);
1426 #if 0
1427 /* if not auto focus, then map the window under the currently
1428 * focused window */
1429 #define _WIDTH(w) (w)->frame->core->width
1430 #define _HEIGHT(w) (w)->frame->core->height
1431 if (!wPreferences.auto_focus && scr->focused_window
1432 && !scr->flags.startup && !transientOwner
1433 && ((wWindowObscuresWindow(wwin, scr->focused_window)
1434 && (_WIDTH(wwin) > (_WIDTH(scr->focused_window)*5)/3
1435 || _HEIGHT(wwin) > (_HEIGHT(scr->focused_window)*5)/3)
1436 && WINDOW_LEVEL(scr->focused_window) == WINDOW_LEVEL(wwin))
1437 || wwin->flags.maximized)) {
1438 MoveInStackListUnder(scr->focused_window->frame->core,
1439 wwin->frame->core);
1441 #undef _WIDTH
1442 #undef _HEIGHT
1444 #endif
1446 if (wPreferences.superfluous && !wPreferences.no_animations
1447 && !scr->flags.startup &&
1448 (wwin->transient_for==None || wwin->transient_for==scr->root_win)
1450 * The brain damaged idiotic non-click to focus modes will
1451 * have trouble with this because:
1453 * 1. window is created and mapped by the client
1454 * 2. window is mapped by wmaker in small size
1455 * 3. window is animated to grow to normal size
1456 * 4. this function returns to normal event loop
1457 * 5. eventually, the EnterNotify event that would trigger
1458 * the window focusing (if the mouse is over that window)
1459 * will be processed by wmaker.
1460 * But since this event will be rather delayed
1461 * (step 3 has a large delay) the time when the event ocurred
1462 * and when it is processed, the client that owns that window
1463 * will reject the XSetInputFocus() for it.
1465 && (wPreferences.focus_mode==WKF_CLICK
1466 || wPreferences.auto_focus)) {
1467 DoWindowBirth(wwin);
1470 wWindowMap(wwin);
1473 /* setup stacking descriptor */
1474 if (transientOwner) {
1475 wwin->frame->core->stacking->child_of = transientOwner->frame->core;
1476 } else {
1477 wwin->frame->core->stacking->child_of = NULL;
1481 if (!scr->focused_window) {
1482 /* first window on the list */
1483 wwin->next = NULL;
1484 wwin->prev = NULL;
1485 scr->focused_window = wwin;
1486 } else {
1487 WWindow *tmp;
1489 /* add window at beginning of focus window list */
1490 tmp = scr->focused_window;
1491 while (tmp->prev)
1492 tmp = tmp->prev;
1493 tmp->prev = wwin;
1494 wwin->next = tmp;
1495 wwin->prev = NULL;
1498 /* raise is set to true if we un-hid the app when this window was born.
1499 * we raise, else old windows of this app will be above this new one. */
1500 if (raise) {
1501 wRaiseFrame(wwin->frame->core);
1504 /* Update name must come after WApplication stuff is done */
1505 wWindowUpdateName(wwin, title);
1506 if (title)
1507 XFree(title);
1509 XUngrabServer(dpy);
1512 *--------------------------------------------------
1514 * Final preparations before window is ready to go
1516 *--------------------------------------------------
1519 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1522 if (!wwin->flags.miniaturized && workspace == scr->current_workspace
1523 && !wwin->flags.hidden) {
1524 if (((transientOwner && transientOwner->flags.focused)
1525 || wPreferences.auto_focus) && !WFLAGP(wwin, no_focusable))
1526 wSetFocusTo(scr, wwin);
1528 wWindowResetMouseGrabs(wwin);
1530 if (!WFLAGP(wwin, no_bind_keys)) {
1531 wWindowSetKeyGrabs(wwin);
1535 WMPostNotificationName(WMNManaged, wwin, NULL);
1538 wColormapInstallForWindow(scr, scr->cmap_window);
1542 *------------------------------------------------------------
1543 * Setup Notification Observers
1544 *------------------------------------------------------------
1546 WMAddNotificationObserver(appearanceObserver, wwin,
1547 WNWindowAppearanceSettingsChanged, wwin);
1551 *--------------------------------------------------
1553 * Cleanup temporary stuff
1555 *--------------------------------------------------
1558 if (win_state)
1559 wWindowDeleteSavedState(win_state);
1561 /* If the window must be withdrawed, then do it now.
1562 * Must do some optimization, 'though */
1563 if (withdraw) {
1564 wwin->flags.mapped = 0;
1565 wClientSetState(wwin, WithdrawnState, None);
1566 wUnmanageWindow(wwin, True, False);
1567 wwin = NULL;
1570 return wwin;
1577 WWindow*
1578 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1579 char *title, int x, int y, int width, int height)
1581 WWindow *wwin;
1582 int foo;
1584 wwin = wWindowCreate();
1586 WMAddNotificationObserver(appearanceObserver, wwin,
1587 WNWindowAppearanceSettingsChanged, wwin);
1589 wwin->flags.internal_window = 1;
1591 WSETUFLAG(wwin, omnipresent, 1);
1592 WSETUFLAG(wwin, no_shadeable, 1);
1593 WSETUFLAG(wwin, no_resizable, 1);
1594 WSETUFLAG(wwin, no_miniaturizable, 1);
1596 wwin->focus_mode = WFM_PASSIVE;
1598 wwin->client_win = window;
1599 wwin->screen_ptr = scr;
1601 wwin->transient_for = owner;
1603 wwin->client.x = x;
1604 wwin->client.y = y;
1605 wwin->client.width = width;
1606 wwin->client.height = height;
1608 wwin->frame_x = wwin->client.x;
1609 wwin->frame_y = wwin->client.y;
1612 foo = WFF_RIGHT_BUTTON|WFF_BORDER;
1613 foo |= WFF_TITLEBAR;
1614 #ifdef XKB_BUTTON_HINT
1615 foo |= WFF_LANGUAGE_BUTTON;
1616 #endif
1618 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1619 wwin->frame_x, wwin->frame_y,
1620 width, height,
1621 &wPreferences.window_title_clearance, foo,
1622 scr->window_title_texture,
1623 scr->resizebar_texture,
1624 scr->window_title_color,
1625 &scr->title_font);
1627 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1629 wwin->frame->flags.is_client_window_frame = 1;
1630 wwin->frame->flags.justification = wPreferences.title_justification;
1632 wFrameWindowChangeTitle(wwin->frame, title);
1634 /* setup button images */
1635 wWindowUpdateButtonImages(wwin);
1637 /* hide buttons */
1638 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1640 wwin->frame->child = wwin;
1642 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1644 #ifdef XKB_BUTTON_HINT
1645 if (wPreferences.modelock)
1646 wwin->frame->on_click_language = windowLanguageClick;
1647 #endif
1649 wwin->frame->on_click_right = windowCloseClick;
1651 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1652 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1654 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1656 wwin->client.y += wwin->frame->top_width;
1657 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1658 0, wwin->frame->top_width);
1660 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1661 wwin->client.width, wwin->client.height);
1663 /* setup the frame descriptor */
1664 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1665 wwin->frame->core->descriptor.parent = wwin;
1666 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1669 XLowerWindow(dpy, window);
1670 XMapSubwindows(dpy, wwin->frame->core->window);
1672 /* setup stacking descriptor */
1673 if (wwin->transient_for!=None && wwin->transient_for!=scr->root_win) {
1674 WWindow *tmp;
1675 tmp = wWindowFor(wwin->transient_for);
1676 if (tmp)
1677 wwin->frame->core->stacking->child_of = tmp->frame->core;
1678 } else {
1679 wwin->frame->core->stacking->child_of = NULL;
1683 if (!scr->focused_window) {
1684 /* first window on the list */
1685 wwin->next = NULL;
1686 wwin->prev = NULL;
1687 scr->focused_window = wwin;
1688 } else {
1689 WWindow *tmp;
1691 /* add window at beginning of focus window list */
1692 tmp = scr->focused_window;
1693 while (tmp->prev)
1694 tmp = tmp->prev;
1695 tmp->prev = wwin;
1696 wwin->next = tmp;
1697 wwin->prev = NULL;
1700 if (wwin->flags.is_gnustep == 0)
1701 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1703 /* if (wPreferences.auto_focus)*/
1704 wSetFocusTo(scr, wwin);
1706 wWindowResetMouseGrabs(wwin);
1708 wWindowSetKeyGrabs(wwin);
1710 return wwin;
1715 *----------------------------------------------------------------------
1716 * wUnmanageWindow--
1717 * Removes the frame window from a window and destroys all data
1718 * related to it. The window will be reparented back to the root window
1719 * if restore is True.
1721 * Side effects:
1722 * Everything related to the window is destroyed and the window
1723 * is removed from the window lists. Focus is set to the previous on the
1724 * window list.
1725 *----------------------------------------------------------------------
1727 void
1728 wUnmanageWindow(WWindow *wwin, Bool restore, Bool destroyed)
1730 WCoreWindow *frame = wwin->frame->core;
1731 WWindow *owner = NULL;
1732 WWindow *newFocusedWindow = NULL;
1733 int wasFocused;
1734 WScreen *scr = wwin->screen_ptr;
1737 /* First close attribute editor window if open */
1738 if (wwin->flags.inspector_open) {
1739 wCloseInspectorForWindow(wwin);
1742 /* Close window menu if it's open for this window */
1743 if (wwin->flags.menu_open_for_me) {
1744 CloseWindowMenu(scr);
1747 if (!destroyed) {
1748 if (!wwin->flags.internal_window)
1749 XRemoveFromSaveSet(dpy, wwin->client_win);
1751 XSelectInput(dpy, wwin->client_win, NoEventMask);
1753 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
1754 XUngrabKey(dpy, AnyKey, AnyModifier, wwin->client_win);
1757 XUnmapWindow(dpy, frame->window);
1759 XUnmapWindow(dpy, wwin->client_win);
1761 /* deselect window */
1762 wSelectWindow(wwin, False);
1764 /* remove all pending events on window */
1765 /* I think this only matters for autoraise */
1766 if (wPreferences.raise_delay)
1767 WMDeleteTimerWithClientData(wwin->frame->core);
1769 XFlush(dpy);
1771 /* reparent the window back to the root */
1772 if (restore)
1773 wClientRestore(wwin);
1775 if (wwin->transient_for!=scr->root_win) {
1776 owner = wWindowFor(wwin->transient_for);
1777 if (owner) {
1778 if (!owner->flags.semi_focused) {
1779 owner = NULL;
1780 } else {
1781 owner->flags.semi_focused = 0;
1786 wasFocused = wwin->flags.focused;
1788 /* remove from window focus list */
1789 if (!wwin->prev && !wwin->next) {
1790 /* was the only window */
1791 scr->focused_window = NULL;
1792 newFocusedWindow = NULL;
1793 } else {
1794 WWindow *tmp;
1796 if (wwin->prev)
1797 wwin->prev->next = wwin->next;
1798 if (wwin->next)
1799 wwin->next->prev = wwin->prev;
1800 else {
1801 scr->focused_window = wwin->prev;
1802 scr->focused_window->next = NULL;
1805 if (wPreferences.focus_mode==WKF_CLICK) {
1807 /* if in click to focus mode and the window
1808 * was a transient, focus the owner window
1810 tmp = NULL;
1811 if (wPreferences.focus_mode==WKF_CLICK) {
1812 tmp = wWindowFor(wwin->transient_for);
1813 if (tmp && (!tmp->flags.mapped || WFLAGP(tmp, no_focusable))) {
1814 tmp = NULL;
1817 /* otherwise, focus the next one in the focus list */
1818 if (!tmp) {
1819 tmp = scr->focused_window;
1820 while (tmp) { /* look for one in the window list first */
1821 if (!WFLAGP(tmp, no_focusable) && !WFLAGP(tmp, skip_window_list)
1822 && (tmp->flags.mapped || tmp->flags.shaded))
1823 break;
1824 tmp = tmp->prev;
1826 if (!tmp) { /* if unsuccessful, choose any focusable window */
1827 tmp = scr->focused_window;
1828 while (tmp) {
1829 if (!WFLAGP(tmp, no_focusable)
1830 && (tmp->flags.mapped || tmp->flags.shaded))
1831 break;
1832 tmp = tmp->prev;
1837 newFocusedWindow = tmp;
1839 } else if (wPreferences.focus_mode==WKF_SLOPPY) {
1840 unsigned int mask;
1841 int foo;
1842 Window bar, win;
1844 /* This is to let the root window get the keyboard input
1845 * if Sloppy focus mode and no other window get focus.
1846 * This way keybindings will not freeze.
1848 tmp = NULL;
1849 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1850 &foo, &foo, &foo, &foo, &mask))
1851 tmp = wWindowFor(win);
1852 if (tmp == wwin)
1853 tmp = NULL;
1854 newFocusedWindow = tmp;
1855 } else {
1856 newFocusedWindow = NULL;
1860 if (!wwin->flags.internal_window) {
1861 WMPostNotificationName(WMNUnmanaged, wwin, NULL);
1864 #ifdef DEBUG
1865 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1866 (unsigned)frame->window);
1867 #endif
1869 if (wasFocused) {
1870 if (newFocusedWindow != owner && owner) {
1871 if (wwin->flags.is_gnustep == 0)
1872 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1874 wSetFocusTo(scr, newFocusedWindow);
1876 wWindowDestroy(wwin);
1877 XFlush(dpy);
1881 void
1882 wWindowMap(WWindow *wwin)
1884 XMapWindow(dpy, wwin->frame->core->window);
1885 if (!wwin->flags.shaded) {
1886 /* window will be remapped when getting MapNotify */
1887 XSelectInput(dpy, wwin->client_win,
1888 wwin->event_mask & ~StructureNotifyMask);
1889 XMapWindow(dpy, wwin->client_win);
1890 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1892 wwin->flags.mapped = 1;
1897 void
1898 wWindowUnmap(WWindow *wwin)
1900 wwin->flags.mapped = 0;
1902 /* prevent window withdrawal when getting UnmapNotify */
1903 XSelectInput(dpy, wwin->client_win,
1904 wwin->event_mask & ~StructureNotifyMask);
1905 XUnmapWindow(dpy, wwin->client_win);
1906 XSelectInput(dpy, wwin->client_win, wwin->event_mask);
1908 XUnmapWindow(dpy, wwin->frame->core->window);
1913 void
1914 wWindowFocus(WWindow *wwin, WWindow *owin)
1916 WWindow *nowner;
1917 WWindow *oowner;
1919 #ifdef KEEP_XKB_LOCK_STATUS
1920 if (wPreferences.modelock) {
1921 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->frame->languagemode);
1923 #endif /* KEEP_XKB_LOCK_STATUS */
1925 wwin->flags.semi_focused = 0;
1927 if (wwin->flags.is_gnustep == 0)
1928 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1930 wwin->flags.focused = 1;
1932 wWindowResetMouseGrabs(wwin);
1934 WMPostNotificationName(WMNChangedFocus, wwin, (void*)True);
1936 if (owin == wwin || !owin)
1937 return;
1939 nowner = wWindowFor(wwin->transient_for);
1941 /* new window is a transient for the old window */
1942 if (nowner == owin) {
1943 owin->flags.semi_focused = 1;
1944 wWindowUnfocus(nowner);
1945 return;
1948 oowner = wWindowFor(owin->transient_for);
1950 /* new window is owner of old window */
1951 if (wwin == oowner) {
1952 wWindowUnfocus(owin);
1953 return;
1956 if (!nowner) {
1957 wWindowUnfocus(owin);
1958 return;
1961 /* new window has same owner of old window */
1962 if (oowner == nowner) {
1963 /* prevent unfocusing of owner */
1964 oowner->flags.semi_focused = 0;
1965 wWindowUnfocus(owin);
1966 oowner->flags.semi_focused = 1;
1968 return;
1971 /* nowner != NULL && oowner != nowner */
1972 nowner->flags.semi_focused = 1;
1973 wWindowUnfocus(nowner);
1974 wWindowUnfocus(owin);
1978 void
1979 wWindowUnfocus(WWindow *wwin)
1981 CloseWindowMenu(wwin->screen_ptr);
1983 if (wwin->flags.is_gnustep == 0)
1984 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1985 ? WS_PFOCUSED : WS_UNFOCUSED);
1987 if (wwin->transient_for!=None
1988 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1989 WWindow *owner;
1990 owner = wWindowFor(wwin->transient_for);
1991 if (owner && owner->flags.semi_focused) {
1992 owner->flags.semi_focused = 0;
1993 if (owner->flags.mapped || owner->flags.shaded) {
1994 wWindowUnfocus(owner);
1995 wFrameWindowPaint(owner->frame);
1999 wwin->flags.focused = 0;
2001 wWindowResetMouseGrabs(wwin);
2003 WMPostNotificationName(WMNChangedFocus, wwin, (void*)False);
2007 void
2008 wWindowUpdateName(WWindow *wwin, char *newTitle)
2010 char *title;
2012 if (!wwin->frame)
2013 return;
2015 wwin->flags.wm_name_changed = 1;
2017 if (!newTitle) {
2018 /* the hint was removed */
2019 title = DEF_WINDOW_TITLE;
2020 } else {
2021 title = newTitle;
2024 if (wFrameWindowChangeTitle(wwin->frame, title)) {
2025 WMPostNotificationName(WMNChangedName, wwin, NULL);
2032 *----------------------------------------------------------------------
2034 * wWindowConstrainSize--
2035 * Constrains size for the client window, taking the maximal size,
2036 * window resize increments and other size hints into account.
2038 * Returns:
2039 * The closest size to what was given that the client window can
2040 * have.
2042 *----------------------------------------------------------------------
2044 void
2045 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
2047 int width = *nwidth;
2048 int height = *nheight;
2049 int winc = 1;
2050 int hinc = 1;
2051 int minW = 1, minH = 1;
2052 int maxW = wwin->screen_ptr->scr_width*2;
2053 int maxH = wwin->screen_ptr->scr_height*2;
2054 int minAX = -1, minAY = -1;
2055 int maxAX = -1, maxAY = -1;
2056 int baseW = 0;
2057 int baseH = 0;
2059 if (wwin->normal_hints) {
2060 winc = wwin->normal_hints->width_inc;
2061 hinc = wwin->normal_hints->height_inc;
2062 minW = wwin->normal_hints->min_width;
2063 minH = wwin->normal_hints->min_height;
2064 maxW = wwin->normal_hints->max_width;
2065 maxH = wwin->normal_hints->max_height;
2066 if (wwin->normal_hints->flags & PAspect) {
2067 minAX = wwin->normal_hints->min_aspect.x;
2068 minAY = wwin->normal_hints->min_aspect.y;
2069 maxAX = wwin->normal_hints->max_aspect.x;
2070 maxAY = wwin->normal_hints->max_aspect.y;
2073 baseW = wwin->normal_hints->base_width;
2074 baseH = wwin->normal_hints->base_height;
2077 if (width < minW)
2078 width = minW;
2079 if (height < minH)
2080 height = minH;
2082 if (width > maxW)
2083 width = maxW;
2084 if (height > maxH)
2085 height = maxH;
2087 /* aspect ratio code borrowed from olwm */
2088 if (minAX > 0) {
2089 /* adjust max aspect ratio */
2090 if (!(maxAX == 1 && maxAY == 1) && width * maxAY > height * maxAX) {
2091 if (maxAX > maxAY) {
2092 height = (width * maxAY) / maxAX;
2093 if (height > maxH) {
2094 height = maxH;
2095 width = (height * maxAX) / maxAY;
2097 } else {
2098 width = (height * maxAX) / maxAY;
2099 if (width > maxW) {
2100 width = maxW;
2101 height = (width * maxAY) / maxAX;
2106 /* adjust min aspect ratio */
2107 if (!(minAX == 1 && minAY == 1) && width * minAY < height * minAX) {
2108 if (minAX > minAY) {
2109 height = (width * minAY) / minAX;
2110 if (height < minH) {
2111 height = minH;
2112 width = (height * minAX) / minAY;
2114 } else {
2115 width = (height * minAX) / minAY;
2116 if (width < minW) {
2117 width = minW;
2118 height = (width * minAY) / minAX;
2124 if (baseW != 0) {
2125 width = (((width - baseW) / winc) * winc) + baseW;
2126 } else {
2127 width = (((width - minW) / winc) * winc) + minW;
2130 if (baseH != 0) {
2131 height = (((height - baseH) / hinc) * hinc) + baseH;
2132 } else {
2133 height = (((height - minH) / hinc) * hinc) + minH;
2136 /* broken stupid apps may cause preposterous values for these.. */
2137 if (width > 0)
2138 *nwidth = width;
2139 if (height > 0)
2140 *nheight = height;
2144 void
2145 wWindowCropSize(WWindow *wwin, int maxW, int maxH,
2146 int *width, int *height)
2148 int baseW = 0, baseH = 0;
2149 int winc = 1, hinc = 1;
2151 if (wwin->normal_hints) {
2152 baseW = wwin->normal_hints->base_width;
2153 baseH = wwin->normal_hints->base_height;
2155 winc = wwin->normal_hints->width_inc;
2156 hinc = wwin->normal_hints->height_inc;
2159 if (*width > maxW)
2160 *width = maxW - (maxW - baseW) % winc;
2162 if (*height > maxH)
2163 *height = maxH - (maxH - baseH) % hinc;
2167 void
2168 wWindowChangeWorkspace(WWindow *wwin, int workspace)
2170 WScreen *scr = wwin->screen_ptr;
2171 WApplication *wapp;
2172 int unmap = 0;
2174 if (workspace >= scr->workspace_count || workspace < 0
2175 || workspace == wwin->frame->workspace)
2176 return;
2178 if (workspace != scr->current_workspace) {
2179 /* Sent to other workspace. Unmap window */
2180 if ((wwin->flags.mapped
2181 || wwin->flags.shaded
2182 || (wwin->flags.miniaturized && !wPreferences.sticky_icons))
2183 && !IS_OMNIPRESENT(wwin) && !wwin->flags.changing_workspace) {
2185 wapp = wApplicationOf(wwin->main_window);
2186 if (wapp) {
2187 wapp->last_workspace = workspace;
2189 if (wwin->flags.miniaturized) {
2190 if (wwin->icon) {
2191 XUnmapWindow(dpy, wwin->icon->core->window);
2192 wwin->icon->mapped = 0;
2194 } else {
2195 unmap = 1;
2196 wSetFocusTo(scr, NULL);
2199 } else {
2200 /* brought to current workspace. Map window */
2201 if (wwin->flags.miniaturized && !wPreferences.sticky_icons) {
2202 if (wwin->icon) {
2203 XMapWindow(dpy, wwin->icon->core->window);
2204 wwin->icon->mapped = 1;
2206 } else if (!wwin->flags.mapped &&
2207 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
2208 wWindowMap(wwin);
2211 if (!IS_OMNIPRESENT(wwin)) {
2212 int oldWorkspace = wwin->frame->workspace;
2214 wwin->frame->workspace = workspace;
2216 WMPostNotificationName(WMNChangedWorkspace, wwin, (void*)oldWorkspace);
2219 if (unmap) {
2220 wWindowUnmap(wwin);
2225 void
2226 wWindowSynthConfigureNotify(WWindow *wwin)
2228 XEvent sevent;
2230 sevent.type = ConfigureNotify;
2231 sevent.xconfigure.display = dpy;
2232 sevent.xconfigure.event = wwin->client_win;
2233 sevent.xconfigure.window = wwin->client_win;
2235 sevent.xconfigure.x = wwin->client.x;
2236 sevent.xconfigure.y = wwin->client.y;
2237 sevent.xconfigure.width = wwin->client.width;
2238 sevent.xconfigure.height = wwin->client.height;
2240 sevent.xconfigure.border_width = wwin->old_border_width;
2241 if (!HAS_TITLEBAR(wwin))
2242 sevent.xconfigure.above = None;
2243 else
2244 sevent.xconfigure.above = wwin->frame->titlebar->window;
2246 sevent.xconfigure.override_redirect = False;
2247 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
2248 XFlush(dpy);
2253 *----------------------------------------------------------------------
2254 * wWindowConfigure--
2255 * Configures the frame, decorations and client window to the
2256 * specified geometry. The geometry is not checked for validity,
2257 * wWindowConstrainSize() must be used for that.
2258 * The size parameters are for the client window, but the position is
2259 * for the frame.
2260 * The client window receives a ConfigureNotify event, according
2261 * to what ICCCM says.
2263 * Returns:
2264 * None
2266 * Side effects:
2267 * Window size and position are changed and client window receives
2268 * a ConfigureNotify event.
2269 *----------------------------------------------------------------------
2271 void
2272 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
2273 WWindow *wwin;
2274 int req_x, req_y; /* new position of the frame */
2275 int req_width, req_height; /* new size of the client */
2277 int synth_notify = False;
2278 int resize;
2280 resize = (req_width!=wwin->client.width
2281 || req_height!=wwin->client.height);
2283 * if the window is being moved but not resized then
2284 * send a synthetic ConfigureNotify
2286 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
2287 synth_notify = True;
2290 if (WFLAGP(wwin, dont_move_off))
2291 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2292 req_width, req_height);
2293 if (resize) {
2294 if (req_width < MIN_WINDOW_SIZE)
2295 req_width = MIN_WINDOW_SIZE;
2296 if (req_height < MIN_WINDOW_SIZE)
2297 req_height = MIN_WINDOW_SIZE;
2299 /* If growing, resize inner part before frame,
2300 * if shrinking, resize frame before.
2301 * This will prevent the frame (that can have a different color)
2302 * to be exposed, causing flicker */
2303 if (req_height > wwin->frame->core->height
2304 || req_width > wwin->frame->core->width)
2305 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2307 if (wwin->flags.shaded) {
2308 wFrameWindowConfigure(wwin->frame, req_x, req_y,
2309 req_width, wwin->frame->core->height);
2310 wwin->old_geometry.height = req_height;
2311 } else {
2312 int h;
2314 h = req_height + wwin->frame->top_width
2315 + wwin->frame->bottom_width;
2317 wFrameWindowConfigure(wwin->frame, req_x, req_y, req_width, h);
2320 if (!(req_height > wwin->frame->core->height
2321 || req_width > wwin->frame->core->width))
2322 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
2324 wwin->client.x = req_x;
2325 wwin->client.y = req_y + wwin->frame->top_width;
2326 wwin->client.width = req_width;
2327 wwin->client.height = req_height;
2328 } else {
2329 wwin->client.x = req_x;
2330 wwin->client.y = req_y + wwin->frame->top_width;
2332 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2334 wwin->frame_x = req_x;
2335 wwin->frame_y = req_y;
2336 if (HAS_BORDER(wwin)) {
2337 wwin->client.x += FRAME_BORDER_WIDTH;
2338 wwin->client.y += FRAME_BORDER_WIDTH;
2341 #ifdef SHAPE
2342 if (wShapeSupported && wwin->flags.shaped && resize) {
2343 wWindowSetShape(wwin);
2345 #endif
2347 if (synth_notify)
2348 wWindowSynthConfigureNotify(wwin);
2349 XFlush(dpy);
2353 void
2354 wWindowMove(wwin, req_x, req_y)
2355 WWindow *wwin;
2356 int req_x, req_y; /* new position of the frame */
2358 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2359 int synth_notify = False;
2361 /* Send a synthetic ConfigureNotify event for every window movement. */
2362 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
2363 synth_notify = True;
2365 #else
2366 /* A single synthetic ConfigureNotify event is sent at the end of
2367 * a completed (opaque) movement in moveres.c */
2368 #endif
2370 if (WFLAGP(wwin, dont_move_off))
2371 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
2372 wwin->frame->core->width, wwin->frame->core->height);
2374 wwin->client.x = req_x;
2375 wwin->client.y = req_y + wwin->frame->top_width;
2376 if (HAS_BORDER(wwin)) {
2377 wwin->client.x += FRAME_BORDER_WIDTH;
2378 wwin->client.y += FRAME_BORDER_WIDTH;
2381 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
2383 wwin->frame_x = req_x;
2384 wwin->frame_y = req_y;
2386 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
2387 if (synth_notify)
2388 wWindowSynthConfigureNotify(wwin);
2389 #endif
2393 void
2394 wWindowUpdateButtonImages(WWindow *wwin)
2396 WScreen *scr = wwin->screen_ptr;
2397 Pixmap pixmap, mask;
2398 WFrameWindow *fwin = wwin->frame;
2400 if (!HAS_TITLEBAR(wwin))
2401 return;
2403 /* miniaturize button */
2405 if (!WFLAGP(wwin, no_miniaturize_button)) {
2406 if (wwin->wm_gnustep_attr
2407 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
2408 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
2410 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
2411 mask = wwin->wm_gnustep_attr->miniaturize_mask;
2412 } else {
2413 mask = None;
2416 if (fwin->lbutton_image
2417 && (fwin->lbutton_image->image != pixmap
2418 || fwin->lbutton_image->mask != mask)) {
2419 wPixmapDestroy(fwin->lbutton_image);
2420 fwin->lbutton_image = NULL;
2423 if (!fwin->lbutton_image) {
2424 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
2425 fwin->lbutton_image->client_owned = 1;
2426 fwin->lbutton_image->client_owned_mask = 1;
2428 } else {
2429 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
2430 wPixmapDestroy(fwin->lbutton_image);
2432 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
2436 #ifdef XKB_BUTTON_HINT
2437 if (!WFLAGP(wwin, no_language_button)) {
2438 if (fwin->languagebutton_image &&
2439 !fwin->languagebutton_image->shared) {
2440 wPixmapDestroy(fwin->languagebutton_image);
2442 fwin->languagebutton_image =
2443 scr->b_pixmaps[WBUT_XKBGROUP1 + fwin->languagemode];
2445 #endif
2447 /* close button */
2449 /* redefine WFLAGP to MGFLAGP to allow broken close operation */
2450 #define MGFLAGP(wwin, FLAG) (wwin)->client_flags.FLAG
2452 if (!WFLAGP(wwin, no_close_button)) {
2453 if (wwin->wm_gnustep_attr
2454 && wwin->wm_gnustep_attr->flags & GSClosePixmapAttr) {
2455 pixmap = wwin->wm_gnustep_attr->close_pixmap;
2457 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr)
2458 mask = wwin->wm_gnustep_attr->close_mask;
2459 else
2460 mask = None;
2462 if (fwin->rbutton_image && (fwin->rbutton_image->image != pixmap
2463 || fwin->rbutton_image->mask != mask)) {
2464 wPixmapDestroy(fwin->rbutton_image);
2465 fwin->rbutton_image = NULL;
2468 if (!fwin->rbutton_image) {
2469 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
2470 fwin->rbutton_image->client_owned = 1;
2471 fwin->rbutton_image->client_owned_mask = 1;
2474 } else if (WFLAGP(wwin, kill_close)) {
2476 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2477 wPixmapDestroy(fwin->rbutton_image);
2479 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
2481 } else if (MGFLAGP(wwin, broken_close)) {
2483 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2484 wPixmapDestroy(fwin->rbutton_image);
2486 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
2488 } else {
2490 if (fwin->rbutton_image && !fwin->rbutton_image->shared)
2491 wPixmapDestroy(fwin->rbutton_image);
2493 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
2497 /* force buttons to be redrawn */
2498 fwin->flags.need_texture_change = 1;
2499 wFrameWindowPaint(fwin);
2504 *---------------------------------------------------------------------------
2505 * wWindowConfigureBorders--
2506 * Update window border configuration according to attribute flags.
2508 *---------------------------------------------------------------------------
2510 void
2511 wWindowConfigureBorders(WWindow *wwin)
2513 if (wwin->frame) {
2514 int flags;
2515 int newy, oldh;
2517 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
2519 #ifdef XKB_BUTTON_HINT
2520 flags |= WFF_LANGUAGE_BUTTON;
2521 #endif
2523 if (HAS_TITLEBAR(wwin))
2524 flags |= WFF_TITLEBAR;
2525 if (HAS_RESIZEBAR(wwin) && IS_RESIZABLE(wwin))
2526 flags |= WFF_RESIZEBAR;
2527 if (HAS_BORDER(wwin))
2528 flags |= WFF_BORDER;
2529 if (wwin->flags.shaded)
2530 flags |= WFF_IS_SHADED;
2532 oldh = wwin->frame->top_width;
2533 wFrameWindowUpdateBorders(wwin->frame, flags);
2534 if (oldh != wwin->frame->top_width) {
2535 newy = wwin->frame_y + oldh - wwin->frame->top_width;
2537 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
2538 wWindowConfigure(wwin, wwin->frame_x, newy,
2539 wwin->client.width, wwin->client.height);
2542 flags = 0;
2543 if (!WFLAGP(wwin, no_miniaturize_button)
2544 && wwin->frame->flags.hide_left_button)
2545 flags |= WFF_LEFT_BUTTON;
2547 #ifdef XKB_BUTTON_HINT
2548 if (!WFLAGP(wwin, no_language_button)
2549 && wwin->frame->flags.hide_language_button) {
2550 flags |= WFF_LANGUAGE_BUTTON;
2552 #endif
2554 if (!WFLAGP(wwin, no_close_button)
2555 && wwin->frame->flags.hide_right_button)
2556 flags |= WFF_RIGHT_BUTTON;
2558 if (flags!=0) {
2559 wWindowUpdateButtonImages(wwin);
2560 wFrameWindowShowButton(wwin->frame, flags);
2563 flags = 0;
2564 if (WFLAGP(wwin, no_miniaturize_button)
2565 && !wwin->frame->flags.hide_left_button)
2566 flags |= WFF_LEFT_BUTTON;
2568 #ifdef XKB_BUTTON_HINT
2569 if (WFLAGP(wwin, no_language_button)
2570 && !wwin->frame->flags.hide_language_button)
2571 flags |= WFF_LANGUAGE_BUTTON;
2572 #endif
2574 if (WFLAGP(wwin, no_close_button)
2575 && !wwin->frame->flags.hide_right_button)
2576 flags |= WFF_RIGHT_BUTTON;
2578 if (flags!=0)
2579 wFrameWindowHideButton(wwin->frame, flags);
2581 #ifdef SHAPE
2582 if (wShapeSupported && wwin->flags.shaped) {
2583 wWindowSetShape(wwin);
2585 #endif
2590 void
2591 wWindowSaveState(WWindow *wwin)
2593 CARD32 data[10];
2594 int i;
2596 memset(data, 0, sizeof(CARD32)*10);
2597 data[0] = wwin->frame->workspace;
2598 data[1] = wwin->flags.miniaturized;
2599 data[2] = wwin->flags.shaded;
2600 data[3] = wwin->flags.hidden;
2601 data[4] = wwin->flags.maximized;
2602 if (wwin->flags.maximized == 0) {
2603 data[5] = wwin->frame_x;
2604 data[6] = wwin->frame_y;
2605 data[7] = wwin->frame->core->width;
2606 data[8] = wwin->frame->core->height;
2607 } else {
2608 data[5] = wwin->old_geometry.x;
2609 data[6] = wwin->old_geometry.y;
2610 data[7] = wwin->old_geometry.width;
2611 data[8] = wwin->old_geometry.height;
2614 for (i = 0; i < MAX_WINDOW_SHORTCUTS; i++) {
2615 if (wwin->screen_ptr->shortcutWindows[i] &&
2616 WMCountInArray(wwin->screen_ptr->shortcutWindows[i], wwin))
2617 data[9] |= 1<<i;
2619 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
2620 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
2621 (unsigned char *)data, 10);
2625 static int
2626 getSavedState(Window window, WSavedState **state)
2628 Atom type_ret;
2629 int fmt_ret;
2630 unsigned long nitems_ret;
2631 unsigned long bytes_after_ret;
2632 CARD32 *data;
2634 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 10,
2635 True, _XA_WINDOWMAKER_STATE,
2636 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
2637 (unsigned char **)&data)!=Success || !data)
2638 return 0;
2640 *state = wmalloc(sizeof(WSavedState));
2642 (*state)->workspace = data[0];
2643 (*state)->miniaturized = data[1];
2644 (*state)->shaded = data[2];
2645 (*state)->hidden = data[3];
2646 (*state)->maximized = data[4];
2647 (*state)->x = data[5];
2648 (*state)->y = data[6];
2649 (*state)->w = data[7];
2650 (*state)->h = data[8];
2651 (*state)->window_shortcuts = data[9];
2653 XFree(data);
2655 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
2656 return 1;
2657 else
2658 return 0;
2662 #ifdef SHAPE
2663 void
2664 wWindowClearShape(WWindow *wwin)
2666 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
2667 0, wwin->frame->top_width, None, ShapeSet);
2668 XFlush(dpy);
2671 void
2672 wWindowSetShape(WWindow *wwin)
2674 XRectangle rect[2];
2675 int count;
2676 #ifdef OPTIMIZE_SHAPE
2677 XRectangle *rects;
2678 XRectangle *urec;
2679 int ordering;
2681 /* only shape is the client's */
2682 if (!HAS_TITLEBAR(wwin) && !HAS_RESIZEBAR(wwin)) {
2683 goto alt_code;
2686 /* Get array of rectangles describing the shape mask */
2687 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
2688 &count, &ordering);
2689 if (!rects) {
2690 goto alt_code;
2693 urec = malloc(sizeof(XRectangle)*(count+2));
2694 if (!urec) {
2695 XFree(rects);
2696 goto alt_code;
2699 /* insert our decoration rectangles in the rect list */
2700 memcpy(urec, rects, sizeof(XRectangle)*count);
2701 XFree(rects);
2703 if (HAS_TITLEBAR(wwin)) {
2704 urec[count].x = -1;
2705 urec[count].y = -1 - wwin->frame->top_width;
2706 urec[count].width = wwin->frame->core->width + 2;
2707 urec[count].height = wwin->frame->top_width + 1;
2708 count++;
2710 if (HAS_RESIZEBAR(wwin)) {
2711 urec[count].x = -1;
2712 urec[count].y = wwin->frame->core->height
2713 - wwin->frame->bottom_width - wwin->frame->top_width;
2714 urec[count].width = wwin->frame->core->width + 2;
2715 urec[count].height = wwin->frame->bottom_width + 1;
2716 count++;
2719 /* shape our frame window */
2720 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2721 0, wwin->frame->top_width, urec, count,
2722 ShapeSet, Unsorted);
2723 XFlush(dpy);
2724 wfree(urec);
2725 return;
2727 alt_code:
2728 #endif /* OPTIMIZE_SHAPE */
2729 count = 0;
2730 if (HAS_TITLEBAR(wwin)) {
2731 rect[count].x = -1;
2732 rect[count].y = -1;
2733 rect[count].width = wwin->frame->core->width + 2;
2734 rect[count].height = wwin->frame->top_width + 1;
2735 count++;
2737 if (HAS_RESIZEBAR(wwin)) {
2738 rect[count].x = -1;
2739 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
2740 rect[count].width = wwin->frame->core->width + 2;
2741 rect[count].height = wwin->frame->bottom_width + 1;
2742 count++;
2744 if (count > 0) {
2745 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
2746 0, 0, rect, count, ShapeSet, Unsorted);
2748 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
2749 0, wwin->frame->top_width, wwin->client_win,
2750 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
2751 XFlush(dpy);
2753 #endif /* SHAPE */
2755 /* ====================================================================== */
2757 static FocusMode
2758 getFocusMode(WWindow *wwin)
2760 FocusMode mode;
2762 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2763 if (wwin->wm_hints->input == True) {
2764 if (wwin->protocols.TAKE_FOCUS)
2765 mode = WFM_LOCALLY_ACTIVE;
2766 else
2767 mode = WFM_PASSIVE;
2768 } else {
2769 if (wwin->protocols.TAKE_FOCUS)
2770 mode = WFM_GLOBALLY_ACTIVE;
2771 else
2772 mode = WFM_NO_INPUT;
2774 } else {
2775 mode = WFM_PASSIVE;
2777 return mode;
2781 void
2782 wWindowSetKeyGrabs(WWindow *wwin)
2784 int i;
2785 WShortKey *key;
2787 for (i=0; i<WKBD_LAST; i++) {
2788 key = &wKeyBindings[i];
2790 if (key->keycode==0)
2791 continue;
2792 if (key->modifier!=AnyModifier) {
2793 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2794 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2795 #ifdef NUMLOCK_HACK
2796 /* Also grab all modifier combinations possible that include,
2797 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2798 * work even if the NumLock/ScrollLock key is on.
2800 wHackedGrabKey(key->keycode, key->modifier,
2801 wwin->frame->core->window, True, GrabModeAsync,
2802 GrabModeAsync);
2803 #endif
2805 XGrabKey(dpy, key->keycode, key->modifier,
2806 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2809 #ifndef LITE
2810 wRootMenuBindShortcuts(wwin->frame->core->window);
2811 #endif
2816 void
2817 wWindowResetMouseGrabs(WWindow *wwin)
2819 /* Mouse grabs can't be done on the client window because of
2820 * ICCCM and because clients that try to do the same will crash.
2822 * But there is a problem wich makes tbar buttons of unfocused
2823 * windows not usable as the click goes to the frame window instead
2824 * of the button itself. Must figure a way to fix that.
2827 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2829 if (!WFLAGP(wwin, no_bind_mouse)) {
2830 /* grabs for Meta+drag */
2831 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2832 True, ButtonPressMask|ButtonReleaseMask,
2833 GrabModeSync, GrabModeAsync, None, None);
2836 if (!wwin->flags.focused && !WFLAGP(wwin, no_focusable)
2837 && !wwin->flags.is_gnustep) {
2838 /* the passive grabs to focus the window */
2839 /* if (wPreferences.focus_mode == WKF_CLICK) */
2840 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2841 True, ButtonPressMask|ButtonReleaseMask,
2842 GrabModeSync, GrabModeAsync, None, None);
2844 XFlush(dpy);
2848 void
2849 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2851 if (attr->flags & GSExtraFlagsAttr) {
2852 if (MGFLAGP(wwin, broken_close) !=
2853 (attr->extra_flags & GSDocumentEditedFlag)) {
2854 wwin->client_flags.broken_close = !MGFLAGP(wwin, broken_close);
2855 wWindowUpdateButtonImages(wwin);
2861 WMagicNumber
2862 wWindowAddSavedState(char *instance, char *class, char *command,
2863 pid_t pid, WSavedState *state)
2865 WWindowState *wstate;
2867 wstate = malloc(sizeof(WWindowState));
2868 if (!wstate)
2869 return 0;
2871 memset(wstate, 0, sizeof(WWindowState));
2872 wstate->pid = pid;
2873 if (instance)
2874 wstate->instance = wstrdup(instance);
2875 if (class)
2876 wstate->class = wstrdup(class);
2877 if (command)
2878 wstate->command = wstrdup(command);
2879 wstate->state = state;
2881 wstate->next = windowState;
2882 windowState = wstate;
2884 #ifdef DEBUG
2885 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2886 class, command);
2887 #endif
2889 return wstate;
2893 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2896 WMagicNumber
2897 wWindowGetSavedState(Window win)
2899 char *instance, *class, *command=NULL;
2900 WWindowState *wstate = windowState;
2902 if (!wstate)
2903 return NULL;
2905 command = GetCommandForWindow(win);
2906 if (!command)
2907 return NULL;
2909 if (PropGetWMClass(win, &class, &instance)) {
2910 while (wstate) {
2911 if (SAME(instance, wstate->instance) &&
2912 SAME(class, wstate->class) &&
2913 SAME(command, wstate->command)) {
2914 break;
2916 wstate = wstate->next;
2918 } else {
2919 wstate = NULL;
2922 #ifdef DEBUG
2923 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2924 class, command);
2925 #endif
2927 if (command) wfree(command);
2928 if (instance) XFree(instance);
2929 if (class) XFree(class);
2931 return wstate;
2935 void
2936 wWindowDeleteSavedState(WMagicNumber id)
2938 WWindowState *tmp, *wstate=(WWindowState*)id;
2940 if (!wstate || !windowState)
2941 return;
2943 tmp = windowState;
2944 if (tmp==wstate) {
2945 windowState = wstate->next;
2946 #ifdef DEBUG
2947 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2948 wstate, wstate->instance, wstate->class, wstate->command);
2949 #endif
2950 if (wstate->instance) wfree(wstate->instance);
2951 if (wstate->class) wfree(wstate->class);
2952 if (wstate->command) wfree(wstate->command);
2953 wfree(wstate->state);
2954 wfree(wstate);
2955 } else {
2956 while (tmp->next) {
2957 if (tmp->next==wstate) {
2958 tmp->next=wstate->next;
2959 #ifdef DEBUG
2960 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2961 wstate, wstate->instance, wstate->class, wstate->command);
2962 #endif
2963 if (wstate->instance) wfree(wstate->instance);
2964 if (wstate->class) wfree(wstate->class);
2965 if (wstate->command) wfree(wstate->command);
2966 wfree(wstate->state);
2967 wfree(wstate);
2968 break;
2970 tmp = tmp->next;
2976 void
2977 wWindowDeleteSavedStatesForPID(pid_t pid)
2979 WWindowState *tmp, *wstate;
2981 if (!windowState)
2982 return;
2984 tmp = windowState;
2985 if (tmp->pid == pid) {
2986 wstate = windowState;
2987 windowState = tmp->next;
2988 #ifdef DEBUG
2989 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2990 wstate, wstate->instance, wstate->class, wstate->command);
2991 #endif
2992 if (wstate->instance) wfree(wstate->instance);
2993 if (wstate->class) wfree(wstate->class);
2994 if (wstate->command) wfree(wstate->command);
2995 wfree(wstate->state);
2996 wfree(wstate);
2997 } else {
2998 while (tmp->next) {
2999 if (tmp->next->pid==pid) {
3000 wstate = tmp->next;
3001 tmp->next = wstate->next;
3002 #ifdef DEBUG
3003 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
3004 wstate, wstate->instance, wstate->class, wstate->command);
3005 #endif
3006 if (wstate->instance) wfree(wstate->instance);
3007 if (wstate->class) wfree(wstate->class);
3008 if (wstate->command) wfree(wstate->command);
3009 wfree(wstate->state);
3010 wfree(wstate);
3011 break;
3013 tmp = tmp->next;
3019 void
3020 wWindowSetOmnipresent(WWindow *wwin, Bool flag)
3022 if (wwin->flags.omnipresent == flag)
3023 return;
3025 wwin->flags.omnipresent = flag;
3026 WMPostNotificationName(WMNChangedState, wwin, "omnipresent");
3030 /* ====================================================================== */
3032 static void
3033 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3035 WWindow *wwin = data;
3037 #ifndef NUMLOCK_HACK
3038 if ((event->xbutton.state & ValidModMask)
3039 != (event->xbutton.state & ~LockMask)) {
3040 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3041 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3043 #endif
3045 event->xbutton.state &= ValidModMask;
3047 CloseWindowMenu(wwin->screen_ptr);
3049 if (wPreferences.focus_mode==WKF_CLICK
3050 && !(event->xbutton.state&ControlMask)
3051 && !WFLAGP(wwin, no_focusable)) {
3052 wSetFocusTo(wwin->screen_ptr, wwin);
3055 if (event->xbutton.button == Button1)
3056 wRaiseFrame(wwin->frame->core);
3058 if (event->xbutton.window != wwin->frame->resizebar->window) {
3059 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
3060 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3061 GrabModeAsync, GrabModeAsync, None,
3062 None, CurrentTime)!=GrabSuccess) {
3063 #ifdef DEBUG0
3064 wwarning("pointer grab failed for window move");
3065 #endif
3066 return;
3070 if (event->xbutton.state & MOD_MASK) {
3071 /* move the window */
3072 wMouseMoveWindow(wwin, event);
3073 XUngrabPointer(dpy, CurrentTime);
3074 } else {
3075 wMouseResizeWindow(wwin, event);
3076 XUngrabPointer(dpy, CurrentTime);
3082 static void
3083 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
3085 WWindow *wwin = data;
3087 event->xbutton.state &= ValidModMask;
3089 if (event->xbutton.button==Button1) {
3090 if (event->xbutton.state == 0) {
3091 if (!WFLAGP(wwin, no_shadeable)) {
3092 /* shade window */
3093 if (wwin->flags.shaded)
3094 wUnshadeWindow(wwin);
3095 else
3096 wShadeWindow(wwin);
3098 } else {
3099 int dir = 0;
3101 if (event->xbutton.state & ControlMask)
3102 dir |= MAX_VERTICAL;
3104 if (event->xbutton.state & ShiftMask) {
3105 dir |= MAX_HORIZONTAL;
3106 if (!(event->xbutton.state & ControlMask))
3107 wSelectWindow(wwin, !wwin->flags.selected);
3110 /* maximize window */
3111 if (dir!=0 && IS_RESIZABLE(wwin)) {
3112 int ndir = dir ^ wwin->flags.maximized;
3114 if (ndir != 0) {
3115 wMaximizeWindow(wwin, ndir);
3116 } else {
3117 wUnmaximizeWindow(wwin);
3121 } else if (event->xbutton.button==Button3) {
3122 if (event->xbutton.state & MOD_MASK) {
3123 wHideOtherApplications(wwin);
3125 } else if (event->xbutton.button==Button2) {
3126 wSelectWindow(wwin, !wwin->flags.selected);
3127 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelUp) {
3128 wShadeWindow(wwin);
3129 } else if (event->xbutton.button == WINGsConfiguration.mouseWheelDown) {
3130 wUnshadeWindow(wwin);
3135 static void
3136 frameMouseDown(WObjDescriptor *desc, XEvent *event)
3138 WWindow *wwin = desc->parent;
3140 event->xbutton.state &= ValidModMask;
3142 CloseWindowMenu(wwin->screen_ptr);
3144 if (/*wPreferences.focus_mode==WKF_CLICK
3145 &&*/ !(event->xbutton.state&ControlMask)
3146 && !WFLAGP(wwin, no_focusable)) {
3147 wSetFocusTo(wwin->screen_ptr, wwin);
3149 if (event->xbutton.button == Button1) {
3150 wRaiseFrame(wwin->frame->core);
3153 if (event->xbutton.state & MOD_MASK) {
3154 /* move the window */
3155 if (XGrabPointer(dpy, wwin->client_win, False,
3156 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3157 GrabModeAsync, GrabModeAsync, None,
3158 None, CurrentTime)!=GrabSuccess) {
3159 #ifdef DEBUG0
3160 wwarning("pointer grab failed for window move");
3161 #endif
3162 return;
3164 if (event->xbutton.button == Button3)
3165 wMouseResizeWindow(wwin, event);
3166 else if (event->xbutton.button==Button1 || event->xbutton.button==Button2)
3167 wMouseMoveWindow(wwin, event);
3168 XUngrabPointer(dpy, CurrentTime);
3173 static void
3174 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
3176 WWindow *wwin = (WWindow*)data;
3178 #ifndef NUMLOCK_HACK
3179 if ((event->xbutton.state & ValidModMask)
3180 != (event->xbutton.state & ~LockMask)) {
3181 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
3182 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
3184 #endif
3185 event->xbutton.state &= ValidModMask;
3187 CloseWindowMenu(wwin->screen_ptr);
3189 if (wPreferences.focus_mode==WKF_CLICK
3190 && !(event->xbutton.state&ControlMask)
3191 && !WFLAGP(wwin, no_focusable)) {
3192 wSetFocusTo(wwin->screen_ptr, wwin);
3195 if (event->xbutton.button == Button1
3196 || event->xbutton.button == Button2) {
3198 if (event->xbutton.button == Button1) {
3199 if (event->xbutton.state & MOD_MASK) {
3200 wLowerFrame(wwin->frame->core);
3201 } else {
3202 wRaiseFrame(wwin->frame->core);
3205 if ((event->xbutton.state & ShiftMask)
3206 && !(event->xbutton.state & ControlMask)) {
3207 wSelectWindow(wwin, !wwin->flags.selected);
3208 return;
3210 if (event->xbutton.window != wwin->frame->titlebar->window
3211 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3212 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3213 GrabModeAsync, GrabModeAsync, None,
3214 None, CurrentTime)!=GrabSuccess) {
3215 #ifdef DEBUG0
3216 wwarning("pointer grab failed for window move");
3217 #endif
3218 return;
3221 /* move the window */
3222 wMouseMoveWindow(wwin, event);
3224 XUngrabPointer(dpy, CurrentTime);
3225 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
3226 && !wwin->flags.internal_window
3227 && !WCHECK_STATE(WSTATE_MODAL)) {
3228 WObjDescriptor *desc;
3230 if (event->xbutton.window != wwin->frame->titlebar->window
3231 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
3232 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
3233 GrabModeAsync, GrabModeAsync, None,
3234 None, CurrentTime)!=GrabSuccess) {
3235 #ifdef DEBUG0
3236 wwarning("pointer grab failed for window move");
3237 #endif
3238 return;
3241 OpenWindowMenu(wwin, event->xbutton.x_root,
3242 wwin->frame_y+wwin->frame->top_width, False);
3244 /* allow drag select */
3245 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
3246 event->xany.send_event = True;
3247 (*desc->handle_mousedown)(desc, event);
3249 XUngrabPointer(dpy, CurrentTime);
3255 static void
3256 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
3258 WWindow *wwin = data;
3260 event->xbutton.state &= ValidModMask;
3262 CloseWindowMenu(wwin->screen_ptr);
3264 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3265 return;
3267 /* if control-click, kill the client */
3268 if (event->xbutton.state & ControlMask) {
3269 wClientKill(wwin);
3270 } else {
3271 if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
3272 /* send delete message */
3273 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3279 static void
3280 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
3282 WWindow *wwin = data;
3284 CloseWindowMenu(wwin->screen_ptr);
3286 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3287 return;
3289 /* send delete message */
3290 if (wwin->protocols.DELETE_WINDOW) {
3291 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
3292 } else {
3293 wClientKill(wwin);
3298 #ifdef XKB_BUTTON_HINT
3299 static void
3300 windowLanguageClick(WCoreWindow *sender, void *data, XEvent *event)
3302 WWindow *wwin = data;
3303 WFrameWindow *fwin = wwin->frame;
3304 WScreen *scr = fwin->screen_ptr;
3305 XkbStateRec staterec;
3306 int tl;
3308 if (event->xbutton.button != Button1 && event->xbutton.button != Button3)
3309 return;
3310 tl = wwin->frame->languagemode;
3311 wwin->frame->languagemode = wwin->frame->last_languagemode;
3312 wwin->frame->last_languagemode = tl;
3313 wSetFocusTo(scr, wwin);
3314 wwin->frame->languagebutton_image =
3315 wwin->frame->screen_ptr->b_pixmaps[WBUT_XKBGROUP1 +
3316 wwin->frame->languagemode];
3317 wFrameWindowUpdateLanguageButton(wwin->frame);
3318 if (event->xbutton.button == Button3)
3319 return;
3320 wRaiseFrame(fwin->core);
3322 #endif
3325 static void
3326 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
3328 WWindow *wwin = data;
3330 event->xbutton.state &= ValidModMask;
3332 CloseWindowMenu(wwin->screen_ptr);
3334 if (event->xbutton.button < Button1 || event->xbutton.button > Button3)
3335 return;
3337 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
3338 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
3339 LastTimestamp);
3340 } else {
3341 WApplication *wapp;
3342 if ((event->xbutton.state & ControlMask) ||
3343 (event->xbutton.button == Button3)) {
3345 wapp = wApplicationOf(wwin->main_window);
3346 if (wapp && !WFLAGP(wwin, no_appicon))
3347 wHideApplication(wapp);
3348 } else if (event->xbutton.state==0) {
3349 wIconifyWindow(wwin);