1 /////////////////////////////////////////////////////////////////////////////
 
   2 // Name:        src/osx/cocoa/textctrl.mm
 
   4 // Author:      Stefan Csomor
 
   5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
 
   7 // Copyright:   (c) Stefan Csomor
 
   8 // Licence:     wxWindows licence
 
   9 /////////////////////////////////////////////////////////////////////////////
 
  11 #include "wx/wxprec.h"
 
  15 #include "wx/textctrl.h"
 
  22     #include "wx/button.h"
 
  24     #include "wx/settings.h"
 
  25     #include "wx/msgdlg.h"
 
  26     #include "wx/toplevel.h"
 
  30     #include <sys/types.h>
 
  36 #if wxUSE_STD_IOSTREAM
 
  44 #include "wx/filefn.h"
 
  45 #include "wx/sysopt.h"
 
  46 #include "wx/thread.h"
 
  47 #include "wx/textcompleter.h"
 
  49 #include "wx/osx/private.h"
 
  50 #include "wx/osx/cocoa/private/textimpl.h"
 
  52 @interface NSView(EditableView)
 
  54 - (void)setEditable:(BOOL)flag;
 
  56 - (void)setSelectable:(BOOL)flag;
 
  59 // An object of this class is created before the text is modified
 
  60 // programmatically and destroyed as soon as this is done. It does several
 
  61 // things, like ensuring that the control is editable to allow setting its text
 
  62 // at all and eating any unwanted focus loss events from textDidEndEditing:
 
  63 // which don't really correspond to focus change.
 
  67     wxMacEditHelper( NSView* textView )
 
  69         m_viewPreviouslyEdited = ms_viewCurrentlyEdited;
 
  70         ms_viewCurrentlyEdited =
 
  71         m_textView = textView;
 
  72         m_formerEditable = YES;
 
  75             m_formerEditable = [textView isEditable];
 
  76             m_formerSelectable = [textView isSelectable];
 
  77             [textView setEditable:YES];
 
  85             [m_textView setEditable:m_formerEditable];
 
  86             [m_textView setSelectable:m_formerSelectable];
 
  89         ms_viewCurrentlyEdited = m_viewPreviouslyEdited;
 
  92     // Returns the last view we were instantiated for or NULL.
 
  93     static NSView *GetCurrentlyEditedView() { return ms_viewCurrentlyEdited; }
 
  96     BOOL m_formerEditable ;
 
  97     BOOL m_formerSelectable;
 
 100     // The original value of ms_viewCurrentlyEdited when this object was
 
 102     NSView* m_viewPreviouslyEdited;
 
 104     static NSView* ms_viewCurrentlyEdited;
 
 107 NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
 
 109 // a minimal NSFormatter that just avoids getting too long entries
 
 110 @interface wxMaximumLengthFormatter : NSFormatter
 
 118 @implementation wxMaximumLengthFormatter
 
 127 - (void) setMaxLength:(int) maxlen 
 
 132 - (NSString *)stringForObjectValue:(id)anObject 
 
 134     if(![anObject isKindOfClass:[NSString class]])
 
 136     return [NSString stringWithString:anObject];
 
 139 - (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString  **)error 
 
 141     *obj = [NSString stringWithString:string];
 
 145 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr 
 
 146               originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error
 
 148     int len = [*partialStringPtr length];
 
 149     if ( maxLength > 0 && len > maxLength )
 
 151         field->SendMaxLenEvent();
 
 157 - (void) setTextEntry:(wxTextEntry*) tf
 
 164 @implementation wxNSSecureTextField
 
 168     static BOOL initialized = NO;
 
 172         wxOSXCocoaClassAddWXMethods( self );
 
 176 - (void)controlTextDidChange:(NSNotification *)aNotification
 
 178     wxUnusedVar(aNotification);
 
 179     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 181         impl->controlTextDidChange();
 
 184 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
 
 186     wxUnusedVar(aNotification);
 
 187     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 190         NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
 
 191         NSView* otherView = wxOSXGetViewFromResponder(responder);
 
 193         wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
 
 194         impl->DoNotifyFocusEvent( false, otherWindow );
 
 198 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
 
 200     wxUnusedVar(textView);
 
 204     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( control );
 
 207         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
 
 210             if (commandSelector == @selector(insertNewline:))
 
 212                 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(wxpeer), wxTopLevelWindow);
 
 213                 if ( tlw && tlw->GetDefaultItem() )
 
 215                     wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
 
 216                     if ( def && def->IsEnabled() )
 
 218                         wxCommandEvent event(wxEVT_BUTTON, def->GetId() );
 
 219                         event.SetEventObject(def);
 
 233 @interface wxNSTextScrollView : NSScrollView
 
 238 @implementation wxNSTextScrollView
 
 242     static BOOL initialized = NO;
 
 246         wxOSXCocoaClassAddWXMethods( self );
 
 252 @implementation wxNSTextFieldEditor
 
 254 - (void) keyDown:(NSEvent*) event
 
 256     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
 
 257     lastKeyDownEvent = event;
 
 258     if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
 
 259         [super keyDown:event];
 
 260     lastKeyDownEvent = nil;
 
 263 - (void) keyUp:(NSEvent*) event
 
 265     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
 
 266     if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
 
 270 - (void) flagsChanged:(NSEvent*) event
 
 272     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
 
 273     if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
 
 274         [super flagsChanged:event];
 
 277 - (BOOL) performKeyEquivalent:(NSEvent*) event
 
 279     BOOL retval = [super performKeyEquivalent:event];
 
 283 - (void) insertText:(id) str
 
 285     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
 
 286     if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
 
 288         [super insertText:str];
 
 294 @implementation wxNSTextView
 
 298     static BOOL initialized = NO;
 
 302         wxOSXCocoaClassAddWXMethods( self );
 
 306 - (void)textDidChange:(NSNotification *)aNotification
 
 308     wxUnusedVar(aNotification);
 
 309     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 311         impl->controlTextDidChange();
 
 314 - (void) setEnabled:(BOOL) flag
 
 316     // from Technical Q&A QA1461
 
 318         [self setTextColor: [NSColor controlTextColor]];
 
 321         [self setTextColor: [NSColor disabledControlTextColor]];
 
 324     [self setSelectable: flag];
 
 325     [self setEditable: flag];
 
 330     return [self isEditable];
 
 333 - (void)textDidEndEditing:(NSNotification *)aNotification
 
 335     wxUnusedVar(aNotification);
 
 337     if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
 
 339         // This notification is generated as the result of calling our own
 
 340         // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
 
 341         // real focus loss event so skip generating it.
 
 345     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 348         NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
 
 349         NSView* otherView = wxOSXGetViewFromResponder(responder);
 
 351         wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
 
 352         impl->DoNotifyFocusEvent( false, otherWindow );
 
 358 @implementation wxNSTextField
 
 362     static BOOL initialized = NO;
 
 366         wxOSXCocoaClassAddWXMethods( self );
 
 370 - (id) initWithFrame:(NSRect) frame
 
 372     self = [super initWithFrame:frame];
 
 379     [fieldEditor release];
 
 383 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
 
 385     if ( editor != fieldEditor )
 
 388         [fieldEditor release];
 
 389         fieldEditor = editor;
 
 393 - (wxNSTextFieldEditor*) fieldEditor
 
 398 - (void) setEnabled:(BOOL) flag
 
 400     [super setEnabled: flag];
 
 402     if (![self drawsBackground]) {
 
 403         // Static text is drawn incorrectly when disabled.
 
 404         // For an explanation, see
 
 405         // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
 
 407             [self setTextColor: [NSColor controlTextColor]];
 
 409             [self setTextColor: [NSColor secondarySelectedControlColor]];
 
 414 - (void)controlTextDidChange:(NSNotification *)aNotification
 
 416     wxUnusedVar(aNotification);
 
 417     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 419         impl->controlTextDidChange();
 
 422 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
 
 423  forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
 
 425     NSMutableArray* matches = NULL;
 
 427     wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self );
 
 428     wxTextEntry * const entry = impl->GetTextEntry();
 
 429     wxTextCompleter * const completer = entry->OSXGetCompleter();
 
 432         const wxString prefix = entry->GetValue();
 
 433         if ( completer->Start(prefix) )
 
 436                 wordStart = wxCFStringRef::AsString(
 
 437                               [[textView string] substringWithRange:charRange]
 
 440             matches = [NSMutableArray array];
 
 443                 const wxString s = completer->GetNext();
 
 447                 // Normally the completer should return only the strings
 
 448                 // starting with the prefix, but there could be exceptions
 
 449                 // and, for compatibility with MSW which simply ignores all
 
 450                 // entries that don't match the current text control contents,
 
 451                 // we ignore them as well. Besides, our own wxTextCompleterFixed
 
 452                 // doesn't respect this rule and, moreover, we need to extract
 
 453                 // just the rest of the string anyhow.
 
 455                 if ( s.StartsWith(prefix, &completion) )
 
 457                     // We discarded the entire prefix above but actually we
 
 458                     // should include the part of it that consists of the
 
 459                     // beginning of the current word, otherwise it would be
 
 460                     // lost when completion is accepted as OS X supposes that
 
 461                     // our matches do start with the "partial word range"
 
 463                     const wxCFStringRef fullWord(wordStart + completion);
 
 464                     [matches addObject: fullWord.AsNSString()];
 
 473 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
 
 475     wxUnusedVar(textView);
 
 476     wxUnusedVar(control);
 
 480     // send back key events wx' common code knows how to handle
 
 482     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 485         wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
 
 488             if (commandSelector == @selector(insertNewline:))
 
 490                 [textView insertNewlineIgnoringFieldEditor:self];
 
 493             else if ( commandSelector == @selector(insertTab:))
 
 495                 [textView insertTabIgnoringFieldEditor:self];
 
 498             else if ( commandSelector == @selector(insertBacktab:))
 
 500                 [textView insertTabIgnoringFieldEditor:self];
 
 509 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
 
 511     wxUnusedVar(aNotification);
 
 512     wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
 
 515         wxNSTextFieldControl* timpl = dynamic_cast<wxNSTextFieldControl*>(impl);
 
 518             NSRange range = [fieldEditor selectedRange];
 
 519             timpl->SetInternalSelection(range.location, range.location + range.length);
 
 522         NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
 
 523         NSView* otherView = wxOSXGetViewFromResponder(responder);
 
 525         wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
 
 526         impl->DoNotifyFocusEvent( false, otherWindow );
 
 531 // wxNSTextViewControl
 
 533 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
 
 534     : wxWidgetCocoaImpl(wxPeer, w),
 
 535       wxTextWidgetImpl(wxPeer)
 
 537     wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
 
 540     [m_scrollView setHasVerticalScroller:YES];
 
 541     [m_scrollView setHasHorizontalScroller:NO];
 
 542     // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed
 
 543     // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
 
 544     NSSize contentSize = [m_scrollView contentSize];
 
 546     wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
 
 547             contentSize.width, contentSize.height)];
 
 549     [tv setVerticallyResizable:YES];
 
 550     [tv setHorizontallyResizable:NO];
 
 551     [tv setAutoresizingMask:NSViewWidthSizable];
 
 553     [m_scrollView setDocumentView: tv];
 
 555     [tv setDelegate: tv];
 
 557     InstallEventHandler(tv);
 
 560 wxNSTextViewControl::~wxNSTextViewControl()
 
 563         [m_textView setDelegate: nil];
 
 566 bool wxNSTextViewControl::CanFocus() const
 
 568     // since this doesn't work (return false), we hardcode
 
 570     //    return [m_textView canBecomeKeyView];
 
 574 wxString wxNSTextViewControl::GetStringValue() const
 
 578         wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
 
 579         wxMacConvertNewlines13To10( &result ) ;
 
 582     return wxEmptyString;
 
 584 void wxNSTextViewControl::SetStringValue( const wxString &str)
 
 587     wxMacConvertNewlines10To13( &st );
 
 588     wxMacEditHelper helper(m_textView);
 
 591         [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
 
 594 void wxNSTextViewControl::Copy()
 
 597         [m_textView copy:nil];
 
 601 void wxNSTextViewControl::Cut()
 
 604         [m_textView cut:nil];
 
 607 void wxNSTextViewControl::Paste()
 
 610         [m_textView paste:nil];
 
 613 bool wxNSTextViewControl::CanPaste() const
 
 618 void wxNSTextViewControl::SetEditable(bool editable)
 
 621         [m_textView setEditable: editable];
 
 624 void wxNSTextViewControl::GetSelection( long* from, long* to) const
 
 628         NSRange range = [m_textView selectedRange];
 
 629         *from = range.location;
 
 630         *to = range.location + range.length;
 
 634 void wxNSTextViewControl::SetSelection( long from , long to )
 
 636     long textLength = [[m_textView string] length];
 
 637     if ((from == -1) && (to == -1))
 
 644         from = wxMin(textLength,wxMax(from,0)) ;
 
 648             to = wxMax(0,wxMin(textLength,to)) ;
 
 651     NSRange selrange = NSMakeRange(from, to-from);
 
 652     [m_textView setSelectedRange:selrange];
 
 653     [m_textView scrollRangeToVisible:selrange];
 
 656 void wxNSTextViewControl::WriteText(const wxString& str)
 
 659     wxMacConvertNewlines10To13( &st );
 
 660     wxMacEditHelper helper(m_textView);
 
 661     NSEvent* formerEvent = m_lastKeyDownEvent;
 
 662     m_lastKeyDownEvent = nil;
 
 663     [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
 
 664     m_lastKeyDownEvent = formerEvent;
 
 667 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
 
 669     if ([m_textView respondsToSelector:@selector(setFont:)])
 
 670         [m_textView setFont: font.OSXGetNSFont()];
 
 673 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
 
 675     if (m_textView && position >=0)
 
 678         NSColor* bgcolor = NULL;
 
 679         NSColor* fgcolor = NULL;
 
 680         // NOTE: It appears that other platforms accept GetStyle with the position == length
 
 681         // but that NSTextStorage does not accept length as a valid position.
 
 682         // Therefore we return the default control style in that case.
 
 683         if (position < (long) [[m_textView string] length]) 
 
 685             NSTextStorage* storage = [m_textView textStorage];
 
 686             font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
 
 687             bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
 
 688             fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
 
 692             NSDictionary* attrs = [m_textView typingAttributes];
 
 693             font = [[attrs objectForKey:NSFontAttributeName] autorelease];
 
 694             bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
 
 695             fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
 
 699             style.SetFont(wxFont(font));
 
 702             style.SetBackgroundColour(wxColour(bgcolor));
 
 705             style.SetTextColour(wxColour(fgcolor));
 
 712 void wxNSTextViewControl::SetStyle(long start,
 
 714                                 const wxTextAttr& style)
 
 719     if ( start == -1 && end == -1 )
 
 721         NSMutableDictionary* const
 
 722             attrs = [NSMutableDictionary dictionaryWithCapacity:3];
 
 723         if ( style.HasFont() )
 
 724             [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName];
 
 725         if ( style.HasBackgroundColour() )
 
 726             [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName];
 
 727         if ( style.HasTextColour() )
 
 728             [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName];
 
 730         [m_textView setTypingAttributes:attrs];
 
 732     else // Set the attributes just for this range.
 
 734         NSRange range = NSMakeRange(start, end-start);
 
 736         NSTextStorage* storage = [m_textView textStorage];
 
 737         if ( style.HasFont() )
 
 738             [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range];
 
 740         if ( style.HasBackgroundColour() )
 
 741             [storage addAttribute:NSBackgroundColorAttributeName value:style.GetBackgroundColour().OSXGetNSColor() range:range];
 
 743         if ( style.HasTextColour() )
 
 744             [storage addAttribute:NSForegroundColorAttributeName value:style.GetTextColour().OSXGetNSColor() range:range];
 
 748 void wxNSTextViewControl::CheckSpelling(bool check)
 
 751         [m_textView setContinuousSpellCheckingEnabled: check];
 
 754 wxSize wxNSTextViewControl::GetBestSize() const
 
 756     if (m_textView && [m_textView layoutManager])
 
 758         NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
 
 759         return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
 
 760                       (int)(rect.size.height + [m_textView textContainerInset].height));
 
 765 // wxNSTextFieldControl
 
 767 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
 
 768     : wxWidgetCocoaImpl(text, w),
 
 769       wxTextWidgetImpl(text)
 
 774 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
 
 777     : wxWidgetCocoaImpl(wxPeer, w),
 
 778       wxTextWidgetImpl(entry)
 
 783 void wxNSTextFieldControl::Init(WXWidget w)
 
 785     NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w;
 
 787     [m_textField setDelegate: tf];
 
 788     m_selStart = m_selEnd = 0;
 
 789     m_hasEditor = [w isKindOfClass:[NSTextField class]];
 
 792 wxNSTextFieldControl::~wxNSTextFieldControl()
 
 795         [m_textField setDelegate: nil];
 
 798 wxString wxNSTextFieldControl::GetStringValue() const
 
 800     return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
 
 803 void wxNSTextFieldControl::SetStringValue( const wxString &str)
 
 805     wxMacEditHelper helper(m_textField);
 
 806     [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
 
 809 void wxNSTextFieldControl::SetMaxLength(unsigned long len)
 
 811     wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease];
 
 812     [formatter setMaxLength:len];
 
 813     [formatter setTextEntry:GetTextEntry()];
 
 814     [m_textField setFormatter:formatter];
 
 817 void wxNSTextFieldControl::Copy()
 
 819     NSText* editor = [m_textField currentEditor];
 
 826 void wxNSTextFieldControl::Cut()
 
 828     NSText* editor = [m_textField currentEditor];
 
 835 void wxNSTextFieldControl::Paste()
 
 837     NSText* editor = [m_textField currentEditor];
 
 844 bool wxNSTextFieldControl::CanPaste() const
 
 849 void wxNSTextFieldControl::SetEditable(bool editable)
 
 851     [m_textField setEditable:editable];
 
 854 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
 
 856     NSText* editor = [m_textField currentEditor];
 
 859         NSRange range = [editor selectedRange];
 
 860         *from = range.location;
 
 861         *to = range.location + range.length;
 
 870 void wxNSTextFieldControl::SetSelection( long from , long to )
 
 872     long textLength = [[m_textField stringValue] length];
 
 873     if ((from == -1) && (to == -1))
 
 880         from = wxMin(textLength,wxMax(from,0)) ;
 
 884             to = wxMax(0,wxMin(textLength,to)) ;
 
 887     NSText* editor = [m_textField currentEditor];
 
 890         [editor setSelectedRange:NSMakeRange(from, to-from)];
 
 893     // the editor might still be in existence, but we might be already passed our 'focus lost' storage
 
 894     // of the selection, so make sure we copy this
 
 899 void wxNSTextFieldControl::WriteText(const wxString& str)
 
 901     NSEvent* formerEvent = m_lastKeyDownEvent;
 
 902     m_lastKeyDownEvent = nil;
 
 903     NSText* editor = [m_textField currentEditor];
 
 906         wxMacEditHelper helper(m_textField);
 
 907         BOOL hasUndo = [editor respondsToSelector:@selector(setAllowsUndo:)];
 
 909             [(NSTextView*)editor setAllowsUndo:NO];
 
 910         [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
 
 912             [(NSTextView*)editor setAllowsUndo:YES];
 
 916         wxString val = GetStringValue() ;
 
 918         GetSelection( &start , &end ) ;
 
 919         val.Remove( start , end - start ) ;
 
 920         val.insert( start , str ) ;
 
 921         SetStringValue( val ) ;
 
 922         SetSelection( start + str.length() , start + str.length() ) ;
 
 924     m_lastKeyDownEvent = formerEvent;
 
 927 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
 
 928     void* WXUNUSED(_cmd), void *WXUNUSED(sender))
 
 930     wxWindow* wxpeer = (wxWindow*) GetWXPeer();
 
 931     if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
 
 933         wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId());
 
 934         event.SetEventObject( wxpeer );
 
 935         event.SetString( GetTextEntry()->GetValue() );
 
 936         wxpeer->HandleWindowEvent( event );
 
 940 void wxNSTextFieldControl::SetInternalSelection( long from , long to )
 
 946 // as becoming first responder on a window - triggers a resign on the same control, we have to avoid
 
 947 // the resign notification writing back native selection values before we can set our own
 
 949 static WXWidget s_widgetBecomingFirstResponder = nil;
 
 951 bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd)
 
 953     s_widgetBecomingFirstResponder = slf;
 
 954     bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd);
 
 955     s_widgetBecomingFirstResponder = nil;
 
 958         NSText* editor = [m_textField currentEditor];
 
 961             long textLength = [[m_textField stringValue] length];
 
 962             m_selStart = wxMin(textLength,wxMax(m_selStart,0)) ;
 
 963             m_selEnd = wxMax(0,wxMin(textLength,m_selEnd)) ;
 
 965             [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)];
 
 971 bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd)
 
 973     if ( slf != s_widgetBecomingFirstResponder )
 
 975         NSText* editor = [m_textField currentEditor];
 
 978             NSRange range = [editor selectedRange];
 
 979             m_selStart = range.location;
 
 980             m_selEnd = range.location + range.length;
 
 983     return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd);
 
 986 bool wxNSTextFieldControl::SetHint(const wxString& hint)
 
 988     wxCFStringRef hintstring(hint);
 
 989     [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
 
 997 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
 
 998                                     wxWindowMac* WXUNUSED(parent),
 
 999                                     wxWindowID WXUNUSED(id),
 
1000                                     const wxString& WXUNUSED(str),
 
1004                                     long WXUNUSED(extraStyle))
 
