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
54 wxWidgetCocoaImpl* impl;
57 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
58 - (wxWidgetCocoaImpl*) implementation;
61 @implementation wxNSSecureTextField
65 static BOOL initialized = NO;
69 wxOSXCocoaClassAddWXMethods( self );
73 - (wxWidgetCocoaImpl*) implementation
78 - (void) setImplementation:(wxWidgetCocoaImpl*) item
83 - (void)controlTextDidChange:(NSNotification *)aNotification
87 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
89 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
90 event.SetEventObject( wxpeer );
91 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
92 wxpeer->HandleWindowEvent( event );
99 @interface wxNSTextView : NSScrollView
101 wxWidgetCocoaImpl* impl;
104 - (void) setImplementation:(wxWidgetCocoaImpl*) item;
105 - (wxWidgetCocoaImpl*) implementation;
108 @implementation wxNSTextView
112 static BOOL initialized = NO;
116 wxOSXCocoaClassAddWXMethods( self );
120 - (wxWidgetCocoaImpl*) implementation
125 - (void) setImplementation:(wxWidgetCocoaImpl*) item
131 - (void)controlTextDidChange:(NSNotification *)aNotification
135 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
137 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
138 event.SetEventObject( wxpeer );
139 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
140 wxpeer->HandleWindowEvent( event );
146 @implementation wxNSTextField
150 static BOOL initialized = NO;
154 wxOSXCocoaClassAddWXMethods( self );
158 - (wxWidgetCocoaImpl*) implementation
163 - (void) setImplementation:(wxWidgetCocoaImpl*) item
168 - (void) setEnabled:(BOOL) flag
170 [super setEnabled: flag];
172 if (![self drawsBackground]) {
173 // Static text is drawn incorrectly when disabled.
174 // For an explanation, see
175 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
177 [self setTextColor: [NSColor controlTextColor]];
179 [self setTextColor: [NSColor secondarySelectedControlColor]];
184 - (void)controlTextDidChange:(NSNotification *)aNotification
188 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
190 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
191 event.SetEventObject( wxpeer );
192 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
193 wxpeer->HandleWindowEvent( event );
199 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
203 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
205 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
206 event.SetEventObject( wxpeer );
207 event.SetWindow( wxpeer );
208 wxpeer->HandleWindowEvent( event );
215 // wxNSTextViewControl
217 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
219 m_scrollView = (NSScrollView*) w;
220 [w setImplementation: this];
222 [m_scrollView setHasVerticalScroller:YES];
223 [m_scrollView setHasHorizontalScroller:NO];
224 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
225 NSSize contentSize = [m_scrollView contentSize];
227 m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
228 contentSize.width, contentSize.height)];
229 [m_textView setVerticallyResizable:YES];
230 [m_textView setHorizontallyResizable:NO];
231 [m_textView setAutoresizingMask:NSViewWidthSizable];
233 [m_scrollView setDocumentView: m_textView];
235 [m_textView setDelegate: w];
238 wxNSTextViewControl::~wxNSTextViewControl()
241 [m_textView setDelegate: nil];
244 wxString wxNSTextViewControl::GetStringValue() const
248 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
249 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
251 return wxEmptyString;
253 void wxNSTextViewControl::SetStringValue( const wxString &str)
256 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
258 void wxNSTextViewControl::Copy()
261 [m_textView copy:nil];
265 void wxNSTextViewControl::Cut()
268 [m_textView cut:nil];
271 void wxNSTextViewControl::Paste()
274 [m_textView paste:nil];
277 bool wxNSTextViewControl::CanPaste() const
282 void wxNSTextViewControl::SetEditable(bool editable)
285 [m_textView setEditable: editable];
288 void wxNSTextViewControl::GetSelection( long* from, long* to) const
292 NSRange range = [m_textView selectedRange];
293 *from = range.location;
294 *to = range.location + range.length;
298 void wxNSTextViewControl::SetSelection( long from , long to )
300 [m_textView setSelectedRange:NSMakeRange(from, to-from)];
303 void wxNSTextViewControl::WriteText(const wxString& str)
305 // temp hack to get logging working early
306 wxString former = GetStringValue();
307 SetStringValue( former + str );
310 // wxNSTextFieldControl
312 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
314 m_textField = (NSTextField*) w;
315 [m_textField setDelegate: w];
318 wxNSTextFieldControl::~wxNSTextFieldControl()
321 [m_textField setDelegate: nil];
324 wxString wxNSTextFieldControl::GetStringValue() const
326 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
327 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
329 void wxNSTextFieldControl::SetStringValue( const wxString &str)
331 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
333 void wxNSTextFieldControl::Copy()
335 NSText* editor = [m_textField currentEditor];
342 void wxNSTextFieldControl::Cut()
344 NSText* editor = [m_textField currentEditor];
351 void wxNSTextFieldControl::Paste()
353 NSText* editor = [m_textField currentEditor];
360 bool wxNSTextFieldControl::CanPaste() const
365 void wxNSTextFieldControl::SetEditable(bool editable)
367 [m_textField setEditable:editable];
370 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
372 NSText* editor = [m_textField currentEditor];
375 NSRange range = [editor selectedRange];
376 *from = range.location;
377 *to = range.location + range.length;
381 void wxNSTextFieldControl::SetSelection( long from , long to )
383 NSText* editor = [m_textField currentEditor];
386 [editor setSelectedRange:NSMakeRange(from, to-from)];
390 void wxNSTextFieldControl::WriteText(const wxString& str)
392 // temp hack to get logging working early
393 wxString former = GetStringValue();
394 SetStringValue( former + str );
397 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
399 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
400 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
402 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
403 event.SetEventObject( wxpeer );
404 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
405 wxpeer->HandleWindowEvent( event );
413 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
422 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
423 NSTextField* v = nil;
424 wxWidgetCocoaImpl* c = NULL;
426 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
428 v = [[wxNSTextView alloc] initWithFrame:r];
429 c = new wxNSTextViewControl( wxpeer, v );
430 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
434 if ( style & wxTE_PASSWORD )
435 v = [[wxNSSecureTextField alloc] initWithFrame:r];
437 v = [[wxNSTextField alloc] initWithFrame:r];
439 if ( style & wxNO_BORDER )
441 // FIXME: How can we remove the native control's border?
442 // setBordered is separate from the text ctrl's border.
448 c = new wxNSTextFieldControl( wxpeer, v );
449 [v setImplementation: c];
450 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
457 #endif // wxUSE_TEXTCTRL