can I add numeric-only support to wxSpinCtrl?

75 views
Skip to first unread message

David Norris

unread,
Mar 14, 2005, 12:07:57 AM3/14/05
to wx-u...@lists.wxwidgets.org
All,

wxSpinCtrl is very convenient, but it does lack one feature: it
doesn't prevent the user from typing non-numeric characters into the
text box. What would the developers think about adding a style flag to
wxSpinCtrl that causes it to drop non-numeric WM_CHAR events? I've
looked at the wxSpinCtrl code and I don't think it should be very hard
to do, but I want a second opinion.

If anyone agrees this would be a good thing and wouldn't cause any
problems, I would like to work on this and submit a patch, as I need
this ability in a program I'm writing.

I also found, in the wx-users archive, that someone has written a
"wxIntCtrl" that apparently does exactly this. Is anyone using this?
If I do work on adding this style to the mainstream wxSpinCtrl, is
there anything else neat about the wxIntCtrl that I should keep in
mind?

Thanks,
--
David Norris
dano...@gmail.com

Milan Babuskov

unread,
Mar 16, 2005, 1:43:59 AM3/16/05
to wx-u...@lists.wxwidgets.org
David Elliott wrote:
> Interesting. I have a wxSpinTextCtrl that derives from wxTextCtrl and
> has a wxSpinButton instead of the other way around. This means normal
> validators can be used (and it defaults to a numeric one). I'll send it
> to you in a private mail. You may use it under the wxWidgets license.

Is there any reason why it is not included in official distribution?

It sounds to me that it should replace wxSpinCtrl.

I also don't understand why is text in wxSpinCtrl left aligned? The
control is showing numbers, so right-alignment seems like only
reasonable option.

--
Milan Babuskov
http://fbexport.sourceforge.net
http://www.flamerobin.org


David Norris

unread,
Mar 15, 2005, 8:56:33 PM3/15/05
to wx-u...@lists.wxwidgets.org
On Tue, 15 Mar 2005 17:24:26 -0500, David Elliott <ell...@stcnet.com> wrote:
> Interesting. I have a wxSpinTextCtrl that derives from wxTextCtrl and
> has a wxSpinButton instead of the other way around. This means normal
> validators can be used (and it defaults to a numeric one). I'll send
> it to you in a private mail. You may use it under the wxWidgets
> license.

David,

Thanks! I haven't had a chance to take a look at it yet but I'll let
you know when I do... it sounds like exactly what I was wanting.

Gratefully,
--
David Norris
dano...@gmail.com

David Elliott

unread,
Mar 16, 2005, 11:57:04 AM3/16/05
to wx-u...@lists.wxwidgets.org
On Mar 16, 2005, at 11:50 AM, Milan Babuskov wrote:

> David Elliott wrote:
>> No problem! On account of "popular" demand I've put this up on
>> tgwbd.org.
>> http://www.tgwbd.org/downloads/wxSpinTextCtrl/
>> so I'm interested to know if it still works, particularly with all
>> the sizing changes.
>
> I tried to add it to my project as one of the files. Is that possible?
>
> Using wxMSW 2.5.4 I get:
>
> Error E2321 .\src\spintext.h 105:
> Declaration does not specify a tag or an identifier
>
> Line 105 is:
> DECLARE_DYNAMIC_CLASS(wxSpinTextCtrl);
>
> Do I have to do something special to add it to project, or I have to
> somehow compile it with wx sources? And how would I go about that?
>
No, I think you just have to remove the semicolon. I use gcc and it
doesn't complain about this.

-Dave


David Norris

unread,
Mar 16, 2005, 2:49:51 PM3/16/05
to wx-u...@lists.wxwidgets.org
On Tue, 15 Mar 2005 17:24:26 -0500, David Elliott <ell...@stcnet.com> wrote:
> Interesting. I have a wxSpinTextCtrl that derives from wxTextCtrl and
> has a wxSpinButton instead of the other way around. This means normal
> validators can be used (and it defaults to a numeric one). I'll send
> it to you in a private mail. You may use it under the wxWidgets
> license.

David,

I got it to build in CVS HEAD; I just had to wrap all the strings in
wxT() to make the Unicode build work. However, there are some quirks,
at least in GTK:

- up/down arrow keys don't work
- the spinner is too tall and gets clipped at the bottom edge, only a
few pixels of the down button are visible

But otherwise it works great. I imagine both of these problems are
easy to fix and I'll see what I can do. If I get them fixed I'll
return the modified sources to you.

