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
233 - (void) setEnabled:(BOOL) flag
235 [super setEnabled: flag];
237 if (![self drawsBackground]) {
238 // Static text is drawn incorrectly when disabled.
239 // For an explanation, see
240 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
242 [self setTextColor: [NSColor controlTextColor]];
244 [self setTextColor: [NSColor secondarySelectedControlColor]];
249 - (void)controlTextDidChange:(NSNotification *)aNotification
251 wxUnusedVar(aNotification);
252 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
254 impl->controlTextDidChange();
257 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
259 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
261 wxUnusedVar(textView);
262 wxUnusedVar(control);
263 if (commandSelector == @selector(insertNewline:))
265 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
268 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
269 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
271 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
272 event.SetEventObject( wxpeer );
273 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
274 wxpeer->HandleWindowEvent( event );
282 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
284 wxUnusedVar(aNotification);
285 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
288 impl->DoNotifyFocusEvent( false, NULL );
293 // wxNSTextViewControl
295 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
297 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
300 [m_scrollView setHasVerticalScroller:YES];
301 [m_scrollView setHasHorizontalScroller:NO];
302 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
303 NSSize contentSize = [m_scrollView contentSize];
305 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
306 contentSize.width, contentSize.height)];
308 [tv setVerticallyResizable:YES];
309 [tv setHorizontallyResizable:NO];
310 [tv setAutoresizingMask:NSViewWidthSizable];
312 [m_scrollView setDocumentView: tv];
314 [tv setDelegate: tv];
316 InstallEventHandler(tv);
319 wxNSTextViewControl::~wxNSTextViewControl()
322 [m_textView setDelegate: nil];
325 wxString wxNSTextViewControl::GetStringValue() const
329 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
330 wxMacConvertNewlines13To10( &result ) ;
333 return wxEmptyString;
335 void wxNSTextViewControl::SetStringValue( const wxString &str)
338 wxMacConvertNewlines10To13( &st );
339 wxMacEditHelper helper(m_textView);
342 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
345 void wxNSTextViewControl::Copy()
348 [m_textView copy:nil];
352 void wxNSTextViewControl::Cut()
355 [m_textView cut:nil];
358 void wxNSTextViewControl::Paste()
361 [m_textView paste:nil];
364 bool wxNSTextViewControl::CanPaste() const
369 void wxNSTextViewControl::SetEditable(bool editable)
372 [m_textView setEditable: editable];
375 void wxNSTextViewControl::GetSelection( long* from, long* to) const
379 NSRange range = [m_textView selectedRange];
380 *from = range.location;
381 *to = range.location + range.length;
385 void wxNSTextViewControl::SetSelection( long from , long to )
387 long textLength = [[m_textView string] length];
388 if ((from == -1) && (to == -1))
395 from = wxMin(textLength,wxMax(from,0)) ;
399 to = wxMax(0,wxMin(textLength,to)) ;
402 NSRange selrange = NSMakeRange(from, to-from);
403 [m_textView setSelectedRange:selrange];
404 [m_textView scrollRangeToVisible:selrange];
407 void wxNSTextViewControl::WriteText(const wxString& str)
410 wxMacConvertNewlines10To13( &st );
411 wxMacEditHelper helper(m_textView);
412 NSEvent* formerEvent = m_lastKeyDownEvent;
413 m_lastKeyDownEvent = nil;
414 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
415 m_lastKeyDownEvent = formerEvent;
418 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
420 if ([m_textView respondsToSelector:@selector(setFont:)])
421 [m_textView setFont: font.OSXGetNSFont()];
424 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
426 if (m_textView && position >=0)
429 NSColor* bgcolor = NULL;
430 NSColor* fgcolor = NULL;
431 // NOTE: It appears that other platforms accept GetStyle with the position == length
432 // but that NSTextStorage does not accept length as a valid position.
433 // Therefore we return the default control style in that case.
434 if (position < [[m_textView string] length])
436 NSTextStorage* storage = [m_textView textStorage];
437 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
438 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
439 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
443 NSDictionary* attrs = [m_textView typingAttributes];
444 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
445 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
446 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
450 style.SetFont(wxFont(font));
453 style.SetBackgroundColour(wxColour(bgcolor));
456 style.SetTextColour(wxColour(fgcolor));
463 void wxNSTextViewControl::SetStyle(long start,
465 const wxTextAttr& style)
468 NSRange range = NSMakeRange(start, end-start);
469 if (start == -1 && end == -1)
470 range = [m_textView selectedRange];
472 NSTextStorage* storage = [m_textView textStorage];
474 wxFont font = style.GetFont();
475 if (style.HasFont() && font.IsOk())
476 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
478 wxColour bgcolor = style.GetBackgroundColour();
479 if (style.HasBackgroundColour() && bgcolor.IsOk())
480 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
482 wxColour fgcolor = style.GetTextColour();
483 if (style.HasTextColour() && fgcolor.IsOk())
484 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
488 void wxNSTextViewControl::CheckSpelling(bool check)
491 [m_textView setContinuousSpellCheckingEnabled: check];
494 wxSize wxNSTextViewControl::GetBestSize() const
496 if (m_textView && [m_textView layoutManager])
498 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
499 wxSize size = wxSize(rect.size.width, rect.size.height);
500 size.x += [m_textView textContainerInset].width;
501 size.y += [m_textView textContainerInset].height;
507 // wxNSTextFieldControl
509 wxNSTextFieldControl::wxNSTextFieldControl( wxWindow *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
511 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
513 [m_textField setDelegate: tf];
514 m_selStart = m_selEnd = 0;
515 m_hasEditor = [w isKindOfClass:[NSTextField class]];
518 wxNSTextFieldControl::~wxNSTextFieldControl()
521 [m_textField setDelegate: nil];
524 wxString wxNSTextFieldControl::GetStringValue() const
526 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
529 void wxNSTextFieldControl::SetStringValue( const wxString &str)
531 wxMacEditHelper helper(m_textField);
532 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
535 void wxNSTextFieldControl::Copy()
537 NSText* editor = [m_textField currentEditor];
544 void wxNSTextFieldControl::Cut()
546 NSText* editor = [m_textField currentEditor];
553 void wxNSTextFieldControl::Paste()
555 NSText* editor = [m_textField currentEditor];
562 bool wxNSTextFieldControl::CanPaste() const
567 void wxNSTextFieldControl::SetEditable(bool editable)
569 [m_textField setEditable:editable];
572 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
574 NSText* editor = [m_textField currentEditor];
577 NSRange range = [editor selectedRange];
578 *from = range.location;
579 *to = range.location + range.length;
588 void wxNSTextFieldControl::SetSelection( long from , long to )
590 long textLength = [[m_textField stringValue] length];
591 if ((from == -1) && (to == -1))
598 from = wxMin(textLength,wxMax(from,0)) ;
602 to = wxMax(0,wxMin(textLength,to)) ;
605 NSText* editor = [m_textField currentEditor];
608 [editor setSelectedRange:NSMakeRange(from, to-from)];
617 void wxNSTextFieldControl::WriteText(const wxString& str)
619 NSEvent* formerEvent = m_lastKeyDownEvent;
620 m_lastKeyDownEvent = nil;
621 NSText* editor = [m_textField currentEditor];
624 wxMacEditHelper helper(m_textField);
625 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
629 wxString val = GetStringValue() ;
631 GetSelection( &start , &end ) ;
632 val.Remove( start , end - start ) ;
633 val.insert( start , str ) ;
634 SetStringValue( val ) ;
635 SetSelection( start + str.length() , start + str.length() ) ;
637 m_lastKeyDownEvent = formerEvent;
640 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
641 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
643 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
644 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
646 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
647 event.SetEventObject( wxpeer );
648 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
649 wxpeer->HandleWindowEvent( event );
657 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
658 wxWindowMac* WXUNUSED(parent),
659 wxWindowID WXUNUSED(id),
660 const wxString& WXUNUSED(str),
664 long WXUNUSED(extraStyle))
666 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
667 wxWidgetCocoaImpl* c = NULL;
669 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
671 wxNSTextScrollView* v = nil;
672 v = [[wxNSTextScrollView alloc] initWithFrame:r];
673 c = new wxNSTextViewControl( wxpeer, v );
677 NSTextField* v = nil;
678 if ( style & wxTE_PASSWORD )
679 v = [[wxNSSecureTextField alloc] initWithFrame:r];
681 v = [[wxNSTextField alloc] initWithFrame:r];
683 if ( style & wxNO_BORDER )
685 // FIXME: How can we remove the native control's border?
686 // setBordered is separate from the text ctrl's border.
692 c = new wxNSTextFieldControl( wxpeer, v );
699 #endif // wxUSE_TEXTCTRL