nicer code
[enigma2.git] / lib / python / Screens / Screen.py
1 from Components.HTMLSkin import *
2 from Components.GUISkin import *
3 from Components.Sources.Source import Source
4
5 import sys
6
7 class Screen(dict, HTMLSkin, GUISkin):
8
9         ALLOW_SUSPEND = False
10
11         def __init__(self, session):
12                 self.skinName = self.__class__.__name__
13                 self.session = session
14                 GUISkin.__init__(self)
15                 
16                 self.onClose = [ ]
17                 self.onFirstExecBegin = [ ]
18                 self.onExecBegin = [ ]
19                 self.onShown = [ ]
20                 
21                 self.onShow = [ ]
22                 self.onHide = [ ]
23                 
24                 self.execing = False
25                 self.shown = False
26                 
27                 self.renderer = [ ]
28                 
29                 # in order to support screens *without* a help,
30                 # we need the list in every screen. how ironic.
31                 self.helpList = [ ]
32                 
33                 self.close_on_next_exec = None
34
35         def execBegin(self):
36                 self.active_components = [ ]
37                 if self.close_on_next_exec is not None:
38                         tmp = self.close_on_next_exec
39                         self.close_on_next_exec = None
40                         self.execing = True
41                         self.close(tmp)
42                 else:
43                         single = self.onFirstExecBegin
44                         self.onFirstExecBegin = []
45                         for x in self.onExecBegin + single:
46                                 x()
47                                 if self.session.current_dialog != self:
48                                         return
49
50 #                       assert self.session == None, "a screen can only exec once per time"
51 #                       self.session = session
52
53                         for val in self.values() + self.renderer:
54                                 val.execBegin()
55                                 if self.session.current_dialog != self:
56                                         return
57                                 self.active_components.append(val)
58
59                         self.execing = True
60         
61                         for x in self.onShown:
62                                 x()
63         
64         def execEnd(self):
65 #               for (name, val) in self.items():
66                 for val in self.active_components:
67                         val.execEnd()
68                 del self.active_components
69 #               assert self.session != None, "execEnd on non-execing screen!"
70 #               self.session = None
71                 self.execing = False
72         
73         # never call this directly - it will be called from the session!
74         def doClose(self):
75                 self.hide()
76                 for x in self.onClose:
77                         x()
78                 
79                 # fixup circular references
80                 del self.helpList
81                 GUISkin.close(self)
82
83                 # first disconnect all render from their sources.
84                 # we might split this out into a "unskin"-call,
85                 # but currently we destroy the screen afterwards
86                 # anyway.
87                 for val in self.renderer:
88                         val.disconnectAll()  # disconnected converter/sources and probably destroy them
89                 
90                 del self.session
91                 for (name, val) in self.items():
92                         val.destroy()
93                         del self[name]
94                 
95                 for val in self.renderer:
96                         val.destroy()
97                 
98                 self.renderer = [ ]
99                 
100                 # really delete all elements now
101                 self.__dict__.clear()
102         
103         def close(self, *retval):
104                 if not self.execing:
105                         self.close_on_next_exec = retval
106                 else:
107                         self.session.close(self, *retval)
108
109         def setFocus(self, o):
110                 self.instance.setFocus(o.instance)
111
112         def show(self):
113                 if self.shown:
114                         return
115                 self.shown = True
116                 self.instance.show()
117                 for x in self.onShow:
118                         x()
119                 for val in self.values() + self.renderer:
120                         if isinstance(val, GUIComponent) or isinstance(val, Source):
121                                 val.onShow()
122
123         def hide(self):
124                 if not self.shown:
125                         return
126                 self.shown = False
127                 self.instance.hide()
128                 for x in self.onHide:
129                         x()
130                 for val in self.values() + self.renderer:
131                         if isinstance(val, GUIComponent) or isinstance(val, Source):
132                                 val.onHide()
133
134         def __repr__(self):
135                 return str(type(self))