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 );
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 );
255 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
257 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
258 event.SetEventObject( wxpeer );
259 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
260 wxpeer->HandleWindowEvent( event );
265 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
267 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
269 wxUnusedVar(textView);
270 wxUnusedVar(control);
271 if (commandSelector == @selector(insertNewline:))
273 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
276 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
277 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
279 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
280 event.SetEventObject( wxpeer );
281 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
282 wxpeer->HandleWindowEvent( event );
290 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
292 wxUnusedVar(aNotification);
293 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
296 impl->DoNotifyFocusEvent( false, NULL );
301 // wxNSTextViewControl
303 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
305 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
308 [m_scrollView setHasVerticalScroller:YES];
309 [m_scrollView setHasHorizontalScroller:NO];
310 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
311 NSSize contentSize = [m_scrollView contentSize];
313 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
314 contentSize.width, contentSize.height)];
316 [tv setVerticallyResizable:YES];
317 [tv setHorizontallyResizable:NO];
318 [tv setAutoresizingMask:NSViewWidthSizable];
320 [m_scrollView setDocumentView: tv];
324 InstallEventHandler(tv);
327 wxNSTextViewControl::~wxNSTextViewControl()
330 [m_textView setDelegate: nil];
333 wxString wxNSTextViewControl::GetStringValue() const
337 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
338 wxMacConvertNewlines13To10( &result ) ;
341 return wxEmptyString;
343 void wxNSTextViewControl::SetStringValue( const wxString &str)
346 wxMacConvertNewlines10To13( &st );
347 wxMacEditHelper helper(m_textView);
350 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
353 void wxNSTextViewControl::Copy()
356 [m_textView copy:nil];
360 void wxNSTextViewControl::Cut()
363 [m_textView cut:nil];
366 void wxNSTextViewControl::Paste()
369 [m_textView paste:nil];
372 bool wxNSTextViewControl::CanPaste() const
377 void wxNSTextViewControl::SetEditable(bool editable)
380 [m_textView setEditable: editable];
383 void wxNSTextViewControl::GetSelection( long* from, long* to) const
387 NSRange range = [m_textView selectedRange];
388 *from = range.location;
389 *to = range.location + range.length;
393 void wxNSTextViewControl::SetSelection( long from , long to )
395 long textLength = [[m_textView string] length];
396 if ((from == -1) && (to == -1))
403 from = wxMin(textLength,wxMax(from,0)) ;
407 to = wxMax(0,wxMin(textLength,to)) ;
410 NSRange selrange = NSMakeRange(from, to-from);
411 [m_textView setSelectedRange:selrange];
412 [m_textView scrollRangeToVisible:selrange];
415 void wxNSTextViewControl::WriteText(const wxString& str)
418 wxMacConvertNewlines10To13( &st );
419 wxMacEditHelper helper(m_textView);
420 NSEvent* formerEvent = m_lastKeyDownEvent;
421 m_lastKeyDownEvent = nil;
422 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
423 m_lastKeyDownEvent = formerEvent;
426 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
428 if ([m_textView respondsToSelector:@selector(setFont:)])
429 [m_textView setFont: font.OSXGetNSFont()];
432 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
434 if (m_textView && position >=0 && position < [[m_textView string] length]) {
435 NSTextStorage* storage = [m_textView textStorage];
436 NSFont* font = [storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL];
438 style.SetFont(wxFont(font));
439 NSColor* bgcolor = [storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL];
441 style.SetBackgroundColour(wxColour(bgcolor));
442 NSColor* fgcolor = [storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL];
443 style.SetTextColour(wxColour(fgcolor));
450 void wxNSTextViewControl::SetStyle(long start,
452 const wxTextAttr& style)
455 NSRange range = NSMakeRange(start, end-start);
456 NSTextStorage* storage = [m_textView textStorage];
458 wxFont font = style.GetFont();
460 [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range];
462 wxColour bgcolor = style.GetBackgroundColour();
464 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
466 wxColour fgcolor = style.GetTextColour();
468 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
472 // wxNSTextFieldControl
474 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
476 m_textField = (NSTextField*) w;
477 [m_textField setDelegate: w];
478 m_selStart = m_selEnd = 0;
479 m_hasEditor = [w isKindOfClass:[NSTextField class]];
482 wxNSTextFieldControl::~wxNSTextFieldControl()
485 [m_textField setDelegate: nil];
488 wxString wxNSTextFieldControl::GetStringValue() const
490 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
493 void wxNSTextFieldControl::SetStringValue( const wxString &str)
495 wxMacEditHelper helper(m_textField);
496 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
499 void wxNSTextFieldControl::Copy()
501 NSText* editor = [m_textField currentEditor];
508 void wxNSTextFieldControl::Cut()
510 NSText* editor = [m_textField currentEditor];
517 void wxNSTextFieldControl::Paste()
519 NSText* editor = [m_textField currentEditor];
526 bool wxNSTextFieldControl::CanPaste() const
531 void wxNSTextFieldControl::SetEditable(bool editable)
533 [m_textField setEditable:editable];
536 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
538 NSText* editor = [m_textField currentEditor];
541 NSRange range = [editor selectedRange];
542 *from = range.location;
543 *to = range.location + range.length;
552 void wxNSTextFieldControl::SetSelection( long from , long to )
554 long textLength = [[m_textField stringValue] length];
555 if ((from == -1) && (to == -1))
562 from = wxMin(textLength,wxMax(from,0)) ;
566 to = wxMax(0,wxMin(textLength,to)) ;
569 NSText* editor = [m_textField currentEditor];
572 [editor setSelectedRange:NSMakeRange(from, to-from)];
581 void wxNSTextFieldControl::WriteText(const wxString& str)
583 NSEvent* formerEvent = m_lastKeyDownEvent;
584 m_lastKeyDownEvent = nil;
585 NSText* editor = [m_textField currentEditor];
588 wxMacEditHelper helper(m_textField);
589 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
593 wxString val = GetStringValue() ;
595 GetSelection( &start , &end ) ;
596 val.Remove( start , end - start ) ;
597 val.insert( start , str ) ;
598 SetStringValue( val ) ;
599 SetSelection( start + str.length() , start + str.length() ) ;
601 m_lastKeyDownEvent = formerEvent;
604 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
605 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
607 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
608 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
610 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
611 event.SetEventObject( wxpeer );
612 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
613 wxpeer->HandleWindowEvent( event );
621 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
622 wxWindowMac* WXUNUSED(parent),
623 wxWindowID WXUNUSED(id),
624 const wxString& WXUNUSED(str),
628 long WXUNUSED(extraStyle))
630 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
631 wxWidgetCocoaImpl* c = NULL;
633 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
635 wxNSTextScrollView* v = nil;
636 v = [[wxNSTextScrollView alloc] initWithFrame:r];
637 c = new wxNSTextViewControl( wxpeer, v );
641 NSTextField* v = nil;
642 if ( style & wxTE_PASSWORD )
643 v = [[wxNSSecureTextField alloc] initWithFrame:r];
645 v = [[wxNSTextField alloc] initWithFrame:r];
647 if ( style & wxNO_BORDER )
649 // FIXME: How can we remove the native control's border?
650 // setBordered is separate from the text ctrl's border.
656 c = new wxNSTextFieldControl( wxpeer, v );
663 #endif // wxUSE_TEXTCTRL