]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/IntCtrl.py
27b968c5a2899d5643bab33b4f01f17602fe0870
1 # 11/19/2003 - Jeff Grimmett (grimmtooth@softhome.net)
3 # o Updated for wx namespace
5 # 11/29/2003 - Jeff Grimmett (grimmtooth@softhome.net)
7 # o intctrl needs the renamer applied.
8 # o intctrl needs new event binders.
10 # 12/08/2003 - Jeff Grimmett (grimmtooth@softhome.net)
12 # o All issues corrected
14 # 12/20/2003 - Jeff Grimmett (grimmtooth@softhome.net)
16 # o wxIntCtrl -> IntCtrl
22 #----------------------------------------------------------------------
24 class TestPanel( wx
.Panel
):
25 def __init__( self
, parent
, log
):
27 wx
.Panel
.__init
__( self
, parent
, -1 )
29 panel
= wx
.Panel( self
, -1 )
31 self
.set_min
= wx
.CheckBox( panel
, -1, "Set minimum value:" )
32 self
.min = wx
.lib
.intctrl
.IntCtrl( panel
, size
=( 50, -1 ) )
33 self
.min.Enable( False )
35 self
.set_max
= wx
.CheckBox( panel
, -1, "Set maximum value:" )
36 self
.max = wx
.lib
.intctrl
.IntCtrl( panel
, size
=( 50, -1 ) )
37 self
.max.Enable( False )
39 self
.limit_target
= wx
.CheckBox( panel
, -1, "Limit control" )
40 self
.allow_none
= wx
.CheckBox( panel
, -1, "Allow empty control" )
41 self
.allow_long
= wx
.CheckBox( panel
, -1, "Allow long integers" )
43 label
= wx
.StaticText( panel
, -1, "Resulting integer control:" )
44 self
.target_ctl
= wx
.lib
.intctrl
.IntCtrl( panel
)
46 grid
= wx
.FlexGridSizer( 0, 2, 0, 0 )
47 grid
.Add( self
.set_min
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
48 grid
.Add( self
.min, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
50 grid
.Add(self
.set_max
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
51 grid
.Add( self
.max, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
53 grid
.Add( self
.limit_target
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
54 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
55 grid
.Add( self
.allow_none
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
56 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
57 grid
.Add( self
.allow_long
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
58 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
60 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
61 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
63 grid
.Add( label
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
64 grid
.Add( self
.target_ctl
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
66 outer_box
= wx
.BoxSizer( wx
.VERTICAL
)
67 outer_box
.AddSizer( grid
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 20 )
69 panel
.SetAutoLayout( True )
70 panel
.SetSizer( outer_box
)
71 outer_box
.Fit( panel
)
75 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMin
, self
.set_min
)
76 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMax
, self
.set_max
)
77 self
.Bind(wx
.EVT_CHECKBOX
, self
.SetTargetMinMax
, self
.limit_target
)
78 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowNone
, self
.allow_none
)
79 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowLong
, self
.allow_long
)
81 self
.Bind(wx
.lib
.intctrl
.EVT_INT
, self
.SetTargetMinMax
, self
.min)
82 self
.Bind(wx
.lib
.intctrl
.EVT_INT
, self
.SetTargetMinMax
, self
.max)
83 self
.Bind(wx
.lib
.intctrl
.EVT_INT
, self
.OnTargetChange
, self
.target_ctl
)
86 def OnSetMin( self
, event
):
87 self
.min.Enable( self
.set_min
.GetValue() )
88 self
.SetTargetMinMax()
90 def OnSetMax( self
, event
):
91 self
.max.Enable( self
.set_max
.GetValue() )
92 self
.SetTargetMinMax()
95 def SetTargetMinMax( self
, event
=None ):
97 self
.target_ctl
.SetLimited( self
.limit_target
.GetValue() )
99 if self
.set_min
.GetValue():
100 min = self
.min.GetValue()
102 if self
.set_max
.GetValue():
103 max = self
.max.GetValue()
105 cur_min
, cur_max
= self
.target_ctl
.GetBounds()
107 if min != cur_min
and not self
.target_ctl
.SetMin( min ):
108 self
.log
.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self
.target_ctl
.GetMax() ) )
109 self
.min.SetForegroundColour( wx
.RED
)
111 self
.min.SetForegroundColour( wx
.BLACK
)
115 if max != cur_max
and not self
.target_ctl
.SetMax( max ):
116 self
.log
.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self
.target_ctl
.GetMin() ) )
117 self
.max.SetForegroundColour( wx
.RED
)
119 self
.max.SetForegroundColour( wx
.BLACK
)
123 if min != cur_min
or max != cur_max
:
124 new_min
, new_max
= self
.target_ctl
.GetBounds()
125 self
.log
.write( "current min, max: (%s, %s)\n" % ( str(new_min
), str(new_max
) ) )
128 def OnSetAllowNone( self
, event
):
129 self
.target_ctl
.SetNoneAllowed( self
.allow_none
.GetValue() )
132 def OnSetAllowLong( self
, event
):
133 self
.target_ctl
.SetLongAllowed( self
.allow_long
.GetValue() )
136 def OnTargetChange( self
, event
):
137 ctl
= event
.GetEventObject()
138 value
= ctl
.GetValue()
139 ib_str
= [ " (out of bounds)", "" ]
140 self
.log
.write( "integer value = %s%s\n" % ( str(value
), ib_str
[ ctl
.IsInBounds(value
) ] ) )
143 #----------------------------------------------------------------------
145 def runTest( frame
, nb
, log
):
146 win
= TestPanel( nb
, log
)
149 #----------------------------------------------------------------------
151 overview
= """<html><body>
153 <B>IntCtrl</B> provides a control that takes and returns integers as
154 value, and provides bounds support and optional value limiting.
157 Here's the API for IntCtrl:
164 <B>limited</B> = False,
165 <B>allow_none</B> = False,
166 <B>allow_long</B> = False,
167 <B>default_color</B> = wxBLACK,
168 <B>oob_color</B> = wxRED,
169 pos = wxDefaultPosition,
170 size = wxDefaultSize,
176 <DD>If no initial value is set, the default will be zero, or
177 the minimum value, if specified. If an illegal string is specified,
178 a ValueError will result. (You can always later set the initial
179 value with SetValue() after instantiation of the control.)
182 <DD>The minimum value that the control should allow. This can be
183 adjusted with SetMin(). If the control is not limited, any value
184 below this bound will be colored with the current out-of-bounds color.
187 <DD>The maximum value that the control should allow. This can be
188 adjusted with SetMax(). If the control is not limited, any value
189 above this bound will be colored with the current out-of-bounds color.
192 <DD>Boolean indicating whether the control prevents values from
193 exceeding the currently set minimum and maximum values (bounds).
194 If <I>False</I> and bounds are set, out-of-bounds values will
195 be colored with the current out-of-bounds color.
197 <DT><B>allow_none</B>
198 <DD>Boolean indicating whether or not the control is allowed to be
199 empty, representing a value of <I>None</I> for the control.
201 <DT><B>allow_long</B>
202 <DD>Boolean indicating whether or not the control is allowed to hold
203 and return a value of type long as well as int. If False, the
204 control will be implicitly limited to have a value such that
205 -sys.maxint-1 <= n <= sys.maxint.
207 <DT><B>default_color</B>
208 <DD>Color value used for in-bounds values of the control.
211 <DD>Color value used for out-of-bounds values of the control
212 when the bounds are set but the control is not limited.
216 <DT><B>EVT_INT(win, id, func)</B>
217 <DD>Respond to a wxEVT_COMMAND_INT_UPDATED event, generated when the
218 value changes. Notice that this event will always be sent when the
219 control's contents changes - whether this is due to user input or
220 comes from the program itself (for example, if SetValue() is called.)
223 <DT><B>SetValue(int)</B>
224 <DD>Sets the value of the control to the integer value specified.
225 The resulting actual value of the control may be altered to
226 conform with the bounds set on the control if limited,
227 or colored if not limited but the value is out-of-bounds.
228 A ValueError exception will be raised if an invalid value
231 <DT><B>GetValue()</B>
232 <DD>Retrieves the integer value from the control. The value
233 retrieved will be sized as an int if possible or a long,
237 <DT><B>SetMin(min=None)</B>
238 <DD>Sets the expected minimum value, or lower bound, of the control.
239 (The lower bound will only be enforced if the control is
240 configured to limit its values to the set bounds.)
241 If a value of <I>None</I> is provided, then the control will have
242 no explicit lower bound. If the value specified is greater than
243 the current lower bound, then the function returns 0 and the
244 lower bound will not change from its current setting. On success,
245 the function returns 1.
246 <DT><DD>If successful and the current value is
247 lower than the new lower bound, if the control is limited, the
248 value will be automatically adjusted to the new minimum value;
249 if not limited, the value in the control will be colored with
250 the current out-of-bounds color.
253 <DD>Gets the current lower bound value for the control.
254 It will return None if no lower bound is currently specified.
257 <DT><B>SetMax(max=None)</B>
258 <DD>Sets the expected maximum value, or upper bound, of the control.
259 (The upper bound will only be enforced if the control is
260 configured to limit its values to the set bounds.)
261 If a value of <I>None</I> is provided, then the control will
262 have no explicit upper bound. If the value specified is less
263 than the current lower bound, then the function returns 0 and
264 the maximum will not change from its current setting. On success,
265 the function returns 1.
266 <DT><DD>If successful and the current value
267 is greater than the new upper bound, if the control is limited
268 the value will be automatically adjusted to the new maximum value;
269 if not limited, the value in the control will be colored with the
270 current out-of-bounds color.
273 <DD>Gets the current upper bound value for the control.
274 It will return None if no upper bound is currently specified.
277 <DT><B>SetBounds(min=None,max=None)</B>
278 <DD>This function is a convenience function for setting the min and max
279 values at the same time. The function only applies the maximum bound
280 if setting the minimum bound is successful, and returns True
281 only if both operations succeed. <B><I>Note:</I></B> leaving out an argument
282 will remove the corresponding bound.
283 <DT><B>GetBounds()</B>
284 <DD>This function returns a two-tuple (min,max), indicating the
285 current bounds of the control. Each value can be None if
286 that bound is not set.
289 <DT><B>IsInBounds(value=None)</B>
290 <DD>Returns <I>True</I> if no value is specified and the current value
291 of the control falls within the current bounds. This function can also
292 be called with a value to see if that value would fall within the current
293 bounds of the given control.
296 <DT><B>SetLimited(bool)</B>
297 <DD>If called with a value of True, this function will cause the control
298 to limit the value to fall within the bounds currently specified.
299 If the control's value currently exceeds the bounds, it will then
300 be limited accordingly.
301 If called with a value of 0, this function will disable value
302 limiting, but coloring of out-of-bounds values will still take
303 place if bounds have been set for the control.
304 <DT><B>IsLimited()</B>
305 <DD>Returns <I>True</I> if the control is currently limiting the
306 value to fall within the current bounds.
309 <DT><B>SetNoneAllowed(bool)</B>
310 <DD>If called with a value of True, this function will cause the control
311 to allow the value to be empty, representing a value of None.
312 If called with a value of false, this function will prevent the value
313 from being None. If the value of the control is currently None,
314 ie. the control is empty, then the value will be changed to that
315 of the lower bound of the control, or 0 if no lower bound is set.
316 <DT><B>IsNoneAllowed()</B>
317 <DD>Returns <I>True</I> if the control currently allows its
321 <DT><B>SetLongAllowed(bool)</B>
322 <DD>If called with a value of True, this function will cause the
323 control to allow the value to be a long. If called with a value
324 of False, and the value of the control is currently a long value,
325 the value of the control will be adjusted to fall within the
326 size of an integer type, at either the sys.maxint or -sys.maxint-1,
327 for positive and negative values, respectively.
328 <DT><B>IsLongAllowed()</B>
329 <DD>Returns <I>True</I> if the control currently allows its
330 value to be of type long.
333 <DT><B>SetColors(default_color=wxBLACK, oob_color=wxRED)</B>
334 <DD>Tells the control what colors to use for normal and out-of-bounds
335 values. If the value currently exceeds the bounds, it will be
336 recolored accordingly.
337 <DT><B>GetColors()</B>
338 <DD>Returns a tuple of <I>(default_color, oob_color)</I> indicating
339 the current color settings for the control.
343 <DD>Will allow cuts to the clipboard of the text portion of the value,
344 leaving the value of zero if the entire contents are "cut."
346 <DD>Will paste the contents of the clipboard to the selected portion
347 of the value; if the resulting string does not represent a legal
348 value, a ValueError will result. If the result is out-of bounds,
349 it will either be adjusted or colored as appropriate.
356 if __name__
== '__main__':
359 run
.main(['', os
.path
.basename(sys
.argv
[0])])