]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxTimeCtrl.py
1 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o wx renamer needed for timectrl lib
8 # o presense of spin control causing probs (see spin ctrl demo for details)
10 # 12/13/2003 - Jeff Grimmett (grimmtooth@softhome.net)
12 # o New binders applied. Issues still exist.
14 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
16 # o wxTimeCtrl -> TimeCtrl
17 # o wxScrolledPanel -> ScrolledPanel
21 import wx
.lib
.timectrl
as timectl
22 import wx
.lib
.scrolledpanel
as scrolled
24 #----------------------------------------------------------------------
26 class TestPanel( scrolled
.ScrolledPanel
):
27 def __init__( self
, parent
, log
):
29 scrolled
.ScrolledPanel
.__init
__( self
, parent
, -1 )
33 text1
= wx
.StaticText( self
, -1, "12-hour format:")
34 self
.time12
= timectl
.TimeCtrl( self
, -1, name
="12 hour control" )
35 spin1
= wx
.SpinButton( self
, -1, wx
.DefaultPosition
, (-1,20), 0 )
36 self
.time12
.BindSpinButton( spin1
)
38 text2
= wx
.StaticText( self
, -1, "24-hour format:")
39 spin2
= wx
.SpinButton( self
, -1, wx
.DefaultPosition
, (-1,20), 0 )
40 self
.time24
= timectl
.TimeCtrl(
41 self
, -1, name
="24 hour control", fmt24hr
=True,
45 text3
= wx
.StaticText( self
, -1, "No seconds\nor spin button:")
46 self
.spinless_ctrl
= timectl
.TimeCtrl(
47 self
, -1, name
="spinless control",
48 display_seconds
= False
51 grid
= wx
.FlexGridSizer( 0, 2, 10, 5 )
52 grid
.Add( text1
, 0, wx
.ALIGN_RIGHT
)
53 hbox1
= wx
.BoxSizer( wx
.HORIZONTAL
)
54 hbox1
.Add( self
.time12
, 0, wx
.ALIGN_CENTRE
)
55 hbox1
.Add( spin1
, 0, wx
.ALIGN_CENTRE
)
56 grid
.Add( hbox1
, 0, wx
.LEFT
)
58 grid
.Add( text2
, 0, wx
.ALIGN_RIGHT|wx
.TOP|wx
.BOTTOM
)
59 hbox2
= wx
.BoxSizer( wx
.HORIZONTAL
)
60 hbox2
.Add( self
.time24
, 0, wx
.ALIGN_CENTRE
)
61 hbox2
.Add( spin2
, 0, wx
.ALIGN_CENTRE
)
62 grid
.Add( hbox2
, 0, wx
.LEFT
)
64 grid
.Add( text3
, 0, wx
.ALIGN_RIGHT|wx
.TOP|wx
.BOTTOM
)
65 grid
.Add( self
.spinless_ctrl
, 0, wx
.LEFT
)
68 buttonChange
= wx
.Button( self
, -1, "Change Controls")
69 self
.radio12to24
= wx
.RadioButton(
70 self
, -1, "Copy 12-hour time to 24-hour control",
71 wx
.DefaultPosition
, wx
.DefaultSize
, wx
.RB_GROUP
74 self
.radio24to12
= wx
.RadioButton(
75 self
, -1, "Copy 24-hour time to 12-hour control"
78 self
.radioWx
= wx
.RadioButton( self
, -1, "Set controls to 'now' using wxDateTime")
79 self
.radioMx
= wx
.RadioButton( self
, -1, "Set controls to 'now' using mxDateTime")
81 radio_vbox
= wx
.BoxSizer( wx
.VERTICAL
)
82 radio_vbox
.Add( self
.radio12to24
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
83 radio_vbox
.Add( self
.radio24to12
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
84 radio_vbox
.Add( self
.radioWx
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
85 radio_vbox
.Add( self
.radioMx
, 0, wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
87 box_label
= wx
.StaticBox( self
, -1, "Change Controls through API" )
88 buttonbox
= wx
.StaticBoxSizer( box_label
, wx
.HORIZONTAL
)
89 buttonbox
.Add( buttonChange
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 )
90 buttonbox
.Add( radio_vbox
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 5 )
92 hbox
= wx
.BoxSizer( wx
.HORIZONTAL
)
93 hbox
.Add( grid
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 15 )
94 hbox
.Add( buttonbox
, 0, wx
.ALIGN_RIGHT|wx
.BOTTOM
, 20 )
97 box_label
= wx
.StaticBox( self
, -1, "Bounds Control" )
98 boundsbox
= wx
.StaticBoxSizer( box_label
, wx
.HORIZONTAL
)
99 self
.set_bounds
= wx
.CheckBox( self
, -1, "Set time bounds:" )
101 minlabel
= wx
.StaticText( self
, -1, "minimum time:" )
102 self
.min = timectl
.TimeCtrl( self
, -1, name
="min", display_seconds
= False )
103 self
.min.Enable( False )
105 maxlabel
= wx
.StaticText( self
, -1, "maximum time:" )
106 self
.max = timectl
.TimeCtrl( self
, -1, name
="max", display_seconds
= False )
107 self
.max.Enable( False )
109 self
.limit_check
= wx
.CheckBox( self
, -1, "Limit control" )
111 label
= wx
.StaticText( self
, -1, "Resulting time control:" )
112 self
.target_ctrl
= timectl
.TimeCtrl( self
, -1, name
="new" )
114 grid2
= wx
.FlexGridSizer( 0, 2, 0, 0 )
115 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
116 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
118 grid2
.Add( self
.set_bounds
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
119 grid3
= wx
.FlexGridSizer( 0, 2, 5, 5 )
120 grid3
.Add(minlabel
, 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
121 grid3
.Add( self
.min, 0, wx
.ALIGN_LEFT
)
122 grid3
.Add(maxlabel
, 0, wx
.ALIGN_RIGHT|wx
.ALIGN_CENTER_VERTICAL
)
123 grid3
.Add( self
.max, 0, wx
.ALIGN_LEFT
)
124 grid2
.Add(grid3
, 0, wx
.ALIGN_LEFT
)
126 grid2
.Add( self
.limit_check
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
127 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
129 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
130 grid2
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
131 grid2
.Add( label
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
132 grid2
.Add( self
.target_ctrl
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
133 boundsbox
.Add(grid2
, 0, wx
.ALIGN_CENTER|wx
.EXPAND|wx
.ALL
, 5)
135 vbox
= wx
.BoxSizer( wx
.VERTICAL
)
137 vbox
.Add( hbox
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
138 vbox
.Add( boundsbox
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
141 outer_box
= wx
.BoxSizer( wx
.VERTICAL
)
142 outer_box
.Add( vbox
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5)
145 # Turn on mxDateTime option only if we can import the module:
147 from mx
import DateTime
149 self
.radioMx
.Enable( False )
152 self
.SetAutoLayout( True )
153 self
.SetSizer( outer_box
)
154 outer_box
.Fit( self
)
155 self
.SetupScrolling()
157 self
.Bind(wx
.EVT_BUTTON
, self
.OnButtonClick
, buttonChange
)
158 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.time12
)
159 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.time24
)
160 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.spinless_ctrl
)
161 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnBoundsCheck
, self
.set_bounds
)
162 self
.Bind(wx
.EVT_CHECKBOX
, self
.SetTargetMinMax
, self
.limit_check
)
163 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.SetTargetMinMax
, self
.min )
164 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.SetTargetMinMax
, self
.max )
165 self
.Bind(timectl
.EVT_TIMEUPDATE
, self
.OnTimeChange
, self
.target_ctrl
)
168 def OnTimeChange( self
, event
):
169 timectrl
= self
.FindWindowById( event
.GetId() )
170 ib_str
= [ " (out of bounds)", "" ]
172 self
.log
.write('%s time = %s%s\n' % ( timectrl
.GetName(), timectrl
.GetValue(), ib_str
[ timectrl
.IsInBounds() ] ) )
175 def OnButtonClick( self
, event
):
176 if self
.radio12to24
.GetValue():
177 self
.time24
.SetValue( self
.time12
.GetValue() )
179 elif self
.radio24to12
.GetValue():
180 self
.time12
.SetValue( self
.time24
.GetValue() )
182 elif self
.radioWx
.GetValue():
183 now
= wx
.DateTime_Now()
184 self
.time12
.SetValue( now
)
185 # (demonstrates that G/SetValue returns/takes a wxDateTime)
186 self
.time24
.SetValue( self
.time12
.GetValue(as_wxDateTime
=True) )
188 # (demonstrates that G/SetValue returns/takes a wxTimeSpan)
189 self
.spinless_ctrl
.SetValue( self
.time12
.GetValue(as_wxTimeSpan
=True) )
191 elif self
.radioMx
.GetValue():
192 from mx
import DateTime
194 self
.time12
.SetValue( now
)
196 # (demonstrates that G/SetValue returns/takes a DateTime)
197 self
.time24
.SetValue( self
.time12
.GetValue(as_mxDateTime
=True) )
199 # (demonstrates that G/SetValue returns/takes a DateTimeDelta)
200 self
.spinless_ctrl
.SetValue( self
.time12
.GetValue(as_mxDateTimeDelta
=True) )
203 def OnBoundsCheck( self
, event
):
204 self
.min.Enable( self
.set_bounds
.GetValue() )
205 self
.max.Enable( self
.set_bounds
.GetValue() )
206 self
.SetTargetMinMax()
209 def SetTargetMinMax( self
, event
=None ):
213 if self
.set_bounds
.GetValue():
214 min = self
.min.GetWxDateTime()
215 max = self
.max.GetWxDateTime()
217 min, max = None, None
219 cur_min
, cur_max
= self
.target_ctrl
.GetBounds()
221 # jmg - A little expirimental change to ensure that min
222 # or max contain valid values before we use them
223 if min and (min != cur_min
): self
.target_ctrl
.SetMin( min )
224 if max and (max != cur_max
): self
.target_ctrl
.SetMax( max )
226 self
.target_ctrl
.SetLimited( self
.limit_check
.GetValue() )
228 if min != cur_min
or max != cur_max
:
229 new_min
, new_max
= self
.target_ctrl
.GetBounds()
231 if new_min
and new_max
:
232 self
.log
.write( "current min, max: (%s, %s)\n" % ( new_min
.FormatTime(), new_max
.FormatTime() ) )
234 self
.log
.write( "current min, max: (None, None)\n" )
236 #----------------------------------------------------------------------
238 def runTest( frame
, nb
, log
):
239 win
= TestPanel( nb
, log
)
242 #----------------------------------------------------------------------
244 overview
= timectl
.__doc
__
246 if __name__
== '__main__':
249 run
.main(['', os
.path
.basename(sys
.argv
[0])])