OSDN Git Service

Merge branch 'master' of git://github.com/monaka/binutils
[pf3gnuchains/pf3gnuchains3x.git] / tk / generic / tkPointer.c
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$
15  */
16
17 #include "tkInt.h"
18
19 #ifdef __WIN32__
20 #include "tkWinInt.h"
21 #endif
22
23 #if defined(MAC_TCL)
24 #include "tkMacInt.h"
25 #define Cursor XCursor
26 #endif
27
28 #if defined(MAC_OSX_TK)
29 #include "tkMacOSXInt.h"
30 #define Cursor XCursor
31 #endif
32
33 /*
34  * Mask that selects any of the state bits corresponding to buttons,
35  * plus masks that select individual buttons' bits:
36  */
37
38 #define ALL_BUTTONS \
39         (Button1Mask|Button2Mask|Button3Mask|Button4Mask|Button5Mask)
40 static unsigned int buttonMasks[] = {
41     Button1Mask, Button2Mask, Button3Mask, Button4Mask, Button5Mask
42 };
43 #define ButtonMask(b) (buttonMasks[(b)-Button1])
44
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. */
55 } ThreadSpecificData;
56 static Tcl_ThreadDataKey dataKey;
57
58 /*
59  * Forward declarations of procedures used in this file.
60  */
61
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));
68 \f
69 /*
70  *----------------------------------------------------------------------
71  *
72  * InitializeEvent --
73  *
74  *      Initializes the common fields for several X events.
75  *
76  * Results:
77  *      None.
78  *
79  * Side effects:
80  *      Fills in the specified event structure.
81  *
82  *----------------------------------------------------------------------
83  */
84
85 static void
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. */
93 {
94     eventPtr->type = type;
95     eventPtr->xany.serial = LastKnownRequestProcessed(winPtr->display);
96     eventPtr->xany.send_event = False;
97     eventPtr->xany.display = winPtr->display;
98
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;
103
104     switch (type) {
105         case EnterNotify:
106         case LeaveNotify:
107             eventPtr->xcrossing.mode = NotifyNormal;
108             eventPtr->xcrossing.state = state;
109             eventPtr->xcrossing.detail = detail;
110             eventPtr->xcrossing.focus = False;
111             break;
112         case MotionNotify:
113             eventPtr->xmotion.state = state;
114             eventPtr->xmotion.is_hint = detail;
115             break;
116         case ButtonPress:
117         case ButtonRelease:
118             eventPtr->xbutton.state = state;
119             eventPtr->xbutton.button = detail;
120             break;
121     }
122     TkChangeEventWindow(eventPtr, winPtr);
123 }
124 \f
125 /*
126  *----------------------------------------------------------------------
127  *
128  * GenerateEnterLeave --
129  *
130  *      Update the current mouse window and position, and generate
131  *      any enter/leave events that are needed.
132  *
133  * Results:
134  *      Returns 1 if enter/leave events were generated.
135  *
136  * Side effects:
137  *      May insert events into the Tk event queue.
138  *
139  *----------------------------------------------------------------------
140  */
141
142 static int
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. */
147 {
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;
153
154     if (winPtr != tsdPtr->lastWinPtr) {
155         if (restrictWinPtr) {
156             int newPos, oldPos;
157
158             newPos = TkPositionInTree(winPtr, restrictWinPtr);
159             oldPos = TkPositionInTree(lastWinPtr, restrictWinPtr);
160
161             /*
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.
164              */
165
166             if ((newPos != oldPos) && ((newPos == TK_GRAB_IN_TREE)
167                     || (oldPos == TK_GRAB_IN_TREE))) {
168                 XEvent event;
169                 int type, detail;
170
171                 if (newPos == TK_GRAB_IN_TREE) {
172                     type = EnterNotify;
173                 } else {
174                     type = LeaveNotify;
175                 }
176                 if ((oldPos == TK_GRAB_ANCESTOR)
177                         || (newPos == TK_GRAB_ANCESTOR)) {
178                     detail = NotifyAncestor;
179                 } else {
180                     detail = NotifyVirtual;
181                 }
182                 InitializeEvent(&event, restrictWinPtr, type, x, y,
183                         state, detail);
184                 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
185             }
186
187         } else {
188             TkWindow *targetPtr;
189
190             if ((lastWinPtr == NULL)
191                 || (lastWinPtr->window == None)) {
192                 targetPtr = winPtr;
193             } else {
194                 targetPtr = lastWinPtr;
195             }
196
197             if (targetPtr && (targetPtr->window != None)) {
198                 XEvent event;
199
200                 /*
201                  * Generate appropriate Enter/Leave events.
202                  */
203
204                 InitializeEvent(&event, targetPtr, LeaveNotify, x, y, state,
205                         NotifyNormal);
206
207                 TkInOutEvents(&event, lastWinPtr, winPtr, LeaveNotify,
208                         EnterNotify, TCL_QUEUE_TAIL);
209                 crossed = 1;
210             }
211         }
212         tsdPtr->lastWinPtr = winPtr;
213     }
214
215     return crossed;
216 }
217 \f
218 /*
219  *----------------------------------------------------------------------
220  *
221  * Tk_UpdatePointer --
222  *
223  *      This function updates the pointer state machine given an
224  *      the current window, position and modifier state.
225  *
226  * Results:
227  *      None.
228  *
229  * Side effects:
230  *      May queue new events and update the grab state.
231  *
232  *----------------------------------------------------------------------
233  */
234
235 void
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. */
241 {
242     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
243             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
244     TkWindow *winPtr = (TkWindow *)tkwin;
245     TkWindow *targetWinPtr;
246     XPoint pos;
247     XEvent event;
248     int changes = (state ^ tsdPtr->lastState) & ALL_BUTTONS;
249     int type, b, mask;
250
251     pos.x = x;
252     pos.y = y;
253
254     /*
255      * Use the current keyboard state, but the old mouse button
256      * state since we haven't generated the button events yet.
257      */
258
259     tsdPtr->lastState = (state & ~ALL_BUTTONS) | (tsdPtr->lastState
260             & ALL_BUTTONS);
261
262     /*
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.
266      */
267
268     if (GenerateEnterLeave(winPtr, x, y, tsdPtr->lastState)) {
269         tsdPtr->lastPos = pos;
270     }
271
272     /*
273      * Generate ButtonPress/ButtonRelease events based on the differences
274      * between the current button state and the last known button state.
275      */
276
277     for (b = Button1; b <= Button3; b++) {
278         mask = ButtonMask(b);
279         if (changes & mask) {
280             if (state & mask) { 
281                 type = ButtonPress;
282
283                 /*
284                  * ButtonPress - Set restrict window if we aren't grabbed, or
285                  * if this is the first button down.
286                  */
287
288                 if (!tsdPtr->restrictWinPtr) {
289                     if (!tsdPtr->grabWinPtr) {
290
291                         /*
292                          * Mouse is not grabbed, so set a button grab.
293                          */
294
295                         tsdPtr->restrictWinPtr = winPtr;
296                         TkpSetCapture(tsdPtr->restrictWinPtr);
297
298                     } else if ((tsdPtr->lastState & ALL_BUTTONS) == 0) {
299
300                         /*
301                          * Mouse is in a non-button grab, so ensure
302                          * the button grab is inside the grab tree.
303                          */
304
305                         if (TkPositionInTree(winPtr, tsdPtr->grabWinPtr)
306                                 == TK_GRAB_IN_TREE) {
307                             tsdPtr->restrictWinPtr = winPtr;
308                         } else {
309                             tsdPtr->restrictWinPtr = tsdPtr->grabWinPtr;
310                         }
311                         TkpSetCapture(tsdPtr->restrictWinPtr);
312                     }
313                 }
314
315             } else {
316                 type = ButtonRelease;
317
318                 /*
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.
322                  */
323
324                 if ((tsdPtr->lastState & ALL_BUTTONS) == mask) {
325                     if (!tsdPtr->grabWinPtr) {
326                         TkpSetCapture(NULL);
327                     }
328                 }
329
330                 /*
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.
334                  */
335
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;
343
344                     GenerateEnterLeave(winPtr, x, y, tsdPtr->lastState);
345                     tsdPtr->lastPos = pos;
346                     continue;
347                 }               
348             }
349
350             /*
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.
355              */
356
357             if (tsdPtr->restrictWinPtr) {
358                 targetWinPtr = tsdPtr->restrictWinPtr;
359             } else if (tsdPtr->grabWinPtr && !winPtr) {
360                 targetWinPtr = tsdPtr->grabWinPtr;
361             } else {
362                 targetWinPtr = winPtr;
363             }
364
365             /*
366              * If we still have a target window, send the event.
367              */
368
369             if (winPtr != NULL) {
370                 InitializeEvent(&event, targetWinPtr, type, x, y,
371                         tsdPtr->lastState, b);
372                 Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
373             }
374
375             /*
376              * Update the state for the next iteration.
377              */
378
379             tsdPtr->lastState = (type == ButtonPress)
380                 ? (tsdPtr->lastState | mask) : (tsdPtr->lastState & ~mask);
381             tsdPtr->lastPos = pos;
382         }
383     }
384
385     /*
386      * Make sure the cursor window is up to date.
387      */
388
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;
394     } else {
395         targetWinPtr = winPtr;
396     }
397     UpdateCursor(targetWinPtr);
398
399     /*
400      * If no other events caused the position to be updated,
401      * generate a motion event.
402      */
403
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;
409         }
410
411         if (targetWinPtr != NULL) {
412             InitializeEvent(&event, targetWinPtr, MotionNotify, x, y,
413                     tsdPtr->lastState, NotifyNormal);
414             Tk_QueueWindowEvent(&event, TCL_QUEUE_TAIL);
415         }
416         tsdPtr->lastPos = pos;
417     }
418 }
419 \f
420 /*
421  *----------------------------------------------------------------------
422  *
423  * XGrabPointer --
424  *
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.
428  *
429  * Results:
430  *      Always returns GrabSuccess.
431  *
432  * Side effects:
433  *      Turns on mouse capture, sets the global grab pointer, and
434  *      clears any window restrictions.
435  *
436  *----------------------------------------------------------------------
437  */
438
439 int
440 XGrabPointer(display, grab_window, owner_events, event_mask, pointer_mode,
441         keyboard_mode, confine_to, cursor, time)
442     Display* display;
443     Window grab_window;
444     Bool owner_events;
445     unsigned int event_mask;
446     int pointer_mode;
447     int keyboard_mode;
448     Window confine_to;
449     Cursor cursor;
450     Time time;
451 {
452     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
453             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
454
455     display->request++;
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);
462     }
463     return GrabSuccess;
464 }
465 \f
466 /*
467  *----------------------------------------------------------------------
468  *
469  * XUngrabPointer --
470  *
471  *      Release the current grab.
472  *
473  * Results:
474  *      None.
475  *
476  * Side effects:
477  *      Releases the mouse capture.
478  *
479  *----------------------------------------------------------------------
480  */
481
482 void
483 XUngrabPointer(display, time)
484     Display* display;
485     Time time;
486 {
487     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
488             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
489
490     display->request++;
491     tsdPtr->grabWinPtr = NULL;
492     tsdPtr->restrictWinPtr = NULL;
493     TkpSetCapture(NULL);
494     UpdateCursor(tsdPtr->lastWinPtr);
495 }
496 \f
497 /*
498  *----------------------------------------------------------------------
499  *
500  * TkPointerDeadWindow --
501  *
502  *      Clean up pointer module state when a window is destroyed.
503  *
504  * Results:
505  *      None.
506  *
507  * Side effects:
508  *      May release the current capture window.
509  *
510  *----------------------------------------------------------------------
511  */
512
513 void
514 TkPointerDeadWindow(winPtr)
515     TkWindow *winPtr;
516 {
517     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
518             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
519
520     if (winPtr == tsdPtr->lastWinPtr) {
521         tsdPtr->lastWinPtr = NULL;
522     }
523     if (winPtr == tsdPtr->grabWinPtr) {
524         tsdPtr->grabWinPtr = NULL;
525     }
526     if (winPtr == tsdPtr->restrictWinPtr) {
527         tsdPtr->restrictWinPtr = NULL;
528     }
529     if (!(tsdPtr->restrictWinPtr || tsdPtr->grabWinPtr)) {
530         TkpSetCapture(NULL);
531     }
532 }
533 \f
534 /*
535  *----------------------------------------------------------------------
536  *
537  * UpdateCursor --
538  *
539  *      Set the windows global cursor to the cursor associated with
540  *      the given Tk window.
541  *
542  * Results:
543  *      None.
544  *
545  * Side effects:
546  *      Changes the mouse cursor.
547  *
548  *----------------------------------------------------------------------
549  */
550
551 static void
552 UpdateCursor(winPtr)
553     TkWindow *winPtr;
554 {
555     Cursor cursor = None;
556     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
557             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
558
559     /*
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
562      * cursor.
563      */
564
565     tsdPtr->cursorWinPtr = winPtr;
566     while (winPtr != NULL) {
567         if (winPtr->atts.cursor != None) {
568             cursor = winPtr->atts.cursor;
569             break;
570         } else if (winPtr->flags & TK_TOP_HIERARCHY) {
571             break;
572         }
573         winPtr = winPtr->parentPtr;
574     }
575     TkpSetCursor((TkpCursor) cursor);
576 }
577 \f
578 /*
579  *----------------------------------------------------------------------
580  *
581  * XDefineCursor --
582  *
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
586  *      and grab state.
587  *
588  * Results:
589  *      None.
590  *
591  * Side effects:
592  *      May update the cursor.
593  *
594  *----------------------------------------------------------------------
595  */
596
597 void
598 XDefineCursor(display, w, cursor)
599     Display* display;
600     Window w;
601     Cursor cursor;
602 {
603     TkWindow *winPtr = (TkWindow *)Tk_IdToWindow(display, w);
604     ThreadSpecificData *tsdPtr = (ThreadSpecificData *) 
605             Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData));
606
607     if (tsdPtr->cursorWinPtr == winPtr) {
608         UpdateCursor(winPtr);
609     }
610     display->request++;
611 }
612 \f
613 /*
614  *----------------------------------------------------------------------
615  *
616  * TkGenerateActivateEvents --
617  *
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.
622  *
623  * Results:
624  *      None.
625  *
626  * Side effects:
627  *      Generates X events.
628  *
629  *----------------------------------------------------------------------
630  */
631
632 void
633 TkGenerateActivateEvents(winPtr, active)
634     TkWindow *winPtr;           /* Toplevel to activate. */
635     int active;                 /* Non-zero if the window is being
636                                  * activated, else 0.*/
637 {
638     XEvent event;
639     
640     /* 
641      * Generate Activate and Deactivate events.  This event
642      * is sent to every subwindow in a toplevel window.
643      */
644
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;
649
650     event.xany.type = active ? ActivateNotify : DeactivateNotify;
651     TkQueueEventForAllChildren(winPtr, &event);
652     
653 }