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
119 @implementation wxMaximumLengthFormatter
128 - (void) setMaxLength:(int) maxlen
133 - (NSString *)stringForObjectValue:(id)anObject
135 if(![anObject isKindOfClass:[NSString class]])
137 return [NSString stringWithString:anObject];
140 - (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error
142 *obj = [NSString stringWithString:string];
146 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
147 originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error
149 int len = [*partialStringPtr length];
150 if ( maxLength > 0 && len > maxLength )
152 field->SendMaxLenEvent();
158 - (void) setTextEntry:(wxTextEntry*) tf
165 @implementation wxNSSecureTextField
169 static BOOL initialized = NO;
173 wxOSXCocoaClassAddWXMethods( self );
177 - (void)controlTextDidChange:(NSNotification *)aNotification
179 wxUnusedVar(aNotification);
180 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
182 impl->controlTextDidChange();
185 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
187 wxUnusedVar(aNotification);
188 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
191 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
192 NSView* otherView = wxOSXGetViewFromResponder(responder);
194 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
195 impl->DoNotifyFocusEvent( false, otherWindow );
199 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
201 wxUnusedVar(textView);
205 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( control );
208 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
211 if (commandSelector == @selector(insertNewline:))
213 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(wxpeer), wxTopLevelWindow);
214 if ( tlw && tlw->GetDefaultItem() )
216 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
217 if ( def && def->IsEnabled() )
219 wxCommandEvent event(wxEVT_BUTTON, def->GetId() );
220 event.SetEventObject(def);
234 @interface wxNSTextScrollView : NSScrollView
239 @implementation wxNSTextScrollView
243 static BOOL initialized = NO;
247 wxOSXCocoaClassAddWXMethods( self );
253 @implementation wxNSTextFieldEditor
255 - (void) keyDown:(NSEvent*) event
257 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
258 lastKeyDownEvent = event;
259 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
260 [super keyDown:event];
261 lastKeyDownEvent = nil;
264 - (void) keyUp:(NSEvent*) event
266 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
267 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
271 - (void) flagsChanged:(NSEvent*) event
273 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
274 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
275 [super flagsChanged:event];
278 - (BOOL) performKeyEquivalent:(NSEvent*) event
280 BOOL retval = [super performKeyEquivalent:event];
284 - (void) insertText:(id) str
286 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
287 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
289 [super insertText:str];
295 @implementation wxNSTextView
299 static BOOL initialized = NO;
303 wxOSXCocoaClassAddWXMethods( self );
307 - (void)textDidChange:(NSNotification *)aNotification
309 wxUnusedVar(aNotification);
310 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
312 impl->controlTextDidChange();
315 - (void) setEnabled:(BOOL) flag
317 // from Technical Q&A QA1461
319 [self setTextColor: [NSColor controlTextColor]];
322 [self setTextColor: [NSColor disabledControlTextColor]];
325 [self setSelectable: flag];
326 [self setEditable: flag];
331 return [self isEditable];
334 - (void)textDidEndEditing:(NSNotification *)aNotification
336 wxUnusedVar(aNotification);
338 if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
340 // This notification is generated as the result of calling our own
341 // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
342 // real focus loss event so skip generating it.
346 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
349 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
350 NSView* otherView = wxOSXGetViewFromResponder(responder);
352 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
353 impl->DoNotifyFocusEvent( false, otherWindow );
359 @implementation wxNSTextField
363 static BOOL initialized = NO;
367 wxOSXCocoaClassAddWXMethods( self );
371 - (id) initWithFrame:(NSRect) frame
373 self = [super initWithFrame:frame];
380 [fieldEditor release];
384 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
386 if ( editor != fieldEditor )
389 [fieldEditor release];
390 fieldEditor = editor;
394 - (wxNSTextFieldEditor*) fieldEditor
399 - (void) setEnabled:(BOOL) flag
401 [super setEnabled: flag];
403 if (![self drawsBackground]) {
404 // Static text is drawn incorrectly when disabled.
405 // For an explanation, see
406 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
408 [self setTextColor: [NSColor controlTextColor]];
410 [self setTextColor: [NSColor secondarySelectedControlColor]];
415 - (void)controlTextDidChange:(NSNotification *)aNotification
417 wxUnusedVar(aNotification);
418 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
420 impl->controlTextDidChange();
423 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
424 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
426 NSMutableArray* matches = NULL;
428 wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self );
429 wxTextEntry * const entry = impl->GetTextEntry();
430 wxTextCompleter * const completer = entry->OSXGetCompleter();
433 const wxString prefix = entry->GetValue();
434 if ( completer->Start(prefix) )
437 wordStart = wxCFStringRef::AsString(
438 [[textView string] substringWithRange:charRange]
441 matches = [NSMutableArray array];
444 const wxString s = completer->GetNext();
448 // Normally the completer should return only the strings
449 // starting with the prefix, but there could be exceptions
450 // and, for compatibility with MSW which simply ignores all
451 // entries that don't match the current text control contents,
452 // we ignore them as well. Besides, our own wxTextCompleterFixed
453 // doesn't respect this rule and, moreover, we need to extract
454 // just the rest of the string anyhow.
456 if ( s.StartsWith(prefix, &completion) )
458 // We discarded the entire prefix above but actually we
459 // should include the part of it that consists of the
460 // beginning of the current word, otherwise it would be
461 // lost when completion is accepted as OS X supposes that
462 // our matches do start with the "partial word range"
464 const wxCFStringRef fullWord(wordStart + completion);
465 [matches addObject: fullWord.AsNSString()];
474 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
476 wxUnusedVar(textView);
477 wxUnusedVar(control);
481 // send back key events wx' common code knows how to handle
483 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
486 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
489 if (commandSelector == @selector(insertNewline:))
491 [textView insertNewlineIgnoringFieldEditor:self];
494 else if ( commandSelector == @selector(insertTab:))
496 [textView insertTabIgnoringFieldEditor:self];
499 else if ( commandSelector == @selector(insertBacktab:))
501 [textView insertTabIgnoringFieldEditor:self];
510 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
512 wxUnusedVar(aNotification);
513 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
516 wxNSTextFieldControl* timpl = dynamic_cast<wxNSTextFieldControl*>(impl);
519 NSRange range = [fieldEditor selectedRange];
520 timpl->SetInternalSelection(range.location, range.location + range.length);
523 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
524 NSView* otherView = wxOSXGetViewFromResponder(responder);
526 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
527 impl->DoNotifyFocusEvent( false, otherWindow );
532 // wxNSTextViewControl
534 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
535 : wxWidgetCocoaImpl(wxPeer, w),
536 wxTextWidgetImpl(wxPeer)
538 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
541 [m_scrollView setHasVerticalScroller:YES];
542 [m_scrollView setHasHorizontalScroller:NO];
543 // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed
544 // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
545 NSSize contentSize = [m_scrollView contentSize];
547 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
548 contentSize.width, contentSize.height)];
550 [tv setVerticallyResizable:YES];
551 [tv setHorizontallyResizable:NO];
552 [tv setAutoresizingMask:NSViewWidthSizable];
554 [m_scrollView setDocumentView: tv];
556 [tv setDelegate: tv];
558 InstallEventHandler(tv);
561 wxNSTextViewControl::~wxNSTextViewControl()
564 [m_textView setDelegate: nil];
567 bool wxNSTextViewControl::CanFocus() const
569 // since this doesn't work (return false), we hardcode
571 // return [m_textView canBecomeKeyView];
575 wxString wxNSTextViewControl::GetStringValue() const
579 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
580 wxMacConvertNewlines13To10( &result ) ;
583 return wxEmptyString;
585 void wxNSTextViewControl::SetStringValue( const wxString &str)
588 wxMacConvertNewlines10To13( &st );
589 wxMacEditHelper helper(m_textView);
592 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
595 void wxNSTextViewControl::Copy()
598 [m_textView copy:nil];
602 void wxNSTextViewControl::Cut()
605 [m_textView cut:nil];
608 void wxNSTextViewControl::Paste()
611 [m_textView paste:nil];
614 bool wxNSTextViewControl::CanPaste() const
619 void wxNSTextViewControl::SetEditable(bool editable)
622 [m_textView setEditable: editable];
625 void wxNSTextViewControl::GetSelection( long* from, long* to) const
629 NSRange range = [m_textView selectedRange];
630 *from = range.location;
631 *to = range.location + range.length;
635 void wxNSTextViewControl::SetSelection( long from , long to )
637 long textLength = [[m_textView string] length];
638 if ((from == -1) && (to == -1))
645 from = wxMin(textLength,wxMax(from,0)) ;
649 to = wxMax(0,wxMin(textLength,to)) ;
652 NSRange selrange = NSMakeRange(from, to-from);
653 [m_textView setSelectedRange:selrange];
654 [m_textView scrollRangeToVisible:selrange];
657 void wxNSTextViewControl::WriteText(const wxString& str)
660 wxMacConvertNewlines10To13( &st );
661 wxMacEditHelper helper(m_textView);
662 NSEvent* formerEvent = m_lastKeyDownEvent;
663 m_lastKeyDownEvent = nil;
664 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
665 m_lastKeyDownEvent = formerEvent;
668 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
670 if ([m_textView respondsToSelector:@selector(setFont:)])
671 [m_textView setFont: font.OSXGetNSFont()];
674 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
676 if (m_textView && position >=0)
679 NSColor* bgcolor = NULL;
680 NSColor* fgcolor = NULL;
681 // NOTE: It appears that other platforms accept GetStyle with the position == length
682 // but that NSTextStorage does not accept length as a valid position.
683 // Therefore we return the default control style in that case.
684 if (position < (long) [[m_textView string] length])
686 NSTextStorage* storage = [m_textView textStorage];
687 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
688 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
689 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
693 NSDictionary* attrs = [m_textView typingAttributes];
694 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
695 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
696 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
700 style.SetFont(wxFont(font));
703 style.SetBackgroundColour(wxColour(bgcolor));
706 style.SetTextColour(wxColour(fgcolor));
713 void wxNSTextViewControl::SetStyle(long start,
715 const wxTextAttr& style)
720 if ( start == -1 && end == -1 )
722 NSMutableDictionary* const
723 attrs = [NSMutableDictionary dictionaryWithCapacity:3];
724 if ( style.HasFont() )
725 [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName];
726 if ( style.HasBackgroundColour() )
727 [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName];
728 if ( style.HasTextColour() )
729 [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName];
731 [m_textView setTypingAttributes:attrs];
733 else // Set the attributes just for this range.
735 NSRange range = NSMakeRange(start, end-start);
737 NSTextStorage* storage = [m_textView textStorage];
738 if ( style.HasFont() )
739 [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range];
741 if ( style.HasBackgroundColour() )
742 [storage addAttribute:NSBackgroundColorAttributeName value:style.GetBackgroundColour().OSXGetNSColor() range:range];
744 if ( style.HasTextColour() )
745 [storage addAttribute:NSForegroundColorAttributeName value:style.GetTextColour().OSXGetNSColor() range:range];
749 void wxNSTextViewControl::CheckSpelling(bool check)
752 [m_textView setContinuousSpellCheckingEnabled: check];
755 wxSize wxNSTextViewControl::GetBestSize() const
757 if (m_textView && [m_textView layoutManager])
759 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
760 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
761 (int)(rect.size.height + [m_textView textContainerInset].height));
766 // wxNSTextFieldControl
768 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
769 : wxWidgetCocoaImpl(text, w),
770 wxTextWidgetImpl(text)
775 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
778 : wxWidgetCocoaImpl(wxPeer, w),
779 wxTextWidgetImpl(entry)
784 void wxNSTextFieldControl::Init(WXWidget w)
786 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w;
788 [m_textField setDelegate: tf];
789 m_selStart = m_selEnd = 0;
790 m_hasEditor = [w isKindOfClass:[NSTextField class]];
793 wxNSTextFieldControl::~wxNSTextFieldControl()
796 [m_textField setDelegate: nil];
799 wxString wxNSTextFieldControl::GetStringValue() const
801 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
804 void wxNSTextFieldControl::SetStringValue( const wxString &str)
806 wxMacEditHelper helper(m_textField);
807 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
810 void wxNSTextFieldControl::SetMaxLength(unsigned long len)
812 wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease];
813 [formatter setMaxLength:len];
814 [formatter setTextEntry:GetTextEntry()];
815 [m_textField setFormatter:formatter];
818 void wxNSTextFieldControl::Copy()
820 NSText* editor = [m_textField currentEditor];
827 void wxNSTextFieldControl::Cut()
829 NSText* editor = [m_textField currentEditor];
836 void wxNSTextFieldControl::Paste()
838 NSText* editor = [m_textField currentEditor];
845 bool wxNSTextFieldControl::CanPaste() const
850 void wxNSTextFieldControl::SetEditable(bool editable)
852 [m_textField setEditable:editable];
855 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
857 NSText* editor = [m_textField currentEditor];
860 NSRange range = [editor selectedRange];
861 *from = range.location;
862 *to = range.location + range.length;
871 void wxNSTextFieldControl::SetSelection( long from , long to )
873 long textLength = [[m_textField stringValue] length];
874 if ((from == -1) && (to == -1))
881 from = wxMin(textLength,wxMax(from,0)) ;
885 to = wxMax(0,wxMin(textLength,to)) ;
888 NSText* editor = [m_textField currentEditor];
891 [editor setSelectedRange:NSMakeRange(from, to-from)];
894 // the editor might still be in existence, but we might be already passed our 'focus lost' storage
895 // of the selection, so make sure we copy this
900 void wxNSTextFieldControl::WriteText(const wxString& str)
902 NSEvent* formerEvent = m_lastKeyDownEvent;
903 m_lastKeyDownEvent = nil;
904 NSText* editor = [m_textField currentEditor];
907 wxMacEditHelper helper(m_textField);
908 BOOL hasUndo = [editor respondsToSelector:@selector(setAllowsUndo:)];
910 [(NSTextView*)editor setAllowsUndo:NO];
911 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
913 [(NSTextView*)editor setAllowsUndo:YES];
917 wxString val = GetStringValue() ;
919 GetSelection( &start , &end ) ;
920 val.Remove( start , end - start ) ;
921 val.insert( start , str ) ;
922 SetStringValue( val ) ;
923 SetSelection( start + str.length() , start + str.length() ) ;
925 m_lastKeyDownEvent = formerEvent;
928 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
929 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
931 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
932 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
934 wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId());
935 event.SetEventObject( wxpeer );
936 event.SetString( GetTextEntry()->GetValue() );
937 wxpeer->HandleWindowEvent( event );
941 void wxNSTextFieldControl::SetInternalSelection( long from , long to )
947 // as becoming first responder on a window - triggers a resign on the same control, we have to avoid
948 // the resign notification writing back native selection values before we can set our own
950 static WXWidget s_widgetBecomingFirstResponder = nil;
952 bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd)
954 s_widgetBecomingFirstResponder = slf;
955 bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd);
956 s_widgetBecomingFirstResponder = nil;
959 NSText* editor = [m_textField currentEditor];
962 long textLength = [[m_textField stringValue] length];
963 m_selStart = wxMin(textLength,wxMax(m_selStart,0)) ;
964 m_selEnd = wxMax(0,wxMin(textLength,m_selEnd)) ;
966 [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)];
972 bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd)
974 if ( slf != s_widgetBecomingFirstResponder )
976 NSText* editor = [m_textField currentEditor];
979 NSRange range = [editor selectedRange];
980 m_selStart = range.location;
981 m_selEnd = range.location + range.length;
984 return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd);
987 bool wxNSTextFieldControl::SetHint(const wxString& hint)
989 wxCFStringRef hintstring(hint);
990 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
998 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
999 wxWindowMac* WXUNUSED(parent),
1000 wxWindowID WXUNUSED(id),
1001 const wxString& WXUNUSED(str),
1005 long WXUNUSED(extraStyle))
1007 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1008 wxWidgetCocoaImpl* c = NULL;
1010 if ( style & wxTE_MULTILINE )
1012 wxNSTextScrollView* v = nil;
1013 v = [[wxNSTextScrollView alloc] initWithFrame:r];
1014 c = new wxNSTextViewControl( wxpeer, v );
1015 c->SetNeedsFocusRect( true );
1019 NSTextField* v = nil;
1020 if ( style & wxTE_PASSWORD )
1021 v = [[wxNSSecureTextField alloc] initWithFrame:r];
1023 v = [[wxNSTextField alloc] initWithFrame:r];
1025 if ( style & wxTE_RIGHT)
1027 [v setAlignment:NSRightTextAlignment];
1029 else if ( style & wxTE_CENTRE)
1031 [v setAlignment:NSCenterTextAlignment];
1034 NSTextFieldCell* cell = [v cell];
1035 [cell setScrollable:YES];
1036 // TODO: Remove if we definitely are sure, it's not needed
1037 // as setting scrolling to yes, should turn off any wrapping
1038 // [cell setLineBreakMode:NSLineBreakByClipping];
1040 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
1042 if ( (style & wxNO_BORDER) || (style & wxSIMPLE_BORDER) )
1044 // under 10.7 the textcontrol can draw its own focus
1045 // even if no border is shown, on previous systems
1046 // we have to emulate this
1049 if ( UMAGetSystemVersion() < 0x1070 )
1050 c->SetNeedsFocusRect( true );
1054 // use native border
1055 c->SetNeedsFrame(false);
1063 #endif // wxUSE_TEXTCTRL