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 | 98 | if ( impl ) |
75a2c6a1 | 99 | impl->controlTextDidChange(); |
4d23a0d3 KO |
100 | } |
101 | ||
6d109846 SC |
102 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
103 | { | |
104 | wxUnusedVar(aNotification); | |
105 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
106 | if ( impl ) | |
107 | { | |
108 | impl->DoNotifyFocusEvent( false, NULL ); | |
109 | } | |
110 | } | |
111 | ||
e32090ba SC |
112 | @end |
113 | ||
1087c6c1 | 114 | @interface wxNSTextScrollView : NSScrollView |
4d23a0d3 | 115 | { |
4d23a0d3 | 116 | } |
1087c6c1 SC |
117 | @end |
118 | ||
1087c6c1 | 119 | @implementation wxNSTextScrollView |
c824c165 KO |
120 | |
121 | + (void)initialize | |
122 | { | |
123 | static BOOL initialized = NO; | |
03647350 VZ |
124 | if (!initialized) |
125 | { | |
c824c165 KO |
126 | initialized = YES; |
127 | wxOSXCocoaClassAddWXMethods( self ); | |
128 | } | |
129 | } | |
130 | ||
7cb2a241 SC |
131 | @end |
132 | ||
133 | @implementation wxNSTextFieldEditor | |
134 | ||
135 | - (void) keyDown:(NSEvent*) event | |
4d23a0d3 | 136 | { |
9e55f38d | 137 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
138 | lastKeyDownEvent = event; |
139 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) | |
140 | [super keyDown:event]; | |
141 | lastKeyDownEvent = nil; | |
4d23a0d3 | 142 | } |
533e2ae1 | 143 | |
7cb2a241 | 144 | - (void) keyUp:(NSEvent*) event |
533e2ae1 | 145 | { |
9e55f38d | 146 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
147 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) |
148 | [super keyUp:event]; | |
533e2ae1 | 149 | } |
1087c6c1 | 150 | |
7cb2a241 | 151 | - (void) flagsChanged:(NSEvent*) event |
1087c6c1 | 152 | { |
9e55f38d | 153 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 SC |
154 | if ( impl == NULL || !impl->DoHandleKeyEvent(event) ) |
155 | [super flagsChanged:event]; | |
1087c6c1 | 156 | } |
1087c6c1 | 157 | |
7cb2a241 SC |
158 | - (BOOL) performKeyEquivalent:(NSEvent*) event |
159 | { | |
160 | BOOL retval = [super performKeyEquivalent:event]; | |
161 | return retval; | |
162 | } | |
1087c6c1 | 163 | |
7cb2a241 | 164 | - (void) insertText:(id) str |
1087c6c1 | 165 | { |
9e55f38d | 166 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] ); |
7cb2a241 | 167 | if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) ) |
1087c6c1 | 168 | { |
7cb2a241 | 169 | [super insertText:str]; |
1087c6c1 | 170 | } |
1087c6c1 SC |
171 | } |
172 | ||
7cb2a241 SC |
173 | @end |
174 | ||
175 | @implementation wxNSTextView | |
1087c6c1 | 176 | |
7cb2a241 | 177 | + (void)initialize |
1087c6c1 | 178 | { |
7cb2a241 | 179 | static BOOL initialized = NO; |
03647350 | 180 | if (!initialized) |
7cb2a241 SC |
181 | { |
182 | initialized = YES; | |
183 | wxOSXCocoaClassAddWXMethods( self ); | |
184 | } | |
1087c6c1 SC |
185 | } |
186 | ||
b9cf2753 KO |
187 | - (void)textDidChange:(NSNotification *)aNotification |
188 | { | |
75a2c6a1 KO |
189 | wxUnusedVar(aNotification); |
190 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
b9cf2753 | 191 | if ( impl ) |
75a2c6a1 | 192 | impl->controlTextDidChange(); |
b9cf2753 KO |
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 | ||
c3e433b1 SC |
232 | - (void) setEnabled:(BOOL) flag |
233 | { | |
234 | [super setEnabled: flag]; | |
235 | ||
236 | if (![self drawsBackground]) { | |
237 | // Static text is drawn incorrectly when disabled. | |
238 | // For an explanation, see | |
239 | // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028 | |
240 | if (flag) { | |
241 | [self setTextColor: [NSColor controlTextColor]]; | |
242 | } else { | |
243 | [self setTextColor: [NSColor secondarySelectedControlColor]]; | |
244 | } | |
245 | } | |
246 | } | |
4d23a0d3 | 247 | |
ffad7b0d SC |
248 | - (void)controlTextDidChange:(NSNotification *)aNotification |
249 | { | |
6331c8c0 SC |
250 | wxUnusedVar(aNotification); |
251 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
ffad7b0d | 252 | if ( impl ) |
75a2c6a1 | 253 | impl->controlTextDidChange(); |
ffad7b0d | 254 | } |
dbeddfb9 | 255 | |
76b1a2c2 SC |
256 | - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector |
257 | { | |
258 | wxUnusedVar(textView); | |
259 | wxUnusedVar(control); | |
260 | ||
261 | BOOL handled = NO; | |
262 | ||
263 | // send back key events wx' common code knows how to handle | |
264 | ||
265 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
266 | if ( impl ) | |
267 | { | |
268 | wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer(); | |
269 | if ( wxpeer ) | |
270 | { | |
271 | if (commandSelector == @selector(insertNewline:)) | |
272 | { | |
273 | [textView insertNewlineIgnoringFieldEditor:self]; | |
274 | handled = YES; | |
275 | } | |
276 | else if ( commandSelector == @selector(insertTab:)) | |
277 | { | |
278 | [textView insertTabIgnoringFieldEditor:self]; | |
279 | handled = YES; | |
280 | } | |
281 | else if ( commandSelector == @selector(insertBacktab:)) | |
282 | { | |
283 | [textView insertTabIgnoringFieldEditor:self]; | |
284 | handled = YES; | |
285 | } | |
286 | } | |
287 | } | |
288 | ||
289 | return handled; | |
290 | } | |
291 | ||
ffad7b0d SC |
292 | - (void)controlTextDidEndEditing:(NSNotification *)aNotification |
293 | { | |
6d109846 SC |
294 | wxUnusedVar(aNotification); |
295 | wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); | |
ffad7b0d SC |
296 | if ( impl ) |
297 | { | |
6d109846 | 298 | impl->DoNotifyFocusEvent( false, NULL ); |
ffad7b0d SC |
299 | } |
300 | } | |
dbeddfb9 SC |
301 | @end |
302 | ||
c824c165 KO |
303 | // wxNSTextViewControl |
304 | ||
c072b9ec VZ |
305 | wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) |
306 | : wxWidgetCocoaImpl(wxPeer, w), | |
307 | wxTextWidgetImpl(wxPeer) | |
c824c165 | 308 | { |
1087c6c1 SC |
309 | wxNSTextScrollView* sv = (wxNSTextScrollView*) w; |
310 | m_scrollView = sv; | |
03647350 | 311 | |
c824c165 KO |
312 | [m_scrollView setHasVerticalScroller:YES]; |
313 | [m_scrollView setHasHorizontalScroller:NO]; | |
314 | [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | |
315 | NSSize contentSize = [m_scrollView contentSize]; | |
03647350 | 316 | |
1087c6c1 | 317 | wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0, |
c824c165 | 318 | contentSize.width, contentSize.height)]; |
1087c6c1 SC |
319 | m_textView = tv; |
320 | [tv setVerticallyResizable:YES]; | |
321 | [tv setHorizontallyResizable:NO]; | |
322 | [tv setAutoresizingMask:NSViewWidthSizable]; | |
03647350 | 323 | |
1087c6c1 | 324 | [m_scrollView setDocumentView: tv]; |
c824c165 | 325 | |
b9cf2753 | 326 | [tv setDelegate: tv]; |
03647350 VZ |
327 | |
328 | InstallEventHandler(tv); | |
c824c165 KO |
329 | } |
330 | ||
331 | wxNSTextViewControl::~wxNSTextViewControl() | |
332 | { | |
333 | if (m_textView) | |
334 | [m_textView setDelegate: nil]; | |
335 | } | |
336 | ||
78a17075 KO |
337 | bool wxNSTextViewControl::CanFocus() const |
338 | { | |
81533a3a SC |
339 | // since this doesn't work (return false), we hardcode |
340 | // if (m_textView) | |
341 | // return [m_textView canBecomeKeyView]; | |
342 | return true; | |
78a17075 KO |
343 | } |
344 | ||
03647350 | 345 | wxString wxNSTextViewControl::GetStringValue() const |
c824c165 | 346 | { |
03647350 | 347 | if (m_textView) |
c824c165 | 348 | { |
f66ecdc4 | 349 | wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding()); |
50b5e38d SC |
350 | wxMacConvertNewlines13To10( &result ) ; |
351 | return result; | |
c824c165 KO |
352 | } |
353 | return wxEmptyString; | |
354 | } | |
03647350 | 355 | void wxNSTextViewControl::SetStringValue( const wxString &str) |
c824c165 | 356 | { |
50b5e38d SC |
357 | wxString st = str; |
358 | wxMacConvertNewlines10To13( &st ); | |
5f65ba36 | 359 | wxMacEditHelper helper(m_textView); |
50b5e38d | 360 | |
c824c165 | 361 | if (m_textView) |
50b5e38d | 362 | [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
c824c165 | 363 | } |
7cb2a241 | 364 | |
03647350 | 365 | void wxNSTextViewControl::Copy() |
c824c165 KO |
366 | { |
367 | if (m_textView) | |
368 | [m_textView copy:nil]; | |
369 | ||
370 | } | |
371 | ||
03647350 | 372 | void wxNSTextViewControl::Cut() |
c824c165 KO |
373 | { |
374 | if (m_textView) | |
375 | [m_textView cut:nil]; | |
376 | } | |
377 | ||
03647350 | 378 | void wxNSTextViewControl::Paste() |
c824c165 KO |
379 | { |
380 | if (m_textView) | |
381 | [m_textView paste:nil]; | |
382 | } | |
383 | ||
03647350 VZ |
384 | bool wxNSTextViewControl::CanPaste() const |
385 | { | |
c824c165 KO |
386 | return true; |
387 | } | |
388 | ||
03647350 | 389 | void wxNSTextViewControl::SetEditable(bool editable) |
c824c165 KO |
390 | { |
391 | if (m_textView) | |
392 | [m_textView setEditable: editable]; | |
393 | } | |
394 | ||
03647350 | 395 | void wxNSTextViewControl::GetSelection( long* from, long* to) const |
c824c165 KO |
396 | { |
397 | if (m_textView) | |
398 | { | |
399 | NSRange range = [m_textView selectedRange]; | |
400 | *from = range.location; | |
401 | *to = range.location + range.length; | |
402 | } | |
403 | } | |
404 | ||
405 | void wxNSTextViewControl::SetSelection( long from , long to ) | |
406 | { | |
50b5e38d SC |
407 | long textLength = [[m_textView string] length]; |
408 | if ((from == -1) && (to == -1)) | |
409 | { | |
410 | from = 0 ; | |
411 | to = textLength ; | |
412 | } | |
413 | else | |
414 | { | |
415 | from = wxMin(textLength,wxMax(from,0)) ; | |
416 | if ( to == -1 ) | |
417 | to = textLength; | |
418 | else | |
419 | to = wxMax(0,wxMin(textLength,to)) ; | |
420 | } | |
421 | ||
2d6aa919 KO |
422 | NSRange selrange = NSMakeRange(from, to-from); |
423 | [m_textView setSelectedRange:selrange]; | |
424 | [m_textView scrollRangeToVisible:selrange]; | |
c824c165 KO |
425 | } |
426 | ||
03647350 | 427 | void wxNSTextViewControl::WriteText(const wxString& str) |
c824c165 | 428 | { |
50b5e38d SC |
429 | wxString st = str; |
430 | wxMacConvertNewlines10To13( &st ); | |
5f65ba36 | 431 | wxMacEditHelper helper(m_textView); |
715824d5 SC |
432 | NSEvent* formerEvent = m_lastKeyDownEvent; |
433 | m_lastKeyDownEvent = nil; | |
50b5e38d | 434 | [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
715824d5 | 435 | m_lastKeyDownEvent = formerEvent; |
c824c165 KO |
436 | } |
437 | ||
63bcc669 | 438 | void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) ) |
f95dd972 SC |
439 | { |
440 | if ([m_textView respondsToSelector:@selector(setFont:)]) | |
441 | [m_textView setFont: font.OSXGetNSFont()]; | |
442 | } | |
443 | ||
16671f22 KO |
444 | bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style) |
445 | { | |
b9cf2753 KO |
446 | if (m_textView && position >=0) |
447 | { | |
448 | NSFont* font = NULL; | |
449 | NSColor* bgcolor = NULL; | |
450 | NSColor* fgcolor = NULL; | |
451 | // NOTE: It appears that other platforms accept GetStyle with the position == length | |
452 | // but that NSTextStorage does not accept length as a valid position. | |
453 | // Therefore we return the default control style in that case. | |
454 | if (position < [[m_textView string] length]) | |
455 | { | |
456 | NSTextStorage* storage = [m_textView textStorage]; | |
457 | font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
458 | bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
459 | fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease]; | |
460 | } | |
461 | else | |
462 | { | |
463 | NSDictionary* attrs = [m_textView typingAttributes]; | |
464 | font = [[attrs objectForKey:NSFontAttributeName] autorelease]; | |
465 | bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease]; | |
466 | fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease]; | |
467 | } | |
468 | ||
16671f22 KO |
469 | if (font) |
470 | style.SetFont(wxFont(font)); | |
b9cf2753 | 471 | |
16671f22 KO |
472 | if (bgcolor) |
473 | style.SetBackgroundColour(wxColour(bgcolor)); | |
b9cf2753 KO |
474 | |
475 | if (fgcolor) | |
476 | style.SetTextColour(wxColour(fgcolor)); | |
16671f22 KO |
477 | return true; |
478 | } | |
479 | ||
480 | return false; | |
481 | } | |
482 | ||
483 | void wxNSTextViewControl::SetStyle(long start, | |
484 | long end, | |
485 | const wxTextAttr& style) | |
486 | { | |
487 | if (m_textView) { | |
488 | NSRange range = NSMakeRange(start, end-start); | |
b9cf2753 KO |
489 | if (start == -1 && end == -1) |
490 | range = [m_textView selectedRange]; | |
491 | ||
16671f22 KO |
492 | NSTextStorage* storage = [m_textView textStorage]; |
493 | ||
494 | wxFont font = style.GetFont(); | |
b9cf2753 KO |
495 | if (style.HasFont() && font.IsOk()) |
496 | [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range]; | |
16671f22 KO |
497 | |
498 | wxColour bgcolor = style.GetBackgroundColour(); | |
b9cf2753 | 499 | if (style.HasBackgroundColour() && bgcolor.IsOk()) |
16671f22 KO |
500 | [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range]; |
501 | ||
502 | wxColour fgcolor = style.GetTextColour(); | |
b9cf2753 | 503 | if (style.HasTextColour() && fgcolor.IsOk()) |
16671f22 KO |
504 | [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range]; |
505 | } | |
506 | } | |
f95dd972 | 507 | |
b9cf2753 KO |
508 | void wxNSTextViewControl::CheckSpelling(bool check) |
509 | { | |
510 | if (m_textView) | |
511 | [m_textView setContinuousSpellCheckingEnabled: check]; | |
512 | } | |
513 | ||
9ab7ff53 KO |
514 | wxSize wxNSTextViewControl::GetBestSize() const |
515 | { | |
516 | if (m_textView && [m_textView layoutManager]) | |
517 | { | |
518 | NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]]; | |
4a49fa24 VZ |
519 | return wxSize((int)(rect.size.width + [m_textView textContainerInset].width), |
520 | (int)(rect.size.height + [m_textView textContainerInset].height)); | |
9ab7ff53 | 521 | } |
c8fdb345 | 522 | return wxSize(0,0); |
9ab7ff53 KO |
523 | } |
524 | ||
c824c165 KO |
525 | // wxNSTextFieldControl |
526 | ||
c072b9ec VZ |
527 | wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w ) |
528 | : wxWidgetCocoaImpl(text, w), | |
529 | wxTextWidgetImpl(text) | |
530 | { | |
c8eab84f | 531 | Init(w); |
c072b9ec VZ |
532 | } |
533 | ||
534 | wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer, | |
535 | wxTextEntry *entry, | |
536 | WXWidget w) | |
537 | : wxWidgetCocoaImpl(wxPeer, w), | |
538 | wxTextWidgetImpl(entry) | |
539 | { | |
c8eab84f | 540 | Init(w); |
c072b9ec VZ |
541 | } |
542 | ||
543 | void wxNSTextFieldControl::Init(WXWidget w) | |
1e181c7a | 544 | { |
c8fdb345 SC |
545 | NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w; |
546 | m_textField = tf; | |
547 | [m_textField setDelegate: tf]; | |
50b5e38d | 548 | m_selStart = m_selEnd = 0; |
7cb2a241 | 549 | m_hasEditor = [w isKindOfClass:[NSTextField class]]; |
1e181c7a SC |
550 | } |
551 | ||
552 | wxNSTextFieldControl::~wxNSTextFieldControl() | |
553 | { | |
c824c165 KO |
554 | if (m_textField) |
555 | [m_textField setDelegate: nil]; | |
1e181c7a SC |
556 | } |
557 | ||
03647350 | 558 | wxString wxNSTextFieldControl::GetStringValue() const |
1e181c7a | 559 | { |
f66ecdc4 | 560 | return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding()); |
1e181c7a | 561 | } |
50b5e38d | 562 | |
03647350 | 563 | void wxNSTextFieldControl::SetStringValue( const wxString &str) |
1e181c7a | 564 | { |
5f65ba36 | 565 | wxMacEditHelper helper(m_textField); |
e32090ba | 566 | [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
1e181c7a | 567 | } |
50b5e38d | 568 | |
03647350 | 569 | void wxNSTextFieldControl::Copy() |
1e181c7a | 570 | { |
e32090ba SC |
571 | NSText* editor = [m_textField currentEditor]; |
572 | if ( editor ) | |
573 | { | |
574 | [editor copy:nil]; | |
575 | } | |
1e181c7a SC |
576 | } |
577 | ||
03647350 | 578 | void wxNSTextFieldControl::Cut() |
1e181c7a | 579 | { |
e32090ba SC |
580 | NSText* editor = [m_textField currentEditor]; |
581 | if ( editor ) | |
582 | { | |
583 | [editor cut:nil]; | |
584 | } | |
1e181c7a SC |
585 | } |
586 | ||
03647350 | 587 | void wxNSTextFieldControl::Paste() |
1e181c7a | 588 | { |
e32090ba SC |
589 | NSText* editor = [m_textField currentEditor]; |
590 | if ( editor ) | |
591 | { | |
592 | [editor paste:nil]; | |
593 | } | |
1e181c7a SC |
594 | } |
595 | ||
03647350 VZ |
596 | bool wxNSTextFieldControl::CanPaste() const |
597 | { | |
e32090ba | 598 | return true; |
1e181c7a SC |
599 | } |
600 | ||
03647350 | 601 | void wxNSTextFieldControl::SetEditable(bool editable) |
1e181c7a | 602 | { |
e32090ba | 603 | [m_textField setEditable:editable]; |
1e181c7a SC |
604 | } |
605 | ||
03647350 | 606 | void wxNSTextFieldControl::GetSelection( long* from, long* to) const |
1e181c7a | 607 | { |
e32090ba SC |
608 | NSText* editor = [m_textField currentEditor]; |
609 | if ( editor ) | |
610 | { | |
611 | NSRange range = [editor selectedRange]; | |
612 | *from = range.location; | |
613 | *to = range.location + range.length; | |
614 | } | |
50b5e38d SC |
615 | else |
616 | { | |
617 | *from = m_selStart; | |
618 | *to = m_selEnd; | |
619 | } | |
1e181c7a SC |
620 | } |
621 | ||
622 | void wxNSTextFieldControl::SetSelection( long from , long to ) | |
623 | { | |
50b5e38d SC |
624 | long textLength = [[m_textField stringValue] length]; |
625 | if ((from == -1) && (to == -1)) | |
626 | { | |
627 | from = 0 ; | |
628 | to = textLength ; | |
629 | } | |
630 | else | |
631 | { | |
632 | from = wxMin(textLength,wxMax(from,0)) ; | |
633 | if ( to == -1 ) | |
634 | to = textLength; | |
635 | else | |
636 | to = wxMax(0,wxMin(textLength,to)) ; | |
637 | } | |
638 | ||
e32090ba SC |
639 | NSText* editor = [m_textField currentEditor]; |
640 | if ( editor ) | |
641 | { | |
642 | [editor setSelectedRange:NSMakeRange(from, to-from)]; | |
643 | } | |
50b5e38d SC |
644 | else |
645 | { | |
646 | m_selStart = from; | |
647 | m_selEnd = to; | |
648 | } | |
1e181c7a SC |
649 | } |
650 | ||
03647350 | 651 | void wxNSTextFieldControl::WriteText(const wxString& str) |
1e181c7a | 652 | { |
715824d5 SC |
653 | NSEvent* formerEvent = m_lastKeyDownEvent; |
654 | m_lastKeyDownEvent = nil; | |
50b5e38d SC |
655 | NSText* editor = [m_textField currentEditor]; |
656 | if ( editor ) | |
657 | { | |
5f65ba36 | 658 | wxMacEditHelper helper(m_textField); |
50b5e38d SC |
659 | [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()]; |
660 | } | |
661 | else | |
662 | { | |
663 | wxString val = GetStringValue() ; | |
664 | long start , end ; | |
665 | GetSelection( &start , &end ) ; | |
666 | val.Remove( start , end - start ) ; | |
667 | val.insert( start , str ) ; | |
668 | SetStringValue( val ) ; | |
669 | SetSelection( start + str.length() , start + str.length() ) ; | |
670 | } | |
715824d5 | 671 | m_lastKeyDownEvent = formerEvent; |
1e181c7a | 672 | } |
dbeddfb9 | 673 | |
03647350 | 674 | void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf), |
6331c8c0 | 675 | void* WXUNUSED(_cmd), void *WXUNUSED(sender)) |
e32090ba SC |
676 | { |
677 | wxWindow* wxpeer = (wxWindow*) GetWXPeer(); | |
03647350 | 678 | if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) ) |
e32090ba SC |
679 | { |
680 | wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId()); | |
681 | event.SetEventObject( wxpeer ); | |
c072b9ec | 682 | event.SetString( GetTextEntry()->GetValue() ); |
e32090ba SC |
683 | wxpeer->HandleWindowEvent( event ); |
684 | } | |
685 | } | |
686 | ||
687 | // | |
688 | // | |
689 | // | |
690 | ||
03647350 VZ |
691 | wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer, |
692 | wxWindowMac* WXUNUSED(parent), | |
693 | wxWindowID WXUNUSED(id), | |
63bcc669 | 694 | const wxString& WXUNUSED(str), |
03647350 | 695 | const wxPoint& pos, |
dbeddfb9 | 696 | const wxSize& size, |
03647350 | 697 | long style, |
6331c8c0 | 698 | long WXUNUSED(extraStyle)) |
dbeddfb9 | 699 | { |
dbeddfb9 | 700 | NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ; |
c824c165 | 701 | wxWidgetCocoaImpl* c = NULL; |
03647350 | 702 | |
c824c165 | 703 | if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 ) |
b15f9375 | 704 | { |
1087c6c1 SC |
705 | wxNSTextScrollView* v = nil; |
706 | v = [[wxNSTextScrollView alloc] initWithFrame:r]; | |
c824c165 | 707 | c = new wxNSTextViewControl( wxpeer, v ); |
c824c165 | 708 | } |
03647350 | 709 | else |
c824c165 | 710 | { |
6331c8c0 | 711 | NSTextField* v = nil; |
c824c165 KO |
712 | if ( style & wxTE_PASSWORD ) |
713 | v = [[wxNSSecureTextField alloc] initWithFrame:r]; | |
714 | else | |
715 | v = [[wxNSTextField alloc] initWithFrame:r]; | |
03647350 | 716 | |
c824c165 KO |
717 | if ( style & wxNO_BORDER ) |
718 | { | |
719 | // FIXME: How can we remove the native control's border? | |
720 | // setBordered is separate from the text ctrl's border. | |
721 | } | |
3f16e51b SC |
722 | |
723 | NSTextFieldCell* cell = [v cell]; | |
724 | [cell setScrollable:YES]; | |
725 | // TODO: Remove if we definitely are sure, it's not needed | |
726 | // as setting scrolling to yes, should turn off any wrapping | |
727 | // [cell setLineBreakMode:NSLineBreakByClipping]; | |
03647350 | 728 | |
b15f9375 SC |
729 | [v setBezeled:NO]; |
730 | [v setBordered:NO]; | |
03647350 | 731 | |
c072b9ec | 732 | c = new wxNSTextFieldControl( wxpeer, wxpeer, v ); |
b15f9375 | 733 | } |
03647350 | 734 | |
dbeddfb9 SC |
735 | return c; |
736 | } | |
737 | ||
738 | ||
739 | #endif // wxUSE_TEXTCTRL |