RemainingToText.py: more flexible use
[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, parent):
17                 ret = False
18                 if self.skinAttributes is not None:
19                         skin.applyAllAttributes(self.long_text, desktop, self.skinAttributes, parent.scale)
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, parent.scale)
28                         skin.applyAllAttributes(self.scrollbar, desktop, scrollbar_attribs+widget_attribs, parent.scale)
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                 if not lineheight:
34                         lineheight = 30 # assume a random lineheight if nothing is visible
35                 lines = (int)(s.height() / lineheight)
36                 self.pageHeight = (int)(lines * lineheight)
37                 self.instance.resize(eSize(s.width(), self.pageHeight+(int)(lineheight/6)))
38                 self.scrollbar.move(ePoint(s.width()-20,0))
39                 self.scrollbar.resize(eSize(20,self.pageHeight+(int)(lineheight/6)))
40                 self.scrollbar.setOrientation(eSlider.orVertical);
41                 self.scrollbar.setRange(0,100)
42                 self.scrollbar.setBorderWidth(1)
43                 self.long_text.move(ePoint(0,0))
44                 self.long_text.resize(eSize(s.width()-30, self.pageHeight*16))
45                 self.setText(self.message)
46                 return ret
47
48         def setText(self, text):
49                 self.message = text
50                 if self.long_text is not None and self.pageHeight:
51                         self.long_text.move(ePoint(0,0))
52                         self.long_text.setText(self.message)
53                         text_height=self.long_text.calculateSize().height()
54                         total=self.pageHeight
55                         pages=1
56                         while total < text_height:
57                                 total=total+self.pageHeight
58                                 pages=pages+1
59                         if pages > 1:
60                                 self.scrollbar.show()
61                                 self.total = total
62                                 self.pages = pages
63                                 self.updateScrollbar()
64                         else:
65                                 self.scrollbar.hide()
66                                 self.total = None
67                                 self.pages = None
68
69         def appendText(self, text):
70                 old_text = self.getText()
71                 if len(str(old_text)) >0:
72                         self.message += text
73                 else:
74                         self.message = text
75                 if self.long_text is not None:
76                         self.long_text.setText(self.message)
77                         text_height=self.long_text.calculateSize().height()
78                         total=self.pageHeight
79                         pages=1
80                         while total < text_height:
81                                 total=total+self.pageHeight
82                                 pages=pages+1
83                         if pages > 1:
84                                 self.scrollbar.show()
85                                 self.total = total
86                                 self.pages = pages
87                                 self.updateScrollbar()
88                         else:
89                                 self.scrollbar.hide()
90                                 self.total = None
91                                 self.pages = None
92
93         def updateScrollbar(self):
94                 start = -self.long_text.position().y() * 100 / self.total
95                 vis = self.pageHeight * 100 / self.total;
96                 self.scrollbar.setStartEnd(start, start+vis)
97
98         def getText(self):
99                 return self.message
100
101         def GUIcreate(self, parent):
102                 self.instance = eWidget(parent)
103                 self.scrollbar = eSlider(self.instance)
104                 self.long_text = eLabel(self.instance)
105
106         def GUIdelete(self):
107                 self.long_text = None
108                 self.scrollbar = None
109                 self.instance = None
110
111         def pageUp(self):
112                 if self.total is not None:
113                         curPos = self.long_text.position()
114                         if curPos.y() < 0:
115                                 self.long_text.move( ePoint( curPos.x(), curPos.y() + self.pageHeight ) )
116                                 self.updateScrollbar()
117
118         def pageDown(self):
119                 if self.total is not None:
120                         curPos = self.long_text.position()
121                         if self.total-self.pageHeight >= abs( curPos.y() - self.pageHeight ):
122                                 self.long_text.move( ePoint( curPos.x(), curPos.y() - self.pageHeight ) )
123                                 self.updateScrollbar()
124
125         def lastPage(self):
126                 i=1
127                 while i < self.pages:
128                         self.pageDown()
129                         i += 1 
130                         self.updateScrollbar()
131
132         def produceHTML(self):
133                 return self.getText()