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 wxNSSecureTextField : NSSecureTextField
57 @implementation wxNSSecureTextField
61 static BOOL initialized = NO;
65 wxOSXCocoaClassAddWXMethods( self );
69 - (void)controlTextDidChange:(NSNotification *)aNotification
71 wxUnusedVar(aNotification);
72 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
75 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
77 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
78 event.SetEventObject( wxpeer );
79 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
80 wxpeer->HandleWindowEvent( event );
85 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
87 wxUnusedVar(aNotification);
88 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
91 impl->DoNotifyFocusEvent( false, NULL );
97 @interface wxNSTextScrollView : NSScrollView
102 @interface wxNSTextView : NSTextView
104 wxNSTextScrollView* scrollView;
107 - (void)setScrollView: (wxNSTextScrollView *) sv;
108 - (wxNSTextScrollView*) scrollView;
112 @implementation wxNSTextScrollView
116 static BOOL initialized = NO;
120 wxOSXCocoaClassAddWXMethods( self );
124 - (void)textDidChange:(NSNotification *)aNotification
126 wxUnusedVar(aNotification);
127 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
130 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
132 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
133 event.SetEventObject( wxpeer );
134 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
135 wxpeer->HandleWindowEvent( event );
140 - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)commandSelector
142 wxUnusedVar(aTextView);
143 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
146 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
147 if (commandSelector == @selector(insertNewline:))
149 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
151 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
152 event.SetEventObject( wxpeer );
153 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
154 wxpeer->HandleWindowEvent( event );
162 - (void)textDidEndEditing:(NSNotification *)aNotification
164 wxUnusedVar(aNotification);
165 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
168 impl->DoNotifyFocusEvent( false, NULL );
173 @implementation wxNSTextView
175 - (BOOL) becomeFirstResponder
177 BOOL val = [super becomeFirstResponder];
181 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( scrollView );
183 impl->DoNotifyFocusEvent( true, NULL );
189 - (void)setScrollView: (wxNSTextScrollView *) sv
194 - (wxNSTextScrollView*) scrollView
201 @implementation wxNSTextField
205 static BOOL initialized = NO;
209 wxOSXCocoaClassAddWXMethods( self );
213 - (void) setEnabled:(BOOL) flag
215 [super setEnabled: flag];
217 if (![self drawsBackground]) {
218 // Static text is drawn incorrectly when disabled.
219 // For an explanation, see
220 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
222 [self setTextColor: [NSColor controlTextColor]];
224 [self setTextColor: [NSColor secondarySelectedControlColor]];
229 - (void)controlTextDidChange:(NSNotification *)aNotification
231 wxUnusedVar(aNotification);
232 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
235 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
237 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
238 event.SetEventObject( wxpeer );
239 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
240 wxpeer->HandleWindowEvent( event );
245 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
247 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
249 wxUnusedVar(textView);
250 wxUnusedVar(control);
251 if (commandSelector == @selector(insertNewline:))
253 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
256 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
257 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
259 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
260 event.SetEventObject( wxpeer );
261 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
262 wxpeer->HandleWindowEvent( event );
270 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
272 wxUnusedVar(aNotification);
273 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
276 impl->DoNotifyFocusEvent( false, NULL );
281 // wxNSTextViewControl
283 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
285 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
288 [m_scrollView setHasVerticalScroller:YES];
289 [m_scrollView setHasHorizontalScroller:NO];
290 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
291 NSSize contentSize = [m_scrollView contentSize];
293 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
294 contentSize.width, contentSize.height)];
296 [tv setVerticallyResizable:YES];
297 [tv setHorizontallyResizable:NO];
298 [tv setAutoresizingMask:NSViewWidthSizable];
300 [m_scrollView setDocumentView: tv];
303 [tv setScrollView:sv];
306 wxNSTextViewControl::~wxNSTextViewControl()
309 [m_textView setDelegate: nil];
312 wxString wxNSTextViewControl::GetStringValue() const
316 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
317 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
319 return wxEmptyString;
321 void wxNSTextViewControl::SetStringValue( const wxString &str)
324 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
326 void wxNSTextViewControl::Copy()
329 [m_textView copy:nil];
333 void wxNSTextViewControl::Cut()
336 [m_textView cut:nil];
339 void wxNSTextViewControl::Paste()
342 [m_textView paste:nil];
345 bool wxNSTextViewControl::CanPaste() const
350 void wxNSTextViewControl::SetEditable(bool editable)
353 [m_textView setEditable: editable];
356 void wxNSTextViewControl::GetSelection( long* from, long* to) const
360 NSRange range = [m_textView selectedRange];
361 *from = range.location;
362 *to = range.location + range.length;
366 void wxNSTextViewControl::SetSelection( long from , long to )
368 NSRange selrange = NSMakeRange(from, to-from);
369 [m_textView setSelectedRange:selrange];
370 [m_textView scrollRangeToVisible:selrange];
373 void wxNSTextViewControl::WriteText(const wxString& str)
375 // temp hack to get logging working early
376 wxString former = GetStringValue();
377 SetStringValue( former + str );
378 SetSelection(GetStringValue().length(), GetStringValue().length());
381 // wxNSTextFieldControl
383 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
385 m_textField = (NSTextField*) w;
386 [m_textField setDelegate: w];
389 wxNSTextFieldControl::~wxNSTextFieldControl()
392 [m_textField setDelegate: nil];
395 wxString wxNSTextFieldControl::GetStringValue() const
397 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
398 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
400 void wxNSTextFieldControl::SetStringValue( const wxString &str)
402 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
404 void wxNSTextFieldControl::Copy()
406 NSText* editor = [m_textField currentEditor];
413 void wxNSTextFieldControl::Cut()
415 NSText* editor = [m_textField currentEditor];
422 void wxNSTextFieldControl::Paste()
424 NSText* editor = [m_textField currentEditor];
431 bool wxNSTextFieldControl::CanPaste() const
436 void wxNSTextFieldControl::SetEditable(bool editable)
438 [m_textField setEditable:editable];
441 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
443 NSText* editor = [m_textField currentEditor];
446 NSRange range = [editor selectedRange];
447 *from = range.location;
448 *to = range.location + range.length;
452 void wxNSTextFieldControl::SetSelection( long from , long to )
454 NSText* editor = [m_textField currentEditor];
457 [editor setSelectedRange:NSMakeRange(from, to-from)];
461 void wxNSTextFieldControl::WriteText(const wxString& str)
463 // temp hack to get logging working early
464 wxString former = GetStringValue();
465 SetStringValue( former + str );
466 SetSelection(GetStringValue().length(), GetStringValue().length());
469 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
470 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
472 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
473 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
475 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
476 event.SetEventObject( wxpeer );
477 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
478 wxpeer->HandleWindowEvent( event );
486 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
487 wxWindowMac* WXUNUSED(parent),
488 wxWindowID WXUNUSED(id),
493 long WXUNUSED(extraStyle))
495 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
496 wxWidgetCocoaImpl* c = NULL;
498 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
500 wxNSTextScrollView* v = nil;
501 v = [[wxNSTextScrollView alloc] initWithFrame:r];
502 c = new wxNSTextViewControl( wxpeer, v );
503 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
507 NSTextField* v = nil;
508 if ( style & wxTE_PASSWORD )
509 v = [[wxNSSecureTextField alloc] initWithFrame:r];
511 v = [[wxNSTextField alloc] initWithFrame:r];
513 if ( style & wxNO_BORDER )
515 // FIXME: How can we remove the native control's border?
516 // setBordered is separate from the text ctrl's border.
522 c = new wxNSTextFieldControl( wxpeer, v );
523 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
530 #endif // wxUSE_TEXTCTRL