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