1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/textctrl.mm
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
7 // RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
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"
49 #include "wx/osx/private.h"
50 #include "wx/osx/cocoa/private/textimpl.h"
52 @interface NSView(EditableView)
54 - (void)setEditable:(BOOL)flag;
56 - (void)setSelectable:(BOOL)flag;
62 wxMacEditHelper( NSView* textView )
64 m_textView = textView;
65 m_formerEditable = YES;
68 m_formerEditable = [textView isEditable];
69 m_formerSelectable = [textView isSelectable];
70 [textView setEditable:YES];
78 [m_textView setEditable:m_formerEditable];
79 [m_textView setSelectable:m_formerSelectable];
84 BOOL m_formerEditable ;
85 BOOL m_formerSelectable;
89 @implementation wxNSSecureTextField
93 static BOOL initialized = NO;
97 wxOSXCocoaClassAddWXMethods( self );
101 - (void)controlTextDidChange:(NSNotification *)aNotification
103 wxUnusedVar(aNotification);
104 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
106 impl->controlTextDidChange();
109 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
111 wxUnusedVar(aNotification);
112 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
115 impl->DoNotifyFocusEvent( false, NULL );
121 @interface wxNSTextScrollView : NSScrollView
126 @implementation wxNSTextScrollView
130 static BOOL initialized = NO;
134 wxOSXCocoaClassAddWXMethods( self );
140 @implementation wxNSTextFieldEditor
142 - (void) keyDown:(NSEvent*) event
144 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
145 lastKeyDownEvent = event;
146 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
147 [super keyDown:event];
148 lastKeyDownEvent = nil;
151 - (void) keyUp:(NSEvent*) event
153 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
154 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
158 - (void) flagsChanged:(NSEvent*) event
160 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
161 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
162 [super flagsChanged:event];
165 - (BOOL) performKeyEquivalent:(NSEvent*) event
167 BOOL retval = [super performKeyEquivalent:event];
171 - (void) insertText:(id) str
173 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
174 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
176 [super insertText:str];
182 @implementation wxNSTextView
186 static BOOL initialized = NO;
190 wxOSXCocoaClassAddWXMethods( self );
194 - (void)textDidChange:(NSNotification *)aNotification
196 wxUnusedVar(aNotification);
197 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
199 impl->controlTextDidChange();
202 - (void) setEnabled:(BOOL) flag
204 // from Technical Q&A QA1461
206 [self setTextColor: [NSColor controlTextColor]];
209 [self setTextColor: [NSColor disabledControlTextColor]];
212 [self setSelectable: flag];
213 [self setEditable: flag];
218 return [self isEditable];
223 @implementation wxNSTextField
227 static BOOL initialized = NO;
231 wxOSXCocoaClassAddWXMethods( self );
235 - (id) initWithFrame:(NSRect) frame
237 self = [super initWithFrame:frame];
244 [fieldEditor release];
248 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
250 fieldEditor = editor;
253 - (wxNSTextFieldEditor*) fieldEditor
258 - (void) setEnabled:(BOOL) flag
260 [super setEnabled: flag];
262 if (![self drawsBackground]) {
263 // Static text is drawn incorrectly when disabled.
264 // For an explanation, see
265 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
267 [self setTextColor: [NSColor controlTextColor]];
269 [self setTextColor: [NSColor secondarySelectedControlColor]];
274 - (void)controlTextDidChange:(NSNotification *)aNotification
276 wxUnusedVar(aNotification);
277 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
279 impl->controlTextDidChange();
282 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
284 wxUnusedVar(textView);
285 wxUnusedVar(control);
289 // send back key events wx' common code knows how to handle
291 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
294 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
297 if (commandSelector == @selector(insertNewline:))
299 [textView insertNewlineIgnoringFieldEditor:self];
302 else if ( commandSelector == @selector(insertTab:))
304 [textView insertTabIgnoringFieldEditor:self];
307 else if ( commandSelector == @selector(insertBacktab:))
309 [textView insertTabIgnoringFieldEditor:self];
318 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
320 wxUnusedVar(aNotification);
321 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
324 impl->DoNotifyFocusEvent( false, NULL );
329 // wxNSTextViewControl
331 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
332 : wxWidgetCocoaImpl(wxPeer, w),
333 wxTextWidgetImpl(wxPeer)
335 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
338 [m_scrollView setHasVerticalScroller:YES];
339 [m_scrollView setHasHorizontalScroller:NO];
340 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
341 NSSize contentSize = [m_scrollView contentSize];
343 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
344 contentSize.width, contentSize.height)];
346 [tv setVerticallyResizable:YES];
347 [tv setHorizontallyResizable:NO];
348 [tv setAutoresizingMask:NSViewWidthSizable];
350 [m_scrollView setDocumentView: tv];
352 [tv setDelegate: tv];
354 InstallEventHandler(tv);
357 wxNSTextViewControl::~wxNSTextViewControl()
360 [m_textView setDelegate: nil];
363 bool wxNSTextViewControl::CanFocus() const
365 // since this doesn't work (return false), we hardcode
367 // return [m_textView canBecomeKeyView];
371 wxString wxNSTextViewControl::GetStringValue() const
375 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
376 wxMacConvertNewlines13To10( &result ) ;
379 return wxEmptyString;
381 void wxNSTextViewControl::SetStringValue( const wxString &str)
384 wxMacConvertNewlines10To13( &st );
385 wxMacEditHelper helper(m_textView);
388 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
391 void wxNSTextViewControl::Copy()
394 [m_textView copy:nil];
398 void wxNSTextViewControl::Cut()
401 [m_textView cut:nil];
404 void wxNSTextViewControl::Paste()
407 [m_textView paste:nil];
410 bool wxNSTextViewControl::CanPaste() const
415 void wxNSTextViewControl::SetEditable(bool editable)
418 [m_textView setEditable: editable];
421 void wxNSTextViewControl::GetSelection( long* from, long* to) const
425 NSRange range = [m_textView selectedRange];
426 *from = range.location;
427 *to = range.location + range.length;
431 void wxNSTextViewControl::SetSelection( long from , long to )
433 long textLength = [[m_textView string] length];
434 if ((from == -1) && (to == -1))
441 from = wxMin(textLength,wxMax(from,0)) ;
445 to = wxMax(0,wxMin(textLength,to)) ;
448 NSRange selrange = NSMakeRange(from, to-from);
449 [m_textView setSelectedRange:selrange];
450 [m_textView scrollRangeToVisible:selrange];
453 void wxNSTextViewControl::WriteText(const wxString& str)
456 wxMacConvertNewlines10To13( &st );
457 wxMacEditHelper helper(m_textView);
458 NSEvent* formerEvent = m_lastKeyDownEvent;
459 m_lastKeyDownEvent = nil;
460 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
461 m_lastKeyDownEvent = formerEvent;
464 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
466 if ([m_textView respondsToSelector:@selector(setFont:)])
467 [m_textView setFont: font.OSXGetNSFont()];
470 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
472 if (m_textView && position >=0)
475 NSColor* bgcolor = NULL;
476 NSColor* fgcolor = NULL;
477 // NOTE: It appears that other platforms accept GetStyle with the position == length
478 // but that NSTextStorage does not accept length as a valid position.
479 // Therefore we return the default control style in that case.
480 if (position < [[m_textView string] length])
482 NSTextStorage* storage = [m_textView textStorage];
483 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
484 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
485 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
489 NSDictionary* attrs = [m_textView typingAttributes];
490 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
491 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
492 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
496 style.SetFont(wxFont(font));
499 style.SetBackgroundColour(wxColour(bgcolor));
502 style.SetTextColour(wxColour(fgcolor));
509 void wxNSTextViewControl::SetStyle(long start,
511 const wxTextAttr& style)
514 NSRange range = NSMakeRange(start, end-start);
515 if (start == -1 && end == -1)
516 range = [m_textView selectedRange];
518 NSTextStorage* storage = [m_textView textStorage];
520 wxFont font = style.GetFont();
521 if (style.HasFont() && font.IsOk())
522 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
524 wxColour bgcolor = style.GetBackgroundColour();
525 if (style.HasBackgroundColour() && bgcolor.IsOk())
526 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
528 wxColour fgcolor = style.GetTextColour();
529 if (style.HasTextColour() && fgcolor.IsOk())
530 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
534 void wxNSTextViewControl::CheckSpelling(bool check)
537 [m_textView setContinuousSpellCheckingEnabled: check];
540 wxSize wxNSTextViewControl::GetBestSize() const
542 if (m_textView && [m_textView layoutManager])
544 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
545 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
546 (int)(rect.size.height + [m_textView textContainerInset].height));
551 // wxNSTextFieldControl
553 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
554 : wxWidgetCocoaImpl(text, w),
555 wxTextWidgetImpl(text)
560 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
563 : wxWidgetCocoaImpl(wxPeer, w),
564 wxTextWidgetImpl(entry)
569 void wxNSTextFieldControl::Init(WXWidget w)
571 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
573 [m_textField setDelegate: tf];
574 m_selStart = m_selEnd = 0;
575 m_hasEditor = [w isKindOfClass:[NSTextField class]];
578 wxNSTextFieldControl::~wxNSTextFieldControl()
581 [m_textField setDelegate: nil];
584 wxString wxNSTextFieldControl::GetStringValue() const
586 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
589 void wxNSTextFieldControl::SetStringValue( const wxString &str)
591 wxMacEditHelper helper(m_textField);
592 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
595 void wxNSTextFieldControl::Copy()
597 NSText* editor = [m_textField currentEditor];
604 void wxNSTextFieldControl::Cut()
606 NSText* editor = [m_textField currentEditor];
613 void wxNSTextFieldControl::Paste()
615 NSText* editor = [m_textField currentEditor];
622 bool wxNSTextFieldControl::CanPaste() const
627 void wxNSTextFieldControl::SetEditable(bool editable)
629 [m_textField setEditable:editable];
632 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
634 NSText* editor = [m_textField currentEditor];
637 NSRange range = [editor selectedRange];
638 *from = range.location;
639 *to = range.location + range.length;
648 void wxNSTextFieldControl::SetSelection( long from , long to )
650 long textLength = [[m_textField stringValue] length];
651 if ((from == -1) && (to == -1))
658 from = wxMin(textLength,wxMax(from,0)) ;
662 to = wxMax(0,wxMin(textLength,to)) ;
665 NSText* editor = [m_textField currentEditor];
668 [editor setSelectedRange:NSMakeRange(from, to-from)];
677 void wxNSTextFieldControl::WriteText(const wxString& str)
679 NSEvent* formerEvent = m_lastKeyDownEvent;
680 m_lastKeyDownEvent = nil;
681 NSText* editor = [m_textField currentEditor];
684 wxMacEditHelper helper(m_textField);
685 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
689 wxString val = GetStringValue() ;
691 GetSelection( &start , &end ) ;
692 val.Remove( start , end - start ) ;
693 val.insert( start , str ) ;
694 SetStringValue( val ) ;
695 SetSelection( start + str.length() , start + str.length() ) ;
697 m_lastKeyDownEvent = formerEvent;
700 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
701 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
703 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
704 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
706 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
707 event.SetEventObject( wxpeer );
708 event.SetString( GetTextEntry()->GetValue() );
709 wxpeer->HandleWindowEvent( event );
713 bool wxNSTextFieldControl::SetHint(const wxString& hint)
715 wxCFStringRef hintstring(hint);
716 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
724 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
725 wxWindowMac* WXUNUSED(parent),
726 wxWindowID WXUNUSED(id),
727 const wxString& WXUNUSED(str),
731 long WXUNUSED(extraStyle))
733 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
734 wxWidgetCocoaImpl* c = NULL;
736 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
738 wxNSTextScrollView* v = nil;
739 v = [[wxNSTextScrollView alloc] initWithFrame:r];
740 c = new wxNSTextViewControl( wxpeer, v );
744 NSTextField* v = nil;
745 if ( style & wxTE_PASSWORD )
746 v = [[wxNSSecureTextField alloc] initWithFrame:r];
748 v = [[wxNSTextField alloc] initWithFrame:r];
750 if ( style & wxNO_BORDER )
752 // FIXME: How can we remove the native control's border?
753 // setBordered is separate from the text ctrl's border.
756 NSTextFieldCell* cell = [v cell];
757 [cell setScrollable:YES];
758 // TODO: Remove if we definitely are sure, it's not needed
759 // as setting scrolling to yes, should turn off any wrapping
760 // [cell setLineBreakMode:NSLineBreakByClipping];
765 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
772 #endif // wxUSE_TEXTCTRL