make switching of keyboard modes more consistent
[enigma2.git] / lib / 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
52                 self.onShow.append(self.saveKeyboardMode);
53                 self.onHide.append(self.restoreKeyboardMode)
54                 self.keyboardMode = None
55
56         def saveKeyboardMode(self):
57                 rcinput = eRCInput.getInstance()
58                 self.keyboardMode = rcinput.getKeyboardMode()
59
60         def setKeyboardModeAscii(self):
61                 rcinput = eRCInput.getInstance()
62                 rcinput.setKeyboardMode(rcinput.kmAscii)
63
64         def setKeyboardModeNone(self):
65                 rcinput = eRCInput.getInstance()
66                 rcinput.setKeyboardMode(rcinput.kmNone)
67
68         def restoreKeyboardMode(self):
69                 rcinput = eRCInput.getInstance()
70                 if self.keyboardMode is not None:
71                         rcinput.setKeyboardMode(self.keyboardMode)
72
73         def execBegin(self):
74                 self.active_components = [ ]
75                 if self.close_on_next_exec is not None:
76                         tmp = self.close_on_next_exec
77                         self.close_on_next_exec = None
78                         self.execing = True
79                         self.close(*tmp)
80                 else:
81                         single = self.onFirstExecBegin
82                         self.onFirstExecBegin = []
83                         for x in self.onExecBegin + single:
84                                 x()
85                                 if not self.stand_alone and self.session.current_dialog != self:
86                                         return
87
88 #                       assert self.session == None, "a screen can only exec once per time"
89 #                       self.session = session
90
91                         for val in self.values() + self.renderer:
92                                 val.execBegin()
93                                 if not self.stand_alone and self.session.current_dialog != self:
94                                         return
95                                 self.active_components.append(val)
96
97                         self.execing = True
98         
99                         for x in self.onShown:
100                                 x()
101         
102         def execEnd(self):
103                 active_components = self.active_components
104 #               for (name, val) in self.items():
105                 self.active_components = None
106                 for val in active_components:
107                         val.execEnd()
108 #               assert self.session != None, "execEnd on non-execing screen!"
109 #               self.session = None
110                 self.execing = False
111         
112         # never call this directly - it will be called from the session!
113         def doClose(self):
114                 self.hide()
115                 for x in self.onClose:
116                         x()
117                 
118                 # fixup circular references
119                 del self.helpList
120                 GUISkin.close(self)
121
122                 # first disconnect all render from their sources.
123                 # we might split this out into a "unskin"-call,
124                 # but currently we destroy the screen afterwards
125                 # anyway.
126                 for val in self.renderer:
127                         val.disconnectAll()  # disconnected converter/sources and probably destroy them. Sources will not be destroyed.
128
129                 del self.session
130                 for (name, val) in self.items():
131                         val.destroy()
132                         del self[name]
133
134                 self.renderer = [ ]
135
136                 # really delete all elements now
137                 self.__dict__.clear()
138         
139         def close(self, *retval):
140                 if not self.execing:
141                         self.close_on_next_exec = retval
142                 else:
143                         self.session.close(self, *retval)
144
145         def setFocus(self, o):
146                 self.instance.setFocus(o.instance)
147
148         def show(self):
149                 if (self.shown and self.already_shown) or not self.instance:
150                         return
151                 self.shown = True
152                 self.already_shown = True
153                 self.instance.show()
154                 for x in self.onShow:
155                         x()
156                 for val in self.values() + self.renderer:
157                         if isinstance(val, GUIComponent) or isinstance(val, Source):
158                                 val.onShow()
159
160         def hide(self):
161                 if not self.shown or not self.instance:
162                         return
163                 self.shown = False
164                 self.instance.hide()
165                 for x in self.onHide:
166                         x()
167                 for val in self.values() + self.renderer:
168                         if isinstance(val, GUIComponent) or isinstance(val, Source):
169                                 val.onHide()
170
171         def __repr__(self):
172                 return str(type(self))
173
174         def getRelatedScreen(self, name):
175                 if name == "session":
176                         return self.session.screen
177                 elif name == "parent":
178                         return self.parent
179                 elif name == "global":
180                         return self.global_screen
181                 else:
182                         return None