+ m_blockEvent = false;
+}
+
+int wxSpinCtrl::GetValue() const
+{
+ wxString val = wxGetWindowText(m_hwndBuddy);
+
+ long n;
+ if ( (wxSscanf(val, wxT("%ld"), &n) != 1) )
+ n = INT_MIN;
+
+ if ( n < m_min )
+ n = m_min;
+ if ( n > m_max )
+ n = m_max;
+
+ return n;
+}
+
+void wxSpinCtrl::SetSelection(long from, long to)
+{
+ // if from and to are both -1, it means (in wxWidgets) that all text should
+ // be selected - translate into Windows convention
+ if ( (from == -1) && (to == -1) )
+ {
+ from = 0;
+ }
+
+ ::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
+// ----------------------------------------------------------------------------
+
+bool wxSpinCtrl::SetFont(const wxFont& font)
+{
+ if ( !wxWindowBase::SetFont(font) )
+ {
+ // nothing to do
+ return false;
+ }
+
+ WXHANDLE hFont = GetFont().GetResourceHandle();
+ (void)::SendMessage(GetBuddyHwnd(), WM_SETFONT, (WPARAM)hFont, TRUE);
+
+ return true;
+}
+
+bool wxSpinCtrl::Show(bool show)
+{
+ if ( !wxControl::Show(show) )
+ {
+ return false;
+ }
+
+ ::ShowWindow(GetBuddyHwnd(), show ? SW_SHOW : SW_HIDE);
+
+ return true;
+}
+
+bool wxSpinCtrl::Reparent(wxWindowBase *newParent)
+{
+ // Reparenting both the updown control and its buddy does not seem to work:
+ // they continue to be connected somehow, but visually there is no feedback
+ // on the buddy edit control. To avoid this problem, we reparent the buddy
+ // window normally, but we recreate the updown control and reassign its
+ // buddy.
+
+ // Get the position before changing the parent as it would be offset after
+ // changing it.
+ const wxRect rect = GetRect();
+
+ if ( !wxWindowBase::Reparent(newParent) )
+ return false;
+
+ newParent->GetChildren().DeleteObject(this);
+
+ // destroy the old spin button after detaching it from this wxWindow object
+ // (notice that m_hWnd will be reset by UnsubclassWin() so save it first)
+ const HWND hwndOld = GetHwnd();
+ UnsubclassWin();
+ if ( !::DestroyWindow(hwndOld) )
+ {
+ wxLogLastError(wxT("DestroyWindow"));
+ }
+
+ // create and initialize the new one
+ if ( !wxSpinButton::Create(GetParent(), GetId(),
+ rect.GetPosition(), rect.GetSize(),
+ GetWindowStyle(), GetName()) )
+ return false;
+
+ // reapply our values to wxSpinButton
+ wxSpinButton::SetValue(GetValue());
+ SetRange(m_min, m_max);
+
+ // also set the size again with wxSIZE_ALLOW_MINUS_ONE flag: this is
+ // necessary if our original position used -1 for either x or y
+ SetSize(rect, wxSIZE_ALLOW_MINUS_ONE);
+
+ // associate it with the buddy control again
+ ::SetParent(GetBuddyHwnd(), GetHwndOf(GetParent()));
+ (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)GetBuddyHwnd(), 0);
+
+ return true;
+}
+
+bool wxSpinCtrl::Enable(bool enable)
+{
+ if ( !wxControl::Enable(enable) )
+ {
+ return false;
+ }
+
+ MSWEnableHWND(GetBuddyHwnd(), enable);
+
+ return true;
+}
+
+void wxSpinCtrl::SetFocus()
+{
+ ::SetFocus(GetBuddyHwnd());
+}
+
+#if wxUSE_TOOLTIPS
+
+void wxSpinCtrl::DoSetToolTip(wxToolTip *tip)
+{
+ wxSpinButton::DoSetToolTip(tip);
+
+ if ( tip )
+ tip->Add(m_hwndBuddy);
+}
+
+#endif // wxUSE_TOOLTIPS
+
+// ----------------------------------------------------------------------------
+// events processing and generation
+// ----------------------------------------------------------------------------
+
+void wxSpinCtrl::SendSpinUpdate(int value)
+{
+ wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId());
+ event.SetEventObject(this);
+ event.SetInt(value);
+
+ (void)HandleWindowEvent(event);
+
+ m_oldValue = value;
+}
+
+bool wxSpinCtrl::MSWOnScroll(int WXUNUSED(orientation), WXWORD wParam,
+ WXWORD pos, WXHWND control)
+{
+ wxCHECK_MSG( control, false, wxT("scrolling what?") );
+
+ if ( wParam != SB_THUMBPOSITION )
+ {
+ // probable SB_ENDSCROLL - we don't react to it
+ return false;
+ }
+
+ int new_value = (short) pos;
+ if (m_oldValue != new_value)
+ SendSpinUpdate( new_value );
+
+ return TRUE;
+}
+
+bool wxSpinCtrl::MSWOnNotify(int WXUNUSED(idCtrl), WXLPARAM lParam, WXLPARAM *result)
+{
+ NM_UPDOWN *lpnmud = (NM_UPDOWN *)lParam;
+
+ if (lpnmud->hdr.hwndFrom != GetHwnd()) // make sure it is the right control
+ return false;
+
+ *result = 0; // never reject UP and DOWN events