]> git.saurik.com Git - wxWidgets.git/commitdiff
fixed event generation for wxChoice: it now sends one and exactly one wxEVT_COMMAND_C...
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 12 Mar 2006 20:20:16 +0000 (20:20 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 12 Mar 2006 20:20:16 +0000 (20:20 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@38042 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

src/msw/choice.cpp

index 698c61b25962e3a439728a8a3eae50652d99dfdf..e35fab7117b04d6c7171aee8e629500fb3186d44 100644 (file)
@@ -633,23 +633,61 @@ WXLRESULT wxChoice::MSWWindowProc(WXUINT nMsg, WXWPARAM wParam, WXLPARAM lParam)
 
 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
 {
 
 bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
 {
+    /*
+        The native control provides a great variety in the events it sends in
+        the different selection scenarios (undoubtedly for greater amusement of
+        the programmers using it). For the reference, here are the cases when
+        the final selection is accepted (things are quite interesting when it
+        is cancelled too):
+
+        A. Selecting with just the arrows without opening the dropdown:
+            1. CBN_SELENDOK
+            2. CBN_SELCHANGE
+
+        B. Opening dropdown with F4 and selecting with arrows:
+            1. CBN_DROPDOWN
+            2. many CBN_SELCHANGE while changing selection in the list
+            3. CBN_SELENDOK
+            4. CBN_CLOSEUP
+
+        C. Selecting with the mouse:
+            1. CBN_DROPDOWN
+            -- no intermediate CBN_SELCHANGEs --
+            2. CBN_SELENDOK
+            3. CBN_CLOSEUP
+            4. CBN_SELCHANGE
+
+        Admire the different order of messages in all of those cases, it must
+        surely have taken a lot of effort to Microsoft developers to achieve
+        such originality.
+     */
     switch ( param )
     {
         case CBN_DROPDOWN:
     switch ( param )
     {
         case CBN_DROPDOWN:
-            // we don't want to track selection using CB_GETCURSEL while the
-            // dropdown is opened
+            // we use this value both because we don't want to track selection
+            // using CB_GETCURSEL while the dropdown is opened and because we
+            // need to reset the selection back to it if it's eventually
+            // cancelled by user
             m_lastAcceptedSelection = GetCurrentSelection();
             break;
 
         case CBN_CLOSEUP:
             m_lastAcceptedSelection = GetCurrentSelection();
             break;
 
         case CBN_CLOSEUP:
-            // it should be safe to use CB_GETCURSEL again
-            m_lastAcceptedSelection = wxID_NONE;
+            // if the selection was accepted by the user, it should have been
+            // reset to wxID_NONE by CBN_SELENDOK, otherwise the selection was
+            // cancelled and we must restore the old one
+            if ( m_lastAcceptedSelection != wxID_NONE )
+            {
+                SetSelection(m_lastAcceptedSelection);
+                m_lastAcceptedSelection = wxID_NONE;
+            }
             break;
 
             break;
 
-        case CBN_SELCHANGE:
-            // don't generate any events while the dropdown is opened as the
-            // selection is not final yet
-            if ( m_lastAcceptedSelection == wxID_NONE )
+        case CBN_SELENDOK:
+            // reset it to prevent CBN_CLOSEUP from undoing the selection (it's
+            // ok to reset it now as GetCurrentSelection() will now return the
+            // same thing anyhow)
+            m_lastAcceptedSelection = wxID_NONE;
+
             {
                 const int n = GetSelection();
 
             {
                 const int n = GetSelection();
 
@@ -668,10 +706,19 @@ bool wxChoice::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
 
                 ProcessCommand(event);
             }
 
                 ProcessCommand(event);
             }
-            return true;
+            break;
+
+        // don't handle CBN_SELENDCANCEL: just leave m_lastAcceptedSelection
+        // valid and the selection will be undone in CBN_CLOSEUP above
+
+        // don't handle CBN_SELCHANGE neither, we don't want to generate events
+        // while the dropdown is opened -- but do add it if we ever need this
+
+        default:
+            return false;
     }
 
     }
 
-    return false;
+    return true;
 }
 
 WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd)
 }
 
 WXHBRUSH wxChoice::MSWControlColor(WXHDC hDC, WXHWND hWnd)