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;
60 wxMacEditHelper( NSView* textView )
62 m_textView = textView;
66 m_formerState = [textView isEditable];
67 [textView setEditable:YES];
74 [m_textView setEditable:m_formerState];
82 @implementation wxNSSecureTextField
86 static BOOL initialized = NO;
90 wxOSXCocoaClassAddWXMethods( self );
94 - (void)controlTextDidChange:(NSNotification *)aNotification
96 wxUnusedVar(aNotification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
99 impl->controlTextDidChange();
102 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
104 wxUnusedVar(aNotification);
105 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
108 impl->DoNotifyFocusEvent( false, NULL );
114 @interface wxNSTextScrollView : NSScrollView
119 @implementation wxNSTextScrollView
123 static BOOL initialized = NO;
127 wxOSXCocoaClassAddWXMethods( self );
133 @implementation wxNSTextFieldEditor
135 - (void) keyDown:(NSEvent*) event
137 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
138 lastKeyDownEvent = event;
139 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
140 [super keyDown:event];
141 lastKeyDownEvent = nil;
144 - (void) keyUp:(NSEvent*) event
146 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
147 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
151 - (void) flagsChanged:(NSEvent*) event
153 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
154 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
155 [super flagsChanged:event];
158 - (BOOL) performKeyEquivalent:(NSEvent*) event
160 BOOL retval = [super performKeyEquivalent:event];
164 - (void) insertText:(id) str
166 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
167 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
169 [super insertText:str];
175 @implementation wxNSTextView
179 static BOOL initialized = NO;
183 wxOSXCocoaClassAddWXMethods( self );
187 - (void)textDidChange:(NSNotification *)aNotification
189 wxUnusedVar(aNotification);
190 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
192 impl->controlTextDidChange();
197 @implementation wxNSTextField
201 static BOOL initialized = NO;
205 wxOSXCocoaClassAddWXMethods( self );
209 - (id) initWithFrame:(NSRect) frame
211 self = [super initWithFrame:frame];
218 [fieldEditor release];
222 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
224 fieldEditor = editor;
227 - (wxNSTextFieldEditor*) fieldEditor
232 - (void) setEnabled:(BOOL) flag
234 [super setEnabled: flag];
236 if (![self drawsBackground]) {
237 // Static text is drawn incorrectly when disabled.
238 // For an explanation, see
239 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
241 [self setTextColor: [NSColor controlTextColor]];
243 [self setTextColor: [NSColor secondarySelectedControlColor]];
248 - (void)controlTextDidChange:(NSNotification *)aNotification
250 wxUnusedVar(aNotification);
251 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
253 impl->controlTextDidChange();
256 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
258 wxUnusedVar(aNotification);
259 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
262 impl->DoNotifyFocusEvent( false, NULL );
267 // wxNSTextViewControl
269 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
270 : wxWidgetCocoaImpl(wxPeer, w),
271 wxTextWidgetImpl(wxPeer)
273 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
276 [m_scrollView setHasVerticalScroller:YES];
277 [m_scrollView setHasHorizontalScroller:NO];
278 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
279 NSSize contentSize = [m_scrollView contentSize];
281 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
282 contentSize.width, contentSize.height)];
284 [tv setVerticallyResizable:YES];
285 [tv setHorizontallyResizable:NO];
286 [tv setAutoresizingMask:NSViewWidthSizable];
288 [m_scrollView setDocumentView: tv];
290 [tv setDelegate: tv];
292 InstallEventHandler(tv);
295 wxNSTextViewControl::~wxNSTextViewControl()
298 [m_textView setDelegate: nil];
301 bool wxNSTextViewControl::CanFocus() const
303 // since this doesn't work (return false), we hardcode
305 // return [m_textView canBecomeKeyView];
309 wxString wxNSTextViewControl::GetStringValue() const
313 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
314 wxMacConvertNewlines13To10( &result ) ;
317 return wxEmptyString;
319 void wxNSTextViewControl::SetStringValue( const wxString &str)
322 wxMacConvertNewlines10To13( &st );
323 wxMacEditHelper helper(m_textView);
326 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
329 void wxNSTextViewControl::Copy()
332 [m_textView copy:nil];
336 void wxNSTextViewControl::Cut()
339 [m_textView cut:nil];
342 void wxNSTextViewControl::Paste()
345 [m_textView paste:nil];
348 bool wxNSTextViewControl::CanPaste() const
353 void wxNSTextViewControl::SetEditable(bool editable)
356 [m_textView setEditable: editable];
359 void wxNSTextViewControl::GetSelection( long* from, long* to) const
363 NSRange range = [m_textView selectedRange];
364 *from = range.location;
365 *to = range.location + range.length;
369 void wxNSTextViewControl::SetSelection( long from , long to )
371 long textLength = [[m_textView string] length];
372 if ((from == -1) && (to == -1))
379 from = wxMin(textLength,wxMax(from,0)) ;
383 to = wxMax(0,wxMin(textLength,to)) ;
386 NSRange selrange = NSMakeRange(from, to-from);
387 [m_textView setSelectedRange:selrange];
388 [m_textView scrollRangeToVisible:selrange];
391 void wxNSTextViewControl::WriteText(const wxString& str)
394 wxMacConvertNewlines10To13( &st );
395 wxMacEditHelper helper(m_textView);
396 NSEvent* formerEvent = m_lastKeyDownEvent;
397 m_lastKeyDownEvent = nil;
398 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
399 m_lastKeyDownEvent = formerEvent;
402 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
404 if ([m_textView respondsToSelector:@selector(setFont:)])
405 [m_textView setFont: font.OSXGetNSFont()];
408 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
410 if (m_textView && position >=0)
413 NSColor* bgcolor = NULL;
414 NSColor* fgcolor = NULL;
415 // NOTE: It appears that other platforms accept GetStyle with the position == length
416 // but that NSTextStorage does not accept length as a valid position.
417 // Therefore we return the default control style in that case.
418 if (position < [[m_textView string] length])
420 NSTextStorage* storage = [m_textView textStorage];
421 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
422 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
423 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
427 NSDictionary* attrs = [m_textView typingAttributes];
428 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
429 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
430 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
434 style.SetFont(wxFont(font));
437 style.SetBackgroundColour(wxColour(bgcolor));
440 style.SetTextColour(wxColour(fgcolor));
447 void wxNSTextViewControl::SetStyle(long start,
449 const wxTextAttr& style)
452 NSRange range = NSMakeRange(start, end-start);
453 if (start == -1 && end == -1)
454 range = [m_textView selectedRange];
456 NSTextStorage* storage = [m_textView textStorage];
458 wxFont font = style.GetFont();
459 if (style.HasFont() && font.IsOk())
460 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
462 wxColour bgcolor = style.GetBackgroundColour();
463 if (style.HasBackgroundColour() && bgcolor.IsOk())
464 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
466 wxColour fgcolor = style.GetTextColour();
467 if (style.HasTextColour() && fgcolor.IsOk())
468 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
472 void wxNSTextViewControl::CheckSpelling(bool check)
475 [m_textView setContinuousSpellCheckingEnabled: check];
478 wxSize wxNSTextViewControl::GetBestSize() const
480 if (m_textView && [m_textView layoutManager])
482 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
483 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
484 (int)(rect.size.height + [m_textView textContainerInset].height));
489 // wxNSTextFieldControl
491 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
492 : wxWidgetCocoaImpl(text, w),
493 wxTextWidgetImpl(text)
498 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
501 : wxWidgetCocoaImpl(wxPeer, w),
502 wxTextWidgetImpl(entry)
507 void wxNSTextFieldControl::Init(WXWidget w)
509 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
511 [m_textField setDelegate: tf];
512 m_selStart = m_selEnd = 0;
513 m_hasEditor = [w isKindOfClass:[NSTextField class]];
516 wxNSTextFieldControl::~wxNSTextFieldControl()
519 [m_textField setDelegate: nil];
522 wxString wxNSTextFieldControl::GetStringValue() const
524 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
527 void wxNSTextFieldControl::SetStringValue( const wxString &str)
529 wxMacEditHelper helper(m_textField);
530 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
533 void wxNSTextFieldControl::Copy()
535 NSText* editor = [m_textField currentEditor];
542 void wxNSTextFieldControl::Cut()
544 NSText* editor = [m_textField currentEditor];
551 void wxNSTextFieldControl::Paste()
553 NSText* editor = [m_textField currentEditor];
560 bool wxNSTextFieldControl::CanPaste() const
565 void wxNSTextFieldControl::SetEditable(bool editable)
567 [m_textField setEditable:editable];
570 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
572 NSText* editor = [m_textField currentEditor];
575 NSRange range = [editor selectedRange];
576 *from = range.location;
577 *to = range.location + range.length;
586 void wxNSTextFieldControl::SetSelection( long from , long to )
588 long textLength = [[m_textField stringValue] length];
589 if ((from == -1) && (to == -1))
596 from = wxMin(textLength,wxMax(from,0)) ;
600 to = wxMax(0,wxMin(textLength,to)) ;
603 NSText* editor = [m_textField currentEditor];
606 [editor setSelectedRange:NSMakeRange(from, to-from)];
615 void wxNSTextFieldControl::WriteText(const wxString& str)
617 NSEvent* formerEvent = m_lastKeyDownEvent;
618 m_lastKeyDownEvent = nil;
619 NSText* editor = [m_textField currentEditor];
622 wxMacEditHelper helper(m_textField);
623 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
627 wxString val = GetStringValue() ;
629 GetSelection( &start , &end ) ;
630 val.Remove( start , end - start ) ;
631 val.insert( start , str ) ;
632 SetStringValue( val ) ;
633 SetSelection( start + str.length() , start + str.length() ) ;
635 m_lastKeyDownEvent = formerEvent;
638 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
639 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
641 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
642 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
644 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
645 event.SetEventObject( wxpeer );
646 event.SetString( GetTextEntry()->GetValue() );
647 wxpeer->HandleWindowEvent( event );
655 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
656 wxWindowMac* WXUNUSED(parent),
657 wxWindowID WXUNUSED(id),
658 const wxString& WXUNUSED(str),
662 long WXUNUSED(extraStyle))
664 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
665 wxWidgetCocoaImpl* c = NULL;
667 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
669 wxNSTextScrollView* v = nil;
670 v = [[wxNSTextScrollView alloc] initWithFrame:r];
671 c = new wxNSTextViewControl( wxpeer, v );
675 NSTextField* v = nil;
676 if ( style & wxTE_PASSWORD )
677 v = [[wxNSSecureTextField alloc] initWithFrame:r];
679 v = [[wxNSTextField alloc] initWithFrame:r];
681 if ( style & wxNO_BORDER )
683 // FIXME: How can we remove the native control's border?
684 // setBordered is separate from the text ctrl's border.
687 NSTextFieldCell* cell = [v cell];
688 [cell setScrollable:YES];
689 // TODO: Remove if we definitely are sure, it's not needed
690 // as setting scrolling to yes, should turn off any wrapping
691 // [cell setLineBreakMode:NSLineBreakByClipping];
696 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
703 #endif // wxUSE_TEXTCTRL