/usr/share/pyshared/PythonCard/components/textfield.py is in python-pythoncard 0.8.2-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | """
__version__ = "$Revision: 1.31 $"
__date__ = "$Date: 2005/03/28 17:29:39 $"
"""
import wx
from PythonCard import event, widget
# KEA 2002-04-04
# custom event posted after loseFocus when a field
# has been changed
wxEVT_CLOSE_FIELD = wx.NewEventType()
##def EVT_CLOSE_FIELD(win, id, func):
## win.Connect(id, -1, wxEVT_CLOSE_FIELD, func)
# use the new PyEventBinder in 2.5.x
EVT_CLOSE_FIELD = wx.PyEventBinder(wxEVT_CLOSE_FIELD)
class CloseFieldEvent(event.Event):
name = 'closeField'
binding = EVT_CLOSE_FIELD
id = wxEVT_CLOSE_FIELD
TextFieldEvents = (
event.KeyPressEvent,
event.KeyDownEvent,
event.KeyUpEvent,
event.TextUpdateEvent,
CloseFieldEvent
)
class TextFieldSpec(widget.WidgetSpec):
def __init__( self ) :
events = list(TextFieldEvents)
attributes = {
'text' : {'presence' : 'optional', 'default' : ''},
'editable' : {'presence' : 'optional', 'default' : 1},
'alignment' : {'presence' : 'optional', 'default' : 'left', 'values' :['left', 'right', 'center']},
'border' : {'presence' : 'optional', 'default' : '3d', 'values' : ['3d', 'none']}
}
widget.WidgetSpec.__init__( self, 'TextField', 'Widget' , events, attributes )
def getAlignment(aString):
if aString == 'left':
return wx.TE_LEFT
elif aString == 'center':
return wx.TE_CENTRE
elif aString == 'right':
return wx.TE_RIGHT
else :
raise 'invalid TextField.alignment value:', aString
class TextField(widget.Widget, wx.TextCtrl):
"""
A text field.
"""
_spec = TextFieldSpec()
def __init__(self, aParent, aResource):
self._border = aResource.border
if aResource.border == 'none':
borderStyle = wx.NO_BORDER
else:
borderStyle = 0
self._alignment = aResource.alignment
wx.TextCtrl.__init__(
self,
aParent,
widget.makeNewId(aResource.id),
aResource.text,
aResource.position,
aResource.size,
#style = wxTE_PROCESS_ENTER | borderStyle | wxCLIP_SIBLINGS,
style = borderStyle | getAlignment(aResource.alignment) | \
wx.CLIP_SIBLINGS | wx.NO_FULL_REPAINT_ON_RESIZE,
name = aResource.name)
widget.Widget.__init__(self, aParent, aResource)
if not aResource.editable:
self.SetEditable(False)
if aResource.border == 'none':
# the erase background event doesn't appear to make the control
# transparent, so further investigation is required
#EVT_ERASE_BACKGROUND(delegate, lambda evt: None)
self.SetBackgroundColour(self.GetParent().GetBackgroundColour())
#adapter = TextFieldEventBinding(self)
#adapter.bindEvents()
## self._bindEvents(event.WIDGET_EVENTS + TextFieldEvents)
# KEA 2004-09-24
# changing to CallAfter to force the gainFocus
# event to occur after the initialize event
wx.CallAfter(self._bindEvents, event.WIDGET_EVENTS + TextFieldEvents)
def _getAlignment(self):
return self._alignment
def _setAlignment(self, aString):
raise AttributeError, "alignment attribute is read-only"
def ClearSelection(self):
if self.CanCut():
# delete the current selection,
# if we can't do a Cut we shouldn't be able to delete either
# which is why i used the test above
sel = self.replaceSelection('')
else:
ins = self.GetInsertionPoint()
try:
self.replace(ins, ins + 1, '')
except:
pass
# KEA the methods for retrieving and manipulating the text
# has to be greatly expanded to match wxPython
# capabilities or more
# KEA new methods to mirror wxPython wxTextCtrl capabilities
def appendText( self, aString ) :
"""Appends the text to the end of the text widget.
After the text is appended, the insertion point will be at the end
of the text widget. If this behavior is not desired, the programmer
should use getInsertionPoint and setInsertionPoint."""
self.AppendText( aString )
def canCopy( self ) :
return self.CanCopy()
def canCut( self ) :
return self.CanCut()
def canPaste( self ) :
return self.CanPaste()
def canRedo( self ) :
return self.CanRedo()
def canUndo( self ) :
return self.CanUndo()
def clear( self ) :
self.Clear()
def copy( self ) :
self.Copy()
def cut( self ) :
self.Cut()
def discardEdits( self ) :
self.DiscardEdits()
def getInsertionPoint( self ) :
return self.GetInsertionPoint()
def getLastPosition( self ) :
return self.GetLastPosition()
def getLineLength( self, aLineNumber ) :
return self.GetLineLength( aLineNumber )
def getLineText( self, aLineNumber ) :
return self.GetLineText( aLineNumber )
def getNumberOfLines( self ) :
return self.GetNumberOfLines()
def getSelection( self ) :
return self.GetSelection()
def getNumberOfLines( self ) :
return self.GetNumberOfLines()
# KEA rename to getModified?
def isModified( self ) :
"""Returns 1 if the text has been modified, 0 otherwise."""
return self.IsModified()
# KEA support LoadFile? If so, it only makes sense for TextArea
# many of the other methods only make sense for the multiline TextArea
# not TextField and PasswordField
# KEA OnChar ties into our user code handlers and our events,
# need to think about this one some more
# KEA OnDropFiles is windows-specific, if you try and call it under *nix
# what happens? just an exception?
def paste( self ) :
self.Paste()
def positionToXY(self, aPosition):
result = self.PositionToXY(aPosition)
if len(result) == 2:
return result
else:
# workaround for wxPython 2.3.2.1
return (result[1], result[2])
def redo( self ) :
self.Redo()
def remove( self, aFrom, aTo ) :
self.Remove( aFrom, aTo )
def replace( self, aFrom, aTo, aString ) :
# KEA workaround for Replace bug, has the side effect of
# possibly changing the insertion point
#self._delegate.Replace( aFrom, aTo, aString )
i = self.GetInsertionPoint()
self.Remove( aFrom, aTo )
self.SetInsertionPoint( aFrom )
self.WriteText( aString )
self.SetInsertionPoint( i )
def replaceSelection(self, aString, select=0):
sel = self.GetSelection()
self.Remove(sel[0], sel[1])
self.WriteText(aString)
if select:
self.SetSelection(sel[0], sel[0] + len(aString))
# KEA support SaveFile?
def setInsertionPoint( self, aPosition ) :
self.SetInsertionPoint( aPosition )
def setInsertionPointEnd( self ) :
self.SetInsertionPointEnd()
def setSelection( self, aFrom, aTo ) :
self.SetSelection( aFrom, aTo )
def showPosition( self, aPosition ) :
self.ShowPosition( aPosition )
def undo( self ) :
self.Undo()
def writeText( self, aString ) :
self.WriteText( aString )
def xyToPosition( self, aX, aY ) :
return self.XYToPosition( aX, aY )
def _getBorder( self ) :
return self._border
def _setBorder( self, aString ) :
raise AttributeError, "border attribute is read-only"
getStringSelection = wx.TextCtrl.GetStringSelection
def getString(self, aFrom, aTo):
return self.GetValue()[aFrom:aTo]
# mimic wxSTC method
ClearAll = wx.TextCtrl.Clear
def _bindEvents(self, eventList):
widget.Widget._bindEvents(self, eventList)
# in order for closeField to work properly
# both gainFocus and loseFocus have to be bound to _dispatch
# regardless of whether they have handlers or not
for eventClass in [event.GainFocusEvent, event.LoseFocusEvent]:
if not self.boundEvents.get(eventClass.binding, None):
self.Bind(eventClass.binding, self._dispatch)
self.boundEvents[eventClass.binding] = eventClass.name
# calling widget.Widget._bindEvents after instead of before
# our component specific initialization would mean any log debug
# statements like this could just be in Widget_bindEvents
# however, until we convert all the components I'm not sure
# that changing the order will always work
if 0:
print "\n boundEvents:"
for name in self.boundEvents.values():
print " ", name
print "\n\n"
print "\n self.eventIdToHandler:"
for id in self.eventIdToHandler:
print " ", id, self.eventIdToHandler[id]._function
print "\n\n"
def _dispatch(self, aWxEvent):
eventType = aWxEvent.GetEventType()
# TextField specific stuff
# the question is how we either call the generic stuff above
# due to the try/except blocks this code would probably
# work in the generic event handling but that would be unclean <wink>
if eventType == wx.wxEVT_SET_FOCUS:
try:
aWxEvent.GetEventObject().DiscardEdits()
except:
pass
elif eventType == wx.wxEVT_KILL_FOCUS:
try:
aWxEvent.target = aWxEvent.GetEventObject()
# only wxTextCtrl should have IsModified
# so an exception will be thrown and the event won't be posted
# for other components, but they shouldn't be binding to these
# handlers anyway, so I'm just being overly defensive
# same with DiscardEdits() above
#modified = obj.IsModified()
if not aWxEvent.target.IsBeingDeleted() and aWxEvent.target.IsModified():
#closeFieldEvent = aWxEvent.Clone()
#closeFieldEvent.SetEventType(event.wxEVT_CLOSE_FIELD)
# should I be using wx.PyEvent() instead?
closeFieldEvent = wx.WindowCreateEvent()
closeFieldEvent.SetEventType(wxEVT_CLOSE_FIELD)
closeFieldEvent.SetEventObject(aWxEvent.target)
closeFieldEvent.SetId(aWxEvent.GetId())
closeFieldEvent.SetTimestamp(aWxEvent.GetTimestamp())
# this is what Robin suggested instead, see:
# http://aspn.activestate.com/ASPN/Mail/Message/wxPython-users/1103427
#obj.GetParent().GetEventHandler().ProcessEvent(closeFieldEvent)
# KEA 2004-04-30
# ProcessEvent will cause closeField to occur before loseFocus and
# gainFocus messages, so should we do a wxCallAfter instead?
# in the case of fields should closeField be an InsteadOfTypeEvent
# and replace the loseFocus event? probably not since they mean
# different things
aWxEvent.target.GetEventHandler().ProcessEvent(closeFieldEvent)
#wx.PostEvent(obj.GetParent(), evt)
#print 'posted closeField'
except:
pass
# rest of the dispatch is standard
widget.Widget._dispatch(self, aWxEvent)
alignment = property(_getAlignment, _setAlignment)
border = property(_getBorder, _setBorder)
editable = property(wx.TextCtrl.IsEditable, wx.TextCtrl.SetEditable)
text = property(wx.TextCtrl.GetValue, wx.TextCtrl.SetValue)
import sys
from PythonCard import registry
registry.Registry.getInstance().register(sys.modules[__name__].TextField)
|