1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
|
/*
* tkMacOSXMouseEvent.c --
*
* This file implements functions that decode & handle mouse events on
* MacOS X.
*
* Copyright 2001-2009, Apple Inc.
* Copyright (c) 2005-2009 Daniel A. Steffen <das@users.sourceforge.net>
*
* See the file "license.terms" for information on usage and redistribution of
* this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkMacOSXPrivate.h"
#include "tkMacOSXWm.h"
#include "tkMacOSXInt.h"
#include "tkMacOSXDebug.h"
#include "tkMacOSXConstants.h"
typedef struct {
unsigned int state;
long delta;
Window window;
Point global;
Point local;
} MouseEventData;
static Tk_Window captureWinPtr = NULL; /* Current capture window; may be
* NULL. */
static int GenerateButtonEvent(MouseEventData *medPtr);
static unsigned int ButtonModifiers2State(UInt32 buttonState,
UInt32 keyModifiers);
#pragma mark TKApplication(TKMouseEvent)
enum {
NSWindowWillMoveEventType = 20
};
/*
* In OS X 10.6 an NSEvent of type NSMouseMoved would always have a non-Nil
* window attribute pointing to the active window. As of 10.8 this behavior
* had changed. The new behavior was that if the mouse were ever moved outside
* of a window, all subsequent NSMouseMoved NSEvents would have a Nil window
* attribute until the mouse returned to the window. In 11.1 it changed again.
* The window attribute can be non-nil, but referencing a window which does not
* belong to the application.
*/
@implementation TKApplication(TKMouseEvent)
- (NSEvent *) tkProcessMouseEvent: (NSEvent *) theEvent
{
NSWindow *eventWindow = [theEvent window];
NSEventType eventType = [theEvent type];
NSRect viewFrame = [[eventWindow contentView] frame];
TkWindow *winPtr = NULL, *grabWinPtr;
Tk_Window tkwin = NULL, capture, target;
NSPoint local, global;
NSInteger button;
Bool inTitleBar = NO;
int win_x, win_y;
unsigned int buttonState = 0;
static int validPresses = 0, ignoredPresses = 0;
#ifdef TK_MAC_DEBUG_EVENTS
TKLog(@"-[%@(%p) %s] %@", [self class], self, _cmd, theEvent);
#endif
/*
* If this event is not for a Tk toplevel, it should just be passed up the
* responder chain. However, there is an exception for synthesized events,
* which are used in testing. Those events are recognized by having their
* (unused) pressure field set to the impossible value -1.0.
*/
if (![eventWindow isMemberOfClass:[TKWindow class]]) {
if (eventWindow && [theEvent pressure] != -1.0) {
return theEvent;
}
}
/*
* Check if the event is located in the titlebar.
*/
if (eventWindow) {
inTitleBar = viewFrame.size.height < [theEvent locationInWindow].y;
}
button = [theEvent buttonNumber] + Button1;
switch (eventType) {
case NSRightMouseUp:
case NSOtherMouseUp:
buttonState &= ~Tk_GetButtonMask(button);
break;
case NSLeftMouseDragged:
case NSRightMouseDragged:
case NSOtherMouseDragged:
case NSRightMouseDown:
case NSOtherMouseDown:
buttonState |= Tk_GetButtonMask(button);
break;
case NSMouseEntered:
if (!inTitleBar) {
[(TKWindow *)eventWindow setMouseInResizeArea:YES];
}
break;
case NSMouseExited:
[(TKWindow *)eventWindow setMouseInResizeArea:NO];
break;
case NSLeftMouseUp:
case NSLeftMouseDown:
case NSMouseMoved:
case NSScrollWheel:
#if 0
case NSCursorUpdate:
case NSTabletPoint:
case NSTabletProximity:
#endif
break;
default: /* This type of event is ignored. */
return theEvent;
}
/*
* Update the button state. We ignore left button presses that start a
* resize or occur in the title bar. See tickets [d72abe6b54] and
* [39cbacb9e8].
*/
if (eventType == NSLeftMouseDown) {
if ([(TKWindow *)eventWindow mouseInResizeArea] &&
([eventWindow styleMask] & NSResizableWindowMask)) {
/*
* When the left button is pressed in the resize area, we receive
* NSMouseDown, but when it is released we do not receive
* NSMouseUp. So ignore the event and clear the button state but
* do not change the ignoredPresses count.
*/
buttonState &= ~Tk_GetButtonMask(Button1);
return theEvent;
}
if (inTitleBar) {
ignoredPresses++;
return theEvent;
}
validPresses++;
buttonState |= Tk_GetButtonMask(Button1);
}
if (eventType == NSLeftMouseUp) {
if (ignoredPresses > 0) {
ignoredPresses--;
} else if (validPresses > 0) {
validPresses--;
}
if (validPresses == 0) {
buttonState &= ~Tk_GetButtonMask(Button1);
}
}
/*
* Find an appropriate NSWindow to attach to this event, and its
* associated Tk window.
*/
capture = TkpGetCapture();
if (capture) {
winPtr = (TkWindow *) capture;
eventWindow = TkMacOSXGetNSWindowForDrawable(winPtr->window);
if (!eventWindow) {
return theEvent;
}
} else {
if (eventWindow) {
winPtr = TkMacOSXGetTkWindow(eventWindow);
}
if (!winPtr) {
eventWindow = [NSApp mainWindow];
winPtr = TkMacOSXGetTkWindow(eventWindow);
}
}
if (!winPtr) {
/*
* We couldn't find a Tk window for this event. We have to ignore it.
*/
#ifdef TK_MAC_DEBUG_EVENTS
TkMacOSXDbgMsg("Event received with no Tk window.");
#endif
return theEvent;
}
tkwin = (Tk_Window) winPtr;
/*
* Compute the mouse position in local (window) and global (screen)
* coordinates. These are Tk coordinates, meaning that the local origin is
* at the top left corner of the containing toplevel and the global origin
* is at top left corner of the primary screen.
*/
global = [NSEvent mouseLocation];
local = [eventWindow tkConvertPointFromScreen: global];
global.x = floor(global.x);
global.y = floor(TkMacOSXZeroScreenHeight() - global.y);
local.x = floor(local.x);
local.y = floor([eventWindow frame].size.height - local.y);
if (Tk_IsEmbedded(winPtr)) {
TkWindow *contPtr = TkpGetOtherWindow(winPtr);
if (Tk_IsTopLevel(contPtr)) {
local.x -= contPtr->wmInfoPtr->xInParent;
local.y -= contPtr->wmInfoPtr->yInParent;
} else {
TkWindow *topPtr = TkMacOSXGetHostToplevel(winPtr)->winPtr;
local.x -= (topPtr->wmInfoPtr->xInParent + contPtr->changes.x);
local.y -= (topPtr->wmInfoPtr->yInParent + contPtr->changes.y);
}
} else {
local.x -= winPtr->wmInfoPtr->xInParent;
local.y -= winPtr->wmInfoPtr->yInParent;
}
/*
* Use the local coordinates to find the Tk window which should receive
* this event. Also convert local into the coordinates of that window.
* (The converted local coordinates are only needed for scrollwheel
* events.)
*/
target = Tk_TopCoordsToWindow(tkwin, local.x, local.y, &win_x, &win_y);
/*
* Ignore the event if a local grab is in effect and the Tk window is
* not in the grabber's subtree.
*/
grabWinPtr = winPtr->dispPtr->grabWinPtr;
if (grabWinPtr && /* There is a grab in effect ... */
!winPtr->dispPtr->grabFlags && /* and it is a local grab ... */
grabWinPtr->mainPtr == winPtr->mainPtr){ /* in the same application. */
Tk_Window tkwin2;
if (!target) {
return theEvent;
}
for (tkwin2 = target;
!Tk_IsTopLevel(tkwin2);
tkwin2 = Tk_Parent(tkwin2)) {
if (tkwin2 == (Tk_Window)grabWinPtr) {
break;
}
}
if (tkwin2 != (Tk_Window)grabWinPtr) {
return theEvent;
}
}
/*
* Generate an XEvent for this mouse event.
*/
unsigned int state = buttonState;
NSUInteger modifiers = [theEvent modifierFlags];
if (modifiers & NSAlphaShiftKeyMask) {
state |= LockMask;
}
if (modifiers & NSShiftKeyMask) {
state |= ShiftMask;
}
if (modifiers & NSControlKeyMask) {
state |= ControlMask;
}
if (modifiers & NSCommandKeyMask) {
state |= Mod1Mask; /* command key */
}
if (modifiers & NSAlternateKeyMask) {
state |= Mod2Mask; /* option key */
}
if (modifiers & NSNumericPadKeyMask) {
state |= Mod3Mask;
}
if (modifiers & NSFunctionKeyMask) {
state |= Mod4Mask;
}
if (eventType != NSScrollWheel) {
/*
* For normal mouse events, Tk_UpdatePointer will send the appropriate
* XEvents using its cached state information. Unfortunately, it will
* also recompute the local coordinates.
*/
#ifdef TK_MAC_DEBUG_EVENTS
TKLog(@"UpdatePointer %p x %.1f y %.1f %d",
target, global.x, global.y, state);
#endif
Tk_UpdatePointer(target, global.x, global.y, state);
} else {
CGFloat delta;
int coarseDelta;
XEvent xEvent;
/*
* For scroll wheel events we need to send the XEvent here.
*/
xEvent.type = MouseWheelEvent;
xEvent.xbutton.x = win_x;
xEvent.xbutton.y = win_y;
xEvent.xbutton.x_root = global.x;
xEvent.xbutton.y_root = global.y;
xEvent.xany.send_event = false;
xEvent.xany.display = Tk_Display(target);
xEvent.xany.window = Tk_WindowId(target);
delta = [theEvent deltaY];
if (delta != 0.0) {
coarseDelta = (delta > -1.0 && delta < 1.0) ?
(signbit(delta) ? -1 : 1) : lround(delta);
xEvent.xbutton.state = state;
xEvent.xkey.keycode = coarseDelta;
xEvent.xany.serial = LastKnownRequestProcessed(Tk_Display(tkwin));
Tk_QueueWindowEvent(&xEvent, TCL_QUEUE_TAIL);
}
delta = [theEvent deltaX];
if (delta != 0.0) {
coarseDelta = (delta > -1.0 && delta < 1.0) ?
(signbit(delta) ? -1 : 1) : lround(delta);
xEvent.xbutton.state = state | ShiftMask;
xEvent.xkey.keycode = coarseDelta;
xEvent.xany.serial = LastKnownRequestProcessed(Tk_Display(tkwin));
Tk_QueueWindowEvent(&xEvent, TCL_QUEUE_TAIL);
}
}
return theEvent;
}
@end
#pragma mark -
/*
*----------------------------------------------------------------------
*
* TkMacOSXButtonKeyState --
*
* Returns the current state of the button & modifier keys.
*
* Results:
* A bitwise inclusive OR of a subset of the following: Button1Mask,
* ShiftMask, LockMask, ControlMask, Mod*Mask.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
unsigned int
TkMacOSXButtonKeyState(void)
{
UInt32 buttonState = 0, keyModifiers;
int isFrontProcess = (GetCurrentEvent() && Tk_MacOSXIsAppInFront());
buttonState = isFrontProcess ? GetCurrentEventButtonState() :
GetCurrentButtonState();
keyModifiers = isFrontProcess ? GetCurrentEventKeyModifiers() :
GetCurrentKeyModifiers();
return ButtonModifiers2State(buttonState, keyModifiers);
}
/*
*----------------------------------------------------------------------
*
* ButtonModifiers2State --
*
* Converts Carbon mouse button state and modifier values into a Tk
* button/modifier state.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static unsigned int
ButtonModifiers2State(
UInt32 buttonState,
UInt32 keyModifiers)
{
unsigned int state;
/*
* Tk on OSX supports at most 9 buttons.
*/
state = (buttonState & 0x7F) * Button1Mask;
/* Handle buttons 8/9 */
state |= (buttonState & 0x180) * (Button8Mask >> 7);
if (keyModifiers & alphaLock) {
state |= LockMask;
}
if (keyModifiers & shiftKey) {
state |= ShiftMask;
}
if (keyModifiers & controlKey) {
state |= ControlMask;
}
if (keyModifiers & cmdKey) {
state |= Mod1Mask; /* command key */
}
if (keyModifiers & optionKey) {
state |= Mod2Mask; /* option key */
}
if (keyModifiers & kEventKeyModifierNumLockMask) {
state |= Mod3Mask;
}
if (keyModifiers & kEventKeyModifierFnMask) {
state |= Mod4Mask;
}
return state;
}
/*
*----------------------------------------------------------------------
*
* XQueryPointer --
*
* Check the current state of the mouse. This is not a complete
* implementation of this function. It only computes the root coordinates
* and the current mask.
*
* Results:
* Sets root_x_return, root_y_return, and mask_return. Returns true on
* success.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Bool
XQueryPointer(
Display *display,
Window w,
Window *root_return,
Window *child_return,
int *root_x_return,
int *root_y_return,
int *win_x_return,
int *win_y_return,
unsigned int *mask_return)
{
int getGlobal = (root_x_return && root_y_return);
int getLocal = (win_x_return && win_y_return && w != None);
(void)display;
(void)root_return;
(void)child_return;
if (getGlobal || getLocal) {
NSPoint global = [NSEvent mouseLocation];
if (getLocal) {
MacDrawable *macWin = (MacDrawable *)w;
NSWindow *win = TkMacOSXGetNSWindowForDrawable(w);
if (win) {
NSPoint local;
local = [win tkConvertPointFromScreen:global];
local.y = [win frame].size.height - local.y;
if (macWin->winPtr && macWin->winPtr->wmInfoPtr) {
local.x -= macWin->winPtr->wmInfoPtr->xInParent;
local.y -= macWin->winPtr->wmInfoPtr->yInParent;
}
*win_x_return = local.x;
*win_y_return = local.y;
}
}
if (getGlobal) {
*root_x_return = global.x;
*root_y_return = TkMacOSXZeroScreenHeight() - global.y;
}
}
if (mask_return) {
*mask_return = TkMacOSXButtonKeyState();
}
return True;
}
/*
*----------------------------------------------------------------------
*
* TkGenerateButtonEventForXPointer --
*
* This procedure generates an X button event for the current pointer
* state as reported by XQueryPointer().
*
* Results:
* True if event(s) are generated - false otherwise.
*
* Side effects:
* Additional events may be placed on the Tk event queue. Grab state may
* also change.
*
*----------------------------------------------------------------------
*/
MODULE_SCOPE int
TkGenerateButtonEventForXPointer(
Window window) /* X Window containing button event. */
{
MouseEventData med;
int global_x, global_y, local_x, local_y;
bzero(&med, sizeof(MouseEventData));
XQueryPointer(NULL, window, NULL, NULL, &global_x, &global_y,
&local_x, &local_y, &med.state);
med.global.h = global_x;
med.global.v = global_y;
med.local.h = local_x;
med.local.v = local_y;
med.window = window;
return GenerateButtonEvent(&med);
}
/*
*----------------------------------------------------------------------
*
* TkGenerateButtonEvent --
*
* Given a global x & y position and the button key status this procedure
* generates the appropriate X button event. It also handles the state
* changes needed to implement implicit grabs.
*
* Results:
* True if event(s) are generated, false otherwise.
*
* Side effects:
* Additional events may be placed on the Tk event queue. Grab state may
* also change.
*
*----------------------------------------------------------------------
*/
int
TkGenerateButtonEvent(
int x, /* X location of mouse, */
int y, /* Y location of mouse. */
Window window, /* X Window containing button event. */
unsigned int state) /* Button Key state suitable for X event. */
{
MacDrawable *macWin = (MacDrawable *)window;
NSWindow *win = TkMacOSXGetNSWindowForDrawable(window);
MouseEventData med;
bzero(&med, sizeof(MouseEventData));
med.state = state;
med.window = window;
med.global.h = x;
med.global.v = y;
med.local = med.global;
if (win) {
NSPoint local = NSMakePoint(x, TkMacOSXZeroScreenHeight() - y);
local = [win tkConvertPointFromScreen:local];
local.y = [win frame].size.height - local.y;
if (macWin->winPtr && macWin->winPtr->wmInfoPtr) {
local.x -= macWin->winPtr->wmInfoPtr->xInParent;
local.y -= macWin->winPtr->wmInfoPtr->yInParent;
}
med.local.h = local.x;
med.local.v = TkMacOSXZeroScreenHeight() - local.y;
}
return GenerateButtonEvent(&med);
}
/*
*----------------------------------------------------------------------
*
* GenerateButtonEvent --
*
* Generate an X button event from a MouseEventData structure. Handles
* the state changes needed to implement implicit grabs.
*
* Results:
* True if event(s) are generated - false otherwise.
*
* Side effects:
* Additional events may be placed on the Tk event queue. Grab state may
* also change.
*
*----------------------------------------------------------------------
*/
static int
GenerateButtonEvent(
MouseEventData *medPtr)
{
Tk_Window tkwin;
int dummy;
TkDisplay *dispPtr;
#if UNUSED
/*
* ButtonDown events will always occur in the front window. ButtonUp
* events, however, may occur anywhere on the screen. ButtonUp events
* should only be sent to Tk if in the front window or during an implicit
* grab.
*/
if ((medPtr->activeNonFloating == NULL)
|| ((!(TkpIsWindowFloating(medPtr->whichWin))
&& (medPtr->activeNonFloating != medPtr->whichWin))
&& TkpGetCapture() == NULL)) {
return false;
}
#endif
dispPtr = TkGetDisplayList();
tkwin = Tk_IdToWindow(dispPtr->display, medPtr->window);
if (tkwin != NULL) {
tkwin = Tk_TopCoordsToWindow(tkwin, medPtr->local.h, medPtr->local.v,
&dummy, &dummy);
}
Tk_UpdatePointer(tkwin, medPtr->global.h, medPtr->global.v, medPtr->state);
return true;
}
/*
*----------------------------------------------------------------------
*
* TkpWarpPointer --
*
* Move the mouse cursor to the screen location specified by the warpX and
* warpY fields of a TkDisplay.
*
* Results:
* None
*
* Side effects:
* The mouse cursor is moved.
*
*----------------------------------------------------------------------
*/
void
TkpWarpPointer(
TkDisplay *dispPtr)
{
CGPoint pt;
if (dispPtr->warpWindow) {
int x, y;
Tk_GetRootCoords(dispPtr->warpWindow, &x, &y);
pt.x = x + dispPtr->warpX;
pt.y = y + dispPtr->warpY;
} else {
pt.x = dispPtr->warpX;
pt.y = dispPtr->warpY;
}
CGWarpMouseCursorPosition(pt);
}
/*
*----------------------------------------------------------------------
*
* TkpSetCapture --
*
* This function captures the mouse so that all future events will be
* reported to this window, even if the mouse is outside the window. If
* the specified window is NULL, then the mouse is released.
*
* Results:
* None.
*
* Side effects:
* Sets the capture flag and captures the mouse.
*
*----------------------------------------------------------------------
*/
void
TkpSetCapture(
TkWindow *winPtr) /* Capture window, or NULL. */
{
while (winPtr && !Tk_IsTopLevel(winPtr)) {
winPtr = winPtr->parentPtr;
}
captureWinPtr = (Tk_Window)winPtr;
}
/*
*----------------------------------------------------------------------
*
* TkpGetCapture --
*
* Results:
* Returns the current grab window
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
Tk_Window
TkpGetCapture(void)
{
return captureWinPtr;
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|