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