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 );
87 @interface wxNSTextView : NSScrollView
93 @implementation wxNSTextView
97 static BOOL initialized = NO;
101 wxOSXCocoaClassAddWXMethods( self );
105 - (void)textDidChange:(NSNotification *)aNotification
107 wxUnusedVar(aNotification);
108 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
111 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
113 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
114 event.SetEventObject( wxpeer );
115 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
116 wxpeer->HandleWindowEvent( event );
121 - (BOOL)textView:(NSTextView *)aTextView doCommandBySelector:(SEL)commandSelector
123 wxUnusedVar(aTextView);
124 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
127 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
128 if (commandSelector == @selector(insertNewline:))
130 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
132 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
133 event.SetEventObject( wxpeer );
134 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
135 wxpeer->HandleWindowEvent( event );
144 @implementation wxNSTextField
148 static BOOL initialized = NO;
152 wxOSXCocoaClassAddWXMethods( self );
156 - (void) setEnabled:(BOOL) flag
158 [super setEnabled: flag];
160 if (![self drawsBackground]) {
161 // Static text is drawn incorrectly when disabled.
162 // For an explanation, see
163 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
165 [self setTextColor: [NSColor controlTextColor]];
167 [self setTextColor: [NSColor secondarySelectedControlColor]];
172 - (void)controlTextDidChange:(NSNotification *)aNotification
174 wxUnusedVar(aNotification);
175 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
178 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
180 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
181 event.SetEventObject( wxpeer );
182 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
183 wxpeer->HandleWindowEvent( event );
188 typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
190 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
192 wxUnusedVar(textView);
193 wxUnusedVar(control);
194 if (commandSelector == @selector(insertNewline:))
196 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
199 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
200 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
202 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
203 event.SetEventObject( wxpeer );
204 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
205 wxpeer->HandleWindowEvent( event );
213 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
217 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
219 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
220 event.SetEventObject( wxpeer );
221 event.SetWindow( wxpeer );
222 wxpeer->HandleWindowEvent( event );
229 // wxNSTextViewControl
231 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
233 m_scrollView = (NSScrollView*) w;
235 [m_scrollView setHasVerticalScroller:YES];
236 [m_scrollView setHasHorizontalScroller:NO];
237 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
238 NSSize contentSize = [m_scrollView contentSize];
240 m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
241 contentSize.width, contentSize.height)];
242 [m_textView setVerticallyResizable:YES];
243 [m_textView setHorizontallyResizable:NO];
244 [m_textView setAutoresizingMask:NSViewWidthSizable];
246 [m_scrollView setDocumentView: m_textView];
248 [m_textView setDelegate: w];
251 wxNSTextViewControl::~wxNSTextViewControl()
254 [m_textView setDelegate: nil];
257 wxString wxNSTextViewControl::GetStringValue() const
261 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
262 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
264 return wxEmptyString;
266 void wxNSTextViewControl::SetStringValue( const wxString &str)
269 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
271 void wxNSTextViewControl::Copy()
274 [m_textView copy:nil];
278 void wxNSTextViewControl::Cut()
281 [m_textView cut:nil];
284 void wxNSTextViewControl::Paste()
287 [m_textView paste:nil];
290 bool wxNSTextViewControl::CanPaste() const
295 void wxNSTextViewControl::SetEditable(bool editable)
298 [m_textView setEditable: editable];
301 void wxNSTextViewControl::GetSelection( long* from, long* to) const
305 NSRange range = [m_textView selectedRange];
306 *from = range.location;
307 *to = range.location + range.length;
311 void wxNSTextViewControl::SetSelection( long from , long to )
313 NSRange selrange = NSMakeRange(from, to-from);
314 [m_textView setSelectedRange:selrange];
315 [m_textView scrollRangeToVisible:selrange];
318 void wxNSTextViewControl::WriteText(const wxString& str)
320 // temp hack to get logging working early
321 wxString former = GetStringValue();
322 SetStringValue( former + str );
323 SetSelection(GetStringValue().length(), GetStringValue().length());
326 // wxNSTextFieldControl
328 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
330 m_textField = (NSTextField*) w;
331 [m_textField setDelegate: w];
334 wxNSTextFieldControl::~wxNSTextFieldControl()
337 [m_textField setDelegate: nil];
340 wxString wxNSTextFieldControl::GetStringValue() const
342 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
343 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
345 void wxNSTextFieldControl::SetStringValue( const wxString &str)
347 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
349 void wxNSTextFieldControl::Copy()
351 NSText* editor = [m_textField currentEditor];
358 void wxNSTextFieldControl::Cut()
360 NSText* editor = [m_textField currentEditor];
367 void wxNSTextFieldControl::Paste()
369 NSText* editor = [m_textField currentEditor];
376 bool wxNSTextFieldControl::CanPaste() const
381 void wxNSTextFieldControl::SetEditable(bool editable)
383 [m_textField setEditable:editable];
386 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
388 NSText* editor = [m_textField currentEditor];
391 NSRange range = [editor selectedRange];
392 *from = range.location;
393 *to = range.location + range.length;
397 void wxNSTextFieldControl::SetSelection( long from , long to )
399 NSText* editor = [m_textField currentEditor];
402 [editor setSelectedRange:NSMakeRange(from, to-from)];
406 void wxNSTextFieldControl::WriteText(const wxString& str)
408 // temp hack to get logging working early
409 wxString former = GetStringValue();
410 SetStringValue( former + str );
411 SetSelection(GetStringValue().length(), GetStringValue().length());
414 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
415 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
417 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
418 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
420 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
421 event.SetEventObject( wxpeer );
422 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
423 wxpeer->HandleWindowEvent( event );
431 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
432 wxWindowMac* WXUNUSED(parent),
433 wxWindowID WXUNUSED(id),
438 long WXUNUSED(extraStyle))
440 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
441 wxWidgetCocoaImpl* c = NULL;
443 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
445 wxNSTextView* v = nil;
446 v = [[wxNSTextView alloc] initWithFrame:r];
447 c = new wxNSTextViewControl( wxpeer, v );
448 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
452 NSTextField* v = nil;
453 if ( style & wxTE_PASSWORD )
454 v = [[wxNSSecureTextField alloc] initWithFrame:r];
456 v = [[wxNSTextField alloc] initWithFrame:r];
458 if ( style & wxNO_BORDER )
460 // FIXME: How can we remove the native control's border?
461 // setBordered is separate from the text ctrl's border.
467 c = new wxNSTextFieldControl( wxpeer, v );
468 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
475 #endif // wxUSE_TEXTCTRL