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