1006     NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
 
1007     wxWidgetCocoaImpl* c = NULL;
 
1009     if ( style & wxTE_MULTILINE )
 
1011         wxNSTextScrollView* v = nil;
 
1012         v = [[wxNSTextScrollView alloc] initWithFrame:r];
 
1013         c = new wxNSTextViewControl( wxpeer, v );
 
1014         c->SetNeedsFocusRect( true );
 
1018         NSTextField* v = nil;
 
1019         if ( style & wxTE_PASSWORD )
 
1020             v = [[wxNSSecureTextField alloc] initWithFrame:r];
 
1022             v = [[wxNSTextField alloc] initWithFrame:r];
 
1024         if ( style & wxTE_RIGHT)
 
1026             [v setAlignment:NSRightTextAlignment];
 
1028         else if ( style & wxTE_CENTRE)
 
1030             [v setAlignment:NSCenterTextAlignment];
 
1033         NSTextFieldCell* cell = [v cell];
 
1034         [cell setScrollable:YES];
 
1035         // TODO: Remove if we definitely are sure, it's not needed
 
1036         // as setting scrolling to yes, should turn off any wrapping
 
1037         // [cell setLineBreakMode:NSLineBreakByClipping]; 
 
1039         c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
 
1041         if ( (style & wxNO_BORDER) || (style & wxSIMPLE_BORDER) )
 
1043             // under 10.7 the textcontrol can draw its own focus
 
1044             // even if no border is shown, on previous systems
 
1045             // we have to emulate this
 
1048             if ( UMAGetSystemVersion() < 0x1070 )
 
1049                 c->SetNeedsFocusRect( true );
 
1053             // use native border
 
1054             c->SetNeedsFrame(false);
 
1062 #endif // wxUSE_TEXTCTRL