1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/textctrl.mm
4 // Author: Stefan Csomor
5 // Modified by: Ryan Norton (MLTE GetLineLength and GetLineText)
7 // RCS-ID: $Id: textctrl.cpp 54820 2008-07-29 20:04:11Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
16 #include "wx/textctrl.h"
23 #include "wx/button.h"
25 #include "wx/settings.h"
26 #include "wx/msgdlg.h"
27 #include "wx/toplevel.h"
31 #include <sys/types.h>
37 #if wxUSE_STD_IOSTREAM
45 #include "wx/filefn.h"
46 #include "wx/sysopt.h"
47 #include "wx/thread.h"
49 #include "wx/osx/private.h"
50 #include "wx/osx/iphone/private/textimpl.h"
52 // currently for some reasong the UITextField leads to a recursion when the keyboard should be shown, so let's leave the code
53 // in case this gets resolved...
55 #define wxOSX_IPHONE_USE_TEXTFIELD 0
60 wxMacEditHelper( UITextView* textView )
62 m_textView = textView;
66 m_formerState = [textView isEditable];
67 [textView setEditable:YES];
74 [m_textView setEditable:m_formerState];
79 UITextView* m_textView;
82 #if wxOSX_IPHONE_USE_TEXTFIELD
84 @interface wxUITextField : UITextField<UITextFieldDelegate>
90 @implementation wxNSSecureTextField
94 static BOOL initialized = NO;
98 wxOSXIPhoneClassAddWXMethods( self );
102 - (void)controlTextDidChange:(NSNotification *)aNotification
104 wxUnusedVar(aNotification);
105 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
107 impl->controlTextDidChange();
110 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
112 wxUnusedVar(aNotification);
113 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
116 impl->DoNotifyFocusEvent( false, NULL );
122 @implementation wxUITextFieldEditor
124 - (void) keyDown:(NSEvent*) event
126 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
127 lastKeyDownEvent = event;
128 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
129 [super keyDown:event];
130 lastKeyDownEvent = nil;
133 - (void) keyUp:(NSEvent*) event
135 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
136 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
140 - (void) flagsChanged:(NSEvent*) event
142 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
143 if ( impl == NULL || !impl->DoHandleKeyEvent(event) )
144 [super flagsChanged:event];
147 - (BOOL) performKeyEquivalent:(NSEvent*) event
149 BOOL retval = [super performKeyEquivalent:event];
153 - (void) insertText:(id) str
155 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( (WXWidget) [self delegate] );
156 if ( impl == NULL || lastKeyDownEvent==nil || !impl->DoHandleCharEvent(lastKeyDownEvent, str) )
158 [super insertText:str];
164 @implementation wxUITextField
168 static BOOL initialized = NO;
172 wxOSXIPhoneClassAddWXMethods( self );
177 - (void)controlTextDidChange:(NSNotification *)aNotification
179 wxUnusedVar(aNotification);
180 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
182 impl->controlTextDidChange();
186 - (BOOL)textFieldShouldReturn:(UITextField *)textField
188 wxUnusedVar(textField);
190 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
193 wxWindow* wxpeer = (wxWindow*) impl->GetWXPeer();
194 if ( wxpeer && wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER )
196 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
197 event.SetEventObject( wxpeer );
198 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
199 wxpeer->HandleWindowEvent( event );
206 - (void)controlTextDidEndEditing:(NSNotification *)aNotification
208 wxUnusedVar(aNotification);
209 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
212 impl->DoNotifyFocusEvent( false, NULL );
219 @interface wxUITextViewDelegate : NSObject<UITextViewDelegate>
223 - (void)textViewDidChange:(UITextView *)textView;
224 - (void)textViewDidBeginEditing:(UITextView *)textView;
225 - (void)textViewDidEndEditing:(UITextView *)textView;
226 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
230 @implementation wxUITextViewDelegate
232 - (void)textViewDidChange:(UITextView *)textView
234 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
236 impl->controlTextDidChange();
239 - (void)textViewDidBeginEditing:(UITextView *)textView
241 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
243 impl->DoNotifyFocusEvent(true, NULL);
246 - (void)textViewDidEndEditing:(UITextView *)textView
248 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
250 impl->DoNotifyFocusEvent(false, NULL);
253 - (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
255 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( textView );
258 if ( !impl->GetWXPeer()->HasFlag(wxTE_MULTILINE) && [text isEqualToString:@"\n"])
260 [textView resignFirstResponder];
271 // wxUITextViewControl
274 wxUITextViewControl::wxUITextViewControl( wxTextCtrl *wxPeer, UITextView* v) : wxWidgetIPhoneImpl(wxPeer, v)
277 wxUITextViewDelegate* d = [[wxUITextViewDelegate alloc] init];
279 [m_textView setDelegate:d];
282 wxUITextViewControl::~wxUITextViewControl()
286 wxUITextViewDelegate* d = [m_textView delegate];
287 [m_textView setDelegate: nil];
292 bool wxUITextViewControl::CanFocus() const
297 wxString wxUITextViewControl::GetStringValue() const
301 wxString result = wxCFStringRef::AsString([m_textView text], m_wxPeer->GetFont().GetEncoding());
302 wxMacConvertNewlines13To10( &result ) ;
305 return wxEmptyString;
308 void wxUITextViewControl::SetStringValue( const wxString &str)
311 wxMacConvertNewlines10To13( &st );
312 wxMacEditHelper helper(m_textView);
315 [m_textView setText: wxCFStringRef( st , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
318 void wxUITextViewControl::Copy()
321 [m_textView copy:nil];
325 void wxUITextViewControl::Cut()
328 [m_textView cut:nil];
331 void wxUITextViewControl::Paste()
334 [m_textView paste:nil];
337 bool wxUITextViewControl::CanPaste() const
342 void wxUITextViewControl::SetEditable(bool editable)
345 [m_textView setEditable: editable];
348 void wxUITextViewControl::GetSelection( long* from, long* to) const
352 NSRange range = [m_textView selectedRange];
353 *from = range.location;
354 *to = range.location + range.length;
358 void wxUITextViewControl::SetSelection( long from , long to )
360 long textLength = [[m_textView text] length];
361 if ((from == -1) && (to == -1))
368 from = wxMin(textLength,wxMax(from,0)) ;
372 to = wxMax(0,wxMin(textLength,to)) ;
375 NSRange selrange = NSMakeRange(from, to-from);
376 [m_textView setSelectedRange:selrange];
377 [m_textView scrollRangeToVisible:selrange];
380 void wxUITextViewControl::WriteText(const wxString& str)
383 wxMacConvertNewlines10To13( &st );
384 wxMacEditHelper helper(m_textView);
386 wxCFStringRef insert( st , m_wxPeer->GetFont().GetEncoding() );
387 NSMutableString* subst = [NSMutableString stringWithString:[m_textView text]];
388 [subst replaceCharactersInRange:[m_textView selectedRange] withString:insert.AsNSString()];
390 [m_textView setText:subst];
393 void wxUITextViewControl::SetFont( const wxFont & font , const wxColour& WXUNUSED(foreground) , long WXUNUSED(windowStyle), bool WXUNUSED(ignoreBlack) )
395 if ([m_textView respondsToSelector:@selector(setFont:)])
396 [m_textView setFont: font.OSXGetUIFont()];
399 bool wxUITextViewControl::GetStyle(long position, wxTextAttr& style)
401 if (m_textView && position >=0)
404 NSColor* bgcolor = NULL;
405 NSColor* fgcolor = NULL;
406 // NOTE: It appears that other platforms accept GetStyle with the position == length
407 // but that UITextStorage does not accept length as a valid position.
408 // Therefore we return the default control style in that case.
410 if (position < [[m_textView string] length])
412 UITextStorage* storage = [m_textView textStorage];
413 font = [[storage attribute:NSFontAttributeName atIndex:position effectiveRange:NULL] autorelease];
414 bgcolor = [[storage attribute:NSBackgroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
415 fgcolor = [[storage attribute:NSForegroundColorAttributeName atIndex:position effectiveRange:NULL] autorelease];
419 NSDictionary* attrs = [m_textView typingAttributes];
420 font = [[attrs objectForKey:NSFontAttributeName] autorelease];
421 bgcolor = [[attrs objectForKey:NSBackgroundColorAttributeName] autorelease];
422 fgcolor = [[attrs objectForKey:NSForegroundColorAttributeName] autorelease];
427 style.SetFont(wxFont(font));
430 style.SetBackgroundColour(wxColour(bgcolor));
433 style.SetTextColour(wxColour(fgcolor));
441 void wxUITextViewControl::SetStyle(long start,
443 const wxTextAttr& style)
446 NSRange range = NSMakeRange(start, end-start);
447 if (start == -1 && end == -1)
448 range = [m_textView selectedRange];
450 UITextStorage* storage = [m_textView textStorage];
452 wxFont font = style.GetFont();
453 if (style.HasFont() && font.IsOk())
454 [storage addAttribute:NSFontAttributeName value:font.OSXGetNSFont() range:range];
456 wxColour bgcolor = style.GetBackgroundColour();
457 if (style.HasBackgroundColour() && bgcolor.IsOk())
458 [storage addAttribute:NSBackgroundColorAttributeName value:bgcolor.OSXGetNSColor() range:range];
460 wxColour fgcolor = style.GetTextColour();
461 if (style.HasTextColour() && fgcolor.IsOk())
462 [storage addAttribute:NSForegroundColorAttributeName value:fgcolor.OSXGetNSColor() range:range];
467 void wxUITextViewControl::CheckSpelling(bool check)
471 wxSize wxUITextViewControl::GetBestSize() const
478 if (m_textView && [m_textView layoutManager])
480 NSRect rect = [[m_textView layoutManager] usedRectForTextContainer: [m_textView textContainer]];
481 wxSize size = wxSize(rect.size.width, rect.size.height);
482 size.x += [m_textView textContainerInset].width;
483 size.y += [m_textView textContainerInset].height;
491 #if wxOSX_IPHONE_USE_TEXTFIELD
494 // wxUITextFieldControl
497 wxUITextFieldControl::wxUITextFieldControl( wxWindow *wxPeer, UITextField* w ) : wxWidgetIPhoneImpl(wxPeer, w)
499 UITextField wxOSX_10_6_AND_LATER(<UITextFieldDelegate>) *tf = (UITextField*) w;
501 [m_textField setDelegate: tf];
502 m_selStart = m_selEnd = 0;
505 wxUITextFieldControl::~wxUITextFieldControl()
508 [m_textField setDelegate: nil];
511 wxString wxUITextFieldControl::GetStringValue() const
513 return wxCFStringRef::AsString([m_textField text], m_wxPeer->GetFont().GetEncoding());
516 void wxUITextFieldControl::SetStringValue( const wxString &str)
518 // wxMacEditHelper helper(m_textField);
519 [m_textField setText: wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
522 void wxUITextFieldControl::Copy()
524 [m_textField copy:nil];
527 void wxUITextFieldControl::Cut()
529 [m_textField cut:nil];
532 void wxUITextFieldControl::Paste()
534 [m_textField paste:nil];
537 bool wxUITextFieldControl::CanPaste() const
542 void wxUITextFieldControl::SetEditable(bool editable)
546 void wxUITextFieldControl::GetSelection( long* from, long* to) const
552 void wxUITextFieldControl::SetSelection( long from , long to )
554 long textLength = [[m_textField text] length];
555 if ((from == -1) && (to == -1))
562 from = wxMin(textLength,wxMax(from,0)) ;
566 to = wxMax(0,wxMin(textLength,to)) ;
573 void wxUITextFieldControl::WriteText(const wxString& str)
576 NSEvent* formerEvent = m_lastKeyDownEvent;
577 UIText* editor = [m_textField currentEditor];
580 wxMacEditHelper helper(m_textField);
581 [editor insertText:wxCFStringRef( str , m_wxPeer->GetFont().GetEncoding() ).AsNSString()];
586 wxString val = GetStringValue() ;
588 GetSelection( &start , &end ) ;
589 val.Remove( start , end - start ) ;
590 val.insert( start , str ) ;
591 SetStringValue( val ) ;
592 SetSelection( start + str.length() , start + str.length() ) ;
595 m_lastKeyDownEvent = formerEvent;
599 void wxUITextFieldControl::controlAction(WXWidget WXUNUSED(slf),
600 void* WXUNUSED(_cmd), void *WXUNUSED(sender))
602 wxWindow* wxpeer = (wxWindow*) GetWXPeer();
603 if ( wxpeer && (wxpeer->GetWindowStyle() & wxTE_PROCESS_ENTER) )
605 wxCommandEvent event(wxEVT_COMMAND_TEXT_ENTER, wxpeer->GetId());
606 event.SetEventObject( wxpeer );
607 event.SetString( static_cast<wxTextCtrl*>(wxpeer)->GetValue() );
608 wxpeer->HandleWindowEvent( event );
618 wxWidgetImplType* wxWidgetImpl::CreateTextControl( wxTextCtrl* wxpeer,
619 wxWindowMac* WXUNUSED(parent),
620 wxWindowID WXUNUSED(id),
625 long WXUNUSED(extraStyle))
627 CGRect r = wxOSXGetFrameForControl( wxpeer, pos , size ) ;
628 wxWidgetIPhoneImpl* c = NULL;
629 wxTextWidgetImpl* t = NULL;
630 id<UITextInputTraits> tv = nil;
632 #if wxOSX_IPHONE_USE_TEXTFIELD
633 if ( style & wxTE_MULTILINE || style & wxTE_RICH || style & wxTE_RICH2 )
636 UITextView * v = nil;
637 v = [[UITextView alloc] initWithFrame:r];
640 wxUITextViewControl* tc = new wxUITextViewControl( wxpeer, v );
644 #if wxOSX_IPHONE_USE_TEXTFIELD
647 UITextField* v = [[UITextField alloc] initWithFrame:r];
650 v.textColor = [UIColor blackColor];
651 v.font = [UIFont systemFontOfSize:17.0];
652 v.placeholder = @"<enter text>";
653 v.backgroundColor = [UIColor whiteColor];
655 v.clearButtonMode = UITextFieldViewModeWhileEditing; // has a clear 'x' button to the right
657 v.delegate = v; // let us be the delegate so we know when the keyboard's "Done" button is pressed
659 [v setBorderStyle:UITextBorderStyleBezel];
660 if ( style & wxNO_BORDER )
661 v.borderStyle = UITextBorderStyleNone;
663 wxUITextFieldControl* tc = new wxUITextFieldControl( wxpeer, v );
669 if ( style & wxTE_PASSWORD )
670 [tv setSecureTextEntry:YES];
672 if ( !(style & wxTE_MULTILINE) )
674 [tv setAutocorrectionType:UITextAutocorrectionTypeNo];
675 [tv setReturnKeyType:UIReturnKeyDone];
677 [tv setKeyboardType:UIKeyboardTypeDefault];
679 t->SetStringValue(str);
685 #endif // wxUSE_TEXTCTRL