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