cut away 1/4th of enigma loading time by loading languages when they are used
[enigma2.git] / lib / python / Components / ScrollLabel.py
1 import skin
2 from HTMLComponent import HTMLComponent
3 from GUIComponent import GUIComponent
4 from enigma import eLabel, eWidget, eSlider, fontRenderClass, ePoint, eSize
5
6 class ScrollLabel(HTMLComponent, GUIComponent):
7         def __init__(self, text=""):
8                 GUIComponent.__init__(self)
9                 self.message = text
10                 self.instance = None
11                 self.long_text = None
12                 self.scrollbar = None
13                 self.pages = None
14                 self.total = None
15
16         def applySkin(self, desktop):
17                 ret = False
18                 if self.skinAttributes is not None:
19                         skin.applyAllAttributes(self.long_text, desktop, self.skinAttributes)
20                         widget_attribs = [ ]
21                         scrollbar_attribs = [ ]
22                         for (attrib, value) in self.skinAttributes:
23                                 if attrib.find("borderColor") != -1 or attrib.find("borderWidth") != -1:
24                                         scrollbar_attribs.append((attrib,value))
25                                 if attrib.find("transparent") != -1 or attrib.find("backgroundColor") != -1:
26                                         widget_attribs.append((attrib,value))
27                         skin.applyAllAttributes(self.instance, desktop, widget_attribs)
28                         skin.applyAllAttributes(self.scrollbar, desktop, scrollbar_attribs+widget_attribs)
29                         ret = True
30                 s = self.long_text.size()
31                 self.instance.move(self.long_text.position())
32                 lineheight=fontRenderClass.getInstance().getLineHeight( self.long_text.getFont() )
33                 lines = (int)(s.height() / lineheight)
34                 self.pageHeight = (int)(lines * lineheight)
35                 self.instance.resize(eSize(s.width(), self.pageHeight+(int)(lineheight/6)))
36                 self.scrollbar.move(ePoint(s.width()-20,0))
37                 self.scrollbar.resize(eSize(20,self.pageHeight+(int)(lineheight/6)))
38                 self.scrollbar.setOrientation(eSlider.orVertical);
39                 self.scrollbar.setRange(0,100)
40                 self.scrollbar.setBorderWidth(1)
41                 self.long_text.move(ePoint(0,0))
42                 self.long_text.resize(eSize(s.width()-30, self.pageHeight*16))
43                 self.setText(self.message)
44                 return ret
45
46         def setText(self, text):
47                 self.message = text
48                 if self.long_text is not None and self.pageHeight:
49                         self.long_text.move(ePoint(0,0))
50                         self.long_text.setText(self.message)
51                         text_height=self.long_text.calculateSize().height()
52                         total=self.pageHeight
53                         pages=1
54                         while total < text_height:
55                                 total=total+self.pageHeight
56                                 pages=pages+1
57                         if pages > 1:
58                                 self.scrollbar.show()
59                                 self.total = total
60                                 self.pages = pages
61                                 self.updateScrollbar()
62                         else:
63                                 self.scrollbar.hide()
64                                 self.total = None
65                                 self.pages = None
66
67         def appendText(self, text):
68                 old_text = self.getText()
69                 if len(str(old_text)) >0:
70                         self.message += text
71                 else:
72                         self.message = text
73                 if self.long_text is not None:
74                         self.long_text.setText(self.message)
75                         text_height=self.long_text.calculateSize().height()
76                         total=self.pageHeight
77                         pages=1
78                         while total < text_height:
79                                 total=total+self.pageHeight
80                                 pages=pages+1
81                         if pages > 1:
82                                 self.scrollbar.show()
83                                 self.total = total
84                                 self.pages = pages
85                                 self.updateScrollbar()
86                         else:
87                                 self.scrollbar.hide()
88                                 self.total = None
89                                 self.pages = None
90
91         def updateScrollbar(self):
92                 start = -self.long_text.position().y() * 100 / self.total
93                 vis = self.pageHeight * 100 / self.total;
94                 self.scrollbar.setStartEnd(start, start+vis)
95
96         def getText(self):
97                 return self.message
98
99         def GUIcreate(self, parent):
100                 self.instance = eWidget(parent)
101                 self.scrollbar = eSlider(self.instance)
102                 self.long_text = eLabel(self.instance)
103
104         def GUIdelete(self):
105                 self.long_text = None
106                 self.scrollbar = None
107                 self.instance = None
108
109         def pageUp(self):
110                 if self.total is not None:
111                         curPos = self.long_text.position()
112                         if curPos.y() < 0:
113                                 self.long_text.move( ePoint( curPos.x(), curPos.y() + self.pageHeight ) )
114                                 self.updateScrollbar()
115
116         def pageDown(self):
117                 if self.total is not None:
118                         curPos = self.long_text.position()
119                         if self.total-self.pageHeight >= abs( curPos.y() - self.pageHeight ):
120                                 self.long_text.move( ePoint( curPos.x(), curPos.y() - self.pageHeight ) )
121                                 self.updateScrollbar()
122
123         def lastPage(self):
124                 i=1
125                 while i < self.pages:
126                         self.pageDown()
127                         i += 1 
128                         self.updateScrollbar()
129
130         def produceHTML(self):
131                 return self.getText()