This update includes the 0.20.3pre3 code
[wmaker-crm.git] / src / window.c
blob656ec2a1c60cb5dd9424c02738091a141b18d1ba
1 /* window.c - client window managing class
2 *
3 * Window Maker window manager
4 *
5 * Copyright (c) 1997, 1998 Alfredo K. Kojima
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 * USA.
23 #include "wconfig.h"
25 #include <X11/Xlib.h>
26 #include <X11/Xutil.h>
27 #ifdef SHAPE
28 #include <X11/extensions/shape.h>
29 #endif
30 #ifdef KEEP_XKB_LOCK_STATUS
31 # include <X11/XKBlib.h>
32 #endif /* KEEP_XKB_LOCK_STATUS */
33 #include <stdlib.h>
34 #include <stdio.h>
35 #include <string.h>
38 #include "WindowMaker.h"
39 #include "GNUstep.h"
40 #ifdef MWM_HINTS
41 # include "motif.h"
42 #endif
43 #include "wcore.h"
44 #include "framewin.h"
45 #include "texture.h"
46 #include "window.h"
47 #include "winspector.h"
48 #include "icon.h"
49 #include "properties.h"
50 #include "actions.h"
51 #include "client.h"
52 #include "funcs.h"
53 #include "keybind.h"
54 #include "stacking.h"
55 #include "defaults.h"
56 #include "workspace.h"
58 /****** Global Variables ******/
60 extern WShortKey wKeyBindings[WKBD_LAST];
62 #ifdef SHAPE
63 extern Bool wShapeSupported;
64 #endif
66 /* contexts */
67 extern XContext wWinContext;
69 /* cursors */
70 extern Cursor wCursor[WCUR_LAST];
72 /* protocol atoms */
73 extern Atom _XA_WM_DELETE_WINDOW;
74 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
76 extern Atom _XA_WINDOWMAKER_STATE;
78 extern WPreferences wPreferences;
80 #define MOD_MASK wPreferences.modifier_mask
82 extern Time LastTimestamp;
84 /* superfluous... */
85 extern void DoWindowBirth(WWindow*);
88 /***** Local Stuff *****/
91 static WWindowState *windowState=NULL;
95 /* local functions */
96 static FocusMode getFocusMode(WWindow *wwin);
98 static int getSavedState(Window window, WSavedState **state);
100 static void setupGNUstepHints(WWindowAttributes *attribs,
101 GNUstepWMAttributes *gs_hints);
103 #ifdef MWM_HINTS
104 static void setupMWMHints(WWindowAttributes *attribs, MWMHints *mwm_hints);
105 #endif
107 /* event handlers */
110 /* frame window (during window grabs) */
111 static void frameMouseDown(WObjDescriptor *desc, XEvent *event);
113 /* close button */
114 static void windowCloseClick(WCoreWindow *sender, void *data, XEvent *event);
115 static void windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event);
117 /* iconify button */
118 static void windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event);
121 static void titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
122 static void titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event);
124 static void resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event);
131 WWindow*
132 wWindowFor(Window window)
134 WObjDescriptor *desc;
136 if (window==None)
137 return NULL;
139 if (XFindContext(dpy, window, wWinContext, (XPointer*)&desc)==XCNOENT)
140 return NULL;
142 if (desc->parent_type==WCLASS_WINDOW)
143 return desc->parent;
144 else if (desc->parent_type==WCLASS_FRAME) {
145 WFrameWindow *frame = (WFrameWindow*)desc->parent;
146 if (frame->flags.is_client_window_frame)
147 return frame->child;
149 return NULL;
153 WWindow*
154 wWindowCreate()
156 WWindow *wwin;
158 wwin = wmalloc(sizeof(WWindow));
159 wretain(wwin);
161 memset(wwin, 0, sizeof(WWindow));
163 wwin->client_descriptor.handle_mousedown = frameMouseDown;
164 wwin->client_descriptor.parent = wwin;
165 wwin->client_descriptor.self = wwin;
166 wwin->client_descriptor.parent_type = WCLASS_WINDOW;
167 return wwin;
171 void
172 wWindowDestroy(WWindow *wwin)
174 int i;
176 wwin->flags.destroyed = 1;
178 for (i = 0; i < 4; i++) {
179 if (wwin->screen_ptr->shortcutWindow[i] == wwin) {
180 wwin->screen_ptr->shortcutWindow[i] = NULL;
184 if (wwin->normal_hints)
185 free(wwin->normal_hints);
187 if (wwin->wm_hints)
188 XFree(wwin->wm_hints);
190 if (wwin->wm_instance)
191 XFree(wwin->wm_instance);
193 if (wwin->wm_class)
194 XFree(wwin->wm_class);
196 if (wwin->wm_gnustep_attr)
197 free(wwin->wm_gnustep_attr);
199 if (wwin->cmap_windows)
200 XFree(wwin->cmap_windows);
202 XDeleteContext(dpy, wwin->client_win, wWinContext);
204 if (wwin->frame)
205 wFrameWindowDestroy(wwin->frame);
207 if (wwin->icon) {
208 RemoveFromStackList(wwin->icon->core);
209 wIconDestroy(wwin->icon);
210 if (wPreferences.auto_arrange_icons)
211 wArrangeIcons(wwin->screen_ptr, True);
213 wrelease(wwin);
219 static void
220 setupGNUstepHints(WWindowAttributes *attribs, GNUstepWMAttributes *gs_hints)
222 if (gs_hints->flags & GSWindowStyleAttr) {
223 attribs->no_titlebar =
224 ((gs_hints->window_style & WMTitledWindowMask)?0:1);
226 attribs->no_close_button = attribs->no_closable =
227 ((gs_hints->window_style & WMClosableWindowMask)?0:1);
229 attribs->no_miniaturize_button = attribs->no_miniaturizable =
230 ((gs_hints->window_style & WMMiniaturizableWindowMask)?0:1);
232 attribs->no_resizebar = attribs->no_resizable =
233 ((gs_hints->window_style & WMResizableWindowMask)?0:1);
234 } else {
235 /* setup the defaults */
236 attribs->no_titlebar = 0;
237 attribs->no_closable = 0;
238 attribs->no_miniaturizable = 0;
239 attribs->no_resizable = 0;
240 attribs->no_close_button = 0;
241 attribs->no_miniaturize_button = 0;
242 attribs->no_resizebar = 0;
245 if (gs_hints->extra_flags & GSNoApplicationIconFlag) {
246 attribs->no_appicon = 1;
251 #ifdef MWM_HINTS
252 static void
253 setupMWMHints(WWindowAttributes *attribs, MWMHints *mwm_hints)
257 * We will ignore all decoration hints that have an equivalent as
258 * functions, because wmaker does not distinguish decoration hints
261 if (mwm_hints->flags & MWM_HINTS_DECORATIONS) {
262 # ifdef DEBUG
263 fprintf(stderr,"has decor hints [ ");
264 # endif
265 attribs->no_titlebar = 1;
266 attribs->no_close_button = 1;
267 attribs->no_miniaturize_button = 1;
268 attribs->no_resizebar = 1;
270 if (mwm_hints->decorations & MWM_DECOR_ALL) {
271 # ifdef DEBUG
272 fprintf(stderr,"ALL ");
273 # endif
274 attribs->no_titlebar = 0;
275 attribs->no_close_button = 0;
276 attribs->no_closable = 0;
277 attribs->no_miniaturize_button = 0;
278 attribs->no_miniaturizable = 0;
279 attribs->no_resizebar = 0;
280 attribs->no_resizable = 0;
283 if (mwm_hints->decorations & MWM_DECOR_BORDER) {
284 # ifdef DEBUG
285 fprintf(stderr,"(BORDER) ");
286 # endif
290 if (mwm_hints->decorations & MWM_DECOR_RESIZEH) {
291 # ifdef DEBUG
292 fprintf(stderr,"RESIZEH ");
293 # endif
294 attribs->no_resizebar = 0;
297 if (mwm_hints->decorations & MWM_DECOR_TITLE) {
298 # ifdef DEBUG
299 fprintf(stderr,"TITLE+close ");
300 # endif
301 attribs->no_titlebar = 0;
302 attribs->no_close_button = 0;
303 attribs->no_closable = 0;
306 if (mwm_hints->decorations & MWM_DECOR_MENU) {
307 # ifdef DEBUG
308 fprintf(stderr,"(MENU) ");
309 # endif
313 if (mwm_hints->decorations & MWM_DECOR_MINIMIZE) {
314 # ifdef DEBUG
315 fprintf(stderr,"MINIMIZE ");
316 # endif
317 attribs->no_miniaturize_button = 0;
318 attribs->no_miniaturizable = 0;
321 if (mwm_hints->decorations & MWM_DECOR_MAXIMIZE) {
322 # ifdef DEBUG
323 fprintf(stderr,"(MAXIMIZE) ");
324 # endif
327 # ifdef DEBUG
328 fprintf(stderr,"]\n");
329 # endif
333 if (mwm_hints->flags & MWM_HINTS_FUNCTIONS) {
334 # ifdef DEBUG
335 fprintf(stderr,"has function hints [ ");
336 # endif
337 attribs->no_closable = 1;
338 attribs->no_miniaturizable = 1;
339 attribs->no_resizable = 1;
341 if (mwm_hints->functions & MWM_FUNC_ALL) {
342 # ifdef DEBUG
343 fprintf(stderr,"ALL ");
344 # endif
345 attribs->no_closable = 0;
346 attribs->no_miniaturizable = 0;
347 attribs->no_resizable = 0;
349 if (mwm_hints->functions & MWM_FUNC_RESIZE) {
350 # ifdef DEBUG
351 fprintf(stderr,"RESIZE ");
352 # endif
353 attribs->no_resizable = 0;
356 if (mwm_hints->functions & MWM_FUNC_MOVE) {
357 # ifdef DEBUG
358 fprintf(stderr,"(MOVE) ");
359 # endif
362 if (mwm_hints->functions & MWM_FUNC_MINIMIZE) {
363 # ifdef DEBUG
364 fprintf(stderr,"MINIMIZE ");
365 # endif
366 attribs->no_miniaturizable = 0;
368 if (mwm_hints->functions & MWM_FUNC_MAXIMIZE) {
369 # ifdef DEBUG
370 fprintf(stderr,"MAXIMIZE ");
371 /* a window must be resizable to be maximizable */
372 attribs->no_resizable = 0;
373 # endif
375 if (mwm_hints->functions & MWM_FUNC_CLOSE) {
376 # ifdef DEBUG
377 fprintf(stderr,"CLOSE ");
378 # endif
379 attribs->no_closable = 0;
381 # ifdef DEBUG
382 fprintf(stderr,"]\n");
383 # endif
386 #endif /* MWM_HINTS */
389 void
390 wWindowCheckAttributeSanity(WWindow *wwin, WWindowAttributes *wflags)
392 if (wflags->no_appicon)
393 wflags->emulate_appicon = 0;
395 if (wwin->main_window!=None) {
396 WApplication *wapp = wApplicationOf(wwin->main_window);
397 if (wapp && !wapp->flags.emulated)
398 wflags->emulate_appicon = 0;
401 if (wwin->transient_for!=None
402 && wwin->transient_for!=wwin->screen_ptr->root_win)
403 wflags->emulate_appicon = 0;
405 if (wflags->sunken && wflags->floating)
406 wflags->sunken = 0;
411 Bool
412 wWindowCanReceiveFocus(WWindow *wwin)
414 if (!wwin->flags.mapped && !wwin->flags.shaded)
415 return False;
416 if (wwin->window_flags.no_focusable || wwin->flags.miniaturized)
417 return False;
418 if (wwin->frame->workspace != wwin->screen_ptr->current_workspace)
419 return False;
421 return True;
426 *----------------------------------------------------------------
427 * wManageWindow--
428 * reparents the window and allocates a descriptor for it.
429 * Window manager hints and other hints are fetched to configure
430 * the window decoration attributes and others. User preferences
431 * for the window are used if available, to configure window
432 * decorations and some behaviour.
434 * Returns:
435 * the new window descriptor
437 * Side effects:
438 * The window is reparented and appropriate notification
439 * is done to the client. Input mask for the window is setup.
440 * The window descriptor is also associated with various window
441 * contexts and inserted in the head of the window list.
442 * Event handler contexts are associated for some objects
443 * (buttons, titlebar and resizebar)
445 * TODO:
446 * Check if the window_flags setting is correct.
447 *----------------------------------------------------------------
449 WWindow*
450 wManageWindow(WScreen *scr, Window window)
452 WWindow *wwin;
453 int x, y;
454 unsigned width, height;
455 XWindowAttributes wattribs;
456 XSetWindowAttributes attribs;
457 int iconic = 0;
458 WWindowState *win_state;
459 #ifdef MWM_HINTS
460 MWMHints *motif_hints = NULL;
461 #endif
462 int window_level;
463 int foo;
464 int workspace = -1;
465 char *title;
467 /* mutex. */
468 XGrabServer(dpy);
469 XSync(dpy, 0);
470 /* make sure the window is still there */
471 if (!XGetWindowAttributes(dpy, window, &wattribs)) {
472 XUngrabServer(dpy);
473 return NULL;
475 wwin = wWindowCreate();
477 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
479 #ifdef DEBUG
480 printf("managing window %x\n", (unsigned)window);
481 #endif
483 #ifdef SHAPE
484 if (wShapeSupported) {
485 int junk;
486 unsigned int ujunk;
487 int b_shaped;
489 XShapeSelectInput(dpy, window, ShapeNotifyMask);
490 XShapeQueryExtents(dpy, window, &b_shaped, &junk, &junk, &ujunk,
491 &ujunk, &junk, &junk, &junk, &ujunk, &ujunk);
492 wwin->flags.shaped = b_shaped;
494 #endif
497 *--------------------------------------------------
499 * Get hints and other information in properties
501 *--------------------------------------------------
503 wwin->wm_hints = XGetWMHints(dpy, window);
504 PropGetWMClass(window, &wwin->wm_class, &wwin->wm_instance);
506 /* setup descriptor */
507 wwin->client_win = window;
508 wwin->screen_ptr = scr;
510 wwin->old_border_width = wattribs.border_width;
512 attribs.event_mask = CLIENT_EVENTS;
513 attribs.do_not_propagate_mask = ButtonPressMask | ButtonReleaseMask;
514 attribs.save_under = False;
515 XChangeWindowAttributes(dpy, window, CWEventMask|CWDontPropagate
516 |CWSaveUnder, &attribs);
517 XSetWindowBorderWidth(dpy, window, 0);
519 /* fill in property/hint data */
520 if (!wFetchName(dpy, window, &title)) {
521 title = NULL;
523 /* get hints from GNUstep app */
524 if (!PropGetGNUstepWMAttr(window, &wwin->wm_gnustep_attr)) {
525 wwin->wm_gnustep_attr=NULL;
528 #ifdef MWM_HINTS
529 if (!PropGetMotifWMHints(window, &motif_hints))
530 motif_hints = NULL;
531 #endif /* MWM_HINTS */
533 wwin->client_leader = PropGetClientLeader(window);
534 if (wwin->client_leader!=None)
535 wwin->main_window = wwin->client_leader;
537 if (wwin->wm_hints)
538 XFree(wwin->wm_hints);
540 wwin->wm_hints = XGetWMHints(dpy, window);
542 if (wwin->wm_hints) {
543 if ((wwin->wm_hints->flags&StateHint)
544 && (wwin->wm_hints->initial_state == IconicState)) {
545 iconic = 1;
546 /* don't do iconify animation */
547 wwin->flags.skip_next_animation = 1;
550 if (wwin->wm_hints->flags & WindowGroupHint) {
551 wwin->group_id = wwin->wm_hints->window_group;
552 /* window_group has priority over CLIENT_LEADER */
553 wwin->main_window = wwin->group_id;
554 } else {
555 wwin->group_id = None;
558 if (wwin->wm_hints->flags & UrgencyHint)
559 wwin->flags.urgent = 1;
560 } else {
561 wwin->group_id = None;
564 PropGetProtocols(window, &wwin->protocols);
566 if (!XGetTransientForHint(dpy, window, &wwin->transient_for)) {
567 wwin->transient_for = None;
568 } else {
569 if (wwin->transient_for==None || wwin->transient_for==window) {
570 wwin->transient_for = scr->root_win;
571 } else {
572 WWindow *owner;
573 owner = wWindowFor(wwin->transient_for);
574 if (owner && owner->main_window!=None) {
575 wwin->main_window = owner->main_window;
576 } /*else {
577 wwin->main_window = None;
580 /* don't let transients start miniaturized if their owners
581 * are not */
582 if (owner && !owner->flags.miniaturized && iconic) {
583 iconic = 0;
584 if (wwin->wm_hints)
585 wwin->wm_hints->initial_state = NormalState;
590 /* guess the focus mode */
591 wwin->focus_mode = getFocusMode(wwin);
593 /* get geometry stuff */
594 wClientGetNormalHints(wwin, &wattribs, True, &x, &y, &width, &height);
596 /* get colormap windows */
597 GetColormapWindows(wwin);
601 *--------------------------------------------------
603 * Setup the decoration/window attributes and
604 * geometry
606 *--------------------------------------------------
608 /* sets global default stuff */
609 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
610 &wwin->window_flags, True);
612 * Decoration setting is done in this precedence (lower to higher)
613 * - use default in the resource database
614 * - guess some settings
615 * - use GNUstep/external window attributes
616 * - set hints specified for the app in the resource DB
618 wwin->window_flags.broken_close = 0;
620 if (wwin->protocols.DELETE_WINDOW)
621 wwin->window_flags.kill_close = 0;
622 else
623 wwin->window_flags.kill_close = 1;
625 /* transients can't be iconified or maximized */
626 if (wwin->transient_for) {
627 wwin->window_flags.no_miniaturizable = 1;
628 wwin->window_flags.no_miniaturize_button = 1;
629 #ifdef DEBUG
630 printf("%x is transient for %x\n", (unsigned)window,
631 (unsigned)wwin->transient_for);
632 #endif
635 /* if the window can't be resized, remove the resizebar */
636 if (wwin->normal_hints->flags & (PMinSize|PMaxSize)
637 && (wwin->normal_hints->min_width==wwin->normal_hints->max_width)
638 && (wwin->normal_hints->min_height==wwin->normal_hints->max_height)) {
639 wwin->window_flags.no_resizable = 1;
640 wwin->window_flags.no_resizebar = 1;
643 /* set GNUstep window attributes */
644 if (wwin->wm_gnustep_attr) {
645 setupGNUstepHints(&wwin->window_flags, wwin->wm_gnustep_attr);
647 if (wwin->wm_gnustep_attr->flags & GSWindowLevelAttr) {
649 switch (wwin->wm_gnustep_attr->window_level) {
650 case WMNormalWindowLevel:
651 window_level = WMNormalLevel;
652 break;
653 case WMFloatingWindowLevel:
654 window_level = WMFloatingLevel;
655 break;
656 case WMDockWindowLevel:
657 window_level = WMDockLevel;
658 break;
659 case WMSubmenuWindowLevel:
660 window_level = WMSubmenuLevel;
661 break;
662 case WMMainMenuWindowLevel:
663 window_level = WMMainMenuLevel;
664 break;
665 default:
666 window_level = WMNormalLevel;
667 break;
669 } else {
670 /* setup defaults */
671 window_level = WMNormalLevel;
673 } else {
674 #ifdef MWM_HINTS
675 if (motif_hints) {
676 setupMWMHints(&wwin->window_flags, motif_hints);
678 #endif /* MWM_HINTS */
679 if (wwin->window_flags.floating)
680 window_level = WMFloatingLevel;
681 else if (wwin->window_flags.sunken)
682 window_level = WMSunkenLevel;
683 else
684 window_level = WMNormalLevel;
686 #ifdef MWM_HINTS
687 if (motif_hints)
688 free(motif_hints);
689 #endif
692 * Set attributes specified only for that window/class.
693 * This might do duplicate work with the 1st wDefaultFillAttributes().
694 * TODO: Do something about that.
696 wDefaultFillAttributes(scr, wwin->wm_instance, wwin->wm_class,
697 &wwin->window_flags, False);
701 * Sanity checks for attributes that depend on other attributes
703 wWindowCheckAttributeSanity(wwin, &wwin->window_flags);
705 wwin->window_flags.no_shadeable = wwin->window_flags.no_titlebar;
708 * Make broken apps behave as a nice app.
710 if (wwin->window_flags.emulate_appicon) {
711 wwin->main_window = wwin->client_win;
715 *------------------------------------------------------------
717 * Setup the initial state of the window
719 *------------------------------------------------------------
721 if (wwin->window_flags.start_miniaturized
722 && !wwin->window_flags.no_miniaturizable) {
723 wwin->flags.miniaturized = 1;
724 iconic = 1;
727 /* if there is a saved state, restore it */
728 win_state = NULL;
729 if (wwin->main_window!=None/* && wwin->main_window!=window*/) {
730 win_state = (WWindowState*)wWindowGetSavedState(wwin->main_window);
731 } else {
732 win_state = (WWindowState*)wWindowGetSavedState(window);
734 if (win_state && !(wwin->wm_hints && wwin->wm_hints->flags&StateHint &&
735 wwin->wm_hints->initial_state==WithdrawnState)) {
736 if (win_state->state->hidden>0)
737 wwin->flags.hidden = win_state->state->hidden;
738 if (win_state->state->shaded>0 && !wwin->window_flags.no_shadeable)
739 wwin->flags.shaded = win_state->state->shaded;
740 if (win_state->state->miniaturized>0 &&
741 !wwin->window_flags.no_miniaturizable) {
742 wwin->flags.miniaturized = win_state->state->miniaturized;
743 iconic = 1;
745 if (!wwin->window_flags.omnipresent) {
746 int w = wDefaultGetStartWorkspace(scr, wwin->wm_instance,
747 wwin->wm_class);
748 if (w < 0 || w >= scr->workspace_count) {
749 workspace = win_state->state->workspace;
750 if (workspace>=scr->workspace_count)
751 workspace = scr->current_workspace;
752 } else {
753 workspace = w;
755 } else {
756 workspace = scr->current_workspace;
760 /* if we're restarting, restore saved state. This will overwrite previous */
762 WSavedState *wstate;
764 if (getSavedState(window, &wstate)) {
765 wwin->flags.shaded = wstate->shaded;
766 wwin->flags.hidden = wstate->hidden;
767 wwin->flags.miniaturized = 0;
768 workspace = wstate->workspace;
769 free(wstate);
774 /* set workspace on which the window starts */
775 if (workspace >= 0) {
776 if (workspace > scr->workspace_count-1) {
777 wWorkspaceMake(scr, workspace - scr->workspace_count + 1);
778 workspace = scr->workspace_count - 1;
780 } else {
781 int w;
783 w = wDefaultGetStartWorkspace(scr, wwin->wm_instance, wwin->wm_class);
784 if (w>=0 && w<scr->workspace_count && !wwin->window_flags.omnipresent) {
785 workspace = w;
786 } else {
787 workspace = scr->current_workspace;
791 /* setup window geometry */
792 if (win_state && win_state->state->use_geometry) {
793 width = win_state->state->w;
794 height = win_state->state->h;
796 wWindowConstrainSize(wwin, &width, &height);
798 /* do not ask for window placement if the window is
799 * transient, during startup, if the initial workspace is another one
800 * or if the window wants to
801 * start iconic.
802 * If geometry was saved, restore it. */
803 if (win_state && win_state->state->use_geometry) {
804 x = win_state->state->x;
805 y = win_state->state->y;
806 } else if (wwin->transient_for==None && !scr->flags.startup &&
807 workspace==scr->current_workspace && !iconic &&
808 !(wwin->normal_hints->flags & (USPosition|PPosition))) {
809 PlaceWindow(wwin, &x, &y, width, height);
812 if (wwin->window_flags.dont_move_off)
813 wScreenBringInside(scr, &x, &y, width, height);
816 *--------------------------------------------------
818 * Create frame, borders and do reparenting
820 *--------------------------------------------------
823 foo = WFF_LEFT_BUTTON | WFF_RIGHT_BUTTON;
824 if (!wwin->window_flags.no_titlebar)
825 foo |= WFF_TITLEBAR;
826 if (!wwin->window_flags.no_resizebar)
827 foo |= WFF_RESIZEBAR;
829 wwin->frame = wFrameWindowCreate(scr, window_level,
830 x, y, width, height, foo,
831 scr->window_title_texture,
832 (WTexture**)scr->resizebar_texture,
833 scr->window_title_pixel,
834 &scr->window_title_gc,
835 &scr->title_font);
837 wwin->frame->flags.is_client_window_frame = 1;
838 wwin->frame->flags.justification = wPreferences.title_justification;
840 /* setup button images */
841 wWindowUpdateButtonImages(wwin);
843 /* hide unused buttons */
844 foo = 0;
845 if (wwin->window_flags.no_close_button)
846 foo |= WFF_RIGHT_BUTTON;
847 if (wwin->window_flags.no_miniaturize_button)
848 foo |= WFF_LEFT_BUTTON;
849 if (foo!=0)
850 wFrameWindowHideButton(wwin->frame, foo);
853 wwin->frame->child = wwin;
855 wFrameWindowChangeTitle(wwin->frame, title ? title : DEF_WINDOW_TITLE);
856 if (title)
857 XFree(title);
859 wwin->frame->workspace = workspace;
861 wwin->frame->on_click_left = windowIconifyClick;
863 wwin->frame->on_click_right = windowCloseClick;
864 wwin->frame->on_dblclick_right = windowCloseDblClick;
866 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
867 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
869 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
871 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
872 0, wwin->frame->top_width);
874 #if 0
876 int gx, gy;
878 wClientGetGravityOffsets(wwin, &gx, &gy);
879 /* set the position of the frame on screen */
880 x += gx * FRAME_BORDER_WIDTH;
881 y += gy * FRAME_BORDER_WIDTH;
882 /* if gravity is to the south, account for the border sizes */
883 if (gy > 0)
884 y -= wwin->frame->top_width + wwin->frame->bottom_width;
886 #endif
888 * wWindowConfigure() will init the client window's size
889 * (wwin->client.{width,height}) and all other geometry
890 * related variables (frame_x,frame_y)
892 wWindowConfigure(wwin, x, y, width, height);
895 *--------------------------------------------------
897 * Setup descriptors and save window to internal
898 * lists
900 *--------------------------------------------------
903 if (wwin->main_window!=None) {
904 WApplication *app;
905 WWindow *leader;
907 /* Leader windows do not necessary set themselves as leaders.
908 * If this is the case, point the leader of this window to
909 * itself */
910 leader = wWindowFor(wwin->main_window);
911 if (leader && leader->main_window==None) {
912 leader->main_window = leader->client_win;
915 app = wApplicationCreate(scr, wwin->main_window);
916 if (app) {
917 app->last_workspace = workspace;
920 * Do application specific stuff, like setting application
921 * wide attributes.
924 if (wwin->flags.hidden) {
925 /* if the window was set to hidden because it was hidden
926 * in a previous incarnation and that state was restored */
927 app->flags.hidden = 1;
930 if (app->flags.hidden) {
931 wwin->flags.hidden = 1;
936 /* setup the frame descriptor */
937 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
938 wwin->frame->core->descriptor.parent = wwin;
939 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
941 /* don't let windows go away if we die */
942 XAddToSaveSet(dpy, window);
944 XLowerWindow(dpy, window);
946 /* if window is in this workspace and should be mapped, then map it */
947 if (!iconic && (workspace == scr->current_workspace
948 || wwin->window_flags.omnipresent)
949 && !wwin->flags.hidden
950 && !(wwin->wm_hints && (wwin->wm_hints->flags & StateHint)
951 && wwin->wm_hints->initial_state == WithdrawnState)) {
952 /* The following "if" is to avoid crashing of clients that expect
953 * WM_STATE set before they get mapped. Else WM_STATE is set later,
954 * after the return from this function.
956 if (wwin->wm_hints && (wwin->wm_hints->flags & StateHint)) {
957 wClientSetState(wwin, wwin->wm_hints->initial_state, None);
958 } else {
959 wClientSetState(wwin, NormalState, None);
961 if (wPreferences.superfluous && !wPreferences.no_animations
962 && !scr->flags.startup && wwin->transient_for==None
964 * The brain damaged idiotic non-click to focus modes will
965 * have trouble with this because:
967 * 1. window is created and mapped by the client
968 * 2. window is mapped by wmaker in small size
969 * 3. window is animated to grow to normal size
970 * 4. this function returns to normal event loop
971 * 5. eventually, the EnterNotify event that would trigger
972 * the window focusing (if the mouse is over that window)
973 * will be processed by wmaker.
974 * But since this event will be rather delayed
975 * (step 3 has a large delay) the time when the event ocurred
976 * and when it is processed, the client that owns that window
977 * will reject the XSetInputFocus() for it.
979 && (wPreferences.focus_mode==WKF_CLICK
980 || wPreferences.auto_focus)) {
981 DoWindowBirth(wwin);
983 XMapSubwindows(dpy, wwin->frame->core->window);
984 wWindowMap(wwin);
985 } else {
986 XMapSubwindows(dpy, wwin->frame->core->window);
989 /* setup stacking descriptor */
990 if (wPreferences.on_top_transients && wwin->transient_for!=None
991 && wwin->transient_for!=scr->root_win) {
992 WWindow *tmp;
993 tmp = wWindowFor(wwin->transient_for);
994 if (tmp)
995 wwin->frame->core->stacking->child_of = tmp->frame->core;
996 } else {
997 wwin->frame->core->stacking->child_of = NULL;
1001 if (!scr->focused_window) {
1002 /* first window on the list */
1003 wwin->next = NULL;
1004 wwin->prev = NULL;
1005 scr->focused_window = wwin;
1006 } else {
1007 WWindow *tmp;
1009 /* add window at beginning of focus window list */
1010 tmp = scr->focused_window;
1011 while (tmp->prev)
1012 tmp = tmp->prev;
1013 tmp->prev = wwin;
1014 wwin->next = tmp;
1015 wwin->prev = NULL;
1019 XUngrabServer(dpy);
1022 *--------------------------------------------------
1024 * Final preparations before window is ready to go
1026 *--------------------------------------------------
1029 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1032 if (!iconic && workspace == scr->current_workspace) {
1033 WWindow *tmp = wWindowFor(wwin->transient_for);
1035 if ((tmp && tmp->flags.focused) || wPreferences.auto_focus)
1036 wSetFocusTo(scr, wwin);
1037 } else {
1038 wwin->flags.ignore_next_unmap = 1;
1040 wWindowResetMouseGrabs(wwin);
1042 if (!wwin->window_flags.no_bind_keys) {
1043 wWindowSetKeyGrabs(wwin);
1047 * Prevent window withdrawal when getting the
1048 * unmap notifies generated during reparenting
1050 wwin->flags.mapped=0;
1052 XSync(dpy, 0);
1054 wColormapInstallForWindow(wwin->screen_ptr, scr->cmap_window);
1056 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1058 *--------------------------------------------------
1059 * Cleanup temporary stuff
1060 *--------------------------------------------------
1063 if (win_state)
1064 wWindowDeleteSavedState(win_state);
1066 return wwin;
1073 WWindow*
1074 wManageInternalWindow(WScreen *scr, Window window, Window owner,
1075 char *title, int x, int y, int width, int height)
1077 WWindow *wwin;
1078 int foo;
1080 wwin = wWindowCreate();
1082 wwin->flags.internal_window = 1;
1083 wwin->window_flags.omnipresent = 1;
1084 wwin->window_flags.no_shadeable = 1;
1085 wwin->window_flags.no_resizable = 1;
1086 wwin->window_flags.no_miniaturizable = 1;
1088 wwin->focus_mode = WFM_PASSIVE;
1090 wwin->client_win = window;
1091 wwin->screen_ptr = scr;
1093 wwin->transient_for = owner;
1095 wwin->client.x = x;
1096 wwin->client.y = y;
1097 wwin->client.width = width;
1098 wwin->client.height = height;
1100 wwin->frame_x = wwin->client.x;
1101 wwin->frame_y = wwin->client.y;
1104 foo = WFF_RIGHT_BUTTON;
1105 foo |= WFF_TITLEBAR;
1107 wwin->frame = wFrameWindowCreate(scr, WMFloatingLevel,
1108 wwin->frame_x, wwin->frame_y,
1109 width, height, foo,
1110 scr->window_title_texture,
1111 (WTexture**)scr->resizebar_texture,
1112 scr->window_title_pixel,
1113 &scr->window_title_gc,
1114 &scr->title_font);
1116 XSaveContext(dpy, window, wWinContext, (XPointer)&wwin->client_descriptor);
1118 wwin->frame->flags.is_client_window_frame = 1;
1119 wwin->frame->flags.justification = wPreferences.title_justification;
1121 wFrameWindowChangeTitle(wwin->frame, title);
1123 /* setup button images */
1124 wWindowUpdateButtonImages(wwin);
1126 /* hide buttons */
1127 wFrameWindowHideButton(wwin->frame, WFF_RIGHT_BUTTON);
1129 wwin->frame->child = wwin;
1131 wwin->frame->workspace = wwin->screen_ptr->current_workspace;
1133 wwin->frame->on_click_right = windowCloseClick;
1135 wwin->frame->on_mousedown_titlebar = titlebarMouseDown;
1136 wwin->frame->on_dblclick_titlebar = titlebarDblClick;
1138 wwin->frame->on_mousedown_resizebar = resizebarMouseDown;
1140 wwin->client.y += wwin->frame->top_width;
1141 XReparentWindow(dpy, wwin->client_win, wwin->frame->core->window,
1142 0, wwin->frame->top_width);
1144 wWindowConfigure(wwin, wwin->frame_x, wwin->frame_y,
1145 wwin->client.width, wwin->client.height);
1147 /* setup the frame descriptor */
1148 wwin->frame->core->descriptor.handle_mousedown = frameMouseDown;
1149 wwin->frame->core->descriptor.parent = wwin;
1150 wwin->frame->core->descriptor.parent_type = WCLASS_WINDOW;
1153 XLowerWindow(dpy, window);
1154 XMapSubwindows(dpy, wwin->frame->core->window);
1156 /* setup stacking descriptor */
1157 if (wPreferences.on_top_transients && wwin->transient_for!=None
1158 && wwin->transient_for!=scr->root_win) {
1159 WWindow *tmp;
1160 tmp = wWindowFor(wwin->transient_for);
1161 if (tmp)
1162 wwin->frame->core->stacking->child_of = tmp->frame->core;
1163 } else {
1164 wwin->frame->core->stacking->child_of = NULL;
1168 if (!scr->focused_window) {
1169 /* first window on the list */
1170 wwin->next = NULL;
1171 wwin->prev = NULL;
1172 scr->focused_window = wwin;
1173 } else {
1174 WWindow *tmp;
1176 /* add window at beginning of focus window list */
1177 tmp = scr->focused_window;
1178 while (tmp->prev)
1179 tmp = tmp->prev;
1180 tmp->prev = wwin;
1181 wwin->next = tmp;
1182 wwin->prev = NULL;
1185 wFrameWindowChangeState(wwin->frame, WS_UNFOCUSED);
1187 /* if (wPreferences.auto_focus)*/
1188 wSetFocusTo(scr, wwin);
1190 wWindowResetMouseGrabs(wwin);
1192 wWindowSetKeyGrabs(wwin);
1194 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_ADD);
1196 return wwin;
1201 *----------------------------------------------------------------------
1202 * wUnmanageWindow--
1203 * Removes the frame window from a window and destroys all data
1204 * related to it. The window will be reparented back to the root window
1205 * if restore is True.
1207 * Side effects:
1208 * Everything related to the window is destroyed and the window
1209 * is removed from the window lists. Focus is set to the previous on the
1210 * window list.
1211 *----------------------------------------------------------------------
1213 void
1214 wUnmanageWindow(WWindow *wwin, int restore)
1216 WCoreWindow *frame = wwin->frame->core;
1217 WWindow *owner;
1218 WWindow *newFocusedWindow;
1219 int wasNotFocused;
1220 WScreen *scr = wwin->screen_ptr;
1222 /* First close attribute editor window if open */
1223 if (wwin->flags.inspector_open) {
1224 WWindow *pwin = wwin->inspector->frame; /* the inspector window */
1225 (*pwin->frame->on_click_right)(NULL, pwin, NULL);
1228 /* Close window menu if it's open for this window */
1229 if (wwin->flags.menu_open_for_me) {
1230 CloseWindowMenu(scr);
1232 if (!wwin->flags.internal_window)
1233 XRemoveFromSaveSet(dpy, wwin->client_win);
1234 XSelectInput(dpy, wwin->client_win, NoEventMask);
1236 XUnmapWindow(dpy, frame->window);
1238 /* deselect window */
1239 wSelectWindow(wwin, False);
1241 /* remove all pending events on window */
1242 /* I think this only matters for autoraise */
1243 if (wPreferences.raise_delay)
1244 WMDeleteTimerWithClientData(wwin->frame->core);
1246 XFlush(dpy);
1248 UpdateSwitchMenu(scr, wwin, ACTION_REMOVE);
1250 /* reparent the window back to the root */
1251 if (restore)
1252 wClientRestore(wwin);
1254 if (wwin->transient_for!=scr->root_win) {
1255 owner = wWindowFor(wwin->transient_for);
1256 if (owner) {
1257 owner->flags.semi_focused = 0;
1258 wFrameWindowChangeState(owner->frame, WS_UNFOCUSED);
1262 wasNotFocused = !wwin->flags.focused;
1264 /* remove from window focus list */
1265 if (!wwin->prev && !wwin->next) {
1266 /* was the only window */
1267 scr->focused_window = NULL;
1268 newFocusedWindow = NULL;
1269 } else {
1270 WWindow *tmp;
1272 if (wwin->prev)
1273 wwin->prev->next = wwin->next;
1274 if (wwin->next)
1275 wwin->next->prev = wwin->prev;
1276 else {
1277 scr->focused_window = wwin->prev;
1278 scr->focused_window->next = NULL;
1281 /* if in click to focus mode and the window
1282 * was a transient, focus the owner window
1284 tmp = NULL;
1285 if (wPreferences.focus_mode==WKF_CLICK) {
1286 tmp = wWindowFor(wwin->transient_for);
1287 if (tmp && (!tmp->flags.mapped || tmp->window_flags.no_focusable)) {
1288 tmp = NULL;
1291 /* otherwise, focus the next one in the focus list */
1292 if (!tmp) {
1293 tmp = scr->focused_window;
1294 while (tmp) {
1295 if (!tmp->window_flags.no_focusable
1296 && (tmp->flags.mapped || tmp->flags.shaded))
1297 break;
1298 tmp = tmp->prev;
1301 if (wPreferences.focus_mode==WKF_CLICK) {
1302 newFocusedWindow = tmp;
1303 } else if (wPreferences.focus_mode==WKF_SLOPPY
1304 || wPreferences.focus_mode==WKF_POINTER) {
1305 unsigned int mask;
1306 int foo;
1307 Window bar, win;
1309 /* This is to let the root window get the keyboard input
1310 * if Sloppy focus mode and no other window get focus.
1311 * This way keybindings will not freeze.
1313 tmp = NULL;
1314 if (XQueryPointer(dpy, scr->root_win, &bar, &win,
1315 &foo, &foo, &foo, &foo, &mask))
1316 tmp = wWindowFor(win);
1317 if (tmp == wwin)
1318 tmp = NULL;
1319 newFocusedWindow = tmp;
1320 } else {
1321 newFocusedWindow = NULL;
1324 #ifdef DEBUG
1325 printf("destroying window %x frame %x\n", (unsigned)wwin->client_win,
1326 (unsigned)frame->window);
1327 #endif
1328 if (!wasNotFocused)
1329 wSetFocusTo(scr, newFocusedWindow);
1330 wWindowDestroy(wwin);
1331 XFlush(dpy);
1335 void
1336 wWindowFocus(WWindow *wwin)
1338 #ifdef KEEP_XKB_LOCK_STATUS
1339 if (wPreferences.modelock) {
1340 if (!wwin->flags.focused) {
1341 XkbLockGroup(dpy, XkbUseCoreKbd, wwin->languagemode);
1344 #endif /* KEEP_XKB_LOCK_STATUS */
1346 wFrameWindowChangeState(wwin->frame, WS_FOCUSED);
1348 wwin->flags.focused=1;
1350 wWindowResetMouseGrabs(wwin);
1352 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1356 void
1357 wWindowMap(WWindow *wwin)
1359 XMapWindow(dpy, wwin->frame->core->window);
1360 wwin->flags.mapped = 1;
1365 void
1366 wWindowUnfocus(WWindow *wwin)
1368 #ifdef KEEP_XKB_LOCK_STATUS
1369 static XkbStateRec staterec;
1370 if (wPreferences.modelock) {
1371 if (wwin->flags.focused) {
1372 XkbGetState(dpy,XkbUseCoreKbd,&staterec);
1373 wwin->languagemode=staterec.compat_state&32?1:0;
1374 XkbLockGroup(dpy,XkbUseCoreKbd,0); /* reset to workspace */
1377 #endif /* KEEP_XKB_LOCK_STATUS */
1379 CloseWindowMenu(wwin->screen_ptr);
1381 wFrameWindowChangeState(wwin->frame, wwin->flags.semi_focused
1382 ? WS_PFOCUSED : WS_UNFOCUSED);
1384 if (wwin->transient_for!=None
1385 && wwin->transient_for!=wwin->screen_ptr->root_win) {
1386 WWindow *owner;
1387 owner = wWindowFor(wwin->transient_for);
1388 if (owner && owner->flags.semi_focused) {
1389 owner->flags.semi_focused = 0;
1390 if (owner->flags.mapped || owner->flags.shaded) {
1391 wWindowUnfocus(owner);
1392 wFrameWindowPaint(owner->frame);
1396 wwin->flags.focused=0;
1397 wWindowResetMouseGrabs(wwin);
1399 UpdateSwitchMenu(wwin->screen_ptr, wwin, ACTION_CHANGE_STATE);
1405 *----------------------------------------------------------------------
1407 * wWindowConstrainSize--
1408 * Constrains size for the client window, taking the maximal size,
1409 * window resize increments and other size hints into account.
1411 * Returns:
1412 * The closest size to what was given that the client window can
1413 * have.
1415 *----------------------------------------------------------------------
1417 void
1418 wWindowConstrainSize(WWindow *wwin, int *nwidth, int *nheight)
1420 XSizeHints *sizeh = wwin->normal_hints;
1421 int width = *nwidth;
1422 int height = *nheight;
1423 int winc = sizeh->width_inc;
1424 int hinc = sizeh->height_inc;
1426 if (width < sizeh->min_width)
1427 width = sizeh->min_width;
1428 if (height < sizeh->min_height)
1429 height = sizeh->min_height;
1431 if (width > sizeh->max_width)
1432 width = sizeh->max_width;
1433 if (height > sizeh->max_height)
1434 height = sizeh->max_height;
1436 /* aspect ratio code borrowed from olwm */
1437 if (sizeh->flags & PAspect) {
1438 /* adjust max aspect ratio */
1439 if (!(sizeh->max_aspect.x==1 && sizeh->max_aspect.y==1)
1440 && width * sizeh->max_aspect.y > height * sizeh->max_aspect.x) {
1441 if (sizeh->max_aspect.x > sizeh->max_aspect.y) {
1442 height = (width * sizeh->max_aspect.y) / sizeh->max_aspect.x;
1443 if (height > sizeh->max_height) {
1444 height = sizeh->max_height;
1445 width = (height*sizeh->max_aspect.x) / sizeh->max_aspect.y;
1447 } else {
1448 width = (height * sizeh->max_aspect.x) / sizeh->max_aspect.y;
1449 if (width > sizeh->max_width) {
1450 width = sizeh->max_width;
1451 height = (width*sizeh->max_aspect.y) / sizeh->max_aspect.x;
1456 /* adjust min aspect ratio */
1457 if (!(sizeh->min_aspect.x==1 && sizeh->min_aspect.y==1)
1458 && width * sizeh->min_aspect.y < height * sizeh->min_aspect.x) {
1459 if (sizeh->min_aspect.x > sizeh->min_aspect.y) {
1460 height = (width * sizeh->min_aspect.y) / sizeh->min_aspect.x;
1461 if (height < sizeh->min_height) {
1462 height = sizeh->min_height;
1463 width = (height*sizeh->min_aspect.x) / sizeh->min_aspect.y;
1465 } else {
1466 width = (height * sizeh->min_aspect.x) / sizeh->min_aspect.y;
1467 if (width > sizeh->min_width) {
1468 width = sizeh->min_width;
1469 height = (width*sizeh->min_aspect.y) / sizeh->min_aspect.x;
1475 if (sizeh->base_width != 0) {
1476 width = (((width - sizeh->base_width) / winc) * winc)
1477 + sizeh->base_width;
1478 } else {
1479 width = (((width - sizeh->min_width) / winc) * winc)
1480 + sizeh->min_width;
1483 if (sizeh->base_width != 0) {
1484 height = (((height - sizeh->base_height) / hinc) * hinc)
1485 + sizeh->base_height;
1486 } else {
1487 height = (((height - sizeh->min_height) / hinc) * hinc)
1488 + sizeh->min_height;
1491 *nwidth = width;
1492 *nheight = height;
1496 void
1497 wWindowChangeWorkspace(WWindow *wwin, int workspace)
1499 WScreen *scr = wwin->screen_ptr;
1500 WApplication *wapp;
1501 int unmap = 0;
1503 if (workspace >= scr->workspace_count || workspace < 0
1504 || workspace == wwin->frame->workspace)
1505 return;
1507 if (workspace != scr->current_workspace) {
1508 /* Sent to other workspace. Unmap window */
1509 if ((wwin->flags.mapped||wwin->flags.shaded)
1510 && !wwin->window_flags.omnipresent
1511 && !wwin->flags.changing_workspace) {
1513 wapp = wApplicationOf(wwin->main_window);
1514 if (wapp) {
1515 wapp->last_workspace = workspace;
1517 unmap = 1;
1518 wwin->flags.mapped = 0;
1519 wSetFocusTo(scr, NULL);
1521 } else {
1522 /* brought to current workspace. Map window */
1523 if (!wwin->flags.mapped &&
1524 !(wwin->flags.miniaturized || wwin->flags.hidden)) {
1525 wWindowMap(wwin);
1528 if (!wwin->window_flags.omnipresent) {
1529 wwin->frame->workspace = workspace;
1530 UpdateSwitchMenu(scr, wwin, ACTION_CHANGE_WORKSPACE);
1532 if (unmap)
1533 XUnmapWindow(dpy, wwin->frame->core->window);
1537 void
1538 wWindowSynthConfigureNotify(WWindow *wwin)
1540 XEvent sevent;
1542 sevent.type = ConfigureNotify;
1543 sevent.xconfigure.display = dpy;
1544 sevent.xconfigure.event = wwin->client_win;
1545 sevent.xconfigure.window = wwin->client_win;
1547 sevent.xconfigure.x = wwin->client.x;
1548 sevent.xconfigure.y = wwin->client.y;
1549 sevent.xconfigure.width = wwin->client.width;
1550 sevent.xconfigure.height = wwin->client.height;
1552 sevent.xconfigure.border_width = wwin->old_border_width;
1553 if (wwin->window_flags.no_titlebar)
1554 sevent.xconfigure.above = None;
1555 else
1556 sevent.xconfigure.above = wwin->frame->titlebar->window;
1558 sevent.xconfigure.override_redirect = False;
1559 XSendEvent(dpy, wwin->client_win, False, StructureNotifyMask, &sevent);
1560 XFlush(dpy);
1565 *----------------------------------------------------------------------
1566 * wWindowConfigure--
1567 * Configures the frame, decorations and client window to the
1568 * specified geometry. The geometry is not checked for validity,
1569 * wWindowConstrainSize() must be used for that.
1570 * The size parameters are for the client window, but the position is
1571 * for the frame.
1572 * The client window receives a ConfigureNotify event, according
1573 * to what ICCCM says.
1575 * Returns:
1576 * None
1578 * Side effects:
1579 * Window size and position are changed and client window receives
1580 * a ConfigureNotify event.
1581 *----------------------------------------------------------------------
1583 void
1584 wWindowConfigure(wwin, req_x, req_y, req_width, req_height)
1585 WWindow *wwin;
1586 int req_x, req_y; /* new position of the frame */
1587 int req_width, req_height; /* new size of the client */
1589 int synth_notify = False;
1590 int resize;
1592 resize = (req_width!=wwin->client.width
1593 || req_height!=wwin->client.height);
1595 * if the window is being moved but not resized then
1596 * send a synthetic ConfigureNotify
1598 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y) && !resize) {
1599 synth_notify = True;
1602 if (wwin->window_flags.dont_move_off)
1603 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1604 req_width, req_height);
1605 if (resize) {
1606 if (req_width < MIN_WINDOW_SIZE)
1607 req_width = MIN_WINDOW_SIZE;
1608 if (req_height < MIN_WINDOW_SIZE)
1609 req_height = MIN_WINDOW_SIZE;
1611 /* If growing, resize inner part before frame,
1612 * if shrinking, resize frame before.
1613 * This will prevent the frame (that can have a different color)
1614 * to be exposed, causing flicker */
1615 if (req_height > wwin->frame->core->height
1616 || req_width > wwin->frame->core->width)
1617 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1619 if (wwin->flags.shaded) {
1620 wFrameWindowResize(wwin->frame, req_width, wwin->frame->core->height);
1621 wwin->old_geometry.height = req_height;
1622 } else {
1623 wFrameWindowResizeInternal(wwin->frame, req_width, req_height);
1626 if (!(req_height > wwin->frame->core->height
1627 || req_width > wwin->frame->core->width))
1628 XResizeWindow(dpy, wwin->client_win, req_width, req_height);
1630 wwin->client.x = req_x;
1631 wwin->client.y = req_y + wwin->frame->top_width;
1632 wwin->client.width = req_width;
1633 wwin->client.height = req_height;
1635 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1636 } else {
1637 wwin->client.x = req_x;
1638 wwin->client.y = req_y + wwin->frame->top_width;
1640 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1642 wwin->frame_x = req_x;
1643 wwin->frame_y = req_y;
1645 #ifdef SHAPE
1646 if (wShapeSupported && wwin->flags.shaped && resize) {
1647 wWindowSetShape(wwin);
1649 #endif
1651 if (synth_notify)
1652 wWindowSynthConfigureNotify(wwin);
1653 XFlush(dpy);
1657 void
1658 wWindowMove(wwin, req_x, req_y)
1659 WWindow *wwin;
1660 int req_x, req_y; /* new position of the frame */
1662 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1663 int synth_notify = False;
1665 /* Send a synthetic ConfigureNotify event for every window movement. */
1666 if ((req_x!=wwin->frame_x || req_y!=wwin->frame_y)) {
1667 synth_notify = True;
1669 #else
1670 /* A single synthetic ConfigureNotify event is sent at the end of
1671 * a completed (opaque) movement in moveres.c */
1672 #endif
1674 if (wwin->window_flags.dont_move_off)
1675 wScreenBringInside(wwin->screen_ptr, &req_x, &req_y,
1676 wwin->frame->core->width, wwin->frame->core->height);
1678 wwin->client.x = req_x;
1679 wwin->client.y = req_y + wwin->frame->top_width;
1681 XMoveWindow(dpy, wwin->frame->core->window, req_x, req_y);
1683 wwin->frame_x = req_x;
1684 wwin->frame_y = req_y;
1686 #ifdef CONFIGURE_WINDOW_WHILE_MOVING
1687 if (synth_notify)
1688 wWindowSynthConfigureNotify(wwin);
1689 #endif
1693 void
1694 wWindowUpdateButtonImages(WWindow *wwin)
1696 WScreen *scr = wwin->screen_ptr;
1697 Pixmap pixmap, mask;
1698 WFrameWindow *fwin = wwin->frame;
1700 if (wwin->window_flags.no_titlebar)
1701 return;
1703 /* miniaturize button */
1705 if (!wwin->window_flags.no_miniaturize_button) {
1706 if (wwin->wm_gnustep_attr
1707 && wwin->wm_gnustep_attr->flags & GSMiniaturizePixmapAttr) {
1708 pixmap = wwin->wm_gnustep_attr->miniaturize_pixmap;
1710 if (wwin->wm_gnustep_attr->flags&GSMiniaturizeMaskAttr) {
1711 mask = wwin->wm_gnustep_attr->miniaturize_mask;
1712 } else {
1713 mask = None;
1716 if (fwin->lbutton_image
1717 && (fwin->lbutton_image->image != pixmap
1718 || fwin->lbutton_image->mask != mask)) {
1719 wPixmapDestroy(fwin->lbutton_image);
1720 fwin->lbutton_image = NULL;
1723 if (!fwin->lbutton_image) {
1724 fwin->lbutton_image = wPixmapCreate(scr, pixmap, mask);
1725 fwin->lbutton_image->client_owned = 1;
1726 fwin->lbutton_image->client_owned_mask = 1;
1728 } else {
1729 if (fwin->lbutton_image && !fwin->lbutton_image->shared) {
1730 wPixmapDestroy(fwin->lbutton_image);
1732 fwin->lbutton_image = scr->b_pixmaps[WBUT_ICONIFY];
1736 /* close button */
1738 if (!wwin->window_flags.no_close_button) {
1739 if (wwin->wm_gnustep_attr
1740 && wwin->wm_gnustep_attr->flags&GSClosePixmapAttr) {
1741 pixmap = wwin->wm_gnustep_attr->close_pixmap;
1743 if (wwin->wm_gnustep_attr->flags&GSCloseMaskAttr) {
1744 mask = wwin->wm_gnustep_attr->close_mask;
1745 } else {
1746 mask = None;
1749 if (fwin->rbutton_image
1750 && (fwin->rbutton_image->image != pixmap
1751 || fwin->rbutton_image->mask != mask)) {
1752 wPixmapDestroy(fwin->rbutton_image);
1753 fwin->rbutton_image = NULL;
1756 if (!fwin->rbutton_image) {
1757 fwin->rbutton_image = wPixmapCreate(scr, pixmap, mask);
1758 fwin->rbutton_image->client_owned = 1;
1759 fwin->rbutton_image->client_owned_mask = 1;
1761 } else if (wwin->window_flags.kill_close) {
1762 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
1763 wPixmapDestroy(fwin->rbutton_image);
1765 fwin->rbutton_image = scr->b_pixmaps[WBUT_KILL];
1766 } else if (wwin->window_flags.broken_close) {
1767 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
1768 wPixmapDestroy(fwin->rbutton_image);
1770 fwin->rbutton_image = scr->b_pixmaps[WBUT_BROKENCLOSE];
1771 } else {
1772 if (fwin->rbutton_image && !fwin->rbutton_image->shared) {
1773 wPixmapDestroy(fwin->rbutton_image);
1775 fwin->rbutton_image = scr->b_pixmaps[WBUT_CLOSE];
1779 /* force buttons to be redrawn */
1780 fwin->flags.need_texture_change = 1;
1781 wFrameWindowPaint(fwin);
1786 *---------------------------------------------------------------------------
1787 * wWindowConfigureBorders--
1788 * Update window border configuration according to window_flags
1790 *---------------------------------------------------------------------------
1792 void
1793 wWindowConfigureBorders(WWindow *wwin)
1795 if (wwin->frame) {
1796 int flags;
1797 int newy, oldh;
1799 flags = WFF_LEFT_BUTTON|WFF_RIGHT_BUTTON;
1800 if (!wwin->window_flags.no_titlebar)
1801 flags |= WFF_TITLEBAR;
1802 if (!wwin->window_flags.no_resizebar)
1803 flags |= WFF_RESIZEBAR;
1805 oldh = wwin->frame->top_width;
1806 wFrameWindowUpdateBorders(wwin->frame, flags);
1807 if (oldh != wwin->frame->top_width) {
1808 newy = wwin->frame_y + oldh - wwin->frame->top_width;
1810 XMoveWindow(dpy, wwin->client_win, 0, wwin->frame->top_width);
1811 wWindowConfigure(wwin, wwin->frame_x, newy,
1812 wwin->client.width, wwin->client.height);
1815 flags = 0;
1816 if (!wwin->window_flags.no_miniaturize_button
1817 && wwin->frame->flags.hide_left_button)
1818 flags |= WFF_LEFT_BUTTON;
1820 if (!wwin->window_flags.no_close_button
1821 && wwin->frame->flags.hide_right_button)
1822 flags |= WFF_RIGHT_BUTTON;
1824 if (flags!=0) {
1825 wWindowUpdateButtonImages(wwin);
1826 wFrameWindowShowButton(wwin->frame, flags);
1829 flags = 0;
1830 if (wwin->window_flags.no_miniaturize_button
1831 && !wwin->frame->flags.hide_left_button)
1832 flags |= WFF_LEFT_BUTTON;
1834 if (wwin->window_flags.no_close_button
1835 && !wwin->frame->flags.hide_right_button)
1836 flags |= WFF_RIGHT_BUTTON;
1838 if (flags!=0)
1839 wFrameWindowHideButton(wwin->frame, flags);
1841 #ifdef SHAPE
1842 if (wShapeSupported && wwin->flags.shaped) {
1843 wWindowSetShape(wwin);
1845 #endif
1850 void
1851 wWindowSaveState(WWindow *wwin)
1853 CARD32 data[9];
1855 memset(data, 0, sizeof(CARD32)*9);
1856 data[0] = wwin->frame->workspace;
1857 data[2] = wwin->flags.shaded;
1858 data[3] = wwin->flags.hidden;
1860 XChangeProperty(dpy, wwin->client_win, _XA_WINDOWMAKER_STATE,
1861 _XA_WINDOWMAKER_STATE, 32, PropModeReplace,
1862 (unsigned char *)data, 9);
1866 static int
1867 getSavedState(Window window, WSavedState **state)
1869 Atom type_ret;
1870 int fmt_ret;
1871 unsigned long nitems_ret;
1872 unsigned long bytes_after_ret;
1873 CARD32 *data;
1875 if (XGetWindowProperty(dpy, window, _XA_WINDOWMAKER_STATE, 0, 9,
1876 True, _XA_WINDOWMAKER_STATE,
1877 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
1878 (unsigned char **)&data)!=Success || !data)
1879 return 0;
1881 *state = malloc(sizeof(WSavedState));
1883 if (*state) {
1884 (*state)->workspace = data[0];
1885 (*state)->miniaturized = data[1];
1886 (*state)->shaded = data[2];
1887 (*state)->hidden = data[3];
1888 (*state)->use_geometry = data[4];
1889 (*state)->x = data[5];
1890 (*state)->y = data[6];
1891 (*state)->w = data[7];
1892 (*state)->h = data[8];
1894 XFree(data);
1896 if (*state && type_ret==_XA_WINDOWMAKER_STATE)
1897 return 1;
1898 else
1899 return 0;
1903 #ifdef SHAPE
1904 void
1905 wWindowClearShape(WWindow *wwin)
1907 XShapeCombineMask(dpy, wwin->frame->core->window, ShapeBounding,
1908 0, wwin->frame->top_width, None, ShapeSet);
1909 XFlush(dpy);
1912 void
1913 wWindowSetShape(WWindow *wwin)
1915 XRectangle rect[2];
1916 int count;
1917 #ifdef OPTIMIZE_SHAPE
1918 XRectangle *rects;
1919 XRectangle *urec;
1920 int ordering;
1922 /* only shape is the client's */
1923 if (wwin->window_flags.no_titlebar && wwin->window_flags.no_resizebar) {
1924 goto alt_code;
1927 /* Get array of rectangles describing the shape mask */
1928 rects = XShapeGetRectangles(dpy, wwin->client_win, ShapeBounding,
1929 &count, &ordering);
1930 if (!rects) {
1931 goto alt_code;
1934 urec = malloc(sizeof(XRectangle)*(count+2));
1935 if (!urec) {
1936 XFree(rects);
1937 goto alt_code;
1940 /* insert our decoration rectangles in the rect list */
1941 memcpy(urec, rects, sizeof(XRectangle)*count);
1942 XFree(rects);
1944 if (!wwin->window_flags.no_titlebar) {
1945 urec[count].x = -1;
1946 urec[count].y = -1 - wwin->frame->top_width;
1947 urec[count].width = wwin->frame->core->width + 2;
1948 urec[count].height = wwin->frame->top_width + 1;
1949 count++;
1951 if (!wwin->window_flags.no_resizebar) {
1952 urec[count].x = -1;
1953 urec[count].y = wwin->frame->core->height
1954 - wwin->frame->bottom_width - wwin->frame->top_width;
1955 urec[count].width = wwin->frame->core->width + 2;
1956 urec[count].height = wwin->frame->bottom_width + 1;
1957 count++;
1960 /* shape our frame window */
1961 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
1962 0, wwin->frame->top_width, urec, count,
1963 ShapeSet, Unsorted);
1964 XFlush(dpy);
1965 free(urec);
1966 return;
1968 alt_code:
1969 #endif /* OPTIMIZE_SHAPE */
1970 count = 0;
1971 if (!wwin->window_flags.no_titlebar) {
1972 rect[count].x = -1;
1973 rect[count].y = -1;
1974 rect[count].width = wwin->frame->core->width + 2;
1975 rect[count].height = wwin->frame->top_width + 1;
1976 count++;
1978 if (!wwin->window_flags.no_resizebar) {
1979 rect[count].x = -1;
1980 rect[count].y = wwin->frame->core->height - wwin->frame->bottom_width;
1981 rect[count].width = wwin->frame->core->width + 2;
1982 rect[count].height = wwin->frame->bottom_width + 1;
1983 count++;
1985 if (count > 0) {
1986 XShapeCombineRectangles(dpy, wwin->frame->core->window, ShapeBounding,
1987 0, 0, rect, count, ShapeSet, Unsorted);
1989 XShapeCombineShape(dpy, wwin->frame->core->window, ShapeBounding,
1990 0, wwin->frame->top_width, wwin->client_win,
1991 ShapeBounding, (count > 0 ? ShapeUnion : ShapeSet));
1992 XFlush(dpy);
1994 #endif /* SHAPE */
1996 /* ====================================================================== */
1998 static FocusMode
1999 getFocusMode(WWindow *wwin)
2001 FocusMode mode;
2003 if ((wwin->wm_hints) && (wwin->wm_hints->flags & InputHint)) {
2004 if (wwin->wm_hints->input == True) {
2005 if (wwin->protocols.TAKE_FOCUS)
2006 mode = WFM_LOCALLY_ACTIVE;
2007 else
2008 mode = WFM_PASSIVE;
2009 } else {
2010 if (wwin->protocols.TAKE_FOCUS)
2011 mode = WFM_GLOBALLY_ACTIVE;
2012 else
2013 mode = WFM_NO_INPUT;
2015 } else {
2016 mode = WFM_PASSIVE;
2018 return mode;
2022 void
2023 wWindowSetKeyGrabs(WWindow *wwin)
2025 int i;
2026 WShortKey *key;
2028 for (i=0; i<WKBD_LAST; i++) {
2029 key = &wKeyBindings[i];
2031 if (key->keycode==0)
2032 continue;
2033 if (key->modifier!=AnyModifier) {
2034 XGrabKey(dpy, key->keycode, key->modifier|LockMask,
2035 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2036 #ifdef NUMLOCK_HACK
2037 /* Also grab all modifier combinations possible that include,
2038 * LockMask, ScrollLockMask and NumLockMask, so that keygrabs
2039 * work even if the NumLock/ScrollLock key is on.
2041 wHackedGrabKey(key->keycode, key->modifier,
2042 wwin->frame->core->window, True, GrabModeAsync,
2043 GrabModeAsync);
2044 #endif
2046 XGrabKey(dpy, key->keycode, key->modifier,
2047 wwin->frame->core->window, True, GrabModeAsync, GrabModeAsync);
2050 wRootMenuBindShortcuts(wwin->frame->core->window);
2055 void
2056 wWindowResetMouseGrabs(WWindow *wwin)
2058 /* Mouse grabs can't be done on the client window because of
2059 * ICCCM and because clients that try to do the same will crash.
2061 * But there is a problem wich makes tbar buttons of unfocused
2062 * windows not usable as the click goes to the frame window instead
2063 * of the button itself. Must figure a way to fix that.
2066 XUngrabButton(dpy, AnyButton, AnyModifier, wwin->client_win);
2068 if (!wwin->window_flags.no_bind_mouse) {
2069 /* grabs for Meta+drag */
2070 wHackedGrabButton(AnyButton, MOD_MASK, wwin->client_win,
2071 True, ButtonPressMask, GrabModeSync,
2072 GrabModeAsync, None, None);
2075 if (!wwin->flags.focused) {
2076 /* the passive grabs to focus the window */
2077 if (wPreferences.focus_mode == WKF_CLICK)
2078 XGrabButton(dpy, AnyButton, AnyModifier, wwin->client_win,
2079 True, ButtonPressMask, GrabModeSync, GrabModeAsync,
2080 None, None);
2082 XFlush(dpy);
2086 void
2087 wWindowUpdateGNUstepAttr(WWindow *wwin, GNUstepWMAttributes *attr)
2090 if (attr->flags & GSExtraFlagsAttr) {
2091 if (wwin->window_flags.broken_close !=
2092 (attr->extra_flags & GSDocumentEditedFlag)) {
2093 wwin->window_flags.broken_close = !wwin->window_flags.broken_close;
2095 wWindowUpdateButtonImages(wwin);
2102 WMagicNumber
2103 wWindowAddSavedState(char *instance, char *class, char *command,
2104 pid_t pid, WSavedState *state)
2106 WWindowState *wstate;
2108 wstate = malloc(sizeof(WWindowState));
2109 if (!wstate)
2110 return 0;
2112 memset(wstate, 0, sizeof(WWindowState));
2113 wstate->pid = pid;
2114 if (instance)
2115 wstate->instance = wstrdup(instance);
2116 if (class)
2117 wstate->class = wstrdup(class);
2118 if (command)
2119 wstate->command = wstrdup(command);
2120 wstate->state = state;
2122 wstate->next = windowState;
2123 windowState = wstate;
2125 #ifdef DEBUG
2126 printf("Added WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2127 class, command);
2128 #endif
2130 return wstate;
2134 #define SAME(x, y) (((x) && (y) && !strcmp((x), (y))) || (!(x) && !(y)))
2137 WMagicNumber
2138 wWindowGetSavedState(Window win)
2140 char *instance, *class, *command=NULL;
2141 WWindowState *wstate = windowState;
2142 char **argv;
2143 int argc;
2145 if (!wstate)
2146 return NULL;
2148 if (XGetCommand(dpy, win, &argv, &argc)) {
2149 if (argc > 0)
2150 command = FlattenStringList(argv, argc);
2151 XFreeStringList(argv);
2153 if (!command)
2154 return NULL;
2156 if (PropGetWMClass(win, &class, &instance)) {
2157 while (wstate) {
2158 if (SAME(instance, wstate->instance) &&
2159 SAME(class, wstate->class) &&
2160 SAME(command, wstate->command)) {
2161 break;
2163 wstate = wstate->next;
2165 } else {
2166 wstate = NULL;
2169 #ifdef DEBUG
2170 printf("Read WindowState with ID %p, for %s.%s : \"%s\"\n", wstate, instance,
2171 class, command);
2172 #endif
2174 if (command) free(command);
2175 if (instance) XFree(instance);
2176 if (class) XFree(class);
2178 return wstate;
2182 void
2183 wWindowDeleteSavedState(WMagicNumber id)
2185 WWindowState *tmp, *wstate=(WWindowState*)id;
2187 if (!wstate || !windowState)
2188 return;
2190 tmp = windowState;
2191 if (tmp==wstate) {
2192 windowState = wstate->next;
2193 #ifdef DEBUG
2194 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2195 wstate, wstate->instance, wstate->class, wstate->command);
2196 #endif
2197 if (wstate->instance) free(wstate->instance);
2198 if (wstate->class) free(wstate->class);
2199 if (wstate->command) free(wstate->command);
2200 free(wstate->state);
2201 free(wstate);
2202 } else {
2203 while (tmp->next) {
2204 if (tmp->next==wstate) {
2205 tmp->next=wstate->next;
2206 #ifdef DEBUG
2207 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2208 wstate, wstate->instance, wstate->class, wstate->command);
2209 #endif
2210 if (wstate->instance) free(wstate->instance);
2211 if (wstate->class) free(wstate->class);
2212 if (wstate->command) free(wstate->command);
2213 free(wstate->state);
2214 free(wstate);
2215 break;
2217 tmp = tmp->next;
2223 void
2224 wWindowDeleteSavedStatesForPID(pid_t pid)
2226 WWindowState *tmp, *wstate;
2228 if (!windowState)
2229 return;
2231 tmp = windowState;
2232 if (tmp->pid == pid) {
2233 wstate = windowState;
2234 windowState = tmp->next;
2235 #ifdef DEBUG
2236 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2237 wstate, wstate->instance, wstate->class, wstate->command);
2238 #endif
2239 if (wstate->instance) free(wstate->instance);
2240 if (wstate->class) free(wstate->class);
2241 if (wstate->command) free(wstate->command);
2242 free(wstate->state);
2243 free(wstate);
2244 } else {
2245 while (tmp->next) {
2246 if (tmp->next->pid==pid) {
2247 wstate = tmp->next;
2248 tmp->next = wstate->next;
2249 #ifdef DEBUG
2250 printf("Deleted WindowState with ID %p, for %s.%s : \"%s\"\n",
2251 wstate, wstate->instance, wstate->class, wstate->command);
2252 #endif
2253 if (wstate->instance) free(wstate->instance);
2254 if (wstate->class) free(wstate->class);
2255 if (wstate->command) free(wstate->command);
2256 free(wstate->state);
2257 free(wstate);
2258 break;
2260 tmp = tmp->next;
2266 /* ====================================================================== */
2268 static void
2269 resizebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2271 WWindow *wwin = data;
2273 #ifndef NUMLOCK_HACK
2274 if ((event->xbutton.state & ValidModMask)
2275 != (event->xbutton.state & ~LockMask)) {
2276 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2277 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2279 #endif
2281 event->xbutton.state &= ValidModMask;
2283 CloseWindowMenu(wwin->screen_ptr);
2285 if (wPreferences.focus_mode==WKF_CLICK
2286 && !(event->xbutton.state&ControlMask)) {
2287 wSetFocusTo(wwin->screen_ptr, wwin);
2290 if (event->xbutton.button == Button1)
2291 wRaiseFrame(wwin->frame->core);
2293 if (event->xbutton.window != wwin->frame->resizebar->window) {
2294 if (XGrabPointer(dpy, wwin->frame->resizebar->window, True,
2295 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2296 GrabModeAsync, GrabModeAsync, None,
2297 None, CurrentTime)!=GrabSuccess) {
2298 #ifdef DEBUG0
2299 wwarning("pointer grab failed for window move");
2300 #endif
2301 return;
2305 if (event->xbutton.state & MOD_MASK) {
2306 /* move the window */
2307 wMouseMoveWindow(wwin, event);
2308 XUngrabPointer(dpy, CurrentTime);
2309 } else {
2310 wMouseResizeWindow(wwin, event);
2311 XUngrabPointer(dpy, CurrentTime);
2317 static void
2318 titlebarDblClick(WCoreWindow *sender, void *data, XEvent *event)
2320 WWindow *wwin = data;
2322 event->xbutton.state &= ValidModMask;
2324 if (event->xbutton.button==Button1) {
2325 if (event->xbutton.state == 0) {
2326 if (!wwin->window_flags.no_shadeable) {
2327 /* shade window */
2328 if (wwin->flags.shaded)
2329 wUnshadeWindow(wwin);
2330 else
2331 wShadeWindow(wwin);
2333 } else {
2334 int dir = 0;
2336 if (event->xbutton.state & ControlMask)
2337 dir |= MAX_VERTICAL;
2339 if (event->xbutton.state & ShiftMask) {
2340 dir |= MAX_HORIZONTAL;
2341 if (!(event->xbutton.state & ControlMask))
2342 wSelectWindow(wwin, !wwin->flags.selected);
2345 /* maximize window */
2346 if (dir !=0 && !wwin->window_flags.no_resizable) {
2347 if (wwin->flags.maximized)
2348 wUnmaximizeWindow(wwin);
2349 else
2350 wMaximizeWindow(wwin, dir);
2353 } else if (event->xbutton.button==Button3) {
2354 if (event->xbutton.state & MOD_MASK) {
2355 wHideOtherApplications(wwin);
2357 } else if (event->xbutton.button==Button2) {
2358 wSelectWindow(wwin, !wwin->flags.selected);
2363 static void
2364 frameMouseDown(WObjDescriptor *desc, XEvent *event)
2366 WWindow *wwin = desc->parent;
2368 event->xbutton.state &= ValidModMask;
2370 CloseWindowMenu(wwin->screen_ptr);
2372 if (wPreferences.focus_mode==WKF_CLICK
2373 && !(event->xbutton.state&ControlMask)) {
2374 wSetFocusTo(wwin->screen_ptr, wwin);
2376 if (event->xbutton.button == Button1) {
2377 wRaiseFrame(wwin->frame->core);
2380 if (event->xbutton.state & MOD_MASK) {
2381 /* move the window */
2382 if (XGrabPointer(dpy, wwin->client_win, False,
2383 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2384 GrabModeAsync, GrabModeAsync, None,
2385 None, CurrentTime)!=GrabSuccess) {
2386 #ifdef DEBUG0
2387 wwarning("pointer grab failed for window move");
2388 #endif
2389 return;
2391 if (event->xbutton.button == Button3 && !wwin->window_flags.no_resizable)
2392 wMouseResizeWindow(wwin, event);
2393 else
2394 wMouseMoveWindow(wwin, event);
2395 XUngrabPointer(dpy, CurrentTime);
2400 static void
2401 titlebarMouseDown(WCoreWindow *sender, void *data, XEvent *event)
2403 WWindow *wwin = (WWindow*)data;
2405 #ifndef NUMLOCK_HACK
2406 if ((event->xbutton.state & ValidModMask)
2407 != (event->xbutton.state & ~LockMask)) {
2408 wwarning(_("the NumLock, ScrollLock or similar key seems to be turned on.\n"\
2409 "Turn it off or some mouse actions and keyboard shortcuts will not work."));
2411 #endif
2412 event->xbutton.state &= ValidModMask;
2414 CloseWindowMenu(wwin->screen_ptr);
2416 if (wPreferences.focus_mode==WKF_CLICK
2417 && !(event->xbutton.state&ControlMask)) {
2418 wSetFocusTo(wwin->screen_ptr, wwin);
2421 if (event->xbutton.button == Button1
2422 || event->xbutton.button == Button2) {
2424 if (event->xbutton.button == Button1) {
2425 if (event->xbutton.state & MOD_MASK) {
2426 wLowerFrame(wwin->frame->core);
2427 } else {
2428 wRaiseFrame(wwin->frame->core);
2431 if ((event->xbutton.state & ShiftMask)
2432 && !(event->xbutton.state & ControlMask)) {
2433 wSelectWindow(wwin, !wwin->flags.selected);
2434 return;
2436 if (event->xbutton.window != wwin->frame->titlebar->window
2437 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2438 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2439 GrabModeAsync, GrabModeAsync, None,
2440 None, CurrentTime)!=GrabSuccess) {
2441 #ifdef DEBUG0
2442 wwarning("pointer grab failed for window move");
2443 #endif
2444 return;
2447 /* move the window */
2448 wMouseMoveWindow(wwin, event);
2450 XUngrabPointer(dpy, CurrentTime);
2451 } else if (event->xbutton.button == Button3 && event->xbutton.state==0
2452 && !wwin->flags.internal_window) {
2453 WObjDescriptor *desc;
2455 if (event->xbutton.window != wwin->frame->titlebar->window
2456 && XGrabPointer(dpy, wwin->frame->titlebar->window, False,
2457 ButtonMotionMask|ButtonReleaseMask|ButtonPressMask,
2458 GrabModeAsync, GrabModeAsync, None,
2459 None, CurrentTime)!=GrabSuccess) {
2460 #ifdef DEBUG0
2461 wwarning("pointer grab failed for window move");
2462 #endif
2463 return;
2466 OpenWindowMenu(wwin, event->xbutton.x_root,
2467 wwin->frame_y+wwin->frame->top_width, False);
2469 /* allow drag select */
2470 desc = &wwin->screen_ptr->window_menu->menu->descriptor;
2471 event->xany.send_event = True;
2472 (*desc->handle_mousedown)(desc, event);
2474 XUngrabPointer(dpy, CurrentTime);
2480 static void
2481 windowCloseClick(WCoreWindow *sender, void *data, XEvent *event)
2483 WWindow *wwin = data;
2485 event->xbutton.state &= ValidModMask;
2487 CloseWindowMenu(wwin->screen_ptr);
2489 /* if control-click, kill the client */
2490 if (event->xbutton.state & ControlMask) {
2491 wClientKill(wwin);
2492 } else if (wwin->protocols.DELETE_WINDOW && event->xbutton.state==0) {
2493 /* send delete message */
2494 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2499 static void
2500 windowCloseDblClick(WCoreWindow *sender, void *data, XEvent *event)
2502 WWindow *wwin = data;
2504 CloseWindowMenu(wwin->screen_ptr);
2506 /* send delete message */
2507 if (wwin->protocols.DELETE_WINDOW) {
2508 wClientSendProtocol(wwin, _XA_WM_DELETE_WINDOW, LastTimestamp);
2509 } else {
2510 wClientKill(wwin);
2515 static void
2516 windowIconifyClick(WCoreWindow *sender, void *data, XEvent *event)
2518 WWindow *wwin = data;
2520 event->xbutton.state &= ValidModMask;
2522 CloseWindowMenu(wwin->screen_ptr);
2524 if (wwin->protocols.MINIATURIZE_WINDOW && event->xbutton.state==0) {
2525 wClientSendProtocol(wwin, _XA_GNUSTEP_WM_MINIATURIZE_WINDOW,
2526 LastTimestamp);
2527 } else {
2528 WApplication *wapp;
2529 if ((event->xbutton.state & ControlMask) ||
2530 (event->xbutton.button == Button3)) {
2532 wapp = wApplicationOf(wwin->main_window);
2533 if (wapp && !wwin->window_flags.no_appicon)
2534 wHideApplication(wapp);
2535 } else if (event->xbutton.state==0) {
2536 wIconifyWindow(wwin);