To clarify the license, can I put your source files into a GPL'd app
I'm writing (assuming I can fix those two quirks)?

In any case, thank you very much for the code :)

--
David Norris
dano...@gmail.com

David Norris

unread,
Mar 16, 2005, 2:57:59 PM3/16/05
to wx-u...@lists.wxwidgets.org
On Wed, 16 Mar 2005 13:49:51 -0600, David Norris <dano...@gmail.com> wrote:
> - up/down arrow keys don't work

Ah hah, I see a /* TODO */ in OnChar() :-) I'll fill it in...

> - the spinner is too tall and gets clipped at the bottom edge, only a
> few pixels of the down button are visible

I think I have this fixed. Seems to be working in GTK. I'll test it on
MSW this evening and send you the changes.

Thanks again!!

--
David Norris
dano...@gmail.com

Milan Babuskov

unread,
Mar 16, 2005, 4:35:19 PM3/16/05
to wx-u...@lists.wxwidgets.org
David Elliott wrote:
>> Line 105 is:
>> DECLARE_DYNAMIC_CLASS(wxSpinTextCtrl);
>>
>> Do I have to do something special to add it to project, or I have to
>> somehow compile it with wx sources? And how would I go about that?
>>
> No, I think you just have to remove the semicolon. I use gcc and it
> doesn't complain about this.

That was the problem (I use Borland). So, it does work with wx2.5.4.
Beside removing the semicolon I didn't have to touch anything.

It works very good. Only numbers can be entered, and what I like the
most, wxTextCtrl is the main control, so when I set the tooltip for
wxSpinTextCtrl user does not have to go to Spin to see the tip.
Although, I believe the correct behaviour would be to set the tooltip
for both controls.

If it would have option to right-align it, it would be perfect :)

David Norris

unread,
Mar 16, 2005, 4:46:25 PM3/16/05
to wx-u...@lists.wxwidgets.org
On Wed, 16 Mar 2005 22:35:19 +0100, Milan Babuskov <mil...@kms.co.yu> wrote:
> That was the problem (I use Borland). So, it does work with wx2.5.4.
> Beside removing the semicolon I didn't have to touch anything.
>
> It works very good. Only numbers can be entered, and what I like the
> most, wxTextCtrl is the main control, so when I set the tooltip for
> wxSpinTextCtrl user does not have to go to Spin to see the tip.
> Although, I believe the correct behaviour would be to set the tooltip
> for both controls.
>
> If it would have option to right-align it, it would be perfect :)

I can add this for you. I'll also make the tooltips set for both
controls. David, I'm afraid that I've gone and changed quite a bit in
the process of adding a couple of features... I hope you won't be
offended at what I've done to your code... :-/

I'll post it as soon as I test on MSW.

--
David Norris
dano...@gmail.com

Milan Babuskov

unread,
Mar 17, 2005, 5:37:20 AM3/17/05
to wx-u...@lists.wxwidgets.org
David Norris wrote:
>>>If it would have option to right-align it, it would be perfect :)
>
> In fact it should already work -- just pass wxTE_RIGHT

It works :)

Great, thanx.

Milan Babuskov

unread,
Mar 17, 2005, 12:18:31 PM3/17/05
to wx-u...@lists.wxwidgets.org
David Norris wrote:
>>- are you sure an integer value is appropriate for initial value?
>
> It's appropriate *for me* :-)
>
> IMHO the ideal solution is in fact to keep the current wxSpinCtrl, and
> simply make it so that you can use a wxGenericValidator with it, or
> maybe add a wxSpinCtrlValidator or something. The validator should
> accept ints or floats, and in each case it should make the control
> only accept the right characters.

I wasn't talking about validators, that is fine. I was talking about
initial value. I can't use wxSpinTextCtrl constructor with doubles.
Anyway, it's really no big deal, so you may just as well forget I
wrote anything :)

>>- where do we have to apply so that this goes in main wx tree, and
>>replace (now obsolete?) wxSpinCtrl?
>
> Another issue is that this wxSpinTextCtrl is a generic control.

I wasn't aware of this.

> wxSpinCtrl has native definitions on a few platforms (notably GTK). So
> a really correct fix would need to keep those native controls around,
> not replace them. The right thing to do IMHO is to work on the
> existing wxSpinCtrl to add the validator support. Then it's backward
> compatible and uses native controls when applicable.

Makes sense.

> Thoughts?

I hope someone else is reading this discussion too.

Well, until (if at all) changes are made to wx lib, I think I'll
simply include wxSpinTextCtrl in my project and use it. I might even
add some features like support for floating-point numbers with
properties like "number of decimals".

