]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/textctrl.mm
nschars is never used for the wxEVT_CHAR case, so don't assign it a different value...
[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
7// RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_TEXTCTRL
15
16#include "wx/textctrl.h"
17
18#ifndef WX_PRECOMP
19 #include "wx/intl.h"
20 #include "wx/app.h"
21 #include "wx/utils.h"
22 #include "wx/dc.h"
23 #include "wx/button.h"
24 #include "wx/menu.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
28#endif
29
30#ifdef __DARWIN__
31 #include <sys/types.h>
32 #include <sys/stat.h>
33#else
34 #include <stat.h>
35#endif
36
37#if wxUSE_STD_IOSTREAM
38 #if wxUSE_IOSTREAMH
39 #include <fstream.h>
40 #else
41 #include <fstream>
42 #endif
43#endif
44
45#include "wx/filefn.h"
46#include "wx/sysopt.h"
47#include "wx/thread.h"
48
49#include "wx/osx/private.h"
1e181c7a 50#include "wx/osx/cocoa/private/textimpl.h"
dbeddfb9 51
03647350 52@interface NSView(EditableView)
5f65ba36
SC
53- (BOOL)isEditable;
54- (void)setEditable:(BOOL)flag;
55@end
56
57class wxMacEditHelper
58{
59public :
60 wxMacEditHelper( NSView* textView )
61 {
63bcc669
SC
62 m_textView = textView;
63 m_formerState = YES;
5f65ba36
SC
64 if ( textView )
65 {
66 m_formerState = [textView isEditable];
67 [textView setEditable:YES];
68 }
69 }
70
71 ~wxMacEditHelper()
72 {
73 if ( m_textView )
74 [m_textView setEditable:m_formerState];
75 }
76
77protected :
78 BOOL m_formerState ;
79 NSView* m_textView;
80} ;
81
03647350 82@implementation wxNSSecureTextField
dbeddfb9 83
4dd9fdf8
SC
84+ (void)initialize
85{
86 static BOOL initialized = NO;
03647350 87 if (!initialized)
4dd9fdf8
SC
88 {
89 initialized = YES;
90 wxOSXCocoaClassAddWXMethods( self );
91 }
92}
dbeddfb9 93
4d23a0d3
KO
94- (void)controlTextDidChange:(NSNotification *)aNotification
95{
6331c8c0
SC
96 wxUnusedVar(aNotification);
97 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
4d23a0d3
KO
98 if ( impl )
99 {
100 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
101 if ( wxpeer ) {
102 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
103 event.SetEventObject( wxpeer );
104 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
105 wxpeer->HandleWindowEvent( event );
106 }
107 }
108}
109
6d109846
SC
110- (void)controlTextDidEndEditing:(NSNotification *)aNotification
111{
112 wxUnusedVar(aNotification);
113 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
114 if ( impl )
115 {
116 impl->DoNotifyFocusEvent( false, NULL );
117 }
118}
119
e32090ba
SC
120@end
121
1087c6c1 122@interface wxNSTextScrollView : NSScrollView
4d23a0d3 123{
4d23a0d3 124}
1087c6c1
SC
125@end
126
1087c6c1 127@implementation wxNSTextScrollView
c824c165
KO
128
129+ (void)initialize
130{
131 static BOOL initialized = NO;
03647350
VZ
132 if (!initialized)
133 {
c824c165
KO
134 initialized = YES;
135 wxOSXCocoaClassAddWXMethods( self );
136 }
137}
138
7cb2a241
SC
139@end
140
141@implementation wxNSTextFieldEditor
142
143- (void) keyDown:(NSEvent*) event
4d23a0d3 144{
9e55f38d 145 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
7cb2a241
SC
146 lastKeyDownEvent = event;
147 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
148 [super keyDown:event];
149 lastKeyDownEvent = nil;
4d23a0d3 150}
533e2ae1 151
7cb2a241 152- (void) keyUp:(NSEvent*) event
533e2ae1 153{
9e55f38d 154 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
7cb2a241
SC
155 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
156 [super keyUp:event];
533e2ae1 157}
1087c6c1 158
7cb2a241 159- (void) flagsChanged:(NSEvent*) event
1087c6c1 160{
9e55f38d 161 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
7cb2a241
SC
162 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
163 [super flagsChanged:event];
1087c6c1 164}
1087c6c1 165
7cb2a241
SC
166- (BOOL) performKeyEquivalent:(NSEvent*) event
167{
168 BOOL retval = [super performKeyEquivalent:event];
169 return retval;
170}
1087c6c1 171
7cb2a241 172- (void) insertText:(id) str
1087c6c1 173{
9e55f38d 174 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
7cb2a241 175 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
1087c6c1 176 {
7cb2a241 177 [super insertText:str];
1087c6c1 178 }
1087c6c1
SC
179}
180
7cb2a241
SC
181@end
182
183@implementation wxNSTextView
1087c6c1 184
7cb2a241 185+ (void)initialize
1087c6c1 186{
7cb2a241 187 static BOOL initialized = NO;
03647350 188 if (!initialized)
7cb2a241
SC
189 {
190 initialized = YES;
191 wxOSXCocoaClassAddWXMethods( self );
192 }
1087c6c1
SC
193}
194
b9cf2753
KO
195- (void)textDidChange:(NSNotification *)aNotification
196{
197 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( [aNotification object] );
198 if ( impl )
199 {
200 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
201 if ( wxpeer ) {
202 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
203 event.SetEventObject( wxpeer );
204 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
205 wxpeer->HandleWindowEvent( event );
206 }
207 }
208}
209
c824c165
KO
210@end
211
e32090ba 212@implementation wxNSTextField
ffad7b0d 213
e32090ba 214+ (void)initialize
dbeddfb9 215{
e32090ba 216 static BOOL initialized = NO;
03647350 217 if (!initialized)
e32090ba
SC
218 {
219 initialized = YES;
220 wxOSXCocoaClassAddWXMethods( self );
221 }
dbeddfb9 222}
e32090ba 223
7cb2a241
SC
224- (id) initWithFrame:(NSRect) frame
225{
226 self = [super initWithFrame:frame];
227 fieldEditor = nil;
228 return self;
229}
230
af665c2d
SC
231- (void) dealloc
232{
233 [fieldEditor release];
234 [super dealloc];
235}
236
7cb2a241
SC
237- (void) setFieldEditor:(wxNSTextFieldEditor*) editor
238{
239 fieldEditor = editor;
240}
241
242- (wxNSTextFieldEditor*) fieldEditor
243{
244 return fieldEditor;
245}
246
247
c3e433b1
SC
248- (void) setEnabled:(BOOL) flag
249{
250 [super setEnabled: flag];
251
252 if (![self drawsBackground]) {
253 // Static text is drawn incorrectly when disabled.
254 // For an explanation, see
255 // http://www.cocoabuilder.com/archive/message/cocoa/2006/7/21/168028
256 if (flag) {
257 [self setTextColor: [NSColor controlTextColor]];
258 } else {
259 [self setTextColor: [NSColor secondarySelectedControlColor]];
260 }
261 }
262}
4d23a0d3 263
ffad7b0d
SC
264- (void)controlTextDidChange:(NSNotification *)aNotification
265{
6331c8c0
SC
266 wxUnusedVar(aNotification);
267 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
ffad7b0d
SC
268 if ( impl )
269 {
270 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
271 if ( wxpeer ) {
272 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
273 event.SetEventObject( wxpeer );
274 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
275 wxpeer->HandleWindowEvent( event );
276 }
277 }
278}
dbeddfb9 279
2d6aa919
KO
280typedef BOOL (*wxOSX_insertNewlineHandlerPtr)(NSView* self, SEL _cmd, NSControl *control, NSTextView* textView, SEL commandSelector);
281
282- (BOOL)control:(NSControl*)control textView:(NSTextView*)textView doCommandBySelector:(SEL)commandSelector
283{
6331c8c0
SC
284 wxUnusedVar(textView);
285 wxUnusedVar(control);
2d6aa919
KO
286 if (commandSelector == @selector(insertNewline:))
287 {
6331c8c0 288 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
2d6aa919
KO
289 if ( impl )
290 {
291 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
03647350 292 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
2d6aa919
KO
293 {
294 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
295 event.SetEventObject( wxpeer );
296 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
297 wxpeer->HandleWindowEvent( event );
298 }
299 }
300 }
03647350 301
2d6aa919
KO
302 return NO;
303}
6d109846 304
ffad7b0d
SC
305- (void)controlTextDidEndEditing:(NSNotification *)aNotification
306{
6d109846
SC
307 wxUnusedVar(aNotification);
308 wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self );
ffad7b0d
SC
309 if ( impl )
310 {
6d109846 311 impl->DoNotifyFocusEvent( false, NULL );
ffad7b0d
SC
312 }
313}
dbeddfb9
SC
314@end
315
c824c165
KO
316// wxNSTextViewControl
317
318wxNSTextViewControl::wxNSTextViewControl( wxTextCtrl *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
319{
1087c6c1
SC
320 wxNSTextScrollView* sv = (wxNSTextScrollView*) w;
321 m_scrollView = sv;
03647350 322
c824c165
KO
323 [m_scrollView setHasVerticalScroller:YES];
324 [m_scrollView setHasHorizontalScroller:NO];
325 [m_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
326 NSSize contentSize = [m_scrollView contentSize];
03647350 327
1087c6c1 328 wxNSTextView* tv = [[wxNSTextView alloc] initWithFrame: NSMakeRect(0, 0,
c824c165 329 contentSize.width, contentSize.height)];
1087c6c1
SC
330 m_textView = tv;
331 [tv setVerticallyResizable:YES];
332 [tv setHorizontallyResizable:NO];
333 [tv setAutoresizingMask:NSViewWidthSizable];
03647350 334
1087c6c1 335 [m_scrollView setDocumentView: tv];
c824c165 336
b9cf2753 337 [tv setDelegate: tv];
03647350
VZ
338
339 InstallEventHandler(tv);
c824c165
KO
340}
341
342wxNSTextViewControl::~wxNSTextViewControl()
343{
344 if (m_textView)
345 [m_textView setDelegate: nil];
346}
347
03647350 348wxString wxNSTextViewControl::GetStringValue() const
c824c165 349{
03647350 350 if (m_textView)
c824c165 351 {
f66ecdc4 352 wxString result = wxCFStringRef::AsString([m_textView string], m_wxPeer->GetFont().GetEncoding());
50b5e38d
SC
353 wxMacConvertNewlines13To10( &result ) ;
354 return result;
c824c165
KO
355 }
356 return wxEmptyString;
357}
03647350 358void wxNSTextViewControl::SetStringValue( const wxString &str)
c824c165 359{
50b5e38d
SC
360 wxString st = str;
361 wxMacConvertNewlines10To13( &st );
5f65ba36 362 wxMacEditHelper helper(m_textView);
50b5e38d 363
c824c165 364 if (m_textView)
50b5e38d 365 [m_textView setString: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
c824c165 366}
7cb2a241 367
03647350 368void wxNSTextViewControl::Copy()
c824c165
KO
369{
370 if (m_textView)
371 [m_textView copy:nil];
372
373}
374
03647350 375void wxNSTextViewControl::Cut()
c824c165
KO
376{
377 if (m_textView)
378 [m_textView cut:nil];
379}
380
03647350 381void wxNSTextViewControl::Paste()
c824c165
KO
382{
383 if (m_textView)
384 [m_textView paste:nil];
385}
386
03647350
VZ
387bool wxNSTextViewControl::CanPaste() const
388{
c824c165
KO
389 return true;
390}
391
03647350 392void wxNSTextViewControl::SetEditable(bool editable)
c824c165
KO
393{
394 if (m_textView)
395 [m_textView setEditable: editable];
396}
397
03647350 398void wxNSTextViewControl::GetSelection( long* from, long* to) const
c824c165
KO
399{
400 if (m_textView)
401 {
402 NSRange range = [m_textView selectedRange];
403 *from = range.location;
404 *to = range.location + range.length;
405 }
406}
407
408void wxNSTextViewControl::SetSelection( long from , long to )
409{
50b5e38d
SC
410 long textLength = [[m_textView string] length];
411 if ((from == -1) && (to == -1))
412 {
413 from = 0 ;
414 to = textLength ;
415 }
416 else
417 {
418 from = wxMin(textLength,wxMax(from,0)) ;
419 if ( to == -1 )
420 to = textLength;
421 else
422 to = wxMax(0,wxMin(textLength,to)) ;
423 }
424
2d6aa919
KO
425 NSRange selrange = NSMakeRange(from, to-from);
426 [m_textView setSelectedRange:selrange];
427 [m_textView scrollRangeToVisible:selrange];
c824c165
KO
428}
429
03647350 430void wxNSTextViewControl::WriteText(const wxString& str)
c824c165 431{
50b5e38d
SC
432 wxString st = str;
433 wxMacConvertNewlines10To13( &st );
5f65ba36 434 wxMacEditHelper helper(m_textView);
715824d5
SC
435 NSEvent* formerEvent = m_lastKeyDownEvent;
436 m_lastKeyDownEvent = nil;
50b5e38d 437 [m_textView insertText:wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
715824d5 438 m_lastKeyDownEvent = formerEvent;
c824c165
KO
439}
440
63bcc669 441void wxNSTextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
f95dd972
SC
442{
443 if ([m_textView respondsToSelector:@selector(setFont:)])
444 [m_textView setFont: font.OSXGetNSFont()];
445}
446
16671f22
KO
447bool wxNSTextViewControl::GetStyle(long position, wxTextAttr& style)
448{
b9cf2753
KO
449 if (m_textView && position >=0)
450 {
451 NSFont* font = NULL;
452 NSColor* bgcolor = NULL;
453 NSColor* fgcolor = NULL;
454 // NOTE: It appears that other platforms accept GetStyle with the position == length
455 // but that NSTextStorage does not accept length as a valid position.
456 // Therefore we return the default control style in that case.
457 if (position < [[m_textView string] length])
458 {
459 NSTextStorage* storage = [m_textView textStorage];
460 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
461 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
462 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
463 }
464 else
465 {
466 NSDictionary* attrs = [m_textView typingAttributes];
467 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
468 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
469 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
470 }
471
16671f22
KO
472 if (font)
473 style.SetFont(wxFont(font));
b9cf2753 474
16671f22
KO
475 if (bgcolor)
476 style.SetBackgroundColour(wxColour(bgcolor));
b9cf2753
KO
477
478 if (fgcolor)
479 style.SetTextColour(wxColour(fgcolor));
16671f22
KO
480 return true;
481 }
482
483 return false;
484}
485
486void wxNSTextViewControl::SetStyle(long start,
487 long end,
488 const wxTextAttr& style)
489{
490 if (m_textView) {
491 NSRange range = NSMakeRange(start, end-start);
b9cf2753
KO
492 if (start == -1 && end == -1)
493 range = [m_textView selectedRange];
494
16671f22
KO
495 NSTextStorage* storage = [m_textView textStorage];
496
497 wxFont font = style.GetFont();
b9cf2753
KO
498 if (style.HasFont() && font.IsOk())
499 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
16671f22
KO
500
501 wxColour bgcolor = style.GetBackgroundColour();
b9cf2753 502 if (style.HasBackgroundColour() && bgcolor.IsOk())
16671f22
KO
503 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
504
505 wxColour fgcolor = style.GetTextColour();
b9cf2753 506 if (style.HasTextColour() && fgcolor.IsOk())
16671f22
KO
507 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
508 }
509}
f95dd972 510
b9cf2753
KO
511void wxNSTextViewControl::CheckSpelling(bool check)
512{
513 if (m_textView)
514 [m_textView setContinuousSpellCheckingEnabled: check];
515}
516
9ab7ff53
KO
517wxSize wxNSTextViewControl::GetBestSize() const
518{
519 if (m_textView && [m_textView layoutManager])
520 {
521 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
522 wxSize size = wxSize(rect.size.width, rect.size.height);
523 size.x += [m_textView textContainerInset].width;
524 size.y += [m_textView textContainerInset].height;
525 return size;
526 }
c8fdb345 527 return wxSize(0,0);
9ab7ff53
KO
528}
529
c824c165
KO
530// wxNSTextFieldControl
531
c84030e0 532wxNSTextFieldControl::wxNSTextFieldControl( wxWindow *wxPeer, WXWidget w ) : wxWidgetCocoaImpl(wxPeer, w)
1e181c7a 533{
c8fdb345
SC
534 NSTextField wxOSX_10_6_AND_LATER(<NSTextFieldDelegate>) *tf = (NSTextField*) w;
535 m_textField = tf;
536 [m_textField setDelegate: tf];
50b5e38d 537 m_selStart = m_selEnd = 0;
7cb2a241 538 m_hasEditor = [w isKindOfClass:[NSTextField class]];
1e181c7a
SC
539}
540
541wxNSTextFieldControl::~wxNSTextFieldControl()
542{
c824c165
KO
543 if (m_textField)
544 [m_textField setDelegate: nil];
1e181c7a
SC
545}
546
03647350 547wxString wxNSTextFieldControl::GetStringValue() const
1e181c7a 548{
f66ecdc4 549 return wxCFStringRef::AsString([m_textField stringValue], m_wxPeer->GetFont().GetEncoding());
1e181c7a 550}
50b5e38d 551
03647350 552void wxNSTextFieldControl::SetStringValue( const wxString &str)
1e181c7a 553{
5f65ba36 554 wxMacEditHelper helper(m_textField);
e32090ba 555 [m_textField setStringValue: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
1e181c7a 556}
50b5e38d 557
03647350 558void wxNSTextFieldControl::Copy()
1e181c7a 559{
e32090ba
SC
560 NSText* editor = [m_textField currentEditor];
561 if ( editor )
562 {
563 [editor copy:nil];
564 }
1e181c7a
SC
565}
566
03647350 567void wxNSTextFieldControl::Cut()
1e181c7a 568{
e32090ba
SC
569 NSText* editor = [m_textField currentEditor];
570 if ( editor )
571 {
572 [editor cut:nil];
573 }
1e181c7a
SC
574}
575
03647350 576void wxNSTextFieldControl::Paste()
1e181c7a 577{
e32090ba
SC
578 NSText* editor = [m_textField currentEditor];
579 if ( editor )
580 {
581 [editor paste:nil];
582 }
1e181c7a
SC
583}
584
03647350
VZ
585bool wxNSTextFieldControl::CanPaste() const
586{
e32090ba 587 return true;
1e181c7a
SC
588}
589
03647350 590void wxNSTextFieldControl::SetEditable(bool editable)
1e181c7a 591{
e32090ba 592 [m_textField setEditable:editable];
1e181c7a
SC
593}
594
03647350 595void wxNSTextFieldControl::GetSelection( long* from, long* to) const
1e181c7a 596{
e32090ba
SC
597 NSText* editor = [m_textField currentEditor];
598 if ( editor )
599 {
600 NSRange range = [editor selectedRange];
601 *from = range.location;
602 *to = range.location + range.length;
603 }
50b5e38d
SC
604 else
605 {
606 *from = m_selStart;
607 *to = m_selEnd;
608 }
1e181c7a
SC
609}
610
611void wxNSTextFieldControl::SetSelection( long from , long to )
612{
50b5e38d
SC
613 long textLength = [[m_textField stringValue] length];
614 if ((from == -1) && (to == -1))
615 {
616 from = 0 ;
617 to = textLength ;
618 }
619 else
620 {
621 from = wxMin(textLength,wxMax(from,0)) ;
622 if ( to == -1 )
623 to = textLength;
624 else
625 to = wxMax(0,wxMin(textLength,to)) ;
626 }
627
e32090ba
SC
628 NSText* editor = [m_textField currentEditor];
629 if ( editor )
630 {
631 [editor setSelectedRange:NSMakeRange(from, to-from)];
632 }
50b5e38d
SC
633 else
634 {
635 m_selStart = from;
636 m_selEnd = to;
637 }
1e181c7a
SC
638}
639
03647350 640void wxNSTextFieldControl::WriteText(const wxString& str)
1e181c7a 641{
715824d5
SC
642 NSEvent* formerEvent = m_lastKeyDownEvent;
643 m_lastKeyDownEvent = nil;
50b5e38d
SC
644 NSText* editor = [m_textField currentEditor];
645 if ( editor )
646 {
5f65ba36 647 wxMacEditHelper helper(m_textField);
50b5e38d
SC
648 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
649 }
650 else
651 {
652 wxString val = GetStringValue() ;
653 long start , end ;
654 GetSelection( &start , &end ) ;
655 val.Remove( start , end - start ) ;
656 val.insert( start , str ) ;
657 SetStringValue( val ) ;
658 SetSelection( start + str.length() , start + str.length() ) ;
659 }
715824d5 660 m_lastKeyDownEvent = formerEvent;
1e181c7a 661}
dbeddfb9 662
03647350 663void wxNSTextFieldControl::controlAction(WXWidget WXUNUSED(slf),
6331c8c0 664 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
e32090ba
SC
665{
666 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
03647350 667 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
e32090ba
SC
668 {
669 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
670 event.SetEventObject( wxpeer );
671 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
672 wxpeer->HandleWindowEvent( event );
673 }
674}
675
676//
677//
678//
679
03647350
VZ
680wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
681 wxWindowMac* WXUNUSED(parent),
682 wxWindowID WXUNUSED(id),
63bcc669 683 const wxString& WXUNUSED(str),
03647350 684 const wxPoint& pos,
dbeddfb9 685 const wxSize& size,
03647350 686 long style,
6331c8c0 687 long WXUNUSED(extraStyle))
dbeddfb9 688{
dbeddfb9 689 NSRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
c824c165 690 wxWidgetCocoaImpl* c = NULL;
03647350 691
c824c165 692 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
b15f9375 693 {
1087c6c1
SC
694 wxNSTextScrollView* v = nil;
695 v = [[wxNSTextScrollView alloc] initWithFrame:r];
c824c165 696 c = new wxNSTextViewControl( wxpeer, v );
c824c165 697 }
03647350 698 else
c824c165 699 {
6331c8c0 700 NSTextField* v = nil;
c824c165
KO
701 if ( style & wxTE_PASSWORD )
702 v = [[wxNSSecureTextField alloc] initWithFrame:r];
703 else
704 v = [[wxNSTextField alloc] initWithFrame:r];
03647350 705
c824c165
KO
706 if ( style & wxNO_BORDER )
707 {
708 // FIXME: How can we remove the native control's border?
709 // setBordered is separate from the text ctrl's border.
710 }
03647350 711
b15f9375
SC
712 [v setBezeled:NO];
713 [v setBordered:NO];
03647350 714
c824c165 715 c = new wxNSTextFieldControl( wxpeer, v );
b15f9375 716 }
03647350 717
dbeddfb9
SC
718 return c;
719}
720
721
722#endif // wxUSE_TEXTCTRL