annotate engine/extensions/pychan/events.py @ 166:81a222e7bd25

Fixes for event handling.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 18 Oct 2008 06:46:21 +0000
parents 5b04a7d3ded6
children 06dddc96ce54
rev   line source
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
1 #coding: utf-8
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
2
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
4 PyChan event handling
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
5 =====================
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
6
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
7 Users shouldn't need to use this module directly.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
8 L{widgets.Widget.capture} and L{widgets.Widget.mapEvents} provide
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
9 a convenient API to capture events.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
10
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
11 Nevertheless to understand how its supposed to work
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
12 take a look at L{EventMapper} and L{EventListener}
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
13
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
14 Available Events
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
15 ----------------
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
16
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
17 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
18
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
19 import fife
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
20 import exceptions
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
21 import manager
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
22 import tools
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
23
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
24 EVENTS = [
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
25 "mouseEntered",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
26 "mouseExited",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
27 "mousePressed",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28 "mouseReleased",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29 "mouseClicked",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 "mouseMoved",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31 "mouseDragged",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32 "action",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 "keyPressed",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
34 "keyReleased",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
35 ]
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
36
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
37 # Add the EVENTS to the docs.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
38 __doc__ += "".join([" - %s\n" % event for event in EVENTS])
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
39
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
40 MOUSE_EVENT, KEY_EVENT, ACTION_EVENT = range(3)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
41 def getEventType(name):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
42 if "mouse" in name:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
43 return MOUSE_EVENT
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
44 if "key" in name:
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
45 return KEY_EVENT
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
46 return ACTION_EVENT
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
47
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
48
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
49 CALLBACK_NONE_MESSAGE = """\
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
50 You passed None as parameter to %s.capture, which would normally remove a mapped event.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
51 But there was no event mapped. Did you accidently call a function instead of passing it?
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
52 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
53
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
54 class EventListener(fife.GUIEventListener):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
55 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
56 Redirector for event callbacks.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
57 Use *only* from L{EventMapper}.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 This class uses the SWIG director feature - overriden
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60 virtual methods are called from C++ to - listen to
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61 Guichan events.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
63 When the module is first loaded the event handler
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
64 methods are auto-generated from the list L{EVENTS}.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
65 This is effectively the same code as::
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66 def mouseEntered(self,event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
67 self._redirectEvent("mouseEntered",event)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
69 This way L{EVENTS} and the actually receivable events
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
70 are forced to be in sync.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
71 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72 def __init__(self,debug=True):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 super(EventListener,self).__init__()
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 self.events = {}
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75 self.indent = 0
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76 self.debug = debug
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 self.guimanager = manager.Manager.manager.guimanager
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
79 def _redirectEvent(self,name,event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
80 self.indent += 4
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
81 event = self.translateEvent(getEventType(name), event)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
82 if name in self.events:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
83 if self.debug: print "-"*self.indent, name
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
84 for f in self.events[name].itervalues():
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
85 f( event )
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
86 self.indent -= 4
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
87
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
88 def translateEvent(self,event_type,event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
89 if event_type == MOUSE_EVENT:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
90 return self.guimanager.translateMouseEvent(event)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
91 if event_type == KEY_EVENT:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
92 return self.guimanager.translateKeyEvent(event)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
93 return event
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
94
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
95 def _redirect(name):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
96 def redirectorFunc(self,event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
97 self._redirectEvent(name,event)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
98 return redirectorFunc
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
99
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
100 for event_name in EVENTS:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
101 setattr(EventListener,event_name,_redirect(event_name))
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
102
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
103 class EventMapper(object):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
104 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
105 Handles events and callbacks for L{widgets.Widget}
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
106 and derived classes.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
107
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
108 Every PyChan widget has an L{EventMapper} instance
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
109 as attribute *event_mapper*.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
110
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
111 This instance handles all necessary house-keeping.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
112 Such an event mapper can be either *attached* or
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
113 *detached*. In its attached state an L{EventListener}
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
114 is added to the Guichan widget and will redirect
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
115 the events to the callbacks.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
116
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
117 In its detached state no events are received from the
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
118 real Guichan widget.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
119
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
120 The event mapper starts in the detached state.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
121 When a new event is captured the mapper attaches itself
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
122 automatically. The widget doesn't need to handle that.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
123 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
124 def __init__(self,widget):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
125 super(EventMapper,self).__init__()
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
126 self.widget = widget
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
127 self.listener = EventListener()
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
128 self.is_attached = False
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
129 self.debug = manager.Manager.manager.debug
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
130
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
131 def __del__(self):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
132 self.detach()
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
133 def __repr__(self):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
134 return "EventMapper(%s)" % repr(self.widget)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
135
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
136 def attach(self):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
137 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
138 Start receiving events.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
139 No need to call this manually.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
140 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
141
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
142 if self.is_attached:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
143 return
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
144 if not self.listener.events:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
145 return
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
146 if self.debug: print "Attach:",self
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
147 self.widget.real_widget.addKeyListener( self.listener )
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
148 self.widget.real_widget.addMouseListener( self.listener )
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
149 self.widget.real_widget.addActionListener( self.listener )
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
150 self.is_attached = True
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
151
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
152 def detach(self):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
153 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
154 Stop receiving events.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
155 No need to call this manually.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
156 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
158 if not self.is_attached:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
159 return
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
160 if self.debug: print "Detach:",self
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
161 self.widget.real_widget.removeKeyListener( self.listener )
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
162 self.widget.real_widget.removeMouseListener( self.listener )
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
163 self.widget.real_widget.removeActionListener( self.listener )
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
164 self.is_attached = False
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
165
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
166 def capture(self,event_name,callback,group_name):
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
167 if event_name not in EVENTS:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
168 raise exceptions.RuntimeError("Unknown eventname: " + event_name)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
169
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
170 if callback is None:
166
81a222e7bd25 Fixes for event handling.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 164
diff changeset
171 if self.isCaptured(event_name,group_name):
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
172 del self.listener.events[event_name][group_name]
166
81a222e7bd25 Fixes for event handling.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 164
diff changeset
173 if not self.listener.events[event_name]:
81a222e7bd25 Fixes for event handling.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 164
diff changeset
174 del self.listener.events[event_name]
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
175 if not self.listener.events:
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
176 self.detach()
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
177 elif self.debug:
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
178 print CALLBACK_NONE_MESSAGE % str(self.widget)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
179 return
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
180
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
181 if not callable(callback):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
182 raise RuntimeError("An event callback must be either a callable or None - not %s" % repr(callback))
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
183
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
184 def captured_f(event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
185 tools.applyOnlySuitable(callback,event=event,widget=self.widget)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
186
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
187 if event_name not in self.listener.events:
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
188 self.listener.events[event_name] = {group_name : captured_f}
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
189 else:
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
190 self.listener.events[event_name][group_name] = captured_f
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
191 self.attach()
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
192
166
81a222e7bd25 Fixes for event handling.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 164
diff changeset
193 def isCaptured(self,event_name,group_name="default"):
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
194 return event_name in self.listener.events and group_name in self.listener.events[event_name]
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
195
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
196 def getCapturedEvents(self):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
197 return self.listener.events.keys()
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
198
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
199
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
200 def splitEventDescriptor(name):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
201 """ Utility function to split "widgetName/eventName" descriptions into tuples. """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
202 L = name.split("/")
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
203 if len(L) not in (1,2,3):
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
204 raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
205 if len(L) == 1:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
206 L = L[0],"action"
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
207 elif L[1] not in EVENTS:
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
208 raise exceptions.RuntimeError("Unknown event name: " + name)
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
209 if len(L) == 2:
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
210 L = L[0],L[1],"default"
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
211 return L
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
212