]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/textctrl.mm
No changes, just minor wxMSW code cleanup: use WinStruct<>.
[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$
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 #include "wx/textcompleter.h"
49
50 #include "wx/osx/private.h"
51 #include "wx/osx/cocoa/private/textimpl.h"
52
53 @interface NSView(EditableView)
54 - (BOOL)isEditable;
55 - (void)setEditable:(BOOL)flag;
56 - (BOOL)isSelectable;
57 - (void)setSelectable:(BOOL)flag;
58 @end
59
60 // An object of this class is created before the text is modified
61 // programmatically and destroyed as soon as this is done. It does several
62 // things, like ensuring that the control is editable to allow setting its text
63 // at all and eating any unwanted focus loss events from textDidEndEditing:
64 // which don't really correspond to focus change.
65 class wxMacEditHelper
66 {
67 public :
68 wxMacEditHelper( NSView* textView )
69 {
70 m_viewPreviouslyEdited = ms_viewCurrentlyEdited;
71 ms_viewCurrentlyEdited =
72 m_textView = textView;
73 m_formerEditable = YES;
74 if ( textView )
75 {
76 m_formerEditable = [textView isEditable];
77 m_formerSelectable = [textView isSelectable];
78 [textView setEditable:YES];
79 }
80 }
81
82 ~wxMacEditHelper()
83 {
84 if ( m_textView )
85 {
86 [m_textView setEditable:m_formerEditable];
87 [m_textView setSelectable:m_formerSelectable];
88 }
89
90 ms_viewCurrentlyEdited = m_viewPreviouslyEdited;
91 }
92
93 // Returns the last view we were instantiated for or NULL.
94 static NSView *GetCurrentlyEditedView() { return ms_viewCurrentlyEdited; }
95
96 protected :
97 BOOL m_formerEditable ;
98 BOOL m_formerSelectable;
99 NSView* m_textView;
100
101 // The original value of ms_viewCurrentlyEdited when this object was
102 // created.
103 NSView* m_viewPreviouslyEdited;
104
105 static NSView* ms_viewCurrentlyEdited;
106 } ;
107
108 NSView* wxMacEditHelper::ms_viewCurrentlyEdited = nil;
109
110 // a minimal NSFormatter that just avoids getting too long entries
111 @interface wxMaximumLengthFormatter : NSFormatter
112 {
113 int maxLength;
114 }
115
116 @end
117
118 @implementation wxMaximumLengthFormatter
119
120 - (id)init
121 {
122 [super init];
123 maxLength = 0;
124 return self;
125 }
126
127 - (void) setMaxLength:(int) maxlen
128 {
129 maxLength = maxlen;
130 }
131
132 - (NSString *)stringForObjectValue:(id)anObject
133 {
134 if(![anObject isKindOfClass:[NSString class]])
135 return nil;
136 return [NSString stringWithString:anObject];
137 }
138
139 - (BOOL)getObjectValue:(id *)obj forString:(NSString *)string errorDescription:(NSString **)error
140 {
141 *obj = [NSString stringWithString:string];
142 return YES;
143 }
144
145 - (BOOL)isPartialStringValid:(NSString **)partialStringPtr proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
146 originalString:(NSString *)origString originalSelectedRange:(NSRange)origSelRange errorDescription:(NSString **)error
147 {
148 int len = [*partialStringPtr length];
149 if ( maxLength > 0 && len > maxLength )
150 {
151 // TODO wxEVT_COMMAND_TEXT_MAXLEN
152 return NO;
153 }
154 return YES;
155 }
156
157 @end
158
159 @implementation wxNSSecureTextField
160
161 + (void)initialize
162 {
163 static BOOL initialized = NO;
164 if (!initialized)
165 {
166 initialized = YES;
167 wxOSXCocoaClassAddWXMethods( self );
168 }
169 }
170
171 - (void)controlTextDidChange:(NSNotification *)aNotification
172 {
173 wxUnusedVar(aNotification);
174 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
175 if ( impl )
176 impl->controlTextDidChange();
177 }
178
179 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
180 {
181 wxUnusedVar(aNotification);
182 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
183 if ( impl )
184 {
185 impl->DoNotifyFocusEvent( false, NULL );
186 }
187 }
188
189 @end
190
191 @interface wxNSTextScrollView : NSScrollView
192 {
193 }
194 @end
195
196 @implementation wxNSTextScrollView
197
198 + (void)initialize
199 {
200 static BOOL initialized = NO;
201 if (!initialized)
202 {
203 initialized = YES;
204 wxOSXCocoaClassAddWXMethods( self );
205 }
206 }
207
208 @end
209
210 @implementation wxNSTextFieldEditor
211
212 - (void) keyDown:(NSEvent*) event
213 {
214 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
215 lastKeyDownEvent = event;
216 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
217 [super keyDown:event];
218 lastKeyDownEvent = nil;
219 }
220
221 - (void) keyUp:(NSEvent*) event
222 {
223 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
224 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
225 [super keyUp:event];
226 }
227
228 - (void) flagsChanged:(NSEvent*) event
229 {
230 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
231 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
232 [super flagsChanged:event];
233 }
234
235 - (BOOL) performKeyEquivalent:(NSEvent*) event
236 {
237 BOOL retval = [super performKeyEquivalent:event];
238 return retval;
239 }
240
241 - (void) insertText:(id) str
242 {
243 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
244 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
245 {
246 [super insertText:str];
247 }
248 }
249
250 @end
251
252 @implementation wxNSTextView
253
254 + (void)initialize
255 {
256 static BOOL initialized = NO;
257 if (!initialized)
258 {
259 initialized = YES;
260 wxOSXCocoaClassAddWXMethods( self );
261 }
262 }
263
264 - (void)textDidChange:(NSNotification *)aNotification
265 {
266 wxUnusedVar(aNotification);
267 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
268 if ( impl )
269 impl->controlTextDidChange();
270 }
271
272 - (void) setEnabled:(BOOL) flag
273 {
274 // from Technical Q&A QA1461
275 if (flag) {
276 [self setTextColor: [NSColor controlTextColor]];
277
278 } else {
279 [self setTextColor: [NSColor disabledControlTextColor]];
280 }
281
282 [self setSelectable: flag];
283 [self setEditable: flag];
284 }
285
286 - (BOOL) isEnabled
287 {
288 return [self isEditable];
289 }
290
291 - (void)textDidEndEditing:(NSNotification *)aNotification
292 {
293 wxUnusedVar(aNotification);
294
295 if ( self == wxMacEditHelper::GetCurrentlyEditedView() )
296 {
297 // This notification is generated as the result of calling our own
298 // wxTextCtrl method (e.g. WriteText()) and doesn't correspond to any
299 // real focus loss event so skip generating it.
300 return;
301 }
302
303 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
304 if ( impl )
305 {
306 impl->DoNotifyFocusEvent( false, NULL );
307 }
308 }
309
310 @end
311
312 @implementation wxNSTextField
313
314 + (void)initialize
315 {
316 static BOOL initialized = NO;
317 if (!initialized)
318 {
319 initialized = YES;
320 wxOSXCocoaClassAddWXMethods( self );
321 }
322 }
323
324 - (id) initWithFrame:(NSRect) frame
325 {
326 self = [super initWithFrame:frame];
327 fieldEditor = nil;
328 return self;
329 }
330
331 - (void) dealloc
332 {
333 [fieldEditor release];
334 [super dealloc];
335 }
336
337 - (void) setFieldEditor:(wxNSTextFieldEditor*) editor
338 {
339 if ( editor != fieldEditor )
340 {
341 [editor retain];
342 [fieldEditor release];
343 fieldEditor = editor;
344 }
345 }
346
347 - (wxNSTextFieldEditor*) fieldEditor
348 {
349 return fieldEditor;
350 }
351
352 - (void) setEnabled:(BOOL) flag
353 {
354 [super setEnabled: flag];
355
356 if (![self drawsBackground]) {
357 // Static text is drawn incorrectly when disabled.
358 // For an explanation, see
359 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
360 if (flag) {
361 [self setTextColor: [NSColor controlTextColor]];
362 } else {
363 [self setTextColor: [NSColor secondarySelectedControlColor]];
364 }
365 }
366 }
367
368 - (void)controlTextDidChange:(NSNotification *)aNotification
369 {
370 wxUnusedVar(aNotification);
371 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
372 if ( impl )
373 impl->controlTextDidChange();
374 }
375
376 - (NSArray *)control:(NSControl *)control textView:(NSTextView *)textView completions:(NSArray *)words
377 forPartialWordRange:(NSRange)charRange indexOfSelectedItem:(NSInteger*)index
378 {
379 NSMutableArray* matches = NULL;
380
381 wxTextWidgetImpl* impl = (wxNSTextFieldControl * ) wxWidgetImpl::FindFromWXWidget( self );
382 wxTextEntry * const entry = impl->GetTextEntry();
383 wxTextCompleter * const completer = entry->OSXGetCompleter();
384 if ( completer )
385 {
386 const wxString prefix = entry->GetValue();
387 if ( completer->Start(prefix) )
388 {
389 const wxString
390 wordStart = wxCFStringRef::AsString(
391 [[textView string] substringWithRange:charRange]
392 );
393
394 matches = [NSMutableArray array];
395 for ( ;; )
396 {
397 const wxString s = completer->GetNext();
398 if ( s.empty() )
399 break;
400
401 // Normally the completer should return only the strings
402 // starting with the prefix, but there could be exceptions
403 // and, for compatibility with MSW which simply ignores all
404 // entries that don't match the current text control contents,
405 // we ignore them as well. Besides, our own wxTextCompleterFixed
406 // doesn't respect this rule and, moreover, we need to extract
407 // just the rest of the string anyhow.
408 wxString completion;
409 if ( s.StartsWith(prefix, &completion) )
410 {
411 // We discarded the entire prefix above but actually we
412 // should include the part of it that consists of the
413 // beginning of the current word, otherwise it would be
414 // lost when completion is accepted as OS X supposes that
415 // our matches do start with the "partial word range"
416 // passed to us.
417 const wxCFStringRef fullWord(wordStart + completion);
418 [matches addObject: fullWord.AsNSString()];
419 }
420 }
421 }
422 }
423
424 return matches;
425 }
426
427 - (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
428 {
429 wxUnusedVar(textView);
430 wxUnusedVar(control);
431
432 BOOL handled = NO;
433
434 // send back key events wx' common code knows how to handle
435
436 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
437 if ( impl )
438 {
439 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
440 if ( wxpeer )
441 {
442 if (commandSelector == @selector(insertNewline:))
443 {
444 [textView insertNewlineIgnoringFieldEditor:self];
445 handled = YES;
446 }
447 else if ( commandSelector == @selector(insertTab:))
448 {
449 [textView insertTabIgnoringFieldEditor:self];
450 handled = YES;
451 }
452 else if ( commandSelector == @selector(insertBacktab:))
453 {
454 [textView insertTabIgnoringFieldEditor:self];
455 handled = YES;
456 }
457 }
458 }
459
460 return handled;
461 }
462
463 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
464 {
465 wxUnusedVar(aNotification);
466 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
467 if ( impl )
468 {
469 impl->DoNotifyFocusEvent( false, NULL );
470 }
471 }
472 @end
473
474 // wxNSTextViewControl
475
476 wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w )
477 : wxWidgetCocoaImpl(wxPeer, w),
478 wxTextWidgetImpl(wxPeer)
479 {
480 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
481 m_scrollView = sv;
482
483 [m_scrollView setHasVerticalScroller:YES];
484 [m_scrollView setHasHorizontalScroller:NO];
485 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
486 NSSize contentSize = [m_scrollView contentSize];
487
488 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
489 contentSize.width, contentSize.height)];
490 m_textView = tv;
491 [tv setVerticallyResizable:YES];
492 [tv setHorizontallyResizable:NO];
493 [tv setAutoresizingMask:NSViewWidthSizable];
494
495 [m_scrollView setDocumentView: tv];
496
497 [tv setDelegate: tv];
498
499 InstallEventHandler(tv);
500 }
501
502 wxNSTextViewControl::~wxNSTextViewControl()
503 {
504 if (m_textView)
505 [m_textView setDelegate: nil];
506 }
507
508 bool wxNSTextViewControl::CanFocus() const
509 {
510 // since this doesn't work (return false), we hardcode
511 // if (m_textView)
512 // return [m_textView canBecomeKeyView];
513 return true;
514 }
515
516 wxString wxNSTextViewControl::GetStringValue() const
517 {
518 if (m_textView)
519 {
520 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
521 wxMacConvertNewlines13To10( &result ) ;
522 return result;
523 }
524 return wxEmptyString;
525 }
526 void wxNSTextViewControl::SetStringValue( const wxString &str)
527 {
528 wxString st = str;
529 wxMacConvertNewlines10To13( &st );
530 wxMacEditHelper helper(m_textView);
531
532 if (m_textView)
533 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
534 }
535
536 void wxNSTextViewControl::Copy()
537 {
538 if (m_textView)
539 [m_textView copy:nil];
540
541 }
542
543 void wxNSTextViewControl::Cut()
544 {
545 if (m_textView)
546 [m_textView cut:nil];
547 }
548
549 void wxNSTextViewControl::Paste()
550 {
551 if (m_textView)
552 [m_textView paste:nil];
553 }
554
555 bool wxNSTextViewControl::CanPaste() const
556 {
557 return true;
558 }
559
560 void wxNSTextViewControl::SetEditable(bool editable)
561 {
562 if (m_textView)
563 [m_textView setEditable: editable];
564 }
565
566 void wxNSTextViewControl::GetSelection( long* from, long* to) const
567 {
568 if (m_textView)
569 {
570 NSRange range = [m_textView selectedRange];
571 *from = range.location;
572 *to = range.location + range.length;
573 }
574 }
575
576 void wxNSTextViewControl::SetSelection( long from , long to )
577 {
578 long textLength = [[m_textView string] length];
579 if ((from == -1) && (to == -1))
580 {
581 from = 0 ;
582 to = textLength ;
583 }
584 else
585 {
586 from = wxMin(textLength,wxMax(from,0)) ;
587 if ( to == -1 )
588 to = textLength;
589 else
590 to = wxMax(0,wxMin(textLength,to)) ;
591 }
592
593 NSRange selrange = NSMakeRange(from, to-from);
594 [m_textView setSelectedRange:selrange];
595 [m_textView scrollRangeToVisible:selrange];
596 }
597
598 void wxNSTextViewControl::WriteText(const wxString& str)
599 {
600 wxString st = str;
601 wxMacConvertNewlines10To13( &st );
602 wxMacEditHelper helper(m_textView);
603 NSEvent* formerEvent = m_lastKeyDownEvent;
604 m_lastKeyDownEvent = nil;
605 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
606 m_lastKeyDownEvent = formerEvent;
607 }
608
609 void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
610 {
611 if ([m_textView respondsToSelector:@selector(setFont:)])
612 [m_textView setFont: font.OSXGetNSFont()];
613 }
614
615 bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
616 {
617 if (m_textView && position >=0)
618 {
619 NSFont* font = NULL;
620 NSColor* bgcolor = NULL;
621 NSColor* fgcolor = NULL;
622 // NOTE: It appears that other platforms accept GetStyle with the position == length
623 // but that NSTextStorage does not accept length as a valid position.
624 // Therefore we return the default control style in that case.
625 if (position < (long) [[m_textView string] length])
626 {
627 NSTextStorage* storage = [m_textView textStorage];
628 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
629 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
630 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
631 }
632 else
633 {
634 NSDictionary* attrs = [m_textView typingAttributes];
635 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
636 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
637 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
638 }
639
640 if (font)
641 style.SetFont(wxFont(font));
642
643 if (bgcolor)
644 style.SetBackgroundColour(wxColour(bgcolor));
645
646 if (fgcolor)
647 style.SetTextColour(wxColour(fgcolor));
648 return true;
649 }
650
651 return false;
652 }
653
654 void wxNSTextViewControl::SetStyle(long start,
655 long end,
656 const wxTextAttr& style)
657 {
658 if (m_textView) {
659 NSRange range = NSMakeRange(start, end-start);
660 if (start == -1 && end == -1)
661 range = [m_textView selectedRange];
662
663 NSTextStorage* storage = [m_textView textStorage];
664
665 wxFont font = style.GetFont();
666 if (style.HasFont() && font.IsOk())
667 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
668
669 wxColour bgcolor = style.GetBackgroundColour();
670 if (style.HasBackgroundColour() && bgcolor.IsOk())
671 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
672
673 wxColour fgcolor = style.GetTextColour();
674 if (style.HasTextColour() && fgcolor.IsOk())
675 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
676 }
677 }
678
679 void wxNSTextViewControl::CheckSpelling(bool check)
680 {
681 if (m_textView)
682 [m_textView setContinuousSpellCheckingEnabled: check];
683 }
684
685 wxSize wxNSTextViewControl::GetBestSize() const
686 {
687 if (m_textView && [m_textView layoutManager])
688 {
689 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
690 return wxSize((int)(rect.size.width + [m_textView textContainerInset].width),
691 (int)(rect.size.height + [m_textView textContainerInset].height));
692 }
693 return wxSize(0,0);
694 }
695
696 // wxNSTextFieldControl
697
698 wxNSTextFieldControl::wxNSTextFieldControl( wxTextCtrl *text, WXWidget w )
699 : wxWidgetCocoaImpl(text, w),
700 wxTextWidgetImpl(text)
701 {
702 Init(w);
703 }
704
705 wxNSTextFieldControl::wxNSTextFieldControl(wxWindow *wxPeer,
706 wxTextEntry *entry,
707 WXWidget w)
708 : wxWidgetCocoaImpl(wxPeer, w),
709 wxTextWidgetImpl(entry)
710 {
711 Init(w);
712 }
713
714 void wxNSTextFieldControl::Init(WXWidget w)
715 {
716 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>)*) w;
717 m_textField = tf;
718 [m_textField setDelegate: tf];
719 m_selStart = m_selEnd = 0;
720 m_hasEditor = [w isKindOfClass:[NSTextField class]];
721 }
722
723 wxNSTextFieldControl::~wxNSTextFieldControl()
724 {
725 if (m_textField)
726 [m_textField setDelegate: nil];
727 }
728
729 wxString wxNSTextFieldControl::GetStringValue() const
730 {
731 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
732 }
733
734 void wxNSTextFieldControl::SetStringValue( const wxString &str)
735 {
736 wxMacEditHelper helper(m_textField);
737 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
738 }
739
740 void wxNSTextFieldControl::SetMaxLength(unsigned long len)
741 {
742 wxMaximumLengthFormatter* formatter = [[[wxMaximumLengthFormatter alloc] init] autorelease];
743 [formatter setMaxLength:len];
744 [m_textField setFormatter:formatter];
745 }
746
747 void wxNSTextFieldControl::Copy()
748 {
749 NSText* editor = [m_textField currentEditor];
750 if ( editor )
751 {
752 [editor copy:nil];
753 }
754 }
755
756 void wxNSTextFieldControl::Cut()
757 {
758 NSText* editor = [m_textField currentEditor];
759 if ( editor )
760 {
761 [editor cut:nil];
762 }
763 }
764
765 void wxNSTextFieldControl::Paste()
766 {
767 NSText* editor = [m_textField currentEditor];
768 if ( editor )
769 {
770 [editor paste:nil];
771 }
772 }
773
774 bool wxNSTextFieldControl::CanPaste() const
775 {
776 return true;
777 }
778
779 void wxNSTextFieldControl::SetEditable(bool editable)
780 {
781 [m_textField setEditable:editable];
782 }
783
784 void wxNSTextFieldControl::GetSelection( long* from, long* to) const
785 {
786 NSText* editor = [m_textField currentEditor];
787 if ( editor )
788 {
789 NSRange range = [editor selectedRange];
790 *from = range.location;
791 *to = range.location + range.length;
792 }
793 else
794 {
795 *from = m_selStart;
796 *to = m_selEnd;
797 }
798 }
799
800 void wxNSTextFieldControl::SetSelection( long from , long to )
801 {
802 long textLength = [[m_textField stringValue] length];
803 if ((from == -1) && (to == -1))
804 {
805 from = 0 ;
806 to = textLength ;
807 }
808 else
809 {
810 from = wxMin(textLength,wxMax(from,0)) ;
811 if ( to == -1 )
812 to = textLength;
813 else
814 to = wxMax(0,wxMin(textLength,to)) ;
815 }
816
817 NSText* editor = [m_textField currentEditor];
818 if ( editor )
819 {
820 [editor setSelectedRange:NSMakeRange(from, to-from)];
821 }
822 else
823 {
824 m_selStart = from;
825 m_selEnd = to;
826 }
827 }
828
829 void wxNSTextFieldControl::WriteText(const wxString& str)
830 {
831 NSEvent* formerEvent = m_lastKeyDownEvent;
832 m_lastKeyDownEvent = nil;
833 NSText* editor = [m_textField currentEditor];
834 if ( editor )
835 {
836 wxMacEditHelper helper(m_textField);
837 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
838 }
839 else
840 {
841 wxString val = GetStringValue() ;
842 long start , end ;
843 GetSelection( &start , &end ) ;
844 val.Remove( start , end - start ) ;
845 val.insert( start , str ) ;
846 SetStringValue( val ) ;
847 SetSelection( start + str.length() , start + str.length() ) ;
848 }
849 m_lastKeyDownEvent = formerEvent;
850 }
851
852 void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
853 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
854 {
855 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
856 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
857 {
858 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
859 event.SetEventObject( wxpeer );
860 event.SetString( GetTextEntry()->GetValue() );
861 wxpeer->HandleWindowEvent( event );
862 }
863 }
864
865 bool wxNSTextFieldControl::SetHint(const wxString& hint)
866 {
867 wxCFStringRef hintstring(hint);
868 [[m_textField cell] setPlaceholderString:hintstring.AsNSString()];
869 return true;
870 }
871
872 //
873 //
874 //
875
876 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
877 wxWindowMac* WXUNUSED(parent),
878 wxWindowID WXUNUSED(id),
879 const wxString& WXUNUSED(str),
880 const wxPoint& pos,
881 const wxSize& size,
882 long style,
883 long WXUNUSED(extraStyle))
884 {
885 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
886 wxWidgetCocoaImpl* c = NULL;
887
888 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
889 {
890 wxNSTextScrollView* v = nil;
891 v = [[wxNSTextScrollView alloc] initWithFrame:r];
892 c = new wxNSTextViewControl( wxpeer, v );
893 }
894 else
895 {
896 NSTextField* v = nil;
897 if ( style & wxTE_PASSWORD )
898 v = [[wxNSSecureTextField alloc] initWithFrame:r];
899 else
900 v = [[wxNSTextField alloc] initWithFrame:r];
901
902 if ( style & wxNO_BORDER )
903 {
904 // FIXME: How can we remove the native control's border?
905 // setBordered is separate from the text ctrl's border.
906 }
907
908 NSTextFieldCell* cell = [v cell];
909 [cell setScrollable:YES];
910 // TODO: Remove if we definitely are sure, it's not needed
911 // as setting scrolling to yes, should turn off any wrapping
912 // [cell setLineBreakMode:NSLineBreakByClipping];
913
914 [v setBezeled:NO];
915 [v setBordered:NO];
916
917 c = new wxNSTextFieldControl( wxpeer, wxpeer, v );
918 }
919 c->SetNeedsFocusRect( true );
920
921 return c;
922 }
923
924
925 #endif // wxUSE_TEXTCTRL