1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/textctrl.mm
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/textctrl.h"
23 #include "wx/button.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
31 #include <sys/types.h>
37 #if wxUSE_STD_IOSTREAM
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
47 #include "wx/thread.h"
48 #include "wx/textcompleter.h"
50 #include "wx/osx/private.h"
51 #include "wx/osx/cocoa/private/textimpl.h"
53 @interface NSView(EditableView)
55 - (void)setEditable:(BOOL)flag;
57 - (void)setSelectable:(BOOL)flag;
60 // An object of this class is created before the text is modified
61 // programmatically and destroyed as soon as this is done. It does several
62 // things, like ensuring that the control is editable to allow setting its text
63 // at all and eating any unwanted focus loss events from textDidEndEditing:
64 // which don't really correspond to focus change.
68 wxMacEditHelper( NSView* textView )
70 m_viewPreviouslyEdited = ms_viewCurrentlyEdited;
71 ms_viewCurrentlyEdited =
72 m_textView = textView;
73 m_formerEditable = YES;
76 m_formerEditable = [textView isEditable];
77 m_formerSelectable = [textView isSelectable];
78 [textView setEditable:YES];
86 [m_textView setEditable:m_formerEditable];
87 [m_textView setSelectable:m_formerSelectable];
90 ms_viewCurrentlyEdited = m_viewPreviouslyEdited;
93 // Returns the last view we were instantiated for or NULL.
94 static NSView *GetCurrentlyEditedView() { return ms_viewCurrentlyEdited; }
97 BOOL m_formerEditable ;
98 BOOL m_formerSelectable;
101 // The original value of ms_viewCurrentlyEdited when this object was
103 NSView* m_viewPreviouslyEdited;
105 static NSView* ms_viewCurrentlyEdited;
108 NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
110 // a minimal NSFormatter that just avoids getting too long entries
111 @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 // TODO wxEVT_COMMAND_TEXT_MAXLEN
159 @implementation wxNSSecureTextField
163 static BOOL initialized = NO;
167 wxOSXCocoaClassAddWXMethods( self );
171 - (void)controlTextDidChange:(NSNotification *)aNotification
173 wxUnusedVar(aNotification);
174 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
176 impl->controlTextDidChange();
179 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
181 wxUnusedVar(aNotification);
182 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
185 impl->DoNotifyFocusEvent( false, NULL );
189 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
191 wxUnusedVar(textView);
195 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( control );
198 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
201 if (commandSelector == @selector(insertNewline:))
203 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(wxpeer), wxTopLevelWindow);
204 if ( tlw && tlw->GetDefaultItem() )
206 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
207 if ( def && def->IsEnabled() )
209 wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, def->GetId() );
210 event.SetEventObject(def);
224 @interface wxNSTextScrollView : NSScrollView
229 @implementation wxNSTextScrollView
233 static BOOL initialized = NO;
237 wxOSXCocoaClassAddWXMethods( self );
243 @implementation wxNSTextFieldEditor
245 - (void) keyDown:(NSEvent*) event
247 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
248 lastKeyDownEvent = event;
249 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
250 [super keyDown:event];
251 lastKeyDownEvent = nil;
254 - (void) keyUp:(NSEvent*) event
256 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
257 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
261 - (void) flagsChanged:(NSEvent*) event
263 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
264 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
265 [super flagsChanged:event];
268 - (BOOL) performKeyEquivalent:(NSEvent*) event
270 BOOL retval = [super performKeyEquivalent:event];
274 - (void) insertText:(id) str
276 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
277 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
279 [super insertText:str];
285 @implementation wxNSTextView
289 static BOOL initialized = NO;
293 wxOSXCocoaClassAddWXMethods( self );
297 - (void)textDidChange:(NSNotification *)aNotification
299 wxUnusedVar(aNotification);
300 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
302 impl->controlTextDidChange();
305 - (void) setEnabled:(BOOL) flag
307 // from Technical Q&A QA1461
309 [self setTextColor: [NSColor controlTextColor]];
312 [self setTextColor: [NSColor disabledControlTextColor]];
315 [self setSelectable: flag];
316 [self setEditable: flag];
321 return [self isEditable];
324 - (void)textDidEndEditing:(NSNotification *)aNotification
326 wxUnusedVar(aNotification);
328 if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
330 // This notification is generated as the result of calling our own
331 // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
332 // real focus loss event so skip generating it.
336 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
339 impl->DoNotifyFocusEvent( false, NULL );
345 @implementation wxNSTextField
349 static BOOL initialized = NO;
353 wxOSXCocoaClassAddWXMethods( self );
357 - (id) initWithFrame:(NSRect) frame
359 self = [super initWithFrame:frame];
366 [fieldEditor release];
370 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
372 if ( editor != fieldEditor )
375 [fieldEditor release];
376 fieldEditor = editor;
380 - (wxNSTextFieldEditor*) fieldEditor
385 - (void) setEnabled:(BOOL) flag
387 [super setEnabled: flag];
389 if (![self drawsBackground]) {
390 // Static text is drawn incorrectly when disabled.
391 // For an explanation, see
392 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
394 [self setTextColor: [NSColor controlTextColor]];
396 [self setTextColor: [NSColor secondarySelectedControlColor]];
401 - (void)controlTextDidChange:(NSNotification *)aNotification
403 wxUnusedVar(aNotification);
404 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
406 impl->controlTextDidChange();
409 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
410 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
412 NSMutableArray* matches = NULL;
414 wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self );
415 wxTextEntry * const entry = impl->GetTextEntry();
416 wxTextCompleter * const completer = entry->OSXGetCompleter();
419 const wxString prefix = entry->GetValue();
420 if ( completer->Start(prefix) )
423 wordStart = wxCFStringRef::AsString(
424 [[textView string] substringWithRange:charRange]
427 matches = [NSMutableArray array];
430 const wxString s = completer->GetNext();
434 // Normally the completer should return only the strings
435 // starting with the prefix, but there could be exceptions
436 // and, for compatibility with MSW which simply ignores all
437 // entries that don't match the current text control contents,
438 // we ignore them as well. Besides, our own wxTextCompleterFixed
439 // doesn't respect this rule and, moreover, we need to extract
440 // just the rest of the string anyhow.
442 if ( s.StartsWith(prefix, &completion) )
444 // We discarded the entire prefix above but actually we
445 // should include the part of it that consists of the
446 // beginning of the current word, otherwise it would be
447 // lost when completion is accepted as OS X supposes that
448 // our matches do start with the "partial word range"
450 const wxCFStringRef fullWord(wordStart + completion);
451 [matches addObject: fullWord.AsNSString()];
460 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
462 wxUnusedVar(textView);
463 wxUnusedVar(control);
467 // send back key events wx' common code knows how to handle
469 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
472 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
475 if (commandSelector == @selector(insertNewline:))
477 [textView insertNewlineIgnoringFieldEditor:self];
480 else if ( commandSelector == @selector(insertTab:))
482 [textView insertTabIgnoringFieldEditor:self];
485 else if ( commandSelector == @selector(insertBacktab:))
487 [textView insertTabIgnoringFieldEditor:self];
496 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
498 wxUnusedVar(aNotification);
499 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
502 wxNSTextFieldControl* timpl = dynamic_cast<wxNSTextFieldControl*>(impl);
505 NSRange range = [fieldEditor selectedRange];
506 timpl->SetInternalSelection(range.location, range.location + range.length);
509 impl->DoNotifyFocusEvent( false, NULL );
514 // wxNSTextViewControl
516 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
517 : wxWidgetCocoaImpl(wxPeer, w),
518 wxTextWidgetImpl(wxPeer)
520 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
523 [m_scrollView setHasVerticalScroller:YES];
524 [m_scrollView setHasHorizontalScroller:NO];
525 // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed
526 // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
527 NSSize contentSize = [m_scrollView contentSize];
529 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
530 contentSize.width, contentSize.height)];
532 [tv setVerticallyResizable:YES];
533 [tv setHorizontallyResizable:NO];
534 [tv setAutoresizingMask:NSViewWidthSizable];
536 [m_scrollView setDocumentView: tv];
538 [tv setDelegate: tv];
540 InstallEventHandler(tv);
543 wxNSTextViewControl::~wxNSTextViewControl()
546 [m_textView setDelegate: nil];
549 bool wxNSTextViewControl::CanFocus() const
551 // since this doesn't work (return false), we hardcode
553 // return [m_textView canBecomeKeyView];
557 wxString wxNSTextViewControl::GetStringValue() const
561 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
562 wxMacConvertNewlines13To10( &result ) ;
565 return wxEmptyString;
567 void wxNSTextViewControl::SetStringValue( const wxString &str)
570 wxMacConvertNewlines10To13( &st );
571 wxMacEditHelper helper(m_textView);
574 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
577 void wxNSTextViewControl::Copy()
580 [m_textView copy:nil];
584 void wxNSTextViewControl::Cut()
587 [m_textView cut:nil];
590 void wxNSTextViewControl::Paste()
593 [m_textView paste:nil];
596 bool wxNSTextViewControl::CanPaste() const
601 void wxNSTextViewControl::SetEditable(bool editable)
604 [m_textView setEditable: editable];
607 void wxNSTextViewControl::GetSelection( long* from, long* to) const
611 NSRange range = [m_textView selectedRange];
612 *from = range.location;
613 *to = range.location + range.length;
617 void wxNSTextViewControl::SetSelection( long from , long to )
619 long textLength = [[m_textView string] length];
620 if ((from == -1) && (to == -1))
627 from = wxMin(textLength,wxMax(from,0)) ;
631 to = wxMax(0,wxMin(textLength,to)) ;
634 NSRange selrange = NSMakeRange(from, to-from);
635 [m_textView setSelectedRange:selrange];
636 [m_textView scrollRangeToVisible:selrange];
639 void wxNSTextViewControl::WriteText(const wxString& str)
642 wxMacConvertNewlines10To13( &st );
643 wxMacEditHelper helper(m_textView);
644 NSEvent* formerEvent = m_lastKeyDownEvent;
645 m_lastKeyDownEvent = nil;
646 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
647 m_lastKeyDownEvent = formerEvent;
650 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
652 if ([m_textView respondsToSelector:@selector(setFont:)])
653 [m_textView setFont: font.OSXGetNSFont()];
656 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
658 if (m_textView && position >=0)
661 NSColor* bgcolor = NULL;
662 NSColor* fgcolor = NULL;
663 // NOTE: It appears that other platforms accept GetStyle with the position == length
664 // but that NSTextStorage does not accept length as a valid position.
665 // Therefore we return the default control style in that case.
666 if (position < (long) [[m_textView string] length])
668 NSTextStorage* storage = [m_textView textStorage];
669 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
670 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
671 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
675 NSDictionary* attrs = [m_textView typingAttributes];
676 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
677 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
678 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
682 style.SetFont(wxFont(font));
685 style.SetBackgroundColour(wxColour(bgcolor));
688 style.SetTextColour(wxColour(fgcolor));
695 void wxNSTextViewControl::SetStyle(long start,
697 const wxTextAttr& style)
702 if ( start == -1 && end == -1 )
704 NSMutableDictionary* const
705 attrs = [NSMutableDictionary dictionaryWithCapacity:3];
706 if ( style.HasFont() )
707 [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName];
708 if ( style.HasBackgroundColour() )
709 [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName];
710 if ( style.HasTextColour() )
711 [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName];
713 [m_textView setTypingAttributes:attrs];
715 else // Set the attributes just for this range.
717 NSRange range = NSMakeRange(start, end-start);
719 NSTextStorage* storage = [m_textView textStorage];
720 if ( style.HasFont() )
721 [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range];
723 if ( style.HasBackgroundColour() )
724 [storage addAttribute:NSBackgroundColorAttributeName value:style.GetBackgroundColour().OSXGetNSColor() range:range];
726 if ( style.HasTextColour() )
727 [storage addAttribute:NSForegroundColorAttributeName value:style.GetTextColour().OSXGetNSColor() range:range];
731 void wxNSTextViewControl::CheckSpelling(bool check)
734 [m_textView setContinuousSpellCheckingEnabled: check];
737 wxSize wxNSTextViewControl::GetBestSize() const
739 if (m_textView && [m_textView layoutManager])
741 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
742 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
743 (int)(rect.size.height + [m_textView textContainerInset].height));
748 // wxNSTextFieldControl
750 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
751 : wxWidgetCocoaImpl(text, w),
752 wxTextWidgetImpl(text)
757 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
760 : wxWidgetCocoaImpl(wxPeer, w),
761 wxTextWidgetImpl(entry)
766 void wxNSTextFieldControl::Init(WXWidget w)
768 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w;
770 [m_textField setDelegate: tf];
771 m_selStart = m_selEnd = 0;
772 m_hasEditor = [w isKindOfClass:[NSTextField class]];
775 wxNSTextFieldControl::~wxNSTextFieldControl()
778 [m_textField setDelegate: nil];
781 wxString wxNSTextFieldControl::GetStringValue() const
783 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
786 void wxNSTextFieldControl::SetStringValue( const wxString &str)
788 wxMacEditHelper helper(m_textField);
789 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
792 void wxNSTextFieldControl::SetMaxLength(unsigned long len)
794 wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease];
795 [formatter setMaxLength:len];
796 [m_textField setFormatter:formatter];
799 void wxNSTextFieldControl::Copy()
801 NSText* editor = [m_textField currentEditor];
808 void wxNSTextFieldControl::Cut()
810 NSText* editor = [m_textField currentEditor];
817 void wxNSTextFieldControl::Paste()
819 NSText* editor = [m_textField currentEditor];
826 bool wxNSTextFieldControl::CanPaste() const
831 void wxNSTextFieldControl::SetEditable(bool editable)
833 [m_textField setEditable:editable];
836 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
838 NSText* editor = [m_textField currentEditor];
841 NSRange range = [editor selectedRange];
842 *from = range.location;
843 *to = range.location + range.length;
852 void wxNSTextFieldControl::SetSelection( long from , long to )
854 long textLength = [[m_textField stringValue] length];
855 if ((from == -1) && (to == -1))
862 from = wxMin(textLength,wxMax(from,0)) ;
866 to = wxMax(0,wxMin(textLength,to)) ;
869 NSText* editor = [m_textField currentEditor];
872 [editor setSelectedRange:NSMakeRange(from, to-from)];
875 // the editor might still be in existence, but we might be already passed our 'focus lost' storage
876 // of the selection, so make sure we copy this
881 void wxNSTextFieldControl::WriteText(const wxString& str)
883 NSEvent* formerEvent = m_lastKeyDownEvent;
884 m_lastKeyDownEvent = nil;
885 NSText* editor = [m_textField currentEditor];
888 wxMacEditHelper helper(m_textField);
889 BOOL hasUndo = [editor respondsToSelector:@selector(setAllowsUndo:)];
891 [(NSTextView*)editor setAllowsUndo:NO];
892 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
894 [(NSTextView*)editor setAllowsUndo:YES];
898 wxString val = GetStringValue() ;
900 GetSelection( &start , &end ) ;
901 val.Remove( start , end - start ) ;
902 val.insert( start , str ) ;
903 SetStringValue( val ) ;
904 SetSelection( start + str.length() , start + str.length() ) ;
906 m_lastKeyDownEvent = formerEvent;
909 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
910 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
912 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
913 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
915 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
916 event.SetEventObject( wxpeer );
917 event.SetString( GetTextEntry()->GetValue() );
918 wxpeer->HandleWindowEvent( event );
922 void wxNSTextFieldControl::SetInternalSelection( long from , long to )
928 // as becoming first responder on a window - triggers a resign on the same control, we have to avoid
929 // the resign notification writing back native selection values before we can set our own
931 static WXWidget s_widgetBecomingFirstResponder = nil;
933 bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd)
935 s_widgetBecomingFirstResponder = slf;
936 bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd);
937 s_widgetBecomingFirstResponder = nil;
940 NSText* editor = [m_textField currentEditor];
943 long textLength = [[m_textField stringValue] length];
944 m_selStart = wxMin(textLength,wxMax(m_selStart,0)) ;
945 m_selEnd = wxMax(0,wxMin(textLength,m_selEnd)) ;
947 [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)];
953 bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd)
955 if ( slf != s_widgetBecomingFirstResponder )
957 NSText* editor = [m_textField currentEditor];
960 NSRange range = [editor selectedRange];
961 m_selStart = range.location;
962 m_selEnd = range.location + range.length;
965 return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd);
968 bool wxNSTextFieldControl::SetHint(const wxString& hint)
970 wxCFStringRef hintstring(hint);
971 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
979 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
980 wxWindowMac* WXUNUSED(parent),
981 wxWindowID WXUNUSED(id),
982 const wxString& WXUNUSED(str),
986 long WXUNUSED(extraStyle))
988 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
989 wxWidgetCocoaImpl* c = NULL;
991 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
993 wxNSTextScrollView* v = nil;
994 v = [[wxNSTextScrollView alloc] initWithFrame:r];
995 c = new wxNSTextViewControl( wxpeer, v );
996 c->SetNeedsFocusRect( true );
1000 NSTextField* v = nil;
1001 if ( style & wxTE_PASSWORD )
1002 v = [[wxNSSecureTextField alloc] initWithFrame:r];
1004 v = [[wxNSTextField alloc] initWithFrame:r];
1006 if ( style & wxTE_RIGHT)
1008 [v setAlignment:NSRightTextAlignment];
1010 else if ( style & wxTE_CENTRE)
1012 [v setAlignment:NSCenterTextAlignment];
1015 NSTextFieldCell* cell = [v cell];
1016 [cell setScrollable:YES];
1017 // TODO: Remove if we definitely are sure, it's not needed
1018 // as setting scrolling to yes, should turn off any wrapping
1019 // [cell setLineBreakMode:NSLineBreakByClipping];
1021 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
1023 if ( (style & wxNO_BORDER) || (style & wxSIMPLE_BORDER) )
1025 // under 10.7 the textcontrol can draw its own focus
1026 // even if no border is shown, on previous systems
1027 // we have to emulate this
1030 if ( UMAGetSystemVersion() < 0x1070 )
1031 c->SetNeedsFocusRect( true );
1035 // use native border
1036 c->SetNeedsFrame(false);
1044 #endif // wxUSE_TEXTCTRL