X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/c824c165b17ef3f87d44236c86c1013276da5498..ee032c59df7ac4facdfdffce016768ab858e18c0:/src/osx/cocoa/textctrl.mm diff --git a/src/osx/cocoa/textctrl.mm b/src/osx/cocoa/textctrl.mm index b23dfd38a2..ca728cf9db 100644 --- a/src/osx/cocoa/textctrl.mm +++ b/src/osx/cocoa/textctrl.mm @@ -50,7 +50,8 @@ #include "wx/osx/cocoa/private/textimpl.h" @interface wxNSSecureTextField : NSSecureTextField - +{ +} @end @implementation wxNSSecureTextField @@ -65,13 +66,50 @@ } } +- (void)controlTextDidChange:(NSNotification *)aNotification +{ + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + 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 ); + } + } +} + +- (void)controlTextDidEndEditing:(NSNotification *)aNotification +{ + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if ( impl ) + { + impl->DoNotifyFocusEvent( false, NULL ); + } +} + @end -@interface wxNSTextView : NSScrollView +@interface wxNSTextScrollView : NSScrollView +{ +} +@end + +@interface wxNSTextView : NSTextView +{ + wxNSTextScrollView* scrollView; +} + +- (void)setScrollView: (wxNSTextScrollView *) sv; +- (wxNSTextScrollView*) scrollView; @end -@implementation wxNSTextView +@implementation wxNSTextScrollView + (void)initialize { @@ -83,6 +121,81 @@ } } +- (void)textDidChange:(NSNotification *)aNotification +{ + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + 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 ); + } + } +} + +- (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 wxNSTextView + +- (BOOL) becomeFirstResponder +{ + BOOL val = [super becomeFirstResponder]; + + if ( val ) + { + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( scrollView ); + if (impl ) + impl->DoNotifyFocusEvent( true, NULL ); + + } + return val; +} + +- (void)setScrollView: (wxNSTextScrollView *) sv +{ + scrollView = sv; +} + +- (wxNSTextScrollView*) scrollView +{ + return scrollView; +} + @end @implementation wxNSTextField @@ -97,9 +210,26 @@ } } -/* +- (void) setEnabled:(BOOL) flag +{ + [super setEnabled: flag]; + + if (![self drawsBackground]) { + // Static text is drawn incorrectly when disabled. + // For an explanation, see + // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028 + if (flag) { + [self setTextColor: [NSColor controlTextColor]]; + } else { + [self setTextColor: [NSColor secondarySelectedControlColor]]; + } + } +} + - (void)controlTextDidChange:(NSNotification *)aNotification { + wxUnusedVar(aNotification); + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if ( impl ) { wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); @@ -112,42 +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; + 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() @@ -212,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) @@ -220,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 @@ -307,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) ) @@ -326,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