]> git.saurik.com Git - wxWidgets.git/commitdiff
Avoid bogus focus loss event when wxTextCtrl is modified in wxOSX.
authorVadim Zeitlin <vadim@wxwidgets.org>
Sat, 30 Jul 2011 21:54:18 +0000 (21:54 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Sat, 30 Jul 2011 21:54:18 +0000 (21:54 +0000)
A wxEVT_KILL_FOCUS event was generated when wxTextCtrl::WriteText() was called
in wxOSX, even though the control didn't lose focus at all in this case. This
was completely unexpected and thoroughly confused wxGrid code which closed the
in place editor immediately after showing it because of this (this was perhaps
only the case since r68319 but the changes there were correct so they only
masked the real bug).

Avoid this bogus event by remembering the NSView currently being
programmatically modified and not doing anything in textDidEndEditing
notification if it is generated for this view.

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

src/osx/cocoa/textctrl.mm

index b5913b841b4af809c2816cfa42b918e4d17b7de0..f22f8de86e4d2a3a768439c536c9ded3f2209df8 100644 (file)
 - (void)setSelectable:(BOOL)flag;
 @end
 
+// An object of this class is created before the text is modified
+// programmatically and destroyed as soon as this is done. It does several
+// things, like ensuring that the control is editable to allow setting its text
+// at all and eating any unwanted focus loss events from textDidEndEditing:
+// which don't really correspond to focus change.
 class wxMacEditHelper
 {
 public :
     wxMacEditHelper( NSView* textView )
     {
+        m_viewPreviouslyEdited = ms_viewCurrentlyEdited;
+        ms_viewCurrentlyEdited =
         m_textView = textView;
         m_formerEditable = YES;
         if ( textView )
@@ -79,14 +86,27 @@ public :
             [m_textView setEditable:m_formerEditable];
             [m_textView setSelectable:m_formerSelectable];
         }
+
+        ms_viewCurrentlyEdited = m_viewPreviouslyEdited;
     }
 
+    // Returns the last view we were instantiated for or NULL.
+    static NSView *GetCurrentlyEditedView() { return ms_viewCurrentlyEdited; }
+
 protected :
     BOOL m_formerEditable ;
     BOOL m_formerSelectable;
     NSView* m_textView;
+
+    // The original value of ms_viewCurrentlyEdited when this object was
+    // created.
+    NSView* m_viewPreviouslyEdited;
+
+    static NSView* ms_viewCurrentlyEdited;
 } ;
 
+NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
+
 @implementation wxNSSecureTextField
 
 + (void)initialize
@@ -222,6 +242,15 @@ protected :
 - (void)textDidEndEditing:(NSNotification *)aNotification
 {
     wxUnusedVar(aNotification);
+
+    if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
+    {
+        // This notification is generated as the result of calling our own
+        // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
+        // real focus loss event so skip generating it.
+        return;
+    }
+
     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
     if ( impl )
     {