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_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 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
186 NSView* otherView = wxOSXGetViewFromResponder(responder);
188 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
189 impl->DoNotifyFocusEvent( false, otherWindow );
193 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
195 wxUnusedVar(textView);
199 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( control );
202 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
205 if (commandSelector == @selector(insertNewline:))
207 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(wxpeer), wxTopLevelWindow);
208 if ( tlw && tlw->GetDefaultItem() )
210 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
211 if ( def && def->IsEnabled() )
213 wxCommandEvent event(wxEVT_BUTTON, def->GetId() );
214 event.SetEventObject(def);
228 @interface wxNSTextScrollView : NSScrollView
233 @implementation wxNSTextScrollView
237 static BOOL initialized = NO;
241 wxOSXCocoaClassAddWXMethods( self );
247 @implementation wxNSTextFieldEditor
249 - (void) keyDown:(NSEvent*) event
251 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
252 lastKeyDownEvent = event;
253 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
254 [super keyDown:event];
255 lastKeyDownEvent = nil;
258 - (void) keyUp:(NSEvent*) event
260 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
261 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
265 - (void) flagsChanged:(NSEvent*) event
267 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
268 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
269 [super flagsChanged:event];
272 - (BOOL) performKeyEquivalent:(NSEvent*) event
274 BOOL retval = [super performKeyEquivalent:event];
278 - (void) insertText:(id) str
280 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
281 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
283 [super insertText:str];
289 @implementation wxNSTextView
293 static BOOL initialized = NO;
297 wxOSXCocoaClassAddWXMethods( self );
301 - (void)textDidChange:(NSNotification *)aNotification
303 wxUnusedVar(aNotification);
304 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
306 impl->controlTextDidChange();
309 - (void) setEnabled:(BOOL) flag
311 // from Technical Q&A QA1461
313 [self setTextColor: [NSColor controlTextColor]];
316 [self setTextColor: [NSColor disabledControlTextColor]];
319 [self setSelectable: flag];
320 [self setEditable: flag];
325 return [self isEditable];
328 - (void)textDidEndEditing:(NSNotification *)aNotification
330 wxUnusedVar(aNotification);
332 if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
334 // This notification is generated as the result of calling our own
335 // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
336 // real focus loss event so skip generating it.
340 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
343 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
344 NSView* otherView = wxOSXGetViewFromResponder(responder);
346 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
347 impl->DoNotifyFocusEvent( false, otherWindow );
353 @implementation wxNSTextField
357 static BOOL initialized = NO;
361 wxOSXCocoaClassAddWXMethods( self );
365 - (id) initWithFrame:(NSRect) frame
367 self = [super initWithFrame:frame];
374 [fieldEditor release];
378 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
380 if ( editor != fieldEditor )
383 [fieldEditor release];
384 fieldEditor = editor;
388 - (wxNSTextFieldEditor*) fieldEditor
393 - (void) setEnabled:(BOOL) flag
395 [super setEnabled: flag];
397 if (![self drawsBackground]) {
398 // Static text is drawn incorrectly when disabled.
399 // For an explanation, see
400 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
402 [self setTextColor: [NSColor controlTextColor]];
404 [self setTextColor: [NSColor secondarySelectedControlColor]];
409 - (void)controlTextDidChange:(NSNotification *)aNotification
411 wxUnusedVar(aNotification);
412 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
414 impl->controlTextDidChange();
417 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
418 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
420 NSMutableArray* matches = NULL;
422 wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self );
423 wxTextEntry * const entry = impl->GetTextEntry();
424 wxTextCompleter * const completer = entry->OSXGetCompleter();
427 const wxString prefix = entry->GetValue();
428 if ( completer->Start(prefix) )
431 wordStart = wxCFStringRef::AsString(
432 [[textView string] substringWithRange:charRange]
435 matches = [NSMutableArray array];
438 const wxString s = completer->GetNext();
442 // Normally the completer should return only the strings
443 // starting with the prefix, but there could be exceptions
444 // and, for compatibility with MSW which simply ignores all
445 // entries that don't match the current text control contents,
446 // we ignore them as well. Besides, our own wxTextCompleterFixed
447 // doesn't respect this rule and, moreover, we need to extract
448 // just the rest of the string anyhow.
450 if ( s.StartsWith(prefix, &completion) )
452 // We discarded the entire prefix above but actually we
453 // should include the part of it that consists of the
454 // beginning of the current word, otherwise it would be
455 // lost when completion is accepted as OS X supposes that
456 // our matches do start with the "partial word range"
458 const wxCFStringRef fullWord(wordStart + completion);
459 [matches addObject: fullWord.AsNSString()];
468 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
470 wxUnusedVar(textView);
471 wxUnusedVar(control);
475 // send back key events wx' common code knows how to handle
477 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
480 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
483 if (commandSelector == @selector(insertNewline:))
485 [textView insertNewlineIgnoringFieldEditor:self];
488 else if ( commandSelector == @selector(insertTab:))
490 [textView insertTabIgnoringFieldEditor:self];
493 else if ( commandSelector == @selector(insertBacktab:))
495 [textView insertTabIgnoringFieldEditor:self];
504 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
506 wxUnusedVar(aNotification);
507 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
510 wxNSTextFieldControl* timpl = dynamic_cast<wxNSTextFieldControl*>(impl);
513 NSRange range = [fieldEditor selectedRange];
514 timpl->SetInternalSelection(range.location, range.location + range.length);
517 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
518 NSView* otherView = wxOSXGetViewFromResponder(responder);
520 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
521 impl->DoNotifyFocusEvent( false, otherWindow );
526 // wxNSTextViewControl
528 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
529 : wxWidgetCocoaImpl(wxPeer, w),
530 wxTextWidgetImpl(wxPeer)
532 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
535 [m_scrollView setHasVerticalScroller:YES];
536 [m_scrollView setHasHorizontalScroller:NO];
537 // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed
538 // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
539 NSSize contentSize = [m_scrollView contentSize];
541 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
542 contentSize.width, contentSize.height)];
544 [tv setVerticallyResizable:YES];
545 [tv setHorizontallyResizable:NO];
546 [tv setAutoresizingMask:NSViewWidthSizable];
548 [m_scrollView setDocumentView: tv];
550 [tv setDelegate: tv];
552 InstallEventHandler(tv);
555 wxNSTextViewControl::~wxNSTextViewControl()
558 [m_textView setDelegate: nil];
561 bool wxNSTextViewControl::CanFocus() const
563 // since this doesn't work (return false), we hardcode
565 // return [m_textView canBecomeKeyView];
569 wxString wxNSTextViewControl::GetStringValue() const
573 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
574 wxMacConvertNewlines13To10( &result ) ;
577 return wxEmptyString;
579 void wxNSTextViewControl::SetStringValue( const wxString &str)
582 wxMacConvertNewlines10To13( &st );
583 wxMacEditHelper helper(m_textView);
586 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
589 void wxNSTextViewControl::Copy()
592 [m_textView copy:nil];
596 void wxNSTextViewControl::Cut()
599 [m_textView cut:nil];
602 void wxNSTextViewControl::Paste()
605 [m_textView paste:nil];
608 bool wxNSTextViewControl::CanPaste() const
613 void wxNSTextViewControl::SetEditable(bool editable)
616 [m_textView setEditable: editable];
619 void wxNSTextViewControl::GetSelection( long* from, long* to) const
623 NSRange range = [m_textView selectedRange];
624 *from = range.location;
625 *to = range.location + range.length;
629 void wxNSTextViewControl::SetSelection( long from , long to )
631 long textLength = [[m_textView string] length];
632 if ((from == -1) && (to == -1))
639 from = wxMin(textLength,wxMax(from,0)) ;
643 to = wxMax(0,wxMin(textLength,to)) ;
646 NSRange selrange = NSMakeRange(from, to-from);
647 [m_textView setSelectedRange:selrange];
648 [m_textView scrollRangeToVisible:selrange];
651 void wxNSTextViewControl::WriteText(const wxString& str)
654 wxMacConvertNewlines10To13( &st );
655 wxMacEditHelper helper(m_textView);
656 NSEvent* formerEvent = m_lastKeyDownEvent;
657 m_lastKeyDownEvent = nil;
658 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
659 m_lastKeyDownEvent = formerEvent;
662 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
664 if ([m_textView respondsToSelector:@selector(setFont:)])
665 [m_textView setFont: font.OSXGetNSFont()];
668 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
670 if (m_textView && position >=0)
673 NSColor* bgcolor = NULL;
674 NSColor* fgcolor = NULL;
675 // NOTE: It appears that other platforms accept GetStyle with the position == length
676 // but that NSTextStorage does not accept length as a valid position.
677 // Therefore we return the default control style in that case.
678 if (position < (long) [[m_textView string] length])
680 NSTextStorage* storage = [m_textView textStorage];
681 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
682 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
683 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
687 NSDictionary* attrs = [m_textView typingAttributes];
688 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
689 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
690 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
694 style.SetFont(wxFont(font));
697 style.SetBackgroundColour(wxColour(bgcolor));
700 style.SetTextColour(wxColour(fgcolor));
707 void wxNSTextViewControl::SetStyle(long start,
709 const wxTextAttr& style)
714 if ( start == -1 && end == -1 )
716 NSMutableDictionary* const
717 attrs = [NSMutableDictionary dictionaryWithCapacity:3];
718 if ( style.HasFont() )
719 [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName];
720 if ( style.HasBackgroundColour() )
721 [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName];
722 if ( style.HasTextColour() )
723 [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName];
725 [m_textView setTypingAttributes:attrs];
727 else // Set the attributes just for this range.
729 NSRange range = NSMakeRange(start, end-start);
731 NSTextStorage* storage = [m_textView textStorage];
732 if ( style.HasFont() )
733 [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range];
735 if ( style.HasBackgroundColour() )
736 [storage addAttribute:NSBackgroundColorAttributeName value:style.GetBackgroundColour().OSXGetNSColor() range:range];
738 if ( style.HasTextColour() )
739 [storage addAttribute:NSForegroundColorAttributeName value:style.GetTextColour().OSXGetNSColor() range:range];
743 void wxNSTextViewControl::CheckSpelling(bool check)
746 [m_textView setContinuousSpellCheckingEnabled: check];
749 wxSize wxNSTextViewControl::GetBestSize() const
751 if (m_textView && [m_textView layoutManager])
753 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
754 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
755 (int)(rect.size.height + [m_textView textContainerInset].height));
760 // wxNSTextFieldControl
762 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
763 : wxWidgetCocoaImpl(text, w),
764 wxTextWidgetImpl(text)
769 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
772 : wxWidgetCocoaImpl(wxPeer, w),
773 wxTextWidgetImpl(entry)
778 void wxNSTextFieldControl::Init(WXWidget w)
780 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w;
782 [m_textField setDelegate: tf];
783 m_selStart = m_selEnd = 0;
784 m_hasEditor = [w isKindOfClass:[NSTextField class]];
787 wxNSTextFieldControl::~wxNSTextFieldControl()
790 [m_textField setDelegate: nil];
793 wxString wxNSTextFieldControl::GetStringValue() const
795 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
798 void wxNSTextFieldControl::SetStringValue( const wxString &str)
800 wxMacEditHelper helper(m_textField);
801 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
804 void wxNSTextFieldControl::SetMaxLength(unsigned long len)
806 wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease];
807 [formatter setMaxLength:len];
808 [m_textField setFormatter:formatter];
811 void wxNSTextFieldControl::Copy()
813 NSText* editor = [m_textField currentEditor];
820 void wxNSTextFieldControl::Cut()
822 NSText* editor = [m_textField currentEditor];
829 void wxNSTextFieldControl::Paste()
831 NSText* editor = [m_textField currentEditor];
838 bool wxNSTextFieldControl::CanPaste() const
843 void wxNSTextFieldControl::SetEditable(bool editable)
845 [m_textField setEditable:editable];
848 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
850 NSText* editor = [m_textField currentEditor];
853 NSRange range = [editor selectedRange];
854 *from = range.location;
855 *to = range.location + range.length;
864 void wxNSTextFieldControl::SetSelection( long from , long to )
866 long textLength = [[m_textField stringValue] length];
867 if ((from == -1) && (to == -1))
874 from = wxMin(textLength,wxMax(from,0)) ;
878 to = wxMax(0,wxMin(textLength,to)) ;
881 NSText* editor = [m_textField currentEditor];
884 [editor setSelectedRange:NSMakeRange(from, to-from)];
887 // the editor might still be in existence, but we might be already passed our 'focus lost' storage
888 // of the selection, so make sure we copy this
893 void wxNSTextFieldControl::WriteText(const wxString& str)
895 NSEvent* formerEvent = m_lastKeyDownEvent;
896 m_lastKeyDownEvent = nil;
897 NSText* editor = [m_textField currentEditor];
900 wxMacEditHelper helper(m_textField);
901 BOOL hasUndo = [editor respondsToSelector:@selector(setAllowsUndo:)];
903 [(NSTextView*)editor setAllowsUndo:NO];
904 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
906 [(NSTextView*)editor setAllowsUndo:YES];
910 wxString val = GetStringValue() ;
912 GetSelection( &start , &end ) ;
913 val.Remove( start , end - start ) ;
914 val.insert( start , str ) ;
915 SetStringValue( val ) ;
916 SetSelection( start + str.length() , start + str.length() ) ;
918 m_lastKeyDownEvent = formerEvent;
921 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
922 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
924 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
925 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
927 wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId());
928 event.SetEventObject( wxpeer );
929 event.SetString( GetTextEntry()->GetValue() );
930 wxpeer->HandleWindowEvent( event );
934 void wxNSTextFieldControl::SetInternalSelection( long from , long to )
940 // as becoming first responder on a window - triggers a resign on the same control, we have to avoid
941 // the resign notification writing back native selection values before we can set our own
943 static WXWidget s_widgetBecomingFirstResponder = nil;
945 bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd)
947 s_widgetBecomingFirstResponder = slf;
948 bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd);
949 s_widgetBecomingFirstResponder = nil;
952 NSText* editor = [m_textField currentEditor];
955 long textLength = [[m_textField stringValue] length];
956 m_selStart = wxMin(textLength,wxMax(m_selStart,0)) ;
957 m_selEnd = wxMax(0,wxMin(textLength,m_selEnd)) ;
959 [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)];
965 bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd)
967 if ( slf != s_widgetBecomingFirstResponder )
969 NSText* editor = [m_textField currentEditor];
972 NSRange range = [editor selectedRange];
973 m_selStart = range.location;
974 m_selEnd = range.location + range.length;
977 return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd);
980 bool wxNSTextFieldControl::SetHint(const wxString& hint)
982 wxCFStringRef hintstring(hint);
983 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
991 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
992 wxWindowMac* WXUNUSED(parent),
993 wxWindowID WXUNUSED(id),
994 const wxString& WXUNUSED(str),
998 long WXUNUSED(extraStyle))
1000 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1001 wxWidgetCocoaImpl* c = NULL;
1003 if ( style & wxTE_MULTILINE )
1005 wxNSTextScrollView* v = nil;
1006 v = [[wxNSTextScrollView alloc] initWithFrame:r];
1007 c = new wxNSTextViewControl( wxpeer, v );
1008 c->SetNeedsFocusRect( true );
1012 NSTextField* v = nil;
1013 if ( style & wxTE_PASSWORD )
1014 v = [[wxNSSecureTextField alloc] initWithFrame:r];
1016 v = [[wxNSTextField alloc] initWithFrame:r];
1018 if ( style & wxTE_RIGHT)
1020 [v setAlignment:NSRightTextAlignment];
1022 else if ( style & wxTE_CENTRE)
1024 [v setAlignment:NSCenterTextAlignment];
1027 NSTextFieldCell* cell = [v cell];
1028 [cell setScrollable:YES];
1029 // TODO: Remove if we definitely are sure, it's not needed
1030 // as setting scrolling to yes, should turn off any wrapping
1031 // [cell setLineBreakMode:NSLineBreakByClipping];
1033 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
1035 if ( (style & wxNO_BORDER) || (style & wxSIMPLE_BORDER) )
1037 // under 10.7 the textcontrol can draw its own focus
1038 // even if no border is shown, on previous systems
1039 // we have to emulate this
1042 if ( UMAGetSystemVersion() < 0x1070 )
1043 c->SetNeedsFocusRect( true );
1047 // use native border
1048 c->SetNeedsFrame(false);
1056 #endif // wxUSE_TEXTCTRL