annotate engine/extensions/pychan/events.py @ 331:48c99636453e

Added Licence header to all pypthon modules in extension/ Commit 3k, Yay :-)
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Mon, 24 Aug 2009 12:01:34 +0000
parents 51cc05d862f2
children dfd48d49c044
rev   line source
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
1 #!/usr/bin/env python
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
2 # -*- coding: utf-8 -*-
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
3
331
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
4 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
5 # Copyright (C) 2005-2009 by the FIFE team
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
6 # http://www.fifengine.de
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
7 # This file is part of FIFE.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
8 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
9 # FIFE is free software; you can redistribute it and/or
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
10 # modify it under the terms of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
11 # License as published by the Free Software Foundation; either
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
12 # version 2.1 of the License, or (at your option) any later version.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
13 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
14 # This library is distributed in the hope that it will be useful,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
17 # Lesser General Public License for more details.
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
18 #
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
19 # You should have received a copy of the GNU Lesser General Public
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
20 # License along with this library; if not, write to the
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
21 # Free Software Foundation, Inc.,
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
23 # ####################################################################
48c99636453e Added Licence header to all pypthon modules in extension/
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 255
diff changeset
24
251
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
25 """\
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
26 PyChan event handling (internal).
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
27 =================================
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
28
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
29 Users shouldn't need to use this module directly.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
30 L{widgets.Widget.capture} and L{widgets.Widget.mapEvents} provide
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
31 a convenient API to capture events.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
32
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
33 Nevertheless to understand how its supposed to work
251
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
34 take a look at L{EventMapper} and L{EventListenerBase}
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
35
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
36 Event callbacks
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
37 ---------------
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
38
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
39 You can either write callbacks yourself or
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
40 use L{tools.callBackWithArguments} or L{tools.attrSetCallback}
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
41 to generate suitable callbacks.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
42
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
43 Here's an example callback::
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
44 def dumpEventInfo(event=0,widget=0):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
45 print widget, " received the event ", event
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
46
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
47 Note the signature - C{event} and C{widget} are keyword
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
48 arguments passed to the callback. If doesn't accept either
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
49 C{event} or C{widget} as argument, these are not passed.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
50
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
51 This way a simple function which ignores C{event} or C{widget}
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
52 can be used, while they are available if needed.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
53
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
54 Currently only one callback can be set per event. In case
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
55 you don't want to write your own callback that dispatches
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
56 to different callbacks you can use L{tools.chainCallbacks}.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
57
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
58 Available Events
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
59 ----------------
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
60
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
61 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
62
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
63 from compat import guichan
251
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
64 import widgets
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
65
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
66 import exceptions
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
67 from internal import get_manager
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
68 import tools
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
69 import traceback
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
70 import weakref
255
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 251
diff changeset
71 import fife_timer as timer
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
72
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
73 EVENTS = [
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
74 "mouseEntered",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
75 "mouseExited",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
76 "mousePressed",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
77 "mouseReleased",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
78 "mouseClicked",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
79 "mouseMoved",
220
6d09d310943b Pass mouseWheelMovedUp and mouseWheelMovedDown events to pychan.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 208
diff changeset
80 "mouseWheelMovedUp",
6d09d310943b Pass mouseWheelMovedUp and mouseWheelMovedDown events to pychan.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 208
diff changeset
81 "mouseWheelMovedDown",
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
82 "mouseDragged",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
83 "action",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
84 "keyPressed",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
85 "keyReleased",
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
86 ]
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 # Add the EVENTS to the docs.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
89 __doc__ += "".join([" - %s\n" % event for event in EVENTS])
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
90
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
91 # The line before seems to leak the variable event into the global namespace ... remove that!
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
92 # This is a python problem, addressed in python3
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
93 try: del event
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
94 except:pass
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
95
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
96 MOUSE_EVENT, KEY_EVENT, ACTION_EVENT = range(3)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
97 def getEventType(name):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
98 if "mouse" in name:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
99 return MOUSE_EVENT
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
100 if "key" in name:
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
101 return KEY_EVENT
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
102 return ACTION_EVENT
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
103
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 CALLBACK_NONE_MESSAGE = """\
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
106 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
107 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
108 """
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
109 class EventListenerBase(object):
157
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 Redirector for event callbacks.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
112 Use *only* from L{EventMapper}.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
113
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
114 This class uses the SWIG director feature - overriden
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
115 virtual methods are called from C++ to - listen to
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
116 Guichan events.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
117
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
118 """
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
119 def __init__(self):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
120 super(EventListenerBase,self).__init__()
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
121 self.events = {}
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
122 self.indent = 0
208
e281223a03a6 Rejoice. PyChan debug messages removed.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 205
diff changeset
123 self.debug = get_manager().debug
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
124 self.is_attached = False
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
125
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
126 def attach(self,widget):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
127 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
128 Start receiving events.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
129 No need to call this manually.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
130 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
131
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
132 if self.is_attached:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
133 return
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
134 if not self.events:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
135 return
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
136 if self.debug: print "Attach:",self
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
137 self.doAttach(widget.real_widget)
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
138 self.widget_ref = weakref.ref(widget)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
139 self.is_attached = True
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
140
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
141 def detach(self):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
142 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
143 Stop receiving events.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
144 No need to call this manually.
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
145 """
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
146 if not self.is_attached:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
147 return
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
148 if self.debug: print "Detach:",self
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
149 self.is_attached = False
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
150
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
151 def _redirectEvent(self,name,event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
152 self.indent += 4
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
153 try:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
154 event = self.translateEvent(getEventType(name), event)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
155 if name in self.events:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
156 if self.debug: print "-"*self.indent, name
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
157 for f in self.events[name].itervalues():
255
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 251
diff changeset
158 def delayed_f():
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 251
diff changeset
159 f( event )
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 251
diff changeset
160 timer.delayCall(0,delayed_f)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
161
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
162 except:
234
e4dfdf4c11fd Workaround for a crash on Win32 when trying to print an event.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 232
diff changeset
163 print name, repr(event)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
164 traceback.print_exc()
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
165 raise
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
166
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
167 finally:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
168 self.indent -= 4
157
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 def translateEvent(self,event_type,event):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
171 if event_type == MOUSE_EVENT:
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
172 return get_manager().hook.translate_mouse_event(event)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
173 if event_type == KEY_EVENT:
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
174 return get_manager().hook.translate_key_event(event)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
175 return event
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
176
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
177 class _ActionEventListener(EventListenerBase,guichan.ActionListener):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
178 def __init__(self):super(_ActionEventListener,self).__init__()
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
179 def doAttach(self,real_widget): real_widget.addActionListener(self)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
180 def doDetach(self,real_widget): real_widget.removeActionListener(self)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
181
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
182 def action(self,e): self._redirectEvent("action",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
183
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
184 class _MouseEventListener(EventListenerBase,guichan.MouseListener):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
185 def __init__(self):super(_MouseEventListener,self).__init__()
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
186 def doAttach(self,real_widget): real_widget.addMouseListener(self)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
187 def doDetach(self,real_widget): real_widget.removeMouseListener(self)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
188
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
189 def mouseEntered(self,e): self._redirectEvent("mouseEntered",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
190 def mouseExited(self,e): self._redirectEvent("mouseExited",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
191 def mousePressed(self,e): self._redirectEvent("mousePressed",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
192 def mouseReleased(self,e): self._redirectEvent("mouseReleased",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
193 def mouseClicked(self,e): self._redirectEvent("mouseClicked",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
194 def mouseMoved(self,e): self._redirectEvent("mouseMoved",e)
220
6d09d310943b Pass mouseWheelMovedUp and mouseWheelMovedDown events to pychan.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 208
diff changeset
195 def mouseWheelMovedUp(self,e): self._redirectEvent("mouseWheelMovedUp",e)
6d09d310943b Pass mouseWheelMovedUp and mouseWheelMovedDown events to pychan.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 208
diff changeset
196 def mouseWheelMovedDown(self,e): self._redirectEvent("mouseWheelMovedDown",e)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
197 def mouseDragged(self,e): self._redirectEvent("mouseDragged",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
198
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
199 class _KeyEventListener(EventListenerBase,guichan.KeyListener):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
200 def __init__(self):super(_KeyEventListener,self).__init__()
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
201 def doAttach(self,real_widget): real_widget.addKeyListener(self)
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
202 def doDetach(self,real_widget): real_widget.removeKeyListener(self)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
203
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
204 def keyPressed(self,e): self._redirectEvent("keyPressed",e)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
205 def keyReleased(self,e): self._redirectEvent("keyReleased",e)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
206
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
207 class EventMapper(object):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
208 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
209 Handles events and callbacks for L{widgets.Widget}
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
210 and derived classes.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
211
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
212 Every PyChan widget has an L{EventMapper} instance
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
213 as attribute B{event_mapper}.
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
214
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
215 This instance handles all necessary house-keeping.
251
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
216 Such an event mapper can be either C{attached} or
92c7ce181881 Documentation cross reference fixes.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 234
diff changeset
217 C{detached}. In its attached state an L{EventListenerBase}
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
218 is added to the Guichan widget and will redirect
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
219 the events to the callbacks.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
220
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
221 In its detached state no events are received from the
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
222 real Guichan widget.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
223
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
224 The event mapper starts in the detached state.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
225 When a new event is captured the mapper attaches itself
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
226 automatically. The widget doesn't need to handle that.
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
227 """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
228 def __init__(self,widget):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
229 super(EventMapper,self).__init__()
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
230 self.widget_ref = weakref.ref(widget)
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
231 self.callbacks = {}
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
232 self.listener = {
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
233 KEY_EVENT : _KeyEventListener(),
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
234 ACTION_EVENT : _ActionEventListener(),
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
235 MOUSE_EVENT : _MouseEventListener(),
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
236 }
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
237 self.is_attached = False
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
238 self.debug = get_manager().debug
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
239
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
240 def __repr__(self):
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
241 return "EventMapper(%s)" % repr(self.widget_ref())
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
242
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
243 def attach(self):
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
244 for listener in self.listener.values():
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
245 listener.attach()
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
246
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
247 def detach(self):
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
248 for listener in self.listener.values():
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
249 listener.detach()
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
250
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
251
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
252 def capture(self,event_name,callback,group_name):
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
253 if event_name not in EVENTS:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
254 raise exceptions.RuntimeError("Unknown eventname: " + event_name)
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
255
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
256 if callback is None:
166
81a222e7bd25 Fixes for event handling.
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 164
diff changeset
257 if self.isCaptured(event_name,group_name):
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
258 self.removeEvent(event_name,group_name)
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
259 elif self.debug:
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
260 print CALLBACK_NONE_MESSAGE % str(self.widget_ref())
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
261 return
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
262 self.addEvent(event_name,callback,group_name)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
263
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
264 def isCaptured(self,event_name,group_name="default"):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
265 return ("%s/%s" % (event_name,group_name)) in self.getCapturedEvents()
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
266
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
267 def getCapturedEvents(self):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
268 events = []
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
269 for event_type, listener in self.listener.items():
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
270 for event_name, group in listener.events.items():
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
271 for group_name in group.keys():
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
272 events.append( "%s/%s" % (event_name, group_name) )
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
273 return events
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
274
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
275 def getListener(self,event_name):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
276 return self.listener[getEventType(event_name)]
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
277
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
278 def removeEvent(self,event_name,group_name):
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
279 listener = self.getListener(event_name)
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
280 del listener.events[event_name][group_name]
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
281
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
282 if not listener.events[event_name]:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
283 del listener.events[event_name]
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
284 if not listener.events:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
285 listener.detach()
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
286
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
287 del self.callbacks[group_name][event_name]
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
288 if len(self.callbacks[group_name]) <= 0:
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
289 del self.callbacks[group_name]
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
290
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
291 def addEvent(self,event_name,callback,group_name):
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
292 if not callable(callback):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
293 raise RuntimeError("An event callback must be either a callable or None - not %s" % repr(callback))
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
294 # The closure self needs to keep a weak ref.
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
295 # Otherwise the GC has problems.
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
296 self_ref = weakref.ref(self)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
297
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
298 # Set up callback dictionary. This should fix some GC issues
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
299 if not self.callbacks.has_key(group_name):
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
300 self.callbacks[group_name] = {}
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
301
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
302 if not self.callbacks[group_name].has_key(event_name):
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
303 self.callbacks[group_name][event_name] = {}
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
304
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
305 self.callbacks[group_name][event_name] = callback
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
306
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
307 def captured_f(event):
255
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 251
diff changeset
308 if self_ref() is not None:
51cc05d862f2 Merged editor_rewrite branch to trunk.
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 251
diff changeset
309 tools.applyOnlySuitable(self_ref().callbacks[group_name][event_name],event=event,widget=self_ref().widget_ref())
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
310
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
311 listener = self.getListener(event_name)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
312
205
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
313 if event_name not in listener.events:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
314 listener.events[event_name] = {group_name : captured_f}
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
315 else:
54bfd1015b35 * PyChan event handling rework (part I)
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 188
diff changeset
316 listener.events[event_name][group_name] = captured_f
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
317 listener.attach(self.widget_ref())
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
318
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
319
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
320 def splitEventDescriptor(name):
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
321 """ Utility function to split "widgetName/eventName" descriptions into tuples. """
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
322 L = name.split("/")
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
323 if len(L) not in (1,2,3):
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
324 raise exceptions.RuntimeError("Invalid widgetname / eventname combination: " + name)
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
325 if len(L) == 1:
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
326 L = L[0],"action"
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
327 elif L[1] not in EVENTS:
157
bb9902910067 input_rework merged!
phoku@33b003aa-7bff-0310-803a-e67f0ece8222
parents:
diff changeset
328 raise exceptions.RuntimeError("Unknown event name: " + name)
164
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
329 if len(L) == 2:
5b04a7d3ded6 typo fix
spq@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 157
diff changeset
330 L = L[0],L[1],"default"
232
f10a35efebc0 Memory leak fix for pychan
cheesesucker@33b003aa-7bff-0310-803a-e67f0ece8222
parents: 220
diff changeset
331 return L