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