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;
529 // wxNSTextFieldControl
531 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
533 m_textField = (NSTextField*) w;
534 [m_textField setDelegate: w];
535 m_selStart = m_selEnd = 0;
536 m_hasEditor = [w isKindOfClass:[NSTextField class]];
539 wxNSTextFieldControl::~wxNSTextFieldControl()
542 [m_textField setDelegate: nil];
545 wxString wxNSTextFieldControl::GetStringValue() const
547 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
550 void wxNSTextFieldControl::SetStringValue( const wxString &str)
552 wxMacEditHelper helper(m_textField);
553 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
556 void wxNSTextFieldControl::Copy()
558 NSText* editor = [m_textField currentEditor];
565 void wxNSTextFieldControl::Cut()
567 NSText* editor = [m_textField currentEditor];
574 void wxNSTextFieldControl::Paste()
576 NSText* editor = [m_textField currentEditor];
583 bool wxNSTextFieldControl::CanPaste() const
588 void wxNSTextFieldControl::SetEditable(bool editable)
590 [m_textField setEditable:editable];
593 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
595 NSText* editor = [m_textField currentEditor];
598 NSRange range = [editor selectedRange];
599 *from = range.location;
600 *to = range.location + range.length;
609 void wxNSTextFieldControl::SetSelection( long from , long to )
611 long textLength = [[m_textField stringValue] length];
612 if ((from == -1) && (to == -1))
619 from = wxMin(textLength,wxMax(from,0)) ;
623 to = wxMax(0,wxMin(textLength,to)) ;
626 NSText* editor = [m_textField currentEditor];
629 [editor setSelectedRange:NSMakeRange(from, to-from)];
638 void wxNSTextFieldControl::WriteText(const wxString& str)
640 NSEvent* formerEvent = m_lastKeyDownEvent;
641 m_lastKeyDownEvent = nil;
642 NSText* editor = [m_textField currentEditor];
645 wxMacEditHelper helper(m_textField);
646 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
650 wxString val = GetStringValue() ;
652 GetSelection( &start , &end ) ;
653 val.Remove( start , end - start ) ;
654 val.insert( start , str ) ;
655 SetStringValue( val ) ;
656 SetSelection( start + str.length() , start + str.length() ) ;
658 m_lastKeyDownEvent = formerEvent;
661 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
662 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
664 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
665 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
667 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
668 event.SetEventObject( wxpeer );
669 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
670 wxpeer->HandleWindowEvent( event );
678 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
679 wxWindowMac* WXUNUSED(parent),
680 wxWindowID WXUNUSED(id),
681 const wxString& WXUNUSED(str),
685 long WXUNUSED(extraStyle))
687 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
688 wxWidgetCocoaImpl* c = NULL;
690 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
692 wxNSTextScrollView* v = nil;
693 v = [[wxNSTextScrollView alloc] initWithFrame:r];
694 c = new wxNSTextViewControl( wxpeer, v );
698 NSTextField* v = nil;
699 if ( style & wxTE_PASSWORD )
700 v = [[wxNSSecureTextField alloc] initWithFrame:r];
702 v = [[wxNSTextField alloc] initWithFrame:r];
704 if ( style & wxNO_BORDER )
706 // FIXME: How can we remove the native control's border?
707 // setBordered is separate from the text ctrl's border.
713 c = new wxNSTextFieldControl( wxpeer, v );
720 #endif // wxUSE_TEXTCTRL