David Norris

unread,
Mar 17, 2005, 12:47:52 PM3/17/05
to wx-u...@lists.wxwidgets.org
On Thu, 17 Mar 2005 12:29:42 -0500, David Elliott <ell...@stcnet.com> wrote:
> > I wasn't talking about validators, that is fine. I was talking about
> > initial value. I can't use wxSpinTextCtrl constructor with doubles.
> > Anyway, it's really no big deal, so you may just as well forget I
> > wrote anything :)
> >
> I *think* with my version you could do this as you could provide your
> own validator for the wxTextCtrl component. With David Norris's
> changes it now creates the validator in the constructor and the
> validator you provide is for the wxSpinCtrl not the wxTextCtrl.

You're right, in your original version you can pass in any validator
that a wxTextCtrl will accept. But I don't think it will behave
correctly if you use a float validator. The current code expects
everything to be an integer. ToLong() will fail for e.g. "3.14159,"
and anytime you press the spin button it will be reset to the last
known good integer plus or minus one. Wouldn't be too terribly hard to
fix this though.

I changed the validators around because it was more convenient (for me
at least) to be able to use wxGenericValidator(int) for the
wxSpinTextCtrl as a whole. I don't think wxTextCtrl will accept a
wxGenericValidator, will it?

Clearly there's a lot of room for improvement here. Again I think the
right way to do it is for the control to handle the validator directly
instead of passing it through to either of the two subcontrols. (Also
remember that there may only be one subcontrol if native controls are
used, like in GTK). The controller code in wxSpinTextCtrl would then
need to be smart enough to do the right thing with the subcontrols
depending on which validator (int, float, etc.) is used. Or maybe it
would be useful to create a new validator for this purpose?

Other potential additions could be support for other bases (hex,
octal, binary), although I'm not sure how many people would actually
need this. Probably not worth the time up front, but it might be worth
thinking about so that it can be designed in a way that allows this to
be added later.

--
David Norris
dano...@gmail.com

David Norris

unread,
Mar 18, 2005, 12:01:46 PM3/18/05
to wx-u...@lists.wxwidgets.org
On Fri, 18 Mar 2005 13:40:50 +0100, Milan Babuskov <mil...@kms.co.yu> wrote:
> David Norris wrote:
> > But I don't see anything wrong with making it a virtual method in case
> > the user does want to do something special (maybe having the spin
> > control iterate through an array of string values?)
>
> He could use List control with height = 1item.

Ah, true. Well, I can't think of any cases where you'd need to
override an intelligent default then, but it can't hurt to make the
virtual method just in case!

> > Was it possibly because of ints vs. shorts? I noticed in your code
> > that the range was set in one place to be [min int, max int] but in
> > another place to be [min short, max short]. Maybe at one time in the
> > past wxSpinButton only used shorts for the range
>
> It still does for people who have older versions of comctl32.dll, like on
> Windows98?

Really? I know the default GETPOS/SETPOS messages took shorts, but I
thought the GETPOS32 variants were always available. MSDN documents it
as being available since Win95, although it's been known to lie. I do
see that there are #ifdefs in the wxSpinButton source to fall back to
GETPOS if the 32-bit variant isn't defined. I wonder if it's
necessary.

> I'm not so sure, but I did try now on Windows98 SE, and it works.

But in any case, even if it does only support shorts, you'd only see
errors if you tried to get/set a value above 32767 or below -32768. In
other words it should work fine within the intersection of the
interval [minint, maxint] and the interval supported by the native
control. Should be sufficient for *most* apps.

--
David Norris
dano...@gmail.com

Milan Babuskov

unread,
Mar 21, 2005, 9:30:27 AM3/21/05
to wx-u...@lists.wxwidgets.org
David Norris wrote:
>>I really like the OnKillFocus() => SyncSpinToText() feature.
>>
>>While experimenting with wxSpinTextCtrl I noticed it is missing. David, would
>>you consider adding it?
>
> Not sure which David you mean, as David Elliott is the wxSpinTextCtrl
> author and I just made some changes...

Any-one ;)

> I don't personally have time to
> work on it anymore right now (I've overcommitted myself to some other
> things) but I do want to do it at some point. Hopefully next week I'll
> have a bit more time to play around.
>
> Until then there's nothing stopping anyone else from making more changes!

Well, I don't need it right now, since my project is far from release time,
but I might add it later if nobody does in the meantime.

Reply all
Reply to author
Forward
0 new messages