]> git.saurik.com Git - wxWidgets.git/blobdiff - src/os2/spinctrl.cpp
Fix explicitly setting focus to generic wxSpinCtrl.
[wxWidgets.git] / src / os2 / spinctrl.cpp
index 2fa04e704975bcbb73591d6ac5d9f1d592d9b038..c9d8187d9444bd7a0adee11f200061757b401769 100644 (file)
@@ -1,6 +1,6 @@
 /////////////////////////////////////////////////////////////////////////////
-// Name:        msw/spinctrl.cpp
-// Purpose:     wxSpinCtrl class implementation for Win32
+// Name:        src/os2/spinctrl.cpp
+// Purpose:     wxSpinCtrl class implementation for OS/2
 // Author:      David Webster
 // Modified by:
 // Created:     10/15/99
 // declarations
 // ============================================================================
 
-
-#ifdef __GNUG__
-    #pragma implementation "spinctrlbase.h"
-    #pragma implementation "spinctrl.h"
-#endif
-
 // ----------------------------------------------------------------------------
 // headers
 // ----------------------------------------------------------------------------
@@ -31,7 +25,7 @@
     #include "wx/wx.h"
 #endif
 
-#if wxUSE_SPINBTN
+#if wxUSE_SPINCTRL
 
 #include "wx/spinctrl.h"
 #include "wx/os2/private.h"
@@ -49,7 +43,9 @@ wxArraySpins                        wxSpinCtrl::m_svAllSpins;
 IMPLEMENT_DYNAMIC_CLASS(wxSpinCtrl, wxControl)
 
 BEGIN_EVENT_TABLE(wxSpinCtrl, wxSpinButton)
-    EVT_SPIN(-1, wxSpinCtrl::OnSpinChange)
+    EVT_CHAR(wxSpinCtrl::OnChar)
+    EVT_SPIN(wxID_ANY, wxSpinCtrl::OnSpinChange)
+    EVT_SET_FOCUS(wxSpinCtrl::OnSetFocus)
 END_EVENT_TABLE()
 // ----------------------------------------------------------------------------
 // constants
