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