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