+// ----------------------------------------------------------------------------
+// wnd proc for the buddy text ctrl
+// ----------------------------------------------------------------------------
+
+LRESULT APIENTRY _EXPORT wxBuddyTextWndProc(HWND hwnd,
+ UINT message,
+ WPARAM wParam,
+ LPARAM lParam)
+{
+ wxSpinCtrl * const spin = wxSpinCtrl::GetSpinForTextCtrl(hwnd);
+
+ // forward some messages (mostly the key and focus ones) to the spin ctrl
+ switch ( message )
+ {
+ case WM_SETFOCUS:
+ // if the focus comes from the spin control itself, don't set it
+ // back to it -- we don't want to go into an infinite loop
+ if ( (WXHWND)wParam == spin->GetHWND() )
+ break;
+ //else: fall through
+
+ case WM_KILLFOCUS:
+ case WM_CHAR:
+ case WM_DEADCHAR:
+ case WM_KEYUP:
+ case WM_KEYDOWN:
+#ifdef WM_HELP
+ // we need to forward WM_HELP too to ensure that the context help
+ // associated with wxSpinCtrl is shown when the text control part of it
+ // is clicked with the "?" cursor
+ case WM_HELP:
+#endif
+ {
+ WXLRESULT result;
+ if ( spin->MSWHandleMessage(&result, message, wParam, lParam) )
+ {
+ // Do not let the message be processed by the window proc
+ // of the text control if it had been already handled at wx
+ // level, this is consistent with what happens for normal,
+ // non-composite controls.
+ return 0;
+ }
+
+ // The control may have been deleted at this point, so check.
+ if ( !::IsWindow(hwnd) )
+ return 0;
+ }
+ break;
+
+ case WM_GETDLGCODE:
+ if ( spin->HasFlag(wxTE_PROCESS_ENTER) )
+ {
+ long dlgCode = ::CallWindowProc
+ (
+ CASTWNDPROC spin->GetBuddyWndProc(),
+ hwnd,
+ message,
+ wParam,
+ lParam
+ );
+ dlgCode |= DLGC_WANTMESSAGE;
+ return dlgCode;
+ }
+ break;
+ }
+
+ return ::CallWindowProc(CASTWNDPROC spin->GetBuddyWndProc(),
+ hwnd, message, wParam, lParam);
+}
+
+/* static */
+wxSpinCtrl *wxSpinCtrl::GetSpinForTextCtrl(WXHWND hwndBuddy)
+{
+ const SpinForTextCtrl::const_iterator
+ it = gs_spinForTextCtrl.find(hwndBuddy);
+ if ( it == gs_spinForTextCtrl.end() )
+ return NULL;
+
+ wxSpinCtrl * const spin = it->second;
+
+ // sanity check
+ wxASSERT_MSG( spin->m_hwndBuddy == hwndBuddy,
+ wxT("wxSpinCtrl has incorrect buddy HWND!") );
+
+ return spin;
+}
+
+// process a WM_COMMAND generated by the buddy text control
+bool wxSpinCtrl::ProcessTextCommand(WXWORD cmd, WXWORD WXUNUSED(id))
+{
+ if ( (cmd == EN_CHANGE) && (!m_blockEvent ))
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, GetId());
+ event.SetEventObject(this);
+ wxString val = wxGetWindowText(m_hwndBuddy);
+ event.SetString(val);
+ event.SetInt(GetValue());
+ return HandleWindowEvent(event);
+ }
+
+ // not processed
+ return false;
+}
+
+void wxSpinCtrl::OnChar(wxKeyEvent& event)
+{
+ switch ( event.GetKeyCode() )
+ {
+ case WXK_RETURN:
+ {
+ wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, m_windowId);
+ InitCommandEvent(event);
+ wxString val = wxGetWindowText(m_hwndBuddy);
+ event.SetString(val);
+ event.SetInt(GetValue());
+ if ( HandleWindowEvent(event) )
+ return;
+ break;
+ }
+
+ case WXK_TAB:
+ // always produce navigation event - even if we process TAB
+ // ourselves the fact that we got here means that the user code
+ // decided to skip processing of this TAB - probably to let it
+ // do its default job.
+ {
+ wxNavigationKeyEvent eventNav;
+ eventNav.SetDirection(!event.ShiftDown());
+ eventNav.SetWindowChange(event.ControlDown());
+ eventNav.SetEventObject(this);
+
+ if ( GetParent()->HandleWindowEvent(eventNav) )
+ return;
+ }
+ break;
+ }
+
+ // no, we didn't process it
+ 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
+ // we do
+ ::SetFocus((HWND)m_hwndBuddy);
+
+ event.Skip();
+}
+
+void wxSpinCtrl::NormalizeValue()
+{
+ const int value = GetValue();
+ const bool changed = value != m_oldValue;
+
+ // notice that we have to call SetValue() even if the value didn't change
+ // because otherwise we could be left with empty buddy control when value
+ // is 0, see comment in SetValue()
+ SetValue(value);
+
+ if ( changed )
+ {
+ SendSpinUpdate(value);
+ }
+}
+