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 @implementation wxNSTextField
74 static BOOL initialized = NO;
78 wxOSXCocoaClassAddWXMethods( self );
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 );
97 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
101 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
103 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
104 event.SetEventObject( wxpeer );
105 event.SetWindow( wxpeer );
106 wxpeer->HandleWindowEvent( event );
113 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
115 m_textField = (NSTextField*) w;
116 [m_textField setDelegate: w];
119 wxNSTextFieldControl::~wxNSTextFieldControl()
123 wxString wxNSTextFieldControl::GetStringValue() const
125 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
126 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
128 void wxNSTextFieldControl::SetStringValue( const wxString &str)
130 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
132 void wxNSTextFieldControl::Copy()
134 NSText* editor = [m_textField currentEditor];
141 void wxNSTextFieldControl::Cut()
143 NSText* editor = [m_textField currentEditor];
150 void wxNSTextFieldControl::Paste()
152 NSText* editor = [m_textField currentEditor];
159 bool wxNSTextFieldControl::CanPaste() const
164 void wxNSTextFieldControl::SetEditable(bool editable)
166 [m_textField setEditable:editable];
169 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
171 NSText* editor = [m_textField currentEditor];
174 NSRange range = [editor selectedRange];
175 *from = range.location;
176 *to = range.location + range.length;
180 void wxNSTextFieldControl::SetSelection( long from , long to )
182 NSText* editor = [m_textField currentEditor];
185 [editor setSelectedRange:NSMakeRange(from, to-from)];
189 void wxNSTextFieldControl::WriteText(const wxString& str)
191 // temp hack to get logging working early
192 wxString former = GetStringValue();
193 SetStringValue( former + str );
196 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
198 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
199 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
201 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
202 event.SetEventObject( wxpeer );
203 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
204 wxpeer->HandleWindowEvent( event );
212 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
221 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
222 NSTextField* v = nil;
224 if ( style & wxTE_PASSWORD )
225 v =[[wxNSSecureTextField alloc] initWithFrame:r];
227 v= [[wxNSTextField alloc] initWithFrame:r];
229 if ( style & wxNO_BORDER )
238 //[v setEditable:NO];
239 //[v setDrawsBackground:NO];
241 wxWidgetCocoaImpl* c = new wxNSTextFieldControl( wxpeer, v );
246 #endif // wxUSE_TEXTCTRL