]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
renaming clickedAction callbacks to more generic controlAction, textctrl updates
[wxWidgets.git] / src / osx / cocoa / textctrl.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/textctrl.mm
3 // Purpose: wxTextCtrl
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
6 // Created: 1998-01-01
7 // RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13
14 #if wxUSE_TEXTCTRL
15
16 #include "wx/textctrl.h"
17
18 #ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/dc.h"
23 #include "wx/button.h"
24 #include "wx/menu.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
28 #endif
29
30 #ifdef __DARWIN__
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #else
34 #include <stat.h>
35 #endif
36
37 #if wxUSE_STD_IOSTREAM
38 #if wxUSE_IOSTREAMH
39 #include <fstream.h>
40 #else
41 #include <fstream>
42 #endif
43 #endif
44
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
47 #include "wx/thread.h"
48
49 #include "wx/osx/private.h"
50 #include "wx/osx/cocoa/private/textimpl.h"
51
52 @interface wxNSSecureTextField : NSSecureTextField
53
54 @end
55
56 @implementation wxNSSecureTextField
57
58 + (void)initialize
59 {
60 static BOOL initialized = NO;
61 if (!initialized)
62 {
63 initialized = YES;
64 wxOSXCocoaClassAddWXMethods( self );
65 }
66 }
67
68 @end
69
70 @implementation wxNSTextField
71
72 + (void)initialize
73 {
74 static BOOL initialized = NO;
75 if (!initialized)
76 {
77 initialized = YES;
78 wxOSXCocoaClassAddWXMethods( self );
79 }
80 }
81
82 /*
83 - (void)controlTextDidChange:(NSNotification *)aNotification
84 {
85 if ( impl )
86 {
87 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
88 if ( wxpeer ) {
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 );
93 }
94 }
95 }
96
97 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
98 {
99 if ( impl )
100 {
101 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
102 if ( wxpeer ) {
103 wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId());
104 event.SetEventObject( wxpeer );
105 event.SetWindow( wxpeer );
106 wxpeer->HandleWindowEvent( event );
107 }
108 }
109 }
110 */
111 @end
112
113 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
114 {
115 m_textField = (NSTextField*) w;
116 [m_textField setDelegate: w];
117 }
118
119 wxNSTextFieldControl::~wxNSTextFieldControl()
120 {
121 }
122
123 wxString wxNSTextFieldControl::GetStringValue() const
124 {
125 wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] );
126 return cf.AsString(m_wxPeer->GetFont().GetEncoding());
127 }
128 void wxNSTextFieldControl::SetStringValue( const wxString &str)
129 {
130 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
131 }
132 void wxNSTextFieldControl::Copy()
133 {
134 NSText* editor = [m_textField currentEditor];
135 if ( editor )
136 {
137 [editor copy:nil];
138 }
139 }
140
141 void wxNSTextFieldControl::Cut()
142 {
143 NSText* editor = [m_textField currentEditor];
144 if ( editor )
145 {
146 [editor cut:nil];
147 }
148 }
149
150 void wxNSTextFieldControl::Paste()
151 {
152 NSText* editor = [m_textField currentEditor];
153 if ( editor )
154 {
155 [editor paste:nil];
156 }
157 }
158
159 bool wxNSTextFieldControl::CanPaste() const
160 {
161 return true;
162 }
163
164 void wxNSTextFieldControl::SetEditable(bool editable)
165 {
166 [m_textField setEditable:editable];
167 }
168
169 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
170 {
171 NSText* editor = [m_textField currentEditor];
172 if ( editor )
173 {
174 NSRange range = [editor selectedRange];
175 *from = range.location;
176 *to = range.location + range.length;
177 }
178 }
179
180 void wxNSTextFieldControl::SetSelection( long from , long to )
181 {
182 NSText* editor = [m_textField currentEditor];
183 if ( editor )
184 {
185 [editor setSelectedRange:NSMakeRange(from, to-from)];
186 }
187 }
188
189 void wxNSTextFieldControl::WriteText(const wxString& str)
190 {
191 // temp hack to get logging working early
192 wxString former = GetStringValue();
193 SetStringValue( former + str );
194 }
195
196 void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender)
197 {
198 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
199 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
200 {
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 );
205 }
206 }
207
208 //
209 //
210 //
211
212 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
213 wxWindowMac* parent,
214 wxWindowID id,
215 const wxString& str,
216 const wxPoint& pos,
217 const wxSize& size,
218 long style,
219 long extraStyle)
220 {
221 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
222 NSTextField* v = nil;
223
224 if ( style & wxTE_PASSWORD )
225 v =[[wxNSSecureTextField alloc] initWithFrame:r];
226 else
227 v= [[wxNSTextField alloc] initWithFrame:r];
228
229 if ( style & wxNO_BORDER )
230 {
231 [v setBezeled:NO];
232 [v setBordered:NO];
233 }
234
235 [v setBezeled:NO];
236 [v setBordered:NO];
237 //[v setBezeled:NO];
238 //[v setEditable:NO];
239 //[v setDrawsBackground:NO];
240
241 wxWidgetCocoaImpl* c = new wxNSTextFieldControl( wxpeer, v );
242 return c;
243 }
244
245
246 #endif // wxUSE_TEXTCTRL