-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScottySMEvents.py
457 lines (390 loc) · 15.4 KB
/
ScottySMEvents.py
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
# -*- coding: utf-8 -*-
#
# IIKit -- the Instrumental Interaction Toolkit
# Copyright 2009-2011, Université Paris-Sud
# by James R. Eagan (code at my last name dot me)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# and GNU Lesser General Public License along with this program.
# If not, see <http://www.gnu.org/licenses/>.
from AppKit import *
from Foundation import *
import objc
import jre.cocoa # To load NSEvent category additions
from IIKit import *
from StateMachines import *
from InstrumentManager import InstrumentManager
import copy
# Event Types as of OS 10.6.2
# NSLeftMouseDown
# NSLeftMouseUp
# NSRightMouseDown
# NSRightMouseUp
# NSMouseMoved
# NSLeftMouseDragged
# NSRightMouseDragged
# NSMouseEntered
# NSMouseExited
# NSKeyDown
# NSKeyUp
# NSFlagsChanged
# NSAppKitDefined
# NSSystemDefined
# NSApplicationDefined
# NSPeriodic
# NSCursorUpdate
# NSScrollWheel
# NSTabletPoint
# NSTabletProximity
# NSOtherMouseDown
# NSOtherMouseUp
# NSOtherMouseDragged
# NSEventTypeGesture
# NSEventTypeMagnify
# NSEventTypeSwipe
# NSEventTypeRotate
# NSEventTypeBeginGesture
# NSEventTypeEndGesture
class CocoaEvent(Event):
def __init__(self, etype, *args, **options):
if isinstance(etype, NSEvent):
self._cocoa_event = etype
etype = etype.type()
else:
etype = etype
super(CocoaEvent, self).__init__(etype)
self.options = copy.copy(options)
def __match__(self, transition):
assert isinstance(self.type, NSEvent), "Event does not wrap a Cocoa Event"
for option, value in self.options.items():
if hasattr(self.type, option):
if getattr(self.type, option) != value:
return False
return True
def cocoaEvent(self):
return self._cocoa_event
# Cocoa General Event Info (See NSEvent API Ref)
def context(self):
return self._cocoa_event.context()
def locationInWindow(self):
return self._cocoa_event.locationInWindow()
def locationInScreen(self):
# This is not actually a part of Cocoa, but is present as a category addition in jre.cocoa.
return self._cocoa_event.locationInScreen()
def modifierFlags(self):
return self._cocoa_event.modifierFlags()
def timestamp(self):
return self._cocoa_event.timestamp()
def window(self):
window = self._cocoa_event.window()
if isinstance(window, (GlassWindow,)):
return window.parentWindow()
else:
return window
def cocoaWindow(self):
# FIXME: check if event is really delivered to a glass window !
window = self._cocoa_event.window()
if not window:
return window
else:
return window.parentWindow()
def glassWindow(self):
return self._cocoa_event.window() # FIXME: check if event is really delivered to a glass window !
def windowNumber(self):
return self._cocoa_event.windowNumber()
def eventRef(self):
return self._cocoa_event.eventRef()
def CGEvent(self):
return self._cocoa_event.CGEvent()
# Cocoa Key Event Info (See NSEvent API Ref)
def characters(self):
return self._cocoa_event.characters()
def charactersIgnoringModifiers(self):
return self._cocoa_event.charactersIgnoringModifiers()
def isARepeat(self):
return self._cocoa_event.isARepeat()
def keyCode(self):
return self._cocoa_event.keyCode()
# Cocoa Mouse Event Info (See NSEvent API Ref)
def buttonNumber(self):
return self._cocoa_event.buttonNumber()
def clickCount(self):
return self._cocoa_event.clickCount()
def pressure(self):
return self._cocoa_event.pressure()
# TODO: Other Cocoa Event info (See NSEvent API Ref)
class CocoaActionEvent(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(CocoaActionEvent, self).__init__(args[0][0], sender=args[0][1], **options)
else:
super(CocoaActionEvent, self).__init__(self._senderMethodName, **options)
class MouseDown(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(MouseDown, self).__init__(args[0], **options)
else:
super(MouseDown, self).__init__(NSMouseEvent, **options)
# NSLeftMouseDown
class LeftMouseDown(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(LeftMouseDown, self).__init__(args[0], **options)
else:
super(LeftMouseDown, self).__init__(NSLeftMouseDown, **options)
# NSLeftMouseUp
class LeftMouseUp(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(LeftMouseUp, self).__init__(args[0], **options)
else:
super(LeftMouseUp, self).__init__(NSLeftMouseUp, **options)
# NSRightMouseDown
class RightMouseDown(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(RightMouseDown, self).__init__(args[0], **options)
else:
super(RightMouseDown, self).__init__(NSRightMouseDown, **options)
# NSRightMouseUp
class RightMouseUp(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(RightMouseUp, self).__init__(args[0], **options)
else:
super(RightMouseUp, self).__init__(NSRightMouseUp, **options)
# NSMouseMoved
class MouseMoved(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(MouseMoved, self).__init__(args[0], **options)
else:
super(MouseMoved, self).__init__(NSMouseMoved, **options)
# NSLeftMouseDragged
class LeftMouseDragged(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(LeftMouseDragged, self).__init__(args[0], **options)
else:
super(LeftMouseDragged, self).__init__(NSLeftMouseDragged, **options)
# NSRightMouseDragged
class RightMouseDragged(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(RightMouseDragged, self).__init__(args[0], **options)
else:
super(RightMouseDragged, self).__init__(NSRightMouseDragged, **options)
# NSMouseEntered
class MouseEntered(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(MouseEntered, self).__init__(args[0], **options)
else:
super(MouseEntered, self).__init__(NSMouseEntered, **options)
# NSMouseExited
class MouseExited(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(MouseExited, self).__init__(args[0], **options)
else:
super(MouseExited, self).__init__(NSMouseExited, **options)
# NSKeyDown
class KeyDown(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(KeyDown, self).__init__(args[0], **options)
else:
super(KeyDown, self).__init__(NSKeyDown, **options)
# NSKeyUp
class KeyUp(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(KeyUp, self).__init__(args[0], **options)
else:
super(KeyUp, self).__init__(NSKeyUp, **options)
# NSFlagsChanged
class FlagsChanged(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(FlagsChanged, self).__init__(args[0], **options)
else:
super(FlagsChanged, self).__init__(NSFlagsChanged, **options)
# NSAppKitDefined
class AppKitDefined(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(AppKitDefined, self).__init__(args[0], **options)
else:
super(AppKitDefined, self).__init__(NSAppKitDefined, **options)
# NSSystemDefined
class SystemDefined(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(SystemDefined, self).__init__(args[0], **options)
else:
super(SystemDefined, self).__init__(NSSystemDefined, **options)
# NSApplicationDefined
class ApplicationDefined(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(ApplicationDefined, self).__init__(args[0], **options)
else:
super(ApplicationDefined, self).__init__(ApplicationDefined, **options)
# NSPeriodic
class Periodic(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(Periodic, self).__init__(args[0], **options)
else:
super(Periodic, self).__init__(NSPeriodic, **options)
# NSCursorUpdate
class CursorUpdate(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(CursorUpdate, self).__init__(args[0], **options)
else:
super(CursorUpdate, self).__init__(NSCursorUpdate, **options)
# NSScrollWheel
class ScrollWheel(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(ScrollWheel, self).__init__(args[0], **options)
else:
super(ScrollWheel, self).__init__(NSScrollWheel, **options)
# NSTabletPoint
class TabletPoint(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(TabletPoint, self).__init__(args[0], **options)
else:
super(TabletPoint, self).__init__(NSTabletPoint, **options)
# NSTabletProximity
class TabletProximity(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(TabletProximity, self).__init__(args[0], **options)
else:
super(TabletProximity, self).__init__(NSTabletProximity, **options)
# NSOtherMouseDown
class OtherMouseDown(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(OtherMouseDown, self).__init__(args[0], **options)
else:
super(OtherMouseDown, self).__init__(NSOtherMouseDown, **options)
# NSOtherMouseUp
class OtherMouseUp(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(OtherMouseUp, self).__init__(args[0], **options)
else:
super(OtherMouseUp, self).__init__(NSOtherMouseUp, **options)
# NSOtherMouseDragged
class OtherMouseDragged(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(OtherMouseDragged, self).__init__(args[0], **options)
else:
super(OtherMouseDragged, self).__init__(NSOtherMouseDragged, **options)
# NSEventTypeGesture
class EventTypeGesture(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(EventTypeGesture, self).__init__(args[0], **options)
else:
super(EventTypeGesture, self).__init__(NSEventTypeGesture, **options)
# NSEventTypeMagnify
class EventTypeMagnify(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(EventTypeMagnify, self).__init__(args[0], **options)
else:
super(EventTypeMagnify, self).__init__(NSEventTypeMagnify, **options)
# NSEventTypeSwipe
class EventTypeSwipe(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(EventTypeSwipe, self).__init__(args[0], **options)
else:
super(EventTypeSwipe, self).__init__(NSEventTypeSwipe, **options)
# NSEventTypeRotate
class EventTypeRotate(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(EventTypeRotate, self).__init__(args[0], **options)
else:
super(EventTypeRotate, self).__init__(NSEventTypeRotate, **options)
# NSEventTypeBeginGesture
class EventTypeBeginGesture(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(EventTypeBeginGesture, self).__init__(args[0], **options)
else:
super(EventTypeBeginGesture, self).__init__(NSEventTypeBeginGesture, **options)
# NSEventTypeEndGesture
class EventTypeEndGesture(CocoaEvent):
def __init__(self, *args, **options):
if args:
super(EventTypeEndGesture, self).__init__(args[0], **options)
else:
super(EventTypeEndGesture, self).__init__(NSEventTypeEndGesture, **options)
# Action method events
class CancelOperation(CocoaActionEvent):
_senderMethodName = 'cancelOperation'
def cocoaEventWrapper(event):
if isinstance(event, tuple):
etype = event[0] # Unpack action method name
else:
etype = event.type()
if etype in _eventMap:
handlerName, eventClass = _eventMap[etype]
return eventClass(event), handlerName
else:
return CocoaEvent(event), None
InstrumentManager.registerEventWrapper(cocoaEventWrapper, 'fr.lri.insitu.Scotty')
_eventMap = {
NSLeftMouseDown: ('mouseDown', LeftMouseDown),
NSLeftMouseUp: ('mouseUp', LeftMouseUp),
NSRightMouseDown: ('rightMouseDown', RightMouseDown),
NSRightMouseUp: ('rightMouseUp', RightMouseUp),
NSMouseMoved: ('mouseMoved', MouseMoved),
NSLeftMouseDragged: ('mouseDragged', LeftMouseDragged),
NSRightMouseDragged: ('rightMouseDragged', RightMouseDragged),
NSMouseEntered: ('mouseEntered', MouseEntered),
NSMouseExited: ('mouseExited', MouseExited),
NSKeyDown: ('keyDown', KeyDown),
NSKeyUp: ('keyUp', KeyUp),
NSFlagsChanged: ('flagsChanged', FlagsChanged),
NSAppKitDefined: ('appKitDefined', AppKitDefined),
NSSystemDefined: ('systemDefined', SystemDefined),
NSApplicationDefined: ('applicationDefined', ApplicationDefined),
NSPeriodic: ('periodic', Periodic),
NSCursorUpdate: ('cursorUpdate', CursorUpdate),
NSScrollWheel: ('scrollWheel', ScrollWheel),
NSTabletPoint: ('tabletPoint', TabletPoint),
NSTabletProximity: ('tabletProximity', TabletProximity),
NSOtherMouseDown: ('otherMouseDown', OtherMouseDown),
NSOtherMouseUp: ('otherMouseUp', OtherMouseUp),
NSOtherMouseDragged: ('otherMouseDragged', OtherMouseDragged),
# TODO: (Gesture events)
# NSEventTypeGesture
# NSEventTypeMagnify
# NSEventTypeSwipe
# NSEventTypeRotate
# NSEventTypeBeginGesture
# NSEventTypeEndGesture
# Action methods
'cancelOperation': ('cancelOperation', CancelOperation),
}
__all__ = [ clsName for clsName, cls in locals().items() \
if isinstance(cls, type) and issubclass(cls, CocoaEvent) ] + [ 'cocoaEventWrapper', ]