]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/wxIntCtrl.py
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.
12 import wx
.lib
.intctrl
as intctrl
14 #----------------------------------------------------------------------
16 class TestPanel( wx
.Panel
):
17 def __init__( self
, parent
, log
):
19 wx
.Panel
.__init
__( self
, parent
, -1 )
21 panel
= wx
.Panel( self
, -1 )
23 self
.set_min
= wx
.CheckBox( panel
, -1, "Set minimum value:" )
24 self
.min = intctrl
.wxIntCtrl( panel
, size
=( 50, -1 ) )
25 self
.min.Enable( False )
27 self
.set_max
= wx
.CheckBox( panel
, -1, "Set maximum value:" )
28 self
.max = intctrl
.wxIntCtrl( panel
, size
=( 50, -1 ) )
29 self
.max.Enable( False )
31 self
.limit_target
= wx
.CheckBox( panel
, -1, "Limit control" )
32 self
.allow_none
= wx
.CheckBox( panel
, -1, "Allow empty control" )
33 self
.allow_long
= wx
.CheckBox( panel
, -1, "Allow long integers" )
35 label
= wx
.StaticText( panel
, -1, "Resulting integer control:" )
36 self
.target_ctl
= intctrl
.wxIntCtrl( panel
)
38 grid
= wx
.FlexGridSizer( 0, 2, 0, 0 )
39 grid
.Add( self
.set_min
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
40 grid
.Add( self
.min, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
42 grid
.Add(self
.set_max
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
43 grid
.Add( self
.max, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
45 grid
.Add( self
.limit_target
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
46 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
47 grid
.Add( self
.allow_none
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
48 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
49 grid
.Add( self
.allow_long
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
50 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
52 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
53 grid
.Add( (20, 0), 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
55 grid
.Add( label
, 0, wx
.ALIGN_LEFT|wx
.ALIGN_CENTER_VERTICAL|wx
.ALL
, 5 )
56 grid
.Add( self
.target_ctl
, 0, wx
.ALIGN_LEFT|wx
.ALL
, 5 )
58 outer_box
= wx
.BoxSizer( wx
.VERTICAL
)
59 outer_box
.AddSizer( grid
, 0, wx
.ALIGN_CENTRE|wx
.ALL
, 20 )
61 panel
.SetAutoLayout( True )
62 panel
.SetSizer( outer_box
)
63 outer_box
.Fit( panel
)
67 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMin
, self
.set_min
)
68 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetMax
, self
.set_max
)
69 self
.Bind(wx
.EVT_CHECKBOX
, self
.SetTargetMinMax
, self
.limit_target
)
70 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowNone
, self
.allow_none
)
71 self
.Bind(wx
.EVT_CHECKBOX
, self
.OnSetAllowLong
, self
.allow_long
)
73 # Once the intctrl library is updated, this should be too.
74 intctrl
.EVT_INT(self
, self
.min.GetId(), self
.SetTargetMinMax
)
75 intctrl
.EVT_INT(self
, self
.max.GetId(), self
.SetTargetMinMax
)
76 intctrl
.EVT_INT(self
, self
.target_ctl
.GetId(), self
.OnTargetChange
)
79 def OnSetMin( self
, event
):
80 self
.min.Enable( self
.set_min
.GetValue() )
81 self
.SetTargetMinMax()
83 def OnSetMax( self
, event
):
84 self
.max.Enable( self
.set_max
.GetValue() )
85 self
.SetTargetMinMax()
88 def SetTargetMinMax( self
, event
=None ):
90 self
.target_ctl
.SetLimited( self
.limit_target
.GetValue() )
92 if self
.set_min
.GetValue():
93 min = self
.min.GetValue()
95 if self
.set_max
.GetValue():
96 max = self
.max.GetValue()
98 cur_min
, cur_max
= self
.target_ctl
.GetBounds()
100 if min != cur_min
and not self
.target_ctl
.SetMin( min ):
101 self
.log
.write( "min (%d) > current max (%d) -- bound not set\n" % ( min, self
.target_ctl
.GetMax() ) )
102 self
.min.SetForegroundColour( wx
.RED
)
104 self
.min.SetForegroundColour( wx
.BLACK
)
108 if max != cur_max
and not self
.target_ctl
.SetMax( max ):
109 self
.log
.write( "max (%d) < current min (%d) -- bound not set\n" % ( max, self
.target_ctl
.GetMin() ) )
110 self
.max.SetForegroundColour( wx
.RED
)
112 self
.max.SetForegroundColour( wx
.BLACK
)
116 if min != cur_min
or max != cur_max
:
117 new_min
, new_max
= self
.target_ctl
.GetBounds()
118 self
.log
.write( "current min, max: (%s, %s)\n" % ( str(new_min
), str(new_max
) ) )
121 def OnSetAllowNone( self
, event
):
122 self
.target_ctl
.SetNoneAllowed( self
.allow_none
.GetValue() )
125 def OnSetAllowLong( self
, event
):
126 self
.target_ctl
.SetLongAllowed( self
.allow_long
.GetValue() )
129 def OnTargetChange( self
, event
):
130 ctl
= event
.GetEventObject()
131 value
= ctl
.GetValue()
132 ib_str
= [ " (out of bounds)", "" ]
133 self
.log
.write( "integer value = %s%s\n" % ( str(value
), ib_str
[ ctl
.IsInBounds(value
) ] ) )
136 #----------------------------------------------------------------------
138 def runTest( frame
, nb
, log
):
139 win
= TestPanel( nb
, log
)
142 #----------------------------------------------------------------------
144 overview
= """<html><body>
146 <B>wxIntCtrl</B> provides a control that takes and returns integers as
147 value, and provides bounds support and optional value limiting.
150 Here's the API for wxIntCtrl:
157 <B>limited</B> = False,
158 <B>allow_none</B> = False,
159 <B>allow_long</B> = False,
160 <B>default_color</B> = wxBLACK,
161 <B>oob_color</B> = wxRED,
162 pos = wxDefaultPosition,
163 size = wxDefaultSize,
169 <DD>If no initial value is set, the default will be zero, or
170 the minimum value, if specified. If an illegal string is specified,
171 a ValueError will result. (You can always later set the initial
172 value with SetValue() after instantiation of the control.)
175 <DD>The minimum value that the control should allow. This can be
176 adjusted with SetMin(). If the control is not limited, any value
177 below this bound will be colored with the current out-of-bounds color.
180 <DD>The maximum value that the control should allow. This can be
181 adjusted with SetMax(). If the control is not limited, any value
182 above this bound will be colored with the current out-of-bounds color.
185 <DD>Boolean indicating whether the control prevents values from
186 exceeding the currently set minimum and maximum values (bounds).
187 If <I>False</I> and bounds are set, out-of-bounds values will
188 be colored with the current out-of-bounds color.
190 <DT><B>allow_none</B>
191 <DD>Boolean indicating whether or not the control is allowed to be
192 empty, representing a value of <I>None</I> for the control.
194 <DT><B>allow_long</B>
195 <DD>Boolean indicating whether or not the control is allowed to hold
196 and return a value of type long as well as int. If False, the
197 control will be implicitly limited to have a value such that
198 -sys.maxint-1 <= n <= sys.maxint.
200 <DT><B>default_color</B>
201 <DD>Color value used for in-bounds values of the control.
204 <DD>Color value used for out-of-bounds values of the control
205 when the bounds are set but the control is not limited.
209 <DT><B>EVT_INT(win, id, func)</B>
210 <DD>Respond to a wxEVT_COMMAND_INT_UPDATED event, generated when the
211 value changes. Notice that this event will always be sent when the
212 control's contents changes - whether this is due to user input or
213 comes from the program itself (for example, if SetValue() is called.)
216 <DT><B>SetValue(int)</B>
217 <DD>Sets the value of the control to the integer value specified.
218 The resulting actual value of the control may be altered to
219 conform with the bounds set on the control if limited,
220 or colored if not limited but the value is out-of-bounds.
221 A ValueError exception will be raised if an invalid value
224 <DT><B>GetValue()</B>
225 <DD>Retrieves the integer value from the control. The value
226 retrieved will be sized as an int if possible or a long,
230 <DT><B>SetMin(min=None)</B>
231 <DD>Sets the expected minimum value, or lower bound, of the control.
232 (The lower bound will only be enforced if the control is
233 configured to limit its values to the set bounds.)
234 If a value of <I>None</I> is provided, then the control will have
235 no explicit lower bound. If the value specified is greater than
236 the current lower bound, then the function returns 0 and the
237 lower bound will not change from its current setting. On success,
238 the function returns 1.
239 <DT><DD>If successful and the current value is
240 lower than the new lower bound, if the control is limited, the
241 value will be automatically adjusted to the new minimum value;
242 if not limited, the value in the control will be colored with
243 the current out-of-bounds color.
246 <DD>Gets the current lower bound value for the control.
247 It will return None if no lower bound is currently specified.
250 <DT><B>SetMax(max=None)</B>
251 <DD>Sets the expected maximum value, or upper bound, of the control.
252 (The upper bound will only be enforced if the control is
253 configured to limit its values to the set bounds.)
254 If a value of <I>None</I> is provided, then the control will
255 have no explicit upper bound. If the value specified is less
256 than the current lower bound, then the function returns 0 and
257 the maximum will not change from its current setting. On success,
258 the function returns 1.
259 <DT><DD>If successful and the current value
260 is greater than the new upper bound, if the control is limited
261 the value will be automatically adjusted to the new maximum value;
262 if not limited, the value in the control will be colored with the
263 current out-of-bounds color.
266 <DD>Gets the current upper bound value for the control.
267 It will return None if no upper bound is currently specified.
270 <DT><B>SetBounds(min=None,max=None)</B>
271 <DD>This function is a convenience function for setting the min and max
272 values at the same time. The function only applies the maximum bound
273 if setting the minimum bound is successful, and returns True
274 only if both operations succeed. <B><I>Note:</I></B> leaving out an argument
275 will remove the corresponding bound.
276 <DT><B>GetBounds()</B>
277 <DD>This function returns a two-tuple (min,max), indicating the
278 current bounds of the control. Each value can be None if
279 that bound is not set.
282 <DT><B>IsInBounds(value=None)</B>
283 <DD>Returns <I>True</I> if no value is specified and the current value
284 of the control falls within the current bounds. This function can also
285 be called with a value to see if that value would fall within the current
286 bounds of the given control.
289 <DT><B>SetLimited(bool)</B>
290 <DD>If called with a value of True, this function will cause the control
291 to limit the value to fall within the bounds currently specified.
292 If the control's value currently exceeds the bounds, it will then
293 be limited accordingly.
294 If called with a value of 0, this function will disable value
295 limiting, but coloring of out-of-bounds values will still take
296 place if bounds have been set for the control.
297 <DT><B>IsLimited()</B>
298 <DD>Returns <I>True</I> if the control is currently limiting the
299 value to fall within the current bounds.
302 <DT><B>SetNoneAllowed(bool)</B>
303 <DD>If called with a value of True, this function will cause the control
304 to allow the value to be empty, representing a value of None.
305 If called with a value of false, this function will prevent the value
306 from being None. If the value of the control is currently None,
307 ie. the control is empty, then the value will be changed to that
308 of the lower bound of the control, or 0 if no lower bound is set.
309 <DT><B>IsNoneAllowed()</B>
310 <DD>Returns <I>True</I> if the control currently allows its
314 <DT><B>SetLongAllowed(bool)</B>
315 <DD>If called with a value of True, this function will cause the
316 control to allow the value to be a long. If called with a value
317 of False, and the value of the control is currently a long value,
318 the value of the control will be adjusted to fall within the
319 size of an integer type, at either the sys.maxint or -sys.maxint-1,
320 for positive and negative values, respectively.
321 <DT><B>IsLongAllowed()</B>
322 <DD>Returns <I>True</I> if the control currently allows its
323 value to be of type long.
326 <DT><B>SetColors(default_color=wxBLACK, oob_color=wxRED)</B>
327 <DD>Tells the control what colors to use for normal and out-of-bounds
328 values. If the value currently exceeds the bounds, it will be
329 recolored accordingly.
330 <DT><B>GetColors()</B>
331 <DD>Returns a tuple of <I>(default_color, oob_color)</I> indicating
332 the current color settings for the control.
336 <DD>Will allow cuts to the clipboard of the text portion of the value,
337 leaving the value of zero if the entire contents are "cut."
339 <DD>Will paste the contents of the clipboard to the selected portion
340 of the value; if the resulting string does not represent a legal
341 value, a ValueError will result. If the result is out-of bounds,
342 it will either be adjusted or colored as appropriate.
349 if __name__
== '__main__':
352 run
.main(['', os
.path
.basename(sys
.argv
[0])])