]>
Commit | Line | Data |
---|---|---|
dbeddfb9 SC |
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" | |
1e181c7a | 50 | #include "wx/osx/cocoa/private/textimpl.h" |
dbeddfb9 | 51 | |
e32090ba | 52 | @interface wxNSSecureTextField : NSSecureTextField |
4d23a0d3 KO |
53 | { |
54 | wxWidgetCocoaImpl* impl; | |
55 | } | |
e32090ba | 56 | |
4d23a0d3 KO |
57 | - (void) setImplementation:(wxWidgetCocoaImpl*) item; |
58 | - (wxWidgetCocoaImpl*) implementation; | |
e32090ba SC |
59 | @end |
60 | ||
61 | @implementation wxNSSecureTextField | |
dbeddfb9 | 62 | |
4dd9fdf8 SC |
63 | + (void)initialize |
64 | { | |
65 | static BOOL initialized = NO; | |
66 | if (!initialized) | |
67 | { | |
68 | initialized = YES; | |
69 | wxOSXCocoaClassAddWXMethods( self ); | |
70 | } | |
71 | } | |
dbeddfb9 | 72 | |
4d23a0d3 KO |
73 | - (wxWidgetCocoaImpl*) implementation |
74 | { | |
75 | return impl; | |
76 | } | |
77 | ||
78 | - (void) setImplementation:(wxWidgetCocoaImpl*) item | |
79 | { | |
80 | impl = item; | |
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 | ||
e32090ba SC |
97 | @end |
98 | ||
c824c165 | 99 | @interface wxNSTextView : NSScrollView |
4d23a0d3 KO |
100 | { |
101 | wxWidgetCocoaImpl* impl; | |
102 | } | |
c824c165 | 103 | |
4d23a0d3 KO |
104 | - (void) setImplementation:(wxWidgetCocoaImpl*) item; |
105 | - (wxWidgetCocoaImpl*) implementation; | |
c824c165 KO |
106 | @end |
107 | ||
108 | @implementation wxNSTextView | |
109 | ||
110 | + (void)initialize | |
111 | { | |
112 | static BOOL initialized = NO; | |
113 | if (!initialized) | |
114 | { | |
115 | initialized = YES; | |
116 | wxOSXCocoaClassAddWXMethods( self ); | |
117 | } | |
118 | } | |
119 | ||
4d23a0d3 KO |
120 | - (wxWidgetCocoaImpl*) implementation |
121 | { | |
122 | return impl; | |
123 | } | |
124 | ||
125 | - (void) setImplementation:(wxWidgetCocoaImpl*) item | |
126 | { | |
127 | impl = item; | |
128 | } | |
129 | ||
130 | ||
131 | - (void)controlTextDidChange:(NSNotification *)aNotification | |
132 | { | |
133 | if ( impl ) | |
134 | { | |
135 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
136 | if ( wxpeer ) { | |
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 ); | |
141 | } | |
142 | } | |
143 | } | |
c824c165 KO |
144 | @end |
145 | ||
e32090ba | 146 | @implementation wxNSTextField |
ffad7b0d | 147 | |
e32090ba | 148 | + (void)initialize |
dbeddfb9 | 149 | { |
e32090ba SC |
150 | static BOOL initialized = NO; |
151 | if (!initialized) | |
152 | { | |
153 | initialized = YES; | |
154 | wxOSXCocoaClassAddWXMethods( self ); | |
155 | } | |
dbeddfb9 | 156 | } |
e32090ba | 157 | |
4d23a0d3 KO |
158 | - (wxWidgetCocoaImpl*) implementation |
159 | { | |
160 | return impl; | |
161 | } | |
162 | ||
163 | - (void) setImplementation:(wxWidgetCocoaImpl*) item | |
164 | { | |
165 | impl = item; | |
166 | } | |
167 | ||
168 | ||
ffad7b0d SC |
169 | - (void)controlTextDidChange:(NSNotification *)aNotification |
170 | { | |
171 | if ( impl ) | |
172 | { | |
173 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
174 | if ( wxpeer ) { | |
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 ); | |
179 | } | |
180 | } | |
181 | } | |
dbeddfb9 | 182 | |
4d23a0d3 | 183 | /* |
ffad7b0d SC |
184 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
185 | { | |
186 | if ( impl ) | |
187 | { | |
188 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
189 | if ( wxpeer ) { | |
190 | wxFocusEvent event(wxEVT_KILL_FOCUS, wxpeer->GetId()); | |
191 | event.SetEventObject( wxpeer ); | |
192 | event.SetWindow( wxpeer ); | |
193 | wxpeer->HandleWindowEvent( event ); | |
194 | } | |
195 | } | |
196 | } | |
ffad7b0d | 197 | */ |
dbeddfb9 SC |
198 | @end |
199 | ||
c824c165 KO |
200 | // wxNSTextViewControl |
201 | ||
202 | wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w) | |
203 | { | |
204 | m_scrollView = (NSScrollView*) w; | |
4d23a0d3 | 205 | [w setImplementation: this]; |
c824c165 KO |
206 | |
207 | [m_scrollView setHasVerticalScroller:YES]; | |
208 | [m_scrollView setHasHorizontalScroller:NO]; | |
209 | [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
210 | NSSize contentSize = [m_scrollView contentSize]; | |
211 | ||
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]; | |
217 | ||
218 | [m_scrollView setDocumentView: m_textView]; | |
219 | ||
220 | [m_textView setDelegate: w]; | |
221 | } | |
222 | ||
223 | wxNSTextViewControl::~wxNSTextViewControl() | |
224 | { | |
225 | if (m_textView) | |
226 | [m_textView setDelegate: nil]; | |
227 | } | |
228 | ||
229 | wxString wxNSTextViewControl::GetStringValue() const | |
230 | { | |
231 | if (m_textView) | |
232 | { | |
233 | wxCFStringRef cf( (CFStringRef) [[m_textView string] retain] ); | |
234 | return cf.AsString(m_wxPeer->GetFont().GetEncoding()); | |
235 | } | |
236 | return wxEmptyString; | |
237 | } | |
238 | void wxNSTextViewControl::SetStringValue( const wxString &str) | |
239 | { | |
240 | if (m_textView) | |
241 | [m_textView setString: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; | |
242 | } | |
243 | void wxNSTextViewControl::Copy() | |
244 | { | |
245 | if (m_textView) | |
246 | [m_textView copy:nil]; | |
247 | ||
248 | } | |
249 | ||
250 | void wxNSTextViewControl::Cut() | |
251 | { | |
252 | if (m_textView) | |
253 | [m_textView cut:nil]; | |
254 | } | |
255 | ||
256 | void wxNSTextViewControl::Paste() | |
257 | { | |
258 | if (m_textView) | |
259 | [m_textView paste:nil]; | |
260 | } | |
261 | ||
262 | bool wxNSTextViewControl::CanPaste() const | |
263 | { | |
264 | return true; | |
265 | } | |
266 | ||
267 | void wxNSTextViewControl::SetEditable(bool editable) | |
268 | { | |
269 | if (m_textView) | |
270 | [m_textView setEditable: editable]; | |
271 | } | |
272 | ||
273 | void wxNSTextViewControl::GetSelection( long* from, long* to) const | |
274 | { | |
275 | if (m_textView) | |
276 | { | |
277 | NSRange range = [m_textView selectedRange]; | |
278 | *from = range.location; | |
279 | *to = range.location + range.length; | |
280 | } | |
281 | } | |
282 | ||
283 | void wxNSTextViewControl::SetSelection( long from , long to ) | |
284 | { | |
285 | [m_textView setSelectedRange:NSMakeRange(from, to-from)]; | |
286 | } | |
287 | ||
288 | void wxNSTextViewControl::WriteText(const wxString& str) | |
289 | { | |
290 | // temp hack to get logging working early | |
291 | wxString former = GetStringValue(); | |
292 | SetStringValue( former + str ); | |
293 | } | |
294 | ||
295 | // wxNSTextFieldControl | |
296 | ||
1e181c7a SC |
297 | wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w) |
298 | { | |
e32090ba SC |
299 | m_textField = (NSTextField*) w; |
300 | [m_textField setDelegate: w]; | |
1e181c7a SC |
301 | } |
302 | ||
303 | wxNSTextFieldControl::~wxNSTextFieldControl() | |
304 | { | |
c824c165 KO |
305 | if (m_textField) |
306 | [m_textField setDelegate: nil]; | |
1e181c7a SC |
307 | } |
308 | ||
309 | wxString wxNSTextFieldControl::GetStringValue() const | |
310 | { | |
e32090ba | 311 | wxCFStringRef cf( (CFStringRef) [[m_textField stringValue] retain] ); |
1e181c7a SC |
312 | return cf.AsString(m_wxPeer->GetFont().GetEncoding()); |
313 | } | |
314 | void wxNSTextFieldControl::SetStringValue( const wxString &str) | |
315 | { | |
e32090ba | 316 | [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
1e181c7a SC |
317 | } |
318 | void wxNSTextFieldControl::Copy() | |
319 | { | |
e32090ba SC |
320 | NSText* editor = [m_textField currentEditor]; |
321 | if ( editor ) | |
322 | { | |
323 | [editor copy:nil]; | |
324 | } | |
1e181c7a SC |
325 | } |
326 | ||
327 | void wxNSTextFieldControl::Cut() | |
328 | { | |
e32090ba SC |
329 | NSText* editor = [m_textField currentEditor]; |
330 | if ( editor ) | |
331 | { | |
332 | [editor cut:nil]; | |
333 | } | |
1e181c7a SC |
334 | } |
335 | ||
336 | void wxNSTextFieldControl::Paste() | |
337 | { | |
e32090ba SC |
338 | NSText* editor = [m_textField currentEditor]; |
339 | if ( editor ) | |
340 | { | |
341 | [editor paste:nil]; | |
342 | } | |
1e181c7a SC |
343 | } |
344 | ||
345 | bool wxNSTextFieldControl::CanPaste() const | |
346 | { | |
e32090ba | 347 | return true; |
1e181c7a SC |
348 | } |
349 | ||
350 | void wxNSTextFieldControl::SetEditable(bool editable) | |
351 | { | |
e32090ba | 352 | [m_textField setEditable:editable]; |
1e181c7a SC |
353 | } |
354 | ||
355 | void wxNSTextFieldControl::GetSelection( long* from, long* to) const | |
356 | { | |
e32090ba SC |
357 | NSText* editor = [m_textField currentEditor]; |
358 | if ( editor ) | |
359 | { | |
360 | NSRange range = [editor selectedRange]; | |
361 | *from = range.location; | |
362 | *to = range.location + range.length; | |
363 | } | |
1e181c7a SC |
364 | } |
365 | ||
366 | void wxNSTextFieldControl::SetSelection( long from , long to ) | |
367 | { | |
e32090ba SC |
368 | NSText* editor = [m_textField currentEditor]; |
369 | if ( editor ) | |
370 | { | |
371 | [editor setSelectedRange:NSMakeRange(from, to-from)]; | |
372 | } | |
1e181c7a SC |
373 | } |
374 | ||
375 | void wxNSTextFieldControl::WriteText(const wxString& str) | |
376 | { | |
377 | // temp hack to get logging working early | |
378 | wxString former = GetStringValue(); | |
379 | SetStringValue( former + str ); | |
380 | } | |
dbeddfb9 | 381 | |
e32090ba SC |
382 | void wxNSTextFieldControl::controlAction(WXWidget slf, void* _cmd, void *sender) |
383 | { | |
384 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
385 | if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) | |
386 | { | |
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 ); | |
391 | } | |
392 | } | |
393 | ||
394 | // | |
395 | // | |
396 | // | |
397 | ||
dbeddfb9 SC |
398 | wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, |
399 | wxWindowMac* parent, | |
400 | wxWindowID id, | |
401 | const wxString& str, | |
402 | const wxPoint& pos, | |
403 | const wxSize& size, | |
404 | long style, | |
405 | long extraStyle) | |
406 | { | |
dbeddfb9 | 407 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
e32090ba | 408 | NSTextField* v = nil; |
c824c165 | 409 | wxWidgetCocoaImpl* c = NULL; |
e32090ba | 410 | |
c824c165 | 411 | if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 ) |
b15f9375 | 412 | { |
c824c165 KO |
413 | v = [[wxNSTextView alloc] initWithFrame:r]; |
414 | c = new wxNSTextViewControl( wxpeer, v ); | |
415 | static_cast<wxNSTextViewControl*>(c)->SetStringValue(str); | |
416 | } | |
417 | else | |
418 | { | |
419 | if ( style & wxTE_PASSWORD ) | |
420 | v = [[wxNSSecureTextField alloc] initWithFrame:r]; | |
421 | else | |
422 | v = [[wxNSTextField alloc] initWithFrame:r]; | |
423 | ||
424 | if ( style & wxNO_BORDER ) | |
425 | { | |
426 | // FIXME: How can we remove the native control's border? | |
427 | // setBordered is separate from the text ctrl's border. | |
428 | } | |
429 | ||
b15f9375 SC |
430 | [v setBezeled:NO]; |
431 | [v setBordered:NO]; | |
c824c165 KO |
432 | |
433 | c = new wxNSTextFieldControl( wxpeer, v ); | |
4d23a0d3 | 434 | [v setImplementation: c]; |
c824c165 | 435 | static_cast<wxNSTextFieldControl*>(c)->SetStringValue(str); |
b15f9375 | 436 | } |
dbeddfb9 | 437 | |
dbeddfb9 SC |
438 | return c; |
439 | } | |
440 | ||
441 | ||
442 | #endif // wxUSE_TEXTCTRL |