]> git.saurik.com Git - wxWidgets.git/commitdiff
Ensure that Enter key presses are never stolen from wxButton in wxMSW.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sun, 11 Dec 2011 17:03:56 +0000 (17:03 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sun, 11 Dec 2011 17:03:56 +0000 (17:03 +0000)
This commit fixes the following bug: when an in-place editor control containing
an embedded button was used in wxDataViewCtrl, pressing Enter on the button
would close the editor, accepting changes, instead as (generic) wxDataViewCtrl
intercepts WXK_RETURN in its EVT_CHAR_HOOK handler. To prevent this from
happening, wxButton now handles EVT_CHAR_HOOK itself and never lets the parent
window intercept it if it's for WXK_RETURN. To ensure that normal
wxEVT_KEY_DOWN and wxEVT_CHAR are still generated in this case, wxButton
handler calls the new wxKeyEvent::DoAllowNextEvent() method that was added to
allow suppressing EVT_CHAR_HOOK only, without affecting the subsequent events.
DoAllowNextEvent() is currently only used in wxMSW but support for it was also
added to wxGTK and (both) wxOSX ports.

See #9102.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@69984 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/event.h
include/wx/msw/button.h
interface/wx/event.h
src/gtk/window.cpp
src/msw/button.cpp
src/msw/window.cpp
src/osx/carbon/app.cpp
src/osx/cocoa/window.mm

index 7b4d53c6a29d9b058da9711718f04b1506214d72..fb45b71c550ed95a88ba8d8e2d3c782bf240f748 100644 (file)
@@ -1707,6 +1707,15 @@ public:
     // Get Y position
     wxCoord GetY() const { return m_y; }
 
+    // Can be called from wxEVT_CHAR_HOOK handler to allow generation of normal
+    // key events even though the event had been handled (by default they would
+    // not be generated in this case).
+    void DoAllowNextEvent() { m_allowNext = true; }
+
+    // Return the value of the "allow next" flag, for internal use only.
+    bool IsNextEventAllowed() const { return m_allowNext; }
+
+
     virtual wxEvent *Clone() const { return new wxKeyEvent(*this); }
     virtual wxEventCategory GetEventCategory() const { return wxEVT_CATEGORY_USER_INPUT; }
 
@@ -1750,6 +1759,8 @@ private:
     {
         if ( m_eventType == wxEVT_CHAR_HOOK )
             m_propagationLevel = wxEVENT_PROPAGATE_MAX;
+
+        m_allowNext = false;
     }
 
     // Copy only the event data present in this class, this is used by
@@ -1768,6 +1779,11 @@ private:
 #endif
     }
 
+    // If this flag is true, the normal key events should still be generated
+    // even if wxEVT_CHAR_HOOK had been handled. By default it is false as
+    // handling wxEVT_CHAR_HOOK suppresses all the subsequent events.
+    bool m_allowNext;
+
     DECLARE_DYNAMIC_CLASS(wxKeyEvent)
 };
 
index ebf260e87f05191dc4f8eed19820fe86ae897282..d1f4c4e36474ec97af0b0aec2aa2edccb0149a26 100644 (file)
@@ -77,6 +77,9 @@ private:
         m_authNeeded = false;
     }
 
+    void OnCharHook(wxKeyEvent& event);
+
+    wxDECLARE_EVENT_TABLE();
     wxDECLARE_DYNAMIC_CLASS_NO_COPY(wxButton);
 };
 
index 31b9606d717f418f911324918cc78e8a954fb1a8..5cb9c497aa53d11c9d1ae44e1c5b4019284c32ad 100644 (file)
@@ -1373,13 +1373,19 @@ enum wxKeyCategoryFlags
         events and so gives the parent window an opportunity to modify the
         keyboard handling of its children, e.g. it is used internally by
         wxWidgets in some ports to intercept pressing Esc key in any child of a
-        dialog to close the dialog itself when it's pressed. If the event is
-        handled, i.e. the handler doesn't call wxEvent::Skip(), neither @c
-        wxEVT_KEY_DOWN nor @c wxEVT_CHAR events will be generated (although @c
-        wxEVT_KEY_UP still will be). Notice that this event is not generated
-        when the mouse is captured as it is considered that the window which
-        has the capture should receive all the keyboard events too without
-        allowing its parent wxTopLevelWindow to interfere with their processing.
+        dialog to close the dialog itself when it's pressed. By default, if
+        this event is handled, i.e. the handler doesn't call wxEvent::Skip(),
+        neither @c wxEVT_KEY_DOWN nor @c wxEVT_CHAR events will be generated
+        (although @c wxEVT_KEY_UP still will be), i.e. it replaces the normal
+        key events. However by calling the special DoAllowNextEvent() method
+        you can handle @c wxEVT_CHAR_HOOK and still allow normal events
+        generation. This is something that is rarely useful but can be required
+        if you need to prevent a parent @c wxEVT_CHAR_HOOK handler from running
+        without suppressing the normal key events. Finally notice that this
+        event is not generated when the mouse is captured as it is considered
+        that the window which has the capture should receive all the keyboard
+        events too without allowing its parent wxTopLevelWindow to interfere
+        with their processing.
     @endEventTable
 
     @see wxKeyboardState
