trade Color and transparent skin attributes to parent widget and embedded
[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                         attribs = [ ]
21                         for (attrib, value) in self.skinAttributes:
22                                 if attrib.find("Color") != -1 or attrib.find("transparent") != -1:
23                                         attribs.append((attrib,value))
24                         skin.applyAllAttributes(self.instance, desktop, attribs)
25                         skin.applyAllAttributes(self.scrollbar, desktop, attribs)
26                         ret = True
27                 s = self.long_text.size()
28                 self.instance.move(self.long_text.position())
29                 lineheight=fontRenderClass.getInstance().getLineHeight( self.long_text.getFont() )
30                 lines = (int)(s.height() / lineheight)
31                 self.pageHeight = (int)(lines * lineheight)
32                 self.instance.resize(eSize(s.width(), self.pageHeight+(int)(lineheight/6)))
33                 self.scrollbar.move(ePoint(s.width()-20,0))
34                 self.scrollbar.resize(eSize(20,self.pageHeight+(int)(lineheight/6)))
35                 self.scrollbar.setOrientation(eSlider.orVertical);
36                 self.scrollbar.setRange(0,100)
37                 self.scrollbar.setBorderWidth(1)
38                 self.long_text.move(ePoint(0,0))
39                 self.long_text.resize(eSize(s.width()-30, self.pageHeight*16))
40                 self.setText(self.message)
41                 return ret
42
43         def setText(self, text):
44                 self.message = text
45                 if self.long_text is not None and self.pageHeight:
46                         self.long_text.move(ePoint(0,0))
47                         self.long_text.setText(self.message)
48                         text_height=self.long_text.calculateSize().height()
49                         total=self.pageHeight
50                         pages=1
51                         while total < text_height:
52                                 total=total+self.pageHeight
53                                 pages=pages+1
54                         if pages > 1:
55                                 self.scrollbar.show()
56                                 self.total = total
57                                 self.pages = pages
58                                 self.updateScrollbar()
59                         else:
60                                 self.scrollbar.hide()
61                                 self.total = None
62                                 self.pages = None
63
64         def appendText(self, text):
65                 old_text = self.getText()
66                 if len(str(old_text)) >0:
67                         self.message += text
68                 else:
69                         self.message = text
70                 if self.long_text is not None:
71                         self.long_text.setText(self.message)
72                         text_height=self.long_text.calculateSize().height()
73                         total=self.pageHeight
74                         pages=1
75                         while total < text_height:
76                                 total=total+self.pageHeight
77                                 pages=pages+1
78                         if pages > 1:
79                                 self.scrollbar.show()
80                                 self.total = total
81                                 self.pages = pages
82                                 self.updateScrollbar()
83                         else:
84                                 self.scrollbar.hide()
85                                 self.total = None
86                                 self.pages = None
87
88         def updateScrollbar(self):
89                 start = -self.long_text.position().y() * 100 / self.total
90                 vis = self.pageHeight * 100 / self.total;
91                 self.scrollbar.setStartEnd(start, start+vis)
92
93         def getText(self):
94                 return self.message
95
96         def GUIcreate(self, parent):
97                 self.instance = eWidget(parent)
98                 self.scrollbar = eSlider(self.instance)
99                 self.long_text = eLabel(self.instance)
100
101         def GUIdelete(self):
102                 self.long_text = None
103                 self.scrollbar = None
104                 self.instance = None
105
106         def pageUp(self):
107                 if self.total is not None:
108                         curPos = self.long_text.position()
109                         if curPos.y() < 0:
110                                 self.long_text.move( ePoint( curPos.x(), curPos.y() + self.pageHeight ) )
111                                 self.updateScrollbar()
112
113         def pageDown(self):
114                 if self.total is not None:
115                         curPos = self.long_text.position()
116                         if self.total-self.pageHeight >= abs( curPos.y() - self.pageHeight ):
117                                 self.long_text.move( ePoint( curPos.x(), curPos.y() - self.pageHeight ) )
118                                 self.updateScrollbar()
119
120         def lastPage(self):
121                 i=1
122                 while i < self.pages:
123                         self.pageDown()
124                         i += 1 
125                         self.updateScrollbar()
126
127         def produceHTML(self):
128                 return self.getText()