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
56 @implementation wxNSSecureTextField
60 static BOOL initialized = NO;
64 wxOSXCocoaClassAddWXMethods( self );
70 @interface wxNSTextView : NSScrollView
74 @implementation wxNSTextView
78 static BOOL initialized = NO;
82 wxOSXCocoaClassAddWXMethods( self );
88 @implementation wxNSTextField
92 static BOOL initialized = NO;
96 wxOSXCocoaClassAddWXMethods( self );
101 - (void)controlTextDidChange:(NSNotification *)aNotification
105 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
107 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
108 event.SetEventObject( wxpeer );
109 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
110 wxpeer->HandleWindowEvent( event );
115 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
119 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
121 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
122 event.SetEventObject( wxpeer );
123 event.SetWindow( wxpeer );
124 wxpeer->HandleWindowEvent( event );
131 // wxNSTextViewControl
133 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
135 m_scrollView = (NSScrollView*) w;
137 [m_scrollView setHasVerticalScroller:YES];
138 [m_scrollView setHasHorizontalScroller:NO];
139 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
140 NSSize contentSize = [m_scrollView contentSize];
142 m_textView = [[NSTextView alloc] initWithFrame: NSMakeRect(0, 0,
143 contentSize.width, contentSize.height)];
144 [m_textView setVerticallyResizable:YES];
145 [m_textView setHorizontallyResizable:NO];
146 [m_textView setAutoresizingMask:NSViewWidthSizable];
148 [m_scrollView setDocumentView: m_textView];
150 [m_textView setDelegate: w];
153 wxNSTextViewControl::~wxNSTextViewControl()
156 [m_textView setDelegate: nil];
159 wxString wxNSTextViewControl::GetStringValue() const
163 wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] );
164 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
166 return wxEmptyString;
168 void wxNSTextViewControl::SetStringValue( const wxString &str)
171 [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
173 void wxNSTextViewControl::Copy()
176 [m_textView copy:nil];
180 void wxNSTextViewControl::Cut()
183 [m_textView cut:nil];
186 void wxNSTextViewControl::Paste()
189 [m_textView paste:nil];
192 bool wxNSTextViewControl::CanPaste() const
197 void wxNSTextViewControl::SetEditable(bool editable)
200 [m_textView setEditable: editable];
203 void wxNSTextViewControl::GetSelection( long* from, long* to) const
207 NSRange range = [m_textView selectedRange];
208 *from = range.location;
209 *to = range.location + range.length;
213 void wxNSTextViewControl::SetSelection( long from , long to )
215 [m_textView setSelectedRange:NSMakeRange(from, to-from)];
218 void wxNSTextViewControl::WriteText(const wxString& str)
220 // temp hack to get logging working early
221 wxString former = GetStringValue();
222 SetStringValue( former + str );
225 // wxNSTextFieldControl
227 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
229 m_textField = (NSTextField*) w;
230 [m_textField setDelegate: w];
233 wxNSTextFieldControl::~wxNSTextFieldControl()
236 [m_textField setDelegate: nil];
239 wxString wxNSTextFieldControl::GetStringValue() const
241 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
242 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
244 void wxNSTextFieldControl::SetStringValue( const wxString &str)
246 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
248 void wxNSTextFieldControl::Copy()
250 NSText* editor = [m_textField currentEditor];
257 void wxNSTextFieldControl::Cut()
259 NSText* editor = [m_textField currentEditor];
266 void wxNSTextFieldControl::Paste()
268 NSText* editor = [m_textField currentEditor];
275 bool wxNSTextFieldControl::CanPaste() const
280 void wxNSTextFieldControl::SetEditable(bool editable)
282 [m_textField setEditable:editable];
285 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
287 NSText* editor = [m_textField currentEditor];
290 NSRange range = [editor selectedRange];
291 *from = range.location;
292 *to = range.location + range.length;
296 void wxNSTextFieldControl::SetSelection( long from , long to )
298 NSText* editor = [m_textField currentEditor];
301 [editor setSelectedRange:NSMakeRange(from, to-from)];
305 void wxNSTextFieldControl::WriteText(const wxString& str)
307 // temp hack to get logging working early
308 wxString former = GetStringValue();
309 SetStringValue( former + str );
312 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
314 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
315 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
317 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
318 event.SetEventObject( wxpeer );
319 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
320 wxpeer->HandleWindowEvent( event );
328 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
337 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
338 NSTextField* v = nil;
339 wxWidgetCocoaImpl* c = NULL;
341 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
343 v = [[wxNSTextView alloc] initWithFrame:r];
344 c = new wxNSTextViewControl( wxpeer, v );
345 static_cast<wxNSTextViewControl*>(c)->SetStringValue(str);
349 if ( style & wxTE_PASSWORD )
350 v = [[wxNSSecureTextField alloc] initWithFrame:r];
352 v = [[wxNSTextField alloc] initWithFrame:r];
354 if ( style & wxNO_BORDER )
356 // FIXME: How can we remove the native control's border?
357 // setBordered is separate from the text ctrl's border.
363 c = new wxNSTextFieldControl( wxpeer, v );
364 static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str);
371 #endif // wxUSE_TEXTCTRL