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