fix service/movie remove (hold cursor position when possible)
[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 updateScrollbar(self):
54                 start = -self.long_text.position().y() * 100 / self.total
55                 vis = self.pageHeight * 100 / self.total;
56                 self.scrollbar.setStartEnd(start, start+vis)
57
58         def getText(self):
59                 return self.message
60
61         def GUIcreate(self, parent):
62                 self.instance = eWidget(parent)
63                 self.scrollbar = eSlider(self.instance)
64                 self.long_text = eLabel(self.instance)
65
66         def GUIdelete(self):
67                 self.long_text = None
68                 self.scrollbar = None
69                 self.instance = None
70
71         def pageUp(self):
72                 if self.total is not None:
73                         curPos = self.long_text.position()
74                         if curPos.y() < 0:
75                                 self.long_text.move( ePoint( curPos.x(), curPos.y() + self.pageHeight ) )
76                                 self.updateScrollbar()
77
78         def pageDown(self):
79                 if self.total is not None:
80                         curPos = self.long_text.position()
81                         if self.total-self.pageHeight >= abs( curPos.y() - self.pageHeight ):
82                                 self.long_text.move( ePoint( curPos.x(), curPos.y() - self.pageHeight ) )
83                                 self.updateScrollbar()
84
85         def produceHTML(self):
86                 return self.getText()