Package fife :: Package extensions :: Package pychan :: Package widgets' :: Module textbox
[hide private]
[frames] | no frames]

Source Code for Module fife.extensions.pychan.widgets'.textbox

  1  # -*- coding: utf-8 -*- 
  2   
  3  # #################################################################### 
  4  #  Copyright (C) 2005-2011 by the FIFE team 
  5  #  http://www.fifengine.net 
  6  #  This file is part of FIFE. 
  7  # 
  8  #  FIFE is free software; you can redistribute it and/or 
  9  #  modify it under the terms of the GNU Lesser General Public 
 10  #  License as published by the Free Software Foundation; either 
 11  #  version 2.1 of the License, or (at your option) any later version. 
 12  # 
 13  #  This library is distributed in the hope that it will be useful, 
 14  #  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 15  #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 16  #  Lesser General Public License for more details. 
 17  # 
 18  #  You should have received a copy of the GNU Lesser General Public 
 19  #  License along with this library; if not, write to the 
 20  #  Free Software Foundation, Inc., 
 21  #  51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA 
 22  # #################################################################### 
 23   
 24  from common import * 
 25  from widget import Widget 
 26   
27 -class TextBox(Widget):
28 """ 29 An editable B{multiline} text edit widget. 30 31 New Attributes 32 ============== 33 34 - text: The text in the TextBox. 35 - filename: A write-only attribute - assigning a filename will cause the widget to load it's text from it. 36 37 Data 38 ==== 39 The text can be read and set via L{distributeData} and L{collectData}. 40 """ 41 42 ATTRIBUTES = Widget.ATTRIBUTES + [ UnicodeAttr('text'), 43 Attr('filename') 44 ] 45 DEFAULT_HEXPAND = 1 46 DEFAULT_VEXPAND = 1 47 DEFAULT_TEXT = u"" 48 DEFAULT_FILENAME = "" 49
50 - def __init__(self, 51 parent = None, 52 name = None, 53 size = None, 54 min_size = None, 55 max_size = None, 56 helptext = None, 57 position = None, 58 style = None, 59 hexpand = None, 60 vexpand = None, 61 font = None, 62 base_color = None, 63 background_color = None, 64 foreground_color = None, 65 selection_color = None, 66 border_size = None, 67 position_technique = None, 68 is_focusable = None, 69 comment = None, 70 margins = None, 71 text = None, 72 filename = None):
73 74 self.real_widget = fife.TextBox() 75 self.text = text or self.DEFAULT_TEXT 76 self.filename = filename or self.DEFAULT_FILENAME 77 super(TextBox,self).__init__(parent=parent, 78 name=name, 79 size=size, 80 min_size=min_size, 81 max_size=max_size, 82 helptext=helptext, 83 position=position, 84 style=style, 85 hexpand=hexpand, 86 vexpand=vexpand, 87 font=font, 88 base_color=base_color, 89 background_color=background_color, 90 foreground_color=foreground_color, 91 selection_color=selection_color, 92 border_size=border_size, 93 position_technique=position_technique, 94 is_focusable=is_focusable, 95 comment=comment) 96 97 # Prepare Data collection framework 98 self.accepts_data = True 99 self.accepts_initial_data = True # Make sense in a way ... 100 self._realSetInitialData = self._setText 101 self._realSetData = self._setText 102 self._realGetData = self._getText
103
104 - def clone(self, prefix):
105 textboxClone = TextBox(None, 106 self._createNameWithPrefix(prefix), 107 self.size, 108 self.min_size, 109 self.max_size, 110 self.helptext, 111 self.position, 112 self.style, 113 self.hexpand, 114 self.vexpand, 115 self.font, 116 self.base_color, 117 self.background_color, 118 self.foreground_color, 119 self.selection_color, 120 self.border_size, 121 self.position_technique, 122 self.is_focusable, 123 self.comment, 124 self.margins, 125 self.text, 126 self.filename) 127 return textboxClone
128 129
130 - def _getFileName(self): return self._filename
131 - def _loadFromFile(self,filename):
132 self._filename = filename 133 if not filename: return 134 try: 135 # FIXME needs encoding detection. 136 self.text = unicode(open(filename).read(),"utf8") 137 except Exception, e: 138 self.text = str(e)
139 filename = property(_getFileName, _loadFromFile) 140
141 - def resizeToContent(self,recurse=True):
142 rows = [self.real_widget.getTextRow(i) for i in range(self.real_widget.getNumberOfRows())] 143 max_w = max(map(self.real_font.getWidth,rows)) 144 self.width = max_w 145 self.height = (self.real_font.getHeight() + 2) * self.real_widget.getNumberOfRows()
146
147 - def _getText(self): return gui2text(self.real_widget.getText())
148 - def _setText(self,text): self.real_widget.setText(text2gui(text))
149 text = property(_getText,_setText) 150
151 - def _setOpaque(self,opaque): self.real_widget.setOpaque(opaque)
152 - def _getOpaque(self): return self.real_widget.isOpaque()
153 opaque = property(_getOpaque,_setOpaque)
154