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 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
258 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
260 wxUnusedVar(textView);
261 wxUnusedVar(control);
262 if (commandSelector == @selector(insertNewline:))
264 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
267 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
268 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
270 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
271 event.SetEventObject( wxpeer );
272 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
273 wxpeer->HandleWindowEvent( event );
281 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
283 wxUnusedVar(aNotification);
284 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
287 impl->DoNotifyFocusEvent( false, NULL );
292 // wxNSTextViewControl
294 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
296 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
299 [m_scrollView setHasVerticalScroller:YES];
300 [m_scrollView setHasHorizontalScroller:NO];
301 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
302 NSSize contentSize = [m_scrollView contentSize];
304 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
305 contentSize.width, contentSize.height)];
307 [tv setVerticallyResizable:YES];
308 [tv setHorizontallyResizable:NO];
309 [tv setAutoresizingMask:NSViewWidthSizable];
311 [m_scrollView setDocumentView: tv];
313 [tv setDelegate: tv];
315 InstallEventHandler(tv);
318 wxNSTextViewControl::~wxNSTextViewControl()
321 [m_textView setDelegate: nil];
324 bool wxNSTextViewControl::CanFocus() const
326 // since this doesn't work (return false), we hardcode
328 // return [m_textView canBecomeKeyView];
332 wxString wxNSTextViewControl::GetStringValue() const
336 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
337 wxMacConvertNewlines13To10( &result ) ;
340 return wxEmptyString;
342 void wxNSTextViewControl::SetStringValue( const wxString &str)
345 wxMacConvertNewlines10To13( &st );
346 wxMacEditHelper helper(m_textView);
349 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
352 void wxNSTextViewControl::Copy()
355 [m_textView copy:nil];
359 void wxNSTextViewControl::Cut()
362 [m_textView cut:nil];
365 void wxNSTextViewControl::Paste()
368 [m_textView paste:nil];
371 bool wxNSTextViewControl::CanPaste() const
376 void wxNSTextViewControl::SetEditable(bool editable)
379 [m_textView setEditable: editable];
382 void wxNSTextViewControl::GetSelection( long* from, long* to) const
386 NSRange range = [m_textView selectedRange];
387 *from = range.location;
388 *to = range.location + range.length;
392 void wxNSTextViewControl::SetSelection( long from , long to )
394 long textLength = [[m_textView string] length];
395 if ((from == -1) && (to == -1))
402 from = wxMin(textLength,wxMax(from,0)) ;
406 to = wxMax(0,wxMin(textLength,to)) ;
409 NSRange selrange = NSMakeRange(from, to-from);
410 [m_textView setSelectedRange:selrange];
411 [m_textView scrollRangeToVisible:selrange];
414 void wxNSTextViewControl::WriteText(const wxString& str)
417 wxMacConvertNewlines10To13( &st );
418 wxMacEditHelper helper(m_textView);
419 NSEvent* formerEvent = m_lastKeyDownEvent;
420 m_lastKeyDownEvent = nil;
421 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
422 m_lastKeyDownEvent = formerEvent;
425 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
427 if ([m_textView respondsToSelector:@selector(setFont:)])
428 [m_textView setFont: font.OSXGetNSFont()];
431 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
433 if (m_textView && position >=0)
436 NSColor* bgcolor = NULL;
437 NSColor* fgcolor = NULL;
438 // NOTE: It appears that other platforms accept GetStyle with the position == length
439 // but that NSTextStorage does not accept length as a valid position.
440 // Therefore we return the default control style in that case.
441 if (position < [[m_textView string] length])
443 NSTextStorage* storage = [m_textView textStorage];
444 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
445 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
446 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
450 NSDictionary* attrs = [m_textView typingAttributes];
451 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
452 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
453 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
457 style.SetFont(wxFont(font));
460 style.SetBackgroundColour(wxColour(bgcolor));
463 style.SetTextColour(wxColour(fgcolor));
470 void wxNSTextViewControl::SetStyle(long start,
472 const wxTextAttr& style)
475 NSRange range = NSMakeRange(start, end-start);
476 if (start == -1 && end == -1)
477 range = [m_textView selectedRange];
479 NSTextStorage* storage = [m_textView textStorage];
481 wxFont font = style.GetFont();
482 if (style.HasFont() && font.IsOk())
483 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
485 wxColour bgcolor = style.GetBackgroundColour();
486 if (style.HasBackgroundColour() && bgcolor.IsOk())
487 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
489 wxColour fgcolor = style.GetTextColour();
490 if (style.HasTextColour() && fgcolor.IsOk())
491 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
495 void wxNSTextViewControl::CheckSpelling(bool check)
498 [m_textView setContinuousSpellCheckingEnabled: check];
501 wxSize wxNSTextViewControl::GetBestSize() const
503 if (m_textView && [m_textView layoutManager])
505 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
506 wxSize size = wxSize(rect.size.width, rect.size.height);
507 size.x += [m_textView textContainerInset].width;
508 size.y += [m_textView textContainerInset].height;
514 // wxNSTextFieldControl
516 wxNSTextFieldControl::wxNSTextFieldControl( wxWindow *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
518 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
520 [m_textField setDelegate: tf];
521 m_selStart = m_selEnd = 0;
522 m_hasEditor = [w isKindOfClass:[NSTextField class]];
525 wxNSTextFieldControl::~wxNSTextFieldControl()
528 [m_textField setDelegate: nil];
531 wxString wxNSTextFieldControl::GetStringValue() const
533 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
536 void wxNSTextFieldControl::SetStringValue( const wxString &str)
538 wxMacEditHelper helper(m_textField);
539 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
542 void wxNSTextFieldControl::Copy()
544 NSText* editor = [m_textField currentEditor];
551 void wxNSTextFieldControl::Cut()
553 NSText* editor = [m_textField currentEditor];
560 void wxNSTextFieldControl::Paste()
562 NSText* editor = [m_textField currentEditor];
569 bool wxNSTextFieldControl::CanPaste() const
574 void wxNSTextFieldControl::SetEditable(bool editable)
576 [m_textField setEditable:editable];
579 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
581 NSText* editor = [m_textField currentEditor];
584 NSRange range = [editor selectedRange];
585 *from = range.location;
586 *to = range.location + range.length;
595 void wxNSTextFieldControl::SetSelection( long from , long to )
597 long textLength = [[m_textField stringValue] length];
598 if ((from == -1) && (to == -1))
605 from = wxMin(textLength,wxMax(from,0)) ;
609 to = wxMax(0,wxMin(textLength,to)) ;
612 NSText* editor = [m_textField currentEditor];
615 [editor setSelectedRange:NSMakeRange(from, to-from)];
624 void wxNSTextFieldControl::WriteText(const wxString& str)
626 NSEvent* formerEvent = m_lastKeyDownEvent;
627 m_lastKeyDownEvent = nil;
628 NSText* editor = [m_textField currentEditor];
631 wxMacEditHelper helper(m_textField);
632 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
636 wxString val = GetStringValue() ;
638 GetSelection( &start , &end ) ;
639 val.Remove( start , end - start ) ;
640 val.insert( start , str ) ;
641 SetStringValue( val ) ;
642 SetSelection( start + str.length() , start + str.length() ) ;
644 m_lastKeyDownEvent = formerEvent;
647 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
648 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
650 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
651 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
653 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
654 event.SetEventObject( wxpeer );
655 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
656 wxpeer->HandleWindowEvent( event );
664 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
665 wxWindowMac* WXUNUSED(parent),
666 wxWindowID WXUNUSED(id),
667 const wxString& WXUNUSED(str),
671 long WXUNUSED(extraStyle))
673 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
674 wxWidgetCocoaImpl* c = NULL;
676 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
678 wxNSTextScrollView* v = nil;
679 v = [[wxNSTextScrollView alloc] initWithFrame:r];
680 c = new wxNSTextViewControl( wxpeer, v );
684 NSTextField* v = nil;
685 if ( style & wxTE_PASSWORD )
686 v = [[wxNSSecureTextField alloc] initWithFrame:r];
688 v = [[wxNSTextField alloc] initWithFrame:r];
690 if ( style & wxNO_BORDER )
692 // FIXME: How can we remove the native control's border?
693 // setBordered is separate from the text ctrl's border.
699 c = new wxNSTextFieldControl( wxpeer, v );
706 #endif // wxUSE_TEXTCTRL