From 1e70e497e23f8f0770d9fa2709bd2760dfed935d Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Wed, 28 Nov 2012 08:51:56 +0000 Subject: [PATCH] supporting selection methods when not focused, closes #1480 git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@73046 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/cocoa/private/textimpl.h | 2 ++ src/osx/cocoa/textctrl.mm | 37 ++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/include/wx/osx/cocoa/private/textimpl.h b/include/wx/osx/cocoa/private/textimpl.h index 0cd220b0ea..93c3ab85d4 100644 --- a/include/wx/osx/cocoa/private/textimpl.h +++ b/include/wx/osx/cocoa/private/textimpl.h @@ -45,6 +45,8 @@ public : virtual bool SetHint(const wxString& hint); virtual void controlAction(WXWidget slf, void* _cmd, void *sender); + virtual bool becomeFirstResponder(WXWidget slf, void *_cmd); + virtual bool resignFirstResponder(WXWidget slf, void *_cmd); protected : NSTextField* m_textField; diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index f2482308a1..6cbc2c06a8 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -515,7 +515,8 @@ wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) [m_scrollView setHasVerticalScroller:YES]; [m_scrollView setHasHorizontalScroller:NO]; - [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; + // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed + // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; NSSize contentSize = [m_scrollView contentSize]; wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0, @@ -911,6 +912,40 @@ void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), } } +// as becoming first responder on a window - triggers a resign on the same control, we have to avoid +// the resign notification writing back native selection values before we can set our own + +static WXWidget s_widgetBecomingFirstResponder = nil; + +bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd) +{ + s_widgetBecomingFirstResponder = slf; + bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd); + s_widgetBecomingFirstResponder = nil; + if ( retval ) + { + NSText* editor = [m_textField currentEditor]; + if ( editor ) + [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)]; + } + return retval; +} + +bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd) +{ + if ( slf != s_widgetBecomingFirstResponder ) + { + NSText* editor = [m_textField currentEditor]; + if ( editor ) + { + NSRange range = [editor selectedRange]; + m_selStart = range.location; + m_selEnd = range.location + range.length; + } + } + return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd); +} + bool wxNSTextFieldControl::SetHint(const wxString& hint) { wxCFStringRef hintstring(hint); -- 2.45.2