+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);
+}
+
+// ----------------------------------------------------------------------------
+// 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.
+
+ if ( !wxWindowBase::Reparent(newParent) )
+ return false;
+
+ newParent->GetChildren().DeleteObject(this);
+
+ // preserve the old values
+ const wxSize size = GetSize();
+ int value = GetValue();
+ const wxRect btnRect = wxRectFromRECT(wxGetWindowRect(GetHwnd()));
+
+ // destroy the old spin button
+ UnsubclassWin();
+ if ( !::DestroyWindow(GetHwnd()) )
+ wxLogLastError(wxT("DestroyWindow"));
+
+ // create and initialize the new one
+ if ( !wxSpinButton::Create(GetParent(), GetId(),
+ btnRect.GetPosition(), btnRect.GetSize(),
+ GetWindowStyle(), GetName()) )
+ return false;
+
+ SetValue(value);
+ SetRange(m_min, m_max);
+ SetInitialSize(size);
+
+ // 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;
+ }
+
+ ::EnableWindow(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 );