bindings.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from . import IDLEenvironment
  2. import string
  3. import win32ui
  4. import win32api
  5. import win32con
  6. from . import keycodes
  7. import sys
  8. import traceback
  9. HANDLER_ARGS_GUESS=0
  10. HANDLER_ARGS_NATIVE=1
  11. HANDLER_ARGS_IDLE=2
  12. HANDLER_ARGS_EXTENSION=3
  13. next_id = 5000
  14. event_to_commands = {}# dict of integer IDs to event names.
  15. command_to_events = {}# dict of event names to int IDs
  16. def assign_command_id(event, id = 0):
  17. global next_id
  18. if id == 0:
  19. id = event_to_commands.get(event, 0)
  20. if id == 0:
  21. id = next_id
  22. next_id = next_id + 1
  23. # Only map the ones we allocated - specified ones are assumed to have a handler
  24. command_to_events[id] = event
  25. event_to_commands[event] = id
  26. return id
  27. class SendCommandHandler:
  28. def __init__(self, cmd):
  29. self.cmd = cmd
  30. def __call__(self, *args):
  31. win32ui.GetMainFrame().SendMessage(win32con.WM_COMMAND, self.cmd)
  32. class Binding:
  33. def __init__(self, handler, handler_args_type):
  34. self.handler = handler
  35. self.handler_args_type = handler_args_type
  36. class BindingsManager:
  37. def __init__(self, parent_view):
  38. self.parent_view = parent_view
  39. self.bindings = {} # dict of Binding instances.
  40. self.keymap = {}
  41. def prepare_configure(self):
  42. self.keymap = {}
  43. def complete_configure(self):
  44. for id in command_to_events.keys():
  45. self.parent_view.HookCommand(self._OnCommand, id)
  46. def close(self):
  47. self.parent_view = self.bindings = self.keymap = None
  48. def report_error(self, problem):
  49. try:
  50. win32ui.SetStatusText(problem, 1)
  51. except win32ui.error:
  52. # No status bar!
  53. print(problem)
  54. def update_keymap(self, keymap):
  55. self.keymap.update(keymap)
  56. def bind(self, event, handler, handler_args_type = HANDLER_ARGS_GUESS, cid = 0):
  57. if handler is None:
  58. handler = SendCommandHandler(cid)
  59. self.bindings[event] = self._new_binding(handler, handler_args_type)
  60. self.bind_command(event, cid)
  61. def bind_command(self, event, id = 0):
  62. "Binds an event to a Windows control/command ID"
  63. id = assign_command_id(event, id)
  64. return id
  65. def get_command_id(self, event):
  66. id = event_to_commands.get(event)
  67. if id is None:
  68. # See if we even have an event of that name!?
  69. if event not in self.bindings:
  70. return None
  71. id = self.bind_command(event)
  72. return id
  73. def _OnCommand(self, id, code):
  74. event = command_to_events.get(id)
  75. if event is None:
  76. self.report_error("No event associated with event ID %d" % id)
  77. return 1
  78. return self.fire(event)
  79. def _new_binding(self, event, handler_args_type):
  80. return Binding(event, handler_args_type)
  81. def _get_IDLE_handler(self, ext, handler):
  82. try:
  83. instance = self.parent_view.idle.IDLEExtension(ext)
  84. name = handler.replace("-", "_") + "_event"
  85. return getattr(instance, name)
  86. except (ImportError, AttributeError):
  87. msg = "Can not find event '%s' in IDLE extension '%s'" % (handler, ext)
  88. self.report_error(msg)
  89. return None
  90. def fire(self, event, event_param = None):
  91. # Fire the specified event. Result is native Pythonwin result
  92. # (ie, 1==pass one, 0 or None==handled)
  93. # First look up the event directly - if there, we are set.
  94. binding = self.bindings.get(event)
  95. if binding is None:
  96. # If possible, find it!
  97. # A native method name
  98. handler = getattr(self.parent_view, event + "Event", None)
  99. if handler is None:
  100. # Can't decide if I should report an error??
  101. self.report_error("The event name '%s' can not be found." % event)
  102. # Either way, just let the default handlers grab it.
  103. return 1
  104. binding = self._new_binding(handler, HANDLER_ARGS_NATIVE)
  105. # Cache it.
  106. self.bindings[event] = binding
  107. handler_args_type = binding.handler_args_type
  108. # Now actually fire it.
  109. if handler_args_type==HANDLER_ARGS_GUESS:
  110. # Can't be native, as natives are never added with "guess".
  111. # Must be extension or IDLE.
  112. if event[0]=="<":
  113. handler_args_type = HANDLER_ARGS_IDLE
  114. else:
  115. handler_args_type = HANDLER_ARGS_EXTENSION
  116. try:
  117. if handler_args_type==HANDLER_ARGS_EXTENSION:
  118. args = self.parent_view.idle, event_param
  119. else:
  120. args = (event_param,)
  121. rc = binding.handler(*args)
  122. if handler_args_type==HANDLER_ARGS_IDLE:
  123. # Convert to our return code.
  124. if rc in [None, "break"]:
  125. rc = 0
  126. else:
  127. rc = 1
  128. except:
  129. message = "Firing event '%s' failed." % event
  130. print(message)
  131. traceback.print_exc()
  132. self.report_error(message)
  133. rc = 1 # Let any default handlers have a go!
  134. return rc
  135. def fire_key_event(self, msg):
  136. key = msg[2]
  137. keyState = 0
  138. if win32api.GetKeyState(win32con.VK_CONTROL) & 0x8000:
  139. keyState = keyState | win32con.RIGHT_CTRL_PRESSED | win32con.LEFT_CTRL_PRESSED
  140. if win32api.GetKeyState(win32con.VK_SHIFT) & 0x8000:
  141. keyState = keyState | win32con.SHIFT_PRESSED
  142. if win32api.GetKeyState(win32con.VK_MENU) & 0x8000:
  143. keyState = keyState | win32con.LEFT_ALT_PRESSED | win32con.RIGHT_ALT_PRESSED
  144. keyinfo = key, keyState
  145. # Special hacks for the dead-char key on non-US keyboards.
  146. # (XXX - which do not work :-(
  147. event = self.keymap.get( keyinfo )
  148. if event is None:
  149. return 1
  150. return self.fire(event, None)