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 @interface wxNSSecureTextField : NSSecureTextField
86 @implementation wxNSSecureTextField
90 static BOOL initialized = NO;
94 wxOSXCocoaClassAddWXMethods( self );
98 - (void)controlTextDidChange:(NSNotification *)aNotification
100 wxUnusedVar(aNotification);
101 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
104 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
106 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
107 event.SetEventObject( wxpeer );
108 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
109 wxpeer->HandleWindowEvent( event );
114 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
116 wxUnusedVar(aNotification);
117 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
120 impl->DoNotifyFocusEvent( false, NULL );
126 @interface wxNSTextScrollView : NSScrollView
131 @interface wxNSTextView : NSTextView
133 wxNSTextScrollView* scrollView;
136 - (void)setScrollView: (wxNSTextScrollView *) sv;
137 - (wxNSTextScrollView*) scrollView;
141 @implementation wxNSTextScrollView
145 static BOOL initialized = NO;
149 wxOSXCocoaClassAddWXMethods( self );
153 - (void)textDidChange:(NSNotification *)aNotification
155 wxUnusedVar(aNotification);
156 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
159 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
161 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
162 event.SetEventObject( wxpeer );
163 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
164 wxpeer->HandleWindowEvent( event );
169 - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)commandSelector
171 wxUnusedVar(aTextView);
172 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
175 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
176 if (commandSelector == @selector(insertNewline:))
178 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
180 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
181 event.SetEventObject( wxpeer );
182 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
183 wxpeer->HandleWindowEvent( event );
191 - (void)textDidEndEditing:(NSNotification *)aNotification
193 wxUnusedVar(aNotification);
194 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
197 impl->DoNotifyFocusEvent( false, NULL );
202 @implementation wxNSTextView
204 - (BOOL) becomeFirstResponder
206 BOOL val = [super becomeFirstResponder];
210 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( scrollView );
212 impl->DoNotifyFocusEvent( true, NULL );
218 - (void)setScrollView: (wxNSTextScrollView *) sv
223 - (wxNSTextScrollView*) scrollView
230 @implementation wxNSTextField
234 static BOOL initialized = NO;
238 wxOSXCocoaClassAddWXMethods( self );
242 - (void) setEnabled:(BOOL) flag
244 [super setEnabled: flag];
246 if (![self drawsBackground]) {
247 // Static text is drawn incorrectly when disabled.
248 // For an explanation, see
249 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
251 [self setTextColor: [NSColor controlTextColor]];
253 [self setTextColor: [NSColor secondarySelectedControlColor]];
258 - (void)controlTextDidChange:(NSNotification *)aNotification
260 wxUnusedVar(aNotification);
261 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
264 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
266 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
267 event.SetEventObject( wxpeer );
268 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
269 wxpeer->HandleWindowEvent( event );
274 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
276 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
278 wxUnusedVar(textView);
279 wxUnusedVar(control);
280 if (commandSelector == @selector(insertNewline:))
282 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
285 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
286 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
288 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
289 event.SetEventObject( wxpeer );
290 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
291 wxpeer->HandleWindowEvent( event );
299 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
301 wxUnusedVar(aNotification);
302 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
305 impl->DoNotifyFocusEvent( false, NULL );
310 // wxNSTextViewControl
312 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
314 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
317 [m_scrollView setHasVerticalScroller:YES];
318 [m_scrollView setHasHorizontalScroller:NO];
319 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
320 NSSize contentSize = [m_scrollView contentSize];
322 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
323 contentSize.width, contentSize.height)];
325 [tv setVerticallyResizable:YES];
326 [tv setHorizontallyResizable:NO];
327 [tv setAutoresizingMask:NSViewWidthSizable];
329 [m_scrollView setDocumentView: tv];
332 [tv setScrollView:sv];
335 wxNSTextViewControl::~wxNSTextViewControl()
338 [m_textView setDelegate: nil];
341 wxString wxNSTextViewControl::GetStringValue() const
345 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
346 wxString result = cf.AsString(m_wxPeer->GetFont().GetEncoding());
347 wxMacConvertNewlines13To10( &result ) ;
350 return wxEmptyString;
352 void wxNSTextViewControl::SetStringValue( const wxString &str)
355 wxMacConvertNewlines10To13( &st );
356 wxMacEditHelper helper(m_textView);
359 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
361 void wxNSTextViewControl::Copy()
364 [m_textView copy:nil];
368 void wxNSTextViewControl::Cut()
371 [m_textView cut:nil];
374 void wxNSTextViewControl::Paste()
377 [m_textView paste:nil];
380 bool wxNSTextViewControl::CanPaste() const
385 void wxNSTextViewControl::SetEditable(bool editable)
388 [m_textView setEditable: editable];
391 void wxNSTextViewControl::GetSelection( long* from, long* to) const
395 NSRange range = [m_textView selectedRange];
396 *from = range.location;
397 *to = range.location + range.length;
401 void wxNSTextViewControl::SetSelection( long from , long to )
403 long textLength = [[m_textView string] length];
404 if ((from == -1) && (to == -1))
411 from = wxMin(textLength,wxMax(from,0)) ;
415 to = wxMax(0,wxMin(textLength,to)) ;
418 NSRange selrange = NSMakeRange(from, to-from);
419 [m_textView setSelectedRange:selrange];
420 [m_textView scrollRangeToVisible:selrange];
423 void wxNSTextViewControl::WriteText(const wxString& str)
426 wxMacConvertNewlines10To13( &st );
427 wxMacEditHelper helper(m_textView);
429 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
432 // wxNSTextFieldControl
434 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
436 m_textField = (NSTextField*) w;
437 [m_textField setDelegate: w];
438 m_selStart = m_selEnd = 0;
441 wxNSTextFieldControl::~wxNSTextFieldControl()
444 [m_textField setDelegate: nil];
447 wxString wxNSTextFieldControl::GetStringValue() const
449 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
450 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
453 void wxNSTextFieldControl::SetStringValue( const wxString &str)
455 wxMacEditHelper helper(m_textField);
456 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
459 void wxNSTextFieldControl::Copy()
461 NSText* editor = [m_textField currentEditor];
468 void wxNSTextFieldControl::Cut()
470 NSText* editor = [m_textField currentEditor];
477 void wxNSTextFieldControl::Paste()
479 NSText* editor = [m_textField currentEditor];
486 bool wxNSTextFieldControl::CanPaste() const
491 void wxNSTextFieldControl::SetEditable(bool editable)
493 [m_textField setEditable:editable];
496 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
498 NSText* editor = [m_textField currentEditor];
501 NSRange range = [editor selectedRange];
502 *from = range.location;
503 *to = range.location + range.length;
512 void wxNSTextFieldControl::SetSelection( long from , long to )
514 long textLength = [[m_textField stringValue] length];
515 if ((from == -1) && (to == -1))
522 from = wxMin(textLength,wxMax(from,0)) ;
526 to = wxMax(0,wxMin(textLength,to)) ;
529 NSText* editor = [m_textField currentEditor];
532 [editor setSelectedRange:NSMakeRange(from, to-from)];
541 void wxNSTextFieldControl::WriteText(const wxString& str)
543 NSText* editor = [m_textField currentEditor];
546 wxMacEditHelper helper(m_textField);
547 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
551 wxString val = GetStringValue() ;
553 GetSelection( &start , &end ) ;
554 val.Remove( start , end - start ) ;
555 val.insert( start , str ) ;
556 SetStringValue( val ) ;
557 SetSelection( start + str.length() , start + str.length() ) ;
561 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
562 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
564 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
565 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
567 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
568 event.SetEventObject( wxpeer );
569 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
570 wxpeer->HandleWindowEvent( event );
578 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
579 wxWindowMac* WXUNUSED(parent),
580 wxWindowID WXUNUSED(id),
585 long WXUNUSED(extraStyle))
587 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
588 wxWidgetCocoaImpl* c = NULL;
590 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
592 wxNSTextScrollView* v = nil;
593 v = [[wxNSTextScrollView alloc] initWithFrame:r];
594 c = new wxNSTextViewControl( wxpeer, v );
595 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
599 NSTextField* v = nil;
600 if ( style & wxTE_PASSWORD )
601 v = [[wxNSSecureTextField alloc] initWithFrame:r];
603 v = [[wxNSTextField alloc] initWithFrame:r];
605 if ( style & wxNO_BORDER )
607 // FIXME: How can we remove the native control's border?
608 // setBordered is separate from the text ctrl's border.
614 c = new wxNSTextFieldControl( wxpeer, v );
615 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
622 #endif // wxUSE_TEXTCTRL