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 );
100 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
102 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
103 event.SetEventObject( wxpeer );
104 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
105 wxpeer->HandleWindowEvent( event );
110 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
112 wxUnusedVar(aNotification);
113 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
116 impl->DoNotifyFocusEvent( false, NULL );
122 @interface wxNSTextScrollView : NSScrollView
127 @implementation wxNSTextScrollView
131 static BOOL initialized = NO;
135 wxOSXCocoaClassAddWXMethods( self );
141 @implementation wxNSTextFieldEditor
143 - (void) keyDown:(NSEvent*) event
145 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
146 lastKeyDownEvent = event;
147 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
148 [super keyDown:event];
149 lastKeyDownEvent = nil;
152 - (void) keyUp:(NSEvent*) event
154 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
155 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
159 - (void) flagsChanged:(NSEvent*) event
161 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
162 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
163 [super flagsChanged:event];
166 - (BOOL) performKeyEquivalent:(NSEvent*) event
168 BOOL retval = [super performKeyEquivalent:event];
172 - (void) insertText:(id) str
174 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
175 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
177 [super insertText:str];
183 @implementation wxNSTextView
187 static BOOL initialized = NO;
191 wxOSXCocoaClassAddWXMethods( self );
195 - (void)textDidChange:(NSNotification *)aNotification
197 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [aNotification object] );
200 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
202 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
203 event.SetEventObject( wxpeer );
204 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
205 wxpeer->HandleWindowEvent( event );
212 @implementation wxNSTextField
216 static BOOL initialized = NO;
220 wxOSXCocoaClassAddWXMethods( self );
224 - (id) initWithFrame:(NSRect) frame
226 self = [super initWithFrame:frame];
233 [fieldEditor release];
237 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
239 fieldEditor = editor;
242 - (wxNSTextFieldEditor*) fieldEditor
248 - (void) setEnabled:(BOOL) flag
250 [super setEnabled: flag];
252 if (![self drawsBackground]) {
253 // Static text is drawn incorrectly when disabled.
254 // For an explanation, see
255 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
257 [self setTextColor: [NSColor controlTextColor]];
259 [self setTextColor: [NSColor secondarySelectedControlColor]];
264 - (void)controlTextDidChange:(NSNotification *)aNotification
266 wxUnusedVar(aNotification);
267 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
270 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
272 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
273 event.SetEventObject( wxpeer );
274 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
275 wxpeer->HandleWindowEvent( event );
280 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
282 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
284 wxUnusedVar(textView);
285 wxUnusedVar(control);
286 if (commandSelector == @selector(insertNewline:))
288 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
291 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
292 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
294 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
295 event.SetEventObject( wxpeer );
296 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
297 wxpeer->HandleWindowEvent( event );
305 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
307 wxUnusedVar(aNotification);
308 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
311 impl->DoNotifyFocusEvent( false, NULL );
316 // wxNSTextViewControl
318 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
320 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
323 [m_scrollView setHasVerticalScroller:YES];
324 [m_scrollView setHasHorizontalScroller:NO];
325 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
326 NSSize contentSize = [m_scrollView contentSize];
328 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
329 contentSize.width, contentSize.height)];
331 [tv setVerticallyResizable:YES];
332 [tv setHorizontallyResizable:NO];
333 [tv setAutoresizingMask:NSViewWidthSizable];
335 [m_scrollView setDocumentView: tv];
337 [tv setDelegate: tv];
339 InstallEventHandler(tv);
342 wxNSTextViewControl::~wxNSTextViewControl()
345 [m_textView setDelegate: nil];
348 wxString wxNSTextViewControl::GetStringValue() const
352 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
353 wxMacConvertNewlines13To10( &result ) ;
356 return wxEmptyString;
358 void wxNSTextViewControl::SetStringValue( const wxString &str)
361 wxMacConvertNewlines10To13( &st );
362 wxMacEditHelper helper(m_textView);
365 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
368 void wxNSTextViewControl::Copy()
371 [m_textView copy:nil];
375 void wxNSTextViewControl::Cut()
378 [m_textView cut:nil];
381 void wxNSTextViewControl::Paste()
384 [m_textView paste:nil];
387 bool wxNSTextViewControl::CanPaste() const
392 void wxNSTextViewControl::SetEditable(bool editable)
395 [m_textView setEditable: editable];
398 void wxNSTextViewControl::GetSelection( long* from, long* to) const
402 NSRange range = [m_textView selectedRange];
403 *from = range.location;
404 *to = range.location + range.length;
408 void wxNSTextViewControl::SetSelection( long from , long to )
410 long textLength = [[m_textView string] length];
411 if ((from == -1) && (to == -1))
418 from = wxMin(textLength,wxMax(from,0)) ;
422 to = wxMax(0,wxMin(textLength,to)) ;
425 NSRange selrange = NSMakeRange(from, to-from);
426 [m_textView setSelectedRange:selrange];
427 [m_textView scrollRangeToVisible:selrange];
430 void wxNSTextViewControl::WriteText(const wxString& str)
433 wxMacConvertNewlines10To13( &st );
434 wxMacEditHelper helper(m_textView);
435 NSEvent* formerEvent = m_lastKeyDownEvent;
436 m_lastKeyDownEvent = nil;
437 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
438 m_lastKeyDownEvent = formerEvent;
441 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
443 if ([m_textView respondsToSelector:@selector(setFont:)])
444 [m_textView setFont: font.OSXGetNSFont()];
447 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
449 if (m_textView && position >=0)
452 NSColor* bgcolor = NULL;
453 NSColor* fgcolor = NULL;
454 // NOTE: It appears that other platforms accept GetStyle with the position == length
455 // but that NSTextStorage does not accept length as a valid position.
456 // Therefore we return the default control style in that case.
457 if (position < [[m_textView string] length])
459 NSTextStorage* storage = [m_textView textStorage];
460 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
461 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
462 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
466 NSDictionary* attrs = [m_textView typingAttributes];
467 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
468 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
469 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
473 style.SetFont(wxFont(font));
476 style.SetBackgroundColour(wxColour(bgcolor));
479 style.SetTextColour(wxColour(fgcolor));
486 void wxNSTextViewControl::SetStyle(long start,
488 const wxTextAttr& style)
491 NSRange range = NSMakeRange(start, end-start);
492 if (start == -1 && end == -1)
493 range = [m_textView selectedRange];
495 NSTextStorage* storage = [m_textView textStorage];
497 wxFont font = style.GetFont();
498 if (style.HasFont() && font.IsOk())
499 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
501 wxColour bgcolor = style.GetBackgroundColour();
502 if (style.HasBackgroundColour() && bgcolor.IsOk())
503 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
505 wxColour fgcolor = style.GetTextColour();
506 if (style.HasTextColour() && fgcolor.IsOk())
507 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
511 void wxNSTextViewControl::CheckSpelling(bool check)
514 [m_textView setContinuousSpellCheckingEnabled: check];
517 wxSize wxNSTextViewControl::GetBestSize() const
519 if (m_textView && [m_textView layoutManager])
521 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
522 wxSize size = wxSize(rect.size.width, rect.size.height);
523 size.x += [m_textView textContainerInset].width;
524 size.y += [m_textView textContainerInset].height;
530 // wxNSTextFieldControl
532 wxNSTextFieldControl::wxNSTextFieldControl( wxWindow *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
534 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
536 [m_textField setDelegate: tf];
537 m_selStart = m_selEnd = 0;
538 m_hasEditor = [w isKindOfClass:[NSTextField class]];
541 wxNSTextFieldControl::~wxNSTextFieldControl()
544 [m_textField setDelegate: nil];
547 wxString wxNSTextFieldControl::GetStringValue() const
549 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
552 void wxNSTextFieldControl::SetStringValue( const wxString &str)
554 wxMacEditHelper helper(m_textField);
555 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
558 void wxNSTextFieldControl::Copy()
560 NSText* editor = [m_textField currentEditor];
567 void wxNSTextFieldControl::Cut()
569 NSText* editor = [m_textField currentEditor];
576 void wxNSTextFieldControl::Paste()
578 NSText* editor = [m_textField currentEditor];
585 bool wxNSTextFieldControl::CanPaste() const
590 void wxNSTextFieldControl::SetEditable(bool editable)
592 [m_textField setEditable:editable];
595 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
597 NSText* editor = [m_textField currentEditor];
600 NSRange range = [editor selectedRange];
601 *from = range.location;
602 *to = range.location + range.length;
611 void wxNSTextFieldControl::SetSelection( long from , long to )
613 long textLength = [[m_textField stringValue] length];
614 if ((from == -1) && (to == -1))
621 from = wxMin(textLength,wxMax(from,0)) ;
625 to = wxMax(0,wxMin(textLength,to)) ;
628 NSText* editor = [m_textField currentEditor];
631 [editor setSelectedRange:NSMakeRange(from, to-from)];
640 void wxNSTextFieldControl::WriteText(const wxString& str)
642 NSEvent* formerEvent = m_lastKeyDownEvent;
643 m_lastKeyDownEvent = nil;
644 NSText* editor = [m_textField currentEditor];
647 wxMacEditHelper helper(m_textField);
648 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
652 wxString val = GetStringValue() ;
654 GetSelection( &start , &end ) ;
655 val.Remove( start , end - start ) ;
656 val.insert( start , str ) ;
657 SetStringValue( val ) ;
658 SetSelection( start + str.length() , start + str.length() ) ;
660 m_lastKeyDownEvent = formerEvent;
663 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
664 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
666 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
667 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
669 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
670 event.SetEventObject( wxpeer );
671 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
672 wxpeer->HandleWindowEvent( event );
680 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
681 wxWindowMac* WXUNUSED(parent),
682 wxWindowID WXUNUSED(id),
683 const wxString& WXUNUSED(str),
687 long WXUNUSED(extraStyle))
689 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
690 wxWidgetCocoaImpl* c = NULL;
692 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
694 wxNSTextScrollView* v = nil;
695 v = [[wxNSTextScrollView alloc] initWithFrame:r];
696 c = new wxNSTextViewControl( wxpeer, v );
700 NSTextField* v = nil;
701 if ( style & wxTE_PASSWORD )
702 v = [[wxNSSecureTextField alloc] initWithFrame:r];
704 v = [[wxNSTextField alloc] initWithFrame:r];
706 if ( style & wxNO_BORDER )
708 // FIXME: How can we remove the native control's border?
709 // setBordered is separate from the text ctrl's border.
715 c = new wxNSTextFieldControl( wxpeer, v );
722 #endif // wxUSE_TEXTCTRL