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 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
434 if ([m_textView respondsToSelector:@selector(setFont:)])
435 [m_textView setFont: font.OSXGetNSFont()];
439 // wxNSTextFieldControl
441 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
443 m_textField = (NSTextField*) w;
444 [m_textField setDelegate: w];
445 m_selStart = m_selEnd = 0;
448 wxNSTextFieldControl::~wxNSTextFieldControl()
451 [m_textField setDelegate: nil];
454 wxString wxNSTextFieldControl::GetStringValue() const
456 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
457 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
460 void wxNSTextFieldControl::SetStringValue( const wxString &str)
462 wxMacEditHelper helper(m_textField);
463 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
466 void wxNSTextFieldControl::Copy()
468 NSText* editor = [m_textField currentEditor];
475 void wxNSTextFieldControl::Cut()
477 NSText* editor = [m_textField currentEditor];
484 void wxNSTextFieldControl::Paste()
486 NSText* editor = [m_textField currentEditor];
493 bool wxNSTextFieldControl::CanPaste() const
498 void wxNSTextFieldControl::SetEditable(bool editable)
500 [m_textField setEditable:editable];
503 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
505 NSText* editor = [m_textField currentEditor];
508 NSRange range = [editor selectedRange];
509 *from = range.location;
510 *to = range.location + range.length;
519 void wxNSTextFieldControl::SetSelection( long from , long to )
521 long textLength = [[m_textField stringValue] length];
522 if ((from == -1) && (to == -1))
529 from = wxMin(textLength,wxMax(from,0)) ;
533 to = wxMax(0,wxMin(textLength,to)) ;
536 NSText* editor = [m_textField currentEditor];
539 [editor setSelectedRange:NSMakeRange(from, to-from)];
548 void wxNSTextFieldControl::WriteText(const wxString& str)
550 NSText* editor = [m_textField currentEditor];
553 wxMacEditHelper helper(m_textField);
554 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
558 wxString val = GetStringValue() ;
560 GetSelection( &start , &end ) ;
561 val.Remove( start , end - start ) ;
562 val.insert( start , str ) ;
563 SetStringValue( val ) ;
564 SetSelection( start + str.length() , start + str.length() ) ;
568 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
569 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
571 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
572 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
574 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
575 event.SetEventObject( wxpeer );
576 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
577 wxpeer->HandleWindowEvent( event );
585 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
586 wxWindowMac* WXUNUSED(parent),
587 wxWindowID WXUNUSED(id),
592 long WXUNUSED(extraStyle))
594 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
595 wxWidgetCocoaImpl* c = NULL;
597 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
599 wxNSTextScrollView* v = nil;
600 v = [[wxNSTextScrollView alloc] initWithFrame:r];
601 c = new wxNSTextViewControl( wxpeer, v );
602 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
606 NSTextField* v = nil;
607 if ( style & wxTE_PASSWORD )
608 v = [[wxNSSecureTextField alloc] initWithFrame:r];
610 v = [[wxNSTextField alloc] initWithFrame:r];
612 if ( style & wxNO_BORDER )
614 // FIXME: How can we remove the native control's border?
615 // setBordered is separate from the text ctrl's border.
621 c = new wxNSTextFieldControl( wxpeer, v );
622 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
629 #endif // wxUSE_TEXTCTRL