1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from muntjac.event import action
17
18 from muntjac.terminal.key_mapper import KeyMapper
19 from muntjac.event.shortcut_action import ShortcutAction
20
21
23 """Notes:
24
25 Empties the keymapper for each repaint to avoid leaks; can cause problems
26 in the future if the client assumes key don't change. (if lazyloading, one
27 must not cache results)
28 """
29
31
32 self.ownActions = None
33
34
35 self.actionHandlers = None
36
37
38 self.actionMapper = None
39
40 self._clientHasActions = False
41
42 self.viewer = viewer
43
44
48
49
63
64
71
72
77
78
80 if actionHandler == self:
81
82 return
83
84 if actionHandler is not None:
85 if self.actionHandlers is None:
86 self.actionHandlers = set()
87
88 if actionHandler not in self.actionHandlers:
89 self.actionHandlers.add(actionHandler)
90 self.requestRepaint()
91
92
94 if (self.actionHandlers is not None
95 and actionHandler in self.actionHandlers):
96 if self.actionHandlers.remove(actionHandler):
97 self.requestRepaint()
98
99 if len(self.actionHandlers) == 0:
100 self.actionHandlers = None
101
102
104 if self.actionHandlers is not None:
105 self.actionHandlers = None
106 self.requestRepaint()
107
108
109
111
112 self.actionMapper = None
113
114 actions = set()
115 if self.actionHandlers is not None:
116 for handler in self.actionHandlers:
117 ac = handler.getActions(actionTarget, self.viewer)
118 if ac is not None:
119 for a in ac:
120 actions.add(a)
121
122 if self.ownActions is not None:
123 actions = actions.union(self.ownActions)
124
125
126
127
128 if (len(actions) > 0) or self._clientHasActions:
129 self.actionMapper = KeyMapper()
130
131 paintTarget.addVariable(self.viewer, "action", "")
132 paintTarget.startTag("actions")
133
134 for a in actions:
135 paintTarget.startTag("action")
136 akey = self.actionMapper.key(a)
137 paintTarget.addAttribute("key", akey);
138 if a.getCaption() is not None:
139 paintTarget.addAttribute("caption", a.getCaption())
140
141 if a.getIcon() is not None:
142 paintTarget.addAttribute("icon", a.getIcon())
143
144 if isinstance(a, ShortcutAction):
145 sa = a
146 paintTarget.addAttribute("kc", sa.getKeyCode())
147 modifiers = sa.getModifiers()
148 if modifiers is not None:
149 smodifiers = [None] * len(modifiers)
150 for i in range(len(modifiers)):
151 smodifiers[i] = str(modifiers[i])
152
153 paintTarget.addAttribute("mk", smodifiers)
154
155 paintTarget.endTag("action")
156
157 paintTarget.endTag("actions")
158
159
160
161
162 self._clientHasActions = len(actions) > 0
163
164
166 if 'action' in variables and self.actionMapper is not None:
167 key = variables.get('action')
168 a = self.actionMapper.get(key)
169 target = variables.get('actiontarget')
170 if a is not None:
171 self.handleAction(a, sender, target)
172
173
175 actions = set()
176 if self.ownActions is not None:
177 for a in self.ownActions:
178 actions.add(a)
179
180 if self.actionHandlers is not None:
181 for h in self.actionHandlers:
182 as_ = h.getActions(target, sender)
183 if as_ is not None:
184 for a in as_:
185 actions.add(a)
186
187 return list(actions)
188
189
191 if self.actionHandlers is not None:
192 arry = list(self.actionHandlers)
193 for handler in arry:
194 handler.handleAction(a, sender, target)
195
196 if ((self.ownActions is not None)
197 and (a in self.ownActions)
198 and isinstance(a, action.IListener)):
199 a.handleAction(sender, target)
200