@@ -1522,6 +1528,34 @@ public:
         Returns the Y position (in client coordinates) of the event.
     */
     wxCoord GetY() const;
+
+    /**
+        Allow normal key events generation.
+
+        Can be called from @c wxEVT_CHAR_HOOK handler to indicate that the
+        generation of normal events should @em not be suppressed, as it happens
+        by default when this event is handled.
+
+        The intended use of this method is to allow some window object to
+        prevent @c wxEVT_CHAR_HOOK handler in its parent window from running by
+        defining its own handler for this event. Without calling this method,
+        this would result in not generating @c wxEVT_KEY_DOWN nor @c wxEVT_CHAR
+        events at all but by calling it you can ensure that these events would
+        still be generated, even if @c wxEVT_CHAR_HOOK event was handled.
+
+        @since 2.9.3
+     */
+    void DoAllowNextEvent();
+
+    /**
+        Returns @true if DoAllowNextEvent() had been called, @false by default.
+
+        This method is used by wxWidgets itself to determine whether the normal
+        key events should be generated after @c wxEVT_CHAR_HOOK processing.
+
+        @since 2.9.3
+     */
+    bool IsNextEventAllowed() const;
 };
 
 
index f7ef4a6db08a9b8b7479fa8f67766fa437028a17..fbbbd782e319f51b70cc4679ba4895d8ef4c7ce6 100644 (file)
@@ -830,7 +830,8 @@ bool SendCharHookEvent(const wxKeyEvent& event, wxWindow *win)
     if ( !g_captureWindow )
     {
         wxKeyEvent eventCharHook(wxEVT_CHAR_HOOK, event);
-        if ( win->HandleWindowEvent(eventCharHook) )
+        if ( win->HandleWindowEvent(eventCharHook)
+                && !event.IsNextEventAllowed() )
             return true;
     }
 
index e87917bd6420e870c91974721c93c80c28490916..3e9bfb5739e9358e37df4c1b06e6d2c0cd8deedf 100644 (file)
 // macros
 // ----------------------------------------------------------------------------
 
+BEGIN_EVENT_TABLE(wxButton, wxButtonBase)
+    EVT_CHAR_HOOK(wxButton::OnCharHook)
+END_EVENT_TABLE()
+
 // ============================================================================
 // implementation
 // ============================================================================
@@ -370,6 +374,25 @@ void wxButton::Command(wxCommandEvent & event)
 // event/message handlers
 // ----------------------------------------------------------------------------
 
+void wxButton::OnCharHook(wxKeyEvent& event)
+{
+    // We want to ensure that the button always processes Enter key events
+    // itself, even if it's inside some control that normally takes over them
+    // (this happens when the button is part of an in-place editor control for
+    // example).
+    if ( event.GetKeyCode() == WXK_RETURN )
+    {
+        // We should ensure that subsequent key events are still generated even
+        // if we did handle EVT_CHAR_HOOK (normally this would suppress their
+        // generation).
+        event.DoAllowNextEvent();
+    }
+    else
+    {
+        event.Skip();
+    }
+}
+
 bool wxButton::MSWCommand(WXUINT param, WXWORD WXUNUSED(id))
 {
     bool processed = false;
index 506bc5175a65433f0174b05444d17fe9621e5b6e..c7bdf169d2048b6382488f01a6b8375388225d4a 100644 (file)
@@ -6644,8 +6644,11 @@ wxKeyboardHook(int nCode, WORD wParam, DWORD lParam)
 
                 if ( handler && handler->ProcessEvent(event) )
                 {
-                    // processed
-                    return 1;
+                    if ( !event.IsNextEventAllowed() )
+                    {
+                        // Stop processing of this event.
+                        return 1;
+                    }
                 }
             }
         }
index d2378b1df97e72359f2a3b79da50050cf6be1816..95b1ab78b05be0a7a88ab4a342927cecc63b0c55 100644 (file)
@@ -1650,7 +1650,7 @@ bool wxApp::MacSendCharEvent( wxWindow* focus , long keymessage , long modifiers
     {
         wxKeyEvent eventCharHook(wxEVT_CHAR_HOOK, event);
         handled = focus->HandleWindowEvent( eventCharHook );
-        if ( handled && eventCharHook.GetSkipped() )
+        if ( handled && eventCharHook.IsNextEventAllowed() )
             handled = false ;
     }
 
index 84f694dd02d076e608a1e1db6f75e6c2816fbb9e..3adaae15f40ef844c766f60accc17903b7d10d5d 100644 (file)
@@ -2306,7 +2306,8 @@ bool wxWidgetCocoaImpl::DoHandleKeyEvent(NSEvent *event)
     if ( wxevent.GetEventType() == wxEVT_KEY_DOWN )
     {
         wxKeyEvent eventHook(wxEVT_CHAR_HOOK, wxevent);
-        if ( GetWXPeer()->OSXHandleKeyEvent(eventHook) )
+        if ( GetWXPeer()->OSXHandleKeyEvent(eventHook)
+                && !eventHook.IsNextEventAllowed() )
             return true;
     }