X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c3e433b15929171af57df3c9c3c63d6ab8aa720b..c443ff6f5350df36902af42d0193e53d79dd6a14:/src/osx/cocoa/textctrl.mm diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index f4debced6c..ca728cf9db 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -51,11 +51,7 @@ @interface wxNSSecureTextField : NSSecureTextField { - wxWidgetCocoaImpl* impl; } - -- (void) setImplementation:(wxWidgetCocoaImpl*) item; -- (wxWidgetCocoaImpl*) implementation; @end @implementation wxNSSecureTextField @@ -70,18 +66,10 @@ } } -- (wxWidgetCocoaImpl*) implementation -{ - return impl; -} - -- (void) setImplementation:(wxWidgetCocoaImpl*) item -{ - impl = item; -} - - (void)controlTextDidChange:(NSNotification *)aNotification { + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl ) { wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); @@ -94,18 +82,34 @@ } } +- (void)controlTextDidEndEditing:(NSNotification *)aNotification +{ + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if ( impl ) + { + impl->DoNotifyFocusEvent( false, NULL ); + } +} + +@end + +@interface wxNSTextScrollView : NSScrollView +{ +} @end -@interface wxNSTextView : NSScrollView +@interface wxNSTextView : NSTextView { - wxWidgetCocoaImpl* impl; + wxNSTextScrollView* scrollView; } -- (void) setImplementation:(wxWidgetCocoaImpl*) item; -- (wxWidgetCocoaImpl*) implementation; +- (void)setScrollView: (wxNSTextScrollView *) sv; +- (wxNSTextScrollView*) scrollView; + @end -@implementation wxNSTextView +@implementation wxNSTextScrollView + (void)initialize { @@ -117,19 +121,10 @@ } } -- (wxWidgetCocoaImpl*) implementation -{ - return impl; -} - -- (void) setImplementation:(wxWidgetCocoaImpl*) item -{ - impl = item; -} - - -- (void)controlTextDidChange:(NSNotification *)aNotification +- (void)textDidChange:(NSNotification *)aNotification { + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl ) { wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); @@ -141,28 +136,78 @@ } } } + +- (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)commandSelector +{ + wxUnusedVar(aTextView); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if ( impl ) + { + wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); + if (commandSelector == @selector(insertNewline:)) + { + if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) + { + wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); + event.SetEventObject( wxpeer ); + event.SetString( static_cast(wxpeer)->GetValue() ); + wxpeer->HandleWindowEvent( event ); + } + } + } + + return NO; +} + +- (void)textDidEndEditing:(NSNotification *)aNotification +{ + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if ( impl ) + { + impl->DoNotifyFocusEvent( false, NULL ); + } +} @end -@implementation wxNSTextField +@implementation wxNSTextView -+ (void)initialize +- (BOOL) becomeFirstResponder { - static BOOL initialized = NO; - if (!initialized) + BOOL val = [super becomeFirstResponder]; + + if ( val ) { - initialized = YES; - wxOSXCocoaClassAddWXMethods( self ); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( scrollView ); + if (impl ) + impl->DoNotifyFocusEvent( true, NULL ); + } + return val; +} + +- (void)setScrollView: (wxNSTextScrollView *) sv +{ + scrollView = sv; } -- (wxWidgetCocoaImpl*) implementation +- (wxNSTextScrollView*) scrollView { - return impl; + return scrollView; } -- (void) setImplementation:(wxWidgetCocoaImpl*) item +@end + +@implementation wxNSTextField + ++ (void)initialize { - impl = item; + static BOOL initialized = NO; + if (!initialized) + { + initialized = YES; + wxOSXCocoaClassAddWXMethods( self ); + } } - (void) setEnabled:(BOOL) flag @@ -183,6 +228,8 @@ - (void)controlTextDidChange:(NSNotification *)aNotification { + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl ) { wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); @@ -195,44 +242,65 @@ } } -/* +typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector); + +- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector +{ + wxUnusedVar(textView); + wxUnusedVar(control); + if (commandSelector == @selector(insertNewline:)) + { + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if ( impl ) + { + wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); + if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) + { + wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); + event.SetEventObject( wxpeer ); + event.SetString( static_cast(wxpeer)->GetValue() ); + wxpeer->HandleWindowEvent( event ); + } + } + } + + return NO; +} + - (void)controlTextDidEndEditing:(NSNotification *)aNotification { + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl ) { - wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); - if ( wxpeer ) { - wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId()); - event.SetEventObject( wxpeer ); - event.SetWindow( wxpeer ); - wxpeer->HandleWindowEvent( event ); - } + impl->DoNotifyFocusEvent( false, NULL ); } } -*/ @end // wxNSTextViewControl wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w) { - m_scrollView = (NSScrollView*) w; - [w setImplementation: this]; + wxNSTextScrollView* sv = (wxNSTextScrollView*) w; + m_scrollView = sv; [m_scrollView setHasVerticalScroller:YES]; [m_scrollView setHasHorizontalScroller:NO]; [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; NSSize contentSize = [m_scrollView contentSize]; - m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0, + wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0, contentSize.width, contentSize.height)]; - [m_textView setVerticallyResizable:YES]; - [m_textView setHorizontallyResizable:NO]; - [m_textView setAutoresizingMask:NSViewWidthSizable]; + m_textView = tv; + [tv setVerticallyResizable:YES]; + [tv setHorizontallyResizable:NO]; + [tv setAutoresizingMask:NSViewWidthSizable]; - [m_scrollView setDocumentView: m_textView]; + [m_scrollView setDocumentView: tv]; - [m_textView setDelegate: w]; + [tv setDelegate: w]; + [tv setScrollView:sv]; } wxNSTextViewControl::~wxNSTextViewControl() @@ -297,7 +365,9 @@ void wxNSTextViewControl::GetSelection( long* from, long* to) const void wxNSTextViewControl::SetSelection( long from , long to ) { - [m_textView setSelectedRange:NSMakeRange(from, to-from)]; + NSRange selrange = NSMakeRange(from, to-from); + [m_textView setSelectedRange:selrange]; + [m_textView scrollRangeToVisible:selrange]; } void wxNSTextViewControl::WriteText(const wxString& str) @@ -305,6 +375,7 @@ void wxNSTextViewControl::WriteText(const wxString& str) // temp hack to get logging working early wxString former = GetStringValue(); SetStringValue( former + str ); + SetSelection(GetStringValue().length(), GetStringValue().length()); } // wxNSTextFieldControl @@ -392,9 +463,11 @@ void wxNSTextFieldControl::WriteText(const wxString& str) // temp hack to get logging working early wxString former = GetStringValue(); SetStringValue( former + str ); + SetSelection(GetStringValue().length(), GetStringValue().length()); } -void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender) +void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), + void* WXUNUSED(_cmd), void *WXUNUSED(sender)) { wxWindow* wxpeer = (wxWindow*) GetWXPeer(); if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) @@ -411,26 +484,27 @@ void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender) // wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, - wxWindowMac* parent, - wxWindowID id, + wxWindowMac* WXUNUSED(parent), + wxWindowID WXUNUSED(id), const wxString& str, const wxPoint& pos, const wxSize& size, long style, - long extraStyle) + long WXUNUSED(extraStyle)) { NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; - NSTextField* v = nil; wxWidgetCocoaImpl* c = NULL; if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 ) { - v = [[wxNSTextView alloc] initWithFrame:r]; + wxNSTextScrollView* v = nil; + v = [[wxNSTextScrollView alloc] initWithFrame:r]; c = new wxNSTextViewControl( wxpeer, v ); static_cast(c)->SetStringValue(str); } else { + NSTextField* v = nil; if ( style & wxTE_PASSWORD ) v = [[wxNSSecureTextField alloc] initWithFrame:r]; else @@ -446,7 +520,6 @@ wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, [v setBordered:NO]; c = new wxNSTextFieldControl( wxpeer, v ); - [v setImplementation: c]; static_cast(c)->SetStringValue(str); }