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( (WXWidget) [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( (WXWidget) [self delegate] );
154 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
158 - (void) flagsChanged:(NSEvent*) event
160 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [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( (WXWidget) [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];
217 [fieldEditor release];
221 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
223 fieldEditor = editor;
226 - (wxNSTextFieldEditor*) fieldEditor
232 - (void) setEnabled:(BOOL) flag
234 [super setEnabled: flag];
236 if (![self drawsBackground]) {
237 // Static text is drawn incorrectly when disabled.
238 // For an explanation, see
239 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
241 [self setTextColor: [NSColor controlTextColor]];
243 [self setTextColor: [NSColor secondarySelectedControlColor]];
248 - (void)controlTextDidChange:(NSNotification *)aNotification
250 wxUnusedVar(aNotification);
251 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
254 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
256 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
257 event.SetEventObject( wxpeer );
258 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
259 wxpeer->HandleWindowEvent( event );
264 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
266 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
268 wxUnusedVar(textView);
269 wxUnusedVar(control);
270 if (commandSelector == @selector(insertNewline:))
272 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
275 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
276 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
278 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
279 event.SetEventObject( wxpeer );
280 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
281 wxpeer->HandleWindowEvent( event );
289 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
291 wxUnusedVar(aNotification);
292 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
295 impl->DoNotifyFocusEvent( false, NULL );
300 // wxNSTextViewControl
302 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
304 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
307 [m_scrollView setHasVerticalScroller:YES];
308 [m_scrollView setHasHorizontalScroller:NO];
309 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
310 NSSize contentSize = [m_scrollView contentSize];
312 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
313 contentSize.width, contentSize.height)];
315 [tv setVerticallyResizable:YES];
316 [tv setHorizontallyResizable:NO];
317 [tv setAutoresizingMask:NSViewWidthSizable];
319 [m_scrollView setDocumentView: tv];
323 InstallEventHandler(tv);
326 wxNSTextViewControl::~wxNSTextViewControl()
329 [m_textView setDelegate: nil];
332 wxString wxNSTextViewControl::GetStringValue() const
336 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
337 wxString result = cf.AsString(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);
421 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
424 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
426 if ([m_textView respondsToSelector:@selector(setFont:)])
427 [m_textView setFont: font.OSXGetNSFont()];
431 // wxNSTextFieldControl
433 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
435 m_textField = (NSTextField*) w;
436 [m_textField setDelegate: w];
437 m_selStart = m_selEnd = 0;
438 m_hasEditor = [w isKindOfClass:[NSTextField class]];
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