@@ -71,8 +67,7 @@ MRESULT EXPENTRY wxSpinCtrlWndProc(
     wxSpinCtrl*                    pSpin = (wxSpinCtrl *)::WinQueryWindowULong( hWnd
                                                                                ,QWL_USER
                                                                               );
-    bool                            bProccesed = FALSE;
-    MRESULT                         rc = (MRESULT)0;
+
     //
     // Forward some messages (the key ones only so far) to the spin ctrl
     //
@@ -116,32 +111,31 @@ wxSpinCtrl::~wxSpinCtrl()
 // construction
 // ----------------------------------------------------------------------------
 
-bool wxSpinCtrl::Create(
-  wxWindow*                         pParent
-, wxWindowID                        vId
-, const wxString&                   rsValue
-, const wxPoint&                    rPos
-, const wxSize&                     rSize
-, long                              lStyle
-, int                               nMin
-, int                               nMax
-, int                               nInitial
-, const wxString&                   rsName
-)
+bool wxSpinCtrl::Create( wxWindow*       pParent,
+                         wxWindowID      vId,
+                         const wxString& WXUNUSED(rsValue),
+                         const wxPoint&  rPos,
+                         const wxSize&   rSize,
+                         long            lStyle,
+                         int             nMin,
+                         int             nMax,
+                         int             nInitial,
+                         const wxString& rsName )
 {
-    SWP                             vSwp;
-
-    if (vId == -1)
+    if (vId == wxID_ANY)
         m_windowId = NewControlId();
     else
         m_windowId = vId;
-    m_backgroundColour = pParent->GetBackgroundColour();
-    m_foregroundColour = pParent->GetForegroundColour();
+    if (pParent)
+    {
+        m_backgroundColour = pParent->GetBackgroundColour();
+        m_foregroundColour = pParent->GetForegroundColour();
+    }
     SetName(rsName);
     SetParent(pParent);
     m_windowStyle      = lStyle;
 
-    int                             lSstyle = 0L;
+    int lSstyle = 0L;
 
     lSstyle = WS_VISIBLE      |
               WS_TABSTOP      |
@@ -173,25 +167,16 @@ bool wxSpinCtrl::Create(
                                       );
     if (m_hWnd == 0)
     {
-        return FALSE;
+        return false;
     }
     m_hWndBuddy = m_hWnd; // One in the same for OS/2
     if(pParent)
         pParent->AddChild((wxSpinButton *)this);
-    wxFont*                          pTextFont = new wxFont( 10
-                                                            ,wxMODERN
-                                                            ,wxNORMAL
-                                                            ,wxNORMAL
-                                                           );
-    SetFont(*pTextFont);
-    ::WinQueryWindowPos(m_hWnd, &vSwp);
-    SetXComp(vSwp.x);
-    SetYComp(vSwp.y);
-    SetSize( rPos.x
-            ,rPos.y
-            ,rSize.x
-            ,rSize.y
-           );
+
+    SetFont(*wxSMALL_FONT);
+    SetXComp(0);
+    SetYComp(0);
+    SetSize( rPos.x, rPos.y, rSize.x, rSize.y );
 
     SetRange(nMin, nMax);
     SetValue(nInitial);
@@ -205,23 +190,23 @@ bool wxSpinCtrl::Create(
     ::WinSetWindowULong(GetHwnd(), QWL_USER, (LONG)this);
     fnWndProcSpinCtrl = (WXFARPROC)::WinSubclassWindow(m_hWnd, (PFNWP)wxSpinCtrlWndProc);
     m_svAllSpins.Add(this);
-    delete pTextFont;
-    return TRUE;
+    return true;
 } // end of wxSpinCtrl::Create
 
 wxSize wxSpinCtrl::DoGetBestSize() const
 {
     wxSize                          vSizeBtn = wxSpinButton::DoGetBestSize();
     int                             nHeight;
+    wxFont                          vFont = (wxFont)GetFont();
 
     vSizeBtn.x += DEFAULT_ITEM_WIDTH + MARGIN_BETWEEN;
 
     wxGetCharSize( GetHWND()
                   ,NULL
                   ,&nHeight
-                  ,(wxFont*)&GetFont()
+                  ,&vFont
                  );
-    nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight);
+    nHeight = EDIT_HEIGHT_FROM_CHAR_HEIGHT(nHeight)+4;
 
     if (vSizeBtn.y < nHeight)
     {
@@ -238,13 +223,7 @@ void wxSpinCtrl::DoGetPosition(
 , int*                              pnY
 ) const
 {
-    WXHWND                          hWnd = GetHWND();
-
-    wxConstCast(this, wxSpinCtrl)->m_hWnd = m_hWndBuddy;
-    wxSpinButton::DoGetPosition( pnX
-                                ,pnY
-                               );
-    wxConstCast(this, wxSpinCtrl)->m_hWnd = hWnd;
+    wxSpinButton::DoGetPosition( pnX,pnY );
 } // end of wxpinCtrl::DoGetPosition
 
 void wxSpinCtrl::DoGetSize(
@@ -300,10 +279,10 @@ bool wxSpinCtrl::Enable(
 {
     if (!wxControl::Enable(bEnable))
     {
-        return FALSE;
+        return false;
     }
     ::WinEnableWindow(GetHwnd(), bEnable);
-    return TRUE;
+    return true;
 } // end of wxSpinCtrl::Enable
 
 wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
@@ -320,7 +299,7 @@ wxSpinCtrl* wxSpinCtrl::GetSpinForTextCtrl(
 
     // sanity check
     wxASSERT_MSG( pSpin->m_hWndBuddy == hWndBuddy,
-                  _T("wxSpinCtrl has incorrect buddy HWND!") );
+                  wxT("wxSpinCtrl has incorrect buddy HWND!") );
 
     return pSpin;
 } // end of wxSpinCtrl::GetSpinForTextCtrl
@@ -345,7 +324,7 @@ void wxSpinCtrl::OnChar (
   wxKeyEvent&                       rEvent
 )
 {
-    switch (rEvent.KeyCode())
+    switch (rEvent.GetKeyCode())
     {
         case WXK_RETURN:
             {
@@ -355,9 +334,9 @@ void wxSpinCtrl::OnChar (
                 wxString                    sVal = wxGetWindowText(m_hWndBuddy);
 
                 InitCommandEvent(vEvent);
-                vEvent.SetString((char*)sVal.c_str());
+                vEvent.SetString(sVal);
                 vEvent.SetInt(GetValue());
-                if (GetEventHandler()->ProcessEvent(vEvent))
+                if (HandleWindowEvent(vEvent))
                     return;
                 break;
             }
@@ -375,7 +354,7 @@ void wxSpinCtrl::OnChar (
                 vEventNav.SetDirection(!rEvent.ShiftDown());
                 vEventNav.SetWindowChange(rEvent.ControlDown());
                 vEventNav.SetEventObject(this);
-                if (GetParent()->GetEventHandler()->ProcessEvent(vEventNav))
+                if (GetParent()->HandleWindowEvent(vEventNav))
                     return;
             }
             break;
@@ -397,43 +376,51 @@ void wxSpinCtrl::OnSpinChange(
 
     vEvent.SetEventObject(this);
     vEvent.SetInt(rEventSpin.GetPosition());
-    (void)GetEventHandler()->ProcessEvent(vEvent);
+    (void)HandleWindowEvent(vEvent);
     if (rEventSpin.GetSkipped())
     {
         vEvent.Skip();
     }
 } // end of wxSpinCtrl::OnSpinChange
 
-bool wxSpinCtrl::ProcessTextCommand(
-  WXWORD                            wCmd
-, WXWORD                            wId
+void wxSpinCtrl::OnSetFocus (
+  wxFocusEvent&                     rEvent
 )
+{
+    //
+    // When we get focus, give it to our buddy window as it needs it more than
+    // we do
+    //
+    ::WinSetFocus(HWND_DESKTOP, (HWND)m_hWndBuddy);
+    rEvent.Skip();
+} // end of wxSpinCtrl::OnSetFocus
+
+bool wxSpinCtrl::ProcessTextCommand( WXWORD wCmd,
+                                     WXWORD WXUNUSED(wId) )
 {
     switch (wCmd)
     {
         case SPBN_CHANGE:
         {
-            wxCommandEvent          vEvent( wxEVT_COMMAND_TEXT_UPDATED
-                                           ,GetId()
-                                          );
+            wxCommandEvent vEvent( wxEVT_COMMAND_TEXT_UPDATED, GetId() );
             vEvent.SetEventObject(this);
 
-            wxString                sVal = wxGetWindowText(m_hWndBuddy);
+            wxString sVal = wxGetWindowText(m_hWndBuddy);
 
-            vEvent.SetString((char*)sVal.c_str());
+            vEvent.SetString(sVal);
             vEvent.SetInt(GetValue());
-            return (GetEventHandler()->ProcessEvent(vEvent));
+            return (HandleWindowEvent(vEvent));
         }
 
         case SPBN_SETFOCUS:
         case SPBN_KILLFOCUS:
         {
-            wxFocusEvent                vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS
-                                               ,m_windowId
-                                              );
+            wxFocusEvent vEvent( wCmd == EN_KILLFOCUS ? wxEVT_KILL_FOCUS : wxEVT_SET_FOCUS
+                                ,m_windowId
+                               );
 
             vEvent.SetEventObject(this);
-            return(GetEventHandler()->ProcessEvent(vEvent));
+            return(HandleWindowEvent(vEvent));
         }
         default:
             break;
@@ -442,7 +429,7 @@ bool wxSpinCtrl::ProcessTextCommand(
     //
     // Not processed
     //
-    return FALSE;
+    return false;
 } // end of wxSpinCtrl::ProcessTextCommand
 
 void wxSpinCtrl::SetFocus()
@@ -457,14 +444,13 @@ bool wxSpinCtrl::SetFont(
     if (!wxWindowBase::SetFont(rFont))
     {
         // nothing to do
-        return FALSE;
+        return false;
     }
 
-    WXHANDLE                        hFont = GetFont().GetResourceHandle();
     wxOS2SetFont( m_hWnd
                  ,rFont
                 );
-    return TRUE;
+    return true;
 } // end of wxSpinCtrl::SetFont
 
 void wxSpinCtrl::SetValue(
@@ -483,9 +469,25 @@ bool wxSpinCtrl::Show(
 {
     if (!wxControl::Show(bShow))
     {
-        return FALSE;
+        return false;
     }
-    return TRUE;
+    return true;
 } // end of wxSpinCtrl::Show
 
-#endif //wxUSE_SPINBTN
+void wxSpinCtrl::SetSelection (
+  long                              lFrom
+, long                              lTo
+)
+{
+    //
+    // If from and to are both -1, it means (in wxWidgets) that all text should
+    // be selected - translate into Windows convention
+    //
+    if ((lFrom == -1) && (lTo == -1))
+    {
+        lFrom = 0;
+    }
+    ::WinSendMsg(m_hWnd, EM_SETSEL, MPFROM2SHORT((USHORT)lFrom, (USHORT)lTo), (MPARAM)0);
+} // end of wxSpinCtrl::SetSelection
+
+#endif //wxUSE_SPINCTRL