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);
419 NSEvent* formerEvent = m_lastKeyDownEvent;
420 m_lastKeyDownEvent = nil;
421 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
422 m_lastKeyDownEvent = formerEvent;
425 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
427 if ([m_textView respondsToSelector:@selector(setFont:)])
428 [m_textView setFont: font.OSXGetNSFont()];
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;
439 m_hasEditor = [w isKindOfClass:[NSTextField class]];
442 wxNSTextFieldControl::~wxNSTextFieldControl()
445 [m_textField setDelegate: nil];
448 wxString wxNSTextFieldControl::GetStringValue() const
450 return wxCFStringRef::AsString([m_textField stringValue], 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 NSEvent* formerEvent = m_lastKeyDownEvent;
544 m_lastKeyDownEvent = nil;
545 NSText* editor = [m_textField currentEditor];
548 wxMacEditHelper helper(m_textField);
549 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
553 wxString val = GetStringValue() ;
555 GetSelection( &start , &end ) ;
556 val.Remove( start , end - start ) ;
557 val.insert( start , str ) ;
558 SetStringValue( val ) ;
559 SetSelection( start + str.length() , start + str.length() ) ;
561 m_lastKeyDownEvent = formerEvent;
564 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
565 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
567 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
568 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
570 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
571 event.SetEventObject( wxpeer );
572 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
573 wxpeer->HandleWindowEvent( event );
581 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
582 wxWindowMac* WXUNUSED(parent),
583 wxWindowID WXUNUSED(id),
588 long WXUNUSED(extraStyle))
590 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
591 wxWidgetCocoaImpl* c = NULL;
593 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
595 wxNSTextScrollView* v = nil;
596 v = [[wxNSTextScrollView alloc] initWithFrame:r];
597 c = new wxNSTextViewControl( wxpeer, v );
598 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
602 NSTextField* v = nil;
603 if ( style & wxTE_PASSWORD )
604 v = [[wxNSSecureTextField alloc] initWithFrame:r];
606 v = [[wxNSTextField alloc] initWithFrame:r];
608 if ( style & wxNO_BORDER )
610 // FIXME: How can we remove the native control's border?
611 // setBordered is separate from the text ctrl's border.
617 c = new wxNSTextFieldControl( wxpeer, v );
618 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
625 #endif // wxUSE_TEXTCTRL