]> git.saurik.com Git - wxWidgets.git/blame - wxPython/demo/TimeCtrl.py
added missing button state
[wxWidgets.git] / wxPython / demo / TimeCtrl.py
CommitLineData
c878ceea 1#
8fa876ca
RD
2# 11/21/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3#
8fa876ca 4# o presense of spin control causing probs (see spin ctrl demo for details)
c878ceea 5#
8fa876ca 6
c878ceea
RD
7import wx
8import wx.lib.scrolledpanel as scrolled
9import wx.lib.masked as masked
1e4a197e
RD
10
11#----------------------------------------------------------------------
12
d4b73b1b 13class TestPanel( scrolled.ScrolledPanel ):
1e4a197e
RD
14 def __init__( self, parent, log ):
15
d4b73b1b 16 scrolled.ScrolledPanel.__init__( self, parent, -1 )
1e4a197e 17 self.log = log
1e4a197e 18
0c8f2860
RD
19 box_label = wx.StaticBox( self, -1, "Change Controls through API" )
20 buttonbox = wx.StaticBoxSizer( box_label, wx.HORIZONTAL )
21
8fa876ca 22 text1 = wx.StaticText( self, -1, "12-hour format:")
c878ceea 23 self.time12 = masked.TimeCtrl( self, -1, name="12 hour control" )
f54a36bb
RD
24 h = self.time12.GetSize().height
25 spin1 = wx.SpinButton( self, -1, wx.DefaultPosition, (-1,h), wx.SP_VERTICAL )
1e4a197e
RD
26 self.time12.BindSpinButton( spin1 )
27
8fa876ca 28 text2 = wx.StaticText( self, -1, "24-hour format:")
f54a36bb 29 spin2 = wx.SpinButton( self, -1, wx.DefaultPosition, (-1,h), wx.SP_VERTICAL )
c878ceea
RD
30 self.time24 = masked.TimeCtrl(
31 self, -1, name="24 hour control", fmt24hr=True,
32 spinButton = spin2
8fa876ca
RD
33 )
34
35 text3 = wx.StaticText( self, -1, "No seconds\nor spin button:")
c878ceea
RD
36 self.spinless_ctrl = masked.TimeCtrl(
37 self, -1, name="spinless control",
38 display_seconds = False
8fa876ca
RD
39 )
40
41 grid = wx.FlexGridSizer( 0, 2, 10, 5 )
42 grid.Add( text1, 0, wx.ALIGN_RIGHT )
43 hbox1 = wx.BoxSizer( wx.HORIZONTAL )
44 hbox1.Add( self.time12, 0, wx.ALIGN_CENTRE )
45 hbox1.Add( spin1, 0, wx.ALIGN_CENTRE )
46 grid.Add( hbox1, 0, wx.LEFT )
47
48 grid.Add( text2, 0, wx.ALIGN_RIGHT|wx.TOP|wx.BOTTOM )
49 hbox2 = wx.BoxSizer( wx.HORIZONTAL )
50 hbox2.Add( self.time24, 0, wx.ALIGN_CENTRE )
51 hbox2.Add( spin2, 0, wx.ALIGN_CENTRE )
52 grid.Add( hbox2, 0, wx.LEFT )
53
54 grid.Add( text3, 0, wx.ALIGN_RIGHT|wx.TOP|wx.BOTTOM )
55 grid.Add( self.spinless_ctrl, 0, wx.LEFT )
56
57
58 buttonChange = wx.Button( self, -1, "Change Controls")
59 self.radio12to24 = wx.RadioButton(
c878ceea
RD
60 self, -1, "Copy 12-hour time to 24-hour control",
61 wx.DefaultPosition, wx.DefaultSize, wx.RB_GROUP
8fa876ca
RD
62 )
63
64 self.radio24to12 = wx.RadioButton(
65 self, -1, "Copy 24-hour time to 12-hour control"
66 )
67
68 self.radioWx = wx.RadioButton( self, -1, "Set controls to 'now' using wxDateTime")
69 self.radioMx = wx.RadioButton( self, -1, "Set controls to 'now' using mxDateTime")
70
71 radio_vbox = wx.BoxSizer( wx.VERTICAL )
72 radio_vbox.Add( self.radio12to24, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
73 radio_vbox.Add( self.radio24to12, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
74 radio_vbox.Add( self.radioWx, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
75 radio_vbox.Add( self.radioMx, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
0c8f2860 76
8fa876ca
RD
77 buttonbox.Add( buttonChange, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
78 buttonbox.Add( radio_vbox, 0, wx.ALIGN_CENTRE|wx.ALL, 5 )
79
80 hbox = wx.BoxSizer( wx.HORIZONTAL )
81 hbox.Add( grid, 0, wx.ALIGN_LEFT|wx.ALL, 15 )
82 hbox.Add( buttonbox, 0, wx.ALIGN_RIGHT|wx.BOTTOM, 20 )
83
84
85 box_label = wx.StaticBox( self, -1, "Bounds Control" )
86 boundsbox = wx.StaticBoxSizer( box_label, wx.HORIZONTAL )
87 self.set_bounds = wx.CheckBox( self, -1, "Set time bounds:" )
88
89 minlabel = wx.StaticText( self, -1, "minimum time:" )
c878ceea 90 self.min = masked.TimeCtrl( self, -1, name="min", display_seconds = False )
1fded56b
RD
91 self.min.Enable( False )
92
8fa876ca 93 maxlabel = wx.StaticText( self, -1, "maximum time:" )
c878ceea 94 self.max = masked.TimeCtrl( self, -1, name="max", display_seconds = False )
1fded56b
RD
95 self.max.Enable( False )
96
8fa876ca 97 self.limit_check = wx.CheckBox( self, -1, "Limit control" )
1fded56b 98
8fa876ca 99 label = wx.StaticText( self, -1, "Resulting time control:" )
c878ceea 100 self.target_ctrl = masked.TimeCtrl( self, -1, name="new" )
1fded56b 101
8fa876ca
RD
102 grid2 = wx.FlexGridSizer( 0, 2, 0, 0 )
103 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
104 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
1fded56b 105
8fa876ca
RD
106 grid2.Add( self.set_bounds, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
107 grid3 = wx.FlexGridSizer( 0, 2, 5, 5 )
108 grid3.Add(minlabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
109 grid3.Add( self.min, 0, wx.ALIGN_LEFT )
110 grid3.Add(maxlabel, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL )
111 grid3.Add( self.max, 0, wx.ALIGN_LEFT )
112 grid2.Add(grid3, 0, wx.ALIGN_LEFT )
1fded56b 113
8fa876ca
RD
114 grid2.Add( self.limit_check, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
115 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
1fded56b 116
8fa876ca
RD
117 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
118 grid2.Add( (20, 0), 0, wx.ALIGN_LEFT|wx.ALL, 5 )
119 grid2.Add( label, 0, wx.ALIGN_LEFT|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5 )
120 grid2.Add( self.target_ctrl, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
121 boundsbox.Add(grid2, 0, wx.ALIGN_CENTER|wx.EXPAND|wx.ALL, 5)
1fded56b 122
8fa876ca 123 vbox = wx.BoxSizer( wx.VERTICAL )
fd3f2efe 124 vbox.Add( (20, 20) )
8fa876ca
RD
125 vbox.Add( hbox, 0, wx.ALIGN_LEFT|wx.ALL, 5)
126 vbox.Add( boundsbox, 0, wx.ALIGN_LEFT|wx.ALL, 5 )
1fded56b 127
1e4a197e 128
8fa876ca
RD
129 outer_box = wx.BoxSizer( wx.VERTICAL )
130 outer_box.Add( vbox, 0, wx.ALIGN_LEFT|wx.ALL, 5)
1e4a197e
RD
131
132
133 # Turn on mxDateTime option only if we can import the module:
134 try:
135 from mx import DateTime
136 except ImportError:
137 self.radioMx.Enable( False )
138
139
1fded56b
RD
140 self.SetAutoLayout( True )
141 self.SetSizer( outer_box )
142 outer_box.Fit( self )
143 self.SetupScrolling()
1e4a197e 144
8fa876ca 145 self.Bind(wx.EVT_BUTTON, self.OnButtonClick, buttonChange )
c878ceea
RD
146 self.Bind(masked.EVT_TIMEUPDATE, self.OnTimeChange, self.time12 )
147 self.Bind(masked.EVT_TIMEUPDATE, self.OnTimeChange, self.time24 )
148 self.Bind(masked.EVT_TIMEUPDATE, self.OnTimeChange, self.spinless_ctrl )
8fa876ca
RD
149 self.Bind(wx.EVT_CHECKBOX, self.OnBoundsCheck, self.set_bounds )
150 self.Bind(wx.EVT_CHECKBOX, self.SetTargetMinMax, self.limit_check )
c878ceea
RD
151 self.Bind(masked.EVT_TIMEUPDATE, self.SetTargetMinMax, self.min )
152 self.Bind(masked.EVT_TIMEUPDATE, self.SetTargetMinMax, self.max )
153 self.Bind(masked.EVT_TIMEUPDATE, self.OnTimeChange, self.target_ctrl )
1e4a197e
RD
154
155
156 def OnTimeChange( self, event ):
1fded56b
RD
157 timectrl = self.FindWindowById( event.GetId() )
158 ib_str = [ " (out of bounds)", "" ]
159
160 self.log.write('%s time = %s%s\n' % ( timectrl.GetName(), timectrl.GetValue(), ib_str[ timectrl.IsInBounds() ] ) )
161
1e4a197e
RD
162
163 def OnButtonClick( self, event ):
164 if self.radio12to24.GetValue():
165 self.time24.SetValue( self.time12.GetValue() )
166
167 elif self.radio24to12.GetValue():
168 self.time12.SetValue( self.time24.GetValue() )
169
170 elif self.radioWx.GetValue():
8fa876ca 171 now = wx.DateTime_Now()
1fded56b 172 self.time12.SetValue( now )
95bfd958 173 # (demonstrates that G/SetValue returns/takes a wx.DateTime)
1fded56b
RD
174 self.time24.SetValue( self.time12.GetValue(as_wxDateTime=True) )
175
95bfd958 176 # (demonstrates that G/SetValue returns/takes a wx.TimeSpan)
1fded56b 177 self.spinless_ctrl.SetValue( self.time12.GetValue(as_wxTimeSpan=True) )
1e4a197e
RD
178
179 elif self.radioMx.GetValue():
180 from mx import DateTime
181 now = DateTime.now()
1fded56b
RD
182 self.time12.SetValue( now )
183
184 # (demonstrates that G/SetValue returns/takes a DateTime)
185 self.time24.SetValue( self.time12.GetValue(as_mxDateTime=True) )
186
187 # (demonstrates that G/SetValue returns/takes a DateTimeDelta)
188 self.spinless_ctrl.SetValue( self.time12.GetValue(as_mxDateTimeDelta=True) )
189
190
191 def OnBoundsCheck( self, event ):
192 self.min.Enable( self.set_bounds.GetValue() )
193 self.max.Enable( self.set_bounds.GetValue() )
194 self.SetTargetMinMax()
195
196
197 def SetTargetMinMax( self, event=None ):
8fa876ca
RD
198 min = None
199 max = None
1fded56b
RD
200
201 if self.set_bounds.GetValue():
202 min = self.min.GetWxDateTime()
203 max = self.max.GetWxDateTime()
204 else:
205 min, max = None, None
206
207 cur_min, cur_max = self.target_ctrl.GetBounds()
c878ceea 208 print cur_min, min
8fa876ca
RD
209 if min and (min != cur_min): self.target_ctrl.SetMin( min )
210 if max and (max != cur_max): self.target_ctrl.SetMax( max )
1fded56b
RD
211
212 self.target_ctrl.SetLimited( self.limit_check.GetValue() )
213
214 if min != cur_min or max != cur_max:
215 new_min, new_max = self.target_ctrl.GetBounds()
8fa876ca 216
1fded56b
RD
217 if new_min and new_max:
218 self.log.write( "current min, max: (%s, %s)\n" % ( new_min.FormatTime(), new_max.FormatTime() ) )
219 else:
220 self.log.write( "current min, max: (None, None)\n" )
1e4a197e
RD
221
222#----------------------------------------------------------------------
223
224def runTest( frame, nb, log ):
225 win = TestPanel( nb, log )
226 return win
227
228#----------------------------------------------------------------------
c878ceea 229import wx.lib.masked.timectrl as timectl
f54a36bb
RD
230overview = """<html>
231<PRE><FONT SIZE=-1>
232""" + timectl.__doc__ + """
233</FONT></PRE>"""
1e4a197e
RD
234
235if __name__ == '__main__':
236 import sys,os
237 import run
c878ceea 238 run.main(['', os.path.basename(sys.argv[0])])
1e4a197e 239