]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/combobox.cpp
Fix overflow warnings (?)
[wxWidgets.git] / src / msw / combobox.cpp
index 5ac0ec4d7286aafd2fdb4c674244af0e06d60871..c14341c0019261ce5aa98854ebc55e38687cbf77 100644 (file)
@@ -213,27 +213,6 @@ LRESULT APIENTRY _EXPORT wxComboEditWndProc(HWND hWnd,
     return ::CallWindowProc(CASTWNDPROC gs_wndprocEdit, hWnd, message, wParam, lParam);
 }
 
-WXHBRUSH wxComboBox::OnCtlColor(WXHDC pDC,
-                                WXHWND WXUNUSED(pWnd),
-                                WXUINT WXUNUSED(nCtlColor),
-                                WXUINT WXUNUSED(message),
-                                WXWPARAM WXUNUSED(wParam),
-                                WXLPARAM WXUNUSED(lParam))
-{
-    HDC hdc = (HDC)pDC;
-    wxColour colBack = GetBackgroundColour();
-
-    if (!IsEnabled())
-        colBack = wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE);
-
-    ::SetBkColor(hdc, wxColourToRGB(colBack));
-    ::SetTextColor(hdc, wxColourToRGB(GetForegroundColour()));
-
-    wxBrush *brush = wxTheBrushList->FindOrCreateBrush(colBack, wxSOLID);
-
-    return (WXHBRUSH)brush->GetResourceHandle();
-}
-
 // ----------------------------------------------------------------------------
 // wxComboBox callbacks
 // ----------------------------------------------------------------------------
@@ -244,16 +223,13 @@ WXLRESULT wxComboBox::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lPara
     // colour correctly (to be the same as our own one)
     switch ( nMsg )
     {
-        // we have to handle both: one for the normal case and the other for
-        // wxCB_READONLY
-        case WM_CTLCOLOREDIT:
-        case WM_CTLCOLORSTATIC:
-            WXWORD nCtlColor;
-            WXHDC hdc;
-            WXHWND hwnd;
-            UnpackCtlColor(wParam, lParam, &nCtlColor, &hdc, &hwnd);
-
-            return (WXLRESULT)OnCtlColor(hdc, hwnd, nCtlColor, nMsg, wParam, lParam);
+        case CB_SETCURSEL:
+            // Selection was set with SetSelection.  Update the value too.
+            if ((int)wParam > GetCount())
+                m_value = wxEmptyString;
+            else
+                m_value = GetString(wParam);
+            break;
     }
 
     return wxChoice::MSWWindowProc(nMsg, wParam, lParam);
@@ -301,18 +277,30 @@ bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
     {
         case CBN_SELCHANGE:
             sel = GetSelection();
-            if ( sel > -1 )
+
+            // somehow we get 2 CBN_SELCHANGE events with the same index when
+            // the user selects an item in the combobox -- ignore duplicates
+            if ( sel > -1 && sel != m_selectionOld )
             {
-                value = GetString(sel);
+                m_selectionOld = sel;
+
+                // GetValue() would still return the old value from here but
+                // according to the docs we should return the new value if the
+                // user calls it in his event handler, so update internal
+                // m_value
+                m_value = GetString(sel);
 
                 wxCommandEvent event(wxEVT_COMMAND_COMBOBOX_SELECTED, GetId());
                 event.SetInt(sel);
                 event.SetEventObject(this);
-                event.SetString(value);
+                event.SetString(m_value);
                 ProcessCommand(event);
             }
-            else
+            else // no valid selection
             {
+                m_selectionOld = sel;
+
+                // hence no EVT_TEXT neither
                 break;
             }
 
@@ -329,25 +317,21 @@ bool wxComboBox::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
                 // want the new one)
                 if ( sel == -1 )
                 {
-                    value = GetValue();
+                    m_value = wxGetWindowText(GetHwnd());
                 }
                 else // we're synthesizing text updated event from sel change
                 {
-                    // We need to retrieve the current selection because the user
-                    // may have changed it in the previous handler (for CBN_SELCHANGE
-                    // above).
+                    // We need to retrieve the current selection because the
+                    // user may have changed it in the previous handler (for
+                    // CBN_SELCHANGE above).
                     sel = GetSelection();
                     if ( sel > -1 )
                     {
-                        value = GetString(sel);
+                        m_value = GetString(sel);
                     }
-                    // we need to do this because the user code expects
-                    // wxComboBox::GetValue() to return the new value from
-                    // "text updated" handler but it hadn't been updated yet
-                    SetValue(value);
                 }
 
-                event.SetString(value);
+                event.SetString(m_value);
                 event.SetEventObject(this);
                 ProcessCommand(event);
             }
@@ -472,6 +456,9 @@ void wxComboBox::SetValue(const wxString& value)
         SetStringSelection(value);
     else
         SetWindowText(GetHwnd(), value.c_str());
+
+    m_value = value;
+    m_selectionOld = GetSelection();
 }
 
 // Clipboard operations
@@ -587,5 +574,19 @@ void wxComboBox::SetSelection(long from, long to)
     }
 }
 
+void wxComboBox::GetSelection(long* from, long* to) const
+{
+    DWORD dwStart, dwEnd;
+    ::SendMessage(GetHwnd(), CB_GETEDITSEL, (WPARAM)&dwStart, (LPARAM)&dwEnd);
+
+    *from = dwStart;
+    *to = dwEnd;
+}
+
+int wxComboBox::GetSelection() const
+{   
+    return wxChoice::GetSelection();    
+}
+
 #endif // wxUSE_COMBOBOX