Debian Codegraph @

pTk/mTk/generic/tkGrab.c

raw symbols SCIP
1 /*
2 * tkGrab.c --
3 *
4 * This file provides procedures that implement grabs for Tk.
5 *
6 * Copyright (c) 1992-1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkGrab.c,v 1.9 2002/08/31 06:12:20 das Exp $
13 */
14
15 #include "tkPort.h"
16 #include "tkInt.h"
17
18 #if !(defined(__WIN32__) || defined(MAC_TCL) || defined(MAC_OSX_TK))
19 #include "tkUnixInt.h"
20 #endif
21
22 /*
23 * The grab state machine has four states: ungrabbed, button pressed,
24 * grabbed, and button pressed while grabbed. In addition, there are
25 * three pieces of grab state information: the current grab window,
26 * the current restrict window, and whether the mouse is captured.
27 *
28 * The current grab window specifies the point in the Tk window
29 * heirarchy above which pointer events will not be reported. Any
30 * window within the subtree below the grab window will continue to
31 * receive events as normal. Events outside of the grab tree will be
32 * reported to the grab window.
33 *
34 * If the current restrict window is set, then all pointer events will
35 * be reported only to the restrict window. The restrict window is
36 * normally set during an automatic button grab.
37 *
38 * The mouse capture state specifies whether the window system will
39 * report mouse events outside of any Tk toplevels. This is set
40 * during a global grab or an automatic button grab.
41 *
42 * The transitions between different states is given in the following
43 * table:
44 *
45 * Event\State U B G GB
46 * ----------- -- -- -- --
47 * FirstPress B B GB GB
48 * Press B B G GB
49 * Release U B G GB
50 * LastRelease U U G G
51 * Grab G G G G
52 * Ungrab U B U U
53 *
54 * Note: U=Ungrabbed, B=Button, G=Grabbed, GB=Grab and Button
55 *
56 * In addition, the following conditions are always true:
57 *
58 * State\Variable Grab Restrict Capture
59 * -------------- ---- -------- -------
60 * Ungrabbed 0 0 0
61 * Button 0 1 1
62 * Grabbed 1 0 b/g
63 * Grab and Button 1 1 1
64 *
65 * Note: 0 means variable is set to NULL, 1 means variable is set to
66 * some window, b/g means the variable is set to a window if a button
67 * is currently down or a global grab is in effect.
68 *
69 * The final complication to all of this is enter and leave events.
70 * In order to correctly handle all of the various cases, Tk cannot
71 * rely on X enter/leave events in all situations. The following
72 * describes the correct sequence of enter and leave events that
73 * should be observed by Tk scripts:
74 *
75 * Event(state) Enter/Leave From -> To
76 * ------------ ----------------------
77 * LastRelease(B | GB): restrict window -> anc(grab window, event window)
78 * Grab(U | B): event window -> anc(grab window, event window)
79 * Grab(G): anc(old grab window, event window) ->
80 * anc(new grab window, event window)
81 * Grab(GB): restrict window -> anc(new grab window, event window)
82 * Ungrab(G): anc(grab window, event window) -> event window
83 * Ungrab(GB): restrict window -> event window
84 *
85 * Note: anc(x,y) returns the least ancestor of y that is in the tree
86 * of x, terminating at toplevels.
87 */
88
89 /*
90 * The following structure is used to pass information to
91 * GrabRestrictProc from EatGrabEvents.
92 */
93
94 typedef struct {
95 Display *display; /* Display from which to discard events. */
96 unsigned int serial; /* Serial number with which to compare. */
97 } GrabInfo;
98
99 /*
100 * Bit definitions for grabFlags field of TkDisplay structures:
101 *
102 * GRAB_GLOBAL 1 means this is a global grab (we grabbed via
103 * the server so all applications are locked out).
104 * 0 means this is a local grab that affects
105 * only this application.
106 * GRAB_TEMP_GLOBAL 1 means we've temporarily grabbed via the
107 * server because a button is down and we want
108 * to make sure that we get the button-up
109 * event. The grab will be released when the
110 * last mouse button goes up.
111 */
112
113 #define GRAB_GLOBAL 1
114 #define GRAB_TEMP_GLOBAL 4
115
116 /*
117 * The following structure is a Tcl_Event that triggers a change in
118 * the grabWinPtr field of a display. This event guarantees that
119 * the change occurs in the proper order relative to enter and leave
120 * events.
121 */
122
123 typedef struct NewGrabWinEvent {
124 Tcl_Event header; /* Standard information for all Tcl events. */
125 TkDisplay *dispPtr; /* Display whose grab window is to change. */
126 Window grabWindow; /* New grab window for display. This is
127 * recorded instead of a (TkWindow *) because
128 * it will allow us to detect cases where
129 * the window is destroyed before this event
130 * is processed. */
131 } NewGrabWinEvent;
132
133 /*
134 * The following magic value is stored in the "send_event" field of
135 * EnterNotify and LeaveNotify events that are generated in this
136 * file. This allows us to separate "real" events coming from the
137 * server from those that we generated.
138 */
139
140 #define GENERATED_EVENT_MAGIC ((Bool) 0x147321ac)
141
142 /*
143 * Mask that selects any of the state bits corresponding to buttons,
144 * plus masks that select individual buttons' bits:
145 */
146
147 #define ALL_BUTTONS \
148 (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)
149 static unsigned int buttonStates[] = {
150 Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask
151 };
152
153 /*
154 * Forward declarations for procedures declared later in this file:
155 */
156
157 static void EatGrabEvents _ANSI_ARGS_((TkDisplay *dispPtr,
158 unsigned int serial));
159 static TkWindow * FindCommonAncestor _ANSI_ARGS_((TkWindow *winPtr1,
160 TkWindow *winPtr2, int *countPtr1,
161 int *countPtr2));
162 static Tk_RestrictAction GrabRestrictProc _ANSI_ARGS_((ClientData arg,
163 XEvent *eventPtr));
164 static int GrabWinEventProc _ANSI_ARGS_((Tcl_Event *evPtr,
165 int flags));
166 static void MovePointer2 _ANSI_ARGS_((TkWindow *sourcePtr,
167 TkWindow *destPtr, int mode, int leaveEvents,
168 int EnterEvents));
169 static void QueueGrabWindowChange _ANSI_ARGS_((TkDisplay *dispPtr,
170 TkWindow *grabWinPtr));
171 static void ReleaseButtonGrab _ANSI_ARGS_((TkDisplay *dispPtr));
172
173 /*
174 *----------------------------------------------------------------------
175 *
176 * Tk_GrabObjCmd --
177 *
178 * This procedure is invoked to process the "grab" Tcl command.
179 * See the user documentation for details on what it does.
180 *
181 * Results:
182 * A standard Tcl result.
183 *
184 * Side effects:
185 * See the user documentation.
186 *
187 *----------------------------------------------------------------------
188 */
189
190 /* ARGSUSED */
191 int
192 Tk_GrabObjCmd(clientData, interp, objc, objv)
193 ClientData clientData; /* Main window associated with
194 * interpreter. */
195 Tcl_Interp *interp; /* Current interpreter. */
196 int objc; /* Number of arguments. */
197 Tcl_Obj *CONST objv[]; /* Argument objects. */
198 {
199 int globalGrab;
200 Tk_Window tkwin;
201 TkDisplay *dispPtr;
202 char *arg;
203 int index;
204 int len;
205 static CONST char *optionStrings[] = { "current", "release",
206 "set", "status", (char *) NULL };
207
208 static CONST char *flagStrings[] = { "-global", (char *) NULL };
209
210 enum options { GRABCMD_CURRENT, GRABCMD_RELEASE,
211 GRABCMD_SET, GRABCMD_STATUS };
212
213 if (objc < 2) {
214 /*
215 * Can't use Tcl_WrongNumArgs here because we want the message to
216 * read:
217 * wrong # args: should be "cmd ?-global window" or "cmd option
218 * ?arg arg ...?"
219 * We can fake it with Tcl_WrongNumArgs if we assume the command name
220 * is "grab", but if it has been aliased, the message will be
221 * incorrect.
222 */
223 Tcl_ResetResult(interp);
224 Tcl_AppendResult(interp, "wrong # args: should be \"",
225 Tcl_GetString(objv[0]), " ?-global? window\" or \"",
226 Tcl_GetString(objv[0]), " option ?arg arg ...?\"",
227 (char *) NULL);
228 return TCL_ERROR;
229 }
230
231 /*
232 * First check for a window name or "-global" as the first argument.
233 */
234
235 arg = Tcl_GetStringFromObj(objv[1], &len);
236 if (arg[0] == '.') {
237 /* [grab window] */
238 if (objc != 2) {
239 Tcl_WrongNumArgs(interp, 1, objv, "?-global? window");
240 return TCL_ERROR;
241 }
242 tkwin = Tk_NameToWindow(interp, arg, (Tk_Window) clientData);
243 if (tkwin == NULL) {
244 return TCL_ERROR;
245 }
246 return Tk_Grab(interp, tkwin, 0);
247 } else if (arg[0] == '-' && len > 1) {
248 if (Tcl_GetIndexFromObj(interp, objv[1], flagStrings, "option", 0,
249 &index) != TCL_OK) {
250 return TCL_ERROR;
251 }
252
253 /* [grab -global window] */
254 if (objc != 3) {
255 Tcl_WrongNumArgs(interp, 1, objv, "?-global? window");
256 return TCL_ERROR;
257 }
258 tkwin = Tk_NameToWindow(interp, Tcl_GetString(objv[2]),
259 (Tk_Window) clientData);
260 if (tkwin == NULL) {
261 return TCL_ERROR;
262 }
263 return Tk_Grab(interp, tkwin, 1);
264 }
265
266 /*
267 * First argument is not a window name and not "-global", find out
268 * which option it is.
269 */
270
271 if (Tcl_GetIndexFromObj(interp, objv[1], optionStrings, "option", 0,
272 &index) != TCL_OK) {
273 return TCL_ERROR;
274 }
275
276 switch ((enum options) index) {
277 case GRABCMD_CURRENT: {
278 /* [grab current ?window?] */
279 if (objc > 3) {
280 Tcl_WrongNumArgs(interp, 1, objv, "current ?window?");
281 return TCL_ERROR;
282 }
283 if (objc == 3) {
284 tkwin = Tk_NameToWindow(interp,
285 Tcl_GetString(objv[2]), (Tk_Window) clientData);
286 if (tkwin == NULL) {
287 return TCL_ERROR;
288 }
289 dispPtr = ((TkWindow *) tkwin)->dispPtr;
290 if (dispPtr->eventualGrabWinPtr != NULL) {
291 Tcl_SetResult(interp,
292 dispPtr->eventualGrabWinPtr->pathName, TCL_STATIC);
293 }
294 } else {
295 for (dispPtr = TkGetDisplayList(); dispPtr != NULL;
296 dispPtr = dispPtr->nextPtr) {
297 if (dispPtr->eventualGrabWinPtr != NULL) {
298 Tcl_AppendElement(interp,
299 dispPtr->eventualGrabWinPtr->pathName);
300 }
301 }
302 }
303 return TCL_OK;
304 }
305
306 case GRABCMD_RELEASE: {
307 /* [grab release window] */
308 if (objc != 3) {
309 Tcl_WrongNumArgs(interp, 1, objv, "release window");
310 return TCL_ERROR;
311 }
312 tkwin = Tk_NameToWindow(interp,
313 Tcl_GetString(objv[2]), (Tk_Window) clientData);
314 if (tkwin == NULL) {
315 Tcl_ResetResult(interp);
316 } else {
317 Tk_Ungrab(tkwin);
318 }
319 break;
320 }
321
322 case GRABCMD_SET: {
323 /* [grab set ?-global? window] */
324 if ((objc != 3) && (objc != 4)) {
325 Tcl_WrongNumArgs(interp, 1, objv, "set ?-global? window");
326 return TCL_ERROR;
327 }
328 if (objc == 3) {
329 globalGrab = 0;
330 tkwin = Tk_NameToWindow(interp,
331 Tcl_GetString(objv[2]), (Tk_Window) clientData);
332 } else {
333 globalGrab = 1;
334 /*
335 * We could just test the argument by hand instead of using
336 * Tcl_GetIndexFromObj; the benefit of using the function is
337 * that it sets up the error message for us, so we are
338 * certain to be consistant with the rest of Tcl.
339 */
340 if (Tcl_GetIndexFromObj(interp, objv[2], flagStrings, "option",
341 0, &index) != TCL_OK) {
342 return TCL_ERROR;
343 }
344 tkwin = Tk_NameToWindow(interp,
345 Tcl_GetString(objv[3]), (Tk_Window) clientData);
346 }
347 if (tkwin == NULL) {
348 return TCL_ERROR;
349 }
350 return Tk_Grab(interp, tkwin, globalGrab);
351 }
352
353 case GRABCMD_STATUS: {
354 /* [grab status window] */
355 TkWindow *winPtr;
356
357 if (objc != 3) {
358 Tcl_WrongNumArgs(interp, 1, objv, "status window");
359 return TCL_ERROR;
360 }
361 winPtr = (TkWindow *) Tk_NameToWindow(interp,
362 Tcl_GetString(objv[2]), (Tk_Window) clientData);
363 if (winPtr == NULL) {
364 return TCL_ERROR;
365 }
366 dispPtr = winPtr->dispPtr;
367 if (dispPtr->eventualGrabWinPtr != winPtr) {
368 Tcl_SetResult(interp, "none", TCL_STATIC);
369 } else if (dispPtr->grabFlags & GRAB_GLOBAL) {
370 Tcl_SetResult(interp, "global", TCL_STATIC);
371 } else {
372 Tcl_SetResult(interp, "local", TCL_STATIC);
373 }
374 break;
375 }
376 }
377
378 return TCL_OK;
379 }
380
381 /*
382 *----------------------------------------------------------------------
383 *
384 * Tk_Grab --
385 *
386 * Grabs the pointer and keyboard, so that mouse-related events are
387 * only reported relative to a given window and its descendants.
388 *
389 * Results:
390 * A standard Tcl result is returned. TCL_OK is the normal return
391 * value; if the grab could not be set then TCL_ERROR is returned
392 * and the interp's result will hold an error message.
393 *
394 * Side effects:
395 * Once this call completes successfully, no window outside the
396 * tree rooted at tkwin will receive pointer- or keyboard-related
397 * events until the next call to Tk_Ungrab. If a previous grab was
398 * in effect within this application, then it is replaced with a new
399 * one.
400 *
401 *----------------------------------------------------------------------
402 */
403
404 int
405 Tk_Grab(interp, tkwin, grabGlobal)
406 Tcl_Interp *interp; /* Used for error reporting. */
407 Tk_Window tkwin; /* Window on whose behalf the pointer
408 * is to be grabbed. */
409 int grabGlobal; /* Non-zero means issue a grab to the
410 * server so that no other application
411 * gets mouse or keyboard events.
412 * Zero means the grab only applies
413 * within this application. */
414 {
415 int grabResult, numTries;
416 TkWindow *winPtr = (TkWindow *) tkwin;
417 TkDisplay *dispPtr = winPtr->dispPtr;
418 TkWindow *winPtr2;
419 unsigned int serial;
420
421 ReleaseButtonGrab(dispPtr);
422 if (dispPtr->eventualGrabWinPtr != NULL) {
423 if ((dispPtr->eventualGrabWinPtr == winPtr)
424 && (grabGlobal == ((dispPtr->grabFlags & GRAB_GLOBAL) != 0))) {
425 return TCL_OK;
426 }
427 if (dispPtr->eventualGrabWinPtr->mainPtr != winPtr->mainPtr) {
428 alreadyGrabbed:
429 Tcl_SetResult(interp, "grab failed: another application has grab",
430 TCL_STATIC);
431 return TCL_ERROR;
432 }
433 Tk_Ungrab((Tk_Window) dispPtr->eventualGrabWinPtr);
434 }
435
436 Tk_MakeWindowExist(tkwin);
437 if (!grabGlobal) {
438 Window dummy1, dummy2;
439 int dummy3, dummy4, dummy5, dummy6;
440 unsigned int state;
441
442 /*
443 * Local grab. However, if any mouse buttons are down, turn
444 * it into a global grab temporarily, until the last button
445 * goes up. This does two things: (a) it makes sure that we
446 * see the button-up event; and (b) it allows us to track mouse
447 * motion among all of the windows of this application.
448 */
449
450 dispPtr->grabFlags &= ~(GRAB_GLOBAL|GRAB_TEMP_GLOBAL);
451 XQueryPointer(dispPtr->display, winPtr->window, &dummy1,
452 &dummy2, &dummy3, &dummy4, &dummy5, &dummy6, &state);
453 if ((state & ALL_BUTTONS) != 0) {
454 dispPtr->grabFlags |= GRAB_TEMP_GLOBAL;
455 goto setGlobalGrab;
456 }
457 } else {
458 dispPtr->grabFlags |= GRAB_GLOBAL;
459 setGlobalGrab:
460
461 /*
462 * Tricky point: must ungrab before grabbing. This is needed
463 * in case there is a button auto-grab already in effect. If
464 * there is, and the mouse has moved to a different window, X
465 * won't generate enter and leave events to move the mouse if
466 * we grab without ungrabbing.
467 */
468
469 XUngrabPointer(dispPtr->display, CurrentTime);
470 serial = NextRequest(dispPtr->display);
471 /*
472 * Another tricky point: there are races with some window
473 * managers that can cause grabs to fail because the window
474 * manager hasn't released its grab quickly enough. To work
475 * around this problem, retry a few times after AlreadyGrabbed
476 * errors to give the grab release enough time to register with
477 * the server.
478 */
479
480 grabResult = 0; /* Needed only to prevent gcc
481 * compiler warnings. */
482 for (numTries = 0; numTries < 10; numTries++) {
483 grabResult = XGrabPointer(dispPtr->display, winPtr->window,
484 True, ButtonPressMask|ButtonReleaseMask|ButtonMotionMask
485 |PointerMotionMask, GrabModeAsync, GrabModeAsync, None,
486 winPtr->atts.cursor, CurrentTime);
487 if (grabResult != AlreadyGrabbed) {
488 break;
489 }
490 Tcl_Sleep(100);
491 }
492 if (grabResult != 0) {
493 grabError:
494 if (grabResult == GrabNotViewable) {
495 Tcl_SetResult(interp, "grab failed: window not viewable",
496 TCL_STATIC);
497 } else if (grabResult == AlreadyGrabbed) {
498 goto alreadyGrabbed;
499 } else if (grabResult == GrabFrozen) {
500 Tcl_SetResult(interp,
501 "grab failed: keyboard or pointer frozen", TCL_STATIC);
502 } else if (grabResult == GrabInvalidTime) {
503 Tcl_SetResult(interp, "grab failed: invalid time",
504 TCL_STATIC);
505 } else {
506 char msg[64 + TCL_INTEGER_SPACE];
507
508 sprintf(msg, "grab failed for unknown reason (code %d)",
509 grabResult);
510 Tcl_AppendResult(interp, msg, (char *) NULL);
511 }
512 return TCL_ERROR;
513 }
514 grabResult = XGrabKeyboard(dispPtr->display, Tk_WindowId(tkwin),
515 False, GrabModeAsync, GrabModeAsync, CurrentTime);
516 if (grabResult != 0) {
517 XUngrabPointer(dispPtr->display, CurrentTime);
518 goto grabError;
519 }
520
521 /*
522 * Eat up any grab-related events generated by the server for the
523 * grab. There are several reasons for doing this:
524 *
525 * 1. We have to synthesize the events for local grabs anyway, since
526 * the server doesn't participate in them.
527 * 2. The server doesn't always generate the right events for global
528 * grabs (e.g. it generates events even if the current window is
529 * in the grab tree, which we don't want).
530 * 3. We want all the grab-related events to be processed immediately
531 * (before other events that are already queued); events coming
532 * from the server will be in the wrong place, but events we
533 * synthesize here will go to the front of the queue.
534 */
535
536 EatGrabEvents(dispPtr, serial);
537 }
538
539 /*
540 * Synthesize leave events to move the pointer from its current window
541 * up to the lowest ancestor that it has in common with the grab window.
542 * However, only do this if the pointer is outside the grab window's
543 * subtree but inside the grab window's application.
544 */
545
546 if ((dispPtr->serverWinPtr != NULL)
547 && (dispPtr->serverWinPtr->mainPtr == winPtr->mainPtr)) {
548 for (winPtr2 = dispPtr->serverWinPtr; ; winPtr2 = winPtr2->parentPtr) {
549 if (winPtr2 == winPtr) {
550 break;
551 }
552 if (winPtr2 == NULL) {
553 MovePointer2(dispPtr->serverWinPtr, winPtr, NotifyGrab, 1, 0);
554 break;
555 }
556 }
557 }
558 QueueGrabWindowChange(dispPtr, winPtr);
559 return TCL_OK;
560 }
561
562 /*
563 *----------------------------------------------------------------------
564 *
565 * Tk_Ungrab --
566 *
567 * Releases a grab on the mouse pointer and keyboard, if there
568 * is one set on the specified window.
569 *
570 * Results:
571 * None.
572 *
573 * Side effects:
574 * Pointer and keyboard events will start being delivered to other
575 * windows again.
576 *
577 *----------------------------------------------------------------------
578 */
579
580 void
581 Tk_Ungrab(tkwin)
582 Tk_Window tkwin; /* Window whose grab should be
583 * released. */
584 {
585 TkDisplay *dispPtr;
586 TkWindow *grabWinPtr, *winPtr;
587 unsigned int serial;
588
589 grabWinPtr = (TkWindow *) tkwin;
590 dispPtr = grabWinPtr->dispPtr;
591 if (grabWinPtr != dispPtr->eventualGrabWinPtr) {
592 return;
593 }
594 ReleaseButtonGrab(dispPtr);
595 QueueGrabWindowChange(dispPtr, (TkWindow *) NULL);
596 if (dispPtr->grabFlags & (GRAB_GLOBAL|GRAB_TEMP_GLOBAL)) {
597 dispPtr->grabFlags &= ~(GRAB_GLOBAL|GRAB_TEMP_GLOBAL);
598 serial = NextRequest(dispPtr->display);
599 XUngrabPointer(dispPtr->display, CurrentTime);
600 XUngrabKeyboard(dispPtr->display, CurrentTime);
601 EatGrabEvents(dispPtr, serial);
602 }
603
604 /*
605 * Generate events to move the pointer back to the window where it
606 * really is. Some notes:
607 * 1. As with grabs, only do this if the "real" window is not a
608 * descendant of the grab window, since in this case the pointer
609 * is already where it's supposed to be.
610 * 2. If the "real" window is in some other application then don't
611 * generate any events at all, since everything's already been
612 * reported correctly.
613 * 3. Only generate enter events. Don't generate leave events,
614 * because we never told the lower-level windows that they
615 * had the pointer in the first place.
616 */
617
618 for (winPtr = dispPtr->serverWinPtr; ; winPtr = winPtr->parentPtr) {
619 if (winPtr == grabWinPtr) {
620 break;
621 }
622 if (winPtr == NULL) {
623 if ((dispPtr->serverWinPtr == NULL) ||
624 (dispPtr->serverWinPtr->mainPtr == grabWinPtr->mainPtr)) {
625 MovePointer2(grabWinPtr, dispPtr->serverWinPtr,
626 NotifyUngrab, 0, 1);
627 }
628 break;
629 }
630 }
631 }
632
633 /*
634 *----------------------------------------------------------------------
635 *
636 * ReleaseButtonGrab --
637 *
638 * This procedure is called to release a simulated button grab, if
639 * there is one in effect. A button grab is present whenever
640 * dispPtr->buttonWinPtr is non-NULL or when the GRAB_TEMP_GLOBAL
641 * flag is set.
642 *
643 * Results:
644 * None.
645 *
646 * Side effects:
647 * DispPtr->buttonWinPtr is reset to NULL, and enter and leave
648 * events are generated if necessary to move the pointer from
649 * the button grab window to its current window.
650 *
651 *----------------------------------------------------------------------
652 */
653
654 static void
655 ReleaseButtonGrab(dispPtr)
656 register TkDisplay *dispPtr; /* Display whose button grab is to be
657 * released. */
658 {
659 unsigned int serial;
660
661 if (dispPtr->buttonWinPtr != NULL) {
662 if (dispPtr->buttonWinPtr != dispPtr->serverWinPtr) {
663 MovePointer2(dispPtr->buttonWinPtr, dispPtr->serverWinPtr,
664 NotifyUngrab, 1, 1);
665 }
666 dispPtr->buttonWinPtr = NULL;
667 }
668 if (dispPtr->grabFlags & GRAB_TEMP_GLOBAL) {
669 dispPtr->grabFlags &= ~GRAB_TEMP_GLOBAL;
670 serial = NextRequest(dispPtr->display);
671 XUngrabPointer(dispPtr->display, CurrentTime);
672 XUngrabKeyboard(dispPtr->display, CurrentTime);
673 EatGrabEvents(dispPtr, serial);
674 }
675 }
676
677 /*
678 *----------------------------------------------------------------------
679 *
680 * TkPointerEvent --
681 *
682 * This procedure is called for each pointer-related event, before
683 * the event has been processed. It does various things to make
684 * grabs work correctly.
685 *
686 * Results:
687 * If the return value is 1 it means the event should be processed
688 * (event handlers should be invoked). If the return value is 0
689 * it means the event should be ignored in order to make grabs
690 * work correctly. In some cases this procedure modifies the event.
691 *
692 * Side effects:
693 * Grab state information may be updated. New events may also be
694 * pushed back onto the event queue to replace or augment the
695 * one passed in here.
696 *
697 *----------------------------------------------------------------------
698 */
699
700 int
701 TkPointerEvent(eventPtr, winPtr)
702 register XEvent *eventPtr; /* Pointer to the event. */
703 TkWindow *winPtr; /* Tk's information for window
704 * where event was reported. */
705 {
706 register TkWindow *winPtr2;
707 TkDisplay *dispPtr = winPtr->dispPtr;
708 unsigned int serial;
709 int outsideGrabTree = 0;
710 int ancestorOfGrab = 0;
711 int appGrabbed = 0; /* Non-zero means event is being
712 * reported to an application that is
713 * affected by the grab. */
714
715 /*
716 * Collect information about the grab (if any).
717 */
718
719 switch (TkGrabState(winPtr)) {
720 case TK_GRAB_IN_TREE:
721 appGrabbed = 1;
722 break;
723 case TK_GRAB_ANCESTOR:
724 appGrabbed = 1;
725 outsideGrabTree = 1;
726 ancestorOfGrab = 1;
727 break;
728 case TK_GRAB_EXCLUDED:
729 appGrabbed = 1;
730 outsideGrabTree = 1;
731 break;
732 }
733
734 if ((eventPtr->type == EnterNotify) || (eventPtr->type == LeaveNotify)) {
735 /*
736 * Keep track of what window the mouse is *really* over.
737 * Any events that we generate have a special send_event value,
738 * which is detected below and used to ignore the event for
739 * purposes of setting serverWinPtr.
740 */
741
742 if (eventPtr->xcrossing.send_event != GENERATED_EVENT_MAGIC) {
743 if ((eventPtr->type == LeaveNotify) &&
744 (winPtr->flags & TK_TOP_HIERARCHY)) {
745 dispPtr->serverWinPtr = NULL;
746 } else {
747 dispPtr->serverWinPtr = winPtr;
748 }
749 }
750
751 /*
752 * When a grab is active, X continues to report enter and leave
753 * events for windows outside the tree of the grab window:
754 * 1. Detect these events and ignore them except for
755 * windows above the grab window.
756 * 2. Allow Enter and Leave events to pass through the
757 * windows above the grab window, but never let them
758 * end up with the pointer *in* one of those windows.
759 */
760
761 if (dispPtr->grabWinPtr != NULL) {
762 if (outsideGrabTree && appGrabbed) {
763 if (!ancestorOfGrab) {
764 return 0;
765 }
766 switch (eventPtr->xcrossing.detail) {
767 case NotifyInferior:
768 return 0;
769 case NotifyAncestor:
770 eventPtr->xcrossing.detail = NotifyVirtual;
771 break;
772 case NotifyNonlinear:
773 eventPtr->xcrossing.detail = NotifyNonlinearVirtual;
774 break;
775 }
776 }
777
778 /*
779 * Make buttons have the same grab-like behavior inside a grab
780 * as they do outside a grab: do this by ignoring enter and
781 * leave events except for the window in which the button was
782 * pressed.
783 */
784
785 if ((dispPtr->buttonWinPtr != NULL)
786 && (winPtr != dispPtr->buttonWinPtr)) {
787 return 0;
788 }
789 }
790 return 1;
791 }
792
793 if (!appGrabbed) {
794 return 1;
795 }
796
797 if (eventPtr->type == MotionNotify) {
798 /*
799 * When grabs are active, X reports motion events relative to the
800 * window under the pointer. Instead, it should report the events
801 * relative to the window the button went down in, if there is a
802 * button down. Otherwise, if the pointer window is outside the
803 * subtree of the grab window, the events should be reported
804 * relative to the grab window. Otherwise, the event should be
805 * reported to the pointer window.
806 */
807
808 winPtr2 = winPtr;
809 if (dispPtr->buttonWinPtr != NULL) {
810 winPtr2 = dispPtr->buttonWinPtr;
811 } else if (outsideGrabTree || (dispPtr->serverWinPtr == NULL)) {
812 winPtr2 = dispPtr->grabWinPtr;
813 }
814 if (winPtr2 != winPtr) {
815 TkChangeEventWindow(eventPtr, winPtr2);
816 Tk_QueueWindowEvent(eventPtr, TCL_QUEUE_HEAD);
817 return 0;
818 }
819 return 1;
820 }
821
822 /*
823 * Process ButtonPress and ButtonRelease events:
824 * 1. Keep track of whether a button is down and what window it
825 * went down in.
826 * 2. If the first button goes down outside the grab tree, pretend
827 * it went down in the grab window. Note: it's important to
828 * redirect events to the grab window like this in order to make
829 * things like menus work, where button presses outside the
830 * grabbed menu need to be seen. An application can always
831 * ignore the events if they occur outside its window.
832 * 3. If a button press or release occurs outside the window where
833 * the first button was pressed, retarget the event so it's reported
834 * to the window where the first button was pressed.
835 * 4. If the last button is released in a window different than where
836 * the first button was pressed, generate Enter/Leave events to
837 * move the mouse from the button window to its current window.
838 * 5. If the grab is set at a time when a button is already down, or
839 * if the window where the button was pressed was deleted, then
840 * dispPtr->buttonWinPtr will stay NULL. Just forget about the
841 * auto-grab for the button press; events will go to whatever
842 * window contains the pointer. If this window isn't in the grab
843 * tree then redirect events to the grab window.
844 * 6. When a button is pressed during a local grab, the X server sets
845 * a grab of its own, since it doesn't even know about our local
846 * grab. This causes enter and leave events no longer to be
847 * generated in the same way as for global grabs. To eliminate this
848 * problem, set a temporary global grab when the first button goes
849 * down and release it when the last button comes up.
850 */
851
852 if ((eventPtr->type == ButtonPress) || (eventPtr->type == ButtonRelease)) {
853 winPtr2 = dispPtr->buttonWinPtr;
854 if (winPtr2 == NULL) {
855 if (outsideGrabTree) {
856 winPtr2 = dispPtr->grabWinPtr; /* Note 5. */
857 } else {
858 winPtr2 = winPtr; /* Note 5. */
859 }
860 }
861 if (eventPtr->type == ButtonPress) {
862 if ((eventPtr->xbutton.state & ALL_BUTTONS) == 0) {
863 if (outsideGrabTree) {
864 TkChangeEventWindow(eventPtr, dispPtr->grabWinPtr);
865 Tk_QueueWindowEvent(eventPtr, TCL_QUEUE_HEAD);
866 return 0; /* Note 2. */
867 }
868 if (!(dispPtr->grabFlags & GRAB_GLOBAL)) { /* Note 6. */
869 serial = NextRequest(dispPtr->display);
870 if (XGrabPointer(dispPtr->display,
871 dispPtr->grabWinPtr->window, True,
872 ButtonPressMask|ButtonReleaseMask|ButtonMotionMask,
873 GrabModeAsync, GrabModeAsync, None, dispPtr->eventualGrabWinPtr->atts.cursor,
874 CurrentTime) == 0) {
875 EatGrabEvents(dispPtr, serial);
876 if (XGrabKeyboard(dispPtr->display, winPtr->window,
877 False, GrabModeAsync, GrabModeAsync,
878 CurrentTime) == 0) {
879 dispPtr->grabFlags |= GRAB_TEMP_GLOBAL;
880 } else {
881 XUngrabPointer(dispPtr->display, CurrentTime);
882 }
883 }
884 }
885 dispPtr->buttonWinPtr = winPtr;
886 return 1;
887 }
888 } else {
889 if ((eventPtr->xbutton.state & ALL_BUTTONS)
890 == buttonStates[eventPtr->xbutton.button - Button1]) {
891 ReleaseButtonGrab(dispPtr); /* Note 4. */
892 }
893 }
894 if (winPtr2 != winPtr) {
895 TkChangeEventWindow(eventPtr, winPtr2);
896 Tk_QueueWindowEvent(eventPtr, TCL_QUEUE_HEAD);
897 return 0; /* Note 3. */
898 }
899 }
900
901 return 1;
902 }
903
904 /*
905 *----------------------------------------------------------------------
906 *
907 * TkChangeEventWindow --
908 *
909 * Given an event and a new window to which the event should be
910 * retargeted, modify fields of the event so that the event is
911 * properly retargeted to the new window.
912 *
913 * Results:
914 * The following fields of eventPtr are modified: window,
915 * subwindow, x, y, same_screen.
916 *
917 * Side effects:
918 * None.
919 *
920 *----------------------------------------------------------------------
921 */
922
923 void
924 TkChangeEventWindow(eventPtr, winPtr)
925 register XEvent *eventPtr; /* Event to retarget. Must have
926 * type ButtonPress, ButtonRelease, KeyPress,
927 * KeyRelease, MotionNotify, EnterNotify,
928 * or LeaveNotify. */
929 TkWindow *winPtr; /* New target window for event. */
930 {
931 int x, y, sameScreen, bd;
932 register TkWindow *childPtr;
933
934 eventPtr->xmotion.window = Tk_WindowId(winPtr);
935 if (eventPtr->xmotion.root ==
936 RootWindow(winPtr->display, winPtr->screenNum)) {
937 Tk_GetRootCoords((Tk_Window) winPtr, &x, &y);
938 eventPtr->xmotion.x = eventPtr->xmotion.x_root - x;
939 eventPtr->xmotion.y = eventPtr->xmotion.y_root - y;
940 eventPtr->xmotion.subwindow = None;
941 for (childPtr = winPtr->childList; childPtr != NULL;
942 childPtr = childPtr->nextPtr) {
943 if (childPtr->flags & TK_TOP_HIERARCHY) {
944 continue;
945 }
946 x = eventPtr->xmotion.x - childPtr->changes.x;
947 y = eventPtr->xmotion.y - childPtr->changes.y;
948 bd = childPtr->changes.border_width;
949 if ((x >= -bd) && (y >= -bd)
950 && (x < (childPtr->changes.width + bd))
951 && (y < (childPtr->changes.height + bd))) {
952 eventPtr->xmotion.subwindow = childPtr->window;
953 }
954 }
955 sameScreen = 1;
956 } else {
957 eventPtr->xmotion.x = 0;
958 eventPtr->xmotion.y = 0;
959 eventPtr->xmotion.subwindow = None;
960 sameScreen = 0;
961 }
962 if (eventPtr->type == MotionNotify) {
963 eventPtr->xmotion.same_screen = sameScreen;
964 } else {
965 eventPtr->xbutton.same_screen = sameScreen;
966 }
967 }
968
969 /*
970 *----------------------------------------------------------------------
971 *
972 * TkInOutEvents --
973 *
974 * This procedure synthesizes EnterNotify and LeaveNotify events
975 * to correctly transfer the pointer from one window to another.
976 * It can also be used to generate FocusIn and FocusOut events
977 * to move the input focus.
978 *
979 * Results:
980 * None.
981 *
982 * Side effects:
983 * Synthesized events may be pushed back onto the event queue.
984 * The event pointed to by eventPtr is modified.
985 *
986 *----------------------------------------------------------------------
987 */
988
989 void
990 TkInOutEvents(eventPtr, sourcePtr, destPtr, leaveType, enterType, position)
991 XEvent *eventPtr; /* A template X event. Must have all fields
992 * properly set except for type, window,
993 * subwindow, x, y, detail, and same_screen
994 * (Not all of these fields are valid for
995 * FocusIn/FocusOut events; x_root and y_root
996 * must be valid for Enter/Leave events, even
997 * though x and y needn't be valid). */
998 TkWindow *sourcePtr; /* Window that used to have the pointer or
999 * focus (NULL means it was not in a window
1000 * managed by this process). */
1001 TkWindow *destPtr; /* Window that is to end up with the pointer
1002 * or focus (NULL means it's not one managed
1003 * by this process). */
1004 int leaveType; /* Type of events to generate for windows
1005 * being left (LeaveNotify or FocusOut). 0
1006 * means don't generate leave events. */
1007 int enterType; /* Type of events to generate for windows
1008 * being entered (EnterNotify or FocusIn). 0
1009 * means don't generate enter events. */
1010 Tcl_QueuePosition position; /* Position at which events are added to
1011 * the system event queue. */
1012 {
1013 register TkWindow *winPtr;
1014 int upLevels, downLevels, i, j, focus;
1015
1016 /*
1017 * There are four possible cases to deal with:
1018 *
1019 * 1. SourcePtr and destPtr are the same. There's nothing to do in
1020 * this case.
1021 * 2. SourcePtr is an ancestor of destPtr in the same top-level
1022 * window. Must generate events down the window tree from source
1023 * to dest.
1024 * 3. DestPtr is an ancestor of sourcePtr in the same top-level
1025 * window. Must generate events up the window tree from sourcePtr
1026 * to destPtr.
1027 * 4. All other cases. Must first generate events up the window tree
1028 * from sourcePtr to its top-level, then down from destPtr's
1029 * top-level to destPtr. This form is called "non-linear."
1030 *
1031 * The call to FindCommonAncestor separates these four cases and decides
1032 * how many levels up and down events have to be generated for.
1033 */
1034
1035 if (sourcePtr == destPtr) {
1036 return;
1037 }
1038 if ((leaveType == FocusOut) || (enterType == FocusIn)) {
1039 focus = 1;
1040 } else {
1041 focus = 0;
1042 }
1043 FindCommonAncestor(sourcePtr, destPtr, &upLevels, &downLevels);
1044
1045 /*
1046 * Generate enter/leave events and add them to the grab event queue.
1047 */
1048
1049
1050 #define QUEUE(w, t, d) \
1051 if (w->window != None) { \
1052 eventPtr->type = t; \
1053 if (focus) { \
1054 eventPtr->xfocus.window = w->window; \
1055 eventPtr->xfocus.detail = d; \
1056 } else { \
1057 eventPtr->xcrossing.detail = d; \
1058 TkChangeEventWindow(eventPtr, w); \
1059 } \
1060 Tk_QueueWindowEvent(eventPtr, position); \
1061 }
1062
1063 if (downLevels == 0) {
1064
1065 /*
1066 * SourcePtr is an inferior of destPtr.
1067 */
1068
1069 if (leaveType != 0) {
1070 QUEUE(sourcePtr, leaveType, NotifyAncestor);
1071 for (winPtr = sourcePtr->parentPtr, i = upLevels-1; i > 0;
1072 winPtr = winPtr->parentPtr, i--) {
1073 QUEUE(winPtr, leaveType, NotifyVirtual);
1074 }
1075 }
1076 if ((enterType != 0) && (destPtr != NULL)) {
1077 QUEUE(destPtr, enterType, NotifyInferior);
1078 }
1079 } else if (upLevels == 0) {
1080
1081 /*
1082 * DestPtr is an inferior of sourcePtr.
1083 */
1084
1085 if ((leaveType != 0) && (sourcePtr != NULL)) {
1086 QUEUE(sourcePtr, leaveType, NotifyInferior);
1087 }
1088 if (enterType != 0) {
1089 for (i = downLevels-1; i > 0; i--) {
1090 for (winPtr = destPtr->parentPtr, j = 1; j < i;
1091 winPtr = winPtr->parentPtr, j++) {
1092 }
1093 QUEUE(winPtr, enterType, NotifyVirtual);
1094 }
1095 if (destPtr != NULL) {
1096 QUEUE(destPtr, enterType, NotifyAncestor);
1097 }
1098 }
1099 } else {
1100
1101 /*
1102 * Non-linear: neither window is an inferior of the other.
1103 */
1104
1105 if (leaveType != 0) {
1106 QUEUE(sourcePtr, leaveType, NotifyNonlinear);
1107 for (winPtr = sourcePtr->parentPtr, i = upLevels-1; i > 0;
1108 winPtr = winPtr->parentPtr, i--) {
1109 QUEUE(winPtr, leaveType, NotifyNonlinearVirtual);
1110 }
1111 }
1112 if (enterType != 0) {
1113 for (i = downLevels-1; i > 0; i--) {
1114 for (winPtr = destPtr->parentPtr, j = 1; j < i;
1115 winPtr = winPtr->parentPtr, j++) {
1116 }
1117 QUEUE(winPtr, enterType, NotifyNonlinearVirtual);
1118 }
1119 if (destPtr != NULL) {
1120 QUEUE(destPtr, enterType, NotifyNonlinear);
1121 }
1122 }
1123 }
1124 }
1125
1126 /*
1127 *----------------------------------------------------------------------
1128 *
1129 * MovePointer2 --
1130 *
1131 * This procedure synthesizes EnterNotify and LeaveNotify events
1132 * to correctly transfer the pointer from one window to another.
1133 * It is different from TkInOutEvents in that no template X event
1134 * needs to be supplied; this procedure generates the template
1135 * event and calls TkInOutEvents.
1136 *
1137 * Results:
1138 * None.
1139 *
1140 * Side effects:
1141 * Synthesized events may be pushed back onto the event queue.
1142 *
1143 *----------------------------------------------------------------------
1144 */
1145
1146 static void
1147 MovePointer2(sourcePtr, destPtr, mode, leaveEvents, enterEvents)
1148 TkWindow *sourcePtr; /* Window currently containing pointer (NULL
1149 * means it's not one managed by this
1150 * process). */
1151 TkWindow *destPtr; /* Window that is to end up containing the
1152 * pointer (NULL means it's not one managed
1153 * by this process). */
1154 int mode; /* Mode for enter/leave events, such as
1155 * NotifyNormal or NotifyUngrab. */
1156 int leaveEvents; /* Non-zero means generate leave events for the
1157 * windows being left. Zero means don't
1158 * generate leave events. */
1159 int enterEvents; /* Non-zero means generate enter events for the
1160 * windows being entered. Zero means don't
1161 * generate enter events. */
1162 {
1163 XEvent event;
1164 Window dummy1, dummy2;
1165 int dummy3, dummy4;
1166 TkWindow *winPtr;
1167
1168 winPtr = sourcePtr;
1169 if ((winPtr == NULL) || (winPtr->window == None)) {
1170 winPtr = destPtr;
1171 if ((winPtr == NULL) || (winPtr->window == None)) {
1172 return;
1173 }
1174 }
1175
1176 event.xcrossing.serial = LastKnownRequestProcessed(
1177 winPtr->display);
1178 event.xcrossing.send_event = GENERATED_EVENT_MAGIC;
1179 event.xcrossing.display = winPtr->display;
1180 event.xcrossing.root = RootWindow(winPtr->display,
1181 winPtr->screenNum);
1182 event.xcrossing.time = TkCurrentTime(winPtr->dispPtr, False);
1183 /* FALSE because we want a real time (even an old one) not CurrentTime */
1184 XQueryPointer(winPtr->display, winPtr->window, &dummy1, &dummy2,
1185 &event.xcrossing.x_root, &event.xcrossing.y_root,
1186 &dummy3, &dummy4, &event.xcrossing.state);
1187 event.xcrossing.mode = mode;
1188 event.xcrossing.focus = False;
1189 TkInOutEvents(&event, sourcePtr, destPtr, (leaveEvents) ? LeaveNotify : 0,
1190 (enterEvents) ? EnterNotify : 0, TCL_QUEUE_MARK);
1191 }
1192
1193 /*
1194 *----------------------------------------------------------------------
1195 *
1196 * TkGrabDeadWindow --
1197 *
1198 * This procedure is invoked whenever a window is deleted, so that
1199 * grab-related cleanup can be performed.
1200 *
1201 * Results:
1202 * None.
1203 *
1204 * Side effects:
1205 * Various cleanups happen, such as generating events to move the
1206 * pointer back to its "natural" window as if an ungrab had been
1207 * done. See the code.
1208 *
1209 *----------------------------------------------------------------------
1210 */
1211
1212 void
1213 TkGrabDeadWindow(winPtr)
1214 register TkWindow *winPtr; /* Window that is in the process
1215 * of being deleted. */
1216 {
1217 TkDisplay *dispPtr = winPtr->dispPtr;
1218
1219 if (dispPtr->eventualGrabWinPtr == winPtr) {
1220 /*
1221 * Grab window was deleted. Release the grab.
1222 */
1223
1224 Tk_Ungrab((Tk_Window) dispPtr->eventualGrabWinPtr);
1225 } else if (dispPtr->buttonWinPtr == winPtr) {
1226 ReleaseButtonGrab(dispPtr);
1227 }
1228 if (dispPtr->serverWinPtr == winPtr) {
1229 if (winPtr->flags & TK_TOP_HIERARCHY) {
1230 dispPtr->serverWinPtr = NULL;
1231 } else {
1232 dispPtr->serverWinPtr = winPtr->parentPtr;
1233 }
1234 }
1235 if (dispPtr->grabWinPtr == winPtr) {
1236 dispPtr->grabWinPtr = NULL;
1237 }
1238 }
1239
1240 /*
1241 *----------------------------------------------------------------------
1242 *
1243 * EatGrabEvents --
1244 *
1245 * This procedure is called to eliminate any Enter, Leave,
1246 * FocusIn, or FocusOut events in the event queue for a
1247 * display that have mode NotifyGrab or NotifyUngrab and
1248 * have a serial number no less than a given value and are not
1249 * generated by the grab module.
1250 *
1251 * Results:
1252 * None.
1253 *
1254 * Side effects:
1255 * DispPtr's display gets sync-ed, and some of the events get
1256 * removed from the Tk event queue.
1257 *
1258 *----------------------------------------------------------------------
1259 */
1260
1261 static void
1262 EatGrabEvents(dispPtr, serial)
1263 TkDisplay *dispPtr; /* Display from which to consume events. */
1264 unsigned int serial; /* Only discard events that have a serial
1265 * number at least this great. */
1266 {
1267 Tk_RestrictProc *oldProc;
1268 GrabInfo info;
1269 ClientData oldArg, dummy;
1270
1271 info.display = dispPtr->display;
1272 info.serial = serial;
1273 TkpSync(info.display);
1274 oldProc = Tk_RestrictEvents(GrabRestrictProc, (ClientData)&info, &oldArg);
1275 while (Tcl_ServiceEvent(TCL_WINDOW_EVENTS)) {
1276 }
1277 Tk_RestrictEvents(oldProc, oldArg, &dummy);
1278 }
1279
1280 /*
1281 *----------------------------------------------------------------------
1282 *
1283 * GrabRestrictProc --
1284 *
1285 * A Tk_RestrictProc used by EatGrabEvents to eliminate any
1286 * Enter, Leave, FocusIn, or FocusOut events in the event queue
1287 * for a display that has mode NotifyGrab or NotifyUngrab and
1288 * have a serial number no less than a given value.
1289 *
1290 * Results:
1291 * Returns either TK_DISCARD_EVENT or TK_DEFER_EVENT.
1292 *
1293 * Side effects:
1294 * None.
1295 *
1296 *----------------------------------------------------------------------
1297 */
1298
1299 static Tk_RestrictAction
1300 GrabRestrictProc(arg, eventPtr)
1301 ClientData arg;
1302 XEvent *eventPtr;
1303 {
1304 GrabInfo *info = (GrabInfo *) arg;
1305 int mode, diff;
1306
1307 /*
1308 * The diff caculation is trickier than it may seem. Don't forget
1309 * that serial numbers can wrap around, so can't compare the two
1310 * serial numbers directly.
1311 */
1312
1313 diff = eventPtr->xany.serial - info->serial;
1314 if ((eventPtr->type == EnterNotify)
1315 || (eventPtr->type == LeaveNotify)) {
1316 mode = eventPtr->xcrossing.mode;
1317 } else if ((eventPtr->type == FocusIn)
1318 || (eventPtr->type == FocusOut)) {
1319 mode = eventPtr->xfocus.mode;
1320 } else {
1321 mode = NotifyNormal;
1322 }
1323 if ((info->display != eventPtr->xany.display) || (mode == NotifyNormal)
1324 || (diff < 0)) {
1325 return TK_DEFER_EVENT;
1326 } else {
1327 return TK_DISCARD_EVENT;
1328 }
1329 }
1330
1331 /*
1332 *----------------------------------------------------------------------
1333 *
1334 * QueueGrabWindowChange --
1335 *
1336 * This procedure queues a special event in the Tcl event queue,
1337 * which will cause the "grabWinPtr" field for the display to get
1338 * modified when the event is processed. This is needed to make
1339 * sure that the grab window changes at the proper time relative
1340 * to grab-related enter and leave events that are also in the
1341 * queue. In particular, this approach works even when multiple
1342 * grabs and ungrabs happen back-to-back.
1343 *
1344 * Results:
1345 * None.
1346 *
1347 * Side effects:
1348 * DispPtr->grabWinPtr will be modified later (by GrabWinEventProc)
1349 * when the event is removed from the grab event queue.
1350 *
1351 *----------------------------------------------------------------------
1352 */
1353
1354 static void
1355 QueueGrabWindowChange(dispPtr, grabWinPtr)
1356 TkDisplay *dispPtr; /* Display on which to change the grab
1357 * window. */
1358 TkWindow *grabWinPtr; /* Window that is to become the new grab
1359 * window (may be NULL). */
1360 {
1361 NewGrabWinEvent *grabEvPtr;
1362
1363 grabEvPtr = (NewGrabWinEvent *) ckalloc(sizeof(NewGrabWinEvent));
1364 grabEvPtr->dispPtr = dispPtr;
1365 if (grabWinPtr == NULL) {
1366 grabEvPtr->grabWindow = None;
1367 } else {
1368 grabEvPtr->grabWindow = grabWinPtr->window;
1369 }
1370 Tcl_QueueProcEvent(GrabWinEventProc, &grabEvPtr->header, TCL_QUEUE_MARK);
1371 dispPtr->eventualGrabWinPtr = grabWinPtr;
1372 }
1373
1374 /*
1375 *----------------------------------------------------------------------
1376 *
1377 * GrabWinEventProc --
1378 *
1379 * This procedure is invoked as a handler for Tcl_Events of type
1380 * NewGrabWinEvent. It updates the current grab window field in
1381 * a display.
1382 *
1383 * Results:
1384 * Returns 1 if the event was processed, 0 if it should be deferred
1385 * for processing later.
1386 *
1387 * Side effects:
1388 * The grabWinPtr field is modified in the display associated with
1389 * the event.
1390 *
1391 *----------------------------------------------------------------------
1392 */
1393
1394 static int
1395 GrabWinEventProc(evPtr, flags)
1396 Tcl_Event *evPtr; /* Event of type NewGrabWinEvent. */
1397 int flags; /* Flags argument to Tk_DoOneEvent: indicates
1398 * what kinds of events are being processed
1399 * right now. */
1400 {
1401 NewGrabWinEvent *grabEvPtr = (NewGrabWinEvent *) evPtr;
1402
1403 grabEvPtr->dispPtr->grabWinPtr = (TkWindow *) Tk_IdToWindow(
1404 grabEvPtr->dispPtr->display, grabEvPtr->grabWindow);
1405 return 1;
1406 }
1407
1408 /*
1409 *----------------------------------------------------------------------
1410 *
1411 * FindCommonAncestor --
1412 *
1413 * Given two windows, this procedure finds their least common
1414 * ancestor and also computes how many levels up this ancestor
1415 * is from each of the original windows.
1416 *
1417 * Results:
1418 * If the windows are in different applications or top-level
1419 * windows, then NULL is returned and *countPtr1 and *countPtr2
1420 * are set to the depths of the two windows in their respective
1421 * top-level windows (1 means the window is a top-level, 2 means
1422 * its parent is a top-level, and so on). Otherwise, the return
1423 * value is a pointer to the common ancestor and the counts are
1424 * set to the distance of winPtr1 and winPtr2 from this ancestor
1425 * (1 means they're children, 2 means grand-children, etc.).
1426 *
1427 * Side effects:
1428 * None.
1429 *
1430 *----------------------------------------------------------------------
1431 */
1432
1433 static TkWindow *
1434 FindCommonAncestor(winPtr1, winPtr2, countPtr1, countPtr2)
1435 TkWindow *winPtr1; /* First window. May be NULL. */
1436 TkWindow *winPtr2; /* Second window. May be NULL. */
1437 int *countPtr1; /* Store nesting level of winPtr1 within
1438 * common ancestor here. */
1439 int *countPtr2; /* Store nesting level of winPtr2 within
1440 * common ancestor here. */
1441 {
1442 register TkWindow *winPtr;
1443 TkWindow *ancestorPtr;
1444 int count1, count2, i;
1445
1446 /*
1447 * Mark winPtr1 and all of its ancestors with a special flag bit.
1448 */
1449
1450 if (winPtr1 != NULL) {
1451 for (winPtr = winPtr1; winPtr != NULL; winPtr = winPtr->parentPtr) {
1452 winPtr->flags |= TK_GRAB_FLAG;
1453 if (winPtr->flags & TK_TOP_HIERARCHY) {
1454 break;
1455 }
1456 }
1457 }
1458
1459 /*
1460 * Search upwards from winPtr2 until an ancestor of winPtr1 is
1461 * found or a top-level window is reached.
1462 */
1463
1464 winPtr = winPtr2;
1465 count2 = 0;
1466 ancestorPtr = NULL;
1467 if (winPtr2 != NULL) {
1468 for (; winPtr != NULL; count2++, winPtr = winPtr->parentPtr) {
1469 if (winPtr->flags & TK_GRAB_FLAG) {
1470 ancestorPtr = winPtr;
1471 break;
1472 }
1473 if (winPtr->flags & TK_TOP_HIERARCHY) {
1474 count2++;
1475 break;
1476 }
1477 }
1478 }
1479
1480 /*
1481 * Search upwards from winPtr1 again, clearing the flag bits and
1482 * remembering how many levels up we had to go.
1483 */
1484
1485 if (winPtr1 == NULL) {
1486 count1 = 0;
1487 } else {
1488 count1 = -1;
1489 for (i = 0, winPtr = winPtr1; winPtr != NULL;
1490 i++, winPtr = winPtr->parentPtr) {
1491 winPtr->flags &= ~TK_GRAB_FLAG;
1492 if (winPtr == ancestorPtr) {
1493 count1 = i;
1494 }
1495 if (winPtr->flags & TK_TOP_HIERARCHY) {
1496 if (count1 == -1) {
1497 count1 = i+1;
1498 }
1499 break;
1500 }
1501 }
1502 }
1503
1504 *countPtr1 = count1;
1505 *countPtr2 = count2;
1506 return ancestorPtr;
1507 }
1508
1509 /*
1510 *----------------------------------------------------------------------
1511 *
1512 * TkPositionInTree --
1513 *
1514 * Compute where the given window is relative to a particular
1515 * subtree of the window hierarchy.
1516 *
1517 * Results:
1518 *
1519 * Returns TK_GRAB_IN_TREE if the window is contained in the
1520 * subtree. Returns TK_GRAB_ANCESTOR if the window is an
1521 * ancestor of the subtree, in the same toplevel. Otherwise
1522 * it returns TK_GRAB_EXCLUDED.
1523 *
1524 * Side effects:
1525 * None.
1526 *
1527 *----------------------------------------------------------------------
1528 */
1529
1530 int
1531 TkPositionInTree(winPtr, treePtr)
1532 TkWindow *winPtr; /* Window to be checked. */
1533 TkWindow *treePtr; /* Root of tree to compare against. */
1534 {
1535 TkWindow *winPtr2;
1536
1537 for (winPtr2 = winPtr; winPtr2 != treePtr;
1538 winPtr2 = winPtr2->parentPtr) {
1539 if (winPtr2 == NULL) {
1540 for (winPtr2 = treePtr; winPtr2 != NULL;
1541 winPtr2 = winPtr2->parentPtr) {
1542 if (winPtr2 == winPtr) {
1543 return TK_GRAB_ANCESTOR;
1544 }
1545 if (winPtr2->flags & TK_TOP_HIERARCHY) {
1546 break;
1547 }
1548 }
1549 return TK_GRAB_EXCLUDED;
1550 }
1551 }
1552 return TK_GRAB_IN_TREE;
1553 }
1554
1555 /*
1556 *----------------------------------------------------------------------
1557 *
1558 * TkGrabState --
1559 *
1560 * Given a window, this procedure returns a value that indicates
1561 * the grab state of the application relative to the window.
1562 *
1563 * Results:
1564 * The return value is one of three things:
1565 * TK_GRAB_NONE - no grab is in effect.
1566 * TK_GRAB_IN_TREE - there is a grab in effect, and winPtr
1567 * is in the grabbed subtree.
1568 * TK_GRAB_ANCESTOR - there is a grab in effect; winPtr is
1569 * an ancestor of the grabbed window, in
1570 * the same toplevel.
1571 * TK_GRAB_EXCLUDED - there is a grab in effect; winPtr is
1572 * outside the tree of the grab and is not
1573 * an ancestor of the grabbed window in the
1574 * same toplevel.
1575 *
1576 * Side effects:
1577 * None.
1578 *
1579 *----------------------------------------------------------------------
1580 */
1581
1582 int
1583 TkGrabState(winPtr)
1584 TkWindow *winPtr; /* Window for which grab information is
1585 * needed. */
1586 {
1587 TkWindow *grabWinPtr = winPtr->dispPtr->grabWinPtr;
1588
1589 if (grabWinPtr == NULL) {
1590 return TK_GRAB_NONE;
1591 }
1592 if ((winPtr->mainPtr != grabWinPtr->mainPtr)
1593 && !(winPtr->dispPtr->grabFlags & GRAB_GLOBAL)) {
1594 return TK_GRAB_NONE;
1595 }
1596
1597 return TkPositionInTree(winPtr, grabWinPtr);
1598 }
1599