config rewrite. some extensions still need to be updated.
[enigma2.git] / lib / python / Components / ActionMap.py
1 from enigma import *
2
3 class ActionMap:
4         def __init__(self, contexts = [ ], actions = { }, prio=0):
5                 self.actions = actions
6                 self.contexts = contexts
7                 self.prio = prio
8                 self.p = eActionMapPtr()
9                 self.bound = False
10                 self.exec_active = False
11                 self.enabled = True
12                 eActionMap.getInstance(self.p)
13         
14         def setEnabled(self, enabled):
15                 self.enabled = enabled
16                 self.checkBind()
17
18         def doBind(self):
19                 if not self.bound:
20                         for ctx in self.contexts:
21                                 self.p.bindAction(ctx, self.prio, self.action)
22                         self.bound = True
23
24         def doUnbind(self):
25                 if self.bound:
26                         for ctx in self.contexts:
27                                 self.p.unbindAction(ctx, self.action)
28                         self.bound = False
29
30         def checkBind(self):
31                 if self.exec_active and self.enabled:
32                         self.doBind()
33                 else:
34                         self.doUnbind()
35
36         def execBegin(self):
37                 self.exec_active = True
38                 self.checkBind()
39
40         def execEnd(self):
41                 self.exec_active = False
42                 self.checkBind()
43
44         def action(self, context, action):
45                 print " ".join(("action -> ", context, action))
46                 if self.actions.has_key(action):
47                         res = self.actions[action]()
48                         if res is not None:
49                                 return res
50                         return 1
51                 else:
52                         print "unknown action %s/%s! typo in keymap?" % (context, action)
53                         return 0
54
55         def destroy(self):
56                 pass
57
58 class NumberActionMap(ActionMap):
59         def action(self, contexts, action):
60                 numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
61                 if (action in numbers and self.actions.has_key(action)):
62                         res = self.actions[action](int(action))
63                         if res is not None:
64                                 return res
65                         return 1
66                 else:
67                         return ActionMap.action(self, contexts, action)
68
69 class HelpableActionMap(ActionMap):
70         """An Actionmap which automatically puts the actions into the helpList.
71
72         Note that you can only use ONE context here!"""
73         
74         # sorry for this complicated code.
75         # it's not more than converting a "documented" actionmap 
76         # (where the values are possibly (function, help)-tuples)
77         # into a "classic" actionmap, where values are just functions.
78         # the classic actionmap is then passed to the ActionMap constructor,
79         # the collected helpstrings (with correct context, action) is
80         # added to the screen's "helpList", which will be picked up by
81         # the "HelpableScreen".
82         def __init__(self, parent, context, actions = { }, prio=0):
83                 alist = [ ]
84                 adict = { }
85                 for (action, funchelp) in actions.iteritems():
86                         # check if this is a tuple
87                         if type(funchelp) is type(()):
88                                 alist.append((action, funchelp[1]))
89                                 adict[action] = funchelp[0]
90                         else:
91                                 adict[action] = funchelp
92
93                 ActionMap.__init__(self, [context], adict, prio)
94
95                 parent.helpList.append((self, context, alist))