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 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
337 wxMacConvertNewlines13To10( &result ) ;
340 return wxEmptyString;
342 void wxNSTextViewControl::SetStringValue( const wxString &str)
345 wxMacConvertNewlines10To13( &st );
346 wxMacEditHelper helper(m_textView);
349 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
352 void wxNSTextViewControl::Copy()
355 [m_textView copy:nil];
359 void wxNSTextViewControl::Cut()
362 [m_textView cut:nil];
365 void wxNSTextViewControl::Paste()
368 [m_textView paste:nil];
371 bool wxNSTextViewControl::CanPaste() const
376 void wxNSTextViewControl::SetEditable(bool editable)
379 [m_textView setEditable: editable];
382 void wxNSTextViewControl::GetSelection( long* from, long* to) const
386 NSRange range = [m_textView selectedRange];
387 *from = range.location;
388 *to = range.location + range.length;
392 void wxNSTextViewControl::SetSelection( long from , long to )
394 long textLength = [[m_textView string] length];
395 if ((from == -1) && (to == -1))
402 from = wxMin(textLength,wxMax(from,0)) ;
406 to = wxMax(0,wxMin(textLength,to)) ;
409 NSRange selrange = NSMakeRange(from, to-from);
410 [m_textView setSelectedRange:selrange];
411 [m_textView scrollRangeToVisible:selrange];
414 void wxNSTextViewControl::WriteText(const wxString& str)
417 wxMacConvertNewlines10To13( &st );
418 wxMacEditHelper helper(m_textView);
420 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
423 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
425 if ([m_textView respondsToSelector:@selector(setFont:)])
426 [m_textView setFont: font.OSXGetNSFont()];
430 // wxNSTextFieldControl
432 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
434 m_textField = (NSTextField*) w;
435 [m_textField setDelegate: w];
436 m_selStart = m_selEnd = 0;
437 m_hasEditor = [w isKindOfClass:[NSTextField class]];
440 wxNSTextFieldControl::~wxNSTextFieldControl()
443 [m_textField setDelegate: nil];
446 wxString wxNSTextFieldControl::GetStringValue() const
448 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
451 void wxNSTextFieldControl::SetStringValue( const wxString &str)
453 wxMacEditHelper helper(m_textField);
454 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
457 void wxNSTextFieldControl::Copy()
459 NSText* editor = [m_textField currentEditor];
466 void wxNSTextFieldControl::Cut()
468 NSText* editor = [m_textField currentEditor];
475 void wxNSTextFieldControl::Paste()
477 NSText* editor = [m_textField currentEditor];
484 bool wxNSTextFieldControl::CanPaste() const
489 void wxNSTextFieldControl::SetEditable(bool editable)
491 [m_textField setEditable:editable];
494 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
496 NSText* editor = [m_textField currentEditor];
499 NSRange range = [editor selectedRange];
500 *from = range.location;
501 *to = range.location + range.length;
510 void wxNSTextFieldControl::SetSelection( long from , long to )
512 long textLength = [[m_textField stringValue] length];
513 if ((from == -1) && (to == -1))
520 from = wxMin(textLength,wxMax(from,0)) ;
524 to = wxMax(0,wxMin(textLength,to)) ;
527 NSText* editor = [m_textField currentEditor];
530 [editor setSelectedRange:NSMakeRange(from, to-from)];
539 void wxNSTextFieldControl::WriteText(const wxString& str)
541 NSText* editor = [m_textField currentEditor];
544 wxMacEditHelper helper(m_textField);
545 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
549 wxString val = GetStringValue() ;
551 GetSelection( &start , &end ) ;
552 val.Remove( start , end - start ) ;
553 val.insert( start , str ) ;
554 SetStringValue( val ) ;
555 SetSelection( start + str.length() , start + str.length() ) ;
559 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
560 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
562 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
563 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
565 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
566 event.SetEventObject( wxpeer );
567 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
568 wxpeer->HandleWindowEvent( event );
576 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
577 wxWindowMac* WXUNUSED(parent),
578 wxWindowID WXUNUSED(id),
583 long WXUNUSED(extraStyle))
585 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
586 wxWidgetCocoaImpl* c = NULL;
588 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
590 wxNSTextScrollView* v = nil;
591 v = [[wxNSTextScrollView alloc] initWithFrame:r];
592 c = new wxNSTextViewControl( wxpeer, v );
593 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
597 NSTextField* v = nil;
598 if ( style & wxTE_PASSWORD )
599 v = [[wxNSSecureTextField alloc] initWithFrame:r];
601 v = [[wxNSTextField alloc] initWithFrame:r];
603 if ( style & wxNO_BORDER )
605 // FIXME: How can we remove the native control's border?
606 // setBordered is separate from the text ctrl's border.
612 c = new wxNSTextFieldControl( wxpeer, v );
613 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
620 #endif // wxUSE_TEXTCTRL