PageRenderTime 20ms CodeModel.GetById 4ms app.highlight 14ms RepoModel.GetById 0ms app.codeStats 0ms

/GUI/Cocoa/Events.py

https://bitbucket.org/alsh/pygui-mirror
Python | 158 lines | 141 code | 9 blank | 8 comment | 0 complexity | 748ed31c5c02fd00da2b7fbd3ade588b MD5 | raw file
  1#
  2#		Python GUI - Events - PyObjC version
  3#
  4
  5import AppKit
  6from AppKit import NSEvent, \
  7    NSShiftKeyMask, NSControlKeyMask, NSCommandKeyMask, NSAlternateKeyMask
  8import Globals
  9from GEvents import Event as GEvent
 10
 11_ns_event_type_to_kind = {
 12    AppKit.NSLeftMouseDown: 'mouse_down',
 13    AppKit.NSLeftMouseUp: 'mouse_up',
 14    AppKit.NSRightMouseDown: 'mouse_down',
 15    AppKit.NSRightMouseUp: 'mouse_up',
 16    AppKit.NSOtherMouseDown: 'mouse_down',
 17    AppKit.NSOtherMouseUp: 'mouse_up',
 18    AppKit.NSMouseMoved: 'mouse_move',
 19    AppKit.NSLeftMouseDragged: 'mouse_drag',
 20    AppKit.NSRightMouseDragged: 'mouse_drag',
 21    AppKit.NSOtherMouseDragged: 'mouse_drag',
 22    AppKit.NSMouseEntered: 'mouse_enter',
 23    AppKit.NSMouseExited: 'mouse_leave',
 24    AppKit.NSKeyDown: 'key_down',
 25    AppKit.NSKeyUp: 'key_up',
 26    AppKit.NSFlagsChanged: 'flags_changed',
 27    AppKit.NSAppKitDefined: 'app_kit_defined',
 28    AppKit.NSSystemDefined: 'system_defined',
 29    AppKit.NSApplicationDefined: 'application_defined',
 30    AppKit.NSPeriodic: 'periodic',
 31    AppKit.NSCursorUpdate: 'cursor_update',
 32}
 33
 34_ns_event_type_to_button = {
 35    AppKit.NSLeftMouseDown: 'left',
 36    AppKit.NSLeftMouseUp: 'left',
 37    AppKit.NSRightMouseDown: 'right',
 38    AppKit.NSRightMouseUp: 'right',
 39    AppKit.NSOtherMouseDown: 'middle',
 40    AppKit.NSOtherMouseUp: 'middle',
 41    AppKit.NSLeftMouseDragged: 'left',
 42    AppKit.NSRightMouseDragged: 'right',
 43    AppKit.NSOtherMouseDragged: 'middle',
 44}
 45
 46_ns_keycode_to_keyname = {
 47    AppKit.NSUpArrowFunctionKey: 'up_arrow',
 48    AppKit.NSDownArrowFunctionKey: 'down_arrow',
 49    AppKit.NSLeftArrowFunctionKey: 'left_arrow',
 50    AppKit.NSRightArrowFunctionKey: 'right_arrow',
 51    AppKit.NSF1FunctionKey: 'f1',
 52    AppKit.NSF2FunctionKey: 'f2',
 53    AppKit.NSF3FunctionKey: 'f3',
 54    AppKit.NSF4FunctionKey: 'f4',
 55    AppKit.NSF5FunctionKey: 'f5',
 56    AppKit.NSF6FunctionKey: 'f6',
 57    AppKit.NSF7FunctionKey: 'f7',
 58    AppKit.NSF8FunctionKey: 'f8',
 59    AppKit.NSF9FunctionKey: 'f9',
 60    AppKit.NSF10FunctionKey: 'f10', 
 61    AppKit.NSF11FunctionKey: 'f11',
 62    AppKit.NSF12FunctionKey: 'f12',
 63    AppKit.NSF13FunctionKey: 'f13',
 64    AppKit.NSF14FunctionKey: 'f14',
 65    AppKit.NSF15FunctionKey : 'f15',
 66    AppKit.NSDeleteFunctionKey: 'delete',
 67    AppKit.NSHomeFunctionKey: 'home',
 68    AppKit.NSEndFunctionKey: 'end',
 69    AppKit.NSPageUpFunctionKey: 'page_up',
 70    AppKit.NSPageDownFunctionKey: 'page_down',
 71    AppKit.NSClearLineFunctionKey: 'clear',
 72    #AppKit.NSHelpFunctionKey: 'help',
 73    AppKit.NSHelpFunctionKey: 'insert',
 74    "\x03": 'enter',
 75}
 76
 77_mouse_events = [
 78    'mouse_down', 'mouse_drag', 'mouse_up',
 79    'mouse_move', 'mouse_enter', 'mouse_exit'
 80]
 81
 82_key_events = [
 83    'key_down', 'key_up'
 84]
 85
 86_ns_screen_height = None
 87
 88class Event(GEvent):
 89    """Platform-dependent modifiers (boolean):
 90        command            The Macintosh Command key.
 91        option             The Macintosh Option key.
 92    """
 93
 94    global_position = (0, 0)
 95    position = (0, 0)
 96    button = ''
 97    num_clicks = 0
 98    char = ""
 99    unichars = ""
