ScrollLabel one or two columns configurable via skin parameter, split - split to...
[openblackhole/openblackhole-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.right_text = None
13                 self.scrollbar = None
14                 self.pages = None
15                 self.total = None
16                 self.split = False
17                 self.splitchar = "|"
18                 self.column = 0
19
20         def applySkin(self, desktop, parent):
21                 ret = False
22                 if self.skinAttributes is not None:
23                         widget_attribs = [ ]
24                         scrollbar_attribs = [ ]
25                         for (attrib, value) in self.skinAttributes:
26                                 if "borderColor" in attrib or "borderWidth" in attrib:
27                                         scrollbar_attribs.append((attrib,value))
28                                 if "transparent" in attrib or "backgroundColor" in attrib:
29                                         widget_attribs.append((attrib,value))
30                                 if "split" in attrib:
31                                         self.split = int(value)
32                                         if self.split:
33                                                 self.right_text = eLabel(self.instance)
34                                 if "colposition" in attrib:
35                                         self.column = int(value)
36                                 if "dividechar" in attrib:
37                                         self.splitchar = value
38                         if self.split:
39                                 skin.applyAllAttributes(self.long_text, desktop, self.skinAttributes + [("halign", "left")], parent.scale)
40                                 skin.applyAllAttributes(self.right_text, desktop, self.skinAttributes + [("transparent", "1"), ("halign", "left" and self.column or "right")], parent.scale)
41                         else:
42                                 skin.applyAllAttributes(self.long_text, desktop, self.skinAttributes, parent.scale)
43                         skin.applyAllAttributes(self.instance, desktop, widget_attribs, parent.scale)
44                         skin.applyAllAttributes(self.scrollbar, desktop, scrollbar_attribs+widget_attribs, parent.scale)
45                         ret = True
46                 s = self.long_text.size()
47                 self.instance.move(self.long_text.position())
48                 lineheight=fontRenderClass.getInstance().getLineHeight( self.long_text.getFont() )
49                 if not lineheight:
50                         lineheight = 30 # assume a random lineheight if nothing is visible
51                 lines = (int)(s.height() / lineheight)
52                 self.pageHeight = (int)(lines * lineheight)
53                 self.instance.resize(eSize(s.width(), self.pageHeight+(int)(lineheight/6)))
54                 self.scrollbar.move(ePoint(s.width()-20,0))
55                 self.scrollbar.resize(eSize(20,self.pageHeight+(int)(lineheight/6)))
56                 self.scrollbar.setOrientation(eSlider.orVertical);
57                 self.scrollbar.setRange(0,100)
58                 self.scrollbar.setBorderWidth(1)
59                 self.long_text.move(ePoint(0,0))
60                 self.long_text.resize(eSize(s.width()-30, self.pageHeight*16))
61                 if self.split:
62                         self.right_text.move(ePoint(self.column,0))
63                         self.right_text.resize(eSize(s.width()-self.column-30, self.pageHeight*16))
64                 self.setText(self.message)
65                 return ret
66
67         def setText(self, text):
68                 self.message = text
69                 if self.long_text is not None and self.pageHeight:
70                         self.long_text.move(ePoint(0,0))
71                         if self.split:
72                                 left = []
73                                 right = []
74                                 for line in self.message.split("\n"):
75                                         line = line.split(self.splitchar,1)
76                                         if len(line) == 1:
77                                                 line.append("")
78                                         left.append(line[0])
79                                         right.append(line[1].lstrip(' '))
80                                 self.long_text.setText("\n".join(left))
81                                 self.right_text.setText("\n".join(right))
82                         else:
83                                 self.long_text.setText(self.message)
84                         text_height=self.long_text.calculateSize().height()
85                         total=self.pageHeight
86                         pages=1
87                         while total < text_height:
88                                 total=total+self.pageHeight
89                                 pages=pages+1
90                         if pages > 1:
91                                 self.scrollbar.show()
92                                 self.total = total
93                                 self.pages = pages
94                                 self.updateScrollbar()
95                         else:
96                                 self.scrollbar.hide()
97                                 self.total = None
98                                 self.pages = None
99
100         def appendText(self, text):
101                 old_text = self.getText()
102                 if len(str(old_text)) >0:
103                         self.message += text
104                 else:
105                         self.message = text
106                 if self.long_text is not None:
107                         self.long_text.setText(self.message)
108                         text_height=self.long_text.calculateSize().height()
109                         total=self.pageHeight
110                         pages=1
111                         while total < text_height:
112                                 total=total+self.pageHeight
113                                 pages=pages+1
114                         if pages > 1:
115                                 self.scrollbar.show()
116                                 self.total = total
117                                 self.pages = pages
118                                 self.updateScrollbar()
119                         else:
120                                 self.scrollbar.hide()
121                                 self.total = None
122                                 self.pages = None
123
124         def updateScrollbar(self):
125                 start = -self.long_text.position().y() * 100 / self.total
126                 vis = self.pageHeight * 100 / self.total;
127                 self.scrollbar.setStartEnd(start, start+vis)
128
129         def getText(self):
130                 return self.message
131
132         def GUIcreate(self, parent):
133                 self.instance = eWidget(parent)
134                 self.scrollbar = eSlider(self.instance)
135                 self.long_text = eLabel(self.instance)
136
137         def GUIdelete(self):
138                 self.long_text = None
139                 self.scrollbar = None
140                 self.instance = None
141                 self.right_text = None
142
143         def pageUp(self):
144                 if self.total is not None:
145                         curPos = self.long_text.position()
146                         if curPos.y() < 0:
147                                 self.long_text.move( ePoint( curPos.x(), curPos.y() + self.pageHeight ) )
148                                 self.split and self.right_text.move( ePoint( curPos.x(), curPos.y() + self.pageHeight ) )
149                                 self.updateScrollbar()
150
151         def pageDown(self):
152                 if self.total is not None:
153                         curPos = self.long_text.position()
154                         if self.total-self.pageHeight >= abs( curPos.y() - self.pageHeight ):
155                                 self.long_text.move( ePoint( curPos.x(), curPos.y() - self.pageHeight ) )
156                                 self.split and self.right_text.move( ePoint( curPos.x(), curPos.y() - self.pageHeight ) )
157                                 self.updateScrollbar()
158
159         def lastPage(self):
160                 if self.pages is not None:
161                         i = 1
162                         while i < self.pages:
163                                 self.pageDown()
164                                 i += 1
165
166         def isAtLastPage(self):
167                 if self.total is not None:
168                         curPos = self.long_text.position()
169                         return self.total - self.pageHeight < abs( curPos.y() - self.pageHeight )
170                 else:
171                         return True
172
173         def produceHTML(self):
174                 return self.getText()