comparison engine/extensions/pychan/widgets/textbox.py @ 248:a2d5e2721489

widgets.py split up.
author phoku@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 26 Mar 2009 16:20:16 +0000
parents
children 51cc05d862f2
comparison
equal deleted inserted replaced
247:040387b7167f 248:a2d5e2721489
1 # -*- coding: utf-8 -*-
2
3 from common import *
4 from widget import Widget
5
6 class TextBox(Widget):
7 """
8 An editable B{multiline} text edit widget.
9
10 New Attributes
11 ==============
12
13 - text: The text in the TextBox.
14 - filename: A write-only attribute - assigning a filename will cause the widget to load it's text from it.
15
16 Data
17 ====
18 The text can be read and set via L{distributeData} and L{collectData}.
19 """
20
21 ATTRIBUTES = Widget.ATTRIBUTES + [UnicodeAttr('text'),Attr('filename')]
22
23 def __init__(self,text=u"",filename = "", **kwargs):
24 self.real_widget = fife.TextBox()
25 self.text = text
26 self.filename = filename
27 super(TextBox,self).__init__(**kwargs)
28
29 # Prepare Data collection framework
30 self.accepts_data = True
31 self.accepts_initial_data = True # Make sense in a way ...
32 self._realSetInitialData = self._setText
33 self._realSetData = self._setText
34 self._realGetData = self._getText
35
36 def _getFileName(self): return self._filename
37 def _loadFromFile(self,filename):
38 self._filename = filename
39 if not filename: return
40 try:
41 # FIXME needs encoding detection.
42 self.text = unicode(open(filename).read(),"utf8")
43 except Exception, e:
44 self.text = str(e)
45 filename = property(_getFileName, _loadFromFile)
46
47 def resizeToContent(self,recurse=True):
48 rows = [self.real_widget.getTextRow(i) for i in range(self.real_widget.getNumberOfRows())]
49 max_w = max(map(self.real_font.getWidth,rows))
50 self.width = max_w
51 self.height = (self.real_font.getHeight() + 2) * self.real_widget.getNumberOfRows()
52
53 def _getText(self): return gui2text(self.real_widget.getText())
54 def _setText(self,text): self.real_widget.setText(text2gui(text))
55 text = property(_getText,_setText)
56
57 def _setOpaque(self,opaque): self.real_widget.setOpaque(opaque)
58 def _getOpaque(self): return self.real_widget.isOpaque()
59 opaque = property(_getOpaque,_setOpaque)