100    key = ''
101    auto = False
102    delta = (0, 0)
103
104    def __init__(self, ns_event):
105        self._ns_event = ns_event
106        _ns_type = ns_event.type()
107        kind = _ns_event_type_to_kind[_ns_type]
108        self.kind = kind
109        self.time = ns_event.timestamp()
110        ns_window = ns_event.window()
111        is_mouse_event = kind in _mouse_events
112        if is_mouse_event:
113            ns_win_pos = ns_event.locationInWindow()
114            x, y = ns_window.convertBaseToScreen_(ns_win_pos)
115        else:
116            ns_last_mouse = Globals.ns_last_mouse_moved_event
117            if ns_last_mouse:
118                ns_window = ns_last_mouse.window()
119                if ns_window:
120                    ns_win_pos = ns_last_mouse.locationInWindow()
121                    x, y = ns_window.convertBaseToScreen_(ns_win_pos)
122                else:
123                    x, y = ns_last_mouse.locationInWindow()
124            else:
125                x, y = NSEvent.mouseLocation()
126        h = Globals.ns_screen_height
127        self.global_position = (x, h - y)
128        if is_mouse_event:
129            self.button = _ns_event_type_to_button.get(_ns_type, '')
130            if kind == 'mouse_down':
131                self.num_clicks = ns_event.clickCount()
132            self.delta = (ns_event.deltaX(), ns_event.deltaY())
133        ns_flags = ns_event.modifierFlags()
134        self.shift = self.extend_contig = (ns_flags & NSShiftKeyMask) <> 0
135        self.control = (ns_flags & NSControlKeyMask) <> 0
136        self.command = self.extend_noncontig = (ns_flags & NSCommandKeyMask) <> 0
137        self.option = (ns_flags & NSAlternateKeyMask) <> 0
138        if kind in _key_events:
139            self.auto = ns_event.isARepeat()
140            ns_chars = ns_event.characters()
141            #print "Event.__init__: ns_chars =", repr(ns_chars) ###
142            self.unichars = ns_chars
143            if len(ns_chars) == 1:
144                if ns_chars == "\x19" and ns_event.keyCode() == 48:
145                    self.char = "\t"
146                elif ns_chars <= "\x7e":
147                    self.char = str(ns_chars)
148                elif ns_chars == "\x7f":
149                    self.char = "\x08"
150                key = _ns_keycode_to_keyname.get(ns_chars, "")
151                self.key = key
152                if key == 'enter':
153                    self.char = "\r"
154                elif key == 'delete':
155                    self.char = "\x7f"
156
157    def _platform_modifiers_str(self):
158        return " command:%s option:%s" % (self.command, self.option)