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;
66 m_formerState = [textView isEditable];
67 [textView setEditable:YES];
74 [m_textView setEditable:m_formerState];
82 @implementation wxNSSecureTextField
86 static BOOL initialized = NO;
90 wxOSXCocoaClassAddWXMethods( self );
94 - (void)controlTextDidChange:(NSNotification *)aNotification
96 wxUnusedVar(aNotification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
100 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
102 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
103 event.SetEventObject( wxpeer );
104 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
105 wxpeer->HandleWindowEvent( event );
110 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
112 wxUnusedVar(aNotification);
113 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
116 impl->DoNotifyFocusEvent( false, NULL );
122 @interface wxNSTextScrollView : NSScrollView
127 @implementation wxNSTextScrollView
131 static BOOL initialized = NO;
135 wxOSXCocoaClassAddWXMethods( self );
141 @implementation wxNSTextFieldEditor
143 - (void) keyDown:(NSEvent*) event
145 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
146 lastKeyDownEvent = event;
147 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
148 [super keyDown:event];
149 lastKeyDownEvent = nil;
152 - (void) keyUp:(NSEvent*) event
154 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
155 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
159 - (void) flagsChanged:(NSEvent*) event
161 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
162 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
163 [super flagsChanged:event];
166 - (BOOL) performKeyEquivalent:(NSEvent*) event
168 BOOL retval = [super performKeyEquivalent:event];
172 - (void) insertText:(id) str
174 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
175 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
177 [super insertText:str];
183 @implementation wxNSTextView
187 static BOOL initialized = NO;
191 wxOSXCocoaClassAddWXMethods( self );
197 @implementation wxNSTextField
201 static BOOL initialized = NO;
205 wxOSXCocoaClassAddWXMethods( self );
209 - (id) initWithFrame:(NSRect) frame
211 self = [super initWithFrame:frame];
218 [fieldEditor release];
222 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
224 fieldEditor = editor;
227 - (wxNSTextFieldEditor*) fieldEditor
233 - (void) setEnabled:(BOOL) flag
235 [super setEnabled: flag];
237 if (![self drawsBackground]) {
238 // Static text is drawn incorrectly when disabled.
239 // For an explanation, see
240 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
242 [self setTextColor: [NSColor controlTextColor]];
244 [self setTextColor: [NSColor secondarySelectedControlColor]];
249 - (void)controlTextDidChange:(NSNotification *)aNotification
251 wxUnusedVar(aNotification);
252 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
255 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
257 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
258 event.SetEventObject( wxpeer );
259 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
260 wxpeer->HandleWindowEvent( event );
265 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
267 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
269 wxUnusedVar(textView);
270 wxUnusedVar(control);
271 if (commandSelector == @selector(insertNewline:))
273 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
276 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
277 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
279 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
280 event.SetEventObject( wxpeer );
281 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
282 wxpeer->HandleWindowEvent( event );
290 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
292 wxUnusedVar(aNotification);
293 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
296 impl->DoNotifyFocusEvent( false, NULL );
301 // wxNSTextViewControl
303 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
305 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
308 [m_scrollView setHasVerticalScroller:YES];
309 [m_scrollView setHasHorizontalScroller:NO];
310 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
311 NSSize contentSize = [m_scrollView contentSize];
313 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
314 contentSize.width, contentSize.height)];
316 [tv setVerticallyResizable:YES];
317 [tv setHorizontallyResizable:NO];
318 [tv setAutoresizingMask:NSViewWidthSizable];
320 [m_scrollView setDocumentView: tv];
324 InstallEventHandler(tv);
327 wxNSTextViewControl::~wxNSTextViewControl()
330 [m_textView setDelegate: nil];
333 wxString wxNSTextViewControl::GetStringValue() const
337 wxString result = wxCFStringRef::AsString([m_textView string], 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);
420 NSEvent* formerEvent = m_lastKeyDownEvent;
421 m_lastKeyDownEvent = nil;
422 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
423 m_lastKeyDownEvent = formerEvent;
426 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
428 if ([m_textView respondsToSelector:@selector(setFont:)])
429 [m_textView setFont: font.OSXGetNSFont()];
433 // wxNSTextFieldControl
435 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
437 m_textField = (NSTextField*) w;
438 [m_textField setDelegate: w];
439 m_selStart = m_selEnd = 0;
440 m_hasEditor = [w isKindOfClass:[NSTextField class]];
443 wxNSTextFieldControl::~wxNSTextFieldControl()
446 [m_textField setDelegate: nil];
449 wxString wxNSTextFieldControl::GetStringValue() const
451 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
454 void wxNSTextFieldControl::SetStringValue( const wxString &str)
456 wxMacEditHelper helper(m_textField);
457 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
460 void wxNSTextFieldControl::Copy()
462 NSText* editor = [m_textField currentEditor];
469 void wxNSTextFieldControl::Cut()
471 NSText* editor = [m_textField currentEditor];
478 void wxNSTextFieldControl::Paste()
480 NSText* editor = [m_textField currentEditor];
487 bool wxNSTextFieldControl::CanPaste() const
492 void wxNSTextFieldControl::SetEditable(bool editable)
494 [m_textField setEditable:editable];
497 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
499 NSText* editor = [m_textField currentEditor];
502 NSRange range = [editor selectedRange];
503 *from = range.location;
504 *to = range.location + range.length;
513 void wxNSTextFieldControl::SetSelection( long from , long to )
515 long textLength = [[m_textField stringValue] length];
516 if ((from == -1) && (to == -1))
523 from = wxMin(textLength,wxMax(from,0)) ;
527 to = wxMax(0,wxMin(textLength,to)) ;
530 NSText* editor = [m_textField currentEditor];
533 [editor setSelectedRange:NSMakeRange(from, to-from)];
542 void wxNSTextFieldControl::WriteText(const wxString& str)
544 NSEvent* formerEvent = m_lastKeyDownEvent;
545 m_lastKeyDownEvent = nil;
546 NSText* editor = [m_textField currentEditor];
549 wxMacEditHelper helper(m_textField);
550 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
554 wxString val = GetStringValue() ;
556 GetSelection( &start , &end ) ;
557 val.Remove( start , end - start ) ;
558 val.insert( start , str ) ;
559 SetStringValue( val ) ;
560 SetSelection( start + str.length() , start + str.length() ) ;
562 m_lastKeyDownEvent = formerEvent;
565 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
566 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
568 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
569 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
571 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
572 event.SetEventObject( wxpeer );
573 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
574 wxpeer->HandleWindowEvent( event );
582 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
583 wxWindowMac* WXUNUSED(parent),
584 wxWindowID WXUNUSED(id),
585 const wxString& WXUNUSED(str),
589 long WXUNUSED(extraStyle))
591 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
592 wxWidgetCocoaImpl* c = NULL;
594 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
596 wxNSTextScrollView* v = nil;
597 v = [[wxNSTextScrollView alloc] initWithFrame:r];
598 c = new wxNSTextViewControl( wxpeer, v );
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 );
624 #endif // wxUSE_TEXTCTRL