Debian Codegraph @

pTk/mTk/generic/tkPointer.c

raw symbols SCIP
1 /*
2 * tkPointer.c --
3 *
4 * This file contains functions for emulating the X server
5 * pointer and grab state machine. This file is used by the
6 * Mac and Windows platforms to generate appropriate enter/leave
7 * events, and to update the global grab window information.
8 *
9 * Copyright (c) 1996 by Sun Microsystems, Inc.
10 *
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 *
14 * RCS: @(#) $Id: tkPointer.c,v 1.7 2002/08/31 06:12:25 das Exp $
15 */
16 #ifdef WIN32
17
18 #include "tkInt.h"
19
20 #ifdef __WIN32__
21 #include "tkWinInt.h"
22 #endif
23
24 #if defined(MAC_TCL)
25 #include "tkMacInt.h"
26 #define Cursor XCursor
27 #endif
28
29 #if defined(MAC_OSX_TK)
30 #include "tkMacOSXInt.h"
31 #define Cursor XCursor
32 #endif
33
34 /*
35 * Mask that selects any of the state bits corresponding to buttons,
36 * plus masks that select individual buttons' bits:
37 */
38
39 #define ALL_BUTTONS \
40 (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)
41 static unsigned int buttonMasks[] = {
42 Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask
43 };
44 #define ButtonMask(b) (buttonMasks[(b)-Button1])
45
46 typedef struct ThreadSpecificData {
47 TkWindow *grabWinPtr; /* Window that defines the top of the
48 * grab tree in a global grab. */
49 int lastState; /* Last known state flags. */
50 XPoint lastPos; /* Last reported mouse position. */
51 TkWindow *lastWinPtr; /* Last reported mouse window. */
52 TkWindow *restrictWinPtr; /* Window to which all mouse events
53 * will be reported. */
54 TkWindow *cursorWinPtr; /* Window that is currently
55 * controlling the global cursor. */
56 } ThreadSpecificData;
57 static Tcl_ThreadDataKey dataKey;
58
59 /*
60 * Forward declarations of procedures used in this file.
61 */
62
63 static int GenerateEnterLeave _ANSI_ARGS_((TkWindow *winPtr,
64 int x, int y, int state));
65 static void InitializeEvent _ANSI_ARGS_((XEvent* eventPtr,
66 TkWindow *winPtr, int type, int x, int y,
67 int state, int detail));
68 static void UpdateCursor _ANSI_ARGS_((TkWindow *winPtr));
69
70 /*
71 *----------------------------------------------------------------------
72 *
73 * InitializeEvent --
74 *
75 * Initializes the common fields for several X events.
76 *
77 * Results:
78 * None.
79 *
80 * Side effects:
81 * Fills in the specified event structure.
82 *
83 *----------------------------------------------------------------------
84 */
85
86 static void
87 InitializeEvent(eventPtr, winPtr, type, x, y, state, detail)
88 XEvent* eventPtr; /* Event structure to initialize. */
89 TkWindow *winPtr; /* Window to make event relative to. */
90 int type; /* Message type. */
91 int x, y; /* Root coords of event. */
92 int state; /* State flags. */
93 int detail; /* Detail value. */
94 {
95 eventPtr->type = type;
96 eventPtr->xany.serial = LastKnownRequestProcessed(winPtr->display);
97 eventPtr->xany.send_event = False;
98 eventPtr->xany.display = winPtr->display;
99
100 eventPtr->xcrossing.root = RootWindow(winPtr->display, winPtr->screenNum);
101 eventPtr->xcrossing.time = TkpGetMS();
102 eventPtr->xcrossing.x_root = x;
103 eventPtr->xcrossing.y_root = y;
104
105 switch (type) {
106 case EnterNotify:
107 case LeaveNotify:
108 eventPtr->xcrossing.mode = NotifyNormal;
109 eventPtr->xcrossing.state = state;
110 eventPtr->xcrossing.detail = detail;
111 eventPtr->xcrossing.focus = False;
112 break;
113 case MotionNotify:
114 eventPtr->xmotion.state = state;
115 eventPtr->xmotion.is_hint = detail;
116 break;
117 case ButtonPress:
118 case ButtonRelease:
119 eventPtr->xbutton.state = state;
120 eventPtr->xbutton.button = detail;
121 break;
122 }
123 TkChangeEventWindow(eventPtr, winPtr);
124 }
125
126 /*
127 *----------------------------------------------------------------------
128 *
129 * GenerateEnterLeave --
130 *
131 * Update the current mouse window and position, and generate
132 * any enter/leave events that are needed.
133 *
134 * Results:
135 * Returns 1 if enter/leave events were generated.
136 *
137 * Side effects:
138 * May insert events into the Tk event queue.
139 *
140 *----------------------------------------------------------------------
141 */
142
143 static int
144 GenerateEnterLeave(winPtr, x, y, state)
145 TkWindow *winPtr; /* Current Tk window (or NULL). */
146 int x,y; /* Current mouse position in root coords. */
147 int state; /* State flags. */
148 {
149 int crossed = 0; /* 1 if mouse crossed a window boundary */
150 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
151 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
152 TkWindow *restrictWinPtr = tsdPtr->restrictWinPtr;
153 TkWindow *lastWinPtr = tsdPtr->lastWinPtr;
154
155 if (winPtr != tsdPtr->lastWinPtr) {
156 if (restrictWinPtr) {
157 int newPos, oldPos;
158
159 newPos = TkPositionInTree(winPtr, restrictWinPtr);
160 oldPos = TkPositionInTree(lastWinPtr, restrictWinPtr);
161
162 /*
163 * Check if the mouse crossed into or out of the restrict
164 * window. If so, we need to generate an Enter or Leave event.
165 */
166
167 if ((newPos != oldPos) && ((newPos == TK_GRAB_IN_TREE)
168 || (oldPos == TK_GRAB_IN_TREE))) {
169 XEvent event;
170 int type, detail;
171
172 if (newPos == TK_GRAB_IN_TREE) {
173 type = EnterNotify;
174 } else {
175 type = LeaveNotify;
176 }
177 if ((oldPos == TK_GRAB_ANCESTOR)
178 || (newPos == TK_GRAB_ANCESTOR)) {
179 detail = NotifyAncestor;
180 } else {
181 detail = NotifyVirtual;
182 }
183 InitializeEvent(&event, restrictWinPtr, type, x, y,
184 state, detail);
185 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
186 }
187
188 } else {
189 TkWindow *targetPtr;
190
191 if ((lastWinPtr == NULL)
192 || (lastWinPtr->window == None)) {
193 targetPtr = winPtr;
194 } else {
195 targetPtr = lastWinPtr;
196 }
197
198 if (targetPtr && (targetPtr->window != None)) {
199 XEvent event;
200
201 /*
202 * Generate appropriate Enter/Leave events.
203 */
204
205 InitializeEvent(&event, targetPtr, LeaveNotify, x, y, state,
206 NotifyNormal);
207
208 TkInOutEvents(&event, lastWinPtr, winPtr, LeaveNotify,
209 EnterNotify, TCL_QUEUE_TAIL);
210 crossed = 1;
211 }
212 }
213 tsdPtr->lastWinPtr = winPtr;
214 }
215
216 return crossed;
217 }
218
219 /*
220 *----------------------------------------------------------------------
221 *
222 * Tk_UpdatePointer --
223 *
224 * This function updates the pointer state machine given an
225 * the current window, position and modifier state.
226 *
227 * Results:
228 * None.
229 *
230 * Side effects:
231 * May queue new events and update the grab state.
232 *
233 *----------------------------------------------------------------------
234 */
235
236 void
237 Tk_UpdatePointer(tkwin, x, y, state)
238 Tk_Window tkwin; /* Window to which pointer event
239 * is reported. May be NULL. */
240 int x, y; /* Pointer location in root coords. */
241 int state; /* Modifier state mask. */
242 {
243 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
244 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
245 TkWindow *winPtr = (TkWindow *)tkwin;
246 TkWindow *targetWinPtr;
247 XPoint pos;
248 XEvent event;
249 int changes = (state ^ tsdPtr->lastState) & ALL_BUTTONS;
250 int type, b, mask;
251
252 pos.x = x;
253 pos.y = y;
254
255 /*
256 * Use the current keyboard state, but the old mouse button
257 * state since we haven't generated the button events yet.
258 */
259
260 tsdPtr->lastState = (state & ~ALL_BUTTONS) | (tsdPtr->lastState
261 & ALL_BUTTONS);
262
263 /*
264 * Generate Enter/Leave events. If the pointer has crossed window
265 * boundaries, update the current mouse position so we don't generate
266 * redundant motion events.
267 */
268
269 if (GenerateEnterLeave(winPtr, x, y, tsdPtr->lastState)) {
270 tsdPtr->lastPos = pos;
271 }
272
273 /*
274 * Generate ButtonPress/ButtonRelease events based on the differences
275 * between the current button state and the last known button state.
276 */
277
278 for (b = Button1; b <= Button3; b++) {
279 mask = ButtonMask(b);
280 if (changes & mask) {
281 if (state & mask) {
282 type = ButtonPress;
283
284 /*
285 * ButtonPress - Set restrict window if we aren't grabbed, or
286 * if this is the first button down.
287 */
288
289 if (!tsdPtr->restrictWinPtr) {
290 if (!tsdPtr->grabWinPtr) {
291
292 /*
293 * Mouse is not grabbed, so set a button grab.
294 */
295
296 tsdPtr->restrictWinPtr = winPtr;
297 TkpSetCapture(tsdPtr->restrictWinPtr);
298
299 } else if ((tsdPtr->lastState & ALL_BUTTONS) == 0) {
300
301 /*
302 * Mouse is in a non-button grab, so ensure
303 * the button grab is inside the grab tree.
304 */
305
306 if (TkPositionInTree(winPtr, tsdPtr->grabWinPtr)
307 == TK_GRAB_IN_TREE) {
308 tsdPtr->restrictWinPtr = winPtr;
309 } else {
310 tsdPtr->restrictWinPtr = tsdPtr->grabWinPtr;
311 }
312 TkpSetCapture(tsdPtr->restrictWinPtr);
313 }
314 }
315
316 } else {
317 type = ButtonRelease;
318
319 /*
320 * ButtonRelease - Release the mouse capture and clear the
321 * restrict window when the last button is released and we
322 * aren't in a global grab.
323 */
324
325 if ((tsdPtr->lastState & ALL_BUTTONS) == mask) {
326 if (!tsdPtr->grabWinPtr) {
327 TkpSetCapture(NULL);
328 }
329 }
330
331 /*
332 * If we are releasing a restrict window, then we need
333 * to send the button event followed by mouse motion from
334 * the restrict window to the current mouse position.
335 */
336
337 if (tsdPtr->restrictWinPtr) {
338 InitializeEvent(&event, tsdPtr->restrictWinPtr, type, x, y,
339 tsdPtr->lastState, b);
340 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
341 tsdPtr->lastState &= ~mask;
342 tsdPtr->lastWinPtr = tsdPtr->restrictWinPtr;
343 tsdPtr->restrictWinPtr = NULL;
344
345 GenerateEnterLeave(winPtr, x, y, tsdPtr->lastState);
346 tsdPtr->lastPos = pos;
347 continue;
348 }
349 }
350
351 /*
352 * If a restrict window is set, make sure the pointer event
353 * is reported relative to that window. Otherwise, if a
354 * global grab is in effect then events outside of windows
355 * managed by Tk should be reported to the grab window.
356 */
357
358 if (tsdPtr->restrictWinPtr) {
359 targetWinPtr = tsdPtr->restrictWinPtr;
360 } else if (tsdPtr->grabWinPtr && !winPtr) {
361 targetWinPtr = tsdPtr->grabWinPtr;
362 } else {
363 targetWinPtr = winPtr;
364 }
365
366 /*
367 * If we still have a target window, send the event.
368 */
369
370 if (winPtr != NULL) {
371 InitializeEvent(&event, targetWinPtr, type, x, y,
372 tsdPtr->lastState, b);
373 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
374 }
375
376 /*
377 * Update the state for the next iteration.
378 */
379
380 tsdPtr->lastState = (type == ButtonPress)
381 ? (tsdPtr->lastState | mask) : (tsdPtr->lastState & ~mask);
382 tsdPtr->lastPos = pos;
383 }
384 }
385
386 /*
387 * Make sure the cursor window is up to date.
388 */
389
390 if (tsdPtr->restrictWinPtr) {
391 targetWinPtr = tsdPtr->restrictWinPtr;
392 } else if (tsdPtr->grabWinPtr) {
393 targetWinPtr = (TkPositionInTree(winPtr, tsdPtr->grabWinPtr)
394 == TK_GRAB_IN_TREE) ? winPtr : tsdPtr->grabWinPtr;
395 } else {
396 targetWinPtr = winPtr;
397 }
398 UpdateCursor(targetWinPtr);
399
400 /*
401 * If no other events caused the position to be updated,
402 * generate a motion event.
403 */
404
405 if (tsdPtr->lastPos.x != pos.x || tsdPtr->lastPos.y != pos.y) {
406 if (tsdPtr->restrictWinPtr) {
407 targetWinPtr = tsdPtr->restrictWinPtr;
408 } else if (tsdPtr->grabWinPtr && !winPtr) {
409 targetWinPtr = tsdPtr->grabWinPtr;
410 }
411
412 if (targetWinPtr != NULL) {
413 InitializeEvent(&event, targetWinPtr, MotionNotify, x, y,
414 tsdPtr->lastState, NotifyNormal);
415 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
416 }
417 tsdPtr->lastPos = pos;
418 }
419 }
420
421 /*
422 *----------------------------------------------------------------------
423 *
424 * XGrabPointer --
425 *
426 * Capture the mouse so event are reported outside of toplevels.
427 * Note that this is a very limited implementation that only
428 * supports GrabModeAsync and owner_events True.
429 *
430 * Results:
431 * Always returns GrabSuccess.
432 *
433 * Side effects:
434 * Turns on mouse capture, sets the global grab pointer, and
435 * clears any window restrictions.
436 *
437 *----------------------------------------------------------------------
438 */
439
440 int
441 XGrabPointer(display, grab_window, owner_events, event_mask, pointer_mode,
442 keyboard_mode, confine_to, cursor, time)
443 Display* display;
444 Window grab_window;
445 Bool owner_events;
446 unsigned int event_mask;
447 int pointer_mode;
448 int keyboard_mode;
449 Window confine_to;
450 Cursor cursor;
451 Time time;
452 {
453 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
454 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
455
456 display->request++;
457 tsdPtr->grabWinPtr = (TkWindow *) Tk_IdToWindow(display, grab_window);
458 tsdPtr->restrictWinPtr = NULL;
459 TkpSetCapture(tsdPtr->grabWinPtr);
460 if (TkPositionInTree(tsdPtr->lastWinPtr, tsdPtr->grabWinPtr)
461 != TK_GRAB_IN_TREE) {
462 UpdateCursor(tsdPtr->grabWinPtr);
463 }
464 return GrabSuccess;
465 }
466
467 /*
468 *----------------------------------------------------------------------
469 *
470 * XUngrabPointer --
471 *
472 * Release the current grab.
473 *
474 * Results:
475 * None.
476 *
477 * Side effects:
478 * Releases the mouse capture.
479 *
480 *----------------------------------------------------------------------
481 */
482
483 void
484 XUngrabPointer(display, time)
485 Display* display;
486 Time time;
487 {
488 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
489 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
490
491 display->request++;
492 tsdPtr->grabWinPtr = NULL;
493 tsdPtr->restrictWinPtr = NULL;
494 TkpSetCapture(NULL);
495 UpdateCursor(tsdPtr->lastWinPtr);
496 }
497
498 /*
499 *----------------------------------------------------------------------
500 *
501 * TkPointerDeadWindow --
502 *
503 * Clean up pointer module state when a window is destroyed.
504 *
505 * Results:
506 * None.
507 *
508 * Side effects:
509 * May release the current capture window.
510 *
511 *----------------------------------------------------------------------
512 */
513
514 void
515 TkPointerDeadWindow(winPtr)
516 TkWindow *winPtr;
517 {
518 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
519 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
520
521 if (winPtr == tsdPtr->lastWinPtr) {
522 tsdPtr->lastWinPtr = NULL;
523 }
524 if (winPtr == tsdPtr->grabWinPtr) {
525 tsdPtr->grabWinPtr = NULL;
526 }
527 if (winPtr == tsdPtr->restrictWinPtr) {
528 tsdPtr->restrictWinPtr = NULL;
529 }
530 if (!(tsdPtr->restrictWinPtr || tsdPtr->grabWinPtr)) {
531 TkpSetCapture(NULL);
532 }
533 }
534
535 /*
536 *----------------------------------------------------------------------
537 *
538 * UpdateCursor --
539 *
540 * Set the windows global cursor to the cursor associated with
541 * the given Tk window.
542 *
543 * Results:
544 * None.
545 *
546 * Side effects:
547 * Changes the mouse cursor.
548 *
549 *----------------------------------------------------------------------
550 */
551
552 static void
553 UpdateCursor(winPtr)
554 TkWindow *winPtr;
555 {
556 Cursor cursor = None;
557 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
558 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
559
560 /*
561 * A window inherits its cursor from its parent if it doesn't
562 * have one of its own. Top level windows inherit the default
563 * cursor.
564 */
565
566 tsdPtr->cursorWinPtr = winPtr;
567 while (winPtr != NULL) {
568 if (winPtr->atts.cursor != None) {
569 cursor = winPtr->atts.cursor;
570 break;
571 } else if (winPtr->flags & TK_TOP_HIERARCHY) {
572 break;
573 }
574 winPtr = winPtr->parentPtr;
575 }
576 TkpSetCursor((TkpCursor) cursor);
577 }
578
579 /*
580 *----------------------------------------------------------------------
581 *
582 * XDefineCursor --
583 *
584 * This function is called to update the cursor on a window.
585 * Since the mouse might be in the specified window, we need to
586 * check the specified window against the current mouse position
587 * and grab state.
588 *
589 * Results:
590 * None.
591 *
592 * Side effects:
593 * May update the cursor.
594 *
595 *----------------------------------------------------------------------
596 */
597
598 void
599 XDefineCursor(display, w, cursor)
600 Display* display;
601 Window w;
602 Cursor cursor;
603 {
604 TkWindow *winPtr = (TkWindow *)Tk_IdToWindow(display, w);
605 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
606 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
607
608 if (tsdPtr->cursorWinPtr == winPtr) {
609 UpdateCursor(winPtr);
610 }
611 display->request++;
612 }
613
614 /*
615 *----------------------------------------------------------------------
616 *
617 * TkGenerateActivateEvents --
618 *
619 * This function is called by the Mac and Windows window manager
620 * routines when a toplevel window is activated or deactivated.
621 * Activate/Deactivate events will be sent to every subwindow of
622 * the toplevel followed by a FocusIn/FocusOut message.
623 *
624 * Results:
625 * None.
626 *
627 * Side effects:
628 * Generates X events.
629 *
630 *----------------------------------------------------------------------
631 */
632
633 void
634 TkGenerateActivateEvents(winPtr, active)
635 TkWindow *winPtr; /* Toplevel to activate. */
636 int active; /* Non-zero if the window is being
637 * activated, else 0.*/
638 {
639 XEvent event;
640
641 /*
642 * Generate Activate and Deactivate events. This event
643 * is sent to every subwindow in a toplevel window.
644 */
645
646 event.xany.serial = winPtr->display->request++;
647 event.xany.send_event = False;
648 event.xany.display = winPtr->display;
649 event.xany.window = winPtr->window;
650
651 event.xany.type = active ? ActivateNotify : DeactivateNotify;
652 TkQueueEventForAllChildren(winPtr, &event);
653
654 }
655
656 #endif /* WIN32 */