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];
221 - (void)textDidEndEditing:(NSNotification *)aNotification
223 wxUnusedVar(aNotification);
224 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
227 impl->DoNotifyFocusEvent( false, NULL );
233 @implementation wxNSTextField
237 static BOOL initialized = NO;
241 wxOSXCocoaClassAddWXMethods( self );
245 - (id) initWithFrame:(NSRect) frame
247 self = [super initWithFrame:frame];
254 [fieldEditor release];
258 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
260 fieldEditor = editor;
263 - (wxNSTextFieldEditor*) fieldEditor
268 - (void) setEnabled:(BOOL) flag
270 [super setEnabled: flag];
272 if (![self drawsBackground]) {
273 // Static text is drawn incorrectly when disabled.
274 // For an explanation, see
275 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
277 [self setTextColor: [NSColor controlTextColor]];
279 [self setTextColor: [NSColor secondarySelectedControlColor]];
284 - (void)controlTextDidChange:(NSNotification *)aNotification
286 wxUnusedVar(aNotification);
287 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
289 impl->controlTextDidChange();
292 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
294 wxUnusedVar(textView);
295 wxUnusedVar(control);
299 // send back key events wx' common code knows how to handle
301 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
304 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
307 if (commandSelector == @selector(insertNewline:))
309 [textView insertNewlineIgnoringFieldEditor:self];
312 else if ( commandSelector == @selector(insertTab:))
314 [textView insertTabIgnoringFieldEditor:self];
317 else if ( commandSelector == @selector(insertBacktab:))
319 [textView insertTabIgnoringFieldEditor:self];
328 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
330 wxUnusedVar(aNotification);
331 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
334 impl->DoNotifyFocusEvent( false, NULL );
339 // wxNSTextViewControl
341 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
342 : wxWidgetCocoaImpl(wxPeer, w),
343 wxTextWidgetImpl(wxPeer)
345 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
348 [m_scrollView setHasVerticalScroller:YES];
349 [m_scrollView setHasHorizontalScroller:NO];
350 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
351 NSSize contentSize = [m_scrollView contentSize];
353 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
354 contentSize.width, contentSize.height)];
356 [tv setVerticallyResizable:YES];
357 [tv setHorizontallyResizable:NO];
358 [tv setAutoresizingMask:NSViewWidthSizable];
360 [m_scrollView setDocumentView: tv];
362 [tv setDelegate: tv];
364 InstallEventHandler(tv);
367 wxNSTextViewControl::~wxNSTextViewControl()
370 [m_textView setDelegate: nil];
373 bool wxNSTextViewControl::CanFocus() const
375 // since this doesn't work (return false), we hardcode
377 // return [m_textView canBecomeKeyView];
381 wxString wxNSTextViewControl::GetStringValue() const
385 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
386 wxMacConvertNewlines13To10( &result ) ;
389 return wxEmptyString;
391 void wxNSTextViewControl::SetStringValue( const wxString &str)
394 wxMacConvertNewlines10To13( &st );
395 wxMacEditHelper helper(m_textView);
398 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
401 void wxNSTextViewControl::Copy()
404 [m_textView copy:nil];
408 void wxNSTextViewControl::Cut()
411 [m_textView cut:nil];
414 void wxNSTextViewControl::Paste()
417 [m_textView paste:nil];
420 bool wxNSTextViewControl::CanPaste() const
425 void wxNSTextViewControl::SetEditable(bool editable)
428 [m_textView setEditable: editable];
431 void wxNSTextViewControl::GetSelection( long* from, long* to) const
435 NSRange range = [m_textView selectedRange];
436 *from = range.location;
437 *to = range.location + range.length;
441 void wxNSTextViewControl::SetSelection( long from , long to )
443 long textLength = [[m_textView string] length];
444 if ((from == -1) && (to == -1))
451 from = wxMin(textLength,wxMax(from,0)) ;
455 to = wxMax(0,wxMin(textLength,to)) ;
458 NSRange selrange = NSMakeRange(from, to-from);
459 [m_textView setSelectedRange:selrange];
460 [m_textView scrollRangeToVisible:selrange];
463 void wxNSTextViewControl::WriteText(const wxString& str)
466 wxMacConvertNewlines10To13( &st );
467 wxMacEditHelper helper(m_textView);
468 NSEvent* formerEvent = m_lastKeyDownEvent;
469 m_lastKeyDownEvent = nil;
470 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
471 m_lastKeyDownEvent = formerEvent;
474 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
476 if ([m_textView respondsToSelector:@selector(setFont:)])
477 [m_textView setFont: font.OSXGetNSFont()];
480 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
482 if (m_textView && position >=0)
485 NSColor* bgcolor = NULL;
486 NSColor* fgcolor = NULL;
487 // NOTE: It appears that other platforms accept GetStyle with the position == length
488 // but that NSTextStorage does not accept length as a valid position.
489 // Therefore we return the default control style in that case.
490 if (position < (long) [[m_textView string] length])
492 NSTextStorage* storage = [m_textView textStorage];
493 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
494 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
495 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
499 NSDictionary* attrs = [m_textView typingAttributes];
500 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
501 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
502 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
506 style.SetFont(wxFont(font));
509 style.SetBackgroundColour(wxColour(bgcolor));
512 style.SetTextColour(wxColour(fgcolor));
519 void wxNSTextViewControl::SetStyle(long start,
521 const wxTextAttr& style)
524 NSRange range = NSMakeRange(start, end-start);
525 if (start == -1 && end == -1)
526 range = [m_textView selectedRange];
528 NSTextStorage* storage = [m_textView textStorage];
530 wxFont font = style.GetFont();
531 if (style.HasFont() && font.IsOk())
532 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
534 wxColour bgcolor = style.GetBackgroundColour();
535 if (style.HasBackgroundColour() && bgcolor.IsOk())
536 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
538 wxColour fgcolor = style.GetTextColour();
539 if (style.HasTextColour() && fgcolor.IsOk())
540 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
544 void wxNSTextViewControl::CheckSpelling(bool check)
547 [m_textView setContinuousSpellCheckingEnabled: check];
550 wxSize wxNSTextViewControl::GetBestSize() const
552 if (m_textView && [m_textView layoutManager])
554 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
555 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
556 (int)(rect.size.height + [m_textView textContainerInset].height));
561 // wxNSTextFieldControl
563 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
564 : wxWidgetCocoaImpl(text, w),
565 wxTextWidgetImpl(text)
570 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
573 : wxWidgetCocoaImpl(wxPeer, w),
574 wxTextWidgetImpl(entry)
579 void wxNSTextFieldControl::Init(WXWidget w)
581 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
583 [m_textField setDelegate: tf];
584 m_selStart = m_selEnd = 0;
585 m_hasEditor = [w isKindOfClass:[NSTextField class]];
588 wxNSTextFieldControl::~wxNSTextFieldControl()
591 [m_textField setDelegate: nil];
594 wxString wxNSTextFieldControl::GetStringValue() const
596 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
599 void wxNSTextFieldControl::SetStringValue( const wxString &str)
601 wxMacEditHelper helper(m_textField);
602 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
605 void wxNSTextFieldControl::Copy()
607 NSText* editor = [m_textField currentEditor];
614 void wxNSTextFieldControl::Cut()
616 NSText* editor = [m_textField currentEditor];
623 void wxNSTextFieldControl::Paste()
625 NSText* editor = [m_textField currentEditor];
632 bool wxNSTextFieldControl::CanPaste() const
637 void wxNSTextFieldControl::SetEditable(bool editable)
639 [m_textField setEditable:editable];
642 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
644 NSText* editor = [m_textField currentEditor];
647 NSRange range = [editor selectedRange];
648 *from = range.location;
649 *to = range.location + range.length;
658 void wxNSTextFieldControl::SetSelection( long from , long to )
660 long textLength = [[m_textField stringValue] length];
661 if ((from == -1) && (to == -1))
668 from = wxMin(textLength,wxMax(from,0)) ;
672 to = wxMax(0,wxMin(textLength,to)) ;
675 NSText* editor = [m_textField currentEditor];
678 [editor setSelectedRange:NSMakeRange(from, to-from)];
687 void wxNSTextFieldControl::WriteText(const wxString& str)
689 NSEvent* formerEvent = m_lastKeyDownEvent;
690 m_lastKeyDownEvent = nil;
691 NSText* editor = [m_textField currentEditor];
694 wxMacEditHelper helper(m_textField);
695 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
699 wxString val = GetStringValue() ;
701 GetSelection( &start , &end ) ;
702 val.Remove( start , end - start ) ;
703 val.insert( start , str ) ;
704 SetStringValue( val ) ;
705 SetSelection( start + str.length() , start + str.length() ) ;
707 m_lastKeyDownEvent = formerEvent;
710 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
711 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
713 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
714 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
716 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
717 event.SetEventObject( wxpeer );
718 event.SetString( GetTextEntry()->GetValue() );
719 wxpeer->HandleWindowEvent( event );
723 bool wxNSTextFieldControl::SetHint(const wxString& hint)
725 wxCFStringRef hintstring(hint);
726 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
734 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
735 wxWindowMac* WXUNUSED(parent),
736 wxWindowID WXUNUSED(id),
737 const wxString& WXUNUSED(str),
741 long WXUNUSED(extraStyle))
743 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
744 wxWidgetCocoaImpl* c = NULL;
746 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
748 wxNSTextScrollView* v = nil;
749 v = [[wxNSTextScrollView alloc] initWithFrame:r];
750 c = new wxNSTextViewControl( wxpeer, v );
754 NSTextField* v = nil;
755 if ( style & wxTE_PASSWORD )
756 v = [[wxNSSecureTextField alloc] initWithFrame:r];
758 v = [[wxNSTextField alloc] initWithFrame:r];
760 if ( style & wxNO_BORDER )
762 // FIXME: How can we remove the native control's border?
763 // setBordered is separate from the text ctrl's border.
766 NSTextFieldCell* cell = [v cell];
767 [cell setScrollable:YES];
768 // TODO: Remove if we definitely are sure, it's not needed
769 // as setting scrolling to yes, should turn off any wrapping
770 // [cell setLineBreakMode:NSLineBreakByClipping];
775 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
777 c->SetNeedsFocusRect( true );
783 #endif // wxUSE_TEXTCTRL