menu positioning for the plugins works now
[enigma2.git] / lib / python / Screens / Menu.py
1 from Screen import *
2 from Components.MenuList import MenuList
3 from Components.ActionMap import ActionMap
4 from Components.Header import Header
5 from Components.Button import Button
6 from Components.Label import Label
7 from Components.ProgressBar import ProgressBar
8
9 from Tools.Directories import resolveFilename, SCOPE_SKIN
10
11 from enigma import quitMainloop
12
13 import xml.dom.minidom
14 from xml.dom import EMPTY_NAMESPACE
15 from skin import elementsWithTag
16
17 from Screens.Setup import *
18
19 from Tools import XMLTools
20
21 # some screens
22 def doGlobal(screen):
23         screen["clock"] = Clock()
24
25
26 #               <item text="TV-Mode">self.setModeTV()</item>
27 #               <item text="Radio-Mode">self.setModeRadio()</item>
28 #               <item text="File-Mode">self.setModeFile()</item>
29 #               <item text="Scart">self.openDialog(ScartLoopThrough)</item>
30 #                       <item text="Sleep Timer"></item>
31
32
33 # read the menu
34 menufile = file(resolveFilename(SCOPE_SKIN, 'menu.xml'), 'r')
35 mdom = xml.dom.minidom.parseString(menufile.read())
36 menufile.close()
37
38
39 def getValbyAttr(x, attr):
40         for p in range(x.attributes.length):
41                 a = x.attributes.item(p)
42                 attrib = str(a.name)
43                 value = str(a.value)
44                 if attrib == attr:
45                         return value
46                         
47         return ""
48
49 class boundFunction:
50         def __init__(self, fnc, *args):
51                 self.fnc = fnc
52                 self.args = args
53         def __call__(self):
54                 self.fnc(*self.args)
55                 
56 class MenuUpdater:
57         def __init__(self):
58                 self.updatedMenuItems = {}
59         
60         def addMenuItem(self, id, pos, text, module, screen):
61                 if not self.updatedMenuAvailable(id):
62                         self.updatedMenuItems[id] = []
63                 self.updatedMenuItems[id].append([text, pos, module, screen])
64         
65         def delMenuItem(self, id, pos, text, module, screen):
66                 self.updatedMenuItems[id].remove([text, pos, module, screen])
67         
68         def updatedMenuAvailable(self, id):
69                 return self.updatedMenuItems.has_key(id)
70         
71         def getUpdatedMenu(self, id):
72                 return self.updatedMenuItems[id]
73         
74 menuupdater = MenuUpdater()
75                 
76 class Menu(Screen):
77         def okbuttonClick(self):
78                 print "okbuttonClick"
79                 selection = self["menu"].getCurrent()
80                 selection[1]()
81
82         def execText(self, text):
83                 exec text
84                 
85         def runScreen(self, arg):
86                 # arg[0] is the module (as string)
87                 # arg[1] is Screen inside this module 
88                 #        plus possible arguments, as 
89                 #        string (as we want to reference 
90                 #        stuff which is just imported)
91                 # FIXME. somehow
92                 print arg
93                 if arg[0] != "":
94                         exec "from " + arg[0] + " import *"
95                         
96                 self.openDialog(*eval(arg[1]))
97
98         def nothing(self):                                                                                                                                      #dummy
99                 pass
100
101         def openDialog(self, *dialog):                          # in every layer needed
102                 self.session.open(*dialog)
103
104         def openSetup(self, dialog):
105                 self.session.open(Setup, dialog)
106
107         def addMenu(self, destList, node):
108                 MenuTitle = _(getValbyAttr(node, "text"))
109                 if MenuTitle != "":                                                                                                                                     #check for title
110                         a = boundFunction(self.session.open, Menu, node, node.childNodes)
111                         #TODO add check if !empty(node.childNodes)
112                         destList.append((MenuTitle, a))
113                 
114         def addItem(self, destList, node):
115                 ItemText = _(getValbyAttr(node, "text"))
116                 if ItemText != "":                                                                                                                                      #check for name
117                         for x in node.childNodes:
118                                 if x.nodeType != xml.dom.minidom.Element.nodeType:
119                                         continue
120                                 elif x.tagName == 'screen':
121                                         module = getValbyAttr(x, "module")
122                                         screen = getValbyAttr(x, "screen")
123
124                                         if len(screen) == 0:
125                                                 screen = module
126
127                                         if module != "":
128                                                 module = "Screens." + module
129                                         
130                                         # check for arguments. they will be appended to the 
131                                         # openDialog call
132                                         args = XMLTools.mergeText(x.childNodes)
133                                         screen += ", " + args
134                                         
135                                         destList.append((ItemText, boundFunction(self.runScreen, (module, screen))))
136                                         return
137                                 elif x.tagName == 'code':
138                                         destList.append((ItemText, boundFunction(self.execText, XMLTools.mergeText(x.childNodes))))
139                                         return
140                                 elif x.tagName == 'setup':
141                                         id = getValbyAttr(x, "id")
142                                         destList.append((ItemText, boundFunction(self.openSetup, id)))
143                                         return
144                         
145                         destList.append((ItemText,self.nothing))
146
147
148         def __init__(self, session, parent, childNode):
149                 Screen.__init__(self, session)
150                 
151                 list = []
152                 menuID = ""
153
154                 menuID = -1
155                 for x in childNode:                                             #walk through the actual nodelist
156                         if x.nodeType != xml.dom.minidom.Element.nodeType:
157                             continue
158                         elif x.tagName == 'item':
159                                 self.addItem(list, x)
160                                 count += 1
161                         elif x.tagName == 'menu':
162                                 self.addMenu(list, x)
163                                 count += 1
164                         elif x.tagName == "id":
165                                 menuID = getValbyAttr(x, "val")
166                                 count = 0
167                         if menuID != -1:
168                                 if menuupdater.updatedMenuAvailable(menuID):
169                                         for x in menuupdater.getUpdatedMenu(menuID):
170                                                 if x[1] == count:
171                                                         list.append((x[0], boundFunction(self.runScreen, (x[2], x[3] + ", "))))
172                                                         count += 1
173
174
175                 self["menu"] = MenuList(list)   
176                                                         
177                 self["actions"] = ActionMap(["OkCancelActions", "MenuActions"], 
178                         {
179                                 "ok": self.okbuttonClick,
180                                 "cancel": self.close,
181                                 "menu": self.close
182                         })
183                 
184                 a = getValbyAttr(parent, "title")
185                 if a == "":                                                                                                             #if empty use name
186                         a = _(getValbyAttr(parent, "text"))
187                 self["title"] = Header(a)
188
189 class MainMenu(Menu):
190         #add file load functions for the xml-file
191         
192         def __init__(self, *x):
193                 Menu.__init__(self, *x)
194                 self.skinName = "Menu"
195
196         def openDialog(self, dialog):
197                 self.session.open(dialog)
198
199         def openSetup(self, dialog):
200                 self.session.open(Setup, dialog)
201
202         def setModeTV(self):
203                 print "set Mode to TV"
204                 pass
205
206         def setModeRadio(self):
207                 print "set Mode to Radio"
208                 pass
209
210         def setModeFile(self):
211                 print "set Mode to File"
212                 pass