Debian Codegraph @

pTk/mTk/win/tkWinPointer.c

raw
1 /*
2 * tkWinPointer.c --
3 *
4 * Windows specific mouse tracking code.
5 *
6 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
7 * Copyright (c) 1998-1999 by Scriptics Corporation.
8 *
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 *
12 * RCS: @(#) $Id: tkWinPointer.c,v 1.8 2000/04/19 01:06:51 ericm Exp $
13 */
14
15 #include "tkWinInt.h"
16
17 /*
18 * Check for enter/leave events every MOUSE_TIMER_INTERVAL milliseconds.
19 */
20
21 #define MOUSE_TIMER_INTERVAL 250
22
23 /*
24 * Declarations of static variables used in this file.
25 */
26
27 static int captured = 0; /* 1 if mouse is currently captured. */
28 static TkWindow *keyboardWinPtr = NULL; /* Current keyboard grab window. */
29 static Tcl_TimerToken mouseTimer; /* Handle to the latest mouse timer. */
30 static int mouseTimerSet = 0; /* 1 if the mouse timer is active. */
31
32 /*
33 * Forward declarations of procedures used in this file.
34 */
35
36 static void MouseTimerProc _ANSI_ARGS_((ClientData clientData));
37
38 /*
39 *----------------------------------------------------------------------
40 *
41 * TkWinGetModifierState --
42 *
43 * Return the modifier state as of the last message.
44 *
45 * Results:
46 * Returns the X modifier mask.
47 *
48 * Side effects:
49 * None.
50 *
51 *----------------------------------------------------------------------
52 */
53
54 int
55 TkWinGetModifierState()
56 {
57 int state = 0;
58
59 if (GetKeyState(VK_SHIFT) & 0x8000) {
60 state |= ShiftMask;
61 }
62 if (GetKeyState(VK_CONTROL) & 0x8000) {
63 state |= ControlMask;
64 }
65 if (GetKeyState(VK_MENU) & 0x8000) {
66 state |= ALT_MASK;
67 }
68 if (GetKeyState(VK_CAPITAL) & 0x0001) {
69 state |= LockMask;
70 }
71 if (GetKeyState(VK_NUMLOCK) & 0x0001) {
72 state |= Mod1Mask;
73 }
74 if (GetKeyState(VK_SCROLL) & 0x0001) {
75 state |= Mod3Mask;
76 }
77 if (GetKeyState(VK_LBUTTON) & 0x8000) {
78 state |= Button1Mask;
79 }
80 if (GetKeyState(VK_MBUTTON) & 0x8000) {
81 state |= Button2Mask;
82 }
83 if (GetKeyState(VK_RBUTTON) & 0x8000) {
84 state |= Button3Mask;
85 }
86 return state;
87 }
88
89 /*
90 *----------------------------------------------------------------------
91 *
92 * Tk_PointerEvent --
93 *
94 * This procedure is called for each pointer-related event.
95 * It converts the position to root coords and updates the
96 * global pointer state machine. It also ensures that the
97 * mouse timer is scheduled.
98 *
99 * Results:
100 * None.
101 *
102 * Side effects:
103 * May queue events and change the grab state.
104 *
105 *----------------------------------------------------------------------
106 */
107
108 void
109 Tk_PointerEvent(hwnd, x, y)
110 HWND hwnd; /* Window for coords, or NULL for
111 * the root window. */
112 int x, y; /* Coords relative to hwnd, or screen
113 * if hwnd is NULL. */
114 {
115 POINT pos;
116 int state;
117 Tk_Window tkwin;
118
119 pos.x = x;
120 pos.y = y;
121
122 /*
123 * Convert client coords to root coords if we were given a window.
124 */
125
126 if (hwnd) {
127 ClientToScreen(hwnd, &pos);
128 }
129
130 /*
131 * If the mouse is captured, Windows will report all pointer
132 * events to the capture window. So, we need to determine which
133 * window the mouse is really over and change the event. Note
134 * that the computed hwnd may point to a window not owned by Tk,
135 * or a toplevel decorative frame, so tkwin can be NULL.
136 */
137
138 if (captured || hwnd == NULL) {
139 hwnd = WindowFromPoint(pos);
140 }
141 tkwin = Tk_HWNDToWindow(hwnd);
142
143 state = TkWinGetModifierState();
144
145 Tk_UpdatePointer(tkwin, pos.x, pos.y, state);
146
147 if ((captured || tkwin) && !mouseTimerSet) {
148 mouseTimerSet = 1;
149 mouseTimer = Tcl_CreateTimerHandler(MOUSE_TIMER_INTERVAL,
150 MouseTimerProc, NULL);
151 }
152 }
153
154 /*
155 *----------------------------------------------------------------------
156 *
157 * XGrabKeyboard --
158 *
159 * Simulates a keyboard grab by setting the focus.
160 *
161 * Results:
162 * Always returns GrabSuccess.
163 *
164 * Side effects:
165 * Sets the keyboard focus to the specified window.
166 *
167 *----------------------------------------------------------------------
168 */
169
170 int
171 XGrabKeyboard(display, grab_window, owner_events, pointer_mode,
172 keyboard_mode, time)
173 Display* display;
174 Window grab_window;
175 Bool owner_events;
176 int pointer_mode;
177 int keyboard_mode;
178 Time time;
179 {
180 keyboardWinPtr = TkWinGetWinPtr(grab_window);
181 return GrabSuccess;
182 }
183
184 /*
185 *----------------------------------------------------------------------
186 *
187 * XUngrabKeyboard --
188 *
189 * Releases the simulated keyboard grab.
190 *
191 * Results:
192 * None.
193 *
194 * Side effects:
195 * Sets the keyboard focus back to the value before the grab.
196 *
197 *----------------------------------------------------------------------
198 */
199
200 void
201 XUngrabKeyboard(display, time)
202 Display* display;
203 Time time;
204 {
205 keyboardWinPtr = NULL;
206 }
207
208 /*
209 *----------------------------------------------------------------------
210 *
211 * MouseTimerProc --
212 *
213 * Check the current mouse position and look for enter/leave
214 * events.
215 *
216 * Results:
217 * None.
218 *
219 * Side effects:
220 * May schedule a new timer and/or generate enter/leave events.
221 *
222 *----------------------------------------------------------------------
223 */
224
225 void
226 MouseTimerProc(clientData)
227 ClientData clientData;
228 {
229 POINT pos;
230
231 mouseTimerSet = 0;
232
233 /*
234 * Get the current mouse position and window. Don't do anything
235 * if the mouse hasn't moved since the last time we looked.
236 */
237
238 GetCursorPos(&pos);
239 Tk_PointerEvent(NULL, pos.x, pos.y);
240 }
241
242 /*
243 *----------------------------------------------------------------------
244 *
245 * TkWinCancelMouseTimer --
246 *
247 * If the mouse timer is set, cancel it.
248 *
249 * Results:
250 * None.
251 *
252 * Side effects:
253 * May cancel the mouse timer.
254 *
255 *----------------------------------------------------------------------
256 */
257
258 void
259 TkWinCancelMouseTimer()
260 {
261 if (mouseTimerSet) {
262 Tcl_DeleteTimerHandler(mouseTimer);
263 mouseTimerSet = 0;
264 }
265 }
266
267 /*
268 *----------------------------------------------------------------------
269 *
270 * TkGetPointerCoords --
271 *
272 * Fetch the position of the mouse pointer.
273 *
274 * Results:
275 * *xPtr and *yPtr are filled in with the root coordinates
276 * of the mouse pointer for the display.
277 *
278 * Side effects:
279 * None.
280 *
281 *----------------------------------------------------------------------
282 */
283
284 void
285 TkGetPointerCoords(tkwin, xPtr, yPtr)
286 Tk_Window tkwin; /* Window that identifies screen on which
287 * lookup is to be done. */
288 int *xPtr, *yPtr; /* Store pointer coordinates here. */
289 {
290 POINT point;
291
292 GetCursorPos(&point);
293 *xPtr = point.x;
294 *yPtr = point.y;
295 }
296
297 /*
298 *----------------------------------------------------------------------
299 *
300 * XQueryPointer --
301 *
302 * Check the current state of the mouse. This is not a complete
303 * implementation of this function. It only computes the root
304 * coordinates and the current mask.
305 *
306 * Results:
307 * Sets root_x_return, root_y_return, and mask_return. Returns
308 * true on success.
309 *
310 * Side effects:
311 * None.
312 *
313 *----------------------------------------------------------------------
314 */
315
316 Bool
317 XQueryPointer(display, w, root_return, child_return, root_x_return,
318 root_y_return, win_x_return, win_y_return, mask_return)
319 Display* display;
320 Window w;
321 Window* root_return;
322 Window* child_return;
323 int* root_x_return;
324 int* root_y_return;
325 int* win_x_return;
326 int* win_y_return;
327 unsigned int* mask_return;
328 {
329 display->request++;
330 TkGetPointerCoords(NULL, root_x_return, root_y_return);
331 *mask_return = TkWinGetModifierState();
332 return True;
333 }
334
335 /*
336 *----------------------------------------------------------------------
337 *
338 * XWarpPointer --
339 *
340 * Move pointer to new location. This is not a complete
341 * implementation of this function.
342 *
343 * Results:
344 * None.
345 *
346 * Side effects:
347 * Mouse pointer changes position on screen.
348 *
349 *----------------------------------------------------------------------
350 */
351
352 void
353 XWarpPointer(display, src_w, dest_w, src_x, src_y, src_width,
354 src_height, dest_x, dest_y)
355 Display* display;
356 Window src_w;
357 Window dest_w;
358 int src_x;
359 int src_y;
360 unsigned int src_width;
361 unsigned int src_height;
362 int dest_x;
363 int dest_y;
364 {
365 RECT r;
366
367 GetWindowRect(Tk_GetHWND(dest_w), &r);
368 SetCursorPos(r.left+dest_x, r.top+dest_y);
369 }
370
371 /*
372 *----------------------------------------------------------------------
373 *
374 * XGetInputFocus --
375 *
376 * Retrieves the current keyboard focus window.
377 *
378 * Results:
379 * Returns the current focus window.
380 *
381 * Side effects:
382 * None.
383 *
384 *----------------------------------------------------------------------
385 */
386
387 void
388 XGetInputFocus(display, focus_return, revert_to_return)
389 Display *display;
390 Window *focus_return;
391 int *revert_to_return;
392 {
393 Tk_Window tkwin = Tk_HWNDToWindow(GetFocus());
394 *focus_return = tkwin ? Tk_WindowId(tkwin) : None;
395 *revert_to_return = RevertToParent;
396 display->request++;
397 }
398
399 /*
400 *----------------------------------------------------------------------
401 *
402 * XSetInputFocus --
403 *
404 * Set the current focus window.
405 *
406 * Results:
407 * None.
408 *
409 * Side effects:
410 * Changes the keyboard focus and causes the selected window to
411 * be activated.
412 *
413 *----------------------------------------------------------------------
414 */
415
416
417 #ifndef _LANG
418 /* Perl/Tk has better one in tkWinWm.c which also does activate of toplevel */
419 void
420 XSetInputFocus(display, focus, revert_to, time)
421 Display* display;
422 Window focus;
423 int revert_to;
424 Time time;
425 {
426 display->request++;
427 if (focus != None) {
428 SetFocus(Tk_GetHWND(focus));
429 }
430 }
431 #endif
432
433 /*
434 *----------------------------------------------------------------------
435 *
436 * TkpChangeFocus --
437 *
438 * This procedure is invoked to move the system focus from
439 * one window to another.
440 *
441 * Results:
442 * The return value is the serial number of the command that
443 * changed the focus. It may be needed by the caller to filter
444 * out focus change events that were queued before the command.
445 * If the procedure doesn't actually change the focus then
446 * it returns 0.
447 *
448 * Side effects:
449 * The official Windows focus window changes; the application's focus
450 * window isn't changed by this procedure.
451 *
452 *----------------------------------------------------------------------
453 */
454
455 int
456 TkpChangeFocus(winPtr, force)
457 TkWindow *winPtr; /* Window that is to receive the X focus. */
458 int force; /* Non-zero means claim the focus even
459 * if it didn't originally belong to
460 * topLevelPtr's application. */
461 {
462 TkDisplay *dispPtr = winPtr->dispPtr;
463 Window focusWindow;
464 int dummy, serial;
465 TkWindow *winPtr2;
466
467 if (!force) {
468 XGetInputFocus(dispPtr->display, &focusWindow, &dummy);
469 winPtr2 = (TkWindow *) Tk_IdToWindow(dispPtr->display, focusWindow);
470 if ((winPtr2 == NULL) || (winPtr2->mainPtr != winPtr->mainPtr)) {
471 return 0;
472 }
473 }
474
475 if (winPtr->window == None) {
476 panic("ChangeXFocus got null X window");
477 }
478
479 /*
480 * Change the foreground window so the focus window is raised to the top of
481 * the system stacking order and gets the keyboard focus.
482 */
483
484 if (force) {
485 TkWinSetForegroundWindow(winPtr);
486 }
487 XSetInputFocus(dispPtr->display, winPtr->window, RevertToParent,
488 CurrentTime);
489
490 /*
491 * Remember the current serial number for the X server and issue
492 * a dummy server request. This marks the position at which we
493 * changed the focus, so we can distinguish FocusIn and FocusOut
494 * events on either side of the mark.
495 */
496
497 serial = NextRequest(winPtr->display);
498 XNoOp(winPtr->display);
499 return serial;
500 }
501
502 /*
503 *----------------------------------------------------------------------
504 *
505 * TkpSetCapture --
506 *
507 * This function captures the mouse so that all future events
508 * will be reported to this window, even if the mouse is outside
509 * the window. If the specified window is NULL, then the mouse
510 * is released.
511 *
512 * Results:
513 * None.
514 *
515 * Side effects:
516 * Sets the capture flag and captures the mouse.
517 *
518 *----------------------------------------------------------------------
519 */
520
521 void
522 TkpSetCapture(winPtr)
523 TkWindow *winPtr; /* Capture window, or NULL. */
524 {
525 if (winPtr) {
526 SetCapture(Tk_GetHWND(Tk_WindowId(winPtr)));
527 captured = 1;
528 } else {
529 captured = 0;
530 ReleaseCapture();
531 }
532 }