X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/283587a491a718c8f568f70a143c9b1b2992306b..f52eff546ab16990767e06e25c00b3d8fd2729f9:/src/msw/spinctrl.cpp?ds=sidebyside diff --git a/src/msw/spinctrl.cpp b/src/msw/spinctrl.cpp index b33dee669b..80d15e665b 100644 --- a/src/msw/spinctrl.cpp +++ b/src/msw/spinctrl.cpp @@ -1,5 +1,5 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: msw/spinctrl.cpp +// Name: src/msw/spinctrl.cpp // Purpose: wxSpinCtrl class implementation for Win32 // Author: Vadim Zeitlin // Modified by: @@ -13,11 +13,6 @@ // declarations // ============================================================================ -#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) - #pragma implementation "spinctrlbase.h" - #pragma implementation "spinctrl.h" -#endif - // ---------------------------------------------------------------------------- // headers // ---------------------------------------------------------------------------- @@ -29,15 +24,17 @@ #pragma hdrstop #endif -#ifndef WX_PRECOMP - #include "wx/wx.h" -#endif - #if wxUSE_SPINCTRL #include "wx/spinctrl.h" + +#ifndef WX_PRECOMP + #include "wx/msw/wrapcctl.h" // include "properly" + #include "wx/event.h" + #include "wx/textctrl.h" +#endif + #include "wx/msw/private.h" -#include "wx/msw/wrapcctl.h" #if wxUSE_TOOLTIPS #include "wx/tooltip.h" @@ -45,9 +42,6 @@ #include // for INT_MIN -#define USE_DEFERRED_SIZING 1 -#define USE_DEFER_BUG_WORKAROUND 0 - // ---------------------------------------------------------------------------- // macros // ---------------------------------------------------------------------------- @@ -117,11 +111,12 @@ wxCONSTRUCTOR_6( wxSpinCtrl , wxWindow* , Parent , wxWindowID , Id , wxString , IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl) #endif +//pmg EVT_KILL_FOCUS BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton) EVT_CHAR(wxSpinCtrl::OnChar) EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus) - + EVT_KILL_FOCUS(wxSpinCtrl::OnKillFocus) EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange) END_EVENT_TABLE() @@ -205,28 +200,14 @@ wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy) // process a WM_COMMAND generated by the buddy text control bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id)) { - switch (cmd) + if ( cmd == EN_CHANGE ) { - case EN_CHANGE: - { - wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); - event.SetEventObject(this); - wxString val = wxGetWindowText(m_hwndBuddy); - event.SetString(val); - event.SetInt(GetValue()); - return GetEventHandler()->ProcessEvent(event); - } - case EN_SETFOCUS: - case EN_KILLFOCUS: - { - wxFocusEvent event(cmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS - : wxEVT_SET_FOCUS, - m_windowId); - event.SetEventObject( this ); - return GetEventHandler()->ProcessEvent(event); - } - default: - break; + wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId()); + event.SetEventObject(this); + wxString val = wxGetWindowText(m_hwndBuddy); + event.SetString(val); + event.SetInt(GetValue()); + return GetEventHandler()->ProcessEvent(event); } // not processed @@ -270,6 +251,13 @@ void wxSpinCtrl::OnChar(wxKeyEvent& event) event.Skip(); } +void wxSpinCtrl::OnKillFocus(wxFocusEvent& event) +{ + // ensure that a correct value is shown by the control + NormalizeValue(); + event.Skip(); +} + void wxSpinCtrl::OnSetFocus(wxFocusEvent& event) { // when we get focus, give it to our buddy window as it needs it more than @@ -279,6 +267,20 @@ void wxSpinCtrl::OnSetFocus(wxFocusEvent& event) event.Skip(); } +void wxSpinCtrl::NormalizeValue() +{ + int value = GetValue(); + SetValue( value ); + if (value != m_oldValue) + { + wxCommandEvent event( wxEVT_COMMAND_SPINCTRL_UPDATED, GetId() ); + event.SetEventObject( this ); + event.SetInt( value ); + GetEventHandler()->ProcessEvent( event ); + m_oldValue = value; + } +} + // ---------------------------------------------------------------------------- // construction // ---------------------------------------------------------------------------- @@ -366,6 +368,8 @@ bool wxSpinCtrl::Create(wxWindow *parent, SetRange(min, max); SetValue(initial); + + m_oldValue = initial; // subclass the text ctrl to be able to intercept some events wxSetWindowUserData(GetBuddyHwnd(), this); @@ -387,14 +391,14 @@ bool wxSpinCtrl::Create(wxWindow *parent, sizeText.y = EDIT_HEIGHT_FROM_CHAR_HEIGHT(cy); } - SetBestSize(size); + SetInitialSize(size); (void)::ShowWindow(GetBuddyHwnd(), SW_SHOW); // associate the text window with the spin button (void)::SendMessage(GetHwnd(), UDM_SETBUDDY, (WPARAM)m_hwndBuddy, 0); - if ( !value.IsEmpty() ) + if ( !value.empty() ) { SetValue(value); } @@ -431,16 +435,36 @@ void wxSpinCtrl::SetValue(const wxString& text) } } +void wxSpinCtrl::SetValue(int val) +{ + wxSpinButton::SetValue(val); + + // normally setting the value of the spin button is enough as it updates + // its buddy control automatically ... + if ( wxGetWindowText(m_hwndBuddy).empty() ) + { + // ... but sometimes it doesn't, notably when the value is 0 and the + // text control is currently empty, the spin button seems to be happy + // to leave it like this, while we really want to always show the + // current value in the control, so do it manually + ::SetWindowText(GetBuddyHwnd(), wxString::Format(_T("%d"), val)); + } + + m_oldValue = GetValue(); +} + int wxSpinCtrl::GetValue() const { wxString val = wxGetWindowText(m_hwndBuddy); long n; - if ( (wxSscanf(val, wxT("%lu"), &n) != 1) ) + if ( (wxSscanf(val, wxT("%ld"), &n) != 1) ) n = INT_MIN; - - if (n < m_min) n = m_min; - if (n > m_max) n = m_max; + + if ( n < m_min ) + n = m_min; + if ( n > m_max ) + n = m_max; return n; } @@ -454,7 +478,7 @@ void wxSpinCtrl::SetSelection(long from, long to) from = 0; } - ::SendMessage((HWND)m_hwndBuddy, EM_SETSEL, (WPARAM)from, (LPARAM)to); + ::SendMessage(GetBuddyHwnd(), EM_SETSEL, (WPARAM)from, (LPARAM)to); } // ---------------------------------------------------------------------------- @@ -524,14 +548,18 @@ void wxSpinCtrl::OnSpinChange(wxSpinEvent& eventSpin) { wxCommandEvent event(wxEVT_COMMAND_SPINCTRL_UPDATED, GetId()); event.SetEventObject(this); - event.SetInt(eventSpin.GetPosition()); - - (void)GetEventHandler()->ProcessEvent(event); + int value = eventSpin.GetPosition(); + event.SetInt( value ); + + if (value != m_oldValue) + (void)GetEventHandler()->ProcessEvent(event); if ( eventSpin.GetSkipped() ) { event.Skip(); } + + m_oldValue = value; } // ---------------------------------------------------------------------------- @@ -570,57 +598,17 @@ void wxSpinCtrl::DoMoveWindow(int x, int y, int width, int height) wxLogDebug(_T("not enough space for wxSpinCtrl!")); } - // if our parent had prepared a defer window handle for us, use it (unless - // we are a top level window) - wxWindowMSW *parent = GetParent(); - int originalX = x; - -#if USE_DEFERRED_SIZING - HDWP hdwp = parent && !IsTopLevel() ? (HDWP)parent->m_hDWP : NULL; -#else - HDWP hdwp = 0; -#endif - // 1) The buddy window - wxMoveWindowDeferred(hdwp, this, GetBuddyHwnd(), - x, y, widthText, height); + DoMoveSibling(m_hwndBuddy, x, y, widthText, height); // 2) The button window x += widthText + MARGIN_BETWEEN; - wxMoveWindowDeferred(hdwp, this, GetHwnd(), - x, y, widthBtn, height); - - if (hdwp) - { - // Store the size so we can report it accurately - wxExtraWindowData* extraData = (wxExtraWindowData*) m_windowReserved; - if (!extraData) - { - extraData = new wxExtraWindowData; - m_windowReserved = (void*) extraData; - } - extraData->m_pos = wxPoint(originalX, y); - extraData->m_size = wxSize(width, height); - extraData->m_deferring = true; - - // hdwp must be updated as it may have been changed - parent->m_hDWP = (WXHANDLE)hdwp; - } + wxSpinButton::DoMoveWindow(x, y, widthBtn, height); } // get total size of the control void wxSpinCtrl::DoGetSize(int *x, int *y) const { -#if USE_DEFER_BUG_WORKAROUND - wxExtraWindowData* extraData = (wxExtraWindowData*) m_windowReserved; - if (extraData && extraData->m_deferring && GetParent() && GetParent()->m_hDWP) - { - *x = extraData->m_size.x; - *y = extraData->m_size.y; - return; - } -#endif - RECT spinrect, textrect, ctrlrect; GetWindowRect(GetHwnd(), &spinrect); GetWindowRect(GetBuddyHwnd(), &textrect); @@ -634,16 +622,6 @@ void wxSpinCtrl::DoGetSize(int *x, int *y) const void wxSpinCtrl::DoGetPosition(int *x, int *y) const { -#if USE_DEFER_BUG_WORKAROUND - wxExtraWindowData* extraData = (wxExtraWindowData*) m_windowReserved; - if (extraData && extraData->m_deferring && GetParent() && GetParent()->m_hDWP) - { - *x = extraData->m_pos.x; - *y = extraData->m_pos.y; - return; - } -#endif - // hack: pretend that our HWND is the text control just for a moment WXHWND hWnd = GetHWND(); wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hwndBuddy; @@ -654,4 +632,3 @@ void wxSpinCtrl::DoGetPosition(int *x, int *y) const } #endif // wxUSE_SPINCTRL -