]> git.saurik.com Git - wxWidgets.git/blobdiff - wxPython/wx/lib/masked/textctrl.py
Warning fixes, source cleaning.
[wxWidgets.git] / wxPython / wx / lib / masked / textctrl.py
index d73fbe4a4e3412fe00b992b55866c442e7143ac3..fc20e71dc19c0215c78156d5bc3edbf69f12cb3b 100644 (file)
@@ -22,7 +22,7 @@ from wx.lib.masked import *
 # be a good place to implement the 2.3 logger class
 from wx.tools.dbg import Logger
 dbg = Logger()
-##dbg(enable=0)
+##dbg(enable=1)
 
 # ## TRICKY BIT: to avoid a ton of boiler-plate, and to
 # ## automate the getter/setter generation for each valid
@@ -76,6 +76,13 @@ class BaseMaskedTextCtrl( wx.TextCtrl, MaskedEditMixin ):
                             style=style, validator=validator,
                             name=name)
 
+        self._PostInit(setupEventHandling = setupEventHandling,
+                      name=name, value=value,**kwargs )
+
+
+    def _PostInit(self,setupEventHandling=True,
+                 name='maskedTextCtrl' , value='', **kwargs):
+
         self.controlInitialized = True
         MaskedEditMixin.__init__( self, name, **kwargs )
 
@@ -190,7 +197,7 @@ class BaseMaskedTextCtrl( wx.TextCtrl, MaskedEditMixin ):
 
         # make SetValue behave the same as if you had typed the value in:
         try:
-            value = self._Paste(value, raise_on_invalid=True, just_return_value=True)
+            value, replace_to = self._Paste(value, raise_on_invalid=True, just_return_value=True)
             if self._isFloat:
                 self._isNeg = False     # (clear current assumptions)
                 value = self._adjustFloat(value)
@@ -206,18 +213,30 @@ class BaseMaskedTextCtrl( wx.TextCtrl, MaskedEditMixin ):
                 dateparts[0] = self._adjustDate(dateparts[0], fixcentury=True)
                 value = string.join(dateparts, ' ')
 ##                dbg('adjusted value: "%s"' % value)
-                value = self._Paste(value, raise_on_invalid=True, just_return_value=True)
+                value, replace_to = self._Paste(value, raise_on_invalid=True, just_return_value=True)
             else:
 ##                dbg('exception thrown', indent=0)
                 raise
 
         self._SetValue(value)   # note: to preserve similar capability, .SetValue()
                                 # does not change IsModified()
-####        dbg('queuing insertion after .SetValue', self._masklength)
-        wx.CallAfter(self._SetInsertionPoint, self._masklength)
-        wx.CallAfter(self._SetSelection, self._masklength, self._masklength)
+####        dbg('queuing insertion after .SetValue', replace_to)
+        # set selection to last char replaced by paste
+        wx.CallAfter(self._SetInsertionPoint, replace_to)
+        wx.CallAfter(self._SetSelection, replace_to, replace_to)
 ##        dbg(indent=0)
 
+    def SetFont(self, *args, **kwargs):
+        """ Set the font, then recalculate control size, if appropriate. """
+        wx.TextCtrl.SetFont(self, *args, **kwargs)
+        if self._autofit:
+##            dbg('calculated size:', self._CalcSize())            
+            self.SetClientSize(self._CalcSize())
+            width = self.GetSize().width
+            height = self.GetBestSize().height
+##            dbg('setting client size to:', (width, height))
+            self.SetBestFittingSize((width, height))
+
 
     def Clear(self):
         """ Blanks the current control value by replacing it with the default value."""
@@ -323,3 +342,29 @@ class TextCtrl( BaseMaskedTextCtrl, MaskedEditAccessorsMixin ):
     pass
 
 
+class PreMaskedTextCtrl( BaseMaskedTextCtrl, MaskedEditAccessorsMixin ):
+    """
+    This allows us to use XRC subclassing.
+    """
+    # This should really be wx.EVT_WINDOW_CREATE but it is not
+    # currently delivered for native controls on all platforms, so
+    # we'll use EVT_SIZE instead.  It should happen shortly after the
+    # control is created as the control is set to its "best" size.
+    _firstEventType = wx.EVT_SIZE
+
+    def __init__(self):
+        pre = wx.PreTextCtrl()
+        self.PostCreate(pre)
+        self.Bind(self._firstEventType, self.OnCreate)
+
+
+    def OnCreate(self, evt):
+        self.Unbind(self._firstEventType)
+        self._PostInit()
+
+i=0
+## CHANGELOG:
+## ====================
+##  Version 1.1
+##  1. Added .SetFont() method that properly resizes control
+##  2. Modified control to support construction via XRC mechanism.