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