]>
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 | |
03647350 | 52 | @interface NSView(EditableView) |
5f65ba36 SC |
53 | - (BOOL)isEditable; |
54 | - (void)setEditable:(BOOL)flag; | |
55 | @end | |
56 | ||
57 | class wxMacEditHelper | |
58 | { | |
59 | public : | |
60 | wxMacEditHelper( NSView* textView ) | |
61 | { | |
63bcc669 SC |
62 | m_textView = textView; |
63 | m_formerState = YES; | |
5f65ba36 SC |
64 | if ( textView ) |
65 | { | |
66 | m_formerState = [textView isEditable]; | |
67 | [textView setEditable:YES]; | |
68 | } | |
69 | } | |
70 | ||
71 | ~wxMacEditHelper() | |
72 | { | |
73 | if ( m_textView ) | |
74 | [m_textView setEditable:m_formerState]; | |
75 | } | |
76 | ||
77 | protected : | |
78 | BOOL m_formerState ; | |
79 | NSView* m_textView; | |
80 | } ; | |
81 | ||
03647350 | 82 | @implementation wxNSSecureTextField |
dbeddfb9 | 83 | |
4dd9fdf8 SC |
84 | + (void)initialize |
85 | { | |
86 | static BOOL initialized = NO; | |
03647350 | 87 | if (!initialized) |
4dd9fdf8 SC |
88 | { |
89 | initialized = YES; | |
90 | wxOSXCocoaClassAddWXMethods( self ); | |
91 | } | |
92 | } | |
dbeddfb9 | 93 | |
4d23a0d3 KO |
94 | - (void)controlTextDidChange:(NSNotification *)aNotification |
95 | { | |
6331c8c0 SC |
96 | wxUnusedVar(aNotification); |
97 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
4d23a0d3 KO |
98 | if ( impl ) |
99 | { | |
100 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
101 | if ( wxpeer ) { | |
102 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId()); | |
103 | event.SetEventObject( wxpeer ); | |
104 | event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() ); | |
105 | wxpeer->HandleWindowEvent( event ); | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
6d109846 SC |
110 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
111 | { | |
112 | wxUnusedVar(aNotification); | |
113 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
114 | if ( impl ) | |
115 | { | |
116 | impl->DoNotifyFocusEvent( false, NULL ); | |
117 | } | |
118 | } | |
119 | ||
e32090ba SC |
120 | @end |
121 | ||
1087c6c1 | 122 | @interface wxNSTextScrollView : NSScrollView |
4d23a0d3 | 123 | { |
4d23a0d3 | 124 | } |
1087c6c1 SC |
125 | @end |
126 | ||
1087c6c1 | 127 | @implementation wxNSTextScrollView |
c824c165 KO |
128 | |
129 | + (void)initialize | |
130 | { | |
131 | static BOOL initialized = NO; | |
03647350 VZ |
132 | if (!initialized) |
133 | { | |
c824c165 KO |
134 | initialized = YES; |
135 | wxOSXCocoaClassAddWXMethods( self ); | |
136 | } | |
137 | } | |
138 | ||
7cb2a241 SC |
139 | @end |
140 | ||
141 | @implementation wxNSTextFieldEditor | |
142 | ||
143 | - (void) keyDown:(NSEvent*) event | |
4d23a0d3 | 144 | { |
9e55f38d | 145 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
146 | lastKeyDownEvent = event; |
147 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) | |
148 | [super keyDown:event]; | |
149 | lastKeyDownEvent = nil; | |
4d23a0d3 | 150 | } |
533e2ae1 | 151 | |
7cb2a241 | 152 | - (void) keyUp:(NSEvent*) event |
533e2ae1 | 153 | { |
9e55f38d | 154 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
155 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) |
156 | [super keyUp:event]; | |
533e2ae1 | 157 | } |
1087c6c1 | 158 | |
7cb2a241 | 159 | - (void) flagsChanged:(NSEvent*) event |
1087c6c1 | 160 | { |
9e55f38d | 161 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
162 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) |
163 | [super flagsChanged:event]; | |
1087c6c1 | 164 | } |
1087c6c1 | 165 | |
7cb2a241 SC |
166 | - (BOOL) performKeyEquivalent:(NSEvent*) event |
167 | { | |
168 | BOOL retval = [super performKeyEquivalent:event]; | |
169 | return retval; | |
170 | } | |
1087c6c1 | 171 | |
7cb2a241 | 172 | - (void) insertText:(id) str |
1087c6c1 | 173 | { |
9e55f38d | 174 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 | 175 | if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) ) |
1087c6c1 | 176 | { |
7cb2a241 | 177 | [super insertText:str]; |
1087c6c1 | 178 | } |
1087c6c1 SC |
179 | } |
180 | ||
7cb2a241 SC |
181 | @end |
182 | ||
183 | @implementation wxNSTextView | |
1087c6c1 | 184 | |
7cb2a241 | 185 | + (void)initialize |
1087c6c1 | 186 | { |
7cb2a241 | 187 | static BOOL initialized = NO; |
03647350 | 188 | if (!initialized) |
7cb2a241 SC |
189 | { |
190 | initialized = YES; | |
191 | wxOSXCocoaClassAddWXMethods( self ); | |
192 | } | |
1087c6c1 SC |
193 | } |
194 | ||
c824c165 KO |
195 | @end |
196 | ||
e32090ba | 197 | @implementation wxNSTextField |
ffad7b0d | 198 | |
e32090ba | 199 | + (void)initialize |
dbeddfb9 | 200 | { |
e32090ba | 201 | static BOOL initialized = NO; |
03647350 | 202 | if (!initialized) |
e32090ba SC |
203 | { |
204 | initialized = YES; | |
205 | wxOSXCocoaClassAddWXMethods( self ); | |
206 | } | |
dbeddfb9 | 207 | } |
e32090ba | 208 | |
7cb2a241 SC |
209 | - (id) initWithFrame:(NSRect) frame |
210 | { | |
211 | self = [super initWithFrame:frame]; | |
212 | fieldEditor = nil; | |
213 | return self; | |
214 | } | |
215 | ||
af665c2d SC |
216 | - (void) dealloc |
217 | { | |
218 | [fieldEditor release]; | |
219 | [super dealloc]; | |
220 | } | |
221 | ||
7cb2a241 SC |
222 | - (void) setFieldEditor:(wxNSTextFieldEditor*) editor |
223 | { | |
224 | fieldEditor = editor; | |
225 | } | |
226 | ||
227 | - (wxNSTextFieldEditor*) fieldEditor | |
228 | { | |
229 | return fieldEditor; | |
230 | } | |
231 | ||
232 | ||
c3e433b1 SC |
233 | - (void) setEnabled:(BOOL) flag |
234 | { | |
235 | [super setEnabled: flag]; | |
236 | ||
237 | if (![self drawsBackground]) { | |
238 | // Static text is drawn incorrectly when disabled. | |
239 | // For an explanation, see | |
240 | // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028 | |
241 | if (flag) { | |
242 | [self setTextColor: [NSColor controlTextColor]]; | |
243 | } else { | |
244 | [self setTextColor: [NSColor secondarySelectedControlColor]]; | |
245 | } | |
246 | } | |
247 | } | |
4d23a0d3 | 248 | |
ffad7b0d SC |
249 | - (void)controlTextDidChange:(NSNotification *)aNotification |
250 | { | |
6331c8c0 SC |
251 | wxUnusedVar(aNotification); |
252 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
ffad7b0d SC |
253 | if ( impl ) |
254 | { | |
255 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
256 | if ( wxpeer ) { | |
257 | wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId()); | |
258 | event.SetEventObject( wxpeer ); | |
259 | event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() ); | |
260 | wxpeer->HandleWindowEvent( event ); | |
261 | } | |
262 | } | |
263 | } | |
dbeddfb9 | 264 | |
2d6aa919 KO |
265 | typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector); |
266 | ||
267 | - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector | |
268 | { | |
6331c8c0 SC |
269 | wxUnusedVar(textView); |
270 | wxUnusedVar(control); | |
2d6aa919 KO |
271 | if (commandSelector == @selector(insertNewline:)) |
272 | { | |
6331c8c0 | 273 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); |
2d6aa919 KO |
274 | if ( impl ) |
275 | { | |
276 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
03647350 | 277 | if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER ) |
2d6aa919 KO |
278 | { |
279 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); | |
280 | event.SetEventObject( wxpeer ); | |
281 | event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() ); | |
282 | wxpeer->HandleWindowEvent( event ); | |
283 | } | |
284 | } | |
285 | } | |
03647350 | 286 | |
2d6aa919 KO |
287 | return NO; |
288 | } | |
6d109846 | 289 | |
ffad7b0d SC |
290 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
291 | { | |
6d109846 SC |
292 | wxUnusedVar(aNotification); |
293 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
ffad7b0d SC |
294 | if ( impl ) |
295 | { | |
6d109846 | 296 | impl->DoNotifyFocusEvent( false, NULL ); |
ffad7b0d SC |
297 | } |
298 | } | |
dbeddfb9 SC |
299 | @end |
300 | ||
c824c165 KO |
301 | // wxNSTextViewControl |
302 | ||
303 | wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w) | |
304 | { | |
1087c6c1 SC |
305 | wxNSTextScrollView* sv = (wxNSTextScrollView*) w; |
306 | m_scrollView = sv; | |
03647350 | 307 | |
c824c165 KO |
308 | [m_scrollView setHasVerticalScroller:YES]; |
309 | [m_scrollView setHasHorizontalScroller:NO]; | |
310 | [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
311 | NSSize contentSize = [m_scrollView contentSize]; | |
03647350 | 312 | |
1087c6c1 | 313 | wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0, |
c824c165 | 314 | contentSize.width, contentSize.height)]; |
1087c6c1 SC |
315 | m_textView = tv; |
316 | [tv setVerticallyResizable:YES]; | |
317 | [tv setHorizontallyResizable:NO]; | |
318 | [tv setAutoresizingMask:NSViewWidthSizable]; | |
03647350 | 319 | |
1087c6c1 | 320 | [m_scrollView setDocumentView: tv]; |
c824c165 | 321 | |
1087c6c1 | 322 | [tv setDelegate: w]; |
03647350 VZ |
323 | |
324 | InstallEventHandler(tv); | |
c824c165 KO |
325 | } |
326 | ||
327 | wxNSTextViewControl::~wxNSTextViewControl() | |
328 | { | |
329 | if (m_textView) | |
330 | [m_textView setDelegate: nil]; | |
331 | } | |
332 | ||
03647350 | 333 | wxString wxNSTextViewControl::GetStringValue() const |
c824c165 | 334 | { |
03647350 | 335 | if (m_textView) |
c824c165 | 336 | { |
f66ecdc4 | 337 | wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding()); |
50b5e38d SC |
338 | wxMacConvertNewlines13To10( &result ) ; |
339 | return result; | |
c824c165 KO |
340 | } |
341 | return wxEmptyString; | |
342 | } | |
03647350 | 343 | void wxNSTextViewControl::SetStringValue( const wxString &str) |
c824c165 | 344 | { |
50b5e38d SC |
345 | wxString st = str; |
346 | wxMacConvertNewlines10To13( &st ); | |
5f65ba36 | 347 | wxMacEditHelper helper(m_textView); |
50b5e38d | 348 | |
c824c165 | 349 | if (m_textView) |
50b5e38d | 350 | [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
c824c165 | 351 | } |
7cb2a241 | 352 | |
03647350 | 353 | void wxNSTextViewControl::Copy() |
c824c165 KO |
354 | { |
355 | if (m_textView) | |
356 | [m_textView copy:nil]; | |
357 | ||
358 | } | |
359 | ||
03647350 | 360 | void wxNSTextViewControl::Cut() |
c824c165 KO |
361 | { |
362 | if (m_textView) | |
363 | [m_textView cut:nil]; | |
364 | } | |
365 | ||
03647350 | 366 | void wxNSTextViewControl::Paste() |
c824c165 KO |
367 | { |
368 | if (m_textView) | |
369 | [m_textView paste:nil]; | |
370 | } | |
371 | ||
03647350 VZ |
372 | bool wxNSTextViewControl::CanPaste() const |
373 | { | |
c824c165 KO |
374 | return true; |
375 | } | |
376 | ||
03647350 | 377 | void wxNSTextViewControl::SetEditable(bool editable) |
c824c165 KO |
378 | { |
379 | if (m_textView) | |
380 | [m_textView setEditable: editable]; | |
381 | } | |
382 | ||
03647350 | 383 | void wxNSTextViewControl::GetSelection( long* from, long* to) const |
c824c165 KO |
384 | { |
385 | if (m_textView) | |
386 | { | |
387 | NSRange range = [m_textView selectedRange]; | |
388 | *from = range.location; | |
389 | *to = range.location + range.length; | |
390 | } | |
391 | } | |
392 | ||
393 | void wxNSTextViewControl::SetSelection( long from , long to ) | |
394 | { | |
50b5e38d SC |
395 | long textLength = [[m_textView string] length]; |
396 | if ((from == -1) && (to == -1)) | |
397 | { | |
398 | from = 0 ; | |
399 | to = textLength ; | |
400 | } | |
401 | else | |
402 | { | |
403 | from = wxMin(textLength,wxMax(from,0)) ; | |
404 | if ( to == -1 ) | |
405 | to = textLength; | |
406 | else | |
407 | to = wxMax(0,wxMin(textLength,to)) ; | |
408 | } | |
409 | ||
2d6aa919 KO |
410 | NSRange selrange = NSMakeRange(from, to-from); |
411 | [m_textView setSelectedRange:selrange]; | |
412 | [m_textView scrollRangeToVisible:selrange]; | |
c824c165 KO |
413 | } |
414 | ||
03647350 | 415 | void wxNSTextViewControl::WriteText(const wxString& str) |
c824c165 | 416 | { |
50b5e38d SC |
417 | wxString st = str; |
418 | wxMacConvertNewlines10To13( &st ); | |
5f65ba36 | 419 | wxMacEditHelper helper(m_textView); |
715824d5 SC |
420 | NSEvent* formerEvent = m_lastKeyDownEvent; |
421 | m_lastKeyDownEvent = nil; | |
50b5e38d | 422 | [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
715824d5 | 423 | m_lastKeyDownEvent = formerEvent; |
c824c165 KO |
424 | } |
425 | ||
63bcc669 | 426 | void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) ) |
f95dd972 SC |
427 | { |
428 | if ([m_textView respondsToSelector:@selector(setFont:)]) | |
429 | [m_textView setFont: font.OSXGetNSFont()]; | |
430 | } | |
431 | ||
16671f22 KO |
432 | bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style) |
433 | { | |
434 | if (m_textView) { | |
435 | NSTextStorage* storage = [m_textView textStorage]; | |
436 | NSFont* font = [storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL]; | |
437 | if (font) | |
438 | style.SetFont(wxFont(font)); | |
439 | NSColor* bgcolor = [storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL]; | |
440 | if (bgcolor) | |
441 | style.SetBackgroundColour(wxColour(bgcolor)); | |
442 | NSColor* fgcolor = [storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL]; | |
443 | style.SetTextColour(wxColour(fgcolor)); | |
444 | return true; | |
445 | } | |
446 | ||
447 | return false; | |
448 | } | |
449 | ||
450 | void wxNSTextViewControl::SetStyle(long start, | |
451 | long end, | |
452 | const wxTextAttr& style) | |
453 | { | |
454 | if (m_textView) { | |
455 | NSRange range = NSMakeRange(start, end-start); | |
456 | NSTextStorage* storage = [m_textView textStorage]; | |
457 | ||
458 | wxFont font = style.GetFont(); | |
459 | if (font.IsOk()) | |
460 | [storage addAttribute:NSFontAttributeName value:style.GetFont().OSXGetNSFont() range:range]; | |
461 | ||
462 | wxColour bgcolor = style.GetBackgroundColour(); | |
463 | if (bgcolor.IsOk()) | |
464 | [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range]; | |
465 | ||
466 | wxColour fgcolor = style.GetTextColour(); | |
467 | if (fgcolor.IsOk()) | |
468 | [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range]; | |
469 | } | |
470 | } | |
f95dd972 | 471 | |
c824c165 KO |
472 | // wxNSTextFieldControl |
473 | ||
1e181c7a SC |
474 | wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w) |
475 | { | |
e32090ba SC |
476 | m_textField = (NSTextField*) w; |
477 | [m_textField setDelegate: w]; | |
50b5e38d | 478 | m_selStart = m_selEnd = 0; |
7cb2a241 | 479 | m_hasEditor = [w isKindOfClass:[NSTextField class]]; |
1e181c7a SC |
480 | } |
481 | ||
482 | wxNSTextFieldControl::~wxNSTextFieldControl() | |
483 | { | |
c824c165 KO |
484 | if (m_textField) |
485 | [m_textField setDelegate: nil]; | |
1e181c7a SC |
486 | } |
487 | ||
03647350 | 488 | wxString wxNSTextFieldControl::GetStringValue() const |
1e181c7a | 489 | { |
f66ecdc4 | 490 | return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding()); |
1e181c7a | 491 | } |
50b5e38d | 492 | |
03647350 | 493 | void wxNSTextFieldControl::SetStringValue( const wxString &str) |
1e181c7a | 494 | { |
5f65ba36 | 495 | wxMacEditHelper helper(m_textField); |
e32090ba | 496 | [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
1e181c7a | 497 | } |
50b5e38d | 498 | |
03647350 | 499 | void wxNSTextFieldControl::Copy() |
1e181c7a | 500 | { |
e32090ba SC |
501 | NSText* editor = [m_textField currentEditor]; |
502 | if ( editor ) | |
503 | { | |
504 | [editor copy:nil]; | |
505 | } | |
1e181c7a SC |
506 | } |
507 | ||
03647350 | 508 | void wxNSTextFieldControl::Cut() |
1e181c7a | 509 | { |
e32090ba SC |
510 | NSText* editor = [m_textField currentEditor]; |
511 | if ( editor ) | |
512 | { | |
513 | [editor cut:nil]; | |
514 | } | |
1e181c7a SC |
515 | } |
516 | ||
03647350 | 517 | void wxNSTextFieldControl::Paste() |
1e181c7a | 518 | { |
e32090ba SC |
519 | NSText* editor = [m_textField currentEditor]; |
520 | if ( editor ) | |
521 | { | |
522 | [editor paste:nil]; | |
523 | } | |
1e181c7a SC |
524 | } |
525 | ||
03647350 VZ |
526 | bool wxNSTextFieldControl::CanPaste() const |
527 | { | |
e32090ba | 528 | return true; |
1e181c7a SC |
529 | } |
530 | ||
03647350 | 531 | void wxNSTextFieldControl::SetEditable(bool editable) |
1e181c7a | 532 | { |
e32090ba | 533 | [m_textField setEditable:editable]; |
1e181c7a SC |
534 | } |
535 | ||
03647350 | 536 | void wxNSTextFieldControl::GetSelection( long* from, long* to) const |
1e181c7a | 537 | { |
e32090ba SC |
538 | NSText* editor = [m_textField currentEditor]; |
539 | if ( editor ) | |
540 | { | |
541 | NSRange range = [editor selectedRange]; | |
542 | *from = range.location; | |
543 | *to = range.location + range.length; | |
544 | } | |
50b5e38d SC |
545 | else |
546 | { | |
547 | *from = m_selStart; | |
548 | *to = m_selEnd; | |
549 | } | |
1e181c7a SC |
550 | } |
551 | ||
552 | void wxNSTextFieldControl::SetSelection( long from , long to ) | |
553 | { | |
50b5e38d SC |
554 | long textLength = [[m_textField stringValue] length]; |
555 | if ((from == -1) && (to == -1)) | |
556 | { | |
557 | from = 0 ; | |
558 | to = textLength ; | |
559 | } | |
560 | else | |
561 | { | |
562 | from = wxMin(textLength,wxMax(from,0)) ; | |
563 | if ( to == -1 ) | |
564 | to = textLength; | |
565 | else | |
566 | to = wxMax(0,wxMin(textLength,to)) ; | |
567 | } | |
568 | ||
e32090ba SC |
569 | NSText* editor = [m_textField currentEditor]; |
570 | if ( editor ) | |
571 | { | |
572 | [editor setSelectedRange:NSMakeRange(from, to-from)]; | |
573 | } | |
50b5e38d SC |
574 | else |
575 | { | |
576 | m_selStart = from; | |
577 | m_selEnd = to; | |
578 | } | |
1e181c7a SC |
579 | } |
580 | ||
03647350 | 581 | void wxNSTextFieldControl::WriteText(const wxString& str) |
1e181c7a | 582 | { |
715824d5 SC |
583 | NSEvent* formerEvent = m_lastKeyDownEvent; |
584 | m_lastKeyDownEvent = nil; | |
50b5e38d SC |
585 | NSText* editor = [m_textField currentEditor]; |
586 | if ( editor ) | |
587 | { | |
5f65ba36 | 588 | wxMacEditHelper helper(m_textField); |
50b5e38d SC |
589 | [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
590 | } | |
591 | else | |
592 | { | |
593 | wxString val = GetStringValue() ; | |
594 | long start , end ; | |
595 | GetSelection( &start , &end ) ; | |
596 | val.Remove( start , end - start ) ; | |
597 | val.insert( start , str ) ; | |
598 | SetStringValue( val ) ; | |
599 | SetSelection( start + str.length() , start + str.length() ) ; | |
600 | } | |
715824d5 | 601 | m_lastKeyDownEvent = formerEvent; |
1e181c7a | 602 | } |
dbeddfb9 | 603 | |
03647350 | 604 | void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), |
6331c8c0 | 605 | void* WXUNUSED(_cmd), void *WXUNUSED(sender)) |
e32090ba SC |
606 | { |
607 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
03647350 | 608 | if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) |
e32090ba SC |
609 | { |
610 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); | |
611 | event.SetEventObject( wxpeer ); | |
612 | event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() ); | |
613 | wxpeer->HandleWindowEvent( event ); | |
614 | } | |
615 | } | |
616 | ||
617 | // | |
618 | // | |
619 | // | |
620 | ||
03647350 VZ |
621 | wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, |
622 | wxWindowMac* WXUNUSED(parent), | |
623 | wxWindowID WXUNUSED(id), | |
63bcc669 | 624 | const wxString& WXUNUSED(str), |
03647350 | 625 | const wxPoint& pos, |
dbeddfb9 | 626 | const wxSize& size, |
03647350 | 627 | long style, |
6331c8c0 | 628 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 629 | { |
dbeddfb9 | 630 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
c824c165 | 631 | wxWidgetCocoaImpl* c = NULL; |
03647350 | 632 | |
c824c165 | 633 | if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 ) |
b15f9375 | 634 | { |
1087c6c1 SC |
635 | wxNSTextScrollView* v = nil; |
636 | v = [[wxNSTextScrollView alloc] initWithFrame:r]; | |
c824c165 | 637 | c = new wxNSTextViewControl( wxpeer, v ); |
c824c165 | 638 | } |
03647350 | 639 | else |
c824c165 | 640 | { |
6331c8c0 | 641 | NSTextField* v = nil; |
c824c165 KO |
642 | if ( style & wxTE_PASSWORD ) |
643 | v = [[wxNSSecureTextField alloc] initWithFrame:r]; | |
644 | else | |
645 | v = [[wxNSTextField alloc] initWithFrame:r]; | |
03647350 | 646 | |
c824c165 KO |
647 | if ( style & wxNO_BORDER ) |
648 | { | |
649 | // FIXME: How can we remove the native control's border? | |
650 | // setBordered is separate from the text ctrl's border. | |
651 | } | |
03647350 | 652 | |
b15f9375 SC |
653 | [v setBezeled:NO]; |
654 | [v setBordered:NO]; | |
03647350 | 655 | |
c824c165 | 656 | c = new wxNSTextFieldControl( wxpeer, v ); |
b15f9375 | 657 | } |
03647350 | 658 | |
dbeddfb9 SC |
659 | return c; |
660 | } | |
661 | ||
662 | ||
663 | #endif // wxUSE_TEXTCTRL |