From 345ff9c65b3ef17709785708b224dfcbf5135583 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sun, 23 Aug 2009 22:48:35 +0000 Subject: [PATCH] Allow entering minus sign in wxMSW wxSpinCtrl if needed. Only restrict input to wxMSW wxSpinCtrl to digits only if it doesn't support negative values, otherwise the user can't enter any negative numbers. As there is no built in support for entering digits and minus sign only, simply allow all characters to be used if the negative numbers must indeed be possible to enter. Closes #11082. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@61754 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/msw/spinctrl.h | 1 + src/msw/spinctrl.cpp | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/include/wx/msw/spinctrl.h b/include/wx/msw/spinctrl.h index 01715a13c6..26fb596c73 100644 --- a/include/wx/msw/spinctrl.h +++ b/include/wx/msw/spinctrl.h @@ -67,6 +67,7 @@ public: virtual void SetValue(int val); virtual int GetValue() const; + virtual void SetRange(int minVal, int maxVal); virtual bool SetFont(const wxFont &font); virtual void SetFocus(); diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index 60238bdf76..80416bf320 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -341,11 +341,6 @@ bool wxSpinCtrl::Create(wxWindow *parent, else if ( style & wxALIGN_CENTER ) msStyle |= ES_CENTER; - // this control is used for numeric entry so normally using these flags by - // default shouldn't be a problem, if it is we can always add a style such - // as wxSP_NON_NUMERIC later - msStyle |= ES_NUMBER; - // calculate the sizes: the size given is the total size for both controls // and we need to fit them both in the given width (height is the same) wxSize sizeText(size), sizeBtn(size); @@ -529,6 +524,28 @@ void wxSpinCtrl::SetSelection(long from, long to) ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); } +// ---------------------------------------------------------------------------- +// wxSpinButton methods +// ---------------------------------------------------------------------------- + +void wxSpinCtrl::SetRange(int minVal, int maxVal) +{ + wxSpinButton::SetRange(minVal, maxVal); + + // this control is used for numeric entry so restrict the input to numeric + // keys only -- but only if we don't need to be able to enter "-" in it as + // otherwise this would become impossible + const DWORD styleOld = ::GetWindowLong(GetBuddyHwnd(), GWL_STYLE); + DWORD styleNew; + if ( minVal < 0 ) + styleNew = styleOld & ~ES_NUMBER; + else + styleNew = styleOld | ES_NUMBER; + + if ( styleNew != styleOld ) + ::SetWindowLong(GetBuddyHwnd(), GWL_STYLE, styleNew); +} + // ---------------------------------------------------------------------------- // forward some methods to subcontrols // ---------------------------------------------------------------------------- -- 2.45.2