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 ;
65 m_formerState = [textView isEditable];
66 [textView setEditable:YES];
73 [m_textView setEditable:m_formerState];
81 @implementation wxNSSecureTextField
85 static BOOL initialized = NO;
89 wxOSXCocoaClassAddWXMethods( self );
93 - (void)controlTextDidChange:(NSNotification *)aNotification
95 wxUnusedVar(aNotification);
96 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
99 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
101 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
102 event.SetEventObject( wxpeer );
103 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
104 wxpeer->HandleWindowEvent( event );
109 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
111 wxUnusedVar(aNotification);
112 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
115 impl->DoNotifyFocusEvent( false, NULL );
121 @interface wxNSTextScrollView : NSScrollView
126 @implementation wxNSTextScrollView
130 static BOOL initialized = NO;
134 wxOSXCocoaClassAddWXMethods( self );
140 @implementation wxNSTextFieldEditor
142 - (void) keyDown:(NSEvent*) event
144 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [self delegate] );
145 lastKeyDownEvent = event;
146 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
147 [super keyDown:event];
148 lastKeyDownEvent = nil;
151 - (void) keyUp:(NSEvent*) event
153 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [self delegate] );
154 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
158 - (void) flagsChanged:(NSEvent*) event
160 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [self delegate] );
161 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
162 [super flagsChanged:event];
165 - (BOOL) performKeyEquivalent:(NSEvent*) event
167 BOOL retval = [super performKeyEquivalent:event];
171 - (void) insertText:(id) str
173 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [self delegate] );
174 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
176 [super insertText:str];
182 @implementation wxNSTextView
186 static BOOL initialized = NO;
190 wxOSXCocoaClassAddWXMethods( self );
196 @implementation wxNSTextField
200 static BOOL initialized = NO;
204 wxOSXCocoaClassAddWXMethods( self );
208 - (id) initWithFrame:(NSRect) frame
210 self = [super initWithFrame:frame];
215 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
217 fieldEditor = editor;
220 - (wxNSTextFieldEditor*) fieldEditor
226 - (void) setEnabled:(BOOL) flag
228 [super setEnabled: flag];
230 if (![self drawsBackground]) {
231 // Static text is drawn incorrectly when disabled.
232 // For an explanation, see
233 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
235 [self setTextColor: [NSColor controlTextColor]];
237 [self setTextColor: [NSColor secondarySelectedControlColor]];
242 - (void)controlTextDidChange:(NSNotification *)aNotification
244 wxUnusedVar(aNotification);
245 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
248 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
250 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
251 event.SetEventObject( wxpeer );
252 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
253 wxpeer->HandleWindowEvent( event );
258 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
260 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
262 wxUnusedVar(textView);
263 wxUnusedVar(control);
264 if (commandSelector == @selector(insertNewline:))
266 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
269 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
270 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
272 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
273 event.SetEventObject( wxpeer );
274 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
275 wxpeer->HandleWindowEvent( event );
283 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
285 wxUnusedVar(aNotification);
286 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
289 impl->DoNotifyFocusEvent( false, NULL );
294 // wxNSTextViewControl
296 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
298 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
301 [m_scrollView setHasVerticalScroller:YES];
302 [m_scrollView setHasHorizontalScroller:NO];
303 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
304 NSSize contentSize = [m_scrollView contentSize];
306 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
307 contentSize.width, contentSize.height)];
309 [tv setVerticallyResizable:YES];
310 [tv setHorizontallyResizable:NO];
311 [tv setAutoresizingMask:NSViewWidthSizable];
313 [m_scrollView setDocumentView: tv];
317 InstallEventHandler(tv);
320 wxNSTextViewControl::~wxNSTextViewControl()
323 [m_textView setDelegate: nil];
326 wxString wxNSTextViewControl::GetStringValue() const
330 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
331 wxString result = cf.AsString(m_wxPeer->GetFont().GetEncoding());
332 wxMacConvertNewlines13To10( &result ) ;
335 return wxEmptyString;
337 void wxNSTextViewControl::SetStringValue( const wxString &str)
340 wxMacConvertNewlines10To13( &st );
341 wxMacEditHelper helper(m_textView);
344 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
347 void wxNSTextViewControl::Copy()
350 [m_textView copy:nil];
354 void wxNSTextViewControl::Cut()
357 [m_textView cut:nil];
360 void wxNSTextViewControl::Paste()
363 [m_textView paste:nil];
366 bool wxNSTextViewControl::CanPaste() const
371 void wxNSTextViewControl::SetEditable(bool editable)
374 [m_textView setEditable: editable];
377 void wxNSTextViewControl::GetSelection( long* from, long* to) const
381 NSRange range = [m_textView selectedRange];
382 *from = range.location;
383 *to = range.location + range.length;
387 void wxNSTextViewControl::SetSelection( long from , long to )
389 long textLength = [[m_textView string] length];
390 if ((from == -1) && (to == -1))
397 from = wxMin(textLength,wxMax(from,0)) ;
401 to = wxMax(0,wxMin(textLength,to)) ;
404 NSRange selrange = NSMakeRange(from, to-from);
405 [m_textView setSelectedRange:selrange];
406 [m_textView scrollRangeToVisible:selrange];
409 void wxNSTextViewControl::WriteText(const wxString& str)
412 wxMacConvertNewlines10To13( &st );
413 wxMacEditHelper helper(m_textView);
415 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
418 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
420 if ([m_textView respondsToSelector:@selector(setFont:)])
421 [m_textView setFont: font.OSXGetNSFont()];
425 // wxNSTextFieldControl
427 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
429 m_textField = (NSTextField*) w;
430 [m_textField setDelegate: w];
431 m_selStart = m_selEnd = 0;
432 m_hasEditor = [w isKindOfClass:[NSTextField class]];
435 wxNSTextFieldControl::~wxNSTextFieldControl()
438 [m_textField setDelegate: nil];
441 wxString wxNSTextFieldControl::GetStringValue() const
443 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
444 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
447 void wxNSTextFieldControl::SetStringValue( const wxString &str)
449 wxMacEditHelper helper(m_textField);
450 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
453 void wxNSTextFieldControl::Copy()
455 NSText* editor = [m_textField currentEditor];
462 void wxNSTextFieldControl::Cut()
464 NSText* editor = [m_textField currentEditor];
471 void wxNSTextFieldControl::Paste()
473 NSText* editor = [m_textField currentEditor];
480 bool wxNSTextFieldControl::CanPaste() const
485 void wxNSTextFieldControl::SetEditable(bool editable)
487 [m_textField setEditable:editable];
490 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
492 NSText* editor = [m_textField currentEditor];
495 NSRange range = [editor selectedRange];
496 *from = range.location;
497 *to = range.location + range.length;
506 void wxNSTextFieldControl::SetSelection( long from , long to )
508 long textLength = [[m_textField stringValue] length];
509 if ((from == -1) && (to == -1))
516 from = wxMin(textLength,wxMax(from,0)) ;
520 to = wxMax(0,wxMin(textLength,to)) ;
523 NSText* editor = [m_textField currentEditor];
526 [editor setSelectedRange:NSMakeRange(from, to-from)];
535 void wxNSTextFieldControl::WriteText(const wxString& str)
537 NSText* editor = [m_textField currentEditor];
540 wxMacEditHelper helper(m_textField);
541 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
545 wxString val = GetStringValue() ;
547 GetSelection( &start , &end ) ;
548 val.Remove( start , end - start ) ;
549 val.insert( start , str ) ;
550 SetStringValue( val ) ;
551 SetSelection( start + str.length() , start + str.length() ) ;
555 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
556 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
558 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
559 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
561 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
562 event.SetEventObject( wxpeer );
563 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
564 wxpeer->HandleWindowEvent( event );
572 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
573 wxWindowMac* WXUNUSED(parent),
574 wxWindowID WXUNUSED(id),
579 long WXUNUSED(extraStyle))
581 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
582 wxWidgetCocoaImpl* c = NULL;
584 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
586 wxNSTextScrollView* v = nil;
587 v = [[wxNSTextScrollView alloc] initWithFrame:r];
588 c = new wxNSTextViewControl( wxpeer, v );
589 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
593 NSTextField* v = nil;
594 if ( style & wxTE_PASSWORD )
595 v = [[wxNSSecureTextField alloc] initWithFrame:r];
597 v = [[wxNSTextField alloc] initWithFrame:r];
599 if ( style & wxNO_BORDER )
601 // FIXME: How can we remove the native control's border?
602 // setBordered is separate from the text ctrl's border.
608 c = new wxNSTextFieldControl( wxpeer, v );
609 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
616 #endif // wxUSE_TEXTCTRL