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
169 - (void)controlTextDidChange:(NSNotification *)aNotification
173 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
175 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
176 event.SetEventObject( wxpeer );
177 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
178 wxpeer->HandleWindowEvent( event );
184 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
188 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
190 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
191 event.SetEventObject( wxpeer );
192 event.SetWindow( wxpeer );
193 wxpeer->HandleWindowEvent( event );
200 // wxNSTextViewControl
202 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
204 m_scrollView = (NSScrollView*) w;
205 [w setImplementation: this];
207 [m_scrollView setHasVerticalScroller:YES];
208 [m_scrollView setHasHorizontalScroller:NO];
209 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
210 NSSize contentSize = [m_scrollView contentSize];
212 m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
213 contentSize.width, contentSize.height)];
214 [m_textView setVerticallyResizable:YES];
215 [m_textView setHorizontallyResizable:NO];
216 [m_textView setAutoresizingMask:NSViewWidthSizable];
218 [m_scrollView setDocumentView: m_textView];
220 [m_textView setDelegate: w];
223 wxNSTextViewControl::~wxNSTextViewControl()
226 [m_textView setDelegate: nil];
229 wxString wxNSTextViewControl::GetStringValue() const
233 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
234 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
236 return wxEmptyString;
238 void wxNSTextViewControl::SetStringValue( const wxString &str)
241 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
243 void wxNSTextViewControl::Copy()
246 [m_textView copy:nil];
250 void wxNSTextViewControl::Cut()
253 [m_textView cut:nil];
256 void wxNSTextViewControl::Paste()
259 [m_textView paste:nil];
262 bool wxNSTextViewControl::CanPaste() const
267 void wxNSTextViewControl::SetEditable(bool editable)
270 [m_textView setEditable: editable];
273 void wxNSTextViewControl::GetSelection( long* from, long* to) const
277 NSRange range = [m_textView selectedRange];
278 *from = range.location;
279 *to = range.location + range.length;
283 void wxNSTextViewControl::SetSelection( long from , long to )
285 [m_textView setSelectedRange:NSMakeRange(from, to-from)];
288 void wxNSTextViewControl::WriteText(const wxString& str)
290 // temp hack to get logging working early
291 wxString former = GetStringValue();
292 SetStringValue( former + str );
295 // wxNSTextFieldControl
297 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
299 m_textField = (NSTextField*) w;
300 [m_textField setDelegate: w];
303 wxNSTextFieldControl::~wxNSTextFieldControl()
306 [m_textField setDelegate: nil];
309 wxString wxNSTextFieldControl::GetStringValue() const
311 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
312 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
314 void wxNSTextFieldControl::SetStringValue( const wxString &str)
316 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
318 void wxNSTextFieldControl::Copy()
320 NSText* editor = [m_textField currentEditor];
327 void wxNSTextFieldControl::Cut()
329 NSText* editor = [m_textField currentEditor];
336 void wxNSTextFieldControl::Paste()
338 NSText* editor = [m_textField currentEditor];
345 bool wxNSTextFieldControl::CanPaste() const
350 void wxNSTextFieldControl::SetEditable(bool editable)
352 [m_textField setEditable:editable];
355 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
357 NSText* editor = [m_textField currentEditor];
360 NSRange range = [editor selectedRange];
361 *from = range.location;
362 *to = range.location + range.length;
366 void wxNSTextFieldControl::SetSelection( long from , long to )
368 NSText* editor = [m_textField currentEditor];
371 [editor setSelectedRange:NSMakeRange(from, to-from)];
375 void wxNSTextFieldControl::WriteText(const wxString& str)
377 // temp hack to get logging working early
378 wxString former = GetStringValue();
379 SetStringValue( former + str );
382 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
384 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
385 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
387 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
388 event.SetEventObject( wxpeer );
389 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
390 wxpeer->HandleWindowEvent( event );
398 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
407 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
408 NSTextField* v = nil;
409 wxWidgetCocoaImpl* c = NULL;
411 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
413 v = [[wxNSTextView alloc] initWithFrame:r];
414 c = new wxNSTextViewControl( wxpeer, v );
415 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
419 if ( style & wxTE_PASSWORD )
420 v = [[wxNSSecureTextField alloc] initWithFrame:r];
422 v = [[wxNSTextField alloc] initWithFrame:r];
424 if ( style & wxNO_BORDER )
426 // FIXME: How can we remove the native control's border?
427 // setBordered is separate from the text ctrl's border.
433 c = new wxNSTextFieldControl( wxpeer, v );
434 [v setImplementation: c];
435 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
442 #endif // wxUSE_TEXTCTRL