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.
9 * Copyright (c) 1996 by Sun Microsystems, Inc.
11 * See the file "license.terms" for information on usage and redistribution
12 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
25 #define Cursor XCursor
28 #if defined(MAC_OSX_TK)
29 #include "tkMacOSXInt.h"
30 #define Cursor XCursor
34 * Mask that selects any of the state bits corresponding to buttons,
35 * plus masks that select individual buttons' bits:
39 (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)
40 static unsigned int buttonMasks[] = {
41 Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask
43 #define ButtonMask(b) (buttonMasks[(b)-Button1])
45 typedef struct ThreadSpecificData {
46 TkWindow *grabWinPtr; /* Window that defines the top of the
47 * grab tree in a global grab. */
48 int lastState; /* Last known state flags. */
49 XPoint lastPos; /* Last reported mouse position. */
50 TkWindow *lastWinPtr; /* Last reported mouse window. */
51 TkWindow *restrictWinPtr; /* Window to which all mouse events
52 * will be reported. */
53 TkWindow *cursorWinPtr; /* Window that is currently
54 * controlling the global cursor. */
56 static Tcl_ThreadDataKey dataKey;
59 * Forward declarations of procedures used in this file.
62 static int GenerateEnterLeave _ANSI_ARGS_((TkWindow *winPtr,
63 int x, int y, int state));
64 static void InitializeEvent _ANSI_ARGS_((XEvent* eventPtr,
65 TkWindow *winPtr, int type, int x, int y,
66 int state, int detail));
67 static void UpdateCursor _ANSI_ARGS_((TkWindow *winPtr));
70 *----------------------------------------------------------------------
74 * Initializes the common fields for several X events.
80 * Fills in the specified event structure.
82 *----------------------------------------------------------------------
86 InitializeEvent(eventPtr, winPtr, type, x, y, state, detail)
87 XEvent* eventPtr; /* Event structure to initialize. */
88 TkWindow *winPtr; /* Window to make event relative to. */
89 int type; /* Message type. */
90 int x, y; /* Root coords of event. */
91 int state; /* State flags. */
92 int detail; /* Detail value. */
94 eventPtr->type = type;
95 eventPtr->xany.serial = LastKnownRequestProcessed(winPtr->display);
96 eventPtr->xany.send_event = False;
97 eventPtr->xany.display = winPtr->display;
99 eventPtr->xcrossing.root = RootWindow(winPtr->display, winPtr->screenNum);
100 eventPtr->xcrossing.time = TkpGetMS();
101 eventPtr->xcrossing.x_root = x;
102 eventPtr->xcrossing.y_root = y;
107 eventPtr->xcrossing.mode = NotifyNormal;
108 eventPtr->xcrossing.state = state;
109 eventPtr->xcrossing.detail = detail;
110 eventPtr->xcrossing.focus = False;
113 eventPtr->xmotion.state = state;
114 eventPtr->xmotion.is_hint = detail;
118 eventPtr->xbutton.state = state;
119 eventPtr->xbutton.button = detail;
122 TkChangeEventWindow(eventPtr, winPtr);
126 *----------------------------------------------------------------------
128 * GenerateEnterLeave --
130 * Update the current mouse window and position, and generate
131 * any enter/leave events that are needed.
134 * Returns 1 if enter/leave events were generated.
137 * May insert events into the Tk event queue.
139 *----------------------------------------------------------------------
143 GenerateEnterLeave(winPtr, x, y, state)
144 TkWindow *winPtr; /* Current Tk window (or NULL). */
145 int x,y; /* Current mouse position in root coords. */
146 int state; /* State flags. */
148 int crossed = 0; /* 1 if mouse crossed a window boundary */
149 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
150 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
151 TkWindow *restrictWinPtr = tsdPtr->restrictWinPtr;
152 TkWindow *lastWinPtr = tsdPtr->lastWinPtr;
154 if (winPtr != tsdPtr->lastWinPtr) {
155 if (restrictWinPtr) {
158 newPos = TkPositionInTree(winPtr, restrictWinPtr);
159 oldPos = TkPositionInTree(lastWinPtr, restrictWinPtr);
162 * Check if the mouse crossed into or out of the restrict
163 * window. If so, we need to generate an Enter or Leave event.
166 if ((newPos != oldPos) && ((newPos == TK_GRAB_IN_TREE)
167 || (oldPos == TK_GRAB_IN_TREE))) {
171 if (newPos == TK_GRAB_IN_TREE) {
176 if ((oldPos == TK_GRAB_ANCESTOR)
177 || (newPos == TK_GRAB_ANCESTOR)) {
178 detail = NotifyAncestor;
180 detail = NotifyVirtual;
182 InitializeEvent(&event, restrictWinPtr, type, x, y,
184 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
190 if ((lastWinPtr == NULL)
191 || (lastWinPtr->window == None)) {
194 targetPtr = lastWinPtr;
197 if (targetPtr && (targetPtr->window != None)) {
201 * Generate appropriate Enter/Leave events.
204 InitializeEvent(&event, targetPtr, LeaveNotify, x, y, state,
207 TkInOutEvents(&event, lastWinPtr, winPtr, LeaveNotify,
208 EnterNotify, TCL_QUEUE_TAIL);
212 tsdPtr->lastWinPtr = winPtr;
219 *----------------------------------------------------------------------
221 * Tk_UpdatePointer --
223 * This function updates the pointer state machine given an
224 * the current window, position and modifier state.
230 * May queue new events and update the grab state.
232 *----------------------------------------------------------------------
236 Tk_UpdatePointer(tkwin, x, y, state)
237 Tk_Window tkwin; /* Window to which pointer event
238 * is reported. May be NULL. */
239 int x, y; /* Pointer location in root coords. */
240 int state; /* Modifier state mask. */
242 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
243 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
244 TkWindow *winPtr = (TkWindow *)tkwin;
245 TkWindow *targetWinPtr;
248 int changes = (state ^ tsdPtr->lastState) & ALL_BUTTONS;
255 * Use the current keyboard state, but the old mouse button
256 * state since we haven't generated the button events yet.
259 tsdPtr->lastState = (state & ~ALL_BUTTONS) | (tsdPtr->lastState
263 * Generate Enter/Leave events. If the pointer has crossed window
264 * boundaries, update the current mouse position so we don't generate
265 * redundant motion events.
268 if (GenerateEnterLeave(winPtr, x, y, tsdPtr->lastState)) {
269 tsdPtr->lastPos = pos;
273 * Generate ButtonPress/ButtonRelease events based on the differences
274 * between the current button state and the last known button state.
277 for (b = Button1; b <= Button3; b++) {
278 mask = ButtonMask(b);
279 if (changes & mask) {
284 * ButtonPress - Set restrict window if we aren't grabbed, or
285 * if this is the first button down.
288 if (!tsdPtr->restrictWinPtr) {
289 if (!tsdPtr->grabWinPtr) {
292 * Mouse is not grabbed, so set a button grab.
295 tsdPtr->restrictWinPtr = winPtr;
296 TkpSetCapture(tsdPtr->restrictWinPtr);
298 } else if ((tsdPtr->lastState & ALL_BUTTONS) == 0) {
301 * Mouse is in a non-button grab, so ensure
302 * the button grab is inside the grab tree.
305 if (TkPositionInTree(winPtr, tsdPtr->grabWinPtr)
306 == TK_GRAB_IN_TREE) {
307 tsdPtr->restrictWinPtr = winPtr;
309 tsdPtr->restrictWinPtr = tsdPtr->grabWinPtr;
311 TkpSetCapture(tsdPtr->restrictWinPtr);
316 type = ButtonRelease;
319 * ButtonRelease - Release the mouse capture and clear the
320 * restrict window when the last button is released and we
321 * aren't in a global grab.
324 if ((tsdPtr->lastState & ALL_BUTTONS) == mask) {
325 if (!tsdPtr->grabWinPtr) {
331 * If we are releasing a restrict window, then we need
332 * to send the button event followed by mouse motion from
333 * the restrict window to the current mouse position.
336 if (tsdPtr->restrictWinPtr) {
337 InitializeEvent(&event, tsdPtr->restrictWinPtr, type, x, y,
338 tsdPtr->lastState, b);
339 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
340 tsdPtr->lastState &= ~mask;
341 tsdPtr->lastWinPtr = tsdPtr->restrictWinPtr;
342 tsdPtr->restrictWinPtr = NULL;
344 GenerateEnterLeave(winPtr, x, y, tsdPtr->lastState);
345 tsdPtr->lastPos = pos;
351 * If a restrict window is set, make sure the pointer event
352 * is reported relative to that window. Otherwise, if a
353 * global grab is in effect then events outside of windows
354 * managed by Tk should be reported to the grab window.
357 if (tsdPtr->restrictWinPtr) {
358 targetWinPtr = tsdPtr->restrictWinPtr;
359 } else if (tsdPtr->grabWinPtr && !winPtr) {
360 targetWinPtr = tsdPtr->grabWinPtr;
362 targetWinPtr = winPtr;
366 * If we still have a target window, send the event.
369 if (winPtr != NULL) {
370 InitializeEvent(&event, targetWinPtr, type, x, y,
371 tsdPtr->lastState, b);
372 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
376 * Update the state for the next iteration.
379 tsdPtr->lastState = (type == ButtonPress)
380 ? (tsdPtr->lastState | mask) : (tsdPtr->lastState & ~mask);
381 tsdPtr->lastPos = pos;
386 * Make sure the cursor window is up to date.
389 if (tsdPtr->restrictWinPtr) {
390 targetWinPtr = tsdPtr->restrictWinPtr;
391 } else if (tsdPtr->grabWinPtr) {
392 targetWinPtr = (TkPositionInTree(winPtr, tsdPtr->grabWinPtr)
393 == TK_GRAB_IN_TREE) ? winPtr : tsdPtr->grabWinPtr;
395 targetWinPtr = winPtr;
397 UpdateCursor(targetWinPtr);
400 * If no other events caused the position to be updated,
401 * generate a motion event.
404 if (tsdPtr->lastPos.x != pos.x || tsdPtr->lastPos.y != pos.y) {
405 if (tsdPtr->restrictWinPtr) {
406 targetWinPtr = tsdPtr->restrictWinPtr;
407 } else if (tsdPtr->grabWinPtr && !winPtr) {
408 targetWinPtr = tsdPtr->grabWinPtr;
411 if (targetWinPtr != NULL) {
412 InitializeEvent(&event, targetWinPtr, MotionNotify, x, y,
413 tsdPtr->lastState, NotifyNormal);
414 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
416 tsdPtr->lastPos = pos;
421 *----------------------------------------------------------------------
425 * Capture the mouse so event are reported outside of toplevels.
426 * Note that this is a very limited implementation that only
427 * supports GrabModeAsync and owner_events True.
430 * Always returns GrabSuccess.
433 * Turns on mouse capture, sets the global grab pointer, and
434 * clears any window restrictions.
436 *----------------------------------------------------------------------
440 XGrabPointer(display, grab_window, owner_events, event_mask, pointer_mode,
441 keyboard_mode, confine_to, cursor, time)
445 unsigned int event_mask;
452 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
453 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
456 tsdPtr->grabWinPtr = (TkWindow *) Tk_IdToWindow(display, grab_window);
457 tsdPtr->restrictWinPtr = NULL;
458 TkpSetCapture(tsdPtr->grabWinPtr);
459 if (TkPositionInTree(tsdPtr->lastWinPtr, tsdPtr->grabWinPtr)
460 != TK_GRAB_IN_TREE) {
461 UpdateCursor(tsdPtr->grabWinPtr);
467 *----------------------------------------------------------------------
471 * Release the current grab.
477 * Releases the mouse capture.
479 *----------------------------------------------------------------------
483 XUngrabPointer(display, time)
487 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
488 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
491 tsdPtr->grabWinPtr = NULL;
492 tsdPtr->restrictWinPtr = NULL;
494 UpdateCursor(tsdPtr->lastWinPtr);
498 *----------------------------------------------------------------------
500 * TkPointerDeadWindow --
502 * Clean up pointer module state when a window is destroyed.
508 * May release the current capture window.
510 *----------------------------------------------------------------------
514 TkPointerDeadWindow(winPtr)
517 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
518 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
520 if (winPtr == tsdPtr->lastWinPtr) {
521 tsdPtr->lastWinPtr = NULL;
523 if (winPtr == tsdPtr->grabWinPtr) {
524 tsdPtr->grabWinPtr = NULL;
526 if (winPtr == tsdPtr->restrictWinPtr) {
527 tsdPtr->restrictWinPtr = NULL;
529 if (!(tsdPtr->restrictWinPtr || tsdPtr->grabWinPtr)) {
535 *----------------------------------------------------------------------
539 * Set the windows global cursor to the cursor associated with
540 * the given Tk window.
546 * Changes the mouse cursor.
548 *----------------------------------------------------------------------
555 Cursor cursor = None;
556 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
557 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
560 * A window inherits its cursor from its parent if it doesn't
561 * have one of its own. Top level windows inherit the default
565 tsdPtr->cursorWinPtr = winPtr;
566 while (winPtr != NULL) {
567 if (winPtr->atts.cursor != None) {
568 cursor = winPtr->atts.cursor;
570 } else if (winPtr->flags & TK_TOP_HIERARCHY) {
573 winPtr = winPtr->parentPtr;
575 TkpSetCursor((TkpCursor) cursor);
579 *----------------------------------------------------------------------
583 * This function is called to update the cursor on a window.
584 * Since the mouse might be in the specified window, we need to
585 * check the specified window against the current mouse position
592 * May update the cursor.
594 *----------------------------------------------------------------------
598 XDefineCursor(display, w, cursor)
603 TkWindow *winPtr = (TkWindow *)Tk_IdToWindow(display, w);
604 ThreadSpecificData *tsdPtr = (ThreadSpecificData *)
605 Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
607 if (tsdPtr->cursorWinPtr == winPtr) {
608 UpdateCursor(winPtr);
614 *----------------------------------------------------------------------
616 * TkGenerateActivateEvents --
618 * This function is called by the Mac and Windows window manager
619 * routines when a toplevel window is activated or deactivated.
620 * Activate/Deactivate events will be sent to every subwindow of
621 * the toplevel followed by a FocusIn/FocusOut message.
627 * Generates X events.
629 *----------------------------------------------------------------------
633 TkGenerateActivateEvents(winPtr, active)
634 TkWindow *winPtr; /* Toplevel to activate. */
635 int active; /* Non-zero if the window is being
636 * activated, else 0.*/
641 * Generate Activate and Deactivate events. This event
642 * is sent to every subwindow in a toplevel window.
645 event.xany.serial = winPtr->display->request++;
646 event.xany.send_event = False;
647 event.xany.display = winPtr->display;
648 event.xany.window = winPtr->window;
650 event.xany.type = active ? ActivateNotify : DeactivateNotify;
651 TkQueueEventForAllChildren(winPtr, &event);