From: Kevin Ollivier Date: Thu, 26 Nov 2009 21:52:42 +0000 (+0000) Subject: Add CheckSpelling support for OS X Cocoa, make sure NSTextView-based wxTextCtrls... X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/b9cf2753b45161ab5ae88ac464e18981c9c8ae63?ds=inline Add CheckSpelling support for OS X Cocoa, make sure NSTextView-based wxTextCtrls emit EVT_TEXT, and a fix for GetStyle when position == length. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@62724 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 9a3e131a81..41d450500e 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -301,10 +301,12 @@ protected : @end - @interface wxNSTextView : NSTextView + @interface wxNSTextView : NSTextView { } - + + - (void)textDidChange:(NSNotification *)aNotification; + @end @interface wxNSMenu : NSMenu diff --git a/include/wx/osx/cocoa/private/textimpl.h b/include/wx/osx/cocoa/private/textimpl.h index 7505bc9882..5d7f07bceb 100644 --- a/include/wx/osx/cocoa/private/textimpl.h +++ b/include/wx/osx/cocoa/private/textimpl.h @@ -60,6 +60,8 @@ public: virtual bool GetStyle(long position, wxTextAttr& style); virtual void SetStyle(long start, long end, const wxTextAttr& style); + + virtual void CheckSpelling(bool check); protected: NSScrollView* m_scrollView; diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index 3a0f1c1979..63a2515307 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -192,6 +192,21 @@ protected : } } +- (void)textDidChange:(NSNotification *)aNotification +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [aNotification object] ); + if ( impl ) + { + wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); + if ( wxpeer ) { + wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId()); + event.SetEventObject( wxpeer ); + event.SetString( static_cast(wxpeer)->GetValue() ); + wxpeer->HandleWindowEvent( event ); + } + } +} + @end @implementation wxNSTextField @@ -319,7 +334,7 @@ wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxW [m_scrollView setDocumentView: tv]; - [tv setDelegate: w]; + [tv setDelegate: tv]; InstallEventHandler(tv); } @@ -431,16 +446,37 @@ void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSE bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style) { - if (m_textView && position >=0 && position < [[m_textView string] length]) { - NSTextStorage* storage = [m_textView textStorage]; - NSFont* font = [storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL]; + if (m_textView && position >=0) + { + NSFont* font = NULL; + NSColor* bgcolor = NULL; + NSColor* fgcolor = NULL; + // NOTE: It appears that other platforms accept GetStyle with the position == length + // but that NSTextStorage does not accept length as a valid position. + // Therefore we return the default control style in that case. + if (position < [[m_textView string] length]) + { + NSTextStorage* storage = [m_textView textStorage]; + font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease]; + bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; + fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; + } + else + { + NSDictionary* attrs = [m_textView typingAttributes]; + font = [[attrs objectForKey:NSFontAttributeName] autorelease]; + bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease]; + fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease]; + } + if (font) style.SetFont(wxFont(font)); - NSColor* bgcolor = [storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL]; + if (bgcolor) style.SetBackgroundColour(wxColour(bgcolor)); - NSColor* fgcolor = [storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL]; - style.SetTextColour(wxColour(fgcolor)); + + if (fgcolor) + style.SetTextColour(wxColour(fgcolor)); return true; } @@ -453,22 +489,31 @@ void wxNSTextViewControl::SetStyle(long start, { if (m_textView) { NSRange range = NSMakeRange(start, end-start); + if (start == -1 && end == -1) + range = [m_textView selectedRange]; + NSTextStorage* storage = [m_textView textStorage]; wxFont font = style.GetFont(); - if (font.IsOk()) - [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range]; + if (style.HasFont() && font.IsOk()) + [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range]; wxColour bgcolor = style.GetBackgroundColour(); - if (bgcolor.IsOk()) + if (style.HasBackgroundColour() && bgcolor.IsOk()) [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range]; wxColour fgcolor = style.GetTextColour(); - if (fgcolor.IsOk()) + if (style.HasTextColour() && fgcolor.IsOk()) [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range]; } } +void wxNSTextViewControl::CheckSpelling(bool check) +{ + if (m_textView) + [m_textView setContinuousSpellCheckingEnabled: check]; +} + // wxNSTextFieldControl wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)