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 #include "wx/cocoa/autorelease.h"
54 @interface NSView(EditableView)
56 - (void)setEditable:(BOOL)flag;
58 - (void)setSelectable:(BOOL)flag;
61 // An object of this class is created before the text is modified
62 // programmatically and destroyed as soon as this is done. It does several
63 // things, like ensuring that the control is editable to allow setting its text
64 // at all and eating any unwanted focus loss events from textDidEndEditing:
65 // which don't really correspond to focus change.
69 wxMacEditHelper( NSView* textView )
71 m_viewPreviouslyEdited = ms_viewCurrentlyEdited;
72 ms_viewCurrentlyEdited =
73 m_textView = textView;
74 m_formerEditable = YES;
77 m_formerEditable = [textView isEditable];
78 m_formerSelectable = [textView isSelectable];
79 [textView setEditable:YES];
87 [m_textView setEditable:m_formerEditable];
88 [m_textView setSelectable:m_formerSelectable];
91 ms_viewCurrentlyEdited = m_viewPreviouslyEdited;
94 // Returns the last view we were instantiated for or NULL.
95 static NSView *GetCurrentlyEditedView() { return ms_viewCurrentlyEdited; }
98 BOOL m_formerEditable ;
99 BOOL m_formerSelectable;
102 // The original value of ms_viewCurrentlyEdited when this object was
104 NSView* m_viewPreviouslyEdited;
106 static NSView* ms_viewCurrentlyEdited;
109 NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
111 // a minimal NSFormatter that just avoids getting too long entries
112 @interface wxMaximumLengthFormatter : NSFormatter
120 @implementation wxMaximumLengthFormatter
129 - (void) setMaxLength:(int) maxlen
134 - (NSString *)stringForObjectValue:(id)anObject
136 if(![anObject isKindOfClass:[NSString class]])
138 return [NSString stringWithString:anObject];
141 - (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error
143 *obj = [NSString stringWithString:string];
147 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
148 originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error
150 int len = [*partialStringPtr length];
151 if ( maxLength > 0 && len > maxLength )
153 field->SendMaxLenEvent();
159 - (void) setTextEntry:(wxTextEntry*) tf
166 @implementation wxNSSecureTextField
170 static BOOL initialized = NO;
174 wxOSXCocoaClassAddWXMethods( self );
178 - (void)controlTextDidChange:(NSNotification *)aNotification
180 wxUnusedVar(aNotification);
181 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
183 impl->controlTextDidChange();
186 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
188 wxUnusedVar(aNotification);
189 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
192 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
193 NSView* otherView = wxOSXGetViewFromResponder(responder);
195 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
196 impl->DoNotifyFocusEvent( false, otherWindow );
200 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
202 wxUnusedVar(textView);
206 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( control );
209 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
212 if (commandSelector == @selector(insertNewline:))
214 wxTopLevelWindow *tlw = wxDynamicCast(wxGetTopLevelParent(wxpeer), wxTopLevelWindow);
215 if ( tlw && tlw->GetDefaultItem() )
217 wxButton *def = wxDynamicCast(tlw->GetDefaultItem(), wxButton);
218 if ( def && def->IsEnabled() )
220 wxCommandEvent event(wxEVT_BUTTON, def->GetId() );
221 event.SetEventObject(def);
235 @interface wxNSTextScrollView : NSScrollView
240 @implementation wxNSTextScrollView
244 static BOOL initialized = NO;
248 wxOSXCocoaClassAddWXMethods( self );
254 @implementation wxNSTextFieldEditor
256 - (void) keyDown:(NSEvent*) event
258 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
259 lastKeyDownEvent = event;
260 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
261 [super keyDown:event];
262 lastKeyDownEvent = nil;
265 - (void) keyUp:(NSEvent*) event
267 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
268 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
272 - (void) flagsChanged:(NSEvent*) event
274 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
275 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
276 [super flagsChanged:event];
279 - (BOOL) performKeyEquivalent:(NSEvent*) event
281 BOOL retval = [super performKeyEquivalent:event];
285 - (void) insertText:(id) str
287 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
288 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
290 [super insertText:str];
296 @implementation wxNSTextView
300 static BOOL initialized = NO;
304 wxOSXCocoaClassAddWXMethods( self );
308 - (void)textDidChange:(NSNotification *)aNotification
310 wxUnusedVar(aNotification);
311 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
313 impl->controlTextDidChange();
316 - (void) setEnabled:(BOOL) flag
318 // from Technical Q&A QA1461
320 [self setTextColor: [NSColor controlTextColor]];
323 [self setTextColor: [NSColor disabledControlTextColor]];
326 [self setSelectable: flag];
327 [self setEditable: flag];
332 return [self isEditable];
335 - (void)textDidEndEditing:(NSNotification *)aNotification
337 wxUnusedVar(aNotification);
339 if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
341 // This notification is generated as the result of calling our own
342 // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
343 // real focus loss event so skip generating it.
347 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
350 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
351 NSView* otherView = wxOSXGetViewFromResponder(responder);
353 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
354 impl->DoNotifyFocusEvent( false, otherWindow );
360 @implementation wxNSTextField
364 static BOOL initialized = NO;
368 wxOSXCocoaClassAddWXMethods( self );
372 - (id) initWithFrame:(NSRect) frame
374 self = [super initWithFrame:frame];
381 [fieldEditor release];
385 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
387 if ( editor != fieldEditor )
390 [fieldEditor release];
391 fieldEditor = editor;
395 - (wxNSTextFieldEditor*) fieldEditor
400 - (void) setEnabled:(BOOL) flag
402 [super setEnabled: flag];
404 if (![self drawsBackground]) {
405 // Static text is drawn incorrectly when disabled.
406 // For an explanation, see
407 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
409 [self setTextColor: [NSColor controlTextColor]];
411 [self setTextColor: [NSColor secondarySelectedControlColor]];
416 - (void)controlTextDidChange:(NSNotification *)aNotification
418 wxUnusedVar(aNotification);
419 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
421 impl->controlTextDidChange();
424 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
425 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
427 NSMutableArray* matches = NULL;
429 wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self );
430 wxTextEntry * const entry = impl->GetTextEntry();
431 wxTextCompleter * const completer = entry->OSXGetCompleter();
434 const wxString prefix = entry->GetValue();
435 if ( completer->Start(prefix) )
438 wordStart = wxCFStringRef::AsString(
439 [[textView string] substringWithRange:charRange]
442 matches = [NSMutableArray array];
445 const wxString s = completer->GetNext();
449 // Normally the completer should return only the strings
450 // starting with the prefix, but there could be exceptions
451 // and, for compatibility with MSW which simply ignores all
452 // entries that don't match the current text control contents,
453 // we ignore them as well. Besides, our own wxTextCompleterFixed
454 // doesn't respect this rule and, moreover, we need to extract
455 // just the rest of the string anyhow.
457 if ( s.StartsWith(prefix, &completion) )
459 // We discarded the entire prefix above but actually we
460 // should include the part of it that consists of the
461 // beginning of the current word, otherwise it would be
462 // lost when completion is accepted as OS X supposes that
463 // our matches do start with the "partial word range"
465 const wxCFStringRef fullWord(wordStart + completion);
466 [matches addObject: fullWord.AsNSString()];
475 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
477 wxUnusedVar(textView);
478 wxUnusedVar(control);
482 // send back key events wx' common code knows how to handle
484 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
487 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
490 if (commandSelector == @selector(insertNewline:))
492 [textView insertNewlineIgnoringFieldEditor:self];
495 else if ( commandSelector == @selector(insertTab:))
497 [textView insertTabIgnoringFieldEditor:self];
500 else if ( commandSelector == @selector(insertBacktab:))
502 [textView insertTabIgnoringFieldEditor:self];
511 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
513 wxUnusedVar(aNotification);
514 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
517 wxNSTextFieldControl* timpl = dynamic_cast<wxNSTextFieldControl*>(impl);
520 NSRange range = [fieldEditor selectedRange];
521 timpl->SetInternalSelection(range.location, range.location + range.length);
524 NSResponder * responder = wxNonOwnedWindowCocoaImpl::GetNextFirstResponder();
525 NSView* otherView = wxOSXGetViewFromResponder(responder);
527 wxWidgetImpl* otherWindow = impl->FindBestFromWXWidget(otherView);
528 impl->DoNotifyFocusEvent( false, otherWindow );
533 // wxNSTextViewControl
535 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
536 : wxWidgetCocoaImpl(wxPeer, w),
537 wxTextWidgetImpl(wxPeer)
539 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
542 [m_scrollView setHasVerticalScroller:YES];
543 [m_scrollView setHasHorizontalScroller:NO];
544 // TODO Remove if no regression, this was causing automatic resizes of multi-line textfields when the tlw changed
545 // [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
546 NSSize contentSize = [m_scrollView contentSize];
548 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
549 contentSize.width, contentSize.height)];
551 [tv setVerticallyResizable:YES];
552 [tv setHorizontallyResizable:NO];
553 [tv setAutoresizingMask:NSViewWidthSizable];
555 [m_scrollView setDocumentView: tv];
557 [tv setDelegate: tv];
559 InstallEventHandler(tv);
562 wxNSTextViewControl::~wxNSTextViewControl()
565 [m_textView setDelegate: nil];
568 bool wxNSTextViewControl::CanFocus() const
570 // since this doesn't work (return false), we hardcode
572 // return [m_textView canBecomeKeyView];
576 wxString wxNSTextViewControl::GetStringValue() const
580 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
581 wxMacConvertNewlines13To10( &result ) ;
584 return wxEmptyString;
586 void wxNSTextViewControl::SetStringValue( const wxString &str)
589 wxMacConvertNewlines10To13( &st );
590 wxMacEditHelper helper(m_textView);
593 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
596 void wxNSTextViewControl::Copy()
599 [m_textView copy:nil];
603 void wxNSTextViewControl::Cut()
606 [m_textView cut:nil];
609 void wxNSTextViewControl::Paste()
612 [m_textView paste:nil];
615 bool wxNSTextViewControl::CanPaste() const
620 void wxNSTextViewControl::SetEditable(bool editable)
623 [m_textView setEditable: editable];
626 void wxNSTextViewControl::GetSelection( long* from, long* to) const
630 NSRange range = [m_textView selectedRange];
631 *from = range.location;
632 *to = range.location + range.length;
636 void wxNSTextViewControl::SetSelection( long from , long to )
638 long textLength = [[m_textView string] length];
639 if ((from == -1) && (to == -1))
646 from = wxMin(textLength,wxMax(from,0)) ;
650 to = wxMax(0,wxMin(textLength,to)) ;
653 NSRange selrange = NSMakeRange(from, to-from);
654 [m_textView setSelectedRange:selrange];
655 [m_textView scrollRangeToVisible:selrange];
658 void wxNSTextViewControl::WriteText(const wxString& str)
661 wxMacConvertNewlines10To13( &st );
662 wxMacEditHelper helper(m_textView);
663 NSEvent* formerEvent = m_lastKeyDownEvent;
664 m_lastKeyDownEvent = nil;
665 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
666 m_lastKeyDownEvent = formerEvent;
669 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
671 if ([m_textView respondsToSelector:@selector(setFont:)])
672 [m_textView setFont: font.OSXGetNSFont()];
675 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
677 if (m_textView && position >=0)
680 NSColor* bgcolor = NULL;
681 NSColor* fgcolor = NULL;
682 // NOTE: It appears that other platforms accept GetStyle with the position == length
683 // but that NSTextStorage does not accept length as a valid position.
684 // Therefore we return the default control style in that case.
685 if (position < (long) [[m_textView string] length])
687 NSTextStorage* storage = [m_textView textStorage];
688 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
689 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
690 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
694 NSDictionary* attrs = [m_textView typingAttributes];
695 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
696 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
697 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
701 style.SetFont(wxFont(font));
704 style.SetBackgroundColour(wxColour(bgcolor));
707 style.SetTextColour(wxColour(fgcolor));
714 void wxNSTextViewControl::SetStyle(long start,
716 const wxTextAttr& style)
721 if ( start == -1 && end == -1 )
723 NSMutableDictionary* const
724 attrs = [NSMutableDictionary dictionaryWithCapacity:3];
725 if ( style.HasFont() )
726 [attrs setValue:style.GetFont().OSXGetNSFont() forKey:NSFontAttributeName];
727 if ( style.HasBackgroundColour() )
728 [attrs setValue:style.GetBackgroundColour().OSXGetNSColor() forKey:NSBackgroundColorAttributeName];
729 if ( style.HasTextColour() )
730 [attrs setValue:style.GetTextColour().OSXGetNSColor() forKey:NSForegroundColorAttributeName];
732 [m_textView setTypingAttributes:attrs];
734 else // Set the attributes just for this range.
736 NSRange range = NSMakeRange(start, end-start);
738 NSTextStorage* storage = [m_textView textStorage];
739 if ( style.HasFont() )
740 [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range];
742 if ( style.HasBackgroundColour() )
743 [storage addAttribute:NSBackgroundColorAttributeName value:style.GetBackgroundColour().OSXGetNSColor() range:range];
745 if ( style.HasTextColour() )
746 [storage addAttribute:NSForegroundColorAttributeName value:style.GetTextColour().OSXGetNSColor() range:range];
750 void wxNSTextViewControl::CheckSpelling(bool check)
753 [m_textView setContinuousSpellCheckingEnabled: check];
756 wxSize wxNSTextViewControl::GetBestSize() const
758 if (m_textView && [m_textView layoutManager])
760 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
761 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
762 (int)(rect.size.height + [m_textView textContainerInset].height));
767 // wxNSTextFieldControl
769 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
770 : wxWidgetCocoaImpl(text, w),
771 wxTextWidgetImpl(text)
776 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
779 : wxWidgetCocoaImpl(wxPeer, w),
780 wxTextWidgetImpl(entry)
785 void wxNSTextFieldControl::Init(WXWidget w)
787 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w;
789 [m_textField setDelegate: tf];
790 m_selStart = m_selEnd = 0;
791 m_hasEditor = [w isKindOfClass:[NSTextField class]];
794 wxNSTextFieldControl::~wxNSTextFieldControl()
797 [m_textField setDelegate: nil];
800 wxString wxNSTextFieldControl::GetStringValue() const
802 wxAutoNSAutoreleasePool pool;
803 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
806 void wxNSTextFieldControl::SetStringValue( const wxString &str)
808 wxMacEditHelper helper(m_textField);
809 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
812 void wxNSTextFieldControl::SetMaxLength(unsigned long len)
814 wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease];
815 [formatter setMaxLength:len];
816 [formatter setTextEntry:GetTextEntry()];
817 [m_textField setFormatter:formatter];
820 void wxNSTextFieldControl::Copy()
822 NSText* editor = [m_textField currentEditor];
829 void wxNSTextFieldControl::Cut()
831 NSText* editor = [m_textField currentEditor];
838 void wxNSTextFieldControl::Paste()
840 NSText* editor = [m_textField currentEditor];
847 bool wxNSTextFieldControl::CanPaste() const
852 void wxNSTextFieldControl::SetEditable(bool editable)
854 [m_textField setEditable:editable];
857 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
859 NSText* editor = [m_textField currentEditor];
862 NSRange range = [editor selectedRange];
863 *from = range.location;
864 *to = range.location + range.length;
873 void wxNSTextFieldControl::SetSelection( long from , long to )
875 long textLength = [[m_textField stringValue] length];
876 if ((from == -1) && (to == -1))
883 from = wxMin(textLength,wxMax(from,0)) ;
887 to = wxMax(0,wxMin(textLength,to)) ;
890 NSText* editor = [m_textField currentEditor];
893 [editor setSelectedRange:NSMakeRange(from, to-from)];
896 // the editor might still be in existence, but we might be already passed our 'focus lost' storage
897 // of the selection, so make sure we copy this
902 void wxNSTextFieldControl::WriteText(const wxString& str)
904 NSEvent* formerEvent = m_lastKeyDownEvent;
905 m_lastKeyDownEvent = nil;
906 NSText* editor = [m_textField currentEditor];
909 wxMacEditHelper helper(m_textField);
910 BOOL hasUndo = [editor respondsToSelector:@selector(setAllowsUndo:)];
912 [(NSTextView*)editor setAllowsUndo:NO];
913 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
915 [(NSTextView*)editor setAllowsUndo:YES];
919 wxString val = GetStringValue() ;
921 GetSelection( &start , &end ) ;
922 val.Remove( start , end - start ) ;
923 val.insert( start , str ) ;
924 SetStringValue( val ) ;
925 SetSelection( start + str.length() , start + str.length() ) ;
927 m_lastKeyDownEvent = formerEvent;
930 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
931 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
933 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
934 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
936 wxCommandEvent event(wxEVT_TEXT_ENTER, wxpeer->GetId());
937 event.SetEventObject( wxpeer );
938 event.SetString( GetTextEntry()->GetValue() );
939 wxpeer->HandleWindowEvent( event );
943 void wxNSTextFieldControl::SetInternalSelection( long from , long to )
949 // as becoming first responder on a window - triggers a resign on the same control, we have to avoid
950 // the resign notification writing back native selection values before we can set our own
952 static WXWidget s_widgetBecomingFirstResponder = nil;
954 bool wxNSTextFieldControl::becomeFirstResponder(WXWidget slf, void *_cmd)
956 s_widgetBecomingFirstResponder = slf;
957 bool retval = wxWidgetCocoaImpl::becomeFirstResponder(slf, _cmd);
958 s_widgetBecomingFirstResponder = nil;
961 NSText* editor = [m_textField currentEditor];
964 long textLength = [[m_textField stringValue] length];
965 m_selStart = wxMin(textLength,wxMax(m_selStart,0)) ;
966 m_selEnd = wxMax(0,wxMin(textLength,m_selEnd)) ;
968 [editor setSelectedRange:NSMakeRange(m_selStart, m_selEnd-m_selStart)];
974 bool wxNSTextFieldControl::resignFirstResponder(WXWidget slf, void *_cmd)
976 if ( slf != s_widgetBecomingFirstResponder )
978 NSText* editor = [m_textField currentEditor];
981 NSRange range = [editor selectedRange];
982 m_selStart = range.location;
983 m_selEnd = range.location + range.length;
986 return wxWidgetCocoaImpl::resignFirstResponder(slf, _cmd);
989 bool wxNSTextFieldControl::SetHint(const wxString& hint)
991 wxCFStringRef hintstring(hint);
992 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
1000 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
1001 wxWindowMac* WXUNUSED(parent),
1002 wxWindowID WXUNUSED(id),
1003 const wxString& WXUNUSED(str),
1007 long WXUNUSED(extraStyle))
1009 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
1010 wxWidgetCocoaImpl* c = NULL;
1012 if ( style & wxTE_MULTILINE )
1014 wxNSTextScrollView* v = nil;
1015 v = [[wxNSTextScrollView alloc] initWithFrame:r];
1016 c = new wxNSTextViewControl( wxpeer, v );
1017 c->SetNeedsFocusRect( true );
1021 NSTextField* v = nil;
1022 if ( style & wxTE_PASSWORD )
1023 v = [[wxNSSecureTextField alloc] initWithFrame:r];
1025 v = [[wxNSTextField alloc] initWithFrame:r];
1027 if ( style & wxTE_RIGHT)
1029 [v setAlignment:NSRightTextAlignment];
1031 else if ( style & wxTE_CENTRE)
1033 [v setAlignment:NSCenterTextAlignment];
1036 NSTextFieldCell* cell = [v cell];
1037 [cell setScrollable:YES];
1038 // TODO: Remove if we definitely are sure, it's not needed
1039 // as setting scrolling to yes, should turn off any wrapping
1040 // [cell setLineBreakMode:NSLineBreakByClipping];
1042 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
1044 if ( (style & wxNO_BORDER) || (style & wxSIMPLE_BORDER) )
1046 // under 10.7 the textcontrol can draw its own focus
1047 // even if no border is shown, on previous systems
1048 // we have to emulate this
1051 if ( UMAGetSystemVersion() < 0x1070 )
1052 c->SetNeedsFocusRect( true );
1056 // use native border
1057 c->SetNeedsFrame(false);
1065 #endif // wxUSE_TEXTCTRL