#----------------------------------------------------------------------------
-# Name: wxTimeCtrl.py
+# Name: timectrl.py
# Author: Will Sadkin
# Created: 09/19/2002
# Copyright: (c) 2002 by Will Sadkin, 2002
# component of that control is inaccessible through the interface exposed in
# wxPython.
#
-# wxTimeCtrl does not use validators, because it does careful manipulation
+# TimeCtrl does not use validators, because it does careful manipulation
# of the cursor in the text window on each keystroke, and validation is
# cursor-position specific, so the control intercepts the key codes before the
# validator would fire.
#
-# wxTimeCtrl now also supports .SetValue() with either strings or wxDateTime
+# TimeCtrl now also supports .SetValue() with either strings or wxDateTime
# values, as well as range limits, with the option of either enforcing them
# or simply coloring the text of the control if the limits are exceeded.
#
# or be counted twice (1 day each per year, for DST adjustments), the date
# portion of all wxDateTimes used/returned have their date portion set to
# Jan 1, 1970 (the "epoch.")
+#----------------------------------------------------------------------------
+# 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o Updated for V2.5 compatability
+# o wx.SpinCtl has some issues that cause the control to
+# lock up. Noted in other places using it too, it's not this module
+# that's at fault.
+#
+# 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
+#
+# o wxMaskedTextCtrl -> MaskedTextCtrl
+# o wxTimeCtrl -> TimeCtrl
#
"""<html><body>
<P>
-<B>wxTimeCtrl</B> provides a multi-cell control that allows manipulation of a time
+<B>TimeCtrl</B> provides a multi-cell control that allows manipulation of a time
value. It supports 12 or 24 hour format, and you can use wxDateTime or mxDateTime
to get/set values from the control.
<P>
-Left/right/tab keys to switch cells within a wxTimeCtrl, and the up/down arrows act
-like a spin control. wxTimeCtrl also allows for an actual spin button to be attached
+Left/right/tab keys to switch cells within a TimeCtrl, and the up/down arrows act
+like a spin control. TimeCtrl also allows for an actual spin button to be attached
to the control, so that it acts like the up/down arrow keys.
<P>
The <B>!</B> or <B>c</B> key sets the value of the control to the current time.
<P>
-Here's the API for wxTimeCtrl:
+Here's the API for TimeCtrl:
<DL><PRE>
- <B>wxTimeCtrl</B>(
+ <B>TimeCtrl</B>(
parent, id = -1,
<B>value</B> = '12:00:00 AM',
- pos = wxDefaultPosition,
- size = wxDefaultSize,
+ pos = wx.DefaultPosition,
+ size = wx.DefaultSize,
<B>style</B> = wxTE_PROCESS_TAB,
- <B>validator</B> = wxDefaultValidator,
+ <B>validator</B> = wx.DefaultValidator,
name = "time",
+ <B>format</B> = 'HHMMSS',
<B>fmt24hr</B> = False,
+ <B>displaySeconds</B> = True,
<B>spinButton</B> = None,
<B>min</B> = None,
<B>max</B> = None,
with SetValue() after instantiation of the control.)
<DL><B>size</B>
<DD>The size of the control will be automatically adjusted for 12/24 hour format
- if wxDefaultSize is specified.
+ if wx.DefaultSize is specified.
<DT><B>style</B>
- <DD>By default, wxTimeCtrl will process TAB events, by allowing tab to the
+ <DD>By default, TimeCtrl will process TAB events, by allowing tab to the
different cells within the control.
<DT><B>validator</B>
- <DD>By default, wxTimeCtrl just uses the default (empty) validator, as all
+ <DD>By default, TimeCtrl just uses the default (empty) validator, as all
of its validation for entry control is handled internally. However, a validator
can be supplied to provide data transfer capability to the control.
<BR>
+ <DT><B>format</B>
+ <DD>This parameter can be used instead of the fmt24hr and displaySeconds
+ parameters, respectively; it provides a shorthand way to specify the time
+ format you want. Accepted values are 'HHMMSS', 'HHMM', '24HHMMSS', and
+ '24HHMM'. If the format is specified, the other two arguments will be ignored.
+ <BR>
<DT><B>fmt24hr</B>
<DD>If True, control will display time in 24 hour time format; if False, it will
use 12 hour AM/PM format. SetValue() will adjust values accordingly for the
- control, based on the format specified.
+ control, based on the format specified. (This value is ignored if the <i>format</i>
+ parameter is specified.)
<BR>
+ <DT><B>displaySeconds</B>
+ <DD>If True, control will include a seconds field; if False, it will
+ just show hours and minutes. (This value is ignored if the <i>format</i>
+ parameter is specified.)
+ <BR>
<DT><B>spinButton</B>
<DD>If specified, this button's events will be bound to the behavior of the
- wxTimeCtrl, working like up/down cursor key events. (See BindSpinButton.)
+ TimeCtrl, working like up/down cursor key events. (See BindSpinButton.)
<BR>
<DT><B>min</B>
<DD>Defines the lower bound for "valid" selections in the control.
- By default, wxTimeCtrl doesn't have bounds. You must set both upper and lower
+ By default, TimeCtrl doesn't have bounds. You must set both upper and lower
bounds to make the control pay attention to them, (as only one bound makes no sense
with times.) "Valid" times will fall between the min and max "pie wedge" of the
clock.
compatibility with previous release.)
<BR>
<BR>
-<DT><B>BindSpinButton(wxSpinBtton)</B>
+<DT><B>BindSpinButton(SpinBtton)</B>
<DD>Binds an externally created spin button to the control, so that up/down spin
events change the active cell or selection in the control (in addition to the
up/down cursor keys.) (This is primarily to allow you to create a "standard"
</body></html>
"""
-import string, copy, types
-from wxPython.wx import *
-from wxPython.tools.dbg import Logger
-from wxPython.lib.maskededit import wxMaskedTextCtrl, Field
+import copy
+import string
+import types
+
+import wx
+
+from wx.tools.dbg import Logger
+from wx.lib.maskededit import BaseMaskedTextCtrl, Field
dbg = Logger()
dbg(enable=0)
accept_mx = False
# This class of event fires whenever the value of the time changes in the control:
-wxEVT_TIMEVAL_UPDATED = wxNewId()
-def EVT_TIMEUPDATE(win, id, func):
- """Used to trap events indicating that the current time has been changed."""
- win.Connect(id, -1, wxEVT_TIMEVAL_UPDATED, func)
+wxEVT_TIMEVAL_UPDATED = wx.NewEventType()
+EVT_TIMEUPDATE = wx.PyEventBinder(wxEVT_TIMEVAL_UPDATED, 1)
-class TimeUpdatedEvent(wxPyCommandEvent):
+class TimeUpdatedEvent(wx.PyCommandEvent):
def __init__(self, id, value ='12:00:00 AM'):
- wxPyCommandEvent.__init__(self, wxEVT_TIMEVAL_UPDATED, id)
+ wx.PyCommandEvent.__init__(self, wxEVT_TIMEVAL_UPDATED, id)
self.value = value
def GetValue(self):
"""Retrieve the value of the time control at the time this event was generated"""
return self.value
-
-class wxTimeCtrl(wxMaskedTextCtrl):
+class TimeCtrlAccessorsMixin:
+ # Define TimeCtrl's list of attributes having their own
+ # Get/Set functions, ignoring those that make no sense for
+ # an numeric control.
+ exposed_basectrl_params = (
+ 'defaultValue',
+ 'description',
+
+ 'useFixedWidthFont',
+ 'emptyBackgroundColour',
+ 'validBackgroundColour',
+ 'invalidBackgroundColour',
+
+ 'validFunc',
+ 'validRequired',
+ )
+ for param in exposed_basectrl_params:
+ propname = param[0].upper() + param[1:]
+ exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname, param))
+ exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param))
+
+ if param.find('Colour') != -1:
+ # add non-british spellings, for backward-compatibility
+ propname.replace('Colour', 'Color')
+
+ exec('def Set%s(self, value): self.SetCtrlParameters(%s=value)' % (propname, param))
+ exec('def Get%s(self): return self.GetCtrlParameter("%s")''' % (propname, param))
+
+
+class TimeCtrl(BaseMaskedTextCtrl):
valid_ctrl_params = {
- 'display_seconds' : True, # by default, shows seconds
+ 'format' : 'HHMMSS', # default format code
+ 'displaySeconds' : True, # by default, shows seconds
'min': None, # by default, no bounds set
'max': None,
'limited': False, # by default, no limiting even if bounds set
'useFixedWidthFont': True, # by default, use a fixed-width font
- 'oob_color': "Yellow" # by default, the default wxMaskedTextCtrl "invalid" color
+ 'oob_color': "Yellow" # by default, the default MaskedTextCtrl "invalid" color
}
def __init__ (
self, parent, id=-1, value = '12:00:00 AM',
- pos = wxDefaultPosition, size = wxDefaultSize,
+ pos = wx.DefaultPosition, size = wx.DefaultSize,
fmt24hr=False,
spinButton = None,
- style = wxTE_PROCESS_TAB,
- validator = wxDefaultValidator,
+ style = wx.TE_PROCESS_TAB,
+ validator = wx.DefaultValidator,
name = "time",
**kwargs ):
# set defaults for control:
dbg('setting defaults:')
- for key, param_value in wxTimeCtrl.valid_ctrl_params.items():
+ for key, param_value in TimeCtrl.valid_ctrl_params.items():
# This is done this way to make setattr behave consistently with
# "private attribute" name mangling
- setattr(self, "_wxTimeCtrl__" + key, copy.copy(param_value))
+ setattr(self, "_TimeCtrl__" + key, copy.copy(param_value))
# create locals from current defaults, so we can override if
# specified in kwargs, and handle uniformly:
max = self.__max
limited = self.__limited
self.__posCurrent = 0
+ # handle deprecated keword argument name:
+ if kwargs.has_key('display_seconds'):
+ kwargs['displaySeconds'] = kwargs['display_seconds']
+ del kwargs['display_seconds']
+ if not kwargs.has_key('displaySeconds'):
+ kwargs['displaySeconds'] = True
+
+ # (handle positional arg (from original release) differently from rest of kwargs:)
+ self.__fmt24hr = False
+ if not kwargs.has_key('format'):
+ if fmt24hr:
+ if kwargs.has_key('displaySeconds') and kwargs['displaySeconds']:
+ kwargs['format'] = '24HHMMSS'
+ del kwargs['displaySeconds']
+ else:
+ kwargs['format'] = '24HHMM'
+ else:
+ if kwargs.has_key('displaySeconds') and kwargs['displaySeconds']:
+ kwargs['format'] = 'HHMMSS'
+ del kwargs['displaySeconds']
+ else:
+ kwargs['format'] = 'HHMM'
+ if not kwargs.has_key('useFixedWidthFont'):
+ # allow control over font selection:
+ kwargs['useFixedWidthFont'] = self.__useFixedWidthFont
- # (handle positional args (from original release) differently from rest of kwargs:)
- self.__fmt24hr = fmt24hr
-
- maskededit_kwargs = {}
-
- # assign keyword args as appropriate:
- for key, param_value in kwargs.items():
- if key not in wxTimeCtrl.valid_ctrl_params.keys():
- raise AttributeError('invalid keyword argument "%s"' % key)
-
- if key == "display_seconds":
- self.__display_seconds = param_value
-
- elif key == "min": min = param_value
- elif key == "max": max = param_value
- elif key == "limited": limited = param_value
-
- elif key == "useFixedWidthFont":
- maskededit_kwargs[key] = param_value
- elif key == "oob_color":
- maskededit_kwargs['invalidBackgroundColor'] = param_value
-
- if self.__fmt24hr:
- if self.__display_seconds: maskededit_kwargs['autoformat'] = 'MILTIMEHHMMSS'
- else: maskededit_kwargs['autoformat'] = 'MILTIMEHHMM'
-
- # Set hour field to zero-pad, right-insert, require explicit field change,
- # select entire field on entry, and require a resultant valid entry
- # to allow character entry:
- hourfield = Field(formatcodes='0r<SV', validRegex='0\d|1\d|2[0123]', validRequired=True)
- else:
- if self.__display_seconds: maskededit_kwargs['autoformat'] = 'TIMEHHMMSS'
- else: maskededit_kwargs['autoformat'] = 'TIMEHHMM'
-
- # Set hour field to allow spaces (at start), right-insert,
- # require explicit field change, select entire field on entry,
- # and require a resultant valid entry to allow character entry:
- hourfield = Field(formatcodes='_0<rSV', validRegex='0[1-9]| [1-9]|1[012]', validRequired=True)
- ampmfield = Field(formatcodes='S', emptyInvalid = True, validRequired = True)
-
- # Field 1 is always a zero-padded right-insert minute field,
- # similarly configured as above:
- minutefield = Field(formatcodes='0r<SV', validRegex='[0-5]\d', validRequired=True)
-
- fields = [ hourfield, minutefield ]
- if self.__display_seconds:
- fields.append(copy.copy(minutefield)) # second field has same constraints as field 1
-
- if not self.__fmt24hr:
- fields.append(ampmfield)
+ maskededit_kwargs = self.SetParameters(**kwargs)
- # set fields argument:
- maskededit_kwargs['fields'] = fields
+ # allow for explicit size specification:
+ if size != wx.DefaultSize:
+ # override (and remove) "autofit" autoformat code in standard time formats:
+ maskededit_kwargs['formatcodes'] = 'T!'
# This allows range validation if set
maskededit_kwargs['validFunc'] = self.IsInBounds
# dynamically without affecting individual field constraint validation
maskededit_kwargs['retainFieldValidation'] = True
- # allow control over font selection:
- maskededit_kwargs['useFixedWidthFont'] = self.__useFixedWidthFont
-
- # allow for explicit size specification:
- if size != wxDefaultSize:
- # override (and remove) "autofit" autoformat code in standard time formats:
- maskededit_kwargs['formatcodes'] = 'T!'
-
# Now we can initialize the base control:
- wxMaskedTextCtrl.__init__(
+ BaseMaskedTextCtrl.__init__(
self, parent, id=id,
pos=pos, size=size,
style = style,
# This makes the up/down keys act like spin button controls:
- self._SetKeycodeHandler(WXK_UP, self.__OnSpinUp)
- self._SetKeycodeHandler(WXK_DOWN, self.__OnSpinDown)
+ self._SetKeycodeHandler(wx.WXK_UP, self.__OnSpinUp)
+ self._SetKeycodeHandler(wx.WXK_DOWN, self.__OnSpinDown)
# This allows ! and c/C to set the control to the current time:
# that : takes you forward, not back, and so we can issue
# EVT_TIMEUPDATE events on changes:
- EVT_SET_FOCUS( self, self._OnFocus ) ## defeat automatic full selection
- EVT_KILL_FOCUS( self, self._OnKillFocus ) ## run internal validator
- EVT_LEFT_UP(self, self.__LimitSelection) ## limit selections to single field
- EVT_LEFT_DCLICK(self, self._OnDoubleClick ) ## select field under cursor on dclick
- EVT_KEY_DOWN( self, self._OnKeyDown ) ## capture control events not normally seen, eg ctrl-tab.
- EVT_CHAR( self, self.__OnChar ) ## remove "shift" attribute from colon key event,
- ## then call wxMaskedTextCtrl._OnChar with
- ## the possibly modified event.
- EVT_TEXT( self, self.GetId(), self.__OnTextChange ) ## color control appropriately and EVT_TIMEUPDATE events
+ self.Bind(wx.EVT_SET_FOCUS, self._OnFocus ) ## defeat automatic full selection
+ self.Bind(wx.EVT_KILL_FOCUS, self._OnKillFocus ) ## run internal validator
+ self.Bind(wx.EVT_LEFT_UP, self.__LimitSelection) ## limit selections to single field
+ self.Bind(wx.EVT_LEFT_DCLICK, self._OnDoubleClick ) ## select field under cursor on dclick
+ self.Bind(wx.EVT_KEY_DOWN, self._OnKeyDown ) ## capture control events not normally seen, eg ctrl-tab.
+ self.Bind(wx.EVT_CHAR, self.__OnChar ) ## remove "shift" attribute from colon key event,
+ ## then call BaseMaskedTextCtrl._OnChar with
+ ## the possibly modified event.
+ self.Bind(wx.EVT_TEXT, self.__OnTextChange, self ) ## color control appropriately and EVT_TIMEUPDATE events
# Validate initial value and set if appropriate
self.BindSpinButton(spinButton) # bind spin button up/down events to this control
+ def SetParameters(self, **kwargs):
+ dbg('TimeCtrl::SetParameters(%s)' % repr(kwargs), indent=1)
+ maskededit_kwargs = {}
+ reset_format = False
+
+ if kwargs.has_key('display_seconds'):
+ kwargs['displaySeconds'] = kwargs['display_seconds']
+ del kwargs['display_seconds']
+ if kwargs.has_key('format') and kwargs.has_key('displaySeconds'):
+ del kwargs['displaySeconds'] # always apply format if specified
+
+ # assign keyword args as appropriate:
+ for key, param_value in kwargs.items():
+ if key not in TimeCtrl.valid_ctrl_params.keys():
+ raise AttributeError('invalid keyword argument "%s"' % key)
+
+ if key == 'format':
+ # handle both local or generic 'maskededit' autoformat codes:
+ if param_value == 'HHMMSS' or param_value == 'TIMEHHMMSS':
+ self.__displaySeconds = True
+ self.__fmt24hr = False
+ elif param_value == 'HHMM' or param_value == 'TIMEHHMM':
+ self.__displaySeconds = False
+ self.__fmt24hr = False
+ elif param_value == '24HHMMSS' or param_value == '24HRTIMEHHMMSS':
+ self.__displaySeconds = True
+ self.__fmt24hr = True
+ elif param_value == '24HHMM' or param_value == '24HRTIMEHHMM':
+ self.__displaySeconds = False
+ self.__fmt24hr = True
+ else:
+ raise AttributeError('"%s" is not a valid format' % param_value)
+ reset_format = True
+
+ elif key in ("displaySeconds", "display_seconds") and not kwargs.has_key('format'):
+ self.__displaySeconds = param_value
+ reset_format = True
+
+ elif key == "min": min = param_value
+ elif key == "max": max = param_value
+ elif key == "limited": limited = param_value
+
+ elif key == "useFixedWidthFont":
+ maskededit_kwargs[key] = param_value
+
+ elif key == "oob_color":
+ maskededit_kwargs['invalidBackgroundColor'] = param_value
+
+ if reset_format:
+ if self.__fmt24hr:
+ if self.__displaySeconds: maskededit_kwargs['autoformat'] = '24HRTIMEHHMMSS'
+ else: maskededit_kwargs['autoformat'] = '24HRTIMEHHMM'
+
+ # Set hour field to zero-pad, right-insert, require explicit field change,
+ # select entire field on entry, and require a resultant valid entry
+ # to allow character entry:
+ hourfield = Field(formatcodes='0r<SV', validRegex='0\d|1\d|2[0123]', validRequired=True)
+ else:
+ if self.__displaySeconds: maskededit_kwargs['autoformat'] = 'TIMEHHMMSS'
+ else: maskededit_kwargs['autoformat'] = 'TIMEHHMM'
+
+ # Set hour field to allow spaces (at start), right-insert,
+ # require explicit field change, select entire field on entry,
+ # and require a resultant valid entry to allow character entry:
+ hourfield = Field(formatcodes='_0<rSV', validRegex='0[1-9]| [1-9]|1[012]', validRequired=True)
+ ampmfield = Field(formatcodes='S', emptyInvalid = True, validRequired = True)
+
+ # Field 1 is always a zero-padded right-insert minute field,
+ # similarly configured as above:
+ minutefield = Field(formatcodes='0r<SV', validRegex='[0-5]\d', validRequired=True)
+
+ fields = [ hourfield, minutefield ]
+ if self.__displaySeconds:
+ fields.append(copy.copy(minutefield)) # second field has same constraints as field 1
+
+ if not self.__fmt24hr:
+ fields.append(ampmfield)
+
+ # set fields argument:
+ maskededit_kwargs['fields'] = fields
+
+ # This allows range validation if set
+ maskededit_kwargs['validFunc'] = self.IsInBounds
+
+ # This allows range limits to affect insertion into control or not
+ # dynamically without affecting individual field constraint validation
+ maskededit_kwargs['retainFieldValidation'] = True
+
+ if hasattr(self, 'controlInitialized') and self.controlInitialized:
+ self.SetCtrlParameters(**maskededit_kwargs) # set appropriate parameters
+
+ # Validate initial value and set if appropriate
+ try:
+ self.SetBounds(min, max)
+ self.SetLimited(limited)
+ self.SetValue(value)
+ except:
+ self.SetValue('12:00:00 AM')
+ dbg(indent=0)
+ return {} # no arguments to return
+ else:
+ dbg(indent=0)
+ return maskededit_kwargs
+
def BindSpinButton(self, sb):
"""
This function binds an externally created spin button to the control, so that
up/down events from the button automatically change the control.
"""
- dbg('wxTimeCtrl::BindSpinButton')
+ dbg('TimeCtrl::BindSpinButton')
self.__spinButton = sb
if self.__spinButton:
# bind event handlers to spin ctrl
- EVT_SPIN_UP(self.__spinButton, self.__spinButton.GetId(), self.__OnSpinUp)
- EVT_SPIN_DOWN(self.__spinButton, self.__spinButton.GetId(), self.__OnSpinDown)
+ self.__spinButton.Bind(wx.EVT_SPIN_UP, self.__OnSpinUp, self.__spinButton)
+ self.__spinButton.Bind(wx.EVT_SPIN_DOWN, self.__OnSpinDown, self.__spinButton)
def __repr__(self):
- return "<wxTimeCtrl: %s>" % self.GetValue()
+ return "<TimeCtrl: %s>" % self.GetValue()
def SetValue(self, value):
and convert wxDateTime, mxDateTime, or 12/24 format time string
into the appropriate format string for the control.
"""
- dbg('wxTimeCtrl::SetValue(%s)' % repr(value), indent=1)
+ dbg('TimeCtrl::SetValue(%s)' % repr(value), indent=1)
try:
strtime = self._toGUI(self.__validateValue(value))
except:
elif as_mxDateTime:
value = DateTime.DateTime(1970, 1, 1, value.GetHour(), value.GetMinute(), value.GetSecond())
elif as_wxTimeSpan:
- value = wxTimeSpan(value.GetHour(), value.GetMinute(), value.GetSecond())
+ value = wx.TimeSpan(value.GetHour(), value.GetMinute(), value.GetSecond())
elif as_mxDateTimeDelta:
value = DateTime.DateTimeDelta(0, value.GetHour(), value.GetMinute(), value.GetSecond())
else:
- value = wxMaskedTextCtrl.GetValue(self)
+ value = BaseMaskedTextCtrl.GetValue(self)
return value
def GetWxDateTime(self, value=None):
"""
- This function is the conversion engine for wxTimeCtrl; it takes
+ This function is the conversion engine for TimeCtrl; it takes
one of the following types:
time string
wxDateTime
"""
global accept_mx
dbg(suspend=1)
- dbg('wxTimeCtrl::GetWxDateTime(%s)' % repr(value), indent=1)
+ dbg('TimeCtrl::GetWxDateTime(%s)' % repr(value), indent=1)
if value is None:
dbg('getting control value')
value = self.GetValue()
if type(value) == types.StringType:
# Construct constant wxDateTime, then try to parse the string:
- wxdt = wxDateTimeFromDMY(1, 0, 1970)
+ wxdt = wx.DateTimeFromDMY(1, 0, 1970)
dbg('attempting conversion')
value = value.strip() # (parser doesn't like leading spaces)
checkTime = wxdt.ParseTime(value)
raise ValueError('cannot convert string "%s" to valid time' % value)
else:
- if isinstance(value, wxDateTime):
+ if isinstance(value, wx.DateTime):
hour, minute, second = value.GetHour(), value.GetMinute(), value.GetSecond()
- elif isinstance(value, wxTimeSpan):
+ elif isinstance(value, wx.TimeSpan):
totalseconds = value.GetSeconds()
hour = totalseconds / 3600
minute = totalseconds / 60 - (hour * 60)
dbg(indent=0, suspend=0)
raise ValueError(error)
- wxdt = wxDateTimeFromDMY(1, 0, 1970)
+ wxdt = wx.DateTimeFromDMY(1, 0, 1970)
wxdt.SetHour(hour)
wxdt.SetMinute(minute)
wxdt.SetSecond(second)
adjusted to the new minimum value; if not limited, the value in the
control will be colored as invalid.
"""
- dbg('wxTimeCtrl::SetMin(%s)'% repr(min), indent=1)
+ dbg('TimeCtrl::SetMin(%s)'% repr(min), indent=1)
if min is not None:
try:
min = self.GetWxDateTime(min)
by default, or as a string if as_string argument is True.
"""
dbg(suspend=1)
- dbg('wxTimeCtrl::GetMin, as_string?', as_string, indent=1)
+ dbg('TimeCtrl::GetMin, as_string?', as_string, indent=1)
if self.__min is None:
dbg('(min == None)')
ret = self.__min
adjusted to this maximum value; if not limited, the value in the
control will be colored as invalid.
"""
- dbg('wxTimeCtrl::SetMax(%s)' % repr(max), indent=1)
+ dbg('TimeCtrl::SetMax(%s)' % repr(max), indent=1)
if max is not None:
try:
max = self.GetWxDateTime(max)
by default, or as a string if as_string argument is True.
"""
dbg(suspend=1)
- dbg('wxTimeCtrl::GetMin, as_string?', as_string, indent=1)
+ dbg('TimeCtrl::GetMin, as_string?', as_string, indent=1)
if self.__max is None:
dbg('(max == None)')
ret = self.__max
limiting, but coloring of out-of-bounds values will still take
place if bounds have been set for the control.
"""
- dbg('wxTimeCtrl::SetLimited(%d)' % limited, indent=1)
+ dbg('TimeCtrl::SetLimited(%d)' % limited, indent=1)
self.__limited = limited
if not limited:
# Note: relies on min and max and value date portions
# always being the same.
- interval = (min + wxTimeSpan(24, 0, 0, 0)) - max
+ interval = (min + wx.TimeSpan(24, 0, 0, 0)) - max
- half_interval = wxTimeSpan(
+ half_interval = wx.TimeSpan(
0, # hours
0, # minutes
interval.GetSeconds() / 2, # seconds
if value < min: # min is on next day, so use value on
# "next day" for "nearest" interval calculation:
- cmp_value = value + wxTimeSpan(24, 0, 0, 0)
+ cmp_value = value + wx.TimeSpan(24, 0, 0, 0)
else: # "before midnight; ok
cmp_value = value
dbg('ValueError getting wxDateTime for %s' % repr(value), indent=0)
raise
- dbg('wxTimeCtrl::IsInBounds(%s)' % repr(value), indent=1)
+ dbg('TimeCtrl::IsInBounds(%s)' % repr(value), indent=1)
if self.__min is None or self.__max is None:
dbg(indent=0)
return True
min = self.GetMin()
max = self.GetMax()
- midnight = wxDateTimeFromDMY(1, 0, 1970)
+ midnight = wx.DateTimeFromDMY(1, 0, 1970)
if min <= max: # they don't span midnight
ret = min <= value <= max
except ValueError:
return False
+ def SetFormat(self, format):
+ self.SetParameters(format=format)
+
+ def GetFormat(self):
+ if self.__displaySeconds:
+ if self.__fmt24hr: return '24HHMMSS'
+ else: return 'HHMMSS'
+ else:
+ if self.__fmt24hr: return '24HHMM'
+ else: return 'HHMM'
#-------------------------------------------------------------------------------------------------------------
# these are private functions and overrides:
def __OnTextChange(self, event=None):
- dbg('wxTimeCtrl::OnTextChange', indent=1)
+ dbg('TimeCtrl::OnTextChange', indent=1)
- # Allow wxMaskedtext base control to color as appropriate,
+ # Allow Maskedtext base control to color as appropriate,
# and Skip the EVT_TEXT event (if appropriate.)
##! WS: For some inexplicable reason, every wxTextCtrl.SetValue()
## call is generating two (2) EVT_TEXT events. (!)
## event iff the value has actually changed. The masked edit
## OnTextChange routine does this, and returns True on a valid event,
## False otherwise.
- if not wxMaskedTextCtrl._OnTextChange(self, event):
+ if not BaseMaskedTextCtrl._OnTextChange(self, event):
return
dbg('firing TimeUpdatedEvent...')
This is necessary to handle the optional spin button, because the insertion
point is lost when the focus shifts to the spin button.
"""
- dbg('wxTimeCtrl::SetInsertionPoint', pos, indent=1)
- wxMaskedTextCtrl.SetInsertionPoint(self, pos) # (causes EVT_TEXT event to fire)
+ dbg('TimeCtrl::SetInsertionPoint', pos, indent=1)
+ BaseMaskedTextCtrl.SetInsertionPoint(self, pos) # (causes EVT_TEXT event to fire)
self.__posCurrent = self.GetInsertionPoint()
dbg(indent=0)
def SetSelection(self, sel_start, sel_to):
- dbg('wxTimeCtrl::SetSelection', sel_start, sel_to, indent=1)
+ dbg('TimeCtrl::SetSelection', sel_start, sel_to, indent=1)
# Adjust selection range to legal extent if not already
if sel_start < 0:
sel_to = cell_end
self.__bSelection = sel_start != sel_to
- wxMaskedTextCtrl.SetSelection(self, sel_start, sel_to)
+ BaseMaskedTextCtrl.SetSelection(self, sel_start, sel_to)
dbg(indent=0)
Event handler for any bound spin button on EVT_SPIN_UP;
causes control to behave as if up arrow was pressed.
"""
- dbg('wxTimeCtrl::OnSpinUp', indent=1)
- self.__OnSpin(WXK_UP)
+ dbg('TimeCtrl::OnSpinUp', indent=1)
+ self.__OnSpin(wx.WXK_UP)
keep_processing = False
dbg(indent=0)
return keep_processing
Event handler for any bound spin button on EVT_SPIN_DOWN;
causes control to behave as if down arrow was pressed.
"""
- dbg('wxTimeCtrl::OnSpinDown', indent=1)
- self.__OnSpin(WXK_DOWN)
+ dbg('TimeCtrl::OnSpinDown', indent=1)
+ self.__OnSpin(wx.WXK_DOWN)
keep_processing = False
dbg(indent=0)
return keep_processing
It then calls the base control's _OnChar routine with the modified
event instance.
"""
- dbg('wxTimeCtrl::OnChar', indent=1)
+ dbg('TimeCtrl::OnChar', indent=1)
keycode = event.GetKeyCode()
dbg('keycode:', keycode)
if keycode == ord(':'):
dbg('colon seen! removing shift attribute')
event.m_shiftDown = False
- wxMaskedTextCtrl._OnChar(self, event ) ## handle each keypress
+ BaseMaskedTextCtrl._OnChar(self, event ) ## handle each keypress
dbg(indent=0)
This is the key handler for '!' and 'c'; this allows the user to
quickly set the value of the control to the current time.
"""
- self.SetValue(wxDateTime_Now().FormatTime())
+ self.SetValue(wx.DateTime_Now().FormatTime())
keep_processing = False
return keep_processing
Event handler for motion events; this handler
changes limits the selection to the new cell boundaries.
"""
- dbg('wxTimeCtrl::LimitSelection', indent=1)
+ dbg('TimeCtrl::LimitSelection', indent=1)
pos = self.GetInsertionPoint()
self.__posCurrent = pos
sel_start, sel_to = self.GetSelection()
def __IncrementValue(self, key, pos):
- dbg('wxTimeCtrl::IncrementValue', key, pos, indent=1)
+ dbg('TimeCtrl::IncrementValue', key, pos, indent=1)
text = self.GetValue()
field = self._FindField(pos)
dbg('field: ', field._index)
start, end = field._extent
slice = text[start:end]
- if key == WXK_UP: increment = 1
+ if key == wx.WXK_UP: increment = 1
else: increment = -1
if slice in ('A', 'P'):
# adjusting this field is trickier, as its value can affect the
# am/pm setting. So, we use wxDateTime to generate a new value for us:
# (Use a fixed date not subject to DST variations:)
- converter = wxDateTimeFromDMY(1, 0, 1970)
+ converter = wx.DateTimeFromDMY(1, 0, 1970)
dbg('text: "%s"' % text)
converter.ParseTime(text.strip())
currenthour = converter.GetHour()
self.SetValue(newvalue)
except ValueError: # must not be in bounds:
- if not wxValidator_IsSilent():
- wxBell()
+ if not wx.Validator_IsSilent():
+ wx.Bell()
dbg(indent=0)
converts it to a string appropriate for the format of the control.
"""
if self.__fmt24hr:
- if self.__display_seconds: strval = wxdt.Format('%H:%M:%S')
- else: strval = wxdt.Format('%H:%M')
+ if self.__displaySeconds: strval = wxdt.Format('%H:%M:%S')
+ else: strval = wxdt.Format('%H:%M')
else:
- if self.__display_seconds: strval = wxdt.Format('%I:%M:%S %p')
- else: strval = wxdt.Format('%I:%M %p')
+ if self.__displaySeconds: strval = wxdt.Format('%I:%M:%S %p')
+ else: strval = wxdt.Format('%I:%M %p')
return strval
not a valid value for the control as currently specified.
It is used by both the SetValue() and the IsValid() methods.
"""
- dbg('wxTimeCtrl::__validateValue(%s)' % repr(value), indent=1)
+ dbg('TimeCtrl::__validateValue(%s)' % repr(value), indent=1)
if not value:
dbg(indent=0)
raise ValueError('%s not a valid time value' % repr(value))
return value
#----------------------------------------------------------------------------
-# Test jig for wxTimeCtrl:
+# Test jig for TimeCtrl:
if __name__ == '__main__':
import traceback
- class TestPanel(wxPanel):
+ class TestPanel(wx.Panel):
def __init__(self, parent, id,
- pos = wxPyDefaultPosition, size = wxPyDefaultSize,
+ pos = wx.DefaultPosition, size = wx.DefaultSize,
fmt24hr = 0, test_mx = 0,
- style = wxTAB_TRAVERSAL ):
+ style = wx.TAB_TRAVERSAL ):
- wxPanel.__init__(self, parent, id, pos, size, style)
+ wx.Panel.__init__(self, parent, id, pos, size, style)
self.test_mx = test_mx
- self.tc = wxTimeCtrl(self, 10, fmt24hr = fmt24hr)
- sb = wxSpinButton( self, 20, wxDefaultPosition, wxSize(-1,20), 0 )
+ self.tc = TimeCtrl(self, 10, fmt24hr = fmt24hr)
+ sb = wx.SpinButton( self, 20, wx.DefaultPosition, (-1,20), 0 )
self.tc.BindSpinButton(sb)
- sizer = wxBoxSizer( wxHORIZONTAL )
- sizer.AddWindow( self.tc, 0, wxALIGN_CENTRE|wxLEFT|wxTOP|wxBOTTOM, 5 )
- sizer.AddWindow( sb, 0, wxALIGN_CENTRE|wxRIGHT|wxTOP|wxBOTTOM, 5 )
+ sizer = wx.BoxSizer( wx.HORIZONTAL )
+ sizer.Add( self.tc, 0, wx.ALIGN_CENTRE|wx.LEFT|wx.TOP|wx.BOTTOM, 5 )
+ sizer.Add( sb, 0, wx.ALIGN_CENTRE|wx.RIGHT|wx.TOP|wx.BOTTOM, 5 )
self.SetAutoLayout( True )
self.SetSizer( sizer )
sizer.Fit( self )
sizer.SetSizeHints( self )
- EVT_TIMEUPDATE(self, self.tc.GetId(), self.OnTimeChange)
+ self.Bind(EVT_TIMEUPDATE, self.OnTimeChange, self.tc)
def OnTimeChange(self, event):
dbg('OnTimeChange: value = ', event.GetValue())
dbg('mxdt =', mxdt.hour, mxdt.minute, mxdt.second)
- class MyApp(wxApp):
+ class MyApp(wx.App):
def OnInit(self):
import sys
fmt24hr = '24' in sys.argv
test_mx = 'mx' in sys.argv
try:
- frame = wxFrame(NULL, -1, "wxTimeCtrl Test", wxPoint(20,20), wxSize(100,100) )
- panel = TestPanel(frame, -1, wxPoint(-1,-1), fmt24hr=fmt24hr, test_mx = test_mx)
+ frame = wx.Frame(None, -1, "TimeCtrl Test", (20,20), (100,100) )
+ panel = TestPanel(frame, -1, (-1,-1), fmt24hr=fmt24hr, test_mx = test_mx)
frame.Show(True)
except:
traceback.print_exc()
app.MainLoop()
except:
traceback.print_exc()
+i=0
+## Version 1.2
+## 1. Changed parameter name display_seconds to displaySeconds, to follow
+## other masked edit conventions.
+## 2. Added format parameter, to remove need to use both fmt24hr and displaySeconds.
+## 3. Changed inheritance to use BaseMaskedTextCtrl, to remove exposure of
+## nonsensical parameter methods from the control, so it will work
+## properly with Boa.