enigma2 20130831 (master) -> 20130902 (master)
[enigma2.git] / usr / lib / enigma2 / python / Screens / Screen.py
1 from Tools.Profile import profile
2
3 profile("LOAD:GUISkin")
4 from Components.GUISkin import GUISkin
5 profile("LOAD:Source")
6 from Components.Sources.Source import Source
7 profile("LOAD:GUIComponent")
8 from Components.GUIComponent import GUIComponent
9 profile("LOAD:eRCInput")
10 from enigma import eRCInput
11
12 class Screen(dict, GUISkin):
13
14         False, SUSPEND_STOPS, SUSPEND_PAUSES = range(3)
15         ALLOW_SUSPEND = False
16
17         global_screen = None
18
19         def __init__(self, session, parent = None):
20                 dict.__init__(self)
21                 self.skinName = self.__class__.__name__
22                 self.session = session
23                 self.parent = parent
24                 GUISkin.__init__(self)
25
26                 self.onClose = [ ]
27                 self.onFirstExecBegin = [ ]
28                 self.onExecBegin = [ ]
29                 self.onShown = [ ]
30
31                 self.onShow = [ ]
32                 self.onHide = [ ]
33
34                 self.execing = False
35                 
36                 self.shown = True
37                 # already shown is false until the screen is really shown (after creation)
38                 self.already_shown = False
39
40                 self.renderer = [ ]
41
42                 # in order to support screens *without* a help,
43                 # we need the list in every screen. how ironic.
44                 self.helpList = [ ]
45
46                 self.close_on_next_exec = None
47
48                 # stand alone screens (for example web screens)
49                 # don't care about having or not having focus.
50                 self.stand_alone = False
51                 self.keyboardMode = None
52
53         def saveKeyboardMode(self):
54                 self.keyboardMode = eRCInput.getInstance().getKeyboardMode()
55
56         def setKeyboardModeAscii(self):
57                 eRCInput.getInstance().setKeyboardMode(eRCInput.kmAscii)
58
59         def setKeyboardModeNone(self):
60                 eRCInput.getInstance().setKeyboardMode(eRCInput.kmNone)
61
62         def restoreKeyboardMode(self):
63                 if self.keyboardMode is not None:
64                         eRCInput.getInstance().setKeyboardMode(self.keyboardMode)
65
66         def execBegin(self):
67                 self.active_components = [ ]
68                 if self.close_on_next_exec is not None:
69                         tmp = self.close_on_next_exec
70                         self.close_on_next_exec = None
71                         self.execing = True
72                         self.close(*tmp)
73                 else:
74                         single = self.onFirstExecBegin
75                         self.onFirstExecBegin = []
76                         for x in self.onExecBegin + single:
77                                 x()
78                                 if not self.stand_alone and self.session.current_dialog != self:
79                                         return
80
81 #                       assert self.session == None, "a screen can only exec once per time"
82 #                       self.session = session
83
84                         for val in self.values() + self.renderer:
85                                 val.execBegin()
86                                 if not self.stand_alone and self.session.current_dialog != self:
87                                         return
88                                 self.active_components.append(val)
89
90                         self.execing = True
91         
92                         for x in self.onShown:
93                                 x()
94         
95         def execEnd(self):
96                 active_components = self.active_components
97 #               for (name, val) in self.items():
98                 self.active_components = None
99                 for val in active_components:
100                         val.execEnd()
101 #               assert self.session != None, "execEnd on non-execing screen!"
102 #               self.session = None
103                 self.execing = False
104         
105         # never call this directly - it will be called from the session!
106         def doClose(self):
107                 self.hide()
108                 for x in self.onClose:
109                         x()
110                 
111                 # fixup circular references
112                 del self.helpList
113                 GUISkin.close(self)
114
115                 # first disconnect all render from their sources.
116                 # we might split this out into a "unskin"-call,
117                 # but currently we destroy the screen afterwards
118                 # anyway.
119                 for val in self.renderer:
120                         val.disconnectAll()  # disconnected converter/sources and probably destroy them. Sources will not be destroyed.
121
122                 del self.session
123
124                 # we can have multiple dict entries with different names but same Element
125                 # but we dont can call destroy multiple times
126                 for name in self.keys():
127                         val = self[name]
128                         del self[name] # remove from dict
129                         if val: # is not a duplicate...
130                                 val.destroy()
131                                 for (n, v) in self.items():
132                                         if v == val: # check if it is the same Element
133                                                 self[n] = None # mark as duplicate
134
135                 self.renderer = [ ]
136
137                 # really delete all elements now
138                 self.__dict__.clear()
139         
140         def close(self, *retval):
141                 if not self.execing:
142                         self.close_on_next_exec = retval
143                 else:
144                         self.session.close(self, *retval)
145
146         def setFocus(self, o):
147                 self.instance.setFocus(o.instance)
148
149         def show(self):
150                 if (self.shown and self.already_shown) or not self.instance:
151                         return
152                 self.shown = True
153                 self.already_shown = True
154                 self.instance.show()
155                 for x in self.onShow:
156                         x()
157                 for val in self.values() + self.renderer:
158                         if isinstance(val, GUIComponent) or isinstance(val, Source):
159                                 val.onShow()
160
161         def hide(self):
162                 if not self.shown or not self.instance:
163                         return
164                 self.shown = False
165                 self.instance.hide()
166                 for x in self.onHide:
167                         x()
168                 for val in self.values() + self.renderer:
169                         if isinstance(val, GUIComponent) or isinstance(val, Source):
170                                 val.onHide()
171
172         def __repr__(self):
173                 return str(type(self))
174
175         def getRelatedScreen(self, name):
176                 if name == "session":
177                         return self.session.screen
178                 elif name == "parent":
179                         return self.parent
180                 elif name == "global":
181                         return self.global_screen
182                 else:
183                         return None